2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Steven Jaconette <stevenjaconette2007@u.northwestern.edu>
11 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
12 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
13 * All rights reserved.
15 * Author: Steven Jaconette <stevenjaconette2007@u.northwestern.edu>
17 * This is free software. You are permitted to use,
18 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
21 #ifndef __VMM_DIRECT_PAGING_32PAE_H__
22 #define __VMM_DIRECT_PAGING_32PAE_H__
24 #include <palacios/vmm_mem.h>
25 #include <palacios/vmm_paging.h>
26 #include <palacios/vmm.h>
27 #include <palacios/vm_guest_mem.h>
28 #include <palacios/vm_guest.h>
30 /* This always builds 3 level page tables - no large pages */
32 static inline int handle_passthrough_pagefault_32pae(struct guest_info * info,
34 pf_error_t error_code,
35 addr_t *actual_start, addr_t *actual_end) {
36 pdpe32pae_t * pdpe = NULL;
37 pde32pae_t * pde = NULL;
38 pte32pae_t * pte = NULL;
41 int pdpe_index = PDPE32PAE_INDEX(fault_addr);
42 int pde_index = PDE32PAE_INDEX(fault_addr);
43 int pte_index = PTE32PAE_INDEX(fault_addr);
46 struct v3_mem_region * region = v3_get_mem_region(info->vm_info, info->vcpu_id, fault_addr);
49 PrintError(info->vm_info, info, "Invalid region in passthrough page fault 32PAE, addr=%p\n",
54 PrintDebug(info->vm_info, info, "Direct Paging 32PAE page fault handler=%p\n", (void *)fault_addr);
56 // Lookup the correct PDPE address based on the PAGING MODE
57 if (info->shdw_pg_mode == SHADOW_PAGING) {
58 pdpe = CR3_TO_PDPE32PAE_VA(info->ctrl_regs.cr3);
60 pdpe = CR3_TO_PDPE32PAE_VA(info->direct_map_pt);
63 PrintDebug(info->vm_info, info, "Top level pdpe error pdp address=%p\n", (void *)pdpe);
64 // Fix up the PDPE entry
65 if (pdpe[pdpe_index].present == 0) {
66 pde = (pde32pae_t *)create_generic_pt_page(info);
67 PrintDebug(info->vm_info, info, "Creating a new pd page=%p\n", (void *)pde);
68 pdpe[pdpe_index].present = 1;
69 // Set default PDPE Flags...
70 pdpe[pdpe_index].pd_base_addr = PAGE_BASE_ADDR((addr_t)V3_PAddr(pde));
72 pde = V3_VAddr((void*)BASE_TO_PAGE_ADDR(pdpe[pdpe_index].pd_base_addr));
74 PrintDebug(info->vm_info, info, "Handling pde error pd base address =%p\n", (void *)pde);
76 *actual_start = BASE_TO_PAGE_ADDR_4KB(PAGE_BASE_ADDR_4KB(fault_addr));
77 *actual_end = BASE_TO_PAGE_ADDR_4KB(PAGE_BASE_ADDR_4KB(fault_addr)+1)-1;
79 // Fix up the PDE entry
80 if (pde[pde_index].present == 0) {
81 pte = (pte32pae_t *)create_generic_pt_page(info);
82 PrintDebug(info->vm_info, info, "Creating a new pt page=%p\n", (void *)pte);
83 pde[pde_index].present = 1;
84 pde[pde_index].writable = 1;
85 pde[pde_index].user_page = 1;
87 pde[pde_index].pt_base_addr = PAGE_BASE_ADDR((addr_t)V3_PAddr(pte));
89 pte = V3_VAddr((void*)BASE_TO_PAGE_ADDR(pde[pde_index].pt_base_addr));
92 PrintDebug(info->vm_info, info, "Handling pte error pt base address=%p\n", (void *)pte);
95 // Fix up the PTE entry
96 if (pte[pte_index].present == 0) {
97 pte[pte_index].user_page = 1;
99 if ((region->flags.alloced == 1) &&
100 (region->flags.read == 1)) {
102 pte[pte_index].present = 1;
104 if (region->flags.write == 1) {
105 pte[pte_index].writable = 1;
107 pte[pte_index].writable = 0;
110 if (v3_gpa_to_hpa(info, fault_addr, &host_addr) == -1) {
111 PrintError(info->vm_info, info, "Could not translate fault address (%p)\n", (void *)fault_addr);
115 pte[pte_index].page_base_addr = PAGE_BASE_ADDR(host_addr);
116 PrintDebug(info->vm_info, info, "PTE mapped to =%p\n", (void *)host_addr);
117 PrintDebug(info->vm_info, info, "PTE is =%llx\n", *(uint64_t *)&(pte[pte_index]));
119 return region->unhandled(info, fault_addr, fault_addr, region, error_code);
122 return region->unhandled(info, fault_addr, fault_addr, region, error_code);
125 PrintDebug(info->vm_info, info, "Handler ends with fault address=%p\n", (void *)fault_addr);
131 static inline int invalidate_addr_32pae_internal(struct guest_info * info, addr_t inv_addr,
132 addr_t *actual_start, uint64_t *actual_size) {
133 pdpe32pae_t * pdpe = NULL;
134 pde32pae_t * pde = NULL;
135 pte32pae_t * pte = NULL;
141 // clear the page table entry
142 int pdpe_index = PDPE32PAE_INDEX(inv_addr);
143 int pde_index = PDE32PAE_INDEX(inv_addr);
144 int pte_index = PTE32PAE_INDEX(inv_addr);
147 // Lookup the correct PDE address based on the PAGING MODE
148 if (info->shdw_pg_mode == SHADOW_PAGING) {
149 pdpe = CR3_TO_PDPE32PAE_VA(info->ctrl_regs.cr3);
151 pdpe = CR3_TO_PDPE32PAE_VA(info->direct_map_pt);
155 if (pdpe[pdpe_index].present == 0) {
156 *actual_start = BASE_TO_PAGE_ADDR_1GB(PAGE_BASE_ADDR_1GB(inv_addr));
157 *actual_size = PAGE_SIZE_1GB;
161 pde = V3_VAddr((void*)BASE_TO_PAGE_ADDR(pdpe[pdpe_index].pd_base_addr));
163 if (pde[pde_index].present == 0) {
164 *actual_start = BASE_TO_PAGE_ADDR_2MB(PAGE_BASE_ADDR_2MB(inv_addr));
165 *actual_size = PAGE_SIZE_2MB;
167 } else if (pde[pde_index].large_page) {
168 pde[pde_index].present = 0;
169 *actual_start = BASE_TO_PAGE_ADDR_2MB(PAGE_BASE_ADDR_2MB(inv_addr));
170 *actual_size = PAGE_SIZE_2MB;
174 pte = V3_VAddr((void*)BASE_TO_PAGE_ADDR(pde[pde_index].pt_base_addr));
176 pte[pte_index].present = 0;
178 *actual_start = BASE_TO_PAGE_ADDR_4KB(PAGE_BASE_ADDR_4KB(inv_addr));
179 *actual_size = PAGE_SIZE_4KB;
185 static inline int invalidate_addr_32pae(struct guest_info * core, addr_t inv_addr,
186 addr_t *actual_start, addr_t *actual_end)
191 rc = invalidate_addr_32pae_internal(core,inv_addr,actual_start,&len);
193 *actual_end = *actual_start + len - 1;
200 static inline int invalidate_addr_32pae_range(struct guest_info * core, addr_t inv_addr_start, addr_t inv_addr_end,
201 addr_t *actual_start, addr_t *actual_end)
208 for (next=inv_addr_start; next<=inv_addr_end; ) {
209 rc = invalidate_addr_32pae_internal(core,next,&start, &len);
210 if (next==inv_addr_start) {
211 // first iteration, capture where we start invalidating
212 *actual_start = start;
220 // last iteration, actual_end is off by one