fault.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. #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_interrupt() || !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. switch (handle_mm_fault(mm, vma, ear0, write)) {
  149. case VM_FAULT_MINOR:
  150. current->min_flt++;
  151. break;
  152. case VM_FAULT_MAJOR:
  153. current->maj_flt++;
  154. break;
  155. case VM_FAULT_SIGBUS:
  156. goto do_sigbus;
  157. default:
  158. goto out_of_memory;
  159. }
  160. up_read(&mm->mmap_sem);
  161. return;
  162. /*
  163. * Something tried to access memory that isn't in our memory map..
  164. * Fix it, but check if it's kernel or user first..
  165. */
  166. bad_area:
  167. up_read(&mm->mmap_sem);
  168. /* User mode accesses just cause a SIGSEGV */
  169. if (user_mode(__frame)) {
  170. info.si_signo = SIGSEGV;
  171. info.si_errno = 0;
  172. /* info.si_code has been set above */
  173. info.si_addr = (void *) ear0;
  174. force_sig_info(SIGSEGV, &info, current);
  175. return;
  176. }
  177. no_context:
  178. /* are we prepared to handle this kernel fault? */
  179. if ((fixup = search_exception_table(__frame->pc)) != 0) {
  180. __frame->pc = fixup;
  181. return;
  182. }
  183. /*
  184. * Oops. The kernel tried to access some bad page. We'll have to
  185. * terminate things with extreme prejudice.
  186. */
  187. bust_spinlocks(1);
  188. if (ear0 < PAGE_SIZE)
  189. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
  190. else
  191. printk(KERN_ALERT "Unable to handle kernel paging request");
  192. printk(" at virtual addr %08lx\n", ear0);
  193. printk(" PC : %08lx\n", __frame->pc);
  194. printk(" EXC : esr0=%08lx ear0=%08lx\n", esr0, ear0);
  195. asm("lrai %1,%0,#1,#0,#0" : "=&r"(lrai) : "r"(ear0));
  196. asm("lrad %1,%0,#1,#0,#0" : "=&r"(lrad) : "r"(ear0));
  197. printk(KERN_ALERT " LRAI: %08lx\n", lrai);
  198. printk(KERN_ALERT " LRAD: %08lx\n", lrad);
  199. __break_hijack_kernel_event();
  200. pge = pgd_offset(current->mm, ear0);
  201. pue = pud_offset(pge, ear0);
  202. _pme = pue->pue[0].ste[0];
  203. printk(KERN_ALERT " PGE : %8p { PME %08lx }\n", pge, _pme);
  204. if (_pme & xAMPRx_V) {
  205. unsigned long dampr, damlr, val;
  206. asm volatile("movsg dampr2,%0 ! movgs %2,dampr2 ! movsg damlr2,%1"
  207. : "=&r"(dampr), "=r"(damlr)
  208. : "r" (_pme | xAMPRx_L|xAMPRx_SS_16Kb|xAMPRx_S|xAMPRx_C|xAMPRx_V)
  209. );
  210. pte = (pte_t *) damlr + __pte_index(ear0);
  211. val = pte_val(*pte);
  212. asm volatile("movgs %0,dampr2" :: "r" (dampr));
  213. printk(KERN_ALERT " PTE : %8p { %08lx }\n", pte, val);
  214. }
  215. die_if_kernel("Oops\n");
  216. do_exit(SIGKILL);
  217. /*
  218. * We ran out of memory, or some other thing happened to us that made
  219. * us unable to handle the page fault gracefully.
  220. */
  221. out_of_memory:
  222. up_read(&mm->mmap_sem);
  223. printk("VM: killing process %s\n", current->comm);
  224. if (user_mode(__frame))
  225. do_exit(SIGKILL);
  226. goto no_context;
  227. do_sigbus:
  228. up_read(&mm->mmap_sem);
  229. /*
  230. * Send a sigbus, regardless of whether we were in kernel
  231. * or user mode.
  232. */
  233. info.si_signo = SIGBUS;
  234. info.si_errno = 0;
  235. info.si_code = BUS_ADRERR;
  236. info.si_addr = (void *) ear0;
  237. force_sig_info(SIGBUS, &info, current);
  238. /* Kernel mode? Handle exceptions or die */
  239. if (!user_mode(__frame))
  240. goto no_context;
  241. return;
  242. /*
  243. * The fault was caused by a kernel PTE (such as installed by vmalloc or kmap)
  244. */
  245. kernel_pte_fault:
  246. {
  247. /*
  248. * Synchronize this task's top level page-table
  249. * with the 'reference' page table.
  250. *
  251. * Do _not_ use "tsk" here. We might be inside
  252. * an interrupt in the middle of a task switch..
  253. */
  254. int index = pgd_index(ear0);
  255. pgd_t *pgd, *pgd_k;
  256. pud_t *pud, *pud_k;
  257. pmd_t *pmd, *pmd_k;
  258. pte_t *pte_k;
  259. pgd = (pgd_t *) __get_TTBR();
  260. pgd = (pgd_t *)__va(pgd) + index;
  261. pgd_k = ((pgd_t *)(init_mm.pgd)) + index;
  262. if (!pgd_present(*pgd_k))
  263. goto no_context;
  264. //set_pgd(pgd, *pgd_k); /////// gcc ICE's on this line
  265. pud_k = pud_offset(pgd_k, ear0);
  266. if (!pud_present(*pud_k))
  267. goto no_context;
  268. pmd_k = pmd_offset(pud_k, ear0);
  269. if (!pmd_present(*pmd_k))
  270. goto no_context;
  271. pud = pud_offset(pgd, ear0);
  272. pmd = pmd_offset(pud, ear0);
  273. set_pmd(pmd, *pmd_k);
  274. pte_k = pte_offset_kernel(pmd_k, ear0);
  275. if (!pte_present(*pte_k))
  276. goto no_context;
  277. return;
  278. }
  279. } /* end do_page_fault() */