traps.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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 = smp_processor_id();
  422. unsigned long flags;
  423. oops_enter();
  424. /* racy, but better than risking deadlock. */
  425. local_irq_save(flags);
  426. if (!spin_trylock(&die_lock)) {
  427. if (cpu == die_owner)
  428. /* nested oops. should stop eventually */;
  429. else
  430. spin_lock(&die_lock);
  431. }
  432. die_nest_count++;
  433. die_owner = cpu;
  434. console_verbose();
  435. bust_spinlocks(1);
  436. return flags;
  437. }
  438. void __kprobes oops_end(unsigned long flags)
  439. {
  440. die_owner = -1;
  441. bust_spinlocks(0);
  442. die_nest_count--;
  443. if (die_nest_count)
  444. /* We still own the lock */
  445. local_irq_restore(flags);
  446. else
  447. /* Nest count reaches zero, release the lock. */
  448. spin_unlock_irqrestore(&die_lock, flags);
  449. if (panic_on_oops)
  450. panic("Fatal exception");
  451. oops_exit();
  452. }
  453. void __kprobes __die(const char * str, struct pt_regs * regs, long err)
  454. {
  455. static int die_counter;
  456. printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
  457. #ifdef CONFIG_PREEMPT
  458. printk("PREEMPT ");
  459. #endif
  460. #ifdef CONFIG_SMP
  461. printk("SMP ");
  462. #endif
  463. #ifdef CONFIG_DEBUG_PAGEALLOC
  464. printk("DEBUG_PAGEALLOC");
  465. #endif
  466. printk("\n");
  467. notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV);
  468. show_registers(regs);
  469. /* Executive summary in case the oops scrolled away */
  470. printk(KERN_ALERT "RIP ");
  471. printk_address(regs->rip);
  472. printk(" RSP <%016lx>\n", regs->rsp);
  473. if (kexec_should_crash(current))
  474. crash_kexec(regs);
  475. }
  476. void die(const char * str, struct pt_regs * regs, long err)
  477. {
  478. unsigned long flags = oops_begin();
  479. if (!user_mode(regs))
  480. report_bug(regs->rip);
  481. __die(str, regs, err);
  482. oops_end(flags);
  483. do_exit(SIGSEGV);
  484. }
  485. void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
  486. {
  487. unsigned long flags = oops_begin();
  488. /*
  489. * We are in trouble anyway, lets at least try
  490. * to get a message out.
  491. */
  492. printk(str, smp_processor_id());
  493. show_registers(regs);
  494. if (kexec_should_crash(current))
  495. crash_kexec(regs);
  496. if (do_panic || panic_on_oops)
  497. panic("Non maskable interrupt");
  498. oops_end(flags);
  499. nmi_exit();
  500. local_irq_enable();
  501. do_exit(SIGSEGV);
  502. }
  503. static void __kprobes do_trap(int trapnr, int signr, char *str,
  504. struct pt_regs * regs, long error_code,
  505. siginfo_t *info)
  506. {
  507. struct task_struct *tsk = current;
  508. if (user_mode(regs)) {
  509. /*
  510. * We want error_code and trap_no set for userspace
  511. * faults and kernelspace faults which result in
  512. * die(), but not kernelspace faults which are fixed
  513. * up. die() gives the process no chance to handle
  514. * the signal and notice the kernel fault information,
  515. * so that won't result in polluting the information
  516. * about previously queued, but not yet delivered,
  517. * faults. See also do_general_protection below.
  518. */
  519. tsk->thread.error_code = error_code;
  520. tsk->thread.trap_no = trapnr;
  521. if (exception_trace && unhandled_signal(tsk, signr))
  522. printk(KERN_INFO
  523. "%s[%d] trap %s rip:%lx rsp:%lx error:%lx\n",
  524. tsk->comm, tsk->pid, str,
  525. regs->rip, regs->rsp, error_code);
  526. if (info)
  527. force_sig_info(signr, info, tsk);
  528. else
  529. force_sig(signr, tsk);
  530. return;
  531. }
  532. /* kernel trap */
  533. {
  534. const struct exception_table_entry *fixup;
  535. fixup = search_exception_tables(regs->rip);
  536. if (fixup)
  537. regs->rip = fixup->fixup;
  538. else {
  539. tsk->thread.error_code = error_code;
  540. tsk->thread.trap_no = trapnr;
  541. die(str, regs, error_code);
  542. }
  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. if (user_mode(regs)) {
  608. tsk->thread.error_code = error_code;
  609. tsk->thread.trap_no = 13;
  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. tsk->thread.error_code = error_code;
  627. tsk->thread.trap_no = 13;
  628. if (notify_die(DIE_GPF, "general protection fault", regs,
  629. error_code, 13, SIGSEGV) == NOTIFY_STOP)
  630. return;
  631. die("general protection fault", regs, error_code);
  632. }
  633. }
  634. static __kprobes void
  635. mem_parity_error(unsigned char reason, struct pt_regs * regs)
  636. {
  637. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  638. reason);
  639. printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
  640. if (panic_on_unrecovered_nmi)
  641. panic("NMI: Not continuing");
  642. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  643. /* Clear and disable the memory parity error line. */
  644. reason = (reason & 0xf) | 4;
  645. outb(reason, 0x61);
  646. }
  647. static __kprobes void
  648. io_check_error(unsigned char reason, struct pt_regs * regs)
  649. {
  650. printk("NMI: IOCK error (debug interrupt?)\n");
  651. show_registers(regs);
  652. /* Re-enable the IOCK line, wait for a few seconds */
  653. reason = (reason & 0xf) | 8;
  654. outb(reason, 0x61);
  655. mdelay(2000);
  656. reason &= ~8;
  657. outb(reason, 0x61);
  658. }
  659. static __kprobes void
  660. unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
  661. {
  662. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  663. reason);
  664. printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
  665. if (panic_on_unrecovered_nmi)
  666. panic("NMI: Not continuing");
  667. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  668. }
  669. /* Runs on IST stack. This code must keep interrupts off all the time.
  670. Nested NMIs are prevented by the CPU. */
  671. asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
  672. {
  673. unsigned char reason = 0;
  674. int cpu;
  675. cpu = smp_processor_id();
  676. /* Only the BSP gets external NMIs from the system. */
  677. if (!cpu)
  678. reason = get_nmi_reason();
  679. if (!(reason & 0xc0)) {
  680. if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
  681. == NOTIFY_STOP)
  682. return;
  683. /*
  684. * Ok, so this is none of the documented NMI sources,
  685. * so it must be the NMI watchdog.
  686. */
  687. if (nmi_watchdog_tick(regs,reason))
  688. return;
  689. if (!do_nmi_callback(regs,cpu))
  690. unknown_nmi_error(reason, regs);
  691. return;
  692. }
  693. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
  694. return;
  695. /* AK: following checks seem to be broken on modern chipsets. FIXME */
  696. if (reason & 0x80)
  697. mem_parity_error(reason, regs);
  698. if (reason & 0x40)
  699. io_check_error(reason, regs);
  700. }
  701. /* runs on IST stack. */
  702. asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
  703. {
  704. if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
  705. return;
  706. }
  707. preempt_conditional_sti(regs);
  708. do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
  709. preempt_conditional_cli(regs);
  710. }
  711. /* Help handler running on IST stack to switch back to user stack
  712. for scheduling or signal handling. The actual stack switch is done in
  713. entry.S */
  714. asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
  715. {
  716. struct pt_regs *regs = eregs;
  717. /* Did already sync */
  718. if (eregs == (struct pt_regs *)eregs->rsp)
  719. ;
  720. /* Exception from user space */
  721. else if (user_mode(eregs))
  722. regs = task_pt_regs(current);
  723. /* Exception from kernel and interrupts are enabled. Move to
  724. kernel process stack. */
  725. else if (eregs->eflags & X86_EFLAGS_IF)
  726. regs = (struct pt_regs *)(eregs->rsp -= sizeof(struct pt_regs));
  727. if (eregs != regs)
  728. *regs = *eregs;
  729. return regs;
  730. }
  731. /* runs on IST stack. */
  732. asmlinkage void __kprobes do_debug(struct pt_regs * regs,
  733. unsigned long error_code)
  734. {
  735. unsigned long condition;
  736. struct task_struct *tsk = current;
  737. siginfo_t info;
  738. get_debugreg(condition, 6);
  739. if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
  740. SIGTRAP) == NOTIFY_STOP)
  741. return;
  742. preempt_conditional_sti(regs);
  743. /* Mask out spurious debug traps due to lazy DR7 setting */
  744. if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
  745. if (!tsk->thread.debugreg7) {
  746. goto clear_dr7;
  747. }
  748. }
  749. tsk->thread.debugreg6 = condition;
  750. /* Mask out spurious TF errors due to lazy TF clearing */
  751. if (condition & DR_STEP) {
  752. /*
  753. * The TF error should be masked out only if the current
  754. * process is not traced and if the TRAP flag has been set
  755. * previously by a tracing process (condition detected by
  756. * the PT_DTRACE flag); remember that the i386 TRAP flag
  757. * can be modified by the process itself in user mode,
  758. * allowing programs to debug themselves without the ptrace()
  759. * interface.
  760. */
  761. if (!user_mode(regs))
  762. goto clear_TF_reenable;
  763. /*
  764. * Was the TF flag set by a debugger? If so, clear it now,
  765. * so that register information is correct.
  766. */
  767. if (tsk->ptrace & PT_DTRACE) {
  768. regs->eflags &= ~TF_MASK;
  769. tsk->ptrace &= ~PT_DTRACE;
  770. }
  771. }
  772. /* Ok, finally something we can handle */
  773. tsk->thread.trap_no = 1;
  774. tsk->thread.error_code = error_code;
  775. info.si_signo = SIGTRAP;
  776. info.si_errno = 0;
  777. info.si_code = TRAP_BRKPT;
  778. info.si_addr = user_mode(regs) ? (void __user *)regs->rip : NULL;
  779. force_sig_info(SIGTRAP, &info, tsk);
  780. clear_dr7:
  781. set_debugreg(0UL, 7);
  782. preempt_conditional_cli(regs);
  783. return;
  784. clear_TF_reenable:
  785. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  786. regs->eflags &= ~TF_MASK;
  787. preempt_conditional_cli(regs);
  788. }
  789. static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
  790. {
  791. const struct exception_table_entry *fixup;
  792. fixup = search_exception_tables(regs->rip);
  793. if (fixup) {
  794. regs->rip = fixup->fixup;
  795. return 1;
  796. }
  797. notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
  798. /* Illegal floating point operation in the kernel */
  799. current->thread.trap_no = trapnr;
  800. die(str, regs, 0);
  801. return 0;
  802. }
  803. /*
  804. * Note that we play around with the 'TS' bit in an attempt to get
  805. * the correct behaviour even in the presence of the asynchronous
  806. * IRQ13 behaviour
  807. */
  808. asmlinkage void do_coprocessor_error(struct pt_regs *regs)
  809. {
  810. void __user *rip = (void __user *)(regs->rip);
  811. struct task_struct * task;
  812. siginfo_t info;
  813. unsigned short cwd, swd;
  814. conditional_sti(regs);
  815. if (!user_mode(regs) &&
  816. kernel_math_error(regs, "kernel x87 math error", 16))
  817. return;
  818. /*
  819. * Save the info for the exception handler and clear the error.
  820. */
  821. task = current;
  822. save_init_fpu(task);
  823. task->thread.trap_no = 16;
  824. task->thread.error_code = 0;
  825. info.si_signo = SIGFPE;
  826. info.si_errno = 0;
  827. info.si_code = __SI_FAULT;
  828. info.si_addr = rip;
  829. /*
  830. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  831. * status. 0x3f is the exception bits in these regs, 0x200 is the
  832. * C1 reg you need in case of a stack fault, 0x040 is the stack
  833. * fault bit. We should only be taking one exception at a time,
  834. * so if this combination doesn't produce any single exception,
  835. * then we have a bad program that isn't synchronizing its FPU usage
  836. * and it will suffer the consequences since we won't be able to
  837. * fully reproduce the context of the exception
  838. */
  839. cwd = get_fpu_cwd(task);
  840. swd = get_fpu_swd(task);
  841. switch (swd & ~cwd & 0x3f) {
  842. case 0x000:
  843. default:
  844. break;
  845. case 0x001: /* Invalid Op */
  846. /*
  847. * swd & 0x240 == 0x040: Stack Underflow
  848. * swd & 0x240 == 0x240: Stack Overflow
  849. * User must clear the SF bit (0x40) if set
  850. */
  851. info.si_code = FPE_FLTINV;
  852. break;
  853. case 0x002: /* Denormalize */
  854. case 0x010: /* Underflow */
  855. info.si_code = FPE_FLTUND;
  856. break;
  857. case 0x004: /* Zero Divide */
  858. info.si_code = FPE_FLTDIV;
  859. break;
  860. case 0x008: /* Overflow */
  861. info.si_code = FPE_FLTOVF;
  862. break;
  863. case 0x020: /* Precision */
  864. info.si_code = FPE_FLTRES;
  865. break;
  866. }
  867. force_sig_info(SIGFPE, &info, task);
  868. }
  869. asmlinkage void bad_intr(void)
  870. {
  871. printk("bad interrupt");
  872. }
  873. asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
  874. {
  875. void __user *rip = (void __user *)(regs->rip);
  876. struct task_struct * task;
  877. siginfo_t info;
  878. unsigned short mxcsr;
  879. conditional_sti(regs);
  880. if (!user_mode(regs) &&
  881. kernel_math_error(regs, "kernel simd math error", 19))
  882. return;
  883. /*
  884. * Save the info for the exception handler and clear the error.
  885. */
  886. task = current;
  887. save_init_fpu(task);
  888. task->thread.trap_no = 19;
  889. task->thread.error_code = 0;
  890. info.si_signo = SIGFPE;
  891. info.si_errno = 0;
  892. info.si_code = __SI_FAULT;
  893. info.si_addr = rip;
  894. /*
  895. * The SIMD FPU exceptions are handled a little differently, as there
  896. * is only a single status/control register. Thus, to determine which
  897. * unmasked exception was caught we must mask the exception mask bits
  898. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  899. */
  900. mxcsr = get_fpu_mxcsr(task);
  901. switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
  902. case 0x000:
  903. default:
  904. break;
  905. case 0x001: /* Invalid Op */
  906. info.si_code = FPE_FLTINV;
  907. break;
  908. case 0x002: /* Denormalize */
  909. case 0x010: /* Underflow */
  910. info.si_code = FPE_FLTUND;
  911. break;
  912. case 0x004: /* Zero Divide */
  913. info.si_code = FPE_FLTDIV;
  914. break;
  915. case 0x008: /* Overflow */
  916. info.si_code = FPE_FLTOVF;
  917. break;
  918. case 0x020: /* Precision */
  919. info.si_code = FPE_FLTRES;
  920. break;
  921. }
  922. force_sig_info(SIGFPE, &info, task);
  923. }
  924. asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
  925. {
  926. }
  927. asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
  928. {
  929. }
  930. asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
  931. {
  932. }
  933. /*
  934. * 'math_state_restore()' saves the current math information in the
  935. * old math state array, and gets the new ones from the current task
  936. *
  937. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  938. * Don't touch unless you *really* know how it works.
  939. */
  940. asmlinkage void math_state_restore(void)
  941. {
  942. struct task_struct *me = current;
  943. clts(); /* Allow maths ops (or we recurse) */
  944. if (!used_math())
  945. init_fpu(me);
  946. restore_fpu_checking(&me->thread.i387.fxsave);
  947. task_thread_info(me)->status |= TS_USEDFPU;
  948. me->fpu_counter++;
  949. }
  950. void __init trap_init(void)
  951. {
  952. set_intr_gate(0,&divide_error);
  953. set_intr_gate_ist(1,&debug,DEBUG_STACK);
  954. set_intr_gate_ist(2,&nmi,NMI_STACK);
  955. set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
  956. set_system_gate(4,&overflow); /* int4 can be called from all */
  957. set_intr_gate(5,&bounds);
  958. set_intr_gate(6,&invalid_op);
  959. set_intr_gate(7,&device_not_available);
  960. set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
  961. set_intr_gate(9,&coprocessor_segment_overrun);
  962. set_intr_gate(10,&invalid_TSS);
  963. set_intr_gate(11,&segment_not_present);
  964. set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
  965. set_intr_gate(13,&general_protection);
  966. set_intr_gate(14,&page_fault);
  967. set_intr_gate(15,&spurious_interrupt_bug);
  968. set_intr_gate(16,&coprocessor_error);
  969. set_intr_gate(17,&alignment_check);
  970. #ifdef CONFIG_X86_MCE
  971. set_intr_gate_ist(18,&machine_check, MCE_STACK);
  972. #endif
  973. set_intr_gate(19,&simd_coprocessor_error);
  974. #ifdef CONFIG_IA32_EMULATION
  975. set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  976. #endif
  977. /*
  978. * Should be a barrier for any external CPU state.
  979. */
  980. cpu_init();
  981. }
  982. static int __init oops_setup(char *s)
  983. {
  984. if (!s)
  985. return -EINVAL;
  986. if (!strcmp(s, "panic"))
  987. panic_on_oops = 1;
  988. return 0;
  989. }
  990. early_param("oops", oops_setup);
  991. static int __init kstack_setup(char *s)
  992. {
  993. if (!s)
  994. return -EINVAL;
  995. kstack_depth_to_print = simple_strtoul(s,NULL,0);
  996. return 0;
  997. }
  998. early_param("kstack", kstack_setup);