HyperPlatform Programmer's Reference
hotplug_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 "hotplug_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 PROCESSOR_CALLBACK_FUNCTION HotplugCallbackpCallbackRoutine;
35 
36 #if defined(ALLOC_PRAGMA)
37 #pragma alloc_text(INIT, HotplugCallbackInitialization)
38 #pragma alloc_text(PAGE, HotplugCallbackTermination)
39 #pragma alloc_text(PAGE, HotplugCallbackpCallbackRoutine)
40 #endif
41 
43 //
44 // variables
45 //
46 
47 static PVOID g_hpp_callback_handle = nullptr;
48 
50 //
51 // implementations
52 //
53 
54 // Registers power callback
55 _Use_decl_annotations_ NTSTATUS HotplugCallbackInitialization() {
56  PAGED_CODE();
57 
58  auto callback_handle = KeRegisterProcessorChangeCallback(
60  if (!callback_handle) {
61  return STATUS_UNSUCCESSFUL;
62  }
63 
64  g_hpp_callback_handle = callback_handle;
65  return STATUS_SUCCESS;
66 }
67 
68 // Unregister power callback
69 _Use_decl_annotations_ void HotplugCallbackTermination() {
70  PAGED_CODE();
71 
73  KeDeregisterProcessorChangeCallback(g_hpp_callback_handle);
74  }
75 }
76 
77 _Use_decl_annotations_ static void HotplugCallbackpCallbackRoutine(
78  PVOID callback_context, PKE_PROCESSOR_CHANGE_NOTIFY_CONTEXT change_context,
79  PNTSTATUS operation_status) {
80  PAGED_CODE();
81  UNREFERENCED_PARAMETER(callback_context);
82  UNREFERENCED_PARAMETER(operation_status);
83 
84  if (change_context->State != KeProcessorAddCompleteNotify) {
85  return;
86  }
87 
88  HYPERPLATFORM_LOG_DEBUG("A new processor %hu:%hu has been added.",
89  change_context->ProcNumber.Group,
90  change_context->ProcNumber.Number);
92 
93  auto status = VmHotplugCallback(change_context->ProcNumber);
94  if (!NT_SUCCESS(status)) {
95  HYPERPLATFORM_LOG_ERROR("Failed to virtualize the new processors.");
96  }
97 }
98 
99 } // extern "C"
static PVOID g_hpp_callback_handle
#define HYPERPLATFORM_LOG_DEBUG(format,...)
Logs a message as respective severity.
Definition: log.h:34
#define HYPERPLATFORM_COMMON_DBG_BREAK()
Sets a break point that works only when a debugger is present.
Definition: common.h:55
NTSTATUS HotplugCallbackInitialization()
void HotplugCallbackTermination()
Declares interfaces to hot-plug functions.
NTSTATUS VmHotplugCallback(const PROCESSOR_NUMBER &proc_num)
Virtualizes the specified processor.
Definition: vm.cpp:997
Declares and implements common things across the project.
Declares interfaces to VMM initialization functions.
#define HYPERPLATFORM_LOG_ERROR(format,...)
Definition: log.h:46
Declares interfaces to logging functions.
static PROCESSOR_CALLBACK_FUNCTION HotplugCallbackpCallbackRoutine