Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Convert shadow paging to use 32 PAE (Direct Paging)
[palacios.git] / palacios / src / palacios / vmm_direct_paging_32.h
1 /*
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
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.
14  *
15  * Author: Steven Jaconette <stevenjaconette2007@u.northwestern.edu>
16  *
17  * This is free software.  You are permitted to use,
18  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
19  */
20
21 #ifndef __VMM_DIRECT_PAGING_32_H__
22 #define __VMM_DIRECT_PAGING_32_H__
23
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>
30
31
32 static inline int handle_passthrough_pagefault_32(struct guest_info * info, 
33                                                   addr_t fault_addr, 
34                                                   pf_error_t error_code) {
35     // Check to see if pde and pte exist (create them if not)
36     pde32_t * pde = NULL;
37     pte32_t * pte = NULL;
38     addr_t host_addr = 0;
39     
40     int pde_index = PDE32_INDEX(fault_addr);
41     int pte_index = PTE32_INDEX(fault_addr);
42     
43     struct v3_mem_region * region = v3_get_mem_region(info->vm_info, info->vcpu_id, fault_addr);
44
45     if (region == NULL) {
46         PrintError(info->vm_info, info, "Invalid region in passthrough page fault 32, addr=%p\n", 
47                    (void *)fault_addr);
48         return -1;
49     }
50     
51     // Lookup the correct PDE address based on the PAGING MODE
52     if (info->shdw_pg_mode == SHADOW_PAGING) {
53         pde = CR3_TO_PDE32_VA(info->ctrl_regs.cr3);
54     } else {
55         pde = CR3_TO_PDE32_VA(info->direct_map_pt);
56     }
57
58
59     // Fix up the PDE entry
60     if (pde[pde_index].present == 0) {
61         pte = (pte32_t *)create_generic_pt_page(info);
62         
63         pde[pde_index].present = 1;
64         pde[pde_index].writable = 1;
65         pde[pde_index].user_page = 1;
66         pde[pde_index].pt_base_addr = PAGE_BASE_ADDR((addr_t)V3_PAddr(pte));
67         
68     } else {
69         pte = V3_VAddr((void*)BASE_TO_PAGE_ADDR(pde[pde_index].pt_base_addr));
70     }
71     
72     // Fix up the PTE entry
73     if (pte[pte_index].present == 0) {
74         
75         
76         if ((region->flags.alloced == 1) && 
77             (region->flags.read == 1)) {
78
79             pte[pte_index].user_page = 1;
80
81             pte[pte_index].present = 1;
82
83             if (region->flags.write == 1) {
84                 pte[pte_index].writable = 1;
85             } else {
86                 pte[pte_index].writable = 0;
87             }
88
89             if (v3_gpa_to_hpa(info, fault_addr, &host_addr) == -1) {
90                 PrintError(info->vm_info, info, "Could not translate fault address (%p)\n", (void *)fault_addr);
91                 return -1;
92             }
93             
94             pte[pte_index].page_base_addr = PAGE_BASE_ADDR(host_addr);
95         } else {
96             return region->unhandled(info, fault_addr, fault_addr, region, error_code);
97         }
98     } else {
99         // We fix all permissions on the first pass, 
100         // so we only get here if its an unhandled exception
101         return region->unhandled(info, fault_addr, fault_addr, region, error_code);         
102     }
103
104     return 0;
105 }
106
107
108
109
110 static inline int invalidate_addr_32_internal(struct guest_info * info, addr_t inv_addr,
111                                               addr_t *actual_start, uint64_t *actual_size) {
112     pde32_t * pde = NULL;
113     pte32_t * pte = NULL;
114
115     // TODO:
116     // Call INVLPGA
117
118     // clear the page table entry
119     int pde_index = PDE32_INDEX(inv_addr);
120     int pte_index = PTE32_INDEX(inv_addr);
121
122     
123     // Lookup the correct PDE address based on the PAGING MODE
124     if (info->shdw_pg_mode == SHADOW_PAGING) {
125         pde = CR3_TO_PDE32_VA(info->ctrl_regs.cr3);
126     } else {
127         pde = CR3_TO_PDE32_VA(info->direct_map_pt);
128     }    
129
130     if (pde[pde_index].present == 0) {
131         *actual_start = BASE_TO_PAGE_ADDR_4MB(PAGE_BASE_ADDR_4MB(inv_addr));
132         *actual_size = PAGE_SIZE_4MB;
133         return 0;
134     } else if (pde[pde_index].large_page) {
135         pde[pde_index].present = 0;
136         pde[pde_index].writable = 0;
137         pde[pde_index].user_page = 0;
138         *actual_start = BASE_TO_PAGE_ADDR_4MB(PAGE_BASE_ADDR_4MB(inv_addr));
139         *actual_size = PAGE_SIZE_4MB;
140         return 0;
141     }
142
143     pte = V3_VAddr((void*)BASE_TO_PAGE_ADDR(pde[pde_index].pt_base_addr));
144
145     pte[pte_index].present = 0;
146     pte[pte_index].writable = 0;
147     pte[pte_index].user_page = 0;
148
149     *actual_start = BASE_TO_PAGE_ADDR_4KB(PAGE_BASE_ADDR_4KB(inv_addr));
150     *actual_size = PAGE_SIZE_4KB;
151
152     return 0;
153 }
154
155
156 static inline int invalidate_addr_32(struct guest_info * core, addr_t inv_addr)
157 {
158   addr_t start;
159   uint64_t len;
160   
161   return invalidate_addr_32_internal(core,inv_addr,&start,&len);
162 }
163    
164 static inline int invalidate_addr_32_range(struct guest_info * core, addr_t inv_addr_start, addr_t inv_addr_end)
165 {
166   addr_t next;
167   addr_t start;
168   uint64_t len;
169   int rc;
170   
171   for (next=inv_addr_start; next<=inv_addr_end; ) {
172     rc = invalidate_addr_32_internal(core,next,&start, &len);
173     if (rc) { 
174       return rc;
175     }
176     next = start + len;
177   }
178   return 0;
179 }
180
181
182
183 #endif