traps.c 31 KB

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