traps_32.c 30 KB

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