fault.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * linux/arch/frv/mm/fault.c
  3. *
  4. * Copyright (C) 2003 Red Hat, Inc. All Rights Reserved.
  5. * - Written by David Howells (dhowells@redhat.com)
  6. * - Derived from arch/m68knommu/mm/fault.c
  7. * - Copyright (C) 1998 D. Jeff Dionne <jeff@lineo.ca>,
  8. * - Copyright (C) 2000 Lineo, Inc. (www.lineo.com)
  9. *
  10. * Based on:
  11. *
  12. * linux/arch/m68k/mm/fault.c
  13. *
  14. * Copyright (C) 1995 Hamish Macdonald
  15. */
  16. #include <linux/mman.h>
  17. #include <linux/mm.h>
  18. #include <linux/kernel.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/hardirq.h>
  21. #include <linux/uaccess.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/gdb-stub.h>
  24. /*****************************************************************************/
  25. /*
  26. * This routine handles page faults. It determines the problem, and
  27. * then passes it off to one of the appropriate routines.
  28. */
  29. asmlinkage void do_page_fault(int datammu, unsigned long esr0, unsigned long ear0)
  30. {
  31. struct vm_area_struct *vma;
  32. struct mm_struct *mm;
  33. unsigned long _pme, lrai, lrad, fixup;
  34. unsigned long flags = 0;
  35. siginfo_t info;
  36. pgd_t *pge;
  37. pud_t *pue;
  38. pte_t *pte;
  39. int fault;
  40. #if 0
  41. const char *atxc[16] = {
  42. [0x0] = "mmu-miss", [0x8] = "multi-dat", [0x9] = "multi-sat",
  43. [0xa] = "tlb-miss", [0xc] = "privilege", [0xd] = "write-prot",
  44. };
  45. printk("do_page_fault(%d,%lx [%s],%lx)\n",
  46. datammu, esr0, atxc[esr0 >> 20 & 0xf], ear0);
  47. #endif
  48. mm = current->mm;
  49. /*
  50. * We fault-in kernel-space virtual memory on-demand. The
  51. * 'reference' page table is init_mm.pgd.
  52. *
  53. * NOTE! We MUST NOT take any locks for this case. We may
  54. * be in an interrupt or a critical region, and should
  55. * only copy the information from the master page table,
  56. * nothing more.
  57. *
  58. * This verifies that the fault happens in kernel space
  59. * and that the fault was a page not present (invalid) error
  60. */
  61. if (!user_mode(__frame) && (esr0 & ESR0_ATXC) == ESR0_ATXC_AMRTLB_MISS) {
  62. if (ear0 >= VMALLOC_START && ear0 < VMALLOC_END)
  63. goto kernel_pte_fault;
  64. if (ear0 >= PKMAP_BASE && ear0 < PKMAP_END)
  65. goto kernel_pte_fault;
  66. }
  67. info.si_code = SEGV_MAPERR;
  68. /*
  69. * If we're in an interrupt or have no user
  70. * context, we must not take the fault..
  71. */
  72. if (faulthandler_disabled() || !mm)
  73. goto no_context;
  74. if (user_mode(__frame))
  75. flags |= FAULT_FLAG_USER;
  76. down_read(&mm->mmap_sem);
  77. vma = find_vma(mm, ear0);
  78. if (!vma)
  79. goto bad_area;
  80. if (vma->vm_start <= ear0)
  81. goto good_area;
  82. if (!(vma->vm_flags & VM_GROWSDOWN))
  83. goto bad_area;
  84. if (user_mode(__frame)) {
  85. /*
  86. * accessing the stack below %esp is always a bug.
  87. * The "+ 32" is there due to some instructions (like
  88. * pusha) doing post-decrement on the stack and that
  89. * doesn't show up until later..
  90. */
  91. if ((ear0 & PAGE_MASK) + 2 * PAGE_SIZE < __frame->sp) {
  92. #if 0
  93. printk("[%d] ### Access below stack @%lx (sp=%lx)\n",
  94. current->pid, ear0, __frame->sp);
  95. show_registers(__frame);
  96. printk("[%d] ### Code: [%08lx] %02x %02x %02x %02x %02x %02x %02x %02x\n",
  97. current->pid,
  98. __frame->pc,
  99. ((u8*)__frame->pc)[0],
  100. ((u8*)__frame->pc)[1],
  101. ((u8*)__frame->pc)[2],
  102. ((u8*)__frame->pc)[3],
  103. ((u8*)__frame->pc)[4],
  104. ((u8*)__frame->pc)[5],
  105. ((u8*)__frame->pc)[6],
  106. ((u8*)__frame->pc)[7]
  107. );
  108. #endif
  109. goto bad_area;
  110. }
  111. }
  112. if (expand_stack(vma, ear0))
  113. goto bad_area;
  114. /*
  115. * Ok, we have a good vm_area for this memory access, so
  116. * we can handle it..
  117. */
  118. good_area:
  119. info.si_code = SEGV_ACCERR;
  120. switch (esr0 & ESR0_ATXC) {
  121. default:
  122. /* handle write to write protected page */
  123. case ESR0_ATXC_WP_EXCEP:
  124. #ifdef TEST_VERIFY_AREA
  125. if (!(user_mode(__frame)))
  126. printk("WP fault at %08lx\n", __frame->pc);
  127. #endif
  128. if (!(vma->vm_flags & VM_WRITE))
  129. goto bad_area;
  130. flags |= FAULT_FLAG_WRITE;
  131. break;
  132. /* handle read from protected page */
  133. case ESR0_ATXC_PRIV_EXCEP:
  134. goto bad_area;
  135. /* handle read, write or exec on absent page
  136. * - can't support write without permitting read
  137. * - don't support execute without permitting read and vice-versa
  138. */
  139. case ESR0_ATXC_AMRTLB_MISS:
  140. if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
  141. goto bad_area;
  142. break;
  143. }
  144. /*
  145. * If for any reason at all we couldn't handle the fault,
  146. * make sure we exit gracefully rather than endlessly redo
  147. * the fault.
  148. */
  149. fault = handle_mm_fault(vma, ear0, flags);
  150. if (unlikely(fault & VM_FAULT_ERROR)) {
  151. if (fault & VM_FAULT_OOM)
  152. goto out_of_memory;
  153. else if (fault & VM_FAULT_SIGSEGV)
  154. goto bad_area;
  155. else if (fault & VM_FAULT_SIGBUS)
  156. goto do_sigbus;
  157. BUG();
  158. }
  159. if (fault & VM_FAULT_MAJOR)
  160. current->maj_flt++;
  161. else
  162. current->min_flt++;
  163. up_read(&mm->mmap_sem);
  164. return;
  165. /*
  166. * Something tried to access memory that isn't in our memory map..
  167. * Fix it, but check if it's kernel or user first..
  168. */
  169. bad_area:
  170. up_read(&mm->mmap_sem);
  171. /* User mode accesses just cause a SIGSEGV */
  172. if (user_mode(__frame)) {
  173. info.si_signo = SIGSEGV;
  174. info.si_errno = 0;
  175. /* info.si_code has been set above */
  176. info.si_addr = (void *) ear0;
  177. force_sig_info(SIGSEGV, &info, current);
  178. return;
  179. }
  180. no_context:
  181. /* are we prepared to handle this kernel fault? */
  182. if ((fixup = search_exception_table(__frame->pc)) != 0) {
  183. __frame->pc = fixup;
  184. return;
  185. }
  186. /*
  187. * Oops. The kernel tried to access some bad page. We'll have to
  188. * terminate things with extreme prejudice.
  189. */
  190. bust_spinlocks(1);
  191. if (ear0 < PAGE_SIZE)
  192. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
  193. else
  194. printk(KERN_ALERT "Unable to handle kernel paging request");
  195. printk(" at virtual addr %08lx\n", ear0);
  196. printk(" PC : %08lx\n", __frame->pc);
  197. printk(" EXC : esr0=%08lx ear0=%08lx\n", esr0, ear0);
  198. asm("lrai %1,%0,#1,#0,#0" : "=&r"(lrai) : "r"(ear0));
  199. asm("lrad %1,%0,#1,#0,#0" : "=&r"(lrad) : "r"(ear0));
  200. printk(KERN_ALERT " LRAI: %08lx\n", lrai);
  201. printk(KERN_ALERT " LRAD: %08lx\n", lrad);
  202. __break_hijack_kernel_event();
  203. pge = pgd_offset(current->mm, ear0);
  204. pue = pud_offset(pge, ear0);
  205. _pme = pue->pue[0].ste[0];
  206. printk(KERN_ALERT " PGE : %8p { PME %08lx }\n", pge, _pme);
  207. if (_pme & xAMPRx_V) {
  208. unsigned long dampr, damlr, val;
  209. asm volatile("movsg dampr2,%0 ! movgs %2,dampr2 ! movsg damlr2,%1"
  210. : "=&r"(dampr), "=r"(damlr)
  211. : "r" (_pme | xAMPRx_L|xAMPRx_SS_16Kb|xAMPRx_S|xAMPRx_C|xAMPRx_V)
  212. );
  213. pte = (pte_t *) damlr + __pte_index(ear0);
  214. val = pte_val(*pte);
  215. asm volatile("movgs %0,dampr2" :: "r" (dampr));
  216. printk(KERN_ALERT " PTE : %8p { %08lx }\n", pte, val);
  217. }
  218. die_if_kernel("Oops\n");
  219. do_exit(SIGKILL);
  220. /*
  221. * We ran out of memory, or some other thing happened to us that made
  222. * us unable to handle the page fault gracefully.
  223. */
  224. out_of_memory:
  225. up_read(&mm->mmap_sem);
  226. if (!user_mode(__frame))
  227. goto no_context;
  228. pagefault_out_of_memory();
  229. return;
  230. do_sigbus:
  231. up_read(&mm->mmap_sem);
  232. /*
  233. * Send a sigbus, regardless of whether we were in kernel
  234. * or user mode.
  235. */
  236. info.si_signo = SIGBUS;
  237. info.si_errno = 0;
  238. info.si_code = BUS_ADRERR;
  239. info.si_addr = (void *) ear0;
  240. force_sig_info(SIGBUS, &info, current);
  241. /* Kernel mode? Handle exceptions or die */
  242. if (!user_mode(__frame))
  243. goto no_context;
  244. return;
  245. /*
  246. * The fault was caused by a kernel PTE (such as installed by vmalloc or kmap)
  247. */
  248. kernel_pte_fault:
  249. {
  250. /*
  251. * Synchronize this task's top level page-table
  252. * with the 'reference' page table.
  253. *
  254. * Do _not_ use "tsk" here. We might be inside
  255. * an interrupt in the middle of a task switch..
  256. */
  257. int index = pgd_index(ear0);
  258. pgd_t *pgd, *pgd_k;
  259. pud_t *pud, *pud_k;
  260. pmd_t *pmd, *pmd_k;
  261. pte_t *pte_k;
  262. pgd = (pgd_t *) __get_TTBR();
  263. pgd = (pgd_t *)__va(pgd) + index;
  264. pgd_k = ((pgd_t *)(init_mm.pgd)) + index;
  265. if (!pgd_present(*pgd_k))
  266. goto no_context;
  267. //set_pgd(pgd, *pgd_k); /////// gcc ICE's on this line
  268. pud_k = pud_offset(pgd_k, ear0);
  269. if (!pud_present(*pud_k))
  270. goto no_context;
  271. pmd_k = pmd_offset(pud_k, ear0);
  272. if (!pmd_present(*pmd_k))
  273. goto no_context;
  274. pud = pud_offset(pgd, ear0);
  275. pmd = pmd_offset(pud, ear0);
  276. set_pmd(pmd, *pmd_k);
  277. pte_k = pte_offset_kernel(pmd_k, ear0);
  278. if (!pte_present(*pte_k))
  279. goto no_context;
  280. return;
  281. }
  282. } /* end do_page_fault() */