Skip to content

Commit

Permalink
add vcpu_t::user_data()
Browse files Browse the repository at this point in the history
Holds per-VCPU data assigned by the user.
  • Loading branch information
wbenny committed Oct 31, 2019
1 parent 1d6b3d0 commit fb0be3b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 17 deletions.
19 changes: 19 additions & 0 deletions src/hvpp/hvpp/hvpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,25 @@ HvppVcpuSuppressRipAdjust(
vcpu_->suppress_rip_adjust();
}

PVOID
NTAPI
HvppVcpuGetUserData(
_In_ PVCPU Vcpu
)
{
return vcpu_->user_data();
}

VOID
NTAPI
HvppVcpuSetUserData(
_In_ PVCPU Vcpu,
_In_ PVOID UserData
)
{
vcpu_->user_data(UserData);
}

#pragma endregion

//////////////////////////////////////////////////////////////////////////
Expand Down
13 changes: 13 additions & 0 deletions src/hvpp/hvpp/hvpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,19 @@ HvppVcpuSuppressRipAdjust(
_In_ PVCPU Vcpu
);

PVOID
NTAPI
HvppVcpuGetUserData(
_In_ PVCPU Vcpu
);

VOID
NTAPI
HvppVcpuSetUserData(
_In_ PVCPU Vcpu,
_In_ PVOID UserData
);

#pragma endregion

//////////////////////////////////////////////////////////////////////////
Expand Down
12 changes: 12 additions & 0 deletions src/hvpp/hvpp/vcpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ vcpu_t::vcpu_t(vmexit_handler& handler) noexcept
, tsc_delta_previous_{}
, tsc_delta_sum_{}

, user_data_{}

//
// Well, this is also not necessary.
// This member is reset to "false" on each VM-exit in entry_host() method.
Expand Down Expand Up @@ -423,6 +425,16 @@ void vcpu_t::suppress_rip_adjust() noexcept
suppress_rip_adjust_ = true;
}

auto vcpu_t::user_data() noexcept -> void*
{
return user_data_;
}

void vcpu_t::user_data(void* new_data) noexcept
{
user_data_ = new_data;
}

void vcpu_t::guest_resume() noexcept
{
resume_context_.rax = 1;
Expand Down
5 changes: 5 additions & 0 deletions src/hvpp/hvpp/vcpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class vcpu_t final
auto context() noexcept -> context_t&;
void suppress_rip_adjust() noexcept;

auto user_data() noexcept -> void*;
void user_data(void* data) noexcept;

//
// Guest helper methods.
//
Expand Down Expand Up @@ -459,6 +462,8 @@ class vcpu_t final
//
interrupt_queue_t pending_interrupt_queue_[interrupt_queue_max];

void* user_data_;

bool suppress_rip_adjust_;
};

Expand Down
33 changes: 16 additions & 17 deletions src/hvppdrv_c/vmexit_custom.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ typedef struct _PER_VCPU_DATA
PHYSICAL_ADDRESS PageExec;
} PER_VCPU_DATA, *PPER_VCPU_DATA;

PER_VCPU_DATA PerVcpuData[32];

VOID
NTAPI
Expand Down Expand Up @@ -50,30 +49,30 @@ HvppHandleExecuteVmcall(
PVCPU_CONTEXT Context = HvppVcpuContext(Vcpu);
PEPT Ept = HvppVcpuGetCurrentEpt(Vcpu);

PPER_VCPU_DATA Data = &PerVcpuData[KeGetCurrentProcessorNumberEx(NULL)];
PPER_VCPU_DATA UserData = (PPER_VCPU_DATA)(HvppVcpuGetUserData(Vcpu));

switch (Context->Rcx)
{
case 0xC1:
{
ULONG_PTR Cr3;
HvppAttachAddressSpace(&Cr3);
Data->PageRead = MmGetPhysicalAddress(Context->RdxAsPointer);
Data->PageExec = MmGetPhysicalAddress(Context->R8AsPointer);
UserData->PageRead = MmGetPhysicalAddress(Context->RdxAsPointer);
UserData->PageExec = MmGetPhysicalAddress(Context->R8AsPointer);
HvppDetachAddressSpace(Cr3);
}

HvppTrace("vmcall (hook) EXEC: 0x%p READ: 0x%p",
Data->PageExec.QuadPart,
Data->PageRead.QuadPart);
UserData->PageExec.QuadPart,
UserData->PageRead.QuadPart);

HvppEptSplit2MbTo4Kb(Ept,
EPT_PD_PAGE_ALIGN(Data->PageExec),
EPT_PD_PAGE_ALIGN(Data->PageExec));
EPT_PD_PAGE_ALIGN(UserData->PageExec),
EPT_PD_PAGE_ALIGN(UserData->PageExec));

HvppEptMap4Kb(Ept,
Data->PageExec,
Data->PageExec,
UserData->PageExec,
UserData->PageExec,
EPT_ACCESS_EXECUTE);

HvppInveptSingleContext(HvppEptGetEptPointer(Ept));
Expand All @@ -83,8 +82,8 @@ HvppHandleExecuteVmcall(
HvppTrace("vmcall (unhook)");

HvppEptJoin4KbTo2Mb(Ept,
EPT_PD_PAGE_ALIGN(Data->PageExec),
EPT_PD_PAGE_ALIGN(Data->PageExec));
EPT_PD_PAGE_ALIGN(UserData->PageExec),
EPT_PD_PAGE_ALIGN(UserData->PageExec));

HvppInveptSingleContext(HvppEptGetEptPointer(Ept));
break;
Expand Down Expand Up @@ -114,7 +113,7 @@ HvppHandleEptViolation(

PEPT Ept = HvppVcpuGetCurrentEpt(Vcpu);

PPER_VCPU_DATA Data = &PerVcpuData[KeGetCurrentProcessorNumberEx(NULL)];
PPER_VCPU_DATA UserData = (PPER_VCPU_DATA)(HvppVcpuGetUserData(Vcpu));

if (EptViolation.DataRead || EptViolation.DataWrite)
{
Expand All @@ -123,8 +122,8 @@ HvppHandleEptViolation(
GuestPhysicalAddress.QuadPart);

HvppEptMap4Kb(Ept,
Data->PageExec,
Data->PageRead,
UserData->PageExec,
UserData->PageRead,
EPT_ACCESS_READ_WRITE);
}
else if (EptViolation.DataExecute)
Expand All @@ -134,8 +133,8 @@ HvppHandleEptViolation(
GuestPhysicalAddress.QuadPart);

HvppEptMap4Kb(Ept,
Data->PageExec,
Data->PageExec,
UserData->PageExec,
UserData->PageExec,
EPT_ACCESS_EXECUTE);
}

Expand Down

0 comments on commit fb0be3b

Please sign in to comment.