summaryrefslogtreecommitdiffstats
path: root/private/ntos/nthals/halws3/i386/w3sproc.c
blob: 54a5b5395d1b2b60f2daec5703edf9b76f1d5755 (plain) (blame)
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
/*++

Copyright (c) 1991  Microsoft Corporation
Copyright (c) 1993  Sequent Computer Systems, Inc.

Module Name:

    w3sproc.c

Abstract:

    WinServer 3000 Start Next Processor C code.

    This module implements the initialization of the system dependent
    functions that define the Hardware Architecture Layer (HAL) for a
    WinServer 3000.

Author:

    Ken Reneris (kenr) 22-Jan-1991
    Phil Hochstetler (phil@sequent.com) 3-30-93

Environment:

    Kernel mode only.

Revision History:

--*/

#include "halp.h"
#include "apic.inc"
#include "w3.inc"
#include "halver.h"

VOID
HalpMapCR3 (
    IN ULONG VirtAddress,
    IN PVOID PhysicalAddress,
    IN ULONG Length
    );

ULONG
HalpBuildTiledCR3 (
    IN PKPROCESSOR_STATE    ProcessorState
    );

VOID
HalpFreeTiledCR3 (
    VOID
    );

ULONG
HalpGetW3EisaInterruptVector(
    IN PBUS_HANDLER BusHandler,
    IN PBUS_HANDLER RootHandler,
    IN ULONG BusInterruptLevel,
    IN ULONG BusInterruptVector,
    OUT PKIRQL Irql,
    OUT PKAFFINITY Affinity
    );


VOID HalpInitOtherBuses (VOID);

#define LOW_MEMORY          0x000100000
#define MAX_PT              8

extern  VOID    StartPx_PMStub(VOID);
extern  PKPCR   HalpProcessorPCR[];


PUCHAR  MpLowStub;                  // pointer to low memory bootup stub
PVOID   MpLowStubPhysicalAddress;   // pointer to low memory bootup stub
PUCHAR  MppIDT;                     // pointer to physical memory 0:0
PVOID   MpFreeCR3[MAX_PT];          // remember pool memory to free
#ifndef NT_UP

BOOLEAN
HalpInitMP (
    IN ULONG Phase,
    IN PLOADER_PARAMETER_BLOCK LoaderBlock
    )
/*++

Routine Description:
    Allows MP initialization from HalInitSystem.

Arguments:
    Same as HalInitSystem

Return Value:
    none.

--*/
{
    PKPCR   pPCR;

    pPCR = KeGetPcr();

    if (Phase == 0) {
        MppIDT   = HalpMapPhysicalMemory (0, 1);

        //
        //  Allocate some low memory for processor bootup stub
        //

        MpLowStubPhysicalAddress = (PVOID)HalpAllocPhysicalMemory (LoaderBlock,
                                            LOW_MEMORY, 1, FALSE);

        if (!MpLowStubPhysicalAddress)
            return TRUE;

        MpLowStub = (PCHAR) HalpMapPhysicalMemory (MpLowStubPhysicalAddress, 1);
        return TRUE;

    } else {

        //
        //  Phase 1 for another processor
        //

    }
}
#endif

VOID
HalpFatal (
    IN PCHAR ErrorCode
    )

/*++

Routine Description:

    Print the fatal error code and direct end user to
    call service representitive for help.

Arguments:

    ErrorCode - the error code string to print on error.

Return Value:

    None.

--*/

{

    HalDisplayString("HAL: FATAL error #");
    HalDisplayString(ErrorCode);
    HalDisplayString(" has occured.\n");
    HalDisplayString("HAL: Call your service representitive for help.\n");
    HalDisplayString(MSG_HALT);
    KeEnterKernelDebugger();
}


VOID
HalpCheckHw (
    )

/*++

Routine Description:

    Verify different aspects of the hardware to assure
    the current hardware is both a WinServer and running
    sufficient level hardware to support Windows NT.

Arguments:

    None.

Return Value:

    None.

--*/

