traps.c 31 KB

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