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