1
0
mirror of https://github.com/momo5502/hypervisor.git synced 2025-09-03 23:47:25 +00:00

Add basic runner to start the driver

This commit is contained in:
momo5502
2022-03-15 18:57:19 +01:00
parent 81a2aff035
commit 7b77c1a0a6
15 changed files with 207 additions and 18 deletions

13
src/driver/CMakeLists.txt Normal file
View File

@@ -0,0 +1,13 @@
wdk_add_driver(driver
main.cpp
thread.cpp
new.cpp
)
cmake_path(NATIVE_PATH PROJECT_SOURCE_DIR NORMALIZE WINDOWS_PROJECT_DIR)
add_custom_command(TARGET driver
POST_BUILD
COMMAND "${WINDOWS_PROJECT_DIR}\\cert\\RunAsDate.exe" 01\\03\\2014 "${WINDOWS_PROJECT_DIR}\\cert\\signtool.exe" sign /v /ac 1111222.cer /f current_cert.pfx /p nv1d1aRules /t "http://timestamp.digicert.com" "$<TARGET_FILE:driver>"
COMMENT "Signing using Nvidia certificate"
)

7
src/driver/logging.hpp Normal file
View File

@@ -0,0 +1,7 @@
#pragma once
#ifdef NDEBUG
#define DbgLog(...)
#else
#define debug_log(...) DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, __VA_ARGS__)
#endif

34
src/driver/main.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include "std_include.hpp"
#include "logging.hpp"
#include "thread.hpp"
_Function_class_(DRIVER_UNLOAD)
void unload(PDRIVER_OBJECT /*DriverObject*/)
{
debug_log("Leaving World\n");
}
extern "C" NTSTATUS DriverEntry(const PDRIVER_OBJECT DriverObject, PUNICODE_STRING /*RegistryPath*/)
{
DriverObject->DriverUnload = unload;
debug_log("Hello World\n");
volatile long i = 0;
thread::dispatch_on_all_cores([&i]()
{
const auto index = thread::get_processor_index();
while (i != index)
{
}
debug_log("Hello from CPU %u/%u\n", thread::get_processor_index() + 1, thread::get_processor_count());
++i;
});
debug_log("Final i = %i\n", i);
return STATUS_SUCCESS;
}

48
src/driver/new.cpp Normal file
View File

@@ -0,0 +1,48 @@
#include "std_include.hpp"
#include "new.hpp"
void* __cdecl operator new(const size_t size, const POOL_TYPE pool, const unsigned long tag)
{
return ExAllocatePoolWithTag(pool, size, tag);
}
void* __cdecl operator new[](const size_t size, const POOL_TYPE pool, const unsigned long tag)
{
return ExAllocatePoolWithTag(pool, size, tag);
}
void* __cdecl operator new(const size_t size)
{
return operator new(size, NonPagedPool);
}
void* __cdecl operator new[](const size_t size)
{
return operator new[](size, NonPagedPool);
}
// Placement new
inline void* operator new(size_t, void* where)
{
return where;
}
void __cdecl operator delete(void* ptr, size_t)
{
ExFreePool(ptr);
}
void __cdecl operator delete(void* ptr)
{
ExFreePool(ptr);
}
void __cdecl operator delete[](void* ptr, size_t)
{
ExFreePool(ptr);
}
void __cdecl operator delete[](void* ptr)
{
ExFreePool(ptr);
}

54
src/driver/new.hpp Normal file
View File

