traps.c 30 KB

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