Slightly more working but we're back to not working.
[akaros.git] / kern / arch / x86 / vmx_mmu.c
index 93e4eae..a253a13 100644 (file)
@@ -67,7 +67,6 @@
 #define PT32_DIR_PSE36_SHIFT 13
 #define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
 
-
 #define PT32_PTE_COPY_MASK \
        (PT_PRESENT_MASK | PT_PWT_MASK | PT_PCD_MASK | \
        PT_ACCESSED_MASK | PT_DIRTY_MASK | PT_PAT_MASK | \
        (PT_PRESENT_MASK | PT_PWT_MASK | PT_PCD_MASK | \
        PT_ACCESSED_MASK | PT_DIRTY_MASK)
 
-
 #define PT64_PTE_COPY_MASK \
        (PT64_NX_MASK | PT32_PTE_COPY_MASK)
 
 #define PT64_NON_PTE_COPY_MASK \
        (PT64_NX_MASK | PT32_NON_PTE_COPY_MASK)
 
-
-
 #define PT_FIRST_AVAIL_BITS_SHIFT 9
 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
 
 #define PT64_INDEX(address, level)\
        (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
 
-
 #define PT32_LEVEL_BITS 10
 
 #define PT32_LEVEL_SHIFT(level) \
 #define PT32_INDEX(address, level)\
        (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
 
-
 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & PAGE_MASK)
 #define PT64_DIR_BASE_ADDR_MASK \
        (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
 #define PT32_DIR_BASE_ADDR_MASK \
        (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
 
-
 #define PFERR_PRESENT_MASK (1U << 0)
 #define PFERR_WRITE_MASK (1U << 1)
 #define PFERR_USER_MASK (1U << 2)
 
 static int is_write_protection(void)
 {
+       print_func_entry();
+       print_func_exit();
        return guest_cr0() & CR0_WP_MASK;
 }
 
 static int is_cpuid_PSE36(void)
 {
+       print_func_entry();
+       print_func_exit();
        return 1;
 }
 
 static int is_present_pte(unsigned long pte)
 {
+       //print_func_entry();
+       //print_func_exit();
        return pte & PT_PRESENT_MASK;
 }
 
 static int is_writeble_pte(unsigned long pte)
 {
+       //print_func_entry();
+       //print_func_exit();
        return pte & PT_WRITABLE_MASK;
 }
 
 static int is_io_pte(unsigned long pte)
 {
+       //print_func_entry();
+       //print_func_exit();
        return pte & PT_SHADOW_IO_MARK;
 }
 
 static void litevm_mmu_free_page(struct litevm_vcpu *vcpu, hpa_t page_hpa)
 {
+       print_func_entry();
        struct litevm_mmu_page *page_head = page_header(page_hpa);
 
        LIST_REMOVE(page_head, link);
@@ -180,26 +184,34 @@ static void litevm_mmu_free_page(struct litevm_vcpu *vcpu, hpa_t page_hpa)
        page_head->page_hpa = page_hpa;
        //list_add(&page_head->link, &vcpu->free_pages);
        LIST_INSERT_HEAD(&vcpu->link, page_head, link);
+       print_func_exit();
 }
 
 static int is_empty_shadow_page(hpa_t page_hpa)
 {
+       print_func_entry();
        uint32_t *pos;
        uint32_t *end;
        for (pos = KADDR(page_hpa), end = pos + PAGE_SIZE / sizeof(uint32_t);
-                     pos != end; pos++)
-               if (*pos != 0)
+                pos != end; pos++)
+               if (*pos != 0) {
+                       print_func_exit();
                        return 0;
+               }
+       print_func_exit();
        return 1;
 }
 
 static hpa_t litevm_mmu_alloc_page(struct litevm_vcpu *vcpu,
-                                  uint64_t *parent_pte)
+                                                                  uint64_t * parent_pte)
 {
+       print_func_entry();
        struct litevm_mmu_page *page;
 
-       if (LIST_EMPTY(&vcpu->link))
+       if (LIST_EMPTY(&vcpu->link)) {
+               print_func_exit();
                return INVALID_PAGE;
+       }
 
        page = LIST_FIRST(&vcpu->link);
        LIST_REMOVE(page, link);
@@ -208,51 +220,69 @@ static hpa_t litevm_mmu_alloc_page(struct litevm_vcpu *vcpu,
        page->slot_bitmap = 0;
        page->global = 1;
        page->parent_pte = parent_pte;
+       print_func_exit();
        return page->page_hpa;
 }
 
 static void page_header_update_slot(struct litevm *litevm, void *pte, gpa_t gpa)
 {
+       print_func_entry();
        int slot = memslot_id(litevm, gfn_to_memslot(litevm, gpa >> PAGE_SHIFT));
        struct litevm_mmu_page *page_head = page_header(PADDR(pte));
 
-       SET_BITMASK_BIT_ATOMIC((uint8_t *)&page_head->slot_bitmap, slot);
+       SET_BITMASK_BIT_ATOMIC((uint8_t *) & page_head->slot_bitmap, slot);
+       print_func_exit();
 }
 
 hpa_t safe_gpa_to_hpa(struct litevm_vcpu *vcpu, gpa_t gpa)
 {
+       print_func_entry();
        hpa_t hpa = gpa_to_hpa(vcpu, gpa);
 
-       return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
+       print_func_exit();
+       return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK) : hpa;
 }
 
-hpa_t gpa_to_hpa(struct litevm_vcpu *vcpu, gpa_t gpa)
+hpa_t gpa_to_hpa(struct litevm_vcpu * vcpu, gpa_t gpa)
 {
+       print_func_entry();
        struct litevm_memory_slot *slot;
        struct page *page;
 
        ASSERT((gpa & HPA_ERR_MASK) == 0);
        slot = gfn_to_memslot(vcpu->litevm, gpa >> PAGE_SHIFT);
-       if (!slot)
+       printk("GFN %016lx memslot %p\n", gpa>>PAGE_SHIFT, slot);
+       if (!slot) {
+               printk("GFN_TO_MEMSLOT FAILED!\n");
+               print_func_exit();
                return gpa | HPA_ERR_MASK;
+       }
        page = gfn_to_page(slot, gpa >> PAGE_SHIFT);
-       return ((hpa_t)page2ppn(page) << PAGE_SHIFT)
-               | (gpa & (PAGE_SIZE-1));
+       printk("Page is %p\n", page);
+       print_func_exit();
+       printk("gpa_to_hpa: return %016lx\n",  ((hpa_t) page2ppn(page) << PAGE_SHIFT)
+               | (gpa & (PAGE_SIZE - 1)));
+       return ((hpa_t) page2ppn(page) << PAGE_SHIFT)
+               | (gpa & (PAGE_SIZE - 1));
 }
 
-hpa_t gva_to_hpa(struct litevm_vcpu *vcpu, gva_t gva)
+hpa_t gva_to_hpa(struct litevm_vcpu * vcpu, gva_t gva)
 {
+       print_func_entry();
        gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
 
-       if (gpa == UNMAPPED_GVA)
+       if (gpa == UNMAPPED_GVA) {
+               print_func_exit();
                return UNMAPPED_GVA;
+       }
+       print_func_exit();
        return gpa_to_hpa(vcpu, gpa);
 }
 
-
 static void release_pt_page_64(struct litevm_vcpu *vcpu, hpa_t page_hpa,
-                              int level)
+                                                          int level)
 {
+       print_func_entry();
        ASSERT(vcpu);
        ASSERT(VALID_PAGE(page_hpa));
        ASSERT(level <= PT64_ROOT_LEVEL && level > 0);
@@ -264,30 +294,35 @@ static void release_pt_page_64(struct litevm_vcpu *vcpu, hpa_t page_hpa,
                uint64_t *end;
 
                for (pos = KADDR(page_hpa), end = pos + PT64_ENT_PER_PAGE;
-                    pos != end; pos++) {
+                        pos != end; pos++) {
                        uint64_t current_ent = *pos;
 
                        *pos = 0;
                        if (is_present_pte(current_ent))
                                release_pt_page_64(vcpu,
-                                                 current_ent &
-                                                 PT64_BASE_ADDR_MASK,
-                                                 level - 1);
+                                                                  current_ent &
+                                                                  PT64_BASE_ADDR_MASK, level - 1);
                }
        }
        litevm_mmu_free_page(vcpu, page_hpa);
+       print_func_exit();
 }
 
 static void nonpaging_new_cr3(struct litevm_vcpu *vcpu)
 {
+       print_func_entry();
+       print_func_exit();
 }
 
 static int nonpaging_map(struct litevm_vcpu *vcpu, gva_t v, hpa_t p)
 {
+       print_func_entry();
        int level = PT32E_ROOT_LEVEL;
        hpa_t table_addr = vcpu->mmu.root_hpa;
+printk("nonpaging_map: v %016lx, p %016lx\n", v, p);
+hexdump(KADDR(p), 32);
 
-       for (; ; level--) {
+       for (;; level--) {
                uint32_t index = PT64_INDEX(v, level);
                uint64_t *table;
 
@@ -298,16 +333,17 @@ static int nonpaging_map(struct litevm_vcpu *vcpu, gva_t v, hpa_t p)
                        mark_page_dirty(vcpu->litevm, v >> PAGE_SHIFT);
                        page_header_update_slot(vcpu->litevm, table, v);
                        table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
-                                                               PT_USER_MASK;
+                               PT_USER_MASK;
+                       print_func_exit();
                        return 0;
                }
 
                if (table[index] == 0) {
-                       hpa_t new_table = litevm_mmu_alloc_page(vcpu,
-                                                            &table[index]);
+                       hpa_t new_table = litevm_mmu_alloc_page(vcpu, &table[index]);
 
                        if (!VALID_PAGE(new_table)) {
                                pgprintk("nonpaging_map: ENOMEM\n");
+                               print_func_exit();
                                return -ENOMEM;
                        }
 
@@ -315,14 +351,16 @@ static int nonpaging_map(struct litevm_vcpu *vcpu, gva_t v, hpa_t p)
                                table[index] = new_table | PT_PRESENT_MASK;
                        else
                                table[index] = new_table | PT_PRESENT_MASK |
-                                               PT_WRITABLE_MASK | PT_USER_MASK;
+                                       PT_WRITABLE_MASK | PT_USER_MASK;
                }
                table_addr = table[index] & PT64_BASE_ADDR_MASK;
        }
+       print_func_exit();
 }
 
 static void nonpaging_flush(struct litevm_vcpu *vcpu)
 {
+       print_func_entry();
        hpa_t root = vcpu->mmu.root_hpa;
 
        ++litevm_stat.tlb_flush;
@@ -335,46 +373,57 @@ static void nonpaging_flush(struct litevm_vcpu *vcpu)
        if (is_paging())
                root |= (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK));
        vmcs_writel(GUEST_CR3, root);
