Home | History | Annotate | Line # | Download | only in x86
sys_machdep.c revision 1.43
      1 /*	$NetBSD: sys_machdep.c,v 1.43 2017/10/21 08:27:19 maxv Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2007, 2009, 2017 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, by Andrew Doran, and by Maxime Villard.
      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.43 2017/10/21 08:27:19 maxv Exp $");
     34 
     35 #include "opt_mtrr.h"
     36 #include "opt_pmc.h"
     37 #include "opt_user_ldt.h"
     38 #include "opt_compat_netbsd.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 #undef	IOPERM	/* not implemented */
     69 #else
     70 #if defined(XEN)
     71 #undef	IOPERM
     72 #else /* defined(XEN) */
     73 #define	IOPERM
     74 #endif /* defined(XEN) */
     75 #endif
     76 
     77 #ifdef XEN
     78 #undef	USER_LDT
     79 #undef	PMC
     80 #endif
     81 
     82 #ifdef PMC
     83 #include <machine/pmc.h>
     84 #endif
     85 
     86 extern struct vm_map *kernel_map;
     87 
     88 int x86_get_ioperm(struct lwp *, void *, register_t *);
     89 int x86_set_ioperm(struct lwp *, void *, register_t *);
     90 int x86_get_mtrr(struct lwp *, void *, register_t *);
     91 int x86_set_mtrr(struct lwp *, void *, register_t *);
     92 int x86_set_sdbase32(void *, char, lwp_t *, bool);
     93 int x86_set_sdbase(void *, char, lwp_t *, bool);
     94 int x86_get_sdbase32(void *, char);
     95 int x86_get_sdbase(void *, char);
     96 
     97 int
     98 x86_get_ldt(struct lwp *l, void *args, register_t *retval)
     99 {
    100 #ifndef USER_LDT
    101 	return EINVAL;
    102 #else
    103 	struct x86_get_ldt_args ua;
    104 	union descriptor *cp;
    105 	int error;
    106 
    107 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    108 		return error;
    109 
    110 	if (ua.num < 0 || ua.num > 8192)
    111 		return EINVAL;
    112 
    113 	cp = malloc(ua.num * sizeof(union descriptor), M_TEMP, M_WAITOK);
    114 	if (cp == NULL)
    115 		return ENOMEM;
    116 
    117 	error = x86_get_ldt1(l, &ua, cp);
    118 	*retval = ua.num;
    119 	if (error == 0)
    120 		error = copyout(cp, ua.desc, ua.num * sizeof(*cp));
    121 
    122 	free(cp, M_TEMP);
    123 	return error;
    124 #endif
    125 }
    126 
    127 int
    128 x86_get_ldt1(struct lwp *l, struct x86_get_ldt_args *ua, union descriptor *cp)
    129 {
    130 #ifndef USER_LDT
    131 	return EINVAL;
    132 #else
    133 	int error;
    134 	struct proc *p = l->l_proc;
    135 	pmap_t pmap = p->p_vmspace->vm_map.pmap;
    136 	int nldt, num;
    137 	union descriptor *lp;
    138 
    139 #ifdef __x86_64__
    140 	const size_t min_ldt_size = LDT_SIZE;
    141 #else
    142 	const size_t min_ldt_size = NLDT * sizeof(union descriptor);
    143 #endif
    144 
    145 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_LDT_GET,
    146 	    NULL, NULL, NULL, NULL);
    147 	if (error)
    148 		return error;
    149 
    150 	if (ua->start < 0 || ua->num < 0 || ua->start > 8192 || ua->num > 8192 ||
    151 	    ua->start + ua->num > 8192)
    152 		return EINVAL;
    153 
    154 	if (ua->start * sizeof(union descriptor) < min_ldt_size)
    155 		return EINVAL;
    156 
    157 	mutex_enter(&cpu_lock);
    158 
    159 	if (pmap->pm_ldt != NULL) {
    160 		nldt = pmap->pm_ldt_len / sizeof(*lp);
    161 		lp = pmap->pm_ldt;
    162 	} else {
    163 #ifdef __x86_64__
    164 		nldt = LDT_SIZE / sizeof(*lp);
    165 #else
    166 		nldt = NLDT;
    167 #endif
    168 		lp = (union descriptor *)ldtstore;
    169 	}
    170 
    171 	if (ua->start > nldt) {
    172 		mutex_exit(&cpu_lock);
    173 		return EINVAL;
    174 	}
    175 
    176 	lp += ua->start;
    177 	num = min(ua->num, nldt - ua->start);
    178 	ua->num = num;
    179 
    180 	memcpy(cp, lp, num * sizeof(union descriptor));
    181 	mutex_exit(&cpu_lock);
    182 
    183 	return 0;
    184 #endif
    185 }
    186 
    187 int
    188 x86_set_ldt(struct lwp *l, void *args, register_t *retval)
    189 {
    190 #ifndef USER_LDT
    191 	return EINVAL;
    192 #else
    193 	struct x86_set_ldt_args ua;
    194 	union descriptor *descv;
    195 	int error;
    196 
    197 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    198 		return error;
    199 
    200 	if (ua.num < 0 || ua.num > 8192)
    201 		return EINVAL;
    202 
    203 	descv = malloc(sizeof (*descv) * ua.num, M_TEMP, M_NOWAIT);
    204 	if (descv == NULL)
    205 		return ENOMEM;
    206 
    207 	error = copyin(ua.desc, descv, sizeof (*descv) * ua.num);
    208 	if (error == 0)
    209 		error = x86_set_ldt1(l, &ua, descv);
    210 	*retval = ua.start;
    211 
    212 	free(descv, M_TEMP);
    213 	return error;
    214 #endif
    215 }
    216 
    217 int
    218 x86_set_ldt1(struct lwp *l, struct x86_set_ldt_args *ua,
    219     union descriptor *descv)
    220 {
    221 #ifndef USER_LDT
    222 	return EINVAL;
    223 #else
    224 	int error, i, n, old_sel, new_sel;
    225 	struct proc *p = l->l_proc;
    226 	pmap_t pmap = p->p_vmspace->vm_map.pmap;
    227 	size_t old_len, new_len;
    228 	union descriptor *old_ldt, *new_ldt;
    229 
    230 #ifdef __x86_64__
    231 	const size_t min_ldt_size = LDT_SIZE;
    232 #else
    233 	const size_t min_ldt_size = NLDT * sizeof(union descriptor);
    234 #endif
    235 
    236 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_LDT_SET,
    237 	    NULL, NULL, NULL, NULL);
    238 	if (error)
    239 		return error;
    240 
    241 	if (ua->start < 0 || ua->num < 0 || ua->start > 8192 || ua->num > 8192 ||
    242 	    ua->start + ua->num > 8192)
    243 		return EINVAL;
    244 
    245 	if (ua->start * sizeof(union descriptor) < min_ldt_size)
    246 		return EINVAL;
    247 
    248 	/* Check descriptors for access violations. */
    249 	for (i = 0; i < ua->num; i++) {
    250 		union descriptor *desc = &descv[i];
    251 
    252 #ifdef __x86_64__
    253 		if (desc->sd.sd_long != 0)
    254 			return EACCES;
    255 #endif
    256 
    257 		switch (desc->sd.sd_type) {
    258 		case SDT_SYSNULL:
    259 			desc->sd.sd_p = 0;
    260 			break;
    261 		case SDT_MEMEC:
    262 		case SDT_MEMEAC:
    263 		case SDT_MEMERC:
    264 		case SDT_MEMERAC:
    265 			/* Must be "present" if executable and conforming. */
    266 			if (desc->sd.sd_p == 0)
    267 				return EACCES;
    268 			break;
    269 		case SDT_MEMRO:
    270 		case SDT_MEMROA:
    271 		case SDT_MEMRW:
    272 		case SDT_MEMRWA:
    273 		case SDT_MEMROD:
    274 		case SDT_MEMRODA:
    275 		case SDT_MEMRWD:
    276 		case SDT_MEMRWDA:
    277 		case SDT_MEME:
    278 		case SDT_MEMEA:
    279 		case SDT_MEMER:
    280 		case SDT_MEMERA:
    281 			break;
    282 		default:
    283 			return EACCES;
    284 		}
    285 
    286 		if (desc->sd.sd_p != 0) {
    287 			/* Only user (ring-3) descriptors may be present. */
    288 			if (desc->sd.sd_dpl != SEL_UPL)
    289 				return EACCES;
    290 		}
    291 	}
    292 
    293 	/*
    294 	 * Install selected changes.  We perform a copy, write, swap dance
    295 	 * here to ensure that all updates happen atomically.
    296 	 */
    297 
    298 	/* Allocate a new LDT. */
    299 	for (;;) {
    300 		new_len = (ua->start + ua->num) * sizeof(union descriptor);
    301 		new_len = max(new_len, pmap->pm_ldt_len);
    302 		new_len = max(new_len, min_ldt_size);
    303 		new_len = round_page(new_len);
    304 		new_ldt = (union descriptor *)uvm_km_alloc(kernel_map,
    305 		    new_len, 0, UVM_KMF_WIRED | UVM_KMF_ZERO | UVM_KMF_WAITVA);
    306 		mutex_enter(&cpu_lock);
    307 		if (pmap->pm_ldt_len <= new_len) {
    308 			break;
    309 		}
    310 		mutex_exit(&cpu_lock);
    311 		uvm_km_free(kernel_map, (vaddr_t)new_ldt, new_len,
    312 		    UVM_KMF_WIRED);
    313 	}
    314 
    315 	/* Copy existing entries, if any. */
    316 	if (pmap->pm_ldt != NULL) {
    317 		old_ldt = pmap->pm_ldt;
    318 		old_len = pmap->pm_ldt_len;
    319 		old_sel = pmap->pm_ldt_sel;
    320 		memcpy(new_ldt, old_ldt, old_len);
    321 	} else {
    322 		old_ldt = NULL;
    323 		old_len = 0;
    324 		old_sel = -1;
    325 		memcpy(new_ldt, ldtstore, min_ldt_size);
    326 	}
    327 
    328 	/* Apply requested changes. */
    329 	for (i = 0, n = ua->start; i < ua->num; i++, n++) {
    330 		new_ldt[n] = descv[i];
    331 	}
    332 
    333 	/* Allocate LDT selector. */
    334 	new_sel = ldt_alloc(new_ldt, new_len);
    335 	if (new_sel == -1) {
    336 		mutex_exit(&cpu_lock);
    337 		uvm_km_free(kernel_map, (vaddr_t)new_ldt, new_len,
    338 		    UVM_KMF_WIRED);
    339 		return ENOMEM;
    340 	}
    341 
    342 	/* All changes are now globally visible.  Swap in the new LDT. */
    343 	pmap->pm_ldt_len = new_len;
    344 	pmap->pm_ldt_sel = new_sel;
    345 	/* membar_store_store for pmap_fork() to read these unlocked safely */
    346 	membar_producer();
    347 	pmap->pm_ldt = new_ldt;
    348 
    349 	/* Switch existing users onto new LDT. */
    350 	pmap_ldt_sync(pmap);
    351 
    352 	/* Free existing LDT (if any). */
    353 	if (old_ldt != NULL) {
    354 		ldt_free(old_sel);
    355 		/* exit the mutex before free */
    356 		mutex_exit(&cpu_lock);
    357 		uvm_km_free(kernel_map, (vaddr_t)old_ldt, old_len,
    358 		    UVM_KMF_WIRED);
    359 	} else {
    360 		mutex_exit(&cpu_lock);
    361 	}
    362 
    363 	return error;
    364 #endif
    365 }
    366 
    367 int
    368 x86_iopl(struct lwp *l, void *args, register_t *retval)
    369 {
    370 	int error;
    371 	struct x86_iopl_args ua;
    372 #ifdef XEN
    373 	int iopl;
    374 #else
    375 	struct trapframe *tf = l->l_md.md_regs;
    376 #endif
    377 
    378 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPL,
    379 	    NULL, NULL, NULL, NULL);
    380 	if (error)
    381 		return error;
    382 
    383 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    384 		return error;
    385 
    386 #ifdef XEN
    387 	if (ua.iopl)
    388 		iopl = SEL_UPL;
    389 	else
    390 		iopl = SEL_KPL;
    391 
    392     {
    393 	struct physdev_op physop;
    394 	struct pcb *pcb;
    395 
    396 	pcb = lwp_getpcb(l);
    397 	pcb->pcb_iopl = iopl;
    398 
    399 	/* Force the change at ring 0. */
    400 	physop.cmd = PHYSDEVOP_SET_IOPL;
    401 	physop.u.set_iopl.iopl = iopl;
    402 	HYPERVISOR_physdev_op(&physop);
    403     }
    404 #elif defined(__x86_64__)
    405 	if (ua.iopl)
    406 		tf->tf_rflags |= PSL_IOPL;
    407 	else
    408 		tf->tf_rflags &= ~PSL_IOPL;
    409 #else
    410 	if (ua.iopl)
    411 		tf->tf_eflags |= PSL_IOPL;
    412 	else
    413 		tf->tf_eflags &= ~PSL_IOPL;
    414 #endif
    415 
    416 	return 0;
    417 }
    418 
    419 int
    420 x86_get_ioperm(struct lwp *l, void *args, register_t *retval)
    421 {
    422 #ifdef IOPERM
    423 	int error;
    424 	struct pcb *pcb = lwp_getpcb(l);
    425 	struct x86_get_ioperm_args ua;
    426 	void *dummymap = NULL;
    427 	void *iomap;
    428 
    429 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPERM_GET,
    430 	    NULL, NULL, NULL, NULL);
    431 	if (error)
    432 		return error;
    433 
    434 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    435 		return error;
    436 
    437 	iomap = pcb->pcb_iomap;
    438 	if (iomap == NULL) {
    439 		iomap = dummymap = kmem_alloc(IOMAPSIZE, KM_SLEEP);
    440 		memset(dummymap, 0xff, IOMAPSIZE);
    441 	}
    442 	error = copyout(iomap, ua.iomap, IOMAPSIZE);
    443 	if (dummymap != NULL) {
    444 		kmem_free(dummymap, IOMAPSIZE);
    445 	}
    446 	return error;
    447 #else
    448 	return EINVAL;
    449 #endif
    450 }
    451 
    452 int
    453 x86_set_ioperm(struct lwp *l, void *args, register_t *retval)
    454 {
    455 #ifdef IOPERM
    456 	struct cpu_info *ci;
    457 	int error;
    458 	struct pcb *pcb = lwp_getpcb(l);
    459 	struct x86_set_ioperm_args ua;
    460 	void *new;
    461 	void *old;
    462 
    463   	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPERM_SET,
    464 	    NULL, NULL, NULL, NULL);
    465 	if (error)
    466 		return error;
    467 
    468 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    469 		return error;
    470 
    471 	new = kmem_alloc(IOMAPSIZE, KM_SLEEP);
    472 	error = copyin(ua.iomap, new, IOMAPSIZE);
    473 	if (error) {
    474 		kmem_free(new, IOMAPSIZE);
    475 		return error;
    476 	}
    477 	old = pcb->pcb_iomap;
    478 	pcb->pcb_iomap = new;
    479 	if (old != NULL) {
    480 		kmem_free(old, IOMAPSIZE);
    481 	}
    482 
    483 	kpreempt_disable();
    484 	ci = curcpu();
    485 	memcpy(ci->ci_iomap, pcb->pcb_iomap, sizeof(ci->ci_iomap));
    486 	ci->ci_tss.tss_iobase =
    487 	    ((uintptr_t)ci->ci_iomap - (uintptr_t)&ci->ci_tss) << 16;
    488 	kpreempt_enable();
    489 
    490 	return error;
    491 #else
    492 	return EINVAL;
    493 #endif
    494 }
    495 
    496 int
    497 x86_get_mtrr(struct lwp *l, void *args, register_t *retval)
    498 {
    499 #ifdef MTRR
    500 	struct x86_get_mtrr_args ua;
    501 	int error, n;
    502 
    503 	if (mtrr_funcs == NULL)
    504 		return ENOSYS;
    505 
    506  	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_MTRR_GET,
    507 	    NULL, NULL, NULL, NULL);
    508 	if (error)
    509 		return error;
    510 
    511 	error = copyin(args, &ua, sizeof ua);
    512 	if (error != 0)
    513 		return error;
    514 
    515 	error = copyin(ua.n, &n, sizeof n);
    516 	if (error != 0)
    517 		return error;
    518 
    519 	KERNEL_LOCK(1, NULL);
    520 	error = mtrr_get(ua.mtrrp, &n, l->l_proc, MTRR_GETSET_USER);
    521 	KERNEL_UNLOCK_ONE(NULL);
    522 
    523 	copyout(&n, ua.n, sizeof (int));
    524 
    525 	return error;
    526 #else
    527 	return EINVAL;
    528 #endif
    529 }
    530 
    531 int
    532 x86_set_mtrr(struct lwp *l, void *args, register_t *retval)
    533 {
    534 #ifdef MTRR
    535 	int error, n;
    536 	struct x86_set_mtrr_args ua;
    537 
    538 	if (mtrr_funcs == NULL)
    539 		return ENOSYS;
    540 
    541  	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_MTRR_SET,
    542 	    NULL, NULL, NULL, NULL);
    543 	if (error)
    544 		return error;
    545 
    546 	error = copyin(args, &ua, sizeof ua);
    547 	if (error != 0)
    548 		return error;
    549 
    550 	error = copyin(ua.n, &n, sizeof n);
    551 	if (error != 0)
    552 		return error;
    553 
    554 	KERNEL_LOCK(1, NULL);
    555 	error = mtrr_set(ua.mtrrp, &n, l->l_proc, MTRR_GETSET_USER);
    556 	if (n != 0)
    557 		mtrr_commit();
    558 	KERNEL_UNLOCK_ONE(NULL);
    559 
    560 	copyout(&n, ua.n, sizeof n);
    561 
    562 	return error;
    563 #else
    564 	return EINVAL;
    565 #endif
    566 }
    567 
    568 #ifdef __x86_64__
    569 #define pcb_fsd pcb_fs
    570 #define pcb_gsd pcb_gs
    571 #define segment_descriptor mem_segment_descriptor
    572 #endif
    573 
    574 int
    575 x86_set_sdbase32(void *arg, char which, lwp_t *l, bool direct)
    576 {
    577 	struct trapframe *tf = l->l_md.md_regs;
    578 	union descriptor usd;
    579 	struct pcb *pcb;
    580 	uint32_t base;
    581 	int error;
    582 
    583 	if (direct) {
    584 		base = (vaddr_t)arg;
    585 	} else {
    586 		error = copyin(arg, &base, sizeof(base));
    587 		if (error != 0)
    588 			return error;
    589 	}
    590 
    591 	memset(&usd, 0, sizeof(usd));
    592 	usd.sd.sd_lobase = base & 0xffffff;
    593 	usd.sd.sd_hibase = (base >> 24) & 0xff;
    594 	usd.sd.sd_lolimit = 0xffff;
    595 	usd.sd.sd_hilimit = 0xf;
    596 	usd.sd.sd_type = SDT_MEMRWA;
    597 	usd.sd.sd_dpl = SEL_UPL;
    598 	usd.sd.sd_p = 1;
    599 	usd.sd.sd_def32 = 1;
    600 	usd.sd.sd_gran = 1;
    601 
    602 	pcb = lwp_getpcb(l);
    603 	kpreempt_disable();
    604 	if (which == 'f') {
    605 		memcpy(&pcb->pcb_fsd, &usd.sd,
    606 		    sizeof(struct segment_descriptor));
    607 		if (l == curlwp) {
    608 			update_descriptor(&curcpu()->ci_gdt[GUFS_SEL], &usd);
    609 		}
    610 		tf->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
    611 	} else /* which == 'g' */ {
    612 		memcpy(&pcb->pcb_gsd, &usd.sd,
    613 		    sizeof(struct segment_descriptor));
    614 		if (l == curlwp) {
    615 			update_descriptor(&curcpu()->ci_gdt[GUGS_SEL], &usd);
    616 #if defined(__x86_64__) && defined(XEN)
    617 			setusergs(GSEL(GUGS_SEL, SEL_UPL));
    618 #endif
    619 		}
    620 		tf->tf_gs = GSEL(GUGS_SEL, SEL_UPL);
    621 	}
    622 	kpreempt_enable();
    623 	return 0;
    624 }
    625 
    626 int
    627 x86_set_sdbase(void *arg, char which, lwp_t *l, bool direct)
    628 {
    629 #ifdef i386
    630 	return x86_set_sdbase32(arg, which, l, direct);
    631 #else
    632 	struct pcb *pcb;
    633 	vaddr_t base;
    634 
    635 	if (l->l_proc->p_flag & PK_32) {
    636 		return x86_set_sdbase32(arg, which, l, direct);
    637 	}
    638 
    639 	if (direct) {
    640 		base = (vaddr_t)arg;
    641 	} else {
    642 		int error = copyin(arg, &base, sizeof(base));
    643 		if (error != 0)
    644 			return error;
    645 	}
    646 
    647 	if (base >= VM_MAXUSER_ADDRESS)
    648 		return EINVAL;
    649 
    650 	pcb = lwp_getpcb(l);
    651 
    652 	kpreempt_disable();
    653 	switch(which) {
    654 	case 'f':
    655 		pcb->pcb_fs = base;
    656 		if (l == curlwp)
    657 			wrmsr(MSR_FSBASE, pcb->pcb_fs);
    658 		break;
    659 	case 'g':
    660 		pcb->pcb_gs = base;
    661 		if (l == curlwp)
    662 			wrmsr(MSR_KERNELGSBASE, pcb->pcb_gs);
    663 		break;
    664 	default:
    665 		panic("x86_set_sdbase");
    666 	}
    667 	kpreempt_enable();
    668 
    669 	return 0;
    670 #endif
    671 }
    672 
    673 int
    674 x86_get_sdbase32(void *arg, char which)
    675 {
    676 	struct segment_descriptor *sd;
    677 	uint32_t base;
    678 
    679 	switch (which) {
    680 	case 'f':
    681 		sd = (void *)&curpcb->pcb_fsd;
    682 		break;
    683 	case 'g':
    684 		sd = (void *)&curpcb->pcb_gsd;
    685 		break;
    686 	default:
    687 		panic("x86_get_sdbase32");
    688 	}
    689 
    690 	base = sd->sd_hibase << 24 | sd->sd_lobase;
    691 	return copyout(&base, arg, sizeof(base));
    692 }
    693 
    694 int
    695 x86_get_sdbase(void *arg, char which)
    696 {
    697 #ifdef i386
    698 	return x86_get_sdbase32(arg, which);
    699 #else
    700 	vaddr_t base;
    701 	struct pcb *pcb;
    702 
    703 	if (curproc->p_flag & PK_32) {
    704 		return x86_get_sdbase32(arg, which);
    705 	}
    706 
    707 	pcb = lwp_getpcb(curlwp);
    708 
    709 	switch(which) {
    710 	case 'f':
    711 		base = pcb->pcb_fs;
    712 		break;
    713 	case 'g':
    714 		base = pcb->pcb_gs;
    715 		break;
    716 	default:
    717 		panic("x86_get_sdbase");
    718 	}
    719 
    720 	return copyout(&base, arg, sizeof(base));
    721 #endif
    722 }
    723 
    724 int
    725 sys_sysarch(struct lwp *l, const struct sys_sysarch_args *uap, register_t *retval)
    726 {
    727 	/* {
    728 		syscallarg(int) op;
    729 		syscallarg(void *) parms;
    730 	} */
    731 	int error = 0;
    732 
    733 	switch(SCARG(uap, op)) {
    734 	case X86_IOPL:
    735 		error = x86_iopl(l, SCARG(uap, parms), retval);
    736 		break;
    737 
    738 #ifdef i386
    739 	/*
    740 	 * On amd64, this is done via netbsd32_sysarch.
    741 	 */
    742 	case X86_GET_LDT:
    743 		error = x86_get_ldt(l, SCARG(uap, parms), retval);
    744 		break;
    745 
    746 	case X86_SET_LDT:
    747 		error = x86_set_ldt(l, SCARG(uap, parms), retval);
    748 		break;
    749 #endif
    750 
    751 	case X86_GET_IOPERM:
    752 		error = x86_get_ioperm(l, SCARG(uap, parms), retval);
    753 		break;
    754 
    755 	case X86_SET_IOPERM:
    756 		error = x86_set_ioperm(l, SCARG(uap, parms), retval);
    757 		break;
    758 
    759 	case X86_GET_MTRR:
    760 		error = x86_get_mtrr(l, SCARG(uap, parms), retval);
    761 		break;
    762 	case X86_SET_MTRR:
    763 		error = x86_set_mtrr(l, SCARG(uap, parms), retval);
    764 		break;
    765 
    766 #ifdef PMC
    767 	case X86_PMC_INFO:
    768 		error = sys_pmc_info(l, SCARG(uap, parms), retval);
    769 		break;
    770 
    771 	case X86_PMC_STARTSTOP:
    772 		error = sys_pmc_startstop(l, SCARG(uap, parms), retval);
    773 		break;
    774 
    775 	case X86_PMC_READ:
    776 		error = sys_pmc_read(l, SCARG(uap, parms), retval);
    777 		break;
    778 #endif
    779 
    780 	case X86_SET_FSBASE:
    781 		error = x86_set_sdbase(SCARG(uap, parms), 'f', curlwp, false);
    782 		break;
    783 
    784 	case X86_SET_GSBASE:
    785 		error = x86_set_sdbase(SCARG(uap, parms), 'g', curlwp, false);
    786 		break;
    787 
    788 	case X86_GET_FSBASE:
    789 		error = x86_get_sdbase(SCARG(uap, parms), 'f');
    790 		break;
    791 
    792 	case X86_GET_GSBASE:
    793 		error = x86_get_sdbase(SCARG(uap, parms), 'g');
    794 		break;
    795 
    796 	default:
    797 		error = EINVAL;
    798 		break;
    799 	}
    800 	return error;
    801 }
    802 
    803 int
    804 cpu_lwp_setprivate(lwp_t *l, void *addr)
    805 {
    806 
    807 #ifdef __x86_64__
    808 	if ((l->l_proc->p_flag & PK_32) == 0) {
    809 		return x86_set_sdbase(addr, 'f', l, true);
    810 	}
    811 #endif
    812 	return x86_set_sdbase(addr, 'g', l, true);
    813 }
    814