traps.c 28 KB

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