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.


80dce910d38e3c6fc08d9b12414ddb6d9fa4a6b8
[palacios.git] / palacios / src / palacios / vmm_direct_paging_64.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_64_H__
22 #define __VMM_DIRECT_PAGING_64_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
30 // Reference: AMD Software Developer Manual Vol.2 Ch.5 "Page Translation and Protection"
31
32 static int get_page_size() {
33
34     // Need to fix this....
35     return PAGE_SIZE_4KB; 
36
37
38 #if 0
39    struct v3_mem_region * base_reg = &(info->vm_info->mem_map.base_region);
40
41    /* If the guest has been configured for 2MiB pages, then we must check for hooked regions of
42      * memory which may overlap with the 2MiB page containing the faulting address (due to
43      * potentially differing access policies in place for e.g. i/o devices and APIC). A 2MiB page
44      * can be used if a) no region overlaps the page [or b) a region does overlap but fully contains
45      * the page]. The [bracketed] text pertains to the #if 0'd code below, state D. TODO modify this
46      * note if someone decides to enable this optimization. It can be tested with the SeaStar
47      * mapping.
48      *
49      * Examples: (CAPS regions are returned by v3_get_next_mem_region; state A returns the base reg)
50      *
51      *    |region| |region|                               2MiB mapped (state A)
52      *                   |reg|          |REG|             2MiB mapped (state B)
53      *   |region|     |reg|   |REG| |region|   |reg|      4KiB mapped (state C)
54      *        |reg|  |reg|   |--REGION---|                [2MiB mapped (state D)]
55      * |--------------------------------------------|     RAM
56      *                             ^                      fault addr
57      * |----|----|----|----|----|page|----|----|----|     2MB pages
58      *                           >>>>>>>>>>>>>>>>>>>>     search space
59      */
60     addr_t pg_start = 0UL, pg_end = 0UL; // 2MiB page containing the faulting address
61     struct v3_mem_region * pg_next_reg = NULL; // next immediate mem reg after page start addr
62     bool use_large_page = false;
63
64     if (region == NULL) {
65         PrintError("%s: invalid region, addr=%p\n", __FUNCTION__, (void *)fault_addr);
66         return -1;
67     }
68
69     // set use_large_page here
70     if (info->vm_info->paging_size == PAGING_2MB) {
71
72         // guest page maps to a host page + offset (so when we shift, it aligns with a host page)
73         pg_start = PAGE_ADDR_2MB(fault_addr);
74         pg_end = (pg_start + PAGE_SIZE_2MB);
75
76         PrintDebug("%s: page   [%p,%p) contains address\n", __FUNCTION__, (void *)pg_start, (void *)pg_end);
77
78         pg_next_reg = v3_get_next_mem_region(info->vm_info, info->cpu_id, pg_start);
79
80         if (pg_next_reg == NULL) {
81             PrintError("%s: Error: address not in base region, %p\n", __FUNCTION__, (void *)fault_addr);
82             return -1;
83         }
84
85         if (pg_next_reg->base == 1) { // next region == base region
86             use_large_page = 1; // State A
87         } else {
88 #if 0       // State B/C and D optimization
89             use_large_page = (pg_next_reg->guest_end >= pg_end) &&
90                 ((pg_next_reg->guest_start >= pg_end) || (pg_next_reg->guest_start <= pg_start));
91             PrintDebug("%s: region [%p,%p) %s partial overlap with page\n", __FUNCTION__,
92                     (void *)pg_next_reg->guest_start, (void *)pg_next_reg->guest_end,
93                     (use_large_page ? "does not have" : "has"));
94 #else       // State B/C
95             use_large_page = (pg_next_reg->guest_start >= pg_end);
96             PrintDebug("%s: region [%p,%p) %s overlap with page\n", __FUNCTION__,
97                     (void *)pg_next_reg->guest_start, (void *)pg_next_reg->guest_end,
98                     (use_large_page ? "does not have" : "has"));
99 #endif
100         }
101     }
102
103     PrintDebug("%s: Address gets a 2MiB page? %s\n", __FUNCTION__, (use_large_page ? "yes" : "no"));
104 #endif
105 }
106
107
108 static inline int handle_passthrough_pagefault_64(struct guest_info * core, addr_t fault_addr, pf_error_t error_code) {
109     pml4e64_t * pml      = NULL;
110     pdpe64_t * pdpe      = NULL;
111     pde64_t * pde        = NULL;
112     pde64_2MB_t * pde2mb = NULL;
113     pte64_t * pte        = NULL;
114     addr_t host_addr     = 0;
115
116     int pml_index  = PML4E64_INDEX(fault_addr);
117     int pdpe_index = PDPE64_INDEX(fault_addr);
118     int pde_index  = PDE64_INDEX(fault_addr);
119     int pte_index  = PTE64_INDEX(fault_addr);
120
121     struct v3_mem_region * region =  v3_get_mem_region(core->vm_info, core->cpu_id, fault_addr);
122     int page_size = PAGE_SIZE_4KB;
123
124
125     /*  Check if:
126      *  1. the guest is configured to use large pages and 
127      *  2. the memory regions can be referenced by a large page
128      */
129     if ((core->use_large_pages == 1) ) {
130         page_size = get_page_size();
131     }
132
133  
134     // Lookup the correct PML address based on the PAGING MODE
135     if (core->shdw_pg_mode == SHADOW_PAGING) {
136         pml = CR3_TO_PML4E64_VA(core->ctrl_regs.cr3);
137     } else {
138         pml = CR3_TO_PML4E64_VA(core->direct_map_pt);
139     }
140
141     //Fix up the PML entry
142     if (pml[pml_index].present == 0) {
143         pdpe = (pdpe64_t *)create_generic_pt_page();
144    
145         // Set default PML Flags...
146         pml[pml_index].present = 1;
147         pml[pml_index].writable = 1;
148         pml[pml_index].user_page = 1;
149
150         pml[pml_index].pdp_base_addr = PAGE_BASE_ADDR_4KB((addr_t)V3_PAddr(pdpe));    
151     } else {
152         pdpe = V3_VAddr((void*)BASE_TO_PAGE_ADDR_4KB(pml[pml_index].pdp_base_addr));
153     }
154
155     // Fix up the PDPE entry
156     if (pdpe[pdpe_index].present == 0) {
157         pde = (pde64_t *)create_generic_pt_page();
158         
159         // Set default PDPE Flags...
160         pdpe[pdpe_index].present = 1;
161         pdpe[pdpe_index].writable = 1;
162         pdpe[pdpe_index].user_page = 1;
163
164         pdpe[pdpe_index].pd_base_addr = PAGE_BASE_ADDR_4KB((addr_t)V3_PAddr(pde));    
165     } else {
166         pde = V3_VAddr((void*)BASE_TO_PAGE_ADDR_4KB(pdpe[pdpe_index].pd_base_addr));
167     }
168
169     // Fix up the 2MiB PDE and exit here
170     if (page_size == PAGE_SIZE_2MB) {
171         pde2mb = (pde64_2MB_t *)pde; // all but these two lines are the same for PTE
172         pde2mb[pde_index].large_page = 1;
173
174         if (pde2mb[pde_index].present == 0) {
175             pde2mb[pde_index].user_page = 1;
176
177             if ( (region->flags.alloced == 1) && 
178                  (region->flags.read == 1)) {
179                 // Full access
180                 pde2mb[pde_index].present = 1;
181
182                 if (region->flags.write == 1) {
183                     pde2mb[pde_index].writable = 1;
184                 } else {
185                     pde2mb[pde_index].writable = 0;
186                 }
187
188                 if (v3_gpa_to_hpa(core, fault_addr, &host_addr) == -1) {
189                     PrintError("Error Could not translate fault addr (%p)\n", (void *)fault_addr);
190                     return -1;
191                 }
192
193                 pde2mb[pde_index].page_base_addr = PAGE_BASE_ADDR_2MB(host_addr);
194             } else {
195                 return region->unhandled(core, fault_addr, fault_addr, region, error_code);
196             }
197         } else {
198             // We fix all permissions on the first pass, 
199             // so we only get here if its an unhandled exception
200
201             return region->unhandled(core, fault_addr, fault_addr, region, error_code);
202         }
203
204         // All done
205         return 0;
206     } 
207
208     // Continue with the 4KiB page heirarchy
209     
210     // Fix up the PDE entry
211     if (pde[pde_index].present == 0) {
212         pte = (pte64_t *)create_generic_pt_page();
213         
214         pde[pde_index].present = 1;
215         pde[pde_index].writable = 1;
216         pde[pde_index].user_page = 1;
217         
218         pde[pde_index].pt_base_addr = PAGE_BASE_ADDR_4KB((addr_t)V3_PAddr(pte));
219     } else {
220         pte = V3_VAddr((void*)BASE_TO_PAGE_ADDR_4KB(pde[pde_index].pt_base_addr));
221     }
222
223     // Fix up the PTE entry
224     if (pte[pte_index].present == 0) {
225         pte[pte_index].user_page = 1;
226         
227         if ((region->flags.alloced == 1) && 
228             (region->flags.read == 1)) {
229             // Full access
230             pte[pte_index].present = 1;
231
232             if (region->flags.write == 1) {
233                 pte[pte_index].writable = 1;
234             } else {
235                 pte[pte_index].writable = 0;
236             }
237
238             if (v3_gpa_to_hpa(core, fault_addr, &host_addr) == -1) {
239                 PrintError("Error Could not translate fault addr (%p)\n", (void *)fault_addr);
240                 return -1;
241             }
242
243             pte[pte_index].page_base_addr = PAGE_BASE_ADDR_4KB(host_addr);
244         } else {
245             return region->unhandled(core, fault_addr, fault_addr, region, error_code);
246         }
247     } else {
248         // We fix all permissions on the first pass, 
249         // so we only get here if its an unhandled exception
250
251         return region->unhandled(core, fault_addr, fault_addr, region, error_code);
252     }
253
254     return 0;
255 }
256
257 static inline int invalidate_addr_64(struct guest_info * core, addr_t inv_addr) {
258     pml4e64_t * pml = NULL;
259     pdpe64_t * pdpe = NULL;
260     pde64_t * pde = NULL;
261     pte64_t * pte = NULL;
262
263
264     // TODO:
265     // Call INVLPGA
266
267     // clear the page table entry
268     int pml_index = PML4E64_INDEX(inv_addr);
269     int pdpe_index = PDPE64_INDEX(inv_addr);
270     int pde_index = PDE64_INDEX(inv_addr);
271     int pte_index = PTE64_INDEX(inv_addr);
272
273     
274     // Lookup the correct PDE address based on the PAGING MODE
275     if (core->shdw_pg_mode == SHADOW_PAGING) {
276         pml = CR3_TO_PML4E64_VA(core->ctrl_regs.cr3);
277     } else {
278         pml = CR3_TO_PML4E64_VA(core->direct_map_pt);
279     }
280
281     if (pml[pml_index].present == 0) {
282         return 0;
283     }
284
285     pdpe = V3_VAddr((void*)BASE_TO_PAGE_ADDR(pml[pml_index].pdp_base_addr));
286
287     if (pdpe[pdpe_index].present == 0) {
288         return 0;
289     } else if (pdpe[pdpe_index].large_page == 1) { // 1GiB
290         pdpe[pdpe_index].present = 0;
291         return 0;
292     }
293
294     pde = V3_VAddr((void*)BASE_TO_PAGE_ADDR(pdpe[pdpe_index].pd_base_addr));
295
296     if (pde[pde_index].present == 0) {
297         return 0;
298     } else if (pde[pde_index].large_page == 1) { // 2MiB
299         pde[pde_index].present = 0;
300         return 0;
301     }
302
303     pte = V3_VAddr((void*)BASE_TO_PAGE_ADDR(pde[pde_index].pt_base_addr));
304
305     pte[pte_index].present = 0; // 4KiB
306
307     return 0;
308 }
309
310
311
312 #endif