traps.c 28 KB

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