ReactOS 0.4.15-dev-7934-g1dc8d80
vdmmain.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/vdm/vdmmain.c
5 * PURPOSE: VDM Support Services
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 * Aleksey Bragin (aleksey@reactos.org)
8 */
9
10/* INCLUDES ******************************************************************/
11
12#include <ntoskrnl.h>
13#define NDEBUG
14#include <debug.h>
15
16/* GLOBALS *******************************************************************/
17
18/* PRIVATE FUNCTIONS *********************************************************/
19
20CODE_SEG("INIT")
21VOID
24{
25 ULONG EFlags, Cr4;
26
27 /* Save interrupt state and disable them */
28 EFlags = __readeflags();
29 _disable();
30
31 /* Enable or disable VME as required */
32 Cr4 = __readcr4();
33 __writecr4(Enable ? Cr4 | CR4_VME : Cr4 & ~CR4_VME);
34
35 /* Restore interrupt state */
36 __writeeflags(EFlags);
37}
38
39CODE_SEG("INIT")
40VOID
43{
48 UCHAR KeyValueInfo[sizeof(KEY_VALUE_BASIC_INFORMATION) + 30];
50
51 /* Make sure that there is a WOW key */
53 L"\\Registry\\Machine\\System\\CurrentControlSet\\"
54 L"Control\\Wow");
56 &Name,
58 NULL,
59 NULL);
61 if (!NT_SUCCESS(Status)) return;
62
63 /* Check if VME is enabled */
64 RtlInitUnicodeString(&Name, L"DisableVme");
65 Status = ZwQueryValueKey(RegHandle,
66 &Name,
68 &KeyValueInfo,
69 sizeof(KeyValueInfo),
71 if (!NT_SUCCESS(Status))
72 {
73 /* Not present, so check if the CPU supports VME */
74 if (KeGetPcr()->Prcb->FeatureBits & KF_V86_VIS)
75 {
76 /* Enable them. FIXME: Use IPI */
79 }
80 }
81
82 /* Close the key */
84}
85
89{
91 UNICODE_STRING PhysMemName = RTL_CONSTANT_STRING(L"\\Device\\PhysicalMemory");
93 HANDLE PhysMemHandle;
95 volatile PVOID NullAddress = NULL;
98
99 /* Open the physical memory section */
101 &PhysMemName,
103 NULL,
104 NULL);
105 Status = ZwOpenSection(&PhysMemHandle,
108 if (!NT_SUCCESS(Status))
109 {
110 DPRINT1("Couldn't open \\Device\\PhysicalMemory\n");
111 return Status;
112 }
113
114 /* Map the BIOS and device registers into the address space */
115 Offset.QuadPart = 0;
117 BaseAddress = 0;
118 Status = ZwMapViewOfSection(PhysMemHandle,
121 0,
122 ViewSize,
123 &Offset,
124 &ViewSize,
125 ViewUnmap,
126 0,
128 if (!NT_SUCCESS(Status))
129 {
130 DPRINT1("Couldn't map physical memory (%x)\n", Status);
131 ZwClose(PhysMemHandle);
132 return Status;
133 }
134
135 /* Enter SEH */
137 {
138 /* Copy the first physical page into the first virtual page */
139 RtlMoveMemory(NullAddress, BaseAddress, ViewSize);
140 }
142 {
143 /* Fail */
144 DPRINT1("Couldn't copy first page (%x)\n", Status);
145 ZwClose(PhysMemHandle);
146 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
148 }
149 _SEH2_END;
150
151 /* Close physical memory section handle */
152 ZwClose(PhysMemHandle);
153
154 /* Unmap the section */
155 Status = ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
156
157 if (!NT_SUCCESS(Status))
158 {
159 DPRINT1("Couldn't unmap the section (%x)\n", Status);
160 return Status;
161 }
162
163 return STATUS_SUCCESS;
164}
165
166/* PUBLIC FUNCTIONS **********************************************************/
167
168/*
169 * @implemented
170 */
172NTAPI
174 IN PVOID ControlData)
175{
177 PAGED_CODE();
178
179 /* Check which control code this is */
180 switch (ControlCode)
181 {
182 /* VDM Execution start */
184
185 /* Call the sub-function */
187 break;
188
189 case VdmInitialize:
190
191 /* Call the init sub-function */
192 Status = VdmpInitialize(ControlData);
193 break;
194
195 default:
196
197 /* Unsupported */
198 DPRINT1("Unknown VDM call: %lx\n", ControlCode);
200 }
201
202 /* Return the status */
203 return Status;
204}
#define PAGED_CODE()
#define CODE_SEG(...)
unsigned char BOOLEAN
struct NameRec_ * Name
Definition: cdprocs.h:460
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
IN CINT OUT PVOID IN ULONG OUT PULONG ReturnLength
Definition: dumpinfo.c:43
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define _SEH2_END
Definition: filesup.c:22
#define _SEH2_TRY
Definition: filesup.c:19
Status
Definition: gdiplustypes.h:25
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
void __cdecl _disable(void)
Definition: intrin_arm.h:365
__INTRIN_INLINE void __writeeflags(uintptr_t Value)
Definition: intrin_x86.h:1669
__INTRIN_INLINE unsigned long __readcr4(void)
Definition: intrin_x86.h:1825
__INTRIN_INLINE uintptr_t __readeflags(void)
Definition: intrin_x86.h:1674
__INTRIN_INLINE void __writecr4(unsigned int Data)
Definition: intrin_x86.h:1799
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define CR4_VME
Definition: ketypes.h:145
#define KeGetPcr()
Definition: ketypes.h:81
#define KF_V86_VIS
Definition: ketypes.h:30
@ VdmInitialize
Definition: ketypes.h:475
@ VdmStartExecution
Definition: ketypes.h:472
NTSYSAPI NTSTATUS NTAPI ZwOpenSection(_Out_ PHANDLE SectionHandle, _In_ ACCESS_MASK DesiredAccess, _In_ POBJECT_ATTRIBUTES ObjectAttributes)
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:408
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
struct _KEY_VALUE_BASIC_INFORMATION KEY_VALUE_BASIC_INFORMATION
@ KeyValueBasicInformation
Definition: nt_native.h:1180
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define SECTION_ALL_ACCESS
Definition: nt_native.h:1293
#define KEY_READ
Definition: nt_native.h:1023
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define NtCurrentProcess()
Definition: nt_native.h:1657
@ ViewUnmap
Definition: nt_native.h:1279
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
_In_ ULONGLONG _In_ ULONGLONG _In_ BOOLEAN Enable
Definition: ntddpcm.h:142
BOOLEAN KeI386VirtualIntExtensions
Definition: v86vdm.c:24
NTSTATUS NTAPI VdmpStartExecution(VOID)
Definition: vdmexec.c:171
#define L(x)
Definition: ntvdm.h:50
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:159
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:34
#define _SEH2_YIELD(__stmt)
Definition: pseh2_64.h:162
#define STATUS_SUCCESS
Definition: shellext.h:65
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
#define NTAPI
Definition: typedefs.h:36
#define IN
Definition: typedefs.h:39
#define RtlMoveMemory(Destination, Source, Length)
Definition: typedefs.h:264
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
VOID NTAPI Ki386VdmEnablePentiumExtentions(IN BOOLEAN Enable)
Definition: vdmmain.c:23
NTSTATUS NTAPI NtVdmControl(IN ULONG ControlCode, IN PVOID ControlData)
Definition: vdmmain.c:173
NTSTATUS NTAPI VdmpInitialize(PVOID ControlData)
Definition: vdmmain.c:88
VOID NTAPI KeI386VdmInitialize(VOID)
Definition: vdmmain.c:42
_In_opt_ PETWENABLECALLBACK _In_opt_ PVOID _Out_ PREGHANDLE RegHandle
Definition: wmifuncs.h:78
_IRQL_requires_same_ typedef _In_ ULONG ControlCode
Definition: wmitypes.h:55
unsigned char UCHAR
Definition: xmlstorage.h:181