00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
#include "ki.h"
00026
00027
#ifdef ALLOC_PRAGMA
00028
#pragma alloc_text(PAGE, KeAddSystemServiceTable)
00029
#pragma alloc_text(PAGE, KeSetSwapContextNotifyRoutine)
00030
#pragma alloc_text(PAGE, KeSetTimeUpdateNotifyRoutine)
00031
#pragma alloc_text(PAGE, KeSetThreadSelectNotifyRoutine)
00032
#pragma alloc_text(PAGE, KeQueryActiveProcessors)
00033
#pragma alloc_text(PAGELK, KiCalibrateTimeAdjustment)
00034
#endif
00035
00036
00037
00038
00039
#undef KeEnterCriticalRegion
00040
VOID
00041 KeEnterCriticalRegion (
00042 VOID
00043 )
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 {
00069
00070
00071
00072
00073
KeGetCurrentThread()->KernelApcDisable -= 1;
00074
return;
00075 }
00076
00077
00078
#undef KeLeaveCriticalRegion
00079
VOID
00080 KeLeaveCriticalRegion (
00081 VOID
00082 )
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 {
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
KiLeaveCriticalRegion();
00118
return;
00119 }
00120
00121 ULONGLONG
00122 KeQueryInterruptTime (
00123 VOID
00124 )
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 {
00145
00146 LARGE_INTEGER CurrentTime;
00147
00148 KiQueryInterruptTime(&CurrentTime);
00149
return CurrentTime.QuadPart;
00150 }
00151
00152
VOID
00153 KeQuerySystemTime (
00154 OUT PLARGE_INTEGER CurrentTime
00155 )
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 {
00176
00177 KiQuerySystemTime(CurrentTime);
00178
return;
00179 }
00180
00181
VOID
00182 KeQueryTickCount (
00183 OUT PLARGE_INTEGER CurrentCount
00184 )
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 {
00205
00206 KiQueryTickCount(CurrentCount);
00207
return;
00208 }
00209
00210 ULONG
00211 KeQueryTimeIncrement (
00212 VOID
00213 )
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233 {
00234
00235
return KeMaximumIncrement;
00236 }
00237
00238
VOID
00239 KeSetDmaIoCoherency (
00240 IN ULONG Attributes
00241 )
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261 {
00262
00263
KiDmaIoCoherency = Attributes;
00264 }
00265
00266
#if defined(i386)
00267
VOID
00268 KeSetProfileIrql (
00269 IN KIRQL ProfileIrql
00270 )
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291 {
00292
00293
ASSERT((ProfileIrql == PROFILE_LEVEL) || (ProfileIrql == HIGH_LEVEL));
00294
KiProfileIrql = ProfileIrql;
00295 }
00296
00297
#endif
00298
00299
#if defined(_ALPHA_)
00300
VOID
00301 KeSetSynchIrql (
00302 IN KIRQL SynchIrql
00303 )
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 {
00325
00326
ASSERT((SynchIrql >= DISPATCH_LEVEL) && (SynchIrql <= SYNCH_LEVEL));
00327
00328 KiSynchIrql = SynchIrql;
00329 }
00330
00331
#endif
00332
00333
00334
VOID
00335 KeSetSystemTime (
00336 IN PLARGE_INTEGER NewTime,
00337 OUT PLARGE_INTEGER OldTime,
00338 IN BOOLEAN AdjustInterruptTime,
00339 IN PLARGE_INTEGER HalTimeToSet OPTIONAL
00340 )
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370 {
00371
00372 LIST_ENTRY AbsoluteListHead;
00373 LIST_ENTRY ExpiredListHead;
00374 ULONG
Index;
00375 PLIST_ENTRY ListHead;
00376 PLIST_ENTRY NextEntry;
00377 KIRQL OldIrql1;
00378 KIRQL OldIrql2;
00379 LARGE_INTEGER TimeDelta;
00380 TIME_FIELDS
TimeFields;
00381
PKTIMER Timer;
00382
00383
ASSERT(KeGetCurrentIrql() <
DISPATCH_LEVEL);
00384
00385
00386
00387
00388
00389
00390
if (ARGUMENT_PRESENT(HalTimeToSet)) {
00391
RtlTimeToTimeFields(HalTimeToSet, &
TimeFields);
00392 }
00393
00394
00395
00396
00397
00398
00399
00400
KeSetSystemAffinityThread((KAFFINITY)1);
00401
KiLockDispatcherDatabase(&OldIrql1);
00402
KeRaiseIrql(
HIGH_LEVEL, &OldIrql2);
00403
00404
00405
00406
00407
00408
00409 KiQuerySystemTime(OldTime);
00410
00411
#if defined(_WIN64)
00412
00413 SharedUserData->SystemHigh2Time = NewTime->HighPart;
00414 SharedUserData->SystemLowTime = NewTime->LowPart;
00415 SharedUserData->SystemHigh1Time = NewTime->HighPart;
00416
00417
#elif defined(ALPHA)
00418
00419 SharedUserData->SystemTime = *(PULONGLONG)NewTime;
00420
00421
#else
00422
00423 SharedUserData->SystemTime.High2Time = NewTime->HighPart;
00424 SharedUserData->SystemTime.LowPart = NewTime->LowPart;
00425 SharedUserData->SystemTime.High1Time = NewTime->HighPart;
00426
00427
#endif // defined(ALPHA) || defined(_IA64_)
00428
00429
if (ARGUMENT_PRESENT(HalTimeToSet)) {
00430
HalSetRealTimeClock(&
TimeFields);
00431 }
00432
00433
00434
00435
00436
00437
00438 TimeDelta.QuadPart = NewTime->QuadPart - OldTime->QuadPart;
00439
00440
00441
00442
00443
00444
00445
KeBootTime.QuadPart =
KeBootTime.QuadPart + TimeDelta.QuadPart;
00446
00447
00448
00449
00450
00451
KeBootTimeBias =
KeBootTimeBias + TimeDelta.QuadPart;
00452
00453
00454
00455
00456
00457
00458
KeLowerIrql(OldIrql2);
00459
if (AdjustInterruptTime) {
00460
00461
00462
00463
00464
00465 AdjustInterruptTime =
KiAdjustInterruptTime (TimeDelta.QuadPart);
00466 }
00467
00468
00469
00470
00471
00472
00473
00474
if (!AdjustInterruptTime) {
00475
00476
00477
00478
00479
00480
00481 InitializeListHead(&AbsoluteListHead);
00482
for (
Index = 0;
Index <
TIMER_TABLE_SIZE;
Index += 1) {
00483 ListHead = &
KiTimerTableListHead[
Index];
00484 NextEntry = ListHead->Flink;
00485
while (NextEntry != ListHead) {
00486 Timer = CONTAINING_RECORD(NextEntry,
KTIMER, TimerListEntry);
00487 NextEntry = NextEntry->Flink;
00488
if (Timer->
Header.
Absolute !=
FALSE) {
00489 RemoveEntryList(&Timer->
TimerListEntry);
00490 InsertTailList(&AbsoluteListHead, &Timer->
TimerListEntry);
00491 }
00492 }
00493 }
00494
00495
00496
00497
00498
00499
00500
00501 InitializeListHead(&ExpiredListHead);
00502
while (AbsoluteListHead.Flink != &AbsoluteListHead) {
00503 Timer = CONTAINING_RECORD(AbsoluteListHead.Flink,
KTIMER, TimerListEntry);
00504
KiRemoveTreeTimer(Timer);
00505 Timer->
DueTime.QuadPart -= TimeDelta.QuadPart;
00506
if (
KiReinsertTreeTimer(Timer, Timer->
DueTime) ==
FALSE) {
00507 Timer->
Header.
Inserted =
TRUE;
00508 InsertTailList(&ExpiredListHead, &Timer->
TimerListEntry);
00509 }
00510 }
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
KiTimerListExpire(&ExpiredListHead, OldIrql1);
00521
00522 }
else {
00523
00524
KiUnlockDispatcherDatabase(OldIrql1);
00525
00526 }
00527
00528
00529
00530
00531
00532
00533
KeRevertToUserAffinityThread();
00534
00535
00536
00537
00538
00539
PoNotifySystemTimeSet();
00540
return;
00541 }
00542
00543 BOOLEAN
00544 KiAdjustInterruptTime (
00545 IN LONGLONG TimeDelta
00546 )
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564 {
00565
ADJUST_INTERRUPT_TIME_CONTEXT Adjust;
00566
00567
00568
00569
00570
00571
if (TimeDelta < 0) {
00572
00573
return FALSE;
00574
00575 }
else {
00576
00577 Adjust.
KiNumber =
KeNumberProcessors;
00578 Adjust.
HalNumber =
KeNumberProcessors;
00579 Adjust.
Adjustment = (ULONGLONG) TimeDelta;
00580 Adjust.
Barrier = 1;
00581
00582
KiIpiGenericCall (
00583 (
PKIPI_BROADCAST_WORKER)
KiCalibrateTimeAdjustment,
00584 (ULONG_PTR)(&Adjust)
00585 );
00586
00587
return TRUE;
00588 }
00589 }
00590
00591
VOID
00592 KiCalibrateTimeAdjustment (
00593 PADJUST_INTERRUPT_TIME_CONTEXT Adjust
00594 )
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611 {
00612 BOOLEAN Enable;
00613 LARGE_INTEGER InterruptTime;
00614 LARGE_INTEGER SetTime;
00615 LARGE_INTEGER PerfFreq;
00616 ULARGE_INTEGER li;
00617 LARGE_INTEGER NewTickCount;
00618 ULONG NewTickOffset;
00619 ULONG cl, divisor;
00620
00621
00622
00623
00624
00625
00626
00627
00628
if (InterlockedDecrement((PLONG) &Adjust->KiNumber)) {
00629
00630 Enable =
KiDisableInterrupts();
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
do {
00642
KiPollFreezeExecution();
00643 }
while (Adjust->KiNumber != (ULONG)-1);
00644
00645
00646
00647
00648
00649
while (Adjust->Barrier) ;
00650
00651 }
else {
00652
00653
00654
00655
00656
00657
00658
KeRemoveQueueDpc (&
KiTimerExpireDpc);
00659
KeInsertQueueDpc (&
KiTimerExpireDpc, (PVOID)
TIMER_TABLE_SIZE,
NULL);
00660
00661
00662
00663
00664
00665
00666 Enable =
KiDisableInterrupts();
00667 InterlockedDecrement((PLONG) &Adjust->KiNumber);
00668
00669
00670
00671
00672
00673
KeQueryPerformanceCounter (&PerfFreq);
00674 InterruptTime.QuadPart =
KeQueryInterruptTime() + Adjust->Adjustment;
00675 SetTime.QuadPart = InterruptTime.QuadPart +
KeTimeIncrement / 2;
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687 li.QuadPart = RtlEnlargedUnsignedMultiply (
00688 (ULONG) SetTime.LowPart,
00689 (ULONG) PerfFreq.LowPart
00690 ).QuadPart;
00691
00692 cl = li.LowPart;
00693 li.QuadPart = li.HighPart +
00694 RtlEnlargedUnsignedMultiply (
00695 (ULONG) SetTime.LowPart,
00696 (ULONG) PerfFreq.HighPart
00697 ).QuadPart;
00698
00699 li.QuadPart = li.QuadPart +
00700 RtlEnlargedUnsignedMultiply (
00701 (ULONG) SetTime.HighPart,
00702 (ULONG) PerfFreq.LowPart
00703 ).QuadPart;
00704
00705 li.HighPart = li.HighPart + SetTime.HighPart * PerfFreq.HighPart;
00706
00707 divisor = 10000000;
00708 Adjust->NewCount.HighPart =
00709 RtlEnlargedUnsignedDivide (
00710 li,
00711 divisor,
00712 &li.HighPart
00713 );
00714
00715 li.LowPart = cl;
00716 Adjust->NewCount.LowPart =
00717 RtlEnlargedUnsignedDivide (
00718 li,
00719 divisor,
00720
NULL
00721 );
00722
00723
00724
00725
00726
00727 NewTickCount =
RtlExtendedLargeIntegerDivide(
00728 InterruptTime,
00729
KeMaximumIncrement,
00730 &NewTickOffset
00731 );
00732
00733
00734
00735
00736
00737
00738
KiTickOffset =
KeMaximumIncrement - NewTickOffset;
00739
KeInterruptTimeBias += Adjust->Adjustment;
00740 SharedUserData->TickCountLow = NewTickCount.LowPart;
00741
00742
#if defined(_WIN64)
00743
00744
KeTickCount = NewTickCount.QuadPart;
00745 SharedUserData->InterruptHigh2Time = InterruptTime.HighPart;
00746 SharedUserData->InterruptTime = InterruptTime.QuadPart;
00747
00748
#elif defined(ALPHA)
00749
00750
KeTickCount = NewTickCount.QuadPart;
00751 SharedUserData->InterruptTime = InterruptTime.QuadPart;
00752
00753
#else
00754
KeTickCount.High2Time = NewTickCount.HighPart;
00755
KeTickCount.LowPart = NewTickCount.LowPart;
00756
KeTickCount.High1Time = NewTickCount.HighPart;
00757
00758 SharedUserData->InterruptTime.High2Time = InterruptTime.HighPart;
00759 SharedUserData->InterruptTime.LowPart = InterruptTime.LowPart;
00760 SharedUserData->InterruptTime.High1Time = InterruptTime.HighPart;
00761
#endif
00762
00763
00764
00765
00766
00767 Adjust->Barrier = 0;
00768 }
00769
00770
HalCalibratePerformanceCounter (
00771 (
volatile PLONG) &Adjust->HalNumber,
00772 (ULONGLONG) Adjust->NewCount.QuadPart
00773 );
00774
00775
KiRestoreInterrupts(Enable);
00776 }
00777
00778
VOID
00779 KeSetTimeIncrement (
00780 IN ULONG MaximumIncrement,
00781 IN ULONG MinimumIncrement
00782 )
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805 {
00806
00807
KeMaximumIncrement = MaximumIncrement;
00808
KeMinimumIncrement =
max(MinimumIncrement, 10 * 1000);
00809
KeTimeAdjustment = MaximumIncrement;
00810
KeTimeIncrement = MaximumIncrement;
00811
KiTickOffset = MaximumIncrement;
00812 }
00813
00814 BOOLEAN
00815 KeAddSystemServiceTable(
00816 IN PULONG_PTR Base,
00817 IN PULONG Count OPTIONAL,
00818 IN ULONG Limit,
00819 IN PUCHAR Number,
00820 IN ULONG Index
00821 )
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855 {
00856
00857
PAGED_CODE();
00858
00859
00860
00861
00862
00863
00864
00865
if ((
Index >
NUMBER_SERVICE_TABLES - 1) ||
00866 (
KeServiceDescriptorTable[
Index].
Base !=
NULL) ||
00867 (
KeServiceDescriptorTableShadow[
Index].
Base !=
NULL)) {
00868
return FALSE;
00869
00870 }
else {
00871
00872
00873
00874
00875
00876
00877
00878
KeServiceDescriptorTableShadow[
Index].
Base = Base;
00879
KeServiceDescriptorTableShadow[
Index].
Count =
Count;
00880
KeServiceDescriptorTableShadow[
Index].
Limit = Limit;
00881
#if defined(_IA64_)
00882
00883
00884
00885
00886
00887
00888
KeServiceDescriptorTableShadow[
Index].TableBaseGpOffset =
00889 (LONG)(*(Base-1) - (ULONG_PTR)Base);
00890
#endif
00891
KeServiceDescriptorTableShadow[
Index].
Number = Number;
00892
if (
Index != 1) {
00893
KeServiceDescriptorTable[
Index].
Base = Base;
00894
KeServiceDescriptorTable[
Index].
Count =
Count;
00895
KeServiceDescriptorTable[
Index].
Limit = Limit;
00896
#if defined(_IA64_)
00897
KeServiceDescriptorTable[
Index].TableBaseGpOffset =
00898 (LONG)(*(Base-1) - (ULONG_PTR)Base);
00899
#endif
00900
KeServiceDescriptorTable[
Index].
Number = Number;
00901 }
00902
00903
return TRUE;
00904 }
00905 }
00906
00907
VOID
00908
FASTCALL
00909 KeSetSwapContextNotifyRoutine(
00910 IN PSWAP_CONTEXT_NOTIFY_ROUTINE NotifyRoutine
00911 )
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930 {
00931
00932
PAGED_CODE();
00933
00934
KiSwapContextNotifyRoutine = NotifyRoutine;
00935
return;
00936 }
00937
00938
VOID
00939
FASTCALL
00940 KeSetThreadSelectNotifyRoutine(
00941 IN PTHREAD_SELECT_NOTIFY_ROUTINE NotifyRoutine
00942 )
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962 {
00963
00964
PAGED_CODE();
00965
00966
KiThreadSelectNotifyRoutine = NotifyRoutine;
00967
return;
00968 }
00969
00970
VOID
00971
FASTCALL
00972 KeSetTimeUpdateNotifyRoutine(
00973 IN PTIME_UPDATE_NOTIFY_ROUTINE NotifyRoutine
00974 )
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994 {
00995
00996
PAGED_CODE();
00997
00998
KiTimeUpdateNotifyRoutine = NotifyRoutine;
00999
return;
01000 }
01001
01002
01003 KAFFINITY
01004 KeQueryActiveProcessors(
01005 VOID
01006 )
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024 {
01025
PAGED_CODE();
01026
01027
return(
KeActiveProcessors);
01028 }