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
* (C) 2006 - Robert Kaiser - FH Wiesbaden
7
****************************************************************************
10
* Author: Rolf Neugebauer and Keir Fraser
11
* Changes: Grzegorz Milos
13
* Description: Simple time and timer functions
15
* Permission is hereby granted, free of charge, to any person obtaining a copy
16
* of this software and associated documentation files (the "Software"), to
17
* deal in the Software without restriction, including without limitation the
18
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
19
* sell copies of the Software, and to permit persons to whom the Software is
20
* furnished to do so, subject to the following conditions:
22
* The above copyright notice and this permission notice shall be included in
23
* all copies or substantial portions of the Software.
25
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31
* DEALINGS IN THE SOFTWARE.
35
#include <mini-os/os.h>
36
#include <mini-os/traps.h>
37
#include <mini-os/types.h>
38
#include <mini-os/hypervisor.h>
39
#include <mini-os/events.h>
40
#include <mini-os/time.h>
41
#include <mini-os/lib.h>
43
/************************************************************************
45
*************************************************************************/
47
/* These are peridically updated in shared_info, and then copied here. */
48
struct shadow_time_info {
49
uint64_t tsc_timestamp; /* TSC at last update of time vals. */
50
uint64_t system_timestamp; /* Time, in nanosecs, since boot. */
51
uint32_t tsc_to_nsec_mul;
52
uint32_t tsc_to_usec_mul;
56
static struct timespec shadow_ts;
57
static uint32_t shadow_ts_version;
59
static struct shadow_time_info shadow;
63
#define rmb() __asm__ __volatile__ ("lock; addl $0,0(%%esp)": : :"memory")
66
#define HANDLE_USEC_OVERFLOW(_tv) \
68
while ( (_tv)->tv_usec >= 1000000 ) \
70
(_tv)->tv_usec -= 1000000; \
75
static inline int time_values_up_to_date(void)
77
struct vcpu_time_info *src = &HYPERVISOR_shared_info->vcpu_info[0].time;
79
return (shadow.version == src->version);
84
* Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
85
* yielding a 64-bit result.
87
static inline uint64_t scale_delta(uint64_t delta, uint32_t mul_frac, int shift)
108
: "=A" (product), "=r" (tmp1), "=r" (tmp2)
109
: "a" ((uint32_t)delta), "1" ((uint32_t)(delta >> 32)), "2" (mul_frac) );
112
"mul %%rdx ; shrd $32,%%rdx,%%rax"
113
: "=a" (product) : "0" (delta), "d" ((uint64_t)mul_frac) );
120
static unsigned long get_nsec_offset(void)
124
delta = now - shadow.tsc_timestamp;
125
return scale_delta(delta, shadow.tsc_to_nsec_mul, shadow.tsc_shift);
129
static void get_time_values_from_xen(void)
131
struct vcpu_time_info *src = &HYPERVISOR_shared_info->vcpu_info[0].time;
134
shadow.version = src->version;
136
shadow.tsc_timestamp = src->tsc_timestamp;
137
shadow.system_timestamp = src->system_time;
138
shadow.tsc_to_nsec_mul = src->tsc_to_system_mul;
139
shadow.tsc_shift = src->tsc_shift;
142
while ((src->version & 1) | (shadow.version ^ src->version));
144
shadow.tsc_to_usec_mul = shadow.tsc_to_nsec_mul / 1000;
150
/* monotonic_clock(): returns # of nanoseconds passed since time_init()
151
* Note: This function is required to return accurate
152
* time even in the absence of multiple timer ticks.
154
uint64_t monotonic_clock(void)
157
uint32_t local_time_version;
160
local_time_version = shadow.version;
162
time = shadow.system_timestamp + get_nsec_offset();
163
if (!time_values_up_to_date())
164
get_time_values_from_xen();
166
} while (local_time_version != shadow.version);
171
static void update_wallclock(void)
173
shared_info_t *s = HYPERVISOR_shared_info;
176
shadow_ts_version = s->wc_version;
178
shadow_ts.tv_sec = s->wc_sec;
179
shadow_ts.tv_nsec = s->wc_nsec;
182
while ((s->wc_version & 1) | (shadow_ts_version ^ s->wc_version));
186
int gettimeofday(struct timeval *tv, void *tz)
188
uint64_t nsec = monotonic_clock();
189
nsec += shadow_ts.tv_nsec;
192
tv->tv_sec = shadow_ts.tv_sec;
193
tv->tv_sec += NSEC_TO_SEC(nsec);
194
tv->tv_usec = NSEC_TO_USEC(nsec % 1000000000UL);
200
void block_domain(s_time_t until)
203
gettimeofday(&tv, NULL);
204
ASSERT(irqs_disabled());
205
if(monotonic_clock() < until)
207
HYPERVISOR_set_timer_op(until);
208
HYPERVISOR_sched_op(SCHEDOP_block, 0);
217
static void timer_handler(evtchn_port_t ev, struct pt_regs *regs, void *ign)
219
get_time_values_from_xen();
225
static evtchn_port_t port;
228
printk("Initialising timer interface\n");
229
port = bind_virq(VIRQ_TIMER, &timer_handler, NULL);
235
/* Clear any pending timer */
236
HYPERVISOR_set_timer_op(0);