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 testing of new time handling on VMX, initial implementation of new
[palacios.git] / palacios / src / palacios / vmm_time.c
1 /* 
2  * This file is part of the Palacios Virtual Machine Monitor developed
3  * by the V3VEE Project with funding from the United States National 
4  * Science Foundation and the Department of Energy.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
10  * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> 
11  * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #include <palacios/vmm_time.h>
21 #include <palacios/vmm.h>
22 #include <palacios/vm_guest.h>
23
24 #ifndef CONFIG_DEBUG_TIME
25 #undef PrintDebug
26 #define PrintDebug(fmt, args...)
27 #endif
28
29 static int handle_cpufreq_hcall(struct guest_info * info, uint_t hcall_id, void * priv_data) {
30     struct vm_time * time_state = &(info->time_state);
31
32     info->vm_regs.rbx = time_state->cpu_freq;
33
34     PrintDebug("Guest request cpu frequency: return %ld\n", (long)info->vm_regs.rbx);
35     
36     return 0;
37 }
38
39
40
41 void v3_init_time(struct guest_info * info) {
42     struct vm_time * time_state = &(info->time_state);
43
44     time_state->cpu_freq = V3_CPU_KHZ();
45  
46     time_state->pause_time = 0;
47     time_state->last_update = 0;
48     time_state->host_offset = 0;
49     
50     INIT_LIST_HEAD(&(time_state->timers));
51     time_state->num_timers = 0;
52
53     v3_register_hypercall(info->vm_info, TIME_CPUFREQ_HCALL, handle_cpufreq_hcall, NULL);
54 }
55
56 uint64_t v3_get_host_time(struct guest_info * info) {
57     uint64_t tmp;
58     rdtscll(tmp);
59     return tmp;
60 }
61
62 uint64_t v3_get_guest_time(struct guest_info * info) {
63     return v3_get_host_time(info) + info->time_state.host_offset;
64 }
65
66
67 int v3_start_time(struct guest_info * info) {
68     /* We start running with guest_time == host_time */
69     uint64_t t = v3_get_host_time(info); 
70
71     PrintDebug("Starting initial guest time as %llu\n", t);
72     info->time_state.last_update = t;
73     info->time_state.pause_time = t;
74     return 0;
75 }
76
77 int v3_pause_time(struct guest_info * info) {
78     V3_ASSERT(info->time_state.pause_time == 0);
79     info->time_state.pause_time = v3_get_guest_time(info);
80     PrintDebug("Time paused at guest time as %llu\n", 
81                info->time_state.pause_time);
82     return 0;
83 }
84
85 int v3_resume_time(struct guest_info * info) {
86     uint64_t t = v3_get_host_time(info);
87     V3_ASSERT(info->time_state.pause_time != 0);
88     info->time_state.host_offset = 
89         (sint64_t)info->time_state.pause_time - (sint64_t)t;
90 #ifdef OPTION_TIME_ADJUST_TSC_OFFSET
91     /* XXX Adjust host_offset towards zero based on resolution/accuracy
92      * constraints. */
93 #endif
94     info->time_state.pause_time = 0;
95     PrintDebug("Time resumed paused at guest time as %llu "
96                "offset %lld from host time.\n", t, info->time_state.host_offset);
97
98     return 0;
99 }
100
101 int v3_add_timer(struct guest_info * info, struct vm_timer_ops * ops, 
102              void * private_data) {
103     struct vm_timer * timer = NULL;
104     timer = (struct vm_timer *)V3_Malloc(sizeof(struct vm_timer));
105     V3_ASSERT(timer != NULL);
106
107     timer->ops = ops;
108     timer->private_data = private_data;
109
110     list_add(&(timer->timer_link), &(info->time_state.timers));
111     info->time_state.num_timers++;
112
113     return 0;
114 }
115
116
117 int v3_remove_timer(struct guest_info * info, struct vm_timer * timer) {
118     list_del(&(timer->timer_link));
119     info->time_state.num_timers--;
120
121     V3_Free(timer);
122     return 0;
123 }
124
125 void v3_update_timers(struct guest_info * info) {
126     struct vm_timer * tmp_timer;
127     uint64_t old_time = info->time_state.last_update;
128     uint64_t cycles;
129
130     info->time_state.last_update = v3_get_guest_time(info);
131     cycles = info->time_state.last_update - old_time;
132
133     list_for_each_entry(tmp_timer, &(info->time_state.timers), timer_link) {
134         tmp_timer->ops->update_timer(info, cycles, info->time_state.cpu_freq, tmp_timer->private_data);
135     }
136 }