Fix rdtsc patch

This commit is contained in:
Gregory Lirent 2024-08-03 21:05:54 +03:00
parent e8c8db62da
commit fc56578682
Signed by: lirent
GPG Key ID: F616BE337EFFEE77

View File

@ -0,0 +1,64 @@
diff -Naur --no-dereference a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
--- a/arch/x86/kvm/vmx/vmx.c 2024-07-01 21:03:34.000000000 +0300
+++ b/arch/x86/kvm/vmx/vmx.c 2024-07-01 20:24:05.000000000 +0300
@@ -2714,6 +2714,7 @@
vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
vmcs_conf->cpu_based_3rd_exec_ctrl = _cpu_based_3rd_exec_control;
- vmcs_conf->vmexit_ctrl = _vmexit_control;
+ vmcs_conf->vmexit_ctrl = _vmexit_control | CPU_BASED_RDTSC_EXITING;
vmcs_conf->vmentry_ctrl = _vmentry_control;
vmcs_conf->misc = misc_msr;
diff -Naur --no-dereference a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
--- a/arch/x86/kvm/vmx/vmx.c 2024-07-01 21:03:34.000000000 +0300
+++ b/arch/x86/kvm/vmx/vmx.c 2024-07-01 20:24:05.000000000 +0300
@@ -6128,6 +6128,7 @@
[EXIT_REASON_ENCLS] = handle_encls,
[EXIT_REASON_BUS_LOCK] = handle_bus_lock_vmexit,
[EXIT_REASON_NOTIFY] = handle_notify,
+ [EXIT_REASON_RDTSC] = handle_rdtsc,
};
static const int kvm_vmx_max_exit_handlers =
diff -Naur --no-dereference a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
--- a/arch/x86/kvm/vmx/vmx.c 2024-07-01 21:03:34.000000000 +0300
+++ b/arch/x86/kvm/vmx/vmx.c 2024-07-01 20:24:05.000000000 +0300
@@ -6070,6 +6070,36 @@
return 1;
}
+static int handle_rdtsc(struct kvm_vcpu *vcpu)
+{
+ static u32 _ = 1;
+ static u64 rdtsc_cur = 0;
+ static u64 rdtsc_prev = 0;
+ u64 _rdtsc = rdtsc();
+
+ printk_once("[HookEntry] fake rdtsc vmx function is working\n");
+
+ if (_)
+ {
+ rdtsc_cur = _rdtsc;
+ _ = 0;
+ }
+ if (rdtsc_prev != 0 && _rdtsc > rdtsc_prev)
+ {
+ rdtsc_cur += (_rdtsc - rdtsc_prev) / 16;
+ }
+
+ if (rdtsc_cur > _rdtsc)
+ {
+ rdtsc_cur = _rdtsc;
+ }
+
+ rdtsc_prev = rdtsc_cur;
+ vcpu->arch.regs[VCPU_REGS_RAX] = rdtsc_cur & -1u;
+ vcpu->arch.regs[VCPU_REGS_RDX] = (rdtsc_cur >> 32) & -1u;
+ return skip_emulated_instruction(vcpu);
+}
+
/*
* The exit handlers return 1 if the exit was handled fully and guest execution
* may resume. Otherwise they set the kvm_run parameter to indicate what needs