traps.c 30 KB

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