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/types.h>
31 #include <sys/errno.h>
33 #include <machine/cpufunc.h>
34 #include <machine/specialreg.h>
39 #define MSR_AMDK8_IPM 0xc0010055
47 HOST_MSR_NUM /* must be the last enumeration */
50 static uint64_t host_msrs[HOST_MSR_NUM];
56 * It is safe to cache the values of the following MSRs because they
57 * don't change based on curcpu, curproc or curthread.
59 host_msrs[IDX_MSR_LSTAR] = rdmsr(MSR_LSTAR);
60 host_msrs[IDX_MSR_CSTAR] = rdmsr(MSR_CSTAR);
61 host_msrs[IDX_MSR_STAR] = rdmsr(MSR_STAR);
62 host_msrs[IDX_MSR_SF_MASK] = rdmsr(MSR_SF_MASK);
66 svm_msr_guest_init(struct svm_softc *sc, int vcpu)
69 * All the MSRs accessible to the guest are either saved/restored by
70 * hardware on every #VMEXIT/VMRUN (e.g., G_PAT) or are saved/restored
71 * by VMSAVE/VMLOAD (e.g., MSR_GSBASE).
73 * There are no guest MSRs that are saved/restored "by hand" so nothing
80 svm_msr_guest_enter(struct svm_softc *sc, int vcpu)
83 * Save host MSRs (if any) and restore guest MSRs (if any).
88 svm_msr_guest_exit(struct svm_softc *sc, int vcpu)
91 * Save guest MSRs (if any) and restore host MSRs.
93 wrmsr(MSR_LSTAR, host_msrs[IDX_MSR_LSTAR]);
94 wrmsr(MSR_CSTAR, host_msrs[IDX_MSR_CSTAR]);
95 wrmsr(MSR_STAR, host_msrs[IDX_MSR_STAR]);
96 wrmsr(MSR_SF_MASK, host_msrs[IDX_MSR_SF_MASK]);
98 /* MSR_KGSBASE will be restored on the way back to userspace */
102 svm_rdmsr(struct svm_softc *sc, int vcpu, u_int num, uint64_t *result,
120 svm_wrmsr(struct svm_softc *sc, int vcpu, u_int num, uint64_t val, bool *retu)
127 * Ignore writes to the "Interrupt Pending Message" MSR.