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) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm_time.h>
21 #include <palacios/vmm.h>
22 #include <palacios/vm_guest.h>
24 static int handle_cpufreq_hcall(struct guest_info * info, uint_t hcall_id, void * priv_data) {
25 struct vm_time * time_state = &(info->time_state);
27 info->vm_regs.rbx = time_state->cpu_freq;
29 PrintDebug("Guest request cpu frequency: return %ld\n", (long)info->vm_regs.rbx);
36 void v3_init_time(struct guest_info * info) {
37 struct vm_time * time_state = &(info->time_state);
39 time_state->cpu_freq = V3_CPU_KHZ();
41 time_state->pause_time = 0;
42 time_state->last_update = 0;
43 time_state->host_offset = 0;
45 INIT_LIST_HEAD(&(time_state->timers));
46 time_state->num_timers = 0;
48 v3_register_hypercall(info->vm_info, TIME_CPUFREQ_HCALL, handle_cpufreq_hcall, NULL);
51 uint64_t v3_get_host_time(struct guest_info * info) {
57 uint64_t v3_get_guest_time(struct guest_info * info) {
58 return v3_get_host_time(info) + info->time_state.host_offset;
62 int v3_start_time(struct guest_info * info) {
63 /* We start running with guest_time == host_time */
64 uint64_t t = v3_get_host_time(info);
65 info->time_state.last_update = t;
66 info->time_state.pause_time = t;
70 int v3_pause_time(struct guest_info * info) {
71 V3_ASSERT(info->time_state.pause_time == 0);
72 info->time_state.pause_time = v3_get_guest_time(info);
76 int v3_resume_time(struct guest_info * info) {
77 uint64_t t = v3_get_host_time(info);
78 V3_ASSERT(info->time_state.pause_time != 0);
79 #ifdef OPTION_TIME_ADJUST_TSC_OFFSET
81 info->time_state.host_offset =
82 (sint64_t)info->time_state.pause_time - (sint64_t)t;
83 info->time_state.pause_time = 0;
87 int v3_add_timer(struct guest_info * info, struct vm_timer_ops * ops,
88 void * private_data) {
89 struct vm_timer * timer = NULL;
90 timer = (struct vm_timer *)V3_Malloc(sizeof(struct vm_timer));
91 V3_ASSERT(timer != NULL);
94 timer->private_data = private_data;
96 list_add(&(timer->timer_link), &(info->time_state.timers));
97 info->time_state.num_timers++;
103 int v3_remove_timer(struct guest_info * info, struct vm_timer * timer) {
104 list_del(&(timer->timer_link));
105 info->time_state.num_timers--;
111 void v3_update_timers(struct guest_info * info) {
112 struct vm_timer * tmp_timer;
113 uint64_t old_time = info->time_state.last_update;
116 info->time_state.last_update = v3_get_guest_time(info);
117 cycles = info->time_state.last_update - old_time;
119 list_for_each_entry(tmp_timer, &(info->time_state.timers), timer_link) {
120 tmp_timer->ops->update_timer(info, cycles, info->time_state.cpu_freq, tmp_timer->private_data);