Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * arch/sparc64/mm/fault.c: Page fault handlers for the 64-bit Sparc.
0004  *
0005  * Copyright (C) 1996, 2008 David S. Miller (davem@davemloft.net)
0006  * Copyright (C) 1997, 1999 Jakub Jelinek (jj@ultra.linux.cz)
0007  */
0008 
0009 #include <asm/head.h>
0010 
0011 #include <linux/string.h>
0012 #include <linux/types.h>
0013 #include <linux/sched.h>
0014 #include <linux/sched/debug.h>
0015 #include <linux/ptrace.h>
0016 #include <linux/mman.h>
0017 #include <linux/signal.h>
0018 #include <linux/mm.h>
0019 #include <linux/extable.h>
0020 #include <linux/init.h>
0021 #include <linux/perf_event.h>
0022 #include <linux/interrupt.h>
0023 #include <linux/kprobes.h>
0024 #include <linux/kdebug.h>
0025 #include <linux/percpu.h>
0026 #include <linux/context_tracking.h>
0027 #include <linux/uaccess.h>
0028 
0029 #include <asm/page.h>
0030 #include <asm/openprom.h>
0031 #include <asm/oplib.h>
0032 #include <asm/asi.h>
0033 #include <asm/lsu.h>
0034 #include <asm/sections.h>
0035 #include <asm/mmu_context.h>
0036 #include <asm/setup.h>
0037 
0038 int show_unhandled_signals = 1;
0039 
0040 static void __kprobes unhandled_fault(unsigned long address,
0041                       struct task_struct *tsk,
0042                       struct pt_regs *regs)
0043 {
0044     if ((unsigned long) address < PAGE_SIZE) {
0045         printk(KERN_ALERT "Unable to handle kernel NULL "
0046                "pointer dereference\n");
0047     } else {
0048         printk(KERN_ALERT "Unable to handle kernel paging request "
0049                "at virtual address %016lx\n", (unsigned long)address);
0050     }
0051     printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
0052            (tsk->mm ?
0053         CTX_HWBITS(tsk->mm->context) :
0054         CTX_HWBITS(tsk->active_mm->context)));
0055     printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
0056            (tsk->mm ? (unsigned long) tsk->mm->pgd :
0057                   (unsigned long) tsk->active_mm->pgd));
0058     die_if_kernel("Oops", regs);
0059 }
0060 
0061 static void __kprobes bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
0062 {
0063     printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
0064            regs->tpc);
0065     printk(KERN_CRIT "OOPS: RPC [%016lx]\n", regs->u_regs[15]);
0066     printk("OOPS: RPC <%pS>\n", (void *) regs->u_regs[15]);
0067     printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
0068     dump_stack();
0069     unhandled_fault(regs->tpc, current, regs);
0070 }
0071 
0072 /*
0073  * We now make sure that mmap_lock is held in all paths that call
0074  * this. Additionally, to prevent kswapd from ripping ptes from
0075  * under us, raise interrupts around the time that we look at the
0076  * pte, kswapd will have to wait to get his smp ipi response from
0077  * us. vmtruncate likewise. This saves us having to get pte lock.
0078  */
0079 static unsigned int get_user_insn(unsigned long tpc)
0080 {
0081     pgd_t *pgdp = pgd_offset(current->mm, tpc);
0082     p4d_t *p4dp;
0083     pud_t *pudp;
0084     pmd_t *pmdp;
0085     pte_t *ptep, pte;
0086     unsigned long pa;
0087     u32 insn = 0;
0088 
0089     if (pgd_none(*pgdp) || unlikely(pgd_bad(*pgdp)))
0090         goto out;
0091     p4dp = p4d_offset(pgdp, tpc);
0092     if (p4d_none(*p4dp) || unlikely(p4d_bad(*p4dp)))
0093         goto out;
0094     pudp = pud_offset(p4dp, tpc);
0095     if (pud_none(*pudp) || unlikely(pud_bad(*pudp)))
0096         goto out;
0097 
0098     /* This disables preemption for us as well. */
0099     local_irq_disable();
0100 
0101     pmdp = pmd_offset(pudp, tpc);
0102     if (pmd_none(*pmdp) || unlikely(pmd_bad(*pmdp)))
0103         goto out_irq_enable;
0104 
0105 #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
0106     if (is_hugetlb_pmd(*pmdp)) {
0107         pa  = pmd_pfn(*pmdp) << PAGE_SHIFT;
0108         pa += tpc & ~HPAGE_MASK;
0109 
0110         /* Use phys bypass so we don't pollute dtlb/dcache. */
0111         __asm__ __volatile__("lduwa [%1] %2, %0"
0112                      : "=r" (insn)
0113                      : "r" (pa), "i" (ASI_PHYS_USE_EC));
0114     } else
0115 #endif
0116     {
0117         ptep = pte_offset_map(pmdp, tpc);
0118         pte = *ptep;
0119         if (pte_present(pte)) {
0120             pa  = (pte_pfn(pte) << PAGE_SHIFT);
0121             pa += (tpc & ~PAGE_MASK);
0122 
0123             /* Use phys bypass so we don't pollute dtlb/dcache. */
0124             __asm__ __volatile__("lduwa [%1] %2, %0"
0125                          : "=r" (insn)
0126                          : "r" (pa), "i" (ASI_PHYS_USE_EC));
0127         }
0128         pte_unmap(ptep);
0129     }
0130 out_irq_enable:
0131     local_irq_enable();
0132 out:
0133     return insn;
0134 }
0135 
0136 static inline void
0137 show_signal_msg(struct pt_regs *regs, int sig, int code,
0138         unsigned long address, struct task_struct *tsk)
0139 {
0140     if (!unhandled_signal(tsk, sig))
0141         return;
0142 
0143     if (!printk_ratelimit())
0144         return;
0145 
0146     printk("%s%s[%d]: segfault at %lx ip %px (rpc %px) sp %px error %x",
0147            task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
0148            tsk->comm, task_pid_nr(tsk), address,
0149            (void *)regs->tpc, (void *)regs->u_regs[UREG_I7],
0150            (void *)regs->u_regs[UREG_FP], code);
0151 
0152     print_vma_addr(KERN_CONT " in ", regs->tpc);
0153 
0154     printk(KERN_CONT "\n");
0155 }
0156 
0157 static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
0158                  unsigned long fault_addr, unsigned int insn,
0159                  int fault_code)
0160 {
0161     unsigned long addr;
0162 
0163     if (fault_code & FAULT_CODE_ITLB) {
0164         addr = regs->tpc;
0165     } else {
0166         /* If we were able to probe the faulting instruction, use it
0167          * to compute a precise fault address.  Otherwise use the fault
0168          * time provided address which may only have page granularity.
0169          */
0170         if (insn)
0171             addr = compute_effective_address(regs, insn, 0);
0172         else
0173             addr = fault_addr;
0174     }
0175 
0176     if (unlikely(show_unhandled_signals))
0177         show_signal_msg(regs, sig, code, addr, current);
0178 
0179     force_sig_fault(sig, code, (void __user *) addr);
0180 }
0181 
0182 static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
0183 {
0184     if (!insn) {
0185         if (!regs->tpc || (regs->tpc & 0x3))
0186             return 0;
0187         if (regs->tstate & TSTATE_PRIV) {
0188             insn = *(unsigned int *) regs->tpc;
0189         } else {
0190             insn = get_user_insn(regs->tpc);
0191         }
0192     }
0193     return insn;
0194 }
0195 
0196 static void __kprobes do_kernel_fault(struct pt_regs *regs, int si_code,
0197                       int fault_code, unsigned int insn,
0198                       unsigned long address)
0199 {
0200     unsigned char asi = ASI_P;
0201  
0202     if ((!insn) && (regs->tstate & TSTATE_PRIV))
0203         goto cannot_handle;
0204 
0205     /* If user insn could be read (thus insn is zero), that
0206      * is fine.  We will just gun down the process with a signal
0207      * in that case.
0208      */
0209 
0210     if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
0211         (insn & 0xc0800000) == 0xc0800000) {
0212         if (insn & 0x2000)
0213             asi = (regs->tstate >> 24);
0214         else
0215             asi = (insn >> 5);
0216         if ((asi & 0xf2) == 0x82) {
0217             if (insn & 0x1000000) {
0218                 handle_ldf_stq(insn, regs);
0219             } else {
0220                 /* This was a non-faulting load. Just clear the
0221                  * destination register(s) and continue with the next
0222                  * instruction. -jj
0223                  */
0224                 handle_ld_nf(insn, regs);
0225             }
0226             return;
0227         }
0228     }
0229         
0230     /* Is this in ex_table? */
0231     if (regs->tstate & TSTATE_PRIV) {
0232         const struct exception_table_entry *entry;
0233 
0234         entry = search_exception_tables(regs->tpc);
0235         if (entry) {
0236             regs->tpc = entry->fixup;
0237             regs->tnpc = regs->tpc + 4;
0238             return;
0239         }
0240     } else {
0241         /* The si_code was set to make clear whether
0242          * this was a SEGV_MAPERR or SEGV_ACCERR fault.
0243          */
0244         do_fault_siginfo(si_code, SIGSEGV, regs, address, insn, fault_code);
0245         return;
0246     }
0247 
0248 cannot_handle:
0249     unhandled_fault (address, current, regs);
0250 }
0251 
0252 static void noinline __kprobes bogus_32bit_fault_tpc(struct pt_regs *regs)
0253 {
0254     static int times;
0255 
0256     if (times++ < 10)
0257         printk(KERN_ERR "FAULT[%s:%d]: 32-bit process reports "
0258                "64-bit TPC [%lx]\n",
0259                current->comm, current->pid,
0260                regs->tpc);
0261     show_regs(regs);
0262 }
0263 
0264 asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
0265 {
0266     enum ctx_state prev_state = exception_enter();
0267     struct mm_struct *mm = current->mm;
0268     struct vm_area_struct *vma;
0269     unsigned int insn = 0;
0270     int si_code, fault_code;
0271     vm_fault_t fault;
0272     unsigned long address, mm_rss;
0273     unsigned int flags = FAULT_FLAG_DEFAULT;
0274 
0275     fault_code = get_thread_fault_code();
0276 
0277     if (kprobe_page_fault(regs, 0))
0278         goto exit_exception;
0279 
0280     si_code = SEGV_MAPERR;
0281     address = current_thread_info()->fault_address;
0282 
0283     if ((fault_code & FAULT_CODE_ITLB) &&
0284         (fault_code & FAULT_CODE_DTLB))
0285         BUG();
0286 
0287     if (test_thread_flag(TIF_32BIT)) {
0288         if (!(regs->tstate & TSTATE_PRIV)) {
0289             if (unlikely((regs->tpc >> 32) != 0)) {
0290                 bogus_32bit_fault_tpc(regs);
0291                 goto intr_or_no_mm;
0292             }
0293         }
0294         if (unlikely((address >> 32) != 0))
0295             goto intr_or_no_mm;
0296     }
0297 
0298     if (regs->tstate & TSTATE_PRIV) {
0299         unsigned long tpc = regs->tpc;
0300 
0301         /* Sanity check the PC. */
0302         if ((tpc >= KERNBASE && tpc < (unsigned long) __init_end) ||
0303             (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
0304             /* Valid, no problems... */
0305         } else {
0306             bad_kernel_pc(regs, address);
0307             goto exit_exception;
0308         }
0309     } else
0310         flags |= FAULT_FLAG_USER;
0311 
0312     /*
0313      * If we're in an interrupt or have no user
0314      * context, we must not take the fault..
0315      */
0316     if (faulthandler_disabled() || !mm)
0317         goto intr_or_no_mm;
0318 
0319     perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
0320 
0321     if (!mmap_read_trylock(mm)) {
0322         if ((regs->tstate & TSTATE_PRIV) &&
0323             !search_exception_tables(regs->tpc)) {
0324             insn = get_fault_insn(regs, insn);
0325             goto handle_kernel_fault;
0326         }
0327 
0328 retry:
0329         mmap_read_lock(mm);
0330     }
0331 
0332     if (fault_code & FAULT_CODE_BAD_RA)
0333         goto do_sigbus;
0334 
0335     vma = find_vma(mm, address);
0336     if (!vma)
0337         goto bad_area;
0338 
0339     /* Pure DTLB misses do not tell us whether the fault causing
0340      * load/store/atomic was a write or not, it only says that there
0341      * was no match.  So in such a case we (carefully) read the
0342      * instruction to try and figure this out.  It's an optimization
0343      * so it's ok if we can't do this.
0344      *
0345      * Special hack, window spill/fill knows the exact fault type.
0346      */
0347     if (((fault_code &
0348           (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
0349         (vma->vm_flags & VM_WRITE) != 0) {
0350         insn = get_fault_insn(regs, 0);
0351         if (!insn)
0352             goto continue_fault;
0353         /* All loads, stores and atomics have bits 30 and 31 both set
0354          * in the instruction.  Bit 21 is set in all stores, but we
0355          * have to avoid prefetches which also have bit 21 set.
0356          */
0357         if ((insn & 0xc0200000) == 0xc0200000 &&
0358             (insn & 0x01780000) != 0x01680000) {
0359             /* Don't bother updating thread struct value,
0360              * because update_mmu_cache only cares which tlb
0361              * the access came from.
0362              */
0363             fault_code |= FAULT_CODE_WRITE;
0364         }
0365     }
0366 continue_fault:
0367 
0368     if (vma->vm_start <= address)
0369         goto good_area;
0370     if (!(vma->vm_flags & VM_GROWSDOWN))
0371         goto bad_area;
0372     if (!(fault_code & FAULT_CODE_WRITE)) {
0373         /* Non-faulting loads shouldn't expand stack. */
0374         insn = get_fault_insn(regs, insn);
0375         if ((insn & 0xc0800000) == 0xc0800000) {
0376             unsigned char asi;
0377 
0378             if (insn & 0x2000)
0379                 asi = (regs->tstate >> 24);
0380             else
0381                 asi = (insn >> 5);
0382             if ((asi & 0xf2) == 0x82)
0383                 goto bad_area;
0384         }
0385     }
0386     if (expand_stack(vma, address))
0387         goto bad_area;
0388     /*
0389      * Ok, we have a good vm_area for this memory access, so
0390      * we can handle it..
0391      */
0392 good_area:
0393     si_code = SEGV_ACCERR;
0394 
0395     /* If we took a ITLB miss on a non-executable page, catch
0396      * that here.
0397      */
0398     if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
0399         WARN(address != regs->tpc,
0400              "address (%lx) != regs->tpc (%lx)\n", address, regs->tpc);
0401         WARN_ON(regs->tstate & TSTATE_PRIV);
0402         goto bad_area;
0403     }
0404 
0405     if (fault_code & FAULT_CODE_WRITE) {
0406         if (!(vma->vm_flags & VM_WRITE))
0407             goto bad_area;
0408 
0409         /* Spitfire has an icache which does not snoop
0410          * processor stores.  Later processors do...
0411          */
0412         if (tlb_type == spitfire &&
0413             (vma->vm_flags & VM_EXEC) != 0 &&
0414             vma->vm_file != NULL)
0415             set_thread_fault_code(fault_code |
0416                           FAULT_CODE_BLKCOMMIT);
0417 
0418         flags |= FAULT_FLAG_WRITE;
0419     } else {
0420         /* Allow reads even for write-only mappings */
0421         if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
0422             goto bad_area;
0423     }
0424 
0425     fault = handle_mm_fault(vma, address, flags, regs);
0426 
0427     if (fault_signal_pending(fault, regs))
0428         goto exit_exception;
0429 
0430     /* The fault is fully completed (including releasing mmap lock) */
0431     if (fault & VM_FAULT_COMPLETED)
0432         goto lock_released;
0433 
0434     if (unlikely(fault & VM_FAULT_ERROR)) {
0435         if (fault & VM_FAULT_OOM)
0436             goto out_of_memory;
0437         else if (fault & VM_FAULT_SIGSEGV)
0438             goto bad_area;
0439         else if (fault & VM_FAULT_SIGBUS)
0440             goto do_sigbus;
0441         BUG();
0442     }
0443 
0444     if (fault & VM_FAULT_RETRY) {
0445         flags |= FAULT_FLAG_TRIED;
0446 
0447         /* No need to mmap_read_unlock(mm) as we would
0448          * have already released it in __lock_page_or_retry
0449          * in mm/filemap.c.
0450          */
0451 
0452         goto retry;
0453     }
0454     mmap_read_unlock(mm);
0455 
0456 lock_released:
0457     mm_rss = get_mm_rss(mm);
0458 #if defined(CONFIG_TRANSPARENT_HUGEPAGE)
0459     mm_rss -= (mm->context.thp_pte_count * (HPAGE_SIZE / PAGE_SIZE));
0460 #endif
0461     if (unlikely(mm_rss >
0462              mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
0463         tsb_grow(mm, MM_TSB_BASE, mm_rss);
0464 #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
0465     mm_rss = mm->context.hugetlb_pte_count + mm->context.thp_pte_count;
0466     mm_rss *= REAL_HPAGE_PER_HPAGE;
0467     if (unlikely(mm_rss >
0468              mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit)) {
0469         if (mm->context.tsb_block[MM_TSB_HUGE].tsb)
0470             tsb_grow(mm, MM_TSB_HUGE, mm_rss);
0471         else
0472             hugetlb_setup(regs);
0473 
0474     }
0475 #endif
0476 exit_exception:
0477     exception_exit(prev_state);
0478     return;
0479 
0480     /*
0481      * Something tried to access memory that isn't in our memory map..
0482      * Fix it, but check if it's kernel or user first..
0483      */
0484 bad_area:
0485     insn = get_fault_insn(regs, insn);
0486     mmap_read_unlock(mm);
0487 
0488 handle_kernel_fault:
0489     do_kernel_fault(regs, si_code, fault_code, insn, address);
0490     goto exit_exception;
0491 
0492 /*
0493  * We ran out of memory, or some other thing happened to us that made
0494  * us unable to handle the page fault gracefully.
0495  */
0496 out_of_memory:
0497     insn = get_fault_insn(regs, insn);
0498     mmap_read_unlock(mm);
0499     if (!(regs->tstate & TSTATE_PRIV)) {
0500         pagefault_out_of_memory();
0501         goto exit_exception;
0502     }
0503     goto handle_kernel_fault;
0504 
0505 intr_or_no_mm:
0506     insn = get_fault_insn(regs, 0);
0507     goto handle_kernel_fault;
0508 
0509 do_sigbus:
0510     insn = get_fault_insn(regs, insn);
0511     mmap_read_unlock(mm);
0512 
0513     /*
0514      * Send a sigbus, regardless of whether we were in kernel
0515      * or user mode.
0516      */
0517     do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, address, insn, fault_code);
0518 
0519     /* Kernel mode? Handle exceptions or die */
0520     if (regs->tstate & TSTATE_PRIV)
0521         goto handle_kernel_fault;
0522 }