traps_32.c 31 KB

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