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.
|
|
|
|
*
|
2013-01-20 03:42:49 +00:00
|
|
|
* $FreeBSD$
|
2011-05-13 04:54:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _VMM_H_
|
|
|
|
#define _VMM_H_
|
|
|
|
|
2018-04-13 17:23:05 +00:00
|
|
|
#include <sys/sdt.h>
|
2014-07-24 01:38:11 +00:00
|
|
|
#include <x86/segments.h>
|
|
|
|
|
Initial support for bhyve save and restore.
Save and restore (also known as suspend and resume) permits a snapshot
to be taken of a guest's state that can later be resumed. In the
current implementation, bhyve(8) creates a UNIX domain socket that is
used by bhyvectl(8) to send a request to save a snapshot (and
optionally exit after the snapshot has been taken). A snapshot
currently consists of two files: the first holds a copy of guest RAM,
and the second file holds other guest state such as vCPU register
values and device model state.
To resume a guest, bhyve(8) must be started with a matching pair of
command line arguments to instantiate the same set of device models as
well as a pointer to the saved snapshot.
While the current implementation is useful for several uses cases, it
has a few limitations. The file format for saving the guest state is
tied to the ABI of internal bhyve structures and is not
self-describing (in that it does not communicate the set of device
models present in the system). In addition, the state saved for some
device models closely matches the internal data structures which might
prove a challenge for compatibility of snapshot files across a range
of bhyve versions. The file format also does not currently support
versioning of individual chunks of state. As a result, the current
file format is not a fixed binary format and future revisions to save
and restore will break binary compatiblity of snapshot files. The
goal is to move to a more flexible format that adds versioning,
etc. and at that point to commit to providing a reasonable level of
compatibility. As a result, the current implementation is not enabled
by default. It can be enabled via the WITH_BHYVE_SNAPSHOT=yes option
for userland builds, and the kernel option BHYVE_SHAPSHOT.
Submitted by: Mihai Tiganus, Flavius Anton, Darius Mihai
Submitted by: Elena Mihailescu, Mihai Carabas, Sergiu Weisz
Relnotes: yes
Sponsored by: University Politehnica of Bucharest
Sponsored by: Matthew Grooms (student scholarships)
Sponsored by: iXsystems
Differential Revision: https://reviews.freebsd.org/D19495
2020-05-05 00:02:04 +00:00
|
|
|
struct vm_snapshot_meta;
|
|
|
|
|
2018-04-13 17:23:05 +00:00
|
|
|
#ifdef _KERNEL
|
|
|
|
SDT_PROVIDER_DECLARE(vmm);
|
|
|
|
#endif
|
|
|
|
|
2014-04-28 22:06:40 +00:00
|
|
|
enum vm_suspend_how {
|
|
|
|
VM_SUSPEND_NONE,
|
|
|
|
VM_SUSPEND_RESET,
|
|
|
|
VM_SUSPEND_POWEROFF,
|
2014-05-02 00:33:56 +00:00
|
|
|
VM_SUSPEND_HALT,
|
2014-07-19 20:59:08 +00:00
|
|
|
VM_SUSPEND_TRIPLEFAULT,
|
2014-04-28 22:06:40 +00:00
|
|
|
VM_SUSPEND_LAST
|
|
|
|
};
|
|
|
|
|
2014-06-10 18:46:00 +00:00
|
|
|
/*
|
|
|
|
* Identifiers for architecturally defined registers.
|
|
|
|
*/
|
|
|
|
enum vm_reg_name {
|
|
|
|
VM_REG_GUEST_RAX,
|
|
|
|
VM_REG_GUEST_RBX,
|
|
|
|
VM_REG_GUEST_RCX,
|
|
|
|
VM_REG_GUEST_RDX,
|
|
|
|
VM_REG_GUEST_RSI,
|
|
|
|
VM_REG_GUEST_RDI,
|
|
|
|
VM_REG_GUEST_RBP,
|
|
|
|
VM_REG_GUEST_R8,
|
|
|
|
VM_REG_GUEST_R9,
|
|
|
|
VM_REG_GUEST_R10,
|
|
|
|
VM_REG_GUEST_R11,
|
|
|
|
VM_REG_GUEST_R12,
|
|
|
|
VM_REG_GUEST_R13,
|
|
|
|
VM_REG_GUEST_R14,
|
|
|
|
VM_REG_GUEST_R15,
|
|
|
|
VM_REG_GUEST_CR0,
|
|
|
|
VM_REG_GUEST_CR3,
|
|
|
|
VM_REG_GUEST_CR4,
|
|
|
|
VM_REG_GUEST_DR7,
|
|
|
|
VM_REG_GUEST_RSP,
|
|
|
|
VM_REG_GUEST_RIP,
|
|
|
|
VM_REG_GUEST_RFLAGS,
|
|
|
|
VM_REG_GUEST_ES,
|
|
|
|
VM_REG_GUEST_CS,
|
|
|
|
VM_REG_GUEST_SS,
|
|
|
|
VM_REG_GUEST_DS,
|
|
|
|
VM_REG_GUEST_FS,
|
|
|
|
VM_REG_GUEST_GS,
|
|
|
|
VM_REG_GUEST_LDTR,
|
|
|
|
VM_REG_GUEST_TR,
|
|
|
|
VM_REG_GUEST_IDTR,
|
|
|
|
VM_REG_GUEST_GDTR,
|
|
|
|
VM_REG_GUEST_EFER,
|
|
|
|
VM_REG_GUEST_CR2,
|
2014-07-16 21:26:26 +00:00
|
|
|
VM_REG_GUEST_PDPTE0,
|
|
|
|
VM_REG_GUEST_PDPTE1,
|
|
|
|
VM_REG_GUEST_PDPTE2,
|
|
|
|
VM_REG_GUEST_PDPTE3,
|
2014-09-12 06:15:20 +00:00
|
|
|
VM_REG_GUEST_INTR_SHADOW,
|
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
|
|
|
VM_REG_GUEST_DR0,
|
|
|
|
VM_REG_GUEST_DR1,
|
|
|
|
VM_REG_GUEST_DR2,
|
|
|
|
VM_REG_GUEST_DR3,
|
|
|
|
VM_REG_GUEST_DR6,
|
2019-12-13 19:21:58 +00:00
|
|
|
VM_REG_GUEST_ENTRY_INST_LENGTH,
|
2014-06-10 18:46:00 +00:00
|
|
|
VM_REG_LAST
|
|
|
|
};
|
|
|
|
|
|
|
|
enum x2apic_state {
|
|
|
|
X2APIC_DISABLED,
|
|
|
|
X2APIC_ENABLED,
|
|
|
|
X2APIC_STATE_LAST
|
|
|
|
};
|
|
|
|
|
2014-07-19 20:59:08 +00:00
|
|
|
#define VM_INTINFO_VECTOR(info) ((info) & 0xff)
|
|
|
|
#define VM_INTINFO_DEL_ERRCODE 0x800
|
|
|
|
#define VM_INTINFO_RSVD 0x7ffff000
|
|
|
|
#define VM_INTINFO_VALID 0x80000000
|
|
|
|
#define VM_INTINFO_TYPE 0x700
|
|
|
|
#define VM_INTINFO_HWINTR (0 << 8)
|
|
|
|
#define VM_INTINFO_NMI (2 << 8)
|
|
|
|
#define VM_INTINFO_HWEXCEPTION (3 << 8)
|
|
|
|
#define VM_INTINFO_SWINTR (4 << 8)
|
|
|
|
|
2019-07-12 18:37:56 +00:00
|
|
|
/*
|
|
|
|
* The VM name has to fit into the pathname length constraints of devfs,
|
|
|
|
* governed primarily by SPECNAMELEN. The length is the total number of
|
|
|
|
* characters in the full path, relative to the mount point and not
|
|
|
|
* including any leading '/' characters.
|
|
|
|
* A prefix and a suffix are added to the name specified by the user.
|
|
|
|
* The prefix is usually "vmm/" or "vmm.io/", but can be a few characters
|
|
|
|
* longer for future use.
|
|
|
|
* The suffix is a string that identifies a bootrom image or some similar
|
|
|
|
* image that is attached to the VM. A separator character gets added to
|
|
|
|
* the suffix automatically when generating the full path, so it must be
|
|
|
|
* accounted for, reducing the effective length by 1.
|
|
|
|
* The effective length of a VM name is 229 bytes for FreeBSD 13 and 37
|
|
|
|
* bytes for FreeBSD 12. A minimum length is set for safety and supports
|
|
|
|
* a SPECNAMELEN as small as 32 on old systems.
|
|
|
|
*/
|
|
|
|
#define VM_MAX_PREFIXLEN 10
|
|
|
|
#define VM_MAX_SUFFIXLEN 15
|
|
|
|
#define VM_MIN_NAMELEN 6
|
|
|
|
#define VM_MAX_NAMELEN \
|
|
|
|
(SPECNAMELEN - VM_MAX_PREFIXLEN - VM_MAX_SUFFIXLEN - 1)
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2019-07-12 18:37:56 +00:00
|
|
|
#ifdef _KERNEL
|
|
|
|
CTASSERT(VM_MAX_NAMELEN >= VM_MIN_NAMELEN);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
struct vm;
|
2014-02-26 00:52:05 +00:00
|
|
|
struct vm_exception;
|
2011-05-13 04:54:01 +00:00
|
|
|
struct seg_desc;
|
|
|
|
struct vm_exit;
|
|
|
|
struct vm_run;
|
2013-11-25 19:04:51 +00:00
|
|
|
struct vhpet;
|
2013-11-12 22:51:03 +00:00
|
|
|
struct vioapic;
|
2011-05-13 04:54:01 +00:00
|
|
|
struct vlapic;
|
2013-10-05 21:22:35 +00:00
|
|
|
struct vmspace;
|
|
|
|
struct vm_object;
|
2014-07-23 04:28:51 +00:00
|
|
|
struct vm_guest_paging;
|
2013-10-05 21:22:35 +00:00
|
|
|
struct pmap;
|
Initial support for bhyve save and restore.
Save and restore (also known as suspend and resume) permits a snapshot
to be taken of a guest's state that can later be resumed. In the
current implementation, bhyve(8) creates a UNIX domain socket that is
used by bhyvectl(8) to send a request to save a snapshot (and
optionally exit after the snapshot has been taken). A snapshot
currently consists of two files: the first holds a copy of guest RAM,
and the second file holds other guest state such as vCPU register
values and device model state.
To resume a guest, bhyve(8) must be started with a matching pair of
command line arguments to instantiate the same set of device models as
well as a pointer to the saved snapshot.
While the current implementation is useful for several uses cases, it
has a few limitations. The file format for saving the guest state is
tied to the ABI of internal bhyve structures and is not
self-describing (in that it does not communicate the set of device
models present in the system). In addition, the state saved for some
device models closely matches the internal data structures which might
prove a challenge for compatibility of snapshot files across a range
of bhyve versions. The file format also does not currently support
versioning of individual chunks of state. As a result, the current
file format is not a fixed binary format and future revisions to save
and restore will break binary compatiblity of snapshot files. The
goal is to move to a more flexible format that adds versioning,
etc. and at that point to commit to providing a reasonable level of
compatibility. As a result, the current implementation is not enabled
by default. It can be enabled via the WITH_BHYVE_SNAPSHOT=yes option
for userland builds, and the kernel option BHYVE_SHAPSHOT.
Submitted by: Mihai Tiganus, Flavius Anton, Darius Mihai
Submitted by: Elena Mihailescu, Mihai Carabas, Sergiu Weisz
Relnotes: yes
Sponsored by: University Politehnica of Bucharest
Sponsored by: Matthew Grooms (student scholarships)
Sponsored by: iXsystems
Differential Revision: https://reviews.freebsd.org/D19495
2020-05-05 00:02:04 +00:00
|
|
|
enum snapshot_req;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2015-05-28 17:37:01 +00:00
|
|
|
struct vm_eventinfo {
|
|
|
|
void *rptr; /* rendezvous cookie */
|
|
|
|
int *sptr; /* suspend cookie */
|
|
|
|
int *iptr; /* reqidle cookie */
|
|
|
|
};
|
|
|
|
|
2014-01-09 03:25:54 +00:00
|
|
|
typedef int (*vmm_init_func_t)(int ipinum);
|
2011-05-13 04:54:01 +00:00
|
|
|
typedef int (*vmm_cleanup_func_t)(void);
|
2013-12-23 19:48:22 +00:00
|
|
|
typedef void (*vmm_resume_func_t)(void);
|
2013-10-05 21:22:35 +00:00
|
|
|
typedef void * (*vmi_init_func_t)(struct vm *vm, struct pmap *pmap);
|
|
|
|
typedef int (*vmi_run_func_t)(void *vmi, int vcpu, register_t rip,
|
2015-05-28 17:37:01 +00:00
|
|
|
struct pmap *pmap, struct vm_eventinfo *info);
|
2011-05-13 04:54:01 +00:00
|
|
|
typedef void (*vmi_cleanup_func_t)(void *vmi);
|
|
|
|
typedef int (*vmi_get_register_t)(void *vmi, int vcpu, int num,
|
|
|
|
uint64_t *retval);
|
|
|
|
typedef int (*vmi_set_register_t)(void *vmi, int vcpu, int num,
|
|
|
|
uint64_t val);
|
|
|
|
typedef int (*vmi_get_desc_t)(void *vmi, int vcpu, int num,
|
|
|
|
struct seg_desc *desc);
|
|
|
|
typedef int (*vmi_set_desc_t)(void *vmi, int vcpu, int num,
|
|
|
|
struct seg_desc *desc);
|
|
|
|
typedef int (*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval);
|
|
|
|
typedef int (*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val);
|
2013-10-05 21:22:35 +00:00
|
|
|
typedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max);
|
|
|
|
typedef void (*vmi_vmspace_free)(struct vmspace *vmspace);
|
2013-12-25 06:46:31 +00:00
|
|
|
typedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu);
|
|
|
|
typedef void (*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic);
|
Initial support for bhyve save and restore.
Save and restore (also known as suspend and resume) permits a snapshot
to be taken of a guest's state that can later be resumed. In the
current implementation, bhyve(8) creates a UNIX domain socket that is
used by bhyvectl(8) to send a request to save a snapshot (and
optionally exit after the snapshot has been taken). A snapshot
currently consists of two files: the first holds a copy of guest RAM,
and the second file holds other guest state such as vCPU register
values and device model state.
To resume a guest, bhyve(8) must be started with a matching pair of
command line arguments to instantiate the same set of device models as
well as a pointer to the saved snapshot.
While the current implementation is useful for several uses cases, it
has a few limitations. The file format for saving the guest state is
tied to the ABI of internal bhyve structures and is not
self-describing (in that it does not communicate the set of device
models present in the system). In addition, the state saved for some
device models closely matches the internal data structures which might
prove a challenge for compatibility of snapshot files across a range
of bhyve versions. The file format also does not currently support
versioning of individual chunks of state. As a result, the current
file format is not a fixed binary format and future revisions to save
and restore will break binary compatiblity of snapshot files. The
goal is to move to a more flexible format that adds versioning,
etc. and at that point to commit to providing a reasonable level of
compatibility. As a result, the current implementation is not enabled
by default. It can be enabled via the WITH_BHYVE_SNAPSHOT=yes option
for userland builds, and the kernel option BHYVE_SHAPSHOT.
Submitted by: Mihai Tiganus, Flavius Anton, Darius Mihai
Submitted by: Elena Mihailescu, Mihai Carabas, Sergiu Weisz
Relnotes: yes
Sponsored by: University Politehnica of Bucharest
Sponsored by: Matthew Grooms (student scholarships)
Sponsored by: iXsystems
Differential Revision: https://reviews.freebsd.org/D19495
2020-05-05 00:02:04 +00:00
|
|
|
typedef int (*vmi_snapshot_t)(void *vmi, struct vm_snapshot_meta *meta);
|
|
|
|
typedef int (*vmi_snapshot_vmcx_t)(void *vmi, struct vm_snapshot_meta *meta,
|
|
|
|
int vcpu);
|
|
|
|
typedef int (*vmi_restore_tsc_t)(void *vmi, int vcpuid, uint64_t now);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
struct vmm_ops {
|
|
|
|
vmm_init_func_t init; /* module wide initialization */
|
|
|
|
vmm_cleanup_func_t cleanup;
|
2013-12-23 19:48:22 +00:00
|
|
|
vmm_resume_func_t resume;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
vmi_init_func_t vminit; /* vm-specific initialization */
|
|
|
|
vmi_run_func_t vmrun;
|
|
|
|
vmi_cleanup_func_t vmcleanup;
|
|
|
|
vmi_get_register_t vmgetreg;
|
|
|
|
vmi_set_register_t vmsetreg;
|
|
|
|
vmi_get_desc_t vmgetdesc;
|
|
|
|
vmi_set_desc_t vmsetdesc;
|
|
|
|
vmi_get_cap_t vmgetcap;
|
|
|
|
vmi_set_cap_t vmsetcap;
|
2013-10-05 21:22:35 +00:00
|
|
|
vmi_vmspace_alloc vmspace_alloc;
|
|
|
|
vmi_vmspace_free vmspace_free;
|
2013-12-25 06:46:31 +00:00
|
|
|
vmi_vlapic_init vlapic_init;
|
|
|
|
vmi_vlapic_cleanup vlapic_cleanup;
|
Initial support for bhyve save and restore.
Save and restore (also known as suspend and resume) permits a snapshot
to be taken of a guest's state that can later be resumed. In the
current implementation, bhyve(8) creates a UNIX domain socket that is
used by bhyvectl(8) to send a request to save a snapshot (and
optionally exit after the snapshot has been taken). A snapshot
currently consists of two files: the first holds a copy of guest RAM,
and the second file holds other guest state such as vCPU register
values and device model state.
To resume a guest, bhyve(8) must be started with a matching pair of
command line arguments to instantiate the same set of device models as
well as a pointer to the saved snapshot.
While the current implementation is useful for several uses cases, it
has a few limitations. The file format for saving the guest state is
tied to the ABI of internal bhyve structures and is not
self-describing (in that it does not communicate the set of device
models present in the system). In addition, the state saved for some
device models closely matches the internal data structures which might
prove a challenge for compatibility of snapshot files across a range
of bhyve versions. The file format also does not currently support
versioning of individual chunks of state. As a result, the current
file format is not a fixed binary format and future revisions to save
and restore will break binary compatiblity of snapshot files. The
goal is to move to a more flexible format that adds versioning,
etc. and at that point to commit to providing a reasonable level of
compatibility. As a result, the current implementation is not enabled
by default. It can be enabled via the WITH_BHYVE_SNAPSHOT=yes option
for userland builds, and the kernel option BHYVE_SHAPSHOT.
Submitted by: Mihai Tiganus, Flavius Anton, Darius Mihai
Submitted by: Elena Mihailescu, Mihai Carabas, Sergiu Weisz
Relnotes: yes
Sponsored by: University Politehnica of Bucharest
Sponsored by: Matthew Grooms (student scholarships)
Sponsored by: iXsystems
Differential Revision: https://reviews.freebsd.org/D19495
2020-05-05 00:02:04 +00:00
|
|
|
|
|
|
|
/* checkpoint operations */
|
|
|
|
vmi_snapshot_t vmsnapshot;
|
|
|
|
vmi_snapshot_vmcx_t vmcx_snapshot;
|
|
|
|
vmi_restore_tsc_t vm_restore_tsc;
|
2011-05-13 04:54:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern struct vmm_ops vmm_ops_intel;
|
|
|
|
extern struct vmm_ops vmm_ops_amd;
|
|
|
|
|
2013-04-12 01:16:52 +00:00
|
|
|
int vm_create(const char *name, struct vm **retvm);
|
2011-05-13 04:54:01 +00:00
|
|
|
void vm_destroy(struct vm *vm);
|
2014-06-07 21:36:52 +00:00
|
|
|
int vm_reinit(struct vm *vm);
|
2011-05-13 04:54:01 +00:00
|
|
|
const char *vm_name(struct vm *vm);
|
2019-04-25 22:51:36 +00:00
|
|
|
uint16_t vm_get_maxcpus(struct vm *vm);
|
Add the ability to control the CPU topology of created VMs
from userland without the need to use sysctls, it allows the old
sysctls to continue to function, but deprecates them at
FreeBSD_version 1200060 (Relnotes for deprecate).
The command line of bhyve is maintained in a backwards compatible way.
The API of libvmmapi is maintained in a backwards compatible way.
The sysctl's are maintained in a backwards compatible way.
Added command option looks like:
bhyve -c [[cpus=]n][,sockets=n][,cores=n][,threads=n][,maxcpus=n]
The optional parts can be specified in any order, but only a single
integer invokes the backwards compatible parse. [,maxcpus=n] is
hidden by #ifdef until kernel support is added, though the api
is put in place.
bhyvectl --get-cpu-topology option added.
Reviewed by: grehan (maintainer, earlier version),
Reviewed by: bcr (manpages)
Approved by: bde (mentor), phk (mentor)
Tested by: Oleg Ginzburg <olevole@olevole.ru> (cbsd)
MFC after: 1 week
Relnotes: Y
Differential Revision: https://reviews.freebsd.org/D9930
2018-04-08 19:24:49 +00:00
|
|
|
void vm_get_topology(struct vm *vm, uint16_t *sockets, uint16_t *cores,
|
|
|
|
uint16_t *threads, uint16_t *maxcpus);
|
|
|
|
int vm_set_topology(struct vm *vm, uint16_t sockets, uint16_t cores,
|
|
|
|
uint16_t threads, uint16_t maxcpus);
|
2015-06-18 06:00:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* APIs that modify the guest memory map require all vcpus to be frozen.
|
|
|
|
*/
|
|
|
|
int vm_mmap_memseg(struct vm *vm, vm_paddr_t gpa, int segid, vm_ooffset_t off,
|
|
|
|
size_t len, int prot, int flags);
|
|
|
|
int vm_alloc_memseg(struct vm *vm, int ident, size_t len, bool sysmem);
|
|
|
|
void vm_free_memseg(struct vm *vm, int ident);
|
2011-05-13 04:54:01 +00:00
|
|
|
int vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
|
|
|
|
int vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
|
2015-06-18 06:00:17 +00:00
|
|
|
int vm_assign_pptdev(struct vm *vm, int bus, int slot, int func);
|
|
|
|
int vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* APIs that inspect the guest memory map require only a *single* vcpu to
|
|
|
|
* be frozen. This acts like a read lock on the guest memory map since any
|
|
|
|
* modification requires *all* vcpus to be frozen.
|
|
|
|
*/
|
|
|
|
int vm_mmap_getnext(struct vm *vm, vm_paddr_t *gpa, int *segid,
|
|
|
|
vm_ooffset_t *segoff, size_t *len, int *prot, int *flags);
|
|
|
|
int vm_get_memseg(struct vm *vm, int ident, size_t *len, bool *sysmem,
|
|
|
|
struct vm_object **objptr);
|
2018-05-15 17:20:58 +00:00
|
|
|
vm_paddr_t vmm_sysmem_maxaddr(struct vm *vm);
|
2015-06-18 06:00:17 +00:00
|
|
|
void *vm_gpa_hold(struct vm *, int vcpuid, vm_paddr_t gpa, size_t len,
|
|
|
|
int prot, void **cookie);
|
2013-10-05 21:22:35 +00:00
|
|
|
void vm_gpa_release(void *cookie);
|
2015-06-18 06:00:17 +00:00
|
|
|
bool vm_mem_allocated(struct vm *vm, int vcpuid, vm_paddr_t gpa);
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
int vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval);
|
|
|
|
int vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val);
|
|
|
|
int vm_get_seg_desc(struct vm *vm, int vcpu, int reg,
|
|
|
|
struct seg_desc *ret_desc);
|
|
|
|
int vm_set_seg_desc(struct vm *vm, int vcpu, int reg,
|
|
|
|
struct seg_desc *desc);
|
|
|
|
int vm_run(struct vm *vm, struct vm_run *vmrun);
|
2014-04-28 22:06:40 +00:00
|
|
|
int vm_suspend(struct vm *vm, enum vm_suspend_how how);
|
2011-05-13 04:54:01 +00:00
|
|
|
int vm_inject_nmi(struct vm *vm, int vcpu);
|
2012-10-24 02:54:21 +00:00
|
|
|
int vm_nmi_pending(struct vm *vm, int vcpuid);
|
|
|
|
void vm_nmi_clear(struct vm *vm, int vcpuid);
|
2014-03-15 23:09:34 +00:00
|
|
|
int vm_inject_extint(struct vm *vm, int vcpu);
|
|
|
|
int vm_extint_pending(struct vm *vm, int vcpuid);
|
|
|
|
void vm_extint_clear(struct vm *vm, int vcpuid);
|
2011-05-13 04:54:01 +00:00
|
|
|
struct vlapic *vm_lapic(struct vm *vm, int cpu);
|
2013-11-12 22:51:03 +00:00
|
|
|
struct vioapic *vm_ioapic(struct vm *vm);
|
2013-11-25 19:04:51 +00:00
|
|
|
struct vhpet *vm_hpet(struct vm *vm);
|
2011-05-13 04:54:01 +00:00
|
|
|
int vm_get_capability(struct vm *vm, int vcpu, int type, int *val);
|
|
|
|
int vm_set_capability(struct vm *vm, int vcpu, int type, int val);
|
2012-09-25 19:08:51 +00:00
|
|
|
int vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state);
|
|
|
|
int vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state);
|
2013-11-12 22:51:03 +00:00
|
|
|
int vm_apicid2vcpuid(struct vm *vm, int apicid);
|
2014-05-31 23:37:34 +00:00
|
|
|
int vm_activate_cpu(struct vm *vm, int vcpu);
|
2018-04-06 22:03:43 +00:00
|
|
|
int vm_suspend_cpu(struct vm *vm, int vcpu);
|
|
|
|
int vm_resume_cpu(struct vm *vm, int vcpu);
|
2012-09-24 19:32:24 +00:00
|
|
|
struct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid);
|
2014-04-28 22:06:40 +00:00
|
|
|
void vm_exit_suspended(struct vm *vm, int vcpuid, uint64_t rip);
|
2018-04-06 22:03:43 +00:00
|
|
|
void vm_exit_debug(struct vm *vm, int vcpuid, uint64_t rip);
|
2014-06-10 16:45:58 +00:00
|
|
|
void vm_exit_rendezvous(struct vm *vm, int vcpuid, uint64_t rip);
|
|
|
|
void vm_exit_astpending(struct vm *vm, int vcpuid, uint64_t rip);
|
2015-05-28 17:37:01 +00:00
|
|
|
void vm_exit_reqidle(struct vm *vm, int vcpuid, uint64_t rip);
|
Initial support for bhyve save and restore.
Save and restore (also known as suspend and resume) permits a snapshot
to be taken of a guest's state that can later be resumed. In the
current implementation, bhyve(8) creates a UNIX domain socket that is
used by bhyvectl(8) to send a request to save a snapshot (and
optionally exit after the snapshot has been taken). A snapshot
currently consists of two files: the first holds a copy of guest RAM,
and the second file holds other guest state such as vCPU register
values and device model state.
To resume a guest, bhyve(8) must be started with a matching pair of
command line arguments to instantiate the same set of device models as
well as a pointer to the saved snapshot.
While the current implementation is useful for several uses cases, it
has a few limitations. The file format for saving the guest state is
tied to the ABI of internal bhyve structures and is not
self-describing (in that it does not communicate the set of device
models present in the system). In addition, the state saved for some
device models closely matches the internal data structures which might
prove a challenge for compatibility of snapshot files across a range
of bhyve versions. The file format also does not currently support
versioning of individual chunks of state. As a result, the current
file format is not a fixed binary format and future revisions to save
and restore will break binary compatiblity of snapshot files. The
goal is to move to a more flexible format that adds versioning,
etc. and at that point to commit to providing a reasonable level of
compatibility. As a result, the current implementation is not enabled
by default. It can be enabled via the WITH_BHYVE_SNAPSHOT=yes option
for userland builds, and the kernel option BHYVE_SHAPSHOT.
Submitted by: Mihai Tiganus, Flavius Anton, Darius Mihai
Submitted by: Elena Mihailescu, Mihai Carabas, Sergiu Weisz
Relnotes: yes
Sponsored by: University Politehnica of Bucharest
Sponsored by: Matthew Grooms (student scholarships)
Sponsored by: iXsystems
Differential Revision: https://reviews.freebsd.org/D19495
2020-05-05 00:02:04 +00:00
|
|
|
int vm_snapshot_req(struct vm *vm, struct vm_snapshot_meta *meta);
|
|
|
|
int vm_restore_time(struct vm *vm);
|
|
|
|
|
2015-04-30 22:23:22 +00:00
|
|
|
#ifdef _SYS__CPUSET_H_
|
2014-01-14 01:55:58 +00:00
|
|
|
/*
|
|
|
|
* Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'.
|
|
|
|
* The rendezvous 'func(arg)' is not allowed to do anything that will
|
|
|
|
* cause the thread to be put to sleep.
|
|
|
|
*
|
|
|
|
* If the rendezvous is being initiated from a vcpu context then the
|
|
|
|
* 'vcpuid' must refer to that vcpu, otherwise it should be set to -1.
|
|
|
|
*
|
|
|
|
* The caller cannot hold any locks when initiating the rendezvous.
|
|
|
|
*
|
|
|
|
* The implementation of this API may cause vcpus other than those specified
|
|
|
|
* by 'dest' to be stalled. The caller should not rely on any vcpus making
|
|
|
|
* forward progress when the rendezvous is in progress.
|
|
|
|
*/
|
|
|
|
typedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg);
|
2020-01-02 22:37:04 +00:00
|
|
|
int vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest,
|
2014-01-14 01:55:58 +00:00
|
|
|
vm_rendezvous_func_t func, void *arg);
|
2015-04-30 22:23:22 +00:00
|
|
|
cpuset_t vm_active_cpus(struct vm *vm);
|
2018-04-06 22:03:43 +00:00
|
|
|
cpuset_t vm_debug_cpus(struct vm *vm);
|
2015-04-30 22:23:22 +00:00
|
|
|
cpuset_t vm_suspended_cpus(struct vm *vm);
|
|
|
|
#endif /* _SYS__CPUSET_H_ */
|
2014-01-14 01:55:58 +00:00
|
|
|
|
|
|
|
static __inline int
|
2015-05-28 17:37:01 +00:00
|
|
|
vcpu_rendezvous_pending(struct vm_eventinfo *info)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (*((uintptr_t *)(info->rptr)) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline int
|
|
|
|
vcpu_suspended(struct vm_eventinfo *info)
|
2014-01-14 01:55:58 +00:00
|
|
|
{
|
|
|
|
|
2015-05-28 17:37:01 +00:00
|
|
|
return (*info->sptr);
|
2014-01-14 01:55:58 +00:00
|
|
|
}
|
|
|
|
|
2014-03-26 23:34:27 +00:00
|
|
|
static __inline int
|
2015-05-28 17:37:01 +00:00
|
|
|
vcpu_reqidle(struct vm_eventinfo *info)
|
2014-03-26 23:34:27 +00:00
|
|
|
{
|
|
|
|
|
2015-05-28 17:37:01 +00:00
|
|
|
return (*info->iptr);
|
2014-03-26 23:34:27 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 22:03:43 +00:00
|
|
|
int vcpu_debugged(struct vm *vm, int vcpuid);
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
/*
|
2019-08-01 02:16:48 +00:00
|
|
|
* Return true if device indicated by bus/slot/func is supposed to be a
|
2011-05-13 04:54:01 +00:00
|
|
|
* pci passthrough device.
|
|
|
|
*
|
2019-08-01 02:16:48 +00:00
|
|
|
* Return false otherwise.
|
2011-05-13 04:54:01 +00:00
|
|
|
*/
|
2019-08-01 02:16:48 +00:00
|
|
|
bool vmm_is_pptdev(int bus, int slot, int func);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
void *vm_iommu_domain(struct vm *vm);
|
|
|
|
|
2012-10-12 18:32:44 +00:00
|
|
|
enum vcpu_state {
|
|
|
|
VCPU_IDLE,
|
2013-10-05 21:22:35 +00:00
|
|
|
VCPU_FROZEN,
|
2012-10-12 18:32:44 +00:00
|
|
|
VCPU_RUNNING,
|
2013-10-05 21:22:35 +00:00
|
|
|
VCPU_SLEEPING,
|
2012-10-12 18:32:44 +00:00
|
|
|
};
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2013-12-22 20:29:59 +00:00
|
|
|
int vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state,
|
|
|
|
bool from_idle);
|
2013-04-25 04:56:43 +00:00
|
|
|
enum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
static int __inline
|
2013-04-25 04:56:43 +00:00
|
|
|
vcpu_is_running(struct vm *vm, int vcpu, int *hostcpu)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
2013-04-25 04:56:43 +00:00
|
|
|
return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 02:53:51 +00:00
|
|
|
#ifdef _SYS_PROC_H_
|
|
|
|
static int __inline
|
|
|
|
vcpu_should_yield(struct vm *vm, int vcpu)
|
|
|
|
{
|
2015-05-06 23:40:24 +00:00
|
|
|
|
|
|
|
if (curthread->td_flags & (TDF_ASTPENDING | TDF_NEEDRESCHED))
|
|
|
|
return (1);
|
|
|
|
else if (curthread->td_owepreempt)
|
|
|
|
return (1);
|
|
|
|
else
|
|
|
|
return (0);
|
2014-07-26 02:53:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-12 18:32:44 +00:00
|
|
|
void *vcpu_stats(struct vm *vm, int vcpu);
|
2013-12-25 06:46:31 +00:00
|
|
|
void vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr);
|
2013-10-05 21:22:35 +00:00
|
|
|
struct vmspace *vm_get_vmspace(struct vm *vm);
|
2014-03-11 16:56:00 +00:00
|
|
|
struct vatpic *vm_atpic(struct vm *vm);
|
2014-03-25 19:20:34 +00:00
|
|
|
struct vatpit *vm_atpit(struct vm *vm);
|
2014-10-26 04:44:28 +00:00
|
|
|
struct vpmtmr *vm_pmtmr(struct vm *vm);
|
2014-12-30 22:19:34 +00:00
|
|
|
struct vrtc *vm_rtc(struct vm *vm);
|
2014-02-26 00:52:05 +00:00
|
|
|
|
|
|
|
/*
|
2015-01-13 22:00:47 +00:00
|
|
|
* Inject exception 'vector' into the guest vcpu. This function returns 0 on
|
2014-02-26 00:52:05 +00:00
|
|
|
* success and non-zero on failure.
|
|
|
|
*
|
|
|
|
* Wrapper functions like 'vm_inject_gp()' should be preferred to calling
|
|
|
|
* this function directly because they enforce the trap-like or fault-like
|
|
|
|
* behavior of an exception.
|
|
|
|
*
|
|
|
|
* This function should only be called in the context of the thread that is
|
|
|
|
* executing this vcpu.
|
|
|
|
*/
|
2015-01-13 22:00:47 +00:00
|
|
|
int vm_inject_exception(struct vm *vm, int vcpuid, int vector, int err_valid,
|
|
|
|
uint32_t errcode, int restart_instruction);
|
2014-02-26 00:52:05 +00:00
|
|
|
|
|
|
|
/*
|
2014-07-19 20:59:08 +00:00
|
|
|
* This function is called after a VM-exit that occurred during exception or
|
|
|
|
* interrupt delivery through the IDT. The format of 'intinfo' is described
|
|
|
|
* in Figure 15-1, "EXITINTINFO for All Intercepts", APM, Vol 2.
|
2014-02-26 00:52:05 +00:00
|
|
|
*
|
2014-07-19 20:59:08 +00:00
|
|
|
* If a VM-exit handler completes the event delivery successfully then it
|
|
|
|
* should call vm_exit_intinfo() to extinguish the pending event. For e.g.,
|
|
|
|
* if the task switch emulation is triggered via a task gate then it should
|
|
|
|
* call this function with 'intinfo=0' to indicate that the external event
|
|
|
|
* is not pending anymore.
|
|
|
|
*
|
|
|
|
* Return value is 0 on success and non-zero on failure.
|
2014-02-26 00:52:05 +00:00
|
|
|
*/
|
2014-07-19 20:59:08 +00:00
|
|
|
int vm_exit_intinfo(struct vm *vm, int vcpuid, uint64_t intinfo);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called before every VM-entry to retrieve a pending
|
|
|
|
* event that should be injected into the guest. This function combines
|
|
|
|
* nested events into a double or triple fault.
|
|
|
|
*
|
|
|
|
* Returns 0 if there are no events that need to be injected into the guest
|
|
|
|
* and non-zero otherwise.
|
|
|
|
*/
|
|
|
|
int vm_entry_intinfo(struct vm *vm, int vcpuid, uint64_t *info);
|
|
|
|
|
|
|
|
int vm_get_intinfo(struct vm *vm, int vcpuid, uint64_t *info1, uint64_t *info2);
|
2014-02-26 00:52:05 +00:00
|
|
|
|
Initial support for bhyve save and restore.
Save and restore (also known as suspend and resume) permits a snapshot
to be taken of a guest's state that can later be resumed. In the
current implementation, bhyve(8) creates a UNIX domain socket that is
used by bhyvectl(8) to send a request to save a snapshot (and
optionally exit after the snapshot has been taken). A snapshot
currently consists of two files: the first holds a copy of guest RAM,
and the second file holds other guest state such as vCPU register
values and device model state.
To resume a guest, bhyve(8) must be started with a matching pair of
command line arguments to instantiate the same set of device models as
well as a pointer to the saved snapshot.
While the current implementation is useful for several uses cases, it
has a few limitations. The file format for saving the guest state is
tied to the ABI of internal bhyve structures and is not
self-describing (in that it does not communicate the set of device
models present in the system). In addition, the state saved for some
device models closely matches the internal data structures which might
prove a challenge for compatibility of snapshot files across a range
of bhyve versions. The file format also does not currently support
versioning of individual chunks of state. As a result, the current
file format is not a fixed binary format and future revisions to save
and restore will break binary compatiblity of snapshot files. The
goal is to move to a more flexible format that adds versioning,
etc. and at that point to commit to providing a reasonable level of
compatibility. As a result, the current implementation is not enabled
by default. It can be enabled via the WITH_BHYVE_SNAPSHOT=yes option
for userland builds, and the kernel option BHYVE_SHAPSHOT.
Submitted by: Mihai Tiganus, Flavius Anton, Darius Mihai
Submitted by: Elena Mihailescu, Mihai Carabas, Sergiu Weisz
Relnotes: yes
Sponsored by: University Politehnica of Bucharest
Sponsored by: Matthew Grooms (student scholarships)
Sponsored by: iXsystems
Differential Revision: https://reviews.freebsd.org/D19495
2020-05-05 00:02:04 +00:00
|
|
|
/*
|
|
|
|
* Function used to keep track of the guest's TSC offset. The
|
|
|
|
* offset is used by the virutalization extensions to provide a consistent
|
|
|
|
* value for the Time Stamp Counter to the guest.
|
|
|
|
*
|
|
|
|
* Return value is 0 on success and non-zero on failure.
|
|
|
|
*/
|
|
|
|
int vm_set_tsc_offset(struct vm *vm, int vcpu_id, uint64_t offset);
|
|
|
|
|
2014-05-23 05:15:17 +00:00
|
|
|
enum vm_reg_name vm_segment_name(int seg_encoding);
|
|
|
|
|
2014-07-23 04:28:51 +00:00
|
|
|
struct vm_copyinfo {
|
|
|
|
uint64_t gpa;
|
|
|
|
size_t len;
|
|
|
|
void *hva;
|
|
|
|
void *cookie;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up 'copyinfo[]' to copy to/from guest linear address space starting
|
|
|
|
* at 'gla' and 'len' bytes long. The 'prot' should be set to PROT_READ for
|
|
|
|
* a copyin or PROT_WRITE for a copyout.
|
|
|
|
*
|
2016-05-03 22:13:04 +00:00
|
|
|
* retval is_fault Interpretation
|
2015-05-06 16:25:20 +00:00
|
|
|
* 0 0 Success
|
|
|
|
* 0 1 An exception was injected into the guest
|
|
|
|
* EFAULT N/A Unrecoverable error
|
2014-07-23 04:28:51 +00:00
|
|
|
*
|
|
|
|
* The 'copyinfo[]' can be passed to 'vm_copyin()' or 'vm_copyout()' only if
|
|
|
|
* the return value is 0. The 'copyinfo[]' resources should be freed by calling
|
|
|
|
* 'vm_copy_teardown()' after the copy is done.
|
|
|
|
*/
|
|
|
|
int vm_copy_setup(struct vm *vm, int vcpuid, struct vm_guest_paging *paging,
|
|
|
|
uint64_t gla, size_t len, int prot, struct vm_copyinfo *copyinfo,
|
2015-05-06 16:25:20 +00:00
|
|
|
int num_copyinfo, int *is_fault);
|
2014-07-23 04:28:51 +00:00
|
|
|
void vm_copy_teardown(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo,
|
|
|
|
int num_copyinfo);
|
|
|
|
void vm_copyin(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo,
|
|
|
|
void *kaddr, size_t len);
|
|
|
|
void vm_copyout(struct vm *vm, int vcpuid, const void *kaddr,
|
|
|
|
struct vm_copyinfo *copyinfo, size_t len);
|
2014-12-23 02:14:49 +00:00
|
|
|
|
|
|
|
int vcpu_trace_exceptions(struct vm *vm, int vcpuid);
|
2011-05-13 04:54:01 +00:00
|
|
|
#endif /* KERNEL */
|
|
|
|
|
2013-09-10 03:48:18 +00:00
|
|
|
#define VM_MAXCPU 16 /* maximum virtual cpus */
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Identifiers for optional vmm capabilities
|
|
|
|
*/
|
|
|
|
enum vm_cap_type {
|
|
|
|
VM_CAP_HALT_EXIT,
|
|
|
|
VM_CAP_MTRAP_EXIT,
|
|
|
|
VM_CAP_PAUSE_EXIT,
|
|
|
|
VM_CAP_UNRESTRICTED_GUEST,
|
2013-10-16 18:20:27 +00:00
|
|
|
VM_CAP_ENABLE_INVPCID,
|
2019-12-13 19:21:58 +00:00
|
|
|
VM_CAP_BPT_EXIT,
|
2020-08-18 07:23:47 +00:00
|
|
|
VM_CAP_RDPID,
|
|
|
|
VM_CAP_RDTSCP,
|
2011-05-13 04:54:01 +00:00
|
|
|
VM_CAP_MAX
|
|
|
|
};
|
|
|
|
|
2014-05-15 14:16:55 +00:00
|
|
|
enum vm_intr_trigger {
|
|
|
|
EDGE_TRIGGER,
|
|
|
|
LEVEL_TRIGGER
|
|
|
|
};
|
2020-09-01 21:16:54 +00:00
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
/*
|
|
|
|
* The 'access' field has the format specified in Table 21-2 of the Intel
|
|
|
|
* Architecture Manual vol 3b.
|
|
|
|
*
|
|
|
|
* XXX The contents of the 'access' field are architecturally defined except
|
|
|
|
* bit 16 - Segment Unusable.
|
|
|
|
*/
|
|
|
|
struct seg_desc {
|
|
|
|
uint64_t base;
|
|
|
|
uint32_t limit;
|
|
|
|
uint32_t access;
|
|
|
|
};
|
2014-07-15 17:37:17 +00:00
|
|
|
#define SEG_DESC_TYPE(access) ((access) & 0x001f)
|
2014-07-16 21:26:26 +00:00
|
|
|
#define SEG_DESC_DPL(access) (((access) >> 5) & 0x3)
|
2014-07-15 17:37:17 +00:00
|
|
|
#define SEG_DESC_PRESENT(access) (((access) & 0x0080) ? 1 : 0)
|
|
|
|
#define SEG_DESC_DEF32(access) (((access) & 0x4000) ? 1 : 0)
|
|
|
|
#define SEG_DESC_GRANULARITY(access) (((access) & 0x8000) ? 1 : 0)
|
|
|
|
#define SEG_DESC_UNUSABLE(access) (((access) & 0x10000) ? 1 : 0)
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2014-05-24 20:26:57 +00:00
|
|
|
enum vm_cpu_mode {
|
2014-07-08 21:48:57 +00:00
|
|
|
CPU_MODE_REAL,
|
|
|
|
CPU_MODE_PROTECTED,
|
2014-05-24 20:26:57 +00:00
|
|
|
CPU_MODE_COMPATIBILITY, /* IA-32E mode (CS.L = 0) */
|
|
|
|
CPU_MODE_64BIT, /* IA-32E mode (CS.L = 1) */
|
|
|
|
};
|
|
|
|
|
|
|
|
enum vm_paging_mode {
|
|
|
|
PAGING_MODE_FLAT,
|
|
|
|
PAGING_MODE_32,
|
|
|
|
PAGING_MODE_PAE,
|
|
|
|
PAGING_MODE_64,
|
2020-08-23 20:37:21 +00:00
|
|
|
PAGING_MODE_64_LA57,
|
2014-05-24 20:26:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct vm_guest_paging {
|
|
|
|
uint64_t cr3;
|
|
|
|
int cpl;
|
|
|
|
enum vm_cpu_mode cpu_mode;
|
|
|
|
enum vm_paging_mode paging_mode;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The data structures 'vie' and 'vie_op' are meant to be opaque to the
|
|
|
|
* consumers of instruction decoding. The only reason why their contents
|
|
|
|
* need to be exposed is because they are part of the 'vm_exit' structure.
|
|
|
|
*/
|
|
|
|
struct vie_op {
|
|
|
|
uint8_t op_byte; /* actual opcode byte */
|
|
|
|
uint8_t op_type; /* type of operation (e.g. MOV) */
|
|
|
|
uint16_t op_flags;
|
|
|
|
};
|
2020-04-17 15:19:42 +00:00
|
|
|
_Static_assert(sizeof(struct vie_op) == 4, "ABI");
|
|
|
|
_Static_assert(_Alignof(struct vie_op) == 2, "ABI");
|
2014-05-24 20:26:57 +00:00
|
|
|
|
|
|
|
#define VIE_INST_SIZE 15
|
|
|
|
struct vie {
|
|
|
|
uint8_t inst[VIE_INST_SIZE]; /* instruction bytes */
|
|
|
|
uint8_t num_valid; /* size of the instruction */
|
2020-06-25 00:18:42 +00:00
|
|
|
|
|
|
|
/* The following fields are all zeroed upon restart. */
|
|
|
|
#define vie_startzero num_processed
|
2014-05-24 20:26:57 +00:00
|
|
|
uint8_t num_processed;
|
|
|
|
|
2014-07-15 17:37:17 +00:00
|
|
|
uint8_t addrsize:4, opsize:4; /* address and operand sizes */
|
2014-05-24 20:26:57 +00:00
|
|
|
uint8_t rex_w:1, /* REX prefix */
|
|
|
|
rex_r:1,
|
|
|
|
rex_x:1,
|
|
|
|
rex_b:1,
|
2014-07-15 17:37:17 +00:00
|
|
|
rex_present:1,
|
2015-01-19 06:53:31 +00:00
|
|
|
repz_present:1, /* REP/REPE/REPZ prefix */
|
|
|
|
repnz_present:1, /* REPNE/REPNZ prefix */
|
2014-07-15 17:37:17 +00:00
|
|
|
opsize_override:1, /* Operand size override */
|
2015-01-19 06:53:31 +00:00
|
|
|
addrsize_override:1, /* Address size override */
|
|
|
|
segment_override:1; /* Segment override */
|
2014-05-24 20:26:57 +00:00
|
|
|
|
|
|
|
uint8_t mod:2, /* ModRM byte */
|
|
|
|
reg:4,
|
|
|
|
rm:4;
|
|
|
|
|
|
|
|
uint8_t ss:2, /* SIB byte */
|
2020-04-21 21:33:06 +00:00
|
|
|
vex_present:1, /* VEX prefixed */
|
|
|
|
vex_l:1, /* L bit */
|
2020-04-17 15:19:42 +00:00
|
|
|
index:4, /* SIB byte */
|
|
|
|
base:4; /* SIB byte */
|
2014-05-24 20:26:57 +00:00
|
|
|
|
|
|
|
uint8_t disp_bytes;
|
|
|
|
uint8_t imm_bytes;
|
|
|
|
|
|
|
|
uint8_t scale;
|
2020-04-17 15:19:42 +00:00
|
|
|
|
2020-04-21 21:33:06 +00:00
|
|
|
uint8_t vex_reg:4, /* vvvv: first source register specifier */
|
|
|
|
vex_pp:2, /* pp */
|
|
|
|
_sparebits:2;
|
|
|
|
|
|
|
|
uint8_t _sparebytes[2];
|
2020-04-17 15:19:42 +00:00
|
|
|
|
2014-05-24 20:26:57 +00:00
|
|
|
int base_register; /* VM_REG_GUEST_xyz */
|
|
|
|
int index_register; /* VM_REG_GUEST_xyz */
|
2015-01-19 06:53:31 +00:00
|
|
|
int segment_register; /* VM_REG_GUEST_xyz */
|
2014-05-24 20:26:57 +00:00
|
|
|
|
|
|
|
int64_t displacement; /* optional addr displacement */
|
|
|
|
int64_t immediate; /* optional immediate operand */
|
|
|
|
|
|
|
|
uint8_t decoded; /* set to 1 if successfully decoded */
|
|
|
|
|
2020-04-17 15:19:42 +00:00
|
|
|
uint8_t _sparebyte;
|
|
|
|
|
2014-05-24 20:26:57 +00:00
|
|
|
struct vie_op op; /* opcode description */
|
|
|
|
};
|
2020-04-17 15:19:42 +00:00
|
|
|
_Static_assert(sizeof(struct vie) == 64, "ABI");
|
|
|
|
_Static_assert(__offsetof(struct vie, disp_bytes) == 22, "ABI");
|
|
|
|
_Static_assert(__offsetof(struct vie, scale) == 24, "ABI");
|
|
|
|
_Static_assert(__offsetof(struct vie, base_register) == 28, "ABI");
|
2014-05-24 20:26:57 +00:00
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
enum vm_exitcode {
|
|
|
|
VM_EXITCODE_INOUT,
|
|
|
|
VM_EXITCODE_VMX,
|
|
|
|
VM_EXITCODE_BOGUS,
|
|
|
|
VM_EXITCODE_RDMSR,
|
|
|
|
VM_EXITCODE_WRMSR,
|
|
|
|
VM_EXITCODE_HLT,
|
|
|
|
VM_EXITCODE_MTRAP,
|
|
|
|
VM_EXITCODE_PAUSE,
|
2012-04-28 16:28:00 +00:00
|
|
|
VM_EXITCODE_PAGING,
|
2013-10-05 21:22:35 +00:00
|
|
|
VM_EXITCODE_INST_EMUL,
|
2012-09-25 02:33:25 +00:00
|
|
|
VM_EXITCODE_SPINUP_AP,
|
2014-04-29 18:42:56 +00:00
|
|
|
VM_EXITCODE_DEPRECATED1, /* used to be SPINDOWN_CPU */
|
2014-01-14 01:55:58 +00:00
|
|
|
VM_EXITCODE_RENDEZVOUS,
|
2014-01-25 20:58:05 +00:00
|
|
|
VM_EXITCODE_IOAPIC_EOI,
|
2014-03-26 23:34:27 +00:00
|
|
|
VM_EXITCODE_SUSPENDED,
|
2014-05-23 05:15:17 +00:00
|
|
|
VM_EXITCODE_INOUT_STR,
|
2014-07-16 21:26:26 +00:00
|
|
|
VM_EXITCODE_TASK_SWITCH,
|
2014-10-06 20:48:01 +00:00
|
|
|
VM_EXITCODE_MONITOR,
|
|
|
|
VM_EXITCODE_MWAIT,
|
2014-09-14 04:39:04 +00:00
|
|
|
VM_EXITCODE_SVM,
|
2015-05-28 17:37:01 +00:00
|
|
|
VM_EXITCODE_REQIDLE,
|
2018-04-06 22:03:43 +00:00
|
|
|
VM_EXITCODE_DEBUG,
|
2018-09-27 11:16:19 +00:00
|
|
|
VM_EXITCODE_VMINSN,
|
2019-12-13 19:21:58 +00:00
|
|
|
VM_EXITCODE_BPT,
|
2012-04-28 16:28:00 +00:00
|
|
|
VM_EXITCODE_MAX
|
2011-05-13 04:54:01 +00:00
|
|
|
};
|
|
|
|
|
2014-05-23 05:15:17 +00:00
|
|
|
struct vm_inout {
|
|
|
|
uint16_t bytes:3; /* 1 or 2 or 4 */
|
|
|
|
uint16_t in:1;
|
|
|
|
uint16_t string:1;
|
|
|
|
uint16_t rep:1;
|
|
|
|
uint16_t port;
|
|
|
|
uint32_t eax; /* valid for out */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct vm_inout_str {
|
|
|
|
struct vm_inout inout; /* must be the first element */
|
2014-05-24 20:26:57 +00:00
|
|
|
struct vm_guest_paging paging;
|
2014-05-23 05:15:17 +00:00
|
|
|
uint64_t rflags;
|
|
|
|
uint64_t cr0;
|
|
|
|
uint64_t index;
|
|
|
|
uint64_t count; /* rep=1 (%rcx), rep=0 (1) */
|
|
|
|
int addrsize;
|
|
|
|
enum vm_reg_name seg_name;
|
|
|
|
struct seg_desc seg_desc;
|
|
|
|
};
|
|
|
|
|
2014-07-16 21:26:26 +00:00
|
|
|
enum task_switch_reason {
|
|
|
|
TSR_CALL,
|
|
|
|
TSR_IRET,
|
|
|
|
TSR_JMP,
|
|
|
|
TSR_IDT_GATE, /* task gate in IDT */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct vm_task_switch {
|
|
|
|
uint16_t tsssel; /* new TSS selector */
|
|
|
|
int ext; /* task switch due to external event */
|
|
|
|
uint32_t errcode;
|
|
|
|
int errcode_valid; /* push 'errcode' on the new stack */
|
|
|
|
enum task_switch_reason reason;
|
|
|
|
struct vm_guest_paging paging;
|
|
|
|
};
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
struct vm_exit {
|
|
|
|
enum vm_exitcode exitcode;
|
|
|
|
int inst_length; /* 0 means unknown */
|
|
|
|
uint64_t rip;
|
|
|
|
union {
|
2014-05-23 05:15:17 +00:00
|
|
|
struct vm_inout inout;
|
|
|
|
struct vm_inout_str inout_str;
|
2012-04-28 16:28:00 +00:00
|
|
|
struct {
|
2012-10-12 23:12:19 +00:00
|
|
|
uint64_t gpa;
|
2013-10-05 21:22:35 +00:00
|
|
|
int fault_type;
|
2012-04-28 16:28:00 +00:00
|
|
|
} paging;
|
2013-10-05 21:22:35 +00:00
|
|
|
struct {
|
|
|
|
uint64_t gpa;
|
|
|
|
uint64_t gla;
|
2015-03-24 17:12:36 +00:00
|
|
|
uint64_t cs_base;
|
2014-07-15 17:37:17 +00:00
|
|
|
int cs_d; /* CS.D */
|
2014-05-24 20:26:57 +00:00
|
|
|
struct vm_guest_paging paging;
|
2013-10-05 21:22:35 +00:00
|
|
|
struct vie vie;
|
|
|
|
} inst_emul;
|
2011-05-13 04:54:01 +00:00
|
|
|
/*
|
|
|
|
* VMX specific payload. Used when there is no "better"
|
|
|
|
* exitcode to represent the VM-exit.
|
|
|
|
*/
|
|
|
|
struct {
|
2014-01-01 21:17:08 +00:00
|
|
|
int status; /* vmx inst status */
|
|
|
|
/*
|
|
|
|
* 'exit_reason' and 'exit_qualification' are valid
|
|
|
|
* only if 'status' is zero.
|
|
|
|
*/
|
2011-05-13 04:54:01 +00:00
|
|
|
uint32_t exit_reason;
|
|
|
|
uint64_t exit_qualification;
|
2014-01-01 21:17:08 +00:00
|
|
|
/*
|
|
|
|
* 'inst_error' and 'inst_type' are valid
|
|
|
|
* only if 'status' is non-zero.
|
|
|
|
*/
|
|
|
|
int inst_type;
|
|
|
|
int inst_error;
|
2011-05-13 04:54:01 +00:00
|
|
|
} vmx;
|
2014-09-14 04:39:04 +00:00
|
|
|
/*
|
|
|
|
* SVM specific payload.
|
|
|
|
*/
|
|
|
|
struct {
|
|
|
|
uint64_t exitcode;
|
|
|
|
uint64_t exitinfo1;
|
|
|
|
uint64_t exitinfo2;
|
|
|
|
} svm;
|
2019-12-13 19:21:58 +00:00
|
|
|
struct {
|
|
|
|
int inst_length;
|
|
|
|
} bpt;
|
2011-05-13 04:54:01 +00:00
|
|
|
struct {
|
|
|
|
uint32_t code; /* ecx value */
|
|
|
|
uint64_t wval;
|
|
|
|
} msr;
|
2012-09-25 02:33:25 +00:00
|
|
|
struct {
|
|
|
|
int vcpu;
|
|
|
|
uint64_t rip;
|
|
|
|
} spinup_ap;
|
2013-12-07 22:18:36 +00:00
|
|
|
struct {
|
|
|
|
uint64_t rflags;
|
2018-03-07 18:03:22 +00:00
|
|
|
uint64_t intr_status;
|
2013-12-07 22:18:36 +00:00
|
|
|
} hlt;
|
2014-01-25 20:58:05 +00:00
|
|
|
struct {
|
|
|
|
int vector;
|
|
|
|
} ioapic_eoi;
|
2014-04-28 22:06:40 +00:00
|
|
|
struct {
|
|
|
|
enum vm_suspend_how how;
|
|
|
|
} suspended;
|
2014-07-16 21:26:26 +00:00
|
|
|
struct vm_task_switch task_switch;
|
2011-05-13 04:54:01 +00:00
|
|
|
} u;
|
|
|
|
};
|
|
|
|
|
2014-07-24 01:38:11 +00:00
|
|
|
/* APIs to inject faults into the guest */
|
|
|
|
void vm_inject_fault(void *vm, int vcpuid, int vector, int errcode_valid,
|
|
|
|
int errcode);
|
|
|
|
|
2014-08-24 02:07:34 +00:00
|
|
|
static __inline void
|
2014-07-24 01:38:11 +00:00
|
|
|
vm_inject_ud(void *vm, int vcpuid)
|
|
|
|
{
|
|
|
|
vm_inject_fault(vm, vcpuid, IDT_UD, 0, 0);
|
|
|
|
}
|
|
|
|
|
2014-08-24 02:07:34 +00:00
|
|
|
static __inline void
|
2014-07-24 01:38:11 +00:00
|
|
|
vm_inject_gp(void *vm, int vcpuid)
|
|
|
|
{
|
|
|
|
vm_inject_fault(vm, vcpuid, IDT_GP, 1, 0);
|
|
|
|
}
|
|
|
|
|
2014-08-24 02:07:34 +00:00
|
|
|
static __inline void
|
2014-07-24 01:38:11 +00:00
|
|
|
vm_inject_ac(void *vm, int vcpuid, int errcode)
|
|
|
|
{
|
|
|
|
vm_inject_fault(vm, vcpuid, IDT_AC, 1, errcode);
|
|
|
|
}
|
|
|
|
|
2014-08-24 02:07:34 +00:00
|
|
|
static __inline void
|
2014-07-24 01:38:11 +00:00
|
|
|
vm_inject_ss(void *vm, int vcpuid, int errcode)
|
|
|
|
{
|
|
|
|
vm_inject_fault(vm, vcpuid, IDT_SS, 1, errcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void vm_inject_pf(void *vm, int vcpuid, int error_code, uint64_t cr2);
|
|
|
|
|
2015-01-13 22:00:47 +00:00
|
|
|
int vm_restart_instruction(void *vm, int vcpuid);
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
#endif /* _VMM_H_ */
|