Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  *  Copyright (C) 1991, 1992  Linus Torvalds
0003  *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
0004  */
0005 #include <linux/kallsyms.h>
0006 #include <linux/kprobes.h>
0007 #include <linux/uaccess.h>
0008 #include <linux/utsname.h>
0009 #include <linux/hardirq.h>
0010 #include <linux/kdebug.h>
0011 #include <linux/module.h>
0012 #include <linux/ptrace.h>
0013 #include <linux/sched/debug.h>
0014 #include <linux/sched/task_stack.h>
0015 #include <linux/ftrace.h>
0016 #include <linux/kexec.h>
0017 #include <linux/bug.h>
0018 #include <linux/nmi.h>
0019 #include <linux/sysfs.h>
0020 #include <linux/kasan.h>
0021 
0022 #include <asm/cpu_entry_area.h>
0023 #include <asm/stacktrace.h>
0024 #include <asm/unwind.h>
0025 
0026 int panic_on_unrecovered_nmi;
0027 int panic_on_io_nmi;
0028 static int die_counter;
0029 
0030 static struct pt_regs exec_summary_regs;
0031 
0032 bool noinstr in_task_stack(unsigned long *stack, struct task_struct *task,
0033                struct stack_info *info)
0034 {
0035     unsigned long *begin = task_stack_page(task);
0036     unsigned long *end   = task_stack_page(task) + THREAD_SIZE;
0037 
0038     if (stack < begin || stack >= end)
0039         return false;
0040 
0041     info->type  = STACK_TYPE_TASK;
0042     info->begin = begin;
0043     info->end   = end;
0044     info->next_sp   = NULL;
0045 
0046     return true;
0047 }
0048 
0049 /* Called from get_stack_info_noinstr - so must be noinstr too */
0050 bool noinstr in_entry_stack(unsigned long *stack, struct stack_info *info)
0051 {
0052     struct entry_stack *ss = cpu_entry_stack(smp_processor_id());
0053 
0054     void *begin = ss;
0055     void *end = ss + 1;
0056 
0057     if ((void *)stack < begin || (void *)stack >= end)
0058         return false;
0059 
0060     info->type  = STACK_TYPE_ENTRY;
0061     info->begin = begin;
0062     info->end   = end;
0063     info->next_sp   = NULL;
0064 
0065     return true;
0066 }
0067 
0068 static void printk_stack_address(unsigned long address, int reliable,
0069                  const char *log_lvl)
0070 {
0071     touch_nmi_watchdog();
0072     printk("%s %s%pBb\n", log_lvl, reliable ? "" : "? ", (void *)address);
0073 }
0074 
0075 static int copy_code(struct pt_regs *regs, u8 *buf, unsigned long src,
0076              unsigned int nbytes)
0077 {
0078     if (!user_mode(regs))
0079         return copy_from_kernel_nofault(buf, (u8 *)src, nbytes);
0080 
0081     /* The user space code from other tasks cannot be accessed. */
0082     if (regs != task_pt_regs(current))
0083         return -EPERM;
0084 
0085     /*
0086      * Even if named copy_from_user_nmi() this can be invoked from
0087      * other contexts and will not try to resolve a pagefault, which is
0088      * the correct thing to do here as this code can be called from any
0089      * context.
0090      */
0091     return copy_from_user_nmi(buf, (void __user *)src, nbytes);
0092 }
0093 
0094 /*
0095  * There are a couple of reasons for the 2/3rd prologue, courtesy of Linus:
0096  *
0097  * In case where we don't have the exact kernel image (which, if we did, we can
0098  * simply disassemble and navigate to the RIP), the purpose of the bigger
0099  * prologue is to have more context and to be able to correlate the code from
0100  * the different toolchains better.
0101  *
0102  * In addition, it helps in recreating the register allocation of the failing
0103  * kernel and thus make sense of the register dump.
0104  *
0105  * What is more, the additional complication of a variable length insn arch like
0106  * x86 warrants having longer byte sequence before rIP so that the disassembler
0107  * can "sync" up properly and find instruction boundaries when decoding the
0108  * opcode bytes.
0109  *
0110  * Thus, the 2/3rds prologue and 64 byte OPCODE_BUFSIZE is just a random
0111  * guesstimate in attempt to achieve all of the above.
0112  */
0113 void show_opcodes(struct pt_regs *regs, const char *loglvl)
0114 {
0115 #define PROLOGUE_SIZE 42
0116 #define EPILOGUE_SIZE 21
0117 #define OPCODE_BUFSIZE (PROLOGUE_SIZE + 1 + EPILOGUE_SIZE)
0118     u8 opcodes[OPCODE_BUFSIZE];
0119     unsigned long prologue = regs->ip - PROLOGUE_SIZE;
0120 
0121     switch (copy_code(regs, opcodes, prologue, sizeof(opcodes))) {
0122     case 0:
0123         printk("%sCode: %" __stringify(PROLOGUE_SIZE) "ph <%02x> %"
0124                __stringify(EPILOGUE_SIZE) "ph\n", loglvl, opcodes,
0125                opcodes[PROLOGUE_SIZE], opcodes + PROLOGUE_SIZE + 1);
0126         break;
0127     case -EPERM:
0128         /* No access to the user space stack of other tasks. Ignore. */
0129         break;
0130     default:
0131         printk("%sCode: Unable to access opcode bytes at RIP 0x%lx.\n",
0132                loglvl, prologue);
0133         break;
0134     }
0135 }
0136 
0137 void show_ip(struct pt_regs *regs, const char *loglvl)
0138 {
0139 #ifdef CONFIG_X86_32
0140     printk("%sEIP: %pS\n", loglvl, (void *)regs->ip);
0141 #else
0142     printk("%sRIP: %04x:%pS\n", loglvl, (int)regs->cs, (void *)regs->ip);
0143 #endif
0144     show_opcodes(regs, loglvl);
0145 }
0146 
0147 void show_iret_regs(struct pt_regs *regs, const char *log_lvl)
0148 {
0149     show_ip(regs, log_lvl);
0150     printk("%sRSP: %04x:%016lx EFLAGS: %08lx", log_lvl, (int)regs->ss,
0151         regs->sp, regs->flags);
0152 }
0153 
0154 static void show_regs_if_on_stack(struct stack_info *info, struct pt_regs *regs,
0155                   bool partial, const char *log_lvl)
0156 {
0157     /*
0158      * These on_stack() checks aren't strictly necessary: the unwind code
0159      * has already validated the 'regs' pointer.  The checks are done for
0160      * ordering reasons: if the registers are on the next stack, we don't
0161      * want to print them out yet.  Otherwise they'll be shown as part of
0162      * the wrong stack.  Later, when show_trace_log_lvl() switches to the
0163      * next stack, this function will be called again with the same regs so
0164      * they can be printed in the right context.
0165      */
0166     if (!partial && on_stack(info, regs, sizeof(*regs))) {
0167         __show_regs(regs, SHOW_REGS_SHORT, log_lvl);
0168 
0169     } else if (partial && on_stack(info, (void *)regs + IRET_FRAME_OFFSET,
0170                        IRET_FRAME_SIZE)) {
0171         /*
0172          * When an interrupt or exception occurs in entry code, the
0173          * full pt_regs might not have been saved yet.  In that case
0174          * just print the iret frame.
0175          */
0176         show_iret_regs(regs, log_lvl);
0177     }
0178 }
0179 
0180 static void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
0181             unsigned long *stack, const char *log_lvl)
0182 {
0183     struct unwind_state state;
0184     struct stack_info stack_info = {0};
0185     unsigned long visit_mask = 0;
0186     int graph_idx = 0;
0187     bool partial = false;
0188 
0189     printk("%sCall Trace:\n", log_lvl);
0190 
0191     unwind_start(&state, task, regs, stack);
0192     stack = stack ? : get_stack_pointer(task, regs);
0193     regs = unwind_get_entry_regs(&state, &partial);
0194 
0195     /*
0196      * Iterate through the stacks, starting with the current stack pointer.
0197      * Each stack has a pointer to the next one.
0198      *
0199      * x86-64 can have several stacks:
0200      * - task stack
0201      * - interrupt stack
0202      * - HW exception stacks (double fault, nmi, debug, mce)
0203      * - entry stack
0204      *
0205      * x86-32 can have up to four stacks:
0206      * - task stack
0207      * - softirq stack
0208      * - hardirq stack
0209      * - entry stack
0210      */
0211     for ( ; stack; stack = PTR_ALIGN(stack_info.next_sp, sizeof(long))) {
0212         const char *stack_name;
0213 
0214         if (get_stack_info(stack, task, &stack_info, &visit_mask)) {
0215             /*
0216              * We weren't on a valid stack.  It's possible that
0217              * we overflowed a valid stack into a guard page.
0218              * See if the next page up is valid so that we can
0219              * generate some kind of backtrace if this happens.
0220              */
0221             stack = (unsigned long *)PAGE_ALIGN((unsigned long)stack);
0222             if (get_stack_info(stack, task, &stack_info, &visit_mask))
0223                 break;
0224         }
0225 
0226         stack_name = stack_type_name(stack_info.type);
0227         if (stack_name)
0228             printk("%s <%s>\n", log_lvl, stack_name);
0229 
0230         if (regs)
0231             show_regs_if_on_stack(&stack_info, regs, partial, log_lvl);
0232 
0233         /*
0234          * Scan the stack, printing any text addresses we find.  At the
0235          * same time, follow proper stack frames with the unwinder.
0236          *
0237          * Addresses found during the scan which are not reported by
0238          * the unwinder are considered to be additional clues which are
0239          * sometimes useful for debugging and are prefixed with '?'.
0240          * This also serves as a failsafe option in case the unwinder
0241          * goes off in the weeds.
0242          */
0243         for (; stack < stack_info.end; stack++) {
0244             unsigned long real_addr;
0245             int reliable = 0;
0246             unsigned long addr = READ_ONCE_NOCHECK(*stack);
0247             unsigned long *ret_addr_p =
0248                 unwind_get_return_address_ptr(&state);
0249 
0250             if (!__kernel_text_address(addr))
0251                 continue;
0252 
0253             /*
0254              * Don't print regs->ip again if it was already printed
0255              * by show_regs_if_on_stack().
0256              */
0257             if (regs && stack == &regs->ip)
0258                 goto next;
0259 
0260             if (stack == ret_addr_p)
0261                 reliable = 1;
0262 
0263             /*
0264              * When function graph tracing is enabled for a
0265              * function, its return address on the stack is
0266              * replaced with the address of an ftrace handler
0267              * (return_to_handler).  In that case, before printing
0268              * the "real" address, we want to print the handler
0269              * address as an "unreliable" hint that function graph
0270              * tracing was involved.
0271              */
0272             real_addr = ftrace_graph_ret_addr(task, &graph_idx,
0273                               addr, stack);
0274             if (real_addr != addr)
0275                 printk_stack_address(addr, 0, log_lvl);
0276             printk_stack_address(real_addr, reliable, log_lvl);
0277 
0278             if (!reliable)
0279                 continue;
0280 
0281 next:
0282             /*
0283              * Get the next frame from the unwinder.  No need to
0284              * check for an error: if anything goes wrong, the rest
0285              * of the addresses will just be printed as unreliable.
0286              */
0287             unwind_next_frame(&state);
0288 
0289             /* if the frame has entry regs, print them */
0290             regs = unwind_get_entry_regs(&state, &partial);
0291             if (regs)
0292                 show_regs_if_on_stack(&stack_info, regs, partial, log_lvl);
0293         }
0294 
0295         if (stack_name)
0296             printk("%s </%s>\n", log_lvl, stack_name);
0297     }
0298 }
0299 
0300 void show_stack(struct task_struct *task, unsigned long *sp,
0301                const char *loglvl)
0302 {
0303     task = task ? : current;
0304 
0305     /*
0306      * Stack frames below this one aren't interesting.  Don't show them
0307      * if we're printing for %current.
0308      */
0309     if (!sp && task == current)
0310         sp = get_stack_pointer(current, NULL);
0311 
0312     show_trace_log_lvl(task, NULL, sp, loglvl);
0313 }
0314 
0315 void show_stack_regs(struct pt_regs *regs)
0316 {
0317     show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT);
0318 }
0319 
0320 static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
0321 static int die_owner = -1;
0322 static unsigned int die_nest_count;
0323 
0324 unsigned long oops_begin(void)
0325 {
0326     int cpu;
0327     unsigned long flags;
0328 
0329     oops_enter();
0330 
0331     /* racy, but better than risking deadlock. */
0332     raw_local_irq_save(flags);
0333     cpu = smp_processor_id();
0334     if (!arch_spin_trylock(&die_lock)) {
0335         if (cpu == die_owner)
0336             /* nested oops. should stop eventually */;
0337         else
0338             arch_spin_lock(&die_lock);
0339     }
0340     die_nest_count++;
0341     die_owner = cpu;
0342     console_verbose();
0343     bust_spinlocks(1);
0344     return flags;
0345 }
0346 NOKPROBE_SYMBOL(oops_begin);
0347 
0348 void __noreturn rewind_stack_and_make_dead(int signr);
0349 
0350 void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
0351 {
0352     if (regs && kexec_should_crash(current))
0353         crash_kexec(regs);
0354 
0355     bust_spinlocks(0);
0356     die_owner = -1;
0357     add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
0358     die_nest_count--;
0359     if (!die_nest_count)
0360         /* Nest count reaches zero, release the lock. */
0361         arch_spin_unlock(&die_lock);
0362     raw_local_irq_restore(flags);
0363     oops_exit();
0364 
0365     /* Executive summary in case the oops scrolled away */
0366     __show_regs(&exec_summary_regs, SHOW_REGS_ALL, KERN_DEFAULT);
0367 
0368     if (!signr)
0369         return;
0370     if (in_interrupt())
0371         panic("Fatal exception in interrupt");
0372     if (panic_on_oops)
0373         panic("Fatal exception");
0374 
0375     /*
0376      * We're not going to return, but we might be on an IST stack or
0377      * have very little stack space left.  Rewind the stack and kill
0378      * the task.
0379      * Before we rewind the stack, we have to tell KASAN that we're going to
0380      * reuse the task stack and that existing poisons are invalid.
0381      */
0382     kasan_unpoison_task_stack(current);
0383     rewind_stack_and_make_dead(signr);
0384 }
0385 NOKPROBE_SYMBOL(oops_end);
0386 
0387 static void __die_header(const char *str, struct pt_regs *regs, long err)
0388 {
0389     const char *pr = "";
0390 
0391     /* Save the regs of the first oops for the executive summary later. */
0392     if (!die_counter)
0393         exec_summary_regs = *regs;
0394 
0395     if (IS_ENABLED(CONFIG_PREEMPTION))
0396         pr = IS_ENABLED(CONFIG_PREEMPT_RT) ? " PREEMPT_RT" : " PREEMPT";
0397 
0398     printk(KERN_DEFAULT
0399            "%s: %04lx [#%d]%s%s%s%s%s\n", str, err & 0xffff, ++die_counter,
0400            pr,
0401            IS_ENABLED(CONFIG_SMP)     ? " SMP"             : "",
0402            debug_pagealloc_enabled()  ? " DEBUG_PAGEALLOC" : "",
0403            IS_ENABLED(CONFIG_KASAN)   ? " KASAN"           : "",
0404            IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION) ?
0405            (boot_cpu_has(X86_FEATURE_PTI) ? " PTI" : " NOPTI") : "");
0406 }
0407 NOKPROBE_SYMBOL(__die_header);
0408 
0409 static int __die_body(const char *str, struct pt_regs *regs, long err)
0410 {
0411     show_regs(regs);
0412     print_modules();
0413 
0414     if (notify_die(DIE_OOPS, str, regs, err,
0415             current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
0416         return 1;
0417 
0418     return 0;
0419 }
0420 NOKPROBE_SYMBOL(__die_body);
0421 
0422 int __die(const char *str, struct pt_regs *regs, long err)
0423 {
0424     __die_header(str, regs, err);
0425     return __die_body(str, regs, err);
0426 }
0427 NOKPROBE_SYMBOL(__die);
0428 
0429 /*
0430  * This is gone through when something in the kernel has done something bad
0431  * and is about to be terminated:
0432  */
0433 void die(const char *str, struct pt_regs *regs, long err)
0434 {
0435     unsigned long flags = oops_begin();
0436     int sig = SIGSEGV;
0437 
0438     if (__die(str, regs, err))
0439         sig = 0;
0440     oops_end(flags, regs, sig);
0441 }
0442 
0443 void die_addr(const char *str, struct pt_regs *regs, long err, long gp_addr)
0444 {
0445     unsigned long flags = oops_begin();
0446     int sig = SIGSEGV;
0447 
0448     __die_header(str, regs, err);
0449     if (gp_addr)
0450         kasan_non_canonical_hook(gp_addr);
0451     if (__die_body(str, regs, err))
0452         sig = 0;
0453     oops_end(flags, regs, sig);
0454 }
0455 
0456 void show_regs(struct pt_regs *regs)
0457 {
0458     enum show_regs_mode print_kernel_regs;
0459 
0460     show_regs_print_info(KERN_DEFAULT);
0461 
0462     print_kernel_regs = user_mode(regs) ? SHOW_REGS_USER : SHOW_REGS_ALL;
0463     __show_regs(regs, print_kernel_regs, KERN_DEFAULT);
0464 
0465     /*
0466      * When in-kernel, we also print out the stack at the time of the fault..
0467      */
0468     if (!user_mode(regs))
0469         show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT);
0470 }