+       print_func_exit();
 }
 
 static gpa_t nonpaging_gva_to_gpa(struct litevm_vcpu *vcpu, gva_t vaddr)
 {
+       print_func_entry();
+       print_func_exit();
        return vaddr;
 }
 
 static int nonpaging_page_fault(struct litevm_vcpu *vcpu, gva_t gva,
-                              uint32_t error_code)
+                                                               uint32_t error_code)
 {
+       print_func_entry();
        int ret;
        gpa_t addr = gva;
 
+printk("nonpaging_page_fault: %016llx\n", gva);
        ASSERT(vcpu);
        ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
 
        for (;;) {
-            hpa_t paddr;
+               hpa_t paddr;
 
-            paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
+               paddr = gpa_to_hpa(vcpu, addr & PT64_BASE_ADDR_MASK);
 
-            if (is_error_hpa(paddr))
-                    return 1;
+               if (is_error_hpa(paddr)) {
+                       print_func_exit();
+                       return 1;
+               }
 
-            ret = nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
-            if (ret) {
-                    nonpaging_flush(vcpu);
-                    continue;
-            }
-            break;
+               ret = nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
+               if (ret) {
+                       nonpaging_flush(vcpu);
+                       continue;
+               }
+               break;
        }
+       print_func_exit();
        return ret;
 }
 
 static void nonpaging_inval_page(struct litevm_vcpu *vcpu, gva_t addr)
 {
+       print_func_entry();
+       print_func_exit();
 }
 
 static void nonpaging_free(struct litevm_vcpu *vcpu)
 {
+       print_func_entry();
        hpa_t root;
 
        ASSERT(vcpu);
@@ -382,10 +431,12 @@ static void nonpaging_free(struct litevm_vcpu *vcpu)
        if (VALID_PAGE(root))
                release_pt_page_64(vcpu, root, vcpu->mmu.shadow_root_level);
        vcpu->mmu.root_hpa = INVALID_PAGE;
+       print_func_exit();
 }
 
 static int nonpaging_init_context(struct litevm_vcpu *vcpu)
 {
+       print_func_entry();
        struct litevm_mmu *context = &vcpu->mmu;
 
        context->new_cr3 = nonpaging_new_cr3;
@@ -398,17 +449,17 @@ static int nonpaging_init_context(struct litevm_vcpu *vcpu)
        context->root_hpa = litevm_mmu_alloc_page(vcpu, 0);
        ASSERT(VALID_PAGE(context->root_hpa));
        vmcs_writel(GUEST_CR3, context->root_hpa);
+       print_func_exit();
        return 0;
 }
 
