Skip to content

Commit b6826b9

Browse files
committed
ukvm_hv_init() is now responsible for guest memory allocation
API change: struct hv *ukvm_hv_init(size_t mem_size). The caller may use (hv->mem) and (hv->mem_size) after calling this function. Rationale: FreeBSD's vmm requires mapping guest memory from the vmm device (as opposed to anonymous memory). Also, we may want to tweak mem_size based on {arch,backend}-specific requirements so this gives us a way to do it. We do however lose the option of mmap()ing arbitrary objects into guest memory, too bad.
1 parent 151eb58 commit b6826b9

3 files changed

Lines changed: 15 additions & 15 deletions

File tree

ukvm/ukvm.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ inline void *ukvm_checked_gpa_p(struct ukvm_hv *hv, ukvm_gpa_t gpa, size_t sz,
6868
}
6969

7070
/*
71-
* Initialise hypervisor, with (mem_size) bytes of guest memory at (mem).
71+
* Initialise hypervisor, with (mem_size) bytes of guest memory.
72+
* (hv->mem) and (hv->mem_size) are valid after this function has been called.
7273
*/
73-
struct ukvm_hv *ukvm_hv_init(uint8_t *mem, size_t mem_size);
74+
struct ukvm_hv *ukvm_hv_init(size_t mem_size);
7475

7576
/*
7677
* Initialise VCPU state with (gpa_ep) as the entry point and (gpa_kend) as the

ukvm/ukvm_hv_kvm.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "ukvm.h"
3232
#include "ukvm_hv_kvm.h"
3333

34-
struct ukvm_hv *ukvm_hv_init(uint8_t *mem, size_t mem_size)
34+
struct ukvm_hv *ukvm_hv_init(size_t mem_size)
3535
{
3636
int ret;
3737

@@ -70,18 +70,22 @@ struct ukvm_hv *ukvm_hv_init(uint8_t *mem, size_t mem_size)
7070
if (hvb->vcpurun == MAP_FAILED)
7171
err(1, "KVM: VCPU mmap failed");
7272

73+
hv->mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE,
74+
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
75+
if (hv->mem == MAP_FAILED)
76+
err(1, "Error allocating guest memory");
77+
hv->mem_size = mem_size;
78+
7379
struct kvm_userspace_memory_region region = {
7480
.slot = 0,
7581
.guest_phys_addr = 0,
76-
.memory_size = mem_size,
77-
.userspace_addr = (uint64_t)mem,
82+
.memory_size = hv->mem_size,
83+
.userspace_addr = (uint64_t)hv->mem,
7884
};
7985
ret = ioctl(hvb->vmfd, KVM_SET_USER_MEMORY_REGION, &region);
8086
if (ret == -1)
8187
err(1, "KVM: ioctl (SET_USER_MEMORY_REGION) failed");
8288

8389
hv->b = hvb;
84-
hv->mem = mem;
85-
hv->mem_size = mem_size;
8690
return hv;
8791
}

ukvm/ukvm_main.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define _GNU_SOURCE
2222
#include <assert.h>
2323
#include <err.h>
24+
#include <libgen.h>
2425
#include <signal.h>
2526
#include <stdint.h>
2627
#include <stdio.h>
@@ -110,7 +111,6 @@ static void usage(const char *prog)
110111

111112
int main(int argc, char **argv)
112113
{
113-
uint8_t *mem;
114114
size_t mem_size = 0x20000000;
115115
ukvm_gpa_t gpa_ep, gpa_kend;
116116
const char *prog;
@@ -163,14 +163,9 @@ int main(int argc, char **argv)
163163
if (sigaction(SIGTERM, &sa, NULL) == -1)
164164
err(1, "Could not install signal handler");
165165

166-
mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE,
167-
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
168-
if (mem == MAP_FAILED)
169-
err(1, "Error allocating guest memory");
166+
struct ukvm_hv *hv = ukvm_hv_init(mem_size);
170167

171-
ukvm_elf_load(elffile, mem, mem_size, &gpa_ep, &gpa_kend);
172-
173-
struct ukvm_hv *hv = ukvm_hv_init(mem, mem_size);
168+
ukvm_elf_load(elffile, hv->mem, hv->mem_size, &gpa_ep, &gpa_kend);
174169

175170
char *cmdline;
176171
ukvm_hv_vcpu_init(hv, gpa_ep, gpa_kend, &cmdline);

0 commit comments

Comments
 (0)