traps_32.c 31 KB

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