-
 static void litevm_mmu_flush_tlb(struct litevm_vcpu *vcpu)
 {
+       print_func_entry();
        struct litevm_mmu_page *page, *npage;
 
        //list_for_each_entry_safe(page, npage, &vcpu->litevm->active_mmu_pages,
-       LIST_FOREACH_SAFE(page, &vcpu->litevm->link,
-                                link, npage) {
+       LIST_FOREACH_SAFE(page, &vcpu->litevm->link, link, npage) {
                if (page->global)
                        continue;
 
@@ -419,24 +470,28 @@ static void litevm_mmu_flush_tlb(struct litevm_vcpu *vcpu)
                release_pt_page_64(vcpu, page->page_hpa, 1);
        }
        ++litevm_stat.tlb_flush;
+       print_func_exit();
 }
 
 static void paging_new_cr3(struct litevm_vcpu *vcpu)
 {
+       print_func_entry();
        litevm_mmu_flush_tlb(vcpu);
+       print_func_exit();
 }
 
 static void mark_pagetable_nonglobal(void *shadow_pte)
 {
+       print_func_entry();
        page_header(PADDR(shadow_pte))->global = 0;
+       print_func_exit();
 }
 
 static inline void set_pte_common(struct litevm_vcpu *vcpu,
-                            uint64_t *shadow_pte,
-                            gpa_t gaddr,
-                            int dirty,
-                            uint64_t access_bits)
+                                                                 uint64_t * shadow_pte,
+                                                                 gpa_t gaddr, int dirty, uint64_t access_bits)
 {
+       print_func_entry();
        hpa_t paddr;
 
        *shadow_pte |= access_bits << PT_SHADOW_BITS_OFFSET;
@@ -461,12 +516,13 @@ static inline void set_pte_common(struct litevm_vcpu *vcpu,
                *shadow_pte |= paddr;
                page_header_update_slot(vcpu->litevm, shadow_pte, gaddr);
        }
+       print_func_exit();
 }
 
 static void inject_page_fault(struct litevm_vcpu *vcpu,
-                             uint64_t addr,
-                             uint32_t err_code)
+                                                         uint64_t addr, uint32_t err_code)
 {
+       print_func_entry();
        uint32_t vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
 
        pgprintk("inject_page_fault: 0x%llx err 0x%x\n", addr, err_code);
@@ -474,31 +530,30 @@ static void inject_page_fault(struct litevm_vcpu *vcpu,
        ++litevm_stat.pf_guest;
 
        if (is_page_fault(vect_info)) {
-               printd( "inject_page_fault: "
-                      "double fault 0x%llx @ 0x%lx\n",
-                      addr, vmcs_readl(GUEST_RIP));
+               printd("inject_page_fault: "
+                          "double fault 0x%llx @ 0x%lx\n", addr, vmcs_readl(GUEST_RIP));
                vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
                vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
-                            DF_VECTOR |
-                            INTR_TYPE_EXCEPTION |
-                            INTR_INFO_DELIEVER_CODE_MASK |
-                            INTR_INFO_VALID_MASK);
+                                        DF_VECTOR |
+                                        INTR_TYPE_EXCEPTION |
+                                        INTR_INFO_DELIEVER_CODE_MASK | INTR_INFO_VALID_MASK);
+               print_func_exit();
                return;
        }
        vcpu->cr2 = addr;
        vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, err_code);
        vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
