xref: /freebsd/sys/amd64/vmm/intel/vmx_msr.c (revision 38f0b757fd84d17d0fc24739a7cda160c4516d81)
1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 
35 #include <machine/cpufunc.h>
36 
37 #include "vmx_msr.h"
38 
39 static boolean_t
40 vmx_ctl_allows_one_setting(uint64_t msr_val, int bitpos)
41 {
42 
43 	if (msr_val & (1UL << (bitpos + 32)))
44 		return (TRUE);
45 	else
46 		return (FALSE);
47 }
48 
49 static boolean_t
50 vmx_ctl_allows_zero_setting(uint64_t msr_val, int bitpos)
51 {
52 
53 	if ((msr_val & (1UL << bitpos)) == 0)
54 		return (TRUE);
55 	else
56 		return (FALSE);
57 }
58 
59 uint32_t
60 vmx_revision(void)
61 {
62 
63 	return (rdmsr(MSR_VMX_BASIC) & 0xffffffff);
64 }
65 
66 /*
67  * Generate a bitmask to be used for the VMCS execution control fields.
68  *
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
73  * VMX Capabilities".
74  *
75  * Returns zero on success and non-zero on error.
76  */
77 int
78 vmx_set_ctlreg(int ctl_reg, int true_ctl_reg, uint32_t ones_mask,
79 	       uint32_t zeros_mask, uint32_t *retval)
80 {
81 	int i;
82 	uint64_t val, trueval;
83 	boolean_t true_ctls_avail, one_allowed, zero_allowed;
84 
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))
87 		return (EINVAL);
88 
89 	if (rdmsr(MSR_VMX_BASIC) & (1UL << 55))
90 		true_ctls_avail = TRUE;
91 	else
92 		true_ctls_avail = FALSE;
93 
94 	val = rdmsr(ctl_reg);
95 	if (true_ctls_avail)
96 		trueval = rdmsr(true_ctl_reg);		/* step c */
97 	else
98 		trueval = val;				/* step a */
99 
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);
103 
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));
107 
108 		if (zero_allowed && !one_allowed) {		/* b(i),c(i) */
109 			if (ones_mask & (1 << i))
110 				return (EINVAL);
111 			*retval &= ~(1 << i);
112 		} else if (one_allowed && !zero_allowed) {	/* b(i),c(i) */
113 			if (zeros_mask & (1 << i))
114 				return (EINVAL);
115 			*retval |= 1 << i;
116 		} else {
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) */
120 				*retval |= 1 << i;
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) */
126 				*retval |= 1 << i;
127 			else {
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,
131 				      true_ctl_reg);
132 			}
133 		}
134 	}
135 
136 	return (0);
137 }
138 
139 void
140 msr_bitmap_initialize(char *bitmap)
141 {
142 
143 	memset(bitmap, 0xff, PAGE_SIZE);
144 }
145 
146 int
147 msr_bitmap_change_access(char *bitmap, u_int msr, int access)
148 {
149 	int byte, bit;
150 
151 	if (msr <= 0x00001FFF)
152 		byte = msr / 8;
153 	else if (msr >= 0xC0000000 && msr <= 0xC0001FFF)
154 		byte = 1024 + (msr - 0xC0000000) / 8;
155 	else
156 		return (EINVAL);
157 
158 	bit = msr & 0x7;
159 
160 	if (access & MSR_BITMAP_ACCESS_READ)
161 		bitmap[byte] &= ~(1 << bit);
162 	else
163 		bitmap[byte] |= 1 << bit;
164 
165 	byte += 2048;
166 	if (access & MSR_BITMAP_ACCESS_WRITE)
167 		bitmap[byte] &= ~(1 << bit);
168 	else
169 		bitmap[byte] |= 1 << bit;
170 
171 	return (0);
172 }
173