Merge trunk head (r43756)
[reactos.git] / reactos / ntoskrnl / rtl / libsupp.c
index d81a3cf..8ff65d5 100644 (file)
@@ -13,7 +13,7 @@
 #define NDEBUG
 #include <debug.h>
 
-#define TAG_ATMT TAG('A', 't', 'o', 'T') /* Atom table */
+#define TAG_ATMT 'TotA' /* Atom table */
 
 extern ULONG NtGlobalFlag;
 
@@ -28,6 +28,30 @@ SIZE_T RtlpAllocDeallocQueryBufferSize = 128;
 
 /* FUNCTIONS *****************************************************************/
 
+PVOID
+NTAPI
+RtlpLookupModuleBase(
+    PVOID Address)
+{
+    PLDR_DATA_TABLE_ENTRY LdrEntry;
+    BOOLEAN InSystem;
+    PVOID p;
+
+    /* Get the base for this file */
+    if ((ULONG_PTR)Address > (ULONG_PTR)MmHighestUserAddress)
+    {
+        /* We are in kernel */
+        p = KiPcToFileHeader(Address, &LdrEntry, FALSE, &InSystem);
+    }
+    else
+    {
+        /* We are in user land */
+        p = KiRosPcToUserFileHeader(Address, &LdrEntry);
+    }
+
+    return p;
+}
+
 VOID
 NTAPI
 RtlInitializeRangeListPackage(VOID)
@@ -38,16 +62,16 @@ RtlInitializeRangeListPackage(VOID)
                                    NULL,
                                    POOL_COLD_ALLOCATION,
                                    sizeof(RTL_RANGE_ENTRY),
-                                   TAG('R', 'R', 'l', 'e'),
+                                   'elRR',
                                    16);
 }
 
 BOOLEAN
 NTAPI
-RtlpCheckForActiveDebugger(BOOLEAN Type)
+RtlpCheckForActiveDebugger(VOID)
 {
     /* This check is meaningless in kernel-mode */
-    return Type;
+    return FALSE;
 }
 
 BOOLEAN
@@ -59,14 +83,14 @@ RtlpSetInDbgPrint(IN BOOLEAN NewValue)
 }
 
 KPROCESSOR_MODE
-STDCALL
+NTAPI
 RtlpGetMode()
 {
    return KernelMode;
 }
 
 PVOID
