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. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/config.h>
  16. #include <linux/signal.h>
  17. #include <linux/sched.h>
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/string.h>
  21. #include <linux/types.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/mman.h>
  24. #include <linux/mm.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/highmem.h>
  27. #include <linux/module.h>
  28. #include <asm/page.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/mmu.h>
  31. #include <asm/mmu_context.h>
  32. #include <asm/system.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/tlbflush.h>
  35. #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
  36. extern void (*debugger)(struct pt_regs *);
  37. extern void (*debugger_fault_handler)(struct pt_regs *);
  38. extern int (*debugger_dabr_match)(struct pt_regs *);
  39. int debugger_kernel_faults = 1;
  40. #endif
  41. unsigned long htab_reloads; /* updated by hashtable.S:hash_page() */
  42. unsigned long htab_evicts; /* updated by hashtable.S:hash_page() */
  43. unsigned long htab_preloads; /* updated by hashtable.S:add_hash_page() */
  44. unsigned long pte_misses; /* updated by do_page_fault() */
  45. unsigned long pte_errors; /* updated by do_page_fault() */
  46. unsigned int probingmem;
  47. /*
  48. * Check whether the instruction at regs->nip is a store using
  49. * an update addressing form which will update r1.
  50. */
  51. static int store_updates_sp(struct pt_regs *regs)
  52. {
  53. unsigned int inst;
  54. if (get_user(inst, (unsigned int __user *)regs->nip))
  55. return 0;
  56. /* check for 1 in the rA field */
  57. if (((inst >> 16) & 0x1f) != 1)
  58. return 0;
  59. /* check major opcode */
  60. switch (inst >> 26) {
  61. case 37: /* stwu */
  62. case 39: /* stbu */
  63. case 45: /* sthu */
  64. case 53: /* stfsu */
  65. case 55: /* stfdu */
  66. return 1;
  67. case 31:
  68. /* check minor opcode */
  69. switch ((inst >> 1) & 0x3ff) {
  70. case 183: /* stwux */
  71. case 247: /* stbux */
  72. case 439: /* sthux */
  73. case 695: /* stfsux */
  74. case 759: /* stfdux */
  75. return 1;
  76. }
  77. }
  78. return 0;
  79. }
  80. /*
  81. * For 600- and 800-family processors, the error_code parameter is DSISR
  82. * for a data fault, SRR1 for an instruction fault. For 400-family processors
  83. * the error_code parameter is ESR for a data fault, 0 for an instruction
  84. * fault.
  85. */
  86. int do_page_fault(struct pt_regs *regs, unsigned long address,
  87. unsigned long error_code)
  88. {
  89. struct vm_area_struct * vma;
  90. struct mm_struct *mm = current->mm;
  91. siginfo_t info;
  92. int code = SEGV_MAPERR;
  93. #if defined(CONFIG_4xx) || defined (CONFIG_BOOKE)
  94. int is_write = error_code & ESR_DST;
  95. #else
  96. int is_write = 0;
  97. /*
  98. * Fortunately the bit assignments in SRR1 for an instruction
  99. * fault and DSISR for a data fault are mostly the same for the
  100. * bits we are interested in. But there are some bits which
  101. * indicate errors in DSISR but can validly be set in SRR1.
  102. */
  103. if (TRAP(regs) == 0x400)
  104. error_code &= 0x48200000;
  105. else
  106. is_write = error_code & 0x02000000;
  107. #endif /* CONFIG_4xx || CONFIG_BOOKE */
  108. #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
  109. if (debugger_fault_handler && TRAP(regs) == 0x300) {
  110. debugger_fault_handler(regs);
  111. return 0;
  112. }
  113. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  114. if (error_code & 0x00400000) {
  115. /* DABR match */
  116. if (debugger_dabr_match(regs))
  117. return 0;
  118. }
  119. #endif /* !(CONFIG_4xx || CONFIG_BOOKE)*/
  120. #endif /* CONFIG_XMON || CONFIG_KGDB */
  121. if (in_atomic() || mm == NULL)
  122. return SIGSEGV;
  123. down_read(&mm->mmap_sem);
  124. vma = find_vma(mm, address);
  125. if (!vma)
  126. goto bad_area;
  127. if (vma->vm_start <= address)
  128. goto good_area;
  129. if (!(vma->vm_flags & VM_GROWSDOWN))
  130. goto bad_area;
  131. if (!is_write)
  132. goto bad_area;
  133. /*
  134. * N.B. The rs6000/xcoff ABI allows programs to access up to
  135. * a few hundred bytes below the stack pointer.
  136. * The kernel signal delivery code writes up to about 1.5kB
  137. * below the stack pointer (r1) before decrementing it.
  138. * The exec code can write slightly over 640kB to the stack
  139. * before setting the user r1. Thus we allow the stack to
  140. * expand to 1MB without further checks.
  141. */
  142. if (address + 0x100000 < vma->vm_end) {
  143. /* get user regs even if this fault is in kernel mode */
  144. struct pt_regs *uregs = current->thread.regs;
  145. if (uregs == NULL)
  146. goto bad_area;
  147. /*
  148. * A user-mode access to an address a long way below
  149. * the stack pointer is only valid if the instruction
  150. * is one which would update the stack pointer to the
  151. * address accessed if the instruction completed,
  152. * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
  153. * (or the byte, halfword, float or double forms).
  154. *
  155. * If we don't check this then any write to the area
  156. * between the last mapped region and the stack will
  157. * expand the stack rather than segfaulting.
  158. */
  159. if (address + 2048 < uregs->gpr[1]
  160. && (!user_mode(regs) || !store_updates_sp(regs)))
  161. goto bad_area;
  162. }
  163. if (expand_stack(vma, address))
  164. goto bad_area;
  165. good_area:
  166. code = SEGV_ACCERR;
  167. #if defined(CONFIG_6xx)
  168. if (error_code & 0x95700000)
  169. /* an error such as lwarx to I/O controller space,
  170. address matching DABR, eciwx, etc. */
  171. goto bad_area;
  172. #endif /* CONFIG_6xx */
  173. #if defined(CONFIG_8xx)
  174. /* The MPC8xx seems to always set 0x80000000, which is
  175. * "undefined". Of those that can be set, this is the only
  176. * one which seems bad.
  177. */
  178. if (error_code & 0x10000000)
  179. /* Guarded storage error. */
  180. goto bad_area;
  181. #endif /* CONFIG_8xx */
  182. /* a write */
  183. if (is_write) {
  184. if (!(vma->vm_flags & VM_WRITE))
  185. goto bad_area;
  186. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  187. /* an exec - 4xx/Book-E allows for per-page execute permission */
  188. } else if (TRAP(regs) == 0x400) {
  189. pte_t *ptep;
  190. pmd_t *pmdp;
  191. #if 0
  192. /* It would be nice to actually enforce the VM execute
  193. permission on CPUs which can do so, but far too
  194. much stuff in userspace doesn't get the permissions
  195. right, so we let any page be executed for now. */
  196. if (! (vma->vm_flags & VM_EXEC))
  197. goto bad_area;
  198. #endif
  199. /* Since 4xx/Book-E supports per-page execute permission,
  200. * we lazily flush dcache to icache. */
  201. ptep = NULL;
  202. if (get_pteptr(mm, address, &ptep, &pmdp)) {
  203. spinlock_t *ptl = pte_lockptr(mm, pmdp);
  204. spin_lock(ptl);
  205. if (pte_present(*ptep)) {
  206. struct page *page = pte_page(*ptep);
  207. if (!test_bit(PG_arch_1, &page->flags)) {
  208. flush_dcache_icache_page(page);
  209. set_bit(PG_arch_1, &page->flags);
  210. }
  211. pte_update(ptep, 0, _PAGE_HWEXEC);
  212. _tlbie(address);
  213. pte_unmap_unlock(ptep, ptl);
  214. up_read(&mm->mmap_sem);
  215. return 0;
  216. }
  217. pte_unmap_unlock(ptep, ptl);
  218. }
  219. #endif
  220. /* a read */
  221. } else {
  222. /* protection fault */
  223. if (error_code & 0x08000000)
  224. goto bad_area;
  225. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  226. goto bad_area;
  227. }
  228. /*
  229. * If for any reason at all we couldn't handle the fault,
  230. * make sure we exit gracefully rather than endlessly redo
  231. * the fault.
  232. */
  233. survive:
  234. switch (handle_mm_fault(mm, vma, address, is_write)) {
  235. case VM_FAULT_MINOR:
  236. current->min_flt++;
  237. break;
  238. case VM_FAULT_MAJOR:
  239. current->maj_flt++;
  240. break;
  241. case VM_FAULT_SIGBUS:
  242. goto do_sigbus;
  243. case VM_FAULT_OOM:
  244. goto out_of_memory;
  245. default:
  246. BUG();
  247. }
  248. up_read(&mm->mmap_sem);
  249. /*
  250. * keep track of tlb+htab misses that are good addrs but
  251. * just need pte's created via handle_mm_fault()
  252. * -- Cort
  253. */
  254. pte_misses++;
  255. return 0;
  256. bad_area:
  257. up_read(&mm->mmap_sem);
  258. pte_errors++;
  259. /* User mode accesses cause a SIGSEGV */
  260. if (user_mode(regs)) {
  261. _exception(SIGSEGV, regs, code, address);
  262. return 0;
  263. }
  264. return SIGSEGV;
  265. /*
  266. * We ran out of memory, or some other thing happened to us that made
  267. * us unable to handle the page fault gracefully.
  268. */
  269. out_of_memory:
  270. up_read(&mm->mmap_sem);
  271. if (current->pid == 1) {
  272. yield();
  273. down_read(&mm->mmap_sem);
  274. goto survive;
  275. }
  276. printk("VM: killing process %s\n", current->comm);
  277. if (user_mode(regs))
  278. do_exit(SIGKILL);
  279. return SIGKILL;
  280. do_sigbus:
  281. up_read(&mm->mmap_sem);
  282. info.si_signo = SIGBUS;
  283. info.si_errno = 0;
  284. info.si_code = BUS_ADRERR;
  285. info.si_addr = (void __user *)address;
  286. force_sig_info (SIGBUS, &info, current);
  287. if (!user_mode(regs))
  288. return SIGBUS;
  289. return 0;
  290. }
  291. /*
  292. * bad_page_fault is called when we have a bad access from the kernel.
  293. * It is called from the DSI and ISI handlers in head.S and from some
  294. * of the procedures in traps.c.
  295. */
  296. void
  297. bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
  298. {
  299. const struct exception_table_entry *entry;
  300. /* Are we prepared to handle this fault? */
  301. if ((entry = search_exception_tables(regs->nip)) != NULL) {
  302. regs->nip = entry->fixup;
  303. return;
  304. }
  305. /* kernel has accessed a bad area */
  306. #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
  307. if (debugger_kernel_faults)
  308. debugger(regs);
  309. #endif
  310. die("kernel access of bad area", regs, sig);
  311. }
  312. #ifdef CONFIG_8xx
  313. /* The pgtable.h claims some functions generically exist, but I
  314. * can't find them......
  315. */
  316. pte_t *va_to_pte(unsigned long address)
  317. {
  318. pgd_t *dir;
  319. pmd_t *pmd;
  320. pte_t *pte;
  321. if (address < TASK_SIZE)
  322. return NULL;
  323. dir = pgd_offset(&init_mm, address);
  324. if (dir) {
  325. pmd = pmd_offset(dir, address & PAGE_MASK);
  326. if (pmd && pmd_present(*pmd)) {
  327. pte = pte_offset_kernel(pmd, address & PAGE_MASK);
  328. if (pte && pte_present(*pte))
  329. return(pte);
  330. }
  331. }
  332. return NULL;
  333. }
  334. unsigned long va_to_phys(unsigned long address)
  335. {
  336. pte_t *pte;
  337. pte = va_to_pte(address);
  338. if (pte)
  339. return(((unsigned long)(pte_val(*pte)) & PAGE_MASK) | (address & ~(PAGE_MASK)));
  340. return (0);
  341. }
  342. void
  343. print_8xx_pte(struct mm_struct *mm, unsigned long addr)
  344. {
  345. pgd_t * pgd;
  346. pmd_t * pmd;
  347. pte_t * pte;
  348. printk(" pte @ 0x%8lx: ", addr);
  349. pgd = pgd_offset(mm, addr & PAGE_MASK);
  350. if (pgd) {
  351. pmd = pmd_offset(pgd, addr & PAGE_MASK);
  352. if (pmd && pmd_present(*pmd)) {
  353. pte = pte_offset_kernel(pmd, addr & PAGE_MASK);
  354. if (pte) {
  355. printk(" (0x%08lx)->(0x%08lx)->0x%08lx\n",
  356. (long)pgd, (long)pte, (long)pte_val(*pte));
  357. #define pp ((long)pte_val(*pte))
  358. printk(" RPN: %05lx PP: %lx SPS: %lx SH: %lx "
  359. "CI: %lx v: %lx\n",
  360. pp>>12, /* rpn */
  361. (pp>>10)&3, /* pp */
  362. (pp>>3)&1, /* small */
  363. (pp>>2)&1, /* shared */
  364. (pp>>1)&1, /* cache inhibit */
  365. pp&1 /* valid */
  366. );
  367. #undef pp
  368. }
  369. else {
  370. printk("no pte\n");
  371. }
  372. }
  373. else {
  374. printk("no pmd\n");
  375. }
  376. }
  377. else {
  378. printk("no pgd\n");
  379. }
  380. }
  381. int
  382. get_8xx_pte(struct mm_struct *mm, unsigned long addr)
  383. {
  384. pgd_t * pgd;
  385. pmd_t * pmd;
  386. pte_t * pte;
  387. int retval = 0;
  388. pgd = pgd_offset(mm, addr & PAGE_MASK);
  389. if (pgd) {
  390. pmd = pmd_offset(pgd, addr & PAGE_MASK);
  391. if (pmd && pmd_present(*pmd)) {
  392. pte = pte_offset_kernel(pmd, addr & PAGE_MASK);
  393. if (pte) {
  394. retval = (int)pte_val(*pte);
  395. }
  396. }
  397. }
  398. return(retval);
  399. }
  400. #endif /* CONFIG_8xx */