diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..587b409 --- /dev/null +++ b/Makefile @@ -0,0 +1,103 @@ +ifeq ($V, 1) + VERBOSE = +else + VERBOSE = @ +endif + +include config.mk + +VMM_SRC := \ + src/vmm/x86.c \ + src/vmm/vmm.c \ + src/vmm/vmm_host.c \ + src/vmm/vmm_mem.c \ + src/vmm/vmm_lapic.c \ + src/vmm/vmm_instruction_emul.c \ + src/vmm/vmm_ioport.c \ + src/vmm/vmm_callout.c \ + src/vmm/vmm_stat.c \ + src/vmm/vmm_util.c \ + src/vmm/vmm_api.c \ + src/vmm/intel/vmx.c \ + src/vmm/intel/vmx_msr.c \ + src/vmm/intel/vmcs.c \ + src/vmm/io/vatpic.c \ + src/vmm/io/vatpit.c \ + src/vmm/io/vhpet.c \ + src/vmm/io/vioapic.c \ + src/vmm/io/vlapic.c \ + src/vmm/io/vpmtmr.c \ + src/vmm/io/vrtc.c + +XHYVE_SRC := \ + src/acpi.c \ + src/atkbdc.c \ + src/block_if.c \ + src/consport.c \ + src/dbgport.c \ + src/inout.c \ + src/ioapic.c \ + src/md5c.c \ + src/mem.c \ + src/mevent.c \ + src/mptbl.c \ + src/pci_ahci.c \ + src/pci_emul.c \ + src/pci_hostbridge.c \ + src/pci_irq.c \ + src/pci_lpc.c \ + src/pci_uart.c \ + src/pci_virtio_block.c \ + src/pci_virtio_vmnet.c \ + src/pci_virtio_rnd.c \ + src/pm.c \ + src/post.c \ + src/rtc.c \ + src/smbiostbl.c \ + src/task_switch.c \ + src/uart_emul.c \ + src/xhyve.c \ + src/virtio.c \ + src/xmsr.c + +FIRMWARE_SRC := \ + src/firmware/kexec.c + +SRC := \ + $(VMM_SRC) \ + $(XHYVE_SRC) \ + $(FIRMWARE_SRC) + +OBJ := $(SRC:src/%.c=build/%.o) +DEP := $(OBJ:%.o=%.d) +INC := -Iinclude + +TARGET = build/xhyve + +all: $(TARGET) | build + +.PHONY: clean all +.SUFFIXES: + +-include $(DEP) + +build: + @mkdir -p build + +build/%.o: src/%.c + @echo cc $< + @mkdir -p $(dir $@) + $(VERBOSE) $(ENV) $(CC) $(CFLAGS) $(INC) $(DEF) -MMD -MT $@ -MF build/$*.d -o $@ -c $< + +$(TARGET).sym: $(OBJ) + @echo ld $(notdir $@) + $(VERBOSE) $(ENV) $(LD) $(LDFLAGS) -Xlinker $(TARGET).lto.o -o $@ $(OBJ) + @echo dsym $(notdir $(TARGET).dSYM) + $(VERBOSE) $(ENV) $(DSYM) $@ -o $(TARGET).dSYM + +$(TARGET): $(TARGET).sym + @echo strip $(notdir $@) + $(VERBOSE) $(ENV) $(STRIP) $(TARGET).sym -o $@ + +clean: + @rm -rf build diff --git a/README.md b/README.md new file mode 100644 index 0000000..eb404e1 --- /dev/null +++ b/README.md @@ -0,0 +1,184 @@ +# xhyve + +![](./xhyve_logo.png) + + +About +----- + +The *xhyve hypervisor* is a port of [bhyve](http://www.bhyve.org) to OS X. It is built on top of Hypervisor.framework in OS X 10.10 Yosemite and higher, runs entirely in userspace, and has no other dependencies. It can run vanilla Linux distributions and may gain support for other guest operating systems in the future. + +License: BSD + +Introduction: [http://www.pagetable.com/?p=831](http://www.pagetable.com/?p=831) + +Requirements +------------ + +* OS X 10.10 Yosemite or later +* A 2010 or later Mac + +Building +-------- + + $ make + +The resulting binary will be in build/xhyve + +Usage +----- + + $ xhyve -h + + +What is bhyve? +-------------- + +bhyve is the FreeBSD hypervisor, roughly analogous to KVM + QEMU on Linux. It has a focus on simplicity and being legacy free. + +It exposes the following peripherals to virtual machines: + + - Local x(2)APIC + - IO-APIC + - 8259A PIC + - 8253/8254 PIT + - HPET + - PM Timer + - RTC + - PCI + - host bridge + - passthrough + - UART + - AHCI (i.e. HDD and CD) + - VirtIO block device + - VirtIO networking + - VirtIO RNG + +Notably absent are sound, USB, HID and any kind of graphics support. With a focus on server virtualization this is not strictly a requirement. bhyve may gain desktop virtualization capabilities in the future but this doesn't seem to be a priority. + +Unlike QEMU, byhve also currently lacks any kind of guest-side firmware (QEMU uses the GPL3 [SeaBIOS](http://www.seabios.org)), but aims to provide a compatible [OVMF EFI](http://www.linux-kvm.org/page/OVMF) in the near future. It does however provide ACPI, SMBIOS and MP Tables. + +bhyve architecture +------------------ + Linux + I/O VM control FreeBSD NetBSD + OpenBSD + | A | A | | + V | V | V V + +-------------++-------------++-------------++-------------+ + | || || || | + | bhyve || bhyvectl || bhyveload || grub2-bhyve | + | || || || | + | || || || | + +-------------++-------------++-------------++-------------+ + +----------------------------------------------------------+ + | libvmmapi | + +----------------------------------------------------------+ + A + | user + ------------------------------┼------------------------------ + | ioctl FreeBSD kernel + V + +----------------------------+ + | VMX/SVM host | + | VMX/SVM guest | + | VMX/SVM nested paging | + | Timers | + | Interrupts | + +----------------------------+ + vmm.ko + + +**vmm.ko** + +The bhyve FreeBSD kernel module. Manages VM and vCPU objects, the guest physical address space and handles guest interaction with PIC, PIT, HPET, PM Timer, x(2)APIC and I/O-APIC. Contains a minimal x86 emulator to decode guest MMIO. Executes the two innermost vCPU runloops (VMX/SVM and interrupts/timers/paging). Has backends for Intel VMX and AMD SVM. Provides an ioctl and mmap API to userspace. + +**libvmmapi** + +Thin abstraction layer between the vmm.ko ioctl interface and the userspace C API. + +**bhyve** + +The userspace bhyve component (kind of a very light-weight QEMU) that executes virtual machines. Runs the guest I/O vCPU runloops. Manages ACPI, PCI and all non in-kernel devices. Interacts with vmm.ko through libvmmapi. + +**bhyvectl** + +Somewhat superfluous utility to introspect and manage the life cycle of virtual machines. Virtual machines and vCPUs can exist as kernel objects independently of a bhyve host process. Typically used to delete VM objects after use. Odd architectural choice. + +**bhyveload** + +Userspace port of the FreeBSD bootloader. Since bhyve still lacks a firmware this is a cumbersome workaround to bootstrap a guest operating system. It creates a VM object, loads the FreeBSD kernel into guest memory, sets up the initial vCPU state and then exits. Only then a VM can be executed by bhyve. + +**grub2-bhyve** + +Performs the same function as bhyveload but is a userspace port of [GRUB2](http://github.com/grehan-freebsd/grub2-bhyve). It is used to bootstrap guest operating systems other than FreeBSD, i.e. Linux, OpenBSD and NetBSD. + +Support for Windows guests is work in progress and dependent on the EFI port. + + +xhyve architecture +------------------ + +----------------------------------------------------------+ + | xhyve | + | | + | I/O | + | | + | | + | | + |+--------------------------------------------------------+| + || vmm VMX guest || + || Timers || + || Interrupts || + |+--------------------------------------------------------+| + +----------------------------------------------------------+ + +----------------------------------------------------------+ + | Hypervisor.framework | + +----------------------------------------------------------+ + A + | user + ------------------------------┼------------------------------ + |syscall xnu kernel + V + + VMX host + VMX nested paging + + +xhyve shares most of the code with bhyve but is architecturally very different. Hypervisor.framework provides an interface to the VMX VMCS guest state and a safe subset of the VMCS control fields, thus making userspace hypervisors without any additional kernel extensions possible. The VMX host state and all aspects of nested paging are handled by the OS X kernel, you can manage the guest physical address space simply through mapping of regions of your own address space. + +*xhyve* is equivalent to the *bhyve* process but gains a subset of a userspace port of the vmm kernel module. SVM, PCI passthrough and the VMX host and EPT aspects are dropped. The vmm component provides a libvmmapi compatible interface to xhyve. Hypervisor.framework seems to enforce a strict 1:1 relationship between a host process/VM and host thread/vCPU, that means VMs and vCPUs can only be interacted with by the processes and threads that created them. Therefore, unlike bhyve, xhyve needs to adhere to a single process model. Multiple virtual machines can be created by launching multiple instances of xhyve. xhyve retains most of the bhyve command line interface. + +*bhyvectl*, *bhyveload* and *grub2-bhyve* are incompatible with a single process model and are dropped. As a stop-gap solution until we have a proper firmware xhyve supports the Linux [kexec protocol](http://www.kernel.org/doc/Documentation/x86/boot.txt), a very simple and straightforward way to bootstrap a Linux kernel. It takes a bzImage and optionally initrd image and kernel parameter string as input. + +TODO +---- + +- vmm: + - enable APIC access page to speed up APIC emulation + - enable x2APIC MSRs (even faster) + - vmm_callout: + - is a quick'n'dirty implementation of the FreeBSD kernel callout mechanism + - seems to be racy + - fix races or perhaps replace with something better + - use per vCPU timer event thread (performance)? + - some 32-bit guests are broken (support PAE paging in VMCS) + - PCID guest support (performance) +- block_if: + - OS X does not support preadv/pwritev, we need to serialize reads and writes for the time being until we find a better solution. + - support block devices other than plain files +- virtio_net: + - make it not require root + - unify TAP and vmnet backends + - performance: send/receive more than a single packet at a time +- ACPI tables don't work + - bhyve creates ASL on the fly and then calls out to an ASL compiler (iasl) on + every VM boot to create the DSDT: + - remove dependency on iasl by creating AML bytecode directly + - shouldn't be to hard since we we are only interested in a very small + subset of ASL +- virtio_rnd: + - is untested +- remove explicit state transitions: + - since only the owning task/thread can modify the VM/vCPUs a lot of the synchronization might be unnecessary +- performance, performance and performance +- remove vestigial code, cleanup diff --git a/bhyve/Makefile b/bhyve/Makefile deleted file mode 100644 index bb81bcb..0000000 --- a/bhyve/Makefile +++ /dev/null @@ -1,50 +0,0 @@ -# -# $FreeBSD$ -# - -PROG= bhyve - -DEBUG_FLAGS= -g -O0 - -MAN= bhyve.8 - -SRCS= \ - atkbdc.c \ - acpi.c \ - bhyverun.c \ - block_if.c \ - consport.c \ - dbgport.c \ - inout.c \ - ioapic.c \ - mem.c \ - mevent.c \ - mptbl.c \ - pci_ahci.c \ - pci_emul.c \ - pci_hostbridge.c \ - pci_irq.c \ - pci_lpc.c \ - pci_passthru.c \ - pci_virtio_block.c \ - pci_virtio_net.c \ - pci_virtio_rnd.c \ - pci_uart.c \ - pm.c \ - post.c \ - rtc.c \ - smbiostbl.c \ - task_switch.c \ - uart_emul.c \ - virtio.c \ - xmsr.c \ - spinup_ap.c - -.PATH: ${.CURDIR}/../../sys/amd64/vmm -SRCS+= vmm_instruction_emul.c - -LIBADD= vmmapi md pthread - -WARNS?= 2 - -.include diff --git a/bhyve/bhyve.8 b/bhyve/bhyve.8 deleted file mode 100644 index ee0f2ca..0000000 --- a/bhyve/bhyve.8 +++ /dev/null @@ -1,325 +0,0 @@ -.\" Copyright (c) 2013 Peter Grehan -.\" All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $FreeBSD$ -.\" -.Dd September 17, 2014 -.Dt BHYVE 8 -.Os -.Sh NAME -.Nm bhyve -.Nd "run a guest operating system inside a virtual machine" -.Sh SYNOPSIS -.Nm -.Op Fl abehuwxACHPWY -.Op Fl c Ar numcpus -.Op Fl g Ar gdbport -.Op Fl l Ar lpcdev Ns Op , Ns Ar conf -.Op Fl m Ar size Ns Op Ar K|k|M|m|G|g|T|t -.Op Fl p Ar vcpu:hostcpu -.Op Fl s Ar slot,emulation Ns Op , Ns Ar conf -.Op Fl U Ar uuid -.Ar vmname -.Sh DESCRIPTION -.Nm -is a hypervisor that runs guest operating systems inside a -virtual machine. -.Pp -Parameters such as the number of virtual CPUs, amount of guest memory, and -I/O connectivity can be specified with command-line parameters. -.Pp -The guest operating system must be loaded with -.Xr bhyveload 4 -or a similar boot loader before running -.Nm . -.Pp -.Nm -runs until the guest operating system reboots or an unhandled hypervisor -exit is detected. -.Sh OPTIONS -.Bl -tag -width 10n -.It Fl a -The guest's local APIC is configured in xAPIC mode. -The xAPIC mode is the default setting so this option is redundant. It will be -deprecated in a future version. -.It Fl A -Generate ACPI tables. -Required for -.Fx Ns /amd64 -guests. -.It Fl b -Enable a low-level console device supported by -.Fx -kernels compiled with -.Cd "device bvmconsole" . -This option will be deprecated in a future version. -.It Fl c Ar numcpus -Number of guest virtual CPUs. -The default is 1 and the maximum is 16. -.It Fl C -Include guest memory in core file. -.It Fl e -Force -.Nm -to exit when a guest issues an access to an I/O port that is not emulated. -This is intended for debug purposes. -.It Fl g Ar gdbport -For -.Fx -kernels compiled with -.Cd "device bvmdebug" , -allow a remote kernel kgdb to be relayed to the guest kernel gdb stub -via a local IPv4 address and this port. -This option will be deprecated in a future version. -.It Fl h -Print help message and exit. -.It Fl H -Yield the virtual CPU thread when a HLT instruction is detected. -If this option is not specified, virtual CPUs will use 100% of a host CPU. -.It Fl l Ar lpcdev Ns Op , Ns Ar conf -Allow devices behind the LPC PCI-ISA bridge to be configured. -The only supported devices are the TTY-class devices, -.Li com1 -and -.Li com2 . -.It Fl m Ar size Ns Op Ar K|k|M|m|G|g|T|t -Guest physical memory size in bytes. -This must be the same size that was given to -.Xr bhyveload 8 . -.Pp -The size argument may be suffixed with one of K, M, G or T (either upper -or lower case) to indicate a multiple of kilobytes, megabytes, gigabytes, -or terabytes. -If no suffix is given, the value is assumed to be in megabytes. -.It Fl p Ar vcpu:hostcpu -Pin guest's virtual CPU -.Em vcpu -to -.Em hostcpu . -.It Fl P -Force the guest virtual CPU to exit when a PAUSE instruction is detected. -.It Fl s Ar slot,emulation Ns Op , Ns Ar conf -Configure a virtual PCI slot and function. -.Pp -.Nm bhyve -provides PCI bus emulation and virtual devices that can be attached to -slots on the bus. -There are 32 available slots, with the option of providing up to 8 functions -per slot. -.Bl -tag -width 10n -.It Ar slot -.Ar pcislot[:function] -.Ar bus:pcislot:function -.Pp -The -.Ar pcislot -value is 0 to 31. The optional function value is 0 to 7. The optional -.Ar bus -value is 0 to 255. -If not specified, the function value defaults to 0. -If not specified, the bus value defaults to 0. -.It Ar emulation -.Bl -tag -width 10n -.It Li hostbridge | Li amd_hostbridge -.Pp -Provide a simple host bridge. -This is usually configured at slot 0, and is required by most guest -operating systems. -The -.Li amd_hostbridge -emulation is identical but uses a PCI vendor ID of -.Li AMD . -.It Li passthru -PCI pass-through device. -.It Li virtio-net -Virtio network interface. -.It Li virtio-blk -Virtio block storage interface. -.It Li virtio-rnd -Virtio RNG interface. -.It Li ahci-cd -AHCI controller attached to an ATAPI CD/DVD. -.It Li ahci-hd -AHCI controller attached to a SATA hard-drive. -.It Li uart -PCI 16550 serial device. -.It Li lpc -LPC PCI-ISA bridge with COM1 and COM2 16550 serial ports. The LPC bridge -emulation can only be configured on bus 0. -.El -.It Op Ar conf -This optional parameter describes the backend for device emulations. -If -.Ar conf -is not specified, the device emulation has no backend and can be -considered unconnected. -.Pp -Network devices: -.Bl -tag -width 10n -.It Ar tapN Ns Op , Ns Ar mac=xx:xx:xx:xx:xx:xx -.It Ar vmnetN Ns Op , Ns Ar mac=xx:xx:xx:xx:xx:xx -.Pp -If -.Ar mac -is not specified, the MAC address is derived from a fixed OUI and the -remaining bytes from an MD5 hash of the slot and function numbers and -the device name. -.Pp -The MAC address is an ASCII string in -.Xr ethers 5 -format. -.El -.Pp -Block storage devices: -.Bl -tag -width 10n -.It Pa /filename Ns Oo , Ns Ar block-device-options Oc -.It Pa /dev/xxx Ns Oo , Ns Ar block-device-options Oc -.El -.Pp -The -.Ar block-device-options -are: -.Bl -tag -width 8n -.It Li nocache -Open the file with -.Dv O_DIRECT . -.It Li direct -Open the file using -.Dv O_SYNC . -.It Li ro -Force the file to be opened read-only. -.It Li sectorsize= Ns Ar logical Ns Oo / Ns Ar physical Oc -Specify the logical and physical sector sizes of the emulated disk. -The physical sector size is optional and is equal to the logical sector size -if not explicitly specified. -.El -.Pp -TTY devices: -.Bl -tag -width 10n -.It Li stdio -Connect the serial port to the standard input and output of -the bhyve process. -.It Pa /dev/xxx -Use the host TTY device for serial port I/O. -.El -.Pp -Pass-through devices: -.Bl -tag -width 10n -.It Ns Ar slot Ns / Ns Ar bus Ns / Ns Ar function -Connect to a PCI device on the host at the selector described by -.Ar slot , -.Ar bus , -and -.Ar function -numbers. -.El -.Pp -The host device must have been reserved at boot-time using the -.Va pptdev -loader variable as described in -.Xr vmm 4 . -.El -.It Fl u -RTC keeps UTC time. -.It Fl U Ar uuid -Set the universally unique identifier -.Pq UUID -in the guest's System Management BIOS System Information structure. -By default a UUID is generated from the host's hostname and -.Ar vmname . -.It Fl w -Ignore accesses to unimplemented Model Specific Registers (MSRs). This is intended for debug purposes. -.It Fl W -Force virtio PCI device emulations to use MSI interrupts instead of MSI-X -interrupts. -.It Fl x -The guest's local APIC is configured in x2APIC mode. -.It Fl Y -Disable MPtable generation. -.It Ar vmname -Alphanumeric name of the guest. -This should be the same as that created by -.Xr bhyveload 8 . -.El -.Sh EXAMPLES -The guest operating system must have been loaded with -.Xr bhyveload 4 -or a similar boot loader before -.Xr bhyve 4 -can be run. -.Pp -To run a virtual machine with 1GB of memory, two virtual CPUs, a virtio -block device backed by the -.Pa /my/image -filesystem image, and a serial port for the console: -.Bd -literal -offset indent -bhyve -c 2 -s 0,hostbridge -s 1,lpc -s 2,virtio-blk,/my/image \\ - -l com1,stdio -A -H -P -m 1G vm1 -.Ed -.Pp -Run a 24GB single-CPU virtual machine with three network ports, one of which -has a MAC address specified: -.Bd -literal -offset indent -bhyve -s 0,hostbridge -s 1,lpc -s 2:0,virtio-net,tap0 \\ - -s 2:1,virtio-net,tap1 \\ - -s 2:2,virtio-net,tap2,mac=00:be:fa:76:45:00 \\ - -s 3,virtio-blk,/my/image -l com1,stdio \\ - -A -H -P -m 24G bigvm -.Ed -.Pp -Run an 8GB quad-CPU virtual machine with 8 AHCI SATA disks, an AHCI ATAPI -CD-ROM, a single virtio network port, an AMD hostbridge, and the console -port connected to an -.Xr nmdm 4 -null-model device. -.Bd -literal -offset indent -bhyve -c 4 \e\ - -s 0,amd_hostbridge -s 1,lpc \\ - -s 1:0,ahci-hd,/images/disk.1 \\ - -s 1:1,ahci-hd,/images/disk.2 \\ - -s 1:2,ahci-hd,/images/disk.3 \\ - -s 1:3,ahci-hd,/images/disk.4 \\ - -s 1:4,ahci-hd,/images/disk.5 \\ - -s 1:5,ahci-hd,/images/disk.6 \\ - -s 1:6,ahci-hd,/images/disk.7 \\ - -s 1:7,ahci-hd,/images/disk.8 \\ - -s 2,ahci-cd,/images.install.iso \\ - -s 3,virtio-net,tap0 \\ - -l com1,/dev/nmdm0A \\ - -A -H -P -m 8G -.Ed -.Sh SEE ALSO -.Xr bhyve 4 , -.Xr nmdm 4 , -.Xr vmm 4 , -.Xr ethers 5 , -.Xr bhyvectl 8 , -.Xr bhyveload 8 -.Sh HISTORY -.Nm -first appeared in -.Fx 10.0 . -.Sh AUTHORS -.An Neel Natu Aq Mt neel@freebsd.org -.An Peter Grehan Aq Mt grehan@freebsd.org diff --git a/bhyve/bhyverun.c b/bhyve/bhyverun.c deleted file mode 100644 index 47a7699..0000000 --- a/bhyve/bhyverun.c +++ /dev/null @@ -1,892 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "bhyverun.h" -#include "acpi.h" -#include "inout.h" -#include "dbgport.h" -#include "ioapic.h" -#include "mem.h" -#include "mevent.h" -#include "mptbl.h" -#include "pci_emul.h" -#include "pci_irq.h" -#include "pci_lpc.h" -#include "smbiostbl.h" -#include "xmsr.h" -#include "spinup_ap.h" -#include "rtc.h" - -#define GUEST_NIO_PORT 0x488 /* guest upcalls via i/o port */ - -#define MB (1024UL * 1024) -#define GB (1024UL * MB) - -typedef int (*vmexit_handler_t)(struct vmctx *, struct vm_exit *, int *vcpu); -extern int vmexit_task_switch(struct vmctx *, struct vm_exit *, int *vcpu); - -char *vmname; - -int guest_ncpus; -char *guest_uuid_str; - -static int guest_vmexit_on_hlt, guest_vmexit_on_pause; -static int virtio_msix = 1; -static int x2apic_mode = 0; /* default is xAPIC */ - -static int strictio; -static int strictmsr = 1; - -static int acpi; - -static char *progname; -static const int BSP = 0; - -static cpuset_t cpumask; - -static void vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip); - -static struct vm_exit vmexit[VM_MAXCPU]; - -struct bhyvestats { - uint64_t vmexit_bogus; - uint64_t vmexit_bogus_switch; - uint64_t vmexit_hlt; - uint64_t vmexit_pause; - uint64_t vmexit_mtrap; - uint64_t vmexit_inst_emul; - uint64_t cpu_switch_rotate; - uint64_t cpu_switch_direct; -} stats; - -struct mt_vmm_info { - pthread_t mt_thr; - struct vmctx *mt_ctx; - int mt_vcpu; -} mt_vmm_info[VM_MAXCPU]; - -static cpuset_t *vcpumap[VM_MAXCPU] = { NULL }; - -static void -usage(int code) -{ - - fprintf(stderr, - "Usage: %s [-abehuwxACHPWY] [-c vcpus] [-g ] [-l ]\n" - " %*s [-m mem] [-p vcpu:hostcpu] [-s ] [-U uuid] \n" - " -a: local apic is in xAPIC mode (deprecated)\n" - " -A: create ACPI tables\n" - " -c: # cpus (default 1)\n" - " -C: include guest memory in core file\n" - " -e: exit on unhandled I/O access\n" - " -g: gdb port\n" - " -h: help\n" - " -H: vmexit from the guest on hlt\n" - " -l: LPC device configuration\n" - " -m: memory size in MB\n" - " -p: pin 'vcpu' to 'hostcpu'\n" - " -P: vmexit from the guest on pause\n" - " -s: PCI slot config\n" - " -u: RTC keeps UTC time\n" - " -U: uuid\n" - " -w: ignore unimplemented MSRs\n" - " -W: force virtio to use single-vector MSI\n" - " -x: local apic is in x2APIC mode\n" - " -Y: disable MPtable generation\n", - progname, (int)strlen(progname), ""); - - exit(code); -} - -static int -pincpu_parse(const char *opt) -{ - int vcpu, pcpu; - - if (sscanf(opt, "%d:%d", &vcpu, &pcpu) != 2) { - fprintf(stderr, "invalid format: %s\n", opt); - return (-1); - } - - if (vcpu < 0 || vcpu >= VM_MAXCPU) { - fprintf(stderr, "vcpu '%d' outside valid range from 0 to %d\n", - vcpu, VM_MAXCPU - 1); - return (-1); - } - - if (pcpu < 0 || pcpu >= CPU_SETSIZE) { - fprintf(stderr, "hostcpu '%d' outside valid range from " - "0 to %d\n", pcpu, CPU_SETSIZE - 1); - return (-1); - } - - if (vcpumap[vcpu] == NULL) { - if ((vcpumap[vcpu] = malloc(sizeof(cpuset_t))) == NULL) { - perror("malloc"); - return (-1); - } - CPU_ZERO(vcpumap[vcpu]); - } - CPU_SET(pcpu, vcpumap[vcpu]); - return (0); -} - -void -vm_inject_fault(void *arg, int vcpu, int vector, int errcode_valid, - int errcode) -{ - struct vmctx *ctx; - int error, restart_instruction; - - ctx = arg; - restart_instruction = 1; - - error = vm_inject_exception(ctx, vcpu, vector, errcode_valid, errcode, - restart_instruction); - assert(error == 0); -} - -void * -paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len) -{ - - return (vm_map_gpa(ctx, gaddr, len)); -} - -int -fbsdrun_vmexit_on_pause(void) -{ - - return (guest_vmexit_on_pause); -} - -int -fbsdrun_vmexit_on_hlt(void) -{ - - return (guest_vmexit_on_hlt); -} - -int -fbsdrun_virtio_msix(void) -{ - - return (virtio_msix); -} - -static void * -fbsdrun_start_thread(void *param) -{ - char tname[MAXCOMLEN + 1]; - struct mt_vmm_info *mtp; - int vcpu; - - mtp = param; - vcpu = mtp->mt_vcpu; - - snprintf(tname, sizeof(tname), "vcpu %d", vcpu); - pthread_set_name_np(mtp->mt_thr, tname); - - vm_loop(mtp->mt_ctx, vcpu, vmexit[vcpu].rip); - - /* not reached */ - exit(1); - return (NULL); -} - -void -fbsdrun_addcpu(struct vmctx *ctx, int fromcpu, int newcpu, uint64_t rip) -{ - int error; - - assert(fromcpu == BSP); - - /* - * The 'newcpu' must be activated in the context of 'fromcpu'. If - * vm_activate_cpu() is delayed until newcpu's pthread starts running - * then vmm.ko is out-of-sync with bhyve and this can create a race - * with vm_suspend(). - */ - error = vm_activate_cpu(ctx, newcpu); - assert(error == 0); - - CPU_SET_ATOMIC(newcpu, &cpumask); - - /* - * Set up the vmexit struct to allow execution to start - * at the given RIP - */ - vmexit[newcpu].rip = rip; - vmexit[newcpu].inst_length = 0; - - mt_vmm_info[newcpu].mt_ctx = ctx; - mt_vmm_info[newcpu].mt_vcpu = newcpu; - - error = pthread_create(&mt_vmm_info[newcpu].mt_thr, NULL, - fbsdrun_start_thread, &mt_vmm_info[newcpu]); - assert(error == 0); -} - -static int -fbsdrun_deletecpu(struct vmctx *ctx, int vcpu) -{ - - if (!CPU_ISSET(vcpu, &cpumask)) { - fprintf(stderr, "Attempting to delete unknown cpu %d\n", vcpu); - exit(1); - } - - CPU_CLR_ATOMIC(vcpu, &cpumask); - return (CPU_EMPTY(&cpumask)); -} - -static int -vmexit_handle_notify(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu, - uint32_t eax) -{ -#if BHYVE_DEBUG - /* - * put guest-driven debug here - */ -#endif - return (VMEXIT_CONTINUE); -} - -static int -vmexit_inout(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu) -{ - int error; - int bytes, port, in, out, string; - int vcpu; - - vcpu = *pvcpu; - - port = vme->u.inout.port; - bytes = vme->u.inout.bytes; - string = vme->u.inout.string; - in = vme->u.inout.in; - out = !in; - - /* Extra-special case of host notifications */ - if (out && port == GUEST_NIO_PORT) { - error = vmexit_handle_notify(ctx, vme, pvcpu, vme->u.inout.eax); - return (error); - } - - error = emulate_inout(ctx, vcpu, vme, strictio); - if (error) { - fprintf(stderr, "Unhandled %s%c 0x%04x at 0x%lx\n", - in ? "in" : "out", - bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'), - port, vmexit->rip); - return (VMEXIT_ABORT); - } else { - return (VMEXIT_CONTINUE); - } -} - -static int -vmexit_rdmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu) -{ - uint64_t val; - uint32_t eax, edx; - int error; - - val = 0; - error = emulate_rdmsr(ctx, *pvcpu, vme->u.msr.code, &val); - if (error != 0) { - fprintf(stderr, "rdmsr to register %#x on vcpu %d\n", - vme->u.msr.code, *pvcpu); - if (strictmsr) { - vm_inject_gp(ctx, *pvcpu); - return (VMEXIT_CONTINUE); - } - } - - eax = val; - error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RAX, eax); - assert(error == 0); - - edx = val >> 32; - error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RDX, edx); - assert(error == 0); - - return (VMEXIT_CONTINUE); -} - -static int -vmexit_wrmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu) -{ - int error; - - error = emulate_wrmsr(ctx, *pvcpu, vme->u.msr.code, vme->u.msr.wval); - if (error != 0) { - fprintf(stderr, "wrmsr to register %#x(%#lx) on vcpu %d\n", - vme->u.msr.code, vme->u.msr.wval, *pvcpu); - if (strictmsr) { - vm_inject_gp(ctx, *pvcpu); - return (VMEXIT_CONTINUE); - } - } - return (VMEXIT_CONTINUE); -} - -static int -vmexit_spinup_ap(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu) -{ - int newcpu; - int retval = VMEXIT_CONTINUE; - - newcpu = spinup_ap(ctx, *pvcpu, - vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip); - - return (retval); -} - -#define DEBUG_EPT_MISCONFIG -#ifdef DEBUG_EPT_MISCONFIG -#define EXIT_REASON_EPT_MISCONFIG 49 -#define VMCS_GUEST_PHYSICAL_ADDRESS 0x00002400 -#define VMCS_IDENT(x) ((x) | 0x80000000) - -static uint64_t ept_misconfig_gpa, ept_misconfig_pte[4]; -static int ept_misconfig_ptenum; -#endif - -static int -vmexit_vmx(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) -{ - - fprintf(stderr, "vm exit[%d]\n", *pvcpu); - fprintf(stderr, "\treason\t\tVMX\n"); - fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip); - fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length); - fprintf(stderr, "\tstatus\t\t%d\n", vmexit->u.vmx.status); - fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason); - fprintf(stderr, "\tqualification\t0x%016lx\n", - vmexit->u.vmx.exit_qualification); - fprintf(stderr, "\tinst_type\t\t%d\n", vmexit->u.vmx.inst_type); - fprintf(stderr, "\tinst_error\t\t%d\n", vmexit->u.vmx.inst_error); -#ifdef DEBUG_EPT_MISCONFIG - if (vmexit->u.vmx.exit_reason == EXIT_REASON_EPT_MISCONFIG) { - vm_get_register(ctx, *pvcpu, - VMCS_IDENT(VMCS_GUEST_PHYSICAL_ADDRESS), - &ept_misconfig_gpa); - vm_get_gpa_pmap(ctx, ept_misconfig_gpa, ept_misconfig_pte, - &ept_misconfig_ptenum); - fprintf(stderr, "\tEPT misconfiguration:\n"); - fprintf(stderr, "\t\tGPA: %#lx\n", ept_misconfig_gpa); - fprintf(stderr, "\t\tPTE(%d): %#lx %#lx %#lx %#lx\n", - ept_misconfig_ptenum, ept_misconfig_pte[0], - ept_misconfig_pte[1], ept_misconfig_pte[2], - ept_misconfig_pte[3]); - } -#endif /* DEBUG_EPT_MISCONFIG */ - return (VMEXIT_ABORT); -} - -static int -vmexit_svm(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) -{ - - fprintf(stderr, "vm exit[%d]\n", *pvcpu); - fprintf(stderr, "\treason\t\tSVM\n"); - fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip); - fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length); - fprintf(stderr, "\texitcode\t%#lx\n", vmexit->u.svm.exitcode); - fprintf(stderr, "\texitinfo1\t%#lx\n", vmexit->u.svm.exitinfo1); - fprintf(stderr, "\texitinfo2\t%#lx\n", vmexit->u.svm.exitinfo2); - return (VMEXIT_ABORT); -} - -static int -vmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) -{ - - assert(vmexit->inst_length == 0); - - stats.vmexit_bogus++; - - return (VMEXIT_CONTINUE); -} - -static int -vmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) -{ - - stats.vmexit_hlt++; - - /* - * Just continue execution with the next instruction. We use - * the HLT VM exit as a way to be friendly with the host - * scheduler. - */ - return (VMEXIT_CONTINUE); -} - -static int -vmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) -{ - - stats.vmexit_pause++; - - return (VMEXIT_CONTINUE); -} - -static int -vmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) -{ - - assert(vmexit->inst_length == 0); - - stats.vmexit_mtrap++; - - return (VMEXIT_CONTINUE); -} - -static int -vmexit_inst_emul(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) -{ - int err, i; - struct vie *vie; - - stats.vmexit_inst_emul++; - - vie = &vmexit->u.inst_emul.vie; - err = emulate_mem(ctx, *pvcpu, vmexit->u.inst_emul.gpa, - vie, &vmexit->u.inst_emul.paging); - - if (err) { - if (err == ESRCH) { - fprintf(stderr, "Unhandled memory access to 0x%lx\n", - vmexit->u.inst_emul.gpa); - } - - fprintf(stderr, "Failed to emulate instruction ["); - for (i = 0; i < vie->num_valid; i++) { - fprintf(stderr, "0x%02x%s", vie->inst[i], - i != (vie->num_valid - 1) ? " " : ""); - } - fprintf(stderr, "] at 0x%lx\n", vmexit->rip); - return (VMEXIT_ABORT); - } - - return (VMEXIT_CONTINUE); -} - -static pthread_mutex_t resetcpu_mtx = PTHREAD_MUTEX_INITIALIZER; -static pthread_cond_t resetcpu_cond = PTHREAD_COND_INITIALIZER; - -static int -vmexit_suspend(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) -{ - enum vm_suspend_how how; - - how = vmexit->u.suspended.how; - - fbsdrun_deletecpu(ctx, *pvcpu); - - if (*pvcpu != BSP) { - pthread_mutex_lock(&resetcpu_mtx); - pthread_cond_signal(&resetcpu_cond); - pthread_mutex_unlock(&resetcpu_mtx); - pthread_exit(NULL); - } - - pthread_mutex_lock(&resetcpu_mtx); - while (!CPU_EMPTY(&cpumask)) { - pthread_cond_wait(&resetcpu_cond, &resetcpu_mtx); - } - pthread_mutex_unlock(&resetcpu_mtx); - - switch (how) { - case VM_SUSPEND_RESET: - exit(0); - case VM_SUSPEND_POWEROFF: - exit(1); - case VM_SUSPEND_HALT: - exit(2); - case VM_SUSPEND_TRIPLEFAULT: - exit(3); - default: - fprintf(stderr, "vmexit_suspend: invalid reason %d\n", how); - exit(100); - } - return (0); /* NOTREACHED */ -} - -static vmexit_handler_t handler[VM_EXITCODE_MAX] = { - [VM_EXITCODE_INOUT] = vmexit_inout, - [VM_EXITCODE_INOUT_STR] = vmexit_inout, - [VM_EXITCODE_VMX] = vmexit_vmx, - [VM_EXITCODE_SVM] = vmexit_svm, - [VM_EXITCODE_BOGUS] = vmexit_bogus, - [VM_EXITCODE_RDMSR] = vmexit_rdmsr, - [VM_EXITCODE_WRMSR] = vmexit_wrmsr, - [VM_EXITCODE_MTRAP] = vmexit_mtrap, - [VM_EXITCODE_INST_EMUL] = vmexit_inst_emul, - [VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap, - [VM_EXITCODE_SUSPENDED] = vmexit_suspend, - [VM_EXITCODE_TASK_SWITCH] = vmexit_task_switch, -}; - -static void -vm_loop(struct vmctx *ctx, int vcpu, uint64_t startrip) -{ - int error, rc, prevcpu; - enum vm_exitcode exitcode; - cpuset_t active_cpus; - - if (vcpumap[vcpu] != NULL) { - error = pthread_setaffinity_np(pthread_self(), - sizeof(cpuset_t), vcpumap[vcpu]); - assert(error == 0); - } - - error = vm_active_cpus(ctx, &active_cpus); - assert(CPU_ISSET(vcpu, &active_cpus)); - - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RIP, startrip); - assert(error == 0); - - while (1) { - error = vm_run(ctx, vcpu, &vmexit[vcpu]); - if (error != 0) - break; - - prevcpu = vcpu; - - exitcode = vmexit[vcpu].exitcode; - if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) { - fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n", - exitcode); - exit(1); - } - - rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu); - - switch (rc) { - case VMEXIT_CONTINUE: - break; - case VMEXIT_ABORT: - abort(); - default: - exit(1); - } - } - fprintf(stderr, "vm_run error %d, errno %d\n", error, errno); -} - -static int -num_vcpus_allowed(struct vmctx *ctx) -{ - int tmp, error; - - error = vm_get_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, &tmp); - - /* - * The guest is allowed to spinup more than one processor only if the - * UNRESTRICTED_GUEST capability is available. - */ - if (error == 0) - return (VM_MAXCPU); - else - return (1); -} - -void -fbsdrun_set_capabilities(struct vmctx *ctx, int cpu) -{ - int err, tmp; - - if (fbsdrun_vmexit_on_hlt()) { - err = vm_get_capability(ctx, cpu, VM_CAP_HALT_EXIT, &tmp); - if (err < 0) { - fprintf(stderr, "VM exit on HLT not supported\n"); - exit(1); - } - vm_set_capability(ctx, cpu, VM_CAP_HALT_EXIT, 1); - if (cpu == BSP) - handler[VM_EXITCODE_HLT] = vmexit_hlt; - } - - if (fbsdrun_vmexit_on_pause()) { - /* - * pause exit support required for this mode - */ - err = vm_get_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, &tmp); - if (err < 0) { - fprintf(stderr, - "SMP mux requested, no pause support\n"); - exit(1); - } - vm_set_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, 1); - if (cpu == BSP) - handler[VM_EXITCODE_PAUSE] = vmexit_pause; - } - - if (x2apic_mode) - err = vm_set_x2apic_state(ctx, cpu, X2APIC_ENABLED); - else - err = vm_set_x2apic_state(ctx, cpu, X2APIC_DISABLED); - - if (err) { - fprintf(stderr, "Unable to set x2apic state (%d)\n", err); - exit(1); - } - - vm_set_capability(ctx, cpu, VM_CAP_ENABLE_INVPCID, 1); -} - -int -main(int argc, char *argv[]) -{ - int c, error, gdb_port, err, bvmcons; - int dump_guest_memory, max_vcpus, mptgen; - int rtc_localtime; - struct vmctx *ctx; - uint64_t rip; - size_t memsize; - - bvmcons = 0; - dump_guest_memory = 0; - progname = basename(argv[0]); - gdb_port = 0; - guest_ncpus = 1; - memsize = 256 * MB; - mptgen = 1; - rtc_localtime = 1; - - while ((c = getopt(argc, argv, "abehuwxACHIPWYp:g:c:s:m:l:U:")) != -1) { - switch (c) { - case 'a': - x2apic_mode = 0; - break; - case 'A': - acpi = 1; - break; - case 'b': - bvmcons = 1; - break; - case 'p': - if (pincpu_parse(optarg) != 0) { - errx(EX_USAGE, "invalid vcpu pinning " - "configuration '%s'", optarg); - } - break; - case 'c': - guest_ncpus = atoi(optarg); - break; - case 'C': - dump_guest_memory = 1; - break; - case 'g': - gdb_port = atoi(optarg); - break; - case 'l': - if (lpc_device_parse(optarg) != 0) { - errx(EX_USAGE, "invalid lpc device " - "configuration '%s'", optarg); - } - break; - case 's': - if (pci_parse_slot(optarg) != 0) - exit(1); - else - break; - case 'm': - error = vm_parse_memsize(optarg, &memsize); - if (error) - errx(EX_USAGE, "invalid memsize '%s'", optarg); - break; - case 'H': - guest_vmexit_on_hlt = 1; - break; - case 'I': - /* - * The "-I" option was used to add an ioapic to the - * virtual machine. - * - * An ioapic is now provided unconditionally for each - * virtual machine and this option is now deprecated. - */ - break; - case 'P': - guest_vmexit_on_pause = 1; - break; - case 'e': - strictio = 1; - break; - case 'u': - rtc_localtime = 0; - break; - case 'U': - guest_uuid_str = optarg; - break; - case 'w': - strictmsr = 0; - break; - case 'W': - virtio_msix = 0; - break; - case 'x': - x2apic_mode = 1; - break; - case 'Y': - mptgen = 0; - break; - case 'h': - usage(0); - default: - usage(1); - } - } - argc -= optind; - argv += optind; - - if (argc != 1) - usage(1); - - vmname = argv[0]; - - ctx = vm_open(vmname); - if (ctx == NULL) { - perror("vm_open"); - exit(1); - } - - if (guest_ncpus < 1) { - fprintf(stderr, "Invalid guest vCPUs (%d)\n", guest_ncpus); - exit(1); - } - - max_vcpus = num_vcpus_allowed(ctx); - if (guest_ncpus > max_vcpus) { - fprintf(stderr, "%d vCPUs requested but only %d available\n", - guest_ncpus, max_vcpus); - exit(1); - } - - fbsdrun_set_capabilities(ctx, BSP); - - if (dump_guest_memory) - vm_set_memflags(ctx, VM_MEM_F_INCORE); - err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL); - if (err) { - fprintf(stderr, "Unable to setup memory (%d)\n", err); - exit(1); - } - - error = init_msr(); - if (error) { - fprintf(stderr, "init_msr error %d", error); - exit(1); - } - - init_mem(); - init_inout(); - pci_irq_init(ctx); - ioapic_init(ctx); - - rtc_init(ctx, rtc_localtime); - sci_init(ctx); - - /* - * Exit if a device emulation finds an error in it's initilization - */ - if (init_pci(ctx) != 0) - exit(1); - - if (gdb_port != 0) - init_dbgport(gdb_port); - - if (bvmcons) - init_bvmcons(); - - error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip); - assert(error == 0); - - /* - * build the guest tables, MP etc. - */ - if (mptgen) { - error = mptable_build(ctx, guest_ncpus); - if (error) - exit(1); - } - - error = smbios_build(ctx); - assert(error == 0); - - if (acpi) { - error = acpi_build(ctx, guest_ncpus); - assert(error == 0); - } - - /* - * Change the proc title to include the VM name. - */ - setproctitle("%s", vmname); - - /* - * Add CPU 0 - */ - fbsdrun_addcpu(ctx, BSP, BSP, rip); - - /* - * Head off to the main event dispatch loop - */ - mevent_dispatch(); - - exit(1); -} diff --git a/bhyve/pci_emul.h b/bhyve/pci_emul.h deleted file mode 100644 index 6b8c4e0..0000000 --- a/bhyve/pci_emul.h +++ /dev/null @@ -1,283 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#ifndef _PCI_EMUL_H_ -#define _PCI_EMUL_H_ - -#include -#include -#include -#include - -#include - -#include - -#define PCI_BARMAX PCIR_MAX_BAR_0 /* BAR registers in a Type 0 header */ - -struct vmctx; -struct pci_devinst; -struct memory_region; - -struct pci_devemu { - char *pe_emu; /* Name of device emulation */ - - /* instance creation */ - int (*pe_init)(struct vmctx *, struct pci_devinst *, - char *opts); - - /* ACPI DSDT enumeration */ - void (*pe_write_dsdt)(struct pci_devinst *); - - /* config space read/write callbacks */ - int (*pe_cfgwrite)(struct vmctx *ctx, int vcpu, - struct pci_devinst *pi, int offset, - int bytes, uint32_t val); - int (*pe_cfgread)(struct vmctx *ctx, int vcpu, - struct pci_devinst *pi, int offset, - int bytes, uint32_t *retval); - - /* BAR read/write callbacks */ - void (*pe_barwrite)(struct vmctx *ctx, int vcpu, - struct pci_devinst *pi, int baridx, - uint64_t offset, int size, uint64_t value); - uint64_t (*pe_barread)(struct vmctx *ctx, int vcpu, - struct pci_devinst *pi, int baridx, - uint64_t offset, int size); -}; -#define PCI_EMUL_SET(x) DATA_SET(pci_devemu_set, x); - -enum pcibar_type { - PCIBAR_NONE, - PCIBAR_IO, - PCIBAR_MEM32, - PCIBAR_MEM64, - PCIBAR_MEMHI64 -}; - -struct pcibar { - enum pcibar_type type; /* io or memory */ - uint64_t size; - uint64_t addr; -}; - -#define PI_NAMESZ 40 - -struct msix_table_entry { - uint64_t addr; - uint32_t msg_data; - uint32_t vector_control; -} __packed; - -/* - * In case the structure is modified to hold extra information, use a define - * for the size that should be emulated. - */ -#define MSIX_TABLE_ENTRY_SIZE 16 -#define MAX_MSIX_TABLE_ENTRIES 2048 -#define PBA_SIZE(msgnum) (roundup2((msgnum), 64) / 8) - -enum lintr_stat { - IDLE, - ASSERTED, - PENDING -}; - -struct pci_devinst { - struct pci_devemu *pi_d; - struct vmctx *pi_vmctx; - uint8_t pi_bus, pi_slot, pi_func; - char pi_name[PI_NAMESZ]; - int pi_bar_getsize; - int pi_prevcap; - int pi_capend; - - struct { - int8_t pin; - enum lintr_stat state; - int pirq_pin; - int ioapic_irq; - pthread_mutex_t lock; - } pi_lintr; - - struct { - int enabled; - uint64_t addr; - uint64_t msg_data; - int maxmsgnum; - } pi_msi; - - struct { - int enabled; - int table_bar; - int pba_bar; - uint32_t table_offset; - int table_count; - uint32_t pba_offset; - int pba_size; - int function_mask; - struct msix_table_entry *table; /* allocated at runtime */ - } pi_msix; - - void *pi_arg; /* devemu-private data */ - - u_char pi_cfgdata[PCI_REGMAX + 1]; - struct pcibar pi_bar[PCI_BARMAX + 1]; -}; - -struct msicap { - uint8_t capid; - uint8_t nextptr; - uint16_t msgctrl; - uint32_t addrlo; - uint32_t addrhi; - uint16_t msgdata; -} __packed; - -struct msixcap { - uint8_t capid; - uint8_t nextptr; - uint16_t msgctrl; - uint32_t table_info; /* bar index and offset within it */ - uint32_t pba_info; /* bar index and offset within it */ -} __packed; - -struct pciecap { - uint8_t capid; - uint8_t nextptr; - uint16_t pcie_capabilities; - - uint32_t dev_capabilities; /* all devices */ - uint16_t dev_control; - uint16_t dev_status; - - uint32_t link_capabilities; /* devices with links */ - uint16_t link_control; - uint16_t link_status; - - uint32_t slot_capabilities; /* ports with slots */ - uint16_t slot_control; - uint16_t slot_status; - - uint16_t root_control; /* root ports */ - uint16_t root_capabilities; - uint32_t root_status; - - uint32_t dev_capabilities2; /* all devices */ - uint16_t dev_control2; - uint16_t dev_status2; - - uint32_t link_capabilities2; /* devices with links */ - uint16_t link_control2; - uint16_t link_status2; - - uint32_t slot_capabilities2; /* ports with slots */ - uint16_t slot_control2; - uint16_t slot_status2; -} __packed; - -typedef void (*pci_lintr_cb)(int b, int s, int pin, int pirq_pin, - int ioapic_irq, void *arg); - -int init_pci(struct vmctx *ctx); -void msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset, - int bytes, uint32_t val); -void msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset, - int bytes, uint32_t val); -void pci_callback(void); -int pci_emul_alloc_bar(struct pci_devinst *pdi, int idx, - enum pcibar_type type, uint64_t size); -int pci_emul_alloc_pbar(struct pci_devinst *pdi, int idx, - uint64_t hostbase, enum pcibar_type type, uint64_t size); -int pci_emul_add_msicap(struct pci_devinst *pi, int msgnum); -int pci_emul_add_pciecap(struct pci_devinst *pi, int pcie_device_type); -void pci_generate_msi(struct pci_devinst *pi, int msgnum); -void pci_generate_msix(struct pci_devinst *pi, int msgnum); -void pci_lintr_assert(struct pci_devinst *pi); -void pci_lintr_deassert(struct pci_devinst *pi); -void pci_lintr_request(struct pci_devinst *pi); -int pci_msi_enabled(struct pci_devinst *pi); -int pci_msix_enabled(struct pci_devinst *pi); -int pci_msix_table_bar(struct pci_devinst *pi); -int pci_msix_pba_bar(struct pci_devinst *pi); -int pci_msi_msgnum(struct pci_devinst *pi); -int pci_parse_slot(char *opt); -void pci_populate_msicap(struct msicap *cap, int msgs, int nextptr); -int pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum); -int pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size, - uint64_t value); -uint64_t pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size); -int pci_count_lintr(int bus); -void pci_walk_lintr(int bus, pci_lintr_cb cb, void *arg); -void pci_write_dsdt(void); -uint64_t pci_ecfg_base(void); -int pci_bus_configured(int bus); - -static __inline void -pci_set_cfgdata8(struct pci_devinst *pi, int offset, uint8_t val) -{ - assert(offset <= PCI_REGMAX); - *(uint8_t *)(pi->pi_cfgdata + offset) = val; -} - -static __inline void -pci_set_cfgdata16(struct pci_devinst *pi, int offset, uint16_t val) -{ - assert(offset <= (PCI_REGMAX - 1) && (offset & 1) == 0); - *(uint16_t *)(pi->pi_cfgdata + offset) = val; -} - -static __inline void -pci_set_cfgdata32(struct pci_devinst *pi, int offset, uint32_t val) -{ - assert(offset <= (PCI_REGMAX - 3) && (offset & 3) == 0); - *(uint32_t *)(pi->pi_cfgdata + offset) = val; -} - -static __inline uint8_t -pci_get_cfgdata8(struct pci_devinst *pi, int offset) -{ - assert(offset <= PCI_REGMAX); - return (*(uint8_t *)(pi->pi_cfgdata + offset)); -} - -static __inline uint16_t -pci_get_cfgdata16(struct pci_devinst *pi, int offset) -{ - assert(offset <= (PCI_REGMAX - 1) && (offset & 1) == 0); - return (*(uint16_t *)(pi->pi_cfgdata + offset)); -} - -static __inline uint32_t -pci_get_cfgdata32(struct pci_devinst *pi, int offset) -{ - assert(offset <= (PCI_REGMAX - 3) && (offset & 3) == 0); - return (*(uint32_t *)(pi->pi_cfgdata + offset)); -} - -#endif /* _PCI_EMUL_H_ */ diff --git a/bhyve/pci_passthru.c b/bhyve/pci_passthru.c deleted file mode 100644 index 04d68c4..0000000 --- a/bhyve/pci_passthru.c +++ /dev/null @@ -1,790 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include - -#include -#include - -#include - -#include -#include -#include -#include -#include -#include - -#include -#include -#include "pci_emul.h" -#include "mem.h" - -#ifndef _PATH_DEVPCI -#define _PATH_DEVPCI "/dev/pci" -#endif - -#ifndef _PATH_DEVIO -#define _PATH_DEVIO "/dev/io" -#endif - -#define LEGACY_SUPPORT 1 - -#define MSIX_TABLE_COUNT(ctrl) (((ctrl) & PCIM_MSIXCTRL_TABLE_SIZE) + 1) -#define MSIX_CAPLEN 12 - -static int pcifd = -1; -static int iofd = -1; - -struct passthru_softc { - struct pci_devinst *psc_pi; - struct pcibar psc_bar[PCI_BARMAX + 1]; - struct { - int capoff; - int msgctrl; - int emulated; - } psc_msi; - struct { - int capoff; - } psc_msix; - struct pcisel psc_sel; -}; - -static int -msi_caplen(int msgctrl) -{ - int len; - - len = 10; /* minimum length of msi capability */ - - if (msgctrl & PCIM_MSICTRL_64BIT) - len += 4; - -#if 0 - /* - * Ignore the 'mask' and 'pending' bits in the MSI capability. - * We'll let the guest manipulate them directly. - */ - if (msgctrl & PCIM_MSICTRL_VECTOR) - len += 10; -#endif - - return (len); -} - -static uint32_t -read_config(const struct pcisel *sel, long reg, int width) -{ - struct pci_io pi; - - bzero(&pi, sizeof(pi)); - pi.pi_sel = *sel; - pi.pi_reg = reg; - pi.pi_width = width; - - if (ioctl(pcifd, PCIOCREAD, &pi) < 0) - return (0); /* XXX */ - else - return (pi.pi_data); -} - -static void -write_config(const struct pcisel *sel, long reg, int width, uint32_t data) -{ - struct pci_io pi; - - bzero(&pi, sizeof(pi)); - pi.pi_sel = *sel; - pi.pi_reg = reg; - pi.pi_width = width; - pi.pi_data = data; - - (void)ioctl(pcifd, PCIOCWRITE, &pi); /* XXX */ -} - -#ifdef LEGACY_SUPPORT -static int -passthru_add_msicap(struct pci_devinst *pi, int msgnum, int nextptr) -{ - int capoff, i; - struct msicap msicap; - u_char *capdata; - - pci_populate_msicap(&msicap, msgnum, nextptr); - - /* - * XXX - * Copy the msi capability structure in the last 16 bytes of the - * config space. This is wrong because it could shadow something - * useful to the device. - */ - capoff = 256 - roundup(sizeof(msicap), 4); - capdata = (u_char *)&msicap; - for (i = 0; i < sizeof(msicap); i++) - pci_set_cfgdata8(pi, capoff + i, capdata[i]); - - return (capoff); -} -#endif /* LEGACY_SUPPORT */ - -static int -cfginitmsi(struct passthru_softc *sc) -{ - int i, ptr, capptr, cap, sts, caplen, table_size; - uint32_t u32; - struct pcisel sel; - struct pci_devinst *pi; - struct msixcap msixcap; - uint32_t *msixcap_ptr; - - pi = sc->psc_pi; - sel = sc->psc_sel; - - /* - * Parse the capabilities and cache the location of the MSI - * and MSI-X capabilities. - */ - sts = read_config(&sel, PCIR_STATUS, 2); - if (sts & PCIM_STATUS_CAPPRESENT) { - ptr = read_config(&sel, PCIR_CAP_PTR, 1); - while (ptr != 0 && ptr != 0xff) { - cap = read_config(&sel, ptr + PCICAP_ID, 1); - if (cap == PCIY_MSI) { - /* - * Copy the MSI capability into the config - * space of the emulated pci device - */ - sc->psc_msi.capoff = ptr; - sc->psc_msi.msgctrl = read_config(&sel, - ptr + 2, 2); - sc->psc_msi.emulated = 0; - caplen = msi_caplen(sc->psc_msi.msgctrl); - capptr = ptr; - while (caplen > 0) { - u32 = read_config(&sel, capptr, 4); - pci_set_cfgdata32(pi, capptr, u32); - caplen -= 4; - capptr += 4; - } - } else if (cap == PCIY_MSIX) { - /* - * Copy the MSI-X capability - */ - sc->psc_msix.capoff = ptr; - caplen = 12; - msixcap_ptr = (uint32_t*) &msixcap; - capptr = ptr; - while (caplen > 0) { - u32 = read_config(&sel, capptr, 4); - *msixcap_ptr = u32; - pci_set_cfgdata32(pi, capptr, u32); - caplen -= 4; - capptr += 4; - msixcap_ptr++; - } - } - ptr = read_config(&sel, ptr + PCICAP_NEXTPTR, 1); - } - } - - if (sc->psc_msix.capoff != 0) { - pi->pi_msix.pba_bar = - msixcap.pba_info & PCIM_MSIX_BIR_MASK; - pi->pi_msix.pba_offset = - msixcap.pba_info & ~PCIM_MSIX_BIR_MASK; - pi->pi_msix.table_bar = - msixcap.table_info & PCIM_MSIX_BIR_MASK; - pi->pi_msix.table_offset = - msixcap.table_info & ~PCIM_MSIX_BIR_MASK; - pi->pi_msix.table_count = MSIX_TABLE_COUNT(msixcap.msgctrl); - pi->pi_msix.pba_size = PBA_SIZE(pi->pi_msix.table_count); - - /* Allocate the emulated MSI-X table array */ - table_size = pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE; - pi->pi_msix.table = calloc(1, table_size); - - /* Mask all table entries */ - for (i = 0; i < pi->pi_msix.table_count; i++) { - pi->pi_msix.table[i].vector_control |= - PCIM_MSIX_VCTRL_MASK; - } - } - -#ifdef LEGACY_SUPPORT - /* - * If the passthrough device does not support MSI then craft a - * MSI capability for it. We link the new MSI capability at the - * head of the list of capabilities. - */ - if ((sts & PCIM_STATUS_CAPPRESENT) != 0 && sc->psc_msi.capoff == 0) { - int origptr, msiptr; - origptr = read_config(&sel, PCIR_CAP_PTR, 1); - msiptr = passthru_add_msicap(pi, 1, origptr); - sc->psc_msi.capoff = msiptr; - sc->psc_msi.msgctrl = pci_get_cfgdata16(pi, msiptr + 2); - sc->psc_msi.emulated = 1; - pci_set_cfgdata8(pi, PCIR_CAP_PTR, msiptr); - } -#endif - - /* Make sure one of the capabilities is present */ - if (sc->psc_msi.capoff == 0 && sc->psc_msix.capoff == 0) - return (-1); - else - return (0); -} - -static uint64_t -msix_table_read(struct passthru_softc *sc, uint64_t offset, int size) -{ - struct pci_devinst *pi; - struct msix_table_entry *entry; - uint8_t *src8; - uint16_t *src16; - uint32_t *src32; - uint64_t *src64; - uint64_t data; - size_t entry_offset; - int index; - - pi = sc->psc_pi; - if (offset < pi->pi_msix.table_offset) - return (-1); - - offset -= pi->pi_msix.table_offset; - index = offset / MSIX_TABLE_ENTRY_SIZE; - if (index >= pi->pi_msix.table_count) - return (-1); - - entry = &pi->pi_msix.table[index]; - entry_offset = offset % MSIX_TABLE_ENTRY_SIZE; - - switch(size) { - case 1: - src8 = (uint8_t *)((void *)entry + entry_offset); - data = *src8; - break; - case 2: - src16 = (uint16_t *)((void *)entry + entry_offset); - data = *src16; - break; - case 4: - src32 = (uint32_t *)((void *)entry + entry_offset); - data = *src32; - break; - case 8: - src64 = (uint64_t *)((void *)entry + entry_offset); - data = *src64; - break; - default: - return (-1); - } - - return (data); -} - -static void -msix_table_write(struct vmctx *ctx, int vcpu, struct passthru_softc *sc, - uint64_t offset, int size, uint64_t data) -{ - struct pci_devinst *pi; - struct msix_table_entry *entry; - uint32_t *dest; - size_t entry_offset; - uint32_t vector_control; - int error, index; - - pi = sc->psc_pi; - if (offset < pi->pi_msix.table_offset) - return; - - offset -= pi->pi_msix.table_offset; - index = offset / MSIX_TABLE_ENTRY_SIZE; - if (index >= pi->pi_msix.table_count) - return; - - entry = &pi->pi_msix.table[index]; - entry_offset = offset % MSIX_TABLE_ENTRY_SIZE; - - /* Only 4 byte naturally-aligned writes are supported */ - assert(size == 4); - assert(entry_offset % 4 == 0); - - vector_control = entry->vector_control; - dest = (uint32_t *)((void *)entry + entry_offset); - *dest = data; - /* If MSI-X hasn't been enabled, do nothing */ - if (pi->pi_msix.enabled) { - /* If the entry is masked, don't set it up */ - if ((entry->vector_control & PCIM_MSIX_VCTRL_MASK) == 0 || - (vector_control & PCIM_MSIX_VCTRL_MASK) == 0) { - error = vm_setup_pptdev_msix(ctx, vcpu, - sc->psc_sel.pc_bus, sc->psc_sel.pc_dev, - sc->psc_sel.pc_func, index, entry->addr, - entry->msg_data, entry->vector_control); - } - } -} - -static int -init_msix_table(struct vmctx *ctx, struct passthru_softc *sc, uint64_t base) -{ - int b, s, f; - int error, idx; - size_t len, remaining; - uint32_t table_size, table_offset; - uint32_t pba_size, pba_offset; - vm_paddr_t start; - struct pci_devinst *pi = sc->psc_pi; - - assert(pci_msix_table_bar(pi) >= 0 && pci_msix_pba_bar(pi) >= 0); - - b = sc->psc_sel.pc_bus; - s = sc->psc_sel.pc_dev; - f = sc->psc_sel.pc_func; - - /* - * If the MSI-X table BAR maps memory intended for - * other uses, it is at least assured that the table - * either resides in its own page within the region, - * or it resides in a page shared with only the PBA. - */ - table_offset = rounddown2(pi->pi_msix.table_offset, 4096); - - table_size = pi->pi_msix.table_offset - table_offset; - table_size += pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE; - table_size = roundup2(table_size, 4096); - - if (pi->pi_msix.pba_bar == pi->pi_msix.table_bar) { - pba_offset = pi->pi_msix.pba_offset; - pba_size = pi->pi_msix.pba_size; - if (pba_offset >= table_offset + table_size || - table_offset >= pba_offset + pba_size) { - /* - * The PBA can reside in the same BAR as the MSI-x - * tables as long as it does not overlap with any - * naturally aligned page occupied by the tables. - */ - } else { - /* Need to also emulate the PBA, not supported yet */ - printf("Unsupported MSI-X configuration: %d/%d/%d\n", - b, s, f); - return (-1); - } - } - - idx = pi->pi_msix.table_bar; - start = pi->pi_bar[idx].addr; - remaining = pi->pi_bar[idx].size; - - /* Map everything before the MSI-X table */ - if (table_offset > 0) { - len = table_offset; - error = vm_map_pptdev_mmio(ctx, b, s, f, start, len, base); - if (error) - return (error); - - base += len; - start += len; - remaining -= len; - } - - /* Skip the MSI-X table */ - base += table_size; - start += table_size; - remaining -= table_size; - - /* Map everything beyond the end of the MSI-X table */ - if (remaining > 0) { - len = remaining; - error = vm_map_pptdev_mmio(ctx, b, s, f, start, len, base); - if (error) - return (error); - } - - return (0); -} - -static int -cfginitbar(struct vmctx *ctx, struct passthru_softc *sc) -{ - int i, error; - struct pci_devinst *pi; - struct pci_bar_io bar; - enum pcibar_type bartype; - uint64_t base, size; - - pi = sc->psc_pi; - - /* - * Initialize BAR registers - */ - for (i = 0; i <= PCI_BARMAX; i++) { - bzero(&bar, sizeof(bar)); - bar.pbi_sel = sc->psc_sel; - bar.pbi_reg = PCIR_BAR(i); - - if (ioctl(pcifd, PCIOCGETBAR, &bar) < 0) - continue; - - if (PCI_BAR_IO(bar.pbi_base)) { - bartype = PCIBAR_IO; - base = bar.pbi_base & PCIM_BAR_IO_BASE; - } else { - switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) { - case PCIM_BAR_MEM_64: - bartype = PCIBAR_MEM64; - break; - default: - bartype = PCIBAR_MEM32; - break; - } - base = bar.pbi_base & PCIM_BAR_MEM_BASE; - } - size = bar.pbi_length; - - if (bartype != PCIBAR_IO) { - if (((base | size) & PAGE_MASK) != 0) { - printf("passthru device %d/%d/%d BAR %d: " - "base %#lx or size %#lx not page aligned\n", - sc->psc_sel.pc_bus, sc->psc_sel.pc_dev, - sc->psc_sel.pc_func, i, base, size); - return (-1); - } - } - - /* Cache information about the "real" BAR */ - sc->psc_bar[i].type = bartype; - sc->psc_bar[i].size = size; - sc->psc_bar[i].addr = base; - - /* Allocate the BAR in the guest I/O or MMIO space */ - error = pci_emul_alloc_pbar(pi, i, base, bartype, size); - if (error) - return (-1); - - /* The MSI-X table needs special handling */ - if (i == pci_msix_table_bar(pi)) { - error = init_msix_table(ctx, sc, base); - if (error) - return (-1); - } else if (bartype != PCIBAR_IO) { - /* Map the physical BAR in the guest MMIO space */ - error = vm_map_pptdev_mmio(ctx, sc->psc_sel.pc_bus, - sc->psc_sel.pc_dev, sc->psc_sel.pc_func, - pi->pi_bar[i].addr, pi->pi_bar[i].size, base); - if (error) - return (-1); - } - - /* - * 64-bit BAR takes up two slots so skip the next one. - */ - if (bartype == PCIBAR_MEM64) { - i++; - assert(i <= PCI_BARMAX); - sc->psc_bar[i].type = PCIBAR_MEMHI64; - } - } - return (0); -} - -static int -cfginit(struct vmctx *ctx, struct pci_devinst *pi, int bus, int slot, int func) -{ - int error; - struct passthru_softc *sc; - - error = 1; - sc = pi->pi_arg; - - bzero(&sc->psc_sel, sizeof(struct pcisel)); - sc->psc_sel.pc_bus = bus; - sc->psc_sel.pc_dev = slot; - sc->psc_sel.pc_func = func; - - if (cfginitmsi(sc) != 0) - goto done; - - if (cfginitbar(ctx, sc) != 0) - goto done; - - error = 0; /* success */ -done: - return (error); -} - -static int -passthru_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) -{ - int bus, slot, func, error; - struct passthru_softc *sc; - - sc = NULL; - error = 1; - - if (pcifd < 0) { - pcifd = open(_PATH_DEVPCI, O_RDWR, 0); - if (pcifd < 0) - goto done; - } - - if (iofd < 0) { - iofd = open(_PATH_DEVIO, O_RDWR, 0); - if (iofd < 0) - goto done; - } - - if (opts == NULL || - sscanf(opts, "%d/%d/%d", &bus, &slot, &func) != 3) - goto done; - - if (vm_assign_pptdev(ctx, bus, slot, func) != 0) - goto done; - - sc = calloc(1, sizeof(struct passthru_softc)); - - pi->pi_arg = sc; - sc->psc_pi = pi; - - /* initialize config space */ - if ((error = cfginit(ctx, pi, bus, slot, func)) != 0) - goto done; - - error = 0; /* success */ -done: - if (error) { - free(sc); - vm_unassign_pptdev(ctx, bus, slot, func); - } - return (error); -} - -static int -bar_access(int coff) -{ - if (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1)) - return (1); - else - return (0); -} - -static int -msicap_access(struct passthru_softc *sc, int coff) -{ - int caplen; - - if (sc->psc_msi.capoff == 0) - return (0); - - caplen = msi_caplen(sc->psc_msi.msgctrl); - - if (coff >= sc->psc_msi.capoff && coff < sc->psc_msi.capoff + caplen) - return (1); - else - return (0); -} - -static int -msixcap_access(struct passthru_softc *sc, int coff) -{ - if (sc->psc_msix.capoff == 0) - return (0); - - return (coff >= sc->psc_msix.capoff && - coff < sc->psc_msix.capoff + MSIX_CAPLEN); -} - -static int -passthru_cfgread(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, - int coff, int bytes, uint32_t *rv) -{ - struct passthru_softc *sc; - - sc = pi->pi_arg; - - /* - * PCI BARs and MSI capability is emulated. - */ - if (bar_access(coff) || msicap_access(sc, coff)) - return (-1); - -#ifdef LEGACY_SUPPORT - /* - * Emulate PCIR_CAP_PTR if this device does not support MSI capability - * natively. - */ - if (sc->psc_msi.emulated) { - if (coff >= PCIR_CAP_PTR && coff < PCIR_CAP_PTR + 4) - return (-1); - } -#endif - - /* Everything else just read from the device's config space */ - *rv = read_config(&sc->psc_sel, coff, bytes); - - return (0); -} - -static int -passthru_cfgwrite(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, - int coff, int bytes, uint32_t val) -{ - int error, msix_table_entries, i; - struct passthru_softc *sc; - - sc = pi->pi_arg; - - /* - * PCI BARs are emulated - */ - if (bar_access(coff)) - return (-1); - - /* - * MSI capability is emulated - */ - if (msicap_access(sc, coff)) { - msicap_cfgwrite(pi, sc->psc_msi.capoff, coff, bytes, val); - - error = vm_setup_pptdev_msi(ctx, vcpu, sc->psc_sel.pc_bus, - sc->psc_sel.pc_dev, sc->psc_sel.pc_func, - pi->pi_msi.addr, pi->pi_msi.msg_data, - pi->pi_msi.maxmsgnum); - if (error != 0) { - printf("vm_setup_pptdev_msi error %d\r\n", errno); - exit(1); - } - return (0); - } - - if (msixcap_access(sc, coff)) { - msixcap_cfgwrite(pi, sc->psc_msix.capoff, coff, bytes, val); - if (pi->pi_msix.enabled) { - msix_table_entries = pi->pi_msix.table_count; - for (i = 0; i < msix_table_entries; i++) { - error = vm_setup_pptdev_msix(ctx, vcpu, - sc->psc_sel.pc_bus, sc->psc_sel.pc_dev, - sc->psc_sel.pc_func, i, - pi->pi_msix.table[i].addr, - pi->pi_msix.table[i].msg_data, - pi->pi_msix.table[i].vector_control); - - if (error) { - printf("vm_setup_pptdev_msix error " - "%d\r\n", errno); - exit(1); - } - } - } - return (0); - } - -#ifdef LEGACY_SUPPORT - /* - * If this device does not support MSI natively then we cannot let - * the guest disable legacy interrupts from the device. It is the - * legacy interrupt that is triggering the virtual MSI to the guest. - */ - if (sc->psc_msi.emulated && pci_msi_enabled(pi)) { - if (coff == PCIR_COMMAND && bytes == 2) - val &= ~PCIM_CMD_INTxDIS; - } -#endif - - write_config(&sc->psc_sel, coff, bytes, val); - - return (0); -} - -static void -passthru_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, - uint64_t offset, int size, uint64_t value) -{ - struct passthru_softc *sc; - struct iodev_pio_req pio; - - sc = pi->pi_arg; - - if (baridx == pci_msix_table_bar(pi)) { - msix_table_write(ctx, vcpu, sc, offset, size, value); - } else { - assert(pi->pi_bar[baridx].type == PCIBAR_IO); - bzero(&pio, sizeof(struct iodev_pio_req)); - pio.access = IODEV_PIO_WRITE; - pio.port = sc->psc_bar[baridx].addr + offset; - pio.width = size; - pio.val = value; - - (void)ioctl(iofd, IODEV_PIO, &pio); - } -} - -static uint64_t -passthru_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, - uint64_t offset, int size) -{ - struct passthru_softc *sc; - struct iodev_pio_req pio; - uint64_t val; - - sc = pi->pi_arg; - - if (baridx == pci_msix_table_bar(pi)) { - val = msix_table_read(sc, offset, size); - } else { - assert(pi->pi_bar[baridx].type == PCIBAR_IO); - bzero(&pio, sizeof(struct iodev_pio_req)); - pio.access = IODEV_PIO_READ; - pio.port = sc->psc_bar[baridx].addr + offset; - pio.width = size; - pio.val = 0; - - (void)ioctl(iofd, IODEV_PIO, &pio); - - val = pio.val; - } - - return (val); -} - -struct pci_devemu passthru = { - .pe_emu = "passthru", - .pe_init = passthru_init, - .pe_cfgwrite = passthru_cfgwrite, - .pe_cfgread = passthru_cfgread, - .pe_barwrite = passthru_write, - .pe_barread = passthru_read, -}; -PCI_EMUL_SET(passthru); diff --git a/bhyve/spinup_ap.c b/bhyve/spinup_ap.c deleted file mode 100644 index c597023..0000000 --- a/bhyve/spinup_ap.c +++ /dev/null @@ -1,104 +0,0 @@ -/*- - * Copyright (c) 2012 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include - -#include -#include - -#include -#include -#include - -#include "bhyverun.h" -#include "spinup_ap.h" - -static void -spinup_ap_realmode(struct vmctx *ctx, int newcpu, uint64_t *rip) -{ - int vector, error; - uint16_t cs; - uint64_t desc_base; - uint32_t desc_limit, desc_access; - - vector = *rip >> PAGE_SHIFT; - *rip = 0; - - /* - * Update the %cs and %rip of the guest so that it starts - * executing real mode code at at 'vector << 12'. - */ - error = vm_set_register(ctx, newcpu, VM_REG_GUEST_RIP, *rip); - assert(error == 0); - - error = vm_get_desc(ctx, newcpu, VM_REG_GUEST_CS, &desc_base, - &desc_limit, &desc_access); - assert(error == 0); - - desc_base = vector << PAGE_SHIFT; - error = vm_set_desc(ctx, newcpu, VM_REG_GUEST_CS, - desc_base, desc_limit, desc_access); - assert(error == 0); - - cs = (vector << PAGE_SHIFT) >> 4; - error = vm_set_register(ctx, newcpu, VM_REG_GUEST_CS, cs); - assert(error == 0); -} - -int -spinup_ap(struct vmctx *ctx, int vcpu, int newcpu, uint64_t rip) -{ - int error; - - assert(newcpu != 0); - assert(newcpu < guest_ncpus); - - error = vcpu_reset(ctx, newcpu); - assert(error == 0); - - fbsdrun_set_capabilities(ctx, newcpu); - - /* - * Enable the 'unrestricted guest' mode for 'newcpu'. - * - * Set up the processor state in power-on 16-bit mode, with the CS:IP - * init'd to the specified low-mem 4K page. - */ - error = vm_set_capability(ctx, newcpu, VM_CAP_UNRESTRICTED_GUEST, 1); - assert(error == 0); - - spinup_ap_realmode(ctx, newcpu, &rip); - - fbsdrun_addcpu(ctx, vcpu, newcpu, rip); - - return (newcpu); -} diff --git a/bhyve/xmsr.c b/bhyve/xmsr.c deleted file mode 100644 index 5b7bfbb..0000000 --- a/bhyve/xmsr.c +++ /dev/null @@ -1,230 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include - -#include -#include -#include - -#include - -#include -#include -#include - -#include "xmsr.h" - -static int cpu_vendor_intel, cpu_vendor_amd; - -int -emulate_wrmsr(struct vmctx *ctx, int vcpu, uint32_t num, uint64_t val) -{ - - if (cpu_vendor_intel) { - switch (num) { - case 0xd04: /* Sandy Bridge uncore PMCs */ - case 0xc24: - return (0); - case MSR_BIOS_UPDT_TRIG: - return (0); - case MSR_BIOS_SIGN: - return (0); - default: - break; - } - } else if (cpu_vendor_amd) { - switch (num) { - case MSR_HWCR: - /* - * Ignore writes to hardware configuration MSR. - */ - return (0); - - case MSR_NB_CFG1: - case MSR_IC_CFG: - return (0); /* Ignore writes */ - - case MSR_PERFEVSEL0: - case MSR_PERFEVSEL1: - case MSR_PERFEVSEL2: - case MSR_PERFEVSEL3: - /* Ignore writes to the PerfEvtSel MSRs */ - return (0); - - case MSR_K7_PERFCTR0: - case MSR_K7_PERFCTR1: - case MSR_K7_PERFCTR2: - case MSR_K7_PERFCTR3: - /* Ignore writes to the PerfCtr MSRs */ - return (0); - - case MSR_P_STATE_CONTROL: - /* Ignore write to change the P-state */ - return (0); - - default: - break; - } - } - return (-1); -} - -int -emulate_rdmsr(struct vmctx *ctx, int vcpu, uint32_t num, uint64_t *val) -{ - int error = 0; - - if (cpu_vendor_intel) { - switch (num) { - case MSR_BIOS_SIGN: - case MSR_IA32_PLATFORM_ID: - case MSR_PKG_ENERGY_STATUS: - case MSR_PP0_ENERGY_STATUS: - case MSR_PP1_ENERGY_STATUS: - case MSR_DRAM_ENERGY_STATUS: - *val = 0; - break; - case MSR_RAPL_POWER_UNIT: - /* - * Use the default value documented in section - * "RAPL Interfaces" in Intel SDM vol3. - */ - *val = 0x000a1003; - break; - default: - error = -1; - break; - } - } else if (cpu_vendor_amd) { - switch (num) { - case MSR_BIOS_SIGN: - *val = 0; - break; - case MSR_HWCR: - /* - * Bios and Kernel Developer's Guides for AMD Families - * 12H, 14H, 15H and 16H. - */ - *val = 0x01000010; /* Reset value */ - *val |= 1 << 9; /* MONITOR/MWAIT disable */ - break; - - case MSR_NB_CFG1: - case MSR_IC_CFG: - /* - * The reset value is processor family dependent so - * just return 0. - */ - *val = 0; - break; - - case MSR_PERFEVSEL0: - case MSR_PERFEVSEL1: - case MSR_PERFEVSEL2: - case MSR_PERFEVSEL3: - /* - * PerfEvtSel MSRs are not properly virtualized so just - * return zero. - */ - *val = 0; - break; - - case MSR_K7_PERFCTR0: - case MSR_K7_PERFCTR1: - case MSR_K7_PERFCTR2: - case MSR_K7_PERFCTR3: - /* - * PerfCtr MSRs are not properly virtualized so just - * return zero. - */ - *val = 0; - break; - - case MSR_SMM_ADDR: - case MSR_SMM_MASK: - /* - * Return the reset value defined in the AMD Bios and - * Kernel Developer's Guide. - */ - *val = 0; - break; - - case MSR_P_STATE_LIMIT: - case MSR_P_STATE_CONTROL: - case MSR_P_STATE_STATUS: - case MSR_P_STATE_CONFIG(0): /* P0 configuration */ - *val = 0; - break; - - /* - * OpenBSD guests test bit 0 of this MSR to detect if the - * workaround for erratum 721 is already applied. - * http://support.amd.com/TechDocs/41322_10h_Rev_Gd.pdf - */ - case 0xC0011029: - *val = 1; - break; - - default: - error = -1; - break; - } - } else { - error = -1; - } - return (error); -} - -int -init_msr(void) -{ - int error; - u_int regs[4]; - char cpu_vendor[13]; - - do_cpuid(0, regs); - ((u_int *)&cpu_vendor)[0] = regs[1]; - ((u_int *)&cpu_vendor)[1] = regs[3]; - ((u_int *)&cpu_vendor)[2] = regs[2]; - cpu_vendor[12] = '\0'; - - error = 0; - if (strcmp(cpu_vendor, "AuthenticAMD") == 0) { - cpu_vendor_amd = 1; - } else if (strcmp(cpu_vendor, "GenuineIntel") == 0) { - cpu_vendor_intel = 1; - } else { - fprintf(stderr, "Unknown cpu vendor \"%s\"\n", cpu_vendor); - error = -1; - } - return (error); -} diff --git a/bhyvectl/Makefile b/bhyvectl/Makefile deleted file mode 100644 index dba3f12..0000000 --- a/bhyvectl/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -# -# $FreeBSD$ -# - -PROG= bhyvectl -SRCS= bhyvectl.c - -MAN= - -LIBADD= vmmapi - -WARNS?= 3 - -CFLAGS+= -I${.CURDIR}/../../sys/amd64/vmm - -.include diff --git a/bhyvectl/bhyvectl.c b/bhyvectl/bhyvectl.c deleted file mode 100644 index 223ee25..0000000 --- a/bhyvectl/bhyvectl.c +++ /dev/null @@ -1,2142 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "amd/vmcb.h" -#include "intel/vmcs.h" - -#define MB (1UL << 20) -#define GB (1UL << 30) - -#define REQ_ARG required_argument -#define NO_ARG no_argument -#define OPT_ARG optional_argument - -static const char *progname; - -static void -usage(bool cpu_intel) -{ - - (void)fprintf(stderr, - "Usage: %s --vm=\n" - " [--cpu=]\n" - " [--create]\n" - " [--destroy]\n" - " [--get-all]\n" - " [--get-stats]\n" - " [--set-desc-ds]\n" - " [--get-desc-ds]\n" - " [--set-desc-es]\n" - " [--get-desc-es]\n" - " [--set-desc-gs]\n" - " [--get-desc-gs]\n" - " [--set-desc-fs]\n" - " [--get-desc-fs]\n" - " [--set-desc-cs]\n" - " [--get-desc-cs]\n" - " [--set-desc-ss]\n" - " [--get-desc-ss]\n" - " [--set-desc-tr]\n" - " [--get-desc-tr]\n" - " [--set-desc-ldtr]\n" - " [--get-desc-ldtr]\n" - " [--set-desc-gdtr]\n" - " [--get-desc-gdtr]\n" - " [--set-desc-idtr]\n" - " [--get-desc-idtr]\n" - " [--run]\n" - " [--capname=]\n" - " [--getcap]\n" - " [--setcap=<0|1>]\n" - " [--desc-base=]\n" - " [--desc-limit=]\n" - " [--desc-access=]\n" - " [--set-cr0=]\n" - " [--get-cr0]\n" - " [--set-cr3=]\n" - " [--get-cr3]\n" - " [--set-cr4=]\n" - " [--get-cr4]\n" - " [--set-dr7=]\n" - " [--get-dr7]\n" - " [--set-rsp=]\n" - " [--get-rsp]\n" - " [--set-rip=]\n" - " [--get-rip]\n" - " [--get-rax]\n" - " [--set-rax=]\n" - " [--get-rbx]\n" - " [--get-rcx]\n" - " [--get-rdx]\n" - " [--get-rsi]\n" - " [--get-rdi]\n" - " [--get-rbp]\n" - " [--get-r8]\n" - " [--get-r9]\n" - " [--get-r10]\n" - " [--get-r11]\n" - " [--get-r12]\n" - " [--get-r13]\n" - " [--get-r14]\n" - " [--get-r15]\n" - " [--set-rflags=]\n" - " [--get-rflags]\n" - " [--set-cs]\n" - " [--get-cs]\n" - " [--set-ds]\n" - " [--get-ds]\n" - " [--set-es]\n" - " [--get-es]\n" - " [--set-fs]\n" - " [--get-fs]\n" - " [--set-gs]\n" - " [--get-gs]\n" - " [--set-ss]\n" - " [--get-ss]\n" - " [--get-tr]\n" - " [--get-ldtr]\n" - " [--set-x2apic-state=]\n" - " [--get-x2apic-state]\n" - " [--unassign-pptdev=]\n" - " [--set-mem=]\n" - " [--get-lowmem]\n" - " [--get-highmem]\n" - " [--get-gpa-pmap]\n" - " [--assert-lapic-lvt=]\n" - " [--inject-nmi]\n" - " [--force-reset]\n" - " [--force-poweroff]\n" - " [--get-rtc-time]\n" - " [--set-rtc-time=]\n" - " [--get-rtc-nvram]\n" - " [--set-rtc-nvram=]\n" - " [--rtc-nvram-offset=]\n" - " [--get-active-cpus]\n" - " [--get-suspended-cpus]\n" - " [--get-intinfo]\n" - " [--get-eptp]\n" - " [--set-exception-bitmap]\n" - " [--get-exception-bitmap]\n" - " [--get-tsc-offset]\n" - " [--get-guest-pat]\n" - " [--get-io-bitmap-address]\n" - " [--get-msr-bitmap]\n" - " [--get-msr-bitmap-address]\n" - " [--get-guest-sysenter]\n" - " [--get-exit-reason]\n", - progname); - - if (cpu_intel) { - (void)fprintf(stderr, - " [--get-vmcs-pinbased-ctls]\n" - " [--get-vmcs-procbased-ctls]\n" - " [--get-vmcs-procbased-ctls2]\n" - " [--get-vmcs-entry-interruption-info]\n" - " [--set-vmcs-entry-interruption-info=]\n" - " [--get-vmcs-guest-physical-address\n" - " [--get-vmcs-guest-linear-address\n" - " [--get-vmcs-host-pat]\n" - " [--get-vmcs-host-cr0]\n" - " [--get-vmcs-host-cr3]\n" - " [--get-vmcs-host-cr4]\n" - " [--get-vmcs-host-rip]\n" - " [--get-vmcs-host-rsp]\n" - " [--get-vmcs-cr0-mask]\n" - " [--get-vmcs-cr0-shadow]\n" - " [--get-vmcs-cr4-mask]\n" - " [--get-vmcs-cr4-shadow]\n" - " [--get-vmcs-cr3-targets]\n" - " [--get-vmcs-apic-access-address]\n" - " [--get-vmcs-virtual-apic-address]\n" - " [--get-vmcs-tpr-threshold]\n" - " [--get-vmcs-vpid]\n" - " [--get-vmcs-instruction-error]\n" - " [--get-vmcs-exit-ctls]\n" - " [--get-vmcs-entry-ctls]\n" - " [--get-vmcs-link]\n" - " [--get-vmcs-exit-qualification]\n" - " [--get-vmcs-exit-interruption-info]\n" - " [--get-vmcs-exit-interruption-error]\n" - " [--get-vmcs-interruptibility]\n" - ); - } else { - (void)fprintf(stderr, - " [--get-vmcb-intercepts]\n" - " [--get-vmcb-asid]\n" - " [--get-vmcb-exit-details]\n" - " [--get-vmcb-tlb-ctrl]\n" - " [--get-vmcb-virq]\n" - " [--get-avic-apic-bar]\n" - " [--get-avic-backing-page]\n" - " [--get-avic-table]\n" - ); - } - exit(1); -} - -static int get_rtc_time, set_rtc_time; -static int get_rtc_nvram, set_rtc_nvram; -static int rtc_nvram_offset; -static uint8_t rtc_nvram_value; -static time_t rtc_secs; - -static int get_stats, getcap, setcap, capval, get_gpa_pmap; -static int inject_nmi, assert_lapic_lvt; -static int force_reset, force_poweroff; -static const char *capname; -static int create, destroy, get_lowmem, get_highmem; -static int get_intinfo; -static int get_active_cpus, get_suspended_cpus; -static uint64_t memsize; -static int set_cr0, get_cr0, set_cr3, get_cr3, set_cr4, get_cr4; -static int set_efer, get_efer; -static int set_dr7, get_dr7; -static int set_rsp, get_rsp, set_rip, get_rip, set_rflags, get_rflags; -static int set_rax, get_rax; -static int get_rbx, get_rcx, get_rdx, get_rsi, get_rdi, get_rbp; -static int get_r8, get_r9, get_r10, get_r11, get_r12, get_r13, get_r14, get_r15; -static int set_desc_ds, get_desc_ds; -static int set_desc_es, get_desc_es; -static int set_desc_fs, get_desc_fs; -static int set_desc_gs, get_desc_gs; -static int set_desc_cs, get_desc_cs; -static int set_desc_ss, get_desc_ss; -static int set_desc_gdtr, get_desc_gdtr; -static int set_desc_idtr, get_desc_idtr; -static int set_desc_tr, get_desc_tr; -static int set_desc_ldtr, get_desc_ldtr; -static int set_cs, set_ds, set_es, set_fs, set_gs, set_ss, set_tr, set_ldtr; -static int get_cs, get_ds, get_es, get_fs, get_gs, get_ss, get_tr, get_ldtr; -static int set_x2apic_state, get_x2apic_state; -enum x2apic_state x2apic_state; -static int unassign_pptdev, bus, slot, func; -static int run; - -/* - * VMCB specific. - */ -static int get_vmcb_intercept, get_vmcb_exit_details, get_vmcb_tlb_ctrl; -static int get_vmcb_virq, get_avic_table; - -/* - * VMCS-specific fields - */ -static int get_pinbased_ctls, get_procbased_ctls, get_procbased_ctls2; -static int get_eptp, get_io_bitmap, get_tsc_offset; -static int get_vmcs_entry_interruption_info, set_vmcs_entry_interruption_info; -static int get_vmcs_interruptibility; -uint32_t vmcs_entry_interruption_info; -static int get_vmcs_gpa, get_vmcs_gla; -static int get_exception_bitmap, set_exception_bitmap, exception_bitmap; -static int get_cr0_mask, get_cr0_shadow; -static int get_cr4_mask, get_cr4_shadow; -static int get_cr3_targets; -static int get_apic_access_addr, get_virtual_apic_addr, get_tpr_threshold; -static int get_msr_bitmap, get_msr_bitmap_address; -static int get_vpid_asid; -static int get_inst_err, get_exit_ctls, get_entry_ctls; -static int get_host_cr0, get_host_cr3, get_host_cr4; -static int get_host_rip, get_host_rsp; -static int get_guest_pat, get_host_pat; -static int get_guest_sysenter, get_vmcs_link; -static int get_exit_reason, get_vmcs_exit_qualification; -static int get_vmcs_exit_interruption_info, get_vmcs_exit_interruption_error; - -static uint64_t desc_base; -static uint32_t desc_limit, desc_access; - -static int get_all; - -static void -dump_vm_run_exitcode(struct vm_exit *vmexit, int vcpu) -{ - printf("vm exit[%d]\n", vcpu); - printf("\trip\t\t0x%016lx\n", vmexit->rip); - printf("\tinst_length\t%d\n", vmexit->inst_length); - switch (vmexit->exitcode) { - case VM_EXITCODE_INOUT: - printf("\treason\t\tINOUT\n"); - printf("\tdirection\t%s\n", vmexit->u.inout.in ? "IN" : "OUT"); - printf("\tbytes\t\t%d\n", vmexit->u.inout.bytes); - printf("\tflags\t\t%s%s\n", - vmexit->u.inout.string ? "STRING " : "", - vmexit->u.inout.rep ? "REP " : ""); - printf("\tport\t\t0x%04x\n", vmexit->u.inout.port); - printf("\teax\t\t0x%08x\n", vmexit->u.inout.eax); - break; - case VM_EXITCODE_VMX: - printf("\treason\t\tVMX\n"); - printf("\tstatus\t\t%d\n", vmexit->u.vmx.status); - printf("\texit_reason\t0x%08x (%u)\n", - vmexit->u.vmx.exit_reason, vmexit->u.vmx.exit_reason); - printf("\tqualification\t0x%016lx\n", - vmexit->u.vmx.exit_qualification); - printf("\tinst_type\t\t%d\n", vmexit->u.vmx.inst_type); - printf("\tinst_error\t\t%d\n", vmexit->u.vmx.inst_error); - break; - case VM_EXITCODE_SVM: - printf("\treason\t\tSVM\n"); - printf("\texit_reason\t\t%#lx\n", vmexit->u.svm.exitcode); - printf("\texitinfo1\t\t%#lx\n", vmexit->u.svm.exitinfo1); - printf("\texitinfo2\t\t%#lx\n", vmexit->u.svm.exitinfo2); - break; - default: - printf("*** unknown vm run exitcode %d\n", vmexit->exitcode); - break; - } -} - -/* AMD 6th generation and Intel compatible MSRs */ -#define MSR_AMD6TH_START 0xC0000000 -#define MSR_AMD6TH_END 0xC0001FFF -/* AMD 7th and 8th generation compatible MSRs */ -#define MSR_AMD7TH_START 0xC0010000 -#define MSR_AMD7TH_END 0xC0011FFF - -static const char * -msr_name(uint32_t msr) -{ - static char buf[32]; - - switch(msr) { - case MSR_TSC: - return ("MSR_TSC"); - case MSR_EFER: - return ("MSR_EFER"); - case MSR_STAR: - return ("MSR_STAR"); - case MSR_LSTAR: - return ("MSR_LSTAR"); - case MSR_CSTAR: - return ("MSR_CSTAR"); - case MSR_SF_MASK: - return ("MSR_SF_MASK"); - case MSR_FSBASE: - return ("MSR_FSBASE"); - case MSR_GSBASE: - return ("MSR_GSBASE"); - case MSR_KGSBASE: - return ("MSR_KGSBASE"); - case MSR_SYSENTER_CS_MSR: - return ("MSR_SYSENTER_CS_MSR"); - case MSR_SYSENTER_ESP_MSR: - return ("MSR_SYSENTER_ESP_MSR"); - case MSR_SYSENTER_EIP_MSR: - return ("MSR_SYSENTER_EIP_MSR"); - case MSR_PAT: - return ("MSR_PAT"); - } - snprintf(buf, sizeof(buf), "MSR %#08x", msr); - - return (buf); -} - -static inline void -print_msr_pm(uint64_t msr, int vcpu, int readable, int writeable) -{ - - if (readable || writeable) { - printf("%-20s[%d]\t\t%c%c\n", msr_name(msr), vcpu, - readable ? 'R' : '-', writeable ? 'W' : '-'); - } -} - -/* - * Reference APM vol2, section 15.11 MSR Intercepts. - */ -static void -dump_amd_msr_pm(const char *bitmap, int vcpu) -{ - int byte, bit, readable, writeable; - uint32_t msr; - - for (msr = 0; msr < 0x2000; msr++) { - byte = msr / 4; - bit = (msr % 4) * 2; - - /* Look at MSRs in the range 0x00000000 to 0x00001FFF */ - readable = (bitmap[byte] & (1 << bit)) ? 0 : 1; - writeable = (bitmap[byte] & (2 << bit)) ? 0 : 1; - print_msr_pm(msr, vcpu, readable, writeable); - - /* Look at MSRs in the range 0xC0000000 to 0xC0001FFF */ - byte += 2048; - readable = (bitmap[byte] & (1 << bit)) ? 0 : 1; - writeable = (bitmap[byte] & (2 << bit)) ? 0 : 1; - print_msr_pm(msr + MSR_AMD6TH_START, vcpu, readable, - writeable); - - /* MSR 0xC0010000 to 0xC0011FF is only for AMD */ - byte += 4096; - readable = (bitmap[byte] & (1 << bit)) ? 0 : 1; - writeable = (bitmap[byte] & (2 << bit)) ? 0 : 1; - print_msr_pm(msr + MSR_AMD7TH_START, vcpu, readable, - writeable); - } -} - -/* - * Reference Intel SDM Vol3 Section 24.6.9 MSR-Bitmap Address - */ -static void -dump_intel_msr_pm(const char *bitmap, int vcpu) -{ - int byte, bit, readable, writeable; - uint32_t msr; - - for (msr = 0; msr < 0x2000; msr++) { - byte = msr / 8; - bit = msr & 0x7; - - /* Look at MSRs in the range 0x00000000 to 0x00001FFF */ - readable = (bitmap[byte] & (1 << bit)) ? 0 : 1; - writeable = (bitmap[2048 + byte] & (1 << bit)) ? 0 : 1; - print_msr_pm(msr, vcpu, readable, writeable); - - /* Look at MSRs in the range 0xC0000000 to 0xC0001FFF */ - byte += 1024; - readable = (bitmap[byte] & (1 << bit)) ? 0 : 1; - writeable = (bitmap[2048 + byte] & (1 << bit)) ? 0 : 1; - print_msr_pm(msr + MSR_AMD6TH_START, vcpu, readable, - writeable); - } -} - -static int -dump_msr_bitmap(int vcpu, uint64_t addr, bool cpu_intel) -{ - int error, fd, map_size; - const char *bitmap; - - error = -1; - bitmap = MAP_FAILED; - - fd = open("/dev/mem", O_RDONLY, 0); - if (fd < 0) { - perror("Couldn't open /dev/mem"); - goto done; - } - - if (cpu_intel) - map_size = PAGE_SIZE; - else - map_size = 2 * PAGE_SIZE; - - bitmap = mmap(NULL, map_size, PROT_READ, MAP_SHARED, fd, addr); - if (bitmap == MAP_FAILED) { - perror("mmap failed"); - goto done; - } - - if (cpu_intel) - dump_intel_msr_pm(bitmap, vcpu); - else - dump_amd_msr_pm(bitmap, vcpu); - - error = 0; -done: - if (bitmap != MAP_FAILED) - munmap((void *)bitmap, map_size); - if (fd >= 0) - close(fd); - - return (error); -} - -static int -vm_get_vmcs_field(struct vmctx *ctx, int vcpu, int field, uint64_t *ret_val) -{ - - return (vm_get_register(ctx, vcpu, VMCS_IDENT(field), ret_val)); -} - -static int -vm_set_vmcs_field(struct vmctx *ctx, int vcpu, int field, uint64_t val) -{ - - return (vm_set_register(ctx, vcpu, VMCS_IDENT(field), val)); -} - -static int -vm_get_vmcb_field(struct vmctx *ctx, int vcpu, int off, int bytes, - uint64_t *ret_val) -{ - - return (vm_get_register(ctx, vcpu, VMCB_ACCESS(off, bytes), ret_val)); -} - -static int -vm_set_vmcb_field(struct vmctx *ctx, int vcpu, int off, int bytes, - uint64_t val) -{ - - return (vm_set_register(ctx, vcpu, VMCB_ACCESS(off, bytes), val)); -} - -enum { - VMNAME = 1000, /* avoid collision with return values from getopt */ - VCPU, - SET_MEM, - SET_EFER, - SET_CR0, - SET_CR3, - SET_CR4, - SET_DR7, - SET_RSP, - SET_RIP, - SET_RAX, - SET_RFLAGS, - DESC_BASE, - DESC_LIMIT, - DESC_ACCESS, - SET_CS, - SET_DS, - SET_ES, - SET_FS, - SET_GS, - SET_SS, - SET_TR, - SET_LDTR, - SET_X2APIC_STATE, - SET_EXCEPTION_BITMAP, - SET_VMCS_ENTRY_INTERRUPTION_INFO, - SET_CAP, - CAPNAME, - UNASSIGN_PPTDEV, - GET_GPA_PMAP, - ASSERT_LAPIC_LVT, - SET_RTC_TIME, - SET_RTC_NVRAM, - RTC_NVRAM_OFFSET, -}; - -static void -print_cpus(const char *banner, const cpuset_t *cpus) -{ - int i, first; - - first = 1; - printf("%s:\t", banner); - if (!CPU_EMPTY(cpus)) { - for (i = 0; i < CPU_SETSIZE; i++) { - if (CPU_ISSET(i, cpus)) { - printf("%s%d", first ? " " : ", ", i); - first = 0; - } - } - } else - printf(" (none)"); - printf("\n"); -} - -static void -print_intinfo(const char *banner, uint64_t info) -{ - int type; - - printf("%s:\t", banner); - if (info & VM_INTINFO_VALID) { - type = info & VM_INTINFO_TYPE; - switch (type) { - case VM_INTINFO_HWINTR: - printf("extint"); - break; - case VM_INTINFO_NMI: - printf("nmi"); - break; - case VM_INTINFO_SWINTR: - printf("swint"); - break; - default: - printf("exception"); - break; - } - printf(" vector %d", (int)VM_INTINFO_VECTOR(info)); - if (info & VM_INTINFO_DEL_ERRCODE) - printf(" errcode %#x", (u_int)(info >> 32)); - } else { - printf("n/a"); - } - printf("\n"); -} - -static bool -cpu_vendor_intel(void) -{ - u_int regs[4]; - char cpu_vendor[13]; - - do_cpuid(0, regs); - ((u_int *)&cpu_vendor)[0] = regs[1]; - ((u_int *)&cpu_vendor)[1] = regs[3]; - ((u_int *)&cpu_vendor)[2] = regs[2]; - cpu_vendor[12] = '\0'; - - if (strcmp(cpu_vendor, "AuthenticAMD") == 0) { - return (false); - } else if (strcmp(cpu_vendor, "GenuineIntel") == 0) { - return (true); - } else { - fprintf(stderr, "Unknown cpu vendor \"%s\"\n", cpu_vendor); - exit(1); - } -} - -static int -get_all_registers(struct vmctx *ctx, int vcpu) -{ - uint64_t cr0, cr3, cr4, dr7, rsp, rip, rflags, efer; - uint64_t rax, rbx, rcx, rdx, rsi, rdi, rbp; - uint64_t r8, r9, r10, r11, r12, r13, r14, r15; - int error = 0; - - if (!error && (get_efer || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_EFER, &efer); - if (error == 0) - printf("efer[%d]\t\t0x%016lx\n", vcpu, efer); - } - - if (!error && (get_cr0 || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CR0, &cr0); - if (error == 0) - printf("cr0[%d]\t\t0x%016lx\n", vcpu, cr0); - } - - if (!error && (get_cr3 || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CR3, &cr3); - if (error == 0) - printf("cr3[%d]\t\t0x%016lx\n", vcpu, cr3); - } - - if (!error && (get_cr4 || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CR4, &cr4); - if (error == 0) - printf("cr4[%d]\t\t0x%016lx\n", vcpu, cr4); - } - - if (!error && (get_dr7 || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DR7, &dr7); - if (error == 0) - printf("dr7[%d]\t\t0x%016lx\n", vcpu, dr7); - } - - if (!error && (get_rsp || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RSP, &rsp); - if (error == 0) - printf("rsp[%d]\t\t0x%016lx\n", vcpu, rsp); - } - - if (!error && (get_rip || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RIP, &rip); - if (error == 0) - printf("rip[%d]\t\t0x%016lx\n", vcpu, rip); - } - - if (!error && (get_rax || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RAX, &rax); - if (error == 0) - printf("rax[%d]\t\t0x%016lx\n", vcpu, rax); - } - - if (!error && (get_rbx || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RBX, &rbx); - if (error == 0) - printf("rbx[%d]\t\t0x%016lx\n", vcpu, rbx); - } - - if (!error && (get_rcx || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RCX, &rcx); - if (error == 0) - printf("rcx[%d]\t\t0x%016lx\n", vcpu, rcx); - } - - if (!error && (get_rdx || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RDX, &rdx); - if (error == 0) - printf("rdx[%d]\t\t0x%016lx\n", vcpu, rdx); - } - - if (!error && (get_rsi || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RSI, &rsi); - if (error == 0) - printf("rsi[%d]\t\t0x%016lx\n", vcpu, rsi); - } - - if (!error && (get_rdi || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RDI, &rdi); - if (error == 0) - printf("rdi[%d]\t\t0x%016lx\n", vcpu, rdi); - } - - if (!error && (get_rbp || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RBP, &rbp); - if (error == 0) - printf("rbp[%d]\t\t0x%016lx\n", vcpu, rbp); - } - - if (!error && (get_r8 || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R8, &r8); - if (error == 0) - printf("r8[%d]\t\t0x%016lx\n", vcpu, r8); - } - - if (!error && (get_r9 || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R9, &r9); - if (error == 0) - printf("r9[%d]\t\t0x%016lx\n", vcpu, r9); - } - - if (!error && (get_r10 || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R10, &r10); - if (error == 0) - printf("r10[%d]\t\t0x%016lx\n", vcpu, r10); - } - - if (!error && (get_r11 || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R11, &r11); - if (error == 0) - printf("r11[%d]\t\t0x%016lx\n", vcpu, r11); - } - - if (!error && (get_r12 || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R12, &r12); - if (error == 0) - printf("r12[%d]\t\t0x%016lx\n", vcpu, r12); - } - - if (!error && (get_r13 || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R13, &r13); - if (error == 0) - printf("r13[%d]\t\t0x%016lx\n", vcpu, r13); - } - - if (!error && (get_r14 || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R14, &r14); - if (error == 0) - printf("r14[%d]\t\t0x%016lx\n", vcpu, r14); - } - - if (!error && (get_r15 || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R15, &r15); - if (error == 0) - printf("r15[%d]\t\t0x%016lx\n", vcpu, r15); - } - - if (!error && (get_rflags || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RFLAGS, - &rflags); - if (error == 0) - printf("rflags[%d]\t0x%016lx\n", vcpu, rflags); - } - - return (error); -} - -static int -get_all_segments(struct vmctx *ctx, int vcpu) -{ - uint64_t cs, ds, es, fs, gs, ss, tr, ldtr; - int error = 0; - - if (!error && (get_desc_ds || get_all)) { - error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_DS, - &desc_base, &desc_limit, &desc_access); - if (error == 0) { - printf("ds desc[%d]\t0x%016lx/0x%08x/0x%08x\n", - vcpu, desc_base, desc_limit, desc_access); - } - } - - if (!error && (get_desc_es || get_all)) { - error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_ES, - &desc_base, &desc_limit, &desc_access); - if (error == 0) { - printf("es desc[%d]\t0x%016lx/0x%08x/0x%08x\n", - vcpu, desc_base, desc_limit, desc_access); - } - } - - if (!error && (get_desc_fs || get_all)) { - error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_FS, - &desc_base, &desc_limit, &desc_access); - if (error == 0) { - printf("fs desc[%d]\t0x%016lx/0x%08x/0x%08x\n", - vcpu, desc_base, desc_limit, desc_access); - } - } - - if (!error && (get_desc_gs || get_all)) { - error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_GS, - &desc_base, &desc_limit, &desc_access); - if (error == 0) { - printf("gs desc[%d]\t0x%016lx/0x%08x/0x%08x\n", - vcpu, desc_base, desc_limit, desc_access); - } - } - - if (!error && (get_desc_ss || get_all)) { - error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_SS, - &desc_base, &desc_limit, &desc_access); - if (error == 0) { - printf("ss desc[%d]\t0x%016lx/0x%08x/0x%08x\n", - vcpu, desc_base, desc_limit, desc_access); - } - } - - if (!error && (get_desc_cs || get_all)) { - error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_CS, - &desc_base, &desc_limit, &desc_access); - if (error == 0) { - printf("cs desc[%d]\t0x%016lx/0x%08x/0x%08x\n", - vcpu, desc_base, desc_limit, desc_access); - } - } - - if (!error && (get_desc_tr || get_all)) { - error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_TR, - &desc_base, &desc_limit, &desc_access); - if (error == 0) { - printf("tr desc[%d]\t0x%016lx/0x%08x/0x%08x\n", - vcpu, desc_base, desc_limit, desc_access); - } - } - - if (!error && (get_desc_ldtr || get_all)) { - error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_LDTR, - &desc_base, &desc_limit, &desc_access); - if (error == 0) { - printf("ldtr desc[%d]\t0x%016lx/0x%08x/0x%08x\n", - vcpu, desc_base, desc_limit, desc_access); - } - } - - if (!error && (get_desc_gdtr || get_all)) { - error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_GDTR, - &desc_base, &desc_limit, &desc_access); - if (error == 0) { - printf("gdtr[%d]\t\t0x%016lx/0x%08x\n", - vcpu, desc_base, desc_limit); - } - } - - if (!error && (get_desc_idtr || get_all)) { - error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_IDTR, - &desc_base, &desc_limit, &desc_access); - if (error == 0) { - printf("idtr[%d]\t\t0x%016lx/0x%08x\n", - vcpu, desc_base, desc_limit); - } - } - - if (!error && (get_cs || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CS, &cs); - if (error == 0) - printf("cs[%d]\t\t0x%04lx\n", vcpu, cs); - } - - if (!error && (get_ds || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DS, &ds); - if (error == 0) - printf("ds[%d]\t\t0x%04lx\n", vcpu, ds); - } - - if (!error && (get_es || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_ES, &es); - if (error == 0) - printf("es[%d]\t\t0x%04lx\n", vcpu, es); - } - - if (!error && (get_fs || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_FS, &fs); - if (error == 0) - printf("fs[%d]\t\t0x%04lx\n", vcpu, fs); - } - - if (!error && (get_gs || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_GS, &gs); - if (error == 0) - printf("gs[%d]\t\t0x%04lx\n", vcpu, gs); - } - - if (!error && (get_ss || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_SS, &ss); - if (error == 0) - printf("ss[%d]\t\t0x%04lx\n", vcpu, ss); - } - - if (!error && (get_tr || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_TR, &tr); - if (error == 0) - printf("tr[%d]\t\t0x%04lx\n", vcpu, tr); - } - - if (!error && (get_ldtr || get_all)) { - error = vm_get_register(ctx, vcpu, VM_REG_GUEST_LDTR, &ldtr); - if (error == 0) - printf("ldtr[%d]\t\t0x%04lx\n", vcpu, ldtr); - } - - return (error); -} - -static int -get_misc_vmcs(struct vmctx *ctx, int vcpu) -{ - uint64_t ctl, cr0, cr3, cr4, rsp, rip, pat, addr, u64; - int error = 0; - - if (!error && (get_cr0_mask || get_all)) { - uint64_t cr0mask; - error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR0_MASK, &cr0mask); - if (error == 0) - printf("cr0_mask[%d]\t\t0x%016lx\n", vcpu, cr0mask); - } - - if (!error && (get_cr0_shadow || get_all)) { - uint64_t cr0shadow; - error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR0_SHADOW, - &cr0shadow); - if (error == 0) - printf("cr0_shadow[%d]\t\t0x%016lx\n", vcpu, cr0shadow); - } - - if (!error && (get_cr4_mask || get_all)) { - uint64_t cr4mask; - error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR4_MASK, &cr4mask); - if (error == 0) - printf("cr4_mask[%d]\t\t0x%016lx\n", vcpu, cr4mask); - } - - if (!error && (get_cr4_shadow || get_all)) { - uint64_t cr4shadow; - error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR4_SHADOW, - &cr4shadow); - if (error == 0) - printf("cr4_shadow[%d]\t\t0x%016lx\n", vcpu, cr4shadow); - } - - if (!error && (get_cr3_targets || get_all)) { - uint64_t target_count, target_addr; - error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET_COUNT, - &target_count); - if (error == 0) { - printf("cr3_target_count[%d]\t0x%016lx\n", - vcpu, target_count); - } - - error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET0, - &target_addr); - if (error == 0) { - printf("cr3_target0[%d]\t\t0x%016lx\n", - vcpu, target_addr); - } - - error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET1, - &target_addr); - if (error == 0) { - printf("cr3_target1[%d]\t\t0x%016lx\n", - vcpu, target_addr); - } - - error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET2, - &target_addr); - if (error == 0) { - printf("cr3_target2[%d]\t\t0x%016lx\n", - vcpu, target_addr); - } - - error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET3, - &target_addr); - if (error == 0) { - printf("cr3_target3[%d]\t\t0x%016lx\n", - vcpu, target_addr); - } - } - - if (!error && (get_pinbased_ctls || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, VMCS_PIN_BASED_CTLS, &ctl); - if (error == 0) - printf("pinbased_ctls[%d]\t0x%016lx\n", vcpu, ctl); - } - - if (!error && (get_procbased_ctls || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, - VMCS_PRI_PROC_BASED_CTLS, &ctl); - if (error == 0) - printf("procbased_ctls[%d]\t0x%016lx\n", vcpu, ctl); - } - - if (!error && (get_procbased_ctls2 || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, - VMCS_SEC_PROC_BASED_CTLS, &ctl); - if (error == 0) - printf("procbased_ctls2[%d]\t0x%016lx\n", vcpu, ctl); - } - - if (!error && (get_vmcs_gla || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, - VMCS_GUEST_LINEAR_ADDRESS, &u64); - if (error == 0) - printf("gla[%d]\t\t0x%016lx\n", vcpu, u64); - } - - if (!error && (get_vmcs_gpa || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, - VMCS_GUEST_PHYSICAL_ADDRESS, &u64); - if (error == 0) - printf("gpa[%d]\t\t0x%016lx\n", vcpu, u64); - } - - if (!error && (get_vmcs_entry_interruption_info || - get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, VMCS_ENTRY_INTR_INFO,&u64); - if (error == 0) { - printf("entry_interruption_info[%d]\t0x%016lx\n", - vcpu, u64); - } - } - - if (!error && (get_tpr_threshold || get_all)) { - uint64_t threshold; - error = vm_get_vmcs_field(ctx, vcpu, VMCS_TPR_THRESHOLD, - &threshold); - if (error == 0) - printf("tpr_threshold[%d]\t0x%016lx\n", vcpu, threshold); - } - - if (!error && (get_inst_err || get_all)) { - uint64_t insterr; - error = vm_get_vmcs_field(ctx, vcpu, VMCS_INSTRUCTION_ERROR, - &insterr); - if (error == 0) { - printf("instruction_error[%d]\t0x%016lx\n", - vcpu, insterr); - } - } - - if (!error && (get_exit_ctls || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_CTLS, &ctl); - if (error == 0) - printf("exit_ctls[%d]\t\t0x%016lx\n", vcpu, ctl); - } - - if (!error && (get_entry_ctls || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, VMCS_ENTRY_CTLS, &ctl); - if (error == 0) - printf("entry_ctls[%d]\t\t0x%016lx\n", vcpu, ctl); - } - - if (!error && (get_host_pat || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_IA32_PAT, &pat); - if (error == 0) - printf("host_pat[%d]\t\t0x%016lx\n", vcpu, pat); - } - - if (!error && (get_host_cr0 || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_CR0, &cr0); - if (error == 0) - printf("host_cr0[%d]\t\t0x%016lx\n", vcpu, cr0); - } - - if (!error && (get_host_cr3 || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_CR3, &cr3); - if (error == 0) - printf("host_cr3[%d]\t\t0x%016lx\n", vcpu, cr3); - } - - if (!error && (get_host_cr4 || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_CR4, &cr4); - if (error == 0) - printf("host_cr4[%d]\t\t0x%016lx\n", vcpu, cr4); - } - - if (!error && (get_host_rip || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_RIP, &rip); - if (error == 0) - printf("host_rip[%d]\t\t0x%016lx\n", vcpu, rip); - } - - if (!error && (get_host_rsp || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_RSP, &rsp); - if (error == 0) - printf("host_rsp[%d]\t\t0x%016lx\n", vcpu, rsp); - } - - if (!error && (get_vmcs_link || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, VMCS_LINK_POINTER, &addr); - if (error == 0) - printf("vmcs_pointer[%d]\t0x%016lx\n", vcpu, addr); - } - - if (!error && (get_vmcs_exit_interruption_info || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_INTR_INFO, &u64); - if (error == 0) { - printf("vmcs_exit_interruption_info[%d]\t0x%016lx\n", - vcpu, u64); - } - } - - if (!error && (get_vmcs_exit_interruption_error || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_INTR_ERRCODE, - &u64); - if (error == 0) { - printf("vmcs_exit_interruption_error[%d]\t0x%016lx\n", - vcpu, u64); - } - } - - if (!error && (get_vmcs_interruptibility || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, - VMCS_GUEST_INTERRUPTIBILITY, &u64); - if (error == 0) { - printf("vmcs_guest_interruptibility[%d]\t0x%016lx\n", - vcpu, u64); - } - } - - if (!error && (get_vmcs_exit_qualification || get_all)) { - error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_QUALIFICATION, - &u64); - if (error == 0) - printf("vmcs_exit_qualification[%d]\t0x%016lx\n", - vcpu, u64); - } - - return (error); -} - -static int -get_misc_vmcb(struct vmctx *ctx, int vcpu) -{ - uint64_t ctl, addr; - int error = 0; - - if (!error && (get_vmcb_intercept || get_all)) { - error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_CR_INTERCEPT, 4, - &ctl); - if (error == 0) - printf("cr_intercept[%d]\t0x%08x\n", vcpu, (int)ctl); - - error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_DR_INTERCEPT, 4, - &ctl); - if (error == 0) - printf("dr_intercept[%d]\t0x%08x\n", vcpu, (int)ctl); - - error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_EXC_INTERCEPT, 4, - &ctl); - if (error == 0) - printf("exc_intercept[%d]\t0x%08x\n", vcpu, (int)ctl); - - error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_INST1_INTERCEPT, - 4, &ctl); - if (error == 0) - printf("inst1_intercept[%d]\t0x%08x\n", vcpu, (int)ctl); - - error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_INST2_INTERCEPT, - 4, &ctl); - if (error == 0) - printf("inst2_intercept[%d]\t0x%08x\n", vcpu, (int)ctl); - } - - if (!error && (get_vmcb_tlb_ctrl || get_all)) { - error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_TLB_CTRL, - 4, &ctl); - if (error == 0) - printf("TLB ctrl[%d]\t0x%016lx\n", vcpu, ctl); - } - - if (!error && (get_vmcb_exit_details || get_all)) { - error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_EXITINFO1, - 8, &ctl); - if (error == 0) - printf("exitinfo1[%d]\t0x%016lx\n", vcpu, ctl); - error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_EXITINFO2, - 8, &ctl); - if (error == 0) - printf("exitinfo2[%d]\t0x%016lx\n", vcpu, ctl); - error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_EXITINTINFO, - 8, &ctl); - if (error == 0) - printf("exitintinfo[%d]\t0x%016lx\n", vcpu, ctl); - } - - if (!error && (get_vmcb_virq || get_all)) { - error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_VIRQ, - 8, &ctl); - if (error == 0) - printf("v_irq/tpr[%d]\t0x%016lx\n", vcpu, ctl); - } - - if (!error && (get_apic_access_addr || get_all)) { - error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_AVIC_BAR, 8, - &addr); - if (error == 0) - printf("AVIC apic_bar[%d]\t0x%016lx\n", vcpu, addr); - } - - if (!error && (get_virtual_apic_addr || get_all)) { - error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_AVIC_PAGE, 8, - &addr); - if (error == 0) - printf("AVIC backing page[%d]\t0x%016lx\n", vcpu, addr); - } - - if (!error && (get_avic_table || get_all)) { - error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_AVIC_LT, 8, - &addr); - if (error == 0) - printf("AVIC logical table[%d]\t0x%016lx\n", - vcpu, addr); - error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_AVIC_PT, 8, - &addr); - if (error == 0) - printf("AVIC physical table[%d]\t0x%016lx\n", - vcpu, addr); - } - - return (error); -} - -static struct option * -setup_options(bool cpu_intel) -{ - const struct option common_opts[] = { - { "vm", REQ_ARG, 0, VMNAME }, - { "cpu", REQ_ARG, 0, VCPU }, - { "set-mem", REQ_ARG, 0, SET_MEM }, - { "set-efer", REQ_ARG, 0, SET_EFER }, - { "set-cr0", REQ_ARG, 0, SET_CR0 }, - { "set-cr3", REQ_ARG, 0, SET_CR3 }, - { "set-cr4", REQ_ARG, 0, SET_CR4 }, - { "set-dr7", REQ_ARG, 0, SET_DR7 }, - { "set-rsp", REQ_ARG, 0, SET_RSP }, - { "set-rip", REQ_ARG, 0, SET_RIP }, - { "set-rax", REQ_ARG, 0, SET_RAX }, - { "set-rflags", REQ_ARG, 0, SET_RFLAGS }, - { "desc-base", REQ_ARG, 0, DESC_BASE }, - { "desc-limit", REQ_ARG, 0, DESC_LIMIT }, - { "desc-access",REQ_ARG, 0, DESC_ACCESS }, - { "set-cs", REQ_ARG, 0, SET_CS }, - { "set-ds", REQ_ARG, 0, SET_DS }, - { "set-es", REQ_ARG, 0, SET_ES }, - { "set-fs", REQ_ARG, 0, SET_FS }, - { "set-gs", REQ_ARG, 0, SET_GS }, - { "set-ss", REQ_ARG, 0, SET_SS }, - { "set-tr", REQ_ARG, 0, SET_TR }, - { "set-ldtr", REQ_ARG, 0, SET_LDTR }, - { "set-x2apic-state",REQ_ARG, 0, SET_X2APIC_STATE }, - { "set-exception-bitmap", - REQ_ARG, 0, SET_EXCEPTION_BITMAP }, - { "capname", REQ_ARG, 0, CAPNAME }, - { "unassign-pptdev", REQ_ARG, 0, UNASSIGN_PPTDEV }, - { "setcap", REQ_ARG, 0, SET_CAP }, - { "get-gpa-pmap", REQ_ARG, 0, GET_GPA_PMAP }, - { "assert-lapic-lvt", REQ_ARG, 0, ASSERT_LAPIC_LVT }, - { "get-rtc-time", NO_ARG, &get_rtc_time, 1 }, - { "set-rtc-time", REQ_ARG, 0, SET_RTC_TIME }, - { "rtc-nvram-offset", REQ_ARG, 0, RTC_NVRAM_OFFSET }, - { "get-rtc-nvram", NO_ARG, &get_rtc_nvram, 1 }, - { "set-rtc-nvram", REQ_ARG, 0, SET_RTC_NVRAM }, - { "getcap", NO_ARG, &getcap, 1 }, - { "get-stats", NO_ARG, &get_stats, 1 }, - { "get-desc-ds",NO_ARG, &get_desc_ds, 1 }, - { "set-desc-ds",NO_ARG, &set_desc_ds, 1 }, - { "get-desc-es",NO_ARG, &get_desc_es, 1 }, - { "set-desc-es",NO_ARG, &set_desc_es, 1 }, - { "get-desc-ss",NO_ARG, &get_desc_ss, 1 }, - { "set-desc-ss",NO_ARG, &set_desc_ss, 1 }, - { "get-desc-cs",NO_ARG, &get_desc_cs, 1 }, - { "set-desc-cs",NO_ARG, &set_desc_cs, 1 }, - { "get-desc-fs",NO_ARG, &get_desc_fs, 1 }, - { "set-desc-fs",NO_ARG, &set_desc_fs, 1 }, - { "get-desc-gs",NO_ARG, &get_desc_gs, 1 }, - { "set-desc-gs",NO_ARG, &set_desc_gs, 1 }, - { "get-desc-tr",NO_ARG, &get_desc_tr, 1 }, - { "set-desc-tr",NO_ARG, &set_desc_tr, 1 }, - { "set-desc-ldtr", NO_ARG, &set_desc_ldtr, 1 }, - { "get-desc-ldtr", NO_ARG, &get_desc_ldtr, 1 }, - { "set-desc-gdtr", NO_ARG, &set_desc_gdtr, 1 }, - { "get-desc-gdtr", NO_ARG, &get_desc_gdtr, 1 }, - { "set-desc-idtr", NO_ARG, &set_desc_idtr, 1 }, - { "get-desc-idtr", NO_ARG, &get_desc_idtr, 1 }, - { "get-lowmem", NO_ARG, &get_lowmem, 1 }, - { "get-highmem",NO_ARG, &get_highmem, 1 }, - { "get-efer", NO_ARG, &get_efer, 1 }, - { "get-cr0", NO_ARG, &get_cr0, 1 }, - { "get-cr3", NO_ARG, &get_cr3, 1 }, - { "get-cr4", NO_ARG, &get_cr4, 1 }, - { "get-dr7", NO_ARG, &get_dr7, 1 }, - { "get-rsp", NO_ARG, &get_rsp, 1 }, - { "get-rip", NO_ARG, &get_rip, 1 }, - { "get-rax", NO_ARG, &get_rax, 1 }, - { "get-rbx", NO_ARG, &get_rbx, 1 }, - { "get-rcx", NO_ARG, &get_rcx, 1 }, - { "get-rdx", NO_ARG, &get_rdx, 1 }, - { "get-rsi", NO_ARG, &get_rsi, 1 }, - { "get-rdi", NO_ARG, &get_rdi, 1 }, - { "get-rbp", NO_ARG, &get_rbp, 1 }, - { "get-r8", NO_ARG, &get_r8, 1 }, - { "get-r9", NO_ARG, &get_r9, 1 }, - { "get-r10", NO_ARG, &get_r10, 1 }, - { "get-r11", NO_ARG, &get_r11, 1 }, - { "get-r12", NO_ARG, &get_r12, 1 }, - { "get-r13", NO_ARG, &get_r13, 1 }, - { "get-r14", NO_ARG, &get_r14, 1 }, - { "get-r15", NO_ARG, &get_r15, 1 }, - { "get-rflags", NO_ARG, &get_rflags, 1 }, - { "get-cs", NO_ARG, &get_cs, 1 }, - { "get-ds", NO_ARG, &get_ds, 1 }, - { "get-es", NO_ARG, &get_es, 1 }, - { "get-fs", NO_ARG, &get_fs, 1 }, - { "get-gs", NO_ARG, &get_gs, 1 }, - { "get-ss", NO_ARG, &get_ss, 1 }, - { "get-tr", NO_ARG, &get_tr, 1 }, - { "get-ldtr", NO_ARG, &get_ldtr, 1 }, - { "get-eptp", NO_ARG, &get_eptp, 1 }, - { "get-exception-bitmap", - NO_ARG, &get_exception_bitmap, 1 }, - { "get-io-bitmap-address", - NO_ARG, &get_io_bitmap, 1 }, - { "get-tsc-offset", NO_ARG, &get_tsc_offset, 1 }, - { "get-msr-bitmap", - NO_ARG, &get_msr_bitmap, 1 }, - { "get-msr-bitmap-address", - NO_ARG, &get_msr_bitmap_address, 1 }, - { "get-guest-pat", NO_ARG, &get_guest_pat, 1 }, - { "get-guest-sysenter", - NO_ARG, &get_guest_sysenter, 1 }, - { "get-exit-reason", - NO_ARG, &get_exit_reason, 1 }, - { "get-x2apic-state", NO_ARG, &get_x2apic_state, 1 }, - { "get-all", NO_ARG, &get_all, 1 }, - { "run", NO_ARG, &run, 1 }, - { "create", NO_ARG, &create, 1 }, - { "destroy", NO_ARG, &destroy, 1 }, - { "inject-nmi", NO_ARG, &inject_nmi, 1 }, - { "force-reset", NO_ARG, &force_reset, 1 }, - { "force-poweroff", NO_ARG, &force_poweroff, 1 }, - { "get-active-cpus", NO_ARG, &get_active_cpus, 1 }, - { "get-suspended-cpus", NO_ARG, &get_suspended_cpus, 1 }, - { "get-intinfo", NO_ARG, &get_intinfo, 1 }, - }; - - const struct option intel_opts[] = { - { "get-vmcs-pinbased-ctls", - NO_ARG, &get_pinbased_ctls, 1 }, - { "get-vmcs-procbased-ctls", - NO_ARG, &get_procbased_ctls, 1 }, - { "get-vmcs-procbased-ctls2", - NO_ARG, &get_procbased_ctls2, 1 }, - { "get-vmcs-guest-linear-address", - NO_ARG, &get_vmcs_gla, 1 }, - { "get-vmcs-guest-physical-address", - NO_ARG, &get_vmcs_gpa, 1 }, - { "get-vmcs-entry-interruption-info", - NO_ARG, &get_vmcs_entry_interruption_info, 1}, - { "get-vmcs-cr0-mask", NO_ARG, &get_cr0_mask, 1 }, - { "get-vmcs-cr0-shadow", NO_ARG,&get_cr0_shadow, 1 }, - { "get-vmcs-cr4-mask", NO_ARG, &get_cr4_mask, 1 }, - { "get-vmcs-cr4-shadow", NO_ARG, &get_cr4_shadow, 1 }, - { "get-vmcs-cr3-targets", NO_ARG, &get_cr3_targets, 1 }, - { "get-vmcs-tpr-threshold", - NO_ARG, &get_tpr_threshold, 1 }, - { "get-vmcs-vpid", NO_ARG, &get_vpid_asid, 1 }, - { "get-vmcs-exit-ctls", NO_ARG, &get_exit_ctls, 1 }, - { "get-vmcs-entry-ctls", - NO_ARG, &get_entry_ctls, 1 }, - { "get-vmcs-instruction-error", - NO_ARG, &get_inst_err, 1 }, - { "get-vmcs-host-pat", NO_ARG, &get_host_pat, 1 }, - { "get-vmcs-host-cr0", - NO_ARG, &get_host_cr0, 1 }, - { "set-vmcs-entry-interruption-info", - REQ_ARG, 0, SET_VMCS_ENTRY_INTERRUPTION_INFO }, - { "get-vmcs-exit-qualification", - NO_ARG, &get_vmcs_exit_qualification, 1 }, - { "get-vmcs-interruptibility", - NO_ARG, &get_vmcs_interruptibility, 1 }, - { "get-vmcs-exit-interruption-error", - NO_ARG, &get_vmcs_exit_interruption_error, 1 }, - { "get-vmcs-exit-interruption-info", - NO_ARG, &get_vmcs_exit_interruption_info, 1 }, - { "get-vmcs-link", NO_ARG, &get_vmcs_link, 1 }, - { "get-vmcs-host-cr3", - NO_ARG, &get_host_cr3, 1 }, - { "get-vmcs-host-cr4", - NO_ARG, &get_host_cr4, 1 }, - { "get-vmcs-host-rip", - NO_ARG, &get_host_rip, 1 }, - { "get-vmcs-host-rsp", - NO_ARG, &get_host_rsp, 1 }, - { "get-apic-access-address", - NO_ARG, &get_apic_access_addr, 1}, - { "get-virtual-apic-address", - NO_ARG, &get_virtual_apic_addr, 1} - }; - - const struct option amd_opts[] = { - { "get-vmcb-intercepts", - NO_ARG, &get_vmcb_intercept, 1 }, - { "get-vmcb-asid", - NO_ARG, &get_vpid_asid, 1 }, - { "get-vmcb-exit-details", - NO_ARG, &get_vmcb_exit_details, 1 }, - { "get-vmcb-tlb-ctrl", - NO_ARG, &get_vmcb_tlb_ctrl, 1 }, - { "get-vmcb-virq", - NO_ARG, &get_vmcb_virq, 1 }, - { "get-avic-apic-bar", - NO_ARG, &get_apic_access_addr, 1 }, - { "get-avic-backing-page", - NO_ARG, &get_virtual_apic_addr, 1 }, - { "get-avic-table", - NO_ARG, &get_avic_table, 1 } - }; - - const struct option null_opt = { - NULL, 0, NULL, 0 - }; - - struct option *all_opts; - char *cp; - int optlen; - - optlen = sizeof(common_opts); - - if (cpu_intel) - optlen += sizeof(intel_opts); - else - optlen += sizeof(amd_opts); - - optlen += sizeof(null_opt); - - all_opts = malloc(optlen); - - cp = (char *)all_opts; - memcpy(cp, common_opts, sizeof(common_opts)); - cp += sizeof(common_opts); - - if (cpu_intel) { - memcpy(cp, intel_opts, sizeof(intel_opts)); - cp += sizeof(intel_opts); - } else { - memcpy(cp, amd_opts, sizeof(amd_opts)); - cp += sizeof(amd_opts); - } - - memcpy(cp, &null_opt, sizeof(null_opt)); - cp += sizeof(null_opt); - - return (all_opts); -} - -static const char * -wday_str(int idx) -{ - static const char *weekdays[] = { - "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" - }; - - if (idx >= 0 && idx < 7) - return (weekdays[idx]); - else - return ("UNK"); -} - -static const char * -mon_str(int idx) -{ - static const char *months[] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" - }; - - if (idx >= 0 && idx < 12) - return (months[idx]); - else - return ("UNK"); -} - -int -main(int argc, char *argv[]) -{ - char *vmname; - int error, ch, vcpu, ptenum; - vm_paddr_t gpa, gpa_pmap; - size_t len; - struct vm_exit vmexit; - uint64_t rax, cr0, cr3, cr4, dr7, rsp, rip, rflags, efer, pat; - uint64_t eptp, bm, addr, u64, pteval[4], *pte, info[2]; - struct vmctx *ctx; - int wired; - cpuset_t cpus; - bool cpu_intel; - uint64_t cs, ds, es, fs, gs, ss, tr, ldtr; - struct tm tm; - struct option *opts; - - cpu_intel = cpu_vendor_intel(); - opts = setup_options(cpu_intel); - - vcpu = 0; - vmname = NULL; - assert_lapic_lvt = -1; - progname = basename(argv[0]); - - while ((ch = getopt_long(argc, argv, "", opts, NULL)) != -1) { - switch (ch) { - case 0: - break; - case VMNAME: - vmname = optarg; - break; - case VCPU: - vcpu = atoi(optarg); - break; - case SET_MEM: - memsize = atoi(optarg) * MB; - memsize = roundup(memsize, 2 * MB); - break; - case SET_EFER: - efer = strtoul(optarg, NULL, 0); - set_efer = 1; - break; - case SET_CR0: - cr0 = strtoul(optarg, NULL, 0); - set_cr0 = 1; - break; - case SET_CR3: - cr3 = strtoul(optarg, NULL, 0); - set_cr3 = 1; - break; - case SET_CR4: - cr4 = strtoul(optarg, NULL, 0); - set_cr4 = 1; - break; - case SET_DR7: - dr7 = strtoul(optarg, NULL, 0); - set_dr7 = 1; - break; - case SET_RSP: - rsp = strtoul(optarg, NULL, 0); - set_rsp = 1; - break; - case SET_RIP: - rip = strtoul(optarg, NULL, 0); - set_rip = 1; - break; - case SET_RAX: - rax = strtoul(optarg, NULL, 0); - set_rax = 1; - break; - case SET_RFLAGS: - rflags = strtoul(optarg, NULL, 0); - set_rflags = 1; - break; - case DESC_BASE: - desc_base = strtoul(optarg, NULL, 0); - break; - case DESC_LIMIT: - desc_limit = strtoul(optarg, NULL, 0); - break; - case DESC_ACCESS: - desc_access = strtoul(optarg, NULL, 0); - break; - case SET_CS: - cs = strtoul(optarg, NULL, 0); - set_cs = 1; - break; - case SET_DS: - ds = strtoul(optarg, NULL, 0); - set_ds = 1; - break; - case SET_ES: - es = strtoul(optarg, NULL, 0); - set_es = 1; - break; - case SET_FS: - fs = strtoul(optarg, NULL, 0); - set_fs = 1; - break; - case SET_GS: - gs = strtoul(optarg, NULL, 0); - set_gs = 1; - break; - case SET_SS: - ss = strtoul(optarg, NULL, 0); - set_ss = 1; - break; - case SET_TR: - tr = strtoul(optarg, NULL, 0); - set_tr = 1; - break; - case SET_LDTR: - ldtr = strtoul(optarg, NULL, 0); - set_ldtr = 1; - break; - case SET_X2APIC_STATE: - x2apic_state = strtol(optarg, NULL, 0); - set_x2apic_state = 1; - break; - case SET_EXCEPTION_BITMAP: - exception_bitmap = strtoul(optarg, NULL, 0); - set_exception_bitmap = 1; - break; - case SET_VMCS_ENTRY_INTERRUPTION_INFO: - vmcs_entry_interruption_info = strtoul(optarg, NULL, 0); - set_vmcs_entry_interruption_info = 1; - break; - case SET_CAP: - capval = strtoul(optarg, NULL, 0); - setcap = 1; - break; - case SET_RTC_TIME: - rtc_secs = strtoul(optarg, NULL, 0); - set_rtc_time = 1; - break; - case SET_RTC_NVRAM: - rtc_nvram_value = (uint8_t)strtoul(optarg, NULL, 0); - set_rtc_nvram = 1; - break; - case RTC_NVRAM_OFFSET: - rtc_nvram_offset = strtoul(optarg, NULL, 0); - break; - case GET_GPA_PMAP: - gpa_pmap = strtoul(optarg, NULL, 0); - get_gpa_pmap = 1; - break; - case CAPNAME: - capname = optarg; - break; - case UNASSIGN_PPTDEV: - unassign_pptdev = 1; - if (sscanf(optarg, "%d/%d/%d", &bus, &slot, &func) != 3) - usage(cpu_intel); - break; - case ASSERT_LAPIC_LVT: - assert_lapic_lvt = atoi(optarg); - break; - default: - usage(cpu_intel); - } - } - argc -= optind; - argv += optind; - - if (vmname == NULL) - usage(cpu_intel); - - error = 0; - - if (!error && create) - error = vm_create(vmname); - - if (!error) { - ctx = vm_open(vmname); - if (ctx == NULL) { - printf("VM:%s is not created.\n", vmname); - exit (1); - } - } - - if (!error && memsize) - error = vm_setup_memory(ctx, memsize, VM_MMAP_NONE); - - if (!error && set_efer) - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_EFER, efer); - - if (!error && set_cr0) - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CR0, cr0); - - if (!error && set_cr3) - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CR3, cr3); - - if (!error && set_cr4) - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CR4, cr4); - - if (!error && set_dr7) - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DR7, dr7); - - if (!error && set_rsp) - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RSP, rsp); - - if (!error && set_rip) - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RIP, rip); - - if (!error && set_rax) - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX, rax); - - if (!error && set_rflags) { - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RFLAGS, - rflags); - } - - if (!error && set_desc_ds) { - error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_DS, - desc_base, desc_limit, desc_access); - } - - if (!error && set_desc_es) { - error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_ES, - desc_base, desc_limit, desc_access); - } - - if (!error && set_desc_ss) { - error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_SS, - desc_base, desc_limit, desc_access); - } - - if (!error && set_desc_cs) { - error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_CS, - desc_base, desc_limit, desc_access); - } - - if (!error && set_desc_fs) { - error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_FS, - desc_base, desc_limit, desc_access); - } - - if (!error && set_desc_gs) { - error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_GS, - desc_base, desc_limit, desc_access); - } - - if (!error && set_desc_tr) { - error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_TR, - desc_base, desc_limit, desc_access); - } - - if (!error && set_desc_ldtr) { - error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_LDTR, - desc_base, desc_limit, desc_access); - } - - if (!error && set_desc_gdtr) { - error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_GDTR, - desc_base, desc_limit, 0); - } - - if (!error && set_desc_idtr) { - error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_IDTR, - desc_base, desc_limit, 0); - } - - if (!error && set_cs) - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CS, cs); - - if (!error && set_ds) - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DS, ds); - - if (!error && set_es) - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_ES, es); - - if (!error && set_fs) - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_FS, fs); - - if (!error && set_gs) - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_GS, gs); - - if (!error && set_ss) - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_SS, ss); - - if (!error && set_tr) - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_TR, tr); - - if (!error && set_ldtr) - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_LDTR, ldtr); - - if (!error && set_x2apic_state) - error = vm_set_x2apic_state(ctx, vcpu, x2apic_state); - - if (!error && unassign_pptdev) - error = vm_unassign_pptdev(ctx, bus, slot, func); - - if (!error && set_exception_bitmap) { - if (cpu_intel) - error = vm_set_vmcs_field(ctx, vcpu, - VMCS_EXCEPTION_BITMAP, - exception_bitmap); - else - error = vm_set_vmcb_field(ctx, vcpu, - VMCB_OFF_EXC_INTERCEPT, - 4, exception_bitmap); - } - - if (!error && cpu_intel && set_vmcs_entry_interruption_info) { - error = vm_set_vmcs_field(ctx, vcpu, VMCS_ENTRY_INTR_INFO, - vmcs_entry_interruption_info); - } - - if (!error && inject_nmi) { - error = vm_inject_nmi(ctx, vcpu); - } - - if (!error && assert_lapic_lvt != -1) { - error = vm_lapic_local_irq(ctx, vcpu, assert_lapic_lvt); - } - - if (!error && (get_lowmem || get_all)) { - gpa = 0; - error = vm_get_memory_seg(ctx, gpa, &len, &wired); - if (error == 0) - printf("lowmem\t\t0x%016lx/%ld%s\n", gpa, len, - wired ? " wired" : ""); - } - - if (!error && (get_highmem || get_all)) { - gpa = 4 * GB; - error = vm_get_memory_seg(ctx, gpa, &len, &wired); - if (error == 0) - printf("highmem\t\t0x%016lx/%ld%s\n", gpa, len, - wired ? " wired" : ""); - } - - if (!error) - error = get_all_registers(ctx, vcpu); - - if (!error) - error = get_all_segments(ctx, vcpu); - - if (!error) { - if (cpu_intel) - error = get_misc_vmcs(ctx, vcpu); - else - error = get_misc_vmcb(ctx, vcpu); - } - - if (!error && (get_x2apic_state || get_all)) { - error = vm_get_x2apic_state(ctx, vcpu, &x2apic_state); - if (error == 0) - printf("x2apic_state[%d]\t%d\n", vcpu, x2apic_state); - } - - if (!error && (get_eptp || get_all)) { - if (cpu_intel) - error = vm_get_vmcs_field(ctx, vcpu, VMCS_EPTP, &eptp); - else - error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_NPT_BASE, - 8, &eptp); - if (error == 0) - printf("%s[%d]\t\t0x%016lx\n", - cpu_intel ? "eptp" : "rvi/npt", vcpu, eptp); - } - - if (!error && (get_exception_bitmap || get_all)) { - if(cpu_intel) - error = vm_get_vmcs_field(ctx, vcpu, - VMCS_EXCEPTION_BITMAP, &bm); - else - error = vm_get_vmcb_field(ctx, vcpu, - VMCB_OFF_EXC_INTERCEPT, - 4, &bm); - if (error == 0) - printf("exception_bitmap[%d]\t%#lx\n", vcpu, bm); - } - - if (!error && (get_io_bitmap || get_all)) { - if (cpu_intel) { - error = vm_get_vmcs_field(ctx, vcpu, VMCS_IO_BITMAP_A, - &bm); - if (error == 0) - printf("io_bitmap_a[%d]\t%#lx\n", vcpu, bm); - error = vm_get_vmcs_field(ctx, vcpu, VMCS_IO_BITMAP_B, - &bm); - if (error == 0) - printf("io_bitmap_b[%d]\t%#lx\n", vcpu, bm); - } else { - error = vm_get_vmcb_field(ctx, vcpu, - VMCB_OFF_IO_PERM, 8, &bm); - if (error == 0) - printf("io_bitmap[%d]\t%#lx\n", vcpu, bm); - } - } - - if (!error && (get_tsc_offset || get_all)) { - uint64_t tscoff; - if (cpu_intel) - error = vm_get_vmcs_field(ctx, vcpu, VMCS_TSC_OFFSET, - &tscoff); - else - error = vm_get_vmcb_field(ctx, vcpu, - VMCB_OFF_TSC_OFFSET, - 8, &tscoff); - if (error == 0) - printf("tsc_offset[%d]\t0x%016lx\n", vcpu, tscoff); - } - - if (!error && (get_msr_bitmap_address || get_all)) { - if (cpu_intel) - error = vm_get_vmcs_field(ctx, vcpu, VMCS_MSR_BITMAP, - &addr); - else - error = vm_get_vmcb_field(ctx, vcpu, - VMCB_OFF_MSR_PERM, 8, &addr); - if (error == 0) - printf("msr_bitmap[%d]\t\t%#lx\n", vcpu, addr); - } - - if (!error && (get_msr_bitmap || get_all)) { - if (cpu_intel) { - error = vm_get_vmcs_field(ctx, vcpu, - VMCS_MSR_BITMAP, &addr); - } else { - error = vm_get_vmcb_field(ctx, vcpu, - VMCB_OFF_MSR_PERM, 8, - &addr); - } - - if (error == 0) - error = dump_msr_bitmap(vcpu, addr, cpu_intel); - } - - if (!error && (get_vpid_asid || get_all)) { - uint64_t vpid; - if (cpu_intel) - error = vm_get_vmcs_field(ctx, vcpu, VMCS_VPID, &vpid); - else - error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_ASID, - 4, &vpid); - if (error == 0) - printf("%s[%d]\t\t0x%04lx\n", - cpu_intel ? "vpid" : "asid", vcpu, vpid); - } - - if (!error && (get_guest_pat || get_all)) { - if (cpu_intel) - error = vm_get_vmcs_field(ctx, vcpu, - VMCS_GUEST_IA32_PAT, &pat); - else - error = vm_get_vmcb_field(ctx, vcpu, - VMCB_OFF_GUEST_PAT, 8, &pat); - if (error == 0) - printf("guest_pat[%d]\t\t0x%016lx\n", vcpu, pat); - } - - if (!error && (get_guest_sysenter || get_all)) { - if (cpu_intel) - error = vm_get_vmcs_field(ctx, vcpu, - VMCS_GUEST_IA32_SYSENTER_CS, - &cs); - else - error = vm_get_vmcb_field(ctx, vcpu, - VMCB_OFF_SYSENTER_CS, 8, - &cs); - - if (error == 0) - printf("guest_sysenter_cs[%d]\t%#lx\n", vcpu, cs); - if (cpu_intel) - error = vm_get_vmcs_field(ctx, vcpu, - VMCS_GUEST_IA32_SYSENTER_ESP, - &rsp); - else - error = vm_get_vmcb_field(ctx, vcpu, - VMCB_OFF_SYSENTER_ESP, 8, - &rsp); - - if (error == 0) - printf("guest_sysenter_sp[%d]\t%#lx\n", vcpu, rsp); - if (cpu_intel) - error = vm_get_vmcs_field(ctx, vcpu, - VMCS_GUEST_IA32_SYSENTER_EIP, - &rip); - else - error = vm_get_vmcb_field(ctx, vcpu, - VMCB_OFF_SYSENTER_EIP, 8, - &rip); - if (error == 0) - printf("guest_sysenter_ip[%d]\t%#lx\n", vcpu, rip); - } - - if (!error && (get_exit_reason || get_all)) { - if (cpu_intel) - error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_REASON, - &u64); - else - error = vm_get_vmcb_field(ctx, vcpu, - VMCB_OFF_EXIT_REASON, 8, - &u64); - if (error == 0) - printf("exit_reason[%d]\t%#lx\n", vcpu, u64); - } - - if (!error && setcap) { - int captype; - captype = vm_capability_name2type(capname); - error = vm_set_capability(ctx, vcpu, captype, capval); - if (error != 0 && errno == ENOENT) - printf("Capability \"%s\" is not available\n", capname); - } - - if (!error && get_gpa_pmap) { - error = vm_get_gpa_pmap(ctx, gpa_pmap, pteval, &ptenum); - if (error == 0) { - printf("gpa %#lx:", gpa_pmap); - pte = &pteval[0]; - while (ptenum-- > 0) - printf(" %#lx", *pte++); - printf("\n"); - } - } - - if (!error && set_rtc_nvram) - error = vm_rtc_write(ctx, rtc_nvram_offset, rtc_nvram_value); - - if (!error && (get_rtc_nvram || get_all)) { - error = vm_rtc_read(ctx, rtc_nvram_offset, &rtc_nvram_value); - if (error == 0) { - printf("rtc nvram[%03d]: 0x%02x\n", rtc_nvram_offset, - rtc_nvram_value); - } - } - - if (!error && set_rtc_time) - error = vm_rtc_settime(ctx, rtc_secs); - - if (!error && (get_rtc_time || get_all)) { - error = vm_rtc_gettime(ctx, &rtc_secs); - if (error == 0) { - gmtime_r(&rtc_secs, &tm); - printf("rtc time %#lx: %s %s %02d %02d:%02d:%02d %d\n", - rtc_secs, wday_str(tm.tm_wday), mon_str(tm.tm_mon), - tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, - 1900 + tm.tm_year); - } - } - - if (!error && (getcap || get_all)) { - int captype, val, getcaptype; - - if (getcap && capname) - getcaptype = vm_capability_name2type(capname); - else - getcaptype = -1; - - for (captype = 0; captype < VM_CAP_MAX; captype++) { - if (getcaptype >= 0 && captype != getcaptype) - continue; - error = vm_get_capability(ctx, vcpu, captype, &val); - if (error == 0) { - printf("Capability \"%s\" is %s on vcpu %d\n", - vm_capability_type2name(captype), - val ? "set" : "not set", vcpu); - } else if (errno == ENOENT) { - error = 0; - printf("Capability \"%s\" is not available\n", - vm_capability_type2name(captype)); - } else { - break; - } - } - } - - if (!error && (get_active_cpus || get_all)) { - error = vm_active_cpus(ctx, &cpus); - if (!error) - print_cpus("active cpus", &cpus); - } - - if (!error && (get_suspended_cpus || get_all)) { - error = vm_suspended_cpus(ctx, &cpus); - if (!error) - print_cpus("suspended cpus", &cpus); - } - - if (!error && (get_intinfo || get_all)) { - error = vm_get_intinfo(ctx, vcpu, &info[0], &info[1]); - if (!error) { - print_intinfo("pending", info[0]); - print_intinfo("current", info[1]); - } - } - - if (!error && (get_stats || get_all)) { - int i, num_stats; - uint64_t *stats; - struct timeval tv; - const char *desc; - - stats = vm_get_stats(ctx, vcpu, &tv, &num_stats); - if (stats != NULL) { - printf("vcpu%d stats:\n", vcpu); - for (i = 0; i < num_stats; i++) { - desc = vm_get_stat_desc(ctx, i); - printf("%-40s\t%ld\n", desc, stats[i]); - } - } - } - - if (!error && run) { - error = vm_run(ctx, vcpu, &vmexit); - if (error == 0) - dump_vm_run_exitcode(&vmexit, vcpu); - else - printf("vm_run error %d\n", error); - } - - if (!error && force_reset) - error = vm_suspend(ctx, VM_SUSPEND_RESET); - - if (!error && force_poweroff) - error = vm_suspend(ctx, VM_SUSPEND_POWEROFF); - - if (error) - printf("errno = %d\n", errno); - - if (!error && destroy) - vm_destroy(ctx); - - free (opts); - exit(error); -} diff --git a/bhyveload/Makefile b/bhyveload/Makefile deleted file mode 100644 index fce0c1b..0000000 --- a/bhyveload/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -# $FreeBSD$ - -PROG= bhyveload -SRCS= bhyveload.c -MAN= bhyveload.8 - -LIBADD= vmmapi - -WARNS?= 3 - -CFLAGS+=-I${.CURDIR}/../../sys/boot/userboot - -.include diff --git a/bhyveload/bhyveload.8 b/bhyveload/bhyveload.8 deleted file mode 100644 index c168832..0000000 --- a/bhyveload/bhyveload.8 +++ /dev/null @@ -1,157 +0,0 @@ -.\" -.\" Copyright (c) 2012 NetApp Inc -.\" All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $FreeBSD$ -.\" -.Dd January 7, 2012 -.Dt BHYVELOAD 8 -.Os -.Sh NAME -.Nm bhyveload -.Nd load a -.Fx -guest inside a bhyve virtual machine -.Sh SYNOPSIS -.Nm -.Op Fl c Ar cons-dev -.Op Fl d Ar disk-path -.Op Fl e Ar name=value -.Op Fl h Ar host-path -.Op Fl m Ar mem-size -.Ar vmname -.Sh DESCRIPTION -.Nm -is used to load a -.Fx -guest inside a -.Xr bhyve 4 -virtual machine. -.Pp -.Nm -is based on -.Xr loader 8 -and will present an interface identical to the -.Fx -loader on the user's terminal. -.Pp -The virtual machine is identified as -.Ar vmname -and will be created if it does not already exist. -.Sh OPTIONS -The following options are available: -.Bl -tag -width indent -.It Fl c Ar cons-dev -.Ar cons-dev -is a -.Xr tty 4 -device to use for -.Nm -terminal I/O. -.Pp -The text string "stdio" is also accepted and selects the use of -unbuffered standard I/O. This is the default value. -.It Fl d Ar disk-path -The -.Ar disk-path -is the pathname of the guest's boot disk image. -.It Fl e Ar name=value -Set the FreeBSD loader environment variable -.Ar name -to -.Ar value . -.Pp -The option may be used more than once to set more than one environment -variable. -.It Fl h Ar host-path -The -.Ar host-path -is the directory at the top of the guest's boot filesystem. -.It Fl m Ar mem-size Xo -.Sm off -.Op Cm K | k | M | m | G | g | T | t -.Xc -.Sm on -.Ar mem-size -is the amount of memory allocated to the guest. -.Pp -The -.Ar mem-size -argument may be suffixed with one of -.Cm K , -.Cm M , -.Cm G -or -.Cm T -(either upper or lower case) to indicate a multiple of -Kilobytes, Megabytes, Gigabytes or Terabytes -respectively. -.Pp -The default value of -.Ar mem-size -is 256M. -.El -.Sh EXAMPLES -To create a virtual machine named -.Ar freebsd-vm -that boots off the ISO image -.Pa /freebsd/release.iso -and has 1GB memory allocated to it: -.Pp -.Dl "bhyveload -m 1G -d /freebsd/release.iso freebsd-vm" -.Pp -To create a virtual machine named -.Ar test-vm -with 256MB of memory allocated, the guest root filesystem under the host -directory -.Pa /user/images/test -and terminal I/O sent to the -.Xr nmdm 4 -device -.Pa /dev/nmdm1B -.Pp -.Dl "bhyveload -m 256MB -h /usr/images/test -c /dev/nmdm1B test-vm" -.Sh SEE ALSO -.Xr bhyve 4 , -.Xr nmdm 4 , -.Xr vmm 4 , -.Xr bhyve 8 , -.Xr loader 8 -.Sh HISTORY -.Nm -first appeared in -.Fx 10.0 , -and was developed at NetApp Inc. -.Sh AUTHORS -.Nm -was developed by -.An -nosplit -.An Neel Natu Aq Mt neel@FreeBSD.org -at NetApp Inc with a lot of help from -.An Doug Rabson Aq Mt dfr@FreeBSD.org . -.Sh BUGS -.Nm -can only load -.Fx -as a guest. diff --git a/bhyveload/bhyveload.c b/bhyveload/bhyveload.c deleted file mode 100644 index 8ebf116..0000000 --- a/bhyveload/bhyveload.c +++ /dev/null @@ -1,746 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -/*- - * Copyright (c) 2011 Google, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "userboot.h" - -#define MB (1024 * 1024UL) -#define GB (1024 * 1024 * 1024UL) -#define BSP 0 - -#define NDISKS 32 - -static char *host_base; -static struct termios term, oldterm; -static int disk_fd[NDISKS]; -static int ndisks; -static int consin_fd, consout_fd; - -static char *vmname, *progname; -static struct vmctx *ctx; - -static uint64_t gdtbase, cr3, rsp; - -static void cb_exit(void *arg, int v); - -/* - * Console i/o callbacks - */ - -static void -cb_putc(void *arg, int ch) -{ - char c = ch; - - (void) write(consout_fd, &c, 1); -} - -static int -cb_getc(void *arg) -{ - char c; - - if (read(consin_fd, &c, 1) == 1) - return (c); - return (-1); -} - -static int -cb_poll(void *arg) -{ - int n; - - if (ioctl(consin_fd, FIONREAD, &n) >= 0) - return (n > 0); - return (0); -} - -/* - * Host filesystem i/o callbacks - */ - -struct cb_file { - int cf_isdir; - size_t cf_size; - struct stat cf_stat; - union { - int fd; - DIR *dir; - } cf_u; -}; - -static int -cb_open(void *arg, const char *filename, void **hp) -{ - struct stat st; - struct cb_file *cf; - char path[PATH_MAX]; - - if (!host_base) - return (ENOENT); - - strlcpy(path, host_base, PATH_MAX); - if (path[strlen(path) - 1] == '/') - path[strlen(path) - 1] = 0; - strlcat(path, filename, PATH_MAX); - cf = malloc(sizeof(struct cb_file)); - if (stat(path, &cf->cf_stat) < 0) { - free(cf); - return (errno); - } - - cf->cf_size = st.st_size; - if (S_ISDIR(cf->cf_stat.st_mode)) { - cf->cf_isdir = 1; - cf->cf_u.dir = opendir(path); - if (!cf->cf_u.dir) - goto out; - *hp = cf; - return (0); - } - if (S_ISREG(cf->cf_stat.st_mode)) { - cf->cf_isdir = 0; - cf->cf_u.fd = open(path, O_RDONLY); - if (cf->cf_u.fd < 0) - goto out; - *hp = cf; - return (0); - } - -out: - free(cf); - return (EINVAL); -} - -static int -cb_close(void *arg, void *h) -{ - struct cb_file *cf = h; - - if (cf->cf_isdir) - closedir(cf->cf_u.dir); - else - close(cf->cf_u.fd); - free(cf); - - return (0); -} - -static int -cb_isdir(void *arg, void *h) -{ - struct cb_file *cf = h; - - return (cf->cf_isdir); -} - -static int -cb_read(void *arg, void *h, void *buf, size_t size, size_t *resid) -{ - struct cb_file *cf = h; - ssize_t sz; - - if (cf->cf_isdir) - return (EINVAL); - sz = read(cf->cf_u.fd, buf, size); - if (sz < 0) - return (EINVAL); - *resid = size - sz; - return (0); -} - -static int -cb_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return, - size_t *namelen_return, char *name) -{ - struct cb_file *cf = h; - struct dirent *dp; - - if (!cf->cf_isdir) - return (EINVAL); - - dp = readdir(cf->cf_u.dir); - if (!dp) - return (ENOENT); - - /* - * Note: d_namlen is in the range 0..255 and therefore less - * than PATH_MAX so we don't need to test before copying. - */ - *fileno_return = dp->d_fileno; - *type_return = dp->d_type; - *namelen_return = dp->d_namlen; - memcpy(name, dp->d_name, dp->d_namlen); - name[dp->d_namlen] = 0; - - return (0); -} - -static int -cb_seek(void *arg, void *h, uint64_t offset, int whence) -{ - struct cb_file *cf = h; - - if (cf->cf_isdir) - return (EINVAL); - if (lseek(cf->cf_u.fd, offset, whence) < 0) - return (errno); - return (0); -} - -static int -cb_stat(void *arg, void *h, int *mode, int *uid, int *gid, uint64_t *size) -{ - struct cb_file *cf = h; - - *mode = cf->cf_stat.st_mode; - *uid = cf->cf_stat.st_uid; - *gid = cf->cf_stat.st_gid; - *size = cf->cf_stat.st_size; - return (0); -} - -/* - * Disk image i/o callbacks - */ - -static int -cb_diskread(void *arg, int unit, uint64_t from, void *to, size_t size, - size_t *resid) -{ - ssize_t n; - - if (unit < 0 || unit >= ndisks ) - return (EIO); - n = pread(disk_fd[unit], to, size, from); - if (n < 0) - return (errno); - *resid = size - n; - return (0); -} - -static int -cb_diskioctl(void *arg, int unit, u_long cmd, void *data) -{ - struct stat sb; - - if (unit < 0 || unit >= ndisks) - return (EBADF); - - switch (cmd) { - case DIOCGSECTORSIZE: - *(u_int *)data = 512; - break; - case DIOCGMEDIASIZE: - if (fstat(disk_fd[unit], &sb) == 0) - *(off_t *)data = sb.st_size; - else - return (ENOTTY); - break; - default: - return (ENOTTY); - } - - return (0); -} - -/* - * Guest virtual machine i/o callbacks - */ -static int -cb_copyin(void *arg, const void *from, uint64_t to, size_t size) -{ - char *ptr; - - to &= 0x7fffffff; - - ptr = vm_map_gpa(ctx, to, size); - if (ptr == NULL) - return (EFAULT); - - memcpy(ptr, from, size); - return (0); -} - -static int -cb_copyout(void *arg, uint64_t from, void *to, size_t size) -{ - char *ptr; - - from &= 0x7fffffff; - - ptr = vm_map_gpa(ctx, from, size); - if (ptr == NULL) - return (EFAULT); - - memcpy(to, ptr, size); - return (0); -} - -static void -cb_setreg(void *arg, int r, uint64_t v) -{ - int error; - enum vm_reg_name vmreg; - - vmreg = VM_REG_LAST; - - switch (r) { - case 4: - vmreg = VM_REG_GUEST_RSP; - rsp = v; - break; - default: - break; - } - - if (vmreg == VM_REG_LAST) { - printf("test_setreg(%d): not implemented\n", r); - cb_exit(NULL, USERBOOT_EXIT_QUIT); - } - - error = vm_set_register(ctx, BSP, vmreg, v); - if (error) { - perror("vm_set_register"); - cb_exit(NULL, USERBOOT_EXIT_QUIT); - } -} - -static void -cb_setmsr(void *arg, int r, uint64_t v) -{ - int error; - enum vm_reg_name vmreg; - - vmreg = VM_REG_LAST; - - switch (r) { - case MSR_EFER: - vmreg = VM_REG_GUEST_EFER; - break; - default: - break; - } - - if (vmreg == VM_REG_LAST) { - printf("test_setmsr(%d): not implemented\n", r); - cb_exit(NULL, USERBOOT_EXIT_QUIT); - } - - error = vm_set_register(ctx, BSP, vmreg, v); - if (error) { - perror("vm_set_msr"); - cb_exit(NULL, USERBOOT_EXIT_QUIT); - } -} - -static void -cb_setcr(void *arg, int r, uint64_t v) -{ - int error; - enum vm_reg_name vmreg; - - vmreg = VM_REG_LAST; - - switch (r) { - case 0: - vmreg = VM_REG_GUEST_CR0; - break; - case 3: - vmreg = VM_REG_GUEST_CR3; - cr3 = v; - break; - case 4: - vmreg = VM_REG_GUEST_CR4; - break; - default: - break; - } - - if (vmreg == VM_REG_LAST) { - printf("test_setcr(%d): not implemented\n", r); - cb_exit(NULL, USERBOOT_EXIT_QUIT); - } - - error = vm_set_register(ctx, BSP, vmreg, v); - if (error) { - perror("vm_set_cr"); - cb_exit(NULL, USERBOOT_EXIT_QUIT); - } -} - -static void -cb_setgdt(void *arg, uint64_t base, size_t size) -{ - int error; - - error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0); - if (error != 0) { - perror("vm_set_desc(gdt)"); - cb_exit(NULL, USERBOOT_EXIT_QUIT); - } - - gdtbase = base; -} - -static void -cb_exec(void *arg, uint64_t rip) -{ - int error; - - if (cr3 == 0) - error = vm_setup_freebsd_registers_i386(ctx, BSP, rip, gdtbase, - rsp); - else - error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase, - rsp); - if (error) { - perror("vm_setup_freebsd_registers"); - cb_exit(NULL, USERBOOT_EXIT_QUIT); - } - - cb_exit(NULL, 0); -} - -/* - * Misc - */ - -static void -cb_delay(void *arg, int usec) -{ - - usleep(usec); -} - -static void -cb_exit(void *arg, int v) -{ - - tcsetattr(consout_fd, TCSAFLUSH, &oldterm); - exit(v); -} - -static void -cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem) -{ - - *ret_lowmem = vm_get_lowmem_size(ctx); - *ret_highmem = vm_get_highmem_size(ctx); -} - -struct env { - const char *str; /* name=value */ - SLIST_ENTRY(env) next; -}; - -static SLIST_HEAD(envhead, env) envhead; - -static void -addenv(const char *str) -{ - struct env *env; - - env = malloc(sizeof(struct env)); - env->str = str; - SLIST_INSERT_HEAD(&envhead, env, next); -} - -static const char * -cb_getenv(void *arg, int num) -{ - int i; - struct env *env; - - i = 0; - SLIST_FOREACH(env, &envhead, next) { - if (i == num) - return (env->str); - i++; - } - - return (NULL); -} - -static struct loader_callbacks cb = { - .getc = cb_getc, - .putc = cb_putc, - .poll = cb_poll, - - .open = cb_open, - .close = cb_close, - .isdir = cb_isdir, - .read = cb_read, - .readdir = cb_readdir, - .seek = cb_seek, - .stat = cb_stat, - - .diskread = cb_diskread, - .diskioctl = cb_diskioctl, - - .copyin = cb_copyin, - .copyout = cb_copyout, - .setreg = cb_setreg, - .setmsr = cb_setmsr, - .setcr = cb_setcr, - .setgdt = cb_setgdt, - .exec = cb_exec, - - .delay = cb_delay, - .exit = cb_exit, - .getmem = cb_getmem, - - .getenv = cb_getenv, -}; - -static int -altcons_open(char *path) -{ - struct stat sb; - int err; - int fd; - - /* - * Allow stdio to be passed in so that the same string - * can be used for the bhyveload console and bhyve com-port - * parameters - */ - if (!strcmp(path, "stdio")) - return (0); - - err = stat(path, &sb); - if (err == 0) { - if (!S_ISCHR(sb.st_mode)) - err = ENOTSUP; - else { - fd = open(path, O_RDWR | O_NONBLOCK); - if (fd < 0) - err = errno; - else - consin_fd = consout_fd = fd; - } - } - - return (err); -} - -static int -disk_open(char *path) -{ - int err, fd; - - if (ndisks >= NDISKS) - return (ERANGE); - - err = 0; - fd = open(path, O_RDONLY); - - if (fd > 0) { - disk_fd[ndisks] = fd; - ndisks++; - } else - err = errno; - - return (err); -} - -static void -usage(void) -{ - - fprintf(stderr, - "usage: %s [-c ] [-d ] [-e ]\n" - " %*s [-h ] [-m mem-size] \n", - progname, - (int)strlen(progname), ""); - exit(1); -} - -int -main(int argc, char** argv) -{ - void *h; - void (*func)(struct loader_callbacks *, void *, int, int); - uint64_t mem_size; - int opt, error, need_reinit; - - progname = basename(argv[0]); - - mem_size = 256 * MB; - - consin_fd = STDIN_FILENO; - consout_fd = STDOUT_FILENO; - - while ((opt = getopt(argc, argv, "c:d:e:h:m:")) != -1) { - switch (opt) { - case 'c': - error = altcons_open(optarg); - if (error != 0) - errx(EX_USAGE, "Could not open '%s'", optarg); - break; - - case 'd': - error = disk_open(optarg); - if (error != 0) - errx(EX_USAGE, "Could not open '%s'", optarg); - break; - - case 'e': - addenv(optarg); - break; - - case 'h': - host_base = optarg; - break; - - case 'm': - error = vm_parse_memsize(optarg, &mem_size); - if (error != 0) - errx(EX_USAGE, "Invalid memsize '%s'", optarg); - break; - case '?': - usage(); - } - } - - argc -= optind; - argv += optind; - - if (argc != 1) - usage(); - - vmname = argv[0]; - - need_reinit = 0; - error = vm_create(vmname); - if (error) { - if (errno != EEXIST) { - perror("vm_create"); - exit(1); - } - need_reinit = 1; - } - - ctx = vm_open(vmname); - if (ctx == NULL) { - perror("vm_open"); - exit(1); - } - - if (need_reinit) { - error = vm_reinit(ctx); - if (error) { - perror("vm_reinit"); - exit(1); - } - } - - error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL); - if (error) { - perror("vm_setup_memory"); - exit(1); - } - - tcgetattr(consout_fd, &term); - oldterm = term; - cfmakeraw(&term); - term.c_cflag |= CLOCAL; - - tcsetattr(consout_fd, TCSAFLUSH, &term); - - h = dlopen("/boot/userboot.so", RTLD_LOCAL); - if (!h) { - printf("%s\n", dlerror()); - return (1); - } - func = dlsym(h, "loader_main"); - if (!func) { - printf("%s\n", dlerror()); - return (1); - } - - addenv("smbios.bios.vendor=BHYVE"); - addenv("boot_serial=1"); - - func(&cb, NULL, USERBOOT_VERSION_3, ndisks); -} diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..7818ce8 --- /dev/null +++ b/config.mk @@ -0,0 +1,73 @@ +############################################################################### +# Config # +# # +# [XHYVE_CONFIG_ASSERT] VMM asserts (disable for release builds?) # +# [XHYVE_CONFIG_TRACE] VMM event tracer # +# [XHYVE_CONFIG_STATS] VMM event profiler # +############################################################################### + +DEFINES := \ + -DXHYVE_CONFIG_ASSERT + +############################################################################### +# Toolchain # +############################################################################### + +CC := clang +AS := clang +LD := clang +STRIP := strip +DSYM := dsymutil + +ENV := \ + LANG=en_US.US-ASCII + +############################################################################### +# CFLAGS # +############################################################################### + +CFLAGS_OPT := \ + -Os \ + -flto \ + -fstrict-aliasing + +CFLAGS_WARN := \ + -Weverything \ + -Werror \ + -Wno-unknown-warning-option \ + -Wno-reserved-id-macro \ + -pedantic + +CFLAGS_DIAG := \ + -fmessage-length=152 \ + -fdiagnostics-show-note-include-stack \ + -fmacro-backtrace-limit=0 \ + -fcolor-diagnostics + +CFLAGS_DBG := \ + -g + +CFLAGS := \ + -arch x86_64 \ + -x c \ + -std=c11 \ + -fno-common \ + -fvisibility=hidden \ + $(DEFINES) \ + $(CFLAGS_OPT) \ + $(CFLAGS_WARN) \ + $(CFLAGS_DIAG) \ + $(CFLAGS_DBG) + +############################################################################### +# LDFLAGS # +############################################################################### + +LDFLAGS_DBG := \ + -Xlinker -object_path_lto + +LDFLAGS := \ + -arch x86_64 \ + -framework Hypervisor \ + -framework vmnet \ + $(LDFLAGS_DBG) diff --git a/bhyve/acpi.h b/include/xhyve/acpi.h similarity index 68% rename from bhyve/acpi.h rename to include/xhyve/acpi.h index 652164a..ebfb611 100644 --- a/bhyve/acpi.h +++ b/include/xhyve/acpi.h @@ -26,29 +26,27 @@ * $FreeBSD$ */ -#ifndef _ACPI_H_ -#define _ACPI_H_ +#pragma once -#define SCI_INT 9 +#include -#define SMI_CMD 0xb2 -#define BHYVE_ACPI_ENABLE 0xa0 -#define BHYVE_ACPI_DISABLE 0xa1 +#define SCI_INT 9 -#define PM1A_EVT_ADDR 0x400 -#define PM1A_CNT_ADDR 0x404 +#define SMI_CMD 0xb2 +#define BHYVE_ACPI_ENABLE 0xa0 +#define BHYVE_ACPI_DISABLE 0xa1 -#define IO_PMTMR 0x408 /* 4-byte i/o port for the timer */ +#define PM1A_EVT_ADDR 0x400 +#define PM1A_EVT_ADDR2 0x402 +#define PM1A_CNT_ADDR 0x404 -struct vmctx; +#define IO_PMTMR 0x408 /* 4-byte i/o port for the timer */ -int acpi_build(struct vmctx *ctx, int ncpu); -void dsdt_line(const char *fmt, ...); -void dsdt_fixed_ioport(uint16_t iobase, uint16_t length); -void dsdt_fixed_irq(uint8_t irq); -void dsdt_fixed_mem32(uint32_t base, uint32_t length); -void dsdt_indent(int levels); -void dsdt_unindent(int levels); -void sci_init(struct vmctx *ctx); - -#endif /* _ACPI_H_ */ +int acpi_build(int ncpu); +void dsdt_line(const char *fmt, ...); +void dsdt_fixed_ioport(uint16_t iobase, uint16_t length); +void dsdt_fixed_irq(uint8_t irq); +void dsdt_fixed_mem32(uint32_t base, uint32_t length); +void dsdt_indent(int levels); +void dsdt_unindent(int levels); +void sci_init(void); diff --git a/bhyve/ahci.h b/include/xhyve/ahci.h similarity index 96% rename from bhyve/ahci.h rename to include/xhyve/ahci.h index 1fd9f20..ec1916e 100644 --- a/bhyve/ahci.h +++ b/include/xhyve/ahci.h @@ -27,8 +27,7 @@ * $FreeBSD$ */ -#ifndef _AHCI_H_ -#define _AHCI_H_ +#pragma once /* ATA register defines */ #define ATA_DATA 0 /* (RW) data */ @@ -306,17 +305,15 @@ /* Just to be sure, if building as module. */ #if MAXPHYS < 512 * 1024 #undef MAXPHYS -#define MAXPHYS 512 * 1024 +#define MAXPHYS 512 * 1024 #endif /* Pessimistic prognosis on number of required S/G entries */ -#define AHCI_SG_ENTRIES (roundup(btoc(MAXPHYS) + 1, 8)) +#define AHCI_SG_ENTRIES (roundup(btoc(MAXPHYS) + 1, 8)) /* Command list. 32 commands. First, 1Kbyte aligned. */ -#define AHCI_CL_OFFSET 0 -#define AHCI_CL_SIZE 32 +#define AHCI_CL_OFFSET 0 +#define AHCI_CL_SIZE 32 /* Command tables. Up to 32 commands, Each, 128byte aligned. */ -#define AHCI_CT_OFFSET (AHCI_CL_OFFSET + AHCI_CL_SIZE * AHCI_MAX_SLOTS) -#define AHCI_CT_SIZE (128 + AHCI_SG_ENTRIES * 16) +#define AHCI_CT_OFFSET (AHCI_CL_OFFSET + AHCI_CL_SIZE * AHCI_MAX_SLOTS) +#define AHCI_CT_SIZE (128 + AHCI_SG_ENTRIES * 16) /* Total main work area. */ -#define AHCI_WORK_SIZE (AHCI_CT_OFFSET + AHCI_CT_SIZE * ch->numslots) - -#endif /* _AHCI_H_ */ +#define AHCI_WORK_SIZE (AHCI_CT_OFFSET + AHCI_CT_SIZE * ch->numslots) diff --git a/bhyve/block_if.h b/include/xhyve/block_if.h similarity index 62% rename from bhyve/block_if.h rename to include/xhyve/block_if.h index 8e63407..fecfdbc 100644 --- a/bhyve/block_if.h +++ b/include/xhyve/block_if.h @@ -33,38 +33,37 @@ * another thread. */ -#ifndef _BLOCK_IF_H_ -#define _BLOCK_IF_H_ +#pragma once #include #include -#define BLOCKIF_IOV_MAX 33 /* not practical to be IOV_MAX */ +#define BLOCKIF_IOV_MAX 33 /* not practical to be IOV_MAX */ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct blockif_req { - struct iovec br_iov[BLOCKIF_IOV_MAX]; - int br_iovcnt; - off_t br_offset; - ssize_t br_resid; - void (*br_callback)(struct blockif_req *req, int err); - void *br_param; + struct iovec br_iov[BLOCKIF_IOV_MAX]; + int br_iovcnt; + off_t br_offset; + ssize_t br_resid; + void (*br_callback)(struct blockif_req *req, int err); + void *br_param; }; +#pragma clang diagnostic pop struct blockif_ctxt; struct blockif_ctxt *blockif_open(const char *optstr, const char *ident); -off_t blockif_size(struct blockif_ctxt *bc); -void blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, - uint8_t *s); -int blockif_sectsz(struct blockif_ctxt *bc); -void blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off); -int blockif_queuesz(struct blockif_ctxt *bc); -int blockif_is_ro(struct blockif_ctxt *bc); -int blockif_candelete(struct blockif_ctxt *bc); -int blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq); -int blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq); -int blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq); -int blockif_delete(struct blockif_ctxt *bc, struct blockif_req *breq); -int blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq); -int blockif_close(struct blockif_ctxt *bc); - -#endif /* _BLOCK_IF_H_ */ +off_t blockif_size(struct blockif_ctxt *bc); +void blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s); +int blockif_sectsz(struct blockif_ctxt *bc); +void blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off); +int blockif_queuesz(struct blockif_ctxt *bc); +int blockif_is_ro(struct blockif_ctxt *bc); +int blockif_candelete(struct blockif_ctxt *bc); +int blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq); +int blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq); +int blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq); +int blockif_delete(struct blockif_ctxt *bc, struct blockif_req *breq); +int blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq); +int blockif_close(struct blockif_ctxt *bc); diff --git a/bhyve/dbgport.h b/include/xhyve/dbgport.h similarity index 94% rename from bhyve/dbgport.h rename to include/xhyve/dbgport.h index 2ddcbf8..22a23af 100644 --- a/bhyve/dbgport.h +++ b/include/xhyve/dbgport.h @@ -26,9 +26,6 @@ * $FreeBSD$ */ -#ifndef _DBGPORT_H_ -#define _DBGPORT_H_ +#pragma once -void init_dbgport(int port); - -#endif +void init_dbgport(int port); diff --git a/include/xhyve/firmware/kexec.h b/include/xhyve/firmware/kexec.h new file mode 100644 index 0000000..791d963 --- /dev/null +++ b/include/xhyve/firmware/kexec.h @@ -0,0 +1,89 @@ +#pragma once + +#include + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpacked" +struct setup_header { + uint8_t setup_sects; /* The size of the setup in sectors */ + uint16_t root_flags; /* If set, the root is mounted readonly */ + uint32_t syssize; /* The size of the 32-bit code in 16-byte paras */ + uint16_t ram_size; /* DO NOT USE - for bootsect.S use only */ + uint16_t vid_mode; /* Video mode control */ + uint16_t root_dev; /* Default root device number */ + uint16_t boot_flag; /* 0xAA55 magic number */ + uint16_t jump; /* Jump instruction */ + uint32_t header; /* Magic signature "HdrS" */ + uint16_t version; /* Boot protocol version supported */ + uint32_t realmode_swtch; /* Boot loader hook (see below) */ + uint16_t start_sys_seg; /* The load-low segment (0x1000) (obsolete) */ + uint16_t kernel_version; /* Pointer to kernel version string */ + uint8_t type_of_loader; /* Boot loader identifier */ + uint8_t loadflags; /* Boot protocol option flags */ + uint16_t setup_move_size; /* Move to high memory size (used with hooks) */ + uint32_t code32_start; /* Boot loader hook (see below) */ + uint32_t ramdisk_image; /* initrd load address (set by boot loader) */ + uint32_t ramdisk_size; /* initrd size (set by boot loader) */ + uint32_t bootsect_kludge; /* DO NOT USE - for bootsect.S use only */ + uint16_t heap_end_ptr; /* Free memory after setup end */ + uint8_t ext_loader_ver; /* Extended boot loader version */ + uint8_t ext_loader_type; /* Extended boot loader ID */ + uint32_t cmd_line_ptr; /* 32-bit pointer to the kernel command line */ + uint32_t nitrd_addr_max; /* Highest legal initrd address */ + uint32_t kernel_alignment; /* Physical addr alignment required for kernel */ + uint8_t relocatable_kernel; /* Whether kernel is relocatable or not */ + uint8_t min_alignment; /* Minimum alignment, as a power of two */ + uint16_t xloadflags; /* Boot protocol option flags */ + uint32_t cmdline_size; /* Maximum size of the kernel command line */ + uint32_t hardware_subarch; /* Hardware subarchitecture */ + uint64_t hardware_subarch_data; /* Subarchitecture-specific data */ + uint32_t payload_offset; /* Offset of kernel payload */ + uint32_t payload_length; /* Length of kernel payload */ + uint64_t setup_data; /* 64bit pointer to linked list of struct setup_data */ + uint64_t pref_address; /* Preferred loading address */ + uint32_t init_size; /* Linear memory required during initialization */ + uint32_t handover_offset; /* Offset of handover entry point */ +} __attribute__((packed)); + +struct zero_page { + uint8_t screen_info[64]; + uint8_t apm_bios_info[20]; + uint8_t _0[4]; + uint64_t tboot_addr; + uint8_t ist_info[16]; + uint8_t _1[16]; + uint8_t hd0_info[16]; + uint8_t hd1_info[16]; + uint8_t sys_desc_table[16]; + uint8_t olpc_ofw_header[16]; + uint32_t ext_ramdisk_image; + uint32_t ext_ramdisk_size; + uint32_t ext_cmd_line_ptr; + uint8_t _2[116]; + uint8_t edid_info[128]; + uint8_t efi_info[32]; + uint32_t alt_mem_k; + uint32_t scratch; + uint8_t e820_entries; + uint8_t eddbuf_entries; + uint8_t edd_mbr_sig_buf_entries; + uint8_t kbd_status; + uint8_t _3[3]; + uint8_t sentinel; + uint8_t _4[1]; + struct setup_header setup_header; + uint8_t _5[(0x290 - 0x1f1 - sizeof(struct setup_header))]; + uint32_t edd_mbr_sig_buffer[16]; + struct { + uint64_t addr; + uint64_t size; + uint32_t type; + } __attribute__((packed)) e820_map[128]; + uint8_t _6[48]; + uint8_t eddbuf[492]; + uint8_t _7[276]; +} __attribute__((packed)); +#pragma clang diagnostic pop + +void kexec_init(char *kernel_path, char *initrd_path, char *cmdline); +uint64_t kexec(void); diff --git a/bhyve/inout.h b/include/xhyve/inout.h similarity index 61% rename from bhyve/inout.h rename to include/xhyve/inout.h index 7f39095..f2c6632 100644 --- a/bhyve/inout.h +++ b/include/xhyve/inout.h @@ -26,54 +26,54 @@ * $FreeBSD$ */ -#ifndef _INOUT_H_ -#define _INOUT_H_ +#pragma once -#include +#include +#include -struct vmctx; struct vm_exit; /* * inout emulation handlers return 0 on success and -1 on failure. */ -typedef int (*inout_func_t)(struct vmctx *ctx, int vcpu, int in, int port, - int bytes, uint32_t *eax, void *arg); +typedef int (*inout_func_t)(int vcpu, int in, int port, + int bytes, uint32_t *eax, void *arg); +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct inout_port { - const char *name; - int port; - int size; - int flags; - inout_func_t handler; - void *arg; + const char *name; + int port; + int size; + int flags; + inout_func_t handler; + void *arg; }; -#define IOPORT_F_IN 0x1 -#define IOPORT_F_OUT 0x2 -#define IOPORT_F_INOUT (IOPORT_F_IN | IOPORT_F_OUT) +#pragma clang diagnostic pop + +#define IOPORT_F_IN 0x1 +#define IOPORT_F_OUT 0x2 +#define IOPORT_F_INOUT (IOPORT_F_IN | IOPORT_F_OUT) /* * The following flags are used internally and must not be used by * device models. */ -#define IOPORT_F_DEFAULT 0x80000000 /* claimed by default handler */ +#define IOPORT_F_DEFAULT 0x80000000 /* claimed by default handler */ -#define INOUT_PORT(name, port, flags, handler) \ - static struct inout_port __CONCAT(__inout_port, __LINE__) = { \ - #name, \ - (port), \ - 1, \ - (flags), \ - (handler), \ - 0 \ - }; \ - DATA_SET(inout_port_set, __CONCAT(__inout_port, __LINE__)) - -void init_inout(void); -int emulate_inout(struct vmctx *, int vcpu, struct vm_exit *vmexit, - int strict); -int register_inout(struct inout_port *iop); -int unregister_inout(struct inout_port *iop); -void init_bvmcons(void); +#define INOUT_PORT(name, port, flags, handler) \ + static struct inout_port __CONCAT(__inout_port, port) = { \ + #name, \ + (port), \ + 1, \ + (flags), \ + (handler), \ + 0 \ + }; \ + DATA_SET(inout_port_set, __CONCAT(__inout_port, port)) -#endif /* _INOUT_H_ */ +void init_inout(void); +int emulate_inout(int vcpu, struct vm_exit *vmexit, int strict); +int register_inout(struct inout_port *iop); +int unregister_inout(struct inout_port *iop); +void init_bvmcons(void); diff --git a/bhyve/ioapic.h b/include/xhyve/ioapic.h similarity index 92% rename from bhyve/ioapic.h rename to include/xhyve/ioapic.h index efdd3c6..7f577ab 100644 --- a/bhyve/ioapic.h +++ b/include/xhyve/ioapic.h @@ -27,13 +27,10 @@ * $FreeBSD$ */ -#ifndef _IOAPIC_H_ -#define _IOAPIC_H_ +#pragma once /* * Allocate a PCI IRQ from the I/O APIC. */ -void ioapic_init(struct vmctx *ctx); -int ioapic_pci_alloc_irq(void); - -#endif +void ioapic_init(void); +int ioapic_pci_alloc_irq(void); diff --git a/bhyve/mem.h b/include/xhyve/mem.h similarity index 63% rename from bhyve/mem.h rename to include/xhyve/mem.h index f671eae..17d68f3 100644 --- a/bhyve/mem.h +++ b/include/xhyve/mem.h @@ -26,36 +26,36 @@ * $FreeBSD$ */ -#ifndef _MEM_H_ -#define _MEM_H_ +#pragma once -#include +#include +#include -struct vmctx; - -typedef int (*mem_func_t)(struct vmctx *ctx, int vcpu, int dir, uint64_t addr, - int size, uint64_t *val, void *arg1, long arg2); +typedef int (*mem_func_t)(int vcpu, int dir, uint64_t addr, int size, + uint64_t *val, void *arg1, long arg2); +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct mem_range { - const char *name; - int flags; - mem_func_t handler; - void *arg1; - long arg2; - uint64_t base; - uint64_t size; + const char *name; + int flags; + mem_func_t handler; + void *arg1; + long arg2; + uint64_t base; + uint64_t size; }; -#define MEM_F_READ 0x1 -#define MEM_F_WRITE 0x2 -#define MEM_F_RW 0x3 -#define MEM_F_IMMUTABLE 0x4 /* mem_range cannot be unregistered */ +#pragma clang diagnostic pop -void init_mem(void); -int emulate_mem(struct vmctx *, int vcpu, uint64_t paddr, struct vie *vie, - struct vm_guest_paging *paging); - -int register_mem(struct mem_range *memp); -int register_mem_fallback(struct mem_range *memp); -int unregister_mem(struct mem_range *memp); +#define MEM_F_READ 0x1 +#define MEM_F_WRITE 0x2 +#define MEM_F_RW 0x3 +#define MEM_F_IMMUTABLE 0x4 /* mem_range cannot be unregistered */ -#endif /* _MEM_H_ */ +void init_mem(void); +int emulate_mem(int vcpu, uint64_t paddr, struct vie *vie, + struct vm_guest_paging *paging); + +int register_mem(struct mem_range *memp); +int register_mem_fallback(struct mem_range *memp); +int unregister_mem(struct mem_range *memp); diff --git a/bhyve/mevent.h b/include/xhyve/mevent.h similarity index 79% rename from bhyve/mevent.h rename to include/xhyve/mevent.h index d6a59c6..48866bd 100644 --- a/bhyve/mevent.h +++ b/include/xhyve/mevent.h @@ -26,8 +26,7 @@ * $FreeBSD$ */ -#ifndef _MEVENT_H_ -#define _MEVENT_H_ +#pragma once enum ev_type { EVF_READ, @@ -38,14 +37,11 @@ enum ev_type { struct mevent; -struct mevent *mevent_add(int fd, enum ev_type type, - void (*func)(int, enum ev_type, void *), - void *param); -int mevent_enable(struct mevent *evp); -int mevent_disable(struct mevent *evp); -int mevent_delete(struct mevent *evp); -int mevent_delete_close(struct mevent *evp); +struct mevent *mevent_add(int fd, enum ev_type type, + void (*func)(int, enum ev_type, void *), void *param); +int mevent_enable(struct mevent *evp); +int mevent_disable(struct mevent *evp); +int mevent_delete(struct mevent *evp); +int mevent_delete_close(struct mevent *evp); -void mevent_dispatch(void); - -#endif /* _MEVENT_H_ */ +void mevent_dispatch(void); diff --git a/bhyve/mptbl.h b/include/xhyve/mptbl.h similarity index 89% rename from bhyve/mptbl.h rename to include/xhyve/mptbl.h index e9e1c42..df1b9e2 100644 --- a/bhyve/mptbl.h +++ b/include/xhyve/mptbl.h @@ -26,10 +26,7 @@ * $FreeBSD$ */ -#ifndef _MPTBL_H_ -#define _MPTBL_H_ +#pragma once -int mptable_build(struct vmctx *ctx, int ncpu); -void mptable_add_oemtbl(void *tbl, int tblsz); - -#endif /* _MPTBL_H_ */ +int mptable_build(int ncpu); +void mptable_add_oemtbl(void *tbl, int tblsz); diff --git a/include/xhyve/pci_emul.h b/include/xhyve/pci_emul.h new file mode 100644 index 0000000..04fcd7f --- /dev/null +++ b/include/xhyve/pci_emul.h @@ -0,0 +1,278 @@ +/*- + * Copyright (c) 2011 NetApp, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +#define PCI_BARMAX PCIR_MAX_BAR_0 /* BAR registers in a Type 0 header */ + +struct pci_devinst; +struct memory_region; + +struct pci_devemu { + /* name of device emulation */ + char *pe_emu; + /* instance creation */ + int (*pe_init)(struct pci_devinst *, char *opts); + /* ACPI DSDT enumeration */ + void (*pe_write_dsdt)(struct pci_devinst *); + /* config space read/write callbacks */ + int (*pe_cfgwrite)(int vcpu, struct pci_devinst *pi, + int offset, int bytes, uint32_t val); + int (*pe_cfgread)(int vcpu, struct pci_devinst *pi, int offset, int bytes, + uint32_t *retval); + /* BAR read/write callbacks */ + void (*pe_barwrite)(int vcpu, struct pci_devinst *pi, int baridx, + uint64_t offset, int size, uint64_t value); + uint64_t (*pe_barread)(int vcpu, struct pci_devinst *pi, int baridx, + uint64_t offset, int size); +}; + +#define PCI_EMUL_SET(x) DATA_SET(pci_devemu_set, x) + +enum pcibar_type { + PCIBAR_NONE, + PCIBAR_IO, + PCIBAR_MEM32, + PCIBAR_MEM64, + PCIBAR_MEMHI64 +}; + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +struct pcibar { + enum pcibar_type type; /* io or memory */ + uint64_t size; + uint64_t addr; +}; +#pragma clang diagnostic pop + +#define PI_NAMESZ 40 + +struct msix_table_entry { + uint64_t addr; + uint32_t msg_data; + uint32_t vector_control; +}; + +/* + * In case the structure is modified to hold extra information, use a define + * for the size that should be emulated. + */ +#define MSIX_TABLE_ENTRY_SIZE 16 +#define MAX_MSIX_TABLE_ENTRIES 2048 +#define PBA_SIZE(msgnum) (roundup2((msgnum), 64) / 8) + +enum lintr_stat { + IDLE, + ASSERTED, + PENDING +}; + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +struct pci_devinst { + struct pci_devemu *pi_d; + uint8_t pi_bus, pi_slot, pi_func; + char pi_name[PI_NAMESZ]; + int pi_bar_getsize; + int pi_prevcap; + int pi_capend; + + struct { + int8_t pin; + enum lintr_stat state; + int pirq_pin; + int ioapic_irq; + pthread_mutex_t lock; + } pi_lintr; + + struct { + int enabled; + uint64_t addr; + uint64_t msg_data; + int maxmsgnum; + } pi_msi; + + struct { + int enabled; + int table_bar; + int pba_bar; + uint32_t table_offset; + int table_count; + uint32_t pba_offset; + int pba_size; + int function_mask; + struct msix_table_entry *table; /* allocated at runtime */ + } pi_msix; + + void *pi_arg; /* devemu-private data */ + + u_char pi_cfgdata[PCI_REGMAX + 1]; + struct pcibar pi_bar[PCI_BARMAX + 1]; +}; +#pragma clang diagnostic pop + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpacked" +struct msicap { + uint8_t capid; + uint8_t nextptr; + uint16_t msgctrl; + uint32_t addrlo; + uint32_t addrhi; + uint16_t msgdata; +} __packed; + +struct msixcap { + uint8_t capid; + uint8_t nextptr; + uint16_t msgctrl; + uint32_t table_info; /* bar index and offset within it */ + uint32_t pba_info; /* bar index and offset within it */ +} __packed; + +struct pciecap { + uint8_t capid; + uint8_t nextptr; + uint16_t pcie_capabilities; + + uint32_t dev_capabilities; /* all devices */ + uint16_t dev_control; + uint16_t dev_status; + + uint32_t link_capabilities; /* devices with links */ + uint16_t link_control; + uint16_t link_status; + + uint32_t slot_capabilities; /* ports with slots */ + uint16_t slot_control; + uint16_t slot_status; + + uint16_t root_control; /* root ports */ + uint16_t root_capabilities; + uint32_t root_status; + + uint32_t dev_capabilities2; /* all devices */ + uint16_t dev_control2; + uint16_t dev_status2; + + uint32_t link_capabilities2; /* devices with links */ + uint16_t link_control2; + uint16_t link_status2; + + uint32_t slot_capabilities2; /* ports with slots */ + uint16_t slot_control2; + uint16_t slot_status2; +} __packed; +#pragma clang diagnostic pop + +typedef void (*pci_lintr_cb)(int b, int s, int pin, int pirq_pin, + int ioapic_irq, void *arg); + +int init_pci(void); +void msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset, + int bytes, uint32_t val); +void msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset, + int bytes, uint32_t val); +void pci_callback(void); +int pci_emul_alloc_bar(struct pci_devinst *pdi, int idx, + enum pcibar_type type, uint64_t size); +int pci_emul_alloc_pbar(struct pci_devinst *pdi, int idx, + uint64_t hostbase, enum pcibar_type type, uint64_t size); +int pci_emul_add_msicap(struct pci_devinst *pi, int msgnum); +int pci_emul_add_pciecap(struct pci_devinst *pi, int pcie_device_type); +void pci_generate_msi(struct pci_devinst *pi, int msgnum); +void pci_generate_msix(struct pci_devinst *pi, int msgnum); +void pci_lintr_assert(struct pci_devinst *pi); +void pci_lintr_deassert(struct pci_devinst *pi); +void pci_lintr_request(struct pci_devinst *pi); +int pci_msi_enabled(struct pci_devinst *pi); +int pci_msix_enabled(struct pci_devinst *pi); +int pci_msix_table_bar(struct pci_devinst *pi); +int pci_msix_pba_bar(struct pci_devinst *pi); +int pci_msi_msgnum(struct pci_devinst *pi); +int pci_parse_slot(char *opt); +void pci_populate_msicap(struct msicap *cap, int msgs, int nextptr); +int pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum); +int pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size, + uint64_t value); +uint64_t pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size); +int pci_count_lintr(int bus); +void pci_walk_lintr(int bus, pci_lintr_cb cb, void *arg); +void pci_write_dsdt(void); +uint64_t pci_ecfg_base(void); +int pci_bus_configured(int bus); + +static __inline void +pci_set_cfgdata8(struct pci_devinst *pi, int offset, uint8_t val) +{ + assert(offset <= PCI_REGMAX); + *(uint8_t *)(((uintptr_t) &pi->pi_cfgdata) + ((unsigned) offset)) = val; +} + +static __inline void +pci_set_cfgdata16(struct pci_devinst *pi, int offset, uint16_t val) +{ + assert(offset <= (PCI_REGMAX - 1) && (offset & 1) == 0); + *(uint16_t *)(((uintptr_t) &pi->pi_cfgdata) + ((unsigned) offset)) = val; +} + +static __inline void +pci_set_cfgdata32(struct pci_devinst *pi, int offset, uint32_t val) +{ + assert(offset <= (PCI_REGMAX - 3) && (offset & 3) == 0); + *(uint32_t *)(((uintptr_t) &pi->pi_cfgdata) + ((unsigned) offset)) = val; +} + +static __inline uint8_t +pci_get_cfgdata8(struct pci_devinst *pi, int offset) +{ + assert(offset <= PCI_REGMAX); + return (*(uint8_t *)(((uintptr_t) &pi->pi_cfgdata) + ((unsigned) offset))); +} + +static __inline uint16_t +pci_get_cfgdata16(struct pci_devinst *pi, int offset) +{ + assert(offset <= (PCI_REGMAX - 1) && (offset & 1) == 0); + return (*(uint16_t *)(((uintptr_t) &pi->pi_cfgdata) + ((unsigned) offset))); +} + +static __inline uint32_t +pci_get_cfgdata32(struct pci_devinst *pi, int offset) +{ + assert(offset <= (PCI_REGMAX - 3) && (offset & 3) == 0); + return (*(uint32_t *)(((uintptr_t) &pi->pi_cfgdata) + ((unsigned) offset))); +} diff --git a/bhyve/pci_irq.h b/include/xhyve/pci_irq.h similarity index 78% rename from bhyve/pci_irq.h rename to include/xhyve/pci_irq.h index 24f9c99..76b72dd 100644 --- a/bhyve/pci_irq.h +++ b/include/xhyve/pci_irq.h @@ -27,19 +27,16 @@ * $FreeBSD$ */ -#ifndef __PCI_IRQ_H__ -#define __PCI_IRQ_H__ +#pragma once struct pci_devinst; -void pci_irq_assert(struct pci_devinst *pi); -void pci_irq_deassert(struct pci_devinst *pi); -void pci_irq_init(struct vmctx *ctx); -void pci_irq_reserve(int irq); -void pci_irq_use(int irq); -int pirq_alloc_pin(struct vmctx *ctx); -int pirq_irq(int pin); -uint8_t pirq_read(int pin); -void pirq_write(struct vmctx *ctx, int pin, uint8_t val); - -#endif +void pci_irq_assert(struct pci_devinst *pi); +void pci_irq_deassert(struct pci_devinst *pi); +void pci_irq_init(void); +void pci_irq_reserve(int irq); +void pci_irq_use(int irq); +int pirq_alloc_pin(void); +int pirq_irq(int pin); +uint8_t pirq_read(int pin); +void pirq_write(int pin, uint8_t val); diff --git a/bhyve/pci_lpc.h b/include/xhyve/pci_lpc.h similarity index 67% rename from bhyve/pci_lpc.h rename to include/xhyve/pci_lpc.h index 55a5865..7e8ed3c 100644 --- a/bhyve/pci_lpc.h +++ b/include/xhyve/pci_lpc.h @@ -26,10 +26,10 @@ * $FreeBSD$ */ -#ifndef _LPC_H_ -#define _LPC_H_ +#pragma once -#include +#include +#include typedef void (*lpc_write_dsdt_t)(void); @@ -37,36 +37,37 @@ struct lpc_dsdt { lpc_write_dsdt_t handler; }; -#define LPC_DSDT(handler) \ - static struct lpc_dsdt __CONCAT(__lpc_dsdt, __LINE__) = { \ - (handler), \ - }; \ - DATA_SET(lpc_dsdt_set, __CONCAT(__lpc_dsdt, __LINE__)) +#define LPC_DSDT(handler) \ + static struct lpc_dsdt __CONCAT(__lpc_dsdt, handler) = { \ + (handler), \ + }; \ + DATA_SET(lpc_dsdt_set, __CONCAT(__lpc_dsdt, handler)) enum lpc_sysres_type { LPC_SYSRES_IO, LPC_SYSRES_MEM }; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct lpc_sysres { enum lpc_sysres_type type; uint32_t base; uint32_t length; }; +#pragma clang diagnostic pop -#define LPC_SYSRES(type, base, length) \ - static struct lpc_sysres __CONCAT(__lpc_sysres, __LINE__) = { \ - (type), \ - (base), \ - (length) \ - }; \ - DATA_SET(lpc_sysres_set, __CONCAT(__lpc_sysres, __LINE__)) +#define LPC_SYSRES(type, base, length) \ + static struct lpc_sysres __CONCAT(__lpc_sysres, base) = {\ + (type), \ + (base), \ + (length) \ + }; \ + DATA_SET(lpc_sysres_set, __CONCAT(__lpc_sysres, base)) -#define SYSRES_IO(base, length) LPC_SYSRES(LPC_SYSRES_IO, base, length) -#define SYSRES_MEM(base, length) LPC_SYSRES(LPC_SYSRES_MEM, base, length) +#define SYSRES_IO(base, length) LPC_SYSRES(LPC_SYSRES_IO, base, length) +#define SYSRES_MEM(base, length) LPC_SYSRES(LPC_SYSRES_MEM, base, length) -int lpc_device_parse(const char *opt); -char *lpc_pirq_name(int pin); -void lpc_pirq_routed(void); - -#endif +int lpc_device_parse(const char *opt); +char *lpc_pirq_name(int pin); +void lpc_pirq_routed(void); diff --git a/bhyve/rtc.h b/include/xhyve/rtc.h similarity index 92% rename from bhyve/rtc.h rename to include/xhyve/rtc.h index 5b08ca3..0437564 100644 --- a/bhyve/rtc.h +++ b/include/xhyve/rtc.h @@ -26,9 +26,7 @@ * $FreeBSD$ */ -#ifndef _RTC_H_ -#define _RTC_H_ +#pragma once -void rtc_init(struct vmctx *ctx, int use_localtime); +void rtc_init(int use_localtime); -#endif /* _RTC_H_ */ diff --git a/bhyve/smbiostbl.h b/include/xhyve/smbiostbl.h similarity index 91% rename from bhyve/smbiostbl.h rename to include/xhyve/smbiostbl.h index e8b3a4f..a860419 100644 --- a/bhyve/smbiostbl.h +++ b/include/xhyve/smbiostbl.h @@ -26,11 +26,6 @@ * $FreeBSD$ */ -#ifndef _SMBIOSTBL_H_ -#define _SMBIOSTBL_H_ +#pragma once -struct vmctx; - -int smbios_build(struct vmctx *ctx); - -#endif /* _SMBIOSTBL_H_ */ +int smbios_build(void); diff --git a/include/xhyve/support/acpi_hpet.h b/include/xhyve/support/acpi_hpet.h new file mode 100644 index 0000000..034d6af --- /dev/null +++ b/include/xhyve/support/acpi_hpet.h @@ -0,0 +1,64 @@ +/*- + * Copyright (c) 2005 Poul-Henning Kamp + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#pragma once + +#define HPET_MEM_WIDTH 0x400 /* Expected memory region size */ + +/* General registers */ +#define HPET_CAPABILITIES 0x0 /* General capabilities and ID */ +#define HPET_CAP_VENDOR_ID 0xffff0000 +#define HPET_CAP_LEG_RT 0x00008000 +#define HPET_CAP_COUNT_SIZE 0x00002000 /* 1 = 64-bit, 0 = 32-bit */ +#define HPET_CAP_NUM_TIM 0x00001f00 +#define HPET_CAP_REV_ID 0x000000ff +#define HPET_PERIOD 0x4 /* Period (1/hz) of timer */ +#define HPET_CONFIG 0x10 /* General configuration register */ +#define HPET_CNF_LEG_RT 0x00000002 +#define HPET_CNF_ENABLE 0x00000001 +#define HPET_ISR 0x20 /* General interrupt status register */ +#define HPET_MAIN_COUNTER 0xf0 /* Main counter register */ + +/* Timer registers */ +#define HPET_TIMER_CAP_CNF(x) ((x) * 0x20 + 0x100) +#define HPET_TCAP_INT_ROUTE 0xffffffff00000000 +#define HPET_TCAP_FSB_INT_DEL 0x00008000 +#define HPET_TCNF_FSB_EN 0x00004000 +#define HPET_TCNF_INT_ROUTE 0x00003e00 +#define HPET_TCNF_32MODE 0x00000100 +#define HPET_TCNF_VAL_SET 0x00000040 +#define HPET_TCAP_SIZE 0x00000020 /* 1 = 64-bit, 0 = 32-bit */ +#define HPET_TCAP_PER_INT 0x00000010 /* Supports periodic interrupts */ +#define HPET_TCNF_TYPE 0x00000008 /* 1 = periodic, 0 = one-shot */ +#define HPET_TCNF_INT_ENB 0x00000004 +#define HPET_TCNF_INT_TYPE 0x00000002 /* 1 = level triggered, 0 = edge */ +#define HPET_TIMER_COMPARATOR(x) ((x) * 0x20 + 0x108) +#define HPET_TIMER_FSB_VAL(x) ((x) * 0x20 + 0x110) +#define HPET_TIMER_FSB_ADDR(x) ((x) * 0x20 + 0x114) + +#define HPET_MIN_CYCLES 128 /* Period considered reliable. */ diff --git a/include/xhyve/support/apicreg.h b/include/xhyve/support/apicreg.h new file mode 100644 index 0000000..a36ccd5 --- /dev/null +++ b/include/xhyve/support/apicreg.h @@ -0,0 +1,509 @@ +/*- + * Copyright (c) 1996, by Peter Wemm and Steve Passe + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. The name of the developer may NOT be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#pragma once + +#include + +/* + * Local && I/O APIC definitions. + */ + +/* + * Pentium P54C+ Built-in APIC + * (Advanced programmable Interrupt Controller) + * + * Base Address of Built-in APIC in memory location + * is 0xfee00000. + * + * Map of APIC Registers: + * + * Offset (hex) Description Read/Write state + * 000 Reserved + * 010 Reserved + * 020 ID Local APIC ID R/W + * 030 VER Local APIC Version R + * 040 Reserved + * 050 Reserved + * 060 Reserved + * 070 Reserved + * 080 Task Priority Register R/W + * 090 Arbitration Priority Register R + * 0A0 Processor Priority Register R + * 0B0 EOI Register W + * 0C0 RRR Remote read R + * 0D0 Logical Destination R/W + * 0E0 Destination Format Register 0..27 R; 28..31 R/W + * 0F0 SVR Spurious Interrupt Vector Reg. 0..3 R; 4..9 R/W + * 100 ISR 000-031 R + * 110 ISR 032-063 R + * 120 ISR 064-095 R + * 130 ISR 095-128 R + * 140 ISR 128-159 R + * 150 ISR 160-191 R + * 160 ISR 192-223 R + * 170 ISR 224-255 R + * 180 TMR 000-031 R + * 190 TMR 032-063 R + * 1A0 TMR 064-095 R + * 1B0 TMR 095-128 R + * 1C0 TMR 128-159 R + * 1D0 TMR 160-191 R + * 1E0 TMR 192-223 R + * 1F0 TMR 224-255 R + * 200 IRR 000-031 R + * 210 IRR 032-063 R + * 220 IRR 064-095 R + * 230 IRR 095-128 R + * 240 IRR 128-159 R + * 250 IRR 160-191 R + * 260 IRR 192-223 R + * 270 IRR 224-255 R + * 280 Error Status Register R + * 290 Reserved + * 2A0 Reserved + * 2B0 Reserved + * 2C0 Reserved + * 2D0 Reserved + * 2E0 Reserved + * 2F0 Local Vector Table (CMCI) R/W + * 300 ICR_LOW Interrupt Command Reg. (0-31) R/W + * 310 ICR_HI Interrupt Command Reg. (32-63) R/W + * 320 Local Vector Table (Timer) R/W + * 330 Local Vector Table (Thermal) R/W (PIV+) + * 340 Local Vector Table (Performance) R/W (P6+) + * 350 LVT1 Local Vector Table (LINT0) R/W + * 360 LVT2 Local Vector Table (LINT1) R/W + * 370 LVT3 Local Vector Table (ERROR) R/W + * 380 Initial Count Reg. for Timer R/W + * 390 Current Count of Timer R + * 3A0 Reserved + * 3B0 Reserved + * 3C0 Reserved + * 3D0 Reserved + * 3E0 Timer Divide Configuration Reg. R/W + * 3F0 Reserved + */ + + +/****************************************************************************** + * global defines, etc. + */ + + +/****************************************************************************** + * LOCAL APIC structure + */ + +#define PAD3 int : 32; int : 32; int : 32 +#define PAD4 int : 32; int : 32; int : 32; int : 32 + +struct LAPIC { + /* reserved */ PAD4; + /* reserved */ PAD4; + uint32_t id; PAD3; + uint32_t version; PAD3; + /* reserved */ PAD4; + /* reserved */ PAD4; + /* reserved */ PAD4; + /* reserved */ PAD4; + uint32_t tpr; PAD3; + uint32_t apr; PAD3; + uint32_t ppr; PAD3; + uint32_t eoi; PAD3; + /* reserved */ PAD4; + uint32_t ldr; PAD3; + uint32_t dfr; PAD3; + uint32_t svr; PAD3; + uint32_t isr0; PAD3; + uint32_t isr1; PAD3; + uint32_t isr2; PAD3; + uint32_t isr3; PAD3; + uint32_t isr4; PAD3; + uint32_t isr5; PAD3; + uint32_t isr6; PAD3; + uint32_t isr7; PAD3; + uint32_t tmr0; PAD3; + uint32_t tmr1; PAD3; + uint32_t tmr2; PAD3; + uint32_t tmr3; PAD3; + uint32_t tmr4; PAD3; + uint32_t tmr5; PAD3; + uint32_t tmr6; PAD3; + uint32_t tmr7; PAD3; + uint32_t irr0; PAD3; + uint32_t irr1; PAD3; + uint32_t irr2; PAD3; + uint32_t irr3; PAD3; + uint32_t irr4; PAD3; + uint32_t irr5; PAD3; + uint32_t irr6; PAD3; + uint32_t irr7; PAD3; + uint32_t esr; PAD3; + /* reserved */ PAD4; + /* reserved */ PAD4; + /* reserved */ PAD4; + /* reserved */ PAD4; + /* reserved */ PAD4; + /* reserved */ PAD4; + uint32_t lvt_cmci; PAD3; + uint32_t icr_lo; PAD3; + uint32_t icr_hi; PAD3; + uint32_t lvt_timer; PAD3; + uint32_t lvt_thermal; PAD3; + uint32_t lvt_pcint; PAD3; + uint32_t lvt_lint0; PAD3; + uint32_t lvt_lint1; PAD3; + uint32_t lvt_error; PAD3; + uint32_t icr_timer; PAD3; + uint32_t ccr_timer; PAD3; + /* reserved */ PAD4; + /* reserved */ PAD4; + /* reserved */ PAD4; + /* reserved */ PAD4; + uint32_t dcr_timer; PAD3; + /* reserved */ PAD4; +}; + +typedef struct LAPIC lapic_t; + +enum LAPIC_REGISTERS { + LAPIC_ID = 0x2, + LAPIC_VERSION = 0x3, + LAPIC_TPR = 0x8, + LAPIC_APR = 0x9, + LAPIC_PPR = 0xa, + LAPIC_EOI = 0xb, + LAPIC_LDR = 0xd, + LAPIC_DFR = 0xe, /* Not in x2APIC */ + LAPIC_SVR = 0xf, + LAPIC_ISR0 = 0x10, + LAPIC_ISR1 = 0x11, + LAPIC_ISR2 = 0x12, + LAPIC_ISR3 = 0x13, + LAPIC_ISR4 = 0x14, + LAPIC_ISR5 = 0x15, + LAPIC_ISR6 = 0x16, + LAPIC_ISR7 = 0x17, + LAPIC_TMR0 = 0x18, + LAPIC_TMR1 = 0x19, + LAPIC_TMR2 = 0x1a, + LAPIC_TMR3 = 0x1b, + LAPIC_TMR4 = 0x1c, + LAPIC_TMR5 = 0x1d, + LAPIC_TMR6 = 0x1e, + LAPIC_TMR7 = 0x1f, + LAPIC_IRR0 = 0x20, + LAPIC_IRR1 = 0x21, + LAPIC_IRR2 = 0x22, + LAPIC_IRR3 = 0x23, + LAPIC_IRR4 = 0x24, + LAPIC_IRR5 = 0x25, + LAPIC_IRR6 = 0x26, + LAPIC_IRR7 = 0x27, + LAPIC_ESR = 0x28, + LAPIC_LVT_CMCI = 0x2f, + LAPIC_ICR_LO = 0x30, + LAPIC_ICR_HI = 0x31, /* Not in x2APIC */ + LAPIC_LVT_TIMER = 0x32, + LAPIC_LVT_THERMAL = 0x33, + LAPIC_LVT_PCINT = 0x34, + LAPIC_LVT_LINT0 = 0x35, + LAPIC_LVT_LINT1 = 0x36, + LAPIC_LVT_ERROR = 0x37, + LAPIC_ICR_TIMER = 0x38, + LAPIC_CCR_TIMER = 0x39, + LAPIC_DCR_TIMER = 0x3e, + LAPIC_SELF_IPI = 0x3f, /* Only in x2APIC */ +}; + +/* + * The LAPIC_SELF_IPI register only exists in x2APIC mode. The + * formula below is applicable only to reserve the memory region, + * i.e. for xAPIC mode, where LAPIC_SELF_IPI finely serves as the + * address past end of the region. + */ +#define LAPIC_MEM_REGION (LAPIC_SELF_IPI * 0x10) + +#define LAPIC_MEM_MUL 0x10 + +/****************************************************************************** + * I/O APIC structure + */ + +struct IOAPIC { + uint32_t ioregsel; PAD3; + uint32_t iowin; PAD3; +}; + +typedef struct IOAPIC ioapic_t; + +#undef PAD4 +#undef PAD3 + + +/****************************************************************************** + * various code 'logical' values + */ + +/****************************************************************************** + * LOCAL APIC defines + */ + +/* default physical locations of LOCAL (CPU) APICs */ +#define DEFAULT_APIC_BASE 0xfee00000 + +/* constants relating to APIC ID registers */ +#define APIC_ID_MASK 0xff000000 +#define APIC_ID_SHIFT 24 +#define APIC_ID_CLUSTER 0xf0 +#define APIC_ID_CLUSTER_ID 0x0f +#define APIC_MAX_CLUSTER 0xe +#define APIC_MAX_INTRACLUSTER_ID 3 +#define APIC_ID_CLUSTER_SHIFT 4 + +/* fields in VER */ +#define APIC_VER_VERSION 0x000000ff +#define APIC_VER_MAXLVT 0x00ff0000 +#define MAXLVTSHIFT 16 +#define APIC_VER_EOI_SUPPRESSION 0x01000000 + +/* fields in LDR */ +#define APIC_LDR_RESERVED 0x00ffffff + +/* fields in DFR */ +#define APIC_DFR_RESERVED 0x0fffffff +#define APIC_DFR_MODEL_MASK 0xf0000000 +#define APIC_DFR_MODEL_FLAT 0xf0000000 +#define APIC_DFR_MODEL_CLUSTER 0x00000000 + +/* fields in SVR */ +#define APIC_SVR_VECTOR 0x000000ff +#define APIC_SVR_VEC_PROG 0x000000f0 +#define APIC_SVR_VEC_FIX 0x0000000f +#define APIC_SVR_ENABLE 0x00000100 +# define APIC_SVR_SWDIS 0x00000000 +# define APIC_SVR_SWEN 0x00000100 +#define APIC_SVR_FOCUS 0x00000200 +# define APIC_SVR_FEN 0x00000000 +# define APIC_SVR_FDIS 0x00000200 +#define APIC_SVR_EOI_SUPPRESSION 0x00001000 + +/* fields in TPR */ +#define APIC_TPR_PRIO 0x000000ff +# define APIC_TPR_INT 0x000000f0 +# define APIC_TPR_SUB 0x0000000f + +/* fields in ESR */ +#define APIC_ESR_SEND_CS_ERROR 0x00000001 +#define APIC_ESR_RECEIVE_CS_ERROR 0x00000002 +#define APIC_ESR_SEND_ACCEPT 0x00000004 +#define APIC_ESR_RECEIVE_ACCEPT 0x00000008 +#define APIC_ESR_SEND_ILLEGAL_VECTOR 0x00000020 +#define APIC_ESR_RECEIVE_ILLEGAL_VECTOR 0x00000040 +#define APIC_ESR_ILLEGAL_REGISTER 0x00000080 + +/* fields in ICR_LOW */ +#define APIC_VECTOR_MASK 0x000000ff + +#define APIC_DELMODE_MASK 0x00000700 +# define APIC_DELMODE_FIXED 0x00000000 +# define APIC_DELMODE_LOWPRIO 0x00000100 +# define APIC_DELMODE_SMI 0x00000200 +# define APIC_DELMODE_RR 0x00000300 +# define APIC_DELMODE_NMI 0x00000400 +# define APIC_DELMODE_INIT 0x00000500 +# define APIC_DELMODE_STARTUP 0x00000600 +# define APIC_DELMODE_RESV 0x00000700 + +#define APIC_DESTMODE_MASK 0x00000800 +# define APIC_DESTMODE_PHY 0x00000000 +# define APIC_DESTMODE_LOG 0x00000800 + +#define APIC_DELSTAT_MASK 0x00001000 +# define APIC_DELSTAT_IDLE 0x00000000 +# define APIC_DELSTAT_PEND 0x00001000 + +#define APIC_RESV1_MASK 0x00002000 + +#define APIC_LEVEL_MASK 0x00004000 +# define APIC_LEVEL_DEASSERT 0x00000000 +# define APIC_LEVEL_ASSERT 0x00004000 + +#define APIC_TRIGMOD_MASK 0x00008000 +# define APIC_TRIGMOD_EDGE 0x00000000 +# define APIC_TRIGMOD_LEVEL 0x00008000 + +#define APIC_RRSTAT_MASK 0x00030000 +# define APIC_RRSTAT_INVALID 0x00000000 +# define APIC_RRSTAT_INPROG 0x00010000 +# define APIC_RRSTAT_VALID 0x00020000 +# define APIC_RRSTAT_RESV 0x00030000 + +#define APIC_DEST_MASK 0x000c0000 +# define APIC_DEST_DESTFLD 0x00000000 +# define APIC_DEST_SELF 0x00040000 +# define APIC_DEST_ALLISELF 0x00080000 +# define APIC_DEST_ALLESELF 0x000c0000 + +#define APIC_RESV2_MASK 0xfff00000 + +#define APIC_ICRLO_RESV_MASK (APIC_RESV1_MASK | APIC_RESV2_MASK) + +/* fields in LVT1/2 */ +#define APIC_LVT_VECTOR 0x000000ff +#define APIC_LVT_DM 0x00000700 +# define APIC_LVT_DM_FIXED 0x00000000 +# define APIC_LVT_DM_SMI 0x00000200 +# define APIC_LVT_DM_NMI 0x00000400 +# define APIC_LVT_DM_INIT 0x00000500 +# define APIC_LVT_DM_EXTINT 0x00000700 +#define APIC_LVT_DS 0x00001000 +#define APIC_LVT_IIPP 0x00002000 +#define APIC_LVT_IIPP_INTALO 0x00002000 +#define APIC_LVT_IIPP_INTAHI 0x00000000 +#define APIC_LVT_RIRR 0x00004000 +#define APIC_LVT_TM 0x00008000 +#define APIC_LVT_M 0x00010000 + + +/* fields in LVT Timer */ +#define APIC_LVTT_VECTOR 0x000000ff +#define APIC_LVTT_DS 0x00001000 +#define APIC_LVTT_M 0x00010000 +#define APIC_LVTT_TM 0x00020000 +# define APIC_LVTT_TM_ONE_SHOT 0x00000000 +# define APIC_LVTT_TM_PERIODIC 0x00020000 + + +/* APIC timer current count */ +#define APIC_TIMER_MAX_COUNT 0xffffffff + +/* fields in TDCR */ +#define APIC_TDCR_2 0x00 +#define APIC_TDCR_4 0x01 +#define APIC_TDCR_8 0x02 +#define APIC_TDCR_16 0x03 +#define APIC_TDCR_32 0x08 +#define APIC_TDCR_64 0x09 +#define APIC_TDCR_128 0x0a +#define APIC_TDCR_1 0x0b + +/* LVT table indices */ +#define APIC_LVT_LINT0 0 +#define APIC_LVT_LINT1 1 +#define APIC_LVT_TIMER 2 +#define APIC_LVT_ERROR 3 +#define APIC_LVT_PMC 4 +#define APIC_LVT_THERMAL 5 +#define APIC_LVT_CMCI 6 +#define APIC_LVT_MAX APIC_LVT_CMCI + +/****************************************************************************** + * I/O APIC defines + */ + +/* default physical locations of an IO APIC */ +#define DEFAULT_IO_APIC_BASE 0xfec00000 + +/* window register offset */ +#define IOAPIC_WINDOW 0x10 +#define IOAPIC_EOIR 0x40 + +/* indexes into IO APIC */ +#define IOAPIC_ID 0x00 +#define IOAPIC_VER 0x01 +#define IOAPIC_ARB 0x02 +#define IOAPIC_REDTBL 0x10 +#define IOAPIC_REDTBL0 IOAPIC_REDTBL +#define IOAPIC_REDTBL1 (IOAPIC_REDTBL+0x02) +#define IOAPIC_REDTBL2 (IOAPIC_REDTBL+0x04) +#define IOAPIC_REDTBL3 (IOAPIC_REDTBL+0x06) +#define IOAPIC_REDTBL4 (IOAPIC_REDTBL+0x08) +#define IOAPIC_REDTBL5 (IOAPIC_REDTBL+0x0a) +#define IOAPIC_REDTBL6 (IOAPIC_REDTBL+0x0c) +#define IOAPIC_REDTBL7 (IOAPIC_REDTBL+0x0e) +#define IOAPIC_REDTBL8 (IOAPIC_REDTBL+0x10) +#define IOAPIC_REDTBL9 (IOAPIC_REDTBL+0x12) +#define IOAPIC_REDTBL10 (IOAPIC_REDTBL+0x14) +#define IOAPIC_REDTBL11 (IOAPIC_REDTBL+0x16) +#define IOAPIC_REDTBL12 (IOAPIC_REDTBL+0x18) +#define IOAPIC_REDTBL13 (IOAPIC_REDTBL+0x1a) +#define IOAPIC_REDTBL14 (IOAPIC_REDTBL+0x1c) +#define IOAPIC_REDTBL15 (IOAPIC_REDTBL+0x1e) +#define IOAPIC_REDTBL16 (IOAPIC_REDTBL+0x20) +#define IOAPIC_REDTBL17 (IOAPIC_REDTBL+0x22) +#define IOAPIC_REDTBL18 (IOAPIC_REDTBL+0x24) +#define IOAPIC_REDTBL19 (IOAPIC_REDTBL+0x26) +#define IOAPIC_REDTBL20 (IOAPIC_REDTBL+0x28) +#define IOAPIC_REDTBL21 (IOAPIC_REDTBL+0x2a) +#define IOAPIC_REDTBL22 (IOAPIC_REDTBL+0x2c) +#define IOAPIC_REDTBL23 (IOAPIC_REDTBL+0x2e) + +/* fields in VER */ +#define IOART_VER_VERSION 0x000000ff +#define IOART_VER_MAXREDIR 0x00ff0000 +#define MAXREDIRSHIFT 16 + +/* + * fields in the IO APIC's redirection table entries + */ +#define IOART_DEST APIC_ID_MASK /* broadcast addr: all APICs */ + +#define IOART_RESV 0x00fe0000 /* reserved */ + +#define IOART_INTMASK 0x00010000 /* R/W: INTerrupt mask */ +# define IOART_INTMCLR 0x00000000 /* clear, allow INTs */ +# define IOART_INTMSET 0x00010000 /* set, inhibit INTs */ + +#define IOART_TRGRMOD 0x00008000 /* R/W: trigger mode */ +# define IOART_TRGREDG 0x00000000 /* edge */ +# define IOART_TRGRLVL 0x00008000 /* level */ + +#define IOART_REM_IRR 0x00004000 /* RO: remote IRR */ + +#define IOART_INTPOL 0x00002000 /* R/W: INT input pin polarity */ +# define IOART_INTAHI 0x00000000 /* active high */ +# define IOART_INTALO 0x00002000 /* active low */ + +#define IOART_DELIVS 0x00001000 /* RO: delivery status */ + +#define IOART_DESTMOD 0x00000800 /* R/W: destination mode */ +# define IOART_DESTPHY 0x00000000 /* physical */ +# define IOART_DESTLOG 0x00000800 /* logical */ + +#define IOART_DELMOD 0x00000700 /* R/W: delivery mode */ +# define IOART_DELFIXED 0x00000000 /* fixed */ +# define IOART_DELLOPRI 0x00000100 /* lowest priority */ +# define IOART_DELSMI 0x00000200 /* System Management INT */ +# define IOART_DELRSV1 0x00000300 /* reserved */ +# define IOART_DELNMI 0x00000400 /* NMI signal */ +# define IOART_DELINIT 0x00000500 /* INIT signal */ +# define IOART_DELRSV2 0x00000600 /* reserved */ +# define IOART_DELEXINT 0x00000700 /* External INTerrupt */ + +#define IOART_INTVEC 0x000000ff /* R/W: INTerrupt vector field */ diff --git a/include/xhyve/support/ata.h b/include/xhyve/support/ata.h new file mode 100644 index 0000000..7d0a7fd --- /dev/null +++ b/include/xhyve/support/ata.h @@ -0,0 +1,643 @@ +/*- + * Copyright (c) 2000 - 2008 Søren Schmidt + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#pragma once + +#include + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +#pragma clang diagnostic ignored "-Wpacked" + +/* ATA/ATAPI device parameters */ +struct ata_params { +/*000*/ u_int16_t config; /* configuration info */ +#define ATA_PROTO_MASK 0x8003 +#define ATA_PROTO_ATAPI 0x8000 +#define ATA_PROTO_ATAPI_12 0x8000 +#define ATA_PROTO_ATAPI_16 0x8001 +#define ATA_PROTO_CFA 0x848a +#define ATA_ATAPI_TYPE_MASK 0x1f00 +#define ATA_ATAPI_TYPE_DIRECT 0x0000 /* disk/floppy */ +#define ATA_ATAPI_TYPE_TAPE 0x0100 /* streaming tape */ +#define ATA_ATAPI_TYPE_CDROM 0x0500 /* CD-ROM device */ +#define ATA_ATAPI_TYPE_OPTICAL 0x0700 /* optical disk */ +#define ATA_DRQ_MASK 0x0060 +#define ATA_DRQ_SLOW 0x0000 /* cpu 3 ms delay */ +#define ATA_DRQ_INTR 0x0020 /* interrupt 10 ms delay */ +#define ATA_DRQ_FAST 0x0040 /* accel 50 us delay */ +#define ATA_RESP_INCOMPLETE 0x0004 + +/*001*/ u_int16_t cylinders; /* # of cylinders */ +/*002*/ u_int16_t specconf; /* specific configuration */ +/*003*/ u_int16_t heads; /* # heads */ + u_int16_t obsolete4; + u_int16_t obsolete5; +/*006*/ u_int16_t sectors; /* # sectors/track */ +/*007*/ u_int16_t vendor7[3]; +/*010*/ u_int8_t serial[20]; /* serial number */ +/*020*/ u_int16_t retired20; + u_int16_t retired21; + u_int16_t obsolete22; +/*023*/ u_int8_t revision[8]; /* firmware revision */ +/*027*/ u_int8_t model[40]; /* model name */ +/*047*/ u_int16_t sectors_intr; /* sectors per interrupt */ +/*048*/ u_int16_t usedmovsd; /* double word read/write? */ +/*049*/ u_int16_t capabilities1; +#define ATA_SUPPORT_DMA 0x0100 +#define ATA_SUPPORT_LBA 0x0200 +#define ATA_SUPPORT_IORDY 0x0400 +#define ATA_SUPPORT_IORDYDIS 0x0800 +#define ATA_SUPPORT_OVERLAP 0x4000 + +/*050*/ u_int16_t capabilities2; +/*051*/ u_int16_t retired_piomode; /* PIO modes 0-2 */ +#define ATA_RETIRED_PIO_MASK 0x0300 + +/*052*/ u_int16_t retired_dmamode; /* DMA modes */ +#define ATA_RETIRED_DMA_MASK 0x0003 + +/*053*/ u_int16_t atavalid; /* fields valid */ +#define ATA_FLAG_54_58 0x0001 /* words 54-58 valid */ +#define ATA_FLAG_64_70 0x0002 /* words 64-70 valid */ +#define ATA_FLAG_88 0x0004 /* word 88 valid */ + +/*054*/ u_int16_t current_cylinders; +/*055*/ u_int16_t current_heads; +/*056*/ u_int16_t current_sectors; +/*057*/ u_int16_t current_size_1; +/*058*/ u_int16_t current_size_2; +/*059*/ u_int16_t multi; +#define ATA_MULTI_VALID 0x0100 + +/*060*/ u_int16_t lba_size_1; + u_int16_t lba_size_2; + u_int16_t obsolete62; +/*063*/ u_int16_t mwdmamodes; /* multiword DMA modes */ +/*064*/ u_int16_t apiomodes; /* advanced PIO modes */ + +/*065*/ u_int16_t mwdmamin; /* min. M/W DMA time/word ns */ +/*066*/ u_int16_t mwdmarec; /* rec. M/W DMA time ns */ +/*067*/ u_int16_t pioblind; /* min. PIO cycle w/o flow */ +/*068*/ u_int16_t pioiordy; /* min. PIO cycle IORDY flow */ +/*069*/ u_int16_t support3; +#define ATA_SUPPORT_RZAT 0x0020 +#define ATA_SUPPORT_DRAT 0x4000 + u_int16_t reserved70; +/*071*/ u_int16_t rlsovlap; /* rel time (us) for overlap */ +/*072*/ u_int16_t rlsservice; /* rel time (us) for service */ + u_int16_t reserved73; + u_int16_t reserved74; +/*075*/ u_int16_t queue; +#define ATA_QUEUE_LEN(x) ((x) & 0x001f) + +/*76*/ u_int16_t satacapabilities; +#define ATA_SATA_GEN1 0x0002 +#define ATA_SATA_GEN2 0x0004 +#define ATA_SATA_GEN3 0x0008 +#define ATA_SUPPORT_NCQ 0x0100 +#define ATA_SUPPORT_IFPWRMNGTRCV 0x0200 +#define ATA_SUPPORT_PHYEVENTCNT 0x0400 +#define ATA_SUPPORT_NCQ_UNLOAD 0x0800 +#define ATA_SUPPORT_NCQ_PRIO 0x1000 +#define ATA_SUPPORT_HAPST 0x2000 +#define ATA_SUPPORT_DAPST 0x4000 +#define ATA_SUPPORT_READLOGDMAEXT 0x8000 + +/*77*/ u_int16_t satacapabilities2; +#define ATA_SATA_CURR_GEN_MASK 0x0006 +#define ATA_SUPPORT_NCQ_STREAM 0x0010 +#define ATA_SUPPORT_NCQ_QMANAGEMENT 0x0020 +#define ATA_SUPPORT_RCVSND_FPDMA_QUEUED 0x0040 +/*78*/ u_int16_t satasupport; +#define ATA_SUPPORT_NONZERO 0x0002 +#define ATA_SUPPORT_AUTOACTIVATE 0x0004 +#define ATA_SUPPORT_IFPWRMNGT 0x0008 +#define ATA_SUPPORT_INORDERDATA 0x0010 +#define ATA_SUPPORT_ASYNCNOTIF 0x0020 +#define ATA_SUPPORT_SOFTSETPRESERVE 0x0040 +/*79*/ u_int16_t sataenabled; +#define ATA_ENABLED_DAPST 0x0080 + +/*080*/ u_int16_t version_major; +/*081*/ u_int16_t version_minor; + + struct { +/*082/085*/ u_int16_t command1; +#define ATA_SUPPORT_SMART 0x0001 +#define ATA_SUPPORT_SECURITY 0x0002 +#define ATA_SUPPORT_REMOVABLE 0x0004 +#define ATA_SUPPORT_POWERMGT 0x0008 +#define ATA_SUPPORT_PACKET 0x0010 +#define ATA_SUPPORT_WRITECACHE 0x0020 +#define ATA_SUPPORT_LOOKAHEAD 0x0040 +#define ATA_SUPPORT_RELEASEIRQ 0x0080 +#define ATA_SUPPORT_SERVICEIRQ 0x0100 +#define ATA_SUPPORT_RESET 0x0200 +#define ATA_SUPPORT_PROTECTED 0x0400 +#define ATA_SUPPORT_WRITEBUFFER 0x1000 +#define ATA_SUPPORT_READBUFFER 0x2000 +#define ATA_SUPPORT_NOP 0x4000 + +/*083/086*/ u_int16_t command2; +#define ATA_SUPPORT_MICROCODE 0x0001 +#define ATA_SUPPORT_QUEUED 0x0002 +#define ATA_SUPPORT_CFA 0x0004 +#define ATA_SUPPORT_APM 0x0008 +#define ATA_SUPPORT_NOTIFY 0x0010 +#define ATA_SUPPORT_STANDBY 0x0020 +#define ATA_SUPPORT_SPINUP 0x0040 +#define ATA_SUPPORT_MAXSECURITY 0x0100 +#define ATA_SUPPORT_AUTOACOUSTIC 0x0200 +#define ATA_SUPPORT_ADDRESS48 0x0400 +#define ATA_SUPPORT_OVERLAY 0x0800 +#define ATA_SUPPORT_FLUSHCACHE 0x1000 +#define ATA_SUPPORT_FLUSHCACHE48 0x2000 + +/*084/087*/ u_int16_t extension; +#define ATA_SUPPORT_SMARTLOG 0x0001 +#define ATA_SUPPORT_SMARTTEST 0x0002 +#define ATA_SUPPORT_MEDIASN 0x0004 +#define ATA_SUPPORT_MEDIAPASS 0x0008 +#define ATA_SUPPORT_STREAMING 0x0010 +#define ATA_SUPPORT_GENLOG 0x0020 +#define ATA_SUPPORT_WRITEDMAFUAEXT 0x0040 +#define ATA_SUPPORT_WRITEDMAQFUAEXT 0x0080 +#define ATA_SUPPORT_64BITWWN 0x0100 +#define ATA_SUPPORT_UNLOAD 0x2000 + } __packed support, enabled; + +/*088*/ u_int16_t udmamodes; /* UltraDMA modes */ +/*089*/ u_int16_t erase_time; /* time req'd in 2min units */ +/*090*/ u_int16_t enhanced_erase_time; /* time req'd in 2min units */ +/*091*/ u_int16_t apm_value; +/*092*/ u_int16_t master_passwd_revision; /* password revision code */ +/*093*/ u_int16_t hwres; +#define ATA_CABLE_ID 0x2000 + +/*094*/ u_int16_t acoustic; +#define ATA_ACOUSTIC_CURRENT(x) ((x) & 0x00ff) +#define ATA_ACOUSTIC_VENDOR(x) (((x) & 0xff00) >> 8) + +/*095*/ u_int16_t stream_min_req_size; +/*096*/ u_int16_t stream_transfer_time; +/*097*/ u_int16_t stream_access_latency; +/*098*/ u_int32_t stream_granularity; +/*100*/ u_int16_t lba_size48_1; + u_int16_t lba_size48_2; + u_int16_t lba_size48_3; + u_int16_t lba_size48_4; + u_int16_t reserved104; +/*105*/ u_int16_t max_dsm_blocks; +/*106*/ u_int16_t pss; +#define ATA_PSS_LSPPS 0x000F +#define ATA_PSS_LSSABOVE512 0x1000 +#define ATA_PSS_MULTLS 0x2000 +#define ATA_PSS_VALID_MASK 0xC000 +#define ATA_PSS_VALID_VALUE 0x4000 +/*107*/ u_int16_t isd; +/*108*/ u_int16_t wwn[4]; + u_int16_t reserved112[5]; +/*117*/ u_int16_t lss_1; +/*118*/ u_int16_t lss_2; +/*119*/ u_int16_t support2; +#define ATA_SUPPORT_WRITEREADVERIFY 0x0002 +#define ATA_SUPPORT_WRITEUNCORREXT 0x0004 +#define ATA_SUPPORT_RWLOGDMAEXT 0x0008 +#define ATA_SUPPORT_MICROCODE3 0x0010 +#define ATA_SUPPORT_FREEFALL 0x0020 +/*120*/ u_int16_t enabled2; + u_int16_t reserved121[6]; +/*127*/ u_int16_t removable_status; +/*128*/ u_int16_t security_status; +#define ATA_SECURITY_LEVEL 0x0100 /* 0: high, 1: maximum */ +#define ATA_SECURITY_ENH_SUPP 0x0020 /* enhanced erase supported */ +#define ATA_SECURITY_COUNT_EXP 0x0010 /* count expired */ +#define ATA_SECURITY_FROZEN 0x0008 /* security config is frozen */ +#define ATA_SECURITY_LOCKED 0x0004 /* drive is locked */ +#define ATA_SECURITY_ENABLED 0x0002 /* ATA Security is enabled */ +#define ATA_SECURITY_SUPPORTED 0x0001 /* ATA Security is supported */ + + u_int16_t reserved129[31]; +/*160*/ u_int16_t cfa_powermode1; + u_int16_t reserved161; +/*162*/ u_int16_t cfa_kms_support; +/*163*/ u_int16_t cfa_trueide_modes; +/*164*/ u_int16_t cfa_memory_modes; + u_int16_t reserved165[4]; +/*169*/ u_int16_t support_dsm; +#define ATA_SUPPORT_DSM_TRIM 0x0001 + u_int16_t reserved170[6]; +/*176*/ u_int8_t media_serial[60]; +/*206*/ u_int16_t sct; + u_int16_t reserved206[2]; +/*209*/ u_int16_t lsalign; +/*210*/ u_int16_t wrv_sectors_m3_1; + u_int16_t wrv_sectors_m3_2; +/*212*/ u_int16_t wrv_sectors_m2_1; + u_int16_t wrv_sectors_m2_2; +/*214*/ u_int16_t nv_cache_caps; +/*215*/ u_int16_t nv_cache_size_1; + u_int16_t nv_cache_size_2; +/*217*/ u_int16_t media_rotation_rate; +#define ATA_RATE_NOT_REPORTED 0x0000 +#define ATA_RATE_NON_ROTATING 0x0001 + u_int16_t reserved218; +/*219*/ u_int16_t nv_cache_opt; +/*220*/ u_int16_t wrv_mode; + u_int16_t reserved221; +/*222*/ u_int16_t transport_major; +/*223*/ u_int16_t transport_minor; + u_int16_t reserved224[31]; +/*255*/ u_int16_t integrity; +} __packed; + +/* ATA Dataset Management */ +#define ATA_DSM_BLK_SIZE 512 +#define ATA_DSM_BLK_RANGES 64 +#define ATA_DSM_RANGE_SIZE 8 +#define ATA_DSM_RANGE_MAX 65535 + +/* + * ATA Device Register + * + * bit 7 Obsolete (was 1 in early ATA specs) + * bit 6 Sets LBA/CHS mode. 1=LBA, 0=CHS + * bit 5 Obsolete (was 1 in early ATA specs) + * bit 4 1 = Slave Drive, 0 = Master Drive + * bit 3-0 In LBA mode, 27-24 of address. In CHS mode, head number +*/ + +#define ATA_DEV_MASTER 0x00 +#define ATA_DEV_SLAVE 0x10 +#define ATA_DEV_LBA 0x40 + +/* ATA limits */ +#define ATA_MAX_28BIT_LBA 268435455UL + +/* ATA Status Register */ +#define ATA_STATUS_ERROR 0x01 +#define ATA_STATUS_DEVICE_FAULT 0x20 + +/* ATA Error Register */ +#define ATA_ERROR_ABORT 0x04 +#define ATA_ERROR_ID_NOT_FOUND 0x10 + +/* ATA HPA Features */ +#define ATA_HPA_FEAT_MAX_ADDR 0x00 +#define ATA_HPA_FEAT_SET_PWD 0x01 +#define ATA_HPA_FEAT_LOCK 0x02 +#define ATA_HPA_FEAT_UNLOCK 0x03 +#define ATA_HPA_FEAT_FREEZE 0x04 + +/* ATA transfer modes */ +#define ATA_MODE_MASK 0x0f +#define ATA_DMA_MASK 0xf0 +#define ATA_PIO 0x00 +#define ATA_PIO0 0x08 +#define ATA_PIO1 0x09 +#define ATA_PIO2 0x0a +#define ATA_PIO3 0x0b +#define ATA_PIO4 0x0c +#define ATA_PIO_MAX 0x0f +#define ATA_DMA 0x10 +#define ATA_WDMA0 0x20 +#define ATA_WDMA1 0x21 +#define ATA_WDMA2 0x22 +#define ATA_UDMA0 0x40 +#define ATA_UDMA1 0x41 +#define ATA_UDMA2 0x42 +#define ATA_UDMA3 0x43 +#define ATA_UDMA4 0x44 +#define ATA_UDMA5 0x45 +#define ATA_UDMA6 0x46 +#define ATA_SA150 0x47 +#define ATA_SA300 0x48 +#define ATA_DMA_MAX 0x4f + + +/* ATA commands */ +#define ATA_NOP 0x00 /* NOP */ +#define ATA_NF_FLUSHQUEUE 0x00 /* flush queued cmd's */ +#define ATA_NF_AUTOPOLL 0x01 /* start autopoll function */ +#define ATA_DATA_SET_MANAGEMENT 0x06 +#define ATA_DSM_TRIM 0x01 +#define ATA_DEVICE_RESET 0x08 /* reset device */ +#define ATA_READ 0x20 /* read */ +#define ATA_READ48 0x24 /* read 48bit LBA */ +#define ATA_READ_DMA48 0x25 /* read DMA 48bit LBA */ +#define ATA_READ_DMA_QUEUED48 0x26 /* read DMA QUEUED 48bit LBA */ +#define ATA_READ_NATIVE_MAX_ADDRESS48 0x27 /* read native max addr 48bit */ +#define ATA_READ_MUL48 0x29 /* read multi 48bit LBA */ +#define ATA_READ_STREAM_DMA48 0x2a /* read DMA stream 48bit LBA */ +#define ATA_READ_LOG_EXT 0x2f /* read log ext - PIO Data-In */ +#define ATA_READ_STREAM48 0x2b /* read stream 48bit LBA */ +#define ATA_WRITE 0x30 /* write */ +#define ATA_WRITE48 0x34 /* write 48bit LBA */ +#define ATA_WRITE_DMA48 0x35 /* write DMA 48bit LBA */ +#define ATA_WRITE_DMA_QUEUED48 0x36 /* write DMA QUEUED 48bit LBA*/ +#define ATA_SET_MAX_ADDRESS48 0x37 /* set max address 48bit */ +#define ATA_WRITE_MUL48 0x39 /* write multi 48bit LBA */ +#define ATA_WRITE_STREAM_DMA48 0x3a +#define ATA_WRITE_STREAM48 0x3b +#define ATA_WRITE_DMA_FUA48 0x3d +#define ATA_WRITE_DMA_QUEUED_FUA48 0x3e +#define ATA_WRITE_LOG_EXT 0x3f +#define ATA_READ_VERIFY 0x40 +#define ATA_READ_VERIFY48 0x42 +#define ATA_READ_LOG_DMA_EXT 0x47 /* read log DMA ext - PIO Data-In */ +#define ATA_READ_FPDMA_QUEUED 0x60 /* read DMA NCQ */ +#define ATA_WRITE_FPDMA_QUEUED 0x61 /* write DMA NCQ */ +#define ATA_NCQ_NON_DATA 0x63 /* NCQ non-data command */ +#define ATA_SEND_FPDMA_QUEUED 0x64 /* send DMA NCQ */ +#define ATA_SFPDMA_DSM 0x00 /* Data set management */ +#define ATA_SFPDMA_DSM_TRIM 0x01 /* Set trim bit in auxilary */ +#define ATA_SFPDMA_HYBRID_EVICT 0x01 /* Hybrid Evict */ +#define ATA_SFPDMA_WLDMA 0x02 /* Write Log DMA EXT */ +#define ATA_RECV_FPDMA_QUEUED 0x65 /* recieve DMA NCQ */ +#define ATA_SEP_ATTN 0x67 /* SEP request */ +#define ATA_SEEK 0x70 /* seek */ +#define ATA_PACKET_CMD 0xa0 /* packet command */ +#define ATA_ATAPI_IDENTIFY 0xa1 /* get ATAPI params*/ +#define ATA_SERVICE 0xa2 /* service command */ +#define ATA_SMART_CMD 0xb0 /* SMART command */ +#define ATA_CFA_ERASE 0xc0 /* CFA erase */ +#define ATA_READ_MUL 0xc4 /* read multi */ +#define ATA_WRITE_MUL 0xc5 /* write multi */ +#define ATA_SET_MULTI 0xc6 /* set multi size */ +#define ATA_READ_DMA_QUEUED 0xc7 /* read DMA QUEUED */ +#define ATA_READ_DMA 0xc8 /* read DMA */ +#define ATA_WRITE_DMA 0xca /* write DMA */ +#define ATA_WRITE_DMA_QUEUED 0xcc /* write DMA QUEUED */ +#define ATA_WRITE_MUL_FUA48 0xce +#define ATA_STANDBY_IMMEDIATE 0xe0 /* standby immediate */ +#define ATA_IDLE_IMMEDIATE 0xe1 /* idle immediate */ +#define ATA_STANDBY_CMD 0xe2 /* standby */ +#define ATA_IDLE_CMD 0xe3 /* idle */ +#define ATA_READ_BUFFER 0xe4 /* read buffer */ +#define ATA_READ_PM 0xe4 /* read portmultiplier */ +#define ATA_SLEEP 0xe6 /* sleep */ +#define ATA_FLUSHCACHE 0xe7 /* flush cache to disk */ +#define ATA_WRITE_PM 0xe8 /* write portmultiplier */ +#define ATA_FLUSHCACHE48 0xea /* flush cache to disk */ +#define ATA_ATA_IDENTIFY 0xec /* get ATA params */ +#define ATA_SETFEATURES 0xef /* features command */ +#define ATA_SF_SETXFER 0x03 /* set transfer mode */ +#define ATA_SF_ENAB_WCACHE 0x02 /* enable write cache */ +#define ATA_SF_DIS_WCACHE 0x82 /* disable write cache */ +#define ATA_SF_ENAB_PUIS 0x06 /* enable PUIS */ +#define ATA_SF_DIS_PUIS 0x86 /* disable PUIS */ +#define ATA_SF_PUIS_SPINUP 0x07 /* PUIS spin-up */ +#define ATA_SF_ENAB_RCACHE 0xaa /* enable readahead cache */ +#define ATA_SF_DIS_RCACHE 0x55 /* disable readahead cache */ +#define ATA_SF_ENAB_RELIRQ 0x5d /* enable release interrupt */ +#define ATA_SF_DIS_RELIRQ 0xdd /* disable release interrupt */ +#define ATA_SF_ENAB_SRVIRQ 0x5e /* enable service interrupt */ +#define ATA_SF_DIS_SRVIRQ 0xde /* disable service interrupt */ +#define ATA_SECURITY_SET_PASSWORD 0xf1 /* set drive password */ +#define ATA_SECURITY_UNLOCK 0xf2 /* unlock drive using passwd */ +#define ATA_SECURITY_ERASE_PREPARE 0xf3 /* prepare to erase drive */ +#define ATA_SECURITY_ERASE_UNIT 0xf4 /* erase all blocks on drive */ +#define ATA_SECURITY_FREEZE_LOCK 0xf5 /* freeze security config */ +#define ATA_SECURITY_DISABLE_PASSWORD 0xf6 /* disable drive password */ +#define ATA_READ_NATIVE_MAX_ADDRESS 0xf8 /* read native max address */ +#define ATA_SET_MAX_ADDRESS 0xf9 /* set max address */ + + +/* ATAPI commands */ +#define ATAPI_TEST_UNIT_READY 0x00 /* check if device is ready */ +#define ATAPI_REZERO 0x01 /* rewind */ +#define ATAPI_REQUEST_SENSE 0x03 /* get sense data */ +#define ATAPI_FORMAT 0x04 /* format unit */ +#define ATAPI_READ 0x08 /* read data */ +#define ATAPI_WRITE 0x0a /* write data */ +#define ATAPI_WEOF 0x10 /* write filemark */ +#define ATAPI_WF_WRITE 0x01 +#define ATAPI_SPACE 0x11 /* space command */ +#define ATAPI_SP_FM 0x01 +#define ATAPI_SP_EOD 0x03 +#define ATAPI_INQUIRY 0x12 /* get inquiry data */ +#define ATAPI_MODE_SELECT 0x15 /* mode select */ +#define ATAPI_ERASE 0x19 /* erase */ +#define ATAPI_MODE_SENSE 0x1a /* mode sense */ +#define ATAPI_START_STOP 0x1b /* start/stop unit */ +#define ATAPI_SS_LOAD 0x01 +#define ATAPI_SS_RETENSION 0x02 +#define ATAPI_SS_EJECT 0x04 +#define ATAPI_PREVENT_ALLOW 0x1e /* media removal */ +#define ATAPI_READ_FORMAT_CAPACITIES 0x23 /* get format capacities */ +#define ATAPI_READ_CAPACITY 0x25 /* get volume capacity */ +#define ATAPI_READ_BIG 0x28 /* read data */ +#define ATAPI_WRITE_BIG 0x2a /* write data */ +#define ATAPI_LOCATE 0x2b /* locate to position */ +#define ATAPI_READ_POSITION 0x34 /* read position */ +#define ATAPI_SYNCHRONIZE_CACHE 0x35 /* flush buf, close channel */ +#define ATAPI_WRITE_BUFFER 0x3b /* write device buffer */ +#define ATAPI_READ_BUFFER 0x3c /* read device buffer */ +#define ATAPI_READ_SUBCHANNEL 0x42 /* get subchannel info */ +#define ATAPI_READ_TOC 0x43 /* get table of contents */ +#define ATAPI_PLAY_10 0x45 /* play by lba */ +#define ATAPI_PLAY_MSF 0x47 /* play by MSF address */ +#define ATAPI_PLAY_TRACK 0x48 /* play by track number */ +#define ATAPI_PAUSE 0x4b /* pause audio operation */ +#define ATAPI_READ_DISK_INFO 0x51 /* get disk info structure */ +#define ATAPI_READ_TRACK_INFO 0x52 /* get track info structure */ +#define ATAPI_RESERVE_TRACK 0x53 /* reserve track */ +#define ATAPI_SEND_OPC_INFO 0x54 /* send OPC structurek */ +#define ATAPI_MODE_SELECT_BIG 0x55 /* set device parameters */ +#define ATAPI_REPAIR_TRACK 0x58 /* repair track */ +#define ATAPI_READ_MASTER_CUE 0x59 /* read master CUE info */ +#define ATAPI_MODE_SENSE_BIG 0x5a /* get device parameters */ +#define ATAPI_CLOSE_TRACK 0x5b /* close track/session */ +#define ATAPI_READ_BUFFER_CAPACITY 0x5c /* get buffer capicity */ +#define ATAPI_SEND_CUE_SHEET 0x5d /* send CUE sheet */ +#define ATAPI_SERVICE_ACTION_IN 0x96 /* get service data */ +#define ATAPI_BLANK 0xa1 /* blank the media */ +#define ATAPI_SEND_KEY 0xa3 /* send DVD key structure */ +#define ATAPI_REPORT_KEY 0xa4 /* get DVD key structure */ +#define ATAPI_PLAY_12 0xa5 /* play by lba */ +#define ATAPI_LOAD_UNLOAD 0xa6 /* changer control command */ +#define ATAPI_READ_STRUCTURE 0xad /* get DVD structure */ +#define ATAPI_PLAY_CD 0xb4 /* universal play command */ +#define ATAPI_SET_SPEED 0xbb /* set drive speed */ +#define ATAPI_MECH_STATUS 0xbd /* get changer status */ +#define ATAPI_READ_CD 0xbe /* read data */ +#define ATAPI_POLL_DSC 0xff /* poll DSC status bit */ + + +struct ata_ioc_devices { + int channel; + char name[2][32]; + struct ata_params params[2]; +}; + +/* pr channel ATA ioctl calls */ +#define IOCATAGMAXCHANNEL _IOR('a', 1, int) +#define IOCATAREINIT _IOW('a', 2, int) +#define IOCATAATTACH _IOW('a', 3, int) +#define IOCATADETACH _IOW('a', 4, int) +#define IOCATADEVICES _IOWR('a', 5, struct ata_ioc_devices) + +/* ATAPI request sense structure */ +struct atapi_sense { + u_int8_t error; /* current or deferred errors */ +#define ATA_SENSE_VALID 0x80 + + u_int8_t segment; /* segment number */ + u_int8_t key; /* sense key */ +#define ATA_SENSE_KEY_MASK 0x0f /* sense key mask */ +#define ATA_SENSE_NO_SENSE 0x00 /* no specific sense key info */ +#define ATA_SENSE_RECOVERED_ERROR 0x01 /* command OK, data recovered */ +#define ATA_SENSE_NOT_READY 0x02 /* no access to drive */ +#define ATA_SENSE_MEDIUM_ERROR 0x03 /* non-recovered data error */ +#define ATA_SENSE_HARDWARE_ERROR 0x04 /* non-recoverable HW failure */ +#define ATA_SENSE_ILLEGAL_REQUEST 0x05 /* invalid command param(s) */ +#define ATA_SENSE_UNIT_ATTENTION 0x06 /* media changed */ +#define ATA_SENSE_DATA_PROTECT 0x07 /* write protect */ +#define ATA_SENSE_BLANK_CHECK 0x08 /* blank check */ +#define ATA_SENSE_VENDOR_SPECIFIC 0x09 /* vendor specific skey */ +#define ATA_SENSE_COPY_ABORTED 0x0a /* copy aborted */ +#define ATA_SENSE_ABORTED_COMMAND 0x0b /* command aborted, try again */ +#define ATA_SENSE_EQUAL 0x0c /* equal */ +#define ATA_SENSE_VOLUME_OVERFLOW 0x0d /* volume overflow */ +#define ATA_SENSE_MISCOMPARE 0x0e /* data dont match the medium */ +#define ATA_SENSE_RESERVED 0x0f +#define ATA_SENSE_ILI 0x20; +#define ATA_SENSE_EOM 0x40; +#define ATA_SENSE_FILEMARK 0x80; + + u_int32_t cmd_info; /* cmd information */ + u_int8_t sense_length; /* additional sense len (n-7) */ + u_int32_t cmd_specific_info; /* additional cmd spec info */ + u_int8_t asc; /* additional sense code */ + u_int8_t ascq; /* additional sense code qual */ + u_int8_t replaceable_unit_code; /* replaceable unit code */ + u_int8_t specific; /* sense key specific */ +#define ATA_SENSE_SPEC_VALID 0x80 +#define ATA_SENSE_SPEC_MASK 0x7f + + u_int8_t specific1; /* sense key specific */ + u_int8_t specific2; /* sense key specific */ +} __packed; + +struct ata_ioc_request { + union { + struct { + u_int8_t command; + u_int8_t feature; + u_int64_t lba; + u_int16_t count; + } ata; + struct { + char ccb[16]; + struct atapi_sense sense; + } atapi; + } u; + caddr_t data; + int count; + int flags; +#define ATA_CMD_CONTROL 0x01 +#define ATA_CMD_READ 0x02 +#define ATA_CMD_WRITE 0x04 +#define ATA_CMD_ATAPI 0x08 + + int timeout; + int error; +}; + +struct ata_security_password { + u_int16_t ctrl; +#define ATA_SECURITY_PASSWORD_USER 0x0000 +#define ATA_SECURITY_PASSWORD_MASTER 0x0001 +#define ATA_SECURITY_ERASE_NORMAL 0x0000 +#define ATA_SECURITY_ERASE_ENHANCED 0x0002 +#define ATA_SECURITY_LEVEL_HIGH 0x0000 +#define ATA_SECURITY_LEVEL_MAXIMUM 0x0100 + + u_int8_t password[32]; + u_int16_t revision; + u_int16_t reserved[238]; +}; + +/* pr device ATA ioctl calls */ +#define IOCATAREQUEST _IOWR('a', 100, struct ata_ioc_request) +#define IOCATAGPARM _IOR('a', 101, struct ata_params) +#define IOCATAGMODE _IOR('a', 102, int) +#define IOCATASMODE _IOW('a', 103, int) + +#define IOCATAGSPINDOWN _IOR('a', 104, int) +#define IOCATASSPINDOWN _IOW('a', 105, int) + + +struct ata_ioc_raid_config { + int lun; + int type; +#define AR_JBOD 0x0001 +#define AR_SPAN 0x0002 +#define AR_RAID0 0x0004 +#define AR_RAID1 0x0008 +#define AR_RAID01 0x0010 +#define AR_RAID3 0x0020 +#define AR_RAID4 0x0040 +#define AR_RAID5 0x0080 + + int interleave; + int status; +#define AR_READY 1 +#define AR_DEGRADED 2 +#define AR_REBUILDING 4 + + int progress; + int total_disks; + int disks[16]; +}; + +struct ata_ioc_raid_status { + int lun; + int type; + int interleave; + int status; + int progress; + int total_disks; + struct { + int state; +#define AR_DISK_ONLINE 0x01 +#define AR_DISK_PRESENT 0x02 +#define AR_DISK_SPARE 0x04 + int lun; + } disks[16]; +}; + +/* ATA RAID ioctl calls */ +#define IOCATARAIDCREATE _IOWR('a', 200, struct ata_ioc_raid_config) +#define IOCATARAIDDELETE _IOW('a', 201, int) +#define IOCATARAIDSTATUS _IOWR('a', 202, struct ata_ioc_raid_status) +#define IOCATARAIDADDSPARE _IOW('a', 203, struct ata_ioc_raid_config) +#define IOCATARAIDREBUILD _IOW('a', 204, int) + +#pragma clang diagnostic pop diff --git a/include/xhyve/support/atomic.h b/include/xhyve/support/atomic.h new file mode 100644 index 0000000..b2a8903 --- /dev/null +++ b/include/xhyve/support/atomic.h @@ -0,0 +1,443 @@ +/*- + * Copyright (c) 1998 Doug Rabson + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#pragma once + +#include +#include + +#define __compiler_membar() __asm __volatile(" " : : : "memory") + +#define mb() __asm __volatile("mfence;" : : : "memory") +#define wmb() __asm __volatile("sfence;" : : : "memory") +#define rmb() __asm __volatile("lfence;" : : : "memory") + +/* + * Various simple operations on memory, each of which is atomic in the + * presence of interrupts and multiple processors. + * + * atomic_set_char(P, V) (*(u_char *)(P) |= (V)) + * atomic_clear_char(P, V) (*(u_char *)(P) &= ~(V)) + * atomic_add_char(P, V) (*(u_char *)(P) += (V)) + * atomic_subtract_char(P, V) (*(u_char *)(P) -= (V)) + * + * atomic_set_short(P, V) (*(u_short *)(P) |= (V)) + * atomic_clear_short(P, V) (*(u_short *)(P) &= ~(V)) + * atomic_add_short(P, V) (*(u_short *)(P) += (V)) + * atomic_subtract_short(P, V) (*(u_short *)(P) -= (V)) + * + * atomic_set_int(P, V) (*(u_int *)(P) |= (V)) + * atomic_clear_int(P, V) (*(u_int *)(P) &= ~(V)) + * atomic_add_int(P, V) (*(u_int *)(P) += (V)) + * atomic_subtract_int(P, V) (*(u_int *)(P) -= (V)) + * atomic_swap_int(P, V) (return (*(u_int *)(P)); *(u_int *)(P) = (V);) + * atomic_readandclear_int(P) (return (*(u_int *)(P)); *(u_int *)(P) = 0;) + * + * atomic_set_long(P, V) (*(u_long *)(P) |= (V)) + * atomic_clear_long(P, V) (*(u_long *)(P) &= ~(V)) + * atomic_add_long(P, V) (*(u_long *)(P) += (V)) + * atomic_subtract_long(P, V) (*(u_long *)(P) -= (V)) + * atomic_swap_long(P, V) (return (*(u_long *)(P)); *(u_long *)(P) = (V);) + * atomic_readandclear_long(P) (return (*(u_long *)(P)); *(u_long *)(P) = 0;) + */ + +#define MPLOCKED "lock ; " + +/* + * The assembly is volatilized to avoid code chunk removal by the compiler. + * GCC aggressively reorders operations and memory clobbering is necessary + * in order to avoid that for memory barriers. + */ +#define ATOMIC_ASM(NAME, TYPE, OP, CONS, V) \ +static __inline void \ +atomic_##NAME##_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\ +{ \ + __asm __volatile(MPLOCKED OP \ + : "+m" (*p) \ + : CONS (V) \ + : "cc"); \ +} \ + \ +static __inline void \ +atomic_##NAME##_barr_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\ +{ \ + __asm __volatile(MPLOCKED OP \ + : "+m" (*p) \ + : CONS (V) \ + : "memory", "cc"); \ +} \ +struct __hack + +/* + * Atomic compare and set, used by the mutex functions + * + * if (*dst == expect) *dst = src (all 32 bit words) + * + * Returns 0 on failure, non-zero on success + */ + +static __inline int +atomic_cmpset_int(volatile u_int *dst, u_int expect, u_int src) +{ + u_char res; + + __asm __volatile( + " " MPLOCKED " " + " cmpxchgl %3,%1 ; " + " sete %0 ; " + "# atomic_cmpset_int" + : "=q" (res), /* 0 */ + "+m" (*dst), /* 1 */ + "+a" (expect) /* 2 */ + : "r" (src) /* 3 */ + : "memory", "cc"); + return (res); +} + +static __inline int +atomic_cmpset_long(volatile u_long *dst, u_long expect, u_long src) +{ + u_char res; + + __asm __volatile( + " " MPLOCKED " " + " cmpxchgq %3,%1 ; " + " sete %0 ; " + "# atomic_cmpset_long" + : "=q" (res), /* 0 */ + "+m" (*dst), /* 1 */ + "+a" (expect) /* 2 */ + : "r" (src) /* 3 */ + : "memory", "cc"); + return (res); +} + +/* + * Atomically add the value of v to the integer pointed to by p and return + * the previous value of *p. + */ +static __inline u_int +atomic_fetchadd_int(volatile u_int *p, u_int v) +{ + + __asm __volatile( + " " MPLOCKED " " + " xaddl %0,%1 ; " + "# atomic_fetchadd_int" + : "+r" (v), /* 0 */ + "+m" (*p) /* 1 */ + : : "cc"); + return (v); +} + +/* + * Atomically add the value of v to the long integer pointed to by p and return + * the previous value of *p. + */ +static __inline u_long +atomic_fetchadd_long(volatile u_long *p, u_long v) +{ + + __asm __volatile( + " " MPLOCKED " " + " xaddq %0,%1 ; " + "# atomic_fetchadd_long" + : "+r" (v), /* 0 */ + "+m" (*p) /* 1 */ + : : "cc"); + return (v); +} + +static __inline int +atomic_testandset_int(volatile u_int *p, u_int v) +{ + u_char res; + + __asm __volatile( + " " MPLOCKED " " + " btsl %2,%1 ; " + " setc %0 ; " + "# atomic_testandset_int" + : "=q" (res), /* 0 */ + "+m" (*p) /* 1 */ + : "Ir" (v & 0x1f) /* 2 */ + : "cc"); + return (res); +} + +static __inline int +atomic_testandset_long(volatile u_long *p, u_int v) +{ + u_char res; + + __asm __volatile( + " " MPLOCKED " " + " btsq %2,%1 ; " + " setc %0 ; " + "# atomic_testandset_long" + : "=q" (res), /* 0 */ + "+m" (*p) /* 1 */ + : "Jr" ((u_long)(v & 0x3f)) /* 2 */ + : "cc"); + return (res); +} + +/* + * We assume that a = b will do atomic loads and stores. Due to the + * IA32 memory model, a simple store guarantees release semantics. + * + * However, loads may pass stores, so for atomic_load_acq we have to + * ensure a Store/Load barrier to do the load in SMP kernels. We use + * "lock cmpxchg" as recommended by the AMD Software Optimization + * Guide, and not mfence. For UP kernels, however, the cache of the + * single processor is always consistent, so we only need to take care + * of the compiler. + */ +#define ATOMIC_STORE(TYPE) \ +static __inline void \ +atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\ +{ \ + __compiler_membar(); \ + *p = v; \ +} \ +struct __hack + +#define ATOMIC_LOAD(TYPE, LOP) \ +static __inline u_##TYPE \ +atomic_load_acq_##TYPE(volatile u_##TYPE *p) \ +{ \ + u_##TYPE res; \ + \ + __asm __volatile(MPLOCKED LOP \ + : "=a" (res), /* 0 */ \ + "+m" (*p) /* 1 */ \ + : : "memory", "cc"); \ + return (res); \ +} \ +struct __hack + +ATOMIC_ASM(set, char, "orb %b1,%0", "iq", v); +ATOMIC_ASM(clear, char, "andb %b1,%0", "iq", ~v); +ATOMIC_ASM(add, char, "addb %b1,%0", "iq", v); +ATOMIC_ASM(subtract, char, "subb %b1,%0", "iq", v); + +ATOMIC_ASM(set, short, "orw %w1,%0", "ir", v); +ATOMIC_ASM(clear, short, "andw %w1,%0", "ir", ~v); +ATOMIC_ASM(add, short, "addw %w1,%0", "ir", v); +ATOMIC_ASM(subtract, short, "subw %w1,%0", "ir", v); + +ATOMIC_ASM(set, int, "orl %1,%0", "ir", v); +ATOMIC_ASM(clear, int, "andl %1,%0", "ir", ~v); +ATOMIC_ASM(add, int, "addl %1,%0", "ir", v); +ATOMIC_ASM(subtract, int, "subl %1,%0", "ir", v); + +ATOMIC_ASM(set, long, "orq %1,%0", "ir", v); +ATOMIC_ASM(clear, long, "andq %1,%0", "ir", ~v); +ATOMIC_ASM(add, long, "addq %1,%0", "ir", v); +ATOMIC_ASM(subtract, long, "subq %1,%0", "ir", v); + +ATOMIC_LOAD(char, "cmpxchgb %b0,%1"); +ATOMIC_LOAD(short, "cmpxchgw %w0,%1"); +ATOMIC_LOAD(int, "cmpxchgl %0,%1"); +ATOMIC_LOAD(long, "cmpxchgq %0,%1"); + +ATOMIC_STORE(char); +ATOMIC_STORE(short); +ATOMIC_STORE(int); +ATOMIC_STORE(long); + +#undef ATOMIC_ASM +#undef ATOMIC_LOAD +#undef ATOMIC_STORE + +/* Read the current value and store a new value in the destination. */ + +static __inline u_int +atomic_swap_int(volatile u_int *p, u_int v) +{ + + __asm __volatile( + " xchgl %1,%0 ; " + "# atomic_swap_int" + : "+r" (v), /* 0 */ + "+m" (*p)); /* 1 */ + return (v); +} + +static __inline u_long +atomic_swap_long(volatile u_long *p, u_long v) +{ + + __asm __volatile( + " xchgq %1,%0 ; " + "# atomic_swap_long" + : "+r" (v), /* 0 */ + "+m" (*p)); /* 1 */ + return (v); +} + +#define atomic_set_acq_char atomic_set_barr_char +#define atomic_set_rel_char atomic_set_barr_char +#define atomic_clear_acq_char atomic_clear_barr_char +#define atomic_clear_rel_char atomic_clear_barr_char +#define atomic_add_acq_char atomic_add_barr_char +#define atomic_add_rel_char atomic_add_barr_char +#define atomic_subtract_acq_char atomic_subtract_barr_char +#define atomic_subtract_rel_char atomic_subtract_barr_char + +#define atomic_set_acq_short atomic_set_barr_short +#define atomic_set_rel_short atomic_set_barr_short +#define atomic_clear_acq_short atomic_clear_barr_short +#define atomic_clear_rel_short atomic_clear_barr_short +#define atomic_add_acq_short atomic_add_barr_short +#define atomic_add_rel_short atomic_add_barr_short +#define atomic_subtract_acq_short atomic_subtract_barr_short +#define atomic_subtract_rel_short atomic_subtract_barr_short + +#define atomic_set_acq_int atomic_set_barr_int +#define atomic_set_rel_int atomic_set_barr_int +#define atomic_clear_acq_int atomic_clear_barr_int +#define atomic_clear_rel_int atomic_clear_barr_int +#define atomic_add_acq_int atomic_add_barr_int +#define atomic_add_rel_int atomic_add_barr_int +#define atomic_subtract_acq_int atomic_subtract_barr_int +#define atomic_subtract_rel_int atomic_subtract_barr_int +#define atomic_cmpset_acq_int atomic_cmpset_int +#define atomic_cmpset_rel_int atomic_cmpset_int + +#define atomic_set_acq_long atomic_set_barr_long +#define atomic_set_rel_long atomic_set_barr_long +#define atomic_clear_acq_long atomic_clear_barr_long +#define atomic_clear_rel_long atomic_clear_barr_long +#define atomic_add_acq_long atomic_add_barr_long +#define atomic_add_rel_long atomic_add_barr_long +#define atomic_subtract_acq_long atomic_subtract_barr_long +#define atomic_subtract_rel_long atomic_subtract_barr_long +#define atomic_cmpset_acq_long atomic_cmpset_long +#define atomic_cmpset_rel_long atomic_cmpset_long + +#define atomic_readandclear_int(p) atomic_swap_int(p, 0) +#define atomic_readandclear_long(p) atomic_swap_long(p, 0) + +/* Operations on 8-bit bytes. */ +#define atomic_set_8 atomic_set_char +#define atomic_set_acq_8 atomic_set_acq_char +#define atomic_set_rel_8 atomic_set_rel_char +#define atomic_clear_8 atomic_clear_char +#define atomic_clear_acq_8 atomic_clear_acq_char +#define atomic_clear_rel_8 atomic_clear_rel_char +#define atomic_add_8 atomic_add_char +#define atomic_add_acq_8 atomic_add_acq_char +#define atomic_add_rel_8 atomic_add_rel_char +#define atomic_subtract_8 atomic_subtract_char +#define atomic_subtract_acq_8 atomic_subtract_acq_char +#define atomic_subtract_rel_8 atomic_subtract_rel_char +#define atomic_load_acq_8 atomic_load_acq_char +#define atomic_store_rel_8 atomic_store_rel_char + +/* Operations on 16-bit words. */ +#define atomic_set_16 atomic_set_short +#define atomic_set_acq_16 atomic_set_acq_short +#define atomic_set_rel_16 atomic_set_rel_short +#define atomic_clear_16 atomic_clear_short +#define atomic_clear_acq_16 atomic_clear_acq_short +#define atomic_clear_rel_16 atomic_clear_rel_short +#define atomic_add_16 atomic_add_short +#define atomic_add_acq_16 atomic_add_acq_short +#define atomic_add_rel_16 atomic_add_rel_short +#define atomic_subtract_16 atomic_subtract_short +#define atomic_subtract_acq_16 atomic_subtract_acq_short +#define atomic_subtract_rel_16 atomic_subtract_rel_short +#define atomic_load_acq_16 atomic_load_acq_short +#define atomic_store_rel_16 atomic_store_rel_short + +/* Operations on 32-bit double words. */ +#define atomic_set_32 atomic_set_int +#define atomic_set_acq_32 atomic_set_acq_int +#define atomic_set_rel_32 atomic_set_rel_int +#define atomic_clear_32 atomic_clear_int +#define atomic_clear_acq_32 atomic_clear_acq_int +#define atomic_clear_rel_32 atomic_clear_rel_int +#define atomic_add_32 atomic_add_int +#define atomic_add_acq_32 atomic_add_acq_int +#define atomic_add_rel_32 atomic_add_rel_int +#define atomic_subtract_32 atomic_subtract_int +#define atomic_subtract_acq_32 atomic_subtract_acq_int +#define atomic_subtract_rel_32 atomic_subtract_rel_int +#define atomic_load_acq_32 atomic_load_acq_int +#define atomic_store_rel_32 atomic_store_rel_int +#define atomic_cmpset_32 atomic_cmpset_int +#define atomic_cmpset_acq_32 atomic_cmpset_acq_int +#define atomic_cmpset_rel_32 atomic_cmpset_rel_int +#define atomic_swap_32 atomic_swap_int +#define atomic_readandclear_32 atomic_readandclear_int +#define atomic_fetchadd_32 atomic_fetchadd_int +#define atomic_testandset_32 atomic_testandset_int + +/* Operations on 64-bit quad words. */ +#define atomic_set_64 atomic_set_long +#define atomic_set_acq_64 atomic_set_acq_long +#define atomic_set_rel_64 atomic_set_rel_long +#define atomic_clear_64 atomic_clear_long +#define atomic_clear_acq_64 atomic_clear_acq_long +#define atomic_clear_rel_64 atomic_clear_rel_long +#define atomic_add_64 atomic_add_long +#define atomic_add_acq_64 atomic_add_acq_long +#define atomic_add_rel_64 atomic_add_rel_long +#define atomic_subtract_64 atomic_subtract_long +#define atomic_subtract_acq_64 atomic_subtract_acq_long +#define atomic_subtract_rel_64 atomic_subtract_rel_long +#define atomic_load_acq_64 atomic_load_acq_long +#define atomic_store_rel_64 atomic_store_rel_long +#define atomic_cmpset_64 atomic_cmpset_long +#define atomic_cmpset_acq_64 atomic_cmpset_acq_long +#define atomic_cmpset_rel_64 atomic_cmpset_rel_long +#define atomic_swap_64 atomic_swap_long +#define atomic_readandclear_64 atomic_readandclear_long +#define atomic_testandset_64 atomic_testandset_long + +/* Operations on pointers. */ +#define atomic_set_ptr atomic_set_long +#define atomic_set_acq_ptr atomic_set_acq_long +#define atomic_set_rel_ptr atomic_set_rel_long +#define atomic_clear_ptr atomic_clear_long +#define atomic_clear_acq_ptr atomic_clear_acq_long +#define atomic_clear_rel_ptr atomic_clear_rel_long +#define atomic_add_ptr atomic_add_long +#define atomic_add_acq_ptr atomic_add_acq_long +#define atomic_add_rel_ptr atomic_add_rel_long +#define atomic_subtract_ptr atomic_subtract_long +#define atomic_subtract_acq_ptr atomic_subtract_acq_long +#define atomic_subtract_rel_ptr atomic_subtract_rel_long +#define atomic_load_acq_ptr atomic_load_acq_long +#define atomic_store_rel_ptr atomic_store_rel_long +#define atomic_cmpset_ptr atomic_cmpset_long +#define atomic_cmpset_acq_ptr atomic_cmpset_acq_long +#define atomic_cmpset_rel_ptr atomic_cmpset_rel_long +#define atomic_swap_ptr atomic_swap_long +#define atomic_readandclear_ptr atomic_readandclear_long diff --git a/include/xhyve/support/bitset.h b/include/xhyve/support/bitset.h new file mode 100644 index 0000000..83de227 --- /dev/null +++ b/include/xhyve/support/bitset.h @@ -0,0 +1,215 @@ +/*- + * Copyright (c) 2008, Jeffrey Roberson + * All rights reserved. + * + * Copyright (c) 2008 Nokia Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#pragma once + +#include +#include + +/* + * Macros addressing word and bit within it, tuned to make compiler + * optimize cases when SETSIZE fits into single machine word. + */ +#define _BITSET_BITS (sizeof(long) * NBBY) + +#define __bitset_words(_s) (howmany(_s, _BITSET_BITS)) + +#define __bitset_mask(_s, n) \ + (1L << ((__bitset_words((_s)) == 1) ? \ + (size_t)(n) : ((n) % _BITSET_BITS))) + +#define __bitset_word(_s, n) \ + ((__bitset_words((_s)) == 1) ? 0 : ((n) / _BITSET_BITS)) + +#define BITSET_DEFINE(t, _s) \ +struct t { \ + long __bits[__bitset_words((_s))]; \ +} + +#define BITSET_T_INITIALIZER(x) \ + { .__bits = { x } } + +#define BITSET_FSET(n) \ + [ 0 ... ((n) - 1) ] = (-1L) + +#define BIT_CLR(_s, n, p) \ + ((p)->__bits[__bitset_word(_s, n)] &= ~__bitset_mask((_s), (n))) + +#define BIT_COPY(_s, f, t) (void)(*(t) = *(f)) + +#define BIT_ISSET(_s, n, p) \ + ((((p)->__bits[__bitset_word(_s, n)] & __bitset_mask((_s), (n))) != 0)) + +#define BIT_SET(_s, n, p) \ + ((p)->__bits[__bitset_word(_s, n)] |= __bitset_mask((_s), (n))) + +#define BIT_ZERO(_s, p) do { \ + size_t __i; \ + for (__i = 0; __i < __bitset_words((_s)); __i++) \ + (p)->__bits[__i] = 0L; \ +} while (0) + +#define BIT_FILL(_s, p) do { \ + size_t __i; \ + for (__i = 0; __i < __bitset_words((_s)); __i++) \ + (p)->__bits[__i] = -1L; \ +} while (0) + +#define BIT_SETOF(_s, n, p) do { \ + BIT_ZERO(_s, p); \ + (p)->__bits[__bitset_word(_s, n)] = __bitset_mask((_s), (n)); \ +} while (0) + +/* Is p empty. */ +#define BIT_EMPTY(_s, p) __extension__ ({ \ + size_t __i; \ + for (__i = 0; __i < __bitset_words((_s)); __i++) \ + if ((p)->__bits[__i]) \ + break; \ + __i == __bitset_words((_s)); \ +}) + +/* Is p full set. */ +#define BIT_ISFULLSET(_s, p) __extension__ ({ \ + size_t __i; \ + for (__i = 0; __i < __bitset_words((_s)); __i++) \ + if ((p)->__bits[__i] != (long)-1) \ + break; \ + __i == __bitset_words((_s)); \ +}) + +/* Is c a subset of p. */ +#define BIT_SUBSET(_s, p, c) __extension__ ({ \ + size_t __i; \ + for (__i = 0; __i < __bitset_words((_s)); __i++) \ + if (((c)->__bits[__i] & \ + (p)->__bits[__i]) != \ + (c)->__bits[__i]) \ + break; \ + __i == __bitset_words((_s)); \ +}) + +/* Are there any common bits between b & c? */ +#define BIT_OVERLAP(_s, p, c) __extension__ ({ \ + size_t __i; \ + for (__i = 0; __i < __bitset_words((_s)); __i++) \ + if (((c)->__bits[__i] & \ + (p)->__bits[__i]) != 0) \ + break; \ + __i != __bitset_words((_s)); \ +}) + +/* Compare two sets, returns 0 if equal 1 otherwise. */ +#define BIT_CMP(_s, p, c) __extension__ ({ \ + size_t __i; \ + for (__i = 0; __i < __bitset_words((_s)); __i++) \ + if (((c)->__bits[__i] != \ + (p)->__bits[__i])) \ + break; \ + __i != __bitset_words((_s)); \ +}) + +#define BIT_OR(_s, d, s) do { \ + size_t __i; \ + for (__i = 0; __i < __bitset_words((_s)); __i++) \ + (d)->__bits[__i] |= (s)->__bits[__i]; \ +} while (0) + +#define BIT_AND(_s, d, s) do { \ + size_t __i; \ + for (__i = 0; __i < __bitset_words((_s)); __i++) \ + (d)->__bits[__i] &= (s)->__bits[__i]; \ +} while (0) + +#define BIT_NAND(_s, d, s) do { \ + size_t __i; \ + for (__i = 0; __i < __bitset_words((_s)); __i++) \ + (d)->__bits[__i] &= ~(s)->__bits[__i]; \ +} while (0) + +#define BIT_CLR_ATOMIC(_s, n, p) \ + atomic_clear_long(((volatile u_long *) \ + &(p)->__bits[__bitset_word(_s, n)]), __bitset_mask((_s), n)) + +#define BIT_SET_ATOMIC(_s, n, p) \ + atomic_set_long(((volatile u_long *) &(p)->__bits[__bitset_word(_s, n)]), \ + __bitset_mask((_s), n)) + +#define BIT_SET_ATOMIC_ACQ(_s, n, p) \ + atomic_set_acq_long(&(p)->__bits[__bitset_word(_s, n)], \ + __bitset_mask((_s), n)) + +/* Convenience functions catering special cases. */ +#define BIT_AND_ATOMIC(_s, d, s) do { \ + size_t __i; \ + for (__i = 0; __i < __bitset_words((_s)); __i++) \ + atomic_clear_long(&(d)->__bits[__i], \ + ~(s)->__bits[__i]); \ +} while (0) + +#define BIT_OR_ATOMIC(_s, d, s) do { \ + size_t __i; \ + for (__i = 0; __i < __bitset_words((_s)); __i++) \ + atomic_set_long(&(d)->__bits[__i], \ + (s)->__bits[__i]); \ +} while (0) + +#define BIT_COPY_STORE_REL(_s, f, t) do { \ + size_t __i; \ + for (__i = 0; __i < __bitset_words((_s)); __i++) \ + atomic_store_rel_long(&(t)->__bits[__i], \ + (f)->__bits[__i]); \ +} while (0) + +#define BIT_FFS(_s, p) __extension__ ({ \ + size_t __i; \ + int __bit; \ + \ + __bit = 0; \ + for (__i = 0; __i < __bitset_words((_s)); __i++) { \ + if ((p)->__bits[__i] != 0) { \ + __bit = ffsl((p)->__bits[__i]); \ + __bit += __i * _BITSET_BITS; \ + break; \ + } \ + } \ + __bit; \ +}) + +#define BIT_COUNT(_s, p) __extension__ ({ \ + size_t __i; \ + int __count; \ + \ + __count = 0; \ + for (__i = 0; __i < __bitset_words((_s)); __i++) \ + __count += __bitcountl((p)->__bits[__i]); \ + __count; \ +}) diff --git a/include/xhyve/support/cpuset.h b/include/xhyve/support/cpuset.h new file mode 100644 index 0000000..46539d3 --- /dev/null +++ b/include/xhyve/support/cpuset.h @@ -0,0 +1,150 @@ +/*- + * Copyright (c) 2008, Jeffrey Roberson + * All rights reserved. + * + * Copyright (c) 2008 Nokia Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#pragma once + +#include + +#define CPU_MAXSIZE 32 + +#ifndef CPU_SETSIZE +#define CPU_SETSIZE CPU_MAXSIZE +#endif + +// #define _NCPUBITS _BITSET_BITS +// #define _NCPUWORDS __bitset_words(CPU_SETSIZE) + +BITSET_DEFINE(_cpuset, CPU_SETSIZE); +typedef struct _cpuset cpuset_t; + +// #define CPUSET_FSET BITSET_FSET(_NCPUWORDS) +// #define CPUSET_T_INITIALIZER BITSET_T_INITIALIZER + +// #define CPUSETBUFSIZ ((2 + sizeof(long) * 2) * _NCPUWORDS) + +#define CPU_CLR(n, p) BIT_CLR(CPU_SETSIZE, n, p) +// #define CPU_COPY(f, t) BIT_COPY(CPU_SETSIZE, f, t) +#define CPU_ISSET(n, p) BIT_ISSET(CPU_SETSIZE, n, p) +#define CPU_SET(n, p) BIT_SET(CPU_SETSIZE, n, p) +#define CPU_ZERO(p) BIT_ZERO(CPU_SETSIZE, p) +// #define CPU_FILL(p) BIT_FILL(CPU_SETSIZE, p) +#define CPU_SETOF(n, p) BIT_SETOF(CPU_SETSIZE, n, p) +#define CPU_EMPTY(p) BIT_EMPTY(CPU_SETSIZE, p) +// #define CPU_ISFULLSET(p) BIT_ISFULLSET(CPU_SETSIZE, p) +// #define CPU_SUBSET(p, c) BIT_SUBSET(CPU_SETSIZE, p, c) +// #define CPU_OVERLAP(p, c) BIT_OVERLAP(CPU_SETSIZE, p, c) +#define CPU_CMP(p, c) BIT_CMP(CPU_SETSIZE, p, c) +// #define CPU_OR(d, s) BIT_OR(CPU_SETSIZE, d, s) +#define CPU_AND(d, s) BIT_AND(CPU_SETSIZE, d, s) +// #define CPU_NAND(d, s) BIT_NAND(CPU_SETSIZE, d, s) +#define CPU_CLR_ATOMIC(n, p) BIT_CLR_ATOMIC(CPU_SETSIZE, n, p) +#define CPU_SET_ATOMIC(n, p) BIT_SET_ATOMIC(CPU_SETSIZE, n, p) +// #define CPU_SET_ATOMIC_ACQ(n, p) BIT_SET_ATOMIC_ACQ(CPU_SETSIZE, n, p) +// #define CPU_AND_ATOMIC(n, p) BIT_AND_ATOMIC(CPU_SETSIZE, n, p) +// #define CPU_OR_ATOMIC(d, s) BIT_OR_ATOMIC(CPU_SETSIZE, d, s) +// #define CPU_COPY_STORE_REL(f, t) BIT_COPY_STORE_REL(CPU_SETSIZE, f, t) +#define CPU_FFS(p) BIT_FFS(CPU_SETSIZE, p) +// #define CPU_COUNT(p) BIT_COUNT(CPU_SETSIZE, p) + +// /* +// * Valid cpulevel_t values. +// */ +// #define CPU_LEVEL_ROOT 1 /* All system cpus. */ +// #define CPU_LEVEL_CPUSET 2 /* Available cpus for which. */ +// #define CPU_LEVEL_WHICH 3 /* Actual mask/id for which. */ + +// /* +// * Valid cpuwhich_t values. +// */ +// #define CPU_WHICH_TID 1 /* Specifies a thread id. */ +// #define CPU_WHICH_PID 2 /* Specifies a process id. */ +// #define CPU_WHICH_CPUSET 3 /* Specifies a set id. */ +// #define CPU_WHICH_IRQ 4 /* Specifies an irq #. */ +// #define CPU_WHICH_JAIL 5 /* Specifies a jail id. */ +// #define CPU_WHICH_DOMAIN 6 /* Specifies a NUMA domain id. */ + +// /* +// * Reserved cpuset identifiers. +// */ +// #define CPUSET_INVALID -1 +// #define CPUSET_DEFAULT 0 + +// #ifdef _KERNEL +// LIST_HEAD(setlist, cpuset); + +// /* +// * cpusets encapsulate cpu binding information for one or more threads. +// * +// * a - Accessed with atomics. +// * s - Set at creation, never modified. Only a ref required to read. +// * c - Locked internally by a cpuset lock. +// * +// * The bitmask is only modified while holding the cpuset lock. It may be +// * read while only a reference is held but the consumer must be prepared +// * to deal with inconsistent results. +// */ +// struct cpuset { +// cpuset_t cs_mask; /* bitmask of valid cpus. */ +// volatile u_int cs_ref; /* (a) Reference count. */ +// int cs_flags; /* (s) Flags from below. */ +// cpusetid_t cs_id; /* (s) Id or INVALID. */ +// struct cpuset *cs_parent; /* (s) Pointer to our parent. */ +// LIST_ENTRY(cpuset) cs_link; /* (c) All identified sets. */ +// LIST_ENTRY(cpuset) cs_siblings; /* (c) Sibling set link. */ +// struct setlist cs_children; /* (c) List of children. */ +// }; + +// #define CPU_SET_ROOT 0x0001 /* Set is a root set. */ +// #define CPU_SET_RDONLY 0x0002 /* No modification allowed. */ + +// extern cpuset_t *cpuset_root; +// struct prison; +// struct proc; + +// struct cpuset *cpuset_thread0(void); +// struct cpuset *cpuset_ref(struct cpuset *); +// void cpuset_rel(struct cpuset *); +// int cpuset_setthread(lwpid_t id, cpuset_t *); +// int cpuset_setithread(lwpid_t id, int cpu); +// int cpuset_create_root(struct prison *, struct cpuset **); +// int cpuset_setproc_update_set(struct proc *, struct cpuset *); +// char *cpusetobj_strprint(char *, const cpuset_t *); +// int cpusetobj_strscan(cpuset_t *, const char *); + +// #else +// __BEGIN_DECLS +// int cpuset(cpusetid_t *); +// int cpuset_setid(cpuwhich_t, id_t, cpusetid_t); +// int cpuset_getid(cpulevel_t, cpuwhich_t, id_t, cpusetid_t *); +// int cpuset_getaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, cpuset_t *); +// int cpuset_setaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, const cpuset_t *); +// __END_DECLS +// #endif diff --git a/include/xhyve/support/i8253reg.h b/include/xhyve/support/i8253reg.h new file mode 100644 index 0000000..2934e95 --- /dev/null +++ b/include/xhyve/support/i8253reg.h @@ -0,0 +1,81 @@ +/*- + * Copyright (c) 1993 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * from: Header: timerreg.h,v 1.2 93/02/28 15:08:58 mccanne Exp + * $FreeBSD$ + */ + +/* + * Register definitions for the Intel 8253 Programmable Interval Timer. + * + * This chip has three independent 16-bit down counters that can be + * read on the fly. There are three mode registers and three countdown + * registers. The countdown registers are addressed directly, via the + * first three I/O ports. The three mode registers are accessed via + * the fourth I/O port, with two bits in the mode byte indicating the + * register. (Why are hardware interfaces always so braindead?). + * + * To write a value into the countdown register, the mode register + * is first programmed with a command indicating the which byte of + * the two byte register is to be modified. The three possibilities + * are load msb (TMR_MR_MSB), load lsb (TMR_MR_LSB), or load lsb then + * msb (TMR_MR_BOTH). + * + * To read the current value ("on the fly") from the countdown register, + * you write a "latch" command into the mode register, then read the stable + * value from the corresponding I/O port. For example, you write + * TMR_MR_LATCH into the corresponding mode register. Presumably, + * after doing this, a write operation to the I/O port would result + * in undefined behavior (but hopefully not fry the chip). + * Reading in this manner has no side effects. + */ + +/* + * Macros for specifying values to be written into a mode register. + */ + +#pragma once + +#define TIMER_REG_CNTR0 0 /* timer 0 counter port */ +#define TIMER_REG_CNTR1 1 /* timer 1 counter port */ +#define TIMER_REG_CNTR2 2 /* timer 2 counter port */ +#define TIMER_REG_MODE 3 /* timer mode port */ +#define TIMER_SEL0 0x00 /* select counter 0 */ +#define TIMER_SEL1 0x40 /* select counter 1 */ +#define TIMER_SEL2 0x80 /* select counter 2 */ +#define TIMER_INTTC 0x00 /* mode 0, intr on terminal cnt */ +#define TIMER_ONESHOT 0x02 /* mode 1, one shot */ +#define TIMER_RATEGEN 0x04 /* mode 2, rate generator */ +#define TIMER_SQWAVE 0x06 /* mode 3, square wave */ +#define TIMER_SWSTROBE 0x08 /* mode 4, s/w triggered strobe */ +#define TIMER_HWSTROBE 0x0a /* mode 5, h/w triggered strobe */ +#define TIMER_LATCH 0x00 /* latch counter for reading */ +#define TIMER_LSB 0x10 /* r/w counter LSB */ +#define TIMER_MSB 0x20 /* r/w counter MSB */ +#define TIMER_16BIT 0x30 /* r/w counter 16 bits, LSB first */ +#define TIMER_BCD 0x01 /* count in BCD */ diff --git a/include/xhyve/support/i8259.h b/include/xhyve/support/i8259.h new file mode 100644 index 0000000..ac1e498 --- /dev/null +++ b/include/xhyve/support/i8259.h @@ -0,0 +1,83 @@ +/*- + * Copyright (c) 2003 Peter Wemm + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* + * Register defintions for the i8259A programmable interrupt controller. + */ + +#pragma once + +/* Initialization control word 1. Written to even address. */ +#define ICW1_IC4 0x01 /* ICW4 present */ +#define ICW1_SNGL 0x02 /* 1 = single, 0 = cascaded */ +#define ICW1_ADI 0x04 /* 1 = 4, 0 = 8 byte vectors */ +#define ICW1_LTIM 0x08 /* 1 = level trigger, 0 = edge */ +#define ICW1_RESET 0x10 /* must be 1 */ +/* 0x20 - 0x80 - in 8080/8085 mode only */ + +/* Initialization control word 2. Written to the odd address. */ +/* No definitions, it is the base vector of the IDT for 8086 mode */ + +/* Initialization control word 3. Written to the odd address. */ +/* For a master PIC, bitfield indicating a slave 8259 on given input */ +/* For slave, lower 3 bits are the slave's ID binary id on master */ + +/* Initialization control word 4. Written to the odd address. */ +#define ICW4_8086 0x01 /* 1 = 8086, 0 = 8080 */ +#define ICW4_AEOI 0x02 /* 1 = Auto EOI */ +#define ICW4_MS 0x04 /* 1 = buffered master, 0 = slave */ +#define ICW4_BUF 0x08 /* 1 = enable buffer mode */ +#define ICW4_SFNM 0x10 /* 1 = special fully nested mode */ + +/* Operation control words. Written after initialization. */ + +/* Operation control word type 1 */ +/* + * No definitions. Written to the odd address. Bitmask for interrupts. + * 1 = disabled. + */ + +/* Operation control word type 2. Bit 3 (0x08) must be zero. Even address. */ +#define OCW2_L0 0x01 /* Level */ +#define OCW2_L1 0x02 +#define OCW2_L2 0x04 +/* 0x08 must be 0 to select OCW2 vs OCW3 */ +/* 0x10 must be 0 to select OCW2 vs ICW1 */ +#define OCW2_EOI 0x20 /* 1 = EOI */ +#define OCW2_SL 0x40 /* EOI mode */ +#define OCW2_R 0x80 /* EOI mode */ + +/* Operation control word type 3. Bit 3 (0x08) must be set. Even address. */ +#define OCW3_RIS 0x01 /* 1 = read IS, 0 = read IR */ +#define OCW3_RR 0x02 /* register read */ +#define OCW3_P 0x04 /* poll mode command */ +/* 0x08 must be 1 to select OCW3 vs OCW2 */ +#define OCW3_SEL 0x08 /* must be 1 */ +/* 0x10 must be 0 to select OCW3 vs ICW1 */ +#define OCW3_SMM 0x20 /* special mode mask */ +#define OCW3_ESMM 0x40 /* enable SMM */ diff --git a/include/xhyve/support/linker_set.h b/include/xhyve/support/linker_set.h new file mode 100644 index 0000000..657435d --- /dev/null +++ b/include/xhyve/support/linker_set.h @@ -0,0 +1,96 @@ +/*- + * Copyright (c) 1999 John D. Polstra + * Copyright (c) 1999,2001 Peter Wemm + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* xhyve: sort of working linker sets for MachO */ + +#pragma once + +#define __GLOBL1(sym) __asm__(".globl " #sym) +#define __GLOBL(sym) __GLOBL1(sym) +#define __section(x) __attribute__((__section__(x))) + +/* + * The following macros are used to declare global sets of objects, which + * are collected by the linker into a `linker_set' as defined below. + * For ELF, this is done by constructing a separate segment for each set. + */ + +#define __MAKE_SET_CONST const + +/* + * Private macros, not to be used outside this header file. + */ +#define __MAKE_SET(set, sym) \ + __GLOBL(__CONCAT(__start_set_,set)); \ + __GLOBL(__CONCAT(__stop_set_,set)); \ + static void const * __MAKE_SET_CONST \ + __set_##set##_sym_##sym __section("__"#set",__set") \ + __used = &(sym) + +/* + * Public macros. + */ +#define TEXT_SET(set, sym) __MAKE_SET(set, sym) +#define DATA_SET(set, sym) __MAKE_SET(set, sym) +#define BSS_SET(set, sym) __MAKE_SET(set, sym) +#define ABS_SET(set, sym) __MAKE_SET(set, sym) +#define SET_ENTRY(set, sym) __MAKE_SET(set, sym) + +/* + * Initialize before referring to a given linker set. + */ +#define SET_DECLARE(set, ptype) \ + extern ptype __weak *__CONCAT(__start_set_,set) \ + __asm("segment$start$__"#set); \ + extern ptype __weak *__CONCAT(__stop_set_,set) \ + __asm("segment$end$__"#set) + +#define SET_BEGIN(set) \ + (&__CONCAT(__start_set_,set)) +#define SET_LIMIT(set) \ + (&__CONCAT(__stop_set_,set)) + +/* + * Iterate over all the elements of a set. + * + * Sets always contain addresses of things, and "pvar" points to words + * containing those addresses. Thus is must be declared as "type **pvar", + * and the address of each set item is obtained inside the loop by "*pvar". + */ +#define SET_FOREACH(pvar, set) \ + for (pvar = SET_BEGIN(set); pvar < SET_LIMIT(set); pvar++) + +#define SET_ITEM(set, i) \ + ((SET_BEGIN(set))[i]) + +/* + * Provide a count of the items in a set. + */ +#define SET_COUNT(set) \ + (SET_LIMIT(set) - SET_BEGIN(set)) diff --git a/include/xhyve/support/md5.h b/include/xhyve/support/md5.h new file mode 100644 index 0000000..4825027 --- /dev/null +++ b/include/xhyve/support/md5.h @@ -0,0 +1,52 @@ +/* MD5.H - header file for MD5C.C + * $FreeBSD$ + */ + +/*- + Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All +rights reserved. + +License to copy and use this software is granted provided that it +is identified as the "RSA Data Security, Inc. MD5 Message-Digest +Algorithm" in all material mentioning or referencing this software +or this function. + +License is also granted to make and use derivative works provided +that such works are identified as "derived from the RSA Data +Security, Inc. MD5 Message-Digest Algorithm" in all material +mentioning or referencing the derived work. + +RSA Data Security, Inc. makes no representations concerning either +the merchantability of this software or the suitability of this +software for any particular purpose. It is provided "as is" +without express or implied warranty of any kind. + +These notices must be retained in any copies of any part of this +documentation and/or software. + */ + +#pragma once + +#include + +#define MD5_BLOCK_LENGTH 64 +#define MD5_DIGEST_LENGTH 16 +#define MD5_DIGEST_STRING_LENGTH (MD5_DIGEST_LENGTH * 2 + 1) + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +/* MD5 context. */ +typedef struct MD5Context { + u_int32_t state[4]; /* state (ABCD) */ + u_int32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */ + unsigned char buffer[64]; /* input buffer */ +} MD5_CTX; +#pragma clang diagnostic pop + +void MD5Init(MD5_CTX *); +void MD5Update(MD5_CTX *, const void *, unsigned int); +void MD5Final(unsigned char [16], MD5_CTX *); +char * MD5End(MD5_CTX *, char *); +char * MD5File(const char *, char *); +char * MD5FileChunk(const char *, char *, off_t, off_t); +char * MD5Data(const void *, unsigned int, char *); diff --git a/include/xhyve/support/misc.h b/include/xhyve/support/misc.h new file mode 100644 index 0000000..d3497b3 --- /dev/null +++ b/include/xhyve/support/misc.h @@ -0,0 +1,64 @@ +#pragma once + +#include +#include +#include + +#define UNUSED __attribute__ ((unused)) +#define CTASSERT(x) _Static_assert ((x), "CTASSERT") +#define XHYVE_PAGE_SIZE 0x1000 +#define XHYVE_PAGE_MASK (XHYVE_PAGE_SIZE - 1) +#define XHYVE_PAGE_SHIFT 12 +#define __aligned(x) __attribute__ ((aligned ((x)))) +#define __packed __attribute__ ((packed)) +#define nitems(x) (sizeof((x)) / sizeof((x)[0])) +#define powerof2(x) ((((x)-1)&(x))==0) +#define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */ +#define nitems(x) (sizeof((x)) / sizeof((x)[0])) +#define min(x, y) (((x) < (y)) ? (x) : (y)) + +#define xhyve_abort(...) \ + do { \ + fprintf(stderr, __VA_ARGS__); \ + abort(); \ + } while (0) + +#define xhyve_warn(...) \ + do { \ + fprintf(stderr, __VA_ARGS__); \ + } while (0) + +#ifdef XHYVE_CONFIG_ASSERT +#define KASSERT(exp, msg) if (!(exp)) xhyve_abort msg +#define KWARN(exp, msg) if (!(exp)) xhyve_warn msg +#else +#define KASSERT(exp, msg) if (0) xhyve_abort msg +#define KWARN(exp, msg) if (0) xhyve_warn msg +#endif + +#define FALSE 0 +#define TRUE 1 + +#define XHYVE_PROT_READ 1 +#define XHYVE_PROT_WRITE 2 +#define XHYVE_PROT_EXECUTE 4 + +#define VM_SUCCESS 0 + +/* sys/sys/types.h */ +typedef unsigned char u_char; +typedef unsigned short u_short; +typedef unsigned int u_int; +typedef unsigned long u_long; + +static inline void cpuid_count(uint32_t ax, uint32_t cx, uint32_t *p) { + __asm__ __volatile__ ("cpuid" + : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3]) + : "0" (ax), "c" (cx)); +} + +static inline void do_cpuid(unsigned ax, unsigned *p) { + __asm__ __volatile__ ("cpuid" + : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3]) + : "0" (ax)); +} diff --git a/include/xhyve/support/mptable.h b/include/xhyve/support/mptable.h new file mode 100644 index 0000000..f8ff7f5 --- /dev/null +++ b/include/xhyve/support/mptable.h @@ -0,0 +1,194 @@ +/*- + * Copyright (c) 1996, by Steve Passe + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. The name of the developer may NOT be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#pragma once + +#include +#include + +enum busTypes { + NOBUS = 0, + CBUS = 1, + CBUSII = 2, + EISA = 3, + ISA = 6, + MCA = 9, + PCI = 13, + XPRESS = 18, + MAX_BUSTYPE = 18, + UNKNOWN_BUSTYPE = 0xff +}; + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpacked" + +/* MP Floating Pointer Structure */ +typedef struct MPFPS { + uint8_t signature[4]; + uint32_t pap; + uint8_t length; + uint8_t spec_rev; + uint8_t checksum; + uint8_t config_type; + uint8_t mpfb2; + uint8_t mpfb3; + uint8_t mpfb4; + uint8_t mpfb5; +} __packed *mpfps_t; + +#define MPFB2_IMCR_PRESENT 0x80 +#define MPFB2_MUL_CLK_SRCS 0x40 + +/* MP Configuration Table Header */ +typedef struct MPCTH { + uint8_t signature[4]; + uint16_t base_table_length; + uint8_t spec_rev; + uint8_t checksum; + uint8_t oem_id[8]; + uint8_t product_id[12]; + uint32_t oem_table_pointer; + uint16_t oem_table_size; + uint16_t entry_count; + uint32_t apic_address; + uint16_t extended_table_length; + uint8_t extended_table_checksum; + uint8_t reserved; +} __packed *mpcth_t; + +/* Base table entries */ + +#define MPCT_ENTRY_PROCESSOR 0 +#define MPCT_ENTRY_BUS 1 +#define MPCT_ENTRY_IOAPIC 2 +#define MPCT_ENTRY_INT 3 +#define MPCT_ENTRY_LOCAL_INT 4 + +typedef struct PROCENTRY { + uint8_t type; + uint8_t apic_id; + uint8_t apic_version; + uint8_t cpu_flags; + uint32_t cpu_signature; + uint32_t feature_flags; + uint32_t reserved1; + uint32_t reserved2; +} __packed *proc_entry_ptr; + +#define PROCENTRY_FLAG_EN 0x01 +#define PROCENTRY_FLAG_BP 0x02 + +typedef struct BUSENTRY { + uint8_t type; + uint8_t bus_id; + uint8_t bus_type[6]; +} __packed *bus_entry_ptr; + +typedef struct IOAPICENTRY { + uint8_t type; + uint8_t apic_id; + uint8_t apic_version; + uint8_t apic_flags; + uint32_t apic_address; +} __packed *io_apic_entry_ptr; + +#define IOAPICENTRY_FLAG_EN 0x01 + +typedef struct INTENTRY { + uint8_t type; + uint8_t int_type; + uint16_t int_flags; + uint8_t src_bus_id; + uint8_t src_bus_irq; + uint8_t dst_apic_id; + uint8_t dst_apic_int; +} __packed *int_entry_ptr; + +#define INTENTRY_TYPE_INT 0 +#define INTENTRY_TYPE_NMI 1 +#define INTENTRY_TYPE_SMI 2 +#define INTENTRY_TYPE_EXTINT 3 + +#define INTENTRY_FLAGS_POLARITY 0x3 +#define INTENTRY_FLAGS_POLARITY_CONFORM 0x0 +#define INTENTRY_FLAGS_POLARITY_ACTIVEHI 0x1 +#define INTENTRY_FLAGS_POLARITY_ACTIVELO 0x3 +#define INTENTRY_FLAGS_TRIGGER 0xc +#define INTENTRY_FLAGS_TRIGGER_CONFORM 0x0 +#define INTENTRY_FLAGS_TRIGGER_EDGE 0x4 +#define INTENTRY_FLAGS_TRIGGER_LEVEL 0xc + +/* Extended table entries */ + +typedef struct EXTENTRY { + uint8_t type; + uint8_t length; +} __packed *ext_entry_ptr; + +#define MPCT_EXTENTRY_SAS 0x80 +#define MPCT_EXTENTRY_BHD 0x81 +#define MPCT_EXTENTRY_CBASM 0x82 + +typedef struct SASENTRY { + uint8_t type; + uint8_t length; + uint8_t bus_id; + uint8_t address_type; + uint64_t address_base; + uint64_t address_length; +} __packed *sas_entry_ptr; + +#define SASENTRY_TYPE_IO 0 +#define SASENTRY_TYPE_MEMORY 1 +#define SASENTRY_TYPE_PREFETCH 2 + +typedef struct BHDENTRY { + uint8_t type; + uint8_t length; + uint8_t bus_id; + uint8_t bus_info; + uint8_t parent_bus; + uint8_t reserved[3]; +} __packed *bhd_entry_ptr; + +#define BHDENTRY_INFO_SUBTRACTIVE_DECODE 0x1 + +typedef struct CBASMENTRY { + uint8_t type; + uint8_t length; + uint8_t bus_id; + uint8_t address_mod; + uint32_t predefined_range; +} __packed *cbasm_entry_ptr; + +#define CBASMENTRY_ADDRESS_MOD_ADD 0x0 +#define CBASMENTRY_ADDRESS_MOD_SUBTRACT 0x1 + +#define CBASMENTRY_RANGE_ISA_IO 0 +#define CBASMENTRY_RANGE_VGA_IO 1 + +#pragma clang diagnostic pop diff --git a/include/xhyve/support/ns16550.h b/include/xhyve/support/ns16550.h new file mode 100644 index 0000000..2a023d0 --- /dev/null +++ b/include/xhyve/support/ns16550.h @@ -0,0 +1,242 @@ +/*- + * Copyright (c) 1991 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * from: @(#)ns16550.h 7.1 (Berkeley) 5/9/91 + * $FreeBSD$ + */ + +/* + * NS8250... UART registers. + */ + +/* 8250 registers #[0-6]. */ + +#pragma once + +#define com_data 0 /* data register (R/W) */ +#define REG_DATA com_data + +#define com_ier 1 /* interrupt enable register (W) */ +#define REG_IER com_ier +#define IER_ERXRDY 0x1 +#define IER_ETXRDY 0x2 +#define IER_ERLS 0x4 +#define IER_EMSC 0x8 + +#define IER_BITS "\20\1ERXRDY\2ETXRDY\3ERLS\4EMSC" + +#define com_iir 2 /* interrupt identification register (R) */ +#define REG_IIR com_iir +#define IIR_IMASK 0xf +#define IIR_RXTOUT 0xc +#define IIR_BUSY 0x7 +#define IIR_RLS 0x6 +#define IIR_RXRDY 0x4 +#define IIR_TXRDY 0x2 +#define IIR_NOPEND 0x1 +#define IIR_MLSC 0x0 +#define IIR_FIFO_MASK 0xc0 /* set if FIFOs are enabled */ + +#define IIR_BITS "\20\1NOPEND\2TXRDY\3RXRDY" + +#define com_lcr 3 /* line control register (R/W) */ +#define com_cfcr com_lcr /* character format control register (R/W) */ +#define REG_LCR com_lcr +#define LCR_DLAB 0x80 +#define CFCR_DLAB LCR_DLAB +#define LCR_EFR_ENABLE 0xbf /* magic to enable EFR on 16650 up */ +#define CFCR_EFR_ENABLE LCR_EFR_ENABLE +#define LCR_SBREAK 0x40 +#define CFCR_SBREAK LCR_SBREAK +#define LCR_PZERO 0x30 +#define CFCR_PZERO LCR_PZERO +#define LCR_PONE 0x20 +#define CFCR_PONE LCR_PONE +#define LCR_PEVEN 0x10 +#define CFCR_PEVEN LCR_PEVEN +#define LCR_PODD 0x00 +#define CFCR_PODD LCR_PODD +#define LCR_PENAB 0x08 +#define CFCR_PENAB LCR_PENAB +#define LCR_STOPB 0x04 +#define CFCR_STOPB LCR_STOPB +#define LCR_8BITS 0x03 +#define CFCR_8BITS LCR_8BITS +#define LCR_7BITS 0x02 +#define CFCR_7BITS LCR_7BITS +#define LCR_6BITS 0x01 +#define CFCR_6BITS LCR_6BITS +#define LCR_5BITS 0x00 +#define CFCR_5BITS LCR_5BITS + +#define com_mcr 4 /* modem control register (R/W) */ +#define REG_MCR com_mcr +#define MCR_PRESCALE 0x80 /* only available on 16650 up */ +#define MCR_LOOPBACK 0x10 +#define MCR_IE 0x08 +#define MCR_IENABLE MCR_IE +#define MCR_DRS 0x04 +#define MCR_RTS 0x02 +#define MCR_DTR 0x01 + +#define MCR_BITS "\20\1DTR\2RTS\3DRS\4IE\5LOOPBACK\10PRESCALE" + +#define com_lsr 5 /* line status register (R/W) */ +#define REG_LSR com_lsr +#define LSR_RCV_FIFO 0x80 +#define LSR_TEMT 0x40 +#define LSR_TSRE LSR_TEMT +#define LSR_THRE 0x20 +#define LSR_TXRDY LSR_THRE +#define LSR_BI 0x10 +#define LSR_FE 0x08 +#define LSR_PE 0x04 +#define LSR_OE 0x02 +#define LSR_RXRDY 0x01 +#define LSR_RCV_MASK 0x1f + +#define LSR_BITS "\20\1RXRDY\2OE\3PE\4FE\5BI\6THRE\7TEMT\10RCV_FIFO" + +#define com_msr 6 /* modem status register (R/W) */ +#define REG_MSR com_msr +#define MSR_DCD 0x80 +#define MSR_RI 0x40 +#define MSR_DSR 0x20 +#define MSR_CTS 0x10 +#define MSR_DDCD 0x08 +#define MSR_TERI 0x04 +#define MSR_DDSR 0x02 +#define MSR_DCTS 0x01 + +#define MSR_BITS "\20\1DCTS\2DDSR\3TERI\4DDCD\5CTS\6DSR\7RI\10DCD" + +/* 8250 multiplexed registers #[0-1]. Access enabled by LCR[7]. */ +#define com_dll 0 /* divisor latch low (R/W) */ +#define com_dlbl com_dll +#define com_dlm 1 /* divisor latch high (R/W) */ +#define com_dlbh com_dlm +#define REG_DLL com_dll +#define REG_DLH com_dlm + +/* 16450 register #7. Not multiplexed. */ +#define com_scr 7 /* scratch register (R/W) */ + +/* 16550 register #2. Not multiplexed. */ +#define com_fcr 2 /* FIFO control register (W) */ +#define com_fifo com_fcr +#define REG_FCR com_fcr +#define FCR_ENABLE 0x01 +#define FIFO_ENABLE FCR_ENABLE +#define FCR_RCV_RST 0x02 +#define FIFO_RCV_RST FCR_RCV_RST +#define FCR_XMT_RST 0x04 +#define FIFO_XMT_RST FCR_XMT_RST +#define FCR_DMA 0x08 +#define FIFO_DMA_MODE FCR_DMA +#define FCR_RX_LOW 0x00 +#define FIFO_RX_LOW FCR_RX_LOW +#define FCR_RX_MEDL 0x40 +#define FIFO_RX_MEDL FCR_RX_MEDL +#define FCR_RX_MEDH 0x80 +#define FIFO_RX_MEDH FCR_RX_MEDH +#define FCR_RX_HIGH 0xc0 +#define FIFO_RX_HIGH FCR_RX_HIGH + +#define FCR_BITS "\20\1ENABLE\2RCV_RST\3XMT_RST\4DMA" + +/* 16650 registers #2,[4-7]. Access enabled by LCR_EFR_ENABLE. */ + +#define com_efr 2 /* enhanced features register (R/W) */ +#define REG_EFR com_efr +#define EFR_CTS 0x80 +#define EFR_AUTOCTS EFR_CTS +#define EFR_RTS 0x40 +#define EFR_AUTORTS EFR_RTS +#define EFR_EFE 0x10 /* enhanced functions enable */ + +#define com_xon1 4 /* XON 1 character (R/W) */ +#define com_xon2 5 /* XON 2 character (R/W) */ +#define com_xoff1 6 /* XOFF 1 character (R/W) */ +#define com_xoff2 7 /* XOFF 2 character (R/W) */ + +#define DW_REG_USR 31 /* DesignWare derived Uart Status Reg */ +#define com_usr 39 /* Octeon 16750/16550 Uart Status Reg */ +#define REG_USR com_usr +#define USR_BUSY 1 /* Uart Busy. Serial transfer in progress */ +#define USR_TXFIFO_NOTFULL 2 /* Uart TX FIFO Not full */ + +/* 16950 register #1. Access enabled by ACR[7]. Also requires !LCR[7]. */ +#define com_asr 1 /* additional status register (R[0-7]/W[0-1]) */ + +/* 16950 register #3. R/W access enabled by ACR[7]. */ +#define com_rfl 3 /* receiver fifo level (R) */ + +/* + * 16950 register #4. Access enabled by ACR[7]. Also requires + * !LCR_EFR_ENABLE. + */ +#define com_tfl 4 /* transmitter fifo level (R) */ + +/* + * 16950 register #5. Accessible if !LCR_EFR_ENABLE. Read access also + * requires ACR[6]. + */ +#define com_icr 5 /* index control register (R/W) */ + +/* + * 16950 register #7. It is the same as com_scr except it has a different + * abbreviation in the manufacturer's data sheet and it also serves as an + * index into the Indexed Control register set. + */ +#define com_spr com_scr /* scratch pad (and index) register (R/W) */ +#define REG_SPR com_scr + +/* + * 16950 indexed control registers #[0-0x13]. Access is via index in SPR, + * data in ICR (if ICR is accessible). + */ + +#define com_acr 0 /* additional control register (R/W) */ +#define ACR_ASE 0x80 /* ASR/RFL/TFL enable */ +#define ACR_ICRE 0x40 /* ICR enable */ +#define ACR_TLE 0x20 /* TTL/RTL enable */ + +#define com_cpr 1 /* clock prescaler register (R/W) */ +#define com_tcr 2 /* times clock register (R/W) */ +#define com_ttl 4 /* transmitter trigger level (R/W) */ +#define com_rtl 5 /* receiver trigger level (R/W) */ +/* ... */ + +/* Hardware extension mode register for RSB-2000/3000. */ +#define com_emr com_msr +#define EMR_EXBUFF 0x04 +#define EMR_CTSFLW 0x08 +#define EMR_DSRFLW 0x10 +#define EMR_RTSFLW 0x20 +#define EMR_DTRFLW 0x40 +#define EMR_EFMODE 0x80 diff --git a/include/xhyve/support/pcireg.h b/include/xhyve/support/pcireg.h new file mode 100644 index 0000000..720a497 --- /dev/null +++ b/include/xhyve/support/pcireg.h @@ -0,0 +1,945 @@ +/*- + * Copyright (c) 1997, Stefan Esser + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + * + */ + +#pragma once + +/* + * PCIM_xxx: mask to locate subfield in register + * PCIR_xxx: config register offset + * PCIC_xxx: device class + * PCIS_xxx: device subclass + * PCIP_xxx: device programming interface + * PCIV_xxx: PCI vendor ID (only required to fixup ancient devices) + * PCID_xxx: device ID + * PCIY_xxx: capability identification number + * PCIZ_xxx: extended capability identification number + */ + +/* some PCI bus constants */ +#define PCI_DOMAINMAX 65535 /* highest supported domain number */ +#define PCI_BUSMAX 255 /* highest supported bus number */ +#define PCI_SLOTMAX 31 /* highest supported slot number */ +#define PCI_FUNCMAX 7 /* highest supported function number */ +#define PCI_REGMAX 255 /* highest supported config register addr. */ +#define PCIE_REGMAX 4095 /* highest supported config register addr. */ +#define PCI_MAXHDRTYPE 2 + +#define PCIE_ARI_SLOTMAX 0 +#define PCIE_ARI_FUNCMAX 255 + +#define PCI_RID_BUS_SHIFT 8 +#define PCI_RID_SLOT_SHIFT 3 +#define PCI_RID_FUNC_SHIFT 0 + +#define PCI_RID(bus, slot, func) \ + ((((bus) & PCI_BUSMAX) << PCI_RID_BUS_SHIFT) | \ + (((slot) & PCI_SLOTMAX) << PCI_RID_SLOT_SHIFT) | \ + (((func) & PCI_FUNCMAX) << PCI_RID_FUNC_SHIFT)) + +#define PCI_ARI_RID(bus, func) \ + ((((bus) & PCI_BUSMAX) << PCI_RID_BUS_SHIFT) | \ + (((func) & PCIE_ARI_FUNCMAX) << PCI_RID_FUNC_SHIFT)) + +#define PCI_RID2BUS(rid) (((rid) >> PCI_RID_BUS_SHIFT) & PCI_BUSMAX) +#define PCI_RID2SLOT(rid) (((rid) >> PCI_RID_SLOT_SHIFT) & PCI_SLOTMAX) +#define PCI_RID2FUNC(rid) (((rid) >> PCI_RID_FUNC_SHIFT) & PCI_FUNCMAX) + +#define PCIE_ARI_RID2SLOT(rid) (0) +#define PCIE_ARI_RID2FUNC(rid) \ + (((rid) >> PCI_RID_FUNC_SHIFT) & PCIE_ARI_FUNCMAX) + +#define PCIE_ARI_SLOT(func) (((func) >> PCI_RID_SLOT_SHIFT) & PCI_SLOTMAX) +#define PCIE_ARI_FUNC(func) (((func) >> PCI_RID_FUNC_SHIFT) & PCI_FUNCMAX) + +/* PCI config header registers for all devices */ + +#define PCIR_DEVVENDOR 0x00 +#define PCIR_VENDOR 0x00 +#define PCIR_DEVICE 0x02 +#define PCIR_COMMAND 0x04 +#define PCIM_CMD_PORTEN 0x0001 +#define PCIM_CMD_MEMEN 0x0002 +#define PCIM_CMD_BUSMASTEREN 0x0004 +#define PCIM_CMD_SPECIALEN 0x0008 +#define PCIM_CMD_MWRICEN 0x0010 +#define PCIM_CMD_PERRESPEN 0x0040 +#define PCIM_CMD_SERRESPEN 0x0100 +#define PCIM_CMD_BACKTOBACK 0x0200 +#define PCIM_CMD_INTxDIS 0x0400 +#define PCIR_STATUS 0x06 +#define PCIM_STATUS_INTxSTATE 0x0008 +#define PCIM_STATUS_CAPPRESENT 0x0010 +#define PCIM_STATUS_66CAPABLE 0x0020 +#define PCIM_STATUS_BACKTOBACK 0x0080 +#define PCIM_STATUS_MDPERR 0x0100 +#define PCIM_STATUS_SEL_FAST 0x0000 +#define PCIM_STATUS_SEL_MEDIMUM 0x0200 +#define PCIM_STATUS_SEL_SLOW 0x0400 +#define PCIM_STATUS_SEL_MASK 0x0600 +#define PCIM_STATUS_STABORT 0x0800 +#define PCIM_STATUS_RTABORT 0x1000 +#define PCIM_STATUS_RMABORT 0x2000 +#define PCIM_STATUS_SERR 0x4000 +#define PCIM_STATUS_PERR 0x8000 +#define PCIR_REVID 0x08 +#define PCIR_PROGIF 0x09 +#define PCIR_SUBCLASS 0x0a +#define PCIR_CLASS 0x0b +#define PCIR_CACHELNSZ 0x0c +#define PCIR_LATTIMER 0x0d +#define PCIR_HDRTYPE 0x0e +#define PCIM_HDRTYPE 0x7f +#define PCIM_HDRTYPE_NORMAL 0x00 +#define PCIM_HDRTYPE_BRIDGE 0x01 +#define PCIM_HDRTYPE_CARDBUS 0x02 +#define PCIM_MFDEV 0x80 +#define PCIR_BIST 0x0f + +/* Capability Register Offsets */ + +#define PCICAP_ID 0x0 +#define PCICAP_NEXTPTR 0x1 + +/* Capability Identification Numbers */ + +#define PCIY_PMG 0x01 /* PCI Power Management */ +#define PCIY_AGP 0x02 /* AGP */ +#define PCIY_VPD 0x03 /* Vital Product Data */ +#define PCIY_SLOTID 0x04 /* Slot Identification */ +#define PCIY_MSI 0x05 /* Message Signaled Interrupts */ +#define PCIY_CHSWP 0x06 /* CompactPCI Hot Swap */ +#define PCIY_PCIX 0x07 /* PCI-X */ +#define PCIY_HT 0x08 /* HyperTransport */ +#define PCIY_VENDOR 0x09 /* Vendor Unique */ +#define PCIY_DEBUG 0x0a /* Debug port */ +#define PCIY_CRES 0x0b /* CompactPCI central resource control */ +#define PCIY_HOTPLUG 0x0c /* PCI Hot-Plug */ +#define PCIY_SUBVENDOR 0x0d /* PCI-PCI bridge subvendor ID */ +#define PCIY_AGP8X 0x0e /* AGP 8x */ +#define PCIY_SECDEV 0x0f /* Secure Device */ +#define PCIY_EXPRESS 0x10 /* PCI Express */ +#define PCIY_MSIX 0x11 /* MSI-X */ +#define PCIY_SATA 0x12 /* SATA */ +#define PCIY_PCIAF 0x13 /* PCI Advanced Features */ + +/* Extended Capability Register Fields */ + +#define PCIR_EXTCAP 0x100 +#define PCIM_EXTCAP_ID 0x0000ffff +#define PCIM_EXTCAP_VER 0x000f0000 +#define PCIM_EXTCAP_NEXTPTR 0xfff00000 +#define PCI_EXTCAP_ID(ecap) ((ecap) & PCIM_EXTCAP_ID) +#define PCI_EXTCAP_VER(ecap) (((ecap) & PCIM_EXTCAP_VER) >> 16) +#define PCI_EXTCAP_NEXTPTR(ecap) (((ecap) & PCIM_EXTCAP_NEXTPTR) >> 20) + +/* Extended Capability Identification Numbers */ + +#define PCIZ_AER 0x0001 /* Advanced Error Reporting */ +#define PCIZ_VC 0x0002 /* Virtual Channel if MFVC Ext Cap not set */ +#define PCIZ_SERNUM 0x0003 /* Device Serial Number */ +#define PCIZ_PWRBDGT 0x0004 /* Power Budgeting */ +#define PCIZ_RCLINK_DCL 0x0005 /* Root Complex Link Declaration */ +#define PCIZ_RCLINK_CTL 0x0006 /* Root Complex Internal Link Control */ +#define PCIZ_RCEC_ASSOC 0x0007 /* Root Complex Event Collector Association */ +#define PCIZ_MFVC 0x0008 /* Multi-Function Virtual Channel */ +#define PCIZ_VC2 0x0009 /* Virtual Channel if MFVC Ext Cap set */ +#define PCIZ_RCRB 0x000a /* RCRB Header */ +#define PCIZ_VENDOR 0x000b /* Vendor Unique */ +#define PCIZ_CAC 0x000c /* Configuration Access Correction -- obsolete */ +#define PCIZ_ACS 0x000d /* Access Control Services */ +#define PCIZ_ARI 0x000e /* Alternative Routing-ID Interpretation */ +#define PCIZ_ATS 0x000f /* Address Translation Services */ +#define PCIZ_SRIOV 0x0010 /* Single Root IO Virtualization */ +#define PCIZ_MRIOV 0x0011 /* Multiple Root IO Virtualization */ +#define PCIZ_MULTICAST 0x0012 /* Multicast */ +#define PCIZ_PAGE_REQ 0x0013 /* Page Request */ +#define PCIZ_AMD 0x0014 /* Reserved for AMD */ +#define PCIZ_RESIZE_BAR 0x0015 /* Resizable BAR */ +#define PCIZ_DPA 0x0016 /* Dynamic Power Allocation */ +#define PCIZ_TPH_REQ 0x0017 /* TPH Requester */ +#define PCIZ_LTR 0x0018 /* Latency Tolerance Reporting */ +#define PCIZ_SEC_PCIE 0x0019 /* Secondary PCI Express */ +#define PCIZ_PMUX 0x001a /* Protocol Multiplexing */ +#define PCIZ_PASID 0x001b /* Process Address Space ID */ +#define PCIZ_LN_REQ 0x001c /* LN Requester */ +#define PCIZ_DPC 0x001d /* Downstream Porto Containment */ +#define PCIZ_L1PM 0x001e /* L1 PM Substates */ + +/* config registers for header type 0 devices */ + +#define PCIR_BARS 0x10 +#define PCIR_BAR(x) (PCIR_BARS + (x) * 4) +#define PCIR_MAX_BAR_0 5 +#define PCI_RID2BAR(rid) (((rid) - PCIR_BARS) / 4) +#define PCI_BAR_IO(x) (((x) & PCIM_BAR_SPACE) == PCIM_BAR_IO_SPACE) +#define PCI_BAR_MEM(x) (((x) & PCIM_BAR_SPACE) == PCIM_BAR_MEM_SPACE) +#define PCIM_BAR_SPACE 0x00000001 +#define PCIM_BAR_MEM_SPACE 0 +#define PCIM_BAR_IO_SPACE 1 +#define PCIM_BAR_MEM_TYPE 0x00000006 +#define PCIM_BAR_MEM_32 0 +#define PCIM_BAR_MEM_1MB 2 /* Locate below 1MB in PCI <= 2.1 */ +#define PCIM_BAR_MEM_64 4 +#define PCIM_BAR_MEM_PREFETCH 0x00000008 +#define PCIM_BAR_MEM_BASE 0xfffffffffffffff0ULL +#define PCIM_BAR_IO_RESERVED 0x00000002 +#define PCIM_BAR_IO_BASE 0xfffffffc +#define PCIR_CIS 0x28 +#define PCIM_CIS_ASI_MASK 0x00000007 +#define PCIM_CIS_ASI_CONFIG 0 +#define PCIM_CIS_ASI_BAR0 1 +#define PCIM_CIS_ASI_BAR1 2 +#define PCIM_CIS_ASI_BAR2 3 +#define PCIM_CIS_ASI_BAR3 4 +#define PCIM_CIS_ASI_BAR4 5 +#define PCIM_CIS_ASI_BAR5 6 +#define PCIM_CIS_ASI_ROM 7 +#define PCIM_CIS_ADDR_MASK 0x0ffffff8 +#define PCIM_CIS_ROM_MASK 0xf0000000 +#define PCIM_CIS_CONFIG_MASK 0xff +#define PCIR_SUBVEND_0 0x2c +#define PCIR_SUBDEV_0 0x2e +#define PCIR_BIOS 0x30 +#define PCIM_BIOS_ENABLE 0x01 +#define PCIM_BIOS_ADDR_MASK 0xfffff800 +#define PCIR_CAP_PTR 0x34 +#define PCIR_INTLINE 0x3c +#define PCIR_INTPIN 0x3d +#define PCIR_MINGNT 0x3e +#define PCIR_MAXLAT 0x3f + +/* config registers for header type 1 (PCI-to-PCI bridge) devices */ + +#define PCIR_MAX_BAR_1 1 +#define PCIR_SECSTAT_1 0x1e + +#define PCIR_PRIBUS_1 0x18 +#define PCIR_SECBUS_1 0x19 +#define PCIR_SUBBUS_1 0x1a +#define PCIR_SECLAT_1 0x1b + +#define PCIR_IOBASEL_1 0x1c +#define PCIR_IOLIMITL_1 0x1d +#define PCIR_IOBASEH_1 0x30 +#define PCIR_IOLIMITH_1 0x32 +#define PCIM_BRIO_16 0x0 +#define PCIM_BRIO_32 0x1 +#define PCIM_BRIO_MASK 0xf + +#define PCIR_MEMBASE_1 0x20 +#define PCIR_MEMLIMIT_1 0x22 + +#define PCIR_PMBASEL_1 0x24 +#define PCIR_PMLIMITL_1 0x26 +#define PCIR_PMBASEH_1 0x28 +#define PCIR_PMLIMITH_1 0x2c +#define PCIM_BRPM_32 0x0 +#define PCIM_BRPM_64 0x1 +#define PCIM_BRPM_MASK 0xf + +#define PCIR_BIOS_1 0x38 +#define PCIR_BRIDGECTL_1 0x3e + +/* config registers for header type 2 (CardBus) devices */ + +#define PCIR_MAX_BAR_2 0 +#define PCIR_CAP_PTR_2 0x14 +#define PCIR_SECSTAT_2 0x16 + +#define PCIR_PRIBUS_2 0x18 +#define PCIR_SECBUS_2 0x19 +#define PCIR_SUBBUS_2 0x1a +#define PCIR_SECLAT_2 0x1b + +#define PCIR_MEMBASE0_2 0x1c +#define PCIR_MEMLIMIT0_2 0x20 +#define PCIR_MEMBASE1_2 0x24 +#define PCIR_MEMLIMIT1_2 0x28 +#define PCIR_IOBASE0_2 0x2c +#define PCIR_IOLIMIT0_2 0x30 +#define PCIR_IOBASE1_2 0x34 +#define PCIR_IOLIMIT1_2 0x38 + +#define PCIR_BRIDGECTL_2 0x3e + +#define PCIR_SUBVEND_2 0x40 +#define PCIR_SUBDEV_2 0x42 + +#define PCIR_PCCARDIF_2 0x44 + +/* PCI device class, subclass and programming interface definitions */ + +#define PCIC_OLD 0x00 +#define PCIS_OLD_NONVGA 0x00 +#define PCIS_OLD_VGA 0x01 + +#define PCIC_STORAGE 0x01 +#define PCIS_STORAGE_SCSI 0x00 +#define PCIS_STORAGE_IDE 0x01 +#define PCIP_STORAGE_IDE_MODEPRIM 0x01 +#define PCIP_STORAGE_IDE_PROGINDPRIM 0x02 +#define PCIP_STORAGE_IDE_MODESEC 0x04 +#define PCIP_STORAGE_IDE_PROGINDSEC 0x08 +#define PCIP_STORAGE_IDE_MASTERDEV 0x80 +#define PCIS_STORAGE_FLOPPY 0x02 +#define PCIS_STORAGE_IPI 0x03 +#define PCIS_STORAGE_RAID 0x04 +#define PCIS_STORAGE_ATA_ADMA 0x05 +#define PCIS_STORAGE_SATA 0x06 +#define PCIP_STORAGE_SATA_AHCI_1_0 0x01 +#define PCIS_STORAGE_SAS 0x07 +#define PCIS_STORAGE_NVM 0x08 +#define PCIP_STORAGE_NVM_NVMHCI_1_0 0x01 +#define PCIP_STORAGE_NVM_ENTERPRISE_NVMHCI_1_0 0x02 +#define PCIS_STORAGE_OTHER 0x80 + +#define PCIC_NETWORK 0x02 +#define PCIS_NETWORK_ETHERNET 0x00 +#define PCIS_NETWORK_TOKENRING 0x01 +#define PCIS_NETWORK_FDDI 0x02 +#define PCIS_NETWORK_ATM 0x03 +#define PCIS_NETWORK_ISDN 0x04 +#define PCIS_NETWORK_WORLDFIP 0x05 +#define PCIS_NETWORK_PICMG 0x06 +#define PCIS_NETWORK_OTHER 0x80 + +#define PCIC_DISPLAY 0x03 +#define PCIS_DISPLAY_VGA 0x00 +#define PCIS_DISPLAY_XGA 0x01 +#define PCIS_DISPLAY_3D 0x02 +#define PCIS_DISPLAY_OTHER 0x80 + +#define PCIC_MULTIMEDIA 0x04 +#define PCIS_MULTIMEDIA_VIDEO 0x00 +#define PCIS_MULTIMEDIA_AUDIO 0x01 +#define PCIS_MULTIMEDIA_TELE 0x02 +#define PCIS_MULTIMEDIA_HDA 0x03 +#define PCIS_MULTIMEDIA_OTHER 0x80 + +#define PCIC_MEMORY 0x05 +#define PCIS_MEMORY_RAM 0x00 +#define PCIS_MEMORY_FLASH 0x01 +#define PCIS_MEMORY_OTHER 0x80 + +#define PCIC_BRIDGE 0x06 +#define PCIS_BRIDGE_HOST 0x00 +#define PCIS_BRIDGE_ISA 0x01 +#define PCIS_BRIDGE_EISA 0x02 +#define PCIS_BRIDGE_MCA 0x03 +#define PCIS_BRIDGE_PCI 0x04 +#define PCIP_BRIDGE_PCI_SUBTRACTIVE 0x01 +#define PCIS_BRIDGE_PCMCIA 0x05 +#define PCIS_BRIDGE_NUBUS 0x06 +#define PCIS_BRIDGE_CARDBUS 0x07 +#define PCIS_BRIDGE_RACEWAY 0x08 +#define PCIS_BRIDGE_PCI_TRANSPARENT 0x09 +#define PCIS_BRIDGE_INFINIBAND 0x0a +#define PCIS_BRIDGE_OTHER 0x80 + +#define PCIC_SIMPLECOMM 0x07 +#define PCIS_SIMPLECOMM_UART 0x00 +#define PCIP_SIMPLECOMM_UART_8250 0x00 +#define PCIP_SIMPLECOMM_UART_16450A 0x01 +#define PCIP_SIMPLECOMM_UART_16550A 0x02 +#define PCIP_SIMPLECOMM_UART_16650A 0x03 +#define PCIP_SIMPLECOMM_UART_16750A 0x04 +#define PCIP_SIMPLECOMM_UART_16850A 0x05 +#define PCIP_SIMPLECOMM_UART_16950A 0x06 +#define PCIS_SIMPLECOMM_PAR 0x01 +#define PCIS_SIMPLECOMM_MULSER 0x02 +#define PCIS_SIMPLECOMM_MODEM 0x03 +#define PCIS_SIMPLECOMM_GPIB 0x04 +#define PCIS_SIMPLECOMM_SMART_CARD 0x05 +#define PCIS_SIMPLECOMM_OTHER 0x80 + +#define PCIC_BASEPERIPH 0x08 +#define PCIS_BASEPERIPH_PIC 0x00 +#define PCIP_BASEPERIPH_PIC_8259A 0x00 +#define PCIP_BASEPERIPH_PIC_ISA 0x01 +#define PCIP_BASEPERIPH_PIC_EISA 0x02 +#define PCIP_BASEPERIPH_PIC_IO_APIC 0x10 +#define PCIP_BASEPERIPH_PIC_IOX_APIC 0x20 +#define PCIS_BASEPERIPH_DMA 0x01 +#define PCIS_BASEPERIPH_TIMER 0x02 +#define PCIS_BASEPERIPH_RTC 0x03 +#define PCIS_BASEPERIPH_PCIHOT 0x04 +#define PCIS_BASEPERIPH_SDHC 0x05 +#define PCIS_BASEPERIPH_IOMMU 0x06 +#define PCIS_BASEPERIPH_OTHER 0x80 + +#define PCIC_INPUTDEV 0x09 +#define PCIS_INPUTDEV_KEYBOARD 0x00 +#define PCIS_INPUTDEV_DIGITIZER 0x01 +#define PCIS_INPUTDEV_MOUSE 0x02 +#define PCIS_INPUTDEV_SCANNER 0x03 +#define PCIS_INPUTDEV_GAMEPORT 0x04 +#define PCIS_INPUTDEV_OTHER 0x80 + +#define PCIC_DOCKING 0x0a +#define PCIS_DOCKING_GENERIC 0x00 +#define PCIS_DOCKING_OTHER 0x80 + +#define PCIC_PROCESSOR 0x0b +#define PCIS_PROCESSOR_386 0x00 +#define PCIS_PROCESSOR_486 0x01 +#define PCIS_PROCESSOR_PENTIUM 0x02 +#define PCIS_PROCESSOR_ALPHA 0x10 +#define PCIS_PROCESSOR_POWERPC 0x20 +#define PCIS_PROCESSOR_MIPS 0x30 +#define PCIS_PROCESSOR_COPROC 0x40 + +#define PCIC_SERIALBUS 0x0c +#define PCIS_SERIALBUS_FW 0x00 +#define PCIS_SERIALBUS_ACCESS 0x01 +#define PCIS_SERIALBUS_SSA 0x02 +#define PCIS_SERIALBUS_USB 0x03 +#define PCIP_SERIALBUS_USB_UHCI 0x00 +#define PCIP_SERIALBUS_USB_OHCI 0x10 +#define PCIP_SERIALBUS_USB_EHCI 0x20 +#define PCIP_SERIALBUS_USB_XHCI 0x30 +#define PCIP_SERIALBUS_USB_DEVICE 0xfe +#define PCIS_SERIALBUS_FC 0x04 +#define PCIS_SERIALBUS_SMBUS 0x05 +#define PCIS_SERIALBUS_INFINIBAND 0x06 +#define PCIS_SERIALBUS_IPMI 0x07 +#define PCIP_SERIALBUS_IPMI_SMIC 0x00 +#define PCIP_SERIALBUS_IPMI_KCS 0x01 +#define PCIP_SERIALBUS_IPMI_BT 0x02 +#define PCIS_SERIALBUS_SERCOS 0x08 +#define PCIS_SERIALBUS_CANBUS 0x09 + +#define PCIC_WIRELESS 0x0d +#define PCIS_WIRELESS_IRDA 0x00 +#define PCIS_WIRELESS_IR 0x01 +#define PCIS_WIRELESS_RF 0x10 +#define PCIS_WIRELESS_BLUETOOTH 0x11 +#define PCIS_WIRELESS_BROADBAND 0x12 +#define PCIS_WIRELESS_80211A 0x20 +#define PCIS_WIRELESS_80211B 0x21 +#define PCIS_WIRELESS_OTHER 0x80 + +#define PCIC_INTELLIIO 0x0e +#define PCIS_INTELLIIO_I2O 0x00 + +#define PCIC_SATCOM 0x0f +#define PCIS_SATCOM_TV 0x01 +#define PCIS_SATCOM_AUDIO 0x02 +#define PCIS_SATCOM_VOICE 0x03 +#define PCIS_SATCOM_DATA 0x04 + +#define PCIC_CRYPTO 0x10 +#define PCIS_CRYPTO_NETCOMP 0x00 +#define PCIS_CRYPTO_ENTERTAIN 0x10 +#define PCIS_CRYPTO_OTHER 0x80 + +#define PCIC_DASP 0x11 +#define PCIS_DASP_DPIO 0x00 +#define PCIS_DASP_PERFCNTRS 0x01 +#define PCIS_DASP_COMM_SYNC 0x10 +#define PCIS_DASP_MGMT_CARD 0x20 +#define PCIS_DASP_OTHER 0x80 + +#define PCIC_OTHER 0xff + +/* Bridge Control Values. */ +#define PCIB_BCR_PERR_ENABLE 0x0001 +#define PCIB_BCR_SERR_ENABLE 0x0002 +#define PCIB_BCR_ISA_ENABLE 0x0004 +#define PCIB_BCR_VGA_ENABLE 0x0008 +#define PCIB_BCR_MASTER_ABORT_MODE 0x0020 +#define PCIB_BCR_SECBUS_RESET 0x0040 +#define PCIB_BCR_SECBUS_BACKTOBACK 0x0080 +#define PCIB_BCR_PRI_DISCARD_TIMEOUT 0x0100 +#define PCIB_BCR_SEC_DISCARD_TIMEOUT 0x0200 +#define PCIB_BCR_DISCARD_TIMER_STATUS 0x0400 +#define PCIB_BCR_DISCARD_TIMER_SERREN 0x0800 + +/* PCI power manangement */ +#define PCIR_POWER_CAP 0x2 +#define PCIM_PCAP_SPEC 0x0007 +#define PCIM_PCAP_PMEREQCLK 0x0008 +#define PCIM_PCAP_DEVSPECINIT 0x0020 +#define PCIM_PCAP_AUXPWR_0 0x0000 +#define PCIM_PCAP_AUXPWR_55 0x0040 +#define PCIM_PCAP_AUXPWR_100 0x0080 +#define PCIM_PCAP_AUXPWR_160 0x00c0 +#define PCIM_PCAP_AUXPWR_220 0x0100 +#define PCIM_PCAP_AUXPWR_270 0x0140 +#define PCIM_PCAP_AUXPWR_320 0x0180 +#define PCIM_PCAP_AUXPWR_375 0x01c0 +#define PCIM_PCAP_AUXPWRMASK 0x01c0 +#define PCIM_PCAP_D1SUPP 0x0200 +#define PCIM_PCAP_D2SUPP 0x0400 +#define PCIM_PCAP_D0PME 0x0800 +#define PCIM_PCAP_D1PME 0x1000 +#define PCIM_PCAP_D2PME 0x2000 +#define PCIM_PCAP_D3PME_HOT 0x4000 +#define PCIM_PCAP_D3PME_COLD 0x8000 + +#define PCIR_POWER_STATUS 0x4 +#define PCIM_PSTAT_D0 0x0000 +#define PCIM_PSTAT_D1 0x0001 +#define PCIM_PSTAT_D2 0x0002 +#define PCIM_PSTAT_D3 0x0003 +#define PCIM_PSTAT_DMASK 0x0003 +#define PCIM_PSTAT_NOSOFTRESET 0x0008 +#define PCIM_PSTAT_PMEENABLE 0x0100 +#define PCIM_PSTAT_D0POWER 0x0000 +#define PCIM_PSTAT_D1POWER 0x0200 +#define PCIM_PSTAT_D2POWER 0x0400 +#define PCIM_PSTAT_D3POWER 0x0600 +#define PCIM_PSTAT_D0HEAT 0x0800 +#define PCIM_PSTAT_D1HEAT 0x0a00 +#define PCIM_PSTAT_D2HEAT 0x0c00 +#define PCIM_PSTAT_D3HEAT 0x0e00 +#define PCIM_PSTAT_DATASELMASK 0x1e00 +#define PCIM_PSTAT_DATAUNKN 0x0000 +#define PCIM_PSTAT_DATADIV10 0x2000 +#define PCIM_PSTAT_DATADIV100 0x4000 +#define PCIM_PSTAT_DATADIV1000 0x6000 +#define PCIM_PSTAT_DATADIVMASK 0x6000 +#define PCIM_PSTAT_PME 0x8000 + +#define PCIR_POWER_BSE 0x6 +#define PCIM_PMCSR_BSE_D3B3 0x00 +#define PCIM_PMCSR_BSE_D3B2 0x40 +#define PCIM_PMCSR_BSE_BPCCE 0x80 + +#define PCIR_POWER_DATA 0x7 + +/* VPD capability registers */ +#define PCIR_VPD_ADDR 0x2 +#define PCIR_VPD_DATA 0x4 + +/* PCI Message Signalled Interrupts (MSI) */ +#define PCIR_MSI_CTRL 0x2 +#define PCIM_MSICTRL_VECTOR 0x0100 +#define PCIM_MSICTRL_64BIT 0x0080 +#define PCIM_MSICTRL_MME_MASK 0x0070 +#define PCIM_MSICTRL_MME_1 0x0000 +#define PCIM_MSICTRL_MME_2 0x0010 +#define PCIM_MSICTRL_MME_4 0x0020 +#define PCIM_MSICTRL_MME_8 0x0030 +#define PCIM_MSICTRL_MME_16 0x0040 +#define PCIM_MSICTRL_MME_32 0x0050 +#define PCIM_MSICTRL_MMC_MASK 0x000E +#define PCIM_MSICTRL_MMC_1 0x0000 +#define PCIM_MSICTRL_MMC_2 0x0002 +#define PCIM_MSICTRL_MMC_4 0x0004 +#define PCIM_MSICTRL_MMC_8 0x0006 +#define PCIM_MSICTRL_MMC_16 0x0008 +#define PCIM_MSICTRL_MMC_32 0x000A +#define PCIM_MSICTRL_MSI_ENABLE 0x0001 +#define PCIR_MSI_ADDR 0x4 +#define PCIR_MSI_ADDR_HIGH 0x8 +#define PCIR_MSI_DATA 0x8 +#define PCIR_MSI_DATA_64BIT 0xc +#define PCIR_MSI_MASK 0x10 +#define PCIR_MSI_PENDING 0x14 + +/* PCI-X definitions */ + +/* For header type 0 devices */ +#define PCIXR_COMMAND 0x2 +#define PCIXM_COMMAND_DPERR_E 0x0001 /* Data Parity Error Recovery */ +#define PCIXM_COMMAND_ERO 0x0002 /* Enable Relaxed Ordering */ +#define PCIXM_COMMAND_MAX_READ 0x000c /* Maximum Burst Read Count */ +#define PCIXM_COMMAND_MAX_READ_512 0x0000 +#define PCIXM_COMMAND_MAX_READ_1024 0x0004 +#define PCIXM_COMMAND_MAX_READ_2048 0x0008 +#define PCIXM_COMMAND_MAX_READ_4096 0x000c +#define PCIXM_COMMAND_MAX_SPLITS 0x0070 /* Maximum Split Transactions */ +#define PCIXM_COMMAND_MAX_SPLITS_1 0x0000 +#define PCIXM_COMMAND_MAX_SPLITS_2 0x0010 +#define PCIXM_COMMAND_MAX_SPLITS_3 0x0020 +#define PCIXM_COMMAND_MAX_SPLITS_4 0x0030 +#define PCIXM_COMMAND_MAX_SPLITS_8 0x0040 +#define PCIXM_COMMAND_MAX_SPLITS_12 0x0050 +#define PCIXM_COMMAND_MAX_SPLITS_16 0x0060 +#define PCIXM_COMMAND_MAX_SPLITS_32 0x0070 +#define PCIXM_COMMAND_VERSION 0x3000 +#define PCIXR_STATUS 0x4 +#define PCIXM_STATUS_DEVFN 0x000000FF +#define PCIXM_STATUS_BUS 0x0000FF00 +#define PCIXM_STATUS_64BIT 0x00010000 +#define PCIXM_STATUS_133CAP 0x00020000 +#define PCIXM_STATUS_SC_DISCARDED 0x00040000 +#define PCIXM_STATUS_UNEXP_SC 0x00080000 +#define PCIXM_STATUS_COMPLEX_DEV 0x00100000 +#define PCIXM_STATUS_MAX_READ 0x00600000 +#define PCIXM_STATUS_MAX_READ_512 0x00000000 +#define PCIXM_STATUS_MAX_READ_1024 0x00200000 +#define PCIXM_STATUS_MAX_READ_2048 0x00400000 +#define PCIXM_STATUS_MAX_READ_4096 0x00600000 +#define PCIXM_STATUS_MAX_SPLITS 0x03800000 +#define PCIXM_STATUS_MAX_SPLITS_1 0x00000000 +#define PCIXM_STATUS_MAX_SPLITS_2 0x00800000 +#define PCIXM_STATUS_MAX_SPLITS_3 0x01000000 +#define PCIXM_STATUS_MAX_SPLITS_4 0x01800000 +#define PCIXM_STATUS_MAX_SPLITS_8 0x02000000 +#define PCIXM_STATUS_MAX_SPLITS_12 0x02800000 +#define PCIXM_STATUS_MAX_SPLITS_16 0x03000000 +#define PCIXM_STATUS_MAX_SPLITS_32 0x03800000 +#define PCIXM_STATUS_MAX_CUM_READ 0x1C000000 +#define PCIXM_STATUS_RCVD_SC_ERR 0x20000000 +#define PCIXM_STATUS_266CAP 0x40000000 +#define PCIXM_STATUS_533CAP 0x80000000 + +/* For header type 1 devices (PCI-X bridges) */ +#define PCIXR_SEC_STATUS 0x2 +#define PCIXM_SEC_STATUS_64BIT 0x0001 +#define PCIXM_SEC_STATUS_133CAP 0x0002 +#define PCIXM_SEC_STATUS_SC_DISC 0x0004 +#define PCIXM_SEC_STATUS_UNEXP_SC 0x0008 +#define PCIXM_SEC_STATUS_SC_OVERRUN 0x0010 +#define PCIXM_SEC_STATUS_SR_DELAYED 0x0020 +#define PCIXM_SEC_STATUS_BUS_MODE 0x03c0 +#define PCIXM_SEC_STATUS_VERSION 0x3000 +#define PCIXM_SEC_STATUS_266CAP 0x4000 +#define PCIXM_SEC_STATUS_533CAP 0x8000 +#define PCIXR_BRIDGE_STATUS 0x4 +#define PCIXM_BRIDGE_STATUS_DEVFN 0x000000FF +#define PCIXM_BRIDGE_STATUS_BUS 0x0000FF00 +#define PCIXM_BRIDGE_STATUS_64BIT 0x00010000 +#define PCIXM_BRIDGE_STATUS_133CAP 0x00020000 +#define PCIXM_BRIDGE_STATUS_SC_DISCARDED 0x00040000 +#define PCIXM_BRIDGE_STATUS_UNEXP_SC 0x00080000 +#define PCIXM_BRIDGE_STATUS_SC_OVERRUN 0x00100000 +#define PCIXM_BRIDGE_STATUS_SR_DELAYED 0x00200000 +#define PCIXM_BRIDGE_STATUS_DEVID_MSGCAP 0x20000000 +#define PCIXM_BRIDGE_STATUS_266CAP 0x40000000 +#define PCIXM_BRIDGE_STATUS_533CAP 0x80000000 + +/* HT (HyperTransport) Capability definitions */ +#define PCIR_HT_COMMAND 0x2 +#define PCIM_HTCMD_CAP_MASK 0xf800 /* Capability type. */ +#define PCIM_HTCAP_SLAVE 0x0000 /* 000xx */ +#define PCIM_HTCAP_HOST 0x2000 /* 001xx */ +#define PCIM_HTCAP_SWITCH 0x4000 /* 01000 */ +#define PCIM_HTCAP_INTERRUPT 0x8000 /* 10000 */ +#define PCIM_HTCAP_REVISION_ID 0x8800 /* 10001 */ +#define PCIM_HTCAP_UNITID_CLUMPING 0x9000 /* 10010 */ +#define PCIM_HTCAP_EXT_CONFIG_SPACE 0x9800 /* 10011 */ +#define PCIM_HTCAP_ADDRESS_MAPPING 0xa000 /* 10100 */ +#define PCIM_HTCAP_MSI_MAPPING 0xa800 /* 10101 */ +#define PCIM_HTCAP_DIRECT_ROUTE 0xb000 /* 10110 */ +#define PCIM_HTCAP_VCSET 0xb800 /* 10111 */ +#define PCIM_HTCAP_RETRY_MODE 0xc000 /* 11000 */ +#define PCIM_HTCAP_X86_ENCODING 0xc800 /* 11001 */ +#define PCIM_HTCAP_GEN3 0xd000 /* 11010 */ +#define PCIM_HTCAP_FLE 0xd800 /* 11011 */ +#define PCIM_HTCAP_PM 0xe000 /* 11100 */ +#define PCIM_HTCAP_HIGH_NODE_COUNT 0xe800 /* 11101 */ + +/* HT MSI Mapping Capability definitions. */ +#define PCIM_HTCMD_MSI_ENABLE 0x0001 +#define PCIM_HTCMD_MSI_FIXED 0x0002 +#define PCIR_HTMSI_ADDRESS_LO 0x4 +#define PCIR_HTMSI_ADDRESS_HI 0x8 + +/* PCI Vendor capability definitions */ +#define PCIR_VENDOR_LENGTH 0x2 +#define PCIR_VENDOR_DATA 0x3 + +/* PCI EHCI Debug Port definitions */ +#define PCIR_DEBUG_PORT 0x2 +#define PCIM_DEBUG_PORT_OFFSET 0x1FFF +#define PCIM_DEBUG_PORT_BAR 0xe000 + +/* PCI-PCI Bridge Subvendor definitions */ +#define PCIR_SUBVENDCAP_ID 0x4 + +/* PCI Express definitions */ +#define PCIER_FLAGS 0x2 +#define PCIEM_FLAGS_VERSION 0x000F +#define PCIEM_FLAGS_TYPE 0x00F0 +#define PCIEM_TYPE_ENDPOINT 0x0000 +#define PCIEM_TYPE_LEGACY_ENDPOINT 0x0010 +#define PCIEM_TYPE_ROOT_PORT 0x0040 +#define PCIEM_TYPE_UPSTREAM_PORT 0x0050 +#define PCIEM_TYPE_DOWNSTREAM_PORT 0x0060 +#define PCIEM_TYPE_PCI_BRIDGE 0x0070 +#define PCIEM_TYPE_PCIE_BRIDGE 0x0080 +#define PCIEM_TYPE_ROOT_INT_EP 0x0090 +#define PCIEM_TYPE_ROOT_EC 0x00a0 +#define PCIEM_FLAGS_SLOT 0x0100 +#define PCIEM_FLAGS_IRQ 0x3e00 +#define PCIER_DEVICE_CAP 0x4 +#define PCIEM_CAP_MAX_PAYLOAD 0x00000007 +#define PCIEM_CAP_PHANTHOM_FUNCS 0x00000018 +#define PCIEM_CAP_EXT_TAG_FIELD 0x00000020 +#define PCIEM_CAP_L0S_LATENCY 0x000001c0 +#define PCIEM_CAP_L1_LATENCY 0x00000e00 +#define PCIEM_CAP_ROLE_ERR_RPT 0x00008000 +#define PCIEM_CAP_SLOT_PWR_LIM_VAL 0x03fc0000 +#define PCIEM_CAP_SLOT_PWR_LIM_SCALE 0x0c000000 +#define PCIEM_CAP_FLR 0x10000000 +#define PCIER_DEVICE_CTL 0x8 +#define PCIEM_CTL_COR_ENABLE 0x0001 +#define PCIEM_CTL_NFER_ENABLE 0x0002 +#define PCIEM_CTL_FER_ENABLE 0x0004 +#define PCIEM_CTL_URR_ENABLE 0x0008 +#define PCIEM_CTL_RELAXED_ORD_ENABLE 0x0010 +#define PCIEM_CTL_MAX_PAYLOAD 0x00e0 +#define PCIEM_CTL_EXT_TAG_FIELD 0x0100 +#define PCIEM_CTL_PHANTHOM_FUNCS 0x0200 +#define PCIEM_CTL_AUX_POWER_PM 0x0400 +#define PCIEM_CTL_NOSNOOP_ENABLE 0x0800 +#define PCIEM_CTL_MAX_READ_REQUEST 0x7000 +#define PCIEM_CTL_BRDG_CFG_RETRY 0x8000 /* PCI-E - PCI/PCI-X bridges */ +#define PCIEM_CTL_INITIATE_FLR 0x8000 /* FLR capable endpoints */ +#define PCIER_DEVICE_STA 0xa +#define PCIEM_STA_CORRECTABLE_ERROR 0x0001 +#define PCIEM_STA_NON_FATAL_ERROR 0x0002 +#define PCIEM_STA_FATAL_ERROR 0x0004 +#define PCIEM_STA_UNSUPPORTED_REQ 0x0008 +#define PCIEM_STA_AUX_POWER 0x0010 +#define PCIEM_STA_TRANSACTION_PND 0x0020 +#define PCIER_LINK_CAP 0xc +#define PCIEM_LINK_CAP_MAX_SPEED 0x0000000f +#define PCIEM_LINK_CAP_MAX_WIDTH 0x000003f0 +#define PCIEM_LINK_CAP_ASPM 0x00000c00 +#define PCIEM_LINK_CAP_L0S_EXIT 0x00007000 +#define PCIEM_LINK_CAP_L1_EXIT 0x00038000 +#define PCIEM_LINK_CAP_CLOCK_PM 0x00040000 +#define PCIEM_LINK_CAP_SURPRISE_DOWN 0x00080000 +#define PCIEM_LINK_CAP_DL_ACTIVE 0x00100000 +#define PCIEM_LINK_CAP_LINK_BW_NOTIFY 0x00200000 +#define PCIEM_LINK_CAP_ASPM_COMPLIANCE 0x00400000 +#define PCIEM_LINK_CAP_PORT 0xff000000 +#define PCIER_LINK_CTL 0x10 +#define PCIEM_LINK_CTL_ASPMC_DIS 0x0000 +#define PCIEM_LINK_CTL_ASPMC_L0S 0x0001 +#define PCIEM_LINK_CTL_ASPMC_L1 0x0002 +#define PCIEM_LINK_CTL_ASPMC 0x0003 +#define PCIEM_LINK_CTL_RCB 0x0008 +#define PCIEM_LINK_CTL_LINK_DIS 0x0010 +#define PCIEM_LINK_CTL_RETRAIN_LINK 0x0020 +#define PCIEM_LINK_CTL_COMMON_CLOCK 0x0040 +#define PCIEM_LINK_CTL_EXTENDED_SYNC 0x0080 +#define PCIEM_LINK_CTL_ECPM 0x0100 +#define PCIEM_LINK_CTL_HAWD 0x0200 +#define PCIEM_LINK_CTL_LBMIE 0x0400 +#define PCIEM_LINK_CTL_LABIE 0x0800 +#define PCIER_LINK_STA 0x12 +#define PCIEM_LINK_STA_SPEED 0x000f +#define PCIEM_LINK_STA_WIDTH 0x03f0 +#define PCIEM_LINK_STA_TRAINING_ERROR 0x0400 +#define PCIEM_LINK_STA_TRAINING 0x0800 +#define PCIEM_LINK_STA_SLOT_CLOCK 0x1000 +#define PCIEM_LINK_STA_DL_ACTIVE 0x2000 +#define PCIEM_LINK_STA_LINK_BW_MGMT 0x4000 +#define PCIEM_LINK_STA_LINK_AUTO_BW 0x8000 +#define PCIER_SLOT_CAP 0x14 +#define PCIEM_SLOT_CAP_APB 0x00000001 +#define PCIEM_SLOT_CAP_PCP 0x00000002 +#define PCIEM_SLOT_CAP_MRLSP 0x00000004 +#define PCIEM_SLOT_CAP_AIP 0x00000008 +#define PCIEM_SLOT_CAP_PIP 0x00000010 +#define PCIEM_SLOT_CAP_HPS 0x00000020 +#define PCIEM_SLOT_CAP_HPC 0x00000040 +#define PCIEM_SLOT_CAP_SPLV 0x00007f80 +#define PCIEM_SLOT_CAP_SPLS 0x00018000 +#define PCIEM_SLOT_CAP_EIP 0x00020000 +#define PCIEM_SLOT_CAP_NCCS 0x00040000 +#define PCIEM_SLOT_CAP_PSN 0xfff80000 +#define PCIER_SLOT_CTL 0x18 +#define PCIEM_SLOT_CTL_ABPE 0x0001 +#define PCIEM_SLOT_CTL_PFDE 0x0002 +#define PCIEM_SLOT_CTL_MRLSCE 0x0004 +#define PCIEM_SLOT_CTL_PDCE 0x0008 +#define PCIEM_SLOT_CTL_CCIE 0x0010 +#define PCIEM_SLOT_CTL_HPIE 0x0020 +#define PCIEM_SLOT_CTL_AIC 0x00c0 +#define PCIEM_SLOT_CTL_PIC 0x0300 +#define PCIEM_SLOT_CTL_PCC 0x0400 +#define PCIEM_SLOT_CTL_EIC 0x0800 +#define PCIEM_SLOT_CTL_DLLSCE 0x1000 +#define PCIER_SLOT_STA 0x1a +#define PCIEM_SLOT_STA_ABP 0x0001 +#define PCIEM_SLOT_STA_PFD 0x0002 +#define PCIEM_SLOT_STA_MRLSC 0x0004 +#define PCIEM_SLOT_STA_PDC 0x0008 +#define PCIEM_SLOT_STA_CC 0x0010 +#define PCIEM_SLOT_STA_MRLSS 0x0020 +#define PCIEM_SLOT_STA_PDS 0x0040 +#define PCIEM_SLOT_STA_EIS 0x0080 +#define PCIEM_SLOT_STA_DLLSC 0x0100 +#define PCIER_ROOT_CTL 0x1c +#define PCIEM_ROOT_CTL_SERR_CORR 0x0001 +#define PCIEM_ROOT_CTL_SERR_NONFATAL 0x0002 +#define PCIEM_ROOT_CTL_SERR_FATAL 0x0004 +#define PCIEM_ROOT_CTL_PME 0x0008 +#define PCIEM_ROOT_CTL_CRS_VIS 0x0010 +#define PCIER_ROOT_CAP 0x1e +#define PCIEM_ROOT_CAP_CRS_VIS 0x0001 +#define PCIER_ROOT_STA 0x20 +#define PCIEM_ROOT_STA_PME_REQID_MASK 0x0000ffff +#define PCIEM_ROOT_STA_PME_STATUS 0x00010000 +#define PCIEM_ROOT_STA_PME_PEND 0x00020000 +#define PCIER_DEVICE_CAP2 0x24 +#define PCIEM_CAP2_ARI 0x20 +#define PCIER_DEVICE_CTL2 0x28 +#define PCIEM_CTL2_COMP_TIMEOUT_VAL 0x000f +#define PCIEM_CTL2_COMP_TIMEOUT_DIS 0x0010 +#define PCIEM_CTL2_ARI 0x0020 +#define PCIEM_CTL2_ATOMIC_REQ_ENABLE 0x0040 +#define PCIEM_CTL2_ATOMIC_EGR_BLOCK 0x0080 +#define PCIEM_CTL2_ID_ORDERED_REQ_EN 0x0100 +#define PCIEM_CTL2_ID_ORDERED_CMP_EN 0x0200 +#define PCIEM_CTL2_LTR_ENABLE 0x0400 +#define PCIEM_CTL2_OBFF 0x6000 +#define PCIEM_OBFF_DISABLE 0x0000 +#define PCIEM_OBFF_MSGA_ENABLE 0x2000 +#define PCIEM_OBFF_MSGB_ENABLE 0x4000 +#define PCIEM_OBFF_WAKE_ENABLE 0x6000 +#define PCIEM_CTL2_END2END_TLP 0x8000 +#define PCIER_DEVICE_STA2 0x2a +#define PCIER_LINK_CAP2 0x2c +#define PCIER_LINK_CTL2 0x30 +#define PCIER_LINK_STA2 0x32 +#define PCIER_SLOT_CAP2 0x34 +#define PCIER_SLOT_CTL2 0x38 +#define PCIER_SLOT_STA2 0x3a + +/* MSI-X definitions */ +#define PCIR_MSIX_CTRL 0x2 +#define PCIM_MSIXCTRL_MSIX_ENABLE 0x8000 +#define PCIM_MSIXCTRL_FUNCTION_MASK 0x4000 +#define PCIM_MSIXCTRL_TABLE_SIZE 0x07FF +#define PCIR_MSIX_TABLE 0x4 +#define PCIR_MSIX_PBA 0x8 +#define PCIM_MSIX_BIR_MASK 0x7 +#define PCIM_MSIX_BIR_BAR_10 0 +#define PCIM_MSIX_BIR_BAR_14 1 +#define PCIM_MSIX_BIR_BAR_18 2 +#define PCIM_MSIX_BIR_BAR_1C 3 +#define PCIM_MSIX_BIR_BAR_20 4 +#define PCIM_MSIX_BIR_BAR_24 5 +#define PCIM_MSIX_VCTRL_MASK 0x1 + +/* PCI Advanced Features definitions */ +#define PCIR_PCIAF_CAP 0x3 +#define PCIM_PCIAFCAP_TP 0x01 +#define PCIM_PCIAFCAP_FLR 0x02 +#define PCIR_PCIAF_CTRL 0x4 +#define PCIR_PCIAFCTRL_FLR 0x01 +#define PCIR_PCIAF_STATUS 0x5 +#define PCIR_PCIAFSTATUS_TP 0x01 + +/* Advanced Error Reporting */ +#define PCIR_AER_UC_STATUS 0x04 +#define PCIM_AER_UC_TRAINING_ERROR 0x00000001 +#define PCIM_AER_UC_DL_PROTOCOL_ERROR 0x00000010 +#define PCIM_AER_UC_SURPRISE_LINK_DOWN 0x00000020 +#define PCIM_AER_UC_POISONED_TLP 0x00001000 +#define PCIM_AER_UC_FC_PROTOCOL_ERROR 0x00002000 +#define PCIM_AER_UC_COMPLETION_TIMEOUT 0x00004000 +#define PCIM_AER_UC_COMPLETER_ABORT 0x00008000 +#define PCIM_AER_UC_UNEXPECTED_COMPLETION 0x00010000 +#define PCIM_AER_UC_RECEIVER_OVERFLOW 0x00020000 +#define PCIM_AER_UC_MALFORMED_TLP 0x00040000 +#define PCIM_AER_UC_ECRC_ERROR 0x00080000 +#define PCIM_AER_UC_UNSUPPORTED_REQUEST 0x00100000 +#define PCIM_AER_UC_ACS_VIOLATION 0x00200000 +#define PCIM_AER_UC_INTERNAL_ERROR 0x00400000 +#define PCIM_AER_UC_MC_BLOCKED_TLP 0x00800000 +#define PCIM_AER_UC_ATOMIC_EGRESS_BLK 0x01000000 +#define PCIM_AER_UC_TLP_PREFIX_BLOCKED 0x02000000 +#define PCIR_AER_UC_MASK 0x08 /* Shares bits with UC_STATUS */ +#define PCIR_AER_UC_SEVERITY 0x0c /* Shares bits with UC_STATUS */ +#define PCIR_AER_COR_STATUS 0x10 +#define PCIM_AER_COR_RECEIVER_ERROR 0x00000001 +#define PCIM_AER_COR_BAD_TLP 0x00000040 +#define PCIM_AER_COR_BAD_DLLP 0x00000080 +#define PCIM_AER_COR_REPLAY_ROLLOVER 0x00000100 +#define PCIM_AER_COR_REPLAY_TIMEOUT 0x00001000 +#define PCIM_AER_COR_ADVISORY_NF_ERROR 0x00002000 +#define PCIM_AER_COR_INTERNAL_ERROR 0x00004000 +#define PCIM_AER_COR_HEADER_LOG_OVFLOW 0x00008000 +#define PCIR_AER_COR_MASK 0x14 /* Shares bits with COR_STATUS */ +#define PCIR_AER_CAP_CONTROL 0x18 +#define PCIM_AER_FIRST_ERROR_PTR 0x0000001f +#define PCIM_AER_ECRC_GEN_CAPABLE 0x00000020 +#define PCIM_AER_ECRC_GEN_ENABLE 0x00000040 +#define PCIM_AER_ECRC_CHECK_CAPABLE 0x00000080 +#define PCIM_AER_ECRC_CHECK_ENABLE 0x00000100 +#define PCIM_AER_MULT_HDR_CAPABLE 0x00000200 +#define PCIM_AER_MULT_HDR_ENABLE 0x00000400 +#define PCIM_AER_TLP_PREFIX_LOG_PRESENT 0x00000800 +#define PCIR_AER_HEADER_LOG 0x1c +#define PCIR_AER_ROOTERR_CMD 0x2c /* Only for root complex ports */ +#define PCIM_AER_ROOTERR_COR_ENABLE 0x00000001 +#define PCIM_AER_ROOTERR_NF_ENABLE 0x00000002 +#define PCIM_AER_ROOTERR_F_ENABLE 0x00000004 +#define PCIR_AER_ROOTERR_STATUS 0x30 /* Only for root complex ports */ +#define PCIM_AER_ROOTERR_COR_ERR 0x00000001 +#define PCIM_AER_ROOTERR_MULTI_COR_ERR 0x00000002 +#define PCIM_AER_ROOTERR_UC_ERR 0x00000004 +#define PCIM_AER_ROOTERR_MULTI_UC_ERR 0x00000008 +#define PCIM_AER_ROOTERR_FIRST_UC_FATAL 0x00000010 +#define PCIM_AER_ROOTERR_NF_ERR 0x00000020 +#define PCIM_AER_ROOTERR_F_ERR 0x00000040 +#define PCIM_AER_ROOTERR_INT_MESSAGE 0xf8000000 +#define PCIR_AER_COR_SOURCE_ID 0x34 /* Only for root complex ports */ +#define PCIR_AER_ERR_SOURCE_ID 0x36 /* Only for root complex ports */ +#define PCIR_AER_TLP_PREFIX_LOG 0x38 /* Only for TLP prefix functions */ + +/* Virtual Channel definitions */ +#define PCIR_VC_CAP1 0x04 +#define PCIM_VC_CAP1_EXT_COUNT 0x00000007 +#define PCIM_VC_CAP1_LOWPRI_EXT_COUNT 0x00000070 +#define PCIR_VC_CAP2 0x08 +#define PCIR_VC_CONTROL 0x0C +#define PCIR_VC_STATUS 0x0E +#define PCIR_VC_RESOURCE_CAP(n) (0x10 + (n) * 0x0C) +#define PCIR_VC_RESOURCE_CTL(n) (0x14 + (n) * 0x0C) +#define PCIR_VC_RESOURCE_STA(n) (0x18 + (n) * 0x0C) + +/* Serial Number definitions */ +#define PCIR_SERIAL_LOW 0x04 +#define PCIR_SERIAL_HIGH 0x08 + +/* SR-IOV definitions */ +#define PCIR_SRIOV_CTL 0x08 +#define PCIM_SRIOV_VF_EN 0x01 +#define PCIM_SRIOV_VF_MSE 0x08 /* Memory space enable. */ +#define PCIM_SRIOV_ARI_EN 0x10 +#define PCIR_SRIOV_TOTAL_VFS 0x0E +#define PCIR_SRIOV_NUM_VFS 0x10 +#define PCIR_SRIOV_VF_OFF 0x14 +#define PCIR_SRIOV_VF_STRIDE 0x16 +#define PCIR_SRIOV_VF_DID 0x1A +#define PCIR_SRIOV_PAGE_CAP 0x1C +#define PCIR_SRIOV_PAGE_SIZE 0x20 + +#define PCI_SRIOV_BASE_PAGE_SHIFT 12 + +#define PCIR_SRIOV_BARS 0x24 +#define PCIR_SRIOV_BAR(x) (PCIR_SRIOV_BARS + (x) * 4) diff --git a/include/xhyve/support/psl.h b/include/xhyve/support/psl.h new file mode 100644 index 0000000..a4779c0 --- /dev/null +++ b/include/xhyve/support/psl.h @@ -0,0 +1,89 @@ +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * from: @(#)psl.h 5.2 (Berkeley) 1/18/91 + * $FreeBSD$ + */ + +#pragma once + +/* + * 386 processor status longword. + */ +#define PSL_C 0x00000001 /* carry bit */ +#define PSL_PF 0x00000004 /* parity bit */ +#define PSL_AF 0x00000010 /* bcd carry bit */ +#define PSL_Z 0x00000040 /* zero bit */ +#define PSL_N 0x00000080 /* negative bit */ +#define PSL_T 0x00000100 /* trace enable bit */ +#define PSL_I 0x00000200 /* interrupt enable bit */ +#define PSL_D 0x00000400 /* string instruction direction bit */ +#define PSL_V 0x00000800 /* overflow bit */ +#define PSL_IOPL 0x00003000 /* i/o privilege level */ +#define PSL_NT 0x00004000 /* nested task bit */ +#define PSL_RF 0x00010000 /* resume flag bit */ +#define PSL_VM 0x00020000 /* virtual 8086 mode bit */ +#define PSL_AC 0x00040000 /* alignment checking */ +#define PSL_VIF 0x00080000 /* virtual interrupt enable */ +#define PSL_VIP 0x00100000 /* virtual interrupt pending */ +#define PSL_ID 0x00200000 /* identification bit */ + +/* + * The i486 manual says that we are not supposed to change reserved flags, + * but this is too much trouble since the reserved flags depend on the cpu + * and setting them to their historical values works in practice. + */ +#define PSL_RESERVED_DEFAULT 0x00000002 + +/* + * Initial flags for kernel and user mode. The kernel later inherits + * PSL_I and some other flags from user mode. + */ +#define PSL_KERNEL PSL_RESERVED_DEFAULT +#define PSL_USER (PSL_RESERVED_DEFAULT | PSL_I) + +/* + * Bits that can be changed in user mode on 486's. We allow these bits + * to be changed using ptrace(), sigreturn() and procfs. Setting PS_NT + * is undesirable but it may as well be allowed since users can inflict + * it on the kernel directly. Changes to PSL_AC are silently ignored on + * 386's. + * + * Users are allowed to change the privileged flag PSL_RF. The cpu sets PSL_RF + * in tf_eflags for faults. Debuggers should sometimes set it there too. + * tf_eflags is kept in the signal context during signal handling and there is + * no other place to remember it, so the PSL_RF bit may be corrupted by the + * signal handler without us knowing. Corruption of the PSL_RF bit at worst + * causes one more or one less debugger trap, so allowing it is fairly + * harmless. + */ +#define PSL_USERCHANGE (PSL_C | PSL_PF | PSL_AF | PSL_Z | PSL_N | PSL_T \ + | PSL_D | PSL_V | PSL_NT | PSL_RF | PSL_AC | PSL_ID) diff --git a/include/xhyve/support/rtc.h b/include/xhyve/support/rtc.h new file mode 100644 index 0000000..fead20c --- /dev/null +++ b/include/xhyve/support/rtc.h @@ -0,0 +1,111 @@ +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * from: @(#)rtc.h 7.1 (Berkeley) 5/12/91 + * $FreeBSD$ + */ + +/* + * MC146818 RTC Register locations + */ + +#pragma once + +#define RTC_SEC 0x00 /* seconds */ +#define RTC_SECALRM 0x01 /* seconds alarm */ +#define RTC_MIN 0x02 /* minutes */ +#define RTC_MINALRM 0x03 /* minutes alarm */ +#define RTC_HRS 0x04 /* hours */ +#define RTC_HRSALRM 0x05 /* hours alarm */ +#define RTC_WDAY 0x06 /* week day */ +#define RTC_DAY 0x07 /* day of month */ +#define RTC_MONTH 0x08 /* month of year */ +#define RTC_YEAR 0x09 /* month of year */ + +#define RTC_STATUSA 0x0a /* status register A */ +#define RTCSA_TUP 0x80 /* time update, don't look now */ +#define RTCSA_RESET 0x70 /* reset divider */ +#define RTCSA_DIVIDER 0x20 /* divider correct for 32768 Hz */ +#define RTCSA_8192 0x03 /* 8192 Hz interrupt */ +#define RTCSA_4096 0x04 +#define RTCSA_2048 0x05 +#define RTCSA_1024 0x06 /* default for profiling */ +#define RTCSA_PROF RTCSA_1024 +#define RTC_PROFRATE 1024 +#define RTCSA_512 0x07 +#define RTCSA_256 0x08 +#define RTCSA_128 0x09 +#define RTCSA_NOPROF RTCSA_128 +#define RTC_NOPROFRATE 128 +#define RTCSA_64 0x0a +#define RTCSA_32 0x0b /* 32 Hz interrupt */ + +#define RTC_STATUSB 0x0b /* status register B */ +#define RTCSB_DST 0x01 /* USA Daylight Savings Time enable */ +#define RTCSB_24HR 0x02 /* 0 = 12 hours, 1 = 24 hours */ +#define RTCSB_BCD 0x04 /* 0 = BCD, 1 = Binary coded time */ +#define RTCSB_SQWE 0x08 /* 1 = output sqare wave at SQW pin */ +#define RTCSB_UINTR 0x10 /* 1 = enable update-ended interrupt */ +#define RTCSB_AINTR 0x20 /* 1 = enable alarm interrupt */ +#define RTCSB_PINTR 0x40 /* 1 = enable periodic clock interrupt */ +#define RTCSB_HALT 0x80 /* stop clock updates */ + +#define RTC_INTR 0x0c /* status register C (R) interrupt source */ +#define RTCIR_UPDATE 0x10 /* update intr */ +#define RTCIR_ALARM 0x20 /* alarm intr */ +#define RTCIR_PERIOD 0x40 /* periodic intr */ +#define RTCIR_INT 0x80 /* interrupt output signal */ + +#define RTC_STATUSD 0x0d /* status register D (R) Lost Power */ +#define RTCSD_PWR 0x80 /* clock power OK */ + +#define RTC_DIAG 0x0e /* status register E - bios diagnostic */ +#define RTCDG_BITS "\020\010clock_battery\007ROM_cksum\006config_unit\005memory_size\004fixed_disk\003invalid_time" + +#define RTC_RESET 0x0f /* status register F - reset code byte */ +#define RTCRS_RST 0x00 /* normal reset */ +#define RTCRS_LOAD 0x04 /* load system */ + +#define RTC_FDISKETTE 0x10 /* diskette drive type in upper/lower nibble */ +#define RTCFDT_NONE 0 /* none present */ +#define RTCFDT_360K 0x10 /* 360K */ +#define RTCFDT_12M 0x20 /* 1.2M */ +#define RTCFDT_720K 0x30 /* 720K */ +#define RTCFDT_144M 0x40 /* 1.44M */ +#define RTCFDT_288M_1 0x50 /* 2.88M, some BIOSes */ +#define RTCFDT_288M 0x60 /* 2.88M */ + +#define RTC_BASELO 0x15 /* low byte of basemem size */ +#define RTC_BASEHI 0x16 /* high byte of basemem size */ +#define RTC_EXTLO 0x17 /* low byte of extended mem size */ +#define RTC_EXTHI 0x18 /* low byte of extended mem size */ + +#define RTC_CENTURY 0x32 /* current century */ diff --git a/include/xhyve/support/segments.h b/include/xhyve/support/segments.h new file mode 100644 index 0000000..4a75c9b --- /dev/null +++ b/include/xhyve/support/segments.h @@ -0,0 +1,277 @@ +/*- + * Copyright (c) 1989, 1990 William F. Jolitz + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * from: @(#)segments.h 7.1 (Berkeley) 5/9/91 + * $FreeBSD$ + */ + +#pragma once + +#include +#include + +// /* +// * X86 Segmentation Data Structures and definitions +// */ + +/* Selectors */ +#define SEL_RPL_MASK 3 /* requester priv level */ +#define ISPL(s) ((s) & 3) /* priority level of a selector */ +#define SEL_KPL 0 /* kernel priority level */ +#define SEL_UPL 3 /* user priority level */ +#define ISLDT(s) ((s) & SEL_LDT) /* is it local or global */ +#define SEL_LDT 4 /* local descriptor table */ +#define IDXSEL(s) (((s)>>3) & 0x1fff) /* index of selector */ +#define LSEL(s,r) (((s)<<3) | SEL_LDT | r) /* a local selector */ +#define GSEL(s,r) (((s)<<3) | r) /* a global selector */ + +// /* +// * User segment descriptors (%cs, %ds etc for i386 apps. 64 bit wide) +// * For long-mode apps, %cs only has the conforming bit in sd_type, the sd_dpl, +// * sd_p, sd_l and sd_def32 which must be zero). %ds only has sd_p. +// */ +// struct segment_descriptor { +// unsigned sd_lolimit:16; /* segment extent (lsb) */ +// unsigned sd_lobase:24; /* segment base address (lsb) */ +// unsigned sd_type:5; /* segment type */ +// unsigned sd_dpl:2; /* segment descriptor priority level */ +// unsigned sd_p:1; /* segment descriptor present */ +// unsigned sd_hilimit:4; /* segment extent (msb) */ +// unsigned sd_xx:2; /* unused */ +// unsigned sd_def32:1; /* default 32 vs 16 bit size */ +// unsigned sd_gran:1; /* limit granularity (byte/page units)*/ +// unsigned sd_hibase:8; /* segment base address (msb) */ +// } __packed; + +struct user_segment_descriptor { + uint64_t sd_lolimit:16; /* segment extent (lsb) */ + uint64_t sd_lobase:24; /* segment base address (lsb) */ + uint64_t sd_type:5; /* segment type */ + uint64_t sd_dpl:2; /* segment descriptor priority level */ + uint64_t sd_p:1; /* segment descriptor present */ + uint64_t sd_hilimit:4; /* segment extent (msb) */ + uint64_t sd_xx:1; /* unused */ + uint64_t sd_long:1; /* long mode (cs only) */ + uint64_t sd_def32:1; /* default 32 vs 16 bit size */ + uint64_t sd_gran:1; /* limit granularity (byte/page units)*/ + uint64_t sd_hibase:8; /* segment base address (msb) */ +}; + +#define USD_GETBASE(sd) (((sd)->sd_lobase) | (sd)->sd_hibase << 24) +#define USD_SETBASE(sd, b) (sd)->sd_lobase = (b); \ + (sd)->sd_hibase = ((b) >> 24); +#define USD_GETLIMIT(sd) (((sd)->sd_lolimit) | (sd)->sd_hilimit << 16) +#define USD_SETLIMIT(sd, l) (sd)->sd_lolimit = (l); \ + (sd)->sd_hilimit = ((l) >> 16); + +// #ifdef __i386__ +// /* +// * Gate descriptors (e.g. indirect descriptors) +// */ +// struct gate_descriptor { +// unsigned gd_looffset:16; /* gate offset (lsb) */ +// unsigned gd_selector:16; /* gate segment selector */ +// unsigned gd_stkcpy:5; /* number of stack wds to cpy */ +// unsigned gd_xx:3; /* unused */ +// unsigned gd_type:5; /* segment type */ +// unsigned gd_dpl:2; /* segment descriptor priority level */ +// unsigned gd_p:1; /* segment descriptor present */ +// unsigned gd_hioffset:16; /* gate offset (msb) */ +// } __packed; + +// /* +// * Generic descriptor +// */ +// union descriptor { +// struct segment_descriptor sd; +// struct gate_descriptor gd; +// }; +// #else +// /* +// * Gate descriptors (e.g. indirect descriptors, trap, interrupt etc. 128 bit) +// * Only interrupt and trap gates have gd_ist. +// */ +// struct gate_descriptor { +// uint64_t gd_looffset:16; /* gate offset (lsb) */ +// uint64_t gd_selector:16; /* gate segment selector */ +// uint64_t gd_ist:3; /* IST table index */ +// uint64_t gd_xx:5; /* unused */ +// uint64_t gd_type:5; /* segment type */ +// uint64_t gd_dpl:2; /* segment descriptor priority level */ +// uint64_t gd_p:1; /* segment descriptor present */ +// uint64_t gd_hioffset:48; /* gate offset (msb) */ +// uint64_t sd_xx1:32; +// } __packed; + +// /* +// * Generic descriptor +// */ +// union descriptor { +// struct user_segment_descriptor sd; +// struct gate_descriptor gd; +// }; +// #endif + + /* system segments and gate types */ +#define SDT_SYSNULL 0 /* system null */ +#define SDT_SYS286TSS 1 /* system 286 TSS available */ +#define SDT_SYSLDT 2 /* system local descriptor table */ +#define SDT_SYS286BSY 3 /* system 286 TSS busy */ +#define SDT_SYS286CGT 4 /* system 286 call gate */ +#define SDT_SYSTASKGT 5 /* system task gate */ +#define SDT_SYS286IGT 6 /* system 286 interrupt gate */ +#define SDT_SYS286TGT 7 /* system 286 trap gate */ +#define SDT_SYSNULL2 8 /* system null again */ +#define SDT_SYS386TSS 9 /* system 386 TSS available */ +#define SDT_SYSTSS 9 /* system available 64 bit TSS */ +#define SDT_SYSNULL3 10 /* system null again */ +#define SDT_SYS386BSY 11 /* system 386 TSS busy */ +#define SDT_SYSBSY 11 /* system busy 64 bit TSS */ +#define SDT_SYS386CGT 12 /* system 386 call gate */ +#define SDT_SYSCGT 12 /* system 64 bit call gate */ +#define SDT_SYSNULL4 13 /* system null again */ +#define SDT_SYS386IGT 14 /* system 386 interrupt gate */ +#define SDT_SYSIGT 14 /* system 64 bit interrupt gate */ +#define SDT_SYS386TGT 15 /* system 386 trap gate */ +#define SDT_SYSTGT 15 /* system 64 bit trap gate */ + +// /* memory segment types */ +// #define SDT_MEMRO 16 memory read only +// #define SDT_MEMROA 17 /* memory read only accessed */ +// #define SDT_MEMRW 18 /* memory read write */ +// #define SDT_MEMRWA 19 /* memory read write accessed */ +// #define SDT_MEMROD 20 /* memory read only expand dwn limit */ +// #define SDT_MEMRODA 21 /* memory read only expand dwn limit accessed */ +// #define SDT_MEMRWD 22 /* memory read write expand dwn limit */ +// #define SDT_MEMRWDA 23 /* memory read write expand dwn limit accessed*/ +// #define SDT_MEME 24 /* memory execute only */ +// #define SDT_MEMEA 25 /* memory execute only accessed */ +// #define SDT_MEMER 26 /* memory execute read */ +// #define SDT_MEMERA 27 /* memory execute read accessed */ +// #define SDT_MEMEC 28 /* memory execute only conforming */ +// #define SDT_MEMEAC 29 /* memory execute only accessed conforming */ +// #define SDT_MEMERC 30 /* memory execute read conforming */ +// #define SDT_MEMERAC 31 /* memory execute read accessed conforming */ + +// /* +// * Size of IDT table +// */ +// #define NIDT 256 /* 32 reserved, 0x80 syscall, most are h/w */ +// #define NRSVIDT 32 /* reserved entries for cpu exceptions */ + +/* + * Entries in the Interrupt Descriptor Table (IDT) + */ +#define IDT_DE 0 /* #DE: Divide Error */ +#define IDT_DB 1 /* #DB: Debug */ +#define IDT_NMI 2 /* Nonmaskable External Interrupt */ +#define IDT_BP 3 /* #BP: Breakpoint */ +#define IDT_OF 4 /* #OF: Overflow */ +#define IDT_BR 5 /* #BR: Bound Range Exceeded */ +#define IDT_UD 6 /* #UD: Undefined/Invalid Opcode */ +#define IDT_NM 7 /* #NM: No Math Coprocessor */ +#define IDT_DF 8 /* #DF: Double Fault */ +#define IDT_FPUGP 9 /* Coprocessor Segment Overrun */ +#define IDT_TS 10 /* #TS: Invalid TSS */ +#define IDT_NP 11 /* #NP: Segment Not Present */ +#define IDT_SS 12 /* #SS: Stack Segment Fault */ +#define IDT_GP 13 /* #GP: General Protection Fault */ +#define IDT_PF 14 /* #PF: Page Fault */ +#define IDT_MF 16 /* #MF: FPU Floating-Point Error */ +#define IDT_AC 17 /* #AC: Alignment Check */ +#define IDT_MC 18 /* #MC: Machine Check */ +#define IDT_XF 19 /* #XF: SIMD Floating-Point Exception */ +#define IDT_IO_INTS NRSVIDT /* Base of IDT entries for I/O interrupts. */ +#define IDT_SYSCALL 0x80 /* System Call Interrupt Vector */ +#define IDT_DTRACE_RET 0x92 /* DTrace pid provider Interrupt Vector */ +#define IDT_EVTCHN 0x93 /* Xen HVM Event Channel Interrupt Vector */ + +// #if defined(__i386__) +// /* +// * Entries in the Global Descriptor Table (GDT) +// * Note that each 4 entries share a single 32 byte L1 cache line. +// * Some of the fast syscall instructions require a specific order here. +// */ +// #define GNULL_SEL 0 /* Null Descriptor */ +// #define GPRIV_SEL 1 /* SMP Per-Processor Private Data */ +// #define GUFS_SEL 2 /* User %fs Descriptor (order critical: 1) */ +// #define GUGS_SEL 3 /* User %gs Descriptor (order critical: 2) */ +// #define GCODE_SEL 4 /* Kernel Code Descriptor (order critical: 1) */ +// #define GDATA_SEL 5 /* Kernel Data Descriptor (order critical: 2) */ +// #define GUCODE_SEL 6 /* User Code Descriptor (order critical: 3) */ +// #define GUDATA_SEL 7 /* User Data Descriptor (order critical: 4) */ +// #define GBIOSLOWMEM_SEL 8 /* BIOS low memory access (must be entry 8) */ +// #define GPROC0_SEL 9 /* Task state process slot zero and up */ +// #define GLDT_SEL 10 /* Default User LDT */ +// #define GUSERLDT_SEL 11 /* User LDT */ +// #define GPANIC_SEL 12 /* Task state to consider panic from */ +// #define GBIOSCODE32_SEL 13 /* BIOS interface (32bit Code) */ +// #define GBIOSCODE16_SEL 14 /* BIOS interface (16bit Code) */ +// #define GBIOSDATA_SEL 15 /* BIOS interface (Data) */ +// #define GBIOSUTIL_SEL 16 /* BIOS interface (Utility) */ +// #define GBIOSARGS_SEL 17 /* BIOS interface (Arguments) */ +// #define GNDIS_SEL 18 /* For the NDIS layer */ +// #define NGDT 19 + +// /* +// * Entries in the Local Descriptor Table (LDT) +// */ +// #define LSYS5CALLS_SEL 0 /* forced by intel BCS */ +// #define LSYS5SIGR_SEL 1 +// #define L43BSDCALLS_SEL 2 /* notyet */ +// #define LUCODE_SEL 3 +// #define LSOL26CALLS_SEL 4 /* Solaris >= 2.6 system call gate */ +// #define LUDATA_SEL 5 +// /* separate stack, es,fs,gs sels ? */ +// /* #define LPOSIXCALLS_SEL 5*/ /* notyet */ +// #define LBSDICALLS_SEL 16 /* BSDI system call gate */ +// #define NLDT (LBSDICALLS_SEL + 1) + +// #else /* !__i386__ */ +// /* +// * Entries in the Global Descriptor Table (GDT) +// */ +// #define GNULL_SEL 0 /* Null Descriptor */ +// #define GNULL2_SEL 1 /* Null Descriptor */ +// #define GUFS32_SEL 2 /* User 32 bit %fs Descriptor */ +// #define GUGS32_SEL 3 /* User 32 bit %gs Descriptor */ +// #define GCODE_SEL 4 /* Kernel Code Descriptor */ +// #define GDATA_SEL 5 /* Kernel Data Descriptor */ +// #define GUCODE32_SEL 6 /* User 32 bit code Descriptor */ +// #define GUDATA_SEL 7 /* User 32/64 bit Data Descriptor */ +// #define GUCODE_SEL 8 /* User 64 bit Code Descriptor */ +// #define GPROC0_SEL 9 /* TSS for entering kernel etc */ +// /* slot 10 is second half of GPROC0_SEL */ +// #define GUSERLDT_SEL 11 /* LDT */ +// /* slot 12 is second half of GUSERLDT_SEL */ +// #define NGDT 13 +// #endif /* __i386__ */ diff --git a/include/xhyve/support/specialreg.h b/include/xhyve/support/specialreg.h new file mode 100644 index 0000000..70a6b44 --- /dev/null +++ b/include/xhyve/support/specialreg.h @@ -0,0 +1,845 @@ +/*- + * Copyright (c) 1991 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * from: @(#)specialreg.h 7.1 (Berkeley) 5/9/91 + * $FreeBSD$ + */ + +#pragma once + +/* + * Bits in 386 special registers: + */ +#define CR0_PE 0x00000001 /* Protected mode Enable */ +#define CR0_MP 0x00000002 /* "Math" (fpu) Present */ +#define CR0_EM 0x00000004 /* EMulate FPU instructions. (trap ESC only) */ +#define CR0_TS 0x00000008 /* Task Switched (if MP, trap ESC and WAIT) */ +#define CR0_PG 0x80000000 /* PaGing enable */ + +/* + * Bits in 486 special registers: + */ +#define CR0_ET 0x00000010 /* Extension type */ +#define CR0_NE 0x00000020 /* Numeric Error enable (EX16 vs IRQ13) */ +#define CR0_WP 0x00010000 /* Write Protect (honor page protect in + all modes) */ +#define CR0_AM 0x00040000 /* Alignment Mask (set to enable AC flag) */ +#define CR0_NW 0x20000000 /* Not Write-through */ +#define CR0_CD 0x40000000 /* Cache Disable */ + +#define CR3_PCID_SAVE 0x8000000000000000 +#define CR3_PCID_MASK 0xfff + +/* + * Bits in PPro special registers + */ +#define CR4_VME 0x00000001 /* Virtual 8086 mode extensions */ +#define CR4_PVI 0x00000002 /* Protected-mode virtual interrupts */ +#define CR4_TSD 0x00000004 /* Time stamp disable */ +#define CR4_DE 0x00000008 /* Debugging extensions */ +#define CR4_PSE 0x00000010 /* Page size extensions */ +#define CR4_PAE 0x00000020 /* Physical address extension */ +#define CR4_MCE 0x00000040 /* Machine check enable */ +#define CR4_PGE 0x00000080 /* Page global enable */ +#define CR4_PCE 0x00000100 /* Performance monitoring counter enable */ +#define CR4_FXSR 0x00000200 /* Fast FPU save/restore used by OS */ +#define CR4_XMM 0x00000400 /* enable SIMD/MMX2 to use except 16 */ +#define CR4_VMXE 0x00002000 /* enable VMX operation (Intel-specific) */ +#define CR4_FSGSBASE 0x00010000 /* Enable FS/GS BASE accessing instructions */ +#define CR4_PCIDE 0x00020000 /* Enable Context ID */ +#define CR4_XSAVE 0x00040000 /* XSETBV/XGETBV */ +#define CR4_SMEP 0x00100000 /* Supervisor-Mode Execution Prevention */ + +/* + * Bits in AMD64 special registers. EFER is 64 bits wide. + */ +#define EFER_SCE 0x000000001 /* System Call Extensions (R/W) */ +#define EFER_LME 0x000000100 /* Long mode enable (R/W) */ +#define EFER_LMA 0x000000400 /* Long mode active (R) */ +#define EFER_NXE 0x000000800 /* PTE No-Execute bit enable (R/W) */ +#define EFER_SVM 0x000001000 /* SVM enable bit for AMD, reserved for Intel */ +#define EFER_LMSLE 0x000002000 /* Long Mode Segment Limit Enable */ +#define EFER_FFXSR 0x000004000 /* Fast FXSAVE/FSRSTOR */ +#define EFER_TCE 0x000008000 /* Translation Cache Extension */ + +/* + * Intel Extended Features registers + */ +#define XCR0 0 /* XFEATURE_ENABLED_MASK register */ + +#define XFEATURE_ENABLED_X87 0x00000001 +#define XFEATURE_ENABLED_SSE 0x00000002 +#define XFEATURE_ENABLED_YMM_HI128 0x00000004 +#define XFEATURE_ENABLED_AVX XFEATURE_ENABLED_YMM_HI128 +#define XFEATURE_ENABLED_BNDREGS 0x00000008 +#define XFEATURE_ENABLED_BNDCSR 0x00000010 +#define XFEATURE_ENABLED_OPMASK 0x00000020 +#define XFEATURE_ENABLED_ZMM_HI256 0x00000040 +#define XFEATURE_ENABLED_HI16_ZMM 0x00000080 + +#define XFEATURE_AVX \ + (XFEATURE_ENABLED_X87 | XFEATURE_ENABLED_SSE | XFEATURE_ENABLED_AVX) +#define XFEATURE_AVX512 \ + (XFEATURE_ENABLED_OPMASK | XFEATURE_ENABLED_ZMM_HI256 | \ + XFEATURE_ENABLED_HI16_ZMM) +#define XFEATURE_MPX \ + (XFEATURE_ENABLED_BNDREGS | XFEATURE_ENABLED_BNDCSR) + +/* + * CPUID instruction features register + */ +#define CPUID_FPU 0x00000001 +#define CPUID_VME 0x00000002 +#define CPUID_DE 0x00000004 +#define CPUID_PSE 0x00000008 +#define CPUID_TSC 0x00000010 +#define CPUID_MSR 0x00000020 +#define CPUID_PAE 0x00000040 +#define CPUID_MCE 0x00000080 +#define CPUID_CX8 0x00000100 +#define CPUID_APIC 0x00000200 +#define CPUID_B10 0x00000400 +#define CPUID_SEP 0x00000800 +#define CPUID_MTRR 0x00001000 +#define CPUID_PGE 0x00002000 +#define CPUID_MCA 0x00004000 +#define CPUID_CMOV 0x00008000 +#define CPUID_PAT 0x00010000 +#define CPUID_PSE36 0x00020000 +#define CPUID_PSN 0x00040000 +#define CPUID_CLFSH 0x00080000 +#define CPUID_B20 0x00100000 +#define CPUID_DS 0x00200000 +#define CPUID_ACPI 0x00400000 +#define CPUID_MMX 0x00800000 +#define CPUID_FXSR 0x01000000 +#define CPUID_SSE 0x02000000 +#define CPUID_XMM 0x02000000 +#define CPUID_SSE2 0x04000000 +#define CPUID_SS 0x08000000 +#define CPUID_HTT 0x10000000 +#define CPUID_TM 0x20000000 +#define CPUID_IA64 0x40000000 +#define CPUID_PBE 0x80000000 + +#define CPUID2_SSE3 0x00000001 +#define CPUID2_PCLMULQDQ 0x00000002 +#define CPUID2_DTES64 0x00000004 +#define CPUID2_MON 0x00000008 +#define CPUID2_DS_CPL 0x00000010 +#define CPUID2_VMX 0x00000020 +#define CPUID2_SMX 0x00000040 +#define CPUID2_EST 0x00000080 +#define CPUID2_TM2 0x00000100 +#define CPUID2_SSSE3 0x00000200 +#define CPUID2_CNXTID 0x00000400 +#define CPUID2_SDBG 0x00000800 +#define CPUID2_FMA 0x00001000 +#define CPUID2_CX16 0x00002000 +#define CPUID2_XTPR 0x00004000 +#define CPUID2_PDCM 0x00008000 +#define CPUID2_PCID 0x00020000 +#define CPUID2_DCA 0x00040000 +#define CPUID2_SSE41 0x00080000 +#define CPUID2_SSE42 0x00100000 +#define CPUID2_X2APIC 0x00200000 +#define CPUID2_MOVBE 0x00400000 +#define CPUID2_POPCNT 0x00800000 +#define CPUID2_TSCDLT 0x01000000 +#define CPUID2_AESNI 0x02000000 +#define CPUID2_XSAVE 0x04000000 +#define CPUID2_OSXSAVE 0x08000000 +#define CPUID2_AVX 0x10000000 +#define CPUID2_F16C 0x20000000 +#define CPUID2_RDRAND 0x40000000 +#define CPUID2_HV 0x80000000 + +/* + * Important bits in the Thermal and Power Management flags + * CPUID.6 EAX and ECX. + */ +#define CPUTPM1_SENSOR 0x00000001 +#define CPUTPM1_TURBO 0x00000002 +#define CPUTPM1_ARAT 0x00000004 +#define CPUTPM2_EFFREQ 0x00000001 + +/* + * Important bits in the AMD extended cpuid flags + */ +#define AMDID_SYSCALL 0x00000800 +#define AMDID_MP 0x00080000 +#define AMDID_NX 0x00100000 +#define AMDID_EXT_MMX 0x00400000 +#define AMDID_FFXSR 0x02000000 +#define AMDID_PAGE1GB 0x04000000 +#define AMDID_RDTSCP 0x08000000 +#define AMDID_LM 0x20000000 +#define AMDID_EXT_3DNOW 0x40000000 +#define AMDID_3DNOW 0x80000000 + +#define AMDID2_LAHF 0x00000001 +#define AMDID2_CMP 0x00000002 +#define AMDID2_SVM 0x00000004 +#define AMDID2_EXT_APIC 0x00000008 +#define AMDID2_CR8 0x00000010 +#define AMDID2_ABM 0x00000020 +#define AMDID2_SSE4A 0x00000040 +#define AMDID2_MAS 0x00000080 +#define AMDID2_PREFETCH 0x00000100 +#define AMDID2_OSVW 0x00000200 +#define AMDID2_IBS 0x00000400 +#define AMDID2_XOP 0x00000800 +#define AMDID2_SKINIT 0x00001000 +#define AMDID2_WDT 0x00002000 +#define AMDID2_LWP 0x00008000 +#define AMDID2_FMA4 0x00010000 +#define AMDID2_TCE 0x00020000 +#define AMDID2_NODE_ID 0x00080000 +#define AMDID2_TBM 0x00200000 +#define AMDID2_TOPOLOGY 0x00400000 +#define AMDID2_PCXC 0x00800000 +#define AMDID2_PNXC 0x01000000 +#define AMDID2_DBE 0x04000000 +#define AMDID2_PTSC 0x08000000 +#define AMDID2_PTSCEL2I 0x10000000 + +/* + * CPUID instruction 1 eax info + */ +#define CPUID_STEPPING 0x0000000f +#define CPUID_MODEL 0x000000f0 +#define CPUID_FAMILY 0x00000f00 +#define CPUID_EXT_MODEL 0x000f0000 +#define CPUID_EXT_FAMILY 0x0ff00000 +// #ifdef __i386__ +// #define CPUID_TO_MODEL(id) \ +// ((((id) & CPUID_MODEL) >> 4) | \ +// ((((id) & CPUID_FAMILY) >= 0x600) ? \ +// (((id) & CPUID_EXT_MODEL) >> 12) : 0)) +// #define CPUID_TO_FAMILY(id) \ +// ((((id) & CPUID_FAMILY) >> 8) + \ +// ((((id) & CPUID_FAMILY) == 0xf00) ? \ +// (((id) & CPUID_EXT_FAMILY) >> 20) : 0)) +// #else +// #define CPUID_TO_MODEL(id) \ +// ((((id) & CPUID_MODEL) >> 4) | \ +// (((id) & CPUID_EXT_MODEL) >> 12)) +// #define CPUID_TO_FAMILY(id) \ +// ((((id) & CPUID_FAMILY) >> 8) + \ +// (((id) & CPUID_EXT_FAMILY) >> 20)) +// #endif + +/* + * CPUID instruction 1 ebx info + */ +#define CPUID_BRAND_INDEX 0x000000ff +#define CPUID_CLFUSH_SIZE 0x0000ff00 +#define CPUID_HTT_CORES 0x00ff0000 +#define CPUID_LOCAL_APIC_ID 0xff000000 + +/* + * CPUID instruction 5 info + */ +#define CPUID5_MON_MIN_SIZE 0x0000ffff /* eax */ +#define CPUID5_MON_MAX_SIZE 0x0000ffff /* ebx */ +#define CPUID5_MON_MWAIT_EXT 0x00000001 /* ecx */ +#define CPUID5_MWAIT_INTRBREAK 0x00000002 /* ecx */ + +/* + * MWAIT cpu power states. Lower 4 bits are sub-states. + */ +#define MWAIT_C0 0xf0 +#define MWAIT_C1 0x00 +#define MWAIT_C2 0x10 +#define MWAIT_C3 0x20 +#define MWAIT_C4 0x30 + +/* + * MWAIT extensions. + */ +/* Interrupt breaks MWAIT even when masked. */ +#define MWAIT_INTRBREAK 0x00000001 + +/* + * CPUID instruction 6 ecx info + */ +#define CPUID_PERF_STAT 0x00000001 +#define CPUID_PERF_BIAS 0x00000008 + +/* + * CPUID instruction 0xb ebx info. + */ +#define CPUID_TYPE_INVAL 0 +#define CPUID_TYPE_SMT 1 +#define CPUID_TYPE_CORE 2 + +/* + * CPUID instruction 0xd Processor Extended State Enumeration Sub-leaf 1 + */ +#define CPUID_EXTSTATE_XSAVEOPT 0x00000001 +#define CPUID_EXTSTATE_XSAVEC 0x00000002 +#define CPUID_EXTSTATE_XINUSE 0x00000004 +#define CPUID_EXTSTATE_XSAVES 0x00000008 + +/* + * AMD extended function 8000_0007h edx info + */ +#define AMDPM_TS 0x00000001 +#define AMDPM_FID 0x00000002 +#define AMDPM_VID 0x00000004 +#define AMDPM_TTP 0x00000008 +#define AMDPM_TM 0x00000010 +#define AMDPM_STC 0x00000020 +#define AMDPM_100MHZ_STEPS 0x00000040 +#define AMDPM_HW_PSTATE 0x00000080 +#define AMDPM_TSC_INVARIANT 0x00000100 +#define AMDPM_CPB 0x00000200 + +/* + * AMD extended function 8000_0008h ecx info + */ +#define AMDID_CMP_CORES 0x000000ff +#define AMDID_COREID_SIZE 0x0000f000 +#define AMDID_COREID_SIZE_SHIFT 12 + +/* + * CPUID instruction 7 Structured Extended Features, leaf 0 ebx info + */ +#define CPUID_STDEXT_FSGSBASE 0x00000001 +#define CPUID_STDEXT_TSC_ADJUST 0x00000002 +#define CPUID_STDEXT_BMI1 0x00000008 +#define CPUID_STDEXT_HLE 0x00000010 +#define CPUID_STDEXT_AVX2 0x00000020 +#define CPUID_STDEXT_SMEP 0x00000080 +#define CPUID_STDEXT_BMI2 0x00000100 +#define CPUID_STDEXT_ERMS 0x00000200 +#define CPUID_STDEXT_INVPCID 0x00000400 +#define CPUID_STDEXT_RTM 0x00000800 +#define CPUID_STDEXT_MPX 0x00004000 +#define CPUID_STDEXT_AVX512F 0x00010000 +#define CPUID_STDEXT_RDSEED 0x00040000 +#define CPUID_STDEXT_ADX 0x00080000 +#define CPUID_STDEXT_SMAP 0x00100000 +#define CPUID_STDEXT_CLFLUSHOPT 0x00800000 +#define CPUID_STDEXT_PROCTRACE 0x02000000 +#define CPUID_STDEXT_AVX512PF 0x04000000 +#define CPUID_STDEXT_AVX512ER 0x08000000 +#define CPUID_STDEXT_AVX512CD 0x10000000 +#define CPUID_STDEXT_SHA 0x20000000 + +/* + * CPUID manufacturers identifiers + */ +#define AMD_VENDOR_ID "AuthenticAMD" +#define CENTAUR_VENDOR_ID "CentaurHauls" +#define CYRIX_VENDOR_ID "CyrixInstead" +#define INTEL_VENDOR_ID "GenuineIntel" +#define NEXGEN_VENDOR_ID "NexGenDriven" +#define NSC_VENDOR_ID "Geode by NSC" +#define RISE_VENDOR_ID "RiseRiseRise" +#define SIS_VENDOR_ID "SiS SiS SiS " +#define TRANSMETA_VENDOR_ID "GenuineTMx86" +#define UMC_VENDOR_ID "UMC UMC UMC " + +/* + * Model-specific registers for the i386 family + */ +#define MSR_P5_MC_ADDR 0x000 +#define MSR_P5_MC_TYPE 0x001 +#define MSR_TSC 0x010 +#define MSR_P5_CESR 0x011 +#define MSR_P5_CTR0 0x012 +#define MSR_P5_CTR1 0x013 +#define MSR_IA32_PLATFORM_ID 0x017 +#define MSR_APICBASE 0x01b +#define MSR_EBL_CR_POWERON 0x02a +#define MSR_TEST_CTL 0x033 +#define MSR_IA32_FEATURE_CONTROL 0x03a +#define MSR_BIOS_UPDT_TRIG 0x079 +#define MSR_BBL_CR_D0 0x088 +#define MSR_BBL_CR_D1 0x089 +#define MSR_BBL_CR_D2 0x08a +#define MSR_BIOS_SIGN 0x08b +#define MSR_PERFCTR0 0x0c1 +#define MSR_PERFCTR1 0x0c2 +#define MSR_PLATFORM_INFO 0x0ce +#define MSR_MPERF 0x0e7 +#define MSR_APERF 0x0e8 +#define MSR_IA32_EXT_CONFIG 0x0ee /* Undocumented. Core Solo/Duo only */ +#define MSR_MTRRcap 0x0fe +#define MSR_BBL_CR_ADDR 0x116 +#define MSR_BBL_CR_DECC 0x118 +#define MSR_BBL_CR_CTL 0x119 +#define MSR_BBL_CR_TRIG 0x11a +#define MSR_BBL_CR_BUSY 0x11b +#define MSR_BBL_CR_CTL3 0x11e +#define MSR_SYSENTER_CS_MSR 0x174 +#define MSR_SYSENTER_ESP_MSR 0x175 +#define MSR_SYSENTER_EIP_MSR 0x176 +#define MSR_MCG_CAP 0x179 +#define MSR_MCG_STATUS 0x17a +#define MSR_MCG_CTL 0x17b +#define MSR_EVNTSEL0 0x186 +#define MSR_EVNTSEL1 0x187 +#define MSR_THERM_CONTROL 0x19a +#define MSR_THERM_INTERRUPT 0x19b +#define MSR_THERM_STATUS 0x19c +#define MSR_IA32_MISC_ENABLE 0x1a0 +#define MSR_IA32_TEMPERATURE_TARGET 0x1a2 +#define MSR_TURBO_RATIO_LIMIT 0x1ad +#define MSR_TURBO_RATIO_LIMIT1 0x1ae +#define MSR_DEBUGCTLMSR 0x1d9 +#define MSR_LASTBRANCHFROMIP 0x1db +#define MSR_LASTBRANCHTOIP 0x1dc +#define MSR_LASTINTFROMIP 0x1dd +#define MSR_LASTINTTOIP 0x1de +#define MSR_ROB_CR_BKUPTMPDR6 0x1e0 +#define MSR_MTRRVarBase 0x200 +#define MSR_MTRR64kBase 0x250 +#define MSR_MTRR16kBase 0x258 +#define MSR_MTRR4kBase 0x268 +#define MSR_PAT 0x277 +#define MSR_MC0_CTL2 0x280 +#define MSR_MTRRdefType 0x2ff +#define MSR_MC0_CTL 0x400 +#define MSR_MC0_STATUS 0x401 +#define MSR_MC0_ADDR 0x402 +#define MSR_MC0_MISC 0x403 +#define MSR_MC1_CTL 0x404 +#define MSR_MC1_STATUS 0x405 +#define MSR_MC1_ADDR 0x406 +#define MSR_MC1_MISC 0x407 +#define MSR_MC2_CTL 0x408 +#define MSR_MC2_STATUS 0x409 +#define MSR_MC2_ADDR 0x40a +#define MSR_MC2_MISC 0x40b +#define MSR_MC3_CTL 0x40c +#define MSR_MC3_STATUS 0x40d +#define MSR_MC3_ADDR 0x40e +#define MSR_MC3_MISC 0x40f +#define MSR_MC4_CTL 0x410 +#define MSR_MC4_STATUS 0x411 +#define MSR_MC4_ADDR 0x412 +#define MSR_MC4_MISC 0x413 +#define MSR_RAPL_POWER_UNIT 0x606 +#define MSR_PKG_ENERGY_STATUS 0x611 +#define MSR_DRAM_ENERGY_STATUS 0x619 +#define MSR_PP0_ENERGY_STATUS 0x639 +#define MSR_PP1_ENERGY_STATUS 0x641 + +/* + * VMX MSRs + */ +#define MSR_VMX_BASIC 0x480 +#define MSR_VMX_PINBASED_CTLS 0x481 +#define MSR_VMX_PROCBASED_CTLS 0x482 +#define MSR_VMX_EXIT_CTLS 0x483 +#define MSR_VMX_ENTRY_CTLS 0x484 +#define MSR_VMX_CR0_FIXED0 0x486 +#define MSR_VMX_CR0_FIXED1 0x487 +#define MSR_VMX_CR4_FIXED0 0x488 +#define MSR_VMX_CR4_FIXED1 0x489 +#define MSR_VMX_PROCBASED_CTLS2 0x48b +#define MSR_VMX_EPT_VPID_CAP 0x48c +#define MSR_VMX_TRUE_PINBASED_CTLS 0x48d +#define MSR_VMX_TRUE_PROCBASED_CTLS 0x48e +#define MSR_VMX_TRUE_EXIT_CTLS 0x48f +#define MSR_VMX_TRUE_ENTRY_CTLS 0x490 + +/* + * X2APIC MSRs + */ +#define MSR_APIC_000 0x800 +#define MSR_APIC_ID 0x802 +#define MSR_APIC_VERSION 0x803 +#define MSR_APIC_TPR 0x808 +#define MSR_APIC_EOI 0x80b +#define MSR_APIC_LDR 0x80d +#define MSR_APIC_SVR 0x80f +#define MSR_APIC_ISR0 0x810 +#define MSR_APIC_ISR1 0x811 +#define MSR_APIC_ISR2 0x812 +#define MSR_APIC_ISR3 0x813 +#define MSR_APIC_ISR4 0x814 +#define MSR_APIC_ISR5 0x815 +#define MSR_APIC_ISR6 0x816 +#define MSR_APIC_ISR7 0x817 +#define MSR_APIC_TMR0 0x818 +#define MSR_APIC_IRR0 0x820 +#define MSR_APIC_ESR 0x828 +#define MSR_APIC_LVT_CMCI 0x82F +#define MSR_APIC_ICR 0x830 +#define MSR_APIC_LVT_TIMER 0x832 +#define MSR_APIC_LVT_THERMAL 0x833 +#define MSR_APIC_LVT_PCINT 0x834 +#define MSR_APIC_LVT_LINT0 0x835 +#define MSR_APIC_LVT_LINT1 0x836 +#define MSR_APIC_LVT_ERROR 0x837 +#define MSR_APIC_ICR_TIMER 0x838 +#define MSR_APIC_CCR_TIMER 0x839 +#define MSR_APIC_DCR_TIMER 0x83e +#define MSR_APIC_SELF_IPI 0x83f + +#define MSR_IA32_XSS 0xda0 + +#define MSR_IA32_TSC_AUX 0xc0000103 + +/* + * Constants related to MSR's. + */ +#define APICBASE_RESERVED 0x000002ff +#define APICBASE_BSP 0x00000100 +#define APICBASE_X2APIC 0x00000400 +#define APICBASE_ENABLED 0x00000800 +#define APICBASE_ADDRESS 0xfffff000 + +/* MSR_IA32_FEATURE_CONTROL related */ +#define IA32_FEATURE_CONTROL_LOCK 0x01 /* lock bit */ +#define IA32_FEATURE_CONTROL_SMX_EN 0x02 /* enable VMX inside SMX */ +#define IA32_FEATURE_CONTROL_VMX_EN 0x04 /* enable VMX outside SMX */ + +/* + * PAT modes. + */ +#define PAT_UNCACHEABLE 0x00 +#define PAT_WRITE_COMBINING 0x01 +#define PAT_WRITE_THROUGH 0x04 +#define PAT_WRITE_PROTECTED 0x05 +#define PAT_WRITE_BACK 0x06 +#define PAT_UNCACHED 0x07 +#define PAT_VALUE(i, m) ((long long)(m) << (8 * (i))) +#define PAT_MASK(i) PAT_VALUE(i, 0xff) + +/* + * Constants related to MTRRs + */ +#define MTRR_UNCACHEABLE 0x00 +#define MTRR_WRITE_COMBINING 0x01 +#define MTRR_WRITE_THROUGH 0x04 +#define MTRR_WRITE_PROTECTED 0x05 +#define MTRR_WRITE_BACK 0x06 +#define MTRR_N64K 8 /* numbers of fixed-size entries */ +#define MTRR_N16K 16 +#define MTRR_N4K 64 +#define MTRR_CAP_WC 0x0000000000000400 +#define MTRR_CAP_FIXED 0x0000000000000100 +#define MTRR_CAP_VCNT 0x00000000000000ff +#define MTRR_DEF_ENABLE 0x0000000000000800 +#define MTRR_DEF_FIXED_ENABLE 0x0000000000000400 +#define MTRR_DEF_TYPE 0x00000000000000ff +#define MTRR_PHYSBASE_PHYSBASE 0x000ffffffffff000 +#define MTRR_PHYSBASE_TYPE 0x00000000000000ff +#define MTRR_PHYSMASK_PHYSMASK 0x000ffffffffff000 +#define MTRR_PHYSMASK_VALID 0x0000000000000800 + +/* + * Cyrix configuration registers, accessible as IO ports. + */ +#define CCR0 0xc0 /* Configuration control register 0 */ +#define CCR0_NC0 0x01 /* First 64K of each 1M memory region is + non-cacheable */ +#define CCR0_NC1 0x02 /* 640K-1M region is non-cacheable */ +#define CCR0_A20M 0x04 /* Enables A20M# input pin */ +#define CCR0_KEN 0x08 /* Enables KEN# input pin */ +#define CCR0_FLUSH 0x10 /* Enables FLUSH# input pin */ +#define CCR0_BARB 0x20 /* Flushes internal cache when entering hold + state */ +#define CCR0_CO 0x40 /* Cache org: 1=direct mapped, 0=2x set + assoc */ +#define CCR0_SUSPEND 0x80 /* Enables SUSP# and SUSPA# pins */ + +#define CCR1 0xc1 /* Configuration control register 1 */ +#define CCR1_RPL 0x01 /* Enables RPLSET and RPLVAL# pins */ +#define CCR1_SMI 0x02 /* Enables SMM pins */ +#define CCR1_SMAC 0x04 /* System management memory access */ +#define CCR1_MMAC 0x08 /* Main memory access */ +#define CCR1_NO_LOCK 0x10 /* Negate LOCK# */ +#define CCR1_SM3 0x80 /* SMM address space address region 3 */ + +#define CCR2 0xc2 +#define CCR2_WB 0x02 /* Enables WB cache interface pins */ +#define CCR2_SADS 0x02 /* Slow ADS */ +#define CCR2_LOCK_NW 0x04 /* LOCK NW Bit */ +#define CCR2_SUSP_HLT 0x08 /* Suspend on HALT */ +#define CCR2_WT1 0x10 /* WT region 1 */ +#define CCR2_WPR1 0x10 /* Write-protect region 1 */ +#define CCR2_BARB 0x20 /* Flushes write-back cache when entering + hold state. */ +#define CCR2_BWRT 0x40 /* Enables burst write cycles */ +#define CCR2_USE_SUSP 0x80 /* Enables suspend pins */ + +#define CCR3 0xc3 +#define CCR3_SMILOCK 0x01 /* SMM register lock */ +#define CCR3_NMI 0x02 /* Enables NMI during SMM */ +#define CCR3_LINBRST 0x04 /* Linear address burst cycles */ +#define CCR3_SMMMODE 0x08 /* SMM Mode */ +#define CCR3_MAPEN0 0x10 /* Enables Map0 */ +#define CCR3_MAPEN1 0x20 /* Enables Map1 */ +#define CCR3_MAPEN2 0x40 /* Enables Map2 */ +#define CCR3_MAPEN3 0x80 /* Enables Map3 */ + +#define CCR4 0xe8 +#define CCR4_IOMASK 0x07 +#define CCR4_MEM 0x08 /* Enables momory bypassing */ +#define CCR4_DTE 0x10 /* Enables directory table entry cache */ +#define CCR4_FASTFPE 0x20 /* Fast FPU exception */ +#define CCR4_CPUID 0x80 /* Enables CPUID instruction */ + +#define CCR5 0xe9 +#define CCR5_WT_ALLOC 0x01 /* Write-through allocate */ +#define CCR5_SLOP 0x02 /* LOOP instruction slowed down */ +#define CCR5_LBR1 0x10 /* Local bus region 1 */ +#define CCR5_ARREN 0x20 /* Enables ARR region */ + +#define CCR6 0xea + +#define CCR7 0xeb + +/* Performance Control Register (5x86 only). */ +#define PCR0 0x20 +#define PCR0_RSTK 0x01 /* Enables return stack */ +#define PCR0_BTB 0x02 /* Enables branch target buffer */ +#define PCR0_LOOP 0x04 /* Enables loop */ +#define PCR0_AIS 0x08 /* Enables all instrcutions stalled to + serialize pipe. */ +#define PCR0_MLR 0x10 /* Enables reordering of misaligned loads */ +#define PCR0_BTBRT 0x40 /* Enables BTB test register. */ +#define PCR0_LSSER 0x80 /* Disable reorder */ + +/* Device Identification Registers */ +#define DIR0 0xfe +#define DIR1 0xff + +/* + * Machine Check register constants. + */ +#define MCG_CAP_COUNT 0x000000ff +#define MCG_CAP_CTL_P 0x00000100 +#define MCG_CAP_EXT_P 0x00000200 +#define MCG_CAP_CMCI_P 0x00000400 +#define MCG_CAP_TES_P 0x00000800 +#define MCG_CAP_EXT_CNT 0x00ff0000 +#define MCG_CAP_SER_P 0x01000000 +#define MCG_STATUS_RIPV 0x00000001 +#define MCG_STATUS_EIPV 0x00000002 +#define MCG_STATUS_MCIP 0x00000004 +#define MCG_CTL_ENABLE 0xffffffffffffffff +#define MCG_CTL_DISABLE 0x0000000000000000 +#define MSR_MC_CTL(x) (MSR_MC0_CTL + (x) * 4) +#define MSR_MC_STATUS(x) (MSR_MC0_STATUS + (x) * 4) +#define MSR_MC_ADDR(x) (MSR_MC0_ADDR + (x) * 4) +#define MSR_MC_MISC(x) (MSR_MC0_MISC + (x) * 4) +#define MSR_MC_CTL2(x) (MSR_MC0_CTL2 + (x)) /* If MCG_CAP_CMCI_P */ +#define MC_STATUS_MCA_ERROR 0x000000000000ffff +#define MC_STATUS_MODEL_ERROR 0x00000000ffff0000 +#define MC_STATUS_OTHER_INFO 0x01ffffff00000000 +#define MC_STATUS_COR_COUNT 0x001fffc000000000 /* If MCG_CAP_CMCI_P */ +#define MC_STATUS_TES_STATUS 0x0060000000000000 /* If MCG_CAP_TES_P */ +#define MC_STATUS_AR 0x0080000000000000 /* If MCG_CAP_TES_P */ +#define MC_STATUS_S 0x0100000000000000 /* If MCG_CAP_TES_P */ +#define MC_STATUS_PCC 0x0200000000000000 +#define MC_STATUS_ADDRV 0x0400000000000000 +#define MC_STATUS_MISCV 0x0800000000000000 +#define MC_STATUS_EN 0x1000000000000000 +#define MC_STATUS_UC 0x2000000000000000 +#define MC_STATUS_OVER 0x4000000000000000 +#define MC_STATUS_VAL 0x8000000000000000 +#define MC_MISC_RA_LSB 0x000000000000003f /* If MCG_CAP_SER_P */ +#define MC_MISC_ADDRESS_MODE 0x00000000000001c0 /* If MCG_CAP_SER_P */ +#define MC_CTL2_THRESHOLD 0x0000000000007fff +#define MC_CTL2_CMCI_EN 0x0000000040000000 + +/* + * The following four 3-byte registers control the non-cacheable regions. + * These registers must be written as three separate bytes. + * + * NCRx+0: A31-A24 of starting address + * NCRx+1: A23-A16 of starting address + * NCRx+2: A15-A12 of starting address | NCR_SIZE_xx. + * + * The non-cacheable region's starting address must be aligned to the + * size indicated by the NCR_SIZE_xx field. + */ +#define NCR1 0xc4 +#define NCR2 0xc7 +#define NCR3 0xca +#define NCR4 0xcd + +#define NCR_SIZE_0K 0 +#define NCR_SIZE_4K 1 +#define NCR_SIZE_8K 2 +#define NCR_SIZE_16K 3 +#define NCR_SIZE_32K 4 +#define NCR_SIZE_64K 5 +#define NCR_SIZE_128K 6 +#define NCR_SIZE_256K 7 +#define NCR_SIZE_512K 8 +#define NCR_SIZE_1M 9 +#define NCR_SIZE_2M 10 +#define NCR_SIZE_4M 11 +#define NCR_SIZE_8M 12 +#define NCR_SIZE_16M 13 +#define NCR_SIZE_32M 14 +#define NCR_SIZE_4G 15 + +/* + * The address region registers are used to specify the location and + * size for the eight address regions. + * + * ARRx + 0: A31-A24 of start address + * ARRx + 1: A23-A16 of start address + * ARRx + 2: A15-A12 of start address | ARR_SIZE_xx + */ +#define ARR0 0xc4 +#define ARR1 0xc7 +#define ARR2 0xca +#define ARR3 0xcd +#define ARR4 0xd0 +#define ARR5 0xd3 +#define ARR6 0xd6 +#define ARR7 0xd9 + +#define ARR_SIZE_0K 0 +#define ARR_SIZE_4K 1 +#define ARR_SIZE_8K 2 +#define ARR_SIZE_16K 3 +#define ARR_SIZE_32K 4 +#define ARR_SIZE_64K 5 +#define ARR_SIZE_128K 6 +#define ARR_SIZE_256K 7 +#define ARR_SIZE_512K 8 +#define ARR_SIZE_1M 9 +#define ARR_SIZE_2M 10 +#define ARR_SIZE_4M 11 +#define ARR_SIZE_8M 12 +#define ARR_SIZE_16M 13 +#define ARR_SIZE_32M 14 +#define ARR_SIZE_4G 15 + +/* + * The region control registers specify the attributes associated with + * the ARRx addres regions. + */ +#define RCR0 0xdc +#define RCR1 0xdd +#define RCR2 0xde +#define RCR3 0xdf +#define RCR4 0xe0 +#define RCR5 0xe1 +#define RCR6 0xe2 +#define RCR7 0xe3 + +#define RCR_RCD 0x01 /* Disables caching for ARRx (x = 0-6). */ +#define RCR_RCE 0x01 /* Enables caching for ARR7. */ +#define RCR_WWO 0x02 /* Weak write ordering. */ +#define RCR_WL 0x04 /* Weak locking. */ +#define RCR_WG 0x08 /* Write gathering. */ +#define RCR_WT 0x10 /* Write-through. */ +#define RCR_NLB 0x20 /* LBA# pin is not asserted. */ + +/* AMD Write Allocate Top-Of-Memory and Control Register */ +#define AMD_WT_ALLOC_TME 0x40000 /* top-of-memory enable */ +#define AMD_WT_ALLOC_PRE 0x20000 /* programmable range enable */ +#define AMD_WT_ALLOC_FRE 0x10000 /* fixed (A0000-FFFFF) range enable */ + +/* AMD64 MSR's */ +#define MSR_EFER 0xc0000080 /* extended features */ +#define MSR_STAR 0xc0000081 /* legacy mode SYSCALL target/cs/ss */ +#define MSR_LSTAR 0xc0000082 /* long mode SYSCALL target rip */ +#define MSR_CSTAR 0xc0000083 /* compat mode SYSCALL target rip */ +#define MSR_SF_MASK 0xc0000084 /* syscall flags mask */ +#define MSR_FSBASE 0xc0000100 /* base address of the %fs "segment" */ +#define MSR_GSBASE 0xc0000101 /* base address of the %gs "segment" */ +#define MSR_KGSBASE 0xc0000102 /* base address of the kernel %gs */ +#define MSR_PERFEVSEL0 0xc0010000 +#define MSR_PERFEVSEL1 0xc0010001 +#define MSR_PERFEVSEL2 0xc0010002 +#define MSR_PERFEVSEL3 0xc0010003 +#define MSR_K7_PERFCTR0 0xc0010004 +#define MSR_K7_PERFCTR1 0xc0010005 +#define MSR_K7_PERFCTR2 0xc0010006 +#define MSR_K7_PERFCTR3 0xc0010007 +#define MSR_SYSCFG 0xc0010010 +#define MSR_HWCR 0xc0010015 +#define MSR_IORRBASE0 0xc0010016 +#define MSR_IORRMASK0 0xc0010017 +#define MSR_IORRBASE1 0xc0010018 +#define MSR_IORRMASK1 0xc0010019 +#define MSR_TOP_MEM 0xc001001a /* boundary for ram below 4G */ +#define MSR_TOP_MEM2 0xc001001d /* boundary for ram above 4G */ +#define MSR_NB_CFG1 0xc001001f /* NB configuration 1 */ +#define MSR_P_STATE_LIMIT 0xc0010061 /* P-state Current Limit Register */ +#define MSR_P_STATE_CONTROL 0xc0010062 /* P-state Control Register */ +#define MSR_P_STATE_STATUS 0xc0010063 /* P-state Status Register */ +#define MSR_P_STATE_CONFIG(n) (0xc0010064 + (n)) /* P-state Config */ +#define MSR_SMM_ADDR 0xc0010112 /* SMM TSEG base address */ +#define MSR_SMM_MASK 0xc0010113 /* SMM TSEG address mask */ +#define MSR_IC_CFG 0xc0011021 /* Instruction Cache Configuration */ +#define MSR_K8_UCODE_UPDATE 0xc0010020 /* update microcode */ +#define MSR_MC0_CTL_MASK 0xc0010044 +#define MSR_VM_CR 0xc0010114 /* SVM: feature control */ +#define MSR_VM_HSAVE_PA 0xc0010117 /* SVM: host save area address */ + +/* MSR_VM_CR related */ +#define VM_CR_SVMDIS 0x10 /* SVM: disabled by BIOS */ + +/* VIA ACE crypto featureset: for via_feature_rng */ +#define VIA_HAS_RNG 1 /* cpu has RNG */ + +/* VIA ACE crypto featureset: for via_feature_xcrypt */ +#define VIA_HAS_AES 1 /* cpu has AES */ +#define VIA_HAS_SHA 2 /* cpu has SHA1 & SHA256 */ +#define VIA_HAS_MM 4 /* cpu has RSA instructions */ +#define VIA_HAS_AESCTR 8 /* cpu has AES-CTR instructions */ + +/* Centaur Extended Feature flags */ +#define VIA_CPUID_HAS_RNG 0x000004 +#define VIA_CPUID_DO_RNG 0x000008 +#define VIA_CPUID_HAS_ACE 0x000040 +#define VIA_CPUID_DO_ACE 0x000080 +#define VIA_CPUID_HAS_ACE2 0x000100 +#define VIA_CPUID_DO_ACE2 0x000200 +#define VIA_CPUID_HAS_PHE 0x000400 +#define VIA_CPUID_DO_PHE 0x000800 +#define VIA_CPUID_HAS_PMM 0x001000 +#define VIA_CPUID_DO_PMM 0x002000 + +/* VIA ACE xcrypt-* instruction context control options */ +#define VIA_CRYPT_CWLO_ROUND_M 0x0000000f +#define VIA_CRYPT_CWLO_ALG_M 0x00000070 +#define VIA_CRYPT_CWLO_ALG_AES 0x00000000 +#define VIA_CRYPT_CWLO_KEYGEN_M 0x00000080 +#define VIA_CRYPT_CWLO_KEYGEN_HW 0x00000000 +#define VIA_CRYPT_CWLO_KEYGEN_SW 0x00000080 +#define VIA_CRYPT_CWLO_NORMAL 0x00000000 +#define VIA_CRYPT_CWLO_INTERMEDIATE 0x00000100 +#define VIA_CRYPT_CWLO_ENCRYPT 0x00000000 +#define VIA_CRYPT_CWLO_DECRYPT 0x00000200 +#define VIA_CRYPT_CWLO_KEY128 0x0000000a /* 128bit, 10 rds */ +#define VIA_CRYPT_CWLO_KEY192 0x0000040c /* 192bit, 12 rds */ +#define VIA_CRYPT_CWLO_KEY256 0x0000080e /* 256bit, 15 rds */ diff --git a/include/xhyve/support/timerreg.h b/include/xhyve/support/timerreg.h new file mode 100644 index 0000000..4cb33b5 --- /dev/null +++ b/include/xhyve/support/timerreg.h @@ -0,0 +1,47 @@ +/*- + * Copyright (C) 2005 TAKAHASHI Yoshihiro. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* + * The outputs of the three timers are connected as follows: + * + * timer 0 -> irq 0 + * timer 1 -> dma chan 0 (for dram refresh) + * timer 2 -> speaker (via keyboard controller) + * + * Timer 0 is used to call hardclock. + * Timer 2 is used to generate console beeps. + */ + +#pragma once + +#include + +#define IO_TIMER1 0x40 /* 8253 Timer #1 */ +#define TIMER_CNTR0 (IO_TIMER1 + TIMER_REG_CNTR0) +#define TIMER_CNTR1 (IO_TIMER1 + TIMER_REG_CNTR1) +#define TIMER_CNTR2 (IO_TIMER1 + TIMER_REG_CNTR2) +#define TIMER_MODE (IO_TIMER1 + TIMER_REG_MODE) diff --git a/include/xhyve/support/tree.h b/include/xhyve/support/tree.h new file mode 100644 index 0000000..9c372cc --- /dev/null +++ b/include/xhyve/support/tree.h @@ -0,0 +1,751 @@ +/* $NetBSD: tree.h,v 1.8 2004/03/28 19:38:30 provos Exp $ */ +/* $OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $ */ +/* $FreeBSD: src/sys/sys/tree.h,v 1.7 2007/12/28 07:03:26 jasone Exp $ */ + +/*- + * Copyright 2002 Niels Provos + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" + +/* + * This file defines data structures for different types of trees: + * splay trees and red-black trees. + * + * A splay tree is a self-organizing data structure. Every operation + * on the tree causes a splay to happen. The splay moves the requested + * node to the root of the tree and partly rebalances it. + * + * This has the benefit that request locality causes faster lookups as + * the requested nodes move to the top of the tree. On the other hand, + * every lookup causes memory writes. + * + * The Balance Theorem bounds the total access time for m operations + * and n inserts on an initially empty tree as O((m + n)lg n). The + * amortized cost for a sequence of m accesses to a splay tree is O(lg n); + * + * A red-black tree is a binary search tree with the node color as an + * extra attribute. It fulfills a set of conditions: + * - every search path from the root to a leaf consists of the + * same number of black nodes, + * - each red node (except for the root) has a black parent, + * - each leaf node is black. + * + * Every operation on a red-black tree is bounded as O(lg n). + * The maximum height of a red-black tree is 2lg (n+1). + */ + +#define SPLAY_HEAD(name, type) \ +struct name { \ + struct type *sph_root; /* root of the tree */ \ +} + +#define SPLAY_INITIALIZER(root) \ + { NULL } + +#define SPLAY_INIT(root) do { \ + (root)->sph_root = NULL; \ +} while (/*CONSTCOND*/ 0) + +#define SPLAY_ENTRY(type) \ +struct { \ + struct type *spe_left; /* left element */ \ + struct type *spe_right; /* right element */ \ +} + +#define SPLAY_LEFT(elm, field) (elm)->field.spe_left +#define SPLAY_RIGHT(elm, field) (elm)->field.spe_right +#define SPLAY_ROOT(head) (head)->sph_root +#define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL) + +/* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */ +#define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \ + SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \ + SPLAY_RIGHT(tmp, field) = (head)->sph_root; \ + (head)->sph_root = tmp; \ +} while (/*CONSTCOND*/ 0) + +#define SPLAY_ROTATE_LEFT(head, tmp, field) do { \ + SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \ + SPLAY_LEFT(tmp, field) = (head)->sph_root; \ + (head)->sph_root = tmp; \ +} while (/*CONSTCOND*/ 0) + +#define SPLAY_LINKLEFT(head, tmp, field) do { \ + SPLAY_LEFT(tmp, field) = (head)->sph_root; \ + tmp = (head)->sph_root; \ + (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \ +} while (/*CONSTCOND*/ 0) + +#define SPLAY_LINKRIGHT(head, tmp, field) do { \ + SPLAY_RIGHT(tmp, field) = (head)->sph_root; \ + tmp = (head)->sph_root; \ + (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \ +} while (/*CONSTCOND*/ 0) + +#define SPLAY_ASSEMBLE(head, node, left, right, field) do { \ + SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \ + SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\ + SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \ + SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \ +} while (/*CONSTCOND*/ 0) + +/* Generates prototypes and inline functions */ + +#define SPLAY_PROTOTYPE(name, type, field, cmp) \ +void name##_SPLAY(struct name *, struct type *); \ +void name##_SPLAY_MINMAX(struct name *, int); \ +struct type *name##_SPLAY_INSERT(struct name *, struct type *); \ +struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \ + \ +/* Finds the node with the same key as elm */ \ +static __inline struct type * \ +name##_SPLAY_FIND(struct name *head, struct type *elm) \ +{ \ + if (SPLAY_EMPTY(head)) \ + return(NULL); \ + name##_SPLAY(head, elm); \ + if ((cmp)(elm, (head)->sph_root) == 0) \ + return (head->sph_root); \ + return (NULL); \ +} \ + \ +static __inline struct type * \ +name##_SPLAY_NEXT(struct name *head, struct type *elm) \ +{ \ + name##_SPLAY(head, elm); \ + if (SPLAY_RIGHT(elm, field) != NULL) { \ + elm = SPLAY_RIGHT(elm, field); \ + while (SPLAY_LEFT(elm, field) != NULL) { \ + elm = SPLAY_LEFT(elm, field); \ + } \ + } else \ + elm = NULL; \ + return (elm); \ +} \ + \ +static __inline struct type * \ +name##_SPLAY_MIN_MAX(struct name *head, int val) \ +{ \ + name##_SPLAY_MINMAX(head, val); \ + return (SPLAY_ROOT(head)); \ +} + +/* Main splay operation. + * Moves node close to the key of elm to top + */ +#define SPLAY_GENERATE(name, type, field, cmp) \ +struct type * \ +name##_SPLAY_INSERT(struct name *head, struct type *elm) \ +{ \ + if (SPLAY_EMPTY(head)) { \ + SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \ + } else { \ + int __comp; \ + name##_SPLAY(head, elm); \ + __comp = (cmp)(elm, (head)->sph_root); \ + if(__comp < 0) { \ + SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\ + SPLAY_RIGHT(elm, field) = (head)->sph_root; \ + SPLAY_LEFT((head)->sph_root, field) = NULL; \ + } else if (__comp > 0) { \ + SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\ + SPLAY_LEFT(elm, field) = (head)->sph_root; \ + SPLAY_RIGHT((head)->sph_root, field) = NULL; \ + } else \ + return ((head)->sph_root); \ + } \ + (head)->sph_root = (elm); \ + return (NULL); \ +} \ + \ +struct type * \ +name##_SPLAY_REMOVE(struct name *head, struct type *elm) \ +{ \ + struct type *__tmp; \ + if (SPLAY_EMPTY(head)) \ + return (NULL); \ + name##_SPLAY(head, elm); \ + if ((cmp)(elm, (head)->sph_root) == 0) { \ + if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \ + (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\ + } else { \ + __tmp = SPLAY_RIGHT((head)->sph_root, field); \ + (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\ + name##_SPLAY(head, elm); \ + SPLAY_RIGHT((head)->sph_root, field) = __tmp; \ + } \ + return (elm); \ + } \ + return (NULL); \ +} \ + \ +void \ +name##_SPLAY(struct name *head, struct type *elm) \ +{ \ + struct type __node, *__left, *__right, *__tmp; \ + int __comp; \ +\ + SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\ + __left = __right = &__node; \ +\ + while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) { \ + if (__comp < 0) { \ + __tmp = SPLAY_LEFT((head)->sph_root, field); \ + if (__tmp == NULL) \ + break; \ + if ((cmp)(elm, __tmp) < 0){ \ + SPLAY_ROTATE_RIGHT(head, __tmp, field); \ + if (SPLAY_LEFT((head)->sph_root, field) == NULL)\ + break; \ + } \ + SPLAY_LINKLEFT(head, __right, field); \ + } else if (__comp > 0) { \ + __tmp = SPLAY_RIGHT((head)->sph_root, field); \ + if (__tmp == NULL) \ + break; \ + if ((cmp)(elm, __tmp) > 0){ \ + SPLAY_ROTATE_LEFT(head, __tmp, field); \ + if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\ + break; \ + } \ + SPLAY_LINKRIGHT(head, __left, field); \ + } \ + } \ + SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \ +} \ + \ +/* Splay with either the minimum or the maximum element \ + * Used to find minimum or maximum element in tree. \ + */ \ +void name##_SPLAY_MINMAX(struct name *head, int __comp) \ +{ \ + struct type __node, *__left, *__right, *__tmp; \ +\ + SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\ + __left = __right = &__node; \ +\ + while (1) { \ + if (__comp < 0) { \ + __tmp = SPLAY_LEFT((head)->sph_root, field); \ + if (__tmp == NULL) \ + break; \ + if (__comp < 0){ \ + SPLAY_ROTATE_RIGHT(head, __tmp, field); \ + if (SPLAY_LEFT((head)->sph_root, field) == NULL)\ + break; \ + } \ + SPLAY_LINKLEFT(head, __right, field); \ + } else if (__comp > 0) { \ + __tmp = SPLAY_RIGHT((head)->sph_root, field); \ + if (__tmp == NULL) \ + break; \ + if (__comp > 0) { \ + SPLAY_ROTATE_LEFT(head, __tmp, field); \ + if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\ + break; \ + } \ + SPLAY_LINKRIGHT(head, __left, field); \ + } \ + } \ + SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \ +} + +#define SPLAY_NEGINF -1 +#define SPLAY_INF 1 + +#define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y) +#define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y) +#define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y) +#define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y) +#define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \ + : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF)) +#define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \ + : name##_SPLAY_MIN_MAX(x, SPLAY_INF)) + +#define SPLAY_FOREACH(x, name, head) \ + for ((x) = SPLAY_MIN(name, head); \ + (x) != NULL; \ + (x) = SPLAY_NEXT(name, head, x)) + +/* Macros that define a red-black tree */ +#define RB_HEAD(name, type) \ +struct name { \ + struct type *rbh_root; /* root of the tree */ \ +} + +#define RB_INITIALIZER(root) \ + { NULL } + +#define RB_INIT(root) do { \ + (root)->rbh_root = NULL; \ +} while (/*CONSTCOND*/ 0) + +/* + * Undef for Linux + */ +#undef RB_BLACK +#undef RB_RED +#undef RB_ROOT + +#define RB_BLACK 0 +#define RB_RED 1 +#define RB_ENTRY(type) \ +struct { \ + struct type *rbe_left; /* left element */ \ + struct type *rbe_right; /* right element */ \ + struct type *rbe_parent; /* parent element */ \ + int rbe_color; /* node color */ \ +} + +#define RB_LEFT(elm, field) (elm)->field.rbe_left +#define RB_RIGHT(elm, field) (elm)->field.rbe_right +#define RB_PARENT(elm, field) (elm)->field.rbe_parent +#define RB_COLOR(elm, field) (elm)->field.rbe_color +#define RB_ROOT(head) (head)->rbh_root +#define RB_EMPTY(head) (RB_ROOT(head) == NULL) + +#define RB_SET(elm, parent, field) do { \ + RB_PARENT(elm, field) = parent; \ + RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \ + RB_COLOR(elm, field) = RB_RED; \ +} while (/*CONSTCOND*/ 0) + +#define RB_SET_BLACKRED(black, red, field) do { \ + RB_COLOR(black, field) = RB_BLACK; \ + RB_COLOR(red, field) = RB_RED; \ +} while (/*CONSTCOND*/ 0) + +#ifndef RB_AUGMENT +#define RB_AUGMENT(x) do {} while (0) +#endif + +#define RB_ROTATE_LEFT(head, elm, tmp, field) do { \ + (tmp) = RB_RIGHT(elm, field); \ + if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \ + RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \ + } \ + RB_AUGMENT(elm); \ + if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \ + if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ + RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ + else \ + RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ + } else \ + (head)->rbh_root = (tmp); \ + RB_LEFT(tmp, field) = (elm); \ + RB_PARENT(elm, field) = (tmp); \ + RB_AUGMENT(tmp); \ + if ((RB_PARENT(tmp, field))) \ + RB_AUGMENT(RB_PARENT(tmp, field)); \ +} while (/*CONSTCOND*/ 0) + +#define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \ + (tmp) = RB_LEFT(elm, field); \ + if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \ + RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \ + } \ + RB_AUGMENT(elm); \ + if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \ + if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ + RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ + else \ + RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ + } else \ + (head)->rbh_root = (tmp); \ + RB_RIGHT(tmp, field) = (elm); \ + RB_PARENT(elm, field) = (tmp); \ + RB_AUGMENT(tmp); \ + if ((RB_PARENT(tmp, field))) \ + RB_AUGMENT(RB_PARENT(tmp, field)); \ +} while (/*CONSTCOND*/ 0) + +/* Generates prototypes and inline functions */ +#define RB_PROTOTYPE(name, type, field, cmp) \ + RB_PROTOTYPE_INTERNAL(name, type, field, cmp,) +#define RB_PROTOTYPE_STATIC(name, type, field, cmp) \ + RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __unused static) +#define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \ +attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \ +attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *); \ +attr struct type *name##_RB_REMOVE(struct name *, struct type *); \ +attr struct type *name##_RB_INSERT(struct name *, struct type *); \ +attr struct type *name##_RB_FIND(struct name *, struct type *); \ +attr struct type *name##_RB_NFIND(struct name *, struct type *); \ +attr struct type *name##_RB_NEXT(struct type *); \ +attr struct type *name##_RB_PREV(struct type *); \ +attr struct type *name##_RB_MINMAX(struct name *, int) \ + +/* Main rb operation. + * Moves node close to the key of elm to top + */ +#define RB_GENERATE(name, type, field, cmp) \ + RB_GENERATE_INTERNAL(name, type, field, cmp,) +#define RB_GENERATE_STATIC(name, type, field, cmp) \ + RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static) +#define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \ +attr void \ +name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ +{ \ + struct type *parent, *gparent, *tmp; \ + while ((parent = RB_PARENT(elm, field)) != NULL && \ + RB_COLOR(parent, field) == RB_RED) { \ + gparent = RB_PARENT(parent, field); \ + if (parent == RB_LEFT(gparent, field)) { \ + tmp = RB_RIGHT(gparent, field); \ + if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ + RB_COLOR(tmp, field) = RB_BLACK; \ + RB_SET_BLACKRED(parent, gparent, field);\ + elm = gparent; \ + continue; \ + } \ + if (RB_RIGHT(parent, field) == elm) { \ + RB_ROTATE_LEFT(head, parent, tmp, field);\ + tmp = parent; \ + parent = elm; \ + elm = tmp; \ + } \ + RB_SET_BLACKRED(parent, gparent, field); \ + RB_ROTATE_RIGHT(head, gparent, tmp, field); \ + } else { \ + tmp = RB_LEFT(gparent, field); \ + if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ + RB_COLOR(tmp, field) = RB_BLACK; \ + RB_SET_BLACKRED(parent, gparent, field);\ + elm = gparent; \ + continue; \ + } \ + if (RB_LEFT(parent, field) == elm) { \ + RB_ROTATE_RIGHT(head, parent, tmp, field);\ + tmp = parent; \ + parent = elm; \ + elm = tmp; \ + } \ + RB_SET_BLACKRED(parent, gparent, field); \ + RB_ROTATE_LEFT(head, gparent, tmp, field); \ + } \ + } \ + RB_COLOR(head->rbh_root, field) = RB_BLACK; \ +} \ + \ +attr void \ +name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \ +{ \ + struct type *tmp; \ + while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \ + elm != RB_ROOT(head)) { \ + if (RB_LEFT(parent, field) == elm) { \ + tmp = RB_RIGHT(parent, field); \ + if (RB_COLOR(tmp, field) == RB_RED) { \ + RB_SET_BLACKRED(tmp, parent, field); \ + RB_ROTATE_LEFT(head, parent, tmp, field);\ + tmp = RB_RIGHT(parent, field); \ + } \ + if ((RB_LEFT(tmp, field) == NULL || \ + RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\ + (RB_RIGHT(tmp, field) == NULL || \ + RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\ + RB_COLOR(tmp, field) = RB_RED; \ + elm = parent; \ + parent = RB_PARENT(elm, field); \ + } else { \ + if (RB_RIGHT(tmp, field) == NULL || \ + RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\ + struct type *oleft; \ + if ((oleft = RB_LEFT(tmp, field)) \ + != NULL) \ + RB_COLOR(oleft, field) = RB_BLACK;\ + RB_COLOR(tmp, field) = RB_RED; \ + RB_ROTATE_RIGHT(head, tmp, oleft, field);\ + tmp = RB_RIGHT(parent, field); \ + } \ + RB_COLOR(tmp, field) = RB_COLOR(parent, field);\ + RB_COLOR(parent, field) = RB_BLACK; \ + if (RB_RIGHT(tmp, field)) \ + RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\ + RB_ROTATE_LEFT(head, parent, tmp, field);\ + elm = RB_ROOT(head); \ + break; \ + } \ + } else { \ + tmp = RB_LEFT(parent, field); \ + if (RB_COLOR(tmp, field) == RB_RED) { \ + RB_SET_BLACKRED(tmp, parent, field); \ + RB_ROTATE_RIGHT(head, parent, tmp, field);\ + tmp = RB_LEFT(parent, field); \ + } \ + if ((RB_LEFT(tmp, field) == NULL || \ + RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\ + (RB_RIGHT(tmp, field) == NULL || \ + RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\ + RB_COLOR(tmp, field) = RB_RED; \ + elm = parent; \ + parent = RB_PARENT(elm, field); \ + } else { \ + if (RB_LEFT(tmp, field) == NULL || \ + RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\ + struct type *oright; \ + if ((oright = RB_RIGHT(tmp, field)) \ + != NULL) \ + RB_COLOR(oright, field) = RB_BLACK;\ + RB_COLOR(tmp, field) = RB_RED; \ + RB_ROTATE_LEFT(head, tmp, oright, field);\ + tmp = RB_LEFT(parent, field); \ + } \ + RB_COLOR(tmp, field) = RB_COLOR(parent, field);\ + RB_COLOR(parent, field) = RB_BLACK; \ + if (RB_LEFT(tmp, field)) \ + RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\ + RB_ROTATE_RIGHT(head, parent, tmp, field);\ + elm = RB_ROOT(head); \ + break; \ + } \ + } \ + } \ + if (elm) \ + RB_COLOR(elm, field) = RB_BLACK; \ +} \ + \ +attr struct type * \ +name##_RB_REMOVE(struct name *head, struct type *elm) \ +{ \ + struct type *child, *parent, *old = elm; \ + int color; \ + if (RB_LEFT(elm, field) == NULL) \ + child = RB_RIGHT(elm, field); \ + else if (RB_RIGHT(elm, field) == NULL) \ + child = RB_LEFT(elm, field); \ + else { \ + struct type *left; \ + elm = RB_RIGHT(elm, field); \ + while ((left = RB_LEFT(elm, field)) != NULL) \ + elm = left; \ + child = RB_RIGHT(elm, field); \ + parent = RB_PARENT(elm, field); \ + color = RB_COLOR(elm, field); \ + if (child) \ + RB_PARENT(child, field) = parent; \ + if (parent) { \ + if (RB_LEFT(parent, field) == elm) \ + RB_LEFT(parent, field) = child; \ + else \ + RB_RIGHT(parent, field) = child; \ + RB_AUGMENT(parent); \ + } else \ + RB_ROOT(head) = child; \ + if (RB_PARENT(elm, field) == old) \ + parent = elm; \ + (elm)->field = (old)->field; \ + if (RB_PARENT(old, field)) { \ + if (RB_LEFT(RB_PARENT(old, field), field) == old)\ + RB_LEFT(RB_PARENT(old, field), field) = elm;\ + else \ + RB_RIGHT(RB_PARENT(old, field), field) = elm;\ + RB_AUGMENT(RB_PARENT(old, field)); \ + } else \ + RB_ROOT(head) = elm; \ + RB_PARENT(RB_LEFT(old, field), field) = elm; \ + if (RB_RIGHT(old, field)) \ + RB_PARENT(RB_RIGHT(old, field), field) = elm; \ + if (parent) { \ + left = parent; \ + do { \ + RB_AUGMENT(left); \ + } while ((left = RB_PARENT(left, field)) != NULL); \ + } \ + goto color; \ + } \ + parent = RB_PARENT(elm, field); \ + color = RB_COLOR(elm, field); \ + if (child) \ + RB_PARENT(child, field) = parent; \ + if (parent) { \ + if (RB_LEFT(parent, field) == elm) \ + RB_LEFT(parent, field) = child; \ + else \ + RB_RIGHT(parent, field) = child; \ + RB_AUGMENT(parent); \ + } else \ + RB_ROOT(head) = child; \ +color: \ + if (color == RB_BLACK) \ + name##_RB_REMOVE_COLOR(head, parent, child); \ + return (old); \ +} \ + \ +/* Inserts a node into the RB tree */ \ +attr struct type * \ +name##_RB_INSERT(struct name *head, struct type *elm) \ +{ \ + struct type *tmp; \ + struct type *parent = NULL; \ + int comp = 0; \ + tmp = RB_ROOT(head); \ + while (tmp) { \ + parent = tmp; \ + comp = (cmp)(elm, parent); \ + if (comp < 0) \ + tmp = RB_LEFT(tmp, field); \ + else if (comp > 0) \ + tmp = RB_RIGHT(tmp, field); \ + else \ + return (tmp); \ + } \ + RB_SET(elm, parent, field); \ + if (parent != NULL) { \ + if (comp < 0) \ + RB_LEFT(parent, field) = elm; \ + else \ + RB_RIGHT(parent, field) = elm; \ + RB_AUGMENT(parent); \ + } else \ + RB_ROOT(head) = elm; \ + name##_RB_INSERT_COLOR(head, elm); \ + return (NULL); \ +} \ + \ +/* Finds the node with the same key as elm */ \ +attr struct type * \ +name##_RB_FIND(struct name *head, struct type *elm) \ +{ \ + struct type *tmp = RB_ROOT(head); \ + int comp; \ + while (tmp) { \ + comp = cmp(elm, tmp); \ + if (comp < 0) \ + tmp = RB_LEFT(tmp, field); \ + else if (comp > 0) \ + tmp = RB_RIGHT(tmp, field); \ + else \ + return (tmp); \ + } \ + return (NULL); \ +} \ + \ +/* Finds the first node greater than or equal to the search key */ \ +attr struct type * \ +name##_RB_NFIND(struct name *head, struct type *elm) \ +{ \ + struct type *tmp = RB_ROOT(head); \ + struct type *res = NULL; \ + int comp; \ + while (tmp) { \ + comp = cmp(elm, tmp); \ + if (comp < 0) { \ + res = tmp; \ + tmp = RB_LEFT(tmp, field); \ + } \ + else if (comp > 0) \ + tmp = RB_RIGHT(tmp, field); \ + else \ + return (tmp); \ + } \ + return (res); \ +} \ + \ +/* ARGSUSED */ \ +attr struct type * \ +name##_RB_NEXT(struct type *elm) \ +{ \ + if (RB_RIGHT(elm, field)) { \ + elm = RB_RIGHT(elm, field); \ + while (RB_LEFT(elm, field)) \ + elm = RB_LEFT(elm, field); \ + } else { \ + if (RB_PARENT(elm, field) && \ + (elm == RB_LEFT(RB_PARENT(elm, field), field))) \ + elm = RB_PARENT(elm, field); \ + else { \ + while (RB_PARENT(elm, field) && \ + (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\ + elm = RB_PARENT(elm, field); \ + elm = RB_PARENT(elm, field); \ + } \ + } \ + return (elm); \ +} \ + \ +/* ARGSUSED */ \ +attr struct type * \ +name##_RB_PREV(struct type *elm) \ +{ \ + if (RB_LEFT(elm, field)) { \ + elm = RB_LEFT(elm, field); \ + while (RB_RIGHT(elm, field)) \ + elm = RB_RIGHT(elm, field); \ + } else { \ + if (RB_PARENT(elm, field) && \ + (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \ + elm = RB_PARENT(elm, field); \ + else { \ + while (RB_PARENT(elm, field) && \ + (elm == RB_LEFT(RB_PARENT(elm, field), field)))\ + elm = RB_PARENT(elm, field); \ + elm = RB_PARENT(elm, field); \ + } \ + } \ + return (elm); \ +} \ + \ +attr struct type * \ +name##_RB_MINMAX(struct name *head, int val) \ +{ \ + struct type *tmp = RB_ROOT(head); \ + struct type *parent = NULL; \ + while (tmp) { \ + parent = tmp; \ + if (val < 0) \ + tmp = RB_LEFT(tmp, field); \ + else \ + tmp = RB_RIGHT(tmp, field); \ + } \ + return (parent); \ +} + +#define RB_NEGINF -1 +#define RB_INF 1 + +#define RB_INSERT(name, x, y) name##_RB_INSERT(x, y) +#define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y) +#define RB_FIND(name, x, y) name##_RB_FIND(x, y) +#define RB_NFIND(name, x, y) name##_RB_NFIND(x, y) +#define RB_NEXT(name, x, y) name##_RB_NEXT(y) +#define RB_PREV(name, x, y) name##_RB_PREV(y) +#define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF) +#define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF) + +#define RB_FOREACH(x, name, head) \ + for ((x) = RB_MIN(name, head); \ + (x) != NULL; \ + (x) = name##_RB_NEXT(x)) + +#define RB_FOREACH_REVERSE(x, name, head) \ + for ((x) = RB_MAX(name, head); \ + (x) != NULL; \ + (x) = name##_RB_PREV(x)) + +#pragma clang diagnostic pop diff --git a/include/xhyve/support/uuid.h b/include/xhyve/support/uuid.h new file mode 100644 index 0000000..009c617 --- /dev/null +++ b/include/xhyve/support/uuid.h @@ -0,0 +1,153 @@ +/*- + * Copyright (c) 2002,2005 Marcel Moolenaar + * Copyright (c) 2002 Hiten Mahesh Pandya + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#pragma once + +#include +#include +#include + +#define _UUID_NODE_LEN 6 + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +struct uuid { + uint32_t time_low; + uint16_t time_mid; + uint16_t time_hi_and_version; + uint8_t clock_seq_hi_and_reserved; + uint8_t clock_seq_low; + uint8_t node[_UUID_NODE_LEN]; +}; +#pragma clang diagnostic pop + +typedef struct uuid uuid_internal_t; + +/* + * This implementation mostly conforms to the DCE 1.1 specification. + * See Also: + * uuidgen(1), uuidgen(2), uuid(3) + */ + +/* Status codes returned by the functions. */ +#define uuid_s_ok 0 +#define uuid_s_bad_version 1 +#define uuid_s_invalid_string_uuid 2 +#define uuid_s_no_memory 3 + +/* + * uuid_create_nil() - create a nil UUID. + * See also: + * http://www.opengroup.org/onlinepubs/009629399/uuid_create_nil.htm + */ +static inline void +uuid_create_nil(uuid_t *u, uint32_t *status) +{ + if (status) + *status = uuid_s_ok; + + bzero(u, sizeof(*u)); +} + +static void +uuid_enc_le(void *buf, const uuid_t *uuid) +{ + uuid_internal_t *u = (uuid_internal_t *) ((void *) uuid); + uint8_t *p = buf; + int i; + + memcpy(p, &u->time_low, 4); + memcpy(p, &u->time_mid, 2); + memcpy(p, &u->time_hi_and_version, 2); + p[8] = u->clock_seq_hi_and_reserved; + p[9] = u->clock_seq_low; + for (i = 0; i < _UUID_NODE_LEN; i++) + p[10 + i] = u->node[i]; +} + +/* + * uuid_from_string() - convert a string representation of an UUID into + * a binary representation. + * See also: + * http://www.opengroup.org/onlinepubs/009629399/uuid_from_string.htm + * + * NOTE: The sequence field is in big-endian, while the time fields are in + * native byte order. + */ +static inline void +uuid_from_string(const char *s, uuid_t *uuid, uint32_t *status) +{ + uuid_internal_t *u = (uuid_internal_t *) ((void *) uuid); + int n; + + /* Short-circuit 2 special cases: NULL pointer and empty string. */ + if (s == NULL || *s == '\0') { + uuid_create_nil(((uuid_t *) u), status); + return; + } + + /* Assume the worst. */ + if (status != NULL) + *status = uuid_s_invalid_string_uuid; + + /* The UUID string representation has a fixed length. */ + if (strlen(s) != 36) + return; + + /* + * We only work with "new" UUIDs. New UUIDs have the form: + * 01234567-89ab-cdef-0123-456789abcdef + * The so called "old" UUIDs, which we don't support, have the form: + * 0123456789ab.cd.ef.01.23.45.67.89.ab + */ + if (s[8] != '-') + return; + + n = sscanf(s, + "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx", + &u->time_low, &u->time_mid, &u->time_hi_and_version, + &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0], + &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]); + + /* Make sure we have all conversions. */ + if (n != 11) + return; + + /* We have a successful scan. Check semantics... */ + n = u->clock_seq_hi_and_reserved; + if ((n & 0x80) != 0x00 && /* variant 0? */ + (n & 0xc0) != 0x80 && /* variant 1? */ + (n & 0xe0) != 0xc0) { /* variant 2? */ + if (status != NULL) + *status = uuid_s_bad_version; + } else { + if (status != NULL) + *status = uuid_s_ok; + } +} diff --git a/bhyve/uart_emul.h b/include/xhyve/uart_emul.h similarity index 80% rename from bhyve/uart_emul.h rename to include/xhyve/uart_emul.h index 993b92e..804dfb8 100644 --- a/bhyve/uart_emul.h +++ b/include/xhyve/uart_emul.h @@ -26,20 +26,17 @@ * $FreeBSD$ */ -#ifndef _UART_EMUL_H_ -#define _UART_EMUL_H_ +#pragma once - -#define UART_IO_BAR_SIZE 8 +#define UART_IO_BAR_SIZE 8 struct uart_softc; typedef void (*uart_intr_func_t)(void *arg); struct uart_softc *uart_init(uart_intr_func_t intr_assert, - uart_intr_func_t intr_deassert, void *arg); + uart_intr_func_t intr_deassert, void *arg); -int uart_legacy_alloc(int unit, int *ioaddr, int *irq); -uint8_t uart_read(struct uart_softc *sc, int offset); -void uart_write(struct uart_softc *sc, int offset, uint8_t value); -int uart_set_backend(struct uart_softc *sc, const char *opt); -#endif +int uart_legacy_alloc(int unit, int *ioaddr, int *irq); +uint8_t uart_read(struct uart_softc *sc, int offset); +void uart_write(struct uart_softc *sc, int offset, uint8_t value); +int uart_set_backend(struct uart_softc *sc, const char *opt); diff --git a/bhyve/virtio.h b/include/xhyve/virtio.h similarity index 77% rename from bhyve/virtio.h rename to include/xhyve/virtio.h index 0e96a1d..feb0fbb 100644 --- a/bhyve/virtio.h +++ b/include/xhyve/virtio.h @@ -26,8 +26,10 @@ * $FreeBSD$ */ -#ifndef _VIRTIO_H_ -#define _VIRTIO_H_ +#pragma once + +#include +#include /* * These are derived from several virtio specifications. @@ -125,35 +127,40 @@ #define VRING_DESC_F_WRITE (1 << 1) #define VRING_DESC_F_INDIRECT (1 << 2) -struct virtio_desc { /* AKA vring_desc */ - uint64_t vd_addr; /* guest physical address */ - uint32_t vd_len; /* length of scatter/gather seg */ - uint16_t vd_flags; /* VRING_F_DESC_* */ - uint16_t vd_next; /* next desc if F_NEXT */ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpacked" + +struct virtio_desc { /* AKA vring_desc */ + uint64_t vd_addr; /* guest physical address */ + uint32_t vd_len; /* length of scatter/gather seg */ + uint16_t vd_flags; /* VRING_F_DESC_* */ + uint16_t vd_next; /* next desc if F_NEXT */ } __packed; -struct virtio_used { /* AKA vring_used_elem */ - uint32_t vu_idx; /* head of used descriptor chain */ - uint32_t vu_tlen; /* length written-to */ +struct virtio_used { /* AKA vring_used_elem */ + uint32_t vu_idx; /* head of used descriptor chain */ + uint32_t vu_tlen; /* length written-to */ } __packed; #define VRING_AVAIL_F_NO_INTERRUPT 1 struct vring_avail { - uint16_t va_flags; /* VRING_AVAIL_F_* */ - uint16_t va_idx; /* counts to 65535, then cycles */ - uint16_t va_ring[]; /* size N, reported in QNUM value */ -/* uint16_t va_used_event; -- after N ring entries */ + uint16_t va_flags; /* VRING_AVAIL_F_* */ + uint16_t va_idx; /* counts to 65535, then cycles */ + uint16_t va_ring[]; /* size N, reported in QNUM value */ +/* uint16_t va_used_event; -- after N ring entries */ } __packed; #define VRING_USED_F_NO_NOTIFY 1 struct vring_used { - uint16_t vu_flags; /* VRING_USED_F_* */ - uint16_t vu_idx; /* counts to 65535, then cycles */ - struct virtio_used vu_ring[]; /* size N */ -/* uint16_t vu_avail_event; -- after N ring entries */ + uint16_t vu_flags; /* VRING_USED_F_* */ + uint16_t vu_idx; /* counts to 65535, then cycles */ + struct virtio_used vu_ring[]; /* size N */ +/* uint16_t vu_avail_event; -- after N ring entries */ } __packed; +#pragma clang diagnostic pop + /* * The address of any given virtual queue is determined by a single * Page Frame Number register. The guest writes the PFN into the @@ -269,16 +276,15 @@ vring_size(u_int qsz) /* constant 3 below = va_flags, va_idx, va_used_event */ size = sizeof(struct virtio_desc) * qsz + sizeof(uint16_t) * (3 + qsz); - size = roundup2(size, VRING_ALIGN); + size = roundup2(size, ((size_t) VRING_ALIGN)); /* constant 3 below = vu_flags, vu_idx, vu_avail_event */ size += sizeof(uint16_t) * 3 + sizeof(struct virtio_used) * qsz; - size = roundup2(size, VRING_ALIGN); + size = roundup2(size, ((size_t) VRING_ALIGN)); return (size); } -struct vmctx; struct pci_devinst; struct vqueue_info; @@ -316,45 +322,53 @@ struct vqueue_info; #define VIRTIO_EVENT_IDX 0x02 /* use the event-index values */ #define VIRTIO_BROKED 0x08 /* ??? */ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" + struct virtio_softc { - struct virtio_consts *vs_vc; /* constants (see below) */ - int vs_flags; /* VIRTIO_* flags from above */ - pthread_mutex_t *vs_mtx; /* POSIX mutex, if any */ - struct pci_devinst *vs_pi; /* PCI device instance */ - uint32_t vs_negotiated_caps; /* negotiated capabilities */ - struct vqueue_info *vs_queues; /* one per vc_nvq */ - int vs_curq; /* current queue */ - uint8_t vs_status; /* value from last status write */ - uint8_t vs_isr; /* ISR flags, if not MSI-X */ - uint16_t vs_msix_cfg_idx; /* MSI-X vector for config event */ + struct virtio_consts *vs_vc; /* constants (see below) */ + int vs_flags; /* VIRTIO_* flags from above */ + pthread_mutex_t *vs_mtx; /* POSIX mutex, if any */ + struct pci_devinst *vs_pi; /* PCI device instance */ + uint32_t vs_negotiated_caps; /* negotiated capabilities */ + struct vqueue_info *vs_queues; /* one per vc_nvq */ + int vs_curq; /* current queue */ + uint8_t vs_status; /* value from last status write */ + uint8_t vs_isr; /* ISR flags, if not MSI-X */ + uint16_t vs_msix_cfg_idx; /* MSI-X vector for config event */ }; -#define VS_LOCK(vs) \ -do { \ - if (vs->vs_mtx) \ - pthread_mutex_lock(vs->vs_mtx); \ +#define VS_LOCK(vs) \ +do { \ + if (vs->vs_mtx) \ + pthread_mutex_lock(vs->vs_mtx); \ } while (0) -#define VS_UNLOCK(vs) \ -do { \ - if (vs->vs_mtx) \ - pthread_mutex_unlock(vs->vs_mtx); \ +#define VS_UNLOCK(vs) \ +do { \ + if (vs->vs_mtx) \ + pthread_mutex_unlock(vs->vs_mtx); \ } while (0) struct virtio_consts { - const char *vc_name; /* name of driver (for diagnostics) */ - int vc_nvq; /* number of virtual queues */ - size_t vc_cfgsize; /* size of dev-specific config regs */ - void (*vc_reset)(void *); /* called on virtual device reset */ - void (*vc_qnotify)(void *, struct vqueue_info *); - /* called on QNOTIFY if no VQ notify */ - int (*vc_cfgread)(void *, int, int, uint32_t *); - /* called to read config regs */ - int (*vc_cfgwrite)(void *, int, int, uint32_t); - /* called to write config regs */ - void (*vc_apply_features)(void *, uint64_t); - /* called to apply negotiated features */ - uint64_t vc_hv_caps; /* hypervisor-provided capabilities */ + /* name of driver (for diagnostics) */ + const char *vc_name; + /* number of virtual queues */ + int vc_nvq; + /* size of dev-specific config regs */ + size_t vc_cfgsize; + /* called on virtual device reset */ + void (*vc_reset)(void *); + /* called on QNOTIFY if no VQ notify */ + void (*vc_qnotify)(void *, struct vqueue_info *); + /* called to read config regs */ + int (*vc_cfgread)(void *, int, int, uint32_t *); + /* called to write config regs */ + int (*vc_cfgwrite)(void *, int, int, uint32_t); + /* called to apply negotiated features */ + void (*vc_apply_features)(void *, uint64_t); + /* hypervisor-provided capabilities */ + uint64_t vc_hv_caps; }; /* @@ -377,25 +391,34 @@ struct virtio_consts { #define VQ_ALLOC 0x01 /* set once we have a pfn */ #define VQ_BROKED 0x02 /* ??? */ struct vqueue_info { - uint16_t vq_qsize; /* size of this queue (a power of 2) */ - void (*vq_notify)(void *, struct vqueue_info *); - /* called instead of vc_notify, if not NULL */ - - struct virtio_softc *vq_vs; /* backpointer to softc */ - uint16_t vq_num; /* we're the num'th queue in the softc */ - - uint16_t vq_flags; /* flags (see above) */ - uint16_t vq_last_avail; /* a recent value of vq_avail->va_idx */ - uint16_t vq_save_used; /* saved vq_used->vu_idx; see vq_endchains */ - uint16_t vq_msix_idx; /* MSI-X index, or VIRTIO_MSI_NO_VECTOR */ - - uint32_t vq_pfn; /* PFN of virt queue (not shifted!) */ - - volatile struct virtio_desc *vq_desc; /* descriptor array */ - volatile struct vring_avail *vq_avail; /* the "avail" ring */ - volatile struct vring_used *vq_used; /* the "used" ring */ - + /* size of this queue (a power of 2) */ + uint16_t vq_qsize; + /* called instead of vc_notify, if not NULL */ + void (*vq_notify)(void *, struct vqueue_info *); + /* backpointer to softc */ + struct virtio_softc *vq_vs; + /* we're the num'th queue in the softc */ + uint16_t vq_num; + /* flags (see above) */ + uint16_t vq_flags; + /* a recent value of vq_avail->va_idx */ + uint16_t vq_last_avail; + /* saved vq_used->vu_idx; see vq_endchains */ + uint16_t vq_save_used; + /* MSI-X index, or VIRTIO_MSI_NO_VECTOR */ + uint16_t vq_msix_idx; + /* PFN of virt queue (not shifted!) */ + uint32_t vq_pfn; + /* descriptor array */ + volatile struct virtio_desc *vq_desc; + /* the "avail" ring */ + volatile struct vring_avail *vq_avail; + /* the "used" ring */ + volatile struct vring_used *vq_used; }; + +#pragma clang diagnostic pop + /* as noted above, these are sort of backwards, name-wise */ #define VQ_AVAIL_EVENT_IDX(vq) \ (*(volatile uint16_t *)&(vq)->vq_used->vu_ring[(vq)->vq_qsize]) @@ -408,7 +431,6 @@ struct vqueue_info { static inline int vq_ring_ready(struct vqueue_info *vq) { - return (vq->vq_flags & VQ_ALLOC); } @@ -419,7 +441,6 @@ vq_ring_ready(struct vqueue_info *vq) static inline int vq_has_descs(struct vqueue_info *vq) { - return (vq_ring_ready(vq) && vq->vq_last_avail != vq->vq_avail->va_idx); } @@ -431,7 +452,6 @@ vq_has_descs(struct vqueue_info *vq) static inline void vq_interrupt(struct virtio_softc *vs, struct vqueue_info *vq) { - if (pci_msix_enabled(vs->vs_pi)) pci_generate_msix(vs->vs_pi, vq->vq_msix_idx); else { @@ -444,21 +464,17 @@ vq_interrupt(struct virtio_softc *vs, struct vqueue_info *vq) } struct iovec; -void vi_softc_linkup(struct virtio_softc *vs, struct virtio_consts *vc, - void *dev_softc, struct pci_devinst *pi, - struct vqueue_info *queues); -int vi_intr_init(struct virtio_softc *vs, int barnum, int use_msix); -void vi_reset_dev(struct virtio_softc *); -void vi_set_io_bar(struct virtio_softc *, int); - -int vq_getchain(struct vqueue_info *vq, uint16_t *pidx, - struct iovec *iov, int n_iov, uint16_t *flags); -void vq_retchain(struct vqueue_info *vq); -void vq_relchain(struct vqueue_info *vq, uint16_t idx, uint32_t iolen); -void vq_endchains(struct vqueue_info *vq, int used_all_avail); - -uint64_t vi_pci_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, - int baridx, uint64_t offset, int size); -void vi_pci_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, - int baridx, uint64_t offset, int size, uint64_t value); -#endif /* _VIRTIO_H_ */ +void vi_softc_linkup(struct virtio_softc *vs, struct virtio_consts *vc, + void *dev_softc, struct pci_devinst *pi, struct vqueue_info *queues); +int vi_intr_init(struct virtio_softc *vs, int barnum, int use_msix); +void vi_reset_dev(struct virtio_softc *); +void vi_set_io_bar(struct virtio_softc *, int); +int vq_getchain(struct vqueue_info *vq, uint16_t *pidx, struct iovec *iov, + int n_iov, uint16_t *flags); +void vq_retchain(struct vqueue_info *vq); +void vq_relchain(struct vqueue_info *vq, uint16_t idx, uint32_t iolen); +void vq_endchains(struct vqueue_info *vq, int used_all_avail); +uint64_t vi_pci_read(int vcpu, struct pci_devinst *pi, int baridx, + uint64_t offset, int size); +void vi_pci_write(int vcpu, struct pci_devinst *pi, int baridx, uint64_t offset, + int size, uint64_t value); diff --git a/vmm/intel/vmcs.h b/include/xhyve/vmm/intel/vmcs.h similarity index 80% rename from vmm/intel/vmcs.h rename to include/xhyve/vmm/intel/vmcs.h index 6d78a69..4ce70e2 100644 --- a/vmm/intel/vmcs.h +++ b/include/xhyve/vmm/intel/vmcs.h @@ -26,75 +26,62 @@ * $FreeBSD$ */ -#ifndef _VMCS_H_ -#define _VMCS_H_ +#pragma once -#ifdef _KERNEL -struct vmcs { - uint32_t identifier; - uint32_t abort_code; - char _impl_specific[PAGE_SIZE - sizeof(uint32_t) * 2]; -}; -CTASSERT(sizeof(struct vmcs) == PAGE_SIZE); +#include +#include +#include +#include -/* MSR save region is composed of an array of 'struct msr_entry' */ -struct msr_entry { - uint32_t index; - uint32_t reserved; - uint64_t val; +int vmcs_getreg(int vcpuid, int ident, uint64_t *rv); +int vmcs_setreg(int vcpuid, int ident, uint64_t val); +int vmcs_getdesc(int vcpuid, int ident, struct seg_desc *desc); +int vmcs_setdesc(int vcpuid, int ident, struct seg_desc *desc); -}; - -int vmcs_set_msr_save(struct vmcs *vmcs, u_long g_area, u_int g_count); -int vmcs_init(struct vmcs *vmcs); -int vmcs_getreg(struct vmcs *vmcs, int running, int ident, uint64_t *rv); -int vmcs_setreg(struct vmcs *vmcs, int running, int ident, uint64_t val); -int vmcs_getdesc(struct vmcs *vmcs, int running, int ident, - struct seg_desc *desc); -int vmcs_setdesc(struct vmcs *vmcs, int running, int ident, - struct seg_desc *desc); - -/* - * Avoid header pollution caused by inline use of 'vtophys()' in vmx_cpufunc.h - */ -#ifdef _VMX_CPUFUNC_H_ static __inline uint64_t -vmcs_read(uint32_t encoding) +vmcs_read(int vcpuid, uint32_t encoding) { - int error; uint64_t val; - error = vmread(encoding, &val); - KASSERT(error == 0, ("vmcs_read(%u) error %d", encoding, error)); + hv_vmx_vcpu_read_vmcs(((hv_vcpuid_t) vcpuid), encoding, &val); return (val); } static __inline void -vmcs_write(uint32_t encoding, uint64_t val) +vmcs_write(int vcpuid, uint32_t encoding, uint64_t val) { - int error; - - error = vmwrite(encoding, val); - KASSERT(error == 0, ("vmcs_write(%u) error %d", encoding, error)); + if (encoding == 0x00004002) { + if (val == 0x0000000000000004) { + abort(); + } + } + hv_vmx_vcpu_write_vmcs(((hv_vcpuid_t) vcpuid), encoding, val); } -#endif /* _VMX_CPUFUNC_H_ */ -#define vmexit_instruction_length() vmcs_read(VMCS_EXIT_INSTRUCTION_LENGTH) -#define vmcs_guest_rip() vmcs_read(VMCS_GUEST_RIP) -#define vmcs_instruction_error() vmcs_read(VMCS_INSTRUCTION_ERROR) -#define vmcs_exit_reason() (vmcs_read(VMCS_EXIT_REASON) & 0xffff) -#define vmcs_exit_qualification() vmcs_read(VMCS_EXIT_QUALIFICATION) -#define vmcs_guest_cr3() vmcs_read(VMCS_GUEST_CR3) -#define vmcs_gpa() vmcs_read(VMCS_GUEST_PHYSICAL_ADDRESS) -#define vmcs_gla() vmcs_read(VMCS_GUEST_LINEAR_ADDRESS) -#define vmcs_idt_vectoring_info() vmcs_read(VMCS_IDT_VECTORING_INFO) -#define vmcs_idt_vectoring_err() vmcs_read(VMCS_IDT_VECTORING_ERROR) - -#endif /* _KERNEL */ +#define vmexit_instruction_length(vcpuid) \ + vmcs_read(vcpuid, VMCS_EXIT_INSTRUCTION_LENGTH) +#define vmcs_guest_rip(vcpuid) \ + vmcs_read(vcpuid, VMCS_GUEST_RIP) +#define vmcs_instruction_error(vcpuid) \ + vmcs_read(vcpuid, VMCS_INSTRUCTION_ERROR) +#define vmcs_exit_reason(vcpuid) \ + (vmcs_read(vcpuid, VMCS_EXIT_REASON) & 0xffff) +#define vmcs_exit_qualification(vcpuid) \ + vmcs_read(vcpuid, VMCS_EXIT_QUALIFICATION) +#define vmcs_guest_cr3(vcpuid) \ + vmcs_read(vcpuid, VMCS_GUEST_CR3) +#define vmcs_gpa(vcpuid) \ + vmcs_read(vcpuid, VMCS_GUEST_PHYSICAL_ADDRESS) +#define vmcs_gla(vcpuid) \ + vmcs_read(vcpuid, VMCS_GUEST_LINEAR_ADDRESS) +#define vmcs_idt_vectoring_info(vcpuid) \ + vmcs_read(vcpuid, VMCS_IDT_VECTORING_INFO) +#define vmcs_idt_vectoring_err(vcpuid) \ + vmcs_read(vcpuid, VMCS_IDT_VECTORING_ERROR) #define VMCS_INITIAL 0xffffffffffffffff -#define VMCS_IDENT(encoding) ((encoding) | 0x80000000) +#define VMCS_IDENT(encoding) ((int) (((unsigned) (encoding)) | 0x80000000)) /* * VMCS field encodings from Appendix H, Intel Architecture Manual Vol3B. */ @@ -342,33 +329,33 @@ vmcs_write(uint32_t encoding, uint64_t val) * * Applies to VM-exits due to hardware exception or EPT fault. */ -#define EXIT_QUAL_NMIUDTI (1 << 12) +#define EXIT_QUAL_NMIUDTI (1U << 12) /* * VMCS interrupt information fields */ #define VMCS_INTR_VALID (1U << 31) -#define VMCS_INTR_T_MASK 0x700 /* Interruption-info type */ -#define VMCS_INTR_T_HWINTR (0 << 8) -#define VMCS_INTR_T_NMI (2 << 8) -#define VMCS_INTR_T_HWEXCEPTION (3 << 8) -#define VMCS_INTR_T_SWINTR (4 << 8) -#define VMCS_INTR_T_PRIV_SWEXCEPTION (5 << 8) -#define VMCS_INTR_T_SWEXCEPTION (6 << 8) -#define VMCS_INTR_DEL_ERRCODE (1 << 11) +#define VMCS_INTR_T_MASK 0x700U /* Interruption-info type */ +#define VMCS_INTR_T_HWINTR (0U << 8) +#define VMCS_INTR_T_NMI (2U << 8) +#define VMCS_INTR_T_HWEXCEPTION (3U << 8) +#define VMCS_INTR_T_SWINTR (4U << 8) +#define VMCS_INTR_T_PRIV_SWEXCEPTION (5U << 8) +#define VMCS_INTR_T_SWEXCEPTION (6U << 8) +#define VMCS_INTR_DEL_ERRCODE (1U << 11) /* * VMCS IDT-Vectoring information fields */ #define VMCS_IDT_VEC_VALID (1U << 31) -#define VMCS_IDT_VEC_ERRCODE_VALID (1 << 11) +#define VMCS_IDT_VEC_ERRCODE_VALID (1U << 11) /* * VMCS Guest interruptibility field */ -#define VMCS_INTERRUPTIBILITY_STI_BLOCKING (1 << 0) -#define VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING (1 << 1) -#define VMCS_INTERRUPTIBILITY_SMI_BLOCKING (1 << 2) -#define VMCS_INTERRUPTIBILITY_NMI_BLOCKING (1 << 3) +#define VMCS_INTERRUPTIBILITY_STI_BLOCKING (1U << 0) +#define VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING (1U << 1) +#define VMCS_INTERRUPTIBILITY_SMI_BLOCKING (1U << 2) +#define VMCS_INTERRUPTIBILITY_NMI_BLOCKING (1U << 3) /* * Exit qualification for EXIT_REASON_INVAL_VMCS @@ -397,5 +384,3 @@ vmcs_write(uint32_t encoding, uint64_t val) * Exit qualification for APIC-write VM exit */ #define APIC_WRITE_OFFSET(qual) ((qual) & 0xFFF) - -#endif diff --git a/vmm/intel/vmx.h b/include/xhyve/vmm/intel/vmx.h similarity index 59% rename from vmm/intel/vmx.h rename to include/xhyve/vmm/intel/vmx.h index bc48861..9402892 100644 --- a/vmm/intel/vmx.h +++ b/include/xhyve/vmm/intel/vmx.h @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,50 +27,12 @@ * $FreeBSD$ */ -#ifndef _VMX_H_ -#define _VMX_H_ +#pragma once -#include "vmcs.h" - -struct pmap; - -struct vmxctx { - register_t guest_rdi; /* Guest state */ - register_t guest_rsi; - register_t guest_rdx; - register_t guest_rcx; - register_t guest_r8; - register_t guest_r9; - register_t guest_rax; - register_t guest_rbx; - register_t guest_rbp; - register_t guest_r10; - register_t guest_r11; - register_t guest_r12; - register_t guest_r13; - register_t guest_r14; - register_t guest_r15; - register_t guest_cr2; - - register_t host_r15; /* Host state */ - register_t host_r14; - register_t host_r13; - register_t host_r12; - register_t host_rbp; - register_t host_rsp; - register_t host_rbx; - /* - * XXX todo debug registers and fpu state - */ - - int inst_fail_status; - - /* - * The pmap needs to be deactivated in vmx_enter_guest() - * so keep a copy of the 'pmap' in each vmxctx. - */ - struct pmap *pmap; -}; +#include +#include +#include +#include struct vmxcap { int set; @@ -77,16 +40,19 @@ struct vmxcap { uint32_t proc_ctls2; }; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct vmxstate { uint64_t nextrip; /* next instruction to be executed by guest */ int lastcpu; /* host cpu that this 'vcpu' last ran on */ uint16_t vpid; }; +#pragma clang diagnostic pop struct apic_page { - uint32_t reg[PAGE_SIZE / 4]; + uint32_t reg[XHYVE_PAGE_SIZE / 4]; }; -CTASSERT(sizeof(struct apic_page) == PAGE_SIZE); +CTASSERT(sizeof(struct apic_page) == XHYVE_PAGE_SIZE); /* Posted Interrupt Descriptor (described in section 29.6 of the Intel SDM) */ struct pir_desc { @@ -109,32 +75,20 @@ enum { /* virtual machine softc */ struct vmx { - struct vmcs vmcs[VM_MAXCPU]; /* one vmcs per virtual cpu */ - struct apic_page apic_page[VM_MAXCPU]; /* one apic page per vcpu */ - char msr_bitmap[PAGE_SIZE]; - struct pir_desc pir_desc[VM_MAXCPU]; - uint64_t guest_msrs[VM_MAXCPU][GUEST_MSR_NUM]; - struct vmxctx ctx[VM_MAXCPU]; - struct vmxcap cap[VM_MAXCPU]; - struct vmxstate state[VM_MAXCPU]; - uint64_t eptp; - struct vm *vm; - long eptgen[MAXCPU]; /* cached pmap->pm_eptgen */ + struct apic_page apic_page[VM_MAXCPU]; /* one apic page per vcpu */ + uint64_t guest_msrs[VM_MAXCPU][GUEST_MSR_NUM]; + struct vmxcap cap[VM_MAXCPU]; + struct vmxstate state[VM_MAXCPU]; + struct vm *vm; }; -CTASSERT((offsetof(struct vmx, vmcs) & PAGE_MASK) == 0); -CTASSERT((offsetof(struct vmx, msr_bitmap) & PAGE_MASK) == 0); -CTASSERT((offsetof(struct vmx, pir_desc[0]) & 63) == 0); #define VMX_GUEST_VMEXIT 0 #define VMX_VMRESUME_ERROR 1 #define VMX_VMLAUNCH_ERROR 2 #define VMX_INVEPT_ERROR 3 -int vmx_enter_guest(struct vmxctx *ctx, struct vmx *vmx, int launched); void vmx_call_isr(uintptr_t entry); u_long vmx_fix_cr0(u_long cr0); u_long vmx_fix_cr4(u_long cr4); extern char vmx_exit_guest[]; - -#endif diff --git a/include/xhyve/vmm/intel/vmx_controls.h b/include/xhyve/vmm/intel/vmx_controls.h new file mode 100644 index 0000000..230afba --- /dev/null +++ b/include/xhyve/vmm/intel/vmx_controls.h @@ -0,0 +1,94 @@ +/*- + * Copyright (c) 2011 NetApp, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#pragma once + +/* Pin-Based VM-Execution Controls */ +#define PINBASED_EXTINT_EXITING (1u << 0) +#define PINBASED_NMI_EXITING (1u << 3) +#define PINBASED_VIRTUAL_NMI (1u << 5) +#define PINBASED_PREMPTION_TIMER (1u << 6) +#define PINBASED_POSTED_INTERRUPT (1u << 7) + +/* Primary Processor-Based VM-Execution Controls */ +#define PROCBASED_INT_WINDOW_EXITING (1u << 2) +#define PROCBASED_TSC_OFFSET (1u << 3) +#define PROCBASED_HLT_EXITING (1u << 7) +#define PROCBASED_INVLPG_EXITING (1u << 9) +#define PROCBASED_MWAIT_EXITING (1u << 10) +#define PROCBASED_RDPMC_EXITING (1u << 11) +#define PROCBASED_RDTSC_EXITING (1u << 12) +#define PROCBASED_CR3_LOAD_EXITING (1u << 15) +#define PROCBASED_CR3_STORE_EXITING (1u << 16) +#define PROCBASED_CR8_LOAD_EXITING (1u << 19) +#define PROCBASED_CR8_STORE_EXITING (1u << 20) +#define PROCBASED_USE_TPR_SHADOW (1u << 21) +#define PROCBASED_NMI_WINDOW_EXITING (1u << 22) +#define PROCBASED_MOV_DR_EXITING (1u << 23) +#define PROCBASED_IO_EXITING (1u << 24) +#define PROCBASED_IO_BITMAPS (1u << 25) +#define PROCBASED_MTF (1u << 27) +#define PROCBASED_MSR_BITMAPS (1u << 28) +#define PROCBASED_MONITOR_EXITING (1u << 29) +#define PROCBASED_PAUSE_EXITING (1u << 30) +#define PROCBASED_SECONDARY_CONTROLS (1U << 31) + +/* Secondary Processor-Based VM-Execution Controls */ +#define PROCBASED2_VIRTUALIZE_APIC_ACCESSES (1u << 0) +#define PROCBASED2_ENABLE_EPT (1u << 1) +#define PROCBASED2_DESC_TABLE_EXITING (1u << 2) +#define PROCBASED2_ENABLE_RDTSCP (1u << 3) +#define PROCBASED2_VIRTUALIZE_X2APIC_MODE (1u << 4) +#define PROCBASED2_ENABLE_VPID (1u << 5) +#define PROCBASED2_WBINVD_EXITING (1u << 6) +#define PROCBASED2_UNRESTRICTED_GUEST (1u << 7) +#define PROCBASED2_APIC_REGISTER_VIRTUALIZATION (1u << 8) +#define PROCBASED2_VIRTUAL_INTERRUPT_DELIVERY (1u << 9) +#define PROCBASED2_PAUSE_LOOP_EXITING (1u << 10) +#define PROCBASED2_RDRAND_EXITING (1u << 11) +#define PROCBASED2_ENABLE_INVPCID (1u << 12) + +/* VM Exit Controls */ +#define VM_EXIT_SAVE_DEBUG_CONTROLS (1u << 2) +#define VM_EXIT_HOST_LMA (1u << 9) +#define VM_EXIT_LOAD_PERF_GLOBAL_CTRL (1u << 12) +#define VM_EXIT_ACKNOWLEDGE_INTERRUPT (1u << 15) +#define VM_EXIT_SAVE_PAT (1u << 18) +#define VM_EXIT_LOAD_PAT (1u << 19) +#define VM_EXIT_SAVE_EFER (1u << 20) +#define VM_EXIT_LOAD_EFER (1u << 21) +#define VM_EXIT_SAVE_PREEMPTION_TIMER (1u << 22) + +/* VM Entry Controls */ +#define VM_ENTRY_LOAD_DEBUG_CONTROLS (1u << 2) +#define VM_ENTRY_GUEST_LMA (1u << 9) +#define VM_ENTRY_INTO_SMM (1u << 10) +#define VM_ENTRY_DEACTIVATE_DUAL_MONITOR (1u << 11) +#define VM_ENTRY_LOAD_PERF_GLOBAL_CTRL (1u << 13) +#define VM_ENTRY_LOAD_PAT (1u << 14) +#define VM_ENTRY_LOAD_EFER (1u << 15) diff --git a/vmm/intel/ept.h b/include/xhyve/vmm/intel/vmx_msr.h similarity index 74% rename from vmm/intel/ept.h rename to include/xhyve/vmm/intel/vmx_msr.h index 1393e46..58d532d 100644 --- a/vmm/intel/ept.h +++ b/include/xhyve/vmm/intel/vmx_msr.h @@ -26,14 +26,20 @@ * $FreeBSD$ */ -#ifndef _EPT_H_ -#define _EPT_H_ +#pragma once + +#include +#include +#include +#include +#include struct vmx; -int ept_init(int ipinum); -void ept_invalidate_mappings(u_long eptp); -struct vmspace *ept_vmspace_alloc(vm_offset_t min, vm_offset_t max); -void ept_vmspace_free(struct vmspace *vmspace); -uint64_t eptp(uint64_t pml4); -#endif +void vmx_msr_init(void); +void vmx_msr_guest_init(struct vmx *vmx, int vcpuid); +int vmx_rdmsr(struct vmx *, int vcpuid, u_int num, uint64_t *val); +int vmx_wrmsr(struct vmx *, int vcpuid, u_int num, uint64_t val); + +int vmx_set_ctlreg(hv_vmx_capability_t cap_field, uint32_t ones_mask, + uint32_t zeros_mask, uint32_t *retval); diff --git a/vmm/io/vatpic.h b/include/xhyve/vmm/io/vatpic.h similarity index 88% rename from vmm/io/vatpic.h rename to include/xhyve/vmm/io/vatpic.h index d4a1be1..a17b9db 100644 --- a/vmm/io/vatpic.h +++ b/include/xhyve/vmm/io/vatpic.h @@ -26,15 +26,19 @@ * $FreeBSD$ */ -#ifndef _VATPIC_H_ -#define _VATPIC_H_ +#pragma once -#include +#include +#include +#include -#define ICU_IMR_OFFSET 1 +#define IO_ICU1 0x020 /* 8259A Interrupt Controller #1 */ +#define IO_ICU2 0x0a0 /* 8259A Interrupt Controller #2 */ -#define IO_ELCR1 0x4d0 -#define IO_ELCR2 0x4d1 +#define ICU_IMR_OFFSET 1 + +#define IO_ELCR1 0x4d0 +#define IO_ELCR2 0x4d1 struct vatpic *vatpic_init(struct vm *vm); void vatpic_cleanup(struct vatpic *vatpic); @@ -53,5 +57,3 @@ int vatpic_set_irq_trigger(struct vm *vm, int irq, enum vm_intr_trigger trigger) void vatpic_pending_intr(struct vm *vm, int *vecptr); void vatpic_intr_accepted(struct vm *vm, int vector); - -#endif /* _VATPIC_H_ */ diff --git a/vmm/io/vatpit.h b/include/xhyve/vmm/io/vatpit.h similarity index 91% rename from vmm/io/vatpit.h rename to include/xhyve/vmm/io/vatpit.h index 5719c9c..87c61a1 100644 --- a/vmm/io/vatpit.h +++ b/include/xhyve/vmm/io/vatpit.h @@ -27,19 +27,22 @@ * $FreeBSD$ */ -#ifndef _VATPIT_H_ -#define _VATPIT_H_ +#pragma once -#include +#include +#include + +//#include #define NMISC_PORT 0x61 +struct vm; +struct vatpit; + struct vatpit *vatpit_init(struct vm *vm); void vatpit_cleanup(struct vatpit *vatpit); int vatpit_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, - uint32_t *eax); + uint32_t *eax); int vatpit_nmisc_handler(struct vm *vm, int vcpuid, bool in, int port, - int bytes, uint32_t *eax); - -#endif /* _VATPIT_H_ */ + int bytes, uint32_t *eax); diff --git a/vmm/io/vhpet.h b/include/xhyve/vmm/io/vhpet.h similarity index 79% rename from vmm/io/vhpet.h rename to include/xhyve/vmm/io/vhpet.h index 330e017..ea09e6e 100644 --- a/vmm/io/vhpet.h +++ b/include/xhyve/vmm/io/vhpet.h @@ -27,18 +27,20 @@ * $FreeBSD$ */ -#ifndef _VHPET_H_ -#define _VHPET_H_ +#pragma once -#define VHPET_BASE 0xfed00000 -#define VHPET_SIZE 1024 +#include + +#define VHPET_BASE 0xfed00000 +#define VHPET_SIZE 0x400 + +struct vm; +struct vhpet; struct vhpet *vhpet_init(struct vm *vm); -void vhpet_cleanup(struct vhpet *vhpet); -int vhpet_mmio_write(void *vm, int vcpuid, uint64_t gpa, uint64_t val, - int size, void *arg); -int vhpet_mmio_read(void *vm, int vcpuid, uint64_t gpa, uint64_t *val, - int size, void *arg); -int vhpet_getcap(struct vm_hpet_cap *cap); - -#endif /* _VHPET_H_ */ +void vhpet_cleanup(struct vhpet *vhpet); +int vhpet_mmio_write(void *vm, int vcpuid, uint64_t gpa, uint64_t val, + int size, void *arg); +int vhpet_mmio_read(void *vm, int vcpuid, uint64_t gpa, uint64_t *val, + int size, void *arg); +int vhpet_getcap(uint32_t *cap); diff --git a/vmm/io/vioapic.h b/include/xhyve/vmm/io/vioapic.h similarity index 71% rename from vmm/io/vioapic.h rename to include/xhyve/vmm/io/vioapic.h index 65176b3..b5df1ba 100644 --- a/vmm/io/vioapic.h +++ b/include/xhyve/vmm/io/vioapic.h @@ -27,24 +27,27 @@ * $FreeBSD$ */ -#ifndef _VIOAPIC_H_ -#define _VIOAPIC_H_ +#pragma once -#define VIOAPIC_BASE 0xFEC00000 -#define VIOAPIC_SIZE 4096 +#include + +#define VIOAPIC_BASE 0xfec00000 +#define VIOAPIC_SIZE 0x1000 + +struct vm; +struct vioapic; struct vioapic *vioapic_init(struct vm *vm); -void vioapic_cleanup(struct vioapic *vioapic); +void vioapic_cleanup(struct vioapic *vioapic); -int vioapic_assert_irq(struct vm *vm, int irq); -int vioapic_deassert_irq(struct vm *vm, int irq); -int vioapic_pulse_irq(struct vm *vm, int irq); +int vioapic_assert_irq(struct vm *vm, int irq); +int vioapic_deassert_irq(struct vm *vm, int irq); +int vioapic_pulse_irq(struct vm *vm, int irq); -int vioapic_mmio_write(void *vm, int vcpuid, uint64_t gpa, - uint64_t wval, int size, void *arg); -int vioapic_mmio_read(void *vm, int vcpuid, uint64_t gpa, - uint64_t *rval, int size, void *arg); +int vioapic_mmio_write(void *vm, int vcpuid, uint64_t gpa, + uint64_t wval, int size, void *arg); +int vioapic_mmio_read(void *vm, int vcpuid, uint64_t gpa, + uint64_t *rval, int size, void *arg); -int vioapic_pincount(struct vm *vm); -void vioapic_process_eoi(struct vm *vm, int vcpuid, int vector); -#endif +int vioapic_pincount(struct vm *vm); +void vioapic_process_eoi(struct vm *vm, int vcpuid, int vector); diff --git a/vmm/io/vlapic.h b/include/xhyve/vmm/io/vlapic.h similarity index 98% rename from vmm/io/vlapic.h rename to include/xhyve/vmm/io/vlapic.h index 0e68b2f..e7520c6 100644 --- a/vmm/io/vlapic.h +++ b/include/xhyve/vmm/io/vlapic.h @@ -26,11 +26,13 @@ * $FreeBSD$ */ -#ifndef _VLAPIC_H_ -#define _VLAPIC_H_ +#pragma once + +#include +#include +#include struct vm; -enum x2apic_state; int vlapic_write(struct vlapic *vlapic, int mmio_access, uint64_t offset, uint64_t data, bool *retu); @@ -106,4 +108,3 @@ void vlapic_icrtmr_write_handler(struct vlapic *vlapic); void vlapic_dcr_write_handler(struct vlapic *vlapic); void vlapic_lvt_write_handler(struct vlapic *vlapic, uint32_t offset); void vlapic_self_ipi_handler(struct vlapic *vlapic, uint64_t val); -#endif /* _VLAPIC_H_ */ diff --git a/vmm/io/vlapic_priv.h b/include/xhyve/vmm/io/vlapic_priv.h similarity index 73% rename from vmm/io/vlapic_priv.h rename to include/xhyve/vmm/io/vlapic_priv.h index 08592c8..62d2811 100644 --- a/vmm/io/vlapic_priv.h +++ b/include/xhyve/vmm/io/vlapic_priv.h @@ -26,10 +26,13 @@ * $FreeBSD$ */ -#ifndef _VLAPIC_PRIV_H_ -#define _VLAPIC_PRIV_H_ +#pragma once -#include +#include +#include +#include +#include +#include /* * APIC Register: Offset Description @@ -95,32 +98,32 @@ #define VLAPIC_CTR3(vlapic, format, p1, p2, p3) \ VCPU_CTR3((vlapic)->vm, (vlapic)->vcpuid, format, p1, p2, p3) -#define VLAPIC_CTR_IRR(vlapic, msg) \ -do { \ - uint32_t *irrptr = &(vlapic)->apic_page->irr0; \ - irrptr[0] = irrptr[0]; /* silence compiler */ \ - VLAPIC_CTR1((vlapic), msg " irr0 0x%08x", irrptr[0 << 2]); \ - VLAPIC_CTR1((vlapic), msg " irr1 0x%08x", irrptr[1 << 2]); \ - VLAPIC_CTR1((vlapic), msg " irr2 0x%08x", irrptr[2 << 2]); \ - VLAPIC_CTR1((vlapic), msg " irr3 0x%08x", irrptr[3 << 2]); \ - VLAPIC_CTR1((vlapic), msg " irr4 0x%08x", irrptr[4 << 2]); \ - VLAPIC_CTR1((vlapic), msg " irr5 0x%08x", irrptr[5 << 2]); \ - VLAPIC_CTR1((vlapic), msg " irr6 0x%08x", irrptr[6 << 2]); \ - VLAPIC_CTR1((vlapic), msg " irr7 0x%08x", irrptr[7 << 2]); \ +#define VLAPIC_CTR_IRR(vlapic, msg) \ +do { \ + uint32_t *x = &(vlapic)->apic_page->irr0; \ + x[0] = x[0]; /* silence compiler */ \ + VLAPIC_CTR1((vlapic), msg " irr0 0x%08x", x[0 << 2]); \ + VLAPIC_CTR1((vlapic), msg " irr1 0x%08x", x[1 << 2]); \ + VLAPIC_CTR1((vlapic), msg " irr2 0x%08x", x[2 << 2]); \ + VLAPIC_CTR1((vlapic), msg " irr3 0x%08x", x[3 << 2]); \ + VLAPIC_CTR1((vlapic), msg " irr4 0x%08x", x[4 << 2]); \ + VLAPIC_CTR1((vlapic), msg " irr5 0x%08x", x[5 << 2]); \ + VLAPIC_CTR1((vlapic), msg " irr6 0x%08x", x[6 << 2]); \ + VLAPIC_CTR1((vlapic), msg " irr7 0x%08x", x[7 << 2]); \ } while (0) -#define VLAPIC_CTR_ISR(vlapic, msg) \ -do { \ - uint32_t *isrptr = &(vlapic)->apic_page->isr0; \ - isrptr[0] = isrptr[0]; /* silence compiler */ \ - VLAPIC_CTR1((vlapic), msg " isr0 0x%08x", isrptr[0 << 2]); \ - VLAPIC_CTR1((vlapic), msg " isr1 0x%08x", isrptr[1 << 2]); \ - VLAPIC_CTR1((vlapic), msg " isr2 0x%08x", isrptr[2 << 2]); \ - VLAPIC_CTR1((vlapic), msg " isr3 0x%08x", isrptr[3 << 2]); \ - VLAPIC_CTR1((vlapic), msg " isr4 0x%08x", isrptr[4 << 2]); \ - VLAPIC_CTR1((vlapic), msg " isr5 0x%08x", isrptr[5 << 2]); \ - VLAPIC_CTR1((vlapic), msg " isr6 0x%08x", isrptr[6 << 2]); \ - VLAPIC_CTR1((vlapic), msg " isr7 0x%08x", isrptr[7 << 2]); \ +#define VLAPIC_CTR_ISR(vlapic, msg) \ +do { \ + uint32_t *x = &(vlapic)->apic_page->isr0; \ + x[0] = x[0]; /* silence compiler */ \ + VLAPIC_CTR1((vlapic), msg " isr0 0x%08x", x[0 << 2]); \ + VLAPIC_CTR1((vlapic), msg " isr1 0x%08x", x[1 << 2]); \ + VLAPIC_CTR1((vlapic), msg " isr2 0x%08x", x[2 << 2]); \ + VLAPIC_CTR1((vlapic), msg " isr3 0x%08x", x[3 << 2]); \ + VLAPIC_CTR1((vlapic), msg " isr4 0x%08x", x[4 << 2]); \ + VLAPIC_CTR1((vlapic), msg " isr5 0x%08x", x[5 << 2]); \ + VLAPIC_CTR1((vlapic), msg " isr6 0x%08x", x[6 << 2]); \ + VLAPIC_CTR1((vlapic), msg " isr7 0x%08x", x[7 << 2]); \ } while (0) enum boot_state { @@ -147,44 +150,40 @@ struct vlapic_ops { void (*enable_x2apic_mode)(struct vlapic *vlapic); }; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct vlapic { - struct vm *vm; - int vcpuid; - struct LAPIC *apic_page; - struct vlapic_ops ops; - - uint32_t esr_pending; - int esr_firing; - - struct callout callout; /* vlapic timer */ - struct bintime timer_fire_bt; /* callout expiry time */ - struct bintime timer_freq_bt; /* timer frequency */ - struct bintime timer_period_bt; /* timer period */ - struct mtx timer_mtx; - + struct vm *vm; + int vcpuid; + struct LAPIC *apic_page; + struct vlapic_ops ops; + uint32_t esr_pending; + int esr_firing; + struct callout callout; /* vlapic timer */ + struct bintime timer_fire_bt; /* callout expiry time */ + struct bintime timer_freq_bt; /* timer frequency */ + struct bintime timer_period_bt; /* timer period */ + OSSpinLock timer_lock; /* * The 'isrvec_stk' is a stack of vectors injected by the local apic. * A vector is popped from the stack when the processor does an EOI. * The vector on the top of the stack is used to compute the * Processor Priority in conjunction with the TPR. */ - uint8_t isrvec_stk[ISRVEC_STK_SIZE]; - int isrvec_stk_top; - - uint64_t msr_apicbase; + uint8_t isrvec_stk[ISRVEC_STK_SIZE]; + int isrvec_stk_top; + uint64_t msr_apicbase; enum boot_state boot_state; - /* * Copies of some registers in the virtual APIC page. We do this for * a couple of different reasons: * - to be able to detect what changed (e.g. svr_last) * - to maintain a coherent snapshot of the register (e.g. lvt_last) */ - uint32_t svr_last; - uint32_t lvt_last[VLAPIC_MAXLVT_INDEX + 1]; + uint32_t svr_last; + uint32_t lvt_last[VLAPIC_MAXLVT_INDEX + 1]; }; +#pragma clang diagnostic pop void vlapic_init(struct vlapic *vlapic); void vlapic_cleanup(struct vlapic *vlapic); - -#endif /* _VLAPIC_PRIV_H_ */ diff --git a/vmm/io/vpmtmr.h b/include/xhyve/vmm/io/vpmtmr.h similarity index 96% rename from vmm/io/vpmtmr.h rename to include/xhyve/vmm/io/vpmtmr.h index 039a281..06caf53 100644 --- a/vmm/io/vpmtmr.h +++ b/include/xhyve/vmm/io/vpmtmr.h @@ -26,17 +26,17 @@ * $FreeBSD$ */ -#ifndef _VPMTMR_H_ -#define _VPMTMR_H_ +#pragma once + +#include #define IO_PMTMR 0x408 +struct vm; struct vpmtmr; struct vpmtmr *vpmtmr_init(struct vm *vm); void vpmtmr_cleanup(struct vpmtmr *pmtmr); int vpmtmr_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, - uint32_t *val); - -#endif + uint32_t *val); diff --git a/vmm/io/vrtc.h b/include/xhyve/vmm/io/vrtc.h similarity index 92% rename from vmm/io/vrtc.h rename to include/xhyve/vmm/io/vrtc.h index 6fbbc9c..334a628 100644 --- a/vmm/io/vrtc.h +++ b/include/xhyve/vmm/io/vrtc.h @@ -26,11 +26,15 @@ * $FreeBSD$ */ -#ifndef _VRTC_H_ -#define _VRTC_H_ +#pragma once -#include +#include +#include +#include +#define IO_RTC 0x070 /* 4990A RTC */ + +struct vm; struct vrtc; struct vrtc *vrtc_init(struct vm *vm); @@ -43,8 +47,6 @@ int vrtc_nvram_write(struct vm *vm, int offset, uint8_t value); int vrtc_nvram_read(struct vm *vm, int offset, uint8_t *retval); int vrtc_addr_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, - uint32_t *val); + uint32_t *val); int vrtc_data_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, - uint32_t *val); - -#endif + uint32_t *val); diff --git a/include/xhyve/vmm/vmm.h b/include/xhyve/vmm/vmm.h new file mode 100644 index 0000000..7b72e8a --- /dev/null +++ b/include/xhyve/vmm/vmm.h @@ -0,0 +1,314 @@ +/*- + * Copyright (c) 2011 NetApp, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" + +#define VM_INTINFO_VECTOR(info) ((info) & 0xff) +#define VM_INTINFO_DEL_ERRCODE 0x800 +#define VM_INTINFO_RSVD 0x7ffff000 +#define VM_INTINFO_VALID 0x80000000 +#define VM_INTINFO_TYPE 0x700 +#define VM_INTINFO_HWINTR (0 << 8) +#define VM_INTINFO_NMI (2 << 8) +#define VM_INTINFO_HWEXCEPTION (3 << 8) +#define VM_INTINFO_SWINTR (4 << 8) + +struct vm; +struct vm_exception; +struct vm_memory_segment; +struct seg_desc; +struct vm_exit; +struct vm_run; +struct vhpet; +struct vioapic; +struct vlapic; +struct vmspace; +struct vm_object; +struct vm_guest_paging; +struct pmap; + +typedef int (*vmm_init_func_t)(void); +typedef int (*vmm_cleanup_func_t)(void); +typedef void *(*vmi_vm_init_func_t)(struct vm *vm); +typedef int (*vmi_vcpu_init_func_t)(void *vmi, int vcpu); +typedef int (*vmi_run_func_t)(void *vmi, int vcpu, register_t rip, + void *rendezvous_cookie, void *suspend_cookie); +typedef void (*vmi_vm_cleanup_func_t)(void *vmi); +typedef void (*vmi_vcpu_cleanup_func_t)(void *vmi, int vcpu); +typedef int (*vmi_get_register_t)(void *vmi, int vcpu, int num, + uint64_t *retval); +typedef int (*vmi_set_register_t)(void *vmi, int vcpu, int num, + uint64_t val); +typedef int (*vmi_get_desc_t)(void *vmi, int vcpu, int num, + struct seg_desc *desc); +typedef int (*vmi_set_desc_t)(void *vmi, int vcpu, int num, + struct seg_desc *desc); +typedef int (*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval); +typedef int (*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val); +typedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu); +typedef void (*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic); +typedef void (*vmi_interrupt)(int vcpu); + +struct vmm_ops { + vmm_init_func_t init; /* module wide initialization */ + vmm_cleanup_func_t cleanup; + vmi_vm_init_func_t vm_init; /* vm-specific initialization */ + vmi_vcpu_init_func_t vcpu_init; + vmi_run_func_t vmrun; + vmi_vm_cleanup_func_t vm_cleanup; + vmi_vcpu_cleanup_func_t vcpu_cleanup; + vmi_get_register_t vmgetreg; + vmi_set_register_t vmsetreg; + vmi_get_desc_t vmgetdesc; + vmi_set_desc_t vmsetdesc; + vmi_get_cap_t vmgetcap; + vmi_set_cap_t vmsetcap; + vmi_vlapic_init vlapic_init; + vmi_vlapic_cleanup vlapic_cleanup; + vmi_interrupt vcpu_interrupt; +}; + +extern struct vmm_ops vmm_ops_intel; + +int vmm_init(void); +int vmm_cleanup(void); +int vm_create(struct vm **retvm); +int vcpu_create(struct vm *vm, int vcpu); +void vm_destroy(struct vm *vm); +void vcpu_destroy(struct vm *vm, int vcpu); +int vm_reinit(struct vm *vm); +const char *vm_name(struct vm *vm); +int vm_malloc(struct vm *vm, uint64_t gpa, size_t len); +void *vm_gpa2hva(struct vm *vm, uint64_t gpa, uint64_t len); +int vm_gpabase2memseg(struct vm *vm, uint64_t gpabase, + struct vm_memory_segment *seg); +int vm_get_memobj(struct vm *vm, uint64_t gpa, size_t len, uint64_t *offset, + void **object); +bool vm_mem_allocated(struct vm *vm, uint64_t gpa); +int vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval); +int vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val); +int vm_get_seg_desc(struct vm *vm, int vcpu, int reg, + struct seg_desc *ret_desc); +int vm_set_seg_desc(struct vm *vm, int vcpu, int reg, struct seg_desc *desc); +int vm_run(struct vm *vm, int vcpu, struct vm_exit *vm_exit); +int vm_suspend(struct vm *vm, enum vm_suspend_how how); +int vm_inject_nmi(struct vm *vm, int vcpu); +int vm_nmi_pending(struct vm *vm, int vcpuid); +void vm_nmi_clear(struct vm *vm, int vcpuid); +int vm_inject_extint(struct vm *vm, int vcpu); +int vm_extint_pending(struct vm *vm, int vcpuid); +void vm_extint_clear(struct vm *vm, int vcpuid); +struct vlapic *vm_lapic(struct vm *vm, int cpu); +struct vioapic *vm_ioapic(struct vm *vm); +struct vhpet *vm_hpet(struct vm *vm); +int vm_get_capability(struct vm *vm, int vcpu, int type, int *val); +int vm_set_capability(struct vm *vm, int vcpu, int type, int val); +int vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state); +int vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state); +int vm_apicid2vcpuid(struct vm *vm, int apicid); +int vm_activate_cpu(struct vm *vm, int vcpu); +struct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid); +void vm_exit_suspended(struct vm *vm, int vcpuid, uint64_t rip); +void vm_exit_rendezvous(struct vm *vm, int vcpuid, uint64_t rip); + +/* + * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'. + * The rendezvous 'func(arg)' is not allowed to do anything that will + * cause the thread to be put to sleep. + * + * If the rendezvous is being initiated from a vcpu context then the + * 'vcpuid' must refer to that vcpu, otherwise it should be set to -1. + * + * The caller cannot hold any locks when initiating the rendezvous. + * + * The implementation of this API may cause vcpus other than those specified + * by 'dest' to be stalled. The caller should not rely on any vcpus making + * forward progress when the rendezvous is in progress. + */ +typedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg); +void vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest, + vm_rendezvous_func_t func, void *arg); +cpuset_t vm_active_cpus(struct vm *vm); +cpuset_t vm_suspended_cpus(struct vm *vm); + +static __inline int +vcpu_rendezvous_pending(void *rendezvous_cookie) +{ + + return (*(uintptr_t *)rendezvous_cookie != 0); +} + +static __inline int +vcpu_suspended(void *suspend_cookie) +{ + + return (*(int *)suspend_cookie); +} + +enum vcpu_state { + VCPU_IDLE, + VCPU_FROZEN, + VCPU_RUNNING, + VCPU_SLEEPING, +}; + +int vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state, + bool from_idle); +enum vcpu_state vcpu_get_state(struct vm *vm, int vcpu); + +static int __inline +vcpu_is_running(struct vm *vm, int vcpu) +{ + return (vcpu_get_state(vm, vcpu) == VCPU_RUNNING); +} + +void *vcpu_stats(struct vm *vm, int vcpu); +void vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr); +struct vatpic *vm_atpic(struct vm *vm); +struct vatpit *vm_atpit(struct vm *vm); +struct vpmtmr *vm_pmtmr(struct vm *vm); +struct vrtc *vm_rtc(struct vm *vm); + +/* + * Inject exception 'vector' into the guest vcpu. This function returns 0 on + * success and non-zero on failure. + * + * Wrapper functions like 'vm_inject_gp()' should be preferred to calling + * this function directly because they enforce the trap-like or fault-like + * behavior of an exception. + * + * This function should only be called in the context of the thread that is + * executing this vcpu. + */ +int vm_inject_exception(struct vm *vm, int vcpuid, int vector, int err_valid, + uint32_t errcode, int restart_instruction); + +/* + * This function is called after a VM-exit that occurred during exception or + * interrupt delivery through the IDT. The format of 'intinfo' is described + * in Figure 15-1, "EXITINTINFO for All Intercepts", APM, Vol 2. + * + * If a VM-exit handler completes the event delivery successfully then it + * should call vm_exit_intinfo() to extinguish the pending event. For e.g., + * if the task switch emulation is triggered via a task gate then it should + * call this function with 'intinfo=0' to indicate that the external event + * is not pending anymore. + * + * Return value is 0 on success and non-zero on failure. + */ +int vm_exit_intinfo(struct vm *vm, int vcpuid, uint64_t intinfo); + +/* + * This function is called before every VM-entry to retrieve a pending + * event that should be injected into the guest. This function combines + * nested events into a double or triple fault. + * + * Returns 0 if there are no events that need to be injected into the guest + * and non-zero otherwise. + */ +int vm_entry_intinfo(struct vm *vm, int vcpuid, uint64_t *info); + +int vm_get_intinfo(struct vm *vm, int vcpuid, uint64_t *info1, uint64_t *info2); + +enum vm_reg_name vm_segment_name(int seg_encoding); + +struct vm_copyinfo { + uint64_t gpa; + size_t len; + void *hva; +}; + +/* + * Set up 'copyinfo[]' to copy to/from guest linear address space starting + * at 'gla' and 'len' bytes long. The 'prot' should be set to PROT_READ for + * a copyin or PROT_WRITE for a copyout. + * + * retval is_fault Intepretation + * 0 0 Success + * 0 1 An exception was injected into the guest + * EFAULT N/A Unrecoverable error + * + * The 'copyinfo[]' can be passed to 'vm_copyin()' or 'vm_copyout()' only if + * the return value is 0. The 'copyinfo[]' resources should be freed by calling + * 'vm_copy_teardown()' after the copy is done. + */ +int vm_copy_setup(struct vm *vm, int vcpuid, struct vm_guest_paging *paging, + uint64_t gla, size_t len, int prot, struct vm_copyinfo *copyinfo, + int num_copyinfo, int *is_fault); +void vm_copy_teardown(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo, + int num_copyinfo); +void vm_copyin(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo, + void *kaddr, size_t len); +void vm_copyout(struct vm *vm, int vcpuid, const void *kaddr, + struct vm_copyinfo *copyinfo, size_t len); + +int vcpu_trace_exceptions(void); + +/* APIs to inject faults into the guest */ +void vm_inject_fault(void *vm, int vcpuid, int vector, int errcode_valid, + int errcode); + +static __inline void +vm_inject_ud(void *vm, int vcpuid) +{ + vm_inject_fault(vm, vcpuid, IDT_UD, 0, 0); +} + +static __inline void +vm_inject_gp(void *vm, int vcpuid) +{ + vm_inject_fault(vm, vcpuid, IDT_GP, 1, 0); +} + +static __inline void +vm_inject_ac(void *vm, int vcpuid, int errcode) +{ + vm_inject_fault(vm, vcpuid, IDT_AC, 1, errcode); +} + +static __inline void +vm_inject_ss(void *vm, int vcpuid, int errcode) +{ + vm_inject_fault(vm, vcpuid, IDT_SS, 1, errcode); +} + +void vm_inject_pf(void *vm, int vcpuid, int error_code, uint64_t cr2); + +int vm_restart_instruction(void *vm, int vcpuid); + +#pragma clang diagnostic pop diff --git a/include/xhyve/vmm/vmm_api.h b/include/xhyve/vmm/vmm_api.h new file mode 100644 index 0000000..1cfd7f0 --- /dev/null +++ b/include/xhyve/vmm/vmm_api.h @@ -0,0 +1,113 @@ +/*- + * Copyright (c) 2011 NetApp, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#pragma once + +#include +#include +#include +#include + +struct iovec; + +/* + * Different styles of mapping the memory assigned to a VM into the address + * space of the controlling process. + */ +enum vm_mmap_style { + VM_MMAP_NONE, /* no mapping */ + VM_MMAP_ALL, /* fully and statically mapped */ + VM_MMAP_SPARSE, /* mappings created on-demand */ +}; + +int xh_vm_create(void); +void xh_vm_destroy(void); +int xh_vcpu_create(int vcpu); +void xh_vcpu_destroy(int vcpu); +int xh_vm_get_memory_seg(uint64_t gpa, size_t *ret_len); +int xh_vm_setup_memory(size_t len, enum vm_mmap_style vms); +void *xh_vm_map_gpa(uint64_t gpa, size_t len); +int xh_vm_gla2gpa(int vcpu, struct vm_guest_paging *paging, uint64_t gla, + int prot, uint64_t *gpa, int *fault); +uint32_t xh_vm_get_lowmem_limit(void); +void xh_vm_set_lowmem_limit(uint32_t limit); +void xh_vm_set_memflags(int flags); +size_t xh_vm_get_lowmem_size(void); +size_t xh_vm_get_highmem_size(void); +int xh_vm_set_desc(int vcpu, int reg, uint64_t base, uint32_t limit, + uint32_t access); +int xh_vm_get_desc(int vcpu, int reg, uint64_t *base, uint32_t *limit, + uint32_t *access); +int xh_vm_get_seg_desc(int vcpu, int reg, struct seg_desc *seg_desc); +int xh_vm_set_register(int vcpu, int reg, uint64_t val); +int xh_vm_get_register(int vcpu, int reg, uint64_t *retval); +int xh_vm_run(int vcpu, struct vm_exit *ret_vmexit); +int xh_vm_suspend(enum vm_suspend_how how); +int xh_vm_reinit(void); +int xh_vm_apicid2vcpu(int apicid); +int xh_vm_inject_exception(int vcpu, int vector, int errcode_valid, + uint32_t errcode, int restart_instruction); +int xh_vm_lapic_irq(int vcpu, int vector); +int xh_vm_lapic_local_irq(int vcpu, int vector); +int xh_vm_lapic_msi(uint64_t addr, uint64_t msg); +int xh_vm_ioapic_assert_irq(int irq); +int xh_vm_ioapic_deassert_irq(int irq); +int xh_vm_ioapic_pulse_irq(int irq); +int xh_vm_ioapic_pincount(int *pincount); +int xh_vm_isa_assert_irq(int atpic_irq, int ioapic_irq); +int xh_vm_isa_deassert_irq(int atpic_irq, int ioapic_irq); +int xh_vm_isa_pulse_irq(int atpic_irq, int ioapic_irq); +int xh_vm_isa_set_irq_trigger(int atpic_irq, enum vm_intr_trigger trigger); +int xh_vm_inject_nmi(int vcpu); +int xh_vm_capability_name2type(const char *capname); +const char *xh_vm_capability_type2name(int type); +int xh_vm_get_capability(int vcpu, enum vm_cap_type cap, int *retval); +int xh_vm_set_capability(int vcpu, enum vm_cap_type cap, int val); +int xh_vm_get_intinfo(int vcpu, uint64_t *i1, uint64_t *i2); +int xh_vm_set_intinfo(int vcpu, uint64_t exit_intinfo); +uint64_t *xh_vm_get_stats(int vcpu, struct timeval *ret_tv, int *ret_entries); +const char *xh_vm_get_stat_desc(int index); +int xh_vm_get_x2apic_state(int vcpu, enum x2apic_state *s); +int xh_vm_set_x2apic_state(int vcpu, enum x2apic_state s); +int xh_vm_get_hpet_capabilities(uint32_t *capabilities); +int xh_vm_copy_setup(int vcpu, struct vm_guest_paging *pg, uint64_t gla, + size_t len, int prot, struct iovec *iov, int iovcnt, int *fault); +void xh_vm_copyin(struct iovec *iov, void *dst, size_t len); +void xh_vm_copyout(const void *src, struct iovec *iov, size_t len); +int xh_vm_rtc_write(int offset, uint8_t value); +int xh_vm_rtc_read(int offset, uint8_t *retval); +int xh_vm_rtc_settime(time_t secs); +int xh_vm_rtc_gettime(time_t *secs); +int xh_vcpu_reset(int vcpu); +int xh_vm_active_cpus(cpuset_t *cpus); +int xh_vm_suspended_cpus(cpuset_t *cpus); +int xh_vm_activate_cpu(int vcpu); +int xh_vm_restart_instruction(int vcpu); +int xh_vm_emulate_instruction(int vcpu, uint64_t gpa, struct vie *vie, + struct vm_guest_paging *paging, mem_region_read_t memread, + mem_region_write_t memwrite, void *memarg); diff --git a/include/xhyve/vmm/vmm_callout.h b/include/xhyve/vmm/vmm_callout.h new file mode 100644 index 0000000..4068bbd --- /dev/null +++ b/include/xhyve/vmm/vmm_callout.h @@ -0,0 +1,117 @@ +#pragma once + +#include +#include +#include +#include + +#define SBT_1S ((sbintime_t)1 << 32) +#define SBT_1M (SBT_1S * 60) +#define SBT_1MS (SBT_1S / 1000) +#define SBT_1US (SBT_1S / 1000000) +#define SBT_1NS (SBT_1S / 1000000000) +#define SBT_MAX 0x7fffffffffffffffLL + +#define FREQ2BT(freq, bt) \ +{ \ + (bt)->sec = 0; \ + (bt)->frac = ((uint64_t)0x8000000000000000 / (freq)) << 1; \ +} + +#define BT2FREQ(bt) \ + (((uint64_t)0x8000000000000000 + ((bt)->frac >> 2)) / \ + ((bt)->frac >> 1)) + +struct bintime { + uint64_t sec; + uint64_t frac; +}; + +typedef int64_t sbintime_t; + +static inline sbintime_t bttosbt(const struct bintime bt) { + return (sbintime_t) ((bt.sec << 32) + (bt.frac >> 32)); +} + +static inline void bintime_mul(struct bintime *bt, unsigned int x) { + uint64_t p1, p2; + + p1 = (bt->frac & 0xffffffffull) * x; + p2 = (bt->frac >> 32) * x + (p1 >> 32); + bt->sec *= x; + bt->sec += (p2 >> 32); + bt->frac = (p2 << 32) | (p1 & 0xffffffffull); +} + +static inline void bintime_add(struct bintime *_bt, const struct bintime *_bt2) +{ + uint64_t _u; + + _u = _bt->frac; + _bt->frac += _bt2->frac; + if (_u > _bt->frac) + _bt->sec++; + _bt->sec += _bt2->sec; +} + +static inline void bintime_sub(struct bintime *_bt, const struct bintime *_bt2) +{ + uint64_t _u; + + _u = _bt->frac; + _bt->frac -= _bt2->frac; + if (_u < _bt->frac) + _bt->sec--; + _bt->sec -= _bt2->sec; +} + +#define bintime_cmp(a, b, cmp) \ + (((a)->sec == (b)->sec) ? \ + ((a)->frac cmp (b)->frac) : \ + ((a)->sec cmp (b)->sec)) + + +void binuptime(struct bintime *bt); +void getmicrotime(struct timeval *tv); + +static inline sbintime_t sbinuptime(void) { + struct bintime _bt; + + binuptime(&_bt); + return (bttosbt(_bt)); +} + +struct callout { + pthread_cond_t wait; + struct callout *prev; + struct callout *next; + uint64_t timeout; + void *argument; + void (*callout)(void *); + int flags; + int queued; +}; + +#define C_ABSOLUTE 0x0200 /* event time is absolute */ +#define CALLOUT_ACTIVE 0x0002 /* callout is currently active */ +#define CALLOUT_PENDING 0x0004 /* callout is waiting for timeout */ +#define CALLOUT_MPSAFE 0x0008 /* callout handler is mp safe */ +#define CALLOUT_RETURNUNLOCKED 0x0010 /* handler returns with mtx unlocked */ +#define CALLOUT_COMPLETED 0x0020 /* callout thread finished */ +#define CALLOUT_WAITING 0x0040 /* thread waiting for callout to finish */ +//#define CALLOUT_QUEUED 0x0080 + +void callout_system_init(void); +void callout_init(struct callout *c, int mpsafe); +int callout_reset_sbt(struct callout *c, sbintime_t sbt, + sbintime_t precision, void (*ftn)(void *), void *arg, + int flags); + +int callout_stop_safe(struct callout *c, int drain); + +#define callout_active(c) ((c)->flags & CALLOUT_ACTIVE) +#define callout_deactivate(c) ((c)->flags &= ~CALLOUT_ACTIVE) +#define callout_pending(c) ((c)->flags & CALLOUT_PENDING) +#define callout_completed(c) ((c)->flags & CALLOUT_COMPLETED) +#define callout_drain(c) callout_stop_safe(c, 1) +#define callout_stop(c) callout_stop_safe(c, 0) diff --git a/include/xhyve/vmm/vmm_common.h b/include/xhyve/vmm/vmm_common.h new file mode 100644 index 0000000..f5c5326 --- /dev/null +++ b/include/xhyve/vmm/vmm_common.h @@ -0,0 +1,322 @@ +/*- + * Copyright (c) 2011 NetApp, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#pragma once + +#include + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" + +#define VM_MAXCPU 16 /* maximum virtual cpus */ + +enum vm_suspend_how { + VM_SUSPEND_NONE, + VM_SUSPEND_RESET, + VM_SUSPEND_POWEROFF, + VM_SUSPEND_HALT, + VM_SUSPEND_TRIPLEFAULT, + VM_SUSPEND_LAST +}; + +enum vm_cap_type { + VM_CAP_HALT_EXIT, + VM_CAP_MTRAP_EXIT, + VM_CAP_PAUSE_EXIT, + VM_CAP_MAX +}; + +enum vm_intr_trigger { + EDGE_TRIGGER, + LEVEL_TRIGGER +}; + +enum x2apic_state { + X2APIC_DISABLED, + X2APIC_ENABLED, + X2APIC_STATE_LAST +}; + +enum vm_cpu_mode { + CPU_MODE_REAL, + CPU_MODE_PROTECTED, + CPU_MODE_COMPATIBILITY, /* IA-32E mode (CS.L = 0) */ + CPU_MODE_64BIT, /* IA-32E mode (CS.L = 1) */ +}; + +enum vm_paging_mode { + PAGING_MODE_FLAT, + PAGING_MODE_32, + PAGING_MODE_PAE, + PAGING_MODE_64, +}; + +struct seg_desc { + uint64_t base; + uint32_t limit; + uint32_t access; +}; + +#define SEG_DESC_TYPE(access) ((access) & 0x001f) +#define SEG_DESC_DPL(access) (((access) >> 5) & 0x3) +#define SEG_DESC_PRESENT(access) (((access) & 0x0080) ? 1 : 0) +#define SEG_DESC_DEF32(access) (((access) & 0x4000) ? 1 : 0) +#define SEG_DESC_GRANULARITY(access) (((access) & 0x8000) ? 1 : 0) +#define SEG_DESC_UNUSABLE(access) (((access) & 0x10000) ? 1 : 0) + +struct vm_guest_paging { + uint64_t cr3; + int cpl; + enum vm_cpu_mode cpu_mode; + enum vm_paging_mode paging_mode; +}; + +enum vm_reg_name { + VM_REG_GUEST_RAX, + VM_REG_GUEST_RBX, + VM_REG_GUEST_RCX, + VM_REG_GUEST_RDX, + VM_REG_GUEST_RSI, + VM_REG_GUEST_RDI, + VM_REG_GUEST_RBP, + VM_REG_GUEST_R8, + VM_REG_GUEST_R9, + VM_REG_GUEST_R10, + VM_REG_GUEST_R11, + VM_REG_GUEST_R12, + VM_REG_GUEST_R13, + VM_REG_GUEST_R14, + VM_REG_GUEST_R15, + VM_REG_GUEST_CR0, + VM_REG_GUEST_CR3, + VM_REG_GUEST_CR4, + VM_REG_GUEST_DR7, + VM_REG_GUEST_RSP, + VM_REG_GUEST_RIP, + VM_REG_GUEST_RFLAGS, + VM_REG_GUEST_ES, + VM_REG_GUEST_CS, + VM_REG_GUEST_SS, + VM_REG_GUEST_DS, + VM_REG_GUEST_FS, + VM_REG_GUEST_GS, + VM_REG_GUEST_LDTR, + VM_REG_GUEST_TR, + VM_REG_GUEST_IDTR, + VM_REG_GUEST_GDTR, + VM_REG_GUEST_EFER, + VM_REG_GUEST_CR2, + VM_REG_GUEST_PDPTE0, + VM_REG_GUEST_PDPTE1, + VM_REG_GUEST_PDPTE2, + VM_REG_GUEST_PDPTE3, + VM_REG_GUEST_INTR_SHADOW, + VM_REG_LAST +}; + +enum vm_exitcode { + VM_EXITCODE_INOUT, + VM_EXITCODE_VMX, + VM_EXITCODE_BOGUS, + VM_EXITCODE_RDMSR, + VM_EXITCODE_WRMSR, + VM_EXITCODE_HLT, + VM_EXITCODE_MTRAP, + VM_EXITCODE_PAUSE, + VM_EXITCODE_PAGING, + VM_EXITCODE_INST_EMUL, + VM_EXITCODE_SPINUP_AP, + VM_EXITCODE_DEPRECATED1, /* used to be SPINDOWN_CPU */ + VM_EXITCODE_RENDEZVOUS, + VM_EXITCODE_IOAPIC_EOI, + VM_EXITCODE_SUSPENDED, + VM_EXITCODE_INOUT_STR, + VM_EXITCODE_TASK_SWITCH, + VM_EXITCODE_MONITOR, + VM_EXITCODE_MWAIT, + VM_EXITCODE_MAX +}; + +struct vm_inout { + uint16_t bytes:3; /* 1 or 2 or 4 */ + uint16_t in:1; + uint16_t string:1; + uint16_t rep:1; + uint16_t port; + uint32_t eax; /* valid for out */ +}; + +struct vm_inout_str { + struct vm_inout inout; /* must be the first element */ + struct vm_guest_paging paging; + uint64_t rflags; + uint64_t cr0; + uint64_t index; + uint64_t count; /* rep=1 (%rcx), rep=0 (1) */ + int addrsize; + enum vm_reg_name seg_name; + struct seg_desc seg_desc; +}; + +struct vie_op { + uint8_t op_byte; /* actual opcode byte */ + uint8_t op_type; /* type of operation (e.g. MOV) */ + uint16_t op_flags; +}; + +#define VIE_INST_SIZE 15 +struct vie { + uint8_t inst[VIE_INST_SIZE]; /* instruction bytes */ + uint8_t num_valid; /* size of the instruction */ + uint8_t num_processed; + uint8_t addrsize:4, opsize:4; /* address and operand sizes */ + uint8_t rex_w:1, /* REX prefix */ + rex_r:1, + rex_x:1, + rex_b:1, + rex_present:1, + repz_present:1, /* REP/REPE/REPZ prefix */ + repnz_present:1, /* REPNE/REPNZ prefix */ + opsize_override:1, /* Operand size override */ + addrsize_override:1, /* Address size override */ + segment_override:1; /* Segment override */ + uint8_t mod:2, /* ModRM byte */ + reg:4, + rm:4; + uint8_t ss:2, /* SIB byte */ + index:4, + base:4; + uint8_t disp_bytes; + uint8_t imm_bytes; + uint8_t scale; + int base_register; /* VM_REG_GUEST_xyz */ + int index_register; /* VM_REG_GUEST_xyz */ + int segment_register; /* VM_REG_GUEST_xyz */ + int64_t displacement; /* optional addr displacement */ + int64_t immediate; /* optional immediate operand */ + uint8_t decoded; /* set to 1 if successfully decoded */ + struct vie_op op; /* opcode description */ +}; + +enum task_switch_reason { + TSR_CALL, + TSR_IRET, + TSR_JMP, + TSR_IDT_GATE /* task gate in IDT */ +}; + +struct vm_task_switch { + uint16_t tsssel; /* new TSS selector */ + int ext; /* task switch due to external event */ + uint32_t errcode; + int errcode_valid; /* push 'errcode' on the new stack */ + enum task_switch_reason reason; + struct vm_guest_paging paging; +}; + +struct vm_exit { + enum vm_exitcode exitcode; + int inst_length; /* 0 means unknown */ + uint64_t rip; + union { + struct vm_inout inout; + struct vm_inout_str inout_str; + struct { + uint64_t gpa; + int fault_type; + } paging; + struct { + uint64_t gpa; + uint64_t gla; + uint64_t cs_base; + int cs_d; /* CS.D */ + struct vm_guest_paging paging; + struct vie vie; + } inst_emul; + /* + * VMX specific payload. Used when there is no "better" + * exitcode to represent the VM-exit. + */ + struct { + int status; /* vmx inst status */ + /* + * 'exit_reason' and 'exit_qualification' are valid + * only if 'status' is zero. + */ + uint32_t exit_reason; + uint64_t exit_qualification; + /* + * 'inst_error' and 'inst_type' are valid + * only if 'status' is non-zero. + */ + int inst_type; + int inst_error; + } vmx; + struct { + uint32_t code; /* ecx value */ + uint64_t wval; + } msr; + struct { + int vcpu; + uint64_t rip; + } spinup_ap; + struct { + uint64_t rflags; + } hlt; + struct { + int vector; + } ioapic_eoi; + struct { + enum vm_suspend_how how; + } suspended; + struct vm_task_switch task_switch; + } u; +}; + +/* FIXME remove */ +struct vm_memory_segment { + uint64_t gpa; /* in */ + size_t len; +}; + +typedef int (*mem_region_read_t)(void *vm, int cpuid, uint64_t gpa, + uint64_t *rval, int rsize, void *arg); + +typedef int (*mem_region_write_t)(void *vm, int cpuid, uint64_t gpa, + uint64_t wval, int wsize, void *arg); + +uint64_t vie_size2mask(int size); + +int vie_calculate_gla(enum vm_cpu_mode cpu_mode, enum vm_reg_name seg, + struct seg_desc *desc, uint64_t off, int length, int addrsize, int prot, + uint64_t *gla); + +int vie_alignment_check(int cpl, int operand_size, uint64_t cr0, + uint64_t rflags, uint64_t gla); + +#pragma clang diagnostic pop diff --git a/bhyve/spinup_ap.h b/include/xhyve/vmm/vmm_host.h similarity index 80% rename from bhyve/spinup_ap.h rename to include/xhyve/vmm/vmm_host.h index 2749ee9..493d399 100644 --- a/bhyve/spinup_ap.h +++ b/include/xhyve/vmm/vmm_host.h @@ -26,9 +26,19 @@ * $FreeBSD$ */ -#ifndef _SPINUP_AP_H_ -#define _SPINUP_AP_H_ +#pragma once -int spinup_ap(struct vmctx *ctx, int vcpu, int newcpu, uint64_t rip); +#include -#endif +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +struct xsave_limits { + int xsave_enabled; + uint64_t xcr0_allowed; + uint32_t xsave_max_size; +}; +#pragma clang diagnostic pop + +void vmm_host_state_init(void); + +const struct xsave_limits *vmm_get_xsave_limits(void); diff --git a/vmm_instruction_emul.h b/include/xhyve/vmm/vmm_instruction_emul.h similarity index 80% rename from vmm_instruction_emul.h rename to include/xhyve/vmm/vmm_instruction_emul.h index 5e7127f..1097b0c 100644 --- a/vmm_instruction_emul.h +++ b/include/xhyve/vmm/vmm_instruction_emul.h @@ -26,19 +26,10 @@ * $FreeBSD$ */ -#ifndef _VMM_INSTRUCTION_EMUL_H_ -#define _VMM_INSTRUCTION_EMUL_H_ +#pragma once -#include - -/* - * Callback functions to read and write memory regions. - */ -typedef int (*mem_region_read_t)(void *vm, int cpuid, uint64_t gpa, - uint64_t *rval, int rsize, void *arg); - -typedef int (*mem_region_write_t)(void *vm, int cpuid, uint64_t gpa, - uint64_t wval, int wsize, void *arg); +#include +#include /* * Emulate the decoded 'vie' instruction. @@ -58,22 +49,9 @@ int vmm_emulate_instruction(void *vm, int cpuid, uint64_t gpa, struct vie *vie, int vie_update_register(void *vm, int vcpuid, enum vm_reg_name reg, uint64_t val, int size); -/* - * Returns 1 if an alignment check exception should be injected and 0 otherwise. - */ -int vie_alignment_check(int cpl, int operand_size, uint64_t cr0, - uint64_t rflags, uint64_t gla); - /* Returns 1 if the 'gla' is not canonical and 0 otherwise. */ int vie_canonical_check(enum vm_cpu_mode cpu_mode, uint64_t gla); -uint64_t vie_size2mask(int size); - -int vie_calculate_gla(enum vm_cpu_mode cpu_mode, enum vm_reg_name seg, - struct seg_desc *desc, uint64_t off, int length, int addrsize, int prot, - uint64_t *gla); - -#ifdef _KERNEL /* * APIs to fetch and decode the instruction from nested page fault handler. * @@ -111,6 +89,3 @@ void vie_init(struct vie *vie, const char *inst_bytes, int inst_length); #define VIE_INVALID_GLA (1UL << 63) /* a non-canonical address */ int vmm_decode_instruction(struct vm *vm, int cpuid, uint64_t gla, enum vm_cpu_mode cpu_mode, int csd, struct vie *vie); -#endif /* _KERNEL */ - -#endif /* _VMM_INSTRUCTION_EMUL_H_ */ diff --git a/vmm/vmm_ioport.h b/include/xhyve/vmm/vmm_ioport.h similarity index 95% rename from vmm/vmm_ioport.h rename to include/xhyve/vmm/vmm_ioport.h index ba51989..e6f1675 100644 --- a/vmm/vmm_ioport.h +++ b/include/xhyve/vmm/vmm_ioport.h @@ -26,12 +26,15 @@ * $FreeBSD$ */ -#ifndef _VMM_IOPORT_H_ -#define _VMM_IOPORT_H_ +#pragma once + +#include +#include + +struct vm; +struct vm_exit; typedef int (*ioport_handler_func_t)(struct vm *vm, int vcpuid, bool in, int port, int bytes, uint32_t *val); int vm_handle_inout(struct vm *vm, int vcpuid, struct vm_exit *vme, bool *retu); - -#endif /* _VMM_IOPORT_H_ */ diff --git a/vmm/vmm_ktr.h b/include/xhyve/vmm/vmm_ktr.h similarity index 69% rename from vmm/vmm_ktr.h rename to include/xhyve/vmm/vmm_ktr.h index 61ff53f..acfe5b6 100644 --- a/vmm/vmm_ktr.h +++ b/include/xhyve/vmm/vmm_ktr.h @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,44 +27,46 @@ * $FreeBSD$ */ -#ifndef _VMM_KTR_H_ -#define _VMM_KTR_H_ +#pragma once -#include -#include +#include -#ifndef KTR_VMM -#define KTR_VMM KTR_GEN +#ifdef XHYVE_CONFIG_TRACE +#define vmmtrace printf +#else +#define vmmtrace if (0) printf #endif +struct vm; +extern const char *vm_name(struct vm *vm); + #define VCPU_CTR0(vm, vcpuid, format) \ -CTR2(KTR_VMM, "vm %s[%d]: " format, vm_name((vm)), (vcpuid)) +vmmtrace("vm %s[%d]: " format "\n", vm_name((vm)), (vcpuid)) #define VCPU_CTR1(vm, vcpuid, format, p1) \ -CTR3(KTR_VMM, "vm %s[%d]: " format, vm_name((vm)), (vcpuid), (p1)) +vmmtrace("vm %s[%d]: " format "\n", vm_name((vm)), (vcpuid), (p1)) #define VCPU_CTR2(vm, vcpuid, format, p1, p2) \ -CTR4(KTR_VMM, "vm %s[%d]: " format, vm_name((vm)), (vcpuid), (p1), (p2)) +vmmtrace("vm %s[%d]: " format "\n", vm_name((vm)), (vcpuid), (p1), (p2)) #define VCPU_CTR3(vm, vcpuid, format, p1, p2, p3) \ -CTR5(KTR_VMM, "vm %s[%d]: " format, vm_name((vm)), (vcpuid), (p1), (p2), (p3)) +vmmtrace("vm %s[%d]: " format "\n", vm_name((vm)), (vcpuid), (p1), (p2), (p3)) #define VCPU_CTR4(vm, vcpuid, format, p1, p2, p3, p4) \ -CTR6(KTR_VMM, "vm %s[%d]: " format, vm_name((vm)), (vcpuid), \ +vmmtrace("vm %s[%d]: " format "\n", vm_name((vm)), (vcpuid), \ (p1), (p2), (p3), (p4)) #define VM_CTR0(vm, format) \ -CTR1(KTR_VMM, "vm %s: " format, vm_name((vm))) +vmmtrace("vm %s: " format "\n", vm_name((vm))) #define VM_CTR1(vm, format, p1) \ -CTR2(KTR_VMM, "vm %s: " format, vm_name((vm)), (p1)) +vmmtrace("vm %s: " format "\n", vm_name((vm)), (p1)) #define VM_CTR2(vm, format, p1, p2) \ -CTR3(KTR_VMM, "vm %s: " format, vm_name((vm)), (p1), (p2)) +vmmtrace("vm %s: " format "\n", vm_name((vm)), (p1), (p2)) #define VM_CTR3(vm, format, p1, p2, p3) \ -CTR4(KTR_VMM, "vm %s: " format, vm_name((vm)), (p1), (p2), (p3)) +vmmtrace("vm %s: " format "\n", vm_name((vm)), (p1), (p2), (p3)) #define VM_CTR4(vm, format, p1, p2, p3, p4) \ -CTR5(KTR_VMM, "vm %s: " format, vm_name((vm)), (p1), (p2), (p3), (p4)) -#endif +vmmtrace("vm %s: " format "\n", vm_name((vm)), (p1), (p2), (p3), (p4)) diff --git a/vmm/vmm_lapic.h b/include/xhyve/vmm/vmm_lapic.h similarity index 95% rename from vmm/vmm_lapic.h rename to include/xhyve/vmm/vmm_lapic.h index 88fa948..e7df042 100644 --- a/vmm/vmm_lapic.h +++ b/include/xhyve/vmm/vmm_lapic.h @@ -26,12 +26,15 @@ * $FreeBSD$ */ -#ifndef _VMM_LAPIC_H_ -#define _VMM_LAPIC_H_ +#pragma once + +#include +#include +#include struct vm; -boolean_t lapic_msr(u_int num); +bool lapic_msr(u_int num); int lapic_rdmsr(struct vm *vm, int cpu, u_int msr, uint64_t *rval, bool *retu); int lapic_wrmsr(struct vm *vm, int cpu, u_int msr, uint64_t wval, @@ -71,5 +74,3 @@ lapic_intr_edge(struct vm *vm, int cpu, int vector) int lapic_set_local_intr(struct vm *vm, int cpu, int vector); int lapic_intr_msi(struct vm *vm, uint64_t addr, uint64_t msg); - -#endif diff --git a/vmm/vmm_mem.h b/include/xhyve/vmm/vmm_mem.h similarity index 75% rename from vmm/vmm_mem.h rename to include/xhyve/vmm/vmm_mem.h index a375070..6efbcf4 100644 --- a/vmm/vmm_mem.h +++ b/include/xhyve/vmm/vmm_mem.h @@ -26,18 +26,13 @@ * $FreeBSD$ */ -#ifndef _VMM_MEM_H_ -#define _VMM_MEM_H_ +#pragma once + +#include +#include struct vmspace; -struct vm_object; -int vmm_mem_init(void); -struct vm_object *vmm_mem_alloc(struct vmspace *, vm_paddr_t gpa, size_t size); -struct vm_object *vmm_mmio_alloc(struct vmspace *, vm_paddr_t gpa, size_t len, - vm_paddr_t hpa); -void vmm_mem_free(struct vmspace *, vm_paddr_t gpa, size_t size); -void vmm_mmio_free(struct vmspace *, vm_paddr_t gpa, size_t size); -vm_paddr_t vmm_mem_maxaddr(void); - -#endif +int vmm_mem_init(void); +void *vmm_mem_alloc(uint64_t gpa, size_t size); +void vmm_mem_free(uint64_t gpa, size_t size, void *object); diff --git a/vmm/vmm_stat.h b/include/xhyve/vmm/vmm_stat.h similarity index 88% rename from vmm/vmm_stat.h rename to include/xhyve/vmm/vmm_stat.h index 1640ba3..8169adb 100644 --- a/vmm/vmm_stat.h +++ b/include/xhyve/vmm/vmm_stat.h @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -29,8 +30,9 @@ * $FreeBSD$ */ -#ifndef _VMM_STAT_H_ -#define _VMM_STAT_H_ +#pragma once + +#include struct vm; @@ -46,6 +48,8 @@ struct vmm_stat_type; typedef void (*vmm_stat_func_t)(struct vm *vm, int vcpu, struct vmm_stat_type *stat); +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct vmm_stat_type { int index; /* position in the stats buffer */ int nelems; /* standalone or array */ @@ -53,14 +57,16 @@ struct vmm_stat_type { vmm_stat_func_t func; enum vmm_stat_scope scope; }; +#pragma clang diagnostic pop void vmm_stat_register(void *arg); #define VMM_STAT_FDEFINE(type, nelems, desc, func, scope) \ struct vmm_stat_type type[1] = { \ { -1, nelems, desc, func, scope } \ - }; \ - SYSINIT(type##_stat, SI_SUB_KLD, SI_ORDER_ANY, vmm_stat_register, type) + } + //}; \ + // SYSINIT(type##_stat, SI_SUB_KLD, SI_ORDER_ANY, vmm_stat_register, type) #define VMM_STAT_DEFINE(type, nelems, desc, scope) \ VMM_STAT_FDEFINE(type, nelems, desc, NULL, scope) @@ -72,8 +78,6 @@ void vmm_stat_register(void *arg); VMM_STAT_DEFINE(type, 1, desc, VMM_STAT_SCOPE_ANY) #define VMM_STAT_INTEL(type, desc) \ VMM_STAT_DEFINE(type, 1, desc, VMM_STAT_SCOPE_INTEL) -#define VMM_STAT_AMD(type, desc) \ - VMM_STAT_DEFINE(type, 1, desc, VMM_STAT_SCOPE_AMD) #define VMM_STAT_FUNC(type, desc, func) \ VMM_STAT_FDEFINE(type, 1, desc, func, VMM_STAT_SCOPE_ANY) @@ -95,13 +99,19 @@ static void __inline vmm_stat_array_incr(struct vm *vm, int vcpu, struct vmm_stat_type *vst, int statidx, uint64_t x) { -#ifdef VMM_KEEP_STATS +#ifdef XHYVE_CONFIG_STATS uint64_t *stats; stats = vcpu_stats(vm, vcpu); if (vst->index >= 0 && statidx < vst->nelems) stats[vst->index + statidx] += x; +#else + (void) vm; + (void) vcpu; + (void) vst; + (void) statidx; + (void) x; #endif } @@ -109,13 +119,19 @@ static void __inline vmm_stat_array_set(struct vm *vm, int vcpu, struct vmm_stat_type *vst, int statidx, uint64_t val) { -#ifdef VMM_KEEP_STATS +#ifdef XHYVE_CONFIG_STATS uint64_t *stats; stats = vcpu_stats(vm, vcpu); if (vst->index >= 0 && statidx < vst->nelems) stats[vst->index + statidx] = val; +#else + (void) vm; + (void) vcpu; + (void) vst; + (void) statidx; + (void) val; #endif } @@ -123,8 +139,13 @@ static void __inline vmm_stat_incr(struct vm *vm, int vcpu, struct vmm_stat_type *vst, uint64_t x) { -#ifdef VMM_KEEP_STATS +#ifdef XHYVE_CONFIG_STATS vmm_stat_array_incr(vm, vcpu, vst, 0, x); +#else + (void) vm; + (void) vcpu; + (void) vst; + (void) x; #endif } @@ -132,8 +153,13 @@ static void __inline vmm_stat_set(struct vm *vm, int vcpu, struct vmm_stat_type *vst, uint64_t val) { -#ifdef VMM_KEEP_STATS +#ifdef XHYVE_CONFIG_STATS vmm_stat_array_set(vm, vcpu, vst, 0, val); +#else + (void) vm; + (void) vcpu; + (void) vst; + (void) val; #endif } @@ -157,4 +183,3 @@ VMM_STAT_DECLARE(VMEXIT_ASTPENDING); VMM_STAT_DECLARE(VMEXIT_USERSPACE); VMM_STAT_DECLARE(VMEXIT_RENDEZVOUS); VMM_STAT_DECLARE(VMEXIT_EXCEPTION); -#endif diff --git a/vmm/vmm_util.h b/include/xhyve/vmm/vmm_util.h similarity index 87% rename from vmm/vmm_util.h rename to include/xhyve/vmm/vmm_util.h index 7f82332..ca71faf 100644 --- a/vmm/vmm_util.h +++ b/include/xhyve/vmm/vmm_util.h @@ -26,15 +26,8 @@ * $FreeBSD$ */ -#ifndef _VMM_UTIL_H_ -#define _VMM_UTIL_H_ +#pragma once struct trapframe; -boolean_t vmm_is_intel(void); -boolean_t vmm_is_amd(void); -boolean_t vmm_supports_1G_pages(void); - -void dump_trapframe(struct trapframe *tf); - -#endif +void dump_trapframe(struct trapframe *tf); diff --git a/vmm/x86.h b/include/xhyve/vmm/x86.h similarity index 87% rename from vmm/x86.h rename to include/xhyve/vmm/x86.h index 6f99d52..9b76974 100644 --- a/vmm/x86.h +++ b/include/xhyve/vmm/x86.h @@ -26,8 +26,9 @@ * $FreeBSD$ */ -#ifndef _X86_H_ -#define _X86_H_ +#pragma once + +#include #define CPUID_0000_0000 (0x0) #define CPUID_0000_0001 (0x1) @@ -61,18 +62,3 @@ int x86_emulate_cpuid(struct vm *vm, int vcpu_id, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx); - -enum vm_cpuid_capability { - VCC_NONE, - VCC_NO_EXECUTE, - VCC_FFXSR, - VCC_TCE, - VCC_LAST -}; - -/* - * Return 'true' if the capability 'cap' is enabled in this virtual cpu - * and 'false' otherwise. - */ -bool vm_cpuid_capability(struct vm *vm, int vcpuid, enum vm_cpuid_capability); -#endif diff --git a/vmm/io/ppt.h b/include/xhyve/xhyve.h similarity index 54% rename from vmm/io/ppt.h rename to include/xhyve/xhyve.h index 8078896..4f85664 100644 --- a/vmm/io/ppt.h +++ b/include/xhyve/xhyve.h @@ -26,29 +26,55 @@ * $FreeBSD$ */ -#ifndef _IO_PPT_H_ -#define _IO_PPT_H_ +#pragma once -int ppt_unassign_all(struct vm *vm); -int ppt_map_mmio(struct vm *vm, int bus, int slot, int func, - vm_paddr_t gpa, size_t len, vm_paddr_t hpa); -int ppt_setup_msi(struct vm *vm, int vcpu, int bus, int slot, int func, - uint64_t addr, uint64_t msg, int numvec); -int ppt_setup_msix(struct vm *vm, int vcpu, int bus, int slot, int func, - int idx, uint64_t addr, uint64_t msg, uint32_t vector_control); -int ppt_assigned_devices(struct vm *vm); -boolean_t ppt_is_mmio(struct vm *vm, vm_paddr_t gpa); +#include +#include -/* - * Returns the number of devices sequestered by the ppt driver for assignment - * to virtual machines. - */ -int ppt_avail_devices(void); - -/* - * The following functions should never be called directly. - * Use 'vm_assign_pptdev()' and 'vm_unassign_pptdev()' instead. - */ -int ppt_assign_device(struct vm *vm, int bus, int slot, int func); -int ppt_unassign_device(struct vm *vm, int bus, int slot, int func); +#ifndef CTASSERT /* Allow lint to override */ +#define CTASSERT(x) _CTASSERT(x, __LINE__) +#define _CTASSERT(x, y) __CTASSERT(x, y) +#define __CTASSERT(x, y) typedef char __assert ## y[(x) ? 1 : -1] #endif + +#define VMEXIT_CONTINUE (0) +#define VMEXIT_ABORT (-1) + +extern int guest_ncpus; +extern char *guest_uuid_str; +extern char *vmname; + +void xh_vm_inject_fault(int vcpu, int vector, int errcode_valid, + uint32_t errcode); + +static __inline void +vm_inject_ud(int vcpuid) +{ + xh_vm_inject_fault(vcpuid, IDT_UD, 0, 0); +} + +static __inline void +vm_inject_gp(int vcpuid) +{ + xh_vm_inject_fault(vcpuid, IDT_GP, 1, 0); +} + +static __inline void +vm_inject_ac(int vcpuid, uint32_t errcode) +{ + xh_vm_inject_fault(vcpuid, IDT_AC, 1, errcode); +} + +static __inline void +vm_inject_ss(int vcpuid, uint32_t errcode) +{ + xh_vm_inject_fault(vcpuid, IDT_SS, 1, errcode); +} + +void *paddr_guest2host(uintptr_t addr, size_t len); + +void vcpu_set_capabilities(int cpu); +void vcpu_add(int fromcpu, int newcpu, uint64_t rip); +int fbsdrun_vmexit_on_hlt(void); +int fbsdrun_vmexit_on_pause(void); +int fbsdrun_virtio_msix(void); diff --git a/bhyve/xmsr.h b/include/xhyve/xmsr.h similarity index 87% rename from bhyve/xmsr.h rename to include/xhyve/xmsr.h index bcf65b7..ff793ff 100644 --- a/bhyve/xmsr.h +++ b/include/xhyve/xmsr.h @@ -26,11 +26,10 @@ * $FreeBSD$ */ -#ifndef _XMSR_H_ -#define _XMSR_H_ +#pragma once + +#include int init_msr(void); -int emulate_wrmsr(struct vmctx *ctx, int vcpu, uint32_t code, uint64_t val); -int emulate_rdmsr(struct vmctx *ctx, int vcpu, uint32_t code, uint64_t *val); - -#endif +int emulate_wrmsr(int vcpu, uint32_t code, uint64_t val); +int emulate_rdmsr(int vcpu, uint32_t code, uint64_t *val); diff --git a/libvmmapi/Makefile b/libvmmapi/Makefile deleted file mode 100644 index 26cf86f..0000000 --- a/libvmmapi/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -# $FreeBSD$ - -LIB= vmmapi -SRCS= vmmapi.c vmmapi_freebsd.c -INCS= vmmapi.h - -WARNS?= 2 - -LIBADD= util - -CFLAGS+= -I${.CURDIR} - -.include diff --git a/libvmmapi/vmmapi.c b/libvmmapi/vmmapi.c deleted file mode 100644 index 1e6e627..0000000 --- a/libvmmapi/vmmapi.c +++ /dev/null @@ -1,1201 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include -#include - -#include "vmmapi.h" - -#define MB (1024 * 1024UL) -#define GB (1024 * 1024 * 1024UL) - -struct vmctx { - int fd; - uint32_t lowmem_limit; - enum vm_mmap_style vms; - int memflags; - size_t lowmem; - char *lowmem_addr; - size_t highmem; - char *highmem_addr; - char *name; -}; - -#define CREATE(x) sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x))) -#define DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x))) - -static int -vm_device_open(const char *name) -{ - int fd, len; - char *vmfile; - - len = strlen("/dev/vmm/") + strlen(name) + 1; - vmfile = malloc(len); - assert(vmfile != NULL); - snprintf(vmfile, len, "/dev/vmm/%s", name); - - /* Open the device file */ - fd = open(vmfile, O_RDWR, 0); - - free(vmfile); - return (fd); -} - -int -vm_create(const char *name) -{ - - return (CREATE((char *)name)); -} - -struct vmctx * -vm_open(const char *name) -{ - struct vmctx *vm; - - vm = malloc(sizeof(struct vmctx) + strlen(name) + 1); - assert(vm != NULL); - - vm->fd = -1; - vm->memflags = 0; - vm->lowmem_limit = 3 * GB; - vm->name = (char *)(vm + 1); - strcpy(vm->name, name); - - if ((vm->fd = vm_device_open(vm->name)) < 0) - goto err; - - return (vm); -err: - vm_destroy(vm); - return (NULL); -} - -void -vm_destroy(struct vmctx *vm) -{ - assert(vm != NULL); - - if (vm->fd >= 0) - close(vm->fd); - DESTROY(vm->name); - - free(vm); -} - -int -vm_parse_memsize(const char *optarg, size_t *ret_memsize) -{ - char *endptr; - size_t optval; - int error; - - optval = strtoul(optarg, &endptr, 0); - if (*optarg != '\0' && *endptr == '\0') { - /* - * For the sake of backward compatibility if the memory size - * specified on the command line is less than a megabyte then - * it is interpreted as being in units of MB. - */ - if (optval < MB) - optval *= MB; - *ret_memsize = optval; - error = 0; - } else - error = expand_number(optarg, ret_memsize); - - return (error); -} - -int -vm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, size_t *ret_len, - int *wired) -{ - int error; - struct vm_memory_segment seg; - - bzero(&seg, sizeof(seg)); - seg.gpa = gpa; - error = ioctl(ctx->fd, VM_GET_MEMORY_SEG, &seg); - *ret_len = seg.len; - if (wired != NULL) - *wired = seg.wired; - return (error); -} - -uint32_t -vm_get_lowmem_limit(struct vmctx *ctx) -{ - - return (ctx->lowmem_limit); -} - -void -vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit) -{ - - ctx->lowmem_limit = limit; -} - -void -vm_set_memflags(struct vmctx *ctx, int flags) -{ - - ctx->memflags = flags; -} - -static int -setup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char **addr) -{ - int error, mmap_flags; - struct vm_memory_segment seg; - - /* - * Create and optionally map 'len' bytes of memory at guest - * physical address 'gpa' - */ - bzero(&seg, sizeof(seg)); - seg.gpa = gpa; - seg.len = len; - error = ioctl(ctx->fd, VM_MAP_MEMORY, &seg); - if (error == 0 && addr != NULL) { - mmap_flags = MAP_SHARED; - if ((ctx->memflags & VM_MEM_F_INCORE) == 0) - mmap_flags |= MAP_NOCORE; - *addr = mmap(NULL, len, PROT_READ | PROT_WRITE, mmap_flags, - ctx->fd, gpa); - } - return (error); -} - -int -vm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms) -{ - char **addr; - int error; - - /* XXX VM_MMAP_SPARSE not implemented yet */ - assert(vms == VM_MMAP_NONE || vms == VM_MMAP_ALL); - ctx->vms = vms; - - /* - * If 'memsize' cannot fit entirely in the 'lowmem' segment then - * create another 'highmem' segment above 4GB for the remainder. - */ - if (memsize > ctx->lowmem_limit) { - ctx->lowmem = ctx->lowmem_limit; - ctx->highmem = memsize - ctx->lowmem; - } else { - ctx->lowmem = memsize; - ctx->highmem = 0; - } - - if (ctx->lowmem > 0) { - addr = (vms == VM_MMAP_ALL) ? &ctx->lowmem_addr : NULL; - error = setup_memory_segment(ctx, 0, ctx->lowmem, addr); - if (error) - return (error); - } - - if (ctx->highmem > 0) { - addr = (vms == VM_MMAP_ALL) ? &ctx->highmem_addr : NULL; - error = setup_memory_segment(ctx, 4*GB, ctx->highmem, addr); - if (error) - return (error); - } - - return (0); -} - -void * -vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len) -{ - - /* XXX VM_MMAP_SPARSE not implemented yet */ - assert(ctx->vms == VM_MMAP_ALL); - - if (gaddr < ctx->lowmem && gaddr + len <= ctx->lowmem) - return ((void *)(ctx->lowmem_addr + gaddr)); - - if (gaddr >= 4*GB) { - gaddr -= 4*GB; - if (gaddr < ctx->highmem && gaddr + len <= ctx->highmem) - return ((void *)(ctx->highmem_addr + gaddr)); - } - - return (NULL); -} - -size_t -vm_get_lowmem_size(struct vmctx *ctx) -{ - - return (ctx->lowmem); -} - -size_t -vm_get_highmem_size(struct vmctx *ctx) -{ - - return (ctx->highmem); -} - -int -vm_set_desc(struct vmctx *ctx, int vcpu, int reg, - uint64_t base, uint32_t limit, uint32_t access) -{ - int error; - struct vm_seg_desc vmsegdesc; - - bzero(&vmsegdesc, sizeof(vmsegdesc)); - vmsegdesc.cpuid = vcpu; - vmsegdesc.regnum = reg; - vmsegdesc.desc.base = base; - vmsegdesc.desc.limit = limit; - vmsegdesc.desc.access = access; - - error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc); - return (error); -} - -int -vm_get_desc(struct vmctx *ctx, int vcpu, int reg, - uint64_t *base, uint32_t *limit, uint32_t *access) -{ - int error; - struct vm_seg_desc vmsegdesc; - - bzero(&vmsegdesc, sizeof(vmsegdesc)); - vmsegdesc.cpuid = vcpu; - vmsegdesc.regnum = reg; - - error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc); - if (error == 0) { - *base = vmsegdesc.desc.base; - *limit = vmsegdesc.desc.limit; - *access = vmsegdesc.desc.access; - } - return (error); -} - -int -vm_get_seg_desc(struct vmctx *ctx, int vcpu, int reg, struct seg_desc *seg_desc) -{ - int error; - - error = vm_get_desc(ctx, vcpu, reg, &seg_desc->base, &seg_desc->limit, - &seg_desc->access); - return (error); -} - -int -vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val) -{ - int error; - struct vm_register vmreg; - - bzero(&vmreg, sizeof(vmreg)); - vmreg.cpuid = vcpu; - vmreg.regnum = reg; - vmreg.regval = val; - - error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg); - return (error); -} - -int -vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val) -{ - int error; - struct vm_register vmreg; - - bzero(&vmreg, sizeof(vmreg)); - vmreg.cpuid = vcpu; - vmreg.regnum = reg; - - error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg); - *ret_val = vmreg.regval; - return (error); -} - -int -vm_run(struct vmctx *ctx, int vcpu, struct vm_exit *vmexit) -{ - int error; - struct vm_run vmrun; - - bzero(&vmrun, sizeof(vmrun)); - vmrun.cpuid = vcpu; - - error = ioctl(ctx->fd, VM_RUN, &vmrun); - bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit)); - return (error); -} - -int -vm_suspend(struct vmctx *ctx, enum vm_suspend_how how) -{ - struct vm_suspend vmsuspend; - - bzero(&vmsuspend, sizeof(vmsuspend)); - vmsuspend.how = how; - return (ioctl(ctx->fd, VM_SUSPEND, &vmsuspend)); -} - -int -vm_reinit(struct vmctx *ctx) -{ - - return (ioctl(ctx->fd, VM_REINIT, 0)); -} - -int -vm_inject_exception(struct vmctx *ctx, int vcpu, int vector, int errcode_valid, - uint32_t errcode, int restart_instruction) -{ - struct vm_exception exc; - - exc.cpuid = vcpu; - exc.vector = vector; - exc.error_code = errcode; - exc.error_code_valid = errcode_valid; - exc.restart_instruction = restart_instruction; - - return (ioctl(ctx->fd, VM_INJECT_EXCEPTION, &exc)); -} - -int -vm_apicid2vcpu(struct vmctx *ctx, int apicid) -{ - /* - * The apic id associated with the 'vcpu' has the same numerical value - * as the 'vcpu' itself. - */ - return (apicid); -} - -int -vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector) -{ - struct vm_lapic_irq vmirq; - - bzero(&vmirq, sizeof(vmirq)); - vmirq.cpuid = vcpu; - vmirq.vector = vector; - - return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq)); -} - -int -vm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector) -{ - struct vm_lapic_irq vmirq; - - bzero(&vmirq, sizeof(vmirq)); - vmirq.cpuid = vcpu; - vmirq.vector = vector; - - return (ioctl(ctx->fd, VM_LAPIC_LOCAL_IRQ, &vmirq)); -} - -int -vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg) -{ - struct vm_lapic_msi vmmsi; - - bzero(&vmmsi, sizeof(vmmsi)); - vmmsi.addr = addr; - vmmsi.msg = msg; - - return (ioctl(ctx->fd, VM_LAPIC_MSI, &vmmsi)); -} - -int -vm_ioapic_assert_irq(struct vmctx *ctx, int irq) -{ - struct vm_ioapic_irq ioapic_irq; - - bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq)); - ioapic_irq.irq = irq; - - return (ioctl(ctx->fd, VM_IOAPIC_ASSERT_IRQ, &ioapic_irq)); -} - -int -vm_ioapic_deassert_irq(struct vmctx *ctx, int irq) -{ - struct vm_ioapic_irq ioapic_irq; - - bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq)); - ioapic_irq.irq = irq; - - return (ioctl(ctx->fd, VM_IOAPIC_DEASSERT_IRQ, &ioapic_irq)); -} - -int -vm_ioapic_pulse_irq(struct vmctx *ctx, int irq) -{ - struct vm_ioapic_irq ioapic_irq; - - bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq)); - ioapic_irq.irq = irq; - - return (ioctl(ctx->fd, VM_IOAPIC_PULSE_IRQ, &ioapic_irq)); -} - -int -vm_ioapic_pincount(struct vmctx *ctx, int *pincount) -{ - - return (ioctl(ctx->fd, VM_IOAPIC_PINCOUNT, pincount)); -} - -int -vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq) -{ - struct vm_isa_irq isa_irq; - - bzero(&isa_irq, sizeof(struct vm_isa_irq)); - isa_irq.atpic_irq = atpic_irq; - isa_irq.ioapic_irq = ioapic_irq; - - return (ioctl(ctx->fd, VM_ISA_ASSERT_IRQ, &isa_irq)); -} - -int -vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq) -{ - struct vm_isa_irq isa_irq; - - bzero(&isa_irq, sizeof(struct vm_isa_irq)); - isa_irq.atpic_irq = atpic_irq; - isa_irq.ioapic_irq = ioapic_irq; - - return (ioctl(ctx->fd, VM_ISA_DEASSERT_IRQ, &isa_irq)); -} - -int -vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq) -{ - struct vm_isa_irq isa_irq; - - bzero(&isa_irq, sizeof(struct vm_isa_irq)); - isa_irq.atpic_irq = atpic_irq; - isa_irq.ioapic_irq = ioapic_irq; - - return (ioctl(ctx->fd, VM_ISA_PULSE_IRQ, &isa_irq)); -} - -int -vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq, - enum vm_intr_trigger trigger) -{ - struct vm_isa_irq_trigger isa_irq_trigger; - - bzero(&isa_irq_trigger, sizeof(struct vm_isa_irq_trigger)); - isa_irq_trigger.atpic_irq = atpic_irq; - isa_irq_trigger.trigger = trigger; - - return (ioctl(ctx->fd, VM_ISA_SET_IRQ_TRIGGER, &isa_irq_trigger)); -} - -int -vm_inject_nmi(struct vmctx *ctx, int vcpu) -{ - struct vm_nmi vmnmi; - - bzero(&vmnmi, sizeof(vmnmi)); - vmnmi.cpuid = vcpu; - - return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi)); -} - -static struct { - const char *name; - int type; -} capstrmap[] = { - { "hlt_exit", VM_CAP_HALT_EXIT }, - { "mtrap_exit", VM_CAP_MTRAP_EXIT }, - { "pause_exit", VM_CAP_PAUSE_EXIT }, - { "unrestricted_guest", VM_CAP_UNRESTRICTED_GUEST }, - { "enable_invpcid", VM_CAP_ENABLE_INVPCID }, - { 0 } -}; - -int -vm_capability_name2type(const char *capname) -{ - int i; - - for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) { - if (strcmp(capstrmap[i].name, capname) == 0) - return (capstrmap[i].type); - } - - return (-1); -} - -const char * -vm_capability_type2name(int type) -{ - int i; - - for (i = 0; capstrmap[i].name != NULL; i++) { - if (capstrmap[i].type == type) - return (capstrmap[i].name); - } - - return (NULL); -} - -int -vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, - int *retval) -{ - int error; - struct vm_capability vmcap; - - bzero(&vmcap, sizeof(vmcap)); - vmcap.cpuid = vcpu; - vmcap.captype = cap; - - error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap); - *retval = vmcap.capval; - return (error); -} - -int -vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val) -{ - struct vm_capability vmcap; - - bzero(&vmcap, sizeof(vmcap)); - vmcap.cpuid = vcpu; - vmcap.captype = cap; - vmcap.capval = val; - - return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap)); -} - -int -vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func) -{ - struct vm_pptdev pptdev; - - bzero(&pptdev, sizeof(pptdev)); - pptdev.bus = bus; - pptdev.slot = slot; - pptdev.func = func; - - return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev)); -} - -int -vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func) -{ - struct vm_pptdev pptdev; - - bzero(&pptdev, sizeof(pptdev)); - pptdev.bus = bus; - pptdev.slot = slot; - pptdev.func = func; - - return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev)); -} - -int -vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func, - vm_paddr_t gpa, size_t len, vm_paddr_t hpa) -{ - struct vm_pptdev_mmio pptmmio; - - bzero(&pptmmio, sizeof(pptmmio)); - pptmmio.bus = bus; - pptmmio.slot = slot; - pptmmio.func = func; - pptmmio.gpa = gpa; - pptmmio.len = len; - pptmmio.hpa = hpa; - - return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio)); -} - -int -vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func, - uint64_t addr, uint64_t msg, int numvec) -{ - struct vm_pptdev_msi pptmsi; - - bzero(&pptmsi, sizeof(pptmsi)); - pptmsi.vcpu = vcpu; - pptmsi.bus = bus; - pptmsi.slot = slot; - pptmsi.func = func; - pptmsi.msg = msg; - pptmsi.addr = addr; - pptmsi.numvec = numvec; - - return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi)); -} - -int -vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func, - int idx, uint64_t addr, uint64_t msg, uint32_t vector_control) -{ - struct vm_pptdev_msix pptmsix; - - bzero(&pptmsix, sizeof(pptmsix)); - pptmsix.vcpu = vcpu; - pptmsix.bus = bus; - pptmsix.slot = slot; - pptmsix.func = func; - pptmsix.idx = idx; - pptmsix.msg = msg; - pptmsix.addr = addr; - pptmsix.vector_control = vector_control; - - return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix); -} - -uint64_t * -vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv, - int *ret_entries) -{ - int error; - - static struct vm_stats vmstats; - - vmstats.cpuid = vcpu; - - error = ioctl(ctx->fd, VM_STATS, &vmstats); - if (error == 0) { - if (ret_entries) - *ret_entries = vmstats.num_entries; - if (ret_tv) - *ret_tv = vmstats.tv; - return (vmstats.statbuf); - } else - return (NULL); -} - -const char * -vm_get_stat_desc(struct vmctx *ctx, int index) -{ - static struct vm_stat_desc statdesc; - - statdesc.index = index; - if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0) - return (statdesc.desc); - else - return (NULL); -} - -int -vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state) -{ - int error; - struct vm_x2apic x2apic; - - bzero(&x2apic, sizeof(x2apic)); - x2apic.cpuid = vcpu; - - error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic); - *state = x2apic.state; - return (error); -} - -int -vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state) -{ - int error; - struct vm_x2apic x2apic; - - bzero(&x2apic, sizeof(x2apic)); - x2apic.cpuid = vcpu; - x2apic.state = state; - - error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic); - - return (error); -} - -/* - * From Intel Vol 3a: - * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT - */ -int -vcpu_reset(struct vmctx *vmctx, int vcpu) -{ - int error; - uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx; - uint32_t desc_access, desc_limit; - uint16_t sel; - - zero = 0; - - rflags = 0x2; - error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags); - if (error) - goto done; - - rip = 0xfff0; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0) - goto done; - - cr0 = CR0_NE; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0) - goto done; - - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0) - goto done; - - cr4 = 0; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0) - goto done; - - /* - * CS: present, r/w, accessed, 16-bit, byte granularity, usable - */ - desc_base = 0xffff0000; - desc_limit = 0xffff; - desc_access = 0x0093; - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - sel = 0xf000; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0) - goto done; - - /* - * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity - */ - desc_base = 0; - desc_limit = 0xffff; - desc_access = 0x0093; - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - sel = 0; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0) - goto done; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0) - goto done; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0) - goto done; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0) - goto done; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0) - goto done; - - /* General purpose registers */ - rdx = 0xf00; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0) - goto done; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0) - goto done; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0) - goto done; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0) - goto done; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0) - goto done; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0) - goto done; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0) - goto done; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0) - goto done; - - /* GDTR, IDTR */ - desc_base = 0; - desc_limit = 0xffff; - desc_access = 0; - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR, - desc_base, desc_limit, desc_access); - if (error != 0) - goto done; - - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR, - desc_base, desc_limit, desc_access); - if (error != 0) - goto done; - - /* TR */ - desc_base = 0; - desc_limit = 0xffff; - desc_access = 0x0000008b; - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access); - if (error) - goto done; - - sel = 0; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0) - goto done; - - /* LDTR */ - desc_base = 0; - desc_limit = 0xffff; - desc_access = 0x00000082; - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base, - desc_limit, desc_access); - if (error) - goto done; - - sel = 0; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0) - goto done; - - /* XXX cr2, debug registers */ - - error = 0; -done: - return (error); -} - -int -vm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num) -{ - int error, i; - struct vm_gpa_pte gpapte; - - bzero(&gpapte, sizeof(gpapte)); - gpapte.gpa = gpa; - - error = ioctl(ctx->fd, VM_GET_GPA_PMAP, &gpapte); - - if (error == 0) { - *num = gpapte.ptenum; - for (i = 0; i < gpapte.ptenum; i++) - pte[i] = gpapte.pte[i]; - } - - return (error); -} - -int -vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities) -{ - int error; - struct vm_hpet_cap cap; - - bzero(&cap, sizeof(struct vm_hpet_cap)); - error = ioctl(ctx->fd, VM_GET_HPET_CAPABILITIES, &cap); - if (capabilities != NULL) - *capabilities = cap.capabilities; - return (error); -} - -int -vm_gla2gpa(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging, - uint64_t gla, int prot, uint64_t *gpa, int *fault) -{ - struct vm_gla2gpa gg; - int error; - - bzero(&gg, sizeof(struct vm_gla2gpa)); - gg.vcpuid = vcpu; - gg.prot = prot; - gg.gla = gla; - gg.paging = *paging; - - error = ioctl(ctx->fd, VM_GLA2GPA, &gg); - if (error == 0) { - *fault = gg.fault; - *gpa = gg.gpa; - } - return (error); -} - -#ifndef min -#define min(a,b) (((a) < (b)) ? (a) : (b)) -#endif - -int -vm_copy_setup(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging, - uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt, - int *fault) -{ - void *va; - uint64_t gpa; - int error, i, n, off; - - for (i = 0; i < iovcnt; i++) { - iov[i].iov_base = 0; - iov[i].iov_len = 0; - } - - while (len) { - assert(iovcnt > 0); - error = vm_gla2gpa(ctx, vcpu, paging, gla, prot, &gpa, fault); - if (error || *fault) - return (error); - - off = gpa & PAGE_MASK; - n = min(len, PAGE_SIZE - off); - - va = vm_map_gpa(ctx, gpa, n); - if (va == NULL) - return (EFAULT); - - iov->iov_base = va; - iov->iov_len = n; - iov++; - iovcnt--; - - gla += n; - len -= n; - } - return (0); -} - -void -vm_copy_teardown(struct vmctx *ctx, int vcpu, struct iovec *iov, int iovcnt) -{ - - return; -} - -void -vm_copyin(struct vmctx *ctx, int vcpu, struct iovec *iov, void *vp, size_t len) -{ - const char *src; - char *dst; - size_t n; - - dst = vp; - while (len) { - assert(iov->iov_len); - n = min(len, iov->iov_len); - src = iov->iov_base; - bcopy(src, dst, n); - - iov++; - dst += n; - len -= n; - } -} - -void -vm_copyout(struct vmctx *ctx, int vcpu, const void *vp, struct iovec *iov, - size_t len) -{ - const char *src; - char *dst; - size_t n; - - src = vp; - while (len) { - assert(iov->iov_len); - n = min(len, iov->iov_len); - dst = iov->iov_base; - bcopy(src, dst, n); - - iov++; - src += n; - len -= n; - } -} - -static int -vm_get_cpus(struct vmctx *ctx, int which, cpuset_t *cpus) -{ - struct vm_cpuset vm_cpuset; - int error; - - bzero(&vm_cpuset, sizeof(struct vm_cpuset)); - vm_cpuset.which = which; - vm_cpuset.cpusetsize = sizeof(cpuset_t); - vm_cpuset.cpus = cpus; - - error = ioctl(ctx->fd, VM_GET_CPUS, &vm_cpuset); - return (error); -} - -int -vm_active_cpus(struct vmctx *ctx, cpuset_t *cpus) -{ - - return (vm_get_cpus(ctx, VM_ACTIVE_CPUS, cpus)); -} - -int -vm_suspended_cpus(struct vmctx *ctx, cpuset_t *cpus) -{ - - return (vm_get_cpus(ctx, VM_SUSPENDED_CPUS, cpus)); -} - -int -vm_activate_cpu(struct vmctx *ctx, int vcpu) -{ - struct vm_activate_cpu ac; - int error; - - bzero(&ac, sizeof(struct vm_activate_cpu)); - ac.vcpuid = vcpu; - error = ioctl(ctx->fd, VM_ACTIVATE_CPU, &ac); - return (error); -} - -int -vm_get_intinfo(struct vmctx *ctx, int vcpu, uint64_t *info1, uint64_t *info2) -{ - struct vm_intinfo vmii; - int error; - - bzero(&vmii, sizeof(struct vm_intinfo)); - vmii.vcpuid = vcpu; - error = ioctl(ctx->fd, VM_GET_INTINFO, &vmii); - if (error == 0) { - *info1 = vmii.info1; - *info2 = vmii.info2; - } - return (error); -} - -int -vm_set_intinfo(struct vmctx *ctx, int vcpu, uint64_t info1) -{ - struct vm_intinfo vmii; - int error; - - bzero(&vmii, sizeof(struct vm_intinfo)); - vmii.vcpuid = vcpu; - vmii.info1 = info1; - error = ioctl(ctx->fd, VM_SET_INTINFO, &vmii); - return (error); -} - -int -vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value) -{ - struct vm_rtc_data rtcdata; - int error; - - bzero(&rtcdata, sizeof(struct vm_rtc_data)); - rtcdata.offset = offset; - rtcdata.value = value; - error = ioctl(ctx->fd, VM_RTC_WRITE, &rtcdata); - return (error); -} - -int -vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval) -{ - struct vm_rtc_data rtcdata; - int error; - - bzero(&rtcdata, sizeof(struct vm_rtc_data)); - rtcdata.offset = offset; - error = ioctl(ctx->fd, VM_RTC_READ, &rtcdata); - if (error == 0) - *retval = rtcdata.value; - return (error); -} - -int -vm_rtc_settime(struct vmctx *ctx, time_t secs) -{ - struct vm_rtc_time rtctime; - int error; - - bzero(&rtctime, sizeof(struct vm_rtc_time)); - rtctime.secs = secs; - error = ioctl(ctx->fd, VM_RTC_SETTIME, &rtctime); - return (error); -} - -int -vm_rtc_gettime(struct vmctx *ctx, time_t *secs) -{ - struct vm_rtc_time rtctime; - int error; - - bzero(&rtctime, sizeof(struct vm_rtc_time)); - error = ioctl(ctx->fd, VM_RTC_GETTIME, &rtctime); - if (error == 0) - *secs = rtctime.secs; - return (error); -} - -int -vm_restart_instruction(void *arg, int vcpu) -{ - struct vmctx *ctx = arg; - - return (ioctl(ctx->fd, VM_RESTART_INSTRUCTION, &vcpu)); -} diff --git a/libvmmapi/vmmapi.h b/libvmmapi/vmmapi.h deleted file mode 100644 index d3ecdc4..0000000 --- a/libvmmapi/vmmapi.h +++ /dev/null @@ -1,173 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#ifndef _VMMAPI_H_ -#define _VMMAPI_H_ - -#include -#include - -/* - * API version for out-of-tree consumers like grub-bhyve for making compile - * time decisions. - */ -#define VMMAPI_VERSION 0101 /* 2 digit major followed by 2 digit minor */ - -struct iovec; -struct vmctx; -enum x2apic_state; - -/* - * Different styles of mapping the memory assigned to a VM into the address - * space of the controlling process. - */ -enum vm_mmap_style { - VM_MMAP_NONE, /* no mapping */ - VM_MMAP_ALL, /* fully and statically mapped */ - VM_MMAP_SPARSE, /* mappings created on-demand */ -}; - -#define VM_MEM_F_INCORE 0x01 /* include guest memory in core file */ - -int vm_create(const char *name); -struct vmctx *vm_open(const char *name); -void vm_destroy(struct vmctx *ctx); -int vm_parse_memsize(const char *optarg, size_t *memsize); -int vm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, size_t *ret_len, - int *wired); -int vm_setup_memory(struct vmctx *ctx, size_t len, enum vm_mmap_style s); -void *vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len); -int vm_get_gpa_pmap(struct vmctx *, uint64_t gpa, uint64_t *pte, int *num); -int vm_gla2gpa(struct vmctx *, int vcpuid, struct vm_guest_paging *paging, - uint64_t gla, int prot, uint64_t *gpa, int *fault); -uint32_t vm_get_lowmem_limit(struct vmctx *ctx); -void vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit); -void vm_set_memflags(struct vmctx *ctx, int flags); -size_t vm_get_lowmem_size(struct vmctx *ctx); -size_t vm_get_highmem_size(struct vmctx *ctx); -int vm_set_desc(struct vmctx *ctx, int vcpu, int reg, - uint64_t base, uint32_t limit, uint32_t access); -int vm_get_desc(struct vmctx *ctx, int vcpu, int reg, - uint64_t *base, uint32_t *limit, uint32_t *access); -int vm_get_seg_desc(struct vmctx *ctx, int vcpu, int reg, - struct seg_desc *seg_desc); -int vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val); -int vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *retval); -int vm_run(struct vmctx *ctx, int vcpu, struct vm_exit *ret_vmexit); -int vm_suspend(struct vmctx *ctx, enum vm_suspend_how how); -int vm_reinit(struct vmctx *ctx); -int vm_apicid2vcpu(struct vmctx *ctx, int apicid); -int vm_inject_exception(struct vmctx *ctx, int vcpu, int vector, - int errcode_valid, uint32_t errcode, int restart_instruction); -int vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector); -int vm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector); -int vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg); -int vm_ioapic_assert_irq(struct vmctx *ctx, int irq); -int vm_ioapic_deassert_irq(struct vmctx *ctx, int irq); -int vm_ioapic_pulse_irq(struct vmctx *ctx, int irq); -int vm_ioapic_pincount(struct vmctx *ctx, int *pincount); -int vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq); -int vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq); -int vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq); -int vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq, - enum vm_intr_trigger trigger); -int vm_inject_nmi(struct vmctx *ctx, int vcpu); -int vm_capability_name2type(const char *capname); -const char *vm_capability_type2name(int type); -int vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, - int *retval); -int vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, - int val); -int vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func); -int vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func); -int vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func, - vm_paddr_t gpa, size_t len, vm_paddr_t hpa); -int vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot, - int func, uint64_t addr, uint64_t msg, int numvec); -int vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot, - int func, int idx, uint64_t addr, uint64_t msg, - uint32_t vector_control); - -int vm_get_intinfo(struct vmctx *ctx, int vcpu, uint64_t *i1, uint64_t *i2); -int vm_set_intinfo(struct vmctx *ctx, int vcpu, uint64_t exit_intinfo); - -/* - * Return a pointer to the statistics buffer. Note that this is not MT-safe. - */ -uint64_t *vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv, - int *ret_entries); -const char *vm_get_stat_desc(struct vmctx *ctx, int index); - -int vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *s); -int vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state s); - -int vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities); - -/* - * Translate the GLA range [gla,gla+len) into GPA segments in 'iov'. - * The 'iovcnt' should be big enough to accomodate all GPA segments. - * - * retval fault Interpretation - * 0 0 Success - * 0 1 An exception was injected into the guest - * EFAULT N/A Error - */ -int vm_copy_setup(struct vmctx *ctx, int vcpu, struct vm_guest_paging *pg, - uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt, - int *fault); -void vm_copyin(struct vmctx *ctx, int vcpu, struct iovec *guest_iov, - void *host_dst, size_t len); -void vm_copyout(struct vmctx *ctx, int vcpu, const void *host_src, - struct iovec *guest_iov, size_t len); -void vm_copy_teardown(struct vmctx *ctx, int vcpu, struct iovec *iov, - int iovcnt); - -/* RTC */ -int vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value); -int vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval); -int vm_rtc_settime(struct vmctx *ctx, time_t secs); -int vm_rtc_gettime(struct vmctx *ctx, time_t *secs); - -/* Reset vcpu register state */ -int vcpu_reset(struct vmctx *ctx, int vcpu); - -int vm_active_cpus(struct vmctx *ctx, cpuset_t *cpus); -int vm_suspended_cpus(struct vmctx *ctx, cpuset_t *cpus); -int vm_activate_cpu(struct vmctx *ctx, int vcpu); - -/* - * FreeBSD specific APIs - */ -int vm_setup_freebsd_registers(struct vmctx *ctx, int vcpu, - uint64_t rip, uint64_t cr3, uint64_t gdtbase, - uint64_t rsp); -int vm_setup_freebsd_registers_i386(struct vmctx *vmctx, int vcpu, - uint32_t eip, uint32_t gdtbase, - uint32_t esp); -void vm_setup_freebsd_gdt(uint64_t *gdtr); -#endif /* _VMMAPI_H_ */ diff --git a/libvmmapi/vmmapi_freebsd.c b/libvmmapi/vmmapi_freebsd.c deleted file mode 100644 index d801184..0000000 --- a/libvmmapi/vmmapi_freebsd.c +++ /dev/null @@ -1,345 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include - -#include -#include -#include - -#include -#include - -#include "vmmapi.h" - -#define I386_TSS_SIZE 104 - -#define DESC_PRESENT 0x00000080 -#define DESC_LONGMODE 0x00002000 -#define DESC_DEF32 0x00004000 -#define DESC_GRAN 0x00008000 -#define DESC_UNUSABLE 0x00010000 - -#define GUEST_NULL_SEL 0 -#define GUEST_CODE_SEL 1 -#define GUEST_DATA_SEL 2 -#define GUEST_TSS_SEL 3 -#define GUEST_GDTR_LIMIT64 (3 * 8 - 1) - -static struct segment_descriptor i386_gdt[] = { - {}, /* NULL */ - { .sd_lolimit = 0xffff, .sd_type = SDT_MEMER, /* CODE */ - .sd_p = 1, .sd_hilimit = 0xf, .sd_def32 = 1, .sd_gran = 1 }, - { .sd_lolimit = 0xffff, .sd_type = SDT_MEMRW, /* DATA */ - .sd_p = 1, .sd_hilimit = 0xf, .sd_def32 = 1, .sd_gran = 1 }, - { .sd_lolimit = I386_TSS_SIZE - 1, /* TSS */ - .sd_type = SDT_SYS386TSS, .sd_p = 1 } -}; - -/* - * Setup the 'vcpu' register set such that it will begin execution at - * 'eip' in flat mode. - */ -int -vm_setup_freebsd_registers_i386(struct vmctx *vmctx, int vcpu, uint32_t eip, - uint32_t gdtbase, uint32_t esp) -{ - uint64_t cr0, rflags, desc_base; - uint32_t desc_access, desc_limit, tssbase; - uint16_t gsel; - struct segment_descriptor *gdt; - int error, tmp; - - /* A 32-bit guest requires unrestricted mode. */ - error = vm_get_capability(vmctx, vcpu, VM_CAP_UNRESTRICTED_GUEST, &tmp); - if (error) - goto done; - error = vm_set_capability(vmctx, vcpu, VM_CAP_UNRESTRICTED_GUEST, 1); - if (error) - goto done; - - cr0 = CR0_PE | CR0_NE; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0) - goto done; - - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, 0)) != 0) - goto done; - - /* - * Forcing EFER to 0 causes bhyve to clear the "IA-32e guest - * mode" entry control. - */ - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_EFER, 0))) - goto done; - - gdt = vm_map_gpa(vmctx, gdtbase, 0x1000); - if (gdt == NULL) - return (EFAULT); - memcpy(gdt, i386_gdt, sizeof(i386_gdt)); - desc_base = gdtbase; - desc_limit = sizeof(i386_gdt) - 1; - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR, - desc_base, desc_limit, 0); - if (error != 0) - goto done; - - /* Place the TSS one page above the GDT. */ - tssbase = gdtbase + 0x1000; - gdt[3].sd_lobase = tssbase; - - rflags = 0x2; - error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags); - if (error) - goto done; - - desc_base = 0; - desc_limit = 0xffffffff; - desc_access = DESC_GRAN | DESC_DEF32 | DESC_PRESENT | SDT_MEMERA; - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS, - desc_base, desc_limit, desc_access); - - desc_access = DESC_GRAN | DESC_DEF32 | DESC_PRESENT | SDT_MEMRWA; - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - desc_base = tssbase; - desc_limit = I386_TSS_SIZE - 1; - desc_access = DESC_PRESENT | SDT_SYS386BSY; - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, 0, 0, - DESC_UNUSABLE); - if (error) - goto done; - - gsel = GSEL(GUEST_CODE_SEL, SEL_KPL); - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, gsel)) != 0) - goto done; - - gsel = GSEL(GUEST_DATA_SEL, SEL_KPL); - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, gsel)) != 0) - goto done; - - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, gsel)) != 0) - goto done; - - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, gsel)) != 0) - goto done; - - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, gsel)) != 0) - goto done; - - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, gsel)) != 0) - goto done; - - gsel = GSEL(GUEST_TSS_SEL, SEL_KPL); - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, gsel)) != 0) - goto done; - - /* LDTR is pointing to the null selector */ - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0) - goto done; - - /* entry point */ - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, eip)) != 0) - goto done; - - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, esp)) != 0) - goto done; - - error = 0; -done: - return (error); -} - -void -vm_setup_freebsd_gdt(uint64_t *gdtr) -{ - gdtr[GUEST_NULL_SEL] = 0; - gdtr[GUEST_CODE_SEL] = 0x0020980000000000; - gdtr[GUEST_DATA_SEL] = 0x0000900000000000; -} - -/* - * Setup the 'vcpu' register set such that it will begin execution at - * 'rip' in long mode. - */ -int -vm_setup_freebsd_registers(struct vmctx *vmctx, int vcpu, - uint64_t rip, uint64_t cr3, uint64_t gdtbase, - uint64_t rsp) -{ - int error; - uint64_t cr0, cr4, efer, rflags, desc_base; - uint32_t desc_access, desc_limit; - uint16_t gsel; - - cr0 = CR0_PE | CR0_PG | CR0_NE; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0) - goto done; - - cr4 = CR4_PAE; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0) - goto done; - - efer = EFER_LME | EFER_LMA; - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_EFER, efer))) - goto done; - - rflags = 0x2; - error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags); - if (error) - goto done; - - desc_base = 0; - desc_limit = 0; - desc_access = 0x0000209B; - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - desc_access = 0x00000093; - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS, - desc_base, desc_limit, desc_access); - if (error) - goto done; - - /* - * XXX TR is pointing to null selector even though we set the - * TSS segment to be usable with a base address and limit of 0. - */ - desc_access = 0x0000008b; - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access); - if (error) - goto done; - - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, 0, 0, - DESC_UNUSABLE); - if (error) - goto done; - - gsel = GSEL(GUEST_CODE_SEL, SEL_KPL); - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, gsel)) != 0) - goto done; - - gsel = GSEL(GUEST_DATA_SEL, SEL_KPL); - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, gsel)) != 0) - goto done; - - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, gsel)) != 0) - goto done; - - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, gsel)) != 0) - goto done; - - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, gsel)) != 0) - goto done; - - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, gsel)) != 0) - goto done; - - /* XXX TR is pointing to the null selector */ - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, 0)) != 0) - goto done; - - /* LDTR is pointing to the null selector */ - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0) - goto done; - - /* entry point */ - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0) - goto done; - - /* page table base */ - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, cr3)) != 0) - goto done; - - desc_base = gdtbase; - desc_limit = GUEST_GDTR_LIMIT64; - error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR, - desc_base, desc_limit, 0); - if (error != 0) - goto done; - - if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, rsp)) != 0) - goto done; - - error = 0; -done: - return (error); -} diff --git a/bhyve/acpi.c b/src/acpi.c similarity index 95% rename from bhyve/acpi.c rename to src/acpi.c index a9dd1cc..cd52123 100644 --- a/bhyve/acpi.c +++ b/src/acpi.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2012 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -50,26 +51,20 @@ * DSDT -> 0xf2800 (variable - can go up to 0x100000) */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include - -#include +#include #include #include -#include -#include #include - -#include -#include - -#include "bhyverun.h" -#include "acpi.h" -#include "pci_emul.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include /* * Define the base address of the ACPI tables, and the offsets to @@ -109,11 +104,14 @@ static FILE *dsdt_fp; static int dsdt_indent_level; static int dsdt_error; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct basl_fio { int fd; - FILE *fp; - char f_name[MAXPATHLEN]; + FILE *fp; + char f_name[MAXPATHLEN]; }; +#pragma clang diagnostic pop #define EFPRINTF(...) \ err = fprintf(__VA_ARGS__); if (err < 0) goto err_exit; @@ -613,7 +611,7 @@ basl_fwrite_mcfg(FILE *fp) EFPRINTF(fp, "[0008]\t\tReserved : 0\n"); EFPRINTF(fp, "\n"); - EFPRINTF(fp, "[0008]\t\tBase Address : %016lX\n", pci_ecfg_base()); + EFPRINTF(fp, "[0008]\t\tBase Address : %016llx\n", pci_ecfg_base()); EFPRINTF(fp, "[0002]\t\tSegment Group: 0000\n"); EFPRINTF(fp, "[0001]\t\tStart Bus: 00\n"); EFPRINTF(fp, "[0001]\t\tEnd Bus: FF\n"); @@ -657,6 +655,8 @@ err_exit: return (errno); } +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wformat-nonliteral" /* * Helper routines for writing to the DSDT from other modules. */ @@ -666,6 +666,8 @@ dsdt_line(const char *fmt, ...) va_list ap; int err; + err = 0; + if (dsdt_error != 0) return; @@ -683,6 +685,7 @@ dsdt_line(const char *fmt, ...) err_exit: dsdt_error = errno; } +#pragma clang diagnostic pop void dsdt_indent(int levels) @@ -838,13 +841,12 @@ basl_start(struct basl_fio *in, struct basl_fio *out) static void basl_end(struct basl_fio *in, struct basl_fio *out) { - basl_close(in); basl_close(out); } static int -basl_load(struct vmctx *ctx, int fd, uint64_t off) +basl_load(int fd, uint64_t off) { struct stat sb; void *gaddr; @@ -852,18 +854,20 @@ basl_load(struct vmctx *ctx, int fd, uint64_t off) if (fstat(fd, &sb) < 0) return (errno); - gaddr = paddr_guest2host(ctx, basl_acpi_base + off, sb.st_size); + gaddr = paddr_guest2host(basl_acpi_base + off, ((size_t) sb.st_size)); if (gaddr == NULL) return (EFAULT); - if (read(fd, gaddr, sb.st_size) < 0) + if (read(fd, gaddr, ((size_t) sb.st_size)) < 0) return (errno); return (0); } +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wformat-nonliteral" static int -basl_compile(struct vmctx *ctx, int (*fwrite_section)(FILE *), uint64_t offset) +basl_compile(int (*fwrite_section)(FILE *), uint64_t offset) { struct basl_fio io[2]; static char iaslbuf[3*MAXPATHLEN + 10]; @@ -897,7 +901,7 @@ basl_compile(struct vmctx *ctx, int (*fwrite_section)(FILE *), uint64_t offset) * Copy the aml output file into guest * memory at the specified location */ - err = basl_load(ctx, io[1].fd, offset); + err = basl_load(io[1].fd, offset); } } basl_end(&io[0], &io[1]); @@ -905,6 +909,7 @@ basl_compile(struct vmctx *ctx, int (*fwrite_section)(FILE *), uint64_t offset) return (err); } +#pragma clang diagnostic pop static int basl_make_templates(void) @@ -923,9 +928,9 @@ basl_make_templates(void) tmpdir = _PATH_TMP; } - len = strlen(tmpdir); + len = (int) strlen(tmpdir); - if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1) < MAXPATHLEN) { + if ((((unsigned long) len) + sizeof(BHYVE_ASL_TEMPLATE) + 1) < MAXPATHLEN) { strcpy(basl_template, tmpdir); while (len > 0 && basl_template[len - 1] == '/') len--; @@ -938,12 +943,12 @@ basl_make_templates(void) /* * len has been intialized (and maybe adjusted) above */ - if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1 + + if ((((unsigned long) len) + sizeof(BHYVE_ASL_TEMPLATE) + 1 + sizeof(BHYVE_ASL_SUFFIX)) < MAXPATHLEN) { strcpy(basl_stemplate, tmpdir); basl_stemplate[len] = '/'; strcpy(&basl_stemplate[len + 1], BHYVE_ASL_TEMPLATE); - len = strlen(basl_stemplate); + len = (int) strlen(basl_stemplate); strcpy(&basl_stemplate[len], BHYVE_ASL_SUFFIX); } else err = E2BIG; @@ -966,18 +971,18 @@ static struct { { basl_fwrite_mcfg, MCFG_OFFSET }, { basl_fwrite_facs, FACS_OFFSET }, { basl_fwrite_dsdt, DSDT_OFFSET }, - { NULL } + { NULL , 0} }; int -acpi_build(struct vmctx *ctx, int ncpu) +acpi_build(int ncpu) { int err; int i; basl_ncpu = ncpu; - err = vm_get_hpet_capabilities(ctx, &hpet_capabilities); + err = xh_vm_get_hpet_capabilities(&hpet_capabilities); if (err != 0) return (err); @@ -1003,7 +1008,7 @@ acpi_build(struct vmctx *ctx, int ncpu) * copying them into guest memory */ while (!err && basl_ftables[i].wsect != NULL) { - err = basl_compile(ctx, basl_ftables[i].wsect, + err = basl_compile(basl_ftables[i].wsect, basl_ftables[i].offset); i++; } diff --git a/bhyve/atkbdc.c b/src/atkbdc.c similarity index 75% rename from bhyve/atkbdc.c rename to src/atkbdc.c index 930b7af..e8df7f9 100644 --- a/bhyve/atkbdc.c +++ b/src/atkbdc.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2014 Tycho Nightingale + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -24,32 +25,23 @@ * SUCH DAMAGE. */ -#include -__FBSDID("$FreeBSD$"); - -#include - -#include - -#include - +#include #include #include #include +#include +#include +#include +#include -#include "inout.h" -#include "pci_lpc.h" - -#define KBD_DATA_PORT 0x60 - -#define KBD_STS_CTL_PORT 0x64 -#define KBD_SYS_FLAG 0x4 - -#define KBDC_RESET 0xfe +#define KBD_DATA_PORT 0x60 +#define KBD_STS_CTL_PORT 0x64 +#define KBD_SYS_FLAG 0x4 +#define KBDC_RESET 0xfe static int -atkbdc_data_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, - uint32_t *eax, void *arg) +atkbdc_data_handler(UNUSED int vcpu, UNUSED int in, UNUSED int port, int bytes, + uint32_t *eax, UNUSED void *arg) { if (bytes != 1) return (-1); @@ -60,8 +52,8 @@ atkbdc_data_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, } static int -atkbdc_sts_ctl_handler(struct vmctx *ctx, int vcpu, int in, int port, - int bytes, uint32_t *eax, void *arg) +atkbdc_sts_ctl_handler(UNUSED int vcpu, int in, UNUSED int port, int bytes, + uint32_t *eax, UNUSED void *arg) { int error, retval; @@ -74,7 +66,7 @@ atkbdc_sts_ctl_handler(struct vmctx *ctx, int vcpu, int in, int port, } else { switch (*eax) { case KBDC_RESET: /* Pulse "reset" line. */ - error = vm_suspend(ctx, VM_SUSPEND_RESET); + error = xh_vm_suspend(VM_SUSPEND_RESET); assert(error == 0 || errno == EALREADY); break; } @@ -85,6 +77,5 @@ atkbdc_sts_ctl_handler(struct vmctx *ctx, int vcpu, int in, int port, INOUT_PORT(atkdbc, KBD_DATA_PORT, IOPORT_F_INOUT, atkbdc_data_handler); SYSRES_IO(KBD_DATA_PORT, 1); -INOUT_PORT(atkbdc, KBD_STS_CTL_PORT, IOPORT_F_INOUT, - atkbdc_sts_ctl_handler); +INOUT_PORT(atkbdc, KBD_STS_CTL_PORT, IOPORT_F_INOUT, atkbdc_sts_ctl_handler); SYSRES_IO(KBD_STS_CTL_PORT, 1); diff --git a/bhyve/block_if.c b/src/block_if.c similarity index 73% rename from bhyve/block_if.c rename to src/block_if.c index ef8e11e..4d5df95 100644 --- a/bhyve/block_if.c +++ b/src/block_if.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2013 Peter Grehan + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,9 +27,6 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - #include #include #include @@ -42,20 +40,25 @@ __FBSDID("$FreeBSD$"); #include #include #include -#include #include #include -#include +#include +#include +#include +#include -#include "bhyverun.h" -#include "mevent.h" -#include "block_if.h" +#define BLOCKIF_SIG 0xb109b109 +/* xhyve: FIXME + * + * // #define BLOCKIF_NUMTHR 8 + * + * OS X does not support preadv/pwritev, we need to serialize reads and writes + * for the time being until we find a better solution. + */ +#define BLOCKIF_NUMTHR 1 -#define BLOCKIF_SIG 0xb109b109 - -#define BLOCKIF_NUMTHR 8 -#define BLOCKIF_MAXREQ (64 + BLOCKIF_NUMTHR) +#define BLOCKIF_MAXREQ (64 + BLOCKIF_NUMTHR) enum blockop { BOP_READ, @@ -72,33 +75,34 @@ enum blockstat { BST_DONE }; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct blockif_elem { TAILQ_ENTRY(blockif_elem) be_link; - struct blockif_req *be_req; - enum blockop be_op; - enum blockstat be_status; - pthread_t be_tid; - off_t be_block; + struct blockif_req *be_req; + enum blockop be_op; + enum blockstat be_status; + pthread_t be_tid; + off_t be_block; }; struct blockif_ctxt { - int bc_magic; - int bc_fd; - int bc_ischr; - int bc_isgeom; - int bc_candelete; - int bc_rdonly; - off_t bc_size; - int bc_sectsz; - int bc_psectsz; - int bc_psectoff; - int bc_closing; - pthread_t bc_btid[BLOCKIF_NUMTHR]; - pthread_mutex_t bc_mtx; - pthread_cond_t bc_cond; - + int bc_magic; + int bc_fd; + int bc_ischr; + int bc_isgeom; + int bc_candelete; + int bc_rdonly; + off_t bc_size; + int bc_sectsz; + int bc_psectsz; + int bc_psectoff; + int bc_closing; + pthread_t bc_btid[BLOCKIF_NUMTHR]; + pthread_mutex_t bc_mtx; + pthread_cond_t bc_cond; /* Request elements and free/pending/busy queues */ - TAILQ_HEAD(, blockif_elem) bc_freeq; + TAILQ_HEAD(, blockif_elem) bc_freeq; TAILQ_HEAD(, blockif_elem) bc_pendq; TAILQ_HEAD(, blockif_elem) bc_busyq; struct blockif_elem bc_reqs[BLOCKIF_MAXREQ]; @@ -107,14 +111,36 @@ struct blockif_ctxt { static pthread_once_t blockif_once = PTHREAD_ONCE_INIT; struct blockif_sig_elem { - pthread_mutex_t bse_mtx; - pthread_cond_t bse_cond; - int bse_pending; - struct blockif_sig_elem *bse_next; + pthread_mutex_t bse_mtx; + pthread_cond_t bse_cond; + int bse_pending; + struct blockif_sig_elem *bse_next; }; static struct blockif_sig_elem *blockif_bse_head; +#pragma clang diagnostic pop + +static ssize_t +preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset) +{ + off_t res; + + res = lseek(fd, offset, SEEK_SET); + assert(res == offset); + return readv(fd, iov, iovcnt); +} + +static ssize_t +pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset) +{ + off_t res; + + res = lseek(fd, offset, SEEK_SET); + assert(res == offset); + return writev(fd, iov, iovcnt); +} + static int blockif_enqueue(struct blockif_ctxt *bc, struct blockif_req *breq, enum blockop op) @@ -137,7 +163,7 @@ blockif_enqueue(struct blockif_ctxt *bc, struct blockif_req *breq, for (i = 0; i < breq->br_iovcnt; i++) off += breq->br_iov[i].iov_len; break; - default: + case BOP_FLUSH: off = OFF_MAX; } be->be_block = off; @@ -202,7 +228,7 @@ static void blockif_proc(struct blockif_ctxt *bc, struct blockif_elem *be, uint8_t *buf) { struct blockif_req *br; - off_t arg[2]; + // off_t arg[2]; ssize_t clen, len, off, boff, voff; int i, err; @@ -224,18 +250,18 @@ blockif_proc(struct blockif_ctxt *bc, struct blockif_elem *be, uint8_t *buf) off = voff = 0; while (br->br_resid > 0) { len = MIN(br->br_resid, MAXPHYS); - if (pread(bc->bc_fd, buf, len, br->br_offset + - off) < 0) { + if (pread(bc->bc_fd, buf, ((size_t) len), br->br_offset + off) < 0) + { err = errno; break; } boff = 0; do { - clen = MIN(len - boff, br->br_iov[i].iov_len - - voff); - memcpy(br->br_iov[i].iov_base + voff, - buf + boff, clen); - if (clen < br->br_iov[i].iov_len - voff) + clen = MIN((len - boff), + (((ssize_t) br->br_iov[i].iov_len) - voff)); + memcpy(((void *) (((uintptr_t) br->br_iov[i].iov_base) + + ((size_t) voff))), buf + boff, clen); + if (clen < (((ssize_t) br->br_iov[i].iov_len) - voff)) voff += clen; else { i++; @@ -266,11 +292,12 @@ blockif_proc(struct blockif_ctxt *bc, struct blockif_elem *be, uint8_t *buf) len = MIN(br->br_resid, MAXPHYS); boff = 0; do { - clen = MIN(len - boff, br->br_iov[i].iov_len - - voff); - memcpy(buf + boff, - br->br_iov[i].iov_base + voff, clen); - if (clen < br->br_iov[i].iov_len - voff) + clen = MIN((len - boff), + (((ssize_t) br->br_iov[i].iov_len) - voff)); + memcpy((buf + boff), + ((void *) (((uintptr_t) br->br_iov[i].iov_base) + + ((size_t) voff))), clen); + if (clen < (((ssize_t) br->br_iov[i].iov_len) - voff)) voff += clen; else { i++; @@ -278,7 +305,7 @@ blockif_proc(struct blockif_ctxt *bc, struct blockif_elem *be, uint8_t *buf) } boff += clen; } while (boff < len); - if (pwrite(bc->bc_fd, buf, len, br->br_offset + + if (pwrite(bc->bc_fd, buf, ((size_t) len), br->br_offset + off) < 0) { err = errno; break; @@ -289,28 +316,27 @@ blockif_proc(struct blockif_ctxt *bc, struct blockif_elem *be, uint8_t *buf) break; case BOP_FLUSH: if (bc->bc_ischr) { - if (ioctl(bc->bc_fd, DIOCGFLUSH)) + if (ioctl(bc->bc_fd, DKIOCSYNCHRONIZECACHE)) err = errno; } else if (fsync(bc->bc_fd)) err = errno; break; case BOP_DELETE: - if (!bc->bc_candelete) + if (!bc->bc_candelete) { err = EOPNOTSUPP; - else if (bc->bc_rdonly) - err = EROFS; - else if (bc->bc_ischr) { - arg[0] = br->br_offset; - arg[1] = br->br_resid; - if (ioctl(bc->bc_fd, DIOCGDELETE, arg)) - err = errno; - else - br->br_resid = 0; - } else + // } else if (bc->bc_rdonly) { + // err = EROFS; + // } else if (bc->bc_ischr) { + // arg[0] = br->br_offset; + // arg[1] = br->br_resid; + // if (ioctl(bc->bc_fd, DIOCGDELETE, arg)) { + // err = errno; + // } else { + // br->br_resid = 0; + // } + } else { err = EOPNOTSUPP; - break; - default: - err = EINVAL; + } break; } @@ -356,7 +382,8 @@ blockif_thr(void *arg) } static void -blockif_sigcont_handler(int signal, enum ev_type type, void *arg) +blockif_sigcont_handler(UNUSED int signal, UNUSED enum ev_type type, + UNUSED void *arg) { struct blockif_sig_elem *bse; @@ -388,14 +415,13 @@ blockif_init(void) } struct blockif_ctxt * -blockif_open(const char *optstr, const char *ident) +blockif_open(const char *optstr, UNUSED const char *ident) { - char tname[MAXCOMLEN + 1]; - char name[MAXPATHLEN]; + // char name[MAXPATHLEN]; char *nopt, *xopts, *cp; struct blockif_ctxt *bc; struct stat sbuf; - struct diocgattr_arg arg; + // struct diocgattr_arg arg; off_t size, psectsz, psectoff; int extra, fd, i, sectsz; int nocache, sync, ro, candelete, geom, ssopt, pssopt; @@ -408,6 +434,7 @@ blockif_open(const char *optstr, const char *ident) sync = 0; ro = 0; + pssopt = 0; /* * The first element in the optstring is always a pathname. * Optional elements follow @@ -434,8 +461,11 @@ blockif_open(const char *optstr, const char *ident) } extra = 0; - if (nocache) - extra |= O_DIRECT; + if (nocache) { + perror("xhyve: nocache support unimplemented"); + goto err; + // extra |= O_DIRECT; + } if (sync) extra |= O_SYNC; @@ -451,34 +481,37 @@ blockif_open(const char *optstr, const char *ident) goto err; } - if (fstat(fd, &sbuf) < 0) { - perror("Could not stat backing file"); + if (fstat(fd, &sbuf) < 0) { + perror("Could not stat backing file"); goto err; - } + } - /* + /* * Deal with raw devices */ - size = sbuf.st_size; + size = sbuf.st_size; sectsz = DEV_BSIZE; psectsz = psectoff = 0; candelete = geom = 0; if (S_ISCHR(sbuf.st_mode)) { - if (ioctl(fd, DIOCGMEDIASIZE, &size) < 0 || - ioctl(fd, DIOCGSECTORSIZE, §sz)) { - perror("Could not fetch dev blk/sector size"); - goto err; - } - assert(size != 0); - assert(sectsz != 0); - if (ioctl(fd, DIOCGSTRIPESIZE, &psectsz) == 0 && psectsz > 0) - ioctl(fd, DIOCGSTRIPEOFFSET, &psectoff); - strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name)); - arg.len = sizeof(arg.value.i); - if (ioctl(fd, DIOCGATTR, &arg) == 0) - candelete = arg.value.i; - if (ioctl(fd, DIOCGPROVIDERNAME, name) == 0) - geom = 1; + perror("xhyve: raw device support unimplemented"); + goto err; + // if (ioctl(fd, DIOCGMEDIASIZE, &size) < 0 || + // ioctl(fd, DIOCGSECTORSIZE, §sz)) + // { + // perror("Could not fetch dev blk/sector size"); + // goto err; + // } + // assert(size != 0); + // assert(sectsz != 0); + // if (ioctl(fd, DIOCGSTRIPESIZE, &psectsz) == 0 && psectsz > 0) + // ioctl(fd, DIOCGSTRIPEOFFSET, &psectoff); + // strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name)); + // arg.len = sizeof(arg.value.i); + // if (ioctl(fd, DIOCGATTR, &arg) == 0) + // candelete = arg.value.i; + // if (ioctl(fd, DIOCGPROVIDERNAME, name) == 0) + // geom = 1; } else psectsz = sbuf.st_blksize; @@ -490,21 +523,21 @@ blockif_open(const char *optstr, const char *ident) goto err; } - /* - * Some backend drivers (e.g. cd0, ada0) require that the I/O - * size be a multiple of the device's sector size. - * - * Validate that the emulated sector size complies with this - * requirement. - */ - if (S_ISCHR(sbuf.st_mode)) { - if (ssopt < sectsz || (ssopt % sectsz) != 0) { - fprintf(stderr, "Sector size %d incompatible " - "with underlying device sector size %d\n", - ssopt, sectsz); - goto err; - } - } + // /* + // * Some backend drivers (e.g. cd0, ada0) require that the I/O + // * size be a multiple of the device's sector size. + // * + // * Validate that the emulated sector size complies with this + // * requirement. + // */ + // if (S_ISCHR(sbuf.st_mode)) { + // if (ssopt < sectsz || (ssopt % sectsz) != 0) { + // fprintf(stderr, "Sector size %d incompatible " + // "with underlying device sector size %d\n", + // ssopt, sectsz); + // goto err; + // } + // } sectsz = ssopt; psectsz = pssopt; @@ -517,7 +550,7 @@ blockif_open(const char *optstr, const char *ident) goto err; } - bc->bc_magic = BLOCKIF_SIG; + bc->bc_magic = (int) BLOCKIF_SIG; bc->bc_fd = fd; bc->bc_ischr = S_ISCHR(sbuf.st_mode); bc->bc_isgeom = geom; @@ -525,8 +558,8 @@ blockif_open(const char *optstr, const char *ident) bc->bc_rdonly = ro; bc->bc_size = size; bc->bc_sectsz = sectsz; - bc->bc_psectsz = psectsz; - bc->bc_psectoff = psectoff; + bc->bc_psectsz = (int) psectsz; + bc->bc_psectoff = (int) psectoff; pthread_mutex_init(&bc->bc_mtx, NULL); pthread_cond_init(&bc->bc_cond, NULL); TAILQ_INIT(&bc->bc_freeq); @@ -539,8 +572,6 @@ blockif_open(const char *optstr, const char *ident) for (i = 0; i < BLOCKIF_NUMTHR; i++) { pthread_create(&bc->bc_btid[i], NULL, blockif_thr, bc); - snprintf(tname, sizeof(tname), "blk-%s-%d", ident, i); - pthread_set_name_np(bc->bc_btid[i], tname); } return (bc); @@ -583,32 +614,28 @@ blockif_request(struct blockif_ctxt *bc, struct blockif_req *breq, int blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq) { - - assert(bc->bc_magic == BLOCKIF_SIG); + assert(bc->bc_magic == ((int) BLOCKIF_SIG)); return (blockif_request(bc, breq, BOP_READ)); } int blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq) { - - assert(bc->bc_magic == BLOCKIF_SIG); + assert(bc->bc_magic == ((int) BLOCKIF_SIG)); return (blockif_request(bc, breq, BOP_WRITE)); } int blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq) { - - assert(bc->bc_magic == BLOCKIF_SIG); + assert(bc->bc_magic == ((int) BLOCKIF_SIG)); return (blockif_request(bc, breq, BOP_FLUSH)); } int blockif_delete(struct blockif_ctxt *bc, struct blockif_req *breq) { - - assert(bc->bc_magic == BLOCKIF_SIG); + assert(bc->bc_magic == ((int) BLOCKIF_SIG)); return (blockif_request(bc, breq, BOP_DELETE)); } @@ -617,7 +644,7 @@ blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq) { struct blockif_elem *be; - assert(bc->bc_magic == BLOCKIF_SIG); + assert(bc->bc_magic == ((int) BLOCKIF_SIG)); pthread_mutex_lock(&bc->bc_mtx); /* @@ -696,7 +723,7 @@ blockif_close(struct blockif_ctxt *bc) err = 0; - assert(bc->bc_magic == BLOCKIF_SIG); + assert(bc->bc_magic == ((int) BLOCKIF_SIG)); /* * Stop the block i/o thread @@ -732,22 +759,22 @@ blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s) uint16_t secpt; /* sectors per track */ uint8_t heads; - assert(bc->bc_magic == BLOCKIF_SIG); + assert(bc->bc_magic == ((int) BLOCKIF_SIG)); sectors = bc->bc_size / bc->bc_sectsz; /* Clamp the size to the largest possible with CHS */ - if (sectors > 65535UL*16*255) - sectors = 65535UL*16*255; + if (sectors > 65535LL*16*255) + sectors = 65535LL*16*255; - if (sectors >= 65536UL*16*63) { + if (sectors >= 65536LL*16*63) { secpt = 255; heads = 16; hcyl = sectors / secpt; } else { secpt = 17; hcyl = sectors / secpt; - heads = (hcyl + 1023) / 1024; + heads = (uint8_t) ((hcyl + 1023) / 1024); if (heads < 4) heads = 4; @@ -764,9 +791,9 @@ blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s) } } - *c = hcyl / heads; + *c = (uint16_t) (hcyl / heads); *h = heads; - *s = secpt; + *s = (uint8_t) secpt; } /* @@ -775,24 +802,21 @@ blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s) off_t blockif_size(struct blockif_ctxt *bc) { - - assert(bc->bc_magic == BLOCKIF_SIG); + assert(bc->bc_magic == ((int) BLOCKIF_SIG)); return (bc->bc_size); } int blockif_sectsz(struct blockif_ctxt *bc) { - - assert(bc->bc_magic == BLOCKIF_SIG); + assert(bc->bc_magic == ((int) BLOCKIF_SIG)); return (bc->bc_sectsz); } void blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off) { - - assert(bc->bc_magic == BLOCKIF_SIG); + assert(bc->bc_magic == ((int) BLOCKIF_SIG)); *size = bc->bc_psectsz; *off = bc->bc_psectoff; } @@ -800,23 +824,20 @@ blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off) int blockif_queuesz(struct blockif_ctxt *bc) { - - assert(bc->bc_magic == BLOCKIF_SIG); + assert(bc->bc_magic == ((int) BLOCKIF_SIG)); return (BLOCKIF_MAXREQ - 1); } int blockif_is_ro(struct blockif_ctxt *bc) { - - assert(bc->bc_magic == BLOCKIF_SIG); + assert(bc->bc_magic == ((int) BLOCKIF_SIG)); return (bc->bc_rdonly); } int blockif_candelete(struct blockif_ctxt *bc) { - - assert(bc->bc_magic == BLOCKIF_SIG); + assert(bc->bc_magic == ((int) BLOCKIF_SIG)); return (bc->bc_candelete); } diff --git a/bhyve/consport.c b/src/consport.c similarity index 82% rename from bhyve/consport.c rename to src/consport.c index 4074e95..f5d0b73 100644 --- a/bhyve/consport.c +++ b/src/consport.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,23 +27,20 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include - +#include #include #include #include #include #include +#include +#include +#include +#include +#include -#include "inout.h" -#include "pci_lpc.h" - -#define BVM_CONSOLE_PORT 0x220 -#define BVM_CONS_SIG ('b' << 8 | 'v') +#define BVM_CONSOLE_PORT 0x220 +#define BVM_CONS_SIG ('b' << 8 | 'v') static struct termios tio_orig, tio_new; @@ -66,14 +64,15 @@ ttyopen(void) static bool tty_char_available(void) { - fd_set rfds; - struct timeval tv; + fd_set rfds; + struct timeval tv; - FD_ZERO(&rfds); - FD_SET(STDIN_FILENO, &rfds); - tv.tv_sec = 0; - tv.tv_usec = 0; - if (select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tv) > 0) { + FD_ZERO(&rfds); + FD_SET(STDIN_FILENO, &rfds); + tv.tv_sec = 0; + tv.tv_usec = 0; + + if (select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tv) > 0) { return (true); } else { return (false); @@ -100,8 +99,8 @@ ttywrite(unsigned char wb) } static int -console_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, - uint32_t *eax, void *arg) +console_handler(UNUSED int vcpu, int in, UNUSED int port, int bytes, + uint32_t *eax, UNUSED void *arg) { static int opened; @@ -128,9 +127,9 @@ console_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, } if (in) - *eax = ttyread(); + *eax = (uint32_t) ttyread(); else - ttywrite(*eax); + ttywrite((unsigned char) *eax); return (0); } @@ -142,12 +141,12 @@ static struct inout_port consport = { BVM_CONSOLE_PORT, 1, IOPORT_F_INOUT, - console_handler + console_handler, + NULL }; void init_bvmcons(void) { - register_inout(&consport); } diff --git a/bhyve/dbgport.c b/src/dbgport.c similarity index 85% rename from bhyve/dbgport.c rename to src/dbgport.c index 534ae65..54c968d 100644 --- a/bhyve/dbgport.c +++ b/src/dbgport.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,34 +27,32 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - #include #include #include #include +#include #include #include #include #include #include +#include +#include +#include +#include -#include "inout.h" -#include "dbgport.h" -#include "pci_lpc.h" - -#define BVM_DBG_PORT 0x224 -#define BVM_DBG_SIG ('B' << 8 | 'V') +#define BVM_DBG_PORT 0x224 +#define BVM_DBG_SIG ('B' << 8 | 'V') static int listen_fd, conn_fd; static struct sockaddr_in sin; static int -dbg_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, - uint32_t *eax, void *arg) +dbg_handler(UNUSED int vcpu, int in, UNUSED int port, int bytes, uint32_t *eax, + UNUSED void *arg) { char ch; int nwritten, nread, printonce; @@ -81,19 +80,19 @@ again: } if (in) { - nread = read(conn_fd, &ch, 1); + nread = (int) read(conn_fd, &ch, 1); if (nread == -1 && errno == EAGAIN) - *eax = -1; + *eax = (uint32_t) (-1); else if (nread == 1) - *eax = ch; + *eax = (uint32_t) ch; else { close(conn_fd); conn_fd = -1; goto again; } } else { - ch = *eax; - nwritten = write(conn_fd, &ch, 1); + ch = (char) *eax; + nwritten = (int) write(conn_fd, &ch, 1); if (nwritten != 1) { close(conn_fd); conn_fd = -1; @@ -108,7 +107,8 @@ static struct inout_port dbgport = { BVM_DBG_PORT, 1, IOPORT_F_INOUT, - dbg_handler + dbg_handler, + NULL }; SYSRES_IO(BVM_DBG_PORT, 4); diff --git a/src/firmware/kexec.c b/src/firmware/kexec.c new file mode 100644 index 0000000..6e45d09 --- /dev/null +++ b/src/firmware/kexec.c @@ -0,0 +1,262 @@ +/*- + * Copyright (c) 2015 xhyve developers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY ???, INC ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include + +#ifndef ALIGNUP +#define ALIGNUP(x, a) (((x - 1) & ~(a - 1)) + a) +#endif + +#define BASE_GDT 0x2000ull +#define BASE_ZEROPAGE 0x3000ull +#define BASE_CMDLINE 0x4000ull +#define BASE_KERNEL 0x100000ull +#define HDRS 0x53726448 /* SrdH */ + +static struct { + uintptr_t base; + size_t size; +} memory, kernel, ramdisk; + +static struct { + char *kernel; + char *initrd; + char *cmdline; +} config; + +static int +kexec_load_kernel(char *path, char *cmdline) { + uint64_t kernel_offset, kernel_size, kernel_init_size, kernel_start, mem_k; + size_t sz, cmdline_len; + volatile struct zero_page *zp; + FILE *f; + + if ((memory.size < (BASE_ZEROPAGE + sizeof(struct zero_page))) || + ((BASE_ZEROPAGE + sizeof(struct zero_page)) > BASE_CMDLINE)) + { + return -1; + } + + zp = ((struct zero_page *) (memory.base + ((off_t) BASE_ZEROPAGE))); + + memset(((void *) ((uintptr_t) zp)), 0, sizeof(struct zero_page)); + + if (!(f = fopen(path, "r"))) { + return -1; + } + + fseek(f, 0L, SEEK_END); + sz = (size_t) ftell(f); + + if (sz < (0x01f1 + sizeof(struct setup_header))) { + fclose(f); + return -1; + } + + fseek(f, 0x01f1, SEEK_SET); + + if (!fread(((void *) ((uintptr_t) &zp->setup_header)), 1, + sizeof(zp->setup_header), f)) + { + fclose(f); + return -1; + } + + if ((zp->setup_header.setup_sects == 0) || /* way way too old */ + (zp->setup_header.boot_flag != 0xaa55) || /* no boot magic */ + (zp->setup_header.header != HDRS) || /* way too old */ + (zp->setup_header.version < 0x020c) || /* too old */ + (!(zp->setup_header.loadflags & 1)) || /* no bzImage */ + (sz < (((zp->setup_header.setup_sects + 1) * 512) + + (zp->setup_header.syssize * 16)))) /* too small */ + { + /* we can't boot this kernel */ + fclose(f); + return -1; + } + + kernel_offset = ((zp->setup_header.setup_sects + 1) * 512); + kernel_size = (sz - kernel_offset); + kernel_init_size = ALIGNUP(zp->setup_header.init_size, 0x1000ull); + kernel_start = (zp->setup_header.relocatable_kernel) ? + ALIGNUP(BASE_KERNEL, zp->setup_header.kernel_alignment) : + zp->setup_header.pref_address; + + if ((kernel_start < BASE_KERNEL) || + (kernel_size > kernel_init_size) || /* XXX: always true? */ + ((kernel_start + kernel_init_size) > memory.size)) /* oom */ + { + fclose(f); + return -1; + } + + /* copy kernel */ + fseek(f, ((long) kernel_offset), SEEK_SET); + if (!fread(((void *) (memory.base + kernel_start)), 1, kernel_size, f)) { + fclose(f); + return -1; + } + + fclose(f); + + /* copy cmdline */ + cmdline_len = strlen(cmdline); + if (((cmdline_len + 1)> zp->setup_header.cmdline_size) || + ((BASE_CMDLINE + (cmdline_len + 1)) > kernel_start)) + { + return -1; + } + + memcpy(((void *) (memory.base + BASE_CMDLINE)), cmdline, cmdline_len); + memset(((void *) (memory.base + BASE_CMDLINE + cmdline_len)), '\0', 1); + zp->setup_header.cmd_line_ptr = ((uint32_t) BASE_CMDLINE); + zp->ext_cmd_line_ptr = ((uint32_t) (BASE_CMDLINE >> 32)); + + zp->setup_header.hardware_subarch = 0; /* PC */ + zp->setup_header.type_of_loader = 0xd; /* kexec */ + + mem_k = (memory.size - 0x100000) >> 10; /* assume memory base is at 0 */ + zp->alt_mem_k = (mem_k > 0xffffffff) ? 0xffffffff : ((uint32_t) mem_k); + + zp->e820_map[0].addr = 0x0000000000000000; + zp->e820_map[0].size = 0x000000000009fc00; + zp->e820_map[0].type = 1; + zp->e820_map[1].addr = 0x0000000000100000; + zp->e820_map[1].size = (memory.size - 0x0000000000100000); + zp->e820_map[1].type = 1; + zp->e820_entries = 2; + + kernel.base = kernel_start; + kernel.size = kernel_init_size; + + return 0; +} + +static int +kexec_load_ramdisk(char *path) { + uint64_t ramdisk_start; + volatile struct zero_page *zp; + size_t sz; + FILE *f; + + zp = ((struct zero_page *) (memory.base + BASE_ZEROPAGE)); + + if (!(f = fopen(path, "r"))) {; + return -1; + } + + fseek(f, 0L, SEEK_END); + sz = (size_t) ftell(f); + fseek(f, 0, SEEK_SET); + + ramdisk_start = ALIGNUP((kernel.base + kernel.size), 0x1000ull); + + if ((ramdisk_start + sz) > memory.size) { + /* not enough memory */ + fclose(f); + return -1; + } + + /* copy ramdisk */ + if (!fread(((void *) (memory.base + ramdisk_start)), 1, sz, f)) { + fclose(f); + return -1; + } + + fclose(f); + + zp->setup_header.ramdisk_image = ((uint32_t) ramdisk_start); + zp->ext_ramdisk_image = ((uint32_t) (ramdisk_start >> 32)); + zp->setup_header.ramdisk_size = ((uint32_t) sz); + zp->ext_ramdisk_size = ((uint32_t) (sz >> 32)); + + ramdisk.base = ramdisk_start; + ramdisk.size = sz; + + return 0; +} + +void +kexec_init(char *kernel_path, char *initrd_path, char *cmdline) { + config.kernel = kernel_path; + config.initrd = initrd_path; + config.cmdline = cmdline; +} + +uint64_t +kexec(void) +{ + uint64_t *gdt_entry; + void *gpa_map; + + gpa_map = xh_vm_map_gpa(0, xh_vm_get_lowmem_size()); + memory.base = (uintptr_t) gpa_map; + memory.size = xh_vm_get_lowmem_size(); + + if (kexec_load_kernel(config.kernel, + config.cmdline ? config.cmdline : "auto")) + { + fprintf(stderr, "kexec: failed to load kernel %s\n", config.kernel); + abort(); + } + + if (config.initrd && kexec_load_ramdisk(config.initrd)) { + fprintf(stderr, "kexec: failed to load initrd %s\n", config.initrd); + abort(); + } + + gdt_entry = ((uint64_t *) (memory.base + BASE_GDT)); + gdt_entry[0] = 0x0000000000000000; /* null */ + gdt_entry[1] = 0x0000000000000000; /* null */ + gdt_entry[2] = 0x00cf9a000000ffff; /* code */ + gdt_entry[3] = 0x00cf92000000ffff; /* data */ + + xh_vcpu_reset(0); + + xh_vm_set_desc(0, VM_REG_GUEST_GDTR, BASE_GDT, 0x1f, 0); + xh_vm_set_desc(0, VM_REG_GUEST_CS, 0, 0xffffffff, 0xc09b); + xh_vm_set_desc(0, VM_REG_GUEST_DS, 0, 0xffffffff, 0xc093); + xh_vm_set_desc(0, VM_REG_GUEST_ES, 0, 0xffffffff, 0xc093); + xh_vm_set_desc(0, VM_REG_GUEST_SS, 0, 0xffffffff, 0xc093); + xh_vm_set_register(0, VM_REG_GUEST_CS, 0x10); + xh_vm_set_register(0, VM_REG_GUEST_DS, 0x18); + xh_vm_set_register(0, VM_REG_GUEST_ES, 0x18); + xh_vm_set_register(0, VM_REG_GUEST_SS, 0x18); + xh_vm_set_register(0, VM_REG_GUEST_CR0, 0x21); /* enable protected mode */ + xh_vm_set_register(0, VM_REG_GUEST_RBP, 0); + xh_vm_set_register(0, VM_REG_GUEST_RDI, 0); + xh_vm_set_register(0, VM_REG_GUEST_RBX, 0); + xh_vm_set_register(0, VM_REG_GUEST_RFLAGS, 0x2); + xh_vm_set_register(0, VM_REG_GUEST_RSI, BASE_ZEROPAGE); + xh_vm_set_register(0, VM_REG_GUEST_RIP, kernel.base); + + return kernel.base; +} diff --git a/bhyve/inout.c b/src/inout.c similarity index 71% rename from bhyve/inout.c rename to src/inout.c index 929bb3c..1ba8e99 100644 --- a/bhyve/inout.c +++ b/src/inout.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,27 +27,18 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include - -#include -#include - -#include -#include -#include - #include #include +#include #include - -#include "bhyverun.h" -#include "inout.h" +#include +#include +#include +#include +#include +#include +#include +#include SET_DECLARE(inout_port_set, struct inout_port); @@ -55,32 +47,35 @@ SET_DECLARE(inout_port_set, struct inout_port); #define VERIFY_IOPORT(port, size) \ assert((port) >= 0 && (size) > 0 && ((port) + (size)) <= MAX_IOPORTS) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" static struct { - const char *name; - int flags; - inout_func_t handler; - void *arg; + const char *name; + int flags; + inout_func_t handler; + void *arg; } inout_handlers[MAX_IOPORTS]; +#pragma clang diagnostic pop static int -default_inout(struct vmctx *ctx, int vcpu, int in, int port, int bytes, - uint32_t *eax, void *arg) +default_inout(UNUSED int vcpu, int in, UNUSED int port, int bytes, + uint32_t *eax, UNUSED void *arg) { - if (in) { - switch (bytes) { - case 4: - *eax = 0xffffffff; - break; - case 2: - *eax = 0xffff; - break; - case 1: - *eax = 0xff; - break; - } - } - - return (0); + if (in) { + switch (bytes) { + case 4: + *eax = 0xffffffff; + break; + case 2: + *eax = 0xffff; + break; + case 1: + *eax = 0xff; + break; + } + } + + return (0); } static void @@ -94,14 +89,42 @@ register_default_iohandler(int start, int size) iop.name = "default"; iop.port = start; iop.size = size; - iop.flags = IOPORT_F_INOUT | IOPORT_F_DEFAULT; + iop.flags = (int) (IOPORT_F_INOUT | IOPORT_F_DEFAULT); iop.handler = default_inout; register_inout(&iop); } +static int +update_register(int vcpuid, enum vm_reg_name reg, + uint64_t val, int size) +{ + int error; + uint64_t origval; + + switch (size) { + case 1: + case 2: + error = xh_vm_get_register(vcpuid, reg, &origval); + if (error) + return (error); + val &= vie_size2mask(size); + val |= origval & ~vie_size2mask(size); + break; + case 4: + val &= 0xffffffffUL; + break; + case 8: + break; + default: + return (EINVAL); + } + + return xh_vm_set_register(vcpuid, reg, val); +} + int -emulate_inout(struct vmctx *ctx, int vcpu, struct vm_exit *vmexit, int strict) +emulate_inout(int vcpu, struct vm_exit *vmexit, int strict) { int addrsize, bytes, flags, in, port, prot, rep; uint32_t eax, val; @@ -141,7 +164,7 @@ emulate_inout(struct vmctx *ctx, int vcpu, struct vm_exit *vmexit, int strict) vis = &vmexit->u.inout_str; rep = vis->inout.rep; addrsize = vis->addrsize; - prot = in ? PROT_WRITE : PROT_READ; + prot = in ? XHYVE_PROT_WRITE : XHYVE_PROT_READ; assert(addrsize == 2 || addrsize == 4 || addrsize == 8); /* Index register */ @@ -152,18 +175,18 @@ emulate_inout(struct vmctx *ctx, int vcpu, struct vm_exit *vmexit, int strict) count = vis->count & vie_size2mask(addrsize); /* Limit number of back-to-back in/out emulations to 16 */ - iterations = MIN(count, 16); + iterations = min(count, 16); while (iterations > 0) { assert(retval == 0); if (vie_calculate_gla(vis->paging.cpu_mode, vis->seg_name, &vis->seg_desc, index, bytes, addrsize, prot, &gla)) { - vm_inject_gp(ctx, vcpu); + vm_inject_gp(vcpu); break; } - error = vm_copy_setup(ctx, vcpu, &vis->paging, gla, - bytes, prot, iov, nitems(iov), &fault); + error = xh_vm_copy_setup(vcpu, &vis->paging, gla, + ((size_t) bytes), prot, iov, nitems(iov), &fault); if (error) { retval = -1; /* Unrecoverable error */ break; @@ -174,33 +197,33 @@ emulate_inout(struct vmctx *ctx, int vcpu, struct vm_exit *vmexit, int strict) if (vie_alignment_check(vis->paging.cpl, bytes, vis->cr0, vis->rflags, gla)) { - vm_inject_ac(ctx, vcpu, 0); + vm_inject_ac(vcpu, 0); break; } val = 0; if (!in) - vm_copyin(ctx, vcpu, iov, &val, bytes); + xh_vm_copyin(iov, &val, ((size_t) bytes)); - retval = handler(ctx, vcpu, in, port, bytes, &val, arg); + retval = handler(vcpu, in, port, bytes, &val, arg); if (retval != 0) break; if (in) - vm_copyout(ctx, vcpu, &val, iov, bytes); + xh_vm_copyout(&val, iov, ((size_t) bytes)); /* Update index */ if (vis->rflags & PSL_D) - index -= bytes; + index -= ((uint64_t) bytes); else - index += bytes; + index += ((uint64_t) bytes); count--; iterations--; } /* Update index register */ - error = vie_update_register(ctx, vcpu, idxreg, index, addrsize); + error = update_register(vcpu, idxreg, index, addrsize); assert(error == 0); /* @@ -208,25 +231,23 @@ emulate_inout(struct vmctx *ctx, int vcpu, struct vm_exit *vmexit, int strict) * prefix. */ if (rep) { - error = vie_update_register(ctx, vcpu, VM_REG_GUEST_RCX, - count, addrsize); + error = update_register(vcpu, VM_REG_GUEST_RCX, count, addrsize); assert(error == 0); } /* Restart the instruction if more iterations remain */ if (retval == 0 && count != 0) { - error = vm_restart_instruction(ctx, vcpu); + error = xh_vm_restart_instruction(vcpu); assert(error == 0); } } else { eax = vmexit->u.inout.eax; val = eax & vie_size2mask(bytes); - retval = handler(ctx, vcpu, in, port, bytes, &val, arg); + retval = handler(vcpu, in, port, bytes, &val, arg); if (retval == 0 && in) { eax &= ~vie_size2mask(bytes); eax |= val & vie_size2mask(bytes); - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX, - eax); + error = xh_vm_set_register(vcpu, VM_REG_GUEST_RAX, eax); assert(error == 0); } } @@ -267,9 +288,9 @@ register_inout(struct inout_port *iop) * Verify that the new registration is not overwriting an already * allocated i/o range. */ - if ((iop->flags & IOPORT_F_DEFAULT) == 0) { + if ((((unsigned) iop->flags) & IOPORT_F_DEFAULT) == 0) { for (i = iop->port; i < iop->port + iop->size; i++) { - if ((inout_handlers[i].flags & IOPORT_F_DEFAULT) == 0) + if ((((unsigned) inout_handlers[i].flags) & IOPORT_F_DEFAULT) == 0) return (-1); } } diff --git a/bhyve/ioapic.c b/src/ioapic.c similarity index 90% rename from bhyve/ioapic.c rename to src/ioapic.c index 0ad69d9..7c7c42d 100644 --- a/bhyve/ioapic.c +++ b/src/ioapic.c @@ -1,6 +1,7 @@ /*- * Copyright (c) 2014 Hudson River Trading LLC * Written by: John H. Baldwin + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -25,15 +26,8 @@ * SUCH DAMAGE. */ -#include -__FBSDID("$FreeBSD$"); - -#include - -#include -#include - -#include "ioapic.h" +#include +#include /* * Assign PCI INTx interrupts to I/O APIC pins in a round-robin @@ -47,10 +41,10 @@ __FBSDID("$FreeBSD$"); static int pci_pins; void -ioapic_init(struct vmctx *ctx) +ioapic_init(void) { - if (vm_ioapic_pincount(ctx, &pci_pins) < 0) { + if (xh_vm_ioapic_pincount(&pci_pins) < 0) { pci_pins = 0; return; } diff --git a/src/md5c.c b/src/md5c.c new file mode 100644 index 0000000..dbbf1d1 --- /dev/null +++ b/src/md5c.c @@ -0,0 +1,284 @@ +/*- + * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm + * + * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All + * rights reserved. + * + * License to copy and use this software is granted provided that it + * is identified as the "RSA Data Security, Inc. MD5 Message-Digest + * Algorithm" in all material mentioning or referencing this software + * or this function. + * + * License is also granted to make and use derivative works provided + * that such works are identified as "derived from the RSA Data + * Security, Inc. MD5 Message-Digest Algorithm" in all material + * mentioning or referencing the derived work. + * + * RSA Data Security, Inc. makes no representations concerning either + * the merchantability of this software or the suitability of this + * software for any particular purpose. It is provided "as is" + * without express or implied warranty of any kind. + * + * These notices must be retained in any copies of any part of this + * documentation and/or software. + * + * This code is the same as the code published by RSA Inc. It has been + * edited for clarity and style only. + */ + +#include +#include +#include + +static void MD5Transform(u_int32_t [4], const unsigned char [64]); + +static unsigned char PADDING[64] = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +/* F, G, H and I are basic MD5 functions. */ +#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) +#define G(x, y, z) (((x) & (z)) | ((y) & (~z))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) +#define I(x, y, z) ((y) ^ ((x) | (~z))) + +/* ROTATE_LEFT rotates x left n bits. */ +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) + +/* + * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. + * Rotation is separate from addition to prevent recomputation. + */ +#define FF(a, b, c, d, x, s, ac) { \ + (a) += F ((b), (c), (d)) + (x) + (u_int32_t)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } +#define GG(a, b, c, d, x, s, ac) { \ + (a) += G ((b), (c), (d)) + (x) + (u_int32_t)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } +#define HH(a, b, c, d, x, s, ac) { \ + (a) += H ((b), (c), (d)) + (x) + (u_int32_t)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } +#define II(a, b, c, d, x, s, ac) { \ + (a) += I ((b), (c), (d)) + (x) + (u_int32_t)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } + +/* MD5 initialization. Begins an MD5 operation, writing a new context. */ + +void +MD5Init(context) + MD5_CTX *context; +{ + + context->count[0] = context->count[1] = 0; + + /* Load magic initialization constants. */ + context->state[0] = 0x67452301; + context->state[1] = 0xefcdab89; + context->state[2] = 0x98badcfe; + context->state[3] = 0x10325476; +} + +/* + * MD5 block update operation. Continues an MD5 message-digest + * operation, processing another message block, and updating the + * context. + */ + +void +MD5Update(context, in, inputLen) + MD5_CTX *context; + const void *in; + unsigned int inputLen; +{ + unsigned int i, index, partLen; + const unsigned char *input = in; + + /* Compute number of bytes mod 64 */ + index = (unsigned int)((context->count[0] >> 3) & 0x3F); + + /* Update number of bits */ + if ((context->count[0] += ((u_int32_t)inputLen << 3)) + < ((u_int32_t)inputLen << 3)) + context->count[1]++; + context->count[1] += ((u_int32_t)inputLen >> 29); + + partLen = 64 - index; + + /* Transform as many times as possible. */ + if (inputLen >= partLen) { + memcpy((void *)&context->buffer[index], (const void *)input, + partLen); + MD5Transform(context->state, context->buffer); + + for (i = partLen; i + 63 < inputLen; i += 64) + MD5Transform(context->state, &input[i]); + + index = 0; + } + else + i = 0; + + /* Buffer remaining input */ + memcpy((void *)&context->buffer[index], (const void *)&input[i], + inputLen-i); +} + +/* + * MD5 padding. Adds padding followed by original length. + */ + +static void +MD5Pad(MD5_CTX *context) +{ + unsigned char bits[8]; + unsigned int index, padLen; + + /* Save number of bits */ + memcpy(bits, context->count, 8); + + /* Pad out to 56 mod 64. */ + index = (unsigned int)((context->count[0] >> 3) & 0x3f); + padLen = (index < 56) ? (56 - index) : (120 - index); + MD5Update(context, PADDING, padLen); + + /* Append length (before padding) */ + MD5Update(context, bits, 8); +} + +/* + * MD5 finalization. Ends an MD5 message-digest operation, writing the + * the message digest and zeroizing the context. + */ + +void +MD5Final (digest, context) + unsigned char digest[16]; + MD5_CTX *context; +{ + /* Do padding. */ + MD5Pad(context); + + /* Store state in digest */ + memcpy(digest, context->state, 16); + + /* Zeroize sensitive information. */ + memset((void *)context, 0, sizeof (*context)); +} + +/* MD5 basic transformation. Transforms state based on block. */ + +static void +MD5Transform (state, block) + u_int32_t state[4]; + const unsigned char block[64]; +{ + u_int32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16]; + + memcpy(x, block, 64); + + /* Round 1 */ +#define S11 7 +#define S12 12 +#define S13 17 +#define S14 22 + FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ + FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ + FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ + FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ + FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ + FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ + FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ + FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ + FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ + FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ + FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ + FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ + FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ + FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ + FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ + FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ + + /* Round 2 */ +#define S21 5 +#define S22 9 +#define S23 14 +#define S24 20 + GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ + GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ + GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ + GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ + GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ + GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ + GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ + GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ + GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ + GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ + GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ + GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ + GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ + GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ + GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ + GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ + + /* Round 3 */ +#define S31 4 +#define S32 11 +#define S33 16 +#define S34 23 + HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ + HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ + HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ + HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ + HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ + HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ + HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ + HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ + HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ + HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ + HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ + HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ + HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ + HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ + HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ + HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ + + /* Round 4 */ +#define S41 6 +#define S42 10 +#define S43 15 +#define S44 21 + II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ + II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ + II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ + II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ + II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ + II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ + II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ + II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ + II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ + II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ + II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ + II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ + II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ + II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ + II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ + II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + + /* Zeroize sensitive information. */ + memset((void *)x, 0, sizeof (x)); +} diff --git a/bhyve/mem.c b/src/mem.c similarity index 85% rename from bhyve/mem.c rename to src/mem.c index 2a9f430..358d6d6 100644 --- a/bhyve/mem.c +++ b/src/mem.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2012 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -32,33 +33,30 @@ * so it can be searched within the range. */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include - -#include +#include #include -#include #include +#include +#include +#include +#include +#include +#include -#include "mem.h" - +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct mmio_rb_range { - RB_ENTRY(mmio_rb_range) mr_link; /* RB tree links */ - struct mem_range mr_param; - uint64_t mr_base; - uint64_t mr_end; + RB_ENTRY(mmio_rb_range) mr_link; /* RB tree links */ + struct mem_range mr_param; + uint64_t mr_base; + uint64_t mr_end; }; +#pragma clang diagnostic pop struct mmio_rb_tree; RB_PROTOTYPE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare); -RB_HEAD(mmio_rb_tree, mmio_rb_range) mmio_rb_root, mmio_rb_fallback; +static RB_HEAD(mmio_rb_tree, mmio_rb_range) mmio_rb_root, mmio_rb_fallback; /* * Per-vCPU cache. Since most accesses from a vCPU will be to @@ -132,38 +130,40 @@ mmio_rb_dump(struct mmio_rb_tree *rbt) } #endif -RB_GENERATE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare); +RB_GENERATE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare) static int -mem_read(void *ctx, int vcpu, uint64_t gpa, uint64_t *rval, int size, void *arg) +mem_read(UNUSED void *unused, int vcpu, uint64_t gpa, uint64_t *rval, int size, + void *arg) { int error; struct mem_range *mr = arg; - error = (*mr->handler)(ctx, vcpu, MEM_F_READ, gpa, size, - rval, mr->arg1, mr->arg2); + error = (*mr->handler)(vcpu, MEM_F_READ, gpa, size, rval, mr->arg1, + mr->arg2); return (error); } static int -mem_write(void *ctx, int vcpu, uint64_t gpa, uint64_t wval, int size, void *arg) +mem_write(UNUSED void* unused, int vcpu, uint64_t gpa, uint64_t wval, int size, + void *arg) { int error; struct mem_range *mr = arg; - error = (*mr->handler)(ctx, vcpu, MEM_F_WRITE, gpa, size, - &wval, mr->arg1, mr->arg2); + error = (*mr->handler)(vcpu, MEM_F_WRITE, gpa, size, &wval, mr->arg1, + mr->arg2); return (error); } int -emulate_mem(struct vmctx *ctx, int vcpu, uint64_t paddr, struct vie *vie, - struct vm_guest_paging *paging) +emulate_mem(int vcpu, uint64_t paddr, struct vie *vie, + struct vm_guest_paging *paging) { struct mmio_rb_range *entry; int err, immutable; - + pthread_rwlock_rdlock(&mmio_rwlock); /* * First check the per-vCPU cache @@ -202,8 +202,8 @@ emulate_mem(struct vmctx *ctx, int vcpu, uint64_t paddr, struct vie *vie, if (immutable) pthread_rwlock_unlock(&mmio_rwlock); - err = vmm_emulate_instruction(ctx, vcpu, paddr, vie, paging, - mem_read, mem_write, &entry->mr_param); + err = xh_vm_emulate_instruction(vcpu, paddr, vie, paging, mem_read, + mem_write, &entry->mr_param); if (!immutable) pthread_rwlock_unlock(&mmio_rwlock); diff --git a/bhyve/mevent.c b/src/mevent.c similarity index 91% rename from bhyve/mevent.c rename to src/mevent.c index 07d3baf..34a7d0e 100644 --- a/bhyve/mevent.c +++ b/src/mevent.c @@ -31,24 +31,18 @@ * using kqueue, and having events be persistent by default. */ -#include -__FBSDID("$FreeBSD$"); - #include #include #include #include #include #include - +#include #include #include #include - -#include -#include - -#include "mevent.h" +#include +#include #define MEVENT_MAX 64 @@ -64,18 +58,21 @@ static int mevent_timid = 43; static int mevent_pipefd[2]; static pthread_mutex_t mevent_lmutex = PTHREAD_MUTEX_INITIALIZER; -struct mevent { - void (*me_func)(int, enum ev_type, void *); +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +struct mevent { + void (*me_func)(int, enum ev_type, void *); #define me_msecs me_fd - int me_fd; - int me_timid; + int me_fd; + int me_timid; enum ev_type me_type; - void *me_param; - int me_cq; - int me_state; - int me_closefd; - LIST_ENTRY(mevent) me_list; + void *me_param; + int me_cq; + int me_state; + int me_closefd; + LIST_ENTRY(mevent) me_list; }; +#pragma clang diagnostic pop static LIST_HEAD(listhead, mevent) global_head, change_head; @@ -92,10 +89,10 @@ mevent_qunlock(void) } static void -mevent_pipe_read(int fd, enum ev_type type, void *param) +mevent_pipe_read(int fd, UNUSED enum ev_type type, UNUSED void *param) { char buf[MEVENT_MAX]; - int status; + ssize_t status; /* * Drain the pipe read side. The fd is non-blocking so this is @@ -169,14 +166,14 @@ mevent_kq_flags(struct mevent *mevp) } static int -mevent_kq_fflags(struct mevent *mevp) +mevent_kq_fflags(UNUSED struct mevent *mevp) { /* XXX nothing yet, perhaps EV_EOF for reads ? */ return (0); } static int -mevent_build(int mfd, struct kevent *kev) +mevent_build(UNUSED int mfd, struct kevent *kev) { struct mevent *mevp, *tmpp; int i; @@ -194,15 +191,15 @@ mevent_build(int mfd, struct kevent *kev) close(mevp->me_fd); } else { if (mevp->me_type == EVF_TIMER) { - kev[i].ident = mevp->me_timid; + kev[i].ident = (uintptr_t) mevp->me_timid; kev[i].data = mevp->me_msecs; } else { - kev[i].ident = mevp->me_fd; + kev[i].ident = (uintptr_t) mevp->me_fd; kev[i].data = 0; } - kev[i].filter = mevent_kq_filter(mevp); - kev[i].flags = mevent_kq_flags(mevp); - kev[i].fflags = mevent_kq_fflags(mevp); + kev[i].filter = (int16_t) mevent_kq_filter(mevp); + kev[i].flags = (uint16_t) mevent_kq_flags(mevp); + kev[i].fflags = (uint32_t) mevent_kq_fflags(mevp); kev[i].udata = mevp; i++; } @@ -388,11 +385,9 @@ mevent_delete_close(struct mevent *evp) static void mevent_set_name(void) { - - pthread_set_name_np(mevent_tid, "mevent"); } -void +__attribute__ ((noreturn)) void mevent_dispatch(void) { struct kevent changelist[MEVENT_MAX]; diff --git a/bhyve/mevent_test.c b/src/mevent_test.c similarity index 93% rename from bhyve/mevent_test.c rename to src/mevent_test.c index 9c68ff7..9ab3089 100644 --- a/bhyve/mevent_test.c +++ b/src/mevent_test.c @@ -33,19 +33,18 @@ * cc mevent_test.c mevent.c -lpthread */ +#include #include -#include #include #include #include -#include #include #include #include #include -#include "mevent.h" +#include #define TEST_PORT 4321 @@ -63,6 +62,15 @@ char *vmname = "test vm"; #define TEVSZ 4096 uint64_t tevbuf[TEVSZ]; +static __inline uint64_t rdtsc(void) +{ + unsigned a, d; + __asm__ __volatile__ ("cpuid"); + __asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d)); + + return (((uint64_t) a) | (((uint64_t) d) << 32)); +} + static void timer_print(void) { @@ -87,7 +95,7 @@ timer_print(void) max = diff; } - printf("timers done: usecs, min %ld, max %ld, mean %ld\n", min, max, + printf("timers done: usecs, min %llu, max %llu, mean %llu\n", min, max, sum/(TEVSZ - 1)); } @@ -246,11 +254,14 @@ acceptor(void *param) return (NULL); } -main() +int +main(void) { pthread_t tid; pthread_create(&tid, NULL, acceptor, NULL); mevent_dispatch(); + + return (0); } diff --git a/bhyve/mptbl.c b/src/mptbl.c similarity index 88% rename from bhyve/mptbl.c rename to src/mptbl.c index 904d103..df2b10b 100644 --- a/bhyve/mptbl.c +++ b/src/mptbl.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2012 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,20 +27,19 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include +// #include +#include #include #include - -#include "acpi.h" -#include "bhyverun.h" -#include "mptbl.h" -#include "pci_emul.h" +#include +#include +#include +#include +#include +#include +#include +#include #define MPTABLE_BASE 0xF0000 @@ -95,16 +95,15 @@ mpt_compute_checksum(void *base, size_t len) sum += *bytes++; } - return (256 - sum); + return ((uint8_t) (256 - sum)); } static void -mpt_build_mpfp(mpfps_t mpfp, vm_paddr_t gpa) +mpt_build_mpfp(mpfps_t mpfp, uint64_t gpa) { - memset(mpfp, 0, sizeof(*mpfp)); memcpy(mpfp->signature, MPFP_SIG, 4); - mpfp->pap = gpa + sizeof(*mpfp); + mpfp->pap = (uint32_t) (gpa + sizeof(*mpfp)); mpfp->length = 1; mpfp->spec_rev = MP_SPECREV; mpfp->checksum = mpt_compute_checksum(mpfp, sizeof(*mpfp)); @@ -130,7 +129,7 @@ mpt_build_proc_entries(proc_entry_ptr mpep, int ncpu) for (i = 0; i < ncpu; i++) { memset(mpep, 0, sizeof(*mpep)); mpep->type = MPCT_ENTRY_PROCESSOR; - mpep->apic_id = i; // XXX + mpep->apic_id = (uint8_t) i; // XXX mpep->apic_version = LAPIC_VERSION; mpep->cpu_flags = PROCENTRY_FLAG_EN; if (i == 0) @@ -187,7 +186,7 @@ mpt_build_ioapic_entries(io_apic_entry_ptr mpei, int id) memset(mpei, 0, sizeof(*mpei)); mpei->type = MPCT_ENTRY_IOAPIC; - mpei->apic_id = id; + mpei->apic_id = (uint8_t) id; mpei->apic_version = IOAPIC_VERSION; mpei->apic_flags = IOAPICENTRY_FLAG_EN; mpei->apic_address = IOAPIC_PADDR; @@ -210,8 +209,8 @@ mpt_count_ioint_entries(void) } static void -mpt_generate_pci_int(int bus, int slot, int pin, int pirq_pin, int ioapic_irq, - void *arg) +mpt_generate_pci_int(int bus, int slot, int pin, UNUSED int pirq_pin, + int ioapic_irq, void *arg) { int_entry_ptr *mpiep, mpie; @@ -225,10 +224,10 @@ mpt_generate_pci_int(int bus, int slot, int pin, int pirq_pin, int ioapic_irq, */ mpie->type = MPCT_ENTRY_INT; mpie->int_type = INTENTRY_TYPE_INT; - mpie->src_bus_id = bus; - mpie->src_bus_irq = slot << 2 | (pin - 1); + mpie->src_bus_id = (uint8_t) bus; + mpie->src_bus_irq = (uint8_t) (slot << 2 | (pin - 1)); mpie->dst_apic_id = mpie[-1].dst_apic_id; - mpie->dst_apic_int = ioapic_irq; + mpie->dst_apic_int = (uint8_t) ioapic_irq; *mpiep = mpie + 1; } @@ -249,13 +248,13 @@ mpt_build_ioint_entries(int_entry_ptr mpie, int id) memset(mpie, 0, sizeof(*mpie)); mpie->type = MPCT_ENTRY_INT; mpie->src_bus_id = 1; - mpie->dst_apic_id = id; + mpie->dst_apic_id = (uint8_t) id; /* * All default configs route IRQs from bus 0 to the first 16 * pins of the first I/O APIC with an APIC ID of 2. */ - mpie->dst_apic_int = pin; + mpie->dst_apic_int = (uint8_t) pin; switch (pin) { case 0: /* Pin 0 is an ExtINT pin. */ @@ -276,7 +275,7 @@ mpt_build_ioint_entries(int_entry_ptr mpie, int id) default: /* All other pins are identity mapped. */ mpie->int_type = INTENTRY_TYPE_INT; - mpie->src_bus_irq = pin; + mpie->src_bus_irq = (uint8_t) pin; break; } mpie++; @@ -296,7 +295,7 @@ mptable_add_oemtbl(void *tbl, int tblsz) } int -mptable_build(struct vmctx *ctx, int ncpu) +mptable_build(int ncpu) { mpcth_t mpch; bus_entry_ptr mpeb; @@ -308,7 +307,7 @@ mptable_build(struct vmctx *ctx, int ncpu) char *curraddr; char *startaddr; - startaddr = paddr_guest2host(ctx, MPTABLE_BASE, MPTABLE_MAX_LENGTH); + startaddr = paddr_guest2host(MPTABLE_BASE, MPTABLE_MAX_LENGTH); if (startaddr == NULL) { fprintf(stderr, "mptable requires mapped mem\n"); return (ENOMEM); @@ -340,7 +339,7 @@ mptable_build(struct vmctx *ctx, int ncpu) mpep = (proc_entry_ptr)curraddr; mpt_build_proc_entries(mpep, ncpu); - curraddr += sizeof(*mpep) * ncpu; + curraddr += sizeof(*mpep) * ((uint64_t) ncpu); mpch->entry_count += ncpu; mpeb = (bus_entry_ptr) curraddr; @@ -356,7 +355,7 @@ mptable_build(struct vmctx *ctx, int ncpu) mpie = (int_entry_ptr) curraddr; ioints = mpt_count_ioint_entries(); mpt_build_ioint_entries(mpie, 0); - curraddr += sizeof(*mpie) * ioints; + curraddr += sizeof(*mpie) * ((uint64_t) ioints); mpch->entry_count += ioints; mpie = (int_entry_ptr)curraddr; @@ -365,12 +364,13 @@ mptable_build(struct vmctx *ctx, int ncpu) mpch->entry_count += MPEII_NUM_LOCAL_IRQ; if (oem_tbl_start) { - mpch->oem_table_pointer = curraddr - startaddr + MPTABLE_BASE; - mpch->oem_table_size = oem_tbl_size; + mpch->oem_table_pointer = + (uint32_t) (curraddr - startaddr + MPTABLE_BASE); + mpch->oem_table_size = (uint16_t) oem_tbl_size; memcpy(curraddr, oem_tbl_start, oem_tbl_size); } - mpch->base_table_length = curraddr - (char *)mpch; + mpch->base_table_length = (uint16_t) (curraddr - (char *)mpch); mpch->checksum = mpt_compute_checksum(mpch, mpch->base_table_length); return (0); diff --git a/bhyve/pci_ahci.c b/src/pci_ahci.c similarity index 84% rename from bhyve/pci_ahci.c rename to src/pci_ahci.c index 35a0859..456a218 100644 --- a/bhyve/pci_ahci.c +++ b/src/pci_ahci.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2013 Zhixiang Yu + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,36 +27,32 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include -#include #include #include #include #include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "bhyverun.h" -#include "pci_emul.h" -#include "ahci.h" -#include "block_if.h" +#include +// #include +#include +#include +#include +#include +#include +#include +#include +#include #define MAX_PORTS 6 /* Intel ICH8 AHCI supports 6 ports */ @@ -83,13 +80,13 @@ enum sata_fis_type { #define PREVENT_ALLOW 0x1E #define READ_CAPACITY 0x25 #define READ_10 0x28 -#define POSITION_TO_ELEMENT 0x2B +// #define POSITION_TO_ELEMENT 0x2B #define READ_TOC 0x43 #define GET_EVENT_STATUS_NOTIFICATION 0x4A #define MODE_SENSE_10 0x5A #define REPORT_LUNS 0xA0 #define READ_12 0xA8 -#define READ_CD 0xBE +// #define READ_CD 0xBE /* * SCSI mode page codes @@ -102,18 +99,25 @@ enum sata_fis_type { */ #define ATA_SF_ENAB_SATA_SF 0x10 #define ATA_SATA_SF_AN 0x05 -#define ATA_SF_DIS_SATA_SF 0x90 +// #define ATA_SF_DIS_SATA_SF 0x90 /* * Debug printf */ #ifdef AHCI_DEBUG static FILE *dbg; -#define DPRINTF(format, arg...) do{fprintf(dbg, format, ##arg);fflush(dbg);}while(0) +#define DPRINTF(format, ...) \ + do { \ + fprintf(dbg, format, __VA_ARGS__); \ + fflush(dbg); \ + } while(0) #else -#define DPRINTF(format, arg...) +#define DPRINTF(format, ...) #endif -#define WPRINTF(format, arg...) printf(format, ##arg) +#define WPRINTF(format, ...) printf(format, __VA_ARGS__) + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct ahci_ioreq { struct blockif_req io_req; @@ -204,14 +208,15 @@ struct pci_ahci_softc { uint32_t lintr; struct ahci_port port[MAX_PORTS]; }; -#define ahci_ctx(sc) ((sc)->asc_pi->pi_vmctx) + +#pragma clang diagnostic pop static void ahci_handle_port(struct ahci_port *p); static inline void lba_to_msf(uint8_t *buf, int lba) { lba += 150; - buf[0] = (lba / 75) / 60; + buf[0] = (uint8_t) ((lba / 75) / 60); buf[1] = (lba / 75) % 60; buf[2] = lba % 75; } @@ -285,7 +290,11 @@ ahci_write_fis(struct ahci_port *p, enum sata_fis_type ft, uint8_t *fis) len = 20; irq = (fis[1] & (1 << 6)) ? AHCI_P_IX_PS : 0; break; - default: + case FIS_TYPE_REGH2D: + case FIS_TYPE_DMAACT: + case FIS_TYPE_DMASETUP: + case FIS_TYPE_DATA: + case FIS_TYPE_BIST: WPRINTF("unsupported fis type %d\n", ft); return; } @@ -295,7 +304,7 @@ ahci_write_fis(struct ahci_port *p, enum sata_fis_type ft, uint8_t *fis) } memcpy(p->rfis + offset, fis, len); if (irq) { - p->is |= irq; + p->is |= ((unsigned) irq); ahci_generate_intr(p->pr_sc); } } @@ -321,18 +330,18 @@ ahci_write_fis_sdb(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t tfd) memset(fis, 0, sizeof(fis)); fis[0] = FIS_TYPE_SETDEVBITS; fis[1] = (1 << 6); - fis[2] = tfd; + fis[2] = (uint8_t) tfd; fis[3] = error; if (fis[2] & ATA_S_ERROR) { - p->err_cfis[0] = slot; - p->err_cfis[2] = tfd; + p->err_cfis[0] = (uint8_t) slot; + p->err_cfis[2] = (uint8_t) tfd; p->err_cfis[3] = error; memcpy(&p->err_cfis[4], cfis + 4, 16); } else { - *(uint32_t *)(fis + 4) = (1 << slot); + *(uint32_t *)((void *) (fis + 4)) = (1 << slot); p->sact &= ~(1 << slot); } - p->tfd &= ~0x77; + p->tfd &= ~((unsigned) 0x77); p->tfd |= tfd; ahci_write_fis(p, FIS_TYPE_SETDEVBITS, fis); } @@ -378,9 +387,9 @@ ahci_write_fis_d2h_ncq(struct ahci_port *p, int slot) p->tfd = ATA_S_READY | ATA_S_DSC; memset(fis, 0, sizeof(fis)); fis[0] = FIS_TYPE_REGD2H; - fis[1] = 0; /* No interrupt */ - fis[2] = p->tfd; /* Status */ - fis[3] = 0; /* No error */ + fis[1] = 0; /* No interrupt */ + fis[2] = (uint8_t) p->tfd; /* Status */ + fis[3] = 0; /* No error */ p->ci &= ~(1 << slot); ahci_write_fis(p, FIS_TYPE_REGD2H, fis); } @@ -413,7 +422,7 @@ ahci_check_stopped(struct ahci_port *p) if (!(p->cmd & AHCI_P_CMD_ST)) { if (p->pending == 0) { p->ccs = 0; - p->cmd &= ~(AHCI_P_CMD_CR | AHCI_P_CMD_CCS_MASK); + p->cmd &= ~((unsigned) (AHCI_P_CMD_CR | AHCI_P_CMD_CCS_MASK)); p->ci = 0; p->sact = 0; p->waitforclear = 0; @@ -430,7 +439,7 @@ ahci_port_stop(struct ahci_port *p) int ncq; int error; - assert(pthread_mutex_isowned_np(&p->pr_sc->mtx)); + ncq = 0; TAILQ_FOREACH(aior, &p->iobhd, io_blist) { /* @@ -445,7 +454,9 @@ ahci_port_stop(struct ahci_port *p) if (cfis[2] == ATA_WRITE_FPDMA_QUEUED || cfis[2] == ATA_READ_FPDMA_QUEUED || cfis[2] == ATA_SEND_FPDMA_QUEUED) + { ncq = 1; + } if (ncq) p->sact &= ~(1 << slot); @@ -530,7 +541,7 @@ ata_string(uint8_t *dest, const char *src, int len) for (i = 0; i < len; i++) { if (*src) - dest[i ^ 1] = *src++; + dest[i ^ 1] = (uint8_t) *src++; else dest[i ^ 1] = ' '; } @@ -543,7 +554,7 @@ atapi_string(uint8_t *dest, const char *src, int len) for (i = 0; i < len; i++) { if (*src) - dest[i] = *src++; + dest[i] = (uint8_t) *src++; else dest[i] = ' '; } @@ -561,22 +572,23 @@ ahci_build_iov(struct ahci_port *p, struct ahci_ioreq *aior, uint32_t dbcsz; /* Copy part of PRDT between 'done' and 'len' bytes into the iov. */ - skip = aior->done; - left = aior->len - aior->done; + skip = (int) aior->done; + left = (int) (aior->len - aior->done); todo = 0; for (i = 0, j = 0; i < prdtl && j < BLOCKIF_IOV_MAX && left > 0; i++, prdt++) { dbcsz = (prdt->dbc & DBCMASK) + 1; /* Skip already done part of the PRDT */ - if (dbcsz <= skip) { + if (dbcsz <= ((uint32_t) skip)) { skip -= dbcsz; continue; } - dbcsz -= skip; - if (dbcsz > left) - dbcsz = left; - breq->br_iov[j].iov_base = paddr_guest2host(ahci_ctx(p->pr_sc), - prdt->dba + skip, dbcsz); + dbcsz -= ((unsigned) skip); + if (dbcsz > ((uint32_t) left)) { + dbcsz = ((uint32_t) left); + } + breq->br_iov[j].iov_base = + paddr_guest2host((prdt->dba + ((uint64_t) skip)), dbcsz); breq->br_iov[j].iov_len = dbcsz; todo += dbcsz; left -= dbcsz; @@ -590,8 +602,8 @@ ahci_build_iov(struct ahci_port *p, struct ahci_ioreq *aior, todo -= extra; assert(todo > 0); while (extra > 0) { - if (breq->br_iov[j - 1].iov_len > extra) { - breq->br_iov[j - 1].iov_len -= extra; + if (breq->br_iov[j - 1].iov_len > ((size_t) extra)) { + breq->br_iov[j - 1].iov_len -= ((size_t) extra); break; } extra -= breq->br_iov[j - 1].iov_len; @@ -601,7 +613,7 @@ ahci_build_iov(struct ahci_port *p, struct ahci_ioreq *aior, breq->br_iovcnt = j; breq->br_resid = todo; - aior->done += todo; + aior->done += ((unsigned) todo); aior->more = (aior->done < aior->len && i < prdtl); } @@ -616,8 +628,8 @@ ahci_handle_rw(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t done) uint32_t len; int err, first, ncq, readop; - prdt = (struct ahci_prdt_entry *)(cfis + 0x80); - hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE); + prdt = (struct ahci_prdt_entry *)((void *) (cfis + 0x80)); + hdr = (struct ahci_cmd_hdr *)((void *) (p->cmd_lst + slot * AHCI_CL_SIZE)); ncq = 0; readop = 1; first = (done == 0); @@ -636,7 +648,7 @@ ahci_handle_rw(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t done) ((uint64_t)cfis[6] << 16) | ((uint64_t)cfis[5] << 8) | cfis[4]; - len = cfis[11] << 8 | cfis[3]; + len = (uint32_t) (cfis[11] << 8 | cfis[3]); if (!len) len = 65536; ncq = 1; @@ -649,18 +661,18 @@ ahci_handle_rw(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t done) ((uint64_t)cfis[6] << 16) | ((uint64_t)cfis[5] << 8) | cfis[4]; - len = cfis[13] << 8 | cfis[12]; + len = (uint32_t) (cfis[13] << 8 | cfis[12]); if (!len) len = 65536; } else { - lba = ((cfis[7] & 0xf) << 24) | (cfis[6] << 16) | - (cfis[5] << 8) | cfis[4]; + lba = (uint64_t) (((cfis[7] & 0xf) << 24) | (cfis[6] << 16) | + (cfis[5] << 8) | cfis[4]); len = cfis[12]; if (!len) len = 256; } - lba *= blockif_sectsz(p->bctx); - len *= blockif_sectsz(p->bctx); + lba *= (uint64_t) blockif_sectsz(p->bctx); + len *= (uint32_t) blockif_sectsz(p->bctx); /* Pull request off free list */ aior = STAILQ_FIRST(&p->iofhd); @@ -672,7 +684,7 @@ ahci_handle_rw(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t done) aior->len = len; aior->done = done; breq = &aior->io_req; - breq->br_offset = lba + done; + breq->br_offset = (off_t) (lba + done); ahci_build_iov(p, aior, prdt, hdr->prdtl); /* Mark this command in-flight. */ @@ -734,21 +746,21 @@ read_prdt(struct ahci_port *p, int slot, uint8_t *cfis, void *to; int i, len; - hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE); + hdr = (struct ahci_cmd_hdr *)((void *) (p->cmd_lst + slot * AHCI_CL_SIZE)); len = size; to = buf; - prdt = (struct ahci_prdt_entry *)(cfis + 0x80); + prdt = (struct ahci_prdt_entry *)((void *) (cfis + 0x80)); for (i = 0; i < hdr->prdtl && len; i++) { uint8_t *ptr; uint32_t dbcsz; int sublen; dbcsz = (prdt->dbc & DBCMASK) + 1; - ptr = paddr_guest2host(ahci_ctx(p->pr_sc), prdt->dba, dbcsz); - sublen = len < dbcsz ? len : dbcsz; + ptr = paddr_guest2host(prdt->dba, dbcsz); + sublen = ((len < ((int) dbcsz)) ? len : ((int) dbcsz)); memcpy(to, ptr, sublen); len -= sublen; - to += sublen; + to = (uint8_t *) (((uintptr_t) to) + ((uintptr_t) sublen)); prdt++; } } @@ -766,11 +778,11 @@ ahci_handle_dsm_trim(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t done first = (done == 0); if (cfis[2] == ATA_DATA_SET_MANAGEMENT) { - len = (uint16_t)cfis[13] << 8 | cfis[12]; + len = (uint32_t) ((((uint16_t) cfis[13]) << 8) | cfis[12]); len *= 512; ncq = 0; } else { /* ATA_SEND_FPDMA_QUEUED */ - len = (uint16_t)cfis[11] << 8 | cfis[3]; + len = (uint32_t) ((((uint16_t) cfis[11]) << 8) | cfis[3]); len *= 512; ncq = 1; } @@ -784,7 +796,7 @@ next: ((uint64_t)entry[2] << 16) | ((uint64_t)entry[1] << 8) | entry[0]; - elen = (uint16_t)entry[7] << 8 | entry[6]; + elen = (uint32_t) ((((uint16_t) entry[7]) << 8) | entry[6]); done += 8; if (elen == 0) { if (done >= len) { @@ -811,8 +823,8 @@ next: aior->more = (len != done); breq = &aior->io_req; - breq->br_offset = elba * blockif_sectsz(p->bctx); - breq->br_resid = elen * blockif_sectsz(p->bctx); + breq->br_offset = (off_t) (elba * ((uint64_t) blockif_sectsz(p->bctx))); + breq->br_resid = elen * ((unsigned) blockif_sectsz(p->bctx)); /* * Mark this command in-flight. @@ -840,24 +852,24 @@ write_prdt(struct ahci_port *p, int slot, uint8_t *cfis, void *from; int i, len; - hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE); + hdr = (struct ahci_cmd_hdr *)((void *) (p->cmd_lst + slot * AHCI_CL_SIZE)); len = size; from = buf; - prdt = (struct ahci_prdt_entry *)(cfis + 0x80); + prdt = (struct ahci_prdt_entry *)((void *) (cfis + 0x80)); for (i = 0; i < hdr->prdtl && len; i++) { uint8_t *ptr; uint32_t dbcsz; int sublen; dbcsz = (prdt->dbc & DBCMASK) + 1; - ptr = paddr_guest2host(ahci_ctx(p->pr_sc), prdt->dba, dbcsz); - sublen = len < dbcsz ? len : dbcsz; + ptr = paddr_guest2host(prdt->dba, dbcsz); + sublen = (len < ((int) dbcsz)) ? len : ((int) dbcsz); memcpy(ptr, from, sublen); len -= sublen; - from += sublen; + from = (void *) (((uintptr_t) from) + ((uintptr_t) sublen)); prdt++; } - hdr->prdbc = size - len; + hdr->prdbc = (uint32_t) (size - len); } static void @@ -868,7 +880,7 @@ ahci_checksum(uint8_t *buf, int size) for (i = 0; i < size - 1; i++) sum += buf[i]; - buf[size - 1] = 0x100 - sum; + buf[size - 1] = (uint8_t) (0x100 - sum); } static void @@ -877,7 +889,7 @@ ahci_handle_read_log(struct ahci_port *p, int slot, uint8_t *cfis) struct ahci_cmd_hdr *hdr; uint8_t buf[512]; - hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE); + hdr = (struct ahci_cmd_hdr *)((void *) (p->cmd_lst + slot * AHCI_CL_SIZE)); if (p->atapi || hdr->prdtl == 0 || cfis[4] != 0x10 || cfis[5] != 0 || cfis[9] != 0 || cfis[12] != 1 || cfis[13] != 0) { ahci_write_fis_d2h(p, slot, cfis, @@ -900,7 +912,7 @@ handle_identify(struct ahci_port *p, int slot, uint8_t *cfis) { struct ahci_cmd_hdr *hdr; - hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE); + hdr = (struct ahci_cmd_hdr *)((void *) (p->cmd_lst + slot * AHCI_CL_SIZE)); if (p->atapi || hdr->prdtl == 0) { ahci_write_fis_d2h(p, slot, cfis, (ATA_E_ABORT << 8) | ATA_S_READY | ATA_S_ERROR); @@ -914,7 +926,7 @@ handle_identify(struct ahci_port *p, int slot, uint8_t *cfis) ro = blockif_is_ro(p->bctx); candelete = blockif_candelete(p->bctx); sectsz = blockif_sectsz(p->bctx); - sectors = blockif_size(p->bctx) / sectsz; + sectors = (uint64_t) (blockif_size(p->bctx) / sectsz); blockif_chs(p->bctx, &cyl, &heads, &sech); blockif_psectsz(p->bctx, &psectsz, &psectoff); memset(buf, 0, sizeof(buf)); @@ -931,10 +943,10 @@ handle_identify(struct ahci_port *p, int slot, uint8_t *cfis) buf[50] = (1 << 14); buf[53] = (1 << 1 | 1 << 2); if (p->mult_sectors) - buf[59] = (0x100 | p->mult_sectors); + buf[59] = (uint16_t) (0x100 | p->mult_sectors); if (sectors <= 0x0fffffff) { - buf[60] = sectors; - buf[61] = (sectors >> 16); + buf[60] = (uint16_t) sectors; + buf[61] = (uint16_t)(sectors >> 16); } else { buf[60] = 0xffff; buf[61] = 0x0fff; @@ -968,9 +980,9 @@ handle_identify(struct ahci_port *p, int slot, uint8_t *cfis) buf[88] = 0x7f; if (p->xfermode & ATA_UDMA0) buf[88] |= (1 << ((p->xfermode & 7) + 8)); - buf[100] = sectors; - buf[101] = (sectors >> 16); - buf[102] = (sectors >> 32); + buf[100] = (uint16_t) sectors; + buf[101] = (uint16_t) (sectors >> 16); + buf[102] = (uint16_t) (sectors >> 32); buf[103] = (sectors >> 48); if (candelete && !ro) { buf[69] |= ATA_SUPPORT_RZAT | ATA_SUPPORT_DRAT; @@ -986,8 +998,8 @@ handle_identify(struct ahci_port *p, int slot, uint8_t *cfis) } if (sectsz > 512) { buf[106] |= 0x1000; - buf[117] = sectsz / 2; - buf[118] = ((sectsz / 2) >> 16); + buf[117] = (uint16_t) (sectsz / 2); + buf[118] = (uint16_t) ((sectsz / 2) >> 16); } buf[119] = (ATA_SUPPORT_RWLOGDMAEXT | 1 << 14); buf[120] = (ATA_SUPPORT_RWLOGDMAEXT | 1 << 14); @@ -1070,7 +1082,7 @@ atapi_inquiry(struct ahci_port *p, int slot, uint8_t *cfis) } else { p->sense_key = ATA_SENSE_ILLEGAL_REQUEST; p->asc = 0x24; - tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR; + tfd = (uint32_t) ((p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR); cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN; ahci_write_fis_d2h(p, slot, cfis, tfd); return; @@ -1097,14 +1109,52 @@ atapi_inquiry(struct ahci_port *p, int slot, uint8_t *cfis) ahci_write_fis_d2h(p, slot, cfis, ATA_S_READY | ATA_S_DSC); } +static __inline void +be16enc(void *pp, uint16_t u) +{ + unsigned char *p = (unsigned char *)pp; + + p[0] = (u >> 8) & 0xff; + p[1] = u & 0xff; +} + +static __inline uint16_t +be16dec(const void *pp) +{ + unsigned char const *p = (unsigned char const *)pp; + + return ((uint16_t) ((((uint32_t) p[0]) << 8) | ((uint32_t) p[1]))); +} + +static __inline void +be32enc(void *pp, uint32_t u) +{ + unsigned char *p = (unsigned char *)pp; + + p[0] = (u >> 24) & 0xff; + p[1] = (u >> 16) & 0xff; + p[2] = (u >> 8) & 0xff; + p[3] = u & 0xff; +} + +static __inline uint32_t +be32dec(const void *pp) +{ + unsigned char const *p = (unsigned char const *)pp; + + return (uint32_t) ((((uint64_t) p[0]) << 24) | + (((uint64_t) p[1]) << 16) | (((uint64_t) p[2]) << 8) | + ((uint64_t) p[3])); +} + static void atapi_read_capacity(struct ahci_port *p, int slot, uint8_t *cfis) { uint8_t buf[8]; uint64_t sectors; - sectors = blockif_size(p->bctx) / 2048; - be32enc(buf, sectors - 1); + sectors = (uint64_t) (blockif_size(p->bctx) / 2048); + be32enc(buf, ((uint32_t) (sectors - 1))); be32enc(buf + 4, 2048); cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN; write_prdt(p, slot, cfis, buf, sizeof(buf)); @@ -1135,7 +1185,7 @@ atapi_read_toc(struct ahci_port *p, int slot, uint8_t *cfis) uint32_t tfd; p->sense_key = ATA_SENSE_ILLEGAL_REQUEST; p->asc = 0x24; - tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR; + tfd = (uint32_t) ((p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR); cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN; ahci_write_fis_d2h(p, slot, cfis, tfd); return; @@ -1163,18 +1213,18 @@ atapi_read_toc(struct ahci_port *p, int slot, uint8_t *cfis) *bp++ = 0x14; *bp++ = 0xaa; *bp++ = 0; - sectors = blockif_size(p->bctx) / blockif_sectsz(p->bctx); + sectors = (uint64_t) (blockif_size(p->bctx) / blockif_sectsz(p->bctx)); sectors >>= 2; if (msf) { *bp++ = 0; - lba_to_msf(bp, sectors); + lba_to_msf(bp, ((int) sectors)); bp += 3; } else { - be32enc(bp, sectors); + be32enc(bp, ((uint32_t) sectors)); bp += 4; } - size = bp - buf; - be16enc(buf, size - 2); + size = (int) (bp - buf); + be16enc(buf, ((uint16_t) (size - 2))); if (len > size) len = size; write_prdt(p, slot, cfis, buf, len); @@ -1190,7 +1240,7 @@ atapi_read_toc(struct ahci_port *p, int slot, uint8_t *cfis) buf[1] = 0xa; buf[2] = 0x1; buf[3] = 0x1; - if (len > sizeof(buf)) + if (((size_t) len) > sizeof(buf)) len = sizeof(buf); write_prdt(p, slot, cfis, buf, len); cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN; @@ -1240,14 +1290,14 @@ atapi_read_toc(struct ahci_port *p, int slot, uint8_t *cfis) *bp++ = 0; *bp++ = 0; *bp++ = 0; - sectors = blockif_size(p->bctx) / blockif_sectsz(p->bctx); + sectors = (uint64_t) (blockif_size(p->bctx) / blockif_sectsz(p->bctx)); sectors >>= 2; if (msf) { *bp++ = 0; - lba_to_msf(bp, sectors); + lba_to_msf(bp, ((int) sectors)); bp += 3; } else { - be32enc(bp, sectors); + be32enc(bp, ((uint32_t) sectors)); bp += 4; } @@ -1269,8 +1319,8 @@ atapi_read_toc(struct ahci_port *p, int slot, uint8_t *cfis) *bp++ = 0; } - size = bp - buf; - be16enc(buf, size - 2); + size = (int) (bp - buf); + be16enc(buf, ((uint16_t) (size - 2))); if (len > size) len = size; write_prdt(p, slot, cfis, buf, len); @@ -1284,7 +1334,7 @@ atapi_read_toc(struct ahci_port *p, int slot, uint8_t *cfis) p->sense_key = ATA_SENSE_ILLEGAL_REQUEST; p->asc = 0x24; - tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR; + tfd = (uint32_t) ((p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR); cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN; ahci_write_fis_d2h(p, slot, cfis, tfd); break; @@ -1320,8 +1370,8 @@ atapi_read(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t done) sc = p->pr_sc; acmd = cfis + 0x40; - hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE); - prdt = (struct ahci_prdt_entry *)(cfis + 0x80); + hdr = (struct ahci_cmd_hdr *)((void *) (p->cmd_lst + slot * AHCI_CL_SIZE)); + prdt = (struct ahci_prdt_entry *)((void *) (cfis + 0x80)); lba = be32dec(acmd + 2); if (acmd[0] == READ_10) @@ -1346,7 +1396,7 @@ atapi_read(struct ahci_port *p, int slot, uint8_t *cfis, uint32_t done) aior->len = len; aior->done = done; breq = &aior->io_req; - breq->br_offset = lba + done; + breq->br_offset = (off_t) (lba + ((uint64_t) done)); ahci_build_iov(p, aior, prdt, hdr->prdtl); /* Mark this command in-flight. */ @@ -1368,7 +1418,7 @@ atapi_request_sense(struct ahci_port *p, int slot, uint8_t *cfis) acmd = cfis + 0x40; len = acmd[4]; - if (len > sizeof(buf)) + if (((size_t) len) > sizeof(buf)) len = sizeof(buf); memset(buf, 0, len); buf[0] = 0x70 | (1 << 7); @@ -1386,6 +1436,8 @@ atapi_start_stop_unit(struct ahci_port *p, int slot, uint8_t *cfis) uint8_t *acmd = cfis + 0x40; uint32_t tfd; + tfd = 0; + switch (acmd[4] & 3) { case 0: case 1: @@ -1398,7 +1450,7 @@ atapi_start_stop_unit(struct ahci_port *p, int slot, uint8_t *cfis) cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN; p->sense_key = ATA_SENSE_ILLEGAL_REQUEST; p->asc = 0x53; - tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR; + tfd = (uint32_t) ((p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR); break; } ahci_write_fis_d2h(p, slot, cfis, tfd); @@ -1412,6 +1464,8 @@ atapi_mode_sense(struct ahci_port *p, int slot, uint8_t *cfis) uint8_t pc, code; int len; + tfd = 0; + acmd = cfis + 0x40; len = be16dec(acmd + 7); pc = acmd[2] >> 6; @@ -1424,8 +1478,9 @@ atapi_mode_sense(struct ahci_port *p, int slot, uint8_t *cfis) { uint8_t buf[16]; - if (len > sizeof(buf)) + if (((size_t) len) > sizeof(buf)) { len = sizeof(buf); + } memset(buf, 0, sizeof(buf)); be16enc(buf, 16 - 2); @@ -1441,8 +1496,9 @@ atapi_mode_sense(struct ahci_port *p, int slot, uint8_t *cfis) { uint8_t buf[30]; - if (len > sizeof(buf)) + if (((size_t) len) > sizeof(buf)) { len = sizeof(buf); + } memset(buf, 0, sizeof(buf)); be16enc(buf, 30 - 2); @@ -1459,20 +1515,19 @@ atapi_mode_sense(struct ahci_port *p, int slot, uint8_t *cfis) } default: goto error; - break; } break; case 3: p->sense_key = ATA_SENSE_ILLEGAL_REQUEST; p->asc = 0x39; - tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR; + tfd = (uint32_t) ((p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR); break; error: case 1: case 2: p->sense_key = ATA_SENSE_ILLEGAL_REQUEST; p->asc = 0x24; - tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR; + tfd = (uint32_t) ((p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR); break; } cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN; @@ -1492,14 +1547,15 @@ atapi_get_event_status_notification(struct ahci_port *p, int slot, if (!(acmd[1] & 1)) { p->sense_key = ATA_SENSE_ILLEGAL_REQUEST; p->asc = 0x24; - tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR; + tfd = (uint32_t) ((p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR); } else { uint8_t buf[8]; int len; len = be16dec(acmd + 7); - if (len > sizeof(buf)) + if (((size_t) len) > sizeof(buf)) { len = sizeof(buf); + } memset(buf, 0, sizeof(buf)); be16enc(buf, 8 - 2); @@ -1572,8 +1628,8 @@ handle_packet_cmd(struct ahci_port *p, int slot, uint8_t *cfis) cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN; p->sense_key = ATA_SENSE_ILLEGAL_REQUEST; p->asc = 0x20; - ahci_write_fis_d2h(p, slot, cfis, (p->sense_key << 12) | - ATA_S_READY | ATA_S_ERROR); + ahci_write_fis_d2h(p, slot, cfis, ((uint32_t) (p->sense_key << 12)) | + ((uint32_t) (ATA_S_READY | ATA_S_ERROR))); break; } } @@ -1722,11 +1778,11 @@ ahci_handle_slot(struct ahci_port *p, int slot) int cfl; sc = p->pr_sc; - hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE); + hdr = (struct ahci_cmd_hdr *)((void *) (p->cmd_lst + slot * AHCI_CL_SIZE)); cfl = (hdr->flags & 0x1f) * 4; - cfis = paddr_guest2host(ahci_ctx(sc), hdr->ctba, - 0x80 + hdr->prdtl * sizeof(struct ahci_prdt_entry)); - prdt = (struct ahci_prdt_entry *)(cfis + 0x80); + cfis = paddr_guest2host(hdr->ctba, + 0x80 + hdr->prdtl * sizeof(struct ahci_prdt_entry)); + prdt = (struct ahci_prdt_entry *)((void *) (cfis + 0x80)); #ifdef AHCI_DEBUG DPRINTF("\ncfis:"); @@ -1778,9 +1834,9 @@ ahci_handle_port(struct ahci_port *p) if (p->waitforclear) break; if ((p->ci & ~p->pending & (1 << p->ccs)) != 0) { - p->cmd &= ~AHCI_P_CMD_CCS_MASK; + p->cmd &= ~((unsigned) AHCI_P_CMD_CCS_MASK); p->cmd |= p->ccs << AHCI_P_CMD_CCS_SHIFT; - ahci_handle_slot(p, p->ccs); + ahci_handle_slot(p, ((int) p->ccs)); } } } @@ -1808,7 +1864,7 @@ ata_ioreq_cb(struct blockif_req *br, int err) cfis = aior->cfis; slot = aior->slot; sc = p->pr_sc; - hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + slot * AHCI_CL_SIZE); + hdr = (struct ahci_cmd_hdr *)((void *) (p->cmd_lst + slot * AHCI_CL_SIZE)); if (cfis[2] == ATA_WRITE_FPDMA_QUEUED || cfis[2] == ATA_READ_FPDMA_QUEUED || @@ -1881,7 +1937,8 @@ atapi_ioreq_cb(struct blockif_req *br, int err) cfis = aior->cfis; slot = aior->slot; sc = p->pr_sc; - hdr = (struct ahci_cmd_hdr *)(p->cmd_lst + aior->slot * AHCI_CL_SIZE); + hdr = (struct ahci_cmd_hdr *) + ((void *) (p->cmd_lst + aior->slot * AHCI_CL_SIZE)); pthread_mutex_lock(&sc->mtx); @@ -1908,7 +1965,7 @@ atapi_ioreq_cb(struct blockif_req *br, int err) } else { p->sense_key = ATA_SENSE_ILLEGAL_REQUEST; p->asc = 0x21; - tfd = (p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR; + tfd = (uint32_t) ((p->sense_key << 12) | ATA_S_READY | ATA_S_ERROR); } cfis[4] = (cfis[4] & ~7) | ATA_I_CMD | ATA_I_IN; ahci_write_fis_d2h(p, slot, cfis, tfd); @@ -1932,7 +1989,7 @@ pci_ahci_ioreq_init(struct ahci_port *pr) int i; pr->ioqsz = blockif_queuesz(pr->bctx); - pr->ioreq = calloc(pr->ioqsz, sizeof(struct ahci_ioreq)); + pr->ioreq = calloc(((size_t) pr->ioqsz), sizeof(struct ahci_ioreq)); STAILQ_INIT(&pr->iofhd); /* @@ -1955,7 +2012,7 @@ pci_ahci_ioreq_init(struct ahci_port *pr) static void pci_ahci_port_write(struct pci_ahci_softc *sc, uint64_t offset, uint64_t value) { - int port = (offset - AHCI_OFFSET) / AHCI_STEP; + int port = (int) ((offset - AHCI_OFFSET) / AHCI_STEP); offset = (offset - AHCI_OFFSET) % AHCI_STEP; struct ahci_port *p = &sc->port[port]; @@ -1964,16 +2021,16 @@ pci_ahci_port_write(struct pci_ahci_softc *sc, uint64_t offset, uint64_t value) switch (offset) { case AHCI_P_CLB: - p->clb = value; + p->clb = (uint32_t) value; break; case AHCI_P_CLBU: - p->clbu = value; + p->clbu = (uint32_t) value; break; case AHCI_P_FB: - p->fb = value; + p->fb = (uint32_t) value; break; case AHCI_P_FBU: - p->fbu = value; + p->fbu = (uint32_t) value; break; case AHCI_P_IS: p->is &= ~value; @@ -2000,8 +2057,7 @@ pci_ahci_port_write(struct pci_ahci_softc *sc, uint64_t offset, uint64_t value) p->cmd |= AHCI_P_CMD_CR; clb = (uint64_t)p->clbu << 32 | p->clb; - p->cmd_lst = paddr_guest2host(ahci_ctx(sc), clb, - AHCI_CL_SIZE * AHCI_MAX_SLOTS); + p->cmd_lst = paddr_guest2host(clb, AHCI_CL_SIZE * AHCI_MAX_SLOTS); } if (value & AHCI_P_CMD_FRE) { @@ -2010,14 +2066,14 @@ pci_ahci_port_write(struct pci_ahci_softc *sc, uint64_t offset, uint64_t value) p->cmd |= AHCI_P_CMD_FR; fb = (uint64_t)p->fbu << 32 | p->fb; /* we don't support FBSCP, so rfis size is 256Bytes */ - p->rfis = paddr_guest2host(ahci_ctx(sc), fb, 256); + p->rfis = paddr_guest2host(fb, 256); } else { - p->cmd &= ~AHCI_P_CMD_FR; + p->cmd &= ~((unsigned) AHCI_P_CMD_FR); } if (value & AHCI_P_CMD_CLO) { - p->tfd &= ~(ATA_S_BUSY | ATA_S_DRQ); - p->cmd &= ~AHCI_P_CMD_CLO; + p->tfd &= ~((unsigned) (ATA_S_BUSY | ATA_S_DRQ)); + p->cmd &= ~((unsigned) AHCI_P_CMD_CLO); } if (value & AHCI_P_CMD_ICC_MASK) { @@ -2033,7 +2089,7 @@ pci_ahci_port_write(struct pci_ahci_softc *sc, uint64_t offset, uint64_t value) WPRINTF("pci_ahci_port: read only registers 0x%"PRIx64"\n", offset); break; case AHCI_P_SCTL: - p->sctl = value; + p->sctl = (uint32_t) value; if (!(p->cmd & AHCI_P_CMD_ST)) { if (value & ATA_SC_DET_RESET) ahci_port_reset(p); @@ -2087,8 +2143,8 @@ pci_ahci_host_write(struct pci_ahci_softc *sc, uint64_t offset, uint64_t value) } static void -pci_ahci_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, - int baridx, uint64_t offset, int size, uint64_t value) +pci_ahci_write(UNUSED int vcpu, struct pci_devinst *pi, int baridx, + uint64_t offset, int size, uint64_t value) { struct pci_ahci_softc *sc = pi->pi_arg; @@ -2099,7 +2155,7 @@ pci_ahci_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, if (offset < AHCI_OFFSET) pci_ahci_host_write(sc, offset, value); - else if (offset < AHCI_OFFSET + sc->ports * AHCI_STEP) + else if (offset < ((uint64_t) (AHCI_OFFSET + (sc->ports * AHCI_STEP)))) pci_ahci_port_write(sc, offset, value); else WPRINTF("pci_ahci: unknown i/o write offset 0x%"PRIx64"\n", offset); @@ -2143,7 +2199,7 @@ static uint64_t pci_ahci_port_read(struct pci_ahci_softc *sc, uint64_t offset) { uint32_t value; - int port = (offset - AHCI_OFFSET) / AHCI_STEP; + int port = (int) ((offset - AHCI_OFFSET) / AHCI_STEP); offset = (offset - AHCI_OFFSET) % AHCI_STEP; switch (offset) { @@ -2181,8 +2237,8 @@ pci_ahci_port_read(struct pci_ahci_softc *sc, uint64_t offset) } static uint64_t -pci_ahci_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, - uint64_t regoff, int size) +pci_ahci_read(UNUSED int vcpu, struct pci_devinst *pi, int baridx, + uint64_t regoff, int size) { struct pci_ahci_softc *sc = pi->pi_arg; uint64_t offset; @@ -2190,15 +2246,16 @@ pci_ahci_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, assert(baridx == 5); assert(size == 1 || size == 2 || size == 4); - assert((regoff & (size - 1)) == 0); + assert((regoff & ((uint64_t) (size - 1))) == 0); pthread_mutex_lock(&sc->mtx); - offset = regoff & ~0x3; /* round down to a multiple of 4 bytes */ + /* round down to a multiple of 4 bytes */ + offset = regoff & ~((uint64_t) 0x3); if (offset < AHCI_OFFSET) - value = pci_ahci_host_read(sc, offset); - else if (offset < AHCI_OFFSET + sc->ports * AHCI_STEP) - value = pci_ahci_port_read(sc, offset); + value = (uint32_t) pci_ahci_host_read(sc, offset); + else if (offset < ((uint64_t) (AHCI_OFFSET + (sc->ports * AHCI_STEP)))) + value = (uint32_t) pci_ahci_port_read(sc, offset); else { value = 0; WPRINTF("pci_ahci: unknown i/o read offset 0x%"PRIx64"\n", @@ -2212,7 +2269,7 @@ pci_ahci_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, } static int -pci_ahci_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts, int atapi) +pci_ahci_init(struct pci_devinst *pi, char *opts, int atapi) { char bident[sizeof("XX:X:X")]; struct blockif_ctxt *bctxt; @@ -2261,7 +2318,7 @@ pci_ahci_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts, int atapi) * md5 sum of the filename */ MD5Init(&mdctx); - MD5Update(&mdctx, opts, strlen(opts)); + MD5Update(&mdctx, opts, ((unsigned int) strlen(opts))); MD5Final(digest, &mdctx); sprintf(sc->port[0].ident, "BHYVE-%02X%02X-%02X%02X-%02X%02X", digest[0], digest[1], digest[2], digest[3], digest[4], digest[5]); @@ -2283,7 +2340,8 @@ pci_ahci_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts, int atapi) AHCI_CAP_SMPS | AHCI_CAP_SSS | AHCI_CAP_SALP | AHCI_CAP_SAL | AHCI_CAP_SCLO | (0x3 << AHCI_CAP_ISS_SHIFT)| AHCI_CAP_PMD | AHCI_CAP_SSC | AHCI_CAP_PSC | - (slots << AHCI_CAP_NCS_SHIFT) | AHCI_CAP_SXS | (sc->ports - 1); + (((unsigned) slots) << AHCI_CAP_NCS_SHIFT) | AHCI_CAP_SXS | + (((unsigned) sc->ports) - 1); /* Only port 0 implemented */ sc->pi = 1; @@ -2298,7 +2356,7 @@ pci_ahci_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts, int atapi) pci_set_cfgdata8(pi, PCIR_PROGIF, PCIP_STORAGE_SATA_AHCI_1_0); pci_emul_add_msicap(pi, 1); pci_emul_alloc_bar(pi, 5, PCIBAR_MEM32, - AHCI_OFFSET + sc->ports * AHCI_STEP); + ((uint64_t) (AHCI_OFFSET + sc->ports * AHCI_STEP))); pci_lintr_request(pi); @@ -2313,23 +2371,21 @@ open_fail: } static int -pci_ahci_hd_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) +pci_ahci_hd_init(struct pci_devinst *pi, char *opts) { - - return (pci_ahci_init(ctx, pi, opts, 0)); + return (pci_ahci_init(pi, opts, 0)); } static int -pci_ahci_atapi_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) +pci_ahci_atapi_init(struct pci_devinst *pi, char *opts) { - - return (pci_ahci_init(ctx, pi, opts, 1)); + return (pci_ahci_init(pi, opts, 1)); } /* * Use separate emulation names to distinguish drive and atapi devices */ -struct pci_devemu pci_de_ahci_hd = { +static struct pci_devemu pci_de_ahci_hd = { .pe_emu = "ahci-hd", .pe_init = pci_ahci_hd_init, .pe_barwrite = pci_ahci_write, @@ -2337,7 +2393,7 @@ struct pci_devemu pci_de_ahci_hd = { }; PCI_EMUL_SET(pci_de_ahci_hd); -struct pci_devemu pci_de_ahci_cd = { +static struct pci_devemu pci_de_ahci_cd = { .pe_emu = "ahci-cd", .pe_init = pci_ahci_atapi_init, .pe_barwrite = pci_ahci_write, diff --git a/bhyve/pci_emul.c b/src/pci_emul.c similarity index 84% rename from bhyve/pci_emul.c rename to src/pci_emul.c index 03ff0c0..10a09f9 100644 --- a/bhyve/pci_emul.c +++ b/src/pci_emul.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,53 +27,47 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include +#include #include +#include +#include +#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include -#include "acpi.h" -#include "bhyverun.h" -#include "inout.h" -#include "ioapic.h" -#include "mem.h" -#include "pci_emul.h" -#include "pci_irq.h" -#include "pci_lpc.h" +#define CONF1_ADDR_PORT 0x0cf8 +#define CONF1_DATA_PORT0 0x0cfc +#define CONF1_DATA_PORT1 0x0cfd +#define CONF1_DATA_PORT2 0x0cfe +#define CONF1_DATA_PORT3 0x0cff -#define CONF1_ADDR_PORT 0x0cf8 -#define CONF1_DATA_PORT 0x0cfc +#define CONF1_ENABLE 0x80000000ul -#define CONF1_ENABLE 0x80000000ul - -#define MAXBUSES (PCI_BUSMAX + 1) -#define MAXSLOTS (PCI_SLOTMAX + 1) -#define MAXFUNCS (PCI_FUNCMAX + 1) +#define MAXBUSES (PCI_BUSMAX + 1) +#define MAXSLOTS (PCI_SLOTMAX + 1) +#define MAXFUNCS (PCI_FUNCMAX + 1) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct funcinfo { - char *fi_name; - char *fi_param; + char *fi_name; + char *fi_param; struct pci_devinst *fi_devi; }; struct intxinfo { - int ii_count; - int ii_pirq_pin; - int ii_ioapic_irq; + int ii_count; + int ii_pirq_pin; + int ii_ioapic_irq; }; struct slotinfo { @@ -81,11 +76,12 @@ struct slotinfo { }; struct businfo { - uint16_t iobase, iolimit; /* I/O window */ - uint32_t membase32, memlimit32; /* mmio window below 4GB */ - uint64_t membase64, memlimit64; /* mmio window above 4GB */ + uint16_t iobase, iolimit; /* I/O window */ + uint32_t membase32, memlimit32; /* mmio window below 4GB */ + uint64_t membase64, memlimit64; /* mmio window above 4GB */ struct slotinfo slotinfo[MAXSLOTS]; }; +#pragma clang diagnostic pop static struct businfo *pci_businfo[MAXBUSES]; @@ -110,17 +106,17 @@ SYSRES_MEM(PCI_EMUL_ECFG_BASE, PCI_EMUL_ECFG_SIZE); static struct pci_devemu *pci_emul_finddev(char *name); static void pci_lintr_route(struct pci_devinst *pi); static void pci_lintr_update(struct pci_devinst *pi); -static void pci_cfgrw(struct vmctx *ctx, int vcpu, int in, int bus, int slot, - int func, int coff, int bytes, uint32_t *val); +static void pci_cfgrw(int vcpu, int in, int bus, int slot, int func, int coff, + int bytes, uint32_t *val); static __inline void CFGWRITE(struct pci_devinst *pi, int coff, uint32_t val, int bytes) { if (bytes == 1) - pci_set_cfgdata8(pi, coff, val); + pci_set_cfgdata8(pi, coff, ((uint8_t) val)); else if (bytes == 2) - pci_set_cfgdata16(pi, coff, val); + pci_set_cfgdata16(pi, coff, ((uint16_t) val)); else pci_set_cfgdata32(pi, coff, val); } @@ -159,7 +155,6 @@ CFGREAD(struct pci_devinst *pi, int coff, int bytes) static void pci_parse_slot_usage(char *aopt) { - fprintf(stderr, "Invalid PCI slot info field \"%s\"\n", aopt); } @@ -242,7 +237,7 @@ pci_valid_pba_offset(struct pci_devinst *pi, uint64_t offset) if (offset < pi->pi_msix.pba_offset) return (0); - if (offset >= pi->pi_msix.pba_offset + pi->pi_msix.pba_size) { + if (offset >= pi->pi_msix.pba_offset + ((unsigned) pi->pi_msix.pba_size)) { return (0); } @@ -264,7 +259,7 @@ pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size, /* * Return if table index is beyond what device supports */ - tab_index = offset / MSIX_TABLE_ENTRY_SIZE; + tab_index = (int) (offset / MSIX_TABLE_ENTRY_SIZE); if (tab_index >= pi->pi_msix.table_count) return (-1); @@ -278,9 +273,9 @@ pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size, dest += msix_entry_offset; if (size == 4) - *((uint32_t *)dest) = value; + *((uint32_t *)((void *) dest)) = (uint32_t) value; else - *((uint64_t *)dest) = value; + *((uint64_t *)((void *) dest)) = value; return (0); } @@ -291,7 +286,7 @@ pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size) char *dest; int msix_entry_offset; int tab_index; - uint64_t retval = ~0; + uint64_t retval = ~((uint64_t) 0); /* * The PCI standard only allows 4 and 8 byte accesses to the MSI-X @@ -308,7 +303,7 @@ pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size) return (retval); } - tab_index = offset / MSIX_TABLE_ENTRY_SIZE; + tab_index = (int) (offset / MSIX_TABLE_ENTRY_SIZE); if (tab_index < pi->pi_msix.table_count) { /* valid MSI-X Table access */ @@ -316,11 +311,11 @@ pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size) dest += msix_entry_offset; if (size == 1) - retval = *((uint8_t *)dest); + retval = *((uint8_t *)((void *) dest)); else if (size == 4) - retval = *((uint32_t *)dest); + retval = *((uint32_t *)((void *) dest)); else - retval = *((uint64_t *)dest); + retval = *((uint64_t *)((void *) dest)); } else if (pci_valid_pba_offset(pi, offset)) { /* return 0 for PBA access */ retval = 0; @@ -350,8 +345,8 @@ pci_msix_pba_bar(struct pci_devinst *pi) } static int -pci_emul_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, - uint32_t *eax, void *arg) +pci_emul_io_handler(int vcpu, int in, int port, int bytes, uint32_t *eax, + void *arg) { struct pci_devinst *pdi = arg; struct pci_devemu *pe = pdi->pi_d; @@ -359,16 +354,17 @@ pci_emul_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, int i; for (i = 0; i <= PCI_BARMAX; i++) { - if (pdi->pi_bar[i].type == PCIBAR_IO && - port >= pdi->pi_bar[i].addr && - port + bytes <= pdi->pi_bar[i].addr + pdi->pi_bar[i].size) { - offset = port - pdi->pi_bar[i].addr; + if ((pdi->pi_bar[i].type == PCIBAR_IO) && + (((uint64_t) port) >= pdi->pi_bar[i].addr) && + (((uint64_t) (port + bytes)) <= + (pdi->pi_bar[i].addr + pdi->pi_bar[i].size))) + { + offset = ((uint64_t) port) - pdi->pi_bar[i].addr; if (in) - *eax = (*pe->pe_barread)(ctx, vcpu, pdi, i, - offset, bytes); + *eax = (uint32_t) (*pe->pe_barread)(vcpu, pdi, i, offset, + bytes); else - (*pe->pe_barwrite)(ctx, vcpu, pdi, i, offset, - bytes, *eax); + (*pe->pe_barwrite)(vcpu, pdi, i, offset, bytes, *eax); return (0); } } @@ -376,7 +372,7 @@ pci_emul_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, } static int -pci_emul_mem_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr, +pci_emul_mem_handler(int vcpu, int dir, uint64_t addr, int size, uint64_t *val, void *arg1, long arg2) { struct pci_devinst *pdi = arg1; @@ -385,32 +381,27 @@ pci_emul_mem_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr, int bidx = (int) arg2; assert(bidx <= PCI_BARMAX); - assert(pdi->pi_bar[bidx].type == PCIBAR_MEM32 || - pdi->pi_bar[bidx].type == PCIBAR_MEM64); - assert(addr >= pdi->pi_bar[bidx].addr && - addr + size <= pdi->pi_bar[bidx].addr + pdi->pi_bar[bidx].size); + assert((pdi->pi_bar[bidx].type == PCIBAR_MEM32) || + (pdi->pi_bar[bidx].type == PCIBAR_MEM64)); + assert((addr >= pdi->pi_bar[bidx].addr) && + ((addr + ((uint64_t) size)) <= + (pdi->pi_bar[bidx].addr + pdi->pi_bar[bidx].size))); offset = addr - pdi->pi_bar[bidx].addr; if (dir == MEM_F_WRITE) { if (size == 8) { - (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset, - 4, *val & 0xffffffff); - (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset + 4, - 4, *val >> 32); + (*pe->pe_barwrite)(vcpu, pdi, bidx, offset, 4, *val & 0xffffffff); + (*pe->pe_barwrite)(vcpu, pdi, bidx, offset + 4, 4, *val >> 32); } else { - (*pe->pe_barwrite)(ctx, vcpu, pdi, bidx, offset, - size, *val); + (*pe->pe_barwrite)(vcpu, pdi, bidx, offset, size, *val); } } else { if (size == 8) { - *val = (*pe->pe_barread)(ctx, vcpu, pdi, bidx, - offset, 4); - *val |= (*pe->pe_barread)(ctx, vcpu, pdi, bidx, - offset + 4, 4) << 32; + *val = (*pe->pe_barread)(vcpu, pdi, bidx, offset, 4); + *val |= (*pe->pe_barread)(vcpu, pdi, bidx, offset + 4, 4) << 32; } else { - *val = (*pe->pe_barread)(ctx, vcpu, pdi, bidx, - offset, size); + *val = (*pe->pe_barread)(vcpu, pdi, bidx, offset, size); } } @@ -459,8 +450,8 @@ modify_bar_registration(struct pci_devinst *pi, int idx, int registration) case PCIBAR_IO: bzero(&iop, sizeof(struct inout_port)); iop.name = pi->pi_name; - iop.port = pi->pi_bar[idx].addr; - iop.size = pi->pi_bar[idx].size; + iop.port = (int) pi->pi_bar[idx].addr; + iop.size = (int)pi->pi_bar[idx].size; if (registration) { iop.flags = IOPORT_F_INOUT; iop.handler = pci_emul_io_handler; @@ -484,7 +475,8 @@ modify_bar_registration(struct pci_devinst *pi, int idx, int registration) } else error = unregister_mem(&mr); break; - default: + case PCIBAR_NONE: + case PCIBAR_MEMHI64: error = EINVAL; break; } @@ -576,8 +568,11 @@ pci_emul_alloc_pbar(struct pci_devinst *pdi, int idx, uint64_t hostbase, assert(idx >= 0 && idx <= PCI_BARMAX); + addr = 0; + limit = 0; + if ((size & (size - 1)) != 0) - size = 1UL << flsl(size); /* round up to a power of 2 */ + size = 1UL << flsl((long) size); /* round up to a power of 2 */ /* Enforce minimum BAR sizes required by the PCI standard */ if (type == PCIBAR_IO) { @@ -633,7 +628,7 @@ pci_emul_alloc_pbar(struct pci_devinst *pdi, int idx, uint64_t hostbase, mask = PCIM_BAR_MEM_BASE; lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32; break; - default: + case PCIBAR_MEMHI64: printf("pci_emul_alloc_base: invalid bar type %d\n", type); assert(0); } @@ -650,7 +645,7 @@ pci_emul_alloc_pbar(struct pci_devinst *pdi, int idx, uint64_t hostbase, /* Initialize the BAR register in config space */ bar = (addr & mask) | lobits; - pci_set_cfgdata32(pdi, PCIR_BAR(idx), bar); + pci_set_cfgdata32(pdi, PCIR_BAR(idx), ((uint32_t) bar)); if (type == PCIBAR_MEM64) { assert(idx + 1 <= PCI_BARMAX); @@ -686,10 +681,10 @@ pci_emul_add_capability(struct pci_devinst *pi, u_char *capdata, int caplen) /* Set the previous capability pointer */ if ((sts & PCIM_STATUS_CAPPRESENT) == 0) { - pci_set_cfgdata8(pi, PCIR_CAP_PTR, capoff); + pci_set_cfgdata8(pi, PCIR_CAP_PTR, ((uint8_t) capoff)); pci_set_cfgdata16(pi, PCIR_STATUS, sts|PCIM_STATUS_CAPPRESENT); } else - pci_set_cfgdata8(pi, pi->pi_prevcap + 1, capoff); + pci_set_cfgdata8(pi, pi->pi_prevcap + 1, ((uint8_t) capoff)); /* Copy the capability */ for (i = 0; i < caplen; i++) @@ -719,7 +714,7 @@ pci_emul_finddev(char *name) } static int -pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int bus, int slot, +pci_emul_init(struct pci_devemu *pde, int bus, int slot, int func, struct funcinfo *fi) { struct pci_devinst *pdi; @@ -727,10 +722,9 @@ pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int bus, int slot, pdi = calloc(1, sizeof(struct pci_devinst)); - pdi->pi_vmctx = ctx; - pdi->pi_bus = bus; - pdi->pi_slot = slot; - pdi->pi_func = func; + pdi->pi_bus = (uint8_t) bus; + pdi->pi_slot = (uint8_t) slot; + pdi->pi_func = (uint8_t) func; pthread_mutex_init(&pdi->pi_lintr.lock, NULL); pdi->pi_lintr.pin = 0; pdi->pi_lintr.state = IDLE; @@ -746,7 +740,7 @@ pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int bus, int slot, pci_set_cfgdata8(pdi, PCIR_COMMAND, PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN); - err = (*pde->pe_init)(ctx, pdi, fi->fi_param); + err = (*pde->pe_init)(pdi, fi->fi_param); if (err == 0) fi->fi_devi = pdi; else @@ -768,8 +762,8 @@ pci_populate_msicap(struct msicap *msicap, int msgnum, int nextptr) bzero(msicap, sizeof(struct msicap)); msicap->capid = PCIY_MSI; - msicap->nextptr = nextptr; - msicap->msgctrl = PCIM_MSICTRL_64BIT | (mmc << 1); + msicap->nextptr = (uint8_t) nextptr; + msicap->msgctrl = (uint16_t) (PCIM_MSICTRL_64BIT | (mmc << 1)); } int @@ -798,7 +792,7 @@ pci_populate_msixcap(struct msixcap *msixcap, int msgnum, int barnum, * zero except for the Table Size. * Note: Table size N is encoded as N-1 */ - msixcap->msgctrl = msgnum - 1; + msixcap->msgctrl = (uint16_t) (msgnum - 1); /* * MSI-X BAR setup: @@ -818,7 +812,7 @@ pci_msix_table_init(struct pci_devinst *pi, int table_entries) assert(table_entries <= MAX_MSIX_TABLE_ENTRIES); table_size = table_entries * MSIX_TABLE_ENTRY_SIZE; - pi->pi_msix.table = calloc(1, table_size); + pi->pi_msix.table = calloc(1, ((size_t) table_size)); /* set mask bit of vector control register */ for (i = 0; i < table_entries; i++) @@ -834,10 +828,10 @@ pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum) assert(msgnum >= 1 && msgnum <= MAX_MSIX_TABLE_ENTRIES); assert(barnum >= 0 && barnum <= PCIR_MAX_BAR_0); - tab_size = msgnum * MSIX_TABLE_ENTRY_SIZE; + tab_size = (uint32_t) (msgnum * MSIX_TABLE_ENTRY_SIZE); /* Align table size to nearest 4K */ - tab_size = roundup2(tab_size, 4096); + tab_size = roundup2(tab_size, 4096u); pi->pi_msix.table_bar = barnum; pi->pi_msix.pba_bar = barnum; @@ -852,7 +846,7 @@ pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum) /* allocate memory for MSI-X Table and PBA */ pci_emul_alloc_bar(pi, barnum, PCIBAR_MEM32, - tab_size + pi->pi_msix.pba_size); + (tab_size + ((uint32_t) pi->pi_msix.pba_size))); return (pci_emul_add_capability(pi, (u_char *)&msixcap, sizeof(msixcap))); @@ -922,8 +916,8 @@ msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset, CFGWRITE(pi, offset, val, bytes); } -void -pciecap_cfgwrite(struct pci_devinst *pi, int capoff, int offset, +static void +pciecap_cfgwrite(struct pci_devinst *pi, UNUSED int capoff, int offset, int bytes, uint32_t val) { @@ -1026,8 +1020,8 @@ pci_emul_iscap(struct pci_devinst *pi, int offset) } static int -pci_emul_fallback_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr, - int size, uint64_t *val, void *arg1, long arg2) +pci_emul_fallback_handler(UNUSED int vcpu, int dir, UNUSED uint64_t addr, + UNUSED int size, uint64_t *val, UNUSED void *arg1, UNUSED long arg2) { /* * Ignore writes; return 0xff's for reads. The mem read code @@ -1041,8 +1035,8 @@ pci_emul_fallback_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr, } static int -pci_emul_ecfg_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr, - int bytes, uint64_t *val, void *arg1, long arg2) +pci_emul_ecfg_handler(int vcpu, int dir, uint64_t addr, int bytes, + uint64_t *val, UNUSED void *arg1, UNUSED long arg2) { int bus, slot, func, coff, in; @@ -1053,7 +1047,7 @@ pci_emul_ecfg_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr, in = (dir == MEM_F_READ); if (in) *val = ~0UL; - pci_cfgrw(ctx, vcpu, in, bus, slot, func, coff, bytes, (uint32_t *)val); + pci_cfgrw(vcpu, in, bus, slot, func, coff, bytes, (uint32_t *)val); return (0); } @@ -1068,7 +1062,7 @@ pci_ecfg_base(void) #define BUSMEM_ROUNDUP (1024 * 1024) int -init_pci(struct vmctx *ctx) +init_pci(void) { struct mem_range mr; struct pci_devemu *pde; @@ -1080,7 +1074,7 @@ init_pci(struct vmctx *ctx) int error; pci_emul_iobase = PCI_EMUL_IOBASE; - pci_emul_membase32 = vm_get_lowmem_limit(ctx); + pci_emul_membase32 = xh_vm_get_lowmem_limit(); pci_emul_membase64 = PCI_EMUL_MEMBASE64; for (bus = 0; bus < MAXBUSES; bus++) { @@ -1090,9 +1084,9 @@ init_pci(struct vmctx *ctx) * Keep track of the i/o and memory resources allocated to * this bus. */ - bi->iobase = pci_emul_iobase; - bi->membase32 = pci_emul_membase32; - bi->membase64 = pci_emul_membase64; + bi->iobase = (uint16_t) pci_emul_iobase; + bi->membase32 = (uint32_t) pci_emul_membase32; + bi->membase64 = (uint32_t) pci_emul_membase64; for (slot = 0; slot < MAXSLOTS; slot++) { si = &bi->slotinfo[slot]; @@ -1102,8 +1096,7 @@ init_pci(struct vmctx *ctx) continue; pde = pci_emul_finddev(fi->fi_name); assert(pde != NULL); - error = pci_emul_init(ctx, pde, bus, slot, - func, fi); + error = pci_emul_init(pde, bus, slot, func, fi); if (error) return (error); } @@ -1115,17 +1108,17 @@ init_pci(struct vmctx *ctx) * reprogram the BARs. */ pci_emul_iobase += BUSIO_ROUNDUP; - pci_emul_iobase = roundup2(pci_emul_iobase, BUSIO_ROUNDUP); - bi->iolimit = pci_emul_iobase; + pci_emul_iobase = roundup2(pci_emul_iobase, ((uint32_t) BUSIO_ROUNDUP)); + bi->iolimit = (uint16_t) pci_emul_iobase; pci_emul_membase32 += BUSMEM_ROUNDUP; pci_emul_membase32 = roundup2(pci_emul_membase32, - BUSMEM_ROUNDUP); - bi->memlimit32 = pci_emul_membase32; + ((uint64_t) BUSMEM_ROUNDUP)); + bi->memlimit32 = (uint32_t) pci_emul_membase32; pci_emul_membase64 += BUSMEM_ROUNDUP; pci_emul_membase64 = roundup2(pci_emul_membase64, - BUSMEM_ROUNDUP); + ((uint64_t) BUSMEM_ROUNDUP)); bi->memlimit64 = pci_emul_membase64; } @@ -1164,7 +1157,7 @@ init_pci(struct vmctx *ctx) * Accesses to memory addresses that are not allocated to system * memory or PCI devices return 0xff's. */ - lowmem = vm_get_lowmem_size(ctx); + lowmem = xh_vm_get_lowmem_size(); bzero(&mr, sizeof(struct mem_range)); mr.name = "PCI hole"; mr.flags = MEM_F_RW | MEM_F_IMMUTABLE; @@ -1188,8 +1181,8 @@ init_pci(struct vmctx *ctx) } static void -pci_apic_prt_entry(int bus, int slot, int pin, int pirq_pin, int ioapic_irq, - void *arg) +pci_apic_prt_entry(UNUSED int bus, int slot, int pin, UNUSED int pirq_pin, + int ioapic_irq, UNUSED void *arg) { dsdt_line(" Package ()"); @@ -1202,8 +1195,8 @@ pci_apic_prt_entry(int bus, int slot, int pin, int pirq_pin, int ioapic_irq, } static void -pci_pirq_prt_entry(int bus, int slot, int pin, int pirq_pin, int ioapic_irq, - void *arg) +pci_pirq_prt_entry(UNUSED int bus, int slot, int pin, int pirq_pin, + UNUSED int ioapic_irq, UNUSED void *arg) { char *name; @@ -1406,7 +1399,7 @@ pci_msi_enabled(struct pci_devinst *pi) return (pi->pi_msi.enabled); } -int +static int pci_msi_maxmsgnum(struct pci_devinst *pi) { if (pi->pi_msi.enabled) @@ -1439,7 +1432,7 @@ pci_generate_msix(struct pci_devinst *pi, int index) mte = &pi->pi_msix.table[index]; if ((mte->vector_control & PCIM_MSIX_VCTRL_MASK) == 0) { /* XXX Set PBA bit if interrupt is disabled */ - vm_lapic_msi(pi->pi_vmctx, mte->addr, mte->msg_data); + xh_vm_lapic_msi(mte->addr, mte->msg_data); } } @@ -1448,8 +1441,8 @@ pci_generate_msi(struct pci_devinst *pi, int index) { if (pci_msi_enabled(pi) && index < pci_msi_maxmsgnum(pi)) { - vm_lapic_msi(pi->pi_vmctx, pi->pi_msi.addr, - pi->pi_msi.msg_data + index); + xh_vm_lapic_msi(pi->pi_msi.addr, pi->pi_msi.msg_data + + ((uint64_t) index)); } } @@ -1488,8 +1481,8 @@ pci_lintr_request(struct pci_devinst *pi) } si->si_intpins[bestpin].ii_count++; - pi->pi_lintr.pin = bestpin + 1; - pci_set_cfgdata8(pi, PCIR_INTPIN, bestpin + 1); + pi->pi_lintr.pin = (int8_t) (bestpin + 1); + pci_set_cfgdata8(pi, PCIR_INTPIN, ((uint8_t) (bestpin + 1))); } static void @@ -1518,12 +1511,12 @@ pci_lintr_route(struct pci_devinst *pi) * not yet assigned. */ if (ii->ii_pirq_pin == 0) - ii->ii_pirq_pin = pirq_alloc_pin(pi->pi_vmctx); + ii->ii_pirq_pin = pirq_alloc_pin(); assert(ii->ii_pirq_pin > 0); pi->pi_lintr.ioapic_irq = ii->ii_ioapic_irq; pi->pi_lintr.pirq_pin = ii->ii_pirq_pin; - pci_set_cfgdata8(pi, PCIR_INTLINE, pirq_irq(ii->ii_pirq_pin)); + pci_set_cfgdata8(pi, PCIR_INTLINE, ((uint8_t) pirq_irq(ii->ii_pirq_pin))); } void @@ -1651,13 +1644,13 @@ pci_emul_hdrtype_fixup(int bus, int slot, int off, int bytes, uint32_t *rv) switch (bytes) { case 1: case 2: - *rv &= ~PCIM_MFDEV; + *rv &= ~((uint32_t) PCIM_MFDEV); if (mfdev) { *rv |= PCIM_MFDEV; } break; case 4: - *rv &= ~(PCIM_MFDEV << 16); + *rv &= ~((uint32_t) (PCIM_MFDEV << 16)); if (mfdev) { *rv |= (PCIM_MFDEV << 16); } @@ -1719,9 +1712,7 @@ pci_emul_cmdsts_write(struct pci_devinst *pi, int coff, uint32_t new, int bytes) else unregister_bar(pi, i); } - break; - default: - assert(0); + break; } } @@ -1733,8 +1724,8 @@ pci_emul_cmdsts_write(struct pci_devinst *pi, int coff, uint32_t new, int bytes) } static void -pci_cfgrw(struct vmctx *ctx, int vcpu, int in, int bus, int slot, int func, - int coff, int bytes, uint32_t *eax) +pci_cfgrw(int vcpu, int in, int bus, int slot, int func, int coff, int bytes, + uint32_t *eax) { struct businfo *bi; struct slotinfo *si; @@ -1787,7 +1778,7 @@ pci_cfgrw(struct vmctx *ctx, int vcpu, int in, int bus, int slot, int func, if (in) { /* Let the device emulation override the default handler */ if (pe->pe_cfgread != NULL) { - needcfg = pe->pe_cfgread(ctx, vcpu, pi, coff, bytes, + needcfg = pe->pe_cfgread(vcpu, pi, coff, bytes, eax); } else { needcfg = 1; @@ -1800,7 +1791,7 @@ pci_cfgrw(struct vmctx *ctx, int vcpu, int in, int bus, int slot, int func, } else { /* Let the device emulation override the default handler */ if (pe->pe_cfgwrite != NULL && - (*pe->pe_cfgwrite)(ctx, vcpu, pi, coff, bytes, *eax) == 0) + (*pe->pe_cfgwrite)(vcpu, pi, coff, bytes, *eax) == 0) return; /* @@ -1857,10 +1848,8 @@ pci_cfgrw(struct vmctx *ctx, int vcpu, int in, int bus, int slot, int func, PCIBAR_MEMHI64); } break; - default: - assert(0); } - pci_set_cfgdata32(pi, coff, bar); + pci_set_cfgdata32(pi, coff, ((uint32_t) bar)); } else if (pci_emul_iscap(pi, coff)) { pci_emul_capwrite(pi, coff, bytes, *eax); @@ -1875,8 +1864,8 @@ pci_cfgrw(struct vmctx *ctx, int vcpu, int in, int bus, int slot, int func, static int cfgenable, cfgbus, cfgslot, cfgfunc, cfgoff; static int -pci_emul_cfgaddr(struct vmctx *ctx, int vcpu, int in, int port, int bytes, - uint32_t *eax, void *arg) +pci_emul_cfgaddr(UNUSED int vcpu, int in, UNUSED int port, int bytes, + uint32_t *eax, UNUSED void *arg) { uint32_t x; @@ -1887,7 +1876,9 @@ pci_emul_cfgaddr(struct vmctx *ctx, int vcpu, int in, int port, int bytes, } if (in) { - x = (cfgbus << 16) | (cfgslot << 11) | (cfgfunc << 8) | cfgoff; + x = (uint32_t) ((cfgbus << 16) | (cfgslot << 11) | (cfgfunc << 8) | + cfgoff); + if (cfgenable) x |= CONF1_ENABLE; *eax = x; @@ -1905,16 +1896,16 @@ pci_emul_cfgaddr(struct vmctx *ctx, int vcpu, int in, int port, int bytes, INOUT_PORT(pci_cfgaddr, CONF1_ADDR_PORT, IOPORT_F_INOUT, pci_emul_cfgaddr); static int -pci_emul_cfgdata(struct vmctx *ctx, int vcpu, int in, int port, int bytes, - uint32_t *eax, void *arg) +pci_emul_cfgdata(int vcpu, int in, int port, int bytes, uint32_t *eax, + UNUSED void *arg) { int coff; assert(bytes == 1 || bytes == 2 || bytes == 4); - coff = cfgoff + (port - CONF1_DATA_PORT); + coff = cfgoff + (port - CONF1_DATA_PORT0); if (cfgenable) { - pci_cfgrw(ctx, vcpu, in, cfgbus, cfgslot, cfgfunc, coff, bytes, + pci_cfgrw(vcpu, in, cfgbus, cfgslot, cfgfunc, coff, bytes, eax); } else { /* Ignore accesses to cfgdata if not enabled by cfgaddr */ @@ -1924,28 +1915,27 @@ pci_emul_cfgdata(struct vmctx *ctx, int vcpu, int in, int port, int bytes, return (0); } -INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+0, IOPORT_F_INOUT, pci_emul_cfgdata); -INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+1, IOPORT_F_INOUT, pci_emul_cfgdata); -INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+2, IOPORT_F_INOUT, pci_emul_cfgdata); -INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+3, IOPORT_F_INOUT, pci_emul_cfgdata); +INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT0, IOPORT_F_INOUT, pci_emul_cfgdata); +INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT1, IOPORT_F_INOUT, pci_emul_cfgdata); +INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT2, IOPORT_F_INOUT, pci_emul_cfgdata); +INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT3, IOPORT_F_INOUT, pci_emul_cfgdata); #define PCI_EMUL_TEST #ifdef PCI_EMUL_TEST /* * Define a dummy test device */ -#define DIOSZ 8 -#define DMEMSZ 4096 +#define DIOSZ 8 +#define DMEMSZ 4096 struct pci_emul_dsoftc { uint8_t ioregs[DIOSZ]; uint8_t memregs[2][DMEMSZ]; }; -#define PCI_EMUL_MSI_MSGS 4 -#define PCI_EMUL_MSIX_MSGS 16 +#define PCI_EMUL_MSI_MSGS 4 static int -pci_emul_dinit(struct vmctx *ctx, struct pci_devinst *pi, char *opts) +pci_emul_dinit(struct pci_devinst *pi, UNUSED char *opts) { int error; struct pci_emul_dsoftc *sc; @@ -1974,15 +1964,15 @@ pci_emul_dinit(struct vmctx *ctx, struct pci_devinst *pi, char *opts) } static void -pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, - uint64_t offset, int size, uint64_t value) +pci_emul_diow(UNUSED int vcpu, struct pci_devinst *pi, int baridx, + uint64_t offset, int size, uint64_t value) { int i; struct pci_emul_dsoftc *sc = pi->pi_arg; if (baridx == 0) { - if (offset + size > DIOSZ) { - printf("diow: iow too large, offset %ld size %d\n", + if (offset + ((uint64_t) size) > DIOSZ) { + printf("diow: iow too large, offset %llu size %d\n", offset, size); return; } @@ -1990,9 +1980,9 @@ pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, if (size == 1) { sc->ioregs[offset] = value & 0xff; } else if (size == 2) { - *(uint16_t *)&sc->ioregs[offset] = value & 0xffff; + *(uint16_t *)((void *) &sc->ioregs[offset]) = value & 0xffff; } else if (size == 4) { - *(uint32_t *)&sc->ioregs[offset] = value; + *(uint32_t *)((void *) &sc->ioregs[offset]) = (uint32_t) value; } else { printf("diow: iow unknown size %d\n", size); } @@ -2001,7 +1991,8 @@ pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, * Special magic value to generate an interrupt */ if (offset == 4 && size == 4 && pci_msi_enabled(pi)) - pci_generate_msi(pi, value % pci_msi_maxmsgnum(pi)); + pci_generate_msi(pi, ((int) (value % + ((uint64_t) pci_msi_maxmsgnum(pi))))); if (value == 0xabcdef) { for (i = 0; i < pci_msi_maxmsgnum(pi); i++) @@ -2010,8 +2001,8 @@ pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, } if (baridx == 1 || baridx == 2) { - if (offset + size > DMEMSZ) { - printf("diow: memw too large, offset %ld size %d\n", + if (offset + ((uint16_t) size) > DMEMSZ) { + printf("diow: memw too large, offset %llu size %d\n", offset, size); return; } @@ -2019,13 +2010,14 @@ pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, i = baridx - 1; /* 'memregs' index */ if (size == 1) { - sc->memregs[i][offset] = value; + sc->memregs[i][offset] = (uint8_t) value; + } else if (size == 2) { - *(uint16_t *)&sc->memregs[i][offset] = value; + *(uint16_t *)((void *) &sc->memregs[i][offset]) = (uint16_t) value; } else if (size == 4) { - *(uint32_t *)&sc->memregs[i][offset] = value; + *(uint32_t *)((void *) &sc->memregs[i][offset]) = (uint32_t) value; } else if (size == 8) { - *(uint64_t *)&sc->memregs[i][offset] = value; + *(uint64_t *)((void *) &sc->memregs[i][offset]) = value; } else { printf("diow: memw unknown size %d\n", size); } @@ -2041,16 +2033,18 @@ pci_emul_diow(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, } static uint64_t -pci_emul_dior(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, - uint64_t offset, int size) +pci_emul_dior(UNUSED int vcpu, struct pci_devinst *pi, int baridx, + uint64_t offset, int size) { struct pci_emul_dsoftc *sc = pi->pi_arg; uint32_t value; int i; + value = 0; + if (baridx == 0) { - if (offset + size > DIOSZ) { - printf("dior: ior too large, offset %ld size %d\n", + if (offset + ((uint64_t) size) > DIOSZ) { + printf("dior: ior too large, offset %llu size %d\n", offset, size); return (0); } @@ -2058,17 +2052,17 @@ pci_emul_dior(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, if (size == 1) { value = sc->ioregs[offset]; } else if (size == 2) { - value = *(uint16_t *) &sc->ioregs[offset]; + value = *(uint16_t *)((void *) &sc->ioregs[offset]); } else if (size == 4) { - value = *(uint32_t *) &sc->ioregs[offset]; + value = *(uint32_t *)((void *) &sc->ioregs[offset]); } else { printf("dior: ior unknown size %d\n", size); } } if (baridx == 1 || baridx == 2) { - if (offset + size > DMEMSZ) { - printf("dior: memr too large, offset %ld size %d\n", + if (offset + ((uint64_t) size) > DMEMSZ) { + printf("dior: memr too large, offset %llu size %d\n", offset, size); return (0); } @@ -2078,11 +2072,11 @@ pci_emul_dior(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, if (size == 1) { value = sc->memregs[i][offset]; } else if (size == 2) { - value = *(uint16_t *) &sc->memregs[i][offset]; + value = *(uint16_t *) ((void *) &sc->memregs[i][offset]); } else if (size == 4) { - value = *(uint32_t *) &sc->memregs[i][offset]; + value = *(uint32_t *) ((void *) &sc->memregs[i][offset]); } else if (size == 8) { - value = *(uint64_t *) &sc->memregs[i][offset]; + value = (uint32_t) *(uint64_t *) ((void *) &sc->memregs[i][offset]); } else { printf("dior: ior unknown size %d\n", size); } @@ -2097,7 +2091,7 @@ pci_emul_dior(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, return (value); } -struct pci_devemu pci_dummy = { +static struct pci_devemu pci_dummy = { .pe_emu = "dummy", .pe_init = pci_emul_dinit, .pe_barwrite = pci_emul_diow, diff --git a/bhyve/pci_hostbridge.c b/src/pci_hostbridge.c similarity index 85% rename from bhyve/pci_hostbridge.c rename to src/pci_hostbridge.c index 54a25ae..85f27f3 100644 --- a/bhyve/pci_hostbridge.c +++ b/src/pci_hostbridge.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,15 +27,12 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include "pci_emul.h" +#include +#include static int -pci_hostbridge_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) +pci_hostbridge_init(struct pci_devinst *pi, UNUSED char *opts) { - /* config space */ pci_set_cfgdata16(pi, PCIR_VENDOR, 0x1275); /* NetApp */ pci_set_cfgdata16(pi, PCIR_DEVICE, 0x1275); /* NetApp */ @@ -48,22 +46,22 @@ pci_hostbridge_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) } static int -pci_amd_hostbridge_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) +pci_amd_hostbridge_init(struct pci_devinst *pi, char *opts) { - (void) pci_hostbridge_init(ctx, pi, opts); + (void) pci_hostbridge_init(pi, opts); pci_set_cfgdata16(pi, PCIR_VENDOR, 0x1022); /* AMD */ pci_set_cfgdata16(pi, PCIR_DEVICE, 0x7432); /* made up */ return (0); } -struct pci_devemu pci_de_amd_hostbridge = { +static struct pci_devemu pci_de_amd_hostbridge = { .pe_emu = "amd_hostbridge", .pe_init = pci_amd_hostbridge_init, }; PCI_EMUL_SET(pci_de_amd_hostbridge); -struct pci_devemu pci_de_hostbridge = { +static struct pci_devemu pci_de_hostbridge = { .pe_emu = "hostbridge", .pe_init = pci_hostbridge_init, }; diff --git a/bhyve/pci_irq.c b/src/pci_irq.c similarity index 82% rename from bhyve/pci_irq.c rename to src/pci_irq.c index f22b15c..4ce0cd5 100644 --- a/bhyve/pci_irq.c +++ b/src/pci_irq.c @@ -1,6 +1,7 @@ /*- * Copyright (c) 2014 Hudson River Trading LLC * Written by: John H. Baldwin + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -25,25 +26,19 @@ * SUCH DAMAGE. */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include - -#include -#include +#include #include -#include #include -#include - -#include "acpi.h" -#include "inout.h" -#include "pci_emul.h" -#include "pci_irq.h" -#include "pci_lpc.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include /* * Implement an 8 pin PCI interrupt router compatible with the router @@ -61,12 +56,15 @@ __FBSDID("$FreeBSD$"); /* IRQ count to disable an IRQ. */ #define IRQ_DISABLED 0xff +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" static struct pirq { - uint8_t reg; - int use_count; - int active_count; + uint8_t reg; + int use_count; + int active_count; pthread_mutex_t lock; } pirqs[8]; +#pragma clang diagnostic pop static u_char irq_counts[16]; static int pirq_cold = 1; @@ -79,7 +77,6 @@ static int pirq_cold = 1; static bool pirq_valid_irq(int reg) { - if (reg & PIRQ_DIS) return (false); return (IRQ_PERMITTED(reg & PIRQ_IRQ)); @@ -88,25 +85,24 @@ pirq_valid_irq(int reg) uint8_t pirq_read(int pin) { - - assert(pin > 0 && pin <= nitems(pirqs)); + assert((pin > 0) && (((unsigned) pin) <= nitems(pirqs))); return (pirqs[pin - 1].reg); } void -pirq_write(struct vmctx *ctx, int pin, uint8_t val) +pirq_write(int pin, uint8_t val) { struct pirq *pirq; - assert(pin > 0 && pin <= nitems(pirqs)); + assert((pin > 0) && (((unsigned) pin) <= nitems(pirqs))); pirq = &pirqs[pin - 1]; pthread_mutex_lock(&pirq->lock); if (pirq->reg != (val & (PIRQ_DIS | PIRQ_IRQ))) { if (pirq->active_count != 0 && pirq_valid_irq(pirq->reg)) - vm_isa_deassert_irq(ctx, pirq->reg & PIRQ_IRQ, -1); + xh_vm_isa_deassert_irq(pirq->reg & PIRQ_IRQ, -1); pirq->reg = val & (PIRQ_DIS | PIRQ_IRQ); if (pirq->active_count != 0 && pirq_valid_irq(pirq->reg)) - vm_isa_assert_irq(ctx, pirq->reg & PIRQ_IRQ, -1); + xh_vm_isa_assert_irq(pirq->reg & PIRQ_IRQ, -1); } pthread_mutex_unlock(&pirq->lock); } @@ -114,8 +110,7 @@ pirq_write(struct vmctx *ctx, int pin, uint8_t val) void pci_irq_reserve(int irq) { - - assert(irq >= 0 && irq < nitems(irq_counts)); + assert((irq >= 0) && (((unsigned) irq) < nitems(irq_counts))); assert(pirq_cold); assert(irq_counts[irq] == 0 || irq_counts[irq] == IRQ_DISABLED); irq_counts[irq] = IRQ_DISABLED; @@ -124,17 +119,16 @@ pci_irq_reserve(int irq) void pci_irq_use(int irq) { - - assert(irq >= 0 && irq < nitems(irq_counts)); + assert((irq >= 0) && (((unsigned) irq) < nitems(irq_counts))); assert(pirq_cold); assert(irq_counts[irq] != IRQ_DISABLED); irq_counts[irq]++; } void -pci_irq_init(struct vmctx *ctx) +pci_irq_init(void) { - int i; + unsigned i; for (i = 0; i < nitems(pirqs); i++) { pirqs[i].reg = PIRQ_DIS; @@ -156,19 +150,18 @@ pci_irq_assert(struct pci_devinst *pi) struct pirq *pirq; if (pi->pi_lintr.pirq_pin > 0) { - assert(pi->pi_lintr.pirq_pin <= nitems(pirqs)); + assert(((unsigned) pi->pi_lintr.pirq_pin) <= nitems(pirqs)); pirq = &pirqs[pi->pi_lintr.pirq_pin - 1]; pthread_mutex_lock(&pirq->lock); pirq->active_count++; if (pirq->active_count == 1 && pirq_valid_irq(pirq->reg)) { - vm_isa_assert_irq(pi->pi_vmctx, pirq->reg & PIRQ_IRQ, - pi->pi_lintr.ioapic_irq); + xh_vm_isa_assert_irq(pirq->reg & PIRQ_IRQ, pi->pi_lintr.ioapic_irq); pthread_mutex_unlock(&pirq->lock); return; } pthread_mutex_unlock(&pirq->lock); } - vm_ioapic_assert_irq(pi->pi_vmctx, pi->pi_lintr.ioapic_irq); + xh_vm_ioapic_assert_irq(pi->pi_lintr.ioapic_irq); } void @@ -177,23 +170,23 @@ pci_irq_deassert(struct pci_devinst *pi) struct pirq *pirq; if (pi->pi_lintr.pirq_pin > 0) { - assert(pi->pi_lintr.pirq_pin <= nitems(pirqs)); + assert(((unsigned) pi->pi_lintr.pirq_pin) <= nitems(pirqs)); pirq = &pirqs[pi->pi_lintr.pirq_pin - 1]; pthread_mutex_lock(&pirq->lock); pirq->active_count--; if (pirq->active_count == 0 && pirq_valid_irq(pirq->reg)) { - vm_isa_deassert_irq(pi->pi_vmctx, pirq->reg & PIRQ_IRQ, - pi->pi_lintr.ioapic_irq); + xh_vm_isa_deassert_irq(pirq->reg & PIRQ_IRQ, + pi->pi_lintr.ioapic_irq); pthread_mutex_unlock(&pirq->lock); return; } pthread_mutex_unlock(&pirq->lock); } - vm_ioapic_deassert_irq(pi->pi_vmctx, pi->pi_lintr.ioapic_irq); + xh_vm_ioapic_deassert_irq(pi->pi_lintr.ioapic_irq); } int -pirq_alloc_pin(struct vmctx *ctx) +pirq_alloc_pin(void) { int best_count, best_irq, best_pin, irq, pin; @@ -202,7 +195,7 @@ pirq_alloc_pin(struct vmctx *ctx) /* First, find the least-used PIRQ pin. */ best_pin = 0; best_count = pirqs[0].use_count; - for (pin = 1; pin < nitems(pirqs); pin++) { + for (pin = 1; ((unsigned) pin) < nitems(pirqs); pin++) { if (pirqs[pin].use_count < best_count) { best_pin = pin; best_count = pirqs[pin].use_count; @@ -214,7 +207,7 @@ pirq_alloc_pin(struct vmctx *ctx) if (pirqs[best_pin].reg == PIRQ_DIS) { best_irq = -1; best_count = 0; - for (irq = 0; irq < nitems(irq_counts); irq++) { + for (irq = 0; ((unsigned) irq) < nitems(irq_counts); irq++) { if (irq_counts[irq] == IRQ_DISABLED) continue; if (best_irq == -1 || irq_counts[irq] < best_count) { @@ -224,8 +217,8 @@ pirq_alloc_pin(struct vmctx *ctx) } assert(best_irq >= 0); irq_counts[best_irq]++; - pirqs[best_pin].reg = best_irq; - vm_isa_set_irq_trigger(ctx, best_irq, LEVEL_TRIGGER); + pirqs[best_pin].reg = (uint8_t) best_irq; + xh_vm_isa_set_irq_trigger(best_irq, LEVEL_TRIGGER); } return (best_pin + 1); @@ -234,7 +227,7 @@ pirq_alloc_pin(struct vmctx *ctx) int pirq_irq(int pin) { - assert(pin > 0 && pin <= nitems(pirqs)); + assert((pin > 0) && (((unsigned) pin) <= nitems(pirqs))); return (pirqs[pin - 1].reg & PIRQ_IRQ); } @@ -247,7 +240,7 @@ pirq_dsdt(void) int irq, pin; irq_prs = NULL; - for (irq = 0; irq < nitems(irq_counts); irq++) { + for (irq = 0; ((unsigned) irq) < nitems(irq_counts); irq++) { if (!IRQ_PERMITTED(irq)) continue; if (irq_prs == NULL) @@ -286,7 +279,7 @@ pirq_dsdt(void) dsdt_line(" Return (0x01)"); dsdt_line("}"); - for (pin = 0; pin < nitems(pirqs); pin++) { + for (pin = 0; ((unsigned) pin) < nitems(pirqs); pin++) { dsdt_line(""); dsdt_line("Device (LNK%c)", 'A' + pin); dsdt_line("{"); diff --git a/bhyve/pci_lpc.c b/src/pci_lpc.c similarity index 84% rename from bhyve/pci_lpc.c rename to src/pci_lpc.c index e98b141..9d9da1c 100644 --- a/bhyve/pci_lpc.c +++ b/src/pci_lpc.c @@ -1,6 +1,7 @@ /*- * Copyright (c) 2013 Neel Natu * Copyright (c) 2013 Tycho Nightingale + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,24 +28,17 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include - #include #include #include - -#include - -#include "acpi.h" -#include "inout.h" -#include "pci_emul.h" -#include "pci_irq.h" -#include "pci_lpc.h" -#include "uart_emul.h" +#include +#include +#include +#include +#include +#include +#include +#include #define IO_ICU1 0x20 #define IO_ICU2 0xA0 @@ -63,6 +57,9 @@ SYSRES_IO(NMISC_PORT, 1); static struct pci_devinst *lpc_bridge; #define LPC_UART_NUM 2 + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" static struct lpc_uart_softc { struct uart_softc *uart_softc; const char *opts; @@ -70,6 +67,7 @@ static struct lpc_uart_softc { int irq; int enabled; } lpc_uart_softc[LPC_UART_NUM]; +#pragma clang diagnostic pop static const char *lpc_uart_names[LPC_UART_NUM] = { "COM1", "COM2" }; @@ -111,11 +109,11 @@ lpc_uart_intr_assert(void *arg) assert(sc->irq >= 0); - vm_isa_pulse_irq(lpc_bridge->pi_vmctx, sc->irq, sc->irq); + xh_vm_isa_pulse_irq(sc->irq, sc->irq); } static void -lpc_uart_intr_deassert(void *arg) +lpc_uart_intr_deassert(UNUSED void *arg) { /* * The COM devices on the LPC bus generate edge triggered interrupts, @@ -124,8 +122,8 @@ lpc_uart_intr_deassert(void *arg) } static int -lpc_uart_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, - uint32_t *eax, void *arg) +lpc_uart_io_handler(UNUSED int vcpu, int in, int port, int bytes, uint32_t *eax, + void *arg) { int offset; struct lpc_uart_softc *sc = arg; @@ -137,15 +135,15 @@ lpc_uart_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, if (in) *eax = uart_read(sc->uart_softc, offset); else - uart_write(sc->uart_softc, offset, *eax); + uart_write(sc->uart_softc, offset, ((uint8_t) *eax)); break; case 2: if (in) { - *eax = uart_read(sc->uart_softc, offset); - *eax |= uart_read(sc->uart_softc, offset + 1) << 8; + *eax = (uint32_t) uart_read(sc->uart_softc, offset); + *eax |= (uint32_t) (uart_read(sc->uart_softc, offset + 1) << 8); } else { - uart_write(sc->uart_softc, offset, *eax); - uart_write(sc->uart_softc, offset + 1, *eax >> 8); + uart_write(sc->uart_softc, offset, ((uint8_t) *eax)); + uart_write(sc->uart_softc, offset + 1, ((uint8_t) (*eax >> 8))); } break; default: @@ -279,7 +277,7 @@ pci_lpc_sysres_dsdt(void) lsp = *lspp; switch (lsp->type) { case LPC_SYSRES_IO: - dsdt_fixed_ioport(lsp->base, lsp->length); + dsdt_fixed_ioport(((uint16_t) lsp->base), ((uint16_t) lsp->length)); break; case LPC_SYSRES_MEM: dsdt_fixed_mem32(lsp->base, lsp->length); @@ -311,8 +309,8 @@ pci_lpc_uart_dsdt(void) dsdt_line(" Name (_CRS, ResourceTemplate ()"); dsdt_line(" {"); dsdt_indent(2); - dsdt_fixed_ioport(sc->iobase, UART_IO_BAR_SIZE); - dsdt_fixed_irq(sc->irq); + dsdt_fixed_ioport(((uint16_t) sc->iobase), UART_IO_BAR_SIZE); + dsdt_fixed_irq(((uint8_t) sc->irq)); dsdt_unindent(2); dsdt_line(" })"); dsdt_line("}"); @@ -321,8 +319,8 @@ pci_lpc_uart_dsdt(void) LPC_DSDT(pci_lpc_uart_dsdt); static int -pci_lpc_cfgwrite(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, - int coff, int bytes, uint32_t val) +pci_lpc_cfgwrite(UNUSED int vcpu, struct pci_devinst *pi, int coff, int bytes, + uint32_t val) { int pirq_pin; @@ -333,7 +331,7 @@ pci_lpc_cfgwrite(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, if (coff >= 0x68 && coff <= 0x6b) pirq_pin = coff - 0x68 + 5; if (pirq_pin != 0) { - pirq_write(ctx, pirq_pin, val); + pirq_write(pirq_pin, ((uint8_t) val)); pci_set_cfgdata8(pi, coff, pirq_read(pirq_pin)); return (0); } @@ -342,14 +340,14 @@ pci_lpc_cfgwrite(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, } static void -pci_lpc_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, - int baridx, uint64_t offset, int size, uint64_t value) +pci_lpc_write(UNUSED int vcpu, UNUSED struct pci_devinst *pi, UNUSED int baridx, + UNUSED uint64_t offset, UNUSED int size, UNUSED uint64_t value) { } static uint64_t -pci_lpc_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, - int baridx, uint64_t offset, int size) +pci_lpc_read(UNUSED int vcpu, UNUSED struct pci_devinst *pi, UNUSED int baridx, + UNUSED uint64_t offset, UNUSED int size) { return (0); } @@ -358,7 +356,7 @@ pci_lpc_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, #define LPC_VENDOR 0x8086 static int -pci_lpc_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) +pci_lpc_init(struct pci_devinst *pi, UNUSED char *opts) { /* @@ -418,7 +416,7 @@ lpc_pirq_routed(void) pci_set_cfgdata8(lpc_bridge, 0x68 + pin, pirq_read(pin + 5)); } -struct pci_devemu pci_de_lpc = { +static struct pci_devemu pci_de_lpc = { .pe_emu = "lpc", .pe_init = pci_lpc_init, .pe_write_dsdt = pci_lpc_write_dsdt, diff --git a/bhyve/pci_uart.c b/src/pci_uart.c similarity index 81% rename from bhyve/pci_uart.c rename to src/pci_uart.c index 21b93bf..01edcb4 100644 --- a/bhyve/pci_uart.c +++ b/src/pci_uart.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2012 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,16 +27,12 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include - +#include #include - -#include "bhyverun.h" -#include "pci_emul.h" -#include "uart_emul.h" +#include +#include +#include +#include /* * Pick a PCI vid/did of a chip with a single uart at @@ -62,31 +59,31 @@ pci_uart_intr_deassert(void *arg) } static void -pci_uart_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, - int baridx, uint64_t offset, int size, uint64_t value) +pci_uart_write(UNUSED int vcpu, struct pci_devinst *pi, int baridx, uint64_t offset, + int size, uint64_t value) { assert(baridx == 0); assert(size == 1); - uart_write(pi->pi_arg, offset, value); + uart_write(pi->pi_arg, ((int) offset), ((uint8_t) value)); } -uint64_t -pci_uart_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, - int baridx, uint64_t offset, int size) +static uint64_t +pci_uart_read(UNUSED int vcpu, struct pci_devinst *pi, int baridx, + uint64_t offset, int size) { uint8_t val; assert(baridx == 0); assert(size == 1); - val = uart_read(pi->pi_arg, offset); + val = uart_read(pi->pi_arg, ((int) offset)); return (val); } static int -pci_uart_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) +pci_uart_init(struct pci_devinst *pi, char *opts) { struct uart_softc *sc; @@ -110,7 +107,7 @@ pci_uart_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) return (0); } -struct pci_devemu pci_de_com = { +static struct pci_devemu pci_de_com = { .pe_emu = "uart", .pe_init = pci_uart_init, .pe_barwrite = pci_uart_write, diff --git a/bhyve/pci_virtio_block.c b/src/pci_virtio_block.c similarity index 75% rename from bhyve/pci_virtio_block.c rename to src/pci_virtio_block.c index 8500be6..b073454 100644 --- a/bhyve/pci_virtio_block.c +++ b/src/pci_virtio_block.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,77 +27,75 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include -#include #include #include #include #include +#include +#include +#include +#include +#include +#include +#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#define VTBLK_RINGSZ 64 -#include "bhyverun.h" -#include "pci_emul.h" -#include "virtio.h" -#include "block_if.h" +#define VTBLK_S_OK 0 +#define VTBLK_S_IOERR 1 +#define VTBLK_S_UNSUPP 2 -#define VTBLK_RINGSZ 64 - -#define VTBLK_S_OK 0 -#define VTBLK_S_IOERR 1 -#define VTBLK_S_UNSUPP 2 - -#define VTBLK_BLK_ID_BYTES 20 +#define VTBLK_BLK_ID_BYTES 20 /* Capability bits */ -#define VTBLK_F_SEG_MAX (1 << 2) /* Maximum request segments */ -#define VTBLK_F_BLK_SIZE (1 << 6) /* cfg block size valid */ -#define VTBLK_F_FLUSH (1 << 9) /* Cache flush support */ -#define VTBLK_F_TOPOLOGY (1 << 10) /* Optimal I/O alignment */ +#define VTBLK_F_SEG_MAX (1 << 2) /* Maximum request segments */ +#define VTBLK_F_BLK_SIZE (1 << 6) /* cfg block size valid */ +#define VTBLK_F_FLUSH (1 << 9) /* Cache flush support */ +#define VTBLK_F_TOPOLOGY (1 << 10) /* Optimal I/O alignment */ /* * Host capabilities */ -#define VTBLK_S_HOSTCAPS \ - ( VTBLK_F_SEG_MAX | \ - VTBLK_F_BLK_SIZE | \ - VTBLK_F_FLUSH | \ - VTBLK_F_TOPOLOGY | \ - VIRTIO_RING_F_INDIRECT_DESC ) /* indirect descriptors */ +#define VTBLK_S_HOSTCAPS \ + (VTBLK_F_SEG_MAX | \ + VTBLK_F_BLK_SIZE | \ + VTBLK_F_FLUSH | \ + VTBLK_F_TOPOLOGY | \ + VIRTIO_RING_F_INDIRECT_DESC) /* indirect descriptors */ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpacked" /* * Config space "registers" */ struct vtblk_config { - uint64_t vbc_capacity; - uint32_t vbc_size_max; - uint32_t vbc_seg_max; + uint64_t vbc_capacity; + uint32_t vbc_size_max; + uint32_t vbc_seg_max; struct { uint16_t cylinders; uint8_t heads; uint8_t sectors; } vbc_geometry; - uint32_t vbc_blk_size; + uint32_t vbc_blk_size; struct { uint8_t physical_block_exp; uint8_t alignment_offset; uint16_t min_io_size; uint32_t opt_io_size; } vbc_topology; - uint8_t vbc_writeback; + uint8_t vbc_writeback; } __packed; /* @@ -109,23 +108,26 @@ struct virtio_blk_hdr { #define VBH_OP_FLUSH_OUT 5 #define VBH_OP_IDENT 8 #define VBH_FLAG_BARRIER 0x80000000 /* OR'ed into vbh_type */ - uint32_t vbh_type; - uint32_t vbh_ioprio; - uint64_t vbh_sector; + uint32_t vbh_type; + uint32_t vbh_ioprio; + uint64_t vbh_sector; } __packed; +#pragma clang diagnostic pop + /* * Debug printf */ static int pci_vtblk_debug; #define DPRINTF(params) if (pci_vtblk_debug) printf params -#define WPRINTF(params) printf params +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct pci_vtblk_ioreq { - struct blockif_req io_req; - struct pci_vtblk_softc *io_sc; - uint8_t *io_status; - uint16_t io_idx; + struct blockif_req io_req; + struct pci_vtblk_softc *io_sc; + uint8_t *io_status; + uint16_t io_idx; }; /* @@ -141,21 +143,23 @@ struct pci_vtblk_softc { struct pci_vtblk_ioreq vbsc_ios[VTBLK_RINGSZ]; }; +#pragma clang diagnostic pop + static void pci_vtblk_reset(void *); static void pci_vtblk_notify(void *, struct vqueue_info *); static int pci_vtblk_cfgread(void *, int, int, uint32_t *); static int pci_vtblk_cfgwrite(void *, int, int, uint32_t); static struct virtio_consts vtblk_vi_consts = { - "vtblk", /* our name */ - 1, /* we support 1 virtqueue */ + "vtblk", /* our name */ + 1, /* we support 1 virtqueue */ sizeof(struct vtblk_config), /* config reg size */ - pci_vtblk_reset, /* reset */ - pci_vtblk_notify, /* device-wide qnotify */ - pci_vtblk_cfgread, /* read PCI config */ - pci_vtblk_cfgwrite, /* write PCI config */ - NULL, /* apply negotiated features */ - VTBLK_S_HOSTCAPS, /* our capabilities */ + pci_vtblk_reset, /* reset */ + pci_vtblk_notify, /* device-wide qnotify */ + pci_vtblk_cfgread, /* read PCI config */ + pci_vtblk_cfgwrite, /* write PCI config */ + NULL, /* apply negotiated features */ + VTBLK_S_HOSTCAPS, /* our capabilities */ }; static void @@ -167,8 +171,12 @@ pci_vtblk_reset(void *vsc) vi_reset_dev(&sc->vbsc_vs); } +/* xhyve: FIXME + * + * pci_vtblk_done seems to deadlock when called from pci_vtblk_proc? + */ static void -pci_vtblk_done(struct blockif_req *br, int err) +pci_vtblk_done_locked(struct blockif_req *br, int err) { struct pci_vtblk_ioreq *io = br->br_param; struct pci_vtblk_softc *sc = io->io_sc; @@ -185,9 +193,19 @@ pci_vtblk_done(struct blockif_req *br, int err) * Return the descriptor back to the host. * We wrote 1 byte (our status) to host. */ - pthread_mutex_lock(&sc->vsc_mtx); + //pthread_mutex_lock(&sc->vsc_mtx); vq_relchain(&sc->vbsc_vq, io->io_idx, 1); vq_endchains(&sc->vbsc_vq, 0); + //pthread_mutex_unlock(&sc->vsc_mtx); +} + +static void +pci_vtblk_done(struct blockif_req *br, int err) { + struct pci_vtblk_ioreq *io = br->br_param; + struct pci_vtblk_softc *sc = io->io_sc; + + pthread_mutex_lock(&sc->vsc_mtx); + pci_vtblk_done_locked(br, err); pthread_mutex_unlock(&sc->vsc_mtx); } @@ -200,7 +218,6 @@ pci_vtblk_proc(struct pci_vtblk_softc *sc, struct vqueue_info *vq) int err; ssize_t iolen; int writeop, type; - off_t offset; struct iovec iov[BLOCKIF_IOV_MAX + 2]; uint16_t idx, flags[BLOCKIF_IOV_MAX + 2]; @@ -220,9 +237,10 @@ pci_vtblk_proc(struct pci_vtblk_softc *sc, struct vqueue_info *vq) assert((flags[0] & VRING_DESC_F_WRITE) == 0); assert(iov[0].iov_len == sizeof(struct virtio_blk_hdr)); vbh = iov[0].iov_base; - memcpy(&io->io_req.br_iov, &iov[1], sizeof(struct iovec) * (n - 2)); + memcpy(&io->io_req.br_iov, &iov[1], + sizeof(struct iovec) * (((size_t) n) - 2)); io->io_req.br_iovcnt = n - 2; - io->io_req.br_offset = vbh->vbh_sector * DEV_BSIZE; + io->io_req.br_offset = (off_t) (vbh->vbh_sector * DEV_BSIZE); io->io_status = iov[--n].iov_base; assert(iov[n].iov_len == 1); assert(flags[n] & VRING_DESC_F_WRITE); @@ -248,8 +266,8 @@ pci_vtblk_proc(struct pci_vtblk_softc *sc, struct vqueue_info *vq) } io->io_req.br_resid = iolen; - DPRINTF(("virtio-block: %s op, %zd bytes, %d segs, offset %ld\n\r", - writeop ? "write" : "read/ident", iolen, i - 1, offset)); + DPRINTF(("virtio-block: %s op, %zd bytes, %d segs\n\r", + writeop ? "write" : "read/ident", iolen, i - 1)); switch (type) { case VBH_OP_READ: @@ -268,10 +286,12 @@ pci_vtblk_proc(struct pci_vtblk_softc *sc, struct vqueue_info *vq) memset(iov[1].iov_base, 0, iov[1].iov_len); strncpy(iov[1].iov_base, sc->vbsc_ident, MIN(iov[1].iov_len, sizeof(sc->vbsc_ident))); - pci_vtblk_done(&io->io_req, 0); + /* xhyve: FIXME */ + pci_vtblk_done_locked(&io->io_req, 0); return; default: - pci_vtblk_done(&io->io_req, EOPNOTSUPP); + /* xhyve: FIXME */ + pci_vtblk_done_locked(&io->io_req, EOPNOTSUPP); return; } assert(err == 0); @@ -287,7 +307,7 @@ pci_vtblk_notify(void *vsc, struct vqueue_info *vq) } static int -pci_vtblk_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) +pci_vtblk_init(struct pci_devinst *pi, char *opts) { char bident[sizeof("XX:X:X")]; struct blockif_ctxt *bctxt; @@ -323,7 +343,7 @@ pci_vtblk_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) io->io_req.br_callback = pci_vtblk_done; io->io_req.br_param = io; io->io_sc = sc; - io->io_idx = i; + io->io_idx = (uint16_t) i; } pthread_mutex_init(&sc->vsc_mtx, NULL); @@ -340,23 +360,24 @@ pci_vtblk_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) * md5 sum of the filename */ MD5Init(&mdctx); - MD5Update(&mdctx, opts, strlen(opts)); + MD5Update(&mdctx, opts, ((unsigned) strlen(opts))); MD5Final(digest, &mdctx); sprintf(sc->vbsc_ident, "BHYVE-%02X%02X-%02X%02X-%02X%02X", digest[0], digest[1], digest[2], digest[3], digest[4], digest[5]); /* setup virtio block config space */ - sc->vbsc_cfg.vbc_capacity = size / DEV_BSIZE; /* 512-byte units */ + sc->vbsc_cfg.vbc_capacity = + (uint64_t) (size / DEV_BSIZE); /* 512-byte units */ sc->vbsc_cfg.vbc_size_max = 0; /* not negotiated */ sc->vbsc_cfg.vbc_seg_max = BLOCKIF_IOV_MAX; sc->vbsc_cfg.vbc_geometry.cylinders = 0; /* no geometry */ sc->vbsc_cfg.vbc_geometry.heads = 0; sc->vbsc_cfg.vbc_geometry.sectors = 0; - sc->vbsc_cfg.vbc_blk_size = sectsz; + sc->vbsc_cfg.vbc_blk_size = (uint32_t) sectsz; sc->vbsc_cfg.vbc_topology.physical_block_exp = - (sts > sectsz) ? (ffsll(sts / sectsz) - 1) : 0; + (uint8_t) ((sts > sectsz) ? (ffsll(sts / sectsz) - 1) : 0); sc->vbsc_cfg.vbc_topology.alignment_offset = - (sto != 0) ? ((sts - sto) / sectsz) : 0; + (uint8_t) ((sto != 0) ? ((sts - sto) / sectsz) : 0); sc->vbsc_cfg.vbc_topology.min_io_size = 0; sc->vbsc_cfg.vbc_topology.opt_io_size = 0; sc->vbsc_cfg.vbc_writeback = 0; @@ -382,9 +403,9 @@ pci_vtblk_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) } static int -pci_vtblk_cfgwrite(void *vsc, int offset, int size, uint32_t value) +pci_vtblk_cfgwrite(UNUSED void *vsc, int offset, UNUSED int size, + UNUSED uint32_t value) { - DPRINTF(("vtblk: write to readonly reg %d\n\r", offset)); return (1); } @@ -401,7 +422,7 @@ pci_vtblk_cfgread(void *vsc, int offset, int size, uint32_t *retval) return (0); } -struct pci_devemu pci_de_vblk = { +static struct pci_devemu pci_de_vblk = { .pe_emu = "virtio-blk", .pe_init = pci_vtblk_init, .pe_barwrite = vi_pci_write, diff --git a/bhyve/pci_virtio_net.c b/src/pci_virtio_net.c similarity index 82% rename from bhyve/pci_virtio_net.c rename to src/pci_virtio_net.c index 3781ea9..97475f7 100644 --- a/bhyve/pci_virtio_net.c +++ b/src/pci_virtio_net.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,96 +27,95 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include #include +#include +#include #include #include -#include -#include -#include #include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include -#include "bhyverun.h" -#include "pci_emul.h" -#include "mevent.h" -#include "virtio.h" - -#define VTNET_RINGSZ 1024 - -#define VTNET_MAXSEGS 32 +#define VTNET_RINGSZ 1024 +#define VTNET_MAXSEGS 32 /* * Host capabilities. Note that we only offer a few of these. */ -#define VIRTIO_NET_F_CSUM (1 << 0) /* host handles partial cksum */ -#define VIRTIO_NET_F_GUEST_CSUM (1 << 1) /* guest handles partial cksum */ -#define VIRTIO_NET_F_MAC (1 << 5) /* host supplies MAC */ -#define VIRTIO_NET_F_GSO_DEPREC (1 << 6) /* deprecated: host handles GSO */ -#define VIRTIO_NET_F_GUEST_TSO4 (1 << 7) /* guest can rcv TSOv4 */ -#define VIRTIO_NET_F_GUEST_TSO6 (1 << 8) /* guest can rcv TSOv6 */ -#define VIRTIO_NET_F_GUEST_ECN (1 << 9) /* guest can rcv TSO with ECN */ -#define VIRTIO_NET_F_GUEST_UFO (1 << 10) /* guest can rcv UFO */ -#define VIRTIO_NET_F_HOST_TSO4 (1 << 11) /* host can rcv TSOv4 */ -#define VIRTIO_NET_F_HOST_TSO6 (1 << 12) /* host can rcv TSOv6 */ -#define VIRTIO_NET_F_HOST_ECN (1 << 13) /* host can rcv TSO with ECN */ -#define VIRTIO_NET_F_HOST_UFO (1 << 14) /* host can rcv UFO */ -#define VIRTIO_NET_F_MRG_RXBUF (1 << 15) /* host can merge RX buffers */ -#define VIRTIO_NET_F_STATUS (1 << 16) /* config status field available */ -#define VIRTIO_NET_F_CTRL_VQ (1 << 17) /* control channel available */ -#define VIRTIO_NET_F_CTRL_RX (1 << 18) /* control channel RX mode support */ -#define VIRTIO_NET_F_CTRL_VLAN (1 << 19) /* control channel VLAN filtering */ -#define VIRTIO_NET_F_GUEST_ANNOUNCE \ - (1 << 21) /* guest can send gratuitous pkts */ +// #define VIRTIO_NET_F_CSUM (1 << 0) /* host handles partial cksum */ +// #define VIRTIO_NET_F_GUEST_CSUM (1 << 1) /* guest handles partial cksum */ +#define VIRTIO_NET_F_MAC (1 << 5) /* host supplies MAC */ +// #define VIRTIO_NET_F_GSO_DEPREC (1 << 6) /* deprecated: host handles GSO */ +// #define VIRTIO_NET_F_GUEST_TSO4 (1 << 7) /* guest can rcv TSOv4 */ +// #define VIRTIO_NET_F_GUEST_TSO6 (1 << 8) /* guest can rcv TSOv6 */ +// #define VIRTIO_NET_F_GUEST_ECN (1 << 9) /* guest can rcv TSO with ECN */ +// #define VIRTIO_NET_F_GUEST_UFO (1 << 10) /* guest can rcv UFO */ +// #define VIRTIO_NET_F_HOST_TSO4 (1 << 11) /* host can rcv TSOv4 */ +// #define VIRTIO_NET_F_HOST_TSO6 (1 << 12) /* host can rcv TSOv6 */ +// #define VIRTIO_NET_F_HOST_ECN (1 << 13) /* host can rcv TSO with ECN */ +// #define VIRTIO_NET_F_HOST_UFO (1 << 14) /* host can rcv UFO */ +#define VIRTIO_NET_F_MRG_RXBUF (1 << 15) /* host can merge RX buffers */ +#define VIRTIO_NET_F_STATUS (1 << 16) /* config status field available */ +// #define VIRTIO_NET_F_CTRL_VQ (1 << 17) /* control channel available */ +// #define VIRTIO_NET_F_CTRL_RX (1 << 18) /* control channel RX mode support */ +// #define VIRTIO_NET_F_CTRL_VLAN (1 << 19) /* control channel VLAN filtering */ +// #define VIRTIO_NET_F_GUEST_ANNOUNCE (1 << 21) /* guest can send gratuit. pkts */ -#define VTNET_S_HOSTCAPS \ - ( VIRTIO_NET_F_MAC | VIRTIO_NET_F_MRG_RXBUF | VIRTIO_NET_F_STATUS | \ - VIRTIO_F_NOTIFY_ON_EMPTY) +#define VTNET_S_HOSTCAPS \ + (VIRTIO_NET_F_MAC | VIRTIO_NET_F_MRG_RXBUF | VIRTIO_NET_F_STATUS | \ + VIRTIO_F_NOTIFY_ON_EMPTY) + +#define ETHER_IS_MULTICAST(addr) (*(addr) & 0x01) /* is address mcast/bcast? */ + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpacked" /* * PCI config-space "registers" */ struct virtio_net_config { - uint8_t mac[6]; + uint8_t mac[6]; uint16_t status; } __packed; /* * Queue definitions. */ -#define VTNET_RXQ 0 -#define VTNET_TXQ 1 -#define VTNET_CTLQ 2 /* NB: not yet supported */ - -#define VTNET_MAXQ 3 +#define VTNET_RXQ 0 +#define VTNET_TXQ 1 +// #define VTNET_CTLQ 2 /* NB: not yet supported */ +#define VTNET_MAXQ 3 /* * Fixed network header size */ struct virtio_net_rxhdr { - uint8_t vrh_flags; - uint8_t vrh_gso_type; - uint16_t vrh_hdr_len; - uint16_t vrh_gso_size; - uint16_t vrh_csum_start; - uint16_t vrh_csum_offset; - uint16_t vrh_bufs; + uint8_t vrh_flags; + uint8_t vrh_gso_type; + uint16_t vrh_hdr_len; + uint16_t vrh_gso_size; + uint16_t vrh_csum_start; + uint16_t vrh_csum_offset; + uint16_t vrh_bufs; } __packed; +#pragma clang diagnostic pop + /* * Debug printf */ @@ -123,6 +123,8 @@ static int pci_vtnet_debug; #define DPRINTF(params) if (pci_vtnet_debug) printf params #define WPRINTF(params) printf params +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" /* * Per-device softc */ @@ -130,26 +132,22 @@ struct pci_vtnet_softc { struct virtio_softc vsc_vs; struct vqueue_info vsc_queues[VTNET_MAXQ - 1]; pthread_mutex_t vsc_mtx; - struct mevent *vsc_mevp; - - int vsc_tapfd; - int vsc_rx_ready; - volatile int resetting; /* set and checked outside lock */ - - uint64_t vsc_features; /* negotiated features */ - + struct mevent *vsc_mevp; + int vsc_tapfd; + int vsc_rx_ready; + volatile int resetting;/* set and checked outside lock */ + uint64_t vsc_features; /* negotiated features */ struct virtio_net_config vsc_config; - - pthread_mutex_t rx_mtx; - int rx_in_progress; - int rx_vhdrlen; - int rx_merge; /* merged rx bufs in use */ - - pthread_t tx_tid; - pthread_mutex_t tx_mtx; - pthread_cond_t tx_cond; - int tx_in_progress; + pthread_mutex_t rx_mtx; + int rx_in_progress; + int rx_vhdrlen; + int rx_merge; /* merged rx bufs in use */ + pthread_t tx_tid; + pthread_mutex_t tx_mtx; + pthread_cond_t tx_cond; + int tx_in_progress; }; +#pragma clang diagnostic pop static void pci_vtnet_reset(void *); /* static void pci_vtnet_notify(void *, struct vqueue_info *); */ @@ -246,7 +244,7 @@ pci_vtnet_tap_tx(struct pci_vtnet_softc *sc, struct iovec *iov, int iovcnt, */ if (len < 60) { iov[iovcnt].iov_base = pad; - iov[iovcnt].iov_len = 60 - len; + iov[iovcnt].iov_len = (size_t) (60 - len); iovcnt++; } (void) writev(sc->vsc_tapfd, iov, iovcnt); @@ -267,15 +265,16 @@ rx_iov_trim(struct iovec *iov, int *niov, int tlen) struct iovec *riov; /* XXX short-cut: assume first segment is >= tlen */ - assert(iov[0].iov_len >= tlen); + assert(iov[0].iov_len >= ((size_t) tlen)); - iov[0].iov_len -= tlen; + iov[0].iov_len -= ((size_t) tlen); if (iov[0].iov_len == 0) { assert(*niov > 1); *niov -= 1; riov = &iov[1]; } else { - iov[0].iov_base = (void *)((uintptr_t)iov[0].iov_base + tlen); + iov[0].iov_base = (void *)((uintptr_t)iov[0].iov_base + + ((size_t) tlen)); riov = &iov[0]; } @@ -336,7 +335,7 @@ pci_vtnet_tap_rx(struct pci_vtnet_softc *sc) vrx = iov[0].iov_base; riov = rx_iov_trim(iov, &n, sc->rx_vhdrlen); - len = readv(sc->vsc_tapfd, riov, n); + len = (int) readv(sc->vsc_tapfd, riov, n); if (len < 0 && errno == EWOULDBLOCK) { /* @@ -364,7 +363,7 @@ pci_vtnet_tap_rx(struct pci_vtnet_softc *sc) /* * Release this chain and handle more chains. */ - vq_relchain(vq, idx, len + sc->rx_vhdrlen); + vq_relchain(vq, idx, ((uint32_t) (len + sc->rx_vhdrlen))); } while (vq_has_descs(vq)); /* Interrupt if needed, including for NOTIFY_ON_EMPTY. */ @@ -372,7 +371,7 @@ pci_vtnet_tap_rx(struct pci_vtnet_softc *sc) } static void -pci_vtnet_tap_callback(int fd, enum ev_type type, void *param) +pci_vtnet_tap_callback(UNUSED int fd, UNUSED enum ev_type type, void *param) { struct pci_vtnet_softc *sc = param; @@ -414,7 +413,7 @@ pci_vtnet_proctx(struct pci_vtnet_softc *sc, struct vqueue_info *vq) n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL); assert(n >= 1 && n <= VTNET_MAXSEGS); plen = 0; - tlen = iov[0].iov_len; + tlen = (int) iov[0].iov_len; for (i = 1; i < n; i++) { plen += iov[i].iov_len; tlen += iov[i].iov_len; @@ -424,7 +423,7 @@ pci_vtnet_proctx(struct pci_vtnet_softc *sc, struct vqueue_info *vq) pci_vtnet_tap_tx(sc, &iov[1], n - 1, plen); /* chain is processed, release it and set tlen */ - vq_relchain(vq, idx, tlen); + vq_relchain(vq, idx, ((uint32_t) tlen)); } static void @@ -534,12 +533,11 @@ pci_vtnet_parsemac(char *mac_str, uint8_t *mac_addr) static int -pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) +pci_vtnet_init(struct pci_devinst *pi, char *opts) { MD5_CTX mdctx; unsigned char digest[16]; char nstr[80]; - char tname[MAXCOMLEN + 1]; struct pci_vtnet_softc *sc; char *devname; char *vtopts; @@ -624,7 +622,7 @@ pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) pi->pi_func, vmname); MD5Init(&mdctx); - MD5Update(&mdctx, nstr, strlen(nstr)); + MD5Update(&mdctx, nstr, ((unsigned int) strlen(nstr))); MD5Final(digest, &mdctx); sc->vsc_config.mac[0] = 0x00; @@ -668,10 +666,6 @@ pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) pthread_mutex_init(&sc->tx_mtx, NULL); pthread_cond_init(&sc->tx_cond, NULL); pthread_create(&sc->tx_tid, NULL, pci_vtnet_tx_thread, (void *)sc); - snprintf(tname, sizeof(tname), "vtnet-%d:%d tx", pi->pi_slot, - pi->pi_func); - pthread_set_name_np(sc->tx_tid, tname); - return (0); } @@ -721,7 +715,7 @@ pci_vtnet_neg_features(void *vsc, uint64_t negotiated_features) } } -struct pci_devemu pci_de_vnet = { +static struct pci_devemu pci_de_vnet = { .pe_emu = "virtio-net", .pe_init = pci_vtnet_init, .pe_barwrite = vi_pci_write, diff --git a/bhyve/pci_virtio_rnd.c b/src/pci_virtio_rnd.c similarity index 80% rename from bhyve/pci_virtio_rnd.c rename to src/pci_virtio_rnd.c index 78448f5..b392c46 100644 --- a/bhyve/pci_virtio_rnd.c +++ b/src/pci_virtio_rnd.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2014 Nahanni Systems Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,57 +32,57 @@ * once it has been seeded at bootup. */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include - -#include -#include -#include +#include #include +#include #include -#include -#include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include -#include "bhyverun.h" -#include "pci_emul.h" -#include "virtio.h" - -#define VTRND_RINGSZ 64 +#define VTRND_RINGSZ 64 static int pci_vtrnd_debug; #define DPRINTF(params) if (pci_vtrnd_debug) printf params #define WPRINTF(params) printf params +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" /* * Per-device softc */ struct pci_vtrnd_softc { struct virtio_softc vrsc_vs; - struct vqueue_info vrsc_vq; - pthread_mutex_t vrsc_mtx; - uint64_t vrsc_cfg; - int vrsc_fd; + struct vqueue_info vrsc_vq; + pthread_mutex_t vrsc_mtx; + uint64_t vrsc_cfg; + int vrsc_fd; }; +#pragma clang diagnostic pop static void pci_vtrnd_reset(void *); static void pci_vtrnd_notify(void *, struct vqueue_info *); static struct virtio_consts vtrnd_vi_consts = { - "vtrnd", /* our name */ - 1, /* we support 1 virtqueue */ - 0, /* config reg size */ - pci_vtrnd_reset, /* reset */ - pci_vtrnd_notify, /* device-wide qnotify */ - NULL, /* read virtio config */ - NULL, /* write virtio config */ - NULL, /* apply negotiated features */ - 0, /* our capabilities */ + "vtrnd", /* our name */ + 1, /* we support 1 virtqueue */ + 0, /* config reg size */ + pci_vtrnd_reset, /* reset */ + pci_vtrnd_notify, /* device-wide qnotify */ + NULL, /* read virtio config */ + NULL, /* write virtio config */ + NULL, /* apply negotiated features */ + 0, /* our capabilities */ }; @@ -115,7 +116,7 @@ pci_vtrnd_notify(void *vsc, struct vqueue_info *vq) while (vq_has_descs(vq)) { vq_getchain(vq, &idx, &iov, 1, NULL); - len = read(sc->vrsc_fd, iov.iov_base, iov.iov_len); + len = (int) read(sc->vrsc_fd, iov.iov_base, iov.iov_len); DPRINTF(("vtrnd: vtrnd_notify(): %d\r\n", len)); @@ -125,14 +126,14 @@ pci_vtrnd_notify(void *vsc, struct vqueue_info *vq) /* * Release this chain and handle more */ - vq_relchain(vq, idx, len); + vq_relchain(vq, idx, ((uint32_t) len)); } vq_endchains(vq, 1); /* Generate interrupt if appropriate. */ } static int -pci_vtrnd_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) +pci_vtrnd_init(struct pci_devinst *pi, UNUSED char *opts) { struct pci_vtrnd_softc *sc; int fd; @@ -149,7 +150,7 @@ pci_vtrnd_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) /* * Check that device is seeded and non-blocking. */ - len = read(fd, &v, sizeof(v)); + len = (int) read(fd, &v, sizeof(v)); if (len <= 0) { WPRINTF(("vtrnd: /dev/random not ready, read(): %d", len)); return (1); @@ -180,7 +181,7 @@ pci_vtrnd_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) } -struct pci_devemu pci_de_vrnd = { +static struct pci_devemu pci_de_vrnd = { .pe_emu = "virtio-rnd", .pe_init = pci_vtrnd_init, .pe_barwrite = vi_pci_write, diff --git a/src/pci_virtio_vmnet.c b/src/pci_virtio_vmnet.c new file mode 100644 index 0000000..2c02d96 --- /dev/null +++ b/src/pci_virtio_vmnet.c @@ -0,0 +1,811 @@ +/*- + * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* + * + * The vmnet support is ported from the Mirage OS project: + * + * https://github.com/mirage/ocaml-vmnet + * + * Copyright (C) 2014 Anil Madhavapeddy + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define VTNET_RINGSZ 1024 +#define VTNET_MAXSEGS 32 + +/* + * Host capabilities. Note that we only offer a few of these. + */ +// #define VIRTIO_NET_F_CSUM (1 << 0) /* host handles partial cksum */ +// #define VIRTIO_NET_F_GUEST_CSUM (1 << 1) /* guest handles partial cksum */ +#define VIRTIO_NET_F_MAC (1 << 5) /* host supplies MAC */ +// #define VIRTIO_NET_F_GSO_DEPREC (1 << 6) /* deprecated: host handles GSO */ +// #define VIRTIO_NET_F_GUEST_TSO4 (1 << 7) /* guest can rcv TSOv4 */ +// #define VIRTIO_NET_F_GUEST_TSO6 (1 << 8) /* guest can rcv TSOv6 */ +// #define VIRTIO_NET_F_GUEST_ECN (1 << 9) /* guest can rcv TSO with ECN */ +// #define VIRTIO_NET_F_GUEST_UFO (1 << 10) /* guest can rcv UFO */ +// #define VIRTIO_NET_F_HOST_TSO4 (1 << 11) /* host can rcv TSOv4 */ +// #define VIRTIO_NET_F_HOST_TSO6 (1 << 12) /* host can rcv TSOv6 */ +// #define VIRTIO_NET_F_HOST_ECN (1 << 13) /* host can rcv TSO with ECN */ +// #define VIRTIO_NET_F_HOST_UFO (1 << 14) /* host can rcv UFO */ +#define VIRTIO_NET_F_MRG_RXBUF (1 << 15) /* host can merge RX buffers */ +#define VIRTIO_NET_F_STATUS (1 << 16) /* config status field available */ +// #define VIRTIO_NET_F_CTRL_VQ (1 << 17) /* control channel available */ +// #define VIRTIO_NET_F_CTRL_RX (1 << 18) /* control channel RX mode support */ +// #define VIRTIO_NET_F_CTRL_VLAN (1 << 19) /* control channel VLAN filtering */ +// #define VIRTIO_NET_F_GUEST_ANNOUNCE (1 << 21) /* guest can send gratuit. pkts */ + +#define VTNET_S_HOSTCAPS \ + (VIRTIO_NET_F_MAC | VIRTIO_NET_F_MRG_RXBUF | VIRTIO_NET_F_STATUS | \ + VIRTIO_F_NOTIFY_ON_EMPTY) + +// #define ETHER_IS_MULTICAST(addr) (*(addr) & 0x01) /* is address mcast/bcast? */ + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpacked" + +/* + * PCI config-space "registers" + */ +struct virtio_net_config { + uint8_t mac[6]; + uint16_t status; +} __packed; + +/* + * Queue definitions. + */ +#define VTNET_RXQ 0 +#define VTNET_TXQ 1 +// #define VTNET_CTLQ 2 /* NB: not yet supported */ +#define VTNET_MAXQ 3 + +/* + * Fixed network header size + */ +struct virtio_net_rxhdr { + uint8_t vrh_flags; + uint8_t vrh_gso_type; + uint16_t vrh_hdr_len; + uint16_t vrh_gso_size; + uint16_t vrh_csum_start; + uint16_t vrh_csum_offset; + uint16_t vrh_bufs; +} __packed; + +#pragma clang diagnostic pop + +/* + * Debug printf + */ +static int pci_vtnet_debug; +#define DPRINTF(params) if (pci_vtnet_debug) printf params + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +/* + * Per-device softc + */ +struct pci_vtnet_softc { + struct virtio_softc vsc_vs; + struct vqueue_info vsc_queues[VTNET_MAXQ - 1]; + pthread_mutex_t vsc_mtx; + struct vmnet_state *vms; + int vsc_rx_ready; + volatile int resetting;/* set and checked outside lock */ + uint64_t vsc_features; /* negotiated features */ + struct virtio_net_config vsc_config; + pthread_mutex_t rx_mtx; + int rx_in_progress; + int rx_vhdrlen; + int rx_merge; /* merged rx bufs in use */ + pthread_t tx_tid; + pthread_mutex_t tx_mtx; + pthread_cond_t tx_cond; + int tx_in_progress; +}; + +static void pci_vtnet_reset(void *); +/* static void pci_vtnet_notify(void *, struct vqueue_info *); */ +static int pci_vtnet_cfgread(void *, int, int, uint32_t *); +static int pci_vtnet_cfgwrite(void *, int, int, uint32_t); +static void pci_vtnet_neg_features(void *, uint64_t); + +static struct virtio_consts vtnet_vi_consts = { + "vtnet", /* our name */ + VTNET_MAXQ - 1, /* we currently support 2 virtqueues */ + sizeof(struct virtio_net_config), /* config reg size */ + pci_vtnet_reset, /* reset */ + NULL, /* device-wide qnotify -- not used */ + pci_vtnet_cfgread, /* read PCI config */ + pci_vtnet_cfgwrite, /* write PCI config */ + pci_vtnet_neg_features, /* apply negotiated features */ + VTNET_S_HOSTCAPS, /* our capabilities */ +}; + +struct vmnet_state { + interface_ref iface; + uint8_t mac[6]; + unsigned int mtu; + unsigned int max_packet_size; +}; + +#pragma clang diagnostic pop + +static void pci_vtnet_tap_callback(struct pci_vtnet_softc *sc); + +static struct vmnet_state * +vmn_create(struct pci_vtnet_softc *sc) +{ + xpc_object_t interface_desc; + uuid_t uuid; + __block interface_ref iface; + __block vmnet_return_t iface_status; + dispatch_semaphore_t iface_created; + dispatch_queue_t if_create_q; + dispatch_queue_t if_q; + struct vmnet_state *vms; + + interface_desc = xpc_dictionary_create(NULL, NULL, 0); + xpc_dictionary_set_uint64(interface_desc, vmnet_operation_mode_key, + VMNET_SHARED_MODE); + uuid_generate_random(uuid); + xpc_dictionary_set_uuid(interface_desc, vmnet_interface_id_key, uuid); + iface = NULL; + iface_status = 0; + + vms = malloc(sizeof(struct vmnet_state)); + + if (!vms) { + return (NULL); + } + + if_create_q = dispatch_queue_create("org.xhyve.vmnet.create", + DISPATCH_QUEUE_SERIAL); + + iface_created = dispatch_semaphore_create(0); + + iface = vmnet_start_interface(interface_desc, if_create_q, + ^(vmnet_return_t status, xpc_object_t interface_param) + { + iface_status = status; + if (status != VMNET_SUCCESS || !interface_param) { + dispatch_semaphore_signal(iface_created); + return; + } + + if (sscanf(xpc_dictionary_get_string(interface_param, + vmnet_mac_address_key), + "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", + &vms->mac[0], &vms->mac[1], &vms->mac[2], &vms->mac[3], + &vms->mac[4], &vms->mac[5]) != 6) + { + assert(0); + } + + vms->mtu = (unsigned) + xpc_dictionary_get_uint64(interface_param, vmnet_mtu_key); + vms->max_packet_size = (unsigned) + xpc_dictionary_get_uint64(interface_param, + vmnet_max_packet_size_key); + dispatch_semaphore_signal(iface_created); + }); + + dispatch_semaphore_wait(iface_created, DISPATCH_TIME_FOREVER); + dispatch_release(if_create_q); + + if (iface == NULL || iface_status != VMNET_SUCCESS) { + printf("virtio_net: Could not create vmnet interface, " + "permission denied or no entitlement?\n"); + free(vms); + return (NULL); + } + + vms->iface = iface; + + if_q = dispatch_queue_create("org.xhyve.vmnet.iface_q", 0); + + vmnet_interface_set_event_callback(iface, VMNET_INTERFACE_PACKETS_AVAILABLE, + if_q, ^(UNUSED interface_event_t event_id, UNUSED xpc_object_t event) + { + pci_vtnet_tap_callback(sc); + }); + + return (vms); +} + +static ssize_t +vmn_read(struct vmnet_state *vms, struct iovec *iov, int n) { + vmnet_return_t r; + struct vmpktdesc v; + int pktcnt; + int i; + + v.vm_pkt_size = 0; + + for (i = 0; i < n; i++) { + v.vm_pkt_size += iov[i].iov_len; + } + + assert(v.vm_pkt_size >= vms->max_packet_size); + + v.vm_pkt_iov = iov; + v.vm_pkt_iovcnt = (uint32_t) n; + v.vm_flags = 0; /* TODO no clue what this is */ + + pktcnt = 1; + + r = vmnet_read(vms->iface, &v, &pktcnt); + + assert(r == VMNET_SUCCESS); + + if (pktcnt < 1) { + return (-1); + } + + return ((ssize_t) v.vm_pkt_size); +} + +static void +vmn_write(struct vmnet_state *vms, struct iovec *iov, int n) { + vmnet_return_t r; + struct vmpktdesc v; + int pktcnt; + int i; + + v.vm_pkt_size = 0; + + for (i = 0; i < n; i++) { + v.vm_pkt_size += iov[i].iov_len; + } + + assert(v.vm_pkt_size <= vms->max_packet_size); + + v.vm_pkt_iov = iov; + v.vm_pkt_iovcnt = (uint32_t) n; + v.vm_flags = 0; /* TODO no clue what this is */ + + pktcnt = 1; + + r = vmnet_write(vms->iface, &v, &pktcnt); + + assert(r == VMNET_SUCCESS); +} + +/* + * If the transmit thread is active then stall until it is done. + */ +static void +pci_vtnet_txwait(struct pci_vtnet_softc *sc) +{ + + pthread_mutex_lock(&sc->tx_mtx); + while (sc->tx_in_progress) { + pthread_mutex_unlock(&sc->tx_mtx); + usleep(10000); + pthread_mutex_lock(&sc->tx_mtx); + } + pthread_mutex_unlock(&sc->tx_mtx); +} + +/* + * If the receive thread is active then stall until it is done. + */ +static void +pci_vtnet_rxwait(struct pci_vtnet_softc *sc) +{ + + pthread_mutex_lock(&sc->rx_mtx); + while (sc->rx_in_progress) { + pthread_mutex_unlock(&sc->rx_mtx); + usleep(10000); + pthread_mutex_lock(&sc->rx_mtx); + } + pthread_mutex_unlock(&sc->rx_mtx); +} + +static void +pci_vtnet_reset(void *vsc) +{ + struct pci_vtnet_softc *sc = vsc; + + DPRINTF(("vtnet: device reset requested !\n")); + + sc->resetting = 1; + + /* + * Wait for the transmit and receive threads to finish their + * processing. + */ + pci_vtnet_txwait(sc); + pci_vtnet_rxwait(sc); + + sc->vsc_rx_ready = 0; + sc->rx_merge = 1; + sc->rx_vhdrlen = sizeof(struct virtio_net_rxhdr); + + /* now reset rings, MSI-X vectors, and negotiated capabilities */ + vi_reset_dev(&sc->vsc_vs); + + sc->resetting = 0; +} + +/* + * Called to send a buffer chain out to the tap device + */ +static void +pci_vtnet_tap_tx(struct pci_vtnet_softc *sc, struct iovec *iov, int iovcnt, + int len) +{ + static char pad[60]; /* all zero bytes */ + + if (!sc->vms) + return; + + /* + * If the length is < 60, pad out to that and add the + * extra zero'd segment to the iov. It is guaranteed that + * there is always an extra iov available by the caller. + */ + if (len < 60) { + iov[iovcnt].iov_base = pad; + iov[iovcnt].iov_len = (size_t) (60 - len); + iovcnt++; + } + vmn_write(sc->vms, iov, iovcnt); +} + +/* + * Called when there is read activity on the tap file descriptor. + * Each buffer posted by the guest is assumed to be able to contain + * an entire ethernet frame + rx header. + * MP note: the dummybuf is only used for discarding frames, so there + * is no need for it to be per-vtnet or locked. + */ +static uint8_t dummybuf[2048]; + +static __inline struct iovec * +rx_iov_trim(struct iovec *iov, int *niov, int tlen) +{ + struct iovec *riov; + + /* XXX short-cut: assume first segment is >= tlen */ + assert(iov[0].iov_len >= ((size_t) tlen)); + + iov[0].iov_len -= ((size_t) tlen); + if (iov[0].iov_len == 0) { + assert(*niov > 1); + *niov -= 1; + riov = &iov[1]; + } else { + iov[0].iov_base = (void *)((uintptr_t)iov[0].iov_base + + ((size_t) tlen)); + riov = &iov[0]; + } + + return (riov); +} + +static void +pci_vtnet_tap_rx(struct pci_vtnet_softc *sc) +{ + struct iovec iov[VTNET_MAXSEGS], *riov; + struct vqueue_info *vq; + void *vrx; + int len, n; + uint16_t idx; + + /* + * Should never be called without a valid tap fd + */ + assert(sc->vms); + + /* + * But, will be called when the rx ring hasn't yet + * been set up or the guest is resetting the device. + */ + if (!sc->vsc_rx_ready || sc->resetting) { + /* + * Drop the packet and try later. + */ + iov[0].iov_base = dummybuf; + iov[0].iov_len = sizeof(dummybuf); + (void) vmn_read(sc->vms, iov, 1); + return; + } + + /* + * Check for available rx buffers + */ + vq = &sc->vsc_queues[VTNET_RXQ]; + if (!vq_has_descs(vq)) { + /* + * Drop the packet and try later. Interrupt on + * empty, if that's negotiated. + */ + iov[0].iov_base = dummybuf; + iov[0].iov_len = sizeof(dummybuf); + (void) vmn_read(sc->vms, iov, 1); + vq_endchains(vq, 1); + return; + } + + do { + /* + * Get descriptor chain. + */ + n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL); + assert(n >= 1 && n <= VTNET_MAXSEGS); + + /* + * Get a pointer to the rx header, and use the + * data immediately following it for the packet buffer. + */ + vrx = iov[0].iov_base; + riov = rx_iov_trim(iov, &n, sc->rx_vhdrlen); + + len = (int) vmn_read(sc->vms, riov, n); + + if (len < 0 && errno == EWOULDBLOCK) { + /* + * No more packets, but still some avail ring + * entries. Interrupt if needed/appropriate. + */ + vq_retchain(vq); + vq_endchains(vq, 0); + return; + } + + /* + * The only valid field in the rx packet header is the + * number of buffers if merged rx bufs were negotiated. + */ + memset(vrx, 0, sc->rx_vhdrlen); + + if (sc->rx_merge) { + struct virtio_net_rxhdr *vrxh; + + vrxh = vrx; + vrxh->vrh_bufs = 1; + } + + /* + * Release this chain and handle more chains. + */ + vq_relchain(vq, idx, ((uint32_t) (len + sc->rx_vhdrlen))); + } while (vq_has_descs(vq)); + + /* Interrupt if needed, including for NOTIFY_ON_EMPTY. */ + vq_endchains(vq, 1); +} + +static void +pci_vtnet_tap_callback(struct pci_vtnet_softc *sc) +{ + pthread_mutex_lock(&sc->rx_mtx); + sc->rx_in_progress = 1; + pci_vtnet_tap_rx(sc); + sc->rx_in_progress = 0; + pthread_mutex_unlock(&sc->rx_mtx); + +} + +static void +pci_vtnet_ping_rxq(void *vsc, struct vqueue_info *vq) +{ + struct pci_vtnet_softc *sc = vsc; + + /* + * A qnotify means that the rx process can now begin + */ + if (sc->vsc_rx_ready == 0) { + sc->vsc_rx_ready = 1; + vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY; + } +} + +static void +pci_vtnet_proctx(struct pci_vtnet_softc *sc, struct vqueue_info *vq) +{ + struct iovec iov[VTNET_MAXSEGS + 1]; + int i, n; + int plen, tlen; + uint16_t idx; + + /* + * Obtain chain of descriptors. The first one is + * really the header descriptor, so we need to sum + * up two lengths: packet length and transfer length. + */ + n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL); + assert(n >= 1 && n <= VTNET_MAXSEGS); + plen = 0; + tlen = (int) iov[0].iov_len; + for (i = 1; i < n; i++) { + plen += iov[i].iov_len; + tlen += iov[i].iov_len; + } + + DPRINTF(("virtio: packet send, %d bytes, %d segs\n\r", plen, n)); + pci_vtnet_tap_tx(sc, &iov[1], n - 1, plen); + + /* chain is processed, release it and set tlen */ + vq_relchain(vq, idx, ((uint32_t) tlen)); +} + +static void +pci_vtnet_ping_txq(void *vsc, struct vqueue_info *vq) +{ + struct pci_vtnet_softc *sc = vsc; + + /* + * Any ring entries to process? + */ + if (!vq_has_descs(vq)) + return; + + /* Signal the tx thread for processing */ + pthread_mutex_lock(&sc->tx_mtx); + vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY; + if (sc->tx_in_progress == 0) + pthread_cond_signal(&sc->tx_cond); + pthread_mutex_unlock(&sc->tx_mtx); +} + +/* + * Thread which will handle processing of TX desc + */ +static void * +pci_vtnet_tx_thread(void *param) +{ + struct pci_vtnet_softc *sc = param; + struct vqueue_info *vq; + int error; + + vq = &sc->vsc_queues[VTNET_TXQ]; + + /* + * Let us wait till the tx queue pointers get initialised & + * first tx signaled + */ + pthread_mutex_lock(&sc->tx_mtx); + error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx); + assert(error == 0); + + for (;;) { + /* note - tx mutex is locked here */ + while (sc->resetting || !vq_has_descs(vq)) { + vq->vq_used->vu_flags &= ~VRING_USED_F_NO_NOTIFY; + mb(); + if (!sc->resetting && vq_has_descs(vq)) + break; + + sc->tx_in_progress = 0; + error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx); + assert(error == 0); + } + vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY; + sc->tx_in_progress = 1; + pthread_mutex_unlock(&sc->tx_mtx); + + do { + /* + * Run through entries, placing them into + * iovecs and sending when an end-of-packet + * is found + */ + pci_vtnet_proctx(sc, vq); + } while (vq_has_descs(vq)); + + /* + * Generate an interrupt if needed. + */ + vq_endchains(vq, 1); + + pthread_mutex_lock(&sc->tx_mtx); + } +} + +#ifdef notyet +static void +pci_vtnet_ping_ctlq(void *vsc, struct vqueue_info *vq) +{ + DPRINTF(("vtnet: control qnotify!\n\r")); +} +#endif + +static int +pci_vtnet_init(struct pci_devinst *pi, UNUSED char *opts) +{ + struct pci_vtnet_softc *sc; + int mac_provided; + + sc = calloc(1, sizeof(struct pci_vtnet_softc)); + + pthread_mutex_init(&sc->vsc_mtx, NULL); + + vi_softc_linkup(&sc->vsc_vs, &vtnet_vi_consts, sc, pi, sc->vsc_queues); + sc->vsc_vs.vs_mtx = &sc->vsc_mtx; + + sc->vsc_queues[VTNET_RXQ].vq_qsize = VTNET_RINGSZ; + sc->vsc_queues[VTNET_RXQ].vq_notify = pci_vtnet_ping_rxq; + sc->vsc_queues[VTNET_TXQ].vq_qsize = VTNET_RINGSZ; + sc->vsc_queues[VTNET_TXQ].vq_notify = pci_vtnet_ping_txq; +#ifdef notyet + sc->vsc_queues[VTNET_CTLQ].vq_qsize = VTNET_RINGSZ; + sc->vsc_queues[VTNET_CTLQ].vq_notify = pci_vtnet_ping_ctlq; +#endif + + /* + * Attempt to open the tap device and read the MAC address + * if specified + */ + mac_provided = 0; + sc->vms = NULL; + + sc->vms = vmn_create(sc); + + if (!sc->vms) { + return (-1); + } + + sc->vsc_config.mac[0] = sc->vms->mac[0]; + sc->vsc_config.mac[1] = sc->vms->mac[1]; + sc->vsc_config.mac[2] = sc->vms->mac[2]; + sc->vsc_config.mac[3] = sc->vms->mac[3]; + sc->vsc_config.mac[4] = sc->vms->mac[4]; + sc->vsc_config.mac[5] = sc->vms->mac[5]; + + /* initialize config space */ + pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_NET); + pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR); + pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_NETWORK); + pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_NET); + pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR); + + /* Link is up if we managed to open tap device. */ + sc->vsc_config.status = 1; + + /* use BAR 1 to map MSI-X table and PBA, if we're using MSI-X */ + if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix())) + return (1); + + /* use BAR 0 to map config regs in IO space */ + vi_set_io_bar(&sc->vsc_vs, 0); + + sc->resetting = 0; + + sc->rx_merge = 1; + sc->rx_vhdrlen = sizeof(struct virtio_net_rxhdr); + sc->rx_in_progress = 0; + pthread_mutex_init(&sc->rx_mtx, NULL); + + /* + * Initialize tx semaphore & spawn TX processing thread. + * As of now, only one thread for TX desc processing is + * spawned. + */ + sc->tx_in_progress = 0; + pthread_mutex_init(&sc->tx_mtx, NULL); + pthread_cond_init(&sc->tx_cond, NULL); + pthread_create(&sc->tx_tid, NULL, pci_vtnet_tx_thread, (void *)sc); + return (0); +} + +static int +pci_vtnet_cfgwrite(void *vsc, int offset, int size, uint32_t value) +{ + struct pci_vtnet_softc *sc = vsc; + void *ptr; + + if (offset < 6) { + assert(offset + size <= 6); + /* + * The driver is allowed to change the MAC address + */ + ptr = &sc->vsc_config.mac[offset]; + memcpy(ptr, &value, size); + } else { + /* silently ignore other writes */ + DPRINTF(("vtnet: write to readonly reg %d\n\r", offset)); + } + + return (0); +} + +static int +pci_vtnet_cfgread(void *vsc, int offset, int size, uint32_t *retval) +{ + struct pci_vtnet_softc *sc = vsc; + void *ptr; + + ptr = (uint8_t *)&sc->vsc_config + offset; + memcpy(retval, ptr, size); + return (0); +} + +static void +pci_vtnet_neg_features(void *vsc, uint64_t negotiated_features) +{ + struct pci_vtnet_softc *sc = vsc; + + sc->vsc_features = negotiated_features; + + if (!(sc->vsc_features & VIRTIO_NET_F_MRG_RXBUF)) { + sc->rx_merge = 0; + /* non-merge rx header is 2 bytes shorter */ + sc->rx_vhdrlen -= 2; + } +} + +static struct pci_devemu pci_de_vnet = { + .pe_emu = "virtio-net", + .pe_init = pci_vtnet_init, + .pe_barwrite = vi_pci_write, + .pe_barread = vi_pci_read +}; +PCI_EMUL_SET(pci_de_vnet); diff --git a/bhyve/pm.c b/src/pm.c similarity index 80% rename from bhyve/pm.c rename to src/pm.c index f7c1c23..56139b0 100644 --- a/bhyve/pm.c +++ b/src/pm.c @@ -1,6 +1,7 @@ /*- * Copyright (c) 2013 Hudson River Trading LLC * Written by: John H. Baldwin + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -25,23 +26,18 @@ * SUCH DAMAGE. */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include - +#include #include #include #include #include -#include - -#include "acpi.h" -#include "inout.h" -#include "mevent.h" -#include "pci_irq.h" -#include "pci_lpc.h" +#include +#include +#include +#include +#include +#include +#include static pthread_mutex_t pm_lock = PTHREAD_MUTEX_INITIALIZER; static struct mevent *power_button; @@ -54,8 +50,8 @@ static sig_t old_power_handler; * reset. */ static int -reset_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, - uint32_t *eax, void *arg) +reset_handler(UNUSED int vcpu, int in, UNUSED int port, int bytes, + uint32_t *eax, UNUSED void *arg) { int error; @@ -66,11 +62,11 @@ reset_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, if (in) *eax = reset_control; else { - reset_control = *eax; + reset_control = (uint8_t) *eax; /* Treat hard and soft resets the same. */ if (reset_control & 0x4) { - error = vm_suspend(ctx, VM_SUSPEND_RESET); + error = xh_vm_suspend(VM_SUSPEND_RESET); assert(error == 0 || errno == EALREADY); } } @@ -84,22 +80,20 @@ INOUT_PORT(reset_reg, 0xCF9, IOPORT_F_INOUT, reset_handler); static int sci_active; static void -sci_assert(struct vmctx *ctx) +sci_assert(void) { - if (sci_active) return; - vm_isa_assert_irq(ctx, SCI_INT, SCI_INT); + xh_vm_isa_assert_irq(SCI_INT, SCI_INT); sci_active = 1; } static void -sci_deassert(struct vmctx *ctx) +sci_deassert(void) { - if (!sci_active) return; - vm_isa_deassert_irq(ctx, SCI_INT, SCI_INT); + xh_vm_isa_deassert_irq(SCI_INT, SCI_INT); sci_active = 0; } @@ -126,7 +120,7 @@ static uint16_t pm1_enable, pm1_status; #define PM1_RTC_EN 0x0400 static void -sci_update(struct vmctx *ctx) +sci_update(void) { int need_sci; @@ -143,14 +137,14 @@ sci_update(struct vmctx *ctx) if ((pm1_enable & PM1_RTC_EN) && (pm1_status & PM1_RTC_STS)) need_sci = 1; if (need_sci) - sci_assert(ctx); + sci_assert(); else - sci_deassert(ctx); + sci_deassert(); } static int -pm1_status_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, - uint32_t *eax, void *arg) +pm1_status_handler(UNUSED int vcpu, int in, UNUSED int port, int bytes, + uint32_t *eax, UNUSED void *arg) { if (bytes != 2) @@ -166,15 +160,15 @@ pm1_status_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, */ pm1_status &= ~(*eax & (PM1_WAK_STS | PM1_RTC_STS | PM1_SLPBTN_STS | PM1_PWRBTN_STS | PM1_BM_STS)); - sci_update(ctx); + sci_update(); } pthread_mutex_unlock(&pm_lock); return (0); } static int -pm1_enable_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, - uint32_t *eax, void *arg) +pm1_enable_handler(UNUSED int vcpu, int in, UNUSED int port, int bytes, + uint32_t *eax, UNUSED void *arg) { if (bytes != 2) @@ -190,24 +184,22 @@ pm1_enable_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, * can't set GBL_EN. */ pm1_enable = *eax & (PM1_PWRBTN_EN | PM1_GBL_EN); - sci_update(ctx); + sci_update(); } pthread_mutex_unlock(&pm_lock); return (0); } INOUT_PORT(pm1_status, PM1A_EVT_ADDR, IOPORT_F_INOUT, pm1_status_handler); -INOUT_PORT(pm1_enable, PM1A_EVT_ADDR + 2, IOPORT_F_INOUT, pm1_enable_handler); +INOUT_PORT(pm1_enable, PM1A_EVT_ADDR2, IOPORT_F_INOUT, pm1_enable_handler); static void -power_button_handler(int signal, enum ev_type type, void *arg) +power_button_handler(UNUSED int signal, UNUSED enum ev_type type, + UNUSED void *arg) { - struct vmctx *ctx; - - ctx = arg; pthread_mutex_lock(&pm_lock); if (!(pm1_status & PM1_PWRBTN_STS)) { pm1_status |= PM1_PWRBTN_STS; - sci_update(ctx); + sci_update(); } pthread_mutex_unlock(&pm_lock); } @@ -226,8 +218,8 @@ static uint16_t pm1_control; #define PM1_ALWAYS_ZERO 0xc003 static int -pm1_control_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, - uint32_t *eax, void *arg) +pm1_control_handler(UNUSED int vcpu, int in, UNUSED int port, int bytes, + uint32_t *eax, UNUSED void *arg) { int error; @@ -241,8 +233,8 @@ pm1_control_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, * to zero in pm1_control. Always preserve SCI_EN as OSPM * can never change it. */ - pm1_control = (pm1_control & PM1_SCI_EN) | - (*eax & ~(PM1_SLP_EN | PM1_ALWAYS_ZERO)); + pm1_control = (uint16_t) ((pm1_control & PM1_SCI_EN) | + (*eax & ~((unsigned) (PM1_SLP_EN | PM1_ALWAYS_ZERO)))); /* * If SLP_EN is set, check for S5. Bhyve's _S5_ method @@ -250,7 +242,7 @@ pm1_control_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, */ if (*eax & PM1_SLP_EN) { if ((pm1_control & PM1_SLP_TYP) >> 10 == 5) { - error = vm_suspend(ctx, VM_SUSPEND_POWEROFF); + error = xh_vm_suspend(VM_SUSPEND_POWEROFF); assert(error == 0 || errno == EALREADY); } } @@ -266,10 +258,9 @@ SYSRES_IO(PM1A_EVT_ADDR, 8); * This write-only register is used to enable and disable ACPI. */ static int -smi_cmd_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, - uint32_t *eax, void *arg) +smi_cmd_handler(UNUSED int vcpu, int in, UNUSED int port, int bytes, + uint32_t *eax, UNUSED void *arg) { - assert(!in); if (bytes != 1) return (-1); @@ -280,7 +271,7 @@ smi_cmd_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, pm1_control |= PM1_SCI_EN; if (power_button == NULL) { power_button = mevent_add(SIGTERM, EVF_SIGNAL, - power_button_handler, ctx); + power_button_handler, NULL); old_power_handler = signal(SIGTERM, SIG_IGN); } break; @@ -300,13 +291,12 @@ INOUT_PORT(smi_cmd, SMI_CMD, IOPORT_F_OUT, smi_cmd_handler); SYSRES_IO(SMI_CMD, 1); void -sci_init(struct vmctx *ctx) +sci_init(void) { - /* * Mark ACPI's SCI as level trigger and bump its use count * in the PIRQ router. */ pci_irq_use(SCI_INT); - vm_isa_set_irq_trigger(ctx, SCI_INT, LEVEL_TRIGGER); + xh_vm_isa_set_irq_trigger(SCI_INT, LEVEL_TRIGGER); } diff --git a/bhyve/post.c b/src/post.c similarity index 86% rename from bhyve/post.c rename to src/post.c index 5215a0c..75bbc54 100644 --- a/bhyve/post.c +++ b/src/post.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,19 +27,15 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include - +#include #include - -#include "inout.h" -#include "pci_lpc.h" +#include +#include +#include static int -post_data_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, - uint32_t *eax, void *arg) +post_data_handler(UNUSED int vcpu, int in, UNUSED int port, int bytes, + uint32_t *eax, UNUSED void *arg) { assert(in == 1); diff --git a/bhyve/rtc.c b/src/rtc.c similarity index 73% rename from bhyve/rtc.c rename to src/rtc.c index 5c70154..a71fdef 100644 --- a/bhyve/rtc.c +++ b/src/rtc.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,38 +27,29 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include - #include #include +#include +#include +#include +#include -#include -#include +#define IO_RTC 0x70 -#include "acpi.h" -#include "pci_lpc.h" -#include "rtc.h" +#define RTC_LMEM_LSB 0x34 +#define RTC_LMEM_MSB 0x35 +#define RTC_HMEM_LSB 0x5b +#define RTC_HMEM_SB 0x5c +#define RTC_HMEM_MSB 0x5d -#define IO_RTC 0x70 - -#define RTC_LMEM_LSB 0x34 -#define RTC_LMEM_MSB 0x35 -#define RTC_HMEM_LSB 0x5b -#define RTC_HMEM_SB 0x5c -#define RTC_HMEM_MSB 0x5d - -#define m_64KB (64*1024) -#define m_16MB (16*1024*1024) -#define m_4GB (4ULL*1024*1024*1024) +#define m_64KB (64*1024) +#define m_16MB (16*1024*1024) /* * Returns the current RTC time as number of seconds since 00:00:00 Jan 1, 1970 */ static time_t -rtc_time(struct vmctx *ctx, int use_localtime) +rtc_time(int use_localtime) { struct tm tm; time_t t; @@ -71,7 +63,7 @@ rtc_time(struct vmctx *ctx, int use_localtime) } void -rtc_init(struct vmctx *ctx, int use_localtime) +rtc_init(int use_localtime) { size_t himem; size_t lomem; @@ -85,28 +77,27 @@ rtc_init(struct vmctx *ctx, int use_localtime) * 0x34/0x35 - 64KB chunks above 16MB, below 4GB * 0x5b/0x5c/0x5d - 64KB chunks above 4GB */ - lomem = (vm_get_lowmem_size(ctx) - m_16MB) / m_64KB; - err = vm_rtc_write(ctx, RTC_LMEM_LSB, lomem); + lomem = (xh_vm_get_lowmem_size() - m_16MB) / m_64KB; + err = xh_vm_rtc_write(RTC_LMEM_LSB, ((uint8_t) lomem)); assert(err == 0); - err = vm_rtc_write(ctx, RTC_LMEM_MSB, lomem >> 8); + err = xh_vm_rtc_write(RTC_LMEM_MSB, ((uint8_t) (lomem >> 8))); assert(err == 0); - himem = vm_get_highmem_size(ctx) / m_64KB; - err = vm_rtc_write(ctx, RTC_HMEM_LSB, himem); + himem = xh_vm_get_highmem_size() / m_64KB; + err = xh_vm_rtc_write(RTC_HMEM_LSB, ((uint8_t) himem)); assert(err == 0); - err = vm_rtc_write(ctx, RTC_HMEM_SB, himem >> 8); + err = xh_vm_rtc_write(RTC_HMEM_SB, ((uint8_t) (himem >> 8))); assert(err == 0); - err = vm_rtc_write(ctx, RTC_HMEM_MSB, himem >> 16); + err = xh_vm_rtc_write(RTC_HMEM_MSB, ((uint8_t) (himem >> 16))); assert(err == 0); - err = vm_rtc_settime(ctx, rtc_time(ctx, use_localtime)); + err = xh_vm_rtc_settime(rtc_time(use_localtime)); assert(err == 0); } static void rtc_dsdt(void) { - dsdt_line(""); dsdt_line("Device (RTC)"); dsdt_line("{"); diff --git a/bhyve/smbiostbl.c b/src/smbiostbl.c similarity index 82% rename from bhyve/smbiostbl.c rename to src/smbiostbl.c index 59a1358..19c1caa 100644 --- a/bhyve/smbiostbl.c +++ b/src/smbiostbl.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2014 Tycho Nightingale + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -24,42 +25,39 @@ * SUCH DAMAGE. */ -#include -__FBSDID("$FreeBSD$"); - -#include - +#include #include #include -#include #include #include #include -#include +#include +#include +#include +#include +#include +#include +#include -#include -#include +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpacked" -#include "bhyverun.h" -#include "smbiostbl.h" +#define GB (1024ULL*1024*1024) -#define MB (1024*1024) -#define GB (1024ULL*1024*1024) - -#define SMBIOS_BASE 0xF1000 +#define SMBIOS_BASE 0xF1000 /* BHYVE_ACPI_BASE - SMBIOS_BASE) */ -#define SMBIOS_MAX_LENGTH (0xF2400 - 0xF1000) +#define SMBIOS_MAX_LENGTH (0xF2400 - 0xF1000) -#define SMBIOS_TYPE_BIOS 0 -#define SMBIOS_TYPE_SYSTEM 1 -#define SMBIOS_TYPE_CHASSIS 3 -#define SMBIOS_TYPE_PROCESSOR 4 -#define SMBIOS_TYPE_MEMARRAY 16 -#define SMBIOS_TYPE_MEMDEVICE 17 -#define SMBIOS_TYPE_MEMARRAYMAP 19 -#define SMBIOS_TYPE_BOOT 32 -#define SMBIOS_TYPE_EOT 127 +#define SMBIOS_TYPE_BIOS 0 +#define SMBIOS_TYPE_SYSTEM 1 +#define SMBIOS_TYPE_CHASSIS 3 +#define SMBIOS_TYPE_PROCESSOR 4 +#define SMBIOS_TYPE_MEMARRAY 16 +#define SMBIOS_TYPE_MEMDEVICE 17 +#define SMBIOS_TYPE_MEMARRAYMAP 19 +#define SMBIOS_TYPE_BOOT 32 +#define SMBIOS_TYPE_EOT 127 struct smbios_structure { uint8_t type; @@ -109,7 +107,7 @@ struct smbios_entry_point { #define SMBIOS_FL_PCI 0x00000080 /* PCI is supported */ #define SMBIOS_FL_SHADOW 0x00001000 /* BIOS shadowing is allowed */ #define SMBIOS_FL_CDBOOT 0x00008000 /* Boot from CD is supported */ -#define SMBIOS_FL_SELBOOT 0x00010000 /* Selectable Boot supported */ +// #define SMBIOS_FL_SELBOOT 0x00010000 /* Selectable Boot supported */ #define SMBIOS_FL_EDD 0x00080000 /* EDD Spec is supported */ #define SMBIOS_XB1_FL_ACPI 0x00000001 /* ACPI is supported */ @@ -303,7 +301,9 @@ struct smbios_table_type127 { struct smbios_structure header; } __packed; -struct smbios_table_type0 smbios_type0_template = { +#pragma clang diagnostic pop + +static struct smbios_table_type0 smbios_type0_template = { { SMBIOS_TYPE_BIOS, sizeof (struct smbios_table_type0), 0 }, 1, /* bios vendor string */ 2, /* bios version string */ @@ -319,14 +319,14 @@ struct smbios_table_type0 smbios_type0_template = { 0xff /* embedded controller firmware minor release */ }; -const char *smbios_type0_strings[] = { +static const char *smbios_type0_strings[] = { "BHYVE", /* vendor string */ "1.00", /* bios version string */ "03/14/2014", /* bios release date string */ NULL }; -struct smbios_table_type1 smbios_type1_template = { +static struct smbios_table_type1 smbios_type1_template = { { SMBIOS_TYPE_SYSTEM, sizeof (struct smbios_table_type1), 0 }, 1, /* manufacturer string */ 2, /* product string */ @@ -342,7 +342,7 @@ static int smbios_type1_initializer(struct smbios_structure *template_entry, const char **template_strings, char *curaddr, char **endaddr, uint16_t *n, uint16_t *size); -const char *smbios_type1_strings[] = { +static const char *smbios_type1_strings[] = { " ", /* manufacturer string */ "BHYVE", /* product name string */ "1.0", /* version string */ @@ -352,7 +352,7 @@ const char *smbios_type1_strings[] = { NULL }; -struct smbios_table_type3 smbios_type3_template = { +static struct smbios_table_type3 smbios_type3_template = { { SMBIOS_TYPE_CHASSIS, sizeof (struct smbios_table_type3), 0 }, 1, /* manufacturer string */ SMBIOS_CHT_UNKNOWN, @@ -370,7 +370,7 @@ struct smbios_table_type3 smbios_type3_template = { 5 /* sku number string */ }; -const char *smbios_type3_strings[] = { +static const char *smbios_type3_strings[] = { " ", /* manufacturer string */ "1.0", /* version string */ "None", /* serial number string */ @@ -379,34 +379,34 @@ const char *smbios_type3_strings[] = { NULL }; -struct smbios_table_type4 smbios_type4_template = { +static struct smbios_table_type4 smbios_type4_template = { { SMBIOS_TYPE_PROCESSOR, sizeof (struct smbios_table_type4), 0 }, 1, /* socket designation string */ SMBIOS_PRT_CENTRAL, SMBIOS_PRF_OTHER, - 2, /* manufacturer string */ - 0, /* cpuid */ - 3, /* version string */ - 0, /* voltage */ - 0, /* external clock frequency in mhz (0=unknown) */ - 0, /* maximum frequency in mhz (0=unknown) */ - 0, /* current frequency in mhz (0=unknown) */ + 2, /* manufacturer string */ + 0, /* cpuid */ + 3, /* version string */ + 0, /* voltage */ + 0, /* external clock frequency in mhz (0=unknown) */ + 0, /* maximum frequency in mhz (0=unknown) */ + 0, /* current frequency in mhz (0=unknown) */ SMBIOS_PRS_PRESENT | SMBIOS_PRS_ENABLED, SMBIOS_PRU_NONE, - -1, /* l1 cache handle */ - -1, /* l2 cache handle */ - -1, /* l3 cache handle */ - 4, /* serial number string */ - 5, /* asset tag string */ - 6, /* part number string */ - 0, /* cores per socket (0=unknown) */ - 0, /* enabled cores per socket (0=unknown) */ - 0, /* threads per socket (0=unknown) */ + (uint16_t) (-1), /* l1 cache handle */ + (uint16_t) (-1), /* l2 cache handle */ + (uint16_t) (-1), /* l3 cache handle */ + 4, /* serial number string */ + 5, /* asset tag string */ + 6, /* part number string */ + 0, /* cores per socket (0=unknown) */ + 0, /* enabled cores per socket (0=unknown) */ + 0, /* threads per socket (0=unknown) */ SMBIOS_PFL_64B, SMBIOS_PRF_OTHER }; -const char *smbios_type4_strings[] = { +static const char *smbios_type4_strings[] = { " ", /* socket designation string */ " ", /* manufacturer string */ " ", /* version string */ @@ -420,48 +420,48 @@ static int smbios_type4_initializer(struct smbios_structure *template_entry, const char **template_strings, char *curaddr, char **endaddr, uint16_t *n, uint16_t *size); -struct smbios_table_type16 smbios_type16_template = { +static struct smbios_table_type16 smbios_type16_template = { { SMBIOS_TYPE_MEMARRAY, sizeof (struct smbios_table_type16), 0 }, SMBIOS_MAL_SYSMB, SMBIOS_MAU_SYSTEM, SMBIOS_MAE_NONE, - 0x80000000, /* max mem capacity in kb (0x80000000=use extended) */ - -1, /* handle of error (if any) */ - 0, /* number of slots or sockets (TBD) */ - 0 /* extended maximum memory capacity in bytes (TBD) */ + 0x80000000, /* max mem capacity in kb (0x80000000=use extended) */ + (uint16_t) (-1), /* handle of error (if any) */ + 0, /* number of slots or sockets (TBD) */ + 0 /* extended maximum memory capacity in bytes (TBD) */ }; static int smbios_type16_initializer(struct smbios_structure *template_entry, const char **template_strings, char *curaddr, char **endaddr, uint16_t *n, uint16_t *size); -struct smbios_table_type17 smbios_type17_template = { +static struct smbios_table_type17 smbios_type17_template = { { SMBIOS_TYPE_MEMDEVICE, sizeof (struct smbios_table_type17), 0 }, - -1, /* handle of physical memory array */ - -1, /* handle of memory error data */ - 64, /* total width in bits including ecc */ - 64, /* data width in bits */ - 0x7fff, /* size in bytes (0x7fff=use extended)*/ + (uint16_t) (-1), /* handle of physical memory array */ + (uint16_t) (-1), /* handle of memory error data */ + 64, /* total width in bits including ecc */ + 64, /* data width in bits */ + 0x7fff, /* size in bytes (0x7fff=use extended)*/ SMBIOS_MDFF_UNKNOWN, - 0, /* set (0x00=none, 0xff=unknown) */ - 1, /* device locator string */ - 2, /* physical bank locator string */ + 0, /* set (0x00=none, 0xff=unknown) */ + 1, /* device locator string */ + 2, /* physical bank locator string */ SMBIOS_MDT_UNKNOWN, SMBIOS_MDF_UNKNOWN, - 0, /* maximum memory speed in mhz (0=unknown) */ - 3, /* manufacturer string */ - 4, /* serial number string */ - 5, /* asset tag string */ - 6, /* part number string */ - 0, /* attributes (0=unknown rank information) */ - 0, /* extended size in mb (TBD) */ - 0, /* current speed in mhz (0=unknown) */ - 0, /* minimum voltage in mv (0=unknown) */ - 0, /* maximum voltage in mv (0=unknown) */ - 0 /* configured voltage in mv (0=unknown) */ + 0, /* maximum memory speed in mhz (0=unknown) */ + 3, /* manufacturer string */ + 4, /* serial number string */ + 5, /* asset tag string */ + 6, /* part number string */ + 0, /* attributes (0=unknown rank information) */ + 0, /* extended size in mb (TBD) */ + 0, /* current speed in mhz (0=unknown) */ + 0, /* minimum voltage in mv (0=unknown) */ + 0, /* maximum voltage in mv (0=unknown) */ + 0 /* configured voltage in mv (0=unknown) */ }; -const char *smbios_type17_strings[] = { +static const char *smbios_type17_strings[] = { " ", /* device locator string */ " ", /* physical bank locator string */ " ", /* manufacturer string */ @@ -475,27 +475,27 @@ static int smbios_type17_initializer(struct smbios_structure *template_entry, const char **template_strings, char *curaddr, char **endaddr, uint16_t *n, uint16_t *size); -struct smbios_table_type19 smbios_type19_template = { +static struct smbios_table_type19 smbios_type19_template = { { SMBIOS_TYPE_MEMARRAYMAP, sizeof (struct smbios_table_type19), 0 }, - 0xffffffff, /* starting phys addr in kb (0xffffffff=use ext) */ - 0xffffffff, /* ending phys addr in kb (0xffffffff=use ext) */ - -1, /* physical memory array handle */ - 1, /* number of devices that form a row */ - 0, /* extended starting phys addr in bytes (TDB) */ - 0 /* extended ending phys addr in bytes (TDB) */ + 0xffffffff, /* starting phys addr in kb (0xffffffff=use ext) */ + 0xffffffff, /* ending phys addr in kb (0xffffffff=use ext) */ + (uint16_t) (-1), /* physical memory array handle */ + 1, /* number of devices that form a row */ + 0, /* extended starting phys addr in bytes (TDB) */ + 0 /* extended ending phys addr in bytes (TDB) */ }; static int smbios_type19_initializer(struct smbios_structure *template_entry, const char **template_strings, char *curaddr, char **endaddr, uint16_t *n, uint16_t *size); -struct smbios_table_type32 smbios_type32_template = { +static struct smbios_table_type32 smbios_type32_template = { { SMBIOS_TYPE_BOOT, sizeof (struct smbios_table_type32), 0 }, { 0, 0, 0, 0, 0, 0 }, SMBIOS_BOOT_NORMAL }; -struct smbios_table_type127 smbios_type127_template = { +static struct smbios_table_type127 smbios_type127_template = { { SMBIOS_TYPE_EOT, sizeof (struct smbios_table_type127), 0 } }; @@ -539,8 +539,8 @@ static uint16_t type16_handle; static int smbios_generic_initializer(struct smbios_structure *template_entry, - const char **template_strings, char *curaddr, char **endaddr, - uint16_t *n, uint16_t *size) + const char **template_strings, char *curaddr, char **endaddr, uint16_t *n, + UNUSED uint16_t *size) { struct smbios_structure *entry; @@ -556,7 +556,7 @@ smbios_generic_initializer(struct smbios_structure *template_entry, int len; string = template_strings[i]; - len = strlen(string) + 1; + len = (int) (strlen(string) + 1); memcpy(curaddr, string, len); curaddr += len; } @@ -609,8 +609,8 @@ smbios_type1_initializer(struct smbios_structure *template_entry, return (-1); MD5Init(&mdctx); - MD5Update(&mdctx, vmname, strlen(vmname)); - MD5Update(&mdctx, hostname, sizeof(hostname)); + MD5Update(&mdctx, vmname, ((unsigned) strlen(vmname))); + MD5Update(&mdctx, hostname, ((unsigned) sizeof(hostname))); MD5Final(digest, &mdctx); /* @@ -652,7 +652,7 @@ smbios_type4_initializer(struct smbios_structure *template_entry, *endaddr += len - 1; *(*endaddr) = '\0'; (*endaddr)++; - type4->socket = nstrings + 1; + type4->socket = (uint8_t) (nstrings + 1); curaddr = *endaddr; } @@ -687,7 +687,7 @@ smbios_type17_initializer(struct smbios_structure *template_entry, curaddr, endaddr, n, size); type17 = (struct smbios_table_type17 *)curaddr; type17->arrayhand = type16_handle; - type17->xsize = guest_lomem; + type17->xsize = (uint32_t) guest_lomem; if (guest_himem > 0) { curaddr = *endaddr; @@ -695,7 +695,7 @@ smbios_type17_initializer(struct smbios_structure *template_entry, curaddr, endaddr, n, size); type17 = (struct smbios_table_type17 *)curaddr; type17->arrayhand = type16_handle; - type17->xsize = guest_himem; + type17->xsize = (uint32_t) guest_himem; } return (0); @@ -770,19 +770,19 @@ smbios_ep_finalizer(struct smbios_entry_point *smbios_ep, uint16_t len, } int -smbios_build(struct vmctx *ctx) +smbios_build(void) { - struct smbios_entry_point *smbios_ep; - uint16_t n; - uint16_t maxssize; - char *curaddr, *startaddr, *ststartaddr; - int i; - int err; + struct smbios_entry_point *smbios_ep; + uint16_t n; + uint16_t maxssize; + char *curaddr, *startaddr, *ststartaddr; + int i; + int err; - guest_lomem = vm_get_lowmem_size(ctx); - guest_himem = vm_get_highmem_size(ctx); + guest_lomem = xh_vm_get_lowmem_size(); + guest_himem = xh_vm_get_highmem_size(); - startaddr = paddr_guest2host(ctx, SMBIOS_BASE, SMBIOS_MAX_LENGTH); + startaddr = paddr_guest2host(SMBIOS_BASE, SMBIOS_MAX_LENGTH); if (startaddr == NULL) { fprintf(stderr, "smbios table requires mapped mem\n"); return (ENOMEM); @@ -800,10 +800,10 @@ smbios_build(struct vmctx *ctx) maxssize = 0; for (i = 0; smbios_template[i].entry != NULL; i++) { struct smbios_structure *entry; - const char **strings; - initializer_func_t initializer; - char *endaddr; - uint16_t size; + const char **strings; + initializer_func_t initializer; + char *endaddr; + uint16_t size; entry = smbios_template[i].entry; strings = smbios_template[i].strings; @@ -821,7 +821,8 @@ smbios_build(struct vmctx *ctx) } assert(curaddr - startaddr < SMBIOS_MAX_LENGTH); - smbios_ep_finalizer(smbios_ep, curaddr - ststartaddr, n, maxssize); + smbios_ep_finalizer(smbios_ep, ((uint16_t) (curaddr - ststartaddr)), n, + maxssize); return (0); } diff --git a/bhyve/task_switch.c b/src/task_switch.c similarity index 66% rename from bhyve/task_switch.c rename to src/task_switch.c index 69dfaae..23b6768 100644 --- a/bhyve/task_switch.c +++ b/src/task_switch.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2014 Neel Natu + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -24,28 +25,20 @@ * SUCH DAMAGE. */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include - -#include -#include -#include -#include -#include - +#include #include #include #include #include #include +#include +#include -#include - -#include "bhyverun.h" +#include +#include +#include +#include +#include /* * Using 'struct i386tss' is tempting but causes myriad sign extension @@ -98,22 +91,22 @@ CTASSERT(sizeof(struct tss32) == 104); #define TSS_BUSY(type) (((type) & 0x2) != 0) static uint64_t -GETREG(struct vmctx *ctx, int vcpu, int reg) +GETREG(int vcpu, int reg) { uint64_t val; int error; - error = vm_get_register(ctx, vcpu, reg, &val); + error = xh_vm_get_register(vcpu, reg, &val); assert(error == 0); return (val); } static void -SETREG(struct vmctx *ctx, int vcpu, int reg, uint64_t val) +SETREG(int vcpu, int reg, uint64_t val) { int error; - error = vm_set_register(ctx, vcpu, reg, val); + error = xh_vm_set_register(vcpu, reg, val); assert(error == 0); } @@ -127,10 +120,10 @@ usd_to_seg_desc(struct user_segment_descriptor *usd) seg_desc.limit = (u_int)(USD_GETLIMIT(usd) << 12) | 0xfff; else seg_desc.limit = (u_int)USD_GETLIMIT(usd); - seg_desc.access = usd->sd_type | usd->sd_dpl << 5 | usd->sd_p << 7; - seg_desc.access |= usd->sd_xx << 12; - seg_desc.access |= usd->sd_def32 << 14; - seg_desc.access |= usd->sd_gran << 15; + seg_desc.access = (uint32_t) (usd->sd_type | (usd->sd_dpl << 5) | (usd->sd_p << 7)); + seg_desc.access |= (uint32_t) (usd->sd_xx << 12); + seg_desc.access |= (uint32_t) (usd->sd_def32 << 14); + seg_desc.access |= (uint32_t) (usd->sd_gran << 15); return (seg_desc); } @@ -149,7 +142,7 @@ usd_to_seg_desc(struct user_segment_descriptor *usd) * Bit 2(GDT/LDT) has the usual interpretation of Table Indicator (TI). */ static void -sel_exception(struct vmctx *ctx, int vcpu, int vector, uint16_t sel, int ext) +sel_exception(int vcpu, int vector, uint16_t sel, int ext) { /* * Bit 2 from the selector is retained as-is in the error code. @@ -163,7 +156,7 @@ sel_exception(struct vmctx *ctx, int vcpu, int vector, uint16_t sel, int ext) sel &= ~0x3; if (ext) sel |= 0x1; - vm_inject_fault(ctx, vcpu, vector, 1, sel); + xh_vm_inject_fault(vcpu, vector, 1, sel); } /* @@ -171,14 +164,14 @@ sel_exception(struct vmctx *ctx, int vcpu, int vector, uint16_t sel, int ext) * and non-zero otherwise. */ static int -desc_table_limit_check(struct vmctx *ctx, int vcpu, uint16_t sel) +desc_table_limit_check(int vcpu, uint16_t sel) { uint64_t base; uint32_t limit, access; int error, reg; reg = ISLDT(sel) ? VM_REG_GUEST_LDTR : VM_REG_GUEST_GDTR; - error = vm_get_desc(ctx, vcpu, reg, &base, &limit, &access); + error = xh_vm_get_desc(vcpu, reg, &base, &limit, &access); assert(error == 0); if (reg == VM_REG_GUEST_LDTR) { @@ -201,7 +194,7 @@ desc_table_limit_check(struct vmctx *ctx, int vcpu, uint16_t sel) * Returns -1 otherwise. */ static int -desc_table_rw(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging, +desc_table_rw(int vcpu, struct vm_guest_paging *paging, uint16_t sel, struct user_segment_descriptor *desc, bool doread, int *faultptr) { @@ -211,35 +204,35 @@ desc_table_rw(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging, int error, reg; reg = ISLDT(sel) ? VM_REG_GUEST_LDTR : VM_REG_GUEST_GDTR; - error = vm_get_desc(ctx, vcpu, reg, &base, &limit, &access); + error = xh_vm_get_desc(vcpu, reg, &base, &limit, &access); assert(error == 0); assert(limit >= SEL_LIMIT(sel)); - error = vm_copy_setup(ctx, vcpu, paging, base + SEL_START(sel), - sizeof(*desc), doread ? PROT_READ : PROT_WRITE, iov, nitems(iov), + error = xh_vm_copy_setup(vcpu, paging, base + SEL_START(sel), + sizeof(*desc), doread ? XHYVE_PROT_READ : XHYVE_PROT_WRITE, iov, nitems(iov), faultptr); if (error || *faultptr) return (error); if (doread) - vm_copyin(ctx, vcpu, iov, desc, sizeof(*desc)); + xh_vm_copyin(iov, desc, sizeof(*desc)); else - vm_copyout(ctx, vcpu, desc, iov, sizeof(*desc)); + xh_vm_copyout(desc, iov, sizeof(*desc)); return (0); } static int -desc_table_read(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging, - uint16_t sel, struct user_segment_descriptor *desc, int *faultptr) +desc_table_read(int vcpu, struct vm_guest_paging *paging, uint16_t sel, + struct user_segment_descriptor *desc, int *faultptr) { - return (desc_table_rw(ctx, vcpu, paging, sel, desc, true, faultptr)); + return (desc_table_rw(vcpu, paging, sel, desc, true, faultptr)); } static int -desc_table_write(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging, - uint16_t sel, struct user_segment_descriptor *desc, int *faultptr) +desc_table_write(int vcpu, struct vm_guest_paging *paging, uint16_t sel, + struct user_segment_descriptor *desc, int *faultptr) { - return (desc_table_rw(ctx, vcpu, paging, sel, desc, false, faultptr)); + return (desc_table_rw(vcpu, paging, sel, desc, false, faultptr)); } /* @@ -250,8 +243,8 @@ desc_table_write(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging, * Returns -1 otherwise. */ static int -read_tss_descriptor(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts, - uint16_t sel, struct user_segment_descriptor *desc, int *faultptr) +read_tss_descriptor(int vcpu, struct vm_task_switch *ts, uint16_t sel, + struct user_segment_descriptor *desc, int *faultptr) { struct vm_guest_paging sup_paging; int error; @@ -260,17 +253,17 @@ read_tss_descriptor(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts, assert(IDXSEL(sel) != 0); /* Fetch the new TSS descriptor */ - if (desc_table_limit_check(ctx, vcpu, sel)) { + if (desc_table_limit_check(vcpu, sel)) { if (ts->reason == TSR_IRET) - sel_exception(ctx, vcpu, IDT_TS, sel, ts->ext); + sel_exception(vcpu, IDT_TS, sel, ts->ext); else - sel_exception(ctx, vcpu, IDT_GP, sel, ts->ext); + sel_exception(vcpu, IDT_GP, sel, ts->ext); return (1); } sup_paging = ts->paging; sup_paging.cpl = 0; /* implicit supervisor mode */ - error = desc_table_read(ctx, vcpu, &sup_paging, sel, desc, faultptr); + error = desc_table_read(vcpu, &sup_paging, sel, desc, faultptr); return (error); } @@ -302,12 +295,13 @@ ldt_desc(int sd_type) return (sd_type == SDT_SYSLDT); } +CTASSERT(sizeof(struct user_segment_descriptor) == 8); /* * Validate the descriptor 'seg_desc' associated with 'segment'. */ static int -validate_seg_desc(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts, - int segment, struct seg_desc *seg_desc, int *faultptr) +validate_seg_desc(int vcpu, struct vm_task_switch *ts, int segment, + struct seg_desc *seg_desc, int *faultptr) { struct vm_guest_paging sup_paging; struct user_segment_descriptor usd; @@ -338,17 +332,17 @@ validate_seg_desc(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts, } /* Get the segment selector */ - sel = GETREG(ctx, vcpu, segment); + sel = (uint16_t) GETREG(vcpu, segment); /* LDT selector must point into the GDT */ if (ldtseg && ISLDT(sel)) { - sel_exception(ctx, vcpu, IDT_TS, sel, ts->ext); + sel_exception(vcpu, IDT_TS, sel, ts->ext); return (1); } /* Descriptor table limit check */ - if (desc_table_limit_check(ctx, vcpu, sel)) { - sel_exception(ctx, vcpu, IDT_TS, sel, ts->ext); + if (desc_table_limit_check(vcpu, sel)) { + sel_exception(vcpu, IDT_TS, sel, ts->ext); return (1); } @@ -356,7 +350,7 @@ validate_seg_desc(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts, if (IDXSEL(sel) == 0) { /* Code and stack segment selectors cannot be NULL */ if (codeseg || stackseg) { - sel_exception(ctx, vcpu, IDT_TS, sel, ts->ext); + sel_exception(vcpu, IDT_TS, sel, ts->ext); return (1); } seg_desc->base = 0; @@ -368,7 +362,7 @@ validate_seg_desc(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts, /* Read the descriptor from the GDT/LDT */ sup_paging = ts->paging; sup_paging.cpl = 0; /* implicit supervisor mode */ - error = desc_table_read(ctx, vcpu, &sup_paging, sel, &usd, faultptr); + error = desc_table_read(vcpu, &sup_paging, sel, &usd, faultptr); if (error || *faultptr) return (error); @@ -377,7 +371,7 @@ validate_seg_desc(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts, (codeseg && !code_desc(usd.sd_type)) || (dataseg && !data_desc(usd.sd_type)) || (stackseg && !stack_desc(usd.sd_type))) { - sel_exception(ctx, vcpu, IDT_TS, sel, ts->ext); + sel_exception(vcpu, IDT_TS, sel, ts->ext); return (1); } @@ -389,17 +383,17 @@ validate_seg_desc(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts, idtvec = IDT_SS; else idtvec = IDT_NP; - sel_exception(ctx, vcpu, idtvec, sel, ts->ext); + sel_exception(vcpu, idtvec, sel, ts->ext); return (1); } - cs = GETREG(ctx, vcpu, VM_REG_GUEST_CS); + cs = (uint16_t) GETREG(vcpu, VM_REG_GUEST_CS); cpl = cs & SEL_RPL_MASK; rpl = sel & SEL_RPL_MASK; dpl = usd.sd_dpl; if (stackseg && (rpl != cpl || dpl != cpl)) { - sel_exception(ctx, vcpu, IDT_TS, sel, ts->ext); + sel_exception(vcpu, IDT_TS, sel, ts->ext); return (1); } @@ -407,7 +401,7 @@ validate_seg_desc(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts, conforming = (usd.sd_type & 0x4) ? true : false; if ((conforming && (cpl < dpl)) || (!conforming && (cpl != dpl))) { - sel_exception(ctx, vcpu, IDT_TS, sel, ts->ext); + sel_exception(vcpu, IDT_TS, sel, ts->ext); return (1); } } @@ -423,7 +417,7 @@ validate_seg_desc(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts, conforming = false; if (!conforming && (rpl > dpl || cpl > dpl)) { - sel_exception(ctx, vcpu, IDT_TS, sel, ts->ext); + sel_exception(vcpu, IDT_TS, sel, ts->ext); return (1); } } @@ -432,44 +426,44 @@ validate_seg_desc(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts, } static void -tss32_save(struct vmctx *ctx, int vcpu, struct vm_task_switch *task_switch, +tss32_save(int vcpu, struct vm_task_switch *task_switch, uint32_t eip, struct tss32 *tss, struct iovec *iov) { /* General purpose registers */ - tss->tss_eax = GETREG(ctx, vcpu, VM_REG_GUEST_RAX); - tss->tss_ecx = GETREG(ctx, vcpu, VM_REG_GUEST_RCX); - tss->tss_edx = GETREG(ctx, vcpu, VM_REG_GUEST_RDX); - tss->tss_ebx = GETREG(ctx, vcpu, VM_REG_GUEST_RBX); - tss->tss_esp = GETREG(ctx, vcpu, VM_REG_GUEST_RSP); - tss->tss_ebp = GETREG(ctx, vcpu, VM_REG_GUEST_RBP); - tss->tss_esi = GETREG(ctx, vcpu, VM_REG_GUEST_RSI); - tss->tss_edi = GETREG(ctx, vcpu, VM_REG_GUEST_RDI); + tss->tss_eax = (uint32_t) GETREG(vcpu, VM_REG_GUEST_RAX); + tss->tss_ecx = (uint32_t) GETREG(vcpu, VM_REG_GUEST_RCX); + tss->tss_edx = (uint32_t) GETREG(vcpu, VM_REG_GUEST_RDX); + tss->tss_ebx = (uint32_t) GETREG(vcpu, VM_REG_GUEST_RBX); + tss->tss_esp = (uint32_t) GETREG(vcpu, VM_REG_GUEST_RSP); + tss->tss_ebp = (uint32_t) GETREG(vcpu, VM_REG_GUEST_RBP); + tss->tss_esi = (uint32_t) GETREG(vcpu, VM_REG_GUEST_RSI); + tss->tss_edi = (uint32_t) GETREG(vcpu, VM_REG_GUEST_RDI); /* Segment selectors */ - tss->tss_es = GETREG(ctx, vcpu, VM_REG_GUEST_ES); - tss->tss_cs = GETREG(ctx, vcpu, VM_REG_GUEST_CS); - tss->tss_ss = GETREG(ctx, vcpu, VM_REG_GUEST_SS); - tss->tss_ds = GETREG(ctx, vcpu, VM_REG_GUEST_DS); - tss->tss_fs = GETREG(ctx, vcpu, VM_REG_GUEST_FS); - tss->tss_gs = GETREG(ctx, vcpu, VM_REG_GUEST_GS); + tss->tss_es = (uint16_t) GETREG(vcpu, VM_REG_GUEST_ES); + tss->tss_cs = (uint16_t) GETREG(vcpu, VM_REG_GUEST_CS); + tss->tss_ss = (uint16_t) GETREG(vcpu, VM_REG_GUEST_SS); + tss->tss_ds = (uint16_t) GETREG(vcpu, VM_REG_GUEST_DS); + tss->tss_fs = (uint16_t) GETREG(vcpu, VM_REG_GUEST_FS); + tss->tss_gs = (uint16_t) GETREG(vcpu, VM_REG_GUEST_GS); /* eflags and eip */ - tss->tss_eflags = GETREG(ctx, vcpu, VM_REG_GUEST_RFLAGS); + tss->tss_eflags = (uint32_t) GETREG(vcpu, VM_REG_GUEST_RFLAGS); if (task_switch->reason == TSR_IRET) - tss->tss_eflags &= ~PSL_NT; + tss->tss_eflags &= ~((unsigned) PSL_NT); tss->tss_eip = eip; /* Copy updated old TSS into guest memory */ - vm_copyout(ctx, vcpu, tss, iov, sizeof(struct tss32)); + xh_vm_copyout(tss, iov, sizeof(struct tss32)); } static void -update_seg_desc(struct vmctx *ctx, int vcpu, int reg, struct seg_desc *sd) +update_seg_desc(int vcpu, int reg, struct seg_desc *sd) { int error; - error = vm_set_desc(ctx, vcpu, reg, sd->base, sd->limit, sd->access); + error = xh_vm_set_desc(vcpu, reg, sd->base, sd->limit, sd->access); assert(error == 0); } @@ -477,8 +471,8 @@ update_seg_desc(struct vmctx *ctx, int vcpu, int reg, struct seg_desc *sd) * Update the vcpu registers to reflect the state of the new task. */ static int -tss32_restore(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts, - uint16_t ot_sel, struct tss32 *tss, struct iovec *iov, int *faultptr) +tss32_restore(int vcpu, struct vm_task_switch *ts, uint16_t ot_sel, + struct tss32 *tss, struct iovec *iov, int *faultptr) { struct seg_desc seg_desc, seg_desc2; uint64_t *pdpte, maxphyaddr, reserved; @@ -497,7 +491,7 @@ tss32_restore(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts, eflags |= PSL_NT; /* LDTR */ - SETREG(ctx, vcpu, VM_REG_GUEST_LDTR, tss->tss_ldt); + SETREG(vcpu, VM_REG_GUEST_LDTR, tss->tss_ldt); /* PBDR */ if (ts->paging.paging_mode != PAGING_MODE_FLAT) { @@ -506,7 +500,7 @@ tss32_restore(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts, * XXX Assuming 36-bit MAXPHYADDR. */ maxphyaddr = (1UL << 36) - 1; - pdpte = paddr_guest2host(ctx, tss->tss_cr3 & ~0x1f, 32); + pdpte = paddr_guest2host(tss->tss_cr3 & ~((unsigned) 0x1f), 32); for (i = 0; i < 4; i++) { /* Check reserved bits if the PDPTE is valid */ if (!(pdpte[i] & 0x1)) @@ -517,54 +511,54 @@ tss32_restore(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts, */ reserved = ~maxphyaddr | 0x1E6; if (pdpte[i] & reserved) { - vm_inject_gp(ctx, vcpu); + vm_inject_gp(vcpu); return (1); } } - SETREG(ctx, vcpu, VM_REG_GUEST_PDPTE0, pdpte[0]); - SETREG(ctx, vcpu, VM_REG_GUEST_PDPTE1, pdpte[1]); - SETREG(ctx, vcpu, VM_REG_GUEST_PDPTE2, pdpte[2]); - SETREG(ctx, vcpu, VM_REG_GUEST_PDPTE3, pdpte[3]); + SETREG(vcpu, VM_REG_GUEST_PDPTE0, pdpte[0]); + SETREG(vcpu, VM_REG_GUEST_PDPTE1, pdpte[1]); + SETREG(vcpu, VM_REG_GUEST_PDPTE2, pdpte[2]); + SETREG(vcpu, VM_REG_GUEST_PDPTE3, pdpte[3]); } - SETREG(ctx, vcpu, VM_REG_GUEST_CR3, tss->tss_cr3); + SETREG(vcpu, VM_REG_GUEST_CR3, tss->tss_cr3); ts->paging.cr3 = tss->tss_cr3; } /* eflags and eip */ - SETREG(ctx, vcpu, VM_REG_GUEST_RFLAGS, eflags); - SETREG(ctx, vcpu, VM_REG_GUEST_RIP, tss->tss_eip); + SETREG(vcpu, VM_REG_GUEST_RFLAGS, eflags); + SETREG(vcpu, VM_REG_GUEST_RIP, tss->tss_eip); /* General purpose registers */ - SETREG(ctx, vcpu, VM_REG_GUEST_RAX, tss->tss_eax); - SETREG(ctx, vcpu, VM_REG_GUEST_RCX, tss->tss_ecx); - SETREG(ctx, vcpu, VM_REG_GUEST_RDX, tss->tss_edx); - SETREG(ctx, vcpu, VM_REG_GUEST_RBX, tss->tss_ebx); - SETREG(ctx, vcpu, VM_REG_GUEST_RSP, tss->tss_esp); - SETREG(ctx, vcpu, VM_REG_GUEST_RBP, tss->tss_ebp); - SETREG(ctx, vcpu, VM_REG_GUEST_RSI, tss->tss_esi); - SETREG(ctx, vcpu, VM_REG_GUEST_RDI, tss->tss_edi); + SETREG(vcpu, VM_REG_GUEST_RAX, tss->tss_eax); + SETREG(vcpu, VM_REG_GUEST_RCX, tss->tss_ecx); + SETREG(vcpu, VM_REG_GUEST_RDX, tss->tss_edx); + SETREG(vcpu, VM_REG_GUEST_RBX, tss->tss_ebx); + SETREG(vcpu, VM_REG_GUEST_RSP, tss->tss_esp); + SETREG(vcpu, VM_REG_GUEST_RBP, tss->tss_ebp); + SETREG(vcpu, VM_REG_GUEST_RSI, tss->tss_esi); + SETREG(vcpu, VM_REG_GUEST_RDI, tss->tss_edi); /* Segment selectors */ - SETREG(ctx, vcpu, VM_REG_GUEST_ES, tss->tss_es); - SETREG(ctx, vcpu, VM_REG_GUEST_CS, tss->tss_cs); - SETREG(ctx, vcpu, VM_REG_GUEST_SS, tss->tss_ss); - SETREG(ctx, vcpu, VM_REG_GUEST_DS, tss->tss_ds); - SETREG(ctx, vcpu, VM_REG_GUEST_FS, tss->tss_fs); - SETREG(ctx, vcpu, VM_REG_GUEST_GS, tss->tss_gs); + SETREG(vcpu, VM_REG_GUEST_ES, tss->tss_es); + SETREG(vcpu, VM_REG_GUEST_CS, tss->tss_cs); + SETREG(vcpu, VM_REG_GUEST_SS, tss->tss_ss); + SETREG(vcpu, VM_REG_GUEST_DS, tss->tss_ds); + SETREG(vcpu, VM_REG_GUEST_FS, tss->tss_fs); + SETREG(vcpu, VM_REG_GUEST_GS, tss->tss_gs); /* * If this is a nested task then write out the new TSS to update * the previous link field. */ if (nested) - vm_copyout(ctx, vcpu, tss, iov, sizeof(*tss)); + xh_vm_copyout(tss, iov, sizeof(*tss)); /* Validate segment descriptors */ - error = validate_seg_desc(ctx, vcpu, ts, VM_REG_GUEST_LDTR, &seg_desc, + error = validate_seg_desc(vcpu, ts, VM_REG_GUEST_LDTR, &seg_desc, faultptr); if (error || *faultptr) return (error); - update_seg_desc(ctx, vcpu, VM_REG_GUEST_LDTR, &seg_desc); + update_seg_desc(vcpu, VM_REG_GUEST_LDTR, &seg_desc); /* * Section "Checks on Guest Segment Registers", Intel SDM, Vol 3. @@ -575,42 +569,42 @@ tss32_restore(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts, * VM-entry checks so the guest can handle any exception injected * during task switch emulation. */ - error = validate_seg_desc(ctx, vcpu, ts, VM_REG_GUEST_CS, &seg_desc, + error = validate_seg_desc(vcpu, ts, VM_REG_GUEST_CS, &seg_desc, faultptr); if (error || *faultptr) return (error); - error = validate_seg_desc(ctx, vcpu, ts, VM_REG_GUEST_SS, &seg_desc2, + error = validate_seg_desc(vcpu, ts, VM_REG_GUEST_SS, &seg_desc2, faultptr); if (error || *faultptr) return (error); - update_seg_desc(ctx, vcpu, VM_REG_GUEST_CS, &seg_desc); - update_seg_desc(ctx, vcpu, VM_REG_GUEST_SS, &seg_desc2); + update_seg_desc(vcpu, VM_REG_GUEST_CS, &seg_desc); + update_seg_desc(vcpu, VM_REG_GUEST_SS, &seg_desc2); ts->paging.cpl = tss->tss_cs & SEL_RPL_MASK; - error = validate_seg_desc(ctx, vcpu, ts, VM_REG_GUEST_DS, &seg_desc, + error = validate_seg_desc(vcpu, ts, VM_REG_GUEST_DS, &seg_desc, faultptr); if (error || *faultptr) return (error); - update_seg_desc(ctx, vcpu, VM_REG_GUEST_DS, &seg_desc); + update_seg_desc(vcpu, VM_REG_GUEST_DS, &seg_desc); - error = validate_seg_desc(ctx, vcpu, ts, VM_REG_GUEST_ES, &seg_desc, + error = validate_seg_desc(vcpu, ts, VM_REG_GUEST_ES, &seg_desc, faultptr); if (error || *faultptr) return (error); - update_seg_desc(ctx, vcpu, VM_REG_GUEST_ES, &seg_desc); + update_seg_desc(vcpu, VM_REG_GUEST_ES, &seg_desc); - error = validate_seg_desc(ctx, vcpu, ts, VM_REG_GUEST_FS, &seg_desc, + error = validate_seg_desc(vcpu, ts, VM_REG_GUEST_FS, &seg_desc, faultptr); if (error || *faultptr) return (error); - update_seg_desc(ctx, vcpu, VM_REG_GUEST_FS, &seg_desc); + update_seg_desc(vcpu, VM_REG_GUEST_FS, &seg_desc); - error = validate_seg_desc(ctx, vcpu, ts, VM_REG_GUEST_GS, &seg_desc, + error = validate_seg_desc(vcpu, ts, VM_REG_GUEST_GS, &seg_desc, faultptr); if (error || *faultptr) return (error); - update_seg_desc(ctx, vcpu, VM_REG_GUEST_GS, &seg_desc); + update_seg_desc(vcpu, VM_REG_GUEST_GS, &seg_desc); return (0); } @@ -621,8 +615,8 @@ tss32_restore(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts, * code to be saved (e.g. #PF). */ static int -push_errcode(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging, - int task_type, uint32_t errcode, int *faultptr) +push_errcode(int vcpu, struct vm_guest_paging *paging, int task_type, + uint32_t errcode, int *faultptr) { struct iovec iov[2]; struct seg_desc seg_desc; @@ -633,11 +627,11 @@ push_errcode(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging, *faultptr = 0; - cr0 = GETREG(ctx, vcpu, VM_REG_GUEST_CR0); - rflags = GETREG(ctx, vcpu, VM_REG_GUEST_RFLAGS); - stacksel = GETREG(ctx, vcpu, VM_REG_GUEST_SS); + cr0 = GETREG(vcpu, VM_REG_GUEST_CR0); + rflags = GETREG(vcpu, VM_REG_GUEST_RFLAGS); + stacksel = (uint16_t) GETREG(vcpu, VM_REG_GUEST_SS); - error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_SS, &seg_desc.base, + error = xh_vm_get_desc(vcpu, VM_REG_GUEST_SS, &seg_desc.base, &seg_desc.limit, &seg_desc.access); assert(error == 0); @@ -661,29 +655,30 @@ push_errcode(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging, else stacksize = 2; - esp = GETREG(ctx, vcpu, VM_REG_GUEST_RSP); - esp -= bytes; + esp = (uint32_t) GETREG(vcpu, VM_REG_GUEST_RSP); + esp -= (uint32_t) bytes; - if (vie_calculate_gla(paging->cpu_mode, VM_REG_GUEST_SS, - &seg_desc, esp, bytes, stacksize, PROT_WRITE, &gla)) { - sel_exception(ctx, vcpu, IDT_SS, stacksel, 1); + if (vie_calculate_gla(paging->cpu_mode, VM_REG_GUEST_SS, &seg_desc, esp, + bytes, stacksize, XHYVE_PROT_WRITE, &gla)) + { + sel_exception(vcpu, IDT_SS, stacksel, 1); *faultptr = 1; return (0); } if (vie_alignment_check(paging->cpl, bytes, cr0, rflags, gla)) { - vm_inject_ac(ctx, vcpu, 1); + vm_inject_ac(vcpu, 1); *faultptr = 1; return (0); } - error = vm_copy_setup(ctx, vcpu, paging, gla, bytes, PROT_WRITE, - iov, nitems(iov), faultptr); + error = xh_vm_copy_setup(vcpu, paging, gla, ((size_t) bytes), + XHYVE_PROT_WRITE, iov, nitems(iov), faultptr); if (error || *faultptr) return (error); - vm_copyout(ctx, vcpu, &errcode, iov, bytes); - SETREG(ctx, vcpu, VM_REG_GUEST_RSP, esp); + xh_vm_copyout(&errcode, iov, ((size_t) bytes)); + SETREG(vcpu, VM_REG_GUEST_RSP, esp); return (0); } @@ -700,8 +695,10 @@ push_errcode(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging, return (VMEXIT_CONTINUE); \ } while (0) +int vmexit_task_switch(struct vm_exit *vmexit, int *pvcpu); + int -vmexit_task_switch(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) +vmexit_task_switch(struct vm_exit *vmexit, int *pvcpu) { struct seg_desc nt; struct tss32 oldtss, newtss; @@ -727,7 +724,7 @@ vmexit_task_switch(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) /* * Calculate the instruction pointer to store in the old TSS. */ - eip = vmexit->rip + vmexit->inst_length; + eip = (uint32_t) (vmexit->rip + ((uint64_t) vmexit->inst_length)); /* * Section 4.6, "Access Rights" in Intel SDM Vol 3. @@ -739,7 +736,7 @@ vmexit_task_switch(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) sup_paging.cpl = 0; /* implicit supervisor mode */ /* Fetch the new TSS descriptor */ - error = read_tss_descriptor(ctx, vcpu, task_switch, nt_sel, &nt_desc, + error = read_tss_descriptor(vcpu, task_switch, nt_sel, &nt_desc, &fault); CHKERR(error, fault); @@ -749,13 +746,13 @@ vmexit_task_switch(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) nt_type = SEG_DESC_TYPE(nt.access); if (nt_type != SDT_SYS386BSY && nt_type != SDT_SYS386TSS && nt_type != SDT_SYS286BSY && nt_type != SDT_SYS286TSS) { - sel_exception(ctx, vcpu, IDT_TS, nt_sel, ext); + sel_exception(vcpu, IDT_TS, nt_sel, ext); goto done; } /* TSS descriptor must have present bit set */ if (!SEG_DESC_PRESENT(nt.access)) { - sel_exception(ctx, vcpu, IDT_NP, nt_sel, ext); + sel_exception(vcpu, IDT_NP, nt_sel, ext); goto done; } @@ -771,14 +768,14 @@ vmexit_task_switch(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) minlimit = 0; assert(minlimit > 0); - if (nt.limit < minlimit) { - sel_exception(ctx, vcpu, IDT_TS, nt_sel, ext); + if (nt.limit < ((uint32_t) minlimit)) { + sel_exception(vcpu, IDT_TS, nt_sel, ext); goto done; } /* TSS must be busy if task switch is due to IRET */ if (reason == TSR_IRET && !TSS_BUSY(nt_type)) { - sel_exception(ctx, vcpu, IDT_TS, nt_sel, ext); + sel_exception(vcpu, IDT_TS, nt_sel, ext); goto done; } @@ -787,18 +784,19 @@ vmexit_task_switch(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) * CALL, JMP, exception or interrupt. */ if (reason != TSR_IRET && TSS_BUSY(nt_type)) { - sel_exception(ctx, vcpu, IDT_GP, nt_sel, ext); + sel_exception(vcpu, IDT_GP, nt_sel, ext); goto done; } /* Fetch the new TSS */ - error = vm_copy_setup(ctx, vcpu, &sup_paging, nt.base, minlimit + 1, - PROT_READ | PROT_WRITE, nt_iov, nitems(nt_iov), &fault); + error = xh_vm_copy_setup(vcpu, &sup_paging, nt.base, + ((size_t) (minlimit + 1)), (XHYVE_PROT_READ | XHYVE_PROT_WRITE), nt_iov, + nitems(nt_iov), &fault); CHKERR(error, fault); - vm_copyin(ctx, vcpu, nt_iov, &newtss, minlimit + 1); + xh_vm_copyin(nt_iov, &newtss, ((size_t) (minlimit + 1))); /* Get the old TSS selector from the guest's task register */ - ot_sel = GETREG(ctx, vcpu, VM_REG_GUEST_TR); + ot_sel = (uint16_t) GETREG(vcpu, VM_REG_GUEST_TR); if (ISLDT(ot_sel) || IDXSEL(ot_sel) == 0) { /* * This might happen if a task switch was attempted without @@ -806,12 +804,12 @@ vmexit_task_switch(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) * TR would contain the values from power-on: * (sel = 0, base = 0, limit = 0xffff). */ - sel_exception(ctx, vcpu, IDT_TS, ot_sel, task_switch->ext); + sel_exception(vcpu, IDT_TS, ot_sel, task_switch->ext); goto done; } /* Get the old TSS base and limit from the guest's task register */ - error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_TR, &ot_base, &ot_lim, + error = xh_vm_get_desc(vcpu, VM_REG_GUEST_TR, &ot_base, &ot_lim, &access); assert(error == 0); assert(!SEG_DESC_UNUSABLE(access) && SEG_DESC_PRESENT(access)); @@ -819,15 +817,16 @@ vmexit_task_switch(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) assert(ot_type == SDT_SYS386BSY || ot_type == SDT_SYS286BSY); /* Fetch the old TSS descriptor */ - error = read_tss_descriptor(ctx, vcpu, task_switch, ot_sel, &ot_desc, + error = read_tss_descriptor(vcpu, task_switch, ot_sel, &ot_desc, &fault); CHKERR(error, fault); /* Get the old TSS */ - error = vm_copy_setup(ctx, vcpu, &sup_paging, ot_base, minlimit + 1, - PROT_READ | PROT_WRITE, ot_iov, nitems(ot_iov), &fault); + error = xh_vm_copy_setup(vcpu, &sup_paging, ot_base, + ((size_t) (minlimit + 1)), (XHYVE_PROT_READ | XHYVE_PROT_WRITE), + ot_iov, nitems(ot_iov), &fault); CHKERR(error, fault); - vm_copyin(ctx, vcpu, ot_iov, &oldtss, minlimit + 1); + xh_vm_copyin(ot_iov, &oldtss, ((size_t) (minlimit + 1))); /* * Clear the busy bit in the old TSS descriptor if the task switch @@ -835,7 +834,7 @@ vmexit_task_switch(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) */ if (reason == TSR_IRET || reason == TSR_JMP) { ot_desc.sd_type &= ~0x2; - error = desc_table_write(ctx, vcpu, &sup_paging, ot_sel, + error = desc_table_write(vcpu, &sup_paging, ot_sel, &ot_desc, &fault); CHKERR(error, fault); } @@ -846,7 +845,7 @@ vmexit_task_switch(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) } /* Save processor state in old TSS */ - tss32_save(ctx, vcpu, task_switch, eip, &oldtss, ot_iov); + tss32_save(vcpu, task_switch, eip, &oldtss, ot_iov); /* * If the task switch was triggered for any reason other than IRET @@ -854,32 +853,32 @@ vmexit_task_switch(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) */ if (reason != TSR_IRET) { nt_desc.sd_type |= 0x2; - error = desc_table_write(ctx, vcpu, &sup_paging, nt_sel, + error = desc_table_write(vcpu, &sup_paging, nt_sel, &nt_desc, &fault); CHKERR(error, fault); } /* Update task register to point at the new TSS */ - SETREG(ctx, vcpu, VM_REG_GUEST_TR, nt_sel); + SETREG(vcpu, VM_REG_GUEST_TR, nt_sel); /* Update the hidden descriptor state of the task register */ nt = usd_to_seg_desc(&nt_desc); - update_seg_desc(ctx, vcpu, VM_REG_GUEST_TR, &nt); + update_seg_desc(vcpu, VM_REG_GUEST_TR, &nt); /* Set CR0.TS */ - cr0 = GETREG(ctx, vcpu, VM_REG_GUEST_CR0); - SETREG(ctx, vcpu, VM_REG_GUEST_CR0, cr0 | CR0_TS); + cr0 = GETREG(vcpu, VM_REG_GUEST_CR0); + SETREG(vcpu, VM_REG_GUEST_CR0, cr0 | CR0_TS); /* * We are now committed to the task switch. Any exceptions encountered * after this point will be handled in the context of the new task and * the saved instruction pointer will belong to the new task. */ - error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RIP, newtss.tss_eip); + error = xh_vm_set_register(vcpu, VM_REG_GUEST_RIP, newtss.tss_eip); assert(error == 0); /* Load processor state from new TSS */ - error = tss32_restore(ctx, vcpu, task_switch, ot_sel, &newtss, nt_iov, + error = tss32_restore(vcpu, task_switch, ot_sel, &newtss, nt_iov, &fault); CHKERR(error, fault); @@ -891,7 +890,7 @@ vmexit_task_switch(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) if (task_switch->errcode_valid) { assert(task_switch->ext); assert(task_switch->reason == TSR_IDT_GATE); - error = push_errcode(ctx, vcpu, &task_switch->paging, nt_type, + error = push_errcode(vcpu, &task_switch->paging, nt_type, task_switch->errcode, &fault); CHKERR(error, fault); } @@ -927,7 +926,7 @@ vmexit_task_switch(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) * exitintinfo. */ if (task_switch->reason == TSR_IDT_GATE) { - error = vm_set_intinfo(ctx, vcpu, 0); + error = xh_vm_set_intinfo(vcpu, 0); assert(error == 0); } diff --git a/bhyve/uart_emul.c b/src/uart_emul.c similarity index 95% rename from bhyve/uart_emul.c rename to src/uart_emul.c index 4242e5c..73021d0 100644 --- a/bhyve/uart_emul.c +++ b/src/uart_emul.c @@ -1,6 +1,7 @@ /*- * Copyright (c) 2012 NetApp, Inc. * Copyright (c) 2013 Neel Natu + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,24 +28,19 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include - +#include +#include #include #include -#include -#include -#include +#include #include -#include -#include +#include #include - -#include "mevent.h" -#include "uart_emul.h" +#include +#include +#include +#include +#include #define COM1_BASE 0x3F8 #define COM1_IRQ 4 @@ -70,6 +66,8 @@ __FBSDID("$FreeBSD$"); static bool uart_stdio; /* stdio in use for i/o */ static struct termios tio_stdio_orig; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" static struct { int baseaddr; int irq; @@ -119,6 +117,7 @@ struct uart_softc { uart_intr_func_t intr_assert; uart_intr_func_t intr_deassert; }; +#pragma clang diagnostic pop static void uart_drain(int fd, enum ev_type ev, void *arg); @@ -302,8 +301,8 @@ uart_reset(struct uart_softc *sc) uint16_t divisor; divisor = DEFAULT_RCLK / DEFAULT_BAUD / 16; - sc->dll = divisor; - sc->dlh = divisor >> 16; + sc->dll = (uint8_t) divisor; + sc->dlh = (uint8_t) (divisor >> 16); rxfifo_reset(sc, 1); /* no fifo until enabled by software */ } @@ -317,7 +316,7 @@ uart_toggle_intr(struct uart_softc *sc) { uint8_t intr_reason; - intr_reason = uart_intr_reason(sc); + intr_reason = (uint8_t) uart_intr_reason(sc); if (intr_reason == IIR_NOPEND) (*sc->intr_deassert)(sc->arg); @@ -348,7 +347,7 @@ uart_drain(int fd, enum ev_type ev, void *arg) } else { while (rxfifo_available(sc) && ((ch = ttyread(&sc->tty)) != -1)) { - rxfifo_putchar(sc, ch); + rxfifo_putchar(sc, ((uint8_t) ch)); } uart_toggle_intr(sc); } @@ -511,7 +510,7 @@ uart_read(struct uart_softc *sc, int offset) switch (offset) { case REG_DATA: - reg = rxfifo_getchar(sc); + reg = (uint8_t) rxfifo_getchar(sc); break; case REG_IER: reg = sc->ier; @@ -519,7 +518,7 @@ uart_read(struct uart_softc *sc, int offset) case REG_IIR: iir = (sc->fcr & FCR_ENABLE) ? IIR_FIFO_MASK : 0; - intr_reason = uart_intr_reason(sc); + intr_reason = (uint8_t) uart_intr_reason(sc); /* * Deal with side effects of reading the IIR register @@ -578,8 +577,11 @@ int uart_legacy_alloc(int which, int *baseaddr, int *irq) { - if (which < 0 || which >= UART_NLDEVS || uart_lres[which].inuse) + if ((which < 0) || (((unsigned) which) >= UART_NLDEVS) || + uart_lres[which].inuse) + { return (-1); + } uart_lres[which].inuse = true; *baseaddr = uart_lres[which].baseaddr; diff --git a/bhyve/virtio.c b/src/virtio.c similarity index 91% rename from bhyve/virtio.c rename to src/virtio.c index 11b1e62..59ad266 100644 --- a/bhyve/virtio.c +++ b/src/virtio.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2013 Chris Torek + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -24,20 +25,15 @@ * SUCH DAMAGE. */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include - #include #include #include -#include - -#include "bhyverun.h" -#include "pci_emul.h" -#include "virtio.h" +#include +#include +#include +#include +#include +#include /* * Functions for dealing with generalized "virtual devices" as @@ -71,7 +67,7 @@ vi_softc_linkup(struct virtio_softc *vs, struct virtio_consts *vc, vs->vs_queues = queues; for (i = 0; i < vc->vc_nvq; i++) { queues[i].vq_vs = vs; - queues[i].vq_num = i; + queues[i].vq_num = (uint16_t) i; } } @@ -90,9 +86,6 @@ vi_reset_dev(struct virtio_softc *vs) struct vqueue_info *vq; int i, nvq; - if (vs->vs_mtx) - assert(pthread_mutex_isowned_np(vs->vs_mtx)); - nvq = vs->vs_vc->vc_nvq; for (vq = vs->vs_queues, i = 0; i < nvq; vq++, i++) { vq->vq_flags = 0; @@ -163,7 +156,7 @@ vi_intr_init(struct virtio_softc *vs, int barnum, int use_msix) * The guest just gave us a page frame number, from which we can * calculate the addresses of the queue. */ -void +static void vi_vq_init(struct virtio_softc *vs, uint32_t pfn) { struct vqueue_info *vq; @@ -175,7 +168,7 @@ vi_vq_init(struct virtio_softc *vs, uint32_t pfn) vq->vq_pfn = pfn; phys = (uint64_t)pfn << VRING_PFN; size = vring_size(vq->vq_qsize); - base = paddr_guest2host(vs->vs_pi->pi_vmctx, phys, size); + base = paddr_guest2host(phys, size); /* First page(s) are descriptors... */ vq->vq_desc = (struct virtio_desc *)base; @@ -186,7 +179,7 @@ vi_vq_init(struct virtio_softc *vs, uint32_t pfn) base += (2 + vq->vq_qsize + 1) * sizeof(uint16_t); /* Then it's rounded up to the next page... */ - base = (char *)roundup2((uintptr_t)base, VRING_ALIGN); + base = (char *) roundup2(((uintptr_t) base), ((uintptr_t) VRING_ALIGN)); /* ... and the last page(s) are the used ring. */ vq->vq_used = (struct vring_used *)base; @@ -202,12 +195,12 @@ vi_vq_init(struct virtio_softc *vs, uint32_t pfn) * descriptor. */ static inline void -_vq_record(int i, volatile struct virtio_desc *vd, struct vmctx *ctx, - struct iovec *iov, int n_iov, uint16_t *flags) { - +_vq_record(int i, volatile struct virtio_desc *vd, struct iovec *iov, int n_iov, + uint16_t *flags) +{ if (i >= n_iov) return; - iov[i].iov_base = paddr_guest2host(ctx, vd->vd_addr, vd->vd_len); + iov[i].iov_base = paddr_guest2host(vd->vd_addr, vd->vd_len); iov[i].iov_len = vd->vd_len; if (flags != NULL) flags[i] = vd->vd_flags; @@ -254,14 +247,13 @@ _vq_record(int i, volatile struct virtio_desc *vd, struct vmctx *ctx, * that vq_has_descs() does one). */ int -vq_getchain(struct vqueue_info *vq, uint16_t *pidx, - struct iovec *iov, int n_iov, uint16_t *flags) +vq_getchain(struct vqueue_info *vq, uint16_t *pidx, struct iovec *iov, + int n_iov, uint16_t *flags) { int i; u_int ndesc, n_indir; u_int idx, next; volatile struct virtio_desc *vdir, *vindir, *vp; - struct vmctx *ctx; struct virtio_softc *vs; const char *name; @@ -301,7 +293,6 @@ vq_getchain(struct vqueue_info *vq, uint16_t *pidx, * check whether we're re-visiting a previously visited * index, but we just abort if the count gets excessive. */ - ctx = vs->vs_pi->pi_vmctx; *pidx = next = vq->vq_avail->va_ring[idx & (vq->vq_qsize - 1)]; vq->vq_last_avail++; for (i = 0; i < VQ_MAX_DESCRIPTORS; next = vdir->vd_next) { @@ -314,7 +305,7 @@ vq_getchain(struct vqueue_info *vq, uint16_t *pidx, } vdir = &vq->vq_desc[next]; if ((vdir->vd_flags & VRING_DESC_F_INDIRECT) == 0) { - _vq_record(i, vdir, ctx, iov, n_iov, flags); + _vq_record(i, vdir, iov, n_iov, flags); i++; } else if ((vs->vs_vc->vc_hv_caps & VIRTIO_RING_F_INDIRECT_DESC) == 0) { @@ -332,8 +323,7 @@ vq_getchain(struct vqueue_info *vq, uint16_t *pidx, name, (u_int)vdir->vd_len); return (-1); } - vindir = paddr_guest2host(ctx, - vdir->vd_addr, vdir->vd_len); + vindir = paddr_guest2host(vdir->vd_addr, vdir->vd_len); /* * Indirects start at the 0th, then follow * their own embedded "next"s until those run @@ -351,7 +341,7 @@ vq_getchain(struct vqueue_info *vq, uint16_t *pidx, name); return (-1); } - _vq_record(i, vp, ctx, iov, n_iov, flags); + _vq_record(i, vp, iov, n_iov, flags); if (++i > VQ_MAX_DESCRIPTORS) goto loopy; if ((vp->vd_flags & VRING_DESC_F_NEXT) == 0) @@ -476,6 +466,8 @@ vq_endchains(struct vqueue_info *vq, int used_all_avail) vq_interrupt(vs, vq); } +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" /* Note: these are in sorted order to make for a fast search */ static struct config_reg { uint16_t cr_offset; /* register offset */ @@ -494,6 +486,7 @@ static struct config_reg { { VTCFG_R_CFGVEC, 2, 0, "CFGVEC" }, { VTCFG_R_QVEC, 2, 0, "QVEC" }, }; +#pragma clang diagnostic pop static inline struct config_reg * vi_find_cr(int offset) { @@ -522,8 +515,8 @@ vi_find_cr(int offset) { * Otherwise dispatch to the actual driver. */ uint64_t -vi_pci_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, - int baridx, uint64_t offset, int size) +vi_pci_read(UNUSED int vcpu, struct pci_devinst *pi, int baridx, + uint64_t offset, int size) { struct virtio_softc *vs = pi->pi_arg; struct virtio_consts *vc; @@ -565,17 +558,17 @@ vi_pci_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, * registers if enabled) and dispatch to underlying driver. * If that fails, fall into general code. */ - newoff = offset - virtio_config_size; + newoff = (uint32_t) (offset - virtio_config_size); max = vc->vc_cfgsize ? vc->vc_cfgsize : 0x100000000; - if (newoff + size > max) + if ((newoff + ((unsigned) size)) > max) goto bad; - error = (*vc->vc_cfgread)(DEV_SOFTC(vs), newoff, size, &value); + error = (*vc->vc_cfgread)(DEV_SOFTC(vs), ((int) newoff), size, &value); if (!error) goto done; } bad: - cr = vi_find_cr(offset); + cr = vi_find_cr((int) offset); if (cr == NULL || cr->cr_size != size) { if (cr != NULL) { /* offset must be OK, so size must be bad */ @@ -592,7 +585,7 @@ bad: switch (offset) { case VTCFG_R_HOSTCAP: - value = vc->vc_hv_caps; + value = (uint32_t) vc->vc_hv_caps; break; case VTCFG_R_GUESTCAP: value = vs->vs_negotiated_caps; @@ -606,7 +599,7 @@ bad: vs->vs_queues[vs->vs_curq].vq_qsize : 0; break; case VTCFG_R_QSEL: - value = vs->vs_curq; + value = (uint32_t) (vs->vs_curq); break; case VTCFG_R_QNOTIFY: value = 0; /* XXX */ @@ -642,8 +635,8 @@ done: * Otherwise dispatch to the actual driver. */ void -vi_pci_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, - int baridx, uint64_t offset, int size, uint64_t value) +vi_pci_write(UNUSED int vcpu, struct pci_devinst *pi, int baridx, + uint64_t offset, int size, uint64_t value) { struct virtio_softc *vs = pi->pi_arg; struct vqueue_info *vq; @@ -684,17 +677,18 @@ vi_pci_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, * Subtract off the standard size (including MSI-X * registers if enabled) and dispatch to underlying driver. */ - newoff = offset - virtio_config_size; + newoff = (uint32_t) (offset - virtio_config_size); max = vc->vc_cfgsize ? vc->vc_cfgsize : 0x100000000; - if (newoff + size > max) + if ((newoff + ((unsigned) size)) > max) goto bad; - error = (*vc->vc_cfgwrite)(DEV_SOFTC(vs), newoff, size, value); + error = (*vc->vc_cfgwrite)(DEV_SOFTC(vs), ((int) newoff), size, + ((uint32_t) value)); if (!error) goto done; } bad: - cr = vi_find_cr(offset); + cr = vi_find_cr((int) offset); if (cr == NULL || cr->cr_size != size || cr->cr_ro) { if (cr != NULL) { /* offset must be OK, wrong size and/or reg is R/O */ @@ -716,7 +710,7 @@ bad: switch (offset) { case VTCFG_R_GUESTCAP: - vs->vs_negotiated_caps = value & vc->vc_hv_caps; + vs->vs_negotiated_caps = (uint32_t) (value & vc->vc_hv_caps); if (vc->vc_apply_features) (*vc->vc_apply_features)(DEV_SOFTC(vs), vs->vs_negotiated_caps); @@ -724,7 +718,7 @@ bad: case VTCFG_R_PFN: if (vs->vs_curq >= vc->vc_nvq) goto bad_qindex; - vi_vq_init(vs, value); + vi_vq_init(vs, ((uint32_t) value)); break; case VTCFG_R_QSEL: /* @@ -732,10 +726,10 @@ bad: * invalid queue; we just need to return a QNUM * of 0 while the bad queue is selected. */ - vs->vs_curq = value; + vs->vs_curq = (int) value; break; case VTCFG_R_QNOTIFY: - if (value >= vc->vc_nvq) { + if (value >= ((uint64_t) vc->vc_nvq)) { fprintf(stderr, "%s: queue %d notify out of range\r\n", name, (int)value); goto done; @@ -751,18 +745,18 @@ bad: name, (int)value); break; case VTCFG_R_STATUS: - vs->vs_status = value; + vs->vs_status = (uint8_t) value; if (value == 0) (*vc->vc_reset)(DEV_SOFTC(vs)); break; case VTCFG_R_CFGVEC: - vs->vs_msix_cfg_idx = value; + vs->vs_msix_cfg_idx = (uint16_t) value; break; case VTCFG_R_QVEC: if (vs->vs_curq >= vc->vc_nvq) goto bad_qindex; vq = &vs->vs_queues[vs->vs_curq]; - vq->vq_msix_idx = value; + vq->vq_msix_idx = (uint16_t) value; break; } goto done; diff --git a/src/vmm/intel/vmcs.c b/src/vmm/intel/vmcs.c new file mode 100644 index 0000000..250c948 --- /dev/null +++ b/src/vmm/intel/vmcs.c @@ -0,0 +1,245 @@ +/*- + * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#include +#include + +static uint64_t +vmcs_fix_regval(uint32_t encoding, uint64_t val) +{ + + switch (encoding) { + case VMCS_GUEST_CR0: + val = vmx_fix_cr0(val); + break; + case VMCS_GUEST_CR4: + val = vmx_fix_cr4(val); + break; + default: + break; + } + return (val); +} + +static uint32_t +vmcs_field_encoding(int ident) +{ + switch (ident) { + case VM_REG_GUEST_CR0: + return (VMCS_GUEST_CR0); + case VM_REG_GUEST_CR3: + return (VMCS_GUEST_CR3); + case VM_REG_GUEST_CR4: + return (VMCS_GUEST_CR4); + case VM_REG_GUEST_DR7: + return (VMCS_GUEST_DR7); + case VM_REG_GUEST_RSP: + return (VMCS_GUEST_RSP); + case VM_REG_GUEST_RIP: + return (VMCS_GUEST_RIP); + case VM_REG_GUEST_RFLAGS: + return (VMCS_GUEST_RFLAGS); + case VM_REG_GUEST_ES: + return (VMCS_GUEST_ES_SELECTOR); + case VM_REG_GUEST_CS: + return (VMCS_GUEST_CS_SELECTOR); + case VM_REG_GUEST_SS: + return (VMCS_GUEST_SS_SELECTOR); + case VM_REG_GUEST_DS: + return (VMCS_GUEST_DS_SELECTOR); + case VM_REG_GUEST_FS: + return (VMCS_GUEST_FS_SELECTOR); + case VM_REG_GUEST_GS: + return (VMCS_GUEST_GS_SELECTOR); + case VM_REG_GUEST_TR: + return (VMCS_GUEST_TR_SELECTOR); + case VM_REG_GUEST_LDTR: + return (VMCS_GUEST_LDTR_SELECTOR); + case VM_REG_GUEST_EFER: + return (VMCS_GUEST_IA32_EFER); + case VM_REG_GUEST_PDPTE0: + return (VMCS_GUEST_PDPTE0); + case VM_REG_GUEST_PDPTE1: + return (VMCS_GUEST_PDPTE1); + case VM_REG_GUEST_PDPTE2: + return (VMCS_GUEST_PDPTE2); + case VM_REG_GUEST_PDPTE3: + return (VMCS_GUEST_PDPTE3); + default: + return ((uint32_t) -1); + } + +} + +static int +vmcs_seg_desc_encoding(int seg, uint32_t *base, uint32_t *lim, uint32_t *acc) +{ + + switch (seg) { + case VM_REG_GUEST_ES: + *base = VMCS_GUEST_ES_BASE; + *lim = VMCS_GUEST_ES_LIMIT; + *acc = VMCS_GUEST_ES_ACCESS_RIGHTS; + break; + case VM_REG_GUEST_CS: + *base = VMCS_GUEST_CS_BASE; + *lim = VMCS_GUEST_CS_LIMIT; + *acc = VMCS_GUEST_CS_ACCESS_RIGHTS; + break; + case VM_REG_GUEST_SS: + *base = VMCS_GUEST_SS_BASE; + *lim = VMCS_GUEST_SS_LIMIT; + *acc = VMCS_GUEST_SS_ACCESS_RIGHTS; + break; + case VM_REG_GUEST_DS: + *base = VMCS_GUEST_DS_BASE; + *lim = VMCS_GUEST_DS_LIMIT; + *acc = VMCS_GUEST_DS_ACCESS_RIGHTS; + break; + case VM_REG_GUEST_FS: + *base = VMCS_GUEST_FS_BASE; + *lim = VMCS_GUEST_FS_LIMIT; + *acc = VMCS_GUEST_FS_ACCESS_RIGHTS; + break; + case VM_REG_GUEST_GS: + *base = VMCS_GUEST_GS_BASE; + *lim = VMCS_GUEST_GS_LIMIT; + *acc = VMCS_GUEST_GS_ACCESS_RIGHTS; + break; + case VM_REG_GUEST_TR: + *base = VMCS_GUEST_TR_BASE; + *lim = VMCS_GUEST_TR_LIMIT; + *acc = VMCS_GUEST_TR_ACCESS_RIGHTS; + break; + case VM_REG_GUEST_LDTR: + *base = VMCS_GUEST_LDTR_BASE; + *lim = VMCS_GUEST_LDTR_LIMIT; + *acc = VMCS_GUEST_LDTR_ACCESS_RIGHTS; + break; + case VM_REG_GUEST_IDTR: + *base = VMCS_GUEST_IDTR_BASE; + *lim = VMCS_GUEST_IDTR_LIMIT; + *acc = VMCS_INVALID_ENCODING; + break; + case VM_REG_GUEST_GDTR: + *base = VMCS_GUEST_GDTR_BASE; + *lim = VMCS_GUEST_GDTR_LIMIT; + *acc = VMCS_INVALID_ENCODING; + break; + default: + return (EINVAL); + } + + return (0); +} + +int +vmcs_getreg(int vcpuid, int ident, uint64_t *retval) +{ + uint32_t encoding; + + /* + * If we need to get at vmx-specific state in the VMCS we can bypass + * the translation of 'ident' to 'encoding' by simply setting the + * sign bit. As it so happens the upper 16 bits are reserved (i.e + * set to 0) in the encodings for the VMCS so we are free to use the + * sign bit. + */ + if (ident < 0) + encoding = ident & 0x7fffffff; + else + encoding = vmcs_field_encoding(ident); + + if (encoding == (uint32_t)-1) + return (EINVAL); + + *retval = vmcs_read(vcpuid, encoding); + + return (0); +} + +int +vmcs_setreg(int vcpuid, int ident, uint64_t val) +{ + uint32_t encoding; + + if (ident < 0) + encoding = ident & 0x7fffffff; + else + encoding = vmcs_field_encoding(ident); + + if (encoding == (uint32_t)-1) + return (EINVAL); + + val = vmcs_fix_regval(encoding, val); + + vmcs_write(vcpuid, encoding, val); + + return (0); +} + +int +vmcs_setdesc(int vcpuid, int seg, struct seg_desc *desc) +{ + int error; + uint32_t base, limit, access; + + error = vmcs_seg_desc_encoding(seg, &base, &limit, &access); + if (error != 0) + xhyve_abort("vmcs_setdesc: invalid segment register %d\n", seg); + + vmcs_write(vcpuid, base, desc->base); + vmcs_write(vcpuid, limit, desc->limit); + if (access != VMCS_INVALID_ENCODING) { + vmcs_write(vcpuid, access, desc->access); + } + + return (0); +} + +int +vmcs_getdesc(int vcpuid, int seg, struct seg_desc *desc) +{ + int error; + uint32_t base, limit, access; + + error = vmcs_seg_desc_encoding(seg, &base, &limit, &access); + if (error != 0) + xhyve_abort("vmcs_setdesc: invalid segment register %d\n", seg); + + desc->base = vmcs_read(vcpuid, base); + desc->limit = (uint32_t) vmcs_read(vcpuid, limit); + if (access != VMCS_INVALID_ENCODING) { + desc->access = (uint32_t) vmcs_read(vcpuid, access); + } + + return (0); +} diff --git a/src/vmm/intel/vmx.c b/src/vmm/intel/vmx.c new file mode 100644 index 0000000..6c94228 --- /dev/null +++ b/src/vmm/intel/vmx.c @@ -0,0 +1,2746 @@ +/*- + * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PROCBASED_CTLS_WINDOW_SETTING \ + (PROCBASED_INT_WINDOW_EXITING | \ + PROCBASED_NMI_WINDOW_EXITING) +#define PROCBASED_CTLS_ONE_SETTING \ + (PROCBASED_SECONDARY_CONTROLS | \ + PROCBASED_MWAIT_EXITING | \ + PROCBASED_MONITOR_EXITING | \ + PROCBASED_IO_EXITING | \ + PROCBASED_MSR_BITMAPS | \ + PROCBASED_CTLS_WINDOW_SETTING | \ + PROCBASED_CR8_LOAD_EXITING | \ + PROCBASED_CR8_STORE_EXITING | \ + PROCBASED_HLT_EXITING | \ + PROCBASED_TSC_OFFSET) +#define PROCBASED_CTLS_ZERO_SETTING \ + (PROCBASED_CR3_LOAD_EXITING | \ + PROCBASED_CR3_STORE_EXITING | \ + PROCBASED_IO_BITMAPS | \ + PROCBASED_RDTSC_EXITING | \ + PROCBASED_USE_TPR_SHADOW | \ + PROCBASED_MOV_DR_EXITING | \ + PROCBASED_MTF | \ + PROCBASED_INVLPG_EXITING | \ + PROCBASED_PAUSE_EXITING) +#define PROCBASED_CTLS2_ONE_SETTING \ + (PROCBASED2_ENABLE_EPT | \ + PROCBASED2_UNRESTRICTED_GUEST | \ + PROCBASED2_ENABLE_VPID | \ + PROCBASED2_ENABLE_RDTSCP) +#define PROCBASED_CTLS2_ZERO_SETTING \ + (PROCBASED2_VIRTUALIZE_APIC_ACCESSES | \ + PROCBASED2_DESC_TABLE_EXITING | \ + PROCBASED2_WBINVD_EXITING | \ + PROCBASED2_PAUSE_LOOP_EXITING /* FIXME */ | \ + PROCBASED2_RDRAND_EXITING | \ + PROCBASED2_ENABLE_INVPCID /* FIXME */) +#define PINBASED_CTLS_ONE_SETTING \ + (PINBASED_EXTINT_EXITING | \ + PINBASED_NMI_EXITING | \ + PINBASED_VIRTUAL_NMI) +#define PINBASED_CTLS_ZERO_SETTING \ + (PINBASED_PREMPTION_TIMER) +#define VM_ENTRY_CTLS_ONE_SETTING \ + (VM_ENTRY_LOAD_EFER) +#define VM_ENTRY_CTLS_ZERO_SETTING \ + (VM_ENTRY_INTO_SMM | \ + VM_ENTRY_DEACTIVATE_DUAL_MONITOR | \ + VM_ENTRY_GUEST_LMA) +#define VM_EXIT_CTLS_ONE_SETTING \ + (VM_EXIT_HOST_LMA | \ + VM_EXIT_LOAD_EFER) +#define VM_EXIT_CTLS_ZERO_SETTING \ + (VM_EXIT_SAVE_PREEMPTION_TIMER) +#define NMI_BLOCKING \ + (VMCS_INTERRUPTIBILITY_NMI_BLOCKING | \ + VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING) +#define HWINTR_BLOCKING \ + (VMCS_INTERRUPTIBILITY_STI_BLOCKING | \ + VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING) + +#define HANDLED 1 +#define UNHANDLED 0 + +static uint32_t pinbased_ctls, procbased_ctls, procbased_ctls2; +static uint32_t exit_ctls, entry_ctls; +static uint64_t cr0_ones_mask, cr0_zeros_mask; +static uint64_t cr4_ones_mask, cr4_zeros_mask; + +/* + * Optional capabilities + */ + +static int cap_halt_exit; +static int cap_pause_exit; +// static int cap_unrestricted_guest; +static int cap_monitor_trap; +// static int cap_invpcid; +// static int pirvec = -1; +// static struct unrhdr *vpid_unr; +// static u_int vpid_alloc_failed; + +/* + * Use the last page below 4GB as the APIC access address. This address is + * occupied by the boot firmware so it is guaranteed that it will not conflict + * with a page in system memory. + */ +// #define APIC_ACCESS_ADDRESS 0xFFFFF000 + +static int vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc); +static int vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval); + +static __inline uint64_t +reg_read(int vcpuid, hv_x86_reg_t reg) { + uint64_t val; + + hv_vcpu_read_register(((hv_vcpuid_t) vcpuid), reg, &val); + return val; +} + +static __inline void +reg_write(int vcpuid, hv_x86_reg_t reg, uint64_t val) { + hv_vcpu_write_register(((hv_vcpuid_t) vcpuid), reg, val); +} + +static void hvdump(int vcpu) { + printf("VMCS_PIN_BASED_CTLS: 0x%016llx\n", + vmcs_read(vcpu, VMCS_PIN_BASED_CTLS)); + printf("VMCS_PRI_PROC_BASED_CTLS: 0x%016llx\n", + vmcs_read(vcpu, VMCS_PRI_PROC_BASED_CTLS)); + printf("VMCS_SEC_PROC_BASED_CTLS: 0x%016llx\n", + vmcs_read(vcpu, VMCS_SEC_PROC_BASED_CTLS)); + printf("VMCS_ENTRY_CTLS: 0x%016llx\n", + vmcs_read(vcpu, VMCS_ENTRY_CTLS)); + printf("VMCS_EXCEPTION_BITMAP: 0x%016llx\n", + vmcs_read(vcpu, VMCS_EXCEPTION_BITMAP)); + printf("VMCS_CR0_MASK: 0x%016llx\n", + vmcs_read(vcpu, VMCS_CR0_MASK)); + printf("VMCS_CR0_SHADOW: 0x%016llx\n", + vmcs_read(vcpu, VMCS_CR0_SHADOW)); + printf("VMCS_CR4_MASK: 0x%016llx\n", + vmcs_read(vcpu, VMCS_CR4_MASK)); + printf("VMCS_CR4_SHADOW: 0x%016llx\n", + vmcs_read(vcpu, VMCS_CR4_SHADOW)); + printf("VMCS_GUEST_CS_SELECTOR: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_CS_SELECTOR)); + printf("VMCS_GUEST_CS_LIMIT: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_CS_LIMIT)); + printf("VMCS_GUEST_CS_ACCESS_RIGHTS: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_CS_ACCESS_RIGHTS)); + printf("VMCS_GUEST_CS_BASE: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_CS_BASE)); + printf("VMCS_GUEST_DS_SELECTOR: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_DS_SELECTOR)); + printf("VMCS_GUEST_DS_LIMIT: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_DS_LIMIT)); + printf("VMCS_GUEST_DS_ACCESS_RIGHTS: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_DS_ACCESS_RIGHTS)); + printf("VMCS_GUEST_DS_BASE: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_DS_BASE)); + printf("VMCS_GUEST_ES_SELECTOR: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_ES_SELECTOR)); + printf("VMCS_GUEST_ES_LIMIT: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_ES_LIMIT)); + printf("VMCS_GUEST_ES_ACCESS_RIGHTS: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_ES_ACCESS_RIGHTS)); + printf("VMCS_GUEST_ES_BASE: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_ES_BASE)); + printf("VMCS_GUEST_FS_SELECTOR: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_FS_SELECTOR)); + printf("VMCS_GUEST_FS_LIMIT: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_FS_LIMIT)); + printf("VMCS_GUEST_FS_ACCESS_RIGHTS: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_FS_ACCESS_RIGHTS)); + printf("VMCS_GUEST_FS_BASE: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_FS_BASE)); + printf("VMCS_GUEST_GS_SELECTOR: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_GS_SELECTOR)); + printf("VMCS_GUEST_GS_LIMIT: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_GS_LIMIT)); + printf("VMCS_GUEST_GS_ACCESS_RIGHTS: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_GS_ACCESS_RIGHTS)); + printf("VMCS_GUEST_GS_BASE: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_GS_BASE)); + printf("VMCS_GUEST_SS_SELECTOR: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_SS_SELECTOR)); + printf("VMCS_GUEST_SS_LIMIT: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_SS_LIMIT)); + printf("VMCS_GUEST_SS_ACCESS_RIGHTS: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_SS_ACCESS_RIGHTS)); + printf("VMCS_GUEST_SS_BASE: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_SS_BASE)); + printf("VMCS_GUEST_LDTR_SELECTOR: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_LDTR_SELECTOR)); + printf("VMCS_GUEST_LDTR_LIMIT: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_LDTR_LIMIT)); + printf("VMCS_GUEST_LDTR_ACCESS_RIGHTS: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_LDTR_ACCESS_RIGHTS)); + printf("VMCS_GUEST_LDTR_BASE: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_LDTR_BASE)); + printf("VMCS_GUEST_TR_SELECTOR: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_TR_SELECTOR)); + printf("VMCS_GUEST_TR_LIMIT: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_TR_LIMIT)); + printf("VMCS_GUEST_TR_ACCESS_RIGHTS: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_TR_ACCESS_RIGHTS)); + printf("VMCS_GUEST_TR_BASE: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_TR_BASE)); + printf("VMCS_GUEST_GDTR_LIMIT: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_GDTR_LIMIT)); + printf("VMCS_GUEST_GDTR_BASE: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_GDTR_BASE)); + printf("VMCS_GUEST_IDTR_LIMIT: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_LDTR_LIMIT)); + printf("VMCS_GUEST_IDTR_BASE: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_LDTR_BASE)); + printf("VMCS_GUEST_CR0: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_CR0)); + printf("VMCS_GUEST_CR3: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_CR3)); + printf("VMCS_GUEST_CR4: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_CR4)); + printf("VMCS_GUEST_IA32_EFER: 0x%016llx\n", + vmcs_read(vcpu, VMCS_GUEST_IA32_EFER)); + printf("\n"); + printf("rip: 0x%016llx rfl: 0x%016llx cr2: 0x%016llx\n", + reg_read(vcpu, HV_X86_RIP), reg_read(vcpu, HV_X86_RFLAGS), + reg_read(vcpu, HV_X86_CR2)); + printf("rax: 0x%016llx rbx: 0x%016llx rcx: 0x%016llx rdx: 0x%016llx\n", + reg_read(vcpu, HV_X86_RAX), reg_read(vcpu, HV_X86_RBX), + reg_read(vcpu, HV_X86_RCX), reg_read(vcpu, HV_X86_RDX)); + printf("rsi: 0x%016llx rdi: 0x%016llx rbp: 0x%016llx rsp: 0x%016llx\n", + reg_read(vcpu, HV_X86_RSI), reg_read(vcpu, HV_X86_RDI), + reg_read(vcpu, HV_X86_RBP), reg_read(vcpu, HV_X86_RSP)); + printf("r8: 0x%016llx r9: 0x%016llx r10: 0x%016llx r11: 0x%016llx\n", + reg_read(vcpu, HV_X86_R8), reg_read(vcpu, HV_X86_R9), + reg_read(vcpu, HV_X86_R10), reg_read(vcpu, HV_X86_R11)); + printf("r12: 0x%016llx r13: 0x%016llx r14: 0x%016llx r15: 0x%016llx\n", + reg_read(vcpu, HV_X86_R12), reg_read(vcpu, HV_X86_R12), + reg_read(vcpu, HV_X86_R14), reg_read(vcpu, HV_X86_R15)); +} + +#ifdef XHYVE_CONFIG_TRACE +static const char * +exit_reason_to_str(int reason) +{ + static char reasonbuf[32]; + + switch (reason) { + case EXIT_REASON_EXCEPTION: + return "exception"; + case EXIT_REASON_EXT_INTR: + return "extint"; + case EXIT_REASON_TRIPLE_FAULT: + return "triplefault"; + case EXIT_REASON_INIT: + return "init"; + case EXIT_REASON_SIPI: + return "sipi"; + case EXIT_REASON_IO_SMI: + return "iosmi"; + case EXIT_REASON_SMI: + return "smi"; + case EXIT_REASON_INTR_WINDOW: + return "intrwindow"; + case EXIT_REASON_NMI_WINDOW: + return "nmiwindow"; + case EXIT_REASON_TASK_SWITCH: + return "taskswitch"; + case EXIT_REASON_CPUID: + return "cpuid"; + case EXIT_REASON_GETSEC: + return "getsec"; + case EXIT_REASON_HLT: + return "hlt"; + case EXIT_REASON_INVD: + return "invd"; + case EXIT_REASON_INVLPG: + return "invlpg"; + case EXIT_REASON_RDPMC: + return "rdpmc"; + case EXIT_REASON_RDTSC: + return "rdtsc"; + case EXIT_REASON_RSM: + return "rsm"; + case EXIT_REASON_VMCALL: + return "vmcall"; + case EXIT_REASON_VMCLEAR: + return "vmclear"; + case EXIT_REASON_VMLAUNCH: + return "vmlaunch"; + case EXIT_REASON_VMPTRLD: + return "vmptrld"; + case EXIT_REASON_VMPTRST: + return "vmptrst"; + case EXIT_REASON_VMREAD: + return "vmread"; + case EXIT_REASON_VMRESUME: + return "vmresume"; + case EXIT_REASON_VMWRITE: + return "vmwrite"; + case EXIT_REASON_VMXOFF: + return "vmxoff"; + case EXIT_REASON_VMXON: + return "vmxon"; + case EXIT_REASON_CR_ACCESS: + return "craccess"; + case EXIT_REASON_DR_ACCESS: + return "draccess"; + case EXIT_REASON_INOUT: + return "inout"; + case EXIT_REASON_RDMSR: + return "rdmsr"; + case EXIT_REASON_WRMSR: + return "wrmsr"; + case EXIT_REASON_INVAL_VMCS: + return "invalvmcs"; + case EXIT_REASON_INVAL_MSR: + return "invalmsr"; + case EXIT_REASON_MWAIT: + return "mwait"; + case EXIT_REASON_MTF: + return "mtf"; + case EXIT_REASON_MONITOR: + return "monitor"; + case EXIT_REASON_PAUSE: + return "pause"; + case EXIT_REASON_MCE_DURING_ENTRY: + return "mce-during-entry"; + case EXIT_REASON_TPR: + return "tpr"; + case EXIT_REASON_APIC_ACCESS: + return "apic-access"; + case EXIT_REASON_GDTR_IDTR: + return "gdtridtr"; + case EXIT_REASON_LDTR_TR: + return "ldtrtr"; + case EXIT_REASON_EPT_FAULT: + return "eptfault"; + case EXIT_REASON_EPT_MISCONFIG: + return "eptmisconfig"; + case EXIT_REASON_INVEPT: + return "invept"; + case EXIT_REASON_RDTSCP: + return "rdtscp"; + case EXIT_REASON_VMX_PREEMPT: + return "vmxpreempt"; + case EXIT_REASON_INVVPID: + return "invvpid"; + case EXIT_REASON_WBINVD: + return "wbinvd"; + case EXIT_REASON_XSETBV: + return "xsetbv"; + case EXIT_REASON_APIC_WRITE: + return "apic-write"; + default: + snprintf(reasonbuf, sizeof(reasonbuf), "%d", reason); + return (reasonbuf); + } +} +#endif /* XHYVE_CONFIG_TRACE */ + +// static int +// vmx_allow_x2apic_msrs(struct vmx *vmx) +// { +// int i, error; + +// error = 0; + +// /* +// * Allow readonly access to the following x2APIC MSRs from the guest. +// */ +// error += guest_msr_ro(vmx, MSR_APIC_ID); +// error += guest_msr_ro(vmx, MSR_APIC_VERSION); +// error += guest_msr_ro(vmx, MSR_APIC_LDR); +// error += guest_msr_ro(vmx, MSR_APIC_SVR); + +// for (i = 0; i < 8; i++) +// error += guest_msr_ro(vmx, MSR_APIC_ISR0 + i); + +// for (i = 0; i < 8; i++) +// error += guest_msr_ro(vmx, MSR_APIC_TMR0 + i); + +// for (i = 0; i < 8; i++) +// error += guest_msr_ro(vmx, MSR_APIC_IRR0 + i); + +// error += guest_msr_ro(vmx, MSR_APIC_ESR); +// error += guest_msr_ro(vmx, MSR_APIC_LVT_TIMER); +// error += guest_msr_ro(vmx, MSR_APIC_LVT_THERMAL); +// error += guest_msr_ro(vmx, MSR_APIC_LVT_PCINT); +// error += guest_msr_ro(vmx, MSR_APIC_LVT_LINT0); +// error += guest_msr_ro(vmx, MSR_APIC_LVT_LINT1); +// error += guest_msr_ro(vmx, MSR_APIC_LVT_ERROR); +// error += guest_msr_ro(vmx, MSR_APIC_ICR_TIMER); +// error += guest_msr_ro(vmx, MSR_APIC_DCR_TIMER); +// error += guest_msr_ro(vmx, MSR_APIC_ICR); + +// /* +// * Allow TPR, EOI and SELF_IPI MSRs to be read and written by the guest. +// * +// * These registers get special treatment described in the section +// * "Virtualizing MSR-Based APIC Accesses". +// */ +// error += guest_msr_rw(vmx, MSR_APIC_TPR); +// error += guest_msr_rw(vmx, MSR_APIC_EOI); +// error += guest_msr_rw(vmx, MSR_APIC_SELF_IPI); + +// return (error); +// } + +u_long +vmx_fix_cr0(u_long cr0) +{ + return ((cr0 | cr0_ones_mask) & ~cr0_zeros_mask); +} + +u_long +vmx_fix_cr4(u_long cr4) +{ + return ((cr4 | cr4_ones_mask) & ~cr4_zeros_mask); +} + +static int +vmx_cleanup(void) +{ + return (0); +} + +static int +vmx_init(void) +{ + int error; + + if (hv_vm_create(HV_VM_DEFAULT)) { + xhyve_abort("hv_vm_create failed\n"); + } + + /* Check support for primary processor-based VM-execution controls */ + error = vmx_set_ctlreg(HV_VMX_CAP_PROCBASED, + PROCBASED_CTLS_ONE_SETTING, + PROCBASED_CTLS_ZERO_SETTING, &procbased_ctls); + if (error) { + printf("vmx_init: processor does not support desired primary " + "processor-based controls\n"); + return (error); + } + + /* Clear the processor-based ctl bits that are set on demand */ + procbased_ctls &= ~PROCBASED_CTLS_WINDOW_SETTING; + + /* Check support for secondary processor-based VM-execution controls */ + error = vmx_set_ctlreg(HV_VMX_CAP_PROCBASED2, + PROCBASED_CTLS2_ONE_SETTING, + PROCBASED_CTLS2_ZERO_SETTING, &procbased_ctls2); + if (error) { + printf("vmx_init: processor does not support desired secondary " + "processor-based controls\n"); + return (error); + } + + /* Check support for pin-based VM-execution controls */ + error = vmx_set_ctlreg(HV_VMX_CAP_PINBASED, + PINBASED_CTLS_ONE_SETTING, + PINBASED_CTLS_ZERO_SETTING, &pinbased_ctls); + if (error) { + printf("vmx_init: processor does not support desired " + "pin-based controls\n"); + return (error); + } + + /* Check support for VM-exit controls */ + error = vmx_set_ctlreg(HV_VMX_CAP_EXIT, + VM_EXIT_CTLS_ONE_SETTING, + VM_EXIT_CTLS_ZERO_SETTING, + &exit_ctls); + if (error) { + printf("vmx_init: processor does not support desired " + "exit controls\n"); + return (error); + } + + /* Check support for VM-entry controls */ + error = vmx_set_ctlreg(HV_VMX_CAP_ENTRY, + VM_ENTRY_CTLS_ONE_SETTING, VM_ENTRY_CTLS_ZERO_SETTING, + &entry_ctls); + if (error) { + printf("vmx_init: processor does not support desired " + "entry controls\n"); + return (error); + } + + /* + * Check support for optional features by testing them + * as individual bits + */ + cap_halt_exit = 1; + cap_monitor_trap = 1; + cap_pause_exit = 1; + // cap_unrestricted_guest = 1; + // cap_invpcid = 1; + + /* FIXME */ + cr0_ones_mask = cr4_ones_mask = 0; + cr0_zeros_mask = cr4_zeros_mask = 0; + + cr0_ones_mask |= (CR0_NE | CR0_ET); + cr0_zeros_mask |= (CR0_NW | CR0_CD); + cr4_ones_mask = 0x2000; + + vmx_msr_init(); + + return (0); +} + +static int +vmx_setup_cr_shadow(int vcpuid, int which, uint32_t initial) +{ + int error, mask_ident, shadow_ident; + uint64_t mask_value; + + if (which != 0 && which != 4) + xhyve_abort("vmx_setup_cr_shadow: unknown cr%d", which); + + if (which == 0) { + mask_ident = VMCS_CR0_MASK; + mask_value = (cr0_ones_mask | cr0_zeros_mask) | (CR0_PG | CR0_PE); + shadow_ident = VMCS_CR0_SHADOW; + } else { + mask_ident = VMCS_CR4_MASK; + mask_value = cr4_ones_mask | cr4_zeros_mask; + shadow_ident = VMCS_CR4_SHADOW; + } + + error = vmcs_setreg(vcpuid, VMCS_IDENT(mask_ident), mask_value); + if (error) + return (error); + + error = vmcs_setreg(vcpuid, VMCS_IDENT(shadow_ident), initial); + if (error) + return (error); + + return (0); +} +#define vmx_setup_cr0_shadow(vcpuid,init) vmx_setup_cr_shadow(vcpuid, 0, (init)) +#define vmx_setup_cr4_shadow(vcpuid,init) vmx_setup_cr_shadow(vcpuid, 4, (init)) + +static void * +vmx_vm_init(struct vm *vm) +{ + struct vmx *vmx; + + vmx = malloc(sizeof(struct vmx)); + assert(vmx); + bzero(vmx, sizeof(struct vmx)); + vmx->vm = vm; + + return (vmx); +} + +static int +vmx_vcpu_init(void *arg, int vcpuid) { + uint32_t exc_bitmap; + struct vmx *vmx; + hv_vcpuid_t hvid; + int error; + + vmx = (struct vmx *) arg; + + if (hv_vcpu_create(&hvid, HV_VCPU_DEFAULT)) { + xhyve_abort("hv_vcpu_create failed\n"); + } + + if (hvid != ((hv_vcpuid_t) vcpuid)) { + /* FIXME */ + xhyve_abort("vcpu id mismatch\n"); + } + + if (hv_vcpu_enable_native_msr(hvid, MSR_GSBASE, 1) || + hv_vcpu_enable_native_msr(hvid, MSR_FSBASE, 1) || + hv_vcpu_enable_native_msr(hvid, MSR_SYSENTER_CS_MSR, 1) || + hv_vcpu_enable_native_msr(hvid, MSR_SYSENTER_ESP_MSR, 1) || + hv_vcpu_enable_native_msr(hvid, MSR_SYSENTER_EIP_MSR, 1) || + hv_vcpu_enable_native_msr(hvid, MSR_TSC, 1) || + hv_vcpu_enable_native_msr(hvid, MSR_IA32_TSC_AUX, 1)) + { + xhyve_abort("vmx_vcpu_init: error setting guest msr access\n"); + } + + vmx_msr_guest_init(vmx, vcpuid); + + vmcs_write(vcpuid, VMCS_PIN_BASED_CTLS, pinbased_ctls); + vmcs_write(vcpuid, VMCS_PRI_PROC_BASED_CTLS, procbased_ctls); + vmcs_write(vcpuid, VMCS_SEC_PROC_BASED_CTLS, procbased_ctls2); + vmcs_write(vcpuid, VMCS_EXIT_CTLS, exit_ctls); + vmcs_write(vcpuid, VMCS_ENTRY_CTLS, entry_ctls); + + /* exception bitmap */ + if (vcpu_trace_exceptions()) + exc_bitmap = 0xffffffff; + else + exc_bitmap = 1 << IDT_MC; + + vmcs_write(vcpuid, VMCS_EXCEPTION_BITMAP, exc_bitmap); + + vmx->cap[vcpuid].set = 0; + vmx->cap[vcpuid].proc_ctls = procbased_ctls; + vmx->cap[vcpuid].proc_ctls2 = procbased_ctls2; + vmx->state[vcpuid].nextrip = ~(uint64_t) 0; + + /* + * Set up the CR0/4 shadows, and init the read shadow + * to the power-on register value from the Intel Sys Arch. + * CR0 - 0x60000010 + * CR4 - 0 + */ + error = vmx_setup_cr0_shadow(vcpuid, 0x60000010); + if (error != 0) + xhyve_abort("vmx_setup_cr0_shadow %d\n", error); + + error = vmx_setup_cr4_shadow(vcpuid, 0); + + if (error != 0) + xhyve_abort("vmx_setup_cr4_shadow %d\n", error); + + return (0); +} + +static int +vmx_handle_cpuid(struct vm *vm, int vcpuid) +{ + uint32_t eax, ebx, ecx, edx; + int error; + + eax = (uint32_t) reg_read(vcpuid, HV_X86_RAX); + ebx = (uint32_t) reg_read(vcpuid, HV_X86_RBX); + ecx = (uint32_t) reg_read(vcpuid, HV_X86_RCX); + edx = (uint32_t) reg_read(vcpuid, HV_X86_RDX); + + error = x86_emulate_cpuid(vm, vcpuid, &eax, &ebx, &ecx, &edx); + + reg_write(vcpuid, HV_X86_RAX, eax); + reg_write(vcpuid, HV_X86_RBX, ebx); + reg_write(vcpuid, HV_X86_RCX, ecx); + reg_write(vcpuid, HV_X86_RDX, edx); + + return (error); +} + +static __inline void +vmx_run_trace(struct vmx *vmx, int vcpu) +{ +#ifdef XHYVE_CONFIG_TRACE + VCPU_CTR1(vmx->vm, vcpu, "Resume execution at %#llx", vmcs_guest_rip(vcpu)); +#else + (void) vmx; + (void) vcpu; +#endif +} + +static __inline void +vmx_exit_trace(struct vmx *vmx, int vcpu, uint64_t rip, uint32_t exit_reason, + int handled) +{ +#ifdef XHYVE_CONFIG_TRACE + VCPU_CTR3(vmx->vm, vcpu, "%s %s vmexit at 0x%0llx", + handled ? "handled" : "unhandled", + exit_reason_to_str((int) exit_reason), rip); +#else + (void) vmx; + (void) vcpu; + (void) rip; + (void) exit_reason; + (void) handled; +#endif +} + +/* + * We depend on 'procbased_ctls' to have the Interrupt Window Exiting bit set. + */ +CTASSERT((PROCBASED_CTLS_ONE_SETTING & PROCBASED_INT_WINDOW_EXITING) != 0); + +static void __inline +vmx_set_int_window_exiting(struct vmx *vmx, int vcpu) +{ + if ((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) == 0) { + vmx->cap[vcpu].proc_ctls |= PROCBASED_INT_WINDOW_EXITING; + vmcs_write(vcpu, VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls); + VCPU_CTR0(vmx->vm, vcpu, "Enabling interrupt window exiting"); + } +} + +static void __inline +vmx_clear_int_window_exiting(struct vmx *vmx, int vcpu) +{ + KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) != 0, + ("intr_window_exiting not set: %#x", vmx->cap[vcpu].proc_ctls)); + vmx->cap[vcpu].proc_ctls &= ~PROCBASED_INT_WINDOW_EXITING; + vmcs_write(vcpu, VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls); + VCPU_CTR0(vmx->vm, vcpu, "Disabling interrupt window exiting"); +} + +static void __inline +vmx_set_nmi_window_exiting(struct vmx *vmx, int vcpu) +{ + + if ((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) == 0) { + vmx->cap[vcpu].proc_ctls |= PROCBASED_NMI_WINDOW_EXITING; + vmcs_write(vcpu, VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls); + VCPU_CTR0(vmx->vm, vcpu, "Enabling NMI window exiting"); + } +} + +static void __inline +vmx_clear_nmi_window_exiting(struct vmx *vmx, int vcpu) +{ + + KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) != 0, + ("nmi_window_exiting not set %#x", vmx->cap[vcpu].proc_ctls)); + vmx->cap[vcpu].proc_ctls &= ~PROCBASED_NMI_WINDOW_EXITING; + vmcs_write(vcpu, VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls); + VCPU_CTR0(vmx->vm, vcpu, "Disabling NMI window exiting"); +} + +static void +vmx_inject_nmi(struct vmx *vmx, int vcpu) +{ + uint32_t gi, info; + + gi = (uint32_t) vmcs_read(vcpu, VMCS_GUEST_INTERRUPTIBILITY); + KASSERT((gi & NMI_BLOCKING) == 0, ("vmx_inject_nmi: invalid guest " + "interruptibility-state %#x", gi)); + + info = (uint32_t) vmcs_read(vcpu, VMCS_ENTRY_INTR_INFO); + KASSERT((info & VMCS_INTR_VALID) == 0, ("vmx_inject_nmi: invalid " + "VM-entry interruption information %#x", info)); + + /* + * Inject the virtual NMI. The vector must be the NMI IDT entry + * or the VMCS entry check will fail. + */ + info = IDT_NMI | VMCS_INTR_T_NMI | VMCS_INTR_VALID; + vmcs_write(vcpu, VMCS_ENTRY_INTR_INFO, info); + + VCPU_CTR0(vmx->vm, vcpu, "Injecting vNMI"); + + /* Clear the request */ + vm_nmi_clear(vmx->vm, vcpu); +} + +static void +vmx_inject_interrupts(struct vmx *vmx, int vcpu, struct vlapic *vlapic, + uint64_t guestrip) +{ + int vector, need_nmi_exiting, extint_pending; + uint64_t rflags, entryinfo; + uint32_t gi, info; + + if (vmx->state[vcpu].nextrip != guestrip) { + gi = (uint32_t) vmcs_read(vcpu, VMCS_GUEST_INTERRUPTIBILITY); + if (gi & HWINTR_BLOCKING) { + VCPU_CTR2(vmx->vm, vcpu, "Guest interrupt blocking " + "cleared due to rip change: %#llx/%#llx", + vmx->state[vcpu].nextrip, guestrip); + gi &= ~HWINTR_BLOCKING; + vmcs_write(vcpu, VMCS_GUEST_INTERRUPTIBILITY, gi); + } + } + + if (vm_entry_intinfo(vmx->vm, vcpu, &entryinfo)) { + KASSERT((entryinfo & VMCS_INTR_VALID) != 0, ("%s: entry " + "intinfo is not valid: %#llx", __func__, entryinfo)); + + info = (uint32_t) vmcs_read(vcpu, VMCS_ENTRY_INTR_INFO); + KASSERT((info & VMCS_INTR_VALID) == 0, ("%s: cannot inject " + "pending exception: %#llx/%#x", __func__, entryinfo, info)); + + info = (uint32_t) entryinfo; + vector = info & 0xff; + if (vector == IDT_BP || vector == IDT_OF) { + /* + * VT-x requires #BP and #OF to be injected as software + * exceptions. + */ + info &= ~VMCS_INTR_T_MASK; + info |= VMCS_INTR_T_SWEXCEPTION; + } + + if (info & VMCS_INTR_DEL_ERRCODE) + vmcs_write(vcpu, VMCS_ENTRY_EXCEPTION_ERROR, entryinfo >> 32); + + vmcs_write(vcpu, VMCS_ENTRY_INTR_INFO, info); + } + + if (vm_nmi_pending(vmx->vm, vcpu)) { + /* + * If there are no conditions blocking NMI injection then + * inject it directly here otherwise enable "NMI window + * exiting" to inject it as soon as we can. + * + * We also check for STI_BLOCKING because some implementations + * don't allow NMI injection in this case. If we are running + * on a processor that doesn't have this restriction it will + * immediately exit and the NMI will be injected in the + * "NMI window exiting" handler. + */ + need_nmi_exiting = 1; + gi = (uint32_t) vmcs_read(vcpu, VMCS_GUEST_INTERRUPTIBILITY); + if ((gi & (HWINTR_BLOCKING | NMI_BLOCKING)) == 0) { + info = (uint32_t) vmcs_read(vcpu, VMCS_ENTRY_INTR_INFO); + if ((info & VMCS_INTR_VALID) == 0) { + vmx_inject_nmi(vmx, vcpu); + need_nmi_exiting = 0; + } else { + VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI " + "due to VM-entry intr info %#x", info); + } + } else { + VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI due to " + "Guest Interruptibility-state %#x", gi); + } + + if (need_nmi_exiting) + vmx_set_nmi_window_exiting(vmx, vcpu); + } + + extint_pending = vm_extint_pending(vmx->vm, vcpu); + + /* + * If interrupt-window exiting is already in effect then don't bother + * checking for pending interrupts. This is just an optimization and + * not needed for correctness. + */ + if ((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) != 0) { + VCPU_CTR0(vmx->vm, vcpu, "Skip interrupt injection due to " + "pending int_window_exiting"); + return; + } + + if (!extint_pending) { + /* Ask the local apic for a vector to inject */ + if (!vlapic_pending_intr(vlapic, &vector)) + return; + + /* + * From the Intel SDM, Volume 3, Section "Maskable + * Hardware Interrupts": + * - maskable interrupt vectors [16,255] can be delivered + * through the local APIC. + */ + KASSERT(vector >= 16 && vector <= 255, + ("invalid vector %d from local APIC", vector)); + } else { + /* Ask the legacy pic for a vector to inject */ + vatpic_pending_intr(vmx->vm, &vector); + + /* + * From the Intel SDM, Volume 3, Section "Maskable + * Hardware Interrupts": + * - maskable interrupt vectors [0,255] can be delivered + * through the INTR pin. + */ + KASSERT(vector >= 0 && vector <= 255, + ("invalid vector %d from INTR", vector)); + } + + /* Check RFLAGS.IF and the interruptibility state of the guest */ + rflags = vmcs_read(vcpu, VMCS_GUEST_RFLAGS); + if ((rflags & PSL_I) == 0) { + VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to " + "rflags %#llx", vector, rflags); + goto cantinject; + } + + gi = (uint32_t) vmcs_read(vcpu, VMCS_GUEST_INTERRUPTIBILITY); + if (gi & HWINTR_BLOCKING) { + VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to " + "Guest Interruptibility-state %#x", vector, gi); + goto cantinject; + } + + info = (uint32_t) vmcs_read(vcpu, VMCS_ENTRY_INTR_INFO); + if (info & VMCS_INTR_VALID) { + /* + * This is expected and could happen for multiple reasons: + * - A vectoring VM-entry was aborted due to astpending + * - A VM-exit happened during event injection. + * - An exception was injected above. + * - An NMI was injected above or after "NMI window exiting" + */ + VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to " + "VM-entry intr info %#x", vector, info); + goto cantinject; + } + + /* Inject the interrupt */ + info = VMCS_INTR_T_HWINTR | VMCS_INTR_VALID; + info |= (uint32_t) vector; + vmcs_write(vcpu, VMCS_ENTRY_INTR_INFO, info); + + if (!extint_pending) { + /* Update the Local APIC ISR */ + vlapic_intr_accepted(vlapic, vector); + } else { + vm_extint_clear(vmx->vm, vcpu); + vatpic_intr_accepted(vmx->vm, vector); + + /* + * After we accepted the current ExtINT the PIC may + * have posted another one. If that is the case, set + * the Interrupt Window Exiting execution control so + * we can inject that one too. + * + * Also, interrupt window exiting allows us to inject any + * pending APIC vector that was preempted by the ExtINT + * as soon as possible. This applies both for the software + * emulated vlapic and the hardware assisted virtual APIC. + */ + vmx_set_int_window_exiting(vmx, vcpu); + } + + VCPU_CTR1(vmx->vm, vcpu, "Injecting hwintr at vector %d", vector); + + return; + +cantinject: + /* + * Set the Interrupt Window Exiting execution control so we can inject + * the interrupt as soon as blocking condition goes away. + */ + vmx_set_int_window_exiting(vmx, vcpu); +} + +/* + * If the Virtual NMIs execution control is '1' then the logical processor + * tracks virtual-NMI blocking in the Guest Interruptibility-state field of + * the VMCS. An IRET instruction in VMX non-root operation will remove any + * virtual-NMI blocking. + * + * This unblocking occurs even if the IRET causes a fault. In this case the + * hypervisor needs to restore virtual-NMI blocking before resuming the guest. + */ +static void +vmx_restore_nmi_blocking(struct vmx *vmx, int vcpuid) +{ + uint32_t gi; + + VCPU_CTR0(vmx->vm, vcpuid, "Restore Virtual-NMI blocking"); + gi = (uint32_t) vmcs_read(vcpuid, VMCS_GUEST_INTERRUPTIBILITY); + gi |= VMCS_INTERRUPTIBILITY_NMI_BLOCKING; + vmcs_write(vcpuid, VMCS_GUEST_INTERRUPTIBILITY, gi); +} + +static void +vmx_clear_nmi_blocking(struct vmx *vmx, int vcpuid) +{ + uint32_t gi; + + VCPU_CTR0(vmx->vm, vcpuid, "Clear Virtual-NMI blocking"); + gi = (uint32_t) vmcs_read(vcpuid, VMCS_GUEST_INTERRUPTIBILITY); + gi &= ~VMCS_INTERRUPTIBILITY_NMI_BLOCKING; + vmcs_write(vcpuid, VMCS_GUEST_INTERRUPTIBILITY, gi); +} + +static void +vmx_assert_nmi_blocking(int vcpuid) +{ + uint32_t gi; + + gi = (uint32_t) vmcs_read(vcpuid, VMCS_GUEST_INTERRUPTIBILITY); + KASSERT(gi & VMCS_INTERRUPTIBILITY_NMI_BLOCKING, + ("NMI blocking is not in effect %#x", gi)); +} + +static int +vmx_emulate_xsetbv(struct vmx *vmx, int vcpu) +{ + uint64_t xcrval; + const struct xsave_limits *limits; + + limits = vmm_get_xsave_limits(); + + /* + * Note that the processor raises a GP# fault on its own if + * xsetbv is executed for CPL != 0, so we do not have to + * emulate that fault here. + */ + + /* Only xcr0 is supported. */ + if (reg_read(vcpu, HV_X86_RCX) != 0) { + vm_inject_gp(vmx->vm, vcpu); + return (HANDLED); + } + + /* We only handle xcr0 if both the host and guest have XSAVE enabled. */ + if (!limits->xsave_enabled || + !(vmcs_read(vcpu, VMCS_GUEST_CR4) & CR4_XSAVE)) + { + vm_inject_ud(vmx->vm, vcpu); + return (HANDLED); + } + + xcrval = reg_read(vcpu, HV_X86_RDX) << 32 + | (reg_read(vcpu, HV_X86_RAX) & 0xffffffff); + + if ((xcrval & ~limits->xcr0_allowed) != 0) { + vm_inject_gp(vmx->vm, vcpu); + return (HANDLED); + } + + if (!(xcrval & XFEATURE_ENABLED_X87)) { + vm_inject_gp(vmx->vm, vcpu); + return (HANDLED); + } + + /* AVX (YMM_Hi128) requires SSE. */ + if (xcrval & XFEATURE_ENABLED_AVX && + (xcrval & XFEATURE_AVX) != XFEATURE_AVX) { + vm_inject_gp(vmx->vm, vcpu); + return (HANDLED); + } + + /* + * AVX512 requires base AVX (YMM_Hi128) as well as OpMask, + * ZMM_Hi256, and Hi16_ZMM. + */ + if (xcrval & XFEATURE_AVX512 && + (xcrval & (XFEATURE_AVX512 | XFEATURE_AVX)) != + (XFEATURE_AVX512 | XFEATURE_AVX)) { + vm_inject_gp(vmx->vm, vcpu); + return (HANDLED); + } + + /* + * Intel MPX requires both bound register state flags to be + * set. + */ + if (((xcrval & XFEATURE_ENABLED_BNDREGS) != 0) != + ((xcrval & XFEATURE_ENABLED_BNDCSR) != 0)) { + vm_inject_gp(vmx->vm, vcpu); + return (HANDLED); + } + + reg_write(vcpu, HV_X86_XCR0, xcrval); + return (HANDLED); +} + +static uint64_t +vmx_get_guest_reg(int vcpu, int ident) +{ + switch (ident) { + case 0: + return (reg_read(vcpu, HV_X86_RAX)); + case 1: + return (reg_read(vcpu, HV_X86_RCX)); + case 2: + return (reg_read(vcpu, HV_X86_RDX)); + case 3: + return (reg_read(vcpu, HV_X86_RBX)); + case 4: + return (vmcs_read(vcpu, VMCS_GUEST_RSP)); + case 5: + return (reg_read(vcpu, HV_X86_RBP)); + case 6: + return (reg_read(vcpu, HV_X86_RSI)); + case 7: + return (reg_read(vcpu, HV_X86_RDI)); + case 8: + return (reg_read(vcpu, HV_X86_R8)); + case 9: + return (reg_read(vcpu, HV_X86_R9)); + case 10: + return (reg_read(vcpu, HV_X86_R10)); + case 11: + return (reg_read(vcpu, HV_X86_R11)); + case 12: + return (reg_read(vcpu, HV_X86_R12)); + case 13: + return (reg_read(vcpu, HV_X86_R13)); + case 14: + return (reg_read(vcpu, HV_X86_R14)); + case 15: + return (reg_read(vcpu, HV_X86_R15)); + default: + xhyve_abort("invalid vmx register %d", ident); + } +} + +static void +vmx_set_guest_reg(int vcpu, int ident, uint64_t regval) +{ + switch (ident) { + case 0: + reg_write(vcpu, HV_X86_RAX, regval); + break; + case 1: + reg_write(vcpu, HV_X86_RCX, regval); + break; + case 2: + reg_write(vcpu, HV_X86_RDX, regval); + break; + case 3: + reg_write(vcpu, HV_X86_RBX, regval); + break; + case 4: + vmcs_write(vcpu, VMCS_GUEST_RSP, regval); + break; + case 5: + reg_write(vcpu, HV_X86_RBP, regval); + break; + case 6: + reg_write(vcpu, HV_X86_RSI, regval); + break; + case 7: + reg_write(vcpu, HV_X86_RDI, regval); + break; + case 8: + reg_write(vcpu, HV_X86_R8, regval); + break; + case 9: + reg_write(vcpu, HV_X86_R9, regval); + break; + case 10: + reg_write(vcpu, HV_X86_R10, regval); + break; + case 11: + reg_write(vcpu, HV_X86_R11, regval); + break; + case 12: + reg_write(vcpu, HV_X86_R12, regval); + break; + case 13: + reg_write(vcpu, HV_X86_R13, regval); + break; + case 14: + reg_write(vcpu, HV_X86_R14, regval); + break; + case 15: + reg_write(vcpu, HV_X86_R15, regval); + break; + default: + xhyve_abort("invalid vmx register %d", ident); + } +} + +static int +vmx_emulate_cr0_access(UNUSED struct vm *vm, int vcpu, uint64_t exitqual) +{ + uint64_t crval, regval; + // *pt; + + /* We only handle mov to %cr0 at this time */ + if ((exitqual & 0xf0) != 0x00) + return (UNHANDLED); + + regval = vmx_get_guest_reg(vcpu, (exitqual >> 8) & 0xf); + + vmcs_write(vcpu, VMCS_CR0_SHADOW, regval); + + crval = regval | cr0_ones_mask; + crval &= ~cr0_zeros_mask; + // printf("cr0: v:0x%016llx 1:0x%08llx 0:0x%08llx v:0x%016llx\n", + // regval, cr0_ones_mask, cr0_zeros_mask, crval); + vmcs_write(vcpu, VMCS_GUEST_CR0, crval); + + if (regval & CR0_PG) { + uint64_t efer, entryctls; + + /* + * If CR0.PG is 1 and EFER.LME is 1 then EFER.LMA and + * the "IA-32e mode guest" bit in VM-entry control must be + * equal. + */ + efer = vmcs_read(vcpu, VMCS_GUEST_IA32_EFER); + if (efer & EFER_LME) { + efer |= EFER_LMA; + vmcs_write(vcpu, VMCS_GUEST_IA32_EFER, efer); + entryctls = vmcs_read(vcpu, VMCS_ENTRY_CTLS); + entryctls |= VM_ENTRY_GUEST_LMA; + vmcs_write(vcpu, VMCS_ENTRY_CTLS, entryctls); + } + + // if (vmcs_read(vcpu, VMCS_GUEST_CR4) & CR4_PAE) { + // if (!(pt = (uint64_t *) vm_gpa2hva(vm, + // vmcs_read(vcpu, VMCS_GUEST_CR3), sizeof(uint64_t) * 4))) + // { + // xhyve_abort("invalid cr3\n"); + // } + + // vmcs_write(vcpu, VMCS_GUEST_PDPTE0, pt[0]); + // vmcs_write(vcpu, VMCS_GUEST_PDPTE1, pt[1]); + // vmcs_write(vcpu, VMCS_GUEST_PDPTE2, pt[2]); + // vmcs_write(vcpu, VMCS_GUEST_PDPTE3, pt[3]); + // } + } + + return (HANDLED); +} + +static int +vmx_emulate_cr4_access(int vcpu, uint64_t exitqual) +{ + uint64_t crval, regval; + + /* We only handle mov to %cr4 at this time */ + if ((exitqual & 0xf0) != 0x00) + return (UNHANDLED); + + regval = vmx_get_guest_reg(vcpu, (exitqual >> 8) & 0xf); + + vmcs_write(vcpu, VMCS_CR4_SHADOW, regval); + + crval = regval | cr4_ones_mask; + crval &= ~cr4_zeros_mask; + vmcs_write(vcpu, VMCS_GUEST_CR4, crval); + + return (HANDLED); +} + +static int +vmx_emulate_cr8_access(struct vmx *vmx, int vcpu, uint64_t exitqual) +{ + struct vlapic *vlapic; + uint64_t cr8; + int regnum; + + /* We only handle mov %cr8 to/from a register at this time. */ + if ((exitqual & 0xe0) != 0x00) { + return (UNHANDLED); + } + + vlapic = vm_lapic(vmx->vm, vcpu); + regnum = (exitqual >> 8) & 0xf; + if (exitqual & 0x10) { + cr8 = vlapic_get_cr8(vlapic); + vmx_set_guest_reg(vcpu, regnum, cr8); + } else { + cr8 = vmx_get_guest_reg(vcpu, regnum); + vlapic_set_cr8(vlapic, cr8); + } + + return (HANDLED); +} + +/* + * From section "Guest Register State" in the Intel SDM: CPL = SS.DPL + */ +static int +vmx_cpl(int vcpu) +{ + uint32_t ssar; + + ssar = (uint32_t) vmcs_read(vcpu, VMCS_GUEST_SS_ACCESS_RIGHTS); + return ((ssar >> 5) & 0x3); +} + +static enum vm_cpu_mode +vmx_cpu_mode(int vcpu) +{ + uint32_t csar; + + if (vmcs_read(vcpu, VMCS_GUEST_IA32_EFER) & EFER_LMA) { + csar = (uint32_t) vmcs_read(vcpu, VMCS_GUEST_CS_ACCESS_RIGHTS); + if (csar & 0x2000) + return (CPU_MODE_64BIT); /* CS.L = 1 */ + else + return (CPU_MODE_COMPATIBILITY); + } else if (vmcs_read(vcpu, VMCS_GUEST_CR0) & CR0_PE) { + return (CPU_MODE_PROTECTED); + } else { + return (CPU_MODE_REAL); + } +} + +static enum vm_paging_mode +vmx_paging_mode(int vcpu) +{ + + if (!(vmcs_read(vcpu, VMCS_GUEST_CR0) & CR0_PG)) + return (PAGING_MODE_FLAT); + if (!(vmcs_read(vcpu, VMCS_GUEST_CR4) & CR4_PAE)) + return (PAGING_MODE_32); + if (vmcs_read(vcpu, VMCS_GUEST_IA32_EFER) & EFER_LME) + return (PAGING_MODE_64); + else + return (PAGING_MODE_PAE); +} + +static uint64_t +inout_str_index(struct vmx *vmx, int vcpuid, int in) +{ + uint64_t val; + int error; + enum vm_reg_name reg; + + reg = in ? VM_REG_GUEST_RDI : VM_REG_GUEST_RSI; + error = vmx_getreg(vmx, vcpuid, reg, &val); + KASSERT(error == 0, ("%s: vmx_getreg error %d", __func__, error)); + return (val); +} + +static uint64_t +inout_str_count(struct vmx *vmx, int vcpuid, int rep) +{ + uint64_t val; + int error; + + if (rep) { + error = vmx_getreg(vmx, vcpuid, VM_REG_GUEST_RCX, &val); + KASSERT(!error, ("%s: vmx_getreg error %d", __func__, error)); + } else { + val = 1; + } + return (val); +} + +static int +inout_str_addrsize(uint32_t inst_info) +{ + uint32_t size; + + size = (inst_info >> 7) & 0x7; + switch (size) { + case 0: + return (2); /* 16 bit */ + case 1: + return (4); /* 32 bit */ + case 2: + return (8); /* 64 bit */ + default: + xhyve_abort("%s: invalid size encoding %d", __func__, size); + } +} + +static void +inout_str_seginfo(struct vmx *vmx, int vcpuid, uint32_t inst_info, int in, + struct vm_inout_str *vis) +{ + int error, s; + + if (in) { + vis->seg_name = VM_REG_GUEST_ES; + } else { + s = (inst_info >> 15) & 0x7; + vis->seg_name = vm_segment_name(s); + } + + error = vmx_getdesc(vmx, vcpuid, vis->seg_name, &vis->seg_desc); + KASSERT(error == 0, ("%s: vmx_getdesc error %d", __func__, error)); +} + +static void +vmx_paging_info(struct vm_guest_paging *paging, int vcpu) +{ + paging->cr3 = vmcs_guest_cr3(vcpu); + paging->cpl = vmx_cpl(vcpu); + paging->cpu_mode = vmx_cpu_mode(vcpu); + paging->paging_mode = vmx_paging_mode(vcpu); +} + +static void +vmexit_inst_emul(struct vm_exit *vmexit, uint64_t gpa, uint64_t gla, int vcpu) +{ + struct vm_guest_paging *paging; + uint32_t csar; + + paging = &vmexit->u.inst_emul.paging; + + vmexit->exitcode = VM_EXITCODE_INST_EMUL; + vmexit->u.inst_emul.gpa = gpa; + vmexit->u.inst_emul.gla = gla; + vmx_paging_info(paging, vcpu); + switch (paging->cpu_mode) { + case CPU_MODE_REAL: + vmexit->u.inst_emul.cs_base = vmcs_read(vcpu, VMCS_GUEST_CS_BASE); + vmexit->u.inst_emul.cs_d = 0; + break; + case CPU_MODE_PROTECTED: + case CPU_MODE_COMPATIBILITY: + vmexit->u.inst_emul.cs_base = vmcs_read(vcpu, VMCS_GUEST_CS_BASE); + csar = (uint32_t) vmcs_read(vcpu, VMCS_GUEST_CS_ACCESS_RIGHTS); + vmexit->u.inst_emul.cs_d = SEG_DESC_DEF32(csar); + break; + case CPU_MODE_64BIT: + vmexit->u.inst_emul.cs_base = 0; + vmexit->u.inst_emul.cs_d = 0; + break; + } + vie_init(&vmexit->u.inst_emul.vie, NULL, 0); +} + +static int +ept_fault_type(uint64_t ept_qual) +{ + int fault_type; + + if (ept_qual & EPT_VIOLATION_DATA_WRITE) + fault_type = XHYVE_PROT_WRITE; + else if (ept_qual & EPT_VIOLATION_INST_FETCH) + fault_type = XHYVE_PROT_EXECUTE; + else + fault_type= XHYVE_PROT_READ; + + return (fault_type); +} + +static bool +ept_emulation_fault(uint64_t ept_qual) +{ + int read, write; + + /* EPT fault on an instruction fetch doesn't make sense here */ + if (ept_qual & EPT_VIOLATION_INST_FETCH) + return (FALSE); + + /* EPT fault must be a read fault or a write fault */ + read = ept_qual & EPT_VIOLATION_DATA_READ ? 1 : 0; + write = ept_qual & EPT_VIOLATION_DATA_WRITE ? 1 : 0; + if ((read | write) == 0) + return (FALSE); + + /* + * The EPT violation must have been caused by accessing a + * guest-physical address that is a translation of a guest-linear + * address. + */ + if ((ept_qual & EPT_VIOLATION_GLA_VALID) == 0 || + (ept_qual & EPT_VIOLATION_XLAT_VALID) == 0) { + return (FALSE); + } + + return (TRUE); +} + +static __inline int +apic_access_virtualization(struct vmx *vmx, int vcpuid) +{ + uint32_t proc_ctls2; + + proc_ctls2 = vmx->cap[vcpuid].proc_ctls2; + return ((proc_ctls2 & PROCBASED2_VIRTUALIZE_APIC_ACCESSES) ? 1 : 0); +} + +static __inline int +x2apic_virtualization(struct vmx *vmx, int vcpuid) +{ + uint32_t proc_ctls2; + + proc_ctls2 = vmx->cap[vcpuid].proc_ctls2; + return ((proc_ctls2 & PROCBASED2_VIRTUALIZE_X2APIC_MODE) ? 1 : 0); +} + +static int +vmx_handle_apic_write(struct vmx *vmx, int vcpuid, struct vlapic *vlapic, + uint64_t qual) +{ + int error, handled, offset; + uint32_t *apic_regs, vector; + bool retu; + + handled = HANDLED; + offset = APIC_WRITE_OFFSET(qual); + + if (!apic_access_virtualization(vmx, vcpuid)) { + /* + * In general there should not be any APIC write VM-exits + * unless APIC-access virtualization is enabled. + * + * However self-IPI virtualization can legitimately trigger + * an APIC-write VM-exit so treat it specially. + */ + if (x2apic_virtualization(vmx, vcpuid) && + offset == APIC_OFFSET_SELF_IPI) { + apic_regs = (uint32_t *)(vlapic->apic_page); + vector = apic_regs[APIC_OFFSET_SELF_IPI / 4]; + vlapic_self_ipi_handler(vlapic, vector); + return (HANDLED); + } else + return (UNHANDLED); + } + + switch (offset) { + case APIC_OFFSET_ID: + vlapic_id_write_handler(vlapic); + break; + case APIC_OFFSET_LDR: + vlapic_ldr_write_handler(vlapic); + break; + case APIC_OFFSET_DFR: + vlapic_dfr_write_handler(vlapic); + break; + case APIC_OFFSET_SVR: + vlapic_svr_write_handler(vlapic); + break; + case APIC_OFFSET_ESR: + vlapic_esr_write_handler(vlapic); + break; + case APIC_OFFSET_ICR_LOW: + retu = false; + error = vlapic_icrlo_write_handler(vlapic, &retu); + if (error != 0 || retu) + handled = UNHANDLED; + break; + case APIC_OFFSET_CMCI_LVT: + case APIC_OFFSET_TIMER_LVT: + case APIC_OFFSET_THERM_LVT: + case APIC_OFFSET_PERF_LVT: + case APIC_OFFSET_LINT0_LVT: + case APIC_OFFSET_LINT1_LVT: + case APIC_OFFSET_ERROR_LVT: + vlapic_lvt_write_handler(vlapic, ((uint32_t) offset)); + break; + case APIC_OFFSET_TIMER_ICR: + vlapic_icrtmr_write_handler(vlapic); + break; + case APIC_OFFSET_TIMER_DCR: + vlapic_dcr_write_handler(vlapic); + break; + default: + handled = UNHANDLED; + break; + } + return (handled); +} + +static bool +apic_access_fault(struct vmx *vmx, int vcpuid, uint64_t gpa) +{ + + if (apic_access_virtualization(vmx, vcpuid) && + (gpa >= DEFAULT_APIC_BASE && gpa < DEFAULT_APIC_BASE + XHYVE_PAGE_SIZE)) + return (true); + else + return (false); +} + +static int +vmx_handle_apic_access(struct vmx *vmx, int vcpuid, struct vm_exit *vmexit) +{ + uint64_t qual; + int access_type, offset, allowed; + + if (!apic_access_virtualization(vmx, vcpuid)) + return (UNHANDLED); + + qual = vmexit->u.vmx.exit_qualification; + access_type = APIC_ACCESS_TYPE(qual); + offset = APIC_ACCESS_OFFSET(qual); + + allowed = 0; + if (access_type == 0) { + /* + * Read data access to the following registers is expected. + */ + switch (offset) { + case APIC_OFFSET_APR: + case APIC_OFFSET_PPR: + case APIC_OFFSET_RRR: + case APIC_OFFSET_CMCI_LVT: + case APIC_OFFSET_TIMER_CCR: + allowed = 1; + break; + default: + break; + } + } else if (access_type == 1) { + /* + * Write data access to the following registers is expected. + */ + switch (offset) { + case APIC_OFFSET_VER: + case APIC_OFFSET_APR: + case APIC_OFFSET_PPR: + case APIC_OFFSET_RRR: + case APIC_OFFSET_ISR0: + case APIC_OFFSET_ISR1: + case APIC_OFFSET_ISR2: + case APIC_OFFSET_ISR3: + case APIC_OFFSET_ISR4: + case APIC_OFFSET_ISR5: + case APIC_OFFSET_ISR6: + case APIC_OFFSET_ISR7: + case APIC_OFFSET_TMR0: + case APIC_OFFSET_TMR1: + case APIC_OFFSET_TMR2: + case APIC_OFFSET_TMR3: + case APIC_OFFSET_TMR4: + case APIC_OFFSET_TMR5: + case APIC_OFFSET_TMR6: + case APIC_OFFSET_TMR7: + case APIC_OFFSET_IRR0: + case APIC_OFFSET_IRR1: + case APIC_OFFSET_IRR2: + case APIC_OFFSET_IRR3: + case APIC_OFFSET_IRR4: + case APIC_OFFSET_IRR5: + case APIC_OFFSET_IRR6: + case APIC_OFFSET_IRR7: + case APIC_OFFSET_CMCI_LVT: + case APIC_OFFSET_TIMER_CCR: + allowed = 1; + break; + default: + break; + } + } + + if (allowed) { + vmexit_inst_emul(vmexit, DEFAULT_APIC_BASE + ((uint32_t) offset), + VIE_INVALID_GLA, vcpuid); + } + + /* + * Regardless of whether the APIC-access is allowed this handler + * always returns UNHANDLED: + * - if the access is allowed then it is handled by emulating the + * instruction that caused the VM-exit (outside the critical section) + * - if the access is not allowed then it will be converted to an + * exitcode of VM_EXITCODE_VMX and will be dealt with in userland. + */ + return (UNHANDLED); +} + +static enum task_switch_reason +vmx_task_switch_reason(uint64_t qual) +{ + int reason; + + reason = (qual >> 30) & 0x3; + switch (reason) { + case 0: + return (TSR_CALL); + case 1: + return (TSR_IRET); + case 2: + return (TSR_JMP); + case 3: + return (TSR_IDT_GATE); + default: + xhyve_abort("%s: invalid reason %d", __func__, reason); + } +} + +static int +emulate_wrmsr(struct vmx *vmx, int vcpuid, u_int num, uint64_t val, bool *retu) +{ + int error; + + if (lapic_msr(num)) + error = lapic_wrmsr(vmx->vm, vcpuid, num, val, retu); + else + error = vmx_wrmsr(vmx, vcpuid, num, val); + + return (error); +} + +static int +emulate_rdmsr(struct vmx *vmx, int vcpuid, u_int num, bool *retu) +{ + uint64_t result; + uint32_t eax, edx; + int error; + + if (lapic_msr(num)) + error = lapic_rdmsr(vmx->vm, vcpuid, num, &result, retu); + else + error = vmx_rdmsr(vmx, vcpuid, num, &result); + + if (error == 0) { + eax = (uint32_t) result; + reg_write(vcpuid, HV_X86_RAX, eax); + edx = (uint32_t) (result >> 32); + reg_write(vcpuid, HV_X86_RDX, edx); + } + + return (error); +} + +static int +vmx_exit_process(struct vmx *vmx, int vcpu, struct vm_exit *vmexit) +{ + int error, errcode, errcode_valid, handled, in; + struct vlapic *vlapic; + struct vm_inout_str *vis; + struct vm_task_switch *ts; + uint32_t eax, ecx, edx, idtvec_info, idtvec_err, intr_info, inst_info; + uint32_t intr_type, intr_vec, reason; + uint64_t exitintinfo, qual, gpa; + bool retu; + + CTASSERT((PINBASED_CTLS_ONE_SETTING & PINBASED_VIRTUAL_NMI) != 0); + CTASSERT((PINBASED_CTLS_ONE_SETTING & PINBASED_NMI_EXITING) != 0); + + handled = UNHANDLED; + + qual = vmexit->u.vmx.exit_qualification; + reason = vmexit->u.vmx.exit_reason; + vmexit->exitcode = VM_EXITCODE_BOGUS; + + vmm_stat_incr(vmx->vm, vcpu, VMEXIT_COUNT, 1); + + /* + * VM exits that can be triggered during event delivery need to + * be handled specially by re-injecting the event if the IDT + * vectoring information field's valid bit is set. + * + * See "Information for VM Exits During Event Delivery" in Intel SDM + * for details. + */ + idtvec_info = (uint32_t) vmcs_idt_vectoring_info(vcpu); + if (idtvec_info & VMCS_IDT_VEC_VALID) { + idtvec_info &= ~(1u << 12); /* clear undefined bit */ + exitintinfo = idtvec_info; + if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) { + idtvec_err = (uint32_t) vmcs_idt_vectoring_err(vcpu); + exitintinfo |= (uint64_t)idtvec_err << 32; + } + error = vm_exit_intinfo(vmx->vm, vcpu, exitintinfo); + KASSERT(error == 0, ("%s: vm_set_intinfo error %d", + __func__, error)); + + /* + * If 'virtual NMIs' are being used and the VM-exit + * happened while injecting an NMI during the previous + * VM-entry, then clear "blocking by NMI" in the + * Guest Interruptibility-State so the NMI can be + * reinjected on the subsequent VM-entry. + * + * However, if the NMI was being delivered through a task + * gate, then the new task must start execution with NMIs + * blocked so don't clear NMI blocking in this case. + */ + intr_type = idtvec_info & VMCS_INTR_T_MASK; + if (intr_type == VMCS_INTR_T_NMI) { + if (reason != EXIT_REASON_TASK_SWITCH) + vmx_clear_nmi_blocking(vmx, vcpu); + else + vmx_assert_nmi_blocking(vcpu); + } + + /* + * Update VM-entry instruction length if the event being + * delivered was a software interrupt or software exception. + */ + if (intr_type == VMCS_INTR_T_SWINTR || + intr_type == VMCS_INTR_T_PRIV_SWEXCEPTION || + intr_type == VMCS_INTR_T_SWEXCEPTION) { + vmcs_write(vcpu, VMCS_ENTRY_INST_LENGTH, + ((uint64_t) vmexit->inst_length)); + } + } + + switch (reason) { + case EXIT_REASON_TASK_SWITCH: + ts = &vmexit->u.task_switch; + ts->tsssel = qual & 0xffff; + ts->reason = vmx_task_switch_reason(qual); + ts->ext = 0; + ts->errcode_valid = 0; + vmx_paging_info(&ts->paging, vcpu); + /* + * If the task switch was due to a CALL, JMP, IRET, software + * interrupt (INT n) or software exception (INT3, INTO), + * then the saved %rip references the instruction that caused + * the task switch. The instruction length field in the VMCS + * is valid in this case. + * + * In all other cases (e.g., NMI, hardware exception) the + * saved %rip is one that would have been saved in the old TSS + * had the task switch completed normally so the instruction + * length field is not needed in this case and is explicitly + * set to 0. + */ + if (ts->reason == TSR_IDT_GATE) { + KASSERT(idtvec_info & VMCS_IDT_VEC_VALID, + ("invalid idtvec_info %#x for IDT task switch", + idtvec_info)); + intr_type = idtvec_info & VMCS_INTR_T_MASK; + if (intr_type != VMCS_INTR_T_SWINTR && + intr_type != VMCS_INTR_T_SWEXCEPTION && + intr_type != VMCS_INTR_T_PRIV_SWEXCEPTION) { + /* Task switch triggered by external event */ + ts->ext = 1; + vmexit->inst_length = 0; + if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) { + ts->errcode_valid = 1; + ts->errcode = (uint32_t) vmcs_idt_vectoring_err(vcpu); + } + } + } + vmexit->exitcode = VM_EXITCODE_TASK_SWITCH; + VCPU_CTR4(vmx->vm, vcpu, "task switch reason %d, tss 0x%04x, " + "%s errcode 0x%016llx", ts->reason, ts->tsssel, + ts->ext ? "external" : "internal", + ((uint64_t)ts->errcode << 32) | ((uint64_t) ts->errcode_valid)); + break; + case EXIT_REASON_CR_ACCESS: + vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CR_ACCESS, 1); + switch (qual & 0xf) { + case 0: + handled = vmx_emulate_cr0_access(vmx->vm, vcpu, qual); + break; + case 4: + handled = vmx_emulate_cr4_access(vcpu, qual); + break; + case 8: + handled = vmx_emulate_cr8_access(vmx, vcpu, qual); + break; + } + break; + case EXIT_REASON_RDMSR: + vmm_stat_incr(vmx->vm, vcpu, VMEXIT_RDMSR, 1); + retu = false; + ecx = (uint32_t) reg_read(vcpu, HV_X86_RCX); + VCPU_CTR1(vmx->vm, vcpu, "rdmsr 0x%08x", ecx); + // printf("EXIT_REASON_RDMSR 0x%08x\n", ecx); + error = emulate_rdmsr(vmx, vcpu, ecx, &retu); + if (error) { + vmexit->exitcode = VM_EXITCODE_RDMSR; + vmexit->u.msr.code = ecx; + } else if (!retu) { + handled = HANDLED; + } else { + /* Return to userspace with a valid exitcode */ + KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS, + ("emulate_rdmsr retu with bogus exitcode")); + } + break; + case EXIT_REASON_WRMSR: + vmm_stat_incr(vmx->vm, vcpu, VMEXIT_WRMSR, 1); + retu = false; + eax = (uint32_t) reg_read(vcpu, HV_X86_RAX); + ecx = (uint32_t) reg_read(vcpu, HV_X86_RCX); + edx = (uint32_t) reg_read(vcpu, HV_X86_RDX); + VCPU_CTR2(vmx->vm, vcpu, "wrmsr 0x%08x value 0x%016llx", + ecx, (uint64_t)edx << 32 | eax); + // printf("EXIT_REASON_WRMSR 0x%08x value 0x%016llx\n", + // ecx, (uint64_t)edx << 32 | eax); + error = emulate_wrmsr(vmx, vcpu, ecx, + (uint64_t)edx << 32 | eax, &retu); + if (error) { + vmexit->exitcode = VM_EXITCODE_WRMSR; + vmexit->u.msr.code = ecx; + vmexit->u.msr.wval = (uint64_t)edx << 32 | eax; + } else if (!retu) { + handled = HANDLED; + } else { + /* Return to userspace with a valid exitcode */ + KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS, + ("emulate_wrmsr retu with bogus exitcode")); + } + break; + case EXIT_REASON_HLT: + vmm_stat_incr(vmx->vm, vcpu, VMEXIT_HLT, 1); + vmexit->exitcode = VM_EXITCODE_HLT; + vmexit->u.hlt.rflags = vmcs_read(vcpu, VMCS_GUEST_RFLAGS); + break; + case EXIT_REASON_MTF: + vmm_stat_incr(vmx->vm, vcpu, VMEXIT_MTRAP, 1); + vmexit->exitcode = VM_EXITCODE_MTRAP; + vmexit->inst_length = 0; + break; + case EXIT_REASON_PAUSE: + vmm_stat_incr(vmx->vm, vcpu, VMEXIT_PAUSE, 1); + vmexit->exitcode = VM_EXITCODE_PAUSE; + break; + case EXIT_REASON_INTR_WINDOW: + vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INTR_WINDOW, 1); + vmx_clear_int_window_exiting(vmx, vcpu); + return (1); + case EXIT_REASON_EXT_INTR: + /* + * External interrupts serve only to cause VM exits and allow + * the host interrupt handler to run. + * + * If this external interrupt triggers a virtual interrupt + * to a VM, then that state will be recorded by the + * host interrupt handler in the VM's softc. We will inject + * this virtual interrupt during the subsequent VM enter. + */ + intr_info = (uint32_t) vmcs_read(vcpu, VMCS_EXIT_INTR_INFO); + + /* + * XXX: Ignore this exit if VMCS_INTR_VALID is not set. + * This appears to be a bug in VMware Fusion? + */ + if (!(intr_info & VMCS_INTR_VALID)) + return (1); + KASSERT((intr_info & VMCS_INTR_VALID) != 0 && + (intr_info & VMCS_INTR_T_MASK) == VMCS_INTR_T_HWINTR, + ("VM exit interruption info invalid: %#x", intr_info)); + + /* + * This is special. We want to treat this as an 'handled' + * VM-exit but not increment the instruction pointer. + */ + vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXTINT, 1); + return (1); + case EXIT_REASON_NMI_WINDOW: + /* Exit to allow the pending virtual NMI to be injected */ + if (vm_nmi_pending(vmx->vm, vcpu)) + vmx_inject_nmi(vmx, vcpu); + vmx_clear_nmi_window_exiting(vmx, vcpu); + vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NMI_WINDOW, 1); + return (1); + case EXIT_REASON_INOUT: + vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INOUT, 1); + vmexit->exitcode = VM_EXITCODE_INOUT; + vmexit->u.inout.bytes = (qual & 0x7) + 1; + vmexit->u.inout.in = in = (qual & 0x8) ? 1 : 0; + vmexit->u.inout.string = (qual & 0x10) ? 1 : 0; + vmexit->u.inout.rep = (qual & 0x20) ? 1 : 0; + vmexit->u.inout.port = (uint16_t)(qual >> 16); + vmexit->u.inout.eax = (uint32_t) reg_read(vcpu, HV_X86_RAX); + // if ((vmexit->u.inout.port == 0x0020) || + // (vmexit->u.inout.port == 0x0021) || + // (vmexit->u.inout.port == 0x00a0) || + // (vmexit->u.inout.port == 0x00a1)) + // { + // printf("EXIT_REASON_INOUT port 0x%03x in %d\n", + // vmexit->u.inout.port, vmexit->u.inout.in); + // } + if (vmexit->u.inout.string) { + inst_info = (uint32_t) vmcs_read(vcpu, VMCS_EXIT_INSTRUCTION_INFO); + vmexit->exitcode = VM_EXITCODE_INOUT_STR; + vis = &vmexit->u.inout_str; + vmx_paging_info(&vis->paging, vcpu); + vis->rflags = vmcs_read(vcpu, VMCS_GUEST_RFLAGS); + vis->cr0 = vmcs_read(vcpu, VMCS_GUEST_CR0); + vis->index = inout_str_index(vmx, vcpu, in); + vis->count = inout_str_count(vmx, vcpu, vis->inout.rep); + vis->addrsize = inout_str_addrsize(inst_info); + inout_str_seginfo(vmx, vcpu, inst_info, in, vis); + } + break; + case EXIT_REASON_CPUID: + vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CPUID, 1); + handled = vmx_handle_cpuid(vmx->vm, vcpu); + break; + case EXIT_REASON_EXCEPTION: + vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXCEPTION, 1); + intr_info = (uint32_t) vmcs_read(vcpu, VMCS_EXIT_INTR_INFO); + KASSERT((intr_info & VMCS_INTR_VALID) != 0, + ("VM exit interruption info invalid: %#x", intr_info)); + + intr_vec = intr_info & 0xff; + intr_type = intr_info & VMCS_INTR_T_MASK; + + /* + * If Virtual NMIs control is 1 and the VM-exit is due to a + * fault encountered during the execution of IRET then we must + * restore the state of "virtual-NMI blocking" before resuming + * the guest. + * + * See "Resuming Guest Software after Handling an Exception". + * See "Information for VM Exits Due to Vectored Events". + */ + if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 && + (intr_vec != IDT_DF) && + (intr_info & EXIT_QUAL_NMIUDTI) != 0) + vmx_restore_nmi_blocking(vmx, vcpu); + + /* + * The NMI has already been handled in vmx_exit_handle_nmi(). + */ + if (intr_type == VMCS_INTR_T_NMI) + return (1); + + if (intr_vec == IDT_PF) { + reg_write(vcpu, HV_X86_CR2, qual); + } + + /* + * Software exceptions exhibit trap-like behavior. This in + * turn requires populating the VM-entry instruction length + * so that the %rip in the trap frame is past the INT3/INTO + * instruction. + */ + if (intr_type == VMCS_INTR_T_SWEXCEPTION) + vmcs_write(vcpu, VMCS_ENTRY_INST_LENGTH, + ((uint64_t) vmexit->inst_length)); + + /* Reflect all other exceptions back into the guest */ + errcode_valid = errcode = 0; + if (intr_info & VMCS_INTR_DEL_ERRCODE) { + errcode_valid = 1; + errcode = (int) vmcs_read(vcpu, VMCS_EXIT_INTR_ERRCODE); + } + VCPU_CTR2(vmx->vm, vcpu, "Reflecting exception %d/%#x into " + "the guest", intr_vec, errcode); + error = vm_inject_exception(vmx->vm, vcpu, ((int) intr_vec), + errcode_valid, ((uint32_t) errcode), 0); + KASSERT(error == 0, ("%s: vm_inject_exception error %d", + __func__, error)); + return (1); + + case EXIT_REASON_EPT_FAULT: + /* + * If 'gpa' lies within the address space allocated to + * memory then this must be a nested page fault otherwise + * this must be an instruction that accesses MMIO space. + */ + gpa = vmcs_gpa(vcpu); + if (vm_mem_allocated(vmx->vm, gpa) || + apic_access_fault(vmx, vcpu, gpa)) { + vmexit->exitcode = VM_EXITCODE_PAGING; + vmexit->inst_length = 0; + vmexit->u.paging.gpa = gpa; + vmexit->u.paging.fault_type = ept_fault_type(qual); + vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NESTED_FAULT, 1); + } else if (ept_emulation_fault(qual)) { + vmexit_inst_emul(vmexit, gpa, vmcs_gla(vcpu), vcpu); + vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INST_EMUL, 1); + } + /* + * If Virtual NMIs control is 1 and the VM-exit is due to an + * EPT fault during the execution of IRET then we must restore + * the state of "virtual-NMI blocking" before resuming. + * + * See description of "NMI unblocking due to IRET" in + * "Exit Qualification for EPT Violations". + */ + if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 && + (qual & EXIT_QUAL_NMIUDTI) != 0) + vmx_restore_nmi_blocking(vmx, vcpu); + break; + case EXIT_REASON_VIRTUALIZED_EOI: + vmexit->exitcode = VM_EXITCODE_IOAPIC_EOI; + vmexit->u.ioapic_eoi.vector = qual & 0xFF; + vmexit->inst_length = 0; /* trap-like */ + break; + case EXIT_REASON_APIC_ACCESS: + handled = vmx_handle_apic_access(vmx, vcpu, vmexit); + break; + case EXIT_REASON_APIC_WRITE: + /* + * APIC-write VM exit is trap-like so the %rip is already + * pointing to the next instruction. + */ + vmexit->inst_length = 0; + vlapic = vm_lapic(vmx->vm, vcpu); + handled = vmx_handle_apic_write(vmx, vcpu, vlapic, qual); + break; + case EXIT_REASON_XSETBV: + handled = vmx_emulate_xsetbv(vmx, vcpu); + break; + case EXIT_REASON_MONITOR: + vmexit->exitcode = VM_EXITCODE_MONITOR; + break; + case EXIT_REASON_MWAIT: + vmexit->exitcode = VM_EXITCODE_MWAIT; + break; + default: + vmm_stat_incr(vmx->vm, vcpu, VMEXIT_UNKNOWN, 1); + break; + } + + if (handled) { + /* + * It is possible that control is returned to userland + * even though we were able to handle the VM exit in the + * kernel. + * + * In such a case we want to make sure that the userland + * restarts guest execution at the instruction *after* + * the one we just processed. Therefore we update the + * guest rip in the VMCS and in 'vmexit'. + */ + vmexit->rip += (uint64_t) vmexit->inst_length; + vmexit->inst_length = 0; + vmcs_write(vcpu, VMCS_GUEST_RIP, vmexit->rip); + } else { + if (vmexit->exitcode == VM_EXITCODE_BOGUS) { + /* + * If this VM exit was not claimed by anybody then + * treat it as a generic VMX exit. + */ + vmexit->exitcode = VM_EXITCODE_VMX; + vmexit->u.vmx.status = VM_SUCCESS; + vmexit->u.vmx.inst_type = 0; + vmexit->u.vmx.inst_error = 0; + } else { + /* + * The exitcode and collateral have been populated. + * The VM exit will be processed further in userland. + */ + } + } + return (handled); +} + +static int +vmx_run(void *arg, int vcpu, register_t rip, void *rendezvous_cookie, + void *suspend_cookie) +{ + int handled; + struct vmx *vmx; + struct vm *vm; + struct vm_exit *vmexit; + struct vlapic *vlapic; + uint32_t exit_reason; + hv_return_t hvr; + + vmx = arg; + vm = vmx->vm; + vlapic = vm_lapic(vm, vcpu); + vmexit = vm_exitinfo(vm, vcpu); + + vmcs_write(vcpu, VMCS_GUEST_RIP, ((uint64_t) rip)); + + do { + KASSERT(vmcs_guest_rip(vcpu) == ((uint64_t) rip), + ("%s: vmcs guest rip mismatch %#llx/%#llx", + __func__, vmcs_guest_rip(vcpu), ((uint64_t) rip))); + + handled = UNHANDLED; + + vmx_inject_interrupts(vmx, vcpu, vlapic, ((uint64_t) rip)); + + /* + * Check for vcpu suspension after injecting events because + * vmx_inject_interrupts() can suspend the vcpu due to a + * triple fault. + */ + if (vcpu_suspended(suspend_cookie)) { + vm_exit_suspended(vmx->vm, vcpu, ((uint64_t) rip)); + break; + } + + if (vcpu_rendezvous_pending(rendezvous_cookie)) { + vm_exit_rendezvous(vmx->vm, vcpu, ((uint64_t) rip)); + break; + } + + vmx_run_trace(vmx, vcpu); + hvr = hv_vcpu_run((hv_vcpuid_t) vcpu); + /* Collect some information for VM exit processing */ + rip = (register_t) vmcs_guest_rip(vcpu); + vmexit->rip = (uint64_t) rip; + vmexit->inst_length = (int) vmexit_instruction_length(vcpu); + vmexit->u.vmx.exit_reason = exit_reason = vmcs_exit_reason(vcpu); + vmexit->u.vmx.exit_qualification = vmcs_exit_qualification(vcpu); + /* Update 'nextrip' */ + vmx->state[vcpu].nextrip = (uint64_t) rip; + if (hvr == HV_SUCCESS) { + handled = vmx_exit_process(vmx, vcpu, vmexit); + } else { + hvdump(vcpu); + xhyve_abort("vmentry error\n"); + } + vmx_exit_trace(vmx, vcpu, ((uint64_t) rip), exit_reason, handled); + rip = (register_t) vmexit->rip; + } while (handled); + + /* + * If a VM exit has been handled then the exitcode must be BOGUS + * If a VM exit is not handled then the exitcode must not be BOGUS + */ + if ((handled && vmexit->exitcode != VM_EXITCODE_BOGUS) || + (!handled && vmexit->exitcode == VM_EXITCODE_BOGUS)) { + xhyve_abort("Mismatch between handled (%d) and exitcode (%d)", + handled, vmexit->exitcode); + } + + if (!handled) + vmm_stat_incr(vm, vcpu, VMEXIT_USERSPACE, 1); + + VCPU_CTR1(vm, vcpu, "returning from vmx_run: exitcode %d", + vmexit->exitcode); + + return (0); +} + +static void +vmx_vm_cleanup(void *arg) +{ + struct vmx *vmx = arg; + + free(vmx); + + return; +} + +static void +vmx_vcpu_cleanup(void *arg, int vcpuid) { + if (arg || vcpuid) xhyve_abort("vmx_vcpu_cleanup\n"); +} + + +static int +vmx_get_intr_shadow(int vcpu, uint64_t *retval) +{ + uint64_t gi; + int error; + + error = vmcs_getreg(vcpu, VMCS_IDENT(VMCS_GUEST_INTERRUPTIBILITY), &gi); + *retval = (gi & HWINTR_BLOCKING) ? 1 : 0; + return (error); +} + +static int +vmx_modify_intr_shadow(struct vmx *vmx, int vcpu, uint64_t val) +{ + uint64_t gi; + int error, ident; + + /* + * Forcing the vcpu into an interrupt shadow is not supported. + */ + if (val) { + error = EINVAL; + goto done; + } + + ident = VMCS_IDENT(VMCS_GUEST_INTERRUPTIBILITY); + error = vmcs_getreg(vcpu, ident, &gi); + if (error == 0) { + gi &= ~HWINTR_BLOCKING; + error = vmcs_setreg(vcpu, ident, gi); + } +done: + VCPU_CTR2(vmx->vm, vcpu, "Setting intr_shadow to %#llx %s", val, + error ? "failed" : "succeeded"); + return (error); +} + +static int +vmx_shadow_reg(int reg) +{ + int shreg; + + shreg = -1; + + switch (reg) { + case VM_REG_GUEST_CR0: + shreg = VMCS_CR0_SHADOW; + break; + case VM_REG_GUEST_CR4: + shreg = VMCS_CR4_SHADOW; + break; + default: + break; + } + + return (shreg); +} + +static const hv_x86_reg_t hvregs[] = { + HV_X86_RAX, + HV_X86_RBX, + HV_X86_RCX, + HV_X86_RDX, + HV_X86_RSI, + HV_X86_RDI, + HV_X86_RBP, + HV_X86_R8, + HV_X86_R9, + HV_X86_R10, + HV_X86_R11, + HV_X86_R12, + HV_X86_R13, + HV_X86_R14, + HV_X86_R15, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_CR2, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX, + HV_X86_REGISTERS_MAX +}; + +static int +vmx_getreg(UNUSED void *arg, int vcpu, int reg, uint64_t *retval) +{ + hv_x86_reg_t hvreg; + + if (reg == VM_REG_GUEST_INTR_SHADOW) + return (vmx_get_intr_shadow(vcpu, retval)); + + hvreg = hvregs[reg]; + if (hvreg != HV_X86_REGISTERS_MAX) { + *retval = reg_read(vcpu, hvreg); + return (0); + } + + return (vmcs_getreg(vcpu, reg, retval)); +} + +static int +vmx_setreg(void *arg, int vcpu, int reg, uint64_t val) +{ + int error, shadow; + uint64_t ctls; + hv_x86_reg_t hvreg; + struct vmx *vmx = arg; + + if (reg == VM_REG_GUEST_INTR_SHADOW) + return (vmx_modify_intr_shadow(vmx, vcpu, val)); + + hvreg = hvregs[reg]; + if (hvreg != HV_X86_REGISTERS_MAX) { + reg_write(vcpu, hvreg, val); + return (0); + } + + error = vmcs_setreg(vcpu, reg, val); + + if (error == 0) { + /* + * If the "load EFER" VM-entry control is 1 then the + * value of EFER.LMA must be identical to "IA-32e mode guest" + * bit in the VM-entry control. + */ + if ((entry_ctls & VM_ENTRY_LOAD_EFER) != 0 && + (reg == VM_REG_GUEST_EFER)) { + vmcs_getreg(vcpu, VMCS_IDENT(VMCS_ENTRY_CTLS), &ctls); + if (val & EFER_LMA) + ctls |= VM_ENTRY_GUEST_LMA; + else + ctls &= ~VM_ENTRY_GUEST_LMA; + vmcs_setreg(vcpu, VMCS_IDENT(VMCS_ENTRY_CTLS), ctls); + } + + shadow = vmx_shadow_reg(reg); + if (shadow > 0) { + /* + * Store the unmodified value in the shadow + */ + error = vmcs_setreg(vcpu, VMCS_IDENT(shadow), val); + } + + if (reg == VM_REG_GUEST_CR3) { + /* + * Invalidate the guest vcpu's TLB mappings to emulate + * the behavior of updating %cr3. + */ + hv_vcpu_invalidate_tlb((hv_vcpuid_t) vcpu); + } + } + + return (error); +} + +static int +vmx_getdesc(UNUSED void *arg, int vcpu, int reg, struct seg_desc *desc) +{ + return (vmcs_getdesc(vcpu, reg, desc)); +} + +static int +vmx_setdesc(UNUSED void *arg, int vcpu, int reg, struct seg_desc *desc) +{ + return (vmcs_setdesc(vcpu, reg, desc)); +} + +static int +vmx_getcap(void *arg, int vcpu, int type, int *retval) +{ + struct vmx *vmx = arg; + int vcap; + int ret; + + ret = ENOENT; + + vcap = vmx->cap[vcpu].set; + + switch (type) { + case VM_CAP_HALT_EXIT: + if (cap_halt_exit) + ret = 0; + break; + case VM_CAP_PAUSE_EXIT: + if (cap_pause_exit) + ret = 0; + break; + case VM_CAP_MTRAP_EXIT: + if (cap_monitor_trap) + ret = 0; + break; + default: + break; + } + + if (ret == 0) + *retval = (vcap & (1 << type)) ? 1 : 0; + + return (ret); +} + +static int +vmx_setcap(void *arg, int vcpu, int type, int val) +{ + struct vmx *vmx = arg; + uint32_t baseval; + uint32_t *pptr; + uint32_t reg; + uint32_t flag; + int retval; + + retval = ENOENT; + pptr = NULL; + baseval = 0; + reg = 0; + flag = 0; + + switch (type) { + case VM_CAP_HALT_EXIT: + if (cap_halt_exit) { + retval = 0; + pptr = &vmx->cap[vcpu].proc_ctls; + baseval = *pptr; + flag = PROCBASED_HLT_EXITING; + reg = VMCS_PRI_PROC_BASED_CTLS; + } + break; + case VM_CAP_MTRAP_EXIT: + if (cap_monitor_trap) { + retval = 0; + pptr = &vmx->cap[vcpu].proc_ctls; + baseval = *pptr; + flag = PROCBASED_MTF; + reg = VMCS_PRI_PROC_BASED_CTLS; + } + break; + case VM_CAP_PAUSE_EXIT: + if (cap_pause_exit) { + retval = 0; + pptr = &vmx->cap[vcpu].proc_ctls; + baseval = *pptr; + flag = PROCBASED_PAUSE_EXITING; + reg = VMCS_PRI_PROC_BASED_CTLS; + } + break; + default: + xhyve_abort("vmx_setcap\n"); + } + + if (retval == 0) { + if (val) { + baseval |= flag; + } else { + baseval &= ~flag; + } + + vmcs_write(vcpu, reg, baseval); + + /* + * Update optional stored flags, and record + * setting + */ + if (pptr != NULL) { + *pptr = baseval; + } + + if (val) { + vmx->cap[vcpu].set |= (1 << type); + } else { + vmx->cap[vcpu].set &= ~(1 << type); + } + + } + + return (retval); +} + +struct vlapic_vtx { + struct vlapic vlapic; + struct pir_desc *pir_desc; + struct vmx *vmx; +}; + +// #define VMX_CTR_PIR(vm, vcpuid, pir_desc, notify, vector, level, msg) \ +// do { \ +// VCPU_CTR2(vm, vcpuid, msg " assert %s-triggered vector %d", \ +// level ? "level" : "edge", vector); \ +// VCPU_CTR1(vm, vcpuid, msg " pir0 0x%016lx", pir_desc->pir[0]); \ +// VCPU_CTR1(vm, vcpuid, msg " pir1 0x%016lx", pir_desc->pir[1]); \ +// VCPU_CTR1(vm, vcpuid, msg " pir2 0x%016lx", pir_desc->pir[2]); \ +// VCPU_CTR1(vm, vcpuid, msg " pir3 0x%016lx", pir_desc->pir[3]); \ +// VCPU_CTR1(vm, vcpuid, msg " notify: %s", notify ? "yes" : "no");\ +// } while (0) + +// /* +// * vlapic->ops handlers that utilize the APICv hardware assist described in +// * Chapter 29 of the Intel SDM. +// */ +// static int +// vmx_set_intr_ready(struct vlapic *vlapic, int vector, bool level) +// { +// struct vlapic_vtx *vlapic_vtx; +// struct pir_desc *pir_desc; +// uint64_t mask; +// int idx, notify; + +// vlapic_vtx = (struct vlapic_vtx *)vlapic; +// pir_desc = vlapic_vtx->pir_desc; + +// /* +// * Keep track of interrupt requests in the PIR descriptor. This is +// * because the virtual APIC page pointed to by the VMCS cannot be +// * modified if the vcpu is running. +// */ +// idx = vector / 64; +// mask = 1UL << (vector % 64); +// atomic_set_long(&pir_desc->pir[idx], mask); +// notify = atomic_cmpset_long(&pir_desc->pending, 0, 1); + +// VMX_CTR_PIR(vlapic->vm, vlapic->vcpuid, pir_desc, notify, vector, +// level, "vmx_set_intr_ready"); +// return (notify); +// } + +// static int +// vmx_pending_intr(struct vlapic *vlapic, int *vecptr) +// { +// struct vlapic_vtx *vlapic_vtx; +// struct pir_desc *pir_desc; +// struct LAPIC *lapic; +// uint64_t pending, pirval; +// uint32_t ppr, vpr; +// int i; + +// /* +// * This function is only expected to be called from the 'HLT' exit +// * handler which does not care about the vector that is pending. +// */ +// KASSERT(vecptr == NULL, ("vmx_pending_intr: vecptr must be NULL")); + +// vlapic_vtx = (struct vlapic_vtx *)vlapic; +// pir_desc = vlapic_vtx->pir_desc; + +// pending = atomic_load_acq_long(&pir_desc->pending); +// if (!pending) +// return (0); /* common case */ + +// /* +// * If there is an interrupt pending then it will be recognized only +// * if its priority is greater than the processor priority. +// * +// * Special case: if the processor priority is zero then any pending +// * interrupt will be recognized. +// */ +// lapic = vlapic->apic_page; +// ppr = lapic->ppr & 0xf0; +// if (ppr == 0) +// return (1); + +// VCPU_CTR1(vlapic->vm, vlapic->vcpuid, "HLT with non-zero PPR %d", +// lapic->ppr); + +// for (i = 3; i >= 0; i--) { +// pirval = pir_desc->pir[i]; +// if (pirval != 0) { +// vpr = (i * 64 + flsl(pirval) - 1) & 0xf0; +// return (vpr > ppr); +// } +// } +// return (0); +// } + +// static void +// vmx_intr_accepted(struct vlapic *vlapic, int vector) +// { + +// xhyve_abort("vmx_intr_accepted: not expected to be called"); +// } + +// static void +// vmx_set_tmr(struct vlapic *vlapic, int vector, bool level) +// { +// struct vlapic_vtx *vlapic_vtx; +// struct vmx *vmx; +// struct vmcs *vmcs; +// uint64_t mask, val; + +// KASSERT(vector >= 0 && vector <= 255, ("invalid vector %d", vector)); +// KASSERT(!vcpu_is_running(vlapic->vm, vlapic->vcpuid, NULL), +// ("vmx_set_tmr: vcpu cannot be running")); + +// vlapic_vtx = (struct vlapic_vtx *)vlapic; +// vmx = vlapic_vtx->vmx; +// vmcs = &vmx->vmcs[vlapic->vcpuid]; +// mask = 1UL << (vector % 64); + +// VMPTRLD(vmcs); +// val = vmcs_read(VMCS_EOI_EXIT(vector)); +// if (level) +// val |= mask; +// else +// val &= ~mask; +// vmcs_write(VMCS_EOI_EXIT(vector), val); +// VMCLEAR(vmcs); +// } + +// static void +// vmx_enable_x2apic_mode(struct vlapic *vlapic) +// { +// struct vmx *vmx; +// struct vmcs *vmcs; +// uint32_t proc_ctls2; +// int vcpuid, error; + +// vcpuid = vlapic->vcpuid; +// vmx = ((struct vlapic_vtx *)vlapic)->vmx; +// vmcs = &vmx->vmcs[vcpuid]; + +// proc_ctls2 = vmx->cap[vcpuid].proc_ctls2; +// KASSERT((proc_ctls2 & PROCBASED2_VIRTUALIZE_APIC_ACCESSES) != 0, +// ("%s: invalid proc_ctls2 %#x", __func__, proc_ctls2)); + +// proc_ctls2 &= ~PROCBASED2_VIRTUALIZE_APIC_ACCESSES; +// proc_ctls2 |= PROCBASED2_VIRTUALIZE_X2APIC_MODE; +// vmx->cap[vcpuid].proc_ctls2 = proc_ctls2; + +// VMPTRLD(vmcs); +// vmcs_write(VMCS_SEC_PROC_BASED_CTLS, proc_ctls2); +// VMCLEAR(vmcs); + +// if (vlapic->vcpuid == 0) { +// /* +// * The nested page table mappings are shared by all vcpus +// * so unmap the APIC access page just once. +// */ +// error = vm_unmap_mmio(vmx->vm, DEFAULT_APIC_BASE, PAGE_SIZE); +// KASSERT(error == 0, ("%s: vm_unmap_mmio error %d", +// __func__, error)); + +// /* +// * The MSR bitmap is shared by all vcpus so modify it only +// * once in the context of vcpu 0. +// */ +// error = vmx_allow_x2apic_msrs(vmx); +// KASSERT(error == 0, ("%s: vmx_allow_x2apic_msrs error %d", +// __func__, error)); +// } +// } + +static struct vlapic * +vmx_vlapic_init(void *arg, int vcpuid) +{ + struct vmx *vmx; + struct vlapic *vlapic; + struct vlapic_vtx *vlapic_vtx; + + vmx = arg; + + vlapic = malloc(sizeof(struct vlapic_vtx)); + assert(vlapic); + bzero(vlapic, sizeof(struct vlapic)); + vlapic->vm = vmx->vm; + vlapic->vcpuid = vcpuid; + vlapic->apic_page = (struct LAPIC *)&vmx->apic_page[vcpuid]; + + vlapic_vtx = (struct vlapic_vtx *)vlapic; + vlapic_vtx->vmx = vmx; + + vlapic_init(vlapic); + + return (vlapic); +} + +static void +vmx_vlapic_cleanup(UNUSED void *arg, struct vlapic *vlapic) +{ + vlapic_cleanup(vlapic); + free(vlapic); +} + +static void +vmx_vcpu_interrupt(int vcpu) { + hv_vcpuid_t hvvcpu; + + hvvcpu = (hv_vcpuid_t) vcpu; + + hv_vcpu_interrupt(&hvvcpu, 1); +} + +struct vmm_ops vmm_ops_intel = { + vmx_init, + vmx_cleanup, + vmx_vm_init, + vmx_vcpu_init, + vmx_run, + vmx_vm_cleanup, + vmx_vcpu_cleanup, + vmx_getreg, + vmx_setreg, + vmx_getdesc, + vmx_setdesc, + vmx_getcap, + vmx_setcap, + vmx_vlapic_init, + vmx_vlapic_cleanup, + vmx_vcpu_interrupt +}; diff --git a/src/vmm/intel/vmx_msr.c b/src/vmm/intel/vmx_msr.c new file mode 100644 index 0000000..9612bee --- /dev/null +++ b/src/vmm/intel/vmx_msr.c @@ -0,0 +1,353 @@ +/*- + * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static bool +vmx_ctl_allows_one_setting(uint64_t msr_val, int bitpos) +{ + if (msr_val & (1UL << (bitpos + 32))) + return (TRUE); + else + return (FALSE); +} + +static bool +vmx_ctl_allows_zero_setting(uint64_t msr_val, int bitpos) +{ + if ((msr_val & (1UL << bitpos)) == 0) + return (TRUE); + else + return (FALSE); +} + +int vmx_set_ctlreg(hv_vmx_capability_t cap_field, uint32_t ones_mask, + uint32_t zeros_mask, uint32_t *retval) +{ + int i; + uint64_t cap; + bool one_allowed, zero_allowed; + + /* We cannot ask the same bit to be set to both '1' and '0' */ + if ((ones_mask ^ zeros_mask) != (ones_mask | zeros_mask)) { + return EINVAL; + } + + if (hv_vmx_read_capability(cap_field, &cap)) { + return EINVAL; + } + + for (i = 0; i < 32; i++) { + one_allowed = vmx_ctl_allows_one_setting(cap, i); + zero_allowed = vmx_ctl_allows_zero_setting(cap, i); + + if (zero_allowed && !one_allowed) { + /* must be zero */ + if (ones_mask & (1 << i)) { + fprintf(stderr, + "vmx_set_ctlreg: cap_field: %d bit: %d must be zero\n", + cap_field, i); + return (EINVAL); + } + *retval &= ~(1 << i); + } else if (one_allowed && !zero_allowed) { + /* must be one */ + if (zeros_mask & (1 << i)) { + fprintf(stderr, + "vmx_set_ctlreg: cap_field: %d bit: %d must be one\n", + cap_field, i); + return (EINVAL); + } + *retval |= 1 << i; + } else { + /* don't care */ + if (zeros_mask & (1 << i)){ + *retval &= ~(1 << i); + } else if (ones_mask & (1 << i)) { + *retval |= 1 << i; + } else { + /* XXX: don't allow unspecified don't cares */ + fprintf(stderr, + "vmx_set_ctlreg: cap_field: %d bit: %d unspecified " + "don't care\n", cap_field, i); + return (EINVAL); + } + } + } + + return (0); +} + +static uint64_t misc_enable; +static uint64_t platform_info; +static uint64_t turbo_ratio_limit; + +static bool +pat_valid(uint64_t val) +{ + int i, pa; + + /* + * From Intel SDM: Table "Memory Types That Can Be Encoded With PAT" + * + * Extract PA0 through PA7 and validate that each one encodes a + * valid memory type. + */ + for (i = 0; i < 8; i++) { + pa = (val >> (i * 8)) & 0xff; + if (pa == 2 || pa == 3 || pa >= 8) + return (false); + } + return (true); +} + +void +vmx_msr_init(void) { + uint64_t bus_freq, tsc_freq, ratio; + size_t length; + int i; + + length = sizeof(uint64_t); + + if (sysctlbyname("machdep.tsc.frequency", &tsc_freq, &length, NULL, 0)) { + xhyve_abort("machdep.tsc.frequency\n"); + } + + if (sysctlbyname("hw.busfrequency", &bus_freq, &length, NULL, 0)) { + xhyve_abort("hw.busfrequency\n"); + } + + /* Initialize emulated MSRs */ + /* FIXME */ + misc_enable = 1; + /* + * Set mandatory bits + * 11: branch trace disabled + * 12: PEBS unavailable + * Clear unsupported features + * 16: SpeedStep enable + * 18: enable MONITOR FSM + */ + misc_enable |= (1u << 12) | (1u << 11); + misc_enable &= ~((1u << 18) | (1u << 16)); + + /* + * XXXtime + * The ratio should really be based on the virtual TSC frequency as + * opposed to the host TSC. + */ + ratio = (tsc_freq / bus_freq) & 0xff; + + /* + * The register definition is based on the micro-architecture + * but the following bits are always the same: + * [15:8] Maximum Non-Turbo Ratio + * [28] Programmable Ratio Limit for Turbo Mode + * [29] Programmable TDC-TDP Limit for Turbo Mode + * [47:40] Maximum Efficiency Ratio + * + * The other bits can be safely set to 0 on all + * micro-architectures up to Haswell. + */ + platform_info = (ratio << 8) | (ratio << 40); + + /* + * The number of valid bits in the MSR_TURBO_RATIO_LIMITx register is + * dependent on the maximum cores per package supported by the micro- + * architecture. For e.g., Westmere supports 6 cores per package and + * uses the low 48 bits. Sandybridge support 8 cores per package and + * uses up all 64 bits. + * + * However, the unused bits are reserved so we pretend that all bits + * in this MSR are valid. + */ + for (i = 0; i < 8; i++) { + turbo_ratio_limit = (turbo_ratio_limit << 8) | ratio; + } +} + +void +vmx_msr_guest_init(struct vmx *vmx, int vcpuid) +{ + uint64_t *guest_msrs; + + guest_msrs = vmx->guest_msrs[vcpuid]; + + + hv_vcpu_enable_native_msr(((hv_vcpuid_t) vcpuid), MSR_LSTAR, 1); + hv_vcpu_enable_native_msr(((hv_vcpuid_t) vcpuid), MSR_CSTAR, 1); + hv_vcpu_enable_native_msr(((hv_vcpuid_t) vcpuid), MSR_STAR, 1); + hv_vcpu_enable_native_msr(((hv_vcpuid_t) vcpuid), MSR_SF_MASK, 1); + hv_vcpu_enable_native_msr(((hv_vcpuid_t) vcpuid), MSR_KGSBASE, 1); + + /* + * Initialize guest IA32_PAT MSR with default value after reset. + */ + guest_msrs[IDX_MSR_PAT] = PAT_VALUE(0, PAT_WRITE_BACK) | + PAT_VALUE(1, PAT_WRITE_THROUGH) | + PAT_VALUE(2, PAT_UNCACHED) | + PAT_VALUE(3, PAT_UNCACHEABLE) | + PAT_VALUE(4, PAT_WRITE_BACK) | + PAT_VALUE(5, PAT_WRITE_THROUGH) | + PAT_VALUE(6, PAT_UNCACHED) | + PAT_VALUE(7, PAT_UNCACHEABLE); + + return; +} + +int +vmx_rdmsr(struct vmx *vmx, int vcpuid, u_int num, uint64_t *val) +{ + const uint64_t *guest_msrs; + int error; + + guest_msrs = vmx->guest_msrs[vcpuid]; + error = 0; + + switch (num) { + case MSR_EFER: + *val = vmcs_read(vcpuid, VMCS_GUEST_IA32_EFER); + break; + case MSR_MCG_CAP: + case MSR_MCG_STATUS: + *val = 0; + break; + case MSR_MTRRcap: + case MSR_MTRRdefType: + case MSR_MTRR4kBase: + case MSR_MTRR4kBase + 1: + case MSR_MTRR4kBase + 2: + case MSR_MTRR4kBase + 3: + case MSR_MTRR4kBase + 4: + case MSR_MTRR4kBase + 5: + case MSR_MTRR4kBase + 6: + case MSR_MTRR4kBase + 7: + case MSR_MTRR4kBase + 8: + case MSR_MTRR16kBase: + case MSR_MTRR16kBase + 1: + case MSR_MTRR64kBase: + *val = 0; + break; + case MSR_IA32_MISC_ENABLE: + *val = misc_enable; + break; + case MSR_PLATFORM_INFO: + *val = platform_info; + break; + case MSR_TURBO_RATIO_LIMIT: + case MSR_TURBO_RATIO_LIMIT1: + *val = turbo_ratio_limit; + break; + case MSR_PAT: + *val = guest_msrs[IDX_MSR_PAT]; + break; + default: + error = EINVAL; + break; + } + return (error); +} + +int +vmx_wrmsr(struct vmx *vmx, int vcpuid, u_int num, uint64_t val) +{ + uint64_t *guest_msrs; + uint64_t changed; + int error; + + guest_msrs = vmx->guest_msrs[vcpuid]; + error = 0; + + switch (num) { + case MSR_EFER: + vmcs_write(vcpuid, VMCS_GUEST_IA32_EFER, val); + break; + case MSR_MCG_CAP: + case MSR_MCG_STATUS: + break; /* ignore writes */ + case MSR_MTRRcap: + vm_inject_gp(vmx->vm, vcpuid); + break; + case MSR_MTRRdefType: + case MSR_MTRR4kBase: + case MSR_MTRR4kBase + 1: + case MSR_MTRR4kBase + 2: + case MSR_MTRR4kBase + 3: + case MSR_MTRR4kBase + 4: + case MSR_MTRR4kBase + 5: + case MSR_MTRR4kBase + 6: + case MSR_MTRR4kBase + 7: + case MSR_MTRR4kBase + 8: + case MSR_MTRR16kBase: + case MSR_MTRR16kBase + 1: + case MSR_MTRR64kBase: + break; /* Ignore writes */ + case MSR_IA32_MISC_ENABLE: + changed = val ^ misc_enable; + /* + * If the host has disabled the NX feature then the guest + * also cannot use it. However, a Linux guest will try to + * enable the NX feature by writing to the MISC_ENABLE MSR. + * + * This can be safely ignored because the memory management + * code looks at CPUID.80000001H:EDX.NX to check if the + * functionality is actually enabled. + */ + changed &= ~(1UL << 34); + + /* + * Punt to userspace if any other bits are being modified. + */ + if (changed) + error = EINVAL; + + break; + case MSR_PAT: + if (pat_valid(val)) + guest_msrs[IDX_MSR_PAT] = val; + else + vm_inject_gp(vmx->vm, vcpuid); + break; + default: + error = EINVAL; + break; + } + + return (error); +} diff --git a/vmm/io/vatpic.c b/src/vmm/io/vatpic.c similarity index 88% rename from vmm/io/vatpic.c rename to src/vmm/io/vatpic.c index 6e94f5b..0b0bb25 100644 --- a/vmm/io/vatpic.c +++ b/src/vmm/io/vatpic.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2014 Tycho Nightingale + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -24,33 +25,22 @@ * SUCH DAMAGE. */ -#include -__FBSDID("$FreeBSD$"); +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include - -#include "vmm_ktr.h" -#include "vmm_lapic.h" -#include "vioapic.h" -#include "vatpic.h" - -static MALLOC_DEFINE(M_VATPIC, "atpic", "bhyve virtual atpic (8259)"); - -#define VATPIC_LOCK(vatpic) mtx_lock_spin(&((vatpic)->mtx)) -#define VATPIC_UNLOCK(vatpic) mtx_unlock_spin(&((vatpic)->mtx)) -#define VATPIC_LOCKED(vatpic) mtx_owned(&((vatpic)->mtx)) +#define VATPIC_LOCK_INIT(v) (v)->lock = OS_SPINLOCK_INIT; +#define VATPIC_LOCK(v) OSSpinLockLock(&(v)->lock) +#define VATPIC_UNLOCK(v) OSSpinLockUnlock(&(v)->lock) enum irqstate { IRQSTATE_ASSERT, @@ -58,34 +48,33 @@ enum irqstate { IRQSTATE_PULSE }; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct atpic { - bool ready; - int icw_num; - int rd_cmd_reg; - - bool aeoi; - bool poll; - bool rotate; - bool sfn; /* special fully-nested mode */ - - int irq_base; - uint8_t request; /* Interrupt Request Register (IIR) */ - uint8_t service; /* Interrupt Service (ISR) */ - uint8_t mask; /* Interrupt Mask Register (IMR) */ - uint8_t smm; /* special mask mode */ - - int acnt[8]; /* sum of pin asserts and deasserts */ - int lowprio; /* lowest priority irq */ - - bool intr_raised; + bool ready; + int icw_num; + int rd_cmd_reg; + bool aeoi; + bool poll; + bool rotate; + bool sfn; /* special fully-nested mode */ + int irq_base; + uint8_t request; /* Interrupt Request Register (IIR) */ + uint8_t service; /* Interrupt Service (ISR) */ + uint8_t mask; /* Interrupt Mask Register (IMR) */ + uint8_t smm; /* special mask mode */ + int acnt[8]; /* sum of pin asserts and deasserts */ + int lowprio; /* lowest priority irq */ + bool intr_raised; }; struct vatpic { - struct vm *vm; - struct mtx mtx; - struct atpic atpic[2]; - uint8_t elc[2]; + struct vm *vm; + OSSpinLock lock; + struct atpic atpic[2]; + uint8_t elc[2]; }; +#pragma clang diagnostic pop #define VATPIC_CTR0(vatpic, fmt) \ VM_CTR0((vatpic)->vm, fmt) @@ -197,8 +186,6 @@ vatpic_notify_intr(struct vatpic *vatpic) struct atpic *atpic; int pin; - KASSERT(VATPIC_LOCKED(vatpic), ("vatpic_notify_intr not locked")); - /* * First check the slave. */ @@ -420,8 +407,6 @@ vatpic_set_pinstate(struct vatpic *vatpic, int pin, bool newstate) KASSERT(pin >= 0 && pin < 16, ("vatpic_set_pinstate: invalid pin number %d", pin)); - KASSERT(VATPIC_LOCKED(vatpic), - ("vatpic_set_pinstate: vatpic is not locked")); atpic = &vatpic->atpic[pin >> 3]; @@ -482,8 +467,6 @@ vatpic_set_irqstate(struct vm *vm, int irq, enum irqstate irqstate) vatpic_set_pinstate(vatpic, irq, true); vatpic_set_pinstate(vatpic, irq, false); break; - default: - panic("vatpic_set_irqstate: invalid irqstate %d", irqstate); } VATPIC_UNLOCK(vatpic); @@ -622,11 +605,10 @@ vatpic_intr_accepted(struct vm *vm, int vector) } static int -vatpic_read(struct vatpic *vatpic, struct atpic *atpic, bool in, int port, - int bytes, uint32_t *eax) +vatpic_read(struct vatpic *vatpic, struct atpic *atpic, UNUSED bool in, + int port, UNUSED int bytes, uint32_t *eax) { int pin; - VATPIC_LOCK(vatpic); if (atpic->poll) { @@ -634,7 +616,7 @@ vatpic_read(struct vatpic *vatpic, struct atpic *atpic, bool in, int port, pin = vatpic_get_highest_irrpin(atpic); if (pin >= 0) { vatpic_pin_accepted(atpic, pin); - *eax = 0x80 | pin; + *eax = 0x80 | ((uint32_t) pin); } else { *eax = 0; } @@ -654,21 +636,21 @@ vatpic_read(struct vatpic *vatpic, struct atpic *atpic, bool in, int port, } VATPIC_UNLOCK(vatpic); - +//printf("vatpic_read 0x%04x 0x%02x\n", port, (uint8_t)*eax); return (0); } static int -vatpic_write(struct vatpic *vatpic, struct atpic *atpic, bool in, int port, - int bytes, uint32_t *eax) +vatpic_write(struct vatpic *vatpic, struct atpic *atpic, UNUSED bool in, + int port, UNUSED int bytes, uint32_t *eax) { int error; uint8_t val; error = 0; - val = *eax; - + val = (uint8_t) *eax; +//printf("vatpic_write 0x%04x 0x%02x %d\n", port, val, atpic->icw_num); VATPIC_LOCK(vatpic); if (port & ICU_IMR_OFFSET) { @@ -707,8 +689,8 @@ vatpic_write(struct vatpic *vatpic, struct atpic *atpic, bool in, int port, } int -vatpic_master_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, - uint32_t *eax) +vatpic_master_handler(struct vm *vm, UNUSED int vcpuid, bool in, int port, + int bytes, uint32_t *eax) { struct vatpic *vatpic; struct atpic *atpic; @@ -727,8 +709,8 @@ vatpic_master_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, } int -vatpic_slave_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, - uint32_t *eax) +vatpic_slave_handler(struct vm *vm, UNUSED int vcpuid, bool in, int port, + int bytes, uint32_t *eax) { struct vatpic *vatpic; struct atpic *atpic; @@ -747,8 +729,8 @@ vatpic_slave_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, } int -vatpic_elc_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, - uint32_t *eax) +vatpic_elc_handler(struct vm *vm, UNUSED int vcpuid, bool in, int port, + int bytes, uint32_t *eax) { struct vatpic *vatpic; bool is_master; @@ -793,10 +775,12 @@ vatpic_init(struct vm *vm) { struct vatpic *vatpic; - vatpic = malloc(sizeof(struct vatpic), M_VATPIC, M_WAITOK | M_ZERO); + vatpic = malloc(sizeof(struct vatpic)); + assert(vatpic); + bzero(vatpic, sizeof(struct vatpic)); vatpic->vm = vm; - mtx_init(&vatpic->mtx, "vatpic lock", NULL, MTX_SPIN); + VATPIC_LOCK_INIT(vatpic); return (vatpic); } @@ -804,5 +788,5 @@ vatpic_init(struct vm *vm) void vatpic_cleanup(struct vatpic *vatpic) { - free(vatpic, M_VATPIC); + free(vatpic); } diff --git a/vmm/io/vatpit.c b/src/vmm/io/vatpit.c similarity index 83% rename from vmm/io/vatpit.c rename to src/vmm/io/vatpit.c index 173ef1f..5c59425 100644 --- a/vmm/io/vatpit.c +++ b/src/vmm/io/vatpit.c @@ -1,6 +1,7 @@ /*- * Copyright (c) 2014 Tycho Nightingale * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -25,30 +26,20 @@ * SUCH DAMAGE. */ -#include -__FBSDID("$FreeBSD$"); +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "vmm_ktr.h" -#include "vatpic.h" -#include "vioapic.h" -#include "vatpit.h" - -static MALLOC_DEFINE(M_VATPIT, "atpit", "bhyve virtual atpit (8254)"); - -#define VATPIT_LOCK(vatpit) mtx_lock_spin(&((vatpit)->mtx)) -#define VATPIT_UNLOCK(vatpit) mtx_unlock_spin(&((vatpit)->mtx)) -#define VATPIT_LOCKED(vatpit) mtx_owned(&((vatpit)->mtx)) +#define VATPIT_LOCK_INIT(v) (v)->lock = OS_SPINLOCK_INIT; +#define VATPIT_LOCK(v) OSSpinLockLock(&(v)->lock) +#define VATPIT_UNLOCK(v) OSSpinLockUnlock(&(v)->lock) #define TIMER_SEL_MASK 0xc0 #define TIMER_RW_MASK 0x30 @@ -69,36 +60,36 @@ static MALLOC_DEFINE(M_VATPIT, "atpit", "bhyve virtual atpit (8254)"); #define PIT_8254_FREQ 1193182 #define TIMER_DIV(freq, hz) (((freq) + (hz) / 2) / (hz)) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct vatpit_callout_arg { - struct vatpit *vatpit; - int channel_num; + struct vatpit *vatpit; + int channel_num; }; - struct channel { - int mode; - uint16_t initial; /* initial counter value */ - sbintime_t now_sbt; /* uptime when counter was loaded */ - uint8_t cr[2]; - uint8_t ol[2]; - bool slatched; /* status latched */ - uint8_t status; - int crbyte; - int olbyte; - int frbyte; - struct callout callout; - sbintime_t callout_sbt; /* target time */ + int mode; + uint16_t initial; /* initial counter value */ + sbintime_t now_sbt; /* uptime when counter was loaded */ + uint8_t cr[2]; + uint8_t ol[2]; + bool slatched; /* status latched */ + uint8_t status; + int crbyte; + int olbyte; + int frbyte; + struct callout callout; + sbintime_t callout_sbt; /* target time */ struct vatpit_callout_arg callout_arg; }; struct vatpit { - struct vm *vm; - struct mtx mtx; - - sbintime_t freq_sbt; - - struct channel channel[3]; + struct vm *vm; + OSSpinLock lock; + sbintime_t freq_sbt; + struct channel channel[3]; }; +#pragma clang diagnostic pop static void pit_timer_start_cntr0(struct vatpit *vatpit); @@ -164,12 +155,11 @@ static void pit_timer_start_cntr0(struct vatpit *vatpit) { struct channel *c; - sbintime_t now, delta, precision; + sbintime_t now, delta; c = &vatpit->channel[0]; if (c->initial != 0) { delta = c->initial * vatpit->freq_sbt; - precision = delta >> tc_precexp; c->callout_sbt = c->callout_sbt + delta; /* @@ -182,7 +172,7 @@ pit_timer_start_cntr0(struct vatpit *vatpit) c->callout_sbt = now + delta; callout_reset_sbt(&c->callout, c->callout_sbt, - precision, vatpit_callout_handler, &c->callout_arg, + 0, vatpit_callout_handler, &c->callout_arg, C_ABSOLUTE); } } @@ -217,8 +207,8 @@ pit_update_counter(struct vatpit *vatpit, struct channel *c, bool latch) if (latch) { c->olbyte = 2; - c->ol[1] = lval; /* LSB */ - c->ol[0] = lval >> 8; /* MSB */ + c->ol[1] = (uint8_t) lval; /* LSB */ + c->ol[0] = (uint8_t) (lval >> 8); /* MSB */ } return (lval); @@ -316,7 +306,7 @@ vatpit_update_mode(struct vatpit *vatpit, uint8_t val) } int -vatpit_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, +vatpit_handler(struct vm *vm, UNUSED int vcpuid, bool in, int port, int bytes, uint32_t *eax) { struct vatpit *vatpit; @@ -329,7 +319,7 @@ vatpit_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, if (bytes != 1) return (-1); - val = *eax; + val = (uint8_t) *eax; if (port == TIMER_MODE) { if (in) { @@ -346,7 +336,7 @@ vatpit_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, /* counter ports */ KASSERT(port >= TIMER_CNTR0 && port <= TIMER_CNTR2, - ("invalid port 0x%x", port)); + ("invalid port 0x%x\n", port)); c = &vatpit->channel[port - TIMER_CNTR0]; VATPIT_LOCK(vatpit); @@ -377,12 +367,13 @@ vatpit_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, } else *eax = c->ol[--c->olbyte]; } else { - c->cr[c->crbyte++] = *eax; + c->cr[c->crbyte++] = (uint8_t) *eax; if (c->crbyte == 2) { c->status &= ~TIMER_STS_NULLCNT; c->frbyte = 0; c->crbyte = 0; - c->initial = c->cr[0] | (uint16_t)c->cr[1] << 8; + c->initial = (uint16_t) c->cr[0]; + c->initial |= (((uint16_t) c->cr[1]) << 8); c->now_sbt = sbinuptime(); /* Start an interval timer for channel 0 */ if (port == TIMER_CNTR0) { @@ -399,8 +390,8 @@ vatpit_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, } int -vatpit_nmisc_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, - uint32_t *eax) +vatpit_nmisc_handler(struct vm *vm, UNUSED int vcpuid, bool in, UNUSED int port, + UNUSED int bytes, uint32_t *eax) { struct vatpit *vatpit; @@ -427,10 +418,12 @@ vatpit_init(struct vm *vm) struct vatpit_callout_arg *arg; int i; - vatpit = malloc(sizeof(struct vatpit), M_VATPIT, M_WAITOK | M_ZERO); + vatpit = malloc(sizeof(struct vatpit)); + assert(vatpit); + bzero(vatpit, sizeof(struct vatpit)); vatpit->vm = vm; - mtx_init(&vatpit->mtx, "vatpit lock", NULL, MTX_SPIN); + VATPIT_LOCK_INIT(vatpit) FREQ2BT(PIT_8254_FREQ, &bt); vatpit->freq_sbt = bttosbt(bt); @@ -453,5 +446,5 @@ vatpit_cleanup(struct vatpit *vatpit) for (i = 0; i < 3; i++) callout_drain(&vatpit->channel[i].callout); - free(vatpit, M_VATPIT); + free(vatpit); } diff --git a/vmm/io/vhpet.c b/src/vmm/io/vhpet.c similarity index 86% rename from vmm/io/vhpet.c rename to src/vmm/io/vhpet.c index 1db1c51..1d81861 100644 --- a/vmm/io/vhpet.c +++ b/src/vmm/io/vhpet.c @@ -1,6 +1,7 @@ /*- * Copyright (c) 2013 Tycho Nightingale * Copyright (c) 2013 Neel Natu + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,29 +28,18 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include -#include - -#include - -#include -#include - -#include "vmm_lapic.h" -#include "vatpic.h" -#include "vioapic.h" -#include "vhpet.h" - -#include "vmm_ktr.h" - -static MALLOC_DEFINE(M_VHPET, "vhpet", "bhyve virtual hpet"); +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #define HPET_FREQ 10000000 /* 10.0 Mhz */ #define FS_PER_S 1000000000000000ul @@ -65,34 +55,35 @@ static MALLOC_DEFINE(M_VHPET, "vhpet", "bhyve virtual hpet"); #define VHPET_NUM_TIMERS 8 CTASSERT(VHPET_NUM_TIMERS >= 3 && VHPET_NUM_TIMERS <= 32); +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct vhpet_callout_arg { struct vhpet *vhpet; int timer_num; }; struct vhpet { - struct vm *vm; - struct mtx mtx; - sbintime_t freq_sbt; - - uint64_t config; /* Configuration */ - uint64_t isr; /* Interrupt Status */ - uint32_t countbase; /* HPET counter base value */ - sbintime_t countbase_sbt; /* uptime corresponding to base value */ - + struct vm *vm; + pthread_mutex_t mtx; + sbintime_t freq_sbt; + uint64_t config; /* Configuration */ + uint64_t isr; /* Interrupt Status */ + uint32_t countbase; /* HPET counter base value */ + sbintime_t countbase_sbt; /* uptime corresponding to base value */ struct { - uint64_t cap_config; /* Configuration */ - uint64_t msireg; /* FSB interrupt routing */ - uint32_t compval; /* Comparator */ - uint32_t comprate; - struct callout callout; - sbintime_t callout_sbt; /* time when counter==compval */ + uint64_t cap_config; /* Configuration */ + uint64_t msireg; /* FSB interrupt routing */ + uint32_t compval; /* Comparator */ + uint32_t comprate; + struct callout callout; + sbintime_t callout_sbt; /* time when counter==compval */ struct vhpet_callout_arg arg; } timer[VHPET_NUM_TIMERS]; }; +#pragma clang diagnostic pop -#define VHPET_LOCK(vhp) mtx_lock(&((vhp)->mtx)) -#define VHPET_UNLOCK(vhp) mtx_unlock(&((vhp)->mtx)) +#define VHPET_LOCK(vhp) pthread_mutex_lock(&((vhp)->mtx)) +#define VHPET_UNLOCK(vhp) pthread_mutex_unlock(&((vhp)->mtx)) static void vhpet_start_timer(struct vhpet *vhpet, int n, uint32_t counter, sbintime_t now); @@ -102,13 +93,12 @@ vhpet_capabilities(void) { uint64_t cap = 0; - cap |= 0x8086 << 16; /* vendor id */ - cap |= (VHPET_NUM_TIMERS - 1) << 8; /* number of timers */ - cap |= 1; /* revision */ - cap &= ~HPET_CAP_COUNT_SIZE; /* 32-bit timer */ - - cap &= 0xffffffff; - cap |= (FS_PER_S / HPET_FREQ) << 32; /* tick period in fs */ + cap |= ((uint64_t) 0x8086) << 16; /* vendor id */ + cap |= ((uint64_t) (VHPET_NUM_TIMERS - 1)) << 8; /* number of timers */ + cap |= (uint64_t) 1; /* revision */ + cap &= ~((uint64_t) HPET_CAP_COUNT_SIZE); /* 32-bit timer */ + cap &= (uint64_t) 0xffffffff; + cap |= ((uint64_t) (FS_PER_S / HPET_FREQ)) << 32; /* tick period in fs */ return (cap); } @@ -155,7 +145,7 @@ vhpet_counter(struct vhpet *vhpet, sbintime_t *nowptr) now = sbinuptime(); delta = now - vhpet->countbase_sbt; KASSERT(delta >= 0, ("vhpet_counter: uptime went backwards: " - "%#lx to %#lx", vhpet->countbase_sbt, now)); + "%#llx to %#llx", vhpet->countbase_sbt, now)); val += delta / vhpet->freq_sbt; if (nowptr != NULL) *nowptr = now; @@ -301,7 +291,7 @@ vhpet_handler(void *a) callout_deactivate(callout); if (!vhpet_counter_enabled(vhpet)) - panic("vhpet(%p) callout with counter disabled", vhpet); + xhyve_abort("vhpet(%p) callout with counter disabled\n", vhpet); counter = vhpet_counter(vhpet, &now); vhpet_start_timer(vhpet, n, counter, now); @@ -335,7 +325,7 @@ vhpet_stop_timer(struct vhpet *vhpet, int n, sbintime_t now) static void vhpet_start_timer(struct vhpet *vhpet, int n, uint32_t counter, sbintime_t now) { - sbintime_t delta, precision; + sbintime_t delta; if (vhpet->timer[n].comprate != 0) vhpet_adjust_compval(vhpet, n, counter); @@ -349,10 +339,9 @@ vhpet_start_timer(struct vhpet *vhpet, int n, uint32_t counter, sbintime_t now) } delta = (vhpet->timer[n].compval - counter) * vhpet->freq_sbt; - precision = delta >> tc_precexp; vhpet->timer[n].callout_sbt = now + delta; callout_reset_sbt(&vhpet->timer[n].callout, vhpet->timer[n].callout_sbt, - precision, vhpet_handler, &vhpet->timer[n].arg, C_ABSOLUTE); + 0, vhpet_handler, &vhpet->timer[n].arg, C_ABSOLUTE); } static void @@ -401,7 +390,7 @@ vhpet_timer_update_config(struct vhpet *vhpet, int n, uint64_t data, if (vhpet_timer_msi_enabled(vhpet, n) || vhpet_timer_edge_trig(vhpet, n)) { if (vhpet->isr & (1 << n)) - panic("vhpet timer %d isr should not be asserted", n); + xhyve_abort("vhpet timer %d isr should not be asserted\n", n); } old_pin = vhpet_timer_ioapic_pin(vhpet, n); oldval = vhpet->timer[n].cap_config; @@ -415,7 +404,7 @@ vhpet_timer_update_config(struct vhpet *vhpet, int n, uint64_t data, return; vhpet->timer[n].cap_config = newval; - VM_CTR2(vhpet->vm, "hpet t%d cap_config set to 0x%016x", n, newval); + VM_CTR2(vhpet->vm, "hpet t%d cap_config set to 0x%016llx", n, newval); /* * Validate the interrupt routing in the HPET_TCNF_INT_ROUTE field. @@ -428,7 +417,7 @@ vhpet_timer_update_config(struct vhpet *vhpet, int n, uint64_t data, VM_CTR3(vhpet->vm, "hpet t%d configured invalid irq %d, " "allowed_irqs 0x%08x", n, new_pin, allowed_irqs); new_pin = 0; - vhpet->timer[n].cap_config &= ~HPET_TCNF_INT_ROUTE; + vhpet->timer[n].cap_config &= ~((uint64_t) HPET_TCNF_INT_ROUTE); } if (!vhpet_periodic_timer(vhpet, n)) @@ -467,8 +456,8 @@ vhpet_timer_update_config(struct vhpet *vhpet, int n, uint64_t data, } int -vhpet_mmio_write(void *vm, int vcpuid, uint64_t gpa, uint64_t val, int size, - void *arg) +vhpet_mmio_write(void *vm, UNUSED int vcpuid, uint64_t gpa, uint64_t val, int size, + UNUSED void *arg) { struct vhpet *vhpet; uint64_t data, mask, oldval, val64; @@ -476,8 +465,9 @@ vhpet_mmio_write(void *vm, int vcpuid, uint64_t gpa, uint64_t val, int size, sbintime_t now, *nowptr; int i, offset; + now = 0; vhpet = vm_hpet(vm); - offset = gpa - VHPET_BASE; + offset = (int) (gpa - VHPET_BASE); VHPET_LOCK(vhpet); @@ -524,7 +514,7 @@ vhpet_mmio_write(void *vm, int vcpuid, uint64_t gpa, uint64_t val, int size, * LegacyReplacement Routing is not supported so clear the * bit explicitly. */ - vhpet->config &= ~HPET_CNF_LEG_RT; + vhpet->config &= ~((uint64_t) HPET_CNF_LEG_RT); if ((oldval ^ vhpet->config) & HPET_CNF_ENABLE) { if (vhpet_counter_enabled(vhpet)) { @@ -539,7 +529,7 @@ vhpet_mmio_write(void *vm, int vcpuid, uint64_t gpa, uint64_t val, int size, } if (offset == HPET_ISR || offset == HPET_ISR + 4) { - isr_clear_mask = vhpet->isr & data; + isr_clear_mask = (uint32_t) (vhpet->isr & data); for (i = 0; i < VHPET_NUM_TIMERS; i++) { if ((isr_clear_mask & (1 << i)) != 0) { VM_CTR1(vhpet->vm, "hpet t%d isr cleared", i); @@ -553,7 +543,7 @@ vhpet_mmio_write(void *vm, int vcpuid, uint64_t gpa, uint64_t val, int size, /* Zero-extend the counter to 64-bits before updating it */ val64 = vhpet_counter(vhpet, NULL); update_register(&val64, data, mask); - vhpet->countbase = val64; + vhpet->countbase = (uint32_t) val64; if (vhpet_counter_enabled(vhpet)) vhpet_start_counting(vhpet); goto done; @@ -579,10 +569,10 @@ vhpet_mmio_write(void *vm, int vcpuid, uint64_t gpa, uint64_t val, int size, */ val64 = vhpet->timer[i].comprate; update_register(&val64, data, mask); - vhpet->timer[i].comprate = val64; + vhpet->timer[i].comprate = (uint32_t) val64; if ((vhpet->timer[i].cap_config & HPET_TCNF_VAL_SET) != 0) { - vhpet->timer[i].compval = val64; + vhpet->timer[i].compval = (uint32_t) val64; } } else { KASSERT(vhpet->timer[i].comprate == 0, @@ -590,9 +580,9 @@ vhpet_mmio_write(void *vm, int vcpuid, uint64_t gpa, uint64_t val, int size, "rate %u", i, vhpet->timer[i].comprate)); val64 = vhpet->timer[i].compval; update_register(&val64, data, mask); - vhpet->timer[i].compval = val64; + vhpet->timer[i].compval = (uint32_t) val64; } - vhpet->timer[i].cap_config &= ~HPET_TCNF_VAL_SET; + vhpet->timer[i].cap_config &= ~((uint64_t) HPET_TCNF_VAL_SET); if (vhpet->timer[i].compval != old_compval || vhpet->timer[i].comprate != old_comprate) { @@ -617,15 +607,16 @@ done: } int -vhpet_mmio_read(void *vm, int vcpuid, uint64_t gpa, uint64_t *rval, int size, - void *arg) +vhpet_mmio_read(void *vm, UNUSED int vcpuid, uint64_t gpa, uint64_t *rval, int size, + UNUSED void *arg) { int i, offset; struct vhpet *vhpet; uint64_t data; + data = 0; vhpet = vm_hpet(vm); - offset = gpa - VHPET_BASE; + offset = (int) (gpa - VHPET_BASE); VHPET_LOCK(vhpet); @@ -707,9 +698,12 @@ vhpet_init(struct vm *vm) struct vhpet_callout_arg *arg; struct bintime bt; - vhpet = malloc(sizeof(struct vhpet), M_VHPET, M_WAITOK | M_ZERO); - vhpet->vm = vm; - mtx_init(&vhpet->mtx, "vhpet lock", NULL, MTX_DEF); + vhpet = malloc(sizeof(struct vhpet)); + assert(vhpet); + bzero(vhpet, sizeof(struct vhpet)); + vhpet->vm = vm; + + pthread_mutex_init(&vhpet->mtx, NULL); FREQ2BT(HPET_FREQ, &bt); vhpet->freq_sbt = bttosbt(bt); @@ -747,13 +741,12 @@ vhpet_cleanup(struct vhpet *vhpet) for (i = 0; i < VHPET_NUM_TIMERS; i++) callout_drain(&vhpet->timer[i].callout); - free(vhpet, M_VHPET); + free(vhpet); } int -vhpet_getcap(struct vm_hpet_cap *cap) +vhpet_getcap(uint32_t *cap) { - - cap->capabilities = vhpet_capabilities(); + *cap = (uint32_t) vhpet_capabilities(); return (0); } diff --git a/vmm/io/vioapic.c b/src/vmm/io/vioapic.c similarity index 82% rename from vmm/io/vioapic.c rename to src/vmm/io/vioapic.c index e6b8b5a..55005c4 100644 --- a/vmm/io/vioapic.c +++ b/src/vmm/io/vioapic.c @@ -1,6 +1,7 @@ /*- * Copyright (c) 2013 Tycho Nightingale * Copyright (c) 2013 Neel Natu + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,24 +28,16 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "vmm_ktr.h" -#include "vmm_lapic.h" -#include "vlapic.h" -#include "vioapic.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #define IOREGSEL 0x00 #define IOWIN 0x10 @@ -52,36 +45,36 @@ __FBSDID("$FreeBSD$"); #define REDIR_ENTRIES 24 #define RTBL_RO_BITS ((uint64_t)(IOART_REM_IRR | IOART_DELIVS)) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct vioapic { - struct vm *vm; - struct mtx mtx; - uint32_t id; - uint32_t ioregsel; + struct vm *vm; + OSSpinLock lock; + uint32_t id; + uint32_t ioregsel; struct { uint64_t reg; - int acnt; /* sum of pin asserts (+1) and deasserts (-1) */ + int acnt; /* sum of pin asserts (+1) and deasserts (-1) */ } rtbl[REDIR_ENTRIES]; }; +#pragma clang diagnostic pop -#define VIOAPIC_LOCK(vioapic) mtx_lock_spin(&((vioapic)->mtx)) -#define VIOAPIC_UNLOCK(vioapic) mtx_unlock_spin(&((vioapic)->mtx)) -#define VIOAPIC_LOCKED(vioapic) mtx_owned(&((vioapic)->mtx)) +#define VIOAPIC_LOCK_INIT(v) (v)->lock = OS_SPINLOCK_INIT; +#define VIOAPIC_LOCK(v) OSSpinLockLock(&(v)->lock) +#define VIOAPIC_UNLOCK(v) OSSpinLockUnlock(&(v)->lock) -static MALLOC_DEFINE(M_VIOAPIC, "vioapic", "bhyve virtual ioapic"); - -#define VIOAPIC_CTR1(vioapic, fmt, a1) \ +#define VIOAPIC_CTR1(vioapic, fmt, a1) \ VM_CTR1((vioapic)->vm, fmt, a1) -#define VIOAPIC_CTR2(vioapic, fmt, a1, a2) \ +#define VIOAPIC_CTR2(vioapic, fmt, a1, a2) \ VM_CTR2((vioapic)->vm, fmt, a1, a2) -#define VIOAPIC_CTR3(vioapic, fmt, a1, a2, a3) \ +#ifdef XHYVE_CONFIG_TRACE +#define VIOAPIC_CTR3(vioapic, fmt, a1, a2, a3) \ VM_CTR3((vioapic)->vm, fmt, a1, a2, a3) +#endif -#define VIOAPIC_CTR4(vioapic, fmt, a1, a2, a3, a4) \ - VM_CTR4((vioapic)->vm, fmt, a1, a2, a3, a4) - -#ifdef KTR +#ifdef XHYVE_CONFIG_TRACE static const char * pinstate_str(bool asserted) { @@ -103,11 +96,8 @@ vioapic_send_intr(struct vioapic *vioapic, int pin) KASSERT(pin >= 0 && pin < REDIR_ENTRIES, ("vioapic_set_pinstate: invalid pin number %d", pin)); - KASSERT(VIOAPIC_LOCKED(vioapic), - ("vioapic_set_pinstate: vioapic is not locked")); - - low = vioapic->rtbl[pin].reg; - high = vioapic->rtbl[pin].reg >> 32; + low = (uint32_t) vioapic->rtbl[pin].reg; + high = (uint32_t) (vioapic->rtbl[pin].reg >> 32); if ((low & IOART_INTMASK) == IOART_INTMSET) { VIOAPIC_CTR1(vioapic, "ioapic pin%d: masked", pin); @@ -134,9 +124,6 @@ vioapic_set_pinstate(struct vioapic *vioapic, int pin, bool newstate) KASSERT(pin >= 0 && pin < REDIR_ENTRIES, ("vioapic_set_pinstate: invalid pin number %d", pin)); - KASSERT(VIOAPIC_LOCKED(vioapic), - ("vioapic_set_pinstate: vioapic is not locked")); - oldcnt = vioapic->rtbl[pin].acnt; if (newstate) vioapic->rtbl[pin].acnt++; @@ -156,10 +143,11 @@ vioapic_set_pinstate(struct vioapic *vioapic, int pin, bool newstate) } else if (oldcnt == 1 && newcnt == 0) { VIOAPIC_CTR1(vioapic, "ioapic pin%d: deasserted", pin); } else { +#ifdef XHYVE_CONFIG_TRACE VIOAPIC_CTR3(vioapic, "ioapic pin%d: %s, ignored, acnt %d", pin, pinstate_str(newstate), newcnt); +#endif } - if (needintr) vioapic_send_intr(vioapic, pin); } @@ -192,8 +180,6 @@ vioapic_set_irqstate(struct vm *vm, int irq, enum irqstate irqstate) vioapic_set_pinstate(vioapic, irq, true); vioapic_set_pinstate(vioapic, irq, false); break; - default: - panic("vioapic_set_irqstate: invalid irqstate %d", irqstate); } VIOAPIC_UNLOCK(vioapic); @@ -226,7 +212,7 @@ vioapic_pulse_irq(struct vm *vm, int irq) * configuration. */ static void -vioapic_update_tmr(struct vm *vm, int vcpuid, void *arg) +vioapic_update_tmr(struct vm *vm, int vcpuid, UNUSED void *arg) { struct vioapic *vioapic; struct vlapic *vlapic; @@ -243,8 +229,8 @@ vioapic_update_tmr(struct vm *vm, int vcpuid, void *arg) */ vlapic_reset_tmr(vlapic); for (pin = 0; pin < REDIR_ENTRIES; pin++) { - low = vioapic->rtbl[pin].reg; - high = vioapic->rtbl[pin].reg >> 32; + low = (uint32_t) vioapic->rtbl[pin].reg; + high = (uint32_t) (vioapic->rtbl[pin].reg >> 32); level = low & IOART_TRGRLVL ? true : false; if (!level) @@ -266,7 +252,7 @@ vioapic_update_tmr(struct vm *vm, int vcpuid, void *arg) } static uint32_t -vioapic_read(struct vioapic *vioapic, int vcpuid, uint32_t addr) +vioapic_read(struct vioapic *vioapic, UNUSED int vcpuid, uint32_t addr) { int regnum, pin, rshift; @@ -274,13 +260,10 @@ vioapic_read(struct vioapic *vioapic, int vcpuid, uint32_t addr) switch (regnum) { case IOAPIC_ID: return (vioapic->id); - break; case IOAPIC_VER: return (((REDIR_ENTRIES - 1) << MAXREDIRSHIFT) | 0x11); - break; case IOAPIC_ARB: return (vioapic->id); - break; default: break; } @@ -294,7 +277,7 @@ vioapic_read(struct vioapic *vioapic, int vcpuid, uint32_t addr) else rshift = 0; - return (vioapic->rtbl[pin].reg >> rshift); + return ((uint32_t) (vioapic->rtbl[pin].reg >> rshift)); } return (0); @@ -337,7 +320,7 @@ vioapic_write(struct vioapic *vioapic, int vcpuid, uint32_t addr, uint32_t data) vioapic->rtbl[pin].reg &= ~mask64 | RTBL_RO_BITS; vioapic->rtbl[pin].reg |= data64 & ~RTBL_RO_BITS; - VIOAPIC_CTR2(vioapic, "ioapic pin%d: redir table entry %#lx", + VIOAPIC_CTR2(vioapic, "ioapic pin%d: redir table entry %#llx", pin, vioapic->rtbl[pin].reg); /* @@ -346,7 +329,7 @@ vioapic_write(struct vioapic *vioapic, int vcpuid, uint32_t addr, uint32_t data) * to update their vlapic trigger-mode registers. */ changed = last ^ vioapic->rtbl[pin].reg; - if (changed & ~(IOART_INTMASK | IOART_INTPOL)) { + if (changed & ~((uint64_t) (IOART_INTMASK | IOART_INTPOL))) { VIOAPIC_CTR1(vioapic, "ioapic pin%d: recalculate " "vlapic trigger-mode register", pin); VIOAPIC_UNLOCK(vioapic); @@ -395,14 +378,14 @@ vioapic_mmio_rw(struct vioapic *vioapic, int vcpuid, uint64_t gpa, if (doread) *data = vioapic->ioregsel; else - vioapic->ioregsel = *data; + vioapic->ioregsel = (uint32_t) *data; } else { if (doread) { *data = vioapic_read(vioapic, vcpuid, vioapic->ioregsel); } else { vioapic_write(vioapic, vcpuid, vioapic->ioregsel, - *data); + ((uint32_t) *data)); } } VIOAPIC_UNLOCK(vioapic); @@ -412,19 +395,20 @@ vioapic_mmio_rw(struct vioapic *vioapic, int vcpuid, uint64_t gpa, int vioapic_mmio_read(void *vm, int vcpuid, uint64_t gpa, uint64_t *rval, - int size, void *arg) + int size, UNUSED void *arg) { int error; struct vioapic *vioapic; vioapic = vm_ioapic(vm); error = vioapic_mmio_rw(vioapic, vcpuid, gpa, rval, size, true); + return (error); } int vioapic_mmio_write(void *vm, int vcpuid, uint64_t gpa, uint64_t wval, - int size, void *arg) + int size, UNUSED void *arg) { int error; struct vioapic *vioapic; @@ -435,7 +419,7 @@ vioapic_mmio_write(void *vm, int vcpuid, uint64_t gpa, uint64_t wval, } void -vioapic_process_eoi(struct vm *vm, int vcpuid, int vector) +vioapic_process_eoi(struct vm *vm, UNUSED int vcpuid, int vector) { struct vioapic *vioapic; int pin; @@ -456,7 +440,7 @@ vioapic_process_eoi(struct vm *vm, int vcpuid, int vector) continue; if ((vioapic->rtbl[pin].reg & IOART_INTVEC) != vector) continue; - vioapic->rtbl[pin].reg &= ~IOART_REM_IRR; + vioapic->rtbl[pin].reg &= ~((uint64_t) IOART_REM_IRR); if (vioapic->rtbl[pin].acnt > 0) { VIOAPIC_CTR2(vioapic, "ioapic pin%d: asserted at eoi, " "acnt %d", pin, vioapic->rtbl[pin].acnt); @@ -472,10 +456,12 @@ vioapic_init(struct vm *vm) int i; struct vioapic *vioapic; - vioapic = malloc(sizeof(struct vioapic), M_VIOAPIC, M_WAITOK | M_ZERO); - + vioapic = malloc(sizeof(struct vioapic)); + assert(vioapic); + bzero(vioapic, sizeof(struct vioapic)); vioapic->vm = vm; - mtx_init(&vioapic->mtx, "vioapic lock", NULL, MTX_SPIN); + + VIOAPIC_LOCK_INIT(vioapic); /* Initialize all redirection entries to mask all interrupts */ for (i = 0; i < REDIR_ENTRIES; i++) @@ -487,13 +473,11 @@ vioapic_init(struct vm *vm) void vioapic_cleanup(struct vioapic *vioapic) { - - free(vioapic, M_VIOAPIC); + free(vioapic); } int -vioapic_pincount(struct vm *vm) +vioapic_pincount(UNUSED struct vm *vm) { - return (REDIR_ENTRIES); } diff --git a/vmm/io/vlapic.c b/src/vmm/io/vlapic.c similarity index 84% rename from vmm/io/vlapic.c rename to src/vmm/io/vlapic.c index 3451e1e..62cf730 100644 --- a/vmm/io/vlapic.c +++ b/src/vmm/io/vlapic.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,32 +27,22 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include - -#include - -#include "vmm_lapic.h" -#include "vmm_ktr.h" -#include "vmm_stat.h" - -#include "vlapic.h" -#include "vlapic_priv.h" -#include "vioapic.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #define PRIO(x) ((x) >> 4) @@ -60,14 +51,14 @@ __FBSDID("$FreeBSD$"); #define x2apic(vlapic) (((vlapic)->msr_apicbase & APICBASE_X2APIC) ? 1 : 0) /* - * The 'vlapic->timer_mtx' is used to provide mutual exclusion between the + * The 'vlapic->timer_lock' is used to provide mutual exclusion between the * vlapic_callout_handler() and vcpu accesses to: * - timer_freq_bt, timer_period_bt, timer_fire_bt * - timer LVT register */ -#define VLAPIC_TIMER_LOCK(vlapic) mtx_lock_spin(&((vlapic)->timer_mtx)) -#define VLAPIC_TIMER_UNLOCK(vlapic) mtx_unlock_spin(&((vlapic)->timer_mtx)) -#define VLAPIC_TIMER_LOCKED(vlapic) mtx_owned(&((vlapic)->timer_mtx)) +#define VLAPIC_TIMER_LOCK_INIT(v) (v)->timer_lock = OS_SPINLOCK_INIT; +#define VLAPIC_TIMER_LOCK(v) OSSpinLockLock(&(v)->timer_lock) +#define VLAPIC_TIMER_UNLOCK(v) OSSpinLockUnlock(&(v)->timer_lock) /* * APIC timer frequency: @@ -81,9 +72,9 @@ vlapic_get_id(struct vlapic *vlapic) { if (x2apic(vlapic)) - return (vlapic->vcpuid); + return ((uint32_t) vlapic->vcpuid); else - return (vlapic->vcpuid << 24); + return ((uint32_t) (vlapic->vcpuid << 24)); } static uint32_t @@ -92,9 +83,9 @@ x2apic_ldr(struct vlapic *vlapic) int apicid; uint32_t ldr; - apicid = vlapic_get_id(vlapic); + apicid = (int) vlapic_get_id(vlapic); ldr = 1 << (apicid & 0xf); - ldr |= (apicid & 0xffff0) << 12; + ldr |= (uint32_t) ((apicid & 0xffff0) << 12); return (ldr); } @@ -114,12 +105,13 @@ vlapic_dfr_write_handler(struct vlapic *vlapic) lapic->dfr &= APIC_DFR_MODEL_MASK; lapic->dfr |= APIC_DFR_RESERVED; - if ((lapic->dfr & APIC_DFR_MODEL_MASK) == APIC_DFR_MODEL_FLAT) + if ((lapic->dfr & APIC_DFR_MODEL_MASK) == APIC_DFR_MODEL_FLAT) { VLAPIC_CTR0(vlapic, "vlapic DFR in Flat Model"); - else if ((lapic->dfr & APIC_DFR_MODEL_MASK) == APIC_DFR_MODEL_CLUSTER) + } else if ((lapic->dfr & APIC_DFR_MODEL_MASK) == APIC_DFR_MODEL_CLUSTER) { VLAPIC_CTR0(vlapic, "vlapic DFR in Cluster Model"); - else + } else { VLAPIC_CTR1(vlapic, "DFR in Unknown Model %#x", lapic->dfr); + } } void @@ -135,7 +127,7 @@ vlapic_ldr_write_handler(struct vlapic *vlapic) lapic->ldr); lapic->ldr = x2apic_ldr(vlapic); } else { - lapic->ldr &= ~APIC_LDR_RESERVED; + lapic->ldr &= ~((unsigned) APIC_LDR_RESERVED); VLAPIC_CTR1(vlapic, "vlapic LDR set to %#x", lapic->ldr); } } @@ -174,7 +166,7 @@ vlapic_timer_divisor(uint32_t dcr) case APIC_TDCR_128: return (128); default: - panic("vlapic_timer_divisor: invalid dcr 0x%08x", dcr); + xhyve_abort("vlapic_timer_divisor: invalid dcr 0x%08x\n", dcr); } } @@ -239,7 +231,7 @@ vlapic_dcr_write_handler(struct vlapic *vlapic) * XXX changes to the frequency divider will not take effect until * the timer is reloaded. */ - FREQ2BT(VLAPIC_BUS_FREQ / divisor, &vlapic->timer_freq_bt); + FREQ2BT(((unsigned) (VLAPIC_BUS_FREQ / divisor)), &vlapic->timer_freq_bt); vlapic->timer_period_bt = vlapic->timer_freq_bt; bintime_mul(&vlapic->timer_period_bt, lapic->icr_timer); @@ -312,11 +304,16 @@ vlapic_get_lvtptr(struct vlapic *vlapic, uint32_t offset) switch (offset) { case APIC_OFFSET_CMCI_LVT: return (&lapic->lvt_cmci); - case APIC_OFFSET_TIMER_LVT ... APIC_OFFSET_ERROR_LVT: + case APIC_OFFSET_TIMER_LVT: + case APIC_OFFSET_THERM_LVT: + case APIC_OFFSET_PERF_LVT: + case APIC_OFFSET_LINT0_LVT: + case APIC_OFFSET_LINT1_LVT: + case APIC_OFFSET_ERROR_LVT: i = (offset - APIC_OFFSET_TIMER_LVT) >> 2; return ((&lapic->lvt_timer) + i);; default: - panic("vlapic_get_lvt: invalid LVT\n"); + xhyve_abort("vlapic_get_lvt: invalid LVT\n"); } } @@ -446,7 +443,7 @@ vlapic_fire_lvt(struct vlapic *vlapic, uint32_t lvt) vlapic_set_error(vlapic, APIC_ESR_SEND_ILLEGAL_VECTOR); return (0); } - if (vlapic_set_intr_ready(vlapic, vec, false)) + if (vlapic_set_intr_ready(vlapic, ((int) vec), false)) vcpu_notify_event(vlapic->vm, vlapic->vcpuid, true); break; case APIC_LVT_DM_NMI: @@ -494,7 +491,7 @@ vlapic_update_ppr(struct vlapic *vlapic) * bits is set in the ISRx registers. */ isrvec = vlapic->isrvec_stk[vlapic->isrvec_stk_top]; - tpr = vlapic->apic_page->tpr; + tpr = (int) vlapic->apic_page->tpr; #if 1 { @@ -502,7 +499,7 @@ vlapic_update_ppr(struct vlapic *vlapic) uint32_t *isrptr; if (vlapic->isrvec_stk_top == 0 && isrvec != 0) - panic("isrvec_stk is corrupted: %d", isrvec); + xhyve_abort("isrvec_stk is corrupted: %d\n", isrvec); /* * Make sure that the priority of the nested interrupts is @@ -513,7 +510,7 @@ vlapic_update_ppr(struct vlapic *vlapic) curprio = PRIO(vlapic->isrvec_stk[i]); if (curprio <= lastprio) { dump_isrvec_stk(vlapic); - panic("isrvec_stk does not satisfy invariant"); + xhyve_abort("isrvec_stk does not satisfy invariant\n"); } lastprio = curprio; } @@ -530,7 +527,7 @@ vlapic_update_ppr(struct vlapic *vlapic) if (i > vlapic->isrvec_stk_top || vlapic->isrvec_stk[i] != vector) { dump_isrvec_stk(vlapic); - panic("ISR and isrvec_stk out of sync"); + xhyve_abort("ISR and isrvec_stk out of sync\n"); } i++; } @@ -543,7 +540,7 @@ vlapic_update_ppr(struct vlapic *vlapic) else ppr = isrvec & 0xf0; - vlapic->apic_page->ppr = ppr; + vlapic->apic_page->ppr = (uint32_t) ppr; VLAPIC_CTR1(vlapic, "vlapic_update_ppr 0x%02x", ppr); } @@ -561,10 +558,10 @@ vlapic_process_eoi(struct vlapic *vlapic) for (i = 7; i >= 0; i--) { idx = i * 4; - bitpos = fls(isrptr[idx]); + bitpos = fls((int) isrptr[idx]); if (bitpos-- != 0) { if (vlapic->isrvec_stk_top <= 0) { - panic("invalid vlapic isrvec_stk_top %d", + xhyve_abort("invalid vlapic isrvec_stk_top %d\n", vlapic->isrvec_stk_top); } isrptr[idx] &= ~(1 << bitpos); @@ -589,7 +586,7 @@ static __inline int vlapic_get_lvt_field(uint32_t lvt, uint32_t mask) { - return (lvt & mask); + return ((int) (lvt & mask)); } static __inline int @@ -628,8 +625,6 @@ static void vlapic_fire_timer(struct vlapic *vlapic) { uint32_t lvt; - - KASSERT(VLAPIC_TIMER_LOCKED(vlapic), ("vlapic_fire_timer not locked")); // The timer LVT always uses the fixed delivery mode. lvt = vlapic_get_lvt(vlapic, APIC_OFFSET_TIMER_LVT); @@ -736,8 +731,10 @@ vlapic_callout_handler(void *arg) if (vlapic_periodic_timer(vlapic)) { binuptime(&btnow); - KASSERT(bintime_cmp(&btnow, &vlapic->timer_fire_bt, >=), - ("vlapic callout at %#lx.%#lx, expected at %#lx.#%lx", + /* FIXME */ + KWARN(bintime_cmp(&btnow, &vlapic->timer_fire_bt, >=), + ("XHYVE: vlapic callout at %#llx.%#llx, expected at " + "%#llx.#%llx\r\n", btnow.sec, btnow.frac, vlapic->timer_fire_bt.sec, vlapic->timer_fire_bt.frac)); @@ -762,8 +759,8 @@ vlapic_callout_handler(void *arg) * up. */ vlapic->timer_fire_bt = btnow; - VLAPIC_CTR2(vlapic, "vlapic timer lagging by %lu " - "usecs, period is %lu usecs - resetting time base", + VLAPIC_CTR2(vlapic, "vlapic timer lagging by %llu " + "usecs, period is %llu usecs - resetting time base", bttosbt(bt) / SBT_1US, bttosbt(vlapic->timer_period_bt) / SBT_1US); } @@ -835,9 +832,9 @@ vlapic_calcdest(struct vm *vm, cpuset_t *dmask, uint32_t dest, bool phys, * Physical mode: destination is APIC ID. */ CPU_ZERO(dmask); - vcpuid = vm_apicid2vcpuid(vm, dest); + vcpuid = vm_apicid2vcpuid(vm, ((int) dest)); if (vcpuid < VM_MAXCPU) - CPU_SET(vcpuid, dmask); + CPU_SET(((unsigned) vcpuid), dmask); } else { /* * In the "Flat Model" the MDA is interpreted as an 8-bit wide @@ -865,7 +862,7 @@ vlapic_calcdest(struct vm *vm, cpuset_t *dmask, uint32_t dest, bool phys, amask = vm_active_cpus(vm); while ((vcpuid = CPU_FFS(&amask)) != 0) { vcpuid--; - CPU_CLR(vcpuid, &amask); + CPU_CLR(((unsigned) vcpuid), &amask); vlapic = vm_lapic(vm, vcpuid); dfr = vlapic->apic_page->dfr; @@ -898,7 +895,7 @@ vlapic_calcdest(struct vm *vm, cpuset_t *dmask, uint32_t dest, bool phys, } if ((mda_ldest & ldest) != 0) { - CPU_SET(vcpuid, dmask); + CPU_SET(((unsigned) vcpuid), dmask); if (lowprio) break; } @@ -926,7 +923,7 @@ vlapic_get_tpr(struct vlapic *vlapic) { struct LAPIC *lapic = vlapic->apic_page; - return (lapic->tpr); + return ((uint8_t) lapic->tpr); } void @@ -934,12 +931,12 @@ vlapic_set_cr8(struct vlapic *vlapic, uint64_t val) { uint8_t tpr; - if (val & ~0xf) { + if (val & ~((uint64_t) 0xf)) { vm_inject_gp(vlapic->vm, vlapic->vcpuid); return; } - tpr = val << 4; + tpr = (uint8_t) (val << 4); vlapic_set_tpr(vlapic, tpr); } @@ -965,7 +962,7 @@ vlapic_icrlo_write_handler(struct vlapic *vlapic, bool *retu) struct LAPIC *lapic; lapic = vlapic->apic_page; - lapic->icr_lo &= ~APIC_DELSTAT_PEND; + lapic->icr_lo &= ~((unsigned) APIC_DELSTAT_PEND); icrval = ((uint64_t)lapic->icr_hi << 32) | lapic->icr_lo; if (x2apic(vlapic)) @@ -981,7 +978,7 @@ vlapic_icrlo_write_handler(struct vlapic *vlapic, bool *retu) return (0); } - VLAPIC_CTR2(vlapic, "icrlo 0x%016lx triggered ipi %d", icrval, vec); + VLAPIC_CTR2(vlapic, "icrlo 0x%016llx triggered ipi %d", icrval, vec); if (mode == APIC_DELMODE_FIXED || mode == APIC_DELMODE_NMI) { switch (icrval & APIC_DEST_MASK) { @@ -991,14 +988,14 @@ vlapic_icrlo_write_handler(struct vlapic *vlapic, bool *retu) x2apic(vlapic)); break; case APIC_DEST_SELF: - CPU_SETOF(vlapic->vcpuid, &dmask); + CPU_SETOF(((unsigned) vlapic->vcpuid), &dmask); break; case APIC_DEST_ALLISELF: dmask = vm_active_cpus(vlapic->vm); break; case APIC_DEST_ALLESELF: dmask = vm_active_cpus(vlapic->vm); - CPU_CLR(vlapic->vcpuid, &dmask); + CPU_CLR(((unsigned) vlapic->vcpuid), &dmask); break; default: CPU_ZERO(&dmask); /* satisfy gcc */ @@ -1007,9 +1004,9 @@ vlapic_icrlo_write_handler(struct vlapic *vlapic, bool *retu) while ((i = CPU_FFS(&dmask)) != 0) { i--; - CPU_CLR(i, &dmask); + CPU_CLR(((unsigned) i), &dmask); if (mode == APIC_DELMODE_FIXED) { - lapic_intr_edge(vlapic->vm, i, vec); + lapic_intr_edge(vlapic->vm, i, ((int) vec)); vmm_stat_array_incr(vlapic->vm, vlapic->vcpuid, IPIS_SENT, i, 1); VLAPIC_CTR2(vlapic, "vlapic sending ipi %d " @@ -1029,7 +1026,7 @@ vlapic_icrlo_write_handler(struct vlapic *vlapic, bool *retu) return (0); if (vlapic->vcpuid == 0 && dest != 0 && dest < VM_MAXCPU) { - vlapic2 = vm_lapic(vlapic->vm, dest); + vlapic2 = vm_lapic(vlapic->vm, ((int) dest)); /* move from INIT to waiting-for-SIPI state */ if (vlapic2->boot_state == BS_INIT) { @@ -1042,7 +1039,7 @@ vlapic_icrlo_write_handler(struct vlapic *vlapic, bool *retu) if (mode == APIC_DELMODE_STARTUP) { if (vlapic->vcpuid == 0 && dest != 0 && dest < VM_MAXCPU) { - vlapic2 = vm_lapic(vlapic->vm, dest); + vlapic2 = vm_lapic(vlapic->vm, ((int) dest)); /* * Ignore SIPIs in any state other than wait-for-SIPI @@ -1055,8 +1052,8 @@ vlapic_icrlo_write_handler(struct vlapic *vlapic, bool *retu) *retu = true; vmexit = vm_exitinfo(vlapic->vm, vlapic->vcpuid); vmexit->exitcode = VM_EXITCODE_SPINUP_AP; - vmexit->u.spinup_ap.vcpu = dest; - vmexit->u.spinup_ap.rip = vec << PAGE_SHIFT; + vmexit->u.spinup_ap.vcpu = (int) dest; + vmexit->u.spinup_ap.rip = vec << XHYVE_PAGE_SHIFT; return (0); } @@ -1097,10 +1094,10 @@ vlapic_pending_intr(struct vlapic *vlapic, int *vecptr) for (i = 7; i >= 0; i--) { idx = i * 4; val = atomic_load_acq_int(&irrptr[idx]); - bitpos = fls(val); + bitpos = fls((int) val); if (bitpos != 0) { vector = i * 32 + (bitpos - 1); - if (PRIO(vector) > PRIO(lapic->ppr)) { + if (((unsigned) PRIO(vector)) > PRIO(lapic->ppr)) { VLAPIC_CTR1(vlapic, "pending intr %d", vector); if (vecptr != NULL) *vecptr = vector; @@ -1119,8 +1116,10 @@ vlapic_intr_accepted(struct vlapic *vlapic, int vector) uint32_t *irrptr, *isrptr; int idx, stk_top; - if (vlapic->ops.intr_accepted) - return ((*vlapic->ops.intr_accepted)(vlapic, vector)); + if (vlapic->ops.intr_accepted) { + (*vlapic->ops.intr_accepted)(vlapic, vector); + return; + } /* * clear the ready bit for vector being accepted in irr @@ -1143,9 +1142,9 @@ vlapic_intr_accepted(struct vlapic *vlapic, int vector) stk_top = vlapic->isrvec_stk_top; if (stk_top >= ISRVEC_STK_SIZE) - panic("isrvec_stk_top overflow %d", stk_top); + xhyve_abort("isrvec_stk_top overflow %d\n", stk_top); - vlapic->isrvec_stk[stk_top] = vector; + vlapic->isrvec_stk[stk_top] = (uint8_t) vector; vlapic_update_ppr(vlapic); } @@ -1187,7 +1186,7 @@ vlapic_svr_write_handler(struct vlapic *vlapic) int vlapic_read(struct vlapic *vlapic, int mmio_access, uint64_t offset, - uint64_t *data, bool *retu) + uint64_t *data, UNUSED bool *retu) { struct LAPIC *lapic = vlapic->apic_page; uint32_t *reg; @@ -1195,7 +1194,7 @@ vlapic_read(struct vlapic *vlapic, int mmio_access, uint64_t offset, /* Ignore MMIO accesses in x2APIC mode */ if (x2apic(vlapic) && mmio_access) { - VLAPIC_CTR1(vlapic, "MMIO read from offset %#lx in x2APIC mode", + VLAPIC_CTR1(vlapic, "MMIO read from offset %#llx in x2APIC mode", offset); *data = 0; goto done; @@ -1205,7 +1204,7 @@ vlapic_read(struct vlapic *vlapic, int mmio_access, uint64_t offset, /* * XXX Generate GP fault for MSR accesses in xAPIC mode */ - VLAPIC_CTR1(vlapic, "x2APIC MSR read from offset %#lx in " + VLAPIC_CTR1(vlapic, "x2APIC MSR read from offset %#llx in " "xAPIC mode", offset); *data = 0; goto done; @@ -1216,7 +1215,7 @@ vlapic_read(struct vlapic *vlapic, int mmio_access, uint64_t offset, goto done; } - offset &= ~3; + offset &= ~((uint64_t) 3); switch(offset) { case APIC_OFFSET_ID: @@ -1246,18 +1245,39 @@ vlapic_read(struct vlapic *vlapic, int mmio_access, uint64_t offset, case APIC_OFFSET_SVR: *data = lapic->svr; break; - case APIC_OFFSET_ISR0 ... APIC_OFFSET_ISR7: - i = (offset - APIC_OFFSET_ISR0) >> 2; + case APIC_OFFSET_ISR0: + case APIC_OFFSET_ISR1: + case APIC_OFFSET_ISR2: + case APIC_OFFSET_ISR3: + case APIC_OFFSET_ISR4: + case APIC_OFFSET_ISR5: + case APIC_OFFSET_ISR6: + case APIC_OFFSET_ISR7: + i = (int) ((offset - APIC_OFFSET_ISR0) >> 2); reg = &lapic->isr0; *data = *(reg + i); break; - case APIC_OFFSET_TMR0 ... APIC_OFFSET_TMR7: - i = (offset - APIC_OFFSET_TMR0) >> 2; + case APIC_OFFSET_TMR0: + case APIC_OFFSET_TMR1: + case APIC_OFFSET_TMR2: + case APIC_OFFSET_TMR3: + case APIC_OFFSET_TMR4: + case APIC_OFFSET_TMR5: + case APIC_OFFSET_TMR6: + case APIC_OFFSET_TMR7: + i = (int) ((offset - APIC_OFFSET_TMR0) >> 2); reg = &lapic->tmr0; *data = *(reg + i); break; - case APIC_OFFSET_IRR0 ... APIC_OFFSET_IRR7: - i = (offset - APIC_OFFSET_IRR0) >> 2; + case APIC_OFFSET_IRR0: + case APIC_OFFSET_IRR1: + case APIC_OFFSET_IRR2: + case APIC_OFFSET_IRR3: + case APIC_OFFSET_IRR4: + case APIC_OFFSET_IRR5: + case APIC_OFFSET_IRR6: + case APIC_OFFSET_IRR7: + i = (int) ((offset - APIC_OFFSET_IRR0) >> 2); reg = &lapic->irr0; *data = atomic_load_acq_int(reg + i); break; @@ -1273,8 +1293,13 @@ vlapic_read(struct vlapic *vlapic, int mmio_access, uint64_t offset, *data = lapic->icr_hi; break; case APIC_OFFSET_CMCI_LVT: - case APIC_OFFSET_TIMER_LVT ... APIC_OFFSET_ERROR_LVT: - *data = vlapic_get_lvt(vlapic, offset); + case APIC_OFFSET_TIMER_LVT: + case APIC_OFFSET_THERM_LVT: + case APIC_OFFSET_PERF_LVT: + case APIC_OFFSET_LINT0_LVT: + case APIC_OFFSET_LINT1_LVT: + case APIC_OFFSET_ERROR_LVT: + *data = vlapic_get_lvt(vlapic, ((uint32_t) offset)); #ifdef INVARIANTS reg = vlapic_get_lvtptr(vlapic, offset); KASSERT(*data == *reg, ("inconsistent lvt value at " @@ -1302,7 +1327,7 @@ vlapic_read(struct vlapic *vlapic, int mmio_access, uint64_t offset, break; } done: - VLAPIC_CTR2(vlapic, "vlapic read offset %#x, data %#lx", offset, *data); + VLAPIC_CTR2(vlapic, "vlapic read offset %#llx, data %#llx", offset, *data); return 0; } @@ -1314,10 +1339,10 @@ vlapic_write(struct vlapic *vlapic, int mmio_access, uint64_t offset, uint32_t *regptr; int retval; - KASSERT((offset & 0xf) == 0 && offset < PAGE_SIZE, - ("vlapic_write: invalid offset %#lx", offset)); + KASSERT((offset & 0xf) == 0 && offset < XHYVE_PAGE_SIZE, + ("vlapic_write: invalid offset %#llx", offset)); - VLAPIC_CTR2(vlapic, "vlapic write offset %#lx, data %#lx", + VLAPIC_CTR2(vlapic, "vlapic write offset %#llx, data %#llx", offset, data); if (offset > sizeof(*lapic)) @@ -1325,7 +1350,7 @@ vlapic_write(struct vlapic *vlapic, int mmio_access, uint64_t offset, /* Ignore MMIO accesses in x2APIC mode */ if (x2apic(vlapic) && mmio_access) { - VLAPIC_CTR2(vlapic, "MMIO write of %#lx to offset %#lx " + VLAPIC_CTR2(vlapic, "MMIO write of %#llx to offset %#llx " "in x2APIC mode", data, offset); return (0); } @@ -1334,7 +1359,7 @@ vlapic_write(struct vlapic *vlapic, int mmio_access, uint64_t offset, * XXX Generate GP fault for MSR accesses in xAPIC mode */ if (!x2apic(vlapic) && !mmio_access) { - VLAPIC_CTR2(vlapic, "x2APIC MSR write of %#lx to offset %#lx " + VLAPIC_CTR2(vlapic, "x2APIC MSR write of %#llx to offset %#llx " "in xAPIC mode", data, offset); return (0); } @@ -1343,7 +1368,7 @@ vlapic_write(struct vlapic *vlapic, int mmio_access, uint64_t offset, switch(offset) { case APIC_OFFSET_ID: - lapic->id = data; + lapic->id = (uint32_t) data; vlapic_id_write_handler(vlapic); break; case APIC_OFFSET_TPR: @@ -1353,39 +1378,44 @@ vlapic_write(struct vlapic *vlapic, int mmio_access, uint64_t offset, vlapic_process_eoi(vlapic); break; case APIC_OFFSET_LDR: - lapic->ldr = data; + lapic->ldr = (uint32_t) data; vlapic_ldr_write_handler(vlapic); break; case APIC_OFFSET_DFR: - lapic->dfr = data; + lapic->dfr = (uint32_t) data; vlapic_dfr_write_handler(vlapic); break; case APIC_OFFSET_SVR: - lapic->svr = data; + lapic->svr = (uint32_t) data; vlapic_svr_write_handler(vlapic); break; case APIC_OFFSET_ICR_LOW: - lapic->icr_lo = data; + lapic->icr_lo = (uint32_t) data; if (x2apic(vlapic)) lapic->icr_hi = data >> 32; retval = vlapic_icrlo_write_handler(vlapic, retu); break; case APIC_OFFSET_ICR_HI: - lapic->icr_hi = data; + lapic->icr_hi = (uint32_t) data; break; case APIC_OFFSET_CMCI_LVT: - case APIC_OFFSET_TIMER_LVT ... APIC_OFFSET_ERROR_LVT: - regptr = vlapic_get_lvtptr(vlapic, offset); - *regptr = data; - vlapic_lvt_write_handler(vlapic, offset); + case APIC_OFFSET_TIMER_LVT: + case APIC_OFFSET_THERM_LVT: + case APIC_OFFSET_PERF_LVT: + case APIC_OFFSET_LINT0_LVT: + case APIC_OFFSET_LINT1_LVT: + case APIC_OFFSET_ERROR_LVT: + regptr = vlapic_get_lvtptr(vlapic, ((uint32_t) offset)); + *regptr = (uint32_t) data; + vlapic_lvt_write_handler(vlapic, ((uint32_t) offset)); break; case APIC_OFFSET_TIMER_ICR: - lapic->icr_timer = data; + lapic->icr_timer = (uint32_t) data; vlapic_icrtmr_write_handler(vlapic); break; case APIC_OFFSET_TIMER_DCR: - lapic->dcr_timer = data; + lapic->dcr_timer = (uint32_t) data; vlapic_dcr_write_handler(vlapic); break; @@ -1402,9 +1432,30 @@ vlapic_write(struct vlapic *vlapic, int mmio_access, uint64_t offset, case APIC_OFFSET_APR: case APIC_OFFSET_PPR: case APIC_OFFSET_RRR: - case APIC_OFFSET_ISR0 ... APIC_OFFSET_ISR7: - case APIC_OFFSET_TMR0 ... APIC_OFFSET_TMR7: - case APIC_OFFSET_IRR0 ... APIC_OFFSET_IRR7: + case APIC_OFFSET_ISR0: + case APIC_OFFSET_ISR1: + case APIC_OFFSET_ISR2: + case APIC_OFFSET_ISR3: + case APIC_OFFSET_ISR4: + case APIC_OFFSET_ISR5: + case APIC_OFFSET_ISR6: + case APIC_OFFSET_ISR7: + case APIC_OFFSET_TMR0: + case APIC_OFFSET_TMR1: + case APIC_OFFSET_TMR2: + case APIC_OFFSET_TMR3: + case APIC_OFFSET_TMR4: + case APIC_OFFSET_TMR5: + case APIC_OFFSET_TMR6: + case APIC_OFFSET_TMR7: + case APIC_OFFSET_IRR0: + case APIC_OFFSET_IRR1: + case APIC_OFFSET_IRR2: + case APIC_OFFSET_IRR3: + case APIC_OFFSET_IRR4: + case APIC_OFFSET_IRR5: + case APIC_OFFSET_IRR6: + case APIC_OFFSET_IRR7: case APIC_OFFSET_TIMER_CCR: default: // Read only. @@ -1457,7 +1508,7 @@ vlapic_init(struct vlapic *vlapic) * Therefore the timer mutex must be a spinlock because blockable * mutexes cannot be acquired in a critical section. */ - mtx_init(&vlapic->timer_mtx, "vlapic timer mtx", NULL, MTX_SPIN); + VLAPIC_TIMER_LOCK_INIT(vlapic); callout_init(&vlapic->callout, 1); vlapic->msr_apicbase = DEFAULT_APIC_BASE | APICBASE_ENABLED; @@ -1487,7 +1538,7 @@ vlapic_set_apicbase(struct vlapic *vlapic, uint64_t new) { if (vlapic->msr_apicbase != new) { - VLAPIC_CTR2(vlapic, "Changing APIC_BASE MSR from %#lx to %#lx " + VLAPIC_CTR2(vlapic, "Changing APIC_BASE MSR from %#llx to %#llx " "not supported", vlapic->msr_apicbase, new); return (-1); } @@ -1504,7 +1555,7 @@ vlapic_set_x2apic_state(struct vm *vm, int vcpuid, enum x2apic_state state) vlapic = vm_lapic(vm, vcpuid); if (state == X2APIC_DISABLED) - vlapic->msr_apicbase &= ~APICBASE_X2APIC; + vlapic->msr_apicbase &= ~((uint64_t) APICBASE_X2APIC); else vlapic->msr_apicbase |= APICBASE_X2APIC; @@ -1555,7 +1606,7 @@ vlapic_deliver_intr(struct vm *vm, bool level, uint32_t dest, bool phys, while ((vcpuid = CPU_FFS(&dmask)) != 0) { vcpuid--; - CPU_CLR(vcpuid, &dmask); + CPU_CLR(((unsigned) vcpuid), &dmask); if (delmode == IOART_DELEXINT) { vm_inject_extint(vm, vcpuid); } else { @@ -1565,7 +1616,7 @@ vlapic_deliver_intr(struct vm *vm, bool level, uint32_t dest, bool phys, } void -vlapic_post_intr(struct vlapic *vlapic, int hostcpu, int ipinum) +vlapic_post_intr(struct vlapic *vlapic, int hostcpu, UNUSED int ipinum) { /* * Post an interrupt to the vcpu currently running on 'hostcpu'. @@ -1579,7 +1630,8 @@ vlapic_post_intr(struct vlapic *vlapic, int hostcpu, int ipinum) if (vlapic->ops.post_intr) (*vlapic->ops.post_intr)(vlapic, hostcpu); else - ipi_cpu(hostcpu, ipinum); + xhyve_abort("hv_vcpu_interrupt\n"); /* FIXME */ + //ipi_cpu(hostcpu, ipinum); } bool @@ -1646,7 +1698,7 @@ vlapic_set_tmr_level(struct vlapic *vlapic, uint32_t dest, bool phys, lowprio = (delmode == APIC_DELMODE_LOWPRIO); vlapic_calcdest(vlapic->vm, &dmask, dest, phys, lowprio, false); - if (!CPU_ISSET(vlapic->vcpuid, &dmask)) + if (!CPU_ISSET(((unsigned) vlapic->vcpuid), &dmask)) return; VLAPIC_CTR1(vlapic, "vector %d set to level-triggered", vector); diff --git a/vmm/io/vpmtmr.c b/src/vmm/io/vpmtmr.c similarity index 76% rename from vmm/io/vpmtmr.c rename to src/vmm/io/vpmtmr.c index 1e7bb93..4f237e4 100644 --- a/vmm/io/vpmtmr.c +++ b/src/vmm/io/vpmtmr.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2014, Neel Natu (neel@freebsd.org) + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -24,18 +25,13 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include - -#include - -#include "vpmtmr.h" +#include +#include +#include +#include +#include +#include +#include /* * The ACPI Power Management timer is a free-running 24- or 32-bit @@ -46,21 +42,24 @@ __FBSDID("$FreeBSD$"); #define PMTMR_FREQ 3579545 /* 3.579545MHz */ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct vpmtmr { sbintime_t freq_sbt; sbintime_t baseuptime; uint32_t baseval; }; - -static MALLOC_DEFINE(M_VPMTMR, "vpmtmr", "bhyve virtual acpi timer"); +#pragma clang diagnostic pop struct vpmtmr * -vpmtmr_init(struct vm *vm) +vpmtmr_init(UNUSED struct vm *vm) { struct vpmtmr *vpmtmr; struct bintime bt; - vpmtmr = malloc(sizeof(struct vpmtmr), M_VPMTMR, M_WAITOK | M_ZERO); + vpmtmr = malloc(sizeof(struct vpmtmr)); + assert(vpmtmr); + bzero(vpmtmr, sizeof(struct vpmtmr)); vpmtmr->baseuptime = sbinuptime(); vpmtmr->baseval = 0; @@ -74,12 +73,12 @@ void vpmtmr_cleanup(struct vpmtmr *vpmtmr) { - free(vpmtmr, M_VPMTMR); + free(vpmtmr); } int -vpmtmr_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, - uint32_t *val) +vpmtmr_handler(struct vm *vm, UNUSED int vcpuid, bool in, UNUSED int port, + int bytes, uint32_t *val) { struct vpmtmr *vpmtmr; sbintime_t now, delta; @@ -96,8 +95,8 @@ vpmtmr_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, now = sbinuptime(); delta = now - vpmtmr->baseuptime; KASSERT(delta >= 0, ("vpmtmr_handler: uptime went backwards: " - "%#lx to %#lx", vpmtmr->baseuptime, now)); - *val = vpmtmr->baseval + delta / vpmtmr->freq_sbt; + "%#llx to %#llx", vpmtmr->baseuptime, now)); + *val = (uint32_t) (vpmtmr->baseval + (delta / vpmtmr->freq_sbt)); return (0); } diff --git a/vmm/io/vrtc.c b/src/vmm/io/vrtc.c similarity index 78% rename from vmm/io/vrtc.c rename to src/vmm/io/vrtc.c index 18ebc4b..2519744 100644 --- a/vmm/io/vrtc.c +++ b/src/vmm/io/vrtc.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2014, Neel Natu (neel@freebsd.org) + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -24,65 +25,86 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -__FBSDID("$FreeBSD$"); +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include - -#include "vmm_ktr.h" -#include "vatpic.h" -#include "vioapic.h" -#include "vrtc.h" +static const u_char bin2bcd_data[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99 +}; /* Register layout of the RTC */ struct rtcdev { - uint8_t sec; - uint8_t alarm_sec; - uint8_t min; - uint8_t alarm_min; - uint8_t hour; - uint8_t alarm_hour; - uint8_t day_of_week; - uint8_t day_of_month; - uint8_t month; - uint8_t year; - uint8_t reg_a; - uint8_t reg_b; - uint8_t reg_c; - uint8_t reg_d; - uint8_t nvram[36]; - uint8_t century; - uint8_t nvram2[128 - 51]; + uint8_t sec; + uint8_t alarm_sec; + uint8_t min; + uint8_t alarm_min; + uint8_t hour; + uint8_t alarm_hour; + uint8_t day_of_week; + uint8_t day_of_month; + uint8_t month; + uint8_t year; + uint8_t reg_a; + uint8_t reg_b; + uint8_t reg_c; + uint8_t reg_d; + uint8_t nvram[36]; + uint8_t century; + uint8_t nvram2[128 - 51]; } __packed; CTASSERT(sizeof(struct rtcdev) == 128); CTASSERT(offsetof(struct rtcdev, century) == RTC_CENTURY); +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" struct vrtc { - struct vm *vm; - struct mtx mtx; - struct callout callout; - u_int addr; /* RTC register to read or write */ - sbintime_t base_uptime; - time_t base_rtctime; - struct rtcdev rtcdev; + struct vm *vm; + pthread_mutex_t mtx; + struct callout callout; + u_int addr; /* RTC register to read or write */ + sbintime_t base_uptime; + time_t base_rtctime; + struct rtcdev rtcdev; }; -#define VRTC_LOCK(vrtc) mtx_lock(&((vrtc)->mtx)) -#define VRTC_UNLOCK(vrtc) mtx_unlock(&((vrtc)->mtx)) -#define VRTC_LOCKED(vrtc) mtx_owned(&((vrtc)->mtx)) +struct clocktime { + int year; /* year (4 digit year) */ + int mon; /* month (1 - 12) */ + int day; /* day (1 - 31) */ + int hour; /* hour (0 - 23) */ + int min; /* minute (0 - 59) */ + int sec; /* second (0 - 59) */ + int dow; /* day of week (0 - 6; 0 = Sunday) */ + long nsec; /* nano seconds */ +}; +#pragma clang diagnostic pop +#define VRTC_LOCK(vrtc) pthread_mutex_lock(&((vrtc)->mtx)) +#define VRTC_UNLOCK(vrtc) pthread_mutex_unlock(&((vrtc)->mtx)) /* * RTC time is considered "broken" if: * - RTC updates are halted by the guest @@ -101,14 +123,106 @@ struct vrtc { static void vrtc_callout_handler(void *arg); static void vrtc_set_reg_c(struct vrtc *vrtc, uint8_t newval); -static MALLOC_DEFINE(M_VRTC, "vrtc", "bhyve virtual rtc"); - -SYSCTL_DECL(_hw_vmm); -SYSCTL_NODE(_hw_vmm, OID_AUTO, vrtc, CTLFLAG_RW, NULL, NULL); - static int rtc_flag_broken_time = 1; -SYSCTL_INT(_hw_vmm_vrtc, OID_AUTO, flag_broken_time, CTLFLAG_RDTUN, - &rtc_flag_broken_time, 0, "Stop guest when invalid RTC time is detected"); +static clock_serv_t mach_clock; + +#define POSIX_BASE_YEAR 1970 +#define FEBRUARY 2 +#define SECDAY (24 * 60 * 60) +#define days_in_year(y) (leapyear(y) ? 366 : 365) +#define days_in_month(y, m) \ + (month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0)) +#define day_of_week(days) (((days) + 4) % 7) + +static const int month_days[12] = { + 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 +}; + +static __inline int +leapyear(int year) +{ + int rv = 0; + + if ((year & 3) == 0) { + rv = 1; + if ((year % 100) == 0) { + rv = 0; + if ((year % 400) == 0) + rv = 1; + } + } + return (rv); +} + +static int +clock_ct_to_ts(struct clocktime *ct, struct timespec *ts) +{ + int i, year, days; + + year = ct->year; + + /* Sanity checks. */ + if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 || + ct->day > days_in_month(year, ct->mon) || + ct->hour > 23 || ct->min > 59 || ct->sec > 59 || + (sizeof(time_t) == 4 && year > 2037)) { /* time_t overflow */ + return (EINVAL); + } + + /* + * Compute days since start of time + * First from years, then from months. + */ + days = 0; + for (i = POSIX_BASE_YEAR; i < year; i++) + days += days_in_year(i); + + /* Months */ + for (i = 1; i < ct->mon; i++) + days += days_in_month(year, i); + days += (ct->day - 1); + + ts->tv_sec = (((time_t)days * 24 + ct->hour) * 60 + ct->min) * 60 + + ct->sec; + ts->tv_nsec = ct->nsec; + + return (0); +} + +static void +clock_ts_to_ct(struct timespec *ts, struct clocktime *ct) +{ + int i, year, days; + time_t rsec; /* remainder seconds */ + time_t secs; + + secs = ts->tv_sec; + days = (int) (secs / SECDAY); + rsec = secs % SECDAY; + + ct->dow = day_of_week(days); + + /* Subtract out whole years, counting them in i. */ + for (year = POSIX_BASE_YEAR; days >= days_in_year(year); year++) + days -= days_in_year(year); + ct->year = year; + + /* Subtract out whole months, counting them in i. */ + for (i = 1; days >= days_in_month(year, i); i++) + days -= days_in_month(year, i); + ct->mon = i; + + /* Days are what is left over (+1) from all that. */ + ct->day = days + 1; + + /* Hours, minutes, seconds are easy */ + ct->hour = (int) (rsec / 3600); + rsec = rsec % 3600; + ct->min = (int) (rsec / 60); + rsec = rsec % 60; + ct->sec = (int) rsec; + ct->nsec = ts->tv_nsec; +} static __inline bool divider_enabled(int reg_a) @@ -146,15 +260,13 @@ vrtc_curtime(struct vrtc *vrtc, sbintime_t *basetime) sbintime_t now, delta; time_t t, secs; - KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__)); - t = vrtc->base_rtctime; *basetime = vrtc->base_uptime; if (update_enabled(vrtc)) { now = sbinuptime(); delta = now - vrtc->base_uptime; KASSERT(delta >= 0, ("vrtc_curtime: uptime went backwards: " - "%#lx to %#lx", vrtc->base_uptime, now)); + "%#llx to %#llx", vrtc->base_uptime, now)); secs = delta / SBT_1S; t += secs; *basetime += secs * SBT_1S; @@ -169,19 +281,18 @@ rtcset(struct rtcdev *rtc, int val) KASSERT(val >= 0 && val < 100, ("%s: invalid bin2bcd index %d", __func__, val)); - return ((rtc->reg_b & RTCSB_BIN) ? val : bin2bcd_data[val]); + return ((uint8_t) ((rtc->reg_b & RTCSB_BIN) ? val : bin2bcd_data[val])); } static void secs_to_rtc(time_t rtctime, struct vrtc *vrtc, int force_update) { + mach_timespec_t mts; struct clocktime ct; struct timespec ts; struct rtcdev *rtc; int hour; - KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__)); - if (rtctime < 0) { KASSERT(rtctime == VRTC_BROKEN_TIME, ("%s: invalid vrtc time %#lx", __func__, rtctime)); @@ -196,8 +307,10 @@ secs_to_rtc(time_t rtctime, struct vrtc *vrtc, int force_update) if (rtc_halted(vrtc) && !force_update) return; - ts.tv_sec = rtctime; - ts.tv_nsec = 0; + clock_get_time(mach_clock, &mts); + ts.tv_sec = mts.tv_sec; + ts.tv_nsec = mts.tv_nsec; + clock_ts_to_ct(&ts, &ct); KASSERT(ct.sec >= 0 && ct.sec <= 59, ("invalid clocktime sec %d", @@ -212,8 +325,7 @@ secs_to_rtc(time_t rtctime, struct vrtc *vrtc, int force_update) ct.day)); KASSERT(ct.mon >= 1 && ct.mon <= 12, ("invalid clocktime month %d", ct.mon)); - KASSERT(ct.year >= POSIX_BASE_YEAR, ("invalid clocktime year %d", - ct.year)); + KASSERT(ct.year >= 1900, ("invalid clocktime year %d", ct.year)); rtc = &vrtc->rtcdev; rtc->sec = rtcset(rtc, ct.sec); @@ -282,8 +394,6 @@ rtc_to_secs(struct vrtc *vrtc) struct vm *vm; int century, error, hour, pm, year; - KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__)); - vm = vrtc->vm; rtc = &vrtc->rtcdev; @@ -367,7 +477,7 @@ rtc_to_secs(struct vrtc *vrtc) error = rtcget(rtc, rtc->century, ¢ury); ct.year = century * 100 + year; - if (error || ct.year < POSIX_BASE_YEAR) { + if (error || ct.year < 1900) { VM_CTR2(vm, "Invalid RTC century %#x/%d", rtc->century, ct.year); goto fail; @@ -399,8 +509,6 @@ vrtc_time_update(struct vrtc *vrtc, time_t newtime, sbintime_t newbase) time_t oldtime; uint8_t alarm_sec, alarm_min, alarm_hour; - KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__)); - rtc = &vrtc->rtcdev; alarm_sec = rtc->alarm_sec; alarm_min = rtc->alarm_min; @@ -411,7 +519,7 @@ vrtc_time_update(struct vrtc *vrtc, time_t newtime, sbintime_t newbase) oldtime, newtime); oldbase = vrtc->base_uptime; - VM_CTR2(vrtc->vm, "Updating RTC base uptime from %#lx to %#lx", + VM_CTR2(vrtc->vm, "Updating RTC base uptime from %#llx to %#llx", oldbase, newbase); vrtc->base_uptime = newbase; @@ -494,8 +602,6 @@ vrtc_freq(struct vrtc *vrtc) SBT_1S / 2, }; - KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__)); - /* * If both periodic and alarm interrupts are enabled then use the * periodic frequency to drive the callout. The minimum periodic @@ -518,9 +624,6 @@ vrtc_freq(struct vrtc *vrtc) static void vrtc_callout_reset(struct vrtc *vrtc, sbintime_t freqsbt) { - - KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__)); - if (freqsbt == 0) { if (callout_active(&vrtc->callout)) { VM_CTR0(vrtc->vm, "RTC callout stopped"); @@ -528,7 +631,7 @@ vrtc_callout_reset(struct vrtc *vrtc, sbintime_t freqsbt) } return; } - VM_CTR1(vrtc->vm, "RTC callout frequency %d hz", SBT_1S / freqsbt); + VM_CTR1(vrtc->vm, "RTC callout frequency %lld hz", SBT_1S / freqsbt); callout_reset_sbt(&vrtc->callout, freqsbt, 0, vrtc_callout_handler, vrtc, 0); } @@ -579,7 +682,7 @@ vrtc_callout_check(struct vrtc *vrtc, sbintime_t freq) active = callout_active(&vrtc->callout) ? 1 : 0; KASSERT((freq == 0 && !active) || (freq != 0 && active), - ("vrtc callout %s with frequency %#lx", + ("vrtc callout %s with frequency %#llx", active ? "active" : "inactive", freq)); } @@ -590,8 +693,6 @@ vrtc_set_reg_c(struct vrtc *vrtc, uint8_t newval) int oldirqf, newirqf; uint8_t oldval, changed; - KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__)); - rtc = &vrtc->rtcdev; newval &= RTCIR_ALARM | RTCIR_PERIOD | RTCIR_UPDATE; @@ -605,7 +706,7 @@ vrtc_set_reg_c(struct vrtc *vrtc, uint8_t newval) } oldval = rtc->reg_c; - rtc->reg_c = newirqf | newval; + rtc->reg_c = (uint8_t) (newirqf | newval); changed = oldval ^ rtc->reg_c; if (changed) { VM_CTR2(vrtc->vm, "RTC reg_c changed from %#x to %#x", @@ -630,8 +731,6 @@ vrtc_set_reg_b(struct vrtc *vrtc, uint8_t newval) int error; uint8_t oldval, changed; - KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__)); - rtc = &vrtc->rtcdev; oldval = rtc->reg_b; oldfreq = vrtc_freq(vrtc); @@ -703,14 +802,12 @@ vrtc_set_reg_a(struct vrtc *vrtc, uint8_t newval) sbintime_t oldfreq, newfreq; uint8_t oldval, changed; - KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__)); - newval &= ~RTCSA_TUP; oldval = vrtc->rtcdev.reg_a; oldfreq = vrtc_freq(vrtc); if (divider_enabled(oldval) && !divider_enabled(newval)) { - VM_CTR2(vrtc->vm, "RTC divider held in reset at %#lx/%#lx", + VM_CTR2(vrtc->vm, "RTC divider held in reset at %#lx/%#llx", vrtc->base_rtctime, vrtc->base_uptime); } else if (!divider_enabled(oldval) && divider_enabled(newval)) { /* @@ -720,7 +817,7 @@ vrtc_set_reg_a(struct vrtc *vrtc, uint8_t newval) * while the dividers were disabled. */ vrtc->base_uptime = sbinuptime(); - VM_CTR2(vrtc->vm, "RTC divider out of reset at %#lx/%#lx", + VM_CTR2(vrtc->vm, "RTC divider out of reset at %#lx/%#llx", vrtc->base_rtctime, vrtc->base_uptime); } else { /* NOTHING */ @@ -790,8 +887,10 @@ vrtc_nvram_write(struct vm *vm, int offset, uint8_t value) /* * Don't allow writes to RTC control registers or the date/time fields. */ - if (offset < offsetof(struct rtcdev, nvram[0]) || - offset == RTC_CENTURY || offset >= sizeof(struct rtcdev)) { + if (((unsigned long) offset) < offsetof(struct rtcdev, nvram) || + offset == RTC_CENTURY || + ((unsigned long) offset) >= sizeof(struct rtcdev)) + { VM_CTR1(vrtc->vm, "RTC nvram write to invalid offset %d", offset); return (EINVAL); @@ -817,7 +916,7 @@ vrtc_nvram_read(struct vm *vm, int offset, uint8_t *retval) /* * Allow all offsets in the RTC to be read. */ - if (offset < 0 || offset >= sizeof(struct rtcdev)) + if (offset < 0 || ((unsigned long) offset) >= sizeof(struct rtcdev)) return (EINVAL); vrtc = vm_rtc(vm); @@ -839,8 +938,8 @@ vrtc_nvram_read(struct vm *vm, int offset, uint8_t *retval) } int -vrtc_addr_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, - uint32_t *val) +vrtc_addr_handler(struct vm *vm, UNUSED int vcpuid, bool in, UNUSED int port, + int bytes, uint32_t *val) { struct vrtc *vrtc; @@ -862,8 +961,8 @@ vrtc_addr_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, } int -vrtc_data_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, - uint32_t *val) +vrtc_data_handler(struct vm *vm, int vcpuid, bool in, UNUSED int port, + int bytes, uint32_t *val) { struct vrtc *vrtc; struct rtcdev *rtc; @@ -878,8 +977,8 @@ vrtc_data_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, return (-1); VRTC_LOCK(vrtc); - offset = vrtc->addr; - if (offset >= sizeof(struct rtcdev)) { + offset = (int) vrtc->addr; + if (((unsigned long) offset) >= sizeof(struct rtcdev)) { VRTC_UNLOCK(vrtc); return (-1); } @@ -916,11 +1015,11 @@ vrtc_data_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, switch (offset) { case 10: VCPU_CTR1(vm, vcpuid, "RTC reg_a set to %#x", *val); - vrtc_set_reg_a(vrtc, *val); + vrtc_set_reg_a(vrtc, ((uint8_t) *val)); break; case 11: VCPU_CTR1(vm, vcpuid, "RTC reg_b set to %#x", *val); - error = vrtc_set_reg_b(vrtc, *val); + error = vrtc_set_reg_b(vrtc, ((uint8_t) *val)); break; case 12: VCPU_CTR1(vm, vcpuid, "RTC reg_c set to %#x (ignored)", @@ -939,7 +1038,7 @@ vrtc_data_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, default: VCPU_CTR2(vm, vcpuid, "RTC offset %#x set to %#x", offset, *val); - *((uint8_t *)rtc + offset) = *val; + *((uint8_t *)rtc + offset) = ((uint8_t) *val); break; } @@ -981,9 +1080,15 @@ vrtc_init(struct vm *vm) struct rtcdev *rtc; time_t curtime; - vrtc = malloc(sizeof(struct vrtc), M_VRTC, M_WAITOK | M_ZERO); + vrtc = malloc(sizeof(struct vrtc)); + assert(vrtc); + bzero(vrtc, sizeof(struct vrtc)); vrtc->vm = vm; - mtx_init(&vrtc->mtx, "vrtc lock", NULL, MTX_DEF); + + pthread_mutex_init(&vrtc->mtx, NULL); + + host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &mach_clock); + callout_init(&vrtc->callout, 1); /* Allow dividers to keep time but disable everything else */ @@ -1013,7 +1118,7 @@ vrtc_init(struct vm *vm) void vrtc_cleanup(struct vrtc *vrtc) { - callout_drain(&vrtc->callout); - free(vrtc, M_VRTC); + mach_port_deallocate(mach_task_self(), mach_clock); + free(vrtc); } diff --git a/vmm/vmm.c b/src/vmm/vmm.c similarity index 56% rename from vmm/vmm.c rename to src/vmm/vmm.c index 51c63f5..bf25c3b 100644 --- a/vmm/vmm.c +++ b/src/vmm/vmm.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,64 +27,40 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "vmm_ioport.h" -#include "vmm_ktr.h" -#include "vmm_host.h" -#include "vmm_mem.h" -#include "vmm_util.h" -#include "vatpic.h" -#include "vatpit.h" -#include "vhpet.h" -#include "vioapic.h" -#include "vlapic.h" -#include "vpmtmr.h" -#include "vrtc.h" -#include "vmm_stat.h" -#include "vmm_lapic.h" - -#include "io/ppt.h" -#include "io/iommu.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include struct vlapic; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" /* * Initialization: * (a) allocated when vcpu is created @@ -92,37 +69,37 @@ struct vlapic; * (x) initialized before use */ struct vcpu { - struct mtx mtx; /* (o) protects 'state' and 'hostcpu' */ - enum vcpu_state state; /* (o) vcpu state */ - int hostcpu; /* (o) vcpu's host cpu */ - struct vlapic *vlapic; /* (i) APIC device model */ - enum x2apic_state x2apic_state; /* (i) APIC mode */ - uint64_t exitintinfo; /* (i) events pending at VM exit */ - int nmi_pending; /* (i) NMI pending */ - int extint_pending; /* (i) INTR pending */ - int exception_pending; /* (i) exception pending */ - int exc_vector; /* (x) exception collateral */ - int exc_errcode_valid; + OSSpinLock lock; /* (o) protects 'state' */ + pthread_mutex_t state_sleep_mtx; + pthread_cond_t state_sleep_cnd; + pthread_mutex_t vcpu_sleep_mtx; + pthread_cond_t vcpu_sleep_cnd; + enum vcpu_state state; /* (o) vcpu state */ + struct vlapic *vlapic; /* (i) APIC device model */ + enum x2apic_state x2apic_state; /* (i) APIC mode */ + uint64_t exitintinfo; /* (i) events pending at VM exit */ + int nmi_pending; /* (i) NMI pending */ + int extint_pending; /* (i) INTR pending */ + int exception_pending; /* (i) exception pending */ + int exc_vector; /* (x) exception collateral */ + int exc_errcode_valid; uint32_t exc_errcode; - struct savefpu *guestfpu; /* (a,i) guest fpu state */ - uint64_t guest_xcr0; /* (i) guest %xcr0 register */ - void *stats; /* (a,i) statistics */ - struct vm_exit exitinfo; /* (x) exit reason and collateral */ - uint64_t nextrip; /* (x) next instruction to execute */ + uint64_t guest_xcr0; /* (i) guest %xcr0 register */ + void *stats; /* (a,i) statistics */ + struct vm_exit exitinfo; /* (x) exit reason and collateral */ + uint64_t nextrip; /* (x) next instruction to execute */ }; -#define vcpu_lock_initialized(v) mtx_initialized(&((v)->mtx)) -#define vcpu_lock_init(v) mtx_init(&((v)->mtx), "vcpu lock", 0, MTX_SPIN) -#define vcpu_lock(v) mtx_lock_spin(&((v)->mtx)) -#define vcpu_unlock(v) mtx_unlock_spin(&((v)->mtx)) -#define vcpu_assert_locked(v) mtx_assert(&((v)->mtx), MA_OWNED) +#define vcpu_lock_init(v) (v)->lock = OS_SPINLOCK_INIT; +#define vcpu_lock(v) OSSpinLockLock(&(v)->lock) +#define vcpu_unlock(v) OSSpinLockUnlock(&(v)->lock) struct mem_seg { - vm_paddr_t gpa; - size_t len; - boolean_t wired; - vm_object_t object; + uint64_t gpa; + size_t len; + void *object; }; + #define VM_MAX_MEMORY_SEGMENTS 2 /* @@ -132,94 +109,75 @@ struct mem_seg { * (x) initialized before use */ struct vm { - void *cookie; /* (i) cpu-specific data */ - void *iommu; /* (x) iommu-specific data */ - struct vhpet *vhpet; /* (i) virtual HPET */ - struct vioapic *vioapic; /* (i) virtual ioapic */ - struct vatpic *vatpic; /* (i) virtual atpic */ - struct vatpit *vatpit; /* (i) virtual atpit */ - struct vpmtmr *vpmtmr; /* (i) virtual ACPI PM timer */ - struct vrtc *vrtc; /* (o) virtual RTC */ - volatile cpuset_t active_cpus; /* (i) active vcpus */ - int suspend; /* (i) stop VM execution */ - volatile cpuset_t suspended_cpus; /* (i) suspended vcpus */ - volatile cpuset_t halted_cpus; /* (x) cpus in a hard halt */ - cpuset_t rendezvous_req_cpus; /* (x) rendezvous requested */ - cpuset_t rendezvous_done_cpus; /* (x) rendezvous finished */ - void *rendezvous_arg; /* (x) rendezvous func/arg */ + void *cookie; /* (i) cpu-specific data */ + struct vhpet *vhpet; /* (i) virtual HPET */ + struct vioapic *vioapic; /* (i) virtual ioapic */ + struct vatpic *vatpic; /* (i) virtual atpic */ + struct vatpit *vatpit; /* (i) virtual atpit */ + struct vpmtmr *vpmtmr; /* (i) virtual ACPI PM timer */ + struct vrtc *vrtc; /* (o) virtual RTC */ + volatile cpuset_t active_cpus; /* (i) active vcpus */ + int suspend; /* (i) stop VM execution */ + volatile cpuset_t suspended_cpus; /* (i) suspended vcpus */ + volatile cpuset_t halted_cpus; /* (x) cpus in a hard halt */ + cpuset_t rendezvous_req_cpus; /* (x) rendezvous requested */ + cpuset_t rendezvous_done_cpus; /* (x) rendezvous finished */ + void *rendezvous_arg; /* (x) rendezvous func/arg */ vm_rendezvous_func_t rendezvous_func; - struct mtx rendezvous_mtx; /* (o) rendezvous lock */ - int num_mem_segs; /* (o) guest memory segments */ - struct mem_seg mem_segs[VM_MAX_MEMORY_SEGMENTS]; - struct vmspace *vmspace; /* (o) guest's address space */ - char name[VM_MAX_NAMELEN]; /* (o) virtual machine name */ - struct vcpu vcpu[VM_MAXCPU]; /* (i) guest vcpus */ + pthread_mutex_t rendezvous_mtx; /* (o) rendezvous lock */ + pthread_cond_t rendezvous_sleep_cnd; + int num_mem_segs; /* (o) guest memory segments */ + struct mem_seg mem_segs[VM_MAX_MEMORY_SEGMENTS]; + struct vcpu vcpu[VM_MAXCPU]; /* (i) guest vcpus */ }; +#pragma clang diagnostic pop static int vmm_initialized; static struct vmm_ops *ops; -#define VMM_INIT(num) (ops != NULL ? (*ops->init)(num) : 0) -#define VMM_CLEANUP() (ops != NULL ? (*ops->cleanup)() : 0) -#define VMM_RESUME() (ops != NULL ? (*ops->resume)() : 0) -#define VMINIT(vm, pmap) (ops != NULL ? (*ops->vminit)(vm, pmap): NULL) -#define VMRUN(vmi, vcpu, rip, pmap, rptr, sptr) \ - (ops != NULL ? (*ops->vmrun)(vmi, vcpu, rip, pmap, rptr, sptr) : ENXIO) -#define VMCLEANUP(vmi) (ops != NULL ? (*ops->vmcleanup)(vmi) : NULL) -#define VMSPACE_ALLOC(min, max) \ - (ops != NULL ? (*ops->vmspace_alloc)(min, max) : NULL) -#define VMSPACE_FREE(vmspace) \ - (ops != NULL ? (*ops->vmspace_free)(vmspace) : ENXIO) -#define VMGETREG(vmi, vcpu, num, retval) \ - (ops != NULL ? (*ops->vmgetreg)(vmi, vcpu, num, retval) : ENXIO) -#define VMSETREG(vmi, vcpu, num, val) \ - (ops != NULL ? (*ops->vmsetreg)(vmi, vcpu, num, val) : ENXIO) -#define VMGETDESC(vmi, vcpu, num, desc) \ - (ops != NULL ? (*ops->vmgetdesc)(vmi, vcpu, num, desc) : ENXIO) -#define VMSETDESC(vmi, vcpu, num, desc) \ - (ops != NULL ? (*ops->vmsetdesc)(vmi, vcpu, num, desc) : ENXIO) -#define VMGETCAP(vmi, vcpu, num, retval) \ - (ops != NULL ? (*ops->vmgetcap)(vmi, vcpu, num, retval) : ENXIO) -#define VMSETCAP(vmi, vcpu, num, val) \ - (ops != NULL ? (*ops->vmsetcap)(vmi, vcpu, num, val) : ENXIO) -#define VLAPIC_INIT(vmi, vcpu) \ - (ops != NULL ? (*ops->vlapic_init)(vmi, vcpu) : NULL) -#define VLAPIC_CLEANUP(vmi, vlapic) \ - (ops != NULL ? (*ops->vlapic_cleanup)(vmi, vlapic) : NULL) - -#define fpu_start_emulating() load_cr0(rcr0() | CR0_TS) -#define fpu_stop_emulating() clts() - -static MALLOC_DEFINE(M_VM, "vm", "vm"); +#define VMM_INIT() \ + (*ops->init)() +#define VMM_CLEANUP() \ + (*ops->cleanup)() +#define VM_INIT(vmi) \ + (*ops->vm_init)(vmi) +#define VCPU_INIT(vmi, vcpu) \ + (*ops->vcpu_init)(vmi, vcpu) +#define VMRUN(vmi, vcpu, rip, rptr, sptr) \ + (*ops->vmrun)(vmi, vcpu, rip, rptr, sptr) +#define VM_CLEANUP(vmi) \ + (*ops->vm_cleanup)(vmi) +#define VCPU_CLEANUP(vmi, vcpu) \ + (*ops->vcpu_cleanup)(vmi, vcpu) +#define VMGETREG(vmi, vcpu, num, retval) \ + (*ops->vmgetreg)(vmi, vcpu, num, retval) +#define VMSETREG(vmi, vcpu, num, val) \ + (*ops->vmsetreg)(vmi, vcpu, num, val) +#define VMGETDESC(vmi, vcpu, num, desc) \ + (*ops->vmgetdesc)(vmi, vcpu, num, desc) +#define VMSETDESC(vmi, vcpu, num, desc) \ + (*ops->vmsetdesc)(vmi, vcpu, num, desc) +#define VMGETCAP(vmi, vcpu, num, retval) \ + (*ops->vmgetcap)(vmi, vcpu, num, retval) +#define VMSETCAP(vmi, vcpu, num, val) \ + (*ops->vmsetcap)(vmi, vcpu, num, val) +#define VLAPIC_INIT(vmi, vcpu) \ + (*ops->vlapic_init)(vmi, vcpu) +#define VLAPIC_CLEANUP(vmi, vlapic) \ + (*ops->vlapic_cleanup)(vmi, vlapic) +#define VCPU_INTERRUPT(vcpu) \ + (*ops->vcpu_interrupt)(vcpu) /* statistics */ -static VMM_STAT(VCPU_TOTAL_RUNTIME, "vcpu total runtime"); - -SYSCTL_NODE(_hw, OID_AUTO, vmm, CTLFLAG_RW, NULL, NULL); +//static VMM_STAT(VCPU_TOTAL_RUNTIME, "vcpu total runtime"); /* * Halt the guest if all vcpus are executing a HLT instruction with * interrupts disabled. */ static int halt_detection_enabled = 1; -SYSCTL_INT(_hw_vmm, OID_AUTO, halt_detection, CTLFLAG_RDTUN, - &halt_detection_enabled, 0, - "Halt VM if all vcpus execute HLT with interrupts disabled"); - -static int vmm_ipinum; -SYSCTL_INT(_hw_vmm, OID_AUTO, ipinum, CTLFLAG_RD, &vmm_ipinum, 0, - "IPI vector used for vcpu notifications"); - -static int trace_guest_exceptions; -SYSCTL_INT(_hw_vmm, OID_AUTO, trace_guest_exceptions, CTLFLAG_RDTUN, - &trace_guest_exceptions, 0, - "Trap into hypervisor on all guest exceptions and reflect them back"); - -static int vmm_force_iommu = 0; -TUNABLE_INT("hw.vmm.force_iommu", &vmm_force_iommu); -SYSCTL_INT(_hw_vmm, OID_AUTO, force_iommu, CTLFLAG_RDTUN, &vmm_force_iommu, 0, - "Force use of I/O MMU even if no passthrough devices were found."); +static int trace_guest_exceptions = 0; static void vcpu_cleanup(struct vm *vm, int i, bool destroy) @@ -229,7 +187,6 @@ vcpu_cleanup(struct vm *vm, int i, bool destroy) VLAPIC_CLEANUP(vm->cookie, vcpu->vlapic); if (destroy) { vmm_stat_free(vcpu->stats); - fpu_save_area_free(vcpu->guestfpu); } } @@ -244,12 +201,12 @@ vcpu_init(struct vm *vm, int vcpu_id, bool create) vcpu = &vm->vcpu[vcpu_id]; if (create) { - KASSERT(!vcpu_lock_initialized(vcpu), ("vcpu %d already " - "initialized", vcpu_id)); vcpu_lock_init(vcpu); + pthread_mutex_init(&vcpu->state_sleep_mtx, NULL); + pthread_cond_init(&vcpu->state_sleep_cnd, NULL); + pthread_mutex_init(&vcpu->vcpu_sleep_mtx, NULL); + pthread_cond_init(&vcpu->vcpu_sleep_cnd, NULL); vcpu->state = VCPU_IDLE; - vcpu->hostcpu = NOCPU; - vcpu->guestfpu = fpu_save_area_alloc(); vcpu->stats = vmm_stat_alloc(); } @@ -260,14 +217,26 @@ vcpu_init(struct vm *vm, int vcpu_id, bool create) vcpu->extint_pending = 0; vcpu->exception_pending = 0; vcpu->guest_xcr0 = XFEATURE_ENABLED_X87; - fpu_save_area_reset(vcpu->guestfpu); vmm_stat_init(vcpu->stats); } -int -vcpu_trace_exceptions(struct vm *vm, int vcpuid) -{ +int vcpu_create(struct vm *vm, int vcpu) { + if (vcpu < 0 || vcpu >= VM_MAXCPU) + xhyve_abort("vcpu_create: invalid cpuid %d\n", vcpu); + return VCPU_INIT(vm->cookie, vcpu); +} + +void vcpu_destroy(struct vm *vm, int vcpu) { + if (vcpu < 0 || vcpu >= VM_MAXCPU) + xhyve_abort("vcpu_destroy: invalid cpuid %d\n", vcpu); + + VCPU_CLEANUP(vm, vcpu); +} + +int +vcpu_trace_exceptions(void) +{ return (trace_guest_exceptions); } @@ -277,150 +246,91 @@ vm_exitinfo(struct vm *vm, int cpuid) struct vcpu *vcpu; if (cpuid < 0 || cpuid >= VM_MAXCPU) - panic("vm_exitinfo: invalid cpuid %d", cpuid); + xhyve_abort("vm_exitinfo: invalid cpuid %d\n", cpuid); vcpu = &vm->vcpu[cpuid]; return (&vcpu->exitinfo); } -static void -vmm_resume(void) -{ - VMM_RESUME(); -} - -static int +int vmm_init(void) { int error; vmm_host_state_init(); - vmm_ipinum = lapic_ipi_alloc(&IDTVEC(justreturn)); - if (vmm_ipinum < 0) - vmm_ipinum = IPI_AST; - error = vmm_mem_init(); if (error) return (error); - if (vmm_is_intel()) - ops = &vmm_ops_intel; - else if (vmm_is_amd()) - ops = &vmm_ops_amd; - else - return (ENXIO); + ops = &vmm_ops_intel; - vmm_resume_p = vmm_resume; + error = VMM_INIT(); - return (VMM_INIT(vmm_ipinum)); -} + if (error == 0) + vmm_initialized = 1; -static int -vmm_handler(module_t mod, int what, void *arg) -{ - int error; - - switch (what) { - case MOD_LOAD: - vmmdev_init(); - if (vmm_force_iommu || ppt_avail_devices() > 0) - iommu_init(); - error = vmm_init(); - if (error == 0) - vmm_initialized = 1; - break; - case MOD_UNLOAD: - error = vmmdev_cleanup(); - if (error == 0) { - vmm_resume_p = NULL; - iommu_cleanup(); - if (vmm_ipinum != IPI_AST) - lapic_ipi_free(vmm_ipinum); - error = VMM_CLEANUP(); - /* - * Something bad happened - prevent new - * VMs from being created - */ - if (error) - vmm_initialized = 0; - } - break; - default: - error = 0; - break; - } return (error); } -static moduledata_t vmm_kmod = { - "vmm", - vmm_handler, - NULL -}; -/* - * vmm initialization has the following dependencies: - * - * - iommu initialization must happen after the pci passthru driver has had - * a chance to attach to any passthru devices (after SI_SUB_CONFIGURE). - * - * - VT-x initialization requires smp_rendezvous() and therefore must happen - * after SMP is fully functional (after SI_SUB_SMP). - */ -DECLARE_MODULE(vmm, vmm_kmod, SI_SUB_SMP + 1, SI_ORDER_ANY); -MODULE_VERSION(vmm, 1); +int +vmm_cleanup(void) { + int error; + + error = VMM_CLEANUP(); + + if (error == 0) + vmm_initialized = 0; + + return error; +} static void vm_init(struct vm *vm, bool create) { - int i; + int vcpu; - vm->cookie = VMINIT(vm, vmspace_pmap(vm->vmspace)); - vm->iommu = NULL; + if (create) { + callout_system_init(); + } + + vm->cookie = VM_INIT(vm); vm->vioapic = vioapic_init(vm); vm->vhpet = vhpet_init(vm); vm->vatpic = vatpic_init(vm); vm->vatpit = vatpit_init(vm); vm->vpmtmr = vpmtmr_init(vm); - if (create) + + if (create) { vm->vrtc = vrtc_init(vm); + } CPU_ZERO(&vm->active_cpus); vm->suspend = 0; CPU_ZERO(&vm->suspended_cpus); - for (i = 0; i < VM_MAXCPU; i++) - vcpu_init(vm, i, create); + for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++) { + vcpu_init(vm, vcpu, create); + } } int -vm_create(const char *name, struct vm **retvm) +vm_create(struct vm **retvm) { struct vm *vm; - struct vmspace *vmspace; - /* - * If vmm.ko could not be successfully initialized then don't attempt - * to create the virtual machine. - */ if (!vmm_initialized) return (ENXIO); - if (name == NULL || strlen(name) >= VM_MAX_NAMELEN) - return (EINVAL); - - vmspace = VMSPACE_ALLOC(0, VM_MAXUSER_ADDRESS); - if (vmspace == NULL) - return (ENOMEM); - - vm = malloc(sizeof(struct vm), M_VM, M_WAITOK | M_ZERO); - strcpy(vm->name, name); + vm = malloc(sizeof(struct vm)); + assert(vm); + bzero(vm, sizeof(struct vm)); vm->num_mem_segs = 0; - vm->vmspace = vmspace; - mtx_init(&vm->rendezvous_mtx, "vm rendezvous lock", 0, MTX_DEF); + pthread_mutex_init(&vm->rendezvous_mtx, NULL); + pthread_cond_init(&vm->rendezvous_sleep_cnd, NULL); vm_init(vm, true); @@ -429,11 +339,11 @@ vm_create(const char *name, struct vm **retvm) } static void -vm_free_mem_seg(struct vm *vm, struct mem_seg *seg) +vm_free_mem_seg(struct mem_seg *seg) { - - if (seg->object != NULL) - vmm_mem_free(vm->vmspace, seg->gpa, seg->len); + if (seg->object != NULL) { + vmm_mem_free(seg->gpa, seg->len, seg->object); + } bzero(seg, sizeof(*seg)); } @@ -441,36 +351,31 @@ vm_free_mem_seg(struct vm *vm, struct mem_seg *seg) static void vm_cleanup(struct vm *vm, bool destroy) { - int i; + int i, vcpu; - ppt_unassign_all(vm); + for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++) { + vcpu_cleanup(vm, vcpu, destroy); + } - if (vm->iommu != NULL) - iommu_destroy_domain(vm->iommu); - - if (destroy) + if (destroy) { vrtc_cleanup(vm->vrtc); - else + } else { vrtc_reset(vm->vrtc); + } vpmtmr_cleanup(vm->vpmtmr); vatpit_cleanup(vm->vatpit); vhpet_cleanup(vm->vhpet); vatpic_cleanup(vm->vatpic); vioapic_cleanup(vm->vioapic); - for (i = 0; i < VM_MAXCPU; i++) - vcpu_cleanup(vm, i, destroy); - - VMCLEANUP(vm->cookie); + VM_CLEANUP(vm->cookie); if (destroy) { - for (i = 0; i < vm->num_mem_segs; i++) - vm_free_mem_seg(vm, &vm->mem_segs[i]); + for (i = 0; i < vm->num_mem_segs; i++) { + vm_free_mem_seg(&vm->mem_segs[i]); + } vm->num_mem_segs = 0; - - VMSPACE_FREE(vm->vmspace); - vm->vmspace = NULL; } } @@ -478,7 +383,7 @@ void vm_destroy(struct vm *vm) { vm_cleanup(vm, true); - free(vm, M_VM); + free(vm); } int @@ -501,35 +406,16 @@ vm_reinit(struct vm *vm) } const char * -vm_name(struct vm *vm) +vm_name(UNUSED struct vm *vm) { - return (vm->name); + return "VM"; } -int -vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa) -{ - vm_object_t obj; - - if ((obj = vmm_mmio_alloc(vm->vmspace, gpa, len, hpa)) == NULL) - return (ENOMEM); - else - return (0); -} - -int -vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len) -{ - - vmm_mmio_free(vm->vmspace, gpa, len); - return (0); -} - -boolean_t -vm_mem_allocated(struct vm *vm, vm_paddr_t gpa) +bool +vm_mem_allocated(struct vm *vm, uint64_t gpa) { int i; - vm_paddr_t gpabase, gpalimit; + uint64_t gpabase, gpalimit; for (i = 0; i < vm->num_mem_segs; i++) { gpabase = vm->mem_segs[i].gpa; @@ -538,21 +424,18 @@ vm_mem_allocated(struct vm *vm, vm_paddr_t gpa) return (TRUE); /* 'gpa' is regular memory */ } - if (ppt_is_mmio(vm, gpa)) - return (TRUE); /* 'gpa' is pci passthru mmio */ - return (FALSE); } int -vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len) +vm_malloc(struct vm *vm, uint64_t gpa, size_t len) { int available, allocated; struct mem_seg *seg; - vm_object_t object; - vm_paddr_t g; + void *object; + uint64_t g; - if ((gpa & PAGE_MASK) || (len & PAGE_MASK) || len == 0) + if ((gpa & XHYVE_PAGE_MASK) || (len & XHYVE_PAGE_MASK) || len == 0) return (EINVAL); available = allocated = 0; @@ -563,7 +446,7 @@ vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len) else available++; - g += PAGE_SIZE; + g += XHYVE_PAGE_SIZE; } /* @@ -585,220 +468,32 @@ vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len) seg = &vm->mem_segs[vm->num_mem_segs]; - if ((object = vmm_mem_alloc(vm->vmspace, gpa, len)) == NULL) + if ((object = vmm_mem_alloc(gpa, len)) == NULL) return (ENOMEM); seg->gpa = gpa; seg->len = len; seg->object = object; - seg->wired = FALSE; vm->num_mem_segs++; return (0); } -static vm_paddr_t -vm_maxmem(struct vm *vm) -{ - int i; - vm_paddr_t gpa, maxmem; - - maxmem = 0; - for (i = 0; i < vm->num_mem_segs; i++) { - gpa = vm->mem_segs[i].gpa + vm->mem_segs[i].len; - if (gpa > maxmem) - maxmem = gpa; - } - return (maxmem); -} - -static void -vm_gpa_unwire(struct vm *vm) -{ - int i, rv; - struct mem_seg *seg; - - for (i = 0; i < vm->num_mem_segs; i++) { - seg = &vm->mem_segs[i]; - if (!seg->wired) - continue; - - rv = vm_map_unwire(&vm->vmspace->vm_map, - seg->gpa, seg->gpa + seg->len, - VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES); - KASSERT(rv == KERN_SUCCESS, ("vm(%s) memory segment " - "%#lx/%ld could not be unwired: %d", - vm_name(vm), seg->gpa, seg->len, rv)); - - seg->wired = FALSE; - } -} - -static int -vm_gpa_wire(struct vm *vm) -{ - int i, rv; - struct mem_seg *seg; - - for (i = 0; i < vm->num_mem_segs; i++) { - seg = &vm->mem_segs[i]; - if (seg->wired) - continue; - - /* XXX rlimits? */ - rv = vm_map_wire(&vm->vmspace->vm_map, - seg->gpa, seg->gpa + seg->len, - VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES); - if (rv != KERN_SUCCESS) - break; - - seg->wired = TRUE; - } - - if (i < vm->num_mem_segs) { - /* - * Undo the wiring before returning an error. - */ - vm_gpa_unwire(vm); - return (EAGAIN); - } - - return (0); -} - -static void -vm_iommu_modify(struct vm *vm, boolean_t map) -{ - int i, sz; - vm_paddr_t gpa, hpa; - struct mem_seg *seg; - void *vp, *cookie, *host_domain; - - sz = PAGE_SIZE; - host_domain = iommu_host_domain(); - - for (i = 0; i < vm->num_mem_segs; i++) { - seg = &vm->mem_segs[i]; - KASSERT(seg->wired, ("vm(%s) memory segment %#lx/%ld not wired", - vm_name(vm), seg->gpa, seg->len)); - - gpa = seg->gpa; - while (gpa < seg->gpa + seg->len) { - vp = vm_gpa_hold(vm, gpa, PAGE_SIZE, VM_PROT_WRITE, - &cookie); - KASSERT(vp != NULL, ("vm(%s) could not map gpa %#lx", - vm_name(vm), gpa)); - - vm_gpa_release(cookie); - - hpa = DMAP_TO_PHYS((uintptr_t)vp); - if (map) { - iommu_create_mapping(vm->iommu, gpa, hpa, sz); - iommu_remove_mapping(host_domain, hpa, sz); - } else { - iommu_remove_mapping(vm->iommu, gpa, sz); - iommu_create_mapping(host_domain, hpa, hpa, sz); - } - - gpa += PAGE_SIZE; - } - } - - /* - * Invalidate the cached translations associated with the domain - * from which pages were removed. - */ - if (map) - iommu_invalidate_tlb(host_domain); - else - iommu_invalidate_tlb(vm->iommu); -} - -#define vm_iommu_unmap(vm) vm_iommu_modify((vm), FALSE) -#define vm_iommu_map(vm) vm_iommu_modify((vm), TRUE) - -int -vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func) -{ - int error; - - error = ppt_unassign_device(vm, bus, slot, func); - if (error) - return (error); - - if (ppt_assigned_devices(vm) == 0) { - vm_iommu_unmap(vm); - vm_gpa_unwire(vm); - } - return (0); -} - -int -vm_assign_pptdev(struct vm *vm, int bus, int slot, int func) -{ - int error; - vm_paddr_t maxaddr; - - /* - * Virtual machines with pci passthru devices get special treatment: - * - the guest physical memory is wired - * - the iommu is programmed to do the 'gpa' to 'hpa' translation - * - * We need to do this before the first pci passthru device is attached. - */ - if (ppt_assigned_devices(vm) == 0) { - KASSERT(vm->iommu == NULL, - ("vm_assign_pptdev: iommu must be NULL")); - maxaddr = vm_maxmem(vm); - vm->iommu = iommu_create_domain(maxaddr); - - error = vm_gpa_wire(vm); - if (error) - return (error); - - vm_iommu_map(vm); - } - - error = ppt_assign_device(vm, bus, slot, func); - return (error); -} - void * -vm_gpa_hold(struct vm *vm, vm_paddr_t gpa, size_t len, int reqprot, - void **cookie) -{ - int count, pageoff; - vm_page_t m; +vm_gpa2hva(struct vm *vm, uint64_t gpa, uint64_t len) { + void *base; + uint64_t offset; - pageoff = gpa & PAGE_MASK; - if (len > PAGE_SIZE - pageoff) - panic("vm_gpa_hold: invalid gpa/len: 0x%016lx/%lu", gpa, len); - - count = vm_fault_quick_hold_pages(&vm->vmspace->vm_map, - trunc_page(gpa), PAGE_SIZE, reqprot, &m, 1); - - if (count == 1) { - *cookie = m; - return ((void *)(PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)) + pageoff)); - } else { - *cookie = NULL; - return (NULL); + if (vm_get_memobj(vm, gpa, len, &offset, &base)) { + return NULL; } -} -void -vm_gpa_release(void *cookie) -{ - vm_page_t m = cookie; - - vm_page_lock(m); - vm_page_unhold(m); - vm_page_unlock(m); + return (void *) (((uintptr_t) base) + offset); } int -vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, +vm_gpabase2memseg(struct vm *vm, uint64_t gpabase, struct vm_memory_segment *seg) { int i; @@ -807,7 +502,6 @@ vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, if (gpabase == vm->mem_segs[i].gpa) { seg->gpa = vm->mem_segs[i].gpa; seg->len = vm->mem_segs[i].len; - seg->wired = vm->mem_segs[i].wired; return (0); } } @@ -815,13 +509,13 @@ vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, } int -vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len, - vm_offset_t *offset, struct vm_object **object) +vm_get_memobj(struct vm *vm, uint64_t gpa, size_t len, + uint64_t *offset, void **object) { int i; size_t seg_len; - vm_paddr_t seg_gpa; - vm_object_t seg_obj; + uint64_t seg_gpa; + void *seg_obj; for (i = 0; i < vm->num_mem_segs; i++) { if ((seg_obj = vm->mem_segs[i].object) == NULL) @@ -830,10 +524,9 @@ vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len, seg_gpa = vm->mem_segs[i].gpa; seg_len = vm->mem_segs[i].len; - if (gpa >= seg_gpa && gpa < seg_gpa + seg_len) { + if ((gpa >= seg_gpa) && ((gpa + len) <= (seg_gpa + seg_len))) { *offset = gpa - seg_gpa; *object = seg_obj; - vm_object_reference(seg_obj); return (0); } } @@ -871,16 +564,15 @@ vm_set_register(struct vm *vm, int vcpuid, int reg, uint64_t val) return (error); /* Set 'nextrip' to match the value of %rip */ - VCPU_CTR1(vm, vcpuid, "Setting nextrip to %#lx", val); + VCPU_CTR1(vm, vcpuid, "Setting nextrip to %#llx", val); vcpu = &vm->vcpu[vcpuid]; vcpu->nextrip = val; return (0); } -static boolean_t +static bool is_descriptor_table(int reg) { - switch (reg) { case VM_REG_GUEST_IDTR: case VM_REG_GUEST_GDTR: @@ -890,10 +582,9 @@ is_descriptor_table(int reg) } } -static boolean_t +static bool is_segment_register(int reg) { - switch (reg) { case VM_REG_GUEST_ES: case VM_REG_GUEST_CS: @@ -913,7 +604,6 @@ int vm_get_seg_desc(struct vm *vm, int vcpu, int reg, struct seg_desc *desc) { - if (vcpu < 0 || vcpu >= VM_MAXCPU) return (EINVAL); @@ -936,56 +626,14 @@ vm_set_seg_desc(struct vm *vm, int vcpu, int reg, return (VMSETDESC(vm->cookie, vcpu, reg, desc)); } -static void -restore_guest_fpustate(struct vcpu *vcpu) -{ - - /* flush host state to the pcb */ - fpuexit(curthread); - - /* restore guest FPU state */ - fpu_stop_emulating(); - fpurestore(vcpu->guestfpu); - - /* restore guest XCR0 if XSAVE is enabled in the host */ - if (rcr4() & CR4_XSAVE) - load_xcr(0, vcpu->guest_xcr0); - - /* - * The FPU is now "dirty" with the guest's state so turn on emulation - * to trap any access to the FPU by the host. - */ - fpu_start_emulating(); -} - -static void -save_guest_fpustate(struct vcpu *vcpu) -{ - - if ((rcr0() & CR0_TS) == 0) - panic("fpu emulation not enabled in host!"); - - /* save guest XCR0 and restore host XCR0 */ - if (rcr4() & CR4_XSAVE) { - vcpu->guest_xcr0 = rxcr(0); - load_xcr(0, vmm_get_host_xcr0()); - } - - /* save guest FPU state */ - fpu_stop_emulating(); - fpusave(vcpu->guestfpu); - fpu_start_emulating(); -} - -static VMM_STAT(VCPU_IDLE_TICKS, "number of ticks vcpu was idle"); +// static VMM_STAT(VCPU_IDLE_TICKS, "number of ticks vcpu was idle"); static int vcpu_set_state_locked(struct vcpu *vcpu, enum vcpu_state newstate, bool from_idle) { int error; - - vcpu_assert_locked(vcpu); + const struct timespec ts = {.tv_sec = 1, .tv_nsec = 0}; /* 1 second */ /* * State transitions from the vmmdev_ioctl() must always begin from @@ -993,21 +641,20 @@ vcpu_set_state_locked(struct vcpu *vcpu, enum vcpu_state newstate, * ioctl() operating on a vcpu at any point. */ if (from_idle) { - while (vcpu->state != VCPU_IDLE) - msleep_spin(&vcpu->state, &vcpu->mtx, "vmstat", hz); + while (vcpu->state != VCPU_IDLE) { + pthread_mutex_lock(&vcpu->state_sleep_mtx); + vcpu_unlock(vcpu); + pthread_cond_timedwait_relative_np(&vcpu->state_sleep_cnd, + &vcpu->state_sleep_mtx, &ts); + vcpu_lock(vcpu); + pthread_mutex_unlock(&vcpu->state_sleep_mtx); + //msleep_spin(&vcpu->state, &vcpu->mtx, "vmstat", hz); + } } else { KASSERT(vcpu->state != VCPU_IDLE, ("invalid transition from " "vcpu idle state")); } - if (vcpu->state == VCPU_RUNNING) { - KASSERT(vcpu->hostcpu == curcpu, ("curcpu %d and hostcpu %d " - "mismatch for running vcpu", curcpu, vcpu->hostcpu)); - } else { - KASSERT(vcpu->hostcpu == NOCPU, ("Invalid hostcpu %d for a " - "vcpu that is not running", vcpu->hostcpu)); - } - /* * The following state transitions are allowed: * IDLE -> FROZEN -> IDLE @@ -1023,22 +670,16 @@ vcpu_set_state_locked(struct vcpu *vcpu, enum vcpu_state newstate, case VCPU_FROZEN: error = (newstate == VCPU_FROZEN); break; - default: - error = 1; - break; } if (error) return (EBUSY); vcpu->state = newstate; - if (newstate == VCPU_RUNNING) - vcpu->hostcpu = curcpu; - else - vcpu->hostcpu = NOCPU; if (newstate == VCPU_IDLE) - wakeup(&vcpu->state); + pthread_cond_broadcast(&vcpu->state_sleep_cnd); + //wakeup(&vcpu->state); return (0); } @@ -1049,7 +690,7 @@ vcpu_require_state(struct vm *vm, int vcpuid, enum vcpu_state newstate) int error; if ((error = vcpu_set_state(vm, vcpuid, newstate, false)) != 0) - panic("Error %d setting state to %d\n", error, newstate); + xhyve_abort("Error %d setting state to %d\n", error, newstate); } static void @@ -1058,15 +699,12 @@ vcpu_require_state_locked(struct vcpu *vcpu, enum vcpu_state newstate) int error; if ((error = vcpu_set_state_locked(vcpu, newstate, false)) != 0) - panic("Error %d setting state to %d", error, newstate); + xhyve_abort("Error %d setting state to %d", error, newstate); } static void vm_set_rendezvous_func(struct vm *vm, vm_rendezvous_func_t func) { - - KASSERT(mtx_owned(&vm->rendezvous_mtx), ("rendezvous_mtx not locked")); - /* * Update 'rendezvous_func' and execute a write memory barrier to * ensure that it is visible across all host cpus. This is not needed @@ -1077,12 +715,13 @@ vm_set_rendezvous_func(struct vm *vm, vm_rendezvous_func_t func) wmb(); } -#define RENDEZVOUS_CTR0(vm, vcpuid, fmt) \ - do { \ - if (vcpuid >= 0) \ - VCPU_CTR0(vm, vcpuid, fmt); \ - else \ - VM_CTR0(vm, fmt); \ +#define RENDEZVOUS_CTR0(vm, vcpuid, fmt) \ + do { \ + if (vcpuid >= 0) {\ + VCPU_CTR0(vm, vcpuid, fmt); \ + } else {\ + VM_CTR0(vm, fmt); \ + } \ } while (0) static void @@ -1092,43 +731,46 @@ vm_handle_rendezvous(struct vm *vm, int vcpuid) KASSERT(vcpuid == -1 || (vcpuid >= 0 && vcpuid < VM_MAXCPU), ("vm_handle_rendezvous: invalid vcpuid %d", vcpuid)); - mtx_lock(&vm->rendezvous_mtx); + pthread_mutex_lock(&vm->rendezvous_mtx); while (vm->rendezvous_func != NULL) { /* 'rendezvous_req_cpus' must be a subset of 'active_cpus' */ CPU_AND(&vm->rendezvous_req_cpus, &vm->active_cpus); if (vcpuid != -1 && - CPU_ISSET(vcpuid, &vm->rendezvous_req_cpus) && - !CPU_ISSET(vcpuid, &vm->rendezvous_done_cpus)) { + CPU_ISSET(((unsigned) vcpuid), &vm->rendezvous_req_cpus) && + !CPU_ISSET(((unsigned) vcpuid), &vm->rendezvous_done_cpus)) { VCPU_CTR0(vm, vcpuid, "Calling rendezvous func"); (*vm->rendezvous_func)(vm, vcpuid, vm->rendezvous_arg); - CPU_SET(vcpuid, &vm->rendezvous_done_cpus); + CPU_SET(((unsigned) vcpuid), &vm->rendezvous_done_cpus); } if (CPU_CMP(&vm->rendezvous_req_cpus, &vm->rendezvous_done_cpus) == 0) { VCPU_CTR0(vm, vcpuid, "Rendezvous completed"); vm_set_rendezvous_func(vm, NULL); - wakeup(&vm->rendezvous_func); + pthread_cond_broadcast(&vm->rendezvous_sleep_cnd); + //wakeup(&vm->rendezvous_func); break; } RENDEZVOUS_CTR0(vm, vcpuid, "Wait for rendezvous completion"); - mtx_sleep(&vm->rendezvous_func, &vm->rendezvous_mtx, 0, - "vmrndv", 0); + pthread_cond_wait(&vm->rendezvous_sleep_cnd, &vm->rendezvous_mtx); + //mtx_sleep(&vm->rendezvous_func, &vm->rendezvous_mtx, 0, "vmrndv", 0); } - mtx_unlock(&vm->rendezvous_mtx); + pthread_mutex_unlock(&vm->rendezvous_mtx); } /* * Emulate a guest 'hlt' by sleeping until the vcpu is ready to run. */ static int -vm_handle_hlt(struct vm *vm, int vcpuid, bool intr_disabled, bool *retu) +vm_handle_hlt(struct vm *vm, int vcpuid, bool intr_disabled) { struct vcpu *vcpu; const char *wmesg; - int t, vcpu_halted, vm_halted; + int vcpu_halted, vm_halted; + const struct timespec ts = {.tv_sec = 1, .tv_nsec = 0}; /* 1 second */ - KASSERT(!CPU_ISSET(vcpuid, &vm->halted_cpus), ("vcpu already halted")); + KASSERT(!CPU_ISSET(((unsigned) vcpuid), &vm->halted_cpus), + ("vcpu already halted")); vcpu = &vm->vcpu[vcpuid]; vcpu_halted = 0; @@ -1156,10 +798,6 @@ vm_handle_hlt(struct vm *vm, int vcpuid, bool intr_disabled, bool *retu) } } - /* Don't go to sleep if the vcpu thread needs to yield */ - if (vcpu_should_yield(vm, vcpuid)) - break; - /* * Some Linux guests implement "halt" by having all vcpus * execute HLT with interrupts disabled. 'halted_cpus' keeps @@ -1171,7 +809,7 @@ vm_handle_hlt(struct vm *vm, int vcpuid, bool intr_disabled, bool *retu) VCPU_CTR0(vm, vcpuid, "Halted"); if (!vcpu_halted && halt_detection_enabled) { vcpu_halted = 1; - CPU_SET_ATOMIC(vcpuid, &vm->halted_cpus); + CPU_SET_ATOMIC(((unsigned) vcpuid), &vm->halted_cpus); } if (CPU_CMP(&vm->halted_cpus, &vm->active_cpus) == 0) { vm_halted = 1; @@ -1181,19 +819,25 @@ vm_handle_hlt(struct vm *vm, int vcpuid, bool intr_disabled, bool *retu) wmesg = "vmidle"; } - t = ticks; + //t = ticks; vcpu_require_state_locked(vcpu, VCPU_SLEEPING); /* * XXX msleep_spin() cannot be interrupted by signals so * wake up periodically to check pending signals. */ - msleep_spin(vcpu, &vcpu->mtx, wmesg, hz); + pthread_mutex_lock(&vcpu->vcpu_sleep_mtx); + vcpu_unlock(vcpu); + pthread_cond_timedwait_relative_np(&vcpu->vcpu_sleep_cnd, + &vcpu->vcpu_sleep_mtx, &ts); + vcpu_lock(vcpu); + pthread_mutex_unlock(&vcpu->vcpu_sleep_mtx); + //msleep_spin(vcpu, &vcpu->mtx, wmesg, hz); vcpu_require_state_locked(vcpu, VCPU_FROZEN); - vmm_stat_incr(vm, vcpuid, VCPU_IDLE_TICKS, ticks - t); + //vmm_stat_incr(vm, vcpuid, VCPU_IDLE_TICKS, ticks - t); } if (vcpu_halted) - CPU_CLR_ATOMIC(vcpuid, &vm->halted_cpus); + CPU_CLR_ATOMIC(((unsigned) vcpuid), &vm->halted_cpus); vcpu_unlock(vcpu); @@ -1203,48 +847,6 @@ vm_handle_hlt(struct vm *vm, int vcpuid, bool intr_disabled, bool *retu) return (0); } -static int -vm_handle_paging(struct vm *vm, int vcpuid, bool *retu) -{ - int rv, ftype; - struct vm_map *map; - struct vcpu *vcpu; - struct vm_exit *vme; - - vcpu = &vm->vcpu[vcpuid]; - vme = &vcpu->exitinfo; - - KASSERT(vme->inst_length == 0, ("%s: invalid inst_length %d", - __func__, vme->inst_length)); - - ftype = vme->u.paging.fault_type; - KASSERT(ftype == VM_PROT_READ || - ftype == VM_PROT_WRITE || ftype == VM_PROT_EXECUTE, - ("vm_handle_paging: invalid fault_type %d", ftype)); - - if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) { - rv = pmap_emulate_accessed_dirty(vmspace_pmap(vm->vmspace), - vme->u.paging.gpa, ftype); - if (rv == 0) { - VCPU_CTR2(vm, vcpuid, "%s bit emulation for gpa %#lx", - ftype == VM_PROT_READ ? "accessed" : "dirty", - vme->u.paging.gpa); - goto done; - } - } - - map = &vm->vmspace->vm_map; - rv = vm_fault(map, vme->u.paging.gpa, ftype, VM_FAULT_NORMAL); - - VCPU_CTR3(vm, vcpuid, "vm_handle_paging rv = %d, gpa = %#lx, " - "ftype = %d", rv, vme->u.paging.gpa, ftype); - - if (rv != KERN_SUCCESS) - return (EFAULT); -done: - return (0); -} - static int vm_handle_inst_emul(struct vm *vm, int vcpuid, bool *retu) { @@ -1269,7 +871,7 @@ vm_handle_inst_emul(struct vm *vm, int vcpuid, bool *retu) paging = &vme->u.inst_emul.paging; cpu_mode = paging->cpu_mode; - VCPU_CTR1(vm, vcpuid, "inst_emul fault accessing gpa %#lx", gpa); + VCPU_CTR1(vm, vcpuid, "inst_emul fault accessing gpa %#llx", gpa); /* Fetch, decode and emulate the faulting instruction */ if (vie->num_valid == 0) { @@ -1290,7 +892,7 @@ vm_handle_inst_emul(struct vm *vm, int vcpuid, bool *retu) return (error); if (vmm_decode_instruction(vm, vcpuid, gla, cpu_mode, cs_d, vie) != 0) { - VCPU_CTR1(vm, vcpuid, "Error decoding instruction at %#lx", + VCPU_CTR1(vm, vcpuid, "Error decoding instruction at %#llx", vme->rip + cs_base); *retu = true; /* dump instruction bytes in userspace */ return (0); @@ -1306,7 +908,7 @@ vm_handle_inst_emul(struct vm *vm, int vcpuid, bool *retu) } /* return to userland unless this is an in-kernel emulated device */ - if (gpa >= DEFAULT_APIC_BASE && gpa < DEFAULT_APIC_BASE + PAGE_SIZE) { + if (gpa >= DEFAULT_APIC_BASE && gpa < DEFAULT_APIC_BASE + XHYVE_PAGE_SIZE) { mread = lapic_mmio_read; mwrite = lapic_mmio_write; } else if (gpa >= VIOAPIC_BASE && gpa < VIOAPIC_BASE + VIOAPIC_SIZE) { @@ -1331,11 +933,12 @@ vm_handle_suspend(struct vm *vm, int vcpuid, bool *retu) { int i, done; struct vcpu *vcpu; + const struct timespec ts = {.tv_sec = 1, .tv_nsec = 0}; /* 1 second */ done = 0; vcpu = &vm->vcpu[vcpuid]; - CPU_SET_ATOMIC(vcpuid, &vm->suspended_cpus); + CPU_SET_ATOMIC(((unsigned) vcpuid), &vm->suspended_cpus); /* * Wait until all 'active_cpus' have suspended themselves. @@ -1354,7 +957,15 @@ vm_handle_suspend(struct vm *vm, int vcpuid, bool *retu) if (vm->rendezvous_func == NULL) { VCPU_CTR0(vm, vcpuid, "Sleeping during suspend"); vcpu_require_state_locked(vcpu, VCPU_SLEEPING); - msleep_spin(vcpu, &vcpu->mtx, "vmsusp", hz); + + pthread_mutex_lock(&vcpu->vcpu_sleep_mtx); + vcpu_unlock(vcpu); + pthread_cond_timedwait_relative_np(&vcpu->vcpu_sleep_cnd, + &vcpu->vcpu_sleep_mtx, &ts); + vcpu_lock(vcpu); + pthread_mutex_unlock(&vcpu->vcpu_sleep_mtx); + //msleep_spin(vcpu, &vcpu->mtx, "vmsusp", hz); + vcpu_require_state_locked(vcpu, VCPU_FROZEN); } else { VCPU_CTR0(vm, vcpuid, "Rendezvous during suspend"); @@ -1369,7 +980,7 @@ vm_handle_suspend(struct vm *vm, int vcpuid, bool *retu) * Wakeup the other sleeping vcpus and return to userspace. */ for (i = 0; i < VM_MAXCPU; i++) { - if (CPU_ISSET(i, &vm->suspended_cpus)) { + if (CPU_ISSET(((unsigned) i), &vm->suspended_cpus)) { vcpu_notify_event(vm, i, false); } } @@ -1386,7 +997,7 @@ vm_suspend(struct vm *vm, enum vm_suspend_how how) if (how <= VM_SUSPEND_NONE || how >= VM_SUSPEND_LAST) return (EINVAL); - if (atomic_cmpset_int(&vm->suspend, 0, how) == 0) { + if (atomic_cmpset_int(((volatile u_int *) &vm->suspend), 0, how) == 0) { VM_CTR2(vm, "virtual machine already suspended %d/%d", vm->suspend, how); return (EALREADY); @@ -1398,7 +1009,7 @@ vm_suspend(struct vm *vm, enum vm_suspend_how how) * Notify all active vcpus that they are now suspended. */ for (i = 0; i < VM_MAXCPU; i++) { - if (CPU_ISSET(i, &vm->active_cpus)) + if (CPU_ISSET(((unsigned) i), &vm->active_cpus)) vcpu_notify_event(vm, i, false); } @@ -1417,7 +1028,7 @@ vm_exit_suspended(struct vm *vm, int vcpuid, uint64_t rip) vmexit->rip = rip; vmexit->inst_length = 0; vmexit->exitcode = VM_EXITCODE_SUSPENDED; - vmexit->u.suspended.how = vm->suspend; + vmexit->u.suspended.how = (enum vm_suspend_how) vm->suspend; } void @@ -1434,73 +1045,47 @@ vm_exit_rendezvous(struct vm *vm, int vcpuid, uint64_t rip) vmm_stat_incr(vm, vcpuid, VMEXIT_RENDEZVOUS, 1); } -void -vm_exit_astpending(struct vm *vm, int vcpuid, uint64_t rip) -{ - struct vm_exit *vmexit; - - vmexit = vm_exitinfo(vm, vcpuid); - vmexit->rip = rip; - vmexit->inst_length = 0; - vmexit->exitcode = VM_EXITCODE_BOGUS; - vmm_stat_incr(vm, vcpuid, VMEXIT_ASTPENDING, 1); -} +void pittest(struct vm *thevm); int -vm_run(struct vm *vm, struct vm_run *vmrun) +vm_run(struct vm *vm, int vcpuid, struct vm_exit *vm_exit) { - int error, vcpuid; + int error; struct vcpu *vcpu; - struct pcb *pcb; - uint64_t tscval; + // uint64_t tscval; struct vm_exit *vme; bool retu, intr_disabled; - pmap_t pmap; void *rptr, *sptr; - vcpuid = vmrun->cpuid; - if (vcpuid < 0 || vcpuid >= VM_MAXCPU) return (EINVAL); - if (!CPU_ISSET(vcpuid, &vm->active_cpus)) + if (!CPU_ISSET(((unsigned) vcpuid), &vm->active_cpus)) return (EINVAL); - if (CPU_ISSET(vcpuid, &vm->suspended_cpus)) + if (CPU_ISSET(((unsigned) vcpuid), &vm->suspended_cpus)) return (EINVAL); rptr = &vm->rendezvous_func; sptr = &vm->suspend; - pmap = vmspace_pmap(vm->vmspace); vcpu = &vm->vcpu[vcpuid]; vme = &vcpu->exitinfo; + retu = false; + restart: - critical_enter(); - - KASSERT(!CPU_ISSET(curcpu, &pmap->pm_active), - ("vm_run: absurd pm_active")); - - tscval = rdtsc(); - - pcb = PCPU_GET(curpcb); - set_pcb_flags(pcb, PCB_FULL_IRET); - - restore_guest_fpustate(vcpu); + // tscval = rdtsc(); vcpu_require_state(vm, vcpuid, VCPU_RUNNING); - error = VMRUN(vm->cookie, vcpuid, vcpu->nextrip, pmap, rptr, sptr); + error = VMRUN(vm->cookie, vcpuid, (register_t) vcpu->nextrip, rptr, sptr); vcpu_require_state(vm, vcpuid, VCPU_FROZEN); - save_guest_fpustate(vcpu); - vmm_stat_incr(vm, vcpuid, VCPU_TOTAL_RUNTIME, rdtsc() - tscval); - - critical_exit(); + // vmm_stat_incr(vm, vcpuid, VCPU_TOTAL_RUNTIME, rdtsc() - tscval); if (error == 0) { retu = false; - vcpu->nextrip = vme->rip + vme->inst_length; - switch (vme->exitcode) { + vcpu->nextrip = vme->rip + ((unsigned) vme->inst_length); + switch (((int) (vme->exitcode))) { case VM_EXITCODE_SUSPENDED: error = vm_handle_suspend(vm, vcpuid, &retu); break; @@ -1514,10 +1099,10 @@ restart: break; case VM_EXITCODE_HLT: intr_disabled = ((vme->u.hlt.rflags & PSL_I) == 0); - error = vm_handle_hlt(vm, vcpuid, intr_disabled, &retu); + error = vm_handle_hlt(vm, vcpuid, intr_disabled); break; case VM_EXITCODE_PAGING: - error = vm_handle_paging(vm, vcpuid, &retu); + error = 0; break; case VM_EXITCODE_INST_EMUL: error = vm_handle_inst_emul(vm, vcpuid, &retu); @@ -1539,8 +1124,8 @@ restart: if (error == 0 && retu == false) goto restart; - /* copy the exit information */ - bcopy(vme, &vmrun->vm_exit, sizeof(struct vm_exit)); + /* copy the exit information (FIXME: zero copy) */ + bcopy(vme, vm_exit, sizeof(struct vm_exit)); return (error); } @@ -1558,7 +1143,7 @@ vm_restart_instruction(void *arg, int vcpuid) return (EINVAL); vcpu = &vm->vcpu[vcpuid]; - state = vcpu_get_state(vm, vcpuid, NULL); + state = vcpu_get_state(vm, vcpuid); if (state == VCPU_RUNNING) { /* * When a vcpu is "running" the next instruction is determined @@ -1567,7 +1152,7 @@ vm_restart_instruction(void *arg, int vcpuid) * instruction to be restarted. */ vcpu->exitinfo.inst_length = 0; - VCPU_CTR1(vm, vcpuid, "restarting instruction at %#lx by " + VCPU_CTR1(vm, vcpuid, "restarting instruction at %#llx by " "setting inst_length to zero", vcpu->exitinfo.rip); } else if (state == VCPU_FROZEN) { /* @@ -1579,10 +1164,10 @@ vm_restart_instruction(void *arg, int vcpuid) error = vm_get_register(vm, vcpuid, VM_REG_GUEST_RIP, &rip); KASSERT(!error, ("%s: error %d getting rip", __func__, error)); VCPU_CTR2(vm, vcpuid, "restarting instruction by updating " - "nextrip from %#lx to %#lx", vcpu->nextrip, rip); + "nextrip from %#llx to %#llx", vcpu->nextrip, rip); vcpu->nextrip = rip; } else { - panic("%s: invalid state %d", __func__, state); + xhyve_abort("%s: invalid state %d\n", __func__, state); } return (0); } @@ -1610,7 +1195,7 @@ vm_exit_intinfo(struct vm *vm, int vcpuid, uint64_t info) } else { info = 0; } - VCPU_CTR2(vm, vcpuid, "%s: info1(%#lx)", __func__, info); + VCPU_CTR2(vm, vcpuid, "%s: info1(%#llx)", __func__, info); vcpu->exitintinfo = info; return (0); } @@ -1628,7 +1213,7 @@ exception_class(uint64_t info) { int type, vector; - KASSERT(info & VM_INTINFO_VALID, ("intinfo must be valid: %#lx", info)); + KASSERT(info & VM_INTINFO_VALID, ("intinfo must be valid: %#llx", info)); type = info & VM_INTINFO_TYPE; vector = info & 0xff; @@ -1676,8 +1261,8 @@ nested_fault(struct vm *vm, int vcpuid, uint64_t info1, uint64_t info2, enum exc_class exc1, exc2; int type1, vector1; - KASSERT(info1 & VM_INTINFO_VALID, ("info1 %#lx is not valid", info1)); - KASSERT(info2 & VM_INTINFO_VALID, ("info2 %#lx is not valid", info2)); + KASSERT(info1 & VM_INTINFO_VALID, ("info1 %#llx is not valid", info1)); + KASSERT(info2 & VM_INTINFO_VALID, ("info2 %#llx is not valid", info2)); /* * If an exception occurs while attempting to call the double-fault @@ -1686,7 +1271,7 @@ nested_fault(struct vm *vm, int vcpuid, uint64_t info1, uint64_t info2, type1 = info1 & VM_INTINFO_TYPE; vector1 = info1 & 0xff; if (type1 == VM_INTINFO_HWEXCEPTION && vector1 == IDT_DF) { - VCPU_CTR2(vm, vcpuid, "triple fault: info1(%#lx), info2(%#lx)", + VCPU_CTR2(vm, vcpuid, "triple fault: info1(%#llx), info2(%#llx)", info1, info2); vm_suspend(vm, VM_SUSPEND_TRIPLEFAULT); *retinfo = 0; @@ -1745,7 +1330,7 @@ vm_entry_intinfo(struct vm *vm, int vcpuid, uint64_t *retinfo) if (vcpu->exception_pending) { info2 = vcpu_exception_intinfo(vcpu); vcpu->exception_pending = 0; - VCPU_CTR2(vm, vcpuid, "Exception %d delivered: %#lx", + VCPU_CTR2(vm, vcpuid, "Exception %d delivered: %#llx", vcpu->exc_vector, info2); } @@ -1762,8 +1347,8 @@ vm_entry_intinfo(struct vm *vm, int vcpuid, uint64_t *retinfo) } if (valid) { - VCPU_CTR4(vm, vcpuid, "%s: info1(%#lx), info2(%#lx), " - "retinfo(%#lx)", __func__, info1, info2, *retinfo); + VCPU_CTR4(vm, vcpuid, "%s: info1(%#llx), info2(%#llx), " + "retinfo(%#llx)", __func__, info1, info2, *retinfo); } return (valid); @@ -1844,7 +1429,7 @@ vm_inject_fault(void *vmarg, int vcpuid, int vector, int errcode_valid, restart_instruction = 1; error = vm_inject_exception(vm, vcpuid, vector, errcode_valid, - errcode, restart_instruction); + ((uint32_t) errcode), restart_instruction); KASSERT(error == 0, ("vm_inject_exception error %d", error)); } @@ -1855,7 +1440,7 @@ vm_inject_pf(void *vmarg, int vcpuid, int error_code, uint64_t cr2) int error; vm = vmarg; - VCPU_CTR2(vm, vcpuid, "Injecting page fault: error_code %#x, cr2 %#lx", + VCPU_CTR2(vm, vcpuid, "Injecting page fault: error_code %#x, cr2 %#llx", error_code, cr2); error = vm_set_register(vm, vcpuid, VM_REG_GUEST_CR2, cr2); @@ -1887,7 +1472,7 @@ vm_nmi_pending(struct vm *vm, int vcpuid) struct vcpu *vcpu; if (vcpuid < 0 || vcpuid >= VM_MAXCPU) - panic("vm_nmi_pending: invalid vcpuid %d", vcpuid); + xhyve_abort("vm_nmi_pending: invalid vcpuid %d\n", vcpuid); vcpu = &vm->vcpu[vcpuid]; @@ -1900,12 +1485,12 @@ vm_nmi_clear(struct vm *vm, int vcpuid) struct vcpu *vcpu; if (vcpuid < 0 || vcpuid >= VM_MAXCPU) - panic("vm_nmi_pending: invalid vcpuid %d", vcpuid); + xhyve_abort("vm_nmi_pending: invalid vcpuid %d\n", vcpuid); vcpu = &vm->vcpu[vcpuid]; if (vcpu->nmi_pending == 0) - panic("vm_nmi_clear: inconsistent nmi_pending state"); + xhyve_abort("vm_nmi_clear: inconsistent nmi_pending state\n"); vcpu->nmi_pending = 0; vmm_stat_incr(vm, vcpuid, VCPU_NMI_COUNT, 1); @@ -1934,7 +1519,7 @@ vm_extint_pending(struct vm *vm, int vcpuid) struct vcpu *vcpu; if (vcpuid < 0 || vcpuid >= VM_MAXCPU) - panic("vm_extint_pending: invalid vcpuid %d", vcpuid); + xhyve_abort("vm_extint_pending: invalid vcpuid %d\n", vcpuid); vcpu = &vm->vcpu[vcpuid]; @@ -1947,12 +1532,12 @@ vm_extint_clear(struct vm *vm, int vcpuid) struct vcpu *vcpu; if (vcpuid < 0 || vcpuid >= VM_MAXCPU) - panic("vm_extint_pending: invalid vcpuid %d", vcpuid); + xhyve_abort("vm_extint_pending: invalid vcpuid %d\n", vcpuid); vcpu = &vm->vcpu[vcpuid]; if (vcpu->extint_pending == 0) - panic("vm_extint_clear: inconsistent extint_pending state"); + xhyve_abort("vm_extint_clear: inconsistent extint_pending state\n"); vcpu->extint_pending = 0; vmm_stat_incr(vm, vcpuid, VCPU_EXTINT_COUNT, 1); @@ -2002,55 +1587,6 @@ vm_hpet(struct vm *vm) return (vm->vhpet); } -boolean_t -vmm_is_pptdev(int bus, int slot, int func) -{ - int found, i, n; - int b, s, f; - char *val, *cp, *cp2; - - /* - * XXX - * The length of an environment variable is limited to 128 bytes which - * puts an upper limit on the number of passthru devices that may be - * specified using a single environment variable. - * - * Work around this by scanning multiple environment variable - * names instead of a single one - yuck! - */ - const char *names[] = { "pptdevs", "pptdevs2", "pptdevs3", NULL }; - - /* set pptdevs="1/2/3 4/5/6 7/8/9 10/11/12" */ - found = 0; - for (i = 0; names[i] != NULL && !found; i++) { - cp = val = kern_getenv(names[i]); - while (cp != NULL && *cp != '\0') { - if ((cp2 = strchr(cp, ' ')) != NULL) - *cp2 = '\0'; - - n = sscanf(cp, "%d/%d/%d", &b, &s, &f); - if (n == 3 && bus == b && slot == s && func == f) { - found = 1; - break; - } - - if (cp2 != NULL) - *cp2++ = ' '; - - cp = cp2; - } - freeenv(val); - } - return (found); -} - -void * -vm_iommu_domain(struct vm *vm) -{ - - return (vm->iommu); -} - int vcpu_set_state(struct vm *vm, int vcpuid, enum vcpu_state newstate, bool from_idle) @@ -2059,32 +1595,29 @@ vcpu_set_state(struct vm *vm, int vcpuid, enum vcpu_state newstate, struct vcpu *vcpu; if (vcpuid < 0 || vcpuid >= VM_MAXCPU) - panic("vm_set_run_state: invalid vcpuid %d", vcpuid); + xhyve_abort("vm_set_run_state: invalid vcpuid %d\n", vcpuid); vcpu = &vm->vcpu[vcpuid]; vcpu_lock(vcpu); error = vcpu_set_state_locked(vcpu, newstate, from_idle); vcpu_unlock(vcpu); - return (error); } enum vcpu_state -vcpu_get_state(struct vm *vm, int vcpuid, int *hostcpu) +vcpu_get_state(struct vm *vm, int vcpuid) { struct vcpu *vcpu; enum vcpu_state state; if (vcpuid < 0 || vcpuid >= VM_MAXCPU) - panic("vm_get_run_state: invalid vcpuid %d", vcpuid); + xhyve_abort("vm_get_run_state: invalid vcpuid %d\n", vcpuid); vcpu = &vm->vcpu[vcpuid]; vcpu_lock(vcpu); state = vcpu->state; - if (hostcpu != NULL) - *hostcpu = vcpu->hostcpu; vcpu_unlock(vcpu); return (state); @@ -2097,11 +1630,11 @@ vm_activate_cpu(struct vm *vm, int vcpuid) if (vcpuid < 0 || vcpuid >= VM_MAXCPU) return (EINVAL); - if (CPU_ISSET(vcpuid, &vm->active_cpus)) + if (CPU_ISSET(((unsigned) vcpuid), &vm->active_cpus)) return (EBUSY); VCPU_CTR0(vm, vcpuid, "activated"); - CPU_SET_ATOMIC(vcpuid, &vm->active_cpus); + CPU_SET_ATOMIC(((unsigned) vcpuid), &vm->active_cpus); return (0); } @@ -2161,50 +1694,40 @@ vm_set_x2apic_state(struct vm *vm, int vcpuid, enum x2apic_state state) * to the host_cpu to cause the vcpu to trap into the hypervisor. */ void -vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr) +vcpu_notify_event(struct vm *vm, int vcpuid, UNUSED bool lapic_intr) { - int hostcpu; struct vcpu *vcpu; vcpu = &vm->vcpu[vcpuid]; - vcpu_lock(vcpu); - hostcpu = vcpu->hostcpu; if (vcpu->state == VCPU_RUNNING) { - KASSERT(hostcpu != NOCPU, ("vcpu running on invalid hostcpu")); - if (hostcpu != curcpu) { - if (lapic_intr) { - vlapic_post_intr(vcpu->vlapic, hostcpu, - vmm_ipinum); - } else { - ipi_cpu(hostcpu, vmm_ipinum); - } - } else { - /* - * If the 'vcpu' is running on 'curcpu' then it must - * be sending a notification to itself (e.g. SELF_IPI). - * The pending event will be picked up when the vcpu - * transitions back to guest context. - */ - } + VCPU_INTERRUPT(vcpuid); + /* FIXME */ + // if (hostcpu != curcpu) { + // if (lapic_intr) { + // vlapic_post_intr(vcpu->vlapic, hostcpu, + // vmm_ipinum); + // } else { + // ipi_cpu(hostcpu, vmm_ipinum); + // } + // } else { + // /* + // * If the 'vcpu' is running on 'curcpu' then it must + // * be sending a notification to itself (e.g. SELF_IPI). + // * The pending event will be picked up when the vcpu + // * transitions back to guest context. + // */ + // } } else { - KASSERT(hostcpu == NOCPU, ("vcpu state %d not consistent " - "with hostcpu %d", vcpu->state, hostcpu)); if (vcpu->state == VCPU_SLEEPING) - wakeup_one(vcpu); + pthread_cond_signal(&vcpu->vcpu_sleep_cnd); + //wakeup_one(vcpu); } vcpu_unlock(vcpu); } -struct vmspace * -vm_get_vmspace(struct vm *vm) -{ - - return (vm->vmspace); -} - int -vm_apicid2vcpuid(struct vm *vm, int apicid) +vm_apicid2vcpuid(UNUSED struct vm *vm, int apicid) { /* * XXX apic id is assumed to be numerically identical to vcpu id @@ -2218,15 +1741,11 @@ vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest, { int i; - /* - * Enforce that this function is called without any locks - */ - WITNESS_WARN(WARN_PANIC, NULL, "vm_smp_rendezvous"); KASSERT(vcpuid == -1 || (vcpuid >= 0 && vcpuid < VM_MAXCPU), ("vm_smp_rendezvous: invalid vcpuid %d", vcpuid)); restart: - mtx_lock(&vm->rendezvous_mtx); + pthread_mutex_lock(&vm->rendezvous_mtx); if (vm->rendezvous_func != NULL) { /* * If a rendezvous is already in progress then we need to @@ -2234,7 +1753,7 @@ restart: * of the targets of the rendezvous. */ RENDEZVOUS_CTR0(vm, vcpuid, "Rendezvous already in progress"); - mtx_unlock(&vm->rendezvous_mtx); + pthread_mutex_unlock(&vm->rendezvous_mtx); vm_handle_rendezvous(vm, vcpuid); goto restart; } @@ -2246,14 +1765,14 @@ restart: CPU_ZERO(&vm->rendezvous_done_cpus); vm->rendezvous_arg = arg; vm_set_rendezvous_func(vm, func); - mtx_unlock(&vm->rendezvous_mtx); + pthread_mutex_unlock(&vm->rendezvous_mtx); /* * Wake up any sleeping vcpus and trigger a VM-exit in any running * vcpus so they handle the rendezvous as soon as possible. */ for (i = 0; i < VM_MAXCPU; i++) { - if (CPU_ISSET(i, &dest)) + if (CPU_ISSET(((unsigned) i), &dest)) vcpu_notify_event(vm, i, false); } @@ -2298,22 +1817,16 @@ vm_segment_name(int seg) VM_REG_GUEST_GS }; - KASSERT(seg >= 0 && seg < nitems(seg_names), + KASSERT(seg >= 0 && seg < ((int) nitems(seg_names)), ("%s: invalid segment encoding %d", __func__, seg)); return (seg_names[seg]); } void -vm_copy_teardown(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo, - int num_copyinfo) +vm_copy_teardown(UNUSED struct vm *vm, UNUSED int vcpuid, + struct vm_copyinfo *copyinfo, int num_copyinfo) { - int idx; - - for (idx = 0; idx < num_copyinfo; idx++) { - if (copyinfo[idx].cookie != NULL) - vm_gpa_release(copyinfo[idx].cookie); - } - bzero(copyinfo, num_copyinfo * sizeof(struct vm_copyinfo)); + bzero(copyinfo, ((unsigned) num_copyinfo) * sizeof(struct vm_copyinfo)); } int @@ -2323,10 +1836,10 @@ vm_copy_setup(struct vm *vm, int vcpuid, struct vm_guest_paging *paging, { int error, idx, nused; size_t n, off, remaining; - void *hva, *cookie; + void *hva; uint64_t gpa; - bzero(copyinfo, sizeof(struct vm_copyinfo) * num_copyinfo); + bzero(copyinfo, sizeof(struct vm_copyinfo) * ((unsigned) num_copyinfo)); nused = 0; remaining = len; @@ -2335,8 +1848,8 @@ vm_copy_setup(struct vm *vm, int vcpuid, struct vm_guest_paging *paging, error = vm_gla2gpa(vm, vcpuid, paging, gla, prot, &gpa, fault); if (error || *fault) return (error); - off = gpa & PAGE_MASK; - n = min(remaining, PAGE_SIZE - off); + off = gpa & XHYVE_PAGE_MASK; + n = min(remaining, XHYVE_PAGE_SIZE - off); copyinfo[nused].gpa = gpa; copyinfo[nused].len = n; remaining -= n; @@ -2345,12 +1858,10 @@ vm_copy_setup(struct vm *vm, int vcpuid, struct vm_guest_paging *paging, } for (idx = 0; idx < nused; idx++) { - hva = vm_gpa_hold(vm, copyinfo[idx].gpa, copyinfo[idx].len, - prot, &cookie); + hva = vm_gpa2hva(vm, copyinfo[idx].gpa, copyinfo[idx].len); if (hva == NULL) break; copyinfo[idx].hva = hva; - copyinfo[idx].cookie = cookie; } if (idx != nused) { @@ -2363,8 +1874,8 @@ vm_copy_setup(struct vm *vm, int vcpuid, struct vm_guest_paging *paging, } void -vm_copyin(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo, void *kaddr, - size_t len) +vm_copyin(UNUSED struct vm *vm, UNUSED int vcpuid, struct vm_copyinfo *copyinfo, + void *kaddr, size_t len) { char *dst; int idx; @@ -2380,7 +1891,7 @@ vm_copyin(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo, void *kaddr, } void -vm_copyout(struct vm *vm, int vcpuid, const void *kaddr, +vm_copyout(UNUSED struct vm *vm, UNUSED int vcpuid, const void *kaddr, struct vm_copyinfo *copyinfo, size_t len) { const char *src; @@ -2395,33 +1906,3 @@ vm_copyout(struct vm *vm, int vcpuid, const void *kaddr, idx++; } } - -/* - * Return the amount of in-use and wired memory for the VM. Since - * these are global stats, only return the values with for vCPU 0 - */ -VMM_STAT_DECLARE(VMM_MEM_RESIDENT); -VMM_STAT_DECLARE(VMM_MEM_WIRED); - -static void -vm_get_rescnt(struct vm *vm, int vcpu, struct vmm_stat_type *stat) -{ - - if (vcpu == 0) { - vmm_stat_set(vm, vcpu, VMM_MEM_RESIDENT, - PAGE_SIZE * vmspace_resident_count(vm->vmspace)); - } -} - -static void -vm_get_wiredcnt(struct vm *vm, int vcpu, struct vmm_stat_type *stat) -{ - - if (vcpu == 0) { - vmm_stat_set(vm, vcpu, VMM_MEM_WIRED, - PAGE_SIZE * pmap_wired_count(vmspace_pmap(vm->vmspace))); - } -} - -VMM_STAT_FUNC(VMM_MEM_RESIDENT, "Resident memory", vm_get_rescnt); -VMM_STAT_FUNC(VMM_MEM_WIRED, "Wired memory", vm_get_wiredcnt); diff --git a/src/vmm/vmm_api.c b/src/vmm/vmm_api.c new file mode 100644 index 0000000..062473e --- /dev/null +++ b/src/vmm/vmm_api.c @@ -0,0 +1,831 @@ +/*- + * Copyright (c) 2015 xhyve developers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static struct vm *vm; +static int memflags; +static uint32_t lowmem_limit; +static enum vm_mmap_style mmap_style; +static size_t lowmem; +static void *lowmem_addr; +static size_t highmem; +static void *highmem_addr; + +static void +vcpu_freeze(int vcpu, bool freeze) +{ + enum vcpu_state state; + + state = (freeze) ? VCPU_FROZEN : VCPU_IDLE; + + if (vcpu_set_state(vm, vcpu, state, freeze)) { + xhyve_abort("vcpu_set_state failed\n"); + } +} + +static void +vcpu_freeze_all(bool freeze) +{ + enum vcpu_state state; + int vcpu; + + state = (freeze) ? VCPU_FROZEN : VCPU_IDLE; + + for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++) { + if (vcpu_set_state(vm, vcpu, state, freeze)) { + xhyve_abort("vcpu_set_state failed\n"); + } + } +} + +int +xh_vm_create(void) +{ + int error; + + if (vm != NULL) { + return (EEXIST); + } + + error = vmm_init(); + + if (error != 0) { + return (error); + } + + memflags = 0; + lowmem_limit = (3ull << 30); + + return (vm_create(&vm)); +} + +void +xh_vm_destroy(void) +{ + assert(vm != NULL); + + vm_destroy(vm); + + if (vmm_cleanup() == 0) { + vm = NULL; + } +} + +int +xh_vcpu_create(int vcpu) +{ + assert(vm != NULL); + return (vcpu_create(vm, vcpu)); +} + +void +xh_vcpu_destroy(int vcpu) +{ + assert(vm != NULL); + vcpu_destroy(vm, vcpu); +} + +int +xh_vm_get_memory_seg(uint64_t gpa, size_t *ret_len) +{ + int error; + + struct vm_memory_segment seg; + + error = vm_gpabase2memseg(vm, gpa, &seg); + + if (error == 0) { + *ret_len = seg.len; + } + + return (error); +} + +static int +setup_memory_segment(uint64_t gpa, size_t len, void **addr) +{ + void *object; + uint64_t offset; + int error; + + vcpu_freeze_all(true); + error = vm_malloc(vm, gpa, len); + if (error == 0) { + error = vm_get_memobj(vm, gpa, len, &offset, &object); + if (error == 0) { + *addr = (void *) (((uintptr_t) object) + offset); + } + } + vcpu_freeze_all(false); + return (error); +} + +int +xh_vm_setup_memory(size_t len, enum vm_mmap_style vms) +{ + void **addr; + int error; + + /* XXX VM_MMAP_SPARSE not implemented yet */ + assert(vms == VM_MMAP_NONE || vms == VM_MMAP_ALL); + + mmap_style = vms; + + /* + * If 'len' cannot fit entirely in the 'lowmem' segment then + * create another 'highmem' segment above 4GB for the remainder. + */ + + lowmem = (len > lowmem_limit) ? lowmem_limit : len; + highmem = (len > lowmem_limit) ? (len - lowmem) : 0; + + if (lowmem > 0) { + addr = (vms == VM_MMAP_ALL) ? &lowmem_addr : NULL; + if ((error = setup_memory_segment(0, lowmem, addr))) { + return (error); + } + } + + if (highmem > 0) { + addr = (vms == VM_MMAP_ALL) ? &highmem_addr : NULL; + if ((error = setup_memory_segment((4ull << 30), highmem, addr))) { + return (error); + } + } + + return (0); +} + +void * +xh_vm_map_gpa(uint64_t gpa, size_t len) +{ + assert(mmap_style == VM_MMAP_ALL); + + if ((gpa < lowmem) && ((gpa + len) <= lowmem)) { + return ((void *) (((uintptr_t) lowmem_addr) + gpa)); + } + + if (gpa >= (4ull << 30)) { + gpa -= (4ull << 30); + if ((gpa < highmem) && ((gpa + len) <= highmem)) { + return ((void *) (((uintptr_t) highmem_addr) + gpa)); + } + } + + return (NULL); +} + +int +xh_vm_gla2gpa(int vcpu, struct vm_guest_paging *paging, uint64_t gla, + int prot, uint64_t *gpa, int *fault) +{ + int error; + + vcpu_freeze(vcpu, true); + error = vm_gla2gpa(vm, vcpu, paging, gla, prot, gpa, fault); + vcpu_freeze(vcpu, false); + + return (error); +} + +uint32_t +xh_vm_get_lowmem_limit(void) +{ + return (lowmem_limit); +} + +void +xh_vm_set_lowmem_limit(uint32_t limit) +{ + lowmem_limit = limit; +} + +void +xh_vm_set_memflags(int flags) +{ + memflags = flags; +} + +size_t +xh_vm_get_lowmem_size(void) +{ + return (lowmem); +} + +size_t +xh_vm_get_highmem_size(void) +{ + return (highmem); +} + +int +xh_vm_set_desc(int vcpu, int reg, uint64_t base, uint32_t limit, + uint32_t access) +{ + struct seg_desc sd; + int error; + + sd.base = base; + sd.limit = limit; + sd.access = access; + vcpu_freeze(vcpu, true); + error = vm_set_seg_desc(vm, vcpu, reg, &sd); + vcpu_freeze(vcpu, false); + + return (error); +} + +int +xh_vm_get_desc(int vcpu, int reg, uint64_t *base, uint32_t *limit, + uint32_t *access) +{ + struct seg_desc sd; + int error; + + vcpu_freeze(vcpu, true); + error = vm_get_seg_desc(vm, vcpu, reg, &sd); + if (error == 0) { + *base = sd.base; + *limit = sd.limit; + *access = sd.access; + } + vcpu_freeze(vcpu, false); + + return (error); +} + +int +xh_vm_get_seg_desc(int vcpu, int reg, struct seg_desc *seg_desc) +{ + int error; + + error = xh_vm_get_desc(vcpu, reg, &seg_desc->base, &seg_desc->limit, + &seg_desc->access); + + return (error); +} + +int +xh_vm_set_register(int vcpu, int reg, uint64_t val) +{ + int error; + + vcpu_freeze(vcpu, true); + error = vm_set_register(vm, vcpu, reg, val); + vcpu_freeze(vcpu, false); + + return (error); +} + +int +xh_vm_get_register(int vcpu, int reg, uint64_t *retval) +{ + int error; + + vcpu_freeze(vcpu, true); + error = vm_get_register(vm, vcpu, reg, retval); + vcpu_freeze(vcpu, false); + + return (error); +} + +int +xh_vm_run(int vcpu, struct vm_exit *ret_vmexit) +{ + int error; + + vcpu_freeze(vcpu, true); + error = vm_run(vm, vcpu, ret_vmexit); + vcpu_freeze(vcpu, false); + + return (error); +} + +int +xh_vm_suspend(enum vm_suspend_how how) +{ + return (vm_suspend(vm, how)); +} + +int +xh_vm_reinit(void) +{ + int error; + + vcpu_freeze_all(true); + error = vm_reinit(vm); + vcpu_freeze_all(false); + + return (error); +} + +int +xh_vm_apicid2vcpu(int apicid) +{ + return (apicid); +} + +int +xh_vm_inject_exception(int vcpu, int vector, int errcode_valid, + uint32_t errcode, int restart_instruction) +{ + int error; + + vcpu_freeze(vcpu, true); + error = vm_inject_exception(vm, vcpu, vector, errcode_valid, errcode, + restart_instruction); + vcpu_freeze(vcpu, false); + + return (error); +} + +int +xh_vm_lapic_irq(int vcpu, int vector) +{ + return (lapic_intr_edge(vm, vcpu, vector)); +} + +int +xh_vm_lapic_local_irq(int vcpu, int vector) +{ + return (lapic_set_local_intr(vm, vcpu, vector)); +} + +int +xh_vm_lapic_msi(uint64_t addr, uint64_t msg) +{ + return (lapic_intr_msi(vm, addr, msg)); +} + +int +xh_vm_ioapic_assert_irq(int irq) +{ + return (vioapic_assert_irq(vm, irq)); +} + +int +xh_vm_ioapic_deassert_irq(int irq) +{ + return (vioapic_deassert_irq(vm, irq)); +} + +int +xh_vm_ioapic_pulse_irq(int irq) +{ + return (vioapic_pulse_irq(vm, irq)); +} + +int +xh_vm_ioapic_pincount(int *pincount) +{ + *pincount = vioapic_pincount(vm); + return (0); +} + +int +xh_vm_isa_assert_irq(int atpic_irq, int ioapic_irq) +{ + int error; + + error = vatpic_assert_irq(vm, atpic_irq); + + if ((error == 0) && (ioapic_irq != -1)) { + error = vioapic_assert_irq(vm, ioapic_irq); + } + + return (error); +} + +int +xh_vm_isa_deassert_irq(int atpic_irq, int ioapic_irq) +{ + int error; + + error = vatpic_deassert_irq(vm, atpic_irq); + if ((error == 0) && (ioapic_irq != -1)) { + error = vioapic_deassert_irq(vm, ioapic_irq); + } + + return (error); +} + +int +xh_vm_isa_pulse_irq(int atpic_irq, int ioapic_irq) +{ + int error; + + error = vatpic_pulse_irq(vm, atpic_irq); + if ((error == 0) && (ioapic_irq != -1)) { + error = vioapic_pulse_irq(vm, ioapic_irq); + } + + return (error); +} + +int +xh_vm_isa_set_irq_trigger(int atpic_irq, enum vm_intr_trigger trigger) +{ + return (vatpic_set_irq_trigger(vm, atpic_irq, trigger)); +} + +int +xh_vm_inject_nmi(int vcpu) +{ + return (vm_inject_nmi(vm, vcpu)); +} + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +static struct { + const char *name; + int type; +} capstrmap[] = { + { "hlt_exit", VM_CAP_HALT_EXIT }, + { "mtrap_exit", VM_CAP_MTRAP_EXIT }, + { "pause_exit", VM_CAP_PAUSE_EXIT }, + { NULL, 0 } +}; +#pragma clang diagnostic pop + +int +xh_vm_capability_name2type(const char *capname) +{ + int i; + + for (i = 0; (capstrmap[i].name != NULL) && (capname != NULL); i++) { + if (strcmp(capstrmap[i].name, capname) == 0) { + return (capstrmap[i].type); + } + } + + return (-1); +} + +const char * +xh_vm_capability_type2name(int type) +{ + int i; + + for (i = 0; (capstrmap[i].name != NULL); i++) { + if (capstrmap[i].type == type) { + return (capstrmap[i].name); + } + } + + return (NULL); +} + +int +xh_vm_get_capability(int vcpu, enum vm_cap_type cap, int *retval) +{ + int error; + + vcpu_freeze(vcpu, true); + error = vm_get_capability(vm, vcpu, cap, retval); + vcpu_freeze(vcpu, false); + + return (error); +} + +int +xh_vm_set_capability(int vcpu, enum vm_cap_type cap, int val) +{ + int error; + + vcpu_freeze(vcpu, true); + error = vm_set_capability(vm, vcpu, cap, val); + vcpu_freeze(vcpu, false); + + return (error); +} + +int +xh_vm_get_intinfo(int vcpu, uint64_t *i1, uint64_t *i2) +{ + int error; + + vcpu_freeze(vcpu, true); + error = vm_get_intinfo(vm, vcpu, i1, i2); + vcpu_freeze(vcpu, false); + + return (error); +} + +int +xh_vm_set_intinfo(int vcpu, uint64_t exit_intinfo) +{ + int error; + + vcpu_freeze(vcpu, true); + error = vm_exit_intinfo(vm, vcpu, exit_intinfo); + vcpu_freeze(vcpu, false); + + return (error); +} + +uint64_t * +xh_vm_get_stats(int vcpu, struct timeval *ret_tv, int *ret_entries) +{ + static uint64_t statbuf[64]; + struct timeval tv; + int re; + int error; + + getmicrotime(&tv); + error = vmm_stat_copy(vm, vcpu, &re, ((uint64_t *) &statbuf)); + + if (error == 0) { + if (ret_entries) { + *ret_entries = re; + } + if (ret_tv) { + *ret_tv = tv; + } + return (((uint64_t *) &statbuf)); + } else { + return (NULL); + } +} + +const char * +xh_vm_get_stat_desc(int index) +{ + static char desc[128]; + + if (vmm_stat_desc_copy(index, ((char *) &desc), sizeof(desc)) == 0) { + return (desc); + } else { + return (NULL); + } +} + +int +xh_vm_get_x2apic_state(int vcpu, enum x2apic_state *s) +{ + return (vm_get_x2apic_state(vm, vcpu, s)); +} + +int +xh_vm_set_x2apic_state(int vcpu, enum x2apic_state s) +{ + int error; + + vcpu_freeze(vcpu, true); + error = vm_set_x2apic_state(vm, vcpu, s); + vcpu_freeze(vcpu, false); + + return (error); +} + +int +xh_vm_get_hpet_capabilities(uint32_t *capabilities) +{ + return (vhpet_getcap(capabilities)); +} + +int +xh_vm_copy_setup(int vcpu, struct vm_guest_paging *pg, uint64_t gla, size_t len, + int prot, struct iovec *iov, int iovcnt, int *fault) +{ + void *va; + uint64_t gpa; + size_t n, off; + int i, error; + + for (i = 0; i < iovcnt; i++) { + iov[i].iov_base = 0; + iov[i].iov_len = 0; + } + + while (len) { + assert(iovcnt > 0); + + error = xh_vm_gla2gpa(vcpu, pg, gla, prot, &gpa, fault); + if ((error) || *fault) { + return (error); + } + + off = gpa & XHYVE_PAGE_MASK; + n = min(len, XHYVE_PAGE_SIZE - off); + + va = xh_vm_map_gpa(gpa, n); + if (va == NULL) { + return (EFAULT); + } + + iov->iov_base = va; + iov->iov_len = n; + iov++; + iovcnt--; + + gla += n; + len -= n; + } + + return (0); +} + +void +xh_vm_copyin(struct iovec *iov, void *dst, size_t len) +{ + const char *src; + char *d; + size_t n; + + d = dst; + while (len) { + assert(iov->iov_len); + n = min(len, iov->iov_len); + src = iov->iov_base; + bcopy(src, d, n); + iov++; + d += n; + len -= n; + } +} + +void +xh_vm_copyout(const void *src, struct iovec *iov, size_t len) +{ + const char *s; + char *dst; + size_t n; + + s = src; + while (len) { + assert(iov->iov_len); + n = min(len, iov->iov_len); + dst = iov->iov_base; + bcopy(s, dst, n); + iov++; + s += n; + len -= n; + } +} + +int +xh_vm_rtc_write(int offset, uint8_t value) +{ + return (vrtc_nvram_write(vm, offset, value)); +} + +int +xh_vm_rtc_read(int offset, uint8_t *retval) +{ + return (vrtc_nvram_read(vm, offset, retval)); +} + +int +xh_vm_rtc_settime(time_t secs) +{ + return (vrtc_set_time(vm, secs)); +} + +int +xh_vm_rtc_gettime(time_t *secs) +{ + *secs = vrtc_get_time(vm); + return (0); +} + +int +xh_vcpu_reset(int vcpu) +{ + int error; + +#define SET_REG(r, v) (error = xh_vm_set_register(vcpu, (r), (v))) +#define SET_DESC(d, b, l, a) (error = xh_vm_set_desc(vcpu, (d), (b), (l), (a))) + + if (SET_REG(VM_REG_GUEST_RFLAGS, 0x2) || + SET_REG(VM_REG_GUEST_RIP, 0xfff0) || + SET_REG(VM_REG_GUEST_CR0, CR0_NE) || + SET_REG(VM_REG_GUEST_CR3, 0) || + SET_REG(VM_REG_GUEST_CR4, 0) || + SET_REG(VM_REG_GUEST_CS, 0xf000) || + SET_REG(VM_REG_GUEST_SS, 0) || + SET_REG(VM_REG_GUEST_DS, 0) || + SET_REG(VM_REG_GUEST_ES, 0) || + SET_REG(VM_REG_GUEST_FS, 0) || + SET_REG(VM_REG_GUEST_GS, 0) || + SET_REG(VM_REG_GUEST_RAX, 0) || + SET_REG(VM_REG_GUEST_RBX, 0) || + SET_REG(VM_REG_GUEST_RCX, 0) || + SET_REG(VM_REG_GUEST_RDX, 0xf00) || + SET_REG(VM_REG_GUEST_RSI, 0) || + SET_REG(VM_REG_GUEST_RDI, 0) || + SET_REG(VM_REG_GUEST_RBP, 0) || + SET_REG(VM_REG_GUEST_RSP, 0) || + SET_REG(VM_REG_GUEST_TR, 0) || + SET_REG(VM_REG_GUEST_LDTR, 0) || + SET_DESC(VM_REG_GUEST_CS, 0xffff0000, 0xffff, 0x0093) || + SET_DESC(VM_REG_GUEST_SS, 0, 0xffff, 0x0093) || + SET_DESC(VM_REG_GUEST_DS, 0, 0xffff, 0x0093) || + SET_DESC(VM_REG_GUEST_ES, 0, 0xffff, 0x0093) || + SET_DESC(VM_REG_GUEST_FS, 0, 0xffff, 0x0093) || + SET_DESC(VM_REG_GUEST_GS, 0, 0xffff, 0x0093) || + SET_DESC(VM_REG_GUEST_GDTR, 0, 0xffff, 0) || + SET_DESC(VM_REG_GUEST_IDTR, 0, 0xffff, 0) || + SET_DESC(VM_REG_GUEST_TR, 0, 0, 0x0000008b) || + SET_DESC(VM_REG_GUEST_LDTR, 0, 0xffff, 0x00000082)) + { + return (error); + } + + return (0); +} + +int +xh_vm_active_cpus(cpuset_t *cpus) +{ + *cpus = vm_active_cpus(vm); + return (0); +} + +int +xh_vm_suspended_cpus(cpuset_t *cpus) +{ + *cpus = vm_suspended_cpus(vm); + return (0); +} + +int +xh_vm_activate_cpu(int vcpu) +{ + int error; + + vcpu_freeze(vcpu, true); + error = vm_activate_cpu(vm, vcpu); + vcpu_freeze(vcpu, false); + + return (error); +} + +int +xh_vm_restart_instruction(int vcpu) +{ + int error; + + vcpu_freeze(vcpu, true); + error = vm_restart_instruction(vm, vcpu); + vcpu_freeze(vcpu, false); + + return (error); +} + +int +xh_vm_emulate_instruction(int vcpu, uint64_t gpa, struct vie *vie, + struct vm_guest_paging *paging, mem_region_read_t memread, + mem_region_write_t memwrite, void *memarg) +{ + int error; + + vcpu_freeze(vcpu, true); + error = vmm_emulate_instruction(vm, vcpu, gpa, vie, paging, memread, + memwrite, memarg); + vcpu_freeze(vcpu, false); + + return (error); +} diff --git a/src/vmm/vmm_callout.c b/src/vmm/vmm_callout.c new file mode 100644 index 0000000..73011fe --- /dev/null +++ b/src/vmm/vmm_callout.c @@ -0,0 +1,379 @@ +/*- + * Copyright (c) 2015 xhyve developers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/* makeshift callout implementation based on OSv and FreeBSD */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define callout_cmp(a, b) ((a)->timeout < (b)->timeout) + +static mach_timebase_info_data_t timebase_info; +static pthread_t callout_thread; +static pthread_mutex_t callout_mtx; +static pthread_cond_t callout_cnd; +static struct callout *callout_queue; +static bool work; +static bool initialized = false; + +static inline uint64_t nanos_to_abs(uint64_t nanos) { + return (nanos * timebase_info.denom) / timebase_info.numer; +} + +static inline uint64_t abs_to_nanos(uint64_t abs) { + return (abs * timebase_info.numer) / timebase_info.denom; +} + +static inline uint64_t sbt2mat(sbintime_t sbt) { + uint64_t s, ns; + + s = (((uint64_t) sbt) >> 32); + ns = (((uint64_t) 1000000000) * (uint32_t) sbt) >> 32; + + return (nanos_to_abs((s * 1000000000) + ns)); +} + +static inline void mat_to_ts(uint64_t mat, struct timespec *ts) { + uint64_t ns; + + ns = abs_to_nanos(mat); + + ts->tv_sec = (ns / 1000000000); + ts->tv_nsec = (ns % 1000000000); +} + +void binuptime(struct bintime *bt) { + uint64_t ns; + + ns = abs_to_nanos(mach_absolute_time()); + + bt->sec = (ns / 1000000000); + bt->frac = (((ns % 1000000000) * (((uint64_t) 1 << 63) / 500000000))); +} + +void getmicrotime(struct timeval *tv) { + uint64_t ns, sns; + + ns = abs_to_nanos(mach_absolute_time()); + + sns = (ns / 1000000000); + tv->tv_sec = (long) sns; + tv->tv_usec = (int) ((ns - sns) / 1000); +} + +static void callout_insert(struct callout *c) { + struct callout *node = callout_queue; + + if (!node) { + callout_queue = c; + c->prev = NULL; + c->next = NULL; + c->queued = 1; + return; + } + + if (callout_cmp(c, node)) { + node->prev = c; + c->prev = NULL; + c->next = node; + callout_queue = c; + c->queued = 1; + return; + } + + while (node->next) { + if (callout_cmp(c, node->next)) { + c->prev = node; + c->next = node->next; + node->next->prev = c; + node->next = c; + c->queued = 1; + return; + } + node = node->next; + } + + c->prev = node; + c->next = NULL; + node->next = c; + c->queued = 1; +} + +static void callout_remove(struct callout *c) { + if (!c->queued) { + return; + } + + if (c->prev) { + c->prev->next = c->next; + } else { + callout_queue = c->next; + } + + if (c->next) { + c->next->prev = c->prev; + } + + c->prev = NULL; + c->next = NULL; + c->queued = 0; +} + +static void *callout_thread_func(UNUSED void *arg) { + struct callout *c; + struct timespec ts; + uint64_t delta, mat; + int ret; + + pthread_mutex_lock(&callout_mtx); + + while (true) { + /* wait for work */ + while (!callout_queue) { + pthread_cond_wait(&callout_cnd, &callout_mtx); + }; + + /* get the callout with the nearest timout */ + c = callout_queue; + + if (!(c->flags & (CALLOUT_ACTIVE | CALLOUT_PENDING))) { + abort(); + } + + /* wait for timeout */ + ret = 0; + while ((ret != ETIMEDOUT) && !work) { + mat = mach_absolute_time(); + if (mat >= c->timeout) { + /* XXX: it might not be worth sleeping for very short timeouts */ + ret = ETIMEDOUT; + break; + } + + delta = c->timeout - mat; + mat_to_ts(delta, &ts); + ret = pthread_cond_timedwait_relative_np(&callout_cnd, &callout_mtx, &ts); + }; + + work = false; + + if (!(ret == ETIMEDOUT) || !c->queued) { + continue; + } + + /* dispatch */ + c->flags &= ~CALLOUT_PENDING; + + pthread_mutex_unlock(&callout_mtx); + c->callout(c->argument); + pthread_mutex_lock(&callout_mtx); + + /* note: after the handler has been invoked the callout structure can look + * much differently, the handler may have rescheduled the callout or + * even freed it. + * + * if the callout is still enqueued it means that it hasn't been + * freed by the user + * + * reset || drain || !stop + */ + + if (c->queued) { + /* if the callout hasn't been rescheduled, remove it */ + if (((c->flags & CALLOUT_PENDING) == 0) || (c->flags & CALLOUT_WAITING)) { + c->flags |= CALLOUT_COMPLETED; + callout_remove(c); + } + } + } + + return NULL; +} + +void callout_init(struct callout *c, int mpsafe) { + if (!mpsafe) { + abort(); + } + + memset(c, 0, sizeof(struct callout)); + + if (pthread_cond_init(&c->wait, NULL)) { + abort(); + } +} + +static int callout_stop_safe_locked(struct callout *c, int drain) { + int result = 0; + + if ((drain) && (pthread_self() != callout_thread) && (callout_pending(c) || + (callout_active(c) && !callout_completed(c)))) + { + if (c->flags & CALLOUT_WAITING) { + abort(); + } + + /* wait for callout */ + c->flags |= CALLOUT_WAITING; + work = true; + + pthread_cond_signal(&callout_cnd); + + while (!(c->flags & CALLOUT_COMPLETED)) { + pthread_cond_wait(&c->wait, &callout_mtx); + } + + c->flags &= ~CALLOUT_WAITING; + result = 1; + } + + callout_remove(c); + + /* clear flags */ + c->flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING | CALLOUT_COMPLETED | + CALLOUT_WAITING); + + return (result); +} + +int callout_stop_safe(struct callout *c, int drain) { + pthread_mutex_lock(&callout_mtx); + callout_stop_safe_locked(c, drain); + pthread_mutex_unlock(&callout_mtx); + return 0; +} + +int callout_reset_sbt(struct callout *c, sbintime_t sbt, + UNUSED sbintime_t precision, void (*ftn)(void *), void *arg, int flags) +{ + int result; + bool is_next_timeout; + + is_next_timeout = false; + + pthread_mutex_lock(&callout_mtx); + + if (!((flags == 0) || (flags == C_ABSOLUTE)) || (c->flags !=0)) { + /* FIXME */ + //printf("XHYVE: callout_reset_sbt 0x%08x 0x%08x\r\n", flags, c->flags); + //abort(); + } + + c->timeout = sbt2mat(sbt); + + if (flags != C_ABSOLUTE) { + c->timeout += mach_absolute_time(); + } + + result = callout_stop_safe_locked(c, 0); + + c->callout = ftn; + c->argument = arg; + c->flags |= (CALLOUT_PENDING | CALLOUT_ACTIVE); + + callout_insert(c); + + if (c == callout_queue) { + work = true; + is_next_timeout = true; + } + + pthread_mutex_unlock(&callout_mtx); + + if (is_next_timeout) { + pthread_cond_signal(&callout_cnd); + is_next_timeout = false; + } + + return (result); +} + +void callout_system_init(void) { + if (initialized) { + return; + } + + mach_timebase_info(&timebase_info); + + if (pthread_mutex_init(&callout_mtx, NULL)) { + abort(); + } + + if (pthread_cond_init(&callout_cnd, NULL)) { + abort(); + } + + callout_queue = NULL; + work = false; + + if (pthread_create(&callout_thread, /*&attr*/ NULL, &callout_thread_func, + NULL)) + { + abort(); + } + + initialized = true; +} + +//static void callout_queue_print(void) { +// struct callout *node; +// +// pthread_mutex_lock(&callout_mtx); +// for (node = callout_queue; node; node = node->next) { +// printf("t:%llu -> ", abs_to_nanos(node->timeout)); +// if (!node->next) { +// break; +// } +// } +// pthread_mutex_unlock(&callout_mtx); +// printf("NULL\n"); +//} + +//void fire (void *arg) { +// printf("fire!\n"); +//} +// +//int main(void) { +// struct callout a; +// sbintime_t sbt; +// printf("xhyve_timer\n"); +// callout_system_init(); +// callout_init(&a, 1); +// sbt = ((sbintime_t) (((uint64_t) 3) << 32)); +// callout_reset_sbt(&a, sbt, 0, &fire, NULL, 0); +// while (1); +// return 0; +//} diff --git a/src/vmm/vmm_host.c b/src/vmm/vmm_host.c new file mode 100644 index 0000000..e11e3cf --- /dev/null +++ b/src/vmm/vmm_host.c @@ -0,0 +1,60 @@ +/*- + * Copyright (c) 2012 NetApp, Inc. + * Copyright (c) 2015 xhyve developers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#include +#include +#include +#include + +static struct xsave_limits vmm_xsave_limits; + +void +vmm_host_state_init(void) +{ + uint32_t avx1_0, regs[4]; + size_t ln; + + vmm_xsave_limits.xsave_enabled = 0; + + ln = sizeof(uint32_t); + if (!sysctlbyname("hw.optional.avx1_0", &avx1_0, &ln, NULL, 0) && avx1_0) { + cpuid_count(0xd, 0x0, regs); + vmm_xsave_limits.xsave_enabled = 1; + vmm_xsave_limits.xcr0_allowed = XFEATURE_AVX; + vmm_xsave_limits.xsave_max_size = regs[1]; + } +} + +const struct xsave_limits * +vmm_get_xsave_limits(void) +{ + return (&vmm_xsave_limits); +} diff --git a/vmm/vmm_instruction_emul.c b/src/vmm/vmm_instruction_emul.c similarity index 91% rename from vmm/vmm_instruction_emul.c rename to src/vmm/vmm_instruction_emul.c index 9c6158a..4fb07b2 100644 --- a/vmm/vmm_instruction_emul.c +++ b/src/vmm/vmm_instruction_emul.c @@ -1,6 +1,7 @@ /*- * Copyright (c) 2012 Sandvine, Inc. * Copyright (c) 2012 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,35 +28,27 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); +#include +#include +#include +#include +#include +#include +#include +#include +#include -#ifdef _KERNEL -#include -#include -#include -#include - -#include -#include - -#include -#include -#else /* !_KERNEL */ -#include -#include -#include - -#include - -#include -#include -#define KASSERT(exp,msg) assert((exp)) -#endif /* _KERNEL */ - -#include -#include -#include +#define PG_V 0x001 /* P Valid */ +#define PG_RW 0x002 /* R/W Read/Write */ +#define PG_U 0x004 /* U/S User/Supervisor */ +#define PG_A 0x020 /* A Accessed */ +#define PG_M 0x040 /* D Dirty */ +#define PG_PS 0x080 /* PS Page size (0=4k,1=4M) */ +#define PGEX_P 0x01 /* Protection violation vs. not present */ +#define PGEX_W 0x02 /* during a Write cycle */ +#define PGEX_U 0x04 /* access from User mode (UPL) */ +#define PGEX_RSV 0x08 /* reserved PTE field is non-zero */ +#define PGEX_I 0x10 /* during an instruction fetch */ /* struct vie_op.op_type */ enum { @@ -297,9 +290,9 @@ vie_read_bytereg(void *vm, int vcpuid, struct vie *vie, uint8_t *rval) * base register right by 8 bits (%ah = %rax >> 8). */ if (lhbr) - *rval = val >> 8; + *rval = (uint8_t) (val >> 8); else - *rval = val; + *rval = (uint8_t) val; return (error); } @@ -386,11 +379,11 @@ getcc(int opsize, uint64_t x, uint64_t y) ("getcc: invalid operand size %d", opsize)); if (opsize == 1) - return (getcc8(x, y)); + return (getcc8(((uint8_t) x), ((uint8_t) y))); else if (opsize == 2) - return (getcc16(x, y)); + return (getcc16(((uint16_t) x), ((uint16_t) y))); else if (opsize == 4) - return (getcc32(x, y)); + return (getcc32(((uint32_t) x), ((uint32_t) y))); else return (getcc64(x, y)); } @@ -442,7 +435,7 @@ emulate_mov(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, size = 1; /* override for byte operation */ error = memread(vm, vcpuid, gpa, &val, size, arg); if (error == 0) - error = vie_write_bytereg(vm, vcpuid, vie, val); + error = vie_write_bytereg(vm, vcpuid, vie, ((uint8_t) val)); break; case 0x8B: /* @@ -490,7 +483,8 @@ emulate_mov(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, * REX + C6/0 mov r/m8, imm8 */ size = 1; /* override for byte operation */ - error = memwrite(vm, vcpuid, gpa, vie->immediate, size, arg); + error = memwrite(vm, vcpuid, gpa, ((uint64_t) vie->immediate), size, + arg); break; case 0xC7: /* @@ -499,7 +493,7 @@ emulate_mov(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, * C7/0 mov r/m32, imm32 * REX.W + C7/0 mov r/m64, imm32 (sign-extended to 64-bits) */ - val = vie->immediate & size2mask[size]; + val = ((uint64_t) vie->immediate) & size2mask[size]; error = memwrite(vm, vcpuid, gpa, val, size, arg); break; default: @@ -511,7 +505,7 @@ emulate_mov(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, static int emulate_movx(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, - mem_region_read_t memread, mem_region_write_t memwrite, + mem_region_read_t memread, UNUSED mem_region_write_t memwrite, void *arg) { int error, size; @@ -584,7 +578,7 @@ emulate_movx(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, reg = gpr_map[vie->reg]; /* sign extend byte */ - val = (int8_t)val; + val = (uint64_t) ((int64_t) ((int8_t) val)); /* write the result */ error = vie_update_register(vm, vcpuid, reg, val, size); @@ -599,9 +593,9 @@ emulate_movx(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, * Helper function to calculate and validate a linear address. */ static int -get_gla(void *vm, int vcpuid, struct vie *vie, struct vm_guest_paging *paging, - int opsize, int addrsize, int prot, enum vm_reg_name seg, - enum vm_reg_name gpr, uint64_t *gla, int *fault) +get_gla(void *vm, int vcpuid, UNUSED struct vie *vie, + struct vm_guest_paging *paging, int opsize, int addrsize, int prot, + enum vm_reg_name seg, enum vm_reg_name gpr, uint64_t *gla, int *fault) { struct seg_desc desc; uint64_t cr0, val, rflags; @@ -656,17 +650,14 @@ emulate_movs(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, struct vm_guest_paging *paging, mem_region_read_t memread, mem_region_write_t memwrite, void *arg) { -#ifdef _KERNEL struct vm_copyinfo copyinfo[2]; -#else - struct iovec copyinfo[2]; -#endif uint64_t dstaddr, srcaddr, dstgpa, srcgpa, val; uint64_t rcx, rdi, rsi, rflags; int error, fault, opsize, seg, repeat; opsize = (vie->op.op_byte == 0xA4) ? 1 : vie->opsize; val = 0; + rcx = 0; error = 0; /* @@ -710,12 +701,13 @@ emulate_movs(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, seg = vie->segment_override ? vie->segment_register : VM_REG_GUEST_DS; error = get_gla(vm, vcpuid, vie, paging, opsize, vie->addrsize, - PROT_READ, seg, VM_REG_GUEST_RSI, &srcaddr, &fault); + XHYVE_PROT_READ, ((enum vm_reg_name) seg), VM_REG_GUEST_RSI, &srcaddr, + &fault); if (error || fault) goto done; - error = vm_copy_setup(vm, vcpuid, paging, srcaddr, opsize, PROT_READ, - copyinfo, nitems(copyinfo), &fault); + error = vm_copy_setup(vm, vcpuid, paging, srcaddr, ((size_t) opsize), + XHYVE_PROT_READ, copyinfo, nitems(copyinfo), &fault); if (error == 0) { if (fault) goto done; /* Resume guest to handle fault */ @@ -723,7 +715,7 @@ emulate_movs(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, /* * case (2): read from system memory and write to mmio. */ - vm_copyin(vm, vcpuid, copyinfo, &val, opsize); + vm_copyin(vm, vcpuid, copyinfo, &val, ((size_t) opsize)); vm_copy_teardown(vm, vcpuid, copyinfo, nitems(copyinfo)); error = memwrite(vm, vcpuid, gpa, val, opsize, arg); if (error) @@ -735,13 +727,13 @@ emulate_movs(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, */ error = get_gla(vm, vcpuid, vie, paging, opsize, vie->addrsize, - PROT_WRITE, VM_REG_GUEST_ES, VM_REG_GUEST_RDI, &dstaddr, + XHYVE_PROT_WRITE, VM_REG_GUEST_ES, VM_REG_GUEST_RDI, &dstaddr, &fault); if (error || fault) goto done; - error = vm_copy_setup(vm, vcpuid, paging, dstaddr, opsize, - PROT_WRITE, copyinfo, nitems(copyinfo), &fault); + error = vm_copy_setup(vm, vcpuid, paging, dstaddr, ((size_t) opsize), + XHYVE_PROT_WRITE, copyinfo, nitems(copyinfo), &fault); if (error == 0) { if (fault) goto done; /* Resume guest to handle fault */ @@ -759,7 +751,7 @@ emulate_movs(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, if (error) goto done; - vm_copyout(vm, vcpuid, &val, copyinfo, opsize); + vm_copyout(vm, vcpuid, &val, copyinfo, ((size_t) opsize)); vm_copy_teardown(vm, vcpuid, copyinfo, nitems(copyinfo)); } else { /* @@ -771,12 +763,12 @@ emulate_movs(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, * to address translation faults. */ error = vm_gla2gpa(vm, vcpuid, paging, srcaddr, - PROT_READ, &srcgpa, &fault); + XHYVE_PROT_READ, &srcgpa, &fault); if (error || fault) goto done; error = vm_gla2gpa(vm, vcpuid, paging, dstaddr, - PROT_WRITE, &dstgpa, &fault); + XHYVE_PROT_WRITE, &dstgpa, &fault); if (error || fault) goto done; @@ -800,11 +792,11 @@ emulate_movs(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, KASSERT(error == 0, ("%s: error %d getting rflags", __func__, error)); if (rflags & PSL_D) { - rsi -= opsize; - rdi -= opsize; + rsi -= ((uint64_t) opsize); + rdi -= ((uint64_t) opsize); } else { - rsi += opsize; - rdi += opsize; + rsi += ((uint64_t) opsize); + rdi += ((uint64_t) opsize); } error = vie_update_register(vm, vcpuid, VM_REG_GUEST_RSI, rsi, @@ -835,7 +827,7 @@ done: static int emulate_stos(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, - struct vm_guest_paging *paging, mem_region_read_t memread, + UNUSED struct vm_guest_paging *paging, UNUSED mem_region_read_t memread, mem_region_write_t memwrite, void *arg) { int error, opsize, repeat; @@ -844,6 +836,7 @@ emulate_stos(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, opsize = (vie->op.op_byte == 0xAA) ? 1 : vie->opsize; repeat = vie->repz_present | vie->repnz_present; + rcx = 0; if (repeat) { error = vie_read_register(vm, vcpuid, VM_REG_GUEST_RCX, &rcx); @@ -871,9 +864,9 @@ emulate_stos(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, KASSERT(error == 0, ("%s: error %d getting rflags", __func__, error)); if (rflags & PSL_D) - rdi -= opsize; + rdi -= ((uint64_t) opsize); else - rdi += opsize; + rdi += ((uint64_t) opsize); error = vie_update_register(vm, vcpuid, VM_REG_GUEST_RDI, rdi, vie->addrsize); @@ -904,6 +897,7 @@ emulate_and(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, uint64_t result, rflags, rflags2, val1, val2; size = vie->opsize; + result = 0; error = EINVAL; switch (vie->op.op_byte) { @@ -956,7 +950,7 @@ emulate_and(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, * perform the operation with the pre-fetched immediate * operand and write the result */ - result = val1 & vie->immediate; + result = val1 & ((uint64_t) vie->immediate); error = memwrite(vm, vcpuid, gpa, result, size, arg); break; default: @@ -976,7 +970,7 @@ emulate_and(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, * The updated status flags are obtained by subtracting 0 from 'result'. */ rflags2 = getcc(size, result, 0); - rflags &= ~RFLAGS_STATUS_BITS; + rflags &= ~((uint64_t) RFLAGS_STATUS_BITS); rflags |= rflags2 & (PSL_PF | PSL_Z | PSL_N); error = vie_update_register(vm, vcpuid, VM_REG_GUEST_RFLAGS, rflags, 8); @@ -991,6 +985,7 @@ emulate_or(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, uint64_t val1, result, rflags, rflags2; size = vie->opsize; + result = 0; error = EINVAL; switch (vie->op.op_byte) { @@ -1018,7 +1013,7 @@ emulate_or(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, * perform the operation with the pre-fetched immediate * operand and write the result */ - result = val1 | vie->immediate; + result = val1 | ((uint64_t) vie->immediate); error = memwrite(vm, vcpuid, gpa, result, size, arg); break; default: @@ -1038,7 +1033,7 @@ emulate_or(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, * The updated status flags are obtained by subtracting 0 from 'result'. */ rflags2 = getcc(size, result, 0); - rflags &= ~RFLAGS_STATUS_BITS; + rflags &= ~((uint64_t) RFLAGS_STATUS_BITS); rflags |= rflags2 & (PSL_PF | PSL_Z | PSL_N); error = vie_update_register(vm, vcpuid, VM_REG_GUEST_RFLAGS, rflags, 8); @@ -1047,7 +1042,8 @@ emulate_or(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, static int emulate_cmp(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, - mem_region_read_t memread, mem_region_write_t memwrite, void *arg) + mem_region_read_t memread, UNUSED mem_region_write_t memwrite, + void *arg) { int error, size; uint64_t op1, op2, rflags, rflags2; @@ -1110,7 +1106,7 @@ emulate_cmp(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, if (error) return (error); - rflags2 = getcc(size, op1, vie->immediate); + rflags2 = getcc(size, op1, ((uint64_t) vie->immediate)); break; default: return (EINVAL); @@ -1118,7 +1114,7 @@ emulate_cmp(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, error = vie_read_register(vm, vcpuid, VM_REG_GUEST_RFLAGS, &rflags); if (error) return (error); - rflags &= ~RFLAGS_STATUS_BITS; + rflags &= ~((uint64_t) RFLAGS_STATUS_BITS); rflags |= rflags2 & RFLAGS_STATUS_BITS; error = vie_update_register(vm, vcpuid, VM_REG_GUEST_RFLAGS, rflags, 8); @@ -1127,13 +1123,16 @@ emulate_cmp(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, static int emulate_sub(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, - mem_region_read_t memread, mem_region_write_t memwrite, void *arg) + mem_region_read_t memread, UNUSED mem_region_write_t memwrite, + void *arg) { int error, size; uint64_t nval, rflags, rflags2, val1, val2; enum vm_reg_name reg; size = vie->opsize; + val1 = 0; + val2 = 0; error = EINVAL; switch (vie->op.op_byte) { @@ -1172,7 +1171,7 @@ emulate_sub(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, if (error) return (error); - rflags &= ~RFLAGS_STATUS_BITS; + rflags &= ~((uint64_t) RFLAGS_STATUS_BITS); rflags |= rflags2 & RFLAGS_STATUS_BITS; error = vie_update_register(vm, vcpuid, VM_REG_GUEST_RFLAGS, rflags, 8); @@ -1186,11 +1185,7 @@ emulate_stack_op(void *vm, int vcpuid, uint64_t mmio_gpa, struct vie *vie, struct vm_guest_paging *paging, mem_region_read_t memread, mem_region_write_t memwrite, void *arg) { -#ifdef _KERNEL struct vm_copyinfo copyinfo[2]; -#else - struct iovec copyinfo[2]; -#endif struct seg_desc ss_desc; uint64_t cr0, rflags, rsp, stack_gla, val; int error, fault, size, stackaddrsize, pushop; @@ -1238,11 +1233,11 @@ emulate_stack_op(void *vm, int vcpuid, uint64_t mmio_gpa, struct vie *vie, error = vie_read_register(vm, vcpuid, VM_REG_GUEST_RSP, &rsp); KASSERT(error == 0, ("%s: error %d getting rsp", __func__, error)); if (pushop) { - rsp -= size; + rsp -= ((uint64_t) size); } if (vie_calculate_gla(paging->cpu_mode, VM_REG_GUEST_SS, &ss_desc, - rsp, size, stackaddrsize, pushop ? PROT_WRITE : PROT_READ, + rsp, size, stackaddrsize, pushop ? XHYVE_PROT_WRITE : XHYVE_PROT_READ, &stack_gla)) { vm_inject_ss(vm, vcpuid, 0); return (0); @@ -1258,8 +1253,8 @@ emulate_stack_op(void *vm, int vcpuid, uint64_t mmio_gpa, struct vie *vie, return (0); } - error = vm_copy_setup(vm, vcpuid, paging, stack_gla, size, - pushop ? PROT_WRITE : PROT_READ, copyinfo, nitems(copyinfo), + error = vm_copy_setup(vm, vcpuid, paging, stack_gla, ((size_t) size), + pushop ? XHYVE_PROT_WRITE : XHYVE_PROT_READ, copyinfo, nitems(copyinfo), &fault); if (error || fault) return (error); @@ -1267,11 +1262,11 @@ emulate_stack_op(void *vm, int vcpuid, uint64_t mmio_gpa, struct vie *vie, if (pushop) { error = memread(vm, vcpuid, mmio_gpa, &val, size, arg); if (error == 0) - vm_copyout(vm, vcpuid, &val, copyinfo, size); + vm_copyout(vm, vcpuid, &val, copyinfo, ((size_t) size)); } else { - vm_copyin(vm, vcpuid, copyinfo, &val, size); + vm_copyin(vm, vcpuid, copyinfo, &val, ((size_t) size)); error = memwrite(vm, vcpuid, mmio_gpa, val, size, arg); - rsp += size; + rsp += ((uint64_t) size); } vm_copy_teardown(vm, vcpuid, copyinfo, nitems(copyinfo)); @@ -1327,7 +1322,7 @@ emulate_pop(void *vm, int vcpuid, uint64_t mmio_gpa, struct vie *vie, static int emulate_group1(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, - struct vm_guest_paging *paging, mem_region_read_t memread, + UNUSED struct vm_guest_paging *paging, mem_region_read_t memread, mem_region_write_t memwrite, void *memarg) { int error; @@ -1355,7 +1350,7 @@ emulate_group1(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, static int emulate_bittest(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, - mem_region_read_t memread, mem_region_write_t memwrite, void *memarg) + mem_region_read_t memread, UNUSED mem_region_write_t memwrite, void *memarg) { uint64_t val, rflags; int error, bitmask, bitoff; @@ -1387,7 +1382,7 @@ emulate_bittest(void *vm, int vcpuid, uint64_t gpa, struct vie *vie, if (val & (1UL << bitoff)) rflags |= PSL_C; else - rflags &= ~PSL_C; + rflags &= ~((uint64_t) PSL_C); error = vie_update_register(vm, vcpuid, VM_REG_GUEST_RFLAGS, rflags, 8); KASSERT(error == 0, ("%s: error %d updating rflags", __func__, error)); @@ -1473,7 +1468,7 @@ vie_alignment_check(int cpl, int size, uint64_t cr0, uint64_t rf, uint64_t gla) if (cpl != 3 || (cr0 & CR0_AM) == 0 || (rf & PSL_AC) == 0) return (0); - return ((gla & (size - 1)) ? 1 : 0); + return ((gla & ((uint64_t) (size - 1))) ? 1 : 0); } int @@ -1515,7 +1510,7 @@ vie_calculate_gla(enum vm_cpu_mode cpu_mode, enum vm_reg_name seg, ("%s: invalid segment %d", __func__, seg)); KASSERT(length == 1 || length == 2 || length == 4 || length == 8, ("%s: invalid operand size %d", __func__, length)); - KASSERT((prot & ~(PROT_READ | PROT_WRITE)) == 0, + KASSERT((prot & ~(XHYVE_PROT_READ | XHYVE_PROT_WRITE)) == 0, ("%s: invalid prot %#x", __func__, prot)); firstoff = offset; @@ -1551,13 +1546,13 @@ vie_calculate_gla(enum vm_cpu_mode cpu_mode, enum vm_reg_name seg, KASSERT(type >= 16 && type <= 31, ("segment %d has invalid " "descriptor type %#x", seg, type)); - if (prot & PROT_READ) { + if (prot & XHYVE_PROT_READ) { /* #GP on a read access to a exec-only code segment */ if ((type & 0xA) == 0x8) return (-1); } - if (prot & PROT_WRITE) { + if (prot & XHYVE_PROT_WRITE) { /* * #GP on a write access to a code segment or a * read-only data segment. @@ -1613,7 +1608,6 @@ vie_calculate_gla(enum vm_cpu_mode cpu_mode, enum vm_reg_name seg, return (0); } -#ifdef _KERNEL void vie_init(struct vie *vie, const char *inst_bytes, int inst_length) { @@ -1627,8 +1621,8 @@ vie_init(struct vie *vie, const char *inst_bytes, int inst_length) vie->segment_register = VM_REG_LAST; if (inst_length) { - bcopy(inst_bytes, vie->inst, inst_length); - vie->num_valid = inst_length; + bcopy(inst_bytes, vie->inst, ((size_t) inst_length)); + vie->num_valid = ((uint8_t) inst_length); } } @@ -1639,37 +1633,18 @@ pf_error_code(int usermode, int prot, int rsvd, uint64_t pte) if (pte & PG_V) error_code |= PGEX_P; - if (prot & VM_PROT_WRITE) + if (prot & XHYVE_PROT_WRITE) error_code |= PGEX_W; if (usermode) error_code |= PGEX_U; if (rsvd) error_code |= PGEX_RSV; - if (prot & VM_PROT_EXECUTE) + if (prot & XHYVE_PROT_EXECUTE) error_code |= PGEX_I; return (error_code); } -static void -ptp_release(void **cookie) -{ - if (*cookie != NULL) { - vm_gpa_release(*cookie); - *cookie = NULL; - } -} - -static void * -ptp_hold(struct vm *vm, vm_paddr_t ptpphys, size_t len, void **cookie) -{ - void *ptr; - - ptp_release(cookie); - ptr = vm_gpa_hold(vm, ptpphys, len, VM_PROT_RW, cookie); - return (ptr); -} - int vm_gla2gpa(struct vm *vm, int vcpuid, struct vm_guest_paging *paging, uint64_t gla, int prot, uint64_t *gpa, int *guest_fault) @@ -1683,15 +1658,20 @@ vm_gla2gpa(struct vm *vm, int vcpuid, struct vm_guest_paging *paging, *guest_fault = 0; usermode = (paging->cpl == 3 ? 1 : 0); - writable = prot & VM_PROT_WRITE; + writable = prot & XHYVE_PROT_WRITE; cookie = NULL; retval = 0; retries = 0; + pte = 0; + pte32 = 0; + ptpshift = 0; + pgsize = 0; + ptpindex = 0; + ptpbase = NULL; + ptpbase32 = NULL; + restart: ptpphys = paging->cr3; /* root of the page tables */ - ptp_release(&cookie); - if (retries++ > 0) - maybe_yield(); if (vie_canonical_check(paging->cpu_mode, gla)) { /* @@ -1711,14 +1691,14 @@ restart: nlevels = 2; while (--nlevels >= 0) { /* Zero out the lower 12 bits. */ - ptpphys &= ~0xfff; + ptpphys &= ~((uint64_t) 0xfff); - ptpbase32 = ptp_hold(vm, ptpphys, PAGE_SIZE, &cookie); + ptpbase32 = vm_gpa2hva(vm, ptpphys, XHYVE_PAGE_SIZE); if (ptpbase32 == NULL) goto error; - ptpshift = PAGE_SHIFT + nlevels * 10; + ptpshift = XHYVE_PAGE_SHIFT + nlevels * 10; ptpindex = (gla >> ptpshift) & 0x3FF; pgsize = 1UL << ptpshift; @@ -1772,7 +1752,7 @@ restart: /* Zero out the lower 5 bits and the upper 32 bits */ ptpphys &= 0xffffffe0UL; - ptpbase = ptp_hold(vm, ptpphys, sizeof(*ptpbase) * 4, &cookie); + ptpbase = vm_gpa2hva(vm, ptpphys, (sizeof(*ptpbase) * 4)); if (ptpbase == NULL) goto error; @@ -1795,11 +1775,11 @@ restart: /* Zero out the lower 12 bits and the upper 12 bits */ ptpphys >>= 12; ptpphys <<= 24; ptpphys >>= 12; - ptpbase = ptp_hold(vm, ptpphys, PAGE_SIZE, &cookie); + ptpbase = vm_gpa2hva(vm, ptpphys, XHYVE_PAGE_SIZE); if (ptpbase == NULL) goto error; - ptpshift = PAGE_SHIFT + nlevels * 9; + ptpshift = XHYVE_PAGE_SHIFT + nlevels * 9; ptpindex = (gla >> ptpshift) & 0x1FF; pgsize = 1UL << ptpshift; @@ -1815,7 +1795,7 @@ restart: /* Set the accessed bit in the page table entry */ if ((pte & PG_A) == 0) { - if (atomic_cmpset_64(&ptpbase[ptpindex], + if (atomic_cmpset_64(((volatile u_long *) &ptpbase[ptpindex]), pte, pte | PG_A) == 0) { goto restart; } @@ -1835,15 +1815,17 @@ restart: /* Set the dirty bit in the page table entry if necessary */ if (writable && (pte & PG_M) == 0) { - if (atomic_cmpset_64(&ptpbase[ptpindex], pte, pte | PG_M) == 0) + if (atomic_cmpset_64(((volatile u_long *) &ptpbase[ptpindex]), pte, + pte | PG_M) == 0) + { goto restart; + } } /* Zero out the lower 'ptpshift' bits and the upper 12 bits */ pte >>= ptpshift; pte <<= (ptpshift + 12); pte >>= 12; *gpa = pte | (gla & (pgsize - 1)); done: - ptp_release(&cookie); KASSERT(retval == 0 || retval == EFAULT, ("%s: unexpected retval %d", __func__, retval)); return (retval); @@ -1863,17 +1845,17 @@ vmm_fetch_instruction(struct vm *vm, int vcpuid, struct vm_guest_paging *paging, int error, prot; if (inst_length > VIE_INST_SIZE) - panic("vmm_fetch_instruction: invalid length %d", inst_length); + xhyve_abort("vmm_fetch_instruction: invalid length %d\n", inst_length); - prot = PROT_READ | PROT_EXEC; - error = vm_copy_setup(vm, vcpuid, paging, rip, inst_length, prot, + prot = XHYVE_PROT_READ | XHYVE_PROT_EXECUTE; + error = vm_copy_setup(vm, vcpuid, paging, rip, ((size_t) inst_length), prot, copyinfo, nitems(copyinfo), faultptr); if (error || *faultptr) return (error); - vm_copyin(vm, vcpuid, copyinfo, vie->inst, inst_length); + vm_copyin(vm, vcpuid, copyinfo, vie->inst, ((size_t) inst_length)); vm_copy_teardown(vm, vcpuid, copyinfo, nitems(copyinfo)); - vie->num_valid = inst_length; + vie->num_valid = (uint8_t) inst_length; return (0); } @@ -2171,7 +2153,7 @@ decode_sib(struct vie *vie) /* 'scale' makes sense only in the context of an index register */ if (vie->index_register < VM_REG_LAST) - vie->scale = 1 << vie->ss; + vie->scale = (uint8_t) (1 << vie->ss); vie_advance(vie); @@ -2194,13 +2176,13 @@ decode_displacement(struct vie *vie) return (0); if (n != 1 && n != 4) - panic("decode_displacement: invalid disp_bytes %d", n); + xhyve_abort("decode_displacement: invalid disp_bytes %d\n", n); for (i = 0; i < n; i++) { if (vie_peek(vie, &x)) return (-1); - u.buf[i] = x; + u.buf[i] = (char) x; vie_advance(vie); } @@ -2251,7 +2233,7 @@ decode_immediate(struct vie *vie) if (vie_peek(vie, &x)) return (-1); - u.buf[i] = x; + u.buf[i] = (char) x; vie_advance(vie); } @@ -2291,10 +2273,10 @@ decode_moffset(struct vie *vie) if (vie_peek(vie, &x)) return (-1); - u.buf[i] = x; + u.buf[i] = (char) x; vie_advance(vie); } - vie->displacement = u.u64; + vie->displacement = (int64_t) u.u64; return (0); } @@ -2353,12 +2335,12 @@ verify_gla(struct vm *vm, int cpuid, uint64_t gla, struct vie *vie) } /* XXX assuming that the base address of the segment is 0 */ - gla2 = base + vie->scale * idx + vie->displacement; + gla2 = base + vie->scale * idx + ((uint64_t) vie->displacement); gla2 &= size2mask[vie->addrsize]; if (gla != gla2) { printf("verify_gla mismatch: " - "base(0x%0lx), scale(%d), index(0x%0lx), " - "disp(0x%0lx), gla(0x%0lx), gla2(0x%0lx)\n", + "base(0x%0llx), scale(%d), index(0x%0llx), " + "disp(0x%0llx), gla(0x%0llx), gla2(0x%0llx)\n", base, vie->scale, idx, vie->displacement, gla, gla2); return (-1); } @@ -2404,4 +2386,3 @@ vmm_decode_instruction(struct vm *vm, int cpuid, uint64_t gla, return (0); } -#endif /* _KERNEL */ diff --git a/vmm/vmm_ioport.c b/src/vmm/vmm_ioport.c similarity index 84% rename from vmm/vmm_ioport.c rename to src/vmm/vmm_ioport.c index 63044e8..b935241 100644 --- a/vmm/vmm_ioport.c +++ b/src/vmm/vmm_ioport.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2014 Tycho Nightingale + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -24,25 +25,22 @@ * SUCH DAMAGE. */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include - -#include -#include - -#include "vatpic.h" -#include "vatpit.h" -#include "vpmtmr.h" -#include "vrtc.h" -#include "vmm_ioport.h" -#include "vmm_ktr.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #define MAX_IOPORTS 1280 -ioport_handler_func_t ioport_handler[MAX_IOPORTS] = { +static const ioport_handler_func_t ioport_handler[MAX_IOPORTS] = { [TIMER_MODE] = vatpit_handler, [TIMER_CNTR0] = vatpit_handler, [TIMER_CNTR1] = vatpit_handler, @@ -59,7 +57,7 @@ ioport_handler_func_t ioport_handler[MAX_IOPORTS] = { [IO_RTC + 1] = vrtc_data_handler, }; -#ifdef KTR +#ifdef XHYVE_CONFIG_TRACE static const char * inout_instruction(struct vm_exit *vmexit) { @@ -90,12 +88,12 @@ inout_instruction(struct vm_exit *vmexit) if (vmexit->u.inout.string) index += 6; - KASSERT(index < nitems(iodesc), ("%s: invalid index %d", + KASSERT(((unsigned) index) < nitems(iodesc), ("%s: invalid index %d", __func__, index)); return (iodesc[index]); } -#endif /* KTR */ +#endif /* XHYVE_CONFIG_TRACE */ static int emulate_inout_port(struct vm *vm, int vcpuid, struct vm_exit *vmexit, @@ -114,7 +112,7 @@ emulate_inout_port(struct vm *vm, int vcpuid, struct vm_exit *vmexit, return (0); } - mask = vie_size2mask(vmexit->u.inout.bytes); + mask = (uint32_t) vie_size2mask(vmexit->u.inout.bytes); if (!vmexit->u.inout.in) { val = vmexit->u.inout.eax & mask; @@ -146,7 +144,7 @@ emulate_inout_port(struct vm *vm, int vcpuid, struct vm_exit *vmexit, } static int -emulate_inout_str(struct vm *vm, int vcpuid, struct vm_exit *vmexit, bool *retu) +emulate_inout_str(bool *retu) { *retu = true; return (0); /* Return to userspace to finish emulation */ @@ -162,15 +160,17 @@ vm_handle_inout(struct vm *vm, int vcpuid, struct vm_exit *vmexit, bool *retu) ("vm_handle_inout: invalid operand size %d", bytes)); if (vmexit->u.inout.string) - error = emulate_inout_str(vm, vcpuid, vmexit, retu); + error = emulate_inout_str(retu); else error = emulate_inout_port(vm, vcpuid, vmexit, retu); +#ifdef XHYVE_CONFIG_TRACE VCPU_CTR4(vm, vcpuid, "%s%s 0x%04x: %s", vmexit->u.inout.rep ? "rep " : "", inout_instruction(vmexit), vmexit->u.inout.port, error ? "error" : (*retu ? "userspace" : "handled")); +#endif return (error); } diff --git a/vmm/vmm_lapic.c b/src/vmm/vmm_lapic.c similarity index 88% rename from vmm/vmm_lapic.c rename to src/vmm/vmm_lapic.c index 6bccd32..7f584db 100644 --- a/vmm/vmm_lapic.c +++ b/src/vmm/vmm_lapic.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,20 +27,15 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include - -#include -#include - -#include -#include "vmm_ktr.h" -#include "vmm_lapic.h" -#include "vlapic.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include /* * Some MSI message definitions @@ -83,11 +79,11 @@ lapic_set_local_intr(struct vm *vm, int cpu, int vector) if (cpu == -1) dmask = vm_active_cpus(vm); else - CPU_SETOF(cpu, &dmask); + CPU_SETOF(((unsigned) cpu), &dmask); error = 0; while ((cpu = CPU_FFS(&dmask)) != 0) { cpu--; - CPU_CLR(cpu, &dmask); + CPU_CLR(((unsigned) cpu), &dmask); vlapic = vm_lapic(vm, cpu); error = vlapic_trigger_lvt(vlapic, vector); if (error) @@ -104,10 +100,10 @@ lapic_intr_msi(struct vm *vm, uint64_t addr, uint64_t msg) uint32_t dest; bool phys; - VM_CTR2(vm, "lapic MSI addr: %#lx msg: %#lx", addr, msg); + VM_CTR2(vm, "lapic MSI addr: %#llx msg: %#llx", addr, msg); if ((addr & MSI_X86_ADDR_MASK) != MSI_X86_ADDR_BASE) { - VM_CTR1(vm, "lapic MSI invalid addr %#lx", addr); + VM_CTR1(vm, "lapic MSI invalid addr %#llx", addr); return (-1); } @@ -135,7 +131,7 @@ lapic_intr_msi(struct vm *vm, uint64_t addr, uint64_t msg) return (0); } -static boolean_t +static bool x2apic_msr(u_int msr) { if (msr >= 0x800 && msr <= 0xBFF) @@ -151,7 +147,7 @@ x2apic_msr_to_regoff(u_int msr) return ((msr - 0x800) << 4); } -boolean_t +bool lapic_msr(u_int msr) { @@ -207,7 +203,7 @@ lapic_mmio_write(void *vm, int cpu, uint64_t gpa, uint64_t wval, int size, int error; uint64_t off; struct vlapic *vlapic; - +//printf("lapic_mmio_write 0x%016llx 0x%016llx\n", gpa, wval); off = gpa - DEFAULT_APIC_BASE; /* @@ -223,8 +219,8 @@ lapic_mmio_write(void *vm, int cpu, uint64_t gpa, uint64_t wval, int size, } int -lapic_mmio_read(void *vm, int cpu, uint64_t gpa, uint64_t *rval, int size, - void *arg) +lapic_mmio_read(void *vm, int cpu, uint64_t gpa, uint64_t *rval, + UNUSED int size, void *arg) { int error; uint64_t off; @@ -237,11 +233,12 @@ lapic_mmio_read(void *vm, int cpu, uint64_t gpa, uint64_t *rval, int size, * 16-byte boundary. They are also suggested to be 4 bytes * wide, alas not all OSes follow suggestions. */ - off &= ~3; + off &= ~((uint64_t) 3); if (off & 0xf) return (EINVAL); vlapic = vm_lapic(vm, cpu); error = vlapic_read(vlapic, 1, off, rval, arg); + //printf("lapic_mmio_read 0x%016llx (0x%016llx)\n", gpa, *rval); return (error); } diff --git a/bhyve/bhyverun.h b/src/vmm/vmm_mem.c similarity index 63% rename from bhyve/bhyverun.h rename to src/vmm/vmm_mem.c index c51bf48..ccd80a6 100644 --- a/bhyve/bhyverun.h +++ b/src/vmm/vmm_mem.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,30 +27,43 @@ * $FreeBSD$ */ -#ifndef _FBSDRUN_H_ -#define _FBSDRUN_H_ +#include +#include +#include +#include +#include +#include -#ifndef CTASSERT /* Allow lint to override */ -#define CTASSERT(x) _CTASSERT(x, __LINE__) -#define _CTASSERT(x, y) __CTASSERT(x, y) -#define __CTASSERT(x, y) typedef char __assert ## y[(x) ? 1 : -1] -#endif +int +vmm_mem_init(void) +{ + return (0); +} -#define VMEXIT_CONTINUE (0) -#define VMEXIT_ABORT (-1) -struct vmctx; -extern int guest_ncpus; -extern char *guest_uuid_str; -extern char *vmname; +void * +vmm_mem_alloc(uint64_t gpa, size_t size) +{ + void *object; -void *paddr_guest2host(struct vmctx *ctx, uintptr_t addr, size_t len); + object = valloc(size); -void fbsdrun_set_capabilities(struct vmctx *ctx, int cpu); -void fbsdrun_addcpu(struct vmctx *ctx, int fromcpu, int newcpu, uint64_t rip); -int fbsdrun_muxed(void); -int fbsdrun_vmexit_on_hlt(void); -int fbsdrun_vmexit_on_pause(void); -int fbsdrun_disable_x2apic(void); -int fbsdrun_virtio_msix(void); -#endif + if (!object) { + xhyve_abort("vmm_mem_alloc failed\n"); + } + + if (hv_vm_map(object, gpa, size, + HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC)) + { + xhyve_abort("hv_vm_map failed\n"); + } + + return object; +} + +void +vmm_mem_free(uint64_t gpa, size_t size, void *object) +{ + hv_vm_unmap(gpa, size); + free(object); +} diff --git a/vmm/vmm_stat.c b/src/vmm/vmm_stat.c similarity index 90% rename from vmm/vmm_stat.c rename to src/vmm/vmm_stat.c index 4ae5fb9..fee25ea 100644 --- a/vmm/vmm_stat.c +++ b/src/vmm/vmm_stat.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,17 +27,11 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include - -#include -#include "vmm_util.h" -#include "vmm_stat.h" +#include +#include +#include +#include +#include /* * 'vst_num_elems' is the total number of addressable statistic elements @@ -49,8 +44,6 @@ __FBSDID("$FreeBSD$"); static int vst_num_elems, vst_num_types; static struct vmm_stat_type *vsttab[MAX_VMM_STAT_ELEMS]; -static MALLOC_DEFINE(M_VMM_STAT, "vmm stat", "vmm stat"); - #define vst_size ((size_t)vst_num_elems * sizeof(uint64_t)) void @@ -62,12 +55,6 @@ vmm_stat_register(void *arg) if (vst->desc == NULL) return; - if (vst->scope == VMM_STAT_SCOPE_INTEL && !vmm_is_intel()) - return; - - if (vst->scope == VMM_STAT_SCOPE_AMD && !vmm_is_amd()) - return; - if (vst_num_elems + vst->nelems >= MAX_VMM_STAT_ELEMS) { printf("Cannot accomodate vmm stat type \"%s\"!\n", vst->desc); return; @@ -108,7 +95,7 @@ void * vmm_stat_alloc(void) { - return (malloc(vst_size, M_VMM_STAT, M_WAITOK)); + return (malloc(vst_size)); } void @@ -121,7 +108,7 @@ vmm_stat_init(void *vp) void vmm_stat_free(void *vp) { - free(vp, M_VMM_STAT); + free(vp); } int diff --git a/vmm/vmm_util.c b/src/vmm/vmm_util.c similarity index 72% rename from vmm/vmm_util.c rename to src/vmm/vmm_util.c index f245f92..73aa849 100644 --- a/vmm/vmm_util.c +++ b/src/vmm/vmm_util.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,55 +27,42 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); +#include +#include +#include -#include -#include +struct trapframe { + register_t tf_rdi; + register_t tf_rsi; + register_t tf_rdx; + register_t tf_rcx; + register_t tf_r8; + register_t tf_r9; + register_t tf_rax; + register_t tf_rbx; + register_t tf_rbp; + register_t tf_r10; + register_t tf_r11; + register_t tf_r12; + register_t tf_r13; + register_t tf_r14; + register_t tf_r15; + uint32_t tf_trapno; + uint16_t tf_fs; + uint16_t tf_gs; + register_t tf_addr; + uint32_t tf_flags; + uint16_t tf_es; + uint16_t tf_ds; + /* below portion defined in hardware */ + register_t tf_err; + register_t tf_rip; + register_t tf_cs; + register_t tf_rflags; + register_t tf_rsp; + register_t tf_ss; +}; -#include - -#include "vmm_util.h" - -boolean_t -vmm_is_intel(void) -{ - - if (strcmp(cpu_vendor, "GenuineIntel") == 0) - return (TRUE); - else - return (FALSE); -} - -boolean_t -vmm_is_amd(void) -{ - if (strcmp(cpu_vendor, "AuthenticAMD") == 0) - return (TRUE); - else - return (FALSE); -} - -boolean_t -vmm_supports_1G_pages(void) -{ - unsigned int regs[4]; - - /* - * CPUID.80000001:EDX[bit 26] = 1 indicates support for 1GB pages - * - * Both Intel and AMD support this bit. - */ - if (cpu_exthigh >= 0x80000001) { - do_cpuid(0x80000001, regs); - if (regs[3] & (1 << 26)) - return (TRUE); - } - return (FALSE); -} - -#include -#include #define DUMP_REG(x) printf(#x "\t\t0x%016lx\n", (long)(tf->tf_ ## x)) #define DUMP_SEG(x) printf(#x "\t\t0x%04x\n", (unsigned)(tf->tf_ ## x)) void diff --git a/vmm/x86.c b/src/vmm/x86.c similarity index 72% rename from vmm/x86.c rename to src/vmm/x86.c index 525e1d9..315db1e 100644 --- a/vmm/x86.c +++ b/src/vmm/x86.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,52 +27,28 @@ * $FreeBSD$ */ -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include - -#include "vmm_host.h" -#include "vmm_ktr.h" -#include "vmm_util.h" -#include "x86.h" - -SYSCTL_DECL(_hw_vmm); -static SYSCTL_NODE(_hw_vmm, OID_AUTO, topology, CTLFLAG_RD, 0, NULL); +#include +#include +#include +#include +#include +#include +#include +#include +#include #define CPUID_VM_HIGH 0x40000000 static const char bhyve_id[12] = "bhyve bhyve "; -static uint64_t bhyve_xcpuids; -SYSCTL_ULONG(_hw_vmm, OID_AUTO, bhyve_xcpuids, CTLFLAG_RW, &bhyve_xcpuids, 0, - "Number of times an unknown cpuid leaf was accessed"); +static volatile u_long bhyve_xcpuids; /* * The default CPU topology is a single thread per package. */ static u_int threads_per_core = 1; -SYSCTL_UINT(_hw_vmm_topology, OID_AUTO, threads_per_core, CTLFLAG_RDTUN, - &threads_per_core, 0, NULL); - static u_int cores_per_package = 1; -SYSCTL_UINT(_hw_vmm_topology, OID_AUTO, cores_per_package, CTLFLAG_RDTUN, - &cores_per_package, 0, NULL); - static int cpuid_leaf_b = 1; -SYSCTL_INT(_hw_vmm_topology, OID_AUTO, cpuid_leaf_b, CTLFLAG_RDTUN, - &cpuid_leaf_b, 0, NULL); /* * Round up to the next power of two, if necessary, and then take log2. @@ -81,7 +58,7 @@ static __inline int log2(u_int x) { - return (fls(x << (1 - powerof2(x))) - 1); + return (fls((int) (x << (1 - powerof2(x)))) - 1); } int @@ -90,12 +67,26 @@ x86_emulate_cpuid(struct vm *vm, int vcpu_id, { const struct xsave_limits *limits; uint64_t cr4; - int error, enable_invpcid, level, width, x2apic_id; + int error, level, width, x2apic_id; unsigned int func, regs[4], logical_cpus; + u_int cpu_feature, amd_feature, amd_feature2, cpu_high, cpu_exthigh; + u_int tsc_is_invariant, smp_tsc; enum x2apic_state x2apic_state; VCPU_CTR2(vm, vcpu_id, "cpuid %#x,%#x", *eax, *ecx); + tsc_is_invariant = 1; + smp_tsc = 1; + do_cpuid(0, regs); + cpu_high = regs[0]; + do_cpuid(1, regs); + cpu_feature = regs[3]; + do_cpuid(0x80000000, regs); + cpu_exthigh = regs[0]; + do_cpuid(0x80000001, regs); + amd_feature = regs[3] & ~(cpu_feature & 0x0183f3ff); + amd_feature2 = regs[2]; + /* * Requests for invalid CPUID levels should map to the highest * available level instead. @@ -133,54 +124,39 @@ x86_emulate_cpuid(struct vm *vm, int vcpu_id, break; case CPUID_8000_0008: cpuid_count(*eax, *ecx, regs); - if (vmm_is_amd()) { - /* - * XXX this might appear silly because AMD - * cpus don't have threads. - * - * However this matches the logical cpus as - * advertised by leaf 0x1 and will work even - * if the 'threads_per_core' tunable is set - * incorrectly on an AMD host. - */ - logical_cpus = threads_per_core * - cores_per_package; - regs[2] = logical_cpus - 1; - } break; - case CPUID_8000_0001: cpuid_count(*eax, *ecx, regs); /* * Hide SVM and Topology Extension features from guest. */ - regs[2] &= ~(AMDID2_SVM | AMDID2_TOPOLOGY); + regs[2] &= ~((unsigned) (AMDID2_SVM | AMDID2_TOPOLOGY)); /* * Don't advertise extended performance counter MSRs * to the guest. */ - regs[2] &= ~AMDID2_PCXC; - regs[2] &= ~AMDID2_PNXC; - regs[2] &= ~AMDID2_PTSCEL2I; + regs[2] &= ~((unsigned) AMDID2_PCXC); + regs[2] &= ~((unsigned) AMDID2_PNXC); + regs[2] &= ~((unsigned) AMDID2_PTSCEL2I); /* * Don't advertise Instruction Based Sampling feature. */ - regs[2] &= ~AMDID2_IBS; + regs[2] &= ~((unsigned) AMDID2_IBS); /* NodeID MSR not available */ - regs[2] &= ~AMDID2_NODE_ID; + regs[2] &= ~((unsigned) AMDID2_NODE_ID); /* Don't advertise the OS visible workaround feature */ - regs[2] &= ~AMDID2_OSVW; + regs[2] &= ~((unsigned) AMDID2_OSVW); /* * Hide rdtscp/ia32_tsc_aux until we know how * to deal with them. */ - regs[3] &= ~AMDID_RDTSCP; + regs[3] &= ~((unsigned) AMDID_RDTSCP); break; case CPUID_8000_0007: @@ -219,78 +195,77 @@ x86_emulate_cpuid(struct vm *vm, int vcpu_id, error = vm_get_x2apic_state(vm, vcpu_id, &x2apic_state); if (error) { - panic("x86_emulate_cpuid: error %d " - "fetching x2apic state", error); + xhyve_abort("x86_emulate_cpuid: error %d " + "fetching x2apic state\n", error); } /* * Override the APIC ID only in ebx */ - regs[1] &= ~(CPUID_LOCAL_APIC_ID); - regs[1] |= (vcpu_id << CPUID_0000_0001_APICID_SHIFT); + regs[1] &= ~((unsigned) CPUID_LOCAL_APIC_ID); + regs[1] |= (((unsigned) vcpu_id) << CPUID_0000_0001_APICID_SHIFT); /* * Don't expose VMX, SpeedStep, TME or SMX capability. * Advertise x2APIC capability and Hypervisor guest. */ - regs[2] &= ~(CPUID2_VMX | CPUID2_EST | CPUID2_TM2); - regs[2] &= ~(CPUID2_SMX); + regs[2] &= ~((unsigned) (CPUID2_VMX | CPUID2_EST | CPUID2_TM2)); + regs[2] &= ~((unsigned) CPUID2_SMX); - regs[2] |= CPUID2_HV; + regs[2] |= (unsigned) CPUID2_HV; - if (x2apic_state != X2APIC_DISABLED) - regs[2] |= CPUID2_X2APIC; + if (x2apic_state != ((unsigned) X2APIC_DISABLED)) + regs[2] |= ((unsigned) CPUID2_X2APIC); else - regs[2] &= ~CPUID2_X2APIC; + regs[2] &= ~((unsigned) CPUID2_X2APIC); /* * Only advertise CPUID2_XSAVE in the guest if * the host is using XSAVE. */ - if (!(regs[2] & CPUID2_OSXSAVE)) - regs[2] &= ~CPUID2_XSAVE; + if (!(regs[2] & ((unsigned) CPUID2_OSXSAVE))) + regs[2] &= ~((unsigned) CPUID2_XSAVE); /* * If CPUID2_XSAVE is being advertised and the * guest has set CR4_XSAVE, set * CPUID2_OSXSAVE. */ - regs[2] &= ~CPUID2_OSXSAVE; - if (regs[2] & CPUID2_XSAVE) { - error = vm_get_register(vm, vcpu_id, - VM_REG_GUEST_CR4, &cr4); + regs[2] &= ~((unsigned) CPUID2_OSXSAVE); + if (regs[2] & ((unsigned) CPUID2_XSAVE)) { + error = vm_get_register(vm, vcpu_id, VM_REG_GUEST_CR4, &cr4); if (error) - panic("x86_emulate_cpuid: error %d " - "fetching %%cr4", error); + xhyve_abort("x86_emulate_cpuid: error %d " + "fetching %%cr4\n", error); if (cr4 & CR4_XSAVE) - regs[2] |= CPUID2_OSXSAVE; + regs[2] |= ((unsigned) CPUID2_OSXSAVE); } /* * Hide monitor/mwait until we know how to deal with * these instructions. */ - regs[2] &= ~CPUID2_MON; + regs[2] &= ~((unsigned) CPUID2_MON); /* * Hide the performance and debug features. */ - regs[2] &= ~CPUID2_PDCM; + regs[2] &= ~((unsigned) CPUID2_PDCM); /* * No TSC deadline support in the APIC yet */ - regs[2] &= ~CPUID2_TSCDLT; + regs[2] &= ~((unsigned) CPUID2_TSCDLT); /* * Hide thermal monitoring */ - regs[3] &= ~(CPUID_ACPI | CPUID_TM); + regs[3] &= ~((unsigned) (CPUID_ACPI | CPUID_TM)); /* * Hide the debug store capability. */ - regs[3] &= ~CPUID_DS; + regs[3] &= ~((unsigned) CPUID_DS); /* * Advertise the Machine Check and MTRR capability. @@ -298,12 +273,12 @@ x86_emulate_cpuid(struct vm *vm, int vcpu_id, * Some guest OSes (e.g. Windows) will not boot if * these features are absent. */ - regs[3] |= (CPUID_MCA | CPUID_MCE | CPUID_MTRR); + regs[3] |= (unsigned) (CPUID_MCA | CPUID_MCE | CPUID_MTRR); logical_cpus = threads_per_core * cores_per_package; - regs[1] &= ~CPUID_HTT_CORES; + regs[1] &= ~((unsigned) CPUID_HTT_CORES); regs[1] |= (logical_cpus & 0xff) << 16; - regs[3] |= CPUID_HTT; + regs[3] |= (unsigned) CPUID_HTT; break; case CPUID_0000_0004: @@ -353,12 +328,8 @@ x86_emulate_cpuid(struct vm *vm, int vcpu_id, CPUID_STDEXT_AVX512CD); regs[2] = 0; regs[3] = 0; - - /* Advertise INVPCID if it is enabled. */ - error = vm_get_capability(vm, vcpu_id, - VM_CAP_ENABLE_INVPCID, &enable_invpcid); - if (error == 0 && enable_invpcid) - regs[1] |= CPUID_STDEXT_INVPCID; + /* FIXME */ + // regs[1] |= CPUID_STDEXT_INVPCID; } break; @@ -384,6 +355,11 @@ x86_emulate_cpuid(struct vm *vm, int vcpu_id, /* * Processor topology enumeration */ + logical_cpus = 0; + width = 0; + level = 0; + x2apic_id = 0; + if (*ecx == 0) { logical_cpus = threads_per_core; width = log2(logical_cpus); @@ -408,8 +384,8 @@ x86_emulate_cpuid(struct vm *vm, int vcpu_id, regs[0] = width & 0x1f; regs[1] = logical_cpus & 0xffff; - regs[2] = (level << 8) | (*ecx & 0xff); - regs[3] = x2apic_id; + regs[2] = (((unsigned) level) << 8) | (*ecx & 0xff); + regs[3] = (unsigned) x2apic_id; break; case CPUID_0000_000D: @@ -488,34 +464,3 @@ x86_emulate_cpuid(struct vm *vm, int vcpu_id, return (1); } - -bool -vm_cpuid_capability(struct vm *vm, int vcpuid, enum vm_cpuid_capability cap) -{ - bool rv; - - KASSERT(cap > 0 && cap < VCC_LAST, ("%s: invalid vm_cpu_capability %d", - __func__, cap)); - - /* - * Simply passthrough the capabilities of the host cpu for now. - */ - rv = false; - switch (cap) { - case VCC_NO_EXECUTE: - if (amd_feature & AMDID_NX) - rv = true; - break; - case VCC_FFXSR: - if (amd_feature & AMDID_FFXSR) - rv = true; - break; - case VCC_TCE: - if (amd_feature2 & AMDID2_TCE) - rv = true; - break; - default: - panic("%s: unknown vm_cpu_capability %d", __func__, cap); - } - return (rv); -} diff --git a/src/xhyve.c b/src/xhyve.c new file mode 100644 index 0000000..0348219 --- /dev/null +++ b/src/xhyve.c @@ -0,0 +1,923 @@ +/*- + * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define GUEST_NIO_PORT 0x488 /* guest upcalls via i/o port */ + +#define MB (1024UL * 1024) + +typedef int (*vmexit_handler_t)(struct vm_exit *, int *vcpu); +extern int vmexit_task_switch(struct vm_exit *, int *vcpu); + +char *vmname = "vm"; + +int guest_ncpus; +char *guest_uuid_str; + +static int guest_vmexit_on_hlt, guest_vmexit_on_pause; +static int virtio_msix = 1; +static int x2apic_mode = 0; /* default is xAPIC */ + +static int strictio; +static int strictmsr = 1; + +static int acpi; + +static char *progname; +static const int BSP = 0; + +static cpuset_t cpumask; + +static void vcpu_loop(int vcpu, uint64_t rip); + +static struct vm_exit vmexit[VM_MAXCPU]; + +static struct bhyvestats { + uint64_t vmexit_bogus; + uint64_t vmexit_bogus_switch; + uint64_t vmexit_hlt; + uint64_t vmexit_pause; + uint64_t vmexit_mtrap; + uint64_t vmexit_inst_emul; + uint64_t cpu_switch_rotate; + uint64_t cpu_switch_direct; +} stats; + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +static struct mt_vmm_info { + pthread_t mt_thr; + int mt_vcpu; +} mt_vmm_info[VM_MAXCPU]; +#pragma clang diagnostic pop + +__attribute__ ((noreturn)) static void +usage(int code) +{ + + fprintf(stderr, + "Usage: %s [-behuwxACHPWY] [-c vcpus] [-g ] [-l ]\n" + " %*s [-m mem] [-p vcpu:hostcpu] [-s ] [-U uuid] -f \n" + " -A: create ACPI tables\n" + " -c: # cpus (default 1)\n" + " -C: include guest memory in core file\n" + " -e: exit on unhandled I/O access\n" + " -f: firmware\n" + " -g: gdb port\n" + " -h: help\n" + " -H: vmexit from the guest on hlt\n" + " -l: LPC device configuration\n" + " -m: memory size in MB\n" + " -p: pin 'vcpu' to 'hostcpu'\n" + " -P: vmexit from the guest on pause\n" + " -s: PCI slot config\n" + " -u: RTC keeps UTC time\n" + " -U: uuid\n" + " -w: ignore unimplemented MSRs\n" + " -W: force virtio to use single-vector MSI\n" + " -x: local apic is in x2APIC mode\n" + " -Y: disable MPtable generation\n", + progname, (int)strlen(progname), ""); + + exit(code); +} + +void +xh_vm_inject_fault(int vcpu, int vector, int errcode_valid, + uint32_t errcode) +{ + int error, restart_instruction; + + restart_instruction = 1; + + error = xh_vm_inject_exception(vcpu, vector, errcode_valid, errcode, + restart_instruction); + assert(error == 0); +} + +void * +paddr_guest2host(uintptr_t gaddr, size_t len) +{ + return (xh_vm_map_gpa(gaddr, len)); +} + +int +fbsdrun_vmexit_on_pause(void) +{ + return (guest_vmexit_on_pause); +} + +int +fbsdrun_vmexit_on_hlt(void) +{ + return (guest_vmexit_on_hlt); +} + +int +fbsdrun_virtio_msix(void) +{ + return (virtio_msix); +} + +static void +spinup_ap_realmode(int newcpu, uint64_t *rip) +{ + int vector, error; + uint16_t cs; + uint64_t desc_base; + uint32_t desc_limit, desc_access; + + vector = (int) (*rip >> XHYVE_PAGE_SHIFT); + *rip = 0; + + /* + * Update the %cs and %rip of the guest so that it starts + * executing real mode code at at 'vector << 12'. + */ + error = xh_vm_set_register(newcpu, VM_REG_GUEST_RIP, *rip); + assert(error == 0); + + error = xh_vm_get_desc(newcpu, VM_REG_GUEST_CS, &desc_base, &desc_limit, + &desc_access); + assert(error == 0); + + desc_base = (uint64_t) (vector << XHYVE_PAGE_SHIFT); + error = xh_vm_set_desc(newcpu, VM_REG_GUEST_CS, desc_base, desc_limit, + desc_access); + assert(error == 0); + + cs = (uint16_t) ((vector << XHYVE_PAGE_SHIFT) >> 4); + error = xh_vm_set_register(newcpu, VM_REG_GUEST_CS, cs); + assert(error == 0); +} + +static void * +vcpu_thread(void *param) +{ + struct mt_vmm_info *mtp; + uint64_t rip_entry; + int vcpu; + int error; + + mtp = param; + vcpu = mtp->mt_vcpu; + rip_entry = 0xfff0; + + error = xh_vcpu_create(vcpu); + assert(error == 0); + + vcpu_set_capabilities(vcpu); + + error = xh_vcpu_reset(vcpu); + assert(error == 0); + + if (vcpu == BSP) { + rip_entry = kexec(); + } else { + rip_entry = vmexit[vcpu].rip; + spinup_ap_realmode(vcpu, &rip_entry); + } + + vmexit[vcpu].rip = rip_entry; + vmexit[vcpu].inst_length = 0; + + vcpu_loop(vcpu, vmexit[vcpu].rip); + + /* not reached */ + exit(1); + return (NULL); +} + +void +vcpu_add(int fromcpu, int newcpu, uint64_t rip) +{ + int error; + + assert(fromcpu == BSP); + + /* + * The 'newcpu' must be activated in the context of 'fromcpu'. If + * vm_activate_cpu() is delayed until newcpu's pthread starts running + * then vmm.ko is out-of-sync with bhyve and this can create a race + * with vm_suspend(). + */ + error = xh_vm_activate_cpu(newcpu); + assert(error == 0); + + CPU_SET_ATOMIC(((unsigned) newcpu), &cpumask); + + mt_vmm_info[newcpu].mt_vcpu = newcpu; + + vmexit[newcpu].rip = rip; + + error = pthread_create(&mt_vmm_info[newcpu].mt_thr, NULL, vcpu_thread, + &mt_vmm_info[newcpu]); + + assert(error == 0); +} + +static int +vcpu_delete(int vcpu) +{ + if (!CPU_ISSET(((unsigned) vcpu), &cpumask)) { + fprintf(stderr, "Attempting to delete unknown cpu %d\n", vcpu); + exit(1); + } + + CPU_CLR_ATOMIC(((unsigned) vcpu), &cpumask); + return (CPU_EMPTY(&cpumask)); +} + +static int +vmexit_handle_notify(UNUSED struct vm_exit *vme, UNUSED int *pvcpu, + UNUSED uint32_t eax) +{ + return (VMEXIT_CONTINUE); +} + +static int +vmexit_inout(struct vm_exit *vme, int *pvcpu) +{ + int error; + int bytes, port, in, out, string; + int vcpu; + + vcpu = *pvcpu; + + port = vme->u.inout.port; + bytes = vme->u.inout.bytes; + string = vme->u.inout.string; + in = vme->u.inout.in; + out = !in; + + /* Extra-special case of host notifications */ + if (out && port == GUEST_NIO_PORT) { + error = vmexit_handle_notify(vme, pvcpu, vme->u.inout.eax); + return (error); + } + + error = emulate_inout(vcpu, vme, strictio); + if (error) { + fprintf(stderr, "Unhandled %s%c 0x%04x at 0x%llx\n", + in ? "in" : "out", + bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'), + port, vmexit->rip); + return (VMEXIT_ABORT); + } else { + return (VMEXIT_CONTINUE); + } +} + +static int +vmexit_rdmsr(struct vm_exit *vme, int *pvcpu) +{ + uint64_t val; + uint32_t eax, edx; + int error; + + val = 0; + error = emulate_rdmsr(*pvcpu, vme->u.msr.code, &val); + if (error != 0) { + fprintf(stderr, "rdmsr to register %#x on vcpu %d\n", + vme->u.msr.code, *pvcpu); + if (strictmsr) { + vm_inject_gp(*pvcpu); + return (VMEXIT_CONTINUE); + } + } + + eax = (uint32_t) val; + error = xh_vm_set_register(*pvcpu, VM_REG_GUEST_RAX, eax); + assert(error == 0); + + edx = val >> 32; + error = xh_vm_set_register(*pvcpu, VM_REG_GUEST_RDX, edx); + assert(error == 0); + + return (VMEXIT_CONTINUE); +} + +static int +vmexit_wrmsr(struct vm_exit *vme, int *pvcpu) +{ + int error; + + error = emulate_wrmsr(*pvcpu, vme->u.msr.code, vme->u.msr.wval); + if (error != 0) { + fprintf(stderr, "wrmsr to register %#x(%#llx) on vcpu %d\n", + vme->u.msr.code, vme->u.msr.wval, *pvcpu); + if (strictmsr) { + vm_inject_gp(*pvcpu); + return (VMEXIT_CONTINUE); + } + } + return (VMEXIT_CONTINUE); +} + +static int +vmexit_spinup_ap(struct vm_exit *vme, int *pvcpu) +{ + assert(vme->u.spinup_ap.vcpu != 0); + assert(vme->u.spinup_ap.vcpu < guest_ncpus); + + vcpu_add(*pvcpu, vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip); + + return (VMEXIT_CONTINUE); +} + +static int +vmexit_vmx(struct vm_exit *vme, int *pvcpu) +{ + fprintf(stderr, "vm exit[%d]\n", *pvcpu); + fprintf(stderr, "\treason\t\tVMX\n"); + fprintf(stderr, "\trip\t\t0x%016llx\n", vme->rip); + fprintf(stderr, "\tinst_length\t%d\n", vme->inst_length); + fprintf(stderr, "\tstatus\t\t%d\n", vme->u.vmx.status); + fprintf(stderr, "\texit_reason\t%u\n", vme->u.vmx.exit_reason); + fprintf(stderr, "\tqualification\t0x%016llx\n", + vme->u.vmx.exit_qualification); + fprintf(stderr, "\tinst_type\t\t%d\n", vme->u.vmx.inst_type); + fprintf(stderr, "\tinst_error\t\t%d\n", vme->u.vmx.inst_error); + return (VMEXIT_ABORT); +} + +static int +vmexit_bogus(struct vm_exit *vme, UNUSED int *pvcpu) +{ + assert(vme->inst_length == 0); + + stats.vmexit_bogus++; + + return (VMEXIT_CONTINUE); +} + +static int +vmexit_hlt(UNUSED struct vm_exit *vme, UNUSED int *pvcpu) +{ + stats.vmexit_hlt++; + + /* + * Just continue execution with the next instruction. We use + * the HLT VM exit as a way to be friendly with the host + * scheduler. + */ + return (VMEXIT_CONTINUE); +} + +static int +vmexit_pause(UNUSED struct vm_exit *vme, UNUSED int *pvcpu) +{ + stats.vmexit_pause++; + + return (VMEXIT_CONTINUE); +} + +static int +vmexit_mtrap(struct vm_exit *vme, UNUSED int *pvcpu) +{ + assert(vme->inst_length == 0); + + stats.vmexit_mtrap++; + + return (VMEXIT_CONTINUE); +} + +static int +vmexit_inst_emul(struct vm_exit *vme, int *pvcpu) +{ + int err, i; + struct vie *vie; + + stats.vmexit_inst_emul++; + + vie = &vme->u.inst_emul.vie; + err = emulate_mem(*pvcpu, vme->u.inst_emul.gpa, vie, + &vme->u.inst_emul.paging); + + if (err) { + if (err == ESRCH) { + fprintf(stderr, "Unhandled memory access to 0x%llx\n", + vme->u.inst_emul.gpa); + } + + fprintf(stderr, "Failed to emulate instruction ["); + for (i = 0; i < vie->num_valid; i++) { + fprintf(stderr, "0x%02x%s", vie->inst[i], + i != (vie->num_valid - 1) ? " " : ""); + } + fprintf(stderr, "] at 0x%llx\n", vme->rip); + return (VMEXIT_ABORT); + } + + return (VMEXIT_CONTINUE); +} + +static pthread_mutex_t resetcpu_mtx = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t resetcpu_cond = PTHREAD_COND_INITIALIZER; + +static int +vmexit_suspend(struct vm_exit *vme, int *pvcpu) +{ + enum vm_suspend_how how; + + how = vme->u.suspended.how; + + vcpu_delete(*pvcpu); + + if (*pvcpu != BSP) { + pthread_mutex_lock(&resetcpu_mtx); + pthread_cond_signal(&resetcpu_cond); + pthread_mutex_unlock(&resetcpu_mtx); + pthread_exit(NULL); + } + + pthread_mutex_lock(&resetcpu_mtx); + while (!CPU_EMPTY(&cpumask)) { + pthread_cond_wait(&resetcpu_cond, &resetcpu_mtx); + } + pthread_mutex_unlock(&resetcpu_mtx); + + switch ((int) (how)) { + case VM_SUSPEND_RESET: + exit(0); + case VM_SUSPEND_POWEROFF: + exit(1); + case VM_SUSPEND_HALT: + exit(2); + case VM_SUSPEND_TRIPLEFAULT: + exit(3); + default: + fprintf(stderr, "vmexit_suspend: invalid reason %d\n", how); + exit(100); + } +} + +static vmexit_handler_t handler[VM_EXITCODE_MAX] = { + [VM_EXITCODE_INOUT] = vmexit_inout, + [VM_EXITCODE_INOUT_STR] = vmexit_inout, + [VM_EXITCODE_VMX] = vmexit_vmx, + [VM_EXITCODE_BOGUS] = vmexit_bogus, + [VM_EXITCODE_RDMSR] = vmexit_rdmsr, + [VM_EXITCODE_WRMSR] = vmexit_wrmsr, + [VM_EXITCODE_MTRAP] = vmexit_mtrap, + [VM_EXITCODE_INST_EMUL] = vmexit_inst_emul, + [VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap, + [VM_EXITCODE_SUSPENDED] = vmexit_suspend, + [VM_EXITCODE_TASK_SWITCH] = vmexit_task_switch, +}; + +void +vcpu_set_capabilities(int cpu) +{ + int err, tmp; + + if (fbsdrun_vmexit_on_hlt()) { + err = xh_vm_get_capability(cpu, VM_CAP_HALT_EXIT, &tmp); + if (err < 0) { + fprintf(stderr, "VM exit on HLT not supported\n"); + exit(1); + } + xh_vm_set_capability(cpu, VM_CAP_HALT_EXIT, 1); + if (cpu == BSP) + handler[VM_EXITCODE_HLT] = vmexit_hlt; + } + + if (fbsdrun_vmexit_on_pause()) { + /* + * pause exit support required for this mode + */ + err = xh_vm_get_capability(cpu, VM_CAP_PAUSE_EXIT, &tmp); + if (err < 0) { + fprintf(stderr, + "SMP mux requested, no pause support\n"); + exit(1); + } + xh_vm_set_capability(cpu, VM_CAP_PAUSE_EXIT, 1); + if (cpu == BSP) + handler[VM_EXITCODE_PAUSE] = vmexit_pause; + } + + if (x2apic_mode) + err = xh_vm_set_x2apic_state(cpu, X2APIC_ENABLED); + else + err = xh_vm_set_x2apic_state(cpu, X2APIC_DISABLED); + + if (err) { + fprintf(stderr, "Unable to set x2apic state (%d)\n", err); + exit(1); + } +} + +static void +vcpu_loop(int vcpu, uint64_t startrip) +{ + int error, rc, prevcpu; + enum vm_exitcode exitcode; + cpuset_t active_cpus; + + error = xh_vm_active_cpus(&active_cpus); + assert(CPU_ISSET(((unsigned) vcpu), &active_cpus)); + + error = xh_vm_set_register(vcpu, VM_REG_GUEST_RIP, startrip); + assert(error == 0); + + while (1) { + error = xh_vm_run(vcpu, &vmexit[vcpu]); + if (error != 0) + break; + + prevcpu = vcpu; + + exitcode = vmexit[vcpu].exitcode; + if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) { + fprintf(stderr, "vcpu_loop: unexpected exitcode 0x%x\n", + exitcode); + exit(1); + } + + rc = (*handler[exitcode])(&vmexit[vcpu], &vcpu); + + switch (rc) { + case VMEXIT_CONTINUE: + break; + case VMEXIT_ABORT: + abort(); + default: + exit(1); + } + } + fprintf(stderr, "vm_run error %d, errno %d\n", error, errno); +} + +static int +num_vcpus_allowed(void) +{ + return (VM_MAXCPU); +} + +static int +expand_number(const char *buf, uint64_t *num) +{ + char *endptr; + uintmax_t umaxval; + uint64_t number; + unsigned shift; + int serrno; + + serrno = errno; + errno = 0; + umaxval = strtoumax(buf, &endptr, 0); + if (umaxval > UINT64_MAX) + errno = ERANGE; + if (errno != 0) + return (-1); + errno = serrno; + number = umaxval; + + switch (tolower((unsigned char)*endptr)) { + case 'e': + shift = 60; + break; + case 'p': + shift = 50; + break; + case 't': + shift = 40; + break; + case 'g': + shift = 30; + break; + case 'm': + shift = 20; + break; + case 'k': + shift = 10; + break; + case 'b': + case '\0': /* No unit. */ + *num = number; + return (0); + default: + /* Unrecognized unit. */ + errno = EINVAL; + return (-1); + } + + if ((number << shift) >> shift != number) { + /* Overflow */ + errno = ERANGE; + return (-1); + } + *num = number << shift; + return (0); +} + +static int +parse_memsize(const char *opt, size_t *ret_memsize) +{ + char *endptr; + size_t optval; + int error; + + optval = strtoul(opt, &endptr, 0); + if (*opt != '\0' && *endptr == '\0') { + /* + * For the sake of backward compatibility if the memory size + * specified on the command line is less than a megabyte then + * it is interpreted as being in units of MB. + */ + if (optval < MB) + optval *= MB; + *ret_memsize = optval; + error = 0; + } else + error = expand_number(opt, ((uint64_t *) ret_memsize)); + + return (error); +} + +static int +firmware_parse(const char *opt) { + char *fw, *kernel, *initrd, *cmdline, *cp; + + fw = strdup(opt); + + if (strncmp(fw, "kexec", strlen("kexec")) != 0) { + goto fail; + } + + if ((cp = strchr(fw, ',')) != NULL) { + *cp = '\0'; + kernel = cp + 1; + } else { + goto fail; + } + + if ((cp = strchr(kernel, ',')) != NULL) { + *cp = '\0'; + initrd = cp + 1; + } else { + goto fail; + } + + if ((cp = strchr(initrd, ',')) != NULL) { + *cp = '\0'; + cmdline = cp + 1; + } else { + goto fail; + } + + initrd = strlen(initrd) ? initrd : NULL; + cmdline = strlen(cmdline) ? cmdline : NULL; + + kexec_init(kernel, initrd, cmdline); + + return 0; + +fail: + fprintf(stderr, "Invalid firmare argument\n" + " -f kexec,'kernel','initrd','\"cmdline\"'\n"); + + return -1; +} + +int +main(int argc, char *argv[]) +{ + int c, error, gdb_port, bvmcons, fw; + int dump_guest_memory, max_vcpus, mptgen; + int rtc_localtime; + uint64_t rip; + size_t memsize; + + bvmcons = 0; + dump_guest_memory = 0; + progname = basename(argv[0]); + gdb_port = 0; + guest_ncpus = 1; + memsize = 256 * MB; + mptgen = 1; + rtc_localtime = 1; + fw = 0; + + while ((c = getopt(argc, argv, "behuwxACHPWY:f:g:c:s:m:l:U:")) != -1) { + switch (c) { + case 'A': + acpi = 1; + break; + case 'b': + bvmcons = 1; + break; + case 'c': + guest_ncpus = atoi(optarg); + break; + case 'C': + dump_guest_memory = 1; + break; + case 'f': + if (firmware_parse(optarg) != 0) { + exit (1); + } else { + fw = 1; + break; + } + case 'g': + gdb_port = atoi(optarg); + break; + case 'l': + if (lpc_device_parse(optarg) != 0) { + errx(EX_USAGE, "invalid lpc device " + "configuration '%s'", optarg); + } + break; + case 's': + if (pci_parse_slot(optarg) != 0) + exit(1); + else + break; + case 'm': + error = parse_memsize(optarg, &memsize); + if (error) + errx(EX_USAGE, "invalid memsize '%s'", optarg); + break; + case 'H': + guest_vmexit_on_hlt = 1; + break; + case 'P': + guest_vmexit_on_pause = 1; + break; + case 'e': + strictio = 1; + break; + case 'u': + rtc_localtime = 0; + break; + case 'U': + guest_uuid_str = optarg; + break; + case 'w': + strictmsr = 0; + break; + case 'W': + virtio_msix = 0; + break; + case 'x': + x2apic_mode = 1; + break; + case 'Y': + mptgen = 0; + break; + case 'h': + usage(0); + default: + usage(1); + } + } + + if (fw != 1) + usage(1); + + error = xh_vm_create(); + if (error) { + fprintf(stderr, "Unable to create VM (%d)\n", error); + exit(1); + } + + if (guest_ncpus < 1) { + fprintf(stderr, "Invalid guest vCPUs (%d)\n", guest_ncpus); + exit(1); + } + + max_vcpus = num_vcpus_allowed(); + if (guest_ncpus > max_vcpus) { + fprintf(stderr, "%d vCPUs requested but only %d available\n", + guest_ncpus, max_vcpus); + exit(1); + } + + error = xh_vm_setup_memory(memsize, VM_MMAP_ALL); + if (error) { + fprintf(stderr, "Unable to setup memory (%d)\n", error); + exit(1); + } + + error = init_msr(); + if (error) { + fprintf(stderr, "init_msr error %d\n", error); + exit(1); + } + + init_mem(); + init_inout(); + pci_irq_init(); + ioapic_init(); + + rtc_init(rtc_localtime); + sci_init(); + + /* + * Exit if a device emulation finds an error in it's initilization + */ + if (init_pci() != 0) + exit(1); + + if (gdb_port != 0) + init_dbgport(gdb_port); + + if (bvmcons) + init_bvmcons(); + + /* + * build the guest tables, MP etc. + */ + if (mptgen) { + error = mptable_build(guest_ncpus); + if (error) + exit(1); + } + + error = smbios_build(); + assert(error == 0); + + if (acpi) { + error = acpi_build(guest_ncpus); + assert(error == 0); + } + + + rip = 0; + + vcpu_add(BSP, BSP, rip); + + /* + * Head off to the main event dispatch loop + */ + mevent_dispatch(); + + exit(1); +} diff --git a/src/xmsr.c b/src/xmsr.c new file mode 100644 index 0000000..79424e1 --- /dev/null +++ b/src/xmsr.c @@ -0,0 +1,104 @@ +/*- + * Copyright (c) 2011 NetApp, Inc. + * Copyright (c) 2015 xhyve developers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +int +emulate_wrmsr(UNUSED int vcpu, uint32_t num, UNUSED uint64_t val) +{ + switch (num) { + case 0xd04: /* Sandy Bridge uncore PMCs */ + case 0xc24: + return (0); + case MSR_BIOS_UPDT_TRIG: + return (0); + case MSR_BIOS_SIGN: + return (0); + default: + break; + } + + return (-1); +} + +int +emulate_rdmsr(UNUSED int vcpu, uint32_t num, uint64_t *val) +{ + int error = 0; + + switch (num) { + case MSR_BIOS_SIGN: + case MSR_IA32_PLATFORM_ID: + case MSR_PKG_ENERGY_STATUS: + case MSR_PP0_ENERGY_STATUS: + case MSR_PP1_ENERGY_STATUS: + case MSR_DRAM_ENERGY_STATUS: + *val = 0; + break; + case MSR_RAPL_POWER_UNIT: + /* + * Use the default value documented in section + * "RAPL Interfaces" in Intel SDM vol3. + */ + *val = 0x000a1003; + break; + default: + error = -1; + break; + } + + return (error); +} + +int +init_msr(void) +{ + u_int regs[4]; + u_int cpu_vendor[4]; + + do_cpuid(0, regs); + cpu_vendor[0] = regs[1]; + cpu_vendor[1] = regs[3]; + cpu_vendor[2] = regs[2]; + cpu_vendor[3] = 0; + + if (strcmp(((char *) cpu_vendor), "GenuineIntel") == 0) { + return 0; + } else { + fprintf(stderr, "Unknown cpu vendor \"%s\"\n", ((char *) cpu_vendor)); + return (-1); + } +} diff --git a/test/initrd.gz b/test/initrd.gz new file mode 100644 index 0000000..c427032 Binary files /dev/null and b/test/initrd.gz differ diff --git a/test/tinycore.txt b/test/tinycore.txt new file mode 100644 index 0000000..e04ded1 --- /dev/null +++ b/test/tinycore.txt @@ -0,0 +1,9 @@ +These are binaries from + http://tinycorelinux.net +with the following patch applied: + +mkdir initrd +( cd initrd ; zcat ../initrd.gz | sudo cpio -idm ) +sudo sed -i '/^# ttyS0$/s#^..##' initrd/etc/securetty +sudo sed -i '/^tty1:/s#tty1#ttyS0#g' initrd/etc/inittab +( cd initrd ; find | sudo cpio -o -H newc ) | gzip -c > initrd.gz diff --git a/test/vmlinuz b/test/vmlinuz new file mode 100644 index 0000000..cb74d02 Binary files /dev/null and b/test/vmlinuz differ diff --git a/vmm.h b/vmm.h deleted file mode 100644 index d3798bc..0000000 --- a/vmm.h +++ /dev/null @@ -1,648 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#ifndef _VMM_H_ -#define _VMM_H_ - -#include - -enum vm_suspend_how { - VM_SUSPEND_NONE, - VM_SUSPEND_RESET, - VM_SUSPEND_POWEROFF, - VM_SUSPEND_HALT, - VM_SUSPEND_TRIPLEFAULT, - VM_SUSPEND_LAST -}; - -/* - * Identifiers for architecturally defined registers. - */ -enum vm_reg_name { - VM_REG_GUEST_RAX, - VM_REG_GUEST_RBX, - VM_REG_GUEST_RCX, - VM_REG_GUEST_RDX, - VM_REG_GUEST_RSI, - VM_REG_GUEST_RDI, - VM_REG_GUEST_RBP, - VM_REG_GUEST_R8, - VM_REG_GUEST_R9, - VM_REG_GUEST_R10, - VM_REG_GUEST_R11, - VM_REG_GUEST_R12, - VM_REG_GUEST_R13, - VM_REG_GUEST_R14, - VM_REG_GUEST_R15, - VM_REG_GUEST_CR0, - VM_REG_GUEST_CR3, - VM_REG_GUEST_CR4, - VM_REG_GUEST_DR7, - VM_REG_GUEST_RSP, - VM_REG_GUEST_RIP, - VM_REG_GUEST_RFLAGS, - VM_REG_GUEST_ES, - VM_REG_GUEST_CS, - VM_REG_GUEST_SS, - VM_REG_GUEST_DS, - VM_REG_GUEST_FS, - VM_REG_GUEST_GS, - VM_REG_GUEST_LDTR, - VM_REG_GUEST_TR, - VM_REG_GUEST_IDTR, - VM_REG_GUEST_GDTR, - VM_REG_GUEST_EFER, - VM_REG_GUEST_CR2, - VM_REG_GUEST_PDPTE0, - VM_REG_GUEST_PDPTE1, - VM_REG_GUEST_PDPTE2, - VM_REG_GUEST_PDPTE3, - VM_REG_GUEST_INTR_SHADOW, - VM_REG_LAST -}; - -enum x2apic_state { - X2APIC_DISABLED, - X2APIC_ENABLED, - X2APIC_STATE_LAST -}; - -#define VM_INTINFO_VECTOR(info) ((info) & 0xff) -#define VM_INTINFO_DEL_ERRCODE 0x800 -#define VM_INTINFO_RSVD 0x7ffff000 -#define VM_INTINFO_VALID 0x80000000 -#define VM_INTINFO_TYPE 0x700 -#define VM_INTINFO_HWINTR (0 << 8) -#define VM_INTINFO_NMI (2 << 8) -#define VM_INTINFO_HWEXCEPTION (3 << 8) -#define VM_INTINFO_SWINTR (4 << 8) - -#ifdef _KERNEL - -#define VM_MAX_NAMELEN 32 - -struct vm; -struct vm_exception; -struct vm_memory_segment; -struct seg_desc; -struct vm_exit; -struct vm_run; -struct vhpet; -struct vioapic; -struct vlapic; -struct vmspace; -struct vm_object; -struct vm_guest_paging; -struct pmap; - -typedef int (*vmm_init_func_t)(int ipinum); -typedef int (*vmm_cleanup_func_t)(void); -typedef void (*vmm_resume_func_t)(void); -typedef void * (*vmi_init_func_t)(struct vm *vm, struct pmap *pmap); -typedef int (*vmi_run_func_t)(void *vmi, int vcpu, register_t rip, - struct pmap *pmap, void *rendezvous_cookie, - void *suspend_cookie); -typedef void (*vmi_cleanup_func_t)(void *vmi); -typedef int (*vmi_get_register_t)(void *vmi, int vcpu, int num, - uint64_t *retval); -typedef int (*vmi_set_register_t)(void *vmi, int vcpu, int num, - uint64_t val); -typedef int (*vmi_get_desc_t)(void *vmi, int vcpu, int num, - struct seg_desc *desc); -typedef int (*vmi_set_desc_t)(void *vmi, int vcpu, int num, - struct seg_desc *desc); -typedef int (*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval); -typedef int (*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val); -typedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max); -typedef void (*vmi_vmspace_free)(struct vmspace *vmspace); -typedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu); -typedef void (*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic); - -struct vmm_ops { - vmm_init_func_t init; /* module wide initialization */ - vmm_cleanup_func_t cleanup; - vmm_resume_func_t resume; - - vmi_init_func_t vminit; /* vm-specific initialization */ - vmi_run_func_t vmrun; - vmi_cleanup_func_t vmcleanup; - vmi_get_register_t vmgetreg; - vmi_set_register_t vmsetreg; - vmi_get_desc_t vmgetdesc; - vmi_set_desc_t vmsetdesc; - vmi_get_cap_t vmgetcap; - vmi_set_cap_t vmsetcap; - vmi_vmspace_alloc vmspace_alloc; - vmi_vmspace_free vmspace_free; - vmi_vlapic_init vlapic_init; - vmi_vlapic_cleanup vlapic_cleanup; -}; - -extern struct vmm_ops vmm_ops_intel; -extern struct vmm_ops vmm_ops_amd; - -int vm_create(const char *name, struct vm **retvm); -void vm_destroy(struct vm *vm); -int vm_reinit(struct vm *vm); -const char *vm_name(struct vm *vm); -int vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len); -int vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa); -int vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len); -void *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot, - void **cookie); -void vm_gpa_release(void *cookie); -int vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, - struct vm_memory_segment *seg); -int vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len, - vm_offset_t *offset, struct vm_object **object); -boolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa); -int vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval); -int vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val); -int vm_get_seg_desc(struct vm *vm, int vcpu, int reg, - struct seg_desc *ret_desc); -int vm_set_seg_desc(struct vm *vm, int vcpu, int reg, - struct seg_desc *desc); -int vm_run(struct vm *vm, struct vm_run *vmrun); -int vm_suspend(struct vm *vm, enum vm_suspend_how how); -int vm_inject_nmi(struct vm *vm, int vcpu); -int vm_nmi_pending(struct vm *vm, int vcpuid); -void vm_nmi_clear(struct vm *vm, int vcpuid); -int vm_inject_extint(struct vm *vm, int vcpu); -int vm_extint_pending(struct vm *vm, int vcpuid); -void vm_extint_clear(struct vm *vm, int vcpuid); -struct vlapic *vm_lapic(struct vm *vm, int cpu); -struct vioapic *vm_ioapic(struct vm *vm); -struct vhpet *vm_hpet(struct vm *vm); -int vm_get_capability(struct vm *vm, int vcpu, int type, int *val); -int vm_set_capability(struct vm *vm, int vcpu, int type, int val); -int vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state); -int vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state); -int vm_apicid2vcpuid(struct vm *vm, int apicid); -int vm_activate_cpu(struct vm *vm, int vcpu); -struct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid); -void vm_exit_suspended(struct vm *vm, int vcpuid, uint64_t rip); -void vm_exit_rendezvous(struct vm *vm, int vcpuid, uint64_t rip); -void vm_exit_astpending(struct vm *vm, int vcpuid, uint64_t rip); - -#ifdef _SYS__CPUSET_H_ -/* - * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'. - * The rendezvous 'func(arg)' is not allowed to do anything that will - * cause the thread to be put to sleep. - * - * If the rendezvous is being initiated from a vcpu context then the - * 'vcpuid' must refer to that vcpu, otherwise it should be set to -1. - * - * The caller cannot hold any locks when initiating the rendezvous. - * - * The implementation of this API may cause vcpus other than those specified - * by 'dest' to be stalled. The caller should not rely on any vcpus making - * forward progress when the rendezvous is in progress. - */ -typedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg); -void vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest, - vm_rendezvous_func_t func, void *arg); -cpuset_t vm_active_cpus(struct vm *vm); -cpuset_t vm_suspended_cpus(struct vm *vm); -#endif /* _SYS__CPUSET_H_ */ - -static __inline int -vcpu_rendezvous_pending(void *rendezvous_cookie) -{ - - return (*(uintptr_t *)rendezvous_cookie != 0); -} - -static __inline int -vcpu_suspended(void *suspend_cookie) -{ - - return (*(int *)suspend_cookie); -} - -/* - * Return 1 if device indicated by bus/slot/func is supposed to be a - * pci passthrough device. - * - * Return 0 otherwise. - */ -int vmm_is_pptdev(int bus, int slot, int func); - -void *vm_iommu_domain(struct vm *vm); - -enum vcpu_state { - VCPU_IDLE, - VCPU_FROZEN, - VCPU_RUNNING, - VCPU_SLEEPING, -}; - -int vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state, - bool from_idle); -enum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu); - -static int __inline -vcpu_is_running(struct vm *vm, int vcpu, int *hostcpu) -{ - return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING); -} - -#ifdef _SYS_PROC_H_ -static int __inline -vcpu_should_yield(struct vm *vm, int vcpu) -{ - - if (curthread->td_flags & (TDF_ASTPENDING | TDF_NEEDRESCHED)) - return (1); - else if (curthread->td_owepreempt) - return (1); - else - return (0); -} -#endif - -void *vcpu_stats(struct vm *vm, int vcpu); -void vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr); -struct vmspace *vm_get_vmspace(struct vm *vm); -int vm_assign_pptdev(struct vm *vm, int bus, int slot, int func); -int vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func); -struct vatpic *vm_atpic(struct vm *vm); -struct vatpit *vm_atpit(struct vm *vm); -struct vpmtmr *vm_pmtmr(struct vm *vm); -struct vrtc *vm_rtc(struct vm *vm); - -/* - * Inject exception 'vector' into the guest vcpu. This function returns 0 on - * success and non-zero on failure. - * - * Wrapper functions like 'vm_inject_gp()' should be preferred to calling - * this function directly because they enforce the trap-like or fault-like - * behavior of an exception. - * - * This function should only be called in the context of the thread that is - * executing this vcpu. - */ -int vm_inject_exception(struct vm *vm, int vcpuid, int vector, int err_valid, - uint32_t errcode, int restart_instruction); - -/* - * This function is called after a VM-exit that occurred during exception or - * interrupt delivery through the IDT. The format of 'intinfo' is described - * in Figure 15-1, "EXITINTINFO for All Intercepts", APM, Vol 2. - * - * If a VM-exit handler completes the event delivery successfully then it - * should call vm_exit_intinfo() to extinguish the pending event. For e.g., - * if the task switch emulation is triggered via a task gate then it should - * call this function with 'intinfo=0' to indicate that the external event - * is not pending anymore. - * - * Return value is 0 on success and non-zero on failure. - */ -int vm_exit_intinfo(struct vm *vm, int vcpuid, uint64_t intinfo); - -/* - * This function is called before every VM-entry to retrieve a pending - * event that should be injected into the guest. This function combines - * nested events into a double or triple fault. - * - * Returns 0 if there are no events that need to be injected into the guest - * and non-zero otherwise. - */ -int vm_entry_intinfo(struct vm *vm, int vcpuid, uint64_t *info); - -int vm_get_intinfo(struct vm *vm, int vcpuid, uint64_t *info1, uint64_t *info2); - -enum vm_reg_name vm_segment_name(int seg_encoding); - -struct vm_copyinfo { - uint64_t gpa; - size_t len; - void *hva; - void *cookie; -}; - -/* - * Set up 'copyinfo[]' to copy to/from guest linear address space starting - * at 'gla' and 'len' bytes long. The 'prot' should be set to PROT_READ for - * a copyin or PROT_WRITE for a copyout. - * - * retval is_fault Intepretation - * 0 0 Success - * 0 1 An exception was injected into the guest - * EFAULT N/A Unrecoverable error - * - * The 'copyinfo[]' can be passed to 'vm_copyin()' or 'vm_copyout()' only if - * the return value is 0. The 'copyinfo[]' resources should be freed by calling - * 'vm_copy_teardown()' after the copy is done. - */ -int vm_copy_setup(struct vm *vm, int vcpuid, struct vm_guest_paging *paging, - uint64_t gla, size_t len, int prot, struct vm_copyinfo *copyinfo, - int num_copyinfo, int *is_fault); -void vm_copy_teardown(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo, - int num_copyinfo); -void vm_copyin(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo, - void *kaddr, size_t len); -void vm_copyout(struct vm *vm, int vcpuid, const void *kaddr, - struct vm_copyinfo *copyinfo, size_t len); - -int vcpu_trace_exceptions(struct vm *vm, int vcpuid); -#endif /* KERNEL */ - -#define VM_MAXCPU 16 /* maximum virtual cpus */ - -/* - * Identifiers for optional vmm capabilities - */ -enum vm_cap_type { - VM_CAP_HALT_EXIT, - VM_CAP_MTRAP_EXIT, - VM_CAP_PAUSE_EXIT, - VM_CAP_UNRESTRICTED_GUEST, - VM_CAP_ENABLE_INVPCID, - VM_CAP_MAX -}; - -enum vm_intr_trigger { - EDGE_TRIGGER, - LEVEL_TRIGGER -}; - -/* - * The 'access' field has the format specified in Table 21-2 of the Intel - * Architecture Manual vol 3b. - * - * XXX The contents of the 'access' field are architecturally defined except - * bit 16 - Segment Unusable. - */ -struct seg_desc { - uint64_t base; - uint32_t limit; - uint32_t access; -}; -#define SEG_DESC_TYPE(access) ((access) & 0x001f) -#define SEG_DESC_DPL(access) (((access) >> 5) & 0x3) -#define SEG_DESC_PRESENT(access) (((access) & 0x0080) ? 1 : 0) -#define SEG_DESC_DEF32(access) (((access) & 0x4000) ? 1 : 0) -#define SEG_DESC_GRANULARITY(access) (((access) & 0x8000) ? 1 : 0) -#define SEG_DESC_UNUSABLE(access) (((access) & 0x10000) ? 1 : 0) - -enum vm_cpu_mode { - CPU_MODE_REAL, - CPU_MODE_PROTECTED, - CPU_MODE_COMPATIBILITY, /* IA-32E mode (CS.L = 0) */ - CPU_MODE_64BIT, /* IA-32E mode (CS.L = 1) */ -}; - -enum vm_paging_mode { - PAGING_MODE_FLAT, - PAGING_MODE_32, - PAGING_MODE_PAE, - PAGING_MODE_64, -}; - -struct vm_guest_paging { - uint64_t cr3; - int cpl; - enum vm_cpu_mode cpu_mode; - enum vm_paging_mode paging_mode; -}; - -/* - * The data structures 'vie' and 'vie_op' are meant to be opaque to the - * consumers of instruction decoding. The only reason why their contents - * need to be exposed is because they are part of the 'vm_exit' structure. - */ -struct vie_op { - uint8_t op_byte; /* actual opcode byte */ - uint8_t op_type; /* type of operation (e.g. MOV) */ - uint16_t op_flags; -}; - -#define VIE_INST_SIZE 15 -struct vie { - uint8_t inst[VIE_INST_SIZE]; /* instruction bytes */ - uint8_t num_valid; /* size of the instruction */ - uint8_t num_processed; - - uint8_t addrsize:4, opsize:4; /* address and operand sizes */ - uint8_t rex_w:1, /* REX prefix */ - rex_r:1, - rex_x:1, - rex_b:1, - rex_present:1, - repz_present:1, /* REP/REPE/REPZ prefix */ - repnz_present:1, /* REPNE/REPNZ prefix */ - opsize_override:1, /* Operand size override */ - addrsize_override:1, /* Address size override */ - segment_override:1; /* Segment override */ - - uint8_t mod:2, /* ModRM byte */ - reg:4, - rm:4; - - uint8_t ss:2, /* SIB byte */ - index:4, - base:4; - - uint8_t disp_bytes; - uint8_t imm_bytes; - - uint8_t scale; - int base_register; /* VM_REG_GUEST_xyz */ - int index_register; /* VM_REG_GUEST_xyz */ - int segment_register; /* VM_REG_GUEST_xyz */ - - int64_t displacement; /* optional addr displacement */ - int64_t immediate; /* optional immediate operand */ - - uint8_t decoded; /* set to 1 if successfully decoded */ - - struct vie_op op; /* opcode description */ -}; - -enum vm_exitcode { - VM_EXITCODE_INOUT, - VM_EXITCODE_VMX, - VM_EXITCODE_BOGUS, - VM_EXITCODE_RDMSR, - VM_EXITCODE_WRMSR, - VM_EXITCODE_HLT, - VM_EXITCODE_MTRAP, - VM_EXITCODE_PAUSE, - VM_EXITCODE_PAGING, - VM_EXITCODE_INST_EMUL, - VM_EXITCODE_SPINUP_AP, - VM_EXITCODE_DEPRECATED1, /* used to be SPINDOWN_CPU */ - VM_EXITCODE_RENDEZVOUS, - VM_EXITCODE_IOAPIC_EOI, - VM_EXITCODE_SUSPENDED, - VM_EXITCODE_INOUT_STR, - VM_EXITCODE_TASK_SWITCH, - VM_EXITCODE_MONITOR, - VM_EXITCODE_MWAIT, - VM_EXITCODE_SVM, - VM_EXITCODE_MAX -}; - -struct vm_inout { - uint16_t bytes:3; /* 1 or 2 or 4 */ - uint16_t in:1; - uint16_t string:1; - uint16_t rep:1; - uint16_t port; - uint32_t eax; /* valid for out */ -}; - -struct vm_inout_str { - struct vm_inout inout; /* must be the first element */ - struct vm_guest_paging paging; - uint64_t rflags; - uint64_t cr0; - uint64_t index; - uint64_t count; /* rep=1 (%rcx), rep=0 (1) */ - int addrsize; - enum vm_reg_name seg_name; - struct seg_desc seg_desc; -}; - -enum task_switch_reason { - TSR_CALL, - TSR_IRET, - TSR_JMP, - TSR_IDT_GATE, /* task gate in IDT */ -}; - -struct vm_task_switch { - uint16_t tsssel; /* new TSS selector */ - int ext; /* task switch due to external event */ - uint32_t errcode; - int errcode_valid; /* push 'errcode' on the new stack */ - enum task_switch_reason reason; - struct vm_guest_paging paging; -}; - -struct vm_exit { - enum vm_exitcode exitcode; - int inst_length; /* 0 means unknown */ - uint64_t rip; - union { - struct vm_inout inout; - struct vm_inout_str inout_str; - struct { - uint64_t gpa; - int fault_type; - } paging; - struct { - uint64_t gpa; - uint64_t gla; - uint64_t cs_base; - int cs_d; /* CS.D */ - struct vm_guest_paging paging; - struct vie vie; - } inst_emul; - /* - * VMX specific payload. Used when there is no "better" - * exitcode to represent the VM-exit. - */ - struct { - int status; /* vmx inst status */ - /* - * 'exit_reason' and 'exit_qualification' are valid - * only if 'status' is zero. - */ - uint32_t exit_reason; - uint64_t exit_qualification; - /* - * 'inst_error' and 'inst_type' are valid - * only if 'status' is non-zero. - */ - int inst_type; - int inst_error; - } vmx; - /* - * SVM specific payload. - */ - struct { - uint64_t exitcode; - uint64_t exitinfo1; - uint64_t exitinfo2; - } svm; - struct { - uint32_t code; /* ecx value */ - uint64_t wval; - } msr; - struct { - int vcpu; - uint64_t rip; - } spinup_ap; - struct { - uint64_t rflags; - } hlt; - struct { - int vector; - } ioapic_eoi; - struct { - enum vm_suspend_how how; - } suspended; - struct vm_task_switch task_switch; - } u; -}; - -/* APIs to inject faults into the guest */ -void vm_inject_fault(void *vm, int vcpuid, int vector, int errcode_valid, - int errcode); - -static __inline void -vm_inject_ud(void *vm, int vcpuid) -{ - vm_inject_fault(vm, vcpuid, IDT_UD, 0, 0); -} - -static __inline void -vm_inject_gp(void *vm, int vcpuid) -{ - vm_inject_fault(vm, vcpuid, IDT_GP, 1, 0); -} - -static __inline void -vm_inject_ac(void *vm, int vcpuid, int errcode) -{ - vm_inject_fault(vm, vcpuid, IDT_AC, 1, errcode); -} - -static __inline void -vm_inject_ss(void *vm, int vcpuid, int errcode) -{ - vm_inject_fault(vm, vcpuid, IDT_SS, 1, errcode); -} - -void vm_inject_pf(void *vm, int vcpuid, int error_code, uint64_t cr2); - -int vm_restart_instruction(void *vm, int vcpuid); - -#endif /* _VMM_H_ */ diff --git a/vmm/amd/amdv.c b/vmm/amd/amdv.c deleted file mode 100644 index 3157e21..0000000 --- a/vmm/amd/amdv.c +++ /dev/null @@ -1,133 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include - -#include -#include "io/iommu.h" - -static int -amd_iommu_init(void) -{ - - printf("amd_iommu_init: not implemented\n"); - return (ENXIO); -} - -static void -amd_iommu_cleanup(void) -{ - - printf("amd_iommu_cleanup: not implemented\n"); -} - -static void -amd_iommu_enable(void) -{ - - printf("amd_iommu_enable: not implemented\n"); -} - -static void -amd_iommu_disable(void) -{ - - printf("amd_iommu_disable: not implemented\n"); -} - -static void * -amd_iommu_create_domain(vm_paddr_t maxaddr) -{ - - printf("amd_iommu_create_domain: not implemented\n"); - return (NULL); -} - -static void -amd_iommu_destroy_domain(void *domain) -{ - - printf("amd_iommu_destroy_domain: not implemented\n"); -} - -static uint64_t -amd_iommu_create_mapping(void *domain, vm_paddr_t gpa, vm_paddr_t hpa, - uint64_t len) -{ - - printf("amd_iommu_create_mapping: not implemented\n"); - return (0); -} - -static uint64_t -amd_iommu_remove_mapping(void *domain, vm_paddr_t gpa, uint64_t len) -{ - - printf("amd_iommu_remove_mapping: not implemented\n"); - return (0); -} - -static void -amd_iommu_add_device(void *domain, uint16_t rid) -{ - - printf("amd_iommu_add_device: not implemented\n"); -} - -static void -amd_iommu_remove_device(void *domain, uint16_t rid) -{ - - printf("amd_iommu_remove_device: not implemented\n"); -} - -static void -amd_iommu_invalidate_tlb(void *domain) -{ - - printf("amd_iommu_invalidate_tlb: not implemented\n"); -} - -struct iommu_ops iommu_ops_amd = { - amd_iommu_init, - amd_iommu_cleanup, - amd_iommu_enable, - amd_iommu_disable, - amd_iommu_create_domain, - amd_iommu_destroy_domain, - amd_iommu_create_mapping, - amd_iommu_remove_mapping, - amd_iommu_add_device, - amd_iommu_remove_device, - amd_iommu_invalidate_tlb, -}; diff --git a/vmm/amd/npt.c b/vmm/amd/npt.c deleted file mode 100644 index bebb4d5..0000000 --- a/vmm/amd/npt.c +++ /dev/null @@ -1,87 +0,0 @@ -/*- - * Copyright (c) 2013 Anish Gupta (akgupt3@gmail.com) - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice unmodified, this list of conditions, and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include - -#include -#include -#include - -#include - -#include "npt.h" - -SYSCTL_DECL(_hw_vmm); -SYSCTL_NODE(_hw_vmm, OID_AUTO, npt, CTLFLAG_RW, NULL, NULL); - -static int npt_flags; -SYSCTL_INT(_hw_vmm_npt, OID_AUTO, pmap_flags, CTLFLAG_RD, - &npt_flags, 0, NULL); - -#define NPT_IPIMASK 0xFF - -/* - * AMD nested page table init. - */ -int -svm_npt_init(int ipinum) -{ - int enable_superpage = 1; - - npt_flags = ipinum & NPT_IPIMASK; - TUNABLE_INT_FETCH("hw.vmm.npt.enable_superpage", &enable_superpage); - if (enable_superpage) - npt_flags |= PMAP_PDE_SUPERPAGE; - - return (0); -} - -static int -npt_pinit(pmap_t pmap) -{ - - return (pmap_pinit_type(pmap, PT_RVI, npt_flags)); -} - -struct vmspace * -svm_npt_alloc(vm_offset_t min, vm_offset_t max) -{ - - return (vmspace_alloc(min, max, npt_pinit)); -} - -void -svm_npt_free(struct vmspace *vmspace) -{ - - vmspace_free(vmspace); -} diff --git a/vmm/amd/npt.h b/vmm/amd/npt.h deleted file mode 100644 index 5966474..0000000 --- a/vmm/amd/npt.h +++ /dev/null @@ -1,36 +0,0 @@ -/*- - * Copyright (c) 2013 Anish Gupta (akgupt3@gmail.com) - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice unmodified, this list of conditions, and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#ifndef _SVM_NPT_H_ -#define _SVM_NPT_H_ - -int svm_npt_init(int ipinum); -struct vmspace *svm_npt_alloc(vm_offset_t min, vm_offset_t max); -void svm_npt_free(struct vmspace *vmspace); - -#endif /* _SVM_NPT_H_ */ diff --git a/vmm/amd/svm.c b/vmm/amd/svm.c deleted file mode 100644 index 20e8f76..0000000 --- a/vmm/amd/svm.c +++ /dev/null @@ -1,2259 +0,0 @@ -/*- - * Copyright (c) 2013, Anish Gupta (akgupt3@gmail.com) - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice unmodified, this list of conditions, and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "vmm_lapic.h" -#include "vmm_stat.h" -#include "vmm_ktr.h" -#include "vmm_ioport.h" -#include "vatpic.h" -#include "vlapic.h" -#include "vlapic_priv.h" - -#include "x86.h" -#include "vmcb.h" -#include "svm.h" -#include "svm_softc.h" -#include "svm_msr.h" -#include "npt.h" - -SYSCTL_DECL(_hw_vmm); -SYSCTL_NODE(_hw_vmm, OID_AUTO, svm, CTLFLAG_RW, NULL, NULL); - -/* - * SVM CPUID function 0x8000_000A, edx bit decoding. - */ -#define AMD_CPUID_SVM_NP BIT(0) /* Nested paging or RVI */ -#define AMD_CPUID_SVM_LBR BIT(1) /* Last branch virtualization */ -#define AMD_CPUID_SVM_SVML BIT(2) /* SVM lock */ -#define AMD_CPUID_SVM_NRIP_SAVE BIT(3) /* Next RIP is saved */ -#define AMD_CPUID_SVM_TSC_RATE BIT(4) /* TSC rate control. */ -#define AMD_CPUID_SVM_VMCB_CLEAN BIT(5) /* VMCB state caching */ -#define AMD_CPUID_SVM_FLUSH_BY_ASID BIT(6) /* Flush by ASID */ -#define AMD_CPUID_SVM_DECODE_ASSIST BIT(7) /* Decode assist */ -#define AMD_CPUID_SVM_PAUSE_INC BIT(10) /* Pause intercept filter. */ -#define AMD_CPUID_SVM_PAUSE_FTH BIT(12) /* Pause filter threshold */ -#define AMD_CPUID_SVM_AVIC BIT(13) /* AVIC present */ - -#define VMCB_CACHE_DEFAULT (VMCB_CACHE_ASID | \ - VMCB_CACHE_IOPM | \ - VMCB_CACHE_I | \ - VMCB_CACHE_TPR | \ - VMCB_CACHE_CR2 | \ - VMCB_CACHE_CR | \ - VMCB_CACHE_DT | \ - VMCB_CACHE_SEG | \ - VMCB_CACHE_NP) - -static uint32_t vmcb_clean = VMCB_CACHE_DEFAULT; -SYSCTL_INT(_hw_vmm_svm, OID_AUTO, vmcb_clean, CTLFLAG_RDTUN, &vmcb_clean, - 0, NULL); - -static MALLOC_DEFINE(M_SVM, "svm", "svm"); -static MALLOC_DEFINE(M_SVM_VLAPIC, "svm-vlapic", "svm-vlapic"); - -/* Per-CPU context area. */ -extern struct pcpu __pcpu[]; - -static uint32_t svm_feature; /* AMD SVM features. */ -SYSCTL_UINT(_hw_vmm_svm, OID_AUTO, features, CTLFLAG_RD, &svm_feature, 0, - "SVM features advertised by CPUID.8000000AH:EDX"); - -static int disable_npf_assist; -SYSCTL_INT(_hw_vmm_svm, OID_AUTO, disable_npf_assist, CTLFLAG_RWTUN, - &disable_npf_assist, 0, NULL); - -/* Maximum ASIDs supported by the processor */ -static uint32_t nasid; -SYSCTL_UINT(_hw_vmm_svm, OID_AUTO, num_asids, CTLFLAG_RD, &nasid, 0, - "Number of ASIDs supported by this processor"); - -/* Current ASID generation for each host cpu */ -static struct asid asid[MAXCPU]; - -/* - * SVM host state saved area of size 4KB for each core. - */ -static uint8_t hsave[MAXCPU][PAGE_SIZE] __aligned(PAGE_SIZE); - -static VMM_STAT_AMD(VCPU_EXITINTINFO, "VM exits during event delivery"); -static VMM_STAT_AMD(VCPU_INTINFO_INJECTED, "Events pending at VM entry"); -static VMM_STAT_AMD(VMEXIT_VINTR, "VM exits due to interrupt window"); - -static int svm_setreg(void *arg, int vcpu, int ident, uint64_t val); - -static __inline int -flush_by_asid(void) -{ - - return (svm_feature & AMD_CPUID_SVM_FLUSH_BY_ASID); -} - -static __inline int -decode_assist(void) -{ - - return (svm_feature & AMD_CPUID_SVM_DECODE_ASSIST); -} - -static void -svm_disable(void *arg __unused) -{ - uint64_t efer; - - efer = rdmsr(MSR_EFER); - efer &= ~EFER_SVM; - wrmsr(MSR_EFER, efer); -} - -/* - * Disable SVM on all CPUs. - */ -static int -svm_cleanup(void) -{ - - smp_rendezvous(NULL, svm_disable, NULL, NULL); - return (0); -} - -/* - * Verify that all the features required by bhyve are available. - */ -static int -check_svm_features(void) -{ - u_int regs[4]; - - /* CPUID Fn8000_000A is for SVM */ - do_cpuid(0x8000000A, regs); - svm_feature = regs[3]; - - nasid = regs[1]; - KASSERT(nasid > 1, ("Insufficient ASIDs for guests: %#x", nasid)); - - /* bhyve requires the Nested Paging feature */ - if (!(svm_feature & AMD_CPUID_SVM_NP)) { - printf("SVM: Nested Paging feature not available.\n"); - return (ENXIO); - } - - /* bhyve requires the NRIP Save feature */ - if (!(svm_feature & AMD_CPUID_SVM_NRIP_SAVE)) { - printf("SVM: NRIP Save feature not available.\n"); - return (ENXIO); - } - - return (0); -} - -static void -svm_enable(void *arg __unused) -{ - uint64_t efer; - - efer = rdmsr(MSR_EFER); - efer |= EFER_SVM; - wrmsr(MSR_EFER, efer); - - wrmsr(MSR_VM_HSAVE_PA, vtophys(hsave[curcpu])); -} - -/* - * Return 1 if SVM is enabled on this processor and 0 otherwise. - */ -static int -svm_available(void) -{ - uint64_t msr; - - /* Section 15.4 Enabling SVM from APM2. */ - if ((amd_feature2 & AMDID2_SVM) == 0) { - printf("SVM: not available.\n"); - return (0); - } - - msr = rdmsr(MSR_VM_CR); - if ((msr & VM_CR_SVMDIS) != 0) { - printf("SVM: disabled by BIOS.\n"); - return (0); - } - - return (1); -} - -static int -svm_init(int ipinum) -{ - int error, cpu; - - if (!svm_available()) - return (ENXIO); - - error = check_svm_features(); - if (error) - return (error); - - vmcb_clean &= VMCB_CACHE_DEFAULT; - - for (cpu = 0; cpu < MAXCPU; cpu++) { - /* - * Initialize the host ASIDs to their "highest" valid values. - * - * The next ASID allocation will rollover both 'gen' and 'num' - * and start off the sequence at {1,1}. - */ - asid[cpu].gen = ~0UL; - asid[cpu].num = nasid - 1; - } - - svm_msr_init(); - svm_npt_init(ipinum); - - /* Enable SVM on all CPUs */ - smp_rendezvous(NULL, svm_enable, NULL, NULL); - - return (0); -} - -static void -svm_restore(void) -{ - - svm_enable(NULL); -} - -/* Pentium compatible MSRs */ -#define MSR_PENTIUM_START 0 -#define MSR_PENTIUM_END 0x1FFF -/* AMD 6th generation and Intel compatible MSRs */ -#define MSR_AMD6TH_START 0xC0000000UL -#define MSR_AMD6TH_END 0xC0001FFFUL -/* AMD 7th and 8th generation compatible MSRs */ -#define MSR_AMD7TH_START 0xC0010000UL -#define MSR_AMD7TH_END 0xC0011FFFUL - -/* - * Get the index and bit position for a MSR in permission bitmap. - * Two bits are used for each MSR: lower bit for read and higher bit for write. - */ -static int -svm_msr_index(uint64_t msr, int *index, int *bit) -{ - uint32_t base, off; - - *index = -1; - *bit = (msr % 4) * 2; - base = 0; - - if (msr >= MSR_PENTIUM_START && msr <= MSR_PENTIUM_END) { - *index = msr / 4; - return (0); - } - - base += (MSR_PENTIUM_END - MSR_PENTIUM_START + 1); - if (msr >= MSR_AMD6TH_START && msr <= MSR_AMD6TH_END) { - off = (msr - MSR_AMD6TH_START); - *index = (off + base) / 4; - return (0); - } - - base += (MSR_AMD6TH_END - MSR_AMD6TH_START + 1); - if (msr >= MSR_AMD7TH_START && msr <= MSR_AMD7TH_END) { - off = (msr - MSR_AMD7TH_START); - *index = (off + base) / 4; - return (0); - } - - return (EINVAL); -} - -/* - * Allow vcpu to read or write the 'msr' without trapping into the hypervisor. - */ -static void -svm_msr_perm(uint8_t *perm_bitmap, uint64_t msr, bool read, bool write) -{ - int index, bit, error; - - error = svm_msr_index(msr, &index, &bit); - KASSERT(error == 0, ("%s: invalid msr %#lx", __func__, msr)); - KASSERT(index >= 0 && index < SVM_MSR_BITMAP_SIZE, - ("%s: invalid index %d for msr %#lx", __func__, index, msr)); - KASSERT(bit >= 0 && bit <= 6, ("%s: invalid bit position %d " - "msr %#lx", __func__, bit, msr)); - - if (read) - perm_bitmap[index] &= ~(1UL << bit); - - if (write) - perm_bitmap[index] &= ~(2UL << bit); -} - -static void -svm_msr_rw_ok(uint8_t *perm_bitmap, uint64_t msr) -{ - - svm_msr_perm(perm_bitmap, msr, true, true); -} - -static void -svm_msr_rd_ok(uint8_t *perm_bitmap, uint64_t msr) -{ - - svm_msr_perm(perm_bitmap, msr, true, false); -} - -static __inline int -svm_get_intercept(struct svm_softc *sc, int vcpu, int idx, uint32_t bitmask) -{ - struct vmcb_ctrl *ctrl; - - KASSERT(idx >=0 && idx < 5, ("invalid intercept index %d", idx)); - - ctrl = svm_get_vmcb_ctrl(sc, vcpu); - return (ctrl->intercept[idx] & bitmask ? 1 : 0); -} - -static __inline void -svm_set_intercept(struct svm_softc *sc, int vcpu, int idx, uint32_t bitmask, - int enabled) -{ - struct vmcb_ctrl *ctrl; - uint32_t oldval; - - KASSERT(idx >=0 && idx < 5, ("invalid intercept index %d", idx)); - - ctrl = svm_get_vmcb_ctrl(sc, vcpu); - oldval = ctrl->intercept[idx]; - - if (enabled) - ctrl->intercept[idx] |= bitmask; - else - ctrl->intercept[idx] &= ~bitmask; - - if (ctrl->intercept[idx] != oldval) { - svm_set_dirty(sc, vcpu, VMCB_CACHE_I); - VCPU_CTR3(sc->vm, vcpu, "intercept[%d] modified " - "from %#x to %#x", idx, oldval, ctrl->intercept[idx]); - } -} - -static __inline void -svm_disable_intercept(struct svm_softc *sc, int vcpu, int off, uint32_t bitmask) -{ - - svm_set_intercept(sc, vcpu, off, bitmask, 0); -} - -static __inline void -svm_enable_intercept(struct svm_softc *sc, int vcpu, int off, uint32_t bitmask) -{ - - svm_set_intercept(sc, vcpu, off, bitmask, 1); -} - -static void -vmcb_init(struct svm_softc *sc, int vcpu, uint64_t iopm_base_pa, - uint64_t msrpm_base_pa, uint64_t np_pml4) -{ - struct vmcb_ctrl *ctrl; - struct vmcb_state *state; - uint32_t mask; - int n; - - ctrl = svm_get_vmcb_ctrl(sc, vcpu); - state = svm_get_vmcb_state(sc, vcpu); - - ctrl->iopm_base_pa = iopm_base_pa; - ctrl->msrpm_base_pa = msrpm_base_pa; - - /* Enable nested paging */ - ctrl->np_enable = 1; - ctrl->n_cr3 = np_pml4; - - /* - * Intercept accesses to the control registers that are not shadowed - * in the VMCB - i.e. all except cr0, cr2, cr3, cr4 and cr8. - */ - for (n = 0; n < 16; n++) { - mask = (BIT(n) << 16) | BIT(n); - if (n == 0 || n == 2 || n == 3 || n == 4 || n == 8) - svm_disable_intercept(sc, vcpu, VMCB_CR_INTCPT, mask); - else - svm_enable_intercept(sc, vcpu, VMCB_CR_INTCPT, mask); - } - - - /* - * Intercept everything when tracing guest exceptions otherwise - * just intercept machine check exception. - */ - if (vcpu_trace_exceptions(sc->vm, vcpu)) { - for (n = 0; n < 32; n++) { - /* - * Skip unimplemented vectors in the exception bitmap. - */ - if (n == 2 || n == 9) { - continue; - } - svm_enable_intercept(sc, vcpu, VMCB_EXC_INTCPT, BIT(n)); - } - } else { - svm_enable_intercept(sc, vcpu, VMCB_EXC_INTCPT, BIT(IDT_MC)); - } - - /* Intercept various events (for e.g. I/O, MSR and CPUID accesses) */ - svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_IO); - svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_MSR); - svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_CPUID); - svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_INTR); - svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_INIT); - svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_NMI); - svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_SMI); - svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_SHUTDOWN); - svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, - VMCB_INTCPT_FERR_FREEZE); - - svm_enable_intercept(sc, vcpu, VMCB_CTRL2_INTCPT, VMCB_INTCPT_MONITOR); - svm_enable_intercept(sc, vcpu, VMCB_CTRL2_INTCPT, VMCB_INTCPT_MWAIT); - - /* - * From section "Canonicalization and Consistency Checks" in APMv2 - * the VMRUN intercept bit must be set to pass the consistency check. - */ - svm_enable_intercept(sc, vcpu, VMCB_CTRL2_INTCPT, VMCB_INTCPT_VMRUN); - - /* - * The ASID will be set to a non-zero value just before VMRUN. - */ - ctrl->asid = 0; - - /* - * Section 15.21.1, Interrupt Masking in EFLAGS - * Section 15.21.2, Virtualizing APIC.TPR - * - * This must be set for %rflag and %cr8 isolation of guest and host. - */ - ctrl->v_intr_masking = 1; - - /* Enable Last Branch Record aka LBR for debugging */ - ctrl->lbr_virt_en = 1; - state->dbgctl = BIT(0); - - /* EFER_SVM must always be set when the guest is executing */ - state->efer = EFER_SVM; - - /* Set up the PAT to power-on state */ - state->g_pat = PAT_VALUE(0, PAT_WRITE_BACK) | - PAT_VALUE(1, PAT_WRITE_THROUGH) | - PAT_VALUE(2, PAT_UNCACHED) | - PAT_VALUE(3, PAT_UNCACHEABLE) | - PAT_VALUE(4, PAT_WRITE_BACK) | - PAT_VALUE(5, PAT_WRITE_THROUGH) | - PAT_VALUE(6, PAT_UNCACHED) | - PAT_VALUE(7, PAT_UNCACHEABLE); -} - -/* - * Initialize a virtual machine. - */ -static void * -svm_vminit(struct vm *vm, pmap_t pmap) -{ - struct svm_softc *svm_sc; - struct svm_vcpu *vcpu; - vm_paddr_t msrpm_pa, iopm_pa, pml4_pa; - int i; - - svm_sc = malloc(sizeof (struct svm_softc), M_SVM, M_WAITOK | M_ZERO); - svm_sc->vm = vm; - svm_sc->nptp = (vm_offset_t)vtophys(pmap->pm_pml4); - - /* - * Intercept read and write accesses to all MSRs. - */ - memset(svm_sc->msr_bitmap, 0xFF, sizeof(svm_sc->msr_bitmap)); - - /* - * Access to the following MSRs is redirected to the VMCB when the - * guest is executing. Therefore it is safe to allow the guest to - * read/write these MSRs directly without hypervisor involvement. - */ - svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_GSBASE); - svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_FSBASE); - svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_KGSBASE); - - svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_STAR); - svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_LSTAR); - svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_CSTAR); - svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_SF_MASK); - svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_SYSENTER_CS_MSR); - svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_SYSENTER_ESP_MSR); - svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_SYSENTER_EIP_MSR); - svm_msr_rw_ok(svm_sc->msr_bitmap, MSR_PAT); - - svm_msr_rd_ok(svm_sc->msr_bitmap, MSR_TSC); - - /* - * Intercept writes to make sure that the EFER_SVM bit is not cleared. - */ - svm_msr_rd_ok(svm_sc->msr_bitmap, MSR_EFER); - - /* Intercept access to all I/O ports. */ - memset(svm_sc->iopm_bitmap, 0xFF, sizeof(svm_sc->iopm_bitmap)); - - iopm_pa = vtophys(svm_sc->iopm_bitmap); - msrpm_pa = vtophys(svm_sc->msr_bitmap); - pml4_pa = svm_sc->nptp; - for (i = 0; i < VM_MAXCPU; i++) { - vcpu = svm_get_vcpu(svm_sc, i); - vcpu->nextrip = ~0; - vcpu->lastcpu = NOCPU; - vcpu->vmcb_pa = vtophys(&vcpu->vmcb); - vmcb_init(svm_sc, i, iopm_pa, msrpm_pa, pml4_pa); - svm_msr_guest_init(svm_sc, i); - } - return (svm_sc); -} - -/* - * Collateral for a generic SVM VM-exit. - */ -static void -vm_exit_svm(struct vm_exit *vme, uint64_t code, uint64_t info1, uint64_t info2) -{ - - vme->exitcode = VM_EXITCODE_SVM; - vme->u.svm.exitcode = code; - vme->u.svm.exitinfo1 = info1; - vme->u.svm.exitinfo2 = info2; -} - -static int -svm_cpl(struct vmcb_state *state) -{ - - /* - * From APMv2: - * "Retrieve the CPL from the CPL field in the VMCB, not - * from any segment DPL" - */ - return (state->cpl); -} - -static enum vm_cpu_mode -svm_vcpu_mode(struct vmcb *vmcb) -{ - struct vmcb_segment seg; - struct vmcb_state *state; - int error; - - state = &vmcb->state; - - if (state->efer & EFER_LMA) { - error = vmcb_seg(vmcb, VM_REG_GUEST_CS, &seg); - KASSERT(error == 0, ("%s: vmcb_seg(cs) error %d", __func__, - error)); - - /* - * Section 4.8.1 for APM2, check if Code Segment has - * Long attribute set in descriptor. - */ - if (seg.attrib & VMCB_CS_ATTRIB_L) - return (CPU_MODE_64BIT); - else - return (CPU_MODE_COMPATIBILITY); - } else if (state->cr0 & CR0_PE) { - return (CPU_MODE_PROTECTED); - } else { - return (CPU_MODE_REAL); - } -} - -static enum vm_paging_mode -svm_paging_mode(uint64_t cr0, uint64_t cr4, uint64_t efer) -{ - - if ((cr0 & CR0_PG) == 0) - return (PAGING_MODE_FLAT); - if ((cr4 & CR4_PAE) == 0) - return (PAGING_MODE_32); - if (efer & EFER_LME) - return (PAGING_MODE_64); - else - return (PAGING_MODE_PAE); -} - -/* - * ins/outs utility routines - */ -static uint64_t -svm_inout_str_index(struct svm_regctx *regs, int in) -{ - uint64_t val; - - val = in ? regs->sctx_rdi : regs->sctx_rsi; - - return (val); -} - -static uint64_t -svm_inout_str_count(struct svm_regctx *regs, int rep) -{ - uint64_t val; - - val = rep ? regs->sctx_rcx : 1; - - return (val); -} - -static void -svm_inout_str_seginfo(struct svm_softc *svm_sc, int vcpu, int64_t info1, - int in, struct vm_inout_str *vis) -{ - int error, s; - - if (in) { - vis->seg_name = VM_REG_GUEST_ES; - } else { - /* The segment field has standard encoding */ - s = (info1 >> 10) & 0x7; - vis->seg_name = vm_segment_name(s); - } - - error = vmcb_getdesc(svm_sc, vcpu, vis->seg_name, &vis->seg_desc); - KASSERT(error == 0, ("%s: svm_getdesc error %d", __func__, error)); -} - -static int -svm_inout_str_addrsize(uint64_t info1) -{ - uint32_t size; - - size = (info1 >> 7) & 0x7; - switch (size) { - case 1: - return (2); /* 16 bit */ - case 2: - return (4); /* 32 bit */ - case 4: - return (8); /* 64 bit */ - default: - panic("%s: invalid size encoding %d", __func__, size); - } -} - -static void -svm_paging_info(struct vmcb *vmcb, struct vm_guest_paging *paging) -{ - struct vmcb_state *state; - - state = &vmcb->state; - paging->cr3 = state->cr3; - paging->cpl = svm_cpl(state); - paging->cpu_mode = svm_vcpu_mode(vmcb); - paging->paging_mode = svm_paging_mode(state->cr0, state->cr4, - state->efer); -} - -#define UNHANDLED 0 - -/* - * Handle guest I/O intercept. - */ -static int -svm_handle_io(struct svm_softc *svm_sc, int vcpu, struct vm_exit *vmexit) -{ - struct vmcb_ctrl *ctrl; - struct vmcb_state *state; - struct svm_regctx *regs; - struct vm_inout_str *vis; - uint64_t info1; - int inout_string; - - state = svm_get_vmcb_state(svm_sc, vcpu); - ctrl = svm_get_vmcb_ctrl(svm_sc, vcpu); - regs = svm_get_guest_regctx(svm_sc, vcpu); - - info1 = ctrl->exitinfo1; - inout_string = info1 & BIT(2) ? 1 : 0; - - /* - * The effective segment number in EXITINFO1[12:10] is populated - * only if the processor has the DecodeAssist capability. - * - * XXX this is not specified explicitly in APMv2 but can be verified - * empirically. - */ - if (inout_string && !decode_assist()) - return (UNHANDLED); - - vmexit->exitcode = VM_EXITCODE_INOUT; - vmexit->u.inout.in = (info1 & BIT(0)) ? 1 : 0; - vmexit->u.inout.string = inout_string; - vmexit->u.inout.rep = (info1 & BIT(3)) ? 1 : 0; - vmexit->u.inout.bytes = (info1 >> 4) & 0x7; - vmexit->u.inout.port = (uint16_t)(info1 >> 16); - vmexit->u.inout.eax = (uint32_t)(state->rax); - - if (inout_string) { - vmexit->exitcode = VM_EXITCODE_INOUT_STR; - vis = &vmexit->u.inout_str; - svm_paging_info(svm_get_vmcb(svm_sc, vcpu), &vis->paging); - vis->rflags = state->rflags; - vis->cr0 = state->cr0; - vis->index = svm_inout_str_index(regs, vmexit->u.inout.in); - vis->count = svm_inout_str_count(regs, vmexit->u.inout.rep); - vis->addrsize = svm_inout_str_addrsize(info1); - svm_inout_str_seginfo(svm_sc, vcpu, info1, - vmexit->u.inout.in, vis); - } - - return (UNHANDLED); -} - -static int -npf_fault_type(uint64_t exitinfo1) -{ - - if (exitinfo1 & VMCB_NPF_INFO1_W) - return (VM_PROT_WRITE); - else if (exitinfo1 & VMCB_NPF_INFO1_ID) - return (VM_PROT_EXECUTE); - else - return (VM_PROT_READ); -} - -static bool -svm_npf_emul_fault(uint64_t exitinfo1) -{ - - if (exitinfo1 & VMCB_NPF_INFO1_ID) { - return (false); - } - - if (exitinfo1 & VMCB_NPF_INFO1_GPT) { - return (false); - } - - if ((exitinfo1 & VMCB_NPF_INFO1_GPA) == 0) { - return (false); - } - - return (true); -} - -static void -svm_handle_inst_emul(struct vmcb *vmcb, uint64_t gpa, struct vm_exit *vmexit) -{ - struct vm_guest_paging *paging; - struct vmcb_segment seg; - struct vmcb_ctrl *ctrl; - char *inst_bytes; - int error, inst_len; - - ctrl = &vmcb->ctrl; - paging = &vmexit->u.inst_emul.paging; - - vmexit->exitcode = VM_EXITCODE_INST_EMUL; - vmexit->u.inst_emul.gpa = gpa; - vmexit->u.inst_emul.gla = VIE_INVALID_GLA; - svm_paging_info(vmcb, paging); - - error = vmcb_seg(vmcb, VM_REG_GUEST_CS, &seg); - KASSERT(error == 0, ("%s: vmcb_seg(CS) error %d", __func__, error)); - - switch(paging->cpu_mode) { - case CPU_MODE_REAL: - vmexit->u.inst_emul.cs_base = seg.base; - vmexit->u.inst_emul.cs_d = 0; - break; - case CPU_MODE_PROTECTED: - case CPU_MODE_COMPATIBILITY: - vmexit->u.inst_emul.cs_base = seg.base; - - /* - * Section 4.8.1 of APM2, Default Operand Size or D bit. - */ - vmexit->u.inst_emul.cs_d = (seg.attrib & VMCB_CS_ATTRIB_D) ? - 1 : 0; - break; - default: - vmexit->u.inst_emul.cs_base = 0; - vmexit->u.inst_emul.cs_d = 0; - break; - } - - /* - * Copy the instruction bytes into 'vie' if available. - */ - if (decode_assist() && !disable_npf_assist) { - inst_len = ctrl->inst_len; - inst_bytes = ctrl->inst_bytes; - } else { - inst_len = 0; - inst_bytes = NULL; - } - vie_init(&vmexit->u.inst_emul.vie, inst_bytes, inst_len); -} - -#ifdef KTR -static const char * -intrtype_to_str(int intr_type) -{ - switch (intr_type) { - case VMCB_EVENTINJ_TYPE_INTR: - return ("hwintr"); - case VMCB_EVENTINJ_TYPE_NMI: - return ("nmi"); - case VMCB_EVENTINJ_TYPE_INTn: - return ("swintr"); - case VMCB_EVENTINJ_TYPE_EXCEPTION: - return ("exception"); - default: - panic("%s: unknown intr_type %d", __func__, intr_type); - } -} -#endif - -/* - * Inject an event to vcpu as described in section 15.20, "Event injection". - */ -static void -svm_eventinject(struct svm_softc *sc, int vcpu, int intr_type, int vector, - uint32_t error, bool ec_valid) -{ - struct vmcb_ctrl *ctrl; - - ctrl = svm_get_vmcb_ctrl(sc, vcpu); - - KASSERT((ctrl->eventinj & VMCB_EVENTINJ_VALID) == 0, - ("%s: event already pending %#lx", __func__, ctrl->eventinj)); - - KASSERT(vector >=0 && vector <= 255, ("%s: invalid vector %d", - __func__, vector)); - - switch (intr_type) { - case VMCB_EVENTINJ_TYPE_INTR: - case VMCB_EVENTINJ_TYPE_NMI: - case VMCB_EVENTINJ_TYPE_INTn: - break; - case VMCB_EVENTINJ_TYPE_EXCEPTION: - if (vector >= 0 && vector <= 31 && vector != 2) - break; - /* FALLTHROUGH */ - default: - panic("%s: invalid intr_type/vector: %d/%d", __func__, - intr_type, vector); - } - ctrl->eventinj = vector | (intr_type << 8) | VMCB_EVENTINJ_VALID; - if (ec_valid) { - ctrl->eventinj |= VMCB_EVENTINJ_EC_VALID; - ctrl->eventinj |= (uint64_t)error << 32; - VCPU_CTR3(sc->vm, vcpu, "Injecting %s at vector %d errcode %#x", - intrtype_to_str(intr_type), vector, error); - } else { - VCPU_CTR2(sc->vm, vcpu, "Injecting %s at vector %d", - intrtype_to_str(intr_type), vector); - } -} - -static void -svm_update_virqinfo(struct svm_softc *sc, int vcpu) -{ - struct vm *vm; - struct vlapic *vlapic; - struct vmcb_ctrl *ctrl; - int pending; - - vm = sc->vm; - vlapic = vm_lapic(vm, vcpu); - ctrl = svm_get_vmcb_ctrl(sc, vcpu); - - /* Update %cr8 in the emulated vlapic */ - vlapic_set_cr8(vlapic, ctrl->v_tpr); - - /* - * If V_IRQ indicates that the interrupt injection attempted on then - * last VMRUN was successful then update the vlapic accordingly. - */ - if (ctrl->v_intr_vector != 0) { - pending = ctrl->v_irq; - KASSERT(ctrl->v_intr_vector >= 16, ("%s: invalid " - "v_intr_vector %d", __func__, ctrl->v_intr_vector)); - KASSERT(!ctrl->v_ign_tpr, ("%s: invalid v_ign_tpr", __func__)); - VCPU_CTR2(vm, vcpu, "v_intr_vector %d %s", ctrl->v_intr_vector, - pending ? "pending" : "accepted"); - if (!pending) - vlapic_intr_accepted(vlapic, ctrl->v_intr_vector); - } -} - -static void -svm_save_intinfo(struct svm_softc *svm_sc, int vcpu) -{ - struct vmcb_ctrl *ctrl; - uint64_t intinfo; - - ctrl = svm_get_vmcb_ctrl(svm_sc, vcpu); - intinfo = ctrl->exitintinfo; - if (!VMCB_EXITINTINFO_VALID(intinfo)) - return; - - /* - * From APMv2, Section "Intercepts during IDT interrupt delivery" - * - * If a #VMEXIT happened during event delivery then record the event - * that was being delivered. - */ - VCPU_CTR2(svm_sc->vm, vcpu, "SVM:Pending INTINFO(0x%lx), vector=%d.\n", - intinfo, VMCB_EXITINTINFO_VECTOR(intinfo)); - vmm_stat_incr(svm_sc->vm, vcpu, VCPU_EXITINTINFO, 1); - vm_exit_intinfo(svm_sc->vm, vcpu, intinfo); -} - -static __inline int -vintr_intercept_enabled(struct svm_softc *sc, int vcpu) -{ - - return (svm_get_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, - VMCB_INTCPT_VINTR)); -} - -static __inline void -enable_intr_window_exiting(struct svm_softc *sc, int vcpu) -{ - struct vmcb_ctrl *ctrl; - - ctrl = svm_get_vmcb_ctrl(sc, vcpu); - - if (ctrl->v_irq && ctrl->v_intr_vector == 0) { - KASSERT(ctrl->v_ign_tpr, ("%s: invalid v_ign_tpr", __func__)); - KASSERT(vintr_intercept_enabled(sc, vcpu), - ("%s: vintr intercept should be enabled", __func__)); - return; - } - - VCPU_CTR0(sc->vm, vcpu, "Enable intr window exiting"); - ctrl->v_irq = 1; - ctrl->v_ign_tpr = 1; - ctrl->v_intr_vector = 0; - svm_set_dirty(sc, vcpu, VMCB_CACHE_TPR); - svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_VINTR); -} - -static __inline void -disable_intr_window_exiting(struct svm_softc *sc, int vcpu) -{ - struct vmcb_ctrl *ctrl; - - ctrl = svm_get_vmcb_ctrl(sc, vcpu); - - if (!ctrl->v_irq && ctrl->v_intr_vector == 0) { - KASSERT(!vintr_intercept_enabled(sc, vcpu), - ("%s: vintr intercept should be disabled", __func__)); - return; - } - -#ifdef KTR - if (ctrl->v_intr_vector == 0) - VCPU_CTR0(sc->vm, vcpu, "Disable intr window exiting"); - else - VCPU_CTR0(sc->vm, vcpu, "Clearing V_IRQ interrupt injection"); -#endif - ctrl->v_irq = 0; - ctrl->v_intr_vector = 0; - svm_set_dirty(sc, vcpu, VMCB_CACHE_TPR); - svm_disable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_VINTR); -} - -static int -svm_modify_intr_shadow(struct svm_softc *sc, int vcpu, uint64_t val) -{ - struct vmcb_ctrl *ctrl; - int oldval, newval; - - ctrl = svm_get_vmcb_ctrl(sc, vcpu); - oldval = ctrl->intr_shadow; - newval = val ? 1 : 0; - if (newval != oldval) { - ctrl->intr_shadow = newval; - VCPU_CTR1(sc->vm, vcpu, "Setting intr_shadow to %d", newval); - } - return (0); -} - -static int -svm_get_intr_shadow(struct svm_softc *sc, int vcpu, uint64_t *val) -{ - struct vmcb_ctrl *ctrl; - - ctrl = svm_get_vmcb_ctrl(sc, vcpu); - *val = ctrl->intr_shadow; - return (0); -} - -/* - * Once an NMI is injected it blocks delivery of further NMIs until the handler - * executes an IRET. The IRET intercept is enabled when an NMI is injected to - * to track when the vcpu is done handling the NMI. - */ -static int -nmi_blocked(struct svm_softc *sc, int vcpu) -{ - int blocked; - - blocked = svm_get_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, - VMCB_INTCPT_IRET); - return (blocked); -} - -static void -enable_nmi_blocking(struct svm_softc *sc, int vcpu) -{ - - KASSERT(!nmi_blocked(sc, vcpu), ("vNMI already blocked")); - VCPU_CTR0(sc->vm, vcpu, "vNMI blocking enabled"); - svm_enable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_IRET); -} - -static void -clear_nmi_blocking(struct svm_softc *sc, int vcpu) -{ - int error; - - KASSERT(nmi_blocked(sc, vcpu), ("vNMI already unblocked")); - VCPU_CTR0(sc->vm, vcpu, "vNMI blocking cleared"); - /* - * When the IRET intercept is cleared the vcpu will attempt to execute - * the "iret" when it runs next. However, it is possible to inject - * another NMI into the vcpu before the "iret" has actually executed. - * - * For e.g. if the "iret" encounters a #NPF when accessing the stack - * it will trap back into the hypervisor. If an NMI is pending for - * the vcpu it will be injected into the guest. - * - * XXX this needs to be fixed - */ - svm_disable_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, VMCB_INTCPT_IRET); - - /* - * Set 'intr_shadow' to prevent an NMI from being injected on the - * immediate VMRUN. - */ - error = svm_modify_intr_shadow(sc, vcpu, 1); - KASSERT(!error, ("%s: error %d setting intr_shadow", __func__, error)); -} - -#define EFER_MBZ_BITS 0xFFFFFFFFFFFF0200UL - -static int -svm_write_efer(struct svm_softc *sc, int vcpu, uint64_t newval, bool *retu) -{ - struct vm_exit *vme; - struct vmcb_state *state; - uint64_t changed, lma, oldval; - int error; - - state = svm_get_vmcb_state(sc, vcpu); - - oldval = state->efer; - VCPU_CTR2(sc->vm, vcpu, "wrmsr(efer) %#lx/%#lx", oldval, newval); - - newval &= ~0xFE; /* clear the Read-As-Zero (RAZ) bits */ - changed = oldval ^ newval; - - if (newval & EFER_MBZ_BITS) - goto gpf; - - /* APMv2 Table 14-5 "Long-Mode Consistency Checks" */ - if (changed & EFER_LME) { - if (state->cr0 & CR0_PG) - goto gpf; - } - - /* EFER.LMA = EFER.LME & CR0.PG */ - if ((newval & EFER_LME) != 0 && (state->cr0 & CR0_PG) != 0) - lma = EFER_LMA; - else - lma = 0; - - if ((newval & EFER_LMA) != lma) - goto gpf; - - if (newval & EFER_NXE) { - if (!vm_cpuid_capability(sc->vm, vcpu, VCC_NO_EXECUTE)) - goto gpf; - } - - /* - * XXX bhyve does not enforce segment limits in 64-bit mode. Until - * this is fixed flag guest attempt to set EFER_LMSLE as an error. - */ - if (newval & EFER_LMSLE) { - vme = vm_exitinfo(sc->vm, vcpu); - vm_exit_svm(vme, VMCB_EXIT_MSR, 1, 0); - *retu = true; - return (0); - } - - if (newval & EFER_FFXSR) { - if (!vm_cpuid_capability(sc->vm, vcpu, VCC_FFXSR)) - goto gpf; - } - - if (newval & EFER_TCE) { - if (!vm_cpuid_capability(sc->vm, vcpu, VCC_TCE)) - goto gpf; - } - - error = svm_setreg(sc, vcpu, VM_REG_GUEST_EFER, newval); - KASSERT(error == 0, ("%s: error %d updating efer", __func__, error)); - return (0); -gpf: - vm_inject_gp(sc->vm, vcpu); - return (0); -} - -static int -emulate_wrmsr(struct svm_softc *sc, int vcpu, u_int num, uint64_t val, - bool *retu) -{ - int error; - - if (lapic_msr(num)) - error = lapic_wrmsr(sc->vm, vcpu, num, val, retu); - else if (num == MSR_EFER) - error = svm_write_efer(sc, vcpu, val, retu); - else - error = svm_wrmsr(sc, vcpu, num, val, retu); - - return (error); -} - -static int -emulate_rdmsr(struct svm_softc *sc, int vcpu, u_int num, bool *retu) -{ - struct vmcb_state *state; - struct svm_regctx *ctx; - uint64_t result; - int error; - - if (lapic_msr(num)) - error = lapic_rdmsr(sc->vm, vcpu, num, &result, retu); - else - error = svm_rdmsr(sc, vcpu, num, &result, retu); - - if (error == 0) { - state = svm_get_vmcb_state(sc, vcpu); - ctx = svm_get_guest_regctx(sc, vcpu); - state->rax = result & 0xffffffff; - ctx->sctx_rdx = result >> 32; - } - - return (error); -} - -#ifdef KTR -static const char * -exit_reason_to_str(uint64_t reason) -{ - static char reasonbuf[32]; - - switch (reason) { - case VMCB_EXIT_INVALID: - return ("invalvmcb"); - case VMCB_EXIT_SHUTDOWN: - return ("shutdown"); - case VMCB_EXIT_NPF: - return ("nptfault"); - case VMCB_EXIT_PAUSE: - return ("pause"); - case VMCB_EXIT_HLT: - return ("hlt"); - case VMCB_EXIT_CPUID: - return ("cpuid"); - case VMCB_EXIT_IO: - return ("inout"); - case VMCB_EXIT_MC: - return ("mchk"); - case VMCB_EXIT_INTR: - return ("extintr"); - case VMCB_EXIT_NMI: - return ("nmi"); - case VMCB_EXIT_VINTR: - return ("vintr"); - case VMCB_EXIT_MSR: - return ("msr"); - case VMCB_EXIT_IRET: - return ("iret"); - case VMCB_EXIT_MONITOR: - return ("monitor"); - case VMCB_EXIT_MWAIT: - return ("mwait"); - default: - snprintf(reasonbuf, sizeof(reasonbuf), "%#lx", reason); - return (reasonbuf); - } -} -#endif /* KTR */ - -/* - * From section "State Saved on Exit" in APMv2: nRIP is saved for all #VMEXITs - * that are due to instruction intercepts as well as MSR and IOIO intercepts - * and exceptions caused by INT3, INTO and BOUND instructions. - * - * Return 1 if the nRIP is valid and 0 otherwise. - */ -static int -nrip_valid(uint64_t exitcode) -{ - switch (exitcode) { - case 0x00 ... 0x0F: /* read of CR0 through CR15 */ - case 0x10 ... 0x1F: /* write of CR0 through CR15 */ - case 0x20 ... 0x2F: /* read of DR0 through DR15 */ - case 0x30 ... 0x3F: /* write of DR0 through DR15 */ - case 0x43: /* INT3 */ - case 0x44: /* INTO */ - case 0x45: /* BOUND */ - case 0x65 ... 0x7C: /* VMEXIT_CR0_SEL_WRITE ... VMEXIT_MSR */ - case 0x80 ... 0x8D: /* VMEXIT_VMRUN ... VMEXIT_XSETBV */ - return (1); - default: - return (0); - } -} - -static int -svm_vmexit(struct svm_softc *svm_sc, int vcpu, struct vm_exit *vmexit) -{ - struct vmcb *vmcb; - struct vmcb_state *state; - struct vmcb_ctrl *ctrl; - struct svm_regctx *ctx; - uint64_t code, info1, info2, val; - uint32_t eax, ecx, edx; - int error, errcode_valid, handled, idtvec, reflect; - bool retu; - - ctx = svm_get_guest_regctx(svm_sc, vcpu); - vmcb = svm_get_vmcb(svm_sc, vcpu); - state = &vmcb->state; - ctrl = &vmcb->ctrl; - - handled = 0; - code = ctrl->exitcode; - info1 = ctrl->exitinfo1; - info2 = ctrl->exitinfo2; - - vmexit->exitcode = VM_EXITCODE_BOGUS; - vmexit->rip = state->rip; - vmexit->inst_length = nrip_valid(code) ? ctrl->nrip - state->rip : 0; - - vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_COUNT, 1); - - /* - * #VMEXIT(INVALID) needs to be handled early because the VMCB is - * in an inconsistent state and can trigger assertions that would - * never happen otherwise. - */ - if (code == VMCB_EXIT_INVALID) { - vm_exit_svm(vmexit, code, info1, info2); - return (0); - } - - KASSERT((ctrl->eventinj & VMCB_EVENTINJ_VALID) == 0, ("%s: event " - "injection valid bit is set %#lx", __func__, ctrl->eventinj)); - - KASSERT(vmexit->inst_length >= 0 && vmexit->inst_length <= 15, - ("invalid inst_length %d: code (%#lx), info1 (%#lx), info2 (%#lx)", - vmexit->inst_length, code, info1, info2)); - - svm_update_virqinfo(svm_sc, vcpu); - svm_save_intinfo(svm_sc, vcpu); - - switch (code) { - case VMCB_EXIT_IRET: - /* - * Restart execution at "iret" but with the intercept cleared. - */ - vmexit->inst_length = 0; - clear_nmi_blocking(svm_sc, vcpu); - handled = 1; - break; - case VMCB_EXIT_VINTR: /* interrupt window exiting */ - vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_VINTR, 1); - handled = 1; - break; - case VMCB_EXIT_INTR: /* external interrupt */ - vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_EXTINT, 1); - handled = 1; - break; - case VMCB_EXIT_NMI: /* external NMI */ - handled = 1; - break; - case 0x40 ... 0x5F: - vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_EXCEPTION, 1); - reflect = 1; - idtvec = code - 0x40; - switch (idtvec) { - case IDT_MC: - /* - * Call the machine check handler by hand. Also don't - * reflect the machine check back into the guest. - */ - reflect = 0; - VCPU_CTR0(svm_sc->vm, vcpu, "Vectoring to MCE handler"); - __asm __volatile("int $18"); - break; - case IDT_PF: - error = svm_setreg(svm_sc, vcpu, VM_REG_GUEST_CR2, - info2); - KASSERT(error == 0, ("%s: error %d updating cr2", - __func__, error)); - /* fallthru */ - case IDT_NP: - case IDT_SS: - case IDT_GP: - case IDT_AC: - case IDT_TS: - errcode_valid = 1; - break; - - case IDT_DF: - errcode_valid = 1; - info1 = 0; - break; - - case IDT_BP: - case IDT_OF: - case IDT_BR: - /* - * The 'nrip' field is populated for INT3, INTO and - * BOUND exceptions and this also implies that - * 'inst_length' is non-zero. - * - * Reset 'inst_length' to zero so the guest %rip at - * event injection is identical to what it was when - * the exception originally happened. - */ - VCPU_CTR2(svm_sc->vm, vcpu, "Reset inst_length from %d " - "to zero before injecting exception %d", - vmexit->inst_length, idtvec); - vmexit->inst_length = 0; - /* fallthru */ - default: - errcode_valid = 0; - info1 = 0; - break; - } - KASSERT(vmexit->inst_length == 0, ("invalid inst_length (%d) " - "when reflecting exception %d into guest", - vmexit->inst_length, idtvec)); - - if (reflect) { - /* Reflect the exception back into the guest */ - VCPU_CTR2(svm_sc->vm, vcpu, "Reflecting exception " - "%d/%#x into the guest", idtvec, (int)info1); - error = vm_inject_exception(svm_sc->vm, vcpu, idtvec, - errcode_valid, info1, 0); - KASSERT(error == 0, ("%s: vm_inject_exception error %d", - __func__, error)); - } - handled = 1; - break; - case VMCB_EXIT_MSR: /* MSR access. */ - eax = state->rax; - ecx = ctx->sctx_rcx; - edx = ctx->sctx_rdx; - retu = false; - - if (info1) { - vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_WRMSR, 1); - val = (uint64_t)edx << 32 | eax; - VCPU_CTR2(svm_sc->vm, vcpu, "wrmsr %#x val %#lx", - ecx, val); - if (emulate_wrmsr(svm_sc, vcpu, ecx, val, &retu)) { - vmexit->exitcode = VM_EXITCODE_WRMSR; - vmexit->u.msr.code = ecx; - vmexit->u.msr.wval = val; - } else if (!retu) { - handled = 1; - } else { - KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS, - ("emulate_wrmsr retu with bogus exitcode")); - } - } else { - VCPU_CTR1(svm_sc->vm, vcpu, "rdmsr %#x", ecx); - vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_RDMSR, 1); - if (emulate_rdmsr(svm_sc, vcpu, ecx, &retu)) { - vmexit->exitcode = VM_EXITCODE_RDMSR; - vmexit->u.msr.code = ecx; - } else if (!retu) { - handled = 1; - } else { - KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS, - ("emulate_rdmsr retu with bogus exitcode")); - } - } - break; - case VMCB_EXIT_IO: - handled = svm_handle_io(svm_sc, vcpu, vmexit); - vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_INOUT, 1); - break; - case VMCB_EXIT_CPUID: - vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_CPUID, 1); - handled = x86_emulate_cpuid(svm_sc->vm, vcpu, - (uint32_t *)&state->rax, - (uint32_t *)&ctx->sctx_rbx, - (uint32_t *)&ctx->sctx_rcx, - (uint32_t *)&ctx->sctx_rdx); - break; - case VMCB_EXIT_HLT: - vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_HLT, 1); - vmexit->exitcode = VM_EXITCODE_HLT; - vmexit->u.hlt.rflags = state->rflags; - break; - case VMCB_EXIT_PAUSE: - vmexit->exitcode = VM_EXITCODE_PAUSE; - vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_PAUSE, 1); - break; - case VMCB_EXIT_NPF: - /* EXITINFO2 contains the faulting guest physical address */ - if (info1 & VMCB_NPF_INFO1_RSV) { - VCPU_CTR2(svm_sc->vm, vcpu, "nested page fault with " - "reserved bits set: info1(%#lx) info2(%#lx)", - info1, info2); - } else if (vm_mem_allocated(svm_sc->vm, info2)) { - vmexit->exitcode = VM_EXITCODE_PAGING; - vmexit->u.paging.gpa = info2; - vmexit->u.paging.fault_type = npf_fault_type(info1); - vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_NESTED_FAULT, 1); - VCPU_CTR3(svm_sc->vm, vcpu, "nested page fault " - "on gpa %#lx/%#lx at rip %#lx", - info2, info1, state->rip); - } else if (svm_npf_emul_fault(info1)) { - svm_handle_inst_emul(vmcb, info2, vmexit); - vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_INST_EMUL, 1); - VCPU_CTR3(svm_sc->vm, vcpu, "inst_emul fault " - "for gpa %#lx/%#lx at rip %#lx", - info2, info1, state->rip); - } - break; - case VMCB_EXIT_MONITOR: - vmexit->exitcode = VM_EXITCODE_MONITOR; - break; - case VMCB_EXIT_MWAIT: - vmexit->exitcode = VM_EXITCODE_MWAIT; - break; - default: - vmm_stat_incr(svm_sc->vm, vcpu, VMEXIT_UNKNOWN, 1); - break; - } - - VCPU_CTR4(svm_sc->vm, vcpu, "%s %s vmexit at %#lx/%d", - handled ? "handled" : "unhandled", exit_reason_to_str(code), - vmexit->rip, vmexit->inst_length); - - if (handled) { - vmexit->rip += vmexit->inst_length; - vmexit->inst_length = 0; - state->rip = vmexit->rip; - } else { - if (vmexit->exitcode == VM_EXITCODE_BOGUS) { - /* - * If this VM exit was not claimed by anybody then - * treat it as a generic SVM exit. - */ - vm_exit_svm(vmexit, code, info1, info2); - } else { - /* - * The exitcode and collateral have been populated. - * The VM exit will be processed further in userland. - */ - } - } - return (handled); -} - -static void -svm_inj_intinfo(struct svm_softc *svm_sc, int vcpu) -{ - uint64_t intinfo; - - if (!vm_entry_intinfo(svm_sc->vm, vcpu, &intinfo)) - return; - - KASSERT(VMCB_EXITINTINFO_VALID(intinfo), ("%s: entry intinfo is not " - "valid: %#lx", __func__, intinfo)); - - svm_eventinject(svm_sc, vcpu, VMCB_EXITINTINFO_TYPE(intinfo), - VMCB_EXITINTINFO_VECTOR(intinfo), - VMCB_EXITINTINFO_EC(intinfo), - VMCB_EXITINTINFO_EC_VALID(intinfo)); - vmm_stat_incr(svm_sc->vm, vcpu, VCPU_INTINFO_INJECTED, 1); - VCPU_CTR1(svm_sc->vm, vcpu, "Injected entry intinfo: %#lx", intinfo); -} - -/* - * Inject event to virtual cpu. - */ -static void -svm_inj_interrupts(struct svm_softc *sc, int vcpu, struct vlapic *vlapic) -{ - struct vmcb_ctrl *ctrl; - struct vmcb_state *state; - struct svm_vcpu *vcpustate; - uint8_t v_tpr; - int vector, need_intr_window, pending_apic_vector; - - state = svm_get_vmcb_state(sc, vcpu); - ctrl = svm_get_vmcb_ctrl(sc, vcpu); - vcpustate = svm_get_vcpu(sc, vcpu); - - need_intr_window = 0; - pending_apic_vector = 0; - - if (vcpustate->nextrip != state->rip) { - ctrl->intr_shadow = 0; - VCPU_CTR2(sc->vm, vcpu, "Guest interrupt blocking " - "cleared due to rip change: %#lx/%#lx", - vcpustate->nextrip, state->rip); - } - - /* - * Inject pending events or exceptions for this vcpu. - * - * An event might be pending because the previous #VMEXIT happened - * during event delivery (i.e. ctrl->exitintinfo). - * - * An event might also be pending because an exception was injected - * by the hypervisor (e.g. #PF during instruction emulation). - */ - svm_inj_intinfo(sc, vcpu); - - /* NMI event has priority over interrupts. */ - if (vm_nmi_pending(sc->vm, vcpu)) { - if (nmi_blocked(sc, vcpu)) { - /* - * Can't inject another NMI if the guest has not - * yet executed an "iret" after the last NMI. - */ - VCPU_CTR0(sc->vm, vcpu, "Cannot inject NMI due " - "to NMI-blocking"); - } else if (ctrl->intr_shadow) { - /* - * Can't inject an NMI if the vcpu is in an intr_shadow. - */ - VCPU_CTR0(sc->vm, vcpu, "Cannot inject NMI due to " - "interrupt shadow"); - need_intr_window = 1; - goto done; - } else if (ctrl->eventinj & VMCB_EVENTINJ_VALID) { - /* - * If there is already an exception/interrupt pending - * then defer the NMI until after that. - */ - VCPU_CTR1(sc->vm, vcpu, "Cannot inject NMI due to " - "eventinj %#lx", ctrl->eventinj); - - /* - * Use self-IPI to trigger a VM-exit as soon as - * possible after the event injection is completed. - * - * This works only if the external interrupt exiting - * is at a lower priority than the event injection. - * - * Although not explicitly specified in APMv2 the - * relative priorities were verified empirically. - */ - ipi_cpu(curcpu, IPI_AST); /* XXX vmm_ipinum? */ - } else { - vm_nmi_clear(sc->vm, vcpu); - - /* Inject NMI, vector number is not used */ - svm_eventinject(sc, vcpu, VMCB_EVENTINJ_TYPE_NMI, - IDT_NMI, 0, false); - - /* virtual NMI blocking is now in effect */ - enable_nmi_blocking(sc, vcpu); - - VCPU_CTR0(sc->vm, vcpu, "Injecting vNMI"); - } - } - - if (!vm_extint_pending(sc->vm, vcpu)) { - /* - * APIC interrupts are delivered using the V_IRQ offload. - * - * The primary benefit is that the hypervisor doesn't need to - * deal with the various conditions that inhibit interrupts. - * It also means that TPR changes via CR8 will be handled - * without any hypervisor involvement. - * - * Note that the APIC vector must remain pending in the vIRR - * until it is confirmed that it was delivered to the guest. - * This can be confirmed based on the value of V_IRQ at the - * next #VMEXIT (1 = pending, 0 = delivered). - * - * Also note that it is possible that another higher priority - * vector can become pending before this vector is delivered - * to the guest. This is alright because vcpu_notify_event() - * will send an IPI and force the vcpu to trap back into the - * hypervisor. The higher priority vector will be injected on - * the next VMRUN. - */ - if (vlapic_pending_intr(vlapic, &vector)) { - KASSERT(vector >= 16 && vector <= 255, - ("invalid vector %d from local APIC", vector)); - pending_apic_vector = vector; - } - goto done; - } - - /* Ask the legacy pic for a vector to inject */ - vatpic_pending_intr(sc->vm, &vector); - KASSERT(vector >= 0 && vector <= 255, ("invalid vector %d from INTR", - vector)); - - /* - * If the guest has disabled interrupts or is in an interrupt shadow - * then we cannot inject the pending interrupt. - */ - if ((state->rflags & PSL_I) == 0) { - VCPU_CTR2(sc->vm, vcpu, "Cannot inject vector %d due to " - "rflags %#lx", vector, state->rflags); - need_intr_window = 1; - goto done; - } - - if (ctrl->intr_shadow) { - VCPU_CTR1(sc->vm, vcpu, "Cannot inject vector %d due to " - "interrupt shadow", vector); - need_intr_window = 1; - goto done; - } - - if (ctrl->eventinj & VMCB_EVENTINJ_VALID) { - VCPU_CTR2(sc->vm, vcpu, "Cannot inject vector %d due to " - "eventinj %#lx", vector, ctrl->eventinj); - need_intr_window = 1; - goto done; - } - - /* - * Legacy PIC interrupts are delivered via the event injection - * mechanism. - */ - svm_eventinject(sc, vcpu, VMCB_EVENTINJ_TYPE_INTR, vector, 0, false); - - vm_extint_clear(sc->vm, vcpu); - vatpic_intr_accepted(sc->vm, vector); - - /* - * Force a VM-exit as soon as the vcpu is ready to accept another - * interrupt. This is done because the PIC might have another vector - * that it wants to inject. Also, if the APIC has a pending interrupt - * that was preempted by the ExtInt then it allows us to inject the - * APIC vector as soon as possible. - */ - need_intr_window = 1; -done: - /* - * The guest can modify the TPR by writing to %CR8. In guest mode - * the processor reflects this write to V_TPR without hypervisor - * intervention. - * - * The guest can also modify the TPR by writing to it via the memory - * mapped APIC page. In this case, the write will be emulated by the - * hypervisor. For this reason V_TPR must be updated before every - * VMRUN. - */ - v_tpr = vlapic_get_cr8(vlapic); - KASSERT(v_tpr <= 15, ("invalid v_tpr %#x", v_tpr)); - if (ctrl->v_tpr != v_tpr) { - VCPU_CTR2(sc->vm, vcpu, "VMCB V_TPR changed from %#x to %#x", - ctrl->v_tpr, v_tpr); - ctrl->v_tpr = v_tpr; - svm_set_dirty(sc, vcpu, VMCB_CACHE_TPR); - } - - if (pending_apic_vector) { - /* - * If an APIC vector is being injected then interrupt window - * exiting is not possible on this VMRUN. - */ - KASSERT(!need_intr_window, ("intr_window exiting impossible")); - VCPU_CTR1(sc->vm, vcpu, "Injecting vector %d using V_IRQ", - pending_apic_vector); - - ctrl->v_irq = 1; - ctrl->v_ign_tpr = 0; - ctrl->v_intr_vector = pending_apic_vector; - ctrl->v_intr_prio = pending_apic_vector >> 4; - svm_set_dirty(sc, vcpu, VMCB_CACHE_TPR); - } else if (need_intr_window) { - /* - * We use V_IRQ in conjunction with the VINTR intercept to - * trap into the hypervisor as soon as a virtual interrupt - * can be delivered. - * - * Since injected events are not subject to intercept checks - * we need to ensure that the V_IRQ is not actually going to - * be delivered on VM entry. The KASSERT below enforces this. - */ - KASSERT((ctrl->eventinj & VMCB_EVENTINJ_VALID) != 0 || - (state->rflags & PSL_I) == 0 || ctrl->intr_shadow, - ("Bogus intr_window_exiting: eventinj (%#lx), " - "intr_shadow (%u), rflags (%#lx)", - ctrl->eventinj, ctrl->intr_shadow, state->rflags)); - enable_intr_window_exiting(sc, vcpu); - } else { - disable_intr_window_exiting(sc, vcpu); - } -} - -static __inline void -restore_host_tss(void) -{ - struct system_segment_descriptor *tss_sd; - - /* - * The TSS descriptor was in use prior to launching the guest so it - * has been marked busy. - * - * 'ltr' requires the descriptor to be marked available so change the - * type to "64-bit available TSS". - */ - tss_sd = PCPU_GET(tss); - tss_sd->sd_type = SDT_SYSTSS; - ltr(GSEL(GPROC0_SEL, SEL_KPL)); -} - -static void -check_asid(struct svm_softc *sc, int vcpuid, pmap_t pmap, u_int thiscpu) -{ - struct svm_vcpu *vcpustate; - struct vmcb_ctrl *ctrl; - long eptgen; - bool alloc_asid; - - KASSERT(CPU_ISSET(thiscpu, &pmap->pm_active), ("%s: nested pmap not " - "active on cpu %u", __func__, thiscpu)); - - vcpustate = svm_get_vcpu(sc, vcpuid); - ctrl = svm_get_vmcb_ctrl(sc, vcpuid); - - /* - * The TLB entries associated with the vcpu's ASID are not valid - * if either of the following conditions is true: - * - * 1. The vcpu's ASID generation is different than the host cpu's - * ASID generation. This happens when the vcpu migrates to a new - * host cpu. It can also happen when the number of vcpus executing - * on a host cpu is greater than the number of ASIDs available. - * - * 2. The pmap generation number is different than the value cached in - * the 'vcpustate'. This happens when the host invalidates pages - * belonging to the guest. - * - * asidgen eptgen Action - * mismatch mismatch - * 0 0 (a) - * 0 1 (b1) or (b2) - * 1 0 (c) - * 1 1 (d) - * - * (a) There is no mismatch in eptgen or ASID generation and therefore - * no further action is needed. - * - * (b1) If the cpu supports FlushByAsid then the vcpu's ASID is - * retained and the TLB entries associated with this ASID - * are flushed by VMRUN. - * - * (b2) If the cpu does not support FlushByAsid then a new ASID is - * allocated. - * - * (c) A new ASID is allocated. - * - * (d) A new ASID is allocated. - */ - - alloc_asid = false; - eptgen = pmap->pm_eptgen; - ctrl->tlb_ctrl = VMCB_TLB_FLUSH_NOTHING; - - if (vcpustate->asid.gen != asid[thiscpu].gen) { - alloc_asid = true; /* (c) and (d) */ - } else if (vcpustate->eptgen != eptgen) { - if (flush_by_asid()) - ctrl->tlb_ctrl = VMCB_TLB_FLUSH_GUEST; /* (b1) */ - else - alloc_asid = true; /* (b2) */ - } else { - /* - * This is the common case (a). - */ - KASSERT(!alloc_asid, ("ASID allocation not necessary")); - KASSERT(ctrl->tlb_ctrl == VMCB_TLB_FLUSH_NOTHING, - ("Invalid VMCB tlb_ctrl: %#x", ctrl->tlb_ctrl)); - } - - if (alloc_asid) { - if (++asid[thiscpu].num >= nasid) { - asid[thiscpu].num = 1; - if (++asid[thiscpu].gen == 0) - asid[thiscpu].gen = 1; - /* - * If this cpu does not support "flush-by-asid" - * then flush the entire TLB on a generation - * bump. Subsequent ASID allocation in this - * generation can be done without a TLB flush. - */ - if (!flush_by_asid()) - ctrl->tlb_ctrl = VMCB_TLB_FLUSH_ALL; - } - vcpustate->asid.gen = asid[thiscpu].gen; - vcpustate->asid.num = asid[thiscpu].num; - - ctrl->asid = vcpustate->asid.num; - svm_set_dirty(sc, vcpuid, VMCB_CACHE_ASID); - /* - * If this cpu supports "flush-by-asid" then the TLB - * was not flushed after the generation bump. The TLB - * is flushed selectively after every new ASID allocation. - */ - if (flush_by_asid()) - ctrl->tlb_ctrl = VMCB_TLB_FLUSH_GUEST; - } - vcpustate->eptgen = eptgen; - - KASSERT(ctrl->asid != 0, ("Guest ASID must be non-zero")); - KASSERT(ctrl->asid == vcpustate->asid.num, - ("ASID mismatch: %u/%u", ctrl->asid, vcpustate->asid.num)); -} - -static __inline void -disable_gintr(void) -{ - - __asm __volatile("clgi"); -} - -static __inline void -enable_gintr(void) -{ - - __asm __volatile("stgi"); -} - -/* - * Start vcpu with specified RIP. - */ -static int -svm_vmrun(void *arg, int vcpu, register_t rip, pmap_t pmap, - void *rend_cookie, void *suspended_cookie) -{ - struct svm_regctx *gctx; - struct svm_softc *svm_sc; - struct svm_vcpu *vcpustate; - struct vmcb_state *state; - struct vmcb_ctrl *ctrl; - struct vm_exit *vmexit; - struct vlapic *vlapic; - struct vm *vm; - uint64_t vmcb_pa; - u_int thiscpu; - int handled; - - svm_sc = arg; - vm = svm_sc->vm; - - vcpustate = svm_get_vcpu(svm_sc, vcpu); - state = svm_get_vmcb_state(svm_sc, vcpu); - ctrl = svm_get_vmcb_ctrl(svm_sc, vcpu); - vmexit = vm_exitinfo(vm, vcpu); - vlapic = vm_lapic(vm, vcpu); - - /* - * Stash 'curcpu' on the stack as 'thiscpu'. - * - * The per-cpu data area is not accessible until MSR_GSBASE is restored - * after the #VMEXIT. Since VMRUN is executed inside a critical section - * 'curcpu' and 'thiscpu' are guaranteed to identical. - */ - thiscpu = curcpu; - - gctx = svm_get_guest_regctx(svm_sc, vcpu); - vmcb_pa = svm_sc->vcpu[vcpu].vmcb_pa; - - if (vcpustate->lastcpu != thiscpu) { - /* - * Force new ASID allocation by invalidating the generation. - */ - vcpustate->asid.gen = 0; - - /* - * Invalidate the VMCB state cache by marking all fields dirty. - */ - svm_set_dirty(svm_sc, vcpu, 0xffffffff); - - /* - * XXX - * Setting 'vcpustate->lastcpu' here is bit premature because - * we may return from this function without actually executing - * the VMRUN instruction. This could happen if a rendezvous - * or an AST is pending on the first time through the loop. - * - * This works for now but any new side-effects of vcpu - * migration should take this case into account. - */ - vcpustate->lastcpu = thiscpu; - vmm_stat_incr(vm, vcpu, VCPU_MIGRATIONS, 1); - } - - svm_msr_guest_enter(svm_sc, vcpu); - - /* Update Guest RIP */ - state->rip = rip; - - do { - /* - * Disable global interrupts to guarantee atomicity during - * loading of guest state. This includes not only the state - * loaded by the "vmrun" instruction but also software state - * maintained by the hypervisor: suspended and rendezvous - * state, NPT generation number, vlapic interrupts etc. - */ - disable_gintr(); - - if (vcpu_suspended(suspended_cookie)) { - enable_gintr(); - vm_exit_suspended(vm, vcpu, state->rip); - break; - } - - if (vcpu_rendezvous_pending(rend_cookie)) { - enable_gintr(); - vm_exit_rendezvous(vm, vcpu, state->rip); - break; - } - - /* We are asked to give the cpu by scheduler. */ - if (vcpu_should_yield(vm, vcpu)) { - enable_gintr(); - vm_exit_astpending(vm, vcpu, state->rip); - break; - } - - svm_inj_interrupts(svm_sc, vcpu, vlapic); - - /* Activate the nested pmap on 'thiscpu' */ - CPU_SET_ATOMIC_ACQ(thiscpu, &pmap->pm_active); - - /* - * Check the pmap generation and the ASID generation to - * ensure that the vcpu does not use stale TLB mappings. - */ - check_asid(svm_sc, vcpu, pmap, thiscpu); - - ctrl->vmcb_clean = vmcb_clean & ~vcpustate->dirty; - vcpustate->dirty = 0; - VCPU_CTR1(vm, vcpu, "vmcb clean %#x", ctrl->vmcb_clean); - - /* Launch Virtual Machine. */ - VCPU_CTR1(vm, vcpu, "Resume execution at %#lx", state->rip); - svm_launch(vmcb_pa, gctx); - - CPU_CLR_ATOMIC(thiscpu, &pmap->pm_active); - - /* - * Restore MSR_GSBASE to point to the pcpu data area. - * - * Note that accesses done via PCPU_GET/PCPU_SET will work - * only after MSR_GSBASE is restored. - * - * Also note that we don't bother restoring MSR_KGSBASE - * since it is not used in the kernel and will be restored - * when the VMRUN ioctl returns to userspace. - */ - wrmsr(MSR_GSBASE, (uint64_t)&__pcpu[thiscpu]); - KASSERT(curcpu == thiscpu, ("thiscpu/curcpu (%u/%u) mismatch", - thiscpu, curcpu)); - - /* - * The host GDTR and IDTR is saved by VMRUN and restored - * automatically on #VMEXIT. However, the host TSS needs - * to be restored explicitly. - */ - restore_host_tss(); - - /* #VMEXIT disables interrupts so re-enable them here. */ - enable_gintr(); - - /* Update 'nextrip' */ - vcpustate->nextrip = state->rip; - - /* Handle #VMEXIT and if required return to user space. */ - handled = svm_vmexit(svm_sc, vcpu, vmexit); - } while (handled); - - svm_msr_guest_exit(svm_sc, vcpu); - - return (0); -} - -static void -svm_vmcleanup(void *arg) -{ - struct svm_softc *sc = arg; - - free(sc, M_SVM); -} - -static register_t * -swctx_regptr(struct svm_regctx *regctx, int reg) -{ - - switch (reg) { - case VM_REG_GUEST_RBX: - return (®ctx->sctx_rbx); - case VM_REG_GUEST_RCX: - return (®ctx->sctx_rcx); - case VM_REG_GUEST_RDX: - return (®ctx->sctx_rdx); - case VM_REG_GUEST_RDI: - return (®ctx->sctx_rdi); - case VM_REG_GUEST_RSI: - return (®ctx->sctx_rsi); - case VM_REG_GUEST_RBP: - return (®ctx->sctx_rbp); - case VM_REG_GUEST_R8: - return (®ctx->sctx_r8); - case VM_REG_GUEST_R9: - return (®ctx->sctx_r9); - case VM_REG_GUEST_R10: - return (®ctx->sctx_r10); - case VM_REG_GUEST_R11: - return (®ctx->sctx_r11); - case VM_REG_GUEST_R12: - return (®ctx->sctx_r12); - case VM_REG_GUEST_R13: - return (®ctx->sctx_r13); - case VM_REG_GUEST_R14: - return (®ctx->sctx_r14); - case VM_REG_GUEST_R15: - return (®ctx->sctx_r15); - default: - return (NULL); - } -} - -static int -svm_getreg(void *arg, int vcpu, int ident, uint64_t *val) -{ - struct svm_softc *svm_sc; - register_t *reg; - - svm_sc = arg; - - if (ident == VM_REG_GUEST_INTR_SHADOW) { - return (svm_get_intr_shadow(svm_sc, vcpu, val)); - } - - if (vmcb_read(svm_sc, vcpu, ident, val) == 0) { - return (0); - } - - reg = swctx_regptr(svm_get_guest_regctx(svm_sc, vcpu), ident); - - if (reg != NULL) { - *val = *reg; - return (0); - } - - VCPU_CTR1(svm_sc->vm, vcpu, "svm_getreg: unknown register %#x", ident); - return (EINVAL); -} - -static int -svm_setreg(void *arg, int vcpu, int ident, uint64_t val) -{ - struct svm_softc *svm_sc; - register_t *reg; - - svm_sc = arg; - - if (ident == VM_REG_GUEST_INTR_SHADOW) { - return (svm_modify_intr_shadow(svm_sc, vcpu, val)); - } - - if (vmcb_write(svm_sc, vcpu, ident, val) == 0) { - return (0); - } - - reg = swctx_regptr(svm_get_guest_regctx(svm_sc, vcpu), ident); - - if (reg != NULL) { - *reg = val; - return (0); - } - - /* - * XXX deal with CR3 and invalidate TLB entries tagged with the - * vcpu's ASID. This needs to be treated differently depending on - * whether 'running' is true/false. - */ - - VCPU_CTR1(svm_sc->vm, vcpu, "svm_setreg: unknown register %#x", ident); - return (EINVAL); -} - -static int -svm_setcap(void *arg, int vcpu, int type, int val) -{ - struct svm_softc *sc; - int error; - - sc = arg; - error = 0; - switch (type) { - case VM_CAP_HALT_EXIT: - svm_set_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, - VMCB_INTCPT_HLT, val); - break; - case VM_CAP_PAUSE_EXIT: - svm_set_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, - VMCB_INTCPT_PAUSE, val); - break; - case VM_CAP_UNRESTRICTED_GUEST: - /* Unrestricted guest execution cannot be disabled in SVM */ - if (val == 0) - error = EINVAL; - break; - default: - error = ENOENT; - break; - } - return (error); -} - -static int -svm_getcap(void *arg, int vcpu, int type, int *retval) -{ - struct svm_softc *sc; - int error; - - sc = arg; - error = 0; - - switch (type) { - case VM_CAP_HALT_EXIT: - *retval = svm_get_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, - VMCB_INTCPT_HLT); - break; - case VM_CAP_PAUSE_EXIT: - *retval = svm_get_intercept(sc, vcpu, VMCB_CTRL1_INTCPT, - VMCB_INTCPT_PAUSE); - break; - case VM_CAP_UNRESTRICTED_GUEST: - *retval = 1; /* unrestricted guest is always enabled */ - break; - default: - error = ENOENT; - break; - } - return (error); -} - -static struct vlapic * -svm_vlapic_init(void *arg, int vcpuid) -{ - struct svm_softc *svm_sc; - struct vlapic *vlapic; - - svm_sc = arg; - vlapic = malloc(sizeof(struct vlapic), M_SVM_VLAPIC, M_WAITOK | M_ZERO); - vlapic->vm = svm_sc->vm; - vlapic->vcpuid = vcpuid; - vlapic->apic_page = (struct LAPIC *)&svm_sc->apic_page[vcpuid]; - - vlapic_init(vlapic); - - return (vlapic); -} - -static void -svm_vlapic_cleanup(void *arg, struct vlapic *vlapic) -{ - - vlapic_cleanup(vlapic); - free(vlapic, M_SVM_VLAPIC); -} - -struct vmm_ops vmm_ops_amd = { - svm_init, - svm_cleanup, - svm_restore, - svm_vminit, - svm_vmrun, - svm_vmcleanup, - svm_getreg, - svm_setreg, - vmcb_getdesc, - vmcb_setdesc, - svm_getcap, - svm_setcap, - svm_npt_alloc, - svm_npt_free, - svm_vlapic_init, - svm_vlapic_cleanup -}; diff --git a/vmm/amd/svm.h b/vmm/amd/svm.h deleted file mode 100644 index 86bd638..0000000 --- a/vmm/amd/svm.h +++ /dev/null @@ -1,54 +0,0 @@ -/*- - * Copyright (c) 2013 Anish Gupta (akgupt3@gmail.com) - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice unmodified, this list of conditions, and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#ifndef _SVM_H_ -#define _SVM_H_ - -/* - * Guest register state that is saved outside the VMCB. - */ -struct svm_regctx { - register_t sctx_rbp; - register_t sctx_rbx; - register_t sctx_rcx; - register_t sctx_rdx; - register_t sctx_rdi; - register_t sctx_rsi; - register_t sctx_r8; - register_t sctx_r9; - register_t sctx_r10; - register_t sctx_r11; - register_t sctx_r12; - register_t sctx_r13; - register_t sctx_r14; - register_t sctx_r15; -}; - -void svm_launch(uint64_t pa, struct svm_regctx *); - -#endif /* _SVM_H_ */ diff --git a/vmm/amd/svm_genassym.c b/vmm/amd/svm_genassym.c deleted file mode 100644 index b7831eb..0000000 --- a/vmm/amd/svm_genassym.c +++ /dev/null @@ -1,48 +0,0 @@ -/*- - * Copyright (c) 2013 Anish Gupta (akgupt3@gmail.com) - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice unmodified, this list of conditions, and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include - -#include "svm.h" - -ASSYM(SCTX_RBX, offsetof(struct svm_regctx, sctx_rbx)); -ASSYM(SCTX_RCX, offsetof(struct svm_regctx, sctx_rcx)); -ASSYM(SCTX_RBP, offsetof(struct svm_regctx, sctx_rbp)); -ASSYM(SCTX_RDX, offsetof(struct svm_regctx, sctx_rdx)); -ASSYM(SCTX_RDI, offsetof(struct svm_regctx, sctx_rdi)); -ASSYM(SCTX_RSI, offsetof(struct svm_regctx, sctx_rsi)); -ASSYM(SCTX_R8, offsetof(struct svm_regctx, sctx_r8)); -ASSYM(SCTX_R9, offsetof(struct svm_regctx, sctx_r9)); -ASSYM(SCTX_R10, offsetof(struct svm_regctx, sctx_r10)); -ASSYM(SCTX_R11, offsetof(struct svm_regctx, sctx_r11)); -ASSYM(SCTX_R12, offsetof(struct svm_regctx, sctx_r12)); -ASSYM(SCTX_R13, offsetof(struct svm_regctx, sctx_r13)); -ASSYM(SCTX_R14, offsetof(struct svm_regctx, sctx_r14)); -ASSYM(SCTX_R15, offsetof(struct svm_regctx, sctx_r15)); diff --git a/vmm/amd/svm_msr.c b/vmm/amd/svm_msr.c deleted file mode 100644 index 088751a..0000000 --- a/vmm/amd/svm_msr.c +++ /dev/null @@ -1,165 +0,0 @@ -/*- - * Copyright (c) 2014, Neel Natu (neel@freebsd.org) - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice unmodified, this list of conditions, and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include - -#include -#include -#include - -#include "svm.h" -#include "vmcb.h" -#include "svm_softc.h" -#include "svm_msr.h" - -#ifndef MSR_AMDK8_IPM -#define MSR_AMDK8_IPM 0xc0010055 -#endif - -enum { - IDX_MSR_LSTAR, - IDX_MSR_CSTAR, - IDX_MSR_STAR, - IDX_MSR_SF_MASK, - HOST_MSR_NUM /* must be the last enumeration */ -}; - -static uint64_t host_msrs[HOST_MSR_NUM]; - -void -svm_msr_init(void) -{ - /* - * It is safe to cache the values of the following MSRs because they - * don't change based on curcpu, curproc or curthread. - */ - host_msrs[IDX_MSR_LSTAR] = rdmsr(MSR_LSTAR); - host_msrs[IDX_MSR_CSTAR] = rdmsr(MSR_CSTAR); - host_msrs[IDX_MSR_STAR] = rdmsr(MSR_STAR); - host_msrs[IDX_MSR_SF_MASK] = rdmsr(MSR_SF_MASK); -} - -void -svm_msr_guest_init(struct svm_softc *sc, int vcpu) -{ - /* - * All the MSRs accessible to the guest are either saved/restored by - * hardware on every #VMEXIT/VMRUN (e.g., G_PAT) or are saved/restored - * by VMSAVE/VMLOAD (e.g., MSR_GSBASE). - * - * There are no guest MSRs that are saved/restored "by hand" so nothing - * more to do here. - */ - return; -} - -void -svm_msr_guest_enter(struct svm_softc *sc, int vcpu) -{ - /* - * Save host MSRs (if any) and restore guest MSRs (if any). - */ -} - -void -svm_msr_guest_exit(struct svm_softc *sc, int vcpu) -{ - /* - * Save guest MSRs (if any) and restore host MSRs. - */ - wrmsr(MSR_LSTAR, host_msrs[IDX_MSR_LSTAR]); - wrmsr(MSR_CSTAR, host_msrs[IDX_MSR_CSTAR]); - wrmsr(MSR_STAR, host_msrs[IDX_MSR_STAR]); - wrmsr(MSR_SF_MASK, host_msrs[IDX_MSR_SF_MASK]); - - /* MSR_KGSBASE will be restored on the way back to userspace */ -} - -int -svm_rdmsr(struct svm_softc *sc, int vcpu, u_int num, uint64_t *result, - bool *retu) -{ - int error = 0; - - switch (num) { - case MSR_MCG_CAP: - case MSR_MCG_STATUS: - *result = 0; - break; - case MSR_MTRRcap: - case MSR_MTRRdefType: - case MSR_MTRR4kBase ... MSR_MTRR4kBase + 8: - case MSR_MTRR16kBase ... MSR_MTRR16kBase + 1: - case MSR_MTRR64kBase: - case MSR_SYSCFG: - *result = 0; - break; - case MSR_AMDK8_IPM: - *result = 0; - break; - default: - error = EINVAL; - break; - } - - return (error); -} - -int -svm_wrmsr(struct svm_softc *sc, int vcpu, u_int num, uint64_t val, bool *retu) -{ - int error = 0; - - switch (num) { - case MSR_MCG_CAP: - case MSR_MCG_STATUS: - break; /* ignore writes */ - case MSR_MTRRcap: - vm_inject_gp(sc->vm, vcpu); - break; - case MSR_MTRRdefType: - case MSR_MTRR4kBase ... MSR_MTRR4kBase + 8: - case MSR_MTRR16kBase ... MSR_MTRR16kBase + 1: - case MSR_MTRR64kBase: - case MSR_SYSCFG: - break; /* Ignore writes */ - case MSR_AMDK8_IPM: - /* - * Ignore writes to the "Interrupt Pending Message" MSR. - */ - break; - default: - error = EINVAL; - break; - } - - return (error); -} diff --git a/vmm/amd/svm_msr.h b/vmm/amd/svm_msr.h deleted file mode 100644 index 07716c8..0000000 --- a/vmm/amd/svm_msr.h +++ /dev/null @@ -1,44 +0,0 @@ -/*- - * Copyright (c) 2014 Neel Natu (neel@freebsd.org) - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice unmodified, this list of conditions, and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#ifndef _SVM_MSR_H_ -#define _SVM_MSR_H_ - -struct svm_softc; - -void svm_msr_init(void); -void svm_msr_guest_init(struct svm_softc *sc, int vcpu); -void svm_msr_guest_enter(struct svm_softc *sc, int vcpu); -void svm_msr_guest_exit(struct svm_softc *sc, int vcpu); - -int svm_wrmsr(struct svm_softc *sc, int vcpu, u_int num, uint64_t val, - bool *retu); -int svm_rdmsr(struct svm_softc *sc, int vcpu, u_int num, uint64_t *result, - bool *retu); - -#endif /* _SVM_MSR_H_ */ diff --git a/vmm/amd/svm_softc.h b/vmm/amd/svm_softc.h deleted file mode 100644 index de0c3f7..0000000 --- a/vmm/amd/svm_softc.h +++ /dev/null @@ -1,114 +0,0 @@ -/*- - * Copyright (c) 2013 Anish Gupta (akgupt3@gmail.com) - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice unmodified, this list of conditions, and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#ifndef _SVM_SOFTC_H_ -#define _SVM_SOFTC_H_ - -#define SVM_IO_BITMAP_SIZE (3 * PAGE_SIZE) -#define SVM_MSR_BITMAP_SIZE (2 * PAGE_SIZE) - -struct asid { - uint64_t gen; /* range is [1, ~0UL] */ - uint32_t num; /* range is [1, nasid - 1] */ -}; - -/* - * XXX separate out 'struct vmcb' from 'svm_vcpu' to avoid wasting space - * due to VMCB alignment requirements. - */ -struct svm_vcpu { - struct vmcb vmcb; /* hardware saved vcpu context */ - struct svm_regctx swctx; /* software saved vcpu context */ - uint64_t vmcb_pa; /* VMCB physical address */ - uint64_t nextrip; /* next instruction to be executed by guest */ - int lastcpu; /* host cpu that the vcpu last ran on */ - uint32_t dirty; /* state cache bits that must be cleared */ - long eptgen; /* pmap->pm_eptgen when the vcpu last ran */ - struct asid asid; -} __aligned(PAGE_SIZE); - -/* - * SVM softc, one per virtual machine. - */ -struct svm_softc { - uint8_t iopm_bitmap[SVM_IO_BITMAP_SIZE]; /* shared by all vcpus */ - uint8_t msr_bitmap[SVM_MSR_BITMAP_SIZE]; /* shared by all vcpus */ - uint8_t apic_page[VM_MAXCPU][PAGE_SIZE]; - struct svm_vcpu vcpu[VM_MAXCPU]; - vm_offset_t nptp; /* nested page table */ - struct vm *vm; -} __aligned(PAGE_SIZE); - -CTASSERT((offsetof(struct svm_softc, nptp) & PAGE_MASK) == 0); - -static __inline struct svm_vcpu * -svm_get_vcpu(struct svm_softc *sc, int vcpu) -{ - - return (&(sc->vcpu[vcpu])); -} - -static __inline struct vmcb * -svm_get_vmcb(struct svm_softc *sc, int vcpu) -{ - - return (&(sc->vcpu[vcpu].vmcb)); -} - -static __inline struct vmcb_state * -svm_get_vmcb_state(struct svm_softc *sc, int vcpu) -{ - - return (&(sc->vcpu[vcpu].vmcb.state)); -} - -static __inline struct vmcb_ctrl * -svm_get_vmcb_ctrl(struct svm_softc *sc, int vcpu) -{ - - return (&(sc->vcpu[vcpu].vmcb.ctrl)); -} - -static __inline struct svm_regctx * -svm_get_guest_regctx(struct svm_softc *sc, int vcpu) -{ - - return (&(sc->vcpu[vcpu].swctx)); -} - -static __inline void -svm_set_dirty(struct svm_softc *sc, int vcpu, uint32_t dirtybits) -{ - struct svm_vcpu *vcpustate; - - vcpustate = svm_get_vcpu(sc, vcpu); - - vcpustate->dirty |= dirtybits; -} - -#endif /* _SVM_SOFTC_H_ */ diff --git a/vmm/amd/svm_support.S b/vmm/amd/svm_support.S deleted file mode 100644 index b363101..0000000 --- a/vmm/amd/svm_support.S +++ /dev/null @@ -1,121 +0,0 @@ -/*- - * Copyright (c) 2013, Anish Gupta (akgupt3@gmail.com) - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice unmodified, this list of conditions, and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $FreeBSD$ - */ -#include - -#include "svm_assym.h" - -/* - * Be friendly to DTrace FBT's prologue/epilogue pattern matching. - * - * They are also responsible for saving/restoring the host %rbp across VMRUN. - */ -#define VENTER push %rbp ; mov %rsp,%rbp -#define VLEAVE pop %rbp - -#define VMLOAD .byte 0x0f, 0x01, 0xda -#define VMRUN .byte 0x0f, 0x01, 0xd8 -#define VMSAVE .byte 0x0f, 0x01, 0xdb - -/* - * svm_launch(uint64_t vmcb, struct svm_regctx *gctx) - * %rdi: physical address of VMCB - * %rsi: pointer to guest context - */ -ENTRY(svm_launch) - VENTER - - /* - * Host register state saved across a VMRUN. - * - * All "callee saved registers" except: - * %rsp: because it is preserved by the processor across VMRUN. - * %rbp: because it is saved/restored by the function prologue/epilogue. - */ - push %rbx - push %r12 - push %r13 - push %r14 - push %r15 - - /* Save the physical address of the VMCB in %rax */ - movq %rdi, %rax - - push %rsi /* push guest context pointer on the stack */ - - /* - * Restore guest state. - */ - movq SCTX_R8(%rsi), %r8 - movq SCTX_R9(%rsi), %r9 - movq SCTX_R10(%rsi), %r10 - movq SCTX_R11(%rsi), %r11 - movq SCTX_R12(%rsi), %r12 - movq SCTX_R13(%rsi), %r13 - movq SCTX_R14(%rsi), %r14 - movq SCTX_R15(%rsi), %r15 - movq SCTX_RBP(%rsi), %rbp - movq SCTX_RBX(%rsi), %rbx - movq SCTX_RCX(%rsi), %rcx - movq SCTX_RDX(%rsi), %rdx - movq SCTX_RDI(%rsi), %rdi - movq SCTX_RSI(%rsi), %rsi /* %rsi must be restored last */ - - VMLOAD - VMRUN - VMSAVE - - pop %rax /* pop guest context pointer from the stack */ - - /* - * Save guest state. - */ - movq %r8, SCTX_R8(%rax) - movq %r9, SCTX_R9(%rax) - movq %r10, SCTX_R10(%rax) - movq %r11, SCTX_R11(%rax) - movq %r12, SCTX_R12(%rax) - movq %r13, SCTX_R13(%rax) - movq %r14, SCTX_R14(%rax) - movq %r15, SCTX_R15(%rax) - movq %rbp, SCTX_RBP(%rax) - movq %rbx, SCTX_RBX(%rax) - movq %rcx, SCTX_RCX(%rax) - movq %rdx, SCTX_RDX(%rax) - movq %rdi, SCTX_RDI(%rax) - movq %rsi, SCTX_RSI(%rax) - - /* Restore host state */ - pop %r15 - pop %r14 - pop %r13 - pop %r12 - pop %rbx - - VLEAVE - ret -END(svm_launch) diff --git a/vmm/amd/vmcb.c b/vmm/amd/vmcb.c deleted file mode 100644 index d860169..0000000 --- a/vmm/amd/vmcb.c +++ /dev/null @@ -1,442 +0,0 @@ -/*- - * Copyright (c) 2013 Anish Gupta (akgupt3@gmail.com) - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice unmodified, this list of conditions, and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include - -#include -#include -#include - -#include "vmm_ktr.h" - -#include "vmcb.h" -#include "svm.h" -#include "svm_softc.h" - -/* - * The VMCB aka Virtual Machine Control Block is a 4KB aligned page - * in memory that describes the virtual machine. - * - * The VMCB contains: - * - instructions or events in the guest to intercept - * - control bits that modify execution environment of the guest - * - guest processor state (e.g. general purpose registers) - */ - -/* - * Return VMCB segment area. - */ -static struct vmcb_segment * -vmcb_segptr(struct vmcb *vmcb, int type) -{ - struct vmcb_state *state; - struct vmcb_segment *seg; - - state = &vmcb->state; - - switch (type) { - case VM_REG_GUEST_CS: - seg = &state->cs; - break; - - case VM_REG_GUEST_DS: - seg = &state->ds; - break; - - case VM_REG_GUEST_ES: - seg = &state->es; - break; - - case VM_REG_GUEST_FS: - seg = &state->fs; - break; - - case VM_REG_GUEST_GS: - seg = &state->gs; - break; - - case VM_REG_GUEST_SS: - seg = &state->ss; - break; - - case VM_REG_GUEST_GDTR: - seg = &state->gdt; - break; - - case VM_REG_GUEST_IDTR: - seg = &state->idt; - break; - - case VM_REG_GUEST_LDTR: - seg = &state->ldt; - break; - - case VM_REG_GUEST_TR: - seg = &state->tr; - break; - - default: - seg = NULL; - break; - } - - return (seg); -} - -static int -vmcb_access(struct svm_softc *softc, int vcpu, int write, int ident, - uint64_t *val) -{ - struct vmcb *vmcb; - int off, bytes; - char *ptr; - - vmcb = svm_get_vmcb(softc, vcpu); - off = VMCB_ACCESS_OFFSET(ident); - bytes = VMCB_ACCESS_BYTES(ident); - - if ((off + bytes) >= sizeof (struct vmcb)) - return (EINVAL); - - ptr = (char *)vmcb; - - if (!write) - *val = 0; - - switch (bytes) { - case 8: - case 4: - case 2: - if (write) - memcpy(ptr + off, val, bytes); - else - memcpy(val, ptr + off, bytes); - break; - default: - VCPU_CTR1(softc->vm, vcpu, - "Invalid size %d for VMCB access: %d", bytes); - return (EINVAL); - } - - /* Invalidate all VMCB state cached by h/w. */ - if (write) - svm_set_dirty(softc, vcpu, 0xffffffff); - - return (0); -} - -/* - * Read from segment selector, control and general purpose register of VMCB. - */ -int -vmcb_read(struct svm_softc *sc, int vcpu, int ident, uint64_t *retval) -{ - struct vmcb *vmcb; - struct vmcb_state *state; - struct vmcb_segment *seg; - int err; - - vmcb = svm_get_vmcb(sc, vcpu); - state = &vmcb->state; - err = 0; - - if (VMCB_ACCESS_OK(ident)) - return (vmcb_access(sc, vcpu, 0, ident, retval)); - - switch (ident) { - case VM_REG_GUEST_CR0: - *retval = state->cr0; - break; - - case VM_REG_GUEST_CR2: - *retval = state->cr2; - break; - - case VM_REG_GUEST_CR3: - *retval = state->cr3; - break; - - case VM_REG_GUEST_CR4: - *retval = state->cr4; - break; - - case VM_REG_GUEST_DR7: - *retval = state->dr7; - break; - - case VM_REG_GUEST_EFER: - *retval = state->efer; - break; - - case VM_REG_GUEST_RAX: - *retval = state->rax; - break; - - case VM_REG_GUEST_RFLAGS: - *retval = state->rflags; - break; - - case VM_REG_GUEST_RIP: - *retval = state->rip; - break; - - case VM_REG_GUEST_RSP: - *retval = state->rsp; - break; - - case VM_REG_GUEST_CS: - case VM_REG_GUEST_DS: - case VM_REG_GUEST_ES: - case VM_REG_GUEST_FS: - case VM_REG_GUEST_GS: - case VM_REG_GUEST_SS: - case VM_REG_GUEST_LDTR: - case VM_REG_GUEST_TR: - seg = vmcb_segptr(vmcb, ident); - KASSERT(seg != NULL, ("%s: unable to get segment %d from VMCB", - __func__, ident)); - *retval = seg->selector; - break; - - case VM_REG_GUEST_GDTR: - case VM_REG_GUEST_IDTR: - /* GDTR and IDTR don't have segment selectors */ - err = EINVAL; - break; - default: - err = EINVAL; - break; - } - - return (err); -} - -/* - * Write to segment selector, control and general purpose register of VMCB. - */ -int -vmcb_write(struct svm_softc *sc, int vcpu, int ident, uint64_t val) -{ - struct vmcb *vmcb; - struct vmcb_state *state; - struct vmcb_segment *seg; - int err, dirtyseg; - - vmcb = svm_get_vmcb(sc, vcpu); - state = &vmcb->state; - dirtyseg = 0; - err = 0; - - if (VMCB_ACCESS_OK(ident)) - return (vmcb_access(sc, vcpu, 1, ident, &val)); - - switch (ident) { - case VM_REG_GUEST_CR0: - state->cr0 = val; - svm_set_dirty(sc, vcpu, VMCB_CACHE_CR); - break; - - case VM_REG_GUEST_CR2: - state->cr2 = val; - svm_set_dirty(sc, vcpu, VMCB_CACHE_CR2); - break; - - case VM_REG_GUEST_CR3: - state->cr3 = val; - svm_set_dirty(sc, vcpu, VMCB_CACHE_CR); - break; - - case VM_REG_GUEST_CR4: - state->cr4 = val; - svm_set_dirty(sc, vcpu, VMCB_CACHE_CR); - break; - - case VM_REG_GUEST_DR7: - state->dr7 = val; - break; - - case VM_REG_GUEST_EFER: - /* EFER_SVM must always be set when the guest is executing */ - state->efer = val | EFER_SVM; - svm_set_dirty(sc, vcpu, VMCB_CACHE_CR); - break; - - case VM_REG_GUEST_RAX: - state->rax = val; - break; - - case VM_REG_GUEST_RFLAGS: - state->rflags = val; - break; - - case VM_REG_GUEST_RIP: - state->rip = val; - break; - - case VM_REG_GUEST_RSP: - state->rsp = val; - break; - - case VM_REG_GUEST_CS: - case VM_REG_GUEST_DS: - case VM_REG_GUEST_ES: - case VM_REG_GUEST_SS: - dirtyseg = 1; /* FALLTHROUGH */ - case VM_REG_GUEST_FS: - case VM_REG_GUEST_GS: - case VM_REG_GUEST_LDTR: - case VM_REG_GUEST_TR: - seg = vmcb_segptr(vmcb, ident); - KASSERT(seg != NULL, ("%s: unable to get segment %d from VMCB", - __func__, ident)); - seg->selector = val; - if (dirtyseg) - svm_set_dirty(sc, vcpu, VMCB_CACHE_SEG); - break; - - case VM_REG_GUEST_GDTR: - case VM_REG_GUEST_IDTR: - /* GDTR and IDTR don't have segment selectors */ - err = EINVAL; - break; - default: - err = EINVAL; - break; - } - - return (err); -} - -int -vmcb_seg(struct vmcb *vmcb, int ident, struct vmcb_segment *seg2) -{ - struct vmcb_segment *seg; - - seg = vmcb_segptr(vmcb, ident); - if (seg != NULL) { - bcopy(seg, seg2, sizeof(struct vmcb_segment)); - return (0); - } else { - return (EINVAL); - } -} - -int -vmcb_setdesc(void *arg, int vcpu, int reg, struct seg_desc *desc) -{ - struct vmcb *vmcb; - struct svm_softc *sc; - struct vmcb_segment *seg; - uint16_t attrib; - - sc = arg; - vmcb = svm_get_vmcb(sc, vcpu); - - seg = vmcb_segptr(vmcb, reg); - KASSERT(seg != NULL, ("%s: invalid segment descriptor %d", - __func__, reg)); - - seg->base = desc->base; - seg->limit = desc->limit; - if (reg != VM_REG_GUEST_GDTR && reg != VM_REG_GUEST_IDTR) { - /* - * Map seg_desc access to VMCB attribute format. - * - * SVM uses the 'P' bit in the segment attributes to indicate a - * NULL segment so clear it if the segment is marked unusable. - */ - attrib = ((desc->access & 0xF000) >> 4) | (desc->access & 0xFF); - if (SEG_DESC_UNUSABLE(desc->access)) { - attrib &= ~0x80; - } - seg->attrib = attrib; - } - - VCPU_CTR4(sc->vm, vcpu, "Setting desc %d: base (%#lx), limit (%#x), " - "attrib (%#x)", reg, seg->base, seg->limit, seg->attrib); - - switch (reg) { - case VM_REG_GUEST_CS: - case VM_REG_GUEST_DS: - case VM_REG_GUEST_ES: - case VM_REG_GUEST_SS: - svm_set_dirty(sc, vcpu, VMCB_CACHE_SEG); - break; - case VM_REG_GUEST_GDTR: - case VM_REG_GUEST_IDTR: - svm_set_dirty(sc, vcpu, VMCB_CACHE_DT); - break; - default: - break; - } - - return (0); -} - -int -vmcb_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc) -{ - struct vmcb *vmcb; - struct svm_softc *sc; - struct vmcb_segment *seg; - - sc = arg; - vmcb = svm_get_vmcb(sc, vcpu); - seg = vmcb_segptr(vmcb, reg); - KASSERT(seg != NULL, ("%s: invalid segment descriptor %d", - __func__, reg)); - - desc->base = seg->base; - desc->limit = seg->limit; - desc->access = 0; - - if (reg != VM_REG_GUEST_GDTR && reg != VM_REG_GUEST_IDTR) { - /* Map seg_desc access to VMCB attribute format */ - desc->access = ((seg->attrib & 0xF00) << 4) | - (seg->attrib & 0xFF); - - /* - * VT-x uses bit 16 to indicate a segment that has been loaded - * with a NULL selector (aka unusable). The 'desc->access' - * field is interpreted in the VT-x format by the - * processor-independent code. - * - * SVM uses the 'P' bit to convey the same information so - * convert it into the VT-x format. For more details refer to - * section "Segment State in the VMCB" in APMv2. - */ - if (reg != VM_REG_GUEST_CS && reg != VM_REG_GUEST_TR) { - if ((desc->access & 0x80) == 0) - desc->access |= 0x10000; /* Unusable segment */ - } - } - - return (0); -} diff --git a/vmm/amd/vmcb.h b/vmm/amd/vmcb.h deleted file mode 100644 index 496f880..0000000 --- a/vmm/amd/vmcb.h +++ /dev/null @@ -1,334 +0,0 @@ -/*- - * Copyright (c) 2013 Anish Gupta (akgupt3@gmail.com) - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice unmodified, this list of conditions, and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#ifndef _VMCB_H_ -#define _VMCB_H_ - -struct svm_softc; - -#define BIT(n) (1ULL << n) - -/* - * Secure Virtual Machine: AMD64 Programmer's Manual Vol2, Chapter 15 - * Layout of VMCB: AMD64 Programmer's Manual Vol2, Appendix B - */ - -/* vmcb_ctrl->intercept[] array indices */ -#define VMCB_CR_INTCPT 0 -#define VMCB_DR_INTCPT 1 -#define VMCB_EXC_INTCPT 2 -#define VMCB_CTRL1_INTCPT 3 -#define VMCB_CTRL2_INTCPT 4 - -/* intercept[VMCB_CTRL1_INTCPT] fields */ -#define VMCB_INTCPT_INTR BIT(0) -#define VMCB_INTCPT_NMI BIT(1) -#define VMCB_INTCPT_SMI BIT(2) -#define VMCB_INTCPT_INIT BIT(3) -#define VMCB_INTCPT_VINTR BIT(4) -#define VMCB_INTCPT_CR0_WRITE BIT(5) -#define VMCB_INTCPT_IDTR_READ BIT(6) -#define VMCB_INTCPT_GDTR_READ BIT(7) -#define VMCB_INTCPT_LDTR_READ BIT(8) -#define VMCB_INTCPT_TR_READ BIT(9) -#define VMCB_INTCPT_IDTR_WRITE BIT(10) -#define VMCB_INTCPT_GDTR_WRITE BIT(11) -#define VMCB_INTCPT_LDTR_WRITE BIT(12) -#define VMCB_INTCPT_TR_WRITE BIT(13) -#define VMCB_INTCPT_RDTSC BIT(14) -#define VMCB_INTCPT_RDPMC BIT(15) -#define VMCB_INTCPT_PUSHF BIT(16) -#define VMCB_INTCPT_POPF BIT(17) -#define VMCB_INTCPT_CPUID BIT(18) -#define VMCB_INTCPT_RSM BIT(19) -#define VMCB_INTCPT_IRET BIT(20) -#define VMCB_INTCPT_INTn BIT(21) -#define VMCB_INTCPT_INVD BIT(22) -#define VMCB_INTCPT_PAUSE BIT(23) -#define VMCB_INTCPT_HLT BIT(24) -#define VMCB_INTCPT_INVPG BIT(25) -#define VMCB_INTCPT_INVPGA BIT(26) -#define VMCB_INTCPT_IO BIT(27) -#define VMCB_INTCPT_MSR BIT(28) -#define VMCB_INTCPT_TASK_SWITCH BIT(29) -#define VMCB_INTCPT_FERR_FREEZE BIT(30) -#define VMCB_INTCPT_SHUTDOWN BIT(31) - -/* intercept[VMCB_CTRL2_INTCPT] fields */ -#define VMCB_INTCPT_VMRUN BIT(0) -#define VMCB_INTCPT_VMMCALL BIT(1) -#define VMCB_INTCPT_VMLOAD BIT(2) -#define VMCB_INTCPT_VMSAVE BIT(3) -#define VMCB_INTCPT_STGI BIT(4) -#define VMCB_INTCPT_CLGI BIT(5) -#define VMCB_INTCPT_SKINIT BIT(6) -#define VMCB_INTCPT_RDTSCP BIT(7) -#define VMCB_INTCPT_ICEBP BIT(8) -#define VMCB_INTCPT_WBINVD BIT(9) -#define VMCB_INTCPT_MONITOR BIT(10) -#define VMCB_INTCPT_MWAIT BIT(11) -#define VMCB_INTCPT_MWAIT_ARMED BIT(12) -#define VMCB_INTCPT_XSETBV BIT(13) - -/* VMCB TLB control */ -#define VMCB_TLB_FLUSH_NOTHING 0 /* Flush nothing */ -#define VMCB_TLB_FLUSH_ALL 1 /* Flush entire TLB */ -#define VMCB_TLB_FLUSH_GUEST 3 /* Flush all guest entries */ -#define VMCB_TLB_FLUSH_GUEST_NONGLOBAL 7 /* Flush guest non-PG entries */ - -/* VMCB state caching */ -#define VMCB_CACHE_NONE 0 /* No caching */ -#define VMCB_CACHE_I BIT(0) /* Intercept, TSC off, Pause filter */ -#define VMCB_CACHE_IOPM BIT(1) /* I/O and MSR permission */ -#define VMCB_CACHE_ASID BIT(2) /* ASID */ -#define VMCB_CACHE_TPR BIT(3) /* V_TPR to V_INTR_VECTOR */ -#define VMCB_CACHE_NP BIT(4) /* Nested Paging */ -#define VMCB_CACHE_CR BIT(5) /* CR0, CR3, CR4 & EFER */ -#define VMCB_CACHE_DR BIT(6) /* Debug registers */ -#define VMCB_CACHE_DT BIT(7) /* GDT/IDT */ -#define VMCB_CACHE_SEG BIT(8) /* User segments, CPL */ -#define VMCB_CACHE_CR2 BIT(9) /* page fault address */ -#define VMCB_CACHE_LBR BIT(10) /* Last branch */ - -/* VMCB control event injection */ -#define VMCB_EVENTINJ_EC_VALID BIT(11) /* Error Code valid */ -#define VMCB_EVENTINJ_VALID BIT(31) /* Event valid */ - -/* Event types that can be injected */ -#define VMCB_EVENTINJ_TYPE_INTR 0 -#define VMCB_EVENTINJ_TYPE_NMI 2 -#define VMCB_EVENTINJ_TYPE_EXCEPTION 3 -#define VMCB_EVENTINJ_TYPE_INTn 4 - -/* VMCB exit code, APM vol2 Appendix C */ -#define VMCB_EXIT_MC 0x52 -#define VMCB_EXIT_INTR 0x60 -#define VMCB_EXIT_NMI 0x61 -#define VMCB_EXIT_VINTR 0x64 -#define VMCB_EXIT_PUSHF 0x70 -#define VMCB_EXIT_POPF 0x71 -#define VMCB_EXIT_CPUID 0x72 -#define VMCB_EXIT_IRET 0x74 -#define VMCB_EXIT_PAUSE 0x77 -#define VMCB_EXIT_HLT 0x78 -#define VMCB_EXIT_IO 0x7B -#define VMCB_EXIT_MSR 0x7C -#define VMCB_EXIT_SHUTDOWN 0x7F -#define VMCB_EXIT_VMSAVE 0x83 -#define VMCB_EXIT_MONITOR 0x8A -#define VMCB_EXIT_MWAIT 0x8B -#define VMCB_EXIT_NPF 0x400 -#define VMCB_EXIT_INVALID -1 - -/* - * Nested page fault. - * Bit definitions to decode EXITINFO1. - */ -#define VMCB_NPF_INFO1_P BIT(0) /* Nested page present. */ -#define VMCB_NPF_INFO1_W BIT(1) /* Access was write. */ -#define VMCB_NPF_INFO1_U BIT(2) /* Access was user access. */ -#define VMCB_NPF_INFO1_RSV BIT(3) /* Reserved bits present. */ -#define VMCB_NPF_INFO1_ID BIT(4) /* Code read. */ - -#define VMCB_NPF_INFO1_GPA BIT(32) /* Guest physical address. */ -#define VMCB_NPF_INFO1_GPT BIT(33) /* Guest page table. */ - -/* - * EXITINTINFO, Interrupt exit info for all intrecepts. - * Section 15.7.2, Intercepts during IDT Interrupt Delivery. - */ -#define VMCB_EXITINTINFO_VECTOR(x) ((x) & 0xFF) -#define VMCB_EXITINTINFO_TYPE(x) (((x) >> 8) & 0x7) -#define VMCB_EXITINTINFO_EC_VALID(x) (((x) & BIT(11)) ? 1 : 0) -#define VMCB_EXITINTINFO_VALID(x) (((x) & BIT(31)) ? 1 : 0) -#define VMCB_EXITINTINFO_EC(x) (((x) >> 32) & 0xFFFFFFFF) - -/* Offset of various VMCB fields. */ -#define VMCB_OFF_CTRL(x) (x) -#define VMCB_OFF_STATE(x) ((x) + 0x400) - -#define VMCB_OFF_CR_INTERCEPT VMCB_OFF_CTRL(0x0) -#define VMCB_OFF_DR_INTERCEPT VMCB_OFF_CTRL(0x4) -#define VMCB_OFF_EXC_INTERCEPT VMCB_OFF_CTRL(0x8) -#define VMCB_OFF_INST1_INTERCEPT VMCB_OFF_CTRL(0xC) -#define VMCB_OFF_INST2_INTERCEPT VMCB_OFF_CTRL(0x10) -#define VMCB_OFF_IO_PERM VMCB_OFF_CTRL(0x40) -#define VMCB_OFF_MSR_PERM VMCB_OFF_CTRL(0x48) -#define VMCB_OFF_TSC_OFFSET VMCB_OFF_CTRL(0x50) -#define VMCB_OFF_ASID VMCB_OFF_CTRL(0x58) -#define VMCB_OFF_TLB_CTRL VMCB_OFF_CTRL(0x5C) -#define VMCB_OFF_VIRQ VMCB_OFF_CTRL(0x60) -#define VMCB_OFF_EXIT_REASON VMCB_OFF_CTRL(0x70) -#define VMCB_OFF_EXITINFO1 VMCB_OFF_CTRL(0x78) -#define VMCB_OFF_EXITINFO2 VMCB_OFF_CTRL(0x80) -#define VMCB_OFF_EXITINTINFO VMCB_OFF_CTRL(0x88) -#define VMCB_OFF_AVIC_BAR VMCB_OFF_CTRL(0x98) -#define VMCB_OFF_NPT_BASE VMCB_OFF_CTRL(0xB0) -#define VMCB_OFF_AVIC_PAGE VMCB_OFF_CTRL(0xE0) -#define VMCB_OFF_AVIC_LT VMCB_OFF_CTRL(0xF0) -#define VMCB_OFF_AVIC_PT VMCB_OFF_CTRL(0xF8) -#define VMCB_OFF_SYSENTER_CS VMCB_OFF_STATE(0x228) -#define VMCB_OFF_SYSENTER_ESP VMCB_OFF_STATE(0x230) -#define VMCB_OFF_SYSENTER_EIP VMCB_OFF_STATE(0x238) -#define VMCB_OFF_GUEST_PAT VMCB_OFF_STATE(0x268) - -/* - * Encode the VMCB offset and bytes that we want to read from VMCB. - */ -#define VMCB_ACCESS(o, w) (0x80000000 | (((w) & 0xF) << 16) | \ - ((o) & 0xFFF)) -#define VMCB_ACCESS_OK(v) ((v) & 0x80000000 ) -#define VMCB_ACCESS_BYTES(v) (((v) >> 16) & 0xF) -#define VMCB_ACCESS_OFFSET(v) ((v) & 0xFFF) - -#ifdef _KERNEL -/* VMCB save state area segment format */ -struct vmcb_segment { - uint16_t selector; - uint16_t attrib; - uint32_t limit; - uint64_t base; -} __attribute__ ((__packed__)); -CTASSERT(sizeof(struct vmcb_segment) == 16); - -/* Code segment descriptor attribute in 12 bit format as saved by VMCB. */ -#define VMCB_CS_ATTRIB_L BIT(9) /* Long mode. */ -#define VMCB_CS_ATTRIB_D BIT(10) /* OPerand size bit. */ - -/* - * The VMCB is divided into two areas - the first one contains various - * control bits including the intercept vector and the second one contains - * the guest state. - */ - -/* VMCB control area - padded up to 1024 bytes */ -struct vmcb_ctrl { - uint32_t intercept[5]; /* all intercepts */ - uint8_t pad1[0x28]; /* Offsets 0x14-0x3B are reserved. */ - uint16_t pause_filthresh; /* Offset 0x3C, PAUSE filter threshold */ - uint16_t pause_filcnt; /* Offset 0x3E, PAUSE filter count */ - uint64_t iopm_base_pa; /* 0x40: IOPM_BASE_PA */ - uint64_t msrpm_base_pa; /* 0x48: MSRPM_BASE_PA */ - uint64_t tsc_offset; /* 0x50: TSC_OFFSET */ - uint32_t asid; /* 0x58: Guest ASID */ - uint8_t tlb_ctrl; /* 0x5C: TLB_CONTROL */ - uint8_t pad2[3]; /* 0x5D-0x5F: Reserved. */ - uint8_t v_tpr; /* 0x60: V_TPR, guest CR8 */ - uint8_t v_irq:1; /* Is virtual interrupt pending? */ - uint8_t :7; /* Padding */ - uint8_t v_intr_prio:4; /* 0x62: Priority for virtual interrupt. */ - uint8_t v_ign_tpr:1; - uint8_t :3; - uint8_t v_intr_masking:1; /* Guest and host sharing of RFLAGS. */ - uint8_t :7; - uint8_t v_intr_vector; /* 0x65: Vector for virtual interrupt. */ - uint8_t pad3[3]; /* Bit64-40 Reserved. */ - uint64_t intr_shadow:1; /* 0x68: Interrupt shadow, section15.2.1 APM2 */ - uint64_t :63; - uint64_t exitcode; /* 0x70, Exitcode */ - uint64_t exitinfo1; /* 0x78, EXITINFO1 */ - uint64_t exitinfo2; /* 0x80, EXITINFO2 */ - uint64_t exitintinfo; /* 0x88, Interrupt exit value. */ - uint64_t np_enable:1; /* 0x90, Nested paging enable. */ - uint64_t :63; - uint8_t pad4[0x10]; /* 0x98-0xA7 reserved. */ - uint64_t eventinj; /* 0xA8, Event injection. */ - uint64_t n_cr3; /* B0, Nested page table. */ - uint64_t lbr_virt_en:1; /* Enable LBR virtualization. */ - uint64_t :63; - uint32_t vmcb_clean; /* 0xC0: VMCB clean bits for caching */ - uint32_t :32; /* 0xC4: Reserved */ - uint64_t nrip; /* 0xC8: Guest next nRIP. */ - uint8_t inst_len; /* 0xD0: #NPF decode assist */ - uint8_t inst_bytes[15]; - uint8_t padd6[0x320]; -} __attribute__ ((__packed__)); -CTASSERT(sizeof(struct vmcb_ctrl) == 1024); - -struct vmcb_state { - struct vmcb_segment es; - struct vmcb_segment cs; - struct vmcb_segment ss; - struct vmcb_segment ds; - struct vmcb_segment fs; - struct vmcb_segment gs; - struct vmcb_segment gdt; - struct vmcb_segment ldt; - struct vmcb_segment idt; - struct vmcb_segment tr; - uint8_t pad1[0x2b]; /* Reserved: 0xA0-0xCA */ - uint8_t cpl; - uint8_t pad2[4]; - uint64_t efer; - uint8_t pad3[0x70]; /* Reserved: 0xd8-0x147 */ - uint64_t cr4; - uint64_t cr3; /* Guest CR3 */ - uint64_t cr0; - uint64_t dr7; - uint64_t dr6; - uint64_t rflags; - uint64_t rip; - uint8_t pad4[0x58]; /* Reserved: 0x180-0x1D7 */ - uint64_t rsp; - uint8_t pad5[0x18]; /* Reserved 0x1E0-0x1F7 */ - uint64_t rax; - uint64_t star; - uint64_t lstar; - uint64_t cstar; - uint64_t sfmask; - uint64_t kernelgsbase; - uint64_t sysenter_cs; - uint64_t sysenter_esp; - uint64_t sysenter_eip; - uint64_t cr2; - uint8_t pad6[0x20]; - uint64_t g_pat; - uint64_t dbgctl; - uint64_t br_from; - uint64_t br_to; - uint64_t int_from; - uint64_t int_to; - uint8_t pad7[0x968]; /* Reserved upto end of VMCB */ -} __attribute__ ((__packed__)); -CTASSERT(sizeof(struct vmcb_state) == 0xC00); - -struct vmcb { - struct vmcb_ctrl ctrl; - struct vmcb_state state; -} __attribute__ ((__packed__)); -CTASSERT(sizeof(struct vmcb) == PAGE_SIZE); -CTASSERT(offsetof(struct vmcb, state) == 0x400); - -int vmcb_read(struct svm_softc *sc, int vcpu, int ident, uint64_t *retval); -int vmcb_write(struct svm_softc *sc, int vcpu, int ident, uint64_t val); -int vmcb_setdesc(void *arg, int vcpu, int ident, struct seg_desc *desc); -int vmcb_getdesc(void *arg, int vcpu, int ident, struct seg_desc *desc); -int vmcb_seg(struct vmcb *vmcb, int ident, struct vmcb_segment *seg); - -#endif /* _KERNEL */ -#endif /* _VMCB_H_ */ diff --git a/vmm/intel/ept.c b/vmm/intel/ept.c deleted file mode 100644 index 54320cb..0000000 --- a/vmm/intel/ept.c +++ /dev/null @@ -1,205 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include - -#include "vmx_cpufunc.h" -#include "ept.h" - -#define EPT_SUPPORTS_EXEC_ONLY(cap) ((cap) & (1UL << 0)) -#define EPT_PWL4(cap) ((cap) & (1UL << 6)) -#define EPT_MEMORY_TYPE_WB(cap) ((cap) & (1UL << 14)) -#define EPT_PDE_SUPERPAGE(cap) ((cap) & (1UL << 16)) /* 2MB pages */ -#define EPT_PDPTE_SUPERPAGE(cap) ((cap) & (1UL << 17)) /* 1GB pages */ -#define INVEPT_SUPPORTED(cap) ((cap) & (1UL << 20)) -#define AD_BITS_SUPPORTED(cap) ((cap) & (1UL << 21)) -#define INVVPID_SUPPORTED(cap) ((cap) & (1UL << 32)) - -#define INVVPID_ALL_TYPES_MASK 0xF0000000000UL -#define INVVPID_ALL_TYPES_SUPPORTED(cap) \ - (((cap) & INVVPID_ALL_TYPES_MASK) == INVVPID_ALL_TYPES_MASK) - -#define INVEPT_ALL_TYPES_MASK 0x6000000UL -#define INVEPT_ALL_TYPES_SUPPORTED(cap) \ - (((cap) & INVEPT_ALL_TYPES_MASK) == INVEPT_ALL_TYPES_MASK) - -#define EPT_PWLEVELS 4 /* page walk levels */ -#define EPT_ENABLE_AD_BITS (1 << 6) - -SYSCTL_DECL(_hw_vmm); -SYSCTL_NODE(_hw_vmm, OID_AUTO, ept, CTLFLAG_RW, NULL, NULL); - -static int ept_enable_ad_bits; - -static int ept_pmap_flags; -SYSCTL_INT(_hw_vmm_ept, OID_AUTO, pmap_flags, CTLFLAG_RD, - &ept_pmap_flags, 0, NULL); - -int -ept_init(int ipinum) -{ - int use_hw_ad_bits, use_superpages, use_exec_only; - uint64_t cap; - - cap = rdmsr(MSR_VMX_EPT_VPID_CAP); - - /* - * Verify that: - * - page walk length is 4 steps - * - extended page tables can be laid out in write-back memory - * - invvpid instruction with all possible types is supported - * - invept instruction with all possible types is supported - */ - if (!EPT_PWL4(cap) || - !EPT_MEMORY_TYPE_WB(cap) || - !INVVPID_SUPPORTED(cap) || - !INVVPID_ALL_TYPES_SUPPORTED(cap) || - !INVEPT_SUPPORTED(cap) || - !INVEPT_ALL_TYPES_SUPPORTED(cap)) - return (EINVAL); - - ept_pmap_flags = ipinum & PMAP_NESTED_IPIMASK; - - use_superpages = 1; - TUNABLE_INT_FETCH("hw.vmm.ept.use_superpages", &use_superpages); - if (use_superpages && EPT_PDE_SUPERPAGE(cap)) - ept_pmap_flags |= PMAP_PDE_SUPERPAGE; /* 2MB superpage */ - - use_hw_ad_bits = 1; - TUNABLE_INT_FETCH("hw.vmm.ept.use_hw_ad_bits", &use_hw_ad_bits); - if (use_hw_ad_bits && AD_BITS_SUPPORTED(cap)) - ept_enable_ad_bits = 1; - else - ept_pmap_flags |= PMAP_EMULATE_AD_BITS; - - use_exec_only = 1; - TUNABLE_INT_FETCH("hw.vmm.ept.use_exec_only", &use_exec_only); - if (use_exec_only && EPT_SUPPORTS_EXEC_ONLY(cap)) - ept_pmap_flags |= PMAP_SUPPORTS_EXEC_ONLY; - - return (0); -} - -#if 0 -static void -ept_dump(uint64_t *ptp, int nlevels) -{ - int i, t, tabs; - uint64_t *ptpnext, ptpval; - - if (--nlevels < 0) - return; - - tabs = 3 - nlevels; - for (t = 0; t < tabs; t++) - printf("\t"); - printf("PTP = %p\n", ptp); - - for (i = 0; i < 512; i++) { - ptpval = ptp[i]; - - if (ptpval == 0) - continue; - - for (t = 0; t < tabs; t++) - printf("\t"); - printf("%3d 0x%016lx\n", i, ptpval); - - if (nlevels != 0 && (ptpval & EPT_PG_SUPERPAGE) == 0) { - ptpnext = (uint64_t *) - PHYS_TO_DMAP(ptpval & EPT_ADDR_MASK); - ept_dump(ptpnext, nlevels); - } - } -} -#endif - -static void -invept_single_context(void *arg) -{ - struct invept_desc desc = *(struct invept_desc *)arg; - - invept(INVEPT_TYPE_SINGLE_CONTEXT, desc); -} - -void -ept_invalidate_mappings(u_long eptp) -{ - struct invept_desc invept_desc = { 0 }; - - invept_desc.eptp = eptp; - - smp_rendezvous(NULL, invept_single_context, NULL, &invept_desc); -} - -static int -ept_pinit(pmap_t pmap) -{ - - return (pmap_pinit_type(pmap, PT_EPT, ept_pmap_flags)); -} - -struct vmspace * -ept_vmspace_alloc(vm_offset_t min, vm_offset_t max) -{ - - return (vmspace_alloc(min, max, ept_pinit)); -} - -void -ept_vmspace_free(struct vmspace *vmspace) -{ - - vmspace_free(vmspace); -} - -uint64_t -eptp(uint64_t pml4) -{ - uint64_t eptp_val; - - eptp_val = pml4 | (EPT_PWLEVELS - 1) << 3 | PAT_WRITE_BACK; - if (ept_enable_ad_bits) - eptp_val |= EPT_ENABLE_AD_BITS; - - return (eptp_val); -} diff --git a/vmm/intel/vmcs.c b/vmm/intel/vmcs.c deleted file mode 100644 index 5962526..0000000 --- a/vmm/intel/vmcs.c +++ /dev/null @@ -1,503 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include "opt_ddb.h" - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include - -#include -#include - -#include -#include -#include "vmm_host.h" -#include "vmx_cpufunc.h" -#include "vmcs.h" -#include "ept.h" -#include "vmx.h" - -#ifdef DDB -#include -#endif - -static uint64_t -vmcs_fix_regval(uint32_t encoding, uint64_t val) -{ - - switch (encoding) { - case VMCS_GUEST_CR0: - val = vmx_fix_cr0(val); - break; - case VMCS_GUEST_CR4: - val = vmx_fix_cr4(val); - break; - default: - break; - } - return (val); -} - -static uint32_t -vmcs_field_encoding(int ident) -{ - switch (ident) { - case VM_REG_GUEST_CR0: - return (VMCS_GUEST_CR0); - case VM_REG_GUEST_CR3: - return (VMCS_GUEST_CR3); - case VM_REG_GUEST_CR4: - return (VMCS_GUEST_CR4); - case VM_REG_GUEST_DR7: - return (VMCS_GUEST_DR7); - case VM_REG_GUEST_RSP: - return (VMCS_GUEST_RSP); - case VM_REG_GUEST_RIP: - return (VMCS_GUEST_RIP); - case VM_REG_GUEST_RFLAGS: - return (VMCS_GUEST_RFLAGS); - case VM_REG_GUEST_ES: - return (VMCS_GUEST_ES_SELECTOR); - case VM_REG_GUEST_CS: - return (VMCS_GUEST_CS_SELECTOR); - case VM_REG_GUEST_SS: - return (VMCS_GUEST_SS_SELECTOR); - case VM_REG_GUEST_DS: - return (VMCS_GUEST_DS_SELECTOR); - case VM_REG_GUEST_FS: - return (VMCS_GUEST_FS_SELECTOR); - case VM_REG_GUEST_GS: - return (VMCS_GUEST_GS_SELECTOR); - case VM_REG_GUEST_TR: - return (VMCS_GUEST_TR_SELECTOR); - case VM_REG_GUEST_LDTR: - return (VMCS_GUEST_LDTR_SELECTOR); - case VM_REG_GUEST_EFER: - return (VMCS_GUEST_IA32_EFER); - case VM_REG_GUEST_PDPTE0: - return (VMCS_GUEST_PDPTE0); - case VM_REG_GUEST_PDPTE1: - return (VMCS_GUEST_PDPTE1); - case VM_REG_GUEST_PDPTE2: - return (VMCS_GUEST_PDPTE2); - case VM_REG_GUEST_PDPTE3: - return (VMCS_GUEST_PDPTE3); - default: - return (-1); - } - -} - -static int -vmcs_seg_desc_encoding(int seg, uint32_t *base, uint32_t *lim, uint32_t *acc) -{ - - switch (seg) { - case VM_REG_GUEST_ES: - *base = VMCS_GUEST_ES_BASE; - *lim = VMCS_GUEST_ES_LIMIT; - *acc = VMCS_GUEST_ES_ACCESS_RIGHTS; - break; - case VM_REG_GUEST_CS: - *base = VMCS_GUEST_CS_BASE; - *lim = VMCS_GUEST_CS_LIMIT; - *acc = VMCS_GUEST_CS_ACCESS_RIGHTS; - break; - case VM_REG_GUEST_SS: - *base = VMCS_GUEST_SS_BASE; - *lim = VMCS_GUEST_SS_LIMIT; - *acc = VMCS_GUEST_SS_ACCESS_RIGHTS; - break; - case VM_REG_GUEST_DS: - *base = VMCS_GUEST_DS_BASE; - *lim = VMCS_GUEST_DS_LIMIT; - *acc = VMCS_GUEST_DS_ACCESS_RIGHTS; - break; - case VM_REG_GUEST_FS: - *base = VMCS_GUEST_FS_BASE; - *lim = VMCS_GUEST_FS_LIMIT; - *acc = VMCS_GUEST_FS_ACCESS_RIGHTS; - break; - case VM_REG_GUEST_GS: - *base = VMCS_GUEST_GS_BASE; - *lim = VMCS_GUEST_GS_LIMIT; - *acc = VMCS_GUEST_GS_ACCESS_RIGHTS; - break; - case VM_REG_GUEST_TR: - *base = VMCS_GUEST_TR_BASE; - *lim = VMCS_GUEST_TR_LIMIT; - *acc = VMCS_GUEST_TR_ACCESS_RIGHTS; - break; - case VM_REG_GUEST_LDTR: - *base = VMCS_GUEST_LDTR_BASE; - *lim = VMCS_GUEST_LDTR_LIMIT; - *acc = VMCS_GUEST_LDTR_ACCESS_RIGHTS; - break; - case VM_REG_GUEST_IDTR: - *base = VMCS_GUEST_IDTR_BASE; - *lim = VMCS_GUEST_IDTR_LIMIT; - *acc = VMCS_INVALID_ENCODING; - break; - case VM_REG_GUEST_GDTR: - *base = VMCS_GUEST_GDTR_BASE; - *lim = VMCS_GUEST_GDTR_LIMIT; - *acc = VMCS_INVALID_ENCODING; - break; - default: - return (EINVAL); - } - - return (0); -} - -int -vmcs_getreg(struct vmcs *vmcs, int running, int ident, uint64_t *retval) -{ - int error; - uint32_t encoding; - - /* - * If we need to get at vmx-specific state in the VMCS we can bypass - * the translation of 'ident' to 'encoding' by simply setting the - * sign bit. As it so happens the upper 16 bits are reserved (i.e - * set to 0) in the encodings for the VMCS so we are free to use the - * sign bit. - */ - if (ident < 0) - encoding = ident & 0x7fffffff; - else - encoding = vmcs_field_encoding(ident); - - if (encoding == (uint32_t)-1) - return (EINVAL); - - if (!running) - VMPTRLD(vmcs); - - error = vmread(encoding, retval); - - if (!running) - VMCLEAR(vmcs); - - return (error); -} - -int -vmcs_setreg(struct vmcs *vmcs, int running, int ident, uint64_t val) -{ - int error; - uint32_t encoding; - - if (ident < 0) - encoding = ident & 0x7fffffff; - else - encoding = vmcs_field_encoding(ident); - - if (encoding == (uint32_t)-1) - return (EINVAL); - - val = vmcs_fix_regval(encoding, val); - - if (!running) - VMPTRLD(vmcs); - - error = vmwrite(encoding, val); - - if (!running) - VMCLEAR(vmcs); - - return (error); -} - -int -vmcs_setdesc(struct vmcs *vmcs, int running, int seg, struct seg_desc *desc) -{ - int error; - uint32_t base, limit, access; - - error = vmcs_seg_desc_encoding(seg, &base, &limit, &access); - if (error != 0) - panic("vmcs_setdesc: invalid segment register %d", seg); - - if (!running) - VMPTRLD(vmcs); - if ((error = vmwrite(base, desc->base)) != 0) - goto done; - - if ((error = vmwrite(limit, desc->limit)) != 0) - goto done; - - if (access != VMCS_INVALID_ENCODING) { - if ((error = vmwrite(access, desc->access)) != 0) - goto done; - } -done: - if (!running) - VMCLEAR(vmcs); - return (error); -} - -int -vmcs_getdesc(struct vmcs *vmcs, int running, int seg, struct seg_desc *desc) -{ - int error; - uint32_t base, limit, access; - uint64_t u64; - - error = vmcs_seg_desc_encoding(seg, &base, &limit, &access); - if (error != 0) - panic("vmcs_getdesc: invalid segment register %d", seg); - - if (!running) - VMPTRLD(vmcs); - if ((error = vmread(base, &u64)) != 0) - goto done; - desc->base = u64; - - if ((error = vmread(limit, &u64)) != 0) - goto done; - desc->limit = u64; - - if (access != VMCS_INVALID_ENCODING) { - if ((error = vmread(access, &u64)) != 0) - goto done; - desc->access = u64; - } -done: - if (!running) - VMCLEAR(vmcs); - return (error); -} - -int -vmcs_set_msr_save(struct vmcs *vmcs, u_long g_area, u_int g_count) -{ - int error; - - VMPTRLD(vmcs); - - /* - * Guest MSRs are saved in the VM-exit MSR-store area. - * Guest MSRs are loaded from the VM-entry MSR-load area. - * Both areas point to the same location in memory. - */ - if ((error = vmwrite(VMCS_EXIT_MSR_STORE, g_area)) != 0) - goto done; - if ((error = vmwrite(VMCS_EXIT_MSR_STORE_COUNT, g_count)) != 0) - goto done; - - if ((error = vmwrite(VMCS_ENTRY_MSR_LOAD, g_area)) != 0) - goto done; - if ((error = vmwrite(VMCS_ENTRY_MSR_LOAD_COUNT, g_count)) != 0) - goto done; - - error = 0; -done: - VMCLEAR(vmcs); - return (error); -} - -int -vmcs_init(struct vmcs *vmcs) -{ - int error, codesel, datasel, tsssel; - u_long cr0, cr4, efer; - uint64_t pat, fsbase, idtrbase; - - codesel = vmm_get_host_codesel(); - datasel = vmm_get_host_datasel(); - tsssel = vmm_get_host_tsssel(); - - /* - * Make sure we have a "current" VMCS to work with. - */ - VMPTRLD(vmcs); - - /* Host state */ - - /* Initialize host IA32_PAT MSR */ - pat = vmm_get_host_pat(); - if ((error = vmwrite(VMCS_HOST_IA32_PAT, pat)) != 0) - goto done; - - /* Load the IA32_EFER MSR */ - efer = vmm_get_host_efer(); - if ((error = vmwrite(VMCS_HOST_IA32_EFER, efer)) != 0) - goto done; - - /* Load the control registers */ - - cr0 = vmm_get_host_cr0(); - if ((error = vmwrite(VMCS_HOST_CR0, cr0)) != 0) - goto done; - - cr4 = vmm_get_host_cr4() | CR4_VMXE; - if ((error = vmwrite(VMCS_HOST_CR4, cr4)) != 0) - goto done; - - /* Load the segment selectors */ - if ((error = vmwrite(VMCS_HOST_ES_SELECTOR, datasel)) != 0) - goto done; - - if ((error = vmwrite(VMCS_HOST_CS_SELECTOR, codesel)) != 0) - goto done; - - if ((error = vmwrite(VMCS_HOST_SS_SELECTOR, datasel)) != 0) - goto done; - - if ((error = vmwrite(VMCS_HOST_DS_SELECTOR, datasel)) != 0) - goto done; - - if ((error = vmwrite(VMCS_HOST_FS_SELECTOR, datasel)) != 0) - goto done; - - if ((error = vmwrite(VMCS_HOST_GS_SELECTOR, datasel)) != 0) - goto done; - - if ((error = vmwrite(VMCS_HOST_TR_SELECTOR, tsssel)) != 0) - goto done; - - /* - * Load the Base-Address for %fs and idtr. - * - * Note that we exclude %gs, tss and gdtr here because their base - * address is pcpu specific. - */ - fsbase = vmm_get_host_fsbase(); - if ((error = vmwrite(VMCS_HOST_FS_BASE, fsbase)) != 0) - goto done; - - idtrbase = vmm_get_host_idtrbase(); - if ((error = vmwrite(VMCS_HOST_IDTR_BASE, idtrbase)) != 0) - goto done; - - /* instruction pointer */ - if ((error = vmwrite(VMCS_HOST_RIP, (u_long)vmx_exit_guest)) != 0) - goto done; - - /* link pointer */ - if ((error = vmwrite(VMCS_LINK_POINTER, ~0)) != 0) - goto done; -done: - VMCLEAR(vmcs); - return (error); -} - -#ifdef DDB -extern int vmxon_enabled[]; - -DB_SHOW_COMMAND(vmcs, db_show_vmcs) -{ - uint64_t cur_vmcs, val; - uint32_t exit; - - if (!vmxon_enabled[curcpu]) { - db_printf("VMX not enabled\n"); - return; - } - - if (have_addr) { - db_printf("Only current VMCS supported\n"); - return; - } - - vmptrst(&cur_vmcs); - if (cur_vmcs == VMCS_INITIAL) { - db_printf("No current VM context\n"); - return; - } - db_printf("VMCS: %jx\n", cur_vmcs); - db_printf("VPID: %lu\n", vmcs_read(VMCS_VPID)); - db_printf("Activity: "); - val = vmcs_read(VMCS_GUEST_ACTIVITY); - switch (val) { - case 0: - db_printf("Active"); - break; - case 1: - db_printf("HLT"); - break; - case 2: - db_printf("Shutdown"); - break; - case 3: - db_printf("Wait for SIPI"); - break; - default: - db_printf("Unknown: %#lx", val); - } - db_printf("\n"); - exit = vmcs_read(VMCS_EXIT_REASON); - if (exit & 0x80000000) - db_printf("Entry Failure Reason: %u\n", exit & 0xffff); - else - db_printf("Exit Reason: %u\n", exit & 0xffff); - db_printf("Qualification: %#lx\n", vmcs_exit_qualification()); - db_printf("Guest Linear Address: %#lx\n", - vmcs_read(VMCS_GUEST_LINEAR_ADDRESS)); - switch (exit & 0x8000ffff) { - case EXIT_REASON_EXCEPTION: - case EXIT_REASON_EXT_INTR: - val = vmcs_read(VMCS_EXIT_INTR_INFO); - db_printf("Interrupt Type: "); - switch (val >> 8 & 0x7) { - case 0: - db_printf("external"); - break; - case 2: - db_printf("NMI"); - break; - case 3: - db_printf("HW exception"); - break; - case 4: - db_printf("SW exception"); - break; - default: - db_printf("?? %lu", val >> 8 & 0x7); - break; - } - db_printf(" Vector: %lu", val & 0xff); - if (val & 0x800) - db_printf(" Error Code: %lx", - vmcs_read(VMCS_EXIT_INTR_ERRCODE)); - db_printf("\n"); - break; - case EXIT_REASON_EPT_FAULT: - case EXIT_REASON_EPT_MISCONFIG: - db_printf("Guest Physical Address: %#lx\n", - vmcs_read(VMCS_GUEST_PHYSICAL_ADDRESS)); - break; - } - db_printf("VM-instruction error: %#lx\n", vmcs_instruction_error()); -} -#endif diff --git a/vmm/intel/vmx.c b/vmm/intel/vmx.c deleted file mode 100644 index 03d755c..0000000 --- a/vmm/intel/vmx.c +++ /dev/null @@ -1,3416 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include "vmm_lapic.h" -#include "vmm_host.h" -#include "vmm_ioport.h" -#include "vmm_ktr.h" -#include "vmm_stat.h" -#include "vatpic.h" -#include "vlapic.h" -#include "vlapic_priv.h" - -#include "ept.h" -#include "vmx_cpufunc.h" -#include "vmx.h" -#include "vmx_msr.h" -#include "x86.h" -#include "vmx_controls.h" - -#define PINBASED_CTLS_ONE_SETTING \ - (PINBASED_EXTINT_EXITING | \ - PINBASED_NMI_EXITING | \ - PINBASED_VIRTUAL_NMI) -#define PINBASED_CTLS_ZERO_SETTING 0 - -#define PROCBASED_CTLS_WINDOW_SETTING \ - (PROCBASED_INT_WINDOW_EXITING | \ - PROCBASED_NMI_WINDOW_EXITING) - -#define PROCBASED_CTLS_ONE_SETTING \ - (PROCBASED_SECONDARY_CONTROLS | \ - PROCBASED_MWAIT_EXITING | \ - PROCBASED_MONITOR_EXITING | \ - PROCBASED_IO_EXITING | \ - PROCBASED_MSR_BITMAPS | \ - PROCBASED_CTLS_WINDOW_SETTING | \ - PROCBASED_CR8_LOAD_EXITING | \ - PROCBASED_CR8_STORE_EXITING) -#define PROCBASED_CTLS_ZERO_SETTING \ - (PROCBASED_CR3_LOAD_EXITING | \ - PROCBASED_CR3_STORE_EXITING | \ - PROCBASED_IO_BITMAPS) - -#define PROCBASED_CTLS2_ONE_SETTING PROCBASED2_ENABLE_EPT -#define PROCBASED_CTLS2_ZERO_SETTING 0 - -#define VM_EXIT_CTLS_ONE_SETTING \ - (VM_EXIT_HOST_LMA | \ - VM_EXIT_SAVE_EFER | \ - VM_EXIT_LOAD_EFER | \ - VM_EXIT_ACKNOWLEDGE_INTERRUPT) - -#define VM_EXIT_CTLS_ZERO_SETTING VM_EXIT_SAVE_DEBUG_CONTROLS - -#define VM_ENTRY_CTLS_ONE_SETTING (VM_ENTRY_LOAD_EFER) - -#define VM_ENTRY_CTLS_ZERO_SETTING \ - (VM_ENTRY_LOAD_DEBUG_CONTROLS | \ - VM_ENTRY_INTO_SMM | \ - VM_ENTRY_DEACTIVATE_DUAL_MONITOR) - -#define HANDLED 1 -#define UNHANDLED 0 - -static MALLOC_DEFINE(M_VMX, "vmx", "vmx"); -static MALLOC_DEFINE(M_VLAPIC, "vlapic", "vlapic"); - -SYSCTL_DECL(_hw_vmm); -SYSCTL_NODE(_hw_vmm, OID_AUTO, vmx, CTLFLAG_RW, NULL, NULL); - -int vmxon_enabled[MAXCPU]; -static char vmxon_region[MAXCPU][PAGE_SIZE] __aligned(PAGE_SIZE); - -static uint32_t pinbased_ctls, procbased_ctls, procbased_ctls2; -static uint32_t exit_ctls, entry_ctls; - -static uint64_t cr0_ones_mask, cr0_zeros_mask; -SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_ones_mask, CTLFLAG_RD, - &cr0_ones_mask, 0, NULL); -SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_zeros_mask, CTLFLAG_RD, - &cr0_zeros_mask, 0, NULL); - -static uint64_t cr4_ones_mask, cr4_zeros_mask; -SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_ones_mask, CTLFLAG_RD, - &cr4_ones_mask, 0, NULL); -SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_zeros_mask, CTLFLAG_RD, - &cr4_zeros_mask, 0, NULL); - -static int vmx_initialized; -SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, initialized, CTLFLAG_RD, - &vmx_initialized, 0, "Intel VMX initialized"); - -/* - * Optional capabilities - */ -static SYSCTL_NODE(_hw_vmm_vmx, OID_AUTO, cap, CTLFLAG_RW, NULL, NULL); - -static int cap_halt_exit; -SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, halt_exit, CTLFLAG_RD, &cap_halt_exit, 0, - "HLT triggers a VM-exit"); - -static int cap_pause_exit; -SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, pause_exit, CTLFLAG_RD, &cap_pause_exit, - 0, "PAUSE triggers a VM-exit"); - -static int cap_unrestricted_guest; -SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, unrestricted_guest, CTLFLAG_RD, - &cap_unrestricted_guest, 0, "Unrestricted guests"); - -static int cap_monitor_trap; -SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, monitor_trap, CTLFLAG_RD, - &cap_monitor_trap, 0, "Monitor trap flag"); - -static int cap_invpcid; -SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, invpcid, CTLFLAG_RD, &cap_invpcid, - 0, "Guests are allowed to use INVPCID"); - -static int virtual_interrupt_delivery; -SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, virtual_interrupt_delivery, CTLFLAG_RD, - &virtual_interrupt_delivery, 0, "APICv virtual interrupt delivery support"); - -static int posted_interrupts; -SYSCTL_INT(_hw_vmm_vmx_cap, OID_AUTO, posted_interrupts, CTLFLAG_RD, - &posted_interrupts, 0, "APICv posted interrupt support"); - -static int pirvec = -1; -SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, posted_interrupt_vector, CTLFLAG_RD, - &pirvec, 0, "APICv posted interrupt vector"); - -static struct unrhdr *vpid_unr; -static u_int vpid_alloc_failed; -SYSCTL_UINT(_hw_vmm_vmx, OID_AUTO, vpid_alloc_failed, CTLFLAG_RD, - &vpid_alloc_failed, 0, NULL); - -/* - * Use the last page below 4GB as the APIC access address. This address is - * occupied by the boot firmware so it is guaranteed that it will not conflict - * with a page in system memory. - */ -#define APIC_ACCESS_ADDRESS 0xFFFFF000 - -static int vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc); -static int vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval); -static int vmxctx_setreg(struct vmxctx *vmxctx, int reg, uint64_t val); -static void vmx_inject_pir(struct vlapic *vlapic); - -#ifdef KTR -static const char * -exit_reason_to_str(int reason) -{ - static char reasonbuf[32]; - - switch (reason) { - case EXIT_REASON_EXCEPTION: - return "exception"; - case EXIT_REASON_EXT_INTR: - return "extint"; - case EXIT_REASON_TRIPLE_FAULT: - return "triplefault"; - case EXIT_REASON_INIT: - return "init"; - case EXIT_REASON_SIPI: - return "sipi"; - case EXIT_REASON_IO_SMI: - return "iosmi"; - case EXIT_REASON_SMI: - return "smi"; - case EXIT_REASON_INTR_WINDOW: - return "intrwindow"; - case EXIT_REASON_NMI_WINDOW: - return "nmiwindow"; - case EXIT_REASON_TASK_SWITCH: - return "taskswitch"; - case EXIT_REASON_CPUID: - return "cpuid"; - case EXIT_REASON_GETSEC: - return "getsec"; - case EXIT_REASON_HLT: - return "hlt"; - case EXIT_REASON_INVD: - return "invd"; - case EXIT_REASON_INVLPG: - return "invlpg"; - case EXIT_REASON_RDPMC: - return "rdpmc"; - case EXIT_REASON_RDTSC: - return "rdtsc"; - case EXIT_REASON_RSM: - return "rsm"; - case EXIT_REASON_VMCALL: - return "vmcall"; - case EXIT_REASON_VMCLEAR: - return "vmclear"; - case EXIT_REASON_VMLAUNCH: - return "vmlaunch"; - case EXIT_REASON_VMPTRLD: - return "vmptrld"; - case EXIT_REASON_VMPTRST: - return "vmptrst"; - case EXIT_REASON_VMREAD: - return "vmread"; - case EXIT_REASON_VMRESUME: - return "vmresume"; - case EXIT_REASON_VMWRITE: - return "vmwrite"; - case EXIT_REASON_VMXOFF: - return "vmxoff"; - case EXIT_REASON_VMXON: - return "vmxon"; - case EXIT_REASON_CR_ACCESS: - return "craccess"; - case EXIT_REASON_DR_ACCESS: - return "draccess"; - case EXIT_REASON_INOUT: - return "inout"; - case EXIT_REASON_RDMSR: - return "rdmsr"; - case EXIT_REASON_WRMSR: - return "wrmsr"; - case EXIT_REASON_INVAL_VMCS: - return "invalvmcs"; - case EXIT_REASON_INVAL_MSR: - return "invalmsr"; - case EXIT_REASON_MWAIT: - return "mwait"; - case EXIT_REASON_MTF: - return "mtf"; - case EXIT_REASON_MONITOR: - return "monitor"; - case EXIT_REASON_PAUSE: - return "pause"; - case EXIT_REASON_MCE_DURING_ENTRY: - return "mce-during-entry"; - case EXIT_REASON_TPR: - return "tpr"; - case EXIT_REASON_APIC_ACCESS: - return "apic-access"; - case EXIT_REASON_GDTR_IDTR: - return "gdtridtr"; - case EXIT_REASON_LDTR_TR: - return "ldtrtr"; - case EXIT_REASON_EPT_FAULT: - return "eptfault"; - case EXIT_REASON_EPT_MISCONFIG: - return "eptmisconfig"; - case EXIT_REASON_INVEPT: - return "invept"; - case EXIT_REASON_RDTSCP: - return "rdtscp"; - case EXIT_REASON_VMX_PREEMPT: - return "vmxpreempt"; - case EXIT_REASON_INVVPID: - return "invvpid"; - case EXIT_REASON_WBINVD: - return "wbinvd"; - case EXIT_REASON_XSETBV: - return "xsetbv"; - case EXIT_REASON_APIC_WRITE: - return "apic-write"; - default: - snprintf(reasonbuf, sizeof(reasonbuf), "%d", reason); - return (reasonbuf); - } -} -#endif /* KTR */ - -static int -vmx_allow_x2apic_msrs(struct vmx *vmx) -{ - int i, error; - - error = 0; - - /* - * Allow readonly access to the following x2APIC MSRs from the guest. - */ - error += guest_msr_ro(vmx, MSR_APIC_ID); - error += guest_msr_ro(vmx, MSR_APIC_VERSION); - error += guest_msr_ro(vmx, MSR_APIC_LDR); - error += guest_msr_ro(vmx, MSR_APIC_SVR); - - for (i = 0; i < 8; i++) - error += guest_msr_ro(vmx, MSR_APIC_ISR0 + i); - - for (i = 0; i < 8; i++) - error += guest_msr_ro(vmx, MSR_APIC_TMR0 + i); - - for (i = 0; i < 8; i++) - error += guest_msr_ro(vmx, MSR_APIC_IRR0 + i); - - error += guest_msr_ro(vmx, MSR_APIC_ESR); - error += guest_msr_ro(vmx, MSR_APIC_LVT_TIMER); - error += guest_msr_ro(vmx, MSR_APIC_LVT_THERMAL); - error += guest_msr_ro(vmx, MSR_APIC_LVT_PCINT); - error += guest_msr_ro(vmx, MSR_APIC_LVT_LINT0); - error += guest_msr_ro(vmx, MSR_APIC_LVT_LINT1); - error += guest_msr_ro(vmx, MSR_APIC_LVT_ERROR); - error += guest_msr_ro(vmx, MSR_APIC_ICR_TIMER); - error += guest_msr_ro(vmx, MSR_APIC_DCR_TIMER); - error += guest_msr_ro(vmx, MSR_APIC_ICR); - - /* - * Allow TPR, EOI and SELF_IPI MSRs to be read and written by the guest. - * - * These registers get special treatment described in the section - * "Virtualizing MSR-Based APIC Accesses". - */ - error += guest_msr_rw(vmx, MSR_APIC_TPR); - error += guest_msr_rw(vmx, MSR_APIC_EOI); - error += guest_msr_rw(vmx, MSR_APIC_SELF_IPI); - - return (error); -} - -u_long -vmx_fix_cr0(u_long cr0) -{ - - return ((cr0 | cr0_ones_mask) & ~cr0_zeros_mask); -} - -u_long -vmx_fix_cr4(u_long cr4) -{ - - return ((cr4 | cr4_ones_mask) & ~cr4_zeros_mask); -} - -static void -vpid_free(int vpid) -{ - if (vpid < 0 || vpid > 0xffff) - panic("vpid_free: invalid vpid %d", vpid); - - /* - * VPIDs [0,VM_MAXCPU] are special and are not allocated from - * the unit number allocator. - */ - - if (vpid > VM_MAXCPU) - free_unr(vpid_unr, vpid); -} - -static void -vpid_alloc(uint16_t *vpid, int num) -{ - int i, x; - - if (num <= 0 || num > VM_MAXCPU) - panic("invalid number of vpids requested: %d", num); - - /* - * If the "enable vpid" execution control is not enabled then the - * VPID is required to be 0 for all vcpus. - */ - if ((procbased_ctls2 & PROCBASED2_ENABLE_VPID) == 0) { - for (i = 0; i < num; i++) - vpid[i] = 0; - return; - } - - /* - * Allocate a unique VPID for each vcpu from the unit number allocator. - */ - for (i = 0; i < num; i++) { - x = alloc_unr(vpid_unr); - if (x == -1) - break; - else - vpid[i] = x; - } - - if (i < num) { - atomic_add_int(&vpid_alloc_failed, 1); - - /* - * If the unit number allocator does not have enough unique - * VPIDs then we need to allocate from the [1,VM_MAXCPU] range. - * - * These VPIDs are not be unique across VMs but this does not - * affect correctness because the combined mappings are also - * tagged with the EP4TA which is unique for each VM. - * - * It is still sub-optimal because the invvpid will invalidate - * combined mappings for a particular VPID across all EP4TAs. - */ - while (i-- > 0) - vpid_free(vpid[i]); - - for (i = 0; i < num; i++) - vpid[i] = i + 1; - } -} - -static void -vpid_init(void) -{ - /* - * VPID 0 is required when the "enable VPID" execution control is - * disabled. - * - * VPIDs [1,VM_MAXCPU] are used as the "overflow namespace" when the - * unit number allocator does not have sufficient unique VPIDs to - * satisfy the allocation. - * - * The remaining VPIDs are managed by the unit number allocator. - */ - vpid_unr = new_unrhdr(VM_MAXCPU + 1, 0xffff, NULL); -} - -static void -vmx_disable(void *arg __unused) -{ - struct invvpid_desc invvpid_desc = { 0 }; - struct invept_desc invept_desc = { 0 }; - - if (vmxon_enabled[curcpu]) { - /* - * See sections 25.3.3.3 and 25.3.3.4 in Intel Vol 3b. - * - * VMXON or VMXOFF are not required to invalidate any TLB - * caching structures. This prevents potential retention of - * cached information in the TLB between distinct VMX episodes. - */ - invvpid(INVVPID_TYPE_ALL_CONTEXTS, invvpid_desc); - invept(INVEPT_TYPE_ALL_CONTEXTS, invept_desc); - vmxoff(); - } - load_cr4(rcr4() & ~CR4_VMXE); -} - -static int -vmx_cleanup(void) -{ - - if (pirvec >= 0) - lapic_ipi_free(pirvec); - - if (vpid_unr != NULL) { - delete_unrhdr(vpid_unr); - vpid_unr = NULL; - } - - smp_rendezvous(NULL, vmx_disable, NULL, NULL); - - return (0); -} - -static void -vmx_enable(void *arg __unused) -{ - int error; - uint64_t feature_control; - - feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL); - if ((feature_control & IA32_FEATURE_CONTROL_LOCK) == 0 || - (feature_control & IA32_FEATURE_CONTROL_VMX_EN) == 0) { - wrmsr(MSR_IA32_FEATURE_CONTROL, - feature_control | IA32_FEATURE_CONTROL_VMX_EN | - IA32_FEATURE_CONTROL_LOCK); - } - - load_cr4(rcr4() | CR4_VMXE); - - *(uint32_t *)vmxon_region[curcpu] = vmx_revision(); - error = vmxon(vmxon_region[curcpu]); - if (error == 0) - vmxon_enabled[curcpu] = 1; -} - -static void -vmx_restore(void) -{ - - if (vmxon_enabled[curcpu]) - vmxon(vmxon_region[curcpu]); -} - -static int -vmx_init(int ipinum) -{ - int error, use_tpr_shadow; - uint64_t basic, fixed0, fixed1, feature_control; - uint32_t tmp, procbased2_vid_bits; - - /* CPUID.1:ECX[bit 5] must be 1 for processor to support VMX */ - if (!(cpu_feature2 & CPUID2_VMX)) { - printf("vmx_init: processor does not support VMX operation\n"); - return (ENXIO); - } - - /* - * Verify that MSR_IA32_FEATURE_CONTROL lock and VMXON enable bits - * are set (bits 0 and 2 respectively). - */ - feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL); - if ((feature_control & IA32_FEATURE_CONTROL_LOCK) == 1 && - (feature_control & IA32_FEATURE_CONTROL_VMX_EN) == 0) { - printf("vmx_init: VMX operation disabled by BIOS\n"); - return (ENXIO); - } - - /* - * Verify capabilities MSR_VMX_BASIC: - * - bit 54 indicates support for INS/OUTS decoding - */ - basic = rdmsr(MSR_VMX_BASIC); - if ((basic & (1UL << 54)) == 0) { - printf("vmx_init: processor does not support desired basic " - "capabilities\n"); - return (EINVAL); - } - - /* Check support for primary processor-based VM-execution controls */ - error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS, - MSR_VMX_TRUE_PROCBASED_CTLS, - PROCBASED_CTLS_ONE_SETTING, - PROCBASED_CTLS_ZERO_SETTING, &procbased_ctls); - if (error) { - printf("vmx_init: processor does not support desired primary " - "processor-based controls\n"); - return (error); - } - - /* Clear the processor-based ctl bits that are set on demand */ - procbased_ctls &= ~PROCBASED_CTLS_WINDOW_SETTING; - - /* Check support for secondary processor-based VM-execution controls */ - error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, - MSR_VMX_PROCBASED_CTLS2, - PROCBASED_CTLS2_ONE_SETTING, - PROCBASED_CTLS2_ZERO_SETTING, &procbased_ctls2); - if (error) { - printf("vmx_init: processor does not support desired secondary " - "processor-based controls\n"); - return (error); - } - - /* Check support for VPID */ - error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2, - PROCBASED2_ENABLE_VPID, 0, &tmp); - if (error == 0) - procbased_ctls2 |= PROCBASED2_ENABLE_VPID; - - /* Check support for pin-based VM-execution controls */ - error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS, - MSR_VMX_TRUE_PINBASED_CTLS, - PINBASED_CTLS_ONE_SETTING, - PINBASED_CTLS_ZERO_SETTING, &pinbased_ctls); - if (error) { - printf("vmx_init: processor does not support desired " - "pin-based controls\n"); - return (error); - } - - /* Check support for VM-exit controls */ - error = vmx_set_ctlreg(MSR_VMX_EXIT_CTLS, MSR_VMX_TRUE_EXIT_CTLS, - VM_EXIT_CTLS_ONE_SETTING, - VM_EXIT_CTLS_ZERO_SETTING, - &exit_ctls); - if (error) { - printf("vmx_init: processor does not support desired " - "exit controls\n"); - return (error); - } - - /* Check support for VM-entry controls */ - error = vmx_set_ctlreg(MSR_VMX_ENTRY_CTLS, MSR_VMX_TRUE_ENTRY_CTLS, - VM_ENTRY_CTLS_ONE_SETTING, VM_ENTRY_CTLS_ZERO_SETTING, - &entry_ctls); - if (error) { - printf("vmx_init: processor does not support desired " - "entry controls\n"); - return (error); - } - - /* - * Check support for optional features by testing them - * as individual bits - */ - cap_halt_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS, - MSR_VMX_TRUE_PROCBASED_CTLS, - PROCBASED_HLT_EXITING, 0, - &tmp) == 0); - - cap_monitor_trap = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS, - MSR_VMX_PROCBASED_CTLS, - PROCBASED_MTF, 0, - &tmp) == 0); - - cap_pause_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS, - MSR_VMX_TRUE_PROCBASED_CTLS, - PROCBASED_PAUSE_EXITING, 0, - &tmp) == 0); - - cap_unrestricted_guest = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, - MSR_VMX_PROCBASED_CTLS2, - PROCBASED2_UNRESTRICTED_GUEST, 0, - &tmp) == 0); - - cap_invpcid = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, - MSR_VMX_PROCBASED_CTLS2, PROCBASED2_ENABLE_INVPCID, 0, - &tmp) == 0); - - /* - * Check support for virtual interrupt delivery. - */ - procbased2_vid_bits = (PROCBASED2_VIRTUALIZE_APIC_ACCESSES | - PROCBASED2_VIRTUALIZE_X2APIC_MODE | - PROCBASED2_APIC_REGISTER_VIRTUALIZATION | - PROCBASED2_VIRTUAL_INTERRUPT_DELIVERY); - - use_tpr_shadow = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS, - MSR_VMX_TRUE_PROCBASED_CTLS, PROCBASED_USE_TPR_SHADOW, 0, - &tmp) == 0); - - error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2, - procbased2_vid_bits, 0, &tmp); - if (error == 0 && use_tpr_shadow) { - virtual_interrupt_delivery = 1; - TUNABLE_INT_FETCH("hw.vmm.vmx.use_apic_vid", - &virtual_interrupt_delivery); - } - - if (virtual_interrupt_delivery) { - procbased_ctls |= PROCBASED_USE_TPR_SHADOW; - procbased_ctls2 |= procbased2_vid_bits; - procbased_ctls2 &= ~PROCBASED2_VIRTUALIZE_X2APIC_MODE; - - /* - * No need to emulate accesses to %CR8 if virtual - * interrupt delivery is enabled. - */ - procbased_ctls &= ~PROCBASED_CR8_LOAD_EXITING; - procbased_ctls &= ~PROCBASED_CR8_STORE_EXITING; - - /* - * Check for Posted Interrupts only if Virtual Interrupt - * Delivery is enabled. - */ - error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS, - MSR_VMX_TRUE_PINBASED_CTLS, PINBASED_POSTED_INTERRUPT, 0, - &tmp); - if (error == 0) { - pirvec = lapic_ipi_alloc(&IDTVEC(justreturn)); - if (pirvec < 0) { - if (bootverbose) { - printf("vmx_init: unable to allocate " - "posted interrupt vector\n"); - } - } else { - posted_interrupts = 1; - TUNABLE_INT_FETCH("hw.vmm.vmx.use_apic_pir", - &posted_interrupts); - } - } - } - - if (posted_interrupts) - pinbased_ctls |= PINBASED_POSTED_INTERRUPT; - - /* Initialize EPT */ - error = ept_init(ipinum); - if (error) { - printf("vmx_init: ept initialization failed (%d)\n", error); - return (error); - } - - /* - * Stash the cr0 and cr4 bits that must be fixed to 0 or 1 - */ - fixed0 = rdmsr(MSR_VMX_CR0_FIXED0); - fixed1 = rdmsr(MSR_VMX_CR0_FIXED1); - cr0_ones_mask = fixed0 & fixed1; - cr0_zeros_mask = ~fixed0 & ~fixed1; - - /* - * CR0_PE and CR0_PG can be set to zero in VMX non-root operation - * if unrestricted guest execution is allowed. - */ - if (cap_unrestricted_guest) - cr0_ones_mask &= ~(CR0_PG | CR0_PE); - - /* - * Do not allow the guest to set CR0_NW or CR0_CD. - */ - cr0_zeros_mask |= (CR0_NW | CR0_CD); - - fixed0 = rdmsr(MSR_VMX_CR4_FIXED0); - fixed1 = rdmsr(MSR_VMX_CR4_FIXED1); - cr4_ones_mask = fixed0 & fixed1; - cr4_zeros_mask = ~fixed0 & ~fixed1; - - vpid_init(); - - vmx_msr_init(); - - /* enable VMX operation */ - smp_rendezvous(NULL, vmx_enable, NULL, NULL); - - vmx_initialized = 1; - - return (0); -} - -static void -vmx_trigger_hostintr(int vector) -{ - uintptr_t func; - struct gate_descriptor *gd; - - gd = &idt[vector]; - - KASSERT(vector >= 32 && vector <= 255, ("vmx_trigger_hostintr: " - "invalid vector %d", vector)); - KASSERT(gd->gd_p == 1, ("gate descriptor for vector %d not present", - vector)); - KASSERT(gd->gd_type == SDT_SYSIGT, ("gate descriptor for vector %d " - "has invalid type %d", vector, gd->gd_type)); - KASSERT(gd->gd_dpl == SEL_KPL, ("gate descriptor for vector %d " - "has invalid dpl %d", vector, gd->gd_dpl)); - KASSERT(gd->gd_selector == GSEL(GCODE_SEL, SEL_KPL), ("gate descriptor " - "for vector %d has invalid selector %d", vector, gd->gd_selector)); - KASSERT(gd->gd_ist == 0, ("gate descriptor for vector %d has invalid " - "IST %d", vector, gd->gd_ist)); - - func = ((long)gd->gd_hioffset << 16 | gd->gd_looffset); - vmx_call_isr(func); -} - -static int -vmx_setup_cr_shadow(int which, struct vmcs *vmcs, uint32_t initial) -{ - int error, mask_ident, shadow_ident; - uint64_t mask_value; - - if (which != 0 && which != 4) - panic("vmx_setup_cr_shadow: unknown cr%d", which); - - if (which == 0) { - mask_ident = VMCS_CR0_MASK; - mask_value = cr0_ones_mask | cr0_zeros_mask; - shadow_ident = VMCS_CR0_SHADOW; - } else { - mask_ident = VMCS_CR4_MASK; - mask_value = cr4_ones_mask | cr4_zeros_mask; - shadow_ident = VMCS_CR4_SHADOW; - } - - error = vmcs_setreg(vmcs, 0, VMCS_IDENT(mask_ident), mask_value); - if (error) - return (error); - - error = vmcs_setreg(vmcs, 0, VMCS_IDENT(shadow_ident), initial); - if (error) - return (error); - - return (0); -} -#define vmx_setup_cr0_shadow(vmcs,init) vmx_setup_cr_shadow(0, (vmcs), (init)) -#define vmx_setup_cr4_shadow(vmcs,init) vmx_setup_cr_shadow(4, (vmcs), (init)) - -static void * -vmx_vminit(struct vm *vm, pmap_t pmap) -{ - uint16_t vpid[VM_MAXCPU]; - int i, error; - struct vmx *vmx; - struct vmcs *vmcs; - uint32_t exc_bitmap; - - vmx = malloc(sizeof(struct vmx), M_VMX, M_WAITOK | M_ZERO); - if ((uintptr_t)vmx & PAGE_MASK) { - panic("malloc of struct vmx not aligned on %d byte boundary", - PAGE_SIZE); - } - vmx->vm = vm; - - vmx->eptp = eptp(vtophys((vm_offset_t)pmap->pm_pml4)); - - /* - * Clean up EPTP-tagged guest physical and combined mappings - * - * VMX transitions are not required to invalidate any guest physical - * mappings. So, it may be possible for stale guest physical mappings - * to be present in the processor TLBs. - * - * Combined mappings for this EP4TA are also invalidated for all VPIDs. - */ - ept_invalidate_mappings(vmx->eptp); - - msr_bitmap_initialize(vmx->msr_bitmap); - - /* - * It is safe to allow direct access to MSR_GSBASE and MSR_FSBASE. - * The guest FSBASE and GSBASE are saved and restored during - * vm-exit and vm-entry respectively. The host FSBASE and GSBASE are - * always restored from the vmcs host state area on vm-exit. - * - * The SYSENTER_CS/ESP/EIP MSRs are identical to FS/GSBASE in - * how they are saved/restored so can be directly accessed by the - * guest. - * - * MSR_EFER is saved and restored in the guest VMCS area on a - * VM exit and entry respectively. It is also restored from the - * host VMCS area on a VM exit. - * - * The TSC MSR is exposed read-only. Writes are disallowed as that - * will impact the host TSC. - * XXX Writes would be implemented with a wrmsr trap, and - * then modifying the TSC offset in the VMCS. - */ - if (guest_msr_rw(vmx, MSR_GSBASE) || - guest_msr_rw(vmx, MSR_FSBASE) || - guest_msr_rw(vmx, MSR_SYSENTER_CS_MSR) || - guest_msr_rw(vmx, MSR_SYSENTER_ESP_MSR) || - guest_msr_rw(vmx, MSR_SYSENTER_EIP_MSR) || - guest_msr_rw(vmx, MSR_EFER) || - guest_msr_ro(vmx, MSR_TSC)) - panic("vmx_vminit: error setting guest msr access"); - - vpid_alloc(vpid, VM_MAXCPU); - - if (virtual_interrupt_delivery) { - error = vm_map_mmio(vm, DEFAULT_APIC_BASE, PAGE_SIZE, - APIC_ACCESS_ADDRESS); - /* XXX this should really return an error to the caller */ - KASSERT(error == 0, ("vm_map_mmio(apicbase) error %d", error)); - } - - for (i = 0; i < VM_MAXCPU; i++) { - vmcs = &vmx->vmcs[i]; - vmcs->identifier = vmx_revision(); - error = vmclear(vmcs); - if (error != 0) { - panic("vmx_vminit: vmclear error %d on vcpu %d\n", - error, i); - } - - vmx_msr_guest_init(vmx, i); - - error = vmcs_init(vmcs); - KASSERT(error == 0, ("vmcs_init error %d", error)); - - VMPTRLD(vmcs); - error = 0; - error += vmwrite(VMCS_HOST_RSP, (u_long)&vmx->ctx[i]); - error += vmwrite(VMCS_EPTP, vmx->eptp); - error += vmwrite(VMCS_PIN_BASED_CTLS, pinbased_ctls); - error += vmwrite(VMCS_PRI_PROC_BASED_CTLS, procbased_ctls); - error += vmwrite(VMCS_SEC_PROC_BASED_CTLS, procbased_ctls2); - error += vmwrite(VMCS_EXIT_CTLS, exit_ctls); - error += vmwrite(VMCS_ENTRY_CTLS, entry_ctls); - error += vmwrite(VMCS_MSR_BITMAP, vtophys(vmx->msr_bitmap)); - error += vmwrite(VMCS_VPID, vpid[i]); - - /* exception bitmap */ - if (vcpu_trace_exceptions(vm, i)) - exc_bitmap = 0xffffffff; - else - exc_bitmap = 1 << IDT_MC; - error += vmwrite(VMCS_EXCEPTION_BITMAP, exc_bitmap); - - if (virtual_interrupt_delivery) { - error += vmwrite(VMCS_APIC_ACCESS, APIC_ACCESS_ADDRESS); - error += vmwrite(VMCS_VIRTUAL_APIC, - vtophys(&vmx->apic_page[i])); - error += vmwrite(VMCS_EOI_EXIT0, 0); - error += vmwrite(VMCS_EOI_EXIT1, 0); - error += vmwrite(VMCS_EOI_EXIT2, 0); - error += vmwrite(VMCS_EOI_EXIT3, 0); - } - if (posted_interrupts) { - error += vmwrite(VMCS_PIR_VECTOR, pirvec); - error += vmwrite(VMCS_PIR_DESC, - vtophys(&vmx->pir_desc[i])); - } - VMCLEAR(vmcs); - KASSERT(error == 0, ("vmx_vminit: error customizing the vmcs")); - - vmx->cap[i].set = 0; - vmx->cap[i].proc_ctls = procbased_ctls; - vmx->cap[i].proc_ctls2 = procbased_ctls2; - - vmx->state[i].nextrip = ~0; - vmx->state[i].lastcpu = NOCPU; - vmx->state[i].vpid = vpid[i]; - - /* - * Set up the CR0/4 shadows, and init the read shadow - * to the power-on register value from the Intel Sys Arch. - * CR0 - 0x60000010 - * CR4 - 0 - */ - error = vmx_setup_cr0_shadow(vmcs, 0x60000010); - if (error != 0) - panic("vmx_setup_cr0_shadow %d", error); - - error = vmx_setup_cr4_shadow(vmcs, 0); - if (error != 0) - panic("vmx_setup_cr4_shadow %d", error); - - vmx->ctx[i].pmap = pmap; - } - - return (vmx); -} - -static int -vmx_handle_cpuid(struct vm *vm, int vcpu, struct vmxctx *vmxctx) -{ - int handled, func; - - func = vmxctx->guest_rax; - - handled = x86_emulate_cpuid(vm, vcpu, - (uint32_t*)(&vmxctx->guest_rax), - (uint32_t*)(&vmxctx->guest_rbx), - (uint32_t*)(&vmxctx->guest_rcx), - (uint32_t*)(&vmxctx->guest_rdx)); - return (handled); -} - -static __inline void -vmx_run_trace(struct vmx *vmx, int vcpu) -{ -#ifdef KTR - VCPU_CTR1(vmx->vm, vcpu, "Resume execution at %#lx", vmcs_guest_rip()); -#endif -} - -static __inline void -vmx_exit_trace(struct vmx *vmx, int vcpu, uint64_t rip, uint32_t exit_reason, - int handled) -{ -#ifdef KTR - VCPU_CTR3(vmx->vm, vcpu, "%s %s vmexit at 0x%0lx", - handled ? "handled" : "unhandled", - exit_reason_to_str(exit_reason), rip); -#endif -} - -static __inline void -vmx_astpending_trace(struct vmx *vmx, int vcpu, uint64_t rip) -{ -#ifdef KTR - VCPU_CTR1(vmx->vm, vcpu, "astpending vmexit at 0x%0lx", rip); -#endif -} - -static VMM_STAT_INTEL(VCPU_INVVPID_SAVED, "Number of vpid invalidations saved"); -static VMM_STAT_INTEL(VCPU_INVVPID_DONE, "Number of vpid invalidations done"); - -/* - * Invalidate guest mappings identified by its vpid from the TLB. - */ -static __inline void -vmx_invvpid(struct vmx *vmx, int vcpu, pmap_t pmap, int running) -{ - struct vmxstate *vmxstate; - struct invvpid_desc invvpid_desc; - - vmxstate = &vmx->state[vcpu]; - if (vmxstate->vpid == 0) - return; - - if (!running) { - /* - * Set the 'lastcpu' to an invalid host cpu. - * - * This will invalidate TLB entries tagged with the vcpu's - * vpid the next time it runs via vmx_set_pcpu_defaults(). - */ - vmxstate->lastcpu = NOCPU; - return; - } - - KASSERT(curthread->td_critnest > 0, ("%s: vcpu %d running outside " - "critical section", __func__, vcpu)); - - /* - * Invalidate all mappings tagged with 'vpid' - * - * We do this because this vcpu was executing on a different host - * cpu when it last ran. We do not track whether it invalidated - * mappings associated with its 'vpid' during that run. So we must - * assume that the mappings associated with 'vpid' on 'curcpu' are - * stale and invalidate them. - * - * Note that we incur this penalty only when the scheduler chooses to - * move the thread associated with this vcpu between host cpus. - * - * Note also that this will invalidate mappings tagged with 'vpid' - * for "all" EP4TAs. - */ - if (pmap->pm_eptgen == vmx->eptgen[curcpu]) { - invvpid_desc._res1 = 0; - invvpid_desc._res2 = 0; - invvpid_desc.vpid = vmxstate->vpid; - invvpid_desc.linear_addr = 0; - invvpid(INVVPID_TYPE_SINGLE_CONTEXT, invvpid_desc); - vmm_stat_incr(vmx->vm, vcpu, VCPU_INVVPID_DONE, 1); - } else { - /* - * The invvpid can be skipped if an invept is going to - * be performed before entering the guest. The invept - * will invalidate combined mappings tagged with - * 'vmx->eptp' for all vpids. - */ - vmm_stat_incr(vmx->vm, vcpu, VCPU_INVVPID_SAVED, 1); - } -} - -static void -vmx_set_pcpu_defaults(struct vmx *vmx, int vcpu, pmap_t pmap) -{ - struct vmxstate *vmxstate; - - vmxstate = &vmx->state[vcpu]; - if (vmxstate->lastcpu == curcpu) - return; - - vmxstate->lastcpu = curcpu; - - vmm_stat_incr(vmx->vm, vcpu, VCPU_MIGRATIONS, 1); - - vmcs_write(VMCS_HOST_TR_BASE, vmm_get_host_trbase()); - vmcs_write(VMCS_HOST_GDTR_BASE, vmm_get_host_gdtrbase()); - vmcs_write(VMCS_HOST_GS_BASE, vmm_get_host_gsbase()); - vmx_invvpid(vmx, vcpu, pmap, 1); -} - -/* - * We depend on 'procbased_ctls' to have the Interrupt Window Exiting bit set. - */ -CTASSERT((PROCBASED_CTLS_ONE_SETTING & PROCBASED_INT_WINDOW_EXITING) != 0); - -static void __inline -vmx_set_int_window_exiting(struct vmx *vmx, int vcpu) -{ - - if ((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) == 0) { - vmx->cap[vcpu].proc_ctls |= PROCBASED_INT_WINDOW_EXITING; - vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls); - VCPU_CTR0(vmx->vm, vcpu, "Enabling interrupt window exiting"); - } -} - -static void __inline -vmx_clear_int_window_exiting(struct vmx *vmx, int vcpu) -{ - - KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) != 0, - ("intr_window_exiting not set: %#x", vmx->cap[vcpu].proc_ctls)); - vmx->cap[vcpu].proc_ctls &= ~PROCBASED_INT_WINDOW_EXITING; - vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls); - VCPU_CTR0(vmx->vm, vcpu, "Disabling interrupt window exiting"); -} - -static void __inline -vmx_set_nmi_window_exiting(struct vmx *vmx, int vcpu) -{ - - if ((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) == 0) { - vmx->cap[vcpu].proc_ctls |= PROCBASED_NMI_WINDOW_EXITING; - vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls); - VCPU_CTR0(vmx->vm, vcpu, "Enabling NMI window exiting"); - } -} - -static void __inline -vmx_clear_nmi_window_exiting(struct vmx *vmx, int vcpu) -{ - - KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) != 0, - ("nmi_window_exiting not set %#x", vmx->cap[vcpu].proc_ctls)); - vmx->cap[vcpu].proc_ctls &= ~PROCBASED_NMI_WINDOW_EXITING; - vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls); - VCPU_CTR0(vmx->vm, vcpu, "Disabling NMI window exiting"); -} - -#define NMI_BLOCKING (VMCS_INTERRUPTIBILITY_NMI_BLOCKING | \ - VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING) -#define HWINTR_BLOCKING (VMCS_INTERRUPTIBILITY_STI_BLOCKING | \ - VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING) - -static void -vmx_inject_nmi(struct vmx *vmx, int vcpu) -{ - uint32_t gi, info; - - gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY); - KASSERT((gi & NMI_BLOCKING) == 0, ("vmx_inject_nmi: invalid guest " - "interruptibility-state %#x", gi)); - - info = vmcs_read(VMCS_ENTRY_INTR_INFO); - KASSERT((info & VMCS_INTR_VALID) == 0, ("vmx_inject_nmi: invalid " - "VM-entry interruption information %#x", info)); - - /* - * Inject the virtual NMI. The vector must be the NMI IDT entry - * or the VMCS entry check will fail. - */ - info = IDT_NMI | VMCS_INTR_T_NMI | VMCS_INTR_VALID; - vmcs_write(VMCS_ENTRY_INTR_INFO, info); - - VCPU_CTR0(vmx->vm, vcpu, "Injecting vNMI"); - - /* Clear the request */ - vm_nmi_clear(vmx->vm, vcpu); -} - -static void -vmx_inject_interrupts(struct vmx *vmx, int vcpu, struct vlapic *vlapic, - uint64_t guestrip) -{ - int vector, need_nmi_exiting, extint_pending; - uint64_t rflags, entryinfo; - uint32_t gi, info; - - if (vmx->state[vcpu].nextrip != guestrip) { - gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY); - if (gi & HWINTR_BLOCKING) { - VCPU_CTR2(vmx->vm, vcpu, "Guest interrupt blocking " - "cleared due to rip change: %#lx/%#lx", - vmx->state[vcpu].nextrip, guestrip); - gi &= ~HWINTR_BLOCKING; - vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi); - } - } - - if (vm_entry_intinfo(vmx->vm, vcpu, &entryinfo)) { - KASSERT((entryinfo & VMCS_INTR_VALID) != 0, ("%s: entry " - "intinfo is not valid: %#lx", __func__, entryinfo)); - - info = vmcs_read(VMCS_ENTRY_INTR_INFO); - KASSERT((info & VMCS_INTR_VALID) == 0, ("%s: cannot inject " - "pending exception: %#lx/%#x", __func__, entryinfo, info)); - - info = entryinfo; - vector = info & 0xff; - if (vector == IDT_BP || vector == IDT_OF) { - /* - * VT-x requires #BP and #OF to be injected as software - * exceptions. - */ - info &= ~VMCS_INTR_T_MASK; - info |= VMCS_INTR_T_SWEXCEPTION; - } - - if (info & VMCS_INTR_DEL_ERRCODE) - vmcs_write(VMCS_ENTRY_EXCEPTION_ERROR, entryinfo >> 32); - - vmcs_write(VMCS_ENTRY_INTR_INFO, info); - } - - if (vm_nmi_pending(vmx->vm, vcpu)) { - /* - * If there are no conditions blocking NMI injection then - * inject it directly here otherwise enable "NMI window - * exiting" to inject it as soon as we can. - * - * We also check for STI_BLOCKING because some implementations - * don't allow NMI injection in this case. If we are running - * on a processor that doesn't have this restriction it will - * immediately exit and the NMI will be injected in the - * "NMI window exiting" handler. - */ - need_nmi_exiting = 1; - gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY); - if ((gi & (HWINTR_BLOCKING | NMI_BLOCKING)) == 0) { - info = vmcs_read(VMCS_ENTRY_INTR_INFO); - if ((info & VMCS_INTR_VALID) == 0) { - vmx_inject_nmi(vmx, vcpu); - need_nmi_exiting = 0; - } else { - VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI " - "due to VM-entry intr info %#x", info); - } - } else { - VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI due to " - "Guest Interruptibility-state %#x", gi); - } - - if (need_nmi_exiting) - vmx_set_nmi_window_exiting(vmx, vcpu); - } - - extint_pending = vm_extint_pending(vmx->vm, vcpu); - - if (!extint_pending && virtual_interrupt_delivery) { - vmx_inject_pir(vlapic); - return; - } - - /* - * If interrupt-window exiting is already in effect then don't bother - * checking for pending interrupts. This is just an optimization and - * not needed for correctness. - */ - if ((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) != 0) { - VCPU_CTR0(vmx->vm, vcpu, "Skip interrupt injection due to " - "pending int_window_exiting"); - return; - } - - if (!extint_pending) { - /* Ask the local apic for a vector to inject */ - if (!vlapic_pending_intr(vlapic, &vector)) - return; - - /* - * From the Intel SDM, Volume 3, Section "Maskable - * Hardware Interrupts": - * - maskable interrupt vectors [16,255] can be delivered - * through the local APIC. - */ - KASSERT(vector >= 16 && vector <= 255, - ("invalid vector %d from local APIC", vector)); - } else { - /* Ask the legacy pic for a vector to inject */ - vatpic_pending_intr(vmx->vm, &vector); - - /* - * From the Intel SDM, Volume 3, Section "Maskable - * Hardware Interrupts": - * - maskable interrupt vectors [0,255] can be delivered - * through the INTR pin. - */ - KASSERT(vector >= 0 && vector <= 255, - ("invalid vector %d from INTR", vector)); - } - - /* Check RFLAGS.IF and the interruptibility state of the guest */ - rflags = vmcs_read(VMCS_GUEST_RFLAGS); - if ((rflags & PSL_I) == 0) { - VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to " - "rflags %#lx", vector, rflags); - goto cantinject; - } - - gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY); - if (gi & HWINTR_BLOCKING) { - VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to " - "Guest Interruptibility-state %#x", vector, gi); - goto cantinject; - } - - info = vmcs_read(VMCS_ENTRY_INTR_INFO); - if (info & VMCS_INTR_VALID) { - /* - * This is expected and could happen for multiple reasons: - * - A vectoring VM-entry was aborted due to astpending - * - A VM-exit happened during event injection. - * - An exception was injected above. - * - An NMI was injected above or after "NMI window exiting" - */ - VCPU_CTR2(vmx->vm, vcpu, "Cannot inject vector %d due to " - "VM-entry intr info %#x", vector, info); - goto cantinject; - } - - /* Inject the interrupt */ - info = VMCS_INTR_T_HWINTR | VMCS_INTR_VALID; - info |= vector; - vmcs_write(VMCS_ENTRY_INTR_INFO, info); - - if (!extint_pending) { - /* Update the Local APIC ISR */ - vlapic_intr_accepted(vlapic, vector); - } else { - vm_extint_clear(vmx->vm, vcpu); - vatpic_intr_accepted(vmx->vm, vector); - - /* - * After we accepted the current ExtINT the PIC may - * have posted another one. If that is the case, set - * the Interrupt Window Exiting execution control so - * we can inject that one too. - * - * Also, interrupt window exiting allows us to inject any - * pending APIC vector that was preempted by the ExtINT - * as soon as possible. This applies both for the software - * emulated vlapic and the hardware assisted virtual APIC. - */ - vmx_set_int_window_exiting(vmx, vcpu); - } - - VCPU_CTR1(vmx->vm, vcpu, "Injecting hwintr at vector %d", vector); - - return; - -cantinject: - /* - * Set the Interrupt Window Exiting execution control so we can inject - * the interrupt as soon as blocking condition goes away. - */ - vmx_set_int_window_exiting(vmx, vcpu); -} - -/* - * If the Virtual NMIs execution control is '1' then the logical processor - * tracks virtual-NMI blocking in the Guest Interruptibility-state field of - * the VMCS. An IRET instruction in VMX non-root operation will remove any - * virtual-NMI blocking. - * - * This unblocking occurs even if the IRET causes a fault. In this case the - * hypervisor needs to restore virtual-NMI blocking before resuming the guest. - */ -static void -vmx_restore_nmi_blocking(struct vmx *vmx, int vcpuid) -{ - uint32_t gi; - - VCPU_CTR0(vmx->vm, vcpuid, "Restore Virtual-NMI blocking"); - gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY); - gi |= VMCS_INTERRUPTIBILITY_NMI_BLOCKING; - vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi); -} - -static void -vmx_clear_nmi_blocking(struct vmx *vmx, int vcpuid) -{ - uint32_t gi; - - VCPU_CTR0(vmx->vm, vcpuid, "Clear Virtual-NMI blocking"); - gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY); - gi &= ~VMCS_INTERRUPTIBILITY_NMI_BLOCKING; - vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi); -} - -static void -vmx_assert_nmi_blocking(struct vmx *vmx, int vcpuid) -{ - uint32_t gi; - - gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY); - KASSERT(gi & VMCS_INTERRUPTIBILITY_NMI_BLOCKING, - ("NMI blocking is not in effect %#x", gi)); -} - -static int -vmx_emulate_xsetbv(struct vmx *vmx, int vcpu, struct vm_exit *vmexit) -{ - struct vmxctx *vmxctx; - uint64_t xcrval; - const struct xsave_limits *limits; - - vmxctx = &vmx->ctx[vcpu]; - limits = vmm_get_xsave_limits(); - - /* - * Note that the processor raises a GP# fault on its own if - * xsetbv is executed for CPL != 0, so we do not have to - * emulate that fault here. - */ - - /* Only xcr0 is supported. */ - if (vmxctx->guest_rcx != 0) { - vm_inject_gp(vmx->vm, vcpu); - return (HANDLED); - } - - /* We only handle xcr0 if both the host and guest have XSAVE enabled. */ - if (!limits->xsave_enabled || !(vmcs_read(VMCS_GUEST_CR4) & CR4_XSAVE)) { - vm_inject_ud(vmx->vm, vcpu); - return (HANDLED); - } - - xcrval = vmxctx->guest_rdx << 32 | (vmxctx->guest_rax & 0xffffffff); - if ((xcrval & ~limits->xcr0_allowed) != 0) { - vm_inject_gp(vmx->vm, vcpu); - return (HANDLED); - } - - if (!(xcrval & XFEATURE_ENABLED_X87)) { - vm_inject_gp(vmx->vm, vcpu); - return (HANDLED); - } - - /* AVX (YMM_Hi128) requires SSE. */ - if (xcrval & XFEATURE_ENABLED_AVX && - (xcrval & XFEATURE_AVX) != XFEATURE_AVX) { - vm_inject_gp(vmx->vm, vcpu); - return (HANDLED); - } - - /* - * AVX512 requires base AVX (YMM_Hi128) as well as OpMask, - * ZMM_Hi256, and Hi16_ZMM. - */ - if (xcrval & XFEATURE_AVX512 && - (xcrval & (XFEATURE_AVX512 | XFEATURE_AVX)) != - (XFEATURE_AVX512 | XFEATURE_AVX)) { - vm_inject_gp(vmx->vm, vcpu); - return (HANDLED); - } - - /* - * Intel MPX requires both bound register state flags to be - * set. - */ - if (((xcrval & XFEATURE_ENABLED_BNDREGS) != 0) != - ((xcrval & XFEATURE_ENABLED_BNDCSR) != 0)) { - vm_inject_gp(vmx->vm, vcpu); - return (HANDLED); - } - - /* - * This runs "inside" vmrun() with the guest's FPU state, so - * modifying xcr0 directly modifies the guest's xcr0, not the - * host's. - */ - load_xcr(0, xcrval); - return (HANDLED); -} - -static uint64_t -vmx_get_guest_reg(struct vmx *vmx, int vcpu, int ident) -{ - const struct vmxctx *vmxctx; - - vmxctx = &vmx->ctx[vcpu]; - - switch (ident) { - case 0: - return (vmxctx->guest_rax); - case 1: - return (vmxctx->guest_rcx); - case 2: - return (vmxctx->guest_rdx); - case 3: - return (vmxctx->guest_rbx); - case 4: - return (vmcs_read(VMCS_GUEST_RSP)); - case 5: - return (vmxctx->guest_rbp); - case 6: - return (vmxctx->guest_rsi); - case 7: - return (vmxctx->guest_rdi); - case 8: - return (vmxctx->guest_r8); - case 9: - return (vmxctx->guest_r9); - case 10: - return (vmxctx->guest_r10); - case 11: - return (vmxctx->guest_r11); - case 12: - return (vmxctx->guest_r12); - case 13: - return (vmxctx->guest_r13); - case 14: - return (vmxctx->guest_r14); - case 15: - return (vmxctx->guest_r15); - default: - panic("invalid vmx register %d", ident); - } -} - -static void -vmx_set_guest_reg(struct vmx *vmx, int vcpu, int ident, uint64_t regval) -{ - struct vmxctx *vmxctx; - - vmxctx = &vmx->ctx[vcpu]; - - switch (ident) { - case 0: - vmxctx->guest_rax = regval; - break; - case 1: - vmxctx->guest_rcx = regval; - break; - case 2: - vmxctx->guest_rdx = regval; - break; - case 3: - vmxctx->guest_rbx = regval; - break; - case 4: - vmcs_write(VMCS_GUEST_RSP, regval); - break; - case 5: - vmxctx->guest_rbp = regval; - break; - case 6: - vmxctx->guest_rsi = regval; - break; - case 7: - vmxctx->guest_rdi = regval; - break; - case 8: - vmxctx->guest_r8 = regval; - break; - case 9: - vmxctx->guest_r9 = regval; - break; - case 10: - vmxctx->guest_r10 = regval; - break; - case 11: - vmxctx->guest_r11 = regval; - break; - case 12: - vmxctx->guest_r12 = regval; - break; - case 13: - vmxctx->guest_r13 = regval; - break; - case 14: - vmxctx->guest_r14 = regval; - break; - case 15: - vmxctx->guest_r15 = regval; - break; - default: - panic("invalid vmx register %d", ident); - } -} - -static int -vmx_emulate_cr0_access(struct vmx *vmx, int vcpu, uint64_t exitqual) -{ - uint64_t crval, regval; - - /* We only handle mov to %cr0 at this time */ - if ((exitqual & 0xf0) != 0x00) - return (UNHANDLED); - - regval = vmx_get_guest_reg(vmx, vcpu, (exitqual >> 8) & 0xf); - - vmcs_write(VMCS_CR0_SHADOW, regval); - - crval = regval | cr0_ones_mask; - crval &= ~cr0_zeros_mask; - vmcs_write(VMCS_GUEST_CR0, crval); - - if (regval & CR0_PG) { - uint64_t efer, entry_ctls; - - /* - * If CR0.PG is 1 and EFER.LME is 1 then EFER.LMA and - * the "IA-32e mode guest" bit in VM-entry control must be - * equal. - */ - efer = vmcs_read(VMCS_GUEST_IA32_EFER); - if (efer & EFER_LME) { - efer |= EFER_LMA; - vmcs_write(VMCS_GUEST_IA32_EFER, efer); - entry_ctls = vmcs_read(VMCS_ENTRY_CTLS); - entry_ctls |= VM_ENTRY_GUEST_LMA; - vmcs_write(VMCS_ENTRY_CTLS, entry_ctls); - } - } - - return (HANDLED); -} - -static int -vmx_emulate_cr4_access(struct vmx *vmx, int vcpu, uint64_t exitqual) -{ - uint64_t crval, regval; - - /* We only handle mov to %cr4 at this time */ - if ((exitqual & 0xf0) != 0x00) - return (UNHANDLED); - - regval = vmx_get_guest_reg(vmx, vcpu, (exitqual >> 8) & 0xf); - - vmcs_write(VMCS_CR4_SHADOW, regval); - - crval = regval | cr4_ones_mask; - crval &= ~cr4_zeros_mask; - vmcs_write(VMCS_GUEST_CR4, crval); - - return (HANDLED); -} - -static int -vmx_emulate_cr8_access(struct vmx *vmx, int vcpu, uint64_t exitqual) -{ - struct vlapic *vlapic; - uint64_t cr8; - int regnum; - - /* We only handle mov %cr8 to/from a register at this time. */ - if ((exitqual & 0xe0) != 0x00) { - return (UNHANDLED); - } - - vlapic = vm_lapic(vmx->vm, vcpu); - regnum = (exitqual >> 8) & 0xf; - if (exitqual & 0x10) { - cr8 = vlapic_get_cr8(vlapic); - vmx_set_guest_reg(vmx, vcpu, regnum, cr8); - } else { - cr8 = vmx_get_guest_reg(vmx, vcpu, regnum); - vlapic_set_cr8(vlapic, cr8); - } - - return (HANDLED); -} - -/* - * From section "Guest Register State" in the Intel SDM: CPL = SS.DPL - */ -static int -vmx_cpl(void) -{ - uint32_t ssar; - - ssar = vmcs_read(VMCS_GUEST_SS_ACCESS_RIGHTS); - return ((ssar >> 5) & 0x3); -} - -static enum vm_cpu_mode -vmx_cpu_mode(void) -{ - uint32_t csar; - - if (vmcs_read(VMCS_GUEST_IA32_EFER) & EFER_LMA) { - csar = vmcs_read(VMCS_GUEST_CS_ACCESS_RIGHTS); - if (csar & 0x2000) - return (CPU_MODE_64BIT); /* CS.L = 1 */ - else - return (CPU_MODE_COMPATIBILITY); - } else if (vmcs_read(VMCS_GUEST_CR0) & CR0_PE) { - return (CPU_MODE_PROTECTED); - } else { - return (CPU_MODE_REAL); - } -} - -static enum vm_paging_mode -vmx_paging_mode(void) -{ - - if (!(vmcs_read(VMCS_GUEST_CR0) & CR0_PG)) - return (PAGING_MODE_FLAT); - if (!(vmcs_read(VMCS_GUEST_CR4) & CR4_PAE)) - return (PAGING_MODE_32); - if (vmcs_read(VMCS_GUEST_IA32_EFER) & EFER_LME) - return (PAGING_MODE_64); - else - return (PAGING_MODE_PAE); -} - -static uint64_t -inout_str_index(struct vmx *vmx, int vcpuid, int in) -{ - uint64_t val; - int error; - enum vm_reg_name reg; - - reg = in ? VM_REG_GUEST_RDI : VM_REG_GUEST_RSI; - error = vmx_getreg(vmx, vcpuid, reg, &val); - KASSERT(error == 0, ("%s: vmx_getreg error %d", __func__, error)); - return (val); -} - -static uint64_t -inout_str_count(struct vmx *vmx, int vcpuid, int rep) -{ - uint64_t val; - int error; - - if (rep) { - error = vmx_getreg(vmx, vcpuid, VM_REG_GUEST_RCX, &val); - KASSERT(!error, ("%s: vmx_getreg error %d", __func__, error)); - } else { - val = 1; - } - return (val); -} - -static int -inout_str_addrsize(uint32_t inst_info) -{ - uint32_t size; - - size = (inst_info >> 7) & 0x7; - switch (size) { - case 0: - return (2); /* 16 bit */ - case 1: - return (4); /* 32 bit */ - case 2: - return (8); /* 64 bit */ - default: - panic("%s: invalid size encoding %d", __func__, size); - } -} - -static void -inout_str_seginfo(struct vmx *vmx, int vcpuid, uint32_t inst_info, int in, - struct vm_inout_str *vis) -{ - int error, s; - - if (in) { - vis->seg_name = VM_REG_GUEST_ES; - } else { - s = (inst_info >> 15) & 0x7; - vis->seg_name = vm_segment_name(s); - } - - error = vmx_getdesc(vmx, vcpuid, vis->seg_name, &vis->seg_desc); - KASSERT(error == 0, ("%s: vmx_getdesc error %d", __func__, error)); -} - -static void -vmx_paging_info(struct vm_guest_paging *paging) -{ - paging->cr3 = vmcs_guest_cr3(); - paging->cpl = vmx_cpl(); - paging->cpu_mode = vmx_cpu_mode(); - paging->paging_mode = vmx_paging_mode(); -} - -static void -vmexit_inst_emul(struct vm_exit *vmexit, uint64_t gpa, uint64_t gla) -{ - struct vm_guest_paging *paging; - uint32_t csar; - - paging = &vmexit->u.inst_emul.paging; - - vmexit->exitcode = VM_EXITCODE_INST_EMUL; - vmexit->u.inst_emul.gpa = gpa; - vmexit->u.inst_emul.gla = gla; - vmx_paging_info(paging); - switch (paging->cpu_mode) { - case CPU_MODE_REAL: - vmexit->u.inst_emul.cs_base = vmcs_read(VMCS_GUEST_CS_BASE); - vmexit->u.inst_emul.cs_d = 0; - break; - case CPU_MODE_PROTECTED: - case CPU_MODE_COMPATIBILITY: - vmexit->u.inst_emul.cs_base = vmcs_read(VMCS_GUEST_CS_BASE); - csar = vmcs_read(VMCS_GUEST_CS_ACCESS_RIGHTS); - vmexit->u.inst_emul.cs_d = SEG_DESC_DEF32(csar); - break; - default: - vmexit->u.inst_emul.cs_base = 0; - vmexit->u.inst_emul.cs_d = 0; - break; - } - vie_init(&vmexit->u.inst_emul.vie, NULL, 0); -} - -static int -ept_fault_type(uint64_t ept_qual) -{ - int fault_type; - - if (ept_qual & EPT_VIOLATION_DATA_WRITE) - fault_type = VM_PROT_WRITE; - else if (ept_qual & EPT_VIOLATION_INST_FETCH) - fault_type = VM_PROT_EXECUTE; - else - fault_type= VM_PROT_READ; - - return (fault_type); -} - -static boolean_t -ept_emulation_fault(uint64_t ept_qual) -{ - int read, write; - - /* EPT fault on an instruction fetch doesn't make sense here */ - if (ept_qual & EPT_VIOLATION_INST_FETCH) - return (FALSE); - - /* EPT fault must be a read fault or a write fault */ - read = ept_qual & EPT_VIOLATION_DATA_READ ? 1 : 0; - write = ept_qual & EPT_VIOLATION_DATA_WRITE ? 1 : 0; - if ((read | write) == 0) - return (FALSE); - - /* - * The EPT violation must have been caused by accessing a - * guest-physical address that is a translation of a guest-linear - * address. - */ - if ((ept_qual & EPT_VIOLATION_GLA_VALID) == 0 || - (ept_qual & EPT_VIOLATION_XLAT_VALID) == 0) { - return (FALSE); - } - - return (TRUE); -} - -static __inline int -apic_access_virtualization(struct vmx *vmx, int vcpuid) -{ - uint32_t proc_ctls2; - - proc_ctls2 = vmx->cap[vcpuid].proc_ctls2; - return ((proc_ctls2 & PROCBASED2_VIRTUALIZE_APIC_ACCESSES) ? 1 : 0); -} - -static __inline int -x2apic_virtualization(struct vmx *vmx, int vcpuid) -{ - uint32_t proc_ctls2; - - proc_ctls2 = vmx->cap[vcpuid].proc_ctls2; - return ((proc_ctls2 & PROCBASED2_VIRTUALIZE_X2APIC_MODE) ? 1 : 0); -} - -static int -vmx_handle_apic_write(struct vmx *vmx, int vcpuid, struct vlapic *vlapic, - uint64_t qual) -{ - int error, handled, offset; - uint32_t *apic_regs, vector; - bool retu; - - handled = HANDLED; - offset = APIC_WRITE_OFFSET(qual); - - if (!apic_access_virtualization(vmx, vcpuid)) { - /* - * In general there should not be any APIC write VM-exits - * unless APIC-access virtualization is enabled. - * - * However self-IPI virtualization can legitimately trigger - * an APIC-write VM-exit so treat it specially. - */ - if (x2apic_virtualization(vmx, vcpuid) && - offset == APIC_OFFSET_SELF_IPI) { - apic_regs = (uint32_t *)(vlapic->apic_page); - vector = apic_regs[APIC_OFFSET_SELF_IPI / 4]; - vlapic_self_ipi_handler(vlapic, vector); - return (HANDLED); - } else - return (UNHANDLED); - } - - switch (offset) { - case APIC_OFFSET_ID: - vlapic_id_write_handler(vlapic); - break; - case APIC_OFFSET_LDR: - vlapic_ldr_write_handler(vlapic); - break; - case APIC_OFFSET_DFR: - vlapic_dfr_write_handler(vlapic); - break; - case APIC_OFFSET_SVR: - vlapic_svr_write_handler(vlapic); - break; - case APIC_OFFSET_ESR: - vlapic_esr_write_handler(vlapic); - break; - case APIC_OFFSET_ICR_LOW: - retu = false; - error = vlapic_icrlo_write_handler(vlapic, &retu); - if (error != 0 || retu) - handled = UNHANDLED; - break; - case APIC_OFFSET_CMCI_LVT: - case APIC_OFFSET_TIMER_LVT ... APIC_OFFSET_ERROR_LVT: - vlapic_lvt_write_handler(vlapic, offset); - break; - case APIC_OFFSET_TIMER_ICR: - vlapic_icrtmr_write_handler(vlapic); - break; - case APIC_OFFSET_TIMER_DCR: - vlapic_dcr_write_handler(vlapic); - break; - default: - handled = UNHANDLED; - break; - } - return (handled); -} - -static bool -apic_access_fault(struct vmx *vmx, int vcpuid, uint64_t gpa) -{ - - if (apic_access_virtualization(vmx, vcpuid) && - (gpa >= DEFAULT_APIC_BASE && gpa < DEFAULT_APIC_BASE + PAGE_SIZE)) - return (true); - else - return (false); -} - -static int -vmx_handle_apic_access(struct vmx *vmx, int vcpuid, struct vm_exit *vmexit) -{ - uint64_t qual; - int access_type, offset, allowed; - - if (!apic_access_virtualization(vmx, vcpuid)) - return (UNHANDLED); - - qual = vmexit->u.vmx.exit_qualification; - access_type = APIC_ACCESS_TYPE(qual); - offset = APIC_ACCESS_OFFSET(qual); - - allowed = 0; - if (access_type == 0) { - /* - * Read data access to the following registers is expected. - */ - switch (offset) { - case APIC_OFFSET_APR: - case APIC_OFFSET_PPR: - case APIC_OFFSET_RRR: - case APIC_OFFSET_CMCI_LVT: - case APIC_OFFSET_TIMER_CCR: - allowed = 1; - break; - default: - break; - } - } else if (access_type == 1) { - /* - * Write data access to the following registers is expected. - */ - switch (offset) { - case APIC_OFFSET_VER: - case APIC_OFFSET_APR: - case APIC_OFFSET_PPR: - case APIC_OFFSET_RRR: - case APIC_OFFSET_ISR0 ... APIC_OFFSET_ISR7: - case APIC_OFFSET_TMR0 ... APIC_OFFSET_TMR7: - case APIC_OFFSET_IRR0 ... APIC_OFFSET_IRR7: - case APIC_OFFSET_CMCI_LVT: - case APIC_OFFSET_TIMER_CCR: - allowed = 1; - break; - default: - break; - } - } - - if (allowed) { - vmexit_inst_emul(vmexit, DEFAULT_APIC_BASE + offset, - VIE_INVALID_GLA); - } - - /* - * Regardless of whether the APIC-access is allowed this handler - * always returns UNHANDLED: - * - if the access is allowed then it is handled by emulating the - * instruction that caused the VM-exit (outside the critical section) - * - if the access is not allowed then it will be converted to an - * exitcode of VM_EXITCODE_VMX and will be dealt with in userland. - */ - return (UNHANDLED); -} - -static enum task_switch_reason -vmx_task_switch_reason(uint64_t qual) -{ - int reason; - - reason = (qual >> 30) & 0x3; - switch (reason) { - case 0: - return (TSR_CALL); - case 1: - return (TSR_IRET); - case 2: - return (TSR_JMP); - case 3: - return (TSR_IDT_GATE); - default: - panic("%s: invalid reason %d", __func__, reason); - } -} - -static int -emulate_wrmsr(struct vmx *vmx, int vcpuid, u_int num, uint64_t val, bool *retu) -{ - int error; - - if (lapic_msr(num)) - error = lapic_wrmsr(vmx->vm, vcpuid, num, val, retu); - else - error = vmx_wrmsr(vmx, vcpuid, num, val, retu); - - return (error); -} - -static int -emulate_rdmsr(struct vmx *vmx, int vcpuid, u_int num, bool *retu) -{ - struct vmxctx *vmxctx; - uint64_t result; - uint32_t eax, edx; - int error; - - if (lapic_msr(num)) - error = lapic_rdmsr(vmx->vm, vcpuid, num, &result, retu); - else - error = vmx_rdmsr(vmx, vcpuid, num, &result, retu); - - if (error == 0) { - eax = result; - vmxctx = &vmx->ctx[vcpuid]; - error = vmxctx_setreg(vmxctx, VM_REG_GUEST_RAX, eax); - KASSERT(error == 0, ("vmxctx_setreg(rax) error %d", error)); - - edx = result >> 32; - error = vmxctx_setreg(vmxctx, VM_REG_GUEST_RDX, edx); - KASSERT(error == 0, ("vmxctx_setreg(rdx) error %d", error)); - } - - return (error); -} - -static int -vmx_exit_process(struct vmx *vmx, int vcpu, struct vm_exit *vmexit) -{ - int error, errcode, errcode_valid, handled, in; - struct vmxctx *vmxctx; - struct vlapic *vlapic; - struct vm_inout_str *vis; - struct vm_task_switch *ts; - uint32_t eax, ecx, edx, idtvec_info, idtvec_err, intr_info, inst_info; - uint32_t intr_type, intr_vec, reason; - uint64_t exitintinfo, qual, gpa; - bool retu; - - CTASSERT((PINBASED_CTLS_ONE_SETTING & PINBASED_VIRTUAL_NMI) != 0); - CTASSERT((PINBASED_CTLS_ONE_SETTING & PINBASED_NMI_EXITING) != 0); - - handled = UNHANDLED; - vmxctx = &vmx->ctx[vcpu]; - - qual = vmexit->u.vmx.exit_qualification; - reason = vmexit->u.vmx.exit_reason; - vmexit->exitcode = VM_EXITCODE_BOGUS; - - vmm_stat_incr(vmx->vm, vcpu, VMEXIT_COUNT, 1); - - /* - * VM-entry failures during or after loading guest state. - * - * These VM-exits are uncommon but must be handled specially - * as most VM-exit fields are not populated as usual. - */ - if (__predict_false(reason == EXIT_REASON_MCE_DURING_ENTRY)) { - VCPU_CTR0(vmx->vm, vcpu, "Handling MCE during VM-entry"); - __asm __volatile("int $18"); - return (1); - } - - /* - * VM exits that can be triggered during event delivery need to - * be handled specially by re-injecting the event if the IDT - * vectoring information field's valid bit is set. - * - * See "Information for VM Exits During Event Delivery" in Intel SDM - * for details. - */ - idtvec_info = vmcs_idt_vectoring_info(); - if (idtvec_info & VMCS_IDT_VEC_VALID) { - idtvec_info &= ~(1 << 12); /* clear undefined bit */ - exitintinfo = idtvec_info; - if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) { - idtvec_err = vmcs_idt_vectoring_err(); - exitintinfo |= (uint64_t)idtvec_err << 32; - } - error = vm_exit_intinfo(vmx->vm, vcpu, exitintinfo); - KASSERT(error == 0, ("%s: vm_set_intinfo error %d", - __func__, error)); - - /* - * If 'virtual NMIs' are being used and the VM-exit - * happened while injecting an NMI during the previous - * VM-entry, then clear "blocking by NMI" in the - * Guest Interruptibility-State so the NMI can be - * reinjected on the subsequent VM-entry. - * - * However, if the NMI was being delivered through a task - * gate, then the new task must start execution with NMIs - * blocked so don't clear NMI blocking in this case. - */ - intr_type = idtvec_info & VMCS_INTR_T_MASK; - if (intr_type == VMCS_INTR_T_NMI) { - if (reason != EXIT_REASON_TASK_SWITCH) - vmx_clear_nmi_blocking(vmx, vcpu); - else - vmx_assert_nmi_blocking(vmx, vcpu); - } - - /* - * Update VM-entry instruction length if the event being - * delivered was a software interrupt or software exception. - */ - if (intr_type == VMCS_INTR_T_SWINTR || - intr_type == VMCS_INTR_T_PRIV_SWEXCEPTION || - intr_type == VMCS_INTR_T_SWEXCEPTION) { - vmcs_write(VMCS_ENTRY_INST_LENGTH, vmexit->inst_length); - } - } - - switch (reason) { - case EXIT_REASON_TASK_SWITCH: - ts = &vmexit->u.task_switch; - ts->tsssel = qual & 0xffff; - ts->reason = vmx_task_switch_reason(qual); - ts->ext = 0; - ts->errcode_valid = 0; - vmx_paging_info(&ts->paging); - /* - * If the task switch was due to a CALL, JMP, IRET, software - * interrupt (INT n) or software exception (INT3, INTO), - * then the saved %rip references the instruction that caused - * the task switch. The instruction length field in the VMCS - * is valid in this case. - * - * In all other cases (e.g., NMI, hardware exception) the - * saved %rip is one that would have been saved in the old TSS - * had the task switch completed normally so the instruction - * length field is not needed in this case and is explicitly - * set to 0. - */ - if (ts->reason == TSR_IDT_GATE) { - KASSERT(idtvec_info & VMCS_IDT_VEC_VALID, - ("invalid idtvec_info %#x for IDT task switch", - idtvec_info)); - intr_type = idtvec_info & VMCS_INTR_T_MASK; - if (intr_type != VMCS_INTR_T_SWINTR && - intr_type != VMCS_INTR_T_SWEXCEPTION && - intr_type != VMCS_INTR_T_PRIV_SWEXCEPTION) { - /* Task switch triggered by external event */ - ts->ext = 1; - vmexit->inst_length = 0; - if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) { - ts->errcode_valid = 1; - ts->errcode = vmcs_idt_vectoring_err(); - } - } - } - vmexit->exitcode = VM_EXITCODE_TASK_SWITCH; - VCPU_CTR4(vmx->vm, vcpu, "task switch reason %d, tss 0x%04x, " - "%s errcode 0x%016lx", ts->reason, ts->tsssel, - ts->ext ? "external" : "internal", - ((uint64_t)ts->errcode << 32) | ts->errcode_valid); - break; - case EXIT_REASON_CR_ACCESS: - vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CR_ACCESS, 1); - switch (qual & 0xf) { - case 0: - handled = vmx_emulate_cr0_access(vmx, vcpu, qual); - break; - case 4: - handled = vmx_emulate_cr4_access(vmx, vcpu, qual); - break; - case 8: - handled = vmx_emulate_cr8_access(vmx, vcpu, qual); - break; - } - break; - case EXIT_REASON_RDMSR: - vmm_stat_incr(vmx->vm, vcpu, VMEXIT_RDMSR, 1); - retu = false; - ecx = vmxctx->guest_rcx; - VCPU_CTR1(vmx->vm, vcpu, "rdmsr 0x%08x", ecx); - error = emulate_rdmsr(vmx, vcpu, ecx, &retu); - if (error) { - vmexit->exitcode = VM_EXITCODE_RDMSR; - vmexit->u.msr.code = ecx; - } else if (!retu) { - handled = HANDLED; - } else { - /* Return to userspace with a valid exitcode */ - KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS, - ("emulate_rdmsr retu with bogus exitcode")); - } - break; - case EXIT_REASON_WRMSR: - vmm_stat_incr(vmx->vm, vcpu, VMEXIT_WRMSR, 1); - retu = false; - eax = vmxctx->guest_rax; - ecx = vmxctx->guest_rcx; - edx = vmxctx->guest_rdx; - VCPU_CTR2(vmx->vm, vcpu, "wrmsr 0x%08x value 0x%016lx", - ecx, (uint64_t)edx << 32 | eax); - error = emulate_wrmsr(vmx, vcpu, ecx, - (uint64_t)edx << 32 | eax, &retu); - if (error) { - vmexit->exitcode = VM_EXITCODE_WRMSR; - vmexit->u.msr.code = ecx; - vmexit->u.msr.wval = (uint64_t)edx << 32 | eax; - } else if (!retu) { - handled = HANDLED; - } else { - /* Return to userspace with a valid exitcode */ - KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS, - ("emulate_wrmsr retu with bogus exitcode")); - } - break; - case EXIT_REASON_HLT: - vmm_stat_incr(vmx->vm, vcpu, VMEXIT_HLT, 1); - vmexit->exitcode = VM_EXITCODE_HLT; - vmexit->u.hlt.rflags = vmcs_read(VMCS_GUEST_RFLAGS); - break; - case EXIT_REASON_MTF: - vmm_stat_incr(vmx->vm, vcpu, VMEXIT_MTRAP, 1); - vmexit->exitcode = VM_EXITCODE_MTRAP; - vmexit->inst_length = 0; - break; - case EXIT_REASON_PAUSE: - vmm_stat_incr(vmx->vm, vcpu, VMEXIT_PAUSE, 1); - vmexit->exitcode = VM_EXITCODE_PAUSE; - break; - case EXIT_REASON_INTR_WINDOW: - vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INTR_WINDOW, 1); - vmx_clear_int_window_exiting(vmx, vcpu); - return (1); - case EXIT_REASON_EXT_INTR: - /* - * External interrupts serve only to cause VM exits and allow - * the host interrupt handler to run. - * - * If this external interrupt triggers a virtual interrupt - * to a VM, then that state will be recorded by the - * host interrupt handler in the VM's softc. We will inject - * this virtual interrupt during the subsequent VM enter. - */ - intr_info = vmcs_read(VMCS_EXIT_INTR_INFO); - - /* - * XXX: Ignore this exit if VMCS_INTR_VALID is not set. - * This appears to be a bug in VMware Fusion? - */ - if (!(intr_info & VMCS_INTR_VALID)) - return (1); - KASSERT((intr_info & VMCS_INTR_VALID) != 0 && - (intr_info & VMCS_INTR_T_MASK) == VMCS_INTR_T_HWINTR, - ("VM exit interruption info invalid: %#x", intr_info)); - vmx_trigger_hostintr(intr_info & 0xff); - - /* - * This is special. We want to treat this as an 'handled' - * VM-exit but not increment the instruction pointer. - */ - vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXTINT, 1); - return (1); - case EXIT_REASON_NMI_WINDOW: - /* Exit to allow the pending virtual NMI to be injected */ - if (vm_nmi_pending(vmx->vm, vcpu)) - vmx_inject_nmi(vmx, vcpu); - vmx_clear_nmi_window_exiting(vmx, vcpu); - vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NMI_WINDOW, 1); - return (1); - case EXIT_REASON_INOUT: - vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INOUT, 1); - vmexit->exitcode = VM_EXITCODE_INOUT; - vmexit->u.inout.bytes = (qual & 0x7) + 1; - vmexit->u.inout.in = in = (qual & 0x8) ? 1 : 0; - vmexit->u.inout.string = (qual & 0x10) ? 1 : 0; - vmexit->u.inout.rep = (qual & 0x20) ? 1 : 0; - vmexit->u.inout.port = (uint16_t)(qual >> 16); - vmexit->u.inout.eax = (uint32_t)(vmxctx->guest_rax); - if (vmexit->u.inout.string) { - inst_info = vmcs_read(VMCS_EXIT_INSTRUCTION_INFO); - vmexit->exitcode = VM_EXITCODE_INOUT_STR; - vis = &vmexit->u.inout_str; - vmx_paging_info(&vis->paging); - vis->rflags = vmcs_read(VMCS_GUEST_RFLAGS); - vis->cr0 = vmcs_read(VMCS_GUEST_CR0); - vis->index = inout_str_index(vmx, vcpu, in); - vis->count = inout_str_count(vmx, vcpu, vis->inout.rep); - vis->addrsize = inout_str_addrsize(inst_info); - inout_str_seginfo(vmx, vcpu, inst_info, in, vis); - } - break; - case EXIT_REASON_CPUID: - vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CPUID, 1); - handled = vmx_handle_cpuid(vmx->vm, vcpu, vmxctx); - break; - case EXIT_REASON_EXCEPTION: - vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXCEPTION, 1); - intr_info = vmcs_read(VMCS_EXIT_INTR_INFO); - KASSERT((intr_info & VMCS_INTR_VALID) != 0, - ("VM exit interruption info invalid: %#x", intr_info)); - - intr_vec = intr_info & 0xff; - intr_type = intr_info & VMCS_INTR_T_MASK; - - /* - * If Virtual NMIs control is 1 and the VM-exit is due to a - * fault encountered during the execution of IRET then we must - * restore the state of "virtual-NMI blocking" before resuming - * the guest. - * - * See "Resuming Guest Software after Handling an Exception". - * See "Information for VM Exits Due to Vectored Events". - */ - if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 && - (intr_vec != IDT_DF) && - (intr_info & EXIT_QUAL_NMIUDTI) != 0) - vmx_restore_nmi_blocking(vmx, vcpu); - - /* - * The NMI has already been handled in vmx_exit_handle_nmi(). - */ - if (intr_type == VMCS_INTR_T_NMI) - return (1); - - /* - * Call the machine check handler by hand. Also don't reflect - * the machine check back into the guest. - */ - if (intr_vec == IDT_MC) { - VCPU_CTR0(vmx->vm, vcpu, "Vectoring to MCE handler"); - __asm __volatile("int $18"); - return (1); - } - - if (intr_vec == IDT_PF) { - error = vmxctx_setreg(vmxctx, VM_REG_GUEST_CR2, qual); - KASSERT(error == 0, ("%s: vmxctx_setreg(cr2) error %d", - __func__, error)); - } - - /* - * Software exceptions exhibit trap-like behavior. This in - * turn requires populating the VM-entry instruction length - * so that the %rip in the trap frame is past the INT3/INTO - * instruction. - */ - if (intr_type == VMCS_INTR_T_SWEXCEPTION) - vmcs_write(VMCS_ENTRY_INST_LENGTH, vmexit->inst_length); - - /* Reflect all other exceptions back into the guest */ - errcode_valid = errcode = 0; - if (intr_info & VMCS_INTR_DEL_ERRCODE) { - errcode_valid = 1; - errcode = vmcs_read(VMCS_EXIT_INTR_ERRCODE); - } - VCPU_CTR2(vmx->vm, vcpu, "Reflecting exception %d/%#x into " - "the guest", intr_vec, errcode); - error = vm_inject_exception(vmx->vm, vcpu, intr_vec, - errcode_valid, errcode, 0); - KASSERT(error == 0, ("%s: vm_inject_exception error %d", - __func__, error)); - return (1); - - case EXIT_REASON_EPT_FAULT: - /* - * If 'gpa' lies within the address space allocated to - * memory then this must be a nested page fault otherwise - * this must be an instruction that accesses MMIO space. - */ - gpa = vmcs_gpa(); - if (vm_mem_allocated(vmx->vm, gpa) || - apic_access_fault(vmx, vcpu, gpa)) { - vmexit->exitcode = VM_EXITCODE_PAGING; - vmexit->inst_length = 0; - vmexit->u.paging.gpa = gpa; - vmexit->u.paging.fault_type = ept_fault_type(qual); - vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NESTED_FAULT, 1); - } else if (ept_emulation_fault(qual)) { - vmexit_inst_emul(vmexit, gpa, vmcs_gla()); - vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INST_EMUL, 1); - } - /* - * If Virtual NMIs control is 1 and the VM-exit is due to an - * EPT fault during the execution of IRET then we must restore - * the state of "virtual-NMI blocking" before resuming. - * - * See description of "NMI unblocking due to IRET" in - * "Exit Qualification for EPT Violations". - */ - if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 && - (qual & EXIT_QUAL_NMIUDTI) != 0) - vmx_restore_nmi_blocking(vmx, vcpu); - break; - case EXIT_REASON_VIRTUALIZED_EOI: - vmexit->exitcode = VM_EXITCODE_IOAPIC_EOI; - vmexit->u.ioapic_eoi.vector = qual & 0xFF; - vmexit->inst_length = 0; /* trap-like */ - break; - case EXIT_REASON_APIC_ACCESS: - handled = vmx_handle_apic_access(vmx, vcpu, vmexit); - break; - case EXIT_REASON_APIC_WRITE: - /* - * APIC-write VM exit is trap-like so the %rip is already - * pointing to the next instruction. - */ - vmexit->inst_length = 0; - vlapic = vm_lapic(vmx->vm, vcpu); - handled = vmx_handle_apic_write(vmx, vcpu, vlapic, qual); - break; - case EXIT_REASON_XSETBV: - handled = vmx_emulate_xsetbv(vmx, vcpu, vmexit); - break; - case EXIT_REASON_MONITOR: - vmexit->exitcode = VM_EXITCODE_MONITOR; - break; - case EXIT_REASON_MWAIT: - vmexit->exitcode = VM_EXITCODE_MWAIT; - break; - default: - vmm_stat_incr(vmx->vm, vcpu, VMEXIT_UNKNOWN, 1); - break; - } - - if (handled) { - /* - * It is possible that control is returned to userland - * even though we were able to handle the VM exit in the - * kernel. - * - * In such a case we want to make sure that the userland - * restarts guest execution at the instruction *after* - * the one we just processed. Therefore we update the - * guest rip in the VMCS and in 'vmexit'. - */ - vmexit->rip += vmexit->inst_length; - vmexit->inst_length = 0; - vmcs_write(VMCS_GUEST_RIP, vmexit->rip); - } else { - if (vmexit->exitcode == VM_EXITCODE_BOGUS) { - /* - * If this VM exit was not claimed by anybody then - * treat it as a generic VMX exit. - */ - vmexit->exitcode = VM_EXITCODE_VMX; - vmexit->u.vmx.status = VM_SUCCESS; - vmexit->u.vmx.inst_type = 0; - vmexit->u.vmx.inst_error = 0; - } else { - /* - * The exitcode and collateral have been populated. - * The VM exit will be processed further in userland. - */ - } - } - return (handled); -} - -static __inline void -vmx_exit_inst_error(struct vmxctx *vmxctx, int rc, struct vm_exit *vmexit) -{ - - KASSERT(vmxctx->inst_fail_status != VM_SUCCESS, - ("vmx_exit_inst_error: invalid inst_fail_status %d", - vmxctx->inst_fail_status)); - - vmexit->inst_length = 0; - vmexit->exitcode = VM_EXITCODE_VMX; - vmexit->u.vmx.status = vmxctx->inst_fail_status; - vmexit->u.vmx.inst_error = vmcs_instruction_error(); - vmexit->u.vmx.exit_reason = ~0; - vmexit->u.vmx.exit_qualification = ~0; - - switch (rc) { - case VMX_VMRESUME_ERROR: - case VMX_VMLAUNCH_ERROR: - case VMX_INVEPT_ERROR: - vmexit->u.vmx.inst_type = rc; - break; - default: - panic("vm_exit_inst_error: vmx_enter_guest returned %d", rc); - } -} - -/* - * If the NMI-exiting VM execution control is set to '1' then an NMI in - * non-root operation causes a VM-exit. NMI blocking is in effect so it is - * sufficient to simply vector to the NMI handler via a software interrupt. - * However, this must be done before maskable interrupts are enabled - * otherwise the "iret" issued by an interrupt handler will incorrectly - * clear NMI blocking. - */ -static __inline void -vmx_exit_handle_nmi(struct vmx *vmx, int vcpuid, struct vm_exit *vmexit) -{ - uint32_t intr_info; - - KASSERT((read_rflags() & PSL_I) == 0, ("interrupts enabled")); - - if (vmexit->u.vmx.exit_reason != EXIT_REASON_EXCEPTION) - return; - - intr_info = vmcs_read(VMCS_EXIT_INTR_INFO); - KASSERT((intr_info & VMCS_INTR_VALID) != 0, - ("VM exit interruption info invalid: %#x", intr_info)); - - if ((intr_info & VMCS_INTR_T_MASK) == VMCS_INTR_T_NMI) { - KASSERT((intr_info & 0xff) == IDT_NMI, ("VM exit due " - "to NMI has invalid vector: %#x", intr_info)); - VCPU_CTR0(vmx->vm, vcpuid, "Vectoring to NMI handler"); - __asm __volatile("int $2"); - } -} - -static int -vmx_run(void *arg, int vcpu, register_t rip, pmap_t pmap, - void *rendezvous_cookie, void *suspend_cookie) -{ - int rc, handled, launched; - struct vmx *vmx; - struct vm *vm; - struct vmxctx *vmxctx; - struct vmcs *vmcs; - struct vm_exit *vmexit; - struct vlapic *vlapic; - uint32_t exit_reason; - - vmx = arg; - vm = vmx->vm; - vmcs = &vmx->vmcs[vcpu]; - vmxctx = &vmx->ctx[vcpu]; - vlapic = vm_lapic(vm, vcpu); - vmexit = vm_exitinfo(vm, vcpu); - launched = 0; - - KASSERT(vmxctx->pmap == pmap, - ("pmap %p different than ctx pmap %p", pmap, vmxctx->pmap)); - - vmx_msr_guest_enter(vmx, vcpu); - - VMPTRLD(vmcs); - - /* - * XXX - * We do this every time because we may setup the virtual machine - * from a different process than the one that actually runs it. - * - * If the life of a virtual machine was spent entirely in the context - * of a single process we could do this once in vmx_vminit(). - */ - vmcs_write(VMCS_HOST_CR3, rcr3()); - - vmcs_write(VMCS_GUEST_RIP, rip); - vmx_set_pcpu_defaults(vmx, vcpu, pmap); - do { - KASSERT(vmcs_guest_rip() == rip, ("%s: vmcs guest rip mismatch " - "%#lx/%#lx", __func__, vmcs_guest_rip(), rip)); - - handled = UNHANDLED; - /* - * Interrupts are disabled from this point on until the - * guest starts executing. This is done for the following - * reasons: - * - * If an AST is asserted on this thread after the check below, - * then the IPI_AST notification will not be lost, because it - * will cause a VM exit due to external interrupt as soon as - * the guest state is loaded. - * - * A posted interrupt after 'vmx_inject_interrupts()' will - * not be "lost" because it will be held pending in the host - * APIC because interrupts are disabled. The pending interrupt - * will be recognized as soon as the guest state is loaded. - * - * The same reasoning applies to the IPI generated by - * pmap_invalidate_ept(). - */ - disable_intr(); - vmx_inject_interrupts(vmx, vcpu, vlapic, rip); - - /* - * Check for vcpu suspension after injecting events because - * vmx_inject_interrupts() can suspend the vcpu due to a - * triple fault. - */ - if (vcpu_suspended(suspend_cookie)) { - enable_intr(); - vm_exit_suspended(vmx->vm, vcpu, rip); - break; - } - - if (vcpu_rendezvous_pending(rendezvous_cookie)) { - enable_intr(); - vm_exit_rendezvous(vmx->vm, vcpu, rip); - break; - } - - if (vcpu_should_yield(vm, vcpu)) { - enable_intr(); - vm_exit_astpending(vmx->vm, vcpu, rip); - vmx_astpending_trace(vmx, vcpu, rip); - handled = HANDLED; - break; - } - - vmx_run_trace(vmx, vcpu); - rc = vmx_enter_guest(vmxctx, vmx, launched); - - /* Collect some information for VM exit processing */ - vmexit->rip = rip = vmcs_guest_rip(); - vmexit->inst_length = vmexit_instruction_length(); - vmexit->u.vmx.exit_reason = exit_reason = vmcs_exit_reason(); - vmexit->u.vmx.exit_qualification = vmcs_exit_qualification(); - - /* Update 'nextrip' */ - vmx->state[vcpu].nextrip = rip; - - if (rc == VMX_GUEST_VMEXIT) { - vmx_exit_handle_nmi(vmx, vcpu, vmexit); - enable_intr(); - handled = vmx_exit_process(vmx, vcpu, vmexit); - } else { - enable_intr(); - vmx_exit_inst_error(vmxctx, rc, vmexit); - } - launched = 1; - vmx_exit_trace(vmx, vcpu, rip, exit_reason, handled); - rip = vmexit->rip; - } while (handled); - - /* - * If a VM exit has been handled then the exitcode must be BOGUS - * If a VM exit is not handled then the exitcode must not be BOGUS - */ - if ((handled && vmexit->exitcode != VM_EXITCODE_BOGUS) || - (!handled && vmexit->exitcode == VM_EXITCODE_BOGUS)) { - panic("Mismatch between handled (%d) and exitcode (%d)", - handled, vmexit->exitcode); - } - - if (!handled) - vmm_stat_incr(vm, vcpu, VMEXIT_USERSPACE, 1); - - VCPU_CTR1(vm, vcpu, "returning from vmx_run: exitcode %d", - vmexit->exitcode); - - VMCLEAR(vmcs); - vmx_msr_guest_exit(vmx, vcpu); - - return (0); -} - -static void -vmx_vmcleanup(void *arg) -{ - int i; - struct vmx *vmx = arg; - - if (apic_access_virtualization(vmx, 0)) - vm_unmap_mmio(vmx->vm, DEFAULT_APIC_BASE, PAGE_SIZE); - - for (i = 0; i < VM_MAXCPU; i++) - vpid_free(vmx->state[i].vpid); - - free(vmx, M_VMX); - - return; -} - -static register_t * -vmxctx_regptr(struct vmxctx *vmxctx, int reg) -{ - - switch (reg) { - case VM_REG_GUEST_RAX: - return (&vmxctx->guest_rax); - case VM_REG_GUEST_RBX: - return (&vmxctx->guest_rbx); - case VM_REG_GUEST_RCX: - return (&vmxctx->guest_rcx); - case VM_REG_GUEST_RDX: - return (&vmxctx->guest_rdx); - case VM_REG_GUEST_RSI: - return (&vmxctx->guest_rsi); - case VM_REG_GUEST_RDI: - return (&vmxctx->guest_rdi); - case VM_REG_GUEST_RBP: - return (&vmxctx->guest_rbp); - case VM_REG_GUEST_R8: - return (&vmxctx->guest_r8); - case VM_REG_GUEST_R9: - return (&vmxctx->guest_r9); - case VM_REG_GUEST_R10: - return (&vmxctx->guest_r10); - case VM_REG_GUEST_R11: - return (&vmxctx->guest_r11); - case VM_REG_GUEST_R12: - return (&vmxctx->guest_r12); - case VM_REG_GUEST_R13: - return (&vmxctx->guest_r13); - case VM_REG_GUEST_R14: - return (&vmxctx->guest_r14); - case VM_REG_GUEST_R15: - return (&vmxctx->guest_r15); - case VM_REG_GUEST_CR2: - return (&vmxctx->guest_cr2); - default: - break; - } - return (NULL); -} - -static int -vmxctx_getreg(struct vmxctx *vmxctx, int reg, uint64_t *retval) -{ - register_t *regp; - - if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) { - *retval = *regp; - return (0); - } else - return (EINVAL); -} - -static int -vmxctx_setreg(struct vmxctx *vmxctx, int reg, uint64_t val) -{ - register_t *regp; - - if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) { - *regp = val; - return (0); - } else - return (EINVAL); -} - -static int -vmx_get_intr_shadow(struct vmx *vmx, int vcpu, int running, uint64_t *retval) -{ - uint64_t gi; - int error; - - error = vmcs_getreg(&vmx->vmcs[vcpu], running, - VMCS_IDENT(VMCS_GUEST_INTERRUPTIBILITY), &gi); - *retval = (gi & HWINTR_BLOCKING) ? 1 : 0; - return (error); -} - -static int -vmx_modify_intr_shadow(struct vmx *vmx, int vcpu, int running, uint64_t val) -{ - struct vmcs *vmcs; - uint64_t gi; - int error, ident; - - /* - * Forcing the vcpu into an interrupt shadow is not supported. - */ - if (val) { - error = EINVAL; - goto done; - } - - vmcs = &vmx->vmcs[vcpu]; - ident = VMCS_IDENT(VMCS_GUEST_INTERRUPTIBILITY); - error = vmcs_getreg(vmcs, running, ident, &gi); - if (error == 0) { - gi &= ~HWINTR_BLOCKING; - error = vmcs_setreg(vmcs, running, ident, gi); - } -done: - VCPU_CTR2(vmx->vm, vcpu, "Setting intr_shadow to %#lx %s", val, - error ? "failed" : "succeeded"); - return (error); -} - -static int -vmx_shadow_reg(int reg) -{ - int shreg; - - shreg = -1; - - switch (reg) { - case VM_REG_GUEST_CR0: - shreg = VMCS_CR0_SHADOW; - break; - case VM_REG_GUEST_CR4: - shreg = VMCS_CR4_SHADOW; - break; - default: - break; - } - - return (shreg); -} - -static int -vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval) -{ - int running, hostcpu; - struct vmx *vmx = arg; - - running = vcpu_is_running(vmx->vm, vcpu, &hostcpu); - if (running && hostcpu != curcpu) - panic("vmx_getreg: %s%d is running", vm_name(vmx->vm), vcpu); - - if (reg == VM_REG_GUEST_INTR_SHADOW) - return (vmx_get_intr_shadow(vmx, vcpu, running, retval)); - - if (vmxctx_getreg(&vmx->ctx[vcpu], reg, retval) == 0) - return (0); - - return (vmcs_getreg(&vmx->vmcs[vcpu], running, reg, retval)); -} - -static int -vmx_setreg(void *arg, int vcpu, int reg, uint64_t val) -{ - int error, hostcpu, running, shadow; - uint64_t ctls; - pmap_t pmap; - struct vmx *vmx = arg; - - running = vcpu_is_running(vmx->vm, vcpu, &hostcpu); - if (running && hostcpu != curcpu) - panic("vmx_setreg: %s%d is running", vm_name(vmx->vm), vcpu); - - if (reg == VM_REG_GUEST_INTR_SHADOW) - return (vmx_modify_intr_shadow(vmx, vcpu, running, val)); - - if (vmxctx_setreg(&vmx->ctx[vcpu], reg, val) == 0) - return (0); - - error = vmcs_setreg(&vmx->vmcs[vcpu], running, reg, val); - - if (error == 0) { - /* - * If the "load EFER" VM-entry control is 1 then the - * value of EFER.LMA must be identical to "IA-32e mode guest" - * bit in the VM-entry control. - */ - if ((entry_ctls & VM_ENTRY_LOAD_EFER) != 0 && - (reg == VM_REG_GUEST_EFER)) { - vmcs_getreg(&vmx->vmcs[vcpu], running, - VMCS_IDENT(VMCS_ENTRY_CTLS), &ctls); - if (val & EFER_LMA) - ctls |= VM_ENTRY_GUEST_LMA; - else - ctls &= ~VM_ENTRY_GUEST_LMA; - vmcs_setreg(&vmx->vmcs[vcpu], running, - VMCS_IDENT(VMCS_ENTRY_CTLS), ctls); - } - - shadow = vmx_shadow_reg(reg); - if (shadow > 0) { - /* - * Store the unmodified value in the shadow - */ - error = vmcs_setreg(&vmx->vmcs[vcpu], running, - VMCS_IDENT(shadow), val); - } - - if (reg == VM_REG_GUEST_CR3) { - /* - * Invalidate the guest vcpu's TLB mappings to emulate - * the behavior of updating %cr3. - * - * XXX the processor retains global mappings when %cr3 - * is updated but vmx_invvpid() does not. - */ - pmap = vmx->ctx[vcpu].pmap; - vmx_invvpid(vmx, vcpu, pmap, running); - } - } - - return (error); -} - -static int -vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc) -{ - int hostcpu, running; - struct vmx *vmx = arg; - - running = vcpu_is_running(vmx->vm, vcpu, &hostcpu); - if (running && hostcpu != curcpu) - panic("vmx_getdesc: %s%d is running", vm_name(vmx->vm), vcpu); - - return (vmcs_getdesc(&vmx->vmcs[vcpu], running, reg, desc)); -} - -static int -vmx_setdesc(void *arg, int vcpu, int reg, struct seg_desc *desc) -{ - int hostcpu, running; - struct vmx *vmx = arg; - - running = vcpu_is_running(vmx->vm, vcpu, &hostcpu); - if (running && hostcpu != curcpu) - panic("vmx_setdesc: %s%d is running", vm_name(vmx->vm), vcpu); - - return (vmcs_setdesc(&vmx->vmcs[vcpu], running, reg, desc)); -} - -static int -vmx_getcap(void *arg, int vcpu, int type, int *retval) -{ - struct vmx *vmx = arg; - int vcap; - int ret; - - ret = ENOENT; - - vcap = vmx->cap[vcpu].set; - - switch (type) { - case VM_CAP_HALT_EXIT: - if (cap_halt_exit) - ret = 0; - break; - case VM_CAP_PAUSE_EXIT: - if (cap_pause_exit) - ret = 0; - break; - case VM_CAP_MTRAP_EXIT: - if (cap_monitor_trap) - ret = 0; - break; - case VM_CAP_UNRESTRICTED_GUEST: - if (cap_unrestricted_guest) - ret = 0; - break; - case VM_CAP_ENABLE_INVPCID: - if (cap_invpcid) - ret = 0; - break; - default: - break; - } - - if (ret == 0) - *retval = (vcap & (1 << type)) ? 1 : 0; - - return (ret); -} - -static int -vmx_setcap(void *arg, int vcpu, int type, int val) -{ - struct vmx *vmx = arg; - struct vmcs *vmcs = &vmx->vmcs[vcpu]; - uint32_t baseval; - uint32_t *pptr; - int error; - int flag; - int reg; - int retval; - - retval = ENOENT; - pptr = NULL; - - switch (type) { - case VM_CAP_HALT_EXIT: - if (cap_halt_exit) { - retval = 0; - pptr = &vmx->cap[vcpu].proc_ctls; - baseval = *pptr; - flag = PROCBASED_HLT_EXITING; - reg = VMCS_PRI_PROC_BASED_CTLS; - } - break; - case VM_CAP_MTRAP_EXIT: - if (cap_monitor_trap) { - retval = 0; - pptr = &vmx->cap[vcpu].proc_ctls; - baseval = *pptr; - flag = PROCBASED_MTF; - reg = VMCS_PRI_PROC_BASED_CTLS; - } - break; - case VM_CAP_PAUSE_EXIT: - if (cap_pause_exit) { - retval = 0; - pptr = &vmx->cap[vcpu].proc_ctls; - baseval = *pptr; - flag = PROCBASED_PAUSE_EXITING; - reg = VMCS_PRI_PROC_BASED_CTLS; - } - break; - case VM_CAP_UNRESTRICTED_GUEST: - if (cap_unrestricted_guest) { - retval = 0; - pptr = &vmx->cap[vcpu].proc_ctls2; - baseval = *pptr; - flag = PROCBASED2_UNRESTRICTED_GUEST; - reg = VMCS_SEC_PROC_BASED_CTLS; - } - break; - case VM_CAP_ENABLE_INVPCID: - if (cap_invpcid) { - retval = 0; - pptr = &vmx->cap[vcpu].proc_ctls2; - baseval = *pptr; - flag = PROCBASED2_ENABLE_INVPCID; - reg = VMCS_SEC_PROC_BASED_CTLS; - } - break; - default: - break; - } - - if (retval == 0) { - if (val) { - baseval |= flag; - } else { - baseval &= ~flag; - } - VMPTRLD(vmcs); - error = vmwrite(reg, baseval); - VMCLEAR(vmcs); - - if (error) { - retval = error; - } else { - /* - * Update optional stored flags, and record - * setting - */ - if (pptr != NULL) { - *pptr = baseval; - } - - if (val) { - vmx->cap[vcpu].set |= (1 << type); - } else { - vmx->cap[vcpu].set &= ~(1 << type); - } - } - } - - return (retval); -} - -struct vlapic_vtx { - struct vlapic vlapic; - struct pir_desc *pir_desc; - struct vmx *vmx; -}; - -#define VMX_CTR_PIR(vm, vcpuid, pir_desc, notify, vector, level, msg) \ -do { \ - VCPU_CTR2(vm, vcpuid, msg " assert %s-triggered vector %d", \ - level ? "level" : "edge", vector); \ - VCPU_CTR1(vm, vcpuid, msg " pir0 0x%016lx", pir_desc->pir[0]); \ - VCPU_CTR1(vm, vcpuid, msg " pir1 0x%016lx", pir_desc->pir[1]); \ - VCPU_CTR1(vm, vcpuid, msg " pir2 0x%016lx", pir_desc->pir[2]); \ - VCPU_CTR1(vm, vcpuid, msg " pir3 0x%016lx", pir_desc->pir[3]); \ - VCPU_CTR1(vm, vcpuid, msg " notify: %s", notify ? "yes" : "no");\ -} while (0) - -/* - * vlapic->ops handlers that utilize the APICv hardware assist described in - * Chapter 29 of the Intel SDM. - */ -static int -vmx_set_intr_ready(struct vlapic *vlapic, int vector, bool level) -{ - struct vlapic_vtx *vlapic_vtx; - struct pir_desc *pir_desc; - uint64_t mask; - int idx, notify; - - vlapic_vtx = (struct vlapic_vtx *)vlapic; - pir_desc = vlapic_vtx->pir_desc; - - /* - * Keep track of interrupt requests in the PIR descriptor. This is - * because the virtual APIC page pointed to by the VMCS cannot be - * modified if the vcpu is running. - */ - idx = vector / 64; - mask = 1UL << (vector % 64); - atomic_set_long(&pir_desc->pir[idx], mask); - notify = atomic_cmpset_long(&pir_desc->pending, 0, 1); - - VMX_CTR_PIR(vlapic->vm, vlapic->vcpuid, pir_desc, notify, vector, - level, "vmx_set_intr_ready"); - return (notify); -} - -static int -vmx_pending_intr(struct vlapic *vlapic, int *vecptr) -{ - struct vlapic_vtx *vlapic_vtx; - struct pir_desc *pir_desc; - struct LAPIC *lapic; - uint64_t pending, pirval; - uint32_t ppr, vpr; - int i; - - /* - * This function is only expected to be called from the 'HLT' exit - * handler which does not care about the vector that is pending. - */ - KASSERT(vecptr == NULL, ("vmx_pending_intr: vecptr must be NULL")); - - vlapic_vtx = (struct vlapic_vtx *)vlapic; - pir_desc = vlapic_vtx->pir_desc; - - pending = atomic_load_acq_long(&pir_desc->pending); - if (!pending) - return (0); /* common case */ - - /* - * If there is an interrupt pending then it will be recognized only - * if its priority is greater than the processor priority. - * - * Special case: if the processor priority is zero then any pending - * interrupt will be recognized. - */ - lapic = vlapic->apic_page; - ppr = lapic->ppr & 0xf0; - if (ppr == 0) - return (1); - - VCPU_CTR1(vlapic->vm, vlapic->vcpuid, "HLT with non-zero PPR %d", - lapic->ppr); - - for (i = 3; i >= 0; i--) { - pirval = pir_desc->pir[i]; - if (pirval != 0) { - vpr = (i * 64 + flsl(pirval) - 1) & 0xf0; - return (vpr > ppr); - } - } - return (0); -} - -static void -vmx_intr_accepted(struct vlapic *vlapic, int vector) -{ - - panic("vmx_intr_accepted: not expected to be called"); -} - -static void -vmx_set_tmr(struct vlapic *vlapic, int vector, bool level) -{ - struct vlapic_vtx *vlapic_vtx; - struct vmx *vmx; - struct vmcs *vmcs; - uint64_t mask, val; - - KASSERT(vector >= 0 && vector <= 255, ("invalid vector %d", vector)); - KASSERT(!vcpu_is_running(vlapic->vm, vlapic->vcpuid, NULL), - ("vmx_set_tmr: vcpu cannot be running")); - - vlapic_vtx = (struct vlapic_vtx *)vlapic; - vmx = vlapic_vtx->vmx; - vmcs = &vmx->vmcs[vlapic->vcpuid]; - mask = 1UL << (vector % 64); - - VMPTRLD(vmcs); - val = vmcs_read(VMCS_EOI_EXIT(vector)); - if (level) - val |= mask; - else - val &= ~mask; - vmcs_write(VMCS_EOI_EXIT(vector), val); - VMCLEAR(vmcs); -} - -static void -vmx_enable_x2apic_mode(struct vlapic *vlapic) -{ - struct vmx *vmx; - struct vmcs *vmcs; - uint32_t proc_ctls2; - int vcpuid, error; - - vcpuid = vlapic->vcpuid; - vmx = ((struct vlapic_vtx *)vlapic)->vmx; - vmcs = &vmx->vmcs[vcpuid]; - - proc_ctls2 = vmx->cap[vcpuid].proc_ctls2; - KASSERT((proc_ctls2 & PROCBASED2_VIRTUALIZE_APIC_ACCESSES) != 0, - ("%s: invalid proc_ctls2 %#x", __func__, proc_ctls2)); - - proc_ctls2 &= ~PROCBASED2_VIRTUALIZE_APIC_ACCESSES; - proc_ctls2 |= PROCBASED2_VIRTUALIZE_X2APIC_MODE; - vmx->cap[vcpuid].proc_ctls2 = proc_ctls2; - - VMPTRLD(vmcs); - vmcs_write(VMCS_SEC_PROC_BASED_CTLS, proc_ctls2); - VMCLEAR(vmcs); - - if (vlapic->vcpuid == 0) { - /* - * The nested page table mappings are shared by all vcpus - * so unmap the APIC access page just once. - */ - error = vm_unmap_mmio(vmx->vm, DEFAULT_APIC_BASE, PAGE_SIZE); - KASSERT(error == 0, ("%s: vm_unmap_mmio error %d", - __func__, error)); - - /* - * The MSR bitmap is shared by all vcpus so modify it only - * once in the context of vcpu 0. - */ - error = vmx_allow_x2apic_msrs(vmx); - KASSERT(error == 0, ("%s: vmx_allow_x2apic_msrs error %d", - __func__, error)); - } -} - -static void -vmx_post_intr(struct vlapic *vlapic, int hostcpu) -{ - - ipi_cpu(hostcpu, pirvec); -} - -/* - * Transfer the pending interrupts in the PIR descriptor to the IRR - * in the virtual APIC page. - */ -static void -vmx_inject_pir(struct vlapic *vlapic) -{ - struct vlapic_vtx *vlapic_vtx; - struct pir_desc *pir_desc; - struct LAPIC *lapic; - uint64_t val, pirval; - int rvi, pirbase = -1; - uint16_t intr_status_old, intr_status_new; - - vlapic_vtx = (struct vlapic_vtx *)vlapic; - pir_desc = vlapic_vtx->pir_desc; - if (atomic_cmpset_long(&pir_desc->pending, 1, 0) == 0) { - VCPU_CTR0(vlapic->vm, vlapic->vcpuid, "vmx_inject_pir: " - "no posted interrupt pending"); - return; - } - - pirval = 0; - pirbase = -1; - lapic = vlapic->apic_page; - - val = atomic_readandclear_long(&pir_desc->pir[0]); - if (val != 0) { - lapic->irr0 |= val; - lapic->irr1 |= val >> 32; - pirbase = 0; - pirval = val; - } - - val = atomic_readandclear_long(&pir_desc->pir[1]); - if (val != 0) { - lapic->irr2 |= val; - lapic->irr3 |= val >> 32; - pirbase = 64; - pirval = val; - } - - val = atomic_readandclear_long(&pir_desc->pir[2]); - if (val != 0) { - lapic->irr4 |= val; - lapic->irr5 |= val >> 32; - pirbase = 128; - pirval = val; - } - - val = atomic_readandclear_long(&pir_desc->pir[3]); - if (val != 0) { - lapic->irr6 |= val; - lapic->irr7 |= val >> 32; - pirbase = 192; - pirval = val; - } - - VLAPIC_CTR_IRR(vlapic, "vmx_inject_pir"); - - /* - * Update RVI so the processor can evaluate pending virtual - * interrupts on VM-entry. - * - * It is possible for pirval to be 0 here, even though the - * pending bit has been set. The scenario is: - * CPU-Y is sending a posted interrupt to CPU-X, which - * is running a guest and processing posted interrupts in h/w. - * CPU-X will eventually exit and the state seen in s/w is - * the pending bit set, but no PIR bits set. - * - * CPU-X CPU-Y - * (vm running) (host running) - * rx posted interrupt - * CLEAR pending bit - * SET PIR bit - * READ/CLEAR PIR bits - * SET pending bit - * (vm exit) - * pending bit set, PIR 0 - */ - if (pirval != 0) { - rvi = pirbase + flsl(pirval) - 1; - intr_status_old = vmcs_read(VMCS_GUEST_INTR_STATUS); - intr_status_new = (intr_status_old & 0xFF00) | rvi; - if (intr_status_new > intr_status_old) { - vmcs_write(VMCS_GUEST_INTR_STATUS, intr_status_new); - VCPU_CTR2(vlapic->vm, vlapic->vcpuid, "vmx_inject_pir: " - "guest_intr_status changed from 0x%04x to 0x%04x", - intr_status_old, intr_status_new); - } - } -} - -static struct vlapic * -vmx_vlapic_init(void *arg, int vcpuid) -{ - struct vmx *vmx; - struct vlapic *vlapic; - struct vlapic_vtx *vlapic_vtx; - - vmx = arg; - - vlapic = malloc(sizeof(struct vlapic_vtx), M_VLAPIC, M_WAITOK | M_ZERO); - vlapic->vm = vmx->vm; - vlapic->vcpuid = vcpuid; - vlapic->apic_page = (struct LAPIC *)&vmx->apic_page[vcpuid]; - - vlapic_vtx = (struct vlapic_vtx *)vlapic; - vlapic_vtx->pir_desc = &vmx->pir_desc[vcpuid]; - vlapic_vtx->vmx = vmx; - - if (virtual_interrupt_delivery) { - vlapic->ops.set_intr_ready = vmx_set_intr_ready; - vlapic->ops.pending_intr = vmx_pending_intr; - vlapic->ops.intr_accepted = vmx_intr_accepted; - vlapic->ops.set_tmr = vmx_set_tmr; - vlapic->ops.enable_x2apic_mode = vmx_enable_x2apic_mode; - } - - if (posted_interrupts) - vlapic->ops.post_intr = vmx_post_intr; - - vlapic_init(vlapic); - - return (vlapic); -} - -static void -vmx_vlapic_cleanup(void *arg, struct vlapic *vlapic) -{ - - vlapic_cleanup(vlapic); - free(vlapic, M_VLAPIC); -} - -struct vmm_ops vmm_ops_intel = { - vmx_init, - vmx_cleanup, - vmx_restore, - vmx_vminit, - vmx_run, - vmx_vmcleanup, - vmx_getreg, - vmx_setreg, - vmx_getdesc, - vmx_setdesc, - vmx_getcap, - vmx_setcap, - ept_vmspace_alloc, - ept_vmspace_free, - vmx_vlapic_init, - vmx_vlapic_cleanup, -}; diff --git a/vmm/intel/vmx_controls.h b/vmm/intel/vmx_controls.h deleted file mode 100644 index 2b117ae..0000000 --- a/vmm/intel/vmx_controls.h +++ /dev/null @@ -1,96 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#ifndef _VMX_CONTROLS_H_ -#define _VMX_CONTROLS_H_ - -/* Pin-Based VM-Execution Controls */ -#define PINBASED_EXTINT_EXITING (1 << 0) -#define PINBASED_NMI_EXITING (1 << 3) -#define PINBASED_VIRTUAL_NMI (1 << 5) -#define PINBASED_PREMPTION_TIMER (1 << 6) -#define PINBASED_POSTED_INTERRUPT (1 << 7) - -/* Primary Processor-Based VM-Execution Controls */ -#define PROCBASED_INT_WINDOW_EXITING (1 << 2) -#define PROCBASED_TSC_OFFSET (1 << 3) -#define PROCBASED_HLT_EXITING (1 << 7) -#define PROCBASED_INVLPG_EXITING (1 << 9) -#define PROCBASED_MWAIT_EXITING (1 << 10) -#define PROCBASED_RDPMC_EXITING (1 << 11) -#define PROCBASED_RDTSC_EXITING (1 << 12) -#define PROCBASED_CR3_LOAD_EXITING (1 << 15) -#define PROCBASED_CR3_STORE_EXITING (1 << 16) -#define PROCBASED_CR8_LOAD_EXITING (1 << 19) -#define PROCBASED_CR8_STORE_EXITING (1 << 20) -#define PROCBASED_USE_TPR_SHADOW (1 << 21) -#define PROCBASED_NMI_WINDOW_EXITING (1 << 22) -#define PROCBASED_MOV_DR_EXITING (1 << 23) -#define PROCBASED_IO_EXITING (1 << 24) -#define PROCBASED_IO_BITMAPS (1 << 25) -#define PROCBASED_MTF (1 << 27) -#define PROCBASED_MSR_BITMAPS (1 << 28) -#define PROCBASED_MONITOR_EXITING (1 << 29) -#define PROCBASED_PAUSE_EXITING (1 << 30) -#define PROCBASED_SECONDARY_CONTROLS (1U << 31) - -/* Secondary Processor-Based VM-Execution Controls */ -#define PROCBASED2_VIRTUALIZE_APIC_ACCESSES (1 << 0) -#define PROCBASED2_ENABLE_EPT (1 << 1) -#define PROCBASED2_DESC_TABLE_EXITING (1 << 2) -#define PROCBASED2_ENABLE_RDTSCP (1 << 3) -#define PROCBASED2_VIRTUALIZE_X2APIC_MODE (1 << 4) -#define PROCBASED2_ENABLE_VPID (1 << 5) -#define PROCBASED2_WBINVD_EXITING (1 << 6) -#define PROCBASED2_UNRESTRICTED_GUEST (1 << 7) -#define PROCBASED2_APIC_REGISTER_VIRTUALIZATION (1 << 8) -#define PROCBASED2_VIRTUAL_INTERRUPT_DELIVERY (1 << 9) -#define PROCBASED2_PAUSE_LOOP_EXITING (1 << 10) -#define PROCBASED2_ENABLE_INVPCID (1 << 12) - -/* VM Exit Controls */ -#define VM_EXIT_SAVE_DEBUG_CONTROLS (1 << 2) -#define VM_EXIT_HOST_LMA (1 << 9) -#define VM_EXIT_LOAD_PERF_GLOBAL_CTRL (1 << 12) -#define VM_EXIT_ACKNOWLEDGE_INTERRUPT (1 << 15) -#define VM_EXIT_SAVE_PAT (1 << 18) -#define VM_EXIT_LOAD_PAT (1 << 19) -#define VM_EXIT_SAVE_EFER (1 << 20) -#define VM_EXIT_LOAD_EFER (1 << 21) -#define VM_EXIT_SAVE_PREEMPTION_TIMER (1 << 22) - -/* VM Entry Controls */ -#define VM_ENTRY_LOAD_DEBUG_CONTROLS (1 << 2) -#define VM_ENTRY_GUEST_LMA (1 << 9) -#define VM_ENTRY_INTO_SMM (1 << 10) -#define VM_ENTRY_DEACTIVATE_DUAL_MONITOR (1 << 11) -#define VM_ENTRY_LOAD_PERF_GLOBAL_CTRL (1 << 13) -#define VM_ENTRY_LOAD_PAT (1 << 14) -#define VM_ENTRY_LOAD_EFER (1 << 15) - -#endif diff --git a/vmm/intel/vmx_cpufunc.h b/vmm/intel/vmx_cpufunc.h deleted file mode 100644 index 2e66443..0000000 --- a/vmm/intel/vmx_cpufunc.h +++ /dev/null @@ -1,218 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#ifndef _VMX_CPUFUNC_H_ -#define _VMX_CPUFUNC_H_ - -struct vmcs; - -/* - * Section 5.2 "Conventions" from Intel Architecture Manual 2B. - * - * error - * VMsucceed 0 - * VMFailInvalid 1 - * VMFailValid 2 see also VMCS VM-Instruction Error Field - */ -#define VM_SUCCESS 0 -#define VM_FAIL_INVALID 1 -#define VM_FAIL_VALID 2 -#define VMX_SET_ERROR_CODE \ - " jnc 1f;" \ - " mov $1, %[error];" /* CF: error = 1 */ \ - " jmp 3f;" \ - "1: jnz 2f;" \ - " mov $2, %[error];" /* ZF: error = 2 */ \ - " jmp 3f;" \ - "2: mov $0, %[error];" \ - "3:" - -/* returns 0 on success and non-zero on failure */ -static __inline int -vmxon(char *region) -{ - int error; - uint64_t addr; - - addr = vtophys(region); - __asm __volatile("vmxon %[addr];" - VMX_SET_ERROR_CODE - : [error] "=r" (error) - : [addr] "m" (*(uint64_t *)&addr) - : "memory"); - - return (error); -} - -/* returns 0 on success and non-zero on failure */ -static __inline int -vmclear(struct vmcs *vmcs) -{ - int error; - uint64_t addr; - - addr = vtophys(vmcs); - __asm __volatile("vmclear %[addr];" - VMX_SET_ERROR_CODE - : [error] "=r" (error) - : [addr] "m" (*(uint64_t *)&addr) - : "memory"); - return (error); -} - -static __inline void -vmxoff(void) -{ - - __asm __volatile("vmxoff"); -} - -static __inline void -vmptrst(uint64_t *addr) -{ - - __asm __volatile("vmptrst %[addr]" :: [addr]"m" (*addr) : "memory"); -} - -static __inline int -vmptrld(struct vmcs *vmcs) -{ - int error; - uint64_t addr; - - addr = vtophys(vmcs); - __asm __volatile("vmptrld %[addr];" - VMX_SET_ERROR_CODE - : [error] "=r" (error) - : [addr] "m" (*(uint64_t *)&addr) - : "memory"); - return (error); -} - -static __inline int -vmwrite(uint64_t reg, uint64_t val) -{ - int error; - - __asm __volatile("vmwrite %[val], %[reg];" - VMX_SET_ERROR_CODE - : [error] "=r" (error) - : [val] "r" (val), [reg] "r" (reg) - : "memory"); - - return (error); -} - -static __inline int -vmread(uint64_t r, uint64_t *addr) -{ - int error; - - __asm __volatile("vmread %[r], %[addr];" - VMX_SET_ERROR_CODE - : [error] "=r" (error) - : [r] "r" (r), [addr] "m" (*addr) - : "memory"); - - return (error); -} - -static void __inline -VMCLEAR(struct vmcs *vmcs) -{ - int err; - - err = vmclear(vmcs); - if (err != 0) - panic("%s: vmclear(%p) error %d", __func__, vmcs, err); - - critical_exit(); -} - -static void __inline -VMPTRLD(struct vmcs *vmcs) -{ - int err; - - critical_enter(); - - err = vmptrld(vmcs); - if (err != 0) - panic("%s: vmptrld(%p) error %d", __func__, vmcs, err); -} - -#define INVVPID_TYPE_ADDRESS 0UL -#define INVVPID_TYPE_SINGLE_CONTEXT 1UL -#define INVVPID_TYPE_ALL_CONTEXTS 2UL - -struct invvpid_desc { - uint16_t vpid; - uint16_t _res1; - uint32_t _res2; - uint64_t linear_addr; -}; -CTASSERT(sizeof(struct invvpid_desc) == 16); - -static void __inline -invvpid(uint64_t type, struct invvpid_desc desc) -{ - int error; - - __asm __volatile("invvpid %[desc], %[type];" - VMX_SET_ERROR_CODE - : [error] "=r" (error) - : [desc] "m" (desc), [type] "r" (type) - : "memory"); - - if (error) - panic("invvpid error %d", error); -} - -#define INVEPT_TYPE_SINGLE_CONTEXT 1UL -#define INVEPT_TYPE_ALL_CONTEXTS 2UL -struct invept_desc { - uint64_t eptp; - uint64_t _res; -}; -CTASSERT(sizeof(struct invept_desc) == 16); - -static void __inline -invept(uint64_t type, struct invept_desc desc) -{ - int error; - - __asm __volatile("invept %[desc], %[type];" - VMX_SET_ERROR_CODE - : [error] "=r" (error) - : [desc] "m" (desc), [type] "r" (type) - : "memory"); - - if (error) - panic("invept error %d", error); -} -#endif diff --git a/vmm/intel/vmx_genassym.c b/vmm/intel/vmx_genassym.c deleted file mode 100644 index e1b98d6..0000000 --- a/vmm/intel/vmx_genassym.c +++ /dev/null @@ -1,88 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include - -#include -#include - -#include -#include "vmx_cpufunc.h" -#include "vmx.h" - -ASSYM(VMXCTX_GUEST_RDI, offsetof(struct vmxctx, guest_rdi)); -ASSYM(VMXCTX_GUEST_RSI, offsetof(struct vmxctx, guest_rsi)); -ASSYM(VMXCTX_GUEST_RDX, offsetof(struct vmxctx, guest_rdx)); -ASSYM(VMXCTX_GUEST_RCX, offsetof(struct vmxctx, guest_rcx)); -ASSYM(VMXCTX_GUEST_R8, offsetof(struct vmxctx, guest_r8)); -ASSYM(VMXCTX_GUEST_R9, offsetof(struct vmxctx, guest_r9)); -ASSYM(VMXCTX_GUEST_RAX, offsetof(struct vmxctx, guest_rax)); -ASSYM(VMXCTX_GUEST_RBX, offsetof(struct vmxctx, guest_rbx)); -ASSYM(VMXCTX_GUEST_RBP, offsetof(struct vmxctx, guest_rbp)); -ASSYM(VMXCTX_GUEST_R10, offsetof(struct vmxctx, guest_r10)); -ASSYM(VMXCTX_GUEST_R11, offsetof(struct vmxctx, guest_r11)); -ASSYM(VMXCTX_GUEST_R12, offsetof(struct vmxctx, guest_r12)); -ASSYM(VMXCTX_GUEST_R13, offsetof(struct vmxctx, guest_r13)); -ASSYM(VMXCTX_GUEST_R14, offsetof(struct vmxctx, guest_r14)); -ASSYM(VMXCTX_GUEST_R15, offsetof(struct vmxctx, guest_r15)); -ASSYM(VMXCTX_GUEST_CR2, offsetof(struct vmxctx, guest_cr2)); - -ASSYM(VMXCTX_HOST_R15, offsetof(struct vmxctx, host_r15)); -ASSYM(VMXCTX_HOST_R14, offsetof(struct vmxctx, host_r14)); -ASSYM(VMXCTX_HOST_R13, offsetof(struct vmxctx, host_r13)); -ASSYM(VMXCTX_HOST_R12, offsetof(struct vmxctx, host_r12)); -ASSYM(VMXCTX_HOST_RBP, offsetof(struct vmxctx, host_rbp)); -ASSYM(VMXCTX_HOST_RSP, offsetof(struct vmxctx, host_rsp)); -ASSYM(VMXCTX_HOST_RBX, offsetof(struct vmxctx, host_rbx)); - -ASSYM(VMXCTX_INST_FAIL_STATUS, offsetof(struct vmxctx, inst_fail_status)); -ASSYM(VMXCTX_PMAP, offsetof(struct vmxctx, pmap)); - -ASSYM(VMX_EPTGEN, offsetof(struct vmx, eptgen)); -ASSYM(VMX_EPTP, offsetof(struct vmx, eptp)); - -ASSYM(VM_FAIL_INVALID, VM_FAIL_INVALID); -ASSYM(VM_FAIL_VALID, VM_FAIL_VALID); -ASSYM(VMX_GUEST_VMEXIT, VMX_GUEST_VMEXIT); -ASSYM(VMX_VMRESUME_ERROR, VMX_VMRESUME_ERROR); -ASSYM(VMX_VMLAUNCH_ERROR, VMX_VMLAUNCH_ERROR); -ASSYM(VMX_INVEPT_ERROR, VMX_INVEPT_ERROR); - -ASSYM(PC_CPUID, offsetof(struct pcpu, pc_cpuid)); - -ASSYM(PM_ACTIVE, offsetof(struct pmap, pm_active)); -ASSYM(PM_EPTGEN, offsetof(struct pmap, pm_eptgen)); - -ASSYM(KERNEL_SS, GSEL(GDATA_SEL, SEL_KPL)); -ASSYM(KERNEL_CS, GSEL(GCODE_SEL, SEL_KPL)); diff --git a/vmm/intel/vmx_msr.c b/vmm/intel/vmx_msr.c deleted file mode 100644 index 3091f68..0000000 --- a/vmm/intel/vmx_msr.c +++ /dev/null @@ -1,483 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include - -#include -#include -#include -#include -#include - -#include "vmx.h" -#include "vmx_msr.h" - -static boolean_t -vmx_ctl_allows_one_setting(uint64_t msr_val, int bitpos) -{ - - if (msr_val & (1UL << (bitpos + 32))) - return (TRUE); - else - return (FALSE); -} - -static boolean_t -vmx_ctl_allows_zero_setting(uint64_t msr_val, int bitpos) -{ - - if ((msr_val & (1UL << bitpos)) == 0) - return (TRUE); - else - return (FALSE); -} - -uint32_t -vmx_revision(void) -{ - - return (rdmsr(MSR_VMX_BASIC) & 0xffffffff); -} - -/* - * Generate a bitmask to be used for the VMCS execution control fields. - * - * The caller specifies what bits should be set to one in 'ones_mask' - * and what bits should be set to zero in 'zeros_mask'. The don't-care - * bits are set to the default value. The default values are obtained - * based on "Algorithm 3" in Section 27.5.1 "Algorithms for Determining - * VMX Capabilities". - * - * Returns zero on success and non-zero on error. - */ -int -vmx_set_ctlreg(int ctl_reg, int true_ctl_reg, uint32_t ones_mask, - uint32_t zeros_mask, uint32_t *retval) -{ - int i; - uint64_t val, trueval; - boolean_t true_ctls_avail, one_allowed, zero_allowed; - - /* We cannot ask the same bit to be set to both '1' and '0' */ - if ((ones_mask ^ zeros_mask) != (ones_mask | zeros_mask)) - return (EINVAL); - - if (rdmsr(MSR_VMX_BASIC) & (1UL << 55)) - true_ctls_avail = TRUE; - else - true_ctls_avail = FALSE; - - val = rdmsr(ctl_reg); - if (true_ctls_avail) - trueval = rdmsr(true_ctl_reg); /* step c */ - else - trueval = val; /* step a */ - - for (i = 0; i < 32; i++) { - one_allowed = vmx_ctl_allows_one_setting(trueval, i); - zero_allowed = vmx_ctl_allows_zero_setting(trueval, i); - - KASSERT(one_allowed || zero_allowed, - ("invalid zero/one setting for bit %d of ctl 0x%0x, " - "truectl 0x%0x\n", i, ctl_reg, true_ctl_reg)); - - if (zero_allowed && !one_allowed) { /* b(i),c(i) */ - if (ones_mask & (1 << i)) - return (EINVAL); - *retval &= ~(1 << i); - } else if (one_allowed && !zero_allowed) { /* b(i),c(i) */ - if (zeros_mask & (1 << i)) - return (EINVAL); - *retval |= 1 << i; - } else { - if (zeros_mask & (1 << i)) /* b(ii),c(ii) */ - *retval &= ~(1 << i); - else if (ones_mask & (1 << i)) /* b(ii), c(ii) */ - *retval |= 1 << i; - else if (!true_ctls_avail) - *retval &= ~(1 << i); /* b(iii) */ - else if (vmx_ctl_allows_zero_setting(val, i))/* c(iii)*/ - *retval &= ~(1 << i); - else if (vmx_ctl_allows_one_setting(val, i)) /* c(iv) */ - *retval |= 1 << i; - else { - panic("vmx_set_ctlreg: unable to determine " - "correct value of ctl bit %d for msr " - "0x%0x and true msr 0x%0x", i, ctl_reg, - true_ctl_reg); - } - } - } - - return (0); -} - -void -msr_bitmap_initialize(char *bitmap) -{ - - memset(bitmap, 0xff, PAGE_SIZE); -} - -int -msr_bitmap_change_access(char *bitmap, u_int msr, int access) -{ - int byte, bit; - - if (msr <= 0x00001FFF) - byte = msr / 8; - else if (msr >= 0xC0000000 && msr <= 0xC0001FFF) - byte = 1024 + (msr - 0xC0000000) / 8; - else - return (EINVAL); - - bit = msr & 0x7; - - if (access & MSR_BITMAP_ACCESS_READ) - bitmap[byte] &= ~(1 << bit); - else - bitmap[byte] |= 1 << bit; - - byte += 2048; - if (access & MSR_BITMAP_ACCESS_WRITE) - bitmap[byte] &= ~(1 << bit); - else - bitmap[byte] |= 1 << bit; - - return (0); -} - -static uint64_t misc_enable; -static uint64_t platform_info; -static uint64_t turbo_ratio_limit; -static uint64_t host_msrs[GUEST_MSR_NUM]; - -static bool -nehalem_cpu(void) -{ - u_int family, model; - - /* - * The family:model numbers belonging to the Nehalem microarchitecture - * are documented in Section 35.5, Intel SDM dated Feb 2014. - */ - family = CPUID_TO_FAMILY(cpu_id); - model = CPUID_TO_MODEL(cpu_id); - if (family == 0x6) { - switch (model) { - case 0x1A: - case 0x1E: - case 0x1F: - case 0x2E: - return (true); - default: - break; - } - } - return (false); -} - -static bool -westmere_cpu(void) -{ - u_int family, model; - - /* - * The family:model numbers belonging to the Westmere microarchitecture - * are documented in Section 35.6, Intel SDM dated Feb 2014. - */ - family = CPUID_TO_FAMILY(cpu_id); - model = CPUID_TO_MODEL(cpu_id); - if (family == 0x6) { - switch (model) { - case 0x25: - case 0x2C: - return (true); - default: - break; - } - } - return (false); -} - -static bool -pat_valid(uint64_t val) -{ - int i, pa; - - /* - * From Intel SDM: Table "Memory Types That Can Be Encoded With PAT" - * - * Extract PA0 through PA7 and validate that each one encodes a - * valid memory type. - */ - for (i = 0; i < 8; i++) { - pa = (val >> (i * 8)) & 0xff; - if (pa == 2 || pa == 3 || pa >= 8) - return (false); - } - return (true); -} - -void -vmx_msr_init(void) -{ - uint64_t bus_freq, ratio; - int i; - - /* - * It is safe to cache the values of the following MSRs because - * they don't change based on curcpu, curproc or curthread. - */ - host_msrs[IDX_MSR_LSTAR] = rdmsr(MSR_LSTAR); - host_msrs[IDX_MSR_CSTAR] = rdmsr(MSR_CSTAR); - host_msrs[IDX_MSR_STAR] = rdmsr(MSR_STAR); - host_msrs[IDX_MSR_SF_MASK] = rdmsr(MSR_SF_MASK); - - /* - * Initialize emulated MSRs - */ - misc_enable = rdmsr(MSR_IA32_MISC_ENABLE); - /* - * Set mandatory bits - * 11: branch trace disabled - * 12: PEBS unavailable - * Clear unsupported features - * 16: SpeedStep enable - * 18: enable MONITOR FSM - */ - misc_enable |= (1 << 12) | (1 << 11); - misc_enable &= ~((1 << 18) | (1 << 16)); - - if (nehalem_cpu() || westmere_cpu()) - bus_freq = 133330000; /* 133Mhz */ - else - bus_freq = 100000000; /* 100Mhz */ - - /* - * XXXtime - * The ratio should really be based on the virtual TSC frequency as - * opposed to the host TSC. - */ - ratio = (tsc_freq / bus_freq) & 0xff; - - /* - * The register definition is based on the micro-architecture - * but the following bits are always the same: - * [15:8] Maximum Non-Turbo Ratio - * [28] Programmable Ratio Limit for Turbo Mode - * [29] Programmable TDC-TDP Limit for Turbo Mode - * [47:40] Maximum Efficiency Ratio - * - * The other bits can be safely set to 0 on all - * micro-architectures up to Haswell. - */ - platform_info = (ratio << 8) | (ratio << 40); - - /* - * The number of valid bits in the MSR_TURBO_RATIO_LIMITx register is - * dependent on the maximum cores per package supported by the micro- - * architecture. For e.g., Westmere supports 6 cores per package and - * uses the low 48 bits. Sandybridge support 8 cores per package and - * uses up all 64 bits. - * - * However, the unused bits are reserved so we pretend that all bits - * in this MSR are valid. - */ - for (i = 0; i < 8; i++) - turbo_ratio_limit = (turbo_ratio_limit << 8) | ratio; -} - -void -vmx_msr_guest_init(struct vmx *vmx, int vcpuid) -{ - uint64_t *guest_msrs; - - guest_msrs = vmx->guest_msrs[vcpuid]; - - /* - * The permissions bitmap is shared between all vcpus so initialize it - * once when initializing the vBSP. - */ - if (vcpuid == 0) { - guest_msr_rw(vmx, MSR_LSTAR); - guest_msr_rw(vmx, MSR_CSTAR); - guest_msr_rw(vmx, MSR_STAR); - guest_msr_rw(vmx, MSR_SF_MASK); - guest_msr_rw(vmx, MSR_KGSBASE); - } - - /* - * Initialize guest IA32_PAT MSR with default value after reset. - */ - guest_msrs[IDX_MSR_PAT] = PAT_VALUE(0, PAT_WRITE_BACK) | - PAT_VALUE(1, PAT_WRITE_THROUGH) | - PAT_VALUE(2, PAT_UNCACHED) | - PAT_VALUE(3, PAT_UNCACHEABLE) | - PAT_VALUE(4, PAT_WRITE_BACK) | - PAT_VALUE(5, PAT_WRITE_THROUGH) | - PAT_VALUE(6, PAT_UNCACHED) | - PAT_VALUE(7, PAT_UNCACHEABLE); - - return; -} - -void -vmx_msr_guest_enter(struct vmx *vmx, int vcpuid) -{ - uint64_t *guest_msrs = vmx->guest_msrs[vcpuid]; - - /* Save host MSRs (if any) and restore guest MSRs */ - wrmsr(MSR_LSTAR, guest_msrs[IDX_MSR_LSTAR]); - wrmsr(MSR_CSTAR, guest_msrs[IDX_MSR_CSTAR]); - wrmsr(MSR_STAR, guest_msrs[IDX_MSR_STAR]); - wrmsr(MSR_SF_MASK, guest_msrs[IDX_MSR_SF_MASK]); - wrmsr(MSR_KGSBASE, guest_msrs[IDX_MSR_KGSBASE]); -} - -void -vmx_msr_guest_exit(struct vmx *vmx, int vcpuid) -{ - uint64_t *guest_msrs = vmx->guest_msrs[vcpuid]; - - /* Save guest MSRs */ - guest_msrs[IDX_MSR_LSTAR] = rdmsr(MSR_LSTAR); - guest_msrs[IDX_MSR_CSTAR] = rdmsr(MSR_CSTAR); - guest_msrs[IDX_MSR_STAR] = rdmsr(MSR_STAR); - guest_msrs[IDX_MSR_SF_MASK] = rdmsr(MSR_SF_MASK); - guest_msrs[IDX_MSR_KGSBASE] = rdmsr(MSR_KGSBASE); - - /* Restore host MSRs */ - wrmsr(MSR_LSTAR, host_msrs[IDX_MSR_LSTAR]); - wrmsr(MSR_CSTAR, host_msrs[IDX_MSR_CSTAR]); - wrmsr(MSR_STAR, host_msrs[IDX_MSR_STAR]); - wrmsr(MSR_SF_MASK, host_msrs[IDX_MSR_SF_MASK]); - - /* MSR_KGSBASE will be restored on the way back to userspace */ -} - -int -vmx_rdmsr(struct vmx *vmx, int vcpuid, u_int num, uint64_t *val, bool *retu) -{ - const uint64_t *guest_msrs; - int error; - - guest_msrs = vmx->guest_msrs[vcpuid]; - error = 0; - - switch (num) { - case MSR_MCG_CAP: - case MSR_MCG_STATUS: - *val = 0; - break; - case MSR_MTRRcap: - case MSR_MTRRdefType: - case MSR_MTRR4kBase ... MSR_MTRR4kBase + 8: - case MSR_MTRR16kBase ... MSR_MTRR16kBase + 1: - case MSR_MTRR64kBase: - *val = 0; - break; - case MSR_IA32_MISC_ENABLE: - *val = misc_enable; - break; - case MSR_PLATFORM_INFO: - *val = platform_info; - break; - case MSR_TURBO_RATIO_LIMIT: - case MSR_TURBO_RATIO_LIMIT1: - *val = turbo_ratio_limit; - break; - case MSR_PAT: - *val = guest_msrs[IDX_MSR_PAT]; - break; - default: - error = EINVAL; - break; - } - return (error); -} - -int -vmx_wrmsr(struct vmx *vmx, int vcpuid, u_int num, uint64_t val, bool *retu) -{ - uint64_t *guest_msrs; - uint64_t changed; - int error; - - guest_msrs = vmx->guest_msrs[vcpuid]; - error = 0; - - switch (num) { - case MSR_MCG_CAP: - case MSR_MCG_STATUS: - break; /* ignore writes */ - case MSR_MTRRcap: - vm_inject_gp(vmx->vm, vcpuid); - break; - case MSR_MTRRdefType: - case MSR_MTRR4kBase ... MSR_MTRR4kBase + 8: - case MSR_MTRR16kBase ... MSR_MTRR16kBase + 1: - case MSR_MTRR64kBase: - break; /* Ignore writes */ - case MSR_IA32_MISC_ENABLE: - changed = val ^ misc_enable; - /* - * If the host has disabled the NX feature then the guest - * also cannot use it. However, a Linux guest will try to - * enable the NX feature by writing to the MISC_ENABLE MSR. - * - * This can be safely ignored because the memory management - * code looks at CPUID.80000001H:EDX.NX to check if the - * functionality is actually enabled. - */ - changed &= ~(1UL << 34); - - /* - * Punt to userspace if any other bits are being modified. - */ - if (changed) - error = EINVAL; - - break; - case MSR_PAT: - if (pat_valid(val)) - guest_msrs[IDX_MSR_PAT] = val; - else - vm_inject_gp(vmx->vm, vcpuid); - break; - default: - error = EINVAL; - break; - } - - return (error); -} diff --git a/vmm/intel/vmx_msr.h b/vmm/intel/vmx_msr.h deleted file mode 100644 index e77881c..0000000 --- a/vmm/intel/vmx_msr.h +++ /dev/null @@ -1,70 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#ifndef _VMX_MSR_H_ -#define _VMX_MSR_H_ - -struct vmx; - -void vmx_msr_init(void); -void vmx_msr_guest_init(struct vmx *vmx, int vcpuid); -void vmx_msr_guest_enter(struct vmx *vmx, int vcpuid); -void vmx_msr_guest_exit(struct vmx *vmx, int vcpuid); -int vmx_rdmsr(struct vmx *, int vcpuid, u_int num, uint64_t *val, bool *retu); -int vmx_wrmsr(struct vmx *, int vcpuid, u_int num, uint64_t val, bool *retu); - -uint32_t vmx_revision(void); - -int vmx_set_ctlreg(int ctl_reg, int true_ctl_reg, uint32_t ones_mask, - uint32_t zeros_mask, uint32_t *retval); - -/* - * According to Section 21.10.4 "Software Access to Related Structures", - * changes to data structures pointed to by the VMCS must be made only when - * there is no logical processor with a current VMCS that points to the - * data structure. - * - * This pretty much limits us to configuring the MSR bitmap before VMCS - * initialization for SMP VMs. Unless of course we do it the hard way - which - * would involve some form of synchronization between the vcpus to vmclear - * all VMCSs' that point to the bitmap. - */ -#define MSR_BITMAP_ACCESS_NONE 0x0 -#define MSR_BITMAP_ACCESS_READ 0x1 -#define MSR_BITMAP_ACCESS_WRITE 0x2 -#define MSR_BITMAP_ACCESS_RW (MSR_BITMAP_ACCESS_READ|MSR_BITMAP_ACCESS_WRITE) -void msr_bitmap_initialize(char *bitmap); -int msr_bitmap_change_access(char *bitmap, u_int msr, int access); - -#define guest_msr_rw(vmx, msr) \ - msr_bitmap_change_access((vmx)->msr_bitmap, (msr), MSR_BITMAP_ACCESS_RW) - -#define guest_msr_ro(vmx, msr) \ - msr_bitmap_change_access((vmx)->msr_bitmap, (msr), MSR_BITMAP_ACCESS_READ) - -#endif diff --git a/vmm/intel/vmx_support.S b/vmm/intel/vmx_support.S deleted file mode 100644 index 84fb5b0..0000000 --- a/vmm/intel/vmx_support.S +++ /dev/null @@ -1,262 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * Copyright (c) 2013 Neel Natu - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include - -#include "vmx_assym.h" - -#ifdef SMP -#define LK lock ; -#else -#define LK -#endif - -/* Be friendly to DTrace FBT's prologue/epilogue pattern matching */ -#define VENTER push %rbp ; mov %rsp,%rbp -#define VLEAVE pop %rbp - -/* - * Assumes that %rdi holds a pointer to the 'vmxctx'. - * - * On "return" all registers are updated to reflect guest state. The two - * exceptions are %rip and %rsp. These registers are atomically switched - * by hardware from the guest area of the vmcs. - * - * We modify %rsp to point to the 'vmxctx' so we can use it to restore - * host context in case of an error with 'vmlaunch' or 'vmresume'. - */ -#define VMX_GUEST_RESTORE \ - movq %rdi,%rsp; \ - movq VMXCTX_GUEST_CR2(%rdi),%rsi; \ - movq %rsi,%cr2; \ - movq VMXCTX_GUEST_RSI(%rdi),%rsi; \ - movq VMXCTX_GUEST_RDX(%rdi),%rdx; \ - movq VMXCTX_GUEST_RCX(%rdi),%rcx; \ - movq VMXCTX_GUEST_R8(%rdi),%r8; \ - movq VMXCTX_GUEST_R9(%rdi),%r9; \ - movq VMXCTX_GUEST_RAX(%rdi),%rax; \ - movq VMXCTX_GUEST_RBX(%rdi),%rbx; \ - movq VMXCTX_GUEST_RBP(%rdi),%rbp; \ - movq VMXCTX_GUEST_R10(%rdi),%r10; \ - movq VMXCTX_GUEST_R11(%rdi),%r11; \ - movq VMXCTX_GUEST_R12(%rdi),%r12; \ - movq VMXCTX_GUEST_R13(%rdi),%r13; \ - movq VMXCTX_GUEST_R14(%rdi),%r14; \ - movq VMXCTX_GUEST_R15(%rdi),%r15; \ - movq VMXCTX_GUEST_RDI(%rdi),%rdi; /* restore rdi the last */ - -/* - * Save and restore the host context. - * - * Assumes that %rdi holds a pointer to the 'vmxctx'. - */ -#define VMX_HOST_SAVE \ - movq %r15, VMXCTX_HOST_R15(%rdi); \ - movq %r14, VMXCTX_HOST_R14(%rdi); \ - movq %r13, VMXCTX_HOST_R13(%rdi); \ - movq %r12, VMXCTX_HOST_R12(%rdi); \ - movq %rbp, VMXCTX_HOST_RBP(%rdi); \ - movq %rsp, VMXCTX_HOST_RSP(%rdi); \ - movq %rbx, VMXCTX_HOST_RBX(%rdi); \ - -#define VMX_HOST_RESTORE \ - movq VMXCTX_HOST_R15(%rdi), %r15; \ - movq VMXCTX_HOST_R14(%rdi), %r14; \ - movq VMXCTX_HOST_R13(%rdi), %r13; \ - movq VMXCTX_HOST_R12(%rdi), %r12; \ - movq VMXCTX_HOST_RBP(%rdi), %rbp; \ - movq VMXCTX_HOST_RSP(%rdi), %rsp; \ - movq VMXCTX_HOST_RBX(%rdi), %rbx; \ - -/* - * vmx_enter_guest(struct vmxctx *vmxctx, int launched) - * %rdi: pointer to the 'vmxctx' - * %rsi: pointer to the 'vmx' - * %edx: launch state of the VMCS - * Interrupts must be disabled on entry. - */ -ENTRY(vmx_enter_guest) - VENTER - /* - * Save host state before doing anything else. - */ - VMX_HOST_SAVE - - /* - * Activate guest pmap on this cpu. - */ - movq VMXCTX_PMAP(%rdi), %r11 - movl PCPU(CPUID), %eax - LK btsl %eax, PM_ACTIVE(%r11) - - /* - * If 'vmx->eptgen[curcpu]' is not identical to 'pmap->pm_eptgen' - * then we must invalidate all mappings associated with this EPTP. - */ - movq PM_EPTGEN(%r11), %r10 - cmpq %r10, VMX_EPTGEN(%rsi, %rax, 8) - je guest_restore - - /* Refresh 'vmx->eptgen[curcpu]' */ - movq %r10, VMX_EPTGEN(%rsi, %rax, 8) - - /* Setup the invept descriptor on the host stack */ - mov %rsp, %r11 - movq VMX_EPTP(%rsi), %rax - movq %rax, -16(%r11) - movq $0x0, -8(%r11) - mov $0x1, %eax /* Single context invalidate */ - invept -16(%r11), %rax - jbe invept_error /* Check invept instruction error */ - -guest_restore: - cmpl $0, %edx - je do_launch - - VMX_GUEST_RESTORE - vmresume - /* - * In the common case 'vmresume' returns back to the host through - * 'vmx_exit_guest' with %rsp pointing to 'vmxctx'. - * - * If there is an error we return VMX_VMRESUME_ERROR to the caller. - */ - movq %rsp, %rdi /* point %rdi back to 'vmxctx' */ - movl $VMX_VMRESUME_ERROR, %eax - jmp decode_inst_error - -do_launch: - VMX_GUEST_RESTORE - vmlaunch - /* - * In the common case 'vmlaunch' returns back to the host through - * 'vmx_exit_guest' with %rsp pointing to 'vmxctx'. - * - * If there is an error we return VMX_VMLAUNCH_ERROR to the caller. - */ - movq %rsp, %rdi /* point %rdi back to 'vmxctx' */ - movl $VMX_VMLAUNCH_ERROR, %eax - jmp decode_inst_error - -invept_error: - movl $VMX_INVEPT_ERROR, %eax - jmp decode_inst_error - -decode_inst_error: - movl $VM_FAIL_VALID, %r11d - jz inst_error - movl $VM_FAIL_INVALID, %r11d -inst_error: - movl %r11d, VMXCTX_INST_FAIL_STATUS(%rdi) - - /* - * The return value is already populated in %eax so we cannot use - * it as a scratch register beyond this point. - */ - - /* - * Deactivate guest pmap from this cpu. - */ - movq VMXCTX_PMAP(%rdi), %r11 - movl PCPU(CPUID), %r10d - LK btrl %r10d, PM_ACTIVE(%r11) - - VMX_HOST_RESTORE - VLEAVE - ret - -/* - * Non-error VM-exit from the guest. Make this a label so it can - * be used by C code when setting up the VMCS. - * The VMCS-restored %rsp points to the struct vmxctx - */ - ALIGN_TEXT - .globl vmx_exit_guest -vmx_exit_guest: - /* - * Save guest state that is not automatically saved in the vmcs. - */ - movq %rdi,VMXCTX_GUEST_RDI(%rsp) - movq %rsi,VMXCTX_GUEST_RSI(%rsp) - movq %rdx,VMXCTX_GUEST_RDX(%rsp) - movq %rcx,VMXCTX_GUEST_RCX(%rsp) - movq %r8,VMXCTX_GUEST_R8(%rsp) - movq %r9,VMXCTX_GUEST_R9(%rsp) - movq %rax,VMXCTX_GUEST_RAX(%rsp) - movq %rbx,VMXCTX_GUEST_RBX(%rsp) - movq %rbp,VMXCTX_GUEST_RBP(%rsp) - movq %r10,VMXCTX_GUEST_R10(%rsp) - movq %r11,VMXCTX_GUEST_R11(%rsp) - movq %r12,VMXCTX_GUEST_R12(%rsp) - movq %r13,VMXCTX_GUEST_R13(%rsp) - movq %r14,VMXCTX_GUEST_R14(%rsp) - movq %r15,VMXCTX_GUEST_R15(%rsp) - - movq %cr2,%rdi - movq %rdi,VMXCTX_GUEST_CR2(%rsp) - - movq %rsp,%rdi - - /* - * Deactivate guest pmap from this cpu. - */ - movq VMXCTX_PMAP(%rdi), %r11 - movl PCPU(CPUID), %r10d - LK btrl %r10d, PM_ACTIVE(%r11) - - VMX_HOST_RESTORE - - /* - * This will return to the caller of 'vmx_enter_guest()' with a return - * value of VMX_GUEST_VMEXIT. - */ - movl $VMX_GUEST_VMEXIT, %eax - VLEAVE - ret -END(vmx_enter_guest) - -/* - * %rdi = interrupt handler entry point - * - * Calling sequence described in the "Instruction Set Reference" for the "INT" - * instruction in Intel SDM, Vol 2. - */ -ENTRY(vmx_call_isr) - VENTER - mov %rsp, %r11 /* save %rsp */ - and $~0xf, %rsp /* align on 16-byte boundary */ - pushq $KERNEL_SS /* %ss */ - pushq %r11 /* %rsp */ - pushfq /* %rflags */ - pushq $KERNEL_CS /* %cs */ - cli /* disable interrupts */ - callq *%rdi /* push %rip and call isr */ - VLEAVE - ret -END(vmx_call_isr) diff --git a/vmm/intel/vtd.c b/vmm/intel/vtd.c deleted file mode 100644 index be57aff..0000000 --- a/vmm/intel/vtd.c +++ /dev/null @@ -1,688 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include - -#include -#include - -#include - -#include -#include - -#include "io/iommu.h" - -/* - * Documented in the "Intel Virtualization Technology for Directed I/O", - * Architecture Spec, September 2008. - */ - -/* Section 10.4 "Register Descriptions" */ -struct vtdmap { - volatile uint32_t version; - volatile uint32_t res0; - volatile uint64_t cap; - volatile uint64_t ext_cap; - volatile uint32_t gcr; - volatile uint32_t gsr; - volatile uint64_t rta; - volatile uint64_t ccr; -}; - -#define VTD_CAP_SAGAW(cap) (((cap) >> 8) & 0x1F) -#define VTD_CAP_ND(cap) ((cap) & 0x7) -#define VTD_CAP_CM(cap) (((cap) >> 7) & 0x1) -#define VTD_CAP_SPS(cap) (((cap) >> 34) & 0xF) -#define VTD_CAP_RWBF(cap) (((cap) >> 4) & 0x1) - -#define VTD_ECAP_DI(ecap) (((ecap) >> 2) & 0x1) -#define VTD_ECAP_COHERENCY(ecap) ((ecap) & 0x1) -#define VTD_ECAP_IRO(ecap) (((ecap) >> 8) & 0x3FF) - -#define VTD_GCR_WBF (1 << 27) -#define VTD_GCR_SRTP (1 << 30) -#define VTD_GCR_TE (1U << 31) - -#define VTD_GSR_WBFS (1 << 27) -#define VTD_GSR_RTPS (1 << 30) -#define VTD_GSR_TES (1U << 31) - -#define VTD_CCR_ICC (1UL << 63) /* invalidate context cache */ -#define VTD_CCR_CIRG_GLOBAL (1UL << 61) /* global invalidation */ - -#define VTD_IIR_IVT (1UL << 63) /* invalidation IOTLB */ -#define VTD_IIR_IIRG_GLOBAL (1ULL << 60) /* global IOTLB invalidation */ -#define VTD_IIR_IIRG_DOMAIN (2ULL << 60) /* domain IOTLB invalidation */ -#define VTD_IIR_IIRG_PAGE (3ULL << 60) /* page IOTLB invalidation */ -#define VTD_IIR_DRAIN_READS (1ULL << 49) /* drain pending DMA reads */ -#define VTD_IIR_DRAIN_WRITES (1ULL << 48) /* drain pending DMA writes */ -#define VTD_IIR_DOMAIN_P 32 - -#define VTD_ROOT_PRESENT 0x1 -#define VTD_CTX_PRESENT 0x1 -#define VTD_CTX_TT_ALL (1UL << 2) - -#define VTD_PTE_RD (1UL << 0) -#define VTD_PTE_WR (1UL << 1) -#define VTD_PTE_SUPERPAGE (1UL << 7) -#define VTD_PTE_ADDR_M (0x000FFFFFFFFFF000UL) - -#define VTD_RID2IDX(rid) (((rid) & 0xff) * 2) - -struct domain { - uint64_t *ptp; /* first level page table page */ - int pt_levels; /* number of page table levels */ - int addrwidth; /* 'AW' field in context entry */ - int spsmask; /* supported super page sizes */ - u_int id; /* domain id */ - vm_paddr_t maxaddr; /* highest address to be mapped */ - SLIST_ENTRY(domain) next; -}; - -static SLIST_HEAD(, domain) domhead; - -#define DRHD_MAX_UNITS 8 -static int drhd_num; -static struct vtdmap *vtdmaps[DRHD_MAX_UNITS]; -static int max_domains; -typedef int (*drhd_ident_func_t)(void); - -static uint64_t root_table[PAGE_SIZE / sizeof(uint64_t)] __aligned(4096); -static uint64_t ctx_tables[256][PAGE_SIZE / sizeof(uint64_t)] __aligned(4096); - -static MALLOC_DEFINE(M_VTD, "vtd", "vtd"); - -static int -vtd_max_domains(struct vtdmap *vtdmap) -{ - int nd; - - nd = VTD_CAP_ND(vtdmap->cap); - - switch (nd) { - case 0: - return (16); - case 1: - return (64); - case 2: - return (256); - case 3: - return (1024); - case 4: - return (4 * 1024); - case 5: - return (16 * 1024); - case 6: - return (64 * 1024); - default: - panic("vtd_max_domains: invalid value of nd (0x%0x)", nd); - } -} - -static u_int -domain_id(void) -{ - u_int id; - struct domain *dom; - - /* Skip domain id 0 - it is reserved when Caching Mode field is set */ - for (id = 1; id < max_domains; id++) { - SLIST_FOREACH(dom, &domhead, next) { - if (dom->id == id) - break; - } - if (dom == NULL) - break; /* found it */ - } - - if (id >= max_domains) - panic("domain ids exhausted"); - - return (id); -} - -static void -vtd_wbflush(struct vtdmap *vtdmap) -{ - - if (VTD_ECAP_COHERENCY(vtdmap->ext_cap) == 0) - pmap_invalidate_cache(); - - if (VTD_CAP_RWBF(vtdmap->cap)) { - vtdmap->gcr = VTD_GCR_WBF; - while ((vtdmap->gsr & VTD_GSR_WBFS) != 0) - ; - } -} - -static void -vtd_ctx_global_invalidate(struct vtdmap *vtdmap) -{ - - vtdmap->ccr = VTD_CCR_ICC | VTD_CCR_CIRG_GLOBAL; - while ((vtdmap->ccr & VTD_CCR_ICC) != 0) - ; -} - -static void -vtd_iotlb_global_invalidate(struct vtdmap *vtdmap) -{ - int offset; - volatile uint64_t *iotlb_reg, val; - - vtd_wbflush(vtdmap); - - offset = VTD_ECAP_IRO(vtdmap->ext_cap) * 16; - iotlb_reg = (volatile uint64_t *)((caddr_t)vtdmap + offset + 8); - - *iotlb_reg = VTD_IIR_IVT | VTD_IIR_IIRG_GLOBAL | - VTD_IIR_DRAIN_READS | VTD_IIR_DRAIN_WRITES; - - while (1) { - val = *iotlb_reg; - if ((val & VTD_IIR_IVT) == 0) - break; - } -} - -static void -vtd_translation_enable(struct vtdmap *vtdmap) -{ - - vtdmap->gcr = VTD_GCR_TE; - while ((vtdmap->gsr & VTD_GSR_TES) == 0) - ; -} - -static void -vtd_translation_disable(struct vtdmap *vtdmap) -{ - - vtdmap->gcr = 0; - while ((vtdmap->gsr & VTD_GSR_TES) != 0) - ; -} - -static int -vtd_init(void) -{ - int i, units, remaining; - struct vtdmap *vtdmap; - vm_paddr_t ctx_paddr; - char *end, envname[32]; - unsigned long mapaddr; - ACPI_STATUS status; - ACPI_TABLE_DMAR *dmar; - ACPI_DMAR_HEADER *hdr; - ACPI_DMAR_HARDWARE_UNIT *drhd; - - /* - * Allow the user to override the ACPI DMAR table by specifying the - * physical address of each remapping unit. - * - * The following example specifies two remapping units at - * physical addresses 0xfed90000 and 0xfeda0000 respectively. - * set vtd.regmap.0.addr=0xfed90000 - * set vtd.regmap.1.addr=0xfeda0000 - */ - for (units = 0; units < DRHD_MAX_UNITS; units++) { - snprintf(envname, sizeof(envname), "vtd.regmap.%d.addr", units); - if (getenv_ulong(envname, &mapaddr) == 0) - break; - vtdmaps[units] = (struct vtdmap *)PHYS_TO_DMAP(mapaddr); - } - - if (units > 0) - goto skip_dmar; - - /* Search for DMAR table. */ - status = AcpiGetTable(ACPI_SIG_DMAR, 0, (ACPI_TABLE_HEADER **)&dmar); - if (ACPI_FAILURE(status)) - return (ENXIO); - - end = (char *)dmar + dmar->Header.Length; - remaining = dmar->Header.Length - sizeof(ACPI_TABLE_DMAR); - while (remaining > sizeof(ACPI_DMAR_HEADER)) { - hdr = (ACPI_DMAR_HEADER *)(end - remaining); - if (hdr->Length > remaining) - break; - /* - * From Intel VT-d arch spec, version 1.3: - * BIOS implementations must report mapping structures - * in numerical order, i.e. All remapping structures of - * type 0 (DRHD) enumerated before remapping structures of - * type 1 (RMRR) and so forth. - */ - if (hdr->Type != ACPI_DMAR_TYPE_HARDWARE_UNIT) - break; - - drhd = (ACPI_DMAR_HARDWARE_UNIT *)hdr; - vtdmaps[units++] = (struct vtdmap *)PHYS_TO_DMAP(drhd->Address); - if (units >= DRHD_MAX_UNITS) - break; - remaining -= hdr->Length; - } - - if (units <= 0) - return (ENXIO); - -skip_dmar: - drhd_num = units; - vtdmap = vtdmaps[0]; - - if (VTD_CAP_CM(vtdmap->cap) != 0) - panic("vtd_init: invalid caching mode"); - - max_domains = vtd_max_domains(vtdmap); - - /* - * Set up the root-table to point to the context-entry tables - */ - for (i = 0; i < 256; i++) { - ctx_paddr = vtophys(ctx_tables[i]); - if (ctx_paddr & PAGE_MASK) - panic("ctx table (0x%0lx) not page aligned", ctx_paddr); - - root_table[i * 2] = ctx_paddr | VTD_ROOT_PRESENT; - } - - return (0); -} - -static void -vtd_cleanup(void) -{ -} - -static void -vtd_enable(void) -{ - int i; - struct vtdmap *vtdmap; - - for (i = 0; i < drhd_num; i++) { - vtdmap = vtdmaps[i]; - vtd_wbflush(vtdmap); - - /* Update the root table address */ - vtdmap->rta = vtophys(root_table); - vtdmap->gcr = VTD_GCR_SRTP; - while ((vtdmap->gsr & VTD_GSR_RTPS) == 0) - ; - - vtd_ctx_global_invalidate(vtdmap); - vtd_iotlb_global_invalidate(vtdmap); - - vtd_translation_enable(vtdmap); - } -} - -static void -vtd_disable(void) -{ - int i; - struct vtdmap *vtdmap; - - for (i = 0; i < drhd_num; i++) { - vtdmap = vtdmaps[i]; - vtd_translation_disable(vtdmap); - } -} - -static void -vtd_add_device(void *arg, uint16_t rid) -{ - int idx; - uint64_t *ctxp; - struct domain *dom = arg; - vm_paddr_t pt_paddr; - struct vtdmap *vtdmap; - uint8_t bus; - - vtdmap = vtdmaps[0]; - bus = PCI_RID2BUS(rid); - ctxp = ctx_tables[bus]; - pt_paddr = vtophys(dom->ptp); - idx = VTD_RID2IDX(rid); - - if (ctxp[idx] & VTD_CTX_PRESENT) { - panic("vtd_add_device: device %x is already owned by " - "domain %d", rid, - (uint16_t)(ctxp[idx + 1] >> 8)); - } - - /* - * Order is important. The 'present' bit is set only after all fields - * of the context pointer are initialized. - */ - ctxp[idx + 1] = dom->addrwidth | (dom->id << 8); - - if (VTD_ECAP_DI(vtdmap->ext_cap)) - ctxp[idx] = VTD_CTX_TT_ALL; - else - ctxp[idx] = 0; - - ctxp[idx] |= pt_paddr | VTD_CTX_PRESENT; - - /* - * 'Not Present' entries are not cached in either the Context Cache - * or in the IOTLB, so there is no need to invalidate either of them. - */ -} - -static void -vtd_remove_device(void *arg, uint16_t rid) -{ - int i, idx; - uint64_t *ctxp; - struct vtdmap *vtdmap; - uint8_t bus; - - bus = PCI_RID2BUS(rid); - ctxp = ctx_tables[bus]; - idx = VTD_RID2IDX(rid); - - /* - * Order is important. The 'present' bit is must be cleared first. - */ - ctxp[idx] = 0; - ctxp[idx + 1] = 0; - - /* - * Invalidate the Context Cache and the IOTLB. - * - * XXX use device-selective invalidation for Context Cache - * XXX use domain-selective invalidation for IOTLB - */ - for (i = 0; i < drhd_num; i++) { - vtdmap = vtdmaps[i]; - vtd_ctx_global_invalidate(vtdmap); - vtd_iotlb_global_invalidate(vtdmap); - } -} - -#define CREATE_MAPPING 0 -#define REMOVE_MAPPING 1 - -static uint64_t -vtd_update_mapping(void *arg, vm_paddr_t gpa, vm_paddr_t hpa, uint64_t len, - int remove) -{ - struct domain *dom; - int i, spshift, ptpshift, ptpindex, nlevels; - uint64_t spsize, *ptp; - - dom = arg; - ptpindex = 0; - ptpshift = 0; - - KASSERT(gpa + len > gpa, ("%s: invalid gpa range %#lx/%#lx", __func__, - gpa, len)); - KASSERT(gpa + len <= dom->maxaddr, ("%s: gpa range %#lx/%#lx beyond " - "domain maxaddr %#lx", __func__, gpa, len, dom->maxaddr)); - - if (gpa & PAGE_MASK) - panic("vtd_create_mapping: unaligned gpa 0x%0lx", gpa); - - if (hpa & PAGE_MASK) - panic("vtd_create_mapping: unaligned hpa 0x%0lx", hpa); - - if (len & PAGE_MASK) - panic("vtd_create_mapping: unaligned len 0x%0lx", len); - - /* - * Compute the size of the mapping that we can accomodate. - * - * This is based on three factors: - * - supported super page size - * - alignment of the region starting at 'gpa' and 'hpa' - * - length of the region 'len' - */ - spshift = 48; - for (i = 3; i >= 0; i--) { - spsize = 1UL << spshift; - if ((dom->spsmask & (1 << i)) != 0 && - (gpa & (spsize - 1)) == 0 && - (hpa & (spsize - 1)) == 0 && - (len >= spsize)) { - break; - } - spshift -= 9; - } - - ptp = dom->ptp; - nlevels = dom->pt_levels; - while (--nlevels >= 0) { - ptpshift = 12 + nlevels * 9; - ptpindex = (gpa >> ptpshift) & 0x1FF; - - /* We have reached the leaf mapping */ - if (spshift >= ptpshift) { - break; - } - - /* - * We are working on a non-leaf page table page. - * - * Create a downstream page table page if necessary and point - * to it from the current page table. - */ - if (ptp[ptpindex] == 0) { - void *nlp = malloc(PAGE_SIZE, M_VTD, M_WAITOK | M_ZERO); - ptp[ptpindex] = vtophys(nlp)| VTD_PTE_RD | VTD_PTE_WR; - } - - ptp = (uint64_t *)PHYS_TO_DMAP(ptp[ptpindex] & VTD_PTE_ADDR_M); - } - - if ((gpa & ((1UL << ptpshift) - 1)) != 0) - panic("gpa 0x%lx and ptpshift %d mismatch", gpa, ptpshift); - - /* - * Update the 'gpa' -> 'hpa' mapping - */ - if (remove) { - ptp[ptpindex] = 0; - } else { - ptp[ptpindex] = hpa | VTD_PTE_RD | VTD_PTE_WR; - - if (nlevels > 0) - ptp[ptpindex] |= VTD_PTE_SUPERPAGE; - } - - return (1UL << ptpshift); -} - -static uint64_t -vtd_create_mapping(void *arg, vm_paddr_t gpa, vm_paddr_t hpa, uint64_t len) -{ - - return (vtd_update_mapping(arg, gpa, hpa, len, CREATE_MAPPING)); -} - -static uint64_t -vtd_remove_mapping(void *arg, vm_paddr_t gpa, uint64_t len) -{ - - return (vtd_update_mapping(arg, gpa, 0, len, REMOVE_MAPPING)); -} - -static void -vtd_invalidate_tlb(void *dom) -{ - int i; - struct vtdmap *vtdmap; - - /* - * Invalidate the IOTLB. - * XXX use domain-selective invalidation for IOTLB - */ - for (i = 0; i < drhd_num; i++) { - vtdmap = vtdmaps[i]; - vtd_iotlb_global_invalidate(vtdmap); - } -} - -static void * -vtd_create_domain(vm_paddr_t maxaddr) -{ - struct domain *dom; - vm_paddr_t addr; - int tmp, i, gaw, agaw, sagaw, res, pt_levels, addrwidth; - struct vtdmap *vtdmap; - - if (drhd_num <= 0) - panic("vtd_create_domain: no dma remapping hardware available"); - - vtdmap = vtdmaps[0]; - - /* - * Calculate AGAW. - * Section 3.4.2 "Adjusted Guest Address Width", Architecture Spec. - */ - addr = 0; - for (gaw = 0; addr < maxaddr; gaw++) - addr = 1ULL << gaw; - - res = (gaw - 12) % 9; - if (res == 0) - agaw = gaw; - else - agaw = gaw + 9 - res; - - if (agaw > 64) - agaw = 64; - - /* - * Select the smallest Supported AGAW and the corresponding number - * of page table levels. - */ - pt_levels = 2; - sagaw = 30; - addrwidth = 0; - tmp = VTD_CAP_SAGAW(vtdmap->cap); - for (i = 0; i < 5; i++) { - if ((tmp & (1 << i)) != 0 && sagaw >= agaw) - break; - pt_levels++; - addrwidth++; - sagaw += 9; - if (sagaw > 64) - sagaw = 64; - } - - if (i >= 5) { - panic("vtd_create_domain: SAGAW 0x%lx does not support AGAW %d", - VTD_CAP_SAGAW(vtdmap->cap), agaw); - } - - dom = malloc(sizeof(struct domain), M_VTD, M_ZERO | M_WAITOK); - dom->pt_levels = pt_levels; - dom->addrwidth = addrwidth; - dom->id = domain_id(); - dom->maxaddr = maxaddr; - dom->ptp = malloc(PAGE_SIZE, M_VTD, M_ZERO | M_WAITOK); - if ((uintptr_t)dom->ptp & PAGE_MASK) - panic("vtd_create_domain: ptp (%p) not page aligned", dom->ptp); - -#ifdef notyet - /* - * XXX superpage mappings for the iommu do not work correctly. - * - * By default all physical memory is mapped into the host_domain. - * When a VM is allocated wired memory the pages belonging to it - * are removed from the host_domain and added to the vm's domain. - * - * If the page being removed was mapped using a superpage mapping - * in the host_domain then we need to demote the mapping before - * removing the page. - * - * There is not any code to deal with the demotion at the moment - * so we disable superpage mappings altogether. - */ - dom->spsmask = VTD_CAP_SPS(vtdmap->cap); -#endif - - SLIST_INSERT_HEAD(&domhead, dom, next); - - return (dom); -} - -static void -vtd_free_ptp(uint64_t *ptp, int level) -{ - int i; - uint64_t *nlp; - - if (level > 1) { - for (i = 0; i < 512; i++) { - if ((ptp[i] & (VTD_PTE_RD | VTD_PTE_WR)) == 0) - continue; - if ((ptp[i] & VTD_PTE_SUPERPAGE) != 0) - continue; - nlp = (uint64_t *)PHYS_TO_DMAP(ptp[i] & VTD_PTE_ADDR_M); - vtd_free_ptp(nlp, level - 1); - } - } - - bzero(ptp, PAGE_SIZE); - free(ptp, M_VTD); -} - -static void -vtd_destroy_domain(void *arg) -{ - struct domain *dom; - - dom = arg; - - SLIST_REMOVE(&domhead, dom, domain, next); - vtd_free_ptp(dom->ptp, dom->pt_levels); - free(dom, M_VTD); -} - -struct iommu_ops iommu_ops_intel = { - vtd_init, - vtd_cleanup, - vtd_enable, - vtd_disable, - vtd_create_domain, - vtd_destroy_domain, - vtd_create_mapping, - vtd_remove_mapping, - vtd_add_device, - vtd_remove_device, - vtd_invalidate_tlb, -}; diff --git a/vmm/io/iommu.c b/vmm/io/iommu.c deleted file mode 100644 index 9cfc4c2..0000000 --- a/vmm/io/iommu.c +++ /dev/null @@ -1,285 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include - -#include -#include - -#include - -#include "vmm_util.h" -#include "vmm_mem.h" -#include "iommu.h" - -SYSCTL_DECL(_hw_vmm); -SYSCTL_NODE(_hw_vmm, OID_AUTO, iommu, CTLFLAG_RW, 0, "bhyve iommu parameters"); - -static int iommu_avail; -SYSCTL_INT(_hw_vmm_iommu, OID_AUTO, initialized, CTLFLAG_RD, &iommu_avail, - 0, "bhyve iommu initialized?"); - -static struct iommu_ops *ops; -static void *host_domain; - -static __inline int -IOMMU_INIT(void) -{ - if (ops != NULL) - return ((*ops->init)()); - else - return (ENXIO); -} - -static __inline void -IOMMU_CLEANUP(void) -{ - if (ops != NULL && iommu_avail) - (*ops->cleanup)(); -} - -static __inline void * -IOMMU_CREATE_DOMAIN(vm_paddr_t maxaddr) -{ - - if (ops != NULL && iommu_avail) - return ((*ops->create_domain)(maxaddr)); - else - return (NULL); -} - -static __inline void -IOMMU_DESTROY_DOMAIN(void *dom) -{ - - if (ops != NULL && iommu_avail) - (*ops->destroy_domain)(dom); -} - -static __inline uint64_t -IOMMU_CREATE_MAPPING(void *domain, vm_paddr_t gpa, vm_paddr_t hpa, uint64_t len) -{ - - if (ops != NULL && iommu_avail) - return ((*ops->create_mapping)(domain, gpa, hpa, len)); - else - return (len); /* XXX */ -} - -static __inline uint64_t -IOMMU_REMOVE_MAPPING(void *domain, vm_paddr_t gpa, uint64_t len) -{ - - if (ops != NULL && iommu_avail) - return ((*ops->remove_mapping)(domain, gpa, len)); - else - return (len); /* XXX */ -} - -static __inline void -IOMMU_ADD_DEVICE(void *domain, uint16_t rid) -{ - - if (ops != NULL && iommu_avail) - (*ops->add_device)(domain, rid); -} - -static __inline void -IOMMU_REMOVE_DEVICE(void *domain, uint16_t rid) -{ - - if (ops != NULL && iommu_avail) - (*ops->remove_device)(domain, rid); -} - -static __inline void -IOMMU_INVALIDATE_TLB(void *domain) -{ - - if (ops != NULL && iommu_avail) - (*ops->invalidate_tlb)(domain); -} - -static __inline void -IOMMU_ENABLE(void) -{ - - if (ops != NULL && iommu_avail) - (*ops->enable)(); -} - -static __inline void -IOMMU_DISABLE(void) -{ - - if (ops != NULL && iommu_avail) - (*ops->disable)(); -} - -void -iommu_init(void) -{ - int error, bus, slot, func; - vm_paddr_t maxaddr; - const char *name; - device_t dev; - - if (vmm_is_intel()) - ops = &iommu_ops_intel; - else if (vmm_is_amd()) - ops = &iommu_ops_amd; - else - ops = NULL; - - error = IOMMU_INIT(); - if (error) - return; - - iommu_avail = 1; - - /* - * Create a domain for the devices owned by the host - */ - maxaddr = vmm_mem_maxaddr(); - host_domain = IOMMU_CREATE_DOMAIN(maxaddr); - if (host_domain == NULL) - panic("iommu_init: unable to create a host domain"); - - /* - * Create 1:1 mappings from '0' to 'maxaddr' for devices assigned to - * the host - */ - iommu_create_mapping(host_domain, 0, 0, maxaddr); - - for (bus = 0; bus <= PCI_BUSMAX; bus++) { - for (slot = 0; slot <= PCI_SLOTMAX; slot++) { - for (func = 0; func <= PCI_FUNCMAX; func++) { - dev = pci_find_dbsf(0, bus, slot, func); - if (dev == NULL) - continue; - - /* skip passthrough devices */ - name = device_get_name(dev); - if (name != NULL && strcmp(name, "ppt") == 0) - continue; - - /* everything else belongs to the host domain */ - iommu_add_device(host_domain, - pci_get_rid(dev)); - } - } - } - IOMMU_ENABLE(); - -} - -void -iommu_cleanup(void) -{ - IOMMU_DISABLE(); - IOMMU_DESTROY_DOMAIN(host_domain); - IOMMU_CLEANUP(); -} - -void * -iommu_create_domain(vm_paddr_t maxaddr) -{ - - return (IOMMU_CREATE_DOMAIN(maxaddr)); -} - -void -iommu_destroy_domain(void *dom) -{ - - IOMMU_DESTROY_DOMAIN(dom); -} - -void -iommu_create_mapping(void *dom, vm_paddr_t gpa, vm_paddr_t hpa, size_t len) -{ - uint64_t mapped, remaining; - - remaining = len; - - while (remaining > 0) { - mapped = IOMMU_CREATE_MAPPING(dom, gpa, hpa, remaining); - gpa += mapped; - hpa += mapped; - remaining -= mapped; - } -} - -void -iommu_remove_mapping(void *dom, vm_paddr_t gpa, size_t len) -{ - uint64_t unmapped, remaining; - - remaining = len; - - while (remaining > 0) { - unmapped = IOMMU_REMOVE_MAPPING(dom, gpa, remaining); - gpa += unmapped; - remaining -= unmapped; - } -} - -void * -iommu_host_domain(void) -{ - - return (host_domain); -} - -void -iommu_add_device(void *dom, uint16_t rid) -{ - - IOMMU_ADD_DEVICE(dom, rid); -} - -void -iommu_remove_device(void *dom, uint16_t rid) -{ - - IOMMU_REMOVE_DEVICE(dom, rid); -} - -void -iommu_invalidate_tlb(void *domain) -{ - - IOMMU_INVALIDATE_TLB(domain); -} diff --git a/vmm/io/iommu.h b/vmm/io/iommu.h deleted file mode 100644 index 36b44fa..0000000 --- a/vmm/io/iommu.h +++ /dev/null @@ -1,75 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#ifndef _IO_IOMMU_H_ -#define _IO_IOMMU_H_ - -typedef int (*iommu_init_func_t)(void); -typedef void (*iommu_cleanup_func_t)(void); -typedef void (*iommu_enable_func_t)(void); -typedef void (*iommu_disable_func_t)(void); -typedef void *(*iommu_create_domain_t)(vm_paddr_t maxaddr); -typedef void (*iommu_destroy_domain_t)(void *domain); -typedef uint64_t (*iommu_create_mapping_t)(void *domain, vm_paddr_t gpa, - vm_paddr_t hpa, uint64_t len); -typedef uint64_t (*iommu_remove_mapping_t)(void *domain, vm_paddr_t gpa, - uint64_t len); -typedef void (*iommu_add_device_t)(void *domain, uint16_t rid); -typedef void (*iommu_remove_device_t)(void *dom, uint16_t rid); -typedef void (*iommu_invalidate_tlb_t)(void *dom); - -struct iommu_ops { - iommu_init_func_t init; /* module wide */ - iommu_cleanup_func_t cleanup; - iommu_enable_func_t enable; - iommu_disable_func_t disable; - - iommu_create_domain_t create_domain; /* domain-specific */ - iommu_destroy_domain_t destroy_domain; - iommu_create_mapping_t create_mapping; - iommu_remove_mapping_t remove_mapping; - iommu_add_device_t add_device; - iommu_remove_device_t remove_device; - iommu_invalidate_tlb_t invalidate_tlb; -}; - -extern struct iommu_ops iommu_ops_intel; -extern struct iommu_ops iommu_ops_amd; - -void iommu_init(void); -void iommu_cleanup(void); -void *iommu_host_domain(void); -void *iommu_create_domain(vm_paddr_t maxaddr); -void iommu_destroy_domain(void *dom); -void iommu_create_mapping(void *dom, vm_paddr_t gpa, vm_paddr_t hpa, - size_t len); -void iommu_remove_mapping(void *dom, vm_paddr_t gpa, size_t len); -void iommu_add_device(void *dom, uint16_t rid); -void iommu_remove_device(void *dom, uint16_t rid); -void iommu_invalidate_tlb(void *domain); -#endif diff --git a/vmm/io/ppt.c b/vmm/io/ppt.c deleted file mode 100644 index b789f77..0000000 --- a/vmm/io/ppt.c +++ /dev/null @@ -1,651 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include - -#include -#include - -#include "vmm_lapic.h" -#include "vmm_ktr.h" - -#include "iommu.h" -#include "ppt.h" - -/* XXX locking */ - -#define MAX_MSIMSGS 32 - -/* - * If the MSI-X table is located in the middle of a BAR then that MMIO - * region gets split into two segments - one segment above the MSI-X table - * and the other segment below the MSI-X table - with a hole in place of - * the MSI-X table so accesses to it can be trapped and emulated. - * - * So, allocate a MMIO segment for each BAR register + 1 additional segment. - */ -#define MAX_MMIOSEGS ((PCIR_MAX_BAR_0 + 1) + 1) - -MALLOC_DEFINE(M_PPTMSIX, "pptmsix", "Passthru MSI-X resources"); - -struct pptintr_arg { /* pptintr(pptintr_arg) */ - struct pptdev *pptdev; - uint64_t addr; - uint64_t msg_data; -}; - -struct pptdev { - device_t dev; - struct vm *vm; /* owner of this device */ - TAILQ_ENTRY(pptdev) next; - struct vm_memory_segment mmio[MAX_MMIOSEGS]; - struct { - int num_msgs; /* guest state */ - - int startrid; /* host state */ - struct resource *res[MAX_MSIMSGS]; - void *cookie[MAX_MSIMSGS]; - struct pptintr_arg arg[MAX_MSIMSGS]; - } msi; - - struct { - int num_msgs; - int startrid; - int msix_table_rid; - struct resource *msix_table_res; - struct resource **res; - void **cookie; - struct pptintr_arg *arg; - } msix; -}; - -SYSCTL_DECL(_hw_vmm); -SYSCTL_NODE(_hw_vmm, OID_AUTO, ppt, CTLFLAG_RW, 0, "bhyve passthru devices"); - -static int num_pptdevs; -SYSCTL_INT(_hw_vmm_ppt, OID_AUTO, devices, CTLFLAG_RD, &num_pptdevs, 0, - "number of pci passthru devices"); - -static TAILQ_HEAD(, pptdev) pptdev_list = TAILQ_HEAD_INITIALIZER(pptdev_list); - -static int -ppt_probe(device_t dev) -{ - int bus, slot, func; - struct pci_devinfo *dinfo; - - dinfo = (struct pci_devinfo *)device_get_ivars(dev); - - bus = pci_get_bus(dev); - slot = pci_get_slot(dev); - func = pci_get_function(dev); - - /* - * To qualify as a pci passthrough device a device must: - * - be allowed by administrator to be used in this role - * - be an endpoint device - */ - if ((dinfo->cfg.hdrtype & PCIM_HDRTYPE) != PCIM_HDRTYPE_NORMAL) - return (ENXIO); - else if (vmm_is_pptdev(bus, slot, func)) - return (0); - else - /* - * Returning BUS_PROBE_NOWILDCARD here matches devices that the - * SR-IOV infrastructure specified as "ppt" passthrough devices. - * All normal devices that did not have "ppt" specified as their - * driver will not be matched by this. - */ - return (BUS_PROBE_NOWILDCARD); -} - -static int -ppt_attach(device_t dev) -{ - struct pptdev *ppt; - - ppt = device_get_softc(dev); - - num_pptdevs++; - TAILQ_INSERT_TAIL(&pptdev_list, ppt, next); - ppt->dev = dev; - - if (bootverbose) - device_printf(dev, "attached\n"); - - return (0); -} - -static int -ppt_detach(device_t dev) -{ - struct pptdev *ppt; - - ppt = device_get_softc(dev); - - if (ppt->vm != NULL) - return (EBUSY); - num_pptdevs--; - TAILQ_REMOVE(&pptdev_list, ppt, next); - - return (0); -} - -static device_method_t ppt_methods[] = { - /* Device interface */ - DEVMETHOD(device_probe, ppt_probe), - DEVMETHOD(device_attach, ppt_attach), - DEVMETHOD(device_detach, ppt_detach), - {0, 0} -}; - -static devclass_t ppt_devclass; -DEFINE_CLASS_0(ppt, ppt_driver, ppt_methods, sizeof(struct pptdev)); -DRIVER_MODULE(ppt, pci, ppt_driver, ppt_devclass, NULL, NULL); - -static struct pptdev * -ppt_find(int bus, int slot, int func) -{ - device_t dev; - struct pptdev *ppt; - int b, s, f; - - TAILQ_FOREACH(ppt, &pptdev_list, next) { - dev = ppt->dev; - b = pci_get_bus(dev); - s = pci_get_slot(dev); - f = pci_get_function(dev); - if (bus == b && slot == s && func == f) - return (ppt); - } - return (NULL); -} - -static void -ppt_unmap_mmio(struct vm *vm, struct pptdev *ppt) -{ - int i; - struct vm_memory_segment *seg; - - for (i = 0; i < MAX_MMIOSEGS; i++) { - seg = &ppt->mmio[i]; - if (seg->len == 0) - continue; - (void)vm_unmap_mmio(vm, seg->gpa, seg->len); - bzero(seg, sizeof(struct vm_memory_segment)); - } -} - -static void -ppt_teardown_msi(struct pptdev *ppt) -{ - int i, rid; - void *cookie; - struct resource *res; - - if (ppt->msi.num_msgs == 0) - return; - - for (i = 0; i < ppt->msi.num_msgs; i++) { - rid = ppt->msi.startrid + i; - res = ppt->msi.res[i]; - cookie = ppt->msi.cookie[i]; - - if (cookie != NULL) - bus_teardown_intr(ppt->dev, res, cookie); - - if (res != NULL) - bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res); - - ppt->msi.res[i] = NULL; - ppt->msi.cookie[i] = NULL; - } - - if (ppt->msi.startrid == 1) - pci_release_msi(ppt->dev); - - ppt->msi.num_msgs = 0; -} - -static void -ppt_teardown_msix_intr(struct pptdev *ppt, int idx) -{ - int rid; - struct resource *res; - void *cookie; - - rid = ppt->msix.startrid + idx; - res = ppt->msix.res[idx]; - cookie = ppt->msix.cookie[idx]; - - if (cookie != NULL) - bus_teardown_intr(ppt->dev, res, cookie); - - if (res != NULL) - bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res); - - ppt->msix.res[idx] = NULL; - ppt->msix.cookie[idx] = NULL; -} - -static void -ppt_teardown_msix(struct pptdev *ppt) -{ - int i; - - if (ppt->msix.num_msgs == 0) - return; - - for (i = 0; i < ppt->msix.num_msgs; i++) - ppt_teardown_msix_intr(ppt, i); - - if (ppt->msix.msix_table_res) { - bus_release_resource(ppt->dev, SYS_RES_MEMORY, - ppt->msix.msix_table_rid, - ppt->msix.msix_table_res); - ppt->msix.msix_table_res = NULL; - ppt->msix.msix_table_rid = 0; - } - - free(ppt->msix.res, M_PPTMSIX); - free(ppt->msix.cookie, M_PPTMSIX); - free(ppt->msix.arg, M_PPTMSIX); - - pci_release_msi(ppt->dev); - - ppt->msix.num_msgs = 0; -} - -int -ppt_avail_devices(void) -{ - - return (num_pptdevs); -} - -int -ppt_assigned_devices(struct vm *vm) -{ - struct pptdev *ppt; - int num; - - num = 0; - TAILQ_FOREACH(ppt, &pptdev_list, next) { - if (ppt->vm == vm) - num++; - } - return (num); -} - -boolean_t -ppt_is_mmio(struct vm *vm, vm_paddr_t gpa) -{ - int i; - struct pptdev *ppt; - struct vm_memory_segment *seg; - - TAILQ_FOREACH(ppt, &pptdev_list, next) { - if (ppt->vm != vm) - continue; - - for (i = 0; i < MAX_MMIOSEGS; i++) { - seg = &ppt->mmio[i]; - if (seg->len == 0) - continue; - if (gpa >= seg->gpa && gpa < seg->gpa + seg->len) - return (TRUE); - } - } - - return (FALSE); -} - -int -ppt_assign_device(struct vm *vm, int bus, int slot, int func) -{ - struct pptdev *ppt; - - ppt = ppt_find(bus, slot, func); - if (ppt != NULL) { - /* - * If this device is owned by a different VM then we - * cannot change its owner. - */ - if (ppt->vm != NULL && ppt->vm != vm) - return (EBUSY); - - ppt->vm = vm; - iommu_add_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev)); - return (0); - } - return (ENOENT); -} - -int -ppt_unassign_device(struct vm *vm, int bus, int slot, int func) -{ - struct pptdev *ppt; - - ppt = ppt_find(bus, slot, func); - if (ppt != NULL) { - /* - * If this device is not owned by this 'vm' then bail out. - */ - if (ppt->vm != vm) - return (EBUSY); - ppt_unmap_mmio(vm, ppt); - ppt_teardown_msi(ppt); - ppt_teardown_msix(ppt); - iommu_remove_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev)); - ppt->vm = NULL; - return (0); - } - return (ENOENT); -} - -int -ppt_unassign_all(struct vm *vm) -{ - struct pptdev *ppt; - int bus, slot, func; - device_t dev; - - TAILQ_FOREACH(ppt, &pptdev_list, next) { - if (ppt->vm == vm) { - dev = ppt->dev; - bus = pci_get_bus(dev); - slot = pci_get_slot(dev); - func = pci_get_function(dev); - vm_unassign_pptdev(vm, bus, slot, func); - } - } - - return (0); -} - -int -ppt_map_mmio(struct vm *vm, int bus, int slot, int func, - vm_paddr_t gpa, size_t len, vm_paddr_t hpa) -{ - int i, error; - struct vm_memory_segment *seg; - struct pptdev *ppt; - - ppt = ppt_find(bus, slot, func); - if (ppt != NULL) { - if (ppt->vm != vm) - return (EBUSY); - - for (i = 0; i < MAX_MMIOSEGS; i++) { - seg = &ppt->mmio[i]; - if (seg->len == 0) { - error = vm_map_mmio(vm, gpa, len, hpa); - if (error == 0) { - seg->gpa = gpa; - seg->len = len; - } - return (error); - } - } - return (ENOSPC); - } - return (ENOENT); -} - -static int -pptintr(void *arg) -{ - struct pptdev *ppt; - struct pptintr_arg *pptarg; - - pptarg = arg; - ppt = pptarg->pptdev; - - if (ppt->vm != NULL) - lapic_intr_msi(ppt->vm, pptarg->addr, pptarg->msg_data); - else { - /* - * XXX - * This is not expected to happen - panic? - */ - } - - /* - * For legacy interrupts give other filters a chance in case - * the interrupt was not generated by the passthrough device. - */ - if (ppt->msi.startrid == 0) - return (FILTER_STRAY); - else - return (FILTER_HANDLED); -} - -int -ppt_setup_msi(struct vm *vm, int vcpu, int bus, int slot, int func, - uint64_t addr, uint64_t msg, int numvec) -{ - int i, rid, flags; - int msi_count, startrid, error, tmp; - struct pptdev *ppt; - - if (numvec < 0 || numvec > MAX_MSIMSGS) - return (EINVAL); - - ppt = ppt_find(bus, slot, func); - if (ppt == NULL) - return (ENOENT); - if (ppt->vm != vm) /* Make sure we own this device */ - return (EBUSY); - - /* Free any allocated resources */ - ppt_teardown_msi(ppt); - - if (numvec == 0) /* nothing more to do */ - return (0); - - flags = RF_ACTIVE; - msi_count = pci_msi_count(ppt->dev); - if (msi_count == 0) { - startrid = 0; /* legacy interrupt */ - msi_count = 1; - flags |= RF_SHAREABLE; - } else - startrid = 1; /* MSI */ - - /* - * The device must be capable of supporting the number of vectors - * the guest wants to allocate. - */ - if (numvec > msi_count) - return (EINVAL); - - /* - * Make sure that we can allocate all the MSI vectors that are needed - * by the guest. - */ - if (startrid == 1) { - tmp = numvec; - error = pci_alloc_msi(ppt->dev, &tmp); - if (error) - return (error); - else if (tmp != numvec) { - pci_release_msi(ppt->dev); - return (ENOSPC); - } else { - /* success */ - } - } - - ppt->msi.startrid = startrid; - - /* - * Allocate the irq resource and attach it to the interrupt handler. - */ - for (i = 0; i < numvec; i++) { - ppt->msi.num_msgs = i + 1; - ppt->msi.cookie[i] = NULL; - - rid = startrid + i; - ppt->msi.res[i] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ, - &rid, flags); - if (ppt->msi.res[i] == NULL) - break; - - ppt->msi.arg[i].pptdev = ppt; - ppt->msi.arg[i].addr = addr; - ppt->msi.arg[i].msg_data = msg + i; - - error = bus_setup_intr(ppt->dev, ppt->msi.res[i], - INTR_TYPE_NET | INTR_MPSAFE, - pptintr, NULL, &ppt->msi.arg[i], - &ppt->msi.cookie[i]); - if (error != 0) - break; - } - - if (i < numvec) { - ppt_teardown_msi(ppt); - return (ENXIO); - } - - return (0); -} - -int -ppt_setup_msix(struct vm *vm, int vcpu, int bus, int slot, int func, - int idx, uint64_t addr, uint64_t msg, uint32_t vector_control) -{ - struct pptdev *ppt; - struct pci_devinfo *dinfo; - int numvec, alloced, rid, error; - size_t res_size, cookie_size, arg_size; - - ppt = ppt_find(bus, slot, func); - if (ppt == NULL) - return (ENOENT); - if (ppt->vm != vm) /* Make sure we own this device */ - return (EBUSY); - - dinfo = device_get_ivars(ppt->dev); - if (!dinfo) - return (ENXIO); - - /* - * First-time configuration: - * Allocate the MSI-X table - * Allocate the IRQ resources - * Set up some variables in ppt->msix - */ - if (ppt->msix.num_msgs == 0) { - numvec = pci_msix_count(ppt->dev); - if (numvec <= 0) - return (EINVAL); - - ppt->msix.startrid = 1; - ppt->msix.num_msgs = numvec; - - res_size = numvec * sizeof(ppt->msix.res[0]); - cookie_size = numvec * sizeof(ppt->msix.cookie[0]); - arg_size = numvec * sizeof(ppt->msix.arg[0]); - - ppt->msix.res = malloc(res_size, M_PPTMSIX, M_WAITOK | M_ZERO); - ppt->msix.cookie = malloc(cookie_size, M_PPTMSIX, - M_WAITOK | M_ZERO); - ppt->msix.arg = malloc(arg_size, M_PPTMSIX, M_WAITOK | M_ZERO); - - rid = dinfo->cfg.msix.msix_table_bar; - ppt->msix.msix_table_res = bus_alloc_resource_any(ppt->dev, - SYS_RES_MEMORY, &rid, RF_ACTIVE); - - if (ppt->msix.msix_table_res == NULL) { - ppt_teardown_msix(ppt); - return (ENOSPC); - } - ppt->msix.msix_table_rid = rid; - - alloced = numvec; - error = pci_alloc_msix(ppt->dev, &alloced); - if (error || alloced != numvec) { - ppt_teardown_msix(ppt); - return (error == 0 ? ENOSPC: error); - } - } - - if ((vector_control & PCIM_MSIX_VCTRL_MASK) == 0) { - /* Tear down the IRQ if it's already set up */ - ppt_teardown_msix_intr(ppt, idx); - - /* Allocate the IRQ resource */ - ppt->msix.cookie[idx] = NULL; - rid = ppt->msix.startrid + idx; - ppt->msix.res[idx] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ, - &rid, RF_ACTIVE); - if (ppt->msix.res[idx] == NULL) - return (ENXIO); - - ppt->msix.arg[idx].pptdev = ppt; - ppt->msix.arg[idx].addr = addr; - ppt->msix.arg[idx].msg_data = msg; - - /* Setup the MSI-X interrupt */ - error = bus_setup_intr(ppt->dev, ppt->msix.res[idx], - INTR_TYPE_NET | INTR_MPSAFE, - pptintr, NULL, &ppt->msix.arg[idx], - &ppt->msix.cookie[idx]); - - if (error != 0) { - bus_teardown_intr(ppt->dev, ppt->msix.res[idx], ppt->msix.cookie[idx]); - bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, ppt->msix.res[idx]); - ppt->msix.cookie[idx] = NULL; - ppt->msix.res[idx] = NULL; - return (ENXIO); - } - } else { - /* Masked, tear it down if it's already been set up */ - ppt_teardown_msix_intr(ppt, idx); - } - - return (0); -} diff --git a/vmm/vmm_dev.c b/vmm/vmm_dev.c deleted file mode 100644 index e3e140a..0000000 --- a/vmm/vmm_dev.c +++ /dev/null @@ -1,689 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include - -#include "vmm_lapic.h" -#include "vmm_stat.h" -#include "vmm_mem.h" -#include "io/ppt.h" -#include "io/vatpic.h" -#include "io/vioapic.h" -#include "io/vhpet.h" -#include "io/vrtc.h" - -struct vmmdev_softc { - struct vm *vm; /* vm instance cookie */ - struct cdev *cdev; - SLIST_ENTRY(vmmdev_softc) link; - int flags; -}; -#define VSC_LINKED 0x01 - -static SLIST_HEAD(, vmmdev_softc) head; - -static struct mtx vmmdev_mtx; - -static MALLOC_DEFINE(M_VMMDEV, "vmmdev", "vmmdev"); - -SYSCTL_DECL(_hw_vmm); - -static struct vmmdev_softc * -vmmdev_lookup(const char *name) -{ - struct vmmdev_softc *sc; - -#ifdef notyet /* XXX kernel is not compiled with invariants */ - mtx_assert(&vmmdev_mtx, MA_OWNED); -#endif - - SLIST_FOREACH(sc, &head, link) { - if (strcmp(name, vm_name(sc->vm)) == 0) - break; - } - - return (sc); -} - -static struct vmmdev_softc * -vmmdev_lookup2(struct cdev *cdev) -{ - - return (cdev->si_drv1); -} - -static int -vmmdev_rw(struct cdev *cdev, struct uio *uio, int flags) -{ - int error, off, c, prot; - vm_paddr_t gpa; - void *hpa, *cookie; - struct vmmdev_softc *sc; - - static char zerobuf[PAGE_SIZE]; - - error = 0; - sc = vmmdev_lookup2(cdev); - if (sc == NULL) - error = ENXIO; - - prot = (uio->uio_rw == UIO_WRITE ? VM_PROT_WRITE : VM_PROT_READ); - while (uio->uio_resid > 0 && error == 0) { - gpa = uio->uio_offset; - off = gpa & PAGE_MASK; - c = min(uio->uio_resid, PAGE_SIZE - off); - - /* - * The VM has a hole in its physical memory map. If we want to - * use 'dd' to inspect memory beyond the hole we need to - * provide bogus data for memory that lies in the hole. - * - * Since this device does not support lseek(2), dd(1) will - * read(2) blocks of data to simulate the lseek(2). - */ - hpa = vm_gpa_hold(sc->vm, gpa, c, prot, &cookie); - if (hpa == NULL) { - if (uio->uio_rw == UIO_READ) - error = uiomove(zerobuf, c, uio); - else - error = EFAULT; - } else { - error = uiomove(hpa, c, uio); - vm_gpa_release(cookie); - } - } - return (error); -} - -static int -vmmdev_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag, - struct thread *td) -{ - int error, vcpu, state_changed, size; - cpuset_t *cpuset; - struct vmmdev_softc *sc; - struct vm_memory_segment *seg; - struct vm_register *vmreg; - struct vm_seg_desc *vmsegdesc; - struct vm_run *vmrun; - struct vm_exception *vmexc; - struct vm_lapic_irq *vmirq; - struct vm_lapic_msi *vmmsi; - struct vm_ioapic_irq *ioapic_irq; - struct vm_isa_irq *isa_irq; - struct vm_isa_irq_trigger *isa_irq_trigger; - struct vm_capability *vmcap; - struct vm_pptdev *pptdev; - struct vm_pptdev_mmio *pptmmio; - struct vm_pptdev_msi *pptmsi; - struct vm_pptdev_msix *pptmsix; - struct vm_nmi *vmnmi; - struct vm_stats *vmstats; - struct vm_stat_desc *statdesc; - struct vm_x2apic *x2apic; - struct vm_gpa_pte *gpapte; - struct vm_suspend *vmsuspend; - struct vm_gla2gpa *gg; - struct vm_activate_cpu *vac; - struct vm_cpuset *vm_cpuset; - struct vm_intinfo *vmii; - struct vm_rtc_time *rtctime; - struct vm_rtc_data *rtcdata; - - sc = vmmdev_lookup2(cdev); - if (sc == NULL) - return (ENXIO); - - error = 0; - vcpu = -1; - state_changed = 0; - - /* - * Some VMM ioctls can operate only on vcpus that are not running. - */ - switch (cmd) { - case VM_RUN: - case VM_GET_REGISTER: - case VM_SET_REGISTER: - case VM_GET_SEGMENT_DESCRIPTOR: - case VM_SET_SEGMENT_DESCRIPTOR: - case VM_INJECT_EXCEPTION: - case VM_GET_CAPABILITY: - case VM_SET_CAPABILITY: - case VM_PPTDEV_MSI: - case VM_PPTDEV_MSIX: - case VM_SET_X2APIC_STATE: - case VM_GLA2GPA: - case VM_ACTIVATE_CPU: - case VM_SET_INTINFO: - case VM_GET_INTINFO: - case VM_RESTART_INSTRUCTION: - /* - * XXX fragile, handle with care - * Assumes that the first field of the ioctl data is the vcpu. - */ - vcpu = *(int *)data; - if (vcpu < 0 || vcpu >= VM_MAXCPU) { - error = EINVAL; - goto done; - } - - error = vcpu_set_state(sc->vm, vcpu, VCPU_FROZEN, true); - if (error) - goto done; - - state_changed = 1; - break; - - case VM_MAP_PPTDEV_MMIO: - case VM_BIND_PPTDEV: - case VM_UNBIND_PPTDEV: - case VM_MAP_MEMORY: - case VM_REINIT: - /* - * ioctls that operate on the entire virtual machine must - * prevent all vcpus from running. - */ - error = 0; - for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++) { - error = vcpu_set_state(sc->vm, vcpu, VCPU_FROZEN, true); - if (error) - break; - } - - if (error) { - while (--vcpu >= 0) - vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false); - goto done; - } - - state_changed = 2; - break; - - default: - break; - } - - switch(cmd) { - case VM_RUN: - vmrun = (struct vm_run *)data; - error = vm_run(sc->vm, vmrun); - break; - case VM_SUSPEND: - vmsuspend = (struct vm_suspend *)data; - error = vm_suspend(sc->vm, vmsuspend->how); - break; - case VM_REINIT: - error = vm_reinit(sc->vm); - break; - case VM_STAT_DESC: { - statdesc = (struct vm_stat_desc *)data; - error = vmm_stat_desc_copy(statdesc->index, - statdesc->desc, sizeof(statdesc->desc)); - break; - } - case VM_STATS: { - CTASSERT(MAX_VM_STATS >= MAX_VMM_STAT_ELEMS); - vmstats = (struct vm_stats *)data; - getmicrotime(&vmstats->tv); - error = vmm_stat_copy(sc->vm, vmstats->cpuid, - &vmstats->num_entries, vmstats->statbuf); - break; - } - case VM_PPTDEV_MSI: - pptmsi = (struct vm_pptdev_msi *)data; - error = ppt_setup_msi(sc->vm, pptmsi->vcpu, - pptmsi->bus, pptmsi->slot, pptmsi->func, - pptmsi->addr, pptmsi->msg, - pptmsi->numvec); - break; - case VM_PPTDEV_MSIX: - pptmsix = (struct vm_pptdev_msix *)data; - error = ppt_setup_msix(sc->vm, pptmsix->vcpu, - pptmsix->bus, pptmsix->slot, - pptmsix->func, pptmsix->idx, - pptmsix->addr, pptmsix->msg, - pptmsix->vector_control); - break; - case VM_MAP_PPTDEV_MMIO: - pptmmio = (struct vm_pptdev_mmio *)data; - error = ppt_map_mmio(sc->vm, pptmmio->bus, pptmmio->slot, - pptmmio->func, pptmmio->gpa, pptmmio->len, - pptmmio->hpa); - break; - case VM_BIND_PPTDEV: - pptdev = (struct vm_pptdev *)data; - error = vm_assign_pptdev(sc->vm, pptdev->bus, pptdev->slot, - pptdev->func); - break; - case VM_UNBIND_PPTDEV: - pptdev = (struct vm_pptdev *)data; - error = vm_unassign_pptdev(sc->vm, pptdev->bus, pptdev->slot, - pptdev->func); - break; - case VM_INJECT_EXCEPTION: - vmexc = (struct vm_exception *)data; - error = vm_inject_exception(sc->vm, vmexc->cpuid, - vmexc->vector, vmexc->error_code_valid, vmexc->error_code, - vmexc->restart_instruction); - break; - case VM_INJECT_NMI: - vmnmi = (struct vm_nmi *)data; - error = vm_inject_nmi(sc->vm, vmnmi->cpuid); - break; - case VM_LAPIC_IRQ: - vmirq = (struct vm_lapic_irq *)data; - error = lapic_intr_edge(sc->vm, vmirq->cpuid, vmirq->vector); - break; - case VM_LAPIC_LOCAL_IRQ: - vmirq = (struct vm_lapic_irq *)data; - error = lapic_set_local_intr(sc->vm, vmirq->cpuid, - vmirq->vector); - break; - case VM_LAPIC_MSI: - vmmsi = (struct vm_lapic_msi *)data; - error = lapic_intr_msi(sc->vm, vmmsi->addr, vmmsi->msg); - break; - case VM_IOAPIC_ASSERT_IRQ: - ioapic_irq = (struct vm_ioapic_irq *)data; - error = vioapic_assert_irq(sc->vm, ioapic_irq->irq); - break; - case VM_IOAPIC_DEASSERT_IRQ: - ioapic_irq = (struct vm_ioapic_irq *)data; - error = vioapic_deassert_irq(sc->vm, ioapic_irq->irq); - break; - case VM_IOAPIC_PULSE_IRQ: - ioapic_irq = (struct vm_ioapic_irq *)data; - error = vioapic_pulse_irq(sc->vm, ioapic_irq->irq); - break; - case VM_IOAPIC_PINCOUNT: - *(int *)data = vioapic_pincount(sc->vm); - break; - case VM_ISA_ASSERT_IRQ: - isa_irq = (struct vm_isa_irq *)data; - error = vatpic_assert_irq(sc->vm, isa_irq->atpic_irq); - if (error == 0 && isa_irq->ioapic_irq != -1) - error = vioapic_assert_irq(sc->vm, - isa_irq->ioapic_irq); - break; - case VM_ISA_DEASSERT_IRQ: - isa_irq = (struct vm_isa_irq *)data; - error = vatpic_deassert_irq(sc->vm, isa_irq->atpic_irq); - if (error == 0 && isa_irq->ioapic_irq != -1) - error = vioapic_deassert_irq(sc->vm, - isa_irq->ioapic_irq); - break; - case VM_ISA_PULSE_IRQ: - isa_irq = (struct vm_isa_irq *)data; - error = vatpic_pulse_irq(sc->vm, isa_irq->atpic_irq); - if (error == 0 && isa_irq->ioapic_irq != -1) - error = vioapic_pulse_irq(sc->vm, isa_irq->ioapic_irq); - break; - case VM_ISA_SET_IRQ_TRIGGER: - isa_irq_trigger = (struct vm_isa_irq_trigger *)data; - error = vatpic_set_irq_trigger(sc->vm, - isa_irq_trigger->atpic_irq, isa_irq_trigger->trigger); - break; - case VM_MAP_MEMORY: - seg = (struct vm_memory_segment *)data; - error = vm_malloc(sc->vm, seg->gpa, seg->len); - break; - case VM_GET_MEMORY_SEG: - seg = (struct vm_memory_segment *)data; - seg->len = 0; - (void)vm_gpabase2memseg(sc->vm, seg->gpa, seg); - error = 0; - break; - case VM_GET_REGISTER: - vmreg = (struct vm_register *)data; - error = vm_get_register(sc->vm, vmreg->cpuid, vmreg->regnum, - &vmreg->regval); - break; - case VM_SET_REGISTER: - vmreg = (struct vm_register *)data; - error = vm_set_register(sc->vm, vmreg->cpuid, vmreg->regnum, - vmreg->regval); - break; - case VM_SET_SEGMENT_DESCRIPTOR: - vmsegdesc = (struct vm_seg_desc *)data; - error = vm_set_seg_desc(sc->vm, vmsegdesc->cpuid, - vmsegdesc->regnum, - &vmsegdesc->desc); - break; - case VM_GET_SEGMENT_DESCRIPTOR: - vmsegdesc = (struct vm_seg_desc *)data; - error = vm_get_seg_desc(sc->vm, vmsegdesc->cpuid, - vmsegdesc->regnum, - &vmsegdesc->desc); - break; - case VM_GET_CAPABILITY: - vmcap = (struct vm_capability *)data; - error = vm_get_capability(sc->vm, vmcap->cpuid, - vmcap->captype, - &vmcap->capval); - break; - case VM_SET_CAPABILITY: - vmcap = (struct vm_capability *)data; - error = vm_set_capability(sc->vm, vmcap->cpuid, - vmcap->captype, - vmcap->capval); - break; - case VM_SET_X2APIC_STATE: - x2apic = (struct vm_x2apic *)data; - error = vm_set_x2apic_state(sc->vm, - x2apic->cpuid, x2apic->state); - break; - case VM_GET_X2APIC_STATE: - x2apic = (struct vm_x2apic *)data; - error = vm_get_x2apic_state(sc->vm, - x2apic->cpuid, &x2apic->state); - break; - case VM_GET_GPA_PMAP: - gpapte = (struct vm_gpa_pte *)data; - pmap_get_mapping(vmspace_pmap(vm_get_vmspace(sc->vm)), - gpapte->gpa, gpapte->pte, &gpapte->ptenum); - error = 0; - break; - case VM_GET_HPET_CAPABILITIES: - error = vhpet_getcap((struct vm_hpet_cap *)data); - break; - case VM_GLA2GPA: { - CTASSERT(PROT_READ == VM_PROT_READ); - CTASSERT(PROT_WRITE == VM_PROT_WRITE); - CTASSERT(PROT_EXEC == VM_PROT_EXECUTE); - gg = (struct vm_gla2gpa *)data; - error = vm_gla2gpa(sc->vm, gg->vcpuid, &gg->paging, gg->gla, - gg->prot, &gg->gpa, &gg->fault); - KASSERT(error == 0 || error == EFAULT, - ("%s: vm_gla2gpa unknown error %d", __func__, error)); - break; - } - case VM_ACTIVATE_CPU: - vac = (struct vm_activate_cpu *)data; - error = vm_activate_cpu(sc->vm, vac->vcpuid); - break; - case VM_GET_CPUS: - error = 0; - vm_cpuset = (struct vm_cpuset *)data; - size = vm_cpuset->cpusetsize; - if (size < sizeof(cpuset_t) || size > CPU_MAXSIZE / NBBY) { - error = ERANGE; - break; - } - cpuset = malloc(size, M_TEMP, M_WAITOK | M_ZERO); - if (vm_cpuset->which == VM_ACTIVE_CPUS) - *cpuset = vm_active_cpus(sc->vm); - else if (vm_cpuset->which == VM_SUSPENDED_CPUS) - *cpuset = vm_suspended_cpus(sc->vm); - else - error = EINVAL; - if (error == 0) - error = copyout(cpuset, vm_cpuset->cpus, size); - free(cpuset, M_TEMP); - break; - case VM_SET_INTINFO: - vmii = (struct vm_intinfo *)data; - error = vm_exit_intinfo(sc->vm, vmii->vcpuid, vmii->info1); - break; - case VM_GET_INTINFO: - vmii = (struct vm_intinfo *)data; - error = vm_get_intinfo(sc->vm, vmii->vcpuid, &vmii->info1, - &vmii->info2); - break; - case VM_RTC_WRITE: - rtcdata = (struct vm_rtc_data *)data; - error = vrtc_nvram_write(sc->vm, rtcdata->offset, - rtcdata->value); - break; - case VM_RTC_READ: - rtcdata = (struct vm_rtc_data *)data; - error = vrtc_nvram_read(sc->vm, rtcdata->offset, - &rtcdata->value); - break; - case VM_RTC_SETTIME: - rtctime = (struct vm_rtc_time *)data; - error = vrtc_set_time(sc->vm, rtctime->secs); - break; - case VM_RTC_GETTIME: - error = 0; - rtctime = (struct vm_rtc_time *)data; - rtctime->secs = vrtc_get_time(sc->vm); - break; - case VM_RESTART_INSTRUCTION: - error = vm_restart_instruction(sc->vm, vcpu); - break; - default: - error = ENOTTY; - break; - } - - if (state_changed == 1) { - vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false); - } else if (state_changed == 2) { - for (vcpu = 0; vcpu < VM_MAXCPU; vcpu++) - vcpu_set_state(sc->vm, vcpu, VCPU_IDLE, false); - } - -done: - /* Make sure that no handler returns a bogus value like ERESTART */ - KASSERT(error >= 0, ("vmmdev_ioctl: invalid error return %d", error)); - return (error); -} - -static int -vmmdev_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, - vm_size_t size, struct vm_object **object, int nprot) -{ - int error; - struct vmmdev_softc *sc; - - sc = vmmdev_lookup2(cdev); - if (sc != NULL && (nprot & PROT_EXEC) == 0) - error = vm_get_memobj(sc->vm, *offset, size, offset, object); - else - error = EINVAL; - - return (error); -} - -static void -vmmdev_destroy(void *arg) -{ - - struct vmmdev_softc *sc = arg; - - if (sc->cdev != NULL) - destroy_dev(sc->cdev); - - if (sc->vm != NULL) - vm_destroy(sc->vm); - - if ((sc->flags & VSC_LINKED) != 0) { - mtx_lock(&vmmdev_mtx); - SLIST_REMOVE(&head, sc, vmmdev_softc, link); - mtx_unlock(&vmmdev_mtx); - } - - free(sc, M_VMMDEV); -} - -static int -sysctl_vmm_destroy(SYSCTL_HANDLER_ARGS) -{ - int error; - char buf[VM_MAX_NAMELEN]; - struct vmmdev_softc *sc; - struct cdev *cdev; - - strlcpy(buf, "beavis", sizeof(buf)); - error = sysctl_handle_string(oidp, buf, sizeof(buf), req); - if (error != 0 || req->newptr == NULL) - return (error); - - mtx_lock(&vmmdev_mtx); - sc = vmmdev_lookup(buf); - if (sc == NULL || sc->cdev == NULL) { - mtx_unlock(&vmmdev_mtx); - return (EINVAL); - } - - /* - * The 'cdev' will be destroyed asynchronously when 'si_threadcount' - * goes down to 0 so we should not do it again in the callback. - */ - cdev = sc->cdev; - sc->cdev = NULL; - mtx_unlock(&vmmdev_mtx); - - /* - * Schedule the 'cdev' to be destroyed: - * - * - any new operations on this 'cdev' will return an error (ENXIO). - * - * - when the 'si_threadcount' dwindles down to zero the 'cdev' will - * be destroyed and the callback will be invoked in a taskqueue - * context. - */ - destroy_dev_sched_cb(cdev, vmmdev_destroy, sc); - - return (0); -} -SYSCTL_PROC(_hw_vmm, OID_AUTO, destroy, CTLTYPE_STRING | CTLFLAG_RW, - NULL, 0, sysctl_vmm_destroy, "A", NULL); - -static struct cdevsw vmmdevsw = { - .d_name = "vmmdev", - .d_version = D_VERSION, - .d_ioctl = vmmdev_ioctl, - .d_mmap_single = vmmdev_mmap_single, - .d_read = vmmdev_rw, - .d_write = vmmdev_rw, -}; - -static int -sysctl_vmm_create(SYSCTL_HANDLER_ARGS) -{ - int error; - struct vm *vm; - struct cdev *cdev; - struct vmmdev_softc *sc, *sc2; - char buf[VM_MAX_NAMELEN]; - - strlcpy(buf, "beavis", sizeof(buf)); - error = sysctl_handle_string(oidp, buf, sizeof(buf), req); - if (error != 0 || req->newptr == NULL) - return (error); - - mtx_lock(&vmmdev_mtx); - sc = vmmdev_lookup(buf); - mtx_unlock(&vmmdev_mtx); - if (sc != NULL) - return (EEXIST); - - error = vm_create(buf, &vm); - if (error != 0) - return (error); - - sc = malloc(sizeof(struct vmmdev_softc), M_VMMDEV, M_WAITOK | M_ZERO); - sc->vm = vm; - - /* - * Lookup the name again just in case somebody sneaked in when we - * dropped the lock. - */ - mtx_lock(&vmmdev_mtx); - sc2 = vmmdev_lookup(buf); - if (sc2 == NULL) { - SLIST_INSERT_HEAD(&head, sc, link); - sc->flags |= VSC_LINKED; - } - mtx_unlock(&vmmdev_mtx); - - if (sc2 != NULL) { - vmmdev_destroy(sc); - return (EEXIST); - } - - error = make_dev_p(MAKEDEV_CHECKNAME, &cdev, &vmmdevsw, NULL, - UID_ROOT, GID_WHEEL, 0600, "vmm/%s", buf); - if (error != 0) { - vmmdev_destroy(sc); - return (error); - } - - mtx_lock(&vmmdev_mtx); - sc->cdev = cdev; - sc->cdev->si_drv1 = sc; - mtx_unlock(&vmmdev_mtx); - - return (0); -} -SYSCTL_PROC(_hw_vmm, OID_AUTO, create, CTLTYPE_STRING | CTLFLAG_RW, - NULL, 0, sysctl_vmm_create, "A", NULL); - -void -vmmdev_init(void) -{ - mtx_init(&vmmdev_mtx, "vmm device mutex", NULL, MTX_DEF); -} - -int -vmmdev_cleanup(void) -{ - int error; - - if (SLIST_EMPTY(&head)) - error = 0; - else - error = EBUSY; - - return (error); -} diff --git a/vmm/vmm_host.c b/vmm/vmm_host.c deleted file mode 100644 index 9e5b966..0000000 --- a/vmm/vmm_host.c +++ /dev/null @@ -1,161 +0,0 @@ -/*- - * Copyright (c) 2012 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include - -#include -#include -#include - -#include "vmm_host.h" - -static uint64_t vmm_host_efer, vmm_host_pat, vmm_host_cr0, vmm_host_cr4, - vmm_host_xcr0; -static struct xsave_limits vmm_xsave_limits; - -void -vmm_host_state_init(void) -{ - int regs[4]; - - vmm_host_efer = rdmsr(MSR_EFER); - vmm_host_pat = rdmsr(MSR_PAT); - - /* - * We always want CR0.TS to be set when the processor does a VM exit. - * - * With emulation turned on unconditionally after a VM exit, we are - * able to trap inadvertent use of the FPU until the guest FPU state - * has been safely squirreled away. - */ - vmm_host_cr0 = rcr0() | CR0_TS; - - vmm_host_cr4 = rcr4(); - - /* - * Only permit a guest to use XSAVE if the host is using - * XSAVE. Only permit a guest to use XSAVE features supported - * by the host. This ensures that the FPU state used by the - * guest is always a subset of the saved guest FPU state. - * - * In addition, only permit known XSAVE features where the - * rules for which features depend on other features is known - * to properly emulate xsetbv. - */ - if (vmm_host_cr4 & CR4_XSAVE) { - vmm_xsave_limits.xsave_enabled = 1; - vmm_host_xcr0 = rxcr(0); - vmm_xsave_limits.xcr0_allowed = vmm_host_xcr0 & - (XFEATURE_AVX | XFEATURE_MPX | XFEATURE_AVX512); - - cpuid_count(0xd, 0x0, regs); - vmm_xsave_limits.xsave_max_size = regs[1]; - } -} - -uint64_t -vmm_get_host_pat(void) -{ - - return (vmm_host_pat); -} - -uint64_t -vmm_get_host_efer(void) -{ - - return (vmm_host_efer); -} - -uint64_t -vmm_get_host_cr0(void) -{ - - return (vmm_host_cr0); -} - -uint64_t -vmm_get_host_cr4(void) -{ - - return (vmm_host_cr4); -} - -uint64_t -vmm_get_host_xcr0(void) -{ - - return (vmm_host_xcr0); -} - -uint64_t -vmm_get_host_datasel(void) -{ - - return (GSEL(GDATA_SEL, SEL_KPL)); - -} - -uint64_t -vmm_get_host_codesel(void) -{ - - return (GSEL(GCODE_SEL, SEL_KPL)); -} - -uint64_t -vmm_get_host_tsssel(void) -{ - - return (GSEL(GPROC0_SEL, SEL_KPL)); -} - -uint64_t -vmm_get_host_fsbase(void) -{ - - return (0); -} - -uint64_t -vmm_get_host_idtrbase(void) -{ - - return (r_idt.rd_base); -} - -const struct xsave_limits * -vmm_get_xsave_limits(void) -{ - - return (&vmm_xsave_limits); -} diff --git a/vmm/vmm_host.h b/vmm/vmm_host.h deleted file mode 100644 index 95618ff..0000000 --- a/vmm/vmm_host.h +++ /dev/null @@ -1,83 +0,0 @@ -/*- - * Copyright (c) 2012 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#ifndef _VMM_HOST_H_ -#define _VMM_HOST_H_ - -#ifndef _KERNEL -#error "no user-servicable parts inside" -#endif - -struct xsave_limits { - int xsave_enabled; - uint64_t xcr0_allowed; - uint32_t xsave_max_size; -}; - -void vmm_host_state_init(void); - -uint64_t vmm_get_host_pat(void); -uint64_t vmm_get_host_efer(void); -uint64_t vmm_get_host_cr0(void); -uint64_t vmm_get_host_cr4(void); -uint64_t vmm_get_host_xcr0(void); -uint64_t vmm_get_host_datasel(void); -uint64_t vmm_get_host_codesel(void); -uint64_t vmm_get_host_tsssel(void); -uint64_t vmm_get_host_fsbase(void); -uint64_t vmm_get_host_idtrbase(void); -const struct xsave_limits *vmm_get_xsave_limits(void); - -/* - * Inline access to host state that is used on every VM entry - */ -static __inline uint64_t -vmm_get_host_trbase(void) -{ - - return ((uint64_t)PCPU_GET(tssp)); -} - -static __inline uint64_t -vmm_get_host_gdtrbase(void) -{ - - return ((uint64_t)&gdt[NGDT * curcpu]); -} - -struct pcpu; -extern struct pcpu __pcpu[]; - -static __inline uint64_t -vmm_get_host_gsbase(void) -{ - - return ((uint64_t)&__pcpu[curcpu]); -} - -#endif diff --git a/vmm/vmm_mem.c b/vmm/vmm_mem.c deleted file mode 100644 index 1019f2b..0000000 --- a/vmm/vmm_mem.c +++ /dev/null @@ -1,154 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include -__FBSDID("$FreeBSD$"); - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "vmm_mem.h" - -int -vmm_mem_init(void) -{ - - return (0); -} - -vm_object_t -vmm_mmio_alloc(struct vmspace *vmspace, vm_paddr_t gpa, size_t len, - vm_paddr_t hpa) -{ - int error; - vm_object_t obj; - struct sglist *sg; - - sg = sglist_alloc(1, M_WAITOK); - error = sglist_append_phys(sg, hpa, len); - KASSERT(error == 0, ("error %d appending physaddr to sglist", error)); - - obj = vm_pager_allocate(OBJT_SG, sg, len, VM_PROT_RW, 0, NULL); - if (obj != NULL) { - /* - * VT-x ignores the MTRR settings when figuring out the - * memory type for translations obtained through EPT. - * - * Therefore we explicitly force the pages provided by - * this object to be mapped as uncacheable. - */ - VM_OBJECT_WLOCK(obj); - error = vm_object_set_memattr(obj, VM_MEMATTR_UNCACHEABLE); - VM_OBJECT_WUNLOCK(obj); - if (error != KERN_SUCCESS) { - panic("vmm_mmio_alloc: vm_object_set_memattr error %d", - error); - } - error = vm_map_find(&vmspace->vm_map, obj, 0, &gpa, len, 0, - VMFS_NO_SPACE, VM_PROT_RW, VM_PROT_RW, 0); - if (error != KERN_SUCCESS) { - vm_object_deallocate(obj); - obj = NULL; - } - } - - /* - * Drop the reference on the sglist. - * - * If the scatter/gather object was successfully allocated then it - * has incremented the reference count on the sglist. Dropping the - * initial reference count ensures that the sglist will be freed - * when the object is deallocated. - * - * If the object could not be allocated then we end up freeing the - * sglist. - */ - sglist_free(sg); - - return (obj); -} - -void -vmm_mmio_free(struct vmspace *vmspace, vm_paddr_t gpa, size_t len) -{ - - vm_map_remove(&vmspace->vm_map, gpa, gpa + len); -} - -vm_object_t -vmm_mem_alloc(struct vmspace *vmspace, vm_paddr_t gpa, size_t len) -{ - int error; - vm_object_t obj; - - if (gpa & PAGE_MASK) - panic("vmm_mem_alloc: invalid gpa %#lx", gpa); - - if (len == 0 || (len & PAGE_MASK) != 0) - panic("vmm_mem_alloc: invalid allocation size %lu", len); - - obj = vm_object_allocate(OBJT_DEFAULT, len >> PAGE_SHIFT); - if (obj != NULL) { - error = vm_map_find(&vmspace->vm_map, obj, 0, &gpa, len, 0, - VMFS_NO_SPACE, VM_PROT_ALL, VM_PROT_ALL, 0); - if (error != KERN_SUCCESS) { - vm_object_deallocate(obj); - obj = NULL; - } - } - - return (obj); -} - -void -vmm_mem_free(struct vmspace *vmspace, vm_paddr_t gpa, size_t len) -{ - - vm_map_remove(&vmspace->vm_map, gpa, gpa + len); -} - -vm_paddr_t -vmm_mem_maxaddr(void) -{ - - return (ptoa(Maxmem)); -} diff --git a/vmm_dev.h b/vmm_dev.h deleted file mode 100644 index 9d031a9..0000000 --- a/vmm_dev.h +++ /dev/null @@ -1,365 +0,0 @@ -/*- - * Copyright (c) 2011 NetApp, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#ifndef _VMM_DEV_H_ -#define _VMM_DEV_H_ - -#ifdef _KERNEL -void vmmdev_init(void); -int vmmdev_cleanup(void); -#endif - -struct vm_memory_segment { - vm_paddr_t gpa; /* in */ - size_t len; - int wired; -}; - -struct vm_register { - int cpuid; - int regnum; /* enum vm_reg_name */ - uint64_t regval; -}; - -struct vm_seg_desc { /* data or code segment */ - int cpuid; - int regnum; /* enum vm_reg_name */ - struct seg_desc desc; -}; - -struct vm_run { - int cpuid; - struct vm_exit vm_exit; -}; - -struct vm_exception { - int cpuid; - int vector; - uint32_t error_code; - int error_code_valid; - int restart_instruction; -}; - -struct vm_lapic_msi { - uint64_t msg; - uint64_t addr; -}; - -struct vm_lapic_irq { - int cpuid; - int vector; -}; - -struct vm_ioapic_irq { - int irq; -}; - -struct vm_isa_irq { - int atpic_irq; - int ioapic_irq; -}; - -struct vm_isa_irq_trigger { - int atpic_irq; - enum vm_intr_trigger trigger; -}; - -struct vm_capability { - int cpuid; - enum vm_cap_type captype; - int capval; - int allcpus; -}; - -struct vm_pptdev { - int bus; - int slot; - int func; -}; - -struct vm_pptdev_mmio { - int bus; - int slot; - int func; - vm_paddr_t gpa; - vm_paddr_t hpa; - size_t len; -}; - -struct vm_pptdev_msi { - int vcpu; - int bus; - int slot; - int func; - int numvec; /* 0 means disabled */ - uint64_t msg; - uint64_t addr; -}; - -struct vm_pptdev_msix { - int vcpu; - int bus; - int slot; - int func; - int idx; - uint64_t msg; - uint32_t vector_control; - uint64_t addr; -}; - -struct vm_nmi { - int cpuid; -}; - -#define MAX_VM_STATS 64 -struct vm_stats { - int cpuid; /* in */ - int num_entries; /* out */ - struct timeval tv; - uint64_t statbuf[MAX_VM_STATS]; -}; - -struct vm_stat_desc { - int index; /* in */ - char desc[128]; /* out */ -}; - -struct vm_x2apic { - int cpuid; - enum x2apic_state state; -}; - -struct vm_gpa_pte { - uint64_t gpa; /* in */ - uint64_t pte[4]; /* out */ - int ptenum; -}; - -struct vm_hpet_cap { - uint32_t capabilities; /* lower 32 bits of HPET capabilities */ -}; - -struct vm_suspend { - enum vm_suspend_how how; -}; - -struct vm_gla2gpa { - int vcpuid; /* inputs */ - int prot; /* PROT_READ or PROT_WRITE */ - uint64_t gla; - struct vm_guest_paging paging; - int fault; /* outputs */ - uint64_t gpa; -}; - -struct vm_activate_cpu { - int vcpuid; -}; - -struct vm_cpuset { - int which; - int cpusetsize; - cpuset_t *cpus; -}; -#define VM_ACTIVE_CPUS 0 -#define VM_SUSPENDED_CPUS 1 - -struct vm_intinfo { - int vcpuid; - uint64_t info1; - uint64_t info2; -}; - -struct vm_rtc_time { - time_t secs; -}; - -struct vm_rtc_data { - int offset; - uint8_t value; -}; - -enum { - /* general routines */ - IOCNUM_ABIVERS = 0, - IOCNUM_RUN = 1, - IOCNUM_SET_CAPABILITY = 2, - IOCNUM_GET_CAPABILITY = 3, - IOCNUM_SUSPEND = 4, - IOCNUM_REINIT = 5, - - /* memory apis */ - IOCNUM_MAP_MEMORY = 10, - IOCNUM_GET_MEMORY_SEG = 11, - IOCNUM_GET_GPA_PMAP = 12, - IOCNUM_GLA2GPA = 13, - - /* register/state accessors */ - IOCNUM_SET_REGISTER = 20, - IOCNUM_GET_REGISTER = 21, - IOCNUM_SET_SEGMENT_DESCRIPTOR = 22, - IOCNUM_GET_SEGMENT_DESCRIPTOR = 23, - - /* interrupt injection */ - IOCNUM_GET_INTINFO = 28, - IOCNUM_SET_INTINFO = 29, - IOCNUM_INJECT_EXCEPTION = 30, - IOCNUM_LAPIC_IRQ = 31, - IOCNUM_INJECT_NMI = 32, - IOCNUM_IOAPIC_ASSERT_IRQ = 33, - IOCNUM_IOAPIC_DEASSERT_IRQ = 34, - IOCNUM_IOAPIC_PULSE_IRQ = 35, - IOCNUM_LAPIC_MSI = 36, - IOCNUM_LAPIC_LOCAL_IRQ = 37, - IOCNUM_IOAPIC_PINCOUNT = 38, - IOCNUM_RESTART_INSTRUCTION = 39, - - /* PCI pass-thru */ - IOCNUM_BIND_PPTDEV = 40, - IOCNUM_UNBIND_PPTDEV = 41, - IOCNUM_MAP_PPTDEV_MMIO = 42, - IOCNUM_PPTDEV_MSI = 43, - IOCNUM_PPTDEV_MSIX = 44, - - /* statistics */ - IOCNUM_VM_STATS = 50, - IOCNUM_VM_STAT_DESC = 51, - - /* kernel device state */ - IOCNUM_SET_X2APIC_STATE = 60, - IOCNUM_GET_X2APIC_STATE = 61, - IOCNUM_GET_HPET_CAPABILITIES = 62, - - /* legacy interrupt injection */ - IOCNUM_ISA_ASSERT_IRQ = 80, - IOCNUM_ISA_DEASSERT_IRQ = 81, - IOCNUM_ISA_PULSE_IRQ = 82, - IOCNUM_ISA_SET_IRQ_TRIGGER = 83, - - /* vm_cpuset */ - IOCNUM_ACTIVATE_CPU = 90, - IOCNUM_GET_CPUSET = 91, - - /* RTC */ - IOCNUM_RTC_READ = 100, - IOCNUM_RTC_WRITE = 101, - IOCNUM_RTC_SETTIME = 102, - IOCNUM_RTC_GETTIME = 103, -}; - -#define VM_RUN \ - _IOWR('v', IOCNUM_RUN, struct vm_run) -#define VM_SUSPEND \ - _IOW('v', IOCNUM_SUSPEND, struct vm_suspend) -#define VM_REINIT \ - _IO('v', IOCNUM_REINIT) -#define VM_MAP_MEMORY \ - _IOWR('v', IOCNUM_MAP_MEMORY, struct vm_memory_segment) -#define VM_GET_MEMORY_SEG \ - _IOWR('v', IOCNUM_GET_MEMORY_SEG, struct vm_memory_segment) -#define VM_SET_REGISTER \ - _IOW('v', IOCNUM_SET_REGISTER, struct vm_register) -#define VM_GET_REGISTER \ - _IOWR('v', IOCNUM_GET_REGISTER, struct vm_register) -#define VM_SET_SEGMENT_DESCRIPTOR \ - _IOW('v', IOCNUM_SET_SEGMENT_DESCRIPTOR, struct vm_seg_desc) -#define VM_GET_SEGMENT_DESCRIPTOR \ - _IOWR('v', IOCNUM_GET_SEGMENT_DESCRIPTOR, struct vm_seg_desc) -#define VM_INJECT_EXCEPTION \ - _IOW('v', IOCNUM_INJECT_EXCEPTION, struct vm_exception) -#define VM_LAPIC_IRQ \ - _IOW('v', IOCNUM_LAPIC_IRQ, struct vm_lapic_irq) -#define VM_LAPIC_LOCAL_IRQ \ - _IOW('v', IOCNUM_LAPIC_LOCAL_IRQ, struct vm_lapic_irq) -#define VM_LAPIC_MSI \ - _IOW('v', IOCNUM_LAPIC_MSI, struct vm_lapic_msi) -#define VM_IOAPIC_ASSERT_IRQ \ - _IOW('v', IOCNUM_IOAPIC_ASSERT_IRQ, struct vm_ioapic_irq) -#define VM_IOAPIC_DEASSERT_IRQ \ - _IOW('v', IOCNUM_IOAPIC_DEASSERT_IRQ, struct vm_ioapic_irq) -#define VM_IOAPIC_PULSE_IRQ \ - _IOW('v', IOCNUM_IOAPIC_PULSE_IRQ, struct vm_ioapic_irq) -#define VM_IOAPIC_PINCOUNT \ - _IOR('v', IOCNUM_IOAPIC_PINCOUNT, int) -#define VM_ISA_ASSERT_IRQ \ - _IOW('v', IOCNUM_ISA_ASSERT_IRQ, struct vm_isa_irq) -#define VM_ISA_DEASSERT_IRQ \ - _IOW('v', IOCNUM_ISA_DEASSERT_IRQ, struct vm_isa_irq) -#define VM_ISA_PULSE_IRQ \ - _IOW('v', IOCNUM_ISA_PULSE_IRQ, struct vm_isa_irq) -#define VM_ISA_SET_IRQ_TRIGGER \ - _IOW('v', IOCNUM_ISA_SET_IRQ_TRIGGER, struct vm_isa_irq_trigger) -#define VM_SET_CAPABILITY \ - _IOW('v', IOCNUM_SET_CAPABILITY, struct vm_capability) -#define VM_GET_CAPABILITY \ - _IOWR('v', IOCNUM_GET_CAPABILITY, struct vm_capability) -#define VM_BIND_PPTDEV \ - _IOW('v', IOCNUM_BIND_PPTDEV, struct vm_pptdev) -#define VM_UNBIND_PPTDEV \ - _IOW('v', IOCNUM_UNBIND_PPTDEV, struct vm_pptdev) -#define VM_MAP_PPTDEV_MMIO \ - _IOW('v', IOCNUM_MAP_PPTDEV_MMIO, struct vm_pptdev_mmio) -#define VM_PPTDEV_MSI \ - _IOW('v', IOCNUM_PPTDEV_MSI, struct vm_pptdev_msi) -#define VM_PPTDEV_MSIX \ - _IOW('v', IOCNUM_PPTDEV_MSIX, struct vm_pptdev_msix) -#define VM_INJECT_NMI \ - _IOW('v', IOCNUM_INJECT_NMI, struct vm_nmi) -#define VM_STATS \ - _IOWR('v', IOCNUM_VM_STATS, struct vm_stats) -#define VM_STAT_DESC \ - _IOWR('v', IOCNUM_VM_STAT_DESC, struct vm_stat_desc) -#define VM_SET_X2APIC_STATE \ - _IOW('v', IOCNUM_SET_X2APIC_STATE, struct vm_x2apic) -#define VM_GET_X2APIC_STATE \ - _IOWR('v', IOCNUM_GET_X2APIC_STATE, struct vm_x2apic) -#define VM_GET_HPET_CAPABILITIES \ - _IOR('v', IOCNUM_GET_HPET_CAPABILITIES, struct vm_hpet_cap) -#define VM_GET_GPA_PMAP \ - _IOWR('v', IOCNUM_GET_GPA_PMAP, struct vm_gpa_pte) -#define VM_GLA2GPA \ - _IOWR('v', IOCNUM_GLA2GPA, struct vm_gla2gpa) -#define VM_ACTIVATE_CPU \ - _IOW('v', IOCNUM_ACTIVATE_CPU, struct vm_activate_cpu) -#define VM_GET_CPUS \ - _IOW('v', IOCNUM_GET_CPUSET, struct vm_cpuset) -#define VM_SET_INTINFO \ - _IOW('v', IOCNUM_SET_INTINFO, struct vm_intinfo) -#define VM_GET_INTINFO \ - _IOWR('v', IOCNUM_GET_INTINFO, struct vm_intinfo) -#define VM_RTC_WRITE \ - _IOW('v', IOCNUM_RTC_WRITE, struct vm_rtc_data) -#define VM_RTC_READ \ - _IOWR('v', IOCNUM_RTC_READ, struct vm_rtc_data) -#define VM_RTC_SETTIME \ - _IOW('v', IOCNUM_RTC_SETTIME, struct vm_rtc_time) -#define VM_RTC_GETTIME \ - _IOR('v', IOCNUM_RTC_GETTIME, struct vm_rtc_time) -#define VM_RESTART_INSTRUCTION \ - _IOW('v', IOCNUM_RESTART_INSTRUCTION, int) -#endif diff --git a/xhyve_logo.png b/xhyve_logo.png new file mode 100644 index 0000000..d2e56b8 Binary files /dev/null and b/xhyve_logo.png differ diff --git a/xhyverun.sh b/xhyverun.sh new file mode 100755 index 0000000..bb81523 --- /dev/null +++ b/xhyverun.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +KERNEL="test/vmlinuz" +INITRD="test/initrd.gz" +CMDLINE="earlyprintk=serial console=ttyS0 acpi=off" + +MEM="-m 1G" +#SMP="-c 2" +#NET="-s 2:0,virtio-net" +#IMG_CD="-s 3,ahci-cd,/somepath/somefile.iso" +#IMG_HDD="-s 4,virtio-blk,/somepath/somefile.img" +PCI_DEV="-s 0:0,hostbridge -s 31,lpc" +LPC_DEV="-l com1,stdio" + +build/xhyve $MEM $SMP $PCI_DEV $LPC_DEV $NET $IMG_CD $IMG_HDD -f kexec,$KERNEL,$INITRD,"$CMDLINE"