linux32_machdep.c revision 1.6 1 /* $NetBSD: linux32_machdep.c,v 1.6 2007/02/17 22:31:41 pavel 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.6 2007/02/17 22:31:41 pavel 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 <machine/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) __P((void));
91
92 static void linux32_save_ucontext __P((struct lwp *, struct trapframe *,
93 const sigset_t *, struct sigaltstack *, struct linux32_ucontext *));
94 static void linux32_save_sigcontext __P((struct lwp *, struct trapframe *,
95 const sigset_t *, struct linux32_sigcontext *));
96 static void linux32_rt_sendsig __P((const ksiginfo_t *, const sigset_t *));
97 static void linux32_old_sendsig __P((const ksiginfo_t *, const sigset_t *));
98 static int linux32_restore_sigcontext __P((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 *)((caddr_t)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 frame.sf_handler = (linux32_handler_t)(long)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_smutex);
145 error = copyout(&frame, fp, sizeof(frame));
146 mutex_enter(&p->p_smutex);
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_T|PSL_VM|PSL_AC) & 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 *)((caddr_t)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 frame.sf_handler = (linux32_handler_t)(long)catcher;
206 frame.sf_sig = native_to_linux32_signo[sig];
207 frame.sf_sip = (linux32_siginfop_t)(long)&fp->sf_si;
208 frame.sf_ucp = (linux32_ucontextp_t)(long)&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 = ksi->ksi_code;
214 lsi->lsi_signo = native_to_linux32_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 lsi->lsi_addr = (netbsd32_voidp)(long)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 lsi->lsi_value.sival_ptr =
244 (netbsd32_voidp)(long)ksi->ksi_sigval.sival_ptr;
245 break;
246 }
247
248 /* Save register context. */
249 sendsig_reset(l, sig);
250 mutex_exit(&p->p_smutex);
251 linux32_save_ucontext(l, tf, mask, sas, &frame.sf_uc);
252 error = copyout(&frame, fp, sizeof(frame));
253 mutex_enter(&p->p_smutex);
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_T|PSL_VM|PSL_AC) & 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(l, tf, mask, sas, uc)
348 struct lwp *l;
349 struct trapframe *tf;
350 const sigset_t *mask;
351 struct sigaltstack *sas;
352 struct linux32_ucontext *uc;
353 {
354 uc->uc_flags = 0;
355 uc->uc_link = (linux32_ucontextp_t)(long)NULL;
356 native_to_linux32_sigaltstack(&uc->uc_stack, sas);
357 linux32_save_sigcontext(l, tf, mask, &uc->uc_mcontext);
358 native_to_linux32_sigset(&uc->uc_sigmask, mask);
359 (void)memset(&uc->uc_fpregs_mem, 0, sizeof(uc->uc_fpregs_mem));
360 }
361
362 static void
363 linux32_save_sigcontext(l, tf, mask, sc)
364 struct lwp *l;
365 struct trapframe *tf;
366 const sigset_t *mask;
367 struct linux32_sigcontext *sc;
368 {
369 /* Save register context. */
370 sc->sc_gs = tf->tf_gs;
371 sc->sc_fs = tf->tf_fs;
372 sc->sc_es = tf->tf_es;
373 sc->sc_ds = tf->tf_ds;
374 sc->sc_eflags = tf->tf_rflags;
375 sc->sc_edi = tf->tf_rdi;
376 sc->sc_esi = tf->tf_rsi;
377 sc->sc_esp = tf->tf_rsp;
378 sc->sc_ebp = tf->tf_rbp;
379 sc->sc_ebx = tf->tf_rbx;
380 sc->sc_edx = tf->tf_rdx;
381 sc->sc_ecx = tf->tf_rcx;
382 sc->sc_eax = tf->tf_rax;
383 sc->sc_eip = tf->tf_rip;
384 sc->sc_cs = tf->tf_cs;
385 sc->sc_esp_at_signal = tf->tf_rsp;
386 sc->sc_ss = tf->tf_ss;
387 sc->sc_err = tf->tf_err;
388 sc->sc_trapno = tf->tf_trapno;
389 /* sc->sc_cr2 = l->l_addr->u_pcb.pcb_cr2; */ /* XXX */
390 sc->sc_387 = (linux32_fpstatep_t)(long)NULL;
391
392 /* Save signal stack. */
393 /* Linux doesn't save the onstack flag in sigframe */
394
395 /* Save signal mask. */
396 native_to_linux32_old_sigset(&sc->sc_mask, mask);
397 }
398
399 int
400 linux32_sys_sigreturn(l, v, retval)
401 struct lwp *l;
402 void *v;
403 register_t *retval;
404 {
405 struct linux32_sys_sigreturn_args /* {
406 syscallarg(linux32_sigcontextp_t) scp;
407 } */ *uap = v;
408 struct linux32_sigcontext ctx;
409 int error;
410
411 if ((error = copyin(NETBSD32PTR64(SCARG(uap, scp)),
412 &ctx, sizeof(ctx))) != 0)
413 return error;
414
415 return linux32_restore_sigcontext(l, &ctx, retval);
416 }
417
418 int
419 linux32_sys_rt_sigreturn(l, v, retval)
420 struct lwp *l;
421 void *v;
422 register_t *retval;
423 {
424 struct linux32_sys_rt_sigreturn_args /* {
425 syscallarg(linux32_ucontextp_t) ucp;
426 } */ *uap = v;
427 struct linux32_ucontext ctx;
428 int error;
429
430 if ((error = copyin(NETBSD32PTR64(SCARG(uap, ucp)),
431 &ctx, sizeof(ctx))) != 0)
432 return error;
433
434 return linux32_restore_sigcontext(l, &ctx.uc_mcontext, retval);
435 }
436
437 static int
438 linux32_restore_sigcontext(l, scp, retval)
439 struct lwp *l;
440 struct linux32_sigcontext *scp;
441 register_t *retval;
442 {
443 struct trapframe *tf;
444 struct proc *p = l->l_proc;
445 struct sigaltstack *sas = &l->l_sigstk;
446 sigset_t mask;
447 ssize_t ss_gap;
448
449 /* Restore register context. */
450 tf = l->l_md.md_regs;
451
452 /*
453 * Check for security violations. If we're returning to
454 * protected mode, the CPU will validate the segment registers
455 * automatically and generate a trap on violations. We handle
456 * the trap, rather than doing all of the checking here.
457 */
458 if (((scp->sc_eflags ^ tf->tf_rflags) & PSL_USERSTATIC) != 0 ||
459 !USERMODE(scp->sc_cs, scp->sc_eflags))
460 return EINVAL;
461
462 if (scp->sc_fs != 0 && !VALID_USER_DSEL32(scp->sc_fs))
463 return EINVAL;
464
465 if (scp->sc_gs != 0 && !VALID_USER_DSEL32(scp->sc_gs))
466 return EINVAL;
467
468 if (scp->sc_es != 0 && !VALID_USER_DSEL32(scp->sc_es))
469 return EINVAL;
470
471 if (!VALID_USER_DSEL32(scp->sc_ds) ||
472 !VALID_USER_DSEL32(scp->sc_ss))
473 return EINVAL;
474
475 if (scp->sc_eip >= VM_MAXUSER_ADDRESS32)
476 return EINVAL;
477
478 tf->tf_gs = (register_t)scp->sc_gs & 0xffffffff;
479 tf->tf_fs = (register_t)scp->sc_fs & 0xffffffff;
480 tf->tf_es = (register_t)scp->sc_es & 0xffffffff;
481 tf->tf_ds = (register_t)scp->sc_ds & 0xffffffff;
482 tf->tf_rflags &= ~PSL_USER;
483 tf->tf_rflags |= ((register_t)scp->sc_eflags & PSL_USER);
484 tf->tf_rdi = (register_t)scp->sc_edi & 0xffffffff;
485 tf->tf_rsi = (register_t)scp->sc_esi & 0xffffffff;
486 tf->tf_rbp = (register_t)scp->sc_ebp & 0xffffffff;
487 tf->tf_rbx = (register_t)scp->sc_ebx & 0xffffffff;
488 tf->tf_rdx = (register_t)scp->sc_edx & 0xffffffff;
489 tf->tf_rcx = (register_t)scp->sc_ecx & 0xffffffff;
490 tf->tf_rax = (register_t)scp->sc_eax & 0xffffffff;
491 tf->tf_rip = (register_t)scp->sc_eip & 0xffffffff;
492 tf->tf_cs = (register_t)scp->sc_cs & 0xffffffff;
493 tf->tf_rsp = (register_t)scp->sc_esp_at_signal & 0xffffffff;
494 tf->tf_ss = (register_t)scp->sc_ss & 0xffffffff;
495
496 mutex_enter(&p->p_smutex);
497
498 /* Restore signal stack. */
499 ss_gap = (ssize_t)
500 ((caddr_t)NETBSD32PTR64(scp->sc_esp_at_signal)
501 - (caddr_t)sas->ss_sp);
502 if (ss_gap >= 0 && ss_gap < sas->ss_size)
503 sas->ss_flags |= SS_ONSTACK;
504 else
505 sas->ss_flags &= ~SS_ONSTACK;
506
507 /* Restore signal mask. */
508 linux32_old_to_native_sigset(&mask, &scp->sc_mask);
509 (void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
510
511 mutex_exit(&p->p_smutex);
512
513 #ifdef DEBUG_LINUX
514 printf("linux32_sigreturn: rip = 0x%lx, rsp = 0x%lx, flags = 0x%lx\n",
515 tf->tf_rip, tf->tf_rsp, tf->tf_rflags);
516 #endif
517 return EJUSTRETURN;
518 }
519