summaryrefslogtreecommitdiff
path: root/xen/time.c
blob: 5cddd8fa97cd6ba503eb0e9f23a97a00a5adbe44 (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
137
138
139
140
141
142
143
144
145
146
147
148
/*
 *  Copyright (C) 2006-2009 Free Software Foundation
 *
 * This program is free software ; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation ; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY ; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the program ; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <sys/types.h>
#include <mach/mach_types.h>
#include <kern/mach_clock.h>
#include <mach/xen.h>
#include <machine/xen.h>
#include <machine/spl.h>
#include <machine/ipl.h>
#include <mach/machine/eflags.h>
#include <xen/evt.h>
#include "time.h"
#include "store.h"

static unsigned64_t lastnsec;

/* 2^64 nanoseconds ~= 500 years */
static unsigned64_t hyp_get_stime(void) {
	unsigned32_t version;
	unsigned64_t cpu_clock, last_cpu_clock, delta, system_time;
	unsigned32_t mul;
	signed8_t shift;
	volatile struct vcpu_time_info *time = &hyp_shared_info.vcpu_info[0].time;

	do {
		version		= time->version;
		rmb();
		cpu_clock	= hyp_cpu_clock();
		last_cpu_clock	= time->tsc_timestamp;
		system_time	= time->system_time;
		mul		= time->tsc_to_system_mul;
		shift		= time->tsc_shift;
		rmb();
	} while (version != time->version);

	delta = cpu_clock - last_cpu_clock;
	if (shift < 0)
		delta >>= -shift;
	else
		delta <<= shift;
	return system_time + ((delta * (unsigned64_t) mul) >> 32);
}

unsigned64_t hyp_get_time(void) {
	unsigned32_t version;
	unsigned32_t sec, nsec;

	do {
		version = hyp_shared_info.wc_version;
		rmb();
		sec = hyp_shared_info.wc_sec;
		nsec = hyp_shared_info.wc_nsec;
		rmb();
	} while (version != hyp_shared_info.wc_version);

	return sec*1000000000ULL + nsec + hyp_get_stime();
}

static void hypclock_intr(int unit, int old_ipl, void *ret_addr, struct i386_interrupt_state *regs) {
	unsigned64_t nsec, delta;

	if (!lastnsec)
		return;

	nsec = hyp_get_stime();
	if (nsec < lastnsec) {
		printf("warning: nsec 0x%08lx%08lx < lastnsec 0x%08lx%08lx\n",(unsigned long)(nsec>>32), (unsigned long)nsec, (unsigned long)(lastnsec>>32), (unsigned long)lastnsec);
		nsec = lastnsec;
	}
	delta = nsec-lastnsec;

	lastnsec += (delta/1000)*1000;
	hypclock_machine_intr(old_ipl, ret_addr, regs, delta);
	/* 10ms tick rest */
	hyp_do_set_timer_op(hyp_get_stime()+10*1000*1000);

#if 0
	char *c = hyp_store_read(0, 1, "control/shutdown");
	if (c) {
		static int go_down = 0;
		if (!go_down) {
			printf("uh oh, shutdown: %s\n", c);
			go_down = 1;
			/* TODO: somehow send startup_reboot notification to init */
			if (!strcmp(c, "reboot")) {
				/* this is just a reboot */
			}
		}
	}
#endif
}

extern struct timeval time;
extern struct timezone tz;

int
readtodc(tp)
	u_int	*tp;
{
	unsigned64_t t = hyp_get_time();
	u_int n = t / 1000000000;

#ifndef	MACH_KERNEL
	n += tz.tz_minuteswest * 60;
	if (tz.tz_dsttime)
		n -= 3600;
#endif	/* MACH_KERNEL */
	*tp = n;

	return(0);
}

int
writetodc()
{
	/* Not allowed in Xen */
	return(-1);
}

void
clkstart()
{
	evtchn_port_t port = hyp_event_channel_bind_virq(VIRQ_TIMER, 0);
	hyp_evt_handler(port, hypclock_intr, 0, SPLHI);

	/* first clock tick */
	clock_interrupt(0, 0, 0);
	lastnsec = hyp_get_stime();

	/* 10ms tick rest */
	hyp_do_set_timer_op(hyp_get_stime()+10*1000*1000);
}