traps_32.c 31 KB

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