traps_32.c 31 KB

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