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.


25eed0233573e9c678b4781b3b5021aff23a9ffd
[palacios.git] / palacios / include / palacios / vmm_time.h
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) 2010, Patrick Bridges <bridges@cs.unm.edu>
11  * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> 
12  * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org> 
13  * All rights reserved.
14  *
15  * Author: Jack Lange <jarusl@cs.northwestern.edu>
16  *         Patrick Bridges <bridges@cs.unm.edu>
17  *
18  * This is free software.  You are permitted to use,
19  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20  */
21
22 #ifndef __VMM_TIME_H
23 #define __VMM_TIME_H
24
25 #ifdef __V3VEE__
26
27 #include <palacios/vmm_types.h>
28 #include <palacios/vmm_list.h>
29 #include <palacios/vmm_msr.h>
30 #include <palacios/vmm_util.h>
31
32 struct guest_info;
33
34 struct vm_time {
35     uint32_t host_cpu_freq;    // in kHZ 
36     uint32_t guest_cpu_freq;   // can be lower than host CPU freq!
37
38     sint64_t guest_host_offset;// Offset of monotonic guest time from host time
39     sint64_t tsc_guest_offset; // Offset of guest TSC from monotonic guest time
40
41     uint64_t last_update;      // Last time (in monotonic guest time) the 
42                                // timers were updated
43
44     uint64_t initial_time;     // Time when VMM started. 
45     uint64_t enter_time;       // Host time the guest was last entered
46     uint64_t exit_time;        // Host time the the VM was exited to
47     struct v3_msr tsc_aux;     // Auxilliary MSR for RDTSCP
48
49     // Installed Timers slaved off of the guest monotonic TSC
50     uint_t num_timers;
51     struct list_head timers;
52
53     // Installed timeout handlers, and the time (in monotonic guest time) of hte 
54     // next timeout.
55     uint64_t next_timeout; 
56     struct list_head timeout_hooks;
57 };
58
59 struct v3_timer_ops {
60     void (*update_timer)(struct guest_info * info, ullong_t cpu_cycles, ullong_t cpu_freq, void * priv_data);
61     void (*advance_timer)(struct guest_info * info, void * private_data);
62 };
63
64 struct v3_timer {
65     void * private_data;
66     struct v3_timer_ops * ops;
67
68     // Need to add accuracy/resolution fields later.
69
70     struct list_head timer_link;
71 };
72
73 typedef void (*v3_timeout_callback_t)(struct guest_info * info, void * priv_data);
74 struct v3_timeout_hook {
75     void * private_data;
76     v3_timeout_callback_t callback;
77     
78     struct list_head hook_link;
79 };
80
81 // Basic functions for handling passage of time in palacios
82 void v3_init_time_core(struct guest_info * core);
83 int v3_init_time_vm(struct v3_vm_info * vm);
84
85 void v3_deinit_time_core(struct guest_info * core);
86 void v3_deinit_time_vm(struct v3_vm_info * vm);
87
88 int v3_start_time(struct guest_info * core);
89
90 int v3_time_enter_vm(struct guest_info * core);
91 int v3_time_exit_vm(struct guest_info * core);
92
93 int v3_adjust_time(struct guest_info * core);
94 int v3_offset_time(struct guest_info * core, sint64_t offset);
95
96 // Basic functions for attaching timers to the passage of time - these timers 
97 // should eventually specify their accuracy and resolution.
98 struct v3_timer * v3_add_timer(struct guest_info * info, struct v3_timer_ops * ops, void * private_data);
99 int v3_remove_timer(struct guest_info * info, struct v3_timer * timer);
100 void v3_update_timers(struct guest_info * info);
101
102 // Functions for handling one-shot timeouts in Palacios. Note that only one
103 // timeout is every currently outstanding (the soonest scheduled one!), and that
104 // all hooks are called on any timeout. If a hook gets called before the desired
105 // timeout time, that hook should reschedule its own timeout if desired.
106 struct v3_timeout_hook * v3_add_timeout_hook(struct guest_info * info, v3_timeout_callback_t callback, void * priv_data);
107 int v3_remove_timeout_hook(struct guest_info * info, struct v3_timeout_hook * hook);
108 int v3_schedule_timeout(struct guest_info * info, ullong_t cycles);
109 int v3_check_timeout(struct guest_info * info);
110
111 // Functions to return the different notions of time in Palacios.
112 static inline uint64_t v3_get_host_time(struct vm_time *t) {
113     uint64_t tmp;
114     rdtscll(tmp);
115     return tmp;
116 }
117
118 // Returns *monotonic* guest time.
119 static inline uint64_t v3_compute_guest_time(struct vm_time *t, uint64_t ht) {
120 #ifdef V3_CONFIG_TIME_HIDE_VM_COST
121     V3_ASSERT(t->exit_time);
122     return t->exit_time + t->guest_host_offset;
123 #else
124     return v3_get_host_time(t) + t->guest_host_offset;
125 #endif
126 }
127
128 static inline uint64_t v3_get_guest_time(struct vm_time *t) {
129     return v3_compute_guest_time(t, v3_get_host_time(t));
130 }
131
132 // Returns the TSC value seen by the guest
133 static inline uint64_t v3_compute_guest_tsc(struct vm_time *t, uint64_t ht) {
134     return v3_compute_guest_time(t, ht) + t->tsc_guest_offset;
135 }
136
137 static inline uint64_t v3_get_guest_tsc(struct vm_time *t) {
138     return v3_compute_guest_tsc(t, v3_get_host_time(t));
139 }
140
141 // Returns offset of guest TSC from host TSC
142 static inline sint64_t v3_tsc_host_offset(struct vm_time *time_state) {
143     return time_state->guest_host_offset + time_state->tsc_guest_offset;
144 }
145
146 // Functions for handling exits on the TSC when fully virtualizing 
147 // the timestamp counter.
148 #define TSC_MSR     0x10
149 #define TSC_AUX_MSR 0xC0000103
150
151 int v3_handle_rdtscp(struct guest_info *info);
152 int v3_handle_rdtsc(struct guest_info *info);
153
154
155
156
157 #endif // !__V3VEE__
158
159 #endif