2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <machine/cpufunc.h>
40 vmx_ctl_allows_one_setting(uint64_t msr_val, int bitpos)
43 if (msr_val & (1UL << (bitpos + 32)))
50 vmx_ctl_allows_zero_setting(uint64_t msr_val, int bitpos)
53 if ((msr_val & (1UL << bitpos)) == 0)
63 return (rdmsr(MSR_VMX_BASIC) & 0xffffffff);
67 * Generate a bitmask to be used for the VMCS execution control fields.
69 * The caller specifies what bits should be set to one in 'ones_mask'
70 * and what bits should be set to zero in 'zeros_mask'. The don't-care
71 * bits are set to the default value. The default values are obtained
72 * based on "Algorithm 3" in Section 27.5.1 "Algorithms for Determining
75 * Returns zero on success and non-zero on error.
78 vmx_set_ctlreg(int ctl_reg, int true_ctl_reg, uint32_t ones_mask,
79 uint32_t zeros_mask, uint32_t *retval)
82 uint64_t val, trueval;
83 boolean_t true_ctls_avail, one_allowed, zero_allowed;
85 /* We cannot ask the same bit to be set to both '1' and '0' */
86 if ((ones_mask ^ zeros_mask) != (ones_mask | zeros_mask))
89 if (rdmsr(MSR_VMX_BASIC) & (1UL << 55))
90 true_ctls_avail = TRUE;
92 true_ctls_avail = FALSE;
96 trueval = rdmsr(true_ctl_reg); /* step c */
98 trueval = val; /* step a */
100 for (i = 0; i < 32; i++) {
101 one_allowed = vmx_ctl_allows_one_setting(trueval, i);
102 zero_allowed = vmx_ctl_allows_zero_setting(trueval, i);
104 KASSERT(one_allowed || zero_allowed,
105 ("invalid zero/one setting for bit %d of ctl 0x%0x, "
106 "truectl 0x%0x\n", i, ctl_reg, true_ctl_reg));
108 if (zero_allowed && !one_allowed) { /* b(i),c(i) */
109 if (ones_mask & (1 << i))
111 *retval &= ~(1 << i);
112 } else if (one_allowed && !zero_allowed) { /* b(i),c(i) */
113 if (zeros_mask & (1 << i))
117 if (zeros_mask & (1 << i)) /* b(ii),c(ii) */
118 *retval &= ~(1 << i);
119 else if (ones_mask & (1 << i)) /* b(ii), c(ii) */
121 else if (!true_ctls_avail)
122 *retval &= ~(1 << i); /* b(iii) */
123 else if (vmx_ctl_allows_zero_setting(val, i))/* c(iii)*/
124 *retval &= ~(1 << i);
125 else if (vmx_ctl_allows_one_setting(val, i)) /* c(iv) */
128 panic("vmx_set_ctlreg: unable to determine "
129 "correct value of ctl bit %d for msr "
130 "0x%0x and true msr 0x%0x", i, ctl_reg,
140 msr_bitmap_initialize(char *bitmap)
143 memset(bitmap, 0xff, PAGE_SIZE);
147 msr_bitmap_change_access(char *bitmap, u_int msr, int access)
151 if (msr <= 0x00001FFF)
153 else if (msr >= 0xC0000000 && msr <= 0xC0001FFF)
154 byte = 1024 + (msr - 0xC0000000) / 8;
160 if (access & MSR_BITMAP_ACCESS_READ)
161 bitmap[byte] &= ~(1 << bit);
163 bitmap[byte] |= 1 << bit;
166 if (access & MSR_BITMAP_ACCESS_WRITE)
167 bitmap[byte] &= ~(1 << bit);
169 bitmap[byte] |= 1 << bit;