traps_32.c 30 KB

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