#include <ntoskrnl.h>
#define NDEBUG
-#include <internal/debug.h>
+#include <debug.h>
+
+#define TAG_ATMT 'TotA' /* Atom table */
extern ULONG NtGlobalFlag;
/* FUNCTIONS *****************************************************************/
+PVOID
+NTAPI
+RtlPcToFileHeader(
+ IN PVOID PcValue,
+ OUT PVOID *BaseOfImage)
+{
+ PLDR_DATA_TABLE_ENTRY LdrEntry;
+ BOOLEAN InSystem;
+
+ /* Get the base for this file */
+ if ((ULONG_PTR)PcValue > (ULONG_PTR)MmHighestUserAddress)
+ {
+ /* We are in kernel */
+ *BaseOfImage = KiPcToFileHeader(PcValue, &LdrEntry, FALSE, &InSystem);
+ }
+ else
+ {
+ /* We are in user land */
+ *BaseOfImage = KiRosPcToUserFileHeader(PcValue, &LdrEntry);
+ }
+
+ return *BaseOfImage;
+}
+
VOID
NTAPI
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
NTAPI
-RtlpSetInDbgPrint(IN BOOLEAN NewValue)
+RtlpSetInDbgPrint(VOID)
{
- /* This check is meaningless in kernel-mode */
+ /* Nothing to set in kernel mode */
return FALSE;
}
+VOID
+NTAPI
+RtlpClearInDbgPrint(VOID)
+{
+ /* Nothing to clear in kernel mode */
+}
+
KPROCESSOR_MODE
-STDCALL
+NTAPI
RtlpGetMode()
{
return KernelMode;
}
PVOID
-STDCALL
+NTAPI
RtlpAllocateMemory(ULONG Bytes,
ULONG Tag)
{
}
+#define TAG_USTR 'RTSU'
+#define TAG_ASTR 'RTSA'
+#define TAG_OSTR 'RTSO'
VOID
-STDCALL
+NTAPI
RtlpFreeMemory(PVOID Mem,
ULONG Tag)
{
- ExFreePoolWithTag(Mem,
- Tag);
+ if (Tag == TAG_ASTR || Tag == TAG_OSTR || Tag == TAG_USTR)
+ ExFreePool(Mem);
+ else
+ ExFreePoolWithTag(Mem, Tag);
}
/*
* @implemented
*/
-VOID STDCALL
+VOID NTAPI
RtlAcquirePebLock(VOID)
{
/*
* @implemented
*/
-VOID STDCALL
+VOID NTAPI
RtlReleasePebLock(VOID)
{
}
NTSTATUS
-STDCALL
+NTAPI
LdrShutdownThread(VOID)
{
return STATUS_SUCCESS;
PPEB
-STDCALL
-RtlpCurrentPeb(VOID)
+NTAPI
+RtlGetCurrentPeb(VOID)
{
return ((PEPROCESS)(KeGetCurrentThread()->ApcState.Process))->Peb;
}
NTSTATUS
-STDCALL
+NTAPI
RtlDeleteHeapLock(
PRTL_CRITICAL_SECTION CriticalSection)
{
- KEBUGCHECK(0);
+ ASSERT(FALSE);
return STATUS_SUCCESS;
}
NTSTATUS
-STDCALL
+NTAPI
RtlEnterHeapLock(
PRTL_CRITICAL_SECTION CriticalSection)
{
- KEBUGCHECK(0);
+ ASSERT(FALSE);
return STATUS_SUCCESS;
}
NTSTATUS
-STDCALL
+NTAPI
RtlInitializeHeapLock(
PRTL_CRITICAL_SECTION CriticalSection)
{
- KEBUGCHECK(0);
+ ASSERT(FALSE);
return STATUS_SUCCESS;
}
NTSTATUS
-STDCALL
+NTAPI
RtlLeaveHeapLock(
PRTL_CRITICAL_SECTION CriticalSection)
{
- KEBUGCHECK(0);
+ ASSERT(FALSE);
return STATUS_SUCCESS;
}
-#ifdef DBG
+#if DBG
VOID FASTCALL
CHECK_PAGED_CODE_RTL(char *file, int line)
{
if(KeGetCurrentIrql() > APC_LEVEL)
{
DbgPrint("%s:%i: Pagable code called at IRQL > APC_LEVEL (%d)\n", file, line, KeGetCurrentIrql());
- KEBUGCHECK(0);
+ ASSERT(FALSE);
}
}
#endif
return FALSE;
}
-#ifndef _ARM_
+#if !defined(_ARM_) && !defined(_AMD64_)
BOOLEAN
NTAPI
ULONG Eip;
BOOLEAN Result, StopSearch = FALSE;
ULONG i = 0;
- PKTHREAD Thread = KeGetCurrentThread();
+ PETHREAD Thread = PsGetCurrentThread();
PTEB Teb;
PKTRAP_FRAME TrapFrame;
&StackBegin,
&StackEnd);
if (!Result) return 0;
- }
+ }
/* Use a SEH block for maximum protection */
- _SEH_TRY
+ _SEH2_TRY
{
/* Check if we want the user-mode stack frame */
if (Flags == 1)
{
/* Get the trap frame and TEB */
- TrapFrame = Thread->TrapFrame;
- Teb = Thread->Teb;
+ TrapFrame = KeGetTrapFrame(&Thread->Tcb);
+ Teb = Thread->Tcb.Teb;
/* Make sure we can trust the TEB and trap frame */
if (!(Teb) ||
- !((PVOID)((ULONG_PTR)TrapFrame & 0x80000000)) ||
- ((PVOID)TrapFrame <= (PVOID)Thread->StackLimit) ||
- ((PVOID)TrapFrame >= (PVOID)Thread->StackBase) ||
(KeIsAttachedProcess()) ||
(KeGetCurrentIrql() >= DISPATCH_LEVEL))
{
/* Invalid or unsafe attempt to get the stack */
- return 0;
+ _SEH2_YIELD(return 0;)
}
/* Get the stack limits */
- StackBegin = (ULONG_PTR)Teb->Tib.StackLimit;
- StackEnd = (ULONG_PTR)Teb->Tib.StackBase;
+ StackBegin = (ULONG_PTR)Teb->NtTib.StackLimit;
+ StackEnd = (ULONG_PTR)Teb->NtTib.StackBase;
#ifdef _M_IX86
Stack = TrapFrame->Ebp;
#elif defined(_M_PPC)
Stack = TrapFrame->Gpr1;
+#else
+#error Unknown architecture
#endif
/* Validate them */
if ((StackBegin < Eip) && (Eip < StackEnd)) break;
/* Check if we reached a user-mode address */
- if (!(Flags) && !(Eip & 0x80000000)) break;
+ if (!(Flags) && !(Eip & 0x80000000)) break; // FIXME: 3GB breakage
/* Save this frame */
Callers[i] = (PVOID)Eip;
Stack = NewStack;
}
}
- _SEH_HANDLE
+ _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{
/* No index */
i = 0;
}
- _SEH_END;
+ _SEH2_END;
/* Return frames parsed */
return i;
#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
PRTL_ATOM_TABLE_ENTRY
RtlpAllocAtomTableEntry(ULONG Size)
{
- PRTL_ATOM_TABLE_ENTRY Entry = ExAllocatePool(NonPagedPool,
- Size);
- if (Entry != NULL)
- {
- RtlZeroMemory(Entry,
- Size);
- }
+ PRTL_ATOM_TABLE_ENTRY Entry;
- return Entry;
+ Entry = ExAllocatePoolWithTag(NonPagedPool, Size, TAG_ATMT);
+ if (Entry != NULL)
+ {
+ RtlZeroMemory(Entry, Size);
+ }
+
+ return Entry;
}
VOID
RtlpFreeAtomTableEntry(PRTL_ATOM_TABLE_ENTRY Entry)
{
- ExFreePool(Entry);
+ ExFreePoolWithTag(Entry, TAG_ATMT);
}
VOID
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
return Entry;
}
-/* FIXME - RtlpCreateUnicodeString is obsolete and should be removed ASAP! */
-BOOLEAN FASTCALL
-RtlpCreateUnicodeString(
- IN OUT PUNICODE_STRING UniDest,
- IN PCWSTR Source,
- IN POOL_TYPE PoolType)
-{
- ULONG Length;
-
- Length = (wcslen (Source) + 1) * sizeof(WCHAR);
- UniDest->Buffer = ExAllocatePoolWithTag(PoolType, Length, TAG('U', 'S', 'T', 'R'));
- if (UniDest->Buffer == NULL)
- return FALSE;
-
- RtlCopyMemory (UniDest->Buffer,
- Source,
- Length);
-
- UniDest->MaximumLength = (USHORT)Length;
- UniDest->Length = (USHORT)Length - sizeof (WCHAR);
-
- return TRUE;
-}
-
/*
* Ldr Resource support code
*/