Ai
3 Star 2 Fork 0

Gitee 极速下载/hyperkit

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/docker/hyperkit
克隆/下载
hyperkit.c 22.82 KB
一键复制 编辑 原始数据 按行查看 历史
Noel Georgi 提交于 2018-11-26 18:27 +08:00 . Pulling changes from xhyve
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
/*-
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <fcntl.h>
#include <libgen.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <sysexits.h>
#include <ctype.h>
#include <inttypes.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/param.h>
#include <dispatch/dispatch.h>
#include <xhyve/support/misc.h>
#include <xhyve/support/atomic.h>
#include <xhyve/support/segments.h>
#include <xhyve/support/cpuset.h>
#include <xhyve/vmm/vmm_api.h>
#include <xhyve/xhyve.h>
#include <xhyve/acpi.h>
#include <xhyve/inout.h>
#include <xhyve/dbgport.h>
#include <xhyve/ioapic.h>
#include <xhyve/mem.h>
#include <xhyve/mevent.h>
#include <xhyve/mptbl.h>
#include <xhyve/pci_emul.h>
#include <xhyve/pci_irq.h>
#include <xhyve/pci_lpc.h>
#include <xhyve/smbiostbl.h>
#include <xhyve/xmsr.h>
#include <xhyve/rtc.h>
#include <xhyve/fwctl.h>
#include <xhyve/firmware/kexec.h>
#include <xhyve/firmware/fbsd.h>
#include <xhyve/firmware/bootrom.h>
#include <xhyve/firmware/multiboot.h>
#ifdef HAVE_OCAML
#include <caml/callback.h>
#include <caml/threads.h>
#endif
#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;
int print_mac;
char *guest_uuid_str;
static char *pidfile;
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;
static struct mt_vmm_info {
pthread_t mt_thr;
int mt_vcpu;
} mt_vmm_info[VM_MAXCPU];
static uint64_t (*fw_func)(void);
__attribute__ ((noreturn)) static void
usage(int code)
{
fprintf(stderr,
"Usage: %s [-behuwxMACHPWY] [-c vcpus] [-F <pidfile>] [-g <gdb port>] [-l <lpc>]\n"
" %*s [-m mem] [-p vcpu:hostcpu] [-s <pci>] [-U uuid] -f <fw>\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"
" -F: pidfile\n"
" -g: gdb port\n"
" -h: help\n"
" -H: vmexit from the guest on hlt\n"
" -l: LPC device configuration. Ex: -l com1,stdio -l com2,autopty -l com2,/dev/myownpty\n"
" -m: memory size in MB, may be suffixed with one of K, M, G or T\n"
" -M: print MAC address and exit if using vmnet\n"
" -P: vmexit from the guest on pause\n"
" -s: <slot,driver,configinfo> PCI slot config\n"
" -u: RTC keeps UTC time\n"
" -U: uuid\n"
" -v: show build version\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);
}
__attribute__ ((noreturn)) static void
show_version()
{
fprintf(stderr, "%s: %s\n\n%s\n",progname, VERSION,
"Homepage: https://github.com/docker/hyperkit\n"
"License: BSD\n");
exit(0);
}
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;
char ident[16];
mtp = param;
vcpu = mtp->mt_vcpu;
rip_entry = 0xfff0;
snprintf(ident, sizeof(ident), "vcpu:%d", vcpu);
pthread_setname_np(ident);
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 = fw_func();
} 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_POWEROFF:
case VM_SUSPEND_HALT:
exit(0);
case VM_SUSPEND_RESET:
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:
xh_vm_vcpu_dump(vcpu);
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, *opt1 = NULL, *opt2 = NULL, *opt3 = NULL, *cp;
fw = strdup(opt);
if (strncmp(fw, "kexec", strlen("kexec")) == 0) {
fw_func = kexec;
} else if (strncmp(fw, "fbsd", strlen("fbsd")) == 0) {
fw_func = fbsd_load;
} else if (strncmp(fw, "bootrom", strlen("bootrom")) == 0) {
fw_func = bootrom_load;
} else if (strncmp(fw, "multiboot", strlen("multiboot")) == 0) {
fw_func = multiboot;
} else {
goto fail;
}
// Gets first comma-separated option from cur and stores it in next.
#define NEXTARG(cur, next, scratch) do { \
if (cur && (scratch = strchr(cur, ',')) != NULL) { \
*scratch = '\0'; \
next = scratch + 1; \
} \
} while(0)
NEXTARG(fw, opt1, cp);
NEXTARG(opt1, opt2, cp);
NEXTARG(opt2, opt3, cp);
#undef NEXTARG
// Replace zero length options with NULLs
opt1 = opt1 && strlen(opt1) ? opt1 : NULL;
opt2 = opt2 && strlen(opt2) ? opt2 : NULL;
opt3 = opt3 && strlen(opt3) ? opt3 : NULL;
int ret = 1;
if (fw_func == kexec) {
ret = kexec_init(opt1, opt2, opt3);
} else if (fw_func == fbsd_load) {
/* FIXME: let user set boot-loader serial device */
ret = fbsd_init(opt1, opt2, opt3, NULL);
} else if (fw_func == bootrom_load) {
ret = bootrom_init(opt1);
} else if (fw_func == multiboot) {
ret = multiboot_init(opt1, opt2, opt3);
}
if (ret)
goto fail;
return 0;
fail:
fprintf(stderr, "Invalid firmware argument\n"
" -f kexec,'kernel'[,'initrd'][,'\"cmdline\"']\n"
" -f fbsd,'userboot','boot volume'[,'\"kernel env\"']\n"
" -f bootrom,'ROM'\n"
" -f multiboot,'kernel'[,module[;cmdline][:module[;cmdline]]...][,cmdline]\n");
return -1;
}
static void
remove_pidfile()
{
int error;
if (pidfile == NULL)
return;
error = unlink(pidfile);
if (error < 0)
fprintf(stderr, "Failed to remove pidfile\n");
}
static int
setup_pidfile()
{
int f, error, pid;
char pid_str[21];
if (pidfile == NULL)
return 0;
pid = getpid();
error = sprintf(pid_str, "%d", pid);
if (error < 0)
goto fail;
f = open(pidfile, O_CREAT|O_EXCL|O_WRONLY, 0644);
if (f < 0)
goto fail;
error = atexit(remove_pidfile);
if (error < 0) {
close(f);
remove_pidfile();
goto fail;
}
if (0 > (write(f, (void*)pid_str, strlen(pid_str)))) {
close(f);
goto fail;
}
error = close(f);
if (error < 0)
goto fail;
return 0;
fail:
fprintf(stderr, "Failed to set up pidfile\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;
struct sigaction sa_ign;
bvmcons = 0;
dump_guest_memory = 0;
progname = basename(argv[0]);
gdb_port = 0;
guest_ncpus = 1;
print_mac = 0;
memsize = 256 * MB;
mptgen = 1;
rtc_localtime = 1;
fw = 0;
while ((c = getopt(argc, argv, "behvuwxMACHPWY:f: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 'F':
pidfile = optarg;
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 'M':
print_mac = 1;
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 'v':
show_version();
case 'h':
usage(0);
default:
usage(1);
}
}
if (fw != 1)
usage(1);
/*
* We don't want SIGPIPEs ever, be sure to do this before any threads
* are created.
*/
sa_ign.sa_handler = SIG_IGN;
sa_ign.sa_flags = 0;
error = sigaction(SIGPIPE, &sa_ign, NULL);
if (error) {
perror("sigaction(SIGPIPE)");
exit(1);
}
#ifdef HAVE_OCAML
caml_startup(argv) ;
caml_release_runtime_system();
#endif
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);
}
error = setup_pidfile();
if (error) {
fprintf(stderr, "pidfile 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);
}
if (bootrom()) {
fwctl_init();
}
rip = 0;
// Use GCD to register signal handlers. These are not reentrant, so can call xhyve directly
dispatch_source_t sigusr1_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, dispatch_get_global_queue(0, 0));
dispatch_source_t sigusr2_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR2, 0, dispatch_get_global_queue(0, 0));
dispatch_source_set_event_handler(sigusr1_source, ^{
fprintf(stdout, "received sigusr1, pausing\n");
xh_hv_pause(1);
});
dispatch_source_set_event_handler(sigusr2_source, ^{
fprintf(stdout, "received sigusr2, unpausing\n");
xh_hv_pause(0);
});
signal(SIGUSR1, SIG_IGN);
signal(SIGUSR2, SIG_IGN);
dispatch_resume(sigusr1_source);
dispatch_resume(sigusr2_source);
vcpu_add(BSP, BSP, rip);
/*
* Head off to the main event dispatch loop
*/
mevent_dispatch();
exit(1);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C/C++
1
https://gitee.com/mirrors/hyperkit.git
git@gitee.com:mirrors/hyperkit.git
mirrors
hyperkit
hyperkit
master

搜索帮助