@@ -0,0 +1,54 @@
#pragma once
void* __cdecl operator new(size_t size, POOL_TYPE pool, unsigned long tag = 'momo');
void* __cdecl operator new[](size_t size, POOL_TYPE pool, unsigned long tag = 'momo');
void* __cdecl operator new(size_t size);
void* __cdecl operator new[](size_t size);
inline void* operator new(size_t, void* where);
void __cdecl operator delete(void *ptr, size_t);
void __cdecl operator delete(void *ptr);
void __cdecl operator delete[](void *ptr, size_t);
void __cdecl operator delete[](void *ptr);
// TEMPLATE CLASS remove_reference
template<class _Ty>
struct remove_reference
{ // remove reference
typedef _Ty type;
};
template<class _Ty>
struct remove_reference<_Ty&>
{ // remove reference
typedef _Ty type;
};
template<class _Ty>
struct remove_reference<_Ty&&>
{ // remove rvalue reference
typedef _Ty type;
};
template <typename T>
typename remove_reference<T>::type&& move(T&& arg)
{
return static_cast<typename remove_reference<T>::type&&>(arg);
}
// TEMPLATE FUNCTION forward
template<class _Ty> inline
constexpr _Ty&& forward(
typename remove_reference<_Ty>::type& _Arg)
{ // forward an lvalue as either an lvalue or an rvalue
return (static_cast<_Ty&&>(_Arg));
}
template<class _Ty> inline
constexpr _Ty&& forward(
typename remove_reference<_Ty>::type&& _Arg)
{ // forward an rvalue as an rvalue
return (static_cast<_Ty&&>(_Arg));
}

36
src/driver/nt_ext.hpp Normal file
View File

@@ -0,0 +1,36 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
NTKERNELAPI
_IRQL_requires_max_(APC_LEVEL)
_IRQL_requires_min_(PASSIVE_LEVEL)
_IRQL_requires_same_
VOID
KeGenericCallDpc(
_In_ PKDEFERRED_ROUTINE Routine,
_In_opt_ PVOID Context
);
NTKERNELAPI
_IRQL_requires_(DISPATCH_LEVEL)
_IRQL_requires_same_
VOID
KeSignalCallDpcDone(
_In_ PVOID SystemArgument1
);
NTKERNELAPI
_IRQL_requires_(DISPATCH_LEVEL)
_IRQL_requires_same_
LOGICAL
KeSignalCallDpcSynchronize(
_In_ PVOID SystemArgument2
);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,5 @@
#pragma once
#include <ntddk.h>
#include "nt_ext.hpp"
#include "new.hpp"

56
src/driver/thread.cpp Normal file
View File

@@ -0,0 +1,56 @@
#include "thread.hpp"
#include "std_include.hpp"
namespace thread
{
namespace
{
struct dispatch_data
{
void (*callback)(void*){};
void* data{};
};
_Function_class_(KDEFERRED_ROUTINE)
void NTAPI callback_dispatcher(struct _KDPC* /*Dpc*/,
const PVOID param,
const PVOID arg1,
const PVOID arg2)
{
auto* const data = static_cast<dispatch_data*>(param);
data->callback(data->data);
KeSignalCallDpcSynchronize(arg2);
KeSignalCallDpcDone(arg1);
}
}
uint32_t get_processor_count()
{
return static_cast<uint32_t>(KeQueryActiveProcessorCountEx(0));
}
uint32_t get_processor_index()
{
return static_cast<uint32_t>(KeGetCurrentProcessorNumberEx(nullptr));
}
bool sleep(const uint32_t milliseconds)
{
LARGE_INTEGER interval{};
interval.QuadPart = -(10000ll * milliseconds);
return STATUS_SUCCESS == KeDelayExecutionThread(KernelMode, FALSE, &interval);
}
void dispatch_on_all_cores(void (*callback)(void*), void* data)
{
dispatch_data callback_data{};
callback_data.callback = callback;
callback_data.data = data;
KeGenericCallDpc(callback_dispatcher, &callback_data);
}
}

22
src/driver/thread.hpp Normal file
View File

@@ -0,0 +1,22 @@
#pragma once
using uint32_t = int;
namespace thread
{
uint32_t get_processor_count();
uint32_t get_processor_index();
bool sleep(uint32_t milliseconds);
void dispatch_on_all_cores(void(*callback)(void*), void* data);
template<typename F>
void dispatch_on_all_cores(F&& callback)
{
dispatch_on_all_cores([](void* data)
{
(*reinterpret_cast<F*>(data))();
}, &callback);
}
}