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.


fixed operand ordering and memory operand masking
[palacios.git] / palacios / src / palacios / vmm_xed.c
index 68e39b0..1aef74a 100644 (file)
@@ -259,6 +259,33 @@ static int decode_string_op(struct guest_info * info,
       instr->str_op_length = 1;
     }
 
+  } else if (instr->op_type == V3_OP_STOS) {
+    instr->num_operands = 2;
+
+    if (get_memory_operand(info, xed_instr, 0, &(instr->dst_operand)) == -1) {
+      PrintError("Could not get Destination memory operand\n");
+      return -1;
+    }
+
+    // STOS reads from rax
+    xed_reg_to_v3_reg(info, xed_decoded_inst_get_reg(xed_instr, XED_OPERAND_REG0), 
+                     &(instr->src_operand.operand), 
+                     &(instr->src_operand.size));
+    instr->src_operand.type = REG_OPERAND;
+    
+    if (instr->prefixes.rep == 1) {
+      addr_t reg_addr = 0;
+      uint_t reg_length = 0;
+
+      xed_reg_to_v3_reg(info, xed_decoded_inst_get_reg(xed_instr, XED_OPERAND_REG1), &reg_addr, &reg_length);
+      instr->str_op_length = MASK(*(addr_t *)reg_addr, reg_length);
+    } else {
+      instr->str_op_length = 1;
+    }
+
+  } else {
+    PrintError("Unhandled String OP\n");
+    return -1;
   }
 
   return 0;
