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_32_H__
22 #define __VMM_DIRECT_PAGING_32_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>
29 #include <palacios/vmm_ctrl_regs.h>
32 /* This always build 2 level page tables - no large pages are used */
34 static inline int handle_passthrough_pagefault_32(struct guest_info * info,
36 pf_error_t error_code,
37 addr_t *actual_start, addr_t *actual_end) {
39 // Check to see if pde and pte exist (create them if not)
44 int pde_index = PDE32_INDEX(fault_addr);
45 int pte_index = PTE32_INDEX(fault_addr);
47 struct v3_mem_region * region = v3_get_mem_region(info->vm_info, info->vcpu_id, fault_addr);
50 PrintError(info->vm_info, info, "Invalid region in passthrough page fault 32, addr=%p\n",
55 // Lookup the correct PDE address based on the PAGING MODE
56 if (info->shdw_pg_mode == SHADOW_PAGING) {
57 pde = CR3_TO_PDE32_VA(info->ctrl_regs.cr3);
59 pde = CR3_TO_PDE32_VA(info->direct_map_pt);
63 *actual_start = BASE_TO_PAGE_ADDR_4KB(PAGE_BASE_ADDR_4KB(fault_addr));
64 *actual_end = BASE_TO_PAGE_ADDR_4KB(PAGE_BASE_ADDR_4KB(fault_addr)+1)-1;
66 // Fix up the PDE entry
67 if (pde[pde_index].present == 0) {
68 pte = (pte32_t *)create_generic_pt_page(info);
70 pde[pde_index].present = 1;
71 pde[pde_index].writable = 1;
72 pde[pde_index].user_page = 1;
73 pde[pde_index].pt_base_addr = PAGE_BASE_ADDR((addr_t)V3_PAddr(pte));
76 pte = V3_VAddr((void*)BASE_TO_PAGE_ADDR(pde[pde_index].pt_base_addr));
79 // Fix up the PTE entry
80 if (pte[pte_index].present == 0) {
83 if ((region->flags.alloced == 1) &&
84 (region->flags.read == 1)) {
86 pte[pte_index].user_page = 1;
88 pte[pte_index].present = 1;
90 if (region->flags.write == 1) {
91 pte[pte_index].writable = 1;
93 pte[pte_index].writable = 0;
96 if (v3_gpa_to_hpa(info, fault_addr, &host_addr) == -1) {
97 PrintError(info->vm_info, info, "Could not translate fault address (%p)\n", (void *)fault_addr);
101 pte[pte_index].page_base_addr = PAGE_BASE_ADDR(host_addr);
103 return region->unhandled(info, fault_addr, fault_addr, region, error_code);
106 // We fix all permissions on the first pass,
107 // so we only get here if its an unhandled exception
108 return region->unhandled(info, fault_addr, fault_addr, region, error_code);
117 static inline int invalidate_addr_32_internal(struct guest_info * info, addr_t inv_addr,
118 addr_t *actual_start, uint64_t *actual_size) {
119 pde32_t * pde = NULL;
120 pte32_t * pte = NULL;
125 // clear the page table entry
126 int pde_index = PDE32_INDEX(inv_addr);
127 int pte_index = PTE32_INDEX(inv_addr);
130 // Lookup the correct PDE address based on the PAGING MODE
131 if (info->shdw_pg_mode == SHADOW_PAGING) {
132 pde = CR3_TO_PDE32_VA(info->ctrl_regs.cr3);
134 pde = CR3_TO_PDE32_VA(info->direct_map_pt);
137 if (pde[pde_index].present == 0) {
138 *actual_start = BASE_TO_PAGE_ADDR_4MB(PAGE_BASE_ADDR_4MB(inv_addr));
139 *actual_size = PAGE_SIZE_4MB;
141 } else if (pde[pde_index].large_page) {
142 pde[pde_index].present = 0;
143 pde[pde_index].writable = 0;
144 pde[pde_index].user_page = 0;
145 *actual_start = BASE_TO_PAGE_ADDR_4MB(PAGE_BASE_ADDR_4MB(inv_addr));
146 *actual_size = PAGE_SIZE_4MB;
150 pte = V3_VAddr((void*)BASE_TO_PAGE_ADDR(pde[pde_index].pt_base_addr));
152 pte[pte_index].present = 0;
153 pte[pte_index].writable = 0;
154 pte[pte_index].user_page = 0;
156 *actual_start = BASE_TO_PAGE_ADDR_4KB(PAGE_BASE_ADDR_4KB(inv_addr));
157 *actual_size = PAGE_SIZE_4KB;
163 static inline int invalidate_addr_32(struct guest_info * core, addr_t inv_addr,
164 addr_t *actual_start, addr_t *actual_end)
169 rc = invalidate_addr_32_internal(core,inv_addr,actual_start,&len);
171 *actual_end = *actual_start + len - 1;
176 static inline int invalidate_addr_32_range(struct guest_info * core, addr_t inv_addr_start, addr_t inv_addr_end,
177 addr_t *actual_start, addr_t *actual_end)
184 for (next=inv_addr_start; next<=inv_addr_end; ) {
185 rc = invalidate_addr_32_internal(core,next,&start, &len);
186 if (next==inv_addr_start) {
187 // first iteration, capture where we start invalidating
188 *actual_start = start;
196 // last iteration, actual_end is off by one