Page MenuHomeFreeBSD

D61734.1795150423.diff
No OneTemporary

Authored By
Unknown
Size
11 KB
Referenced Files
None
Subscribers
None

D61734.1795150423.diff

diff --git a/tools/test/README b/tools/test/README
--- a/tools/test/README
+++ b/tools/test/README
@@ -14,6 +14,7 @@
iconv Character set conversion tests.
malloc A program to test and benchmark malloc().
net A set of generic test programs for networking.
+mbload Run the VMX tests of kvm-unit-tests in bhyve(8).
netfibs Programs to test multi-FIB network stacks.
posixshm A program to test POSIX shared memory.
ppsapi Test 1 Pulse Per Second (1PPS) input for time control.
diff --git a/tools/test/mbload/Makefile b/tools/test/mbload/Makefile
new file mode 100644
--- /dev/null
+++ b/tools/test/mbload/Makefile
@@ -0,0 +1,6 @@
+PROG= mbload
+MAN=
+LIBADD= vmmapi
+SCRIPTS= vmx-tests.sh
+
+.include <bsd.prog.mk>
diff --git a/tools/test/mbload/README b/tools/test/mbload/README
new file mode 100644
--- /dev/null
+++ b/tools/test/mbload/README
@@ -0,0 +1,47 @@
+mbload loads a Multiboot kernel into a bhyve virtual machine, like
+bhyveload(8) does with a FreeBSD kernel:
+
+ mbload [-m memsize] vmname kernel [argument ...]
+ bhyve -c 1 -m memsize -H -s 0,hostbridge -s 31,lpc -l com1,stdio vmname
+
+Its purpose is to run the tests of kvm-unit-tests
+(https://gitlab.com/kvm-unit-tests/kvm-unit-tests), which are such kernels,
+without changing them. vmx-tests.sh runs the tests for VMX, which test
+nested virtualization (the hw.vmm.vmx.nested tunable in vmm(4)).
+
+kvm-unit-tests is not part of FreeBSD. Build x86/vmx.flat from it where it
+builds (it wants GNU make and GCC; the file does not depend on the system
+that it was built on):
+
+ git clone https://gitlab.com/kvm-unit-tests/kvm-unit-tests.git
+ cd kvm-unit-tests && ./configure && make x86/vmx.flat
+
+Then, on an Intel machine with hw.vmm.vmx.nested set and vmm.ko loaded:
+
+ make
+ MBLOAD=$(make -V .OBJDIR)/mbload sh vmx-tests.sh /path/to/vmx.flat
+
+This prints one line for every test and leaves each test's output in
+vmx-tests.log/. A test name on the command line runs only that test.
+
+What to expect: a test that needs something that the guest is not offered
+(EPT, VPID, the preemption timer, APIC virtualization, VMCS shadowing)
+reports that and is listed as SKIP. ABORT is a test that could not go on,
+in most cases because it sets a control that is not offered without looking
+at the capability MSRs first, so that its VM entry fails. These tests do not
+pass at present:
+
+- MSR_switch, atomic_switch_max_msrs_test, atomic_switch_overflow_msrs_test,
+ vmx_store_tsc_test, rdtsc_vmexit_diff_test and part of vmx_controls_test:
+ the VM-entry and VM-exit MSR areas are not implemented.
+- interrupt, nmi_hlt, vmx_apic_passthrough_test and
+ vmx_apic_passthrough_tpr_threshold_test: they run a guest without
+ "external-interrupt exiting" or "NMI exiting", which a guest hypervisor has
+ to set.
+- vmx_pending_event_test and vmx_pending_event_hlt_test: they put the local
+ APIC back into xAPIC mode, which bhyve does not do for a running guest.
+- vmx_pf_exception_test (one check of 19 million) and vmx_pf_no_vpid_test:
+ bhyve lets a guest set CR4.PKE although CPUID does not have PKU. The
+ second one also takes a very long time.
+- exit_monitor_from_l2_test does not end; kvm-unit-tests leaves it out of
+ its own default run as well.
diff --git a/tools/test/mbload/mbload.c b/tools/test/mbload/mbload.c
new file mode 100644
--- /dev/null
+++ b/tools/test/mbload/mbload.c
@@ -0,0 +1,214 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Wanpeng Qian <wanpengqian@gmail.com>
+ */
+
+/*
+ * Load a Multiboot (version 1) ELF32 kernel into a bhyve virtual machine,
+ * the way bhyveload(8) loads a FreeBSD kernel. bhyve(8) started without a
+ * boot ROM then runs it. This is what the tests of kvm-unit-tests are, and
+ * what QEMU's -kernel option does with them.
+ */
+
+#include <sys/param.h>
+#include <sys/elf32.h>
+#include <sys/stat.h>
+
+#include <machine/specialreg.h>
+#include <machine/vmm.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <libgen.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+#include <unistd.h>
+#include <vmmapi.h>
+
+#define MB_BOOT_MAGIC 0x2badb002
+#define MB_INFO_MEMORY 0x00000001
+#define MB_INFO_CMDLINE 0x00000004
+#define MB_INFO_MMAP 0x00000040
+
+#define MB_GDT_ADDR 0x10000 /* the TSS is one page above */
+#define MB_STACK_ADDR 0x20000
+#define MB_INFO_ADDR 0x20000
+#define MB_INFO_SIZE 0x1000
+
+#define MB_MB (1024 * 1024UL)
+#define MB_GB (1024 * MB_MB)
+
+#define MB_LOWMEM_END 0x9fc00
+#define MB_HIMEM_START 0x100000
+
+struct mb_info {
+ uint32_t flags;
+ uint32_t mem_lower;
+ uint32_t mem_upper;
+ uint32_t boot_device;
+ uint32_t cmdline;
+ uint32_t mods_count;
+ uint32_t mods_addr;
+ uint32_t syms[4];
+ uint32_t mmap_length;
+ uint32_t mmap_addr;
+} __packed;
+
+struct mb_mmap {
+ uint32_t size;
+ uint64_t base;
+ uint64_t length;
+ uint32_t type;
+} __packed;
+
+static void
+usage(void)
+{
+ fprintf(stderr, "usage: %s [-m memsize] vmname kernel [argument ...]\n",
+ getprogname());
+ exit(EX_USAGE);
+}
+
+static uint32_t
+load_elf(struct vmctx *ctx, const char *path)
+{
+ Elf32_Ehdr eh;
+ Elf32_Phdr ph;
+ char *p;
+ int fd, i;
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ err(1, "%s", path);
+ if (pread(fd, &eh, sizeof(eh), 0) != (ssize_t)sizeof(eh) ||
+ !IS_ELF(eh) || eh.e_ident[EI_CLASS] != ELFCLASS32 ||
+ eh.e_machine != EM_386 || eh.e_phentsize != sizeof(ph))
+ errx(1, "%s: not an ELF32 i386 executable", path);
+ for (i = 0; i < eh.e_phnum; i++) {
+ if (pread(fd, &ph, sizeof(ph), eh.e_phoff + i * sizeof(ph)) !=
+ (ssize_t)sizeof(ph))
+ err(1, "%s: program header %d", path, i);
+ if (ph.p_type != PT_LOAD || ph.p_memsz == 0)
+ continue;
+ if (ph.p_filesz > ph.p_memsz)
+ errx(1, "%s: bad program header %d", path, i);
+ p = vm_map_gpa(ctx, ph.p_paddr, ph.p_memsz);
+ if (p == NULL)
+ errx(1, "%s: segment %d is outside guest memory",
+ path, i);
+ if (pread(fd, p, ph.p_filesz, ph.p_offset) !=
+ (ssize_t)ph.p_filesz)
+ err(1, "%s: segment %d", path, i);
+ memset(p + ph.p_filesz, 0, ph.p_memsz - ph.p_filesz);
+ }
+ close(fd);
+ return (eh.e_entry);
+}
+
+static void
+setup_info(struct vmctx *ctx, int argc, char **argv)
+{
+ struct mb_info *mbi;
+ struct mb_mmap *mm;
+ char *base, *cmdline;
+ size_t highmem, len, lowmem, off;
+ int i;
+
+ base = vm_map_gpa(ctx, MB_INFO_ADDR, MB_INFO_SIZE);
+ if (base == NULL)
+ errx(1, "guest memory is too small");
+ memset(base, 0, MB_INFO_SIZE);
+ lowmem = vm_get_lowmem_size(ctx);
+ highmem = vm_get_highmem_size(ctx);
+
+ mbi = (void *)base;
+ mbi->flags = MB_INFO_MEMORY | MB_INFO_CMDLINE | MB_INFO_MMAP;
+ mbi->mem_lower = MB_LOWMEM_END / 1024;
+ mbi->mem_upper = (lowmem - MB_HIMEM_START) / 1024;
+ off = roundup2(sizeof(*mbi), 8);
+
+ mm = (void *)(base + off);
+ mbi->mmap_addr = MB_INFO_ADDR + off;
+ mm[0].base = 0;
+ mm[0].length = MB_LOWMEM_END;
+ mm[1].base = MB_HIMEM_START;
+ mm[1].length = lowmem - MB_HIMEM_START;
+ mm[2].base = 4 * MB_GB;
+ mm[2].length = highmem;
+ for (i = 0; i < (highmem != 0 ? 3 : 2); i++) {
+ mm[i].size = sizeof(*mm) - sizeof(mm->size);
+ mm[i].type = 1;
+ mbi->mmap_length += sizeof(*mm);
+ }
+ off += mbi->mmap_length;
+
+ /* The command line: the kernel's name and the arguments. */
+ cmdline = base + off;
+ mbi->cmdline = MB_INFO_ADDR + off;
+ for (i = 0; i < argc; i++) {
+ len = strlen(argv[i]);
+ if (off + len + 2 > MB_INFO_SIZE)
+ errx(1, "the arguments are too long");
+ if (i != 0)
+ *cmdline++ = ' ';
+ memcpy(cmdline, argv[i], len);
+ cmdline += len;
+ off += len + 1;
+ }
+}
+
+int
+main(int argc, char **argv)
+{
+ struct vmctx *ctx;
+ struct vcpu *vcpu;
+ const char *vmname;
+ size_t memsize;
+ uint32_t entry;
+ int error, opt;
+
+ memsize = 256 * MB_MB;
+ while ((opt = getopt(argc, argv, "m:")) != -1) {
+ switch (opt) {
+ case 'm':
+ if (vm_parse_memsize(optarg, &memsize) != 0)
+ errx(EX_USAGE, "invalid memsize '%s'", optarg);
+ break;
+ default:
+ usage();
+ }
+ }
+ argc -= optind;
+ argv += optind;
+ if (argc < 2)
+ usage();
+ vmname = argv[0];
+
+ error = vm_create(vmname);
+ if (error != 0 && errno != EEXIST)
+ err(1, "vm_create");
+ ctx = vm_open(vmname);
+ if (ctx == NULL)
+ err(1, "vm_open");
+ if (error != 0 && vm_reinit(ctx) != 0)
+ err(1, "vm_reinit");
+ vcpu = vm_vcpu_open(ctx, 0);
+ if (vm_setup_memory(ctx, memsize, VM_MMAP_ALL) != 0)
+ err(1, "vm_setup_memory");
+
+ entry = load_elf(ctx, argv[1]);
+ argv[1] = basename(argv[1]);
+ setup_info(ctx, argc - 1, argv + 1);
+
+ if (vm_setup_freebsd_registers_i386(vcpu, entry, MB_GDT_ADDR,
+ MB_STACK_ADDR) != 0 ||
+ vm_set_register(vcpu, VM_REG_GUEST_RAX, MB_BOOT_MAGIC) != 0 ||
+ vm_set_register(vcpu, VM_REG_GUEST_RBX, MB_INFO_ADDR) != 0)
+ err(1, "cannot set the registers");
+ return (0);
+}
diff --git a/tools/test/mbload/vmx-tests.sh b/tools/test/mbload/vmx-tests.sh
new file mode 100644
--- /dev/null
+++ b/tools/test/mbload/vmx-tests.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
+#-
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2026 Wanpeng Qian <wanpengqian@gmail.com>
+#
+# Run the VMX tests of kvm-unit-tests (x86/vmx.flat) in bhyve(8) to test
+# nested virtualization (hw.vmm.vmx.nested, see vmm(4)). Every test runs
+# in a virtual machine of its own, because a test that cannot go on ends
+# the whole program. See the README.
+#
+
+usage()
+{
+ echo "usage: $0 [-d logdir] [-t seconds] vmx.flat [test ...]" >&2
+ exit 64
+}
+
+run()
+{
+ bhyvectl --destroy --vm="$vm" > /dev/null 2>&1
+ "$mbload" -m 512M "$vm" "$flat" "$1" || exit 1
+ # -w and -x: the tests turn the local APIC off and on and switch it
+ # to x2APIC mode, which bhyve does not do after the guest has started.
+ # lpc.fwcfg=qemu: they read the number of CPUs from fw_cfg.
+ timeout "$tmo" bhyve -c 1 -m 512M -H -w -x -o lpc.fwcfg=qemu \
+ -s 0,hostbridge -s 31,lpc -l com1,stdio "$vm" \
+ > "$2" 2>&1 < /dev/null
+ rc=$?
+ bhyvectl --destroy --vm="$vm" > /dev/null 2>&1
+}
+
+logdir=vmx-tests.log
+tmo=300
+while getopts d:t: opt; do
+ case $opt in
+ d) logdir=$OPTARG ;;
+ t) tmo=$OPTARG ;;
+ *) usage ;;
+ esac
+done
+shift $((OPTIND - 1))
+[ $# -ge 1 ] || usage
+flat=$1
+shift
+[ -r "$flat" ] || { echo "$0: cannot read $flat" >&2; exit 1; }
+if [ "$(sysctl -n hw.vmm.vmx.nested 2> /dev/null)" != 1 ]; then
+ echo "$0: hw.vmm.vmx.nested is not set" >&2
+ exit 1
+fi
+mbload=${MBLOAD:-mbload}
+vm=vmxtest$$
+mkdir -p "$logdir" || exit 1
+
+if [ $# -eq 0 ]; then
+ # A filter that matches no test makes the program list all of them.
+ # The tests for EPT and the ones that need more than one CPU are
+ # left out.
+ run no_such_test "$logdir/list"
+ set -- $(sed -n 's/^filter = no_such_test, test = //p' \
+ "$logdir/list" | tr -d '\r' | tr ' ' '_' |
+ grep -v -e '^ept_access' -e '^vmx_smp' -e '_smp_')
+ [ $# -gt 0 ] || { echo "$0: no tests in $flat" >&2; exit 1; }
+fi
+
+for t; do
+ log=$logdir/$(echo "$t" | tr / _).log
+ run "$t" "$log"
+ pass=$(grep -c '^PASS' "$log")
+ fail=$(grep -c '^FAIL' "$log")
+ # The test ends with HLT and interrupts off: bhyve exits with 2.
+ if [ $rc -eq 124 ]; then
+ res=TIMEOUT
+ elif ! grep -q '^SUMMARY' "$log"; then
+ res=ABORT
+ elif [ "$fail" -ne 0 ]; then
+ res=FAIL
+ elif [ "$pass" -eq 0 ]; then
+ res=SKIP
+ else
+ res=PASS
+ fi
+ printf '%-8s %-44s %6d passed %4d failed\n' "$res" "$t" "$pass" "$fail"
+done

File Metadata

Mime Type
text/plain
Expires
Fri, Nov 20, 4:53 AM (23 h, 19 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41860373
Default Alt Text
D61734.1795150423.diff (11 KB)

Event Timeline