summaryrefslogtreecommitdiff
path: root/sys/compat/linux32/arch/aarch64/linux32_machdep.c
blob: 4f2b0a0e61ef5e4049d382471efce0a7cc473841 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*	$NetBSD: linux32_machdep.c,v 1.2 2021/12/03 09:20:23 ryo Exp $	*/

/*-
 * Copyright (c) 2021 Ryo Shimizu <ryo@nerv.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: linux32_machdep.c,v 1.2 2021/12/03 09:20:23 ryo Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/exec.h>

#include <compat/netbsd32/netbsd32.h>
#include <compat/netbsd32/netbsd32_exec.h>

#include <compat/linux/common/linux_types.h>
#include <compat/linux/common/linux_signal.h>
#include <compat/linux/common/linux_errno.h>
#include <compat/linux/common/linux_machdep.h>

#include <compat/linux32/common/linux32_types.h>
#include <compat/linux32/common/linux32_signal.h>
#include <compat/linux32/common/linux32_errno.h>
#include <compat/linux32/common/linux32_exec.h>
#include <compat/linux32/common/linux32_machdep.h>
#include <compat/linux32/linux32_syscall.h>
#include <compat/linux32/linux32_syscallargs.h>

#include <machine/netbsd32_machdep.h>
#include <arm/vfpreg.h>

static void
linux32_save_sigcontext(struct lwp *l, struct linux32_ucontext *luc)
{
	ucontext32_t uc;
	__greg32_t *gr = uc.uc_mcontext.__gregs;
	__vfpregset32_t *vfpregs = &uc.uc_mcontext.__vfpregs;
	struct linux32_aux_sigframe *aux;
	int i;

	cpu_getmcontext32(l, &uc.uc_mcontext, &uc.uc_flags);

	luc->luc_mcontext.arm_r0 = gr[_REG_R0];
	luc->luc_mcontext.arm_r1 = gr[_REG_R1];
	luc->luc_mcontext.arm_r2 = gr[_REG_R2];
	luc->luc_mcontext.arm_r3 = gr[_REG_R3];
	luc->luc_mcontext.arm_r4 = gr[_REG_R4];
	luc->luc_mcontext.arm_r5 = gr[_REG_R5];
	luc->luc_mcontext.arm_r6 = gr[_REG_R6];
	luc->luc_mcontext.arm_r7 = gr[_REG_R7];
	luc->luc_mcontext.arm_r8 = gr[_REG_R8];
	luc->luc_mcontext.arm_r9 = gr[_REG_R9];
	luc->luc_mcontext.arm_r10 = gr[_REG_R10];
	luc->luc_mcontext.arm_fp = gr[_REG_R11];
	luc->luc_mcontext.arm_ip = gr[_REG_R12];
	luc->luc_mcontext.arm_sp = gr[_REG_R13];
	luc->luc_mcontext.arm_lr = gr[_REG_R14];
	luc->luc_mcontext.arm_pc = gr[_REG_R15];
	luc->luc_mcontext.arm_cpsr = gr[_REG_CPSR];
	luc->luc_mcontext.trap_no = 0;
	luc->luc_mcontext.error_code = 0;
	luc->luc_mcontext.oldmask = 0;
	luc->luc_mcontext.fault_address = 0;

	if (uc.uc_flags & _UC_FPU) {
		aux = (struct linux32_aux_sigframe *)luc->luc_regspace;
#define LINUX32_VFP_MAGIC	0x56465001
		aux->vfp.magic = LINUX32_VFP_MAGIC;
		aux->vfp.size = sizeof(struct linux32_vfp_sigframe);

		CTASSERT(__arraycount(aux->vfp.ufp.fpregs) ==
		    __arraycount(vfpregs->__vfp_fstmx));
		for (i = 0; i < __arraycount(aux->vfp.ufp.fpregs); i++)
			aux->vfp.ufp.fpregs[i] = vfpregs->__vfp_fstmx[i];
		aux->vfp.ufp.fpscr = vfpregs->__vfp_fpscr;

		aux->vfp.ufp_exc.fpexc = VFP_FPEXC_EN | VFP_FPEXC_VECITR;
		aux->vfp.ufp_exc.fpinst = 0;
		aux->vfp.ufp_exc.fpinst2 = 0;
		aux->end_magic = 0 ;
	}
}

static int
linux32_restore_sigcontext(struct lwp *l, struct linux32_ucontext *luc)
{
	struct proc * const p = l->l_proc;
	ucontext32_t uc;
	__greg32_t *gr = uc.uc_mcontext.__gregs;
	__vfpregset32_t *vfpregs = &uc.uc_mcontext.__vfpregs;
	struct linux32_aux_sigframe *aux;
	int i, error;

	memset(&uc, 0, sizeof(uc));

	/* build .uc_sigmask */
	linux32_to_native_sigset(&uc.uc_sigmask, &luc->luc_sigmask);
	uc.uc_flags |= _UC_SIGMASK;

	/* build .uc_stack */
	if (luc->luc_stack.ss_flags & LINUX_SS_ONSTACK)
		uc.uc_stack.ss_flags |= SS_ONSTACK;
	if (luc->luc_stack.ss_flags & LINUX_SS_DISABLE)
		uc.uc_stack.ss_flags |= SS_DISABLE;
	uc.uc_flags |= _UC_STACK;

	/* build .uc_mcontext */
	gr[_REG_R0] = luc->luc_mcontext.arm_r0;
	gr[_REG_R1] = luc->luc_mcontext.arm_r1;
	gr[_REG_R2] = luc->luc_mcontext.arm_r2;
	gr[_REG_R3] = luc->luc_mcontext.arm_r3;
	gr[_REG_R4] = luc->luc_mcontext.arm_r4;
	gr[_REG_R5] = luc->luc_mcontext.arm_r5;
	gr[_REG_R6] = luc->luc_mcontext.arm_r6;
	gr[_REG_R7] = luc->luc_mcontext.arm_r7;
	gr[_REG_R8] = luc->luc_mcontext.arm_r8;
	gr[_REG_R9] = luc->luc_mcontext.arm_r9;
	gr[_REG_R10] = luc->luc_mcontext.arm_r10;
	gr[_REG_R11] = luc->luc_mcontext.arm_fp;
	gr[_REG_R12] = luc->luc_mcontext.arm_ip;
	gr[_REG_R13] = luc->luc_mcontext.arm_sp;
	gr[_REG_R14] = luc->luc_mcontext.arm_lr;
	gr[_REG_R15] = luc->luc_mcontext.arm_pc;
	gr[_REG_CPSR] = luc->luc_mcontext.arm_cpsr;
	uc.uc_flags |= _UC_CPU;

	aux = (struct linux32_aux_sigframe *)luc->luc_regspace;
	if (aux->vfp.magic == LINUX32_VFP_MAGIC &&
	    aux->vfp.size == sizeof(struct linux32_vfp_sigframe)) {

		CTASSERT(__arraycount(vfpregs->__vfp_fstmx) ==
		    __arraycount(aux->vfp.ufp.fpregs));
		for (i = 0; i < __arraycount(vfpregs->__vfp_fstmx); i++)
			vfpregs->__vfp_fstmx[i] = aux->vfp.ufp.fpregs[i];
		vfpregs->__vfp_fpscr = aux->vfp.ufp.fpscr;

		uc.uc_flags |= _UC_FPU;
	}

	mutex_enter(p->p_lock);
	error = setucontext32(l, &uc);
	mutex_exit(p->p_lock);

	return error;
}

