[NTOSKRNL/USERINIT/WIN32K]
[reactos.git] / reactos / ntoskrnl / ex / init.c
index f1feaad..d197e9d 100644 (file)
 #include <ntoskrnl.h>
 #define NDEBUG
 #include <debug.h>
-#include "ntstrsafe.h"
+
+/* Temporary hack */
+BOOLEAN
+NTAPI
+MmArmInitSystem(
+    IN ULONG Phase,
+    IN PLOADER_PARAMETER_BLOCK LoaderBlock
+);
 
 typedef struct _INIT_BUFFER
 {
@@ -36,7 +43,7 @@ ULONG NtBuildNumber = VER_PRODUCTBUILD;
 #endif
 
 /* NT System Info */
-ULONG NtGlobalFlag;
+ULONG NtGlobalFlag = 0;
 ULONG ExSuiteMask;
 
 /* Cm Version Info */
@@ -79,6 +86,7 @@ BOOLEAN ExpRealTimeIsUniversal;
 
 NTSTATUS
 NTAPI
+INIT_FUNCTION
 ExpCreateSystemRootLink(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 {
     UNICODE_STRING LinkName;
@@ -190,6 +198,7 @@ ExpCreateSystemRootLink(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 
 VOID
 NTAPI
+INIT_FUNCTION
 ExpInitNls(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 {
     LARGE_INTEGER SectionSize;
@@ -235,7 +244,7 @@ ExpInitNls(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
         /* Allocate the a new buffer since loader memory will be freed */
         ExpNlsTableBase = ExAllocatePoolWithTag(NonPagedPool,
                                                 ExpNlsTableSize,
-                                                'iltR');
+                                                TAG_RTLI);
         if (!ExpNlsTableBase) KeBugCheck(PHASE0_INITIALIZATION_FAILED);
 
         /* Copy the codepage data in its new location. */
@@ -303,7 +312,7 @@ ExpInitNls(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
                                        KernelMode,
                                        &ExpNlsSectionPointer,
                                        NULL);
-    ZwClose(NlsSection);
+    ObCloseHandle(NlsSection, KernelMode);
     if (!NT_SUCCESS(Status))
     {
         /* Failed */
@@ -321,10 +330,11 @@ ExpInitNls(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
     }
 
     /* Copy the codepage data in its new location. */
+    ASSERT(SectionBase > MmSystemRangeStart);
     RtlCopyMemory(SectionBase, ExpNlsTableBase, ExpNlsTableSize);
 
     /* Free the previously allocated buffer and set the new location */
-    ExFreePoolWithTag(ExpNlsTableBase, 'iltR');
+    ExFreePoolWithTag(ExpNlsTableBase, TAG_RTLI);
     ExpNlsTableBase = SectionBase;
 
     /* Initialize the NLS Tables */
@@ -364,6 +374,7 @@ ExpInitNls(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 
 VOID
 NTAPI
+INIT_FUNCTION
 ExpLoadInitialProcess(IN PINIT_BUFFER InitBuffer,
                       OUT PRTL_USER_PROCESS_PARAMETERS *ProcessParameters,
                       OUT PCHAR *ProcessEnvironment)
@@ -584,6 +595,7 @@ ExpLoadInitialProcess(IN PINIT_BUFFER InitBuffer,
 
 ULONG
 NTAPI
+INIT_FUNCTION
 ExComputeTickCountMultiplier(IN ULONG ClockIncrement)
 {
     ULONG MsRemainder = 0, MsIncrement;
@@ -616,6 +628,7 @@ ExComputeTickCountMultiplier(IN ULONG ClockIncrement)
 
 BOOLEAN
 NTAPI
+INIT_FUNCTION
 ExpInitSystemPhase0(VOID)
 {
     /* Initialize EXRESOURCE Support */
@@ -638,6 +651,7 @@ ExpInitSystemPhase0(VOID)
 
 BOOLEAN
 NTAPI
+INIT_FUNCTION
 ExpInitSystemPhase1(VOID)
 {
     /* Initialize worker threads */
@@ -675,6 +689,7 @@ ExpInitSystemPhase1(VOID)
 
 BOOLEAN
 NTAPI
+INIT_FUNCTION
 ExInitSystem(VOID)
 {
     /* Check the initialization phase */
@@ -700,6 +715,7 @@ ExInitSystem(VOID)
 
 BOOLEAN
 NTAPI
+INIT_FUNCTION
 ExpIsLoaderValid(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 {
     PLOADER_PARAMETER_EXTENSION Extension;
@@ -725,6 +741,7 @@ ExpIsLoaderValid(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 
 VOID
 NTAPI
+INIT_FUNCTION
 ExpLoadBootSymbols(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 {
     ULONG i = 0;
@@ -817,6 +834,53 @@ ExpLoadBootSymbols(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 
 VOID
 NTAPI
+INIT_FUNCTION
+ExBurnMemory(IN PLOADER_PARAMETER_BLOCK LoaderBlock,
+             IN ULONG PagesToDestroy,
+             IN TYPE_OF_MEMORY MemoryType)
+{
+    PLIST_ENTRY ListEntry;
+    PMEMORY_ALLOCATION_DESCRIPTOR MemDescriptor;
+
+    DPRINT1("Burn RAM amount: %d pages\n", PagesToDestroy);
+
+    /* Loop the memory descriptors, beginning at the end */
+    for (ListEntry = LoaderBlock->MemoryDescriptorListHead.Blink;
+         ListEntry != &LoaderBlock->MemoryDescriptorListHead;
+         ListEntry = ListEntry->Blink)
+    {
+        /* Get the memory descriptor structure */
+        MemDescriptor = CONTAINING_RECORD(ListEntry,
+                                          MEMORY_ALLOCATION_DESCRIPTOR,
+                                          ListEntry);
+
+        /* Is memory free there or is it temporary? */
+        if (MemDescriptor->MemoryType == LoaderFree ||
+            MemDescriptor->MemoryType == LoaderFirmwareTemporary)
+        {
+            /* Check if the descriptor has more pages than we want */
+            if (MemDescriptor->PageCount > PagesToDestroy)
+            {
+                /* Change block's page count, ntoskrnl doesn't care much */
+                MemDescriptor->PageCount -= PagesToDestroy;
+                break;
+            }
+            else
+            {
+                /* Change block type */
+                MemDescriptor->MemoryType = MemoryType;
+                PagesToDestroy -= MemDescriptor->PageCount;
+
+                /* Check if we are done */
+                if (PagesToDestroy == 0) break;
+            }
+        }
+    }
+}
+
+VOID
+NTAPI
+INIT_FUNCTION
 ExpInitializeExecutive(IN ULONG Cpu,
                        IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 {
@@ -827,9 +891,9 @@ ExpInitializeExecutive(IN ULONG Cpu,
     PCHAR CommandLine, PerfMem;
     ULONG PerfMemUsed;
     PLDR_DATA_TABLE_ENTRY NtosEntry;
-    PRTL_MESSAGE_RESOURCE_ENTRY MsgEntry;
+    PMESSAGE_RESOURCE_ENTRY MsgEntry;
     ANSI_STRING CsdString;
-    SIZE_T Remaining = 0;
+    size_t Remaining = 0;
     PCHAR RcEnd = NULL;
     CHAR VersionBuffer [65];
 
@@ -919,12 +983,7 @@ ExpInitializeExecutive(IN ULONG Cpu,
             {
                 /* Read the number of pages we'll use */
                 PerfMemUsed = atol(PerfMem + 1) * (1024 * 1024 / PAGE_SIZE);
-                if (PerfMem)
-                {
-                    /* FIXME: TODO */
-                    DPRINT1("Burnable memory support not yet present."
-                            "/BURNMEM option ignored.\n");
-                }
+                if (PerfMemUsed) ExBurnMemory(LoaderBlock, PerfMemUsed, LoaderBad);
             }
         }
     }
@@ -1001,11 +1060,14 @@ ExpInitializeExecutive(IN ULONG Cpu,
         if (CmNtCSDReleaseType == 1) CmNtSpBuildNumber |= 1830 << 16;
     }
 
+    /* Add loaded CmNtGlobalFlag value */
+    NtGlobalFlag |= CmNtGlobalFlag;
+
     /* Initialize the executive at phase 0 */
     if (!ExInitSystem()) KeBugCheck(PHASE0_INITIALIZATION_FAILED);
 
     /* Initialize the memory manager at phase 0 */
-    if (!MmInitSystem(0, LoaderBlock)) KeBugCheck(PHASE0_INITIALIZATION_FAILED);
+    if (!MmArmInitSystem(0, LoaderBlock)) KeBugCheck(PHASE0_INITIALIZATION_FAILED);
 
     /* Load boot symbols */
     ExpLoadBootSymbols(LoaderBlock);
@@ -1044,7 +1106,7 @@ ExpInitializeExecutive(IN ULONG Cpu,
         if (NT_SUCCESS(Status))
         {
             /* Setup the string */
-            RtlInitAnsiString(&CsdString, MsgEntry->Text);
+            RtlInitAnsiString(&CsdString, (PCHAR)MsgEntry->Text);
 
             /* Remove trailing newline */
             while ((CsdString.Length > 0) &&
@@ -1226,12 +1288,13 @@ ExpInitializeExecutive(IN ULONG Cpu,
     SharedUserData->NtMinorVersion = NtMinorVersion;
 
     /* Set the machine type */
-    SharedUserData->ImageNumberLow = IMAGE_FILE_MACHINE_ARCHITECTURE;
-    SharedUserData->ImageNumberHigh = IMAGE_FILE_MACHINE_ARCHITECTURE;
+    SharedUserData->ImageNumberLow = IMAGE_FILE_MACHINE_NATIVE;
+    SharedUserData->ImageNumberHigh = IMAGE_FILE_MACHINE_NATIVE;
 }
 
 VOID
 NTAPI
+INIT_FUNCTION
 Phase1InitializationDiscard(IN PVOID Context)
 {
     PLOADER_PARAMETER_BLOCK LoaderBlock = Context;
@@ -1240,13 +1303,14 @@ Phase1InitializationDiscard(IN PVOID Context)
     LARGE_INTEGER SystemBootTime, UniversalBootTime, OldTime, Timeout;
     BOOLEAN SosEnabled, NoGuiBoot, ResetBias = FALSE, AlternateShell = FALSE;
     PLDR_DATA_TABLE_ENTRY NtosEntry;
-    PRTL_MESSAGE_RESOURCE_ENTRY MsgEntry;
+    PMESSAGE_RESOURCE_ENTRY MsgEntry;
     PCHAR CommandLine, Y2KHackRequired, SafeBoot, Environment;
     PCHAR StringBuffer, EndBuffer, BeginBuffer, MpString = "";
     PINIT_BUFFER InitBuffer;
     ANSI_STRING TempString;
     ULONG LastTzBias, Length, YearHack = 0, Disposition, MessageCode = 0;
     SIZE_T Size;
+    size_t Remaining;
     PRTL_USER_PROCESS_INFORMATION ProcessInfo;
     KEY_VALUE_PARTIAL_INFORMATION KeyPartialInfo;
     UNICODE_STRING KeyName, DebugString;
@@ -1257,7 +1321,7 @@ Phase1InitializationDiscard(IN PVOID Context)
     /* Allocate the initialization buffer */
     InitBuffer = ExAllocatePoolWithTag(NonPagedPool,
                                        sizeof(INIT_BUFFER),
-                                       'tinI');
+                                       TAG_INIT);
     if (!InitBuffer)
     {
         /* Bugcheck */
@@ -1326,14 +1390,14 @@ Phase1InitializationDiscard(IN PVOID Context)
     StringBuffer = InitBuffer->VersionBuffer;
     BeginBuffer = StringBuffer;
     EndBuffer = StringBuffer;
-    Size = 256;
+    Remaining = sizeof(InitBuffer->VersionBuffer);
     if (CmCSDVersionString.Length)
     {
         /* Print the version string */
         Status = RtlStringCbPrintfExA(StringBuffer,
-                                      255,
+                                      Remaining,
                                       &EndBuffer,
-                                      &Size,
+                                      &Remaining,
                                       0,
                                       ": %wZ",
                                       &CmCSDVersionString);
@@ -1346,16 +1410,17 @@ Phase1InitializationDiscard(IN PVOID Context)
     else
     {
         /* No version */
-        Size = 255;
+        *EndBuffer = ANSI_NULL; /* Null-terminate the string */
     }
 
-    /* Null-terminate the string */
-    *EndBuffer++ = ANSI_NULL;
+    /* Skip over the null-terminator to start a new string */
+    ++EndBuffer;
+    --Remaining;
 
     /* Build the version number */
     StringBuffer = InitBuffer->VersionNumber;
     Status = RtlStringCbPrintfA(StringBuffer,
-                                24,
+                                sizeof(InitBuffer->VersionNumber),
                                 "%u.%u",
                                 VER_PRODUCTMAJORVERSION,
                                 VER_PRODUCTMINORVERSION);
@@ -1370,8 +1435,8 @@ Phase1InitializationDiscard(IN PVOID Context)
     {
         /* Create the banner message */
         Status = RtlStringCbPrintfA(EndBuffer,
-                                    Size,
-                                    MsgEntry->Text,
+                                    Remaining,
+                                    (PCHAR)MsgEntry->Text,
                                     StringBuffer,
                                     NtBuildNumber & 0xFFFF,
                                     BeginBuffer);
@@ -1384,7 +1449,7 @@ Phase1InitializationDiscard(IN PVOID Context)
     else
     {
         /* Use hard-coded banner message */
-        Status = RtlStringCbCopyA(EndBuffer, Size, "REACTOS (R)\n");
+        Status = RtlStringCbCopyA(EndBuffer, Remaining, "REACTOS (R)\n");
         if (!NT_SUCCESS(Status))
         {
             /* Bugcheck */
@@ -1484,9 +1549,9 @@ Phase1InitializationDiscard(IN PVOID Context)
     /* Create the string */
     StringBuffer = InitBuffer->VersionBuffer;
     Status = RtlStringCbPrintfA(StringBuffer,
-                                256,
+                                sizeof(InitBuffer->VersionBuffer),
                                 NT_SUCCESS(MsgStatus) ?
-                                MsgEntry->Text :
+                                (PCHAR)MsgEntry->Text :
                                 "%u System Processor [%u MB Memory] %Z\n",
                                 KeNumberProcessors,
                                 Size,
@@ -1537,7 +1602,7 @@ Phase1InitializationDiscard(IN PVOID Context)
     if (!MmInitSystem(1, LoaderBlock)) KeBugCheck(MEMORY1_INITIALIZATION_FAILED);
 
     /* Create NLS section */
-    ExpInitNls(KeLoaderBlock);
+    ExpInitNls(LoaderBlock);
 
     /* Initialize Cache Views */
     if (!CcInitializeCacheManager()) KeBugCheck(CACHE_INITIALIZATION_FAILED);
@@ -1612,7 +1677,7 @@ Phase1InitializationDiscard(IN PVOID Context)
             else if (!strncmp(SafeBoot, "NETWORK", 7))
             {
                 /* With Networking */
-                InitSafeBootMode = 1;
+                InitSafeBootMode = 2;
                 SafeBoot += 7;
                 MessageCode = BOOTING_IN_SAFEMODE_NETWORK;
             }
@@ -1650,7 +1715,7 @@ Phase1InitializationDiscard(IN PVOID Context)
             if (NT_SUCCESS(Status))
             {
                 /* Display it */
-                InbvDisplayString(MsgEntry->Text);
+                InbvDisplayString((PCHAR)MsgEntry->Text);
             }
         }
     }
@@ -1670,7 +1735,7 @@ Phase1InitializationDiscard(IN PVOID Context)
             if (NT_SUCCESS(Status))
             {
                 /* Display it */
-                InbvDisplayString(MsgEntry->Text);
+                InbvDisplayString((PCHAR)MsgEntry->Text);
             }
 
             /* Setup boot logging */
@@ -1726,8 +1791,11 @@ Phase1InitializationDiscard(IN PVOID Context)
                                          KeyValuePartialInformation,
                                          &KeyPartialInfo,
                                          sizeof(KeyPartialInfo),
-                                         &Size);
-                if (!NT_SUCCESS(Status)) AlternateShell = FALSE;
+                                         &Length);
+                if (!(NT_SUCCESS(Status) || Status == STATUS_BUFFER_OVERFLOW))
+                {
+                    AlternateShell = FALSE;
+                }
             }
 
             /* Create the option key */
@@ -1822,8 +1890,8 @@ Phase1InitializationDiscard(IN PVOID Context)
         NtClose(OptionHandle);
     }
 
-    /* Unmap Low memory, and initialize the MPW and Balancer Thread */
-    MmInitSystem(2, LoaderBlock);
+    /* FIXME: This doesn't do anything for now */
+    MmArmInitSystem(2, LoaderBlock);
 
     /* Update progress bar */
     InbvUpdateProgressBar(80);
@@ -1850,6 +1918,7 @@ Phase1InitializationDiscard(IN PVOID Context)
     InbvUpdateProgressBar(90);
 
     /* Launch initial process */
+    DPRINT1("Free non-cache pages: %lx\n", MmAvailablePages + MiMemoryConsumers[MC_CACHE].PagesUsed);
     ProcessInfo = &InitBuffer->ProcessInfo;
     ExpLoadInitialProcess(InitBuffer, &ProcessParameters, &Environment);
 
@@ -1878,24 +1947,25 @@ Phase1InitializationDiscard(IN PVOID Context)
     ZwClose(ProcessInfo->ProcessHandle);
 
     /* Free the initial process environment */
-    Length = 0;
+    Size = 0;
     ZwFreeVirtualMemory(NtCurrentProcess(),
                         (PVOID*)&Environment,
-                        &Length,
+                        &Size,
                         MEM_RELEASE);
 
     /* Free the initial process parameters */
-    Length = 0;
+    Size = 0;
     ZwFreeVirtualMemory(NtCurrentProcess(),
                         (PVOID*)&ProcessParameters,
-                        &Length,
+                        &Size,
                         MEM_RELEASE);
 
     /* Increase init phase */
     ExpInitializationPhase++;
 
     /* Free the boot buffer */
-    ExFreePool(InitBuffer);
+    ExFreePoolWithTag(InitBuffer, TAG_INIT);
+    DPRINT1("Free non-cache pages: %lx\n", MmAvailablePages + MiMemoryConsumers[MC_CACHE].PagesUsed);
 }
 
 VOID
@@ -1906,5 +1976,5 @@ Phase1Initialization(IN PVOID Context)
     Phase1InitializationDiscard(Context);
 
     /* Jump into zero page thread */
-    MmZeroPageThreadMain(NULL);
+    MmZeroPageThread();
 }