ia64/xen-unstable

view extras/mini-os/time.c @ 6515:ac8cae1f2c47

Don't attempt to create paravirtualized devices for VMX domains for now.

Signed-off-by: Arun Sharma <arun.sharma@intel.com>
author adsharma@los-vmm.sc.intel.com
date Tue Aug 09 11:06:45 2005 -0800 (2005-08-09)
parents a83ac0806d6b
children 0cf2430f520f 6721abf6b16d f51fe43c5d1c 6783e59e1c45 8799d14bef77
line source
1 /* -*- Mode:C; c-basic-offset:4; tab-width:4 -*-
2 ****************************************************************************
3 * (C) 2003 - Rolf Neugebauer - Intel Research Cambridge
4 * (C) 2002-2003 - Keir Fraser - University of Cambridge
5 * (C) 2005 - Grzegorz Milos - Intel Research Cambridge
6 ****************************************************************************
7 *
8 * File: time.c
9 * Author: Rolf Neugebauer and Keir Fraser
10 * Changes: Grzegorz Milos
11 *
12 * Description: Simple time and timer functions
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to
16 * deal in the Software without restriction, including without limitation the
17 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 * sell copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 * DEALINGS IN THE SOFTWARE.
31 */
34 #include <os.h>
35 #include <traps.h>
36 #include <types.h>
37 #include <hypervisor.h>
38 #include <events.h>
39 #include <time.h>
40 #include <lib.h>
42 /************************************************************************
43 * Time functions
44 *************************************************************************/
46 /* Cached *multiplier* to convert TSC counts to microseconds.
47 * (see the equation below).
48 * Equal to 2^32 * (1 / (clocks per usec) ).
49 * Initialized in time_init.
50 */
51 static unsigned long fast_gettimeoffset_quotient;
54 /* These are peridically updated in shared_info, and then copied here. */
55 static u32 shadow_tsc_stamp;
56 static s64 shadow_system_time;
57 static u32 shadow_time_version;
58 static struct timeval shadow_tv;
60 #ifndef rmb
61 #define rmb() __asm__ __volatile__ ("lock; addl $0,0(%%esp)": : :"memory")
62 #endif
64 #define HANDLE_USEC_OVERFLOW(_tv) \
65 do { \
66 while ( (_tv).tv_usec >= 1000000 ) \
67 { \
68 (_tv).tv_usec -= 1000000; \
69 (_tv).tv_sec++; \
70 } \
71 } while ( 0 )
73 static void get_time_values_from_xen(void)
74 {
75 do {
76 shadow_time_version = HYPERVISOR_shared_info->time_version2;
77 rmb();
78 shadow_tv.tv_sec = HYPERVISOR_shared_info->wc_sec;
79 shadow_tv.tv_usec = HYPERVISOR_shared_info->wc_usec;
80 shadow_tsc_stamp = (u32)HYPERVISOR_shared_info->tsc_timestamp;
81 shadow_system_time = HYPERVISOR_shared_info->system_time;
82 rmb();
83 }
84 while ( shadow_time_version != HYPERVISOR_shared_info->time_version1 );
85 }
88 #define TIME_VALUES_UP_TO_DATE \
89 (shadow_time_version == HYPERVISOR_shared_info->time_version2)
91 static u32 get_time_delta_usecs(void)
92 {
93 register unsigned long eax, edx;
95 /* Read the Time Stamp Counter */
97 rdtsc(eax,edx);
99 /* .. relative to previous jiffy (32 bits is enough) */
100 eax -= shadow_tsc_stamp;
102 /*
103 * Time offset = (tsc_low delta) * fast_gettimeoffset_quotient
104 * = (tsc_low delta) * (usecs_per_clock)
105 * = (tsc_low delta) * (usecs_per_jiffy / clocks_per_jiffy)
106 *
107 * Using a mull instead of a divl saves up to 31 clock cycles
108 * in the critical path.
109 */
111 __asm__("mull %2"
112 :"=a" (eax), "=d" (edx)
113 :"rm" (fast_gettimeoffset_quotient),
114 "0" (eax));
116 /* our adjusted time offset in microseconds */
117 return edx;
118 }
120 s64 get_s_time (void)
121 {
122 u64 u_delta;
123 s64 ret;
125 again:
127 u_delta = get_time_delta_usecs();
128 ret = shadow_system_time + (1000 * u_delta);
130 if ( unlikely(!TIME_VALUES_UP_TO_DATE) )
131 {
132 /*
133 * We may have blocked for a long time, rendering our calculations
134 * invalid (e.g. the time delta may have overflowed). Detect that
135 * and recalculate with fresh values.
136 */
137 get_time_values_from_xen();
138 goto again;
139 }
141 return ret;
142 }
144 void gettimeofday(struct timeval *tv)
145 {
146 struct timeval _tv;
148 do {
149 get_time_values_from_xen();
150 _tv.tv_usec = get_time_delta_usecs();
151 _tv.tv_sec = shadow_tv.tv_sec;
152 _tv.tv_usec += shadow_tv.tv_usec;
153 }
154 while ( unlikely(!TIME_VALUES_UP_TO_DATE) );
156 HANDLE_USEC_OVERFLOW(_tv);
157 *tv = _tv;
158 }
160 static void print_current_time(void)
161 {
162 struct timeval tv;
164 get_time_values_from_xen();
166 gettimeofday(&tv);
167 printk("T(s=%ld us=%ld)\n", tv.tv_sec, tv.tv_usec);
168 }
170 void block(u32 millisecs)
171 {
172 struct timeval tv;
173 gettimeofday(&tv);
174 //printk("tv.tv_sec=%ld, tv.tv_usec=%ld, shadow_system_time=%lld\n", tv.tv_sec, tv.tv_usec, shadow_system_time );
175 HYPERVISOR_set_timer_op(get_s_time() + 1000000LL * (s64) millisecs);
176 HYPERVISOR_block();
177 }
180 /*
181 * Just a dummy
182 */
183 static void timer_handler(int ev, struct pt_regs *regs)
184 {
185 static int i;
187 get_time_values_from_xen();
189 i++;
190 if (i >= 1000) {
191 print_current_time();
192 i = 0;
193 }
194 }
198 void init_time(void)
199 {
200 u64 __cpu_khz;
201 unsigned long cpu_khz;
203 __cpu_khz = HYPERVISOR_shared_info->cpu_freq;
205 cpu_khz = (u32) (__cpu_khz/1000);
207 printk("Xen reported: %lu.%03lu MHz processor.\n",
208 cpu_khz / 1000, cpu_khz % 1000);
209 /* (10^6 * 2^32) / cpu_hz = (10^3 * 2^32) / cpu_khz =
210 (2^32 * 1 / (clocks/us)) */
211 {
212 unsigned long eax=0, edx=1000;
213 __asm__("divl %2"
214 :"=a" (fast_gettimeoffset_quotient), "=d" (edx)
215 :"r" (cpu_khz),
216 "0" (eax), "1" (edx));
217 }
219 bind_virq(VIRQ_TIMER, &timer_handler);
220 }