fault.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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/module.h>
  20. #include <linux/sched.h>
  21. #include <asm/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;
  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. siginfo_t info;
  48. int fault;
  49. tsk = current;
  50. /*
  51. * We fault-in kernel-space virtual memory on-demand. The
  52. * 'reference' page table is init_mm.pgd.
  53. *
  54. * NOTE! We MUST NOT take any locks for this case. We may
  55. * be in an interrupt or a critical region, and should
  56. * only copy the information from the master page table,
  57. * nothing more.
  58. *
  59. * NOTE2: This is done so that, when updating the vmalloc
  60. * mappings we don't have to walk all processes pgdirs and
  61. * add the high mappings all at once. Instead we do it as they
  62. * are used. However vmalloc'ed page entries have the PAGE_GLOBAL
  63. * bit set so sometimes the TLB can use a lingering entry.
  64. *
  65. * This verifies that the fault happens in kernel space
  66. * and that the fault was not a protection error.
  67. */
  68. if (address >= VMALLOC_START &&
  69. (vector != 0x300 && vector != 0x400) &&
  70. !user_mode(regs))
  71. goto vmalloc_fault;
  72. /* If exceptions were enabled, we can reenable them here */
  73. if (user_mode(regs)) {
  74. /* Exception was in userspace: reenable interrupts */
  75. local_irq_enable();
  76. } else {
  77. /* If exception was in a syscall, then IRQ's may have
  78. * been enabled or disabled. If they were enabled,
  79. * reenable them.
  80. */
  81. if (regs->sr && (SPR_SR_IEE | SPR_SR_TEE))
  82. local_irq_enable();
  83. }
  84. mm = tsk->mm;
  85. info.si_code = SEGV_MAPERR;
  86. /*
  87. * If we're in an interrupt or have no user
  88. * context, we must not take the fault..
  89. */
  90. if (in_interrupt() || !mm)
  91. goto no_context;
  92. down_read(&mm->mmap_sem);
  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. if (expand_stack(vma, address))
  111. goto bad_area;
  112. /*
  113. * Ok, we have a good vm_area for this memory access, so
  114. * we can handle it..
  115. */
  116. good_area:
  117. info.si_code = SEGV_ACCERR;
  118. /* first do some preliminary protection checks */
  119. if (write_acc) {
  120. if (!(vma->vm_flags & VM_WRITE))
  121. goto bad_area;
  122. } else {
  123. /* not present */
  124. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  125. goto bad_area;
  126. }
  127. /* are we trying to execute nonexecutable area */
  128. if ((vector == 0x400) && !(vma->vm_page_prot.pgprot & _PAGE_EXEC))
  129. goto bad_area;
  130. /*
  131. * If for any reason at all we couldn't handle the fault,
  132. * make sure we exit gracefully rather than endlessly redo
  133. * the fault.
  134. */
  135. fault = handle_mm_fault(mm, vma, address, write_acc);
  136. if (unlikely(fault & VM_FAULT_ERROR)) {
  137. if (fault & VM_FAULT_OOM)
  138. goto out_of_memory;
  139. else if (fault & VM_FAULT_SIGBUS)
  140. goto do_sigbus;
  141. BUG();
  142. }
  143. /*RGD modeled on Cris */
  144. if (fault & VM_FAULT_MAJOR)
  145. tsk->maj_flt++;
  146. else
  147. tsk->min_flt++;
  148. up_read(&mm->mmap_sem);
  149. return;
  150. /*
  151. * Something tried to access memory that isn't in our memory map..
  152. * Fix it, but check if it's kernel or user first..
  153. */
  154. bad_area:
  155. up_read(&mm->mmap_sem);
  156. bad_area_nosemaphore:
  157. /* User mode accesses just cause a SIGSEGV */
  158. if (user_mode(regs)) {
  159. info.si_signo = SIGSEGV;
  160. info.si_errno = 0;
  161. /* info.si_code has been set above */
  162. info.si_addr = (void *)address;
  163. force_sig_info(SIGSEGV, &info, tsk);
  164. return;
  165. }
  166. no_context:
  167. /* Are we prepared to handle this kernel fault?
  168. *
  169. * (The kernel has valid exception-points in the source
  170. * when it acesses user-memory. When it fails in one
  171. * of those points, we find it in a table and do a jump
  172. * to some fixup code that loads an appropriate error
  173. * code)
  174. */
  175. {
  176. const struct exception_table_entry *entry;
  177. __asm__ __volatile__("l.nop 42");
  178. if ((entry = search_exception_tables(regs->pc)) != NULL) {
  179. /* Adjust the instruction pointer in the stackframe */
  180. regs->pc = entry->fixup;
  181. return;
  182. }
  183. }
  184. /*
  185. * Oops. The kernel tried to access some bad page. We'll have to
  186. * terminate things with extreme prejudice.
  187. */
  188. if ((unsigned long)(address) < PAGE_SIZE)
  189. printk(KERN_ALERT
  190. "Unable to handle kernel NULL pointer dereference");
  191. else
  192. printk(KERN_ALERT "Unable to handle kernel access");
  193. printk(" at virtual address 0x%08lx\n", address);
  194. die("Oops", regs, write_acc);
  195. do_exit(SIGKILL);
  196. /*
  197. * We ran out of memory, or some other thing happened to us that made
  198. * us unable to handle the page fault gracefully.
  199. */
  200. out_of_memory:
  201. __asm__ __volatile__("l.nop 42");
  202. __asm__ __volatile__("l.nop 1");
  203. up_read(&mm->mmap_sem);
  204. printk("VM: killing process %s\n", tsk->comm);
  205. if (user_mode(regs))
  206. do_exit(SIGKILL);
  207. goto no_context;
  208. do_sigbus:
  209. up_read(&mm->mmap_sem);
  210. /*
  211. * Send a sigbus, regardless of whether we were in kernel
  212. * or user mode.
  213. */
  214. info.si_signo = SIGBUS;
  215. info.si_errno = 0;
  216. info.si_code = BUS_ADRERR;
  217. info.si_addr = (void *)address;
  218. force_sig_info(SIGBUS, &info, tsk);
  219. /* Kernel mode? Handle exceptions or die */
  220. if (!user_mode(regs))
  221. goto no_context;
  222. return;
  223. vmalloc_fault:
  224. {
  225. /*
  226. * Synchronize this task's top level page-table
  227. * with the 'reference' page table.
  228. *
  229. * Use current_pgd instead of tsk->active_mm->pgd
  230. * since the latter might be unavailable if this
  231. * code is executed in a misfortunately run irq
  232. * (like inside schedule() between switch_mm and
  233. * switch_to...).
  234. */
  235. int offset = pgd_index(address);
  236. pgd_t *pgd, *pgd_k;
  237. pud_t *pud, *pud_k;
  238. pmd_t *pmd, *pmd_k;
  239. pte_t *pte_k;
  240. /*
  241. phx_warn("do_page_fault(): vmalloc_fault will not work, "
  242. "since current_pgd assign a proper value somewhere\n"
  243. "anyhow we don't need this at the moment\n");
  244. phx_mmu("vmalloc_fault");
  245. */
  246. pgd = (pgd_t *)current_pgd + offset;
  247. pgd_k = init_mm.pgd + offset;
  248. /* Since we're two-level, we don't need to do both
  249. * set_pgd and set_pmd (they do the same thing). If
  250. * we go three-level at some point, do the right thing
  251. * with pgd_present and set_pgd here.
  252. *
  253. * Also, since the vmalloc area is global, we don't
  254. * need to copy individual PTE's, it is enough to
  255. * copy the pgd pointer into the pte page of the
  256. * root task. If that is there, we'll find our pte if
  257. * it exists.
  258. */
  259. pud = pud_offset(pgd, address);
  260. pud_k = pud_offset(pgd_k, address);
  261. if (!pud_present(*pud_k))
  262. goto no_context;
  263. pmd = pmd_offset(pud, address);
  264. pmd_k = pmd_offset(pud_k, address);
  265. if (!pmd_present(*pmd_k))
  266. goto bad_area_nosemaphore;
  267. set_pmd(pmd, *pmd_k);
  268. /* Make sure the actual PTE exists as well to
  269. * catch kernel vmalloc-area accesses to non-mapped
  270. * addresses. If we don't do this, this will just
  271. * silently loop forever.
  272. */
  273. pte_k = pte_offset_kernel(pmd_k, address);
  274. if (!pte_present(*pte_k))
  275. goto no_context;
  276. return;
  277. }
  278. }