fault.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * PowerPC version
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. *
  5. * Derived from "arch/i386/mm/fault.c"
  6. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  7. *
  8. * Modified by Cort Dougan and Paul Mackerras.
  9. *
  10. * Modified for PPC64 by Dave Engebretsen (engebret@ibm.com)
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <linux/signal.h>
  18. #include <linux/sched.h>
  19. #include <linux/sched/task_stack.h>
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/string.h>
  23. #include <linux/types.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/mman.h>
  26. #include <linux/mm.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/highmem.h>
  29. #include <linux/extable.h>
  30. #include <linux/kprobes.h>
  31. #include <linux/kdebug.h>
  32. #include <linux/perf_event.h>
  33. #include <linux/ratelimit.h>
  34. #include <linux/context_tracking.h>
  35. #include <linux/hugetlb.h>
  36. #include <linux/uaccess.h>
  37. #include <asm/firmware.h>
  38. #include <asm/page.h>
  39. #include <asm/pgtable.h>
  40. #include <asm/mmu.h>
  41. #include <asm/mmu_context.h>
  42. #include <asm/tlbflush.h>
  43. #include <asm/siginfo.h>
  44. #include <asm/debug.h>
  45. #include "icswx.h"
  46. static inline bool notify_page_fault(struct pt_regs *regs)
  47. {
  48. bool ret = false;
  49. #ifdef CONFIG_KPROBES
  50. /* kprobe_running() needs smp_processor_id() */
  51. if (!user_mode(regs)) {
  52. preempt_disable();
  53. if (kprobe_running() && kprobe_fault_handler(regs, 11))
  54. ret = true;
  55. preempt_enable();
  56. }
  57. #endif /* CONFIG_KPROBES */
  58. if (unlikely(debugger_fault_handler(regs)))
  59. ret = true;
  60. return ret;
  61. }
  62. /*
  63. * Check whether the instruction at regs->nip is a store using
  64. * an update addressing form which will update r1.
  65. */
  66. static int store_updates_sp(struct pt_regs *regs)
  67. {
  68. unsigned int inst;
  69. if (get_user(inst, (unsigned int __user *)regs->nip))
  70. return 0;
  71. /* check for 1 in the rA field */
  72. if (((inst >> 16) & 0x1f) != 1)
  73. return 0;
  74. /* check major opcode */
  75. switch (inst >> 26) {
  76. case 37: /* stwu */
  77. case 39: /* stbu */
  78. case 45: /* sthu */
  79. case 53: /* stfsu */
  80. case 55: /* stfdu */
  81. return 1;
  82. case 62: /* std or stdu */
  83. return (inst & 3) == 1;
  84. case 31:
  85. /* check minor opcode */
  86. switch ((inst >> 1) & 0x3ff) {
  87. case 181: /* stdux */
  88. case 183: /* stwux */
  89. case 247: /* stbux */
  90. case 439: /* sthux */
  91. case 695: /* stfsux */
  92. case 759: /* stfdux */
  93. return 1;
  94. }
  95. }
  96. return 0;
  97. }
  98. /*
  99. * do_page_fault error handling helpers
  100. */
  101. static int
  102. __bad_area_nosemaphore(struct pt_regs *regs, unsigned long address, int si_code)
  103. {
  104. /*
  105. * If we are in kernel mode, bail out with a SEGV, this will
  106. * be caught by the assembly which will restore the non-volatile
  107. * registers before calling bad_page_fault()
  108. */
  109. if (!user_mode(regs))
  110. return SIGSEGV;
  111. _exception(SIGSEGV, regs, si_code, address);
  112. return 0;
  113. }
  114. static noinline int bad_area_nosemaphore(struct pt_regs *regs, unsigned long address)
  115. {
  116. return __bad_area_nosemaphore(regs, address, SEGV_MAPERR);
  117. }
  118. static int __bad_area(struct pt_regs *regs, unsigned long address, int si_code)
  119. {
  120. struct mm_struct *mm = current->mm;
  121. /*
  122. * Something tried to access memory that isn't in our memory map..
  123. * Fix it, but check if it's kernel or user first..
  124. */
  125. up_read(&mm->mmap_sem);
  126. return __bad_area_nosemaphore(regs, address, si_code);
  127. }
  128. static noinline int bad_area(struct pt_regs *regs, unsigned long address)
  129. {
  130. return __bad_area(regs, address, SEGV_MAPERR);
  131. }
  132. static int do_sigbus(struct pt_regs *regs, unsigned long address,
  133. unsigned int fault)
  134. {
  135. siginfo_t info;
  136. unsigned int lsb = 0;
  137. if (!user_mode(regs))
  138. return SIGBUS;
  139. current->thread.trap_nr = BUS_ADRERR;
  140. info.si_signo = SIGBUS;
  141. info.si_errno = 0;
  142. info.si_code = BUS_ADRERR;
  143. info.si_addr = (void __user *)address;
  144. #ifdef CONFIG_MEMORY_FAILURE
  145. if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
  146. pr_err("MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
  147. current->comm, current->pid, address);
  148. info.si_code = BUS_MCEERR_AR;
  149. }
  150. if (fault & VM_FAULT_HWPOISON_LARGE)
  151. lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
  152. if (fault & VM_FAULT_HWPOISON)
  153. lsb = PAGE_SHIFT;
  154. #endif
  155. info.si_addr_lsb = lsb;
  156. force_sig_info(SIGBUS, &info, current);
  157. return 0;
  158. }
  159. static int mm_fault_error(struct pt_regs *regs, unsigned long addr, int fault)
  160. {
  161. /*
  162. * Kernel page fault interrupted by SIGKILL. We have no reason to
  163. * continue processing.
  164. */
  165. if (fatal_signal_pending(current) && !user_mode(regs))
  166. return SIGKILL;
  167. /* Out of memory */
  168. if (fault & VM_FAULT_OOM) {
  169. /*
  170. * We ran out of memory, or some other thing happened to us that
  171. * made us unable to handle the page fault gracefully.
  172. */
  173. if (!user_mode(regs))
  174. return SIGSEGV;
  175. pagefault_out_of_memory();
  176. } else {
  177. if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
  178. VM_FAULT_HWPOISON_LARGE))
  179. return do_sigbus(regs, addr, fault);
  180. else if (fault & VM_FAULT_SIGSEGV)
  181. return bad_area_nosemaphore(regs, addr);
  182. else
  183. BUG();
  184. }
  185. return 0;
  186. }
  187. /* Is this a bad kernel fault ? */
  188. static bool bad_kernel_fault(bool is_exec, unsigned long error_code,
  189. unsigned long address)
  190. {
  191. if (is_exec && (error_code & (DSISR_NOEXEC_OR_G | DSISR_KEYFAULT))) {
  192. printk_ratelimited(KERN_CRIT "kernel tried to execute"
  193. " exec-protected page (%lx) -"
  194. "exploit attempt? (uid: %d)\n",
  195. address, from_kuid(&init_user_ns,
  196. current_uid()));
  197. }
  198. return is_exec || (address >= TASK_SIZE);
  199. }
  200. #ifdef CONFIG_PPC_SMLPAR
  201. static inline void cmo_account_page_fault(void)
  202. {
  203. if (firmware_has_feature(FW_FEATURE_CMO)) {
  204. u32 page_ins;
  205. preempt_disable();
  206. page_ins = be32_to_cpu(get_lppaca()->page_ins);
  207. page_ins += 1 << PAGE_FACTOR;
  208. get_lppaca()->page_ins = cpu_to_be32(page_ins);
  209. preempt_enable();
  210. }
  211. }
  212. #else
  213. static inline void cmo_account_page_fault(void) { }
  214. #endif /* CONFIG_PPC_SMLPAR */
  215. #ifdef CONFIG_PPC_STD_MMU
  216. static void sanity_check_fault(bool is_write, unsigned long error_code)
  217. {
  218. /*
  219. * For hash translation mode, we should never get a
  220. * PROTFAULT. Any update to pte to reduce access will result in us
  221. * removing the hash page table entry, thus resulting in a DSISR_NOHPTE
  222. * fault instead of DSISR_PROTFAULT.
  223. *
  224. * A pte update to relax the access will not result in a hash page table
  225. * entry invalidate and hence can result in DSISR_PROTFAULT.
  226. * ptep_set_access_flags() doesn't do a hpte flush. This is why we have
  227. * the special !is_write in the below conditional.
  228. *
  229. * For platforms that doesn't supports coherent icache and do support
  230. * per page noexec bit, we do setup things such that we do the
  231. * sync between D/I cache via fault. But that is handled via low level
  232. * hash fault code (hash_page_do_lazy_icache()) and we should not reach
  233. * here in such case.
  234. *
  235. * For wrong access that can result in PROTFAULT, the above vma->vm_flags
  236. * check should handle those and hence we should fall to the bad_area
  237. * handling correctly.
  238. *
  239. * For embedded with per page exec support that doesn't support coherent
  240. * icache we do get PROTFAULT and we handle that D/I cache sync in
  241. * set_pte_at while taking the noexec/prot fault. Hence this is WARN_ON
  242. * is conditional for server MMU.
  243. *
  244. * For radix, we can get prot fault for autonuma case, because radix
  245. * page table will have them marked noaccess for user.
  246. */
  247. if (!radix_enabled() && !is_write)
  248. WARN_ON_ONCE(error_code & DSISR_PROTFAULT);
  249. }
  250. #else
  251. static void sanity_check_fault(bool is_write, unsigned long error_code) { }
  252. #endif /* CONFIG_PPC_STD_MMU */
  253. /*
  254. * Define the correct "is_write" bit in error_code based
  255. * on the processor family
  256. */
  257. #if (defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  258. #define page_fault_is_write(__err) ((__err) & ESR_DST)
  259. #define page_fault_is_bad(__err) (0)
  260. #else
  261. #define page_fault_is_write(__err) ((__err) & DSISR_ISSTORE)
  262. #if defined(CONFIG_8xx)
  263. #define page_fault_is_bad(__err) ((__err) & 0x10000000)
  264. #elif defined(CONFIG_PPC64)
  265. #define page_fault_is_bad(__err) ((__err) & DSISR_BAD_FAULT_64S)
  266. #else
  267. #define page_fault_is_bad(__err) ((__err) & DSISR_BAD_FAULT_32S)
  268. #endif
  269. #endif
  270. /*
  271. * For 600- and 800-family processors, the error_code parameter is DSISR
  272. * for a data fault, SRR1 for an instruction fault. For 400-family processors
  273. * the error_code parameter is ESR for a data fault, 0 for an instruction
  274. * fault.
  275. * For 64-bit processors, the error_code parameter is
  276. * - DSISR for a non-SLB data access fault,
  277. * - SRR1 & 0x08000000 for a non-SLB instruction access fault
  278. * - 0 any SLB fault.
  279. *
  280. * The return value is 0 if the fault was handled, or the signal
  281. * number if this is a kernel fault that can't be handled here.
  282. */
  283. static int __do_page_fault(struct pt_regs *regs, unsigned long address,
  284. unsigned long error_code)
  285. {
  286. struct vm_area_struct * vma;
  287. struct mm_struct *mm = current->mm;
  288. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  289. int is_exec = TRAP(regs) == 0x400;
  290. int is_user = user_mode(regs);
  291. int is_write = page_fault_is_write(error_code);
  292. int fault;
  293. int store_update_sp = 0;
  294. #ifdef CONFIG_PPC_ICSWX
  295. /*
  296. * we need to do this early because this "data storage
  297. * interrupt" does not update the DAR/DEAR so we don't want to
  298. * look at it
  299. */
  300. if (error_code & ICSWX_DSI_UCT) {
  301. int rc = acop_handle_fault(regs, address, error_code);
  302. if (rc)
  303. return rc;
  304. }
  305. #endif /* CONFIG_PPC_ICSWX */
  306. if (notify_page_fault(regs))
  307. return 0;
  308. if (unlikely(page_fault_is_bad(error_code))) {
  309. if (is_user) {
  310. _exception(SIGBUS, regs, BUS_OBJERR, address);
  311. return 0;
  312. }
  313. return SIGBUS;
  314. }
  315. /* Additional sanity check(s) */
  316. sanity_check_fault(is_write, error_code);
  317. /*
  318. * The kernel should never take an execute fault nor should it
  319. * take a page fault to a kernel address.
  320. */
  321. if (unlikely(!is_user && bad_kernel_fault(is_exec, error_code, address)))
  322. return SIGSEGV;
  323. /* We restore the interrupt state now */
  324. if (!arch_irq_disabled_regs(regs))
  325. local_irq_enable();
  326. if (faulthandler_disabled() || mm == NULL) {
  327. if (!is_user)
  328. return SIGSEGV;
  329. /* faulthandler_disabled() in user mode is really bad,
  330. as is current->mm == NULL. */
  331. printk(KERN_EMERG "Page fault in user mode with "
  332. "faulthandler_disabled() = %d mm = %p\n",
  333. faulthandler_disabled(), mm);
  334. printk(KERN_EMERG "NIP = %lx MSR = %lx\n",
  335. regs->nip, regs->msr);
  336. die("Weird page fault", regs, SIGSEGV);
  337. }
  338. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  339. /*
  340. * We want to do this outside mmap_sem, because reading code around nip
  341. * can result in fault, which will cause a deadlock when called with
  342. * mmap_sem held
  343. */
  344. if (is_write && is_user)
  345. store_update_sp = store_updates_sp(regs);
  346. if (is_user)
  347. flags |= FAULT_FLAG_USER;
  348. /* When running in the kernel we expect faults to occur only to
  349. * addresses in user space. All other faults represent errors in the
  350. * kernel and should generate an OOPS. Unfortunately, in the case of an
  351. * erroneous fault occurring in a code path which already holds mmap_sem
  352. * we will deadlock attempting to validate the fault against the
  353. * address space. Luckily the kernel only validly references user
  354. * space from well defined areas of code, which are listed in the
  355. * exceptions table.
  356. *
  357. * As the vast majority of faults will be valid we will only perform
  358. * the source reference check when there is a possibility of a deadlock.
  359. * Attempt to lock the address space, if we cannot we then validate the
  360. * source. If this is invalid we can skip the address space check,
  361. * thus avoiding the deadlock.
  362. */
  363. if (!down_read_trylock(&mm->mmap_sem)) {
  364. if (!is_user && !search_exception_tables(regs->nip))
  365. return bad_area_nosemaphore(regs, address);
  366. retry:
  367. down_read(&mm->mmap_sem);
  368. } else {
  369. /*
  370. * The above down_read_trylock() might have succeeded in
  371. * which case we'll have missed the might_sleep() from
  372. * down_read():
  373. */
  374. might_sleep();
  375. }
  376. vma = find_vma(mm, address);
  377. if (!vma)
  378. return bad_area(regs, address);
  379. if (vma->vm_start <= address)
  380. goto good_area;
  381. if (!(vma->vm_flags & VM_GROWSDOWN))
  382. return bad_area(regs, address);
  383. /*
  384. * N.B. The POWER/Open ABI allows programs to access up to
  385. * 288 bytes below the stack pointer.
  386. * The kernel signal delivery code writes up to about 1.5kB
  387. * below the stack pointer (r1) before decrementing it.
  388. * The exec code can write slightly over 640kB to the stack
  389. * before setting the user r1. Thus we allow the stack to
  390. * expand to 1MB without further checks.
  391. */
  392. if (address + 0x100000 < vma->vm_end) {
  393. /* get user regs even if this fault is in kernel mode */
  394. struct pt_regs *uregs = current->thread.regs;
  395. if (uregs == NULL)
  396. return bad_area(regs, address);
  397. /*
  398. * A user-mode access to an address a long way below
  399. * the stack pointer is only valid if the instruction
  400. * is one which would update the stack pointer to the
  401. * address accessed if the instruction completed,
  402. * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
  403. * (or the byte, halfword, float or double forms).
  404. *
  405. * If we don't check this then any write to the area
  406. * between the last mapped region and the stack will
  407. * expand the stack rather than segfaulting.
  408. */
  409. if (address + 2048 < uregs->gpr[1] && !store_update_sp)
  410. return bad_area(regs, address);
  411. }
  412. if (expand_stack(vma, address))
  413. return bad_area(regs, address);
  414. good_area:
  415. if (is_exec) {
  416. /*
  417. * Allow execution from readable areas if the MMU does not
  418. * provide separate controls over reading and executing.
  419. *
  420. * Note: That code used to not be enabled for 4xx/BookE.
  421. * It is now as I/D cache coherency for these is done at
  422. * set_pte_at() time and I see no reason why the test
  423. * below wouldn't be valid on those processors. This -may-
  424. * break programs compiled with a really old ABI though.
  425. */
  426. if (!(vma->vm_flags & VM_EXEC) &&
  427. (cpu_has_feature(CPU_FTR_NOEXECUTE) ||
  428. !(vma->vm_flags & (VM_READ | VM_WRITE))))
  429. return bad_area(regs, address);
  430. /* a write */
  431. } else if (is_write) {
  432. if (!(vma->vm_flags & VM_WRITE))
  433. return bad_area(regs, address);
  434. flags |= FAULT_FLAG_WRITE;
  435. /* a read */
  436. } else {
  437. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  438. return bad_area(regs, address);
  439. }
  440. /*
  441. * If for any reason at all we couldn't handle the fault,
  442. * make sure we exit gracefully rather than endlessly redo
  443. * the fault.
  444. */
  445. fault = handle_mm_fault(vma, address, flags);
  446. /*
  447. * Handle the retry right now, the mmap_sem has been released in that
  448. * case.
  449. */
  450. if (unlikely(fault & VM_FAULT_RETRY)) {
  451. /* We retry only once */
  452. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  453. /*
  454. * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  455. * of starvation.
  456. */
  457. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  458. flags |= FAULT_FLAG_TRIED;
  459. if (!fatal_signal_pending(current))
  460. goto retry;
  461. }
  462. /*
  463. * User mode? Just return to handle the fatal exception otherwise
  464. * return to bad_page_fault
  465. */
  466. return is_user ? 0 : SIGBUS;
  467. }
  468. up_read(&current->mm->mmap_sem);
  469. if (unlikely(fault & VM_FAULT_ERROR))
  470. return mm_fault_error(regs, address, fault);
  471. /*
  472. * Major/minor page fault accounting.
  473. */
  474. if (fault & VM_FAULT_MAJOR) {
  475. current->maj_flt++;
  476. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
  477. cmo_account_page_fault();
  478. } else {
  479. current->min_flt++;
  480. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
  481. }
  482. return 0;
  483. }
  484. NOKPROBE_SYMBOL(__do_page_fault);
  485. int do_page_fault(struct pt_regs *regs, unsigned long address,
  486. unsigned long error_code)
  487. {
  488. enum ctx_state prev_state = exception_enter();
  489. int rc = __do_page_fault(regs, address, error_code);
  490. exception_exit(prev_state);
  491. return rc;
  492. }
  493. NOKPROBE_SYMBOL(do_page_fault);
  494. /*
  495. * bad_page_fault is called when we have a bad access from the kernel.
  496. * It is called from the DSI and ISI handlers in head.S and from some
  497. * of the procedures in traps.c.
  498. */
  499. void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
  500. {
  501. const struct exception_table_entry *entry;
  502. /* Are we prepared to handle this fault? */
  503. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  504. regs->nip = extable_fixup(entry);
  505. return;
  506. }
  507. /* kernel has accessed a bad area */
  508. switch (regs->trap) {
  509. case 0x300:
  510. case 0x380:
  511. printk(KERN_ALERT "Unable to handle kernel paging request for "
  512. "data at address 0x%08lx\n", regs->dar);
  513. break;
  514. case 0x400:
  515. case 0x480:
  516. printk(KERN_ALERT "Unable to handle kernel paging request for "
  517. "instruction fetch\n");
  518. break;
  519. case 0x600:
  520. printk(KERN_ALERT "Unable to handle kernel paging request for "
  521. "unaligned access at address 0x%08lx\n", regs->dar);
  522. break;
  523. default:
  524. printk(KERN_ALERT "Unable to handle kernel paging request for "
  525. "unknown fault\n");
  526. break;
  527. }
  528. printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n",
  529. regs->nip);
  530. if (task_stack_end_corrupted(current))
  531. printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
  532. die("Kernel access of bad area", regs, sig);
  533. }