fault.c 12 KB

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