From 5479548a31d8f4ee360f6c1d595db2d0babe8bbb Mon Sep 17 00:00:00 2001
From: "iliya.saroukha" <iliya.saroukhanian@etu.hesge.ch>
Date: Sun, 2 Feb 2025 13:58:15 +0100
Subject: [PATCH] feat: adding vmcs to vcpu structure

---
 proto/vcpu/vcpu.c | 26 ++++++++++++++++++++++----
 proto/vcpu/vcpu.h |  9 ++++++++-
 2 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/proto/vcpu/vcpu.c b/proto/vcpu/vcpu.c
index 8f367a4..7822986 100644
--- a/proto/vcpu/vcpu.c
+++ b/proto/vcpu/vcpu.c
@@ -1,21 +1,39 @@
 #include "vcpu.h"
 #include "../debug/debug.h"
 #include "../region/vxmon.h"
-#include "linux/string.h"
+#include "linux/slab.h"
 #include <linux/errno.h>
 
-int init_vcpu(struct vcpu_t *vcpu, struct vmxon_t *vmxon) {
+int init_vcpu(struct vcpu_t *vcpu, unsigned long id) {
     if (!vcpu) {
         DEBUG_FMT("vcpu isn't allocated\n");
         return -EFAULT;
     }
 
+    struct vmxon_t *vmxon = alloc_vmxon();
+
     if (!vmxon) {
-        DEBUG_FMT("vmxon region isn't allocated\n");
-        return -EFAULT;
+        DEBUG_FMT("VMXON region allocation failed\n");
+        return -ENOMEM;
+    }
+
+    struct vmcs_t *vmcs = alloc_vmcs();
+
+    if (!vmcs) {
+        DEBUG_FMT("VMCS region allocation failed\n");
+        return -ENOMEM;
     }
 
     vcpu->vmxon = vmxon;
+    vcpu->vmcs = vmcs;
+    vcpu->id = id;
 
     return 0;
 }
+
+void destroy_vcpu(struct vcpu_t *vcpu) {
+    kfree(vcpu->vmxon);
+    kfree(vcpu->vmcs);
+    /*BUG: prolly won't change the pointer itself */
+    vcpu = NULL;
+}
diff --git a/proto/vcpu/vcpu.h b/proto/vcpu/vcpu.h
index a61e34d..3851dda 100644
--- a/proto/vcpu/vcpu.h
+++ b/proto/vcpu/vcpu.h
@@ -1,12 +1,19 @@
 #pragma once
 
+#include "../region/vmcs.h"
 #include "../region/vxmon.h"
 
 struct vcpu_t {
+    unsigned long id;
+    /*vmxon region has to be freed!*/
     struct vmxon_t *vmxon;
+    /*vmcs region has to be freed!*/
+    struct vmcs_t *vmcs;
     /*NOTE: prolly should store the PAs of pointers to avoid unnecessary
      * address translations, although they should reside in the TLB by that
      * point*/
 };
 
-int init_vcpu(struct vcpu_t *vcpu, struct vmxon_t *vmxon);
+int init_vcpu(struct vcpu_t *vcpu, unsigned long id);
+
+void destroy_vcpu(struct vcpu_t *vcpu);
-- 
GitLab