Commit c173727c authored by Jithu Joseph's avatar Jithu Joseph Committed by Vikram Narayanan
Browse files

LCD Using dynamically created stack for using shared IPC page

parent a06b7d57
......@@ -16,6 +16,17 @@
typedef unsigned int uint;
// The idea is to get the shared page which is
// to be used for IPC . It is the lowest page
// the bottom of the 4 Page Stack block.
// The address of which can be obtained by
// clearing 14 bits from stack pointer
static inline char* get_shared(void)
{
char *shared;
__asm__("and %%rsp,%0; ":"=r" (shared) : "0" (0xFFFFFFFFFFFFC000));
return shared;
}
unsigned long
strlen(const char *s)
......@@ -144,10 +155,14 @@ printf(int fd, char *fmt, ...)
}
void temp_fn() {
int check = 107;
printk(KERN_ERR "printk2 %d\n", check);
char *ptr;
// int check = 107;
// printk(KERN_ERR "printk2 %d\n", check);
printf(1, "temp func \n");
ptr = get_shared();
*ptr = 'J';
}
static volatile int shared_var = 0;
......
......@@ -717,6 +717,8 @@ static int vmx_setup_initial_page_table(lcd_struct *vcpu) {
return ret;
}
// commented to check the dynamically allocated stack
#if 0
/* Map stack PT */
gpa = gva = LCD_STACK_BOTTOM;
for (i = 0; i < (LCD_STACK_SIZE >> PAGE_SHIFT); ++i) {
......@@ -740,7 +742,7 @@ static int vmx_setup_initial_page_table(lcd_struct *vcpu) {
gva += PAGE_SIZE;
gpa += PAGE_SIZE;
}
#endif
/* Map descriptors and tables in EPT */
ret = ept_set_epte(vcpu, LCD_GDT_ADDR, __pa(vcpu->gdt), 0);
if (ret) {
......@@ -1336,7 +1338,8 @@ static void vmx_setup_initial_guest_state(lcd_struct *vcpu)
vmcs_writel(GUEST_IDTR_BASE, LCD_IDT_ADDR);
vmcs_writel(GUEST_IDTR_LIMIT, IDT_ENTRIES*16);
vmcs_writel(GUEST_RIP, LCD_TEST_CODE_ADDR);
vmcs_writel(GUEST_RSP, LCD_STACK_ADDR);
// commented out to test the dynamically allocated stack
// vmcs_writel(GUEST_RSP, LCD_STACK_ADDR);
vmcs_writel(GUEST_RFLAGS, 0x02);
vmcs_writel(GUEST_DR7, 0);
......@@ -1890,19 +1893,26 @@ int lcd_destroy(lcd_struct* lcd) {
}
EXPORT_SYMBOL(lcd_destroy);
static int lcd_va_to_pa(void* va, void** pa) {
*(unsigned long*)pa = vmalloc_to_pfn(va)<<PAGE_SHIFT;
static int lcd_va_to_pa(void* va, void** pa, int vmallocd) {
if (vmallocd == 1) {
*(unsigned long*)pa = vmalloc_to_pfn(va)<<PAGE_SHIFT;
} else {
*(unsigned long*)pa = virt_to_phys(va);
}
return 0;
}
static int __move_host_mapping(lcd_struct *lcd, void* hva,
unsigned int size) {
unsigned int size, int vmallocd) {
unsigned int mapped = 0;
void *pa;
void *va = (void*)round_down(((unsigned long)hva), PAGE_SIZE);
int ret = 0;
printk(KERN_ERR "mapping base - 0x%p (phys 0x%p)(phys2 %p) size %d\n", hva, (vmalloc_to_pfn(va)<<PAGE_SHIFT),virt_to_phys(va),size);
while (mapped < size) {
ret = lcd_va_to_pa(va, &pa);
ret = lcd_va_to_pa(va, &pa, vmallocd);
if (ret != 0) {
return ret;
}
......@@ -1923,18 +1933,49 @@ static int __move_host_mapping(lcd_struct *lcd, void* hva,
return 0;
}
static char *my_shared;
static int lcd_setup_stack(lcd_struct *lcd) {
char *sp = NULL;
char *stack_top = NULL;
int ret = 0;
printk (KERN_ERR "lcd : Entered lcd_setup_stack\n");
// allocate a few pages for LCD stack from
// Linux kernel memory allocator
sp = __get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
if (!sp) {
return -ENOMEM;
}
my_shared = sp;
stack_top = (sp + (PAGE_SIZE * 4) - 1);
printk (KERN_ERR "lcd : stack bootm %p , stack top %p and myshare %p", sp, stack_top, my_shared);
// map the stack in the LCD address space
ret = __move_host_mapping(lcd, (void *)sp , (PAGE_SIZE * 4), 0);
if (ret != 0) {
printk(KERN_ERR "lcd: Unable to map the dynamically allocate stack into LCD space\n");
return ret;
}
//setup the stack
vmcs_writel(GUEST_RSP, stack_top);
return 0;
}
int lcd_move_module(lcd_struct *lcd, struct module *mod) {
int ret;
lcd->mod = mod;
// lcd_va_to_pa(va, pa) // 4KB page assumption
ret = __move_host_mapping(lcd, mod->module_init, mod->init_size);
ret = __move_host_mapping(lcd, mod->module_init, mod->init_size, 1);
if (!ret) {
ret = __move_host_mapping(lcd, mod->module_core, mod->core_size);
ret = __move_host_mapping(lcd, mod->module_core, mod->core_size, 1);
if (!ret) {
vmcs_writel(GUEST_RIP, (unsigned long)mod->module_init);
}
}
lcd_setup_stack(lcd);
printk(KERN_ERR "lcd: failed to move module %d\n", ret);
......@@ -1996,7 +2037,8 @@ static void vmx_handle_external_interrupt(lcd_struct *lcd) {
}
static void vmx_handle_vmcall(lcd_struct *lcd) {
/* printk(KERN_INFO "lcd: got vmcall %llu\n", lcd->regs[VCPU_REGS_RAX]); */
//printk(KERN_ERR "lcd_run: got vmcall %llu and %c\n", lcd->regs[VCPU_REGS_RAX], lcd->regs[VCPU_REGS_RAX]);
printk(KERN_ERR "%c", lcd->regs[VCPU_REGS_RAX]);
}
static void vmx_handle_page_fault(lcd_struct *lcd) {
......@@ -2075,7 +2117,7 @@ int lcd_run(lcd_struct *lcd) {
ret = vmx_run_vcpu(lcd);
if (ret == EXIT_REASON_VMCALL ||
ret == EXIT_REASON_CPUID) {
/* vmx_step_instruction(); */
vmx_step_instruction();
}
if (ret == EXIT_REASON_EXTERNAL_INTERRUPT) {
......@@ -2085,10 +2127,12 @@ int lcd_run(lcd_struct *lcd) {
vmx_put_cpu(lcd);
if (ret == EXIT_REASON_VMCALL)
vmx_handle_vmcall(lcd);
else if (ret == EXIT_REASON_EPT_VIOLATION)
if (ret == EXIT_REASON_VMCALL) {
vmx_handle_vmcall(lcd);
}
else if (ret == EXIT_REASON_EPT_VIOLATION) {
done = vmx_handle_ept_violation(lcd);
}
else if (ret == EXIT_REASON_EXCEPTION_NMI) {
if (vmx_handle_nmi_exception(lcd))
done = 1;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment