dragonfly.git
2 years agolibnvmm.3: Extend NAME section and add MLINKS for functions.
Sascha Wildner [Thu, 29 Jul 2021 08:06:34 +0000 (10:06 +0200)]
libnvmm.3: Extend NAME section and add MLINKS for functions.

2 years agonvmm: Some documentation adjustments.
Sascha Wildner [Thu, 29 Jul 2021 03:41:43 +0000 (05:41 +0200)]
nvmm: Some documentation adjustments.

* Fix '.Lb libnvmm' for mandoc(1).

* Use standard list in libnvmm.3's ERRORS section.

* Add a SYNOPSIS to nvmm.4.

* Use short form .Nm in nvmmctl.8.

2 years agoRevert "WIP"
Aaron LI [Wed, 28 Jul 2021 23:30:27 +0000 (07:30 +0800)]
Revert "WIP"

This reverts commit 5d4c71cc3f59393bac29819031ed5afcedda6359.

Oops, didn't intend to commit this...

2 years agoWIP
Aaron LI [Wed, 28 Jul 2021 11:08:35 +0000 (19:08 +0800)]
WIP

2 years agotest/nvmm/demo: Clean up header inclusions
Aaron LI [Wed, 28 Jul 2021 11:06:44 +0000 (19:06 +0800)]
test/nvmm/demo: Clean up header inclusions

* Remove unused header inclusions.
* Reorder header inclusions.
* Define MSR_APICBASE, APICBASE_BSP and APICBASE_EN locally to be more
  OS-independent.

2 years agotestcases/libnvmm: Remove unneeded header inclusions
Aaron LI [Wed, 28 Jul 2021 10:44:24 +0000 (18:44 +0800)]
testcases/libnvmm: Remove unneeded header inclusions

They are now provided by the <dev/virtual/nvmm/nvmm.h> header file.

2 years agolibnvmm: Remove unneeded header inclusions and defines
Aaron LI [Wed, 28 Jul 2021 10:41:18 +0000 (18:41 +0800)]
libnvmm: Remove unneeded header inclusions and defines

They are now provided by the <dev/virtual/nvmm/nvmm.h> header file.

2 years agonvmm: Adjust comments about TSC sync and gd_reqflags check
Aaron LI [Wed, 28 Jul 2021 10:37:22 +0000 (18:37 +0800)]
nvmm: Adjust comments about TSC sync and gd_reqflags check

Replace QEMU with a more generic "emulator", and adjust the comments
about TSC sync to be more consistent and concise.

2 years agonvmm: Move DragonFly-specific code to os_vmspace_create()
Aaron LI [Wed, 28 Jul 2021 10:30:22 +0000 (18:30 +0800)]
nvmm: Move DragonFly-specific code to os_vmspace_create()

Move the DragonFly-specific pmap code to os_vmspace_create() in
nvmm_dragonfly.c, making the nvmm.c code more OS-independent.

2 years agonvmmctl(8): Fix installing to a ${DESTDIR} on hosts without the nvmm group.
Sascha Wildner [Tue, 27 Jul 2021 13:55:52 +0000 (15:55 +0200)]
nvmmctl(8): Fix installing to a ${DESTDIR} on hosts without the nvmm group.

This affects building master ISO/IMGs on older hosts, or any other
such scenario involving a ${DESTDIR}. If the host's world is too old,
let install(1) use the databases (master.passwd, group) from the source
tree.

installworlds for the purpose of upgrading the machine without ${DESTDIR}
had the databases upgraded earlier in the install and are not affected.

2 years agoRevert "install(1): Disable -N option in btools."
Sascha Wildner [Tue, 27 Jul 2021 13:49:23 +0000 (15:49 +0200)]
Revert "install(1): Disable -N option in btools."

This reverts commit bf226617061340a3979d19957444da0997124229.

This is too useful to be missing.

2 years agosys/vfs/ext2fs: Fix incorrect VFS_VGET() argument in ext2_fhtovp()
Tomohiro Kusumi [Fri, 23 Jul 2021 16:34:50 +0000 (01:34 +0900)]
sys/vfs/ext2fs: Fix incorrect VFS_VGET() argument in ext2_fhtovp()

A bug introduced when porting from FreeBSD.

2 years agokernel/nvmm: Fix doing just 'make depend' in the module directory.
Sascha Wildner [Fri, 23 Jul 2021 14:49:41 +0000 (16:49 +0200)]
kernel/nvmm: Fix doing just 'make depend' in the module directory.

*_if.h and opt_*.h have to be generated before assym.s.

2 years agokernel - Rename vm_map_wire() and vm_map_unwire() (3)
Aaron LI [Fri, 23 Jul 2021 13:03:36 +0000 (21:03 +0800)]
kernel - Rename vm_map_wire() and vm_map_unwire() (3)

Remove a now obsolete comment.

2 years agokernel - Rename vm_map_wire() and vm_map_unwire() (2)
Matthew Dillon [Fri, 23 Jul 2021 05:18:07 +0000 (22:18 -0700)]
kernel - Rename vm_map_wire() and vm_map_unwire() (2)

* Deconfuse comment

2 years agokernel - Rename vm_map_wire() and vm_map_unwire()
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.

2 years agokernel - Correct flags argument to vm_map_wire() (no operational change)
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
2 years agosys/vfs/hammer2: Fix bulkfree message/comments on old ondisk format
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

2 years agosys/vfs/hammer2: Fix "2MB segs cleaned" message to 4MB
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.

2 years agosys/vfs/hammer2: Rename hammer2_bulk_scan() to hammer2_bulkfree_scan()
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.

2 years agosys/vfs/hammer2: Remove unused hammer2_bulkfree_info::saved_mirror_tid
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.

2 years agosys/vfs/hammer2: Remove unused hammer2_chain_save::pri
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.

2 years agonvmm: Fix building nvmm into kernel
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.

2 years agosyslogd(8): Fix klog may be broken into multiple incomplete lines
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
2 years agonvmm: Fix comm page unmapping issue by doing it on userland side
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"

2 years agonvmm: Add back NetBSD bits
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).

2 years agonvmm: Move DragonFly module code in nvmm.c to nvmm_dragonfly.c
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.

2 years agonvmm: Reorganize OS-specific code in nvmm_os.c
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.

2 years agonvmm: Optimize DB regs save & restore
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"

2 years agokernel - Improve invltlb latency warnings
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.

2 years agokernel - Make most IPI related globals __read_mostly
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.

2 years agokernel - Use higher invltlb synchronization timeout if guest
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.

2 years agokernel - Remove 'td %p blah: timeout race' kprintf
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).

2 years agokernel - Make kprintf / kputchar more robust
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.

2 years agonvmm - Use a critical section around the vmrun again
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.

2 years agonvmm: Don't assume comm page size to be PAGE_SIZE
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.

2 years agonvmm: Add missing comm page unmapping
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.

2 years agonvmm: Adjust EPT/NPT transform code for NetBSD
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.

2 years agox86_64/cpu.h: Rename nvmm_break_wanted() to hvm_break_wanted()
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.

2 years agonvmm: Revert to use os_curcpu() in vmx_vmcs_enter()
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.

2 years agonvmm: Implement os_ipi_broadcast() for _change_cpu()
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.

2 years agonvmm: Various minor style tweaks
Aaron LI [Sun, 11 Jul 2021 05:14:20 +0000 (13:14 +0800)]
nvmm: Various minor style tweaks

2 years agonvmm: Add volatile and memory barrier to clts inline ASM
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.

2 years agonvmm - Fix overflow tests
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.

2 years agonvmm - Change NVMM_MAX_RAM
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.

2 years agonvmm - Improve cast portability of vmx_resume_rip
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).

2 years agonvmm - Move M_NVMM definition to nvmm_os.c
Matthew Dillon [Sat, 10 Jul 2021 18:29:15 +0000 (11:29 -0700)]
nvmm - Move M_NVMM definition to nvmm_os.c

2 years agonvmm: Save host DB regs in per-cpu structure
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

2 years agonvmm - Fix event commit logic
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.

2 years agokernel - Expand GDT table to maximum size
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.

2 years agoNVMM: Update examples and libnvmm tests
Aaron LI [Sun, 4 Jul 2021 13:40:14 +0000 (21:40 +0800)]
NVMM: Update examples and libnvmm tests

2 years agoNVMM: Update libnvmm.3 and TODO.nvmm
Aaron LI [Sun, 4 Jul 2021 13:31:40 +0000 (21:31 +0800)]
NVMM: Update libnvmm.3 and TODO.nvmm

2 years agonvmmctl(8): Misc cleanups
Aaron LI [Sun, 4 Jul 2021 01:32:23 +0000 (09:32 +0800)]
nvmmctl(8): Misc cleanups

2 years agolibnvmm: More OS independence
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.

2 years agolibnvmm: Misc cleanups
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.

2 years agonvmm: More OS independence
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.

2 years agonvmm: Various misc cleanups and minor updates
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.

2 years agoRevert "libnvmm: Fix mmap() failure with 'permission denied'"
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).

2 years agonvmm: Create comm page in nvmm_vcpu_create() rather than via mmap()
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.

2 years agonvmm: Reduce NVMM_MAX_VCPUS from 256 to 128
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.

2 years agonvmm: Rename a few things for clarity
Aaron LI [Sat, 3 Jul 2021 10:03:29 +0000 (18:03 +0800)]
nvmm: Rename a few things for clarity

