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