fault.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * linux/arch/cris/mm/fault.c
  3. *
  4. * Copyright (C) 2000-2006 Axis Communications AB
  5. *
  6. * Authors: Bjorn Wesen
  7. *
  8. */
  9. #include <linux/mm.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/module.h>
  12. #include <asm/uaccess.h>
  13. extern int find_fixup_code(struct pt_regs *);
  14. extern void die_if_kernel(const char *, struct pt_regs *, long);
  15. /* debug of low-level TLB reload */
  16. #undef DEBUG
  17. #ifdef DEBUG
  18. #define D(x) x
  19. #else
  20. #define D(x)
  21. #endif
  22. /* debug of higher-level faults */
  23. #define DPG(x)
  24. /* current active page directory */
  25. DEFINE_PER_CPU(pgd_t *, current_pgd);
  26. unsigned long cris_signal_return_page;
  27. /*
  28. * This routine handles page faults. It determines the address,
  29. * and the problem, and then passes it off to one of the appropriate
  30. * routines.
  31. *
  32. * Notice that the address we're given is aligned to the page the fault
  33. * occurred in, since we only get the PFN in R_MMU_CAUSE not the complete
  34. * address.
  35. *
  36. * error_code:
  37. * bit 0 == 0 means no page found, 1 means protection fault
  38. * bit 1 == 0 means read, 1 means write
  39. *
  40. * If this routine detects a bad access, it returns 1, otherwise it
  41. * returns 0.
  42. */
  43. asmlinkage void
  44. do_page_fault(unsigned long address, struct pt_regs *regs,
  45. int protection, int writeaccess)
  46. {
  47. struct task_struct *tsk;
  48. struct mm_struct *mm;
  49. struct vm_area_struct * vma;
  50. siginfo_t info;
  51. int fault;
  52. D(printk(KERN_DEBUG
  53. "Page fault for %lX on %X at %lX, prot %d write %d\n",
  54. address, smp_processor_id(), instruction_pointer(regs),
  55. protection, writeaccess));
  56. tsk = current;
  57. /*
  58. * We fault-in kernel-space virtual memory on-demand. The
  59. * 'reference' page table is init_mm.pgd.
  60. *
  61. * NOTE! We MUST NOT take any locks for this case. We may
  62. * be in an interrupt or a critical region, and should
  63. * only copy the information from the master page table,
  64. * nothing more.
  65. *
  66. * NOTE2: This is done so that, when updating the vmalloc
  67. * mappings we don't have to walk all processes pgdirs and
  68. * add the high mappings all at once. Instead we do it as they
  69. * are used. However vmalloc'ed page entries have the PAGE_GLOBAL
  70. * bit set so sometimes the TLB can use a lingering entry.
  71. *
  72. * This verifies that the fault happens in kernel space
  73. * and that the fault was not a protection error (error_code & 1).
  74. */
  75. if (address >= VMALLOC_START &&
  76. !protection &&
  77. !user_mode(regs))
  78. goto vmalloc_fault;
  79. /* When stack execution is not allowed we store the signal
  80. * trampolines in the reserved cris_signal_return_page.
  81. * Handle this in the exact same way as vmalloc (we know
  82. * that the mapping is there and is valid so no need to
  83. * call handle_mm_fault).
  84. */
  85. if (cris_signal_return_page &&
  86. address == cris_signal_return_page &&
  87. !protection && user_mode(regs))
  88. goto vmalloc_fault;
  89. /* we can and should enable interrupts at this point */
  90. local_irq_enable();
  91. mm = tsk->mm;
  92. info.si_code = SEGV_MAPERR;
  93. /*
  94. * If we're in an interrupt or have no user
  95. * context, we must not take the fault..
  96. */
  97. if (in_interrupt() || !mm)
  98. goto no_context;
  99. down_read(&mm->mmap_sem);
  100. vma = find_vma(mm, address);
  101. if (!vma)
  102. goto bad_area;
  103. if (vma->vm_start <= address)
  104. goto good_area;
  105. if (!(vma->vm_flags & VM_GROWSDOWN))
  106. goto bad_area;
  107. if (user_mode(regs)) {
  108. /*
  109. * accessing the stack below usp is always a bug.
  110. * we get page-aligned addresses so we can only check
  111. * if we're within a page from usp, but that might be
  112. * enough to catch brutal errors at least.
  113. */
  114. if (address + PAGE_SIZE < rdusp())
  115. goto bad_area;
  116. }
  117. if (expand_stack(vma, address))
  118. goto bad_area;
  119. /*
  120. * Ok, we have a good vm_area for this memory access, so
  121. * we can handle it..
  122. */
  123. good_area:
  124. info.si_code = SEGV_ACCERR;
  125. /* first do some preliminary protection checks */
  126. if (writeaccess == 2){
  127. if (!(vma->vm_flags & VM_EXEC))
  128. goto bad_area;
  129. } else if (writeaccess == 1) {
  130. if (!(vma->vm_flags & VM_WRITE))
  131. goto bad_area;
  132. } else {
  133. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  134. goto bad_area;
  135. }
  136. /*
  137. * If for any reason at all we couldn't handle the fault,
  138. * make sure we exit gracefully rather than endlessly redo
  139. * the fault.
  140. */
  141. fault = handle_mm_fault(mm, vma, address, (writeaccess & 1) ? FAULT_FLAG_WRITE : 0);
  142. if (unlikely(fault & VM_FAULT_ERROR)) {
  143. if (fault & VM_FAULT_OOM)
  144. goto out_of_memory;
  145. else if (fault & VM_FAULT_SIGBUS)
  146. goto do_sigbus;
  147. BUG();
  148. }
  149. if (fault & VM_FAULT_MAJOR)
  150. tsk->maj_flt++;
  151. else
  152. tsk->min_flt++;
  153. up_read(&mm->mmap_sem);
  154. return;
  155. /*
  156. * Something tried to access memory that isn't in our memory map..
  157. * Fix it, but check if it's kernel or user first..
  158. */
  159. bad_area:
  160. up_read(&mm->mmap_sem);
  161. bad_area_nosemaphore:
  162. DPG(show_registers(regs));
  163. /* User mode accesses just cause a SIGSEGV */
  164. if (user_mode(regs)) {
  165. info.si_signo = SIGSEGV;
  166. info.si_errno = 0;
  167. /* info.si_code has been set above */
  168. info.si_addr = (void *)address;
  169. force_sig_info(SIGSEGV, &info, tsk);
  170. printk(KERN_NOTICE "%s (pid %d) segfaults for page "
  171. "address %08lx at pc %08lx\n",
  172. tsk->comm, tsk->pid, address, instruction_pointer(regs));
  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. if (find_fixup_code(regs))
  185. return;
  186. /*
  187. * Oops. The kernel tried to access some bad page. We'll have to
  188. * terminate things with extreme prejudice.
  189. */
  190. if (!oops_in_progress) {
  191. oops_in_progress = 1;
  192. if ((unsigned long) (address) < PAGE_SIZE)
  193. printk(KERN_ALERT "Unable to handle kernel NULL "
  194. "pointer dereference");
  195. else
  196. printk(KERN_ALERT "Unable to handle kernel access"
  197. " at virtual address %08lx\n", address);
  198. die_if_kernel("Oops", regs, (writeaccess << 1) | protection);
  199. oops_in_progress = 0;
  200. }
  201. do_exit(SIGKILL);
  202. /*
  203. * We ran out of memory, or some other thing happened to us that made
  204. * us unable to handle the page fault gracefully.
  205. */
  206. out_of_memory:
  207. up_read(&mm->mmap_sem);
  208. printk("VM: killing process %s\n", tsk->comm);
  209. if (user_mode(regs))
  210. do_exit(SIGKILL);
  211. goto no_context;
  212. do_sigbus:
  213. up_read(&mm->mmap_sem);
  214. /*
  215. * Send a sigbus, regardless of whether we were in kernel
  216. * or user mode.
  217. */
  218. info.si_signo = SIGBUS;
  219. info.si_errno = 0;
  220. info.si_code = BUS_ADRERR;
  221. info.si_addr = (void *)address;
  222. force_sig_info(SIGBUS, &info, tsk);
  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. pud_t *pud, *pud_k;
  242. pmd_t *pmd, *pmd_k;
  243. pte_t *pte_k;
  244. pgd = (pgd_t *)per_cpu(current_pgd, smp_processor_id()) + offset;
  245. pgd_k = init_mm.pgd + offset;
  246. /* Since we're two-level, we don't need to do both
  247. * set_pgd and set_pmd (they do the same thing). If
  248. * we go three-level at some point, do the right thing
  249. * with pgd_present and set_pgd here.
  250. *
  251. * Also, since the vmalloc area is global, we don't
  252. * need to copy individual PTE's, it is enough to
  253. * copy the pgd pointer into the pte page of the
  254. * root task. If that is there, we'll find our pte if
  255. * it exists.
  256. */
  257. pud = pud_offset(pgd, address);
  258. pud_k = pud_offset(pgd_k, address);
  259. if (!pud_present(*pud_k))
  260. goto no_context;
  261. pmd = pmd_offset(pud, address);
  262. pmd_k = pmd_offset(pud_k, address);
  263. if (!pmd_present(*pmd_k))
  264. goto bad_area_nosemaphore;
  265. set_pmd(pmd, *pmd_k);
  266. /* Make sure the actual PTE exists as well to
  267. * catch kernel vmalloc-area accesses to non-mapped
  268. * addresses. If we don't do this, this will just
  269. * silently loop forever.
  270. */
  271. pte_k = pte_offset_kernel(pmd_k, address);
  272. if (!pte_present(*pte_k))
  273. goto no_context;
  274. return;
  275. }
  276. }
  277. /* Find fixup code. */
  278. int
  279. find_fixup_code(struct pt_regs *regs)
  280. {
  281. const struct exception_table_entry *fixup;
  282. if ((fixup = search_exception_tables(instruction_pointer(regs))) != 0) {
  283. /* Adjust the instruction pointer in the stackframe. */
  284. instruction_pointer(regs) = fixup->fixup;
  285. arch_fixup(regs);
  286. return 1;
  287. }
  288. return 0;
  289. }