Home | History | Annotate | Line # | Download | only in x86
sys_machdep.c revision 1.13
      1  1.13    ad /*	$NetBSD: sys_machdep.c,v 1.13 2008/04/27 11:37:48 ad Exp $	*/
      2   1.1    ad 
      3   1.1    ad /*-
      4   1.1    ad  * Copyright (c) 1998, 2007 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.1    ad  * by Charles M. Hannum, and by Andrew Doran.
      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  * 3. All advertising materials mentioning features or use of this software
     19   1.1    ad  *    must display the following acknowledgement:
     20   1.1    ad  *        This product includes software developed by the NetBSD
     21   1.1    ad  *        Foundation, Inc. and its contributors.
     22   1.1    ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1    ad  *    contributors may be used to endorse or promote products derived
     24   1.1    ad  *    from this software without specific prior written permission.
     25   1.1    ad  *
     26   1.1    ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1    ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1    ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1    ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1    ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1    ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1    ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1    ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1    ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1    ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1    ad  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1    ad  */
     38   1.1    ad 
     39   1.1    ad #include <sys/cdefs.h>
     40  1.13    ad __KERNEL_RCSID(0, "$NetBSD: sys_machdep.c,v 1.13 2008/04/27 11:37:48 ad Exp $");
     41   1.1    ad 
     42   1.1    ad #include "opt_compat_netbsd.h"
     43   1.1    ad #include "opt_mtrr.h"
     44   1.1    ad #include "opt_perfctrs.h"
     45   1.1    ad #include "opt_user_ldt.h"
     46   1.1    ad #include "opt_vm86.h"
     47   1.1    ad #include "opt_xen.h"
     48   1.1    ad 
     49   1.1    ad #include <sys/param.h>
     50   1.1    ad #include <sys/systm.h>
     51   1.1    ad #include <sys/ioctl.h>
     52   1.1    ad #include <sys/file.h>
     53   1.1    ad #include <sys/time.h>
     54   1.1    ad #include <sys/proc.h>
     55   1.1    ad #include <sys/user.h>
     56   1.1    ad #include <sys/uio.h>
     57   1.1    ad #include <sys/kernel.h>
     58   1.1    ad #include <sys/buf.h>
     59   1.1    ad #include <sys/signal.h>
     60   1.1    ad #include <sys/malloc.h>
     61   1.9  yamt #include <sys/kmem.h>
     62   1.1    ad #include <sys/kauth.h>
     63   1.1    ad 
     64   1.1    ad #include <sys/mount.h>
     65   1.1    ad #include <sys/syscallargs.h>
     66   1.1    ad 
     67   1.1    ad #include <uvm/uvm_extern.h>
     68   1.1    ad 
     69   1.1    ad #include <machine/cpu.h>
     70   1.1    ad #include <machine/cpufunc.h>
     71   1.1    ad #include <machine/gdt.h>
     72   1.1    ad #include <machine/psl.h>
     73   1.1    ad #include <machine/reg.h>
     74   1.1    ad #include <machine/sysarch.h>
     75   1.1    ad #include <machine/mtrr.h>
     76   1.1    ad 
     77   1.1    ad #ifdef __x86_64__
     78   1.1    ad /* Need to be checked. */
     79   1.1    ad #undef	USER_LDT
     80   1.1    ad #undef	PERFCTRS
     81   1.1    ad #undef	VM86
     82   1.1    ad #undef	IOPERM
     83   1.1    ad #else
     84   1.9  yamt #if defined(XEN)
     85   1.9  yamt #undef	IOPERM
     86   1.9  yamt #else /* defined(XEN) */
     87   1.1    ad #define	IOPERM
     88   1.9  yamt #endif /* defined(XEN) */
     89   1.1    ad #endif
     90   1.1    ad 
     91   1.1    ad #ifdef VM86
     92   1.1    ad #include <machine/vm86.h>
     93   1.1    ad #endif
     94   1.1    ad 
     95   1.1    ad #ifdef PERFCTRS
     96   1.1    ad #include <machine/pmc.h>
     97   1.1    ad #endif
     98   1.1    ad 
     99   1.1    ad extern struct vm_map *kernel_map;
    100   1.1    ad 
    101   1.1    ad int x86_get_ioperm(struct lwp *, void *, register_t *);
    102   1.1    ad int x86_set_ioperm(struct lwp *, void *, register_t *);
    103   1.1    ad int x86_get_mtrr(struct lwp *, void *, register_t *);
    104   1.1    ad int x86_set_mtrr(struct lwp *, void *, register_t *);
    105   1.5    ad int x86_set_sdbase(void *arg, char which);
    106   1.5    ad int x86_get_sdbase(void *arg, char which);
    107   1.1    ad 
    108   1.1    ad #ifdef LDT_DEBUG
    109   1.1    ad static void x86_print_ldt(int, const struct segment_descriptor *);
    110   1.1    ad 
    111   1.1    ad static void
    112   1.1    ad x86_print_ldt(int i, const struct segment_descriptor *d)
    113   1.1    ad {
    114   1.1    ad 	printf("[%d] lolimit=0x%x, lobase=0x%x, type=%u, dpl=%u, p=%u, "
    115   1.1    ad 	    "hilimit=0x%x, xx=%x, def32=%u, gran=%u, hibase=0x%x\n",
    116   1.1    ad 	    i, d->sd_lolimit, d->sd_lobase, d->sd_type, d->sd_dpl, d->sd_p,
    117   1.1    ad 	    d->sd_hilimit, d->sd_xx, d->sd_def32, d->sd_gran, d->sd_hibase);
    118   1.1    ad }
    119   1.1    ad #endif
    120   1.1    ad 
    121   1.1    ad int
    122   1.2   dsl x86_get_ldt_len(struct lwp *l)
    123   1.2   dsl {
    124   1.2   dsl #ifndef USER_LDT
    125   1.2   dsl 	return -1;
    126   1.2   dsl #else
    127   1.2   dsl 	pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap;
    128   1.2   dsl 	int nldt;
    129   1.2   dsl 
    130   1.3    ad 	mutex_enter(&pmap->pm_lock);
    131   1.2   dsl 
    132   1.2   dsl 	if (pmap->pm_flags & PMF_USER_LDT) {
    133   1.2   dsl 		nldt = pmap->pm_ldt_len;
    134   1.2   dsl 	} else {
    135   1.2   dsl 		nldt = NLDT;
    136   1.2   dsl 	}
    137   1.3    ad 	mutex_exit(&pmap->pm_lock);
    138   1.2   dsl 	return nldt;
    139   1.2   dsl #endif
    140   1.2   dsl }
    141   1.2   dsl 
    142   1.2   dsl 
    143   1.2   dsl int
    144   1.1    ad x86_get_ldt(struct lwp *l, void *args, register_t *retval)
    145   1.1    ad {
    146   1.2   dsl #ifndef USER_LDT
    147   1.2   dsl 	return EINVAL;
    148   1.2   dsl #else
    149   1.2   dsl 	struct x86_get_ldt_args ua;
    150   1.2   dsl 	union descriptor *cp;
    151   1.2   dsl 	int error;
    152   1.2   dsl 
    153   1.2   dsl 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    154   1.2   dsl 		return error;
    155   1.2   dsl 
    156   1.2   dsl 	if (ua.num < 0 || ua.num > 8192)
    157   1.2   dsl 		return EINVAL;
    158   1.2   dsl 
    159   1.2   dsl 	cp = malloc(ua.num * sizeof(union descriptor), M_TEMP, M_WAITOK);
    160   1.2   dsl 	if (cp == NULL)
    161   1.2   dsl 		return ENOMEM;
    162   1.2   dsl 
    163   1.2   dsl 	error = x86_get_ldt1(l, &ua, cp);
    164   1.2   dsl 	*retval = ua.num;
    165   1.2   dsl 	if (error == 0)
    166   1.2   dsl 		error = copyout(cp, ua.desc, ua.num * sizeof(*cp));
    167   1.2   dsl 
    168   1.2   dsl 	free(cp, M_TEMP);
    169   1.2   dsl 	return error;
    170   1.2   dsl #endif
    171   1.2   dsl }
    172   1.2   dsl 
    173   1.2   dsl int
    174   1.2   dsl x86_get_ldt1(struct lwp *l, struct x86_get_ldt_args *ua, union descriptor *cp)
    175   1.2   dsl {
    176   1.2   dsl #ifndef USER_LDT
    177   1.2   dsl 	return EINVAL;
    178   1.2   dsl #else
    179   1.1    ad 	int error;
    180   1.1    ad 	struct proc *p = l->l_proc;
    181   1.1    ad 	pmap_t pmap = p->p_vmspace->vm_map.pmap;
    182   1.1    ad 	int nldt, num;
    183   1.2   dsl 	union descriptor *lp;
    184   1.1    ad 
    185   1.1    ad 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_LDT_GET,
    186   1.1    ad 	    NULL, NULL, NULL, NULL);
    187   1.1    ad 	if (error)
    188   1.1    ad 		return (error);
    189   1.1    ad 
    190   1.1    ad #ifdef	LDT_DEBUG
    191   1.2   dsl 	printf("x86_get_ldt: start=%d num=%d descs=%p\n", ua->start,
    192   1.2   dsl 	    ua->num, ua->desc);
    193   1.1    ad #endif
    194   1.1    ad 
    195   1.2   dsl 	if (ua->start < 0 || ua->num < 0 || ua->start > 8192 || ua->num > 8192 ||
    196   1.2   dsl 	    ua->start + ua->num > 8192)
    197   1.1    ad 		return (EINVAL);
    198   1.1    ad 
    199   1.3    ad 	mutex_enter(&pmap->pm_lock);
    200   1.1    ad 
    201   1.1    ad 	if (pmap->pm_flags & PMF_USER_LDT) {
    202   1.1    ad 		nldt = pmap->pm_ldt_len;
    203   1.1    ad 		lp = pmap->pm_ldt;
    204   1.1    ad 	} else {
    205   1.1    ad 		nldt = NLDT;
    206   1.1    ad 		lp = ldt;
    207   1.1    ad 	}
    208   1.1    ad 
    209   1.2   dsl 	if (ua->start > nldt) {
    210   1.3    ad 		mutex_exit(&pmap->pm_lock);
    211   1.1    ad 		return (EINVAL);
    212   1.1    ad 	}
    213   1.1    ad 
    214   1.2   dsl 	lp += ua->start;
    215   1.2   dsl 	num = min(ua->num, nldt - ua->start);
    216   1.2   dsl 	ua->num = num;
    217   1.1    ad #ifdef LDT_DEBUG
    218   1.1    ad 	{
    219   1.1    ad 		int i;
    220   1.1    ad 		for (i = 0; i < num; i++)
    221   1.1    ad 			x86_print_ldt(i, &lp[i].sd);
    222   1.1    ad 	}
    223   1.1    ad #endif
    224   1.1    ad 
    225   1.1    ad 	memcpy(cp, lp, num * sizeof(union descriptor));
    226   1.3    ad 	mutex_exit(&pmap->pm_lock);
    227   1.1    ad 
    228   1.2   dsl 	return 0;
    229   1.2   dsl #endif
    230   1.2   dsl }
    231   1.2   dsl 
    232   1.2   dsl int
    233   1.2   dsl x86_set_ldt(struct lwp *l, void *args, register_t *retval)
    234   1.2   dsl {
    235   1.2   dsl #ifndef USER_LDT
    236   1.2   dsl 	return EINVAL;
    237   1.2   dsl #else
    238   1.2   dsl 	struct x86_set_ldt_args ua;
    239   1.2   dsl 	union descriptor *descv;
    240   1.2   dsl 	int error;
    241   1.2   dsl 
    242   1.2   dsl 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    243   1.2   dsl 		return (error);
    244   1.2   dsl 
    245   1.2   dsl 	if (ua.num < 0 || ua.num > 8192)
    246   1.2   dsl 		return EINVAL;
    247   1.2   dsl 
    248   1.2   dsl 	descv = malloc(sizeof (*descv) * ua.num, M_TEMP, M_NOWAIT);
    249   1.2   dsl 	if (descv == NULL)
    250   1.2   dsl 		return ENOMEM;
    251   1.2   dsl 
    252   1.2   dsl 	error = copyin(ua.desc, descv, sizeof (*descv) * ua.num);
    253   1.1    ad 	if (error == 0)
    254   1.2   dsl 		error = x86_set_ldt1(l, &ua, descv);
    255   1.2   dsl 	*retval = ua.start;
    256   1.1    ad 
    257   1.2   dsl 	free(descv, M_TEMP);
    258   1.2   dsl 	return error;
    259   1.1    ad #endif
    260   1.1    ad }
    261   1.1    ad 
    262   1.1    ad int
    263   1.2   dsl x86_set_ldt1(struct lwp *l, struct x86_set_ldt_args *ua,
    264   1.2   dsl     union descriptor *descv)
    265   1.1    ad {
    266   1.2   dsl #ifndef USER_LDT
    267   1.2   dsl 	return EINVAL;
    268   1.2   dsl #else
    269   1.1    ad 	int error, i, n, sel, free_sel;
    270   1.1    ad 	struct proc *p = l->l_proc;
    271   1.1    ad 	struct pcb *pcb = &l->l_addr->u_pcb;
    272   1.1    ad 	pmap_t pmap = p->p_vmspace->vm_map.pmap;
    273   1.1    ad 	size_t old_len, new_len, ldt_len, free_len;
    274   1.1    ad 	union descriptor *old_ldt, *new_ldt, *free_ldt;
    275   1.1    ad 
    276   1.1    ad 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_LDT_SET,
    277   1.1    ad 	    NULL, NULL, NULL, NULL);
    278   1.1    ad 	if (error)
    279   1.1    ad 		return (error);
    280   1.1    ad 
    281   1.2   dsl 	if (ua->start < 0 || ua->num < 0 || ua->start > 8192 || ua->num > 8192 ||
    282   1.2   dsl 	    ua->start + ua->num > 8192)
    283   1.1    ad 		return (EINVAL);
    284   1.1    ad 
    285   1.1    ad 	/* Check descriptors for access violations. */
    286   1.2   dsl 	for (i = 0; i < ua->num; i++) {
    287   1.1    ad 		union descriptor *desc = &descv[i];
    288   1.1    ad 
    289   1.1    ad 		switch (desc->sd.sd_type) {
    290   1.1    ad 		case SDT_SYSNULL:
    291   1.1    ad 			desc->sd.sd_p = 0;
    292   1.1    ad 			break;
    293   1.1    ad 		case SDT_SYS286CGT:
    294   1.1    ad 		case SDT_SYS386CGT:
    295   1.1    ad 			/*
    296   1.1    ad 			 * Only allow call gates targeting a segment
    297   1.1    ad 			 * in the LDT or a user segment in the fixed
    298   1.1    ad 			 * part of the gdt.  Segments in the LDT are
    299   1.1    ad 			 * constrained (below) to be user segments.
    300   1.1    ad 			 */
    301   1.1    ad 			if (desc->gd.gd_p != 0 &&
    302   1.1    ad 			    !ISLDT(desc->gd.gd_selector) &&
    303   1.1    ad 			    ((IDXSEL(desc->gd.gd_selector) >= NGDT) ||
    304   1.1    ad 			     (gdt[IDXSEL(desc->gd.gd_selector)].sd.sd_dpl !=
    305   1.1    ad 				 SEL_UPL))) {
    306   1.2   dsl 				return EACCES;
    307   1.1    ad 			}
    308   1.1    ad 			break;
    309   1.1    ad 		case SDT_MEMEC:
    310   1.1    ad 		case SDT_MEMEAC:
    311   1.1    ad 		case SDT_MEMERC:
    312   1.1    ad 		case SDT_MEMERAC:
    313   1.1    ad 			/* Must be "present" if executable and conforming. */
    314   1.2   dsl 			if (desc->sd.sd_p == 0)
    315   1.2   dsl 				return EACCES;
    316   1.1    ad 			break;
    317   1.1    ad 		case SDT_MEMRO:
    318   1.1    ad 		case SDT_MEMROA:
    319   1.1    ad 		case SDT_MEMRW:
    320   1.1    ad 		case SDT_MEMRWA:
    321   1.1    ad 		case SDT_MEMROD:
    322   1.1    ad 		case SDT_MEMRODA:
    323   1.1    ad 		case SDT_MEMRWD:
    324   1.1    ad 		case SDT_MEMRWDA:
    325   1.1    ad 		case SDT_MEME:
    326   1.1    ad 		case SDT_MEMEA:
    327   1.1    ad 		case SDT_MEMER:
    328   1.1    ad 		case SDT_MEMERA:
    329   1.1    ad 			break;
    330   1.1    ad 		default:
    331   1.1    ad 			/*
    332   1.1    ad 			 * Make sure that unknown descriptor types are
    333   1.1    ad 			 * not marked present.
    334   1.1    ad 			 */
    335   1.2   dsl 			if (desc->sd.sd_p != 0)
    336   1.2   dsl 				return EACCES;
    337   1.1    ad 			break;
    338   1.1    ad 		}
    339   1.1    ad 
    340   1.1    ad 		if (desc->sd.sd_p != 0) {
    341   1.1    ad 			/* Only user (ring-3) descriptors may be present. */
    342   1.2   dsl 			if (desc->sd.sd_dpl != SEL_UPL)
    343   1.2   dsl 				return EACCES;
    344   1.1    ad 		}
    345   1.1    ad 	}
    346   1.1    ad 
    347   1.1    ad 	/* allocate user ldt */
    348   1.1    ad 	free_sel = -1;
    349   1.1    ad 	new_ldt = NULL;
    350   1.1    ad 	new_len = 0;
    351   1.1    ad 	free_ldt = NULL;
    352   1.1    ad 	free_len = 0;
    353   1.3    ad 	mutex_enter(&pmap->pm_lock);
    354   1.2   dsl 	if (pmap->pm_ldt == 0 || (ua->start + ua->num) > pmap->pm_ldt_len) {
    355   1.1    ad 		if (pmap->pm_flags & PMF_USER_LDT)
    356   1.1    ad 			ldt_len = pmap->pm_ldt_len;
    357   1.1    ad 		else
    358   1.1    ad 			ldt_len = 512;
    359   1.2   dsl 		while ((ua->start + ua->num) > ldt_len)
    360   1.1    ad 			ldt_len *= 2;
    361   1.1    ad 		new_len = ldt_len * sizeof(union descriptor);
    362   1.1    ad 
    363   1.3    ad 		mutex_exit(&pmap->pm_lock);
    364   1.1    ad 		new_ldt = (union descriptor *)uvm_km_alloc(kernel_map,
    365   1.1    ad 		    new_len, 0, UVM_KMF_WIRED);
    366   1.1    ad 		memset(new_ldt, 0, new_len);
    367   1.1    ad 		sel = ldt_alloc(new_ldt, new_len);
    368   1.3    ad 		mutex_enter(&pmap->pm_lock);
    369   1.1    ad 
    370   1.1    ad 		if (pmap->pm_ldt != NULL && ldt_len <= pmap->pm_ldt_len) {
    371   1.1    ad 			/*
    372   1.1    ad 			 * Another thread (re)allocated the LDT to
    373   1.1    ad 			 * sufficient size while we were blocked in
    374   1.1    ad 			 * uvm_km_alloc. Oh well. The new entries
    375   1.1    ad 			 * will quite probably not be right, but
    376   1.1    ad 			 * hey.. not our problem if user applications
    377   1.1    ad 			 * have race conditions like that.
    378   1.1    ad 			 */
    379   1.1    ad 			goto copy;
    380   1.1    ad 		}
    381   1.1    ad 
    382   1.1    ad 		old_ldt = pmap->pm_ldt;
    383   1.1    ad 		free_ldt = old_ldt;
    384   1.1    ad 		free_len = pmap->pm_ldt_len * sizeof(union descriptor);
    385   1.1    ad 
    386   1.1    ad 		if (old_ldt != NULL) {
    387   1.1    ad 			old_len = pmap->pm_ldt_len * sizeof(union descriptor);
    388   1.1    ad 		} else {
    389   1.1    ad 			old_len = NLDT * sizeof(union descriptor);
    390   1.1    ad 			old_ldt = ldt;
    391   1.1    ad 		}
    392   1.1    ad 
    393   1.1    ad 		memcpy(new_ldt, old_ldt, old_len);
    394   1.1    ad 		memset((char *)new_ldt + old_len, 0, new_len - old_len);
    395   1.1    ad 
    396   1.1    ad 		pmap->pm_ldt = new_ldt;
    397   1.1    ad 		pmap->pm_ldt_len = ldt_len;
    398   1.1    ad 
    399   1.1    ad 		if (pmap->pm_flags & PMF_USER_LDT)
    400   1.1    ad 			free_sel = pmap->pm_ldt_sel;
    401   1.1    ad 		else {
    402   1.1    ad 			pmap->pm_flags |= PMF_USER_LDT;
    403   1.1    ad 			free_sel = -1;
    404   1.1    ad 		}
    405   1.1    ad 		pmap->pm_ldt_sel = sel;
    406   1.1    ad 		pcb->pcb_ldt_sel = pmap->pm_ldt_sel;
    407   1.1    ad 		if (pcb == curpcb)
    408   1.1    ad 			lldt(pcb->pcb_ldt_sel);
    409   1.1    ad 		new_ldt = NULL;
    410   1.1    ad 	}
    411   1.1    ad copy:
    412   1.1    ad 	/* Now actually replace the descriptors. */
    413   1.2   dsl 	for (i = 0, n = ua->start; i < ua->num; i++, n++)
    414   1.1    ad 		pmap->pm_ldt[n] = descv[i];
    415   1.1    ad 
    416   1.3    ad 	mutex_exit(&pmap->pm_lock);
    417   1.1    ad 
    418   1.1    ad 	if (new_ldt != NULL)
    419   1.1    ad 		uvm_km_free(kernel_map, (vaddr_t)new_ldt, new_len,
    420   1.1    ad 		    UVM_KMF_WIRED);
    421   1.1    ad 	if (free_sel != -1)
    422   1.1    ad 		ldt_free(free_sel);
    423   1.1    ad 	if (free_ldt != NULL)
    424   1.1    ad 		uvm_km_free(kernel_map, (vaddr_t)free_ldt, free_len,
    425   1.1    ad 		    UVM_KMF_WIRED);
    426   1.2   dsl 
    427   1.1    ad 	return (error);
    428   1.1    ad #endif
    429   1.1    ad }
    430   1.1    ad 
    431   1.1    ad int
    432   1.1    ad x86_iopl(struct lwp *l, void *args, register_t *retval)
    433   1.1    ad {
    434   1.1    ad 	int error;
    435   1.1    ad 	struct x86_iopl_args ua;
    436   1.1    ad #ifdef XEN
    437   1.9  yamt 	int iopl;
    438   1.1    ad #else
    439   1.1    ad 	struct trapframe *tf = l->l_md.md_regs;
    440   1.1    ad #endif
    441   1.1    ad 
    442   1.1    ad 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPL,
    443   1.1    ad 	    NULL, NULL, NULL, NULL);
    444   1.1    ad 	if (error)
    445   1.1    ad 		return (error);
    446   1.1    ad 
    447   1.1    ad 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    448   1.1    ad 		return error;
    449   1.1    ad 
    450   1.1    ad #ifdef XEN
    451   1.9  yamt 	if (ua.iopl)
    452   1.9  yamt 		iopl = SEL_UPL;
    453   1.9  yamt 	else
    454   1.9  yamt 		iopl = SEL_KPL;
    455  1.10  yamt 	l->l_addr->u_pcb.pcb_iopl = iopl;
    456   1.1    ad 	/* Force the change at ring 0. */
    457   1.1    ad #ifdef XEN3
    458   1.1    ad 	{
    459   1.1    ad 		struct physdev_op physop;
    460   1.1    ad 		physop.cmd = PHYSDEVOP_SET_IOPL;
    461   1.9  yamt 		physop.u.set_iopl.iopl = iopl;
    462   1.1    ad 		HYPERVISOR_physdev_op(&physop);
    463   1.1    ad 	}
    464   1.1    ad #else /* XEN3 */
    465   1.1    ad 	{
    466   1.1    ad 		dom0_op_t op;
    467   1.1    ad 		op.cmd = DOM0_IOPL;
    468   1.1    ad 		op.u.iopl.domain = DOMID_SELF;
    469   1.9  yamt 		op.u.iopl.iopl = iopl;
    470   1.1    ad 		HYPERVISOR_dom0_op(&op);
    471   1.1    ad 	}
    472   1.1    ad #endif /* XEN3 */
    473   1.1    ad #elif defined(__x86_64__)
    474   1.1    ad 	if (ua.iopl)
    475   1.1    ad 		tf->tf_rflags |= PSL_IOPL;
    476   1.1    ad 	else
    477   1.1    ad 		tf->tf_rflags &= ~PSL_IOPL;
    478   1.1    ad #else
    479   1.1    ad 	if (ua.iopl)
    480   1.1    ad 		tf->tf_eflags |= PSL_IOPL;
    481   1.1    ad 	else
    482   1.1    ad 		tf->tf_eflags &= ~PSL_IOPL;
    483   1.1    ad #endif
    484   1.1    ad 
    485   1.1    ad 	return 0;
    486   1.1    ad }
    487   1.1    ad 
    488   1.1    ad int
    489   1.1    ad x86_get_ioperm(struct lwp *l, void *args, register_t *retval)
    490   1.1    ad {
    491   1.1    ad #ifdef IOPERM
    492   1.1    ad 	int error;
    493   1.1    ad 	struct pcb *pcb = &l->l_addr->u_pcb;
    494   1.1    ad 	struct x86_get_ioperm_args ua;
    495   1.9  yamt 	void *dummymap = NULL;
    496   1.9  yamt 	void *iomap;
    497   1.1    ad 
    498   1.1    ad 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPERM_GET,
    499   1.1    ad 	    NULL, NULL, NULL, NULL);
    500   1.1    ad 	if (error)
    501   1.1    ad 		return (error);
    502   1.1    ad 
    503   1.1    ad 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    504   1.1    ad 		return (error);
    505   1.1    ad 
    506   1.9  yamt 	iomap = pcb->pcb_iomap;
    507   1.9  yamt 	if (iomap == NULL) {
    508   1.9  yamt 		iomap = dummymap = kmem_alloc(IOMAPSIZE, KM_SLEEP);
    509   1.9  yamt 		memset(dummymap, 0xff, IOMAPSIZE);
    510   1.9  yamt 	}
    511   1.9  yamt 	error = copyout(iomap, ua.iomap, IOMAPSIZE);
    512   1.9  yamt 	if (dummymap != NULL) {
    513   1.9  yamt 		kmem_free(dummymap, IOMAPSIZE);
    514   1.9  yamt 	}
    515   1.9  yamt 	return error;
    516   1.1    ad #else
    517   1.1    ad 	return EINVAL;
    518   1.1    ad #endif
    519   1.1    ad }
    520   1.1    ad 
    521   1.1    ad int
    522   1.1    ad x86_set_ioperm(struct lwp *l, void *args, register_t *retval)
    523   1.1    ad {
    524   1.1    ad #ifdef IOPERM
    525   1.9  yamt 	struct cpu_info *ci;
    526   1.1    ad 	int error;
    527   1.1    ad 	struct pcb *pcb = &l->l_addr->u_pcb;
    528   1.1    ad 	struct x86_set_ioperm_args ua;
    529   1.9  yamt 	void *new;
    530   1.9  yamt 	void *old;
    531   1.1    ad 
    532   1.1    ad   	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPERM_SET,
    533   1.1    ad 	    NULL, NULL, NULL, NULL);
    534   1.1    ad 	if (error)
    535   1.1    ad 		return (error);
    536   1.1    ad 
    537   1.1    ad 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    538   1.1    ad 		return (error);
    539   1.1    ad 
    540   1.9  yamt 	new = kmem_alloc(IOMAPSIZE, KM_SLEEP);
    541   1.9  yamt 	error = copyin(ua.iomap, new, IOMAPSIZE);
    542   1.9  yamt 	if (error) {
    543   1.9  yamt 		kmem_free(new, IOMAPSIZE);
    544   1.9  yamt 		return error;
    545   1.9  yamt 	}
    546   1.9  yamt 	old = pcb->pcb_iomap;
    547   1.9  yamt 	pcb->pcb_iomap = new;
    548   1.9  yamt 	if (old != NULL) {
    549   1.9  yamt 		kmem_free(old, IOMAPSIZE);
    550   1.9  yamt 	}
    551   1.9  yamt 
    552  1.13    ad 	kpreempt_disable();
    553   1.9  yamt 	ci = curcpu();
    554   1.9  yamt 	memcpy(ci->ci_iomap, pcb->pcb_iomap, sizeof(ci->ci_iomap));
    555   1.9  yamt 	ci->ci_tss.tss_iobase =
    556   1.9  yamt 	    ((uintptr_t)ci->ci_iomap - (uintptr_t)&ci->ci_tss) << 16;
    557  1.13    ad 	kpreempt_enable();
    558   1.9  yamt 
    559   1.9  yamt 	return error;
    560   1.1    ad #else
    561   1.1    ad 	return EINVAL;
    562   1.1    ad #endif
    563   1.1    ad }
    564   1.1    ad 
    565   1.1    ad int
    566   1.1    ad x86_get_mtrr(struct lwp *l, void *args, register_t *retval)
    567   1.1    ad {
    568   1.1    ad #ifdef MTRR
    569   1.1    ad 	struct x86_get_mtrr_args ua;
    570   1.1    ad 	int error, n;
    571   1.1    ad 
    572   1.1    ad 	if (mtrr_funcs == NULL)
    573   1.1    ad 		return ENOSYS;
    574   1.1    ad 
    575   1.1    ad  	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_MTRR_GET,
    576   1.1    ad 	    NULL, NULL, NULL, NULL);
    577   1.1    ad 	if (error)
    578   1.1    ad 		return (error);
    579   1.1    ad 
    580   1.1    ad 	error = copyin(args, &ua, sizeof ua);
    581   1.1    ad 	if (error != 0)
    582   1.1    ad 		return error;
    583   1.1    ad 
    584   1.1    ad 	error = copyin(ua.n, &n, sizeof n);
    585   1.1    ad 	if (error != 0)
    586   1.1    ad 		return error;
    587   1.1    ad 
    588  1.12    ad 	KERNEL_LOCK(1, NULL);
    589   1.1    ad 	error = mtrr_get(ua.mtrrp, &n, l->l_proc, MTRR_GETSET_USER);
    590  1.12    ad 	KERNEL_UNLOCK_ONE(NULL);
    591   1.1    ad 
    592   1.1    ad 	copyout(&n, ua.n, sizeof (int));
    593   1.1    ad 
    594   1.1    ad 	return error;
    595   1.1    ad #else
    596   1.1    ad 	return EINVAL;
    597   1.1    ad #endif
    598   1.1    ad }
    599   1.1    ad 
    600   1.1    ad int
    601   1.1    ad x86_set_mtrr(struct lwp *l, void *args, register_t *retval)
    602   1.1    ad {
    603   1.1    ad #ifdef MTRR
    604   1.1    ad 	int error, n;
    605   1.1    ad 	struct x86_set_mtrr_args ua;
    606   1.1    ad 
    607   1.1    ad 	if (mtrr_funcs == NULL)
    608   1.1    ad 		return ENOSYS;
    609   1.1    ad 
    610   1.1    ad  	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_MTRR_SET,
    611   1.1    ad 	    NULL, NULL, NULL, NULL);
    612   1.1    ad 	if (error)
    613   1.1    ad 		return (error);
    614   1.1    ad 
    615   1.1    ad 	error = copyin(args, &ua, sizeof ua);
    616   1.1    ad 	if (error != 0)
    617   1.1    ad 		return error;
    618   1.1    ad 
    619   1.1    ad 	error = copyin(ua.n, &n, sizeof n);
    620   1.1    ad 	if (error != 0)
    621   1.1    ad 		return error;
    622   1.1    ad 
    623  1.12    ad 	KERNEL_LOCK(1, NULL);
    624   1.1    ad 	error = mtrr_set(ua.mtrrp, &n, l->l_proc, MTRR_GETSET_USER);
    625   1.1    ad 	if (n != 0)
    626   1.1    ad 		mtrr_commit();
    627  1.12    ad 	KERNEL_UNLOCK_ONE(NULL);
    628   1.1    ad 
    629   1.1    ad 	copyout(&n, ua.n, sizeof n);
    630   1.1    ad 
    631   1.1    ad 	return error;
    632   1.1    ad #else
    633   1.1    ad 	return EINVAL;
    634   1.1    ad #endif
    635   1.1    ad }
    636   1.1    ad 
    637   1.1    ad int
    638   1.5    ad x86_set_sdbase(void *arg, char which)
    639   1.5    ad {
    640   1.5    ad #ifdef i386
    641   1.6    ad 	struct segment_descriptor sd;
    642   1.5    ad 	vaddr_t base;
    643   1.6    ad 	int error;
    644   1.5    ad 
    645   1.5    ad 	error = copyin(arg, &base, sizeof(base));
    646   1.5    ad 	if (error != 0)
    647   1.5    ad 		return error;
    648   1.5    ad 
    649   1.6    ad 	sd.sd_lobase = base & 0xffffff;
    650   1.6    ad 	sd.sd_hibase = (base >> 24) & 0xff;
    651   1.6    ad 	sd.sd_lolimit = 0xffff;
    652   1.6    ad 	sd.sd_hilimit = 0xf;
    653   1.6    ad 	sd.sd_type = SDT_MEMRWA;
    654   1.6    ad 	sd.sd_dpl = SEL_UPL;
    655   1.6    ad 	sd.sd_p = 1;
    656   1.6    ad 	sd.sd_xx = 0;
    657   1.6    ad 	sd.sd_def32 = 1;
    658   1.6    ad 	sd.sd_gran = 1;
    659   1.6    ad 
    660  1.13    ad 	kpreempt_disable();
    661   1.6    ad 	if (which == 'f') {
    662   1.6    ad 		memcpy(&curpcb->pcb_fsd, &sd, sizeof(sd));
    663   1.6    ad 		memcpy(&curcpu()->ci_gdt[GUFS_SEL], &sd, sizeof(sd));
    664   1.6    ad 	} else /* which == 'g' */ {
    665   1.6    ad 		memcpy(&curpcb->pcb_gsd, &sd, sizeof(sd));
    666   1.6    ad 		memcpy(&curcpu()->ci_gdt[GUGS_SEL], &sd, sizeof(sd));
    667   1.6    ad 	}
    668  1.13    ad 	kpreempt_enable();
    669   1.5    ad 
    670   1.5    ad 	return 0;
    671   1.5    ad #else
    672   1.5    ad 	return EINVAL;
    673   1.5    ad #endif
    674   1.5    ad }
    675   1.5    ad 
    676   1.5    ad int
    677   1.5    ad x86_get_sdbase(void *arg, char which)
    678   1.5    ad {
    679   1.5    ad #ifdef i386
    680   1.5    ad 	struct segment_descriptor *sd;
    681   1.5    ad 	vaddr_t base;
    682   1.5    ad 
    683   1.5    ad 	switch (which) {
    684   1.5    ad 	case 'f':
    685   1.6    ad 		sd = (struct segment_descriptor *)&curpcb->pcb_fsd;
    686   1.5    ad 		break;
    687   1.5    ad 	case 'g':
    688   1.6    ad 		sd = (struct segment_descriptor *)&curpcb->pcb_gsd;
    689   1.5    ad 		break;
    690   1.5    ad 	default:
    691   1.5    ad 		panic("x86_get_sdbase");
    692   1.5    ad 	}
    693   1.5    ad 
    694   1.5    ad 	base = sd->sd_hibase << 24 | sd->sd_lobase;
    695   1.5    ad 	return copyout(&base, &arg, sizeof(base));
    696   1.5    ad #else
    697   1.5    ad 	return EINVAL;
    698   1.5    ad #endif
    699   1.5    ad }
    700   1.5    ad 
    701   1.5    ad int
    702   1.8   dsl sys_sysarch(struct lwp *l, const struct sys_sysarch_args *uap, register_t *retval)
    703   1.1    ad {
    704   1.8   dsl 	/* {
    705   1.1    ad 		syscallarg(int) op;
    706   1.1    ad 		syscallarg(void *) parms;
    707   1.8   dsl 	} */
    708   1.1    ad 	int error = 0;
    709   1.1    ad 
    710   1.1    ad 	switch(SCARG(uap, op)) {
    711   1.1    ad 	case X86_IOPL:
    712   1.1    ad 		error = x86_iopl(l, SCARG(uap, parms), retval);
    713   1.1    ad 		break;
    714   1.1    ad 
    715   1.1    ad 	case X86_GET_LDT:
    716   1.1    ad 		error = x86_get_ldt(l, SCARG(uap, parms), retval);
    717   1.1    ad 		break;
    718   1.1    ad 
    719   1.1    ad 	case X86_SET_LDT:
    720   1.1    ad 		error = x86_set_ldt(l, SCARG(uap, parms), retval);
    721   1.1    ad 		break;
    722   1.1    ad 
    723   1.1    ad 	case X86_GET_IOPERM:
    724   1.1    ad 		error = x86_get_ioperm(l, SCARG(uap, parms), retval);
    725   1.1    ad 		break;
    726   1.1    ad 
    727   1.1    ad 	case X86_SET_IOPERM:
    728   1.1    ad 		error = x86_set_ioperm(l, SCARG(uap, parms), retval);
    729   1.1    ad 		break;
    730   1.1    ad 
    731   1.1    ad 	case X86_GET_MTRR:
    732   1.1    ad 		error = x86_get_mtrr(l, SCARG(uap, parms), retval);
    733   1.1    ad 		break;
    734   1.1    ad 	case X86_SET_MTRR:
    735   1.1    ad 		error = x86_set_mtrr(l, SCARG(uap, parms), retval);
    736   1.1    ad 		break;
    737   1.1    ad 
    738   1.1    ad #ifdef VM86
    739   1.1    ad 	case X86_VM86:
    740   1.1    ad 		error = x86_vm86(l, SCARG(uap, parms), retval);
    741   1.1    ad 		break;
    742   1.1    ad #ifdef COMPAT_16
    743   1.1    ad 	case X86_OLD_VM86:
    744   1.1    ad 		error = compat_16_x86_vm86(l, SCARG(uap, parms), retval);
    745   1.1    ad 		break;
    746   1.1    ad #endif
    747   1.1    ad #endif
    748   1.1    ad 
    749   1.1    ad #ifdef PERFCTRS
    750   1.1    ad 	case X86_PMC_INFO:
    751  1.12    ad 		KERNEL_LOCK(1, NULL);
    752   1.1    ad 		error = pmc_info(l, SCARG(uap, parms), retval);
    753  1.12    ad 		KERNEL_UNLOCK_ONE(NULL);
    754   1.1    ad 		break;
    755   1.1    ad 
    756   1.1    ad 	case X86_PMC_STARTSTOP:
    757  1.12    ad 		KERNEL_LOCK(1, NULL);
    758   1.1    ad 		error = pmc_startstop(l, SCARG(uap, parms), retval);
    759  1.12    ad 		KERNEL_UNLOCK_ONE(NULL);
    760   1.1    ad 		break;
    761   1.1    ad 
    762   1.1    ad 	case X86_PMC_READ:
    763  1.12    ad 		KERNEL_LOCK(1, NULL);
    764   1.1    ad 		error = pmc_read(l, SCARG(uap, parms), retval);
    765  1.12    ad 		KERNEL_UNLOCK_ONE(NULL);
    766   1.1    ad 		break;
    767   1.1    ad #endif
    768   1.1    ad 
    769   1.5    ad 	case X86_SET_FSBASE:
    770   1.5    ad 		error = x86_set_sdbase(SCARG(uap, parms), 'f');
    771   1.5    ad 		break;
    772   1.5    ad 
    773   1.5    ad 	case X86_SET_GSBASE:
    774   1.5    ad 		error = x86_set_sdbase(SCARG(uap, parms), 'g');
    775   1.5    ad 		break;
    776   1.5    ad 
    777   1.5    ad 	case X86_GET_FSBASE:
    778   1.5    ad 		error = x86_get_sdbase(SCARG(uap, parms), 'f');
    779   1.5    ad 		break;
    780   1.5    ad 
    781   1.5    ad 	case X86_GET_GSBASE:
    782   1.5    ad 		error = x86_get_sdbase(SCARG(uap, parms), 'g');
    783   1.5    ad 		break;
    784   1.5    ad 
    785   1.1    ad 	default:
    786   1.1    ad 		error = EINVAL;
    787   1.1    ad 		break;
    788   1.1    ad 	}
    789   1.1    ad 	return (error);
    790   1.1    ad }
    791