fault.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 <linux/wait.h>
  10. #include <linux/uaccess.h>
  11. #include <arch/system.h>
  12. extern int find_fixup_code(struct pt_regs *);
  13. extern void die_if_kernel(const char *, struct pt_regs *, long);
  14. extern void show_registers(struct pt_regs *regs);
  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. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  53. D(printk(KERN_DEBUG
  54. "Page fault for %lX on %X at %lX, prot %d write %d\n",
  55. address, smp_processor_id(), instruction_pointer(regs),
  56. protection, writeaccess));
  57. tsk = current;
  58. /*
  59. * We fault-in kernel-space virtual memory on-demand. The
  60. * 'reference' page table is init_mm.pgd.
  61. *
  62. * NOTE! We MUST NOT take any locks for this case. We may
  63. * be in an interrupt or a critical region, and should
  64. * only copy the information from the master page table,
  65. * nothing more.
  66. *
  67. * NOTE2: This is done so that, when updating the vmalloc
  68. * mappings we don't have to walk all processes pgdirs and
  69. * add the high mappings all at once. Instead we do it as they
  70. * are used. However vmalloc'ed page entries have the PAGE_GLOBAL
  71. * bit set so sometimes the TLB can use a lingering entry.
  72. *
  73. * This verifies that the fault happens in kernel space
  74. * and that the fault was not a protection error (error_code & 1).
  75. */
  76. if (address >= VMALLOC_START &&
  77. !protection &&
  78. !user_mode(regs))
  79. goto vmalloc_fault;
  80. /* When stack execution is not allowed we store the signal
  81. * trampolines in the reserved cris_signal_return_page.
  82. * Handle this in the exact same way as vmalloc (we know
  83. * that the mapping is there and is valid so no need to
  84. * call handle_mm_fault).
  85. */
  86. if (cris_signal_return_page &&
  87. address == cris_signal_return_page &&
  88. !protection && user_mode(regs))
  89. goto vmalloc_fault;
  90. /* we can and should enable interrupts at this point */
  91. local_irq_enable();
  92. mm = tsk->mm;
  93. info.si_code = SEGV_MAPERR;
  94. /*
  95. * If we're in an interrupt, have pagefaults disabled or have no
  96. * user context, we must not take the fault.
  97. */
  98. if (faulthandler_disabled() || !mm)
  99. goto no_context;
  100. if (user_mode(regs))
  101. flags |= FAULT_FLAG_USER;
  102. retry:
  103. down_read(&mm->mmap_sem);
  104. vma = find_vma(mm, address);
  105. if (!vma)
  106. goto bad_area;
  107. if (vma->vm_start <= address)
  108. goto good_area;
  109. if (!(vma->vm_flags & VM_GROWSDOWN))
  110. goto bad_area;
  111. if (user_mode(regs)) {
  112. /*
  113. * accessing the stack below usp is always a bug.
  114. * we get page-aligned addresses so we can only check
  115. * if we're within a page from usp, but that might be
  116. * enough to catch brutal errors at least.
  117. */
  118. if (address + PAGE_SIZE < rdusp())
  119. goto bad_area;
  120. }
  121. if (expand_stack(vma, address))
  122. goto bad_area;
  123. /*
  124. * Ok, we have a good vm_area for this memory access, so
  125. * we can handle it..
  126. */
  127. good_area:
  128. info.si_code = SEGV_ACCERR;
  129. /* first do some preliminary protection checks */
  130. if (writeaccess == 2){
  131. if (!(vma->vm_flags & VM_EXEC))
  132. goto bad_area;
  133. } else if (writeaccess == 1) {
  134. if (!(vma->vm_flags & VM_WRITE))
  135. goto bad_area;
  136. flags |= FAULT_FLAG_WRITE;
  137. } else {
  138. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  139. goto bad_area;
  140. }
  141. /*
  142. * If for any reason at all we couldn't handle the fault,
  143. * make sure we exit gracefully rather than endlessly redo
  144. * the fault.
  145. */
  146. fault = handle_mm_fault(mm, vma, address, flags);
  147. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  148. return;
  149. if (unlikely(fault & VM_FAULT_ERROR)) {
  150. if (fault & VM_FAULT_OOM)
  151. goto out_of_memory;
  152. else if (fault & VM_FAULT_SIGSEGV)
  153. goto bad_area;
  154. else if (fault & VM_FAULT_SIGBUS)
  155. goto do_sigbus;
  156. BUG();
  157. }
  158. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  159. if (fault & VM_FAULT_MAJOR)
  160. tsk->maj_flt++;
  161. else
  162. tsk->min_flt++;
  163. if (fault & VM_FAULT_RETRY) {
  164. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  165. flags |= FAULT_FLAG_TRIED;
  166. /*
  167. * No need to up_read(&mm->mmap_sem) as we would
  168. * have already released it in __lock_page_or_retry
  169. * in mm/filemap.c.
  170. */
  171. goto retry;
  172. }
  173. }
  174. up_read(&mm->mmap_sem);
  175. return;
  176. /*
  177. * Something tried to access memory that isn't in our memory map..
  178. * Fix it, but check if it's kernel or user first..
  179. */
  180. bad_area:
  181. up_read(&mm->mmap_sem);
  182. bad_area_nosemaphore:
  183. DPG(show_registers(regs));
  184. /* User mode accesses just cause a SIGSEGV */
  185. if (user_mode(regs)) {
  186. #ifdef CONFIG_NO_SEGFAULT_TERMINATION
  187. DECLARE_WAIT_QUEUE_HEAD(wq);
  188. #endif
  189. printk(KERN_NOTICE "%s (pid %d) segfaults for page "
  190. "address %08lx at pc %08lx\n",
  191. tsk->comm, tsk->pid,
  192. address, instruction_pointer(regs));
  193. /* With DPG on, we've already dumped registers above. */
  194. DPG(if (0))
  195. show_registers(regs);
  196. #ifdef CONFIG_NO_SEGFAULT_TERMINATION
  197. wait_event_interruptible(wq, 0 == 1);
  198. #else
  199. info.si_signo = SIGSEGV;
  200. info.si_errno = 0;
  201. /* info.si_code has been set above */
  202. info.si_addr = (void *)address;
  203. force_sig_info(SIGSEGV, &info, tsk);
  204. #endif
  205. return;
  206. }
  207. no_context:
  208. /* Are we prepared to handle this kernel fault?
  209. *
  210. * (The kernel has valid exception-points in the source
  211. * when it accesses user-memory. When it fails in one
  212. * of those points, we find it in a table and do a jump
  213. * to some fixup code that loads an appropriate error
  214. * code)
  215. */
  216. if (find_fixup_code(regs))
  217. return;
  218. /*
  219. * Oops. The kernel tried to access some bad page. We'll have to
  220. * terminate things with extreme prejudice.
  221. */
  222. if (!oops_in_progress) {
  223. oops_in_progress = 1;
  224. if ((unsigned long) (address) < PAGE_SIZE)
  225. printk(KERN_ALERT "Unable to handle kernel NULL "
  226. "pointer dereference");
  227. else
  228. printk(KERN_ALERT "Unable to handle kernel access"
  229. " at virtual address %08lx\n", address);
  230. die_if_kernel("Oops", regs, (writeaccess << 1) | protection);
  231. oops_in_progress = 0;
  232. }
  233. do_exit(SIGKILL);
  234. /*
  235. * We ran out of memory, or some other thing happened to us that made
  236. * us unable to handle the page fault gracefully.
  237. */
  238. out_of_memory:
  239. up_read(&mm->mmap_sem);
  240. if (!user_mode(regs))
  241. goto no_context;
  242. pagefault_out_of_memory();
  243. return;
  244. do_sigbus:
  245. up_read(&mm->mmap_sem);
  246. /*
  247. * Send a sigbus, regardless of whether we were in kernel
  248. * or user mode.
  249. */
  250. info.si_signo = SIGBUS;
  251. info.si_errno = 0;
  252. info.si_code = BUS_ADRERR;
  253. info.si_addr = (void *)address;
  254. force_sig_info(SIGBUS, &info, tsk);
  255. /* Kernel mode? Handle exceptions or die */
  256. if (!user_mode(regs))
  257. goto no_context;
  258. return;
  259. vmalloc_fault:
  260. {
  261. /*
  262. * Synchronize this task's top level page-table
  263. * with the 'reference' page table.
  264. *
  265. * Use current_pgd instead of tsk->active_mm->pgd
  266. * since the latter might be unavailable if this
  267. * code is executed in a misfortunately run irq
  268. * (like inside schedule() between switch_mm and
  269. * switch_to...).
  270. */
  271. int offset = pgd_index(address);
  272. pgd_t *pgd, *pgd_k;
  273. pud_t *pud, *pud_k;
  274. pmd_t *pmd, *pmd_k;
  275. pte_t *pte_k;
  276. pgd = (pgd_t *)per_cpu(current_pgd, smp_processor_id()) + offset;
  277. pgd_k = init_mm.pgd + offset;
  278. /* Since we're two-level, we don't need to do both
  279. * set_pgd and set_pmd (they do the same thing). If
  280. * we go three-level at some point, do the right thing
  281. * with pgd_present and set_pgd here.
  282. *
  283. * Also, since the vmalloc area is global, we don't
  284. * need to copy individual PTE's, it is enough to
  285. * copy the pgd pointer into the pte page of the
  286. * root task. If that is there, we'll find our pte if
  287. * it exists.
  288. */
  289. pud = pud_offset(pgd, address);
  290. pud_k = pud_offset(pgd_k, address);
  291. if (!pud_present(*pud_k))
  292. goto no_context;
  293. pmd = pmd_offset(pud, address);
  294. pmd_k = pmd_offset(pud_k, address);
  295. if (!pmd_present(*pmd_k))
  296. goto bad_area_nosemaphore;
  297. set_pmd(pmd, *pmd_k);
  298. /* Make sure the actual PTE exists as well to
  299. * catch kernel vmalloc-area accesses to non-mapped
  300. * addresses. If we don't do this, this will just
  301. * silently loop forever.
  302. */
  303. pte_k = pte_offset_kernel(pmd_k, address);
  304. if (!pte_present(*pte_k))
  305. goto no_context;
  306. return;
  307. }
  308. }
  309. /* Find fixup code. */
  310. int
  311. find_fixup_code(struct pt_regs *regs)
  312. {
  313. const struct exception_table_entry *fixup;
  314. /* in case of delay slot fault (v32) */
  315. unsigned long ip = (instruction_pointer(regs) & ~0x1);
  316. fixup = search_exception_tables(ip);
  317. if (fixup != 0) {
  318. /* Adjust the instruction pointer in the stackframe. */
  319. instruction_pointer(regs) = fixup->fixup;
  320. arch_fixup(regs);
  321. return 1;
  322. }
  323. return 0;
  324. }