Simplify the assignment of memory to virtual machines by requiring a single

command line option "-m <memsize in MB>" to specify the memory size.

Prior to this change the user needed to explicitly specify the amount of
memory allocated below 4G (-m <lowmem>) and the amount above 4G (-M <highmem>).

The "-M" option is no longer supported by 'bhyveload' and 'bhyve'.

The start of the PCI hole is fixed at 3GB and cannot be directly changed
using command line options. However it is still possible to change this in
special circumstances via the 'vm_set_lowmem_limit()' API provided by
libvmmapi.

Submitted by:	Dinakar Medavaram (initial version)
Reviewed by:	grehan
Obtained from:	NetApp
This commit is contained in:
Neel Natu 2013-03-18 22:38:30 +00:00
parent a3d8ae5d2d
commit b060ba5024
12 changed files with 181 additions and 170 deletions

View File

@ -48,8 +48,16 @@ __FBSDID("$FreeBSD$");
#include "vmmapi.h" #include "vmmapi.h"
#define GB (1024 * 1024 * 1024UL)
struct vmctx { struct vmctx {
int fd; int fd;
uint32_t lowmem_limit;
enum vm_mmap_style vms;
size_t lowmem;
char *lowmem_addr;
size_t highmem;
char *highmem_addr;
char *name; char *name;
}; };
@ -90,6 +98,7 @@ vm_open(const char *name)
assert(vm != NULL); assert(vm != NULL);
vm->fd = -1; vm->fd = -1;
vm->lowmem_limit = 3 * GB;
vm->name = (char *)(vm + 1); vm->name = (char *)(vm + 1);
strcpy(vm->name, name); strcpy(vm->name, name);
@ -151,8 +160,22 @@ vm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, size_t *ret_len)
return (error); return (error);
} }
int uint32_t
vm_setup_memory(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char **mapaddr) vm_get_lowmem_limit(struct vmctx *ctx)
{
return (ctx->lowmem_limit);
}
void
vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit)
{
ctx->lowmem_limit = limit;
}
static int
setup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char **addr)
{ {
int error; int error;
struct vm_memory_segment seg; struct vm_memory_segment seg;
@ -165,20 +188,69 @@ vm_setup_memory(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char **mapaddr)
seg.gpa = gpa; seg.gpa = gpa;
seg.len = len; seg.len = len;
error = ioctl(ctx->fd, VM_MAP_MEMORY, &seg); error = ioctl(ctx->fd, VM_MAP_MEMORY, &seg);
if (error == 0 && mapaddr != NULL) { if (error == 0 && addr != NULL) {
*mapaddr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, *addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
ctx->fd, gpa); ctx->fd, gpa);
} }
return (error); return (error);
} }
char * int
vm_map_memory(struct vmctx *ctx, vm_paddr_t gpa, size_t len) vm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms)
{
char **addr;
int error;
/* XXX VM_MMAP_SPARSE not implemented yet */
assert(vms == VM_MMAP_NONE || vms == VM_MMAP_ALL);
ctx->vms = vms;
/*
* If 'memsize' cannot fit entirely in the 'lowmem' segment then
* create another 'highmem' segment above 4GB for the remainder.
*/
if (memsize > ctx->lowmem_limit) {
ctx->lowmem = ctx->lowmem_limit;
ctx->highmem = memsize - ctx->lowmem;
} else {
ctx->lowmem = memsize;
ctx->highmem = 0;
}
if (ctx->lowmem > 0) {
addr = (vms == VM_MMAP_ALL) ? &ctx->lowmem_addr : NULL;
error = setup_memory_segment(ctx, 0, ctx->lowmem, addr);
if (error)
return (error);
}
if (ctx->highmem > 0) {
addr = (vms == VM_MMAP_ALL) ? &ctx->highmem_addr : NULL;
error = setup_memory_segment(ctx, 4*GB, ctx->highmem, addr);
if (error)
return (error);
}
return (0);
}
void *
vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len)
{ {
/* Map 'len' bytes of memory at guest physical address 'gpa' */ /* XXX VM_MMAP_SPARSE not implemented yet */
return ((char *)mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, assert(ctx->vms == VM_MMAP_ALL);
ctx->fd, gpa));
if (gaddr < ctx->lowmem && gaddr + len <= ctx->lowmem)
return ((void *)(ctx->lowmem_addr + gaddr));
if (gaddr >= 4*GB) {
gaddr -= 4*GB;
if (gaddr < ctx->highmem && gaddr + len <= ctx->highmem)
return ((void *)(ctx->highmem_addr + gaddr));
}
return (NULL);
} }
int int

