#include <palacios/vm_guest.h>
#include <palacios/vmm_ctrl_regs.h>
#include <palacios/vmm.h>
+#include <palacios/vmm_decoder.h>
#include <palacios/vmcb.h>
+#include <palacios/vm_guest_mem.h>
+#include <palacios/vmm_lowlevel.h>
+#include <palacios/vmm_sprintf.h>
-v3_vm_cpu_mode_t v3_get_cpu_mode(struct guest_info * info) {
+
+v3_cpu_mode_t v3_get_vm_cpu_mode(struct guest_info * info) {
struct cr0_32 * cr0;
struct efer_64 * efer;
struct cr4_32 * cr4 = (struct cr4_32 *)&(info->ctrl_regs.cr4);
if (info->shdw_pg_mode == SHADOW_PAGING) {
cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
- efer = (struct efer_64 *)&(info->guest_efer);
+ efer = (struct efer_64 *)&(info->shdw_pg_state.guest_efer);
} else if (info->shdw_pg_mode == NESTED_PAGING) {
cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
efer = (struct efer_64 *)&(guest_state->efer);
uint_t v3_get_addr_width(struct guest_info * info) {
struct cr0_32 * cr0;
struct cr4_32 * cr4 = (struct cr4_32 *)&(info->ctrl_regs.cr4);
- struct efer_64 * efer = (struct efer_64 *)&(info->guest_efer);
+ struct efer_64 * efer;
struct v3_segment * cs = &(info->segments.cs);
+ vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
if (info->shdw_pg_mode == SHADOW_PAGING) {
cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
+ efer = (struct efer_64 *)&(info->shdw_pg_state.guest_efer);
} else if (info->shdw_pg_mode == NESTED_PAGING) {
cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
+ efer = (struct efer_64 *)&(guest_state->efer);
} else {
PrintError("Invalid Paging Mode...\n");
V3_ASSERT(0);
static const uchar_t LONG_32_COMPAT_STR[] = "32bit Compat";
static const uchar_t LONG_16_COMPAT_STR[] = "16bit Compat";
-const uchar_t * v3_cpu_mode_to_str(v3_vm_cpu_mode_t mode) {
+const uchar_t * v3_cpu_mode_to_str(v3_cpu_mode_t mode) {
switch (mode) {
case REAL:
return REAL_STR;
}
}
-v3_vm_mem_mode_t v3_get_mem_mode(struct guest_info * info) {
+v3_mem_mode_t v3_get_vm_mem_mode(struct guest_info * info) {
struct cr0_32 * cr0;
if (info->shdw_pg_mode == SHADOW_PAGING) {
static const uchar_t PHYS_MEM_STR[] = "Physical Memory";
static const uchar_t VIRT_MEM_STR[] = "Virtual Memory";
-const uchar_t * v3_mem_mode_to_str(v3_vm_mem_mode_t mode) {
+const uchar_t * v3_mem_mode_to_str(v3_mem_mode_t mode) {
switch (mode) {
case PHYSICAL_MEM:
return PHYS_MEM_STR;
}
-void v3_print_segments(struct guest_info * info) {
- struct v3_segments * segs = &(info->segments);
+void v3_print_segments(struct v3_segments * segs) {
int i = 0;
struct v3_segment * seg_ptr;
seg_ptr=(struct v3_segment *)segs;
char *seg_names[] = {"CS", "DS" , "ES", "FS", "GS", "SS" , "LDTR", "GDTR", "IDTR", "TR", NULL};
- PrintDebug("Segments\n");
+ V3_Print("Segments\n");
for (i = 0; seg_names[i] != NULL; i++) {
- PrintDebug("\t%s: Sel=%x, base=%p, limit=%x (long_mode=%d, db=%d)\n", seg_names[i], seg_ptr[i].selector,
+ V3_Print("\t%s: Sel=%x, base=%p, limit=%x (long_mode=%d, db=%d)\n", seg_names[i], seg_ptr[i].selector,
(void *)(addr_t)seg_ptr[i].base, seg_ptr[i].limit,
seg_ptr[i].long_mode, seg_ptr[i].db);
}
+}
+
+//
+// We don't handle those fancy 64 bit system segments...
+//
+int v3_translate_segment(struct guest_info * info, uint16_t selector, struct v3_segment * seg) {
+ struct v3_segment * gdt = &(info->segments.gdtr);
+ addr_t gdt_addr = 0;
+ uint16_t seg_offset = (selector & ~0x7);
+ addr_t seg_addr = 0;
+ struct gen_segment * gen_seg = NULL;
+ struct seg_selector sel;
+
+ memset(seg, 0, sizeof(struct v3_segment));
+
+ sel.value = selector;
+
+ if (sel.ti == 1) {
+ PrintError("LDT translations not supported\n");
+ return -1;
+ }
+
+ if (guest_va_to_host_va(info, gdt->base, &gdt_addr) == -1) {
+ PrintError("Unable to translate GDT address\n");
+ return -1;
+ }
+
+ seg_addr = gdt_addr + seg_offset;
+ gen_seg = (struct gen_segment *)seg_addr;
+
+ //translate
+ seg->selector = selector;
+
+ seg->limit = gen_seg->limit_hi;
+ seg->limit <<= 16;
+ seg->limit += gen_seg->limit_lo;
+ seg->base = gen_seg->base_hi;
+ seg->base <<= 24;
+ seg->base += gen_seg->base_lo;
+
+ if (gen_seg->granularity == 1) {
+ seg->limit <<= 12;
+ seg->limit |= 0xfff;
+ }
+
+ seg->type = gen_seg->type;
+ seg->system = gen_seg->system;
+ seg->dpl = gen_seg->dpl;
+ seg->present = gen_seg->present;
+ seg->avail = gen_seg->avail;
+ seg->long_mode = gen_seg->long_mode;
+ seg->db = gen_seg->db;
+ seg->granularity = gen_seg->granularity;
+
+ return 0;
}
+
+
void v3_print_ctrl_regs(struct guest_info * info) {
struct v3_ctrl_regs * regs = &(info->ctrl_regs);
int i = 0;
char * reg_names[] = {"CR0", "CR2", "CR3", "CR4", "CR8", "FLAGS", NULL};
vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA(info->vmm_data);
- reg_ptr= (v3_reg_t *)regs;
+ reg_ptr = (v3_reg_t *)regs;
- PrintDebug("32 bit Ctrl Regs:\n");
+ V3_Print("32 bit Ctrl Regs:\n");
for (i = 0; reg_names[i] != NULL; i++) {
- PrintDebug("\t%s=0x%p\n", reg_names[i], (void *)(addr_t)reg_ptr[i]);
+ V3_Print("\t%s=0x%p\n", reg_names[i], (void *)(addr_t)reg_ptr[i]);
}
- PrintDebug("\tEFER=0x%p\n", (void*)(addr_t)(guest_state->efer));
+ V3_Print("\tEFER=0x%p\n", (void*)(addr_t)(guest_state->efer));
+
+}
+
+
+void v3_print_guest_state(struct guest_info * info) {
+ addr_t linear_addr = 0;
+
+ V3_Print("RIP: %p\n", (void *)(addr_t)(info->rip));
+ linear_addr = get_addr_linear(info, info->rip, &(info->segments.cs));
+ V3_Print("RIP Linear: %p\n", (void *)linear_addr);
+
+ V3_Print("NumExits: %u\n", (uint32_t)info->num_exits);
+
+ v3_print_segments(&(info->segments));
+ v3_print_ctrl_regs(info);
+
+ if (info->shdw_pg_mode == SHADOW_PAGING) {
+ V3_Print("Shadow Paging Guest Registers:\n");
+ V3_Print("\tGuest CR0=%p\n", (void *)(addr_t)(info->shdw_pg_state.guest_cr0));
+ V3_Print("\tGuest CR3=%p\n", (void *)(addr_t)(info->shdw_pg_state.guest_cr3));
+ V3_Print("\tGuest EFER=%p\n", (void *)(addr_t)(info->shdw_pg_state.guest_efer.value));
+ // CR4
+ }
+ v3_print_GPRs(info);
+ v3_print_stack(info);
}
+void v3_print_stack(struct guest_info * info) {
+ addr_t linear_addr = 0;
+ addr_t host_addr = 0;
+ int i = 0;
+ v3_cpu_mode_t cpu_mode = v3_get_vm_cpu_mode(info);
+
+
+ linear_addr = get_addr_linear(info, info->vm_regs.rsp, &(info->segments.ss));
+
+ V3_Print("Stack at %p:\n", (void *)linear_addr);
+
+ if (info->mem_mode == PHYSICAL_MEM) {
+ if (guest_pa_to_host_va(info, linear_addr, &host_addr) == -1) {
+ PrintError("Could not translate Stack address\n");
+ return;
+ }
+ } else if (info->mem_mode == VIRTUAL_MEM) {
+ if (guest_va_to_host_va(info, linear_addr, &host_addr) == -1) {
+ PrintError("Could not translate Virtual Stack address\n");
+ return;
+ }
+ }
+
+ V3_Print("Host Address of rsp = 0x%p\n", (void *)host_addr);
+
+ // We start i at one because the current stack pointer points to an unused stack element
+ for (i = 0; i <= 24; i++) {
+ if (cpu_mode == LONG) {
+ V3_Print("\t%p\n", (void *)*(addr_t *)(host_addr + (i * 8)));
+ } else if (cpu_mode == REAL) {
+ V3_Print("Don't currently handle 16 bit stacks... \n");
+ } else {
+ // 32 bit stacks...
+ V3_Print("\t%.8x\n", *(uint32_t *)(host_addr + (i * 4)));
+ }
+ }
+
+}
+
#ifdef __V3_32BIT__
+
void v3_print_GPRs(struct guest_info * info) {
struct v3_gprs * regs = &(info->vm_regs);
int i = 0;
reg_ptr= (v3_reg_t *)regs;
- PrintDebug("32 bit GPRs:\n");
+ V3_Print("32 bit GPRs:\n");
for (i = 0; reg_names[i] != NULL; i++) {
- PrintDebug("\t%s=0x%p\n", reg_names[i], (void *)(addr_t)reg_ptr[i]);
+ V3_Print("\t%s=0x%p\n", reg_names[i], (void *)(addr_t)reg_ptr[i]);
}
}
+
#elif __V3_64BIT__
+
void v3_print_GPRs(struct guest_info * info) {
struct v3_gprs * regs = &(info->vm_regs);
int i = 0;
char * reg_names[] = { "RDI", "RSI", "RBP", "RSP", "RBX", "RDX", "RCX", "RAX", \
"R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15", NULL};
- reg_ptr= (v3_reg_t *)regs;
+ reg_ptr = (v3_reg_t *)regs;
- PrintDebug("64 bit GPRs:\n");
+ V3_Print("64 bit GPRs:\n");
for (i = 0; reg_names[i] != NULL; i++) {
- PrintDebug("\t%s=0x%p\n", reg_names[i], (void *)(addr_t)reg_ptr[i]);
+ V3_Print("\t%s=0x%p\n", reg_names[i], (void *)(addr_t)reg_ptr[i]);
}
}
-
-
#endif