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.


initial simulation functionality
[palacios.git] / palacios / src / palacios / vmm_time.c
index 2295b3f..9c0fe18 100644 (file)
@@ -58,7 +58,7 @@
  * (1) Add support for temporarily skewing guest time off of where it should
  *     be to support slack simulation of guests. The idea is that simulators
  *     set this skew to be the difference between how much time passed for a 
- *     simulated feature and a real implementation of that feature, making 
+ *     simulated feature and a real implementation of that feature, making time
  *     pass at a different rate from real time on this core. The VMM will then
  *     attempt to move this skew back towards 0 subject to resolution/accuracy
  *     constraints from various system timers.
  *     The main effort in doing this will be to get accuracy/resolution 
  *     information from each local timer and to use this to bound how much skew
  *     is removed on each exit.
+ *
+ * (2) Look more into sychronizing the offsets *across* virtual and physical 
+ *     cores so that multicore guests stay mostly in sync.
+ *
+ * (3) Look into using the AMD TSC multiplier feature and adding explicit time
+ *     dilation support to time handling.
  */
 
 
 static int handle_cpufreq_hcall(struct guest_info * info, uint_t hcall_id, void * priv_data) {
-    struct vm_time * time_state = &(info->time_state);
+    struct vm_core_time * time_state = &(info->time_state);
 
     info->vm_regs.rbx = time_state->guest_cpu_freq;
 
@@ -96,9 +102,41 @@ int v3_start_time(struct guest_info * info) {
     return 0;
 }
 
+int v3_pause_time( struct guest_info * info )
+{
+    struct vm_core_time * time_state = &(info->time_state);
+    if (time_state->pause_time != 0) {
+       PrintError("Attempted to pause time when time already paused.\n");
+       return -1;
+    }
+    time_state->pause_time = v3_get_host_time( time_state );
+
+    return 0;
+}
+
+int v3_resume_time( struct guest_info * info )
+{
+    struct vm_core_time * time_state = &(info->time_state);
+    uint64_t host_time, guest_time;
+    sint64_t offset;
+
+    if (time_state->pause_time == 0) {
+       PrintError("Attempted to resume time when time not paused.\n");
+       return -1;
+    }
+
+    host_time = v3_get_host_time(time_state);
+    guest_time = v3_compute_guest_time(time_state, host_time);
+    offset = (sint64_t)guest_time - (sint64_t)host_time;
+    time_state->guest_host_offset = offset;
+    time_state->pause_time = 0;
+
+    return 0;
+}
+
 int v3_offset_time( struct guest_info * info, sint64_t offset )
 {
-    struct vm_time * time_state = &(info->time_state);
+    struct vm_core_time * time_state = &(info->time_state);
     PrintDebug("Adding additional offset of %lld to guest time.\n", offset);
     time_state->guest_host_offset += offset;
     return 0;
@@ -107,7 +145,7 @@ int v3_offset_time( struct guest_info * info, sint64_t offset )
 #ifdef V3_CONFIG_TIME_DILATION
 static uint64_t compute_target_host_time(struct guest_info * info, uint64_t guest_time)
 {
-    struct vm_time * time_state = &(info->time_state);
+    struct vm_core_time * time_state = &(info->time_state);
     uint64_t guest_elapsed, desired_elapsed;
     
     guest_elapsed = (guest_time - time_state->initial_time);
@@ -117,7 +155,7 @@ static uint64_t compute_target_host_time(struct guest_info * info, uint64_t gues
 
 static uint64_t compute_target_guest_time(struct guest_info *info)
 {
-    struct vm_time * time_state = &(info->time_state);
+    struct vm_core_time * time_state = &(info->time_state);
     uint64_t host_elapsed, desired_elapsed;
 
     host_elapsed = v3_get_host_time(time_state) - time_state->initial_time;
@@ -130,7 +168,7 @@ static uint64_t compute_target_guest_time(struct guest_info *info)
 /* Yield time in the host to deal with a guest that wants to run slower than 
  * the native host cycle frequency */
 static int yield_host_time(struct guest_info * info) {
-    struct vm_time * time_state = &(info->time_state);
+    struct vm_core_time * time_state = &(info->time_state);
     uint64_t host_time, target_host_time;
     uint64_t guest_time, old_guest_time;
 
@@ -158,7 +196,7 @@ static int yield_host_time(struct guest_info * info) {
 }
 
 static int skew_guest_time(struct guest_info * info) {
-    struct vm_time * time_state = &(info->time_state);
+    struct vm_core_time * time_state = &(info->time_state);
     uint64_t target_guest_time, guest_time;
     /* Now the host may have gotten ahead of the guest because
      * yielding is a coarse grained thing. Figure out what guest time
@@ -175,7 +213,7 @@ static int skew_guest_time(struct guest_info * info) {
        if (time_state->enter_time) {
            /* Limit forward skew to 10% of the amount the guest has
             * run since we last could skew time */
-           max_skew = (sint64_t)(guest_time - time_state->enter_time) / 10.0;
+           max_skew = (sint64_t)(guest_time - time_state->enter_time) / 10;
        } else {
            max_skew = 0;
        }
@@ -212,12 +250,16 @@ int v3_adjust_time(struct guest_info * info) {
 
 /* Called immediately upon entry in the the VMM */
 int 
-v3_time_exit_vm( struct guest_info * info ) 
+v3_time_exit_vm( struct guest_info * info, uint64_t * guest_cycles ) 
 {
-    struct vm_time * time_state = &(info->time_state);
+    struct vm_core_time * time_state = &(info->time_state);
     
     time_state->exit_time = v3_get_host_time(time_state);
 
+#ifdef V3_CONFIG_TIME_DILATION
+    v3_pause_time( info );
+#endif
+
     return 0;
 }
 
@@ -225,22 +267,13 @@ v3_time_exit_vm( struct guest_info * info )
 int 
 v3_time_enter_vm( struct guest_info * info )
 {
-    struct vm_time * time_state = &(info->time_state);
+    struct vm_core_time * time_state = &(info->time_state);
     uint64_t host_time;
 
     host_time = v3_get_host_time(time_state);
     time_state->enter_time = host_time;
 #ifdef V3_CONFIG_TIME_DILATION
-    { 
-        uint64_t guest_time;
-       sint64_t offset;
-        guest_time = v3_compute_guest_time(time_state, host_time);
-       // XXX we probably want to use an inline function to do these
-        // time differences to deal with sign and overflow carefully
-       offset = (sint64_t)guest_time - (sint64_t)host_time;
-       PrintDebug("v3_time_enter_vm: guest time offset %lld from host time.\n", offset);
-        time_state->guest_host_offset = offset;
-    }
+    v3_resume_time( info );
 #else
     time_state->guest_host_offset = 0;
 #endif
@@ -275,7 +308,7 @@ int v3_remove_timer(struct guest_info * info, struct v3_timer * timer) {
 }
 
 void v3_update_timers(struct guest_info * info) {
-    struct vm_time *time_state = &info->time_state;
+    struct vm_core_time *time_state = &info->time_state;
     struct v3_timer * tmp_timer;
     sint64_t cycles;
     uint64_t old_time = info->time_state.last_update;
@@ -290,6 +323,7 @@ void v3_update_timers(struct guest_info * info) {
     }
 }
 
+
 /* 
  * Handle full virtualization of the time stamp counter.  As noted
  * above, we don't store the actual value of the TSC, only the guest's
@@ -359,7 +393,7 @@ int v3_handle_rdtscp(struct guest_info * info) {
 
 static int tsc_aux_msr_read_hook(struct guest_info *info, uint_t msr_num, 
                                 struct v3_msr *msr_val, void *priv) {
-    struct vm_time * time_state = &(info->time_state);
+    struct vm_core_time * time_state = &(info->time_state);
 
     V3_ASSERT(msr_num == TSC_AUX_MSR);
 
@@ -371,7 +405,7 @@ static int tsc_aux_msr_read_hook(struct guest_info *info, uint_t msr_num,
 
 static int tsc_aux_msr_write_hook(struct guest_info *info, uint_t msr_num, 
                              struct v3_msr msr_val, void *priv) {
-    struct vm_time * time_state = &(info->time_state);
+    struct vm_core_time * time_state = &(info->time_state);
 
     V3_ASSERT(msr_num == TSC_AUX_MSR);
 
@@ -395,7 +429,7 @@ static int tsc_msr_read_hook(struct guest_info *info, uint_t msr_num,
 
 static int tsc_msr_write_hook(struct guest_info *info, uint_t msr_num,
                             struct v3_msr msr_val, void *priv) {
-    struct vm_time * time_state = &(info->time_state);
+    struct vm_core_time * time_state = &(info->time_state);
     uint64_t guest_time, new_tsc;
 
     V3_ASSERT(msr_num == TSC_MSR);
@@ -431,6 +465,9 @@ int v3_init_time_vm(struct v3_vm_info * vm) {
     ret = v3_register_hypercall(vm, TIME_CPUFREQ_HCALL, 
                                handle_cpufreq_hcall, NULL);
 
+    PrintDebug("Setting base time dilation factor.\n");
+    vm->time_state.td_mult = 1;
+
     return ret;
 }
 
@@ -442,7 +479,7 @@ void v3_deinit_time_vm(struct v3_vm_info * vm) {
 }
 
 void v3_init_time_core(struct guest_info * info) {
-    struct vm_time * time_state = &(info->time_state);
+    struct vm_core_time * time_state = &(info->time_state);
     v3_cfg_tree_t * cfg_tree = info->core_cfg_data;
     char * khz = NULL;
 
@@ -471,6 +508,10 @@ void v3_init_time_core(struct guest_info * info) {
     time_state->last_update = 0;
     time_state->guest_host_offset = 0;
     time_state->tsc_guest_offset = 0;
+    time_state->enter_time = 0;
+    time_state->exit_time = 0;
+    time_state->pause_time = 0;
+
 
     INIT_LIST_HEAD(&(time_state->timers));
     time_state->num_timers = 0;
@@ -481,7 +522,7 @@ void v3_init_time_core(struct guest_info * info) {
 
 
 void v3_deinit_time_core(struct guest_info * core) {
-    struct vm_time * time_state = &(core->time_state);
+    struct vm_core_time * time_state = &(core->time_state);
     struct v3_timer * tmr = NULL;
     struct v3_timer * tmp = NULL;