traps.c 30 KB

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