fault.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * linux/arch/cris/mm/fault.c
  3. *
  4. * Copyright (C) 2000, 2001 Axis Communications AB
  5. *
  6. * Authors: Bjorn Wesen
  7. *
  8. * $Log: fault.c,v $
  9. * Revision 1.11 2004/05/14 07:58:05 starvik
  10. * Merge of changes from 2.4
  11. *
  12. * Revision 1.10 2003/10/27 14:51:24 starvik
  13. * Removed debugcode
  14. *
  15. * Revision 1.9 2003/10/27 14:50:42 starvik
  16. * Changed do_page_fault signature
  17. *
  18. * Revision 1.8 2003/07/04 13:02:48 tobiasa
  19. * Moved code snippet from arch/cris/mm/fault.c that searches for fixup code
  20. * to seperate function in arch-specific files.
  21. *
  22. * Revision 1.7 2003/01/22 06:48:38 starvik
  23. * Fixed warnings issued by GCC 3.2.1
  24. *
  25. * Revision 1.6 2003/01/09 14:42:52 starvik
  26. * Merge of Linux 2.5.55
  27. *
  28. * Revision 1.5 2002/12/11 14:44:48 starvik
  29. * Extracted v10 (ETRAX 100LX) specific stuff to arch/cris/arch-v10/mm
  30. *
  31. * Revision 1.4 2002/11/13 15:10:28 starvik
  32. * pte_offset has been renamed to pte_offset_kernel
  33. *
  34. * Revision 1.3 2002/11/05 06:45:13 starvik
  35. * Merge of Linux 2.5.45
  36. *
  37. * Revision 1.2 2001/12/18 13:35:22 bjornw
  38. * Applied the 2.4.13->2.4.16 CRIS patch to 2.5.1 (is a copy of 2.4.15).
  39. *
  40. * Revision 1.20 2001/11/22 13:34:06 bjornw
  41. * * Bug workaround (LX TR89): force a rerun of the whole of an interrupted
  42. * unaligned write, because the second half of the write will be corrupted
  43. * otherwise. Affected unaligned writes spanning not-yet mapped pages.
  44. * * Optimization: use the wr_rd bit in R_MMU_CAUSE to know whether a miss
  45. * was due to a read or a write (before we didn't know this until the next
  46. * restart of the interrupted instruction, thus wasting one fault-irq)
  47. *
  48. * Revision 1.19 2001/11/12 19:02:10 pkj
  49. * Fixed compiler warnings.
  50. *
  51. * Revision 1.18 2001/07/18 22:14:32 bjornw
  52. * Enable interrupts in the bulk of do_page_fault
  53. *
  54. * Revision 1.17 2001/07/18 13:07:23 bjornw
  55. * * Detect non-existant PTE's in vmalloc pmd synchronization
  56. * * Remove comment about fast-paths for VMALLOC_START etc, because all that
  57. * was totally bogus anyway it turned out :)
  58. * * Fix detection of vmalloc-area synchronization
  59. * * Add some comments
  60. *
  61. * Revision 1.16 2001/06/13 00:06:08 bjornw
  62. * current_pgd should be volatile
  63. *
  64. * Revision 1.15 2001/06/13 00:02:23 bjornw
  65. * Use a separate variable to store the current pgd to avoid races in schedule
  66. *
  67. * Revision 1.14 2001/05/16 17:41:07 hp
  68. * Last comment tweak further tweaked.
  69. *
  70. * Revision 1.13 2001/05/15 00:58:44 hp
  71. * Expand a bit on the comment why we compare address >= TASK_SIZE rather
  72. * than >= VMALLOC_START.
  73. *
  74. * Revision 1.12 2001/04/04 10:51:14 bjornw
  75. * mmap_sem is grabbed for reading
  76. *
  77. * Revision 1.11 2001/03/23 07:36:07 starvik
  78. * Corrected according to review remarks
  79. *
  80. * Revision 1.10 2001/03/21 16:10:11 bjornw
  81. * CRIS_FRAME_FIXUP not needed anymore, use FRAME_NORMAL
  82. *
  83. * Revision 1.9 2001/03/05 13:22:20 bjornw
  84. * Spell-fix and fix in vmalloc_fault handling
  85. *
  86. * Revision 1.8 2000/11/22 14:45:31 bjornw
  87. * * 2.4.0-test10 removed the set_pgdir instantaneous kernel global mapping
  88. * into all processes. Instead we fill in the missing PTE entries on demand.
  89. *
  90. * Revision 1.7 2000/11/21 16:39:09 bjornw
  91. * fixup switches frametype
  92. *
  93. * Revision 1.6 2000/11/17 16:54:08 bjornw
  94. * More detailed siginfo reporting
  95. *
  96. *
  97. */
  98. #include <linux/mm.h>
  99. #include <linux/interrupt.h>
  100. #include <linux/module.h>
  101. #include <asm/uaccess.h>
  102. extern int find_fixup_code(struct pt_regs *);
  103. extern void die_if_kernel(const char *, struct pt_regs *, long);
  104. /* debug of low-level TLB reload */
  105. #undef DEBUG
  106. #ifdef DEBUG
  107. #define D(x) x
  108. #else
  109. #define D(x)
  110. #endif
  111. /* debug of higher-level faults */
  112. #define DPG(x)
  113. /* current active page directory */
  114. volatile pgd_t *current_pgd;
  115. /*
  116. * This routine handles page faults. It determines the address,
  117. * and the problem, and then passes it off to one of the appropriate
  118. * routines.
  119. *
  120. * Notice that the address we're given is aligned to the page the fault
  121. * occurred in, since we only get the PFN in R_MMU_CAUSE not the complete
  122. * address.
  123. *
  124. * error_code:
  125. * bit 0 == 0 means no page found, 1 means protection fault
  126. * bit 1 == 0 means read, 1 means write
  127. *
  128. * If this routine detects a bad access, it returns 1, otherwise it
  129. * returns 0.
  130. */
  131. asmlinkage void
  132. do_page_fault(unsigned long address, struct pt_regs *regs,
  133. int protection, int writeaccess)
  134. {
  135. struct task_struct *tsk;
  136. struct mm_struct *mm;
  137. struct vm_area_struct * vma;
  138. siginfo_t info;
  139. D(printk("Page fault for %X at %X, prot %d write %d\n",
  140. address, regs->erp, protection, writeaccess));
  141. tsk = current;
  142. /*
  143. * We fault-in kernel-space virtual memory on-demand. The
  144. * 'reference' page table is init_mm.pgd.
  145. *
  146. * NOTE! We MUST NOT take any locks for this case. We may
  147. * be in an interrupt or a critical region, and should
  148. * only copy the information from the master page table,
  149. * nothing more.
  150. *
  151. * NOTE2: This is done so that, when updating the vmalloc
  152. * mappings we don't have to walk all processes pgdirs and
  153. * add the high mappings all at once. Instead we do it as they
  154. * are used. However vmalloc'ed page entries have the PAGE_GLOBAL
  155. * bit set so sometimes the TLB can use a lingering entry.
  156. *
  157. * This verifies that the fault happens in kernel space
  158. * and that the fault was not a protection error (error_code & 1).
  159. */
  160. if (address >= VMALLOC_START &&
  161. !protection &&
  162. !user_mode(regs))
  163. goto vmalloc_fault;
  164. /* we can and should enable interrupts at this point */
  165. sti();
  166. mm = tsk->mm;
  167. info.si_code = SEGV_MAPERR;
  168. /*
  169. * If we're in an interrupt or have no user
  170. * context, we must not take the fault..
  171. */
  172. if (in_interrupt() || !mm)
  173. goto no_context;
  174. down_read(&mm->mmap_sem);
  175. vma = find_vma(mm, address);
  176. if (!vma)
  177. goto bad_area;
  178. if (vma->vm_start <= address)
  179. goto good_area;
  180. if (!(vma->vm_flags & VM_GROWSDOWN))
  181. goto bad_area;
  182. if (user_mode(regs)) {
  183. /*
  184. * accessing the stack below usp is always a bug.
  185. * we get page-aligned addresses so we can only check
  186. * if we're within a page from usp, but that might be
  187. * enough to catch brutal errors at least.
  188. */
  189. if (address + PAGE_SIZE < rdusp())
  190. goto bad_area;
  191. }
  192. if (expand_stack(vma, address))
  193. goto bad_area;
  194. /*
  195. * Ok, we have a good vm_area for this memory access, so
  196. * we can handle it..
  197. */
  198. good_area:
  199. info.si_code = SEGV_ACCERR;
  200. /* first do some preliminary protection checks */
  201. if (writeaccess) {
  202. if (!(vma->vm_flags & VM_WRITE))
  203. goto bad_area;
  204. } else {
  205. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  206. goto bad_area;
  207. }
  208. /*
  209. * If for any reason at all we couldn't handle the fault,
  210. * make sure we exit gracefully rather than endlessly redo
  211. * the fault.
  212. */
  213. switch (handle_mm_fault(mm, vma, address, writeaccess)) {
  214. case 1:
  215. tsk->min_flt++;
  216. break;
  217. case 2:
  218. tsk->maj_flt++;
  219. break;
  220. case 0:
  221. goto do_sigbus;
  222. default:
  223. goto out_of_memory;
  224. }
  225. up_read(&mm->mmap_sem);
  226. return;
  227. /*
  228. * Something tried to access memory that isn't in our memory map..
  229. * Fix it, but check if it's kernel or user first..
  230. */
  231. bad_area:
  232. up_read(&mm->mmap_sem);
  233. bad_area_nosemaphore:
  234. DPG(show_registers(regs));
  235. /* User mode accesses just cause a SIGSEGV */
  236. if (user_mode(regs)) {
  237. info.si_signo = SIGSEGV;
  238. info.si_errno = 0;
  239. /* info.si_code has been set above */
  240. info.si_addr = (void *)address;
  241. force_sig_info(SIGSEGV, &info, tsk);
  242. return;
  243. }
  244. no_context:
  245. /* Are we prepared to handle this kernel fault?
  246. *
  247. * (The kernel has valid exception-points in the source
  248. * when it acesses user-memory. When it fails in one
  249. * of those points, we find it in a table and do a jump
  250. * to some fixup code that loads an appropriate error
  251. * code)
  252. */
  253. if (find_fixup_code(regs))
  254. return;
  255. /*
  256. * Oops. The kernel tried to access some bad page. We'll have to
  257. * terminate things with extreme prejudice.
  258. */
  259. if ((unsigned long) (address) < PAGE_SIZE)
  260. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
  261. else
  262. printk(KERN_ALERT "Unable to handle kernel access");
  263. printk(" at virtual address %08lx\n",address);
  264. die_if_kernel("Oops", regs, (writeaccess << 1) | protection);
  265. do_exit(SIGKILL);
  266. /*
  267. * We ran out of memory, or some other thing happened to us that made
  268. * us unable to handle the page fault gracefully.
  269. */
  270. out_of_memory:
  271. up_read(&mm->mmap_sem);
  272. printk("VM: killing process %s\n", tsk->comm);
  273. if (user_mode(regs))
  274. do_exit(SIGKILL);
  275. goto no_context;
  276. do_sigbus:
  277. up_read(&mm->mmap_sem);
  278. /*
  279. * Send a sigbus, regardless of whether we were in kernel
  280. * or user mode.
  281. */
  282. info.si_signo = SIGBUS;
  283. info.si_errno = 0;
  284. info.si_code = BUS_ADRERR;
  285. info.si_addr = (void *)address;
  286. force_sig_info(SIGBUS, &info, tsk);
  287. /* Kernel mode? Handle exceptions or die */
  288. if (!user_mode(regs))
  289. goto no_context;
  290. return;
  291. vmalloc_fault:
  292. {
  293. /*
  294. * Synchronize this task's top level page-table
  295. * with the 'reference' page table.
  296. *
  297. * Use current_pgd instead of tsk->active_mm->pgd
  298. * since the latter might be unavailable if this
  299. * code is executed in a misfortunately run irq
  300. * (like inside schedule() between switch_mm and
  301. * switch_to...).
  302. */
  303. int offset = pgd_index(address);
  304. pgd_t *pgd, *pgd_k;
  305. pmd_t *pmd, *pmd_k;
  306. pte_t *pte_k;
  307. pgd = (pgd_t *)current_pgd + offset;
  308. pgd_k = init_mm.pgd + offset;
  309. /* Since we're two-level, we don't need to do both
  310. * set_pgd and set_pmd (they do the same thing). If
  311. * we go three-level at some point, do the right thing
  312. * with pgd_present and set_pgd here.
  313. *
  314. * Also, since the vmalloc area is global, we don't
  315. * need to copy individual PTE's, it is enough to
  316. * copy the pgd pointer into the pte page of the
  317. * root task. If that is there, we'll find our pte if
  318. * it exists.
  319. */
  320. pmd = pmd_offset(pgd, address);
  321. pmd_k = pmd_offset(pgd_k, address);
  322. if (!pmd_present(*pmd_k))
  323. goto bad_area_nosemaphore;
  324. set_pmd(pmd, *pmd_k);
  325. /* Make sure the actual PTE exists as well to
  326. * catch kernel vmalloc-area accesses to non-mapped
  327. * addresses. If we don't do this, this will just
  328. * silently loop forever.
  329. */
  330. pte_k = pte_offset_kernel(pmd_k, address);
  331. if (!pte_present(*pte_k))
  332. goto no_context;
  333. return;
  334. }
  335. }