fault_32.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Page fault handler for SH with an MMU.
  3. *
  4. * Copyright (C) 1999 Niibe Yutaka
  5. * Copyright (C) 2003 - 2009 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/perf_event.h>
  19. #include <asm/io_trapped.h>
  20. #include <asm/mmu_context.h>
  21. #include <asm/tlbflush.h>
  22. #include <asm/traps.h>
  23. static inline int notify_page_fault(struct pt_regs *regs, int trap)
  24. {
  25. int ret = 0;
  26. if (kprobes_built_in() && !user_mode(regs)) {
  27. preempt_disable();
  28. if (kprobe_running() && kprobe_fault_handler(regs, trap))
  29. ret = 1;
  30. preempt_enable();
  31. }
  32. return ret;
  33. }
  34. static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
  35. {
  36. unsigned index = pgd_index(address);
  37. pgd_t *pgd_k;
  38. pud_t *pud, *pud_k;
  39. pmd_t *pmd, *pmd_k;
  40. pgd += index;
  41. pgd_k = init_mm.pgd + index;
  42. if (!pgd_present(*pgd_k))
  43. return NULL;
  44. pud = pud_offset(pgd, address);
  45. pud_k = pud_offset(pgd_k, address);
  46. if (!pud_present(*pud_k))
  47. return NULL;
  48. if (!pud_present(*pud))
  49. set_pud(pud, *pud_k);
  50. pmd = pmd_offset(pud, address);
  51. pmd_k = pmd_offset(pud_k, address);
  52. if (!pmd_present(*pmd_k))
  53. return NULL;
  54. if (!pmd_present(*pmd))
  55. set_pmd(pmd, *pmd_k);
  56. else {
  57. /*
  58. * The page tables are fully synchronised so there must
  59. * be another reason for the fault. Return NULL here to
  60. * signal that we have not taken care of the fault.
  61. */
  62. BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
  63. return NULL;
  64. }
  65. return pmd_k;
  66. }
  67. /*
  68. * Handle a fault on the vmalloc or module mapping area
  69. */
  70. static noinline int vmalloc_fault(unsigned long address)
  71. {
  72. pgd_t *pgd_k;
  73. pmd_t *pmd_k;
  74. pte_t *pte_k;
  75. /* Make sure we are in vmalloc/module/P3 area: */
  76. if (!(address >= VMALLOC_START && address < P3_ADDR_MAX))
  77. return -1;
  78. /*
  79. * Synchronize this task's top level page-table
  80. * with the 'reference' page table.
  81. *
  82. * Do _not_ use "current" here. We might be inside
  83. * an interrupt in the middle of a task switch..
  84. */
  85. pgd_k = get_TTB();
  86. pmd_k = vmalloc_sync_one(pgd_k, address);
  87. if (!pmd_k)
  88. return -1;
  89. pte_k = pte_offset_kernel(pmd_k, address);
  90. if (!pte_present(*pte_k))
  91. return -1;
  92. return 0;
  93. }
  94. static int fault_in_kernel_space(unsigned long address)
  95. {
  96. return address >= TASK_SIZE;
  97. }
  98. /*
  99. * This routine handles page faults. It determines the address,
  100. * and the problem, and then passes it off to one of the appropriate
  101. * routines.
  102. */
  103. asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
  104. unsigned long writeaccess,
  105. unsigned long address)
  106. {
  107. unsigned long vec;
  108. struct task_struct *tsk;
  109. struct mm_struct *mm;
  110. struct vm_area_struct * vma;
  111. int si_code;
  112. int fault;
  113. siginfo_t info;
  114. unsigned int flags = (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
  115. (writeaccess ? FAULT_FLAG_WRITE : 0));
  116. tsk = current;
  117. mm = tsk->mm;
  118. si_code = SEGV_MAPERR;
  119. vec = lookup_exception_vector();
  120. /*
  121. * We fault-in kernel-space virtual memory on-demand. The
  122. * 'reference' page table is init_mm.pgd.
  123. *
  124. * NOTE! We MUST NOT take any locks for this case. We may
  125. * be in an interrupt or a critical region, and should
  126. * only copy the information from the master page table,
  127. * nothing more.
  128. */
  129. if (unlikely(fault_in_kernel_space(address))) {
  130. if (vmalloc_fault(address) >= 0)
  131. return;
  132. if (notify_page_fault(regs, vec))
  133. return;
  134. goto bad_area_nosemaphore;
  135. }
  136. if (unlikely(notify_page_fault(regs, vec)))
  137. return;
  138. /* Only enable interrupts if they were on before the fault */
  139. if ((regs->sr & SR_IMASK) != SR_IMASK)
  140. local_irq_enable();
  141. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  142. /*
  143. * If we're in an interrupt, have no user context or are running
  144. * in an atomic region then we must not take the fault:
  145. */
  146. if (in_atomic() || !mm)
  147. goto no_context;
  148. retry:
  149. down_read(&mm->mmap_sem);
  150. vma = find_vma(mm, address);
  151. if (!vma)
  152. goto bad_area;
  153. if (vma->vm_start <= address)
  154. goto good_area;
  155. if (!(vma->vm_flags & VM_GROWSDOWN))
  156. goto bad_area;
  157. if (expand_stack(vma, address))
  158. goto bad_area;
  159. /*
  160. * Ok, we have a good vm_area for this memory access, so
  161. * we can handle it..
  162. */
  163. good_area:
  164. si_code = SEGV_ACCERR;
  165. if (writeaccess) {
  166. if (!(vma->vm_flags & VM_WRITE))
  167. goto bad_area;
  168. } else {
  169. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  170. goto bad_area;
  171. }
  172. /*
  173. * If for any reason at all we couldn't handle the fault,
  174. * make sure we exit gracefully rather than endlessly redo
  175. * the fault.
  176. */
  177. fault = handle_mm_fault(mm, vma, address, flags);
  178. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  179. return;
  180. if (unlikely(fault & VM_FAULT_ERROR)) {
  181. if (fault & VM_FAULT_OOM)
  182. goto out_of_memory;
  183. else if (fault & VM_FAULT_SIGBUS)
  184. goto do_sigbus;
  185. BUG();
  186. }
  187. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  188. if (fault & VM_FAULT_MAJOR) {
  189. tsk->maj_flt++;
  190. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  191. regs, address);
  192. } else {
  193. tsk->min_flt++;
  194. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  195. regs, address);
  196. }
  197. if (fault & VM_FAULT_RETRY) {
  198. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  199. /*
  200. * No need to up_read(&mm->mmap_sem) as we would
  201. * have already released it in __lock_page_or_retry
  202. * in mm/filemap.c.
  203. */
  204. goto retry;
  205. }
  206. }
  207. up_read(&mm->mmap_sem);
  208. return;
  209. /*
  210. * Something tried to access memory that isn't in our memory map..
  211. * Fix it, but check if it's kernel or user first..
  212. */
  213. bad_area:
  214. up_read(&mm->mmap_sem);
  215. bad_area_nosemaphore:
  216. if (user_mode(regs)) {
  217. info.si_signo = SIGSEGV;
  218. info.si_errno = 0;
  219. info.si_code = si_code;
  220. info.si_addr = (void *) address;
  221. force_sig_info(SIGSEGV, &info, tsk);
  222. return;
  223. }
  224. no_context:
  225. /* Are we prepared to handle this kernel fault? */
  226. if (fixup_exception(regs))
  227. return;
  228. if (handle_trapped_io(regs, address))
  229. return;
  230. /*
  231. * Oops. The kernel tried to access some bad page. We'll have to
  232. * terminate things with extreme prejudice.
  233. *
  234. */
  235. bust_spinlocks(1);
  236. if (oops_may_print()) {
  237. unsigned long page;
  238. if (address < PAGE_SIZE)
  239. printk(KERN_ALERT "Unable to handle kernel NULL "
  240. "pointer dereference");
  241. else
  242. printk(KERN_ALERT "Unable to handle kernel paging "
  243. "request");
  244. printk(" at virtual address %08lx\n", address);
  245. printk(KERN_ALERT "pc = %08lx\n", regs->pc);
  246. page = (unsigned long)get_TTB();
  247. if (page) {
  248. page = ((__typeof__(page) *)page)[address >> PGDIR_SHIFT];
  249. printk(KERN_ALERT "*pde = %08lx\n", page);
  250. if (page & _PAGE_PRESENT) {
  251. page &= PAGE_MASK;
  252. address &= 0x003ff000;
  253. page = ((__typeof__(page) *)
  254. __va(page))[address >>
  255. PAGE_SHIFT];
  256. printk(KERN_ALERT "*pte = %08lx\n", page);
  257. }
  258. }
  259. }
  260. die("Oops", regs, writeaccess);
  261. bust_spinlocks(0);
  262. do_exit(SIGKILL);
  263. /*
  264. * We ran out of memory, or some other thing happened to us that made
  265. * us unable to handle the page fault gracefully.
  266. */
  267. out_of_memory:
  268. up_read(&mm->mmap_sem);
  269. if (!user_mode(regs))
  270. goto no_context;
  271. pagefault_out_of_memory();
  272. return;
  273. do_sigbus:
  274. up_read(&mm->mmap_sem);
  275. /*
  276. * Send a sigbus, regardless of whether we were in kernel
  277. * or user mode.
  278. */
  279. info.si_signo = SIGBUS;
  280. info.si_errno = 0;
  281. info.si_code = BUS_ADRERR;
  282. info.si_addr = (void *)address;
  283. force_sig_info(SIGBUS, &info, tsk);
  284. /* Kernel mode? Handle exceptions or die */
  285. if (!user_mode(regs))
  286. goto no_context;
  287. }
  288. /*
  289. * Called with interrupts disabled.
  290. */
  291. asmlinkage int __kprobes
  292. handle_tlbmiss(struct pt_regs *regs, unsigned long writeaccess,
  293. unsigned long address)
  294. {
  295. pgd_t *pgd;
  296. pud_t *pud;
  297. pmd_t *pmd;
  298. pte_t *pte;
  299. pte_t entry;
  300. /*
  301. * We don't take page faults for P1, P2, and parts of P4, these
  302. * are always mapped, whether it be due to legacy behaviour in
  303. * 29-bit mode, or due to PMB configuration in 32-bit mode.
  304. */
  305. if (address >= P3SEG && address < P3_ADDR_MAX) {
  306. pgd = pgd_offset_k(address);
  307. } else {
  308. if (unlikely(address >= TASK_SIZE || !current->mm))
  309. return 1;
  310. pgd = pgd_offset(current->mm, address);
  311. }
  312. pud = pud_offset(pgd, address);
  313. if (pud_none_or_clear_bad(pud))
  314. return 1;
  315. pmd = pmd_offset(pud, address);
  316. if (pmd_none_or_clear_bad(pmd))
  317. return 1;
  318. pte = pte_offset_kernel(pmd, address);
  319. entry = *pte;
  320. if (unlikely(pte_none(entry) || pte_not_present(entry)))
  321. return 1;
  322. if (unlikely(writeaccess && !pte_write(entry)))
  323. return 1;
  324. if (writeaccess)
  325. entry = pte_mkdirty(entry);
  326. entry = pte_mkyoung(entry);
  327. set_pte(pte, entry);
  328. #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SMP)
  329. /*
  330. * SH-4 does not set MMUCR.RC to the corresponding TLB entry in
  331. * the case of an initial page write exception, so we need to
  332. * flush it in order to avoid potential TLB entry duplication.
  333. */
  334. if (writeaccess == 2)
  335. local_flush_tlb_one(get_asid(), address & PAGE_MASK);
  336. #endif
  337. update_mmu_cache(NULL, address, pte);
  338. return 0;
  339. }