From f715c49b313c0c6ef4ceebc168c644c2f0544c8f Mon Sep 17 00:00:00 2001
From: "iliya.saroukha" <iliya.saroukhanian@etu.hesge.ch>
Date: Mon, 9 Dec 2024 23:55:58 +0100
Subject: [PATCH] semi-feat: sometimes vmxon executes successfully, most of the
 time the CF (Carry Flag) is set to 1..

---
 proto/hypervisor.c | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)

diff --git a/proto/hypervisor.c b/proto/hypervisor.c
index ee967a3..4eb4bcf 100644
--- a/proto/hypervisor.c
+++ b/proto/hypervisor.c
@@ -12,7 +12,6 @@
 
 /*============== my includes ==============*/
 #include "debug/debug.h"
-#include "linux/cleanup.h"
 #include "msr/msr.h"
 #include "region/vxmon_reg.h"
 
@@ -24,7 +23,7 @@
 /*    : clobbered registers list          (optional)*/
 /*    );*/
 
-static struct vmxon_reg_t vmxon_region;
+static struct vmxon_region_t vmxon_region;
 
 static void cr4_enable_vmx(void) {
     unsigned long cr4;
@@ -50,10 +49,16 @@ static bool vmx_support_cpuid(void) {
 static unsigned char vmxon(void *pa) {
     unsigned char ret;
 
-    __asm__ __volatile__("vmxon %[pa]; setna %[ret]"
-                         : [ret] "=rm"(ret)
-                         : [pa] "m"(vmxon_region.pa)
-                         : "cc", "memory");
+    __asm__ volatile("vmxon %[pa]; setna %[ret]"
+                     : [ret] "=rm"(ret)
+                     : [pa] "m"(pa)
+                     : "cc", "memory");
+
+    unsigned long long rflags;
+
+    __asm__ volatile("pushfq; popq %0" : "=r"(rflags));
+    DEBUG_FMT("RFLAGS: 0x%llx\n", rflags);
+
     return ret;
 }
 
@@ -86,8 +91,6 @@ static int my_init(void) {
     cr4_enable_vmx();
 
     pr_info("Checking the necessary flags of the IA32_FEATURE_CONTROL_MSR\n");
-    DEBUG_FMT("IA32_FEATURE_CONTROL = %llu\n",
-              __rdmsr(IA32_FEATURE_CONTROL_MSR));
     if (!ia32_feature_control_flags()) {
         pr_err("The flags of the IA32_FEATURE_CONTROL MSR do not permit "
                "virtualization\n");
@@ -97,30 +100,27 @@ static int my_init(void) {
     pr_info("Executing VMXON with address = 0x%px as its operand\n",
             vmxon_region.pa);
 
-    /*__asm__ volatile("vmxon %0" ::"m"(vmxon_region.pa) : "memory");*/
-
-    unsigned char vmxon_ret;
+    unsigned char vmxon_ret = 0;
 
     if ((vmxon_ret = vmxon(vmxon_region.pa) != 0)) {
-        pr_err("vmxon failed with return code %d\n", vmxon_ret);
+        kfree(vmxon_region.va);
+        pr_err("`vmxon` failed with return code %d\n", vmxon_ret);
         return -1;
     }
 
-    DEBUG_FMT("vmxon ret = %d\n", vmxon_ret);
+    pr_info("`vmxon` was successfully executed!\n");
 
     return 0;
 }
 
 static void my_exit(void) {
-    printk(KERN_INFO "Executing VMXOFF\n");
-
+    pr_info("Executing VMXOFF\n");
     __asm__ volatile("vmxoff");
 
-    printk(KERN_INFO "Freeing memory of the VMXON region\n");
-
+    pr_info("Freeing memory of the VMXON region\n");
     kfree(vmxon_region.va);
 
-    printk(KERN_INFO "Hypervisor has exited\n");
+    pr_info("vmbr.ko has exited\n");
 }
 
 module_init(my_init);
-- 
GitLab