traps.c 28 KB

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