traps.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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. force_sig(SIGSEGV, current);
  548. return;
  549. gp_in_vm86:
  550. local_irq_enable();
  551. handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
  552. return;
  553. gp_in_kernel:
  554. if (!fixup_exception(regs)) {
  555. current->thread.error_code = error_code;
  556. current->thread.trap_no = 13;
  557. if (notify_die(DIE_GPF, "general protection fault", regs,
  558. error_code, 13, SIGSEGV) == NOTIFY_STOP)
  559. return;
  560. die("general protection fault", regs, error_code);
  561. }
  562. }
  563. static __kprobes void
  564. mem_parity_error(unsigned char reason, struct pt_regs * regs)
  565. {
  566. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x on "
  567. "CPU %d.\n", reason, smp_processor_id());
  568. printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
  569. #if defined(CONFIG_EDAC)
  570. if(edac_handler_set()) {
  571. edac_atomic_assert_error();
  572. return;
  573. }
  574. #endif
  575. if (panic_on_unrecovered_nmi)
  576. panic("NMI: Not continuing");
  577. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  578. /* Clear and disable the memory parity error line. */
  579. clear_mem_error(reason);
  580. }
  581. static __kprobes void
  582. io_check_error(unsigned char reason, struct pt_regs * regs)
  583. {
  584. unsigned long i;
  585. printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
  586. show_registers(regs);
  587. /* Re-enable the IOCK line, wait for a few seconds */
  588. reason = (reason & 0xf) | 8;
  589. outb(reason, 0x61);
  590. i = 2000;
  591. while (--i) udelay(1000);
  592. reason &= ~8;
  593. outb(reason, 0x61);
  594. }
  595. static __kprobes void
  596. unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
  597. {
  598. #ifdef CONFIG_MCA
  599. /* Might actually be able to figure out what the guilty party
  600. * is. */
  601. if( MCA_bus ) {
  602. mca_handle_nmi();
  603. return;
  604. }
  605. #endif
  606. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x on "
  607. "CPU %d.\n", reason, smp_processor_id());
  608. printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
  609. if (panic_on_unrecovered_nmi)
  610. panic("NMI: Not continuing");
  611. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  612. }
  613. static DEFINE_SPINLOCK(nmi_print_lock);
  614. void __kprobes die_nmi(struct pt_regs *regs, const char *msg)
  615. {
  616. if (notify_die(DIE_NMIWATCHDOG, msg, regs, 0, 2, SIGINT) ==
  617. NOTIFY_STOP)
  618. return;
  619. spin_lock(&nmi_print_lock);
  620. /*
  621. * We are in trouble anyway, lets at least try
  622. * to get a message out.
  623. */
  624. bust_spinlocks(1);
  625. printk(KERN_EMERG "%s", msg);
  626. printk(" on CPU%d, eip %08lx, registers:\n",
  627. smp_processor_id(), regs->eip);
  628. show_registers(regs);
  629. console_silent();
  630. spin_unlock(&nmi_print_lock);
  631. bust_spinlocks(0);
  632. /* If we are in kernel we are probably nested up pretty bad
  633. * and might aswell get out now while we still can.
  634. */
  635. if (!user_mode_vm(regs)) {
  636. current->thread.trap_no = 2;
  637. crash_kexec(regs);
  638. }
  639. do_exit(SIGSEGV);
  640. }
  641. static __kprobes void default_do_nmi(struct pt_regs * regs)
  642. {
  643. unsigned char reason = 0;
  644. /* Only the BSP gets external NMIs from the system. */
  645. if (!smp_processor_id())
  646. reason = get_nmi_reason();
  647. if (!(reason & 0xc0)) {
  648. if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
  649. == NOTIFY_STOP)
  650. return;
  651. #ifdef CONFIG_X86_LOCAL_APIC
  652. /*
  653. * Ok, so this is none of the documented NMI sources,
  654. * so it must be the NMI watchdog.
  655. */
  656. if (nmi_watchdog_tick(regs, reason))
  657. return;
  658. if (!do_nmi_callback(regs, smp_processor_id()))
  659. #endif
  660. unknown_nmi_error(reason, regs);
  661. return;
  662. }
  663. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
  664. return;
  665. if (reason & 0x80)
  666. mem_parity_error(reason, regs);
  667. if (reason & 0x40)
  668. io_check_error(reason, regs);
  669. /*
  670. * Reassert NMI in case it became active meanwhile
  671. * as it's edge-triggered.
  672. */
  673. reassert_nmi();
  674. }
  675. fastcall __kprobes void do_nmi(struct pt_regs * regs, long error_code)
  676. {
  677. int cpu;
  678. nmi_enter();
  679. cpu = smp_processor_id();
  680. ++nmi_count(cpu);
  681. default_do_nmi(regs);
  682. nmi_exit();
  683. }
  684. #ifdef CONFIG_KPROBES
  685. fastcall void __kprobes do_int3(struct pt_regs *regs, long error_code)
  686. {
  687. if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
  688. == NOTIFY_STOP)
  689. return;
  690. /* This is an interrupt gate, because kprobes wants interrupts
  691. disabled. Normal trap handlers don't. */
  692. restore_interrupts(regs);
  693. do_trap(3, SIGTRAP, "int3", 1, regs, error_code, NULL);
  694. }
  695. #endif
  696. /*
  697. * Our handling of the processor debug registers is non-trivial.
  698. * We do not clear them on entry and exit from the kernel. Therefore
  699. * it is possible to get a watchpoint trap here from inside the kernel.
  700. * However, the code in ./ptrace.c has ensured that the user can
  701. * only set watchpoints on userspace addresses. Therefore the in-kernel
  702. * watchpoint trap can only occur in code which is reading/writing
  703. * from user space. Such code must not hold kernel locks (since it
  704. * can equally take a page fault), therefore it is safe to call
  705. * force_sig_info even though that claims and releases locks.
  706. *
  707. * Code in ./signal.c ensures that the debug control register
  708. * is restored before we deliver any signal, and therefore that
  709. * user code runs with the correct debug control register even though
  710. * we clear it here.
  711. *
  712. * Being careful here means that we don't have to be as careful in a
  713. * lot of more complicated places (task switching can be a bit lazy
  714. * about restoring all the debug state, and ptrace doesn't have to
  715. * find every occurrence of the TF bit that could be saved away even
  716. * by user code)
  717. */
  718. fastcall void __kprobes do_debug(struct pt_regs * regs, long error_code)
  719. {
  720. unsigned int condition;
  721. struct task_struct *tsk = current;
  722. get_debugreg(condition, 6);
  723. if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
  724. SIGTRAP) == NOTIFY_STOP)
  725. return;
  726. /* It's safe to allow irq's after DR6 has been saved */
  727. if (regs->eflags & X86_EFLAGS_IF)
  728. local_irq_enable();
  729. /* Mask out spurious debug traps due to lazy DR7 setting */
  730. if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
  731. if (!tsk->thread.debugreg[7])
  732. goto clear_dr7;
  733. }
  734. if (regs->eflags & VM_MASK)
  735. goto debug_vm86;
  736. /* Save debug status register where ptrace can see it */
  737. tsk->thread.debugreg[6] = condition;
  738. /*
  739. * Single-stepping through TF: make sure we ignore any events in
  740. * kernel space (but re-enable TF when returning to user mode).
  741. */
  742. if (condition & DR_STEP) {
  743. /*
  744. * We already checked v86 mode above, so we can
  745. * check for kernel mode by just checking the CPL
  746. * of CS.
  747. */
  748. if (!user_mode(regs))
  749. goto clear_TF_reenable;
  750. }
  751. /* Ok, finally something we can handle */
  752. send_sigtrap(tsk, regs, error_code);
  753. /* Disable additional traps. They'll be re-enabled when
  754. * the signal is delivered.
  755. */
  756. clear_dr7:
  757. set_debugreg(0, 7);
  758. return;
  759. debug_vm86:
  760. handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
  761. return;
  762. clear_TF_reenable:
  763. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  764. regs->eflags &= ~TF_MASK;
  765. return;
  766. }
  767. /*
  768. * Note that we play around with the 'TS' bit in an attempt to get
  769. * the correct behaviour even in the presence of the asynchronous
  770. * IRQ13 behaviour
  771. */
  772. void math_error(void __user *eip)
  773. {
  774. struct task_struct * task;
  775. siginfo_t info;
  776. unsigned short cwd, swd;
  777. /*
  778. * Save the info for the exception handler and clear the error.
  779. */
  780. task = current;
  781. save_init_fpu(task);
  782. task->thread.trap_no = 16;
  783. task->thread.error_code = 0;
  784. info.si_signo = SIGFPE;
  785. info.si_errno = 0;
  786. info.si_code = __SI_FAULT;
  787. info.si_addr = eip;
  788. /*
  789. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  790. * status. 0x3f is the exception bits in these regs, 0x200 is the
  791. * C1 reg you need in case of a stack fault, 0x040 is the stack
  792. * fault bit. We should only be taking one exception at a time,
  793. * so if this combination doesn't produce any single exception,
  794. * then we have a bad program that isn't syncronizing its FPU usage
  795. * and it will suffer the consequences since we won't be able to
  796. * fully reproduce the context of the exception
  797. */
  798. cwd = get_fpu_cwd(task);
  799. swd = get_fpu_swd(task);
  800. switch (swd & ~cwd & 0x3f) {
  801. case 0x000: /* No unmasked exception */
  802. return;
  803. default: /* Multiple exceptions */
  804. break;
  805. case 0x001: /* Invalid Op */
  806. /*
  807. * swd & 0x240 == 0x040: Stack Underflow
  808. * swd & 0x240 == 0x240: Stack Overflow
  809. * User must clear the SF bit (0x40) if set
  810. */
  811. info.si_code = FPE_FLTINV;
  812. break;
  813. case 0x002: /* Denormalize */
  814. case 0x010: /* Underflow */
  815. info.si_code = FPE_FLTUND;
  816. break;
  817. case 0x004: /* Zero Divide */
  818. info.si_code = FPE_FLTDIV;
  819. break;
  820. case 0x008: /* Overflow */
  821. info.si_code = FPE_FLTOVF;
  822. break;
  823. case 0x020: /* Precision */
  824. info.si_code = FPE_FLTRES;
  825. break;
  826. }
  827. force_sig_info(SIGFPE, &info, task);
  828. }
  829. fastcall void do_coprocessor_error(struct pt_regs * regs, long error_code)
  830. {
  831. ignore_fpu_irq = 1;
  832. math_error((void __user *)regs->eip);
  833. }
  834. static void simd_math_error(void __user *eip)
  835. {
  836. struct task_struct * task;
  837. siginfo_t info;
  838. unsigned short mxcsr;
  839. /*
  840. * Save the info for the exception handler and clear the error.
  841. */
  842. task = current;
  843. save_init_fpu(task);
  844. task->thread.trap_no = 19;
  845. task->thread.error_code = 0;
  846. info.si_signo = SIGFPE;
  847. info.si_errno = 0;
  848. info.si_code = __SI_FAULT;
  849. info.si_addr = eip;
  850. /*
  851. * The SIMD FPU exceptions are handled a little differently, as there
  852. * is only a single status/control register. Thus, to determine which
  853. * unmasked exception was caught we must mask the exception mask bits
  854. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  855. */
  856. mxcsr = get_fpu_mxcsr(task);
  857. switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
  858. case 0x000:
  859. default:
  860. break;
  861. case 0x001: /* Invalid Op */
  862. info.si_code = FPE_FLTINV;
  863. break;
  864. case 0x002: /* Denormalize */
  865. case 0x010: /* Underflow */
  866. info.si_code = FPE_FLTUND;
  867. break;
  868. case 0x004: /* Zero Divide */
  869. info.si_code = FPE_FLTDIV;
  870. break;
  871. case 0x008: /* Overflow */
  872. info.si_code = FPE_FLTOVF;
  873. break;
  874. case 0x020: /* Precision */
  875. info.si_code = FPE_FLTRES;
  876. break;
  877. }
  878. force_sig_info(SIGFPE, &info, task);
  879. }
  880. fastcall void do_simd_coprocessor_error(struct pt_regs * regs,
  881. long error_code)
  882. {
  883. if (cpu_has_xmm) {
  884. /* Handle SIMD FPU exceptions on PIII+ processors. */
  885. ignore_fpu_irq = 1;
  886. simd_math_error((void __user *)regs->eip);
  887. } else {
  888. /*
  889. * Handle strange cache flush from user space exception
  890. * in all other cases. This is undocumented behaviour.
  891. */
  892. if (regs->eflags & VM_MASK) {
  893. handle_vm86_fault((struct kernel_vm86_regs *)regs,
  894. error_code);
  895. return;
  896. }
  897. current->thread.trap_no = 19;
  898. current->thread.error_code = error_code;
  899. die_if_kernel("cache flush denied", regs, error_code);
  900. force_sig(SIGSEGV, current);
  901. }
  902. }
  903. fastcall void do_spurious_interrupt_bug(struct pt_regs * regs,
  904. long error_code)
  905. {
  906. #if 0
  907. /* No need to warn about this any longer. */
  908. printk("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
  909. #endif
  910. }
  911. fastcall unsigned long patch_espfix_desc(unsigned long uesp,
  912. unsigned long kesp)
  913. {
  914. struct desc_struct *gdt = __get_cpu_var(gdt_page).gdt;
  915. unsigned long base = (kesp - uesp) & -THREAD_SIZE;
  916. unsigned long new_kesp = kesp - base;
  917. unsigned long lim_pages = (new_kesp | (THREAD_SIZE - 1)) >> PAGE_SHIFT;
  918. __u64 desc = *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS];
  919. /* Set up base for espfix segment */
  920. desc &= 0x00f0ff0000000000ULL;
  921. desc |= ((((__u64)base) << 16) & 0x000000ffffff0000ULL) |
  922. ((((__u64)base) << 32) & 0xff00000000000000ULL) |
  923. ((((__u64)lim_pages) << 32) & 0x000f000000000000ULL) |
  924. (lim_pages & 0xffff);
  925. *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS] = desc;
  926. return new_kesp;
  927. }
  928. /*
  929. * 'math_state_restore()' saves the current math information in the
  930. * old math state array, and gets the new ones from the current task
  931. *
  932. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  933. * Don't touch unless you *really* know how it works.
  934. *
  935. * Must be called with kernel preemption disabled (in this case,
  936. * local interrupts are disabled at the call-site in entry.S).
  937. */
  938. asmlinkage void math_state_restore(void)
  939. {
  940. struct thread_info *thread = current_thread_info();
  941. struct task_struct *tsk = thread->task;
  942. clts(); /* Allow maths ops (or we recurse) */
  943. if (!tsk_used_math(tsk))
  944. init_fpu(tsk);
  945. restore_fpu(tsk);
  946. thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
  947. tsk->fpu_counter++;
  948. }
  949. EXPORT_SYMBOL_GPL(math_state_restore);
  950. #ifndef CONFIG_MATH_EMULATION
  951. asmlinkage void math_emulate(long arg)
  952. {
  953. printk(KERN_EMERG "math-emulation not enabled and no coprocessor found.\n");
  954. printk(KERN_EMERG "killing %s.\n",current->comm);
  955. force_sig(SIGFPE,current);
  956. schedule();
  957. }
  958. #endif /* CONFIG_MATH_EMULATION */
  959. #ifdef CONFIG_X86_F00F_BUG
  960. void __init trap_init_f00f_bug(void)
  961. {
  962. __set_fixmap(FIX_F00F_IDT, __pa(&idt_table), PAGE_KERNEL_RO);
  963. /*
  964. * Update the IDT descriptor and reload the IDT so that
  965. * it uses the read-only mapped virtual address.
  966. */
  967. idt_descr.address = fix_to_virt(FIX_F00F_IDT);
  968. load_idt(&idt_descr);
  969. }
  970. #endif
  971. /*
  972. * This needs to use 'idt_table' rather than 'idt', and
  973. * thus use the _nonmapped_ version of the IDT, as the
  974. * Pentium F0 0F bugfix can have resulted in the mapped
  975. * IDT being write-protected.
  976. */
  977. void set_intr_gate(unsigned int n, void *addr)
  978. {
  979. _set_gate(n, DESCTYPE_INT, addr, __KERNEL_CS);
  980. }
  981. /*
  982. * This routine sets up an interrupt gate at directory privilege level 3.
  983. */
  984. static inline void set_system_intr_gate(unsigned int n, void *addr)
  985. {
  986. _set_gate(n, DESCTYPE_INT | DESCTYPE_DPL3, addr, __KERNEL_CS);
  987. }
  988. static void __init set_trap_gate(unsigned int n, void *addr)
  989. {
  990. _set_gate(n, DESCTYPE_TRAP, addr, __KERNEL_CS);
  991. }
  992. static void __init set_system_gate(unsigned int n, void *addr)
  993. {
  994. _set_gate(n, DESCTYPE_TRAP | DESCTYPE_DPL3, addr, __KERNEL_CS);
  995. }
  996. static void __init set_task_gate(unsigned int n, unsigned int gdt_entry)
  997. {
  998. _set_gate(n, DESCTYPE_TASK, (void *)0, (gdt_entry<<3));
  999. }
  1000. void __init trap_init(void)
  1001. {
  1002. #ifdef CONFIG_EISA
  1003. void __iomem *p = ioremap(0x0FFFD9, 4);
  1004. if (readl(p) == 'E'+('I'<<8)+('S'<<16)+('A'<<24)) {
  1005. EISA_bus = 1;
  1006. }
  1007. iounmap(p);
  1008. #endif
  1009. #ifdef CONFIG_X86_LOCAL_APIC
  1010. init_apic_mappings();
  1011. #endif
  1012. set_trap_gate(0,&divide_error);
  1013. set_intr_gate(1,&debug);
  1014. set_intr_gate(2,&nmi);
  1015. set_system_intr_gate(3, &int3); /* int3/4 can be called from all */
  1016. set_system_gate(4,&overflow);
  1017. set_trap_gate(5,&bounds);
  1018. set_trap_gate(6,&invalid_op);
  1019. set_trap_gate(7,&device_not_available);
  1020. set_task_gate(8,GDT_ENTRY_DOUBLEFAULT_TSS);
  1021. set_trap_gate(9,&coprocessor_segment_overrun);
  1022. set_trap_gate(10,&invalid_TSS);
  1023. set_trap_gate(11,&segment_not_present);
  1024. set_trap_gate(12,&stack_segment);
  1025. set_trap_gate(13,&general_protection);
  1026. set_intr_gate(14,&page_fault);
  1027. set_trap_gate(15,&spurious_interrupt_bug);
  1028. set_trap_gate(16,&coprocessor_error);
  1029. set_trap_gate(17,&alignment_check);
  1030. #ifdef CONFIG_X86_MCE
  1031. set_trap_gate(18,&machine_check);
  1032. #endif
  1033. set_trap_gate(19,&simd_coprocessor_error);
  1034. if (cpu_has_fxsr) {
  1035. /*
  1036. * Verify that the FXSAVE/FXRSTOR data will be 16-byte aligned.
  1037. * Generates a compile-time "error: zero width for bit-field" if
  1038. * the alignment is wrong.
  1039. */
  1040. struct fxsrAlignAssert {
  1041. int _:!(offsetof(struct task_struct,
  1042. thread.i387.fxsave) & 15);
  1043. };
  1044. printk(KERN_INFO "Enabling fast FPU save and restore... ");
  1045. set_in_cr4(X86_CR4_OSFXSR);
  1046. printk("done.\n");
  1047. }
  1048. if (cpu_has_xmm) {
  1049. printk(KERN_INFO "Enabling unmasked SIMD FPU exception "
  1050. "support... ");
  1051. set_in_cr4(X86_CR4_OSXMMEXCPT);
  1052. printk("done.\n");
  1053. }
  1054. set_system_gate(SYSCALL_VECTOR,&system_call);
  1055. /*
  1056. * Should be a barrier for any external CPU state.
  1057. */
  1058. cpu_init();
  1059. trap_init_hook();
  1060. }
  1061. static int __init kstack_setup(char *s)
  1062. {
  1063. kstack_depth_to_print = simple_strtoul(s, NULL, 0);
  1064. return 1;
  1065. }
  1066. __setup("kstack=", kstack_setup);
  1067. static int __init code_bytes_setup(char *s)
  1068. {
  1069. code_bytes = simple_strtoul(s, NULL, 0);
  1070. if (code_bytes > 8192)
  1071. code_bytes = 8192;
  1072. return 1;
  1073. }
  1074. __setup("code_bytes=", code_bytes_setup);