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 memory conversions and copies for the guest/host contexts
[palacios.git] / palacios / src / geekos / vm_guest_mem.c
1 #include <geekos/vm_guest_mem.h>
2 #include <geekos/vmm.h>
3 #include <geekos/vmm_paging.h>
4
5 extern struct vmm_os_hooks * os_hooks;
6
7
8 /**********************************/
9 /* GROUP 0                        */
10 /**********************************/
11
12 int host_va_to_host_pa(addr_t host_va, addr_t * host_pa) {
13   if ((os_hooks) && (os_hooks)->vaddr_to_paddr) {
14
15     *host_pa = (addr_t)(os_hooks)->vaddr_to_paddr((void *)host_va);
16   
17     if (*host_pa == 0) {
18       return -1;
19     }
20   } else {
21     return -1;
22   }
23   return 0;
24 }
25
26
27 int host_pa_to_host_va(addr_t host_pa, addr_t * host_va) {
28   if ((os_hooks) && (os_hooks)->paddr_to_vaddr) {
29
30     *host_va = (addr_t)(os_hooks)->paddr_to_vaddr((void *)host_pa);
31     
32     if (*host_va == 0) {
33       return -1;
34     }
35   } else {
36     return -1;
37   }
38   return 0;
39 }
40
41
42
43 int guest_pa_to_host_pa(struct guest_info * guest_info, addr_t guest_pa, addr_t * host_pa) {
44   // we use the shadow map here...
45   if (lookup_shadow_map_addr(&(guest_info->mem_map), guest_pa, host_pa) != HOST_REGION_PHYSICAL_MEMORY) {
46     return -1;
47   }
48
49   return 0;
50 }
51
52
53 /* !! Currently not implemented !! */
54 // This is a scan of the shadow map
55 // For now we ignore it
56 // 
57 int host_pa_to_guest_pa(struct guest_info * guest_info, addr_t host_pa, addr_t * guest_pa) {
58   *guest_pa = 0;
59
60   return -1;
61 }
62
63
64
65 /**********************************/
66 /* GROUP 1                        */
67 /**********************************/
68
69
70 /* !! Currently not implemented !! */
71 // This will return negative until we implement host_pa_to_guest_pa()
72 int host_va_to_guest_pa(struct guest_info * guest_info, addr_t host_va, addr_t * guest_pa) {
73   addr_t host_pa;
74   *guest_pa = 0;
75
76   if (host_va_to_host_pa(host_va, &host_pa) != 0) {
77     return -1;
78   }
79
80   if (host_pa_to_guest_pa(guest_info, host_pa, guest_pa) != 0) {
81     return -1;
82   }
83
84   return 0;
85 }
86
87
88
89
90 int guest_pa_to_host_va(struct guest_info * guest_info, addr_t guest_pa, addr_t * host_va) {
91   addr_t host_pa;
92
93   *host_va = 0;
94
95   if (guest_pa_to_host_pa(guest_info, guest_pa, &host_pa) != 0) {
96     return -1;
97   }
98   
99   if (host_pa_to_host_va(host_pa, host_va) != 0) {
100     return -1;
101   }
102
103   return 0;
104 }
105
106
107 int guest_va_to_guest_pa(struct guest_info * guest_info, addr_t guest_va, addr_t * guest_pa) {
108   if (guest_info->page_mode == SHADOW_PAGING) {
109     switch (guest_info->cpu_mode) {
110     case REAL:
111     case PROTECTED:
112     case LONG:
113     case PROTECTED_PAE:
114       // guest virtual address is the same as the physical
115       *guest_pa = guest_va;
116       return 0;
117     case PROTECTED_PG:
118       {
119         addr_t tmp_pa;
120         pde32_t * pde;
121         addr_t guest_pde = CR3_TO_PDE32(guest_info->shdw_pg_state.guest_cr3.r_reg);
122
123         if (guest_pa_to_host_va(guest_info, guest_pde, (addr_t *)&pde) == -1) {
124           return -1;
125         }
126
127         switch (pde32_lookup(pde, guest_va, &tmp_pa)) {
128         case NOT_PRESENT: 
129           *guest_pa = 0;
130           return -1;
131         case LARGE_PAGE:
132           *guest_pa = tmp_pa;
133           return 0;
134         case PTE32:
135           {
136             pte32_t * pte;
137
138             if (guest_pa_to_host_va(guest_info, tmp_pa, (addr_t*)&pte) == -1) {
139               return -1;
140             }
141             
142             if (pte32_lookup(pte, guest_va, guest_pa) != 0) {
143               return -1;
144             }
145
146             return 0;
147           }
148         default:
149           return -1;
150         }
151       }
152       case PROTECTED_PAE_PG:
153         {
154           // Fill in
155         }
156       case LONG_PG:
157         {
158           // Fill in
159         }
160     default:
161       return -1;
162     }
163   } else if (guest_info->page_mode == NESTED_PAGING) {
164
165     // Fill in
166
167   } else {
168     return -1;
169   }
170
171
172   return 0;
173 }
174
175
176
177 /* !! Currently not implemented !! */
178 /* This will be a real pain.... its your standard page table walker in guest memory
179  * 
180  * For now we ignore it...
181  */
182 int guest_pa_to_guest_va(struct guest_info * guest_info, addr_t guest_pa, addr_t * guest_va) {
183   *guest_va = 0;
184   return -1;
185 }
186
187
188 /**********************************/
189 /* GROUP 2                        */
190 /**********************************/
191
192
193 int guest_va_to_host_pa(struct guest_info * guest_info, addr_t guest_va, addr_t * host_pa) {
194   addr_t guest_pa;
195
196   *host_pa = 0;
197
198   if (guest_va_to_guest_pa(guest_info, guest_va, &guest_pa) != 0) {
199     return -1;
200   }
201   
202   if (guest_pa_to_host_pa(guest_info, guest_pa, host_pa) != 0) {
203     return -1;
204   }
205
206   return 0;
207 }
208
209 /* !! Currently not implemented !! */
210 int host_pa_to_guest_va(struct guest_info * guest_info, addr_t host_pa, addr_t * guest_va) {
211   addr_t guest_pa;
212
213   *guest_va = 0;
214
215   if (host_pa_to_guest_pa(guest_info, host_pa, &guest_pa) != 0) {
216     return -1;
217   }
218
219   if (guest_pa_to_guest_va(guest_info, guest_pa, guest_va) != 0) {
220     return -1;
221   }
222
223   return 0;
224 }
225
226
227
228
229 int guest_va_to_host_va(struct guest_info * guest_info, addr_t guest_va, addr_t * host_va) {
230   addr_t guest_pa;
231   addr_t host_pa;
232
233   *host_va = 0;
234
235   if (guest_va_to_guest_pa(guest_info, guest_va, &guest_pa) != 0) {
236     return -1;
237   }
238
239   if (guest_pa_to_host_pa(guest_info, guest_pa, &host_pa) != 0) {
240     return -1;
241   }
242
243   if (host_pa_to_host_va(host_pa, host_va) != 0) {
244     return -1;
245   }
246
247   return 0;
248 }
249
250
251 /* !! Currently not implemented !! */
252 int host_va_to_guest_va(struct guest_info * guest_info, addr_t host_va, addr_t * guest_va) {
253   addr_t host_pa;
254   addr_t guest_pa;
255
256   *guest_va = 0;
257
258   if (host_va_to_host_pa(host_va, &host_pa) != 0) {
259     return -1;
260   }
261
262   if (host_pa_to_guest_pa(guest_info, host_pa, &guest_pa) != 0) {
263     return -1;
264   }
265
266   if (guest_pa_to_guest_va(guest_info, guest_pa, guest_va) != 0) {
267     return -1;
268   }
269
270   return 0;
271 }
272
273
274
275
276
277
278 /* This is a straight address conversion + copy, 
279  *   except for the tiny little issue of crossing page boundries.....
280  */
281 int read_guest_va_memory(struct guest_info * guest_info, addr_t guest_va, int count, char * dest) {
282   addr_t cursor = guest_va;
283
284   while (count > 0) {
285     int dist_to_pg_edge = (PAGE_OFFSET(cursor) + PAGE_SIZE) - cursor;
286     int bytes_to_copy = (dist_to_pg_edge > count) ? count : dist_to_pg_edge;
287     addr_t host_addr;
288
289     if (guest_va_to_host_va(guest_info, cursor, &host_addr) != 0) {
290       return -1;
291     }
292
293     memcpy(dest, (void*)cursor, bytes_to_copy);
294
295     count -= bytes_to_copy;
296     cursor += bytes_to_copy;    
297   }
298
299   return 0;
300 }
301
302
303
304
305
306
307 /* This is a straight address conversion + copy, 
308  *   except for the tiny little issue of crossing page boundries.....
309  */
310 int read_guest_pa_memory(struct guest_info * guest_info, addr_t guest_pa, int count, char * dest) {
311   addr_t cursor = guest_pa;
312
313   while (count > 0) {
314     int dist_to_pg_edge = (PAGE_OFFSET(cursor) + PAGE_SIZE) - cursor;
315     int bytes_to_copy = (dist_to_pg_edge > count) ? count : dist_to_pg_edge;
316     addr_t host_addr;
317
318     if (guest_pa_to_host_va(guest_info, cursor, &host_addr) != 0) {
319       return -1;
320     }
321
322     memcpy(dest, (void*)cursor, bytes_to_copy);
323
324     count -= bytes_to_copy;
325     cursor += bytes_to_copy;    
326   }
327
328   return 0;
329 }
330