fault.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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/config.h>
  18. #include <linux/signal.h>
  19. #include <linux/sched.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/module.h>
  30. #include <linux/kprobes.h>
  31. #include <asm/page.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/mmu.h>
  34. #include <asm/mmu_context.h>
  35. #include <asm/system.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/tlbflush.h>
  38. #include <asm/kdebug.h>
  39. #include <asm/siginfo.h>
  40. #ifdef CONFIG_KPROBES
  41. ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain);
  42. /* Hook to register for page fault notifications */
  43. int register_page_fault_notifier(struct notifier_block *nb)
  44. {
  45. return atomic_notifier_chain_register(&notify_page_fault_chain, nb);
  46. }
  47. int unregister_page_fault_notifier(struct notifier_block *nb)
  48. {
  49. return atomic_notifier_chain_unregister(&notify_page_fault_chain, nb);
  50. }
  51. static inline int notify_page_fault(enum die_val val, const char *str,
  52. struct pt_regs *regs, long err, int trap, int sig)
  53. {
  54. struct die_args args = {
  55. .regs = regs,
  56. .str = str,
  57. .err = err,
  58. .trapnr = trap,
  59. .signr = sig
  60. };
  61. return atomic_notifier_call_chain(&notify_page_fault_chain, val, &args);
  62. }
  63. #else
  64. static inline int notify_page_fault(enum die_val val, const char *str,
  65. struct pt_regs *regs, long err, int trap, int sig)
  66. {
  67. return NOTIFY_DONE;
  68. }
  69. #endif
  70. /*
  71. * Check whether the instruction at regs->nip is a store using
  72. * an update addressing form which will update r1.
  73. */
  74. static int store_updates_sp(struct pt_regs *regs)
  75. {
  76. unsigned int inst;
  77. if (get_user(inst, (unsigned int __user *)regs->nip))
  78. return 0;
  79. /* check for 1 in the rA field */
  80. if (((inst >> 16) & 0x1f) != 1)
  81. return 0;
  82. /* check major opcode */
  83. switch (inst >> 26) {
  84. case 37: /* stwu */
  85. case 39: /* stbu */
  86. case 45: /* sthu */
  87. case 53: /* stfsu */
  88. case 55: /* stfdu */
  89. return 1;
  90. case 62: /* std or stdu */
  91. return (inst & 3) == 1;
  92. case 31:
  93. /* check minor opcode */
  94. switch ((inst >> 1) & 0x3ff) {
  95. case 181: /* stdux */
  96. case 183: /* stwux */
  97. case 247: /* stbux */
  98. case 439: /* sthux */
  99. case 695: /* stfsux */
  100. case 759: /* stfdux */
  101. return 1;
  102. }
  103. }
  104. return 0;
  105. }
  106. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  107. static void do_dabr(struct pt_regs *regs, unsigned long address,
  108. unsigned long error_code)
  109. {
  110. siginfo_t info;
  111. if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
  112. 11, SIGSEGV) == NOTIFY_STOP)
  113. return;
  114. if (debugger_dabr_match(regs))
  115. return;
  116. /* Clear the DABR */
  117. set_dabr(0);
  118. /* Deliver the signal to userspace */
  119. info.si_signo = SIGTRAP;
  120. info.si_errno = 0;
  121. info.si_code = TRAP_HWBKPT;
  122. info.si_addr = (void __user *)address;
  123. force_sig_info(SIGTRAP, &info, current);
  124. }
  125. #endif /* !(CONFIG_4xx || CONFIG_BOOKE)*/
  126. /*
  127. * For 600- and 800-family processors, the error_code parameter is DSISR
  128. * for a data fault, SRR1 for an instruction fault. For 400-family processors
  129. * the error_code parameter is ESR for a data fault, 0 for an instruction
  130. * fault.
  131. * For 64-bit processors, the error_code parameter is
  132. * - DSISR for a non-SLB data access fault,
  133. * - SRR1 & 0x08000000 for a non-SLB instruction access fault
  134. * - 0 any SLB fault.
  135. *
  136. * The return value is 0 if the fault was handled, or the signal
  137. * number if this is a kernel fault that can't be handled here.
  138. */
  139. int __kprobes do_page_fault(struct pt_regs *regs, unsigned long address,
  140. unsigned long error_code)
  141. {
  142. struct vm_area_struct * vma;
  143. struct mm_struct *mm = current->mm;
  144. siginfo_t info;
  145. int code = SEGV_MAPERR;
  146. int is_write = 0;
  147. int trap = TRAP(regs);
  148. int is_exec = trap == 0x400;
  149. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  150. /*
  151. * Fortunately the bit assignments in SRR1 for an instruction
  152. * fault and DSISR for a data fault are mostly the same for the
  153. * bits we are interested in. But there are some bits which
  154. * indicate errors in DSISR but can validly be set in SRR1.
  155. */
  156. if (trap == 0x400)
  157. error_code &= 0x48200000;
  158. else
  159. is_write = error_code & DSISR_ISSTORE;
  160. #else
  161. is_write = error_code & ESR_DST;
  162. #endif /* CONFIG_4xx || CONFIG_BOOKE */
  163. if (notify_page_fault(DIE_PAGE_FAULT, "page_fault", regs, error_code,
  164. 11, SIGSEGV) == NOTIFY_STOP)
  165. return 0;
  166. if (trap == 0x300) {
  167. if (debugger_fault_handler(regs))
  168. return 0;
  169. }
  170. /* On a kernel SLB miss we can only check for a valid exception entry */
  171. if (!user_mode(regs) && (address >= TASK_SIZE))
  172. return SIGSEGV;
  173. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  174. if (error_code & DSISR_DABRMATCH) {
  175. /* DABR match */
  176. do_dabr(regs, address, error_code);
  177. return 0;
  178. }
  179. #endif /* !(CONFIG_4xx || CONFIG_BOOKE)*/
  180. if (in_atomic() || mm == NULL) {
  181. if (!user_mode(regs))
  182. return SIGSEGV;
  183. /* in_atomic() in user mode is really bad,
  184. as is current->mm == NULL. */
  185. printk(KERN_EMERG "Page fault in user mode with"
  186. "in_atomic() = %d mm = %p\n", in_atomic(), mm);
  187. printk(KERN_EMERG "NIP = %lx MSR = %lx\n",
  188. regs->nip, regs->msr);
  189. die("Weird page fault", regs, SIGSEGV);
  190. }
  191. /* When running in the kernel we expect faults to occur only to
  192. * addresses in user space. All other faults represent errors in the
  193. * kernel and should generate an OOPS. Unfortunately, in the case of an
  194. * erroneous fault occurring in a code path which already holds mmap_sem
  195. * we will deadlock attempting to validate the fault against the
  196. * address space. Luckily the kernel only validly references user
  197. * space from well defined areas of code, which are listed in the
  198. * exceptions table.
  199. *
  200. * As the vast majority of faults will be valid we will only perform
  201. * the source reference check when there is a possibility of a deadlock.
  202. * Attempt to lock the address space, if we cannot we then validate the
  203. * source. If this is invalid we can skip the address space check,
  204. * thus avoiding the deadlock.
  205. */
  206. if (!down_read_trylock(&mm->mmap_sem)) {
  207. if (!user_mode(regs) && !search_exception_tables(regs->nip))
  208. goto bad_area_nosemaphore;
  209. down_read(&mm->mmap_sem);
  210. }
  211. vma = find_vma(mm, address);
  212. if (!vma)
  213. goto bad_area;
  214. if (vma->vm_start <= address)
  215. goto good_area;
  216. if (!(vma->vm_flags & VM_GROWSDOWN))
  217. goto bad_area;
  218. /*
  219. * N.B. The POWER/Open ABI allows programs to access up to
  220. * 288 bytes below the stack pointer.
  221. * The kernel signal delivery code writes up to about 1.5kB
  222. * below the stack pointer (r1) before decrementing it.
  223. * The exec code can write slightly over 640kB to the stack
  224. * before setting the user r1. Thus we allow the stack to
  225. * expand to 1MB without further checks.
  226. */
  227. if (address + 0x100000 < vma->vm_end) {
  228. /* get user regs even if this fault is in kernel mode */
  229. struct pt_regs *uregs = current->thread.regs;
  230. if (uregs == NULL)
  231. goto bad_area;
  232. /*
  233. * A user-mode access to an address a long way below
  234. * the stack pointer is only valid if the instruction
  235. * is one which would update the stack pointer to the
  236. * address accessed if the instruction completed,
  237. * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
  238. * (or the byte, halfword, float or double forms).
  239. *
  240. * If we don't check this then any write to the area
  241. * between the last mapped region and the stack will
  242. * expand the stack rather than segfaulting.
  243. */
  244. if (address + 2048 < uregs->gpr[1]
  245. && (!user_mode(regs) || !store_updates_sp(regs)))
  246. goto bad_area;
  247. }
  248. if (expand_stack(vma, address))
  249. goto bad_area;
  250. good_area:
  251. code = SEGV_ACCERR;
  252. #if defined(CONFIG_6xx)
  253. if (error_code & 0x95700000)
  254. /* an error such as lwarx to I/O controller space,
  255. address matching DABR, eciwx, etc. */
  256. goto bad_area;
  257. #endif /* CONFIG_6xx */
  258. #if defined(CONFIG_8xx)
  259. /* The MPC8xx seems to always set 0x80000000, which is
  260. * "undefined". Of those that can be set, this is the only
  261. * one which seems bad.
  262. */
  263. if (error_code & 0x10000000)
  264. /* Guarded storage error. */
  265. goto bad_area;
  266. #endif /* CONFIG_8xx */
  267. if (is_exec) {
  268. #ifdef CONFIG_PPC64
  269. /* protection fault */
  270. if (error_code & DSISR_PROTFAULT)
  271. goto bad_area;
  272. if (!(vma->vm_flags & VM_EXEC))
  273. goto bad_area;
  274. #endif
  275. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  276. pte_t *ptep;
  277. pmd_t *pmdp;
  278. /* Since 4xx/Book-E supports per-page execute permission,
  279. * we lazily flush dcache to icache. */
  280. ptep = NULL;
  281. if (get_pteptr(mm, address, &ptep, &pmdp)) {
  282. spinlock_t *ptl = pte_lockptr(mm, pmdp);
  283. spin_lock(ptl);
  284. if (pte_present(*ptep)) {
  285. struct page *page = pte_page(*ptep);
  286. if (!test_bit(PG_arch_1, &page->flags)) {
  287. flush_dcache_icache_page(page);
  288. set_bit(PG_arch_1, &page->flags);
  289. }
  290. pte_update(ptep, 0, _PAGE_HWEXEC);
  291. _tlbie(address);
  292. pte_unmap_unlock(ptep, ptl);
  293. up_read(&mm->mmap_sem);
  294. return 0;
  295. }
  296. pte_unmap_unlock(ptep, ptl);
  297. }
  298. #endif
  299. /* a write */
  300. } else if (is_write) {
  301. if (!(vma->vm_flags & VM_WRITE))
  302. goto bad_area;
  303. /* a read */
  304. } else {
  305. /* protection fault */
  306. if (error_code & 0x08000000)
  307. goto bad_area;
  308. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  309. goto bad_area;
  310. }
  311. /*
  312. * If for any reason at all we couldn't handle the fault,
  313. * make sure we exit gracefully rather than endlessly redo
  314. * the fault.
  315. */
  316. survive:
  317. switch (handle_mm_fault(mm, vma, address, is_write)) {
  318. case VM_FAULT_MINOR:
  319. current->min_flt++;
  320. break;
  321. case VM_FAULT_MAJOR:
  322. current->maj_flt++;
  323. break;
  324. case VM_FAULT_SIGBUS:
  325. goto do_sigbus;
  326. case VM_FAULT_OOM:
  327. goto out_of_memory;
  328. default:
  329. BUG();
  330. }
  331. up_read(&mm->mmap_sem);
  332. return 0;
  333. bad_area:
  334. up_read(&mm->mmap_sem);
  335. bad_area_nosemaphore:
  336. /* User mode accesses cause a SIGSEGV */
  337. if (user_mode(regs)) {
  338. _exception(SIGSEGV, regs, code, address);
  339. return 0;
  340. }
  341. if (is_exec && (error_code & DSISR_PROTFAULT)
  342. && printk_ratelimit())
  343. printk(KERN_CRIT "kernel tried to execute NX-protected"
  344. " page (%lx) - exploit attempt? (uid: %d)\n",
  345. address, current->uid);
  346. return SIGSEGV;
  347. /*
  348. * We ran out of memory, or some other thing happened to us that made
  349. * us unable to handle the page fault gracefully.
  350. */
  351. out_of_memory:
  352. up_read(&mm->mmap_sem);
  353. if (current->pid == 1) {
  354. yield();
  355. down_read(&mm->mmap_sem);
  356. goto survive;
  357. }
  358. printk("VM: killing process %s\n", current->comm);
  359. if (user_mode(regs))
  360. do_exit(SIGKILL);
  361. return SIGKILL;
  362. do_sigbus:
  363. up_read(&mm->mmap_sem);
  364. if (user_mode(regs)) {
  365. info.si_signo = SIGBUS;
  366. info.si_errno = 0;
  367. info.si_code = BUS_ADRERR;
  368. info.si_addr = (void __user *)address;
  369. force_sig_info(SIGBUS, &info, current);
  370. return 0;
  371. }
  372. return SIGBUS;
  373. }
  374. /*
  375. * bad_page_fault is called when we have a bad access from the kernel.
  376. * It is called from the DSI and ISI handlers in head.S and from some
  377. * of the procedures in traps.c.
  378. */
  379. void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
  380. {
  381. const struct exception_table_entry *entry;
  382. /* Are we prepared to handle this fault? */
  383. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  384. regs->nip = entry->fixup;
  385. return;
  386. }
  387. /* kernel has accessed a bad area */
  388. printk(KERN_ALERT "Unable to handle kernel paging request for ");
  389. switch (regs->trap) {
  390. case 0x300:
  391. case 0x380:
  392. printk("data at address 0x%08lx\n", regs->dar);
  393. break;
  394. case 0x400:
  395. case 0x480:
  396. printk("instruction fetch\n");
  397. break;
  398. default:
  399. printk("unknown fault\n");
  400. }
  401. printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n",
  402. regs->nip);
  403. die("Kernel access of bad area", regs, sig);
  404. }