Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Assure that unhandled IO reads get back zeros
[palacios.git] / palacios / src / palacios / vmx_io.c
index 4e94f69..46de7ca 100644 (file)
@@ -25,7 +25,7 @@
 #include <palacios/vm_guest_mem.h>
 #include <palacios/vmm_decoder.h>
 
-#ifndef CONFIG_DEBUG_IO
+#ifndef V3_CONFIG_DEBUG_IO
 #undef PrintDebug
 #define PrintDebug(fmt, args...)
 #endif
@@ -50,35 +50,42 @@ int v3_init_vmx_io_map(struct v3_vm_info * vm) {
     vm->io_map.update_map = update_map;
     
     vm->io_map.arch_data = V3_VAddr(V3_AllocPages(2));
-    memset(vm->io_map.arch_data, 0, PAGE_SIZE_4KB * 2);
+    memset(vm->io_map.arch_data, 0xff, PAGE_SIZE_4KB * 2);
 
     v3_refresh_io_map(vm);
 
     return 0;
 }
 
+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;
 
-
     hook = v3_get_io_hook(core->vm_info, io_qual.port);
 
-    if (hook == NULL) {
-        PrintError("Hook not present for IN on port %x\n", io_qual.port);
-        return -1;
-    }
-
     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(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;
-    }
+    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;
 
+    } 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;
+       }
+    }
+    
 
     core->rip += exit_info->instr_len;
 
@@ -97,10 +104,6 @@ int v3_handle_vmx_io_ins(struct guest_info * core, struct vmx_exit_info * exit_i
 
     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);
 
@@ -131,16 +134,25 @@ int v3_handle_vmx_io_ins(struct guest_info * core, struct vmx_exit_info * exit_i
 
 
 
-    if (guest_va_to_host_va(core, 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(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;
-        }
+
+       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;
         core->vm_regs.rdi += rdi_change;
@@ -166,22 +178,20 @@ int v3_handle_vmx_io_out(struct guest_info * core, struct vmx_exit_info * exit_i
 
     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(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 (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;
+       }
     }
 
-
-
     core->rip += exit_info->instr_len;
 
     return 0;
@@ -201,11 +211,6 @@ int v3_handle_vmx_io_outs(struct guest_info * core, struct vmx_exit_info * exit_
 
     hook = v3_get_io_hook(core->vm_info, io_qual.port);
 
-    if (hook == NULL) {
-        PrintError("Hook not present for OUTS on port 0x%x\n", io_qual.port);
-        return -1;
-    }
-
     PrintDebug("OUTS on port 0x%x\n", io_qual.port);
 
     write_size = io_qual.access_size + 1;
@@ -236,16 +241,22 @@ int v3_handle_vmx_io_outs(struct guest_info * core, struct vmx_exit_info * exit_
 
     PrintDebug("OUTS size=%d for %ld steps\n", write_size, rep_num);
 
-    if (guest_va_to_host_va(core, 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(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;
-       }
+
+       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;
        core->vm_regs.rsi += rsi_change;