fault.c 11 KB

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