traps_32.c 31 KB

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