linux_machdep.c revision 1.12 1 /* $NetBSD: linux_machdep.c,v 1.12 1995/08/14 02:19:54 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1995 Frank van der Linden
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project
18 * by Frank van der Linden
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/signalvar.h>
37 #include <sys/kernel.h>
38 #include <sys/map.h>
39 #include <sys/proc.h>
40 #include <sys/user.h>
41 #include <sys/buf.h>
42 #include <sys/reboot.h>
43 #include <sys/conf.h>
44 #include <sys/file.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/msgbuf.h>
49 #include <sys/mount.h>
50 #include <sys/vnode.h>
51 #include <sys/device.h>
52 #include <sys/sysctl.h>
53 #include <sys/syscallargs.h>
54
55 #include <compat/linux/linux_types.h>
56 #include <compat/linux/linux_signal.h>
57 #include <compat/linux/linux_syscallargs.h>
58 #include <compat/linux/linux_util.h>
59
60 #include <machine/cpu.h>
61 #include <machine/cpufunc.h>
62 #include <machine/psl.h>
63 #include <machine/reg.h>
64 #include <machine/segments.h>
65 #include <machine/specialreg.h>
66 #include <machine/sysarch.h>
67 #include <machine/linux_machdep.h>
68
69 /*
70 * Deal with some i386-specific things in the Linux emulation code.
71 * This means just signals for now, will include stuff like
72 * I/O map permissions and V86 mode sometime.
73 */
74
75 /*
76 * Send an interrupt to process.
77 *
78 * Stack is set up to allow sigcode stored
79 * in u. to call routine, followed by kcall
80 * to sigreturn routine below. After sigreturn
81 * resets the signal mask, the stack, and the
82 * frame pointer, it returns to the user
83 * specified pc, psl.
84 */
85
86 void
87 linux_sendsig(catcher, sig, mask, code)
88 sig_t catcher;
89 int sig, mask;
90 u_long code;
91 {
92 register struct proc *p = curproc;
93 register struct trapframe *tf;
94 struct linux_sigframe *fp, frame;
95 struct sigacts *psp = p->p_sigacts;
96 int oonstack;
97 extern char linux_sigcode[], linux_esigcode[];
98
99 tf = p->p_md.md_regs;
100 oonstack = psp->ps_sigstk.ss_flags & SA_ONSTACK;
101
102 /*
103 * Allocate space for the signal handler context.
104 */
105 if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
106 (psp->ps_sigonstack & sigmask(sig))) {
107 fp = (struct linux_sigframe *)(psp->ps_sigstk.ss_base +
108 psp->ps_sigstk.ss_size - sizeof(struct linux_sigframe));
109 psp->ps_sigstk.ss_flags |= SA_ONSTACK;
110 } else {
111 fp = (struct linux_sigframe *)tf->tf_esp - 1;
112 }
113
114 frame.sf_handler = catcher;
115 frame.sf_sig = bsd_to_linux_sig[sig];
116
117 /*
118 * Build the signal context to be used by sigreturn.
119 */
120 frame.sf_sc.sc_mask = mask;
121 #ifdef VM86
122 if (tf->tf_eflags & PSL_VM) {
123 frame.sf_sc.sc_gs = tf->tf_vm86_gs;
124 frame.sf_sc.sc_fs = tf->tf_vm86_fs;
125 frame.sf_sc.sc_es = tf->tf_vm86_es;
126 frame.sf_sc.sc_ds = tf->tf_vm86_ds;
127 } else
128 #else
129 {
130 __asm("movl %%gs,%w0" : "=r" (frame.sf_sc.sc_gs));
131 __asm("movl %%fs,%w0" : "=r" (frame.sf_sc.sc_fs));
132 frame.sf_sc.sc_es = tf->tf_es;
133 frame.sf_sc.sc_ds = tf->tf_ds;
134 }
135 #endif
136 frame.sf_sc.sc_edi = tf->tf_edi;
137 frame.sf_sc.sc_esi = tf->tf_esi;
138 frame.sf_sc.sc_ebp = tf->tf_ebp;
139 frame.sf_sc.sc_ebx = tf->tf_ebx;
140 frame.sf_sc.sc_edx = tf->tf_edx;
141 frame.sf_sc.sc_ecx = tf->tf_ecx;
142 frame.sf_sc.sc_eax = tf->tf_eax;
143 frame.sf_sc.sc_eip = tf->tf_eip;
144 frame.sf_sc.sc_cs = tf->tf_cs;
145 frame.sf_sc.sc_eflags = tf->tf_eflags;
146 frame.sf_sc.sc_esp_at_signal = tf->tf_esp;
147 frame.sf_sc.sc_ss = tf->tf_ss;
148 frame.sf_sc.sc_err = tf->tf_err;
149 frame.sf_sc.sc_trapno = tf->tf_trapno;
150
151 if (copyout(&frame, fp, sizeof(frame)) != 0) {
152 /*
153 * Process has trashed its stack; give it an illegal
154 * instruction to halt it in its tracks.
155 */
156 sigexit(p, SIGILL);
157 /* NOTREACHED */
158 }
159
160 /*
161 * Build context to run handler in.
162 */
163 tf->tf_esp = (int)fp;
164 tf->tf_eip = (int)(((char *)PS_STRINGS) -
165 (linux_esigcode - linux_sigcode));
166 #ifdef VM86
167 tf->tf_eflags &= ~PSL_VM;
168 #endif
169 tf->tf_cs = LSEL(LUCODE_SEL, SEL_UPL);
170 tf->tf_ds = LSEL(LUDATA_SEL, SEL_UPL);
171 tf->tf_es = LSEL(LUDATA_SEL, SEL_UPL);
172 tf->tf_ss = LSEL(LUDATA_SEL, SEL_UPL);
173 }
174
175 /*
176 * System call to cleanup state after a signal
177 * has been taken. Reset signal mask and
178 * stack state from context left by sendsig (above).
179 * Return to previous pc and psl as specified by
180 * context left by sendsig. Check carefully to
181 * make sure that the user has not modified the
182 * psl to gain improper privileges or to cause
183 * a machine fault.
184 */
185 int
186 linux_sigreturn(p, uap, retval)
187 struct proc *p;
188 struct linux_sigreturn_args /* {
189 syscallarg(struct linux_sigcontext *) scp;
190 } */ *uap;
191 register_t *retval;
192 {
193 struct linux_sigcontext *scp, context;
194 register struct trapframe *tf;
195
196 tf = p->p_md.md_regs;
197
198 /*
199 * The trampoline code hands us the context.
200 * It is unsafe to keep track of it ourselves, in the event that a
201 * program jumps out of a signal handler.
202 */
203 scp = SCARG(uap, scp);
204 if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0)
205 return (EFAULT);
206
207 /*
208 * Check for security violations.
209 */
210 if (((context.sc_eflags ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
211 ISPL(context.sc_cs) != SEL_UPL)
212 return (EINVAL);
213
214 p->p_sigacts->ps_sigstk.ss_flags &= ~SA_ONSTACK;
215 p->p_sigmask = context.sc_mask & ~sigcantmask;
216
217 /*
218 * Restore signal context.
219 */
220 #ifdef VM86
221 if (context.sc_eflags & PSL_VM) {
222 tf->tf_vm86_gs = context.sc_gs;
223 tf->tf_vm86_fs = context.sc_fs;
224 tf->tf_vm86_es = context.sc_es;
225 tf->tf_vm86_ds = context.sc_ds;
226 } else
227 #endif
228 {
229 /* %fs and %gs were restored by the trampoline. */
230 tf->tf_es = context.sc_es;
231 tf->tf_ds = context.sc_ds;
232 }
233 tf->tf_edi = context.sc_edi;
234 tf->tf_esi = context.sc_esi;
235 tf->tf_ebp = context.sc_ebp;
236 tf->tf_ebx = context.sc_ebx;
237 tf->tf_edx = context.sc_edx;
238 tf->tf_ecx = context.sc_ecx;
239 tf->tf_eax = context.sc_eax;
240 tf->tf_eip = context.sc_eip;
241 tf->tf_cs = context.sc_cs;
242 tf->tf_eflags = context.sc_eflags;
243 tf->tf_esp = context.sc_esp_at_signal;
244 tf->tf_ss = context.sc_ss;
245
246 return (EJUSTRETURN);
247 }
248
249 #ifdef USER_LDT
250
251 int
252 linux_read_ldt(p, uap, retval)
253 struct proc *p;
254 struct linux_modify_ldt_args /* {
255 syscallarg(int) func;
256 syscallarg(void *) ptr;
257 syscallarg(size_t) bytecount;
258 } */ *uap;
259 register_t *retval;
260 {
261 struct i386_get_ldt_args gl;
262 int error;
263 caddr_t sg;
264 char *parms;
265
266 sg = stackgap_init(p->p_emul);
267
268 gl.start = 0;
269 gl.desc = SCARG(uap, ptr);
270 gl.num = SCARG(uap, bytecount) / sizeof(union descriptor);
271
272 parms = stackgap_alloc(&sg, sizeof(gl));
273
274 if (error = copyout(&gl, parms, sizeof(gl)))
275 return (error);
276
277 if (error = i386_get_ldt(p, parms, retval))
278 return (error);
279
280 *retval *= sizeof(union descriptor);
281 return (0);
282 }
283
284 struct linux_ldt_info {
285 u_int entry_number;
286 u_long base_addr;
287 u_int limit;
288 u_int seg_32bit:1;
289 u_int contents:2;
290 u_int read_exec_only:1;
291 u_int limit_in_pages:1;
292 u_int seg_not_present:1;
293 };
294
295 int
296 linux_write_ldt(p, uap, retval)
297 struct proc *p;
298 struct linux_modify_ldt_args /* {
299 syscallarg(int) func;
300 syscallarg(void *) ptr;
301 syscallarg(size_t) bytecount;
302 } */ *uap;
303 register_t *retval;
304 {
305 struct linux_ldt_info ldt_info;
306 struct segment_descriptor sd;
307 struct i386_set_ldt_args sl;
308 int error;
309 caddr_t sg;
310 char *parms;
311
312 if (SCARG(uap, bytecount) != sizeof(ldt_info))
313 return (EINVAL);
314 if (error = copyin(SCARG(uap, ptr), &ldt_info, sizeof(ldt_info)))
315 return error;
316 if (ldt_info.contents == 3)
317 return (EINVAL);
318
319 sg = stackgap_init(p->p_emul);
320
321 sd.sd_lobase = ldt_info.base_addr & 0xffffff;
322 sd.sd_hibase = (ldt_info.base_addr >> 24) & 0xff;
323 sd.sd_lolimit = ldt_info.limit & 0xffff;
324 sd.sd_hilimit = (ldt_info.limit >> 16) & 0xf;
325 sd.sd_type =
326 16 | (ldt_info.contents << 2) | (!ldt_info.read_exec_only << 1);
327 sd.sd_dpl = SEL_UPL;
328 sd.sd_p = !ldt_info.seg_not_present;
329 sd.sd_def32 = ldt_info.seg_32bit;
330 sd.sd_gran = ldt_info.limit_in_pages;
331
332 sl.start = ldt_info.entry_number;
333 sl.desc = stackgap_alloc(&sg, sizeof(sd));
334 sl.num = 1;
335
336 #if 0
337 printf("linux_write_ldt: idx=%d, base=%x, limit=%x\n",
338 ldt_info.entry_number, ldt_info.base_addr, ldt_info.limit);
339 #endif
340
341 parms = stackgap_alloc(&sg, sizeof(sl));
342
343 if (error = copyout(&sd, sl.desc, sizeof(sd)))
344 return (error);
345 if (error = copyout(&sl, parms, sizeof(sl)))
346 return (error);
347
348 if (error = i386_set_ldt(p, parms, retval))
349 return (error);
350
351 *retval = 0;
352 return (0);
353 }
354
355 #endif /* USER_LDT */
356
357 int
358 linux_modify_ldt(p, uap, retval)
359 struct proc *p;
360 struct linux_modify_ldt_args /* {
361 syscallarg(int) func;
362 syscallarg(void *) ptr;
363 syscallarg(size_t) bytecount;
364 } */ *uap;
365 register_t *retval;
366 {
367
368 switch (SCARG(uap, func)) {
369 #ifdef USER_LDT
370 case 0:
371 return (linux_read_ldt(p, uap, retval));
372
373 case 1:
374 return (linux_write_ldt(p, uap, retval));
375 #endif /* USER_LDT */
376
377 default:
378 return (ENOSYS);
379 }
380 }
381