traps.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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. touch_nmi_watchdog();
  307. printk_address(addr);
  308. }
  309. static struct stacktrace_ops print_trace_ops = {
  310. .warning = print_trace_warning,
  311. .warning_symbol = print_trace_warning_symbol,
  312. .stack = print_trace_stack,
  313. .address = print_trace_address,
  314. };
  315. void
  316. show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack)
  317. {
  318. printk("\nCall Trace:\n");
  319. dump_trace(tsk, regs, stack, &print_trace_ops, NULL);
  320. printk("\n");
  321. }
  322. static void
  323. _show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *rsp)
  324. {
  325. unsigned long *stack;
  326. int i;
  327. const int cpu = smp_processor_id();
  328. unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
  329. unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
  330. // debugging aid: "show_stack(NULL, NULL);" prints the
  331. // back trace for this cpu.
  332. if (rsp == NULL) {
  333. if (tsk)
  334. rsp = (unsigned long *)tsk->thread.rsp;
  335. else
  336. rsp = (unsigned long *)&rsp;
  337. }
  338. stack = rsp;
  339. for(i=0; i < kstack_depth_to_print; i++) {
  340. if (stack >= irqstack && stack <= irqstack_end) {
  341. if (stack == irqstack_end) {
  342. stack = (unsigned long *) (irqstack_end[-1]);
  343. printk(" <EOI> ");
  344. }
  345. } else {
  346. if (((long) stack & (THREAD_SIZE-1)) == 0)
  347. break;
  348. }
  349. if (i && ((i % 4) == 0))
  350. printk("\n");
  351. printk(" %016lx", *stack++);
  352. touch_nmi_watchdog();
  353. }
  354. show_trace(tsk, regs, rsp);
  355. }
  356. void show_stack(struct task_struct *tsk, unsigned long * rsp)
  357. {
  358. _show_stack(tsk, NULL, rsp);
  359. }
  360. /*
  361. * The architecture-independent dump_stack generator
  362. */
  363. void dump_stack(void)
  364. {
  365. unsigned long dummy;
  366. show_trace(NULL, NULL, &dummy);
  367. }
  368. EXPORT_SYMBOL(dump_stack);
  369. void show_registers(struct pt_regs *regs)
  370. {
  371. int i;
  372. int in_kernel = !user_mode(regs);
  373. unsigned long rsp;
  374. const int cpu = smp_processor_id();
  375. struct task_struct *cur = cpu_pda(cpu)->pcurrent;
  376. rsp = regs->rsp;
  377. printk("CPU %d ", cpu);
  378. __show_regs(regs);
  379. printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
  380. cur->comm, cur->pid, task_thread_info(cur), cur);
  381. /*
  382. * When in-kernel, we also print out the stack and code at the
  383. * time of the fault..
  384. */
  385. if (in_kernel) {
  386. printk("Stack: ");
  387. _show_stack(NULL, regs, (unsigned long*)rsp);
  388. printk("\nCode: ");
  389. if (regs->rip < PAGE_OFFSET)
  390. goto bad;
  391. for (i=0; i<20; i++) {
  392. unsigned char c;
  393. if (__get_user(c, &((unsigned char*)regs->rip)[i])) {
  394. bad:
  395. printk(" Bad RIP value.");
  396. break;
  397. }
  398. printk("%02x ", c);
  399. }
  400. }
  401. printk("\n");
  402. }
  403. int is_valid_bugaddr(unsigned long rip)
  404. {
  405. unsigned short ud2;
  406. if (__copy_from_user(&ud2, (const void __user *) rip, sizeof(ud2)))
  407. return 0;
  408. return ud2 == 0x0b0f;
  409. }
  410. #ifdef CONFIG_BUG
  411. void out_of_line_bug(void)
  412. {
  413. BUG();
  414. }
  415. EXPORT_SYMBOL(out_of_line_bug);
  416. #endif
  417. static DEFINE_SPINLOCK(die_lock);
  418. static int die_owner = -1;
  419. static unsigned int die_nest_count;
  420. unsigned __kprobes long oops_begin(void)
  421. {
  422. int cpu;
  423. unsigned long flags;
  424. oops_enter();
  425. /* racy, but better than risking deadlock. */
  426. local_irq_save(flags);
  427. cpu = smp_processor_id();
  428. if (!spin_trylock(&die_lock)) {
  429. if (cpu == die_owner)
  430. /* nested oops. should stop eventually */;
  431. else
  432. spin_lock(&die_lock);
  433. }
  434. die_nest_count++;
  435. die_owner = cpu;
  436. console_verbose();
  437. bust_spinlocks(1);
  438. return flags;
  439. }
  440. void __kprobes oops_end(unsigned long flags)
  441. {
  442. die_owner = -1;
  443. bust_spinlocks(0);
  444. die_nest_count--;
  445. if (die_nest_count)
  446. /* We still own the lock */
  447. local_irq_restore(flags);
  448. else
  449. /* Nest count reaches zero, release the lock. */
  450. spin_unlock_irqrestore(&die_lock, flags);
  451. if (panic_on_oops)
  452. panic("Fatal exception");
  453. oops_exit();
  454. }
  455. void __kprobes __die(const char * str, struct pt_regs * regs, long err)
  456. {
  457. static int die_counter;
  458. printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
  459. #ifdef CONFIG_PREEMPT
  460. printk("PREEMPT ");
  461. #endif
  462. #ifdef CONFIG_SMP
  463. printk("SMP ");
  464. #endif
  465. #ifdef CONFIG_DEBUG_PAGEALLOC
  466. printk("DEBUG_PAGEALLOC");
  467. #endif
  468. printk("\n");
  469. notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV);
  470. show_registers(regs);
  471. add_taint(TAINT_DIE);
  472. /* Executive summary in case the oops scrolled away */
  473. printk(KERN_ALERT "RIP ");
  474. printk_address(regs->rip);
  475. printk(" RSP <%016lx>\n", regs->rsp);
  476. if (kexec_should_crash(current))
  477. crash_kexec(regs);
  478. }
  479. void die(const char * str, struct pt_regs * regs, long err)
  480. {
  481. unsigned long flags = oops_begin();
  482. if (!user_mode(regs))
  483. report_bug(regs->rip, regs);
  484. __die(str, regs, err);
  485. oops_end(flags);
  486. do_exit(SIGSEGV);
  487. }
  488. void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
  489. {
  490. unsigned long flags = oops_begin();
  491. /*
  492. * We are in trouble anyway, lets at least try
  493. * to get a message out.
  494. */
  495. printk(str, smp_processor_id());
  496. show_registers(regs);
  497. if (kexec_should_crash(current))
  498. crash_kexec(regs);
  499. if (do_panic || panic_on_oops)
  500. panic("Non maskable interrupt");
  501. oops_end(flags);
  502. nmi_exit();
  503. local_irq_enable();
  504. do_exit(SIGSEGV);
  505. }
  506. static void __kprobes do_trap(int trapnr, int signr, char *str,
  507. struct pt_regs * regs, long error_code,
  508. siginfo_t *info)
  509. {
  510. struct task_struct *tsk = current;
  511. if (user_mode(regs)) {
  512. /*
  513. * We want error_code and trap_no set for userspace
  514. * faults and kernelspace faults which result in
  515. * die(), but not kernelspace faults which are fixed
  516. * up. die() gives the process no chance to handle
  517. * the signal and notice the kernel fault information,
  518. * so that won't result in polluting the information
  519. * about previously queued, but not yet delivered,
  520. * faults. See also do_general_protection below.
  521. */
  522. tsk->thread.error_code = error_code;
  523. tsk->thread.trap_no = trapnr;
  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. tsk->thread.error_code = error_code;
  543. tsk->thread.trap_no = trapnr;
  544. die(str, regs, error_code);
  545. }
  546. return;
  547. }
  548. }
  549. #define DO_ERROR(trapnr, signr, str, name) \
  550. asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
  551. { \
  552. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  553. == NOTIFY_STOP) \
  554. return; \
  555. conditional_sti(regs); \
  556. do_trap(trapnr, signr, str, regs, error_code, NULL); \
  557. }
  558. #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
  559. asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
  560. { \
  561. siginfo_t info; \
  562. info.si_signo = signr; \
  563. info.si_errno = 0; \
  564. info.si_code = sicode; \
  565. info.si_addr = (void __user *)siaddr; \
  566. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  567. == NOTIFY_STOP) \
  568. return; \
  569. conditional_sti(regs); \
  570. do_trap(trapnr, signr, str, regs, error_code, &info); \
  571. }
  572. DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip)
  573. DO_ERROR( 4, SIGSEGV, "overflow", overflow)
  574. DO_ERROR( 5, SIGSEGV, "bounds", bounds)
  575. DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->rip)
  576. DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
  577. DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
  578. DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
  579. DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
  580. DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
  581. DO_ERROR(18, SIGSEGV, "reserved", reserved)
  582. /* Runs on IST stack */
  583. asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
  584. {
  585. if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
  586. 12, SIGBUS) == NOTIFY_STOP)
  587. return;
  588. preempt_conditional_sti(regs);
  589. do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
  590. preempt_conditional_cli(regs);
  591. }
  592. asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
  593. {
  594. static const char str[] = "double fault";
  595. struct task_struct *tsk = current;
  596. /* Return not checked because double check cannot be ignored */
  597. notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
  598. tsk->thread.error_code = error_code;
  599. tsk->thread.trap_no = 8;
  600. /* This is always a kernel trap and never fixable (and thus must
  601. never return). */
  602. for (;;)
  603. die(str, regs, error_code);
  604. }
  605. asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
  606. long error_code)
  607. {
  608. struct task_struct *tsk = current;
  609. conditional_sti(regs);
  610. if (user_mode(regs)) {
  611. tsk->thread.error_code = error_code;
  612. tsk->thread.trap_no = 13;
  613. if (exception_trace && unhandled_signal(tsk, SIGSEGV))
  614. printk(KERN_INFO
  615. "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n",
  616. tsk->comm, tsk->pid,
  617. regs->rip, regs->rsp, error_code);
  618. force_sig(SIGSEGV, tsk);
  619. return;
  620. }
  621. /* kernel gp */
  622. {
  623. const struct exception_table_entry *fixup;
  624. fixup = search_exception_tables(regs->rip);
  625. if (fixup) {
  626. regs->rip = fixup->fixup;
  627. return;
  628. }
  629. tsk->thread.error_code = error_code;
  630. tsk->thread.trap_no = 13;
  631. if (notify_die(DIE_GPF, "general protection fault", regs,
  632. error_code, 13, SIGSEGV) == NOTIFY_STOP)
  633. return;
  634. die("general protection fault", regs, error_code);
  635. }
  636. }
  637. static __kprobes void
  638. mem_parity_error(unsigned char reason, struct pt_regs * regs)
  639. {
  640. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  641. reason);
  642. printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
  643. if (panic_on_unrecovered_nmi)
  644. panic("NMI: Not continuing");
  645. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  646. /* Clear and disable the memory parity error line. */
  647. reason = (reason & 0xf) | 4;
  648. outb(reason, 0x61);
  649. }
  650. static __kprobes void
  651. io_check_error(unsigned char reason, struct pt_regs * regs)
  652. {
  653. printk("NMI: IOCK error (debug interrupt?)\n");
  654. show_registers(regs);
  655. /* Re-enable the IOCK line, wait for a few seconds */
  656. reason = (reason & 0xf) | 8;
  657. outb(reason, 0x61);
  658. mdelay(2000);
  659. reason &= ~8;
  660. outb(reason, 0x61);
  661. }
  662. static __kprobes void
  663. unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
  664. {
  665. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  666. reason);
  667. printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
  668. if (panic_on_unrecovered_nmi)
  669. panic("NMI: Not continuing");
  670. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  671. }
  672. /* Runs on IST stack. This code must keep interrupts off all the time.
  673. Nested NMIs are prevented by the CPU. */
  674. asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
  675. {
  676. unsigned char reason = 0;
  677. int cpu;
  678. cpu = smp_processor_id();
  679. /* Only the BSP gets external NMIs from the system. */
  680. if (!cpu)
  681. reason = get_nmi_reason();
  682. if (!(reason & 0xc0)) {
  683. if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
  684. == NOTIFY_STOP)
  685. return;
  686. /*
  687. * Ok, so this is none of the documented NMI sources,
  688. * so it must be the NMI watchdog.
  689. */
  690. if (nmi_watchdog_tick(regs,reason))
  691. return;
  692. if (!do_nmi_callback(regs,cpu))
  693. unknown_nmi_error(reason, regs);
  694. return;
  695. }
  696. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
  697. return;
  698. /* AK: following checks seem to be broken on modern chipsets. FIXME */
  699. if (reason & 0x80)
  700. mem_parity_error(reason, regs);
  701. if (reason & 0x40)
  702. io_check_error(reason, regs);
  703. }
  704. /* runs on IST stack. */
  705. asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
  706. {
  707. if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
  708. return;
  709. }
  710. preempt_conditional_sti(regs);
  711. do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
  712. preempt_conditional_cli(regs);
  713. }
  714. /* Help handler running on IST stack to switch back to user stack
  715. for scheduling or signal handling. The actual stack switch is done in
  716. entry.S */
  717. asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
  718. {
  719. struct pt_regs *regs = eregs;
  720. /* Did already sync */
  721. if (eregs == (struct pt_regs *)eregs->rsp)
  722. ;
  723. /* Exception from user space */
  724. else if (user_mode(eregs))
  725. regs = task_pt_regs(current);
  726. /* Exception from kernel and interrupts are enabled. Move to
  727. kernel process stack. */
  728. else if (eregs->eflags & X86_EFLAGS_IF)
  729. regs = (struct pt_regs *)(eregs->rsp -= sizeof(struct pt_regs));
  730. if (eregs != regs)
  731. *regs = *eregs;
  732. return regs;
  733. }
  734. /* runs on IST stack. */
  735. asmlinkage void __kprobes do_debug(struct pt_regs * regs,
  736. unsigned long error_code)
  737. {
  738. unsigned long condition;
  739. struct task_struct *tsk = current;
  740. siginfo_t info;
  741. get_debugreg(condition, 6);
  742. if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
  743. SIGTRAP) == NOTIFY_STOP)
  744. return;
  745. preempt_conditional_sti(regs);
  746. /* Mask out spurious debug traps due to lazy DR7 setting */
  747. if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
  748. if (!tsk->thread.debugreg7) {
  749. goto clear_dr7;
  750. }
  751. }
  752. tsk->thread.debugreg6 = condition;
  753. /* Mask out spurious TF errors due to lazy TF clearing */
  754. if (condition & DR_STEP) {
  755. /*
  756. * The TF error should be masked out only if the current
  757. * process is not traced and if the TRAP flag has been set
  758. * previously by a tracing process (condition detected by
  759. * the PT_DTRACE flag); remember that the i386 TRAP flag
  760. * can be modified by the process itself in user mode,
  761. * allowing programs to debug themselves without the ptrace()
  762. * interface.
  763. */
  764. if (!user_mode(regs))
  765. goto clear_TF_reenable;
  766. /*
  767. * Was the TF flag set by a debugger? If so, clear it now,
  768. * so that register information is correct.
  769. */
  770. if (tsk->ptrace & PT_DTRACE) {
  771. regs->eflags &= ~TF_MASK;
  772. tsk->ptrace &= ~PT_DTRACE;
  773. }
  774. }
  775. /* Ok, finally something we can handle */
  776. tsk->thread.trap_no = 1;
  777. tsk->thread.error_code = error_code;
  778. info.si_signo = SIGTRAP;
  779. info.si_errno = 0;
  780. info.si_code = TRAP_BRKPT;
  781. info.si_addr = user_mode(regs) ? (void __user *)regs->rip : NULL;
  782. force_sig_info(SIGTRAP, &info, tsk);
  783. clear_dr7:
  784. set_debugreg(0UL, 7);
  785. preempt_conditional_cli(regs);
  786. return;
  787. clear_TF_reenable:
  788. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  789. regs->eflags &= ~TF_MASK;
  790. preempt_conditional_cli(regs);
  791. }
  792. static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
  793. {
  794. const struct exception_table_entry *fixup;
  795. fixup = search_exception_tables(regs->rip);
  796. if (fixup) {
  797. regs->rip = fixup->fixup;
  798. return 1;
  799. }
  800. notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
  801. /* Illegal floating point operation in the kernel */
  802. current->thread.trap_no = trapnr;
  803. die(str, regs, 0);
  804. return 0;
  805. }
  806. /*
  807. * Note that we play around with the 'TS' bit in an attempt to get
  808. * the correct behaviour even in the presence of the asynchronous
  809. * IRQ13 behaviour
  810. */
  811. asmlinkage void do_coprocessor_error(struct pt_regs *regs)
  812. {
  813. void __user *rip = (void __user *)(regs->rip);
  814. struct task_struct * task;
  815. siginfo_t info;
  816. unsigned short cwd, swd;
  817. conditional_sti(regs);
  818. if (!user_mode(regs) &&
  819. kernel_math_error(regs, "kernel x87 math error", 16))
  820. return;
  821. /*
  822. * Save the info for the exception handler and clear the error.
  823. */
  824. task = current;
  825. save_init_fpu(task);
  826. task->thread.trap_no = 16;
  827. task->thread.error_code = 0;
  828. info.si_signo = SIGFPE;
  829. info.si_errno = 0;
  830. info.si_code = __SI_FAULT;
  831. info.si_addr = rip;
  832. /*
  833. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  834. * status. 0x3f is the exception bits in these regs, 0x200 is the
  835. * C1 reg you need in case of a stack fault, 0x040 is the stack
  836. * fault bit. We should only be taking one exception at a time,
  837. * so if this combination doesn't produce any single exception,
  838. * then we have a bad program that isn't synchronizing its FPU usage
  839. * and it will suffer the consequences since we won't be able to
  840. * fully reproduce the context of the exception
  841. */
  842. cwd = get_fpu_cwd(task);
  843. swd = get_fpu_swd(task);
  844. switch (swd & ~cwd & 0x3f) {
  845. case 0x000:
  846. default:
  847. break;
  848. case 0x001: /* Invalid Op */
  849. /*
  850. * swd & 0x240 == 0x040: Stack Underflow
  851. * swd & 0x240 == 0x240: Stack Overflow
  852. * User must clear the SF bit (0x40) if set
  853. */
  854. info.si_code = FPE_FLTINV;
  855. break;
  856. case 0x002: /* Denormalize */
  857. case 0x010: /* Underflow */
  858. info.si_code = FPE_FLTUND;
  859. break;
  860. case 0x004: /* Zero Divide */
  861. info.si_code = FPE_FLTDIV;
  862. break;
  863. case 0x008: /* Overflow */
  864. info.si_code = FPE_FLTOVF;
  865. break;
  866. case 0x020: /* Precision */
  867. info.si_code = FPE_FLTRES;
  868. break;
  869. }
  870. force_sig_info(SIGFPE, &info, task);
  871. }
  872. asmlinkage void bad_intr(void)
  873. {
  874. printk("bad interrupt");
  875. }
  876. asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
  877. {
  878. void __user *rip = (void __user *)(regs->rip);
  879. struct task_struct * task;
  880. siginfo_t info;
  881. unsigned short mxcsr;
  882. conditional_sti(regs);
  883. if (!user_mode(regs) &&
  884. kernel_math_error(regs, "kernel simd math error", 19))
  885. return;
  886. /*
  887. * Save the info for the exception handler and clear the error.
  888. */
  889. task = current;
  890. save_init_fpu(task);
  891. task->thread.trap_no = 19;
  892. task->thread.error_code = 0;
  893. info.si_signo = SIGFPE;
  894. info.si_errno = 0;
  895. info.si_code = __SI_FAULT;
  896. info.si_addr = rip;
  897. /*
  898. * The SIMD FPU exceptions are handled a little differently, as there
  899. * is only a single status/control register. Thus, to determine which
  900. * unmasked exception was caught we must mask the exception mask bits
  901. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  902. */
  903. mxcsr = get_fpu_mxcsr(task);
  904. switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
  905. case 0x000:
  906. default:
  907. break;
  908. case 0x001: /* Invalid Op */
  909. info.si_code = FPE_FLTINV;
  910. break;
  911. case 0x002: /* Denormalize */
  912. case 0x010: /* Underflow */
  913. info.si_code = FPE_FLTUND;
  914. break;
  915. case 0x004: /* Zero Divide */
  916. info.si_code = FPE_FLTDIV;
  917. break;
  918. case 0x008: /* Overflow */
  919. info.si_code = FPE_FLTOVF;
  920. break;
  921. case 0x020: /* Precision */
  922. info.si_code = FPE_FLTRES;
  923. break;
  924. }
  925. force_sig_info(SIGFPE, &info, task);
  926. }
  927. asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
  928. {
  929. }
  930. asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
  931. {
  932. }
  933. asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
  934. {
  935. }
  936. /*
  937. * 'math_state_restore()' saves the current math information in the
  938. * old math state array, and gets the new ones from the current task
  939. *
  940. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  941. * Don't touch unless you *really* know how it works.
  942. */
  943. asmlinkage void math_state_restore(void)
  944. {
  945. struct task_struct *me = current;
  946. clts(); /* Allow maths ops (or we recurse) */
  947. if (!used_math())
  948. init_fpu(me);
  949. restore_fpu_checking(&me->thread.i387.fxsave);
  950. task_thread_info(me)->status |= TS_USEDFPU;
  951. me->fpu_counter++;
  952. }
  953. void __init trap_init(void)
  954. {
  955. set_intr_gate(0,&divide_error);
  956. set_intr_gate_ist(1,&debug,DEBUG_STACK);
  957. set_intr_gate_ist(2,&nmi,NMI_STACK);
  958. set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
  959. set_system_gate(4,&overflow); /* int4 can be called from all */
  960. set_intr_gate(5,&bounds);
  961. set_intr_gate(6,&invalid_op);
  962. set_intr_gate(7,&device_not_available);
  963. set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
  964. set_intr_gate(9,&coprocessor_segment_overrun);
  965. set_intr_gate(10,&invalid_TSS);
  966. set_intr_gate(11,&segment_not_present);
  967. set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
  968. set_intr_gate(13,&general_protection);
  969. set_intr_gate(14,&page_fault);
  970. set_intr_gate(15,&spurious_interrupt_bug);
  971. set_intr_gate(16,&coprocessor_error);
  972. set_intr_gate(17,&alignment_check);
  973. #ifdef CONFIG_X86_MCE
  974. set_intr_gate_ist(18,&machine_check, MCE_STACK);
  975. #endif
  976. set_intr_gate(19,&simd_coprocessor_error);
  977. #ifdef CONFIG_IA32_EMULATION
  978. set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  979. #endif
  980. /*
  981. * Should be a barrier for any external CPU state.
  982. */
  983. cpu_init();
  984. }
  985. static int __init oops_setup(char *s)
  986. {
  987. if (!s)
  988. return -EINVAL;
  989. if (!strcmp(s, "panic"))
  990. panic_on_oops = 1;
  991. return 0;
  992. }
  993. early_param("oops", oops_setup);
  994. static int __init kstack_setup(char *s)
  995. {
  996. if (!s)
  997. return -EINVAL;
  998. kstack_depth_to_print = simple_strtoul(s,NULL,0);
  999. return 0;
  1000. }
  1001. early_param("kstack", kstack_setup);