Author: tkreuzer
Date: Thu Jul 24 18:04:22 2008
New Revision: 34765
URL: 
http://svn.reactos.org/svn/reactos?rev=34765&view=rev
Log:
- add ke/amd64/boot.S containing KiSystemStartup
- add ke/amd64/kiinit.c, stubbed out
- allow warnings for now, so we can compile a lot more files
Added:
    branches/ros-amd64-bringup/reactos/ntoskrnl/ke/amd64/
    branches/ros-amd64-bringup/reactos/ntoskrnl/ke/amd64/boot.S   (with props)
    branches/ros-amd64-bringup/reactos/ntoskrnl/ke/amd64/kiinit.c   (with props)
Modified:
    branches/ros-amd64-bringup/reactos/ntoskrnl/ntoskrnl-generic.rbuild
    branches/ros-amd64-bringup/reactos/ntoskrnl/ntoskrnl.rbuild
Added: branches/ros-amd64-bringup/reactos/ntoskrnl/ke/amd64/boot.S
URL:
http://svn.reactos.org/svn/reactos/branches/ros-amd64-bringup/reactos/ntosk…
==============================================================================
--- branches/ros-amd64-bringup/reactos/ntoskrnl/ke/amd64/boot.S (added)
+++ branches/ros-amd64-bringup/reactos/ntoskrnl/ke/amd64/boot.S [iso-8859-1] Thu Jul 24
18:04:22 2008
@@ -1,0 +1,86 @@
+/*
+ * FILE:            ntoskrnl/ke/i386/boot.S
+ * COPYRIGHT:       See COPYING in the top level directory
+ * PURPOSE:         FreeLDR Wrapper Bootstrap Code and Bootstrap Trampoline
+ * PROGRAMMERs:     Alex Ionescu (alex(a)relsoft.net)
+ *                  Thomas Weidenmueller <w3seek(a)reactos.org>
+ */
+
+/* INCLUDES ******************************************************************/
+
+#include <asm.h>
+.intel_syntax noprefix
+.code64
+
+/* GLOBALS *******************************************************************/
+
+.bss
+.align 16
+
+/* Kernel Boot Stack */
+.globl _P0BootStack
+.space KERNEL_STACK_SIZE
+_P0BootStack:
+
+/* Kernel Double-Fault and Temporary DPC Stack */
+.globl _KiDoubleFaultStack
+.space KERNEL_STACK_SIZE
+_KiDoubleFaultStack:
+
+/* FUNCTIONS *****************************************************************/
+.global _KiSystemStartup
+.text
+.func KiSystemStartup
+_KiSystemStartup:
+
+    /* NTLDR Boot: Call the main kernel initialization */
+    test rcx, 0x80000000
+    jnz _KiSystemStartupReal
+
+    /* FREELDR Boot: Cal the FreeLDR wrapper */
+    jmp _KiRosPrepareForSystemStartup
+.endfunc
+
+/**
+ * VOID
+ * KiSetupStackAndInitializeKernel(
+ *             esp+4 = ? -> rcx
+ *             esp+8 = ? -> rdx
+ *        PVOID pNewstack     // esp+12 = new stack -> r8
+ *               esp+16 -> r9
+ *               esp+20 -> rsp + 8
+ *               esp+24 -> rsp + 16?
+ */
+.globl _KiSetupStackAndInitializeKernel
+.func KiSetupStackAndInitializeKernel
+_KiSetupStackAndInitializeKernel:
+
+    /* Save current stack */
+    mov rsi, rsp
+
+    /* Setup the new stack */
+    mov rsp, r8
+    sub rsp, NPX_FRAME_LENGTH + KTRAP_FRAME_ALIGN + KTRAP_FRAME_LENGTH
+    push CR0_EM + CR0_TS + CR0_MP
+
+    /* Copy stack parameters to the new stack */
+    push [rsi + 16]
+    push [rsi + 8]
+    xor rbp, rbp
+    call _KiInitializeKernel
+
+    /* Set the priority of this thread to 0 */
+    mov rbx, PCR[KPCR_CURRENT_THREAD]
+    mov byte ptr [rbx+KTHREAD_PRIORITY], 0
+
+    /* Force interrupts enabled and lower IRQL back to DISPATCH_LEVEL */
+    sti
+    mov rcx, DISPATCH_LEVEL
+    call _KfLowerIrql
+
+    /* Set the right wait IRQL */
+    mov byte ptr [rbx+KTHREAD_WAIT_IRQL], DISPATCH_LEVEL;
+
+    /* Jump into the idle loop */
+    jmp _KiIdleLoop
+.endfunc
Propchange: branches/ros-amd64-bringup/reactos/ntoskrnl/ke/amd64/boot.S
------------------------------------------------------------------------------
    svn:eol-style = native
