]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/xmsr.c
kern: cpuset: allow jails to modify child jails' roots
[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, int vcpu, uint32_t num, uint64_t val)
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 vmctx *ctx, int vcpu, 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                         *val = 0;
119                         break;
120                 case MSR_RAPL_POWER_UNIT:
121                         /*
122                          * Use the default value documented in section
123                          * "RAPL Interfaces" in Intel SDM vol3.
124                          */
125                         *val = 0x000a1003;
126                         break;
127                 default:
128                         error = -1;
129                         break;
130                 }
131         } else if (cpu_vendor_amd || cpu_vendor_hygon) {
132                 switch (num) {
133                 case MSR_BIOS_SIGN:
134                         *val = 0;
135                         break;
136                 case MSR_HWCR:
137                         /*
138                          * Bios and Kernel Developer's Guides for AMD Families
139                          * 12H, 14H, 15H and 16H.
140                          */
141                         *val = 0x01000010;      /* Reset value */
142                         *val |= 1 << 9;         /* MONITOR/MWAIT disable */
143                         break;
144
145                 case MSR_NB_CFG1:
146                 case MSR_LS_CFG:
147                 case MSR_IC_CFG:
148                         /*
149                          * The reset value is processor family dependent so
150                          * just return 0.
151                          */
152                         *val = 0;
153                         break;
154
155                 case MSR_PERFEVSEL0:
156                 case MSR_PERFEVSEL1:
157                 case MSR_PERFEVSEL2:
158                 case MSR_PERFEVSEL3:
159                         /*
160                          * PerfEvtSel MSRs are not properly virtualized so just
161                          * return zero.
162                          */
163                         *val = 0;
164                         break;
165
166                 case MSR_K7_PERFCTR0:
167                 case MSR_K7_PERFCTR1:
168                 case MSR_K7_PERFCTR2:
169                 case MSR_K7_PERFCTR3:
170                         /*
171                          * PerfCtr MSRs are not properly virtualized so just
172                          * return zero.
173                          */
174                         *val = 0;
175                         break;
176
177                 case MSR_SMM_ADDR:
178                 case MSR_SMM_MASK:
179                         /*
180                          * Return the reset value defined in the AMD Bios and
181                          * Kernel Developer's Guide.
182                          */
183                         *val = 0;
184                         break;
185
186                 case MSR_P_STATE_LIMIT:
187                 case MSR_P_STATE_CONTROL:
188                 case MSR_P_STATE_STATUS:
189                 case MSR_P_STATE_CONFIG(0):     /* P0 configuration */
190                         *val = 0;
191                         break;
192
193                 /*
194                  * OpenBSD guests test bit 0 of this MSR to detect if the
195                  * workaround for erratum 721 is already applied.
196                  * https://support.amd.com/TechDocs/41322_10h_Rev_Gd.pdf
197                  */
198                 case 0xC0011029:
199                         *val = 1;
200                         break;
201
202                 default:
203                         error = -1;
204                         break;
205                 }
206         } else {
207                 error = -1;
208         }
209         return (error);
210 }
211
212 int
213 init_msr(void)
214 {
215         int error;
216         u_int regs[4];
217         char cpu_vendor[13];
218
219         do_cpuid(0, regs);
220         ((u_int *)&cpu_vendor)[0] = regs[1];
221         ((u_int *)&cpu_vendor)[1] = regs[3];
222         ((u_int *)&cpu_vendor)[2] = regs[2];
223         cpu_vendor[12] = '\0';
224
225         error = 0;
226         if (strcmp(cpu_vendor, "AuthenticAMD") == 0) {
227                 cpu_vendor_amd = 1;
228         } else if (strcmp(cpu_vendor, "HygonGenuine") == 0) {
229                 cpu_vendor_hygon = 1;
230         } else if (strcmp(cpu_vendor, "GenuineIntel") == 0) {
231                 cpu_vendor_intel = 1;
232         } else {
233                 EPRINTLN("Unknown cpu vendor \"%s\"", cpu_vendor);
234                 error = -1;
235         }
236         return (error);
237 }