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