]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/vmm/vmm_msr.c
IFC @ r242940
[FreeBSD/FreeBSD.git] / sys / amd64 / vmm / vmm_msr.c
1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/smp.h>
35
36 #include <machine/specialreg.h>
37
38 #include <machine/vmm.h>
39 #include "vmm_lapic.h"
40 #include "vmm_msr.h"
41
42 #define VMM_MSR_F_EMULATE       0x01
43 #define VMM_MSR_F_READONLY      0x02
44 #define VMM_MSR_F_INVALID       0x04
45
46 struct vmm_msr {
47         int             num;
48         int             flags;
49         uint64_t        hostval;
50 };
51
52 static struct vmm_msr vmm_msr[] = {
53         { MSR_LSTAR,    0 },
54         { MSR_CSTAR,    0 },
55         { MSR_STAR,     0 },
56         { MSR_SF_MASK,  0 },
57         { MSR_PAT,      VMM_MSR_F_EMULATE | VMM_MSR_F_INVALID },
58         { MSR_BIOS_SIGN,VMM_MSR_F_EMULATE },
59         { MSR_MCG_CAP,  VMM_MSR_F_EMULATE | VMM_MSR_F_READONLY },
60 };
61
62 #define vmm_msr_num     (sizeof(vmm_msr) / sizeof(vmm_msr[0]))
63 CTASSERT(VMM_MSR_NUM >= vmm_msr_num);
64
65 #define readonly_msr(idx)       \
66         ((vmm_msr[(idx)].flags & VMM_MSR_F_READONLY) != 0)
67
68 #define emulated_msr(idx)       \
69         ((vmm_msr[(idx)].flags & VMM_MSR_F_EMULATE) != 0)
70
71 #define invalid_msr(idx)        \
72         ((vmm_msr[(idx)].flags & VMM_MSR_F_INVALID) != 0)
73
74 void
75 vmm_msr_init(void)
76 {
77         int i;
78
79         for (i = 0; i < vmm_msr_num; i++) {
80                 if (emulated_msr(i))
81                         continue;
82                 /*
83                  * XXX this assumes that the value of the host msr does not
84                  * change after we have cached it.
85                  */
86                 vmm_msr[i].hostval = rdmsr(vmm_msr[i].num);
87         }
88 }
89
90 void
91 guest_msrs_init(struct vm *vm, int cpu)
92 {
93         int i;
94         uint64_t *guest_msrs;
95
96         guest_msrs = vm_guest_msrs(vm, cpu);
97         
98         for (i = 0; i < vmm_msr_num; i++) {
99                 switch (vmm_msr[i].num) {
100                 case MSR_LSTAR:
101                 case MSR_CSTAR:
102                 case MSR_STAR:
103                 case MSR_SF_MASK:
104                 case MSR_BIOS_SIGN:
105                 case MSR_MCG_CAP:
106                         guest_msrs[i] = 0;
107                         break;
108                 case MSR_PAT:
109                         guest_msrs[i] = PAT_VALUE(0, PAT_WRITE_BACK)      |
110                                 PAT_VALUE(1, PAT_WRITE_THROUGH)   |
111                                 PAT_VALUE(2, PAT_UNCACHED)        |
112                                 PAT_VALUE(3, PAT_UNCACHEABLE)     |
113                                 PAT_VALUE(4, PAT_WRITE_BACK)      |
114                                 PAT_VALUE(5, PAT_WRITE_THROUGH)   |
115                                 PAT_VALUE(6, PAT_UNCACHED)        |
116                                 PAT_VALUE(7, PAT_UNCACHEABLE);
117                         break;
118                 default:
119                         panic("guest_msrs_init: missing initialization for msr "
120                               "0x%0x", vmm_msr[i].num);
121                 }
122         }
123 }
124
125 static int
126 msr_num_to_idx(u_int num)
127 {
128         int i;
129
130         for (i = 0; i < vmm_msr_num; i++)
131                 if (vmm_msr[i].num == num)
132                         return (i);
133
134         return (-1);
135 }
136
137 int
138 emulate_wrmsr(struct vm *vm, int cpu, u_int num, uint64_t val)
139 {
140         int handled, idx;
141         uint64_t *guest_msrs;
142
143         handled = 0;
144
145         if (lapic_msr(num))
146                 return (lapic_wrmsr(vm, cpu, num, val));
147
148         idx = msr_num_to_idx(num);
149         if (idx < 0)
150                 goto done;
151
152         if (invalid_msr(idx))
153                 goto done;
154
155         if (!readonly_msr(idx)) {
156                 guest_msrs = vm_guest_msrs(vm, cpu);
157
158                 /* Stash the value */
159                 guest_msrs[idx] = val;
160
161                 /* Update processor state for non-emulated MSRs */
162                 if (!emulated_msr(idx))
163                         wrmsr(vmm_msr[idx].num, val);
164         }
165
166         handled = 1;
167 done:
168         return (handled);
169 }
170
171 int
172 emulate_rdmsr(struct vm *vm, int cpu, u_int num)
173 {
174         int error, handled, idx;
175         uint32_t eax, edx;
176         uint64_t result, *guest_msrs;
177
178         handled = 0;
179
180         if (lapic_msr(num)) {
181                 handled = lapic_rdmsr(vm, cpu, num, &result);
182                 goto done;
183         }
184
185         idx = msr_num_to_idx(num);
186         if (idx < 0)
187                 goto done;
188
189         if (invalid_msr(idx))
190                 goto done;
191
192         guest_msrs = vm_guest_msrs(vm, cpu);
193         result = guest_msrs[idx];
194
195         /*
196          * If this is not an emulated msr register make sure that the processor
197          * state matches our cached state.
198          */
199         if (!emulated_msr(idx) && (rdmsr(num) != result)) {
200                 panic("emulate_rdmsr: msr 0x%0x has inconsistent cached "
201                       "(0x%016lx) and actual (0x%016lx) values", num,
202                       result, rdmsr(num));
203         }
204
205         handled = 1;
206
207 done:
208         if (handled) {
209                 eax = result;
210                 edx = result >> 32;
211                 error = vm_set_register(vm, cpu, VM_REG_GUEST_RAX, eax);
212                 if (error)
213                         panic("vm_set_register(rax) error %d", error);
214                 error = vm_set_register(vm, cpu, VM_REG_GUEST_RDX, edx);
215                 if (error)
216                         panic("vm_set_register(rdx) error %d", error);
217         }
218         return (handled);
219 }
220
221 void
222 restore_guest_msrs(struct vm *vm, int cpu)
223 {
224         int i;
225         uint64_t *guest_msrs;
226
227         guest_msrs = vm_guest_msrs(vm, cpu);
228
229         for (i = 0; i < vmm_msr_num; i++) {
230                 if (emulated_msr(i))
231                         continue;
232                 else
233                         wrmsr(vmm_msr[i].num, guest_msrs[i]);
234         }
235 }
236
237 void
238 restore_host_msrs(struct vm *vm, int cpu)
239 {
240         int i;
241
242         for (i = 0; i < vmm_msr_num; i++) {
243                 if (emulated_msr(i))
244                         continue;
245                 else
246                         wrmsr(vmm_msr[i].num, vmm_msr[i].hostval);
247         }
248 }
249
250 /*
251  * Must be called by the CPU-specific code before any guests are
252  * created
253  */
254 void
255 guest_msr_valid(int msr)
256 {
257         int i;
258
259         for (i = 0; i < vmm_msr_num; i++) {
260                 if (vmm_msr[i].num == msr && invalid_msr(i)) {
261                         vmm_msr[i].flags &= ~VMM_MSR_F_INVALID;
262                 }
263         }
264 }