Added: branches/ros-amd64-bringup/reactos/ntoskrnl/ke/amd64/kiinit.c
URL:
http://svn.reactos.org/svn/reactos/branches/ros-amd64-bringup/reactos/ntosk…
==============================================================================
--- branches/ros-amd64-bringup/reactos/ntoskrnl/ke/amd64/kiinit.c (added)
+++ branches/ros-amd64-bringup/reactos/ntoskrnl/ke/amd64/kiinit.c [iso-8859-1] Thu Jul 24
18:04:22 2008
@@ -1,0 +1,785 @@
+/*
+ * PROJECT:         ReactOS Kernel
+ * LICENSE:         GPL - See COPYING in the top level directory
+ * FILE:            ntoskrnl/ke/i386/kiinit.c
+ * PURPOSE:         Kernel Initialization for x86 CPUs
+ * PROGRAMMERS:     Alex Ionescu (alex.ionescu(a)reactos.org)
+ */
+
+/* INCLUDES *****************************************************************/
+
+#include <ntoskrnl.h>
+#define NDEBUG
+#include <debug.h>
+
+/* GLOBALS *******************************************************************/
+
+/* Spinlocks used only on X86 */
+KSPIN_LOCK KiFreezeExecutionLock;
+KSPIN_LOCK Ki486CompatibilityLock;
+
+/* BIOS Memory Map. Not NTLDR-compliant yet */
+extern ULONG KeMemoryMapRangeCount;
+extern ADDRESS_RANGE KeMemoryMap[64];
+
+/* FUNCTIONS *****************************************************************/
+
+VOID
+NTAPI
+KiInitMachineDependent(VOID)
+{
+#if 0
+    ULONG Protect;
+    ULONG CpuCount;
+    BOOLEAN FbCaching = FALSE;
+    NTSTATUS Status;
+    ULONG ReturnLength;
+    ULONG i, Affinity, Sample = 0;
+    PFX_SAVE_AREA FxSaveArea;
+    ULONG MXCsrMask = 0xFFBF;
+    ULONG Dummy[4];
+    KI_SAMPLE_MAP Samples[4];
+    PKI_SAMPLE_MAP CurrentSample = Samples;
+
+    /* Check for large page support */
+    if (KeFeatureBits & KF_LARGE_PAGE)
+    {
+        /* FIXME: Support this */
+        DPRINT1("Large Page support detected but not yet taken advantage
of!\n");
+    }
+
+    /* Check for global page support */
+    if (KeFeatureBits & KF_GLOBAL_PAGE)
+    {
+        /* Do an IPI to enable it on all CPUs */
+        CpuCount = KeNumberProcessors;
+        KeIpiGenericCall(Ki386EnableGlobalPage, (ULONG_PTR)&CpuCount);
+    }
+
+    /* Check for PAT and/or MTRR support */
+    if (KeFeatureBits & (KF_PAT | KF_MTRR))
+    {
+        /* Query the HAL to make sure we can use it */
+        Status = HalQuerySystemInformation(HalFrameBufferCachingInformation,
+                                           sizeof(BOOLEAN),
+                                           &FbCaching,
+                                           &ReturnLength);
+        if ((NT_SUCCESS(Status)) && (FbCaching))
+        {
+            /* We can't, disable it */
+            KeFeatureBits &= ~(KF_PAT | KF_MTRR);
+        }
+    }
+
+    /* Check for PAT support and enable it */
+    if (KeFeatureBits & KF_PAT) KiInitializePAT();
+
+    /* Assume no errata for now */
+    SharedUserData->ProcessorFeatures[PF_FLOATING_POINT_PRECISION_ERRATA] = 0;
+
+    /* Check if we have an NPX */
+    if (KeI386NpxPresent)
+    {
+        /* Loop every CPU */
+        i = KeActiveProcessors;
+        for (Affinity = 1; i; Affinity <<= 1)
+        {
+            /* Check if this is part of the set */
+            if (i & Affinity)
+            {
+                /* Run on this CPU */
+                i &= ~Affinity;
+                KeSetSystemAffinityThread(Affinity);
+
+                /* Detect FPU errata */
+                if (KiIsNpxErrataPresent())
+                {
+                    /* Disable NPX support */
+                    KeI386NpxPresent = FALSE;
+                    SharedUserData->
+                        ProcessorFeatures[PF_FLOATING_POINT_PRECISION_ERRATA] =
+                        TRUE;
+                    break;
+                }
+            }
+        }
+    }
+
+    /* If there's no NPX, then we're emulating the FPU */
+    SharedUserData->ProcessorFeatures[PF_FLOATING_POINT_EMULATED] =
+        !KeI386NpxPresent;
+
+    /* Check if there's no NPX, so that we can disable associated features */
+    if (!KeI386NpxPresent)
+    {
+        /* Remove NPX-related bits */
+        KeFeatureBits &= ~(KF_XMMI64 | KF_XMMI | KF_FXSR | KF_MMX);
+
+        /* Disable kernel flags */
+        KeI386FxsrPresent = KeI386XMMIPresent = FALSE;
+
+        /* Disable processor features that might've been set until now */
+        SharedUserData->ProcessorFeatures[PF_FLOATING_POINT_PRECISION_ERRATA] =
+        SharedUserData->ProcessorFeatures[PF_XMMI64_INSTRUCTIONS_AVAILABLE]   =
+        SharedUserData->ProcessorFeatures[PF_XMMI_INSTRUCTIONS_AVAILABLE]     =
+        SharedUserData->ProcessorFeatures[PF_3DNOW_INSTRUCTIONS_AVAILABLE]    =
+        SharedUserData->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] = 0;
+    }
+
+    /* Check for CR4 support */
+    if (KeFeatureBits & KF_CR4)
+    {
+        /* Do an IPI call to enable the Debug Exceptions */
+        CpuCount = KeNumberProcessors;
+        KeIpiGenericCall(Ki386EnableDE, (ULONG_PTR)&CpuCount);
+    }
+
+    /* Check if FXSR was found */
+    if (KeFeatureBits & KF_FXSR)
+    {
+        /* Do an IPI call to enable the FXSR */
+        CpuCount = KeNumberProcessors;
+        KeIpiGenericCall(Ki386EnableFxsr, (ULONG_PTR)&CpuCount);
+
+        /* Check if XMM was found too */
+        if (KeFeatureBits & KF_XMMI)
+        {
+            /* Do an IPI call to enable XMMI exceptions */
+            CpuCount = KeNumberProcessors;
+            KeIpiGenericCall(Ki386EnableXMMIExceptions, (ULONG_PTR)&CpuCount);
+
+            /* FIXME: Implement and enable XMM Page Zeroing for Mm */
+
+            /* Patch the RtlPrefetchMemoryNonTemporal routine to enable it */
+            Protect = MmGetPageProtect(NULL, RtlPrefetchMemoryNonTemporal);
+            MmSetPageProtect(NULL,
+                             RtlPrefetchMemoryNonTemporal,
+                             Protect | PAGE_IS_WRITABLE);
+            *(PCHAR)RtlPrefetchMemoryNonTemporal = 0x90;
+            MmSetPageProtect(NULL, RtlPrefetchMemoryNonTemporal, Protect);
+        }
+    }
+
+    /* Check for, and enable SYSENTER support */
+    KiRestoreFastSyscallReturnState();
+
+    /* Loop every CPU */
+    i = KeActiveProcessors;
+    for (Affinity = 1; i; Affinity <<= 1)
+    {
+        /* Check if this is part of the set */
+        if (i & Affinity)
+        {
+            /* Run on this CPU */
+            i &= ~Affinity;
+            KeSetSystemAffinityThread(Affinity);
+
+            /* Reset MHz to 0 for this CPU */
+            KeGetCurrentPrcb()->MHz = 0;
+
+            /* Check if we can use RDTSC */
+            if (KeFeatureBits & KF_RDTSC)
+            {
+                /* Start sampling loop */
+                for (;;)
+                {
+                    /* Do a dummy CPUID to start the sample */
+                    CPUID(Dummy, 0);
+
+                    /* Fill out the starting data */
+                    CurrentSample->PerfStart = KeQueryPerformanceCounter(NULL);
+                    CurrentSample->TSCStart = __rdtsc();
+                    CurrentSample->PerfFreq.QuadPart = -50000;
+
+                    /* Sleep for this sample */
+                    KeDelayExecutionThread(KernelMode,
+                                           FALSE,
+                                           &CurrentSample->PerfFreq);
+
+                    /* Do another dummy CPUID */
+                    CPUID(Dummy, 0);
+
+                    /* Fill out the ending data */
+                    CurrentSample->PerfEnd =
+                        KeQueryPerformanceCounter(&CurrentSample->PerfFreq);
+                    CurrentSample->TSCEnd = __rdtsc();
+
+                    /* Calculate the differences */
+                    CurrentSample->PerfDelta = CurrentSample->PerfEnd.QuadPart -
+                                               CurrentSample->PerfStart.QuadPart;
+                    CurrentSample->TSCDelta = CurrentSample->TSCEnd -
+                                              CurrentSample->TSCStart;
+
+                    /* Compute CPU Speed */
+                    CurrentSample->MHz = (ULONG)((CurrentSample->TSCDelta *
+                                                  CurrentSample->
+                                                  PerfFreq.QuadPart + 500000) /
+                                                 (CurrentSample->PerfDelta *
+                                                  1000000));
+
+                    /* Check if this isn't the first sample */
+                    if (Sample)
+                    {
+                        /* Check if we got a good precision within 1MHz */
+                        if ((CurrentSample->MHz == CurrentSample[-1].MHz) ||
+                            (CurrentSample->MHz == CurrentSample[-1].MHz + 1) ||
+                            (CurrentSample->MHz == CurrentSample[-1].MHz - 1))
+                        {
+                            /* We did, stop sampling */
+                            break;
+                        }
+                    }
+
+                    /* Move on */
+                    CurrentSample++;
+                    Sample++;
+
+                    if (Sample == sizeof(Samples) / sizeof(Samples[0]))
+                    {
+                        /* Restart */
+                        CurrentSample = Samples;
+                        Sample = 0;
+                    }
+                }
+
+                /* Save the CPU Speed */
+                KeGetCurrentPrcb()->MHz = CurrentSample[-1].MHz;
+            }
+
+            /* Check if we have MTRR */
+            if (KeFeatureBits & KF_MTRR)
+            {
+                /* Then manually initialize MTRR for the CPU */
+                KiInitializeMTRR(i ? FALSE : TRUE);
+            }
+
+            /* Check if we have AMD MTRR and initialize it for the CPU */
+            if (KeFeatureBits & KF_AMDK6MTRR) KiAmdK6InitializeMTRR();
+
+            /* Check if this is a buggy Pentium and apply the fixup if so */
+            if (KiI386PentiumLockErrataPresent) KiI386PentiumLockErrataFixup();
+
+            /* Check if the CPU supports FXSR */
+            if (KeFeatureBits & KF_FXSR)
+            {
+                /* Get the current thread NPX state */
+                FxSaveArea = (PVOID)
+                             ((ULONG_PTR)KeGetCurrentThread()->InitialStack -
+                             NPX_FRAME_LENGTH);
+
+                /* Clear initial MXCsr mask */
+                FxSaveArea->U.FxArea.MXCsrMask = 0;
+
+                /* Save the current NPX State */
+#ifdef __GNUC__
+                asm volatile("fxsave %0\n\t" : "=m" (*FxSaveArea));
+#else
+                __asm fxsave [FxSaveArea]
+#endif
+                /* Check if the current mask doesn't match the reserved bits */
+                if (FxSaveArea->U.FxArea.MXCsrMask != 0)
+                {
+                    /* Then use whatever it's holding */
+                    MXCsrMask = FxSaveArea->U.FxArea.MXCsrMask;
+                }
+
+                /* Check if nobody set the kernel-wide mask */
+                if (!KiMXCsrMask)
+                {
+                    /* Then use the one we calculated above */
+                    KiMXCsrMask = MXCsrMask;
+                }
+                else
+                {
+                    /* Was it set to the same value we found now? */
+                    if (KiMXCsrMask != MXCsrMask)
+                    {
+                        /* No, something is definitely wrong */
+                        KEBUGCHECKEX(MULTIPROCESSOR_CONFIGURATION_NOT_SUPPORTED,
+                                     KF_FXSR,
+                                     KiMXCsrMask,
+                                     MXCsrMask,
+                                     0);
+                    }
+                }
+
+                /* Now set the kernel mask */
+                KiMXCsrMask &= MXCsrMask;
+            }
+        }
+    }
+
+    /* Return affinity back to where it was */
+    KeRevertToUserAffinityThread();
+
+    /* NT allows limiting the duration of an ISR with a registry key */
+    if (KiTimeLimitIsrMicroseconds)
+    {
+        /* FIXME: TODO */
+        DPRINT1("ISR Time Limit not yet supported\n");
+    }
+#endif
+}
+
+VOID
+NTAPI
+KiInitializePcr(IN ULONG ProcessorNumber,
+                IN PKIPCR Pcr,
+                IN PKIDTENTRY Idt,
+                IN PKGDTENTRY Gdt,
+                IN PKTSS Tss,
+                IN PKTHREAD IdleThread,
+                IN PVOID DpcStack)
+{
+#if 0
+    /* Setup the TIB */
+    Pcr->NtTib.ExceptionList = EXCEPTION_CHAIN_END;
+    Pcr->NtTib.StackBase = 0;
+    Pcr->NtTib.StackLimit = 0;
+    Pcr->NtTib.Self = NULL;
+
+    /* Set the Current Thread */
+    Pcr->PrcbData.CurrentThread = IdleThread;
+
+    /* Set pointers to ourselves */
+    Pcr->Self = (PKPCR)Pcr;
+    Pcr->Prcb = &Pcr->PrcbData;
+
+    /* Set the PCR Version */
+    Pcr->MajorVersion = PCR_MAJOR_VERSION;
+    Pcr->MinorVersion = PCR_MINOR_VERSION;
+
+    /* Set the PCRB Version */
+    Pcr->PrcbData.MajorVersion = 1;
+    Pcr->PrcbData.MinorVersion = 1;
+
+    /* Set the Build Type */
+    Pcr->PrcbData.BuildType = 0;
+#ifndef CONFIG_SMP
+    Pcr->PrcbData.BuildType |= PRCB_BUILD_UNIPROCESSOR;
+#endif
+#ifdef DBG
+    Pcr->PrcbData.BuildType |= PRCB_BUILD_DEBUG;
+#endif
+
+    /* Set the Processor Number and current Processor Mask */
+    Pcr->PrcbData.Number = (UCHAR)ProcessorNumber;
+    Pcr->PrcbData.SetMember = 1 << ProcessorNumber;
+
+    /* Set the PRCB for this Processor */
+    KiProcessorBlock[ProcessorNumber] = Pcr->Prcb;
+
+    /* Start us out at PASSIVE_LEVEL */
+    Pcr->Irql = PASSIVE_LEVEL;
+
+    /* Set the GDI, IDT, TSS and DPC Stack */
+    Pcr->GDT = (PVOID)Gdt;
+    Pcr->IDT = Idt;
+    Pcr->TSS = Tss;
+    Pcr->TssCopy = Tss;
+    Pcr->PrcbData.DpcStack = DpcStack;
+
+    /* Setup the processor set */
+    Pcr->PrcbData.MultiThreadProcessorSet = Pcr->PrcbData.SetMember;
+#endif
+}
+
+VOID
+NTAPI
+KiInitializeKernel(IN PKPROCESS InitProcess,
+                   IN PKTHREAD InitThread,
+                   IN PVOID IdleStack,
+                   IN PKPRCB Prcb,
+                   IN CCHAR Number,
+                   IN PLOADER_PARAMETER_BLOCK LoaderBlock)
+{
+#if 0
+    BOOLEAN NpxPresent;
+    ULONG FeatureBits;
+    LARGE_INTEGER PageDirectory;
+    PVOID DpcStack;
+    ULONG Vendor[3];
+
+    /* Detect and set the CPU Type */
+    KiSetProcessorType();
+
+    /* Set CR0 features based on detected CPU */
+    KiSetCR0Bits();
+
+    /* Check if an FPU is present */
+    NpxPresent = KiIsNpxPresent();
+
+    /* Initialize the Power Management Support for this PRCB */
+    PoInitializePrcb(Prcb);
+
+    /* Bugcheck if this is a 386 CPU */
+    if (Prcb->CpuType == 3) KeBugCheckEx(0x5D, 0x386, 0, 0, 0);
+
+    /* Get the processor features for the CPU */
+    FeatureBits = KiGetFeatureBits();
+
+    /* Set the default NX policy (opt-in) */
+    SharedUserData->NXSupportPolicy = NX_SUPPORT_POLICY_OPTIN;
+
+    /* Check if NPX is always on */
+    if (strstr(KeLoaderBlock->LoadOptions, "NOEXECUTE=ALWAYSON"))
+    {
+        /* Set it always on */
+        SharedUserData->NXSupportPolicy = NX_SUPPORT_POLICY_ALWAYSON;
+        FeatureBits |= KF_NX_ENABLED;
+    }
+    else if (strstr(KeLoaderBlock->LoadOptions, "NOEXECUTE=OPTOUT"))
+    {
+        /* Set it in opt-out mode */
+        SharedUserData->NXSupportPolicy = NX_SUPPORT_POLICY_OPTOUT;
+        FeatureBits |= KF_NX_ENABLED;
+    }
+    else if ((strstr(KeLoaderBlock->LoadOptions, "NOEXECUTE=OPTIN")) ||
+             (strstr(KeLoaderBlock->LoadOptions, "NOEXECUTE")))
+    {
+        /* Set the feature bits */
+        FeatureBits |= KF_NX_ENABLED;
+    }
+    else if ((strstr(KeLoaderBlock->LoadOptions, "NOEXECUTE=ALWAYSOFF")) ||
+             (strstr(KeLoaderBlock->LoadOptions, "EXECUTE")))
+    {
+        /* Set disabled mode */
+        SharedUserData->NXSupportPolicy = NX_SUPPORT_POLICY_ALWAYSOFF;
+        FeatureBits |= KF_NX_DISABLED;
+    }
+
+    /* Save feature bits */
+    Prcb->FeatureBits = FeatureBits;
+
+    /* Save CPU state */
+    KiSaveProcessorControlState(&Prcb->ProcessorState);
+
+    /* Get cache line information for this CPU */
+    KiGetCacheInformation();
+
+    /* Initialize spinlocks and DPC data */
+    KiInitSpinLocks(Prcb, Number);
+
+    /* Check if this is the Boot CPU */
+    if (!Number)
+    {
+        /* Set Node Data */
+        KeNodeBlock[0] = &KiNode0;
+        Prcb->ParentNode = KeNodeBlock[0];
+        KeNodeBlock[0]->ProcessorMask = Prcb->SetMember;
+
+        /* Set boot-level flags */
+        KeI386NpxPresent = NpxPresent;
+        KeI386CpuType = Prcb->CpuType;
+        KeI386CpuStep = Prcb->CpuStep;
+        KeProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
+        KeProcessorLevel = (USHORT)Prcb->CpuType;
+        if (Prcb->CpuID) KeProcessorRevision = Prcb->CpuStep;
+        KeFeatureBits = FeatureBits;
+        KeI386FxsrPresent = (KeFeatureBits & KF_FXSR) ? TRUE : FALSE;
+        KeI386XMMIPresent = (KeFeatureBits & KF_XMMI) ? TRUE : FALSE;
+
+        /* Detect 8-byte compare exchange support */
+        if (!(KeFeatureBits & KF_CMPXCHG8B))
+        {
+            /* Copy the vendor string */
+            RtlCopyMemory(Vendor, Prcb->VendorString, sizeof(Vendor));
+
+            /* Bugcheck the system. Windows *requires* this */
+            KeBugCheckEx(0x5D,
+                         (1 << 24 ) | (Prcb->CpuType << 16) |
Prcb->CpuStep,
+                         Vendor[0],
+                         Vendor[1],
+                         Vendor[2]);
+        }
+
+        /* Set the current MP Master KPRCB to the Boot PRCB */
+        Prcb->MultiThreadSetMaster = Prcb;
+
+        /* Lower to APC_LEVEL */
+        KeLowerIrql(APC_LEVEL);
+
+        /* Initialize some spinlocks */
+        KeInitializeSpinLock(&KiFreezeExecutionLock);
+        KeInitializeSpinLock(&Ki486CompatibilityLock);
+
+        /* Initialize portable parts of the OS */
+        KiInitSystem();
+
+        /* Initialize the Idle Process and the Process Listhead */
+        InitializeListHead(&KiProcessListHead);
+        PageDirectory.QuadPart = 0;
+        KeInitializeProcess(InitProcess,
+                            0,
+                            0xFFFFFFFF,
+                            &PageDirectory,
+                            FALSE);
+        InitProcess->QuantumReset = MAXCHAR;
+    }
+    else
+    {
+        /* FIXME */
+        DPRINT1("SMP Boot support not yet present\n");
+    }
+
+    /* Setup the Idle Thread */
+    KeInitializeThread(InitProcess,
+                       InitThread,
+                       NULL,
+                       NULL,
+                       NULL,
+                       NULL,
+                       NULL,
+                       IdleStack);
+    InitThread->NextProcessor = Number;
+    InitThread->Priority = HIGH_PRIORITY;
+    InitThread->State = Running;
+    InitThread->Affinity = 1 << Number;
+    InitThread->WaitIrql = DISPATCH_LEVEL;
+    InitProcess->ActiveProcessors = 1 << Number;
+
+    /* HACK for MmUpdatePageDir */
+    ((PETHREAD)InitThread)->ThreadsProcess = (PEPROCESS)InitProcess;
+
+    /* Set basic CPU Features that user mode can read */
+    SharedUserData->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] =
+        (KeFeatureBits & KF_MMX) ? TRUE: FALSE;
+    SharedUserData->ProcessorFeatures[PF_COMPARE_EXCHANGE_DOUBLE] =
+        (KeFeatureBits & KF_CMPXCHG8B) ? TRUE: FALSE;
+    SharedUserData->ProcessorFeatures[PF_XMMI_INSTRUCTIONS_AVAILABLE] =
+        ((KeFeatureBits & KF_FXSR) && (KeFeatureBits & KF_XMMI)) ? TRUE:
FALSE;
+    SharedUserData->ProcessorFeatures[PF_XMMI64_INSTRUCTIONS_AVAILABLE] =
+        ((KeFeatureBits & KF_FXSR) && (KeFeatureBits & KF_XMMI64)) ?
TRUE: FALSE;
+    SharedUserData->ProcessorFeatures[PF_3DNOW_INSTRUCTIONS_AVAILABLE] =
+        (KeFeatureBits & KF_3DNOW) ? TRUE: FALSE;
+    SharedUserData->ProcessorFeatures[PF_RDTSC_INSTRUCTION_AVAILABLE] =
+        (KeFeatureBits & KF_RDTSC) ? TRUE: FALSE;
+
+    /* Set up the thread-related fields in the PRCB */
+    Prcb->CurrentThread = InitThread;
+    Prcb->NextThread = NULL;
+    Prcb->IdleThread = InitThread;
+
+    /* Initialize the Kernel Executive */
+    ExpInitializeExecutive(Number, LoaderBlock);
+
+    /* Only do this on the boot CPU */
+    if (!Number)
+    {
+        /* Calculate the time reciprocal */
+        KiTimeIncrementReciprocal =
+            KiComputeReciprocal(KeMaximumIncrement,
+                                &KiTimeIncrementShiftCount);
+
+        /* Update DPC Values in case they got updated by the executive */
+        Prcb->MaximumDpcQueueDepth = KiMaximumDpcQueueDepth;
+        Prcb->MinimumDpcRate = KiMinimumDpcRate;
+        Prcb->AdjustDpcThreshold = KiAdjustDpcThreshold;
+
+        /* Allocate the DPC Stack */
+        DpcStack = MmCreateKernelStack(FALSE, 0);
+        if (!DpcStack) KeBugCheckEx(NO_PAGES_AVAILABLE, 1, 0, 0, 0);
+        Prcb->DpcStack = DpcStack;
+
+        /* Allocate the IOPM save area. */
+        Ki386IopmSaveArea = ExAllocatePoolWithTag(PagedPool,
+                                                  PAGE_SIZE * 2,
+                                                  TAG('K', 'e', '
', ' '));
+        if (!Ki386IopmSaveArea)
+        {
+            /* Bugcheck. We need this for V86/VDM support. */
+            KeBugCheckEx(NO_PAGES_AVAILABLE, 2, PAGE_SIZE * 2, 0, 0);
+        }
+    }
+
+    /* Raise to Dispatch */
+    KfRaiseIrql(DISPATCH_LEVEL);
+
+    /* Set the Idle Priority to 0. This will jump into Phase 1 */
+    KeSetPriorityThread(InitThread, 0);
+
+    /* If there's no thread scheduled, put this CPU in the Idle summary */
+    KiAcquirePrcbLock(Prcb);
+    if (!Prcb->NextThread) KiIdleSummary |= 1 << Number;
+    KiReleasePrcbLock(Prcb);
+
+    /* Raise back to HIGH_LEVEL and clear the PRCB for the loader block */
+    KfRaiseIrql(HIGH_LEVEL);
+    LoaderBlock->Prcb = 0;
+#endif
+}
+
+VOID
+FASTCALL
+KiGetMachineBootPointers(IN PKGDTENTRY *Gdt,
+                         IN PKIDTENTRY *Idt,
+                         IN PKIPCR *Pcr,
+                         IN PKTSS *Tss)
+{
+#if 0
+    KDESCRIPTOR GdtDescriptor = {0}, IdtDescriptor = {0};
+    KGDTENTRY TssSelector, PcrSelector;
+    USHORT Tr = 0, Fs;
+
+    /* Get GDT and IDT descriptors */
+    Ke386GetGlobalDescriptorTable(*(PKDESCRIPTOR)&GdtDescriptor.Limit);
+    Ke386GetInterruptDescriptorTable(*(PKDESCRIPTOR)&IdtDescriptor.Limit);
+
+    /* Save IDT and GDT */
+    *Gdt = (PKGDTENTRY)GdtDescriptor.Base;
+    *Idt = (PKIDTENTRY)IdtDescriptor.Base;
+
+    /* Get TSS and FS Selectors */
+    Ke386GetTr(Tr);
+    if (Tr != KGDT_TSS) Tr = KGDT_TSS; // FIXME: HACKHACK
+    Fs = Ke386GetFs();
+
+    /* Get PCR Selector, mask it and get its GDT Entry */
+    PcrSelector = *(PKGDTENTRY)((ULONG_PTR)*Gdt + (Fs & ~RPL_MASK));
+
+    /* Get the KPCR itself */
+    *Pcr = (PKIPCR)(ULONG_PTR)(PcrSelector.BaseLow |
+                               PcrSelector.HighWord.Bytes.BaseMid << 16 |
+                               PcrSelector.HighWord.Bytes.BaseHi << 24);
+
+    /* Get TSS Selector, mask it and get its GDT Entry */
+    TssSelector = *(PKGDTENTRY)((ULONG_PTR)*Gdt + (Tr & ~RPL_MASK));
+
+    /* Get the KTSS itself */
+    *Tss = (PKTSS)(ULONG_PTR)(TssSelector.BaseLow |
+                              TssSelector.HighWord.Bytes.BaseMid << 16 |
+                              TssSelector.HighWord.Bytes.BaseHi << 24);
+#endif
+}
+
+VOID
+NTAPI
+KiSystemStartupReal(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
+{
+#if 0
+    ULONG Cpu;
+    PKTHREAD InitialThread;
+    ULONG InitialStack;
+    PKGDTENTRY Gdt;
+    PKIDTENTRY Idt;
+    KIDTENTRY NmiEntry, DoubleFaultEntry;
+    PKTSS Tss;
+    PKIPCR Pcr;
+
+    /* Save the loader block and get the current CPU */
+    KeLoaderBlock = LoaderBlock;
+    Cpu = KeNumberProcessors;
+    if (!Cpu)
+    {
+        /* If this is the boot CPU, set FS and the CPU Number*/
+        Ke386SetFs(KGDT_R0_PCR);
+        __writefsdword(KPCR_PROCESSOR_NUMBER, Cpu);
+
+        /* Set the initial stack and idle thread as well */
+        LoaderBlock->KernelStack = (ULONG_PTR)P0BootStack;
+        LoaderBlock->Thread = (ULONG_PTR)&KiInitialThread;
+    }
+
+    /* Save the initial thread and stack */
+    InitialStack = LoaderBlock->KernelStack;
+    InitialThread = (PKTHREAD)LoaderBlock->Thread;
+
+    /* Clean the APC List Head */
+    InitializeListHead(&InitialThread->ApcState.ApcListHead[KernelMode]);
+
+    /* Initialize the machine type */
+    KiInitializeMachineType();
+
+    /* Skip initial setup if this isn't the Boot CPU */
+    if (Cpu) goto AppCpuInit;
+
+    /* Get GDT, IDT, PCR and TSS pointers */
+    KiGetMachineBootPointers(&Gdt, &Idt, &Pcr, &Tss);
+
+    /* Setup the TSS descriptors and entries */
+    Ki386InitializeTss(Tss, Idt, Gdt);
+
+    /* Initialize the PCR */
+    RtlZeroMemory(Pcr, PAGE_SIZE);
+    KiInitializePcr(Cpu,
+                    Pcr,
+                    Idt,
+                    Gdt,
+                    Tss,
+                    InitialThread,
+                    KiDoubleFaultStack);
+
+    /* Set us as the current process */
+    InitialThread->ApcState.Process = &KiInitialProcess.Pcb;
+
+    /* Clear DR6/7 to cleanup bootloader debugging */
+    __writefsdword(KPCR_TEB, 0);
+    __writefsdword(KPCR_DR6, 0);
+    __writefsdword(KPCR_DR7, 0);
+
+    /* Setup the IDT */
+    KeInitExceptions();
+
+    /* Load Ring 3 selectors for DS/ES */
+    Ke386SetDs(KGDT_R3_DATA | RPL_MASK);
+    Ke386SetEs(KGDT_R3_DATA | RPL_MASK);
+
+    /* Save NMI and double fault traps */
+    RtlCopyMemory(&NmiEntry, &Idt[2], sizeof(KIDTENTRY));
+    RtlCopyMemory(&DoubleFaultEntry, &Idt[8], sizeof(KIDTENTRY));
+
+    /* Copy kernel's trap handlers */
+    RtlCopyMemory(Idt,
+                  (PVOID)KiIdtDescriptor.Base,
+                  KiIdtDescriptor.Limit + 1);
+
+    /* Restore NMI and double fault */
+    RtlCopyMemory(&Idt[2], &NmiEntry, sizeof(KIDTENTRY));
+    RtlCopyMemory(&Idt[8], &DoubleFaultEntry, sizeof(KIDTENTRY));
+
+AppCpuInit:
+    /* Loop until we can release the freeze lock */
+    do
+    {
+        /* Loop until execution can continue */
+        while (*(volatile PKSPIN_LOCK*)&KiFreezeExecutionLock == (PVOID)1);
+    } while(InterlockedBitTestAndSet((PLONG)&KiFreezeExecutionLock, 0));
+
+    /* Setup CPU-related fields */
+    __writefsdword(KPCR_NUMBER, Cpu);
+    __writefsdword(KPCR_SET_MEMBER, 1 << Cpu);
+    __writefsdword(KPCR_SET_MEMBER_COPY, 1 << Cpu);
+    __writefsdword(KPCR_PRCB_SET_MEMBER, 1 << Cpu);
+
+    /* Initialize the Processor with HAL */
+    HalInitializeProcessor(Cpu, KeLoaderBlock);
+
+    /* Set active processors */
+    KeActiveProcessors |= __readfsdword(KPCR_SET_MEMBER);
+    KeNumberProcessors++;
+
+    /* Check if this is the boot CPU */
+    if (!Cpu)
+    {
+        /* Initialize debugging system */
+        KdInitSystem(0, KeLoaderBlock);
+
+        /* Check for break-in */
+        if (KdPollBreakIn()) DbgBreakPointWithStatus(1);
+    }
+
+    /* Raise to HIGH_LEVEL */
+    KfRaiseIrql(HIGH_LEVEL);
+
+    /* Align stack and make space for the trap frame and NPX frame */
+    InitialStack &= ~(KTRAP_FRAME_ALIGN - 1);
+
+    /* Switch to new kernel stack and start kernel bootstrapping */
+    KiSetupStackAndInitializeKernel(&KiInitialProcess.Pcb,
+                                    InitialThread,
+                                    (PVOID)InitialStack,
+                                    (PKPRCB)__readfsdword(KPCR_PRCB),
+                                    (CCHAR)Cpu,
+                                    KeLoaderBlock);
+#endif
+}
Propchange: branches/ros-amd64-bringup/reactos/ntoskrnl/ke/amd64/kiinit.c
------------------------------------------------------------------------------
    svn:eol-style = native
Modified: branches/ros-amd64-bringup/reactos/ntoskrnl/ntoskrnl-generic.rbuild
URL:
http://svn.reactos.org/svn/reactos/branches/ros-amd64-bringup/reactos/ntosk…
==============================================================================
--- branches/ros-amd64-bringup/reactos/ntoskrnl/ntoskrnl-generic.rbuild [iso-8859-1]
(original)
+++ branches/ros-amd64-bringup/reactos/ntoskrnl/ntoskrnl-generic.rbuild [iso-8859-1] Thu
Jul 24 18:04:22 2008
@@ -87,7 +87,13 @@
                                <file>ctxhelp.S</file>
                        </directory>
                </if>
-               <!-- file>apc.c</file -->
+               <if property="ARCH" value="amd64">
+                       <directory name="amd64">
+                               <file first="true">boot.S</file>
+                               <file>kiinit.c</file>
+                       </directory>
+               </if>
+               <file>apc.c</file>
                <!-- file>balmgr.c</file -->
                <!-- file>bug.c</file -->
                <!-- file>clock.c</file -->
@@ -95,14 +101,14 @@
                <file>devqueue.c</file>
                <!-- file>dpc.c</file -->
                <file>eventobj.c</file>
-               <!-- file>except.c</file -->
-               <!-- file>freeldr.c</file -->
-               <!-- file>gate.c</file -->
-               <!-- file>gmutex.c</file -->
+               <file>except.c</file>
+               <file>freeldr.c</file>
+               <file>gate.c</file>
+               <file>gmutex.c</file>
                <file>ipi.c</file>
                <!-- file>krnlinit.c</file -->
-               <!-- file>mutex.c</file -->
-               <!-- file>procobj.c</file -->
+               <file>mutex.c</file>
+               <file>procobj.c</file>
                <!-- file>profobj.c</file -->
                <!-- file>queue.c</file -->
                <!-- file>semphobj.c</file -->
@@ -145,9 +151,9 @@
                <file>cmdata.c</file>
                <file>cmdelay.c</file>
                <file>cmindex.c</file>
-               <!-- file>cminit.c</file -->
+               <file>cminit.c</file>
                <file>cmhook.c</file>
-               <!-- file>cmkcbncb.c</file -->
+               <file>cmkcbncb.c</file>
                <file>cmkeydel.c</file>
                <file>cmlazy.c</file>
                <file>cmmapvw.c</file>
@@ -155,14 +161,14 @@
                <file>cmparse.c</file>
                <file>cmse.c</file>
                <file>cmsecach.c</file>
-               <!-- file>cmsysini.c</file -->
+               <file>cmsysini.c</file>
                <file>cmvalue.c</file>
                <file>cmvalche.c</file>
                <file>cmwraprs.c</file>
-               <!-- file>ntapi.c</file -->
+               <file>ntapi.c</file>
        </directory>
        <directory name="dbgk">
-               <!-- file>dbgkutil.c</file -->
+               <file>dbgkutil.c</file>
                <!-- file>dbgkobj.c</file -->
        </directory>
        <directory name="ex" root="intermediate">
@@ -181,30 +187,30 @@
                <file>dbgctrl.c</file>
                <file>efi.c</file>
                <!-- file>event.c</file -->
-               <!-- file>evtpair.c</file -->
+               <file>evtpair.c</file>
                <file>exintrin.c</file>
                <file>fastinterlck.c</file>
                <file>fmutex.c</file>
-               <!-- file>handle.c</file -->
-               <!-- file>harderr.c</file -->
+               <file>handle.c</file>
+               <file>harderr.c</file>
                <file>hdlsterm.c</file>
                <!-- file>init.c</file -->
                <file>keyedevt.c</file>
-               <!-- file>locale.c</file -->
+               <file>locale.c</file>
                <!-- file>lookas.c</file -->
                <!-- file>mutant.c</file -->
-               <!-- file>pushlock.c</file -->
-               <!-- file>profile.c</file -->
-               <!-- file>resource.c</file -->
+               <file>pushlock.c</file>
+               <file>profile.c</file>
+               <file>resource.c</file>
                <file>rundown.c</file>
                <!-- file>sem.c</file -->
                <file>shutdown.c</file>
                <!-- file>sysinfo.c</file -->
                <!-- file>time.c</file -->
                <!-- file>timer.c</file -->
-               <!-- file>uuid.c</file -->
+               <file>uuid.c</file>
                <file>win32k.c</file>
-               <!-- file>work.c</file -->
+               <file>work.c</file>
                <file>xipdisp.c</file>
                <file>zone.c</file>
        </directory>
@@ -244,11 +250,11 @@
                        <file>deviface.c</file>
                        <file>driver.c</file>
                        <file>drvrlist.c</file>
-                       <!-- file>error.c</file -->
+                       <file>error.c</file>
                        <!-- file>file.c</file -->
                        <!-- file>iocomp.c</file -->
                        <file>ioevent.c</file>
-                       <!-- file>iofunc.c</file -->
+                       <file>iofunc.c</file>
                        <file>iomdl.c</file>
                        <!-- file>iomgr.c</file -->
                        <file>iorsrce.c</file>
@@ -261,10 +267,10 @@
                        <file>remlock.c</file>
                        <!-- file>util.c</file -->
                        <file>symlink.c</file>
-                       <!-- file>volume.c</file -->
+                       <file>volume.c</file>
                </directory>
                <directory name="pnpmgr">
-                       <!-- file>plugplay.c</file -->
+                       <file>plugplay.c</file>
                        <file>pnpdma.c</file>
                        <file>pnpmgr.c</file>
                        <file>pnpnotify.c</file>
@@ -289,17 +295,17 @@
                        <if property="KDBG" value="1">
                                <!-- file>kdb.c</file -->
                                <!-- file>kdb_cli.c</file -->
-                               <!-- file>kdb_expr.c</file -->
+                               <file>kdb_expr.c</file>
                                <file>kdb_keyboard.c</file>
-                               <!-- file>kdb_serial.c</file -->
+                               <file>kdb_serial.c</file>
                        </if>
                        <if property="DBG_OR_KDBG" value="true">
-                               <!-- file>kdb_symbols.c</file -->
+                               <file>kdb_symbols.c</file>
                        </if>
                </directory>
                <directory name="kd">
                        <directory name="wrappers">
-                               <!-- file>bochs.c</file -->
+                               <file>bochs.c</file>
                                <if property="ARCH"
value="i386">
                                        <file>gdbstub.c</file>
                                </if>
@@ -326,13 +332,13 @@
        </if>
        <directory name="lpc">
                <file>close.c</file>
-               <!-- file>complete.c</file -->
-               <!-- file>connect.c</file -->
-               <!-- file>create.c</file -->
+               <file>complete.c</file>
+               <file>connect.c</file>
+               <file>create.c</file>
                <file>listen.c</file>
                <file>port.c</file>
-               <!-- file>reply.c</file -->
-               <!-- file>send.c</file -->
+               <file>reply.c</file>
+               <file>send.c</file>
        </directory>
        <directory name="mm">
                <if property="ARCH" value="i386">
@@ -365,8 +371,8 @@
                <!-- file>mminit.c</file -->
                <!-- file>mpw.c</file -->
                <!-- file>ncache.c</file -->
-               <!-- file>npool.c</file -->
-               <!-- file>pagefile.c</file -->
+               <file>npool.c</file>
+               <file>pagefile.c</file>
                <file>pageop.c</file>
                <file>pager.c</file>
                <file>pagfault.c</file>
@@ -374,12 +380,12 @@
                <file>pe.c</file>
                <file>physical.c</file>
                <!-- file>pool.c</file -->
-               <!-- file>ppool.c</file -->
+               <file>ppool.c</file>
                <!-- file>procsup.c</file -->
                <!-- file>region.c</file -->
                <file>rmap.c</file>
                <!-- file>section.c</file -->
-               <!-- file>sysldr.c</file -->
+               <file>sysldr.c</file>
                <file>verifier.c</file>
                <!-- file>virtual.c</file -->
                <file>wset.c</file>
@@ -391,18 +397,18 @@
        <directory name="ob">
                <!-- file>obdir.c</file -->
                <!-- file>obinit.c</file -->
-               <!-- file>obhandle.c</file -->
+               <file>obhandle.c</file>
                <file>obname.c</file>
                <file>oblife.c</file>
                <file>obref.c</file>
-               <!-- file>obsdcach.c</file -->
+               <file>obsdcach.c</file>
                <file>obsecure.c</file>
-               <!-- file>oblink.c</file -->
-               <!-- file>obwait.c</file -->
+               <file>oblink.c</file>
+               <file>obwait.c</file>
        </directory>
        <directory name="po">
                <file>power.c</file>
-               <!-- file>events.c</file -->
+               <file>events.c</file>
        </directory>
        <directory name="ps">
                <if property="ARCH" value="i386">
@@ -415,16 +421,16 @@
                                <file>psctx.c</file>
                        </directory>
                </if>
-               <!-- file>debug.c</file -->
-               <!-- file>job.c</file -->
-               <!-- file>kill.c</file -->
+               <file>debug.c</file>
+               <file>job.c</file>
+               <file>kill.c</file>
                <file>psnotify.c</file>
-               <!-- file>process.c</file -->
+               <file>process.c</file>
                <!-- file>psmgr.c</file -->
-               <!-- file>query.c</file -->
+               <file>query.c</file>
                <file>quota.c</file>
-               <!-- file>security.c</file -->
-               <!-- file>state.c</file -->
+               <file>security.c</file>
+               <file>state.c</file>
                <!-- file>thread.c</file -->
                <!-- file>win32.c</file -->
        </directory>
@@ -441,10 +447,10 @@
        <directory name="se">
                <file>access.c</file>
                <file>acl.c</file>
-               <!-- file>audit.c</file -->
-               <!-- file>lsa.c</file -->
+               <file>audit.c</file>
+               <file>lsa.c</file>
                <file>priv.c</file>
-               <!-- file>sd.c</file -->
+               <file>sd.c</file>
                <file>semgr.c</file>
                <file>sid.c</file>
                <!-- file>token.c</file -->
Modified: branches/ros-amd64-bringup/reactos/ntoskrnl/ntoskrnl.rbuild
URL:
http://svn.reactos.org/svn/reactos/branches/ros-amd64-bringup/reactos/ntosk…
==============================================================================
--- branches/ros-amd64-bringup/reactos/ntoskrnl/ntoskrnl.rbuild [iso-8859-1] (original)
+++ branches/ros-amd64-bringup/reactos/ntoskrnl/ntoskrnl.rbuild [iso-8859-1] Thu Jul 24
18:04:22 2008
@@ -1,7 +1,7 @@
 <?xml version="1.0"?>
 <!DOCTYPE module SYSTEM "../tools/rbuild/project.dtd">
 <group 
xmlns:xi="http://www.w3.org/2001/XInclude">
-       <module name="ntoskrnl" type="kernel"
installbase="system32" installname="ntoskrnl.exe">
+       <module name="ntoskrnl" type="kernel"
installbase="system32" installname="ntoskrnl.exe"
allowwarnings="true">
                <xi:include href="ntoskrnl-generic.rbuild" />
        </module>
 </group>