fault_32.c 7.4 KB

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