1999-08-01 11:21:05 +00:00
|
|
|
/*
|
2003-05-18 17:16:18 +00:00
|
|
|
* ReactOS W32 Subsystem
|
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2009-10-27 10:34:16 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2003-05-18 17:16:18 +00:00
|
|
|
*/
|
2005-01-06 13:58:04 +00:00
|
|
|
/* $Id$
|
2003-05-18 17:16:18 +00:00
|
|
|
*
|
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
|
|
|
|
* FILE: subsys/win32k/eng/mem.c
|
|
|
|
* PROGRAMER: Jason Filby
|
|
|
|
* REVISION HISTORY:
|
|
|
|
* 3/7/1999: Created
|
|
|
|
*/
|
|
|
|
|
2004-05-10 17:07:20 +00:00
|
|
|
#include <w32k.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
|
|
|
|
*/
|
2008-11-29 22:48:58 +00:00
|
|
|
PVOID APIENTRY
|
2002-06-15 21:44:08 +00:00
|
|
|
EngAllocMem(ULONG Flags,
|
|
|
|
ULONG MemSize,
|
|
|
|
ULONG Tag)
|
1999-08-01 11:21:05 +00:00
|
|
|
{
|
2001-03-31 15:35:08 +00:00
|
|
|
PVOID newMem;
|
1999-08-01 11:21:05 +00:00
|
|
|
|
2004-07-03 22:36:27 +00:00
|
|
|
newMem = ExAllocatePoolWithTag(PagedPool, MemSize, Tag);
|
1999-08-01 11:21:05 +00:00
|
|
|
|
2003-09-26 10:45:45 +00:00
|
|
|
if (Flags == FL_ZERO_MEMORY && NULL != newMem)
|
2001-03-31 15:35:08 +00:00
|
|
|
{
|
|
|
|
RtlZeroMemory(newMem, MemSize);
|
|
|
|
}
|
1999-08-01 11:21:05 +00:00
|
|
|
|
2001-03-31 15:35:08 +00:00
|
|
|
return newMem;
|
1999-08-01 11:21:05 +00:00
|
|
|
}
|
|
|
|
|
2003-07-11 15:59:37 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2008-11-29 22:48:58 +00:00
|
|
|
VOID APIENTRY
|
2002-06-15 21:44:08 +00:00
|
|
|
EngFreeMem(PVOID Mem)
|
1999-08-01 11:21:05 +00:00
|
|
|
{
|
2001-03-31 15:35:08 +00:00
|
|
|
ExFreePool(Mem);
|
1999-08-01 11:21:05 +00:00
|
|
|
}
|
|
|
|
|
2003-07-11 15:59:37 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2008-11-29 22:48:58 +00:00
|
|
|
PVOID APIENTRY
|
2008-04-25 00:38:32 +00:00
|
|
|
EngAllocUserMem(SIZE_T cj, ULONG Tag)
|
1999-08-01 11:21:05 +00:00
|
|
|
{
|
2003-06-06 10:17:44 +00:00
|
|
|
PVOID NewMem = NULL;
|
|
|
|
NTSTATUS Status;
|
2009-03-28 17:06:17 +00:00
|
|
|
SIZE_T MemSize = cj;
|
1999-08-01 11:21:05 +00:00
|
|
|
|
2009-03-28 16:53:51 +00:00
|
|
|
Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &NewMem, 0, &MemSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
|
2000-06-18 12:29:32 +00:00
|
|
|
|
2003-06-06 10:17:44 +00:00
|
|
|
if (! NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-03-28 17:06:17 +00:00
|
|
|
/* TODO: Add allocation info to AVL tree (stored inside W32PROCESS structure) */
|
2003-06-06 10:17:44 +00:00
|
|
|
|
2009-03-28 17:06:17 +00:00
|
|
|
return NewMem;
|
1999-08-01 11:21:05 +00:00
|
|
|
}
|
|
|
|
|
2003-07-11 15:59:37 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2008-11-29 22:48:58 +00:00
|
|
|
VOID APIENTRY
|
2002-06-15 21:44:08 +00:00
|
|
|
EngFreeUserMem(PVOID pv)
|
1999-08-01 11:21:05 +00:00
|
|
|
{
|
2009-03-28 17:06:17 +00:00
|
|
|
PVOID BaseAddress = pv;
|
2009-03-28 16:53:51 +00:00
|
|
|
SIZE_T MemSize = 0;
|
2003-06-06 10:17:44 +00:00
|
|
|
|
2009-03-28 17:06:17 +00:00
|
|
|
ZwFreeVirtualMemory(NtCurrentProcess(), &BaseAddress, &MemSize, MEM_RELEASE);
|
|
|
|
|
|
|
|
/* 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(
|
|
|
|
IN PVOID Address,
|
|
|
|
IN SIZE_T Size,
|
|
|
|
IN ULONG ProbeMode,
|
|
|
|
OUT PVOID *SafeAddress)
|
|
|
|
{
|
|
|
|
NTSTATUS Status = STATUS_SUCCESS;
|
|
|
|
PMDL mdl;
|
|
|
|
LOCK_OPERATION Operation;
|
|
|
|
|
|
|
|
if (ProbeMode == PAGE_READONLY) Operation = IoReadAccess;
|
|
|
|
else if (ProbeMode == PAGE_READWRITE) Operation = IoModifyAccess;
|
|
|
|
else return NULL;
|
|
|
|
|
|
|
|
mdl = IoAllocateMdl(Address, Size, FALSE, TRUE, NULL);
|
|
|
|
if (mdl == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-11-30 19:28:11 +00:00
|
|
|
_SEH2_TRY
|
2008-07-21 23:56:26 +00:00
|
|
|
{
|
|
|
|
MmProbeAndLockPages(mdl, UserMode, Operation);
|
|
|
|
}
|
2008-11-30 19:28:11 +00:00
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
|
2008-07-21 23:56:26 +00:00
|
|
|
{
|
2008-11-30 19:28:11 +00:00
|
|
|
Status = _SEH2_GetExceptionCode();
|
2008-07-21 23:56:26 +00:00
|
|
|
}
|
2008-11-30 19:28:11 +00:00
|
|
|
_SEH2_END
|
2008-07-21 23:56:26 +00:00
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
IoFreeMdl(mdl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*SafeAddress = MmGetSystemAddressForMdlSafe(mdl, NormalPagePriority);
|
|
|
|
|
|
|
|
if(!*SafeAddress)
|
|
|
|
{
|
|
|
|
MmUnlockPages(mdl);
|
|
|
|
IoFreeMdl(mdl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mdl;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
2008-11-29 22:48:58 +00:00
|
|
|
APIENTRY
|
2008-07-21 23:56:26 +00:00
|
|
|
HackUnsecureVirtualMemory(
|
|
|
|
IN PVOID SecureHandle)
|
|
|
|
{
|
|
|
|
PMDL mdl = (PMDL)SecureHandle;
|
|
|
|
|
|
|
|
MmUnlockPages(mdl);
|
|
|
|
IoFreeMdl(mdl);
|
|
|
|
}
|
|
|
|
|
2003-07-11 15:59:37 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2008-11-29 22:48:58 +00:00
|
|
|
HANDLE APIENTRY
|
2003-06-19 17:13:28 +00:00
|
|
|
EngSecureMem(PVOID Address, ULONG Length)
|
|
|
|
{
|
|
|
|
return MmSecureVirtualMemory(Address, Length, PAGE_READWRITE);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2009-07-31 18:21:24 +00:00
|
|
|
MmUnsecureVirtualMemory((PVOID) Mem);
|
2003-06-19 17:13:28 +00:00
|
|
|
}
|
|
|
|
|
2003-05-18 17:16:18 +00:00
|
|
|
/* EOF */
|