-                    PF_VECTOR |
-                    INTR_TYPE_EXCEPTION |
-                    INTR_INFO_DELIEVER_CODE_MASK |
-                    INTR_INFO_VALID_MASK);
+                                PF_VECTOR |
+                                INTR_TYPE_EXCEPTION |
+                                INTR_INFO_DELIEVER_CODE_MASK | INTR_INFO_VALID_MASK);
 
+       print_func_exit();
 }
 
-static inline int fix_read_pf(uint64_t *shadow_ent)
+static inline int fix_read_pf(uint64_t * shadow_ent)
 {
-       if ((*shadow_ent & PT_SHADOW_USER_MASK) &&
-           !(*shadow_ent & PT_USER_MASK)) {
+       print_func_entry();
+       if ((*shadow_ent & PT_SHADOW_USER_MASK) && !(*shadow_ent & PT_USER_MASK)) {
                /*
                 * If supervisor write protect is disabled, we shadow kernel
                 * pages as user pages so we can trap the write access.
@@ -506,19 +561,27 @@ static inline int fix_read_pf(uint64_t *shadow_ent)
                *shadow_ent |= PT_USER_MASK;
                *shadow_ent &= ~PT_WRITABLE_MASK;
 
+               print_func_exit();
                return 1;
 
        }
+       print_func_exit();
        return 0;
 }
 
 static int may_access(uint64_t pte, int write, int user)
 {
+       print_func_entry();
 
-       if (user && !(pte & PT_USER_MASK))
+       if (user && !(pte & PT_USER_MASK)) {
+               print_func_exit();
                return 0;
-       if (write && !(pte & PT_WRITABLE_MASK))
+       }
+       if (write && !(pte & PT_WRITABLE_MASK)) {
+               print_func_exit();
                return 0;
+       }
+       print_func_exit();
        return 1;
 }
 
@@ -527,41 +590,49 @@ static int may_access(uint64_t pte, int write, int user)
  */
 static void paging_inval_page(struct litevm_vcpu *vcpu, gva_t addr)
 {
+       print_func_entry();
        hpa_t page_addr = vcpu->mmu.root_hpa;
        int level = vcpu->mmu.shadow_root_level;
 
+printk("paging_inval_page: addr %016lx\n", addr);
        ++litevm_stat.invlpg;
 
-       for (; ; level--) {
+       for (;; level--) {
                uint32_t index = PT64_INDEX(addr, level);
                uint64_t *table = KADDR(page_addr);
 
-               if (level == PT_PAGE_TABLE_LEVEL ) {
+               if (level == PT_PAGE_TABLE_LEVEL) {
                        table[index] = 0;
+                       print_func_exit();
                        return;
                }
 
-               if (!is_present_pte(table[index]))
+               if (!is_present_pte(table[index])) {
+                       print_func_exit();
                        return;
+               }
 
                page_addr = table[index] & PT64_BASE_ADDR_MASK;
 
-               if (level == PT_DIRECTORY_LEVEL &&
-                         (table[index] & PT_SHADOW_PS_MARK)) {
+               if (level == PT_DIRECTORY_LEVEL && (table[index] & PT_SHADOW_PS_MARK)) {
                        table[index] = 0;
                        release_pt_page_64(vcpu, page_addr, PT_PAGE_TABLE_LEVEL);
 
                        //flush tlb
                        vmcs_writel(GUEST_CR3, vcpu->mmu.root_hpa |
-                                   (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
+                                               (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
+                       print_func_exit();
                        return;
                }
        }
+       print_func_exit();
 }
 
 static void paging_free(struct litevm_vcpu *vcpu)
 {
+       print_func_entry();
        nonpaging_free(vcpu);
+       print_func_exit();
 }
 
 #define PTTYPE 64
@@ -574,6 +645,7 @@ static void paging_free(struct litevm_vcpu *vcpu)
 
 static int paging64_init_context(struct litevm_vcpu *vcpu)
 {
+       print_func_entry();
        struct litevm_mmu *context = &vcpu->mmu;
 
        ASSERT(is_pae());
@@ -587,12 +659,14 @@ static int paging64_init_context(struct litevm_vcpu *vcpu)
        context->root_hpa = litevm_mmu_alloc_page(vcpu, 0);
        ASSERT(VALID_PAGE(context->root_hpa));
        vmcs_writel(GUEST_CR3, context->root_hpa |
-                   (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
+                               (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
+       print_func_exit();
        return 0;
 }
 
 static int paging32_init_context(struct litevm_vcpu *vcpu)
 {
+       print_func_entry();
        struct litevm_mmu *context = &vcpu->mmu;
 
        context->new_cr3 = paging_new_cr3;
@@ -605,68 +679,86 @@ static int paging32_init_context(struct litevm_vcpu *vcpu)
        context->root_hpa = litevm_mmu_alloc_page(vcpu, 0);
        ASSERT(VALID_PAGE(context->root_hpa));
        vmcs_writel(GUEST_CR3, context->root_hpa |
-                   (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
+                               (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
+       print_func_exit();
        return 0;
 }
 
 static int paging32E_init_context(struct litevm_vcpu *vcpu)
 {
+       print_func_entry();
        int ret;
 
-       if ((ret = paging64_init_context(vcpu)))
+       if ((ret = paging64_init_context(vcpu))) {
+               print_func_exit();
                return ret;
+       }
 
        vcpu->mmu.root_level = PT32E_ROOT_LEVEL;
        vcpu->mmu.shadow_root_level = PT32E_ROOT_LEVEL;
+       print_func_exit();
        return 0;
 }
 
 static int init_litevm_mmu(struct litevm_vcpu *vcpu)
 {
+       print_func_entry();
        ASSERT(vcpu);
        ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
 
-       if (!is_paging())
+       if (!is_paging()) {
+               print_func_exit();
                return nonpaging_init_context(vcpu);
-       else if (is_long_mode())
+       } else if (is_long_mode()) {
+               print_func_exit();
                return paging64_init_context(vcpu);
-       else if (is_pae())
+       } else if (is_pae()) {
+               print_func_exit();
                return paging32E_init_context(vcpu);
-       else
+       } else {
+               print_func_exit();
                return paging32_init_context(vcpu);
+       }
 }
 
 static void destroy_litevm_mmu(struct litevm_vcpu *vcpu)
 {
+       print_func_entry();
        ASSERT(vcpu);
        if (VALID_PAGE(vcpu->mmu.root_hpa)) {
                vcpu->mmu.free(vcpu);
                vcpu->mmu.root_hpa = INVALID_PAGE;
        }
+       print_func_exit();
 }
 
 int litevm_mmu_reset_context(struct litevm_vcpu *vcpu)
 {
+       print_func_entry();
        destroy_litevm_mmu(vcpu);
+       print_func_exit();
        return init_litevm_mmu(vcpu);
 }
 
 static void free_mmu_pages(struct litevm_vcpu *vcpu)
 {
+       print_func_entry();
        /* todo: use the right macros */
        while (!LIST_EMPTY(&vcpu->link)) {
                struct litevm_mmu_page *vmpage;
                vmpage = LIST_FIRST(&vcpu->link);
                LIST_REMOVE(vmpage, link);
-               uintptr_t ppn = vmpage->page_hpa>>PAGE_SHIFT;
+               uintptr_t ppn = vmpage->page_hpa >> PAGE_SHIFT;
                page_decref(ppn2page(ppn));
                assert(page_is_free(ppn));
                vmpage->page_hpa = INVALID_PAGE;
        }
+       print_func_exit();
 }
 
 static int alloc_mmu_pages(struct litevm_vcpu *vcpu)
 {
+       print_func_entry();
        int i;
 
        ASSERT(vcpu);
@@ -678,55 +770,65 @@ static int alloc_mmu_pages(struct litevm_vcpu *vcpu)
                struct page *page;
                struct litevm_mmu_page *page_header = &vcpu->page_header_buf[i];
 
-               if ((page = kpage_alloc_addr()) == NULL)
+               if (kpage_alloc(&page) != ESUCCESS)
                        goto error_1;
                page->pg_private = page_header;
-               page_header->page_hpa = (hpa_t)page2ppn(page) << PAGE_SHIFT;
+               page_header->page_hpa = (hpa_t) page2pa(page);
                memset(KADDR(page_header->page_hpa), 0, PAGE_SIZE);
                LIST_INSERT_HEAD(&vcpu->link, page_header, link);
        }
+       print_func_exit();
        return 0;
 
 error_1:
        free_mmu_pages(vcpu);
+       print_func_exit();
        return -ENOMEM;
 }
 
 int litevm_mmu_init(struct litevm_vcpu *vcpu)
 {
+       print_func_entry();
        int r;
 
        ASSERT(vcpu);
        ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
        ASSERT(LIST_EMPTY(&vcpu->link));
 
-       if ((r = alloc_mmu_pages(vcpu)))
+       if ((r = alloc_mmu_pages(vcpu))) {
+               print_func_exit();
                return r;
+       }
 
        if ((r = init_litevm_mmu(vcpu))) {
                free_mmu_pages(vcpu);
+               print_func_exit();
                return r;
        }
+       print_func_exit();
        return 0;
 }
 
 void litevm_mmu_destroy(struct litevm_vcpu *vcpu)
 {
+       print_func_entry();
        ASSERT(vcpu);
 
        destroy_litevm_mmu(vcpu);
        free_mmu_pages(vcpu);
+       print_func_exit();
 }
 
 void litevm_mmu_slot_remove_write_access(struct litevm *litevm, int slot)
 {
+       print_func_entry();
        struct litevm_mmu_page *page, *link;
 
        LIST_FOREACH(page, &litevm->link, link) {
                int i;
                uint64_t *pt;
 
-               if (!GET_BITMASK_BIT((uint8_t*)&page->slot_bitmap, slot))
+               if (!GET_BITMASK_BIT((uint8_t *) & page->slot_bitmap, slot))
                        continue;
 
                pt = KADDR(page->page_hpa);
@@ -736,4 +838,5 @@ void litevm_mmu_slot_remove_write_access(struct litevm *litevm, int slot)
                                pt[i] &= ~PT_WRITABLE_MASK;
 
        }
+       print_func_exit();
 }