2002-09-17 20:54:42 +02:00
|
|
|
/*
|
|
|
|
* Win32 virtual memory functions
|
|
|
|
*
|
|
|
|
* Copyright 1997, 2002 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-09-17 20:54:42 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2005-06-06 22:13:08 +02:00
|
|
|
#include <stdarg.h>
|
2002-09-17 20:54:42 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "ntstatus.h"
|
2005-11-28 17:32:54 +01:00
|
|
|
#define WIN32_NO_STATUS
|
2015-03-14 03:38:17 +01:00
|
|
|
#define NONAMELESSUNION
|
2005-06-06 22:13:08 +02:00
|
|
|
#include "windef.h"
|
2002-09-17 20:54:42 +02:00
|
|
|
#include "winternl.h"
|
|
|
|
#include "wine/server.h"
|
|
|
|
#include "wine/debug.h"
|
2003-10-08 21:11:08 +02:00
|
|
|
#include "ntdll_misc.h"
|
2002-09-17 20:54:42 +02:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(virtual);
|
|
|
|
|
|
|
|
|
2020-06-02 17:08:59 +02:00
|
|
|
/**********************************************************************
|
|
|
|
* RtlCreateUserStack (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlCreateUserStack( SIZE_T commit, SIZE_T reserve, ULONG zero_bits,
|
|
|
|
SIZE_T commit_align, SIZE_T reserve_align, INITIAL_TEB *stack )
|
2009-06-25 14:18:53 +02:00
|
|
|
{
|
2020-07-22 14:16:22 +02:00
|
|
|
PROCESS_STACK_ALLOCATION_INFORMATION alloc;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
2020-06-02 17:08:59 +02:00
|
|
|
TRACE("commit %#lx, reserve %#lx, zero_bits %u, commit_align %#lx, reserve_align %#lx, stack %p\n",
|
|
|
|
commit, reserve, zero_bits, commit_align, reserve_align, stack);
|
2009-06-25 14:18:53 +02:00
|
|
|
|
2020-06-02 17:08:59 +02:00
|
|
|
if (!commit_align || !reserve_align)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
2009-06-25 14:18:53 +02:00
|
|
|
|
2020-06-02 17:08:59 +02:00
|
|
|
if (!commit || !reserve)
|
2009-06-25 14:18:53 +02:00
|
|
|
{
|
2020-06-02 17:08:59 +02:00
|
|
|
IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress );
|
|
|
|
if (!reserve) reserve = nt->OptionalHeader.SizeOfStackReserve;
|
|
|
|
if (!commit) commit = nt->OptionalHeader.SizeOfStackCommit;
|
2009-06-25 14:18:53 +02:00
|
|
|
}
|
|
|
|
|
2020-06-02 17:08:59 +02:00
|
|
|
reserve = (reserve + reserve_align - 1) & ~(reserve_align - 1);
|
|
|
|
commit = (commit + commit_align - 1) & ~(commit_align - 1);
|
2010-04-09 16:25:25 +02:00
|
|
|
|
2020-07-22 14:16:22 +02:00
|
|
|
if (reserve < commit) reserve = commit;
|
|
|
|
if (reserve < 0x100000) reserve = 0x100000;
|
|
|
|
reserve = (reserve + 0xffff) & ~0xffff; /* round to 64K boundary */
|
|
|
|
|
|
|
|
alloc.ReserveSize = reserve;
|
|
|
|
alloc.ZeroBits = zero_bits;
|
|
|
|
status = NtSetInformationProcess( GetCurrentProcess(), ProcessThreadStackAllocation,
|
|
|
|
&alloc, sizeof(alloc) );
|
|
|
|
if (!status)
|
|
|
|
{
|
|
|
|
void *addr = alloc.StackBase;
|
|
|
|
SIZE_T size = page_size;
|
|
|
|
|
|
|
|
NtAllocateVirtualMemory( GetCurrentProcess(), &addr, 0, &size, MEM_COMMIT, PAGE_NOACCESS );
|
|
|
|
addr = (char *)alloc.StackBase + page_size;
|
|
|
|
NtAllocateVirtualMemory( GetCurrentProcess(), &addr, 0, &size, MEM_COMMIT, PAGE_READWRITE | PAGE_GUARD );
|
|
|
|
addr = (char *)alloc.StackBase + 2 * page_size;
|
|
|
|
size = reserve - 2 * page_size;
|
|
|
|
NtAllocateVirtualMemory( GetCurrentProcess(), &addr, 0, &size, MEM_COMMIT, PAGE_READWRITE );
|
|
|
|
|
|
|
|
/* note: limit is lower than base since the stack grows down */
|
|
|
|
stack->OldStackBase = 0;
|
|
|
|
stack->OldStackLimit = 0;
|
|
|
|
stack->DeallocationStack = alloc.StackBase;
|
|
|
|
stack->StackBase = (char *)alloc.StackBase + reserve;
|
|
|
|
stack->StackLimit = (char *)alloc.StackBase + 2 * page_size;
|
|
|
|
}
|
|
|
|
return status;
|
2020-06-02 17:08:59 +02:00
|
|
|
}
|
2009-06-25 14:18:53 +02:00
|
|
|
|
2013-04-04 13:00:47 +02:00
|
|
|
|
2020-06-02 17:08:59 +02:00
|
|
|
/**********************************************************************
|
|
|
|
* RtlFreeUserStack (NTDLL.@)
|
|
|
|
*/
|
|
|
|
void WINAPI RtlFreeUserStack( void *stack )
|
|
|
|
{
|
|
|
|
SIZE_T size = 0;
|
2018-12-03 17:46:51 +01:00
|
|
|
|
2020-06-02 17:08:59 +02:00
|
|
|
TRACE("stack %p\n", stack);
|
2009-08-05 11:23:02 +02:00
|
|
|
|
2020-06-02 17:08:59 +02:00
|
|
|
NtFreeVirtualMemory( NtCurrentProcess(), &stack, &size, MEM_RELEASE );
|
2004-06-18 02:26:57 +02:00
|
|
|
}
|