Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Kernel Shim Engine groundwork #2872

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions drivers/bus/pcix/pci.h
Expand Up @@ -1818,7 +1818,4 @@ extern BOOLEAN PciEnableNativeModeATA;
extern PPCI_IRQ_ROUTING_TABLE PciIrqRoutingTable;
extern BOOLEAN PciRunningDatacenter;

/* Exported by NTOS, should this go in the NDK? */
extern NTSYSAPI BOOLEAN InitSafeBootMode;

#endif /* _PCIX_PCH_ */
20 changes: 15 additions & 5 deletions media/sdb/CMakeLists.txt
@@ -1,7 +1,17 @@

add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/sysmain.sdb
COMMAND native-xml2sdb -i ${CMAKE_CURRENT_SOURCE_DIR}/sysmain.xml -o ${CMAKE_CURRENT_BINARY_DIR}/sysmain.sdb
set(SYSMAIN_SDB ${CMAKE_CURRENT_BINARY_DIR}/sysmain.sdb)
set(DRVMAIN_SDB ${CMAKE_CURRENT_BINARY_DIR}/drvmain.sdb)

add_custom_command(OUTPUT ${SYSMAIN_SDB}
COMMAND native-xml2sdb -i ${CMAKE_CURRENT_SOURCE_DIR}/sysmain.xml -o ${SYSMAIN_SDB}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/sysmain.xml native-xml2sdb)

add_custom_target(compatdb DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/sysmain.sdb)
add_cd_file(TARGET compatdb FILE ${CMAKE_CURRENT_BINARY_DIR}/sysmain.sdb DESTINATION reactos/AppPatch FOR all)

add_custom_command(OUTPUT ${DRVMAIN_SDB}
COMMAND native-xml2sdb -i ${CMAKE_CURRENT_SOURCE_DIR}/drvmain.xml -o ${DRVMAIN_SDB}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/drvmain.xml native-xml2sdb)

add_custom_target(compatdb DEPENDS
${SYSMAIN_SDB}
${DRVMAIN_SDB}
)
add_cd_file(TARGET compatdb FILE ${SYSMAIN_SDB} ${DRVMAIN_SDB} DESTINATION reactos/AppPatch FOR all)
45 changes: 45 additions & 0 deletions media/sdb/drvmain.xml
@@ -0,0 +1,45 @@
<SDB>
<DATABASE>
<NAME>ReactOS driver compatibility database</NAME>
<OS_PLATFORM >1</OS_PLATFORM><!-- 2 -->
<DATABASE_ID>{f9ab2228-3312-4a73-b6f9-936d70e112ef}</DATABASE_ID>

<!-- APPHELP blocking is not supported yet! -->
<EXE NAME="something.sys" APP_NAME="BuggyDriver" VENDOR="ACME">
<APPHELP>...</APPHELP>
<MATCHING_FILE NAME="*" />
</EXE>

<!-- -->
<KDRIVER NAME="beep.sys" APP_NAME="Beep driver" VENDOR="ACME">
<KSHIM_REF NAME="SkipDriverUnload" />
</KDRIVER>

<!-- KDEVICE is not supported yet! -->
<KDEVICE NAME="USB:USB\VID_0000&amp;PID_0000">
<FLAG NAME="USB">
<FLAG_MASK_KERNEL>1024</FLAG_MASK_KERNEL>
</FLAG>
</KDEVICE>

<KSHIM NAME="driverscope">
<MODULE>NT kernel component</MODULE>
</KSHIM>

<KSHIM NAME="KmWin7VersionLie">
<MODULE>NT kernel component</MODULE>
</KSHIM>

<KSHIM NAME="KmWin8VersionLie">
<MODULE>NT kernel component</MODULE>
</KSHIM>

<KSHIM NAME="KmWin81VersionLie">
<MODULE>NT kernel component</MODULE>
</KSHIM>

<KSHIM NAME="SkipDriverUnload">
<MODULE>NT kernel component</MODULE>
</KSHIM>
</DATABASE>
</SDB>
4 changes: 0 additions & 4 deletions ntoskrnl/config/cmboot.c
Expand Up @@ -13,10 +13,6 @@
#define NDEBUG
#include "debug.h"

