From 0bbe7eda197a819f71390e1b9883c646b1ed7b0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Mon, 17 Nov 2014 02:08:12 +0000 Subject: [PATCH] [NTVDM] - Remove unneeded commented code; - Fix few things related to committing the reserved low-memory VDM page by kernel32; - Decommit this memory when we terminate NTVDM. svn path=/trunk/; revision=65426 --- reactos/subsystems/ntvdm/emulator.c | 58 ++++++++++++++++++++++------- reactos/subsystems/ntvdm/emulator.h | 2 +- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/reactos/subsystems/ntvdm/emulator.c b/reactos/subsystems/ntvdm/emulator.c index a67b77558c8..2d22307df20 100644 --- a/reactos/subsystems/ntvdm/emulator.c +++ b/reactos/subsystems/ntvdm/emulator.c @@ -560,7 +560,7 @@ BOOLEAN EmulatorInitialize(HANDLE ConsoleInput, HANDLE ConsoleOutput) { #ifdef STANDALONE - /* Allocate memory for the 16-bit address space */ + /* Allocate 16 MB memory for the 16-bit address space */ BaseAddress = HeapAlloc(GetProcessHeap(), /*HEAP_ZERO_MEMORY*/ 0, MAX_ADDRESS); if (BaseAddress == NULL) { @@ -571,10 +571,19 @@ BOOLEAN EmulatorInitialize(HANDLE ConsoleInput, HANDLE ConsoleOutput) #else NTSTATUS Status; - SIZE_T MemorySize = MAX_ADDRESS; + SIZE_T MemorySize = MAX_ADDRESS; // See: kernel32/client/vdm.c!BaseGetVdmConfigInfo - /* The reserved region starts from the very first page */ - BaseAddress = NULL; + /* + * The reserved region starts from the very first page. + * We need to commit the reserved first 16 MB virtual address. + */ + BaseAddress = (PVOID)1; // NULL has another signification for NtAllocateVirtualMemory + + /* + * Since to get NULL, we allocated from 0x1, account for this. + * See also: kernel32/client/proc.c!CreateProcessInternalW + */ + MemorySize -= 1; /* Commit the reserved memory */ Status = NtAllocateVirtualMemory(NtCurrentProcess(), @@ -585,10 +594,12 @@ BOOLEAN EmulatorInitialize(HANDLE ConsoleInput, HANDLE ConsoleOutput) PAGE_EXECUTE_READWRITE); if (!NT_SUCCESS(Status)) { - wprintf(L"FATAL: Failed to commit VDM memory.\n"); + wprintf(L"FATAL: Failed to commit VDM memory, Status 0x%08lx\n", Status); return FALSE; } + ASSERT(BaseAddress == NULL); + #endif /* @@ -607,20 +618,12 @@ BOOLEAN EmulatorInitialize(HANDLE ConsoleInput, HANDLE ConsoleOutput) if (!ClockInitialize()) { wprintf(L"FATAL: Failed to initialize the clock\n"); + EmulatorCleanup(); return FALSE; } /* Initialize the CPU */ CpuInitialize(); - // Fast486Initialize(&EmulatorContext, - // EmulatorReadMemory, - // EmulatorWriteMemory, - // EmulatorReadIo, - // EmulatorWriteIo, - // NULL, - // EmulatorBiosOperation, - // EmulatorIntAcknowledge, - // NULL /* TODO: Use a TLB */); /* Initialize DMA */ @@ -659,6 +662,7 @@ BOOLEAN EmulatorInitialize(HANDLE ConsoleInput, HANDLE ConsoleOutput) if (InputThread == NULL) { DisplayMessage(L"Failed to create the console input thread."); + EmulatorCleanup(); return FALSE; } /************************************************************/ @@ -667,6 +671,7 @@ BOOLEAN EmulatorInitialize(HANDLE ConsoleInput, HANDLE ConsoleOutput) if (!VgaInitialize(ConsoleOutput)) { DisplayMessage(L"Failed to initialize VGA support."); + EmulatorCleanup(); return FALSE; } @@ -683,6 +688,11 @@ BOOLEAN EmulatorInitialize(HANDLE ConsoleInput, HANDLE ConsoleOutput) VOID EmulatorCleanup(VOID) { +#ifndef STANDALONE + NTSTATUS Status; + SIZE_T MemorySize = MAX_ADDRESS; +#endif + VgaCleanup(); /* Close the input thread handle */ @@ -698,8 +708,28 @@ VOID EmulatorCleanup(VOID) CpuCleanup(); +#ifdef STANDALONE + /* Free the memory allocated for the 16-bit address space */ if (BaseAddress != NULL) HeapFree(GetProcessHeap(), 0, BaseAddress); + +#else + + /* The reserved region starts from the very first page */ + // BaseAddress = (PVOID)1; + + /* Since to get NULL, we allocated from 0x1, account for this */ + MemorySize -= 1; + + Status = NtFreeVirtualMemory(NtCurrentProcess(), + &BaseAddress, + &MemorySize, + MEM_DECOMMIT); + if (!NT_SUCCESS(Status)) + { + DPRINT1("NTVDM: Failed to decommit VDM memory, Status 0x%08lx\n", Status); + } +#endif } diff --git a/reactos/subsystems/ntvdm/emulator.h b/reactos/subsystems/ntvdm/emulator.h index 3c209ccedf2..493cfa64340 100644 --- a/reactos/subsystems/ntvdm/emulator.h +++ b/reactos/subsystems/ntvdm/emulator.h @@ -23,7 +23,7 @@ #define TO_LINEAR(seg, off) (((seg) << 4) + (off)) #define MAX_SEGMENT 0xFFFF #define MAX_OFFSET 0xFFFF -#define MAX_ADDRESS 0x1000000 // 16 MB of RAM +#define MAX_ADDRESS 0x1000000 // 16 MB of RAM; see also: kernel32/client/vdm.c!BaseGetVdmConfigInfo #define FAR_POINTER(x) \ (PVOID)((ULONG_PTR)BaseAddress + TO_LINEAR(HIWORD(x), LOWORD(x)))