summaryrefslogtreecommitdiffstats
path: root/lib/libvmmapi
diff options
context:
space:
mode:
authorjhb <jhb@FreeBSD.org>2014-02-05 04:39:03 +0000
committerjhb <jhb@FreeBSD.org>2014-02-05 04:39:03 +0000
commitf4e46bef9870d0ed15e9f02b57af7ccf63794d09 (patch)
tree9028dde86802e6e728a5c5a95359f88aeedcf6dc /lib/libvmmapi
parentc9758b2e1cc84a86a515276115bc1bc41de75c54 (diff)
downloadFreeBSD-src-f4e46bef9870d0ed15e9f02b57af7ccf63794d09.zip
FreeBSD-src-f4e46bef9870d0ed15e9f02b57af7ccf63794d09.tar.gz
Add support for FreeBSD/i386 guests under bhyve.
- Similar to the hack for bootinfo32.c in userboot, define _MACHINE_ELF_WANT_32BIT in the load_elf32 file handlers in userboot. This allows userboot to load 32-bit kernels and modules. - Copy the SMAP generation code out of bootinfo64.c and into its own file so it can be shared with bootinfo32.c to pass an SMAP to the i386 kernel. - Use uint32_t instead of u_long when aligning module metadata in bootinfo32.c in userboot, as otherwise the metadata used 64-bit alignment which corrupted the layout. - Populate the basemem and extmem members of the bootinfo struct passed to 32-bit kernels. - Fix the 32-bit stack in userboot to start at the top of the stack instead of the bottom so that there is room to grow before the kernel switches to its own stack. - Push a fake return address onto the 32-bit stack in addition to the arguments normally passed to exec() in the loader. This return address is needed to convince recover_bootinfo() in the 32-bit locore code that it is being invoked from a "new" boot block. - Add a routine to libvmmapi to setup a 32-bit flat mode register state including a GDT and TSS that is able to start the i386 kernel and update bhyveload to use it when booting an i386 kernel. - Use the guest register state to determine the CPU's current instruction mode (32-bit vs 64-bit) and paging mode (flat, 32-bit, PAE, or long mode) in the instruction emulation code. Update the gla2gpa() routine used when fetching instructions to handle flat mode, 32-bit paging, and PAE paging in addition to long mode paging. Don't look for a REX prefix when the CPU is in 32-bit mode, and use the detected mode to enable the existing 32-bit mode code when decoding the mod r/m byte. Reviewed by: grehan, neel MFC after: 1 month
Diffstat (limited to 'lib/libvmmapi')
-rw-r--r--lib/libvmmapi/vmmapi.h3
-rw-r--r--lib/libvmmapi/vmmapi_freebsd.c166
2 files changed, 167 insertions, 2 deletions
diff --git a/lib/libvmmapi/vmmapi.h b/lib/libvmmapi/vmmapi.h
index 8428987..4a3db5d 100644
--- a/lib/libvmmapi/vmmapi.h
+++ b/lib/libvmmapi/vmmapi.h
@@ -111,5 +111,8 @@ int vcpu_reset(struct vmctx *ctx, int vcpu);
int vm_setup_freebsd_registers(struct vmctx *ctx, int vcpu,
uint64_t rip, uint64_t cr3, uint64_t gdtbase,
uint64_t rsp);
+int vm_setup_freebsd_registers_i386(struct vmctx *vmctx, int vcpu,
+ uint32_t eip, uint32_t gdtbase,
+ uint32_t esp);
void vm_setup_freebsd_gdt(uint64_t *gdtr);
#endif /* _VMMAPI_H_ */
diff --git a/lib/libvmmapi/vmmapi_freebsd.c b/lib/libvmmapi/vmmapi_freebsd.c
index 9bd2988..d801184 100644
--- a/lib/libvmmapi/vmmapi_freebsd.c
+++ b/lib/libvmmapi/vmmapi_freebsd.c
@@ -35,14 +35,176 @@ __FBSDID("$FreeBSD$");
#include <machine/segments.h>
#include <machine/vmm.h>
+#include <errno.h>
+#include <string.h>
+
#include "vmmapi.h"
+#define I386_TSS_SIZE 104
+
+#define DESC_PRESENT 0x00000080
+#define DESC_LONGMODE 0x00002000
+#define DESC_DEF32 0x00004000
+#define DESC_GRAN 0x00008000
#define DESC_UNUSABLE 0x00010000
#define GUEST_NULL_SEL 0
#define GUEST_CODE_SEL 1
#define GUEST_DATA_SEL 2
-#define GUEST_GDTR_LIMIT (3 * 8 - 1)
+#define GUEST_TSS_SEL 3
+#define GUEST_GDTR_LIMIT64 (3 * 8 - 1)
+
+static struct segment_descriptor i386_gdt[] = {
+ {}, /* NULL */
+ { .sd_lolimit = 0xffff, .sd_type = SDT_MEMER, /* CODE */
+ .sd_p = 1, .sd_hilimit = 0xf, .sd_def32 = 1, .sd_gran = 1 },
+ { .sd_lolimit = 0xffff, .sd_type = SDT_MEMRW, /* DATA */
+ .sd_p = 1, .sd_hilimit = 0xf, .sd_def32 = 1, .sd_gran = 1 },
+ { .sd_lolimit = I386_TSS_SIZE - 1, /* TSS */
+ .sd_type = SDT_SYS386TSS, .sd_p = 1 }
+};
+
+/*
+ * Setup the 'vcpu' register set such that it will begin execution at
+ * 'eip' in flat mode.
+ */
+int
+vm_setup_freebsd_registers_i386(struct vmctx *vmctx, int vcpu, uint32_t eip,
+ uint32_t gdtbase, uint32_t esp)
+{
+ uint64_t cr0, rflags, desc_base;
+ uint32_t desc_access, desc_limit, tssbase;
+ uint16_t gsel;
+ struct segment_descriptor *gdt;
+ int error, tmp;
+
+ /* A 32-bit guest requires unrestricted mode. */
+ error = vm_get_capability(vmctx, vcpu, VM_CAP_UNRESTRICTED_GUEST, &tmp);
+ if (error)
+ goto done;
+ error = vm_set_capability(vmctx, vcpu, VM_CAP_UNRESTRICTED_GUEST, 1);
+ if (error)
+ goto done;
+
+ cr0 = CR0_PE | CR0_NE;
+ if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
+ goto done;
+
+ if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, 0)) != 0)
+ goto done;
+
+ /*
+ * Forcing EFER to 0 causes bhyve to clear the "IA-32e guest
+ * mode" entry control.
+ */
+ if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_EFER, 0)))
+ goto done;
+
+ gdt = vm_map_gpa(vmctx, gdtbase, 0x1000);
+ if (gdt == NULL)
+ return (EFAULT);
+ memcpy(gdt, i386_gdt, sizeof(i386_gdt));
+ desc_base = gdtbase;
+ desc_limit = sizeof(i386_gdt) - 1;
+ error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
+ desc_base, desc_limit, 0);
+ if (error != 0)
+ goto done;
+
+ /* Place the TSS one page above the GDT. */
+ tssbase = gdtbase + 0x1000;
+ gdt[3].sd_lobase = tssbase;
+
+ rflags = 0x2;
+ error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
+ if (error)
+ goto done;
+
+ desc_base = 0;
+ desc_limit = 0xffffffff;
+ desc_access = DESC_GRAN | DESC_DEF32 | DESC_PRESENT | SDT_MEMERA;
+ error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
+ desc_base, desc_limit, desc_access);
+
+ desc_access = DESC_GRAN | DESC_DEF32 | DESC_PRESENT | SDT_MEMRWA;
+ error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
+ desc_base, desc_limit, desc_access);
+ if (error)
+ goto done;
+
+ error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
+ desc_base, desc_limit, desc_access);
+ if (error)
+ goto done;
+
+ error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
+ desc_base, desc_limit, desc_access);
+ if (error)
+ goto done;
+
+ error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
+ desc_base, desc_limit, desc_access);
+ if (error)
+ goto done;
+
+ error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
+ desc_base, desc_limit, desc_access);
+ if (error)
+ goto done;
+
+ desc_base = tssbase;
+ desc_limit = I386_TSS_SIZE - 1;
+ desc_access = DESC_PRESENT | SDT_SYS386BSY;
+ error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR,
+ desc_base, desc_limit, desc_access);
+ if (error)
+ goto done;
+
+
+ error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, 0, 0,
+ DESC_UNUSABLE);
+ if (error)
+ goto done;
+
+ gsel = GSEL(GUEST_CODE_SEL, SEL_KPL);
+ if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, gsel)) != 0)
+ goto done;
+
+ gsel = GSEL(GUEST_DATA_SEL, SEL_KPL);
+ if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, gsel)) != 0)
+ goto done;
+
+ if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, gsel)) != 0)
+ goto done;
+
+ if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, gsel)) != 0)
+ goto done;
+
+ if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, gsel)) != 0)
+ goto done;
+
+ if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, gsel)) != 0)
+ goto done;
+
+ gsel = GSEL(GUEST_TSS_SEL, SEL_KPL);
+ if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, gsel)) != 0)
+ goto done;
+
+ /* LDTR is pointing to the null selector */
+ if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
+ goto done;
+
+ /* entry point */
+ if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, eip)) != 0)
+ goto done;
+
+ if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, esp)) != 0)
+ goto done;
+
+ error = 0;
+done:
+ return (error);
+}
void
vm_setup_freebsd_gdt(uint64_t *gdtr)
@@ -168,7 +330,7 @@ vm_setup_freebsd_registers(struct vmctx *vmctx, int vcpu,
goto done;
desc_base = gdtbase;
- desc_limit = GUEST_GDTR_LIMIT;
+ desc_limit = GUEST_GDTR_LIMIT64;
error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
desc_base, desc_limit, 0);
if (error != 0)
OpenPOWER on IntegriCloud