traps.c 30 KB

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