fault.c 8.6 KB

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