11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds * This file is subject to the terms and conditions of the GNU General Public
31da177e4SLinus Torvalds * License. See the file "COPYING" in the main directory of this archive
41da177e4SLinus Torvalds * for more details.
51da177e4SLinus Torvalds *
61da177e4SLinus Torvalds * Copyright (C) 1995 - 2000 by Ralf Baechle
71da177e4SLinus Torvalds */
8c3fc5cd5SRalf Baechle #include <linux/context_tracking.h>
91da177e4SLinus Torvalds #include <linux/signal.h>
101da177e4SLinus Torvalds #include <linux/sched.h>
111da177e4SLinus Torvalds #include <linux/interrupt.h>
121da177e4SLinus Torvalds #include <linux/kernel.h>
131da177e4SLinus Torvalds #include <linux/errno.h>
141da177e4SLinus Torvalds #include <linux/string.h>
151da177e4SLinus Torvalds #include <linux/types.h>
161da177e4SLinus Torvalds #include <linux/ptrace.h>
17d79d853dSMarkos Chandras #include <linux/ratelimit.h>
181da177e4SLinus Torvalds #include <linux/mman.h>
191da177e4SLinus Torvalds #include <linux/mm.h>
201da177e4SLinus Torvalds #include <linux/smp.h>
21c1bf207dSDavid Daney #include <linux/kprobes.h>
227f788d2dSDeng-Cheng Zhu #include <linux/perf_event.h>
2370ffdb93SDavid Hildenbrand #include <linux/uaccess.h>
241da177e4SLinus Torvalds
251da177e4SLinus Torvalds #include <asm/branch.h>
261da177e4SLinus Torvalds #include <asm/mmu_context.h>
271da177e4SLinus Torvalds #include <asm/ptrace.h>
2816033d61SThiemo Seufer #include <asm/highmem.h> /* For VMALLOC_END */
29c1bf207dSDavid Daney #include <linux/kdebug.h>
301da177e4SLinus Torvalds
31d79d853dSMarkos Chandras int show_unhandled_signals = 1;
32d79d853dSMarkos Chandras
331da177e4SLinus Torvalds /*
341da177e4SLinus Torvalds * This routine handles page faults. It determines the address,
351da177e4SLinus Torvalds * and the problem, and then passes it off to one of the appropriate
361da177e4SLinus Torvalds * routines.
371da177e4SLinus Torvalds */
__do_page_fault(struct pt_regs * regs,unsigned long write,unsigned long address)38f5e2d818STiezhu Yang static void __do_page_fault(struct pt_regs *regs, unsigned long write,
391da177e4SLinus Torvalds unsigned long address)
401da177e4SLinus Torvalds {
411da177e4SLinus Torvalds struct vm_area_struct * vma = NULL;
421da177e4SLinus Torvalds struct task_struct *tsk = current;
431da177e4SLinus Torvalds struct mm_struct *mm = tsk->mm;
441da177e4SLinus Torvalds const int field = sizeof(unsigned long) * 2;
45f43a54a0SEric W. Biederman int si_code;
4650a7ca3cSSouptick Joarder vm_fault_t fault;
47dde16072SPeter Xu unsigned int flags = FAULT_FLAG_DEFAULT;
481da177e4SLinus Torvalds
49d79d853dSMarkos Chandras static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
50d79d853dSMarkos Chandras
511da177e4SLinus Torvalds #if 0
52d6f70360SRalf Baechle printk("Cpu%d[%s:%d:%0*lx:%ld:%0*lx]\n", raw_smp_processor_id(),
531da177e4SLinus Torvalds current->comm, current->pid, field, address, write,
541da177e4SLinus Torvalds field, regs->cp0_epc);
551da177e4SLinus Torvalds #endif
561da177e4SLinus Torvalds
57c1bf207dSDavid Daney #ifdef CONFIG_KPROBES
58c1bf207dSDavid Daney /*
59e3b28831SRalf Baechle * This is to notify the fault handler of the kprobes.
60c1bf207dSDavid Daney */
61c1bf207dSDavid Daney if (notify_die(DIE_PAGE_FAULT, "page fault", regs, -1,
62e3b28831SRalf Baechle current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
63c1bf207dSDavid Daney return;
64c1bf207dSDavid Daney #endif
65c1bf207dSDavid Daney
66f43a54a0SEric W. Biederman si_code = SEGV_MAPERR;
671da177e4SLinus Torvalds
681da177e4SLinus Torvalds /*
691da177e4SLinus Torvalds * We fault-in kernel-space virtual memory on-demand. The
701da177e4SLinus Torvalds * 'reference' page table is init_mm.pgd.
711da177e4SLinus Torvalds *
721da177e4SLinus Torvalds * NOTE! We MUST NOT take any locks for this case. We may
731da177e4SLinus Torvalds * be in an interrupt or a critical region, and should
741da177e4SLinus Torvalds * only copy the information from the master page table,
751da177e4SLinus Torvalds * nothing more.
761da177e4SLinus Torvalds */
772ca2ebfdSDavid Daney #ifdef CONFIG_64BIT
782ca2ebfdSDavid Daney # define VMALLOC_FAULT_TARGET no_context
792ca2ebfdSDavid Daney #else
802ca2ebfdSDavid Daney # define VMALLOC_FAULT_TARGET vmalloc_fault
812ca2ebfdSDavid Daney #endif
822ca2ebfdSDavid Daney
8316033d61SThiemo Seufer if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END))
842ca2ebfdSDavid Daney goto VMALLOC_FAULT_TARGET;
85656be92fSAtsushi Nemoto #ifdef MODULE_START
86656be92fSAtsushi Nemoto if (unlikely(address >= MODULE_START && address < MODULE_END))
872ca2ebfdSDavid Daney goto VMALLOC_FAULT_TARGET;
88656be92fSAtsushi Nemoto #endif
891da177e4SLinus Torvalds
901da177e4SLinus Torvalds /*
911da177e4SLinus Torvalds * If we're in an interrupt or have no user
921da177e4SLinus Torvalds * context, we must not take the fault..
931da177e4SLinus Torvalds */
9470ffdb93SDavid Hildenbrand if (faulthandler_disabled() || !mm)
951da177e4SLinus Torvalds goto bad_area_nosemaphore;
961da177e4SLinus Torvalds
97759496baSJohannes Weiner if (user_mode(regs))
98759496baSJohannes Weiner flags |= FAULT_FLAG_USER;
992558fd7fSPeter Xu
1002558fd7fSPeter Xu perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
10143ca4957SKautuk Consul retry:
10289154dd5SMichel Lespinasse vma = lock_mm_and_find_vma(mm, address, regs);
1031da177e4SLinus Torvalds if (!vma)
1041da177e4SLinus Torvalds goto bad_area_nosemaphore;
1051da177e4SLinus Torvalds /*
1061da177e4SLinus Torvalds * Ok, we have a good vm_area for this memory access, so
1071da177e4SLinus Torvalds * we can handle it..
1081da177e4SLinus Torvalds */
1091da177e4SLinus Torvalds si_code = SEGV_ACCERR;
1101da177e4SLinus Torvalds
1111da177e4SLinus Torvalds if (write) {
1121da177e4SLinus Torvalds if (!(vma->vm_flags & VM_WRITE))
1131da177e4SLinus Torvalds goto bad_area;
1141da177e4SLinus Torvalds flags |= FAULT_FLAG_WRITE;
1151da177e4SLinus Torvalds } else {
1161da177e4SLinus Torvalds if (cpu_has_rixi) {
117f43a54a0SEric W. Biederman if (address == regs->cp0_epc && !(vma->vm_flags & VM_EXEC)) {
1181da177e4SLinus Torvalds #if 0
1191da177e4SLinus Torvalds pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] XI violation\n",
1201da177e4SLinus Torvalds raw_smp_processor_id(),
1211da177e4SLinus Torvalds current->comm, current->pid,
122759496baSJohannes Weiner field, address, write,
1231da177e4SLinus Torvalds field, regs->cp0_epc);
12405857c64SSteven J. Hill #endif
1256dd9344cSDavid Daney goto bad_area;
1266dd9344cSDavid Daney }
1276dd9344cSDavid Daney if (!(vma->vm_flags & VM_READ) &&
1286dd9344cSDavid Daney exception_epc(regs) != address) {
1296dd9344cSDavid Daney #if 0
1306dd9344cSDavid Daney pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] RI violation\n",
1316dd9344cSDavid Daney raw_smp_processor_id(),
1326dd9344cSDavid Daney current->comm, current->pid,
1336dd9344cSDavid Daney field, address, write,
1346dd9344cSDavid Daney field, regs->cp0_epc);
135e070dab7SRalf Baechle #endif
136e070dab7SRalf Baechle goto bad_area;
1376dd9344cSDavid Daney }
1386dd9344cSDavid Daney } else {
1396dd9344cSDavid Daney if (unlikely(!vma_is_accessible(vma)))
1406dd9344cSDavid Daney goto bad_area;
1416dd9344cSDavid Daney }
1426dd9344cSDavid Daney }
1436dd9344cSDavid Daney
1446dd9344cSDavid Daney /*
1456dd9344cSDavid Daney * If for any reason at all we couldn't handle the fault,
1466dd9344cSDavid Daney * make sure we exit gracefully rather than endlessly redo
1475093c587SAnshuman Khandual * the fault.
1481da177e4SLinus Torvalds */
1491da177e4SLinus Torvalds fault = handle_mm_fault(vma, address, flags, regs);
1506dd9344cSDavid Daney
1511da177e4SLinus Torvalds if (fault_signal_pending(fault, regs)) {
1521da177e4SLinus Torvalds if (!user_mode(regs))
1531da177e4SLinus Torvalds goto no_context;
1541da177e4SLinus Torvalds return;
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds
1572558fd7fSPeter Xu /* The fault is fully completed (including releasing mmap lock) */
15843ca4957SKautuk Consul if (fault & VM_FAULT_COMPLETED)
159fceb90bbSThomas Bogendoerfer return;
160fceb90bbSThomas Bogendoerfer
161fceb90bbSThomas Bogendoerfer if (unlikely(fault & VM_FAULT_ERROR)) {
16243ca4957SKautuk Consul if (fault & VM_FAULT_OOM)
163fceb90bbSThomas Bogendoerfer goto out_of_memory;
16443ca4957SKautuk Consul else if (fault & VM_FAULT_SIGSEGV)
165*d9272525SPeter Xu goto bad_area;
166*d9272525SPeter Xu else if (fault & VM_FAULT_SIGBUS)
167*d9272525SPeter Xu goto do_sigbus;
168*d9272525SPeter Xu BUG();
16983c54070SNick Piggin }
17083c54070SNick Piggin
1711da177e4SLinus Torvalds if (fault & VM_FAULT_RETRY) {
17233692f27SLinus Torvalds flags |= FAULT_FLAG_TRIED;
17333692f27SLinus Torvalds
17483c54070SNick Piggin /*
17583c54070SNick Piggin * No need to mmap_read_unlock(mm) as we would
1761da177e4SLinus Torvalds * have already released it in __lock_page_or_retry
1771da177e4SLinus Torvalds * in mm/filemap.c.
17836ef159fSQi Zheng */
17943ca4957SKautuk Consul
18045cac65bSShaohua Li goto retry;
18143ca4957SKautuk Consul }
18243ca4957SKautuk Consul
1833e4e28c5SMichel Lespinasse mmap_read_unlock(mm);
18443ca4957SKautuk Consul return;
18543ca4957SKautuk Consul
18643ca4957SKautuk Consul /*
18743ca4957SKautuk Consul * Something tried to access memory that isn't in our memory map..
18843ca4957SKautuk Consul * Fix it, but check if it's kernel or user first..
18943ca4957SKautuk Consul */
1901da177e4SLinus Torvalds bad_area:
19189154dd5SMichel Lespinasse mmap_read_unlock(mm);
1921da177e4SLinus Torvalds
1931da177e4SLinus Torvalds bad_area_nosemaphore:
1941da177e4SLinus Torvalds /* User mode accesses just cause a SIGSEGV */
1951da177e4SLinus Torvalds if (user_mode(regs)) {
1961da177e4SLinus Torvalds tsk->thread.cp0_badvaddr = address;
1971da177e4SLinus Torvalds tsk->thread.error_code = write;
1981da177e4SLinus Torvalds if (show_unhandled_signals &&
19989154dd5SMichel Lespinasse unhandled_signal(tsk, SIGSEGV) &&
2001da177e4SLinus Torvalds __ratelimit(&ratelimit_state)) {
2011da177e4SLinus Torvalds pr_info("do_page_fault(): sending SIGSEGV to %s for invalid %s %0*lx\n",
2021da177e4SLinus Torvalds tsk->comm,
2031da177e4SLinus Torvalds write ? "write access to" : "read access from",
2041da177e4SLinus Torvalds field, address);
2051da177e4SLinus Torvalds pr_info("epc = %0*lx in", field,
206d79d853dSMarkos Chandras (unsigned long) regs->cp0_epc);
207d79d853dSMarkos Chandras print_vma_addr(KERN_CONT " ", regs->cp0_epc);
208d79d853dSMarkos Chandras pr_cont("\n");
2092a872a5dSMatt Redfearn pr_info("ra = %0*lx in", field,
2101da177e4SLinus Torvalds (unsigned long) regs->regs[31]);
2111da177e4SLinus Torvalds print_vma_addr(KERN_CONT " ", regs->regs[31]);
212d79d853dSMarkos Chandras pr_cont("\n");
213d79d853dSMarkos Chandras }
214d79d853dSMarkos Chandras current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
2152a872a5dSMatt Redfearn force_sig_fault(SIGSEGV, si_code, (void __user *)address);
2162a872a5dSMatt Redfearn return;
217d79d853dSMarkos Chandras }
218d79d853dSMarkos Chandras
2192a872a5dSMatt Redfearn no_context:
2202a872a5dSMatt Redfearn /* Are we prepared to handle this kernel fault? */
221d79d853dSMarkos Chandras if (fixup_exception(regs)) {
222e3b28831SRalf Baechle current->thread.cp0_baduaddr = address;
2232e1661d2SEric W. Biederman return;
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds
2261da177e4SLinus Torvalds /*
2271da177e4SLinus Torvalds * Oops. The kernel tried to access some bad page. We'll have to
2281da177e4SLinus Torvalds * terminate things with extreme prejudice.
2291da177e4SLinus Torvalds */
2301da177e4SLinus Torvalds bust_spinlocks(1);
2311da177e4SLinus Torvalds
2321da177e4SLinus Torvalds printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
2331da177e4SLinus Torvalds "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
2341da177e4SLinus Torvalds raw_smp_processor_id(), field, address, field, regs->cp0_epc,
2351da177e4SLinus Torvalds field, regs->regs[31]);
2361da177e4SLinus Torvalds die("Oops", regs);
2371da177e4SLinus Torvalds
2381da177e4SLinus Torvalds out_of_memory:
2391da177e4SLinus Torvalds /*
2401da177e4SLinus Torvalds * We ran out of memory, call the OOM killer, and return the userspace
2411da177e4SLinus Torvalds * (which will retry the fault, or kill us if we got oom-killed).
242d6f70360SRalf Baechle */
2431da177e4SLinus Torvalds mmap_read_unlock(mm);
2441da177e4SLinus Torvalds if (!user_mode(regs))
2451da177e4SLinus Torvalds goto no_context;
2461da177e4SLinus Torvalds pagefault_out_of_memory();
247c7c1e384SRalf Baechle return;
248c7c1e384SRalf Baechle
249c7c1e384SRalf Baechle do_sigbus:
250c7c1e384SRalf Baechle mmap_read_unlock(mm);
25189154dd5SMichel Lespinasse
25287134102SJohannes Weiner /* Kernel mode? Handle exceptions or die */
25387134102SJohannes Weiner if (!user_mode(regs))
254c7c1e384SRalf Baechle goto no_context;
255c7c1e384SRalf Baechle
2561da177e4SLinus Torvalds /*
2571da177e4SLinus Torvalds * Send a sigbus, regardless of whether we were in kernel
25889154dd5SMichel Lespinasse * or user mode.
2591da177e4SLinus Torvalds */
2601da177e4SLinus Torvalds #if 0
2611da177e4SLinus Torvalds printk("do_page_fault() #3: sending SIGBUS to %s for "
2621da177e4SLinus Torvalds "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
26372f941afSPaul Burton tsk->comm,
2641da177e4SLinus Torvalds write ? "write access to" : "read access from",
2651da177e4SLinus Torvalds field, address,
2661da177e4SLinus Torvalds field, (unsigned long) regs->cp0_epc,
2671da177e4SLinus Torvalds field, (unsigned long) regs->regs[31]);
26841c594abSRalf Baechle #endif
26941c594abSRalf Baechle current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
27041c594abSRalf Baechle tsk->thread.cp0_badvaddr = address;
27141c594abSRalf Baechle force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
27241c594abSRalf Baechle
27341c594abSRalf Baechle return;
27441c594abSRalf Baechle #ifndef CONFIG_64BIT
27541c594abSRalf Baechle vmalloc_fault:
27641c594abSRalf Baechle {
277e3b28831SRalf Baechle /*
2781da177e4SLinus Torvalds * Synchronize this task's top level page-table
2792e1661d2SEric W. Biederman * with the 'reference' page table.
2801da177e4SLinus Torvalds *
2811da177e4SLinus Torvalds * Do _not_ use "tsk" here. We might be inside
2822ca2ebfdSDavid Daney * an interrupt in the middle of a task switch..
2831da177e4SLinus Torvalds */
2841da177e4SLinus Torvalds int offset = pgd_index(address);
2851da177e4SLinus Torvalds pgd_t *pgd, *pgd_k;
2861da177e4SLinus Torvalds p4d_t *p4d, *p4d_k;
2871da177e4SLinus Torvalds pud_t *pud, *pud_k;
2881da177e4SLinus Torvalds pmd_t *pmd, *pmd_k;
2891da177e4SLinus Torvalds pte_t *pte_k;
2901da177e4SLinus Torvalds
2911da177e4SLinus Torvalds pgd = (pgd_t *) pgd_current[raw_smp_processor_id()] + offset;
29231168f03SMike Rapoport pgd_k = init_mm.pgd + offset;
2931da177e4SLinus Torvalds
2942bee1b58SMike Rapoport if (!pgd_present(*pgd_k))
295c6e8b587SRalf Baechle goto no_context;
2961da177e4SLinus Torvalds set_pgd(pgd, *pgd_k);
2971da177e4SLinus Torvalds
2981da177e4SLinus Torvalds p4d = p4d_offset(pgd, address);
299d6f70360SRalf Baechle p4d_k = p4d_offset(pgd_k, address);
3001da177e4SLinus Torvalds if (!p4d_present(*p4d_k))
3011da177e4SLinus Torvalds goto no_context;
3021da177e4SLinus Torvalds
3031da177e4SLinus Torvalds pud = pud_offset(p4d, address);
3041da177e4SLinus Torvalds pud_k = pud_offset(p4d_k, address);
3051da177e4SLinus Torvalds if (!pud_present(*pud_k))
3062bee1b58SMike Rapoport goto no_context;
3072bee1b58SMike Rapoport
3082bee1b58SMike Rapoport pmd = pmd_offset(pud, address);
3092bee1b58SMike Rapoport pmd_k = pmd_offset(pud_k, address);
3102bee1b58SMike Rapoport if (!pmd_present(*pmd_k))
3112bee1b58SMike Rapoport goto no_context;
3122bee1b58SMike Rapoport set_pmd(pmd, *pmd_k);
313c6e8b587SRalf Baechle
314c6e8b587SRalf Baechle pte_k = pte_offset_kernel(pmd_k, address);
315c6e8b587SRalf Baechle if (!pte_present(*pte_k))
316c6e8b587SRalf Baechle goto no_context;
317c6e8b587SRalf Baechle return;
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds #endif
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds NOKPROBE_SYMBOL(__do_page_fault);
3221da177e4SLinus Torvalds
do_page_fault(struct pt_regs * regs,unsigned long write,unsigned long address)3231da177e4SLinus Torvalds asmlinkage void do_page_fault(struct pt_regs *regs,
3241da177e4SLinus Torvalds unsigned long write, unsigned long address)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds enum ctx_state prev_state;
3272ca2ebfdSDavid Daney
3281da177e4SLinus Torvalds prev_state = exception_enter();
329f5e2d818STiezhu Yang __do_page_fault(regs, write, address);
330c3fc5cd5SRalf Baechle exception_exit(prev_state);
331f5e2d818STiezhu Yang }
332c3fc5cd5SRalf Baechle NOKPROBE_SYMBOL(do_page_fault);
333c3fc5cd5SRalf Baechle