traps.c 29 KB

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