traps_32.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. *
  4. * Pentium III FXSR, SSE support
  5. * Gareth Hughes <gareth@valinux.com>, May 2000
  6. */
  7. /*
  8. * 'Traps.c' handles hardware traps and faults after we have saved some
  9. * state in 'asm.s'.
  10. */
  11. #include <linux/sched.h>
  12. #include <linux/kernel.h>
  13. #include <linux/string.h>
  14. #include <linux/errno.h>
  15. #include <linux/timer.h>
  16. #include <linux/mm.h>
  17. #include <linux/init.h>
  18. #include <linux/delay.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/highmem.h>
  22. #include <linux/kallsyms.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/utsname.h>
  25. #include <linux/kprobes.h>
  26. #include <linux/kexec.h>
  27. #include <linux/unwind.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/nmi.h>
  30. #include <linux/bug.h>
  31. #ifdef CONFIG_EISA
  32. #include <linux/ioport.h>
  33. #include <linux/eisa.h>
  34. #endif
  35. #ifdef CONFIG_MCA
  36. #include <linux/mca.h>
  37. #endif
  38. #if defined(CONFIG_EDAC)
  39. #include <linux/edac.h>
  40. #endif
  41. #include <asm/processor.h>
  42. #include <asm/system.h>
  43. #include <asm/io.h>
  44. #include <asm/atomic.h>
  45. #include <asm/debugreg.h>
  46. #include <asm/desc.h>
  47. #include <asm/i387.h>
  48. #include <asm/nmi.h>
  49. #include <asm/unwind.h>
  50. #include <asm/smp.h>
  51. #include <asm/arch_hooks.h>
  52. #include <linux/kdebug.h>
  53. #include <asm/stacktrace.h>
  54. #include <linux/module.h>
  55. #include "mach_traps.h"
  56. int panic_on_unrecovered_nmi;
  57. DECLARE_BITMAP(used_vectors, NR_VECTORS);
  58. EXPORT_SYMBOL_GPL(used_vectors);
  59. asmlinkage int system_call(void);
  60. /* Do we ignore FPU interrupts ? */
  61. char ignore_fpu_irq = 0;
  62. /*
  63. * The IDT has to be page-aligned to simplify the Pentium
  64. * F0 0F bug workaround.. We have a special link segment
  65. * for this.
  66. */
  67. gate_desc idt_table[256]
  68. __attribute__((__section__(".data.idt"))) = { { { { 0, 0 } } }, };
  69. asmlinkage void divide_error(void);
  70. asmlinkage void debug(void);
  71. asmlinkage void nmi(void);
  72. asmlinkage void int3(void);
  73. asmlinkage void overflow(void);
  74. asmlinkage void bounds(void);
  75. asmlinkage void invalid_op(void);
  76. asmlinkage void device_not_available(void);
  77. asmlinkage void coprocessor_segment_overrun(void);
  78. asmlinkage void invalid_TSS(void);
  79. asmlinkage void segment_not_present(void);
  80. asmlinkage void stack_segment(void);
  81. asmlinkage void general_protection(void);
  82. asmlinkage void page_fault(void);
  83. asmlinkage void coprocessor_error(void);
  84. asmlinkage void simd_coprocessor_error(void);
  85. asmlinkage void alignment_check(void);
  86. asmlinkage void spurious_interrupt_bug(void);
  87. asmlinkage void machine_check(void);
  88. int kstack_depth_to_print = 24;
  89. static unsigned int code_bytes = 64;
  90. static inline int valid_stack_ptr(struct thread_info *tinfo, void *p, unsigned size)
  91. {
  92. return p > (void *)tinfo &&
  93. p <= (void *)tinfo + THREAD_SIZE - size;
  94. }
  95. /* The form of the top of the frame on the stack */
  96. struct stack_frame {
  97. struct stack_frame *next_frame;
  98. unsigned long return_address;
  99. };
  100. static inline unsigned long print_context_stack(struct thread_info *tinfo,
  101. unsigned long *stack, unsigned long bp,
  102. const struct stacktrace_ops *ops, void *data)
  103. {
  104. struct stack_frame *frame = (struct stack_frame *)bp;
  105. /*
  106. * if EBP is "deeper" into the stack than the actual stack pointer,
  107. * we need to rewind the stack pointer a little to start at the
  108. * first stack frame, but only if EBP is in this stack frame.
  109. */
  110. if (stack > (unsigned long *) bp
  111. && valid_stack_ptr(tinfo, frame, sizeof(*frame)))
  112. stack = (unsigned long *) bp;
  113. while (valid_stack_ptr(tinfo, stack, sizeof(*stack))) {
  114. unsigned long addr;
  115. addr = *stack;
  116. if (__kernel_text_address(addr)) {
  117. if ((unsigned long) stack == bp + 4) {
  118. ops->address(data, addr, 1);
  119. frame = frame->next_frame;
  120. bp = (unsigned long) frame;
  121. } else {
  122. ops->address(data, addr, 0);
  123. }
  124. }
  125. stack++;
  126. }
  127. return bp;
  128. }
  129. #define MSG(msg) ops->warning(data, msg)
  130. void dump_trace(struct task_struct *task, struct pt_regs *regs,
  131. unsigned long *stack,
  132. const struct stacktrace_ops *ops, void *data)
  133. {
  134. unsigned long bp = 0;
  135. if (!task)
  136. task = current;
  137. if (!stack) {
  138. unsigned long dummy;
  139. stack = &dummy;
  140. if (task != current)
  141. stack = (unsigned long *)task->thread.sp;
  142. }
  143. #ifdef CONFIG_FRAME_POINTER
  144. if (!bp) {
  145. if (task == current) {
  146. /* Grab bp right from our regs */
  147. asm ("movl %%ebp, %0" : "=r" (bp) : );
  148. } else {
  149. /* bp is the last reg pushed by switch_to */
  150. bp = *(unsigned long *) task->thread.sp;
  151. }
  152. }
  153. #endif
  154. while (1) {
  155. struct thread_info *context;
  156. context = (struct thread_info *)
  157. ((unsigned long)stack & (~(THREAD_SIZE - 1)));
  158. bp = print_context_stack(context, stack, bp, ops, data);
  159. /* Should be after the line below, but somewhere
  160. in early boot context comes out corrupted and we
  161. can't reference it -AK */
  162. if (ops->stack(data, "IRQ") < 0)
  163. break;
  164. stack = (unsigned long*)context->previous_esp;
  165. if (!stack)
  166. break;
  167. touch_nmi_watchdog();
  168. }
  169. }
  170. EXPORT_SYMBOL(dump_trace);
  171. static void
  172. print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
  173. {
  174. printk(data);
  175. print_symbol(msg, symbol);
  176. printk("\n");
  177. }
  178. static void print_trace_warning(void *data, char *msg)
  179. {
  180. printk("%s%s\n", (char *)data, msg);
  181. }
  182. static int print_trace_stack(void *data, char *name)
  183. {
  184. return 0;
  185. }
  186. /*
  187. * Print one address/symbol entries per line.
  188. */
  189. static void print_trace_address(void *data, unsigned long addr, int reliable)
  190. {
  191. printk("%s [<%08lx>] ", (char *)data, addr);
  192. if (!reliable)
  193. printk("? ");
  194. print_symbol("%s\n", addr);
  195. touch_nmi_watchdog();
  196. }
  197. static const struct stacktrace_ops print_trace_ops = {
  198. .warning = print_trace_warning,
  199. .warning_symbol = print_trace_warning_symbol,
  200. .stack = print_trace_stack,
  201. .address = print_trace_address,
  202. };
  203. static void
  204. show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
  205. unsigned long * stack, char *log_lvl)
  206. {
  207. dump_trace(task, regs, stack, &print_trace_ops, log_lvl);
  208. printk("%s =======================\n", log_lvl);
  209. }
  210. void show_trace(struct task_struct *task, struct pt_regs *regs,
  211. unsigned long * stack)
  212. {
  213. show_trace_log_lvl(task, regs, stack, "");
  214. }
  215. static void show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  216. unsigned long *sp, char *log_lvl)
  217. {
  218. unsigned long *stack;
  219. int i;
  220. if (sp == NULL) {
  221. if (task)
  222. sp = (unsigned long*)task->thread.sp;
  223. else
  224. sp = (unsigned long *)&sp;
  225. }
  226. stack = sp;
  227. for(i = 0; i < kstack_depth_to_print; i++) {
  228. if (kstack_end(stack))
  229. break;
  230. if (i && ((i % 8) == 0))
  231. printk("\n%s ", log_lvl);
  232. printk("%08lx ", *stack++);
  233. }
  234. printk("\n%sCall Trace:\n", log_lvl);
  235. show_trace_log_lvl(task, regs, sp, log_lvl);
  236. }
  237. void show_stack(struct task_struct *task, unsigned long *sp)
  238. {
  239. printk(" ");
  240. show_stack_log_lvl(task, NULL, sp, "");
  241. }
  242. /*
  243. * The architecture-independent dump_stack generator
  244. */
  245. void dump_stack(void)
  246. {
  247. unsigned long stack;
  248. printk("Pid: %d, comm: %.20s %s %s %.*s\n",
  249. current->pid, current->comm, print_tainted(),
  250. init_utsname()->release,
  251. (int)strcspn(init_utsname()->version, " "),
  252. init_utsname()->version);
  253. show_trace(current, NULL, &stack);
  254. }
  255. EXPORT_SYMBOL(dump_stack);
  256. void show_registers(struct pt_regs *regs)
  257. {
  258. int i;
  259. print_modules();
  260. __show_registers(regs, 0);
  261. printk(KERN_EMERG "Process %.*s (pid: %d, ti=%p task=%p task.ti=%p)",
  262. TASK_COMM_LEN, current->comm, task_pid_nr(current),
  263. current_thread_info(), current, task_thread_info(current));
  264. /*
  265. * When in-kernel, we also print out the stack and code at the
  266. * time of the fault..
  267. */
  268. if (!user_mode_vm(regs)) {
  269. u8 *ip;
  270. unsigned int code_prologue = code_bytes * 43 / 64;
  271. unsigned int code_len = code_bytes;
  272. unsigned char c;
  273. printk("\n" KERN_EMERG "Stack: ");
  274. show_stack_log_lvl(NULL, regs, &regs->sp, KERN_EMERG);
  275. printk(KERN_EMERG "Code: ");
  276. ip = (u8 *)regs->ip - code_prologue;
  277. if (ip < (u8 *)PAGE_OFFSET ||
  278. probe_kernel_address(ip, c)) {
  279. /* try starting at EIP */
  280. ip = (u8 *)regs->ip;
  281. code_len = code_len - code_prologue + 1;
  282. }
  283. for (i = 0; i < code_len; i++, ip++) {
  284. if (ip < (u8 *)PAGE_OFFSET ||
  285. probe_kernel_address(ip, c)) {
  286. printk(" Bad EIP value.");
  287. break;
  288. }
  289. if (ip == (u8 *)regs->ip)
  290. printk("<%02x> ", c);
  291. else
  292. printk("%02x ", c);
  293. }
  294. }
  295. printk("\n");
  296. }
  297. int is_valid_bugaddr(unsigned long ip)
  298. {
  299. unsigned short ud2;
  300. if (ip < PAGE_OFFSET)
  301. return 0;
  302. if (probe_kernel_address((unsigned short *)ip, ud2))
  303. return 0;
  304. return ud2 == 0x0b0f;
  305. }
  306. static int die_counter;
  307. int __kprobes __die(const char * str, struct pt_regs * regs, long err)
  308. {
  309. unsigned long sp;
  310. unsigned short ss;
  311. printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
  312. #ifdef CONFIG_PREEMPT
  313. printk("PREEMPT ");
  314. #endif
  315. #ifdef CONFIG_SMP
  316. printk("SMP ");
  317. #endif
  318. #ifdef CONFIG_DEBUG_PAGEALLOC
  319. printk("DEBUG_PAGEALLOC");
  320. #endif
  321. printk("\n");
  322. if (notify_die(DIE_OOPS, str, regs, err,
  323. current->thread.trap_no, SIGSEGV) !=
  324. NOTIFY_STOP) {
  325. show_registers(regs);
  326. /* Executive summary in case the oops scrolled away */
  327. sp = (unsigned long) (&regs->sp);
  328. savesegment(ss, ss);
  329. if (user_mode(regs)) {
  330. sp = regs->sp;
  331. ss = regs->ss & 0xffff;
  332. }
  333. printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
  334. print_symbol("%s", regs->ip);
  335. printk(" SS:ESP %04x:%08lx\n", ss, sp);
  336. return 0;
  337. } else {
  338. return 1;
  339. }
  340. }
  341. /*
  342. * This is gone through when something in the kernel has done something bad and
  343. * is about to be terminated.
  344. */
  345. void die(const char * str, struct pt_regs * regs, long err)
  346. {
  347. static struct {
  348. raw_spinlock_t lock;
  349. u32 lock_owner;
  350. int lock_owner_depth;
  351. } die = {
  352. .lock = __RAW_SPIN_LOCK_UNLOCKED,
  353. .lock_owner = -1,
  354. .lock_owner_depth = 0
  355. };
  356. unsigned long flags;
  357. oops_enter();
  358. if (die.lock_owner != raw_smp_processor_id()) {
  359. console_verbose();
  360. raw_local_irq_save(flags);
  361. __raw_spin_lock(&die.lock);
  362. die.lock_owner = smp_processor_id();
  363. die.lock_owner_depth = 0;
  364. bust_spinlocks(1);
  365. } else
  366. raw_local_irq_save(flags);
  367. if (++die.lock_owner_depth < 3) {
  368. report_bug(regs->ip, regs);
  369. if (__die(str, regs, err))
  370. regs = NULL;
  371. } else {
  372. printk(KERN_EMERG "Recursive die() failure, output suppressed\n");
  373. }
  374. bust_spinlocks(0);
  375. die.lock_owner = -1;
  376. add_taint(TAINT_DIE);
  377. __raw_spin_unlock(&die.lock);
  378. raw_local_irq_restore(flags);
  379. if (!regs)
  380. return;
  381. if (kexec_should_crash(current))
  382. crash_kexec(regs);
  383. if (in_interrupt())
  384. panic("Fatal exception in interrupt");
  385. if (panic_on_oops)
  386. panic("Fatal exception");
  387. oops_exit();
  388. do_exit(SIGSEGV);
  389. }
  390. static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
  391. {
  392. if (!user_mode_vm(regs))
  393. die(str, regs, err);
  394. }
  395. static void __kprobes do_trap(int trapnr, int signr, char *str, int vm86,
  396. struct pt_regs * regs, long error_code,
  397. siginfo_t *info)
  398. {
  399. struct task_struct *tsk = current;
  400. if (regs->flags & VM_MASK) {
  401. if (vm86)
  402. goto vm86_trap;
  403. goto trap_signal;
  404. }
  405. if (!user_mode(regs))
  406. goto kernel_trap;
  407. trap_signal: {
  408. /*
  409. * We want error_code and trap_no set for userspace faults and
  410. * kernelspace faults which result in die(), but not
  411. * kernelspace faults which are fixed up. die() gives the
  412. * process no chance to handle the signal and notice the
  413. * kernel fault information, so that won't result in polluting
  414. * the information about previously queued, but not yet
  415. * delivered, faults. See also do_general_protection below.
  416. */
  417. tsk->thread.error_code = error_code;
  418. tsk->thread.trap_no = trapnr;
  419. if (info)
  420. force_sig_info(signr, info, tsk);
  421. else
  422. force_sig(signr, tsk);
  423. return;
  424. }
  425. kernel_trap: {
  426. if (!fixup_exception(regs)) {
  427. tsk->thread.error_code = error_code;
  428. tsk->thread.trap_no = trapnr;
  429. die(str, regs, error_code);
  430. }
  431. return;
  432. }
  433. vm86_trap: {
  434. int ret = handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, trapnr);
  435. if (ret) goto trap_signal;
  436. return;
  437. }
  438. }
  439. #define DO_ERROR(trapnr, signr, str, name) \
  440. void do_##name(struct pt_regs * regs, long error_code) \
  441. { \
  442. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  443. == NOTIFY_STOP) \
  444. return; \
  445. do_trap(trapnr, signr, str, 0, regs, error_code, NULL); \
  446. }
  447. #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr, irq) \
  448. void do_##name(struct pt_regs * regs, long error_code) \
  449. { \
  450. siginfo_t info; \
  451. if (irq) \
  452. local_irq_enable(); \
  453. info.si_signo = signr; \
  454. info.si_errno = 0; \
  455. info.si_code = sicode; \
  456. info.si_addr = (void __user *)siaddr; \
  457. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  458. == NOTIFY_STOP) \
  459. return; \
  460. do_trap(trapnr, signr, str, 0, regs, error_code, &info); \
  461. }
  462. #define DO_VM86_ERROR(trapnr, signr, str, name) \
  463. void do_##name(struct pt_regs * regs, long error_code) \
  464. { \
  465. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  466. == NOTIFY_STOP) \
  467. return; \
  468. do_trap(trapnr, signr, str, 1, regs, error_code, NULL); \
  469. }
  470. #define DO_VM86_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
  471. void do_##name(struct pt_regs * regs, long error_code) \
  472. { \
  473. siginfo_t info; \
  474. info.si_signo = signr; \
  475. info.si_errno = 0; \
  476. info.si_code = sicode; \
  477. info.si_addr = (void __user *)siaddr; \
  478. trace_hardirqs_fixup(); \
  479. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  480. == NOTIFY_STOP) \
  481. return; \
  482. do_trap(trapnr, signr, str, 1, regs, error_code, &info); \
  483. }
  484. DO_VM86_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
  485. #ifndef CONFIG_KPROBES
  486. DO_VM86_ERROR( 3, SIGTRAP, "int3", int3)
  487. #endif
  488. DO_VM86_ERROR( 4, SIGSEGV, "overflow", overflow)
  489. DO_VM86_ERROR( 5, SIGSEGV, "bounds", bounds)
  490. DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip, 0)
  491. DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
  492. DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
  493. DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
  494. DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
  495. DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0, 0)
  496. DO_ERROR_INFO(32, SIGSEGV, "iret exception", iret_error, ILL_BADSTK, 0, 1)
  497. void __kprobes do_general_protection(struct pt_regs * regs,
  498. long error_code)
  499. {
  500. int cpu = get_cpu();
  501. struct tss_struct *tss = &per_cpu(init_tss, cpu);
  502. struct thread_struct *thread = &current->thread;
  503. /*
  504. * Perform the lazy TSS's I/O bitmap copy. If the TSS has an
  505. * invalid offset set (the LAZY one) and the faulting thread has
  506. * a valid I/O bitmap pointer, we copy the I/O bitmap in the TSS
  507. * and we set the offset field correctly. Then we let the CPU to
  508. * restart the faulting instruction.
  509. */
  510. if (tss->x86_tss.io_bitmap_base == INVALID_IO_BITMAP_OFFSET_LAZY &&
  511. thread->io_bitmap_ptr) {
  512. memcpy(tss->io_bitmap, thread->io_bitmap_ptr,
  513. thread->io_bitmap_max);
  514. /*
  515. * If the previously set map was extending to higher ports
  516. * than the current one, pad extra space with 0xff (no access).
  517. */
  518. if (thread->io_bitmap_max < tss->io_bitmap_max)
  519. memset((char *) tss->io_bitmap +
  520. thread->io_bitmap_max, 0xff,
  521. tss->io_bitmap_max - thread->io_bitmap_max);
  522. tss->io_bitmap_max = thread->io_bitmap_max;
  523. tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
  524. tss->io_bitmap_owner = thread;
  525. put_cpu();
  526. return;
  527. }
  528. put_cpu();
  529. if (regs->flags & VM_MASK)
  530. goto gp_in_vm86;
  531. if (!user_mode(regs))
  532. goto gp_in_kernel;
  533. current->thread.error_code = error_code;
  534. current->thread.trap_no = 13;
  535. if (show_unhandled_signals && unhandled_signal(current, SIGSEGV) &&
  536. printk_ratelimit())
  537. printk(KERN_INFO
  538. "%s[%d] general protection ip:%lx sp:%lx error:%lx\n",
  539. current->comm, task_pid_nr(current),
  540. regs->ip, regs->sp, error_code);
  541. force_sig(SIGSEGV, current);
  542. return;
  543. gp_in_vm86:
  544. local_irq_enable();
  545. handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
  546. return;
  547. gp_in_kernel:
  548. if (!fixup_exception(regs)) {
  549. current->thread.error_code = error_code;
  550. current->thread.trap_no = 13;
  551. if (notify_die(DIE_GPF, "general protection fault", regs,
  552. error_code, 13, SIGSEGV) == NOTIFY_STOP)
  553. return;
  554. die("general protection fault", regs, error_code);
  555. }
  556. }
  557. static __kprobes void
  558. mem_parity_error(unsigned char reason, struct pt_regs * regs)
  559. {
  560. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x on "
  561. "CPU %d.\n", reason, smp_processor_id());
  562. printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
  563. #if defined(CONFIG_EDAC)
  564. if(edac_handler_set()) {
  565. edac_atomic_assert_error();
  566. return;
  567. }
  568. #endif
  569. if (panic_on_unrecovered_nmi)
  570. panic("NMI: Not continuing");
  571. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  572. /* Clear and disable the memory parity error line. */
  573. clear_mem_error(reason);
  574. }
  575. static __kprobes void
  576. io_check_error(unsigned char reason, struct pt_regs * regs)
  577. {
  578. unsigned long i;
  579. printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
  580. show_registers(regs);
  581. /* Re-enable the IOCK line, wait for a few seconds */
  582. reason = (reason & 0xf) | 8;
  583. outb(reason, 0x61);
  584. i = 2000;
  585. while (--i) udelay(1000);
  586. reason &= ~8;
  587. outb(reason, 0x61);
  588. }
  589. static __kprobes void
  590. unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
  591. {
  592. #ifdef CONFIG_MCA
  593. /* Might actually be able to figure out what the guilty party
  594. * is. */
  595. if( MCA_bus ) {
  596. mca_handle_nmi();
  597. return;
  598. }
  599. #endif
  600. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x on "
  601. "CPU %d.\n", reason, smp_processor_id());
  602. printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
  603. if (panic_on_unrecovered_nmi)
  604. panic("NMI: Not continuing");
  605. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  606. }
  607. static DEFINE_SPINLOCK(nmi_print_lock);
  608. void __kprobes die_nmi(struct pt_regs *regs, const char *msg)
  609. {
  610. if (notify_die(DIE_NMIWATCHDOG, msg, regs, 0, 2, SIGINT) ==
  611. NOTIFY_STOP)
  612. return;
  613. spin_lock(&nmi_print_lock);
  614. /*
  615. * We are in trouble anyway, lets at least try
  616. * to get a message out.
  617. */
  618. bust_spinlocks(1);
  619. printk(KERN_EMERG "%s", msg);
  620. printk(" on CPU%d, ip %08lx, registers:\n",
  621. smp_processor_id(), regs->ip);
  622. show_registers(regs);
  623. console_silent();
  624. spin_unlock(&nmi_print_lock);
  625. bust_spinlocks(0);
  626. /* If we are in kernel we are probably nested up pretty bad
  627. * and might aswell get out now while we still can.
  628. */
  629. if (!user_mode_vm(regs)) {
  630. current->thread.trap_no = 2;
  631. crash_kexec(regs);
  632. }
  633. do_exit(SIGSEGV);
  634. }
  635. static __kprobes void default_do_nmi(struct pt_regs * regs)
  636. {
  637. unsigned char reason = 0;
  638. /* Only the BSP gets external NMIs from the system. */
  639. if (!smp_processor_id())
  640. reason = get_nmi_reason();
  641. if (!(reason & 0xc0)) {
  642. if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
  643. == NOTIFY_STOP)
  644. return;
  645. #ifdef CONFIG_X86_LOCAL_APIC
  646. /*
  647. * Ok, so this is none of the documented NMI sources,
  648. * so it must be the NMI watchdog.
  649. */
  650. if (nmi_watchdog_tick(regs, reason))
  651. return;
  652. if (!do_nmi_callback(regs, smp_processor_id()))
  653. #endif
  654. unknown_nmi_error(reason, regs);
  655. return;
  656. }
  657. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
  658. return;
  659. if (reason & 0x80)
  660. mem_parity_error(reason, regs);
  661. if (reason & 0x40)
  662. io_check_error(reason, regs);
  663. /*
  664. * Reassert NMI in case it became active meanwhile
  665. * as it's edge-triggered.
  666. */
  667. reassert_nmi();
  668. }
  669. static int ignore_nmis;
  670. __kprobes void do_nmi(struct pt_regs * regs, long error_code)
  671. {
  672. int cpu;
  673. nmi_enter();
  674. cpu = smp_processor_id();
  675. ++nmi_count(cpu);
  676. if (!ignore_nmis)
  677. default_do_nmi(regs);
  678. nmi_exit();
  679. }
  680. void stop_nmi(void)
  681. {
  682. acpi_nmi_disable();
  683. ignore_nmis++;
  684. }
  685. void restart_nmi(void)
  686. {
  687. ignore_nmis--;
  688. acpi_nmi_enable();
  689. }
  690. #ifdef CONFIG_KPROBES
  691. void __kprobes do_int3(struct pt_regs *regs, long error_code)
  692. {
  693. trace_hardirqs_fixup();
  694. if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
  695. == NOTIFY_STOP)
  696. return;
  697. /* This is an interrupt gate, because kprobes wants interrupts
  698. disabled. Normal trap handlers don't. */
  699. restore_interrupts(regs);
  700. do_trap(3, SIGTRAP, "int3", 1, regs, error_code, NULL);
  701. }
  702. #endif
  703. /*
  704. * Our handling of the processor debug registers is non-trivial.
  705. * We do not clear them on entry and exit from the kernel. Therefore
  706. * it is possible to get a watchpoint trap here from inside the kernel.
  707. * However, the code in ./ptrace.c has ensured that the user can
  708. * only set watchpoints on userspace addresses. Therefore the in-kernel
  709. * watchpoint trap can only occur in code which is reading/writing
  710. * from user space. Such code must not hold kernel locks (since it
  711. * can equally take a page fault), therefore it is safe to call
  712. * force_sig_info even though that claims and releases locks.
  713. *
  714. * Code in ./signal.c ensures that the debug control register
  715. * is restored before we deliver any signal, and therefore that
  716. * user code runs with the correct debug control register even though
  717. * we clear it here.
  718. *
  719. * Being careful here means that we don't have to be as careful in a
  720. * lot of more complicated places (task switching can be a bit lazy
  721. * about restoring all the debug state, and ptrace doesn't have to
  722. * find every occurrence of the TF bit that could be saved away even
  723. * by user code)
  724. */
  725. void __kprobes do_debug(struct pt_regs * regs, long error_code)
  726. {
  727. unsigned int condition;
  728. struct task_struct *tsk = current;
  729. trace_hardirqs_fixup();
  730. get_debugreg(condition, 6);
  731. /*
  732. * The processor cleared BTF, so don't mark that we need it set.
  733. */
  734. clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
  735. tsk->thread.debugctlmsr = 0;
  736. if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
  737. SIGTRAP) == NOTIFY_STOP)
  738. return;
  739. /* It's safe to allow irq's after DR6 has been saved */
  740. if (regs->flags & X86_EFLAGS_IF)
  741. local_irq_enable();
  742. /* Mask out spurious debug traps due to lazy DR7 setting */
  743. if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
  744. if (!tsk->thread.debugreg7)
  745. goto clear_dr7;
  746. }
  747. if (regs->flags & VM_MASK)
  748. goto debug_vm86;
  749. /* Save debug status register where ptrace can see it */
  750. tsk->thread.debugreg6 = condition;
  751. /*
  752. * Single-stepping through TF: make sure we ignore any events in
  753. * kernel space (but re-enable TF when returning to user mode).
  754. */
  755. if (condition & DR_STEP) {
  756. /*
  757. * We already checked v86 mode above, so we can
  758. * check for kernel mode by just checking the CPL
  759. * of CS.
  760. */
  761. if (!user_mode(regs))
  762. goto clear_TF_reenable;
  763. }
  764. /* Ok, finally something we can handle */
  765. send_sigtrap(tsk, regs, error_code);
  766. /* Disable additional traps. They'll be re-enabled when
  767. * the signal is delivered.
  768. */
  769. clear_dr7:
  770. set_debugreg(0, 7);
  771. return;
  772. debug_vm86:
  773. handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
  774. return;
  775. clear_TF_reenable:
  776. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  777. regs->flags &= ~TF_MASK;
  778. return;
  779. }
  780. /*
  781. * Note that we play around with the 'TS' bit in an attempt to get
  782. * the correct behaviour even in the presence of the asynchronous
  783. * IRQ13 behaviour
  784. */
  785. void math_error(void __user *ip)
  786. {
  787. struct task_struct * task;
  788. siginfo_t info;
  789. unsigned short cwd, swd;
  790. /*
  791. * Save the info for the exception handler and clear the error.
  792. */
  793. task = current;
  794. save_init_fpu(task);
  795. task->thread.trap_no = 16;
  796. task->thread.error_code = 0;
  797. info.si_signo = SIGFPE;
  798. info.si_errno = 0;
  799. info.si_code = __SI_FAULT;
  800. info.si_addr = ip;
  801. /*
  802. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  803. * status. 0x3f is the exception bits in these regs, 0x200 is the
  804. * C1 reg you need in case of a stack fault, 0x040 is the stack
  805. * fault bit. We should only be taking one exception at a time,
  806. * so if this combination doesn't produce any single exception,
  807. * then we have a bad program that isn't syncronizing its FPU usage
  808. * and it will suffer the consequences since we won't be able to
  809. * fully reproduce the context of the exception
  810. */
  811. cwd = get_fpu_cwd(task);
  812. swd = get_fpu_swd(task);
  813. switch (swd & ~cwd & 0x3f) {
  814. case 0x000: /* No unmasked exception */
  815. return;
  816. default: /* Multiple exceptions */
  817. break;
  818. case 0x001: /* Invalid Op */
  819. /*
  820. * swd & 0x240 == 0x040: Stack Underflow
  821. * swd & 0x240 == 0x240: Stack Overflow
  822. * User must clear the SF bit (0x40) if set
  823. */
  824. info.si_code = FPE_FLTINV;
  825. break;
  826. case 0x002: /* Denormalize */
  827. case 0x010: /* Underflow */
  828. info.si_code = FPE_FLTUND;
  829. break;
  830. case 0x004: /* Zero Divide */
  831. info.si_code = FPE_FLTDIV;
  832. break;
  833. case 0x008: /* Overflow */
  834. info.si_code = FPE_FLTOVF;
  835. break;
  836. case 0x020: /* Precision */
  837. info.si_code = FPE_FLTRES;
  838. break;
  839. }
  840. force_sig_info(SIGFPE, &info, task);
  841. }
  842. void do_coprocessor_error(struct pt_regs * regs, long error_code)
  843. {
  844. ignore_fpu_irq = 1;
  845. math_error((void __user *)regs->ip);
  846. }
  847. static void simd_math_error(void __user *ip)
  848. {
  849. struct task_struct * task;
  850. siginfo_t info;
  851. unsigned short mxcsr;
  852. /*
  853. * Save the info for the exception handler and clear the error.
  854. */
  855. task = current;
  856. save_init_fpu(task);
  857. task->thread.trap_no = 19;
  858. task->thread.error_code = 0;
  859. info.si_signo = SIGFPE;
  860. info.si_errno = 0;
  861. info.si_code = __SI_FAULT;
  862. info.si_addr = ip;
  863. /*
  864. * The SIMD FPU exceptions are handled a little differently, as there
  865. * is only a single status/control register. Thus, to determine which
  866. * unmasked exception was caught we must mask the exception mask bits
  867. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  868. */
  869. mxcsr = get_fpu_mxcsr(task);
  870. switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
  871. case 0x000:
  872. default:
  873. break;
  874. case 0x001: /* Invalid Op */
  875. info.si_code = FPE_FLTINV;
  876. break;
  877. case 0x002: /* Denormalize */
  878. case 0x010: /* Underflow */
  879. info.si_code = FPE_FLTUND;
  880. break;
  881. case 0x004: /* Zero Divide */
  882. info.si_code = FPE_FLTDIV;
  883. break;
  884. case 0x008: /* Overflow */
  885. info.si_code = FPE_FLTOVF;
  886. break;
  887. case 0x020: /* Precision */
  888. info.si_code = FPE_FLTRES;
  889. break;
  890. }
  891. force_sig_info(SIGFPE, &info, task);
  892. }
  893. void do_simd_coprocessor_error(struct pt_regs * regs,
  894. long error_code)
  895. {
  896. if (cpu_has_xmm) {
  897. /* Handle SIMD FPU exceptions on PIII+ processors. */
  898. ignore_fpu_irq = 1;
  899. simd_math_error((void __user *)regs->ip);
  900. } else {
  901. /*
  902. * Handle strange cache flush from user space exception
  903. * in all other cases. This is undocumented behaviour.
  904. */
  905. if (regs->flags & VM_MASK) {
  906. handle_vm86_fault((struct kernel_vm86_regs *)regs,
  907. error_code);
  908. return;
  909. }
  910. current->thread.trap_no = 19;
  911. current->thread.error_code = error_code;
  912. die_if_kernel("cache flush denied", regs, error_code);
  913. force_sig(SIGSEGV, current);
  914. }
  915. }
  916. void do_spurious_interrupt_bug(struct pt_regs * regs,
  917. long error_code)
  918. {
  919. #if 0
  920. /* No need to warn about this any longer. */
  921. printk("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
  922. #endif
  923. }
  924. unsigned long patch_espfix_desc(unsigned long uesp,
  925. unsigned long kesp)
  926. {
  927. struct desc_struct *gdt = __get_cpu_var(gdt_page).gdt;
  928. unsigned long base = (kesp - uesp) & -THREAD_SIZE;
  929. unsigned long new_kesp = kesp - base;
  930. unsigned long lim_pages = (new_kesp | (THREAD_SIZE - 1)) >> PAGE_SHIFT;
  931. __u64 desc = *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS];
  932. /* Set up base for espfix segment */
  933. desc &= 0x00f0ff0000000000ULL;
  934. desc |= ((((__u64)base) << 16) & 0x000000ffffff0000ULL) |
  935. ((((__u64)base) << 32) & 0xff00000000000000ULL) |
  936. ((((__u64)lim_pages) << 32) & 0x000f000000000000ULL) |
  937. (lim_pages & 0xffff);
  938. *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS] = desc;
  939. return new_kesp;
  940. }
  941. /*
  942. * 'math_state_restore()' saves the current math information in the
  943. * old math state array, and gets the new ones from the current task
  944. *
  945. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  946. * Don't touch unless you *really* know how it works.
  947. *
  948. * Must be called with kernel preemption disabled (in this case,
  949. * local interrupts are disabled at the call-site in entry.S).
  950. */
  951. asmlinkage void math_state_restore(void)
  952. {
  953. struct thread_info *thread = current_thread_info();
  954. struct task_struct *tsk = thread->task;
  955. clts(); /* Allow maths ops (or we recurse) */
  956. if (!tsk_used_math(tsk))
  957. init_fpu(tsk);
  958. restore_fpu(tsk);
  959. thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
  960. tsk->fpu_counter++;
  961. }
  962. EXPORT_SYMBOL_GPL(math_state_restore);
  963. #ifndef CONFIG_MATH_EMULATION
  964. asmlinkage void math_emulate(long arg)
  965. {
  966. printk(KERN_EMERG "math-emulation not enabled and no coprocessor found.\n");
  967. printk(KERN_EMERG "killing %s.\n",current->comm);
  968. force_sig(SIGFPE,current);
  969. schedule();
  970. }
  971. #endif /* CONFIG_MATH_EMULATION */
  972. void __init trap_init(void)
  973. {
  974. int i;
  975. #ifdef CONFIG_EISA
  976. void __iomem *p = ioremap(0x0FFFD9, 4);
  977. if (readl(p) == 'E'+('I'<<8)+('S'<<16)+('A'<<24)) {
  978. EISA_bus = 1;
  979. }
  980. iounmap(p);
  981. #endif
  982. #ifdef CONFIG_X86_LOCAL_APIC
  983. init_apic_mappings();
  984. #endif
  985. set_trap_gate(0,&divide_error);
  986. set_intr_gate(1,&debug);
  987. set_intr_gate(2,&nmi);
  988. set_system_intr_gate(3, &int3); /* int3/4 can be called from all */
  989. set_system_gate(4,&overflow);
  990. set_trap_gate(5,&bounds);
  991. set_trap_gate(6,&invalid_op);
  992. set_trap_gate(7,&device_not_available);
  993. set_task_gate(8,GDT_ENTRY_DOUBLEFAULT_TSS);
  994. set_trap_gate(9,&coprocessor_segment_overrun);
  995. set_trap_gate(10,&invalid_TSS);
  996. set_trap_gate(11,&segment_not_present);
  997. set_trap_gate(12,&stack_segment);
  998. set_trap_gate(13,&general_protection);
  999. set_intr_gate(14,&page_fault);
  1000. set_trap_gate(15,&spurious_interrupt_bug);
  1001. set_trap_gate(16,&coprocessor_error);
  1002. set_trap_gate(17,&alignment_check);
  1003. #ifdef CONFIG_X86_MCE
  1004. set_trap_gate(18,&machine_check);
  1005. #endif
  1006. set_trap_gate(19,&simd_coprocessor_error);
  1007. if (cpu_has_fxsr) {
  1008. /*
  1009. * Verify that the FXSAVE/FXRSTOR data will be 16-byte aligned.
  1010. * Generates a compile-time "error: zero width for bit-field" if
  1011. * the alignment is wrong.
  1012. */
  1013. struct fxsrAlignAssert {
  1014. int _:!(offsetof(struct task_struct,
  1015. thread.i387.fxsave) & 15);
  1016. };
  1017. printk(KERN_INFO "Enabling fast FPU save and restore... ");
  1018. set_in_cr4(X86_CR4_OSFXSR);
  1019. printk("done.\n");
  1020. }
  1021. if (cpu_has_xmm) {
  1022. printk(KERN_INFO "Enabling unmasked SIMD FPU exception "
  1023. "support... ");
  1024. set_in_cr4(X86_CR4_OSXMMEXCPT);
  1025. printk("done.\n");
  1026. }
  1027. set_system_gate(SYSCALL_VECTOR,&system_call);
  1028. /* Reserve all the builtin and the syscall vector. */
  1029. for (i = 0; i < FIRST_EXTERNAL_VECTOR; i++)
  1030. set_bit(i, used_vectors);
  1031. set_bit(SYSCALL_VECTOR, used_vectors);
  1032. /*
  1033. * Should be a barrier for any external CPU state.
  1034. */
  1035. cpu_init();
  1036. trap_init_hook();
  1037. }
  1038. static int __init kstack_setup(char *s)
  1039. {
  1040. kstack_depth_to_print = simple_strtoul(s, NULL, 0);
  1041. return 1;
  1042. }
  1043. __setup("kstack=", kstack_setup);
  1044. static int __init code_bytes_setup(char *s)
  1045. {
  1046. code_bytes = simple_strtoul(s, NULL, 0);
  1047. if (code_bytes > 8192)
  1048. code_bytes = 8192;
  1049. return 1;
  1050. }
  1051. __setup("code_bytes=", code_bytes_setup);