traps_32.c 32 KB

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