Home | History | Annotate | Line # | Download | only in riscv
      1 /*	$NetBSD: netbsd32_machdep.c,v 1.7 2026/10/20 06:40:44 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matt Thomas of 3am Software Foundry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 
     34 __RCSID("$NetBSD: netbsd32_machdep.c,v 1.7 2026/10/20 06:40:44 skrll Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/ucontext.h>
     38 
     39 #include <uvm/uvm_extern.h>
     40 
     41 #include <compat/netbsd32/netbsd32.h>
     42 #include <compat/netbsd32/netbsd32_syscallargs.h>
     43 
     44 #include <riscv/frame.h>
     45 #include <riscv/locore.h>
     46 #include <riscv/userret.h>
     47 
     48 char machine_arch32[] = MACHINE_ARCH32;
     49 char machine32[] = MACHINE;
     50 
     51 void
     52 netbsd32_setregs(struct lwp *l, struct exec_package *pack, vaddr_t stack)
     53 {
     54 	l->l_proc->p_flag |= PK_32;
     55 	setregs(l, pack, stack);
     56 }
     57 
     58 /*
     59  * Start a new LWP
     60  */
     61 void
     62 startlwp32(void *arg)
     63 {
     64 	ucontext32_t * const uc = arg;
     65 	int error __diagused;
     66 
     67 	error = cpu_setmcontext32(curlwp, &uc->uc_mcontext, uc->uc_flags);
     68 	KASSERT(error == 0);
     69 
     70 	// Even though this is a ucontext32_t, the space allocated was for a
     71 	// full ucontext_t
     72 	kmem_free(uc, sizeof(ucontext_t));
     73 	userret(curlwp);
     74 }
     75 
     76 // We've worked hard to make sure struct reg32 and __gregset32_t are the same.
     77 // Ditto for struct fpreg and fregset_t.
     78 
     79 CTASSERT(sizeof(struct reg32) == sizeof(__gregset32_t));
     80 CTASSERT(sizeof(struct fpreg) == sizeof(__fregset_t));
     81 
     82 void
     83 cpu_getmcontext32(struct lwp *l, mcontext32_t *mcp, unsigned int *flags)
     84 {
     85 	const struct trapframe * const tf = l->l_md.md_utf;
     86 
     87 	/* Save register context. */
     88 	for (size_t i = _X_RA; i <= _X_T6; i++) {
     89 		mcp->__gregs[i] = tf->tf_reg[i];
     90 	}
     91 	mcp->__gregs[_REG_PC] = tf->tf_pc;
     92 
     93 	mcp->__gregs[_REG_TP] = (uint32_t)(uintptr_t)l->l_private;
     94 
     95 	*flags |= _UC_CPU | _UC_TLSBASE;
     96 
     97 	/* Save floating point register context, if any. */
     98 	KASSERT(l == curlwp);
     99 	if (fpu_valid_p(l)) {
    100 		/*
    101 		 * If this process is the current FP owner, dump its
    102 		 * context to the PCB first.
    103 		 */
    104 		fpu_save(l);
    105 
    106 		struct pcb * const pcb = lwp_getpcb(l);
    107 		*(struct fpreg *)mcp->__fregs = pcb->pcb_fpregs;
    108 		*flags |= _UC_FPU;
    109 	}
    110 }
    111 
    112 int
    113 cpu_mcontext32_validate(struct lwp *l, const mcontext32_t *mcp)
    114 {
    115 	/*
    116 	 * Verify that at least the PC and SP are user addresses.
    117 	 */
    118 	if ((int32_t) mcp->__gregs[_REG_PC] < 0
    119 	    || (int32_t) mcp->__gregs[_REG_SP] < 0
    120 	    || (mcp->__gregs[_REG_PC] & 1))
    121 		return EINVAL;
    122 
    123 	return 0;
    124 }
    125 
    126 int
    127 cpu_setmcontext32(struct lwp *l, const mcontext32_t *mcp, unsigned int flags)
    128 {
    129 	struct trapframe * const tf = l->l_md.md_utf;
    130 	struct proc * const p = l->l_proc;
    131 	const __greg32_t * const gr = mcp->__gregs;
    132 	int error;
    133 
    134 	/* Restore register context, if any. */
    135 	if (flags & _UC_CPU) {
    136 		error = cpu_mcontext32_validate(l, mcp);
    137 		if (error)
    138 			return error;
    139 
    140 		/* Save register context. */
    141 		for (size_t i = _X_RA; i <= _X_T6; i++) {
    142 			tf->tf_reg[i] = (int32_t)gr[i];
    143 		}
    144 		tf->tf_pc = (int32_t) gr[_REG_PC];
    145 	}
    146 
    147 	/* Restore the private thread context */
    148 	if (flags & _UC_TLSBASE) {
    149 		lwp_setprivate(l,
    150 		    (void *)(uintptr_t)(uint32_t)mcp->__gregs[_REG_TP]);
    151 	}
    152 
    153 	/* Restore floating point register context, if any. */
    154 	if (flags & _UC_FPU) {
    155 		KASSERT(l == curlwp);
    156 		/* Tell PCU we are replacing the FPU contents. */
    157 		fpu_replace(l);
    158 
    159 		/*
    160 		 * The PCB FP regs struct includes the FP CSR, so use the
    161 		 * proper size of fpreg when copying.
    162 		 */
    163 		struct pcb * const pcb = lwp_getpcb(l);
    164 		pcb->pcb_fpregs = *(const struct fpreg *)mcp->__fregs;
    165 	}
    166 
    167 	mutex_enter(p->p_lock);
    168 	if (flags & _UC_SETSTACK)
    169 		l->l_sigstk.ss_flags |= SS_ONSTACK;
    170 	if (flags & _UC_CLRSTACK)
    171 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
    172 	mutex_exit(p->p_lock);
    173 
    174 	return (0);
    175 }
    176 
    177 int
    178 netbsd32_sysarch(struct lwp *l, const struct netbsd32_sysarch_args *uap,
    179     register_t *retval)
    180 {
    181 	return ENOSYS;
    182 }
    183 
    184 vaddr_t
    185 netbsd32_vm_default_addr(struct proc *p, vaddr_t base, vsize_t size,
    186     int topdown)
    187 {
    188 	if (topdown)
    189 		return VM_DEFAULT_ADDRESS32_TOPDOWN(base, size);
    190 	else
    191 		return VM_DEFAULT_ADDRESS32_BOTTOMUP(base, size);
    192 }
    193 
    194 void
    195 netbsd32_machdep_md_init(void)
    196 {
    197 
    198 	/* nothing to do */
    199 }
    200 
    201 void
    202 netbsd32_machdep_md_fini(void)
    203 {
    204 
    205 	/* nothing to do */
    206 }
    207