linux32_machdep.c revision 1.1 1 1.1 ryo /* $NetBSD: linux32_machdep.c,v 1.1 2021/11/25 03:08:04 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: linux32_machdep.c,v 1.1 2021/11/25 03:08:04 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
37 1.1 ryo #include <compat/netbsd32/netbsd32.h>
38 1.1 ryo #include <compat/netbsd32/netbsd32_exec.h>
39 1.1 ryo
40 1.1 ryo #include <compat/linux/common/linux_types.h>
41 1.1 ryo #include <compat/linux/common/linux_signal.h>
42 1.1 ryo #include <compat/linux/common/linux_errno.h>
43 1.1 ryo #include <compat/linux/common/linux_machdep.h>
44 1.1 ryo
45 1.1 ryo #include <compat/linux32/common/linux32_types.h>
46 1.1 ryo #include <compat/linux32/common/linux32_signal.h>
47 1.1 ryo #include <compat/linux32/common/linux32_errno.h>
48 1.1 ryo #include <compat/linux32/common/linux32_exec.h>
49 1.1 ryo #include <compat/linux32/common/linux32_machdep.h>
50 1.1 ryo #include <compat/linux32/linux32_syscall.h>
51 1.1 ryo #include <compat/linux32/linux32_syscallargs.h>
52 1.1 ryo
53 1.1 ryo #include <machine/netbsd32_machdep.h>
54 1.1 ryo #include <arm/vfpreg.h>
55 1.1 ryo
56 1.1 ryo static void
57 1.1 ryo linux32_save_sigcontext(struct lwp *l, struct linux32_ucontext *luc)
58 1.1 ryo {
59 1.1 ryo ucontext32_t uc;
60 1.1 ryo __greg32_t *gr = uc.uc_mcontext.__gregs;
61 1.1 ryo __vfpregset32_t *vfpregs = &uc.uc_mcontext.__vfpregs;
62 1.1 ryo struct linux32_aux_sigframe *aux;
63 1.1 ryo int i;
64 1.1 ryo
65 1.1 ryo cpu_getmcontext32(l, &uc.uc_mcontext, &uc.uc_flags);
66 1.1 ryo
67 1.1 ryo memset(luc, 0, sizeof(*luc));
68 1.1 ryo luc->luc_mcontext.arm_r0 = gr[_REG_R0];
69 1.1 ryo luc->luc_mcontext.arm_r1 = gr[_REG_R1];
70 1.1 ryo luc->luc_mcontext.arm_r2 = gr[_REG_R2];
71 1.1 ryo luc->luc_mcontext.arm_r3 = gr[_REG_R3];
72 1.1 ryo luc->luc_mcontext.arm_r4 = gr[_REG_R4];
73 1.1 ryo luc->luc_mcontext.arm_r5 = gr[_REG_R5];
74 1.1 ryo luc->luc_mcontext.arm_r6 = gr[_REG_R6];
75 1.1 ryo luc->luc_mcontext.arm_r7 = gr[_REG_R7];
76 1.1 ryo luc->luc_mcontext.arm_r8 = gr[_REG_R8];
77 1.1 ryo luc->luc_mcontext.arm_r9 = gr[_REG_R9];
78 1.1 ryo luc->luc_mcontext.arm_r10 = gr[_REG_R10];
79 1.1 ryo luc->luc_mcontext.arm_fp = gr[_REG_R11];
80 1.1 ryo luc->luc_mcontext.arm_ip = gr[_REG_R12];
81 1.1 ryo luc->luc_mcontext.arm_sp = gr[_REG_R13];
82 1.1 ryo luc->luc_mcontext.arm_lr = gr[_REG_R14];
83 1.1 ryo luc->luc_mcontext.arm_pc = gr[_REG_R15];
84 1.1 ryo luc->luc_mcontext.arm_cpsr = gr[_REG_CPSR];
85 1.1 ryo luc->luc_mcontext.trap_no = 0;
86 1.1 ryo luc->luc_mcontext.error_code = 0;
87 1.1 ryo luc->luc_mcontext.oldmask = 0;
88 1.1 ryo luc->luc_mcontext.fault_address = 0;
89 1.1 ryo
90 1.1 ryo if (uc.uc_flags & _UC_FPU) {
91 1.1 ryo aux = (struct linux32_aux_sigframe *)luc->luc_regspace;
92 1.1 ryo #define LINUX32_VFP_MAGIC 0x56465001
93 1.1 ryo aux->vfp.magic = LINUX32_VFP_MAGIC;
94 1.1 ryo aux->vfp.size = sizeof(struct linux32_vfp_sigframe);
95 1.1 ryo
96 1.1 ryo CTASSERT(__arraycount(aux->vfp.ufp.fpregs) ==
97 1.1 ryo __arraycount(vfpregs->__vfp_fstmx));
98 1.1 ryo for (i = 0; i < __arraycount(aux->vfp.ufp.fpregs); i++)
99 1.1 ryo aux->vfp.ufp.fpregs[i] = vfpregs->__vfp_fstmx[i];
100 1.1 ryo aux->vfp.ufp.fpscr = vfpregs->__vfp_fpscr;
101 1.1 ryo
102 1.1 ryo aux->vfp.ufp_exc.fpexc = VFP_FPEXC_EN | VFP_FPEXC_VECITR;
103 1.1 ryo aux->vfp.ufp_exc.fpinst = 0;
104 1.1 ryo aux->vfp.ufp_exc.fpinst2 = 0;
105 1.1 ryo aux->end_magic = 0 ;
106 1.1 ryo }
107 1.1 ryo }
108 1.1 ryo
109 1.1 ryo static int
110 1.1 ryo linux32_restore_sigcontext(struct lwp *l, struct linux32_ucontext *luc)
111 1.1 ryo {
112 1.1 ryo ucontext32_t uc;
113 1.1 ryo __greg32_t *gr = uc.uc_mcontext.__gregs;
114 1.1 ryo __vfpregset32_t *vfpregs = &uc.uc_mcontext.__vfpregs;
115 1.1 ryo struct linux32_aux_sigframe *aux;
116 1.1 ryo int i;
117 1.1 ryo
118 1.1 ryo memset(&uc, 0, sizeof(uc));
119 1.1 ryo gr[_REG_R0] = luc->luc_mcontext.arm_r0;
120 1.1 ryo gr[_REG_R1] = luc->luc_mcontext.arm_r1;
121 1.1 ryo gr[_REG_R2] = luc->luc_mcontext.arm_r2;
122 1.1 ryo gr[_REG_R3] = luc->luc_mcontext.arm_r3;
123 1.1 ryo gr[_REG_R4] = luc->luc_mcontext.arm_r4;
124 1.1 ryo gr[_REG_R5] = luc->luc_mcontext.arm_r5;
125 1.1 ryo gr[_REG_R6] = luc->luc_mcontext.arm_r6;
126 1.1 ryo gr[_REG_R7] = luc->luc_mcontext.arm_r7;
127 1.1 ryo gr[_REG_R8] = luc->luc_mcontext.arm_r8;
128 1.1 ryo gr[_REG_R9] = luc->luc_mcontext.arm_r9;
129 1.1 ryo gr[_REG_R10] = luc->luc_mcontext.arm_r10;
130 1.1 ryo gr[_REG_R11] = luc->luc_mcontext.arm_fp;
131 1.1 ryo gr[_REG_R12] = luc->luc_mcontext.arm_ip;
132 1.1 ryo gr[_REG_R13] = luc->luc_mcontext.arm_sp;
133 1.1 ryo gr[_REG_R14] = luc->luc_mcontext.arm_lr;
134 1.1 ryo gr[_REG_R15] = luc->luc_mcontext.arm_pc;
135 1.1 ryo gr[_REG_CPSR] = luc->luc_mcontext.arm_cpsr;
136 1.1 ryo uc.uc_flags |= _UC_CPU;
137 1.1 ryo
138 1.1 ryo aux = (struct linux32_aux_sigframe *)luc->luc_regspace;
139 1.1 ryo if (aux->vfp.magic == LINUX32_VFP_MAGIC &&
140 1.1 ryo aux->vfp.size == sizeof(struct linux32_vfp_sigframe)) {
141 1.1 ryo
142 1.1 ryo CTASSERT(__arraycount(vfpregs->__vfp_fstmx) ==
143 1.1 ryo __arraycount(aux->vfp.ufp.fpregs));
144 1.1 ryo for (i = 0; i < __arraycount(vfpregs->__vfp_fstmx); i++)
145 1.1 ryo vfpregs->__vfp_fstmx[i] = aux->vfp.ufp.fpregs[i];
146 1.1 ryo vfpregs->__vfp_fpscr = aux->vfp.ufp.fpscr;
147 1.1 ryo
148 1.1 ryo uc.uc_flags |= _UC_FPU;
149 1.1 ryo }
150 1.1 ryo
151 1.1 ryo return cpu_setmcontext32(l, &uc.uc_mcontext, uc.uc_flags);
152 1.1 ryo }
153 1.1 ryo
154 1.1 ryo void
155 1.1 ryo linux32_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
156 1.1 ryo {
157 1.1 ryo struct lwp * const l = curlwp;
158 1.1 ryo struct proc * const p = l->l_proc;
159 1.1 ryo struct trapframe * const tf = lwp_trapframe(l);
160 1.1 ryo stack_t * const ss = &l->l_sigstk;
161 1.1 ryo const int sig = ksi->ksi_signo;
162 1.1 ryo const sig_t handler = SIGACTION(p, sig).sa_handler;
163 1.1 ryo struct linux32_rt_sigframe rt_sigframe_buf, *u_rt_sigframe;
164 1.1 ryo struct linux32_sigframe *tmp_sigframe, *u_sigframe;
165 1.1 ryo struct linux32_siginfo *tmp_siginfo, *u_siginfo;
166 1.1 ryo vaddr_t sp;
167 1.1 ryo int error;
168 1.1 ryo const bool onstack_p = /* use signal stack? */
169 1.1 ryo (ss->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
170 1.1 ryo (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
171 1.1 ryo const bool rt_p = !!(SIGACTION(curproc, ksi->ksi_signo).sa_flags &
172 1.1 ryo SA_SIGINFO);
173 1.1 ryo
174 1.1 ryo sp = onstack_p ? ((vaddr_t)ss->ss_sp + ss->ss_size) & -16 :
175 1.1 ryo tf->tf_reg[13];
176 1.1 ryo
177 1.1 ryo memset(&rt_sigframe_buf, 0, sizeof(rt_sigframe_buf));
178 1.1 ryo
179 1.1 ryo if (rt_p) {
180 1.1 ryo /* allocate rt_sigframe on stack */
181 1.1 ryo sp -= sizeof(struct linux32_rt_sigframe);
182 1.1 ryo u_rt_sigframe = (struct linux32_rt_sigframe *)sp;
183 1.1 ryo u_sigframe = &u_rt_sigframe->sig;
184 1.1 ryo u_siginfo = &u_rt_sigframe->info;
185 1.1 ryo tmp_sigframe = &rt_sigframe_buf.sig;
186 1.1 ryo tmp_siginfo = &rt_sigframe_buf.info;
187 1.1 ryo } else {
188 1.1 ryo /* allocate sigframe on stack */
189 1.1 ryo sp -= sizeof(struct linux32_sigframe);
190 1.1 ryo u_rt_sigframe = NULL;
191 1.1 ryo u_sigframe = (struct linux32_sigframe *)sp;
192 1.1 ryo u_siginfo = NULL;
193 1.1 ryo tmp_sigframe = &rt_sigframe_buf.sig;
194 1.1 ryo tmp_siginfo = NULL;
195 1.1 ryo }
196 1.1 ryo
197 1.1 ryo if ((vaddr_t)sp >= VM_MAXUSER_ADDRESS32) {
198 1.1 ryo sigexit(l, SIGILL);
199 1.1 ryo return;
200 1.1 ryo }
201 1.1 ryo
202 1.1 ryo /* build linux sigframe, and copyout to user stack */
203 1.1 ryo tmp_sigframe->uc.luc_flags = 0;
204 1.1 ryo tmp_sigframe->uc.luc_link = NULL;
205 1.1 ryo NETBSD32PTR32(tmp_sigframe->uc.luc_stack.ss_sp, ss->ss_sp);
206 1.1 ryo tmp_sigframe->uc.luc_stack.ss_size = ss->ss_size;
207 1.1 ryo tmp_sigframe->uc.luc_stack.ss_flags = 0;
208 1.1 ryo if (ss->ss_flags & SS_ONSTACK)
209 1.1 ryo tmp_sigframe->uc.luc_stack.ss_flags |= LINUX_SS_ONSTACK;
210 1.1 ryo if (ss->ss_flags & SS_DISABLE)
211 1.1 ryo tmp_sigframe->uc.luc_stack.ss_flags |= LINUX_SS_DISABLE;
212 1.1 ryo native_to_linux32_sigset(&tmp_sigframe->uc.luc_sigmask, mask);
213 1.1 ryo if (tmp_siginfo != NULL)
214 1.1 ryo native_to_linux32_siginfo(tmp_siginfo, &ksi->ksi_info);
215 1.1 ryo sendsig_reset(l, sig);
216 1.1 ryo
217 1.1 ryo mutex_exit(p->p_lock);
218 1.1 ryo linux32_save_sigcontext(l, &tmp_sigframe->uc);
219 1.1 ryo
220 1.1 ryo /* copy linux sigframe onto the user stack */
221 1.1 ryo if (rt_p) {
222 1.1 ryo error = copyout(&rt_sigframe_buf, u_rt_sigframe,
223 1.1 ryo sizeof(rt_sigframe_buf));
224 1.1 ryo } else {
225 1.1 ryo error = copyout(tmp_sigframe, u_sigframe,
226 1.1 ryo sizeof(*tmp_sigframe));
227 1.1 ryo }
228 1.1 ryo
229 1.1 ryo mutex_enter(p->p_lock);
230 1.1 ryo
231 1.1 ryo if (error != 0 || (vaddr_t)handler >= VM_MAXUSER_ADDRESS32) {
232 1.1 ryo sigexit(l, SIGILL);
233 1.1 ryo return;
234 1.1 ryo }
235 1.1 ryo
236 1.1 ryo /* build context to run handler in. */
237 1.1 ryo tf->tf_reg[0] = native_to_linux_signo[sig];
238 1.1 ryo tf->tf_reg[1] = NETBSD32PTR32I(u_siginfo);
239 1.1 ryo tf->tf_reg[2] = NETBSD32PTR32I(&u_sigframe->uc);
240 1.1 ryo tf->tf_pc = (uint64_t)handler;
241 1.1 ryo tf->tf_reg[13] = sp;
242 1.1 ryo
243 1.1 ryo /* sigreturn trampoline */
244 1.1 ryo extern char linux32_sigcode[], linux32_rt_sigcode[];
245 1.1 ryo vsize_t linux_sigcode_offset;
246 1.1 ryo if (rt_p) {
247 1.1 ryo /* set return address to linux32_rt_sigcode() */
248 1.1 ryo linux_sigcode_offset = linux32_rt_sigcode - linux32_sigcode;
249 1.1 ryo } else {
250 1.1 ryo /* set return address to linux32_sigcode() */
251 1.1 ryo linux_sigcode_offset = linux32_sigcode - linux32_sigcode;
252 1.1 ryo }
253 1.1 ryo /* tf->tf_reg[14] is aarch32 lr */
254 1.1 ryo tf->tf_reg[14] = NETBSD32PTR32I((char *)p->p_sigctx.ps_sigcode +
255 1.1 ryo linux_sigcode_offset);
256 1.1 ryo
257 1.1 ryo if (onstack_p)
258 1.1 ryo ss->ss_flags |= SS_ONSTACK;
259 1.1 ryo }
260 1.1 ryo
261 1.1 ryo int
262 1.1 ryo linux32_sys_sigreturn(struct lwp *l,
263 1.1 ryo const struct linux32_sys_sigreturn_args *uap, register_t *retval)
264 1.1 ryo {
265 1.1 ryo struct trapframe * const tf = lwp_trapframe(l);
266 1.1 ryo struct linux32_sigframe sigframe;
267 1.1 ryo int error;
268 1.1 ryo
269 1.1 ryo error = copyin((void *)tf->tf_reg[13], &sigframe, sizeof(sigframe));
270 1.1 ryo if (error != 0)
271 1.1 ryo goto done;
272 1.1 ryo
273 1.1 ryo error = linux32_restore_sigcontext(l, &sigframe.uc);
274 1.1 ryo if (error != 0)
275 1.1 ryo goto done;
276 1.1 ryo error = EJUSTRETURN;
277 1.1 ryo
278 1.1 ryo done:
279 1.1 ryo return error;
280 1.1 ryo }
281 1.1 ryo
282 1.1 ryo int
283 1.1 ryo linux32_sys_rt_sigreturn(struct lwp *l,
284 1.1 ryo const struct linux32_sys_rt_sigreturn_args *uap, register_t *retval)
285 1.1 ryo {
286 1.1 ryo struct trapframe * const tf = lwp_trapframe(l);
287 1.1 ryo struct linux32_rt_sigframe rt_sigframe;
288 1.1 ryo int error;
289 1.1 ryo
290 1.1 ryo error = copyin((void *)tf->tf_reg[13], &rt_sigframe,
291 1.1 ryo sizeof(rt_sigframe));
292 1.1 ryo if (error != 0)
293 1.1 ryo goto done;
294 1.1 ryo
295 1.1 ryo error = linux32_restore_sigcontext(l, &rt_sigframe.sig.uc);
296 1.1 ryo if (error != 0)
297 1.1 ryo goto done;
298 1.1 ryo error = EJUSTRETURN;
299 1.1 ryo
300 1.1 ryo done:
301 1.1 ryo return error;
302 1.1 ryo }
303 1.1 ryo
304 1.1 ryo void
305 1.1 ryo linux32_setregs(struct lwp *l, struct exec_package *pack, u_long stack)
306 1.1 ryo {
307 1.1 ryo struct trapframe * const tf = lwp_trapframe(l);
308 1.1 ryo
309 1.1 ryo netbsd32_setregs(l, pack, stack);
310 1.1 ryo
311 1.1 ryo /* Same as netbsd32_setregs(), but some registers are set for linux */
312 1.1 ryo tf->tf_reg[0] = 0;
313 1.1 ryo tf->tf_reg[12] = 0;
314 1.1 ryo tf->tf_reg[13] = stack; /* sp */
315 1.1 ryo tf->tf_reg[14] = pack->ep_entry;/* lr */
316 1.1 ryo tf->tf_reg[18] = 0;
317 1.1 ryo tf->tf_pc = pack->ep_entry;
318 1.1 ryo }
319 1.1 ryo
320 1.1 ryo int
321 1.1 ryo linux32_sys_set_tls(struct lwp *l, const struct linux32_sys_set_tls_args *uap,
322 1.1 ryo register_t *retval)
323 1.1 ryo {
324 1.1 ryo /* {
325 1.1 ryo syscallarg(netbsd32_voidp) tls;
326 1.1 ryo } */
327 1.1 ryo return lwp_setprivate(l, SCARG_P32(uap, tls));
328 1.1 ryo }
329 1.1 ryo
330 1.1 ryo int
331 1.1 ryo linux32_sys_get_tls(struct lwp *l, const void *uap, register_t *retval)
332 1.1 ryo {
333 1.1 ryo retval[0] = NETBSD32PTR32I(l->l_private);
334 1.1 ryo return 0;
335 1.1 ryo }
336