Home | History | Annotate | Line # | Download | only in x86
sys_machdep.c revision 1.9
      1 /*	$NetBSD: sys_machdep.c,v 1.9 2008/01/04 15:55:31 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum, and by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: sys_machdep.c,v 1.9 2008/01/04 15:55:31 yamt Exp $");
     41 
     42 #include "opt_compat_netbsd.h"
     43 #include "opt_mtrr.h"
     44 #include "opt_perfctrs.h"
     45 #include "opt_user_ldt.h"
     46 #include "opt_vm86.h"
     47 #include "opt_xen.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/ioctl.h>
     52 #include <sys/file.h>
     53 #include <sys/time.h>
     54 #include <sys/proc.h>
     55 #include <sys/user.h>
     56 #include <sys/uio.h>
     57 #include <sys/kernel.h>
     58 #include <sys/buf.h>
     59 #include <sys/signal.h>
     60 #include <sys/malloc.h>
     61 #include <sys/kmem.h>
     62 #include <sys/kauth.h>
     63 
     64 #include <sys/mount.h>
     65 #include <sys/syscallargs.h>
     66 
     67 #include <uvm/uvm_extern.h>
     68 
     69 #include <machine/cpu.h>
     70 #include <machine/cpufunc.h>
     71 #include <machine/gdt.h>
     72 #include <machine/psl.h>
     73 #include <machine/reg.h>
     74 #include <machine/sysarch.h>
     75 #include <machine/mtrr.h>
     76 
     77 #ifdef __x86_64__
     78 /* Need to be checked. */
     79 #undef	USER_LDT
     80 #undef	PERFCTRS
     81 #undef	VM86
     82 #undef	IOPERM
     83 #else
     84 #if defined(XEN)
     85 #undef	IOPERM
     86 #else /* defined(XEN) */
     87 #define	IOPERM
     88 #endif /* defined(XEN) */
     89 #endif
     90 
     91 #ifdef VM86
     92 #include <machine/vm86.h>
     93 #endif
     94 
     95 #ifdef PERFCTRS
     96 #include <machine/pmc.h>
     97 #endif
     98 
     99 /* XXX needs changes from vmlocking branch */
    100 #define	mutex_enter(x)	/* nothing */
    101 #define	mutex_exit(x)	/* nothing */
    102 
    103 extern struct vm_map *kernel_map;
    104 
    105 int x86_get_ioperm(struct lwp *, void *, register_t *);
    106 int x86_set_ioperm(struct lwp *, void *, register_t *);
    107 int x86_get_mtrr(struct lwp *, void *, register_t *);
    108 int x86_set_mtrr(struct lwp *, void *, register_t *);
    109 int x86_set_sdbase(void *arg, char which);
    110 int x86_get_sdbase(void *arg, char which);
    111 
    112 #ifdef LDT_DEBUG
    113 static void x86_print_ldt(int, const struct segment_descriptor *);
    114 
    115 static void
    116 x86_print_ldt(int i, const struct segment_descriptor *d)
    117 {
    118 	printf("[%d] lolimit=0x%x, lobase=0x%x, type=%u, dpl=%u, p=%u, "
    119 	    "hilimit=0x%x, xx=%x, def32=%u, gran=%u, hibase=0x%x\n",
    120 	    i, d->sd_lolimit, d->sd_lobase, d->sd_type, d->sd_dpl, d->sd_p,
    121 	    d->sd_hilimit, d->sd_xx, d->sd_def32, d->sd_gran, d->sd_hibase);
    122 }
    123 #endif
    124 
    125 int
    126 x86_get_ldt_len(struct lwp *l)
    127 {
    128 #ifndef USER_LDT
    129 	return -1;
    130 #else
    131 	pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap;
    132 	int nldt;
    133 
    134 	mutex_enter(&pmap->pm_lock);
    135 
    136 	if (pmap->pm_flags & PMF_USER_LDT) {
    137 		nldt = pmap->pm_ldt_len;
    138 	} else {
    139 		nldt = NLDT;
    140 	}
    141 	mutex_exit(&pmap->pm_lock);
    142 	return nldt;
    143 #endif
    144 }
    145 
    146 
    147 int
    148 x86_get_ldt(struct lwp *l, void *args, register_t *retval)
    149 {
    150 #ifndef USER_LDT
    151 	return EINVAL;
    152 #else
    153 	struct x86_get_ldt_args ua;
    154 	union descriptor *cp;
    155 	int error;
    156 
    157 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    158 		return error;
    159 
    160 	if (ua.num < 0 || ua.num > 8192)
    161 		return EINVAL;
    162 
    163 	cp = malloc(ua.num * sizeof(union descriptor), M_TEMP, M_WAITOK);
    164 	if (cp == NULL)
    165 		return ENOMEM;
    166 
    167 	error = x86_get_ldt1(l, &ua, cp);
    168 	*retval = ua.num;
    169 	if (error == 0)
    170 		error = copyout(cp, ua.desc, ua.num * sizeof(*cp));
    171 
    172 	free(cp, M_TEMP);
    173 	return error;
    174 #endif
    175 }
    176 
    177 int
    178 x86_get_ldt1(struct lwp *l, struct x86_get_ldt_args *ua, union descriptor *cp)
    179 {
    180 #ifndef USER_LDT
    181 	return EINVAL;
    182 #else
    183 	int error;
    184 	struct proc *p = l->l_proc;
    185 	pmap_t pmap = p->p_vmspace->vm_map.pmap;
    186 	int nldt, num;
    187 	union descriptor *lp;
    188 
    189 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_LDT_GET,
    190 	    NULL, NULL, NULL, NULL);
    191 	if (error)
    192 		return (error);
    193 
    194 #ifdef	LDT_DEBUG
    195 	printf("x86_get_ldt: start=%d num=%d descs=%p\n", ua->start,
    196 	    ua->num, ua->desc);
    197 #endif
    198 
    199 	if (ua->start < 0 || ua->num < 0 || ua->start > 8192 || ua->num > 8192 ||
    200 	    ua->start + ua->num > 8192)
    201 		return (EINVAL);
    202 
    203 	mutex_enter(&pmap->pm_lock);
    204 
    205 	if (pmap->pm_flags & PMF_USER_LDT) {
    206 		nldt = pmap->pm_ldt_len;
    207 		lp = pmap->pm_ldt;
    208 	} else {
    209 		nldt = NLDT;
    210 		lp = ldt;
    211 	}
    212 
    213 	if (ua->start > nldt) {
    214 		mutex_exit(&pmap->pm_lock);
    215 		return (EINVAL);
    216 	}
    217 
    218 	lp += ua->start;
    219 	num = min(ua->num, nldt - ua->start);
    220 	ua->num = num;
    221 #ifdef LDT_DEBUG
    222 	{
    223 		int i;
    224 		for (i = 0; i < num; i++)
    225 			x86_print_ldt(i, &lp[i].sd);
    226 	}
    227 #endif
    228 
    229 	memcpy(cp, lp, num * sizeof(union descriptor));
    230 	mutex_exit(&pmap->pm_lock);
    231 
    232 	return 0;
    233 #endif
    234 }
    235 
    236 int
    237 x86_set_ldt(struct lwp *l, void *args, register_t *retval)
    238 {
    239 #ifndef USER_LDT
    240 	return EINVAL;
    241 #else
    242 	struct x86_set_ldt_args ua;
    243 	union descriptor *descv;
    244 	int error;
    245 
    246 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    247 		return (error);
    248 
    249 	if (ua.num < 0 || ua.num > 8192)
    250 		return EINVAL;
    251 
    252 	descv = malloc(sizeof (*descv) * ua.num, M_TEMP, M_NOWAIT);
    253 	if (descv == NULL)
    254 		return ENOMEM;
    255 
    256 	error = copyin(ua.desc, descv, sizeof (*descv) * ua.num);
    257 	if (error == 0)
    258 		error = x86_set_ldt1(l, &ua, descv);
    259 	*retval = ua.start;
    260 
    261 	free(descv, M_TEMP);
    262 	return error;
    263 #endif
    264 }
    265 
    266 int
    267 x86_set_ldt1(struct lwp *l, struct x86_set_ldt_args *ua,
    268     union descriptor *descv)
    269 {
    270 #ifndef USER_LDT
    271 	return EINVAL;
    272 #else
    273 	int error, i, n, sel, free_sel;
    274 	struct proc *p = l->l_proc;
    275 	struct pcb *pcb = &l->l_addr->u_pcb;
    276 	pmap_t pmap = p->p_vmspace->vm_map.pmap;
    277 	size_t old_len, new_len, ldt_len, free_len;
    278 	union descriptor *old_ldt, *new_ldt, *free_ldt;
    279 
    280 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_LDT_SET,
    281 	    NULL, NULL, NULL, NULL);
    282 	if (error)
    283 		return (error);
    284 
    285 	if (ua->start < 0 || ua->num < 0 || ua->start > 8192 || ua->num > 8192 ||
    286 	    ua->start + ua->num > 8192)
    287 		return (EINVAL);
    288 
    289 	/* Check descriptors for access violations. */
    290 	for (i = 0; i < ua->num; i++) {
    291 		union descriptor *desc = &descv[i];
    292 
    293 		switch (desc->sd.sd_type) {
    294 		case SDT_SYSNULL:
    295 			desc->sd.sd_p = 0;
    296 			break;
    297 		case SDT_SYS286CGT:
    298 		case SDT_SYS386CGT:
    299 			/*
    300 			 * Only allow call gates targeting a segment
    301 			 * in the LDT or a user segment in the fixed
    302 			 * part of the gdt.  Segments in the LDT are
    303 			 * constrained (below) to be user segments.
    304 			 */
    305 			if (desc->gd.gd_p != 0 &&
    306 			    !ISLDT(desc->gd.gd_selector) &&
    307 			    ((IDXSEL(desc->gd.gd_selector) >= NGDT) ||
    308 			     (gdt[IDXSEL(desc->gd.gd_selector)].sd.sd_dpl !=
    309 				 SEL_UPL))) {
    310 				return EACCES;
    311 			}
    312 			break;
    313 		case SDT_MEMEC:
    314 		case SDT_MEMEAC:
    315 		case SDT_MEMERC:
    316 		case SDT_MEMERAC:
    317 			/* Must be "present" if executable and conforming. */
    318 			if (desc->sd.sd_p == 0)
    319 				return EACCES;
    320 			break;
    321 		case SDT_MEMRO:
    322 		case SDT_MEMROA:
    323 		case SDT_MEMRW:
    324 		case SDT_MEMRWA:
    325 		case SDT_MEMROD:
    326 		case SDT_MEMRODA:
    327 		case SDT_MEMRWD:
    328 		case SDT_MEMRWDA:
    329 		case SDT_MEME:
    330 		case SDT_MEMEA:
    331 		case SDT_MEMER:
    332 		case SDT_MEMERA:
    333 			break;
    334 		default:
    335 			/*
    336 			 * Make sure that unknown descriptor types are
    337 			 * not marked present.
    338 			 */
    339 			if (desc->sd.sd_p != 0)
    340 				return EACCES;
    341 			break;
    342 		}
    343 
    344 		if (desc->sd.sd_p != 0) {
    345 			/* Only user (ring-3) descriptors may be present. */
    346 			if (desc->sd.sd_dpl != SEL_UPL)
    347 				return EACCES;
    348 		}
    349 	}
    350 
    351 	/* allocate user ldt */
    352 	free_sel = -1;
    353 	new_ldt = NULL;
    354 	new_len = 0;
    355 	free_ldt = NULL;
    356 	free_len = 0;
    357 	mutex_enter(&pmap->pm_lock);
    358 	if (pmap->pm_ldt == 0 || (ua->start + ua->num) > pmap->pm_ldt_len) {
    359 		if (pmap->pm_flags & PMF_USER_LDT)
    360 			ldt_len = pmap->pm_ldt_len;
    361 		else
    362 			ldt_len = 512;
    363 		while ((ua->start + ua->num) > ldt_len)
    364 			ldt_len *= 2;
    365 		new_len = ldt_len * sizeof(union descriptor);
    366 
    367 		mutex_exit(&pmap->pm_lock);
    368 		new_ldt = (union descriptor *)uvm_km_alloc(kernel_map,
    369 		    new_len, 0, UVM_KMF_WIRED);
    370 		memset(new_ldt, 0, new_len);
    371 		sel = ldt_alloc(new_ldt, new_len);
    372 		mutex_enter(&pmap->pm_lock);
    373 
    374 		if (pmap->pm_ldt != NULL && ldt_len <= pmap->pm_ldt_len) {
    375 			/*
    376 			 * Another thread (re)allocated the LDT to
    377 			 * sufficient size while we were blocked in
    378 			 * uvm_km_alloc. Oh well. The new entries
    379 			 * will quite probably not be right, but
    380 			 * hey.. not our problem if user applications
    381 			 * have race conditions like that.
    382 			 */
    383 			goto copy;
    384 		}
    385 
    386 		old_ldt = pmap->pm_ldt;
    387 		free_ldt = old_ldt;
    388 		free_len = pmap->pm_ldt_len * sizeof(union descriptor);
    389 
    390 		if (old_ldt != NULL) {
    391 			old_len = pmap->pm_ldt_len * sizeof(union descriptor);
    392 		} else {
    393 			old_len = NLDT * sizeof(union descriptor);
    394 			old_ldt = ldt;
    395 		}
    396 
    397 		memcpy(new_ldt, old_ldt, old_len);
    398 		memset((char *)new_ldt + old_len, 0, new_len - old_len);
    399 
    400 		pmap->pm_ldt = new_ldt;
    401 		pmap->pm_ldt_len = ldt_len;
    402 
    403 		if (pmap->pm_flags & PMF_USER_LDT)
    404 			free_sel = pmap->pm_ldt_sel;
    405 		else {
    406 			pmap->pm_flags |= PMF_USER_LDT;
    407 			free_sel = -1;
    408 		}
    409 		pmap->pm_ldt_sel = sel;
    410 		pcb->pcb_ldt_sel = pmap->pm_ldt_sel;
    411 		if (pcb == curpcb)
    412 			lldt(pcb->pcb_ldt_sel);
    413 		new_ldt = NULL;
    414 	}
    415 copy:
    416 	/* Now actually replace the descriptors. */
    417 	for (i = 0, n = ua->start; i < ua->num; i++, n++)
    418 		pmap->pm_ldt[n] = descv[i];
    419 
    420 	mutex_exit(&pmap->pm_lock);
    421 
    422 	if (new_ldt != NULL)
    423 		uvm_km_free(kernel_map, (vaddr_t)new_ldt, new_len,
    424 		    UVM_KMF_WIRED);
    425 	if (free_sel != -1)
    426 		ldt_free(free_sel);
    427 	if (free_ldt != NULL)
    428 		uvm_km_free(kernel_map, (vaddr_t)free_ldt, free_len,
    429 		    UVM_KMF_WIRED);
    430 
    431 	return (error);
    432 #endif
    433 }
    434 
    435 int
    436 x86_iopl(struct lwp *l, void *args, register_t *retval)
    437 {
    438 	int error;
    439 	struct x86_iopl_args ua;
    440 #ifdef XEN
    441 	int iopl;
    442 #else
    443 	struct trapframe *tf = l->l_md.md_regs;
    444 #endif
    445 
    446 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPL,
    447 	    NULL, NULL, NULL, NULL);
    448 	if (error)
    449 		return (error);
    450 
    451 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    452 		return error;
    453 
    454 #ifdef XEN
    455 	if (ua.iopl)
    456 		iopl = SEL_UPL;
    457 	else
    458 		iopl = SEL_KPL;
    459 	/* Force the change at ring 0. */
    460 #ifdef XEN3
    461 	{
    462 		struct physdev_op physop;
    463 		physop.cmd = PHYSDEVOP_SET_IOPL;
    464 		physop.u.set_iopl.iopl = iopl;
    465 		HYPERVISOR_physdev_op(&physop);
    466 	}
    467 #else /* XEN3 */
    468 	{
    469 		dom0_op_t op;
    470 		op.cmd = DOM0_IOPL;
    471 		op.u.iopl.domain = DOMID_SELF;
    472 		op.u.iopl.iopl = iopl;
    473 		HYPERVISOR_dom0_op(&op);
    474 	}
    475 #endif /* XEN3 */
    476 #elif defined(__x86_64__)
    477 	if (ua.iopl)
    478 		tf->tf_rflags |= PSL_IOPL;
    479 	else
    480 		tf->tf_rflags &= ~PSL_IOPL;
    481 #else
    482 	if (ua.iopl)
    483 		tf->tf_eflags |= PSL_IOPL;
    484 	else
    485 		tf->tf_eflags &= ~PSL_IOPL;
    486 #endif
    487 
    488 	return 0;
    489 }
    490 
    491 int
    492 x86_get_ioperm(struct lwp *l, void *args, register_t *retval)
    493 {
    494 #ifdef IOPERM
    495 	int error;
    496 	struct pcb *pcb = &l->l_addr->u_pcb;
    497 	struct x86_get_ioperm_args ua;
    498 	void *dummymap = NULL;
    499 	void *iomap;
    500 
    501 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPERM_GET,
    502 	    NULL, NULL, NULL, NULL);
    503 	if (error)
    504 		return (error);
    505 
    506 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    507 		return (error);
    508 
    509 	iomap = pcb->pcb_iomap;
    510 	if (iomap == NULL) {
    511 		iomap = dummymap = kmem_alloc(IOMAPSIZE, KM_SLEEP);
    512 		memset(dummymap, 0xff, IOMAPSIZE);
    513 	}
    514 	error = copyout(iomap, ua.iomap, IOMAPSIZE);
    515 	if (dummymap != NULL) {
    516 		kmem_free(dummymap, IOMAPSIZE);
    517 	}
    518 	return error;
    519 #else
    520 	return EINVAL;
    521 #endif
    522 }
    523 
    524 int
    525 x86_set_ioperm(struct lwp *l, void *args, register_t *retval)
    526 {
    527 #ifdef IOPERM
    528 	struct cpu_info *ci;
    529 	int error;
    530 	struct pcb *pcb = &l->l_addr->u_pcb;
    531 	struct x86_set_ioperm_args ua;
    532 	void *new;
    533 	void *old;
    534 
    535   	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPERM_SET,
    536 	    NULL, NULL, NULL, NULL);
    537 	if (error)
    538 		return (error);
    539 
    540 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    541 		return (error);
    542 
    543 	new = kmem_alloc(IOMAPSIZE, KM_SLEEP);
    544 	error = copyin(ua.iomap, new, IOMAPSIZE);
    545 	if (error) {
    546 		kmem_free(new, IOMAPSIZE);
    547 		return error;
    548 	}
    549 	old = pcb->pcb_iomap;
    550 	pcb->pcb_iomap = new;
    551 	if (old != NULL) {
    552 		kmem_free(old, IOMAPSIZE);
    553 	}
    554 
    555 	crit_enter();
    556 	ci = curcpu();
    557 	memcpy(ci->ci_iomap, pcb->pcb_iomap, sizeof(ci->ci_iomap));
    558 	ci->ci_tss.tss_iobase =
    559 	    ((uintptr_t)ci->ci_iomap - (uintptr_t)&ci->ci_tss) << 16;
    560 	crit_exit();
    561 
    562 	return error;
    563 #else
    564 	return EINVAL;
    565 #endif
    566 }
    567 
    568 int
    569 x86_get_mtrr(struct lwp *l, void *args, register_t *retval)
    570 {
    571 #ifdef MTRR
    572 	struct x86_get_mtrr_args ua;
    573 	int error, n;
    574 
    575 	if (mtrr_funcs == NULL)
    576 		return ENOSYS;
    577 
    578  	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_MTRR_GET,
    579 	    NULL, NULL, NULL, NULL);
    580 	if (error)
    581 		return (error);
    582 
    583 	error = copyin(args, &ua, sizeof ua);
    584 	if (error != 0)
    585 		return error;
    586 
    587 	error = copyin(ua.n, &n, sizeof n);
    588 	if (error != 0)
    589 		return error;
    590 
    591 	error = mtrr_get(ua.mtrrp, &n, l->l_proc, MTRR_GETSET_USER);
    592 
    593 	copyout(&n, ua.n, sizeof (int));
    594 
    595 	return error;
    596 #else
    597 	return EINVAL;
    598 #endif
    599 }
    600 
    601 int
    602 x86_set_mtrr(struct lwp *l, void *args, register_t *retval)
    603 {
    604 #ifdef MTRR
    605 	int error, n;
    606 	struct x86_set_mtrr_args ua;
    607 
    608 	if (mtrr_funcs == NULL)
    609 		return ENOSYS;
    610 
    611  	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_MTRR_SET,
    612 	    NULL, NULL, NULL, NULL);
    613 	if (error)
    614 		return (error);
    615 
    616 	error = copyin(args, &ua, sizeof ua);
    617 	if (error != 0)
    618 		return error;
    619 
    620 	error = copyin(ua.n, &n, sizeof n);
    621 	if (error != 0)
    622 		return error;
    623 
    624 	error = mtrr_set(ua.mtrrp, &n, l->l_proc, MTRR_GETSET_USER);
    625 	if (n != 0)
    626 		mtrr_commit();
    627 
    628 	copyout(&n, ua.n, sizeof n);
    629 
    630 	return error;
    631 #else
    632 	return EINVAL;
    633 #endif
    634 }
    635 
    636 int
    637 x86_set_sdbase(void *arg, char which)
    638 {
    639 #ifdef i386
    640 	struct segment_descriptor sd;
    641 	vaddr_t base;
    642 	int error;
    643 
    644 	error = copyin(arg, &base, sizeof(base));
    645 	if (error != 0)
    646 		return error;
    647 
    648 	sd.sd_lobase = base & 0xffffff;
    649 	sd.sd_hibase = (base >> 24) & 0xff;
    650 	sd.sd_lolimit = 0xffff;
    651 	sd.sd_hilimit = 0xf;
    652 	sd.sd_type = SDT_MEMRWA;
    653 	sd.sd_dpl = SEL_UPL;
    654 	sd.sd_p = 1;
    655 	sd.sd_xx = 0;
    656 	sd.sd_def32 = 1;
    657 	sd.sd_gran = 1;
    658 
    659 	crit_enter();
    660 	if (which == 'f') {
    661 		memcpy(&curpcb->pcb_fsd, &sd, sizeof(sd));
    662 		memcpy(&curcpu()->ci_gdt[GUFS_SEL], &sd, sizeof(sd));
    663 	} else /* which == 'g' */ {
    664 		memcpy(&curpcb->pcb_gsd, &sd, sizeof(sd));
    665 		memcpy(&curcpu()->ci_gdt[GUGS_SEL], &sd, sizeof(sd));
    666 	}
    667 	crit_exit();
    668 
    669 	return 0;
    670 #else
    671 	return EINVAL;
    672 #endif
    673 }
    674 
    675 int
    676 x86_get_sdbase(void *arg, char which)
    677 {
    678 #ifdef i386
    679 	struct segment_descriptor *sd;
    680 	vaddr_t base;
    681 
    682 	switch (which) {
    683 	case 'f':
    684 		sd = (struct segment_descriptor *)&curpcb->pcb_fsd;
    685 		break;
    686 	case 'g':
    687 		sd = (struct segment_descriptor *)&curpcb->pcb_gsd;
    688 		break;
    689 	default:
    690 		panic("x86_get_sdbase");
    691 	}
    692 
    693 	base = sd->sd_hibase << 24 | sd->sd_lobase;
    694 	return copyout(&base, &arg, sizeof(base));
    695 #else
    696 	return EINVAL;
    697 #endif
    698 }
    699 
    700 int
    701 sys_sysarch(struct lwp *l, const struct sys_sysarch_args *uap, register_t *retval)
    702 {
    703 	/* {
    704 		syscallarg(int) op;
    705 		syscallarg(void *) parms;
    706 	} */
    707 	int error = 0;
    708 
    709 	switch(SCARG(uap, op)) {
    710 	case X86_IOPL:
    711 		error = x86_iopl(l, SCARG(uap, parms), retval);
    712 		break;
    713 
    714 	case X86_GET_LDT:
    715 		error = x86_get_ldt(l, SCARG(uap, parms), retval);
    716 		break;
    717 
    718 	case X86_SET_LDT:
    719 		error = x86_set_ldt(l, SCARG(uap, parms), retval);
    720 		break;
    721 
    722 	case X86_GET_IOPERM:
    723 		error = x86_get_ioperm(l, SCARG(uap, parms), retval);
    724 		break;
    725 
    726 	case X86_SET_IOPERM:
    727 		error = x86_set_ioperm(l, SCARG(uap, parms), retval);
    728 		break;
    729 
    730 	case X86_GET_MTRR:
    731 		error = x86_get_mtrr(l, SCARG(uap, parms), retval);
    732 		break;
    733 	case X86_SET_MTRR:
    734 		error = x86_set_mtrr(l, SCARG(uap, parms), retval);
    735 		break;
    736 
    737 #ifdef VM86
    738 	case X86_VM86:
    739 		error = x86_vm86(l, SCARG(uap, parms), retval);
    740 		break;
    741 #ifdef COMPAT_16
    742 	case X86_OLD_VM86:
    743 		error = compat_16_x86_vm86(l, SCARG(uap, parms), retval);
    744 		break;
    745 #endif
    746 #endif
    747 
    748 #ifdef PERFCTRS
    749 	case X86_PMC_INFO:
    750 		error = pmc_info(l, SCARG(uap, parms), retval);
    751 		break;
    752 
    753 	case X86_PMC_STARTSTOP:
    754 		error = pmc_startstop(l, SCARG(uap, parms), retval);
    755 		break;
    756 
    757 	case X86_PMC_READ:
    758 		error = pmc_read(l, SCARG(uap, parms), retval);
    759 		break;
    760 #endif
    761 
    762 	case X86_SET_FSBASE:
    763 		error = x86_set_sdbase(SCARG(uap, parms), 'f');
    764 		break;
    765 
    766 	case X86_SET_GSBASE:
    767 		error = x86_set_sdbase(SCARG(uap, parms), 'g');
    768 		break;
    769 
    770 	case X86_GET_FSBASE:
    771 		error = x86_get_sdbase(SCARG(uap, parms), 'f');
    772 		break;
    773 
    774 	case X86_GET_GSBASE:
    775 		error = x86_get_sdbase(SCARG(uap, parms), 'g');
    776 		break;
    777 
    778 	default:
    779 		error = EINVAL;
    780 		break;
    781 	}
    782 	return (error);
    783 }
    784