{
    UNICODE_STRING unicodeValueName;
    UNICODE_STRING KeyName;
    OBJECT_ATTRIBUTES ObjectAttributes;
    PKEY_VALUE_PARTIAL_INFORMATION KeyValueInformation;
    NTSTATUS NtStatus;
    HANDLE SystemHandle;
    UCHAR KeyValueBuffer[512];
    ULONG resultLength;
    PWSTR s;
    ULONG m, d, y;

    KeyValueInformation = (PKEY_VALUE_PARTIAL_INFORMATION)KeyValueBuffer;

    RtlInitUnicodeString( 
		    &unicodeValueName,
		    L"\\Registry\\Machine\\Hardware\\Description\\System"
		    );

    InitializeObjectAttributes(
		    &ObjectAttributes,
		    &unicodeValueName,
		    OBJ_CASE_INSENSITIVE,
		    (HANDLE) NULL,
		    (PSECURITY_DESCRIPTOR) NULL
		    );

    NtStatus = ZwOpenKey( 
		    &SystemHandle,
		    KEY_READ,
		    &ObjectAttributes
		    );

    if (!NT_SUCCESS( NtStatus ))
	HalpFatal("1015801");

    RtlInitUnicodeString( &KeyName, L"SystemBiosDate" );

    NtStatus = ZwQueryValueKey(
		    SystemHandle,
		    &KeyName,
		    KeyValuePartialInformation,
		    KeyValueInformation,
		    sizeof(KeyValueBuffer),
		    &resultLength
		    );

    if (!NT_SUCCESS( NtStatus )
    || KeyValueInformation->Type != REG_SZ
    || KeyValueInformation->DataLength < 16
       )
	HalpFatal("1015802");

    //
    // Bios date must be greater than or equal to "02/27/93"
    //
	
    s = (PWSTR)KeyValueInformation->Data;
    m = (*s++ - L'0') * 10; m += (*s++ - L'0'); *s++;	// skip '/'
    d = (*s++ - L'0') * 10; d += (*s++ - L'0'); *s++;	// skip '/'
    y = (*s++ - L'0') * 10; y += (*s++ - L'0');

    if ((y * 365) + (m * 31) + d < (93 * 365) + (2 * 31) + 27)
	HalpFatal("1015803");

    RtlInitUnicodeString( &KeyName, L"VideoBiosVersion" );

    NtStatus = ZwQueryValueKey(
		    SystemHandle,
		    &KeyName,
		    KeyValueBasicInformation,
		    KeyValueInformation,
		    sizeof(KeyValueBuffer),
		    &resultLength
		    );

    if (!NT_SUCCESS( NtStatus )
    || KeyValueInformation->Type != REG_MULTI_SZ
       )
	HalpFatal("1015804");

    ZwClose(SystemHandle);

    RtlInitUnicodeString( 
		    &unicodeValueName,
		    L"\\Registry\\Machine\\Hardware\\Description\\System\\EisaAdapter\\0\\DiskController"
		    );

    InitializeObjectAttributes(
		    &ObjectAttributes,
		    &unicodeValueName,
		    OBJ_CASE_INSENSITIVE,
		    (HANDLE) NULL,
		    (PSECURITY_DESCRIPTOR) NULL
		    );

    NtStatus = ZwOpenKey( 
		    &SystemHandle,
		    KEY_READ,
		    &ObjectAttributes
		    );

    if (!NT_SUCCESS( NtStatus ))
	HalpFatal("1015805");

    ZwClose(SystemHandle);

    RtlInitUnicodeString( 
		    &unicodeValueName,
		    L"\\Registry\\Machine\\Hardware\\Description\\System\\EisaAdapter\\0\\DisplayController"
		    );

    InitializeObjectAttributes(
		    &ObjectAttributes,
		    &unicodeValueName,
		    OBJ_CASE_INSENSITIVE,
		    (HANDLE) NULL,
		    (PSECURITY_DESCRIPTOR) NULL
		    );

    NtStatus = ZwOpenKey( 
		    &SystemHandle,
		    KEY_READ,
		    &ObjectAttributes
		    );

    if (!NT_SUCCESS( NtStatus ))
	HalpFatal("1015806");

    ZwClose(SystemHandle);
}

BOOLEAN
HalAllProcessorsStarted (
    VOID
    )
{
    return TRUE;
}

VOID
HalReportResourceUsage (
    VOID
    )
/*++

Routine Description:
    The registery is now enabled - time to report resources which are
    used by the HAL.

Arguments:

Return Value:

--*/
{
    ANSI_STRING     AHalName;
    UNICODE_STRING  UHalName;

    HalInitSystemPhase2 ();

    RtlInitAnsiString (&AHalName, HalName);
    RtlAnsiStringToUnicodeString (&UHalName, &AHalName, TRUE);

    HalpReportResourceUsage (
        &UHalName,          // descriptive name
        Eisa                // WinServer 3000 is an EISA machine
    );

    RtlFreeUnicodeString (&UHalName);

    //
    // Take advantage of the registry being enabled to check
    // it for a WinServer with the proper hardware support.
    //

    HalpCheckHw();

}


VOID
HalpRebootNow(
    )
