fault.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. /*
  2. * Copyright (C) 1995 Linus Torvalds
  3. * Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs.
  4. */
  5. #include <linux/interrupt.h>
  6. #include <linux/mmiotrace.h>
  7. #include <linux/bootmem.h>
  8. #include <linux/compiler.h>
  9. #include <linux/highmem.h>
  10. #include <linux/kprobes.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/vt_kern.h>
  14. #include <linux/signal.h>
  15. #include <linux/kernel.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/string.h>
  18. #include <linux/module.h>
  19. #include <linux/kdebug.h>
  20. #include <linux/errno.h>
  21. #include <linux/magic.h>
  22. #include <linux/sched.h>
  23. #include <linux/types.h>
  24. #include <linux/init.h>
  25. #include <linux/mman.h>
  26. #include <linux/tty.h>
  27. #include <linux/smp.h>
  28. #include <linux/mm.h>
  29. #include <asm-generic/sections.h>
  30. #include <asm/tlbflush.h>
  31. #include <asm/pgalloc.h>
  32. #include <asm/segment.h>
  33. #include <asm/system.h>
  34. #include <asm/proto.h>
  35. #include <asm/traps.h>
  36. #include <asm/desc.h>
  37. /*
  38. * Page fault error code bits:
  39. *
  40. * bit 0 == 0: no page found 1: protection fault
  41. * bit 1 == 0: read access 1: write access
  42. * bit 2 == 0: kernel-mode access 1: user-mode access
  43. * bit 3 == 1: use of reserved bit detected
  44. * bit 4 == 1: fault was an instruction fetch
  45. */
  46. enum x86_pf_error_code {
  47. PF_PROT = 1 << 0,
  48. PF_WRITE = 1 << 1,
  49. PF_USER = 1 << 2,
  50. PF_RSVD = 1 << 3,
  51. PF_INSTR = 1 << 4,
  52. };
  53. /*
  54. * (returns 0 if mmiotrace is disabled)
  55. */
  56. static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
  57. {
  58. if (unlikely(is_kmmio_active()))
  59. if (kmmio_handler(regs, addr) == 1)
  60. return -1;
  61. return 0;
  62. }
  63. static inline int notify_page_fault(struct pt_regs *regs)
  64. {
  65. int ret = 0;
  66. /* kprobe_running() needs smp_processor_id() */
  67. if (kprobes_built_in() && !user_mode_vm(regs)) {
  68. preempt_disable();
  69. if (kprobe_running() && kprobe_fault_handler(regs, 14))
  70. ret = 1;
  71. preempt_enable();
  72. }
  73. return ret;
  74. }
  75. /*
  76. * Prefetch quirks:
  77. *
  78. * 32-bit mode:
  79. *
  80. * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
  81. * Check that here and ignore it.
  82. *
  83. * 64-bit mode:
  84. *
  85. * Sometimes the CPU reports invalid exceptions on prefetch.
  86. * Check that here and ignore it.
  87. *
  88. * Opcode checker based on code by Richard Brunner.
  89. */
  90. static inline int
  91. check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
  92. unsigned char opcode, int *prefetch)
  93. {
  94. unsigned char instr_hi = opcode & 0xf0;
  95. unsigned char instr_lo = opcode & 0x0f;
  96. switch (instr_hi) {
  97. case 0x20:
  98. case 0x30:
  99. /*
  100. * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
  101. * In X86_64 long mode, the CPU will signal invalid
  102. * opcode if some of these prefixes are present so
  103. * X86_64 will never get here anyway
  104. */
  105. return ((instr_lo & 7) == 0x6);
  106. #ifdef CONFIG_X86_64
  107. case 0x40:
  108. /*
  109. * In AMD64 long mode 0x40..0x4F are valid REX prefixes
  110. * Need to figure out under what instruction mode the
  111. * instruction was issued. Could check the LDT for lm,
  112. * but for now it's good enough to assume that long
  113. * mode only uses well known segments or kernel.
  114. */
  115. return (!user_mode(regs)) || (regs->cs == __USER_CS);
  116. #endif
  117. case 0x60:
  118. /* 0x64 thru 0x67 are valid prefixes in all modes. */
  119. return (instr_lo & 0xC) == 0x4;
  120. case 0xF0:
  121. /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
  122. return !instr_lo || (instr_lo>>1) == 1;
  123. case 0x00:
  124. /* Prefetch instruction is 0x0F0D or 0x0F18 */
  125. if (probe_kernel_address(instr, opcode))
  126. return 0;
  127. *prefetch = (instr_lo == 0xF) &&
  128. (opcode == 0x0D || opcode == 0x18);
  129. return 0;
  130. default:
  131. return 0;
  132. }
  133. }
  134. static int
  135. is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
  136. {
  137. unsigned char *max_instr;
  138. unsigned char *instr;
  139. int prefetch = 0;
  140. /*
  141. * If it was a exec (instruction fetch) fault on NX page, then
  142. * do not ignore the fault:
  143. */
  144. if (error_code & PF_INSTR)
  145. return 0;
  146. instr = (void *)convert_ip_to_linear(current, regs);
  147. max_instr = instr + 15;
  148. if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
  149. return 0;
  150. while (instr < max_instr) {
  151. unsigned char opcode;
  152. if (probe_kernel_address(instr, opcode))
  153. break;
  154. instr++;
  155. if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
  156. break;
  157. }
  158. return prefetch;
  159. }
  160. static void
  161. force_sig_info_fault(int si_signo, int si_code, unsigned long address,
  162. struct task_struct *tsk)
  163. {
  164. siginfo_t info;
  165. info.si_signo = si_signo;
  166. info.si_errno = 0;
  167. info.si_code = si_code;
  168. info.si_addr = (void __user *)address;
  169. force_sig_info(si_signo, &info, tsk);
  170. }
  171. #ifdef CONFIG_X86_64
  172. static int bad_address(void *p)
  173. {
  174. unsigned long dummy;
  175. return probe_kernel_address((unsigned long *)p, dummy);
  176. }
  177. #endif
  178. static void dump_pagetable(unsigned long address)
  179. {
  180. #ifdef CONFIG_X86_32
  181. __typeof__(pte_val(__pte(0))) page;
  182. page = read_cr3();
  183. page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
  184. #ifdef CONFIG_X86_PAE
  185. printk("*pdpt = %016Lx ", page);
  186. if ((page >> PAGE_SHIFT) < max_low_pfn
  187. && page & _PAGE_PRESENT) {
  188. page &= PAGE_MASK;
  189. page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
  190. & (PTRS_PER_PMD - 1)];
  191. printk(KERN_CONT "*pde = %016Lx ", page);
  192. page &= ~_PAGE_NX;
  193. }
  194. #else
  195. printk("*pde = %08lx ", page);
  196. #endif
  197. /*
  198. * We must not directly access the pte in the highpte
  199. * case if the page table is located in highmem.
  200. * And let's rather not kmap-atomic the pte, just in case
  201. * it's allocated already:
  202. */
  203. if ((page >> PAGE_SHIFT) < max_low_pfn
  204. && (page & _PAGE_PRESENT)
  205. && !(page & _PAGE_PSE)) {
  206. page &= PAGE_MASK;
  207. page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
  208. & (PTRS_PER_PTE - 1)];
  209. printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
  210. }
  211. printk("\n");
  212. #else /* CONFIG_X86_64 */
  213. pgd_t *pgd;
  214. pud_t *pud;
  215. pmd_t *pmd;
  216. pte_t *pte;
  217. pgd = (pgd_t *)read_cr3();
  218. pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
  219. pgd += pgd_index(address);
  220. if (bad_address(pgd))
  221. goto bad;
  222. printk("PGD %lx ", pgd_val(*pgd));
  223. if (!pgd_present(*pgd))
  224. goto out;
  225. pud = pud_offset(pgd, address);
  226. if (bad_address(pud))
  227. goto bad;
  228. printk("PUD %lx ", pud_val(*pud));
  229. if (!pud_present(*pud) || pud_large(*pud))
  230. goto out;
  231. pmd = pmd_offset(pud, address);
  232. if (bad_address(pmd))
  233. goto bad;
  234. printk("PMD %lx ", pmd_val(*pmd));
  235. if (!pmd_present(*pmd) || pmd_large(*pmd))
  236. goto out;
  237. pte = pte_offset_kernel(pmd, address);
  238. if (bad_address(pte))
  239. goto bad;
  240. printk("PTE %lx", pte_val(*pte));
  241. out:
  242. printk("\n");
  243. return;
  244. bad:
  245. printk("BAD\n");
  246. #endif
  247. }
  248. #ifdef CONFIG_X86_32
  249. static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
  250. {
  251. unsigned index = pgd_index(address);
  252. pgd_t *pgd_k;
  253. pud_t *pud, *pud_k;
  254. pmd_t *pmd, *pmd_k;
  255. pgd += index;
  256. pgd_k = init_mm.pgd + index;
  257. if (!pgd_present(*pgd_k))
  258. return NULL;
  259. /*
  260. * set_pgd(pgd, *pgd_k); here would be useless on PAE
  261. * and redundant with the set_pmd() on non-PAE. As would
  262. * set_pud.
  263. */
  264. pud = pud_offset(pgd, address);
  265. pud_k = pud_offset(pgd_k, address);
  266. if (!pud_present(*pud_k))
  267. return NULL;
  268. pmd = pmd_offset(pud, address);
  269. pmd_k = pmd_offset(pud_k, address);
  270. if (!pmd_present(*pmd_k))
  271. return NULL;
  272. if (!pmd_present(*pmd)) {
  273. set_pmd(pmd, *pmd_k);
  274. arch_flush_lazy_mmu_mode();
  275. } else {
  276. BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
  277. }
  278. return pmd_k;
  279. }
  280. /*
  281. * Did it hit the DOS screen memory VA from vm86 mode?
  282. */
  283. static inline void
  284. check_v8086_mode(struct pt_regs *regs, unsigned long address,
  285. struct task_struct *tsk)
  286. {
  287. unsigned long bit;
  288. if (!v8086_mode(regs))
  289. return;
  290. bit = (address - 0xA0000) >> PAGE_SHIFT;
  291. if (bit < 32)
  292. tsk->thread.screen_bitmap |= 1 << bit;
  293. }
  294. #else /* CONFIG_X86_64: */
  295. static const char errata93_warning[] =
  296. KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
  297. KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
  298. KERN_ERR "******* Please consider a BIOS update.\n"
  299. KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
  300. /*
  301. * No vm86 mode in 64-bit mode:
  302. */
  303. static inline void
  304. check_v8086_mode(struct pt_regs *regs, unsigned long address,
  305. struct task_struct *tsk)
  306. {
  307. }
  308. #endif
  309. /*
  310. * Workaround for K8 erratum #93 & buggy BIOS.
  311. *
  312. * BIOS SMM functions are required to use a specific workaround
  313. * to avoid corruption of the 64bit RIP register on C stepping K8.
  314. *
  315. * A lot of BIOS that didn't get tested properly miss this.
  316. *
  317. * The OS sees this as a page fault with the upper 32bits of RIP cleared.
  318. * Try to work around it here.
  319. *
  320. * Note we only handle faults in kernel here.
  321. * Does nothing on 32-bit.
  322. */
  323. static int is_errata93(struct pt_regs *regs, unsigned long address)
  324. {
  325. #ifdef CONFIG_X86_64
  326. static int once;
  327. if (address != regs->ip)
  328. return 0;
  329. if ((address >> 32) != 0)
  330. return 0;
  331. address |= 0xffffffffUL << 32;
  332. if ((address >= (u64)_stext && address <= (u64)_etext) ||
  333. (address >= MODULES_VADDR && address <= MODULES_END)) {
  334. if (!once) {
  335. printk(errata93_warning);
  336. once = 1;
  337. }
  338. regs->ip = address;
  339. return 1;
  340. }
  341. #endif
  342. return 0;
  343. }
  344. /*
  345. * Work around K8 erratum #100 K8 in compat mode occasionally jumps
  346. * to illegal addresses >4GB.
  347. *
  348. * We catch this in the page fault handler because these addresses
  349. * are not reachable. Just detect this case and return. Any code
  350. * segment in LDT is compatibility mode.
  351. */
  352. static int is_errata100(struct pt_regs *regs, unsigned long address)
  353. {
  354. #ifdef CONFIG_X86_64
  355. if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
  356. return 1;
  357. #endif
  358. return 0;
  359. }
  360. static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
  361. {
  362. #ifdef CONFIG_X86_F00F_BUG
  363. unsigned long nr;
  364. /*
  365. * Pentium F0 0F C7 C8 bug workaround:
  366. */
  367. if (boot_cpu_data.f00f_bug) {
  368. nr = (address - idt_descr.address) >> 3;
  369. if (nr == 6) {
  370. do_invalid_op(regs, 0);
  371. return 1;
  372. }
  373. }
  374. #endif
  375. return 0;
  376. }
  377. static void
  378. show_fault_oops(struct pt_regs *regs, unsigned long error_code,
  379. unsigned long address)
  380. {
  381. #ifdef CONFIG_X86_32
  382. if (!oops_may_print())
  383. return;
  384. #endif
  385. #ifdef CONFIG_X86_PAE
  386. if (error_code & PF_INSTR) {
  387. unsigned int level;
  388. pte_t *pte = lookup_address(address, &level);
  389. if (pte && pte_present(*pte) && !pte_exec(*pte)) {
  390. printk(KERN_CRIT "kernel tried to execute "
  391. "NX-protected page - exploit attempt? "
  392. "(uid: %d)\n", current_uid());
  393. }
  394. }
  395. #endif
  396. printk(KERN_ALERT "BUG: unable to handle kernel ");
  397. if (address < PAGE_SIZE)
  398. printk(KERN_CONT "NULL pointer dereference");
  399. else
  400. printk(KERN_CONT "paging request");
  401. printk(KERN_CONT " at %p\n", (void *) address);
  402. printk(KERN_ALERT "IP:");
  403. printk_address(regs->ip, 1);
  404. dump_pagetable(address);
  405. }
  406. static noinline void
  407. pgtable_bad(struct pt_regs *regs, unsigned long error_code,
  408. unsigned long address)
  409. {
  410. struct task_struct *tsk;
  411. unsigned long flags;
  412. int sig;
  413. flags = oops_begin();
  414. tsk = current;
  415. sig = SIGKILL;
  416. printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
  417. tsk->comm, address);
  418. dump_pagetable(address);
  419. tsk->thread.cr2 = address;
  420. tsk->thread.trap_no = 14;
  421. tsk->thread.error_code = error_code;
  422. if (__die("Bad pagetable", regs, error_code))
  423. sig = 0;
  424. oops_end(flags, regs, sig);
  425. }
  426. static noinline void
  427. no_context(struct pt_regs *regs, unsigned long error_code,
  428. unsigned long address)
  429. {
  430. struct task_struct *tsk = current;
  431. unsigned long *stackend;
  432. #ifdef CONFIG_X86_64
  433. unsigned long flags;
  434. int sig;
  435. #endif
  436. /* Are we prepared to handle this kernel fault? */
  437. if (fixup_exception(regs))
  438. return;
  439. /*
  440. * 32-bit:
  441. *
  442. * Valid to do another page fault here, because if this fault
  443. * had been triggered by is_prefetch fixup_exception would have
  444. * handled it.
  445. *
  446. * 64-bit:
  447. *
  448. * Hall of shame of CPU/BIOS bugs.
  449. */
  450. if (is_prefetch(regs, error_code, address))
  451. return;
  452. if (is_errata93(regs, address))
  453. return;
  454. /*
  455. * Oops. The kernel tried to access some bad page. We'll have to
  456. * terminate things with extreme prejudice:
  457. */
  458. #ifdef CONFIG_X86_32
  459. bust_spinlocks(1);
  460. #else
  461. flags = oops_begin();
  462. #endif
  463. show_fault_oops(regs, error_code, address);
  464. stackend = end_of_stack(tsk);
  465. if (*stackend != STACK_END_MAGIC)
  466. printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
  467. tsk->thread.cr2 = address;
  468. tsk->thread.trap_no = 14;
  469. tsk->thread.error_code = error_code;
  470. #ifdef CONFIG_X86_32
  471. die("Oops", regs, error_code);
  472. bust_spinlocks(0);
  473. do_exit(SIGKILL);
  474. #else
  475. sig = SIGKILL;
  476. if (__die("Oops", regs, error_code))
  477. sig = 0;
  478. /* Executive summary in case the body of the oops scrolled away */
  479. printk(KERN_EMERG "CR2: %016lx\n", address);
  480. oops_end(flags, regs, sig);
  481. #endif
  482. }
  483. /*
  484. * Print out info about fatal segfaults, if the show_unhandled_signals
  485. * sysctl is set:
  486. */
  487. static inline void
  488. show_signal_msg(struct pt_regs *regs, unsigned long error_code,
  489. unsigned long address, struct task_struct *tsk)
  490. {
  491. if (!unhandled_signal(tsk, SIGSEGV))
  492. return;
  493. if (!printk_ratelimit())
  494. return;
  495. printk(KERN_CONT "%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
  496. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  497. tsk->comm, task_pid_nr(tsk), address,
  498. (void *)regs->ip, (void *)regs->sp, error_code);
  499. print_vma_addr(KERN_CONT " in ", regs->ip);
  500. printk(KERN_CONT "\n");
  501. }
  502. static void
  503. __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  504. unsigned long address, int si_code)
  505. {
  506. struct task_struct *tsk = current;
  507. /* User mode accesses just cause a SIGSEGV */
  508. if (error_code & PF_USER) {
  509. /*
  510. * It's possible to have interrupts off here:
  511. */
  512. local_irq_enable();
  513. /*
  514. * Valid to do another page fault here because this one came
  515. * from user space:
  516. */
  517. if (is_prefetch(regs, error_code, address))
  518. return;
  519. if (is_errata100(regs, address))
  520. return;
  521. if (unlikely(show_unhandled_signals))
  522. show_signal_msg(regs, error_code, address, tsk);
  523. /* Kernel addresses are always protection faults: */
  524. tsk->thread.cr2 = address;
  525. tsk->thread.error_code = error_code | (address >= TASK_SIZE);
  526. tsk->thread.trap_no = 14;
  527. force_sig_info_fault(SIGSEGV, si_code, address, tsk);
  528. return;
  529. }
  530. if (is_f00f_bug(regs, address))
  531. return;
  532. no_context(regs, error_code, address);
  533. }
  534. static noinline void
  535. bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  536. unsigned long address)
  537. {
  538. __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
  539. }
  540. static void
  541. __bad_area(struct pt_regs *regs, unsigned long error_code,
  542. unsigned long address, int si_code)
  543. {
  544. struct mm_struct *mm = current->mm;
  545. /*
  546. * Something tried to access memory that isn't in our memory map..
  547. * Fix it, but check if it's kernel or user first..
  548. */
  549. up_read(&mm->mmap_sem);
  550. __bad_area_nosemaphore(regs, error_code, address, si_code);
  551. }
  552. static noinline void
  553. bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
  554. {
  555. __bad_area(regs, error_code, address, SEGV_MAPERR);
  556. }
  557. static noinline void
  558. bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
  559. unsigned long address)
  560. {
  561. __bad_area(regs, error_code, address, SEGV_ACCERR);
  562. }
  563. /* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
  564. static void
  565. out_of_memory(struct pt_regs *regs, unsigned long error_code,
  566. unsigned long address)
  567. {
  568. /*
  569. * We ran out of memory, call the OOM killer, and return the userspace
  570. * (which will retry the fault, or kill us if we got oom-killed):
  571. */
  572. up_read(&current->mm->mmap_sem);
  573. pagefault_out_of_memory();
  574. }
  575. static void
  576. do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
  577. {
  578. struct task_struct *tsk = current;
  579. struct mm_struct *mm = tsk->mm;
  580. up_read(&mm->mmap_sem);
  581. /* Kernel mode? Handle exceptions or die: */
  582. if (!(error_code & PF_USER))
  583. no_context(regs, error_code, address);
  584. #ifdef CONFIG_X86_32
  585. /* User space => ok to do another page fault: */
  586. if (is_prefetch(regs, error_code, address))
  587. return;
  588. #endif
  589. tsk->thread.cr2 = address;
  590. tsk->thread.error_code = error_code;
  591. tsk->thread.trap_no = 14;
  592. force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
  593. }
  594. static noinline void
  595. mm_fault_error(struct pt_regs *regs, unsigned long error_code,
  596. unsigned long address, unsigned int fault)
  597. {
  598. if (fault & VM_FAULT_OOM) {
  599. out_of_memory(regs, error_code, address);
  600. } else {
  601. if (fault & VM_FAULT_SIGBUS)
  602. do_sigbus(regs, error_code, address);
  603. else
  604. BUG();
  605. }
  606. }
  607. static int spurious_fault_check(unsigned long error_code, pte_t *pte)
  608. {
  609. if ((error_code & PF_WRITE) && !pte_write(*pte))
  610. return 0;
  611. if ((error_code & PF_INSTR) && !pte_exec(*pte))
  612. return 0;
  613. return 1;
  614. }
  615. /*
  616. * Handle a spurious fault caused by a stale TLB entry.
  617. *
  618. * This allows us to lazily refresh the TLB when increasing the
  619. * permissions of a kernel page (RO -> RW or NX -> X). Doing it
  620. * eagerly is very expensive since that implies doing a full
  621. * cross-processor TLB flush, even if no stale TLB entries exist
  622. * on other processors.
  623. *
  624. * There are no security implications to leaving a stale TLB when
  625. * increasing the permissions on a page.
  626. */
  627. static noinline int
  628. spurious_fault(unsigned long error_code, unsigned long address)
  629. {
  630. pgd_t *pgd;
  631. pud_t *pud;
  632. pmd_t *pmd;
  633. pte_t *pte;
  634. int ret;
  635. /* Reserved-bit violation or user access to kernel space? */
  636. if (error_code & (PF_USER | PF_RSVD))
  637. return 0;
  638. pgd = init_mm.pgd + pgd_index(address);
  639. if (!pgd_present(*pgd))
  640. return 0;
  641. pud = pud_offset(pgd, address);
  642. if (!pud_present(*pud))
  643. return 0;
  644. if (pud_large(*pud))
  645. return spurious_fault_check(error_code, (pte_t *) pud);
  646. pmd = pmd_offset(pud, address);
  647. if (!pmd_present(*pmd))
  648. return 0;
  649. if (pmd_large(*pmd))
  650. return spurious_fault_check(error_code, (pte_t *) pmd);
  651. pte = pte_offset_kernel(pmd, address);
  652. if (!pte_present(*pte))
  653. return 0;
  654. ret = spurious_fault_check(error_code, pte);
  655. if (!ret)
  656. return 0;
  657. /*
  658. * Make sure we have permissions in PMD.
  659. * If not, then there's a bug in the page tables:
  660. */
  661. ret = spurious_fault_check(error_code, (pte_t *) pmd);
  662. WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
  663. return ret;
  664. }
  665. /*
  666. * 32-bit:
  667. *
  668. * Handle a fault on the vmalloc or module mapping area
  669. *
  670. * 64-bit:
  671. *
  672. * Handle a fault on the vmalloc area
  673. *
  674. * This assumes no large pages in there.
  675. */
  676. static noinline int vmalloc_fault(unsigned long address)
  677. {
  678. #ifdef CONFIG_X86_32
  679. unsigned long pgd_paddr;
  680. pmd_t *pmd_k;
  681. pte_t *pte_k;
  682. /* Make sure we are in vmalloc area: */
  683. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  684. return -1;
  685. /*
  686. * Synchronize this task's top level page-table
  687. * with the 'reference' page table.
  688. *
  689. * Do _not_ use "current" here. We might be inside
  690. * an interrupt in the middle of a task switch..
  691. */
  692. pgd_paddr = read_cr3();
  693. pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
  694. if (!pmd_k)
  695. return -1;
  696. pte_k = pte_offset_kernel(pmd_k, address);
  697. if (!pte_present(*pte_k))
  698. return -1;
  699. return 0;
  700. #else
  701. pgd_t *pgd, *pgd_ref;
  702. pud_t *pud, *pud_ref;
  703. pmd_t *pmd, *pmd_ref;
  704. pte_t *pte, *pte_ref;
  705. /* Make sure we are in vmalloc area: */
  706. if (!(address >= VMALLOC_START && address < VMALLOC_END))
  707. return -1;
  708. /*
  709. * Copy kernel mappings over when needed. This can also
  710. * happen within a race in page table update. In the later
  711. * case just flush:
  712. */
  713. pgd = pgd_offset(current->active_mm, address);
  714. pgd_ref = pgd_offset_k(address);
  715. if (pgd_none(*pgd_ref))
  716. return -1;
  717. if (pgd_none(*pgd))
  718. set_pgd(pgd, *pgd_ref);
  719. else
  720. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  721. /*
  722. * Below here mismatches are bugs because these lower tables
  723. * are shared:
  724. */
  725. pud = pud_offset(pgd, address);
  726. pud_ref = pud_offset(pgd_ref, address);
  727. if (pud_none(*pud_ref))
  728. return -1;
  729. if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
  730. BUG();
  731. pmd = pmd_offset(pud, address);
  732. pmd_ref = pmd_offset(pud_ref, address);
  733. if (pmd_none(*pmd_ref))
  734. return -1;
  735. if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
  736. BUG();
  737. pte_ref = pte_offset_kernel(pmd_ref, address);
  738. if (!pte_present(*pte_ref))
  739. return -1;
  740. pte = pte_offset_kernel(pmd, address);
  741. /*
  742. * Don't use pte_page here, because the mappings can point
  743. * outside mem_map, and the NUMA hash lookup cannot handle
  744. * that:
  745. */
  746. if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
  747. BUG();
  748. return 0;
  749. #endif
  750. }
  751. int show_unhandled_signals = 1;
  752. static inline int
  753. access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
  754. {
  755. if (write) {
  756. /* write, present and write, not present: */
  757. if (unlikely(!(vma->vm_flags & VM_WRITE)))
  758. return 1;
  759. return 0;
  760. }
  761. /* read, present: */
  762. if (unlikely(error_code & PF_PROT))
  763. return 1;
  764. /* read, not present: */
  765. if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
  766. return 1;
  767. return 0;
  768. }
  769. static int fault_in_kernel_space(unsigned long address)
  770. {
  771. #ifdef CONFIG_X86_32
  772. return address >= TASK_SIZE;
  773. #else
  774. return address >= TASK_SIZE64;
  775. #endif
  776. }
  777. /*
  778. * This routine handles page faults. It determines the address,
  779. * and the problem, and then passes it off to one of the appropriate
  780. * routines.
  781. */
  782. #ifdef CONFIG_X86_64
  783. asmlinkage
  784. #endif
  785. void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
  786. {
  787. struct vm_area_struct *vma;
  788. struct task_struct *tsk;
  789. unsigned long address;
  790. struct mm_struct *mm;
  791. int write;
  792. int fault;
  793. tsk = current;
  794. mm = tsk->mm;
  795. prefetchw(&mm->mmap_sem);
  796. /* Get the faulting address: */
  797. address = read_cr2();
  798. if (unlikely(kmmio_fault(regs, address)))
  799. return;
  800. /*
  801. * We fault-in kernel-space virtual memory on-demand. The
  802. * 'reference' page table is init_mm.pgd.
  803. *
  804. * NOTE! We MUST NOT take any locks for this case. We may
  805. * be in an interrupt or a critical region, and should
  806. * only copy the information from the master page table,
  807. * nothing more.
  808. *
  809. * This verifies that the fault happens in kernel space
  810. * (error_code & 4) == 0, and that the fault was not a
  811. * protection error (error_code & 9) == 0.
  812. */
  813. if (unlikely(fault_in_kernel_space(address))) {
  814. if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
  815. vmalloc_fault(address) >= 0)
  816. return;
  817. /* Can handle a stale RO->RW TLB: */
  818. if (spurious_fault(error_code, address))
  819. return;
  820. /* kprobes don't want to hook the spurious faults: */
  821. if (notify_page_fault(regs))
  822. return;
  823. /*
  824. * Don't take the mm semaphore here. If we fixup a prefetch
  825. * fault we could otherwise deadlock:
  826. */
  827. bad_area_nosemaphore(regs, error_code, address);
  828. return;
  829. }
  830. /* kprobes don't want to hook the spurious faults: */
  831. if (unlikely(notify_page_fault(regs)))
  832. return;
  833. /*
  834. * It's safe to allow irq's after cr2 has been saved and the
  835. * vmalloc fault has been handled.
  836. *
  837. * User-mode registers count as a user access even for any
  838. * potential system fault or CPU buglet:
  839. */
  840. if (user_mode_vm(regs)) {
  841. local_irq_enable();
  842. error_code |= PF_USER;
  843. } else {
  844. if (regs->flags & X86_EFLAGS_IF)
  845. local_irq_enable();
  846. }
  847. if (unlikely(error_code & PF_RSVD))
  848. pgtable_bad(regs, error_code, address);
  849. /*
  850. * If we're in an interrupt, have no user context or are running
  851. * in an atomic region then we must not take the fault:
  852. */
  853. if (unlikely(in_atomic() || !mm)) {
  854. bad_area_nosemaphore(regs, error_code, address);
  855. return;
  856. }
  857. /*
  858. * When running in the kernel we expect faults to occur only to
  859. * addresses in user space. All other faults represent errors in
  860. * the kernel and should generate an OOPS. Unfortunately, in the
  861. * case of an erroneous fault occurring in a code path which already
  862. * holds mmap_sem we will deadlock attempting to validate the fault
  863. * against the address space. Luckily the kernel only validly
  864. * references user space from well defined areas of code, which are
  865. * listed in the exceptions table.
  866. *
  867. * As the vast majority of faults will be valid we will only perform
  868. * the source reference check when there is a possibility of a
  869. * deadlock. Attempt to lock the address space, if we cannot we then
  870. * validate the source. If this is invalid we can skip the address
  871. * space check, thus avoiding the deadlock:
  872. */
  873. if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
  874. if ((error_code & PF_USER) == 0 &&
  875. !search_exception_tables(regs->ip)) {
  876. bad_area_nosemaphore(regs, error_code, address);
  877. return;
  878. }
  879. down_read(&mm->mmap_sem);
  880. } else {
  881. /*
  882. * The above down_read_trylock() might have succeeded in
  883. * which case we'll have missed the might_sleep() from
  884. * down_read():
  885. */
  886. might_sleep();
  887. }
  888. vma = find_vma(mm, address);
  889. if (unlikely(!vma)) {
  890. bad_area(regs, error_code, address);
  891. return;
  892. }
  893. if (likely(vma->vm_start <= address))
  894. goto good_area;
  895. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
  896. bad_area(regs, error_code, address);
  897. return;
  898. }
  899. if (error_code & PF_USER) {
  900. /*
  901. * Accessing the stack below %sp is always a bug.
  902. * The large cushion allows instructions like enter
  903. * and pusha to work. ("enter $65535, $31" pushes
  904. * 32 pointers and then decrements %sp by 65535.)
  905. */
  906. if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
  907. bad_area(regs, error_code, address);
  908. return;
  909. }
  910. }
  911. if (unlikely(expand_stack(vma, address))) {
  912. bad_area(regs, error_code, address);
  913. return;
  914. }
  915. /*
  916. * Ok, we have a good vm_area for this memory access, so
  917. * we can handle it..
  918. */
  919. good_area:
  920. write = error_code & PF_WRITE;
  921. if (unlikely(access_error(error_code, write, vma))) {
  922. bad_area_access_error(regs, error_code, address);
  923. return;
  924. }
  925. /*
  926. * If for any reason at all we couldn't handle the fault,
  927. * make sure we exit gracefully rather than endlessly redo
  928. * the fault:
  929. */
  930. fault = handle_mm_fault(mm, vma, address, write);
  931. if (unlikely(fault & VM_FAULT_ERROR)) {
  932. mm_fault_error(regs, error_code, address, fault);
  933. return;
  934. }
  935. if (fault & VM_FAULT_MAJOR)
  936. tsk->maj_flt++;
  937. else
  938. tsk->min_flt++;
  939. check_v8086_mode(regs, address, tsk);
  940. up_read(&mm->mmap_sem);
  941. }
  942. DEFINE_SPINLOCK(pgd_lock);
  943. LIST_HEAD(pgd_list);
  944. void vmalloc_sync_all(void)
  945. {
  946. unsigned long address;
  947. #ifdef CONFIG_X86_32
  948. if (SHARED_KERNEL_PMD)
  949. return;
  950. for (address = VMALLOC_START & PMD_MASK;
  951. address >= TASK_SIZE && address < FIXADDR_TOP;
  952. address += PMD_SIZE) {
  953. unsigned long flags;
  954. struct page *page;
  955. spin_lock_irqsave(&pgd_lock, flags);
  956. list_for_each_entry(page, &pgd_list, lru) {
  957. if (!vmalloc_sync_one(page_address(page), address))
  958. break;
  959. }
  960. spin_unlock_irqrestore(&pgd_lock, flags);
  961. }
  962. #else /* CONFIG_X86_64 */
  963. for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
  964. address += PGDIR_SIZE) {
  965. const pgd_t *pgd_ref = pgd_offset_k(address);
  966. unsigned long flags;
  967. struct page *page;
  968. if (pgd_none(*pgd_ref))
  969. continue;
  970. spin_lock_irqsave(&pgd_lock, flags);
  971. list_for_each_entry(page, &pgd_list, lru) {
  972. pgd_t *pgd;
  973. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  974. if (pgd_none(*pgd))
  975. set_pgd(pgd, *pgd_ref);
  976. else
  977. BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
  978. }
  979. spin_unlock_irqrestore(&pgd_lock, flags);
  980. }
  981. #endif
  982. }