Commit Graph

8924 Commits

Author SHA1 Message Date
John Baldwin
c0f35dbf19 vmm: Use a cpuset_t for vCPUs waiting for STARTUP IPIs.
Retire the boot_state member of struct vlapic and instead use a cpuset
in the VM to track vCPUs waiting for STARTUP IPIs.  INIT IPIs add
vCPUs to this set, and STARTUP IPIs remove vCPUs from the set.
STARTUP IPIs are only reported to userland for vCPUs that were removed
from the set.

In particular, this permits a subsequent change to allocate vCPUs on
demand when the vCPU may not be allocated until after a STARTUP IPI is
reported to userland.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37173
2022-11-18 10:25:38 -08:00
John Baldwin
223de44c93 vmm devmem_mmap_single: Bump object reference under memsegs lock.
Reported by:	markj
Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37273
2022-11-18 10:25:38 -08:00
John Baldwin
67b69e76e8 vmm: Use an sx lock to protect the memory map.
Previously bhyve obtained a "read lock" on the memory map for ioctls
needing to read the map by locking the last vCPU.  This is now
replaced by a new per-VM sx lock.  Modifying the map requires
exclusively locking the sx lock as well as locking all existing vCPUs.
Reading the map requires either locking one vCPU or the sx lock.

This permits safely modifying or querying the memory map while some
vCPUs do not exist which will be true in a future commit.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37172
2022-11-18 10:25:38 -08:00
John Baldwin
08ebb36076 vmm: Destroy mutexes.
Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37171
2022-11-18 10:25:38 -08:00
John Baldwin
d5118d0fc4 vmm stat: Add a special nelems constant for arrays sized by vCPU count.
Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37170
2022-11-18 10:25:38 -08:00
John Baldwin
58eefc67a1 vmm vmx: Allocate vpids on demand as each vCPU is initialized.
Compared to the previous version this does mean that if the system as
a whole runs out of dedicated vPIDs you might end up with some vCPUs
within a single VM using dedicated vPIDs and others using shared
vPIDs, but this should not break anything.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37169
2022-11-18 10:25:38 -08:00
John Baldwin
3f0f4b1598 vmm: Lookup vcpu pointers in vmmdev_ioctl.
Centralize mapping vCPU IDs to struct vcpu objects in vmmdev_ioctl and
pass vcpu pointers to the routines in vmm.c.  For operations that want
to perform an action on all vCPUs or on a single vCPU, pass pointers
to both the VM and the vCPU using a NULL vCPU pointer to request
global actions.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37168
2022-11-18 10:25:38 -08:00
John Baldwin
0cbc39d53d vmm ppt: Remove unused vcpu arg from MSI setup handlers.
Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37167
2022-11-18 10:25:37 -08:00
John Baldwin
e42c24d56b vmm: Remove unused vcpuid argument from vioapic_process_eoi.
Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37166
2022-11-18 10:25:37 -08:00
John Baldwin
d8be3d523d vmm: Use struct vcpu in the rendezvous code.
Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37165
2022-11-18 10:25:37 -08:00
John Baldwin
949f0f47a4 vmm: Remove support for vm_rendezvous with a cpuid of -1.
This is not currently used.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37164
2022-11-18 10:25:37 -08:00
John Baldwin
9388bc1e3a vmm: Remove vcpuid from I/O port handlers.
No I/O ports are vCPU-specific (unlike memory which does have
vCPU-specific ranges such as the local APIC).

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37163
2022-11-18 10:25:37 -08:00
John Baldwin
80cb5d845b vmm: Pass vcpu instead of vm and vcpuid to APIs used from CPU backends.
Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37162
2022-11-18 10:25:37 -08:00
John Baldwin
d3956e4673 vmm: Use struct vcpu in the instruction emulation code.
This passes struct vcpu down in place of struct vm and and integer
vcpu index through the in-kernel instruction emulation code.  To
minimize userland disruption, helper macros are used for the vCPU
arguments passed into and through the shared instruction emulation
code.

