xref: /netbsd-src/sys/compat/linux32/arch/amd64/linux32_machdep.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: linux32_machdep.c,v 1.20 2008/10/19 09:44:31 njoly Exp $ */
2 
3 /*-
4  * Copyright (c) 2006 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 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: linux32_machdep.c,v 1.20 2008/10/19 09:44:31 njoly Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/signalvar.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/user.h>
42 #include <sys/buf.h>
43 #include <sys/reboot.h>
44 #include <sys/conf.h>
45 #include <sys/exec.h>
46 #include <sys/file.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/msgbuf.h>
51 #include <sys/mount.h>
52 #include <sys/vnode.h>
53 #include <sys/device.h>
54 #include <sys/syscallargs.h>
55 #include <sys/filedesc.h>
56 #include <sys/exec_elf.h>
57 #include <sys/disklabel.h>
58 #include <sys/ioctl.h>
59 #include <sys/wait.h>
60 #include <miscfs/specfs/specdev.h>
61 
62 #include <machine/netbsd32_machdep.h>
63 
64 #include <compat/netbsd32/netbsd32.h>
65 #include <compat/netbsd32/netbsd32_syscallargs.h>
66 
67 #include <compat/linux/common/linux_signal.h>
68 #include <compat/linux/common/linux_errno.h>
69 
70 #include <compat/linux32/common/linux32_types.h>
71 #include <compat/linux32/common/linux32_errno.h>
72 #include <compat/linux32/common/linux32_machdep.h>
73 #include <compat/linux32/common/linux32_signal.h>
74 #include <compat/linux32/common/linux32_exec.h>
75 #include <compat/linux32/linux32_syscallargs.h>
76 
77 #include <sys/cpu.h>
78 #include <machine/cpufunc.h>
79 #include <machine/psl.h>
80 #include <machine/reg.h>
81 #include <machine/segments.h>
82 #include <machine/specialreg.h>
83 #include <machine/sysarch.h>
84 #include <machine/vmparam.h>
85 
86 extern char linux32_sigcode[1];
87 extern char linux32_rt_sigcode[1];
88 extern char linux32_esigcode[1];
89 
90 extern void (osyscall_return)(void);
91 
92 static void linux32_save_ucontext(struct lwp *, struct trapframe *,
93     const sigset_t *, struct sigaltstack *, struct linux32_ucontext *);
94 static void linux32_save_sigcontext(struct lwp *, struct trapframe *,
95     const sigset_t *, struct linux32_sigcontext *);
96 static void linux32_rt_sendsig(const ksiginfo_t *, const sigset_t *);
97 static void linux32_old_sendsig(const ksiginfo_t *, const sigset_t *);
98 static int linux32_restore_sigcontext(struct lwp *,
99     struct linux32_sigcontext *, register_t *);
100 
101 void
102 linux32_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
103 {
104 	if (SIGACTION(curproc, ksi->ksi_signo).sa_flags & SA_SIGINFO)
105 		linux32_rt_sendsig(ksi, mask);
106 	else
107 		linux32_old_sendsig(ksi, mask);
108 	return;
109 }
110 
111 void
112 linux32_old_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
113 {
114 	struct lwp *l = curlwp;
115 	struct proc *p = l->l_proc;
116 	struct trapframe *tf;
117 	struct linux32_sigframe *fp, frame;
118 	int onstack, error;
119 	int sig = ksi->ksi_signo;
120 	sig_t catcher = SIGACTION(p, sig).sa_handler;
121 	struct sigaltstack *sas = &l->l_sigstk;
122 
123 	tf = l->l_md.md_regs;
124 	/* Do we need to jump onto the signal stack? */
125 	onstack = (sas->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
126 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
127 
128 
129 	/* Allocate space for the signal handler context. */
130 	if (onstack)
131 		fp = (struct linux32_sigframe *)((char *)sas->ss_sp +
132 		    sas->ss_size);
133 	else
134 		fp = (struct linux32_sigframe *)tf->tf_rsp;
135 	fp--;
136 
137 	/* Build stack frame for signal trampoline. */
138 	NETBSD32PTR32(frame.sf_handler, catcher);
139 	frame.sf_sig = native_to_linux32_signo[sig];
140 
141 	linux32_save_sigcontext(l, tf, mask, &frame.sf_sc);
142 
143 	sendsig_reset(l, sig);
144 	mutex_exit(p->p_lock);
145 	error = copyout(&frame, fp, sizeof(frame));
146 	mutex_enter(p->p_lock);
147 
148 	if (error != 0) {
149 		/*
150 		 * Process has trashed its stack; give it an illegal
151 		 * instruction to halt it in its tracks.
152 		 */
153 		sigexit(l, SIGILL);
154 		/* NOTREACHED */
155 	}
156 
157 	/*
158 	 * Build context to run handler in.
159 	 */
160 	tf->tf_gs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
161 	tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
162 	tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
163 	tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
164 	tf->tf_rip = ((long)p->p_sigctx.ps_sigcode) & 0xffffffff;
165 	tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL) & 0xffffffff;
166 	tf->tf_rflags &= ~PSL_CLEARSIG & 0xffffffff;
167 	tf->tf_rsp = (long)fp & 0xffffffff;
168 	tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
169 
170 	/* Remember that we're now on the signal stack. */
171 	if (onstack)
172 		sas->ss_flags |= SS_ONSTACK;
173 
174 	return;
175 }
176 
177 void
178 linux32_rt_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
179 {
180 	struct lwp *l = curlwp;
181 	struct proc *p = l->l_proc;
182 	struct trapframe *tf;
183 	struct linux32_rt_sigframe *fp, frame;
184 	int onstack, error;
185 	linux32_siginfo_t *lsi;
186 	int sig = ksi->ksi_signo;
187 	sig_t catcher = SIGACTION(p, sig).sa_handler;
188 	struct sigaltstack *sas = &l->l_sigstk;
189 
190 	tf = l->l_md.md_regs;
191 	/* Do we need to jump onto the signal stack? */
192 	onstack = (sas->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
193 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
194 
195 
196 	/* Allocate space for the signal handler context. */
197 	if (onstack)
198 		fp = (struct linux32_rt_sigframe *)((char *)sas->ss_sp +
199 		    sas->ss_size);
200 	else
201 		fp = (struct linux32_rt_sigframe *)tf->tf_rsp;
202 	fp--;
203 
204 	/* Build stack frame for signal trampoline. */
205 	NETBSD32PTR32(frame.sf_handler, catcher);
206 	frame.sf_sig = native_to_linux32_signo[sig];
207 	NETBSD32PTR32(frame.sf_sip, &fp->sf_si);
208 	NETBSD32PTR32(frame.sf_ucp, &fp->sf_uc);
209 
210 	lsi = &frame.sf_si;
211 	(void)memset(lsi, 0, sizeof(frame.sf_si));
212 	lsi->lsi_errno = native_to_linux32_errno[ksi->ksi_errno];
213 	lsi->lsi_code = native_to_linux_si_code(ksi->ksi_code);
214 	lsi->lsi_signo = frame.sf_sig;
215 	switch (lsi->lsi_signo) {
216 	case LINUX32_SIGILL:
217 	case LINUX32_SIGFPE:
218 	case LINUX32_SIGSEGV:
219 	case LINUX32_SIGBUS:
220 	case LINUX32_SIGTRAP:
221 		NETBSD32PTR32(lsi->lsi_addr, ksi->ksi_addr);
222 		break;
223 	case LINUX32_SIGCHLD:
224 		lsi->lsi_uid = ksi->ksi_uid;
225 		lsi->lsi_pid = ksi->ksi_pid;
226 		lsi->lsi_utime = ksi->ksi_utime;
227 		lsi->lsi_stime = ksi->ksi_stime;
228 
229 		/* We use the same codes */
230 		lsi->lsi_code = ksi->ksi_code;
231 		/* XXX is that right? */
232 		lsi->lsi_status = WEXITSTATUS(ksi->ksi_status);
233 		break;
234 	case LINUX32_SIGIO:
235 		lsi->lsi_band = ksi->ksi_band;
236 		lsi->lsi_fd = ksi->ksi_fd;
237 		break;
238 	default:
239 		lsi->lsi_uid = ksi->ksi_uid;
240 		lsi->lsi_pid = ksi->ksi_pid;
241 		if (lsi->lsi_signo == LINUX32_SIGALRM ||
242 		    lsi->lsi_signo >= LINUX32_SIGRTMIN)
243 			NETBSD32PTR32(lsi->lsi_value.sival_ptr,
244 			     ksi->ksi_value.sival_ptr);
245 		break;
246 	}
247 
248 	/* Save register context. */
249 	linux32_save_ucontext(l, tf, mask, sas, &frame.sf_uc);
250 	sendsig_reset(l, sig);
251 	mutex_exit(p->p_lock);
252 	error = copyout(&frame, fp, sizeof(frame));
253 	mutex_enter(p->p_lock);
254 
255 	if (error != 0) {
256 		/*
257 		 * Process has trashed its stack; give it an illegal
258 		 * instruction to halt it in its tracks.
259 		 */
260 		sigexit(l, SIGILL);
261 		/* NOTREACHED */
262 	}
263 
264 	/*
265 	 * Build context to run handler in.
266 	 */
267 	tf->tf_gs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
268 	tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
269 	tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
270 	tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
271 	tf->tf_rip = (((long)p->p_sigctx.ps_sigcode) +
272 	    (linux32_rt_sigcode - linux32_sigcode)) & 0xffffffff;
273 	tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL) & 0xffffffff;
274 	tf->tf_rflags &= ~PSL_CLEARSIG & 0xffffffff;
275 	tf->tf_rsp = (long)fp & 0xffffffff;
276 	tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
277 
278 	/* Remember that we're now on the signal stack. */
279 	if (onstack)
280 		sas->ss_flags |= SS_ONSTACK;
281 
282 	return;
283 }
284 
285 void
286 linux32_setregs(struct lwp *l, struct exec_package *pack, u_long stack)
287 {
288 	struct pcb *pcb = &l->l_addr->u_pcb;
289 	struct trapframe *tf;
290 	struct proc *p = l->l_proc;
291 	void **retaddr;
292 
293 	/* If we were using the FPU, forget about it. */
294 	if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
295 		fpusave_lwp(l, 0);
296 
297 #if defined(USER_LDT) && 0
298 	pmap_ldt_cleanup(p);
299 #endif
300 
301 	netbsd32_adjust_limits(p);
302 
303 	l->l_md.md_flags &= ~MDP_USEDFPU;
304 	pcb->pcb_flags = 0;
305 	pcb->pcb_savefpu.fp_fxsave.fx_fcw = __Linux_NPXCW__;
306 	pcb->pcb_savefpu.fp_fxsave.fx_mxcsr = __INITIAL_MXCSR__;
307 	pcb->pcb_savefpu.fp_fxsave.fx_mxcsr_mask = __INITIAL_MXCSR_MASK__;
308 	pcb->pcb_fs = 0;
309 	pcb->pcb_gs = 0;
310 
311 
312 	p->p_flag |= PK_32;
313 
314 	tf = l->l_md.md_regs;
315 	tf->tf_rax = 0;
316 	tf->tf_rbx = (u_int64_t)p->p_psstr & 0xffffffff;
317 	tf->tf_rcx = pack->ep_entry & 0xffffffff;
318 	tf->tf_rdx = 0;
319 	tf->tf_rsi = 0;
320 	tf->tf_rdi = 0;
321 	tf->tf_rbp = 0;
322 	tf->tf_rsp = stack & 0xffffffff;
323 	tf->tf_r8 = 0;
324 	tf->tf_r9 = 0;
325 	tf->tf_r10 = 0;
326 	tf->tf_r11 = 0;
327 	tf->tf_r12 = 0;
328 	tf->tf_r13 = 0;
329 	tf->tf_r14 = 0;
330 	tf->tf_r15 = 0;
331 	tf->tf_rip = pack->ep_entry & 0xffffffff;
332 	tf->tf_rflags = PSL_USERSET;
333 	tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL) & 0xffffffff;
334 	tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
335 	tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
336 	tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
337 	tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
338 	tf->tf_gs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
339 
340 	/* XXX frob return address to return via old iret method, not sysret */
341 	retaddr = (void **)tf - 1;
342 	*retaddr = (void *)osyscall_return;
343 	return;
344 }
345 
346 static void
347 linux32_save_ucontext(struct lwp *l, struct trapframe *tf, const sigset_t *mask, struct sigaltstack *sas, struct linux32_ucontext *uc)
348 {
349 	uc->uc_flags = 0;
350 	NETBSD32PTR32(uc->uc_link, NULL);
351 	native_to_linux32_sigaltstack(&uc->uc_stack, sas);
352 	linux32_save_sigcontext(l, tf, mask, &uc->uc_mcontext);
353 	native_to_linux32_sigset(&uc->uc_sigmask, mask);
354 	(void)memset(&uc->uc_fpregs_mem, 0, sizeof(uc->uc_fpregs_mem));
355 }
356 
357 static void
358 linux32_save_sigcontext(l, tf, mask, sc)
359 	struct lwp *l;
360 	struct trapframe *tf;
361 	const sigset_t *mask;
362 	struct linux32_sigcontext *sc;
363 {
364 	/* Save register context. */
365 	sc->sc_gs = tf->tf_gs;
366 	sc->sc_fs = tf->tf_fs;
367 	sc->sc_es = tf->tf_es;
368 	sc->sc_ds = tf->tf_ds;
369 	sc->sc_eflags = tf->tf_rflags;
370 	sc->sc_edi = tf->tf_rdi;
371 	sc->sc_esi = tf->tf_rsi;
372 	sc->sc_esp = tf->tf_rsp;
373 	sc->sc_ebp = tf->tf_rbp;
374 	sc->sc_ebx = tf->tf_rbx;
375 	sc->sc_edx = tf->tf_rdx;
376 	sc->sc_ecx = tf->tf_rcx;
377 	sc->sc_eax = tf->tf_rax;
378 	sc->sc_eip = tf->tf_rip;
379 	sc->sc_cs = tf->tf_cs;
380 	sc->sc_esp_at_signal = tf->tf_rsp;
381 	sc->sc_ss = tf->tf_ss;
382 	sc->sc_err = tf->tf_err;
383 	sc->sc_trapno = tf->tf_trapno;
384 	sc->sc_cr2 = l->l_addr->u_pcb.pcb_cr2;
385 	NETBSD32PTR32(sc->sc_387, NULL);
386 
387 	/* Save signal stack. */
388 	/* Linux doesn't save the onstack flag in sigframe */
389 
390 	/* Save signal mask. */
391 	native_to_linux32_old_sigset(&sc->sc_mask, mask);
392 }
393 
394 int
395 linux32_sys_sigreturn(struct lwp *l, const struct linux32_sys_sigreturn_args *uap, register_t *retval)
396 {
397 	/* {
398 		syscallarg(linux32_sigcontextp_t) scp;
399 	} */
400 	struct linux32_sigcontext ctx;
401 	int error;
402 
403 	if ((error = copyin(SCARG_P32(uap, scp), &ctx, sizeof(ctx))) != 0)
404 		return error;
405 
406 	return linux32_restore_sigcontext(l, &ctx, retval);
407 }
408 
409 int
410 linux32_sys_rt_sigreturn(struct lwp *l, const struct linux32_sys_rt_sigreturn_args *uap, register_t *retval)
411 {
412 	/* {
413 		syscallarg(linux32_ucontextp_t) ucp;
414 	} */
415 	struct linux32_ucontext ctx;
416 	int error;
417 
418 	if ((error = copyin(SCARG_P32(uap, ucp), &ctx, sizeof(ctx))) != 0)
419 		return error;
420 
421 	return linux32_restore_sigcontext(l, &ctx.uc_mcontext, retval);
422 }
423 
424 static int
425 linux32_restore_sigcontext(l, scp, retval)
426 	struct lwp *l;
427 	struct linux32_sigcontext *scp;
428 	register_t *retval;
429 {
430 	struct trapframe *tf;
431 	struct proc *p = l->l_proc;
432 	struct sigaltstack *sas = &l->l_sigstk;
433 	sigset_t mask;
434 	ssize_t ss_gap;
435 
436 	/* Restore register context. */
437 	tf = l->l_md.md_regs;
438 
439 	/*
440 	 * Check for security violations.  If we're returning to
441 	 * protected mode, the CPU will validate the segment registers
442 	 * automatically and generate a trap on violations.  We handle
443 	 * the trap, rather than doing all of the checking here.
444 	 */
445 	if (((scp->sc_eflags ^ tf->tf_rflags) & PSL_USERSTATIC) != 0 ||
446 	    !USERMODE(scp->sc_cs, scp->sc_eflags))
447 		return EINVAL;
448 
449 	if (scp->sc_fs != 0 && !VALID_USER_DSEL32(scp->sc_fs))
450 		return EINVAL;
451 
452 	if (scp->sc_gs != 0 && !VALID_USER_DSEL32(scp->sc_gs))
453 		return EINVAL;
454 
455 	if (scp->sc_es != 0 && !VALID_USER_DSEL32(scp->sc_es))
456 		return EINVAL;
457 
458 	if (!VALID_USER_DSEL32(scp->sc_ds) ||
459 	    !VALID_USER_DSEL32(scp->sc_ss))
460 		return EINVAL;
461 
462 	if (scp->sc_eip >= VM_MAXUSER_ADDRESS32)
463 		return EINVAL;
464 
465 	tf->tf_gs = (register_t)scp->sc_gs & 0xffffffff;
466 	tf->tf_fs = (register_t)scp->sc_fs & 0xffffffff;
467 	tf->tf_es = (register_t)scp->sc_es & 0xffffffff;
468 	tf->tf_ds = (register_t)scp->sc_ds & 0xffffffff;
469 	tf->tf_rflags &= ~PSL_USER;
470 	tf->tf_rflags |= ((register_t)scp->sc_eflags & PSL_USER);
471 	tf->tf_rdi = (register_t)scp->sc_edi & 0xffffffff;
472 	tf->tf_rsi = (register_t)scp->sc_esi & 0xffffffff;
473 	tf->tf_rbp = (register_t)scp->sc_ebp & 0xffffffff;
474 	tf->tf_rbx = (register_t)scp->sc_ebx & 0xffffffff;
475 	tf->tf_rdx = (register_t)scp->sc_edx & 0xffffffff;
476 	tf->tf_rcx = (register_t)scp->sc_ecx & 0xffffffff;
477 	tf->tf_rax = (register_t)scp->sc_eax & 0xffffffff;
478 	tf->tf_rip = (register_t)scp->sc_eip & 0xffffffff;
479 	tf->tf_cs = (register_t)scp->sc_cs & 0xffffffff;
480 	tf->tf_rsp = (register_t)scp->sc_esp_at_signal & 0xffffffff;
481 	tf->tf_ss = (register_t)scp->sc_ss & 0xffffffff;
482 
483 	mutex_enter(p->p_lock);
484 
485 	/* Restore signal stack. */
486 	ss_gap = (ssize_t)
487 	    ((char *)NETBSD32IPTR64(scp->sc_esp_at_signal)
488 	     - (char *)sas->ss_sp);
489 	if (ss_gap >= 0 && ss_gap < sas->ss_size)
490 		sas->ss_flags |= SS_ONSTACK;
491 	else
492 		sas->ss_flags &= ~SS_ONSTACK;
493 
494 	/* Restore signal mask. */
495 	linux32_old_to_native_sigset(&mask, &scp->sc_mask);
496 	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
497 
498 	mutex_exit(p->p_lock);
499 
500 #ifdef DEBUG_LINUX
501 	printf("linux32_sigreturn: rip = 0x%lx, rsp = 0x%lx, flags = 0x%lx\n",
502 	    tf->tf_rip, tf->tf_rsp, tf->tf_rflags);
503 #endif
504 	return EJUSTRETURN;
505 }
506