/*++
--*/
{
#define KEYBPORT (PUCHAR)0x64
#define RESET 0xfe

#ifndef NT_UP
    PKPCR             pPCR;
    PWS3_HAL_PRIVATE pPriv;
    USHORT	   FCRAddr;
    UCHAR		 i;
    UCHAR	        Me;

    pPCR = KeGetPcr();
    pPriv = (PWS3_HAL_PRIVATE) &pPCR->HalReserved[0];
    Me = pPriv->PcrNumber;

    //
    // Reset each of the slaves and issue the reset command.
    //

    for (i = 1; HalpProcessorPCR[i]; i++) {

	//
	// Never reset myself
	//

	if (i == Me)
	    continue;

	//
	// Put processor into reset by setting the FCR reset bit
	//

        pPriv = (PWS3_HAL_PRIVATE) &HalpProcessorPCR[i]->HalReserved[0];
        FCRAddr = pPriv->ProcSlotAddr | FCR;
        WRITE_PORT_USHORT((PUSHORT) FCRAddr, 
            (USHORT)(READ_PORT_USHORT((PUSHORT) FCRAddr) |
		    ((USHORT) FCR_RESET_MASK)));
    }

    //
    // Send the reset command to the keyboard controller
    //

    for (;;) {
        WRITE_PORT_UCHAR(KEYBPORT, RESET);
        KeStallExecutionProcessor(30 * 1000000);
    }
#else
    //
    // Send the reset command to the keyboard controller
    //

    for (;;) {
        WRITE_PORT_UCHAR(KEYBPORT, RESET);
        KeStallExecutionProcessor(30 * 1000000);
    }
#endif
}

VOID
HalpResetAllProcessors (
    )
/*++

Routine Description:
    Called very late in the process of rebooting the machine
    from HalpReboot() to attempt to put the machine in a state
    where sending the reset command to the keyboard controller will
    actually reboot the machine.

    On a WinServer 3000, only the P0 processor will see the system
    reset line so we must attempt to shutdown each of the slaves.
    If the P0 processor is not responding, then we make a best attempt.
    Also, this has some implication on leaving errors asserted (like
    the APIC NMI state) which the BIOS does not handle.

    N.B.  Is is critical that we do not put P0 into reset by writing
    the feature control register (FCR) because it will not see the
    reset and the system will hang.

Arguments:
    None.

Return Value:
    None.

--*/
{
#ifdef NT_UP
    HalDisplayString("System Reboot in progress.\n");
    HalDisplayString("Please wait while the system reboots.\n");
    HalpRebootNow();
#else
    PWS3_HAL_PRIVATE pPriv;
    PKPCR             pPCR;

    pPCR = KeGetPcr();
    pPriv = (PWS3_HAL_PRIVATE) &pPCR->HalReserved[0];

    HalDisplayString("System Reboot in progress.\n");
    HalDisplayString("Please wait while the system reboots.\n");

    if (pPriv->PcrNumber == 0)
	HalpRebootNow();

    //
    // On P1-PN, so try to signal P0 to reboot the system by
    // taking over P0's IPI handler and sending an IPI.
    //

    HalpProcessorPCR[0]->IDT[APIC_IPI_VECTOR].ExtendedOffset = HIGHWORD(HalpRebootNow);
    HalpProcessorPCR[0]->IDT[APIC_IPI_VECTOR].Offset = LOWWORD(HalpRebootNow);
    HalRequestIpi(1);
    KeStallExecutionProcessor(30 * 1000000);

    //
    // We only reach here if P0 fails to reboot the system.
    // Try to reset all the slaves but our self and P0 and
    // then try to reboot.  This is the last attempt and
    // if it fails, we will probably hang the system.
    //

    HalpRebootNow();
#endif
}

ULONG
HalpBuildTiledCR3 (
    IN PKPROCESSOR_STATE    ProcessorState
    )
/*++

Routine Description:
    When the x86 processor is reset it starts in real-mode.  In order to
    move the processor from real-mode to protected mode with flat addressing
    the segment which loads CR0 needs to have it's linear address mapped
    to machine the phyiscal location of the segment for said instruction so
    the processor can continue to execute the following instruction.

    This function is called to built such a tiled page directory.  In
    addition, other flat addresses are tiled to match the current running
    flat address for the new state.  Once the processor is in flat mode,
    we move to a NT tiled page which can then load up the remaining processors
    state.

Arguments:
    ProcessorState  - The state the new processor should start in.

Return Value:
    Physical address of Tiled page directory


--*/
{
#define GetPdeAddress(va) ((PHARDWARE_PTE)((((((ULONG)(va)) >> 22) & 0x3ff) << 2) + (PUCHAR)MpFreeCR3[0]))
#define GetPteAddress(va) ((PHARDWARE_PTE)((((((ULONG)(va)) >> 12) & 0x3ff) << 2) + (PUCHAR)pPageTable))

// bugbug kenr 27mar92 - fix physical memory usage!

    MpFreeCR3[0] = ExAllocatePool (NonPagedPool, PAGE_SIZE);
    RtlZeroMemory (MpFreeCR3[0], PAGE_SIZE);

    //
    //  Map page for real mode stub (one page)
    //
    HalpMapCR3 ((ULONG) MpLowStubPhysicalAddress,
                MpLowStubPhysicalAddress,
                PAGE_SIZE);

    //
    //  Map page for protect mode stub (one page)
    //
    HalpMapCR3 ((ULONG) &StartPx_PMStub, NULL, 0x1000);


    //
    //  Map page(s) for processors GDT
    //
    HalpMapCR3 (ProcessorState->SpecialRegisters.Gdtr.Base, NULL,
                ProcessorState->SpecialRegisters.Gdtr.Limit);


    //
    //  Map page(s) for processors IDT
    //
    HalpMapCR3 (ProcessorState->SpecialRegisters.Idtr.Base, NULL,
                ProcessorState->SpecialRegisters.Idtr.Limit);

    return MmGetPhysicalAddress (MpFreeCR3[0]).LowPart;
}


