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.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
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.
15 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * Patrick Bridges <bridges@cs.unm.edu>
18 * This is free software. You are permitted to use,
19 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
27 #include <palacios/vmm_types.h>
28 #include <palacios/vmm_list.h>
29 #include <palacios/vmm_msr.h>
30 #include <palacios/vmm_util.h>
34 /* Per-VM time information */
39 /* Per-core time information */
41 uint32_t host_cpu_freq; // in kHZ
42 uint32_t guest_cpu_freq; // can be lower than host CPU freq!
44 sint64_t guest_host_offset;// Offset of monotonic guest time from host time
45 sint64_t tsc_guest_offset; // Offset of guest TSC from monotonic guest time
47 uint64_t last_update; // Last time (in monotonic guest time) the
48 // timers were updated
50 uint64_t initial_time; // Time when VMM started.
51 uint64_t enter_time; // Host time the guest was last entered
52 uint64_t exit_time; // Host time the the VM was exited to
53 uint64_t pause_time; // Time at which the VM core was paused
54 struct v3_msr tsc_aux; // Auxilliary MSR for RDTSCP
56 // Installed Timers slaved off of the guest monotonic TSC
58 struct list_head timers;
60 // Installed timeout handlers, and the time (in monotonic guest time) of hte
62 uint64_t next_timeout;
63 struct list_head timeout_hooks;
67 void (*update_timer)(struct guest_info * info, ullong_t cpu_cycles, ullong_t cpu_freq, void * priv_data);
68 void (*advance_timer)(struct guest_info * info, void * private_data);
73 struct v3_timer_ops * ops;
75 // Need to add accuracy/resolution fields later.
77 struct list_head timer_link;
80 typedef void (*v3_timeout_callback_t)(struct guest_info * info, void * priv_data);
81 struct v3_timeout_hook {
83 v3_timeout_callback_t callback;
85 struct list_head hook_link;
88 // Basic functions for handling passage of time in palacios
89 void v3_init_time_core(struct guest_info * core);
90 int v3_init_time_vm(struct v3_vm_info * vm);
92 void v3_deinit_time_core(struct guest_info * core);
93 void v3_deinit_time_vm(struct v3_vm_info * vm);
95 int v3_start_time(struct guest_info * core);
97 int v3_time_enter_vm(struct guest_info * core);
98 int v3_time_exit_vm(struct guest_info * core);
100 int v3_pause_time(struct guest_info * core);
101 int v3_resume_time(struct guest_info * core);
102 int v3_offset_time(struct guest_info * core, sint64_t offset);
104 int v3_adjust_time(struct guest_info * core);
106 // Basic functions for attaching timers to the passage of time - these timers
107 // should eventually specify their accuracy and resolution.
108 struct v3_timer * v3_add_timer(struct guest_info * info, struct v3_timer_ops * ops, void * private_data);
109 int v3_remove_timer(struct guest_info * info, struct v3_timer * timer);
110 void v3_update_timers(struct guest_info * info);
112 // Functions for handling one-shot timeouts in Palacios. Note that only one
113 // timeout is every currently outstanding (the soonest scheduled one!), and that
114 // all hooks are called on any timeout. If a hook gets called before the desired
115 // timeout time, that hook should reschedule its own timeout if desired.
116 struct v3_timeout_hook * v3_add_timeout_hook(struct guest_info * info, v3_timeout_callback_t callback, void * priv_data);
117 int v3_remove_timeout_hook(struct guest_info * info, struct v3_timeout_hook * hook);
118 int v3_schedule_timeout(struct guest_info * info, ullong_t cycles);
119 int v3_check_timeout(struct guest_info * info);
121 // Functions to return the different notions of time in Palacios.
122 static inline uint64_t v3_get_host_time(struct vm_core_time *t) {
128 // Returns *monotonic* guest time.
129 static inline uint64_t v3_compute_guest_time(struct vm_core_time *t, uint64_t ht) {
131 return t->pause_time + t->guest_host_offset;
133 return ht + t->guest_host_offset;
136 static inline uint64_t v3_get_guest_time(struct vm_core_time *t) {
137 return v3_compute_guest_time(t, v3_get_host_time(t));
140 // Returns the TSC value seen by the guest
141 static inline uint64_t v3_compute_guest_tsc(struct vm_core_time *t, uint64_t ht) {
142 return v3_compute_guest_time(t, ht) + t->tsc_guest_offset;
145 static inline uint64_t v3_get_guest_tsc(struct vm_core_time *t) {
146 return v3_compute_guest_tsc(t, v3_get_host_time(t));
149 // Returns offset of guest TSC from host TSC
150 static inline sint64_t v3_tsc_host_offset(struct vm_core_time *time_state) {
151 return time_state->guest_host_offset + time_state->tsc_guest_offset;
154 // Functions for handling exits on the TSC when fully virtualizing
155 // the timestamp counter.
157 #define TSC_AUX_MSR 0xC0000103
159 int v3_handle_rdtscp(struct guest_info *info);
160 int v3_handle_rdtsc(struct guest_info *info);