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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/disp.h>
#include <sys/var.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/x86_archext.h>
#include <sys/archsystm.h>
#include <sys/cpuvar.h>
#include <sys/psm_defs.h>
#include <sys/clock.h>
#include <sys/atomic.h>
#include <sys/lockstat.h>
#include <sys/smp_impldefs.h>
#include <sys/dtrace.h>
#include <sys/time.h>
/*
* Using the Pentium's TSC register for gethrtime()
* ------------------------------------------------
*
* The Pentium family, like many chip architectures, has a high-resolution
* timestamp counter ("TSC") which increments once per CPU cycle. The contents
* of the timestamp counter are read with the RDTSC instruction.
*
* As with its UltraSPARC equivalent (the %tick register), TSC's cycle count
* must be translated into nanoseconds in order to implement gethrtime().
* We avoid inducing floating point operations in this conversion by
* implementing the same nsec_scale algorithm as that found in the sun4u
* platform code. The sun4u NATIVE_TIME_TO_NSEC_SCALE block comment contains
* a detailed description of the algorithm; the comment is not reproduced
* here. This implementation differs only in its value for NSEC_SHIFT:
* we implement an NSEC_SHIFT of 5 (instead of sun4u's 4) to allow for
* 60 MHz Pentiums.
*
* While TSC and %tick are both cycle counting registers, TSC's functionality
* falls short in several critical ways:
*
* (a) TSCs on different CPUs are not guaranteed to be in sync. While in
* practice they often _are_ in sync, this isn't guaranteed by the
* architecture.
*
* (b) The TSC cannot be reliably set to an arbitrary value. The architecture
* only supports writing the low 32-bits of TSC, making it impractical
* to rewrite.
*
* (c) The architecture doesn't have the capacity to interrupt based on
* arbitrary values of TSC; there is no TICK_CMPR equivalent.
*
* Together, (a) and (b) imply that software must track the skew between
* TSCs and account for it (it is assumed that while there may exist skew,
* there does not exist drift). To determine the skew between CPUs, we
* have newly onlined CPUs call tsc_sync_slave(), while the CPU performing
* the online operation calls tsc_sync_master(). Once both CPUs are ready,
* the master sets a shared flag, and each reads its TSC register. To reduce
* bias, we then wait until both CPUs are ready again, but this time the
* slave sets the shared flag, and each reads its TSC register again. The
* master compares the average of the two sample values, and, if observable
* skew is found, changes the gethrtimef function pointer to point to a
* gethrtime() implementation which will take the discovered skew into
* consideration.
*
* In the absence of time-of-day clock adjustments, gethrtime() must stay in
* sync with gettimeofday(). This is problematic; given (c), the software
* cannot drive its time-of-day source from TSC, and yet they must somehow be
* kept in sync. We implement this by having a routine, tsc_tick(), which
* is called once per second from the interrupt which drives time-of-day.
* tsc_tick() recalculates nsec_scale based on the number of the CPU cycles
* since boot versus the number of seconds since boot. This algorithm
* becomes more accurate over time and converges quickly; the error in
* nsec_scale is typically under 1 ppm less than 10 seconds after boot, and
* is less than 100 ppb 1 minute after boot.
*
* Note that the hrtime base for gethrtime, tsc_hrtime_base, is modified
* atomically with nsec_scale under CLOCK_LOCK. This assures that time
* monotonically increases.
*/
#define NSEC_SHIFT 5
static uint_t nsec_scale;
/*
* These two variables used to be grouped together inside of a structure that
* lived on a single cache line. A regression (bug ID 4623398) caused the
* compiler to emit code that "optimized" away the while-loops below. The
* result was that no synchronization between the onlining and onlined CPUs
* took place.
*/
static volatile int tsc_ready;
static volatile int tsc_sync_go;
/*
* Used as indices into the tsc_sync_snaps[] array.
*/
#define TSC_MASTER 0
#define TSC_SLAVE 1
/*
* Used in the tsc_master_sync()/tsc_slave_sync() rendezvous.
*/
#define TSC_SYNC_STOP 1
#define TSC_SYNC_GO 2
#define TSC_SYNC_AGAIN 3
/*
* XX64 Is the faster way to do this with a 64-bit ABI?
*/
#define TSC_CONVERT_AND_ADD(tsc, hrt, scale) { \
unsigned int *_l = (unsigned int *)&(tsc); \
(hrt) += mul32(_l[1], scale) << NSEC_SHIFT; \
(hrt) += mul32(_l[0], scale) >> (32 - NSEC_SHIFT); \
}
#define TSC_CONVERT(tsc, hrt, scale) { \
unsigned int *_l = (unsigned int *)&(tsc); \
(hrt) = mul32(_l[1], scale) << NSEC_SHIFT; \
(hrt) += mul32(_l[0], scale) >> (32 - NSEC_SHIFT); \
}
static int tsc_max_delta;
static hrtime_t tsc_sync_snaps[2];
static hrtime_t tsc_sync_delta[NCPU];
static hrtime_t tsc_sync_tick_delta[NCPU];
static hrtime_t tsc_last = 0;
static hrtime_t tsc_last_jumped = 0;
static hrtime_t tsc_hrtime_base = 0;
static int tsc_jumped = 0;
static hrtime_t shadow_tsc_hrtime_base;
static hrtime_t shadow_tsc_last;
static uint_t shadow_nsec_scale;
static uint32_t shadow_hres_lock;
/*
* Called by the master after the sync operation is complete. If the
* slave is discovered to lag, gethrtimef will be changed to point to
* tsc_gethrtime_delta().
*/
static void
tsc_digest(processorid_t target)
{
hrtime_t tdelta, hdelta = 0;
int max = tsc_max_delta;
processorid_t source = CPU->cpu_id;
int update;
update = tsc_sync_delta[source] != 0 ||
gethrtimef == tsc_gethrtime_delta;
/*
* We divide by 2 since each of the data points is the sum of two TSC
* reads; this takes the average of the two.
*/
tdelta = (tsc_sync_snaps[TSC_SLAVE] - tsc_sync_snaps[TSC_MASTER]) / 2;
if ((tdelta > max) || ((tdelta >= 0) && update)) {
TSC_CONVERT_AND_ADD(tdelta, hdelta, nsec_scale);
tsc_sync_delta[target] = tsc_sync_delta[source] - hdelta;
tsc_sync_tick_delta[target] = -tdelta;
gethrtimef = tsc_gethrtime_delta;
gethrtimeunscaledf = tsc_gethrtimeunscaled_delta;
return;
}
tdelta = -tdelta;
if ((tdelta > max) || update) {
TSC_CONVERT_AND_ADD(tdelta, hdelta, nsec_scale);
tsc_sync_delta[target] = tsc_sync_delta[source] + hdelta;
tsc_sync_tick_delta[target] = tdelta;
gethrtimef = tsc_gethrtime_delta;
gethrtimeunscaledf = tsc_gethrtimeunscaled_delta;
}
}
/*
* Called by a CPU which has just performed an online operation on another
* CPU. It is expected that the newly onlined CPU will call tsc_sync_slave().
*/
void
tsc_sync_master(processorid_t slave)
{
int flags;
hrtime_t hrt;
ASSERT(tsc_sync_go != TSC_SYNC_GO);
flags = clear_int_flag();
/*
* Wait for the slave CPU to arrive.
*/
while (tsc_ready != TSC_SYNC_GO)
continue;
/*
* Tell the slave CPU to begin reading its TSC; read our own.
*/
tsc_sync_go = TSC_SYNC_GO;
hrt = tsc_read();
/*
* Tell the slave that we're ready, and wait for the slave to tell us
* to read our TSC again.
*/
tsc_ready = TSC_SYNC_AGAIN;
while (tsc_sync_go != TSC_SYNC_AGAIN)
continue;
hrt += tsc_read();
tsc_sync_snaps[TSC_MASTER] = hrt;
/*
* Wait for the slave to finish reading its TSC.
*/
while (tsc_ready != TSC_SYNC_STOP)
continue;
/*
* At this point, both CPUs have performed their tsc_read() calls.
* We'll digest it now before letting the slave CPU return.
*/
tsc_digest(slave);
tsc_sync_go = TSC_SYNC_STOP;
restore_int_flag(flags);
}
/*
* Called by a CPU which has just been onlined. It is expected that the CPU
* performing the online operation will call tsc_sync_master().
*/
void
tsc_sync_slave(void)
{
int flags;
hrtime_t hrt;
ASSERT(tsc_sync_go != TSC_SYNC_GO);
flags = clear_int_flag();
/* to test tsc_gethrtime_delta, add wrmsr(REG_TSC, 0) here */
/*
* Tell the master CPU that we're ready, and wait for the master to
* tell us to begin reading our TSC.
*/
tsc_ready = TSC_SYNC_GO;
while (tsc_sync_go != TSC_SYNC_GO)
continue;
hrt = tsc_read();
/*
* Wait for the master CPU to be ready to read its TSC again.
*/
while (tsc_ready != TSC_SYNC_AGAIN)
continue;
/*
* Tell the master CPU to read its TSC again; read ours again.
*/
tsc_sync_go = TSC_SYNC_AGAIN;
hrt += tsc_read();
tsc_sync_snaps[TSC_SLAVE] = hrt;
/*
* Tell the master that we're done, and wait to be dismissed.
*/
tsc_ready = TSC_SYNC_STOP;
while (tsc_sync_go != TSC_SYNC_STOP)
continue;
restore_int_flag(flags);
}
void
tsc_hrtimeinit(uint64_t cpu_freq_hz)
{
longlong_t tsc;
int flags;
/*
* cpu_freq_hz is the measured cpu frequency in hertz
*/
/*
* We can't accommodate CPUs slower than 31.25 MHz.
*/
ASSERT(cpu_freq_hz > NANOSEC / (1 << NSEC_SHIFT));
nsec_scale =
(uint_t)
(((uint64_t)NANOSEC << (32 - NSEC_SHIFT)) / cpu_freq_hz);
flags = clear_int_flag();
tsc = tsc_read();
(void) tsc_gethrtime();
tsc_max_delta = tsc_read() - tsc;
restore_int_flag(flags);
}
/*
* Called once per second on some CPU from the cyclic subsystem's
* CY_HIGH_LEVEL interrupt. (no longer CPU0-only)
*/
void
tsc_tick(void)
{
hrtime_t now, delta;
ushort_t spl;
/*
* Before we set the new variables, we set the shadow values. This
* allows for lock free operation in dtrace_gethrtime().
*/
lock_set_spl((lock_t *)&shadow_hres_lock + HRES_LOCK_OFFSET,
ipltospl(CBE_HIGH_PIL), &spl);
shadow_tsc_hrtime_base = tsc_hrtime_base;
shadow_tsc_last = tsc_last;
shadow_nsec_scale = nsec_scale;
shadow_hres_lock++;
splx(spl);
CLOCK_LOCK(&spl);
now = tsc_read();
if (gethrtimef == tsc_gethrtime_delta)
now += tsc_sync_tick_delta[CPU->cpu_id];
if (now < tsc_last) {
/*
* The TSC has just jumped into the past. We assume that
* this is due to a suspend/resume cycle, and we're going
* to use the _current_ value of TSC as the delta. This
* will keep tsc_hrtime_base correct. We're also going to
* assume that rate of tsc does not change after a suspend
* resume (i.e nsec_scale remains the same).
*/
delta = now;
tsc_last_jumped += tsc_last;
tsc_jumped = 1;
} else {
/*
* Determine the number of TSC ticks since the last clock
* tick, and add that to the hrtime base.
*/
delta = now - tsc_last;
}
TSC_CONVERT_AND_ADD(delta, tsc_hrtime_base, nsec_scale);
tsc_last = now;
CLOCK_UNLOCK(spl);
}
hrtime_t
tsc_gethrtime(void)
{
uint32_t old_hres_lock;
hrtime_t tsc, hrt;
do {
old_hres_lock = hres_lock;
if ((tsc = tsc_read()) >= tsc_last) {
/*
* It would seem to be obvious that this is true
* (that is, the past is less than the present),
* but it isn't true in the presence of suspend/resume
* cycles. If we manage to call gethrtime()
* after a resume, but before the first call to
* tsc_tick(), we will see the jump. In this case,
* we will simply use the value in TSC as the delta.
*/
tsc -= tsc_last;
} else if (tsc >= tsc_last - 2*tsc_max_delta) {
/*
* There is a chance that tsc_tick() has just run on
* another CPU, and we have drifted just enough so that
* we appear behind tsc_last. In this case, force the
* delta to be zero.
*/
tsc = 0;
}
hrt = tsc_hrtime_base;
TSC_CONVERT_AND_ADD(tsc, hrt, nsec_scale);
} while ((old_hres_lock & ~1) != hres_lock);
return (hrt);
}
/*
* This is similar to the above, but it cannot actually spin on hres_lock.
* As a result, it caches all of the variables it needs; if the variables
* don't change, it's done.
*/
hrtime_t
dtrace_gethrtime(void)
{
uint32_t old_hres_lock;
hrtime_t tsc, hrt;
do {
old_hres_lock = hres_lock;
/*
* See the comments in tsc_gethrtime(), above.
*/
if ((tsc = tsc_read()) >= tsc_last)
tsc -= tsc_last;
else if (tsc >= tsc_last - 2*tsc_max_delta)
tsc = 0;
hrt = tsc_hrtime_base;
TSC_CONVERT_AND_ADD(tsc, hrt, nsec_scale);
if ((old_hres_lock & ~1) == hres_lock)
break;
/*
* If we're here, the clock lock is locked -- or it has been
* unlocked and locked since we looked. This may be due to
* tsc_tick() running on another CPU -- or it may be because
* some code path has ended up in dtrace_probe() with
* CLOCK_LOCK held. We'll try to determine that we're in
* the former case by taking another lap if the lock has
* changed since when we first looked at it.
*/
if (old_hres_lock != hres_lock)
continue;
/*
* So the lock was and is locked. We'll use the old data
* instead.
*/
old_hres_lock = shadow_hres_lock;
/*
* See the comments in tsc_gethrtime(), above.
*/
if ((tsc = tsc_read()) >= shadow_tsc_last)
tsc -= shadow_tsc_last;
else if (tsc >= shadow_tsc_last - 2 * tsc_max_delta)
tsc = 0;
hrt = shadow_tsc_hrtime_base;
TSC_CONVERT_AND_ADD(tsc, hrt, shadow_nsec_scale);
} while ((old_hres_lock & ~1) != shadow_hres_lock);
return (hrt);
}
hrtime_t
tsc_gethrtime_delta(void)
{
uint32_t old_hres_lock;
hrtime_t tsc, hrt;
int flags;
do {
old_hres_lock = hres_lock;
/*
* We need to disable interrupts here to assure that we
* don't migrate between the call to tsc_read() and
* adding the CPU's TSC tick delta. Note that disabling
* and reenabling preemption is forbidden here because
* we may be in the middle of a fast trap. In the amd64
* kernel we cannot tolerate preemption during a fast
* trap. See _update_sregs().
*/
flags = clear_int_flag();
tsc = tsc_read() + tsc_sync_tick_delta[CPU->cpu_id];
restore_int_flag(flags);
/* See comments in tsc_gethrtime() above */
if (tsc >= tsc_last) {
tsc -= tsc_last;
} else if (tsc >= tsc_last - 2 * tsc_max_delta) {
tsc = 0;
}
hrt = tsc_hrtime_base;
TSC_CONVERT_AND_ADD(tsc, hrt, nsec_scale);
} while ((old_hres_lock & ~1) != hres_lock);
return (hrt);
}
extern uint64_t cpu_freq_hz;
extern int tsc_gethrtime_enable;
/*
* The following converts nanoseconds of highres-time to ticks
*/
static uint64_t
hrtime2tick(hrtime_t ts)
{
hrtime_t q = ts / NANOSEC;
hrtime_t r = ts - (q * NANOSEC);
return (q * cpu_freq_hz + ((r * cpu_freq_hz) / NANOSEC));
}
/*
* This is used to convert scaled high-res time from nanoseconds to
* unscaled hardware ticks. (Read from hardware timestamp counter)
*/
uint64_t
unscalehrtime(hrtime_t ts)
{
if (tsc_gethrtime_enable) {
uint64_t unscale = 0;
hrtime_t rescale;
hrtime_t diff = ts;
while (diff > (nsec_per_tick)) {
unscale += hrtime2tick(diff);
rescale = unscale;
scalehrtime(&rescale);
diff = ts - rescale;
}
return (unscale);
}
return (0);
}
hrtime_t
tsc_gethrtimeunscaled(void)
{
uint32_t old_hres_lock;
hrtime_t tsc;
do {
old_hres_lock = hres_lock;
if ((tsc = tsc_read()) < tsc_last) {
/*
* see comments in tsc_gethrtime
*/
tsc += tsc_last_jumped;
}
} while ((old_hres_lock & ~1) != hres_lock);
return (tsc);
}
/* Convert a tsc timestamp to nanoseconds */
void
tsc_scalehrtime(hrtime_t *tsc)
{
hrtime_t hrt;
hrtime_t mytsc;
if (tsc == NULL)
return;
mytsc = *tsc;
TSC_CONVERT(mytsc, hrt, nsec_scale);
*tsc = hrt;
}
hrtime_t
tsc_gethrtimeunscaled_delta(void)
{
hrtime_t hrt;
int flags;
/*
* Similarly to tsc_gethrtime_delta, we need to disable preemption
* to prevent migration between the call to tsc_gethrtimeunscaled
* and adding the CPU's hrtime delta. Note that disabling and
* reenabling preemption is forbidden here because we may be in the
* middle of a fast trap. In the amd64 kernel we cannot tolerate
* preemption during a fast trap. See _update_sregs().
*/
flags = clear_int_flag();
hrt = tsc_gethrtimeunscaled() + tsc_sync_tick_delta[CPU->cpu_id];
restore_int_flag(flags);
return (hrt);
}
|