linux_machdep.c revision 1.9.2.3 1 1.9.2.3 yamt /* $NetBSD: linux_machdep.c,v 1.9.2.3 2007/02/26 09:09:13 yamt Exp $ */
2 1.1 manu
3 1.1 manu /*-
4 1.1 manu * Copyright (c) 2005 Emmanuel Dreyfus, all rights reserved.
5 1.1 manu *
6 1.1 manu * Redistribution and use in source and binary forms, with or without
7 1.1 manu * modification, are permitted provided that the following conditions
8 1.1 manu * are met:
9 1.1 manu * 1. Redistributions of source code must retain the above copyright
10 1.1 manu * notice, this list of conditions and the following disclaimer.
11 1.1 manu * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 manu * notice, this list of conditions and the following disclaimer in the
13 1.1 manu * documentation and/or other materials provided with the distribution.
14 1.1 manu * 3. All advertising materials mentioning features or use of this software
15 1.1 manu * must display the following acknowledgement:
16 1.1 manu * This product includes software developed by Emmanuel Dreyfus
17 1.1 manu * 4. The name of the author may not be used to endorse or promote
18 1.1 manu * products derived from this software without specific prior written
19 1.1 manu * permission.
20 1.1 manu *
21 1.1 manu * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22 1.1 manu * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 1.1 manu * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1 manu * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25 1.1 manu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 manu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 manu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 manu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 manu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 manu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 manu * POSSIBILITY OF SUCH DAMAGE.
32 1.1 manu */
33 1.1 manu
34 1.1 manu #include <sys/cdefs.h>
35 1.1 manu
36 1.9.2.3 yamt __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.9.2.3 2007/02/26 09:09:13 yamt Exp $");
37 1.1 manu
38 1.1 manu #include <sys/param.h>
39 1.1 manu #include <sys/types.h>
40 1.1 manu #include <sys/systm.h>
41 1.1 manu #include <sys/signal.h>
42 1.1 manu #include <sys/exec.h>
43 1.1 manu #include <sys/proc.h>
44 1.1 manu #include <sys/ptrace.h> /* for process_read_fpregs() */
45 1.1 manu #include <sys/user.h>
46 1.9.2.1 yamt #include <sys/wait.h>
47 1.1 manu #include <sys/ucontext.h>
48 1.1 manu
49 1.1 manu #include <machine/reg.h>
50 1.1 manu #include <machine/pcb.h>
51 1.1 manu #include <machine/fpu.h>
52 1.1 manu #include <machine/mcontext.h>
53 1.2 fvdl #include <machine/specialreg.h>
54 1.2 fvdl #include <machine/vmparam.h>
55 1.1 manu
56 1.1 manu #include <compat/linux/common/linux_signal.h>
57 1.1 manu #include <compat/linux/common/linux_errno.h>
58 1.1 manu #include <compat/linux/common/linux_exec.h>
59 1.1 manu #include <compat/linux/common/linux_ioctl.h>
60 1.1 manu #include <compat/linux/common/linux_prctl.h>
61 1.1 manu #include <compat/linux/common/linux_machdep.h>
62 1.4 fvdl #include <compat/linux/linux_syscall.h>
63 1.1 manu #include <compat/linux/linux_syscallargs.h>
64 1.1 manu
65 1.5 fvdl static void linux_buildcontext(struct lwp *, void *, void *);
66 1.5 fvdl
67 1.1 manu void
68 1.1 manu linux_setregs(l, epp, stack)
69 1.1 manu struct lwp *l;
70 1.1 manu struct exec_package *epp;
71 1.1 manu u_long stack;
72 1.1 manu {
73 1.1 manu struct pcb *pcb = &l->l_addr->u_pcb;
74 1.1 manu struct trapframe *tf;
75 1.1 manu
76 1.1 manu /* If we were using the FPU, forget about it. */
77 1.1 manu if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
78 1.1 manu fpusave_lwp(l, 0);
79 1.1 manu
80 1.1 manu l->l_md.md_flags &= ~MDP_USEDFPU;
81 1.1 manu pcb->pcb_flags = 0;
82 1.1 manu pcb->pcb_savefpu.fp_fxsave.fx_fcw = __NetBSD_NPXCW__;
83 1.1 manu pcb->pcb_savefpu.fp_fxsave.fx_mxcsr = __INITIAL_MXCSR__;
84 1.1 manu pcb->pcb_savefpu.fp_fxsave.fx_mxcsr_mask = __INITIAL_MXCSR_MASK__;
85 1.4 fvdl pcb->pcb_fs = 0;
86 1.4 fvdl pcb->pcb_gs = 0;
87 1.1 manu
88 1.9.2.3 yamt l->l_proc->p_flag &= ~PK_32;
89 1.1 manu
90 1.1 manu tf = l->l_md.md_regs;
91 1.1 manu tf->tf_rax = 0;
92 1.1 manu tf->tf_rbx = 0;
93 1.1 manu tf->tf_rcx = epp->ep_entry;
94 1.1 manu tf->tf_rdx = 0;
95 1.1 manu tf->tf_rsi = 0;
96 1.1 manu tf->tf_rdi = 0;
97 1.1 manu tf->tf_rbp = 0;
98 1.1 manu tf->tf_rsp = stack;
99 1.1 manu tf->tf_r8 = 0;
100 1.1 manu tf->tf_r9 = 0;
101 1.1 manu tf->tf_r10 = 0;
102 1.1 manu tf->tf_r11 = 0;
103 1.1 manu tf->tf_r12 = 0;
104 1.1 manu tf->tf_r13 = 0;
105 1.1 manu tf->tf_r14 = 0;
106 1.1 manu tf->tf_r15 = 0;
107 1.1 manu tf->tf_rip = epp->ep_entry;
108 1.4 fvdl tf->tf_rflags = PSL_USERSET;
109 1.4 fvdl tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
110 1.4 fvdl tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
111 1.1 manu tf->tf_ds = 0;
112 1.1 manu tf->tf_es = 0;
113 1.1 manu tf->tf_fs = 0;
114 1.1 manu tf->tf_gs = 0;
115 1.1 manu
116 1.1 manu return;
117 1.1 manu }
118 1.1 manu
119 1.1 manu void
120 1.1 manu linux_sendsig(ksi, mask)
121 1.1 manu const ksiginfo_t *ksi;
122 1.1 manu const sigset_t *mask;
123 1.1 manu {
124 1.1 manu struct lwp *l = curlwp;
125 1.1 manu struct proc *p = l->l_proc;
126 1.1 manu struct sigacts *ps = p->p_sigacts;
127 1.9.2.3 yamt int onstack, error;
128 1.1 manu int sig = ksi->ksi_signo;
129 1.1 manu struct linux_rt_sigframe *sfp, sigframe;
130 1.1 manu struct linux__fpstate *fpsp, fpstate;
131 1.1 manu struct fpreg fpregs;
132 1.1 manu struct trapframe *tf = l->l_md.md_regs;
133 1.1 manu sig_t catcher = SIGACTION(p, sig).sa_handler;
134 1.1 manu linux_sigset_t lmask;
135 1.1 manu char *sp;
136 1.4 fvdl
137 1.1 manu /* Do we need to jump onto the signal stack? */
138 1.1 manu onstack =
139 1.9.2.3 yamt (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
140 1.1 manu (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
141 1.1 manu
142 1.1 manu /* Allocate space for the signal handler context. */
143 1.1 manu if (onstack)
144 1.9.2.3 yamt sp = ((caddr_t)l->l_sigstk.ss_sp +
145 1.9.2.3 yamt l->l_sigstk.ss_size);
146 1.1 manu else
147 1.1 manu sp = (caddr_t)tf->tf_rsp - 128;
148 1.1 manu
149 1.1 manu /*
150 1.1 manu * Save FPU state, if any
151 1.1 manu */
152 1.1 manu if (l->l_md.md_flags & MDP_USEDFPU) {
153 1.1 manu sp = (char *)
154 1.1 manu (((long)sp - sizeof(struct linux__fpstate)) & ~0xfUL);
155 1.1 manu fpsp = (struct linux__fpstate *)sp;
156 1.9.2.3 yamt } else
157 1.1 manu fpsp = NULL;
158 1.1 manu
159 1.1 manu /*
160 1.1 manu * Populate the rt_sigframe
161 1.1 manu */
162 1.1 manu sp = (char *)
163 1.1 manu ((((long)sp - sizeof(struct linux_rt_sigframe)) & ~0xfUL) - 8);
164 1.1 manu sfp = (struct linux_rt_sigframe *)sp;
165 1.1 manu
166 1.1 manu bzero(&sigframe, sizeof(sigframe));
167 1.3 manu if (ps->sa_sigdesc[sig].sd_vers != 0)
168 1.7 manu sigframe.pretcode =
169 1.7 manu (char *)(u_long)ps->sa_sigdesc[sig].sd_tramp;
170 1.3 manu else
171 1.3 manu sigframe.pretcode = NULL;
172 1.1 manu
173 1.1 manu /*
174 1.1 manu * The user context
175 1.1 manu */
176 1.1 manu sigframe.uc.luc_flags = 0;
177 1.1 manu sigframe.uc.luc_link = NULL;
178 1.1 manu
179 1.1 manu /* This is used regardless of SA_ONSTACK in Linux */
180 1.9.2.3 yamt sigframe.uc.luc_stack.ss_sp = l->l_sigstk.ss_sp;
181 1.9.2.3 yamt sigframe.uc.luc_stack.ss_size = l->l_sigstk.ss_size;
182 1.1 manu sigframe.uc.luc_stack.ss_flags = 0;
183 1.9.2.3 yamt if (l->l_sigstk.ss_flags & SS_ONSTACK)
184 1.1 manu sigframe.uc.luc_stack.ss_flags |= LINUX_SS_ONSTACK;
185 1.9.2.3 yamt if (l->l_sigstk.ss_flags & SS_DISABLE)
186 1.1 manu sigframe.uc.luc_stack.ss_flags |= LINUX_SS_DISABLE;
187 1.1 manu
188 1.1 manu sigframe.uc.luc_mcontext.r8 = tf->tf_r8;
189 1.1 manu sigframe.uc.luc_mcontext.r9 = tf->tf_r9;
190 1.1 manu sigframe.uc.luc_mcontext.r10 = tf->tf_r10;
191 1.1 manu sigframe.uc.luc_mcontext.r11 = tf->tf_r11;
192 1.1 manu sigframe.uc.luc_mcontext.r12 = tf->tf_r12;
193 1.1 manu sigframe.uc.luc_mcontext.r13 = tf->tf_r13;
194 1.1 manu sigframe.uc.luc_mcontext.r14 = tf->tf_r14;
195 1.1 manu sigframe.uc.luc_mcontext.r15 = tf->tf_r15;
196 1.1 manu sigframe.uc.luc_mcontext.rdi = tf->tf_rdi;
197 1.1 manu sigframe.uc.luc_mcontext.rsi = tf->tf_rsi;
198 1.1 manu sigframe.uc.luc_mcontext.rbp = tf->tf_rbp;
199 1.1 manu sigframe.uc.luc_mcontext.rbx = tf->tf_rbx;
200 1.1 manu sigframe.uc.luc_mcontext.rdx = tf->tf_rdx;
201 1.1 manu sigframe.uc.luc_mcontext.rcx = tf->tf_rcx;
202 1.1 manu sigframe.uc.luc_mcontext.rsp = tf->tf_rsp;
203 1.6 fvdl sigframe.uc.luc_mcontext.rip = tf->tf_rip;
204 1.1 manu sigframe.uc.luc_mcontext.eflags = tf->tf_rflags;
205 1.1 manu sigframe.uc.luc_mcontext.cs = tf->tf_cs;
206 1.1 manu sigframe.uc.luc_mcontext.gs = tf->tf_gs;
207 1.1 manu sigframe.uc.luc_mcontext.fs = tf->tf_fs;
208 1.1 manu sigframe.uc.luc_mcontext.err = tf->tf_err;
209 1.1 manu sigframe.uc.luc_mcontext.trapno = tf->tf_trapno;
210 1.1 manu native_to_linux_sigset(&lmask, mask);
211 1.1 manu sigframe.uc.luc_mcontext.oldmask = lmask.sig[0];
212 1.1 manu sigframe.uc.luc_mcontext.cr2 = (long)l->l_addr->u_pcb.pcb_onfault;
213 1.1 manu sigframe.uc.luc_mcontext.fpstate = fpsp;
214 1.1 manu native_to_linux_sigset(&sigframe.uc.luc_sigmask, mask);
215 1.1 manu
216 1.1 manu /*
217 1.1 manu * the siginfo structure
218 1.1 manu */
219 1.1 manu sigframe.info.lsi_signo = native_to_linux_signo[sig];
220 1.1 manu sigframe.info.lsi_errno = native_to_linux_errno[ksi->ksi_errno];
221 1.1 manu sigframe.info.lsi_code = ksi->ksi_code;
222 1.1 manu
223 1.1 manu /* XXX This is a rought conversion, taken from i386 code */
224 1.1 manu switch (sigframe.info.lsi_signo) {
225 1.1 manu case LINUX_SIGILL:
226 1.1 manu case LINUX_SIGFPE:
227 1.1 manu case LINUX_SIGSEGV:
228 1.1 manu case LINUX_SIGBUS:
229 1.1 manu case LINUX_SIGTRAP:
230 1.1 manu sigframe.info._sifields._sigfault._addr = ksi->ksi_addr;
231 1.1 manu break;
232 1.1 manu case LINUX_SIGCHLD:
233 1.1 manu sigframe.info._sifields._sigchld._pid = ksi->ksi_pid;
234 1.1 manu sigframe.info._sifields._sigchld._uid = ksi->ksi_uid;
235 1.1 manu sigframe.info._sifields._sigchld._utime = ksi->ksi_utime;
236 1.1 manu sigframe.info._sifields._sigchld._stime = ksi->ksi_stime;
237 1.9 manu
238 1.9 manu if (WCOREDUMP(ksi->ksi_status)) {
239 1.9 manu sigframe.info.lsi_code = LINUX_CLD_DUMPED;
240 1.9 manu sigframe.info._sifields._sigchld._status =
241 1.9 manu _WSTATUS(ksi->ksi_status);
242 1.9 manu } else if (_WSTATUS(ksi->ksi_status)) {
243 1.9 manu sigframe.info.lsi_code = LINUX_CLD_KILLED;
244 1.9 manu sigframe.info._sifields._sigchld._status =
245 1.9 manu _WSTATUS(ksi->ksi_status);
246 1.9 manu } else {
247 1.9 manu sigframe.info.lsi_code = LINUX_CLD_EXITED;
248 1.9 manu sigframe.info._sifields._sigchld._status =
249 1.9 manu ((ksi->ksi_status & 0xff00U) >> 8);
250 1.9 manu }
251 1.1 manu break;
252 1.1 manu case LINUX_SIGIO:
253 1.1 manu sigframe.info._sifields._sigpoll._band = ksi->ksi_band;
254 1.1 manu sigframe.info._sifields._sigpoll._fd = ksi->ksi_fd;
255 1.1 manu break;
256 1.1 manu default:
257 1.1 manu sigframe.info._sifields._sigchld._pid = ksi->ksi_pid;
258 1.1 manu sigframe.info._sifields._sigchld._uid = ksi->ksi_uid;
259 1.1 manu if ((sigframe.info.lsi_signo == LINUX_SIGALRM) ||
260 1.1 manu (sigframe.info.lsi_signo >= LINUX_SIGRTMIN))
261 1.1 manu sigframe.info._sifields._timer._sigval.sival_ptr =
262 1.1 manu ksi->ksi_sigval.sival_ptr;
263 1.1 manu break;
264 1.1 manu }
265 1.1 manu
266 1.9.2.3 yamt sendsig_reset(l, sig);
267 1.9.2.3 yamt mutex_exit(&p->p_smutex);
268 1.9.2.3 yamt error = 0;
269 1.9.2.3 yamt
270 1.9.2.3 yamt /*
271 1.9.2.3 yamt * Save FPU state, if any
272 1.9.2.3 yamt */
273 1.9.2.3 yamt if (fpsp != NULL) {
274 1.9.2.3 yamt (void)process_read_fpregs(l, &fpregs);
275 1.9.2.3 yamt bzero(&fpstate, sizeof(fpstate));
276 1.9.2.3 yamt fpstate.cwd = fpregs.fp_fcw;
277 1.9.2.3 yamt fpstate.swd = fpregs.fp_fsw;
278 1.9.2.3 yamt fpstate.twd = fpregs.fp_ftw;
279 1.9.2.3 yamt fpstate.fop = fpregs.fp_fop;
280 1.9.2.3 yamt fpstate.rip = fpregs.fp_rip;
281 1.9.2.3 yamt fpstate.rdp = fpregs.fp_rdp;
282 1.9.2.3 yamt fpstate.mxcsr = fpregs.fp_mxcsr;
283 1.9.2.3 yamt fpstate.mxcsr_mask = fpregs.fp_mxcsr_mask;
284 1.9.2.3 yamt memcpy(&fpstate.st_space, &fpregs.fp_st,
285 1.9.2.3 yamt sizeof(fpstate.st_space));
286 1.9.2.3 yamt memcpy(&fpstate.xmm_space, &fpregs.fp_xmm,
287 1.9.2.3 yamt sizeof(fpstate.xmm_space));
288 1.9.2.3 yamt error = copyout(&fpstate, fpsp, sizeof(fpstate));
289 1.9.2.3 yamt }
290 1.9.2.3 yamt
291 1.9.2.3 yamt if (error == 0)
292 1.9.2.3 yamt error = copyout(&sigframe, sp, sizeof(sigframe));
293 1.9.2.3 yamt
294 1.9.2.3 yamt mutex_enter(&p->p_smutex);
295 1.9.2.3 yamt
296 1.9.2.3 yamt if (error != 0) {
297 1.1 manu sigexit(l, SIGILL);
298 1.1 manu return;
299 1.1 manu }
300 1.1 manu
301 1.6 fvdl linux_buildcontext(l, catcher, sp);
302 1.1 manu tf->tf_rdi = sigframe.info.lsi_signo;
303 1.1 manu tf->tf_rax = 0;
304 1.1 manu tf->tf_rsi = (long)&sfp->info;
305 1.1 manu tf->tf_rdx = (long)&sfp->uc;
306 1.1 manu
307 1.1 manu /*
308 1.1 manu * Remember we use signal stack
309 1.1 manu */
310 1.1 manu if (onstack)
311 1.9.2.3 yamt l->l_sigstk.ss_flags |= SS_ONSTACK;
312 1.1 manu return;
313 1.1 manu }
314 1.1 manu
315 1.1 manu int
316 1.1 manu linux_sys_modify_ldt(l, v, retval)
317 1.1 manu struct lwp *l;
318 1.1 manu void *v;
319 1.1 manu register_t *retval;
320 1.1 manu {
321 1.9.2.2 yamt printf("linux_sys_modify_ldt\n");
322 1.1 manu return 0;
323 1.1 manu }
324 1.1 manu
325 1.1 manu int
326 1.1 manu linux_sys_iopl(l, v, retval)
327 1.1 manu struct lwp *l;
328 1.1 manu void *v;
329 1.1 manu register_t *retval;
330 1.1 manu {
331 1.1 manu return 0;
332 1.1 manu }
333 1.1 manu
334 1.1 manu int
335 1.1 manu linux_sys_ioperm(l, v, retval)
336 1.1 manu struct lwp *l;
337 1.1 manu void *v;
338 1.1 manu register_t *retval;
339 1.1 manu {
340 1.1 manu return 0;
341 1.1 manu }
342 1.1 manu
343 1.1 manu dev_t
344 1.1 manu linux_fakedev(dev, raw)
345 1.1 manu dev_t dev;
346 1.1 manu int raw;
347 1.1 manu {
348 1.9.2.2 yamt return ((minor(dev) & 0xff) | ((major(dev) & 0xfff) << 8)
349 1.9.2.2 yamt | (((unsigned long long int) (minor(dev) & ~0xff)) << 12)
350 1.9.2.2 yamt | (((unsigned long long int) (major(dev) & ~0xfff)) << 32));
351 1.1 manu }
352 1.1 manu
353 1.1 manu int
354 1.9.2.1 yamt linux_machdepioctl(l, v, retval)
355 1.9.2.1 yamt struct lwp *l;
356 1.1 manu void *v;
357 1.1 manu register_t *retval;
358 1.1 manu {
359 1.1 manu return 0;
360 1.1 manu }
361 1.1 manu
362 1.1 manu int
363 1.1 manu linux_sys_rt_sigreturn(l, v, retval)
364 1.1 manu struct lwp *l;
365 1.1 manu void *v;
366 1.1 manu register_t *retval;
367 1.1 manu {
368 1.6 fvdl struct linux_ucontext *luctx;
369 1.1 manu struct trapframe *tf = l->l_md.md_regs;
370 1.1 manu struct linux_sigcontext *lsigctx;
371 1.1 manu struct linux__fpstate fpstate;
372 1.6 fvdl struct linux_rt_sigframe frame, *fp;
373 1.1 manu ucontext_t uctx;
374 1.1 manu mcontext_t *mctx;
375 1.1 manu struct fxsave64 *fxsave;
376 1.1 manu int error;
377 1.1 manu
378 1.6 fvdl fp = (struct linux_rt_sigframe *)(tf->tf_rsp - 8);
379 1.6 fvdl if ((error = copyin(fp, &frame, sizeof(frame))) != 0) {
380 1.9.2.3 yamt mutex_enter(&l->l_proc->p_smutex);
381 1.1 manu sigexit(l, SIGILL);
382 1.1 manu return error;
383 1.1 manu }
384 1.6 fvdl luctx = &frame.uc;
385 1.6 fvdl lsigctx = &luctx->luc_mcontext;
386 1.1 manu
387 1.1 manu bzero(&uctx, sizeof(uctx));
388 1.1 manu mctx = (mcontext_t *)&uctx.uc_mcontext;
389 1.1 manu fxsave = (struct fxsave64 *)&mctx->__fpregs;
390 1.1 manu
391 1.1 manu /*
392 1.1 manu * Set the flags. Linux always have CPU, stack and signal state,
393 1.1 manu * FPU is optional. uc_flags is not used to tell what we have.
394 1.1 manu */
395 1.1 manu uctx.uc_flags = (_UC_SIGMASK|_UC_CPU|_UC_STACK|_UC_CLRSTACK);
396 1.1 manu if (lsigctx->fpstate != NULL)
397 1.1 manu uctx.uc_flags |= _UC_FPU;
398 1.1 manu uctx.uc_link = NULL;
399 1.1 manu
400 1.1 manu /*
401 1.1 manu * Signal set
402 1.1 manu */
403 1.6 fvdl linux_to_native_sigset(&uctx.uc_sigmask, &luctx->luc_sigmask);
404 1.1 manu
405 1.1 manu /*
406 1.1 manu * CPU state
407 1.1 manu */
408 1.1 manu mctx->__gregs[_REG_R8] = lsigctx->r8;
409 1.1 manu mctx->__gregs[_REG_R9] = lsigctx->r9;
410 1.1 manu mctx->__gregs[_REG_R10] = lsigctx->r10;
411 1.1 manu mctx->__gregs[_REG_R11] = lsigctx->r11;
412 1.1 manu mctx->__gregs[_REG_R12] = lsigctx->r12;
413 1.1 manu mctx->__gregs[_REG_R13] = lsigctx->r13;
414 1.1 manu mctx->__gregs[_REG_R14] = lsigctx->r14;
415 1.1 manu mctx->__gregs[_REG_R15] = lsigctx->r15;
416 1.1 manu mctx->__gregs[_REG_RDI] = lsigctx->rdi;
417 1.1 manu mctx->__gregs[_REG_RSI] = lsigctx->rsi;
418 1.1 manu mctx->__gregs[_REG_RBP] = lsigctx->rbp;
419 1.1 manu mctx->__gregs[_REG_RBX] = lsigctx->rbx;
420 1.1 manu mctx->__gregs[_REG_RAX] = tf->tf_rax;
421 1.1 manu mctx->__gregs[_REG_RDX] = lsigctx->rdx;
422 1.1 manu mctx->__gregs[_REG_RCX] = lsigctx->rcx;
423 1.1 manu mctx->__gregs[_REG_RIP] = lsigctx->rip;
424 1.1 manu mctx->__gregs[_REG_RFL] = lsigctx->eflags;
425 1.1 manu mctx->__gregs[_REG_CS] = lsigctx->cs;
426 1.1 manu mctx->__gregs[_REG_GS] = lsigctx->gs;
427 1.1 manu mctx->__gregs[_REG_FS] = lsigctx->fs;
428 1.1 manu mctx->__gregs[_REG_ERR] = lsigctx->err;
429 1.1 manu mctx->__gregs[_REG_TRAPNO] = lsigctx->trapno;
430 1.1 manu mctx->__gregs[_REG_ES] = tf->tf_es;
431 1.1 manu mctx->__gregs[_REG_DS] = tf->tf_ds;
432 1.1 manu mctx->__gregs[_REG_URSP] = lsigctx->rsp; /* XXX */
433 1.1 manu mctx->__gregs[_REG_SS] = tf->tf_ss;
434 1.1 manu
435 1.1 manu /*
436 1.1 manu * FPU state
437 1.1 manu */
438 1.1 manu if (lsigctx->fpstate != NULL) {
439 1.1 manu error = copyin(lsigctx->fpstate, &fpstate, sizeof(fpstate));
440 1.1 manu if (error != 0) {
441 1.9.2.3 yamt mutex_enter(&l->l_proc->p_smutex);
442 1.1 manu sigexit(l, SIGILL);
443 1.1 manu return error;
444 1.1 manu }
445 1.1 manu
446 1.1 manu fxsave->fx_fcw = fpstate.cwd;
447 1.1 manu fxsave->fx_fsw = fpstate.swd;
448 1.1 manu fxsave->fx_ftw = fpstate.twd;
449 1.1 manu fxsave->fx_fop = fpstate.fop;
450 1.1 manu fxsave->fx_rip = fpstate.rip;
451 1.1 manu fxsave->fx_rdp = fpstate.rdp;
452 1.1 manu fxsave->fx_mxcsr = fpstate.mxcsr;
453 1.1 manu fxsave->fx_mxcsr_mask = fpstate.mxcsr_mask;
454 1.1 manu memcpy(&fxsave->fx_st, &fpstate.st_space,
455 1.1 manu sizeof(fxsave->fx_st));
456 1.1 manu memcpy(&fxsave->fx_xmm, &fpstate.xmm_space,
457 1.1 manu sizeof(fxsave->fx_xmm));
458 1.1 manu }
459 1.1 manu
460 1.1 manu /*
461 1.1 manu * And the stack
462 1.1 manu */
463 1.1 manu uctx.uc_stack.ss_flags = 0;
464 1.6 fvdl if (luctx->luc_stack.ss_flags & LINUX_SS_ONSTACK);
465 1.1 manu uctx.uc_stack.ss_flags = SS_ONSTACK;
466 1.1 manu
467 1.6 fvdl if (luctx->luc_stack.ss_flags & LINUX_SS_DISABLE);
468 1.1 manu uctx.uc_stack.ss_flags = SS_DISABLE;
469 1.1 manu
470 1.6 fvdl uctx.uc_stack.ss_sp = luctx->luc_stack.ss_sp;
471 1.6 fvdl uctx.uc_stack.ss_size = luctx->luc_stack.ss_size;
472 1.1 manu
473 1.1 manu /*
474 1.1 manu * And let setucontext deal with that.
475 1.1 manu */
476 1.9.2.3 yamt mutex_enter(&l->l_proc->p_smutex);
477 1.9.2.3 yamt error = setucontext(l, &uctx);
478 1.9.2.3 yamt mutex_exit(&l->l_proc->p_smutex);
479 1.9.2.3 yamt
480 1.9.2.3 yamt return error;
481 1.1 manu }
482 1.1 manu
483 1.1 manu int
484 1.1 manu linux_sys_arch_prctl(l, v, retval)
485 1.1 manu struct lwp *l;
486 1.1 manu void *v;
487 1.1 manu register_t *retval;
488 1.1 manu {
489 1.1 manu struct linux_sys_arch_prctl_args /* {
490 1.1 manu syscallarg(int) code;
491 1.1 manu syscallarg(unsigned long) addr;
492 1.1 manu } */ *uap = v;
493 1.2 fvdl struct pcb *pcb = &l->l_addr->u_pcb;
494 1.2 fvdl struct trapframe *tf = l->l_md.md_regs;
495 1.1 manu int error;
496 1.2 fvdl uint64_t taddr;
497 1.1 manu
498 1.1 manu switch(SCARG(uap, code)) {
499 1.1 manu case LINUX_ARCH_SET_GS:
500 1.2 fvdl taddr = SCARG(uap, addr);
501 1.2 fvdl if (taddr >= VM_MAXUSER_ADDRESS)
502 1.2 fvdl return EINVAL;
503 1.2 fvdl pcb->pcb_gs = taddr;
504 1.2 fvdl pcb->pcb_flags |= PCB_GS64;
505 1.2 fvdl if (l == curlwp)
506 1.2 fvdl wrmsr(MSR_KERNELGSBASE, taddr);
507 1.1 manu break;
508 1.1 manu
509 1.1 manu case LINUX_ARCH_GET_GS:
510 1.2 fvdl if (pcb->pcb_flags & PCB_GS64)
511 1.2 fvdl taddr = pcb->pcb_gs;
512 1.2 fvdl else {
513 1.2 fvdl error = memseg_baseaddr(l, tf->tf_fs, NULL, 0, &taddr);
514 1.2 fvdl if (error != 0)
515 1.2 fvdl return error;
516 1.2 fvdl }
517 1.2 fvdl error = copyout(&taddr, (char *)SCARG(uap, addr), 8);
518 1.2 fvdl if (error != 0)
519 1.1 manu return error;
520 1.1 manu break;
521 1.1 manu
522 1.1 manu case LINUX_ARCH_SET_FS:
523 1.2 fvdl taddr = SCARG(uap, addr);
524 1.2 fvdl if (taddr >= VM_MAXUSER_ADDRESS)
525 1.2 fvdl return EINVAL;
526 1.2 fvdl pcb->pcb_fs = taddr;
527 1.2 fvdl pcb->pcb_flags |= PCB_FS64;
528 1.2 fvdl if (l == curlwp)
529 1.2 fvdl wrmsr(MSR_FSBASE, taddr);
530 1.1 manu break;
531 1.1 manu
532 1.1 manu case LINUX_ARCH_GET_FS:
533 1.2 fvdl if (pcb->pcb_flags & PCB_FS64)
534 1.2 fvdl taddr = pcb->pcb_fs;
535 1.2 fvdl else {
536 1.2 fvdl error = memseg_baseaddr(l, tf->tf_fs, NULL, 0, &taddr);
537 1.2 fvdl if (error != 0)
538 1.2 fvdl return error;
539 1.2 fvdl }
540 1.2 fvdl error = copyout(&taddr, (char *)SCARG(uap, addr), 8);
541 1.2 fvdl if (error != 0)
542 1.1 manu return error;
543 1.1 manu break;
544 1.1 manu
545 1.1 manu default:
546 1.1 manu #ifdef DEBUG_LINUX
547 1.1 manu printf("linux_sys_arch_prctl: unexpected code %d\n",
548 1.1 manu SCARG(uap, code));
549 1.1 manu #endif
550 1.1 manu return EINVAL;
551 1.1 manu }
552 1.1 manu
553 1.1 manu return 0;
554 1.1 manu }
555 1.4 fvdl
556 1.4 fvdl const int linux_vsyscall_to_syscall[] = {
557 1.4 fvdl LINUX_SYS_gettimeofday,
558 1.4 fvdl LINUX_SYS_time,
559 1.4 fvdl LINUX_SYS_nosys,
560 1.4 fvdl LINUX_SYS_nosys,
561 1.4 fvdl };
562 1.4 fvdl
563 1.4 fvdl int
564 1.4 fvdl linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
565 1.4 fvdl {
566 1.4 fvdl struct trapframe *tf = arg;
567 1.4 fvdl uint64_t retaddr;
568 1.4 fvdl int vsyscallnr;
569 1.4 fvdl
570 1.4 fvdl /*
571 1.4 fvdl * Check for a vsyscall. %rip must be the fault address,
572 1.4 fvdl * and the address must be in the Linux vsyscall area.
573 1.4 fvdl * Also, vsyscalls are only done at 1024-byte boundaries.
574 1.4 fvdl */
575 1.4 fvdl
576 1.4 fvdl if (__predict_true(trapaddr < LINUX_VSYSCALL_START))
577 1.4 fvdl return 0;
578 1.4 fvdl
579 1.4 fvdl if (trapaddr != tf->tf_rip)
580 1.4 fvdl return 0;
581 1.4 fvdl
582 1.4 fvdl if ((tf->tf_rip & (LINUX_VSYSCALL_SIZE - 1)) != 0)
583 1.4 fvdl return 0;
584 1.4 fvdl
585 1.4 fvdl vsyscallnr = (tf->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SIZE;
586 1.4 fvdl
587 1.4 fvdl if (vsyscallnr > LINUX_VSYSCALL_MAXNR)
588 1.4 fvdl return 0;
589 1.4 fvdl
590 1.4 fvdl /*
591 1.4 fvdl * Get the return address from the top of the stack,
592 1.4 fvdl * and fix up the return address.
593 1.4 fvdl * This assumes the faulting instruction was callq *reg,
594 1.4 fvdl * which is the only way that vsyscalls are ever entered.
595 1.4 fvdl */
596 1.4 fvdl if (copyin((void *)tf->tf_rsp, &retaddr, sizeof retaddr) != 0)
597 1.4 fvdl return 0;
598 1.4 fvdl tf->tf_rip = retaddr;
599 1.4 fvdl tf->tf_rax = linux_vsyscall_to_syscall[vsyscallnr];
600 1.4 fvdl tf->tf_rsp += 8; /* "pop" the return address */
601 1.4 fvdl
602 1.4 fvdl #if 0
603 1.4 fvdl printf("usertrap: rip %p rsp %p retaddr %p vsys %d sys %d\n",
604 1.4 fvdl (void *)tf->tf_rip, (void *)tf->tf_rsp, (void *)retaddr,
605 1.4 fvdl vsyscallnr, (int)tf->tf_rax);
606 1.4 fvdl #endif
607 1.4 fvdl
608 1.4 fvdl (*l->l_proc->p_md.md_syscall)(tf);
609 1.4 fvdl
610 1.4 fvdl return 1;
611 1.4 fvdl }
612 1.5 fvdl
613 1.5 fvdl static void
614 1.5 fvdl linux_buildcontext(struct lwp *l, void *catcher, void *f)
615 1.5 fvdl {
616 1.5 fvdl struct trapframe *tf = l->l_md.md_regs;
617 1.5 fvdl
618 1.5 fvdl tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
619 1.5 fvdl tf->tf_rip = (u_int64_t)catcher;
620 1.5 fvdl tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
621 1.5 fvdl tf->tf_rflags &= ~(PSL_T|PSL_VM|PSL_AC);
622 1.5 fvdl tf->tf_rsp = (u_int64_t)f;
623 1.5 fvdl tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
624 1.5 fvdl }
625 1.9.2.1 yamt
626 1.9.2.2 yamt void *
627 1.9.2.1 yamt linux_get_newtls(l)
628 1.9.2.1 yamt struct lwp *l;
629 1.9.2.1 yamt {
630 1.9.2.1 yamt struct trapframe *tf = l->l_md.md_regs;
631 1.9.2.1 yamt
632 1.9.2.2 yamt return (void *)tf->tf_r8;
633 1.9.2.1 yamt }
634 1.9.2.1 yamt
635 1.9.2.1 yamt int
636 1.9.2.1 yamt linux_set_newtls(l, tls)
637 1.9.2.1 yamt struct lwp *l;
638 1.9.2.2 yamt void *tls;
639 1.9.2.1 yamt {
640 1.9.2.1 yamt struct linux_sys_arch_prctl_args cup;
641 1.9.2.1 yamt register_t retval;
642 1.9.2.1 yamt
643 1.9.2.1 yamt SCARG(&cup, code) = LINUX_ARCH_SET_FS;
644 1.9.2.2 yamt SCARG(&cup, addr) = (unsigned long)tls;
645 1.9.2.1 yamt
646 1.9.2.1 yamt return linux_sys_arch_prctl(l, &cup, &retval);
647 1.9.2.1 yamt }
648