View File

@ -32,24 +32,26 @@
struct vmctx; struct vmctx;
enum x2apic_state; enum x2apic_state;
/*
* Different styles of mapping the memory assigned to a VM into the address
* space of the controlling process.
*/
enum vm_mmap_style {
VM_MMAP_NONE, /* no mapping */
VM_MMAP_ALL, /* fully and statically mapped */
VM_MMAP_SPARSE, /* mappings created on-demand */
};
int vm_create(const char *name); int vm_create(const char *name);
struct vmctx *vm_open(const char *name); struct vmctx *vm_open(const char *name);
void vm_destroy(struct vmctx *ctx); void vm_destroy(struct vmctx *ctx);
size_t vmm_get_mem_total(void); size_t vmm_get_mem_total(void);
size_t vmm_get_mem_free(void); size_t vmm_get_mem_free(void);
int vm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, size_t *ret_len); int vm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, size_t *ret_len);
/* int vm_setup_memory(struct vmctx *ctx, size_t len, enum vm_mmap_style s);
* Create a memory segment of 'len' bytes in the guest physical address space void *vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len);
* at offset 'gpa'. uint32_t vm_get_lowmem_limit(struct vmctx *ctx);
* void vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit);
* If 'mapaddr' is not NULL then this region is mmap'ed into the address
* space of the calling process. If there is an mmap error then *mapaddr
* will be set to MAP_FAILED.
*/
int vm_setup_memory(struct vmctx *ctx, vm_paddr_t gpa, size_t len,
char **mapaddr);
char * vm_map_memory(struct vmctx *ctx, vm_paddr_t gpa, size_t len);
int vm_set_desc(struct vmctx *ctx, int vcpu, int reg, int vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
uint64_t base, uint32_t limit, uint32_t access); uint64_t base, uint32_t limit, uint32_t access);
int vm_get_desc(struct vmctx *ctx, int vcpu, int reg, int vm_get_desc(struct vmctx *ctx, int vcpu, int reg,

View File

@ -680,29 +680,26 @@ basl_end(struct basl_fio *in, struct basl_fio *out)
} }
static int static int
basl_load(int fd, uint64_t off) basl_load(struct vmctx *ctx, int fd, uint64_t off)
{ {
struct stat sb; struct stat sb;
void *gaddr; void *gaddr;
int err;
err = 0; if (fstat(fd, &sb) < 0)
if (fstat(fd, &sb) < 0) { return (errno);
err = errno;
} else {
gaddr = paddr_guest2host(basl_acpi_base + off, sb.st_size);
if (gaddr != NULL) {
if (read(fd, gaddr, sb.st_size) < 0)
err = errno;
} else
err = EFAULT;
}
return (err); gaddr = paddr_guest2host(ctx, basl_acpi_base + off, sb.st_size);
if (gaddr == NULL)
return (EFAULT);
if (read(fd, gaddr, sb.st_size) < 0)
return (errno);
return (0);
} }
static int static int
basl_compile(int (*fwrite_section)(FILE *fp), uint64_t offset) basl_compile(struct vmctx *ctx, int (*fwrite_section)(FILE *), uint64_t offset)
{ {
struct basl_fio io[2]; struct basl_fio io[2];
static char iaslbuf[3*MAXPATHLEN + 10]; static char iaslbuf[3*MAXPATHLEN + 10];
@ -736,7 +733,7 @@ basl_compile(int (*fwrite_section)(FILE *fp), uint64_t offset)
* Copy the aml output file into guest * Copy the aml output file into guest
* memory at the specified location * memory at the specified location
*/ */
err = basl_load(io[1].fd, offset); err = basl_load(ctx, io[1].fd, offset);
} }
} }
basl_end(&io[0], &io[1]); basl_end(&io[0], &io[1]);
@ -842,7 +839,7 @@ acpi_build(struct vmctx *ctx, int ncpu, int ioapic)
* copying them into guest memory * copying them into guest memory
*/ */
while (!err && basl_ftables[i].wsect != NULL) { while (!err && basl_ftables[i].wsect != NULL) {
err = basl_compile(basl_ftables[i].wsect, err = basl_compile(ctx, basl_ftables[i].wsect,
basl_ftables[i].offset); basl_ftables[i].offset);
i++; i++;
} }

View File

