X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fpalacios%2Fvmx_io.c;h=1dbe95e8e60a8ebb6ab32877d9d90ab658e259a3;hb=26f40a6baf7790464d09fb935d9ad27578bdf4e6;hp=fb9af1a84389167d81430aee6494321385dc3296;hpb=bf9d843e65ef72688da11ea6d1216909729395b3;p=palacios.git diff --git a/palacios/src/palacios/vmx_io.c b/palacios/src/palacios/vmx_io.c index fb9af1a..1dbe95e 100644 --- a/palacios/src/palacios/vmx_io.c +++ b/palacios/src/palacios/vmx_io.c @@ -19,23 +19,21 @@ #include #include -#include -#include #include #include #include #include #include -#ifndef CONFIG_DEBUG_IO +#ifndef V3_CONFIG_DEBUG_IO #undef PrintDebug #define PrintDebug(fmt, args...) #endif /* Same as SVM */ -static int update_map(struct guest_info * info, uint16_t port, int hook_read, int hook_write) { - uchar_t * bitmap = (uint8_t *)(info->io_map.arch_data); +static int update_map(struct v3_vm_info * vm, uint16_t port, int hook_read, int hook_write) { + uint8_t * bitmap = (uint8_t *)(vm->io_map.arch_data); int major = port / 8; int minor = port % 8; @@ -48,87 +46,78 @@ static int update_map(struct guest_info * info, uint16_t port, int hook_read, in return 0; } -int v3_init_vmx_io_map(struct guest_info * info) { - info->io_map.update_map = update_map; +int v3_init_vmx_io_map(struct v3_vm_info * vm) { + vm->io_map.update_map = update_map; - info->io_map.arch_data = V3_VAddr(V3_AllocPages(2)); - memset(info->io_map.arch_data, 0, PAGE_SIZE_4KB * 2); + vm->io_map.arch_data = V3_VAddr(V3_AllocPages(2)); + memset(vm->io_map.arch_data, 0xff, PAGE_SIZE_4KB * 2); - v3_refresh_io_map(info); + v3_refresh_io_map(vm); return 0; } -int v3_handle_vmx_io_in(struct guest_info * info) { - struct vmx_exit_io_qual io_qual; +int v3_deinit_vmx_io_map(struct v3_vm_info * vm) { + V3_FreePages(V3_PAddr(vm->io_map.arch_data), 2); + return 0; +} + + +int v3_handle_vmx_io_in(struct guest_info * core, struct vmx_exit_info * exit_info) { + struct vmx_exit_io_qual io_qual = *(struct vmx_exit_io_qual *)&(exit_info->exit_qual);; struct v3_io_hook * hook = NULL; int read_size = 0; - uint32_t instr_length = 0; - - io_qual.value = 0; - vmcs_read(VMCS_EXIT_QUAL, &(io_qual.value)); - hook = v3_get_io_hook(info, io_qual.port); - if (hook == NULL) { - PrintError("Hook not present for IN on port %x\n", io_qual.port); - return -1; - } + hook = v3_get_io_hook(core->vm_info, io_qual.port); read_size = io_qual.access_size + 1; PrintDebug("IN of %d bytes on port %d (0x%x)\n", read_size, io_qual.port, io_qual.port); - if (hook->read(io_qual.port, &(info->vm_regs.rax), read_size, hook->priv_data) != read_size) { - PrintError("Read failure for IN on port %x\n", io_qual.port); - return -1; - } + if (hook == NULL) { + PrintDebug("IN operation on unhooked IO port 0x%x - returning zeros\n", io_qual.port); + core->vm_regs.rax >>= 8*read_size; + core->vm_regs.rax <<= 8*read_size; - if (vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_length) != VMX_SUCCESS) { - PrintError("Could not read instruction length\n"); - return -1; + } else { + if (hook->read(core, io_qual.port, &(core->vm_regs.rax), read_size, hook->priv_data) != read_size) { + PrintError("Read failure for IN on port %x\n", io_qual.port); + return -1; + } } + - info->rip += instr_length; + core->rip += exit_info->instr_len; return 0; } -int v3_handle_vmx_io_ins(struct guest_info * info) { - struct vmx_exit_io_qual io_qual; +int v3_handle_vmx_io_ins(struct guest_info * core, struct vmx_exit_info * exit_info) { + struct vmx_exit_io_qual io_qual = *(struct vmx_exit_io_qual *)&(exit_info->exit_qual);; struct v3_io_hook * hook = NULL; int read_size = 0; - addr_t guest_va = 0; + addr_t guest_va = exit_info->guest_linear_addr; addr_t host_addr = 0; int rdi_change = 0; - ulong_t rep_num = 1; - struct rflags * flags = (struct rflags *)&(info->ctrl_regs.rflags); - int instr_len = 0; + uint32_t rep_num = 1; + struct rflags * flags = (struct rflags *)&(core->ctrl_regs.rflags); - io_qual.value = 0; - vmcs_read(VMCS_EXIT_QUAL, &(io_qual.value)); - hook = v3_get_io_hook(info, io_qual.port); + hook = v3_get_io_hook(core->vm_info, io_qual.port); - if (hook == NULL) { - PrintError("Hook not present for INS on port 0x%x\n", io_qual.port); - return -1; - } PrintDebug("INS on port 0x%x\n", io_qual.port); read_size = io_qual.access_size + 1; if (io_qual.rep) { - struct vmx_exit_io_instr_info instr_info; - - instr_info.value = 0; - vmcs_read(VMCS_EXIT_INSTR_INFO, &instr_info.value); + struct vmx_exit_io_instr_info instr_info = *(struct vmx_exit_io_instr_info *)&(exit_info->instr_info); if (instr_info.addr_size == 0) { - rep_num = info->vm_regs.rcx & 0xffff; + rep_num = core->vm_regs.rcx & 0xffff; } else if(instr_info.addr_size == 1) { - rep_num = info->vm_regs.rcx & 0xffffffff; + rep_num = core->vm_regs.rcx & 0xffffffff; } else if(instr_info.addr_size == 2) { - rep_num = info->vm_regs.rcx & 0xffffffffffffffffLL; + rep_num = core->vm_regs.rcx & 0xffffffffffffffffLL; } else { PrintDebug("Unknown INS address size!\n"); return -1; @@ -143,91 +132,84 @@ int v3_handle_vmx_io_ins(struct guest_info * info) { PrintDebug("INS size=%d for %ld steps\n", read_size, rep_num); - vmcs_read(VMCS_GUEST_LINEAR_ADDR, &guest_va); - if (guest_va_to_host_va(info, guest_va, &host_addr) == -1) { + + if (v3_gva_to_hva(core, guest_va, &host_addr) == -1) { PrintError("Could not convert Guest VA to host VA\n"); return -1; } do { - if (hook->read(io_qual.port, (char *)host_addr, read_size, hook->priv_data) != read_size) { - PrintError("Read Failure for INS on port 0x%x\n", io_qual.port); - return -1; - } + + if (hook == NULL) { + PrintDebug("INS operation on unhooked IO port 0x%x - returning zeros\n", io_qual.port); + + memset((char*)host_addr,0,read_size); + + } else { + if (hook->read(core, io_qual.port, (char *)host_addr, read_size, hook->priv_data) != read_size) { + PrintError("Read Failure for INS on port 0x%x\n", io_qual.port); + return -1; + } + } + host_addr += rdi_change; - info->vm_regs.rdi += rdi_change; + core->vm_regs.rdi += rdi_change; if (io_qual.rep) { - info->vm_regs.rcx--; + core->vm_regs.rcx--; } } while (--rep_num > 0); - vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_len); - info->rip += instr_len; + core->rip += exit_info->instr_len; return 0; } -int v3_handle_vmx_io_out(struct guest_info * info) { - struct vmx_exit_io_qual io_qual; +int v3_handle_vmx_io_out(struct guest_info * core, struct vmx_exit_info * exit_info) { + struct vmx_exit_io_qual io_qual = *(struct vmx_exit_io_qual *)&(exit_info->exit_qual); struct v3_io_hook * hook = NULL; int write_size = 0; - uint32_t instr_length = 0; - vmcs_read(VMCS_EXIT_QUAL, &(io_qual.value)); - hook = v3_get_io_hook(info, io_qual.port); + hook = v3_get_io_hook(core->vm_info, io_qual.port); - if (hook == NULL) { - PrintError("Hook not present for out on port %x\n", io_qual.port); - return -1; - } write_size = io_qual.access_size + 1; PrintDebug("OUT of %d bytes on port %d (0x%x)\n", write_size, io_qual.port, io_qual.port); - if (hook->write(io_qual.port, &(info->vm_regs.rax), write_size, hook->priv_data) != write_size) { - PrintError("Write failure for out on port %x\n",io_qual.port); - return -1; + if (hook == NULL) { + PrintDebug("OUT operation on unhooked IO port 0x%x - ignored\n", io_qual.port); + } else { + if (hook->write(core, io_qual.port, &(core->vm_regs.rax), write_size, hook->priv_data) != write_size) { + PrintError("Write failure for out on port %x\n",io_qual.port); + return -1; + } } - - if (vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_length) != VMX_SUCCESS) { - PrintError("Could not read instruction length\n"); - return -1; - } - - info->rip += instr_length; + core->rip += exit_info->instr_len; return 0; } -int v3_handle_vmx_io_outs(struct guest_info * info) { - struct vmx_exit_io_qual io_qual; +int v3_handle_vmx_io_outs(struct guest_info * core, struct vmx_exit_info * exit_info) { + struct vmx_exit_io_qual io_qual = *(struct vmx_exit_io_qual *)&(exit_info->exit_qual); struct v3_io_hook * hook = NULL; int write_size; - addr_t guest_va; + addr_t guest_va = exit_info->guest_linear_addr; addr_t host_addr; int rsi_change; - ulong_t rep_num = 1; - struct rflags * flags = (struct rflags *)&(info->ctrl_regs.rflags); - int instr_len = 0; - - vmcs_read(VMCS_EXIT_QUAL, &(io_qual.value)); - hook = v3_get_io_hook(info, io_qual.port); + uint32_t rep_num = 1; + struct rflags * flags = (struct rflags *)&(core->ctrl_regs.rflags); - if (hook == NULL) { - PrintError("Hook not present for OUTS on port 0x%x\n", io_qual.port); - return -1; - } + hook = v3_get_io_hook(core->vm_info, io_qual.port); PrintDebug("OUTS on port 0x%x\n", io_qual.port); @@ -235,17 +217,14 @@ int v3_handle_vmx_io_outs(struct guest_info * info) { if (io_qual.rep) { // Grab the address sized bits of rcx - struct vmx_exit_io_instr_info instr_info; - - instr_info.value = 0; - vmcs_read(VMCS_EXIT_INSTR_INFO, &instr_info.value); + struct vmx_exit_io_instr_info instr_info = *(struct vmx_exit_io_instr_info *)&(exit_info->instr_info); if (instr_info.addr_size == 0) { - rep_num = info->vm_regs.rcx & 0xffff; + rep_num = core->vm_regs.rcx & 0xffff; } else if(instr_info.addr_size == 1) { - rep_num = info->vm_regs.rcx & 0xffffffff; + rep_num = core->vm_regs.rcx & 0xffffffff; } else if(instr_info.addr_size == 2) { - rep_num = info->vm_regs.rcx & 0xffffffffffffffffLL; + rep_num = core->vm_regs.rcx & 0xffffffffffffffffLL; } else { PrintDebug("Unknown INS address size!\n"); return -1; @@ -258,33 +237,38 @@ int v3_handle_vmx_io_outs(struct guest_info * info) { rsi_change = write_size; } - vmcs_read(VMCS_GUEST_LINEAR_ADDR, &guest_va); + PrintDebug("OUTS size=%d for %ld steps\n", write_size, rep_num); - if (guest_va_to_host_va(info, guest_va, &host_addr) == -1) { + if (v3_gva_to_hva(core, guest_va, &host_addr) == -1) { PrintError("Could not convert guest VA to host VA\n"); return -1; } do { - if (hook->write(io_qual.port, (char *)host_addr, write_size, hook->priv_data) != write_size) { - PrintError("Read failure for INS on port 0x%x\n", io_qual.port); - return -1; - } + + if (hook == NULL) { + PrintDebug("OUTS operation on unhooked IO port 0x%x - ignored\n", io_qual.port); + } else { + if (hook->write(core, io_qual.port, (char *)host_addr, write_size, hook->priv_data) != write_size) { + PrintError("Read failure for INS on port 0x%x\n", io_qual.port); + return -1; + } + } + host_addr += rsi_change; - info->vm_regs.rsi += rsi_change; + core->vm_regs.rsi += rsi_change; if (io_qual.rep) { - --info->vm_regs.rcx; + --core->vm_regs.rcx; } } while (--rep_num > 0); - vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_len); - info->rip += instr_len; + core->rip += exit_info->instr_len; return 0; }