traps.c 27 KB

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