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
|
/* $NetBSD: linux_machdep.c,v 1.3 2021/11/01 05:07:16 thorpej 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: linux_machdep.c,v 1.3 2021/11/01 05:07:16 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/exec.h>
#include <sys/syscallargs.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_exec.h>
#include <compat/linux/common/linux_ioctl.h>
#include <compat/linux/common/linux_prctl.h>
#include <compat/linux/common/linux_machdep.h>
#include <compat/linux/common/linux_ipc.h>
#include <compat/linux/common/linux_sem.h>
#include <compat/linux/linux_syscall.h>
#include <compat/linux/linux_syscallargs.h>
void
linux_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack)
{
setregs(l, epp, stack);
}
static void
linux_save_sigcontext(struct lwp *l, struct linux_sigcontext *ctx)
{
ucontext_t uc;
cpu_getmcontext(l, &uc.uc_mcontext, &uc.uc_flags);
memset(ctx, 0, sizeof(*ctx));
/* ctx->fault_address = 0; */
CTASSERT(sizeof(ctx->regs) <= sizeof(uc.uc_mcontext.__gregs));
for (int i = 0; i < __arraycount(ctx->regs); i++)
ctx->regs[i] = uc.uc_mcontext.__gregs[i];
ctx->sp = uc.uc_mcontext.__gregs[_REG_SP];
ctx->pc = uc.uc_mcontext.__gregs[_REG_PC];
ctx->pstate = uc.uc_mcontext.__gregs[_REG_SPSR];
if (uc.uc_flags & _UC_FPU) {
struct fpsimd_context *fpsimd;
fpsimd = (struct fpsimd_context *)ctx->__reserved;
fpsimd->head.magic = FPSIMD_MAGIC;
fpsimd->head.size = sizeof(struct fpsimd_context);
fpsimd->fpsr = uc.uc_mcontext.__fregs.__fpsr;
fpsimd->fpcr = uc.uc_mcontext.__fregs.__fpcr;
CTASSERT(sizeof(fpsimd->vregs) ==
sizeof(uc.uc_mcontext.__fregs.__qregs));
memcpy(fpsimd->vregs, uc.uc_mcontext.__fregs.__qregs,
sizeof(uc.uc_mcontext.__fregs.__qregs));
}
/* ctx->__reserved[] has already been terminated by memset() */
}
static void
aarch64_linux_to_native_ucontext(ucontext_t *uc, struct linux_ucontext *luc)
{
struct linux_sigcontext *ctx = &luc->luc_mcontext;
struct fpsimd_context *fpsimd;
memset(uc, 0, sizeof(*uc));
/* build .uc_flags, .uc_link, and .uc_sigmask */
uc->uc_flags = (_UC_SIGMASK | _UC_CPU | _UC_STACK | _UC_CLRSTACK);
uc->uc_link = NULL;
linux_to_native_sigset(&uc->uc_sigmask, &luc->luc_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_stack.ss_sp = luc->luc_stack.ss_sp;
uc->uc_stack.ss_size = luc->luc_stack.ss_size;
/* build .uc_mcontext */
CTASSERT(sizeof(ctx->regs) <= sizeof(uc->uc_mcontext.__gregs));
for (int i = 0; i < __arraycount(ctx->regs); i++)
uc->uc_mcontext.__gregs[i] = ctx->regs[i];
uc->uc_mcontext.__gregs[_REG_SP] = ctx->sp;
uc->uc_mcontext.__gregs[_REG_PC] = ctx->pc;
uc->uc_mcontext.__gregs[_REG_SPSR] = ctx->pstate;
fpsimd = (struct fpsimd_context *)ctx->__reserved;
if (fpsimd->head.magic == FPSIMD_MAGIC) {
uc->uc_flags |= _UC_FPU;
uc->uc_mcontext.__fregs.__fpsr = fpsimd->fpsr;
uc->uc_mcontext.__fregs.__fpcr = fpsimd->fpcr;
CTASSERT(sizeof(fpsimd->vregs) ==
sizeof(uc->uc_mcontext.__fregs.__qregs));
memcpy(uc->uc_mcontext.__fregs.__qregs, fpsimd->vregs,
sizeof(uc->uc_mcontext.__fregs.__qregs));
}
}
void
linux_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 linux_rt_sigframe *u_sigframe, *tmp_sigframe;
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;
sp = onstack_p ? ((vaddr_t)ss->ss_sp + ss->ss_size) & -16 : tf->tf_sp;
/* allocate sigframe on userstack */
sp -= sizeof(struct linux_rt_sigframe);
u_sigframe = (struct linux_rt_sigframe *)sp;
/* build linux sigframe, and copyout to user stack */
tmp_sigframe = kmem_zalloc(sizeof(*tmp_sigframe), KM_SLEEP);
tmp_sigframe->uc.luc_flags = 0;
tmp_sigframe->uc.luc_link = NULL;
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_linux_sigset(&tmp_sigframe->uc.luc_sigmask, mask);
native_to_linux_siginfo(&tmp_sigframe->info, &ksi->ksi_info);
sendsig_reset(l, sig);
mutex_exit(p->p_lock);
linux_save_sigcontext(l, &tmp_sigframe->uc.luc_mcontext);
/* copy linux sigframe onto the user stack */
error = copyout(tmp_sigframe, u_sigframe, sizeof(*tmp_sigframe));
kmem_free(tmp_sigframe, sizeof(*tmp_sigframe));
mutex_enter(p->p_lock);
if (error != 0 || (vaddr_t)handler >= VM_MAXUSER_ADDRESS) {
sigexit(l, SIGILL);
return;
}
/* build context to run handler in. */
tf->tf_reg[0] = native_to_linux_signo[sig];
tf->tf_reg[1] = (uint64_t)&u_sigframe->info;
tf->tf_reg[2] = (uint64_t)&u_sigframe->uc;
tf->tf_pc = (uint64_t)handler;
tf->tf_sp = sp;
/* sigreturn trampoline */
extern char linux_sigcode[], linux_rt_sigcode[];
vsize_t linux_rt_sigcode_offset = linux_rt_sigcode - linux_sigcode;
tf->tf_lr = (uint64_t)p->p_sigctx.ps_sigcode + linux_rt_sigcode_offset;
if (onstack_p)
ss->ss_flags |= SS_ONSTACK;
}
int
linux_sys_rt_sigreturn(struct lwp *l, const void *v, register_t *retval)
{
struct trapframe * const tf = lwp_trapframe(l);
struct proc * const p = l->l_proc;
struct linux_rt_sigframe *lsigframe;
ucontext_t uc;
int error;
/*
* struct linux_rt_sigframe is variable size,
* but netbsd/aarch64's linux_sendsig() always pushes a full size
* linux_rt_sigframe.
*/
lsigframe = kmem_zalloc(sizeof(*lsigframe), KM_SLEEP);
error = copyin((void *)tf->tf_sp, lsigframe, sizeof(*lsigframe));
if (error != 0)
goto done;
aarch64_linux_to_native_ucontext(&uc, &lsigframe->uc);
mutex_enter(p->p_lock);
error = setucontext(l, &uc);
mutex_exit(p->p_lock);
if (error != 0)
goto done;
error = EJUSTRETURN;
done:
kmem_free(lsigframe, sizeof(*lsigframe));
return error;
}
void
linux_trapsignal(struct lwp *l, ksiginfo_t *ksi)
{
/*
* XXX: TODO
* should be convert siginfo from native to linux
*/
trapsignal(l, ksi);
}
int
linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
{
/* not used */
return 0;
}
dev_t
linux_fakedev(dev_t dev, int raw)
{
/* TODO if needed */
return dev;
}
int
linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap,
register_t *retval)
{
/* TODO if needed */
return EINVAL;
}
|