traps.c 28 KB

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