A few other APIs used by the instruction emulation code have also been
updated to accept struct vcpu in the kernel including
vm_get/set_register and vm_inject_fault.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37161
2022-11-18 10:25:37 -08:00
John Baldwin
28b561ad9d vmm: Add vm_gpa_hold_global wrapper function.
This handles the case that guest pages are being held not on behalf of
a virtual CPU but globally.  Previously this was handled by passing a
vcpuid of -1 to vm_gpa_hold, but that will not work in the future when
vm_gpa_hold is changed to accept a struct vcpu pointer.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37160
2022-11-18 10:25:36 -08:00
John Baldwin
0f435e6476 vmm: Add _KERNEL guards for io headers shared with userspace.
Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37159
2022-11-18 10:25:36 -08:00
John Baldwin
2b4fe856f4 bhyve: Remove unused vm and vcpu arguments from vm_copy routines.
The arguments identifying the VM and vCPU are only needed for
vm_copy_setup.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37158
2022-11-18 10:25:36 -08:00
John Baldwin
3dc3d32ad6 vmm: Use struct vcpu with the vmm_stat API.
The function callbacks still use struct vm and and vCPU index.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37157
2022-11-18 10:25:36 -08:00
John Baldwin
950af9ffc6 vmm: Expose struct vcpu as an opaque type.
Pass a pointer to the current struct vcpu to the vcpu_init callback
and save this pointer in the CPU-specific vcpu structures.

Add routines to fetch a struct vcpu by index from a VM and to query
the VM and vcpuid from a struct vcpu.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37156
2022-11-18 10:25:36 -08:00
John Baldwin
d030f941e6 vmm: Use VLAPIC_CTR* in more places.
Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37155
2022-11-18 10:25:36 -08:00
John Baldwin
57e0119ef3 vmm vmx: Add VMX_CTR* wrapper macros.
These macros are similar to VCPU_CTR* but accept a single vmx_vcpu
pointer as the first argument instead of separate vm and vcpuid.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37154
2022-11-18 10:25:36 -08:00
John Baldwin
fca494dad0 vmm svm: Add SVM_CTR* wrapper macros.
These macros are similar to VCPU_CTR* but accept a single svm_vcpu
pointer as the first argument instead of separate vm and vcpuid.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37153
2022-11-18 10:25:36 -08:00
John Baldwin
869c8d1946 vmm: Remove the per-vm cookie argument from vmmops taking a vcpu.
This requires storing a reference to the per-vm cookie in the
CPU-specific vCPU structure.  Take advantage of this new field to
remove no-longer-needed function arguments in the CPU-specific
backends.  In particular, stop passing the per-vm cookie to functions
that either don't use it or only use it for KTR traces.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37152
2022-11-18 10:25:35 -08:00
John Baldwin
1aa5150479 vmm: Refactor storage of CPU-dependent per-vCPU data.
Rather than storing static arrays of per-vCPU data in the CPU-specific
per-VM structure, adopt a more dynamic model similar to that used to
manage CPU-specific per-VM data.

That is, add new vmmops methods to init and cleanup a single vCPU.
The init method returns a pointer that is stored in 'struct vcpu' as a
cookie pointer.  This cookie pointer is now passed to other vmmops
callbacks in place of the integer index.  The index is now only used
in KTR traces and when calling back into the CPU-independent layer.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37151
2022-11-18 10:25:35 -08:00
John Baldwin
73abae4493 vmm vmx: Add a global bool to indicate if the host has the TSC_AUX MSR.
A future commit will remove direct access to vCPU structures from
struct vmx, so add a dedicated boolean for this rather than checking
the capabilities for vCPU 0.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37269
2022-11-18 10:25:35 -08:00
John Baldwin
39ec056e6d vmm: Rework snapshotting of CPU-specific per-vCPU data.
Previously some per-vCPU state was saved in vmmops_snapshot and other
state was saved in vmmops_vcmx_snapshot.  Consolidate all per-vCPU
state into the latter routine and rename the hook to the more generic
'vcpu_snapshot'.  Note that the CPU-independent per-vCPU data is still
stored in a separate blob as well as the per-vCPU local APIC data.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37150
2022-11-18 10:25:35 -08:00
John Baldwin
19b9dd2e08 vmm svm: Mark all VMCB state caches dirty on vCPU restore.
Mark Johnston noticed that this was missing VMCB_CACHE_LBR.  Just set
all the bits as is done in svm_run() rather than trying to clear
individual bits.

Reported by:	markj
Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37259
2022-11-18 10:25:35 -08:00
John Baldwin
0f00260c67 vmm vmx: Refactor per-vCPU data.
Add a struct vmx_vcpu to hold per-vCPU data specific to VT-x and
move parallel arrays out of struct vmx into a single array of
this structure.

