fault.c 5.9 KB

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