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.


Added initialization of yield_start_cycle to start_time()
[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  *         Patrick G. Bridges <bridges@cs.unm.edu>
16  *
17  * This is free software.  You are permitted to use,
18  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
19  */
20
21 #include <palacios/vmm_time.h>
22 #include <palacios/vmm.h>
23 #include <palacios/vm_guest.h>
24
25 #ifndef CONFIG_DEBUG_TIME
26 #undef PrintDebug
27 #define PrintDebug(fmt, args...)
28 #endif
29
30 static int handle_cpufreq_hcall(struct guest_info * info, uint_t hcall_id, void * priv_data) {
31     struct vm_time * time_state = &(info->time_state);
32
33     info->vm_regs.rbx = time_state->cpu_freq;
34
35     PrintDebug("Guest request cpu frequency: return %ld\n", (long)info->vm_regs.rbx);
36     
37     return 0;
38 }
39
40
41
42 void v3_init_time(struct guest_info * info) {
43     struct vm_time * time_state = &(info->time_state);
44
45     time_state->cpu_freq = V3_CPU_KHZ();
46  
47     time_state->pause_time = 0;
48     time_state->last_update = 0;
49     time_state->host_offset = 0;
50     
51     INIT_LIST_HEAD(&(time_state->timers));
52     time_state->num_timers = 0;
53
54     v3_register_hypercall(info->vm_info, TIME_CPUFREQ_HCALL, handle_cpufreq_hcall, NULL);
55 }
56
57 int v3_start_time(struct guest_info * info) {
58     /* We start running with guest_time == host_time */
59     uint64_t t = v3_get_host_time(&info->time_state); 
60
61     PrintDebug("Starting initial guest time as %llu\n", t);
62     info->time_state.last_update = t;
63     info->time_state.pause_time = t;
64     info->yield_start_cycle = t;
65     return 0;
66 }
67
68 int v3_pause_time(struct guest_info * info) {
69     V3_ASSERT(info->time_state.pause_time == 0);
70     info->time_state.pause_time = v3_get_guest_time(&info->time_state);
71     PrintDebug("Time paused at guest time as %llu\n", 
72                info->time_state.pause_time);
73     return 0;
74 }
75
76 int v3_resume_time(struct guest_info * info) {
77     uint64_t t = v3_get_host_time(&info->time_state);
78     V3_ASSERT(info->time_state.pause_time != 0);
79     info->time_state.host_offset = 
80         (sint64_t)info->time_state.pause_time - (sint64_t)t;
81 #ifdef CONFIG_TIME_TSC_OFFSET_ADJUST
82     /* XXX Adjust host_offset towards zero based on resolution/accuracy
83      * constraints. */
84 #endif
85     info->time_state.pause_time = 0;
86     PrintDebug("Time resumed paused at guest time as %llu "
87                "offset %lld from host time.\n", t, info->time_state.host_offset);
88
89     return 0;
90 }
91
92 int v3_add_timer(struct guest_info * info, struct vm_timer_ops * ops, 
93              void * private_data) {
94     struct vm_timer * timer = NULL;
95     timer = (struct vm_timer *)V3_Malloc(sizeof(struct vm_timer));
96     V3_ASSERT(timer != NULL);
97
98     timer->ops = ops;
99     timer->private_data = private_data;
100
101     list_add(&(timer->timer_link), &(info->time_state.timers));
102     info->time_state.num_timers++;
103
104     return 0;
105 }
106
107
108 int v3_remove_timer(struct guest_info * info, struct vm_timer * timer) {
109     list_del(&(timer->timer_link));
110     info->time_state.num_timers--;
111
112     V3_Free(timer);
113     return 0;
114 }
115
116 void v3_update_timers(struct guest_info * info) {
117     struct vm_timer * tmp_timer;
118     uint64_t old_time = info->time_state.last_update;
119     uint64_t cycles;
120
121     info->time_state.last_update = v3_get_guest_time(&info->time_state);
122     cycles = info->time_state.last_update - old_time;
123
124     list_for_each_entry(tmp_timer, &(info->time_state.timers), timer_link) {
125         tmp_timer->ops->update_timer(info, cycles, info->time_state.cpu_freq, tmp_timer->private_data);
126     }
127 }