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