10 /**************************/
11 /* Access to guest memory */
12 /**************************/
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
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
28 struct v3_guest_mem_block block[0];
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)
46 uint64_t left, right, middle;
50 if (num_bytes) { *num_bytes=0;}
54 left = 0; right=map->numblocks-1;
57 middle = (right+left)/2;
58 if (gpa > map->block[middle].cumgpa) {
60 } else if (gpa < map->block[middle].gpa) {
69 *num_bytes = map->block[middle].cumgpa - gpa + 1;
71 return map->block[middle].uva + (gpa - map->block[middle].gpa);
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);
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);
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);
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);
94 /********************************/
95 /* Guest memory access tracking */
96 /********************************/
98 #include "../linux_module/memtrack.h"
100 int v3_guest_mem_track_start(char *vmdev,
101 v3_mem_track_access_t access,
102 v3_mem_track_reset_t reset,
105 int v3_guest_mem_track_stop(char *vmdev);
107 v3_mem_track_snapshot *v3_guest_mem_track_snapshot(char *vmdev);
109 void v3_guest_mem_track_free_snapshot(v3_mem_track_snapshot *snap);