Home | History | Annotate | Line # | Download | only in x86
sys_machdep.c revision 1.42
      1 /*	$NetBSD: sys_machdep.c,v 1.42 2017/10/21 06:55:54 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.42 2017/10/21 06:55:54 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 		switch (desc->sd.sd_type) {
    253 		case SDT_SYSNULL:
    254 			desc->sd.sd_p = 0;
    255 			break;
    256 		case SDT_MEMEC:
    257 		case SDT_MEMEAC:
    258 		case SDT_MEMERC:
    259 		case SDT_MEMERAC:
    260 			/* Must be "present" if executable and conforming. */
    261 			if (desc->sd.sd_p == 0)
    262 				return EACCES;
    263 			break;
    264 		case SDT_MEMRO:
    265 		case SDT_MEMROA:
    266 		case SDT_MEMRW:
    267 		case SDT_MEMRWA:
    268 		case SDT_MEMROD:
    269 		case SDT_MEMRODA:
    270 		case SDT_MEMRWD:
    271 		case SDT_MEMRWDA:
    272 		case SDT_MEME:
    273 		case SDT_MEMEA:
    274 		case SDT_MEMER:
    275 		case SDT_MEMERA:
    276 			break;
    277 		default:
    278 			return EACCES;
    279 		}
    280 
    281 		if (desc->sd.sd_p != 0) {
    282 			/* Only user (ring-3) descriptors may be present. */
    283 			if (desc->sd.sd_dpl != SEL_UPL)
    284 				return EACCES;
    285 		}
    286 	}
    287 
    288 	/*
    289 	 * Install selected changes.  We perform a copy, write, swap dance
    290 	 * here to ensure that all updates happen atomically.
    291 	 */
    292 
    293 	/* Allocate a new LDT. */
    294 	for (;;) {
    295 		new_len = (ua->start + ua->num) * sizeof(union descriptor);
    296 		new_len = max(new_len, pmap->pm_ldt_len);
    297 		new_len = max(new_len, min_ldt_size);
    298 		new_len = round_page(new_len);
    299 		new_ldt = (union descriptor *)uvm_km_alloc(kernel_map,
    300 		    new_len, 0, UVM_KMF_WIRED | UVM_KMF_ZERO | UVM_KMF_WAITVA);
    301 		mutex_enter(&cpu_lock);
    302 		if (pmap->pm_ldt_len <= new_len) {
    303 			break;
    304 		}
    305 		mutex_exit(&cpu_lock);
    306 		uvm_km_free(kernel_map, (vaddr_t)new_ldt, new_len,
    307 		    UVM_KMF_WIRED);
    308 	}
    309 
    310 	/* Copy existing entries, if any. */
    311 	if (pmap->pm_ldt != NULL) {
    312 		old_ldt = pmap->pm_ldt;
    313 		old_len = pmap->pm_ldt_len;
    314 		old_sel = pmap->pm_ldt_sel;
    315 		memcpy(new_ldt, old_ldt, old_len);
    316 	} else {
    317 		old_ldt = NULL;
    318 		old_len = 0;
    319 		old_sel = -1;
    320 		memcpy(new_ldt, ldtstore, min_ldt_size);
    321 	}
    322 
    323 	/* Apply requested changes. */
    324 	for (i = 0, n = ua->start; i < ua->num; i++, n++) {
    325 		new_ldt[n] = descv[i];
    326 	}
    327 
    328 	/* Allocate LDT selector. */
    329 	new_sel = ldt_alloc(new_ldt, new_len);
    330 	if (new_sel == -1) {
    331 		mutex_exit(&cpu_lock);
    332 		uvm_km_free(kernel_map, (vaddr_t)new_ldt, new_len,
    333 		    UVM_KMF_WIRED);
    334 		return ENOMEM;
    335 	}
    336 
    337 	/* All changes are now globally visible.  Swap in the new LDT. */
    338 	pmap->pm_ldt_len = new_len;
    339 	pmap->pm_ldt_sel = new_sel;
    340 	/* membar_store_store for pmap_fork() to read these unlocked safely */
    341 	membar_producer();
    342 	pmap->pm_ldt = new_ldt;
    343 
    344 	/* Switch existing users onto new LDT. */
    345 	pmap_ldt_sync(pmap);
    346 
    347 	/* Free existing LDT (if any). */
    348 	if (old_ldt != NULL) {
    349 		ldt_free(old_sel);
    350 		/* exit the mutex before free */
    351 		mutex_exit(&cpu_lock);
    352 		uvm_km_free(kernel_map, (vaddr_t)old_ldt, old_len,
    353 		    UVM_KMF_WIRED);
    354 	} else {
    355 		mutex_exit(&cpu_lock);
    356 	}
    357 
    358 	return error;
    359 #endif
    360 }
    361 
    362 int
    363 x86_iopl(struct lwp *l, void *args, register_t *retval)
    364 {
    365 	int error;
    366 	struct x86_iopl_args ua;
    367 #ifdef XEN
    368 	int iopl;
    369 #else
    370 	struct trapframe *tf = l->l_md.md_regs;
    371 #endif
    372 
    373 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPL,
    374 	    NULL, NULL, NULL, NULL);
    375 	if (error)
    376 		return error;
    377 
    378 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    379 		return error;
    380 
    381 #ifdef XEN
    382 	if (ua.iopl)
    383 		iopl = SEL_UPL;
    384 	else
    385 		iopl = SEL_KPL;
    386 
    387     {
    388 	struct physdev_op physop;
    389 	struct pcb *pcb;
    390 
    391 	pcb = lwp_getpcb(l);
    392 	pcb->pcb_iopl = iopl;
    393 
    394 	/* Force the change at ring 0. */
    395 	physop.cmd = PHYSDEVOP_SET_IOPL;
    396 	physop.u.set_iopl.iopl = iopl;
    397 	HYPERVISOR_physdev_op(&physop);
    398     }
    399 #elif defined(__x86_64__)
    400 	if (ua.iopl)
    401 		tf->tf_rflags |= PSL_IOPL;
    402 	else
    403 		tf->tf_rflags &= ~PSL_IOPL;
    404 #else
    405 	if (ua.iopl)
    406 		tf->tf_eflags |= PSL_IOPL;
    407 	else
    408 		tf->tf_eflags &= ~PSL_IOPL;
    409 #endif
    410 
    411 	return 0;
    412 }
    413 
    414 int
    415 x86_get_ioperm(struct lwp *l, void *args, register_t *retval)
    416 {
    417 #ifdef IOPERM
    418 	int error;
    419 	struct pcb *pcb = lwp_getpcb(l);
    420 	struct x86_get_ioperm_args ua;
    421 	void *dummymap = NULL;
    422 	void *iomap;
    423 
    424 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPERM_GET,
    425 	    NULL, NULL, NULL, NULL);
    426 	if (error)
    427 		return error;
    428 
    429 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    430 		return error;
    431 
    432 	iomap = pcb->pcb_iomap;
    433 	if (iomap == NULL) {
    434 		iomap = dummymap = kmem_alloc(IOMAPSIZE, KM_SLEEP);
    435 		memset(dummymap, 0xff, IOMAPSIZE);
    436 	}
    437 	error = copyout(iomap, ua.iomap, IOMAPSIZE);
    438 	if (dummymap != NULL) {
    439 		kmem_free(dummymap, IOMAPSIZE);
    440 	}
    441 	return error;
    442 #else
    443 	return EINVAL;
    444 #endif
    445 }
    446 
    447 int
    448 x86_set_ioperm(struct lwp *l, void *args, register_t *retval)
    449 {
    450 #ifdef IOPERM
    451 	struct cpu_info *ci;
    452 	int error;
    453 	struct pcb *pcb = lwp_getpcb(l);
    454 	struct x86_set_ioperm_args ua;
    455 	void *new;
    456 	void *old;
    457 
    458   	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPERM_SET,
    459 	    NULL, NULL, NULL, NULL);
    460 	if (error)
    461 		return error;
    462 
    463 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    464 		return error;
    465 
    466 	new = kmem_alloc(IOMAPSIZE, KM_SLEEP);
    467 	error = copyin(ua.iomap, new, IOMAPSIZE);
    468 	if (error) {
    469 		kmem_free(new, IOMAPSIZE);
    470 		return error;
    471 	}
    472 	old = pcb->pcb_iomap;
    473 	pcb->pcb_iomap = new;
    474 	if (old != NULL) {
    475 		kmem_free(old, IOMAPSIZE);
    476 	}
    477 
    478 	kpreempt_disable();
    479 	ci = curcpu();
    480 	memcpy(ci->ci_iomap, pcb->pcb_iomap, sizeof(ci->ci_iomap));
    481 	ci->ci_tss.tss_iobase =
    482 	    ((uintptr_t)ci->ci_iomap - (uintptr_t)&ci->ci_tss) << 16;
    483 	kpreempt_enable();
    484 
    485 	return error;
    486 #else
    487 	return EINVAL;
    488 #endif
    489 }
    490 
    491 int
    492 x86_get_mtrr(struct lwp *l, void *args, register_t *retval)
    493 {
    494 #ifdef MTRR
    495 	struct x86_get_mtrr_args ua;
    496 	int error, n;
    497 
    498 	if (mtrr_funcs == NULL)
    499 		return ENOSYS;
    500 
    501  	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_MTRR_GET,
    502 	    NULL, NULL, NULL, NULL);
    503 	if (error)
    504 		return error;
    505 
    506 	error = copyin(args, &ua, sizeof ua);
    507 	if (error != 0)
    508 		return error;
    509 
    510 	error = copyin(ua.n, &n, sizeof n);
    511 	if (error != 0)
    512 		return error;
    513 
    514 	KERNEL_LOCK(1, NULL);
    515 	error = mtrr_get(ua.mtrrp, &n, l->l_proc, MTRR_GETSET_USER);
    516 	KERNEL_UNLOCK_ONE(NULL);
    517 
    518 	copyout(&n, ua.n, sizeof (int));
    519 
    520 	return error;
    521 #else
    522 	return EINVAL;
    523 #endif
    524 }
    525 
    526 int
    527 x86_set_mtrr(struct lwp *l, void *args, register_t *retval)
    528 {
    529 #ifdef MTRR
    530 	int error, n;
    531 	struct x86_set_mtrr_args ua;
    532 
    533 	if (mtrr_funcs == NULL)
    534 		return ENOSYS;
    535 
    536  	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_MTRR_SET,
    537 	    NULL, NULL, NULL, NULL);
    538 	if (error)
    539 		return error;
    540 
    541 	error = copyin(args, &ua, sizeof ua);
    542 	if (error != 0)
    543 		return error;
    544 
    545 	error = copyin(ua.n, &n, sizeof n);
    546 	if (error != 0)
    547 		return error;
    548 
    549 	KERNEL_LOCK(1, NULL);
    550 	error = mtrr_set(ua.mtrrp, &n, l->l_proc, MTRR_GETSET_USER);
    551 	if (n != 0)
    552 		mtrr_commit();
    553 	KERNEL_UNLOCK_ONE(NULL);
    554 
    555 	copyout(&n, ua.n, sizeof n);
    556 
    557 	return error;
    558 #else
    559 	return EINVAL;
    560 #endif
    561 }
    562 
    563 #ifdef __x86_64__
    564 #define pcb_fsd pcb_fs
    565 #define pcb_gsd pcb_gs
    566 #define segment_descriptor mem_segment_descriptor
    567 #endif
    568 
    569 int
    570 x86_set_sdbase32(void *arg, char which, lwp_t *l, bool direct)
    571 {
    572 	struct trapframe *tf = l->l_md.md_regs;
    573 	union descriptor usd;
    574 	struct pcb *pcb;
    575 	uint32_t base;
    576 	int error;
    577 
    578 	if (direct) {
    579 		base = (vaddr_t)arg;
    580 	} else {
    581 		error = copyin(arg, &base, sizeof(base));
    582 		if (error != 0)
    583 			return error;
    584 	}
    585 
    586 	memset(&usd, 0, sizeof(usd));
    587 	usd.sd.sd_lobase = base & 0xffffff;
    588 	usd.sd.sd_hibase = (base >> 24) & 0xff;
    589 	usd.sd.sd_lolimit = 0xffff;
    590 	usd.sd.sd_hilimit = 0xf;
    591 	usd.sd.sd_type = SDT_MEMRWA;
    592 	usd.sd.sd_dpl = SEL_UPL;
    593 	usd.sd.sd_p = 1;
    594 	usd.sd.sd_def32 = 1;
    595 	usd.sd.sd_gran = 1;
    596 
    597 	pcb = lwp_getpcb(l);
    598 	kpreempt_disable();
    599 	if (which == 'f') {
    600 		memcpy(&pcb->pcb_fsd, &usd.sd,
    601 		    sizeof(struct segment_descriptor));
    602 		if (l == curlwp) {
    603 			update_descriptor(&curcpu()->ci_gdt[GUFS_SEL], &usd);
    604 		}
    605 		tf->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
    606 	} else /* which == 'g' */ {
    607 		memcpy(&pcb->pcb_gsd, &usd.sd,
    608 		    sizeof(struct segment_descriptor));
    609 		if (l == curlwp) {
    610 			update_descriptor(&curcpu()->ci_gdt[GUGS_SEL], &usd);
    611 #if defined(__x86_64__) && defined(XEN)
    612 			setusergs(GSEL(GUGS_SEL, SEL_UPL));
    613 #endif
    614 		}
    615 		tf->tf_gs = GSEL(GUGS_SEL, SEL_UPL);
    616 	}
    617 	kpreempt_enable();
    618 	return 0;
    619 }
    620 
    621 int
    622 x86_set_sdbase(void *arg, char which, lwp_t *l, bool direct)
    623 {
    624 #ifdef i386
    625 	return x86_set_sdbase32(arg, which, l, direct);
    626 #else
    627 	struct pcb *pcb;
    628 	vaddr_t base;
    629 
    630 	if (l->l_proc->p_flag & PK_32) {
    631 		return x86_set_sdbase32(arg, which, l, direct);
    632 	}
    633 
    634 	if (direct) {
    635 		base = (vaddr_t)arg;
    636 	} else {
    637 		int error = copyin(arg, &base, sizeof(base));
    638 		if (error != 0)
    639 			return error;
    640 	}
    641 
    642 	if (base >= VM_MAXUSER_ADDRESS)
    643 		return EINVAL;
    644 
    645 	pcb = lwp_getpcb(l);
    646 
    647 	kpreempt_disable();
    648 	switch(which) {
    649 	case 'f':
    650 		pcb->pcb_fs = base;
    651 		if (l == curlwp)
    652 			wrmsr(MSR_FSBASE, pcb->pcb_fs);
    653 		break;
    654 	case 'g':
    655 		pcb->pcb_gs = base;
    656 		if (l == curlwp)
    657 			wrmsr(MSR_KERNELGSBASE, pcb->pcb_gs);
    658 		break;
    659 	default:
    660 		panic("x86_set_sdbase");
    661 	}
    662 	kpreempt_enable();
    663 
    664 	return 0;
    665 #endif
    666 }
    667 
    668 int
    669 x86_get_sdbase32(void *arg, char which)
    670 {
    671 	struct segment_descriptor *sd;
    672 	uint32_t base;
    673 
    674 	switch (which) {
    675 	case 'f':
    676 		sd = (void *)&curpcb->pcb_fsd;
    677 		break;
    678 	case 'g':
    679 		sd = (void *)&curpcb->pcb_gsd;
    680 		break;
    681 	default:
    682 		panic("x86_get_sdbase32");
    683 	}
    684 
    685 	base = sd->sd_hibase << 24 | sd->sd_lobase;
    686 	return copyout(&base, arg, sizeof(base));
    687 }
    688 
    689 int
    690 x86_get_sdbase(void *arg, char which)
    691 {
    692 #ifdef i386
    693 	return x86_get_sdbase32(arg, which);
    694 #else
    695 	vaddr_t base;
    696 	struct pcb *pcb;
    697 
    698 	if (curproc->p_flag & PK_32) {
    699 		return x86_get_sdbase32(arg, which);
    700 	}
    701 
    702 	pcb = lwp_getpcb(curlwp);
    703 
    704 	switch(which) {
    705 	case 'f':
    706 		base = pcb->pcb_fs;
    707 		break;
    708 	case 'g':
    709 		base = pcb->pcb_gs;
    710 		break;
    711 	default:
    712 		panic("x86_get_sdbase");
    713 	}
    714 
    715 	return copyout(&base, arg, sizeof(base));
    716 #endif
    717 }
    718 
    719 int
    720 sys_sysarch(struct lwp *l, const struct sys_sysarch_args *uap, register_t *retval)
    721 {
    722 	/* {
    723 		syscallarg(int) op;
    724 		syscallarg(void *) parms;
    725 	} */
    726 	int error = 0;
    727 
    728 	switch(SCARG(uap, op)) {
    729 	case X86_IOPL:
    730 		error = x86_iopl(l, SCARG(uap, parms), retval);
    731 		break;
    732 
    733 #ifdef i386
    734 	/*
    735 	 * On amd64, this is done via netbsd32_sysarch.
    736 	 */
    737 	case X86_GET_LDT:
    738 		error = x86_get_ldt(l, SCARG(uap, parms), retval);
    739 		break;
    740 
    741 	case X86_SET_LDT:
    742 		error = x86_set_ldt(l, SCARG(uap, parms), retval);
    743 		break;
    744 #endif
    745 
    746 	case X86_GET_IOPERM:
    747 		error = x86_get_ioperm(l, SCARG(uap, parms), retval);
    748 		break;
    749 
    750 	case X86_SET_IOPERM:
    751 		error = x86_set_ioperm(l, SCARG(uap, parms), retval);
    752 		break;
    753 
    754 	case X86_GET_MTRR:
    755 		error = x86_get_mtrr(l, SCARG(uap, parms), retval);
    756 		break;
    757 	case X86_SET_MTRR:
    758 		error = x86_set_mtrr(l, SCARG(uap, parms), retval);
    759 		break;
    760 
    761 #ifdef PMC
    762 	case X86_PMC_INFO:
    763 		error = sys_pmc_info(l, SCARG(uap, parms), retval);
    764 		break;
    765 
    766 	case X86_PMC_STARTSTOP:
    767 		error = sys_pmc_startstop(l, SCARG(uap, parms), retval);
    768 		break;
    769 
    770 	case X86_PMC_READ:
    771 		error = sys_pmc_read(l, SCARG(uap, parms), retval);
    772 		break;
    773 #endif
    774 
    775 	case X86_SET_FSBASE:
    776 		error = x86_set_sdbase(SCARG(uap, parms), 'f', curlwp, false);
    777 		break;
    778 
    779 	case X86_SET_GSBASE:
    780 		error = x86_set_sdbase(SCARG(uap, parms), 'g', curlwp, false);
    781 		break;
    782 
    783 	case X86_GET_FSBASE:
    784 		error = x86_get_sdbase(SCARG(uap, parms), 'f');
    785 		break;
    786 
    787 	case X86_GET_GSBASE:
    788 		error = x86_get_sdbase(SCARG(uap, parms), 'g');
    789 		break;
    790 
    791 	default:
    792 		error = EINVAL;
    793 		break;
    794 	}
    795 	return error;
    796 }
    797 
    798 int
    799 cpu_lwp_setprivate(lwp_t *l, void *addr)
    800 {
    801 
    802 #ifdef __x86_64__
    803 	if ((l->l_proc->p_flag & PK_32) == 0) {
    804 		return x86_set_sdbase(addr, 'f', l, true);
    805 	}
    806 #endif
    807 	return x86_set_sdbase(addr, 'g', l, true);
    808 }
    809