fault.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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/signal.h>
  9. #include <linux/sched.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/string.h>
  14. #include <linux/types.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/mman.h>
  17. #include <linux/mm.h>
  18. #include <linux/smp.h>
  19. #include <linux/module.h>
  20. #include <linux/kprobes.h>
  21. #include <linux/perf_event.h>
  22. #include <asm/branch.h>
  23. #include <asm/mmu_context.h>
  24. #include <asm/system.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/ptrace.h>
  27. #include <asm/highmem.h> /* For VMALLOC_END */
  28. #include <linux/kdebug.h>
  29. /*
  30. * This routine handles page faults. It determines the address,
  31. * and the problem, and then passes it off to one of the appropriate
  32. * routines.
  33. */
  34. asmlinkage void __kprobes do_page_fault(struct pt_regs *regs, unsigned long write,
  35. unsigned long address)
  36. {
  37. struct vm_area_struct * vma = NULL;
  38. struct task_struct *tsk = current;
  39. struct mm_struct *mm = tsk->mm;
  40. const int field = sizeof(unsigned long) * 2;
  41. siginfo_t info;
  42. int fault;
  43. #if 0
  44. printk("Cpu%d[%s:%d:%0*lx:%ld:%0*lx]\n", raw_smp_processor_id(),
  45. current->comm, current->pid, field, address, write,
  46. field, regs->cp0_epc);
  47. #endif
  48. #ifdef CONFIG_KPROBES
  49. /*
  50. * This is to notify the fault handler of the kprobes. The
  51. * exception code is redundant as it is also carried in REGS,
  52. * but we pass it anyhow.
  53. */
  54. if (notify_die(DIE_PAGE_FAULT, "page fault", regs, -1,
  55. (regs->cp0_cause >> 2) & 0x1f, SIGSEGV) == NOTIFY_STOP)
  56. return;
  57. #endif
  58. info.si_code = SEGV_MAPERR;
  59. /*
  60. * We fault-in kernel-space virtual memory on-demand. The
  61. * 'reference' page table is init_mm.pgd.
  62. *
  63. * NOTE! We MUST NOT take any locks for this case. We may
  64. * be in an interrupt or a critical region, and should
  65. * only copy the information from the master page table,
  66. * nothing more.
  67. */
  68. #ifdef CONFIG_64BIT
  69. # define VMALLOC_FAULT_TARGET no_context
  70. #else
  71. # define VMALLOC_FAULT_TARGET vmalloc_fault
  72. #endif
  73. if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END))
  74. goto VMALLOC_FAULT_TARGET;
  75. #ifdef MODULE_START
  76. if (unlikely(address >= MODULE_START && address < MODULE_END))
  77. goto VMALLOC_FAULT_TARGET;
  78. #endif
  79. /*
  80. * If we're in an interrupt or have no user
  81. * context, we must not take the fault..
  82. */
  83. if (in_atomic() || !mm)
  84. goto bad_area_nosemaphore;
  85. down_read(&mm->mmap_sem);
  86. vma = find_vma(mm, address);
  87. if (!vma)
  88. goto bad_area;
  89. if (vma->vm_start <= address)
  90. goto good_area;
  91. if (!(vma->vm_flags & VM_GROWSDOWN))
  92. goto bad_area;
  93. if (expand_stack(vma, address))
  94. goto bad_area;
  95. /*
  96. * Ok, we have a good vm_area for this memory access, so
  97. * we can handle it..
  98. */
  99. good_area:
  100. info.si_code = SEGV_ACCERR;
  101. if (write) {
  102. if (!(vma->vm_flags & VM_WRITE))
  103. goto bad_area;
  104. } else {
  105. if (kernel_uses_smartmips_rixi) {
  106. if (address == regs->cp0_epc && !(vma->vm_flags & VM_EXEC)) {
  107. #if 0
  108. pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] XI violation\n",
  109. raw_smp_processor_id(),
  110. current->comm, current->pid,
  111. field, address, write,
  112. field, regs->cp0_epc);
  113. #endif
  114. goto bad_area;
  115. }
  116. if (!(vma->vm_flags & VM_READ)) {
  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 (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
  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(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
  137. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  138. if (unlikely(fault & VM_FAULT_ERROR)) {
  139. if (fault & VM_FAULT_OOM)
  140. goto out_of_memory;
  141. else if (fault & VM_FAULT_SIGBUS)
  142. goto do_sigbus;
  143. BUG();
  144. }
  145. if (fault & VM_FAULT_MAJOR) {
  146. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
  147. tsk->maj_flt++;
  148. } else {
  149. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
  150. tsk->min_flt++;
  151. }
  152. up_read(&mm->mmap_sem);
  153. return;
  154. /*
  155. * Something tried to access memory that isn't in our memory map..
  156. * Fix it, but check if it's kernel or user first..
  157. */
  158. bad_area:
  159. up_read(&mm->mmap_sem);
  160. bad_area_nosemaphore:
  161. /* User mode accesses just cause a SIGSEGV */
  162. if (user_mode(regs)) {
  163. tsk->thread.cp0_badvaddr = address;
  164. tsk->thread.error_code = write;
  165. #if 0
  166. printk("do_page_fault() #2: sending SIGSEGV to %s for "
  167. "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
  168. tsk->comm,
  169. write ? "write access to" : "read access from",
  170. field, address,
  171. field, (unsigned long) regs->cp0_epc,
  172. field, (unsigned long) regs->regs[31]);
  173. #endif
  174. info.si_signo = SIGSEGV;
  175. info.si_errno = 0;
  176. /* info.si_code has been set above */
  177. info.si_addr = (void __user *) address;
  178. force_sig_info(SIGSEGV, &info, tsk);
  179. return;
  180. }
  181. no_context:
  182. /* Are we prepared to handle this kernel fault? */
  183. if (fixup_exception(regs)) {
  184. current->thread.cp0_baduaddr = address;
  185. return;
  186. }
  187. /*
  188. * Oops. The kernel tried to access some bad page. We'll have to
  189. * terminate things with extreme prejudice.
  190. */
  191. bust_spinlocks(1);
  192. printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
  193. "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
  194. raw_smp_processor_id(), field, address, field, regs->cp0_epc,
  195. field, regs->regs[31]);
  196. die("Oops", regs);
  197. out_of_memory:
  198. /*
  199. * We ran out of memory, call the OOM killer, and return the userspace
  200. * (which will retry the fault, or kill us if we got oom-killed).
  201. */
  202. up_read(&mm->mmap_sem);
  203. pagefault_out_of_memory();
  204. return;
  205. do_sigbus:
  206. up_read(&mm->mmap_sem);
  207. /* Kernel mode? Handle exceptions or die */
  208. if (!user_mode(regs))
  209. goto no_context;
  210. else
  211. /*
  212. * Send a sigbus, regardless of whether we were in kernel
  213. * or user mode.
  214. */
  215. #if 0
  216. printk("do_page_fault() #3: sending SIGBUS to %s for "
  217. "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
  218. tsk->comm,
  219. write ? "write access to" : "read access from",
  220. field, address,
  221. field, (unsigned long) regs->cp0_epc,
  222. field, (unsigned long) regs->regs[31]);
  223. #endif
  224. tsk->thread.cp0_badvaddr = address;
  225. info.si_signo = SIGBUS;
  226. info.si_errno = 0;
  227. info.si_code = BUS_ADRERR;
  228. info.si_addr = (void __user *) address;
  229. force_sig_info(SIGBUS, &info, tsk);
  230. return;
  231. #ifndef CONFIG_64BIT
  232. vmalloc_fault:
  233. {
  234. /*
  235. * Synchronize this task's top level page-table
  236. * with the 'reference' page table.
  237. *
  238. * Do _not_ use "tsk" here. We might be inside
  239. * an interrupt in the middle of a task switch..
  240. */
  241. int offset = __pgd_offset(address);
  242. pgd_t *pgd, *pgd_k;
  243. pud_t *pud, *pud_k;
  244. pmd_t *pmd, *pmd_k;
  245. pte_t *pte_k;
  246. pgd = (pgd_t *) pgd_current[raw_smp_processor_id()] + offset;
  247. pgd_k = init_mm.pgd + offset;
  248. if (!pgd_present(*pgd_k))
  249. goto no_context;
  250. set_pgd(pgd, *pgd_k);
  251. pud = pud_offset(pgd, address);
  252. pud_k = pud_offset(pgd_k, address);
  253. if (!pud_present(*pud_k))
  254. goto no_context;
  255. pmd = pmd_offset(pud, address);
  256. pmd_k = pmd_offset(pud_k, address);
  257. if (!pmd_present(*pmd_k))
  258. goto no_context;
  259. set_pmd(pmd, *pmd_k);
  260. pte_k = pte_offset_kernel(pmd_k, address);
  261. if (!pte_present(*pte_k))
  262. goto no_context;
  263. return;
  264. }
  265. #endif
  266. }