Author: hbelusca
Date: Mon Nov 17 02:08:12 2014
New Revision: 65426

URL: http://svn.reactos.org/svn/reactos?rev=65426&view=rev
Log:
[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.

Modified:
    trunk/reactos/subsystems/ntvdm/emulator.c
    trunk/reactos/subsystems/ntvdm/emulator.h

Modified: trunk/reactos/subsystems/ntvdm/emulator.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/subsystems/ntvdm/emulator.c?rev=65426&r1=65425&r2=65426&view=diff
==============================================================================
--- trunk/reactos/subsystems/ntvdm/emulator.c   [iso-8859-1] (original)
+++ trunk/reactos/subsystems/ntvdm/emulator.c   [iso-8859-1] Mon Nov 17 
02:08:12 2014
@@ -560,7 +560,7 @@
 {
 #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 @@
 #else
 
     NTSTATUS Status;
-    SIZE_T MemorySize = MAX_ADDRESS;
-
-    /* The reserved region starts from the very first page */
-    BaseAddress = NULL;
+    SIZE_T MemorySize = MAX_ADDRESS; // See: 
kernel32/client/vdm.c!BaseGetVdmConfigInfo
+
+    /*
+     * 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,9 +594,11 @@
                                      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 @@
     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 @@
     if (InputThread == NULL)
     {
         DisplayMessage(L"Failed to create the console input thread.");
+        EmulatorCleanup();
         return FALSE;
     }
     /************************************************************/
@@ -667,6 +671,7 @@
     if (!VgaInitialize(ConsoleOutput))
     {
         DisplayMessage(L"Failed to initialize VGA support.");
+        EmulatorCleanup();
         return FALSE;
     }
 
@@ -683,6 +688,11 @@
 
 VOID EmulatorCleanup(VOID)
 {
+#ifndef STANDALONE
+    NTSTATUS Status;
+    SIZE_T MemorySize = MAX_ADDRESS;
+#endif
+
     VgaCleanup();
 
     /* Close the input thread handle */
@@ -698,8 +708,28 @@
 
     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
 }
 
 

Modified: trunk/reactos/subsystems/ntvdm/emulator.h
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/subsystems/ntvdm/emulator.h?rev=65426&r1=65425&r2=65426&view=diff
==============================================================================
--- trunk/reactos/subsystems/ntvdm/emulator.h   [iso-8859-1] (original)
+++ trunk/reactos/subsystems/ntvdm/emulator.h   [iso-8859-1] Mon Nov 17 
02:08:12 2014
@@ -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)))


Reply via email to