traps.c 30 KB

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