-STDCALL
+NTAPI
 RtlpAllocateMemory(ULONG Bytes,
                    ULONG Tag)
 {
@@ -76,11 +100,11 @@ RtlpAllocateMemory(ULONG Bytes,
 }
 
 
-#define TAG_USTR        TAG('U', 'S', 'T', 'R')
-#define TAG_ASTR        TAG('A', 'S', 'T', 'R')
-#define TAG_OSTR        TAG('O', 'S', 'T', 'R')
+#define TAG_USTR        'RTSU'
+#define TAG_ASTR        'RTSA'
+#define TAG_OSTR        'RTSO'
 VOID
-STDCALL
+NTAPI
 RtlpFreeMemory(PVOID Mem,
                ULONG Tag)
 {
@@ -93,7 +117,7 @@ RtlpFreeMemory(PVOID Mem,
 /*
  * @implemented
  */
-VOID STDCALL
+VOID NTAPI
 RtlAcquirePebLock(VOID)
 {
 
@@ -102,14 +126,14 @@ RtlAcquirePebLock(VOID)
 /*
  * @implemented
  */
-VOID STDCALL
+VOID NTAPI
 RtlReleasePebLock(VOID)
 {
 
 }
 
 NTSTATUS
-STDCALL
+NTAPI
 LdrShutdownThread(VOID)
 {
     return STATUS_SUCCESS;
@@ -117,14 +141,14 @@ LdrShutdownThread(VOID)
 
 
 PPEB
-STDCALL
+NTAPI
 RtlGetCurrentPeb(VOID)
 {
    return ((PEPROCESS)(KeGetCurrentThread()->ApcState.Process))->Peb;
 }
 
 NTSTATUS
-STDCALL
+NTAPI
 RtlDeleteHeapLock(
     PRTL_CRITICAL_SECTION CriticalSection)
 {
@@ -133,7 +157,7 @@ RtlDeleteHeapLock(
 }
 
 NTSTATUS
-STDCALL
+NTAPI
 RtlEnterHeapLock(
     PRTL_CRITICAL_SECTION CriticalSection)
 {
@@ -142,7 +166,7 @@ RtlEnterHeapLock(
 }
 
 NTSTATUS
-STDCALL
+NTAPI
 RtlInitializeHeapLock(
     PRTL_CRITICAL_SECTION CriticalSection)
 {
@@ -151,7 +175,7 @@ RtlInitializeHeapLock(
 }
 
 NTSTATUS
-STDCALL
+NTAPI
 RtlLeaveHeapLock(
     PRTL_CRITICAL_SECTION CriticalSection)
 {
@@ -159,7 +183,7 @@ RtlLeaveHeapLock(
     return STATUS_SUCCESS;
 }
 
-#ifdef DBG
+#if DBG
 VOID FASTCALL
 CHECK_PAGED_CODE_RTL(char *file, int line)
 {
@@ -313,7 +337,7 @@ RtlWalkFrameChain(OUT PVOID *Callers,
     }
 
     /* Use a SEH block for maximum protection */
-    _SEH_TRY
+    _SEH2_TRY
     {
         /* Check if we want the user-mode stack frame */
         if (Flags == 1)
@@ -341,6 +365,8 @@ RtlWalkFrameChain(OUT PVOID *Callers,
             Stack = TrapFrame->Ebp;
 #elif defined(_M_PPC)
             Stack = TrapFrame->Gpr1;
+#else
+#error Unknown architecture
 #endif
 
             /* Validate them */
@@ -398,12 +424,12 @@ RtlWalkFrameChain(OUT PVOID *Callers,
             Stack = NewStack;
         }
     }
-    _SEH_HANDLE
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
         /* No index */
         i = 0;
     }
-    _SEH_END;
+    _SEH2_END;
 
     /* Return frames parsed */
     return i;
@@ -411,6 +437,19 @@ RtlWalkFrameChain(OUT PVOID *Callers,
 
 #endif
 
+#ifdef _AMD64_
+VOID
+NTAPI
+RtlpGetStackLimits(
+    OUT PULONG_PTR LowLimit,
+    OUT PULONG_PTR HighLimit)
+{
+    PKTHREAD CurrentThread = KeGetCurrentThread();
+    *HighLimit = (ULONG_PTR)CurrentThread->InitialStack;
+    *LowLimit = (ULONG_PTR)CurrentThread->StackLimit;
+}
+#endif
+
 /* RTL Atom Tables ************************************************************/
 
 NTSTATUS
@@ -516,29 +555,36 @@ RtlpCreateAtomHandle(PRTL_ATOM_TABLE AtomTable, PRTL_ATOM_TABLE_ENTRY Entry)
    HANDLE Handle;
    USHORT HandleIndex;
 
+   /* Initialize ex handle table entry */
    ExEntry.Object = Entry;
    ExEntry.GrantedAccess = 0x1; /* FIXME - valid handle */
 
+   /* Create ex handle */
    Handle = ExCreateHandle(AtomTable->ExHandleTable,
-                                &ExEntry);
-   if (Handle != NULL)
+                           &ExEntry);
+   if (!Handle) return FALSE;
+
+   /* Calculate HandleIndex (by getting rid of the first two bits) */
+   HandleIndex = (USHORT)((ULONG_PTR)Handle >> 2);
+
+   /* Index must be less than 0xC000 */
+   if (HandleIndex >= 0xC000)
    {
-      HandleIndex = (USHORT)((ULONG_PTR)Handle >> 2);
-      /* FIXME - Handle Indexes >= 0xC000 ?! */
-      if ((ULONG_PTR)HandleIndex >> 2 < 0xC000)
-      {
-         Entry->HandleIndex = HandleIndex;
-         Entry->Atom = 0xC000 + HandleIndex;
-
-         return TRUE;
-      }
-      else
-         ExDestroyHandle(AtomTable->ExHandleTable,
-                         Handle,
-                         NULL);
+       /* Destroy ex handle */
+       ExDestroyHandle(AtomTable->ExHandleTable,
+                       Handle,
+                       NULL);
+
+       /* Return failure */
+       return FALSE;
    }
 
-   return FALSE;
+   /* Initialize atom table entry */
+   Entry->HandleIndex = HandleIndex;
+   Entry->Atom = 0xC000 + HandleIndex;
+
+   /* Return success */
+   return TRUE;
 }
 
 PRTL_ATOM_TABLE_ENTRY