// SPDX-License-Identifier: GPL-2.0-or-later1/*2* OpenRISC fault.c3*4* Linux architectural port borrowing liberally from similar works of5* others. All original copyrights apply as per the original source6* declaration.7*8* Modifications for the OpenRISC architecture:9* Copyright (C) 2003 Matjaz Breskvar <[email protected]>10* Copyright (C) 2010-2011 Jonas Bonn <[email protected]>11*/1213#include <linux/mm.h>14#include <linux/interrupt.h>15#include <linux/extable.h>16#include <linux/sched/signal.h>17#include <linux/perf_event.h>1819#include <linux/uaccess.h>20#include <asm/bug.h>21#include <asm/mmu_context.h>22#include <asm/siginfo.h>23#include <asm/signal.h>2425#define NUM_TLB_ENTRIES 6426#define TLB_OFFSET(add) (((add) >> PAGE_SHIFT) & (NUM_TLB_ENTRIES-1))2728/* __PHX__ :: - check the vmalloc_fault in do_page_fault()29* - also look into include/asm/mmu_context.h30*/31volatile pgd_t *current_pgd[NR_CPUS];3233asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address,34unsigned long vector, int write_acc);3536/*37* This routine handles page faults. It determines the address,38* and the problem, and then passes it off to one of the appropriate39* routines.40*41* If this routine detects a bad access, it returns 1, otherwise it42* returns 0.43*/4445asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address,46unsigned long vector, int write_acc)47{48struct task_struct *tsk;49struct mm_struct *mm;50struct vm_area_struct *vma;51int si_code;52vm_fault_t fault;53unsigned int flags = FAULT_FLAG_DEFAULT;5455tsk = current;5657/*58* We fault-in kernel-space virtual memory on-demand. The59* 'reference' page table is init_mm.pgd.60*61* NOTE! We MUST NOT take any locks for this case. We may62* be in an interrupt or a critical region, and should63* only copy the information from the master page table,64* nothing more.65*66* NOTE2: This is done so that, when updating the vmalloc67* mappings we don't have to walk all processes pgdirs and68* add the high mappings all at once. Instead we do it as they69* are used. However vmalloc'ed page entries have the PAGE_GLOBAL70* bit set so sometimes the TLB can use a lingering entry.71*72* This verifies that the fault happens in kernel space73* and that the fault was not a protection error.74*/7576if (address >= VMALLOC_START &&77(vector != 0x300 && vector != 0x400) &&78!user_mode(regs))79goto vmalloc_fault;8081/* If exceptions were enabled, we can reenable them here */82if (user_mode(regs)) {83/* Exception was in userspace: reenable interrupts */84local_irq_enable();85flags |= FAULT_FLAG_USER;86} else {87/* If exception was in a syscall, then IRQ's may have88* been enabled or disabled. If they were enabled,89* reenable them.90*/91if (regs->sr && (SPR_SR_IEE | SPR_SR_TEE))92local_irq_enable();93}9495mm = tsk->mm;96si_code = SEGV_MAPERR;9798/*99* If we're in an interrupt or have no user100* context, we must not take the fault..101*/102103if (in_interrupt() || !mm)104goto no_context;105106perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);107108retry:109mmap_read_lock(mm);110vma = find_vma(mm, address);111112if (!vma)113goto bad_area;114115if (vma->vm_start <= address)116goto good_area;117118if (!(vma->vm_flags & VM_GROWSDOWN))119goto bad_area;120121if (user_mode(regs)) {122/*123* accessing the stack below usp is always a bug.124* we get page-aligned addresses so we can only check125* if we're within a page from usp, but that might be126* enough to catch brutal errors at least.127*/128if (address + PAGE_SIZE < regs->sp)129goto bad_area;130}131vma = expand_stack(mm, address);132if (!vma)133goto bad_area_nosemaphore;134135/*136* Ok, we have a good vm_area for this memory access, so137* we can handle it..138*/139140good_area:141si_code = SEGV_ACCERR;142143/* first do some preliminary protection checks */144145if (write_acc) {146if (!(vma->vm_flags & VM_WRITE))147goto bad_area;148flags |= FAULT_FLAG_WRITE;149} else {150/* not present */151if (!(vma->vm_flags & (VM_READ | VM_EXEC)))152goto bad_area;153}154155/* are we trying to execute nonexecutable area */156if ((vector == 0x400) && !(vma->vm_page_prot.pgprot & _PAGE_EXEC))157goto bad_area;158159/*160* If for any reason at all we couldn't handle the fault,161* make sure we exit gracefully rather than endlessly redo162* the fault.163*/164165fault = handle_mm_fault(vma, address, flags, regs);166167if (fault_signal_pending(fault, regs)) {168if (!user_mode(regs))169goto no_context;170return;171}172173/* The fault is fully completed (including releasing mmap lock) */174if (fault & VM_FAULT_COMPLETED)175return;176177if (unlikely(fault & VM_FAULT_ERROR)) {178if (fault & VM_FAULT_OOM)179goto out_of_memory;180else if (fault & VM_FAULT_SIGSEGV)181goto bad_area;182else if (fault & VM_FAULT_SIGBUS)183goto do_sigbus;184BUG();185}186187/*RGD modeled on Cris */188if (fault & VM_FAULT_RETRY) {189flags |= FAULT_FLAG_TRIED;190191/* No need to mmap_read_unlock(mm) as we would192* have already released it in __lock_page_or_retry193* in mm/filemap.c.194*/195196goto retry;197}198199mmap_read_unlock(mm);200return;201202/*203* Something tried to access memory that isn't in our memory map..204* Fix it, but check if it's kernel or user first..205*/206207bad_area:208mmap_read_unlock(mm);209210bad_area_nosemaphore:211212/* User mode accesses just cause a SIGSEGV */213214if (user_mode(regs)) {215force_sig_fault(SIGSEGV, si_code, (void __user *)address);216return;217}218219no_context:220221/* Are we prepared to handle this kernel fault?222*223* (The kernel has valid exception-points in the source224* when it acesses user-memory. When it fails in one225* of those points, we find it in a table and do a jump226* to some fixup code that loads an appropriate error227* code)228*/229230{231const struct exception_table_entry *entry;232233if ((entry = search_exception_tables(regs->pc)) != NULL) {234/* Adjust the instruction pointer in the stackframe */235regs->pc = entry->fixup;236return;237}238}239240/*241* Oops. The kernel tried to access some bad page. We'll have to242* terminate things with extreme prejudice.243*/244245if ((unsigned long)(address) < PAGE_SIZE)246printk(KERN_ALERT247"Unable to handle kernel NULL pointer dereference");248else249printk(KERN_ALERT "Unable to handle kernel access");250printk(" at virtual address 0x%08lx\n", address);251252die("Oops", regs, write_acc);253254/*255* We ran out of memory, or some other thing happened to us that made256* us unable to handle the page fault gracefully.257*/258259out_of_memory:260mmap_read_unlock(mm);261if (!user_mode(regs))262goto no_context;263pagefault_out_of_memory();264return;265266do_sigbus:267mmap_read_unlock(mm);268269/*270* Send a sigbus, regardless of whether we were in kernel271* or user mode.272*/273force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);274275/* Kernel mode? Handle exceptions or die */276if (!user_mode(regs))277goto no_context;278return;279280vmalloc_fault:281{282/*283* Synchronize this task's top level page-table284* with the 'reference' page table.285*286* Use current_pgd instead of tsk->active_mm->pgd287* since the latter might be unavailable if this288* code is executed in a misfortunately run irq289* (like inside schedule() between switch_mm and290* switch_to...).291*/292293int offset = pgd_index(address);294pgd_t *pgd, *pgd_k;295p4d_t *p4d, *p4d_k;296pud_t *pud, *pud_k;297pmd_t *pmd, *pmd_k;298pte_t *pte_k;299300/*301phx_warn("do_page_fault(): vmalloc_fault will not work, "302"since current_pgd assign a proper value somewhere\n"303"anyhow we don't need this at the moment\n");304305phx_mmu("vmalloc_fault");306*/307pgd = (pgd_t *)current_pgd[smp_processor_id()] + offset;308pgd_k = init_mm.pgd + offset;309310/* Since we're two-level, we don't need to do both311* set_pgd and set_pmd (they do the same thing). If312* we go three-level at some point, do the right thing313* with pgd_present and set_pgd here.314*315* Also, since the vmalloc area is global, we don't316* need to copy individual PTE's, it is enough to317* copy the pgd pointer into the pte page of the318* root task. If that is there, we'll find our pte if319* it exists.320*/321322p4d = p4d_offset(pgd, address);323p4d_k = p4d_offset(pgd_k, address);324if (!p4d_present(*p4d_k))325goto no_context;326327pud = pud_offset(p4d, address);328pud_k = pud_offset(p4d_k, address);329if (!pud_present(*pud_k))330goto no_context;331332pmd = pmd_offset(pud, address);333pmd_k = pmd_offset(pud_k, address);334335if (!pmd_present(*pmd_k))336goto bad_area_nosemaphore;337338set_pmd(pmd, *pmd_k);339340/* Make sure the actual PTE exists as well to341* catch kernel vmalloc-area accesses to non-mapped342* addresses. If we don't do this, this will just343* silently loop forever.344*/345346pte_k = pte_offset_kernel(pmd_k, address);347if (!pte_present(*pte_k))348goto no_context;349350return;351}352}353354355