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.


508f3514da2b092dbc5b047b0ac59c74d85e9a9f
[palacios.git] / linux_usr / v3_guest_mem.h
1 #ifndef _V3_GUEST_MEM_
2 #define _V3_GUEST_MEM_
3
4 #include <stdint.h>
5 #include "v3_ctrl.h"
6
7 struct v3_guest_mem_block {
8   void     *gpa;      // guest physical address this region starts at
9   void     *cumgpa;   // cumulative GPA in the VM including this block
10   void     *hpa;      // mapped to this host physical address
11   void     *uva;      // which is mapped here in this process
12   uint64_t numpages;  // this many 4K pages
13 };
14
15 // Whole memory map of guest's physical memory
16 // that is backed by host physical memory (ie, everything we 
17 // can read or write from the host user space)
18 struct v3_guest_mem_map {
19   int      fd;              // used by mmap
20   uint64_t numblocks;
21   struct v3_guest_mem_block block[0];
22 };
23
24 // This function gets the basic memory map, but does not map it
25 struct v3_guest_mem_map * v3_guest_mem_get_map(char *vmdev);
26 // This function mmaps it into the guest address space
27 // and fills out the "myaddr" fields
28 int v3_map_guest_mem(struct v3_guest_mem_map *map);
29 // This function unmaps it - it assumes myaddrs are valid
30 int v3_unmap_guest_mem(struct v3_guest_mem_map *map);
31 // What is the first GPA of this guest that contains memory ?
32 void *v3_gpa_start(struct v3_guest_mem_map *map);
33 // What is the last GPA of this guest that contains memory ?
34 void *v3_gpa_end(struct v3_guest_mem_map *map);
35 // map from a gpa to a uva, optionally giving the number of 
36 // subsequent bytes for which the mapping is continguous
37 static inline void *v3_gpa_to_uva(struct v3_guest_mem_map *map, void *gpa, uint64_t *num_bytes)
38 {
39     uint64_t left, right, middle;
40
41     if (!map->fd) { 
42         // not mapped
43         if (num_bytes) { *num_bytes=0;}
44         return 0;
45     }
46     
47     left = 0; right=map->numblocks-1;
48     
49     while (right>=left) { 
50         middle = (right+left)/2;
51         if (gpa > map->block[middle].cumgpa) { 
52             left=middle+1;
53         } else if (gpa < map->block[middle].gpa) { 
54             right=middle-1;
55         } else {
56             break;
57         }
58     }
59
60     if (right>=left) { 
61         if (num_bytes) { 
62             *num_bytes = map->block[middle].cumgpa - gpa + 1;
63         }
64         return map->block[middle].uva + (gpa - map->block[middle].gpa);
65     } else {
66         if (num_bytes) { 
67             *num_bytes=0;
68         } 
69         return 0;
70     }
71 }
72
73 // efficiently map function over the specified guest memory
74 int v3_guest_mem_apply(void (*func)(void *data, uint64_t num_bytes, void *priv),
75                        struct v3_guest_mem_map *map, void *gpa, uint64_t num_bytes, void *priv);
76
77 // read data out of guest
78 int v3_guest_mem_read(struct v3_guest_mem_map *map, void *gpa, uint64_t num_bytes, char *data);
79
80 // write data into  guest
81 int v3_guest_mem_write(struct v3_guest_mem_map *map, void *gpa, uint64_t num_bytes, char *data);
82
83 // hash the guest's data
84 int v3_guest_mem_hash(struct v3_guest_mem_map *map, void *gpa, uint64_t num_bytes, uint64_t *hash);
85
86 #endif
87