Home | History | Annotate | Line # | Download | only in x86
sys_machdep.c revision 1.46
      1 /*	$NetBSD: sys_machdep.c,v 1.46 2018/01/04 14:02:23 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.46 2018/01/04 14:02:23 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 	CTASSERT(offsetof(struct cpu_tss, iomap) -
    484 	    offsetof(struct cpu_tss, tss) == IOMAP_VALIDOFF);
    485 
    486 	kpreempt_disable();
    487 	ci = curcpu();
    488 	memcpy(ci->ci_tss->iomap, pcb->pcb_iomap, IOMAPSIZE);
    489 	ci->ci_tss->tss.tss_iobase = IOMAP_VALIDOFF << 16;
    490 	kpreempt_enable();
    491 
    492 	return error;
    493 #else
    494 	return EINVAL;
    495 #endif
    496 }
    497 
    498 int
    499 x86_get_mtrr(struct lwp *l, void *args, register_t *retval)
    500 {
    501 #ifdef MTRR
    502 	struct x86_get_mtrr_args ua;
    503 	int error, n;
    504 
    505 	if (mtrr_funcs == NULL)
    506 		return ENOSYS;
    507 
    508  	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_MTRR_GET,
    509 	    NULL, NULL, NULL, NULL);
    510 	if (error)
    511 		return error;
    512 
    513 	error = copyin(args, &ua, sizeof ua);
    514 	if (error != 0)
    515 		return error;
    516 
    517 	error = copyin(ua.n, &n, sizeof n);
    518 	if (error != 0)
    519 		return error;
    520 
    521 	KERNEL_LOCK(1, NULL);
    522 	error = mtrr_get(ua.mtrrp, &n, l->l_proc, MTRR_GETSET_USER);
    523 	KERNEL_UNLOCK_ONE(NULL);
    524 
    525 	copyout(&n, ua.n, sizeof (int));
    526 
    527 	return error;
    528 #else
    529 	return EINVAL;
    530 #endif
    531 }
    532 
    533 int
    534 x86_set_mtrr(struct lwp *l, void *args, register_t *retval)
    535 {
    536 #ifdef MTRR
    537 	int error, n;
    538 	struct x86_set_mtrr_args ua;
    539 
    540 	if (mtrr_funcs == NULL)
    541 		return ENOSYS;
    542 
    543  	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_MTRR_SET,
    544 	    NULL, NULL, NULL, NULL);
    545 	if (error)
    546 		return error;
    547 
    548 	error = copyin(args, &ua, sizeof ua);
    549 	if (error != 0)
    550 		return error;
    551 
    552 	error = copyin(ua.n, &n, sizeof n);
    553 	if (error != 0)
    554 		return error;
    555 
    556 	KERNEL_LOCK(1, NULL);
    557 	error = mtrr_set(ua.mtrrp, &n, l->l_proc, MTRR_GETSET_USER);
    558 	if (n != 0)
    559 		mtrr_commit();
    560 	KERNEL_UNLOCK_ONE(NULL);
    561 
    562 	copyout(&n, ua.n, sizeof n);
    563 
    564 	return error;
    565 #else
    566 	return EINVAL;
    567 #endif
    568 }
    569 
    570 #ifdef __x86_64__
    571 #define pcb_fsd pcb_fs
    572 #define pcb_gsd pcb_gs
    573 #define segment_descriptor mem_segment_descriptor
    574 #endif
    575 
    576 int
    577 x86_set_sdbase32(void *arg, char which, lwp_t *l, bool direct)
    578 {
    579 	struct trapframe *tf = l->l_md.md_regs;
    580 	union descriptor usd;
    581 	struct pcb *pcb;
    582 	uint32_t base;
    583 	int error;
    584 
    585 	if (direct) {
    586 		base = (vaddr_t)arg;
    587 	} else {
    588 		error = copyin(arg, &base, sizeof(base));
    589 		if (error != 0)
    590 			return error;
    591 	}
    592 
    593 	memset(&usd, 0, sizeof(usd));
    594 	usd.sd.sd_lobase = base & 0xffffff;
    595 	usd.sd.sd_hibase = (base >> 24) & 0xff;
    596 	usd.sd.sd_lolimit = 0xffff;
    597 	usd.sd.sd_hilimit = 0xf;
    598 	usd.sd.sd_type = SDT_MEMRWA;
    599 	usd.sd.sd_dpl = SEL_UPL;
    600 	usd.sd.sd_p = 1;
    601 	usd.sd.sd_def32 = 1;
    602 	usd.sd.sd_gran = 1;
    603 
    604 	pcb = lwp_getpcb(l);
    605 	kpreempt_disable();
    606 	if (which == 'f') {
    607 		memcpy(&pcb->pcb_fsd, &usd.sd,
    608 		    sizeof(struct segment_descriptor));
    609 		if (l == curlwp) {
    610 			update_descriptor(&curcpu()->ci_gdt[GUFS_SEL], &usd);
    611 		}
    612 		tf->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
    613 	} else /* which == 'g' */ {
    614 		memcpy(&pcb->pcb_gsd, &usd.sd,
    615 		    sizeof(struct segment_descriptor));
    616 		if (l == curlwp) {
    617 			update_descriptor(&curcpu()->ci_gdt[GUGS_SEL], &usd);
    618 #if defined(__x86_64__) && defined(XEN)
    619 			setusergs(GSEL(GUGS_SEL, SEL_UPL));
    620 #endif
    621 		}
    622 		tf->tf_gs = GSEL(GUGS_SEL, SEL_UPL);
    623 	}
    624 	kpreempt_enable();
    625 	return 0;
    626 }
    627 
    628 int
    629 x86_set_sdbase(void *arg, char which, lwp_t *l, bool direct)
    630 {
    631 #ifdef i386
    632 	return x86_set_sdbase32(arg, which, l, direct);
    633 #else
    634 	struct pcb *pcb;
    635 	vaddr_t base;
    636 
    637 	if (l->l_proc->p_flag & PK_32) {
    638 		return x86_set_sdbase32(arg, which, l, direct);
    639 	}
    640 
    641 	if (direct) {
    642 		base = (vaddr_t)arg;
    643 	} else {
    644 		int error = copyin(arg, &base, sizeof(base));
    645 		if (error != 0)
    646 			return error;
    647 	}
    648 
    649 	if (base >= VM_MAXUSER_ADDRESS)
    650 		return EINVAL;
    651 
    652 	pcb = lwp_getpcb(l);
    653 
    654 	kpreempt_disable();
    655 	switch(which) {
    656 	case 'f':
    657 		pcb->pcb_fs = base;
    658 		if (l == curlwp)
    659 			wrmsr(MSR_FSBASE, pcb->pcb_fs);
    660 		break;
    661 	case 'g':
    662 		pcb->pcb_gs = base;
    663 		if (l == curlwp)
    664 			wrmsr(MSR_KERNELGSBASE, pcb->pcb_gs);
    665 		break;
    666 	default:
    667 		panic("x86_set_sdbase");
    668 	}
    669 	kpreempt_enable();
    670 
    671 	return 0;
    672 #endif
    673 }
    674 
    675 int
    676 x86_get_sdbase32(void *arg, char which)
    677 {
    678 	struct segment_descriptor *sd;
    679 	uint32_t base;
    680 
    681 	switch (which) {
    682 	case 'f':
    683 		sd = (void *)&curpcb->pcb_fsd;
    684 		break;
    685 	case 'g':
    686 		sd = (void *)&curpcb->pcb_gsd;
    687 		break;
    688 	default:
    689 		panic("x86_get_sdbase32");
    690 	}
    691 
    692 	base = sd->sd_hibase << 24 | sd->sd_lobase;
    693 	return copyout(&base, arg, sizeof(base));
    694 }
    695 
    696 int
    697 x86_get_sdbase(void *arg, char which)
    698 {
    699 #ifdef i386
    700 	return x86_get_sdbase32(arg, which);
    701 #else
    702 	vaddr_t base;
    703 	struct pcb *pcb;
    704 
    705 	if (curproc->p_flag & PK_32) {
    706 		return x86_get_sdbase32(arg, which);
    707 	}
    708 
    709 	pcb = lwp_getpcb(curlwp);
    710 
    711 	switch(which) {
    712 	case 'f':
    713 		base = pcb->pcb_fs;
    714 		break;
    715 	case 'g':
    716 		base = pcb->pcb_gs;
    717 		break;
    718 	default:
    719 		panic("x86_get_sdbase");
    720 	}
    721 
    722 	return copyout(&base, arg, sizeof(base));
    723 #endif
    724 }
    725 
    726 int
    727 sys_sysarch(struct lwp *l, const struct sys_sysarch_args *uap, register_t *retval)
    728 {
    729 	/* {
    730 		syscallarg(int) op;
    731 		syscallarg(void *) parms;
    732 	} */
    733 	int error = 0;
    734 
    735 	switch(SCARG(uap, op)) {
    736 	case X86_IOPL:
    737 		error = x86_iopl(l, SCARG(uap, parms), retval);
    738 		break;
    739 
    740 #ifdef i386
    741 	/*
    742 	 * On amd64, this is done via netbsd32_sysarch.
    743 	 */
    744 	case X86_GET_LDT:
    745 		error = x86_get_ldt(l, SCARG(uap, parms), retval);
    746 		break;
    747 
    748 	case X86_SET_LDT:
    749 		error = x86_set_ldt(l, SCARG(uap, parms), retval);
    750 		break;
    751 #endif
    752 
    753 	case X86_GET_IOPERM:
    754 		error = x86_get_ioperm(l, SCARG(uap, parms), retval);
    755 		break;
    756 
    757 	case X86_SET_IOPERM:
    758 		error = x86_set_ioperm(l, SCARG(uap, parms), retval);
    759 		break;
    760 
    761 	case X86_GET_MTRR:
    762 		error = x86_get_mtrr(l, SCARG(uap, parms), retval);
    763 		break;
    764 	case X86_SET_MTRR:
    765 		error = x86_set_mtrr(l, SCARG(uap, parms), retval);
    766 		break;
    767 
    768 #ifdef PMC
    769 	case X86_PMC_INFO:
    770 		error = sys_pmc_info(l, SCARG(uap, parms), retval);
    771 		break;
    772 
    773 	case X86_PMC_STARTSTOP:
    774 		error = sys_pmc_startstop(l, SCARG(uap, parms), retval);
    775 		break;
    776 
    777 	case X86_PMC_READ:
    778 		error = sys_pmc_read(l, SCARG(uap, parms), retval);
    779 		break;
    780 #endif
    781 
    782 	case X86_SET_FSBASE:
    783 		error = x86_set_sdbase(SCARG(uap, parms), 'f', curlwp, false);
    784 		break;
    785 
    786 	case X86_SET_GSBASE:
    787 		error = x86_set_sdbase(SCARG(uap, parms), 'g', curlwp, false);
    788 		break;
    789 
    790 	case X86_GET_FSBASE:
    791 		error = x86_get_sdbase(SCARG(uap, parms), 'f');
    792 		break;
    793 
    794 	case X86_GET_GSBASE:
    795 		error = x86_get_sdbase(SCARG(uap, parms), 'g');
    796 		break;
    797 
    798 	default:
    799 		error = EINVAL;
    800 		break;
    801 	}
    802 	return error;
    803 }
    804 
    805 int
    806 cpu_lwp_setprivate(lwp_t *l, void *addr)
    807 {
    808 
    809 #ifdef __x86_64__
    810 	if ((l->l_proc->p_flag & PK_32) == 0) {
    811 		return x86_set_sdbase(addr, 'f', l, true);
    812 	}
    813 #endif
    814 	return x86_set_sdbase(addr, 'g', l, true);
    815 }
    816