traps_32.c 31 KB

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