traps.c 28 KB

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