fault.c 8.3 KB

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