linux_machdep.c revision 1.50.24.1 1 /* $NetBSD: linux_machdep.c,v 1.50.24.1 2022/08/03 11:11:33 martin Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Eric Haszlakiewicz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Based on sys/arch/i386/i386/linux_machdep.c:
32 * linux_machdep.c,v 1.42 1998/09/11 12:50:06 mycroft Exp
33 * written by Frank van der Linden
34 *
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.50.24.1 2022/08/03 11:11:33 martin Exp $");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/signalvar.h>
43 #include <sys/kernel.h>
44 #include <sys/proc.h>
45 #include <sys/buf.h>
46 #include <sys/reboot.h>
47 #include <sys/conf.h>
48 #include <sys/exec.h>
49 #include <sys/file.h>
50 #include <sys/callout.h>
51 #include <sys/mbuf.h>
52 #include <sys/msgbuf.h>
53 #include <sys/mount.h>
54 #include <sys/vnode.h>
55 #include <sys/device.h>
56 #include <sys/syscallargs.h>
57 #include <sys/filedesc.h>
58 #include <sys/exec_elf.h>
59 #include <sys/ioctl.h>
60 #include <sys/kauth.h>
61
62 #include <uvm/uvm_extern.h>
63
64 #include <compat/linux/common/linux_types.h>
65 #include <compat/linux/common/linux_signal.h>
66 #include <compat/linux/common/linux_siginfo.h>
67 #include <compat/linux/common/linux_util.h>
68 #include <compat/linux/common/linux_ioctl.h>
69 #include <compat/linux/common/linux_exec.h>
70 #include <compat/linux/common/linux_machdep.h>
71 #include <compat/linux/common/linux_emuldata.h>
72
73 #include <compat/linux/linux_syscallargs.h>
74
75 #include <machine/alpha.h>
76 #include <machine/reg.h>
77
78 #if defined(_KERNEL_OPT)
79 #include "wsdisplay.h"
80 #endif
81 #if (NWSDISPLAY >0)
82 #include <dev/wscons/wsdisplay_usl_io.h>
83 #endif
84 #ifdef DEBUG
85 #include <machine/sigdebug.h>
86 #endif
87
88 /*
89 * Deal with some alpha-specific things in the Linux emulation code.
90 */
91
92 void
93 linux_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack)
94 {
95 #ifdef DEBUG
96 struct trapframe *tfp = l->l_md.md_tf;
97 #endif
98
99 setregs(l, epp, stack);
100 #ifdef DEBUG
101 /*
102 * Linux has registers set to zero on entry; for DEBUG kernels
103 * the alpha setregs() fills registers with 0xbabefacedeadbeef.
104 */
105 memset(tfp->tf_regs, 0, FRAME_SIZE * sizeof tfp->tf_regs[0]);
106 #endif
107 }
108
109 void
110 setup_linux_rt_sigframe(struct trapframe *tf, const ksiginfo_t *ksi,
111 const sigset_t *mask)
112 {
113 struct lwp *l = curlwp;
114 struct proc *p = l->l_proc;
115 struct linux_rt_sigframe *sfp, sigframe;
116 int onstack, error;
117 int fsize, rndfsize;
118 int sig = ksi->ksi_signo;
119 extern char linux_rt_sigcode[], linux_rt_esigcode[];
120
121 /* Do we need to jump onto the signal stack? */
122 onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
123 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
124
125 /* Allocate space for the signal handler context. */
126 fsize = sizeof(struct linux_rt_sigframe);
127 rndfsize = ((fsize + 15) / 16) * 16;
128
129 if (onstack)
130 sfp = (struct linux_rt_sigframe *)
131 ((char *)l->l_sigstk.ss_sp + l->l_sigstk.ss_size);
132 else
133 sfp = (struct linux_rt_sigframe *)(alpha_pal_rdusp());
134 sfp = (struct linux_rt_sigframe *)((char *)sfp - rndfsize);
135
136 #ifdef DEBUG
137 if ((sigdebug & SDB_KSTACK) && (p->p_pid == sigpid))
138 printf("linux_sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
139 sig, &onstack, sfp);
140 #endif /* DEBUG */
141
142 memset(&sigframe, 0, sizeof(sigframe));
143
144 /*
145 * Build the signal context to be used by sigreturn.
146 */
147 memset(&sigframe.uc, 0, sizeof(struct linux_ucontext));
148 sigframe.uc.uc_mcontext.sc_onstack = onstack;
149
150 /* Setup potentially partial signal mask in sc_mask. */
151 /* But get all of it in uc_sigmask */
152 native_to_linux_old_sigset(&sigframe.uc.uc_mcontext.sc_mask, mask);
153 native_to_linux_sigset(&sigframe.uc.uc_sigmask, mask);
154
155 sigframe.uc.uc_mcontext.sc_pc = tf->tf_regs[FRAME_PC];
156 sigframe.uc.uc_mcontext.sc_ps = ALPHA_PSL_USERMODE;
157 frametoreg(tf, (struct reg *)sigframe.uc.uc_mcontext.sc_regs);
158 sigframe.uc.uc_mcontext.sc_regs[R_SP] = alpha_pal_rdusp();
159
160 fpu_load();
161 alpha_pal_wrfen(1);
162 sigframe.uc.uc_mcontext.sc_fpcr = alpha_read_fpcr();
163 sigframe.uc.uc_mcontext.sc_fp_control = alpha_read_fp_c(l);
164 alpha_pal_wrfen(0);
165
166 sigframe.uc.uc_mcontext.sc_traparg_a0 = tf->tf_regs[FRAME_A0];
167 sigframe.uc.uc_mcontext.sc_traparg_a1 = tf->tf_regs[FRAME_A1];
168 sigframe.uc.uc_mcontext.sc_traparg_a2 = tf->tf_regs[FRAME_A2];
169 native_to_linux_siginfo(&sigframe.info, &ksi->ksi_info);
170 sendsig_reset(l, sig);
171 mutex_exit(p->p_lock);
172 error = copyout((void *)&sigframe, (void *)sfp, fsize);
173 mutex_enter(p->p_lock);
174
175 if (error != 0) {
176 #ifdef DEBUG
177 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
178 printf("sendsig(%d): copyout failed on sig %d\n",
179 p->p_pid, sig);
180 #endif
181 /*
182 * Process has trashed its stack; give it an illegal
183 * instruction to halt it in its tracks.
184 */
185 sigexit(l, SIGILL);
186 /* NOTREACHED */
187 }
188
189 /* Pass pointers to siginfo and ucontext in the regs */
190 tf->tf_regs[FRAME_A1] = (unsigned long)&sfp->info;
191 tf->tf_regs[FRAME_A2] = (unsigned long)&sfp->uc;
192
193 /* Address of trampoline code. End up at this PC after mi_switch */
194 tf->tf_regs[FRAME_PC] =
195 (u_int64_t)(p->p_psstrp - (linux_rt_esigcode - linux_rt_sigcode));
196
197 /* Adjust the stack */
198 alpha_pal_wrusp((unsigned long)sfp);
199
200 /* Remember that we're now on the signal stack. */
201 if (onstack)
202 l->l_sigstk.ss_flags |= SS_ONSTACK;
203 }
204
205 void setup_linux_sigframe(struct trapframe *tf, const ksiginfo_t *ksi,
206 const sigset_t *mask)
207 {
208 struct lwp *l = curlwp;
209 struct proc *p = l->l_proc;
210 struct linux_sigframe *sfp, sigframe;
211 int onstack, error;
212 int fsize, rndfsize;
213 int sig = ksi->ksi_signo;
214 extern char linux_sigcode[], linux_esigcode[];
215
216 /* Do we need to jump onto the signal stack? */
217 onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
218 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
219
220 /* Allocate space for the signal handler context. */
221 fsize = sizeof(struct linux_sigframe);
222 rndfsize = ((fsize + 15) / 16) * 16;
223
224 if (onstack)
225 sfp = (struct linux_sigframe *)
226 ((char *)l->l_sigstk.ss_sp + l->l_sigstk.ss_size);
227 else
228 sfp = (struct linux_sigframe *)(alpha_pal_rdusp());
229 sfp = (struct linux_sigframe *)((char *)sfp - rndfsize);
230
231 #ifdef DEBUG
232 if ((sigdebug & SDB_KSTACK) && (p->p_pid == sigpid))
233 printf("linux_sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
234 sig, &onstack, sfp);
235 #endif /* DEBUG */
236
237 memset(&sigframe, 0, sizeof(sigframe));
238
239 /*
240 * Build the signal context to be used by sigreturn.
241 */
242 memset(&sigframe.sf_sc, 0, sizeof(struct linux_sigcontext));
243 sigframe.sf_sc.sc_onstack = onstack;
244 native_to_linux_old_sigset(&sigframe.sf_sc.sc_mask, mask);
245 sigframe.sf_sc.sc_pc = tf->tf_regs[FRAME_PC];
246 sigframe.sf_sc.sc_ps = ALPHA_PSL_USERMODE;
247 frametoreg(tf, (struct reg *)sigframe.sf_sc.sc_regs);
248 sigframe.sf_sc.sc_regs[R_SP] = alpha_pal_rdusp();
249
250 if (l == fpcurlwp) {
251 struct pcb *pcb = lwp_getpcb(l);
252
253 alpha_pal_wrfen(1);
254 savefpstate(&pcb->pcb_fp);
255 alpha_pal_wrfen(0);
256 sigframe.sf_sc.sc_fpcr = pcb->pcb_fp.fpr_cr;
257 fpcurlwp = NULL;
258 }
259 /* XXX ownedfp ? etc...? */
260
261 sigframe.sf_sc.sc_traparg_a0 = tf->tf_regs[FRAME_A0];
262 sigframe.sf_sc.sc_traparg_a1 = tf->tf_regs[FRAME_A1];
263 sigframe.sf_sc.sc_traparg_a2 = tf->tf_regs[FRAME_A2];
264
265 sendsig_reset(l, sig);
266 mutex_exit(p->p_lock);
267 error = copyout((void *)&sigframe, (void *)sfp, fsize);
268 mutex_enter(p->p_lock);
269
270 if (error != 0) {
271 #ifdef DEBUG
272 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
273 printf("sendsig(%d): copyout failed on sig %d\n",
274 p->p_pid, sig);
275 #endif
276 /*
277 * Process has trashed its stack; give it an illegal
278 * instruction to halt it in its tracks.
279 */
280 sigexit(l, SIGILL);
281 /* NOTREACHED */
282 }
283
284 /* Pass pointers to sigcontext in the regs */
285 tf->tf_regs[FRAME_A1] = 0;
286 tf->tf_regs[FRAME_A2] = (unsigned long)&sfp->sf_sc;
287
288 /* Address of trampoline code. End up at this PC after mi_switch */
289 tf->tf_regs[FRAME_PC] =
290 (u_int64_t)(p->p_psstrp - (linux_esigcode - linux_sigcode));
291
292 /* Adjust the stack */
293 alpha_pal_wrusp((unsigned long)sfp);
294
295 /* Remember that we're now on the signal stack. */
296 if (onstack)
297 l->l_sigstk.ss_flags |= SS_ONSTACK;
298 }
299
300 /*
301 * Send an interrupt to process.
302 *
303 * Stack is set up to allow sigcode stored
304 * in u. to call routine, followed by kcall
305 * to sigreturn routine below. After sigreturn
306 * resets the signal mask, the stack, and the
307 * frame pointer, it returns to the user
308 * specified pc, psl.
309 */
310 void
311 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
312 {
313 struct lwp *l = curlwp;
314 struct proc *p = l->l_proc;
315 struct trapframe *tf = l->l_md.md_tf;
316 const int sig = ksi->ksi_signo;
317 sig_t catcher = SIGACTION(p, sig).sa_handler;
318 #ifdef notyet
319 struct linux_emuldata *edp;
320
321 /* Setup the signal frame (and part of the trapframe) */
322 /*OLD: if (p->p_sigacts->ps_siginfo & sigmask(sig))*/
323 /* XXX XAX this is broken now. need someplace to store what
324 XXX XAX kind of signal handler a signal has.*/
325 #if 0
326 edp = (struct linux_emuldata *)p->p_emuldata;
327 #else
328 edp = 0;
329 #endif
330 if (edp && sigismember(&edp->ps_siginfo, sig))
331 setup_linux_rt_sigframe(tf, ksi, mask);
332 else
333 #endif /* notyet */
334 setup_linux_sigframe(tf, ksi, mask);
335
336 /* Signal handler for trampoline code */
337 tf->tf_regs[FRAME_T12] = (u_int64_t)catcher;
338 tf->tf_regs[FRAME_A0] = native_to_linux_signo[sig];
339
340 /*
341 * Linux has a custom restorer option. To support it we would
342 * need to store an array of restorers and a sigcode block
343 * which knew to use it. Doesn't seem worth the trouble.
344 * -erh
345 */
346
347 #ifdef DEBUG
348 if (sigdebug & SDB_FOLLOW)
349 printf("sendsig(%d): pc %lx, catcher %lx\n", l->l_proc->p_pid,
350 tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]);
351 if ((sigdebug & SDB_KSTACK) && l->l_proc->p_pid == sigpid)
352 printf("sendsig(%d): sig %d returns\n", l->l_proc->p_pid, sig);
353 #endif
354 }
355
356 /*
357 * System call to cleanup state after a signal
358 * has been taken. Reset signal mask and
359 * stack state from context left by sendsig (above).
360 * Return to previous pc as specified by context
361 * left by sendsig.
362 * Linux real-time signals use a different sigframe,
363 * but the sigcontext is the same.
364 */
365
366 int
367 linux_restore_sigcontext(struct lwp *l, struct linux_sigcontext context,
368 sigset_t *mask)
369 {
370 struct proc *p = l->l_proc;
371 struct pcb *pcb;
372
373 /*
374 * Linux doesn't (yet) have alternate signal stacks.
375 * However, the OSF/1 sigcontext which they use has
376 * an onstack member. This could be needed in the future.
377 */
378 mutex_enter(p->p_lock);
379 if (context.sc_onstack & LINUX_SA_ONSTACK)
380 l->l_sigstk.ss_flags |= SS_ONSTACK;
381 else
382 l->l_sigstk.ss_flags &= ~SS_ONSTACK;
383
384 /* Reset the signal mask */
385 (void) sigprocmask1(l, SIG_SETMASK, mask, 0);
386 mutex_exit(p->p_lock);
387
388 /*
389 * Check for security violations.
390 * Linux doesn't allow any changes to the PSL.
391 */
392 if (context.sc_ps != ALPHA_PSL_USERMODE)
393 return(EINVAL);
394
395 l->l_md.md_tf->tf_regs[FRAME_PC] = context.sc_pc;
396 l->l_md.md_tf->tf_regs[FRAME_PS] = context.sc_ps;
397
398 regtoframe((struct reg *)context.sc_regs, l->l_md.md_tf);
399 alpha_pal_wrusp(context.sc_regs[R_SP]);
400
401 if (l == fpcurlwp)
402 fpcurlwp = NULL;
403
404 /* Restore fp regs and fpr_cr */
405 pcb = lwp_getpcb(l);
406 memcpy(&pcb->pcb_fp, (struct fpreg *)context.sc_fpregs,
407 sizeof(struct fpreg));
408 /* XXX sc_ownedfp ? */
409 /* XXX sc_fp_control ? */
410
411 #ifdef DEBUG
412 if (sigdebug & SDB_FOLLOW)
413 printf("linux_rt_sigreturn(%d): returns\n", p->p_pid);
414 #endif
415 return (EJUSTRETURN);
416 }
417
418 int
419 linux_sys_rt_sigreturn(struct lwp *l, const struct linux_sys_rt_sigreturn_args *uap, register_t *retval)
420 {
421 /* {
422 syscallarg(struct linux_rt_sigframe *) sfp;
423 } */
424 struct linux_rt_sigframe *sfp, sigframe;
425 sigset_t mask;
426
427 /*
428 * The trampoline code hands us the context.
429 * It is unsafe to keep track of it ourselves, in the event that a
430 * program jumps out of a signal handler.
431 */
432
433 sfp = SCARG(uap, sfp);
434
435 if (ALIGN(sfp) != (u_int64_t)sfp)
436 return(EINVAL);
437
438 /*
439 * Fetch the frame structure.
440 */
441 if (copyin((void *)sfp, &sigframe,
442 sizeof(struct linux_rt_sigframe)) != 0)
443 return (EFAULT);
444
445 /* Grab the signal mask */
446 linux_to_native_sigset(&mask, &sigframe.uc.uc_sigmask);
447
448 return(linux_restore_sigcontext(l, sigframe.uc.uc_mcontext, &mask));
449 }
450
451
452 int
453 linux_sys_sigreturn(struct lwp *l, const struct linux_sys_sigreturn_args *uap, register_t *retval)
454 {
455 /* {
456 syscallarg(struct linux_sigframe *) sfp;
457 } */
458 struct linux_sigframe *sfp, frame;
459 sigset_t mask;
460
461 /*
462 * The trampoline code hands us the context.
463 * It is unsafe to keep track of it ourselves, in the event that a
464 * program jumps out of a signal handler.
465 */
466
467 sfp = SCARG(uap, sfp);
468 if (ALIGN(sfp) != (u_int64_t)sfp)
469 return(EINVAL);
470
471 /*
472 * Fetch the frame structure.
473 */
474 if (copyin((void *)sfp, &frame, sizeof(struct linux_sigframe)) != 0)
475 return(EFAULT);
476
477 /* Grab the signal mask. */
478 /* XXX use frame.extramask */
479 linux_old_to_native_sigset(&mask, frame.sf_sc.sc_mask);
480
481 return(linux_restore_sigcontext(l, frame.sf_sc, &mask));
482 }
483
484 /*
485 * We come here in a last attempt to satisfy a Linux ioctl() call
486 */
487 /* XXX XAX update this, add maps, etc... */
488 int
489 linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
490 {
491 /* {
492 syscallarg(int) fd;
493 syscallarg(u_long) com;
494 syscallarg(void *) data;
495 } */
496 struct sys_ioctl_args bia;
497 u_long com;
498
499 SCARG(&bia, fd) = SCARG(uap, fd);
500 SCARG(&bia, data) = SCARG(uap, data);
501 com = SCARG(uap, com);
502
503 switch (com) {
504 default:
505 printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
506 return EINVAL;
507 }
508 SCARG(&bia, com) = com;
509 return sys_ioctl(l, &bia, retval);
510 }
511
512 /* XXX XAX fix this */
513 dev_t
514 linux_fakedev(dev_t dev, int raw)
515 {
516 return dev;
517 }
518
519 int
520 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
521 {
522 return 0;
523 }
524