traps_32.c 30 KB

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