diff --git a/proto/hypervisor.c b/proto/hypervisor.c index a39e9c3378ce26d85dba0e31eba876582370affe..12d2061c26d4e5860a54a1a49177f0cecd001e9b 100644 --- a/proto/hypervisor.c +++ b/proto/hypervisor.c @@ -12,27 +12,27 @@ #include <linux/slab.h> /*============== my includes ==============*/ -#include "asm/smp.h" -#include "asm/special_insns.h" -#include "asm/tlbflush.h" #include "debug/debug.h" -#include "linux/threads.h" #include "msr/msr.h" -#include "region/vxmon.h" -#include "vcpu/vcpu.h" +#include "vmm/vmm.h" #include "vmx/vmx.h" #define NULL ((void *)0) +#define NB_VCPUS 1 + /*asm ( assembler template*/ /* : output operands (optional)*/ /* : input operands (optional)*/ /* : clobbered registers list (optional)*/ /* );*/ -static struct vcpu_t vcpus[1]; +static struct vmm_t ctx; static int my_init(void) { + DEBUG_FMT("CPU id = %d\n", smp_processor_id()); + DEBUG_FMT("Nb available CPUs = %d\n", num_online_cpus()); + pr_info("Checking VMX support using CPUID\n"); if (!vmx_support_cpuid()) { pr_err("VMX isn't supported\n"); @@ -41,26 +41,15 @@ static int my_init(void) { DEBUG_FMT("IA32_VMX_BASIC_MSR = 0x%llx\n", __rdmsr(IA32_VMX_BASIC)); - pr_info("Allocating VMXON region\n"); + pr_info("Initializing VMM context\n"); - struct vmxon_t *vmxon_reg = alloc_vmxon(); + int ret_err; - if (!vmxon_reg) { - pr_err("VMXON region allocation failed\n"); - return -ENOMEM; + if ((ret_err = init_vmm(&ctx, 1)) != 0) { + pr_err("VMM initialization has failed\n"); + return ret_err; } - if (init_vcpu(&vcpus[0], vmxon_reg) != 0) { - pr_err("VCPU initialization failed\n"); - return -EFAULT; - } - - pr_info("VA of the allocated region = 0x%px\n", vmxon_reg); - pr_info("PA of the allocated region = 0x%lx\n", __pa(vmxon_reg)); - - pr_info("Reading VMXON region for VMCS ID: 0x%x\n", - vmxon_reg->header.vmcs_rev_id); - pr_info("Patching CR0 and CR4 depending on the value of their respective " "MSRs\n"); patch_control_registers(); @@ -70,6 +59,7 @@ static int my_init(void) { if ((ret_cr4_vmx = cr4_enable_vmx()) != 0) { pr_err("CPU not available, VMXE bit in CR4 has already been set\n"); + destroy_vmm(&ctx); return ret_cr4_vmx; } @@ -77,28 +67,20 @@ static int my_init(void) { if (!ia32_feature_control_flags()) { pr_err("The flags of the IA32_FEATURE_CONTROL MSR do not permit " "virtualization\n"); + destroy_vmm(&ctx); return -EPERM; } - pr_info("Executing VMXON with address = 0x%lx as its operand\n", - __pa(vmxon_reg)); - - unsigned char vmxon_ret = 0; + for (unsigned long i = 0; i < ctx.vcpu_count; i++) { + if ((ret_err = kvm_cpu_vmxon(__pa(&ctx.vcpu_table[i]))) != 0) { + pr_err("Failed to execute `vmxon` on vCPU[%lu]\n", i); + destroy_vmm(&ctx); + return ret_err; + } - /*if ((vmxon_ret = vmxon(vmxon_region.pa) != 0)) {*/ - if ((vmxon_ret = kvm_cpu_vmxon(__pa(vmxon_reg)) != 0)) { - /*unsigned long vm_err = __rdmsr(0x4400);*/ - /*pr_err("VM_ERR val = 0x%lx\n", vm_err);*/ - cr4_clear_bits(13); - - kfree(vmxon_reg); - /*__asm__ volatile("vmxoff");*/ - pr_err("`vmxon` failed with return code %d\n", vmxon_ret); - return -1; + pr_info("[+] vCPU[%lu] is in VMX operation\n", i); } - pr_info("`vmxon` was successfully executed!\n"); - return 0; } @@ -112,8 +94,8 @@ static void my_exit(void) { return; } - pr_info("Freeing memory of the VMXON region\n"); - kfree(vcpus[0].vmxon); + pr_info("[*] Freeing VMM ctx\n"); + destroy_vmm(&ctx); pr_info("vmbr.ko has exited\n"); }