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
00026
00027
#include "ki.h"
00028
00029
00030
00031
00032
00033
00034
#if defined(ALLOC_PRAGMA)
00035
00036
#pragma alloc_text(INIT, KiInitializeKernel)
00037
00038
#endif
00039
00040
#if !defined(NT_UP)
00041
VOID
00042
KiPhase0SyncIoMap (
00043 VOID
00044 );
00045
#endif
00046
00047
VOID
00048
KiSetDbatInvalid(
00049 IN ULONG Number
00050 );
00051
00052
VOID
00053
KiSetDbat(
00054 IN ULONG Number,
00055 IN ULONG PhysicalAddress,
00056 IN ULONG VirtualAddress,
00057 IN ULONG Length,
00058 IN ULONG Coherence
00059 );
00060
00061 ULONG
00062 KiInitExceptionFilter(
00063 IN PEXCEPTION_POINTERS ExceptionPointers
00064 )
00065 {
00066
#if DBG
00067
DbgPrint(
"KE: Phase0 Exception Pointers = %x\n",ExceptionPointers);
00068
DbgPrint(
"Code %x Addr %lx Info0 %x Info1 %x Info2 %x Info3 %x\n",
00069 ExceptionPointers->ExceptionRecord->ExceptionCode,
00070 (ULONG)ExceptionPointers->ExceptionRecord->ExceptionAddress,
00071 ExceptionPointers->ExceptionRecord->ExceptionInformation[0],
00072 ExceptionPointers->ExceptionRecord->ExceptionInformation[1],
00073 ExceptionPointers->ExceptionRecord->ExceptionInformation[2],
00074 ExceptionPointers->ExceptionRecord->ExceptionInformation[3]
00075 );
00076 DbgBreakPoint();
00077
#endif
00078
return EXCEPTION_EXECUTE_HANDLER;
00079 }
00080
00081
VOID
00082 KiInitializeKernel (
00083 IN
PKPROCESS Process,
00084 IN
PKTHREAD Thread,
00085 IN PVOID IdleStack,
00086 IN PKPRCB Prcb,
00087 IN CCHAR Number,
00088 IN
PLOADER_PARAMETER_BLOCK LoaderBlock
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
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 {
00129
00130 LONG
Index;
00131 KIRQL OldIrql;
00132
PRESTART_BLOCK RestartBlock;
00133 KPCR *Pcr;
00134
00135
00136
00137
00138
00139
00140 Pcr = (PKPCR)PCRsprg1;
00141
00142
#if !defined(NT_UP)
00143
00144
if (Number != 0) {
00145
KiPhase0SyncIoMap();
00146 }
00147
00148
#endif
00149
00150
00151
00152
00153
00154 Pcr->ProcessorRevision = KeGetPvr() & 0xFFFF;
00155 Pcr->ProcessorVersion = (KeGetPvr() >> 16);
00156
00157
00158
00159
00160
00161
00162
KeProcessorArchitecture = PROCESSOR_ARCHITECTURE_PPC;
00163
KeFeatureBits = 0;
00164
if (
KeProcessorLevel == 0 ||
00165
KeProcessorLevel > (
USHORT)Pcr->ProcessorVersion
00166 ) {
00167
KeProcessorLevel = (
USHORT)Pcr->ProcessorVersion;
00168 }
00169
if (
KeProcessorRevision == 0 ||
00170
KeProcessorRevision > (
USHORT)Pcr->ProcessorRevision
00171 ) {
00172
KeProcessorRevision = (
USHORT)Pcr->ProcessorRevision;
00173 }
00174
00175
00176
00177
00178
00179
HalInitializeProcessor(Number);
00180 HalSweepIcache();
00181
00182
00183
00184
00185
00186
KeLoaderBlock = LoaderBlock;
00187
00188
00189
00190
00191
00192 Prcb->MinorVersion =
PRCB_MINOR_VERSION;
00193 Prcb->MajorVersion =
PRCB_MAJOR_VERSION;
00194 Prcb->BuildType = 0;
00195
00196
#if DBG
00197
00198 Prcb->BuildType |= PRCB_BUILD_DEBUG;
00199
00200
#endif
00201
00202
#if defined(NT_UP)
00203
00204 Prcb->BuildType |= PRCB_BUILD_UNIPROCESSOR;
00205
00206
#endif
00207
00208 Prcb->CurrentThread = Thread;
00209 Prcb->NextThread = (
PKTHREAD)
NULL;
00210 Prcb->IdleThread = Thread;
00211 Prcb->SetMember = 1 << Number;
00212 Prcb->PcrPage = LoaderBlock->u.Ppc.PcrPage;
00213 Prcb->ProcessorState.SpecialRegisters.Sprg0 =
00214 LoaderBlock->u.Ppc.PcrPage <<
PAGE_SHIFT;
00215 Prcb->ProcessorState.SpecialRegisters.Sprg1 = (ULONG)Pcr;
00216
00217
#if !defined(NT_UP)
00218
00219 Prcb->TargetSet = 0;
00220 Prcb->WorkerRoutine =
NULL;
00221 Prcb->RequestSummary = 0;
00222 Prcb->IpiFrozen = 0;
00223
00224
#if NT_INST
00225
00226 Prcb->IpiCounts = &KiIpiCounts[Number];
00227
00228
#endif
00229
00230
#endif
00231
00232 Prcb->MaximumDpcQueueDepth =
KiMaximumDpcQueueDepth;
00233 Prcb->MinimumDpcRate =
KiMinimumDpcRate;
00234 Prcb->AdjustDpcThreshold =
KiAdjustDpcThreshold;
00235
00236
00237
00238
00239
00240 InitializeListHead(&Prcb->DpcListHead);
00241
KeInitializeSpinLock(&Prcb->DpcLock);
00242
00243
00244
00245
00246
00247
KiProcessorBlock[Number] = Prcb;
00248
00249
00250
00251
00252
00253 Pcr->InitialStack = IdleStack;
00254 Pcr->StackLimit = (PVOID)((ULONG)IdleStack - KERNEL_STACK_SIZE);
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
if (Number == 0) {
00267
00268
00269
00270
00271
00272 KxUnexpectedInterrupt.DispatchAddress =
KiUnexpectedInterrupt;
00273
00274
00275
00276
00277
00278
00279 KxUnexpectedInterrupt.DispatchCode[0] =
00280 *(PULONG)(KxUnexpectedInterrupt.DispatchAddress);
00281 KxUnexpectedInterrupt.DispatchCode[1] =
00282 *(((PULONG)(KxUnexpectedInterrupt.DispatchAddress))+1);
00283
00284
00285
00286
00287
00288
KeInitializeSpinLock(&
KiContextSwapLock);
00289
00290
00291
00292
00293
00294
00295
00296
KiDmaIoCoherency =
DMA_READ_DCACHE_INVALIDATE |
DMA_WRITE_DCACHE_SNOOP;
00297 }
00298
00299
for (
Index = 0;
Index < MAXIMUM_VECTOR;
Index += 1) {
00300 Pcr->InterruptRoutine[
Index] =
00301 (PKINTERRUPT_ROUTINE)(&KxUnexpectedInterrupt.DispatchCode);
00302 }
00303
00304
00305
00306
00307
00308 Pcr->ProfileCount = 0;
00309 Pcr->ProfileInterval = 0x200000;
00310
00311
00312
00313
00314
00315 Pcr->InterruptRoutine[0] =
KiUnexpectedInterrupt;
00316
00317
00318
00319
00320
00321
00322 Pcr->InterruptRoutine[
APC_LEVEL] =
KiUnexpectedInterrupt;
00323 Pcr->InterruptRoutine[
DISPATCH_LEVEL] =
KiUnexpectedInterrupt;
00324
00325 Pcr->ReservedVectors = (1 <<
PASSIVE_LEVEL) |
00326 (1 <<
APC_LEVEL) |
00327 (1 <<
DISPATCH_LEVEL);
00328
00329
00330
00331
00332
00333
00334 Pcr->CurrentIrql =
APC_LEVEL;
00335 Pcr->SetMember = 1 << Number;
00336 Pcr->NotMember = ~Pcr->SetMember;
00337 Pcr->Number = Number;
00338
00339
00340
00341
00342
00343
00344 Pcr->StallScaleFactor = 50;
00345
00346
00347
00348
00349
00350 Thread->ApcState.Process = Process;
00351
00352
00353
00354
00355
00356
SetMember(Number,
KeActiveProcessors);
00357
00358
00359
00360
00361
00362
00363
if ((Number + 1) >
KeNumberProcessors) {
00364
KeNumberProcessors = Number + 1;
00365 }
00366
00367
00368
00369
00370
00371
00372
if (Number == 0) {
00373
00374
00375
00376
00377
00378
if (
KdInitSystem(LoaderBlock,
FALSE) ==
FALSE) {
00379
KeBugCheck(PHASE0_INITIALIZATION_FAILED);
00380 }
00381
00382
00383
00384
00385
00386 HalSweepDcache();
00387 HalSweepIcache();
00388
00389
00390
00391
00392
00393
00394
KeFlushCurrentTb();
00395
00396
#if DBG
00397
if ((PCR->IcacheMode) || (PCR->DcacheMode))
00398 {
00399
DbgPrint(
"****** Dynamic Cache Mode Kernel Invocation\n");
00400
if (PCR->IcacheMode)
00401
DbgPrint(
"****** Icache is OFF\n");
00402
if (PCR->DcacheMode)
00403
DbgPrint(
"****** Dcache is OFF\n");
00404 }
00405
#endif
00406
00407
00408
00409
00410
00411 Prcb->RestartBlock = SYSTEM_BLOCK->RestartBlock;
00412
00413
00414
00415
00416
00417
for (
Index = 1;
Index <
MAXIMUM_PROCESSORS;
Index += 1) {
00418
KiProcessorBlock[
Index] = (PKPRCB)
NULL;
00419 }
00420
00421
00422
00423
00424
00425
KiInitSystem();
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
KeInitializeProcess(Process,
00436 (KPRIORITY)0,
00437 (KAFFINITY)(0xffffffff),
00438 (PULONG)(PDE_BASE + ((PDE_BASE >>
PDI_SHIFT - 2) & 0xffc)),
00439
FALSE);
00440
00441 Process->ThreadQuantum = MAXCHAR;
00442
00443 }
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
KeInitializeThread(Thread, (PVOID)((ULONG)IdleStack -
PAGE_SIZE),
00458 (
PKSYSTEM_ROUTINE)
KeBugCheck,
00459 (
PKSTART_ROUTINE)
NULL,
00460 (PVOID)
NULL, (PCONTEXT)
NULL, (PVOID)
NULL, Process);
00461
00462 Thread->InitialStack = IdleStack;
00463 Thread->StackBase = IdleStack;
00464 Thread->StackLimit = (PVOID)((ULONG)IdleStack - KERNEL_STACK_SIZE);
00465 Thread->NextProcessor = Number;
00466 Thread->Priority = HIGH_PRIORITY;
00467 Thread->State =
Running;
00468 Thread->Affinity = (KAFFINITY)(1 << Number);
00469 Thread->WaitIrql =
DISPATCH_LEVEL;
00470
00471
00472
00473
00474
00475
00476
if (Number == 0) {
00477
SetMember(Number, Process->ActiveProcessors);
00478 }
00479
00480
00481
00482
00483
00484
try {
00485
ExpInitializeExecutive(Number, LoaderBlock);
00486
00487 } except (
KiInitExceptionFilter(GetExceptionInformation())) {
00488
KeBugCheck (PHASE0_EXCEPTION);
00489 }
00490
00491
00492
00493
00494
00495
00496
00497
00498
if (Number == 0) {
00499
KiTimeIncrementReciprocal =
KiComputeReciprocal((LONG)
KeMaximumIncrement,
00500 &
KiTimeIncrementShiftCount);
00501 Prcb->MaximumDpcQueueDepth =
KiMaximumDpcQueueDepth;
00502 Prcb->MinimumDpcRate =
KiMinimumDpcRate;
00503 Prcb->AdjustDpcThreshold =
KiAdjustDpcThreshold;
00504 }
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515 KeRaiseIrqlToDpcLevel(&OldIrql);
00516
KeSetPriorityThread(Thread, (KPRIORITY)0);
00517 Thread->Priority = LOW_REALTIME_PRIORITY;
00518
00519
00520
00521
00522
00523
KeRaiseIrql(
HIGH_LEVEL, &OldIrql);
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
#if !defined(NT_UP)
00534
00535 RestartBlock = Prcb->RestartBlock;
00536
if (RestartBlock !=
NULL) {
00537 RestartBlock->
BootStatus.
BootFinished = 1;
00538 }
00539
00540
00541
00542
00543
00544
00545
if (Number != 0) {
00546
SetMember(Number,
KiIdleSummary);
00547 }
00548
00549
#endif
00550
00551
return;
00552 }
00553
00554 #define NumBats 3
00555 #define RoundDownTo8MB(x) ((x) & ~(0x7fffff))
00556 #define VirtBase 0xb0000000
00557 #define EightMeg(x) ((x) << 23)
00558
00559
00560
static struct {
00561 ULONG
PhysBase;
00562 ULONG
RefCount;
00563 }
AllocatedBats[
NumBats];
00564
00565 PVOID
00566 KePhase0MapIo (
00567 IN PVOID PhysicalAddress,
00568 IN ULONG Length
00569 )
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611 {
00612 ULONG Base,
Offset;
00613 LONG i, FreeBat = -1;
00614
00615
00616
00617
00618 Base =
RoundDownTo8MB((ULONG)PhysicalAddress);
00619
Offset = (ULONG)PhysicalAddress - Base;
00620
00621
00622
00623
if ( (
Offset + Length) > 0x800000 ) {
00624
return (PVOID)0;
00625 }
00626
00627
for ( i = 0 ; i <
NumBats ; i++ ) {
00628
if (
AllocatedBats[i].RefCount ) {
00629
if ( Base ==
AllocatedBats[i].PhysBase ) {
00630
00631
AllocatedBats[i].RefCount++;
00632
return (PVOID)(
VirtBase +
EightMeg(i) +
Offset);
00633 }
00634 }
else {
00635
00636 FreeBat = i;
00637 }
00638 }
00639
00640
00641
00642
if ( FreeBat == -1 ) {
00643
return (PVOID)0;
00644 }
00645
00646
AllocatedBats[FreeBat].PhysBase = Base;
00647
AllocatedBats[FreeBat].RefCount = 1;
00648
KiSetDbat(FreeBat + 1, Base,
VirtBase +
EightMeg(FreeBat), 0x800000, 6);
00649
return (PVOID)(
VirtBase +
EightMeg(FreeBat) +
Offset);
00650 }
00651
00652
VOID
00653 KePhase0DeleteIoMap (
00654 IN PVOID PhysicalAddress,
00655 IN ULONG Length
00656 )
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677 {
00678 ULONG Base;
00679 LONG i;
00680
00681 Base =
RoundDownTo8MB((ULONG)PhysicalAddress);
00682
00683
for ( i = 0 ; i <
NumBats ; i++ ) {
00684
if (
AllocatedBats[i].RefCount ) {
00685
if ( Base ==
AllocatedBats[i].PhysBase ) {
00686
00687
if ( --
AllocatedBats[i].RefCount == 0 ) {
00688
KiSetDbatInvalid(i+1);
00689 }
00690
return;
00691 }
00692 }
00693 }
00694
00695
00696
00697
00698
KeBugCheck(MEMORY_MANAGEMENT);
00699 }
00700
00701
00702
#if !defined(NT_UP)
00703
00704
VOID
00705 KiPhase0SyncIoMap (
00706 VOID
00707 )
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727 {
00728 LONG i;
00729
00730
for ( i = 0 ; i <
NumBats ; i++ ) {
00731
if (
AllocatedBats[i].RefCount ) {
00732
KiSetDbat(i + 1,
00733
AllocatedBats[i].
PhysBase,
00734
VirtBase +
EightMeg(i),
00735 0x800000,
00736 6);
00737 }
00738 }
00739 }
00740
00741
#endif
00742