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.


added invlpg support
[palacios.git] / palacios / src / palacios / vmm_paging.c
1 #include <palacios/vmm_paging.h>
2
3 #include <palacios/vmm.h>
4
5 #include <palacios/vm_guest_mem.h>
6
7
8 extern struct vmm_os_hooks * os_hooks;
9
10 void delete_page_tables_pde32(pde32_t * pde) {
11   int i, j;
12
13   if (pde == NULL) { 
14     return;
15   }
16
17   for (i = 0; (i < MAX_PDE32_ENTRIES); i++) {
18     if (pde[i].present) {
19       pte32_t * pte = (pte32_t *)(pde[i].pt_base_addr << PAGE_POWER);
20       
21       for (j = 0; (j < MAX_PTE32_ENTRIES); j++) {
22         if ((pte[j].present)) {
23           os_hooks->free_page((void *)(pte[j].page_base_addr << PAGE_POWER));
24         }
25       }
26       
27       os_hooks->free_page(pte);
28     }
29   }
30
31   os_hooks->free_page(pde);
32 }
33
34
35
36
37
38
39
40 /* We can't do a full lookup because we don't know what context the page tables are in...
41  * The entry addresses could be pointing to either guest physical memory or host physical memory
42  * Instead we just return the entry address, and a flag to show if it points to a pte or a large page...
43  */
44 pde32_entry_type_t pde32_lookup(pde32_t * pd, addr_t addr, addr_t * entry) {
45   pde32_t * pde_entry = &(pd[PDE32_INDEX(addr)]);
46
47   if (!pde_entry->present) {
48     *entry = 0;
49     return PDE32_ENTRY_NOT_PRESENT;
50   } else  {
51     *entry = PAGE_ADDR(pde_entry->pt_base_addr);
52     
53     if (pde_entry->large_page) {
54       *entry += PAGE_OFFSET(addr);
55       return PDE32_ENTRY_LARGE_PAGE;
56     } else {
57       *entry = PDE32_T_ADDR(*pde_entry);
58       return PDE32_ENTRY_PTE32;
59     }
60   }  
61   return PDE32_ENTRY_NOT_PRESENT;
62 }
63
64
65
66 /* Takes a virtual addr (addr) and returns the physical addr (entry) as defined in the page table
67  */
68 int pte32_lookup(pte32_t * pt, addr_t addr, addr_t * entry) {
69   pte32_t * pte_entry = &(pt[PTE32_INDEX(addr)]);
70
71   if (!pte_entry->present) {
72     *entry = 0;
73     PrintDebug("Lookup at non present page (index=%d)\n", PTE32_INDEX(addr));
74     return -1;
75   } else {
76     *entry = PTE32_T_ADDR(*pte_entry) + PT32_PAGE_OFFSET(addr);
77     return 0;
78   }
79
80   return -1;
81 }
82
83
84
85 pt_access_status_t can_access_pde32(pde32_t * pde, addr_t addr, pf_error_t access_type) {
86   pde32_t * entry = &pde[PDE32_INDEX(addr)];
87
88   if (entry->present == 0) {
89     return PT_ENTRY_NOT_PRESENT;
90   } else if ((entry->writable == 0) && (access_type.write == 1)) {
91     return PT_WRITE_ERROR;
92   } else if ((entry->user_page == 0) && (access_type.user == 1)) {
93     // Check CR0.WP
94     return PT_USER_ERROR;
95   }
96
97   return PT_ACCESS_OK;
98 }
99
100
101 pt_access_status_t can_access_pte32(pte32_t * pte, addr_t addr, pf_error_t access_type) {
102   pte32_t * entry = &pte[PTE32_INDEX(addr)];
103
104   if (entry->present == 0) {
105     return PT_ENTRY_NOT_PRESENT;
106   } else if ((entry->writable == 0) && (access_type.write == 1)) {
107     return PT_WRITE_ERROR;
108   } else if ((entry->user_page == 0) && (access_type.user == 1)) {
109     // Check CR0.WP
110     return PT_USER_ERROR;
111   }
112
113   return PT_ACCESS_OK;
114 }
115
116
117
118
119 /* We generate a page table to correspond to a given memory layout
120  * pulling pages from the mem_list when necessary
121  * If there are any gaps in the layout, we add them as unmapped pages
122  */
123 pde32_t * create_passthrough_pde32_pts(struct guest_info * guest_info) {
124   ullong_t current_page_addr = 0;
125   int i, j;
126   struct shadow_map * map = &(guest_info->mem_map);
127
128   pde32_t * pde = os_hooks->allocate_pages(1);
129
130   for (i = 0; i < MAX_PDE32_ENTRIES; i++) {
131     int pte_present = 0;
132     pte32_t * pte = os_hooks->allocate_pages(1);
133     
134
135     for (j = 0; j < MAX_PTE32_ENTRIES; j++) {
136       shadow_region_t * region = get_shadow_region_by_addr(map, current_page_addr);
137
138       if (!region || 
139           (region->host_type == HOST_REGION_NOTHING) || 
140           (region->host_type == HOST_REGION_UNALLOCATED) || 
141           (region->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE) || 
142           (region->host_type == HOST_REGION_REMOTE) ||
143           (region->host_type == HOST_REGION_SWAPPED)) {
144         pte[j].present = 0;
145         pte[j].writable = 0;
146         pte[j].user_page = 0;
147         pte[j].write_through = 0;
148         pte[j].cache_disable = 0;
149         pte[j].accessed = 0;
150         pte[j].dirty = 0;
151         pte[j].pte_attr = 0;
152         pte[j].global_page = 0;
153         pte[j].vmm_info = 0;
154         pte[j].page_base_addr = 0;
155       } else {
156         addr_t host_addr;
157         pte[j].present = 1;
158         pte[j].writable = 1;
159         pte[j].user_page = 1;
160         pte[j].write_through = 0;
161         pte[j].cache_disable = 0;
162         pte[j].accessed = 0;
163         pte[j].dirty = 0;
164         pte[j].pte_attr = 0;
165         pte[j].global_page = 0;
166         pte[j].vmm_info = 0;
167
168         if (guest_pa_to_host_pa(guest_info, current_page_addr, &host_addr) == -1) {
169           // BIG ERROR
170           // PANIC
171           return NULL;
172         }
173         
174         pte[j].page_base_addr = host_addr >> 12;
175         
176         pte_present = 1;
177       }
178
179       current_page_addr += PAGE_SIZE;
180     }
181
182     if (pte_present == 0) { 
183       os_hooks->free_page(pte);
184
185       pde[i].present = 0;
186       pde[i].writable = 0;
187       pde[i].user_page = 0;
188       pde[i].write_through = 0;
189       pde[i].cache_disable = 0;
190       pde[i].accessed = 0;
191       pde[i].reserved = 0;
192       pde[i].large_page = 0;
193       pde[i].global_page = 0;
194       pde[i].vmm_info = 0;
195       pde[i].pt_base_addr = 0;
196     } else {
197       pde[i].present = 1;
198       pde[i].writable = 1;
199       pde[i].user_page = 1;
200       pde[i].write_through = 0;
201       pde[i].cache_disable = 0;
202       pde[i].accessed = 0;
203       pde[i].reserved = 0;
204       pde[i].large_page = 0;
205       pde[i].global_page = 0;
206       pde[i].vmm_info = 0;
207       pde[i].pt_base_addr = PAGE_ALIGNED_ADDR(pte);
208     }
209
210   }
211
212   return pde;
213 }
214
215
216
217
218
219
220 void PrintPDE32(addr_t virtual_address, pde32_t * pde)
221 {
222   PrintDebug("PDE %p -> %p : present=%x, writable=%x, user=%x, wt=%x, cd=%x, accessed=%x, reserved=%x, largePages=%x, globalPage=%x, kernelInfo=%x\n",
223              virtual_address,
224              (void *) (pde->pt_base_addr << PAGE_POWER),
225              pde->present,
226              pde->writable,
227              pde->user_page, 
228              pde->write_through,
229              pde->cache_disable,
230              pde->accessed,
231              pde->reserved,
232              pde->large_page,
233              pde->global_page,
234              pde->vmm_info);
235 }
236   
237 void PrintPTE32(addr_t virtual_address, pte32_t * pte)
238 {
239   PrintDebug("PTE %p -> %p : present=%x, writable=%x, user=%x, wt=%x, cd=%x, accessed=%x, dirty=%x, pteAttribute=%x, globalPage=%x, vmm_info=%x\n",
240              virtual_address,
241              (void*)(pte->page_base_addr << PAGE_POWER),
242              pte->present,
243              pte->writable,
244              pte->user_page,
245              pte->write_through,
246              pte->cache_disable,
247              pte->accessed,
248              pte->dirty,
249              pte->pte_attr,
250              pte->global_page,
251              pte->vmm_info);
252 }
253
254
255
256 void PrintPD32(pde32_t * pde)
257 {
258   int i;
259
260   PrintDebug("Page Directory at %p:\n", pde);
261   for (i = 0; (i < MAX_PDE32_ENTRIES); i++) { 
262     if ( pde[i].present) {
263       PrintPDE32((addr_t)(PAGE_SIZE * MAX_PTE32_ENTRIES * i), &(pde[i]));
264     }
265   }
266 }
267
268 void PrintPT32(addr_t starting_address, pte32_t * pte) 
269 {
270   int i;
271
272   PrintDebug("Page Table at %p:\n", pte);
273   for (i = 0; (i < MAX_PTE32_ENTRIES) ; i++) { 
274     if (pte[i].present) {
275       PrintPTE32(starting_address + (PAGE_SIZE * i), &(pte[i]));
276     }
277   }
278 }
279
280
281
282
283
284 void PrintDebugPageTables(pde32_t * pde)
285 {
286   int i;
287   
288   PrintDebug("Dumping the pages starting with the pde page at %p\n", pde);
289
290   for (i = 0; (i < MAX_PDE32_ENTRIES); i++) { 
291     if (pde[i].present) {
292       PrintPDE32((addr_t)(PAGE_SIZE * MAX_PTE32_ENTRIES * i), &(pde[i]));
293       PrintPT32((addr_t)(PAGE_SIZE * MAX_PTE32_ENTRIES * i), (pte32_t *)(pde[i].pt_base_addr << PAGE_POWER));
294     }
295   }
296 }
297