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