linux_machdep.c revision 1.36 1 /* $NetBSD: linux_machdep.c,v 1.36 1998/01/22 00:39:19 thorpej 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 "opt_vm86.h"
35 #include "opt_user_ldt.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/signalvar.h>
40 #include <sys/kernel.h>
41 #include <sys/map.h>
42 #include <sys/proc.h>
43 #include <sys/user.h>
44 #include <sys/buf.h>
45 #include <sys/reboot.h>
46 #include <sys/conf.h>
47 #include <sys/exec.h>
48 #include <sys/file.h>
49 #include <sys/callout.h>
50 #include <sys/malloc.h>
51 #include <sys/mbuf.h>
52 #include <sys/msgbuf.h>
53 #include <sys/mount.h>
54 #include <sys/vnode.h>
55 #include <sys/device.h>
56 #include <sys/syscallargs.h>
57 #include <sys/filedesc.h>
58
59 #include <compat/linux/linux_types.h>
60 #include <compat/linux/linux_signal.h>
61 #include <compat/linux/linux_syscallargs.h>
62 #include <compat/linux/linux_util.h>
63 #include <compat/linux/linux_ioctl.h>
64
65 #include <machine/cpu.h>
66 #include <machine/cpufunc.h>
67 #include <machine/psl.h>
68 #include <machine/reg.h>
69 #include <machine/segments.h>
70 #include <machine/specialreg.h>
71 #include <machine/sysarch.h>
72 #include <machine/vm86.h>
73 #include <machine/vmparam.h>
74 #include <machine/linux_machdep.h>
75
76 /*
77 * To see whether pcvt is configured (for virtual console ioctl calls).
78 */
79 #ifndef NVT
80 #include "vt.h"
81 #endif
82 #if NVT > 0
83 #include <arch/i386/isa/pcvt/pcvt_ioctl.h>
84 #endif
85
86 #ifdef USER_LDT
87 #include <machine/cpu.h>
88 int linux_read_ldt __P((struct proc *, struct linux_sys_modify_ldt_args *,
89 register_t *));
90 int linux_write_ldt __P((struct proc *, struct linux_sys_modify_ldt_args *,
91 register_t *));
92 #endif
93
94 /*
95 * Deal with some i386-specific things in the Linux emulation code.
96 * This means just signals for now, will include stuff like
97 * I/O map permissions and V86 mode sometime.
98 */
99
100 /*
101 * Send an interrupt to process.
102 *
103 * Stack is set up to allow sigcode stored
104 * in u. to call routine, followed by kcall
105 * to sigreturn routine below. After sigreturn
106 * resets the signal mask, the stack, and the
107 * frame pointer, it returns to the user
108 * specified pc, psl.
109 */
110
111 void
112 linux_sendsig(catcher, sig, mask, code)
113 sig_t catcher;
114 int sig, mask;
115 u_long code;
116 {
117 register struct proc *p = curproc;
118 register struct trapframe *tf;
119 struct linux_sigframe *fp, frame;
120 struct sigacts *psp = p->p_sigacts;
121 int oonstack;
122 extern char linux_sigcode[], linux_esigcode[];
123
124 tf = p->p_md.md_regs;
125 oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
126
127 /*
128 * Allocate space for the signal handler context.
129 */
130 if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
131 (psp->ps_sigonstack & sigmask(sig))) {
132 fp = (struct linux_sigframe *)(psp->ps_sigstk.ss_sp +
133 psp->ps_sigstk.ss_size - sizeof(struct linux_sigframe));
134 psp->ps_sigstk.ss_flags |= SS_ONSTACK;
135 } else {
136 fp = (struct linux_sigframe *)tf->tf_esp - 1;
137 }
138
139 frame.sf_handler = catcher;
140 frame.sf_sig = bsd_to_linux_sig[sig];
141
142 /*
143 * Build the signal context to be used by sigreturn.
144 */
145 frame.sf_sc.sc_mask = mask;
146 #ifdef VM86
147 if (tf->tf_eflags & PSL_VM) {
148 frame.sf_sc.sc_gs = tf->tf_vm86_gs;
149 frame.sf_sc.sc_fs = tf->tf_vm86_fs;
150 frame.sf_sc.sc_es = tf->tf_vm86_es;
151 frame.sf_sc.sc_ds = tf->tf_vm86_ds;
152 frame.sf_sc.sc_eflags = get_vflags(p);
153 } else
154 #endif
155 {
156 __asm("movl %%gs,%w0" : "=r" (frame.sf_sc.sc_gs));
157 __asm("movl %%fs,%w0" : "=r" (frame.sf_sc.sc_fs));
158 frame.sf_sc.sc_es = tf->tf_es;
159 frame.sf_sc.sc_ds = tf->tf_ds;
160 frame.sf_sc.sc_eflags = tf->tf_eflags;
161 }
162 frame.sf_sc.sc_edi = tf->tf_edi;
163 frame.sf_sc.sc_esi = tf->tf_esi;
164 frame.sf_sc.sc_ebp = tf->tf_ebp;
165 frame.sf_sc.sc_ebx = tf->tf_ebx;
166 frame.sf_sc.sc_edx = tf->tf_edx;
167 frame.sf_sc.sc_ecx = tf->tf_ecx;
168 frame.sf_sc.sc_eax = tf->tf_eax;
169 frame.sf_sc.sc_eip = tf->tf_eip;
170 frame.sf_sc.sc_cs = tf->tf_cs;
171 frame.sf_sc.sc_esp_at_signal = tf->tf_esp;
172 frame.sf_sc.sc_ss = tf->tf_ss;
173 frame.sf_sc.sc_err = tf->tf_err;
174 frame.sf_sc.sc_trapno = tf->tf_trapno;
175
176 if (copyout(&frame, fp, sizeof(frame)) != 0) {
177 /*
178 * Process has trashed its stack; give it an illegal
179 * instruction to halt it in its tracks.
180 */
181 sigexit(p, SIGILL);
182 /* NOTREACHED */
183 }
184
185 /*
186 * Build context to run handler in.
187 */
188 tf->tf_es = GSEL(GUDATA_SEL, SEL_UPL);
189 tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
190 tf->tf_eip = (int)(((char *)PS_STRINGS) -
191 (linux_esigcode - linux_sigcode));
192 tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
193 tf->tf_eflags &= ~(PSL_T|PSL_VM|PSL_AC);
194 tf->tf_esp = (int)fp;
195 tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
196 }
197
198 /*
199 * System call to cleanup state after a signal
200 * has been taken. Reset signal mask and
201 * stack state from context left by sendsig (above).
202 * Return to previous pc and psl as specified by
203 * context left by sendsig. Check carefully to
204 * make sure that the user has not modified the
205 * psl to gain improper privileges or to cause
206 * a machine fault.
207 */
208 int
209 linux_sys_sigreturn(p, v, retval)
210 struct proc *p;
211 void *v;
212 register_t *retval;
213 {
214 struct linux_sys_sigreturn_args /* {
215 syscallarg(struct linux_sigcontext *) scp;
216 } */ *uap = v;
217 struct linux_sigcontext *scp, context;
218 register struct trapframe *tf;
219
220 tf = p->p_md.md_regs;
221
222 /*
223 * The trampoline code hands us the context.
224 * It is unsafe to keep track of it ourselves, in the event that a
225 * program jumps out of a signal handler.
226 */
227 scp = SCARG(uap, scp);
228 if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0)
229 return (EFAULT);
230
231 /*
232 * Restore signal context.
233 */
234 #ifdef VM86
235 if (context.sc_eflags & PSL_VM) {
236 tf->tf_vm86_gs = context.sc_gs;
237 tf->tf_vm86_fs = context.sc_fs;
238 tf->tf_vm86_es = context.sc_es;
239 tf->tf_vm86_ds = context.sc_ds;
240 set_vflags(p, context.sc_eflags);
241 } else
242 #endif
243 {
244 /*
245 * Check for security violations. If we're returning to
246 * protected mode, the CPU will validate the segment registers
247 * automatically and generate a trap on violations. We handle
248 * the trap, rather than doing all of the checking here.
249 */
250 if (((context.sc_eflags ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
251 !USERMODE(context.sc_cs, context.sc_eflags))
252 return (EINVAL);
253
254 /* %fs and %gs were restored by the trampoline. */
255 tf->tf_es = context.sc_es;
256 tf->tf_ds = context.sc_ds;
257 tf->tf_eflags = context.sc_eflags;
258 }
259 tf->tf_edi = context.sc_edi;
260 tf->tf_esi = context.sc_esi;
261 tf->tf_ebp = context.sc_ebp;
262 tf->tf_ebx = context.sc_ebx;
263 tf->tf_edx = context.sc_edx;
264 tf->tf_ecx = context.sc_ecx;
265 tf->tf_eax = context.sc_eax;
266 tf->tf_eip = context.sc_eip;
267 tf->tf_cs = context.sc_cs;
268 tf->tf_esp = context.sc_esp_at_signal;
269 tf->tf_ss = context.sc_ss;
270
271 p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
272 p->p_sigmask = context.sc_mask & ~sigcantmask;
273
274 return (EJUSTRETURN);
275 }
276
277 #ifdef USER_LDT
278
279 int
280 linux_read_ldt(p, uap, retval)
281 struct proc *p;
282 struct linux_sys_modify_ldt_args /* {
283 syscallarg(int) func;
284 syscallarg(void *) ptr;
285 syscallarg(size_t) bytecount;
286 } */ *uap;
287 register_t *retval;
288 {
289 struct i386_get_ldt_args gl;
290 int error;
291 caddr_t sg;
292 char *parms;
293
294 sg = stackgap_init(p->p_emul);
295
296 gl.start = 0;
297 gl.desc = SCARG(uap, ptr);
298 gl.num = SCARG(uap, bytecount) / sizeof(union descriptor);
299
300 parms = stackgap_alloc(&sg, sizeof(gl));
301
302 if ((error = copyout(&gl, parms, sizeof(gl))) != 0)
303 return (error);
304
305 if ((error = i386_get_ldt(p, parms, retval)) != 0)
306 return (error);
307
308 *retval *= sizeof(union descriptor);
309 return (0);
310 }
311
312 struct linux_ldt_info {
313 u_int entry_number;
314 u_long base_addr;
315 u_int limit;
316 u_int seg_32bit:1;
317 u_int contents:2;
318 u_int read_exec_only:1;
319 u_int limit_in_pages:1;
320 u_int seg_not_present:1;
321 };
322
323 int
324 linux_write_ldt(p, uap, retval)
325 struct proc *p;
326 struct linux_sys_modify_ldt_args /* {
327 syscallarg(int) func;
328 syscallarg(void *) ptr;
329 syscallarg(size_t) bytecount;
330 } */ *uap;
331 register_t *retval;
332 {
333 struct linux_ldt_info ldt_info;
334 struct segment_descriptor sd;
335 struct i386_set_ldt_args sl;
336 int error;
337 caddr_t sg;
338 char *parms;
339
340 if (SCARG(uap, bytecount) != sizeof(ldt_info))
341 return (EINVAL);
342 if ((error = copyin(SCARG(uap, ptr), &ldt_info, sizeof(ldt_info))) != 0)
343 return error;
344 if (ldt_info.contents == 3)
345 return (EINVAL);
346
347 sg = stackgap_init(p->p_emul);
348
349 sd.sd_lobase = ldt_info.base_addr & 0xffffff;
350 sd.sd_hibase = (ldt_info.base_addr >> 24) & 0xff;
351 sd.sd_lolimit = ldt_info.limit & 0xffff;
352 sd.sd_hilimit = (ldt_info.limit >> 16) & 0xf;
353 sd.sd_type =
354 16 | (ldt_info.contents << 2) | (!ldt_info.read_exec_only << 1);
355 sd.sd_dpl = SEL_UPL;
356 sd.sd_p = !ldt_info.seg_not_present;
357 sd.sd_def32 = ldt_info.seg_32bit;
358 sd.sd_gran = ldt_info.limit_in_pages;
359
360 sl.start = ldt_info.entry_number;
361 sl.desc = stackgap_alloc(&sg, sizeof(sd));
362 sl.num = 1;
363
364 #if 0
365 printf("linux_write_ldt: idx=%d, base=%x, limit=%x\n",
366 ldt_info.entry_number, ldt_info.base_addr, ldt_info.limit);
367 #endif
368
369 parms = stackgap_alloc(&sg, sizeof(sl));
370
371 if ((error = copyout(&sd, sl.desc, sizeof(sd))) != 0)
372 return (error);
373 if ((error = copyout(&sl, parms, sizeof(sl))) != 0)
374 return (error);
375
376 if ((error = i386_set_ldt(p, parms, retval)) != 0)
377 return (error);
378
379 *retval = 0;
380 return (0);
381 }
382
383 #endif /* USER_LDT */
384
385 int
386 linux_sys_modify_ldt(p, v, retval)
387 struct proc *p;
388 void *v;
389 register_t *retval;
390 {
391 struct linux_sys_modify_ldt_args /* {
392 syscallarg(int) func;
393 syscallarg(void *) ptr;
394 syscallarg(size_t) bytecount;
395 } */ *uap = v;
396
397 switch (SCARG(uap, func)) {
398 #ifdef USER_LDT
399 case 0:
400 return (linux_read_ldt(p, uap, retval));
401
402 case 1:
403 return (linux_write_ldt(p, uap, retval));
404 #endif /* USER_LDT */
405
406 default:
407 return (ENOSYS);
408 }
409 }
410
411 /*
412 * XXX Pathetic hack to make svgalib work. This will fake the major
413 * device number of an opened VT so that svgalib likes it. grmbl.
414 * Should probably do it 'wrong the right way' and use a mapping
415 * array for all major device numbers, and map linux_mknod too.
416 */
417 dev_t
418 linux_fakedev(dev)
419 dev_t dev;
420 {
421
422 if (major(dev) == NETBSD_CONS_MAJOR)
423 return makedev(LINUX_CONS_MAJOR, (minor(dev) + 1));
424 return dev;
425 }
426
427 /*
428 * We come here in a last attempt to satisfy a Linux ioctl() call
429 */
430 int
431 linux_machdepioctl(p, v, retval)
432 struct proc *p;
433 void *v;
434 register_t *retval;
435 {
436 struct linux_sys_ioctl_args /* {
437 syscallarg(int) fd;
438 syscallarg(u_long) com;
439 syscallarg(caddr_t) data;
440 } */ *uap = v;
441 struct sys_ioctl_args bia;
442 u_long com;
443 #if NVT > 0
444 int error;
445 struct vt_mode lvt;
446 caddr_t bvtp, sg;
447 #endif
448
449 SCARG(&bia, fd) = SCARG(uap, fd);
450 SCARG(&bia, data) = SCARG(uap, data);
451 com = SCARG(uap, com);
452
453 switch (com) {
454 #if NVT > 0
455 case LINUX_KDGKBMODE:
456 com = KDGKBMODE;
457 break;
458 case LINUX_KDSKBMODE:
459 com = KDSKBMODE;
460 if ((unsigned)SCARG(uap, data) == LINUX_K_MEDIUMRAW)
461 SCARG(&bia, data) = (caddr_t)K_RAW;
462 break;
463 case LINUX_KDMKTONE:
464 com = KDMKTONE;
465 break;
466 case LINUX_KDSETMODE:
467 com = KDSETMODE;
468 break;
469 case LINUX_KDENABIO:
470 com = KDENABIO;
471 break;
472 case LINUX_KDDISABIO:
473 com = KDDISABIO;
474 break;
475 case LINUX_KDGETLED:
476 com = KDGETLED;
477 break;
478 case LINUX_KDSETLED:
479 com = KDSETLED;
480 break;
481 case LINUX_VT_OPENQRY:
482 com = VT_OPENQRY;
483 break;
484 case LINUX_VT_GETMODE:
485 SCARG(&bia, com) = VT_GETMODE;
486 if ((error = sys_ioctl(p, &bia, retval)))
487 return error;
488 if ((error = copyin(SCARG(uap, data), (caddr_t)&lvt,
489 sizeof (struct vt_mode))))
490 return error;
491 lvt.relsig = bsd_to_linux_sig[lvt.relsig];
492 lvt.acqsig = bsd_to_linux_sig[lvt.acqsig];
493 lvt.frsig = bsd_to_linux_sig[lvt.frsig];
494 return copyout((caddr_t)&lvt, SCARG(uap, data),
495 sizeof (struct vt_mode));
496 case LINUX_VT_SETMODE:
497 com = VT_SETMODE;
498 if ((error = copyin(SCARG(uap, data), (caddr_t)&lvt,
499 sizeof (struct vt_mode))))
500 return error;
501 lvt.relsig = linux_to_bsd_sig[lvt.relsig];
502 lvt.acqsig = linux_to_bsd_sig[lvt.acqsig];
503 lvt.frsig = linux_to_bsd_sig[lvt.frsig];
504 sg = stackgap_init(p->p_emul);
505 bvtp = stackgap_alloc(&sg, sizeof (struct vt_mode));
506 if ((error = copyout(&lvt, bvtp, sizeof (struct vt_mode))))
507 return error;
508 SCARG(&bia, data) = bvtp;
509 break;
510 case LINUX_VT_RELDISP:
511 com = VT_RELDISP;
512 break;
513 case LINUX_VT_ACTIVATE:
514 com = VT_ACTIVATE;
515 break;
516 case LINUX_VT_WAITACTIVE:
517 com = VT_WAITACTIVE;
518 break;
519 #endif
520 default:
521 printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
522 return EINVAL;
523 }
524 SCARG(&bia, com) = com;
525 return sys_ioctl(p, &bia, retval);
526 }
527
528 /*
529 * Set I/O permissions for a process. Just set the maximum level
530 * right away (ignoring the argument), otherwise we would have
531 * to rely on I/O permission maps, which are not implemented.
532 */
533 int
534 linux_sys_iopl(p, v, retval)
535 struct proc *p;
536 void *v;
537 register_t *retval;
538 {
539 #if 0
540 struct linux_sys_iopl_args /* {
541 syscallarg(int) level;
542 } */ *uap = v;
543 #endif
544 struct trapframe *fp = p->p_md.md_regs;
545
546 if (suser(p->p_ucred, &p->p_acflag) != 0)
547 return EPERM;
548 fp->tf_eflags |= PSL_IOPL;
549 *retval = 0;
550 return 0;
551 }
552
553 /*
554 * See above. If a root process tries to set access to an I/O port,
555 * just let it have the whole range.
556 */
557 int
558 linux_sys_ioperm(p, v, retval)
559 struct proc *p;
560 void *v;
561 register_t *retval;
562 {
563 struct linux_sys_ioperm_args /* {
564 syscallarg(unsigned int) lo;
565 syscallarg(unsigned int) hi;
566 syscallarg(int) val;
567 } */ *uap = v;
568 struct trapframe *fp = p->p_md.md_regs;
569
570 if (suser(p->p_ucred, &p->p_acflag) != 0)
571 return EPERM;
572 if (SCARG(uap, val))
573 fp->tf_eflags |= PSL_IOPL;
574 *retval = 0;
575 return 0;
576 }
577