12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
261e85e36SJonas Bonn /*
361e85e36SJonas Bonn * OpenRISC fault.c
461e85e36SJonas Bonn *
561e85e36SJonas Bonn * Linux architectural port borrowing liberally from similar works of
661e85e36SJonas Bonn * others. All original copyrights apply as per the original source
761e85e36SJonas Bonn * declaration.
861e85e36SJonas Bonn *
961e85e36SJonas Bonn * Modifications for the OpenRISC architecture:
1061e85e36SJonas Bonn * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
1161e85e36SJonas Bonn * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
1261e85e36SJonas Bonn */
1361e85e36SJonas Bonn
1461e85e36SJonas Bonn #include <linux/mm.h>
1561e85e36SJonas Bonn #include <linux/interrupt.h>
16ce139ab8SPaul Gortmaker #include <linux/extable.h>
173f07c014SIngo Molnar #include <linux/sched/signal.h>
1838caa902SPeter Xu #include <linux/perf_event.h>
1961e85e36SJonas Bonn
207c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
21*31c67b5fSStafford Horne #include <asm/bug.h>
22d8fee3f6SStafford Horne #include <asm/mmu_context.h>
2361e85e36SJonas Bonn #include <asm/siginfo.h>
2461e85e36SJonas Bonn #include <asm/signal.h>
2561e85e36SJonas Bonn
2661e85e36SJonas Bonn #define NUM_TLB_ENTRIES 64
2761e85e36SJonas Bonn #define TLB_OFFSET(add) (((add) >> PAGE_SHIFT) & (NUM_TLB_ENTRIES-1))
2861e85e36SJonas Bonn
2961e85e36SJonas Bonn /* __PHX__ :: - check the vmalloc_fault in do_page_fault()
3011648cbbSRandy Dunlap * - also look into include/asm/mmu_context.h
3161e85e36SJonas Bonn */
328e6d08e0SStefan Kristiansson volatile pgd_t *current_pgd[NR_CPUS];
3361e85e36SJonas Bonn
341c4de499SStafford Horne asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address,
351c4de499SStafford Horne unsigned long vector, int write_acc);
361c4de499SStafford Horne
3761e85e36SJonas Bonn /*
3861e85e36SJonas Bonn * This routine handles page faults. It determines the address,
3961e85e36SJonas Bonn * and the problem, and then passes it off to one of the appropriate
4061e85e36SJonas Bonn * routines.
4161e85e36SJonas Bonn *
4261e85e36SJonas Bonn * If this routine detects a bad access, it returns 1, otherwise it
4361e85e36SJonas Bonn * returns 0.
4461e85e36SJonas Bonn */
4561e85e36SJonas Bonn
do_page_fault(struct pt_regs * regs,unsigned long address,unsigned long vector,int write_acc)4661e85e36SJonas Bonn asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address,
4761e85e36SJonas Bonn unsigned long vector, int write_acc)
4861e85e36SJonas Bonn {
4961e85e36SJonas Bonn struct task_struct *tsk;
5061e85e36SJonas Bonn struct mm_struct *mm;
5161e85e36SJonas Bonn struct vm_area_struct *vma;
5275bfb9a1SEric W. Biederman int si_code;
5350a7ca3cSSouptick Joarder vm_fault_t fault;
54dde16072SPeter Xu unsigned int flags = FAULT_FLAG_DEFAULT;
5561e85e36SJonas Bonn
5661e85e36SJonas Bonn tsk = current;
5761e85e36SJonas Bonn
5861e85e36SJonas Bonn /*
5961e85e36SJonas Bonn * We fault-in kernel-space virtual memory on-demand. The
6061e85e36SJonas Bonn * 'reference' page table is init_mm.pgd.
6161e85e36SJonas Bonn *
6261e85e36SJonas Bonn * NOTE! We MUST NOT take any locks for this case. We may
6361e85e36SJonas Bonn * be in an interrupt or a critical region, and should
6461e85e36SJonas Bonn * only copy the information from the master page table,
6561e85e36SJonas Bonn * nothing more.
6661e85e36SJonas Bonn *
6761e85e36SJonas Bonn * NOTE2: This is done so that, when updating the vmalloc
6861e85e36SJonas Bonn * mappings we don't have to walk all processes pgdirs and
6961e85e36SJonas Bonn * add the high mappings all at once. Instead we do it as they
7061e85e36SJonas Bonn * are used. However vmalloc'ed page entries have the PAGE_GLOBAL
7161e85e36SJonas Bonn * bit set so sometimes the TLB can use a lingering entry.
7261e85e36SJonas Bonn *
7361e85e36SJonas Bonn * This verifies that the fault happens in kernel space
7461e85e36SJonas Bonn * and that the fault was not a protection error.
7561e85e36SJonas Bonn */
7661e85e36SJonas Bonn
7761e85e36SJonas Bonn if (address >= VMALLOC_START &&
7861e85e36SJonas Bonn (vector != 0x300 && vector != 0x400) &&
7961e85e36SJonas Bonn !user_mode(regs))
8061e85e36SJonas Bonn goto vmalloc_fault;
8161e85e36SJonas Bonn
8261e85e36SJonas Bonn /* If exceptions were enabled, we can reenable them here */
8361e85e36SJonas Bonn if (user_mode(regs)) {
8461e85e36SJonas Bonn /* Exception was in userspace: reenable interrupts */
8561e85e36SJonas Bonn local_irq_enable();
86759496baSJohannes Weiner flags |= FAULT_FLAG_USER;
8761e85e36SJonas Bonn } else {
8861e85e36SJonas Bonn /* If exception was in a syscall, then IRQ's may have
8961e85e36SJonas Bonn * been enabled or disabled. If they were enabled,
9061e85e36SJonas Bonn * reenable them.
9161e85e36SJonas Bonn */
9261e85e36SJonas Bonn if (regs->sr && (SPR_SR_IEE | SPR_SR_TEE))
9361e85e36SJonas Bonn local_irq_enable();
9461e85e36SJonas Bonn }
9561e85e36SJonas Bonn
9661e85e36SJonas Bonn mm = tsk->mm;
9775bfb9a1SEric W. Biederman si_code = SEGV_MAPERR;
9861e85e36SJonas Bonn
9961e85e36SJonas Bonn /*
10061e85e36SJonas Bonn * If we're in an interrupt or have no user
10161e85e36SJonas Bonn * context, we must not take the fault..
10261e85e36SJonas Bonn */
10361e85e36SJonas Bonn
10461e85e36SJonas Bonn if (in_interrupt() || !mm)
10561e85e36SJonas Bonn goto no_context;
10661e85e36SJonas Bonn
10738caa902SPeter Xu perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
10838caa902SPeter Xu
1094971f2bdSKautuk Consul retry:
110d8ed45c5SMichel Lespinasse mmap_read_lock(mm);
11161e85e36SJonas Bonn vma = find_vma(mm, address);
11261e85e36SJonas Bonn
11361e85e36SJonas Bonn if (!vma)
11461e85e36SJonas Bonn goto bad_area;
11561e85e36SJonas Bonn
11661e85e36SJonas Bonn if (vma->vm_start <= address)
11761e85e36SJonas Bonn goto good_area;
11861e85e36SJonas Bonn
11961e85e36SJonas Bonn if (!(vma->vm_flags & VM_GROWSDOWN))
12061e85e36SJonas Bonn goto bad_area;
12161e85e36SJonas Bonn
12261e85e36SJonas Bonn if (user_mode(regs)) {
12361e85e36SJonas Bonn /*
12461e85e36SJonas Bonn * accessing the stack below usp is always a bug.
12561e85e36SJonas Bonn * we get page-aligned addresses so we can only check
12661e85e36SJonas Bonn * if we're within a page from usp, but that might be
12761e85e36SJonas Bonn * enough to catch brutal errors at least.
12861e85e36SJonas Bonn */
12961e85e36SJonas Bonn if (address + PAGE_SIZE < regs->sp)
13061e85e36SJonas Bonn goto bad_area;
13161e85e36SJonas Bonn }
1328d7071afSLinus Torvalds vma = expand_stack(mm, address);
1338d7071afSLinus Torvalds if (!vma)
1348d7071afSLinus Torvalds goto bad_area_nosemaphore;
13561e85e36SJonas Bonn
13661e85e36SJonas Bonn /*
13761e85e36SJonas Bonn * Ok, we have a good vm_area for this memory access, so
13861e85e36SJonas Bonn * we can handle it..
13961e85e36SJonas Bonn */
14061e85e36SJonas Bonn
14161e85e36SJonas Bonn good_area:
14275bfb9a1SEric W. Biederman si_code = SEGV_ACCERR;
14361e85e36SJonas Bonn
14461e85e36SJonas Bonn /* first do some preliminary protection checks */
14561e85e36SJonas Bonn
14661e85e36SJonas Bonn if (write_acc) {
14761e85e36SJonas Bonn if (!(vma->vm_flags & VM_WRITE))
14861e85e36SJonas Bonn goto bad_area;
1494971f2bdSKautuk Consul flags |= FAULT_FLAG_WRITE;
15061e85e36SJonas Bonn } else {
15161e85e36SJonas Bonn /* not present */
15261e85e36SJonas Bonn if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
15361e85e36SJonas Bonn goto bad_area;
15461e85e36SJonas Bonn }
15561e85e36SJonas Bonn
15661e85e36SJonas Bonn /* are we trying to execute nonexecutable area */
15761e85e36SJonas Bonn if ((vector == 0x400) && !(vma->vm_page_prot.pgprot & _PAGE_EXEC))
15861e85e36SJonas Bonn goto bad_area;
15961e85e36SJonas Bonn
16061e85e36SJonas Bonn /*
16161e85e36SJonas Bonn * If for any reason at all we couldn't handle the fault,
16261e85e36SJonas Bonn * make sure we exit gracefully rather than endlessly redo
16361e85e36SJonas Bonn * the fault.
16461e85e36SJonas Bonn */
16561e85e36SJonas Bonn
16638caa902SPeter Xu fault = handle_mm_fault(vma, address, flags, regs);
1674971f2bdSKautuk Consul
168caa82ae7SAl Viro if (fault_signal_pending(fault, regs)) {
169caa82ae7SAl Viro if (!user_mode(regs))
170caa82ae7SAl Viro goto no_context;
1714971f2bdSKautuk Consul return;
172caa82ae7SAl Viro }
1734971f2bdSKautuk Consul
174d9272525SPeter Xu /* The fault is fully completed (including releasing mmap lock) */
175d9272525SPeter Xu if (fault & VM_FAULT_COMPLETED)
176d9272525SPeter Xu return;
177d9272525SPeter Xu
17861e85e36SJonas Bonn if (unlikely(fault & VM_FAULT_ERROR)) {
17961e85e36SJonas Bonn if (fault & VM_FAULT_OOM)
18061e85e36SJonas Bonn goto out_of_memory;
18133692f27SLinus Torvalds else if (fault & VM_FAULT_SIGSEGV)
18233692f27SLinus Torvalds goto bad_area;
18361e85e36SJonas Bonn else if (fault & VM_FAULT_SIGBUS)
18461e85e36SJonas Bonn goto do_sigbus;
18561e85e36SJonas Bonn BUG();
18661e85e36SJonas Bonn }
1874971f2bdSKautuk Consul
18861e85e36SJonas Bonn /*RGD modeled on Cris */
1894971f2bdSKautuk Consul if (fault & VM_FAULT_RETRY) {
19045cac65bSShaohua Li flags |= FAULT_FLAG_TRIED;
1914971f2bdSKautuk Consul
1923e4e28c5SMichel Lespinasse /* No need to mmap_read_unlock(mm) as we would
1934971f2bdSKautuk Consul * have already released it in __lock_page_or_retry
1944971f2bdSKautuk Consul * in mm/filemap.c.
1954971f2bdSKautuk Consul */
1964971f2bdSKautuk Consul
1974971f2bdSKautuk Consul goto retry;
1984971f2bdSKautuk Consul }
19961e85e36SJonas Bonn
200d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
20161e85e36SJonas Bonn return;
20261e85e36SJonas Bonn
20361e85e36SJonas Bonn /*
20461e85e36SJonas Bonn * Something tried to access memory that isn't in our memory map..
20561e85e36SJonas Bonn * Fix it, but check if it's kernel or user first..
20661e85e36SJonas Bonn */
20761e85e36SJonas Bonn
20861e85e36SJonas Bonn bad_area:
209d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
21061e85e36SJonas Bonn
21161e85e36SJonas Bonn bad_area_nosemaphore:
21261e85e36SJonas Bonn
21361e85e36SJonas Bonn /* User mode accesses just cause a SIGSEGV */
21461e85e36SJonas Bonn
21561e85e36SJonas Bonn if (user_mode(regs)) {
2162e1661d2SEric W. Biederman force_sig_fault(SIGSEGV, si_code, (void __user *)address);
21761e85e36SJonas Bonn return;
21861e85e36SJonas Bonn }
21961e85e36SJonas Bonn
22061e85e36SJonas Bonn no_context:
22161e85e36SJonas Bonn
22261e85e36SJonas Bonn /* Are we prepared to handle this kernel fault?
22361e85e36SJonas Bonn *
22461e85e36SJonas Bonn * (The kernel has valid exception-points in the source
22561e85e36SJonas Bonn * when it acesses user-memory. When it fails in one
22661e85e36SJonas Bonn * of those points, we find it in a table and do a jump
22761e85e36SJonas Bonn * to some fixup code that loads an appropriate error
22861e85e36SJonas Bonn * code)
22961e85e36SJonas Bonn */
23061e85e36SJonas Bonn
23161e85e36SJonas Bonn {
23261e85e36SJonas Bonn const struct exception_table_entry *entry;
23361e85e36SJonas Bonn
23461e85e36SJonas Bonn if ((entry = search_exception_tables(regs->pc)) != NULL) {
23561e85e36SJonas Bonn /* Adjust the instruction pointer in the stackframe */
23661e85e36SJonas Bonn regs->pc = entry->fixup;
23761e85e36SJonas Bonn return;
23861e85e36SJonas Bonn }
23961e85e36SJonas Bonn }
24061e85e36SJonas Bonn
24161e85e36SJonas Bonn /*
24261e85e36SJonas Bonn * Oops. The kernel tried to access some bad page. We'll have to
24361e85e36SJonas Bonn * terminate things with extreme prejudice.
24461e85e36SJonas Bonn */
24561e85e36SJonas Bonn
24661e85e36SJonas Bonn if ((unsigned long)(address) < PAGE_SIZE)
24761e85e36SJonas Bonn printk(KERN_ALERT
24861e85e36SJonas Bonn "Unable to handle kernel NULL pointer dereference");
24961e85e36SJonas Bonn else
25061e85e36SJonas Bonn printk(KERN_ALERT "Unable to handle kernel access");
25161e85e36SJonas Bonn printk(" at virtual address 0x%08lx\n", address);
25261e85e36SJonas Bonn
25361e85e36SJonas Bonn die("Oops", regs, write_acc);
25461e85e36SJonas Bonn
25561e85e36SJonas Bonn /*
25661e85e36SJonas Bonn * We ran out of memory, or some other thing happened to us that made
25761e85e36SJonas Bonn * us unable to handle the page fault gracefully.
25861e85e36SJonas Bonn */
25961e85e36SJonas Bonn
26061e85e36SJonas Bonn out_of_memory:
261d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
262609838cfSJohannes Weiner if (!user_mode(regs))
26361e85e36SJonas Bonn goto no_context;
264609838cfSJohannes Weiner pagefault_out_of_memory();
265609838cfSJohannes Weiner return;
26661e85e36SJonas Bonn
26761e85e36SJonas Bonn do_sigbus:
268d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
26961e85e36SJonas Bonn
27061e85e36SJonas Bonn /*
27161e85e36SJonas Bonn * Send a sigbus, regardless of whether we were in kernel
27261e85e36SJonas Bonn * or user mode.
27361e85e36SJonas Bonn */
2742e1661d2SEric W. Biederman force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
27561e85e36SJonas Bonn
27661e85e36SJonas Bonn /* Kernel mode? Handle exceptions or die */
27761e85e36SJonas Bonn if (!user_mode(regs))
27861e85e36SJonas Bonn goto no_context;
27961e85e36SJonas Bonn return;
28061e85e36SJonas Bonn
28161e85e36SJonas Bonn vmalloc_fault:
28261e85e36SJonas Bonn {
28361e85e36SJonas Bonn /*
28461e85e36SJonas Bonn * Synchronize this task's top level page-table
28561e85e36SJonas Bonn * with the 'reference' page table.
28661e85e36SJonas Bonn *
28761e85e36SJonas Bonn * Use current_pgd instead of tsk->active_mm->pgd
28861e85e36SJonas Bonn * since the latter might be unavailable if this
28961e85e36SJonas Bonn * code is executed in a misfortunately run irq
29061e85e36SJonas Bonn * (like inside schedule() between switch_mm and
29161e85e36SJonas Bonn * switch_to...).
29261e85e36SJonas Bonn */
29361e85e36SJonas Bonn
29461e85e36SJonas Bonn int offset = pgd_index(address);
29561e85e36SJonas Bonn pgd_t *pgd, *pgd_k;
296b187fb7fSMike Rapoport p4d_t *p4d, *p4d_k;
29761e85e36SJonas Bonn pud_t *pud, *pud_k;
29861e85e36SJonas Bonn pmd_t *pmd, *pmd_k;
29961e85e36SJonas Bonn pte_t *pte_k;
30061e85e36SJonas Bonn
30161e85e36SJonas Bonn /*
30261e85e36SJonas Bonn phx_warn("do_page_fault(): vmalloc_fault will not work, "
30361e85e36SJonas Bonn "since current_pgd assign a proper value somewhere\n"
30461e85e36SJonas Bonn "anyhow we don't need this at the moment\n");
30561e85e36SJonas Bonn
30661e85e36SJonas Bonn phx_mmu("vmalloc_fault");
30761e85e36SJonas Bonn */
3088e6d08e0SStefan Kristiansson pgd = (pgd_t *)current_pgd[smp_processor_id()] + offset;
30961e85e36SJonas Bonn pgd_k = init_mm.pgd + offset;
31061e85e36SJonas Bonn
31161e85e36SJonas Bonn /* Since we're two-level, we don't need to do both
31261e85e36SJonas Bonn * set_pgd and set_pmd (they do the same thing). If
31361e85e36SJonas Bonn * we go three-level at some point, do the right thing
31461e85e36SJonas Bonn * with pgd_present and set_pgd here.
31561e85e36SJonas Bonn *
31661e85e36SJonas Bonn * Also, since the vmalloc area is global, we don't
31761e85e36SJonas Bonn * need to copy individual PTE's, it is enough to
31861e85e36SJonas Bonn * copy the pgd pointer into the pte page of the
31961e85e36SJonas Bonn * root task. If that is there, we'll find our pte if
32061e85e36SJonas Bonn * it exists.
32161e85e36SJonas Bonn */
32261e85e36SJonas Bonn
323b187fb7fSMike Rapoport p4d = p4d_offset(pgd, address);
324b187fb7fSMike Rapoport p4d_k = p4d_offset(pgd_k, address);
325b187fb7fSMike Rapoport if (!p4d_present(*p4d_k))
326b187fb7fSMike Rapoport goto no_context;
327b187fb7fSMike Rapoport
328b187fb7fSMike Rapoport pud = pud_offset(p4d, address);
329b187fb7fSMike Rapoport pud_k = pud_offset(p4d_k, address);
33061e85e36SJonas Bonn if (!pud_present(*pud_k))
33161e85e36SJonas Bonn goto no_context;
33261e85e36SJonas Bonn
33361e85e36SJonas Bonn pmd = pmd_offset(pud, address);
33461e85e36SJonas Bonn pmd_k = pmd_offset(pud_k, address);
33561e85e36SJonas Bonn
33661e85e36SJonas Bonn if (!pmd_present(*pmd_k))
33761e85e36SJonas Bonn goto bad_area_nosemaphore;
33861e85e36SJonas Bonn
33961e85e36SJonas Bonn set_pmd(pmd, *pmd_k);
34061e85e36SJonas Bonn
34161e85e36SJonas Bonn /* Make sure the actual PTE exists as well to
34261e85e36SJonas Bonn * catch kernel vmalloc-area accesses to non-mapped
34361e85e36SJonas Bonn * addresses. If we don't do this, this will just
34461e85e36SJonas Bonn * silently loop forever.
34561e85e36SJonas Bonn */
34661e85e36SJonas Bonn
34761e85e36SJonas Bonn pte_k = pte_offset_kernel(pmd_k, address);
34861e85e36SJonas Bonn if (!pte_present(*pte_k))
34961e85e36SJonas Bonn goto no_context;
35061e85e36SJonas Bonn
35161e85e36SJonas Bonn return;
35261e85e36SJonas Bonn }
35361e85e36SJonas Bonn }
354