HyperPlatform Programmer's Reference
power_callback.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2017, Satoshi Tanda. All rights reserved.
2 // Use of this source code is governed by a MIT-style license that can be
3 // found in the LICENSE file.
4 
7 
8 #include "power_callback.h"
9 #include "common.h"
10 #include "log.h"
11 #include "vm.h"
12 
13 extern "C" {
15 //
16 // macro utilities
17 //
18 
20 //
21 // constants and macros
22 //
23 
25 //
26 // types
27 //
28 
30 //
31 // prototypes
32 //
33 
34 static CALLBACK_FUNCTION PowerCallbackpCallbackRoutine;
35 
36 #if defined(ALLOC_PRAGMA)
37 #pragma alloc_text(INIT, PowerCallbackInitialization)
38 #pragma alloc_text(PAGE, PowerCallbackTermination)
39 #pragma alloc_text(PAGE, PowerCallbackpCallbackRoutine)
40 #endif
41 
43 //
44 // variables
45 //
46 
47 static PCALLBACK_OBJECT g_pcp_callback_object = nullptr;
48 static PVOID g_pcp_registration = nullptr;
49 
51 //
52 // implementations
53 //
54 
55 // Registers power callback
56 _Use_decl_annotations_ NTSTATUS PowerCallbackInitialization() {
57  PAGED_CODE();
58 
59  UNICODE_STRING name = RTL_CONSTANT_STRING(L"\\Callback\\PowerState");
60  OBJECT_ATTRIBUTES oa =
61  RTL_CONSTANT_OBJECT_ATTRIBUTES(&name, OBJ_CASE_INSENSITIVE);
62 
63  auto status = ExCreateCallback(&g_pcp_callback_object, &oa, FALSE, TRUE);
64  if (!NT_SUCCESS(status)) {
65  return status;
66  }
67 
68  g_pcp_registration = ExRegisterCallback(
70  if (!g_pcp_registration) {
71  ObDereferenceObject(g_pcp_callback_object);
72  g_pcp_callback_object = nullptr;
73  return STATUS_UNSUCCESSFUL;
74  }
75  return status;
76 }
77 
78 // Unregister power callback
79 _Use_decl_annotations_ void PowerCallbackTermination() {
80  PAGED_CODE();
81 
82  if (g_pcp_registration) {
83  ExUnregisterCallback(g_pcp_registration);
84  }
86  ObDereferenceObject(g_pcp_callback_object);
87  }
88 }
89 
90 // Power callback routine dealing with hibernate and sleep
91 _Use_decl_annotations_ static void PowerCallbackpCallbackRoutine(
92  PVOID callback_context, PVOID argument1, PVOID argument2) {
93  UNREFERENCED_PARAMETER(callback_context);
94  PAGED_CODE();
95 
96  HYPERPLATFORM_LOG_DEBUG("PowerCallback %p:%p", argument1, argument2);
97 
98  if (argument1 != reinterpret_cast<void*>(PO_CB_SYSTEM_STATE_LOCK)) {
99  return;
100  }
101 
103 
104  if (argument2) {
105  // the computer has just reentered S0.
106  HYPERPLATFORM_LOG_INFO("Resuming the system...");
107  auto status = VmInitialization();
108  if (!NT_SUCCESS(status)) {
110  "Failed to re-virtualize processors. Please unload the driver.");
111  }
112  } else {
113  // the computer is about to exit system power state S0
114  HYPERPLATFORM_LOG_INFO("Suspending the system...");
115  VmTermination();
116  }
117 }
118 
119 } // extern "C"
#define HYPERPLATFORM_LOG_INFO(format,...)
Definition: log.h:38
#define HYPERPLATFORM_LOG_DEBUG(format,...)
Logs a message as respective severity.
Definition: log.h:34
NTSTATUS VmInitialization()
Virtualizes all processors.
Definition: vm.cpp:143
NTSTATUS PowerCallbackInitialization()
void VmTermination()
De-virtualize all processors.
Definition: vm.cpp:890
#define HYPERPLATFORM_COMMON_DBG_BREAK()
Sets a break point that works only when a debugger is present.
Definition: common.h:55
void PowerCallbackTermination()
Declares interfaces to power functions.
static CALLBACK_FUNCTION PowerCallbackpCallbackRoutine
Declares and implements common things across the project.
static PCALLBACK_OBJECT g_pcp_callback_object
Declares interfaces to VMM initialization functions.
#define HYPERPLATFORM_LOG_ERROR(format,...)
Definition: log.h:46
Declares interfaces to logging functions.
static PVOID g_pcp_registration