]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/xmsr.c
contrib/tzdata: import tzdata 2023b
[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 vmctx *ctx __unused, int vcpu __unused, uint32_t num,
53     uint64_t val __unused)
54 {
55
56         if (cpu_vendor_intel) {
57                 switch (num) {
58                 case 0xd04:             /* Sandy Bridge uncore PMCs */
59                 case 0xc24:
60                         return (0);
61                 case MSR_BIOS_UPDT_TRIG:
62                         return (0);
63                 case MSR_BIOS_SIGN:
64                         return (0);
65                 default:
66                         break;
67                 }
68         } else if (cpu_vendor_amd || cpu_vendor_hygon) {
69                 switch (num) {
70                 case MSR_HWCR:
71                         /*
72                          * Ignore writes to hardware configuration MSR.
73                          */
74                         return (0);
75
76                 case MSR_NB_CFG1:
77                 case MSR_LS_CFG:
78                 case MSR_IC_CFG:
79                         return (0);     /* Ignore writes */
80
81                 case MSR_PERFEVSEL0:
82                 case MSR_PERFEVSEL1:
83                 case MSR_PERFEVSEL2:
84                 case MSR_PERFEVSEL3:
85                         /* Ignore writes to the PerfEvtSel MSRs */
86                         return (0);
87
88                 case MSR_K7_PERFCTR0:
89                 case MSR_K7_PERFCTR1:
90                 case MSR_K7_PERFCTR2:
91                 case MSR_K7_PERFCTR3:
92                         /* Ignore writes to the PerfCtr MSRs */
93                         return (0);
94
95                 case MSR_P_STATE_CONTROL:
96                         /* Ignore write to change the P-state */
97                         return (0);
98
99                 default:
100                         break;
101                 }
102         }
103         return (-1);
104 }
105
106 int
107 emulate_rdmsr(struct vmctx *ctx __unused, int vcpu __unused, uint32_t num,
108     uint64_t *val)
109 {
110         int error = 0;
111
112         if (cpu_vendor_intel) {
113                 switch (num) {
114                 case MSR_BIOS_SIGN:
115                 case MSR_IA32_PLATFORM_ID:
116                 case MSR_PKG_ENERGY_STATUS:
117                 case MSR_PP0_ENERGY_STATUS:
118                 case MSR_PP1_ENERGY_STATUS:
119                 case MSR_DRAM_ENERGY_STATUS:
120                 case MSR_MISC_FEATURE_ENABLES:
121                         *val = 0;
122                         break;
123                 case MSR_RAPL_POWER_UNIT:
124                         /*
125                          * Use the default value documented in section
126                          * "RAPL Interfaces" in Intel SDM vol3.
127                          */
128                         *val = 0x000a1003;
129                         break;
130                 case MSR_IA32_FEATURE_CONTROL:
131                         /*
132                          * Windows guests check this MSR.
133                          * Set the lock bit to avoid writes
134                          * to this MSR.
135                          */
136                         *val = IA32_FEATURE_CONTROL_LOCK;
137                         break;
138                 default:
139                         error = -1;
140                         break;
141                 }
142         } else if (cpu_vendor_amd || cpu_vendor_hygon) {
143                 switch (num) {
144                 case MSR_BIOS_SIGN:
145                         *val = 0;
146                         break;
147                 case MSR_HWCR:
148                         /*
149                          * Bios and Kernel Developer's Guides for AMD Families
150                          * 12H, 14H, 15H and 16H.
151                          */
152                         *val = 0x01000010;      /* Reset value */
153                         *val |= 1 << 9;         /* MONITOR/MWAIT disable */
154                         break;
155
156                 case MSR_NB_CFG1:
157                 case MSR_LS_CFG:
158                 case MSR_IC_CFG:
159                         /*
160                          * The reset value is processor family dependent so
161                          * just return 0.
162                          */
163                         *val = 0;
164                         break;
165
166                 case MSR_PERFEVSEL0:
167                 case MSR_PERFEVSEL1:
168                 case MSR_PERFEVSEL2:
169                 case MSR_PERFEVSEL3:
170                         /*
171                          * PerfEvtSel MSRs are not properly virtualized so just
172                          * return zero.
173                          */
174                         *val = 0;
175                         break;
176
177                 case MSR_K7_PERFCTR0:
178                 case MSR_K7_PERFCTR1:
179                 case MSR_K7_PERFCTR2:
180                 case MSR_K7_PERFCTR3:
181                         /*
182                          * PerfCtr MSRs are not properly virtualized so just
183                          * return zero.
184                          */
185                         *val = 0;
186                         break;
187
188                 case MSR_SMM_ADDR:
189                 case MSR_SMM_MASK:
190                         /*
191                          * Return the reset value defined in the AMD Bios and
192                          * Kernel Developer's Guide.
193                          */
194                         *val = 0;
195                         break;
196
197                 case MSR_P_STATE_LIMIT:
198                 case MSR_P_STATE_CONTROL:
199                 case MSR_P_STATE_STATUS:
200                 case MSR_P_STATE_CONFIG(0):     /* P0 configuration */
201                         *val = 0;
202                         break;
203
204                 /*
205                  * OpenBSD guests test bit 0 of this MSR to detect if the
206                  * workaround for erratum 721 is already applied.
207                  * https://support.amd.com/TechDocs/41322_10h_Rev_Gd.pdf
208                  */
209                 case 0xC0011029:
210                         *val = 1;
211                         break;
212
213                 default:
214                         error = -1;
215                         break;
216                 }
217         } else {
218                 error = -1;
219         }
220         return (error);
221 }
222
223 int
224 init_msr(void)
225 {
226         int error;
227         u_int regs[4];
228         char cpu_vendor[13];
229
230         do_cpuid(0, regs);
231         ((u_int *)&cpu_vendor)[0] = regs[1];
232         ((u_int *)&cpu_vendor)[1] = regs[3];
233         ((u_int *)&cpu_vendor)[2] = regs[2];
234         cpu_vendor[12] = '\0';
235
236         error = 0;
237         if (strcmp(cpu_vendor, "AuthenticAMD") == 0) {
238                 cpu_vendor_amd = 1;
239         } else if (strcmp(cpu_vendor, "HygonGenuine") == 0) {
240                 cpu_vendor_hygon = 1;
241         } else if (strcmp(cpu_vendor, "GenuineIntel") == 0) {
242                 cpu_vendor_intel = 1;
243         } else {
244                 EPRINTLN("Unknown cpu vendor \"%s\"", cpu_vendor);
245                 error = -1;
246         }
247         return (error);
248 }