fault_32.c 7.2 KB

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