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, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
21 #ifndef __VMM_PAGING_H__
22 #define __VMM_PAGING_H__
27 #include <palacios/vmm_types.h>
28 #include <palacios/vmm_util.h>
33 In the following, when we say "page table", we mean the whole 2 or 4 layer
34 page table (PDEs, PTEs), etc.
37 guest-visible paging state
38 This is the state that the guest thinks the machine is using
40 - guest physical memory
41 The physical memory addresses the guest is allowed to use
42 (see shadow page maps, below)
44 (we care about when the current one changes)
45 - guest paging registers (these are never written to hardware)
51 This the state that the machine will actually use when the guest
52 is running. It consists of:
53 - current shadow page table
54 This is the page table actually useed when the guest is running.
55 It is changed/regenerated when the guest page table changes
56 It mostly reflects the guest page table, except that it restricts
57 physical addresses to those the VMM allocates to the guest.
59 This is a mapping from guest physical memory addresses to
60 the current location of the guest physical memory content.
61 It maps from regions of physical memory addresses to regions
62 located in physical memory or elsewhere.
63 (8192,16384) -> MEM(8912,...)
64 (0,8191) -> DISK(65536,..)
65 - guest paging registers (these are written to guest state)
70 This is the state we expect to be operative when the VMM is running.
71 Typically, this is set up by the host os into which we have embedded
72 the VMM, but we include the description here for clarity.
74 This is the page table we use when we are executing in
75 the VMM (or the host os)
81 The reason why the shadow paging state and the host paging state are
82 distinct is to permit the guest to use any virtual address it wants,
83 irrespective of the addresses the VMM or the host os use. These guest
84 virtual addresses are reflected in the shadow paging state. When we
85 exit from the guest, we switch to the host paging state so that any
86 virtual addresses that overlap between the guest and VMM/host now map
87 to the physical addresses epxected by the VMM/host. On AMD SVM, this
88 switch is done by the hardware. On Intel VT, the switch is done
89 by the hardware as well, but we are responsible for manually updating
90 the host state in the vmcs before entering the guest.
96 #define MAX_PDE32_ENTRIES 1024
97 #define MAX_PTE32_ENTRIES 1024
99 #define MAX_PDPE32PAE_ENTRIES 4
100 #define MAX_PDE32PAE_ENTRIES 512
101 #define MAX_PTE32PAE_ENTRIES 512
103 #define MAX_PML4E64_ENTRIES 512
104 #define MAX_PDPE64_ENTRIES 512
105 #define MAX_PDE64_ENTRIES 512
106 #define MAX_PTE64_ENTRIES 512
109 typedef enum {PAGE_4KB, PAGE_2MB, PAGE_4MB, PAGE_1GB,
111 PAGE_PT32, PAGE_PD32,
112 PAGE_PDP32PAE, PAGE_PD32PAE, PAGE_PT32PAE,
113 PAGE_PML464, PAGE_PDP64, PAGE_PD64, PAGE_PT64} page_type_t;
116 /* Converts an address into a page table index */
117 #define PDE32_INDEX(x) ((((uint_t)x) >> 22) & 0x3ff)
118 #define PTE32_INDEX(x) ((((uint_t)x) >> 12) & 0x3ff)
121 #define PDPE32PAE_INDEX(x) ((((uint_t)x) >> 30) & 0x3)
122 #define PDE32PAE_INDEX(x) ((((uint_t)x) >> 21) & 0x1ff)
123 #define PTE32PAE_INDEX(x) ((((uint_t)x) >> 12) & 0x1ff)
125 #define PML4E64_INDEX(x) ((((ullong_t)x) >> 39) & 0x1ff)
126 #define PDPE64_INDEX(x) ((((ullong_t)x) >> 30) & 0x1ff)
127 #define PDE64_INDEX(x) ((((ullong_t)x) >> 21) & 0x1ff)
128 #define PTE64_INDEX(x) ((((ullong_t)x) >> 12) & 0x1ff)
131 /* Gets the base address needed for a Page Table entry */
132 #define PAGE_BASE_ADDR(x) ((x) >> 12)
133 #define PAGE_BASE_ADDR_4KB(x) ((x) >> 12)
134 #define PAGE_BASE_ADDR_2MB(x) ((x) >> 21)
135 #define PAGE_BASE_ADDR_4MB(x) ((x) >> 22)
136 #define PAGE_BASE_ADDR_1GB(x) ((x) >> 30)
138 #define BASE_TO_PAGE_ADDR(x) (((addr_t)x) << 12)
139 #define BASE_TO_PAGE_ADDR_4KB(x) (((addr_t)x) << 12)
140 #define BASE_TO_PAGE_ADDR_2MB(x) (((addr_t)x) << 21)
141 #define BASE_TO_PAGE_ADDR_4MB(x) (((addr_t)x) << 22)
142 #define BASE_TO_PAGE_ADDR_1GB(x) (((addr_t)x) << 30)
146 #define PAGE_OFFSET(x) ((x) & 0xfff)
147 #define PAGE_OFFSET_4KB(x) ((x) & 0xfff)
148 #define PAGE_OFFSET_2MB(x) ((x) & 0x1fffff)
149 #define PAGE_OFFSET_4MB(x) ((x) & 0x3fffff)
150 #define PAGE_OFFSET_1GB(x) ((x) & 0x3fffffff)
152 #define PAGE_POWER 12
153 #define PAGE_POWER_4KB 12
154 #define PAGE_POWER_2MB 21
155 #define PAGE_POWER_4MB 22
156 #define PAGE_POWER_1GB 30
158 // We shift instead of mask because we don't know the address size
159 #define PAGE_ADDR(x) (((x) >> PAGE_POWER) << PAGE_POWER)
160 #define PAGE_ADDR_4KB(x) (((x) >> PAGE_POWER_4KB) << PAGE_POWER_4KB)
161 #define PAGE_ADDR_2MB(x) (((x) >> PAGE_POWER_2MB) << PAGE_POWER_2MB)
162 #define PAGE_ADDR_4MB(x) (((x) >> PAGE_POWER_4MB) << PAGE_POWER_4MB)
163 #define PAGE_ADDR_1GB(x) (((x) >> PAGE_POWER_1GB) << PAGE_POWER_1GB)
165 #define PAGE_SIZE 4096
166 #define PAGE_SIZE_4KB 4096
167 #define PAGE_SIZE_2MB (4096 * 512)
168 #define PAGE_SIZE_4MB (4096 * 1024)
169 #define PAGE_SIZE_1GB 0x40000000
177 #define CR3_TO_PDE32_PA(cr3) ((addr_t)(((uint_t)cr3) & 0xfffff000))
178 #define CR3_TO_PDPE32PAE_PA(cr3) ((addr_t)(((uint_t)cr3) & 0xffffffe0))
179 #define CR3_TO_PML4E64_PA(cr3) ((addr_t)(((ullong_t)cr3) & 0x000ffffffffff000LL))
181 #define CR3_TO_PDE32_VA(cr3) ((pde32_t *)V3_VAddr((void *)(addr_t)(((uint_t)cr3) & 0xfffff000)))
182 #define CR3_TO_PDPE32PAE_VA(cr3) ((pdpe32pae_t *)V3_VAddr((void *)(addr_t)(((uint_t)cr3) & 0xffffffe0)))
183 #define CR3_TO_PML4E64_VA(cr3) ((pml4e64_t *)V3_VAddr((void *)(addr_t)(((ullong_t)cr3) & 0x000ffffffffff000LL)))
190 /* Page Table Flag Values */
191 #define PT32_HOOK 0x1
192 #define V3_LARGE_PG 0x2
196 /* We'll use the general form for now....
197 typedef enum {PDE32_ENTRY_NOT_PRESENT, PDE32_ENTRY_PTE32, PDE32_ENTRY_LARGE_PAGE} pde32_entry_type_t;
198 typedef enum {PTE32_ENTRY_NOT_PRESENT, PTE32_ENTRY_PAGE} pte32_entry_type_t;
200 typedef enum {PDPE32PAE_ENTRY_NOT_PRESENT, PDPE32PAE_ENTRY_PAGE} pdpe32pae_entry_type_t;
201 typedef enum {PDE32PAE_ENTRY_NOT_PRESENT, PDE32PAE_ENTRY_PTE32, PDE32PAE_ENTRY_LARGE_PAGE} pde32pae_entry_type_t;
202 typedef enum {PTE32PAE_ENTRY_NOT_PRESENT, PTE32PAE_ENTRY_PAGE} pte32pae_entry_type_t;
204 typedef enum {PML4E64_ENTRY_NOT_PRESENT, PML4E64_ENTRY_PAGE} pml4e64_entry_type_t;
205 typedef enum {PDPE64_ENTRY_NOT_PRESENT, PDPE64_ENTRY_PTE32, PDPE64_ENTRY_LARGE_PAGE} pdpe64_entry_type_t;
206 typedef enum {PDE64_ENTRY_NOT_PRESENT, PDE64_ENTRY_PTE32, PDE64_ENTRY_LARGE_PAGE} pde64_entry_type_t;
207 typedef enum {PTE64_ENTRY_NOT_PRESENT, PTE64_ENTRY_PAGE} pte64_entry_type_t;
211 typedef enum {PT_ENTRY_NOT_PRESENT, PT_ENTRY_LARGE_PAGE, PT_ENTRY_PAGE} pt_entry_type_t;
212 typedef enum {PT_ACCESS_OK, PT_ACCESS_NOT_PRESENT, PT_ACCESS_WRITE_ERROR, PT_ACCESS_USER_ERROR} pt_access_status_t;
215 typedef struct gen_pt {
218 uint_t user_page : 1;
219 } __attribute__((packed)) gen_pt_t;
221 typedef struct pde32 {
224 uint_t user_page : 1;
225 uint_t write_through : 1;
226 uint_t cache_disable : 1;
229 uint_t large_page : 1;
230 uint_t global_page : 1;
232 uint_t pt_base_addr : 20;
233 } __attribute__((packed)) pde32_t;
235 typedef struct pde32_4MB {
238 uint_t user_page : 1;
239 uint_t write_through : 1;
240 uint_t cache_disable : 1;
243 uint_t large_page : 1;
244 uint_t global_page : 1;
248 uint_t page_base_addr : 10;
250 } __attribute__((packed)) pde32_4MB_t;
252 typedef struct pte32 {
255 uint_t user_page : 1;
256 uint_t write_through : 1;
257 uint_t cache_disable : 1;
261 uint_t global_page : 1;
263 uint_t page_base_addr : 20;
264 } __attribute__((packed)) pte32_t;
267 /* 32 bit PAE PAGE STRUCTURES */
268 typedef struct pdpe32pae {
270 uint_t rsvd : 2; // MBZ
271 uint_t write_through : 1;
272 uint_t cache_disable : 1;
275 uint_t rsvd2 : 2; // MBZ
277 uint_t pd_base_addr : 24;
278 uint_t rsvd3 : 28; // MBZ
279 } __attribute__((packed)) pdpe32pae_t;
283 typedef struct pde32pae {
286 uint_t user_page : 1;
287 uint_t write_through : 1;
288 uint_t cache_disable : 1;
291 uint_t large_page : 1;
292 uint_t global_page : 1;
294 uint_t pt_base_addr : 24;
296 } __attribute__((packed)) pde32pae_t;
298 typedef struct pde32pae_2MB {
301 uint_t user_page : 1;
302 uint_t write_through : 1;
303 uint_t cache_disable : 1;
307 uint_t global_page : 1;
311 uint_t page_base_addr : 15;
314 } __attribute__((packed)) pde32pae_2MB_t;
316 typedef struct pte32pae {
319 uint_t user_page : 1;
320 uint_t write_through : 1;
321 uint_t cache_disable : 1;
325 uint_t global_page : 1;
327 uint_t page_base_addr : 24;
329 } __attribute__((packed)) pte32pae_t;
338 /* LONG MODE 64 bit PAGE STRUCTURES */
339 typedef struct pml4e64 {
342 uint_t user_page : 1;
343 uint_t write_through : 1;
344 uint_t cache_disable : 1;
349 ullong_t pdp_base_addr : 40;
350 uint_t available : 11;
351 uint_t no_execute : 1;
352 } __attribute__((packed)) pml4e64_t;
355 typedef struct pdpe64 {
358 uint_t user_page : 1;
359 uint_t write_through : 1;
360 uint_t cache_disable : 1;
363 uint_t large_page : 1;
366 ullong_t pd_base_addr : 40;
367 uint_t available : 11;
368 uint_t no_execute : 1;
369 } __attribute__((packed)) pdpe64_t;
372 // We Don't support this
373 typedef struct pdpe64_1GB {
376 uint_t user_page : 1;
377 uint_t write_through : 1;
378 uint_t cache_disable : 1;
381 uint_t large_page : 1;
382 uint_t global_page : 1;
386 ullong_t page_base_addr : 22;
387 uint_t available : 11;
388 uint_t no_execute : 1;
389 } __attribute__((packed)) pdpe64_1GB_t;
393 typedef struct pde64 {
396 uint_t user_page : 1;
397 uint_t write_through : 1;
398 uint_t cache_disable : 1;
401 uint_t large_page : 1;
402 uint_t global_page : 1;
404 ullong_t pt_base_addr : 40;
405 uint_t available : 11;
406 uint_t no_execute : 1;
407 } __attribute__((packed)) pde64_t;
409 typedef struct pde64_2MB {
412 uint_t user_page : 1;
413 uint_t write_through : 1;
414 uint_t cache_disable : 1;
417 uint_t large_page : 1;
418 uint_t global_page : 1;
422 ullong_t page_base_addr : 31;
423 uint_t available : 11;
424 uint_t no_execute : 1;
425 } __attribute__((packed)) pde64_2MB_t;
428 typedef struct pte64 {
431 uint_t user_page : 1;
432 uint_t write_through : 1;
433 uint_t cache_disable : 1;
437 uint_t global_page : 1;
439 ullong_t page_base_addr : 40;
440 uint_t available : 11;
441 uint_t no_execute : 1;
442 } __attribute__((packed)) pte64_t;
444 /* *************** */
446 typedef struct pf_error_code {
447 uint_t present : 1; // if 0, fault due to page not present
448 uint_t write : 1; // if 1, faulting access was a write
449 uint_t user : 1; // if 1, faulting access was in user mode
450 uint_t rsvd_access : 1; // if 1, fault from reading a 1 from a reserved field (?)
451 uint_t ifetch : 1; // if 1, faulting access was an instr fetch (only with NX)
453 } __attribute__((packed)) pf_error_t;
461 int v3_translate_guest_pt_32(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr, addr_t * paddr);
462 int v3_translate_guest_pt_32pae(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr, addr_t * paddr);
463 int v3_translate_guest_pt_64(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr, addr_t * paddr);
465 int v3_translate_host_pt_32(struct guest_info * info, v3_reg_t host_cr3, addr_t vaddr, addr_t * paddr);
466 int v3_translate_host_pt_32pae(struct guest_info * info, v3_reg_t host_cr3, addr_t vaddr, addr_t * paddr);
467 int v3_translate_host_pt_64(struct guest_info * info, v3_reg_t host_cr3, addr_t vaddr, addr_t * paddr);
470 int v3_find_host_pt_32_page(struct guest_info * info, v3_reg_t host_cr3, page_type_t type, addr_t vaddr,
471 addr_t * page_ptr, addr_t * page_pa);
472 int v3_find_host_pt_32pae_page(struct guest_info * info, v3_reg_t host_cr3, page_type_t type, addr_t vaddr,
473 addr_t * page_ptr, addr_t * page_pa);
474 int v3_find_host_pt_64_page(struct guest_info * info, v3_reg_t host_cr3, page_type_t type, addr_t vaddr,
475 addr_t * page_ptr, addr_t * page_pa);
476 int v3_find_guest_pt_32_page(struct guest_info * info, v3_reg_t guest_cr3,
477 page_type_t type, addr_t vaddr,
478 addr_t * page_ptr, addr_t * page_pa);
479 int v3_find_guest_pt_32pae_page(struct guest_info * info, v3_reg_t guest_cr3,
480 page_type_t type, addr_t vaddr,
481 addr_t * page_ptr, addr_t * page_pa);
482 int v3_find_guest_pt_64_page(struct guest_info * info, v3_reg_t guest_cr3,
483 page_type_t type, addr_t vaddr,
484 addr_t * page_ptr, addr_t * page_pa);
488 pt_access_status_t inline v3_can_access_pde32(pde32_t * pde, addr_t addr, pf_error_t access_type);
489 pt_access_status_t inline v3_can_access_pte32(pte32_t * pte, addr_t addr, pf_error_t access_type);
491 pt_access_status_t inline v3_can_access_pdpe32pae(pdpe32pae_t * pdpe, addr_t addr, pf_error_t access_type);
492 pt_access_status_t inline v3_can_access_pde32pae(pde32pae_t * pde, addr_t addr, pf_error_t access_type);
493 pt_access_status_t inline v3_can_access_pte32pae(pte32pae_t * pte, addr_t addr, pf_error_t access_type);
495 pt_access_status_t inline v3_can_access_pml4e64(pml4e64_t * pmle, addr_t addr, pf_error_t access_type);
496 pt_access_status_t inline v3_can_access_pdpe64(pdpe64_t * pdpe, addr_t addr, pf_error_t access_type);
497 pt_access_status_t inline v3_can_access_pde64(pde64_t * pde, addr_t addr, pf_error_t access_type);
498 pt_access_status_t inline v3_can_access_pte64(pte64_t * pte, addr_t addr, pf_error_t access_type);
501 int v3_check_host_pt_32(struct guest_info * info, v3_reg_t host_cr3, addr_t vaddr,
502 pf_error_t access_type, pt_access_status_t * access_status);
503 int v3_check_host_pt_32pae(struct guest_info * info, v3_reg_t host_cr3, addr_t vaddr,
504 pf_error_t access_type, pt_access_status_t * access_status);
505 int v3_check_host_pt_64(struct guest_info * info, v3_reg_t host_cr3, addr_t vaddr,
506 pf_error_t access_type, pt_access_status_t * access_status);
507 int v3_check_guest_pt_32(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr,
508 pf_error_t access_type, pt_access_status_t * access_status);
509 int v3_check_guest_pt_32pae(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr,
510 pf_error_t access_type, pt_access_status_t * access_status);
511 int v3_check_guest_pt_64(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr,
512 pf_error_t access_type, pt_access_status_t * access_status);
516 page_type_t v3_get_guest_data_page_type_32(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr);
517 page_type_t v3_get_guest_data_page_type_32pae(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr);
518 page_type_t v3_get_guest_data_page_type_64(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr);
519 page_type_t v3_get_host_data_page_type_32(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr);
520 page_type_t v3_get_host_data_page_type_32pae(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr);
521 page_type_t v3_get_host_data_page_type_64(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr);
524 int v3_drill_host_pt_32(struct guest_info * info, v3_reg_t host_cr3, addr_t vaddr,
525 int (*callback)(struct guest_info * info, page_type_t type, addr_t vaddr, addr_t page_ptr, addr_t page_pa, void * private_data),
526 void * private_data);
527 int v3_drill_host_pt_32pae(struct guest_info * info, v3_reg_t host_cr3, addr_t vaddr,
528 int (*callback)(struct guest_info * info, page_type_t type, addr_t vaddr, addr_t page_ptr, addr_t page_pa, void * private_data),
529 void * private_data);
530 int v3_drill_host_pt_64(struct guest_info * info, v3_reg_t host_cr3, addr_t vaddr,
531 int (*callback)(struct guest_info * info, page_type_t type, addr_t vaddr, addr_t page_ptr, addr_t page_pa, void * private_data),
532 void * private_data);
534 int v3_drill_guest_pt_32(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr,
535 int (*callback)(struct guest_info * info, page_type_t type, addr_t vaddr, addr_t page_ptr, addr_t page_pa, void * private_data),
536 void * private_data);
537 int v3_drill_guest_pt_32pae(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr,
538 int (*callback)(struct guest_info * info, page_type_t type, addr_t vaddr, addr_t page_ptr, addr_t page_pa, void * private_data),
539 void * private_data);
540 int v3_drill_guest_pt_64(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr,
541 int (*callback)(struct guest_info * info, page_type_t type, addr_t vaddr, addr_t page_ptr, addr_t page_pa, void * private_data),
542 void * private_data);
547 int v3_walk_host_pt_32(struct guest_info * info, v3_reg_t host_cr3,
548 int (*callback)(struct guest_info * info, page_type_t type, addr_t vaddr, addr_t page_va, addr_t page_pa, void * private_data),
549 void * private_data);
551 int v3_walk_host_pt_32pae(struct guest_info * info, v3_reg_t host_cr3,
552 int (*callback)(struct guest_info * info, page_type_t type, addr_t vaddr, addr_t page_va, addr_t page_pa, void * private_data),
553 void * private_data);
555 int v3_walk_host_pt_64(struct guest_info * info, v3_reg_t host_cr3,
556 int (*callback)(struct guest_info * info, page_type_t type, addr_t vaddr, addr_t page_va, addr_t page_pa, void * private_data),
557 void * private_data);
559 int v3_walk_guest_pt_32(struct guest_info * info, v3_reg_t guest_cr3,
560 int (*callback)(struct guest_info * info, page_type_t type, addr_t vaddr, addr_t page_va, addr_t page_pa, void * private_data),
561 void * private_data);
563 int v3_walk_guest_pt_32pae(struct guest_info * info, v3_reg_t guest_cr3,
564 int (*callback)(struct guest_info * info, page_type_t type, addr_t vaddr, addr_t page_va, addr_t page_pa, void * private_data),
565 void * private_data);
567 int v3_walk_guest_pt_64(struct guest_info * info, v3_reg_t guest_cr3,
568 int (*callback)(struct guest_info * info, page_type_t type, addr_t vaddr, addr_t page_va, addr_t page_pa, void * private_data),
569 void * private_data);
572 pde32_t * create_passthrough_pts_32(struct guest_info * guest_info);
573 pdpe32pae_t * create_passthrough_pts_32PAE(struct guest_info * guest_info);
574 pml4e64_t * create_passthrough_pts_64(struct guest_info * info);
577 void delete_page_tables_32(pde32_t * pde);
578 void delete_page_tables_32PAE(pdpe32pae_t * pdpe);
579 void delete_page_tables_64(pml4e64_t * pml4);
583 const uchar_t * v3_page_type_to_str(page_type_t type);
587 void PrintPTEntry(struct guest_info * info, page_type_t type, addr_t vaddr, void * entry);
588 void PrintHostPageTables(struct guest_info * info, addr_t cr3);
589 void PrintGuestPageTables(struct guest_info * info, addr_t cr3);
590 void PrintHostPageTree(struct guest_info * info, addr_t virtual_addr, addr_t cr3);
591 void PrintGuestPageTree(struct guest_info * info, addr_t virtual_addr, addr_t cr3);