summaryrefslogtreecommitdiff
path: root/sys/src/9/xen/xentimer.c
blob: 2a57cc5b9ae86feb76457be0a19de1d6a6a17b51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"

static vcpu_time_info_t shadow[MAX_VIRT_CPUS];		// XXX should be in Mach
static ulong wallclock;
static ulong wallclocksystime;

/*
 * Return a consistent set of time parameters.
 */
static vcpu_time_info_t *
getshadow(void)
{
	vcpu_time_info_t *s, *t;

	t = &HYPERVISOR_shared_info->vcpu_info[m->machno].time;
	s = &shadow[m->machno];		// XXX place in mach struct
	while(t->version != s->version) {
		if (t->version&1)
			continue;
		s->version = t->version;
		s->tsc_timestamp = t->tsc_timestamp;
		s->system_time = t->system_time;
		s->tsc_to_system_mul = t->tsc_to_system_mul;
		s->tsc_shift = t->tsc_shift;
	}
	return s;
}


/* just get it from the shared info */
void
guesscpuhz(int) // XXX no arg!
{
	vcpu_time_info_t *t;

	t = getshadow();
	m->cpuhz = (1000000000LL << 32) / t->tsc_to_system_mul;
	if(t->tsc_shift < 0)
		m->cpuhz <<= -t->tsc_shift;
	else
		m->cpuhz >>= t->tsc_shift;
	m->cpumhz = m->cpuhz / 1000000L;
}

void
xentimerset(uvlong next)
{
	uvlong soon;

	soon = fastticks(0) + 100000;
	if (next < soon)
		next = soon;
	HYPERVISOR_set_timer_op(next);
}

void
xentimerclock(Ureg* ureg, void*)
{

	timerintr(ureg, 0);
}

void
xentimerenable(void)
{
	intrenable(VIRQ_TIMER, xentimerclock, nil, 0, "Xen Timer");
}

uvlong
xentimerread(uvlong *hz)
{
	uvlong x;
	uvlong delta, sdelta;
	vcpu_time_info_t *t;

	t = getshadow();
	cycles(&x);
	delta = x - t->tsc_timestamp;
	if (t->tsc_shift < 0)
		delta >>= -t->tsc_shift;
	else
		delta <<= t->tsc_shift;
	mul64fract(&sdelta, delta, t->tsc_to_system_mul);
	x = t->system_time + sdelta;
	if (HYPERVISOR_shared_info->wc_sec != wallclock) {
		wallclock = HYPERVISOR_shared_info->wc_sec;
		wallclocksystime = x;
	}
	if (hz)
		*hz = 1000000000;
	return x;
}

ulong
xenwallclock()
{
	ulong elapsed;

	elapsed = (ulong)((xentimerread(0) - wallclocksystime)/1000000000);
	return wallclock + elapsed;
}

void
microdelay(int microsecs)
{
	uvlong targ, hz;

	targ = xentimerread(&hz);
	targ += microsecs * hz / 1000000;
	while(xentimerread(0) < targ)
		continue;
}

void
delay(int millisecs)
{
	microdelay(millisecs * 1000);
}

/*  
 *  performance measurement ticks.  must be low overhead.
 *  doesn't have to count over a second.
 */
ulong
perfticks(void)
{
	uvlong x;

	cycles(&x);
	return x;
}