traps.c 29 KB

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