traps.c 30 KB

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