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