fault.c 8.0 KB

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