From 3056fd2a7e0a8dd4e67a9507d98e57d1aac2c76d Mon Sep 17 00:00:00 2001
From: "iliya.saroukha" <iliya.saroukhanian@etu.hesge.ch>
Date: Sun, 2 Feb 2025 13:58:36 +0100
Subject: [PATCH] wip: drawing up basic VMM structure

---
 proto/vmm/vmm.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++
 proto/vmm/vmm.h | 12 +++++++++
 2 files changed, 80 insertions(+)
 create mode 100644 proto/vmm/vmm.c
 create mode 100644 proto/vmm/vmm.h

diff --git a/proto/vmm/vmm.c b/proto/vmm/vmm.c
new file mode 100644
index 0000000..7e63d0c
--- /dev/null
+++ b/proto/vmm/vmm.c
@@ -0,0 +1,68 @@
+#include "vmm.h"
+#include "../debug/debug.h"
+#include "linux/gfp_types.h"
+#include "linux/slab.h"
+#include "linux/smp.h"
+
+int init_vmm(struct vmm_t *ctx, unsigned long vcpu_count) {
+    if (!ctx) {
+        DEBUG_FMT("VMM context wasn't properly allocated!\n")
+        return -EFAULT;
+    }
+
+    ctx->vcpu_table = kzalloc(vcpu_count * sizeof(struct vcpu_t), GFP_KERNEL);
+
+    if (!ctx->vcpu_table) {
+        DEBUG_FMT("Allocation for the vcpu table has failed\n");
+        return -ENOMEM;
+    }
+
+    ctx->vcpu_count = vcpu_count;
+
+    int proc_id = smp_processor_id();
+    int ret = init_vcpu(&ctx->vcpu_table[proc_id], proc_id);
+
+    if (ret != 0) {
+        destroy_vmm(ctx);
+        DEBUG_FMT("vCPU[%d] initialization has failed\n", proc_id);
+        return ret;
+    }
+
+    DEBUG_FMT("VA of vCPU[%d]'s VMXON region: 0x%px\n", proc_id,
+              ctx->vcpu_table[proc_id].vmxon);
+    DEBUG_FMT("PA of vCPU[%d]'s VMXON region: 0x%lx\n", proc_id,
+              __pa(ctx->vcpu_table[proc_id].vmxon));
+
+    DEBUG_FMT("vCPU[%d]'s VMCS rev id: 0x%x\n", proc_id,
+              ctx->vcpu_table[proc_id].vmxon->vmcs_rev_id);
+
+    /*for (unsigned long i = 0; i < ctx->vcpu_count; i++) {*/
+    /*    int ret = init_vcpu(&ctx->vcpu_table[i], );*/
+    /**/
+    /*    if (ret != 0) {*/
+    /*        destroy_vmm(ctx);*/
+    /*        DEBUG_FMT("vCPU[%lu] initialization has failed\n", i);*/
+    /*        return ret;*/
+    /*    }*/
+    /**/
+    /*    DEBUG_FMT("VA of vCPU[%lu]'s VMXON region: 0x%px\n", i,*/
+    /*              ctx->vcpu_table[i].vmxon);*/
+    /*    DEBUG_FMT("PA of vCPU[%lu]'s VMXON region: 0x%lx\n", i,*/
+    /*              __pa(ctx->vcpu_table[i].vmxon));*/
+    /**/
+    /*    DEBUG_FMT("vCPU[%lu]'s VMCS rev id: 0x%x\n", i,*/
+    /*              ctx->vcpu_table[i].vmxon->vmcs_rev_id);*/
+    /*}*/
+
+    return 0;
+}
+
+void destroy_vmm(struct vmm_t *ctx) {
+    for (unsigned long i = 0; i < ctx->vcpu_count; i++) {
+        destroy_vcpu(&ctx->vcpu_table[i]);
+    }
+
+    kfree(ctx->vcpu_table);
+    /*BUG: prolly won't change the pointer itself */
+    ctx->vcpu_table = NULL;
+}
diff --git a/proto/vmm/vmm.h b/proto/vmm/vmm.h
new file mode 100644
index 0000000..ecdc583
--- /dev/null
+++ b/proto/vmm/vmm.h
@@ -0,0 +1,12 @@
+#include "../vcpu/vcpu.h"
+
+struct vmm_t {
+    /*vcpu_table has to be freed!*/
+    struct vcpu_t *vcpu_table;
+    unsigned long vcpu_count;
+    char stack[1 << 12];
+};
+
+int init_vmm(struct vmm_t *ctx, unsigned long vcpu_count);
+
+void destroy_vmm(struct vmm_t *ctx);
-- 
GitLab