linux32_machdep.c revision 1.1.2.2 1 1.1.2.2 yamt /* $NetBSD: linux32_machdep.c,v 1.1.2.2 2006/02/18 15:38:59 yamt Exp $ */
2 1.1.2.2 yamt
3 1.1.2.2 yamt /*-
4 1.1.2.2 yamt * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
5 1.1.2.2 yamt *
6 1.1.2.2 yamt * Redistribution and use in source and binary forms, with or without
7 1.1.2.2 yamt * modification, are permitted provided that the following conditions
8 1.1.2.2 yamt * are met:
9 1.1.2.2 yamt * 1. Redistributions of source code must retain the above copyright
10 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer.
11 1.1.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer in the
13 1.1.2.2 yamt * documentation and/or other materials provided with the distribution.
14 1.1.2.2 yamt * 3. All advertising materials mentioning features or use of this software
15 1.1.2.2 yamt * must display the following acknowledgement:
16 1.1.2.2 yamt * This product includes software developed by Emmanuel Dreyfus
17 1.1.2.2 yamt * 4. The name of the author may not be used to endorse or promote
18 1.1.2.2 yamt * products derived from this software without specific prior written
19 1.1.2.2 yamt * permission.
20 1.1.2.2 yamt *
21 1.1.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22 1.1.2.2 yamt * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 1.1.2.2 yamt * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1.2.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25 1.1.2.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1.2.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1.2.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1.2.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1.2.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1.2.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1.2.2 yamt * POSSIBILITY OF SUCH DAMAGE.
32 1.1.2.2 yamt */
33 1.1.2.2 yamt #include <sys/cdefs.h>
34 1.1.2.2 yamt __KERNEL_RCSID(0, "$NetBSD: linux32_machdep.c,v 1.1.2.2 2006/02/18 15:38:59 yamt Exp $");
35 1.1.2.2 yamt
36 1.1.2.2 yamt #include <sys/param.h>
37 1.1.2.2 yamt #include <sys/systm.h>
38 1.1.2.2 yamt #include <sys/signalvar.h>
39 1.1.2.2 yamt #include <sys/kernel.h>
40 1.1.2.2 yamt #include <sys/proc.h>
41 1.1.2.2 yamt #include <sys/user.h>
42 1.1.2.2 yamt #include <sys/buf.h>
43 1.1.2.2 yamt #include <sys/reboot.h>
44 1.1.2.2 yamt #include <sys/conf.h>
45 1.1.2.2 yamt #include <sys/exec.h>
46 1.1.2.2 yamt #include <sys/file.h>
47 1.1.2.2 yamt #include <sys/callout.h>
48 1.1.2.2 yamt #include <sys/malloc.h>
49 1.1.2.2 yamt #include <sys/mbuf.h>
50 1.1.2.2 yamt #include <sys/msgbuf.h>
51 1.1.2.2 yamt #include <sys/mount.h>
52 1.1.2.2 yamt #include <sys/vnode.h>
53 1.1.2.2 yamt #include <sys/device.h>
54 1.1.2.2 yamt #include <sys/sa.h>
55 1.1.2.2 yamt #include <sys/syscallargs.h>
56 1.1.2.2 yamt #include <sys/filedesc.h>
57 1.1.2.2 yamt #include <sys/exec_elf.h>
58 1.1.2.2 yamt #include <sys/disklabel.h>
59 1.1.2.2 yamt #include <sys/ioctl.h>
60 1.1.2.2 yamt #include <sys/wait.h>
61 1.1.2.2 yamt #include <miscfs/specfs/specdev.h>
62 1.1.2.2 yamt
63 1.1.2.2 yamt #include <machine/netbsd32_machdep.h>
64 1.1.2.2 yamt
65 1.1.2.2 yamt #include <compat/netbsd32/netbsd32.h>
66 1.1.2.2 yamt #include <compat/netbsd32/netbsd32_syscallargs.h>
67 1.1.2.2 yamt
68 1.1.2.2 yamt #include <compat/linux/common/linux_signal.h>
69 1.1.2.2 yamt #include <compat/linux/common/linux_errno.h>
70 1.1.2.2 yamt
71 1.1.2.2 yamt #include <compat/linux32/common/linux32_types.h>
72 1.1.2.2 yamt #include <compat/linux32/common/linux32_errno.h>
73 1.1.2.2 yamt #include <compat/linux32/common/linux32_machdep.h>
74 1.1.2.2 yamt #include <compat/linux32/common/linux32_signal.h>
75 1.1.2.2 yamt #include <compat/linux32/common/linux32_exec.h>
76 1.1.2.2 yamt #include <compat/linux32/linux32_syscallargs.h>
77 1.1.2.2 yamt
78 1.1.2.2 yamt #include <machine/cpu.h>
79 1.1.2.2 yamt #include <machine/cpufunc.h>
80 1.1.2.2 yamt #include <machine/psl.h>
81 1.1.2.2 yamt #include <machine/reg.h>
82 1.1.2.2 yamt #include <machine/segments.h>
83 1.1.2.2 yamt #include <machine/specialreg.h>
84 1.1.2.2 yamt #include <machine/sysarch.h>
85 1.1.2.2 yamt #include <machine/vmparam.h>
86 1.1.2.2 yamt
87 1.1.2.2 yamt extern char linux32_sigcode[1];
88 1.1.2.2 yamt extern char linux32_rt_sigcode[1];
89 1.1.2.2 yamt extern char linux32_esigcode[1];
90 1.1.2.2 yamt
91 1.1.2.2 yamt extern void (osyscall_return) __P((void));
92 1.1.2.2 yamt
93 1.1.2.2 yamt static void linux32_save_ucontext __P((struct lwp *, struct trapframe *,
94 1.1.2.2 yamt const sigset_t *, struct sigaltstack *, struct linux32_ucontext *));
95 1.1.2.2 yamt static void linux32_save_sigcontext __P((struct lwp *, struct trapframe *,
96 1.1.2.2 yamt const sigset_t *, struct linux32_sigcontext *));
97 1.1.2.2 yamt static void linux32_rt_sendsig __P((const ksiginfo_t *, const sigset_t *));
98 1.1.2.2 yamt static void linux32_old_sendsig __P((const ksiginfo_t *, const sigset_t *));
99 1.1.2.2 yamt static int linux32_restore_sigcontext __P((struct lwp *,
100 1.1.2.2 yamt struct linux32_sigcontext *, register_t *));
101 1.1.2.2 yamt
102 1.1.2.2 yamt void
103 1.1.2.2 yamt linux32_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
104 1.1.2.2 yamt {
105 1.1.2.2 yamt if (SIGACTION(curproc, ksi->ksi_signo).sa_flags & SA_SIGINFO)
106 1.1.2.2 yamt linux32_rt_sendsig(ksi, mask);
107 1.1.2.2 yamt else
108 1.1.2.2 yamt linux32_old_sendsig(ksi, mask);
109 1.1.2.2 yamt return;
110 1.1.2.2 yamt }
111 1.1.2.2 yamt
112 1.1.2.2 yamt void
113 1.1.2.2 yamt linux32_old_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
114 1.1.2.2 yamt {
115 1.1.2.2 yamt struct lwp *l = curlwp;
116 1.1.2.2 yamt struct proc *p = l->l_proc;
117 1.1.2.2 yamt struct trapframe *tf;
118 1.1.2.2 yamt struct linux32_sigframe *fp, frame;
119 1.1.2.2 yamt int onstack;
120 1.1.2.2 yamt int sig = ksi->ksi_signo;
121 1.1.2.2 yamt sig_t catcher = SIGACTION(p, sig).sa_handler;
122 1.1.2.2 yamt struct sigaltstack *sas = &p->p_sigctx.ps_sigstk;
123 1.1.2.2 yamt
124 1.1.2.2 yamt tf = l->l_md.md_regs;
125 1.1.2.2 yamt /* Do we need to jump onto the signal stack? */
126 1.1.2.2 yamt onstack = (sas->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
127 1.1.2.2 yamt (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
128 1.1.2.2 yamt
129 1.1.2.2 yamt
130 1.1.2.2 yamt /* Allocate space for the signal handler context. */
131 1.1.2.2 yamt if (onstack)
132 1.1.2.2 yamt fp = (struct linux32_sigframe *)((caddr_t)sas->ss_sp +
133 1.1.2.2 yamt sas->ss_size);
134 1.1.2.2 yamt else
135 1.1.2.2 yamt fp = (struct linux32_sigframe *)tf->tf_rsp;
136 1.1.2.2 yamt fp--;
137 1.1.2.2 yamt
138 1.1.2.2 yamt /* Build stack frame for signal trampoline. */
139 1.1.2.2 yamt frame.sf_handler = (linux32_handler_t)(long)catcher;
140 1.1.2.2 yamt frame.sf_sig = native_to_linux32_signo[sig];
141 1.1.2.2 yamt
142 1.1.2.2 yamt linux32_save_sigcontext(l, tf, mask, &frame.sf_sc);
143 1.1.2.2 yamt
144 1.1.2.2 yamt if (copyout(&frame, fp, sizeof(frame)) != 0) {
145 1.1.2.2 yamt /*
146 1.1.2.2 yamt * Process has trashed its stack; give it an illegal
147 1.1.2.2 yamt * instruction to halt it in its tracks.
148 1.1.2.2 yamt */
149 1.1.2.2 yamt sigexit(l, SIGILL);
150 1.1.2.2 yamt /* NOTREACHED */
151 1.1.2.2 yamt }
152 1.1.2.2 yamt
153 1.1.2.2 yamt /*
154 1.1.2.2 yamt * Build context to run handler in.
155 1.1.2.2 yamt */
156 1.1.2.2 yamt tf->tf_gs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
157 1.1.2.2 yamt tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
158 1.1.2.2 yamt tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
159 1.1.2.2 yamt tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
160 1.1.2.2 yamt tf->tf_rip = ((long)p->p_sigctx.ps_sigcode) & 0xffffffff;
161 1.1.2.2 yamt tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL) & 0xffffffff;
162 1.1.2.2 yamt tf->tf_rflags &= ~(PSL_T|PSL_VM|PSL_AC) & 0xffffffff;
163 1.1.2.2 yamt tf->tf_rsp = (long)fp & 0xffffffff;
164 1.1.2.2 yamt tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
165 1.1.2.2 yamt
166 1.1.2.2 yamt /* Remember that we're now on the signal stack. */
167 1.1.2.2 yamt if (onstack)
168 1.1.2.2 yamt sas->ss_flags |= SS_ONSTACK;
169 1.1.2.2 yamt
170 1.1.2.2 yamt return;
171 1.1.2.2 yamt }
172 1.1.2.2 yamt
173 1.1.2.2 yamt void
174 1.1.2.2 yamt linux32_rt_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
175 1.1.2.2 yamt {
176 1.1.2.2 yamt struct lwp *l = curlwp;
177 1.1.2.2 yamt struct proc *p = l->l_proc;
178 1.1.2.2 yamt struct trapframe *tf;
179 1.1.2.2 yamt struct linux32_rt_sigframe *fp, frame;
180 1.1.2.2 yamt int onstack;
181 1.1.2.2 yamt linux32_siginfo_t *lsi;
182 1.1.2.2 yamt int sig = ksi->ksi_signo;
183 1.1.2.2 yamt sig_t catcher = SIGACTION(p, sig).sa_handler;
184 1.1.2.2 yamt struct sigaltstack *sas = &p->p_sigctx.ps_sigstk;
185 1.1.2.2 yamt
186 1.1.2.2 yamt tf = l->l_md.md_regs;
187 1.1.2.2 yamt /* Do we need to jump onto the signal stack? */
188 1.1.2.2 yamt onstack = (sas->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
189 1.1.2.2 yamt (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
190 1.1.2.2 yamt
191 1.1.2.2 yamt
192 1.1.2.2 yamt /* Allocate space for the signal handler context. */
193 1.1.2.2 yamt if (onstack)
194 1.1.2.2 yamt fp = (struct linux32_rt_sigframe *)((caddr_t)sas->ss_sp +
195 1.1.2.2 yamt sas->ss_size);
196 1.1.2.2 yamt else
197 1.1.2.2 yamt fp = (struct linux32_rt_sigframe *)tf->tf_rsp;
198 1.1.2.2 yamt fp--;
199 1.1.2.2 yamt
200 1.1.2.2 yamt /* Build stack frame for signal trampoline. */
201 1.1.2.2 yamt frame.sf_handler = (linux32_handler_t)(long)catcher;
202 1.1.2.2 yamt frame.sf_sig = native_to_linux32_signo[sig];
203 1.1.2.2 yamt frame.sf_sip = (linux32_siginfop_t)(long)&fp->sf_si;
204 1.1.2.2 yamt frame.sf_ucp = (linux32_ucontextp_t)(long)&fp->sf_uc;
205 1.1.2.2 yamt
206 1.1.2.2 yamt lsi = &frame.sf_si;
207 1.1.2.2 yamt (void)memset(lsi, 0, sizeof(frame.sf_si));
208 1.1.2.2 yamt lsi->lsi_errno = native_to_linux32_errno[ksi->ksi_errno];
209 1.1.2.2 yamt lsi->lsi_code = ksi->ksi_code;
210 1.1.2.2 yamt lsi->lsi_signo = native_to_linux32_signo[frame.sf_sig];
211 1.1.2.2 yamt switch (lsi->lsi_signo) {
212 1.1.2.2 yamt case LINUX32_SIGILL:
213 1.1.2.2 yamt case LINUX32_SIGFPE:
214 1.1.2.2 yamt case LINUX32_SIGSEGV:
215 1.1.2.2 yamt case LINUX32_SIGBUS:
216 1.1.2.2 yamt case LINUX32_SIGTRAP:
217 1.1.2.2 yamt lsi->lsi_addr = (netbsd32_voidp)(long)ksi->ksi_addr;
218 1.1.2.2 yamt break;
219 1.1.2.2 yamt case LINUX32_SIGCHLD:
220 1.1.2.2 yamt lsi->lsi_uid = ksi->ksi_uid;
221 1.1.2.2 yamt lsi->lsi_pid = ksi->ksi_pid;
222 1.1.2.2 yamt lsi->lsi_utime = ksi->ksi_utime;
223 1.1.2.2 yamt lsi->lsi_stime = ksi->ksi_stime;
224 1.1.2.2 yamt
225 1.1.2.2 yamt /* We use the same codes */
226 1.1.2.2 yamt lsi->lsi_code = ksi->ksi_code;
227 1.1.2.2 yamt /* XXX is that right? */
228 1.1.2.2 yamt lsi->lsi_status = WEXITSTATUS(ksi->ksi_status);
229 1.1.2.2 yamt break;
230 1.1.2.2 yamt case LINUX32_SIGIO:
231 1.1.2.2 yamt lsi->lsi_band = ksi->ksi_band;
232 1.1.2.2 yamt lsi->lsi_fd = ksi->ksi_fd;
233 1.1.2.2 yamt break;
234 1.1.2.2 yamt default:
235 1.1.2.2 yamt lsi->lsi_uid = ksi->ksi_uid;
236 1.1.2.2 yamt lsi->lsi_pid = ksi->ksi_pid;
237 1.1.2.2 yamt if (lsi->lsi_signo == LINUX32_SIGALRM ||
238 1.1.2.2 yamt lsi->lsi_signo >= LINUX32_SIGRTMIN)
239 1.1.2.2 yamt lsi->lsi_value.sival_ptr =
240 1.1.2.2 yamt (netbsd32_voidp)(long)ksi->ksi_sigval.sival_ptr;
241 1.1.2.2 yamt break;
242 1.1.2.2 yamt }
243 1.1.2.2 yamt
244 1.1.2.2 yamt /* Save register context. */
245 1.1.2.2 yamt linux32_save_ucontext(l, tf, mask, sas, &frame.sf_uc);
246 1.1.2.2 yamt
247 1.1.2.2 yamt if (copyout(&frame, fp, sizeof(frame)) != 0) {
248 1.1.2.2 yamt /*
249 1.1.2.2 yamt * Process has trashed its stack; give it an illegal
250 1.1.2.2 yamt * instruction to halt it in its tracks.
251 1.1.2.2 yamt */
252 1.1.2.2 yamt sigexit(l, SIGILL);
253 1.1.2.2 yamt /* NOTREACHED */
254 1.1.2.2 yamt }
255 1.1.2.2 yamt
256 1.1.2.2 yamt /*
257 1.1.2.2 yamt * Build context to run handler in.
258 1.1.2.2 yamt */
259 1.1.2.2 yamt tf->tf_gs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
260 1.1.2.2 yamt tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
261 1.1.2.2 yamt tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
262 1.1.2.2 yamt tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
263 1.1.2.2 yamt tf->tf_rip = (((long)p->p_sigctx.ps_sigcode) +
264 1.1.2.2 yamt (linux32_rt_sigcode - linux32_sigcode)) & 0xffffffff;
265 1.1.2.2 yamt tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL) & 0xffffffff;
266 1.1.2.2 yamt tf->tf_rflags &= ~(PSL_T|PSL_VM|PSL_AC) & 0xffffffff;
267 1.1.2.2 yamt tf->tf_rsp = (long)fp & 0xffffffff;
268 1.1.2.2 yamt tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
269 1.1.2.2 yamt
270 1.1.2.2 yamt /* Remember that we're now on the signal stack. */
271 1.1.2.2 yamt if (onstack)
272 1.1.2.2 yamt sas->ss_flags |= SS_ONSTACK;
273 1.1.2.2 yamt
274 1.1.2.2 yamt return;
275 1.1.2.2 yamt }
276 1.1.2.2 yamt
277 1.1.2.2 yamt void
278 1.1.2.2 yamt linux32_setregs(struct lwp *l, struct exec_package *pack, u_long stack)
279 1.1.2.2 yamt {
280 1.1.2.2 yamt struct pcb *pcb = &l->l_addr->u_pcb;
281 1.1.2.2 yamt struct trapframe *tf;
282 1.1.2.2 yamt struct proc *p = l->l_proc;
283 1.1.2.2 yamt void **retaddr;
284 1.1.2.2 yamt
285 1.1.2.2 yamt /* If we were using the FPU, forget about it. */
286 1.1.2.2 yamt if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
287 1.1.2.2 yamt fpusave_lwp(l, 0);
288 1.1.2.2 yamt
289 1.1.2.2 yamt #if defined(USER_LDT) && 0
290 1.1.2.2 yamt pmap_ldt_cleanup(p);
291 1.1.2.2 yamt #endif
292 1.1.2.2 yamt
293 1.1.2.2 yamt netbsd32_adjust_limits(p);
294 1.1.2.2 yamt
295 1.1.2.2 yamt l->l_md.md_flags &= ~MDP_USEDFPU;
296 1.1.2.2 yamt pcb->pcb_flags = 0;
297 1.1.2.2 yamt pcb->pcb_savefpu.fp_fxsave.fx_fcw = __Linux_NPXCW__;
298 1.1.2.2 yamt pcb->pcb_savefpu.fp_fxsave.fx_mxcsr = __INITIAL_MXCSR__;
299 1.1.2.2 yamt pcb->pcb_savefpu.fp_fxsave.fx_mxcsr_mask = __INITIAL_MXCSR_MASK__;
300 1.1.2.2 yamt pcb->pcb_fs = 0;
301 1.1.2.2 yamt pcb->pcb_gs = 0;
302 1.1.2.2 yamt
303 1.1.2.2 yamt
304 1.1.2.2 yamt l->l_proc->p_flag |= P_32;
305 1.1.2.2 yamt
306 1.1.2.2 yamt tf = l->l_md.md_regs;
307 1.1.2.2 yamt tf->tf_rax = 0;
308 1.1.2.2 yamt tf->tf_rbx = (u_int64_t)l->l_proc->p_psstr & 0xffffffff;
309 1.1.2.2 yamt tf->tf_rcx = pack->ep_entry & 0xffffffff;
310 1.1.2.2 yamt tf->tf_rdx = 0;
311 1.1.2.2 yamt tf->tf_rsi = 0;
312 1.1.2.2 yamt tf->tf_rdi = 0;
313 1.1.2.2 yamt tf->tf_rbp = 0;
314 1.1.2.2 yamt tf->tf_rsp = stack & 0xffffffff;
315 1.1.2.2 yamt tf->tf_r8 = 0;
316 1.1.2.2 yamt tf->tf_r9 = 0;
317 1.1.2.2 yamt tf->tf_r10 = 0;
318 1.1.2.2 yamt tf->tf_r11 = 0;
319 1.1.2.2 yamt tf->tf_r12 = 0;
320 1.1.2.2 yamt tf->tf_r13 = 0;
321 1.1.2.2 yamt tf->tf_r14 = 0;
322 1.1.2.2 yamt tf->tf_r15 = 0;
323 1.1.2.2 yamt tf->tf_rip = pack->ep_entry & 0xffffffff;
324 1.1.2.2 yamt tf->tf_rflags = PSL_USERSET;
325 1.1.2.2 yamt tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL) & 0xffffffff;
326 1.1.2.2 yamt tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
327 1.1.2.2 yamt tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
328 1.1.2.2 yamt tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
329 1.1.2.2 yamt tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
330 1.1.2.2 yamt tf->tf_gs = GSEL(GUDATA32_SEL, SEL_UPL) & 0xffffffff;
331 1.1.2.2 yamt
332 1.1.2.2 yamt /* XXX frob return address to return via old iret method, not sysret */
333 1.1.2.2 yamt retaddr = (void **)tf - 1;
334 1.1.2.2 yamt *retaddr = (void *)osyscall_return;
335 1.1.2.2 yamt return;
336 1.1.2.2 yamt }
337 1.1.2.2 yamt
338 1.1.2.2 yamt static void
339 1.1.2.2 yamt linux32_save_ucontext(l, tf, mask, sas, uc)
340 1.1.2.2 yamt struct lwp *l;
341 1.1.2.2 yamt struct trapframe *tf;
342 1.1.2.2 yamt const sigset_t *mask;
343 1.1.2.2 yamt struct sigaltstack *sas;
344 1.1.2.2 yamt struct linux32_ucontext *uc;
345 1.1.2.2 yamt {
346 1.1.2.2 yamt uc->uc_flags = 0;
347 1.1.2.2 yamt uc->uc_link = (linux32_ucontextp_t)(long)NULL;
348 1.1.2.2 yamt native_to_linux32_sigaltstack(&uc->uc_stack, sas);
349 1.1.2.2 yamt linux32_save_sigcontext(l, tf, mask, &uc->uc_mcontext);
350 1.1.2.2 yamt native_to_linux32_sigset(&uc->uc_sigmask, mask);
351 1.1.2.2 yamt (void)memset(&uc->uc_fpregs_mem, 0, sizeof(uc->uc_fpregs_mem));
352 1.1.2.2 yamt }
353 1.1.2.2 yamt
354 1.1.2.2 yamt static void
355 1.1.2.2 yamt linux32_save_sigcontext(l, tf, mask, sc)
356 1.1.2.2 yamt struct lwp *l;
357 1.1.2.2 yamt struct trapframe *tf;
358 1.1.2.2 yamt const sigset_t *mask;
359 1.1.2.2 yamt struct linux32_sigcontext *sc;
360 1.1.2.2 yamt {
361 1.1.2.2 yamt /* Save register context. */
362 1.1.2.2 yamt sc->sc_gs = tf->tf_gs;
363 1.1.2.2 yamt sc->sc_fs = tf->tf_fs;
364 1.1.2.2 yamt sc->sc_es = tf->tf_es;
365 1.1.2.2 yamt sc->sc_ds = tf->tf_ds;
366 1.1.2.2 yamt sc->sc_eflags = tf->tf_rflags;
367 1.1.2.2 yamt sc->sc_edi = tf->tf_rdi;
368 1.1.2.2 yamt sc->sc_esi = tf->tf_rsi;
369 1.1.2.2 yamt sc->sc_esp = tf->tf_rsp;
370 1.1.2.2 yamt sc->sc_ebp = tf->tf_rbp;
371 1.1.2.2 yamt sc->sc_ebx = tf->tf_rbx;
372 1.1.2.2 yamt sc->sc_edx = tf->tf_rdx;
373 1.1.2.2 yamt sc->sc_ecx = tf->tf_rcx;
374 1.1.2.2 yamt sc->sc_eax = tf->tf_rax;
375 1.1.2.2 yamt sc->sc_eip = tf->tf_rip;
376 1.1.2.2 yamt sc->sc_cs = tf->tf_cs;
377 1.1.2.2 yamt sc->sc_esp_at_signal = tf->tf_rsp;
378 1.1.2.2 yamt sc->sc_ss = tf->tf_ss;
379 1.1.2.2 yamt sc->sc_err = tf->tf_err;
380 1.1.2.2 yamt sc->sc_trapno = tf->tf_trapno;
381 1.1.2.2 yamt /* sc->sc_cr2 = l->l_addr->u_pcb.pcb_cr2; */ /* XXX */
382 1.1.2.2 yamt sc->sc_387 = (linux32_fpstatep_t)(long)NULL;
383 1.1.2.2 yamt
384 1.1.2.2 yamt /* Save signal stack. */
385 1.1.2.2 yamt /* Linux doesn't save the onstack flag in sigframe */
386 1.1.2.2 yamt
387 1.1.2.2 yamt /* Save signal mask. */
388 1.1.2.2 yamt native_to_linux32_old_sigset(&sc->sc_mask, mask);
389 1.1.2.2 yamt }
390 1.1.2.2 yamt
391 1.1.2.2 yamt int
392 1.1.2.2 yamt linux32_sys_sigreturn(l, v, retval)
393 1.1.2.2 yamt struct lwp *l;
394 1.1.2.2 yamt void *v;
395 1.1.2.2 yamt register_t *retval;
396 1.1.2.2 yamt {
397 1.1.2.2 yamt struct linux32_sys_sigreturn_args /* {
398 1.1.2.2 yamt syscallarg(linux32_sigcontextp_t) scp;
399 1.1.2.2 yamt } */ *uap = v;
400 1.1.2.2 yamt struct linux32_sigcontext ctx;
401 1.1.2.2 yamt int error;
402 1.1.2.2 yamt
403 1.1.2.2 yamt if ((error = copyin(NETBSD32PTR64(SCARG(uap, scp)),
404 1.1.2.2 yamt &ctx, sizeof(ctx))) != 0)
405 1.1.2.2 yamt return error;
406 1.1.2.2 yamt
407 1.1.2.2 yamt return linux32_restore_sigcontext(l, &ctx, retval);
408 1.1.2.2 yamt }
409 1.1.2.2 yamt
410 1.1.2.2 yamt int
411 1.1.2.2 yamt linux32_sys_rt_sigreturn(l, v, retval)
412 1.1.2.2 yamt struct lwp *l;
413 1.1.2.2 yamt void *v;
414 1.1.2.2 yamt register_t *retval;
415 1.1.2.2 yamt {
416 1.1.2.2 yamt struct linux32_sys_rt_sigreturn_args /* {
417 1.1.2.2 yamt syscallarg(linux32_ucontextp_t) ucp;
418 1.1.2.2 yamt } */ *uap = v;
419 1.1.2.2 yamt struct linux32_ucontext ctx;
420 1.1.2.2 yamt int error;
421 1.1.2.2 yamt
422 1.1.2.2 yamt if ((error = copyin(NETBSD32PTR64(SCARG(uap, ucp)),
423 1.1.2.2 yamt &ctx, sizeof(ctx))) != 0)
424 1.1.2.2 yamt return error;
425 1.1.2.2 yamt
426 1.1.2.2 yamt return linux32_restore_sigcontext(l, &ctx.uc_mcontext, retval);
427 1.1.2.2 yamt }
428 1.1.2.2 yamt
429 1.1.2.2 yamt static int
430 1.1.2.2 yamt linux32_restore_sigcontext(l, scp, retval)
431 1.1.2.2 yamt struct lwp *l;
432 1.1.2.2 yamt struct linux32_sigcontext *scp;
433 1.1.2.2 yamt register_t *retval;
434 1.1.2.2 yamt {
435 1.1.2.2 yamt struct trapframe *tf;
436 1.1.2.2 yamt struct proc *p = l->l_proc;
437 1.1.2.2 yamt struct sigaltstack *sas = &p->p_sigctx.ps_sigstk;
438 1.1.2.2 yamt sigset_t mask;
439 1.1.2.2 yamt ssize_t ss_gap;
440 1.1.2.2 yamt
441 1.1.2.2 yamt /* Restore register context. */
442 1.1.2.2 yamt tf = l->l_md.md_regs;
443 1.1.2.2 yamt
444 1.1.2.2 yamt /*
445 1.1.2.2 yamt * Check for security violations. If we're returning to
446 1.1.2.2 yamt * protected mode, the CPU will validate the segment registers
447 1.1.2.2 yamt * automatically and generate a trap on violations. We handle
448 1.1.2.2 yamt * the trap, rather than doing all of the checking here.
449 1.1.2.2 yamt */
450 1.1.2.2 yamt if (((scp->sc_eflags ^ tf->tf_rflags) & PSL_USERSTATIC) != 0 ||
451 1.1.2.2 yamt !USERMODE(scp->sc_cs, scp->sc_eflags))
452 1.1.2.2 yamt return EINVAL;
453 1.1.2.2 yamt
454 1.1.2.2 yamt if (scp->sc_fs != 0 && !VALID_USER_DSEL32(scp->sc_fs))
455 1.1.2.2 yamt return EINVAL;
456 1.1.2.2 yamt
457 1.1.2.2 yamt if (scp->sc_gs != 0 && !VALID_USER_DSEL32(scp->sc_gs))
458 1.1.2.2 yamt return EINVAL;
459 1.1.2.2 yamt
460 1.1.2.2 yamt if (scp->sc_es != 0 && !VALID_USER_DSEL32(scp->sc_es))
461 1.1.2.2 yamt return EINVAL;
462 1.1.2.2 yamt
463 1.1.2.2 yamt if (!VALID_USER_DSEL32(scp->sc_ds) ||
464 1.1.2.2 yamt !VALID_USER_DSEL32(scp->sc_ss))
465 1.1.2.2 yamt return EINVAL;
466 1.1.2.2 yamt
467 1.1.2.2 yamt if (scp->sc_eip >= VM_MAXUSER_ADDRESS32)
468 1.1.2.2 yamt return EINVAL;
469 1.1.2.2 yamt
470 1.1.2.2 yamt tf->tf_gs = (register_t)scp->sc_gs & 0xffffffff;
471 1.1.2.2 yamt tf->tf_fs = (register_t)scp->sc_fs & 0xffffffff;
472 1.1.2.2 yamt tf->tf_es = (register_t)scp->sc_es & 0xffffffff;
473 1.1.2.2 yamt tf->tf_ds = (register_t)scp->sc_ds & 0xffffffff;
474 1.1.2.2 yamt tf->tf_rflags &= ~PSL_USER;
475 1.1.2.2 yamt tf->tf_rflags |= ((register_t)scp->sc_eflags & PSL_USER);
476 1.1.2.2 yamt tf->tf_rdi = (register_t)scp->sc_edi & 0xffffffff;
477 1.1.2.2 yamt tf->tf_rsi = (register_t)scp->sc_esi & 0xffffffff;
478 1.1.2.2 yamt tf->tf_rbp = (register_t)scp->sc_ebp & 0xffffffff;
479 1.1.2.2 yamt tf->tf_rbx = (register_t)scp->sc_ebx & 0xffffffff;
480 1.1.2.2 yamt tf->tf_rdx = (register_t)scp->sc_edx & 0xffffffff;
481 1.1.2.2 yamt tf->tf_rcx = (register_t)scp->sc_ecx & 0xffffffff;
482 1.1.2.2 yamt tf->tf_rax = (register_t)scp->sc_eax & 0xffffffff;
483 1.1.2.2 yamt tf->tf_rip = (register_t)scp->sc_eip & 0xffffffff;
484 1.1.2.2 yamt tf->tf_cs = (register_t)scp->sc_cs & 0xffffffff;
485 1.1.2.2 yamt tf->tf_rsp = (register_t)scp->sc_esp_at_signal & 0xffffffff;
486 1.1.2.2 yamt tf->tf_ss = (register_t)scp->sc_ss & 0xffffffff;
487 1.1.2.2 yamt
488 1.1.2.2 yamt /* Restore signal stack. */
489 1.1.2.2 yamt ss_gap = (ssize_t)
490 1.1.2.2 yamt ((caddr_t)NETBSD32PTR64(scp->sc_esp_at_signal)
491 1.1.2.2 yamt - (caddr_t)sas->ss_sp);
492 1.1.2.2 yamt if (ss_gap >= 0 && ss_gap < sas->ss_size)
493 1.1.2.2 yamt sas->ss_flags |= SS_ONSTACK;
494 1.1.2.2 yamt else
495 1.1.2.2 yamt sas->ss_flags &= ~SS_ONSTACK;
496 1.1.2.2 yamt
497 1.1.2.2 yamt /* Restore signal mask. */
498 1.1.2.2 yamt linux32_old_to_native_sigset(&mask, &scp->sc_mask);
499 1.1.2.2 yamt (void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
500 1.1.2.2 yamt #ifdef DEBUG_LINUX
501 1.1.2.2 yamt printf("linux32_sigreturn: rip = 0x%lx, rsp = 0x%lx, flags = 0x%lx\n",
502 1.1.2.2 yamt tf->tf_rip, tf->tf_rsp, tf->tf_rflags);
503 1.1.2.2 yamt #endif
504 1.1.2.2 yamt return EJUSTRETURN;
505 1.1.2.2 yamt }
506