Matthew Dillon [Fri, 23 Jul 2021 04:44:00 +0000 (21:44 -0700)]
kernel - Rename vm_map_wire() and vm_map_unwire()
* These names are mutant throwbacks to an earlier age and no
longer mean what is implied.
* Rename vm_map_wire() to vm_map_kernel_wiring(). This function can
wire and unwire VM ranges in a vm_map under kernel control. Userland
has no say.
* Rename vm_map_unwire() to vm_map_user_wiring(). This function can
wire and unwire VM ranges in a vm_map under user control. Userland
can adjust the user wiring state for pages.
Matthew Dillon [Fri, 23 Jul 2021 04:34:03 +0000 (21:34 -0700)]
kernel - Correct flags argument to vm_map_wire() (no operational change)
* vm/vm_unix.c was making two calls to vm_map_wire() passing FALSE for
kmflags. It needs to pass 0 here, not FALSE. Same value, less
confusion.
Reported-by: aly
Tomohiro Kusumi [Thu, 22 Jul 2021 17:09:34 +0000 (02:09 +0900)]
sys/vfs/hammer2: Fix bulkfree message/comments on old ondisk format
in addition to
b0ac2d2999bc7ac111dbb5ffbc0f7ffbb4c92988.
The bulkfree descriptions written in 2015 or before were not uptodate
with the current ondisk spec.
freemap leaf block: 64KB -> 32KB (out of 64KB)
freemap level0: 2MB -> 4MB
freemap level1: 2GB -> 1GB
Tomohiro Kusumi [Wed, 21 Jul 2021 17:54:33 +0000 (02:54 +0900)]
sys/vfs/hammer2: Fix "2MB segs cleaned" message to 4MB
HAMMER2_FREEMAP_LEVEL0_RADIX was 21 (now 22) when this message
first appeared in
d0fcd3ec4a1cbbfa32581cf24ecd6f3509f1f57c in 2015.
Tomohiro Kusumi [Wed, 21 Jul 2021 16:03:28 +0000 (01:03 +0900)]
sys/vfs/hammer2: Rename hammer2_bulk_scan() to hammer2_bulkfree_scan()
Sync with function name in kprintf messages.
This was also the only function without "free" prefix.
Tomohiro Kusumi [Wed, 21 Jul 2021 15:22:00 +0000 (00:22 +0900)]
sys/vfs/hammer2: Remove unused hammer2_bulkfree_info::saved_mirror_tid
No longer used since
c8c0a18a66946d3ec5fd24de73f593da9c4af6ba in 2017.
Tomohiro Kusumi [Wed, 21 Jul 2021 15:04:10 +0000 (00:04 +0900)]
sys/vfs/hammer2: Remove unused hammer2_chain_save::pri
Never used since first appeared in
125966e80c1aba734d3d5f12a8fcfde2bbcdb018 in 2015.
Aaron LI [Wed, 21 Jul 2021 06:53:38 +0000 (14:53 +0800)]
nvmm: Fix building nvmm into kernel
* Remove 'DIAGNOSTIC' define from 'nvmm_os.h'. First, 'DIAGNOSTIC' and
'INVARIANTS' are different options. Secondly, 'DIAGNOSTIC' should be
defined in kernel configs, e.g., LINT64 defines it.
* Remove 'LOCORE' define from 'nvmm_x86_{svmfunc,vmxfunc}.S'. It's
already defined on the command line, when compiling ASM source files
with the kernel (i.e., building the module into the kernel).
* Manually define 'LOCORE' for 'nvmm_x86_{svmfunc,vmxfunc}.o'
compilation in the Makefile, fixing building as a standalone module.
This fixes building nvmm into kernel, like LINT64.
Thanks swildner for reporting the issue and helping with the fix.
Aaron LI [Mon, 19 Jul 2021 23:33:33 +0000 (07:33 +0800)]
syslogd(8): Fix klog may be broken into multiple incomplete lines
Fix readklog() to not emit partial lines by changing it to use a static
buffer for reading log lines from /dev/klog. Thus we can make it
only emit the line for printing if there is a complete line.
This fixes the issue that a kernel log line may be broken into multiple
incomplete lines that are written into log file by syslogd(8),
especially when the system is under load.
Patch-by: Matt Dillon
Aaron LI [Tue, 20 Jul 2021 11:47:38 +0000 (19:47 +0800)]
nvmm: Fix comm page unmapping issue by doing it on userland side
Previously, we're mapping and unmapping the comm page for userland
process both on the kernel side. The unmapping was called at the point
of closing the fd.
However, the kernel may destroy process' vmspace before or after closing
the fd, so there is no guarantee that kernel can get the correct address
space to unmap the comm page for the userland process. Therefore, do
the comm page unmapping on the userland side in libnvmm to fix the above
issue.
In addition, add the 'comm_size' member to 'struct nvmm_capability' to
expose the comm page size to userland, which is required for the
unmapping operation. This breaks ABI, so bump NVMM_KERN_VERSION.
Update nvmmctl(8) accordingly to print the comm page size.
This commit partially reverts:
"nvmm: Add missing comm page unmapping"
Aaron LI [Sun, 18 Jul 2021 12:03:57 +0000 (20:03 +0800)]
nvmm: Add back NetBSD bits
Bring back the NetBSD bits lost during the porting. One minor change is
required: MODULE_CLASS_MISC -> MODULE_CLASS_DRIVER. Otherwise, loading
nvmm.kmod would fail with error:
module error: incompatible module class 1 for `nvmm' (wanted 3)
Now the same code builds and works on NetBSD (of course, NetBSD requires
a different set of Makefiles).
Aaron LI [Sun, 18 Jul 2021 10:58:01 +0000 (18:58 +0800)]
nvmm: Move DragonFly module code in nvmm.c to nvmm_dragonfly.c
Move the OS-specific DragonFly module code in nvmm.c to nvmm_dragonfly.c,
making nvmm.c source file more OS-independent. Adjust several static
functions and variables for this purpose.
Aaron LI [Sun, 18 Jul 2021 10:29:45 +0000 (18:29 +0800)]
nvmm: Reorganize OS-specific code in nvmm_os.c
Split the OS-specific code in 'nvmm_os.c' into two separate source
files: nvmm_dragonfly.c and nvmm_netbsd.c, one for each supported OS.
Update Makefile and sys/conf/files accordingly.
Aaron LI [Sun, 18 Jul 2021 08:32:04 +0000 (16:32 +0800)]
nvmm: Optimize DB regs save & restore
Check PCB_DBREGS flag before save/restore DB regs. If the current
thread doesn't use DB regs, then no need to save & restore them.
Meanwhile, revert to use x86_dbregs_save() and x86_dbregs_restore()
for the NetBSD code.
This is a partial revert of commit:
"nvmm: Save host DB regs in per-cpu structure"
Matthew Dillon [Fri, 16 Jul 2021 03:36:34 +0000 (20:36 -0700)]
kernel - Improve invltlb latency warnings
* Improve kprintf()s for smp_invltlb latency warnings. Make
it abundantly clear that these are mostly WARNING messages,
not fatal messages.
* Tested on VM with host under load and VM running nice +5.
Matthew Dillon [Fri, 16 Jul 2021 02:55:08 +0000 (19:55 -0700)]
kernel - Make most IPI related globals __read_mostly
* Most of these globals should be made __read_mostly, so make
them __read_mostly.
Matthew Dillon [Fri, 16 Jul 2021 02:53:36 +0000 (19:53 -0700)]
kernel - Use higher invltlb synchronization timeout if guest
* Increase the invltlb synchronization timeout from 10 seconds
to 60 seconds if running as a guest. Just in case the host
is heavily paging the guest, 10 seconds might not be enough.
Of course, performance will be absolutely terrible if broadcast
IPIs take that long to synchronize, but there isn't anything
the guest can do about it.
Matthew Dillon [Fri, 16 Jul 2021 02:27:35 +0000 (19:27 -0700)]
kernel - Remove 'td %p blah: timeout race' kprintf
* Remove debugging kprintf() used to verify that the race can
happen. Yes, it can, particularly on a guest VM (but it can
happen on a host too).
Matthew Dillon [Thu, 15 Jul 2021 21:02:21 +0000 (14:02 -0700)]
kernel - Make kprintf / kputchar more robust
* kprintf() via kputchar() can deadlock due to kprintf()s made from
Xinvltlb (which ignores critical sections). However, we still want
to be able to dump diagnostic information from Xinvltlb!
* Adjust sccnputc() to use a non-blocking syscons lock.
* Adjust kputchar() to not issue a wakeup() on the console support
thread if called with mycpu->gd_intr_nesting_level != 0. To
compensate, the support thread now checks at a minimum rate of
once per second.
* Adjust kvcprintf() to always spin_trylock(), which means that
lines will be lost if multiple cpu's compete for the console
(but all lines will still wind up in the dmesg buffer). This
needs to be done to allow kprintf()s from Xinvltlb (which ignore
critical sections) to not deadlock the machine.
Matthew Dillon [Thu, 15 Jul 2021 21:00:09 +0000 (14:00 -0700)]
nvmm - Use a critical section around the vmrun again
* In DragonFly, a thread in kernel mode will not be preemptively migrated
to another CPU or preemptively switched to another normal kernel thread,
but can be preemptively switched to an interrupt thread (which switches
back to the kernel thread it preempted the instant it is done or blocks).
However, we still need to use a critical section to prevent this nominal
interrupt thread preemption to avoid exposing interrupt threads to
guest DB and FP register state. We operate under the assumption that
the hard interrupt code won't mess with this state.
Aaron LI [Mon, 12 Jul 2021 14:37:55 +0000 (22:37 +0800)]
nvmm: Don't assume comm page size to be PAGE_SIZE
Don't assume the comm page size to be PAGE_SIZE but add the
'NVMM_COMM_PAGE_SIZE' macro to determine the size. So the code is
now independent to comm page size and thus it can be easily extended
in the future.
Aaron LI [Mon, 12 Jul 2021 14:36:54 +0000 (22:36 +0800)]
nvmm: Add missing comm page unmapping
The comm page mapping has been changed from libnvmm to the kernel side,
so I removed the munmap() code from libnvmm, but the kernel wasn't doing
the unmapping. Now add the missing unmapping operation.
Aaron LI [Sun, 11 Jul 2021 14:47:23 +0000 (22:47 +0800)]
nvmm: Adjust EPT/NPT transform code for NetBSD
* Rename variable 'pmap_ept_has_ad' to 'vmx_ept_has_ad' to avoid
conflict with the global variable in NetBSD.
* Set 'pmap_ept_has_ad' global for NetBSD in vmx_ident().
* Add macro os_vmspace_pmap() to 'nvmm_os.h'.
* Add ifdef's in vmx_machine_create() and svm_machine_create() to
properly handle EPT/NPT pmap transform on both DragonFly and NetBSD.
Aaron LI [Sun, 11 Jul 2021 14:44:28 +0000 (22:44 +0800)]
x86_64/cpu.h: Rename nvmm_break_wanted() to hvm_break_wanted()
To avoid confusion with the NVMM code. In addition, this macro checks
against RQF_HVM_MASK, so naming it hvm_break_wanted() seems more
appropriate.
Aaron LI [Sun, 11 Jul 2021 08:08:23 +0000 (16:08 +0800)]
nvmm: Revert to use os_curcpu() in vmx_vmcs_enter()
os_curcpu() returns a pointer to the current per-cpu data, which is used
in vmx_vmcs_enter() to clear a VMCS from a remote CPU. So it's more
efficient and clean than using the os_curcpu_number(), which requires
another lookup to obtain the per-cpu data pointer from the CPU id.
This effectively reverts the commit:
nvmm: Fix issues of porting 'curcpu()' as 'mycpu'
The "VMX fail valid" panics I had then were actually caused by the
missing wait for the asynchronous IPI request to clear VMCS from a
remote CPU, which had been fixed in a later commit.
Aaron LI [Sun, 11 Jul 2021 06:05:26 +0000 (14:05 +0800)]
nvmm: Implement os_ipi_broadcast() for _change_cpu()
os_ipi_broadcast() is used in {svm,vmx}_init() and {svm,vmx}_fini() to
enable/disable SVM/VMX on all CPUs. The previous implementation used an
interlock and made significant changes to the code. Reimplement it in
another way (loop on each CPU and execute the task), so it can be easily
enclosed in the os_ipi_broadcast() function.
The new implementation referred to our pc64/vmm/vmx.c code.
Aaron LI [Sun, 11 Jul 2021 05:14:20 +0000 (13:14 +0800)]
nvmm: Various minor style tweaks
Aaron LI [Sun, 11 Jul 2021 05:10:45 +0000 (13:10 +0800)]
nvmm: Add volatile and memory barrier to clts inline ASM
The clts inline ASM should have a memory barrier. Also add 'volatile'
to it. Now it fully matches the one in NetBSD FPU code.
Matthew Dillon [Sat, 10 Jul 2021 19:12:35 +0000 (12:12 -0700)]
nvmm - Fix overflow tests
* Overflow tests require special case to ensure that compiler
optimizations do not remove the test.
* Plus general cleanup.
Matthew Dillon [Sat, 10 Jul 2021 18:39:00 +0000 (11:39 -0700)]
nvmm - Change NVMM_MAX_RAM
* Add __NetBSD__ conditional back in with original 128GB limit.
* Set DragonFly limit to 127TB (down from 128TB) to ensure that
no valid memory exists at the canonical transition.
Additional limits may have to be set as Intel specifies that the
backing memory map not have an address space larger than the
hardware's physical address space.
Matthew Dillon [Sat, 10 Jul 2021 18:32:30 +0000 (11:32 -0700)]
nvmm - Improve cast portability of vmx_resume_rip
* Cast pointer to function through uintptr_t for maximum portability
(avoids warnings on some compilers).
Matthew Dillon [Sat, 10 Jul 2021 18:29:15 +0000 (11:29 -0700)]
nvmm - Move M_NVMM definition to nvmm_os.c
Aaron LI [Fri, 9 Jul 2021 04:50:33 +0000 (12:50 +0800)]
nvmm: Save host DB regs in per-cpu structure
* The host DB regs save/restore operations are performed outside the
hard interrupt disablement. If the DB regs were saved in curthread's
PCB, an interrupt thread running just after hard interrupts are
enabled again could overwrite the DB regs in the PCB with the guest DB
regs. Saving the host DB regs in the per-cpu structure avoid such
issues.
* This change also makes the code a bit more OS-independent.
Credit-to: Matt Dillon
Referred-to: FreeBSD bhyve
Matthew Dillon [Tue, 6 Jul 2021 06:22:22 +0000 (23:22 -0700)]
nvmm - Fix event commit logic
* Because DragonFly needs to do some tests after hard interrupt
disablement, possibly aborting a VM entry before the VM is actually
run. Certain assumptions made by QEMU related to event injection
might not hold. Two situations can cause events to be lost.
(1) QEMU overwrites its own event after assuming the event would be
injected when it might not be.
(2) Or nvmm overwrites an event for the same reason.
* To fix this we avoid trying to inject the event unless we are
absolutely sure that we will issue the VM entry instruction, and we
force the system call (the ioctl() actually) to restart with ERESTART
to ensure that QEMU doesn't see the ioctl() return until the event is
actually injected.
* The tests DragonFly needs to do relate to ASTs and SWIs that might be
queued via an IPI prior to VM entry. We absolutely do not want to run
the VM if any such flags are pending as it might cause other unrelated
interactive processes to stall out for too long a period of time, or
allow a signal delivery to the QEMU process to race the VM entry.
Matthew Dillon [Thu, 1 Jul 2021 16:32:51 +0000 (09:32 -0700)]
kernel - Expand GDT table to maximum size
* Expand the GDT table from 9 entries to 65536 entries
(limit field 0xFFFF).
* This deals with an Intel quirk in VMX where the descriptor for the
GDT limit field is not restored on a VM exit, but instead
unconditionally set to 0xFFFF.
Aaron LI [Sun, 4 Jul 2021 13:40:14 +0000 (21:40 +0800)]
NVMM: Update examples and libnvmm tests
Aaron LI [Sun, 4 Jul 2021 13:31:40 +0000 (21:31 +0800)]
NVMM: Update libnvmm.3 and TODO.nvmm
Aaron LI [Sun, 4 Jul 2021 01:32:23 +0000 (09:32 +0800)]
nvmmctl(8): Misc cleanups
Aaron LI [Sun, 4 Jul 2021 03:56:40 +0000 (11:56 +0800)]
libnvmm: More OS independence
* Define PTE_* values in 'libnvmm_x86.c' to avoid including some OS
headers: <machine/pte.h>, <machine/vmparam.h> and <machine/pmap.h>.
* Redefine __BIT() macro to avoid handling its difference between NetBSD
and DragonFly.
* 'nvmm_compat.h' is no longer needed and thus removed.
Aaron LI [Sun, 4 Jul 2021 01:27:40 +0000 (09:27 +0800)]
libnvmm: Misc cleanups
* Remove unused arguments.
* Use literal numbers instead of __BITS().
* Improve two printf()s.
* Remove NetBSD VCS markers and update copyright.
Aaron LI [Sun, 4 Jul 2021 12:21:09 +0000 (20:21 +0800)]
nvmm: More OS independence
* Abstract out various OS-specific defines and functions and organize
them in 'nvmm_os.h' and 'nvmm_os.c'. This provides more flexibility
for us to port NVMM to DragonFly and other OSes.
* Remove 'nvmm_compat.h' and merge the code into the above new files.
Some local changes specific to DragonFly are not abstracted out, and
there also exists some NetBSD-specific code. The current version only
builds and works on DragonFly.
The OS-dependent code in nvmm_os.* and nvmm_x86.h is a bit messy at the
moment. I'll further clean them up later.
Aaron LI [Sun, 4 Jul 2021 00:56:02 +0000 (08:56 +0800)]
nvmm: Various misc cleanups and minor updates
* Rework header inclusions.
* Improve the arguments given to uvmspace_alloc().
* Lowercase "NVMM" in printf() logs.
* Reorganize a few macros in 'nvmm_x86.h' for clarity.
* Remove NetBSD VCS markers and update copyright.
Aaron LI [Sat, 3 Jul 2021 15:10:56 +0000 (23:10 +0800)]
Revert "libnvmm: Fix mmap() failure with 'permission denied'"
Because libnvmm no longer calls mmap() to map the VCPU comm page, revert
to the original code to distinguish root owner (open '/dev/nvmm' with
O_WRONLY) vs. non-root owner (open with O_RDONLY).
Aaron LI [Sat, 3 Jul 2021 14:59:38 +0000 (22:59 +0800)]
nvmm: Create comm page in nvmm_vcpu_create() rather than via mmap()
Create the VCPU comm page in nvmm_vcpu_create() in kernel, rather than
via mmap() in userland. With this change, the 'mmap' operation support
is no longer needed by the '/dev/nvmm' device.
This change breaks ABI, so bump NVMM_KERN_VERSION accordingly.
No API change.
Aaron LI [Sun, 4 Jul 2021 12:41:42 +0000 (20:41 +0800)]
nvmm: Reduce NVMM_MAX_VCPUS from 256 to 128
Unfortunately not all x86 guests can work with a max value of 256, due
to overflowing uint8_t. Reduce it to 128.
Aaron LI [Sat, 3 Jul 2021 10:03:29 +0000 (18:03 +0800)]
nvmm: Rename a few things for clarity
Aaron LI [Sat, 3 Jul 2021 08:59:33 +0000 (16:59 +0800)]
nvmm: Make FPU state more OS-indenpendent
* Introduce an OS-indenpendent 'nvmm_x64_state_fpu' structure, derived
from NetBSD's current FPU implementation.
* Also introduce the 'nvmm_x86_xsave' structure, containing the FPU area
and the XSAVE header.
* Add the 'nvmm_x86_xsave_size()' that determines the XSAVE area size to
simplify the code.
* Rename gfpu -> gxsave, for clarity.
* Define 'CTASSERT' because 'nvmm.h' and 'nvmm_x86.h' headers will
be used by libnvmm(3), but <sys/cdefs.h> only defines 'CTASSERT' for
kernel.
* Update libnvmm.3 man page accordingly.
Aaron LI [Sat, 3 Jul 2021 08:11:46 +0000 (16:11 +0800)]
nvmm: Rewrite vmx_vmx{on,off}() as inline ASM functions
Aaron LI [Sat, 3 Jul 2021 08:04:02 +0000 (16:04 +0800)]
nvmm: Make svm_vmrun() void
Aaron LI [Sat, 3 Jul 2021 08:00:28 +0000 (16:00 +0800)]
nvmm: Add SVM CET definitions
Not actually used. For completeness.
Aaron LI [Sat, 3 Jul 2021 07:14:23 +0000 (15:14 +0800)]
nvmm: Redefine CPUID values to be OS-indenpendent
Redefine all CPUID values locally to be OS-indenpendent.
Remove those compat CPUID defines from nvmm_compat.h, no longer needed.
Aaron LI [Sat, 3 Jul 2021 06:30:35 +0000 (14:30 +0800)]
nvmm: Improve CPUID emulation #5: handle Fn0000_0001:EBX[23:16]
Handle CPUID Fn0000_0001:EBX[23:16] to report the logical CPU count.
Aaron LI [Sat, 3 Jul 2021 06:29:15 +0000 (14:29 +0800)]
nvmm: Improve CPUID emulation #4: handle Fn0000_0004 on Intel
Handle CPUID Fn0000_0004 (Deterministic Cache Parameters) on Intel CPUs.
Aaron LI [Sat, 3 Jul 2021 05:07:42 +0000 (13:07 +0800)]
nvmm: Improve CPUID emulation #3: handle Fn8000_0008:ECX on AMD
Properly handle Fn8000_0008:ECX on AMD CPUs to report correct CPU count
info. Similar to Fn0000_000B:ECX on Intel CPUs.
Aaron LI [Sat, 3 Jul 2021 04:04:35 +0000 (12:04 +0800)]
nvmm: Improve CPUID emulation #2: mask upper bits of guest EAX/ECX
Use uint32_t instead of uint64_t for guest EAX/ECX and mask the upper
bits, to prevent wrong results if the upper bits happen to contain
garbage. Not encountered in the wild so far, but could happen.
Aaron LI [Sat, 3 Jul 2021 03:51:53 +0000 (11:51 +0800)]
nvmm: Improve CPUID emulation #1: flags
* Mask PQE (Platform Quality of Service Enforcement); shouldn't be
exposed.
* Add LA57, for completeness.
* Add more flags in Fn8000_0001:EDX, for AMD CPUs.
Aaron LI [Sat, 3 Jul 2021 03:16:33 +0000 (11:16 +0800)]
nvmm: Clarify state handling
* Make a clear distinction between global host state and per-cpu host
state. The former gets saved in a global structure, while the latter
stays in the per-cpu structure.
* Make the host XCR0 part of the global host state, and stop using
rdxcr() in each world switch because it's unnecessary.
Aaron LI [Sat, 3 Jul 2021 02:03:40 +0000 (10:03 +0800)]
nvmm: Clarify the RESET state
Just use plain values instead of macros.
This also eliminates the PAT* compat code in 'nvmm_compat.h'.
Aaron LI [Sat, 3 Jul 2021 01:50:03 +0000 (09:50 +0800)]
nvmm: Add #CP (control protection exception)
Aaron LI [Sat, 3 Jul 2021 00:29:53 +0000 (08:29 +0800)]
libnvmm: Clarify x86 MOVS emulation
Aaron LI [Wed, 30 Jun 2021 13:57:02 +0000 (21:57 +0800)]
libnvmm: Fix a memory leak in nvmm_machine_create()
Also free the allocated 'pages' when ioctl(NVMM_IOC_MACHINE_CREATE)
fails.
Aaron LI [Tue, 29 Jun 2021 23:29:27 +0000 (07:29 +0800)]
nvmm: Improve FPU support and reduce diff against NetBSD
I was using 'struct savexmm64' to translate NetBSD's 'struct xsave_header'.
This works but isn't good enough, because 'savexmm64' can't deal with
xstate, so I disabled the xstate header related code in the old code.
This commit changes to use 'struct saveymm64' instead. It contains the
XSAVE header and YMM xstate component, allowing us to enable the
originally disabled xstate header code in NVMM.
In addition, define some compat macros to adapt NetBSD's FPU structures
to ours, reducing the NVMM code difference against NetBSD.
Will later work on AVX support in guest VM.
Aaron LI [Tue, 29 Jun 2021 14:33:06 +0000 (22:33 +0800)]
doc: Import nvmm TODO note from NetBSD-current
Aaron LI [Wed, 16 Jun 2021 22:49:59 +0000 (06:49 +0800)]
libnvmm.3: Mention regression tests in FILES section
Aaron LI [Sat, 19 Jun 2021 12:31:52 +0000 (20:31 +0800)]
testcases/libnvmm: Improve makefile to not write in source tree
Both build and dfregress(8) would write output in place in the source
tree, which however may be on a readonly mount via NFS. Improve the
makefile to cpdup the whole directory to /tmp and then do everything
there. (credit to Matt Dillon for the idea)
Aaron LI [Wed, 16 Jun 2021 14:35:26 +0000 (22:35 +0800)]
testcases/libnvmm: Add to dfregress(8) test framework
Rewrite and add makefiles to add these testcases to dfregress(8) test
framework. Add a handy 'make test' target to easily run the tests.
Remove unused ATF test scripts.
Aaron LI [Wed, 16 Jun 2021 14:33:53 +0000 (22:33 +0800)]
testcases/libnvmm: Port to DragonFly
Minor tweaks similar to the porting of libnvmm(3).
Aaron LI [Tue, 15 Jun 2021 23:17:28 +0000 (07:17 +0800)]
Import libnvmm tests from NetBSD-current
Branch: NetBSD-current
Date: 2021-06-25
Path: tests/lib/libnvmm
Aaron LI [Mon, 7 Jun 2021 13:41:21 +0000 (21:41 +0800)]
libnvmm.3: Mention 'calc-vm' and 'demo' test code in FILES
Also sort the items in FILES section.
Aaron LI [Mon, 7 Jun 2021 13:40:58 +0000 (21:40 +0800)]
test/nvmm/demo: Improve progress logs to help test/debug
* Add several more progress logs.
* Reduce accepting trap count to 6, reducing the total test time.
* Update the example output in README.
Aaron LI [Sun, 13 Jun 2021 08:06:05 +0000 (16:06 +0800)]
test/nvmm/demo: Rewrite makefiles to not write in source tree
Rewrite the makefiles so it no longer write in the source tree but
output in the /tmp directory. This is useful for building with a
NFS-exported readonly mount of the source tree (e.g., used by dillon).
Aaron LI [Tue, 8 Jun 2021 06:29:40 +0000 (14:29 +0800)]
test/nvmm/demo: Port 'smallkern' to DragonFly
Aaron LI [Tue, 8 Jun 2021 04:52:46 +0000 (12:52 +0800)]
test/nvmm/demo: Make 'smallkern' more self-contained
Provide local 'asm.h' and 'trap.h' headers (derived from NetBSD),
extract necessary PTE_* and PSL_* defines, making 'smallkern' much more
self-contained, which greatly reduces the needed modifications for
porting it to DragonFly. Moreover, it helps to keep the ported code
working on both operating systems.
Aaron LI [Tue, 8 Jun 2021 04:47:45 +0000 (12:47 +0800)]
test/nvmm/demo: Various cleanups to 'smallkern'
* Remove unused variables, symbols, function prototypes and functions.
* Move function prototypes and 'extern' declarations to header files.
* Add 'static' qualifier for file-local variables.
* Add inclusion guard to header files.
* Various minor adjustments.
Aaron LI [Sun, 6 Jun 2021 04:02:25 +0000 (12:02 +0800)]
test/nvmm/demo: Fix ELF load/mmap issue on DragonFly
The 'smallkern' ELF built on DragonFly has a zero-sized GNU_STACK
segment, which causes mmap() to fail (EINVAL). Add conditionals
in elf_parse() to ignore such a segment (while also check for
unsupported non-LOAD segments).
Now 'toyvirt' correctly loads the 'smallkern' ELF on DragonFly.
In addition, assert in toyvirt_mem_add() that the size must be
greater than zero.
Aaron LI [Tue, 8 Jun 2021 01:34:31 +0000 (09:34 +0800)]
test/nvmm/demo: Port 'toyvirt' to DragonFly
Just some minor subsitutions.
Use 'ifdef's to make it work on both NetBSD and DragonFly.
Aaron LI [Fri, 28 May 2021 15:10:04 +0000 (23:10 +0800)]
test/nvmm/demo: Fix some compilation warnings
Aaron LI [Thu, 27 May 2021 23:33:45 +0000 (07:33 +0800)]
test/nvmm/demo: Update 'toyvirt' to current libnvmm(3)
Various adjustments to the demo code to make it work again with the
current libnvmm(3) API in NetBSD 9.1.
In addition, add one more return check of nvmm_vcpu_configure() and
improve the logging messages a bit. Update the example output in
README accordingly.
Tested on NetBSD 9.1. Porting to DragonFly follows.
Aaron LI [Fri, 28 May 2021 10:39:58 +0000 (18:39 +0800)]
test/nvmm/demo: Update makefiles and README
* Adjust makefiles to be a bit more generic.
* Adjust compiler flags to enable more warnings and debug info.
* Add top-level makefile to ease the build.
* Update README.
Aaron LI [Thu, 27 May 2021 23:28:27 +0000 (07:28 +0800)]
test/nvmm: Add a demo for demonstration of libnvmm(3) API
The demo consists of two components:
* toyvirt: a toy virtualizer, that executes in a VM the 64bit ELF binary
given as argument;
* smallkern: an example of such binary.
Obtained from: https://www.netbsd.org/~maxv/nvmm/nvmm-demo.zip
Aaron LI [Sun, 13 Jun 2021 07:12:44 +0000 (15:12 +0800)]
test/nvmm: Add a Makefile and a test script for 'calc-vm'
We write the built binary in /tmp instead of current directory. This is
useful for building with a NFS-exported readonly mount of the source
tree (e.g., used by dillon).
Also add a test script that runs the 'calc-vm' test program in a loop.
It helped reveal the VMCS remote clear bug.
Aaron LI [Sun, 30 May 2021 23:18:21 +0000 (07:18 +0800)]
test/nvmm: Add progress logs in 'calc-vm'
Help test/debug NVMM/libnvmm.
Aaron LI [Sun, 30 May 2021 00:51:01 +0000 (08:51 +0800)]
test/nvmm: Enhance error checks in 'calc-vm'
Enhance error checks to help test/debug NVMM/libnvmm.
Aaron LI [Thu, 27 May 2021 23:22:52 +0000 (07:22 +0800)]
test/nvmm: Add 'calc-vm' (simple VM-based calculator)
A simple calculator. Creates a VM which performs the addition of the two
ints given as argument.
Obtained from: https://www.netbsd.org/~maxv/nvmm/calc-vm.c
Blog: https://blog.netbsd.org/tnf/entry/from_zero_to_nvmm
Aaron LI [Sun, 13 Jun 2021 14:34:25 +0000 (22:34 +0800)]
Bump __DragonFly_version for adding nvmm(4) and libnvmm(3)
Matthew Dillon [Thu, 24 Jun 2021 00:51:40 +0000 (17:51 -0700)]
nvmm - Fix TSC synchronization issues
* Save the guest TSC offset in cpudata as 'gtsc_offset', replacing the
origin absolute TSC value stored as 'gtsc'.
* QEMU and other emulators probably have no intention of actually
forcing the TSC state in the SETSTATE call, so don't act on it
if it matches the value we previously returned.
This allows the guest to inherit a completely synchronized TSC from
the host. Without it, the TSC's for the VCPUs wind up being badly
out of sync.
* Updating MSR_TSC completely blows up TSC mp synchronization. We
assume QEMU did not intend to update the TSC if it tries to write
0 or tries to write the value returned in the previous getstate.
* This allows kernels to use the TSC as a clock, which costs nothing,
verses the ACPI or HPET which have horrible overhead and a global
mutex in QEMU.
Matthew Dillon [Wed, 23 Jun 2021 06:35:30 +0000 (23:35 -0700)]
nvmm - Change max emulated RAM from 128GB to 128TB
* Increase the max emulated RAM from 128GB to 128TB. Ok, I'm not
sure what the actual maximum is, but it sure as heck is more
than 128GB.
* Successfully booted a 8TB qemu on the threadripper (host ate
~275GB to boot it, mostly initializing the vm_page_array[]).
This points to other things we could work on in the kernel
to reduce memory overhead. Our really fat struct vm_page's,
for one.
Aaron LI [Sun, 27 Jun 2021 05:36:00 +0000 (13:36 +0800)]
nvmm: Revamp host TLB flush mechanism
* Leverage the pmap layer to track guest pmap generation id and the host
CPUs that the guest pmap is active on. This avoids the inefficient
_tlb_flush() callbacks from NVMM that invalidate all TLB entries.
* Currently just add all CPUs to the backing pmap for guest physical
memory as they are encountered. Do not yet try to remove any CPUs,
because multiple vCPUs may wind up (temporarily) scheduled to the same
physical CPU. So more sophisticated tracking is needed.
* Fix a bug in SVM's host TLB flush handling where breaking out of the
loop and returning, then re-entering the loop on the same cpu, could
improperly clear the machine flush request.
Credit to Matt Dillon.
Aaron LI [Fri, 25 Jun 2021 10:30:26 +0000 (18:30 +0800)]
pmap: Add some API routines to help NVMM manage guest memory
Add the following three routines for NVMM to use. NVMM can use these
routines to manipulate the cpumask for the pmap backing guest physical
memory.
* pmap_add_cpu()
* pmap_del_cpu()
* pmap_del_all_cpus()
NOTE: The scheduler might somtimes overload multiple vCPUs on the same
physical cpu, so operating is not quite as simple as calling
add_cpu/del_cpu in the core vmrun routines.
Credit to Matt Dillon
Aaron LI [Fri, 25 Jun 2021 10:08:46 +0000 (18:08 +0800)]
pmap: Change pmap->pm_invgen to uint64_t to be compatible with NVMM
Change the 'pmap->pm_invgen' member from 'long' to 'uint64_t', to be
compatible with NVMM's machgen.
Update the atomic operation on 'pm_invgen' accordingly, and no need to
use the '_acq' acquire version (including a read barrier).
Credit to Matt Dillon.
Matthew Dillon [Tue, 29 Jun 2021 06:04:31 +0000 (23:04 -0700)]
nnvm - Move *_fpu_enter/leave inside the cli/sti
* Move the host-to-guest and guest-to-host FP code inside the
hard interrupt disablement. The main reason this needs to
be done is that DragonFly's normal interrupt mechanism is
allowed to use the FP unit (using npxpush/npxpop).
In addition, interrupts will allow the 'interrupt thread' to
preempt the current kernel thread outside of a critical section.
And inside a critical section the interrupt still fires, but
just sets a flag.
* I don't want the host kernel dealing with guest FP state at all,
under any circumstances.
Aaron LI [Sun, 27 Jun 2021 03:59:13 +0000 (11:59 +0800)]
nvmm: Check for pending host events before VM entry
mycpu->gd_reqflags can accumulate action items (pending host interrupts,
AST (asynchronous software trap), etc.). Even if not in a critical
section, some action items can accumulate. When in a critical section,
even more action items can accumulate. Thus, gd_reqflags MUST be
checked *after* hard interrupt disablement to determine if the VM entry
has to be aborted, making the state safe to VM entry.
Credit to Matt Dillon.
Aaron LI [Sun, 27 Jun 2021 03:54:20 +0000 (11:54 +0800)]
nvmm: Improve nvmm_return_needed() by using nvmm_break_wanted()
Use the newly added nvmm_break_wanted() routine to check for pending
host events, improving nvmm_return_needed(). Just stuff
nvmm_break_wanted() into nvmm_return_needed() and get rid of
preempt_needed(), making the code clearer.
Also add __predict_false() macro to help performance a bit.
Matthew Dillon [Wed, 23 Jun 2021 05:19:33 +0000 (22:19 -0700)]
kernel - Add RQF_XINVLTLB to gd_reqflags
Add RQF_XINVLTLB to gd_reqflags. This bit is set on every CPU related
to a pmap after a pmap_inval*() operation makes an adjustment in that
pmap, as part of the IPI sequence.
Will be used by NVMM.
Aaron LI [Sat, 26 Jun 2021 23:58:06 +0000 (07:58 +0800)]
NVMM: Sync with NetBSD #2: SVM & VMX backends
This commit syncs the NVMM kernel part to match NetBSD current (as of
2021-06-25). The main changes are as follows:
* Improve host FPU handling. The host FPU state is now save in PCB
instead of in vCPU data area.
* Clear TS flag from the host's CR0 in _vcpu_init(), because it is also
cleared inside the _vcpu_run() loop. Not clearing it could trigger
DNAs on VMEXITs.
* Set VMCS_HOST_IDTR_BASE on each CPU independently, because the IDT is
now per-CPU (in NetBSD).
NOTE: DragonFly is also using per-CPU IDT, so this change fixes a
porting issue.
* Disable interrupts earlier to prevent possible race against TLB flush
IPIs, because such IPIs don't respect the IPL, so enforcing IPL_HIGH
has no effect.
* VMX: Improve CR0 handling:
- Flush the guest TLB when certain CR0 bits change.
- Employ VMCS_CR0_SHADOW to allow the guest to update certain static
CR0 bits. Guest gets the illusion that the CR0 change was applied,
but the "real" CR0 bits remain unchanged.
- Force CR0_ET to 1 in shadow CR0; force CR0_ET and CR0_NE in real
CR0.
- Add comments to clarify better.
NOTE:
NetBSD has overhauled the FPU handling, so NVMM no longer needs to save
host FPU state in the _cpudata structure. I haven't found a way to do
this on DragonFly yet, so leave it and investigate it later.
Aaron LI [Sat, 26 Jun 2021 11:14:24 +0000 (19:14 +0800)]
NVMM: Sync with NetBSD #1: copyright headers
Aaron LI [Mon, 14 Jun 2021 23:26:03 +0000 (07:26 +0800)]
nvmm: Fix SVM TSS restore on DragonFly
In DragonFly, PCPU(tss_gdt) points directly to the gdt[] entry for the
current CPU's TSS descriptor; while NetBSD's CPUVAR(GDT) points to the
gdtstore[] table. Fix that 'and' instruction so it works on DragonFly.
(Credit to Matt Dillon for debugging and fixing this.)
The 'and' instruction clears the busy bit (bit 41) so the TSS descriptor
becomes "available" for the reloading, as required by 'ltr' instruction.
(The TSS descriptor was in use prior to launching the guest so it has
been marked busy.)
Credit:
* Illumos: Bug #13029: AMD bhyve should reload TSS ASAP
https://www.illumos.org/issues/13029
* Illumos: 13029 AMD bhyve should reload TSS ASAP
https://github.com/illumos/illumos-gate/commit/
4d3fdeb14779bb6b0838521971d9ac99d65b0572
Aaron LI [Sun, 13 Jun 2021 06:25:39 +0000 (14:25 +0800)]
nvmm: Implement waits for lwkt_send_ipiq_mask()
Unlike lwkt_send_ipiq(), lwkt_send_ipiq_mask() doesn't have a sequence
number to wait for completion, and a wait mechanism like that would be
very expensive.
Here we choose a simple method. Just have {vmx,svm}_change_cpu()
decrement a global with an atomic op and issue a wakeup() when it hits
0. And the callers can just tsleep in a loop until its zero
Credit to Matt Dillon for the patch.
Aaron LI [Sun, 13 Jun 2021 06:16:59 +0000 (14:16 +0800)]
nvmm: Fix VMX VMCS remote clear issues
When clearing a VMCS from a remote CPU, must wait for the IPI to
complete. Otherwise the VMCS may be wrong when the thread migrates to
another CPU and thus cause panics when executing VMX instructions.
Credit to Matt Dillon for the debugging and fix.
Aaron LI [Sun, 13 Jun 2021 07:00:51 +0000 (15:00 +0800)]
nvmm: Fix issues of porting 'curcpu()' as 'mycpu'
In NVMM porting step #10, I ported NetBSD's 'curcpu()' as our 'mycpu'.
This was incorrect, because the 'struct globaldata *' pointer returned
by 'mycpu' is NOT stable and can change. (see the comments in
'pc64/include/thread.h')
Use 'mycpuid' to implement 'curcpu()' and adjust the code accordingly.