linux_machdep.c revision 1.2 1 /* $NetBSD: linux_machdep.c,v 1.2 2005/05/15 21:43:08 fvdl Exp $ */
2
3 /*-
4 * Copyright (c) 2005 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
34 #include <sys/cdefs.h>
35
36 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.2 2005/05/15 21:43:08 fvdl Exp $");
37
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/systm.h>
41 #include <sys/signal.h>
42 #include <sys/exec.h>
43 #include <sys/proc.h>
44 #include <sys/ptrace.h> /* for process_read_fpregs() */
45 #include <sys/user.h>
46 #include <sys/ucontext.h>
47
48 #include <machine/reg.h>
49 #include <machine/pcb.h>
50 #include <machine/fpu.h>
51 #include <machine/mcontext.h>
52 #include <machine/specialreg.h>
53 #include <machine/vmparam.h>
54
55 #include <compat/linux/common/linux_signal.h>
56 #include <compat/linux/common/linux_errno.h>
57 #include <compat/linux/common/linux_exec.h>
58 #include <compat/linux/common/linux_ioctl.h>
59 #include <compat/linux/common/linux_prctl.h>
60 #include <compat/linux/common/linux_machdep.h>
61 #include <compat/linux/linux_syscallargs.h>
62
63
64 void
65 linux_setregs(l, epp, stack)
66 struct lwp *l;
67 struct exec_package *epp;
68 u_long stack;
69 {
70 struct pcb *pcb = &l->l_addr->u_pcb;
71 struct trapframe *tf;
72
73 /* If we were using the FPU, forget about it. */
74 if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
75 fpusave_lwp(l, 0);
76
77 l->l_md.md_flags &= ~MDP_USEDFPU;
78 pcb->pcb_flags = 0;
79 pcb->pcb_savefpu.fp_fxsave.fx_fcw = __NetBSD_NPXCW__;
80 pcb->pcb_savefpu.fp_fxsave.fx_mxcsr = __INITIAL_MXCSR__;
81 pcb->pcb_savefpu.fp_fxsave.fx_mxcsr_mask = __INITIAL_MXCSR_MASK__;
82
83 l->l_proc->p_flag &= ~P_32;
84
85 printf("stack = 0x%lx, entry = 0x%lx\n", stack, epp->ep_entry);
86 tf = l->l_md.md_regs;
87 tf->tf_rax = 0;
88 tf->tf_rbx = 0;
89 tf->tf_rcx = epp->ep_entry;
90 tf->tf_rdx = 0;
91 tf->tf_rsi = 0;
92 tf->tf_rdi = 0;
93 tf->tf_rbp = 0;
94 tf->tf_rsp = stack;
95 tf->tf_r8 = 0;
96 tf->tf_r9 = 0;
97 tf->tf_r10 = 0;
98 tf->tf_r11 = 0;
99 tf->tf_r12 = 0;
100 tf->tf_r13 = 0;
101 tf->tf_r14 = 0;
102 tf->tf_r15 = 0;
103 tf->tf_rip = epp->ep_entry;
104 tf->tf_rflags = PSL_MBO | PSL_I;
105 tf->tf_cs = LSEL(LUCODE_SEL, SEL_UPL);
106 tf->tf_ss = LSEL(LUDATA_SEL, SEL_UPL);
107 tf->tf_ds = 0;
108 tf->tf_es = 0;
109 tf->tf_fs = 0;
110 tf->tf_gs = 0;
111
112 return;
113 }
114
115 void
116 linux_sendsig(ksi, mask)
117 const ksiginfo_t *ksi;
118 const sigset_t *mask;
119 {
120 struct lwp *l = curlwp;
121 struct proc *p = l->l_proc;
122 struct sigacts *ps = p->p_sigacts;
123 int onstack;
124 int sig = ksi->ksi_signo;
125 struct linux_rt_sigframe *sfp, sigframe;
126 struct linux__fpstate *fpsp, fpstate;
127 struct fpreg fpregs;
128 struct trapframe *tf = l->l_md.md_regs;
129 sig_t catcher = SIGACTION(p, sig).sa_handler;
130 linux_sigset_t lmask;
131 char *sp;
132 int error;
133
134 /* Do we need to jump onto the signal stack? */
135 onstack =
136 (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
137 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
138
139 /* Allocate space for the signal handler context. */
140 if (onstack)
141 sp = ((caddr_t)p->p_sigctx.ps_sigstk.ss_sp +
142 p->p_sigctx.ps_sigstk.ss_size);
143 else
144 sp = (caddr_t)tf->tf_rsp - 128;
145
146
147 /*
148 * Save FPU state, if any
149 */
150 if (l->l_md.md_flags & MDP_USEDFPU) {
151 sp = (char *)
152 (((long)sp - sizeof(struct linux__fpstate)) & ~0xfUL);
153 fpsp = (struct linux__fpstate *)sp;
154
155 (void)process_read_fpregs(l, &fpregs);
156 bzero(&fpstate, sizeof(fpstate));
157
158 fpstate.cwd = fpregs.fp_fcw;
159 fpstate.swd = fpregs.fp_fsw;
160 fpstate.twd = fpregs.fp_ftw;
161 fpstate.fop = fpregs.fp_fop;
162 fpstate.rip = fpregs.fp_rip;
163 fpstate.rdp = fpregs.fp_rdp;
164 fpstate.mxcsr = fpregs.fp_mxcsr;
165 fpstate.mxcsr_mask = fpregs.fp_mxcsr_mask;
166 memcpy(&fpstate.st_space, &fpregs.fp_st,
167 sizeof(fpstate.st_space));
168 memcpy(&fpstate.xmm_space, &fpregs.fp_xmm,
169 sizeof(fpstate.xmm_space));
170
171 if ((error = copyout(&fpstate, fpsp, sizeof(fpstate))) != 0) {
172 sigexit(l, SIGILL);
173 return;
174 }
175 } else {
176 fpsp = NULL;
177 }
178
179 /*
180 * Populate the rt_sigframe
181 */
182 sp = (char *)
183 ((((long)sp - sizeof(struct linux_rt_sigframe)) & ~0xfUL) - 8);
184 sfp = (struct linux_rt_sigframe *)sp;
185
186 bzero(&sigframe, sizeof(sigframe));
187 sigframe.pretcode = (char *)ps->sa_sigdesc[sig].sd_tramp;
188
189 /*
190 * The user context
191 */
192 sigframe.uc.luc_flags = 0;
193 sigframe.uc.luc_link = NULL;
194
195 /* This is used regardless of SA_ONSTACK in Linux */
196 sigframe.uc.luc_stack.ss_sp = p->p_sigctx.ps_sigstk.ss_sp;
197 sigframe.uc.luc_stack.ss_size = p->p_sigctx.ps_sigstk.ss_size;
198 sigframe.uc.luc_stack.ss_flags = 0;
199 if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
200 sigframe.uc.luc_stack.ss_flags |= LINUX_SS_ONSTACK;
201 if (p->p_sigctx.ps_sigstk.ss_flags & SS_DISABLE)
202 sigframe.uc.luc_stack.ss_flags |= LINUX_SS_DISABLE;
203
204 sigframe.uc.luc_mcontext.r8 = tf->tf_r8;
205 sigframe.uc.luc_mcontext.r9 = tf->tf_r9;
206 sigframe.uc.luc_mcontext.r10 = tf->tf_r10;
207 sigframe.uc.luc_mcontext.r11 = tf->tf_r11;
208 sigframe.uc.luc_mcontext.r12 = tf->tf_r12;
209 sigframe.uc.luc_mcontext.r13 = tf->tf_r13;
210 sigframe.uc.luc_mcontext.r14 = tf->tf_r14;
211 sigframe.uc.luc_mcontext.r15 = tf->tf_r15;
212 sigframe.uc.luc_mcontext.rdi = tf->tf_rdi;
213 sigframe.uc.luc_mcontext.rsi = tf->tf_rsi;
214 sigframe.uc.luc_mcontext.rbp = tf->tf_rbp;
215 sigframe.uc.luc_mcontext.rbx = tf->tf_rbx;
216 sigframe.uc.luc_mcontext.rdx = tf->tf_rdx;
217 sigframe.uc.luc_mcontext.rcx = tf->tf_rcx;
218 sigframe.uc.luc_mcontext.rsp = tf->tf_rsp;
219 sigframe.uc.luc_mcontext.eflags = tf->tf_rflags;
220 sigframe.uc.luc_mcontext.cs = tf->tf_cs;
221 sigframe.uc.luc_mcontext.gs = tf->tf_gs;
222 sigframe.uc.luc_mcontext.fs = tf->tf_fs;
223 sigframe.uc.luc_mcontext.err = tf->tf_err;
224 sigframe.uc.luc_mcontext.trapno = tf->tf_trapno;
225 native_to_linux_sigset(&lmask, mask);
226 sigframe.uc.luc_mcontext.oldmask = lmask.sig[0];
227 sigframe.uc.luc_mcontext.cr2 = (long)l->l_addr->u_pcb.pcb_onfault;
228 sigframe.uc.luc_mcontext.fpstate = fpsp;
229 native_to_linux_sigset(&sigframe.uc.luc_sigmask, mask);
230
231 /*
232 * the siginfo structure
233 */
234 sigframe.info.lsi_signo = native_to_linux_signo[sig];
235 sigframe.info.lsi_errno = native_to_linux_errno[ksi->ksi_errno];
236 sigframe.info.lsi_code = ksi->ksi_code;
237
238 /* XXX This is a rought conversion, taken from i386 code */
239 switch (sigframe.info.lsi_signo) {
240 case LINUX_SIGILL:
241 case LINUX_SIGFPE:
242 case LINUX_SIGSEGV:
243 case LINUX_SIGBUS:
244 case LINUX_SIGTRAP:
245 sigframe.info._sifields._sigfault._addr = ksi->ksi_addr;
246 break;
247 case LINUX_SIGCHLD:
248 sigframe.info._sifields._sigchld._pid = ksi->ksi_pid;
249 sigframe.info._sifields._sigchld._uid = ksi->ksi_uid;
250 sigframe.info._sifields._sigchld._status = ksi->ksi_status;
251 sigframe.info._sifields._sigchld._utime = ksi->ksi_utime;
252 sigframe.info._sifields._sigchld._stime = ksi->ksi_stime;
253 break;
254 case LINUX_SIGIO:
255 sigframe.info._sifields._sigpoll._band = ksi->ksi_band;
256 sigframe.info._sifields._sigpoll._fd = ksi->ksi_fd;
257 break;
258 default:
259 sigframe.info._sifields._sigchld._pid = ksi->ksi_pid;
260 sigframe.info._sifields._sigchld._uid = ksi->ksi_uid;
261 if ((sigframe.info.lsi_signo == LINUX_SIGALRM) ||
262 (sigframe.info.lsi_signo >= LINUX_SIGRTMIN))
263 sigframe.info._sifields._timer._sigval.sival_ptr =
264 ksi->ksi_sigval.sival_ptr;
265 break;
266 }
267
268 if ((error = copyout(&sigframe, sp, sizeof(sigframe))) != 0) {
269 sigexit(l, SIGILL);
270 return;
271 }
272
273 /*
274 * Setup registers
275 */
276 buildcontext(l, catcher, sp);
277 tf->tf_rdi = sigframe.info.lsi_signo;
278 tf->tf_rax = 0;
279 tf->tf_rsi = (long)&sfp->info;
280 tf->tf_rdx = (long)&sfp->uc;
281
282 /*
283 * Remember we use signal stack
284 */
285 if (onstack)
286 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
287 return;
288 }
289
290 int
291 linux_sys_modify_ldt(l, v, retval)
292 struct lwp *l;
293 void *v;
294 register_t *retval;
295 {
296 return 0;
297 }
298
299 int
300 linux_sys_iopl(l, v, retval)
301 struct lwp *l;
302 void *v;
303 register_t *retval;
304 {
305 return 0;
306 }
307
308 int
309 linux_sys_ioperm(l, v, retval)
310 struct lwp *l;
311 void *v;
312 register_t *retval;
313 {
314 return 0;
315 }
316
317 dev_t
318 linux_fakedev(dev, raw)
319 dev_t dev;
320 int raw;
321 {
322 return 0;
323 }
324
325 int
326 linux_machdepioctl(p, v, retval)
327 struct proc *p;
328 void *v;
329 register_t *retval;
330 {
331 return 0;
332 }
333
334 int
335 linux_sys_rt_sigreturn(l, v, retval)
336 struct lwp *l;
337 void *v;
338 register_t *retval;
339 {
340 struct linux_sys_rt_sigreturn_args /* {
341 syscallarg(struct linux_ucontext *) ucp;
342 } */ *uap = v;
343 struct linux_ucontext luctx;
344 struct trapframe *tf = l->l_md.md_regs;
345 struct linux_sigcontext *lsigctx;
346 struct linux__fpstate fpstate;
347 ucontext_t uctx;
348 mcontext_t *mctx;
349 struct fxsave64 *fxsave;
350 int error;
351
352 if ((error = copyin(SCARG(uap, ucp), &luctx, sizeof(luctx))) != 0) {
353 sigexit(l, SIGILL);
354 return error;
355 }
356 lsigctx = &luctx.luc_mcontext;
357
358 bzero(&uctx, sizeof(uctx));
359 mctx = (mcontext_t *)&uctx.uc_mcontext;
360 fxsave = (struct fxsave64 *)&mctx->__fpregs;
361
362 /*
363 * Set the flags. Linux always have CPU, stack and signal state,
364 * FPU is optional. uc_flags is not used to tell what we have.
365 */
366 uctx.uc_flags = (_UC_SIGMASK|_UC_CPU|_UC_STACK|_UC_CLRSTACK);
367 if (lsigctx->fpstate != NULL)
368 uctx.uc_flags |= _UC_FPU;
369 uctx.uc_link = NULL;
370
371 /*
372 * Signal set
373 */
374 linux_to_native_sigset(&uctx.uc_sigmask, &luctx.luc_sigmask);
375
376 /*
377 * CPU state
378 */
379 mctx->__gregs[_REG_R8] = lsigctx->r8;
380 mctx->__gregs[_REG_R9] = lsigctx->r9;
381 mctx->__gregs[_REG_R10] = lsigctx->r10;
382 mctx->__gregs[_REG_R11] = lsigctx->r11;
383 mctx->__gregs[_REG_R12] = lsigctx->r12;
384 mctx->__gregs[_REG_R13] = lsigctx->r13;
385 mctx->__gregs[_REG_R14] = lsigctx->r14;
386 mctx->__gregs[_REG_R15] = lsigctx->r15;
387 mctx->__gregs[_REG_RDI] = lsigctx->rdi;
388 mctx->__gregs[_REG_RSI] = lsigctx->rsi;
389 mctx->__gregs[_REG_RBP] = lsigctx->rbp;
390 mctx->__gregs[_REG_RBX] = lsigctx->rbx;
391 mctx->__gregs[_REG_RAX] = tf->tf_rax;
392 mctx->__gregs[_REG_RDX] = lsigctx->rdx;
393 mctx->__gregs[_REG_RCX] = lsigctx->rcx;
394 mctx->__gregs[_REG_RIP] = lsigctx->rip;
395 mctx->__gregs[_REG_RFL] = lsigctx->eflags;
396 mctx->__gregs[_REG_CS] = lsigctx->cs;
397 mctx->__gregs[_REG_GS] = lsigctx->gs;
398 mctx->__gregs[_REG_FS] = lsigctx->fs;
399 mctx->__gregs[_REG_ERR] = lsigctx->err;
400 mctx->__gregs[_REG_TRAPNO] = lsigctx->trapno;
401 mctx->__gregs[_REG_ES] = tf->tf_es;
402 mctx->__gregs[_REG_DS] = tf->tf_ds;
403 mctx->__gregs[_REG_URSP] = lsigctx->rsp; /* XXX */
404 mctx->__gregs[_REG_SS] = tf->tf_ss;
405
406 /*
407 * FPU state
408 */
409 if (lsigctx->fpstate != NULL) {
410 error = copyin(lsigctx->fpstate, &fpstate, sizeof(fpstate));
411 if (error != 0) {
412 sigexit(l, SIGILL);
413 return error;
414 }
415
416 fxsave->fx_fcw = fpstate.cwd;
417 fxsave->fx_fsw = fpstate.swd;
418 fxsave->fx_ftw = fpstate.twd;
419 fxsave->fx_fop = fpstate.fop;
420 fxsave->fx_rip = fpstate.rip;
421 fxsave->fx_rdp = fpstate.rdp;
422 fxsave->fx_mxcsr = fpstate.mxcsr;
423 fxsave->fx_mxcsr_mask = fpstate.mxcsr_mask;
424 memcpy(&fxsave->fx_st, &fpstate.st_space,
425 sizeof(fxsave->fx_st));
426 memcpy(&fxsave->fx_xmm, &fpstate.xmm_space,
427 sizeof(fxsave->fx_xmm));
428 }
429
430 /*
431 * And the stack
432 */
433 uctx.uc_stack.ss_flags = 0;
434 if (luctx.luc_stack.ss_flags & LINUX_SS_ONSTACK);
435 uctx.uc_stack.ss_flags = SS_ONSTACK;
436
437 if (luctx.luc_stack.ss_flags & LINUX_SS_DISABLE);
438 uctx.uc_stack.ss_flags = SS_DISABLE;
439
440 uctx.uc_stack.ss_sp = luctx.luc_stack.ss_sp;
441 uctx.uc_stack.ss_size = luctx.luc_stack.ss_size;
442
443 /*
444 * And let setucontext deal with that.
445 */
446 return setucontext(l, &uctx);
447 }
448
449 int
450 linux_sys_arch_prctl(l, v, retval)
451 struct lwp *l;
452 void *v;
453 register_t *retval;
454 {
455 struct linux_sys_arch_prctl_args /* {
456 syscallarg(int) code;
457 syscallarg(unsigned long) addr;
458 } */ *uap = v;
459 struct pcb *pcb = &l->l_addr->u_pcb;
460 struct trapframe *tf = l->l_md.md_regs;
461 int error;
462 uint64_t taddr;
463
464 switch(SCARG(uap, code)) {
465 case LINUX_ARCH_SET_GS:
466 taddr = SCARG(uap, addr);
467 if (taddr >= VM_MAXUSER_ADDRESS)
468 return EINVAL;
469 pcb->pcb_gs = taddr;
470 pcb->pcb_flags |= PCB_GS64;
471 if (l == curlwp)
472 wrmsr(MSR_KERNELGSBASE, taddr);
473 break;
474
475 case LINUX_ARCH_GET_GS:
476 if (pcb->pcb_flags & PCB_GS64)
477 taddr = pcb->pcb_gs;
478 else {
479 error = memseg_baseaddr(l, tf->tf_fs, NULL, 0, &taddr);
480 if (error != 0)
481 return error;
482 }
483 error = copyout(&taddr, (char *)SCARG(uap, addr), 8);
484 if (error != 0)
485 return error;
486 break;
487
488 case LINUX_ARCH_SET_FS:
489 taddr = SCARG(uap, addr);
490 if (taddr >= VM_MAXUSER_ADDRESS)
491 return EINVAL;
492 pcb->pcb_fs = taddr;
493 pcb->pcb_flags |= PCB_FS64;
494 if (l == curlwp)
495 wrmsr(MSR_FSBASE, taddr);
496 break;
497
498 case LINUX_ARCH_GET_FS:
499 if (pcb->pcb_flags & PCB_FS64)
500 taddr = pcb->pcb_fs;
501 else {
502 error = memseg_baseaddr(l, tf->tf_fs, NULL, 0, &taddr);
503 if (error != 0)
504 return error;
505 }
506 error = copyout(&taddr, (char *)SCARG(uap, addr), 8);
507 if (error != 0)
508 return error;
509 break;
510
511 default:
512 #ifdef DEBUG_LINUX
513 printf("linux_sys_arch_prctl: unexpected code %d\n",
514 SCARG(uap, code));
515 #endif
516 return EINVAL;
517 }
518
519 return 0;
520 }
521