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