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