1999-08-01 11:21:05 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS kernel
|
|
|
|
* PURPOSE: GDI Driver Memory Management Functions
|
2015-11-10 17:41:55 +00:00
|
|
|
* FILE: win32ss/gdi/eng/mem.c
|
1999-08-01 11:21:05 +00:00
|
|
|
* PROGRAMER: Jason Filby
|
|
|
|
*/
|
|
|
|
|
2010-04-26 13:58:46 +00:00
|
|
|
#include <win32k.h>
|
2003-06-19 17:13:28 +00:00
|
|
|
|
2005-06-29 07:09:25 +00:00
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
2003-07-11 15:59:37 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2015-03-10 00:09:20 +00:00
|
|
|
_Must_inspect_result_
|
|
|
|
_When_(fl & FL_ZERO_MEMORY, _Ret_opt_bytecount_(cjMemSize))
|
|
|
|
_When_(!(fl & FL_ZERO_MEMORY), _Ret_opt_bytecap_(cjMemSize))
|
2015-03-10 00:12:41 +00:00
|
|
|
__drv_allocatesMem(Mem)
|
2015-03-10 00:09:20 +00:00
|
|
|
ENGAPI
|
2012-05-01 09:26:45 +00:00
|
|
|
PVOID
|
|
|
|
APIENTRY
|
|
|
|
EngAllocMem(
|
2015-03-10 00:09:20 +00:00
|
|
|
_In_ ULONG fl,
|
|
|
|
_In_ ULONG cjMemSize,
|
|
|
|
_In_ ULONG ulTag)
|
1999-08-01 11:21:05 +00:00
|
|
|
{
|
2012-05-01 09:26:45 +00:00
|
|
|
PVOID pvBaseAddress;
|
1999-08-01 11:21:05 +00:00
|
|
|
|
2015-03-10 00:09:20 +00:00
|
|
|
pvBaseAddress = ExAllocatePoolWithTag((fl & FL_NONPAGED_MEMORY) ?
|
2012-05-01 09:26:45 +00:00
|
|
|
NonPagedPool : PagedPool,
|
|
|
|
cjMemSize,
|
|
|
|
ulTag);
|
1999-08-01 11:21:05 +00:00
|
|
|
|
2012-05-01 09:26:45 +00:00
|
|
|
if (pvBaseAddress == NULL)
|
2011-12-17 08:35:31 +00:00
|
|
|
return NULL;
|
1999-08-01 11:21:05 +00:00
|
|
|
|
2015-03-10 00:09:20 +00:00
|
|
|
if (fl & FL_ZERO_MEMORY)
|
2012-05-01 09:26:45 +00:00
|
|
|
RtlZeroMemory(pvBaseAddress, cjMemSize);
|
2011-12-17 08:35:31 +00:00
|
|
|
|
2012-05-01 09:26:45 +00:00
|
|
|
return pvBaseAddress;
|
1999-08-01 11:21:05 +00:00
|
|
|
}
|
|
|
|
|
2003-07-11 15:59:37 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2012-05-01 09:26:45 +00:00
|
|
|
VOID
|
|
|
|
APIENTRY
|
|
|
|
EngFreeMem(PVOID pvBaseAddress)
|
1999-08-01 11:21:05 +00:00
|
|
|
{
|
2012-09-18 20:55:15 +00:00
|
|
|
/* Windows allows to pass NULL */
|
|
|
|
if (pvBaseAddress)
|
|
|
|
{
|
2012-10-06 20:15:36 +00:00
|
|
|
/* Use 0 as tag, which equals a call to ExFreePool */
|
|
|
|
ExFreePoolWithTag(pvBaseAddress, 0);
|
2012-09-18 20:55:15 +00:00
|
|
|
}
|
1999-08-01 11:21:05 +00:00
|
|
|
}
|
|
|
|
|
2003-07-11 15:59:37 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2015-03-10 00:09:20 +00:00
|
|
|
_Must_inspect_result_
|
|
|
|
_Ret_opt_bytecount_(cjMemSize)
|
2015-03-10 00:12:41 +00:00
|
|
|
__drv_allocatesMem(UserMem)
|
2015-03-10 00:09:20 +00:00
|
|
|
ENGAPI
|
2012-05-01 09:26:45 +00:00
|
|
|
PVOID
|
|
|
|
APIENTRY
|
2015-03-10 00:09:20 +00:00
|
|
|
EngAllocUserMem(
|
|
|
|
_In_ SIZE_T cjMemSize,
|
|
|
|
_In_ ULONG ulTag)
|
1999-08-01 11:21:05 +00:00
|
|
|
{
|
2012-05-01 09:26:45 +00:00
|
|
|
PVOID pvBaseAddress = NULL;
|
|
|
|
NTSTATUS Status;
|
1999-08-01 11:21:05 +00:00
|
|
|
|
2012-05-01 09:26:45 +00:00
|
|
|
Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
|
|
|
|
&pvBaseAddress,
|
|
|
|
0,
|
2015-03-10 00:09:20 +00:00
|
|
|
&cjMemSize,
|
2012-05-01 09:26:45 +00:00
|
|
|
MEM_COMMIT | MEM_RESERVE,
|
|
|
|
PAGE_READWRITE);
|
2000-06-18 12:29:32 +00:00
|
|
|
|
2012-05-01 09:26:45 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
2003-06-06 10:17:44 +00:00
|
|
|
{
|
2012-05-01 09:26:45 +00:00
|
|
|
return NULL;
|
2003-06-06 10:17:44 +00:00
|
|
|
}
|
|
|
|
|
2012-05-01 09:26:45 +00:00
|
|
|
/* TODO: Add allocation info to AVL tree (stored inside W32PROCESS structure) */
|
|
|
|
//hSecure = EngSecureMem(pvBaseAddress, cj);
|
2003-06-06 10:17:44 +00:00
|
|
|
|
2015-03-10 00:09:20 +00:00
|
|
|
return pvBaseAddress;
|
1999-08-01 11:21:05 +00:00
|
|
|
}
|
|
|
|
|
2003-07-11 15:59:37 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2012-05-01 09:26:45 +00:00
|
|
|
VOID
|
|
|
|
APIENTRY
|
|
|
|
EngFreeUserMem(PVOID pvBaseAddress)
|
1999-08-01 11:21:05 +00:00
|
|
|
{
|
2012-05-01 09:26:45 +00:00
|
|
|
SIZE_T cjSize = 0;
|
2003-06-06 10:17:44 +00:00
|
|
|
|
2012-05-01 09:26:45 +00:00
|
|
|
ZwFreeVirtualMemory(NtCurrentProcess(),
|
|
|
|
&pvBaseAddress,
|
|
|
|
&cjSize,
|
|
|
|
MEM_RELEASE);
|
2009-03-28 17:06:17 +00:00
|
|
|
|
|
|
|
/* TODO: Remove allocation info from AVL tree */
|
1999-08-01 11:21:05 +00:00
|
|
|
}
|
2003-06-19 17:13:28 +00:00
|
|
|
|
2008-07-21 23:56:26 +00:00
|
|
|
PVOID
|
2008-11-29 22:48:58 +00:00
|
|
|
APIENTRY
|
2008-07-21 23:56:26 +00:00
|
|
|
HackSecureVirtualMemory(
|
2012-05-01 09:26:45 +00:00
|
|
|
IN PVOID Address,
|
|
|
|
IN SIZE_T Size,
|
|
|
|
IN ULONG ProbeMode,
|
|
|
|
OUT PVOID *SafeAddress)
|
2008-07-21 23:56:26 +00:00
|
|
|
{
|
2012-05-01 09:26:45 +00:00
|
|
|
NTSTATUS Status = STATUS_SUCCESS;
|
|
|
|
PMDL pmdl;
|
|
|
|
LOCK_OPERATION Operation;
|
|
|
|
|
|
|
|
if (ProbeMode == PAGE_READONLY) Operation = IoReadAccess;
|
|
|
|
else if (ProbeMode == PAGE_READWRITE) Operation = IoModifyAccess;
|
|
|
|
else return NULL;
|
|
|
|
|
2014-05-10 14:33:37 +00:00
|
|
|
pmdl = IoAllocateMdl(Address, (ULONG)Size, FALSE, TRUE, NULL);
|
2012-05-01 09:26:45 +00:00
|
|
|
if (pmdl == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
MmProbeAndLockPages(pmdl, UserMode, Operation);
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
Status = _SEH2_GetExceptionCode();
|
|
|
|
}
|
|
|
|
_SEH2_END
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
IoFreeMdl(pmdl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*SafeAddress = MmGetSystemAddressForMdlSafe(pmdl, NormalPagePriority);
|
|
|
|
|
|
|
|
if(!*SafeAddress)
|
|
|
|
{
|
|
|
|
MmUnlockPages(pmdl);
|
|
|
|
IoFreeMdl(pmdl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pmdl;
|
2008-07-21 23:56:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
2008-11-29 22:48:58 +00:00
|
|
|
APIENTRY
|
2008-07-21 23:56:26 +00:00
|
|
|
HackUnsecureVirtualMemory(
|
2012-05-01 09:26:45 +00:00
|
|
|
IN PVOID SecureHandle)
|
2008-07-21 23:56:26 +00:00
|
|
|
{
|
2012-05-01 09:26:45 +00:00
|
|
|
PMDL pmdl = (PMDL)SecureHandle;
|
2008-07-21 23:56:26 +00:00
|
|
|
|
2012-05-01 09:26:45 +00:00
|
|
|
MmUnlockPages(pmdl);
|
|
|
|
IoFreeMdl(pmdl);
|
2008-07-21 23:56:26 +00:00
|
|
|
}
|
|
|
|
|
2003-07-11 15:59:37 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2012-05-09 20:15:23 +00:00
|
|
|
HANDLE
|
|
|
|
APIENTRY
|
2003-06-19 17:13:28 +00:00
|
|
|
EngSecureMem(PVOID Address, ULONG Length)
|
|
|
|
{
|
2012-05-09 20:15:23 +00:00
|
|
|
{// HACK!!!
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
ProbeForWrite(Address, Length, 1);
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
_SEH2_YIELD(return NULL);
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
return (HANDLE)-1;
|
|
|
|
}
|
2012-05-01 09:26:45 +00:00
|
|
|
return MmSecureVirtualMemory(Address, Length, PAGE_READWRITE);
|
2003-06-19 17:13:28 +00:00
|
|
|
}
|
|
|
|
|
2012-05-09 20:15:23 +00:00
|
|
|
HANDLE
|
|
|
|
APIENTRY
|
|
|
|
EngSecureMemForRead(PVOID Address, ULONG Length)
|
|
|
|
{
|
|
|
|
{// HACK!!!
|
|
|
|
ULONG cPages;
|
|
|
|
volatile BYTE *pjProbe;
|
|
|
|
|
|
|
|
_SEH2_TRY
|
|
|
|
{
|
|
|
|
ProbeForRead(Address, Length, 1);
|
|
|
|
cPages = ADDRESS_AND_SIZE_TO_SPAN_PAGES(Address, Length);
|
|
|
|
pjProbe = ALIGN_DOWN_POINTER_BY(Address, PAGE_SIZE);
|
|
|
|
while(cPages--)
|
|
|
|
{
|
|
|
|
/* Do a read probe */
|
|
|
|
(void)*pjProbe;
|
|
|
|
pjProbe += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
_SEH2_YIELD(return NULL);
|
|
|
|
}
|
|
|
|
_SEH2_END;
|
|
|
|
return (HANDLE)-1;
|
|
|
|
}
|
|
|
|
return MmSecureVirtualMemory(Address, Length, PAGE_READONLY);
|
|
|
|
}
|
|
|
|
|
2003-07-11 15:59:37 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2008-11-29 22:48:58 +00:00
|
|
|
VOID APIENTRY
|
2003-06-19 17:13:28 +00:00
|
|
|
EngUnsecureMem(HANDLE Mem)
|
|
|
|
{
|
2010-06-06 03:12:56 +00:00
|
|
|
if (Mem == (HANDLE)-1) return; // HACK!!!
|
2012-05-01 09:26:45 +00:00
|
|
|
MmUnsecureVirtualMemory((PVOID) Mem);
|
2003-06-19 17:13:28 +00:00
|
|
|
}
|
|
|
|
|
2003-05-18 17:16:18 +00:00
|
|
|
/* EOF */
|