X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?p=palacios.git;a=blobdiff_plain;f=palacios%2Fsrc%2Fpalacios%2Fvm_guest.c;h=c2bb07df25b24d48499b5be9c930ac573d8df693;hp=c846415f8e721dac0ec1505da963bd52aea8c314;hb=571979dad8fc2138a7e11c4fe61e812a0a0b17d1;hpb=6dee811714540921f73487de80e069decb80c602 diff --git a/palacios/src/palacios/vm_guest.c b/palacios/src/palacios/vm_guest.c index c846415..c2bb07d 100644 --- a/palacios/src/palacios/vm_guest.c +++ b/palacios/src/palacios/vm_guest.c @@ -23,7 +23,12 @@ #include #include #include +#include #include +#include +#include +#include + v3_cpu_mode_t v3_get_vm_cpu_mode(struct guest_info * info) { @@ -155,27 +160,83 @@ const uchar_t * v3_mem_mode_to_str(v3_mem_mode_t mode) { } -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; @@ -183,20 +244,85 @@ void v3_print_ctrl_regs(struct guest_info * info) { 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; @@ -205,13 +331,15 @@ void v3_print_GPRs(struct guest_info * info) { 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; @@ -219,15 +347,13 @@ void v3_print_GPRs(struct guest_info * info) { 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