fault.c 12 KB

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