fault.c 11 KB

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