-
Notifications
You must be signed in to change notification settings - Fork 92
/
clock.c
1464 lines (1164 loc) · 46.9 KB
/
clock.c
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* CLOCK.C (C) Copyright Jan Jaeger, 2000-2012 */
/* (C) and others 2013-2021 */
/* TOD Clock functions */
/* */
/* Released under "The Q Public License Version 1" */
/* (http://www.hercules-390.org/herclic.html) as modifications to */
/* Hercules. */
/* The emulated hardware clock is based on the host clock, adjusted */
/* by means of an offset and a steering rate. */
/* --------------------------------------------------------------------
z/Architecture Clock Formats
--------------------------------------------------------------------
TOD (SCK, SCKC, STCK, STCKC)
+--------------------------+------+
| | |
+--------------------------+------+
0 51 63
ETOD (STCKE)
+--+---------------------------+--+------------------------+-----+
|EI| | | | PGF |
+--+---------------------------+--+------------------------+-----+
0 7 59 63 111 127
Where:
- TOD: bit 51 represents one microsecond
- TOD: bit 63 represents 244 picoseconds
- ETOD: EI = 8-bit Epoch Index field
- ETOD: PF = 16-bit Programmable Field (STKPF)
- ETOD: bit 59 represents one microsecond
- ETOD: bit 63 represents 62.5 nanoseconds
--------------------------------------------------------------------
Hercules Clock and Timer Formats
--------------------------------------------------------------------
64-bit Clock/Timer Format
+------------------------------+--+
| | |
+------------------------------+--+
0 59 63
128-bit Clock Format
+------------------------------+--+------------------------------+
| | | |
+------------------------------+--+------------------------------+
0 59 63 127
Where:
- Bit 59 represents one microsecond
- Bit 63 represents 62.5 nanoseconds
--------------------------------------------------------------------
Usage Notes
--------------------------------------------------------------------
- Bits 0-63 of the Hercules 64-bit clock format are identical to
bits 0-63 of the 128-bit Hercules clock format.
- The 128-bit Hercules clock format extends the 64-bit clock format
by an additional 64-bits to the right (low-order) of the 64-bit
Hercules clock format.
- Hercules timers only use the 64-bit Hercules clock/timer format.
- The Hercules clock format has a period of over 36,533 years.
- With masking, the Hercules 128-bit clock format may be used for
extended TOD clock operations.
- The ETOD2TOD() call may be used to convert a Hercules 128-bit
clock value to a standard z/Architecture 64-bit TOD clock value.
*/
#include "hstdinc.h"
DISABLE_GCC_UNUSED_FUNCTION_WARNING;
#define _CLOCK_C_
#define _HENGINE_DLL_
#include "hercules.h"
#include "opcode.h"
#include "inline.h"
#include "sr.h"
#ifndef COMPILE_THIS_ONLY_ONCE
#define COMPILE_THIS_ONLY_ONCE
/*-------------------------------------------------------------------*/
/* Global clock variables */
/*-------------------------------------------------------------------*/
ETOD universal_tod;
ETOD hw_tod; /* Hardware clock */
S64 tod_epoch; /* Bits 0-7 TOD clock epoch */
/* Bits 8-63 offset bits 0-55 */
ETOD tod_value; /* Bits 0-7 TOD clock epoch */
/* Bits 8-63 TOD bits 0-55 */
/* Bits 64-111 TOD bits 56-103 */
CSR episode_old;
CSR episode_new;
CSR* episode_current = &episode_new;
/* The hercules hardware clock, based on the universal clock, but */
/* running at its own speed as optionally set by set_tod_steering() */
/* The hardware clock returns a unique value */
double hw_steering = 0.0; /* Current TOD clock steering rate */
TOD hw_episode; /* TOD of start of steering episode */
S64 hw_offset = 0; /* Current offset between TOD - HW */
ETOD hw_unique_clock_tick = {0, 0};
int default_epoch = 1900;
int default_yroffset = 0;
int default_tzoffset = 0;
/*-------------------------------------------------------------------*/
/* Function forward references */
/*-------------------------------------------------------------------*/
static TOD universal_clock();
static TOD hw_calculate_unique_tick();
TOD hw_clock();
static TOD hw_adjust( TOD base_tod );
static TOD hw_clock_locked();
void set_tod_clock( const U64 tod );
TOD get_tod_clock( REGS* regs );
TOD update_tod_clock();
DLL_EXPORT TOD etod_clock( REGS*, ETOD*, ETOD_format );
ETOD* host_ETOD( ETOD* );
/*-------------------------------------------------------------------*/
void csr_reset();
double get_tod_steering();
void set_tod_steering( const double steering );
static void set_gross_steering_rate( const S32 gsr );
static void set_fine_steering_rate ( const S32 fsr );
static INLINE void prepare_new_episode();
static INLINE void start_new_episode();
static void set_tod_offset ( const S64 offset );
static void adjust_tod_offset( const S64 offset );
S64 get_tod_epoch();
void set_tod_epoch ( const S64 );
void adjust_tod_epoch ( const S64 );
static U64 set_tod_epoch_all( const U64 epoch );
/*-------------------------------------------------------------------*/
S64 get_cpu_timer( REGS* regs );
void set_cpu_timer( REGS* regs, const S64 timer );
void update_cpu_timer();
U64 thread_cputime_us( const REGS* regs );
/*-------------------------------------------------------------------*/
static INLINE void configure_time();
int configure_epoch( int epoch );
int configure_yroffset( int yroffset );
int configure_tzoffset( int tzoffset );
int query_tzoffset();
static INLINE bool is_leapyear( const unsigned int year );
static INLINE S64 lyear_adjust( const int epoch );
/*-------------------------------------------------------------------*/
#if defined( _FEATURE_INTERVAL_TIMER )
#if defined( _FEATURE_ECPSVM )
static INLINE S32 get_ecps_vtimer( const REGS* regs );
static INLINE void set_ecps_vtimer( REGS* regs, const S32 vtimer );
#endif
static INLINE S32 get_int_timer( const REGS* regs );
void set_int_timer( REGS* regs, const S32 itimer );
int chk_int_timer( REGS* regs );
#endif // defined( _FEATURE_INTERVAL_TIMER )
#endif // COMPILE_THIS_ONLY_ONCE
//-------------------------------------------------------------------
// ARCH_DEP() code
//-------------------------------------------------------------------
// ARCH_DEP (build-architecture / FEATURE-dependent) functions here.
// All BUILD architecture dependent (ARCH_DEP) function are compiled
// multiple times (once for each defined build architecture) and each
// time they are compiled with a different set of FEATURE_XXX defines
// appropriate for that architecture. Use #ifdef FEATURE_XXX guards
// to check whether the current BUILD architecture has that given
// feature #defined for it or not. WARNING: Do NOT use _FEATURE_XXX.
// The underscore feature #defines mean something else entirely. Only
// test for FEATURE_XXX. (WITHOUT the underscore)
//-------------------------------------------------------------------
#if defined( FEATURE_028_TOD_CLOCK_STEER_FACILITY )
void ARCH_DEP(set_gross_s_rate) (REGS *regs)
{
S32 gsr;
gsr = ARCH_DEP(vfetch4) (regs->GR(1) & ADDRESS_MAXWRAP(regs), 1, regs);
set_gross_steering_rate(gsr);
}
/*-------------------------------------------------------------------*/
void ARCH_DEP(set_fine_s_rate) (REGS *regs)
{
S32 fsr;
fsr = ARCH_DEP(vfetch4) (regs->GR(1) & ADDRESS_MAXWRAP(regs), 1, regs);
set_fine_steering_rate(fsr);
}
/*-------------------------------------------------------------------*/
void ARCH_DEP(set_tod_offset) (REGS *regs)
{
S64 offset;
offset = ARCH_DEP(vfetch8) (regs->GR(1) & ADDRESS_MAXWRAP(regs), 1, regs);
set_tod_offset(offset >> 8);
}
/*-------------------------------------------------------------------*/
void ARCH_DEP( set_tod_offset_user )( REGS* regs )
{
S64 offset;
/* "This function specifies a value that is to replace the
*USER*-specified portion of the TOD epoch difference;"
Since, at the basic-machine or LPAR hypervisor level, the
user-specified epoch difference is always ZERO (see the
'query_tod_offset_user' function), we should ideally never
see this function ever being called since z/VM should be
handling it itself (i.e. simulating it for the SIE guest).
Therefore we ignore this call and do absolutely nothing.
*/
/* Fetch the value anyway to check for any addressing exception */
offset = ARCH_DEP( vfetch8 )( regs->GR(1) & ADDRESS_MAXWRAP( regs ), 1, regs );
/* Inform the user that an unexpected situation has occurred */
if (MLVL( VERBOSE ))
{
char buf[32];
MSGBUF( buf, "PTFF-STOU 0x%16.16"PRIX64"!", offset );
WRMSG( HHC90000, "D", buf );
}
}
/*-------------------------------------------------------------------*/
#if defined( FEATURE_139_MULTIPLE_EPOCH_FACILITY )
void ARCH_DEP( set_tod_offset_extended )( REGS* regs )
{
// TODO...
}
#endif /* defined( FEATURE_139_MULTIPLE_EPOCH_FACILITY ) */
/*-------------------------------------------------------------------*/
#if defined( FEATURE_139_MULTIPLE_EPOCH_FACILITY )
void ARCH_DEP( set_tod_offset_user_extended )( REGS* regs )
{
// TODO...
}
#endif /* defined( FEATURE_139_MULTIPLE_EPOCH_FACILITY ) */
/*-------------------------------------------------------------------*/
void ARCH_DEP(adjust_tod_offset) (REGS *regs)
{
S64 offset;
offset = ARCH_DEP(vfetch8) (regs->GR(1) & ADDRESS_MAXWRAP(regs), 1, regs);
adjust_tod_offset(offset >> 8);
}
/*-------------------------------------------------------------------*/
void ARCH_DEP(query_physical_clock) (REGS *regs)
{
ARCH_DEP(vstore8) (universal_clock() << 8, regs->GR(1) & ADDRESS_MAXWRAP(regs), 1, regs);
}
/*-------------------------------------------------------------------*/
void ARCH_DEP( query_utc_information )( REGS* regs )
{
/* "The information in the UIB is provided by the server-time-
protocol (STP) facility that also controls TOD-clock steering.
When STP is not installed, all fields in the UIB are zero."
*/
static const BYTE uib[256] = {0};
ARCH_DEP( vstorec )( &uib, sizeof( uib )-1, regs->GR(1) & ADDRESS_MAXWRAP( regs ), 1, regs );
}
/*-------------------------------------------------------------------*/
void ARCH_DEP(query_steering_information) (REGS *regs)
{
PTFFQSI qsi;
obtain_lock( &sysblk.todlock );
{
STORE_DW( qsi.physclk, universal_clock() << 8);
STORE_DW( qsi.oldestart, episode_old.start_time << 8);
STORE_DW( qsi.oldebase, episode_old.base_offset << 8);
STORE_FW( qsi.oldfsr, episode_old.fine_s_rate );
STORE_FW( qsi.oldgsr, episode_old.gross_s_rate );
STORE_DW( qsi.newestart, episode_new.start_time << 8);
STORE_DW( qsi.newebase, episode_new.base_offset << 8);
STORE_FW( qsi.newfsr, episode_new.fine_s_rate );
STORE_FW( qsi.newgsr, episode_new.gross_s_rate );
}
release_lock( &sysblk.todlock );
ARCH_DEP(vstorec) (&qsi, sizeof(qsi)-1, regs->GR(1) & ADDRESS_MAXWRAP(regs), 1, regs);
}
/*-------------------------------------------------------------------*/
#if defined( FEATURE_139_MULTIPLE_EPOCH_FACILITY )
void ARCH_DEP( query_steering_information_extended )( REGS* regs )
{
// TODO...
}
#endif /* defined( FEATURE_139_MULTIPLE_EPOCH_FACILITY ) */
/*-------------------------------------------------------------------*/
/* Helper function to fill in PTFFQTO struct fields */
/*-------------------------------------------------------------------*/
static void build_qto_locked( PTFFQTO* qto, REGS* regs )
{
STORE_DW( qto->todoff, (hw_clock_locked() - universal_tod.high) << 8);
STORE_DW( qto->physclk, (universal_tod.high << 8) | (universal_tod.low >> (64-8)));
STORE_DW( qto->ltodoff, episode_current->base_offset << 8);
STORE_DW( qto->todepoch, regs->tod_epoch << 8);
}
/*-------------------------------------------------------------------*/
void ARCH_DEP(query_tod_offset) (REGS *regs)
{
PTFFQTO qto;
obtain_lock( &sysblk.todlock );
{
build_qto_locked( &qto, regs );
}
release_lock( &sysblk.todlock );
ARCH_DEP(vstorec) (&qto, sizeof(qto)-1, regs->GR(1) & ADDRESS_MAXWRAP(regs), 1, regs);
}
/*-------------------------------------------------------------------*/
void ARCH_DEP( query_tod_offset_user )( REGS* regs )
{
/* "The 64-bit TOD user specified epoch difference value returned
is the user-specified portion of the *GUEST* epoch difference
for the current level of CPU execution. When executed at the
basic-machine or LPAR hypervisor level, this value is zero."
*/
struct
{
PTFFQTO qto;
DBLWRD tod_user_specified_epoch_difference;
}
qtou;
obtain_lock( &sysblk.todlock );
{
build_qto_locked( &qtou.qto, regs );
STORE_DW( qtou.tod_user_specified_epoch_difference, 0 );
}
release_lock( &sysblk.todlock );
ARCH_DEP( vstorec )( &qtou, sizeof( qtou )-1, regs->GR(1) & ADDRESS_MAXWRAP( regs ), 1, regs );
}
/*-------------------------------------------------------------------*/
#if defined( FEATURE_139_MULTIPLE_EPOCH_FACILITY )
void ARCH_DEP( query_tod_offset_user_extended )( REGS* regs )
{
// TODO...
}
#endif /* defined( FEATURE_139_MULTIPLE_EPOCH_FACILITY ) */
/*-------------------------------------------------------------------*/
void ARCH_DEP(query_available_functions) (REGS *regs)
{
BYTE qaf[16] = {0};
BIT_ARRAY_SET( qaf, PTFF_GPR0_FC_QAF );
BIT_ARRAY_SET( qaf, PTFF_GPR0_FC_QTO );
BIT_ARRAY_SET( qaf, PTFF_GPR0_FC_QSI );
BIT_ARRAY_SET( qaf, PTFF_GPR0_FC_QPT );
BIT_ARRAY_SET( qaf, PTFF_GPR0_FC_QUI );
BIT_ARRAY_SET( qaf, PTFF_GPR0_FC_QTOU );
#if defined( FEATURE_139_MULTIPLE_EPOCH_FACILITY )
BIT_ARRAY_SET( qaf, PTFF_GPR0_FC_QSIE );
BIT_ARRAY_SET( qaf, PTFF_GPR0_FC_QTOUE );
#endif
BIT_ARRAY_SET( qaf, PTFF_GPR0_FC_ATO );
BIT_ARRAY_SET( qaf, PTFF_GPR0_FC_STO );
BIT_ARRAY_SET( qaf, PTFF_GPR0_FC_SFS );
BIT_ARRAY_SET( qaf, PTFF_GPR0_FC_SGS );
BIT_ARRAY_SET( qaf, PTFF_GPR0_FC_STOU );
#if defined( FEATURE_139_MULTIPLE_EPOCH_FACILITY )
BIT_ARRAY_SET( qaf, PTFF_GPR0_FC_STOE );
BIT_ARRAY_SET( qaf, PTFF_GPR0_FC_STOUE );
#endif
ARCH_DEP(vstorec) (&qaf, sizeof(qaf)-1, regs->GR(1) & ADDRESS_MAXWRAP(regs), 1, regs);
}
#endif /* defined( FEATURE_028_TOD_CLOCK_STEER_FACILITY ) */
/*-------------------------------------------------------------------*/
#if defined( FEATURE_INTERVAL_TIMER )
void ARCH_DEP( store_int_timer_locked )( REGS* regs )
{
S32 itimer;
S32 vtimer=0;
itimer = get_int_timer( regs );
STORE_FW( regs->psa->inttimer, itimer );
#if defined( FEATURE_ECPSVM )
if (regs->ecps_vtmrpt)
{
vtimer = get_ecps_vtimer( regs );
STORE_FW( regs->ecps_vtmrpt, vtimer );
}
#endif
chk_int_timer( regs );
#if defined( FEATURE_ECPSVM )
if (regs->ecps_vtmrpt)
regs->ecps_oldtmr = vtimer;
#endif
}
/*-------------------------------------------------------------------*/
DLL_EXPORT
void ARCH_DEP( store_int_timer )( REGS* regs )
{
OBTAIN_INTLOCK( HOSTREGS ? regs : NULL );
{
ARCH_DEP( store_int_timer_locked )( regs );
}
RELEASE_INTLOCK( HOSTREGS ? regs : NULL );
}
/*-------------------------------------------------------------------*/
DLL_EXPORT
void ARCH_DEP( fetch_int_timer )( REGS* regs )
{
S32 itimer;
FETCH_FW( itimer, regs->psa->inttimer );
OBTAIN_INTLOCK( HOSTREGS ? regs : NULL );
{
set_int_timer( regs, itimer );
#if defined( FEATURE_ECPSVM )
if (regs->ecps_vtmrpt)
{
FETCH_FW( itimer, regs->ecps_vtmrpt );
set_ecps_vtimer( regs, itimer );
}
#endif
}
RELEASE_INTLOCK( HOSTREGS ? regs : NULL );
}
#endif /* defined( FEATURE_INTERVAL_TIMER ) */
/*-------------------------------------------------------------------*/
/* (delineates ARCH_DEP from non-arch_dep) */
/*-------------------------------------------------------------------*/
#if !defined( _GEN_ARCH )
#if defined( _ARCH_NUM_1 )
#define _GEN_ARCH _ARCH_NUM_1
#include "clock.c"
#endif
#if defined( _ARCH_NUM_2 )
#undef _GEN_ARCH
#define _GEN_ARCH _ARCH_NUM_2
#include "clock.c"
#endif
/*-------------------------------------------------------------------*/
/* (delineates ARCH_DEP from non-arch_dep) */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* non-ARCH_DEP section: compiled only ONCE after last arch built */
/*-------------------------------------------------------------------*/
/* Note: the last architecture has been built so the normal non- */
/* underscore FEATURE values are now #defined according to the */
/* LAST built architecture just built (usually zarch = 900). This */
/* means from this point onward (to the end of file) you should */
/* ONLY be testing the underscore _FEATURE values to see if the */
/* given feature was defined for *ANY* of the build architectures. */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* host_ETOD - Primary high-resolution clock fetch and conversion */
/*-------------------------------------------------------------------*/
ETOD* host_ETOD( ETOD* ETOD )
{
struct timespec time;
/* Should use CLOCK_MONOTONIC + adjustment, but host sleep/hibernate
* destroys consistent monotonic clock.
*/
clock_gettime( CLOCK_REALTIME, &time );
timespec2ETOD( ETOD, &time );
return ( ETOD ); /* Return address of result */
}
/*-------------------------------------------------------------------*/
void csr_reset()
{
episode_new.start_time = 0;
episode_new.base_offset = 0;
episode_new.fine_s_rate = 0;
episode_new.gross_s_rate = 0;
episode_current = &episode_new;
episode_old = episode_new;
}
/*-------------------------------------------------------------------*/
static
TOD universal_clock() /* really: any clock used as a base */
{
host_ETOD( &universal_tod );
return (universal_tod.high);
}
/*-------------------------------------------------------------------*/
static
TOD hw_calculate_unique_tick()
{
static const ETOD m1 = ETOD_init(0,65536);
ETOD temp;
register TOD result;
register int n;
temp.high = universal_tod.high;
temp.low = universal_tod.low;
hw_unique_clock_tick.low = 1;
for (n = 0; n < 65536; ++n)
result = hw_adjust(universal_clock());
ETOD_sub(&temp, universal_tod, temp);
ETOD_sub(&temp, temp, m1);
ETOD_shift(&hw_unique_clock_tick, temp, 16);
if (hw_unique_clock_tick.low == 0 &&
hw_unique_clock_tick.high == 0)
hw_unique_clock_tick.high = 1;
#if defined(TOD_95BIT_PRECISION) || \
defined(TOD_64BIT_PRECISION) || \
defined(TOD_MIN_PRECISION)
else
{
#if defined(TOD_95BIT_PRECISION)
static const ETOD adj = ETOD_init(0,0x0000000100000000ULL);
#else
static const ETOD adj = ETOD_init(0,0x8000000000000000ULL);
#endif
ETOD_add(&hw_unique_clock_tick, hw_unique_clock_tick, adj);
#if defined(TOD_95BIT_PRECISION)
hw_unique_clock_tick.low &= 0xFFFFFFFE00000000ULL;
#else
hw_unique_clock_tick.low = 0;
#endif
}
#endif /* defined(TOD_95BIT_PRECISION) ... */
return ( result );
}
/*-------------------------------------------------------------------*/
static
TOD hw_adjust( TOD base_tod )
{
/* Apply hardware offset, this is the offset achieved by all
previous steering episodes */
base_tod += hw_offset;
/* Apply the steering offset from the current steering episode */
/* TODO: Shift resolution to permit adjustment by less than 62.5
* nanosecond increments (1/16 microsecond).
*/
base_tod += (S64)(base_tod - hw_episode) * hw_steering;
/* Ensure that the clock returns a unique value */
if (hw_tod.high < base_tod)
hw_tod.high = base_tod,
hw_tod.low = universal_tod.low;
else if (hw_unique_clock_tick.low == 0 &&
hw_unique_clock_tick.high == 0)
hw_calculate_unique_tick();
else
ETOD_add( &hw_tod, hw_tod, hw_unique_clock_tick );
return ( hw_tod.high );
}
/*-------------------------------------------------------------------*/
static
TOD hw_clock_locked()
{
/* Get time of day (GMT); adjust speed and ensure uniqueness */
return hw_adjust( universal_clock() );
}
/*-------------------------------------------------------------------*/
TOD hw_clock()
{
register TOD temp_tod;
obtain_lock( &sysblk.todlock );
{
/* Get time of day (GMT); adjust speed and ensure uniqueness */
temp_tod = hw_clock_locked();
}
release_lock( &sysblk.todlock );
return temp_tod;
}
/*-------------------------------------------------------------------*/
/* set_tod_steering */
/*-------------------------------------------------------------------*/
/* Sets a new steering rate. When a new steering episode begins, */
/* the offset is adjusted, and the new steering rate takes effect */
/*-------------------------------------------------------------------*/
void set_tod_steering( const double steering )
{
obtain_lock( &sysblk.todlock );
{
/* Get current offset between hw_adjust and universal TOD value */
hw_offset = hw_clock_locked() - universal_tod.high;
hw_episode = hw_tod.high;
hw_steering = steering;
}
release_lock( &sysblk.todlock );
}
/*-------------------------------------------------------------------*/
/* start_new_episode */
/*-------------------------------------------------------------------*/
static INLINE
void start_new_episode()
{
hw_offset = hw_tod.high - universal_tod.high;
hw_episode = hw_tod.high;
episode_new.start_time = hw_episode;
/* TODO: Convert to binary arithmetic to avoid floating point conversions */
hw_steering = ldexp(2,-44) *
(S32)(episode_new.fine_s_rate + episode_new.gross_s_rate);
episode_current = &episode_new;
}
/*-------------------------------------------------------------------*/
/* prepare_new_episode */
/*-------------------------------------------------------------------*/
static INLINE
void prepare_new_episode()
{
if (episode_current == &episode_new)
{
episode_old = episode_new;
episode_current = &episode_old;
}
}
/*-------------------------------------------------------------------*/
/* Adjust the epoch for all active cpu's in the configuration */
/*-------------------------------------------------------------------*/
static
U64 set_tod_epoch_all( const U64 epoch )
{
int cpu;
/* Update the TOD clock of all CPU's in the configuration
as we simulate 1 shared TOD clock, and do not support
the TOD clock sync check.
*/
for (cpu = 0; cpu < sysblk.maxcpu; cpu++)
{
obtain_lock( &sysblk.cpulock[ cpu ]);
{
if (IS_CPU_ONLINE(cpu))
sysblk.regs[ cpu ]->tod_epoch = epoch;
}
release_lock( &sysblk.cpulock[ cpu ]);
}
return epoch;
}
/*-------------------------------------------------------------------*/
double get_tod_steering()
{
return hw_steering;
}
/*-------------------------------------------------------------------*/
void set_tod_epoch( const S64 epoch )
{
obtain_lock( &sysblk.todlock );
{
csr_reset();
tod_epoch = epoch;
}
release_lock( &sysblk.todlock );
set_tod_epoch_all( tod_epoch );
}
/*-------------------------------------------------------------------*/
void adjust_tod_epoch( const S64 adjustment )
{
obtain_lock( &sysblk.todlock );
{
csr_reset();
tod_epoch += adjustment;
}
release_lock( &sysblk.todlock );
set_tod_epoch_all( tod_epoch );
}
/*-------------------------------------------------------------------*/
void set_tod_clock(const U64 tod)
{
set_tod_epoch(tod - hw_clock());
}
/*-------------------------------------------------------------------*/
S64 get_tod_epoch()
{
return tod_epoch;
}
/*-------------------------------------------------------------------*/
static
void set_gross_steering_rate( const S32 gsr )
{
obtain_lock( &sysblk.todlock );
{
prepare_new_episode();
episode_new.gross_s_rate = gsr;
}
release_lock( &sysblk.todlock );
}
/*-------------------------------------------------------------------*/
static
void set_fine_steering_rate( const S32 fsr )
{
obtain_lock( &sysblk.todlock );
{
prepare_new_episode();
episode_new.fine_s_rate = fsr;
}
release_lock( &sysblk.todlock );
}
/*-------------------------------------------------------------------*/
static
void set_tod_offset( const S64 offset )
{
obtain_lock( &sysblk.todlock );
{
prepare_new_episode();
episode_new.base_offset = offset;
}
release_lock( &sysblk.todlock );
}
/*-------------------------------------------------------------------*/
static
void adjust_tod_offset( const S64 offset )
{
obtain_lock( &sysblk.todlock );
{
prepare_new_episode();
episode_new.base_offset = episode_old.base_offset + offset;
}
release_lock( &sysblk.todlock );
}
/*-------------------------------------------------------------------*/
/* thread_cputime_us */
/*-------------------------------------------------------------------*/
/* The CPU timer is internally kept as an offset to the hw_clock(). */
/* The cpu timer counts down as the clock approaches the timer epoch.*/
/* */
/* To be in agreement with reporting of time in association with */
/* real diagnose code x'204' for partition management and resource */
/* reporting, only "user" time is considered as CPU time used. */
/* */
/* System time is considered to be overhead time of the system */
/* (partition overhead or management time). */
/*-------------------------------------------------------------------*/
U64 thread_cputime_us( const REGS* regs )
{
U64 result;
int rc = -1;
struct timespec cputime;
if (sysblk.cpuclockid[ regs->cpuad ])
{
rc = clock_gettime( sysblk.cpuclockid[ regs->cpuad ], &cputime );
}
result = (likely(rc == 0)) ? timespec2usecs( &cputime )
: ETOD_high64_to_usecs( host_tod() );
return (result);
}
/*-------------------------------------------------------------------*/
void set_cpu_timer( REGS* regs, const S64 timer )
{
U64 new_epoch_us = (sysblk.lparmode && !WAITSTATE( ®s->psw )) ?
thread_cputime_us( regs ) : (U64) ETOD_high64_to_usecs( host_tod() );
if (regs->bcputime <= new_epoch_us)
{
/* Update real CPU time used and base CPU time epoch */
regs->rcputime += new_epoch_us - regs->bcputime;
regs->bcputime = new_epoch_us;
}
regs->cpu_timer = TOD_high64_to_ETOD_high56( timer ) + hw_clock();
}
/*-------------------------------------------------------------------*/
S64 get_cpu_timer( REGS* regs )
{
S64 timer;
timer = (S64)ETOD_high64_to_TOD_high56( regs->cpu_timer - hw_clock() );
return timer;
}
/*-------------------------------------------------------------------*/
DLL_EXPORT
TOD etod_clock( REGS* regs, ETOD* ETOD, ETOD_format format )
{
/* STORE CLOCK and STORE CLOCK EXTENDED values must be in ascending
* order for comparison. Consequently, swap delays for a subsequent
* STORE CLOCK, STORE CLOCK EXTENDED, or TRACE instruction may be
* introduced when a STORE CLOCK value is advanced due to the use of
* the CPU address in bits 66-71.
*
* If the regs pointer is null, then the request is a raw request,
* and the format operand should specify ETOD_raw or ETOD_fast. For
* raw and fast requests, the CPU address is not inserted into the
* returned value.
*
* A spin loop is used for the introduction of the delay, moderated
* by obtaining and releasing of the TOD lock. This permits raw and
* fast clock requests to complete without additional delay.
*/
U64 high;
U64 low;
U8 swapped = 0;
do
{
obtain_lock(&sysblk.todlock);
high = hw_clock_locked();
low = hw_tod.low;
/* If we are in the old episode, and the new episode has arrived
* then we must take action to start the new episode.
*/
if (episode_current == &episode_old)
start_new_episode();
/* Set the clock to the new updated value with offset applied */
high += episode_current->base_offset;
/* Place CPU stamp into clock value for Standard and Extended
* formats (raw or fast requests fall through)
*/
if (regs && format >= ETOD_standard)
{
register U64 cpuad;
register U64 amask;
register U64 lmask;
/* Set CPU address masks */
if (sysblk.maxcpu <= 64)
amask = 0x3F, lmask = 0xFFFFFFFFFFC00000ULL;
else if (sysblk.maxcpu <= 128)
amask = 0x7F, lmask = 0xFFFFFFFFFF800000ULL;
else /* sysblk.maxcpu <= 256) */
amask = 0xFF, lmask = 0xFFFFFFFFFF000000ULL;
/* Clean CPU address */
cpuad = (U64)regs->cpuad & amask;