traps_64.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
  4. *
  5. * Pentium III FXSR, SSE support
  6. * Gareth Hughes <gareth@valinux.com>, May 2000
  7. */
  8. /*
  9. * 'Traps.c' handles hardware traps and faults after we have saved some
  10. * state in 'entry.S'.
  11. */
  12. #include <linux/moduleparam.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kallsyms.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/utsname.h>
  19. #include <linux/kdebug.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/string.h>
  24. #include <linux/unwind.h>
  25. #include <linux/delay.h>
  26. #include <linux/errno.h>
  27. #include <linux/kexec.h>
  28. #include <linux/sched.h>
  29. #include <linux/timer.h>
  30. #include <linux/init.h>
  31. #include <linux/bug.h>
  32. #include <linux/nmi.h>
  33. #include <linux/mm.h>
  34. #include <linux/smp.h>
  35. #include <linux/io.h>
  36. #if defined(CONFIG_EDAC)
  37. #include <linux/edac.h>
  38. #endif
  39. #include <asm/stacktrace.h>
  40. #include <asm/processor.h>
  41. #include <asm/debugreg.h>
  42. #include <asm/atomic.h>
  43. #include <asm/system.h>
  44. #include <asm/unwind.h>
  45. #include <asm/desc.h>
  46. #include <asm/i387.h>
  47. #include <asm/pgalloc.h>
  48. #include <asm/proto.h>
  49. #include <asm/pda.h>
  50. #include <asm/traps.h>
  51. #include <mach_traps.h>
  52. int panic_on_unrecovered_nmi;
  53. int kstack_depth_to_print = 12;
  54. static unsigned int code_bytes = 64;
  55. static int ignore_nmis;
  56. static int die_counter;
  57. static inline void conditional_sti(struct pt_regs *regs)
  58. {
  59. if (regs->flags & X86_EFLAGS_IF)
  60. local_irq_enable();
  61. }
  62. static inline void preempt_conditional_sti(struct pt_regs *regs)
  63. {
  64. inc_preempt_count();
  65. if (regs->flags & X86_EFLAGS_IF)
  66. local_irq_enable();
  67. }
  68. static inline void preempt_conditional_cli(struct pt_regs *regs)
  69. {
  70. if (regs->flags & X86_EFLAGS_IF)
  71. local_irq_disable();
  72. /* Make sure to not schedule here because we could be running
  73. on an exception stack. */
  74. dec_preempt_count();
  75. }
  76. void printk_address(unsigned long address, int reliable)
  77. {
  78. printk(" [<%016lx>] %s%pS\n",
  79. address, reliable ? "" : "? ", (void *) address);
  80. }
  81. static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
  82. unsigned *usedp, char **idp)
  83. {
  84. static char ids[][8] = {
  85. [DEBUG_STACK - 1] = "#DB",
  86. [NMI_STACK - 1] = "NMI",
  87. [DOUBLEFAULT_STACK - 1] = "#DF",
  88. [STACKFAULT_STACK - 1] = "#SS",
  89. [MCE_STACK - 1] = "#MC",
  90. #if DEBUG_STKSZ > EXCEPTION_STKSZ
  91. [N_EXCEPTION_STACKS ...
  92. N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
  93. #endif
  94. };
  95. unsigned k;
  96. /*
  97. * Iterate over all exception stacks, and figure out whether
  98. * 'stack' is in one of them:
  99. */
  100. for (k = 0; k < N_EXCEPTION_STACKS; k++) {
  101. unsigned long end = per_cpu(orig_ist, cpu).ist[k];
  102. /*
  103. * Is 'stack' above this exception frame's end?
  104. * If yes then skip to the next frame.
  105. */
  106. if (stack >= end)
  107. continue;
  108. /*
  109. * Is 'stack' above this exception frame's start address?
  110. * If yes then we found the right frame.
  111. */
  112. if (stack >= end - EXCEPTION_STKSZ) {
  113. /*
  114. * Make sure we only iterate through an exception
  115. * stack once. If it comes up for the second time
  116. * then there's something wrong going on - just
  117. * break out and return NULL:
  118. */
  119. if (*usedp & (1U << k))
  120. break;
  121. *usedp |= 1U << k;
  122. *idp = ids[k];
  123. return (unsigned long *)end;
  124. }
  125. /*
  126. * If this is a debug stack, and if it has a larger size than
  127. * the usual exception stacks, then 'stack' might still
  128. * be within the lower portion of the debug stack:
  129. */
  130. #if DEBUG_STKSZ > EXCEPTION_STKSZ
  131. if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
  132. unsigned j = N_EXCEPTION_STACKS - 1;
  133. /*
  134. * Black magic. A large debug stack is composed of
  135. * multiple exception stack entries, which we
  136. * iterate through now. Dont look:
  137. */
  138. do {
  139. ++j;
  140. end -= EXCEPTION_STKSZ;
  141. ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
  142. } while (stack < end - EXCEPTION_STKSZ);
  143. if (*usedp & (1U << j))
  144. break;
  145. *usedp |= 1U << j;
  146. *idp = ids[j];
  147. return (unsigned long *)end;
  148. }
  149. #endif
  150. }
  151. return NULL;
  152. }
  153. /*
  154. * x86-64 can have up to three kernel stacks:
  155. * process stack
  156. * interrupt stack
  157. * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
  158. */
  159. static inline int valid_stack_ptr(struct thread_info *tinfo,
  160. void *p, unsigned int size, void *end)
  161. {
  162. void *t = tinfo;
  163. if (end) {
  164. if (p < end && p >= (end-THREAD_SIZE))
  165. return 1;
  166. else
  167. return 0;
  168. }
  169. return p > t && p < t + THREAD_SIZE - size;
  170. }
  171. /* The form of the top of the frame on the stack */
  172. struct stack_frame {
  173. struct stack_frame *next_frame;
  174. unsigned long return_address;
  175. };
  176. static inline unsigned long
  177. print_context_stack(struct thread_info *tinfo,
  178. unsigned long *stack, unsigned long bp,
  179. const struct stacktrace_ops *ops, void *data,
  180. unsigned long *end)
  181. {
  182. struct stack_frame *frame = (struct stack_frame *)bp;
  183. while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
  184. unsigned long addr;
  185. addr = *stack;
  186. if (__kernel_text_address(addr)) {
  187. if ((unsigned long) stack == bp + 8) {
  188. ops->address(data, addr, 1);
  189. frame = frame->next_frame;
  190. bp = (unsigned long) frame;
  191. } else {
  192. ops->address(data, addr, bp == 0);
  193. }
  194. }
  195. stack++;
  196. }
  197. return bp;
  198. }
  199. void dump_trace(struct task_struct *task, struct pt_regs *regs,
  200. unsigned long *stack, unsigned long bp,
  201. const struct stacktrace_ops *ops, void *data)
  202. {
  203. const unsigned cpu = get_cpu();
  204. unsigned long *irqstack_end = (unsigned long *)cpu_pda(cpu)->irqstackptr;
  205. unsigned used = 0;
  206. struct thread_info *tinfo;
  207. if (!task)
  208. task = current;
  209. if (!stack) {
  210. unsigned long dummy;
  211. stack = &dummy;
  212. if (task && task != current)
  213. stack = (unsigned long *)task->thread.sp;
  214. }
  215. #ifdef CONFIG_FRAME_POINTER
  216. if (!bp) {
  217. if (task == current) {
  218. /* Grab bp right from our regs */
  219. asm("movq %%rbp, %0" : "=r" (bp) : );
  220. } else {
  221. /* bp is the last reg pushed by switch_to */
  222. bp = *(unsigned long *) task->thread.sp;
  223. }
  224. }
  225. #endif
  226. /*
  227. * Print function call entries in all stacks, starting at the
  228. * current stack address. If the stacks consist of nested
  229. * exceptions
  230. */
  231. tinfo = task_thread_info(task);
  232. for (;;) {
  233. char *id;
  234. unsigned long *estack_end;
  235. estack_end = in_exception_stack(cpu, (unsigned long)stack,
  236. &used, &id);
  237. if (estack_end) {
  238. if (ops->stack(data, id) < 0)
  239. break;
  240. bp = print_context_stack(tinfo, stack, bp, ops,
  241. data, estack_end);
  242. ops->stack(data, "<EOE>");
  243. /*
  244. * We link to the next stack via the
  245. * second-to-last pointer (index -2 to end) in the
  246. * exception stack:
  247. */
  248. stack = (unsigned long *) estack_end[-2];
  249. continue;
  250. }
  251. if (irqstack_end) {
  252. unsigned long *irqstack;
  253. irqstack = irqstack_end -
  254. (IRQSTACKSIZE - 64) / sizeof(*irqstack);
  255. if (stack >= irqstack && stack < irqstack_end) {
  256. if (ops->stack(data, "IRQ") < 0)
  257. break;
  258. bp = print_context_stack(tinfo, stack, bp,
  259. ops, data, irqstack_end);
  260. /*
  261. * We link to the next stack (which would be
  262. * the process stack normally) the last
  263. * pointer (index -1 to end) in the IRQ stack:
  264. */
  265. stack = (unsigned long *) (irqstack_end[-1]);
  266. irqstack_end = NULL;
  267. ops->stack(data, "EOI");
  268. continue;
  269. }
  270. }
  271. break;
  272. }
  273. /*
  274. * This handles the process stack:
  275. */
  276. bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
  277. put_cpu();
  278. }
  279. EXPORT_SYMBOL(dump_trace);
  280. static void
  281. print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
  282. {
  283. print_symbol(msg, symbol);
  284. printk("\n");
  285. }
  286. static void print_trace_warning(void *data, char *msg)
  287. {
  288. printk("%s\n", msg);
  289. }
  290. static int print_trace_stack(void *data, char *name)
  291. {
  292. printk(" <%s> ", name);
  293. return 0;
  294. }
  295. static void print_trace_address(void *data, unsigned long addr, int reliable)
  296. {
  297. touch_nmi_watchdog();
  298. printk_address(addr, reliable);
  299. }
  300. static const struct stacktrace_ops print_trace_ops = {
  301. .warning = print_trace_warning,
  302. .warning_symbol = print_trace_warning_symbol,
  303. .stack = print_trace_stack,
  304. .address = print_trace_address,
  305. };
  306. static void
  307. show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
  308. unsigned long *stack, unsigned long bp, char *log_lvl)
  309. {
  310. printk("Call Trace:\n");
  311. dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
  312. }
  313. void show_trace(struct task_struct *task, struct pt_regs *regs,
  314. unsigned long *stack, unsigned long bp)
  315. {
  316. show_trace_log_lvl(task, regs, stack, bp, "");
  317. }
  318. static void
  319. show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  320. unsigned long *sp, unsigned long bp, char *log_lvl)
  321. {
  322. unsigned long *stack;
  323. int i;
  324. const int cpu = smp_processor_id();
  325. unsigned long *irqstack_end =
  326. (unsigned long *) (cpu_pda(cpu)->irqstackptr);
  327. unsigned long *irqstack =
  328. (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
  329. /*
  330. * debugging aid: "show_stack(NULL, NULL);" prints the
  331. * back trace for this cpu.
  332. */
  333. if (sp == NULL) {
  334. if (task)
  335. sp = (unsigned long *)task->thread.sp;
  336. else
  337. sp = (unsigned long *)&sp;
  338. }
  339. stack = sp;
  340. for (i = 0; i < kstack_depth_to_print; i++) {
  341. if (stack >= irqstack && stack <= irqstack_end) {
  342. if (stack == irqstack_end) {
  343. stack = (unsigned long *) (irqstack_end[-1]);
  344. printk(" <EOI> ");
  345. }
  346. } else {
  347. if (((long) stack & (THREAD_SIZE-1)) == 0)
  348. break;
  349. }
  350. if (i && ((i % 4) == 0))
  351. printk("\n");
  352. printk(" %016lx", *stack++);
  353. touch_nmi_watchdog();
  354. }
  355. printk("\n");
  356. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  357. }
  358. void show_stack(struct task_struct *task, unsigned long *sp)
  359. {
  360. show_stack_log_lvl(task, NULL, sp, 0, "");
  361. }
  362. /*
  363. * The architecture-independent dump_stack generator
  364. */
  365. void dump_stack(void)
  366. {
  367. unsigned long bp = 0;
  368. unsigned long stack;
  369. #ifdef CONFIG_FRAME_POINTER
  370. if (!bp)
  371. asm("movq %%rbp, %0" : "=r" (bp) : );
  372. #endif
  373. printk("Pid: %d, comm: %.20s %s %s %.*s\n",
  374. current->pid, current->comm, print_tainted(),
  375. init_utsname()->release,
  376. (int)strcspn(init_utsname()->version, " "),
  377. init_utsname()->version);
  378. show_trace(NULL, NULL, &stack, bp);
  379. }
  380. EXPORT_SYMBOL(dump_stack);
  381. void show_registers(struct pt_regs *regs)
  382. {
  383. int i;
  384. unsigned long sp;
  385. const int cpu = smp_processor_id();
  386. struct task_struct *cur = cpu_pda(cpu)->pcurrent;
  387. sp = regs->sp;
  388. printk("CPU %d ", cpu);
  389. __show_regs(regs, 1);
  390. printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
  391. cur->comm, cur->pid, task_thread_info(cur), cur);
  392. /*
  393. * When in-kernel, we also print out the stack and code at the
  394. * time of the fault..
  395. */
  396. if (!user_mode(regs)) {
  397. unsigned int code_prologue = code_bytes * 43 / 64;
  398. unsigned int code_len = code_bytes;
  399. unsigned char c;
  400. u8 *ip;
  401. printk("Stack: ");
  402. show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
  403. regs->bp, "");
  404. printk(KERN_EMERG "Code: ");
  405. ip = (u8 *)regs->ip - code_prologue;
  406. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  407. /* try starting at RIP */
  408. ip = (u8 *)regs->ip;
  409. code_len = code_len - code_prologue + 1;
  410. }
  411. for (i = 0; i < code_len; i++, ip++) {
  412. if (ip < (u8 *)PAGE_OFFSET ||
  413. probe_kernel_address(ip, c)) {
  414. printk(" Bad RIP value.");
  415. break;
  416. }
  417. if (ip == (u8 *)regs->ip)
  418. printk("<%02x> ", c);
  419. else
  420. printk("%02x ", c);
  421. }
  422. }
  423. printk("\n");
  424. }
  425. int is_valid_bugaddr(unsigned long ip)
  426. {
  427. unsigned short ud2;
  428. if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
  429. return 0;
  430. return ud2 == 0x0b0f;
  431. }
  432. static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
  433. static int die_owner = -1;
  434. static unsigned int die_nest_count;
  435. unsigned __kprobes long oops_begin(void)
  436. {
  437. int cpu;
  438. unsigned long flags;
  439. oops_enter();
  440. /* racy, but better than risking deadlock. */
  441. raw_local_irq_save(flags);
  442. cpu = smp_processor_id();
  443. if (!__raw_spin_trylock(&die_lock)) {
  444. if (cpu == die_owner)
  445. /* nested oops. should stop eventually */;
  446. else
  447. __raw_spin_lock(&die_lock);
  448. }
  449. die_nest_count++;
  450. die_owner = cpu;
  451. console_verbose();
  452. bust_spinlocks(1);
  453. return flags;
  454. }
  455. void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  456. {
  457. die_owner = -1;
  458. bust_spinlocks(0);
  459. die_nest_count--;
  460. if (!die_nest_count)
  461. /* Nest count reaches zero, release the lock. */
  462. __raw_spin_unlock(&die_lock);
  463. raw_local_irq_restore(flags);
  464. if (!regs) {
  465. oops_exit();
  466. return;
  467. }
  468. if (panic_on_oops)
  469. panic("Fatal exception");
  470. oops_exit();
  471. do_exit(signr);
  472. }
  473. int __kprobes __die(const char *str, struct pt_regs *regs, long err)
  474. {
  475. printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff, ++die_counter);
  476. #ifdef CONFIG_PREEMPT
  477. printk("PREEMPT ");
  478. #endif
  479. #ifdef CONFIG_SMP
  480. printk("SMP ");
  481. #endif
  482. #ifdef CONFIG_DEBUG_PAGEALLOC
  483. printk("DEBUG_PAGEALLOC");
  484. #endif
  485. printk("\n");
  486. if (notify_die(DIE_OOPS, str, regs, err,
  487. current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
  488. return 1;
  489. show_registers(regs);
  490. add_taint(TAINT_DIE);
  491. /* Executive summary in case the oops scrolled away */
  492. printk(KERN_ALERT "RIP ");
  493. printk_address(regs->ip, 1);
  494. printk(" RSP <%016lx>\n", regs->sp);
  495. if (kexec_should_crash(current))
  496. crash_kexec(regs);
  497. return 0;
  498. }
  499. void die(const char *str, struct pt_regs *regs, long err)
  500. {
  501. unsigned long flags = oops_begin();
  502. if (!user_mode(regs))
  503. report_bug(regs->ip, regs);
  504. if (__die(str, regs, err))
  505. regs = NULL;
  506. oops_end(flags, regs, SIGSEGV);
  507. }
  508. notrace __kprobes void
  509. die_nmi(char *str, struct pt_regs *regs, int do_panic)
  510. {
  511. unsigned long flags;
  512. if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
  513. return;
  514. flags = oops_begin();
  515. /*
  516. * We are in trouble anyway, lets at least try
  517. * to get a message out.
  518. */
  519. printk(KERN_EMERG "%s", str);
  520. printk(" on CPU%d, ip %08lx, registers:\n",
  521. smp_processor_id(), regs->ip);
  522. show_registers(regs);
  523. if (kexec_should_crash(current))
  524. crash_kexec(regs);
  525. if (do_panic || panic_on_oops)
  526. panic("Non maskable interrupt");
  527. oops_end(flags, NULL, SIGBUS);
  528. nmi_exit();
  529. local_irq_enable();
  530. do_exit(SIGBUS);
  531. }
  532. static void __kprobes
  533. do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
  534. long error_code, siginfo_t *info)
  535. {
  536. struct task_struct *tsk = current;
  537. if (!user_mode(regs))
  538. goto kernel_trap;
  539. /*
  540. * We want error_code and trap_no set for userspace faults and
  541. * kernelspace faults which result in die(), but not
  542. * kernelspace faults which are fixed up. die() gives the
  543. * process no chance to handle the signal and notice the
  544. * kernel fault information, so that won't result in polluting
  545. * the information about previously queued, but not yet
  546. * delivered, faults. See also do_general_protection below.
  547. */
  548. tsk->thread.error_code = error_code;
  549. tsk->thread.trap_no = trapnr;
  550. if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
  551. printk_ratelimit()) {
  552. printk(KERN_INFO
  553. "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
  554. tsk->comm, tsk->pid, str,
  555. regs->ip, regs->sp, error_code);
  556. print_vma_addr(" in ", regs->ip);
  557. printk("\n");
  558. }
  559. if (info)
  560. force_sig_info(signr, info, tsk);
  561. else
  562. force_sig(signr, tsk);
  563. return;
  564. kernel_trap:
  565. if (!fixup_exception(regs)) {
  566. tsk->thread.error_code = error_code;
  567. tsk->thread.trap_no = trapnr;
  568. die(str, regs, error_code);
  569. }
  570. return;
  571. }
  572. #define DO_ERROR(trapnr, signr, str, name) \
  573. asmlinkage void do_##name(struct pt_regs *regs, long error_code) \
  574. { \
  575. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  576. == NOTIFY_STOP) \
  577. return; \
  578. conditional_sti(regs); \
  579. do_trap(trapnr, signr, str, regs, error_code, NULL); \
  580. }
  581. #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
  582. asmlinkage void do_##name(struct pt_regs *regs, long error_code) \
  583. { \
  584. siginfo_t info; \
  585. info.si_signo = signr; \
  586. info.si_errno = 0; \
  587. info.si_code = sicode; \
  588. info.si_addr = (void __user *)siaddr; \
  589. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  590. == NOTIFY_STOP) \
  591. return; \
  592. conditional_sti(regs); \
  593. do_trap(trapnr, signr, str, regs, error_code, &info); \
  594. }
  595. DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
  596. DO_ERROR(4, SIGSEGV, "overflow", overflow)
  597. DO_ERROR(5, SIGSEGV, "bounds", bounds)
  598. DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
  599. DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
  600. DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
  601. DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
  602. DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
  603. /* Runs on IST stack */
  604. asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
  605. {
  606. if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
  607. 12, SIGBUS) == NOTIFY_STOP)
  608. return;
  609. preempt_conditional_sti(regs);
  610. do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
  611. preempt_conditional_cli(regs);
  612. }
  613. asmlinkage void do_double_fault(struct pt_regs *regs, long error_code)
  614. {
  615. static const char str[] = "double fault";
  616. struct task_struct *tsk = current;
  617. /* Return not checked because double check cannot be ignored */
  618. notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
  619. tsk->thread.error_code = error_code;
  620. tsk->thread.trap_no = 8;
  621. /* This is always a kernel trap and never fixable (and thus must
  622. never return). */
  623. for (;;)
  624. die(str, regs, error_code);
  625. }
  626. asmlinkage void __kprobes
  627. do_general_protection(struct pt_regs *regs, long error_code)
  628. {
  629. struct task_struct *tsk;
  630. conditional_sti(regs);
  631. tsk = current;
  632. if (!user_mode(regs))
  633. goto gp_in_kernel;
  634. tsk->thread.error_code = error_code;
  635. tsk->thread.trap_no = 13;
  636. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  637. printk_ratelimit()) {
  638. printk(KERN_INFO
  639. "%s[%d] general protection ip:%lx sp:%lx error:%lx",
  640. tsk->comm, tsk->pid,
  641. regs->ip, regs->sp, error_code);
  642. print_vma_addr(" in ", regs->ip);
  643. printk("\n");
  644. }
  645. force_sig(SIGSEGV, tsk);
  646. return;
  647. gp_in_kernel:
  648. if (fixup_exception(regs))
  649. return;
  650. tsk->thread.error_code = error_code;
  651. tsk->thread.trap_no = 13;
  652. if (notify_die(DIE_GPF, "general protection fault", regs,
  653. error_code, 13, SIGSEGV) == NOTIFY_STOP)
  654. return;
  655. die("general protection fault", regs, error_code);
  656. }
  657. static notrace __kprobes void
  658. mem_parity_error(unsigned char reason, struct pt_regs *regs)
  659. {
  660. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  661. reason);
  662. printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
  663. #if defined(CONFIG_EDAC)
  664. if (edac_handler_set()) {
  665. edac_atomic_assert_error();
  666. return;
  667. }
  668. #endif
  669. if (panic_on_unrecovered_nmi)
  670. panic("NMI: Not continuing");
  671. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  672. /* Clear and disable the memory parity error line. */
  673. reason = (reason & 0xf) | 4;
  674. outb(reason, 0x61);
  675. }
  676. static notrace __kprobes void
  677. io_check_error(unsigned char reason, struct pt_regs *regs)
  678. {
  679. printk("NMI: IOCK error (debug interrupt?)\n");
  680. show_registers(regs);
  681. /* Re-enable the IOCK line, wait for a few seconds */
  682. reason = (reason & 0xf) | 8;
  683. outb(reason, 0x61);
  684. mdelay(2000);
  685. reason &= ~8;
  686. outb(reason, 0x61);
  687. }
  688. static notrace __kprobes void
  689. unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
  690. {
  691. if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) ==
  692. NOTIFY_STOP)
  693. return;
  694. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  695. reason);
  696. printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
  697. if (panic_on_unrecovered_nmi)
  698. panic("NMI: Not continuing");
  699. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  700. }
  701. /* Runs on IST stack. This code must keep interrupts off all the time.
  702. Nested NMIs are prevented by the CPU. */
  703. asmlinkage notrace __kprobes void default_do_nmi(struct pt_regs *regs)
  704. {
  705. unsigned char reason = 0;
  706. int cpu;
  707. cpu = smp_processor_id();
  708. /* Only the BSP gets external NMIs from the system. */
  709. if (!cpu)
  710. reason = get_nmi_reason();
  711. if (!(reason & 0xc0)) {
  712. if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
  713. == NOTIFY_STOP)
  714. return;
  715. /*
  716. * Ok, so this is none of the documented NMI sources,
  717. * so it must be the NMI watchdog.
  718. */
  719. if (nmi_watchdog_tick(regs, reason))
  720. return;
  721. if (!do_nmi_callback(regs, cpu))
  722. unknown_nmi_error(reason, regs);
  723. return;
  724. }
  725. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
  726. return;
  727. /* AK: following checks seem to be broken on modern chipsets. FIXME */
  728. if (reason & 0x80)
  729. mem_parity_error(reason, regs);
  730. if (reason & 0x40)
  731. io_check_error(reason, regs);
  732. }
  733. asmlinkage notrace __kprobes void
  734. do_nmi(struct pt_regs *regs, long error_code)
  735. {
  736. nmi_enter();
  737. add_pda(__nmi_count, 1);
  738. if (!ignore_nmis)
  739. default_do_nmi(regs);
  740. nmi_exit();
  741. }
  742. void stop_nmi(void)
  743. {
  744. acpi_nmi_disable();
  745. ignore_nmis++;
  746. }
  747. void restart_nmi(void)
  748. {
  749. ignore_nmis--;
  750. acpi_nmi_enable();
  751. }
  752. /* runs on IST stack. */
  753. asmlinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
  754. {
  755. if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
  756. == NOTIFY_STOP)
  757. return;
  758. preempt_conditional_sti(regs);
  759. do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
  760. preempt_conditional_cli(regs);
  761. }
  762. /* Help handler running on IST stack to switch back to user stack
  763. for scheduling or signal handling. The actual stack switch is done in
  764. entry.S */
  765. asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
  766. {
  767. struct pt_regs *regs = eregs;
  768. /* Did already sync */
  769. if (eregs == (struct pt_regs *)eregs->sp)
  770. ;
  771. /* Exception from user space */
  772. else if (user_mode(eregs))
  773. regs = task_pt_regs(current);
  774. /* Exception from kernel and interrupts are enabled. Move to
  775. kernel process stack. */
  776. else if (eregs->flags & X86_EFLAGS_IF)
  777. regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
  778. if (eregs != regs)
  779. *regs = *eregs;
  780. return regs;
  781. }
  782. /* runs on IST stack. */
  783. asmlinkage void __kprobes do_debug(struct pt_regs *regs,
  784. unsigned long error_code)
  785. {
  786. struct task_struct *tsk = current;
  787. unsigned long condition;
  788. siginfo_t info;
  789. get_debugreg(condition, 6);
  790. /*
  791. * The processor cleared BTF, so don't mark that we need it set.
  792. */
  793. clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
  794. tsk->thread.debugctlmsr = 0;
  795. if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
  796. SIGTRAP) == NOTIFY_STOP)
  797. return;
  798. preempt_conditional_sti(regs);
  799. /* Mask out spurious debug traps due to lazy DR7 setting */
  800. if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
  801. if (!tsk->thread.debugreg7)
  802. goto clear_dr7;
  803. }
  804. tsk->thread.debugreg6 = condition;
  805. /*
  806. * Single-stepping through TF: make sure we ignore any events in
  807. * kernel space (but re-enable TF when returning to user mode).
  808. */
  809. if (condition & DR_STEP) {
  810. if (!user_mode(regs))
  811. goto clear_TF_reenable;
  812. }
  813. /* Ok, finally something we can handle */
  814. tsk->thread.trap_no = 1;
  815. tsk->thread.error_code = error_code;
  816. info.si_signo = SIGTRAP;
  817. info.si_errno = 0;
  818. info.si_code = get_si_code(condition);
  819. info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
  820. force_sig_info(SIGTRAP, &info, tsk);
  821. clear_dr7:
  822. set_debugreg(0, 7);
  823. preempt_conditional_cli(regs);
  824. return;
  825. clear_TF_reenable:
  826. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  827. regs->flags &= ~X86_EFLAGS_TF;
  828. preempt_conditional_cli(regs);
  829. return;
  830. }
  831. static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
  832. {
  833. if (fixup_exception(regs))
  834. return 1;
  835. notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
  836. /* Illegal floating point operation in the kernel */
  837. current->thread.trap_no = trapnr;
  838. die(str, regs, 0);
  839. return 0;
  840. }
  841. /*
  842. * Note that we play around with the 'TS' bit in an attempt to get
  843. * the correct behaviour even in the presence of the asynchronous
  844. * IRQ13 behaviour
  845. */
  846. asmlinkage void do_coprocessor_error(struct pt_regs *regs)
  847. {
  848. void __user *ip = (void __user *)(regs->ip);
  849. struct task_struct *task;
  850. siginfo_t info;
  851. unsigned short cwd, swd;
  852. conditional_sti(regs);
  853. if (!user_mode(regs) &&
  854. kernel_math_error(regs, "kernel x87 math error", 16))
  855. return;
  856. /*
  857. * Save the info for the exception handler and clear the error.
  858. */
  859. task = current;
  860. save_init_fpu(task);
  861. task->thread.trap_no = 16;
  862. task->thread.error_code = 0;
  863. info.si_signo = SIGFPE;
  864. info.si_errno = 0;
  865. info.si_code = __SI_FAULT;
  866. info.si_addr = ip;
  867. /*
  868. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  869. * status. 0x3f is the exception bits in these regs, 0x200 is the
  870. * C1 reg you need in case of a stack fault, 0x040 is the stack
  871. * fault bit. We should only be taking one exception at a time,
  872. * so if this combination doesn't produce any single exception,
  873. * then we have a bad program that isn't synchronizing its FPU usage
  874. * and it will suffer the consequences since we won't be able to
  875. * fully reproduce the context of the exception
  876. */
  877. cwd = get_fpu_cwd(task);
  878. swd = get_fpu_swd(task);
  879. switch (swd & ~cwd & 0x3f) {
  880. case 0x000: /* No unmasked exception */
  881. default: /* Multiple exceptions */
  882. break;
  883. case 0x001: /* Invalid Op */
  884. /*
  885. * swd & 0x240 == 0x040: Stack Underflow
  886. * swd & 0x240 == 0x240: Stack Overflow
  887. * User must clear the SF bit (0x40) if set
  888. */
  889. info.si_code = FPE_FLTINV;
  890. break;
  891. case 0x002: /* Denormalize */
  892. case 0x010: /* Underflow */
  893. info.si_code = FPE_FLTUND;
  894. break;
  895. case 0x004: /* Zero Divide */
  896. info.si_code = FPE_FLTDIV;
  897. break;
  898. case 0x008: /* Overflow */
  899. info.si_code = FPE_FLTOVF;
  900. break;
  901. case 0x020: /* Precision */
  902. info.si_code = FPE_FLTRES;
  903. break;
  904. }
  905. force_sig_info(SIGFPE, &info, task);
  906. }
  907. asmlinkage void bad_intr(void)
  908. {
  909. printk("bad interrupt");
  910. }
  911. asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
  912. {
  913. void __user *ip = (void __user *)(regs->ip);
  914. struct task_struct *task;
  915. siginfo_t info;
  916. unsigned short mxcsr;
  917. conditional_sti(regs);
  918. if (!user_mode(regs) &&
  919. kernel_math_error(regs, "kernel simd math error", 19))
  920. return;
  921. /*
  922. * Save the info for the exception handler and clear the error.
  923. */
  924. task = current;
  925. save_init_fpu(task);
  926. task->thread.trap_no = 19;
  927. task->thread.error_code = 0;
  928. info.si_signo = SIGFPE;
  929. info.si_errno = 0;
  930. info.si_code = __SI_FAULT;
  931. info.si_addr = ip;
  932. /*
  933. * The SIMD FPU exceptions are handled a little differently, as there
  934. * is only a single status/control register. Thus, to determine which
  935. * unmasked exception was caught we must mask the exception mask bits
  936. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  937. */
  938. mxcsr = get_fpu_mxcsr(task);
  939. switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
  940. case 0x000:
  941. default:
  942. break;
  943. case 0x001: /* Invalid Op */
  944. info.si_code = FPE_FLTINV;
  945. break;
  946. case 0x002: /* Denormalize */
  947. case 0x010: /* Underflow */
  948. info.si_code = FPE_FLTUND;
  949. break;
  950. case 0x004: /* Zero Divide */
  951. info.si_code = FPE_FLTDIV;
  952. break;
  953. case 0x008: /* Overflow */
  954. info.si_code = FPE_FLTOVF;
  955. break;
  956. case 0x020: /* Precision */
  957. info.si_code = FPE_FLTRES;
  958. break;
  959. }
  960. force_sig_info(SIGFPE, &info, task);
  961. }
  962. asmlinkage void do_spurious_interrupt_bug(struct pt_regs *regs)
  963. {
  964. }
  965. asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
  966. {
  967. }
  968. asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
  969. {
  970. }
  971. /*
  972. * 'math_state_restore()' saves the current math information in the
  973. * old math state array, and gets the new ones from the current task
  974. *
  975. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  976. * Don't touch unless you *really* know how it works.
  977. */
  978. asmlinkage void math_state_restore(void)
  979. {
  980. struct task_struct *me = current;
  981. if (!used_math()) {
  982. local_irq_enable();
  983. /*
  984. * does a slab alloc which can sleep
  985. */
  986. if (init_fpu(me)) {
  987. /*
  988. * ran out of memory!
  989. */
  990. do_group_exit(SIGKILL);
  991. return;
  992. }
  993. local_irq_disable();
  994. }
  995. clts(); /* Allow maths ops (or we recurse) */
  996. /*
  997. * Paranoid restore. send a SIGSEGV if we fail to restore the state.
  998. */
  999. if (unlikely(restore_fpu_checking(me))) {
  1000. stts();
  1001. force_sig(SIGSEGV, me);
  1002. return;
  1003. }
  1004. task_thread_info(me)->status |= TS_USEDFPU;
  1005. me->fpu_counter++;
  1006. }
  1007. EXPORT_SYMBOL_GPL(math_state_restore);
  1008. void __init trap_init(void)
  1009. {
  1010. set_intr_gate(0, &divide_error);
  1011. set_intr_gate_ist(1, &debug, DEBUG_STACK);
  1012. set_intr_gate_ist(2, &nmi, NMI_STACK);
  1013. /* int3 can be called from all */
  1014. set_system_gate_ist(3, &int3, DEBUG_STACK);
  1015. /* int4 can be called from all */
  1016. set_system_gate(4, &overflow);
  1017. set_intr_gate(5, &bounds);
  1018. set_intr_gate(6, &invalid_op);
  1019. set_intr_gate(7, &device_not_available);
  1020. set_intr_gate_ist(8, &double_fault, DOUBLEFAULT_STACK);
  1021. set_intr_gate(9, &coprocessor_segment_overrun);
  1022. set_intr_gate(10, &invalid_TSS);
  1023. set_intr_gate(11, &segment_not_present);
  1024. set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK);
  1025. set_intr_gate(13, &general_protection);
  1026. set_intr_gate(14, &page_fault);
  1027. set_intr_gate(15, &spurious_interrupt_bug);
  1028. set_intr_gate(16, &coprocessor_error);
  1029. set_intr_gate(17, &alignment_check);
  1030. #ifdef CONFIG_X86_MCE
  1031. set_intr_gate_ist(18, &machine_check, MCE_STACK);
  1032. #endif
  1033. set_intr_gate(19, &simd_coprocessor_error);
  1034. #ifdef CONFIG_IA32_EMULATION
  1035. set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  1036. #endif
  1037. /*
  1038. * Should be a barrier for any external CPU state:
  1039. */
  1040. cpu_init();
  1041. }
  1042. static int __init oops_setup(char *s)
  1043. {
  1044. if (!s)
  1045. return -EINVAL;
  1046. if (!strcmp(s, "panic"))
  1047. panic_on_oops = 1;
  1048. return 0;
  1049. }
  1050. early_param("oops", oops_setup);
  1051. static int __init kstack_setup(char *s)
  1052. {
  1053. if (!s)
  1054. return -EINVAL;
  1055. kstack_depth_to_print = simple_strtoul(s, NULL, 0);
  1056. return 0;
  1057. }
  1058. early_param("kstack", kstack_setup);
  1059. static int __init code_bytes_setup(char *s)
  1060. {
  1061. code_bytes = simple_strtoul(s, NULL, 0);
  1062. if (code_bytes > 8192)
  1063. code_bytes = 8192;
  1064. return 1;
  1065. }
  1066. __setup("code_bytes=", code_bytes_setup);