fault.c 7.1 KB

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