Home | History | Annotate | Line # | Download | only in aarch64
linux32_machdep.c revision 1.1
      1 /*	$NetBSD: linux32_machdep.c,v 1.1 2021/11/25 03:08:04 ryo Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2021 Ryo Shimizu <ryo (at) nerv.org>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     25  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: linux32_machdep.c,v 1.1 2021/11/25 03:08:04 ryo Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/proc.h>
     35 #include <sys/exec.h>
     36 
     37 #include <compat/netbsd32/netbsd32.h>
     38 #include <compat/netbsd32/netbsd32_exec.h>
     39 
     40 #include <compat/linux/common/linux_types.h>
     41 #include <compat/linux/common/linux_signal.h>
     42 #include <compat/linux/common/linux_errno.h>
     43 #include <compat/linux/common/linux_machdep.h>
     44 
     45 #include <compat/linux32/common/linux32_types.h>
     46 #include <compat/linux32/common/linux32_signal.h>
     47 #include <compat/linux32/common/linux32_errno.h>
     48 #include <compat/linux32/common/linux32_exec.h>
     49 #include <compat/linux32/common/linux32_machdep.h>
     50 #include <compat/linux32/linux32_syscall.h>
     51 #include <compat/linux32/linux32_syscallargs.h>
     52 
     53 #include <machine/netbsd32_machdep.h>
     54 #include <arm/vfpreg.h>
     55 
     56 static void
     57 linux32_save_sigcontext(struct lwp *l, struct linux32_ucontext *luc)
     58 {
     59 	ucontext32_t uc;
     60 	__greg32_t *gr = uc.uc_mcontext.__gregs;
     61 	__vfpregset32_t *vfpregs = &uc.uc_mcontext.__vfpregs;
     62 	struct linux32_aux_sigframe *aux;
     63 	int i;
     64 
     65 	cpu_getmcontext32(l, &uc.uc_mcontext, &uc.uc_flags);
     66 
     67 	memset(luc, 0, sizeof(*luc));
     68 	luc->luc_mcontext.arm_r0 = gr[_REG_R0];
     69 	luc->luc_mcontext.arm_r1 = gr[_REG_R1];
     70 	luc->luc_mcontext.arm_r2 = gr[_REG_R2];
     71 	luc->luc_mcontext.arm_r3 = gr[_REG_R3];
     72 	luc->luc_mcontext.arm_r4 = gr[_REG_R4];
     73 	luc->luc_mcontext.arm_r5 = gr[_REG_R5];
     74 	luc->luc_mcontext.arm_r6 = gr[_REG_R6];
     75 	luc->luc_mcontext.arm_r7 = gr[_REG_R7];
     76 	luc->luc_mcontext.arm_r8 = gr[_REG_R8];
     77 	luc->luc_mcontext.arm_r9 = gr[_REG_R9];
     78 	luc->luc_mcontext.arm_r10 = gr[_REG_R10];
     79 	luc->luc_mcontext.arm_fp = gr[_REG_R11];
     80 	luc->luc_mcontext.arm_ip = gr[_REG_R12];
     81 	luc->luc_mcontext.arm_sp = gr[_REG_R13];
     82 	luc->luc_mcontext.arm_lr = gr[_REG_R14];
     83 	luc->luc_mcontext.arm_pc = gr[_REG_R15];
     84 	luc->luc_mcontext.arm_cpsr = gr[_REG_CPSR];
     85 	luc->luc_mcontext.trap_no = 0;
     86 	luc->luc_mcontext.error_code = 0;
     87 	luc->luc_mcontext.oldmask = 0;
     88 	luc->luc_mcontext.fault_address = 0;
     89 
     90 	if (uc.uc_flags & _UC_FPU) {
     91 		aux = (struct linux32_aux_sigframe *)luc->luc_regspace;
     92 #define LINUX32_VFP_MAGIC	0x56465001
     93 		aux->vfp.magic = LINUX32_VFP_MAGIC;
     94 		aux->vfp.size = sizeof(struct linux32_vfp_sigframe);
     95 
     96 		CTASSERT(__arraycount(aux->vfp.ufp.fpregs) ==
     97 		    __arraycount(vfpregs->__vfp_fstmx));
     98 		for (i = 0; i < __arraycount(aux->vfp.ufp.fpregs); i++)
     99 			aux->vfp.ufp.fpregs[i] = vfpregs->__vfp_fstmx[i];
    100 		aux->vfp.ufp.fpscr = vfpregs->__vfp_fpscr;
    101 
    102 		aux->vfp.ufp_exc.fpexc = VFP_FPEXC_EN | VFP_FPEXC_VECITR;
    103 		aux->vfp.ufp_exc.fpinst = 0;
    104 		aux->vfp.ufp_exc.fpinst2 = 0;
    105 		aux->end_magic = 0 ;
    106 	}
    107 }
    108 
    109 static int
    110 linux32_restore_sigcontext(struct lwp *l, struct linux32_ucontext *luc)
    111 {
    112 	ucontext32_t uc;
    113 	__greg32_t *gr = uc.uc_mcontext.__gregs;
    114 	__vfpregset32_t *vfpregs = &uc.uc_mcontext.__vfpregs;
    115 	struct linux32_aux_sigframe *aux;
    116 	int i;
    117 
    118 	memset(&uc, 0, sizeof(uc));
    119 	gr[_REG_R0] = luc->luc_mcontext.arm_r0;
    120 	gr[_REG_R1] = luc->luc_mcontext.arm_r1;
    121 	gr[_REG_R2] = luc->luc_mcontext.arm_r2;
    122 	gr[_REG_R3] = luc->luc_mcontext.arm_r3;
    123 	gr[_REG_R4] = luc->luc_mcontext.arm_r4;
    124 	gr[_REG_R5] = luc->luc_mcontext.arm_r5;
    125 	gr[_REG_R6] = luc->luc_mcontext.arm_r6;
    126 	gr[_REG_R7] = luc->luc_mcontext.arm_r7;
    127 	gr[_REG_R8] = luc->luc_mcontext.arm_r8;
    128 	gr[_REG_R9] = luc->luc_mcontext.arm_r9;
    129 	gr[_REG_R10] = luc->luc_mcontext.arm_r10;
    130 	gr[_REG_R11] = luc->luc_mcontext.arm_fp;
    131 	gr[_REG_R12] = luc->luc_mcontext.arm_ip;
    132 	gr[_REG_R13] = luc->luc_mcontext.arm_sp;
    133 	gr[_REG_R14] = luc->luc_mcontext.arm_lr;
    134 	gr[_REG_R15] = luc->luc_mcontext.arm_pc;
    135 	gr[_REG_CPSR] = luc->luc_mcontext.arm_cpsr;
    136 	uc.uc_flags |= _UC_CPU;
    137 
    138 	aux = (struct linux32_aux_sigframe *)luc->luc_regspace;
    139 	if (aux->vfp.magic == LINUX32_VFP_MAGIC &&
    140 	    aux->vfp.size == sizeof(struct linux32_vfp_sigframe)) {
    141 
    142 		CTASSERT(__arraycount(vfpregs->__vfp_fstmx) ==
    143 		    __arraycount(aux->vfp.ufp.fpregs));
    144 		for (i = 0; i < __arraycount(vfpregs->__vfp_fstmx); i++)
    145 			vfpregs->__vfp_fstmx[i] = aux->vfp.ufp.fpregs[i];
    146 		vfpregs->__vfp_fpscr = aux->vfp.ufp.fpscr;
    147 
    148 		uc.uc_flags |= _UC_FPU;
    149 	}
    150 
    151 	return cpu_setmcontext32(l, &uc.uc_mcontext, uc.uc_flags);
    152 }
    153 
    154 void
    155 linux32_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
    156 {
    157 	struct lwp * const l = curlwp;
    158 	struct proc * const p = l->l_proc;
    159 	struct trapframe * const tf = lwp_trapframe(l);
    160 	stack_t * const ss = &l->l_sigstk;
    161 	const int sig = ksi->ksi_signo;
    162 	const sig_t handler = SIGACTION(p, sig).sa_handler;
    163 	struct linux32_rt_sigframe rt_sigframe_buf, *u_rt_sigframe;
    164 	struct linux32_sigframe *tmp_sigframe, *u_sigframe;
    165 	struct linux32_siginfo *tmp_siginfo, *u_siginfo;
    166 	vaddr_t sp;
    167 	int error;
    168 	const bool onstack_p = /* use signal stack? */
    169 	    (ss->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    170 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    171 	const bool rt_p = !!(SIGACTION(curproc, ksi->ksi_signo).sa_flags &
    172 	    SA_SIGINFO);
    173 
    174 	sp = onstack_p ? ((vaddr_t)ss->ss_sp + ss->ss_size) & -16 :
    175 	    tf->tf_reg[13];
    176 
    177 	memset(&rt_sigframe_buf, 0, sizeof(rt_sigframe_buf));
    178 
    179 	if (rt_p) {
    180 		/* allocate rt_sigframe on stack */
    181 		sp -= sizeof(struct linux32_rt_sigframe);
    182 		u_rt_sigframe = (struct linux32_rt_sigframe *)sp;
    183 		u_sigframe = &u_rt_sigframe->sig;
    184 		u_siginfo = &u_rt_sigframe->info;
    185 		tmp_sigframe = &rt_sigframe_buf.sig;
    186 		tmp_siginfo = &rt_sigframe_buf.info;
    187 	} else {
    188 		/* allocate sigframe on stack */
    189 		sp -= sizeof(struct linux32_sigframe);
    190 		u_rt_sigframe = NULL;
    191 		u_sigframe = (struct linux32_sigframe *)sp;
    192 		u_siginfo = NULL;
    193 		tmp_sigframe = &rt_sigframe_buf.sig;
    194 		tmp_siginfo = NULL;
    195 	}
    196 
    197 	if ((vaddr_t)sp >= VM_MAXUSER_ADDRESS32) {
    198 		sigexit(l, SIGILL);
    199 		return;
    200 	}
    201 
    202 	/* build linux sigframe, and copyout to user stack */
    203 	tmp_sigframe->uc.luc_flags = 0;
    204 	tmp_sigframe->uc.luc_link = NULL;
    205 	NETBSD32PTR32(tmp_sigframe->uc.luc_stack.ss_sp, ss->ss_sp);
    206 	tmp_sigframe->uc.luc_stack.ss_size = ss->ss_size;
    207 	tmp_sigframe->uc.luc_stack.ss_flags = 0;
    208 	if (ss->ss_flags & SS_ONSTACK)
    209 		tmp_sigframe->uc.luc_stack.ss_flags |= LINUX_SS_ONSTACK;
    210 	if (ss->ss_flags & SS_DISABLE)
    211 		tmp_sigframe->uc.luc_stack.ss_flags |= LINUX_SS_DISABLE;
    212 	native_to_linux32_sigset(&tmp_sigframe->uc.luc_sigmask, mask);
    213 	if (tmp_siginfo != NULL)
    214 		native_to_linux32_siginfo(tmp_siginfo, &ksi->ksi_info);
    215 	sendsig_reset(l, sig);
    216 
    217 	mutex_exit(p->p_lock);
    218 	linux32_save_sigcontext(l, &tmp_sigframe->uc);
    219 
    220 	/* copy linux sigframe onto the user stack */
    221 	if (rt_p) {
    222 		error = copyout(&rt_sigframe_buf, u_rt_sigframe,
    223 		    sizeof(rt_sigframe_buf));
    224 	} else {
    225 		error = copyout(tmp_sigframe, u_sigframe,
    226 		    sizeof(*tmp_sigframe));
    227 	}
    228 
    229 	mutex_enter(p->p_lock);
    230 
    231 	if (error != 0 || (vaddr_t)handler >= VM_MAXUSER_ADDRESS32) {
    232 		sigexit(l, SIGILL);
    233 		return;
    234 	}
    235 
    236 	/* build context to run handler in. */
    237 	tf->tf_reg[0] = native_to_linux_signo[sig];
    238 	tf->tf_reg[1] = NETBSD32PTR32I(u_siginfo);
    239 	tf->tf_reg[2] = NETBSD32PTR32I(&u_sigframe->uc);
    240 	tf->tf_pc = (uint64_t)handler;
    241 	tf->tf_reg[13] = sp;
    242 
    243 	/* sigreturn trampoline */
    244 	extern char linux32_sigcode[], linux32_rt_sigcode[];
    245 	vsize_t linux_sigcode_offset;
    246 	if (rt_p) {
    247 		/* set return address to linux32_rt_sigcode() */
    248 		linux_sigcode_offset = linux32_rt_sigcode - linux32_sigcode;
    249 	} else {
    250 		/* set return address to linux32_sigcode() */
    251 		linux_sigcode_offset = linux32_sigcode - linux32_sigcode;
    252 	}
    253 	/* tf->tf_reg[14] is aarch32 lr */
    254 	tf->tf_reg[14] = NETBSD32PTR32I((char *)p->p_sigctx.ps_sigcode +
    255 	    linux_sigcode_offset);
    256 
    257 	if (onstack_p)
    258 		ss->ss_flags |= SS_ONSTACK;
    259 }
    260 
    261 int
    262 linux32_sys_sigreturn(struct lwp *l,
    263     const struct linux32_sys_sigreturn_args *uap, register_t *retval)
    264 {
    265 	struct trapframe * const tf = lwp_trapframe(l);
    266 	struct linux32_sigframe sigframe;
    267 	int error;
    268 
    269 	error = copyin((void *)tf->tf_reg[13], &sigframe, sizeof(sigframe));
    270 	if (error != 0)
    271 		goto done;
    272 
    273 	error = linux32_restore_sigcontext(l, &sigframe.uc);
    274 	if (error != 0)
    275 		goto done;
    276 	error = EJUSTRETURN;
    277 
    278  done:
    279 	return error;
    280 }
    281 
    282 int
    283 linux32_sys_rt_sigreturn(struct lwp *l,
    284     const struct linux32_sys_rt_sigreturn_args *uap, register_t *retval)
    285 {
    286 	struct trapframe * const tf = lwp_trapframe(l);
    287 	struct linux32_rt_sigframe rt_sigframe;
    288 	int error;
    289 
    290 	error = copyin((void *)tf->tf_reg[13], &rt_sigframe,
    291 	    sizeof(rt_sigframe));
    292 	if (error != 0)
    293 		goto done;
    294 
    295 	error = linux32_restore_sigcontext(l, &rt_sigframe.sig.uc);
    296 	if (error != 0)
    297 		goto done;
    298 	error = EJUSTRETURN;
    299 
    300  done:
    301 	return error;
    302 }
    303 
    304 void
    305 linux32_setregs(struct lwp *l, struct exec_package *pack, u_long stack)
    306 {
    307 	struct trapframe * const tf = lwp_trapframe(l);
    308 
    309 	netbsd32_setregs(l, pack, stack);
    310 
    311 	/* Same as netbsd32_setregs(), but some registers are set for linux */
    312 	tf->tf_reg[0] = 0;
    313 	tf->tf_reg[12] = 0;
    314 	tf->tf_reg[13] = stack;		/* sp */
    315 	tf->tf_reg[14] = pack->ep_entry;/* lr */
    316 	tf->tf_reg[18] = 0;
    317 	tf->tf_pc = pack->ep_entry;
    318 }
    319 
    320 int
    321 linux32_sys_set_tls(struct lwp *l, const struct linux32_sys_set_tls_args *uap,
    322     register_t *retval)
    323 {
    324 	/* {
    325 		syscallarg(netbsd32_voidp) tls;
    326 	} */
    327 	return lwp_setprivate(l, SCARG_P32(uap, tls));
    328 }
    329 
    330 int
    331 linux32_sys_get_tls(struct lwp *l, const void *uap, register_t *retval)
    332 {
    333 	retval[0] = NETBSD32PTR32I(l->l_private);
    334 	return 0;
    335 }
    336