2011-05-13 04:54:01 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2011-05-13 04:54:01 +00:00
|
|
|
* 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 <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2017-02-14 13:35:59 +00:00
|
|
|
#ifndef WITHOUT_CAPSICUM
|
|
|
|
#include <sys/capsicum.h>
|
|
|
|
#endif
|
2011-05-13 04:54:01 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2013-12-07 22:18:36 +00:00
|
|
|
#include <machine/atomic.h>
|
2011-05-13 04:54:01 +00:00
|
|
|
#include <machine/segments.h>
|
|
|
|
|
2017-02-14 13:35:59 +00:00
|
|
|
#ifndef WITHOUT_CAPSICUM
|
|
|
|
#include <capsicum_helpers.h>
|
|
|
|
#endif
|
2011-05-13 04:54:01 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-10-23 21:42:53 +00:00
|
|
|
#include <string.h>
|
2013-10-09 03:56:07 +00:00
|
|
|
#include <err.h>
|
2017-02-14 13:35:59 +00:00
|
|
|
#include <errno.h>
|
2011-05-13 04:54:01 +00:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
2012-10-31 19:17:55 +00:00
|
|
|
#include <pthread_np.h>
|
2013-10-09 03:56:07 +00:00
|
|
|
#include <sysexits.h>
|
2015-06-18 06:00:17 +00:00
|
|
|
#include <stdbool.h>
|
Add the ability to control the CPU topology of created VMs
from userland without the need to use sysctls, it allows the old
sysctls to continue to function, but deprecates them at
FreeBSD_version 1200060 (Relnotes for deprecate).
The command line of bhyve is maintained in a backwards compatible way.
The API of libvmmapi is maintained in a backwards compatible way.
The sysctl's are maintained in a backwards compatible way.
Added command option looks like:
bhyve -c [[cpus=]n][,sockets=n][,cores=n][,threads=n][,maxcpus=n]
The optional parts can be specified in any order, but only a single
integer invokes the backwards compatible parse. [,maxcpus=n] is
hidden by #ifdef until kernel support is added, though the api
is put in place.
bhyvectl --get-cpu-topology option added.
Reviewed by: grehan (maintainer, earlier version),
Reviewed by: bcr (manpages)
Approved by: bde (mentor), phk (mentor)
Tested by: Oleg Ginzburg <olevole@olevole.ru> (cbsd)
MFC after: 1 week
Relnotes: Y
Differential Revision: https://reviews.freebsd.org/D9930
2018-04-08 19:24:49 +00:00
|
|
|
#include <stdint.h>
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
#include <machine/vmm.h>
|
2017-02-14 13:35:59 +00:00
|
|
|
#ifndef WITHOUT_CAPSICUM
|
|
|
|
#include <machine/vmm_dev.h>
|
|
|
|
#endif
|
2011-05-13 04:54:01 +00:00
|
|
|
#include <vmmapi.h>
|
|
|
|
|
2012-12-13 01:58:11 +00:00
|
|
|
#include "bhyverun.h"
|
2012-11-20 07:01:26 +00:00
|
|
|
#include "acpi.h"
|
Initial bhyve native graphics support.
This adds emulations for a raw framebuffer device, PS2 keyboard/mouse,
XHCI USB controller and a USB tablet.
A simple VNC server is provided for keyboard/mouse input, and graphics
output.
A VGA emulation is included, but is currently disconnected until an
additional bhyve change to block out VGA memory is committed.
Credits:
- raw framebuffer, VNC server, XHCI controller, USB bus/device emulation
and UEFI f/w support by Leon Dang
- VGA, console/g, initial VNC server by tychon@
- PS2 keyboard/mouse jointly done by tychon@ and Leon Dang
- hypervisor framebuffer mem support by neel@
Tested by: Michael Dexter, in a number of revisions of this code.
With the appropriate UEFI image, FreeBSD, Windows and Linux guests can
installed and run in graphics mode using the UEFI/GOP framebuffer.
2016-05-27 06:30:35 +00:00
|
|
|
#include "atkbdc.h"
|
2011-05-13 04:54:01 +00:00
|
|
|
#include "inout.h"
|
|
|
|
#include "dbgport.h"
|
2015-10-02 21:09:49 +00:00
|
|
|
#include "fwctl.h"
|
2018-05-01 15:17:46 +00:00
|
|
|
#include "gdb.h"
|
2014-01-29 14:56:48 +00:00
|
|
|
#include "ioapic.h"
|
2012-10-19 18:11:17 +00:00
|
|
|
#include "mem.h"
|
2011-05-13 04:54:01 +00:00
|
|
|
#include "mevent.h"
|
2012-10-26 13:40:12 +00:00
|
|
|
#include "mptbl.h"
|
2011-05-13 04:54:01 +00:00
|
|
|
#include "pci_emul.h"
|
2014-05-15 14:16:55 +00:00
|
|
|
#include "pci_irq.h"
|
2013-10-29 00:18:11 +00:00
|
|
|
#include "pci_lpc.h"
|
2014-03-04 17:12:06 +00:00
|
|
|
#include "smbiostbl.h"
|
2011-05-13 04:54:01 +00:00
|
|
|
#include "xmsr.h"
|
2012-09-25 02:33:25 +00:00
|
|
|
#include "spinup_ap.h"
|
2013-07-11 03:54:35 +00:00
|
|
|
#include "rtc.h"
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
#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);
|
2014-07-16 21:26:26 +00:00
|
|
|
extern int vmexit_task_switch(struct vmctx *, struct vm_exit *, int *vcpu);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
char *vmname;
|
|
|
|
|
|
|
|
int guest_ncpus;
|
Add the ability to control the CPU topology of created VMs
from userland without the need to use sysctls, it allows the old
sysctls to continue to function, but deprecates them at
FreeBSD_version 1200060 (Relnotes for deprecate).
The command line of bhyve is maintained in a backwards compatible way.
The API of libvmmapi is maintained in a backwards compatible way.
The sysctl's are maintained in a backwards compatible way.
Added command option looks like:
bhyve -c [[cpus=]n][,sockets=n][,cores=n][,threads=n][,maxcpus=n]
The optional parts can be specified in any order, but only a single
integer invokes the backwards compatible parse. [,maxcpus=n] is
hidden by #ifdef until kernel support is added, though the api
is put in place.
bhyvectl --get-cpu-topology option added.
Reviewed by: grehan (maintainer, earlier version),
Reviewed by: bcr (manpages)
Approved by: bde (mentor), phk (mentor)
Tested by: Oleg Ginzburg <olevole@olevole.ru> (cbsd)
MFC after: 1 week
Relnotes: Y
Differential Revision: https://reviews.freebsd.org/D9930
2018-04-08 19:24:49 +00:00
|
|
|
uint16_t cores, maxcpus, sockets, threads;
|
|
|
|
|
2014-03-04 17:12:06 +00:00
|
|
|
char *guest_uuid_str;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2014-02-20 01:48:25 +00:00
|
|
|
static int guest_vmexit_on_hlt, guest_vmexit_on_pause;
|
2013-10-17 22:01:17 +00:00
|
|
|
static int virtio_msix = 1;
|
2014-02-20 01:48:25 +00:00
|
|
|
static int x2apic_mode = 0; /* default is xAPIC */
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2011-05-19 21:53:25 +00:00
|
|
|
static int strictio;
|
2013-12-19 22:27:28 +00:00
|
|
|
static int strictmsr = 1;
|
2011-05-19 21:53:25 +00:00
|
|
|
|
2012-11-20 07:01:26 +00:00
|
|
|
static int acpi;
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static char *progname;
|
|
|
|
static const int BSP = 0;
|
|
|
|
|
2014-03-20 18:15:37 +00:00
|
|
|
static cpuset_t cpumask;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
static void vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip);
|
|
|
|
|
2014-07-24 01:38:11 +00:00
|
|
|
static struct vm_exit vmexit[VM_MAXCPU];
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2013-10-04 23:29:07 +00:00
|
|
|
struct bhyvestats {
|
2011-05-13 04:54:01 +00:00
|
|
|
uint64_t vmexit_bogus;
|
2015-05-28 17:37:01 +00:00
|
|
|
uint64_t vmexit_reqidle;
|
2011-05-13 04:54:01 +00:00
|
|
|
uint64_t vmexit_hlt;
|
|
|
|
uint64_t vmexit_pause;
|
|
|
|
uint64_t vmexit_mtrap;
|
2013-10-05 21:22:35 +00:00
|
|
|
uint64_t vmexit_inst_emul;
|
2011-05-13 04:54:01 +00:00
|
|
|
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];
|
|
|
|
|
2014-05-05 18:06:35 +00:00
|
|
|
static cpuset_t *vcpumap[VM_MAXCPU] = { NULL };
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static void
|
|
|
|
usage(int code)
|
|
|
|
{
|
|
|
|
|
|
|
|
fprintf(stderr,
|
Add the ability to control the CPU topology of created VMs
from userland without the need to use sysctls, it allows the old
sysctls to continue to function, but deprecates them at
FreeBSD_version 1200060 (Relnotes for deprecate).
The command line of bhyve is maintained in a backwards compatible way.
The API of libvmmapi is maintained in a backwards compatible way.
The sysctl's are maintained in a backwards compatible way.
Added command option looks like:
bhyve -c [[cpus=]n][,sockets=n][,cores=n][,threads=n][,maxcpus=n]
The optional parts can be specified in any order, but only a single
integer invokes the backwards compatible parse. [,maxcpus=n] is
hidden by #ifdef until kernel support is added, though the api
is put in place.
bhyvectl --get-cpu-topology option added.
Reviewed by: grehan (maintainer, earlier version),
Reviewed by: bcr (manpages)
Approved by: bde (mentor), phk (mentor)
Tested by: Oleg Ginzburg <olevole@olevole.ru> (cbsd)
MFC after: 1 week
Relnotes: Y
Differential Revision: https://reviews.freebsd.org/D9930
2018-04-08 19:24:49 +00:00
|
|
|
"Usage: %s [-abehuwxACHPSWY]\n"
|
|
|
|
" %*s [-c [[cpus=]numcpus][,sockets=n][,cores=n][,threads=n]]\n"
|
|
|
|
" %*s [-g <gdb port>] [-l <lpc>]\n"
|
2014-06-27 15:20:34 +00:00
|
|
|
" %*s [-m mem] [-p vcpu:hostcpu] [-s <pci>] [-U uuid] <vm>\n"
|
2014-02-20 01:48:25 +00:00
|
|
|
" -a: local apic is in xAPIC mode (deprecated)\n"
|
2014-06-26 20:12:38 +00:00
|
|
|
" -A: create ACPI tables\n"
|
2018-05-17 12:18:41 +00:00
|
|
|
" -c: number of cpus and/or topology specification\n"
|
2014-05-13 16:40:27 +00:00
|
|
|
" -C: include guest memory in core file\n"
|
2013-10-23 21:42:53 +00:00
|
|
|
" -e: exit on unhandled I/O access\n"
|
2014-06-27 15:20:34 +00:00
|
|
|
" -g: gdb port\n"
|
2011-05-13 04:54:01 +00:00
|
|
|
" -h: help\n"
|
2014-06-27 15:20:34 +00:00
|
|
|
" -H: vmexit from the guest on hlt\n"
|
2013-10-29 00:18:11 +00:00
|
|
|
" -l: LPC device configuration\n"
|
2013-12-19 22:27:28 +00:00
|
|
|
" -m: memory size in MB\n"
|
2014-06-27 15:20:34 +00:00
|
|
|
" -p: pin 'vcpu' to 'hostcpu'\n"
|
|
|
|
" -P: vmexit from the guest on pause\n"
|
|
|
|
" -s: <slot,driver,configinfo> PCI slot config\n"
|
2015-06-18 06:00:17 +00:00
|
|
|
" -S: guest memory cannot be swapped\n"
|
2015-02-24 02:04:16 +00:00
|
|
|
" -u: RTC keeps UTC time\n"
|
2014-06-27 15:20:34 +00:00
|
|
|
" -U: uuid\n"
|
2014-02-20 01:48:25 +00:00
|
|
|
" -w: ignore unimplemented MSRs\n"
|
2014-06-27 15:20:34 +00:00
|
|
|
" -W: force virtio to use single-vector MSI\n"
|
2014-03-04 17:12:06 +00:00
|
|
|
" -x: local apic is in x2APIC mode\n"
|
2014-06-27 15:20:34 +00:00
|
|
|
" -Y: disable MPtable generation\n",
|
Add the ability to control the CPU topology of created VMs
from userland without the need to use sysctls, it allows the old
sysctls to continue to function, but deprecates them at
FreeBSD_version 1200060 (Relnotes for deprecate).
The command line of bhyve is maintained in a backwards compatible way.
The API of libvmmapi is maintained in a backwards compatible way.
The sysctl's are maintained in a backwards compatible way.
Added command option looks like:
bhyve -c [[cpus=]n][,sockets=n][,cores=n][,threads=n][,maxcpus=n]
The optional parts can be specified in any order, but only a single
integer invokes the backwards compatible parse. [,maxcpus=n] is
hidden by #ifdef until kernel support is added, though the api
is put in place.
bhyvectl --get-cpu-topology option added.
Reviewed by: grehan (maintainer, earlier version),
Reviewed by: bcr (manpages)
Approved by: bde (mentor), phk (mentor)
Tested by: Oleg Ginzburg <olevole@olevole.ru> (cbsd)
MFC after: 1 week
Relnotes: Y
Differential Revision: https://reviews.freebsd.org/D9930
2018-04-08 19:24:49 +00:00
|
|
|
progname, (int)strlen(progname), "", (int)strlen(progname), "",
|
|
|
|
(int)strlen(progname), "");
|
2013-10-04 23:29:07 +00:00
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
exit(code);
|
|
|
|
}
|
|
|
|
|
Add the ability to control the CPU topology of created VMs
from userland without the need to use sysctls, it allows the old
sysctls to continue to function, but deprecates them at
FreeBSD_version 1200060 (Relnotes for deprecate).
The command line of bhyve is maintained in a backwards compatible way.
The API of libvmmapi is maintained in a backwards compatible way.
The sysctl's are maintained in a backwards compatible way.
Added command option looks like:
bhyve -c [[cpus=]n][,sockets=n][,cores=n][,threads=n][,maxcpus=n]
The optional parts can be specified in any order, but only a single
integer invokes the backwards compatible parse. [,maxcpus=n] is
hidden by #ifdef until kernel support is added, though the api
is put in place.
bhyvectl --get-cpu-topology option added.
Reviewed by: grehan (maintainer, earlier version),
Reviewed by: bcr (manpages)
Approved by: bde (mentor), phk (mentor)
Tested by: Oleg Ginzburg <olevole@olevole.ru> (cbsd)
MFC after: 1 week
Relnotes: Y
Differential Revision: https://reviews.freebsd.org/D9930
2018-04-08 19:24:49 +00:00
|
|
|
/*
|
|
|
|
* XXX This parser is known to have the following issues:
|
|
|
|
* 1. It accepts null key=value tokens ",,".
|
|
|
|
* 2. It accepts whitespace after = and before value.
|
|
|
|
* 3. Values out of range of INT are silently wrapped.
|
|
|
|
* 4. It doesn't check non-final values.
|
|
|
|
* 5. The apparently bogus limits of UINT16_MAX are for future expansion.
|
|
|
|
*
|
|
|
|
* The acceptance of a null specification ('-c ""') is by design to match the
|
|
|
|
* manual page syntax specification, this results in a topology of 1 vCPU.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
topology_parse(const char *opt)
|
|
|
|
{
|
|
|
|
uint64_t ncpus;
|
|
|
|
int c, chk, n, s, t, tmp;
|
|
|
|
char *cp, *str;
|
|
|
|
bool ns, scts;
|
|
|
|
|
|
|
|
c = 1, n = 1, s = 1, t = 1;
|
|
|
|
ns = false, scts = false;
|
|
|
|
str = strdup(opt);
|
2018-05-25 18:54:40 +00:00
|
|
|
if (str == NULL)
|
|
|
|
goto out;
|
Add the ability to control the CPU topology of created VMs
from userland without the need to use sysctls, it allows the old
sysctls to continue to function, but deprecates them at
FreeBSD_version 1200060 (Relnotes for deprecate).
The command line of bhyve is maintained in a backwards compatible way.
The API of libvmmapi is maintained in a backwards compatible way.
The sysctl's are maintained in a backwards compatible way.
Added command option looks like:
bhyve -c [[cpus=]n][,sockets=n][,cores=n][,threads=n][,maxcpus=n]
The optional parts can be specified in any order, but only a single
integer invokes the backwards compatible parse. [,maxcpus=n] is
hidden by #ifdef until kernel support is added, though the api
is put in place.
bhyvectl --get-cpu-topology option added.
Reviewed by: grehan (maintainer, earlier version),
Reviewed by: bcr (manpages)
Approved by: bde (mentor), phk (mentor)
Tested by: Oleg Ginzburg <olevole@olevole.ru> (cbsd)
MFC after: 1 week
Relnotes: Y
Differential Revision: https://reviews.freebsd.org/D9930
2018-04-08 19:24:49 +00:00
|
|
|
|
|
|
|
while ((cp = strsep(&str, ",")) != NULL) {
|
|
|
|
if (sscanf(cp, "%i%n", &tmp, &chk) == 1) {
|
|
|
|
n = tmp;
|
|
|
|
ns = true;
|
|
|
|
} else if (sscanf(cp, "cpus=%i%n", &tmp, &chk) == 1) {
|
|
|
|
n = tmp;
|
|
|
|
ns = true;
|
|
|
|
} else if (sscanf(cp, "sockets=%i%n", &tmp, &chk) == 1) {
|
|
|
|
s = tmp;
|
|
|
|
scts = true;
|
|
|
|
} else if (sscanf(cp, "cores=%i%n", &tmp, &chk) == 1) {
|
|
|
|
c = tmp;
|
|
|
|
scts = true;
|
|
|
|
} else if (sscanf(cp, "threads=%i%n", &tmp, &chk) == 1) {
|
|
|
|
t = tmp;
|
|
|
|
scts = true;
|
|
|
|
#ifdef notyet /* Do not expose this until vmm.ko implements it */
|
|
|
|
} else if (sscanf(cp, "maxcpus=%i%n", &tmp, &chk) == 1) {
|
|
|
|
m = tmp;
|
|
|
|
#endif
|
|
|
|
/* Skip the empty argument case from -c "" */
|
|
|
|
} else if (cp[0] == '\0')
|
|
|
|
continue;
|
|
|
|
else
|
2018-05-25 02:07:05 +00:00
|
|
|
goto out;
|
Add the ability to control the CPU topology of created VMs
from userland without the need to use sysctls, it allows the old
sysctls to continue to function, but deprecates them at
FreeBSD_version 1200060 (Relnotes for deprecate).
The command line of bhyve is maintained in a backwards compatible way.
The API of libvmmapi is maintained in a backwards compatible way.
The sysctl's are maintained in a backwards compatible way.
Added command option looks like:
bhyve -c [[cpus=]n][,sockets=n][,cores=n][,threads=n][,maxcpus=n]
The optional parts can be specified in any order, but only a single
integer invokes the backwards compatible parse. [,maxcpus=n] is
hidden by #ifdef until kernel support is added, though the api
is put in place.
bhyvectl --get-cpu-topology option added.
Reviewed by: grehan (maintainer, earlier version),
Reviewed by: bcr (manpages)
Approved by: bde (mentor), phk (mentor)
Tested by: Oleg Ginzburg <olevole@olevole.ru> (cbsd)
MFC after: 1 week
Relnotes: Y
Differential Revision: https://reviews.freebsd.org/D9930
2018-04-08 19:24:49 +00:00
|
|
|
/* Any trailing garbage causes an error */
|
|
|
|
if (cp[chk] != '\0')
|
2018-05-25 02:07:05 +00:00
|
|
|
goto out;
|
Add the ability to control the CPU topology of created VMs
from userland without the need to use sysctls, it allows the old
sysctls to continue to function, but deprecates them at
FreeBSD_version 1200060 (Relnotes for deprecate).
The command line of bhyve is maintained in a backwards compatible way.
The API of libvmmapi is maintained in a backwards compatible way.
The sysctl's are maintained in a backwards compatible way.
Added command option looks like:
bhyve -c [[cpus=]n][,sockets=n][,cores=n][,threads=n][,maxcpus=n]
The optional parts can be specified in any order, but only a single
integer invokes the backwards compatible parse. [,maxcpus=n] is
hidden by #ifdef until kernel support is added, though the api
is put in place.
bhyvectl --get-cpu-topology option added.
Reviewed by: grehan (maintainer, earlier version),
Reviewed by: bcr (manpages)
Approved by: bde (mentor), phk (mentor)
Tested by: Oleg Ginzburg <olevole@olevole.ru> (cbsd)
MFC after: 1 week
Relnotes: Y
Differential Revision: https://reviews.freebsd.org/D9930
2018-04-08 19:24:49 +00:00
|
|
|
}
|
2018-05-25 02:07:05 +00:00
|
|
|
free(str);
|
2018-05-25 18:54:40 +00:00
|
|
|
str = NULL;
|
2018-05-25 02:07:05 +00:00
|
|
|
|
Add the ability to control the CPU topology of created VMs
from userland without the need to use sysctls, it allows the old
sysctls to continue to function, but deprecates them at
FreeBSD_version 1200060 (Relnotes for deprecate).
The command line of bhyve is maintained in a backwards compatible way.
The API of libvmmapi is maintained in a backwards compatible way.
The sysctl's are maintained in a backwards compatible way.
Added command option looks like:
bhyve -c [[cpus=]n][,sockets=n][,cores=n][,threads=n][,maxcpus=n]
The optional parts can be specified in any order, but only a single
integer invokes the backwards compatible parse. [,maxcpus=n] is
hidden by #ifdef until kernel support is added, though the api
is put in place.
bhyvectl --get-cpu-topology option added.
Reviewed by: grehan (maintainer, earlier version),
Reviewed by: bcr (manpages)
Approved by: bde (mentor), phk (mentor)
Tested by: Oleg Ginzburg <olevole@olevole.ru> (cbsd)
MFC after: 1 week
Relnotes: Y
Differential Revision: https://reviews.freebsd.org/D9930
2018-04-08 19:24:49 +00:00
|
|
|
/*
|
|
|
|
* Range check 1 <= n <= UINT16_MAX all values
|
|
|
|
*/
|
|
|
|
if (n < 1 || s < 1 || c < 1 || t < 1 ||
|
|
|
|
n > UINT16_MAX || s > UINT16_MAX || c > UINT16_MAX ||
|
|
|
|
t > UINT16_MAX)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* If only the cpus was specified, use that as sockets */
|
|
|
|
if (!scts)
|
|
|
|
s = n;
|
|
|
|
/*
|
|
|
|
* Compute sockets * cores * threads avoiding overflow
|
|
|
|
* The range check above insures these are 16 bit values
|
|
|
|
* If n was specified check it against computed ncpus
|
|
|
|
*/
|
|
|
|
ncpus = (uint64_t)s * c * t;
|
|
|
|
if (ncpus > UINT16_MAX || (ns && n != ncpus))
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
guest_ncpus = ncpus;
|
|
|
|
sockets = s;
|
|
|
|
cores = c;
|
|
|
|
threads = t;
|
|
|
|
return(0);
|
2018-05-25 02:07:05 +00:00
|
|
|
|
|
|
|
out:
|
2018-05-25 19:12:30 +00:00
|
|
|
free(str);
|
2018-05-25 02:07:05 +00:00
|
|
|
return (-1);
|
Add the ability to control the CPU topology of created VMs
from userland without the need to use sysctls, it allows the old
sysctls to continue to function, but deprecates them at
FreeBSD_version 1200060 (Relnotes for deprecate).
The command line of bhyve is maintained in a backwards compatible way.
The API of libvmmapi is maintained in a backwards compatible way.
The sysctl's are maintained in a backwards compatible way.
Added command option looks like:
bhyve -c [[cpus=]n][,sockets=n][,cores=n][,threads=n][,maxcpus=n]
The optional parts can be specified in any order, but only a single
integer invokes the backwards compatible parse. [,maxcpus=n] is
hidden by #ifdef until kernel support is added, though the api
is put in place.
bhyvectl --get-cpu-topology option added.
Reviewed by: grehan (maintainer, earlier version),
Reviewed by: bcr (manpages)
Approved by: bde (mentor), phk (mentor)
Tested by: Oleg Ginzburg <olevole@olevole.ru> (cbsd)
MFC after: 1 week
Relnotes: Y
Differential Revision: https://reviews.freebsd.org/D9930
2018-04-08 19:24:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-05 18:06:35 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-07-24 01:38:11 +00:00
|
|
|
void
|
|
|
|
vm_inject_fault(void *arg, int vcpu, int vector, int errcode_valid,
|
|
|
|
int errcode)
|
|
|
|
{
|
|
|
|
struct vmctx *ctx;
|
2015-01-18 03:08:30 +00:00
|
|
|
int error, restart_instruction;
|
2014-07-24 01:38:11 +00:00
|
|
|
|
|
|
|
ctx = arg;
|
2015-01-18 03:08:30 +00:00
|
|
|
restart_instruction = 1;
|
2014-07-24 01:38:11 +00:00
|
|
|
|
2015-01-18 03:08:30 +00:00
|
|
|
error = vm_inject_exception(ctx, vcpu, vector, errcode_valid, errcode,
|
|
|
|
restart_instruction);
|
|
|
|
assert(error == 0);
|
2014-07-24 01:38:11 +00:00
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
void *
|
2013-03-18 22:38:30 +00:00
|
|
|
paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
|
|
|
|
2013-03-18 22:38:30 +00:00
|
|
|
return (vm_map_gpa(ctx, gaddr, len));
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fbsdrun_vmexit_on_pause(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (guest_vmexit_on_pause);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fbsdrun_vmexit_on_hlt(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (guest_vmexit_on_hlt);
|
|
|
|
}
|
|
|
|
|
2013-10-17 22:01:17 +00:00
|
|
|
int
|
|
|
|
fbsdrun_virtio_msix(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (virtio_msix);
|
|
|
|
}
|
|
|
|
|
2011-05-15 04:03:11 +00:00
|
|
|
static void *
|
2011-05-13 04:54:01 +00:00
|
|
|
fbsdrun_start_thread(void *param)
|
|
|
|
{
|
2012-10-31 19:17:55 +00:00
|
|
|
char tname[MAXCOMLEN + 1];
|
|
|
|
struct mt_vmm_info *mtp;
|
2011-05-13 04:54:01 +00:00
|
|
|
int vcpu;
|
|
|
|
|
2012-10-31 19:17:55 +00:00
|
|
|
mtp = param;
|
2011-05-13 04:54:01 +00:00
|
|
|
vcpu = mtp->mt_vcpu;
|
2012-10-31 19:17:55 +00:00
|
|
|
|
2013-11-06 00:25:17 +00:00
|
|
|
snprintf(tname, sizeof(tname), "vcpu %d", vcpu);
|
2012-10-31 19:17:55 +00:00
|
|
|
pthread_set_name_np(mtp->mt_thr, tname);
|
|
|
|
|
2018-05-01 15:17:46 +00:00
|
|
|
gdb_cpu_add(vcpu);
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
vm_loop(mtp->mt_ctx, vcpu, vmexit[vcpu].rip);
|
|
|
|
|
|
|
|
/* not reached */
|
|
|
|
exit(1);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-03-20 18:15:37 +00:00
|
|
|
fbsdrun_addcpu(struct vmctx *ctx, int fromcpu, int newcpu, uint64_t rip)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2014-03-20 18:15:37 +00:00
|
|
|
assert(fromcpu == BSP);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2014-05-31 23:37:34 +00:00
|
|
|
/*
|
|
|
|
* 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);
|
2015-10-22 08:37:11 +00:00
|
|
|
if (error != 0)
|
|
|
|
err(EX_OSERR, "could not activate CPU %d", newcpu);
|
2014-05-31 23:37:34 +00:00
|
|
|
|
2014-03-20 18:15:37 +00:00
|
|
|
CPU_SET_ATOMIC(newcpu, &cpumask);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the vmexit struct to allow execution to start
|
|
|
|
* at the given RIP
|
|
|
|
*/
|
2014-03-20 18:15:37 +00:00
|
|
|
vmexit[newcpu].rip = rip;
|
|
|
|
vmexit[newcpu].inst_length = 0;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2014-03-20 18:15:37 +00:00
|
|
|
mt_vmm_info[newcpu].mt_ctx = ctx;
|
|
|
|
mt_vmm_info[newcpu].mt_vcpu = newcpu;
|
2013-10-05 21:22:35 +00:00
|
|
|
|
2014-03-20 18:15:37 +00:00
|
|
|
error = pthread_create(&mt_vmm_info[newcpu].mt_thr, NULL,
|
|
|
|
fbsdrun_start_thread, &mt_vmm_info[newcpu]);
|
2013-10-05 21:22:35 +00:00
|
|
|
assert(error == 0);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
2013-12-07 22:18:36 +00:00
|
|
|
static int
|
|
|
|
fbsdrun_deletecpu(struct vmctx *ctx, int vcpu)
|
|
|
|
{
|
|
|
|
|
2014-03-20 18:15:37 +00:00
|
|
|
if (!CPU_ISSET(vcpu, &cpumask)) {
|
2014-05-05 16:35:37 +00:00
|
|
|
fprintf(stderr, "Attempting to delete unknown cpu %d\n", vcpu);
|
2013-12-07 22:18:36 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2014-03-20 18:15:37 +00:00
|
|
|
CPU_CLR_ATOMIC(vcpu, &cpumask);
|
|
|
|
return (CPU_EMPTY(&cpumask));
|
2013-12-07 22:18:36 +00:00
|
|
|
}
|
|
|
|
|
2011-05-15 04:03:11 +00:00
|
|
|
static int
|
2011-05-13 04:54:01 +00:00
|
|
|
vmexit_handle_notify(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu,
|
|
|
|
uint32_t eax)
|
|
|
|
{
|
2013-10-04 23:29:07 +00:00
|
|
|
#if BHYVE_DEBUG
|
|
|
|
/*
|
|
|
|
* put guest-driven debug here
|
|
|
|
*/
|
2011-05-13 04:54:01 +00:00
|
|
|
#endif
|
|
|
|
return (VMEXIT_CONTINUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_inout(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
|
|
|
|
{
|
|
|
|
int error;
|
2015-12-31 07:08:21 +00:00
|
|
|
int bytes, port, in, out;
|
2011-05-13 04:54:01 +00:00
|
|
|
int vcpu;
|
|
|
|
|
|
|
|
vcpu = *pvcpu;
|
|
|
|
|
|
|
|
port = vme->u.inout.port;
|
|
|
|
bytes = vme->u.inout.bytes;
|
|
|
|
in = vme->u.inout.in;
|
|
|
|
out = !in;
|
|
|
|
|
|
|
|
/* Extra-special case of host notifications */
|
2014-05-23 05:15:17 +00:00
|
|
|
if (out && port == GUEST_NIO_PORT) {
|
|
|
|
error = vmexit_handle_notify(ctx, vme, pvcpu, vme->u.inout.eax);
|
|
|
|
return (error);
|
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2014-05-23 05:15:17 +00:00
|
|
|
error = emulate_inout(ctx, vcpu, vme, strictio);
|
2014-07-25 20:18:35 +00:00
|
|
|
if (error) {
|
2015-04-15 18:49:03 +00:00
|
|
|
fprintf(stderr, "Unhandled %s%c 0x%04x at 0x%lx\n",
|
|
|
|
in ? "in" : "out",
|
|
|
|
bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'),
|
|
|
|
port, vmexit->rip);
|
2014-05-12 23:35:10 +00:00
|
|
|
return (VMEXIT_ABORT);
|
2014-07-25 20:18:35 +00:00
|
|
|
} else {
|
|
|
|
return (VMEXIT_CONTINUE);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_rdmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
|
|
|
|
{
|
2013-12-19 22:27:28 +00:00
|
|
|
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);
|
2014-02-26 00:52:05 +00:00
|
|
|
if (strictmsr) {
|
2014-07-24 01:38:11 +00:00
|
|
|
vm_inject_gp(ctx, *pvcpu);
|
2015-01-18 03:08:30 +00:00
|
|
|
return (VMEXIT_CONTINUE);
|
2014-02-26 00:52:05 +00:00
|
|
|
}
|
2013-12-19 22:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_wrmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
|
|
|
|
{
|
2013-12-19 22:27:28 +00:00
|
|
|
int error;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2013-12-19 22:27:28 +00:00
|
|
|
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);
|
2014-02-26 00:52:05 +00:00
|
|
|
if (strictmsr) {
|
2014-07-24 01:38:11 +00:00
|
|
|
vm_inject_gp(ctx, *pvcpu);
|
2015-01-18 03:08:30 +00:00
|
|
|
return (VMEXIT_CONTINUE);
|
2014-02-26 00:52:05 +00:00
|
|
|
}
|
2013-12-19 22:27:28 +00:00
|
|
|
}
|
|
|
|
return (VMEXIT_CONTINUE);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
2012-09-25 02:33:25 +00:00
|
|
|
static int
|
|
|
|
vmexit_spinup_ap(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
|
|
|
|
{
|
|
|
|
|
2016-07-06 04:56:45 +00:00
|
|
|
(void)spinup_ap(ctx, *pvcpu,
|
|
|
|
vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip);
|
2012-09-25 02:33:25 +00:00
|
|
|
|
2016-07-06 04:56:45 +00:00
|
|
|
return (VMEXIT_CONTINUE);
|
2012-09-25 02:33:25 +00:00
|
|
|
}
|
|
|
|
|
2014-06-27 18:00:38 +00:00
|
|
|
#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
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static int
|
|
|
|
vmexit_vmx(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
|
|
|
|
2012-10-31 03:29:52 +00:00
|
|
|
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);
|
2014-01-01 21:17:08 +00:00
|
|
|
fprintf(stderr, "\tstatus\t\t%d\n", vmexit->u.vmx.status);
|
2012-10-31 03:29:52 +00:00
|
|
|
fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason);
|
|
|
|
fprintf(stderr, "\tqualification\t0x%016lx\n",
|
|
|
|
vmexit->u.vmx.exit_qualification);
|
2014-01-01 21:17:08 +00:00
|
|
|
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);
|
2014-06-27 18:00:38 +00:00
|
|
|
#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 */
|
2011-05-13 04:54:01 +00:00
|
|
|
return (VMEXIT_ABORT);
|
|
|
|
}
|
|
|
|
|
2014-09-14 04:39:04 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static int
|
|
|
|
vmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
2013-10-04 23:29:07 +00:00
|
|
|
|
2015-01-18 03:08:30 +00:00
|
|
|
assert(vmexit->inst_length == 0);
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
stats.vmexit_bogus++;
|
|
|
|
|
2015-01-18 03:08:30 +00:00
|
|
|
return (VMEXIT_CONTINUE);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 17:37:01 +00:00
|
|
|
static int
|
|
|
|
vmexit_reqidle(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(vmexit->inst_length == 0);
|
|
|
|
|
|
|
|
stats.vmexit_reqidle++;
|
|
|
|
|
|
|
|
return (VMEXIT_CONTINUE);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static int
|
|
|
|
vmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
2013-10-04 23:29:07 +00:00
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
stats.vmexit_hlt++;
|
|
|
|
|
2013-10-04 23:29:07 +00:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
2013-10-04 23:29:07 +00:00
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
stats.vmexit_pause++;
|
|
|
|
|
2013-10-04 23:29:07 +00:00
|
|
|
return (VMEXIT_CONTINUE);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
2013-10-04 23:29:07 +00:00
|
|
|
|
2015-01-18 03:08:30 +00:00
|
|
|
assert(vmexit->inst_length == 0);
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
stats.vmexit_mtrap++;
|
|
|
|
|
2018-05-01 15:17:46 +00:00
|
|
|
gdb_cpu_mtrap(*pvcpu);
|
|
|
|
|
2015-01-18 03:08:30 +00:00
|
|
|
return (VMEXIT_CONTINUE);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
2012-04-28 16:28:00 +00:00
|
|
|
static int
|
2013-10-05 21:22:35 +00:00
|
|
|
vmexit_inst_emul(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
2012-04-28 16:28:00 +00:00
|
|
|
{
|
2015-04-01 20:36:07 +00:00
|
|
|
int err, i;
|
|
|
|
struct vie *vie;
|
|
|
|
|
2013-10-05 21:22:35 +00:00
|
|
|
stats.vmexit_inst_emul++;
|
2012-04-28 16:28:00 +00:00
|
|
|
|
2015-04-01 20:36:07 +00:00
|
|
|
vie = &vmexit->u.inst_emul.vie;
|
2013-10-05 21:22:35 +00:00
|
|
|
err = emulate_mem(ctx, *pvcpu, vmexit->u.inst_emul.gpa,
|
2015-04-01 20:36:07 +00:00
|
|
|
vie, &vmexit->u.inst_emul.paging);
|
2012-10-19 18:11:17 +00:00
|
|
|
|
|
|
|
if (err) {
|
2015-04-01 20:36:07 +00:00
|
|
|
if (err == ESRCH) {
|
2012-10-31 03:29:52 +00:00
|
|
|
fprintf(stderr, "Unhandled memory access to 0x%lx\n",
|
2013-10-05 21:22:35 +00:00
|
|
|
vmexit->u.inst_emul.gpa);
|
2012-10-19 18:11:17 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 20:36:07 +00:00
|
|
|
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);
|
2012-04-28 16:28:00 +00:00
|
|
|
return (VMEXIT_ABORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (VMEXIT_CONTINUE);
|
|
|
|
}
|
|
|
|
|
2014-03-26 23:34:27 +00:00
|
|
|
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)
|
|
|
|
{
|
2014-04-28 22:06:40 +00:00
|
|
|
enum vm_suspend_how how;
|
|
|
|
|
|
|
|
how = vmexit->u.suspended.how;
|
2014-03-26 23:34:27 +00:00
|
|
|
|
|
|
|
fbsdrun_deletecpu(ctx, *pvcpu);
|
|
|
|
|
2014-04-28 22:06:40 +00:00
|
|
|
if (*pvcpu != BSP) {
|
2014-03-26 23:34:27 +00:00
|
|
|
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);
|
2014-04-28 22:06:40 +00:00
|
|
|
|
2014-05-02 00:33:56 +00:00
|
|
|
switch (how) {
|
|
|
|
case VM_SUSPEND_RESET:
|
2014-04-28 22:06:40 +00:00
|
|
|
exit(0);
|
2014-05-02 00:33:56 +00:00
|
|
|
case VM_SUSPEND_POWEROFF:
|
2014-04-28 22:06:40 +00:00
|
|
|
exit(1);
|
2014-05-02 00:33:56 +00:00
|
|
|
case VM_SUSPEND_HALT:
|
|
|
|
exit(2);
|
2014-07-19 20:59:08 +00:00
|
|
|
case VM_SUSPEND_TRIPLEFAULT:
|
|
|
|
exit(3);
|
2014-05-02 00:33:56 +00:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "vmexit_suspend: invalid reason %d\n", how);
|
|
|
|
exit(100);
|
|
|
|
}
|
2014-04-28 22:06:40 +00:00
|
|
|
return (0); /* NOTREACHED */
|
2014-03-26 23:34:27 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 15:17:46 +00:00
|
|
|
static int
|
|
|
|
vmexit_debug(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
|
|
|
|
{
|
|
|
|
|
|
|
|
gdb_cpu_suspend(*pvcpu);
|
|
|
|
return (VMEXIT_CONTINUE);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static vmexit_handler_t handler[VM_EXITCODE_MAX] = {
|
2012-04-28 16:28:00 +00:00
|
|
|
[VM_EXITCODE_INOUT] = vmexit_inout,
|
2014-05-23 05:15:17 +00:00
|
|
|
[VM_EXITCODE_INOUT_STR] = vmexit_inout,
|
2012-04-28 16:28:00 +00:00
|
|
|
[VM_EXITCODE_VMX] = vmexit_vmx,
|
2014-09-14 04:39:04 +00:00
|
|
|
[VM_EXITCODE_SVM] = vmexit_svm,
|
2012-04-28 16:28:00 +00:00
|
|
|
[VM_EXITCODE_BOGUS] = vmexit_bogus,
|
2015-05-28 17:37:01 +00:00
|
|
|
[VM_EXITCODE_REQIDLE] = vmexit_reqidle,
|
2012-04-28 16:28:00 +00:00
|
|
|
[VM_EXITCODE_RDMSR] = vmexit_rdmsr,
|
|
|
|
[VM_EXITCODE_WRMSR] = vmexit_wrmsr,
|
|
|
|
[VM_EXITCODE_MTRAP] = vmexit_mtrap,
|
2013-10-05 21:22:35 +00:00
|
|
|
[VM_EXITCODE_INST_EMUL] = vmexit_inst_emul,
|
2012-09-25 02:33:25 +00:00
|
|
|
[VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap,
|
2014-07-16 21:26:26 +00:00
|
|
|
[VM_EXITCODE_SUSPENDED] = vmexit_suspend,
|
|
|
|
[VM_EXITCODE_TASK_SWITCH] = vmexit_task_switch,
|
2018-05-01 15:17:46 +00:00
|
|
|
[VM_EXITCODE_DEBUG] = vmexit_debug,
|
2011-05-13 04:54:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2015-01-18 03:08:30 +00:00
|
|
|
vm_loop(struct vmctx *ctx, int vcpu, uint64_t startrip)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
2015-12-31 10:55:50 +00:00
|
|
|
int error, rc;
|
2013-07-18 18:40:54 +00:00
|
|
|
enum vm_exitcode exitcode;
|
2014-05-31 23:37:34 +00:00
|
|
|
cpuset_t active_cpus;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2014-05-05 18:06:35 +00:00
|
|
|
if (vcpumap[vcpu] != NULL) {
|
2013-02-11 20:36:07 +00:00
|
|
|
error = pthread_setaffinity_np(pthread_self(),
|
2014-05-05 18:06:35 +00:00
|
|
|
sizeof(cpuset_t), vcpumap[vcpu]);
|
2011-05-13 04:54:01 +00:00
|
|
|
assert(error == 0);
|
|
|
|
}
|
|
|
|
|
2014-05-31 23:37:34 +00:00
|
|
|
error = vm_active_cpus(ctx, &active_cpus);
|
|
|
|
assert(CPU_ISSET(vcpu, &active_cpus));
|
|
|
|
|
2015-01-18 03:08:30 +00:00
|
|
|
error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RIP, startrip);
|
|
|
|
assert(error == 0);
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
while (1) {
|
2015-01-18 03:08:30 +00:00
|
|
|
error = vm_run(ctx, vcpu, &vmexit[vcpu]);
|
2013-12-22 20:29:59 +00:00
|
|
|
if (error != 0)
|
|
|
|
break;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2013-07-18 18:40:54 +00:00
|
|
|
exitcode = vmexit[vcpu].exitcode;
|
|
|
|
if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) {
|
|
|
|
fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n",
|
|
|
|
exitcode);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-12-31 07:08:21 +00:00
|
|
|
rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu);
|
2013-07-18 18:40:54 +00:00
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
switch (rc) {
|
|
|
|
case VMEXIT_CONTINUE:
|
|
|
|
break;
|
2014-05-12 23:35:10 +00:00
|
|
|
case VMEXIT_ABORT:
|
|
|
|
abort();
|
2011-05-13 04:54:01 +00:00
|
|
|
default:
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, "vm_run error %d, errno %d\n", error, errno);
|
|
|
|
}
|
|
|
|
|
2013-01-04 02:04:41 +00:00
|
|
|
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);
|
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2013-10-16 18:20:27 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-20 01:48:25 +00:00
|
|
|
if (x2apic_mode)
|
2013-10-16 18:20:27 +00:00
|
|
|
err = vm_set_x2apic_state(ctx, cpu, X2APIC_ENABLED);
|
2014-02-20 01:48:25 +00:00
|
|
|
else
|
|
|
|
err = vm_set_x2apic_state(ctx, cpu, X2APIC_DISABLED);
|
2013-10-16 18:20:27 +00:00
|
|
|
|
|
|
|
if (err) {
|
|
|
|
fprintf(stderr, "Unable to set x2apic state (%d)\n", err);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
vm_set_capability(ctx, cpu, VM_CAP_ENABLE_INVPCID, 1);
|
|
|
|
}
|
|
|
|
|
2015-06-18 06:00:17 +00:00
|
|
|
static struct vmctx *
|
|
|
|
do_open(const char *vmname)
|
|
|
|
{
|
|
|
|
struct vmctx *ctx;
|
|
|
|
int error;
|
|
|
|
bool reinit, romboot;
|
2017-02-14 13:35:59 +00:00
|
|
|
#ifndef WITHOUT_CAPSICUM
|
|
|
|
cap_rights_t rights;
|
|
|
|
const cap_ioctl_t *cmds;
|
|
|
|
size_t ncmds;
|
|
|
|
#endif
|
2015-06-18 06:00:17 +00:00
|
|
|
|
|
|
|
reinit = romboot = false;
|
|
|
|
|
|
|
|
if (lpc_bootrom())
|
|
|
|
romboot = true;
|
|
|
|
|
|
|
|
error = vm_create(vmname);
|
|
|
|
if (error) {
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
if (romboot) {
|
|
|
|
reinit = true;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* The virtual machine has been setup by the
|
|
|
|
* userspace bootloader.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
perror("vm_create");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!romboot) {
|
|
|
|
/*
|
|
|
|
* If the virtual machine was just created then a
|
|
|
|
* bootrom must be configured to boot it.
|
|
|
|
*/
|
|
|
|
fprintf(stderr, "virtual machine cannot be booted\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = vm_open(vmname);
|
|
|
|
if (ctx == NULL) {
|
|
|
|
perror("vm_open");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-02-14 13:35:59 +00:00
|
|
|
#ifndef WITHOUT_CAPSICUM
|
|
|
|
cap_rights_init(&rights, CAP_IOCTL, CAP_MMAP_RW);
|
|
|
|
if (cap_rights_limit(vm_get_device_fd(ctx), &rights) == -1 &&
|
|
|
|
errno != ENOSYS)
|
|
|
|
errx(EX_OSERR, "Unable to apply rights for sandbox");
|
|
|
|
vm_get_ioctls(&ncmds);
|
|
|
|
cmds = vm_get_ioctls(NULL);
|
|
|
|
if (cmds == NULL)
|
|
|
|
errx(EX_OSERR, "out of memory");
|
|
|
|
if (cap_ioctls_limit(vm_get_device_fd(ctx), cmds, ncmds) == -1 &&
|
|
|
|
errno != ENOSYS)
|
|
|
|
errx(EX_OSERR, "Unable to apply rights for sandbox");
|
|
|
|
free((cap_ioctl_t *)cmds);
|
|
|
|
#endif
|
|
|
|
|
2015-06-18 06:00:17 +00:00
|
|
|
if (reinit) {
|
|
|
|
error = vm_reinit(ctx);
|
|
|
|
if (error) {
|
|
|
|
perror("vm_reinit");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
Add the ability to control the CPU topology of created VMs
from userland without the need to use sysctls, it allows the old
sysctls to continue to function, but deprecates them at
FreeBSD_version 1200060 (Relnotes for deprecate).
The command line of bhyve is maintained in a backwards compatible way.
The API of libvmmapi is maintained in a backwards compatible way.
The sysctl's are maintained in a backwards compatible way.
Added command option looks like:
bhyve -c [[cpus=]n][,sockets=n][,cores=n][,threads=n][,maxcpus=n]
The optional parts can be specified in any order, but only a single
integer invokes the backwards compatible parse. [,maxcpus=n] is
hidden by #ifdef until kernel support is added, though the api
is put in place.
bhyvectl --get-cpu-topology option added.
Reviewed by: grehan (maintainer, earlier version),
Reviewed by: bcr (manpages)
Approved by: bde (mentor), phk (mentor)
Tested by: Oleg Ginzburg <olevole@olevole.ru> (cbsd)
MFC after: 1 week
Relnotes: Y
Differential Revision: https://reviews.freebsd.org/D9930
2018-04-08 19:24:49 +00:00
|
|
|
error = vm_set_topology(ctx, sockets, cores, threads, maxcpus);
|
|
|
|
if (error)
|
|
|
|
errx(EX_OSERR, "vm_set_topology");
|
2015-06-18 06:00:17 +00:00
|
|
|
return (ctx);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2018-05-01 15:17:46 +00:00
|
|
|
int c, error, dbg_port, gdb_port, err, bvmcons;
|
2015-06-18 06:00:17 +00:00
|
|
|
int max_vcpus, mptgen, memflags;
|
2015-02-24 02:04:16 +00:00
|
|
|
int rtc_localtime;
|
2018-05-01 15:17:46 +00:00
|
|
|
bool gdb_stop;
|
2011-05-13 04:54:01 +00:00
|
|
|
struct vmctx *ctx;
|
|
|
|
uint64_t rip;
|
2013-03-18 22:38:30 +00:00
|
|
|
size_t memsize;
|
2015-06-18 06:00:17 +00:00
|
|
|
char *optstr;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2012-10-27 22:33:23 +00:00
|
|
|
bvmcons = 0;
|
2011-05-13 04:54:01 +00:00
|
|
|
progname = basename(argv[0]);
|
2018-05-01 15:17:46 +00:00
|
|
|
dbg_port = 0;
|
2013-10-08 16:36:17 +00:00
|
|
|
gdb_port = 0;
|
2018-05-01 15:17:46 +00:00
|
|
|
gdb_stop = false;
|
2011-05-13 04:54:01 +00:00
|
|
|
guest_ncpus = 1;
|
Add the ability to control the CPU topology of created VMs
from userland without the need to use sysctls, it allows the old
sysctls to continue to function, but deprecates them at
FreeBSD_version 1200060 (Relnotes for deprecate).
The command line of bhyve is maintained in a backwards compatible way.
The API of libvmmapi is maintained in a backwards compatible way.
The sysctl's are maintained in a backwards compatible way.
Added command option looks like:
bhyve -c [[cpus=]n][,sockets=n][,cores=n][,threads=n][,maxcpus=n]
The optional parts can be specified in any order, but only a single
integer invokes the backwards compatible parse. [,maxcpus=n] is
hidden by #ifdef until kernel support is added, though the api
is put in place.
bhyvectl --get-cpu-topology option added.
Reviewed by: grehan (maintainer, earlier version),
Reviewed by: bcr (manpages)
Approved by: bde (mentor), phk (mentor)
Tested by: Oleg Ginzburg <olevole@olevole.ru> (cbsd)
MFC after: 1 week
Relnotes: Y
Differential Revision: https://reviews.freebsd.org/D9930
2018-04-08 19:24:49 +00:00
|
|
|
sockets = cores = threads = 1;
|
|
|
|
maxcpus = 0;
|
2013-03-18 22:38:30 +00:00
|
|
|
memsize = 256 * MB;
|
2014-05-02 04:51:31 +00:00
|
|
|
mptgen = 1;
|
2015-02-24 02:04:16 +00:00
|
|
|
rtc_localtime = 1;
|
2015-06-18 06:00:17 +00:00
|
|
|
memflags = 0;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2018-05-01 15:17:46 +00:00
|
|
|
optstr = "abehuwxACHIPSWYp:g:G:c:s:m:l:U:";
|
2015-06-18 06:00:17 +00:00
|
|
|
while ((c = getopt(argc, argv, optstr)) != -1) {
|
2011-05-13 04:54:01 +00:00
|
|
|
switch (c) {
|
2012-09-26 00:06:17 +00:00
|
|
|
case 'a':
|
2014-02-20 01:48:25 +00:00
|
|
|
x2apic_mode = 0;
|
2012-09-26 00:06:17 +00:00
|
|
|
break;
|
2012-11-20 07:01:26 +00:00
|
|
|
case 'A':
|
|
|
|
acpi = 1;
|
|
|
|
break;
|
2012-10-27 22:33:23 +00:00
|
|
|
case 'b':
|
|
|
|
bvmcons = 1;
|
|
|
|
break;
|
2011-05-13 04:54:01 +00:00
|
|
|
case 'p':
|
2014-05-05 18:06:35 +00:00
|
|
|
if (pincpu_parse(optarg) != 0) {
|
|
|
|
errx(EX_USAGE, "invalid vcpu pinning "
|
|
|
|
"configuration '%s'", optarg);
|
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
Add the ability to control the CPU topology of created VMs
from userland without the need to use sysctls, it allows the old
sysctls to continue to function, but deprecates them at
FreeBSD_version 1200060 (Relnotes for deprecate).
The command line of bhyve is maintained in a backwards compatible way.
The API of libvmmapi is maintained in a backwards compatible way.
The sysctl's are maintained in a backwards compatible way.
Added command option looks like:
bhyve -c [[cpus=]n][,sockets=n][,cores=n][,threads=n][,maxcpus=n]
The optional parts can be specified in any order, but only a single
integer invokes the backwards compatible parse. [,maxcpus=n] is
hidden by #ifdef until kernel support is added, though the api
is put in place.
bhyvectl --get-cpu-topology option added.
Reviewed by: grehan (maintainer, earlier version),
Reviewed by: bcr (manpages)
Approved by: bde (mentor), phk (mentor)
Tested by: Oleg Ginzburg <olevole@olevole.ru> (cbsd)
MFC after: 1 week
Relnotes: Y
Differential Revision: https://reviews.freebsd.org/D9930
2018-04-08 19:24:49 +00:00
|
|
|
if (topology_parse(optarg) != 0) {
|
|
|
|
errx(EX_USAGE, "invalid cpu topology "
|
|
|
|
"'%s'", optarg);
|
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
break;
|
2014-05-13 16:40:27 +00:00
|
|
|
case 'C':
|
2015-06-18 06:00:17 +00:00
|
|
|
memflags |= VM_MEM_F_INCORE;
|
2014-05-13 16:40:27 +00:00
|
|
|
break;
|
2011-05-13 04:54:01 +00:00
|
|
|
case 'g':
|
2018-05-01 15:17:46 +00:00
|
|
|
dbg_port = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
if (optarg[0] == 'w') {
|
|
|
|
gdb_stop = true;
|
|
|
|
optarg++;
|
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
gdb_port = atoi(optarg);
|
|
|
|
break;
|
2013-10-29 00:18:11 +00:00
|
|
|
case 'l':
|
|
|
|
if (lpc_device_parse(optarg) != 0) {
|
|
|
|
errx(EX_USAGE, "invalid lpc device "
|
|
|
|
"configuration '%s'", optarg);
|
|
|
|
}
|
|
|
|
break;
|
2011-05-13 04:54:01 +00:00
|
|
|
case 's':
|
2014-01-27 22:26:15 +00:00
|
|
|
if (pci_parse_slot(optarg) != 0)
|
2013-04-26 02:24:50 +00:00
|
|
|
exit(1);
|
|
|
|
else
|
|
|
|
break;
|
2015-06-18 06:00:17 +00:00
|
|
|
case 'S':
|
|
|
|
memflags |= VM_MEM_F_WIRED;
|
|
|
|
break;
|
2011-05-13 04:54:01 +00:00
|
|
|
case 'm':
|
2013-10-09 03:56:07 +00:00
|
|
|
error = vm_parse_memsize(optarg, &memsize);
|
|
|
|
if (error)
|
|
|
|
errx(EX_USAGE, "invalid memsize '%s'", optarg);
|
2011-05-13 04:54:01 +00:00
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
guest_vmexit_on_hlt = 1;
|
|
|
|
break;
|
2012-08-04 22:48:04 +00:00
|
|
|
case 'I':
|
2013-10-31 05:44:45 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-08-04 22:48:04 +00:00
|
|
|
break;
|
2011-05-13 04:54:01 +00:00
|
|
|
case 'P':
|
|
|
|
guest_vmexit_on_pause = 1;
|
|
|
|
break;
|
2011-05-19 21:53:25 +00:00
|
|
|
case 'e':
|
|
|
|
strictio = 1;
|
|
|
|
break;
|
2015-02-24 02:04:16 +00:00
|
|
|
case 'u':
|
|
|
|
rtc_localtime = 0;
|
|
|
|
break;
|
2014-03-04 17:12:06 +00:00
|
|
|
case 'U':
|
|
|
|
guest_uuid_str = optarg;
|
|
|
|
break;
|
2013-12-19 22:27:28 +00:00
|
|
|
case 'w':
|
|
|
|
strictmsr = 0;
|
|
|
|
break;
|
2013-10-17 22:01:17 +00:00
|
|
|
case 'W':
|
|
|
|
virtio_msix = 0;
|
|
|
|
break;
|
2014-02-20 01:48:25 +00:00
|
|
|
case 'x':
|
|
|
|
x2apic_mode = 1;
|
|
|
|
break;
|
2014-05-02 04:51:31 +00:00
|
|
|
case 'Y':
|
|
|
|
mptgen = 0;
|
|
|
|
break;
|
2011-05-13 04:54:01 +00:00
|
|
|
case 'h':
|
|
|
|
usage(0);
|
|
|
|
default:
|
|
|
|
usage(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc != 1)
|
|
|
|
usage(1);
|
|
|
|
|
|
|
|
vmname = argv[0];
|
2015-06-18 06:00:17 +00:00
|
|
|
ctx = do_open(vmname);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2013-01-04 02:04:41 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-10-16 18:20:27 +00:00
|
|
|
fbsdrun_set_capabilities(ctx, BSP);
|
2012-09-26 00:06:17 +00:00
|
|
|
|
2015-06-18 06:00:17 +00:00
|
|
|
vm_set_memflags(ctx, memflags);
|
2013-03-18 22:38:30 +00:00
|
|
|
err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
|
|
|
|
if (err) {
|
2015-06-18 06:00:17 +00:00
|
|
|
fprintf(stderr, "Unable to setup memory (%d)\n", errno);
|
2013-03-18 22:38:30 +00:00
|
|
|
exit(1);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
2014-09-20 21:46:31 +00:00
|
|
|
error = init_msr();
|
|
|
|
if (error) {
|
|
|
|
fprintf(stderr, "init_msr error %d", error);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-04-10 18:59:20 +00:00
|
|
|
init_mem();
|
2011-05-13 04:54:01 +00:00
|
|
|
init_inout();
|
Initial bhyve native graphics support.
This adds emulations for a raw framebuffer device, PS2 keyboard/mouse,
XHCI USB controller and a USB tablet.
A simple VNC server is provided for keyboard/mouse input, and graphics
output.
A VGA emulation is included, but is currently disconnected until an
additional bhyve change to block out VGA memory is committed.
Credits:
- raw framebuffer, VNC server, XHCI controller, USB bus/device emulation
and UEFI f/w support by Leon Dang
- VGA, console/g, initial VNC server by tychon@
- PS2 keyboard/mouse jointly done by tychon@ and Leon Dang
- hypervisor framebuffer mem support by neel@
Tested by: Michael Dexter, in a number of revisions of this code.
With the appropriate UEFI image, FreeBSD, Windows and Linux guests can
installed and run in graphics mode using the UEFI/GOP framebuffer.
2016-05-27 06:30:35 +00:00
|
|
|
atkbdc_init(ctx);
|
2014-05-15 14:16:55 +00:00
|
|
|
pci_irq_init(ctx);
|
2014-01-29 14:56:48 +00:00
|
|
|
ioapic_init(ctx);
|
2013-07-04 05:35:56 +00:00
|
|
|
|
2015-02-24 02:04:16 +00:00
|
|
|
rtc_init(ctx, rtc_localtime);
|
2014-05-15 14:16:55 +00:00
|
|
|
sci_init(ctx);
|
2013-07-11 03:54:35 +00:00
|
|
|
|
2013-07-04 05:35:56 +00:00
|
|
|
/*
|
2016-11-08 23:59:41 +00:00
|
|
|
* Exit if a device emulation finds an error in its initilization
|
2013-07-04 05:35:56 +00:00
|
|
|
*/
|
|
|
|
if (init_pci(ctx) != 0)
|
|
|
|
exit(1);
|
|
|
|
|
2018-05-01 15:17:46 +00:00
|
|
|
if (dbg_port != 0)
|
|
|
|
init_dbgport(dbg_port);
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
if (gdb_port != 0)
|
2018-05-01 15:17:46 +00:00
|
|
|
init_gdb(ctx, gdb_port, gdb_stop);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2012-10-27 22:33:23 +00:00
|
|
|
if (bvmcons)
|
|
|
|
init_bvmcons();
|
|
|
|
|
2015-06-18 06:00:17 +00:00
|
|
|
if (lpc_bootrom()) {
|
|
|
|
if (vm_set_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, 1)) {
|
|
|
|
fprintf(stderr, "ROM boot failed: unrestricted guest "
|
|
|
|
"capability not available\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
error = vcpu_reset(ctx, BSP);
|
|
|
|
assert(error == 0);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip);
|
|
|
|
assert(error == 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* build the guest tables, MP etc.
|
|
|
|
*/
|
2014-05-02 04:51:31 +00:00
|
|
|
if (mptgen) {
|
|
|
|
error = mptable_build(ctx, guest_ncpus);
|
|
|
|
if (error)
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2014-03-04 17:12:06 +00:00
|
|
|
error = smbios_build(ctx);
|
|
|
|
assert(error == 0);
|
|
|
|
|
2012-11-20 07:01:26 +00:00
|
|
|
if (acpi) {
|
2013-10-31 05:44:45 +00:00
|
|
|
error = acpi_build(ctx, guest_ncpus);
|
2012-11-20 07:01:26 +00:00
|
|
|
assert(error == 0);
|
|
|
|
}
|
|
|
|
|
2015-10-02 21:09:49 +00:00
|
|
|
if (lpc_bootrom())
|
|
|
|
fwctl_init();
|
|
|
|
|
2017-02-14 13:35:59 +00:00
|
|
|
#ifndef WITHOUT_CAPSICUM
|
|
|
|
caph_cache_catpages();
|
|
|
|
|
|
|
|
if (caph_limit_stdout() == -1 || caph_limit_stderr() == -1)
|
|
|
|
errx(EX_OSERR, "Unable to apply rights for sandbox");
|
|
|
|
|
|
|
|
if (cap_enter() == -1 && errno != ENOSYS)
|
|
|
|
errx(EX_OSERR, "cap_enter() failed");
|
|
|
|
#endif
|
|
|
|
|
2013-11-06 00:25:17 +00:00
|
|
|
/*
|
|
|
|
* Change the proc title to include the VM name.
|
|
|
|
*/
|
|
|
|
setproctitle("%s", vmname);
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
/*
|
|
|
|
* Add CPU 0
|
|
|
|
*/
|
2014-03-20 18:15:37 +00:00
|
|
|
fbsdrun_addcpu(ctx, BSP, BSP, rip);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Head off to the main event dispatch loop
|
|
|
|
*/
|
|
|
|
mevent_dispatch();
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|