ReactOS  0.4.14-dev-52-g6116262
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 
20 INIT_FUNCTION
21 VOID
22 NTAPI
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 
39 INIT_FUNCTION
40 VOID
41 NTAPI
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);
60  Status = ZwOpenKey(&RegHandle, KEY_READ, &ObjectAttributes);
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),
70  &ReturnLength);
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 
87 NTAPI
88 VdmpInitialize(PVOID ControlData)
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,
120  &BaseAddress,
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 */
136  _SEH2_TRY
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  */
171 NTSTATUS
172 NTAPI
174  IN PVOID ControlData)
175 {
177  PAGED_CODE();
178 
179  /* Check which control code this is */
180  switch (ControlCode)
181  {
182  /* VDM Execution start */
183  case VdmStartExecution:
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 }
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
IN CINT OUT PVOID IN ULONG OUT PULONG ReturnLength
Definition: dumpinfo.c:39
__INTRIN_INLINE void __writecr4(unsigned int Data)
Definition: intrin_x86.h:1680
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
struct _KEY_VALUE_BASIC_INFORMATION KEY_VALUE_BASIC_INFORMATION
#define KEY_READ
Definition: nt_native.h:1023
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
LONG NTSTATUS
Definition: precomp.h:26
_In_ ULONGLONG _In_ ULONGLONG _In_ BOOLEAN Enable
Definition: ntddpcm.h:140
#define KeGetPcr()
Definition: ke.h:25
#define PAGED_CODE()
Definition: video.h:57
#define RtlMoveMemory(Destination, Source, Length)
Definition: typedefs.h:263
_SEH2_TRY
Definition: create.c:4250
NTSTATUS NTAPI NtVdmControl(IN ULONG ControlCode, IN PVOID ControlData)
Definition: vdmmain.c:173
INIT_FUNCTION VOID NTAPI KeI386VdmInitialize(VOID)
Definition: vdmmain.c:42
#define SECTION_ALL_ACCESS
Definition: nt_native.h:1293
__INTRIN_INLINE uintptr_t __readeflags(void)
Definition: intrin_x86.h:1555
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
struct NameRec_ * Name
Definition: cdprocs.h:464
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
NTSTATUS NTAPI VdmpInitialize(PVOID ControlData)
Definition: vdmmain.c:88
_IRQL_requires_same_ typedef _In_ ULONG ControlCode
Definition: wmitypes.h:55
_In_opt_ PETWENABLECALLBACK _In_opt_ PVOID _Out_ PREGHANDLE RegHandle
Definition: wmifuncs.h:76
#define NtCurrentProcess()
Definition: nt_native.h:1657
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define CR4_VME
Definition: ketypes.h:84
#define KF_V86_VIS
Definition: ketypes.h:143
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
unsigned char UCHAR
Definition: xmlstorage.h:181
static const WCHAR L[]
Definition: oid.c:1250
__INTRIN_INLINE void __writeeflags(uintptr_t Value)
Definition: intrin_x86.h:1550
#define PAGE_SIZE
Definition: env_spec_w32.h:49
__INTRIN_INLINE unsigned long __readcr4(void)
Definition: intrin_x86.h:1713
Status
Definition: gdiplustypes.h:24
INIT_FUNCTION VOID NTAPI Ki386VdmEnablePentiumExtentions(IN BOOLEAN Enable)
Definition: vdmmain.c:23
_SEH2_END
Definition: create.c:4424
#define DPRINT1
Definition: precomp.h:8
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:404
void __cdecl _disable(void)
Definition: intrin_arm.h:365
unsigned int ULONG
Definition: retypes.h:1
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
NTSYSAPI NTSTATUS NTAPI ZwOpenSection(_Out_ PHANDLE SectionHandle, _In_ ACCESS_MASK DesiredAccess, _In_ POBJECT_ATTRIBUTES ObjectAttributes)
return STATUS_SUCCESS
Definition: btrfs.c:2966
BOOLEAN KeI386VirtualIntExtensions
Definition: v86vdm.c:24
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
NTSTATUS NTAPI VdmpStartExecution(VOID)
Definition: vdmexec.c:171
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14