fault.c 7.2 KB

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