xref: /titanic_41/usr/src/uts/i86xpv/os/xpv_timestamp.c (revision 5bbb4db2c3f208d12bf0fd11769728f9e5ba66a2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/clock.h>
29 #include <sys/panic.h>
30 #include <sys/atomic.h>
31 #include <sys/hypervisor.h>
32 
33 #include <sys/archsystm.h>
34 
35 /*
36  * On the hypervisor, we have a virtualized system time based upon the
37  * information provided for each VCPU, which is updated every time it is
38  * scheduled onto a real CPU.  Thus, none of the traditional code in
39  * i86pc/os/timestamp.c applies, our gethrtime() implementation is run through
40  * the PSM, and there is no scaling step to apply.
41  *
42  * However, the platform does not guarantee monotonicity; thus we have to fake
43  * this up, which is a deeply unpleasant thing to have to do.
44  *
45  * Note that the virtualized interface still relies on the current TSC to
46  * calculate the time in nanoseconds since the VCPU was scheduled, and is thus
47  * subject to all the problems with that.  For the most part, the hypervisor is
48  * supposed to deal with them.
49  *
50  * Another wrinkle involves suspend/resume/migration.  If we come back and time
51  * is apparently less, we may have resumed on a different machine or on the
52  * same machine after a reboot.  In this case we need to maintain an addend to
53  * ensure time continues reasonably.  Otherwise we could end up taking a very
54  * long time to expire cyclics in the heap.  Thus we have two functions:
55  *
56  * xpv_getsystime()
57  *
58  *	The unadulterated system time from the hypervisor.  This is only to be
59  *	used when programming the hypervisor (setting a timer or calculating
60  *	the TOD).
61  *
62  * xpv_gethrtime()
63  *
64  *	This is the monotonic hrtime counter to be used by everything else such
65  *	as the cyclic subsystem.  We should never pass an hrtime directly into
66  *	a hypervisor interface, as hrtime_addend may well be non-zero.
67  */
68 
69 int hrtime_fake_mt = 1;
70 static volatile hrtime_t hrtime_last;
71 static hrtime_t hrtime_suspend_time;
72 static hrtime_t hrtime_addend;
73 
74 /*
75  * These functions are used in DTrace probe context, and must be removed from
76  * fbt consideration.  Currently fbt ignores all weak symbols, so this will
77  * achieve that.
78  */
79 #pragma weak xpv_gethrtime = dtrace_xpv_gethrtime
80 #pragma weak xpv_getsystime = dtrace_xpv_getsystime
81 #pragma weak dtrace_gethrtime = dtrace_xpv_gethrtime
82 #pragma weak tsc_read = dtrace_xpv_gethrtime
83 
84 hrtime_t
85 dtrace_xpv_getsystime(void)
86 {
87 	vcpu_time_info_t *src;
88 	vcpu_time_info_t __vti, *dst = &__vti;
89 	uint64_t tsc_delta;
90 	kthread_t *t = curthread;
91 	uint64_t tsc;
92 	hrtime_t result;
93 
94 	/*
95 	 * This stops us from wandering off the virtual cpu.
96 	 */
97 	t->t_preempt++;
98 
99 	src = &CPU->cpu_m.mcpu_vcpu_info->time;
100 
101 	/*
102 	 * Loop until version has not been changed during our update, and a Xen
103 	 * update is not under way (lowest bit is set).
104 	 */
105 	do {
106 		dst->version = src->version;
107 
108 		membar_consumer();
109 
110 		dst->tsc_timestamp = src->tsc_timestamp;
111 		dst->system_time = src->system_time;
112 		dst->tsc_to_system_mul = src->tsc_to_system_mul;
113 		dst->tsc_shift = src->tsc_shift;
114 
115 		/*
116 		 * Note that this use of the -actual- TSC register
117 		 * should probably be the SOLE one in the system on this
118 		 * paravirtualized platform.
119 		 */
120 		tsc = __rdtsc_insn();
121 		tsc_delta = tsc - dst->tsc_timestamp;
122 
123 		membar_consumer();
124 
125 	} while ((src->version & 1) | (dst->version ^ src->version));
126 
127 	if (dst->tsc_shift >= 0)
128 		tsc_delta <<= dst->tsc_shift;
129 	else if (dst->tsc_shift < 0)
130 		tsc_delta >>= -dst->tsc_shift;
131 
132 	result = dst->system_time +
133 	    ((uint64_t)(tsc_delta * (uint64_t)dst->tsc_to_system_mul) >> 32);
134 
135 	t->t_preempt--;
136 
137 	return (result);
138 }
139 
140 hrtime_t
141 dtrace_xpv_gethrtime(void)
142 {
143 	hrtime_t result = xpv_getsystime() + hrtime_addend;
144 
145 	if (hrtime_fake_mt) {
146 		hrtime_t last;
147 		do {
148 			last = hrtime_last;
149 			if (result < last)
150 				result = last + 1;
151 		} while (atomic_cas_64((volatile uint64_t *)&hrtime_last,
152 		    last, result) != last);
153 	}
154 
155 	return (result);
156 }
157 
158 void
159 xpv_time_suspend(void)
160 {
161 	hrtime_suspend_time = xpv_getsystime();
162 }
163 
164 void
165 xpv_time_resume(void)
166 {
167 	hrtime_t delta = xpv_getsystime() - hrtime_suspend_time;
168 
169 	if (delta < 0)
170 		hrtime_addend += -delta;
171 }
172