fault.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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/kprobes.h>
  22. #include <linux/perf_event.h>
  23. #include <linux/uaccess.h>
  24. #include <asm/branch.h>
  25. #include <asm/mmu_context.h>
  26. #include <asm/ptrace.h>
  27. #include <asm/highmem.h> /* For VMALLOC_END */
  28. #include <asm/traps.h>
  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 __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. int si_code;
  44. vm_fault_t fault;
  45. unsigned int flags = FAULT_FLAG_DEFAULT;
  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.
  55. */
  56. if (notify_die(DIE_PAGE_FAULT, "page fault", regs, -1,
  57. current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
  58. return;
  59. #endif
  60. si_code = SEGV_MAPERR;
  61. /*
  62. * We fault-in kernel-space virtual memory on-demand. The
  63. * 'reference' page table is init_mm.pgd.
  64. *
  65. * NOTE! We MUST NOT take any locks for this case. We may
  66. * be in an interrupt or a critical region, and should
  67. * only copy the information from the master page table,
  68. * nothing more.
  69. */
  70. #ifdef CONFIG_64BIT
  71. # define VMALLOC_FAULT_TARGET no_context
  72. #else
  73. # define VMALLOC_FAULT_TARGET vmalloc_fault
  74. #endif
  75. if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END))
  76. goto VMALLOC_FAULT_TARGET;
  77. #ifdef MODULES_VADDR
  78. if (unlikely(address >= MODULES_VADDR && address < MODULES_END))
  79. goto VMALLOC_FAULT_TARGET;
  80. #endif
  81. /*
  82. * If we're in an interrupt or have no user
  83. * context, we must not take the fault..
  84. */
  85. if (faulthandler_disabled() || !mm)
  86. goto bad_area_nosemaphore;
  87. if (user_mode(regs))
  88. flags |= FAULT_FLAG_USER;
  89. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  90. retry:
  91. vma = lock_mm_and_find_vma(mm, address, regs);
  92. if (!vma)
  93. goto bad_area_nosemaphore;
  94. /*
  95. * Ok, we have a good vm_area for this memory access, so
  96. * we can handle it..
  97. */
  98. si_code = SEGV_ACCERR;
  99. if (write) {
  100. if (!(vma->vm_flags & VM_WRITE))
  101. goto bad_area;
  102. flags |= FAULT_FLAG_WRITE;
  103. } else {
  104. if (cpu_has_rixi) {
  105. if (address == regs->cp0_epc && !(vma->vm_flags & VM_EXEC)) {
  106. #if 0
  107. pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] XI violation\n",
  108. raw_smp_processor_id(),
  109. current->comm, current->pid,
  110. field, address, write,
  111. field, regs->cp0_epc);
  112. #endif
  113. goto bad_area;
  114. }
  115. if (!(vma->vm_flags & VM_READ) &&
  116. exception_epc(regs) != address) {
  117. #if 0
  118. pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] RI violation\n",
  119. raw_smp_processor_id(),
  120. current->comm, current->pid,
  121. field, address, write,
  122. field, regs->cp0_epc);
  123. #endif
  124. goto bad_area;
  125. }
  126. } else {
  127. if (unlikely(!vma_is_accessible(vma)))
  128. goto bad_area;
  129. }
  130. }
  131. /*
  132. * If for any reason at all we couldn't handle the fault,
  133. * make sure we exit gracefully rather than endlessly redo
  134. * the fault.
  135. */
  136. fault = handle_mm_fault(vma, address, flags, regs);
  137. if (fault_signal_pending(fault, regs)) {
  138. if (!user_mode(regs))
  139. goto no_context;
  140. return;
  141. }
  142. /* The fault is fully completed (including releasing mmap lock) */
  143. if (fault & VM_FAULT_COMPLETED)
  144. return;
  145. if (unlikely(fault & VM_FAULT_ERROR)) {
  146. if (fault & VM_FAULT_OOM)
  147. goto out_of_memory;
  148. else if (fault & VM_FAULT_SIGSEGV)
  149. goto bad_area;
  150. else if (fault & VM_FAULT_SIGBUS)
  151. goto do_sigbus;
  152. BUG();
  153. }
  154. if (fault & VM_FAULT_RETRY) {
  155. flags |= FAULT_FLAG_TRIED;
  156. /*
  157. * No need to mmap_read_unlock(mm) as we would
  158. * have already released it in __lock_page_or_retry
  159. * in mm/filemap.c.
  160. */
  161. goto retry;
  162. }
  163. mmap_read_unlock(mm);
  164. return;
  165. /*
  166. * Something tried to access memory that isn't in our memory map..
  167. * Fix it, but check if it's kernel or user first..
  168. */
  169. bad_area:
  170. mmap_read_unlock(mm);
  171. bad_area_nosemaphore:
  172. /* User mode accesses just cause a SIGSEGV */
  173. if (user_mode(regs)) {
  174. tsk->thread.cp0_badvaddr = address;
  175. tsk->thread.error_code = write;
  176. if (show_unhandled_signals &&
  177. unhandled_signal(tsk, SIGSEGV) &&
  178. __ratelimit(&ratelimit_state)) {
  179. pr_info("do_page_fault(): sending SIGSEGV to %s for invalid %s %0*lx\n",
  180. tsk->comm,
  181. write ? "write access to" : "read access from",
  182. field, address);
  183. pr_info("epc = %0*lx in", field,
  184. (unsigned long) regs->cp0_epc);
  185. print_vma_addr(KERN_CONT " ", regs->cp0_epc);
  186. pr_cont("\n");
  187. pr_info("ra = %0*lx in", field,
  188. (unsigned long) regs->regs[31]);
  189. print_vma_addr(KERN_CONT " ", regs->regs[31]);
  190. pr_cont("\n");
  191. }
  192. current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
  193. force_sig_fault(SIGSEGV, si_code, (void __user *)address);
  194. return;
  195. }
  196. no_context:
  197. /* Are we prepared to handle this kernel fault? */
  198. if (fixup_exception(regs)) {
  199. current->thread.cp0_baduaddr = address;
  200. return;
  201. }
  202. /*
  203. * Oops. The kernel tried to access some bad page. We'll have to
  204. * terminate things with extreme prejudice.
  205. */
  206. bust_spinlocks(1);
  207. printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
  208. "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
  209. raw_smp_processor_id(), field, address, field, regs->cp0_epc,
  210. field, regs->regs[31]);
  211. die("Oops", regs);
  212. out_of_memory:
  213. /*
  214. * We ran out of memory, call the OOM killer, and return the userspace
  215. * (which will retry the fault, or kill us if we got oom-killed).
  216. */
  217. mmap_read_unlock(mm);
  218. if (!user_mode(regs))
  219. goto no_context;
  220. pagefault_out_of_memory();
  221. return;
  222. do_sigbus:
  223. mmap_read_unlock(mm);
  224. /* Kernel mode? Handle exceptions or die */
  225. if (!user_mode(regs))
  226. goto no_context;
  227. /*
  228. * Send a sigbus, regardless of whether we were in kernel
  229. * or user mode.
  230. */
  231. #if 0
  232. printk("do_page_fault() #3: sending SIGBUS to %s for "
  233. "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
  234. tsk->comm,
  235. write ? "write access to" : "read access from",
  236. field, address,
  237. field, (unsigned long) regs->cp0_epc,
  238. field, (unsigned long) regs->regs[31]);
  239. #endif
  240. current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
  241. tsk->thread.cp0_badvaddr = address;
  242. force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
  243. return;
  244. #ifndef CONFIG_64BIT
  245. vmalloc_fault:
  246. {
  247. /*
  248. * Synchronize this task's top level page-table
  249. * with the 'reference' page table.
  250. *
  251. * Do _not_ use "tsk" here. We might be inside
  252. * an interrupt in the middle of a task switch..
  253. */
  254. int offset = pgd_index(address);
  255. pgd_t *pgd, *pgd_k;
  256. p4d_t *p4d, *p4d_k;
  257. pud_t *pud, *pud_k;
  258. pmd_t *pmd, *pmd_k;
  259. pte_t *pte_k;
  260. pgd = (pgd_t *) pgd_current[raw_smp_processor_id()] + offset;
  261. pgd_k = init_mm.pgd + offset;
  262. if (!pgd_present(*pgd_k))
  263. goto no_context;
  264. set_pgd(pgd, *pgd_k);
  265. p4d = p4d_offset(pgd, address);
  266. p4d_k = p4d_offset(pgd_k, address);
  267. if (!p4d_present(*p4d_k))
  268. goto no_context;
  269. pud = pud_offset(p4d, address);
  270. pud_k = pud_offset(p4d_k, address);
  271. if (!pud_present(*pud_k))
  272. goto no_context;
  273. pmd = pmd_offset(pud, address);
  274. pmd_k = pmd_offset(pud_k, address);
  275. if (!pmd_present(*pmd_k))
  276. goto no_context;
  277. set_pmd(pmd, *pmd_k);
  278. pte_k = pte_offset_kernel(pmd_k, address);
  279. if (!pte_present(*pte_k))
  280. goto no_context;
  281. return;
  282. }
  283. #endif
  284. }
  285. NOKPROBE_SYMBOL(__do_page_fault);
  286. asmlinkage void do_page_fault(struct pt_regs *regs,
  287. unsigned long write, unsigned long address)
  288. {
  289. enum ctx_state prev_state;
  290. prev_state = exception_enter();
  291. __do_page_fault(regs, write, address);
  292. exception_exit(prev_state);
  293. }
  294. NOKPROBE_SYMBOL(do_page_fault);