traps_32.c 31 KB

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