fault.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * OpenRISC fault.c
  3. *
  4. * Linux architectural port borrowing liberally from similar works of
  5. * others. All original copyrights apply as per the original source
  6. * declaration.
  7. *
  8. * Modifications for the OpenRISC architecture:
  9. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  10. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  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/mm.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/extable.h>
  20. #include <linux/sched/signal.h>
  21. #include <linux/uaccess.h>
  22. #include <asm/siginfo.h>
  23. #include <asm/signal.h>
  24. #define NUM_TLB_ENTRIES 64
  25. #define TLB_OFFSET(add) (((add) >> PAGE_SHIFT) & (NUM_TLB_ENTRIES-1))
  26. unsigned long pte_misses; /* updated by do_page_fault() */
  27. unsigned long pte_errors; /* updated by do_page_fault() */
  28. /* __PHX__ :: - check the vmalloc_fault in do_page_fault()
  29. * - also look into include/asm-or32/mmu_context.h
  30. */
  31. volatile pgd_t *current_pgd[NR_CPUS];
  32. extern void die(char *, struct pt_regs *, long);
  33. /*
  34. * This routine handles page faults. It determines the address,
  35. * and the problem, and then passes it off to one of the appropriate
  36. * routines.
  37. *
  38. * If this routine detects a bad access, it returns 1, otherwise it
  39. * returns 0.
  40. */
  41. asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address,
  42. unsigned long vector, int write_acc)
  43. {
  44. struct task_struct *tsk;
  45. struct mm_struct *mm;
  46. struct vm_area_struct *vma;
  47. int si_code;
  48. vm_fault_t fault;
  49. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  50. tsk = current;
  51. /*
  52. * We fault-in kernel-space virtual memory on-demand. The
  53. * 'reference' page table is init_mm.pgd.
  54. *
  55. * NOTE! We MUST NOT take any locks for this case. We may
  56. * be in an interrupt or a critical region, and should
  57. * only copy the information from the master page table,
  58. * nothing more.
  59. *
  60. * NOTE2: This is done so that, when updating the vmalloc
  61. * mappings we don't have to walk all processes pgdirs and
  62. * add the high mappings all at once. Instead we do it as they
  63. * are used. However vmalloc'ed page entries have the PAGE_GLOBAL
  64. * bit set so sometimes the TLB can use a lingering entry.
  65. *
  66. * This verifies that the fault happens in kernel space
  67. * and that the fault was not a protection error.
  68. */
  69. if (address >= VMALLOC_START &&
  70. (vector != 0x300 && vector != 0x400) &&
  71. !user_mode(regs))
  72. goto vmalloc_fault;
  73. /* If exceptions were enabled, we can reenable them here */
  74. if (user_mode(regs)) {
  75. /* Exception was in userspace: reenable interrupts */
  76. local_irq_enable();
  77. flags |= FAULT_FLAG_USER;
  78. } else {
  79. /* If exception was in a syscall, then IRQ's may have
  80. * been enabled or disabled. If they were enabled,
  81. * reenable them.
  82. */
  83. if (regs->sr && (SPR_SR_IEE | SPR_SR_TEE))
  84. local_irq_enable();
  85. }
  86. mm = tsk->mm;
  87. si_code = SEGV_MAPERR;
  88. /*
  89. * If we're in an interrupt or have no user
  90. * context, we must not take the fault..
  91. */
  92. if (in_interrupt() || !mm)
  93. goto no_context;
  94. retry:
  95. down_read(&mm->mmap_sem);
  96. vma = find_vma(mm, address);
  97. if (!vma)
  98. goto bad_area;
  99. if (vma->vm_start <= address)
  100. goto good_area;
  101. if (!(vma->vm_flags & VM_GROWSDOWN))
  102. goto bad_area;
  103. if (user_mode(regs)) {
  104. /*
  105. * accessing the stack below usp is always a bug.
  106. * we get page-aligned addresses so we can only check
  107. * if we're within a page from usp, but that might be
  108. * enough to catch brutal errors at least.
  109. */
  110. if (address + PAGE_SIZE < regs->sp)
  111. goto bad_area;
  112. }
  113. if (expand_stack(vma, address))
  114. goto bad_area;
  115. /*
  116. * Ok, we have a good vm_area for this memory access, so
  117. * we can handle it..
  118. */
  119. good_area:
  120. si_code = SEGV_ACCERR;
  121. /* first do some preliminary protection checks */
  122. if (write_acc) {
  123. if (!(vma->vm_flags & VM_WRITE))
  124. goto bad_area;
  125. flags |= FAULT_FLAG_WRITE;
  126. } else {
  127. /* not present */
  128. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  129. goto bad_area;
  130. }
  131. /* are we trying to execute nonexecutable area */
  132. if ((vector == 0x400) && !(vma->vm_page_prot.pgprot & _PAGE_EXEC))
  133. goto bad_area;
  134. /*
  135. * If for any reason at all we couldn't handle the fault,
  136. * make sure we exit gracefully rather than endlessly redo
  137. * the fault.
  138. */
  139. fault = handle_mm_fault(vma, address, flags);
  140. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  141. return;
  142. if (unlikely(fault & VM_FAULT_ERROR)) {
  143. if (fault & VM_FAULT_OOM)
  144. goto out_of_memory;
  145. else if (fault & VM_FAULT_SIGSEGV)
  146. goto bad_area;
  147. else if (fault & VM_FAULT_SIGBUS)
  148. goto do_sigbus;
  149. BUG();
  150. }
  151. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  152. /*RGD modeled on Cris */
  153. if (fault & VM_FAULT_MAJOR)
  154. tsk->maj_flt++;
  155. else
  156. tsk->min_flt++;
  157. if (fault & VM_FAULT_RETRY) {
  158. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  159. flags |= FAULT_FLAG_TRIED;
  160. /* No need to up_read(&mm->mmap_sem) as we would
  161. * have already released it in __lock_page_or_retry
  162. * in mm/filemap.c.
  163. */
  164. goto retry;
  165. }
  166. }
  167. up_read(&mm->mmap_sem);
  168. return;
  169. /*
  170. * Something tried to access memory that isn't in our memory map..
  171. * Fix it, but check if it's kernel or user first..
  172. */
  173. bad_area:
  174. up_read(&mm->mmap_sem);
  175. bad_area_nosemaphore:
  176. /* User mode accesses just cause a SIGSEGV */
  177. if (user_mode(regs)) {
  178. force_sig_fault(SIGSEGV, si_code, (void __user *)address, tsk);
  179. return;
  180. }
  181. no_context:
  182. /* Are we prepared to handle this kernel fault?
  183. *
  184. * (The kernel has valid exception-points in the source
  185. * when it acesses user-memory. When it fails in one
  186. * of those points, we find it in a table and do a jump
  187. * to some fixup code that loads an appropriate error
  188. * code)
  189. */
  190. {
  191. const struct exception_table_entry *entry;
  192. __asm__ __volatile__("l.nop 42");
  193. if ((entry = search_exception_tables(regs->pc)) != NULL) {
  194. /* Adjust the instruction pointer in the stackframe */
  195. regs->pc = entry->fixup;
  196. return;
  197. }
  198. }
  199. /*
  200. * Oops. The kernel tried to access some bad page. We'll have to
  201. * terminate things with extreme prejudice.
  202. */
  203. if ((unsigned long)(address) < PAGE_SIZE)
  204. printk(KERN_ALERT
  205. "Unable to handle kernel NULL pointer dereference");
  206. else
  207. printk(KERN_ALERT "Unable to handle kernel access");
  208. printk(" at virtual address 0x%08lx\n", address);
  209. die("Oops", regs, write_acc);
  210. do_exit(SIGKILL);
  211. /*
  212. * We ran out of memory, or some other thing happened to us that made
  213. * us unable to handle the page fault gracefully.
  214. */
  215. out_of_memory:
  216. __asm__ __volatile__("l.nop 42");
  217. __asm__ __volatile__("l.nop 1");
  218. up_read(&mm->mmap_sem);
  219. if (!user_mode(regs))
  220. goto no_context;
  221. pagefault_out_of_memory();
  222. return;
  223. do_sigbus:
  224. up_read(&mm->mmap_sem);
  225. /*
  226. * Send a sigbus, regardless of whether we were in kernel
  227. * or user mode.
  228. */
  229. force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address, tsk);
  230. /* Kernel mode? Handle exceptions or die */
  231. if (!user_mode(regs))
  232. goto no_context;
  233. return;
  234. vmalloc_fault:
  235. {
  236. /*
  237. * Synchronize this task's top level page-table
  238. * with the 'reference' page table.
  239. *
  240. * Use current_pgd instead of tsk->active_mm->pgd
  241. * since the latter might be unavailable if this
  242. * code is executed in a misfortunately run irq
  243. * (like inside schedule() between switch_mm and
  244. * switch_to...).
  245. */
  246. int offset = pgd_index(address);
  247. pgd_t *pgd, *pgd_k;
  248. pud_t *pud, *pud_k;
  249. pmd_t *pmd, *pmd_k;
  250. pte_t *pte_k;
  251. /*
  252. phx_warn("do_page_fault(): vmalloc_fault will not work, "
  253. "since current_pgd assign a proper value somewhere\n"
  254. "anyhow we don't need this at the moment\n");
  255. phx_mmu("vmalloc_fault");
  256. */
  257. pgd = (pgd_t *)current_pgd[smp_processor_id()] + offset;
  258. pgd_k = init_mm.pgd + offset;
  259. /* Since we're two-level, we don't need to do both
  260. * set_pgd and set_pmd (they do the same thing). If
  261. * we go three-level at some point, do the right thing
  262. * with pgd_present and set_pgd here.
  263. *
  264. * Also, since the vmalloc area is global, we don't
  265. * need to copy individual PTE's, it is enough to
  266. * copy the pgd pointer into the pte page of the
  267. * root task. If that is there, we'll find our pte if
  268. * it exists.
  269. */
  270. pud = pud_offset(pgd, address);
  271. pud_k = pud_offset(pgd_k, address);
  272. if (!pud_present(*pud_k))
  273. goto no_context;
  274. pmd = pmd_offset(pud, address);
  275. pmd_k = pmd_offset(pud_k, address);
  276. if (!pmd_present(*pmd_k))
  277. goto bad_area_nosemaphore;
  278. set_pmd(pmd, *pmd_k);
  279. /* Make sure the actual PTE exists as well to
  280. * catch kernel vmalloc-area accesses to non-mapped
  281. * addresses. If we don't do this, this will just
  282. * silently loop forever.
  283. */
  284. pte_k = pte_offset_kernel(pmd_k, address);
  285. if (!pte_present(*pte_k))
  286. goto no_context;
  287. return;
  288. }
  289. }