fault.c 8.4 KB

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