@ -80,9 +80,6 @@ int guest_tslice = DEFAULT_GUEST_TSLICE;
int guest_hz = DEFAULT_GUEST_HZ; int guest_hz = DEFAULT_GUEST_HZ;
char *vmname; char *vmname;
u_long lomem_sz;
u_long himem_sz;
int guest_ncpus; int guest_ncpus;
static int pincpu = -1; static int pincpu = -1;
@ -95,9 +92,6 @@ static int strictio;
static int acpi; static int acpi;
static char *lomem_addr;
static char *himem_addr;
static char *progname; static char *progname;
static const int BSP = 0; static const int BSP = 0;
@ -147,8 +141,7 @@ usage(int code)
" -z: guest hz (default is %d)\n" " -z: guest hz (default is %d)\n"
" -s: <slot,driver,configinfo> PCI slot config\n" " -s: <slot,driver,configinfo> PCI slot config\n"
" -S: <slot,driver,configinfo> legacy PCI slot config\n" " -S: <slot,driver,configinfo> legacy PCI slot config\n"
" -m: lowmem in MB\n" " -m: memory size in MB\n"
" -M: highmem in MB\n"
" -x: mux vcpus to 1 hcpu\n" " -x: mux vcpus to 1 hcpu\n"
" -t: mux vcpu timeslice hz (default %d)\n", " -t: mux vcpu timeslice hz (default %d)\n",
progname, DEFAULT_GDB_PORT, DEFAULT_GUEST_HZ, progname, DEFAULT_GDB_PORT, DEFAULT_GUEST_HZ,
@ -157,19 +150,10 @@ usage(int code)
} }
void * void *
paddr_guest2host(uintptr_t gaddr, size_t len) paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len)
{ {
if (gaddr < lomem_sz && gaddr + len <= lomem_sz) return (vm_map_gpa(ctx, gaddr, len));
return ((void *)(lomem_addr + gaddr));
if (gaddr >= 4*GB) {
gaddr -= 4*GB;
if (gaddr < himem_sz && gaddr + len <= himem_sz)
return ((void *)(himem_addr + gaddr));
}
return (NULL);
} }
int int
@ -604,6 +588,7 @@ main(int argc, char *argv[])
int max_vcpus; int max_vcpus;
struct vmctx *ctx; struct vmctx *ctx;
uint64_t rip; uint64_t rip;
size_t memsize;
bvmcons = 0; bvmcons = 0;
inject_bkpt = 0; inject_bkpt = 0;
@ -611,8 +596,9 @@ main(int argc, char *argv[])
gdb_port = DEFAULT_GDB_PORT; gdb_port = DEFAULT_GDB_PORT;
guest_ncpus = 1; guest_ncpus = 1;
ioapic = 0; ioapic = 0;
memsize = 256 * MB;
while ((c = getopt(argc, argv, "abehABHIPxp:g:c:z:s:S:n:m:M:")) != -1) { while ((c = getopt(argc, argv, "abehABHIPxp:g:c:z:s:S:n:m:")) != -1) {
switch (c) { switch (c) {
case 'a': case 'a':
disable_x2apic = 1; disable_x2apic = 1;
@ -651,10 +637,7 @@ main(int argc, char *argv[])
pci_parse_slot(optarg, 1); pci_parse_slot(optarg, 1);
break; break;
case 'm': case 'm':
lomem_sz = strtoul(optarg, NULL, 0) * MB; memsize = strtoul(optarg, NULL, 0) * MB;
break;
case 'M':
himem_sz = strtoul(optarg, NULL, 0) * MB;
break; break;
case 'H': case 'H':
guest_vmexit_on_hlt = 1; guest_vmexit_on_hlt = 1;
@ -739,17 +722,10 @@ main(int argc, char *argv[])
exit(1); exit(1);
} }
if (lomem_sz != 0) { err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
lomem_addr = vm_map_memory(ctx, 0, lomem_sz); if (err) {
if (lomem_addr == (char *) MAP_FAILED) { fprintf(stderr, "Unable to setup memory (%d)\n", err);
lomem_sz = 0; exit(1);
} else if (himem_sz != 0) {
himem_addr = vm_map_memory(ctx, 4*GB, himem_sz);
if (himem_addr == (char *) MAP_FAILED) {
lomem_sz = 0;
himem_sz = 0;
}
}
} }
init_inout(); init_inout();

View File

@ -41,9 +41,7 @@ extern int guest_tslice;
extern int guest_ncpus; extern int guest_ncpus;
extern char *vmname; extern char *vmname;
extern u_long lomem_sz, himem_sz; void *paddr_guest2host(struct vmctx *ctx, uintptr_t addr, size_t len);
void *paddr_guest2host(uintptr_t addr, size_t len);
void fbsdrun_addcpu(struct vmctx *ctx, int cpu, uint64_t rip); void fbsdrun_addcpu(struct vmctx *ctx, int cpu, uint64_t rip);
int fbsdrun_muxed(void); int fbsdrun_muxed(void);

View File

@ -349,7 +349,7 @@ mptable_build(struct vmctx *ctx, int ncpu, int ioapic)
char *curraddr; char *curraddr;
char *startaddr; char *startaddr;
startaddr = paddr_guest2host(MPTABLE_BASE, MPTABLE_MAX_LENGTH); startaddr = paddr_guest2host(ctx, MPTABLE_BASE, MPTABLE_MAX_LENGTH);
if (startaddr == NULL) { if (startaddr == NULL) {
printf("mptable requires mapped mem\n"); printf("mptable requires mapped mem\n");
return (ENOMEM); return (ENOMEM);

View File

@ -86,6 +86,8 @@ static struct lirqinfo {
SET_DECLARE(pci_devemu_set, struct pci_devemu); SET_DECLARE(pci_devemu_set, struct pci_devemu);
static uint32_t pci_hole_startaddr;
static uint64_t pci_emul_iobase; static uint64_t pci_emul_iobase;
static uint64_t pci_emul_membase32; static uint64_t pci_emul_membase32;
static uint64_t pci_emul_membase64; static uint64_t pci_emul_membase64;
@ -93,7 +95,6 @@ static uint64_t pci_emul_membase64;
#define PCI_EMUL_IOBASE 0x2000 #define PCI_EMUL_IOBASE 0x2000
#define PCI_EMUL_IOLIMIT 0x10000 #define PCI_EMUL_IOLIMIT 0x10000
#define PCI_EMUL_MEMBASE32 (lomem_sz)
#define PCI_EMUL_MEMLIMIT32 0xE0000000 /* 3.5GB */ #define PCI_EMUL_MEMLIMIT32 0xE0000000 /* 3.5GB */
#define PCI_EMUL_MEMBASE64 0xD000000000UL #define PCI_EMUL_MEMBASE64 0xD000000000UL
@ -870,8 +871,10 @@ init_pci(struct vmctx *ctx)
int slot, func; int slot, func;
int error; int error;
pci_hole_startaddr = vm_get_lowmem_limit(ctx);
pci_emul_iobase = PCI_EMUL_IOBASE; pci_emul_iobase = PCI_EMUL_IOBASE;
pci_emul_membase32 = PCI_EMUL_MEMBASE32; pci_emul_membase32 = pci_hole_startaddr;
pci_emul_membase64 = PCI_EMUL_MEMBASE64; pci_emul_membase64 = PCI_EMUL_MEMBASE64;
for (slot = 0; slot < MAXSLOTS; slot++) { for (slot = 0; slot < MAXSLOTS; slot++) {
@ -904,8 +907,8 @@ init_pci(struct vmctx *ctx)
memset(&memp, 0, sizeof(struct mem_range)); memset(&memp, 0, sizeof(struct mem_range));
memp.name = "PCI hole"; memp.name = "PCI hole";
memp.flags = MEM_F_RW; memp.flags = MEM_F_RW;
memp.base = lomem_sz; memp.base = pci_hole_startaddr;
memp.size = (4ULL * 1024 * 1024 * 1024) - lomem_sz; memp.size = (4ULL * 1024 * 1024 * 1024) - pci_hole_startaddr;
memp.handler = pci_emul_fallback_handler; memp.handler = pci_emul_fallback_handler;
error = register_mem_fallback(&memp); error = register_mem_fallback(&memp);

View File

@ -141,6 +141,7 @@ struct pci_vtblk_softc {
uint16_t msix_table_idx_req; uint16_t msix_table_idx_req;
uint16_t msix_table_idx_cfg; uint16_t msix_table_idx_cfg;
}; };
#define vtblk_ctx(sc) ((sc)->vbsc_pi->pi_vmctx)
/* /*
* Return the size of IO BAR that maps virtio header and device specific * Return the size of IO BAR that maps virtio header and device specific
@ -227,13 +228,14 @@ pci_vtblk_proc(struct pci_vtblk_softc *sc, struct vring_hqueue *hq)
assert(nsegs >= 3); assert(nsegs >= 3);
assert(nsegs < VTBLK_MAXSEGS + 2); assert(nsegs < VTBLK_MAXSEGS + 2);
vid = paddr_guest2host(vd->vd_addr, vd->vd_len); vid = paddr_guest2host(vtblk_ctx(sc), vd->vd_addr, vd->vd_len);
assert((vid->vd_flags & VRING_DESC_F_INDIRECT) == 0); assert((vid->vd_flags & VRING_DESC_F_INDIRECT) == 0);
/* /*
* The first descriptor will be the read-only fixed header * The first descriptor will be the read-only fixed header
*/ */
vbh = paddr_guest2host(vid[0].vd_addr, sizeof(struct virtio_blk_hdr)); vbh = paddr_guest2host(vtblk_ctx(sc), vid[0].vd_addr,
sizeof(struct virtio_blk_hdr));
assert(vid[0].vd_len == sizeof(struct virtio_blk_hdr)); assert(vid[0].vd_len == sizeof(struct virtio_blk_hdr));
assert(vid[0].vd_flags & VRING_DESC_F_NEXT); assert(vid[0].vd_flags & VRING_DESC_F_NEXT);
assert((vid[0].vd_flags & VRING_DESC_F_WRITE) == 0); assert((vid[0].vd_flags & VRING_DESC_F_WRITE) == 0);
@ -252,8 +254,8 @@ pci_vtblk_proc(struct pci_vtblk_softc *sc, struct vring_hqueue *hq)
* Build up the iovec based on the guest's data descriptors * Build up the iovec based on the guest's data descriptors
*/ */
for (i = 1, iolen = 0; i < nsegs - 1; i++) { for (i = 1, iolen = 0; i < nsegs - 1; i++) {
iov[i-1].iov_base = paddr_guest2host(vid[i].vd_addr, iov[i-1].iov_base = paddr_guest2host(vtblk_ctx(sc),
vid[i].vd_len); vid[i].vd_addr, vid[i].vd_len);
iov[i-1].iov_len = vid[i].vd_len; iov[i-1].iov_len = vid[i].vd_len;
iolen += vid[i].vd_len; iolen += vid[i].vd_len;
@ -271,7 +273,7 @@ pci_vtblk_proc(struct pci_vtblk_softc *sc, struct vring_hqueue *hq)
} }
/* Lastly, get the address of the status byte */ /* Lastly, get the address of the status byte */
status = paddr_guest2host(vid[nsegs - 1].vd_addr, 1); status = paddr_guest2host(vtblk_ctx(sc), vid[nsegs - 1].vd_addr, 1);
assert(vid[nsegs - 1].vd_len == 1); assert(vid[nsegs - 1].vd_len == 1);
assert((vid[nsegs - 1].vd_flags & VRING_DESC_F_NEXT) == 0); assert((vid[nsegs - 1].vd_flags & VRING_DESC_F_NEXT) == 0);
assert(vid[nsegs - 1].vd_flags & VRING_DESC_F_WRITE); assert(vid[nsegs - 1].vd_flags & VRING_DESC_F_WRITE);
@ -347,7 +349,7 @@ pci_vtblk_ring_init(struct pci_vtblk_softc *sc, uint64_t pfn)
hq = &sc->vbsc_q; hq = &sc->vbsc_q;
hq->hq_size = VTBLK_RINGSZ; hq->hq_size = VTBLK_RINGSZ;
hq->hq_dtable = paddr_guest2host(pfn << VRING_PFN, hq->hq_dtable = paddr_guest2host(vtblk_ctx(sc), pfn << VRING_PFN,
vring_size(VTBLK_RINGSZ)); vring_size(VTBLK_RINGSZ));
hq->hq_avail_flags = (uint16_t *)(hq->hq_dtable + hq->hq_size); hq->hq_avail_flags = (uint16_t *)(hq->hq_dtable + hq->hq_size);
hq->hq_avail_idx = hq->hq_avail_flags + 1; hq->hq_avail_idx = hq->hq_avail_flags + 1;

View File

@ -148,6 +148,7 @@ struct pci_vtnet_softc {
struct vring_hqueue vsc_hq[VTNET_MAXQ]; struct vring_hqueue vsc_hq[VTNET_MAXQ];
uint16_t vsc_msix_table_idx[VTNET_MAXQ]; uint16_t vsc_msix_table_idx[VTNET_MAXQ];
}; };
#define vtnet_ctx(sc) ((sc)->vsc_pi->pi_vmctx)
/* /*
* Return the size of IO BAR that maps virtio header and device specific * Return the size of IO BAR that maps virtio header and device specific
@ -331,7 +332,7 @@ pci_vtnet_tap_rx(struct pci_vtnet_softc *sc)
* Get a pointer to the rx header, and use the * Get a pointer to the rx header, and use the
* data immediately following it for the packet buffer. * data immediately following it for the packet buffer.
*/ */
vrx = paddr_guest2host(vd->vd_addr, vd->vd_len); vrx = paddr_guest2host(vtnet_ctx(sc), vd->vd_addr, vd->vd_len);
buf = (uint8_t *)(vrx + 1); buf = (uint8_t *)(vrx + 1);
len = read(sc->vsc_tapfd, buf, len = read(sc->vsc_tapfd, buf,
@ -439,7 +440,8 @@ pci_vtnet_proctx(struct pci_vtnet_softc *sc, struct vring_hqueue *hq)
for (i = 0, plen = 0; for (i = 0, plen = 0;
i < VTNET_MAXSEGS; i < VTNET_MAXSEGS;
i++, vd = &hq->hq_dtable[vd->vd_next]) { i++, vd = &hq->hq_dtable[vd->vd_next]) {
iov[i].iov_base = paddr_guest2host(vd->vd_addr, vd->vd_len); iov[i].iov_base = paddr_guest2host(vtnet_ctx(sc),
vd->vd_addr, vd->vd_len);
iov[i].iov_len = vd->vd_len; iov[i].iov_len = vd->vd_len;
plen += vd->vd_len; plen += vd->vd_len;
tlen += vd->vd_len; tlen += vd->vd_len;
@ -522,7 +524,7 @@ pci_vtnet_ring_init(struct pci_vtnet_softc *sc, uint64_t pfn)
hq = &sc->vsc_hq[qnum]; hq = &sc->vsc_hq[qnum];
hq->hq_size = pci_vtnet_qsize(qnum); hq->hq_size = pci_vtnet_qsize(qnum);
hq->hq_dtable = paddr_guest2host(pfn << VRING_PFN, hq->hq_dtable = paddr_guest2host(vtnet_ctx(sc), pfn << VRING_PFN,
vring_size(hq->hq_size)); vring_size(hq->hq_size));
hq->hq_avail_flags = (uint16_t *)(hq->hq_dtable + hq->hq_size); hq->hq_avail_flags = (uint16_t *)(hq->hq_dtable + hq->hq_size);
hq->hq_avail_idx = hq->hq_avail_flags + 1; hq->hq_avail_idx = hq->hq_avail_flags + 1;

View File

@ -186,9 +186,8 @@ usage(void)
" [--set-x2apic-state=<state>]\n" " [--set-x2apic-state=<state>]\n"
" [--get-x2apic-state]\n" " [--get-x2apic-state]\n"
" [--unassign-pptdev=<bus/slot/func>]\n" " [--unassign-pptdev=<bus/slot/func>]\n"
" [--set-lowmem=<memory below 4GB in units of MB>]\n" " [--set-mem=<memory in units of MB>]\n"
" [--get-lowmem]\n" " [--get-lowmem]\n"
" [--set-highmem=<memory above 4GB in units of MB>]\n"
" [--get-highmem]\n", " [--get-highmem]\n",
progname); progname);
exit(1); exit(1);
@ -197,7 +196,7 @@ usage(void)
static int get_stats, getcap, setcap, capval; static int get_stats, getcap, setcap, capval;
static const char *capname; static const char *capname;
static int create, destroy, get_lowmem, get_highmem; static int create, destroy, get_lowmem, get_highmem;
static uint64_t lowmem, highmem; static uint64_t memsize;
static int set_cr0, get_cr0, set_cr3, get_cr3, set_cr4, get_cr4; static int set_cr0, get_cr0, set_cr3, get_cr3, set_cr4, get_cr4;
static int set_efer, get_efer; static int set_efer, get_efer;
static int set_dr7, get_dr7; static int set_dr7, get_dr7;
@ -351,8 +350,7 @@ vm_set_vmcs_field(struct vmctx *ctx, int vcpu, int field, uint64_t val)
enum { enum {
VMNAME = 1000, /* avoid collision with return values from getopt */ VMNAME = 1000, /* avoid collision with return values from getopt */
VCPU, VCPU,
SET_LOWMEM, SET_MEM,
SET_HIGHMEM,
SET_EFER, SET_EFER,
SET_CR0, SET_CR0,
SET_CR3, SET_CR3,
@ -400,8 +398,7 @@ main(int argc, char *argv[])
struct option opts[] = { struct option opts[] = {
{ "vm", REQ_ARG, 0, VMNAME }, { "vm", REQ_ARG, 0, VMNAME },
{ "cpu", REQ_ARG, 0, VCPU }, { "cpu", REQ_ARG, 0, VCPU },
{ "set-lowmem", REQ_ARG, 0, SET_LOWMEM }, { "set-mem", REQ_ARG, 0, SET_MEM },
{ "set-highmem",REQ_ARG, 0, SET_HIGHMEM },
{ "set-efer", REQ_ARG, 0, SET_EFER }, { "set-efer", REQ_ARG, 0, SET_EFER },
{ "set-cr0", REQ_ARG, 0, SET_CR0 }, { "set-cr0", REQ_ARG, 0, SET_CR0 },
{ "set-cr3", REQ_ARG, 0, SET_CR3 }, { "set-cr3", REQ_ARG, 0, SET_CR3 },
@ -572,13 +569,9 @@ main(int argc, char *argv[])
case VCPU: case VCPU:
vcpu = atoi(optarg); vcpu = atoi(optarg);
break; break;
case SET_LOWMEM: case SET_MEM:
lowmem = atoi(optarg) * MB; memsize = atoi(optarg) * MB;
lowmem = roundup(lowmem, 2 * MB); memsize = roundup(memsize, 2 * MB);
break;
case SET_HIGHMEM:
highmem = atoi(optarg) * MB;
highmem = roundup(highmem, 2 * MB);
break; break;
case SET_EFER: case SET_EFER:
efer = strtoul(optarg, NULL, 0); efer = strtoul(optarg, NULL, 0);
@ -702,11 +695,8 @@ main(int argc, char *argv[])
error = -1; error = -1;
} }
if (!error && lowmem) if (!error && memsize)
error = vm_setup_memory(ctx, 0, lowmem, NULL); error = vm_setup_memory(ctx, memsize, VM_MMAP_NONE);
if (!error && highmem)
error = vm_setup_memory(ctx, 4 * GB, highmem, NULL);
if (!error && set_efer) if (!error && set_efer)
error = vm_set_register(ctx, vcpu, VM_REG_GUEST_EFER, efer); error = vm_set_register(ctx, vcpu, VM_REG_GUEST_EFER, efer);

View File

@ -35,8 +35,7 @@
guest inside a bhyve virtual machine guest inside a bhyve virtual machine
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl m Ar lowmem .Op Fl m Ar mem-size
.Op Fl M Ar highmem
.Op Fl d Ar disk-path .Op Fl d Ar disk-path
.Op Fl h Ar host-path .Op Fl h Ar host-path
.Ar vmname .Ar vmname
@ -61,22 +60,13 @@ and will be created if it does not already exist.
.Sh OPTIONS .Sh OPTIONS
The following options are available: The following options are available:
.Bl -tag -width indent .Bl -tag -width indent
.It Fl m Ar lowmem .It Fl m Ar mem-size
.Ar lowmem .Ar mem-size
is the amount of memory allocated below 4GB in the guest's physical address is the amount of memory allocated to the guest in units of megabytes.
space.
.Pp .Pp
The default value of The default value of
.Ar lowmem .Ar mem-size
is 256MB. is 256.
.It Fl M Ar highmem
.Ar highmem
is the amount of memory allocated above 4GB in the guest's physical address
space.
.Pp
The default value of
.Ar highmem
is 0MB.
.It Fl d Ar disk-path .It Fl d Ar disk-path
The The
.Ar disk-path .Ar disk-path
@ -93,16 +83,8 @@ that boots off the ISO image
.Pa /freebsd/release.iso .Pa /freebsd/release.iso
and has 1GB memory allocated to it: and has 1GB memory allocated to it:
.Pp .Pp
.Dl "bhyveload -m 256 -M 768 -d /freebsd/release.iso freebsd-vm" .Dl "bhyveload -m 1024 -d /freebsd/release.iso freebsd-vm"
.Pp .Pp
In the example above the 1GB allocation is split in two segments:
.Pp
.Bl -dash -compact
.It
256MB below the 4GB boundary (0MB - 256MB)
.It
768MB above the 4GB boundary (4096MB - 4864MB)
.El
.Sh SEE ALSO .Sh SEE ALSO
.Xr bhyve 4 , .Xr bhyve 4 ,
.Xr bhyve 8 , .Xr bhyve 8 ,

View File

@ -88,8 +88,7 @@ static char *host_base = "/";
static struct termios term, oldterm; static struct termios term, oldterm;
static int disk_fd = -1; static int disk_fd = -1;
static char *vmname, *progname, *membase; static char *vmname, *progname;
static uint64_t lowmem, highmem;
static struct vmctx *ctx; static struct vmctx *ctx;
static uint64_t gdtbase, cr3, rsp; static uint64_t gdtbase, cr3, rsp;
@ -323,30 +322,30 @@ cb_diskioctl(void *arg, int unit, u_long cmd, void *data)
static int static int
cb_copyin(void *arg, const void *from, uint64_t to, size_t size) cb_copyin(void *arg, const void *from, uint64_t to, size_t size)
{ {
char *ptr;
to &= 0x7fffffff; to &= 0x7fffffff;
if (to > lowmem)
ptr = vm_map_gpa(ctx, to, size);
if (ptr == NULL)
return (EFAULT); return (EFAULT);
if (to + size > lowmem)
size = lowmem - to;
memcpy(&membase[to], from, size);
memcpy(ptr, from, size);
return (0); return (0);
} }
static int static int
cb_copyout(void *arg, uint64_t from, void *to, size_t size) cb_copyout(void *arg, uint64_t from, void *to, size_t size)
{ {
char *ptr;
from &= 0x7fffffff; from &= 0x7fffffff;
if (from > lowmem)
ptr = vm_map_gpa(ctx, from, size);
if (ptr == NULL)
return (EFAULT); return (EFAULT);
if (from + size > lowmem)
size = lowmem - from;
memcpy(to, &membase[from], size);
memcpy(to, ptr, size);
return (0); return (0);
} }
@ -493,8 +492,8 @@ static void
cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem) cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem)
{ {
*ret_lowmem = lowmem; vm_get_memory_seg(ctx, 0, ret_lowmem);
*ret_highmem = highmem; vm_get_memory_seg(ctx, 4 * GB, ret_highmem);
} }
static const char * static const char *
@ -551,9 +550,9 @@ static void
usage(void) usage(void)
{ {
printf("usage: %s [-d <disk image path>] [-h <host filesystem path>] " fprintf(stderr,
"[-m <lowmem>][-M <highmem>] " "usage: %s [-m mem-size][-d <disk-path>] [-h <host-path>] "
"<vmname>\n", progname); "<vmname>\n", progname);
exit(1); exit(1);
} }
@ -562,16 +561,16 @@ main(int argc, char** argv)
{ {
void *h; void *h;
void (*func)(struct loader_callbacks *, void *, int, int); void (*func)(struct loader_callbacks *, void *, int, int);
uint64_t mem_size;
int opt, error; int opt, error;
char *disk_image; char *disk_image;
progname = argv[0]; progname = argv[0];
lowmem = 128 * MB; mem_size = 256 * MB;
highmem = 0;
disk_image = NULL; disk_image = NULL;
while ((opt = getopt(argc, argv, "d:h:m:M:")) != -1) { while ((opt = getopt(argc, argv, "d:h:m:")) != -1) {
switch (opt) { switch (opt) {
case 'd': case 'd':
disk_image = optarg; disk_image = optarg;
@ -582,11 +581,7 @@ main(int argc, char** argv)
break; break;
case 'm': case 'm':
lowmem = strtoul(optarg, NULL, 0) * MB; mem_size = strtoul(optarg, NULL, 0) * MB;
break;
case 'M':
highmem = strtoul(optarg, NULL, 0) * MB;
break; break;
case '?': case '?':
@ -615,20 +610,12 @@ main(int argc, char** argv)
exit(1); exit(1);
} }
error = vm_setup_memory(ctx, 0, lowmem, &membase); error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL);
if (error) { if (error) {
perror("vm_setup_memory(lowmem)"); perror("vm_setup_memory");
exit(1); exit(1);
} }
if (highmem != 0) {
error = vm_setup_memory(ctx, 4 * GB, highmem, NULL);
if (error) {
perror("vm_setup_memory(highmem)");
exit(1);
}
}
tcgetattr(0, &term); tcgetattr(0, &term);
oldterm = term; oldterm = term;
term.c_lflag &= ~(ICANON|ECHO); term.c_lflag &= ~(ICANON|ECHO);