fault_32.c 7.6 KB

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