Skip to content

Commit

Permalink
core/opal: Emergency stack for re-entry
Browse files Browse the repository at this point in the history
This detects OPAL being re-entered by the OS, and switches to an
emergency stack if it was. This protects the firmware's main stack
from re-entrancy and allows the OS to use NMI facilities for crash
/ debug functionality.

Further nested re-entry will destroy the previous emergency stack
and prevent returning, but those should be rare cases.

This stack is sized at 16kB, which doubles the size of CPU stacks,
so as not to introduce a regression in primary stack size. The 16kB
stack originally had a 4kB machine check stack at the top, which was
removed by 80eee19 ("opal: Remove machine check interrupt patching
in OPAL."). So it is possible the size could be tightened again, but
that would require further analysis.

Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
Signed-off-by: Stewart Smith <stewart@linux.ibm.com>
  • Loading branch information
npiggin authored and stewartsmith committed Apr 19, 2018
1 parent 8514e4d commit 3fdd262
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 16 deletions.
22 changes: 18 additions & 4 deletions asm/head.S
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@

#define PPC_INST_STOP .long 0x4c0002e4

#define GET_STACK(stack_reg,pir_reg) \
sldi stack_reg,pir_reg,STACK_SHIFT; \
addis stack_reg,stack_reg,CPU_STACKS_OFFSET@ha; \
#define GET_STACK(stack_reg,pir_reg) \
sldi stack_reg,pir_reg,STACK_SHIFT; \
addis stack_reg,stack_reg,CPU_STACKS_OFFSET@ha; \
addi stack_reg,stack_reg,CPU_STACKS_OFFSET@l;

#define GET_CPU() \
#define GET_EMERGENCY_STACK(stack_reg,pir_reg) \
sldi stack_reg,pir_reg,STACK_SHIFT; \
addis stack_reg,stack_reg,EMERGENCY_CPU_STACKS_OFFSET@ha; \
addi stack_reg,stack_reg,EMERGENCY_CPU_STACKS_OFFSET@l;

#define GET_CPU() \
clrrdi %r13,%r1,STACK_SHIFT

#define SAVE_GPR(reg,sp) std %r##reg,STACK_GPR##reg(sp)
Expand Down Expand Up @@ -1013,8 +1018,17 @@ opal_entry:
b 1b

4: /* Quiesce protocol done, get our per CPU stack */
/* Emergency stack if we have re-entered OPAL */
lwz %r11,CPUTHREAD_IN_OPAL_CALL(%r12)
cmpwi %r11,1

mfspr %r12,SPR_PIR
beq 5f
GET_STACK(%r12,%r12)
b 6f
5:
GET_EMERGENCY_STACK(%r12,%r12)
6:
stdu %r12,-STACK_FRAMESIZE(%r12)

/* Save caller r1, establish new r1 */
Expand Down
13 changes: 8 additions & 5 deletions core/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,18 @@ unsigned long __attrconst cpu_stack_bottom(unsigned int pir)

unsigned long __attrconst cpu_stack_top(unsigned int pir)
{
/* This is the top of the MC stack which is above the normal
* stack, which means a SP between cpu_stack_bottom() and
* cpu_stack_top() can either be a normal stack pointer or
* a Machine Check stack pointer
*/
/* This is the top of the normal stack. */
return ((unsigned long)&cpu_stacks[pir]) +
NORMAL_STACK_SIZE - STACK_TOP_GAP;
}

unsigned long __attrconst cpu_emergency_stack_top(unsigned int pir)
{
/* This is the top of the emergency stack, above the normal stack. */
return ((unsigned long)&cpu_stacks[pir]) +
NORMAL_STACK_SIZE + EMERGENCY_STACK_SIZE - STACK_TOP_GAP;
}

static void cpu_wake(struct cpu_thread *cpu)
{
/* Is it idle ? If not, no need to wake */
Expand Down
13 changes: 11 additions & 2 deletions core/stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void ___print_backtrace(unsigned int pir, struct bt_entry *entries,
static char bt_text_buf[4096];
int i, l = 0, max;
char *buf = out_buf;
unsigned long bottom, top, tbot, ttop;
unsigned long bottom, top, normal_top, tbot, ttop;
char mark;

if (!out_buf) {
Expand All @@ -81,14 +81,17 @@ void ___print_backtrace(unsigned int pir, struct bt_entry *entries,
max = *len - 1;

bottom = cpu_stack_bottom(pir);
top = cpu_stack_top(pir);
normal_top = cpu_stack_top(pir);
top = cpu_emergency_stack_top(pir);
tbot = SKIBOOT_BASE;
ttop = (unsigned long)&_etext;

l += snprintf(buf, max, "CPU %04x Backtrace:\n", pir);
for (i = 0; i < count && l < max; i++) {
if (entries->sp < bottom || entries->sp > top)
mark = '!';
else if (entries->sp > normal_top)
mark = 'E';
else if (entries->pc < tbot || entries->pc > ttop)
mark = '*';
else
Expand Down Expand Up @@ -161,6 +164,12 @@ void __nomcount __mcount_stack_check(uint64_t sp, uint64_t lr)
int64_t mark = sp - bot;
uint64_t top = base + NORMAL_STACK_SIZE;

/*
* Don't check the emergency stack just yet.
*/
if (c->in_opal_call > 1)
return;

/*
* Don't re-enter on this CPU or don't enter at all if somebody
* has spotted an overflow
Expand Down
1 change: 1 addition & 0 deletions include/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ static inline void cpu_give_self_os(void)

extern unsigned long __attrconst cpu_stack_bottom(unsigned int pir);
extern unsigned long __attrconst cpu_stack_top(unsigned int pir);
extern unsigned long __attrconst cpu_emergency_stack_top(unsigned int pir);

extern void cpu_idle_job(void);
extern void cpu_idle_delay(unsigned long delay);
Expand Down
10 changes: 6 additions & 4 deletions include/mem-map.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
*/
#define SKIBOOT_BASE 0x30000000

/* Stack size set to 16K, some of it will be used for
* machine check (see stack.h)
/* Stack size set to 32K, 16K for general stack and 16K for an emergency
* stack.
*/
#define STACK_SHIFT 14
#define STACK_SHIFT 15
#define STACK_SIZE (1 << STACK_SHIFT)

/* End of the exception region we copy from 0x0. 0x0-0x100 will have
Expand Down Expand Up @@ -109,7 +109,9 @@
* each stack is STACK_SIZE in size (naturally aligned power of
* two) and the bottom of the stack contains the cpu thread
* structure for the processor, so it can be obtained by a simple
* bit mask from the stack pointer.
* bit mask from the stack pointer. Within the CPU stack is divided
* into a normal and emergency stack to cope with a single level of
* re-entrancy.
*
* The size of this array is dynamically determined at boot time
*/
Expand Down
9 changes: 8 additions & 1 deletion include/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@
#define STACK_TOP_GAP 0x100

/* Remaining stack space (gap included) */
#define NORMAL_STACK_SIZE STACK_SIZE
#define NORMAL_STACK_SIZE (STACK_SIZE/2)

/* Emergency (re-entry) stack size */
#define EMERGENCY_STACK_SIZE (STACK_SIZE/2)

/* Offset to get to normal CPU stacks */
#define CPU_STACKS_OFFSET (CPU_STACKS_BASE + \
NORMAL_STACK_SIZE - STACK_TOP_GAP)

/* Offset to get to emergency CPU stacks */
#define EMERGENCY_CPU_STACKS_OFFSET (CPU_STACKS_BASE + NORMAL_STACK_SIZE + \
EMERGENCY_STACK_SIZE - STACK_TOP_GAP)

/* Gap below the stack. If our stack checker sees the stack below that
* gap, it will flag a stack overflow
*/
Expand Down

0 comments on commit 3fdd262

Please sign in to comment.