fault.c 8.4 KB

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