While here, dynamically allocate the VMCS, APIC page and PIR
descriptors for each vCPU rather than embedding them.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37149
2022-11-18 10:25:35 -08:00
John Baldwin
215d2fd53f vmm svm: Refactor per-vCPU data.
- Allocate VMCBs separately to avoid excessive padding in struct
  svm_vcpu.

- Allocate APIC pages dynamically directly in struct vlapic.

- Move vm_mtrr into struct svm_vcpu rather than using a separate
  parallel array.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37148
2022-11-18 10:25:35 -08:00
John Baldwin
35abc6c238 vmm: Use vm_get_maxcpus() instead of VM_MAXCPU in various places.
Mostly these are loops that iterate over all possible vCPU IDs for a
specific virtual machine.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37147
2022-11-18 10:25:34 -08:00
John Baldwin
a7db532e3a vmm: Simplify saving of absolute TSC values in snapshots.
Read the current "now" TSC value and use it to compute absolute time
saved value in vm_snapshot_vcpus rather than iterating over vCPUs
multiple times in vm_snapshot_vm.

Reviewed by:	corvink, markj
Differential Revision:	https://reviews.freebsd.org/D37146
2022-11-18 10:25:34 -08:00
Mateusz Guzik
c3f1a13902 Retire broken GPROF support from the kernel
The option is not even recognized and with that patched it does not
compile. Even if it did work, it would be prohibitively expensive to
use.

Interested parties can use pmcstat or dtrace instead.
2022-11-15 14:17:10 +00:00
Mark Johnston
8b1adff8bc bhyve: Drop volatile qualifiers from snapshot code
They accomplish nothing since the qualifier is casted away in calls to
memcpy() and copyin()/copyout().  No functional change intended.

MFC after:	2 weeks
Reviewed by:	corvink, jhb
Differential Revision:	https://reviews.freebsd.org/D37292
2022-11-11 10:02:26 -05:00
Elliott Mitchell
ccd9b49f20 sys: use .S for assembly language files that use the preprocessor
Reviewed by:	imp
Pull Request:	https://github.com/freebsd/freebsd-src/pull/609
Differential Revision: https://reviews.freebsd.org/D35908
2022-11-02 10:29:00 -04:00
Konstantin Belousov
4d447b30f7 vmm: do not leak halted_cpus bit after suspension
Reported by:	bz
PR:	267468
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D37227
2022-11-01 20:44:42 +02:00
Mitchell Horne
aba921bd9e ddb: print the actual syscall name
Some architectures will pretty-print a system call trap in the
backtrace. Rather than printing the symbol, use the syscallname()
function to pull the string from the sv_syscallnames array corresponding
to the process. This simplifies the function somewhat.

Mostly, this will result in dropping the "sys" prefix, e.g. "sys_exit"
will now be printed simply as "exit".

Make two minor tweaks to the function signature: use a u_int for the
syscall number since this is a more correct type (see the 'code' member
of struct syscall_args), and make the thread pointer the first argument.
The latter is more natural and conventional.

Suggested by:   jrtc27
Reviewed by:	jrtc27, markj, jhb
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D37200
2022-10-28 18:21:08 -03:00
Mitchell Horne
1da65dcb1c linux: populate sv_syscallnames in each sysentvec
This allows the syscallname() function to give a usable result for Linux
ABIs.

Reported by:	jrtc27
Reviewed by:	jrtc27, markj, jhb
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D37199
2022-10-28 18:21:08 -03:00
Jung-uk Kim
19ee8335c5 acpica: Merge ACPICA 20221020 2022-10-27 22:04:32 -04:00
John Baldwin
769b884e2e vmm: Fix AP startup with old userspace binaries.
Older binaries that do not request IPI exits to userspace do not
start user threads for other vCPUs until a STARTUP IPI triggers a
VM_EXITCODE_SPINUP_AP exit to userland.  This means that those vcpus
are not yet active (in terms of vm_active_cpus) when the INIT and
STARTUP IPIs are delivered to the vCPUs.

The changes in commit 0bda8d3e9f changed the INIT and STARTUP IPIs
to reuse the existing vlapic_calcdest() function.  This function
silently ignores IPIs sent to inactive vCPUs.  As a result, when using
an old bhyve binary, the INIT and STARTUP IPIs sent to wakeup APs were
ignored.

To fix, restructure the compat code for the INIT and STARTUP IPIs to
ignore the results of vlapic_calcdest() and manually parse the APIC ID
and resulting vcpuid.  As part of this, make the compat code always
conditonal on the ipi_exit capability being disabled.

