reactos/subsystems/mvdm/ntvdm/ntvdm.c

546 lines
13 KiB
C
Raw Normal View History

/*
* COPYRIGHT: GPL - See COPYING in the top level directory
* PROJECT: ReactOS Virtual DOS Machine
* FILE: subsystems/mvdm/ntvdm/ntvdm.c
* PURPOSE: Virtual DOS Machine
* PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
*/
/* INCLUDES *******************************************************************/
#include "ntvdm.h"
#define NDEBUG
#include <debug.h>
#include "emulator.h"
#include "bios/bios.h"
#include "cpu/cpu.h"
#include "dos/dem.h"
/* VARIABLES ******************************************************************/
NTVDM_SETTINGS GlobalSettings;
// Command line of NTVDM
INT NtVdmArgc;
WCHAR** NtVdmArgv;
/* PRIVATE FUNCTIONS **********************************************************/
static NTSTATUS
NTAPI
NtVdmConfigureBios(IN PWSTR ValueName,
IN ULONG ValueType,
IN PVOID ValueData,
IN ULONG ValueLength,
IN PVOID Context,
IN PVOID EntryContext)
{
PNTVDM_SETTINGS Settings = (PNTVDM_SETTINGS)Context;
UNICODE_STRING ValueString;
/* Check for the type of the value */
if (ValueType != REG_SZ)
{
RtlInitEmptyAnsiString(&Settings->BiosFileName, NULL, 0);
return STATUS_SUCCESS;
}
/* Convert the UNICODE string to ANSI and store it */
RtlInitEmptyUnicodeString(&ValueString, (PWCHAR)ValueData, ValueLength);
ValueString.Length = ValueString.MaximumLength;
RtlUnicodeStringToAnsiString(&Settings->BiosFileName, &ValueString, TRUE);
return STATUS_SUCCESS;
}
static NTSTATUS
NTAPI
NtVdmConfigureRom(IN PWSTR ValueName,
IN ULONG ValueType,
IN PVOID ValueData,
IN ULONG ValueLength,
IN PVOID Context,
IN PVOID EntryContext)
{
PNTVDM_SETTINGS Settings = (PNTVDM_SETTINGS)Context;
UNICODE_STRING ValueString;
/* Check for the type of the value */
if (ValueType != REG_MULTI_SZ)
{
RtlInitEmptyAnsiString(&Settings->RomFiles, NULL, 0);
return STATUS_SUCCESS;
}
/* Convert the UNICODE string to ANSI and store it */
RtlInitEmptyUnicodeString(&ValueString, (PWCHAR)ValueData, ValueLength);
ValueString.Length = ValueString.MaximumLength;
RtlUnicodeStringToAnsiString(&Settings->RomFiles, &ValueString, TRUE);
return STATUS_SUCCESS;
}
static NTSTATUS
NTAPI
NtVdmConfigureFloppy(IN PWSTR ValueName,
IN ULONG ValueType,
IN PVOID ValueData,
IN ULONG ValueLength,
IN PVOID Context,
IN PVOID EntryContext)
{
BOOLEAN Success;
PNTVDM_SETTINGS Settings = (PNTVDM_SETTINGS)Context;
2018-02-11 23:38:10 +00:00
ULONG DiskNumber = PtrToUlong(EntryContext);
ASSERT(DiskNumber < ARRAYSIZE(Settings->FloppyDisks));
/* Check whether the Hard Disk entry was not already configured */
if (Settings->FloppyDisks[DiskNumber].Buffer != NULL)
{
DPRINT1("Floppy Disk %d -- '%wZ' already configured\n", DiskNumber, &Settings->FloppyDisks[DiskNumber]);
return STATUS_SUCCESS;
}
/* Check for the type of the value */
if (ValueType != REG_SZ)
{
RtlInitEmptyUnicodeString(&Settings->FloppyDisks[DiskNumber], NULL, 0);
return STATUS_SUCCESS;
}
/* Initialize the string */
Success = RtlCreateUnicodeString(&Settings->FloppyDisks[DiskNumber], (PCWSTR)ValueData);
ASSERT(Success);
return STATUS_SUCCESS;
}
static NTSTATUS
NTAPI
NtVdmConfigureHDD(IN PWSTR ValueName,
IN ULONG ValueType,
IN PVOID ValueData,
IN ULONG ValueLength,
IN PVOID Context,
IN PVOID EntryContext)
{
BOOLEAN Success;
PNTVDM_SETTINGS Settings = (PNTVDM_SETTINGS)Context;
2018-02-11 23:38:10 +00:00
ULONG DiskNumber = PtrToUlong(EntryContext);
ASSERT(DiskNumber < ARRAYSIZE(Settings->HardDisks));
/* Check whether the Hard Disk entry was not already configured */
if (Settings->HardDisks[DiskNumber].Buffer != NULL)
{
DPRINT1("Hard Disk %d -- '%wZ' already configured\n", DiskNumber, &Settings->HardDisks[DiskNumber]);
return STATUS_SUCCESS;
}
/* Check for the type of the value */
if (ValueType != REG_SZ)
{
RtlInitEmptyUnicodeString(&Settings->HardDisks[DiskNumber], NULL, 0);
return STATUS_SUCCESS;
}
/* Initialize the string */
Success = RtlCreateUnicodeString(&Settings->HardDisks[DiskNumber], (PCWSTR)ValueData);
ASSERT(Success);
return STATUS_SUCCESS;
}
static RTL_QUERY_REGISTRY_TABLE
NtVdmConfigurationTable[] =
{
{
NtVdmConfigureBios,
0,
L"BiosFile",
NULL,
REG_NONE,
NULL,
0
},
{
NtVdmConfigureRom,
RTL_QUERY_REGISTRY_NOEXPAND,
L"RomFiles",
NULL,
REG_NONE,
NULL,
0
},
{
NtVdmConfigureFloppy,
0,
L"FloppyDisk0",
(PVOID)0,
REG_NONE,
NULL,
0
},
{
NtVdmConfigureFloppy,
0,
L"FloppyDisk1",
(PVOID)1,
REG_NONE,
NULL,
0
},
{
NtVdmConfigureHDD,
0,
L"HardDisk0",
(PVOID)0,
REG_NONE,
NULL,
0
},
{
NtVdmConfigureHDD,
0,
L"HardDisk1",
(PVOID)1,
REG_NONE,
NULL,
0
},
{
NtVdmConfigureHDD,
0,
L"HardDisk2",
(PVOID)2,
REG_NONE,
NULL,
0
},
{
NtVdmConfigureHDD,
0,
L"HardDisk3",
(PVOID)3,
REG_NONE,
NULL,
0
},
/* End of table */
{0}
};
static BOOL
LoadGlobalSettings(IN PNTVDM_SETTINGS Settings)
{
NTSTATUS Status;
ASSERT(Settings);
/*
* Now we can do:
* - CPU core choice
* - Video choice
* - Sound choice
* - Mem?
* - ...
* - Standalone mode?
* - Debug settings
*/
Status = RtlQueryRegistryValues(RTL_REGISTRY_CONTROL,
L"NTVDM",
NtVdmConfigurationTable,
Settings,
NULL);
if (!NT_SUCCESS(Status))
{
DPRINT1("NTVDM registry settings cannot be fully initialized, using default ones. Status = 0x%08lx\n", Status);
}
return NT_SUCCESS(Status);
}
static VOID
FreeGlobalSettings(IN PNTVDM_SETTINGS Settings)
{
USHORT i;
ASSERT(Settings);
if (Settings->BiosFileName.Buffer)
RtlFreeAnsiString(&Settings->BiosFileName);
if (Settings->RomFiles.Buffer)
RtlFreeAnsiString(&Settings->RomFiles);
for (i = 0; i < ARRAYSIZE(Settings->FloppyDisks); ++i)
{
if (Settings->FloppyDisks[i].Buffer)
RtlFreeUnicodeString(&Settings->FloppyDisks[i]);
}
for (i = 0; i < ARRAYSIZE(Settings->HardDisks); ++i)
{
if (Settings->HardDisks[i].Buffer)
RtlFreeUnicodeString(&Settings->HardDisks[i]);
}
}
static VOID
ConsoleCleanup(VOID);
/** HACK!! **/
#include "./console/console.c"
/** HACK!! **/
/*static*/ VOID
VdmShutdown(BOOLEAN Immediate)
{
/*
* Immediate = TRUE: Immediate shutdown;
* FALSE: Delayed shutdown.
*/
static BOOLEAN MustShutdown = FALSE;
/* If a shutdown is ongoing, just return */
if (MustShutdown)
{
DPRINT1("Shutdown is ongoing...\n");
Sleep(INFINITE);
return;
}
/* First notify DOS to see whether we can shut down now */
MustShutdown = DosShutdown(Immediate);
/*
* In case we perform an immediate shutdown, or the DOS says
* we can shut down, do it now.
*/
MustShutdown = MustShutdown || Immediate;
if (MustShutdown)
{
EmulatorTerminate();
BiosCleanup();
EmulatorCleanup();
ConsoleCleanup();
FreeGlobalSettings(&GlobalSettings);
DPRINT1("\n\n\nNTVDM - Exiting...\n\n\n");
/* Some VDDs rely on the fact that NTVDM calls ExitProcess on Windows */
ExitProcess(0);
}
}
/* PUBLIC FUNCTIONS ***********************************************************/
VOID
DisplayMessage(IN LPCWSTR Format, ...)
{
#ifndef WIN2K_COMPLIANT
WCHAR StaticBuffer[256];
LPWSTR Buffer = StaticBuffer; // Use the static buffer by default.
#else
WCHAR Buffer[2048]; // Large enough. If not, increase it by hand.
#endif
size_t MsgLen;
va_list args;
va_start(args, Format);
#ifndef WIN2K_COMPLIANT
/*
* Retrieve the message length and if it is too long, allocate
* an auxiliary buffer; otherwise use the static buffer.
* The string is built to be NULL-terminated.
*/
MsgLen = _vscwprintf(Format, args);
if (MsgLen >= ARRAYSIZE(StaticBuffer))
{
Buffer = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, (MsgLen + 1) * sizeof(WCHAR));
if (Buffer == NULL)
{
/* Allocation failed, use the static buffer and display a suitable error message */
Buffer = StaticBuffer;
Format = L"DisplayMessage()\nOriginal message is too long and allocating an auxiliary buffer failed.";
MsgLen = wcslen(Format);
}
}
#else
MsgLen = ARRAYSIZE(Buffer) - 1;
#endif
RtlZeroMemory(Buffer, (MsgLen + 1) * sizeof(WCHAR));
_vsnwprintf(Buffer, MsgLen, Format, args);
va_end(args);
/* Display the message */
DPRINT1("\n\nNTVDM Subsystem\n%S\n\n", Buffer);
MessageBoxW(hConsoleWnd, Buffer, L"NTVDM Subsystem", MB_OK);
#ifndef WIN2K_COMPLIANT
/* Free the buffer if needed */
if (Buffer != StaticBuffer) RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
#endif
}
/*
* This function, derived from DisplayMessage, is used by the BIOS and
* the DOS to display messages to an output device. A printer function
* is given for printing the characters.
*/
VOID
PrintMessageAnsi(IN CHAR_PRINT CharPrint,
IN LPCSTR Format, ...)
{
static CHAR CurChar = 0;
LPSTR str;
#ifndef WIN2K_COMPLIANT
CHAR StaticBuffer[256];
LPSTR Buffer = StaticBuffer; // Use the static buffer by default.
#else
CHAR Buffer[2048]; // Large enough. If not, increase it by hand.
#endif
size_t MsgLen;
va_list args;
va_start(args, Format);
#ifndef WIN2K_COMPLIANT
/*
* Retrieve the message length and if it is too long, allocate
* an auxiliary buffer; otherwise use the static buffer.
* The string is built to be NULL-terminated.
*/
MsgLen = _vscprintf(Format, args);
if (MsgLen >= ARRAYSIZE(StaticBuffer))
{
Buffer = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, (MsgLen + 1) * sizeof(CHAR));
if (Buffer == NULL)
{
/* Allocation failed, use the static buffer and display a suitable error message */
Buffer = StaticBuffer;
Format = "DisplayMessageAnsi()\nOriginal message is too long and allocating an auxiliary buffer failed.";
MsgLen = strlen(Format);
}
}
#else
MsgLen = ARRAYSIZE(Buffer) - 1;
#endif
RtlZeroMemory(Buffer, (MsgLen + 1) * sizeof(CHAR));
_vsnprintf(Buffer, MsgLen, Format, args);
va_end(args);
/* Display the message */
// DPRINT1("\n\nNTVDM DOS32\n%s\n\n", Buffer);
MsgLen = strlen(Buffer);
str = Buffer;
while (MsgLen--)
{
if (*str == '\n' && CurChar != '\r')
CharPrint('\r');
CurChar = *str++;
CharPrint(CurChar);
}
#ifndef WIN2K_COMPLIANT
/* Free the buffer if needed */
if (Buffer != StaticBuffer) RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
#endif
}
INT
wmain(INT argc, WCHAR *argv[])
{
NtVdmArgc = argc;
NtVdmArgv = argv;
#ifdef STANDALONE
if (argc < 2)
{
wprintf(L"\nReactOS Virtual DOS Machine\n\n"
L"Usage: NTVDM <executable> [<parameters>]\n");
return 0;
}
#endif
[NTVDM] - Add some level of "Advanced debugging" (see ntvdm.h) which one can adjust to enable/disable debugging features inside NTVDM (this can be useful as long as NTVDM is under heavy bugfixing. When it will be more perfect, this stuff will be removed). - Add the possibility to load option ROMs at a given segment. Currently their list should be specified from inside ntvdm.c (in the BiosInitialize call), but I plan to make it available from a registry option (or via command-line for NTVDM-standalone mode). - Start to separate the initialization of "static" BIOS data (stuff that lives in ROM space) and initialization of "dynamic" BIOS data (eg. initializing the interrupt vector table, the BIOS data area at segment 40h, ...) so that we may be able to reuse part of our code to be able to more-or-less run external (16-bit) BIOSes, or the Windows NTVDM BIOS that uses BOPs to run some of its stuff in ntvdm in 32-bit (as we do for our 32-bit BIOS, except that *all* of our bios is 32-bit, not just some parts). Also, some file reorganization will be in order there soon... - Add video BIOS version information in memory so that tools such as Microsoft Diagnostics can correctly recognize our video BIOS (btw, we try to emulate the functionality of Cirrus' CL-GD5434). - Correctly put video BIOS ROM header (+ checksum) in memory so that it is recognized as such by diagnostics tools. - During BIOS POST, scan for ROMs starting segment 0xC000 (where video ROMs reside). - Store statically the BIOS configuration table. - Fix INT 16h, AH=12h "Get extended shift states" so that it correctly returns the state of right Ctrl and Alt keys. - Fix bit-setting state; report that our keyboard is 101/102 enhanced keyboard. - Correctly set the error return values (AH=86h and CF set) when a function of INT 15h is unsupported. - Implement INT 15h, AH=C9h "Get CPU Type and Mask Revision"; INT 1Ah, AH=02h "Get Real-Time Clock Time" and Ah=04h "Get Real-Time Clock Date" by reading the CMOS. - Implement CMOS century register support. svn path=/trunk/; revision=68598
2015-08-04 20:17:05 +00:00
#ifdef ADVANCED_DEBUGGING
{
INT i = 20;
printf("Waiting for debugger (10 secs)..");
while (i--)
{
printf(".");
if (IsDebuggerPresent())
{
DbgBreakPoint();
break;
}
Sleep(500);
}
printf("Continue\n");
}
#endif
/* Load the global VDM settings */
LoadGlobalSettings(&GlobalSettings);
DPRINT1("\n\n\nNTVDM - Starting...\n\n\n");
/* Initialize the console */
if (!ConsoleInit())
{
wprintf(L"FATAL: A problem occurred when trying to initialize the console\n");
goto Cleanup;
}
/* Initialize the emulator */
[NTVDM] - Move all the hardware initialization to EmulatorInitialize (since emulator.c can be viewed as support functions for emulating a PC motherboard) --> PS2 and VGA go there. - Break bios.c into bios.c and kbdbios.c (the keyboard bios module) (according to the IBM documentation as well as other emulator sources or SeaBIOS or...). - Move Exception handling from int32.c to emulator.c, because it's something tight to the emulator, not to the interrupt system by itself (yet it happens that INT 00h to 07h are commonly set to some exception handlers). In the bios.c, initialize those vectors with the default exception handler. - Handling IRQs is done fully in bios.c now: introduce PicSetIRQMask and EnableHwIRQ helper functions (adapted from their equivalents from SeaBIOS) that allows the bios to set (and activate in the PIC) a given IRQ with its corresponding handler. Also introduce PicIRQComplete that serves as a PIC IRQ completer (i.e. sends the EOI to the right PIC(s)). - Continuing on that, at the moment I set dumb default PIC IRQ handlers for IRQ 08h - 0Fh and IRQ 70h - 77h). - By default I disable all the IRQs; there are then set on-demand with EnableHwIRQ. - Rework the POST (aka. BiosInitialize function): * the memory size is now get from the CMOS (as well as the extended memory size via INT 12h, AH=88h), * then we initialize the interrupts, * then platform hardware (ie. the chips) are initialized, * and finally the keyboard and video bioses. - As said before, move memory sizes into the CMOS. - Simplify video bios initialization. svn path=/branches/ntvdm/; revision=61796
2014-01-25 00:21:51 +00:00
if (!EmulatorInitialize(ConsoleInput, ConsoleOutput))
{
wprintf(L"FATAL: Failed to initialize the emulator\n");
goto Cleanup;
}
[NTVDM] - Add some level of "Advanced debugging" (see ntvdm.h) which one can adjust to enable/disable debugging features inside NTVDM (this can be useful as long as NTVDM is under heavy bugfixing. When it will be more perfect, this stuff will be removed). - Add the possibility to load option ROMs at a given segment. Currently their list should be specified from inside ntvdm.c (in the BiosInitialize call), but I plan to make it available from a registry option (or via command-line for NTVDM-standalone mode). - Start to separate the initialization of "static" BIOS data (stuff that lives in ROM space) and initialization of "dynamic" BIOS data (eg. initializing the interrupt vector table, the BIOS data area at segment 40h, ...) so that we may be able to reuse part of our code to be able to more-or-less run external (16-bit) BIOSes, or the Windows NTVDM BIOS that uses BOPs to run some of its stuff in ntvdm in 32-bit (as we do for our 32-bit BIOS, except that *all* of our bios is 32-bit, not just some parts). Also, some file reorganization will be in order there soon... - Add video BIOS version information in memory so that tools such as Microsoft Diagnostics can correctly recognize our video BIOS (btw, we try to emulate the functionality of Cirrus' CL-GD5434). - Correctly put video BIOS ROM header (+ checksum) in memory so that it is recognized as such by diagnostics tools. - During BIOS POST, scan for ROMs starting segment 0xC000 (where video ROMs reside). - Store statically the BIOS configuration table. - Fix INT 16h, AH=12h "Get extended shift states" so that it correctly returns the state of right Ctrl and Alt keys. - Fix bit-setting state; report that our keyboard is 101/102 enhanced keyboard. - Correctly set the error return values (AH=86h and CF set) when a function of INT 15h is unsupported. - Implement INT 15h, AH=C9h "Get CPU Type and Mask Revision"; INT 1Ah, AH=02h "Get Real-Time Clock Time" and Ah=04h "Get Real-Time Clock Date" by reading the CMOS. - Implement CMOS century register support. svn path=/trunk/; revision=68598
2015-08-04 20:17:05 +00:00
/* Initialize the system BIOS and option ROMs */
if (!BiosInitialize(GlobalSettings.BiosFileName.Buffer,
GlobalSettings.RomFiles.Buffer))
{
wprintf(L"FATAL: Failed to initialize the VDM BIOS.\n");
goto Cleanup;
}
/* Let's go! Start simulation */
CpuSimulate();
/* Quit the VDM */
Cleanup:
VdmShutdown(TRUE);
return 0;
}
/* EOF */