Commit 3c809a98 authored by Charles Jacobsen's avatar Charles Jacobsen Committed by Vikram Narayanan

Fixed some bugs, wasn't setting parent lcd_arch.

parent b98e368f
...@@ -269,7 +269,7 @@ struct lcd_arch { ...@@ -269,7 +269,7 @@ struct lcd_arch {
* The guest physical address space is shared by all lcd_arch_thread's. * The guest physical address space is shared by all lcd_arch_thread's.
*/ */
struct { struct {
spinlock_t lock; struct mutex lock;
lcd_arch_epte_t *root; lcd_arch_epte_t *root;
u64 vmcs_ptr; /* to be loaded in vmcs EPT_POINTER field */ u64 vmcs_ptr; /* to be loaded in vmcs EPT_POINTER field */
bool access_dirty_enabled; bool access_dirty_enabled;
...@@ -313,7 +313,7 @@ struct lcd_arch_thread* lcd_arch_add_thread(struct lcd_arch *lcd_arch); ...@@ -313,7 +313,7 @@ struct lcd_arch_thread* lcd_arch_add_thread(struct lcd_arch *lcd_arch);
* Does logical consistency checks (e.g., runs through checks * Does logical consistency checks (e.g., runs through checks
* listed in Intel SDM V3 26.1, 26.2, and 26.3). * listed in Intel SDM V3 26.1, 26.2, and 26.3).
*/ */
int lcd_arch_check(struct lcd_arch *lcd_arch); int lcd_arch_check(struct lcd_arch_thread *t);
/** /**
* Tear down a lcd_arch_thread and remove it from its containing lcd_arch. * Tear down a lcd_arch_thread and remove it from its containing lcd_arch.
*/ */
...@@ -415,7 +415,7 @@ int lcd_arch_set_pc(struct lcd_arch_thread *t, gva_t a); ...@@ -415,7 +415,7 @@ int lcd_arch_set_pc(struct lcd_arch_thread *t, gva_t a);
* Set the lcd's stack pointer to the guest virtual address * Set the lcd's stack pointer to the guest virtual address
* a. * a.
*/ */
int lcd_arch_set_sp(struct lcd_arch_thread *t, gva_t a) int lcd_arch_set_sp(struct lcd_arch_thread *t, gva_t a);
/** /**
* Set the lcd's gva root pointer (for x86, %cr3) to the * Set the lcd's gva root pointer (for x86, %cr3) to the
* guest physical address a. * guest physical address a.
......
...@@ -129,7 +129,7 @@ fail5: ...@@ -129,7 +129,7 @@ fail5:
fail4: fail4:
lcd_arch_ept_unmap_range(lcd, __gpa(0), 1024); lcd_arch_ept_unmap_range(lcd, __gpa(0), 1024);
fail3: fail3:
lcd_arch_destory(lcd); lcd_arch_destroy(lcd);
fail1: fail1:
return ret; return ret;
} }
...@@ -165,8 +165,8 @@ static int test04(void) ...@@ -165,8 +165,8 @@ static int test04(void)
/* /*
* Set up an lcd_thread, added to lcd * Set up an lcd_thread, added to lcd
*/ */
lcd_thread = lcd_arch_add_thread(lcd); t = lcd_arch_add_thread(lcd);
if (!lcd_thread) { if (!t) {
LCD_ARCH_ERR("error setting up lcd_thread"); LCD_ARCH_ERR("error setting up lcd_thread");
goto fail4; goto fail4;
} }
...@@ -217,6 +217,7 @@ fail11: ...@@ -217,6 +217,7 @@ fail11:
fail10: fail10:
fail9: fail9:
fail8: fail8:
fail7:
fail6: fail6:
fail5: fail5:
lcd_arch_destroy_thread(t); lcd_arch_destroy_thread(t);
......
...@@ -23,6 +23,9 @@ ...@@ -23,6 +23,9 @@
#include <linux/tboot.h> #include <linux/tboot.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/kmsg_dump.h> #include <linux/kmsg_dump.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/kernel.h>
/* DEBUGGING -------------------------------------------------- */ /* DEBUGGING -------------------------------------------------- */
...@@ -939,6 +942,9 @@ void lcd_arch_exit(void) ...@@ -939,6 +942,9 @@ void lcd_arch_exit(void)
kmem_cache_destroy(lcd_arch_thread_cache); kmem_cache_destroy(lcd_arch_thread_cache);
} }
module_init(lcd_arch_init);
module_exit(lcd_arch_exit);
/* VMX EPT -------------------------------------------------- */ /* VMX EPT -------------------------------------------------- */
/** /**
...@@ -1276,7 +1282,7 @@ static void vmx_free_ept(struct lcd_arch *lcd) ...@@ -1276,7 +1282,7 @@ static void vmx_free_ept(struct lcd_arch *lcd)
* Initializes the EPT's root global page directory page, the * Initializes the EPT's root global page directory page, the
* VMCS pointer, and the spinlock. * VMCS pointer, and the spinlock.
*/ */
int vmx_init_ept(struct lcd_arch *lcd) int vmx_init_ept(struct lcd_arch *lcd_arch)
{ {
hva_t page; hva_t page;
u64 eptp; u64 eptp;
...@@ -1311,7 +1317,7 @@ int vmx_init_ept(struct lcd_arch *lcd) ...@@ -1311,7 +1317,7 @@ int vmx_init_ept(struct lcd_arch *lcd)
eptp |= VMX_EPT_AD_ENABLE_BIT; eptp |= VMX_EPT_AD_ENABLE_BIT;
} }
eptp |= hpa_val(va2hpa(lcd_arch->ept.root)) & PAGE_MASK; eptp |= hpa_val(va2hpa(lcd_arch->ept.root)) & PAGE_MASK;
lcd->ept.vmcs_ptr = eptp; lcd_arch->ept.vmcs_ptr = eptp;
/* /*
* Init the mutex * Init the mutex
...@@ -1855,7 +1861,7 @@ static void vmx_setup_vmcs_guest_settings(struct lcd_arch_thread *t, ...@@ -1855,7 +1861,7 @@ static void vmx_setup_vmcs_guest_settings(struct lcd_arch_thread *t,
/* /*
* VPID * VPID
*/ */
vmcs_write16(VIRTUAL_PROCESSOR_ID, vcpu->vpid); vmcs_write16(VIRTUAL_PROCESSOR_ID, t->vpid);
/* /*
* No VMCS Shadow (Intel SDM V3 24.4.2) * No VMCS Shadow (Intel SDM V3 24.4.2)
*/ */
...@@ -2099,7 +2105,7 @@ struct lcd_arch* lcd_arch_create(void) ...@@ -2099,7 +2105,7 @@ struct lcd_arch* lcd_arch_create(void)
* Set up list * Set up list
*/ */
mutex_init(&lcd_arch->lcd_arch_threads.lock); mutex_init(&lcd_arch->lcd_arch_threads.lock);
list_init(&lcd_arch->lcd_arch_threads.list); INIT_LIST_HEAD(&lcd_arch->lcd_arch_threads.list);
return lcd_arch; return lcd_arch;
...@@ -2114,7 +2120,7 @@ void lcd_arch_destroy(struct lcd_arch *lcd_arch) ...@@ -2114,7 +2120,7 @@ void lcd_arch_destroy(struct lcd_arch *lcd_arch)
/* /*
* Assumes all lcd_arch_thread's are destroyed ... * Assumes all lcd_arch_thread's are destroyed ...
*/ */
if (mutex_lock_interruptible(lcd_arch->lcd_arch_threads.lock)) { if (mutex_lock_interruptible(&lcd_arch->lcd_arch_threads.lock)) {
LCD_ARCH_ERR("interrupted, skipping checks and freeing"); LCD_ARCH_ERR("interrupted, skipping checks and freeing");
goto free_junk; goto free_junk;
} }
...@@ -2123,7 +2129,7 @@ void lcd_arch_destroy(struct lcd_arch *lcd_arch) ...@@ -2123,7 +2129,7 @@ void lcd_arch_destroy(struct lcd_arch *lcd_arch)
*/ */
if (!list_empty(&lcd_arch->lcd_arch_threads.list)) if (!list_empty(&lcd_arch->lcd_arch_threads.list))
LCD_ARCH_ERR("lcd_arch still contains some threads..."); LCD_ARCH_ERR("lcd_arch still contains some threads...");
mutex_unlock(lcd_arch->lcd_arch_threads.lock); mutex_unlock(&lcd_arch->lcd_arch_threads.lock);
free_junk: free_junk:
...@@ -2135,6 +2141,7 @@ free_junk: ...@@ -2135,6 +2141,7 @@ free_junk:
/* LCD_ARCH_THREAD CREATE / DESTROY ---------------------------------------- */ /* LCD_ARCH_THREAD CREATE / DESTROY ---------------------------------------- */
#if 0
/** /**
* Pack base, limit, and flags into a segment descriptor. * Pack base, limit, and flags into a segment descriptor.
* *
...@@ -2161,6 +2168,7 @@ static void vmx_pack_desc(struct desc_struct *desc, u64 base, u64 limit, ...@@ -2161,6 +2168,7 @@ static void vmx_pack_desc(struct desc_struct *desc, u64 base, u64 limit,
desc->d = d; desc->d = d;
desc->g = g; desc->g = g;
} }
#endif
/** /**
* Reserves a vpid and sets it in the vcpu. * Reserves a vpid and sets it in the vcpu.
...@@ -2189,7 +2197,7 @@ static void vmx_free_vpid(struct lcd_arch_thread *t) ...@@ -2189,7 +2197,7 @@ static void vmx_free_vpid(struct lcd_arch_thread *t)
{ {
spin_lock(&vpids.lock); spin_lock(&vpids.lock);
if (t->vpid != 0) if (t->vpid != 0)
__clear_bit(vmx->vpid, vpids.bitmap); __clear_bit(t->vpid, vpids.bitmap);
spin_unlock(&vpids.lock); spin_unlock(&vpids.lock);
} }
...@@ -2224,6 +2232,11 @@ struct lcd_arch_thread* lcd_arch_add_thread(struct lcd_arch *lcd_arch) ...@@ -2224,6 +2232,11 @@ struct lcd_arch_thread* lcd_arch_add_thread(struct lcd_arch *lcd_arch)
* Not loaded on a cpu right now * Not loaded on a cpu right now
*/ */
t->cpu = -1; t->cpu = -1;
/*
* Add to lcd_arch (must happen before we set up the vmcs!)
*/
t->lcd_arch = lcd_arch;
/* /*
* Initialize VMCS register values and settings * Initialize VMCS register values and settings
...@@ -2237,14 +2250,13 @@ struct lcd_arch_thread* lcd_arch_add_thread(struct lcd_arch *lcd_arch) ...@@ -2237,14 +2250,13 @@ struct lcd_arch_thread* lcd_arch_add_thread(struct lcd_arch *lcd_arch)
/* /*
* Add t to lcd_arch's list * Add t to lcd_arch's list
*/ */
t->lcd_arch = lcd_arch; INIT_LIST_HEAD(&t->lcd_arch_threads);
list_init(&t.lcd_arch_threads); if (mutex_lock_interruptible(&lcd_arch->lcd_arch_threads.lock))
if (mutex_lock_interruptible(lcd_arch->lcd_arch_threads.lock))
goto fail_list; goto fail_list;
list_add(&t->lcd_arch_threads, &lcd_arch->lcd_arch_threads.list); list_add(&t->lcd_arch_threads, &lcd_arch->lcd_arch_threads.list);
mutex_unlock(lcd_arch->lcd_arch_threads.lock); mutex_unlock(&lcd_arch->lcd_arch_threads.lock);
return t; return t;
...@@ -2287,14 +2299,14 @@ void lcd_arch_destroy_thread(struct lcd_arch_thread *t) ...@@ -2287,14 +2299,14 @@ void lcd_arch_destroy_thread(struct lcd_arch_thread *t)
/* /*
* Remove t from containing lcd_arch * Remove t from containing lcd_arch
*/ */
if (mutex_lock_interruptible(t->lcd_arch->lcd_arch_threads.lock)) { if (mutex_lock_interruptible(&t->lcd_arch->lcd_arch_threads.lock)) {
LCD_ARCH_ERR("interrupted, still try to free ..."); LCD_ARCH_ERR("interrupted, still try to free ...");
goto free_rest; goto free_rest;
} }
list_del(&t->lcd_arch_threads); list_del(&t->lcd_arch_threads);
mutex_unlock(t->lcd_arch->lcd_arch_threads.lock); mutex_unlock(&t->lcd_arch->lcd_arch_threads.lock);
free_rest: free_rest:
/* /*
...@@ -2433,7 +2445,7 @@ static int vmx_handle_hard_exception(struct lcd_arch_thread *t) ...@@ -2433,7 +2445,7 @@ static int vmx_handle_hard_exception(struct lcd_arch_thread *t)
return LCD_ARCH_STATUS_PAGE_FAULT; return LCD_ARCH_STATUS_PAGE_FAULT;
default: default:
LCD_ARCH_ERR("hw exception: vector = %x, info = %x", LCD_ARCH_ERR("hw exception: vector = %x, info = %x",
vector, vcpu->exit_intr_info); vector, t->exit_intr_info);
vmx_handle_external_intr(t); vmx_handle_external_intr(t);
return -EIO; return -EIO;
} }
...@@ -2459,7 +2471,7 @@ static int vmx_handle_exception_nmi(struct lcd_arch_thread *t) ...@@ -2459,7 +2471,7 @@ static int vmx_handle_exception_nmi(struct lcd_arch_thread *t)
* NMI, div by zero, overflow, ... * NMI, div by zero, overflow, ...
*/ */
LCD_ARCH_ERR("exception or nmi: info = %x\n", LCD_ARCH_ERR("exception or nmi: info = %x\n",
vcpu->exit_intr_info); t->exit_intr_info);
return vmx_handle_external_intr(t); return vmx_handle_external_intr(t);
//return -EIO; //return -EIO;
} }
...@@ -2659,25 +2671,40 @@ static int __noclone vmx_enter(struct lcd_arch_thread *t) ...@@ -2659,25 +2671,40 @@ static int __noclone vmx_enter(struct lcd_arch_thread *t)
".popsection" ".popsection"
: : "c"(t), : : "c"(t),
[host_rsp_field]"i"(HOST_RSP), [host_rsp_field]"i"(HOST_RSP),
[launched]"i"(offsetof(struct lcd_arch, launched)), [launched]"i"(offsetof(struct lcd_arch_thread, launched)),
[fail]"i"(offsetof(struct lcd_arch, fail)), [fail]"i"(offsetof(struct lcd_arch_thread, fail)),
[host_rsp]"i"(offsetof(struct lcd_arch, host_rsp)), [host_rsp]"i"(offsetof(struct lcd_arch_thread, host_rsp)),
[rax]"i"(offsetof(struct lcd_arch, regs[LCD_ARCH_REGS_RAX])), [rax]"i"(offsetof(struct lcd_arch_thread,
[rbx]"i"(offsetof(struct lcd_arch, regs[LCD_ARCH_REGS_RBX])), regs[LCD_ARCH_REGS_RAX])),
[rcx]"i"(offsetof(struct lcd_arch, regs[LCD_ARCH_REGS_RCX])), [rbx]"i"(offsetof(struct lcd_arch_thread,
[rdx]"i"(offsetof(struct lcd_arch, regs[LCD_ARCH_REGS_RDX])), regs[LCD_ARCH_REGS_RBX])),
[rsi]"i"(offsetof(struct lcd_arch, regs[LCD_ARCH_REGS_RSI])), [rcx]"i"(offsetof(struct lcd_arch_thread,
[rdi]"i"(offsetof(struct lcd_arch, regs[LCD_ARCH_REGS_RDI])), regs[LCD_ARCH_REGS_RCX])),
[rbp]"i"(offsetof(struct lcd_arch, regs[LCD_ARCH_REGS_RBP])), [rdx]"i"(offsetof(struct lcd_arch_thread,
[r8]"i"(offsetof(struct lcd_arch, regs[LCD_ARCH_REGS_R8])), regs[LCD_ARCH_REGS_RDX])),
[r9]"i"(offsetof(struct lcd_arch, regs[LCD_ARCH_REGS_R9])), [rsi]"i"(offsetof(struct lcd_arch_thread,
[r10]"i"(offsetof(struct lcd_arch, regs[LCD_ARCH_REGS_R10])), regs[LCD_ARCH_REGS_RSI])),
[r11]"i"(offsetof(struct lcd_arch, regs[LCD_ARCH_REGS_R11])), [rdi]"i"(offsetof(struct lcd_arch_thread,
[r12]"i"(offsetof(struct lcd_arch, regs[LCD_ARCH_REGS_R12])), regs[LCD_ARCH_REGS_RDI])),
[r13]"i"(offsetof(struct lcd_arch, regs[LCD_ARCH_REGS_R13])), [rbp]"i"(offsetof(struct lcd_arch_thread,
[r14]"i"(offsetof(struct lcd_arch, regs[LCD_ARCH_REGS_R14])), regs[LCD_ARCH_REGS_RBP])),
[r15]"i"(offsetof(struct lcd_arch, regs[LCD_ARCH_REGS_R15])), [r8]"i"(offsetof(struct lcd_arch_thread,
[cr2]"i"(offsetof(struct lcd_arch, cr2)), regs[LCD_ARCH_REGS_R8])),
[r9]"i"(offsetof(struct lcd_arch_thread,
regs[LCD_ARCH_REGS_R9])),
[r10]"i"(offsetof(struct lcd_arch_thread,
regs[LCD_ARCH_REGS_R10])),
[r11]"i"(offsetof(struct lcd_arch_thread,
regs[LCD_ARCH_REGS_R11])),
[r12]"i"(offsetof(struct lcd_arch_thread,
regs[LCD_ARCH_REGS_R12])),
[r13]"i"(offsetof(struct lcd_arch_thread,
regs[LCD_ARCH_REGS_R13])),
[r14]"i"(offsetof(struct lcd_arch_thread,
regs[LCD_ARCH_REGS_R14])),
[r15]"i"(offsetof(struct lcd_arch_thread,
regs[LCD_ARCH_REGS_R15])),
[cr2]"i"(offsetof(struct lcd_arch_thread, cr2)),
[wordsize]"i"(sizeof(ulong)) [wordsize]"i"(sizeof(ulong))
: "cc", "memory" : "cc", "memory"
, "rax", "rdx", "rbx", "rdi", "rsi" , "rax", "rdx", "rbx", "rdi", "rsi"
......
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