2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: subsystems/mvdm/ntvdm/vddsup.c
5 * PURPOSE: Virtual Device Drivers (VDD) Support
6 * PROGRAMMERS: Hermes Belusca-Maito (hermes.belusca@sfr.fr)
9 /* INCLUDES *******************************************************************/
22 typedef VOID (WINAPI
*VDD_PROC
)(VOID
);
24 typedef struct _VDD_MODULE
27 VDD_PROC DispatchRoutine
;
28 } VDD_MODULE
, *PVDD_MODULE
;
30 // WARNING: A structure with the same name exists in nt_vdd.h,
31 // however it is not declared because its inclusion was prevented
32 // with #define NO_NTVDD_COMPAT, see ntvdm.h
33 typedef struct _VDD_USER_HANDLERS
38 PFNVDD_UCREATE Ucr_Handler
;
39 PFNVDD_UTERMINATE Uterm_Handler
;
40 PFNVDD_UBLOCK Ublock_Handler
;
41 PFNVDD_URESUME Uresume_Handler
;
42 } VDD_USER_HANDLERS
, *PVDD_USER_HANDLERS
;
44 /* PRIVATE VARIABLES **********************************************************/
46 // TODO: Maybe use a linked list.
47 // But the number of elements must be <= MAXUSHORT (MAXWORD)
48 #define MAX_VDD_MODULES 0xFF + 1
49 static VDD_MODULE VDDList
[MAX_VDD_MODULES
] = {{NULL
}};
51 // Valid handles of VDD DLLs start at 1 and finish at MAX_VDD_MODULES
52 #define ENTRY_TO_HANDLE(Entry) ((Entry) + 1)
53 #define HANDLE_TO_ENTRY(Handle) ((Handle) - 1)
54 #define IS_VALID_HANDLE(Handle) ((Handle) > 0 && (Handle) <= MAX_VDD_MODULES)
56 static LIST_ENTRY VddUserHooksList
= {&VddUserHooksList
, &VddUserHooksList
};
58 /* PRIVATE FUNCTIONS **********************************************************/
60 static USHORT
GetNextFreeVDDEntry(VOID
)
62 USHORT Entry
= MAX_VDD_MODULES
;
63 for (Entry
= 0; Entry
< ARRAYSIZE(VDDList
); ++Entry
)
65 if (VDDList
[Entry
].hDll
== NULL
) break;
70 static VOID WINAPI
ThirdPartyVDDBop(LPWORD Stack
)
72 /* Get the Function Number and skip it */
73 BYTE FuncNum
= *(PBYTE
)SEG_OFF_TO_PTR(getCS(), getIP());
84 LPCSTR DllName
= NULL
,
85 InitRoutineName
= NULL
,
86 DispatchRoutineName
= NULL
;
88 VDD_PROC InitRoutine
= NULL
,
89 DispatchRoutine
= NULL
;
91 DPRINT("RegisterModule() called\n");
93 /* Clear the Carry Flag (no error happened so far) */
96 /* Retrieve the next free entry in the table (used later on) */
97 Entry
= GetNextFreeVDDEntry();
98 if (Entry
>= MAX_VDD_MODULES
)
100 DPRINT1("Failed to create a new VDD module entry\n");
106 /* Retrieve the VDD name in DS:SI */
107 DllName
= (LPCSTR
)SEG_OFF_TO_PTR(getDS(), getSI());
109 /* Retrieve the initialization routine API name in ES:DI (optional --> ES=DI=0) */
110 if (TO_LINEAR(getES(), getDI()) != 0)
111 InitRoutineName
= (LPCSTR
)SEG_OFF_TO_PTR(getES(), getDI());
113 /* Retrieve the dispatch routine API name in DS:BX */
114 DispatchRoutineName
= (LPCSTR
)SEG_OFF_TO_PTR(getDS(), getBX());
116 DPRINT1("DllName = '%s' - InitRoutineName = '%s' - DispatchRoutineName = '%s'\n",
117 (DllName
? DllName
: "n/a"),
118 (InitRoutineName
? InitRoutineName
: "n/a"),
119 (DispatchRoutineName
? DispatchRoutineName
: "n/a"));
121 /* Load the VDD DLL */
122 hDll
= LoadLibraryA(DllName
);
125 DWORD LastError
= GetLastError();
128 if (LastError
== ERROR_NOT_ENOUGH_MEMORY
)
130 DPRINT1("Not enough memory to load DLL '%s'\n", DllName
);
136 DPRINT1("Failed to load DLL '%s'; last error = %d\n", DllName
, LastError
);
142 /* Load the initialization routine if needed */
145 InitRoutine
= (VDD_PROC
)GetProcAddress(hDll
, InitRoutineName
);
146 if (InitRoutine
== NULL
)
148 DPRINT1("Failed to load the initialization routine '%s'\n", InitRoutineName
);
155 /* Load the dispatch routine */
156 DispatchRoutine
= (VDD_PROC
)GetProcAddress(hDll
, DispatchRoutineName
);
157 if (DispatchRoutine
== NULL
)
159 DPRINT1("Failed to load the dispatch routine '%s'\n", DispatchRoutineName
);
165 /* If we reached this point, that means everything is OK */
167 /* Register the VDD DLL */
168 VDDList
[Entry
].hDll
= hDll
;
169 VDDList
[Entry
].DispatchRoutine
= DispatchRoutine
;
171 /* Call the initialization routine if needed */
172 if (InitRoutine
) InitRoutine();
174 /* We succeeded. RetVal will contain a valid VDD DLL handle */
176 RetVal
= ENTRY_TO_HANDLE(Entry
); // Convert the entry to a valid handle
181 /* Unload the VDD DLL */
182 if (hDll
) FreeLibrary(hDll
);
184 /* Set the Carry Flag to indicate that an error happened */
189 // /* Clear the Carry Flag (success) */
196 /* UnRegisterModule */
199 WORD Handle
= getAX();
200 WORD Entry
= HANDLE_TO_ENTRY(Handle
); // Convert the handle to a valid entry
202 DPRINT("UnRegisterModule() called\n");
205 if (!IS_VALID_HANDLE(Handle
) || VDDList
[Entry
].hDll
== NULL
)
207 DPRINT1("Invalid VDD DLL Handle: %d\n", Entry
);
213 /* Unregister the VDD DLL */
214 FreeLibrary(VDDList
[Entry
].hDll
);
215 VDDList
[Entry
].hDll
= NULL
;
216 VDDList
[Entry
].DispatchRoutine
= NULL
;
223 WORD Handle
= getAX();
224 WORD Entry
= HANDLE_TO_ENTRY(Handle
); // Convert the handle to a valid entry
226 DPRINT("DispatchCall() called\n");
229 if (!IS_VALID_HANDLE(Handle
) ||
230 VDDList
[Entry
].hDll
== NULL
||
231 VDDList
[Entry
].DispatchRoutine
== NULL
)
233 DPRINT1("Invalid VDD DLL Handle: %d\n", Entry
);
239 /* Call the dispatch routine */
240 VDDList
[Entry
].DispatchRoutine();
246 DPRINT1("Unknown 3rd-party VDD BOP Function: 0x%02X\n", FuncNum
);
253 static BOOL
LoadInstallableVDD(VOID
)
255 // FIXME: These strings should be localized.
256 #define ERROR_MEMORYVDD L"Insufficient memory to load installable Virtual Device Drivers."
257 #define ERROR_REGVDD L"Virtual Device Driver format in the registry is invalid."
258 #define ERROR_LOADVDD L"An installable Virtual Device Driver failed Dll initialization."
266 LPCWSTR VDDKeyName
= L
"SYSTEM\\CurrentControlSet\\Control\\VirtualDeviceDrivers";
267 LPWSTR VDDValueName
= L
"VDD";
268 LPWSTR VDDList
= NULL
;
272 /* Try to open the VDD registry key */
273 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
278 if (Error
== ERROR_FILE_NOT_FOUND
)
280 /* If the key just doesn't exist, don't do anything else */
283 else if (Error
!= ERROR_SUCCESS
)
285 /* The key exists but there was an access error: display an error and quit */
286 DisplayMessage(ERROR_REGVDD
);
291 * Retrieve the size of the VDD registry value
292 * and check that it's of REG_MULTI_SZ type.
294 Error
= RegQueryValueExW(hVDDKey
,
300 if (Error
== ERROR_FILE_NOT_FOUND
)
302 /* If the value just doesn't exist, don't do anything else */
306 else if (Error
!= ERROR_SUCCESS
|| Type
!= REG_MULTI_SZ
)
309 * The value exists but there was an access error or
310 * is of the wrong type: display an error and quit.
312 DisplayMessage(ERROR_REGVDD
);
317 /* Allocate the buffer */
318 BufSize
= (BufSize
< 2*sizeof(WCHAR
) ? 2*sizeof(WCHAR
) : BufSize
);
319 VDDList
= RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY
, BufSize
);
322 DisplayMessage(ERROR_MEMORYVDD
);
327 /* Retrieve the list of VDDs to load */
328 if (RegQueryValueExW(hVDDKey
,
333 &BufSize
) != ERROR_SUCCESS
)
335 DisplayMessage(ERROR_REGVDD
);
341 VDDValueName
= VDDList
;
344 DPRINT1("Loading VDD '%S'...", VDDList
);
345 hVDD
= LoadLibraryW(VDDList
);
348 DbgPrint("Failed\n");
349 DisplayMessage(ERROR_LOADVDD
);
353 DbgPrint("Succeeded\n");
355 /* Go to next string */
356 VDDList
+= wcslen(VDDList
) + 1;
358 VDDList
= VDDValueName
;
361 if (VDDList
) RtlFreeHeap(RtlGetProcessHeap(), 0, VDDList
);
362 RegCloseKey(hVDDKey
);
366 /* PUBLIC FUNCTIONS ***********************************************************/
369 * NOTE: This function can be called multiple times by the same VDD, if
370 * it wants to install different hooks for a same action. The most recent
371 * registered hooks are called first.
375 VDDInstallUserHook(IN HANDLE hVdd
,
376 IN PFNVDD_UCREATE Ucr_Handler
,
377 IN PFNVDD_UTERMINATE Uterm_Handler
,
378 IN PFNVDD_UBLOCK Ublock_Handler
,
379 IN PFNVDD_URESUME Uresume_Handler
)
381 PVDD_USER_HANDLERS UserHook
;
383 /* Check validity of the VDD handle */
384 if (hVdd
== NULL
|| hVdd
== INVALID_HANDLE_VALUE
)
386 SetLastError(ERROR_INVALID_PARAMETER
);
390 // NOTE: If we want that a VDD can install hooks only once, it's here
391 // that we need to check whether a hook entry is already registered.
393 /* Create and initialize a new hook entry... */
394 UserHook
= RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(*UserHook
));
395 if (UserHook
== NULL
)
397 SetLastError(ERROR_OUTOFMEMORY
);
401 UserHook
->hVdd
= hVdd
;
402 UserHook
->Ucr_Handler
= Ucr_Handler
;
403 UserHook
->Uterm_Handler
= Uterm_Handler
;
404 UserHook
->Ublock_Handler
= Ublock_Handler
;
405 UserHook
->Uresume_Handler
= Uresume_Handler
;
407 /* ... and add it at the top of the list of hooks */
408 InsertHeadList(&VddUserHooksList
, &UserHook
->Entry
);
414 * NOTE: This function uninstalls the latest installed hooks for a given VDD.
415 * It can be called multiple times by the same VDD to uninstall many hooks
416 * installed by multiple invocations of VDDInstallUserHook.
420 VDDDeInstallUserHook(IN HANDLE hVdd
)
423 PVDD_USER_HANDLERS UserHook
;
425 /* Check validity of the VDD handle */
426 if (hVdd
== NULL
|| hVdd
== INVALID_HANDLE_VALUE
)
428 SetLastError(ERROR_INVALID_PARAMETER
);
432 /* Uninstall the latest installed hooks */
433 for (Pointer
= VddUserHooksList
.Flink
; Pointer
!= &VddUserHooksList
; Pointer
= Pointer
->Flink
)
435 UserHook
= CONTAINING_RECORD(Pointer
, VDD_USER_HANDLERS
, Entry
);
436 if (UserHook
->hVdd
== hVdd
)
438 RemoveEntryList(&UserHook
->Entry
);
439 RtlFreeHeap(RtlGetProcessHeap(), 0, UserHook
);
444 SetLastError(ERROR_INVALID_PARAMETER
);
449 * Internal functions for calling the VDD user hooks.
450 * Their names come directly from the Windows 2kX DDK.
453 VOID
VDDCreateUserHook(USHORT DosPDB
)
456 PVDD_USER_HANDLERS UserHook
;
458 /* Call the hooks starting from the most recent ones */
459 for (Pointer
= VddUserHooksList
.Flink
; Pointer
!= &VddUserHooksList
; Pointer
= Pointer
->Flink
)
461 UserHook
= CONTAINING_RECORD(Pointer
, VDD_USER_HANDLERS
, Entry
);
462 if (UserHook
->Ucr_Handler
) UserHook
->Ucr_Handler(DosPDB
);
466 VOID
VDDTerminateUserHook(USHORT DosPDB
)
469 PVDD_USER_HANDLERS UserHook
;
471 /* Call the hooks starting from the most recent ones */
472 for (Pointer
= VddUserHooksList
.Flink
; Pointer
!= &VddUserHooksList
; Pointer
= Pointer
->Flink
)
474 UserHook
= CONTAINING_RECORD(Pointer
, VDD_USER_HANDLERS
, Entry
);
475 if (UserHook
->Uterm_Handler
) UserHook
->Uterm_Handler(DosPDB
);
479 VOID
VDDBlockUserHook(VOID
)
482 PVDD_USER_HANDLERS UserHook
;
484 /* Call the hooks starting from the most recent ones */
485 for (Pointer
= VddUserHooksList
.Flink
; Pointer
!= &VddUserHooksList
; Pointer
= Pointer
->Flink
)
487 UserHook
= CONTAINING_RECORD(Pointer
, VDD_USER_HANDLERS
, Entry
);
488 if (UserHook
->Ublock_Handler
) UserHook
->Ublock_Handler();
492 VOID
VDDResumeUserHook(VOID
)
495 PVDD_USER_HANDLERS UserHook
;
497 /* Call the hooks starting from the most recent ones */
498 for (Pointer
= VddUserHooksList
.Flink
; Pointer
!= &VddUserHooksList
; Pointer
= Pointer
->Flink
)
500 UserHook
= CONTAINING_RECORD(Pointer
, VDD_USER_HANDLERS
, Entry
);
501 if (UserHook
->Uresume_Handler
) UserHook
->Uresume_Handler();
507 VOID
VDDSupInitialize(VOID
)
509 /* Register the 3rd-party VDD BOP Handler */
510 RegisterBop(BOP_3RDPARTY
, ThirdPartyVDDBop
);
512 /* Load the installable VDDs from the registry */
513 LoadInstallableVDD();