2006-11-08 11:47:44 +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.
|
2006-11-08 11:47:44 +00:00
|
|
|
*/
|
|
|
|
|
2010-04-26 13:58:46 +00:00
|
|
|
#include <win32k.h>
|
2006-11-08 11:47:44 +00:00
|
|
|
|
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
2014-12-28 20:50:35 +00:00
|
|
|
HANDLE GlobalUserHeap = NULL;
|
|
|
|
PVOID GlobalUserHeapSection = NULL;
|
|
|
|
|
2006-11-08 11:47:44 +00:00
|
|
|
|
2012-07-31 18:40:52 +00:00
|
|
|
_Function_class_(RTL_HEAP_COMMIT_ROUTINE)
|
|
|
|
_IRQL_requires_same_
|
|
|
|
static
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
IntUserHeapCommitRoutine(
|
|
|
|
_In_ PVOID Base,
|
|
|
|
_Inout_ PVOID *CommitAddress,
|
|
|
|
_Inout_ PSIZE_T CommitSize)
|
2006-11-08 11:47:44 +00:00
|
|
|
{
|
2009-07-26 16:17:50 +00:00
|
|
|
PPROCESSINFO W32Process;
|
2006-11-08 11:47:44 +00:00
|
|
|
PW32HEAP_USER_MAPPING Mapping;
|
|
|
|
PVOID UserBase = NULL;
|
|
|
|
NTSTATUS Status;
|
2010-10-07 07:56:26 +00:00
|
|
|
SIZE_T Delta;
|
|
|
|
PVOID UserCommitAddress;
|
2006-11-08 11:47:44 +00:00
|
|
|
|
|
|
|
W32Process = PsGetCurrentProcessWin32Process();
|
|
|
|
|
|
|
|
if (W32Process != NULL)
|
|
|
|
{
|
2011-12-14 04:07:06 +00:00
|
|
|
/* Search for the mapping */
|
2006-11-08 11:47:44 +00:00
|
|
|
Mapping = &W32Process->HeapMappings;
|
|
|
|
while (Mapping != NULL)
|
|
|
|
{
|
|
|
|
if (Mapping->KernelMapping == Base)
|
|
|
|
{
|
|
|
|
UserBase = Mapping->UserMapping;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mapping = Mapping->Next;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(UserBase != NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-05-07 14:39:31 +00:00
|
|
|
SIZE_T ViewSize = 0;
|
2006-11-08 11:47:44 +00:00
|
|
|
LARGE_INTEGER Offset;
|
|
|
|
|
|
|
|
/* HACK: This needs to be handled during startup only... */
|
|
|
|
ASSERT(Base == (PVOID)GlobalUserHeap);
|
|
|
|
|
2011-12-14 04:07:06 +00:00
|
|
|
/* Temporarily map it into user space */
|
2006-11-08 11:47:44 +00:00
|
|
|
Offset.QuadPart = 0;
|
|
|
|
Status = MmMapViewOfSection(GlobalUserHeapSection,
|
|
|
|
PsGetCurrentProcess(),
|
|
|
|
&UserBase,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
&Offset,
|
|
|
|
&ViewSize,
|
|
|
|
ViewUnmap,
|
|
|
|
SEC_NO_CHANGE,
|
2011-12-14 04:07:06 +00:00
|
|
|
PAGE_EXECUTE_READ); /* Would prefer PAGE_READONLY, but thanks to RTL heaps... */
|
2006-11-08 11:47:44 +00:00
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2010-10-07 07:56:26 +00:00
|
|
|
/* Apply the commit address offset to the user base address */
|
2014-12-28 20:50:35 +00:00
|
|
|
Delta = (SIZE_T)((ULONG_PTR)(*CommitAddress) - (ULONG_PTR)Base);
|
|
|
|
UserCommitAddress = (PVOID)((ULONG_PTR)UserBase + Delta);
|
2006-11-08 11:47:44 +00:00
|
|
|
|
2010-10-07 07:56:26 +00:00
|
|
|
/* Perform the actual commit */
|
2006-11-08 11:47:44 +00:00
|
|
|
Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
|
2010-10-07 07:56:26 +00:00
|
|
|
&UserCommitAddress,
|
2006-11-08 11:47:44 +00:00
|
|
|
0,
|
|
|
|
CommitSize,
|
|
|
|
MEM_COMMIT,
|
|
|
|
PAGE_EXECUTE_READ);
|
2010-10-07 07:56:26 +00:00
|
|
|
|
2006-11-08 11:47:44 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
2010-10-07 07:56:26 +00:00
|
|
|
/* Determine the address to return */
|
2014-12-28 20:50:35 +00:00
|
|
|
Delta = (SIZE_T)((ULONG_PTR)UserCommitAddress - (ULONG_PTR)UserBase);
|
|
|
|
*CommitAddress = (PVOID)((ULONG_PTR)Base + Delta);
|
2006-11-08 11:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (W32Process == NULL)
|
|
|
|
{
|
|
|
|
MmUnmapViewOfSection(PsGetCurrentProcess(),
|
|
|
|
UserBase);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2008-10-19 02:05:41 +00:00
|
|
|
static PWIN32HEAP
|
2013-08-14 11:02:08 +00:00
|
|
|
IntUserHeapCreate(IN PVOID SectionObject,
|
2006-11-08 11:47:44 +00:00
|
|
|
IN PVOID *SystemMappedBase,
|
|
|
|
IN ULONG HeapSize)
|
|
|
|
{
|
|
|
|
PVOID MappedView = NULL;
|
|
|
|
LARGE_INTEGER Offset;
|
2008-04-25 01:16:08 +00:00
|
|
|
SIZE_T ViewSize = PAGE_SIZE;
|
2006-11-08 11:47:44 +00:00
|
|
|
RTL_HEAP_PARAMETERS Parameters = {0};
|
2008-10-19 02:05:41 +00:00
|
|
|
PVOID pHeap;
|
2006-11-08 11:47:44 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
Offset.QuadPart = 0;
|
|
|
|
|
|
|
|
/* Commit the first page before creating the heap! */
|
|
|
|
Status = MmMapViewOfSection(SectionObject,
|
|
|
|
PsGetCurrentProcess(),
|
|
|
|
&MappedView,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
&Offset,
|
|
|
|
&ViewSize,
|
|
|
|
ViewUnmap,
|
|
|
|
SEC_NO_CHANGE,
|
2011-12-14 04:07:06 +00:00
|
|
|
PAGE_EXECUTE_READ); /* Would prefer PAGE_READONLY, but thanks to RTL heaps... */
|
2006-11-08 11:47:44 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
|
|
|
|
&MappedView,
|
|
|
|
0,
|
|
|
|
&ViewSize,
|
|
|
|
MEM_COMMIT,
|
2011-12-14 04:07:06 +00:00
|
|
|
PAGE_EXECUTE_READ); /* Would prefer PAGE_READONLY, but thanks to RTL heaps... */
|
2006-11-08 11:47:44 +00:00
|
|
|
|
|
|
|
MmUnmapViewOfSection(PsGetCurrentProcess(),
|
|
|
|
MappedView);
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Create the heap, don't serialize in kmode! The caller is responsible
|
|
|
|
to synchronize the heap! */
|
|
|
|
Parameters.Length = sizeof(Parameters);
|
2008-09-15 13:09:30 +00:00
|
|
|
Parameters.InitialCommit = ViewSize;
|
2006-11-08 11:47:44 +00:00
|
|
|
Parameters.InitialReserve = (SIZE_T)HeapSize;
|
|
|
|
Parameters.CommitRoutine = IntUserHeapCommitRoutine;
|
|
|
|
|
2008-10-19 02:05:41 +00:00
|
|
|
pHeap = RtlCreateHeap(HEAP_ZERO_MEMORY | HEAP_NO_SERIALIZE,
|
2006-11-08 11:47:44 +00:00
|
|
|
*SystemMappedBase,
|
|
|
|
(SIZE_T)HeapSize,
|
2008-09-15 13:09:30 +00:00
|
|
|
ViewSize,
|
2006-11-08 11:47:44 +00:00
|
|
|
NULL,
|
|
|
|
&Parameters);
|
|
|
|
|
2008-10-19 02:05:41 +00:00
|
|
|
return pHeap;
|
2006-11-08 11:47:44 +00:00
|
|
|
}
|
|
|
|
|
2008-10-19 02:05:41 +00:00
|
|
|
PWIN32HEAP
|
2013-08-14 11:02:08 +00:00
|
|
|
UserCreateHeap(OUT PVOID *SectionObject,
|
2006-11-08 11:47:44 +00:00
|
|
|
IN OUT PVOID *SystemBase,
|
2008-05-07 14:39:31 +00:00
|
|
|
IN SIZE_T HeapSize)
|
2006-11-08 11:47:44 +00:00
|
|
|
{
|
|
|
|
LARGE_INTEGER SizeHeap;
|
2008-10-19 02:05:41 +00:00
|
|
|
PWIN32HEAP pHeap = NULL;
|
2006-11-08 11:47:44 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
SizeHeap.QuadPart = HeapSize;
|
|
|
|
|
2011-12-14 04:07:06 +00:00
|
|
|
/* Create the section and map it into session space */
|
2006-11-08 11:47:44 +00:00
|
|
|
Status = MmCreateSection((PVOID*)SectionObject,
|
|
|
|
SECTION_ALL_ACCESS,
|
|
|
|
NULL,
|
|
|
|
&SizeHeap,
|
2011-12-14 04:07:06 +00:00
|
|
|
PAGE_EXECUTE_READWRITE, /* Would prefer PAGE_READWRITE, but thanks to RTL heaps... */
|
2012-09-19 01:56:28 +00:00
|
|
|
SEC_RESERVE | 1,
|
2006-11-08 11:47:44 +00:00
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
SetLastNtError(Status);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-09-19 01:56:28 +00:00
|
|
|
Status = MmMapViewInSessionSpace(*SectionObject,
|
|
|
|
SystemBase,
|
|
|
|
&HeapSize);
|
2006-11-08 11:47:44 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
ObDereferenceObject(*SectionObject);
|
|
|
|
*SectionObject = NULL;
|
|
|
|
|
|
|
|
SetLastNtError(Status);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-12-14 04:07:06 +00:00
|
|
|
/* Create the heap */
|
2008-10-19 02:05:41 +00:00
|
|
|
pHeap = IntUserHeapCreate(*SectionObject,
|
2006-11-08 11:47:44 +00:00
|
|
|
SystemBase,
|
|
|
|
HeapSize);
|
|
|
|
|
2008-10-19 02:05:41 +00:00
|
|
|
if (pHeap == NULL)
|
2006-11-08 11:47:44 +00:00
|
|
|
{
|
|
|
|
ObDereferenceObject(*SectionObject);
|
|
|
|
*SectionObject = NULL;
|
|
|
|
|
|
|
|
SetLastNtError(STATUS_UNSUCCESSFUL);
|
|
|
|
}
|
|
|
|
|
2008-10-19 02:05:41 +00:00
|
|
|
return pHeap;
|
2006-11-08 11:47:44 +00:00
|
|
|
}
|
2014-12-29 13:56:28 +00:00
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
UnmapGlobalUserHeap(IN PEPROCESS Process)
|
|
|
|
{
|
|
|
|
NTSTATUS Status = STATUS_SUCCESS;
|
|
|
|
PPROCESSINFO W32Process;
|
|
|
|
PW32HEAP_USER_MAPPING HeapMapping;
|
|
|
|
|
|
|
|
TRACE_CH(UserProcess, "IntUnmapDesktopView called for process 0x%p\n", Process);
|
|
|
|
|
|
|
|
W32Process = PsGetProcessWin32Process(Process);
|
|
|
|
if (W32Process == NULL)
|
|
|
|
{
|
|
|
|
ERR_CH(UserProcess, "UnmapGlobalUserHeap - We don't have a Win32 process!\n");
|
|
|
|
ASSERT(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The first mapping entry must be the global user heap */
|
|
|
|
HeapMapping = &W32Process->HeapMappings;
|
|
|
|
ASSERT(HeapMapping->KernelMapping == (PVOID)GlobalUserHeap);
|
|
|
|
|
|
|
|
/* Unmap if we're the last thread using the global user heap */
|
|
|
|
if (--HeapMapping->Count == 0)
|
|
|
|
{
|
|
|
|
TRACE_CH(UserProcess, "UnmapGlobalUserHeap - Unmapping\n");
|
|
|
|
Status = MmUnmapViewOfSection(Process, HeapMapping->UserMapping);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
MapGlobalUserHeap(IN PEPROCESS Process,
|
|
|
|
OUT PVOID* KernelMapping,
|
|
|
|
OUT PVOID* UserMapping)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
|
|
|
PPROCESSINFO W32Process;
|
|
|
|
PW32HEAP_USER_MAPPING HeapMapping;
|
|
|
|
PVOID UserBase = NULL;
|
|
|
|
|
|
|
|
SIZE_T ViewSize = 0;
|
|
|
|
LARGE_INTEGER Offset;
|
|
|
|
|
|
|
|
TRACE_CH(UserProcess, "MapGlobalUserHeap called for process 0x%p\n", Process);
|
|
|
|
|
|
|
|
W32Process = PsGetProcessWin32Process(Process);
|
|
|
|
if (W32Process == NULL)
|
|
|
|
{
|
|
|
|
ERR_CH(UserProcess, "MapGlobalUserHeap - We don't have a Win32 process!\n");
|
|
|
|
ASSERT(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE_CH(UserProcess, "MapGlobalUserHeap - We got a Win32 process, find for existing global user heap mapping...\n");
|
|
|
|
|
|
|
|
/* The first mapping entry must be the global user heap */
|
|
|
|
HeapMapping = &W32Process->HeapMappings;
|
|
|
|
|
|
|
|
/* Find out if another thread already mapped the global user heap */
|
|
|
|
if (HeapMapping->KernelMapping == (PVOID)GlobalUserHeap)
|
|
|
|
{
|
|
|
|
HeapMapping->Count++;
|
|
|
|
|
|
|
|
TRACE_CH(UserProcess, "MapGlobalUserHeap - A mapping was found, return it.\n");
|
|
|
|
|
|
|
|
*KernelMapping = HeapMapping->KernelMapping;
|
|
|
|
*UserMapping = HeapMapping->UserMapping;
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE_CH(UserProcess, "MapGlobalUserHeap - No mapping was found, let's map...\n");
|
|
|
|
|
|
|
|
/* We're the first, map the global heap into the process */
|
|
|
|
Offset.QuadPart = 0;
|
|
|
|
Status = MmMapViewOfSection(GlobalUserHeapSection,
|
|
|
|
Process,
|
|
|
|
&UserBase,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
&Offset,
|
|
|
|
&ViewSize,
|
|
|
|
ViewUnmap,
|
|
|
|
SEC_NO_CHANGE,
|
|
|
|
PAGE_EXECUTE_READ); /* Would prefer PAGE_READONLY, but thanks to RTL heaps... */
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
ERR_CH(UserProcess, "MapGlobalUserHeap - Failed to map the global heap! 0x%x\n", Status);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE_CH(UserProcess, "MapGlobalUserHeap -- Mapped kernel global heap 0x%p to user space at 0x%p\n",
|
|
|
|
GlobalUserHeap, UserBase);
|
|
|
|
|
|
|
|
/* Add the mapping */
|
|
|
|
HeapMapping->Next = NULL;
|
|
|
|
HeapMapping->KernelMapping = (PVOID)GlobalUserHeap;
|
|
|
|
HeapMapping->UserMapping = UserBase;
|
|
|
|
HeapMapping->Limit = ViewSize;
|
|
|
|
HeapMapping->Count = 1;
|
|
|
|
|
|
|
|
*KernelMapping = HeapMapping->KernelMapping;
|
|
|
|
*UserMapping = HeapMapping->UserMapping;
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|