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