1 /*	$OpenBSD: linux_machdep.c,v 1.29 2003/08/15 20:32:13 tedu Exp $	*/
2 /*	$NetBSD: linux_machdep.c,v 1.29 1996/05/03 19:42:11 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1995 Frank van der Linden
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed for the NetBSD Project
19  *      by Frank van der Linden
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/signalvar.h>
38 #include <sys/kernel.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/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/msgbuf.h>
48 #include <sys/mount.h>
49 #include <sys/vnode.h>
50 #include <sys/device.h>
51 #include <sys/sysctl.h>
52 #include <sys/syscallargs.h>
53 #include <sys/filedesc.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 #include <compat/linux/linux_ioctl.h>
60 
61 #include <machine/cpu.h>
62 #include <machine/cpufunc.h>
63 #include <machine/psl.h>
64 #include <machine/reg.h>
65 #include <machine/segments.h>
66 #include <machine/specialreg.h>
67 #include <machine/sysarch.h>
68 #include <machine/vm86.h>
69 #include <machine/linux_machdep.h>
70 
71 /*
72  * To see whether wsdisplay is configured (for virtual console ioctl calls).
73  */
74 #include "wsdisplay.h"
75 #include <sys/ioctl.h>
76 #if NWSDISPLAY > 0 && defined(WSDISPLAY_COMPAT_USL)
77 #include <dev/wscons/wsconsio.h>
78 #include <dev/wscons/wsdisplay_usl_io.h>
79 #endif
80 
81 #ifdef USER_LDT
82 #include <machine/cpu.h>
83 int linux_read_ldt(struct proc *, struct linux_sys_modify_ldt_args *,
84     register_t *);
85 int linux_write_ldt(struct proc *, struct linux_sys_modify_ldt_args *,
86     register_t *);
87 #endif
88 
89 /*
90  * Deal with some i386-specific things in the Linux emulation code.
91  * This means just signals for now, will include stuff like
92  * I/O map permissions and V86 mode sometime.
93  */
94 
95 /*
96  * Send an interrupt to process.
97  *
98  * Stack is set up to allow sigcode stored
99  * in u. to call routine, followed by kcall
100  * to sigreturn routine below.  After sigreturn
101  * resets the signal mask, the stack, and the
102  * frame pointer, it returns to the user
103  * specified pc, psl.
104  */
105 
106 void
linux_sendsig(catcher,sig,mask,code,type,val)107 linux_sendsig(catcher, sig, mask, code, type, val)
108 	sig_t catcher;
109 	int sig, mask;
110 	u_long code;
111 	int type;
112 	union sigval val;
113 {
114 	struct proc *p = curproc;
115 	struct pmap *pmap = vm_map_pmap(&p->p_vmspace->vm_map);
116 	struct trapframe *tf;
117 	struct linux_sigframe *fp, frame;
118 	struct sigacts *psp = p->p_sigacts;
119 	int oonstack;
120 
121 	tf = p->p_md.md_regs;
122 	oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
123 
124 	/*
125 	 * Allocate space for the signal handler context.
126 	 */
127 	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
128 	    (psp->ps_sigonstack & sigmask(sig))) {
129 		fp = (struct linux_sigframe *)(psp->ps_sigstk.ss_sp +
130 		    psp->ps_sigstk.ss_size - sizeof(struct linux_sigframe));
131 		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
132 	} else {
133 		fp = (struct linux_sigframe *)tf->tf_esp - 1;
134 	}
135 
136 	frame.sf_handler = catcher;
137 	frame.sf_sig = bsd_to_linux_sig[sig];
138 
139 	/*
140 	 * Build the signal context to be used by sigreturn.
141 	 */
142 	frame.sf_sc.sc_mask   = mask;
143 #ifdef VM86
144 	if (tf->tf_eflags & PSL_VM) {
145 		frame.sf_sc.sc_gs = tf->tf_vm86_gs;
146 		frame.sf_sc.sc_fs = tf->tf_vm86_fs;
147 		frame.sf_sc.sc_es = tf->tf_vm86_es;
148 		frame.sf_sc.sc_ds = tf->tf_vm86_ds;
149 		frame.sf_sc.sc_eflags = get_vflags(p);
150 	} else
151 #endif
152 	{
153 		__asm("movl %%gs,%k0" : "=r" (frame.sf_sc.sc_gs));
154 		__asm("movl %%fs,%k0" : "=r" (frame.sf_sc.sc_fs));
155 		frame.sf_sc.sc_es = tf->tf_es;
156 		frame.sf_sc.sc_ds = tf->tf_ds;
157 		frame.sf_sc.sc_eflags = tf->tf_eflags;
158 	}
159 	frame.sf_sc.sc_edi = tf->tf_edi;
160 	frame.sf_sc.sc_esi = tf->tf_esi;
161 	frame.sf_sc.sc_ebp = tf->tf_ebp;
162 	frame.sf_sc.sc_ebx = tf->tf_ebx;
163 	frame.sf_sc.sc_edx = tf->tf_edx;
164 	frame.sf_sc.sc_ecx = tf->tf_ecx;
165 	frame.sf_sc.sc_eax = tf->tf_eax;
166 	frame.sf_sc.sc_eip = tf->tf_eip;
167 	frame.sf_sc.sc_cs = tf->tf_cs;
168 	frame.sf_sc.sc_esp_at_signal = tf->tf_esp;
169 	frame.sf_sc.sc_ss = tf->tf_ss;
170 	frame.sf_sc.sc_err = tf->tf_err;
171 	frame.sf_sc.sc_trapno = tf->tf_trapno;
172 
173 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
174 		/*
175 		 * Process has trashed its stack; give it an illegal
176 		 * instruction to halt it in its tracks.
177 		 */
178 		sigexit(p, SIGILL);
179 		/* NOTREACHED */
180 	}
181 
182 	/*
183 	 * Build context to run handler in.
184 	 */
185 	tf->tf_es = GSEL(GUDATA_SEL, SEL_UPL);
186 	tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
187 	tf->tf_eip = p->p_sigcode;
188 	tf->tf_cs = pmap->pm_hiexec > I386_MAX_EXE_ADDR ?
189 	    GSEL(GUCODE1_SEL, SEL_UPL) : GSEL(GUCODE_SEL, SEL_UPL);
190 	tf->tf_eflags &= ~(PSL_D|PSL_T|PSL_VM|PSL_AC);
191 	tf->tf_esp = (int)fp;
192 	tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
193 }
194 
195 /*
196  * System call to cleanup state after a signal
197  * has been taken.  Reset signal mask and
198  * stack state from context left by sendsig (above).
199  * Return to previous pc and psl as specified by
200  * context left by sendsig. Check carefully to
201  * make sure that the user has not modified the
202  * psl to gain improper privileges or to cause
203  * a machine fault.
204  */
205 int
linux_sys_sigreturn(p,v,retval)206 linux_sys_sigreturn(p, v, retval)
207 	struct proc *p;
208 	void *v;
209 	register_t *retval;
210 {
211 	struct linux_sys_sigreturn_args /* {
212 		syscallarg(struct linux_sigcontext *) scp;
213 	} */ *uap = v;
214 	struct linux_sigcontext *scp, context;
215 	struct trapframe *tf;
216 
217 	tf = p->p_md.md_regs;
218 
219 	/*
220 	 * The trampoline code hands us the context.
221 	 * It is unsafe to keep track of it ourselves, in the event that a
222 	 * program jumps out of a signal handler.
223 	 */
224 	scp = SCARG(uap, scp);
225 	if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0)
226 		return (EFAULT);
227 
228 	/*
229 	 * Restore signal context.
230 	 */
231 #ifdef VM86
232 	if (context.sc_eflags & PSL_VM) {
233 		tf->tf_vm86_gs = context.sc_gs;
234 		tf->tf_vm86_fs = context.sc_fs;
235 		tf->tf_vm86_es = context.sc_es;
236 		tf->tf_vm86_ds = context.sc_ds;
237 		set_vflags(p, context.sc_eflags);
238 	} else
239 #endif
240 	{
241 		/*
242 		 * Check for security violations.  If we're returning to
243 		 * protected mode, the CPU will validate the segment registers
244 		 * automatically and generate a trap on violations.  We handle
245 		 * the trap, rather than doing all of the checking here.
246 		 */
247 		if (((context.sc_eflags ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
248 		    !USERMODE(context.sc_cs, context.sc_eflags))
249 			return (EINVAL);
250 
251 		/* %fs and %gs were restored by the trampoline. */
252 		tf->tf_es = context.sc_es;
253 		tf->tf_ds = context.sc_ds;
254 		tf->tf_eflags = context.sc_eflags;
255 	}
256 	tf->tf_edi = context.sc_edi;
257 	tf->tf_esi = context.sc_esi;
258 	tf->tf_ebp = context.sc_ebp;
259 	tf->tf_ebx = context.sc_ebx;
260 	tf->tf_edx = context.sc_edx;
261 	tf->tf_ecx = context.sc_ecx;
262 	tf->tf_eax = context.sc_eax;
263 	tf->tf_eip = context.sc_eip;
264 	tf->tf_cs = context.sc_cs;
265 	tf->tf_esp = context.sc_esp_at_signal;
266 	tf->tf_ss = context.sc_ss;
267 
268 	p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
269 	p->p_sigmask = context.sc_mask & ~sigcantmask;
270 
271 	return (EJUSTRETURN);
272 }
273 
274 int
linux_sys_rt_sigreturn(p,v,retval)275 linux_sys_rt_sigreturn(p, v, retval)
276 	struct proc *p;
277 	void *v;
278 	register_t *retval;
279 {
280 	return(ENOSYS);
281 }
282 
283 #ifdef USER_LDT
284 
285 int
linux_read_ldt(p,uap,retval)286 linux_read_ldt(p, uap, retval)
287 	struct proc *p;
288 	struct linux_sys_modify_ldt_args /* {
289 		syscallarg(int) func;
290 		syscallarg(void *) ptr;
291 		syscallarg(size_t) bytecount;
292 	} */ *uap;
293 	register_t *retval;
294 {
295 	struct i386_get_ldt_args gl;
296 	int error;
297 	caddr_t sg;
298 	char *parms;
299 
300 	if (user_ldt_enable == 0)
301 		return (ENOSYS);
302 
303 	sg = stackgap_init(p->p_emul);
304 
305 	gl.start = 0;
306 	gl.desc = SCARG(uap, ptr);
307 	gl.num = SCARG(uap, bytecount) / sizeof(union descriptor);
308 
309 	parms = stackgap_alloc(&sg, sizeof(gl));
310 
311 	if ((error = copyout(&gl, parms, sizeof(gl))) != 0)
312 		return (error);
313 
314 	if ((error = i386_get_ldt(p, parms, retval)) != 0)
315 		return (error);
316 
317 	*retval *= sizeof(union descriptor);
318 	return (0);
319 }
320 
321 struct linux_ldt_info {
322 	u_int entry_number;
323 	u_long base_addr;
324 	u_int limit;
325 	u_int seg_32bit:1;
326 	u_int contents:2;
327 	u_int read_exec_only:1;
328 	u_int limit_in_pages:1;
329 	u_int seg_not_present:1;
330 };
331 
332 int
linux_write_ldt(p,uap,retval)333 linux_write_ldt(p, uap, retval)
334 	struct proc *p;
335 	struct linux_sys_modify_ldt_args /* {
336 		syscallarg(int) func;
337 		syscallarg(void *) ptr;
338 		syscallarg(size_t) bytecount;
339 	} */ *uap;
340 	register_t *retval;
341 {
342 	struct linux_ldt_info ldt_info;
343 	struct segment_descriptor sd;
344 	struct i386_set_ldt_args sl;
345 	int error;
346 	caddr_t sg;
347 	char *parms;
348 
349 	if (user_ldt_enable == 0)
350 		return (ENOSYS);
351 
352 	if (SCARG(uap, bytecount) != sizeof(ldt_info))
353 		return (EINVAL);
354 	if ((error = copyin(SCARG(uap, ptr), &ldt_info, sizeof(ldt_info))) != 0)
355 		return error;
356 	if (ldt_info.contents == 3)
357 		return (EINVAL);
358 
359 	sg = stackgap_init(p->p_emul);
360 
361 	sd.sd_lobase = ldt_info.base_addr & 0xffffff;
362 	sd.sd_hibase = (ldt_info.base_addr >> 24) & 0xff;
363 	sd.sd_lolimit = ldt_info.limit & 0xffff;
364 	sd.sd_hilimit = (ldt_info.limit >> 16) & 0xf;
365 	sd.sd_type =
366 	    16 | (ldt_info.contents << 2) | (!ldt_info.read_exec_only << 1);
367 	sd.sd_dpl = SEL_UPL;
368 	sd.sd_p = !ldt_info.seg_not_present;
369 	sd.sd_def32 = ldt_info.seg_32bit;
370 	sd.sd_gran = ldt_info.limit_in_pages;
371 
372 	sl.start = ldt_info.entry_number;
373 	sl.desc = stackgap_alloc(&sg, sizeof(sd));
374 	sl.num = 1;
375 
376 #if 0
377 	printf("linux_write_ldt: idx=%d, base=%x, limit=%x\n",
378 	    ldt_info.entry_number, ldt_info.base_addr, ldt_info.limit);
379 #endif
380 
381 	parms = stackgap_alloc(&sg, sizeof(sl));
382 
383 	if ((error = copyout(&sd, sl.desc, sizeof(sd))) != 0)
384 		return (error);
385 	if ((error = copyout(&sl, parms, sizeof(sl))) != 0)
386 		return (error);
387 
388 	if ((error = i386_set_ldt(p, parms, retval)) != 0)
389 		return (error);
390 
391 	*retval = 0;
392 	return (0);
393 }
394 
395 #endif /* USER_LDT */
396 
397 int
linux_sys_modify_ldt(p,v,retval)398 linux_sys_modify_ldt(p, v, retval)
399 	struct proc *p;
400 	void *v;
401 	register_t *retval;
402 {
403 	struct linux_sys_modify_ldt_args /* {
404 		syscallarg(int) func;
405 		syscallarg(void *) ptr;
406 		syscallarg(size_t) bytecount;
407 	} */ *uap = v;
408 
409 	switch (SCARG(uap, func)) {
410 #ifdef USER_LDT
411 	case 0:
412 		return (linux_read_ldt(p, uap, retval));
413 
414 	case 1:
415 		return (linux_write_ldt(p, uap, retval));
416 #endif /* USER_LDT */
417 
418 	default:
419 		return (ENOSYS);
420 	}
421 }
422 
423 /*
424  * XXX Pathetic hack to make svgalib work. This will fake the major
425  * device number of an opened VT so that svgalib likes it. grmbl.
426  * Should probably do it 'wrong the right way' and use a mapping
427  * array for all major device numbers, and map linux_mknod too.
428  */
429 dev_t
linux_fakedev(dev)430 linux_fakedev(dev)
431 	dev_t dev;
432 {
433 
434 	if (major(dev) == NATIVE_CONS_MAJOR)
435 		return makedev(LINUX_CONS_MAJOR, (minor(dev) + 1));
436 	return dev;
437 }
438 
439 /*
440  * We come here in a last attempt to satisfy a Linux ioctl() call
441  */
442 int
linux_machdepioctl(p,v,retval)443 linux_machdepioctl(p, v, retval)
444 	struct proc *p;
445 	void *v;
446 	register_t *retval;
447 {
448 	struct linux_sys_ioctl_args /* {
449 		syscallarg(int) fd;
450 		syscallarg(u_long) com;
451 		syscallarg(caddr_t) data;
452 	} */ *uap = v;
453 	struct sys_ioctl_args bia;
454 	u_long com;
455 	int error;
456 #if (NWSDISPLAY > 0 && defined(WSDISPLAY_COMPAT_USL))
457 	struct vt_mode lvt;
458 	caddr_t bvtp, sg;
459 #endif
460 	struct filedesc *fdp;
461 	struct file *fp;
462 	int fd;
463 	int (*ioctlf)(struct file *, u_long, caddr_t, struct proc *);
464 	struct ioctl_pt pt;
465 
466 	fd = SCARG(uap, fd);
467 	SCARG(&bia, fd) = SCARG(uap, fd);
468 	SCARG(&bia, data) = SCARG(uap, data);
469 	com = SCARG(uap, com);
470 
471 	fdp = p->p_fd;
472 	if ((fp = fd_getfile(fdp, fd)) == NULL)
473 		return (EBADF);
474 
475 	switch (com) {
476 #if (NWSDISPLAY > 0 && defined(WSDISPLAY_COMPAT_USL))
477 	case LINUX_KDGKBMODE:
478 		com = KDGKBMODE;
479 		break;
480 	case LINUX_KDSKBMODE:
481 		com = KDSKBMODE;
482 		if ((unsigned)SCARG(uap, data) == LINUX_K_MEDIUMRAW)
483 			SCARG(&bia, data) = (caddr_t)K_RAW;
484 		break;
485 	case LINUX_KIOCSOUND:
486 		SCARG(&bia, data) =
487 			(caddr_t)(((unsigned long)SCARG(&bia, data)) & 0xffff);
488 		/* fall through */
489 	case LINUX_KDMKTONE:
490 		com = KDMKTONE;
491 		break;
492 	case LINUX_KDSETMODE:
493 		com = KDSETMODE;
494 		break;
495 	case LINUX_KDGETMODE:
496 #if NWSDISPLAY > 0 && defined(WSDISPLAY_COMPAT_USL)
497 		com = WSDISPLAYIO_GMODE;
498 #else
499 		com = KDGETMODE;
500 #endif
501 		break;
502 	case LINUX_KDENABIO:
503 		com = KDENABIO;
504 		break;
505 	case LINUX_KDDISABIO:
506 		com = KDDISABIO;
507 		break;
508 	case LINUX_KDGETLED:
509 		com = KDGETLED;
510 		break;
511 	case LINUX_KDSETLED:
512 		com = KDSETLED;
513 		break;
514 	case LINUX_VT_OPENQRY:
515 		com = VT_OPENQRY;
516 		break;
517 	case LINUX_VT_GETMODE: {
518 		int sig;
519 
520 		SCARG(&bia, com) = VT_GETMODE;
521 		if ((error = sys_ioctl(p, &bia, retval)))
522 			return error;
523 		if ((error = copyin(SCARG(uap, data), (caddr_t)&lvt,
524 		    sizeof (struct vt_mode))))
525 			return error;
526 		/* We need to bounds check here in case there
527 		   is a race with another thread */
528 		if ((error = bsd_to_linux_signal(lvt.relsig, &sig)))
529 			return error;
530 		lvt.relsig = sig;
531 
532 		if ((error = bsd_to_linux_signal(lvt.acqsig, &sig)))
533 			return error;
534 		lvt.acqsig = sig;
535 
536 		if ((error = bsd_to_linux_signal(lvt.frsig, &sig)))
537 			return error;
538 		lvt.frsig = sig;
539 
540 		return copyout((caddr_t)&lvt, SCARG(uap, data),
541 		    sizeof (struct vt_mode));
542 	}
543 	case LINUX_VT_SETMODE: {
544 		int sig;
545 
546 		com = VT_SETMODE;
547 		if ((error = copyin(SCARG(uap, data), (caddr_t)&lvt,
548 		    sizeof (struct vt_mode))))
549 			return error;
550 		if ((error = linux_to_bsd_signal(lvt.relsig, &sig)))
551 			return error;
552 		lvt.relsig = sig;
553 
554 		if ((error = linux_to_bsd_signal(lvt.acqsig, &sig)))
555 			return error;
556 		lvt.acqsig = sig;
557 
558 		if ((error = linux_to_bsd_signal(lvt.frsig, &sig)))
559 			return error;
560 		lvt.frsig = sig;
561 
562 		sg = stackgap_init(p->p_emul);
563 		bvtp = stackgap_alloc(&sg, sizeof (struct vt_mode));
564 		if ((error = copyout(&lvt, bvtp, sizeof (struct vt_mode))))
565 			return error;
566 		SCARG(&bia, data) = bvtp;
567 		break;
568 	}
569 	case LINUX_VT_DISALLOCATE:
570 		/* XXX should use WSDISPLAYIO_DELSCREEN */
571 		return 0;
572 	case LINUX_VT_RELDISP:
573 		com = VT_RELDISP;
574 		break;
575 	case LINUX_VT_ACTIVATE:
576 		com = VT_ACTIVATE;
577 		break;
578 	case LINUX_VT_WAITACTIVE:
579 		com = VT_WAITACTIVE;
580 		break;
581 	case LINUX_VT_GETSTATE:
582 		com = VT_GETSTATE;
583 		break;
584 	case LINUX_KDGKBTYPE:
585 	{
586 		char tmp = KB_101;
587 
588 		/* This is what Linux does */
589 		return copyout(&tmp, SCARG(uap, data), sizeof(char));
590 	}
591 #endif
592 	default:
593 		/*
594 		 * Unknown to us. If it's on a device, just pass it through
595 		 * using PTIOCLINUX, the device itself might be able to
596 		 * make some sense of it.
597 		 * XXX hack: if the function returns EJUSTRETURN,
598 		 * it has stuffed a sysctl return value in pt.data.
599 		 */
600 		FREF(fp);
601 		ioctlf = fp->f_ops->fo_ioctl;
602 		pt.com = SCARG(uap, com);
603 		pt.data = SCARG(uap, data);
604 		error = ioctlf(fp, PTIOCLINUX, (caddr_t)&pt, p);
605 		FRELE(fp);
606 		if (error == EJUSTRETURN) {
607 			retval[0] = (register_t)pt.data;
608 			error = 0;
609 		}
610 
611 		if (error == ENOTTY)
612 			printf("linux_machdepioctl: invalid ioctl %08lx\n",
613 			    com);
614 		return (error);
615 	}
616 	SCARG(&bia, com) = com;
617 	return sys_ioctl(p, &bia, retval);
618 }
619 
620 /*
621  * Set I/O permissions for a process. Just set the maximum level
622  * right away (ignoring the argument), otherwise we would have
623  * to rely on I/O permission maps, which are not implemented.
624  */
625 int
linux_sys_iopl(p,v,retval)626 linux_sys_iopl(p, v, retval)
627 	struct proc *p;
628 	void *v;
629 	register_t *retval;
630 {
631 #if 0
632 	struct linux_sys_iopl_args /* {
633 		syscallarg(int) level;
634 	} */ *uap = v;
635 #endif
636 	struct trapframe *fp = p->p_md.md_regs;
637 
638 	if (suser(p, 0) != 0)
639 		return EPERM;
640 	if (securelevel > 0)
641 		return EPERM;
642 	fp->tf_eflags |= PSL_IOPL;
643 	*retval = 0;
644 	return 0;
645 }
646 
647 /*
648  * See above. If a root process tries to set access to an I/O port,
649  * just let it have the whole range.
650  */
651 int
linux_sys_ioperm(p,v,retval)652 linux_sys_ioperm(p, v, retval)
653 	struct proc *p;
654 	void *v;
655 	register_t *retval;
656 {
657 	struct linux_sys_ioperm_args /* {
658 		syscallarg(unsigned int) lo;
659 		syscallarg(unsigned int) hi;
660 		syscallarg(int) val;
661 	} */ *uap = v;
662 	struct trapframe *fp = p->p_md.md_regs;
663 
664 	if (suser(p, 0) != 0)
665 		return EPERM;
666 	if (securelevel > 0)
667 		return EPERM;
668 	if (SCARG(uap, val))
669 		fp->tf_eflags |= PSL_IOPL;
670 	*retval = 0;
671 	return 0;
672 }
673