linux_machdep.c revision 1.1 1 1.1 ryo /* $NetBSD: linux_machdep.c,v 1.1 2021/09/23 06:56:27 ryo Exp $ */
2 1.1 ryo
3 1.1 ryo /*-
4 1.1 ryo * Copyright (c) 2021 Ryo Shimizu <ryo (at) nerv.org>
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.1 ryo __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.1 2021/09/23 06:56:27 ryo 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.1 ryo #include <compat/linux/linux_syscall.h>
46 1.1 ryo #include <compat/linux/linux_syscallargs.h>
47 1.1 ryo
48 1.1 ryo void
49 1.1 ryo linux_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack)
50 1.1 ryo {
51 1.1 ryo setregs(l, epp, stack);
52 1.1 ryo }
53 1.1 ryo
54 1.1 ryo static void
55 1.1 ryo linux_save_sigcontext(struct lwp *l, struct linux_sigcontext *ctx)
56 1.1 ryo {
57 1.1 ryo ucontext_t uc;
58 1.1 ryo
59 1.1 ryo cpu_getmcontext(l, &uc.uc_mcontext, &uc.uc_flags);
60 1.1 ryo
61 1.1 ryo memset(ctx, 0, sizeof(*ctx));
62 1.1 ryo /* ctx->fault_address = 0; */
63 1.1 ryo CTASSERT(sizeof(ctx->regs) <= sizeof(uc.uc_mcontext.__gregs));
64 1.1 ryo for (int i = 0; i < __arraycount(ctx->regs); i++)
65 1.1 ryo ctx->regs[i] = uc.uc_mcontext.__gregs[i];
66 1.1 ryo ctx->sp = uc.uc_mcontext.__gregs[_REG_SP];
67 1.1 ryo ctx->pc = uc.uc_mcontext.__gregs[_REG_PC];
68 1.1 ryo ctx->pstate = uc.uc_mcontext.__gregs[_REG_SPSR];
69 1.1 ryo
70 1.1 ryo if (uc.uc_flags & _UC_FPU) {
71 1.1 ryo struct fpsimd_context *fpsimd;
72 1.1 ryo
73 1.1 ryo fpsimd = (struct fpsimd_context *)ctx->__reserved;
74 1.1 ryo fpsimd->head.magic = FPSIMD_MAGIC;
75 1.1 ryo fpsimd->head.size = sizeof(struct fpsimd_context);
76 1.1 ryo fpsimd->fpsr = uc.uc_mcontext.__fregs.__fpsr;
77 1.1 ryo fpsimd->fpcr = uc.uc_mcontext.__fregs.__fpcr;
78 1.1 ryo CTASSERT(sizeof(fpsimd->vregs) ==
79 1.1 ryo sizeof(uc.uc_mcontext.__fregs.__qregs));
80 1.1 ryo memcpy(fpsimd->vregs, uc.uc_mcontext.__fregs.__qregs,
81 1.1 ryo sizeof(uc.uc_mcontext.__fregs.__qregs));
82 1.1 ryo }
83 1.1 ryo
84 1.1 ryo /* ctx->__reserved[] has already been terminated by memset() */
85 1.1 ryo }
86 1.1 ryo
87 1.1 ryo static void
88 1.1 ryo aarch64_linux_to_native_ucontext(ucontext_t *uc, struct linux_ucontext *luc)
89 1.1 ryo {
90 1.1 ryo struct linux_sigcontext *ctx = &luc->luc_mcontext;
91 1.1 ryo struct fpsimd_context *fpsimd;
92 1.1 ryo
93 1.1 ryo memset(uc, 0, sizeof(*uc));
94 1.1 ryo
95 1.1 ryo /* build .uc_flags, .uc_link, and .uc_sigmask */
96 1.1 ryo uc->uc_flags = (_UC_SIGMASK | _UC_CPU | _UC_STACK | _UC_CLRSTACK);
97 1.1 ryo uc->uc_link = NULL;
98 1.1 ryo linux_to_native_sigset(&uc->uc_sigmask, &luc->luc_sigmask);
99 1.1 ryo
100 1.1 ryo /* build .uc_stack */
101 1.1 ryo if (luc->luc_stack.ss_flags & LINUX_SS_ONSTACK)
102 1.1 ryo uc->uc_stack.ss_flags |= SS_ONSTACK;
103 1.1 ryo if (luc->luc_stack.ss_flags & LINUX_SS_DISABLE)
104 1.1 ryo uc->uc_stack.ss_flags |= SS_DISABLE;
105 1.1 ryo uc->uc_stack.ss_sp = luc->luc_stack.ss_sp;
106 1.1 ryo uc->uc_stack.ss_size = luc->luc_stack.ss_size;
107 1.1 ryo
108 1.1 ryo /* build .uc_mcontext */
109 1.1 ryo CTASSERT(sizeof(ctx->regs) <= sizeof(uc->uc_mcontext.__gregs));
110 1.1 ryo for (int i = 0; i < __arraycount(ctx->regs); i++)
111 1.1 ryo uc->uc_mcontext.__gregs[i] = ctx->regs[i];
112 1.1 ryo uc->uc_mcontext.__gregs[_REG_SP] = ctx->sp;
113 1.1 ryo uc->uc_mcontext.__gregs[_REG_PC] = ctx->pc;
114 1.1 ryo uc->uc_mcontext.__gregs[_REG_SPSR] = ctx->pstate;
115 1.1 ryo
116 1.1 ryo fpsimd = (struct fpsimd_context *)ctx->__reserved;
117 1.1 ryo if (fpsimd->head.magic == FPSIMD_MAGIC) {
118 1.1 ryo uc->uc_flags |= _UC_FPU;
119 1.1 ryo uc->uc_mcontext.__fregs.__fpsr = fpsimd->fpsr;
120 1.1 ryo uc->uc_mcontext.__fregs.__fpcr = fpsimd->fpcr;
121 1.1 ryo CTASSERT(sizeof(fpsimd->vregs) ==
122 1.1 ryo sizeof(uc->uc_mcontext.__fregs.__qregs));
123 1.1 ryo memcpy(uc->uc_mcontext.__fregs.__qregs, fpsimd->vregs,
124 1.1 ryo sizeof(uc->uc_mcontext.__fregs.__qregs));
125 1.1 ryo }
126 1.1 ryo }
127 1.1 ryo
128 1.1 ryo void
129 1.1 ryo linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
130 1.1 ryo {
131 1.1 ryo struct lwp * const l = curlwp;
132 1.1 ryo struct proc * const p = l->l_proc;
133 1.1 ryo struct trapframe * const tf = lwp_trapframe(l);
134 1.1 ryo struct sigaltstack * const ss = &l->l_sigstk;
135 1.1 ryo const int sig = ksi->ksi_signo;
136 1.1 ryo const sig_t handler = SIGACTION(p, sig).sa_handler;
137 1.1 ryo struct linux_rt_sigframe *u_sigframe, *tmp_sigframe;
138 1.1 ryo vaddr_t sp;
139 1.1 ryo int error;
140 1.1 ryo const bool onstack_p = /* use signal stack? */
141 1.1 ryo (ss->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
142 1.1 ryo (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
143 1.1 ryo
144 1.1 ryo sp = onstack_p ? ((vaddr_t)ss->ss_sp + ss->ss_size) & -16 : tf->tf_sp;
145 1.1 ryo
146 1.1 ryo /* allocate sigframe on userstack */
147 1.1 ryo sp -= sizeof(struct linux_rt_sigframe);
148 1.1 ryo u_sigframe = (struct linux_rt_sigframe *)sp;
149 1.1 ryo
150 1.1 ryo /* build linux sigframe, and copyout to user stack */
151 1.1 ryo tmp_sigframe = kmem_zalloc(sizeof(*tmp_sigframe), KM_SLEEP);
152 1.1 ryo tmp_sigframe->uc.luc_flags = 0;
153 1.1 ryo tmp_sigframe->uc.luc_link = NULL;
154 1.1 ryo tmp_sigframe->uc.luc_stack.ss_sp = ss->ss_sp;
155 1.1 ryo tmp_sigframe->uc.luc_stack.ss_size = ss->ss_size;
156 1.1 ryo tmp_sigframe->uc.luc_stack.ss_flags = 0;
157 1.1 ryo if (ss->ss_flags & SS_ONSTACK)
158 1.1 ryo tmp_sigframe->uc.luc_stack.ss_flags |= LINUX_SS_ONSTACK;
159 1.1 ryo if (ss->ss_flags & SS_DISABLE)
160 1.1 ryo tmp_sigframe->uc.luc_stack.ss_flags |= LINUX_SS_DISABLE;
161 1.1 ryo native_to_linux_sigset(&tmp_sigframe->uc.luc_sigmask, mask);
162 1.1 ryo native_to_linux_siginfo(&tmp_sigframe->info, &ksi->ksi_info);
163 1.1 ryo sendsig_reset(l, sig);
164 1.1 ryo
165 1.1 ryo mutex_exit(p->p_lock);
166 1.1 ryo linux_save_sigcontext(l, &tmp_sigframe->uc.luc_mcontext);
167 1.1 ryo
168 1.1 ryo /* copy linux sigframe onto the user stack */
169 1.1 ryo error = copyout(tmp_sigframe, u_sigframe, sizeof(*tmp_sigframe));
170 1.1 ryo kmem_free(tmp_sigframe, sizeof(*tmp_sigframe));
171 1.1 ryo
172 1.1 ryo mutex_enter(p->p_lock);
173 1.1 ryo
174 1.1 ryo if (error != 0 || (vaddr_t)handler >= VM_MAXUSER_ADDRESS) {
175 1.1 ryo sigexit(l, SIGILL);
176 1.1 ryo return;
177 1.1 ryo }
178 1.1 ryo
179 1.1 ryo /* build context to run handler in. */
180 1.1 ryo tf->tf_reg[0] = native_to_linux_signo[sig];
181 1.1 ryo tf->tf_reg[1] = (uint64_t)&u_sigframe->info;
182 1.1 ryo tf->tf_reg[2] = (uint64_t)&u_sigframe->uc;
183 1.1 ryo tf->tf_pc = (uint64_t)handler;
184 1.1 ryo tf->tf_sp = sp;
185 1.1 ryo
186 1.1 ryo /* sigreturn trampoline */
187 1.1 ryo extern char linux_sigcode[], linux_rt_sigcode[];
188 1.1 ryo vsize_t linux_rt_sigcode_offset = linux_rt_sigcode - linux_sigcode;
189 1.1 ryo tf->tf_lr = (uint64_t)p->p_sigctx.ps_sigcode + linux_rt_sigcode_offset;
190 1.1 ryo
191 1.1 ryo if (onstack_p)
192 1.1 ryo ss->ss_flags |= SS_ONSTACK;
193 1.1 ryo }
194 1.1 ryo
195 1.1 ryo int
196 1.1 ryo linux_sys_rt_sigreturn(struct lwp *l, const void *v, register_t *retval)
197 1.1 ryo {
198 1.1 ryo struct trapframe * const tf = lwp_trapframe(l);
199 1.1 ryo struct proc * const p = l->l_proc;
200 1.1 ryo struct linux_rt_sigframe *lsigframe;
201 1.1 ryo ucontext_t uc;
202 1.1 ryo int error;
203 1.1 ryo
204 1.1 ryo /*
205 1.1 ryo * struct linux_rt_sigframe is variable size,
206 1.1 ryo * but netbsd/aarch64's linux_sendsig() always pushes a full size
207 1.1 ryo * linux_rt_sigframe.
208 1.1 ryo */
209 1.1 ryo lsigframe = kmem_zalloc(sizeof(*lsigframe), KM_SLEEP);
210 1.1 ryo error = copyin((void *)tf->tf_sp, lsigframe, sizeof(*lsigframe));
211 1.1 ryo if (error != 0)
212 1.1 ryo goto done;
213 1.1 ryo
214 1.1 ryo aarch64_linux_to_native_ucontext(&uc, &lsigframe->uc);
215 1.1 ryo
216 1.1 ryo mutex_enter(p->p_lock);
217 1.1 ryo error = setucontext(l, &uc);
218 1.1 ryo mutex_exit(p->p_lock);
219 1.1 ryo if (error != 0)
220 1.1 ryo goto done;
221 1.1 ryo error = EJUSTRETURN;
222 1.1 ryo
223 1.1 ryo done:
224 1.1 ryo kmem_free(lsigframe, sizeof(*lsigframe));
225 1.1 ryo return error;
226 1.1 ryo }
227 1.1 ryo
228 1.1 ryo void
229 1.1 ryo linux_trapsignal(struct lwp *l, ksiginfo_t *ksi)
230 1.1 ryo {
231 1.1 ryo /*
232 1.1 ryo * XXX: TODO
233 1.1 ryo * should be convert siginfo from native to linux
234 1.1 ryo */
235 1.1 ryo trapsignal(l, ksi);
236 1.1 ryo }
237 1.1 ryo
238 1.1 ryo int
239 1.1 ryo linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
240 1.1 ryo {
241 1.1 ryo /* not used */
242 1.1 ryo return 0;
243 1.1 ryo }
244 1.1 ryo
245 1.1 ryo dev_t
246 1.1 ryo linux_fakedev(dev_t dev, int raw)
247 1.1 ryo {
248 1.1 ryo /* TODO if needed */
249 1.1 ryo return dev;
250 1.1 ryo }
251 1.1 ryo
252 1.1 ryo int
253 1.1 ryo linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap,
254 1.1 ryo register_t *retval)
255 1.1 ryo {
256 1.1 ryo /* TODO if needed */
257 1.1 ryo return EINVAL;
258 1.1 ryo }
259