traps.c 30 KB

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