HyperPlatform Programmer's Reference
driver.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 #ifndef POOL_NX_OPTIN
9 #define POOL_NX_OPTIN 1
10 #endif
11 #include "driver.h"
12 #include "common.h"
13 #include "global_object.h"
14 #include "hotplug_callback.h"
15 #include "log.h"
16 #include "power_callback.h"
17 #include "util.h"
18 #include "vm.h"
19 #include "performance.h"
20 
21 extern "C" {
23 //
24 // macro utilities
25 //
26 
28 //
29 // constants and macros
30 //
31 
33 //
34 // types
35 //
36 
38 //
39 // prototypes
40 //
41 
42 DRIVER_INITIALIZE DriverEntry;
43 
44 static DRIVER_UNLOAD DriverpDriverUnload;
45 
46 _IRQL_requires_max_(PASSIVE_LEVEL) bool DriverpIsSuppoetedOS();
47 
48 #if defined(ALLOC_PRAGMA)
49 #pragma alloc_text(INIT, DriverEntry)
50 #pragma alloc_text(PAGE, DriverpDriverUnload)
51 #pragma alloc_text(INIT, DriverpIsSuppoetedOS)
52 #endif
53 
55 //
56 // variables
57 //
58 
60 //
61 // implementations
62 //
63 
64 // A driver entry point
65 _Use_decl_annotations_ NTSTATUS DriverEntry(PDRIVER_OBJECT driver_object,
66  PUNICODE_STRING registry_path) {
67  UNREFERENCED_PARAMETER(registry_path);
68  PAGED_CODE();
69 
70  static const wchar_t kLogFilePath[] = L"\\SystemRoot\\HyperPlatform.log";
71  static const auto kLogLevel =
74 
75  auto status = STATUS_UNSUCCESSFUL;
76  driver_object->DriverUnload = DriverpDriverUnload;
78 
79  // Request NX Non-Paged Pool when available
80  ExInitializeDriverRuntime(DrvRtPoolNxOptIn);
81 
82  // Initialize log functions
83  bool need_reinitialization = false;
84  status = LogInitialization(kLogLevel, kLogFilePath);
85  if (status == STATUS_REINITIALIZATION_NEEDED) {
86  need_reinitialization = true;
87  } else if (!NT_SUCCESS(status)) {
88  return status;
89  }
90 
91  // Test if the system is supported
92  if (!DriverpIsSuppoetedOS()) {
94  return STATUS_CANCELLED;
95  }
96 
97  // Initialize global variables
98  status = GlobalObjectInitialization();
99  if (!NT_SUCCESS(status)) {
100  LogTermination();
101  return status;
102  }
103 
104  // Initialize perf functions
105  status = PerfInitialization();
106  if (!NT_SUCCESS(status)) {
108  LogTermination();
109  return status;
110  }
111 
112  // Initialize utility functions
113  status = UtilInitialization(driver_object);
114  if (!NT_SUCCESS(status)) {
115  PerfTermination();
117  LogTermination();
118  return status;
119  }
120 
121  // Initialize power callback
122  status = PowerCallbackInitialization();
123  if (!NT_SUCCESS(status)) {
124  UtilTermination();
125  PerfTermination();
127  LogTermination();
128  return status;
129  }
130 
131  // Initialize hot-plug callback
133  if (!NT_SUCCESS(status)) {
135  UtilTermination();
136  PerfTermination();
138  LogTermination();
139  return status;
140  }
141 
142  // Virtualize all processors
143  status = VmInitialization();
144  if (!NT_SUCCESS(status)) {
147  UtilTermination();
148  PerfTermination();
150  LogTermination();
151  return status;
152  }
153 
154  // Register re-initialization for the log functions if needed
155  if (need_reinitialization) {
156  LogRegisterReinitialization(driver_object);
157  }
158 
159  HYPERPLATFORM_LOG_INFO("The VMM has been installed.");
160  return status;
161 }
162 
163 // Unload handler
164 _Use_decl_annotations_ static void DriverpDriverUnload(
165  PDRIVER_OBJECT driver_object) {
166  UNREFERENCED_PARAMETER(driver_object);
167  PAGED_CODE();
168 
170 
171  VmTermination();
174  UtilTermination();
175  PerfTermination();
177  LogTermination();
178 }
179 
180 // Test if the system is one of supported OS versions
181 _Use_decl_annotations_ bool DriverpIsSuppoetedOS() {
182  PAGED_CODE();
183 
184  RTL_OSVERSIONINFOW os_version = {};
185  auto status = RtlGetVersion(&os_version);
186  if (!NT_SUCCESS(status)) {
187  return false;
188  }
189  if (os_version.dwMajorVersion != 6 && os_version.dwMajorVersion != 10) {
190  return false;
191  }
192  // 4-gigabyte tuning (4GT) should not be enabled
193  if (!IsX64() &&
194  reinterpret_cast<ULONG_PTR>(MmSystemRangeStart) != 0x80000000) {
195  return false;
196  }
197  return true;
198 }
199 
200 } // extern "C"
NTSTATUS LogInitialization(ULONG flag, const wchar_t *log_file_path)
Definition: log.cpp:159
#define HYPERPLATFORM_LOG_INFO(format,...)
Definition: log.h:38
Declares interfaces to global object functions.
NTSTATUS VmInitialization()
Virtualizes all processors.
Definition: vm.cpp:143
void LogTermination()
Terminates the log system. Should be called from a DriverUnload routine.
Definition: log.cpp:348
bool DriverpIsSuppoetedOS()
Definition: driver.cpp:181
NTSTATUS PowerCallbackInitialization()
void VmTermination()
De-virtualize all processors.
Definition: vm.cpp:890
Declares interfaces to performance measurement functions.
constexpr bool IsX64()
Checks if a system is x64.
Definition: common.h:128
#define HYPERPLATFORM_COMMON_DBG_BREAK()
Sets a break point that works only when a debugger is present.
Definition: common.h:55
static const auto kLogOptDisableFunctionName
For LogInitialization(). Do not log a current function name.
Definition: log.h:110
void PowerCallbackTermination()
NTSTATUS HotplugCallbackInitialization()
Declares interfaces to driver functions.
Declares interfaces to power functions.
void UtilTermination()
Frees all resources allocated for the sake of the Util functions.
Definition: util.cpp:170
DRIVER_INITIALIZE DriverEntry
Definition: driver.cpp:42
void HotplugCallbackTermination()
NTSTATUS GlobalObjectInitialization()
Calls all constructors and register all destructor.
constexpr bool IsReleaseBuild()
Checks if the project is compiled as Release.
Definition: common.h:138
static DRIVER_UNLOAD DriverpDriverUnload
Definition: driver.cpp:44
Declares interfaces to utility functions.
NTSTATUS PerfInitialization()
Makes HYPERPLATFORM_PERFORMANCE_MEASURE_THIS_SCOPE() ready for use.
Definition: performance.cpp:53
void PerfTermination()
Ends performance monitoring and outputs its results.
Definition: performance.cpp:73
Declares interfaces to hot-plug functions.
void GlobalObjectTermination()
Calls all destructors.
Declares and implements common things across the project.
Declares interfaces to VMM initialization functions.
void LogRegisterReinitialization(PDRIVER_OBJECT driver_object)
Definition: log.cpp:305
NTSTATUS UtilInitialization(PDRIVER_OBJECT driver_object)
Definition: util.cpp:142
Declares interfaces to logging functions.
static const auto kLogPutLevelInfo
For LogInitialization(). Enables ERROR, WARN and INFO levels of logs.
Definition: log.h:94
static const auto kLogPutLevelDebug
For LogInitialization(). Enables all levels of logs.
Definition: log.h:90