traps.c 31 KB

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