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.


VMX is working for a 32-bit Linux kernel. It should also work for a 64-bit kernel...
[palacios.git] / palacios / src / palacios / vmx_io.c
index 0f841e4..8554606 100644 (file)
@@ -34,8 +34,7 @@
 
 
 /* Same as SVM */
-static int update_map(struct guest_info * info, uint16_t port, int hook_read, int hook_write)
-{
+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);
     int major = port / 8;
     int minor = port % 8;
@@ -49,43 +48,39 @@ 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)
-{
+int v3_init_vmx_io_map(struct guest_info * info) {
     info->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);
+    memset(info->io_map.arch_data, 0, PAGE_SIZE_4KB * 2);
 
     return 0;
 }
 
 int v3_handle_vmx_io_in(struct guest_info * info) {
-    ulong_t exit_qual;
+    struct vmx_exit_io_qual io_qual;
+    struct v3_io_hook * hook = NULL;
+    int read_size = 0;
     uint32_t instr_length = 0;
 
-    vmcs_read(VMCS_EXIT_QUAL, &exit_qual);
-
-    struct vmexit_io_qual * io_qual = (struct vmexit_io_qual *)&exit_qual;
-
-    struct v3_io_hook * hook = v3_get_io_hook(info, io_qual->port);
-    int read_size = 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);
+        PrintError("Hook not present for IN on port %x\n", io_qual.port);
         return -1;
     }
 
-    read_size = io_qual->access_size + 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);
+    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);
+    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 (vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_length) != VMX_SUCCESS) {
         PrintError("Could not read instruction length\n");
         return -1;
@@ -96,34 +91,49 @@ int v3_handle_vmx_io_in(struct guest_info * info) {
     return 0;
 }
 
-int v3_handle_vmx_io_ins(struct guest_info * info)
-{
-    ulong_t exit_qual;
-
-    vmcs_read(VMCS_EXIT_QUAL, &exit_qual);
-
-    struct vmexit_io_qual * io_qual = (struct vmexit_io_qual *)&exit_qual;
-    struct v3_io_hook * hook = v3_get_io_hook(info, io_qual->port);
-    int read_size;
-    addr_t guest_va;
-    addr_t host_addr;
-    int rdi_change;
+int v3_handle_vmx_io_ins(struct guest_info * info) {
+    struct vmx_exit_io_qual io_qual;
+    struct v3_io_hook * hook = NULL;
+    int read_size = 0;
+    addr_t guest_va = 0;
+    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;
+
+    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 INS on port 0x%x\n", 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);
+    PrintDebug("INS on port 0x%x\n", io_qual.port);
+
+    read_size = io_qual.access_size + 1;
 
-    read_size = io_qual->access_size + 1;
+    if (io_qual.rep) {
+        struct vmx_exit_io_instr_info instr_info;
 
-    if (io_qual->rep) {
-        rep_num = info->vm_regs.rcx & get_gpr_mask(info);
+       instr_info.value = 0;
+        vmcs_read(VMCS_EXIT_INSTR_INFO, &instr_info.value);
+
+        if (instr_info.addr_size == 0) {
+            rep_num = info->vm_regs.rcx & 0xffff;
+        } else if(instr_info.addr_size == 1) {
+            rep_num = info->vm_regs.rcx & 0xffffffff;
+        } else if(instr_info.addr_size == 2) {
+            rep_num = info->vm_regs.rcx & 0xffffffffffffffffLL;
+        } else {
+            PrintDebug("Unknown INS address size!\n");
+            return -1;
+        }
     }
     
-    if ( ((struct rflags *)&(info->ctrl_regs.rflags))->df ) {
+    if (flags->df) {
         rdi_change = -read_size;
     } else {
         rdi_change = read_size;
@@ -139,25 +149,22 @@ int v3_handle_vmx_io_ins(struct guest_info * info)
     }
 
     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);
+        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;
         }
 
         host_addr += rdi_change;
         info->vm_regs.rdi += rdi_change;
 
-        if (io_qual->rep) {
-            --info->vm_regs.rcx;
+        if (io_qual.rep) {
+            info->vm_regs.rcx--;
         }
-        --rep_num;
-
-    } while (rep_num > 0);
+        
+    } while (--rep_num > 0);
 
-    int instr_len = 0;
 
     vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_len);
-
     info->rip += instr_len;
 
     return 0;
@@ -166,30 +173,28 @@ int v3_handle_vmx_io_ins(struct guest_info * info)
 
 
 int v3_handle_vmx_io_out(struct guest_info * info) {
-    ulong_t exit_qual;
-
-    vmcs_read(VMCS_EXIT_QUAL, &exit_qual);
-
-    struct vmexit_io_qual * io_qual = (struct vmexit_io_qual *)&exit_qual;
+    struct vmx_exit_io_qual io_qual;
+    struct v3_io_hook * hook = NULL;
+    int write_size = 0;
+    uint32_t instr_length = 0;
 
-    struct v3_io_hook * hook = v3_get_io_hook(info, io_qual->port);
+    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 out on port %x\n", io_qual->port);
+        PrintError("Hook not present for out on port %x\n", io_qual.port);
         return -1;
     }
 
-    int write_size = io_qual->access_size + 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);
+    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);
+    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;
     }
 
