fault_32.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. 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. fault = handle_mm_fault(mm, vma, address, writeaccess);
  116. if (unlikely(fault & VM_FAULT_ERROR)) {
  117. if (fault & VM_FAULT_OOM)
  118. goto out_of_memory;
  119. else if (fault & VM_FAULT_SIGBUS)
  120. goto do_sigbus;
  121. BUG();
  122. }
  123. if (fault & VM_FAULT_MAJOR)
  124. tsk->maj_flt++;
  125. else
  126. tsk->min_flt++;
  127. up_read(&mm->mmap_sem);
  128. return;
  129. /*
  130. * Something tried to access memory that isn't in our memory map..
  131. * Fix it, but check if it's kernel or user first..
  132. */
  133. bad_area:
  134. up_read(&mm->mmap_sem);
  135. bad_area_nosemaphore:
  136. if (user_mode(regs)) {
  137. info.si_signo = SIGSEGV;
  138. info.si_errno = 0;
  139. info.si_code = si_code;
  140. info.si_addr = (void *) address;
  141. force_sig_info(SIGSEGV, &info, tsk);
  142. return;
  143. }
  144. no_context:
  145. /* Are we prepared to handle this kernel fault? */
  146. if (fixup_exception(regs))
  147. return;
  148. if (handle_trapped_io(regs, address))
  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. bust_spinlocks(1);
  156. if (oops_may_print()) {
  157. unsigned long page;
  158. if (address < PAGE_SIZE)
  159. printk(KERN_ALERT "Unable to handle kernel NULL "
  160. "pointer dereference");
  161. else
  162. printk(KERN_ALERT "Unable to handle kernel paging "
  163. "request");
  164. printk(" at virtual address %08lx\n", address);
  165. printk(KERN_ALERT "pc = %08lx\n", regs->pc);
  166. page = (unsigned long)get_TTB();
  167. if (page) {
  168. page = ((__typeof__(page) *)page)[address >> PGDIR_SHIFT];
  169. printk(KERN_ALERT "*pde = %08lx\n", page);
  170. if (page & _PAGE_PRESENT) {
  171. page &= PAGE_MASK;
  172. address &= 0x003ff000;
  173. page = ((__typeof__(page) *)
  174. __va(page))[address >>
  175. PAGE_SHIFT];
  176. printk(KERN_ALERT "*pte = %08lx\n", page);
  177. }
  178. }
  179. }
  180. die("Oops", regs, writeaccess);
  181. bust_spinlocks(0);
  182. do_exit(SIGKILL);
  183. /*
  184. * We ran out of memory, or some other thing happened to us that made
  185. * us unable to handle the page fault gracefully.
  186. */
  187. out_of_memory:
  188. up_read(&mm->mmap_sem);
  189. if (is_global_init(current)) {
  190. yield();
  191. down_read(&mm->mmap_sem);
  192. goto survive;
  193. }
  194. printk("VM: killing process %s\n", tsk->comm);
  195. if (user_mode(regs))
  196. do_group_exit(SIGKILL);
  197. goto no_context;
  198. do_sigbus:
  199. up_read(&mm->mmap_sem);
  200. /*
  201. * Send a sigbus, regardless of whether we were in kernel
  202. * or user mode.
  203. */
  204. info.si_signo = SIGBUS;
  205. info.si_errno = 0;
  206. info.si_code = BUS_ADRERR;
  207. info.si_addr = (void *)address;
  208. force_sig_info(SIGBUS, &info, tsk);
  209. /* Kernel mode? Handle exceptions or die */
  210. if (!user_mode(regs))
  211. goto no_context;
  212. }
  213. #ifdef CONFIG_SH_STORE_QUEUES
  214. /*
  215. * This is a special case for the SH-4 store queues, as pages for this
  216. * space still need to be faulted in before it's possible to flush the
  217. * store queue cache for writeout to the remapped region.
  218. */
  219. #define P3_ADDR_MAX (P4SEG_STORE_QUE + 0x04000000)
  220. #else
  221. #define P3_ADDR_MAX P4SEG
  222. #endif
  223. /*
  224. * Called with interrupts disabled.
  225. */
  226. asmlinkage int __kprobes __do_page_fault(struct pt_regs *regs,
  227. unsigned long writeaccess,
  228. unsigned long address)
  229. {
  230. pgd_t *pgd;
  231. pud_t *pud;
  232. pmd_t *pmd;
  233. pte_t *pte;
  234. pte_t entry;
  235. #ifdef CONFIG_SH_KGDB
  236. if (kgdb_nofault && kgdb_bus_err_hook)
  237. kgdb_bus_err_hook();
  238. #endif
  239. /*
  240. * We don't take page faults for P1, P2, and parts of P4, these
  241. * are always mapped, whether it be due to legacy behaviour in
  242. * 29-bit mode, or due to PMB configuration in 32-bit mode.
  243. */
  244. if (address >= P3SEG && address < P3_ADDR_MAX) {
  245. pgd = pgd_offset_k(address);
  246. } else {
  247. if (unlikely(address >= TASK_SIZE || !current->mm))
  248. return 1;
  249. pgd = pgd_offset(current->mm, address);
  250. }
  251. pud = pud_offset(pgd, address);
  252. if (pud_none_or_clear_bad(pud))
  253. return 1;
  254. pmd = pmd_offset(pud, address);
  255. if (pmd_none_or_clear_bad(pmd))
  256. return 1;
  257. pte = pte_offset_kernel(pmd, address);
  258. entry = *pte;
  259. if (unlikely(pte_none(entry) || pte_not_present(entry)))
  260. return 1;
  261. if (unlikely(writeaccess && !pte_write(entry)))
  262. return 1;
  263. if (writeaccess)
  264. entry = pte_mkdirty(entry);
  265. entry = pte_mkyoung(entry);
  266. #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SMP)
  267. /*
  268. * ITLB is not affected by "ldtlb" instruction.
  269. * So, we need to flush the entry by ourselves.
  270. */
  271. local_flush_tlb_one(get_asid(), address & PAGE_MASK);
  272. #endif
  273. set_pte(pte, entry);
  274. update_mmu_cache(NULL, address, entry);
  275. return 0;
  276. }