Skip to content
Snippets Groups Projects
Commit 73c7c7eb authored by iliya.saroukha's avatar iliya.saroukha :first_quarter_moon:
Browse files

wip: refacto and VMM work

parent ac93955c
No related branches found
No related tags found
No related merge requests found
...@@ -12,27 +12,27 @@ ...@@ -12,27 +12,27 @@
#include <linux/slab.h> #include <linux/slab.h>
/*============== my includes ==============*/ /*============== my includes ==============*/
#include "asm/smp.h"
#include "asm/special_insns.h"
#include "asm/tlbflush.h"
#include "debug/debug.h" #include "debug/debug.h"
#include "linux/threads.h"
#include "msr/msr.h" #include "msr/msr.h"
#include "region/vxmon.h" #include "vmm/vmm.h"
#include "vcpu/vcpu.h"
#include "vmx/vmx.h" #include "vmx/vmx.h"
#define NULL ((void *)0) #define NULL ((void *)0)
#define NB_VCPUS 1
/*asm ( assembler template*/ /*asm ( assembler template*/
/* : output operands (optional)*/ /* : output operands (optional)*/
/* : input operands (optional)*/ /* : input operands (optional)*/
/* : clobbered registers list (optional)*/ /* : clobbered registers list (optional)*/
/* );*/ /* );*/
static struct vcpu_t vcpus[1]; static struct vmm_t ctx;
static int my_init(void) { 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"); pr_info("Checking VMX support using CPUID\n");
if (!vmx_support_cpuid()) { if (!vmx_support_cpuid()) {
pr_err("VMX isn't supported\n"); pr_err("VMX isn't supported\n");
...@@ -41,26 +41,15 @@ static int my_init(void) { ...@@ -41,26 +41,15 @@ static int my_init(void) {
DEBUG_FMT("IA32_VMX_BASIC_MSR = 0x%llx\n", __rdmsr(IA32_VMX_BASIC)); 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();
if (!vmxon_reg) { int ret_err;
pr_err("VMXON region allocation failed\n");
return -ENOMEM;
}
if (init_vcpu(&vcpus[0], vmxon_reg) != 0) { if ((ret_err = init_vmm(&ctx, 1)) != 0) {
pr_err("VCPU initialization failed\n"); pr_err("VMM initialization has failed\n");
return -EFAULT; return ret_err;
} }
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 " pr_info("Patching CR0 and CR4 depending on the value of their respective "
"MSRs\n"); "MSRs\n");
patch_control_registers(); patch_control_registers();
...@@ -70,6 +59,7 @@ static int my_init(void) { ...@@ -70,6 +59,7 @@ static int my_init(void) {
if ((ret_cr4_vmx = cr4_enable_vmx()) != 0) { if ((ret_cr4_vmx = cr4_enable_vmx()) != 0) {
pr_err("CPU not available, VMXE bit in CR4 has already been set\n"); pr_err("CPU not available, VMXE bit in CR4 has already been set\n");
destroy_vmm(&ctx);
return ret_cr4_vmx; return ret_cr4_vmx;
} }
...@@ -77,27 +67,19 @@ static int my_init(void) { ...@@ -77,27 +67,19 @@ static int my_init(void) {
if (!ia32_feature_control_flags()) { if (!ia32_feature_control_flags()) {
pr_err("The flags of the IA32_FEATURE_CONTROL MSR do not permit " pr_err("The flags of the IA32_FEATURE_CONTROL MSR do not permit "
"virtualization\n"); "virtualization\n");
destroy_vmm(&ctx);
return -EPERM; return -EPERM;
} }
pr_info("Executing VMXON with address = 0x%lx as its operand\n", for (unsigned long i = 0; i < ctx.vcpu_count; i++) {
__pa(vmxon_reg)); if ((ret_err = kvm_cpu_vmxon(__pa(&ctx.vcpu_table[i]))) != 0) {
pr_err("Failed to execute `vmxon` on vCPU[%lu]\n", i);
unsigned char vmxon_ret = 0; 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("`vmxon` was successfully executed!\n"); pr_info("[+] vCPU[%lu] is in VMX operation\n", i);
}
return 0; return 0;
} }
...@@ -112,8 +94,8 @@ static void my_exit(void) { ...@@ -112,8 +94,8 @@ static void my_exit(void) {
return; return;
} }
pr_info("Freeing memory of the VMXON region\n"); pr_info("[*] Freeing VMM ctx\n");
kfree(vcpus[0].vmxon); destroy_vmm(&ctx);
pr_info("vmbr.ko has exited\n"); pr_info("vmbr.ko has exited\n");
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment