Home | History | Annotate | Line # | Download | only in aarch64
      1  1.4  msaitoh /*	$NetBSD: linux_machdep.c,v 1.4 2024/02/07 04:20:28 msaitoh Exp $	*/
      2  1.1      ryo 
      3  1.1      ryo /*-
      4  1.4  msaitoh  * Copyright (c) 2021 Ryo Shimizu
      5  1.1      ryo  * All rights reserved.
      6  1.1      ryo  *
      7  1.1      ryo  * Redistribution and use in source and binary forms, with or without
      8  1.1      ryo  * modification, are permitted provided that the following conditions
      9  1.1      ryo  * are met:
     10  1.1      ryo  * 1. Redistributions of source code must retain the above copyright
     11  1.1      ryo  *    notice, this list of conditions and the following disclaimer.
     12  1.1      ryo  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1      ryo  *    notice, this list of conditions and the following disclaimer in the
     14  1.1      ryo  *    documentation and/or other materials provided with the distribution.
     15  1.1      ryo  *
     16  1.1      ryo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     17  1.1      ryo  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  1.1      ryo  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1      ryo  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     20  1.1      ryo  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  1.1      ryo  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  1.1      ryo  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1      ryo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24  1.1      ryo  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     25  1.1      ryo  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1      ryo  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1      ryo  */
     28  1.1      ryo 
     29  1.1      ryo #include <sys/cdefs.h>
     30  1.4  msaitoh __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.4 2024/02/07 04:20:28 msaitoh Exp $");
     31  1.1      ryo 
     32  1.1      ryo #include <sys/param.h>
     33  1.1      ryo #include <sys/systm.h>
     34  1.1      ryo #include <sys/proc.h>
     35  1.1      ryo #include <sys/exec.h>
     36  1.1      ryo #include <sys/syscallargs.h>
     37  1.1      ryo 
     38  1.1      ryo #include <compat/linux/common/linux_types.h>
     39  1.1      ryo #include <compat/linux/common/linux_signal.h>
     40  1.1      ryo #include <compat/linux/common/linux_errno.h>
     41  1.1      ryo #include <compat/linux/common/linux_exec.h>
     42  1.1      ryo #include <compat/linux/common/linux_ioctl.h>
     43  1.1      ryo #include <compat/linux/common/linux_prctl.h>
     44  1.1      ryo #include <compat/linux/common/linux_machdep.h>
     45  1.2      ryo #include <compat/linux/common/linux_ipc.h>
     46  1.2      ryo #include <compat/linux/common/linux_sem.h>
     47  1.1      ryo #include <compat/linux/linux_syscall.h>
     48  1.1      ryo #include <compat/linux/linux_syscallargs.h>
     49  1.1      ryo 
     50  1.1      ryo void
     51  1.1      ryo linux_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack)
     52  1.1      ryo {
     53  1.1      ryo 	setregs(l, epp, stack);
     54  1.1      ryo }
     55  1.1      ryo 
     56  1.1      ryo static void
     57  1.1      ryo linux_save_sigcontext(struct lwp *l, struct linux_sigcontext *ctx)
     58  1.1      ryo {
     59  1.1      ryo 	ucontext_t uc;
     60  1.1      ryo 
     61  1.1      ryo 	cpu_getmcontext(l, &uc.uc_mcontext, &uc.uc_flags);
     62  1.1      ryo 
     63  1.1      ryo 	memset(ctx, 0, sizeof(*ctx));
     64  1.1      ryo 	/* ctx->fault_address = 0; */
     65  1.1      ryo 	CTASSERT(sizeof(ctx->regs) <= sizeof(uc.uc_mcontext.__gregs));
     66  1.1      ryo 	for (int i = 0; i < __arraycount(ctx->regs); i++)
     67  1.1      ryo 		ctx->regs[i] = uc.uc_mcontext.__gregs[i];
     68  1.1      ryo 	ctx->sp = uc.uc_mcontext.__gregs[_REG_SP];
     69  1.1      ryo 	ctx->pc = uc.uc_mcontext.__gregs[_REG_PC];
     70  1.1      ryo 	ctx->pstate = uc.uc_mcontext.__gregs[_REG_SPSR];
     71  1.1      ryo 
     72  1.1      ryo 	if (uc.uc_flags & _UC_FPU) {
     73  1.1      ryo 		struct fpsimd_context *fpsimd;
     74  1.1      ryo 
     75  1.1      ryo 		fpsimd = (struct fpsimd_context *)ctx->__reserved;
     76  1.1      ryo 		fpsimd->head.magic = FPSIMD_MAGIC;
     77  1.1      ryo 		fpsimd->head.size = sizeof(struct fpsimd_context);
     78  1.1      ryo 		fpsimd->fpsr = uc.uc_mcontext.__fregs.__fpsr;
     79  1.1      ryo 		fpsimd->fpcr = uc.uc_mcontext.__fregs.__fpcr;
     80  1.1      ryo 		CTASSERT(sizeof(fpsimd->vregs) ==
     81  1.1      ryo 		    sizeof(uc.uc_mcontext.__fregs.__qregs));
     82  1.1      ryo 		memcpy(fpsimd->vregs, uc.uc_mcontext.__fregs.__qregs,
     83  1.1      ryo 		    sizeof(uc.uc_mcontext.__fregs.__qregs));
     84  1.1      ryo 	}
     85  1.1      ryo 
     86  1.1      ryo 	/* ctx->__reserved[] has already been terminated by memset() */
     87  1.1      ryo }
     88  1.1      ryo 
     89  1.1      ryo static void
     90  1.1      ryo aarch64_linux_to_native_ucontext(ucontext_t *uc, struct linux_ucontext *luc)
     91  1.1      ryo {
     92  1.1      ryo 	struct linux_sigcontext *ctx = &luc->luc_mcontext;
     93  1.1      ryo 	struct fpsimd_context *fpsimd;
     94  1.1      ryo 
     95  1.1      ryo 	memset(uc, 0, sizeof(*uc));
     96  1.1      ryo 
     97  1.1      ryo 	/* build .uc_flags, .uc_link, and .uc_sigmask */
     98  1.1      ryo 	uc->uc_flags = (_UC_SIGMASK | _UC_CPU | _UC_STACK | _UC_CLRSTACK);
     99  1.1      ryo 	uc->uc_link = NULL;
    100  1.1      ryo 	linux_to_native_sigset(&uc->uc_sigmask, &luc->luc_sigmask);
    101  1.1      ryo 
    102  1.1      ryo 	/* build .uc_stack */
    103  1.1      ryo 	if (luc->luc_stack.ss_flags & LINUX_SS_ONSTACK)
    104  1.1      ryo 		uc->uc_stack.ss_flags |= SS_ONSTACK;
    105  1.1      ryo 	if (luc->luc_stack.ss_flags & LINUX_SS_DISABLE)
    106  1.1      ryo 		uc->uc_stack.ss_flags |= SS_DISABLE;
    107  1.1      ryo 	uc->uc_stack.ss_sp = luc->luc_stack.ss_sp;
    108  1.1      ryo 	uc->uc_stack.ss_size = luc->luc_stack.ss_size;
    109  1.1      ryo 
    110  1.1      ryo 	/* build .uc_mcontext */
    111  1.1      ryo 	CTASSERT(sizeof(ctx->regs) <= sizeof(uc->uc_mcontext.__gregs));
    112  1.1      ryo 	for (int i = 0; i < __arraycount(ctx->regs); i++)
    113  1.1      ryo 		uc->uc_mcontext.__gregs[i] = ctx->regs[i];
    114  1.1      ryo 	uc->uc_mcontext.__gregs[_REG_SP] = ctx->sp;
    115  1.1      ryo 	uc->uc_mcontext.__gregs[_REG_PC] = ctx->pc;
    116  1.1      ryo 	uc->uc_mcontext.__gregs[_REG_SPSR] = ctx->pstate;
    117  1.1      ryo 
    118  1.1      ryo 	fpsimd = (struct fpsimd_context *)ctx->__reserved;
    119  1.1      ryo 	if (fpsimd->head.magic == FPSIMD_MAGIC) {
    120  1.1      ryo 		uc->uc_flags |= _UC_FPU;
    121  1.1      ryo 		uc->uc_mcontext.__fregs.__fpsr = fpsimd->fpsr;
    122  1.1      ryo 		uc->uc_mcontext.__fregs.__fpcr = fpsimd->fpcr;
    123  1.1      ryo 		CTASSERT(sizeof(fpsimd->vregs) ==
    124  1.1      ryo 		    sizeof(uc->uc_mcontext.__fregs.__qregs));
    125  1.1      ryo 		memcpy(uc->uc_mcontext.__fregs.__qregs, fpsimd->vregs,
    126  1.1      ryo 		    sizeof(uc->uc_mcontext.__fregs.__qregs));
    127  1.1      ryo 	}
    128  1.1      ryo }
    129  1.1      ryo 
    130  1.1      ryo void
    131  1.1      ryo linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
    132  1.1      ryo {
    133  1.1      ryo 	struct lwp * const l = curlwp;
    134  1.1      ryo 	struct proc * const p = l->l_proc;
    135  1.1      ryo 	struct trapframe * const tf = lwp_trapframe(l);
    136  1.3  thorpej 	stack_t * const ss = &l->l_sigstk;
    137  1.1      ryo 	const int sig = ksi->ksi_signo;
    138  1.1      ryo 	const sig_t handler = SIGACTION(p, sig).sa_handler;
    139  1.1      ryo 	struct linux_rt_sigframe *u_sigframe, *tmp_sigframe;
    140  1.1      ryo 	vaddr_t sp;
    141  1.1      ryo 	int error;
    142  1.1      ryo 	const bool onstack_p = /* use signal stack? */
    143  1.1      ryo 	    (ss->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    144  1.1      ryo 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    145  1.1      ryo 
    146  1.1      ryo 	sp = onstack_p ? ((vaddr_t)ss->ss_sp + ss->ss_size) & -16 : tf->tf_sp;
    147  1.1      ryo 
    148  1.1      ryo 	/* allocate sigframe on userstack */
    149  1.1      ryo 	sp -= sizeof(struct linux_rt_sigframe);
    150  1.1      ryo 	u_sigframe = (struct linux_rt_sigframe *)sp;
    151  1.1      ryo 
    152  1.1      ryo 	/* build linux sigframe, and copyout to user stack */
    153  1.1      ryo 	tmp_sigframe = kmem_zalloc(sizeof(*tmp_sigframe), KM_SLEEP);
    154  1.1      ryo 	tmp_sigframe->uc.luc_flags = 0;
    155  1.1      ryo 	tmp_sigframe->uc.luc_link = NULL;
    156  1.1      ryo 	tmp_sigframe->uc.luc_stack.ss_sp = ss->ss_sp;
    157  1.1      ryo 	tmp_sigframe->uc.luc_stack.ss_size = ss->ss_size;
    158  1.1      ryo 	tmp_sigframe->uc.luc_stack.ss_flags = 0;
    159  1.1      ryo 	if (ss->ss_flags & SS_ONSTACK)
    160  1.1      ryo 		tmp_sigframe->uc.luc_stack.ss_flags |= LINUX_SS_ONSTACK;
    161  1.1      ryo 	if (ss->ss_flags & SS_DISABLE)
    162  1.1      ryo 		tmp_sigframe->uc.luc_stack.ss_flags |= LINUX_SS_DISABLE;
    163  1.1      ryo 	native_to_linux_sigset(&tmp_sigframe->uc.luc_sigmask, mask);
    164  1.1      ryo 	native_to_linux_siginfo(&tmp_sigframe->info, &ksi->ksi_info);
    165  1.1      ryo 	sendsig_reset(l, sig);
    166  1.1      ryo 
    167  1.1      ryo 	mutex_exit(p->p_lock);
    168  1.1      ryo 	linux_save_sigcontext(l, &tmp_sigframe->uc.luc_mcontext);
    169  1.1      ryo 
    170  1.1      ryo 	/* copy linux sigframe onto the user stack */
    171  1.1      ryo 	error = copyout(tmp_sigframe, u_sigframe, sizeof(*tmp_sigframe));
    172  1.1      ryo 	kmem_free(tmp_sigframe, sizeof(*tmp_sigframe));
    173  1.1      ryo 
    174  1.1      ryo 	mutex_enter(p->p_lock);
    175  1.1      ryo 
    176  1.1      ryo 	if (error != 0 || (vaddr_t)handler >= VM_MAXUSER_ADDRESS) {
    177  1.1      ryo 		sigexit(l, SIGILL);
    178  1.1      ryo 		return;
    179  1.1      ryo 	}
    180  1.1      ryo 
    181  1.1      ryo 	/* build context to run handler in. */
    182  1.1      ryo 	tf->tf_reg[0] = native_to_linux_signo[sig];
    183  1.1      ryo 	tf->tf_reg[1] = (uint64_t)&u_sigframe->info;
    184  1.1      ryo 	tf->tf_reg[2] = (uint64_t)&u_sigframe->uc;
    185  1.1      ryo 	tf->tf_pc = (uint64_t)handler;
    186  1.1      ryo 	tf->tf_sp = sp;
    187  1.1      ryo 
    188  1.1      ryo 	/* sigreturn trampoline */
    189  1.1      ryo 	extern char linux_sigcode[], linux_rt_sigcode[];
    190  1.1      ryo 	vsize_t linux_rt_sigcode_offset = linux_rt_sigcode - linux_sigcode;
    191  1.1      ryo 	tf->tf_lr = (uint64_t)p->p_sigctx.ps_sigcode + linux_rt_sigcode_offset;
    192  1.1      ryo 
    193  1.1      ryo 	if (onstack_p)
    194  1.1      ryo 		ss->ss_flags |= SS_ONSTACK;
    195  1.1      ryo }
    196  1.1      ryo 
    197  1.1      ryo int
    198  1.1      ryo linux_sys_rt_sigreturn(struct lwp *l, const void *v, register_t *retval)
    199  1.1      ryo {
    200  1.1      ryo 	struct trapframe * const tf = lwp_trapframe(l);
    201  1.1      ryo 	struct proc * const p = l->l_proc;
    202  1.1      ryo 	struct linux_rt_sigframe *lsigframe;
    203  1.1      ryo 	ucontext_t uc;
    204  1.1      ryo 	int error;
    205  1.1      ryo 
    206  1.1      ryo 	/*
    207  1.1      ryo 	 * struct linux_rt_sigframe is variable size,
    208  1.1      ryo 	 * but netbsd/aarch64's linux_sendsig() always pushes a full size
    209  1.1      ryo 	 * linux_rt_sigframe.
    210  1.1      ryo 	 */
    211  1.1      ryo 	lsigframe = kmem_zalloc(sizeof(*lsigframe), KM_SLEEP);
    212  1.1      ryo 	error = copyin((void *)tf->tf_sp, lsigframe, sizeof(*lsigframe));
    213  1.1      ryo 	if (error != 0)
    214  1.1      ryo 		goto done;
    215  1.1      ryo 
    216  1.1      ryo 	aarch64_linux_to_native_ucontext(&uc, &lsigframe->uc);
    217  1.1      ryo 
    218  1.1      ryo 	mutex_enter(p->p_lock);
    219  1.1      ryo 	error = setucontext(l, &uc);
    220  1.1      ryo 	mutex_exit(p->p_lock);
    221  1.1      ryo 	if (error != 0)
    222  1.1      ryo 		goto done;
    223  1.1      ryo 	error = EJUSTRETURN;
    224  1.1      ryo 
    225  1.1      ryo  done:
    226  1.1      ryo 	kmem_free(lsigframe, sizeof(*lsigframe));
    227  1.1      ryo 	return error;
    228  1.1      ryo }
    229  1.1      ryo 
    230  1.1      ryo void
    231  1.1      ryo linux_trapsignal(struct lwp *l, ksiginfo_t *ksi)
    232  1.1      ryo {
    233  1.1      ryo 	/*
    234  1.1      ryo 	 * XXX: TODO
    235  1.1      ryo 	 * should be convert siginfo from native to linux
    236  1.1      ryo 	 */
    237  1.1      ryo 	trapsignal(l, ksi);
    238  1.1      ryo }
    239  1.1      ryo 
    240  1.1      ryo int
    241  1.1      ryo linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
    242  1.1      ryo {
    243  1.1      ryo 	/* not used */
    244  1.1      ryo 	return 0;
    245  1.1      ryo }
    246  1.1      ryo 
    247  1.1      ryo dev_t
    248  1.1      ryo linux_fakedev(dev_t dev, int raw)
    249  1.1      ryo {
    250  1.1      ryo 	/* TODO if needed */
    251  1.1      ryo 	return dev;
    252  1.1      ryo }
    253  1.1      ryo 
    254  1.1      ryo int
    255  1.1      ryo linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap,
    256  1.1      ryo     register_t *retval)
    257  1.1      ryo {
    258  1.1      ryo 	/* TODO if needed */
    259  1.1      ryo 	return EINVAL;
    260  1.1      ryo }
    261