]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/xmsr.c
zfs: merge openzfs/zfs@e61076683
[FreeBSD/FreeBSD.git] / usr.sbin / bhyve / xmsr.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/types.h>
35
36 #include <machine/cpufunc.h>
37 #include <machine/vmm.h>
38 #include <machine/specialreg.h>
39
40 #include <vmmapi.h>
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "debug.h"
47 #include "xmsr.h"
48
49 static int cpu_vendor_intel, cpu_vendor_amd, cpu_vendor_hygon;
50
51 int
52 emulate_wrmsr(struct vcpu *vcpu __unused, uint32_t num, uint64_t val __unused)
53 {
54
55         if (cpu_vendor_intel) {
56                 switch (num) {
57                 case 0xd04:             /* Sandy Bridge uncore PMCs */
58                 case 0xc24:
59                         return (0);
60                 case MSR_BIOS_UPDT_TRIG:
61                         return (0);
62                 case MSR_BIOS_SIGN:
63                         return (0);
64                 default:
65                         break;
66                 }
67         } else if (cpu_vendor_amd || cpu_vendor_hygon) {
68                 switch (num) {
69                 case MSR_HWCR:
70                         /*
71                          * Ignore writes to hardware configuration MSR.
72                          */
73                         return (0);
74
75                 case MSR_NB_CFG1:
76                 case MSR_LS_CFG:
77                 case MSR_IC_CFG:
78                         return (0);     /* Ignore writes */
79
80                 case MSR_PERFEVSEL0:
81                 case MSR_PERFEVSEL1:
82                 case MSR_PERFEVSEL2:
83                 case MSR_PERFEVSEL3:
84                         /* Ignore writes to the PerfEvtSel MSRs */
85                         return (0);
86
87                 case MSR_K7_PERFCTR0:
88                 case MSR_K7_PERFCTR1:
89                 case MSR_K7_PERFCTR2:
90                 case MSR_K7_PERFCTR3:
91                         /* Ignore writes to the PerfCtr MSRs */
92                         return (0);
93
94                 case MSR_P_STATE_CONTROL:
95                         /* Ignore write to change the P-state */
96                         return (0);
97
98                 default:
99                         break;
100                 }
101         }
102         return (-1);
103 }
104
105 int
106 emulate_rdmsr(struct vcpu *vcpu __unused, uint32_t num, uint64_t *val)
107 {
108         int error = 0;
109
110         if (cpu_vendor_intel) {
111                 switch (num) {
112                 case MSR_BIOS_SIGN:
113                 case MSR_IA32_PLATFORM_ID:
114                 case MSR_PKG_ENERGY_STATUS:
115                 case MSR_PP0_ENERGY_STATUS:
116                 case MSR_PP1_ENERGY_STATUS:
117                 case MSR_DRAM_ENERGY_STATUS:
118                 case MSR_MISC_FEATURE_ENABLES:
119                         *val = 0;
120                         break;
121                 case MSR_RAPL_POWER_UNIT:
122                         /*
123                          * Use the default value documented in section
124                          * "RAPL Interfaces" in Intel SDM vol3.
125                          */
126                         *val = 0x000a1003;
127                         break;
128                 case MSR_IA32_FEATURE_CONTROL:
129                         /*
130                          * Windows guests check this MSR.
131                          * Set the lock bit to avoid writes
132                          * to this MSR.
133                          */
134                         *val = IA32_FEATURE_CONTROL_LOCK;
135                         break;
136                 default:
137                         error = -1;
138                         break;
139                 }
140         } else if (cpu_vendor_amd || cpu_vendor_hygon) {
141                 switch (num) {
142                 case MSR_BIOS_SIGN:
143                         *val = 0;
144                         break;
145                 case MSR_HWCR:
146                         /*
147                          * Bios and Kernel Developer's Guides for AMD Families
148                          * 12H, 14H, 15H and 16H.
149                          */
150                         *val = 0x01000010;      /* Reset value */
151                         *val |= 1 << 9;         /* MONITOR/MWAIT disable */
152                         break;
153
154                 case MSR_NB_CFG1:
155                 case MSR_LS_CFG:
156                 case MSR_IC_CFG:
157                         /*
158                          * The reset value is processor family dependent so
159                          * just return 0.
160                          */
161                         *val = 0;
162                         break;
163
164                 case MSR_PERFEVSEL0:
165                 case MSR_PERFEVSEL1:
166                 case MSR_PERFEVSEL2:
167                 case MSR_PERFEVSEL3:
168                         /*
169                          * PerfEvtSel MSRs are not properly virtualized so just
170                          * return zero.
171                          */
172                         *val = 0;
173                         break;
174
175                 case MSR_K7_PERFCTR0:
176                 case MSR_K7_PERFCTR1:
177                 case MSR_K7_PERFCTR2:
178                 case MSR_K7_PERFCTR3:
179                         /*
180                          * PerfCtr MSRs are not properly virtualized so just
181                          * return zero.
182                          */
183                         *val = 0;
184                         break;
185
186                 case MSR_SMM_ADDR:
187                 case MSR_SMM_MASK:
188                         /*
189                          * Return the reset value defined in the AMD Bios and
190                          * Kernel Developer's Guide.
191                          */
192                         *val = 0;
193                         break;
194
195                 case MSR_P_STATE_LIMIT:
196                 case MSR_P_STATE_CONTROL:
197                 case MSR_P_STATE_STATUS:
198                 case MSR_P_STATE_CONFIG(0):     /* P0 configuration */
199                         *val = 0;
200                         break;
201
202                 /*
203                  * OpenBSD guests test bit 0 of this MSR to detect if the
204                  * workaround for erratum 721 is already applied.
205                  * https://support.amd.com/TechDocs/41322_10h_Rev_Gd.pdf
206                  */
207                 case 0xC0011029:
208                         *val = 1;
209                         break;
210
211                 default:
212                         error = -1;
213                         break;
214                 }
215         } else {
216                 error = -1;
217         }
218         return (error);
219 }
220
221 int
222 init_msr(void)
223 {
224         int error;
225         u_int regs[4];
226         char cpu_vendor[13];
227
228         do_cpuid(0, regs);
229         ((u_int *)&cpu_vendor)[0] = regs[1];
230         ((u_int *)&cpu_vendor)[1] = regs[3];
231         ((u_int *)&cpu_vendor)[2] = regs[2];
232         cpu_vendor[12] = '\0';
233
234         error = 0;
235         if (strcmp(cpu_vendor, "AuthenticAMD") == 0) {
236                 cpu_vendor_amd = 1;
237         } else if (strcmp(cpu_vendor, "HygonGenuine") == 0) {
238                 cpu_vendor_hygon = 1;
239         } else if (strcmp(cpu_vendor, "GenuineIntel") == 0) {
240                 cpu_vendor_intel = 1;
241         } else {
242                 EPRINTLN("Unknown cpu vendor \"%s\"", cpu_vendor);
243                 error = -1;
244         }
245         return (error);
246 }