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