fault_32.c 7.7 KB

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