-    uint32_t instr_length = 0;
 
     if (vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_length) != VMX_SUCCESS) {
         PrintError("Could not read instruction length\n");
@@ -204,33 +209,48 @@ int v3_handle_vmx_io_out(struct guest_info * info) {
 
 
 int v3_handle_vmx_io_outs(struct guest_info * info) {
-    ulong_t exit_qual;
-
-    vmcs_read(VMCS_EXIT_QUAL, &exit_qual);
-
-    struct vmexit_io_qual * io_qual = (struct vmexit_io_qual *)&exit_qual;
-    struct v3_io_hook * hook = v3_get_io_hook(info, io_qual->port);
+    struct vmx_exit_io_qual io_qual;
+    struct v3_io_hook * hook = NULL;
     int write_size;
     addr_t guest_va;
     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);
 
     if (hook == NULL) {
-        PrintError("Hook not present for OUTS on port 0x%x\n", io_qual->port);
+        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);
+    PrintDebug("OUTS on port 0x%x\n", io_qual.port);
 
-    write_size = io_qual->access_size + 1;
+    write_size = io_qual.access_size + 1;
 
-    if (io_qual->rep) {
+    if (io_qual.rep) {
         // Grab the address sized bits of rcx
-        rep_num = info->vm_regs.rcx & get_gpr_mask(info);
+        struct vmx_exit_io_instr_info instr_info;
+
+       instr_info.value = 0;
+        vmcs_read(VMCS_EXIT_INSTR_INFO, &instr_info.value);
+
+        if (instr_info.addr_size == 0) {
+            rep_num = info->vm_regs.rcx & 0xffff;
+        } else if(instr_info.addr_size == 1) {
+            rep_num = info->vm_regs.rcx & 0xffffffff;
+        } else if(instr_info.addr_size == 2) {
+            rep_num = info->vm_regs.rcx & 0xffffffffffffffffLL;
+        } else {
+            PrintDebug("Unknown INS address size!\n");
+            return -1;
+        }
     }
 
-    if ( ((struct rflags *)&(info->ctrl_regs.rflags))->df ) {
+    if (flags->df) {
         rsi_change = -write_size;
     } else {
         rsi_change = write_size;
@@ -246,25 +266,22 @@ int v3_handle_vmx_io_outs(struct guest_info * info) {
     }
 
     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);
+       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;
        }
 
        host_addr += rsi_change;
        info->vm_regs.rsi += rsi_change;
 
-       if (io_qual->rep) {
+       if (io_qual.rep) {
            --info->vm_regs.rcx;
        }
-       --rep_num;
 
-    } while (rep_num > 0);
+    } while (--rep_num > 0);
 
-    int instr_len = 0;
 
     vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_len);
-
     info->rip += instr_len;
 
     return 0;