/* GLOBALS ********************************************************************/

extern ULONG InitSafeBootMode;

/* FUNCTIONS ******************************************************************/

INIT_FUNCTION
Expand Down
1 change: 0 additions & 1 deletion ntoskrnl/config/ntapi.c
Expand Up @@ -15,7 +15,6 @@

BOOLEAN CmBootAcceptFirstTime = TRUE;
BOOLEAN CmFirstTime = TRUE;
extern ULONG InitSafeBootMode;


/* PRIVATE FUNCTIONS *********************************************************/
Expand Down
154 changes: 154 additions & 0 deletions ntoskrnl/include/internal/kse.h
@@ -0,0 +1,154 @@
/*
* PROJECT: ReactOS Kernel
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
* PURPOSE: Kernel Shim Engine types
* COPYRIGHT: Copyright 2020 Herv� Poussineau (hpoussin@reactos.org)
* COPYRIGHT: Copyright 2020 Mark Jansen (mark.jansen@reactos.org)
*/


#define KseHookFunction 0
#define KseHookIRPCallback 1
#define KseHookInvalid 2

#define KseHookCallbackDriverInit 1
#define KseHookCallbackDriverStartIo 2
#define KseHookCallbackDriverUnload 3
#define KseHookCallbackAddDevice 4
#define KseHookCallbackMajorFunction 100
Comment on lines +14 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to use enums for these?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly following existing style


typedef struct _KSE_HOOK
{
ULONG Type;
union
{
PCHAR FunctionName; // if Type == KseHookFunction
ULONG CallbackId; // if Type == KseHookIRPCallback, KseHookCallback..
};
PVOID HookFunction;
PVOID OriginalFunction; // if Type == KseHookFunction
} KSE_HOOK, *PKSE_HOOK;


#define KseCollectionNtExport 0
#define KseCollectionHalExport 1
#define KseCollectionDriverExport 2
#define KseCollectionCallback 3
#define KseCollectionInvalid 4

typedef struct _KSE_HOOK_COLLECTION
{
ULONG Type;
PWCHAR ExportDriverName; // if Type == KseCollectionDriverExport
PKSE_HOOK HookArray;
} KSE_HOOK_COLLECTION, *PKSE_HOOK_COLLECTION;


typedef VOID
(NTAPI *PKSE_HOOK_DRIVER_TARGETED)(
IN PUNICODE_STRING BaseName,
IN PVOID BaseAddress,
IN ULONG SizeOfImage,
IN ULONG TimeDateStamp,
IN ULONG CheckSum);

typedef VOID
(NTAPI *PKSE_HOOK_DRIVER_UNTARGETED)(
IN PVOID BaseAddress);


typedef struct _KSE_DRIVER_IO_CALLBACKS
{
PDRIVER_INITIALIZE DriverInit;
PDRIVER_STARTIO DriverStartIo;
PDRIVER_UNLOAD DriverUnload;
PDRIVER_ADD_DEVICE AddDevice;
PDRIVER_DISPATCH MajorFunction[IRP_MJ_MAXIMUM_FUNCTION + 1];
} KSE_DRIVER_IO_CALLBACKS, *PKSE_DRIVER_IO_CALLBACKS;


typedef PKSE_DRIVER_IO_CALLBACKS
(NTAPI KSE_GET_IO_CALLBACKS)(
IN PDRIVER_OBJECT DriverObject);
typedef KSE_GET_IO_CALLBACKS *PKSE_GET_IO_CALLBACKS;

typedef NTSTATUS
(NTAPI KSE_SET_COMPLETION_HOOK)(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp,
IN PIO_COMPLETION_ROUTINE CompletionRoutine,
IN PVOID Context);
typedef KSE_SET_COMPLETION_HOOK *PKSE_SET_COMPLETION_HOOK;


typedef struct _KSE_CALLBACK_ROUTINES
{
PKSE_GET_IO_CALLBACKS KseGetIoCallbacksRoutine;
PKSE_SET_COMPLETION_HOOK KseSetCompletionHookRoutine;
} KSE_CALLBACK_ROUTINES, *PKSE_CALLBACK_ROUTINES;

