Emulate the 'bit test' instruction. MFC r282259: Re-implement RTC current time calculation to eliminate the possibility of losing time. MFC r282281: Advertise the MTRR feature via CPUID and emulate the minimal set of MTRR MSRs. MFC r282284: When an instruction cannot be decoded just return to userspace so bhyve(8) can dump the instruction bytes. MFC r282287: Don't require <sys/cpuset.h> to be always included before <machine/vmm.h>. MFC r282296: Emulate MSR_SYSCFG which is accessed by Linux on AMD cpus when MTRRs are enabled. MFC r282301: Relax limits when transitioning a vector from the IRR to the ISR and also when extinguishing it from the ISR in response to an EOI. MFC r282335: Advertise an additional memory BAR in the "dummy" device emulation. MFC r282336: Emulate machine check related MSRs to allow guest OSes like Windows to boot. MFC r282351: Don't advertise the Intel SMX capability to the guest. MFC r282407: Emulate the 'CMP r/m8, imm8' instruction. MFC r282519: Add macros for AMD-specific bits in MSR_EFER: LMSLE, FFXSR and TCE. MFC r282520: Emulate guest writes to EFER_MSR properly. MFC r282558: Deprecate the 3-way return values from vm_gla2gpa() and vm_copy_setup(). MFC r282571: Check 'td_owepreempt' and yield the vcpu thread if it is set. MFC r282595: Allow byte reads of AHCI registers. MFC r282784: Handling indirect descriptors is a capability of the host and not one that needs to be negotiated. Use the host capabilities field and not the negotiated field when verifying that indirect descriptors are supported. MFC r282788: Allow configuration of the sector size advertised to the guest. MFC r282865: Set the subvendor field in config space to the vendor ID. This is required by the Windows virtio drivers to correctly match a device. MFC r282922: Bump the size of the blockif scatter-gather list to 67. MFC r283075: Fix off-by-one in array index bounds check. bhyveload would allow you to create 33 entries on an array that only has 32 slots MFC r283168: Temporarily revert r282922 which bumped the max descriptors. MFC r283255: Emulate the "CMP r/m, reg" instruction (opcode 39H). MFC r283256: Add an option "--get-vmcs-exit-inst-length" to display the instruction length of the instruction that caused the VM-exit. MFC r283264: Change the header type of the emulated host-bridge from type 1 to type 0. MFC r283293: Don't rely on the 'VM-exit instruction length' field in the VMCS to always have an accurate length on an EPT violation. MFC r283299: Remove bogus verification of instruction length after instruction decode. MFC r283308: Exceptions don't deliver an error code in real mode. MFC r283657: Fix non-deterministic delays when accessing a vcpu that was in "running" or "sleeping" state. MFC r283973: Use tunable 'hw.vmm.svm.features' to disable specific SVM features even though they might be available in hardware. Use tunable 'hw.vmm.svm.num_asids' to limit the number of ASIDs used by the hypervisor. MFC r284046: Fix regression in 'verify_gla()' with the RIP-relative addressing mode. MFC r284174: Support guest writes to the TSC by enabling the "use TSC offsetting" execution control.
190 lines
4.7 KiB
C
190 lines
4.7 KiB
C
/*-
|
|
* Copyright (c) 2014 Nahanni Systems 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
|
|
* in this position and unchanged.
|
|
* 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.
|
|
*/
|
|
|
|
/*
|
|
* virtio entropy device emulation.
|
|
* Randomness is sourced from /dev/random which does not block
|
|
* once it has been seeded at bootup.
|
|
*/
|
|
|
|
#include <sys/cdefs.h>
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
#include <sys/param.h>
|
|
#include <sys/linker_set.h>
|
|
#include <sys/uio.h>
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <assert.h>
|
|
#include <pthread.h>
|
|
|
|
#include "bhyverun.h"
|
|
#include "pci_emul.h"
|
|
#include "virtio.h"
|
|
|
|
#define VTRND_RINGSZ 64
|
|
|
|
|
|
static int pci_vtrnd_debug;
|
|
#define DPRINTF(params) if (pci_vtrnd_debug) printf params
|
|
#define WPRINTF(params) printf params
|
|
|
|
/*
|
|
* 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;
|
|
};
|
|
|
|
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 */
|
|
};
|
|
|
|
|
|
static void
|
|
pci_vtrnd_reset(void *vsc)
|
|
{
|
|
struct pci_vtrnd_softc *sc;
|
|
|
|
sc = vsc;
|
|
|
|
DPRINTF(("vtrnd: device reset requested !\n"));
|
|
vi_reset_dev(&sc->vrsc_vs);
|
|
}
|
|
|
|
|
|
static void
|
|
pci_vtrnd_notify(void *vsc, struct vqueue_info *vq)
|
|
{
|
|
struct iovec iov;
|
|
struct pci_vtrnd_softc *sc;
|
|
int len;
|
|
uint16_t idx;
|
|
|
|
sc = vsc;
|
|
|
|
if (sc->vrsc_fd < 0) {
|
|
vq_endchains(vq, 0);
|
|
return;
|
|
}
|
|
|
|
while (vq_has_descs(vq)) {
|
|
vq_getchain(vq, &idx, &iov, 1, NULL);
|
|
|
|
len = read(sc->vrsc_fd, iov.iov_base, iov.iov_len);
|
|
|
|
DPRINTF(("vtrnd: vtrnd_notify(): %d\r\n", len));
|
|
|
|
/* Catastrophe if unable to read from /dev/random */
|
|
assert(len > 0);
|
|
|
|
/*
|
|
* Release this chain and handle more
|
|
*/
|
|
vq_relchain(vq, idx, len);
|
|
}
|
|
vq_endchains(vq, 1); /* Generate interrupt if appropriate. */
|
|
}
|
|
|
|
|
|
static int
|
|
pci_vtrnd_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
|
|
{
|
|
struct pci_vtrnd_softc *sc;
|
|
int fd;
|
|
int len;
|
|
uint8_t v;
|
|
|
|
/*
|
|
* Should always be able to open /dev/random.
|
|
*/
|
|
fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
|
|
|
|
assert(fd >= 0);
|
|
|
|
/*
|
|
* Check that device is seeded and non-blocking.
|
|
*/
|
|
len = read(fd, &v, sizeof(v));
|
|
if (len <= 0) {
|
|
WPRINTF(("vtrnd: /dev/random not ready, read(): %d", len));
|
|
return (1);
|
|
}
|
|
|
|
sc = calloc(1, sizeof(struct pci_vtrnd_softc));
|
|
|
|
vi_softc_linkup(&sc->vrsc_vs, &vtrnd_vi_consts, sc, pi, &sc->vrsc_vq);
|
|
sc->vrsc_vs.vs_mtx = &sc->vrsc_mtx;
|
|
|
|
sc->vrsc_vq.vq_qsize = VTRND_RINGSZ;
|
|
|
|
/* keep /dev/random opened while emulating */
|
|
sc->vrsc_fd = fd;
|
|
|
|
/* initialize config space */
|
|
pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_RANDOM);
|
|
pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
|
|
pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_CRYPTO);
|
|
pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_ENTROPY);
|
|
pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
|
|
|
|
if (vi_intr_init(&sc->vrsc_vs, 1, fbsdrun_virtio_msix()))
|
|
return (1);
|
|
vi_set_io_bar(&sc->vrsc_vs, 0);
|
|
|
|
return (0);
|
|
}
|
|
|
|
|
|
struct pci_devemu pci_de_vrnd = {
|
|
.pe_emu = "virtio-rnd",
|
|
.pe_init = pci_vtrnd_init,
|
|
.pe_barwrite = vi_pci_write,
|
|
.pe_barread = vi_pci_read
|
|
};
|
|
PCI_EMUL_SET(pci_de_vrnd);
|