traps.c 30 KB

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