fault.c 7.9 KB

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