2 years agonvmm: Make FPU state more OS-indenpendent
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.

2 years agonvmm: Rewrite vmx_vmx{on,off}() as inline ASM functions
Aaron LI [Sat, 3 Jul 2021 08:11:46 +0000 (16:11 +0800)]
nvmm: Rewrite vmx_vmx{on,off}() as inline ASM functions

2 years agonvmm: Make svm_vmrun() void
Aaron LI [Sat, 3 Jul 2021 08:04:02 +0000 (16:04 +0800)]
nvmm: Make svm_vmrun() void

2 years agonvmm: Add SVM CET definitions
Aaron LI [Sat, 3 Jul 2021 08:00:28 +0000 (16:00 +0800)]
nvmm: Add SVM CET definitions

Not actually used.  For completeness.

2 years agonvmm: Redefine CPUID values to be OS-indenpendent
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.

2 years agonvmm: Improve CPUID emulation #5: handle Fn0000_0001:EBX[23:16]
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.

2 years agonvmm: Improve CPUID emulation #4: handle Fn0000_0004 on Intel
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.

2 years agonvmm: Improve CPUID emulation #3: handle Fn8000_0008:ECX on AMD
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.

2 years agonvmm: Improve CPUID emulation #2: mask upper bits of guest EAX/ECX
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.

2 years agonvmm: Improve CPUID emulation #1: flags
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.

2 years agonvmm: Clarify state handling
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.

2 years agonvmm: Clarify the RESET state
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'.

2 years agonvmm: Add #CP (control protection exception)
Aaron LI [Sat, 3 Jul 2021 01:50:03 +0000 (09:50 +0800)]
nvmm: Add #CP (control protection exception)

2 years agolibnvmm: Clarify x86 MOVS emulation
Aaron LI [Sat, 3 Jul 2021 00:29:53 +0000 (08:29 +0800)]
libnvmm: Clarify x86 MOVS emulation

2 years agolibnvmm: Fix a memory leak in nvmm_machine_create()
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.

2 years agonvmm: Improve FPU support and reduce diff against NetBSD
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.

2 years agodoc: Import nvmm TODO note from NetBSD-current
Aaron LI [Tue, 29 Jun 2021 14:33:06 +0000 (22:33 +0800)]
doc: Import nvmm TODO note from NetBSD-current

2 years agolibnvmm.3: Mention regression tests in FILES section
Aaron LI [Wed, 16 Jun 2021 22:49:59 +0000 (06:49 +0800)]
libnvmm.3: Mention regression tests in FILES section

2 years agotestcases/libnvmm: Improve makefile to not write in source tree
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)

2 years agotestcases/libnvmm: Add to dfregress(8) test framework
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.

2 years agotestcases/libnvmm: Port to DragonFly
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).

2 years agoImport libnvmm tests from NetBSD-current
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

2 years agolibnvmm.3: Mention 'calc-vm' and 'demo' test code in FILES
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.

2 years agotest/nvmm/demo: Improve progress logs to help test/debug
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.

2 years agotest/nvmm/demo: Rewrite makefiles to not write in source tree
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).

2 years agotest/nvmm/demo: Port 'smallkern' to DragonFly
Aaron LI [Tue, 8 Jun 2021 06:29:40 +0000 (14:29 +0800)]
test/nvmm/demo: Port 'smallkern' to DragonFly

2 years agotest/nvmm/demo: Make 'smallkern' more self-contained
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.

2 years agotest/nvmm/demo: Various cleanups to 'smallkern'
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.

2 years agotest/nvmm/demo: Fix ELF load/mmap issue on DragonFly
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.

2 years agotest/nvmm/demo: Port 'toyvirt' to DragonFly
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.

2 years agotest/nvmm/demo: Fix some compilation warnings
Aaron LI [Fri, 28 May 2021 15:10:04 +0000 (23:10 +0800)]
test/nvmm/demo: Fix some compilation warnings

2 years agotest/nvmm/demo: Update 'toyvirt' to current libnvmm(3)
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.

2 years agotest/nvmm/demo: Update makefiles and README
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.

2 years agotest/nvmm: Add a demo for demonstration of libnvmm(3) API
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

2 years agotest/nvmm: Add a Makefile and a test script for 'calc-vm'
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.

2 years agotest/nvmm: Add progress logs in 'calc-vm'
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.

2 years agotest/nvmm: Enhance error checks in 'calc-vm'
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.

2 years agotest/nvmm: Add 'calc-vm' (simple VM-based calculator)
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

2 years agoBump __DragonFly_version for adding nvmm(4) and libnvmm(3)
Aaron LI [Sun, 13 Jun 2021 14:34:25 +0000 (22:34 +0800)]
Bump __DragonFly_version for adding nvmm(4) and libnvmm(3)