fault.c 16 KB

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