VOID
HalpMapCR3 (
    IN ULONG VirtAddress,
    IN PVOID PhysicalAddress,
    IN ULONG Length
    )
/*++

Routine Description:
    Called to build a page table entry for the passed page directory.
    Used to build a tiled page directory with real-mode & flat mode.

Arguments:
    VirtAddress     - Current virtual address
    PhysicalAddress - Optional. Physical address to be mapped to, if passed
                      as a NULL then the physical address of the passed
                      virtual address is assumed.
    Length          - number of bytes to map

Return Value:
    none.

--*/
{
    ULONG         i;
    PHARDWARE_PTE PTE;
    PVOID         pPageTable;
    PHYSICAL_ADDRESS pPhysicalPage;


    while (Length) {
        PTE = GetPdeAddress (VirtAddress);
        if (!PTE->PageFrameNumber) {
            pPageTable = ExAllocatePool (NonPagedPool, PAGE_SIZE);
            RtlZeroMemory (pPageTable, PAGE_SIZE);

            for (i=0; i<MAX_PT; i++) {
                if (!MpFreeCR3[i]) {
                    MpFreeCR3[i] = pPageTable;
                    break;
                }
            }
            ASSERT (i<MAX_PT);

            pPhysicalPage = MmGetPhysicalAddress (pPageTable);
            PTE->PageFrameNumber = (pPhysicalPage.LowPart >> PAGE_SHIFT);
            PTE->Valid = 1;
            PTE->Write = 1;
        }

        pPhysicalPage.LowPart = PTE->PageFrameNumber << PAGE_SHIFT;
        pPhysicalPage.HighPart = 0;
        pPageTable = MmMapIoSpace (pPhysicalPage, PAGE_SIZE, TRUE);

        PTE = GetPteAddress (VirtAddress);

        if (!PhysicalAddress) {
            PhysicalAddress = (PVOID)MmGetPhysicalAddress ((PVOID)VirtAddress).LowPart;
        }

        PTE->PageFrameNumber = ((ULONG) PhysicalAddress >> PAGE_SHIFT);
        PTE->Valid = 1;
        PTE->Write = 1;

        MmUnmapIoSpace (pPageTable, PAGE_SIZE);

        PhysicalAddress = 0;
        VirtAddress += PAGE_SIZE;
        if (Length > PAGE_SIZE) {
            Length -= PAGE_SIZE;
        } else {
            Length = 0;
        }
    }
}



VOID
HalpFreeTiledCR3 (
    VOID
    )
/*++

Routine Description:
    Free's any memory allocated when the tiled page directory was built.

Arguments:
    none

Return Value:
    none
--*/
{
    ULONG   i;

    for (i=0; MpFreeCR3[i]; i++) {
        ExFreePool (MpFreeCR3[i]);
        MpFreeCR3[i] = 0;
    }
}

VOID
HalpInitOtherBuses (
    VOID
    )
{
    PBUS_HANDLER     Bus;

    //
    // Change GetInterruptVector handler on Eisa Bus 0 to a
    // Winserver specific handler which supports the Winserver's
    // Eisa interrupt vectors 16-23.
    //

    Bus = HalpHandlerForBus (Eisa, 0);
    Bus->GetInterruptVector = HalpGetW3EisaInterruptVector;

    //
    // no other internal buses supported
    //
}


NTSTATUS
HalpGetMcaLog (
    OUT PMCA_EXCEPTION  Exception,
    OUT PULONG          ReturnedLength
    )
{
    return STATUS_NOT_SUPPORTED;
}

NTSTATUS
HalpMcaRegisterDriver(
    IN PMCA_DRIVER_INFO DriverInfo
    )
{
    return STATUS_NOT_SUPPORTED;
}

ULONG
FASTCALL
HalSystemVectorDispatchEntry (
    IN ULONG Vector,
    OUT PKINTERRUPT_ROUTINE **FlatDispatch,
    OUT PKINTERRUPT_ROUTINE *NoConnection
    )
{
    return FALSE;
}