fault_32.c 7.3 KB

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