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