traps.c 28 KB

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