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.


Avoid strict-aliasing related issues when compiling with optimization
[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
8 #include "memtrack.h"
9
10 /**************************/
11 /* Access to guest memory */
12 /**************************/
13
14 struct v3_guest_mem_block {
15   void     *gpa;      // guest physical address this region starts at
16   void     *cumgpa;   // cumulative GPA in the VM including this block
17   void     *hpa;      // mapped to this host physical address
18   void     *uva;      // which is mapped here in this process
19   uint64_t numpages;  // this many 4K pages
20 };
21
22 // Whole memory map of guest's physical memory
23 // that is backed by host physical memory (ie, everything we 
24 // can read or write from the host user space)
25 struct v3_guest_mem_map {
26   int      fd;              // used by mmap
27   uint64_t numblocks;
28   struct v3_guest_mem_block block[0];
29 };
30
31 // This function gets the basic memory map, but does not map it
32 struct v3_guest_mem_map * v3_guest_mem_get_map(char *vmdev);
33 // This function mmaps it into the guest address space
34 // and fills out the "myaddr" fields
35 int v3_map_guest_mem(struct v3_guest_mem_map *map);
36 // This function unmaps it - it assumes myaddrs are valid
37 int v3_unmap_guest_mem(struct v3_guest_mem_map *map);
38 // What is the first GPA of this guest that contains memory ?
39 void *v3_gpa_start(struct v3_guest_mem_map *map);
40 // What is the last GPA of this guest that contains memory ?
41 void *v3_gpa_end(struct v3_guest_mem_map *map);
42 // map from a gpa to a uva, optionally giving the number of 
43 // subsequent bytes for which the mapping is continguous
44 static inline void *v3_gpa_to_uva(struct v3_guest_mem_map *map, void *gpa, uint64_t *num_bytes)
45 {
46     uint64_t left, right, middle;
47
48     if (!map->fd) { 
49         // not mapped
50         if (num_bytes) { *num_bytes=0;}
51         return 0;
52     }
53     
54     left = 0; right=map->numblocks-1;
55     
56     while (right>=left) { 
57         middle = (right+left)/2;
58         if (gpa > map->block[middle].cumgpa) { 
59             left=middle+1;
60         } else if (gpa < map->block[middle].gpa) { 
61             right=middle-1;
62         } else {
63             break;
64         }
65     }
66
67     if (right>=left) { 
68         if (num_bytes) { 
69             *num_bytes = map->block[middle].cumgpa - gpa + 1;
70         }
71         return map->block[middle].uva + (gpa - map->block[middle].gpa);
72     } else {
73         if (num_bytes) { 
74             *num_bytes=0;
75         } 
76         return 0;
77     }
78 }
79
80 // efficiently map function over the specified guest memory
81 int v3_guest_mem_apply(void (*func)(void *data, uint64_t num_bytes, void *priv),
82                        struct v3_guest_mem_map *map, void *gpa, uint64_t num_bytes, void *priv);
83
84 // read data out of guest
85 int v3_guest_mem_read(struct v3_guest_mem_map *map, void *gpa, uint64_t num_bytes, char *data);
86
87 // write data into  guest
88 int v3_guest_mem_write(struct v3_guest_mem_map *map, void *gpa, uint64_t num_bytes, char *data);
89
90 // hash the guest's data
91 int v3_guest_mem_hash(struct v3_guest_mem_map *map, void *gpa, uint64_t num_bytes, uint64_t *hash);
92
93
94 /********************************/
95 /* Guest memory access tracking */
96 /********************************/
97
98 #include "../linux_module/memtrack.h"
99
100 int v3_guest_mem_track_start(char *vmdev, 
101                              v3_mem_track_access_t access, 
102                              v3_mem_track_reset_t reset, 
103                              uint64_t period);
104
105 int v3_guest_mem_track_stop(char *vmdev);
106
107 v3_mem_track_snapshot *v3_guest_mem_track_snapshot(char *vmdev);
108
109 void v3_guest_mem_track_free_snapshot(v3_mem_track_snapshot *snap);
110
111
112
113 #endif
114