Bitdefender Hypervisor Memory Introspection
hook_xcr.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Bitdefender
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #include "hook_xcr.h"
6 #include "callbacks.h"
7 #include "guests.h"
8 
9 
12  _In_ DWORD Xcr,
13  _In_ DWORD Flags,
15  _In_opt_ void *Context,
16  _Out_opt_ HOOK_XCR **Hook
17  )
34 {
35  if (NULL == Callback)
36  {
38  }
39 
40  if (NULL != Hook)
41  {
42  *Hook = NULL;
43  }
44 
45  HOOK_XCR *pHook = HpAllocWithTag(sizeof(*pHook), IC_TAG_XCRH);
46  if (NULL == pHook)
47  {
49  }
50 
51  pHook->Callback = Callback;
52  pHook->Context = Context;
53  pHook->Disabled = FALSE;
54  pHook->Flags = Flags;
55  pHook->Xcr = Xcr;
56 
57  if (0 == gGuest.XcrHooks->HooksCount++)
58  {
60  if (!INT_SUCCESS(status))
61  {
63  return status;
64  }
65  }
66 
68 
69  if (NULL != Hook)
70  {
71  *Hook = pHook;
72  }
73 
74  return INT_STATUS_SUCCESS;
75 }
76 
77 
78 static INTSTATUS
80  _In_ HOOK_XCR *Hook
81  )
92 {
93  if (!Hook->Disabled)
94  {
96  }
97 
99 
100  if (0 >= --gGuest.XcrHooks->HooksCount)
101  {
103  }
104 
105  return INT_STATUS_SUCCESS;
106 }
107 
108 
109 INTSTATUS
111  _In_ HOOK_XCR *Hook
112  )
125 {
126  if (NULL == Hook)
127  {
129  }
130 
131  Hook->Disabled = TRUE;
132 
133  // If we're not handling an XCR violation right now than we can safely delete the XCR hook.
135  {
136  RemoveEntryList(&Hook->Link);
137 
138  INTSTATUS status = IntHookXcrDeleteHook(Hook);
139  if (!INT_SUCCESS(status))
140  {
141  ERROR("[ERROR] IntHookXcrDeleteHook failed: 0x%08x\n", status);
142  }
143  }
144 
145  return INT_STATUS_SUCCESS;
146 }
147 
148 
149 static void
151  void
152  )
156 {
158  {
159  INTSTATUS status = IntHookXcrRemoveHook(pHook);
160  if (!INT_SUCCESS(status))
161  {
162  ERROR("[ERROR] IntHookXcrRemoveHook failed: 0x%08x\n", status);
163  }
164  }
165 }
166 
167 
168 INTSTATUS
170  void
171  )
180 {
181  INTSTATUS status = INT_STATUS_SUCCESS;
182 
183  if (NULL == gGuest.XcrHooks)
184  {
186  }
187 
189  {
190  if (pHook->Disabled)
191  {
192  RemoveEntryList(&pHook->Link);
193 
194  status = IntHookXcrDeleteHook(pHook);
195  if (!INT_SUCCESS(status))
196  {
197  ERROR("[ERROR] IntHookXcrDeleteHook failed: 0x%08x\n", status);
198  }
199  }
200  }
201 
202  return status;
203 }
204 
205 
206 INTSTATUS
208  void
209  )
216 {
218  if (NULL == gGuest.XcrHooks)
219  {
221  }
222 
224 
225  return INT_STATUS_SUCCESS;
226 }
227 
228 
229 INTSTATUS
231  void
232  )
239 {
240  if (NULL == gGuest.XcrHooks)
241  {
243  }
244 
246 
248 
249  return INT_STATUS_SUCCESS;
250 }
#define _In_opt_
Definition: intro_sal.h:16
INTSTATUS IntHookXcrRemoveHook(HOOK_XCR *Hook)
Remove an extended control register hook.
Definition: hook_xcr.c:110
#define _In_
Definition: intro_sal.h:21
#define INT_STATUS_SUCCESS
Definition: introstatus.h:54
#define IC_TAG_XCRH
XCR hook.
Definition: memtags.h:67
#define INT_SUCCESS(Status)
Definition: introstatus.h:42
#define ERROR(fmt,...)
Definition: glue.h:62
INTSTATUS IntHookXcrCommit(void)
Commit the extended control register hooks.
Definition: hook_xcr.c:169
#define HpAllocWithTag(Len, Tag)
Definition: glue.h:516
int INTSTATUS
The status data type.
Definition: introstatus.h:24
DWORD Flags
Flags. Can be used by the caller.
Definition: hook_xcr.h:42
BOOLEAN Disabled
If true, the hook has been removed/disabled.
Definition: hook_xcr.h:44
#define _Out_opt_
Definition: intro_sal.h:30
INTSTATUS IntHookXcrUninit(void)
Uninit the extended control register hooks state.
Definition: hook_xcr.c:230
#define INT_STATUS_NOT_INITIALIZED
Definition: introstatus.h:266
static BOOLEAN RemoveEntryList(LIST_ENTRY *Entry)
Definition: introlists.h:87
XCR_HOOK_STATE * XcrHooks
XCR hook state.
Definition: guests.h:388
INTSTATUS IntHookXcrInit(void)
Initialize the extended control registers hook state.
Definition: hook_xcr.c:207
CPU_STATE State
The state of this VCPU. Describes what action is the VCPU currently doing.
Definition: guests.h:173
void * Context
Optional context.
Definition: hook_xcr.h:46
#define TRUE
Definition: intro_types.h:30
#define HpFreeAndNullWithTag(Add, Tag)
Definition: glue.h:517
static void InsertTailList(LIST_ENTRY *ListHead, LIST_ENTRY *Entry)
Definition: introlists.h:135
Handling XSETBV.
Definition: guests.h:28
static INTSTATUS IntDisableXcrNotifications(void)
Definition: callbacks.h:244
static void InitializeListHead(LIST_ENTRY *ListHead)
Definition: introlists.h:69
uint32_t DWORD
Definition: intro_types.h:49
#define IC_TAG_XCRS
XCR hook state.
Definition: memtags.h:68
GUEST_STATE gGuest
The current guest state.
Definition: guests.c:50
PFUNC_XcrWriteHookCallback Callback
Callback.
Definition: hook_xcr.h:45
static INTSTATUS IntEnableXcrNotifications(void)
Definition: callbacks.h:227
#define INT_STATUS_NOT_INITIALIZED_HINT
Definition: introstatus.h:320
#define INT_STATUS_INVALID_PARAMETER_1
Definition: introstatus.h:62
LIST_HEAD XcrHooksList
The list of XCR hooks.
Definition: hook_xcr.h:31
VCPU_STATE * gVcpu
The state of the current VCPU.
Definition: guests.c:59
static INTSTATUS IntHookXcrDeleteHook(HOOK_XCR *Hook)
Permanently delete an extended control register hook.
Definition: hook_xcr.c:79
LIST_ENTRY Link
List entry element.
Definition: hook_xcr.h:41
INT64 HooksCount
Total number of XCR hooks.
Definition: hook_xcr.h:32
DWORD Xcr
Intercepted XCR.
Definition: hook_xcr.h:43
#define list_for_each(_head, _struct_type, _var)
Definition: introlists.h:41
INTSTATUS(* PFUNC_XcrWriteHookCallback)(void *Context, DWORD Xcr, INTRO_ACTION *Action)
Extended control register write callback.
Definition: hook_xcr.h:19
static void IntHookXcrRemoveAllHooks(void)
Remove all extended control register write hooks.
Definition: hook_xcr.c:150
INTSTATUS IntHookXcrSetHook(DWORD Xcr, DWORD Flags, PFUNC_XcrWriteHookCallback Callback, void *Context, HOOK_XCR **Hook)
Set an extended control register write hook.
Definition: hook_xcr.c:11
#define FALSE
Definition: intro_types.h:34
#define INT_STATUS_INSUFFICIENT_RESOURCES
Definition: introstatus.h:281
#define INT_STATUS_INVALID_PARAMETER_3
Definition: introstatus.h:68