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.


Refactoring and additions to direct paging (nested and passthrough)
[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 /* This always build 2 level page tables - no large pages are used */
33
34 static inline int handle_passthrough_pagefault_32(struct guest_info * info, 
35                                                   addr_t fault_addr, 
36                                                   pf_error_t error_code,
37                                                   addr_t *actual_start, addr_t *actual_end) {
38
39     // Check to see if pde and pte exist (create them if not)
40     pde32_t * pde = NULL;
41     pte32_t * pte = NULL;
42     addr_t host_addr = 0;
43     
44     int pde_index = PDE32_INDEX(fault_addr);
45     int pte_index = PTE32_INDEX(fault_addr);
46     
47     struct v3_mem_region * region = v3_get_mem_region(info->vm_info, info->vcpu_id, fault_addr);
48
49     if (region == NULL) {
50         PrintError(info->vm_info, info, "Invalid region in passthrough page fault 32, addr=%p\n", 
51                    (void *)fault_addr);
52         return -1;
53     }
54     
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);
58     } else {
59         pde = CR3_TO_PDE32_VA(info->direct_map_pt);
60     }
61
62
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;
65
66     // Fix up the PDE entry
67     if (pde[pde_index].present == 0) {
68         pte = (pte32_t *)create_generic_pt_page(info);
69         
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));
74         
75     } else {
76         pte = V3_VAddr((void*)BASE_TO_PAGE_ADDR(pde[pde_index].pt_base_addr));
77     }
78     
79     // Fix up the PTE entry
80     if (pte[pte_index].present == 0) {
81         
82         
83         if ((region->flags.alloced == 1) && 
84             (region->flags.read == 1)) {
85
86             pte[pte_index].user_page = 1;
87
88             pte[pte_index].present = 1;
89
90             if (region->flags.write == 1) {
91                 pte[pte_index].writable = 1;
92             } else {
93                 pte[pte_index].writable = 0;
94             }
95
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);
98                 return -1;
99             }
100             
101             pte[pte_index].page_base_addr = PAGE_BASE_ADDR(host_addr);
102         } else {
103             return region->unhandled(info, fault_addr, fault_addr, region, error_code);
104         }
105     } else {
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);         
109     }
110
111     return 0;
112 }
113
114
115
116
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;
121
122     // TODO:
123     // Call INVLPGA
124
125     // clear the page table entry
126     int pde_index = PDE32_INDEX(inv_addr);
127     int pte_index = PTE32_INDEX(inv_addr);
128
129     
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);
133     } else {
134         pde = CR3_TO_PDE32_VA(info->direct_map_pt);
135     }    
136
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;
140         return 0;
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;
147         return 0;
148     }
149
150     pte = V3_VAddr((void*)BASE_TO_PAGE_ADDR(pde[pde_index].pt_base_addr));
151
152     pte[pte_index].present = 0;
153     pte[pte_index].writable = 0;
154     pte[pte_index].user_page = 0;
155
156     *actual_start = BASE_TO_PAGE_ADDR_4KB(PAGE_BASE_ADDR_4KB(inv_addr));
157     *actual_size = PAGE_SIZE_4KB;
158
159     return 0;
160 }
161
162
163 static inline int invalidate_addr_32(struct guest_info * core, addr_t inv_addr,
164                                      addr_t *actual_start, addr_t *actual_end)
165 {
166   uint64_t len;
167   int rc;
168
169   rc = invalidate_addr_32_internal(core,inv_addr,actual_start,&len);
170
171   *actual_end = *actual_start + len - 1;
172
173   return rc;
174 }
175    
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)
178 {
179   addr_t next;
180   addr_t start;
181   uint64_t len;
182   int rc;
183   
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;
189     }
190    if (rc) { 
191       return rc;
192     }
193     next = start + len;
194     *actual_end = next;
195   }
196   // last iteration, actual_end is off by one
197   (*actual_end)--;
198   return 0;
199 }
200
201
202
203 #endif