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