@@ -350,12 +377,15 @@ int v3_decode(struct guest_info * info, addr_t instr_ptr, struct x86_instr * ins
 
     struct x86_operand * v3_op = NULL;
 
-    if (xed_operand_written(op)) {
+    /*
+      if (xed_operand_written(op)) {
       v3_op = &(instr->dst_operand);
-    } else {
+      } else {
       v3_op = &(instr->src_operand);
-    }
+      }
+    */
 
+    v3_op = &(instr->dst_operand);
 
     if (xed_operand_is_register(op_enum)) {
       xed_reg_enum_t xed_reg =  xed_decoded_inst_get_reg(&xed_instr, op_enum);
@@ -380,6 +410,7 @@ int v3_decode(struct guest_info * info, addr_t instr_ptr, struct x86_instr * ins
 
       case XED_OPERAND_MEM0:
        {
+         PrintDebug("Memory operand (1)\n");
          if (get_memory_operand(info, &xed_instr, 0, v3_op) == -1) {
            PrintError("Could not get first memory operand\n");
            return -1;
@@ -415,12 +446,14 @@ int v3_decode(struct guest_info * info, addr_t instr_ptr, struct x86_instr * ins
     
     struct x86_operand * v3_op;
 
-    if (xed_operand_written(op)) {
+    /*
+      if (xed_operand_written(op)) {
       v3_op = &(instr->dst_operand);
-    } else {
+      } else {
       v3_op = &(instr->src_operand);
-    }
-
+      }
+    */
+    v3_op = &(instr->src_operand);
 
     if (xed_operand_is_register(op_enum)) {
       xed_reg_enum_t xed_reg =  xed_decoded_inst_get_reg(&xed_instr, op_enum);
@@ -446,6 +479,7 @@ int v3_decode(struct guest_info * info, addr_t instr_ptr, struct x86_instr * ins
 
       case XED_OPERAND_MEM0:
        {
+         PrintDebug("Memory operand (2)\n");
          if (get_memory_operand(info, &xed_instr, 0, v3_op) == -1) {
            PrintError("Could not get first memory operand\n");
            return -1;
@@ -542,6 +576,7 @@ static int get_memory_operand(struct guest_info * info,  xed_decoded_inst_t * xe
   addr_t scale;
   addr_t index;
   ullong_t displacement;
+  int addr_width = v3_get_addr_width(info);;
   // struct v3_segment * seg_reg;
 
   PrintDebug("Xen mode = %s\n", xed_machine_mode_enum_t2str(xed_state_get_machine_mode(info->decoder_state)));
@@ -550,6 +585,8 @@ static int get_memory_operand(struct guest_info * info,  xed_decoded_inst_t * xe
   PrintDebug("Stack Address width: %s\n",
             xed_address_width_enum_t2str(xed_state_get_stack_address_width(info->decoder_state)));
 
+  
+
   memset((void*)&mem_op, '\0', sizeof(struct memory_operand));
 
   xed_reg_enum_t xed_seg = xed_decoded_inst_get_seg_reg(xed_instr, op_index);
@@ -605,8 +642,8 @@ static int get_memory_operand(struct guest_info * info,  xed_decoded_inst_t * xe
   
   
 
-  PrintDebug("Struct: Seg=%p, base=%p, index=%p, scale=%p, displacement=%p (size=%d)\n", 
-            (void *)mem_op.segment, (void*)mem_op.base, (void *)mem_op.index, 
+  PrintDebug("Struct: Seg=%p (size=%d), base=%p, index=%p, scale=%p, displacement=%p (size=%d)\n", 
+            (void *)mem_op.segment, mem_op.segment_size, (void*)mem_op.base, (void *)mem_op.index, 
             (void *)mem_op.scale, (void *)(addr_t)mem_op.displacement, mem_op.displacement_size);
 
 
@@ -616,13 +653,20 @@ static int get_memory_operand(struct guest_info * info,  xed_decoded_inst_t * xe
   base = MASK(mem_op.base, mem_op.base_size);
   index = MASK(mem_op.index, mem_op.index_size);
   scale = mem_op.scale;
-  // displacement = MASK(mem_op.displacement, mem_op.displacement_size);
-  displacement = mem_op.displacement;
+
+  // This is a horrendous hack...
+  // XED really screwed the pooch in calculating the displacement
+  if (v3_get_cpu_mode(info) == LONG) {
+    displacement = mem_op.displacement;
+  } else {
+    displacement = MASK(mem_op.displacement, mem_op.displacement_size);
+  }
 
   PrintDebug("Seg=%p, base=%p, index=%p, scale=%p, displacement=%p\n", 
             (void *)seg, (void *)base, (void *)index, (void *)scale, (void *)(addr_t)displacement);
   
-  operand->operand = seg + base + (scale * index) + displacement;
+  operand->operand = MASK((seg + base + (scale * index) + displacement), addr_width);
+
   return 0;
 }
 
@@ -1008,21 +1052,27 @@ static int xed_reg_to_v3_reg(struct guest_info * info, xed_reg_enum_t xed_reg, a
      */
   case XED_REG_CS:
     *v3_reg = (addr_t)&(info->segments.cs);
+    *reg_len = 8;
     return SEGMENT_REGISTER;
   case XED_REG_DS:
     *v3_reg = (addr_t)&(info->segments.ds);
+    *reg_len = 8;
     return SEGMENT_REGISTER;
   case XED_REG_ES:
     *v3_reg = (addr_t)&(info->segments.es);
+    *reg_len = 8;
     return SEGMENT_REGISTER;
   case XED_REG_SS:
     *v3_reg = (addr_t)&(info->segments.ss);
+    *reg_len = 8;
     return SEGMENT_REGISTER;
   case XED_REG_FS:
     *v3_reg = (addr_t)&(info->segments.fs);
+    *reg_len = 8;
     return SEGMENT_REGISTER;
   case XED_REG_GS:
     *v3_reg = (addr_t)&(info->segments.gs);
+    *reg_len = 8;
     return SEGMENT_REGISTER;
 
 
@@ -1236,6 +1286,7 @@ static v3_op_type_t get_opcode(xed_iform_enum_t iform) {
     return V3_OP_MOVSX;
 
 
+
   case XED_IFORM_DEC_MEMv:
   case XED_IFORM_DEC_MEMb:
     return V3_OP_DEC;
@@ -1324,13 +1375,19 @@ static v3_op_type_t get_opcode(xed_iform_enum_t iform) {
   case XED_IFORM_SETZ_MEMb:
     return V3_OP_SETZ;
 
-
   case XED_IFORM_MOVSB:
   case XED_IFORM_MOVSW:
   case XED_IFORM_MOVSD:
   case XED_IFORM_MOVSQ:
     return V3_OP_MOVS;
 
+  case XED_IFORM_STOSB:
+  case XED_IFORM_STOSW:
+  case XED_IFORM_STOSD:
+  case XED_IFORM_STOSQ:
+    return V3_OP_STOS;
+
+
   default:
     return V3_INVALID_OP;
   }