traps.c 31 KB

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