Home | History | Annotate | Line # | Download | only in x86
sys_machdep.c revision 1.10
      1 /*	$NetBSD: sys_machdep.c,v 1.10 2008/01/05 21:47:18 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.10 2008/01/05 21:47:18 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 	l->l_addr->u_pcb.pcb_iopl = iopl;
    460 	/* Force the change at ring 0. */
    461 #ifdef XEN3
    462 	{
    463 		struct physdev_op physop;
    464 		physop.cmd = PHYSDEVOP_SET_IOPL;
    465 		physop.u.set_iopl.iopl = iopl;
    466 		HYPERVISOR_physdev_op(&physop);
    467 	}
    468 #else /* XEN3 */
    469 	{
    470 		dom0_op_t op;
    471 		op.cmd = DOM0_IOPL;
    472 		op.u.iopl.domain = DOMID_SELF;
    473 		op.u.iopl.iopl = iopl;
    474 		HYPERVISOR_dom0_op(&op);
    475 	}
    476 #endif /* XEN3 */
    477 #elif defined(__x86_64__)
    478 	if (ua.iopl)
    479 		tf->tf_rflags |= PSL_IOPL;
    480 	else
    481 		tf->tf_rflags &= ~PSL_IOPL;
    482 #else
    483 	if (ua.iopl)
    484 		tf->tf_eflags |= PSL_IOPL;
    485 	else
    486 		tf->tf_eflags &= ~PSL_IOPL;
    487 #endif
    488 
    489 	return 0;
    490 }
    491 
    492 int
    493 x86_get_ioperm(struct lwp *l, void *args, register_t *retval)
    494 {
    495 #ifdef IOPERM
    496 	int error;
    497 	struct pcb *pcb = &l->l_addr->u_pcb;
    498 	struct x86_get_ioperm_args ua;
    499 	void *dummymap = NULL;
    500 	void *iomap;
    501 
    502 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPERM_GET,
    503 	    NULL, NULL, NULL, NULL);
    504 	if (error)
    505 		return (error);
    506 
    507 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    508 		return (error);
    509 
    510 	iomap = pcb->pcb_iomap;
    511 	if (iomap == NULL) {
    512 		iomap = dummymap = kmem_alloc(IOMAPSIZE, KM_SLEEP);
    513 		memset(dummymap, 0xff, IOMAPSIZE);
    514 	}
    515 	error = copyout(iomap, ua.iomap, IOMAPSIZE);
    516 	if (dummymap != NULL) {
    517 		kmem_free(dummymap, IOMAPSIZE);
    518 	}
    519 	return error;
    520 #else
    521 	return EINVAL;
    522 #endif
    523 }
    524 
    525 int
    526 x86_set_ioperm(struct lwp *l, void *args, register_t *retval)
    527 {
    528 #ifdef IOPERM
    529 	struct cpu_info *ci;
    530 	int error;
    531 	struct pcb *pcb = &l->l_addr->u_pcb;
    532 	struct x86_set_ioperm_args ua;
    533 	void *new;
    534 	void *old;
    535 
    536   	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPERM_SET,
    537 	    NULL, NULL, NULL, NULL);
    538 	if (error)
    539 		return (error);
    540 
    541 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    542 		return (error);
    543 
    544 	new = kmem_alloc(IOMAPSIZE, KM_SLEEP);
    545 	error = copyin(ua.iomap, new, IOMAPSIZE);
    546 	if (error) {
    547 		kmem_free(new, IOMAPSIZE);
    548 		return error;
    549 	}
    550 	old = pcb->pcb_iomap;
    551 	pcb->pcb_iomap = new;
    552 	if (old != NULL) {
    553 		kmem_free(old, IOMAPSIZE);
    554 	}
    555 
    556 	crit_enter();
    557 	ci = curcpu();
    558 	memcpy(ci->ci_iomap, pcb->pcb_iomap, sizeof(ci->ci_iomap));
    559 	ci->ci_tss.tss_iobase =
    560 	    ((uintptr_t)ci->ci_iomap - (uintptr_t)&ci->ci_tss) << 16;
    561 	crit_exit();
    562 
    563 	return error;
    564 #else
    565 	return EINVAL;
    566 #endif
    567 }
    568 
    569 int
    570 x86_get_mtrr(struct lwp *l, void *args, register_t *retval)
    571 {
    572 #ifdef MTRR
    573 	struct x86_get_mtrr_args ua;
    574 	int error, n;
    575 
    576 	if (mtrr_funcs == NULL)
    577 		return ENOSYS;
    578 
    579  	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_MTRR_GET,
    580 	    NULL, NULL, NULL, NULL);
    581 	if (error)
    582 		return (error);
    583 
    584 	error = copyin(args, &ua, sizeof ua);
    585 	if (error != 0)
    586 		return error;
    587 
    588 	error = copyin(ua.n, &n, sizeof n);
    589 	if (error != 0)
    590 		return error;
    591 
    592 	error = mtrr_get(ua.mtrrp, &n, l->l_proc, MTRR_GETSET_USER);
    593 
    594 	copyout(&n, ua.n, sizeof (int));
    595 
    596 	return error;
    597 #else
    598 	return EINVAL;
    599 #endif
    600 }
    601 
    602 int
    603 x86_set_mtrr(struct lwp *l, void *args, register_t *retval)
    604 {
    605 #ifdef MTRR
    606 	int error, n;
    607 	struct x86_set_mtrr_args ua;
    608 
    609 	if (mtrr_funcs == NULL)
    610 		return ENOSYS;
    611 
    612  	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_MTRR_SET,
    613 	    NULL, NULL, NULL, NULL);
    614 	if (error)
    615 		return (error);
    616 
    617 	error = copyin(args, &ua, sizeof ua);
    618 	if (error != 0)
    619 		return error;
    620 
    621 	error = copyin(ua.n, &n, sizeof n);
    622 	if (error != 0)
    623 		return error;
    624 
    625 	error = mtrr_set(ua.mtrrp, &n, l->l_proc, MTRR_GETSET_USER);
    626 	if (n != 0)
    627 		mtrr_commit();
    628 
    629 	copyout(&n, ua.n, sizeof n);
    630 
    631 	return error;
    632 #else
    633 	return EINVAL;
    634 #endif
    635 }
    636 
    637 int
    638 x86_set_sdbase(void *arg, char which)
    639 {
    640 #ifdef i386
    641 	struct segment_descriptor sd;
    642 	vaddr_t base;
    643 	int error;
    644 
    645 	error = copyin(arg, &base, sizeof(base));
    646 	if (error != 0)
    647 		return error;
    648 
    649 	sd.sd_lobase = base & 0xffffff;
    650 	sd.sd_hibase = (base >> 24) & 0xff;
    651 	sd.sd_lolimit = 0xffff;
    652 	sd.sd_hilimit = 0xf;
    653 	sd.sd_type = SDT_MEMRWA;
    654 	sd.sd_dpl = SEL_UPL;
    655 	sd.sd_p = 1;
    656 	sd.sd_xx = 0;
    657 	sd.sd_def32 = 1;
    658 	sd.sd_gran = 1;
    659 
    660 	crit_enter();
    661 	if (which == 'f') {
    662 		memcpy(&curpcb->pcb_fsd, &sd, sizeof(sd));
    663 		memcpy(&curcpu()->ci_gdt[GUFS_SEL], &sd, sizeof(sd));
    664 	} else /* which == 'g' */ {
    665 		memcpy(&curpcb->pcb_gsd, &sd, sizeof(sd));
    666 		memcpy(&curcpu()->ci_gdt[GUGS_SEL], &sd, sizeof(sd));
    667 	}
    668 	crit_exit();
    669 
    670 	return 0;
    671 #else
    672 	return EINVAL;
    673 #endif
    674 }
    675 
    676 int
    677 x86_get_sdbase(void *arg, char which)
    678 {
    679 #ifdef i386
    680 	struct segment_descriptor *sd;
    681 	vaddr_t base;
    682 
    683 	switch (which) {
    684 	case 'f':
    685 		sd = (struct segment_descriptor *)&curpcb->pcb_fsd;
    686 		break;
    687 	case 'g':
    688 		sd = (struct segment_descriptor *)&curpcb->pcb_gsd;
    689 		break;
    690 	default:
    691 		panic("x86_get_sdbase");
    692 	}
    693 
    694 	base = sd->sd_hibase << 24 | sd->sd_lobase;
    695 	return copyout(&base, &arg, sizeof(base));
    696 #else
    697 	return EINVAL;
    698 #endif
    699 }
    700 
    701 int
    702 sys_sysarch(struct lwp *l, const struct sys_sysarch_args *uap, register_t *retval)
    703 {
    704 	/* {
    705 		syscallarg(int) op;
    706 		syscallarg(void *) parms;
    707 	} */
    708 	int error = 0;
    709 
    710 	switch(SCARG(uap, op)) {
    711 	case X86_IOPL:
    712 		error = x86_iopl(l, SCARG(uap, parms), retval);
    713 		break;
    714 
    715 	case X86_GET_LDT:
    716 		error = x86_get_ldt(l, SCARG(uap, parms), retval);
    717 		break;
    718 
    719 	case X86_SET_LDT:
    720 		error = x86_set_ldt(l, SCARG(uap, parms), retval);
    721 		break;
    722 
    723 	case X86_GET_IOPERM:
    724 		error = x86_get_ioperm(l, SCARG(uap, parms), retval);
    725 		break;
    726 
    727 	case X86_SET_IOPERM:
    728 		error = x86_set_ioperm(l, SCARG(uap, parms), retval);
    729 		break;
    730 
    731 	case X86_GET_MTRR:
    732 		error = x86_get_mtrr(l, SCARG(uap, parms), retval);
    733 		break;
    734 	case X86_SET_MTRR:
    735 		error = x86_set_mtrr(l, SCARG(uap, parms), retval);
    736 		break;
    737 
    738 #ifdef VM86
    739 	case X86_VM86:
    740 		error = x86_vm86(l, SCARG(uap, parms), retval);
    741 		break;
    742 #ifdef COMPAT_16
    743 	case X86_OLD_VM86:
    744 		error = compat_16_x86_vm86(l, SCARG(uap, parms), retval);
    745 		break;
    746 #endif
    747 #endif
    748 
    749 #ifdef PERFCTRS
    750 	case X86_PMC_INFO:
    751 		error = pmc_info(l, SCARG(uap, parms), retval);
    752 		break;
    753 
    754 	case X86_PMC_STARTSTOP:
    755 		error = pmc_startstop(l, SCARG(uap, parms), retval);
    756 		break;
    757 
    758 	case X86_PMC_READ:
    759 		error = pmc_read(l, SCARG(uap, parms), retval);
    760 		break;
    761 #endif
    762 
    763 	case X86_SET_FSBASE:
    764 		error = x86_set_sdbase(SCARG(uap, parms), 'f');
    765 		break;
    766 
    767 	case X86_SET_GSBASE:
    768 		error = x86_set_sdbase(SCARG(uap, parms), 'g');
    769 		break;
    770 
    771 	case X86_GET_FSBASE:
    772 		error = x86_get_sdbase(SCARG(uap, parms), 'f');
    773 		break;
    774 
    775 	case X86_GET_GSBASE:
    776 		error = x86_get_sdbase(SCARG(uap, parms), 'g');
    777 		break;
    778 
    779 	default:
    780 		error = EINVAL;
    781 		break;
    782 	}
    783 	return (error);
    784 }
    785