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