Home | History | Annotate | Line # | Download | only in x86
sys_machdep.c revision 1.2
      1  1.2  dsl /*	$NetBSD: sys_machdep.c,v 1.2 2007/06/23 15:22:18 dsl 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.2  dsl __KERNEL_RCSID(0, "$NetBSD: sys_machdep.c,v 1.2 2007/06/23 15:22:18 dsl 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.1   ad #include <sys/kauth.h>
     62  1.1   ad 
     63  1.1   ad #include <sys/mount.h>
     64  1.1   ad #include <sys/syscallargs.h>
     65  1.1   ad 
     66  1.1   ad #include <uvm/uvm_extern.h>
     67  1.1   ad 
     68  1.1   ad #include <machine/cpu.h>
     69  1.1   ad #include <machine/cpufunc.h>
     70  1.1   ad #include <machine/gdt.h>
     71  1.1   ad #include <machine/psl.h>
     72  1.1   ad #include <machine/reg.h>
     73  1.1   ad #include <machine/sysarch.h>
     74  1.1   ad #include <machine/mtrr.h>
     75  1.1   ad 
     76  1.1   ad #ifdef __x86_64__
     77  1.1   ad /* Need to be checked. */
     78  1.1   ad #undef	USER_LDT
     79  1.1   ad #undef	PERFCTRS
     80  1.1   ad #undef	VM86
     81  1.1   ad #undef	IOPERM
     82  1.1   ad #else
     83  1.1   ad #define	IOPERM
     84  1.1   ad #endif
     85  1.1   ad 
     86  1.1   ad #ifdef VM86
     87  1.1   ad #include <machine/vm86.h>
     88  1.1   ad #endif
     89  1.1   ad 
     90  1.1   ad #ifdef PERFCTRS
     91  1.1   ad #include <machine/pmc.h>
     92  1.1   ad #endif
     93  1.1   ad 
     94  1.1   ad extern struct vm_map *kernel_map;
     95  1.1   ad 
     96  1.1   ad int x86_get_ioperm(struct lwp *, void *, register_t *);
     97  1.1   ad int x86_set_ioperm(struct lwp *, void *, register_t *);
     98  1.1   ad int x86_get_mtrr(struct lwp *, void *, register_t *);
     99  1.1   ad int x86_set_mtrr(struct lwp *, void *, register_t *);
    100  1.1   ad 
    101  1.1   ad #ifdef LDT_DEBUG
    102  1.1   ad static void x86_print_ldt(int, const struct segment_descriptor *);
    103  1.1   ad 
    104  1.1   ad static void
    105  1.1   ad x86_print_ldt(int i, const struct segment_descriptor *d)
    106  1.1   ad {
    107  1.1   ad 	printf("[%d] lolimit=0x%x, lobase=0x%x, type=%u, dpl=%u, p=%u, "
    108  1.1   ad 	    "hilimit=0x%x, xx=%x, def32=%u, gran=%u, hibase=0x%x\n",
    109  1.1   ad 	    i, d->sd_lolimit, d->sd_lobase, d->sd_type, d->sd_dpl, d->sd_p,
    110  1.1   ad 	    d->sd_hilimit, d->sd_xx, d->sd_def32, d->sd_gran, d->sd_hibase);
    111  1.1   ad }
    112  1.1   ad #endif
    113  1.1   ad 
    114  1.1   ad int
    115  1.2  dsl x86_get_ldt_len(struct lwp *l)
    116  1.2  dsl {
    117  1.2  dsl #ifndef USER_LDT
    118  1.2  dsl 	return -1;
    119  1.2  dsl #else
    120  1.2  dsl 	pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap;
    121  1.2  dsl 	int nldt;
    122  1.2  dsl 
    123  1.2  dsl 	simple_lock(&pmap->pm_lock);
    124  1.2  dsl 
    125  1.2  dsl 	if (pmap->pm_flags & PMF_USER_LDT) {
    126  1.2  dsl 		nldt = pmap->pm_ldt_len;
    127  1.2  dsl 	} else {
    128  1.2  dsl 		nldt = NLDT;
    129  1.2  dsl 	}
    130  1.2  dsl 	simple_unlock(&pmap->pm_lock);
    131  1.2  dsl 	return nldt;
    132  1.2  dsl #endif
    133  1.2  dsl }
    134  1.2  dsl 
    135  1.2  dsl 
    136  1.2  dsl int
    137  1.1   ad x86_get_ldt(struct lwp *l, void *args, register_t *retval)
    138  1.1   ad {
    139  1.2  dsl #ifndef USER_LDT
    140  1.2  dsl 	return EINVAL;
    141  1.2  dsl #else
    142  1.2  dsl 	struct x86_get_ldt_args ua;
    143  1.2  dsl 	union descriptor *cp;
    144  1.2  dsl 	int error;
    145  1.2  dsl 
    146  1.2  dsl 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    147  1.2  dsl 		return error;
    148  1.2  dsl 
    149  1.2  dsl 	if (ua.num < 0 || ua.num > 8192)
    150  1.2  dsl 		return EINVAL;
    151  1.2  dsl 
    152  1.2  dsl 	cp = malloc(ua.num * sizeof(union descriptor), M_TEMP, M_WAITOK);
    153  1.2  dsl 	if (cp == NULL)
    154  1.2  dsl 		return ENOMEM;
    155  1.2  dsl 
    156  1.2  dsl 	error = x86_get_ldt1(l, &ua, cp);
    157  1.2  dsl 	*retval = ua.num;
    158  1.2  dsl 	if (error == 0)
    159  1.2  dsl 		error = copyout(cp, ua.desc, ua.num * sizeof(*cp));
    160  1.2  dsl 
    161  1.2  dsl 	free(cp, M_TEMP);
    162  1.2  dsl 	return error;
    163  1.2  dsl #endif
    164  1.2  dsl }
    165  1.2  dsl 
    166  1.2  dsl int
    167  1.2  dsl x86_get_ldt1(struct lwp *l, struct x86_get_ldt_args *ua, union descriptor *cp)
    168  1.2  dsl {
    169  1.2  dsl #ifndef USER_LDT
    170  1.2  dsl 	return EINVAL;
    171  1.2  dsl #else
    172  1.1   ad 	int error;
    173  1.1   ad 	struct proc *p = l->l_proc;
    174  1.1   ad 	pmap_t pmap = p->p_vmspace->vm_map.pmap;
    175  1.1   ad 	int nldt, num;
    176  1.2  dsl 	union descriptor *lp;
    177  1.1   ad 
    178  1.1   ad 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_LDT_GET,
    179  1.1   ad 	    NULL, NULL, NULL, NULL);
    180  1.1   ad 	if (error)
    181  1.1   ad 		return (error);
    182  1.1   ad 
    183  1.1   ad #ifdef	LDT_DEBUG
    184  1.2  dsl 	printf("x86_get_ldt: start=%d num=%d descs=%p\n", ua->start,
    185  1.2  dsl 	    ua->num, ua->desc);
    186  1.1   ad #endif
    187  1.1   ad 
    188  1.2  dsl 	if (ua->start < 0 || ua->num < 0 || ua->start > 8192 || ua->num > 8192 ||
    189  1.2  dsl 	    ua->start + ua->num > 8192)
    190  1.1   ad 		return (EINVAL);
    191  1.1   ad 
    192  1.1   ad 	simple_lock(&pmap->pm_lock);
    193  1.1   ad 
    194  1.1   ad 	if (pmap->pm_flags & PMF_USER_LDT) {
    195  1.1   ad 		nldt = pmap->pm_ldt_len;
    196  1.1   ad 		lp = pmap->pm_ldt;
    197  1.1   ad 	} else {
    198  1.1   ad 		nldt = NLDT;
    199  1.1   ad 		lp = ldt;
    200  1.1   ad 	}
    201  1.1   ad 
    202  1.2  dsl 	if (ua->start > nldt) {
    203  1.1   ad 		simple_unlock(&pmap->pm_lock);
    204  1.1   ad 		return (EINVAL);
    205  1.1   ad 	}
    206  1.1   ad 
    207  1.2  dsl 	lp += ua->start;
    208  1.2  dsl 	num = min(ua->num, nldt - ua->start);
    209  1.2  dsl 	ua->num = num;
    210  1.1   ad #ifdef LDT_DEBUG
    211  1.1   ad 	{
    212  1.1   ad 		int i;
    213  1.1   ad 		for (i = 0; i < num; i++)
    214  1.1   ad 			x86_print_ldt(i, &lp[i].sd);
    215  1.1   ad 	}
    216  1.1   ad #endif
    217  1.1   ad 
    218  1.1   ad 	memcpy(cp, lp, num * sizeof(union descriptor));
    219  1.1   ad 	simple_unlock(&pmap->pm_lock);
    220  1.1   ad 
    221  1.2  dsl 	return 0;
    222  1.2  dsl #endif
    223  1.2  dsl }
    224  1.2  dsl 
    225  1.2  dsl int
    226  1.2  dsl x86_set_ldt(struct lwp *l, void *args, register_t *retval)
    227  1.2  dsl {
    228  1.2  dsl #ifndef USER_LDT
    229  1.2  dsl 	return EINVAL;
    230  1.2  dsl #else
    231  1.2  dsl 	struct x86_set_ldt_args ua;
    232  1.2  dsl 	union descriptor *descv;
    233  1.2  dsl 	int error;
    234  1.2  dsl 
    235  1.2  dsl 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    236  1.2  dsl 		return (error);
    237  1.2  dsl 
    238  1.2  dsl 	if (ua.num < 0 || ua.num > 8192)
    239  1.2  dsl 		return EINVAL;
    240  1.2  dsl 
    241  1.2  dsl 	descv = malloc(sizeof (*descv) * ua.num, M_TEMP, M_NOWAIT);
    242  1.2  dsl 	if (descv == NULL)
    243  1.2  dsl 		return ENOMEM;
    244  1.2  dsl 
    245  1.2  dsl 	error = copyin(ua.desc, descv, sizeof (*descv) * ua.num);
    246  1.1   ad 	if (error == 0)
    247  1.2  dsl 		error = x86_set_ldt1(l, &ua, descv);
    248  1.2  dsl 	*retval = ua.start;
    249  1.1   ad 
    250  1.2  dsl 	free(descv, M_TEMP);
    251  1.2  dsl 	return error;
    252  1.1   ad #endif
    253  1.1   ad }
    254  1.1   ad 
    255  1.1   ad int
    256  1.2  dsl x86_set_ldt1(struct lwp *l, struct x86_set_ldt_args *ua,
    257  1.2  dsl     union descriptor *descv)
    258  1.1   ad {
    259  1.2  dsl #ifndef USER_LDT
    260  1.2  dsl 	return EINVAL;
    261  1.2  dsl #else
    262  1.1   ad 	int error, i, n, sel, free_sel;
    263  1.1   ad 	struct proc *p = l->l_proc;
    264  1.1   ad 	struct pcb *pcb = &l->l_addr->u_pcb;
    265  1.1   ad 	pmap_t pmap = p->p_vmspace->vm_map.pmap;
    266  1.1   ad 	size_t old_len, new_len, ldt_len, free_len;
    267  1.1   ad 	union descriptor *old_ldt, *new_ldt, *free_ldt;
    268  1.1   ad 
    269  1.1   ad 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_LDT_SET,
    270  1.1   ad 	    NULL, NULL, NULL, NULL);
    271  1.1   ad 	if (error)
    272  1.1   ad 		return (error);
    273  1.1   ad 
    274  1.2  dsl 	if (ua->start < 0 || ua->num < 0 || ua->start > 8192 || ua->num > 8192 ||
    275  1.2  dsl 	    ua->start + ua->num > 8192)
    276  1.1   ad 		return (EINVAL);
    277  1.1   ad 
    278  1.1   ad 	/* Check descriptors for access violations. */
    279  1.2  dsl 	for (i = 0; i < ua->num; i++) {
    280  1.1   ad 		union descriptor *desc = &descv[i];
    281  1.1   ad 
    282  1.1   ad 		switch (desc->sd.sd_type) {
    283  1.1   ad 		case SDT_SYSNULL:
    284  1.1   ad 			desc->sd.sd_p = 0;
    285  1.1   ad 			break;
    286  1.1   ad 		case SDT_SYS286CGT:
    287  1.1   ad 		case SDT_SYS386CGT:
    288  1.1   ad 			/*
    289  1.1   ad 			 * Only allow call gates targeting a segment
    290  1.1   ad 			 * in the LDT or a user segment in the fixed
    291  1.1   ad 			 * part of the gdt.  Segments in the LDT are
    292  1.1   ad 			 * constrained (below) to be user segments.
    293  1.1   ad 			 */
    294  1.1   ad 			if (desc->gd.gd_p != 0 &&
    295  1.1   ad 			    !ISLDT(desc->gd.gd_selector) &&
    296  1.1   ad 			    ((IDXSEL(desc->gd.gd_selector) >= NGDT) ||
    297  1.1   ad 			     (gdt[IDXSEL(desc->gd.gd_selector)].sd.sd_dpl !=
    298  1.1   ad 				 SEL_UPL))) {
    299  1.2  dsl 				return EACCES;
    300  1.1   ad 			}
    301  1.1   ad 			break;
    302  1.1   ad 		case SDT_MEMEC:
    303  1.1   ad 		case SDT_MEMEAC:
    304  1.1   ad 		case SDT_MEMERC:
    305  1.1   ad 		case SDT_MEMERAC:
    306  1.1   ad 			/* Must be "present" if executable and conforming. */
    307  1.2  dsl 			if (desc->sd.sd_p == 0)
    308  1.2  dsl 				return EACCES;
    309  1.1   ad 			break;
    310  1.1   ad 		case SDT_MEMRO:
    311  1.1   ad 		case SDT_MEMROA:
    312  1.1   ad 		case SDT_MEMRW:
    313  1.1   ad 		case SDT_MEMRWA:
    314  1.1   ad 		case SDT_MEMROD:
    315  1.1   ad 		case SDT_MEMRODA:
    316  1.1   ad 		case SDT_MEMRWD:
    317  1.1   ad 		case SDT_MEMRWDA:
    318  1.1   ad 		case SDT_MEME:
    319  1.1   ad 		case SDT_MEMEA:
    320  1.1   ad 		case SDT_MEMER:
    321  1.1   ad 		case SDT_MEMERA:
    322  1.1   ad 			break;
    323  1.1   ad 		default:
    324  1.1   ad 			/*
    325  1.1   ad 			 * Make sure that unknown descriptor types are
    326  1.1   ad 			 * not marked present.
    327  1.1   ad 			 */
    328  1.2  dsl 			if (desc->sd.sd_p != 0)
    329  1.2  dsl 				return EACCES;
    330  1.1   ad 			break;
    331  1.1   ad 		}
    332  1.1   ad 
    333  1.1   ad 		if (desc->sd.sd_p != 0) {
    334  1.1   ad 			/* Only user (ring-3) descriptors may be present. */
    335  1.2  dsl 			if (desc->sd.sd_dpl != SEL_UPL)
    336  1.2  dsl 				return EACCES;
    337  1.1   ad 		}
    338  1.1   ad 	}
    339  1.1   ad 
    340  1.1   ad 	/* allocate user ldt */
    341  1.1   ad 	free_sel = -1;
    342  1.1   ad 	new_ldt = NULL;
    343  1.1   ad 	new_len = 0;
    344  1.1   ad 	free_ldt = NULL;
    345  1.1   ad 	free_len = 0;
    346  1.1   ad 	simple_lock(&pmap->pm_lock);
    347  1.2  dsl 	if (pmap->pm_ldt == 0 || (ua->start + ua->num) > pmap->pm_ldt_len) {
    348  1.1   ad 		if (pmap->pm_flags & PMF_USER_LDT)
    349  1.1   ad 			ldt_len = pmap->pm_ldt_len;
    350  1.1   ad 		else
    351  1.1   ad 			ldt_len = 512;
    352  1.2  dsl 		while ((ua->start + ua->num) > ldt_len)
    353  1.1   ad 			ldt_len *= 2;
    354  1.1   ad 		new_len = ldt_len * sizeof(union descriptor);
    355  1.1   ad 
    356  1.1   ad 		simple_unlock(&pmap->pm_lock);
    357  1.1   ad 		new_ldt = (union descriptor *)uvm_km_alloc(kernel_map,
    358  1.1   ad 		    new_len, 0, UVM_KMF_WIRED);
    359  1.1   ad 		memset(new_ldt, 0, new_len);
    360  1.1   ad 		sel = ldt_alloc(new_ldt, new_len);
    361  1.1   ad 		simple_lock(&pmap->pm_lock);
    362  1.1   ad 
    363  1.1   ad 		if (pmap->pm_ldt != NULL && ldt_len <= pmap->pm_ldt_len) {
    364  1.1   ad 			/*
    365  1.1   ad 			 * Another thread (re)allocated the LDT to
    366  1.1   ad 			 * sufficient size while we were blocked in
    367  1.1   ad 			 * uvm_km_alloc. Oh well. The new entries
    368  1.1   ad 			 * will quite probably not be right, but
    369  1.1   ad 			 * hey.. not our problem if user applications
    370  1.1   ad 			 * have race conditions like that.
    371  1.1   ad 			 */
    372  1.1   ad 			goto copy;
    373  1.1   ad 		}
    374  1.1   ad 
    375  1.1   ad 		old_ldt = pmap->pm_ldt;
    376  1.1   ad 		free_ldt = old_ldt;
    377  1.1   ad 		free_len = pmap->pm_ldt_len * sizeof(union descriptor);
    378  1.1   ad 
    379  1.1   ad 		if (old_ldt != NULL) {
    380  1.1   ad 			old_len = pmap->pm_ldt_len * sizeof(union descriptor);
    381  1.1   ad 		} else {
    382  1.1   ad 			old_len = NLDT * sizeof(union descriptor);
    383  1.1   ad 			old_ldt = ldt;
    384  1.1   ad 		}
    385  1.1   ad 
    386  1.1   ad 		memcpy(new_ldt, old_ldt, old_len);
    387  1.1   ad 		memset((char *)new_ldt + old_len, 0, new_len - old_len);
    388  1.1   ad 
    389  1.1   ad 		pmap->pm_ldt = new_ldt;
    390  1.1   ad 		pmap->pm_ldt_len = ldt_len;
    391  1.1   ad 
    392  1.1   ad 		if (pmap->pm_flags & PMF_USER_LDT)
    393  1.1   ad 			free_sel = pmap->pm_ldt_sel;
    394  1.1   ad 		else {
    395  1.1   ad 			pmap->pm_flags |= PMF_USER_LDT;
    396  1.1   ad 			free_sel = -1;
    397  1.1   ad 		}
    398  1.1   ad 		pmap->pm_ldt_sel = sel;
    399  1.1   ad 		pcb->pcb_ldt_sel = pmap->pm_ldt_sel;
    400  1.1   ad 		if (pcb == curpcb)
    401  1.1   ad 			lldt(pcb->pcb_ldt_sel);
    402  1.1   ad 		new_ldt = NULL;
    403  1.1   ad 	}
    404  1.1   ad copy:
    405  1.1   ad 	/* Now actually replace the descriptors. */
    406  1.2  dsl 	for (i = 0, n = ua->start; i < ua->num; i++, n++)
    407  1.1   ad 		pmap->pm_ldt[n] = descv[i];
    408  1.1   ad 
    409  1.1   ad 	simple_unlock(&pmap->pm_lock);
    410  1.1   ad 
    411  1.1   ad 	if (new_ldt != NULL)
    412  1.1   ad 		uvm_km_free(kernel_map, (vaddr_t)new_ldt, new_len,
    413  1.1   ad 		    UVM_KMF_WIRED);
    414  1.1   ad 	if (free_sel != -1)
    415  1.1   ad 		ldt_free(free_sel);
    416  1.1   ad 	if (free_ldt != NULL)
    417  1.1   ad 		uvm_km_free(kernel_map, (vaddr_t)free_ldt, free_len,
    418  1.1   ad 		    UVM_KMF_WIRED);
    419  1.2  dsl 
    420  1.1   ad 	return (error);
    421  1.1   ad #endif
    422  1.1   ad }
    423  1.1   ad 
    424  1.1   ad int
    425  1.1   ad x86_iopl(struct lwp *l, void *args, register_t *retval)
    426  1.1   ad {
    427  1.1   ad 	int error;
    428  1.1   ad 	struct x86_iopl_args ua;
    429  1.1   ad #ifdef XEN
    430  1.1   ad 	struct pcb *pcb = &l->l_addr->u_pcb;
    431  1.1   ad #else
    432  1.1   ad 	struct trapframe *tf = l->l_md.md_regs;
    433  1.1   ad #endif
    434  1.1   ad 
    435  1.1   ad 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPL,
    436  1.1   ad 	    NULL, NULL, NULL, NULL);
    437  1.1   ad 	if (error)
    438  1.1   ad 		return (error);
    439  1.1   ad 
    440  1.1   ad 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    441  1.1   ad 		return error;
    442  1.1   ad 
    443  1.1   ad #ifdef XEN
    444  1.1   ad 	{
    445  1.1   ad 		pcb->pcb_tss.tss_ioopt &= ~SEL_RPL;
    446  1.1   ad 		if (ua.iopl)
    447  1.1   ad 			pcb->pcb_tss.tss_ioopt |= SEL_UPL; /* i/o pl */
    448  1.1   ad 		else
    449  1.1   ad 			pcb->pcb_tss.tss_ioopt |= SEL_KPL; /* i/o pl */
    450  1.1   ad 	}
    451  1.1   ad 	/* Force the change at ring 0. */
    452  1.1   ad #ifdef XEN3
    453  1.1   ad 	{
    454  1.1   ad 		struct physdev_op physop;
    455  1.1   ad 		physop.cmd = PHYSDEVOP_SET_IOPL;
    456  1.1   ad 		physop.u.set_iopl.iopl = pcb->pcb_tss.tss_ioopt & SEL_RPL;
    457  1.1   ad 		HYPERVISOR_physdev_op(&physop);
    458  1.1   ad 	}
    459  1.1   ad #else /* XEN3 */
    460  1.1   ad 	{
    461  1.1   ad 		dom0_op_t op;
    462  1.1   ad 		op.cmd = DOM0_IOPL;
    463  1.1   ad 		op.u.iopl.domain = DOMID_SELF;
    464  1.1   ad 		op.u.iopl.iopl = pcb->pcb_tss.tss_ioopt & SEL_RPL; /* i/o pl */
    465  1.1   ad 		HYPERVISOR_dom0_op(&op);
    466  1.1   ad 	}
    467  1.1   ad #endif /* XEN3 */
    468  1.1   ad #elif defined(__x86_64__)
    469  1.1   ad 	if (ua.iopl)
    470  1.1   ad 		tf->tf_rflags |= PSL_IOPL;
    471  1.1   ad 	else
    472  1.1   ad 		tf->tf_rflags &= ~PSL_IOPL;
    473  1.1   ad #else
    474  1.1   ad 	if (ua.iopl)
    475  1.1   ad 		tf->tf_eflags |= PSL_IOPL;
    476  1.1   ad 	else
    477  1.1   ad 		tf->tf_eflags &= ~PSL_IOPL;
    478  1.1   ad #endif
    479  1.1   ad 
    480  1.1   ad 	return 0;
    481  1.1   ad }
    482  1.1   ad 
    483  1.1   ad int
    484  1.1   ad x86_get_ioperm(struct lwp *l, void *args, register_t *retval)
    485  1.1   ad {
    486  1.1   ad #ifdef IOPERM
    487  1.1   ad 	int error;
    488  1.1   ad 	struct pcb *pcb = &l->l_addr->u_pcb;
    489  1.1   ad 	struct x86_get_ioperm_args ua;
    490  1.1   ad 
    491  1.1   ad 	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPERM_GET,
    492  1.1   ad 	    NULL, NULL, NULL, NULL);
    493  1.1   ad 	if (error)
    494  1.1   ad 		return (error);
    495  1.1   ad 
    496  1.1   ad 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    497  1.1   ad 		return (error);
    498  1.1   ad 
    499  1.1   ad 	return copyout(pcb->pcb_iomap, ua.iomap, sizeof(pcb->pcb_iomap));
    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_ioperm(struct lwp *l, void *args, register_t *retval)
    507  1.1   ad {
    508  1.1   ad #ifdef IOPERM
    509  1.1   ad 	int error;
    510  1.1   ad 	struct pcb *pcb = &l->l_addr->u_pcb;
    511  1.1   ad 	struct x86_set_ioperm_args ua;
    512  1.1   ad 
    513  1.1   ad   	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPERM_SET,
    514  1.1   ad 	    NULL, NULL, NULL, NULL);
    515  1.1   ad 	if (error)
    516  1.1   ad 		return (error);
    517  1.1   ad 
    518  1.1   ad 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
    519  1.1   ad 		return (error);
    520  1.1   ad 
    521  1.1   ad 	return copyin(ua.iomap, pcb->pcb_iomap, sizeof(pcb->pcb_iomap));
    522  1.1   ad #else
    523  1.1   ad 	return EINVAL;
    524  1.1   ad #endif
    525  1.1   ad }
    526  1.1   ad 
    527  1.1   ad int
    528  1.1   ad x86_get_mtrr(struct lwp *l, void *args, register_t *retval)
    529  1.1   ad {
    530  1.1   ad #ifdef MTRR
    531  1.1   ad 	struct x86_get_mtrr_args ua;
    532  1.1   ad 	int error, n;
    533  1.1   ad 
    534  1.1   ad 	if (mtrr_funcs == NULL)
    535  1.1   ad 		return ENOSYS;
    536  1.1   ad 
    537  1.1   ad  	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_MTRR_GET,
    538  1.1   ad 	    NULL, NULL, NULL, NULL);
    539  1.1   ad 	if (error)
    540  1.1   ad 		return (error);
    541  1.1   ad 
    542  1.1   ad 	error = copyin(args, &ua, sizeof ua);
    543  1.1   ad 	if (error != 0)
    544  1.1   ad 		return error;
    545  1.1   ad 
    546  1.1   ad 	error = copyin(ua.n, &n, sizeof n);
    547  1.1   ad 	if (error != 0)
    548  1.1   ad 		return error;
    549  1.1   ad 
    550  1.1   ad 	error = mtrr_get(ua.mtrrp, &n, l->l_proc, MTRR_GETSET_USER);
    551  1.1   ad 
    552  1.1   ad 	copyout(&n, ua.n, sizeof (int));
    553  1.1   ad 
    554  1.1   ad 	return error;
    555  1.1   ad #else
    556  1.1   ad 	return EINVAL;
    557  1.1   ad #endif
    558  1.1   ad }
    559  1.1   ad 
    560  1.1   ad int
    561  1.1   ad x86_set_mtrr(struct lwp *l, void *args, register_t *retval)
    562  1.1   ad {
    563  1.1   ad #ifdef MTRR
    564  1.1   ad 	int error, n;
    565  1.1   ad 	struct x86_set_mtrr_args ua;
    566  1.1   ad 
    567  1.1   ad 	if (mtrr_funcs == NULL)
    568  1.1   ad 		return ENOSYS;
    569  1.1   ad 
    570  1.1   ad  	error = kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_MTRR_SET,
    571  1.1   ad 	    NULL, NULL, NULL, NULL);
    572  1.1   ad 	if (error)
    573  1.1   ad 		return (error);
    574  1.1   ad 
    575  1.1   ad 	error = copyin(args, &ua, sizeof ua);
    576  1.1   ad 	if (error != 0)
    577  1.1   ad 		return error;
    578  1.1   ad 
    579  1.1   ad 	error = copyin(ua.n, &n, sizeof n);
    580  1.1   ad 	if (error != 0)
    581  1.1   ad 		return error;
    582  1.1   ad 
    583  1.1   ad 	error = mtrr_set(ua.mtrrp, &n, l->l_proc, MTRR_GETSET_USER);
    584  1.1   ad 	if (n != 0)
    585  1.1   ad 		mtrr_commit();
    586  1.1   ad 
    587  1.1   ad 	copyout(&n, ua.n, sizeof n);
    588  1.1   ad 
    589  1.1   ad 	return error;
    590  1.1   ad #else
    591  1.1   ad 	return EINVAL;
    592  1.1   ad #endif
    593  1.1   ad }
    594  1.1   ad 
    595  1.1   ad int
    596  1.1   ad sys_sysarch(struct lwp *l, void *v, register_t *retval)
    597  1.1   ad {
    598  1.1   ad 	struct sys_sysarch_args /* {
    599  1.1   ad 		syscallarg(int) op;
    600  1.1   ad 		syscallarg(void *) parms;
    601  1.1   ad 	} */ *uap = v;
    602  1.1   ad 	int error = 0;
    603  1.1   ad 
    604  1.1   ad 	switch(SCARG(uap, op)) {
    605  1.1   ad 	case X86_IOPL:
    606  1.1   ad 		error = x86_iopl(l, SCARG(uap, parms), retval);
    607  1.1   ad 		break;
    608  1.1   ad 
    609  1.1   ad 	case X86_GET_LDT:
    610  1.1   ad 		error = x86_get_ldt(l, SCARG(uap, parms), retval);
    611  1.1   ad 		break;
    612  1.1   ad 
    613  1.1   ad 	case X86_SET_LDT:
    614  1.1   ad 		error = x86_set_ldt(l, SCARG(uap, parms), retval);
    615  1.1   ad 		break;
    616  1.1   ad 
    617  1.1   ad 	case X86_GET_IOPERM:
    618  1.1   ad 		error = x86_get_ioperm(l, SCARG(uap, parms), retval);
    619  1.1   ad 		break;
    620  1.1   ad 
    621  1.1   ad 	case X86_SET_IOPERM:
    622  1.1   ad 		error = x86_set_ioperm(l, SCARG(uap, parms), retval);
    623  1.1   ad 		break;
    624  1.1   ad 
    625  1.1   ad 	case X86_GET_MTRR:
    626  1.1   ad 		error = x86_get_mtrr(l, SCARG(uap, parms), retval);
    627  1.1   ad 		break;
    628  1.1   ad 	case X86_SET_MTRR:
    629  1.1   ad 		error = x86_set_mtrr(l, SCARG(uap, parms), retval);
    630  1.1   ad 		break;
    631  1.1   ad 
    632  1.1   ad #ifdef VM86
    633  1.1   ad 	case X86_VM86:
    634  1.1   ad 		error = x86_vm86(l, SCARG(uap, parms), retval);
    635  1.1   ad 		break;
    636  1.1   ad #ifdef COMPAT_16
    637  1.1   ad 	case X86_OLD_VM86:
    638  1.1   ad 		error = compat_16_x86_vm86(l, SCARG(uap, parms), retval);
    639  1.1   ad 		break;
    640  1.1   ad #endif
    641  1.1   ad #endif
    642  1.1   ad 
    643  1.1   ad #ifdef PERFCTRS
    644  1.1   ad 	case X86_PMC_INFO:
    645  1.1   ad 		error = pmc_info(l, SCARG(uap, parms), retval);
    646  1.1   ad 		break;
    647  1.1   ad 
    648  1.1   ad 	case X86_PMC_STARTSTOP:
    649  1.1   ad 		error = pmc_startstop(l, SCARG(uap, parms), retval);
    650  1.1   ad 		break;
    651  1.1   ad 
    652  1.1   ad 	case X86_PMC_READ:
    653  1.1   ad 		error = pmc_read(l, SCARG(uap, parms), retval);
    654  1.1   ad 		break;
    655  1.1   ad #endif
    656  1.1   ad 
    657  1.1   ad 	default:
    658  1.1   ad 		error = EINVAL;
    659  1.1   ad 		break;
    660  1.1   ad 	}
    661  1.1   ad 	return (error);
    662  1.1   ad }
    663