Reviewed by:	c.koehne_beckhoff.com, markj
Differential Revision:	https://reviews.freebsd.org/D37093
2022-10-26 14:22:56 -07:00
Mark Johnston
ed72168431 bhyve: Address some signed/unsigned comparison warnings
MFC after:	1 week
2022-10-25 11:16:57 -04:00
Konstantin Belousov
934bfc128e Add vm_page_any_valid()
Use it and several other vm_page_*_valid() functions in more places.

Suggested and reviewed by:	markj
Tested by:	pho
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D37024
2022-10-19 20:24:07 +03:00
Colin Percival
469ad86031 amd64: Add FIRECRACKER kernel configuration
This kernel configuration supports the Firecracker VMM environment.

Relnotes:	FreeBSD can now run inside the Firecracker VMM
		via the amd64 FIRECRACKER kernel configuration.
Sponsored by:	https://www.patreon.com/cperciva
Differential Revision:	https://reviews.freebsd.org/D36672
2022-10-17 23:02:22 -07:00
Corvin Köhne
2a2a64c4b9 vmm: validate icr value
Not all combinations of icr values are allowed. Neither Intel nor AMD
document what happens when an invalid value is written to the icr.
Ignore the IPI. So, the guest will note that the IPI wasn't delivered.

Reviewed by:		jhb
Differential Revision:  https://reviews.freebsd.org/D36946
Sponsored by:           Beckhoff Automation GmbH & Co. KG
2022-10-14 12:03:05 +02:00
Corvin Köhne
f56801d6d9 vmm: increase vlapic version
Mac os panics on apic versions lower than 0x14.

See https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/i386/lapic_native.c.auto.html

Additionally, an upcoming commit will validate the icr values written by
the guest. Older intel processors allow some different combinations than
the newer ones. AMD documents that only the newer combinations are
allowed. So, bumping the version allows us to avoid a differentiation
between AMD and Intel.

Intel documents that newer processors than the P6 are using the new
combinations. Sadly, Intel does not document which apic version belongs
to those processors. Linux identifies newer apics by a version larger or
equal to 0x14. Intel and AMD allow apic version between 0x10 and 0x15.
So, using 0x14 seems to be fine.

See 3eba620e7b/arch/x86/kernel/apic/apic.c (L238)

Reviewed by:		jhb
Differential Revision:  https://reviews.freebsd.org/D36945
Sponsored by:           Beckhoff Automation GmbH & Co. KG
2022-10-14 12:03:05 +02:00
Corvin Köhne
0bda8d3e9f vmm: permit some IPIs to be handled by userspace
Add VM_EXITCODE_IPI to permit returning unhandled IPIs to userland.
INIT and STARTUP IPIs are now returned to userland. Due to backward
compatibility reasons, a new capability is added for enabling
VM_EXITCODE_IPI.

Reviewed by:		jhb
Differential Revision:  https://reviews.freebsd.org/D35623
Sponsored by:           Beckhoff Automation GmbH & Co. KG
2022-10-14 12:03:05 +02:00
Konstantin Belousov
e0612ed490 amd64 pmap: add comment explaining why INVLPG is functional for PCID config
Reviewed by:	alc, markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D36919
2022-10-11 00:33:17 +03:00
Konstantin Belousov
273d0715f6 amd64: remove useless addr2 variables in page range invalidation handlers
Reviewed by:	alc, markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D36919
2022-10-11 00:33:12 +03:00
Mark Johnston
98d920d9cf bhyve: Annotate unused function parameters
MFC after:	1 week
2022-10-08 11:33:21 -04:00
John Baldwin
4d90a5afc5 sys: Consolidate common implementation details of PV entries.
Add a <sys/_pv_entry.h> intended for use in <machine/pmap.h> to
define struct pv_entry, pv_chunk, and related macros and inline
functions.

Note that powerpc does not yet use this as while the mmu_radix pmap
in powerpc uses the new scheme (albeit with fewer PV entries in a
chunk than normal due to an used pv_pmap field in struct pv_entry),
the Book-E pmaps for powerpc use the older style PV entries without
chunks (and thus require the pv_pmap field).

Suggested by:	kib
Reviewed by:	kib
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D36685
2022-10-07 10:14:03 -07:00
Mitchell Horne
b05b1ecbef amd64, arm64 pmap: fix a comment typo
There is no such error code.

Fixes:	1d5ebad06c ("pmap: optimize MADV_WILLNEED on existing superpages")
2022-10-06 19:04:54 -03:00