xref: /netbsd-src/sys/compat/linux/arch/amd64/linux_machdep.c (revision 21709ad01db1a91e3c201ed96d7d83992cdf0208)
1 /*	$NetBSD: linux_machdep.c,v 1.62 2024/10/01 16:35:42 riastradh Exp $ */
2 
3 /*-
4  * Copyright (c) 2005 Emmanuel Dreyfus, all rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Emmanuel Dreyfus
17  * 4. The name of the author may not be used to endorse or promote
18  *    products derived from this software without specific prior written
19  *    permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 
36 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.62 2024/10/01 16:35:42 riastradh Exp $");
37 
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/systm.h>
41 #include <sys/signal.h>
42 #include <sys/exec.h>
43 #include <sys/proc.h>
44 #include <sys/ptrace.h> /* for process_read_fpregs() */
45 #include <sys/ucontext.h>
46 #include <sys/conf.h>
47 
48 #include <machine/reg.h>
49 #include <machine/pcb.h>
50 #include <machine/mcontext.h>
51 #include <machine/specialreg.h>
52 #include <machine/vmparam.h>
53 #include <machine/cpufunc.h>
54 #include <x86/include/sysarch.h>
55 
56 /*
57  * To see whether wscons is configured (for virtual console ioctl calls).
58  */
59 #if defined(_KERNEL_OPT)
60 #include "opt_user_ldt.h"
61 #include "wsdisplay.h"
62 #endif
63 
64 #if (NWSDISPLAY > 0)
65 #include <dev/wscons/wsconsio.h>
66 #include <dev/wscons/wsdisplay_usl_io.h>
67 #endif
68 
69 
70 #include <compat/linux/common/linux_signal.h>
71 #include <compat/linux/common/linux_errno.h>
72 #include <compat/linux/common/linux_exec.h>
73 #include <compat/linux/common/linux_ioctl.h>
74 #include <compat/linux/common/linux_prctl.h>
75 #include <compat/linux/common/linux_machdep.h>
76 #include <compat/linux/common/linux_ipc.h>
77 #include <compat/linux/common/linux_sem.h>
78 #include <compat/linux/linux_syscall.h>
79 #include <compat/linux/linux_syscallargs.h>
80 
81 static void linux_buildcontext(struct lwp *, void *, void *);
82 
83 void
84 linux_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack)
85 {
86 	struct pcb *pcb = lwp_getpcb(l);
87 	struct trapframe *tf;
88 
89 #ifdef USER_LDT
90 	pmap_ldt_cleanup(l);
91 #endif
92 
93 	fpu_clear(l, __NetBSD_NPXCW__);
94 
95 	kpreempt_disable();
96 	pcb->pcb_flags = 0;
97 	l->l_proc->p_flag &= ~PK_32;
98 	l->l_md.md_flags = MDL_IRET;
99 	cpu_segregs64_zero(l);
100 	kpreempt_enable();
101 
102 	tf = l->l_md.md_regs;
103 	tf->tf_rax = 0;
104 	tf->tf_rbx = 0;
105 	tf->tf_rcx = epp->ep_entry;
106 	tf->tf_rdx = 0;
107 	tf->tf_rsi = 0;
108 	tf->tf_rdi = 0;
109 	tf->tf_rbp = 0;
110 	tf->tf_rsp = stack;
111 	tf->tf_r8 = 0;
112 	tf->tf_r9 = 0;
113 	tf->tf_r10 = 0;
114 	tf->tf_r11 = 0;
115 	tf->tf_r12 = 0;
116 	tf->tf_r13 = 0;
117 	tf->tf_r14 = 0;
118 	tf->tf_r15 = 0;
119 	tf->tf_rip = epp->ep_entry;
120 	tf->tf_rflags = PSL_USERSET;
121 	tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
122 	tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
123 	tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
124 	tf->tf_es = 0;
125 
126 	return;
127 }
128 
129 void
130 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
131 {
132 	struct lwp *l = curlwp;
133 	struct proc *p = l->l_proc;
134 	struct pcb *pcb = lwp_getpcb(l);
135 	struct sigacts *ps = p->p_sigacts;
136 	int onstack, error;
137 	int sig = ksi->ksi_signo;
138 	struct linux_rt_sigframe *sfp, sigframe;
139 	struct linux__fpstate *fpsp;
140 	struct fpreg fpregs;
141 	struct trapframe *tf = l->l_md.md_regs;
142 	sig_t catcher = SIGACTION(p, sig).sa_handler;
143 	linux_sigset_t lmask;
144 	char *sp;
145 
146 	/* Do we need to jump onto the signal stack? */
147 	onstack =
148 	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
149 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
150 
151 	/* Allocate space for the signal handler context. */
152 	if (onstack)
153 		sp = ((char *)l->l_sigstk.ss_sp +
154 		    l->l_sigstk.ss_size);
155 	else
156 		sp = (char *)tf->tf_rsp - 128;
157 
158 	/* Save FPU state */
159 	sp = (char *) (((long)sp - sizeof (*fpsp)) & ~0xfUL);
160 	fpsp = (struct linux__fpstate *)sp;
161 
162 	/*
163 	 * Populate the rt_sigframe
164 	 */
165 	sp = (char *)
166 	    ((((long)sp - sizeof(struct linux_rt_sigframe)) & ~0xfUL) - 8);
167 	sfp = (struct linux_rt_sigframe *)sp;
168 
169 	memset(&sigframe, 0, sizeof(sigframe));
170 	if (ps->sa_sigdesc[sig].sd_vers != __SIGTRAMP_SIGCODE_VERSION)
171 		sigframe.pretcode =
172 		    (char *)(u_long)ps->sa_sigdesc[sig].sd_tramp;
173 	else
174 		sigframe.pretcode = NULL;
175 
176 	/*
177 	 * The user context
178 	 */
179 	sigframe.uc.luc_flags = 0;
180 	sigframe.uc.luc_link = NULL;
181 
182 	/* This is used regardless of SA_ONSTACK in Linux */
183 	sigframe.uc.luc_stack.ss_sp = l->l_sigstk.ss_sp;
184 	sigframe.uc.luc_stack.ss_size = l->l_sigstk.ss_size;
185 	sigframe.uc.luc_stack.ss_flags = 0;
186 	if (l->l_sigstk.ss_flags & SS_ONSTACK)
187 		sigframe.uc.luc_stack.ss_flags |= LINUX_SS_ONSTACK;
188 	if (l->l_sigstk.ss_flags & SS_DISABLE)
189 		sigframe.uc.luc_stack.ss_flags |= LINUX_SS_DISABLE;
190 
191 	sigframe.uc.luc_mcontext.r8 = tf->tf_r8;
192 	sigframe.uc.luc_mcontext.r9 = tf->tf_r9;
193 	sigframe.uc.luc_mcontext.r10 = tf->tf_r10;
194 	sigframe.uc.luc_mcontext.r11 = tf->tf_r11;
195 	sigframe.uc.luc_mcontext.r12 = tf->tf_r12;
196 	sigframe.uc.luc_mcontext.r13 = tf->tf_r13;
197 	sigframe.uc.luc_mcontext.r14 = tf->tf_r14;
198 	sigframe.uc.luc_mcontext.r15 = tf->tf_r15;
199 	sigframe.uc.luc_mcontext.rdi = tf->tf_rdi;
200 	sigframe.uc.luc_mcontext.rsi = tf->tf_rsi;
201 	sigframe.uc.luc_mcontext.rbp = tf->tf_rbp;
202 	sigframe.uc.luc_mcontext.rbx = tf->tf_rbx;
203 	sigframe.uc.luc_mcontext.rdx = tf->tf_rdx;
204 	sigframe.uc.luc_mcontext.rax = tf->tf_rax;
205 	sigframe.uc.luc_mcontext.rcx = tf->tf_rcx;
206 	sigframe.uc.luc_mcontext.rsp = tf->tf_rsp;
207 	sigframe.uc.luc_mcontext.rip = tf->tf_rip;
208 	sigframe.uc.luc_mcontext.eflags = tf->tf_rflags;
209 	sigframe.uc.luc_mcontext.cs = GSEL(GUCODE_SEL, SEL_UPL);
210 	sigframe.uc.luc_mcontext.gs = tf->tf_gs & 0xFFFF;
211 	sigframe.uc.luc_mcontext.fs = tf->tf_fs & 0xFFFF;
212 	sigframe.uc.luc_mcontext.err = tf->tf_err;
213 	sigframe.uc.luc_mcontext.trapno = tf->tf_trapno;
214 	native_to_linux_sigset(&lmask, mask);
215 	sigframe.uc.luc_mcontext.oldmask = lmask.sig[0];
216 	sigframe.uc.luc_mcontext.cr2 = (long)pcb->pcb_onfault;
217 	sigframe.uc.luc_mcontext.fpstate = fpsp;
218 	native_to_linux_sigset(&sigframe.uc.luc_sigmask, mask);
219 	native_to_linux_siginfo(&sigframe.info, &ksi->ksi_info);
220 	sendsig_reset(l, sig);
221 	mutex_exit(p->p_lock);
222 	error = 0;
223 
224 	/*
225 	 * Save FPU state, if any
226 	 */
227 	if (fpsp != NULL) {
228 		size_t fp_size = sizeof fpregs;
229 		/* The netbsd and linux structures both match the fxsave data */
230 		memset(&fpregs, 0, sizeof(fpregs));
231 		(void)process_read_fpregs(l, &fpregs, &fp_size);
232 		error = copyout(&fpregs, fpsp, sizeof(*fpsp));
233 	}
234 
235 	if (error == 0)
236 		error = copyout(&sigframe, sp, sizeof(sigframe));
237 
238 	mutex_enter(p->p_lock);
239 
240 	if (error != 0) {
241 		sigexit(l, SIGILL);
242 		return;
243 	}
244 
245 	if ((vaddr_t)catcher >= VM_MAXUSER_ADDRESS) {
246 		sigexit(l, SIGILL);
247 		return;
248 	}
249 
250 	linux_buildcontext(l, catcher, sp);
251 	tf->tf_rdi = sigframe.info.lsi_signo;
252 	tf->tf_rax = 0;
253 	tf->tf_rsi = (long)&sfp->info;
254 	tf->tf_rdx = (long)&sfp->uc;
255 
256 	/*
257 	 * Remember we use signal stack
258 	 */
259 	if (onstack)
260 		l->l_sigstk.ss_flags |= SS_ONSTACK;
261 	return;
262 }
263 
264 int
265 linux_sys_modify_ldt(struct lwp *l, const struct linux_sys_modify_ldt_args *v, register_t *retval)
266 {
267 	return 0;
268 }
269 
270 int
271 linux_sys_iopl(struct lwp *l, const struct linux_sys_iopl_args *v, register_t *retval)
272 {
273 	return 0;
274 }
275 
276 int
277 linux_sys_ioperm(struct lwp *l, const struct linux_sys_ioperm_args *v, register_t *retval)
278 {
279 	return 0;
280 }
281 
282 dev_t
283 linux_fakedev(dev_t dev, int raw)
284 {
285 
286        extern const struct cdevsw ptc_cdevsw, pts_cdevsw;
287        const struct cdevsw *cd = cdevsw_lookup(dev);
288 
289        if (raw) {
290 #if (NWSDISPLAY > 0)
291 	       extern const struct cdevsw wsdisplay_cdevsw;
292 	       if (cd == &wsdisplay_cdevsw)
293 		       return makedev(LINUX_CONS_MAJOR, (minor(dev) + 1));
294 #endif
295        }
296 
297        if (cd == &ptc_cdevsw)
298 	       return makedev(LINUX_PTC_MAJOR, minor(dev));
299        if (cd == &pts_cdevsw)
300 	       return makedev(LINUX_PTS_MAJOR, minor(dev));
301 
302 	return ((minor(dev) & 0xff) | ((major(dev) & 0xfff) << 8)
303 	    | (((unsigned long long int) (minor(dev) & ~0xff)) << 12)
304 	    | (((unsigned long long int) (major(dev) & ~0xfff)) << 32));
305 }
306 
307 int
308 linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *v, register_t *retval)
309 {
310 	return 0;
311 }
312 
313 int
314 linux_sys_rt_sigreturn(struct lwp *l, const void *v, register_t *retval)
315 {
316 	struct linux_ucontext *luctx;
317 	struct trapframe *tf = l->l_md.md_regs;
318 	struct linux_sigcontext *lsigctx;
319 	struct linux_rt_sigframe frame, *fp;
320 	ucontext_t uctx;
321 	mcontext_t *mctx;
322 	struct fxsave *fxarea;
323 	int error;
324 
325 	fp = (struct linux_rt_sigframe *)(tf->tf_rsp - 8);
326 	if ((error = copyin(fp, &frame, sizeof(frame))) != 0) {
327 		mutex_enter(l->l_proc->p_lock);
328 		sigexit(l, SIGILL);
329 		return error;
330 	}
331 	luctx = &frame.uc;
332 	lsigctx = &luctx->luc_mcontext;
333 
334 	memset(&uctx, 0, sizeof(uctx));
335 	mctx = (mcontext_t *)&uctx.uc_mcontext;
336 	fxarea = (struct fxsave *)&mctx->__fpregs;
337 
338 	/*
339 	 * Set the flags. Linux always have CPU, stack and signal state,
340 	 * FPU is optional. uc_flags is not used to tell what we have.
341 	 */
342 	uctx.uc_flags = (_UC_SIGMASK|_UC_CPU|_UC_STACK|_UC_CLRSTACK);
343 	if (lsigctx->fpstate != NULL)
344 		uctx.uc_flags |= _UC_FPU;
345 	uctx.uc_link = NULL;
346 
347 	/*
348 	 * Signal set
349 	 */
350 	linux_to_native_sigset(&uctx.uc_sigmask, &luctx->luc_sigmask);
351 
352 	/*
353 	 * CPU state
354 	 */
355 	mctx->__gregs[_REG_R8] = lsigctx->r8;
356 	mctx->__gregs[_REG_R9] = lsigctx->r9;
357 	mctx->__gregs[_REG_R10] = lsigctx->r10;
358 	mctx->__gregs[_REG_R11] = lsigctx->r11;
359 	mctx->__gregs[_REG_R12] = lsigctx->r12;
360 	mctx->__gregs[_REG_R13] = lsigctx->r13;
361 	mctx->__gregs[_REG_R14] = lsigctx->r14;
362 	mctx->__gregs[_REG_R15] = lsigctx->r15;
363 	mctx->__gregs[_REG_RDI] = lsigctx->rdi;
364 	mctx->__gregs[_REG_RSI] = lsigctx->rsi;
365 	mctx->__gregs[_REG_RBP] = lsigctx->rbp;
366 	mctx->__gregs[_REG_RBX] = lsigctx->rbx;
367 	mctx->__gregs[_REG_RAX] = lsigctx->rax;
368 	mctx->__gregs[_REG_RDX] = lsigctx->rdx;
369 	mctx->__gregs[_REG_RCX] = lsigctx->rcx;
370 	mctx->__gregs[_REG_RIP] = lsigctx->rip;
371 	mctx->__gregs[_REG_RFLAGS] = lsigctx->eflags;
372 	mctx->__gregs[_REG_CS] = lsigctx->cs & 0xFFFF;
373 	mctx->__gregs[_REG_GS] = lsigctx->gs & 0xFFFF;
374 	mctx->__gregs[_REG_FS] = lsigctx->fs & 0xFFFF;
375 	mctx->__gregs[_REG_ERR] = lsigctx->err;
376 	mctx->__gregs[_REG_TRAPNO] = lsigctx->trapno;
377 	mctx->__gregs[_REG_ES] = tf->tf_es & 0xFFFF;
378 	mctx->__gregs[_REG_DS] = tf->tf_ds & 0xFFFF;
379 	mctx->__gregs[_REG_RSP] = lsigctx->rsp; /* XXX */
380 	mctx->__gregs[_REG_SS] = tf->tf_ss & 0xFFFF;
381 
382 	/*
383 	 * FPU state
384 	 */
385 	if (lsigctx->fpstate != NULL) {
386 		/* Both structures match the fxstate data */
387 		error = copyin(lsigctx->fpstate, fxarea, sizeof(*fxarea));
388 		if (error != 0) {
389 			mutex_enter(l->l_proc->p_lock);
390 			sigexit(l, SIGILL);
391 			return error;
392 		}
393 	}
394 
395 	/*
396 	 * And the stack
397 	 */
398 	uctx.uc_stack.ss_flags = 0;
399 	if (luctx->luc_stack.ss_flags & LINUX_SS_ONSTACK)
400 		uctx.uc_stack.ss_flags |= SS_ONSTACK;
401 
402 	if (luctx->luc_stack.ss_flags & LINUX_SS_DISABLE)
403 		uctx.uc_stack.ss_flags |= SS_DISABLE;
404 
405 	uctx.uc_stack.ss_sp = luctx->luc_stack.ss_sp;
406 	uctx.uc_stack.ss_size = luctx->luc_stack.ss_size;
407 
408 	/*
409 	 * And let setucontext deal with that.
410 	 */
411 	mutex_enter(l->l_proc->p_lock);
412 	error = setucontext(l, &uctx);
413 	mutex_exit(l->l_proc->p_lock);
414 	if (error)
415 		return error;
416 
417 	return EJUSTRETURN;
418 }
419 
420 int
421 linux_sys_arch_prctl(struct lwp *l,
422     const struct linux_sys_arch_prctl_args *uap, register_t *retval)
423 {
424 	/* {
425 		syscallarg(int) code;
426 		syscallarg(unsigned long) addr;
427 	} */
428 	void *addr = (void *)SCARG(uap, addr);
429 
430 	switch(SCARG(uap, code)) {
431 	case LINUX_ARCH_SET_GS:
432 		return x86_set_sdbase(addr, 'g', l, true);
433 
434 	case LINUX_ARCH_GET_GS:
435 		return x86_get_sdbase(addr, 'g');
436 
437 	case LINUX_ARCH_SET_FS:
438 		return x86_set_sdbase(addr, 'f', l, true);
439 
440 	case LINUX_ARCH_GET_FS:
441 		return x86_get_sdbase(addr, 'f');
442 
443 	default:
444 #ifdef DEBUG_LINUX
445 		printf("linux_sys_arch_prctl: unexpected code %d\n",
446 		    SCARG(uap, code));
447 #endif
448 		return EINVAL;
449 	}
450 	/* NOTREACHED */
451 }
452 
453 const int linux_vsyscall_to_syscall[] = {
454 	LINUX_SYS_gettimeofday,
455 	LINUX_SYS_time,
456 	LINUX_SYS_nosys,	/* nosys */
457 	LINUX_SYS_nosys,	/* nosys */
458 };
459 
460 int
461 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
462 {
463 	struct trapframe *tf = arg;
464 	uint64_t retaddr;
465 	size_t vsyscallnr;
466 
467 	/*
468 	 * Check for a vsyscall. %rip must be the fault address,
469 	 * and the address must be in the Linux vsyscall area.
470 	 * Also, vsyscalls are only done at 1024-byte boundaries.
471 	 */
472 
473 	if (__predict_true(trapaddr < LINUX_VSYSCALL_START))
474 		return 0;
475 
476 	if (trapaddr != tf->tf_rip)
477 		return 0;
478 
479 	if ((tf->tf_rip & (LINUX_VSYSCALL_SIZE - 1)) != 0)
480 		return 0;
481 
482 	vsyscallnr = (tf->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SIZE;
483 
484 	if (vsyscallnr > LINUX_VSYSCALL_MAXNR)
485 		return 0;
486 
487 	/*
488 	 * Get the return address from the top of the stack,
489 	 * and fix up the return address.
490 	 * This assumes the faulting instruction was callq *reg,
491 	 * which is the only way that vsyscalls are ever entered.
492 	 */
493 	if (copyin((void *)tf->tf_rsp, &retaddr, sizeof retaddr) != 0)
494 		return 0;
495 	if ((vaddr_t)retaddr >= VM_MAXUSER_ADDRESS)
496 		return 0;
497 	tf->tf_rip = retaddr;
498 	tf->tf_rax = linux_vsyscall_to_syscall[vsyscallnr];
499 	tf->tf_rsp += 8;	/* "pop" the return address */
500 
501 #if 0
502 	printf("usertrap: rip %p rsp %p retaddr %p vsys %d sys %d\n",
503 	    (void *)tf->tf_rip, (void *)tf->tf_rsp, (void *)retaddr,
504 	    vsyscallnr, (int)tf->tf_rax);
505 #endif
506 
507 	(*l->l_proc->p_md.md_syscall)(tf);
508 
509 	return 1;
510 }
511 
512 static void
513 linux_buildcontext(struct lwp *l, void *catcher, void *f)
514 {
515 	struct trapframe *tf = l->l_md.md_regs;
516 
517 	tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
518 	tf->tf_rip = (u_int64_t)catcher;
519 	tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
520 	tf->tf_rflags &= ~PSL_CLEARSIG;
521 	tf->tf_rsp = (u_int64_t)f;
522 	tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
523 }
524