fault.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/string.h>
  22. #include <linux/types.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/mman.h>
  25. #include <linux/mm.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/highmem.h>
  28. #include <linux/extable.h>
  29. #include <linux/kprobes.h>
  30. #include <linux/kdebug.h>
  31. #include <linux/perf_event.h>
  32. #include <linux/ratelimit.h>
  33. #include <linux/context_tracking.h>
  34. #include <linux/hugetlb.h>
  35. #include <linux/uaccess.h>
  36. #include <asm/firmware.h>
  37. #include <asm/page.h>
  38. #include <asm/pgtable.h>
  39. #include <asm/mmu.h>
  40. #include <asm/mmu_context.h>
  41. #include <asm/tlbflush.h>
  42. #include <asm/siginfo.h>
  43. #include <asm/debug.h>
  44. #include "icswx.h"
  45. #ifdef CONFIG_KPROBES
  46. static inline int notify_page_fault(struct pt_regs *regs)
  47. {
  48. int ret = 0;
  49. /* kprobe_running() needs smp_processor_id() */
  50. if (!user_mode(regs)) {
  51. preempt_disable();
  52. if (kprobe_running() && kprobe_fault_handler(regs, 11))
  53. ret = 1;
  54. preempt_enable();
  55. }
  56. return ret;
  57. }
  58. #else
  59. static inline int notify_page_fault(struct pt_regs *regs)
  60. {
  61. return 0;
  62. }
  63. #endif
  64. /*
  65. * Check whether the instruction at regs->nip is a store using
  66. * an update addressing form which will update r1.
  67. */
  68. static int store_updates_sp(struct pt_regs *regs)
  69. {
  70. unsigned int inst;
  71. if (get_user(inst, (unsigned int __user *)regs->nip))
  72. return 0;
  73. /* check for 1 in the rA field */
  74. if (((inst >> 16) & 0x1f) != 1)
  75. return 0;
  76. /* check major opcode */
  77. switch (inst >> 26) {
  78. case 37: /* stwu */
  79. case 39: /* stbu */
  80. case 45: /* sthu */
  81. case 53: /* stfsu */
  82. case 55: /* stfdu */
  83. return 1;
  84. case 62: /* std or stdu */
  85. return (inst & 3) == 1;
  86. case 31:
  87. /* check minor opcode */
  88. switch ((inst >> 1) & 0x3ff) {
  89. case 181: /* stdux */
  90. case 183: /* stwux */
  91. case 247: /* stbux */
  92. case 439: /* sthux */
  93. case 695: /* stfsux */
  94. case 759: /* stfdux */
  95. return 1;
  96. }
  97. }
  98. return 0;
  99. }
  100. /*
  101. * do_page_fault error handling helpers
  102. */
  103. #define MM_FAULT_RETURN 0
  104. #define MM_FAULT_CONTINUE -1
  105. #define MM_FAULT_ERR(sig) (sig)
  106. static int do_sigbus(struct pt_regs *regs, unsigned long address,
  107. unsigned int fault)
  108. {
  109. siginfo_t info;
  110. unsigned int lsb = 0;
  111. up_read(&current->mm->mmap_sem);
  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. /*
  142. * If we have retry set, the mmap semaphore will have
  143. * alrady been released in __lock_page_or_retry(). Else
  144. * we release it now.
  145. */
  146. if (!(fault & VM_FAULT_RETRY))
  147. up_read(&current->mm->mmap_sem);
  148. /* Coming from kernel, we need to deal with uaccess fixups */
  149. if (user_mode(regs))
  150. return MM_FAULT_RETURN;
  151. return MM_FAULT_ERR(SIGKILL);
  152. }
  153. /* No fault: be happy */
  154. if (!(fault & VM_FAULT_ERROR))
  155. return MM_FAULT_CONTINUE;
  156. /* Out of memory */
  157. if (fault & VM_FAULT_OOM) {
  158. up_read(&current->mm->mmap_sem);
  159. /*
  160. * We ran out of memory, or some other thing happened to us that
  161. * made us unable to handle the page fault gracefully.
  162. */
  163. if (!user_mode(regs))
  164. return MM_FAULT_ERR(SIGKILL);
  165. pagefault_out_of_memory();
  166. return MM_FAULT_RETURN;
  167. }
  168. if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE))
  169. return do_sigbus(regs, addr, fault);
  170. /* We don't understand the fault code, this is fatal */
  171. BUG();
  172. return MM_FAULT_CONTINUE;
  173. }
  174. /*
  175. * For 600- and 800-family processors, the error_code parameter is DSISR
  176. * for a data fault, SRR1 for an instruction fault. For 400-family processors
  177. * the error_code parameter is ESR for a data fault, 0 for an instruction
  178. * fault.
  179. * For 64-bit processors, the error_code parameter is
  180. * - DSISR for a non-SLB data access fault,
  181. * - SRR1 & 0x08000000 for a non-SLB instruction access fault
  182. * - 0 any SLB fault.
  183. *
  184. * The return value is 0 if the fault was handled, or the signal
  185. * number if this is a kernel fault that can't be handled here.
  186. */
  187. int do_page_fault(struct pt_regs *regs, unsigned long address,
  188. unsigned long error_code)
  189. {
  190. enum ctx_state prev_state = exception_enter();
  191. struct vm_area_struct * vma;
  192. struct mm_struct *mm = current->mm;
  193. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  194. int code = SEGV_MAPERR;
  195. int is_write = 0;
  196. int trap = TRAP(regs);
  197. int is_exec = trap == 0x400;
  198. int fault;
  199. int rc = 0, store_update_sp = 0;
  200. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  201. /*
  202. * Fortunately the bit assignments in SRR1 for an instruction
  203. * fault and DSISR for a data fault are mostly the same for the
  204. * bits we are interested in. But there are some bits which
  205. * indicate errors in DSISR but can validly be set in SRR1.
  206. */
  207. if (trap == 0x400)
  208. error_code &= 0x48200000;
  209. else
  210. is_write = error_code & DSISR_ISSTORE;
  211. #else
  212. is_write = error_code & ESR_DST;
  213. #endif /* CONFIG_4xx || CONFIG_BOOKE */
  214. #ifdef CONFIG_PPC_ICSWX
  215. /*
  216. * we need to do this early because this "data storage
  217. * interrupt" does not update the DAR/DEAR so we don't want to
  218. * look at it
  219. */
  220. if (error_code & ICSWX_DSI_UCT) {
  221. rc = acop_handle_fault(regs, address, error_code);
  222. if (rc)
  223. goto bail;
  224. }
  225. #endif /* CONFIG_PPC_ICSWX */
  226. if (notify_page_fault(regs))
  227. goto bail;
  228. if (unlikely(debugger_fault_handler(regs)))
  229. goto bail;
  230. /* On a kernel SLB miss we can only check for a valid exception entry */
  231. if (!user_mode(regs) && (address >= TASK_SIZE)) {
  232. rc = SIGSEGV;
  233. goto bail;
  234. }
  235. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE) || \
  236. defined(CONFIG_PPC_BOOK3S_64))
  237. if (error_code & DSISR_DABRMATCH) {
  238. /* breakpoint match */
  239. do_break(regs, address, error_code);
  240. goto bail;
  241. }
  242. #endif
  243. /* We restore the interrupt state now */
  244. if (!arch_irq_disabled_regs(regs))
  245. local_irq_enable();
  246. if (faulthandler_disabled() || mm == NULL) {
  247. if (!user_mode(regs)) {
  248. rc = SIGSEGV;
  249. goto bail;
  250. }
  251. /* faulthandler_disabled() in user mode is really bad,
  252. as is current->mm == NULL. */
  253. printk(KERN_EMERG "Page fault in user mode with "
  254. "faulthandler_disabled() = %d mm = %p\n",
  255. faulthandler_disabled(), mm);
  256. printk(KERN_EMERG "NIP = %lx MSR = %lx\n",
  257. regs->nip, regs->msr);
  258. die("Weird page fault", regs, SIGSEGV);
  259. }
  260. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  261. /*
  262. * We want to do this outside mmap_sem, because reading code around nip
  263. * can result in fault, which will cause a deadlock when called with
  264. * mmap_sem held
  265. */
  266. if (!is_exec && user_mode(regs))
  267. store_update_sp = store_updates_sp(regs);
  268. if (user_mode(regs))
  269. flags |= FAULT_FLAG_USER;
  270. /* When running in the kernel we expect faults to occur only to
  271. * addresses in user space. All other faults represent errors in the
  272. * kernel and should generate an OOPS. Unfortunately, in the case of an
  273. * erroneous fault occurring in a code path which already holds mmap_sem
  274. * we will deadlock attempting to validate the fault against the
  275. * address space. Luckily the kernel only validly references user
  276. * space from well defined areas of code, which are listed in the
  277. * exceptions table.
  278. *
  279. * As the vast majority of faults will be valid we will only perform
  280. * the source reference check when there is a possibility of a deadlock.
  281. * Attempt to lock the address space, if we cannot we then validate the
  282. * source. If this is invalid we can skip the address space check,
  283. * thus avoiding the deadlock.
  284. */
  285. if (!down_read_trylock(&mm->mmap_sem)) {
  286. if (!user_mode(regs) && !search_exception_tables(regs->nip))
  287. goto bad_area_nosemaphore;
  288. retry:
  289. down_read(&mm->mmap_sem);
  290. } else {
  291. /*
  292. * The above down_read_trylock() might have succeeded in
  293. * which case we'll have missed the might_sleep() from
  294. * down_read():
  295. */
  296. might_sleep();
  297. }
  298. vma = find_vma(mm, address);
  299. if (!vma)
  300. goto bad_area;
  301. if (vma->vm_start <= address)
  302. goto good_area;
  303. if (!(vma->vm_flags & VM_GROWSDOWN))
  304. goto bad_area;
  305. /*
  306. * N.B. The POWER/Open ABI allows programs to access up to
  307. * 288 bytes below the stack pointer.
  308. * The kernel signal delivery code writes up to about 1.5kB
  309. * below the stack pointer (r1) before decrementing it.
  310. * The exec code can write slightly over 640kB to the stack
  311. * before setting the user r1. Thus we allow the stack to
  312. * expand to 1MB without further checks.
  313. */
  314. if (address + 0x100000 < vma->vm_end) {
  315. /* get user regs even if this fault is in kernel mode */
  316. struct pt_regs *uregs = current->thread.regs;
  317. if (uregs == NULL)
  318. goto bad_area;
  319. /*
  320. * A user-mode access to an address a long way below
  321. * the stack pointer is only valid if the instruction
  322. * is one which would update the stack pointer to the
  323. * address accessed if the instruction completed,
  324. * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
  325. * (or the byte, halfword, float or double forms).
  326. *
  327. * If we don't check this then any write to the area
  328. * between the last mapped region and the stack will
  329. * expand the stack rather than segfaulting.
  330. */
  331. if (address + 2048 < uregs->gpr[1] && !store_update_sp)
  332. goto bad_area;
  333. }
  334. if (expand_stack(vma, address))
  335. goto bad_area;
  336. good_area:
  337. code = SEGV_ACCERR;
  338. #if defined(CONFIG_6xx)
  339. if (error_code & 0x95700000)
  340. /* an error such as lwarx to I/O controller space,
  341. address matching DABR, eciwx, etc. */
  342. goto bad_area;
  343. #endif /* CONFIG_6xx */
  344. #if defined(CONFIG_8xx)
  345. /* The MPC8xx seems to always set 0x80000000, which is
  346. * "undefined". Of those that can be set, this is the only
  347. * one which seems bad.
  348. */
  349. if (error_code & 0x10000000)
  350. /* Guarded storage error. */
  351. goto bad_area;
  352. #endif /* CONFIG_8xx */
  353. if (is_exec) {
  354. /*
  355. * Allow execution from readable areas if the MMU does not
  356. * provide separate controls over reading and executing.
  357. *
  358. * Note: That code used to not be enabled for 4xx/BookE.
  359. * It is now as I/D cache coherency for these is done at
  360. * set_pte_at() time and I see no reason why the test
  361. * below wouldn't be valid on those processors. This -may-
  362. * break programs compiled with a really old ABI though.
  363. */
  364. if (!(vma->vm_flags & VM_EXEC) &&
  365. (cpu_has_feature(CPU_FTR_NOEXECUTE) ||
  366. !(vma->vm_flags & (VM_READ | VM_WRITE))))
  367. goto bad_area;
  368. #ifdef CONFIG_PPC_STD_MMU
  369. /*
  370. * protfault should only happen due to us
  371. * mapping a region readonly temporarily. PROT_NONE
  372. * is also covered by the VMA check above.
  373. */
  374. WARN_ON_ONCE(error_code & DSISR_PROTFAULT);
  375. #endif /* CONFIG_PPC_STD_MMU */
  376. /* a write */
  377. } else if (is_write) {
  378. if (!(vma->vm_flags & VM_WRITE))
  379. goto bad_area;
  380. flags |= FAULT_FLAG_WRITE;
  381. /* a read */
  382. } else {
  383. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  384. goto bad_area;
  385. WARN_ON_ONCE(error_code & DSISR_PROTFAULT);
  386. }
  387. /*
  388. * If for any reason at all we couldn't handle the fault,
  389. * make sure we exit gracefully rather than endlessly redo
  390. * the fault.
  391. */
  392. fault = handle_mm_fault(vma, address, flags);
  393. if (unlikely(fault & (VM_FAULT_RETRY|VM_FAULT_ERROR))) {
  394. if (fault & VM_FAULT_SIGSEGV)
  395. goto bad_area;
  396. rc = mm_fault_error(regs, address, fault);
  397. if (rc >= MM_FAULT_RETURN)
  398. goto bail;
  399. else
  400. rc = 0;
  401. }
  402. /*
  403. * Major/minor page fault accounting is only done on the
  404. * initial attempt. If we go through a retry, it is extremely
  405. * likely that the page will be found in page cache at that point.
  406. */
  407. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  408. if (fault & VM_FAULT_MAJOR) {
  409. current->maj_flt++;
  410. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  411. regs, address);
  412. #ifdef CONFIG_PPC_SMLPAR
  413. if (firmware_has_feature(FW_FEATURE_CMO)) {
  414. u32 page_ins;
  415. preempt_disable();
  416. page_ins = be32_to_cpu(get_lppaca()->page_ins);
  417. page_ins += 1 << PAGE_FACTOR;
  418. get_lppaca()->page_ins = cpu_to_be32(page_ins);
  419. preempt_enable();
  420. }
  421. #endif /* CONFIG_PPC_SMLPAR */
  422. } else {
  423. current->min_flt++;
  424. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  425. regs, address);
  426. }
  427. if (fault & VM_FAULT_RETRY) {
  428. /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  429. * of starvation. */
  430. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  431. flags |= FAULT_FLAG_TRIED;
  432. goto retry;
  433. }
  434. }
  435. up_read(&mm->mmap_sem);
  436. goto bail;
  437. bad_area:
  438. up_read(&mm->mmap_sem);
  439. bad_area_nosemaphore:
  440. /* User mode accesses cause a SIGSEGV */
  441. if (user_mode(regs)) {
  442. _exception(SIGSEGV, regs, code, address);
  443. goto bail;
  444. }
  445. if (is_exec && (error_code & DSISR_PROTFAULT))
  446. printk_ratelimited(KERN_CRIT "kernel tried to execute NX-protected"
  447. " page (%lx) - exploit attempt? (uid: %d)\n",
  448. address, from_kuid(&init_user_ns, current_uid()));
  449. rc = SIGSEGV;
  450. bail:
  451. exception_exit(prev_state);
  452. return rc;
  453. }
  454. NOKPROBE_SYMBOL(do_page_fault);
  455. /*
  456. * bad_page_fault is called when we have a bad access from the kernel.
  457. * It is called from the DSI and ISI handlers in head.S and from some
  458. * of the procedures in traps.c.
  459. */
  460. void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
  461. {
  462. const struct exception_table_entry *entry;
  463. /* Are we prepared to handle this fault? */
  464. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  465. regs->nip = entry->fixup;
  466. return;
  467. }
  468. /* kernel has accessed a bad area */
  469. switch (regs->trap) {
  470. case 0x300:
  471. case 0x380:
  472. printk(KERN_ALERT "Unable to handle kernel paging request for "
  473. "data at address 0x%08lx\n", regs->dar);
  474. break;
  475. case 0x400:
  476. case 0x480:
  477. printk(KERN_ALERT "Unable to handle kernel paging request for "
  478. "instruction fetch\n");
  479. break;
  480. case 0x600:
  481. printk(KERN_ALERT "Unable to handle kernel paging request for "
  482. "unaligned access at address 0x%08lx\n", regs->dar);
  483. break;
  484. default:
  485. printk(KERN_ALERT "Unable to handle kernel paging request for "
  486. "unknown fault\n");
  487. break;
  488. }
  489. printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n",
  490. regs->nip);
  491. if (task_stack_end_corrupted(current))
  492. printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
  493. die("Kernel access of bad area", regs, sig);
  494. }