void
linux32_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
{
	struct lwp * const l = curlwp;
	struct proc * const p = l->l_proc;
	struct trapframe * const tf = lwp_trapframe(l);
	stack_t * const ss = &l->l_sigstk;
	const int sig = ksi->ksi_signo;
	const sig_t handler = SIGACTION(p, sig).sa_handler;
	struct linux32_rt_sigframe rt_sigframe_buf, *u_rt_sigframe;
	struct linux32_sigframe *tmp_sigframe, *u_sigframe;
	struct linux32_siginfo *tmp_siginfo, *u_siginfo;
	vaddr_t sp;
	int error;
	const bool onstack_p = /* use signal stack? */
	    (ss->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
	const bool rt_p = !!(SIGACTION(curproc, ksi->ksi_signo).sa_flags &
	    SA_SIGINFO);

	sp = onstack_p ? ((vaddr_t)ss->ss_sp + ss->ss_size) & -16 :
	    tf->tf_reg[13];

	memset(&rt_sigframe_buf, 0, sizeof(rt_sigframe_buf));

	if (rt_p) {
		/* allocate rt_sigframe on stack */
		sp -= sizeof(struct linux32_rt_sigframe);
		u_rt_sigframe = (struct linux32_rt_sigframe *)sp;
		u_sigframe = &u_rt_sigframe->sig;
		u_siginfo = &u_rt_sigframe->info;
		tmp_sigframe = &rt_sigframe_buf.sig;
		tmp_siginfo = &rt_sigframe_buf.info;
	} else {
		/* allocate sigframe on stack */
		sp -= sizeof(struct linux32_sigframe);
		u_rt_sigframe = NULL;
		u_sigframe = (struct linux32_sigframe *)sp;
		u_siginfo = NULL;
		tmp_sigframe = &rt_sigframe_buf.sig;
		tmp_siginfo = NULL;
	}

	if ((vaddr_t)sp >= VM_MAXUSER_ADDRESS32) {
		sigexit(l, SIGILL);
		return;
	}

	/* build linux sigframe, and copyout to user stack */
	tmp_sigframe->uc.luc_flags = 0;
	tmp_sigframe->uc.luc_link = NULL;
	NETBSD32PTR32(tmp_sigframe->uc.luc_stack.ss_sp, ss->ss_sp);
	tmp_sigframe->uc.luc_stack.ss_size = ss->ss_size;
	tmp_sigframe->uc.luc_stack.ss_flags = 0;
	if (ss->ss_flags & SS_ONSTACK)
		tmp_sigframe->uc.luc_stack.ss_flags |= LINUX_SS_ONSTACK;
	if (ss->ss_flags & SS_DISABLE)
		tmp_sigframe->uc.luc_stack.ss_flags |= LINUX_SS_DISABLE;
	native_to_linux32_sigset(&tmp_sigframe->uc.luc_sigmask, mask);
	if (tmp_siginfo != NULL)
		native_to_linux32_siginfo(tmp_siginfo, &ksi->ksi_info);
	sendsig_reset(l, sig);

	mutex_exit(p->p_lock);
	linux32_save_sigcontext(l, &tmp_sigframe->uc);

	/* copy linux sigframe onto the user stack */
	if (rt_p) {
		error = copyout(&rt_sigframe_buf, u_rt_sigframe,
		    sizeof(rt_sigframe_buf));
	} else {
		error = copyout(tmp_sigframe, u_sigframe,
		    sizeof(*tmp_sigframe));
	}

	mutex_enter(p->p_lock);

	if (error != 0 || (vaddr_t)handler >= VM_MAXUSER_ADDRESS32) {
		sigexit(l, SIGILL);
		return;
	}

	/* build context to run handler in. */
	tf->tf_reg[0] = native_to_linux_signo[sig];
	tf->tf_reg[1] = NETBSD32PTR32I(u_siginfo);
	tf->tf_reg[2] = NETBSD32PTR32I(&u_sigframe->uc);
	tf->tf_pc = (uint64_t)handler;
	tf->tf_reg[13] = sp;

	/* sigreturn trampoline */
	extern char linux32_sigcode[], linux32_rt_sigcode[];
	vsize_t linux_sigcode_offset;
	if (rt_p) {
		/* set return address to linux32_rt_sigcode() */
		linux_sigcode_offset = linux32_rt_sigcode - linux32_sigcode;
	} else {
		/* set return address to linux32_sigcode() */
		linux_sigcode_offset = linux32_sigcode - linux32_sigcode;
	}
	/* tf->tf_reg[14] is aarch32 lr */
	tf->tf_reg[14] = NETBSD32PTR32I((char *)p->p_sigctx.ps_sigcode +
	    linux_sigcode_offset);

	if (onstack_p)
		ss->ss_flags |= SS_ONSTACK;
}

int
linux32_sys_sigreturn(struct lwp *l,
    const struct linux32_sys_sigreturn_args *uap, register_t *retval)
{
	struct trapframe * const tf = lwp_trapframe(l);
	struct linux32_sigframe sigframe;
	int error;

	error = copyin((void *)tf->tf_reg[13], &sigframe, sizeof(sigframe));
	if (error != 0)
		goto done;

	error = linux32_restore_sigcontext(l, &sigframe.uc);
	if (error != 0)
		goto done;
	error = EJUSTRETURN;

 done:
	return error;
}

int
linux32_sys_rt_sigreturn(struct lwp *l,
    const struct linux32_sys_rt_sigreturn_args *uap, register_t *retval)
{
	struct trapframe * const tf = lwp_trapframe(l);
	struct linux32_rt_sigframe rt_sigframe;
	int error;

	error = copyin((void *)tf->tf_reg[13], &rt_sigframe,
	    sizeof(rt_sigframe));
	if (error != 0)
		goto done;

	error = linux32_restore_sigcontext(l, &rt_sigframe.sig.uc);
	if (error != 0)
		goto done;
	error = EJUSTRETURN;

 done:
	return error;
}

void
linux32_setregs(struct lwp *l, struct exec_package *pack, u_long stack)
{
	struct trapframe * const tf = lwp_trapframe(l);

	netbsd32_setregs(l, pack, stack);

	/* Same as netbsd32_setregs(), but some registers are set for linux */
	tf->tf_reg[0] = 0;
	tf->tf_reg[12] = 0;
	tf->tf_reg[13] = stack;		/* sp */
	tf->tf_reg[14] = pack->ep_entry;/* lr */
	tf->tf_reg[18] = 0;
	tf->tf_pc = pack->ep_entry;
}

int
linux32_sys_set_tls(struct lwp *l, const struct linux32_sys_set_tls_args *uap,
    register_t *retval)
{
	/* {
		syscallarg(netbsd32_voidp) tls;
	} */
	return lwp_setprivate(l, SCARG_P32(uap, tls));
}

int
linux32_sys_get_tls(struct lwp *l, const void *uap, register_t *retval)
{
	retval[0] = NETBSD32PTR32I(l->l_private);
	return 0;
}