typedef struct _KSE_SHIM
{
ULONG Size;
const GUID* ShimGuid;
PWCHAR ShimName;
PKSE_CALLBACK_ROUTINES KseCallbackRoutines;
PKSE_HOOK_DRIVER_TARGETED ShimmedDriverTargetedNotification;
PKSE_HOOK_DRIVER_UNTARGETED ShimmedDriverUntargetedNotification;
PKSE_HOOK_COLLECTION HookCollectionsArray;
} *PKSE_SHIM, KSE_SHIM;


/* Exported functions */
//KseQueryDeviceData
//KseQueryDeviceDataList
//KseQueryDeviceFlags
//KseSetDeviceFlags

NTSTATUS
NTAPI
KseRegisterShim(
IN PKSE_SHIM Shim,
IN PVOID Unknown,
IN ULONG Flags);

NTSTATUS
NTAPI
KseRegisterShimEx(
IN PKSE_SHIM Shim,
IN PVOID Unknown,
IN ULONG Flags,
IN PVOID DriverObject OPTIONAL);

NTSTATUS
NTAPI
KseUnregisterShim(
IN PKSE_SHIM Shim,
IN PVOID Unknown1,
IN PVOID Unknown2);

/******************************************************* PRIVATE STUFF *****************************************/

NTSTATUS
NTAPI
KseInitialize(
IN ULONG BootPhase,
IN PLOADER_PARAMETER_BLOCK LoaderBlock);

NTSTATUS
NTAPI
KseShimDriverIoCallbacks(
IN PDRIVER_OBJECT DriverObject);

NTSTATUS
NTAPI
KseDriverLoadImage(
IN PLDR_DATA_TABLE_ENTRY LdrEntry);

NTSTATUS
NTAPI
KseVersionLieInitialize(VOID);

NTSTATUS
NTAPI
KseDriverScopeInitialize(VOID);
1 change: 1 addition & 0 deletions ntoskrnl/include/internal/ntoskrnl.h
Expand Up @@ -80,6 +80,7 @@
#include "hdl.h"
#include "arch/intrin_i.h"
#include <arbiter.h>
#include "kse.h"

/*
* generic information class probing code
Expand Down
3 changes: 3 additions & 0 deletions ntoskrnl/io/iomgr/driver.c
Expand Up @@ -896,6 +896,8 @@ IopInitializeBuiltinDriver(IN PLDR_DATA_TABLE_ENTRY BootLdrEntry)
}
ASSERT(NextEntry != &PsLoadedModuleList);

KseDriverLoadImage(LdrEntry);

/*
* Initialize the driver
*/
Expand Down Expand Up @@ -1658,6 +1660,7 @@ IopCreateDriver(IN PUNICODE_STRING DriverName OPTIONAL,
/* Returns to caller the object */
*pDriverObject = DriverObject;
}
KseShimDriverIoCallbacks(DriverObject);

/* We're going to say if we don't have any DOs from DriverEntry, then we're not legacy.
* Other parts of the I/O manager depend on this behavior */
Expand Down
6 changes: 6 additions & 0 deletions ntoskrnl/io/iomgr/iomgr.c
Expand Up @@ -535,6 +535,9 @@ IoInitSystem(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
return FALSE;
}

/* Initialize Kernel Shim engine */
KseInitialize(0, LoaderBlock);

/* Initialize PnP manager */
IopInitializePlugPlayServices();

Expand All @@ -544,6 +547,9 @@ IoInitSystem(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
/* Initialize WMI */
WmiInitialize();

/* Initialize Kernel Shim engine */
KseInitialize(1, LoaderBlock);

/* Initialize HAL Root Bus Driver */
HalInitPnpDriver();

Expand Down
20 changes: 20 additions & 0 deletions ntoskrnl/kse/driverscope.c
@@ -0,0 +1,20 @@
/*
* PROJECT: ReactOS Kernel
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
* PURPOSE: KSE 'DriverScope' shim implementation
* COPYRIGHT: Copyright 2020 Herv� Poussineau (hpoussin@reactos.org)
*/

#include <ntoskrnl.h>

//#define NDEBUG
#include <debug.h>

NTSTATUS
NTAPI
KseDriverScopeInitialize()
{
UNIMPLEMENTED_ONCE;

return STATUS_SUCCESS;
}