[NTOSKRNL] Handle some more KeFeatureFlags in amd64/cpu.c and set RtlpUse16ByteSLists
[reactos.git] / reactos / dll / ntdll / ldr / ldrinit.c
index 5e22253..b6422b1 100644 (file)
@@ -10,7 +10,6 @@
 /* INCLUDES *****************************************************************/
 
 #include <ntdll.h>
-#include <callback.h>
 
 #define NDEBUG
 #include <debug.h>
@@ -88,6 +87,7 @@ ULONG RtlpShutdownProcessFlags; // TODO: Use it
 
 NTSTATUS LdrPerformRelocations(PIMAGE_NT_HEADERS NTHeaders, PVOID ImageBase);
 void actctx_init(void);
+extern BOOLEAN RtlpUse16ByteSLists;
 
 #ifdef _WIN64
 #define DEFAULT_SECURITY_COOKIE 0x00002B992DDFA232ll
@@ -501,7 +501,7 @@ LdrpInitializeThread(IN PCONTEXT Context)
     NTSTATUS Status;
     PVOID EntryPoint;
 
-    DPRINT("LdrpInitializeThread() called for %wZ (%lx/%lx)\n",
+    DPRINT("LdrpInitializeThread() called for %wZ (%p/%p)\n",
             &LdrpImageEntry->BaseDllName,
             NtCurrentTeb()->RealClientId.UniqueProcess,
             NtCurrentTeb()->RealClientId.UniqueThread);
@@ -566,7 +566,7 @@ LdrpInitializeThread(IN PCONTEXT Context)
                     if (!LdrpShutdownInProgress)
                     {
                         /* Call the Entrypoint */
-                        DPRINT("%wZ - Calling entry point at %p for thread attaching, %lx/%lx\n",
+                        DPRINT("%wZ - Calling entry point at %p for thread attaching, %p/%p\n",
                                 &LdrEntry->BaseDllName, LdrEntry->EntryPoint,
                                 NtCurrentTeb()->RealClientId.UniqueProcess,
                                 NtCurrentTeb()->RealClientId.UniqueThread);
@@ -626,7 +626,7 @@ LdrpRunInitializeRoutines(IN PCONTEXT Context OPTIONAL)
     PTEB OldTldTeb;
     BOOLEAN DllStatus;
 
-    DPRINT("LdrpRunInitializeRoutines() called for %wZ (%lx/%lx)\n",
+    DPRINT("LdrpRunInitializeRoutines() called for %wZ (%p/%p)\n",
         &LdrpImageEntry->BaseDllName,
         NtCurrentTeb()->RealClientId.UniqueProcess,
         NtCurrentTeb()->RealClientId.UniqueThread);
@@ -661,7 +661,7 @@ LdrpRunInitializeRoutines(IN PCONTEXT Context OPTIONAL)
     /* Show debug message */
     if (ShowSnaps)
     {
-        DPRINT1("[%x,%x] LDR: Real INIT LIST for Process %wZ\n",
+        DPRINT1("[%p,%p] LDR: Real INIT LIST for Process %wZ\n",
                 NtCurrentTeb()->RealClientId.UniqueThread,
                 NtCurrentTeb()->RealClientId.UniqueProcess,
                 &Peb->ProcessParameters->ImagePathName);
@@ -695,7 +695,7 @@ LdrpRunInitializeRoutines(IN PCONTEXT Context OPTIONAL)
                     /* Display debug message */
                     if (ShowSnaps)
                     {
-                        DPRINT1("[%x,%x] LDR: %wZ init routine %p\n",
+                        DPRINT1("[%p,%p] LDR: %wZ init routine %p\n",
                                 NtCurrentTeb()->RealClientId.UniqueThread,
                                 NtCurrentTeb()->RealClientId.UniqueProcess,
                                 &LdrEntry->FullDllName,
@@ -963,7 +963,7 @@ LdrShutdownProcess(VOID)
                 }
 
                 /* Call the Entrypoint */
-                DPRINT("%wZ - Calling entry point at %x for thread detaching\n",
+                DPRINT("%wZ - Calling entry point at %p for thread detaching\n",
                         &LdrEntry->BaseDllName, LdrEntry->EntryPoint);
                 LdrpCallInitRoutine(EntryPoint,
                                  LdrEntry->DllBase,
@@ -1078,7 +1078,7 @@ LdrShutdownThread(VOID)
                     if (!LdrpShutdownInProgress)
                     {
                         /* Call the Entrypoint */
-                        DPRINT("%wZ - Calling entry point at %x for thread detaching\n",
+                        DPRINT("%wZ - Calling entry point at %p for thread detaching\n",
                                 &LdrEntry->BaseDllName, LdrEntry->EntryPoint);
                         LdrpCallInitRoutine(EntryPoint,
                                          LdrEntry->DllBase,
@@ -1253,7 +1253,7 @@ LdrpAllocateTls(VOID)
         /* Show debug message */
         if (ShowSnaps)
         {
-            DPRINT1("LDR: TlsVector %x Index %d = %x copied from %x to %x\n",
+            DPRINT1("LDR: TlsVector %p Index %lu = %p copied from %x to %p\n",
                     TlsVector,
                     TlsData->TlsDirectory.Characteristics,
                     &TlsVector[TlsData->TlsDirectory.Characteristics],
@@ -1605,7 +1605,7 @@ LdrpInitializeProcess(IN PCONTEXT Context,
     /* Start verbose debugging messages right now if they were requested */
     if (ShowSnaps)
     {
-        DPRINT1("LDR: PID: 0x%x started - '%wZ'\n",
+        DPRINT1("LDR: PID: 0x%p started - '%wZ'\n",
                 Teb->ClientId.UniqueProcess,
                 &CommandLine);
     }
@@ -1696,18 +1696,6 @@ LdrpInitializeProcess(IN PCONTEXT Context,
         return STATUS_NO_MEMORY;
     }
 
-    // FIXME: Is it located properly?
-    /* Initialize table of callbacks for the kernel. */
-    Peb->KernelCallbackTable = RtlAllocateHeap(RtlGetProcessHeap(),
-                                               0,
-                                               sizeof(PVOID) *
-                                                (USER32_CALLBACK_MAXIMUM + 1));
-    if (!Peb->KernelCallbackTable)
-    {
-        DPRINT1("Failed to create callback table\n");
-        ZwTerminateProcess(NtCurrentProcess(), STATUS_INSUFFICIENT_RESOURCES);
-    }
-
     /* Allocate an Activation Context Stack */
     Status = RtlAllocateActivationContextStack(&Teb->ActivationContextStackPointer);
     if (!NT_SUCCESS(Status)) return Status;
@@ -1788,7 +1776,7 @@ LdrpInitializeProcess(IN PCONTEXT Context,
     /* Check if we failed */
     if (!NT_SUCCESS(Status))
     {
-        /* Aassume System32 */
+        /* Assume System32 */
         LdrpKnownDllObjectDirectory = NULL;
         RtlInitUnicodeString(&LdrpKnownDllPath, StringBuffer);
         LdrpKnownDllPath.Length -= sizeof(WCHAR);
@@ -1856,24 +1844,24 @@ LdrpInitializeProcess(IN PCONTEXT Context,
     PebLdr.Initialized = TRUE;
 
     /* Allocate a data entry for the Image */
-    LdrpImageEntry = NtLdrEntry = LdrpAllocateDataTableEntry(Peb->ImageBaseAddress);
+    LdrpImageEntry = LdrpAllocateDataTableEntry(Peb->ImageBaseAddress);
 
     /* Set it up */
-    NtLdrEntry->EntryPoint = LdrpFetchAddressOfEntryPoint(NtLdrEntry->DllBase);
-    NtLdrEntry->LoadCount = -1;
-    NtLdrEntry->EntryPointActivationContext = 0;
-    NtLdrEntry->FullDllName = ImageFileName;
+    LdrpImageEntry->EntryPoint = LdrpFetchAddressOfEntryPoint(LdrpImageEntry->DllBase);
+    LdrpImageEntry->LoadCount = -1;
+    LdrpImageEntry->EntryPointActivationContext = 0;
+    LdrpImageEntry->FullDllName = ImageFileName;
 
     if (IsDotNetImage)
-        NtLdrEntry->Flags = LDRP_COR_IMAGE;
+        LdrpImageEntry->Flags = LDRP_COR_IMAGE;
     else
-        NtLdrEntry->Flags = 0;
+        LdrpImageEntry->Flags = 0;
 
     /* Check if the name is empty */
     if (!ImageFileName.Buffer[0])
     {
         /* Use the same Base name */
-        NtLdrEntry->BaseDllName = NtLdrEntry->FullDllName;
+        LdrpImageEntry->BaseDllName = LdrpImageEntry->FullDllName;
     }
     else
     {
@@ -1892,21 +1880,21 @@ LdrpInitializeProcess(IN PCONTEXT Context,
         if (!NtDllName)
         {
             /* Use the same Base name */
-            NtLdrEntry->BaseDllName = NtLdrEntry->FullDllName;
+            LdrpImageEntry->BaseDllName = LdrpImageEntry->FullDllName;
         }
         else
         {
             /* Setup the name */
-            NtLdrEntry->BaseDllName.Length = (USHORT)((ULONG_PTR)ImageFileName.Buffer + ImageFileName.Length - (ULONG_PTR)NtDllName);
-            NtLdrEntry->BaseDllName.MaximumLength = NtLdrEntry->BaseDllName.Length + sizeof(WCHAR);
-            NtLdrEntry->BaseDllName.Buffer = (PWSTR)((ULONG_PTR)ImageFileName.Buffer +
-                                                     (ImageFileName.Length - NtLdrEntry->BaseDllName.Length));
+            LdrpImageEntry->BaseDllName.Length = (USHORT)((ULONG_PTR)ImageFileName.Buffer + ImageFileName.Length - (ULONG_PTR)NtDllName);
+            LdrpImageEntry->BaseDllName.MaximumLength = LdrpImageEntry->BaseDllName.Length + sizeof(WCHAR);
+            LdrpImageEntry->BaseDllName.Buffer = (PWSTR)((ULONG_PTR)ImageFileName.Buffer +
+                                                     (ImageFileName.Length - LdrpImageEntry->BaseDllName.Length));
         }
     }
 
     /* Processing done, insert it */
-    LdrpInsertMemoryTableEntry(NtLdrEntry);
-    NtLdrEntry->Flags |= LDRP_ENTRY_PROCESSED;
+    LdrpInsertMemoryTableEntry(LdrpImageEntry);
+    LdrpImageEntry->Flags |= LDRP_ENTRY_PROCESSED;
 
     /* Now add an entry for NTDLL */
     NtLdrEntry = LdrpAllocateDataTableEntry(SystemArgument1);
@@ -2083,6 +2071,40 @@ LdrpInitializeProcess(IN PCONTEXT Context,
                             &ExecuteOptions,
                             sizeof(ULONG));
 
+    // FIXME: Should be done by Application Compatibility features,
+    // by reading the registry, etc...
+    // For now, this is the old code from ntdll!RtlGetVersion().
+    RtlInitEmptyUnicodeString(&Peb->CSDVersion, NULL, 0);
+    if (((Peb->OSCSDVersion >> 8) & 0xFF) != 0)
+    {
+        WCHAR szCSDVersion[128];
+        LONG i;
+        ULONG Length = ARRAYSIZE(szCSDVersion) - 1;
+        i = _snwprintf(szCSDVersion, Length,
+                       L"Service Pack %d",
+                       ((Peb->OSCSDVersion >> 8) & 0xFF));
+        if (i < 0)
+        {
+            /* Null-terminate if it was overflowed */
+            szCSDVersion[Length] = UNICODE_NULL;
+        }
+
+        Length *= sizeof(WCHAR);
+        Peb->CSDVersion.Buffer = RtlAllocateHeap(RtlGetProcessHeap(),
+                                                 0,
+                                                 Length + sizeof(UNICODE_NULL));
+        if (Peb->CSDVersion.Buffer)
+        {
+            Peb->CSDVersion.Length = Length;
+            Peb->CSDVersion.MaximumLength = Length + sizeof(UNICODE_NULL);
+
+            RtlCopyMemory(Peb->CSDVersion.Buffer,
+                          szCSDVersion,
+                          Peb->CSDVersion.MaximumLength);
+            Peb->CSDVersion.Buffer[Peb->CSDVersion.Length / sizeof(WCHAR)] = UNICODE_NULL;
+        }
+    }
+
     /* Check if we had Shim Data */
     if (OldShimData)
     {
@@ -2158,10 +2180,15 @@ LdrpInit(PCONTEXT Context,
     MEMORY_BASIC_INFORMATION MemoryBasicInfo;
     PPEB Peb = NtCurrentPeb();
 
-    DPRINT("LdrpInit() %lx/%lx\n",
+    DPRINT("LdrpInit() %p/%p\n",
         NtCurrentTeb()->RealClientId.UniqueProcess,
         NtCurrentTeb()->RealClientId.UniqueThread);
 
+#ifdef _WIN64
+    /* Set the SList header usage */
+    RtlpUse16ByteSLists = SharedUserData->ProcessorFeatures[PF_COMPARE_EXCHANGE128];
+#endif /* _WIN64 */
+
     /* Check if we have a deallocation stack */
     if (!Teb->DeallocationStack)
     {