fault.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Page fault handler for SH with an MMU.
  3. *
  4. * Copyright (C) 1999 Niibe Yutaka
  5. * Copyright (C) 2003 Paul Mundt
  6. *
  7. * Based on linux/arch/i386/mm/fault.c:
  8. * Copyright (C) 1995 Linus Torvalds
  9. *
  10. * This file is subject to the terms and conditions of the GNU General Public
  11. * License. See the file "COPYING" in the main directory of this archive
  12. * for more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/hardirq.h>
  17. #include <linux/kprobes.h>
  18. #include <asm/system.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/kgdb.h>
  21. extern void die(const char *,struct pt_regs *,long);
  22. /*
  23. * This routine handles page faults. It determines the address,
  24. * and the problem, and then passes it off to one of the appropriate
  25. * routines.
  26. */
  27. asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
  28. unsigned long writeaccess,
  29. unsigned long address)
  30. {
  31. struct task_struct *tsk;
  32. struct mm_struct *mm;
  33. struct vm_area_struct * vma;
  34. unsigned long page;
  35. int si_code;
  36. siginfo_t info;
  37. #ifdef CONFIG_SH_KGDB
  38. if (kgdb_nofault && kgdb_bus_err_hook)
  39. kgdb_bus_err_hook();
  40. #endif
  41. tsk = current;
  42. mm = tsk->mm;
  43. si_code = SEGV_MAPERR;
  44. /*
  45. * If we're in an interrupt or have no user
  46. * context, we must not take the fault..
  47. */
  48. if (in_atomic() || !mm)
  49. goto no_context;
  50. down_read(&mm->mmap_sem);
  51. vma = find_vma(mm, address);
  52. if (!vma)
  53. goto bad_area;
  54. if (vma->vm_start <= address)
  55. goto good_area;
  56. if (!(vma->vm_flags & VM_GROWSDOWN))
  57. goto bad_area;
  58. if (expand_stack(vma, address))
  59. goto bad_area;
  60. /*
  61. * Ok, we have a good vm_area for this memory access, so
  62. * we can handle it..
  63. */
  64. good_area:
  65. si_code = SEGV_ACCERR;
  66. if (writeaccess) {
  67. if (!(vma->vm_flags & VM_WRITE))
  68. goto bad_area;
  69. } else {
  70. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  71. goto bad_area;
  72. }
  73. /*
  74. * If for any reason at all we couldn't handle the fault,
  75. * make sure we exit gracefully rather than endlessly redo
  76. * the fault.
  77. */
  78. survive:
  79. switch (handle_mm_fault(mm, vma, address, writeaccess)) {
  80. case VM_FAULT_MINOR:
  81. tsk->min_flt++;
  82. break;
  83. case VM_FAULT_MAJOR:
  84. tsk->maj_flt++;
  85. break;
  86. case VM_FAULT_SIGBUS:
  87. goto do_sigbus;
  88. case VM_FAULT_OOM:
  89. goto out_of_memory;
  90. default:
  91. BUG();
  92. }
  93. up_read(&mm->mmap_sem);
  94. return;
  95. /*
  96. * Something tried to access memory that isn't in our memory map..
  97. * Fix it, but check if it's kernel or user first..
  98. */
  99. bad_area:
  100. up_read(&mm->mmap_sem);
  101. if (user_mode(regs)) {
  102. info.si_signo = SIGSEGV;
  103. info.si_errno = 0;
  104. info.si_code = si_code;
  105. info.si_addr = (void *) address;
  106. force_sig_info(SIGSEGV, &info, tsk);
  107. return;
  108. }
  109. no_context:
  110. /* Are we prepared to handle this kernel fault? */
  111. if (fixup_exception(regs))
  112. return;
  113. /*
  114. * Oops. The kernel tried to access some bad page. We'll have to
  115. * terminate things with extreme prejudice.
  116. *
  117. */
  118. if (address < PAGE_SIZE)
  119. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
  120. else
  121. printk(KERN_ALERT "Unable to handle kernel paging request");
  122. printk(" at virtual address %08lx\n", address);
  123. printk(KERN_ALERT "pc = %08lx\n", regs->pc);
  124. asm volatile("mov.l %1, %0"
  125. : "=r" (page)
  126. : "m" (__m(MMU_TTB)));
  127. if (page) {
  128. page = ((unsigned long *) page)[address >> 22];
  129. printk(KERN_ALERT "*pde = %08lx\n", page);
  130. if (page & _PAGE_PRESENT) {
  131. page &= PAGE_MASK;
  132. address &= 0x003ff000;
  133. page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
  134. printk(KERN_ALERT "*pte = %08lx\n", page);
  135. }
  136. }
  137. die("Oops", regs, writeaccess);
  138. do_exit(SIGKILL);
  139. /*
  140. * We ran out of memory, or some other thing happened to us that made
  141. * us unable to handle the page fault gracefully.
  142. */
  143. out_of_memory:
  144. up_read(&mm->mmap_sem);
  145. if (is_init(current)) {
  146. yield();
  147. down_read(&mm->mmap_sem);
  148. goto survive;
  149. }
  150. printk("VM: killing process %s\n", tsk->comm);
  151. if (user_mode(regs))
  152. do_exit(SIGKILL);
  153. goto no_context;
  154. do_sigbus:
  155. up_read(&mm->mmap_sem);
  156. /*
  157. * Send a sigbus, regardless of whether we were in kernel
  158. * or user mode.
  159. */
  160. info.si_signo = SIGBUS;
  161. info.si_errno = 0;
  162. info.si_code = BUS_ADRERR;
  163. info.si_addr = (void *)address;
  164. force_sig_info(SIGBUS, &info, tsk);
  165. /* Kernel mode? Handle exceptions or die */
  166. if (!user_mode(regs))
  167. goto no_context;
  168. }
  169. #ifdef CONFIG_SH_STORE_QUEUES
  170. /*
  171. * This is a special case for the SH-4 store queues, as pages for this
  172. * space still need to be faulted in before it's possible to flush the
  173. * store queue cache for writeout to the remapped region.
  174. */
  175. #define P3_ADDR_MAX (P4SEG_STORE_QUE + 0x04000000)
  176. #else
  177. #define P3_ADDR_MAX P4SEG
  178. #endif
  179. /*
  180. * Called with interrupts disabled.
  181. */
  182. asmlinkage int __kprobes __do_page_fault(struct pt_regs *regs,
  183. unsigned long writeaccess,
  184. unsigned long address)
  185. {
  186. pgd_t *pgd;
  187. pud_t *pud;
  188. pmd_t *pmd;
  189. pte_t *pte;
  190. pte_t entry;
  191. struct mm_struct *mm = current->mm;
  192. spinlock_t *ptl;
  193. int ret = 1;
  194. #ifdef CONFIG_SH_KGDB
  195. if (kgdb_nofault && kgdb_bus_err_hook)
  196. kgdb_bus_err_hook();
  197. #endif
  198. /*
  199. * We don't take page faults for P1, P2, and parts of P4, these
  200. * are always mapped, whether it be due to legacy behaviour in
  201. * 29-bit mode, or due to PMB configuration in 32-bit mode.
  202. */
  203. if (address >= P3SEG && address < P3_ADDR_MAX) {
  204. pgd = pgd_offset_k(address);
  205. mm = NULL;
  206. } else {
  207. if (unlikely(address >= TASK_SIZE || !mm))
  208. return 1;
  209. pgd = pgd_offset(mm, address);
  210. }
  211. pud = pud_offset(pgd, address);
  212. if (pud_none_or_clear_bad(pud))
  213. return 1;
  214. pmd = pmd_offset(pud, address);
  215. if (pmd_none_or_clear_bad(pmd))
  216. return 1;
  217. if (mm)
  218. pte = pte_offset_map_lock(mm, pmd, address, &ptl);
  219. else
  220. pte = pte_offset_kernel(pmd, address);
  221. entry = *pte;
  222. if (unlikely(pte_none(entry) || pte_not_present(entry)))
  223. goto unlock;
  224. if (unlikely(writeaccess && !pte_write(entry)))
  225. goto unlock;
  226. if (writeaccess)
  227. entry = pte_mkdirty(entry);
  228. entry = pte_mkyoung(entry);
  229. #ifdef CONFIG_CPU_SH4
  230. /*
  231. * ITLB is not affected by "ldtlb" instruction.
  232. * So, we need to flush the entry by ourselves.
  233. */
  234. __flush_tlb_page(get_asid(), address & PAGE_MASK);
  235. #endif
  236. set_pte(pte, entry);
  237. update_mmu_cache(NULL, address, entry);
  238. ret = 0;
  239. unlock:
  240. if (mm)
  241. pte_unmap_unlock(pte, ptl);
  242. return ret;
  243. }