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