mirror of
https://github.com/momo5502/hypervisor.git
synced 2025-07-02 01:01:57 +00:00
Register power callback
This commit is contained in:
@ -157,12 +157,93 @@ NTSTATUS create_io_device(const PDRIVER_OBJECT DriverObject)
|
||||
return Status;
|
||||
}
|
||||
|
||||
_Function_class_(CALLBACK_FUNCTION)
|
||||
VOID
|
||||
PowerCallback(
|
||||
_In_opt_ PVOID CallbackContext,
|
||||
_In_opt_ PVOID Argument1,
|
||||
_In_opt_ PVOID Argument2
|
||||
)
|
||||
{
|
||||
UNREFERENCED_PARAMETER(CallbackContext);
|
||||
|
||||
//
|
||||
// Ignore non-Sx changes
|
||||
//
|
||||
if (Argument1 != (PVOID)PO_CB_SYSTEM_STATE_LOCK)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Check if this is S0->Sx, or Sx->S0
|
||||
//
|
||||
if (ARGUMENT_PRESENT(Argument2))
|
||||
{
|
||||
//
|
||||
// Reload the hypervisor
|
||||
//
|
||||
debug_log("Waking up!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
// Unload the hypervisor
|
||||
//
|
||||
debug_log("Going to sleep!\n");
|
||||
}
|
||||
}
|
||||
|
||||
PVOID g_PowerCallbackRegistration{nullptr};
|
||||
|
||||
NTSTATUS register_sleep_callback()
|
||||
{
|
||||
PCALLBACK_OBJECT callbackObject;
|
||||
UNICODE_STRING callbackName =
|
||||
RTL_CONSTANT_STRING(L"\\Callback\\PowerState");
|
||||
OBJECT_ATTRIBUTES objectAttributes =
|
||||
RTL_CONSTANT_OBJECT_ATTRIBUTES(&callbackName,
|
||||
OBJ_CASE_INSENSITIVE |
|
||||
OBJ_KERNEL_HANDLE);
|
||||
|
||||
auto status = ExCreateCallback(&callbackObject, &objectAttributes, FALSE, TRUE);
|
||||
if (!NT_SUCCESS(status))
|
||||
{
|
||||
return status;
|
||||
}
|
||||
|
||||
//
|
||||
// Now register our routine with this callback
|
||||
//
|
||||
g_PowerCallbackRegistration = ExRegisterCallback(callbackObject,
|
||||
PowerCallback,
|
||||
NULL);
|
||||
|
||||
//
|
||||
// Dereference it in both cases -- either it's registered, so that is now
|
||||
// taking a reference, and we'll unregister later, or it failed to register
|
||||
// so we failing now, and it's gone.
|
||||
//
|
||||
ObDereferenceObject(callbackObject);
|
||||
|
||||
//
|
||||
// Fail if we couldn't register the power callback
|
||||
//
|
||||
if (g_PowerCallbackRegistration == NULL)
|
||||
{
|
||||
return STATUS_INSUFFICIENT_RESOURCES;
|
||||
}
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
_Function_class_(DRIVER_UNLOAD)
|
||||
|
||||
void unload(PDRIVER_OBJECT DriverObject)
|
||||
{
|
||||
debug_log("Leaving World\n");
|
||||
IrpUnloadHandler(DriverObject);
|
||||
ExUnregisterCallback(g_PowerCallbackRegistration);
|
||||
}
|
||||
|
||||
void throw_test()
|
||||
@ -198,7 +279,8 @@ extern "C" NTSTATUS DriverEntry(const PDRIVER_OBJECT DriverObject, PUNICODE_STRI
|
||||
debug_log("Final i = %i\n", i);
|
||||
|
||||
throw_test();
|
||||
|
||||
register_sleep_callback();
|
||||
|
||||
return create_io_device(DriverObject);
|
||||
|
||||
//return STATUS_SUCCESS;
|
||||
|
Reference in New Issue
Block a user