2 * Copyright (c) 2014, Neel Natu (neel@freebsd.org)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/errno.h>
32 #include <sys/systm.h>
34 #include <machine/cpufunc.h>
35 #include <machine/specialreg.h>
36 #include <machine/vmm.h>
40 #include "svm_softc.h"
44 #define MSR_AMDK8_IPM 0xc0010055
52 HOST_MSR_NUM /* must be the last enumeration */
55 static uint64_t host_msrs[HOST_MSR_NUM];
61 * It is safe to cache the values of the following MSRs because they
62 * don't change based on curcpu, curproc or curthread.
64 host_msrs[IDX_MSR_LSTAR] = rdmsr(MSR_LSTAR);
65 host_msrs[IDX_MSR_CSTAR] = rdmsr(MSR_CSTAR);
66 host_msrs[IDX_MSR_STAR] = rdmsr(MSR_STAR);
67 host_msrs[IDX_MSR_SF_MASK] = rdmsr(MSR_SF_MASK);
71 svm_msr_guest_init(struct svm_softc *sc, int vcpu)
74 * All the MSRs accessible to the guest are either saved/restored by
75 * hardware on every #VMEXIT/VMRUN (e.g., G_PAT) or are saved/restored
76 * by VMSAVE/VMLOAD (e.g., MSR_GSBASE).
78 * There are no guest MSRs that are saved/restored "by hand" so nothing
85 svm_msr_guest_enter(struct svm_softc *sc, int vcpu)
88 * Save host MSRs (if any) and restore guest MSRs (if any).
93 svm_msr_guest_exit(struct svm_softc *sc, int vcpu)
96 * Save guest MSRs (if any) and restore host MSRs.
98 wrmsr(MSR_LSTAR, host_msrs[IDX_MSR_LSTAR]);
99 wrmsr(MSR_CSTAR, host_msrs[IDX_MSR_CSTAR]);
100 wrmsr(MSR_STAR, host_msrs[IDX_MSR_STAR]);
101 wrmsr(MSR_SF_MASK, host_msrs[IDX_MSR_SF_MASK]);
103 /* MSR_KGSBASE will be restored on the way back to userspace */
107 svm_rdmsr(struct svm_softc *sc, int vcpu, u_int num, uint64_t *result,
118 case MSR_MTRRdefType:
119 case MSR_MTRR4kBase ... MSR_MTRR4kBase + 8:
120 case MSR_MTRR16kBase ... MSR_MTRR16kBase + 1:
121 case MSR_MTRR64kBase:
137 svm_wrmsr(struct svm_softc *sc, int vcpu, u_int num, uint64_t val, bool *retu)
144 break; /* ignore writes */
146 vm_inject_gp(sc->vm, vcpu);
148 case MSR_MTRRdefType:
149 case MSR_MTRR4kBase ... MSR_MTRR4kBase + 8:
150 case MSR_MTRR16kBase ... MSR_MTRR16kBase + 1:
151 case MSR_MTRR64kBase:
153 break; /* Ignore writes */
156 * Ignore writes to the "Interrupt Pending Message" MSR.