2011-05-13 04:54:01 +00:00
|
|
|
/*-
|
2017-11-27 15:03:07 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2011-05-13 04:54:01 +00:00
|
|
|
* Copyright (c) 2011 NetApp, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _VMX_H_
|
|
|
|
#define _VMX_H_
|
|
|
|
|
|
|
|
#include "vmcs.h"
|
|
|
|
|
2013-10-05 21:22:35 +00:00
|
|
|
struct pmap;
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
struct vmxctx {
|
|
|
|
register_t guest_rdi; /* Guest state */
|
|
|
|
register_t guest_rsi;
|
|
|
|
register_t guest_rdx;
|
|
|
|
register_t guest_rcx;
|
|
|
|
register_t guest_r8;
|
|
|
|
register_t guest_r9;
|
|
|
|
register_t guest_rax;
|
|
|
|
register_t guest_rbx;
|
|
|
|
register_t guest_rbp;
|
|
|
|
register_t guest_r10;
|
|
|
|
register_t guest_r11;
|
|
|
|
register_t guest_r12;
|
|
|
|
register_t guest_r13;
|
|
|
|
register_t guest_r14;
|
|
|
|
register_t guest_r15;
|
|
|
|
register_t guest_cr2;
|
Save and restore guest debug registers.
Currently most of the debug registers are not saved and restored
during VM transitions allowing guest and host debug register values to
leak into the opposite context. One result is that hardware
watchpoints do not work reliably within a guest under VT-x.
Due to differences in SVM and VT-x, slightly different approaches are
used.
For VT-x:
- Enable debug register save/restore for VM entry/exit in the VMCS for
DR7 and MSR_DEBUGCTL.
- Explicitly save DR0-3,6 of the guest.
- Explicitly save DR0-3,6-7, MSR_DEBUGCTL, and the trap flag from
%rflags for the host. Note that because DR6 is "software" managed
and not stored in the VMCS a kernel debugger which single steps
through VM entry could corrupt the guest DR6 (since a single step
trap taken after loading the guest DR6 could alter the DR6
register). To avoid this, explicitly disable single-stepping via
the trace flag before loading the guest DR6. A determined debugger
could still defeat this by setting a breakpoint after the guest DR6
was loaded and then single-stepping.
For SVM:
- Enable debug register caching in the VMCB for DR6/DR7.
- Explicitly save DR0-3 of the guest.
- Explicitly save DR0-3,6-7, and MSR_DEBUGCTL for the host. Since SVM
saves the guest DR6 in the VMCB, the race with single-stepping
described for VT-x does not exist.
For both platforms, expose all of the guest DRx values via --get-drX
and --set-drX flags to bhyvectl.
Discussed with: avg, grehan
Tested by: avg (SVM), myself (VT-x)
MFC after: 1 month
Differential Revision: https://reviews.freebsd.org/D13229
2018-01-17 23:11:25 +00:00
|
|
|
register_t guest_dr0;
|
|
|
|
register_t guest_dr1;
|
|
|
|
register_t guest_dr2;
|
|
|
|
register_t guest_dr3;
|
|
|
|
register_t guest_dr6;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
register_t host_r15; /* Host state */
|
|
|
|
register_t host_r14;
|
|
|
|
register_t host_r13;
|
|
|
|
register_t host_r12;
|
|
|
|
register_t host_rbp;
|
|
|
|
register_t host_rsp;
|
|
|
|
register_t host_rbx;
|
Save and restore guest debug registers.
Currently most of the debug registers are not saved and restored
during VM transitions allowing guest and host debug register values to
leak into the opposite context. One result is that hardware
watchpoints do not work reliably within a guest under VT-x.
Due to differences in SVM and VT-x, slightly different approaches are
used.
For VT-x:
- Enable debug register save/restore for VM entry/exit in the VMCS for
DR7 and MSR_DEBUGCTL.
- Explicitly save DR0-3,6 of the guest.
- Explicitly save DR0-3,6-7, MSR_DEBUGCTL, and the trap flag from
%rflags for the host. Note that because DR6 is "software" managed
and not stored in the VMCS a kernel debugger which single steps
through VM entry could corrupt the guest DR6 (since a single step
trap taken after loading the guest DR6 could alter the DR6
register). To avoid this, explicitly disable single-stepping via
the trace flag before loading the guest DR6. A determined debugger
could still defeat this by setting a breakpoint after the guest DR6
was loaded and then single-stepping.
For SVM:
- Enable debug register caching in the VMCB for DR6/DR7.
- Explicitly save DR0-3 of the guest.
- Explicitly save DR0-3,6-7, and MSR_DEBUGCTL for the host. Since SVM
saves the guest DR6 in the VMCB, the race with single-stepping
described for VT-x does not exist.
For both platforms, expose all of the guest DRx values via --get-drX
and --set-drX flags to bhyvectl.
Discussed with: avg, grehan
Tested by: avg (SVM), myself (VT-x)
MFC after: 1 month
Differential Revision: https://reviews.freebsd.org/D13229
2018-01-17 23:11:25 +00:00
|
|
|
register_t host_dr0;
|
|
|
|
register_t host_dr1;
|
|
|
|
register_t host_dr2;
|
|
|
|
register_t host_dr3;
|
|
|
|
register_t host_dr6;
|
|
|
|
register_t host_dr7;
|
|
|
|
uint64_t host_debugctl;
|
|
|
|
int host_tf;
|
2013-10-05 21:22:35 +00:00
|
|
|
|
2014-02-04 02:45:08 +00:00
|
|
|
int inst_fail_status;
|
2013-10-05 21:22:35 +00:00
|
|
|
|
|
|
|
/*
|
2014-05-18 03:50:17 +00:00
|
|
|
* The pmap needs to be deactivated in vmx_enter_guest()
|
2014-02-04 02:45:08 +00:00
|
|
|
* so keep a copy of the 'pmap' in each vmxctx.
|
2013-10-05 21:22:35 +00:00
|
|
|
*/
|
|
|
|
struct pmap *pmap;
|
2011-05-13 04:54:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct vmxcap {
|
|
|
|
int set;
|
|
|
|
uint32_t proc_ctls;
|
2013-10-16 18:20:27 +00:00
|
|
|
uint32_t proc_ctls2;
|
2019-12-13 19:21:58 +00:00
|
|
|
uint32_t exc_bitmap;
|
2011-05-13 04:54:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct vmxstate {
|
2015-01-06 19:04:02 +00:00
|
|
|
uint64_t nextrip; /* next instruction to be executed by guest */
|
2011-05-13 04:54:01 +00:00
|
|
|
int lastcpu; /* host cpu that this 'vcpu' last ran on */
|
|
|
|
uint16_t vpid;
|
|
|
|
};
|
|
|
|
|
2013-12-25 06:46:31 +00:00
|
|
|
struct apic_page {
|
|
|
|
uint32_t reg[PAGE_SIZE / 4];
|
|
|
|
};
|
|
|
|
CTASSERT(sizeof(struct apic_page) == PAGE_SIZE);
|
|
|
|
|
2014-01-11 04:22:00 +00:00
|
|
|
/* Posted Interrupt Descriptor (described in section 29.6 of the Intel SDM) */
|
|
|
|
struct pir_desc {
|
|
|
|
uint64_t pir[4];
|
|
|
|
uint64_t pending;
|
|
|
|
uint64_t unused[3];
|
|
|
|
} __aligned(64);
|
|
|
|
CTASSERT(sizeof(struct pir_desc) == 64);
|
|
|
|
|
2014-09-20 02:35:21 +00:00
|
|
|
/* Index into the 'guest_msrs[]' array */
|
|
|
|
enum {
|
|
|
|
IDX_MSR_LSTAR,
|
|
|
|
IDX_MSR_CSTAR,
|
|
|
|
IDX_MSR_STAR,
|
|
|
|
IDX_MSR_SF_MASK,
|
|
|
|
IDX_MSR_KGSBASE,
|
2015-02-24 05:35:15 +00:00
|
|
|
IDX_MSR_PAT,
|
2020-08-18 07:23:47 +00:00
|
|
|
IDX_MSR_TSC_AUX,
|
2014-09-20 02:35:21 +00:00
|
|
|
GUEST_MSR_NUM /* must be the last enumeration */
|
|
|
|
};
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
/* virtual machine softc */
|
|
|
|
struct vmx {
|
|
|
|
struct vmcs vmcs[VM_MAXCPU]; /* one vmcs per virtual cpu */
|
2013-12-25 06:46:31 +00:00
|
|
|
struct apic_page apic_page[VM_MAXCPU]; /* one apic page per vcpu */
|
2011-05-13 04:54:01 +00:00
|
|
|
char msr_bitmap[PAGE_SIZE];
|
2014-01-11 04:22:00 +00:00
|
|
|
struct pir_desc pir_desc[VM_MAXCPU];
|
2014-09-20 02:35:21 +00:00
|
|
|
uint64_t guest_msrs[VM_MAXCPU][GUEST_MSR_NUM];
|
2011-05-13 04:54:01 +00:00
|
|
|
struct vmxctx ctx[VM_MAXCPU];
|
|
|
|
struct vmxcap cap[VM_MAXCPU];
|
|
|
|
struct vmxstate state[VM_MAXCPU];
|
2013-10-05 21:22:35 +00:00
|
|
|
uint64_t eptp;
|
2011-05-13 04:54:01 +00:00
|
|
|
struct vm *vm;
|
2014-02-04 02:45:08 +00:00
|
|
|
long eptgen[MAXCPU]; /* cached pmap->pm_eptgen */
|
2011-05-13 04:54:01 +00:00
|
|
|
};
|
|
|
|
CTASSERT((offsetof(struct vmx, vmcs) & PAGE_MASK) == 0);
|
|
|
|
CTASSERT((offsetof(struct vmx, msr_bitmap) & PAGE_MASK) == 0);
|
2014-01-11 04:22:00 +00:00
|
|
|
CTASSERT((offsetof(struct vmx, pir_desc[0]) & 63) == 0);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2014-01-01 21:17:08 +00:00
|
|
|
#define VMX_GUEST_VMEXIT 0
|
|
|
|
#define VMX_VMRESUME_ERROR 1
|
|
|
|
#define VMX_VMLAUNCH_ERROR 2
|
2014-02-04 02:45:08 +00:00
|
|
|
int vmx_enter_guest(struct vmxctx *ctx, struct vmx *vmx, int launched);
|
2014-01-11 03:14:05 +00:00
|
|
|
void vmx_call_isr(uintptr_t entry);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
u_long vmx_fix_cr0(u_long cr0);
|
|
|
|
u_long vmx_fix_cr4(u_long cr4);
|
|
|
|
|
2015-06-09 00:14:47 +00:00
|
|
|
int vmx_set_tsc_offset(struct vmx *vmx, int vcpu, uint64_t offset);
|
|
|
|
|
2014-05-18 03:50:17 +00:00
|
|
|
extern char vmx_exit_guest[];
|
2018-02-12 14:45:27 +00:00
|
|
|
extern char vmx_exit_guest_flush_rsb[];
|
2014-05-18 03:50:17 +00:00
|
|
|
|
2020-08-18 07:23:47 +00:00
|
|
|
static inline bool
|
|
|
|
vmx_have_msr_tsc_aux(struct vmx *vmx)
|
|
|
|
{
|
|
|
|
int rdpid_rdtscp_bits = ((1 << VM_CAP_RDPID) | (1 << VM_CAP_RDTSCP));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since the values of these bits are uniform across all vCPUs
|
2020-11-28 01:16:59 +00:00
|
|
|
* (see discussion in vmx_modinit() and initialization of these bits
|
|
|
|
* in vmx_init()), just always use vCPU-zero's capability set and
|
2020-08-18 07:23:47 +00:00
|
|
|
* remove the need to require a vcpuid argument.
|
|
|
|
*/
|
|
|
|
return ((vmx->cap[0].set & rdpid_rdtscp_bits) != 0);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
#endif
|