traps.c 31 KB

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