fault.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1995 - 2000 by Ralf Baechle
  7. */
  8. #include <linux/context_tracking.h>
  9. #include <linux/signal.h>
  10. #include <linux/sched.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/string.h>
  15. #include <linux/types.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/ratelimit.h>
  18. #include <linux/mman.h>
  19. #include <linux/mm.h>
  20. #include <linux/smp.h>
  21. #include <linux/module.h>
  22. #include <linux/kprobes.h>
  23. #include <linux/perf_event.h>
  24. #include <linux/uaccess.h>
  25. #include <asm/branch.h>
  26. #include <asm/mmu_context.h>
  27. #include <asm/ptrace.h>
  28. #include <asm/highmem.h> /* For VMALLOC_END */
  29. #include <linux/kdebug.h>
  30. int show_unhandled_signals = 1;
  31. /*
  32. * This routine handles page faults. It determines the address,
  33. * and the problem, and then passes it off to one of the appropriate
  34. * routines.
  35. */
  36. static void __kprobes __do_page_fault(struct pt_regs *regs, unsigned long write,
  37. unsigned long address)
  38. {
  39. struct vm_area_struct * vma = NULL;
  40. struct task_struct *tsk = current;
  41. struct mm_struct *mm = tsk->mm;
  42. const int field = sizeof(unsigned long) * 2;
  43. siginfo_t info;
  44. int fault;
  45. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  46. static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
  47. #if 0
  48. printk("Cpu%d[%s:%d:%0*lx:%ld:%0*lx]\n", raw_smp_processor_id(),
  49. current->comm, current->pid, field, address, write,
  50. field, regs->cp0_epc);
  51. #endif
  52. #ifdef CONFIG_KPROBES
  53. /*
  54. * This is to notify the fault handler of the kprobes. The
  55. * exception code is redundant as it is also carried in REGS,
  56. * but we pass it anyhow.
  57. */
  58. if (notify_die(DIE_PAGE_FAULT, "page fault", regs, -1,
  59. (regs->cp0_cause >> 2) & 0x1f, SIGSEGV) == NOTIFY_STOP)
  60. return;
  61. #endif
  62. info.si_code = SEGV_MAPERR;
  63. /*
  64. * We fault-in kernel-space virtual memory on-demand. The
  65. * 'reference' page table is init_mm.pgd.
  66. *
  67. * NOTE! We MUST NOT take any locks for this case. We may
  68. * be in an interrupt or a critical region, and should
  69. * only copy the information from the master page table,
  70. * nothing more.
  71. */
  72. #ifdef CONFIG_64BIT
  73. # define VMALLOC_FAULT_TARGET no_context
  74. #else
  75. # define VMALLOC_FAULT_TARGET vmalloc_fault
  76. #endif
  77. if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END))
  78. goto VMALLOC_FAULT_TARGET;
  79. #ifdef MODULE_START
  80. if (unlikely(address >= MODULE_START && address < MODULE_END))
  81. goto VMALLOC_FAULT_TARGET;
  82. #endif
  83. /*
  84. * If we're in an interrupt or have no user
  85. * context, we must not take the fault..
  86. */
  87. if (faulthandler_disabled() || !mm)
  88. goto bad_area_nosemaphore;
  89. if (user_mode(regs))
  90. flags |= FAULT_FLAG_USER;
  91. retry:
  92. down_read(&mm->mmap_sem);
  93. vma = find_vma(mm, address);
  94. if (!vma)
  95. goto bad_area;
  96. if (vma->vm_start <= address)
  97. goto good_area;
  98. if (!(vma->vm_flags & VM_GROWSDOWN))
  99. goto bad_area;
  100. if (expand_stack(vma, address))
  101. goto bad_area;
  102. /*
  103. * Ok, we have a good vm_area for this memory access, so
  104. * we can handle it..
  105. */
  106. good_area:
  107. info.si_code = SEGV_ACCERR;
  108. if (write) {
  109. if (!(vma->vm_flags & VM_WRITE))
  110. goto bad_area;
  111. flags |= FAULT_FLAG_WRITE;
  112. } else {
  113. if (cpu_has_rixi) {
  114. if (address == regs->cp0_epc && !(vma->vm_flags & VM_EXEC)) {
  115. #if 0
  116. pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] XI violation\n",
  117. raw_smp_processor_id(),
  118. current->comm, current->pid,
  119. field, address, write,
  120. field, regs->cp0_epc);
  121. #endif
  122. goto bad_area;
  123. }
  124. if (!(vma->vm_flags & VM_READ) &&
  125. exception_epc(regs) != address) {
  126. #if 0
  127. pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] RI violation\n",
  128. raw_smp_processor_id(),
  129. current->comm, current->pid,
  130. field, address, write,
  131. field, regs->cp0_epc);
  132. #endif
  133. goto bad_area;
  134. }
  135. } else {
  136. if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
  137. goto bad_area;
  138. }
  139. }
  140. /*
  141. * If for any reason at all we couldn't handle the fault,
  142. * make sure we exit gracefully rather than endlessly redo
  143. * the fault.
  144. */
  145. fault = handle_mm_fault(mm, vma, address, flags);
  146. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  147. return;
  148. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  149. if (unlikely(fault & VM_FAULT_ERROR)) {
  150. if (fault & VM_FAULT_OOM)
  151. goto out_of_memory;
  152. else if (fault & VM_FAULT_SIGSEGV)
  153. goto bad_area;
  154. else if (fault & VM_FAULT_SIGBUS)
  155. goto do_sigbus;
  156. BUG();
  157. }
  158. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  159. if (fault & VM_FAULT_MAJOR) {
  160. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  161. regs, address);
  162. tsk->maj_flt++;
  163. } else {
  164. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  165. regs, address);
  166. tsk->min_flt++;
  167. }
  168. if (fault & VM_FAULT_RETRY) {
  169. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  170. flags |= FAULT_FLAG_TRIED;
  171. /*
  172. * No need to up_read(&mm->mmap_sem) as we would
  173. * have already released it in __lock_page_or_retry
  174. * in mm/filemap.c.
  175. */
  176. goto retry;
  177. }
  178. }
  179. up_read(&mm->mmap_sem);
  180. return;
  181. /*
  182. * Something tried to access memory that isn't in our memory map..
  183. * Fix it, but check if it's kernel or user first..
  184. */
  185. bad_area:
  186. up_read(&mm->mmap_sem);
  187. bad_area_nosemaphore:
  188. /* User mode accesses just cause a SIGSEGV */
  189. if (user_mode(regs)) {
  190. tsk->thread.cp0_badvaddr = address;
  191. tsk->thread.error_code = write;
  192. if (show_unhandled_signals &&
  193. unhandled_signal(tsk, SIGSEGV) &&
  194. __ratelimit(&ratelimit_state)) {
  195. pr_info("\ndo_page_fault(): sending SIGSEGV to %s for invalid %s %0*lx",
  196. tsk->comm,
  197. write ? "write access to" : "read access from",
  198. field, address);
  199. pr_info("epc = %0*lx in", field,
  200. (unsigned long) regs->cp0_epc);
  201. print_vma_addr(" ", regs->cp0_epc);
  202. pr_info("ra = %0*lx in", field,
  203. (unsigned long) regs->regs[31]);
  204. print_vma_addr(" ", regs->regs[31]);
  205. pr_info("\n");
  206. }
  207. info.si_signo = SIGSEGV;
  208. info.si_errno = 0;
  209. /* info.si_code has been set above */
  210. info.si_addr = (void __user *) address;
  211. force_sig_info(SIGSEGV, &info, tsk);
  212. return;
  213. }
  214. no_context:
  215. /* Are we prepared to handle this kernel fault? */
  216. if (fixup_exception(regs)) {
  217. current->thread.cp0_baduaddr = address;
  218. return;
  219. }
  220. /*
  221. * Oops. The kernel tried to access some bad page. We'll have to
  222. * terminate things with extreme prejudice.
  223. */
  224. bust_spinlocks(1);
  225. printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
  226. "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
  227. raw_smp_processor_id(), field, address, field, regs->cp0_epc,
  228. field, regs->regs[31]);
  229. die("Oops", regs);
  230. out_of_memory:
  231. /*
  232. * We ran out of memory, call the OOM killer, and return the userspace
  233. * (which will retry the fault, or kill us if we got oom-killed).
  234. */
  235. up_read(&mm->mmap_sem);
  236. if (!user_mode(regs))
  237. goto no_context;
  238. pagefault_out_of_memory();
  239. return;
  240. do_sigbus:
  241. up_read(&mm->mmap_sem);
  242. /* Kernel mode? Handle exceptions or die */
  243. if (!user_mode(regs))
  244. goto no_context;
  245. else
  246. /*
  247. * Send a sigbus, regardless of whether we were in kernel
  248. * or user mode.
  249. */
  250. #if 0
  251. printk("do_page_fault() #3: sending SIGBUS to %s for "
  252. "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
  253. tsk->comm,
  254. write ? "write access to" : "read access from",
  255. field, address,
  256. field, (unsigned long) regs->cp0_epc,
  257. field, (unsigned long) regs->regs[31]);
  258. #endif
  259. tsk->thread.cp0_badvaddr = address;
  260. info.si_signo = SIGBUS;
  261. info.si_errno = 0;
  262. info.si_code = BUS_ADRERR;
  263. info.si_addr = (void __user *) address;
  264. force_sig_info(SIGBUS, &info, tsk);
  265. return;
  266. #ifndef CONFIG_64BIT
  267. vmalloc_fault:
  268. {
  269. /*
  270. * Synchronize this task's top level page-table
  271. * with the 'reference' page table.
  272. *
  273. * Do _not_ use "tsk" here. We might be inside
  274. * an interrupt in the middle of a task switch..
  275. */
  276. int offset = __pgd_offset(address);
  277. pgd_t *pgd, *pgd_k;
  278. pud_t *pud, *pud_k;
  279. pmd_t *pmd, *pmd_k;
  280. pte_t *pte_k;
  281. pgd = (pgd_t *) pgd_current[raw_smp_processor_id()] + offset;
  282. pgd_k = init_mm.pgd + offset;
  283. if (!pgd_present(*pgd_k))
  284. goto no_context;
  285. set_pgd(pgd, *pgd_k);
  286. pud = pud_offset(pgd, address);
  287. pud_k = pud_offset(pgd_k, address);
  288. if (!pud_present(*pud_k))
  289. goto no_context;
  290. pmd = pmd_offset(pud, address);
  291. pmd_k = pmd_offset(pud_k, address);
  292. if (!pmd_present(*pmd_k))
  293. goto no_context;
  294. set_pmd(pmd, *pmd_k);
  295. pte_k = pte_offset_kernel(pmd_k, address);
  296. if (!pte_present(*pte_k))
  297. goto no_context;
  298. return;
  299. }
  300. #endif
  301. }
  302. asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
  303. unsigned long write, unsigned long address)
  304. {
  305. enum ctx_state prev_state;
  306. prev_state = exception_enter();
  307. __do_page_fault(regs, write, address);
  308. exception_exit(prev_state);
  309. }