diff options
author | Benjamin Herrenschmidt <benh@kernel.crashing.org> | 2014-11-18 14:43:47 +1100 |
---|---|---|
committer | Benjamin Herrenschmidt <benh@kernel.crashing.org> | 2014-11-18 14:43:47 +1100 |
commit | 20410452b956edaf7790803d441768fbf707b36c (patch) | |
tree | e72d2826778c8c51c91641788bebb5892379d9be | |
parent | efa6bbc48ff549cd70352106b8d93f6e156295f5 (diff) | |
download | skiboot-20410452b956edaf7790803d441768fbf707b36c.zip skiboot-20410452b956edaf7790803d441768fbf707b36c.tar.gz skiboot-20410452b956edaf7790803d441768fbf707b36c.tar.bz2 |
Rename backtrace.c to stack.c and move stack related bits
... from util.c to stack.c
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
-rw-r--r-- | core/Makefile.inc | 2 | ||||
-rw-r--r-- | core/stack.c (renamed from core/backtrace.c) | 82 | ||||
-rw-r--r-- | core/utils.c | 81 |
3 files changed, 83 insertions, 82 deletions
diff --git a/core/Makefile.inc b/core/Makefile.inc index 07dfe75..615b27b 100644 --- a/core/Makefile.inc +++ b/core/Makefile.inc @@ -1,7 +1,7 @@ # -*-Makefile-*- SUBDIRS += core -CORE_OBJS = relocate.o console.o backtrace.o init.o chip.o mem_region.o +CORE_OBJS = relocate.o console.o stack.o init.o chip.o mem_region.o CORE_OBJS += malloc.o lock.o cpu.o utils.o fdt.o opal.o interrupts.o CORE_OBJS += timebase.o opal-msg.o pci.o pci-opal.o fast-reboot.o CORE_OBJS += device.o exceptions.o trace.o affinity.o vpd.o diff --git a/core/backtrace.c b/core/stack.c index 9b75186..0d8a7eb 100644 --- a/core/backtrace.c +++ b/core/stack.c @@ -100,3 +100,85 @@ void backtrace(void) __backtrace(bt_buf, &ents); __print_backtrace(mfspr(SPR_PIR), bt_buf, ents, NULL, NULL); } + +void __noreturn __nomcount __stack_chk_fail(void); +void __noreturn __nomcount __stack_chk_fail(void) +{ + prlog(PR_EMERG, "Stack corruption detected !\n"); + abort(); +} + +#ifdef STACK_CHECK_ENABLED + +void __nomcount __mcount_stack_check(uint64_t sp, uint64_t lr); +void __nomcount __mcount_stack_check(uint64_t sp, uint64_t lr) +{ + struct cpu_thread *c = this_cpu(); + uint64_t base = (uint64_t)c; + uint64_t bot = base + sizeof(struct cpu_thread); + int64_t mark = sp - bot; + uint64_t top = base + NORMAL_STACK_SIZE; + + /* + * Don't re-enter on this CPU or don't enter at all if somebody + * has spotted an overflow + */ + if (c->in_mcount) + return; + c->in_mcount = true; + + /* Capture lowest stack for this thread */ + if (mark < c->stack_bot_mark) { + c->stack_bot_mark = mark; + c->stack_bot_pc = lr; + c->stack_bot_tok = c->current_token; + } + + /* Stack is within bounds ? check for warning and bail */ + if (sp >= (bot + STACK_SAFETY_GAP) && sp < top) { + if (mark < STACK_WARNING_GAP) { + prlog(PR_EMERG, "CPU %04x Stack usage danger !" + " pc=%08llx sp=%08llx (gap=%lld) token=%lld\n", + c->pir, lr, sp, mark, c->current_token); + backtrace(); + } + c->in_mcount = false; + return; + } + + prlog(PR_EMERG, "CPU %04x Stack overflow detected !" + " pc=%08llx sp=%08llx (gap=%lld) token=%lld\n", + c->pir, lr, sp, mark, c->current_token); + abort(); +} + +static int64_t lowest_stack_mark = LONG_MAX; +static struct lock stack_check_lock = LOCK_UNLOCKED; + +void check_stacks(void) +{ + struct cpu_thread *c; + uint64_t lmark, lpc, ltok; + int found = -1; + + for_each_cpu(c) { + if (!c->stack_bot_mark || + c->stack_bot_mark >= lowest_stack_mark) + continue; + lock(&stack_check_lock); + if (c->stack_bot_mark >= lowest_stack_mark) { + unlock(&stack_check_lock); + continue; + } + lmark = lowest_stack_mark = c->stack_bot_mark; + lpc = c->stack_bot_pc; + ltok = c->stack_bot_tok; + found = c->pir; + unlock(&stack_check_lock); + } + if (found >= 0) + prlog(PR_NOTICE, "CPU %04x lowest stack mark %lld bytes left" + " pc=%08llx token=%lld\n", found, lmark, lpc, ltok); +} + +#endif /* STACK_CHECK_ENABLED */ diff --git a/core/utils.c b/core/utils.c index 1b1a8bf..63046fc 100644 --- a/core/utils.c +++ b/core/utils.c @@ -68,84 +68,3 @@ char __attrconst tohex(uint8_t nibble) return __tohex[nibble]; } -void __noreturn __nomcount __stack_chk_fail(void); -void __noreturn __nomcount __stack_chk_fail(void) -{ - prlog(PR_EMERG, "Stack corruption detected !\n"); - abort(); -} - -#ifdef STACK_CHECK_ENABLED - -void __nomcount __mcount_stack_check(uint64_t sp, uint64_t lr); -void __nomcount __mcount_stack_check(uint64_t sp, uint64_t lr) -{ - struct cpu_thread *c = this_cpu(); - uint64_t base = (uint64_t)c; - uint64_t bot = base + sizeof(struct cpu_thread); - int64_t mark = sp - bot; - uint64_t top = base + NORMAL_STACK_SIZE; - - /* - * Don't re-enter on this CPU or don't enter at all if somebody - * has spotted an overflow - */ - if (c->in_mcount) - return; - c->in_mcount = true; - - /* Capture lowest stack for this thread */ - if (mark < c->stack_bot_mark) { - c->stack_bot_mark = mark; - c->stack_bot_pc = lr; - c->stack_bot_tok = c->current_token; - } - - /* Stack is within bounds ? check for warning and bail */ - if (sp >= (bot + STACK_SAFETY_GAP) && sp < top) { - if (mark < STACK_WARNING_GAP) { - prlog(PR_EMERG, "CPU %04x Stack usage danger !" - " pc=%08llx sp=%08llx (gap=%lld) token=%lld\n", - c->pir, lr, sp, mark, c->current_token); - backtrace(); - } - c->in_mcount = false; - return; - } - - prlog(PR_EMERG, "CPU %04x Stack overflow detected !" - " pc=%08llx sp=%08llx (gap=%lld) token=%lld\n", - c->pir, lr, sp, mark, c->current_token); - abort(); -} - -static int64_t lowest_stack_mark = LONG_MAX; -static struct lock stack_check_lock = LOCK_UNLOCKED; - -void check_stacks(void) -{ - struct cpu_thread *c; - uint64_t lmark, lpc, ltok; - int found = -1; - - for_each_cpu(c) { - if (!c->stack_bot_mark || - c->stack_bot_mark >= lowest_stack_mark) - continue; - lock(&stack_check_lock); - if (c->stack_bot_mark >= lowest_stack_mark) { - unlock(&stack_check_lock); - continue; - } - lmark = lowest_stack_mark = c->stack_bot_mark; - lpc = c->stack_bot_pc; - ltok = c->stack_bot_tok; - found = c->pir; - unlock(&stack_check_lock); - } - if (found >= 0) - prlog(PR_NOTICE, "CPU %04x lowest stack mark %lld bytes left" - " pc=%08llx token=%lld\n", found, lmark, lpc, ltok); -} - -#endif /* STACK_CHECK_ENABLED */ |