fault.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 <asm/pgtable.h>
  22. #include <asm/uaccess.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. siginfo_t info;
  35. pgd_t *pge;
  36. pud_t *pue;
  37. pte_t *pte;
  38. int write;
  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 (in_atomic() || !mm)
  73. goto no_context;
  74. down_read(&mm->mmap_sem);
  75. vma = find_vma(mm, ear0);
  76. if (!vma)
  77. goto bad_area;
  78. if (vma->vm_start <= ear0)
  79. goto good_area;
  80. if (!(vma->vm_flags & VM_GROWSDOWN))
  81. goto bad_area;
  82. if (user_mode(__frame)) {
  83. /*
  84. * accessing the stack below %esp is always a bug.
  85. * The "+ 32" is there due to some instructions (like
  86. * pusha) doing post-decrement on the stack and that
  87. * doesn't show up until later..
  88. */
  89. if ((ear0 & PAGE_MASK) + 2 * PAGE_SIZE < __frame->sp) {
  90. #if 0
  91. printk("[%d] ### Access below stack @%lx (sp=%lx)\n",
  92. current->pid, ear0, __frame->sp);
  93. show_registers(__frame);
  94. printk("[%d] ### Code: [%08lx] %02x %02x %02x %02x %02x %02x %02x %02x\n",
  95. current->pid,
  96. __frame->pc,
  97. ((u8*)__frame->pc)[0],
  98. ((u8*)__frame->pc)[1],
  99. ((u8*)__frame->pc)[2],
  100. ((u8*)__frame->pc)[3],
  101. ((u8*)__frame->pc)[4],
  102. ((u8*)__frame->pc)[5],
  103. ((u8*)__frame->pc)[6],
  104. ((u8*)__frame->pc)[7]
  105. );
  106. #endif
  107. goto bad_area;
  108. }
  109. }
  110. if (expand_stack(vma, ear0))
  111. goto bad_area;
  112. /*
  113. * Ok, we have a good vm_area for this memory access, so
  114. * we can handle it..
  115. */
  116. good_area:
  117. info.si_code = SEGV_ACCERR;
  118. write = 0;
  119. switch (esr0 & ESR0_ATXC) {
  120. default:
  121. /* handle write to write protected page */
  122. case ESR0_ATXC_WP_EXCEP:
  123. #ifdef TEST_VERIFY_AREA
  124. if (!(user_mode(__frame)))
  125. printk("WP fault at %08lx\n", __frame->pc);
  126. #endif
  127. if (!(vma->vm_flags & VM_WRITE))
  128. goto bad_area;
  129. write = 1;
  130. break;
  131. /* handle read from protected page */
  132. case ESR0_ATXC_PRIV_EXCEP:
  133. goto bad_area;
  134. /* handle read, write or exec on absent page
  135. * - can't support write without permitting read
  136. * - don't support execute without permitting read and vice-versa
  137. */
  138. case ESR0_ATXC_AMRTLB_MISS:
  139. if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
  140. goto bad_area;
  141. break;
  142. }
  143. /*
  144. * If for any reason at all we couldn't handle the fault,
  145. * make sure we exit gracefully rather than endlessly redo
  146. * the fault.
  147. */
  148. fault = handle_mm_fault(mm, vma, ear0, write ? FAULT_FLAG_WRITE : 0);
  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 (fault & VM_FAULT_MAJOR)
  159. current->maj_flt++;
  160. else
  161. current->min_flt++;
  162. up_read(&mm->mmap_sem);
  163. return;
  164. /*
  165. * Something tried to access memory that isn't in our memory map..
  166. * Fix it, but check if it's kernel or user first..
  167. */
  168. bad_area:
  169. up_read(&mm->mmap_sem);
  170. /* User mode accesses just cause a SIGSEGV */
  171. if (user_mode(__frame)) {
  172. info.si_signo = SIGSEGV;
  173. info.si_errno = 0;
  174. /* info.si_code has been set above */
  175. info.si_addr = (void *) ear0;
  176. force_sig_info(SIGSEGV, &info, current);
  177. return;
  178. }
  179. no_context:
  180. /* are we prepared to handle this kernel fault? */
  181. if ((fixup = search_exception_table(__frame->pc)) != 0) {
  182. __frame->pc = fixup;
  183. return;
  184. }
  185. /*
  186. * Oops. The kernel tried to access some bad page. We'll have to
  187. * terminate things with extreme prejudice.
  188. */
  189. bust_spinlocks(1);
  190. if (ear0 < PAGE_SIZE)
  191. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
  192. else
  193. printk(KERN_ALERT "Unable to handle kernel paging request");
  194. printk(" at virtual addr %08lx\n", ear0);
  195. printk(" PC : %08lx\n", __frame->pc);
  196. printk(" EXC : esr0=%08lx ear0=%08lx\n", esr0, ear0);
  197. asm("lrai %1,%0,#1,#0,#0" : "=&r"(lrai) : "r"(ear0));
  198. asm("lrad %1,%0,#1,#0,#0" : "=&r"(lrad) : "r"(ear0));
  199. printk(KERN_ALERT " LRAI: %08lx\n", lrai);
  200. printk(KERN_ALERT " LRAD: %08lx\n", lrad);
  201. __break_hijack_kernel_event();
  202. pge = pgd_offset(current->mm, ear0);
  203. pue = pud_offset(pge, ear0);
  204. _pme = pue->pue[0].ste[0];
  205. printk(KERN_ALERT " PGE : %8p { PME %08lx }\n", pge, _pme);
  206. if (_pme & xAMPRx_V) {
  207. unsigned long dampr, damlr, val;
  208. asm volatile("movsg dampr2,%0 ! movgs %2,dampr2 ! movsg damlr2,%1"
  209. : "=&r"(dampr), "=r"(damlr)
  210. : "r" (_pme | xAMPRx_L|xAMPRx_SS_16Kb|xAMPRx_S|xAMPRx_C|xAMPRx_V)
  211. );
  212. pte = (pte_t *) damlr + __pte_index(ear0);
  213. val = pte_val(*pte);
  214. asm volatile("movgs %0,dampr2" :: "r" (dampr));
  215. printk(KERN_ALERT " PTE : %8p { %08lx }\n", pte, val);
  216. }
  217. die_if_kernel("Oops\n");
  218. do_exit(SIGKILL);
  219. /*
  220. * We ran out of memory, or some other thing happened to us that made
  221. * us unable to handle the page fault gracefully.
  222. */
  223. out_of_memory:
  224. up_read(&mm->mmap_sem);
  225. if (!user_mode(__frame))
  226. goto no_context;
  227. pagefault_out_of_memory();
  228. return;
  229. do_sigbus:
  230. up_read(&mm->mmap_sem);
  231. /*
  232. * Send a sigbus, regardless of whether we were in kernel
  233. * or user mode.
  234. */
  235. info.si_signo = SIGBUS;
  236. info.si_errno = 0;
  237. info.si_code = BUS_ADRERR;
  238. info.si_addr = (void *) ear0;
  239. force_sig_info(SIGBUS, &info, current);
  240. /* Kernel mode? Handle exceptions or die */
  241. if (!user_mode(__frame))
  242. goto no_context;
  243. return;
  244. /*
  245. * The fault was caused by a kernel PTE (such as installed by vmalloc or kmap)
  246. */
  247. kernel_pte_fault:
  248. {
  249. /*
  250. * Synchronize this task's top level page-table
  251. * with the 'reference' page table.
  252. *
  253. * Do _not_ use "tsk" here. We might be inside
  254. * an interrupt in the middle of a task switch..
  255. */
  256. int index = pgd_index(ear0);
  257. pgd_t *pgd, *pgd_k;
  258. pud_t *pud, *pud_k;
  259. pmd_t *pmd, *pmd_k;
  260. pte_t *pte_k;
  261. pgd = (pgd_t *) __get_TTBR();
  262. pgd = (pgd_t *)__va(pgd) + index;
  263. pgd_k = ((pgd_t *)(init_mm.pgd)) + index;
  264. if (!pgd_present(*pgd_k))
  265. goto no_context;
  266. //set_pgd(pgd, *pgd_k); /////// gcc ICE's on this line
  267. pud_k = pud_offset(pgd_k, ear0);
  268. if (!pud_present(*pud_k))
  269. goto no_context;
  270. pmd_k = pmd_offset(pud_k, ear0);
  271. if (!pmd_present(*pmd_k))
  272. goto no_context;
  273. pud = pud_offset(pgd, ear0);
  274. pmd = pmd_offset(pud, ear0);
  275. set_pmd(pmd, *pmd_k);
  276. pte_k = pte_offset_kernel(pmd_k, ear0);
  277. if (!pte_present(*pte_k))
  278. goto no_context;
  279. return;
  280. }
  281. } /* end do_page_fault() */