traps.c 31 KB

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