4 #include "v3_guest_mem.h"
8 fprintf(stderr,"usage: v3_guest_mem_access /dev/v3-vmN read|write|hash gpa_hex numbytes [<data]\n");
11 int main(int argc, char *argv[])
14 enum {READ, WRITE, HASH} mode;
17 struct v3_guest_mem_map *map;
30 if (toupper(argv[2][0])=='R') {
32 } else if (toupper(argv[2][0])=='W') {
34 } else if (toupper(argv[2][0])=='H') {
37 fprintf(stderr,"Unknown mode %s\n", argv[2]);
41 if (sscanf(argv[3],"%lx",&gpa)!=1) {
42 fprintf(stderr,"Don't understand address %s\n",argv[3]);
46 numbytes=atol(argv[4]);
48 if (!(map=v3_guest_mem_get_map(vmdev))) {
49 fprintf(stderr,"Cannot get guest memory map for %s\n",vmdev);
53 //for (i=0; i< map->numblocks; i++) {
54 // fprintf(stderr,"Region %llu: gpa=%p, hpa=%p, numpages=%llu\n",
55 // i, map->block[i].gpa, map->block[i].hpa, map->block[i].numpages);
58 if (v3_map_guest_mem(map)) {
59 fprintf(stderr, "Cannot map guest memory\n");
64 if (mode==READ || mode==WRITE) {
65 data = malloc(numbytes);
67 fprintf(stderr, "Cannot allocate memory\n");
68 v3_unmap_guest_mem(map);
75 for (i=0;i<numbytes;) {
76 rc = read(0,data+i,(numbytes-i));
78 fprintf(stderr, "Cannot read from stdin\n");
80 v3_unmap_guest_mem(map);
86 if (v3_guest_mem_write(map,(void*)gpa,numbytes,data)) {
87 fprintf(stderr, "Failed to write all of guest memory\n");
89 v3_unmap_guest_mem(map);
93 fprintf(stderr, "Write complete (%lu bytes)\n", numbytes);
100 if (v3_guest_mem_read(map,(void*)gpa,numbytes,data)) {
101 fprintf(stderr, "Failed to read all of guest memory\n");
103 v3_unmap_guest_mem(map);
106 for (i=0;i<numbytes;) {
107 rc = write(1,data+i,(numbytes-i));
109 fprintf(stderr, "Cannot write to stdout\n");
111 v3_unmap_guest_mem(map);
118 fprintf(stderr, "Read complete (%lu bytes)\n", numbytes);
125 if (v3_guest_mem_hash(map,(void*)gpa,numbytes,&hash)) {
126 fprintf(stderr, "Failed to hash all of guest memory\n");
127 v3_unmap_guest_mem(map);
131 fprintf(stderr, "Hash complete (%lu bytes), result is 0x%lx\n", numbytes, hash);
138 if (v3_unmap_guest_mem(map)) {
139 fprintf(stderr, "Cannot unmap guest memory\n");