fault.c 7.2 KB

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