fault_32.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 >= P3SEG && 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. tsk = current;
  115. mm = tsk->mm;
  116. si_code = SEGV_MAPERR;
  117. vec = lookup_exception_vector();
  118. /*
  119. * We fault-in kernel-space virtual memory on-demand. The
  120. * 'reference' page table is init_mm.pgd.
  121. *
  122. * NOTE! We MUST NOT take any locks for this case. We may
  123. * be in an interrupt or a critical region, and should
  124. * only copy the information from the master page table,
  125. * nothing more.
  126. */
  127. if (unlikely(fault_in_kernel_space(address))) {
  128. if (vmalloc_fault(address) >= 0)
  129. return;
  130. if (notify_page_fault(regs, vec))
  131. return;
  132. goto bad_area_nosemaphore;
  133. }
  134. if (unlikely(notify_page_fault(regs, vec)))
  135. return;
  136. /* Only enable interrupts if they were on before the fault */
  137. if ((regs->sr & SR_IMASK) != SR_IMASK)
  138. local_irq_enable();
  139. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  140. /*
  141. * If we're in an interrupt, have no user context or are running
  142. * in an atomic region then we must not take the fault:
  143. */
  144. if (in_atomic() || !mm)
  145. goto no_context;
  146. down_read(&mm->mmap_sem);
  147. vma = find_vma(mm, address);
  148. if (!vma)
  149. goto bad_area;
  150. if (vma->vm_start <= address)
  151. goto good_area;
  152. if (!(vma->vm_flags & VM_GROWSDOWN))
  153. goto bad_area;
  154. if (expand_stack(vma, address))
  155. goto bad_area;
  156. /*
  157. * Ok, we have a good vm_area for this memory access, so
  158. * we can handle it..
  159. */
  160. good_area:
  161. si_code = SEGV_ACCERR;
  162. if (writeaccess) {
  163. if (!(vma->vm_flags & VM_WRITE))
  164. goto bad_area;
  165. } else {
  166. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  167. goto bad_area;
  168. }
  169. /*
  170. * If for any reason at all we couldn't handle the fault,
  171. * make sure we exit gracefully rather than endlessly redo
  172. * the fault.
  173. */
  174. fault = handle_mm_fault(mm, vma, address, writeaccess ? FAULT_FLAG_WRITE : 0);
  175. if (unlikely(fault & VM_FAULT_ERROR)) {
  176. if (fault & VM_FAULT_OOM)
  177. goto out_of_memory;
  178. else if (fault & VM_FAULT_SIGBUS)
  179. goto do_sigbus;
  180. else if (fault & VM_FAULT_SIGSEGV)
  181. goto bad_area;
  182. BUG();
  183. }
  184. if (fault & VM_FAULT_MAJOR) {
  185. tsk->maj_flt++;
  186. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  187. regs, address);
  188. } else {
  189. tsk->min_flt++;
  190. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  191. regs, address);
  192. }
  193. up_read(&mm->mmap_sem);
  194. return;
  195. /*
  196. * Something tried to access memory that isn't in our memory map..
  197. * Fix it, but check if it's kernel or user first..
  198. */
  199. bad_area:
  200. up_read(&mm->mmap_sem);
  201. bad_area_nosemaphore:
  202. if (user_mode(regs)) {
  203. info.si_signo = SIGSEGV;
  204. info.si_errno = 0;
  205. info.si_code = si_code;
  206. info.si_addr = (void *) address;
  207. force_sig_info(SIGSEGV, &info, tsk);
  208. return;
  209. }
  210. no_context:
  211. /* Are we prepared to handle this kernel fault? */
  212. if (fixup_exception(regs))
  213. return;
  214. if (handle_trapped_io(regs, address))
  215. return;
  216. /*
  217. * Oops. The kernel tried to access some bad page. We'll have to
  218. * terminate things with extreme prejudice.
  219. *
  220. */
  221. bust_spinlocks(1);
  222. if (oops_may_print()) {
  223. unsigned long page;
  224. if (address < PAGE_SIZE)
  225. printk(KERN_ALERT "Unable to handle kernel NULL "
  226. "pointer dereference");
  227. else
  228. printk(KERN_ALERT "Unable to handle kernel paging "
  229. "request");
  230. printk(" at virtual address %08lx\n", address);
  231. printk(KERN_ALERT "pc = %08lx\n", regs->pc);
  232. page = (unsigned long)get_TTB();
  233. if (page) {
  234. page = ((__typeof__(page) *)page)[address >> PGDIR_SHIFT];
  235. printk(KERN_ALERT "*pde = %08lx\n", page);
  236. if (page & _PAGE_PRESENT) {
  237. page &= PAGE_MASK;
  238. address &= 0x003ff000;
  239. page = ((__typeof__(page) *)
  240. __va(page))[address >>
  241. PAGE_SHIFT];
  242. printk(KERN_ALERT "*pte = %08lx\n", page);
  243. }
  244. }
  245. }
  246. die("Oops", regs, writeaccess);
  247. bust_spinlocks(0);
  248. do_exit(SIGKILL);
  249. /*
  250. * We ran out of memory, or some other thing happened to us that made
  251. * us unable to handle the page fault gracefully.
  252. */
  253. out_of_memory:
  254. up_read(&mm->mmap_sem);
  255. if (!user_mode(regs))
  256. goto no_context;
  257. pagefault_out_of_memory();
  258. return;
  259. do_sigbus:
  260. up_read(&mm->mmap_sem);
  261. /*
  262. * Send a sigbus, regardless of whether we were in kernel
  263. * or user mode.
  264. */
  265. info.si_signo = SIGBUS;
  266. info.si_errno = 0;
  267. info.si_code = BUS_ADRERR;
  268. info.si_addr = (void *)address;
  269. force_sig_info(SIGBUS, &info, tsk);
  270. /* Kernel mode? Handle exceptions or die */
  271. if (!user_mode(regs))
  272. goto no_context;
  273. }
  274. /*
  275. * Called with interrupts disabled.
  276. */
  277. asmlinkage int __kprobes
  278. handle_tlbmiss(struct pt_regs *regs, unsigned long writeaccess,
  279. unsigned long address)
  280. {
  281. pgd_t *pgd;
  282. pud_t *pud;
  283. pmd_t *pmd;
  284. pte_t *pte;
  285. pte_t entry;
  286. /*
  287. * We don't take page faults for P1, P2, and parts of P4, these
  288. * are always mapped, whether it be due to legacy behaviour in
  289. * 29-bit mode, or due to PMB configuration in 32-bit mode.
  290. */
  291. if (address >= P3SEG && address < P3_ADDR_MAX) {
  292. pgd = pgd_offset_k(address);
  293. } else {
  294. if (unlikely(address >= TASK_SIZE || !current->mm))
  295. return 1;
  296. pgd = pgd_offset(current->mm, address);
  297. }
  298. pud = pud_offset(pgd, address);
  299. if (pud_none_or_clear_bad(pud))
  300. return 1;
  301. pmd = pmd_offset(pud, address);
  302. if (pmd_none_or_clear_bad(pmd))
  303. return 1;
  304. pte = pte_offset_kernel(pmd, address);
  305. entry = *pte;
  306. if (unlikely(pte_none(entry) || pte_not_present(entry)))
  307. return 1;
  308. if (unlikely(writeaccess && !pte_write(entry)))
  309. return 1;
  310. if (writeaccess)
  311. entry = pte_mkdirty(entry);
  312. entry = pte_mkyoung(entry);
  313. set_pte(pte, entry);
  314. #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SMP)
  315. /*
  316. * SH-4 does not set MMUCR.RC to the corresponding TLB entry in
  317. * the case of an initial page write exception, so we need to
  318. * flush it in order to avoid potential TLB entry duplication.
  319. */
  320. if (writeaccess == 2)
  321. local_flush_tlb_one(get_asid(), address & PAGE_MASK);
  322. #endif
  323. update_mmu_cache(NULL, address, pte);
  324. return 0;
  325. }