fault.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Page fault handler for SH with an MMU.
  3. *
  4. * Copyright (C) 1999 Niibe Yutaka
  5. * Copyright (C) 2003 - 2007 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/tlbflush.h>
  21. #include <asm/kgdb.h>
  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. int si_code;
  35. siginfo_t info;
  36. trace_hardirqs_on();
  37. local_irq_enable();
  38. #ifdef CONFIG_SH_KGDB
  39. if (kgdb_nofault && kgdb_bus_err_hook)
  40. kgdb_bus_err_hook();
  41. #endif
  42. tsk = current;
  43. mm = tsk->mm;
  44. si_code = SEGV_MAPERR;
  45. if (unlikely(address >= TASK_SIZE)) {
  46. /*
  47. * Synchronize this task's top level page-table
  48. * with the 'reference' page table.
  49. *
  50. * Do _not_ use "tsk" here. We might be inside
  51. * an interrupt in the middle of a task switch..
  52. */
  53. int offset = pgd_index(address);
  54. pgd_t *pgd, *pgd_k;
  55. pud_t *pud, *pud_k;
  56. pmd_t *pmd, *pmd_k;
  57. pgd = get_TTB() + offset;
  58. pgd_k = swapper_pg_dir + offset;
  59. /* This will never happen with the folded page table. */
  60. if (!pgd_present(*pgd)) {
  61. if (!pgd_present(*pgd_k))
  62. goto bad_area_nosemaphore;
  63. set_pgd(pgd, *pgd_k);
  64. return;
  65. }
  66. pud = pud_offset(pgd, address);
  67. pud_k = pud_offset(pgd_k, address);
  68. if (pud_present(*pud) || !pud_present(*pud_k))
  69. goto bad_area_nosemaphore;
  70. set_pud(pud, *pud_k);
  71. pmd = pmd_offset(pud, address);
  72. pmd_k = pmd_offset(pud_k, address);
  73. if (pmd_present(*pmd) || !pmd_present(*pmd_k))
  74. goto bad_area_nosemaphore;
  75. set_pmd(pmd, *pmd_k);
  76. return;
  77. }
  78. /*
  79. * If we're in an interrupt or have no user
  80. * context, we must not take the fault..
  81. */
  82. if (in_atomic() || !mm)
  83. goto no_context;
  84. down_read(&mm->mmap_sem);
  85. vma = find_vma(mm, address);
  86. if (!vma)
  87. goto bad_area;
  88. if (vma->vm_start <= address)
  89. goto good_area;
  90. if (!(vma->vm_flags & VM_GROWSDOWN))
  91. goto bad_area;
  92. if (expand_stack(vma, address))
  93. goto bad_area;
  94. /*
  95. * Ok, we have a good vm_area for this memory access, so
  96. * we can handle it..
  97. */
  98. good_area:
  99. si_code = SEGV_ACCERR;
  100. if (writeaccess) {
  101. if (!(vma->vm_flags & VM_WRITE))
  102. goto bad_area;
  103. } else {
  104. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  105. goto bad_area;
  106. }
  107. /*
  108. * If for any reason at all we couldn't handle the fault,
  109. * make sure we exit gracefully rather than endlessly redo
  110. * the fault.
  111. */
  112. survive:
  113. switch (handle_mm_fault(mm, vma, address, writeaccess)) {
  114. case VM_FAULT_MINOR:
  115. tsk->min_flt++;
  116. break;
  117. case VM_FAULT_MAJOR:
  118. tsk->maj_flt++;
  119. break;
  120. case VM_FAULT_SIGBUS:
  121. goto do_sigbus;
  122. case VM_FAULT_OOM:
  123. goto out_of_memory;
  124. default:
  125. BUG();
  126. }
  127. up_read(&mm->mmap_sem);
  128. return;
  129. /*
  130. * Something tried to access memory that isn't in our memory map..
  131. * Fix it, but check if it's kernel or user first..
  132. */
  133. bad_area:
  134. up_read(&mm->mmap_sem);
  135. bad_area_nosemaphore:
  136. if (user_mode(regs)) {
  137. info.si_signo = SIGSEGV;
  138. info.si_errno = 0;
  139. info.si_code = si_code;
  140. info.si_addr = (void *) address;
  141. force_sig_info(SIGSEGV, &info, tsk);
  142. return;
  143. }
  144. no_context:
  145. /* Are we prepared to handle this kernel fault? */
  146. if (fixup_exception(regs))
  147. return;
  148. /*
  149. * Oops. The kernel tried to access some bad page. We'll have to
  150. * terminate things with extreme prejudice.
  151. *
  152. */
  153. bust_spinlocks(1);
  154. if (oops_may_print()) {
  155. __typeof__(pte_val(__pte(0))) page;
  156. if (address < PAGE_SIZE)
  157. printk(KERN_ALERT "Unable to handle kernel NULL "
  158. "pointer dereference");
  159. else
  160. printk(KERN_ALERT "Unable to handle kernel paging "
  161. "request");
  162. printk(" at virtual address %08lx\n", address);
  163. printk(KERN_ALERT "pc = %08lx\n", regs->pc);
  164. page = (unsigned long)get_TTB();
  165. if (page) {
  166. page = ((__typeof__(page) *) __va(page))[address >>
  167. PGDIR_SHIFT];
  168. printk(KERN_ALERT "*pde = %08lx\n", page);
  169. if (page & _PAGE_PRESENT) {
  170. page &= PAGE_MASK;
  171. address &= 0x003ff000;
  172. page = ((__typeof__(page) *)
  173. __va(page))[address >>
  174. PAGE_SHIFT];
  175. printk(KERN_ALERT "*pte = %08lx\n", page);
  176. }
  177. }
  178. }
  179. die("Oops", regs, writeaccess);
  180. bust_spinlocks(0);
  181. do_exit(SIGKILL);
  182. /*
  183. * We ran out of memory, or some other thing happened to us that made
  184. * us unable to handle the page fault gracefully.
  185. */
  186. out_of_memory:
  187. up_read(&mm->mmap_sem);
  188. if (is_init(current)) {
  189. yield();
  190. down_read(&mm->mmap_sem);
  191. goto survive;
  192. }
  193. printk("VM: killing process %s\n", tsk->comm);
  194. if (user_mode(regs))
  195. do_exit(SIGKILL);
  196. goto no_context;
  197. do_sigbus:
  198. up_read(&mm->mmap_sem);
  199. /*
  200. * Send a sigbus, regardless of whether we were in kernel
  201. * or user mode.
  202. */
  203. info.si_signo = SIGBUS;
  204. info.si_errno = 0;
  205. info.si_code = BUS_ADRERR;
  206. info.si_addr = (void *)address;
  207. force_sig_info(SIGBUS, &info, tsk);
  208. /* Kernel mode? Handle exceptions or die */
  209. if (!user_mode(regs))
  210. goto no_context;
  211. }
  212. #ifdef CONFIG_SH_STORE_QUEUES
  213. /*
  214. * This is a special case for the SH-4 store queues, as pages for this
  215. * space still need to be faulted in before it's possible to flush the
  216. * store queue cache for writeout to the remapped region.
  217. */
  218. #define P3_ADDR_MAX (P4SEG_STORE_QUE + 0x04000000)
  219. #else
  220. #define P3_ADDR_MAX P4SEG
  221. #endif
  222. /*
  223. * Called with interrupts disabled.
  224. */
  225. asmlinkage int __kprobes __do_page_fault(struct pt_regs *regs,
  226. unsigned long writeaccess,
  227. unsigned long address)
  228. {
  229. pgd_t *pgd;
  230. pud_t *pud;
  231. pmd_t *pmd;
  232. pte_t *pte;
  233. pte_t entry;
  234. struct mm_struct *mm = current->mm;
  235. spinlock_t *ptl = NULL;
  236. int ret = 1;
  237. #ifdef CONFIG_SH_KGDB
  238. if (kgdb_nofault && kgdb_bus_err_hook)
  239. kgdb_bus_err_hook();
  240. #endif
  241. /*
  242. * We don't take page faults for P1, P2, and parts of P4, these
  243. * are always mapped, whether it be due to legacy behaviour in
  244. * 29-bit mode, or due to PMB configuration in 32-bit mode.
  245. */
  246. if (address >= P3SEG && address < P3_ADDR_MAX) {
  247. pgd = pgd_offset_k(address);
  248. mm = NULL;
  249. } else {
  250. if (unlikely(address >= TASK_SIZE || !mm))
  251. return 1;
  252. pgd = pgd_offset(mm, address);
  253. }
  254. pud = pud_offset(pgd, address);
  255. if (pud_none_or_clear_bad(pud))
  256. return 1;
  257. pmd = pmd_offset(pud, address);
  258. if (pmd_none_or_clear_bad(pmd))
  259. return 1;
  260. if (mm)
  261. pte = pte_offset_map_lock(mm, pmd, address, &ptl);
  262. else
  263. pte = pte_offset_kernel(pmd, address);
  264. entry = *pte;
  265. if (unlikely(pte_none(entry) || pte_not_present(entry)))
  266. goto unlock;
  267. if (unlikely(writeaccess && !pte_write(entry)))
  268. goto unlock;
  269. if (writeaccess)
  270. entry = pte_mkdirty(entry);
  271. entry = pte_mkyoung(entry);
  272. #ifdef CONFIG_CPU_SH4
  273. /*
  274. * ITLB is not affected by "ldtlb" instruction.
  275. * So, we need to flush the entry by ourselves.
  276. */
  277. local_flush_tlb_one(get_asid(), address & PAGE_MASK);
  278. #endif
  279. set_pte(pte, entry);
  280. update_mmu_cache(NULL, address, entry);
  281. ret = 0;
  282. unlock:
  283. if (mm)
  284. pte_unmap_unlock(pte, ptl);
  285. return ret;
  286. }