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.


2162ea8092d6895ad1df0a75b3fc0b3dfce0b812
[palacios.git] / linux_usr / v3_guest_mem_access.c
1 #include <stdint.h>
2 #include <stdio.h>
3
4 #include "v3_guest_mem.h"
5
6 void usage() 
7 {
8   fprintf(stderr,"usage: v3_guest_mem_access /dev/v3-vmN read|write|hash gpa_hex numbytes [<data]\n");
9 }
10
11 int main(int argc, char *argv[])
12 {
13   char *vmdev;
14   enum {READ, WRITE, HASH} mode;
15   uint64_t gpa;
16   uint64_t numbytes;
17   struct v3_guest_mem_map *map;
18   uint64_t i;
19   int rc;
20   uint64_t hash;
21   uint8_t *data=0;
22     
23   if (argc!=5) { 
24     usage();
25     return -1;
26   }
27
28   vmdev=argv[1];
29   
30   if (toupper(argv[2][0])=='R') { 
31       mode=READ;
32   } else if (toupper(argv[2][0])=='W') {
33       mode=WRITE;
34   } else if (toupper(argv[2][0])=='H') {
35       mode=HASH;
36   } else {
37       fprintf(stderr,"Unknown mode %s\n", argv[2]);
38       return -1;
39   }
40   
41   if (sscanf(argv[3],"%llx",&gpa)!=1) { 
42     fprintf(stderr,"Don't understand address %s\n",argv[3]);
43     return -1;
44   }
45
46   numbytes=atol(argv[4]);
47
48   if (!(map=v3_guest_mem_get_map(vmdev))) { 
49     fprintf(stderr,"Cannot get guest memory map for %s\n",vmdev);
50     return -1;
51   }
52   
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);
56   //}  
57
58   if (v3_map_guest_mem(map)) { 
59     fprintf(stderr, "Cannot map guest memory\n");
60     free(map);
61     return -1;
62   }
63
64   if (mode==READ || mode==WRITE) { 
65       data = malloc(numbytes);
66       if (!data) { 
67           fprintf(stderr, "Cannot allocate memory\n");
68           v3_unmap_guest_mem(map);
69           return -1;
70       }
71   }
72
73   switch (mode) { 
74       case WRITE:
75           for (i=0;i<numbytes;) {
76               rc = read(0,data+i,(numbytes-i));
77               if (rc<=0) {
78                   fprintf(stderr, "Cannot read from stdin\n");
79                   free(data);
80                   v3_unmap_guest_mem(map);
81                   return -1;
82               } else {
83                   i+=rc;
84               }
85           }
86           if (v3_guest_mem_write(map,(void*)gpa,numbytes,data)) { 
87               fprintf(stderr, "Failed to write all of guest memory\n");
88               free(data);
89               v3_unmap_guest_mem(map);
90               return -1;
91           }
92
93           fprintf(stderr, "Write complete (%llu bytes)\n", numbytes);
94
95           free(data);
96
97           break;
98           
99       case READ:
100           if (v3_guest_mem_read(map,(void*)gpa,numbytes,data)) { 
101               fprintf(stderr, "Failed to read all of guest memory\n");
102               free(data);
103               v3_unmap_guest_mem(map);
104               return -1;
105           }
106           for (i=0;i<numbytes;) {
107               rc = write(1,data+i,(numbytes-i));
108               if (rc<=0) {
109                   fprintf(stderr, "Cannot write to stdout\n");
110                   free(data);
111                   v3_unmap_guest_mem(map);
112                   return -1;
113               } else {
114                   i+=rc;
115               }
116           }
117           
118           fprintf(stderr, "Read complete (%llu bytes)\n", numbytes);
119           
120           free(data);
121
122           break;
123           
124       case HASH:
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);
128               return -1;
129           }
130           
131           fprintf(stderr, "Hash complete (%llu bytes), result is 0x%llx\n", numbytes, hash);
132           
133           break;
134           
135   }
136   
137   
138   if (v3_unmap_guest_mem(map)) { 
139       fprintf(stderr, "Cannot unmap guest memory\n");
140       free(map);
141       return -1;
142   }
143   
144   return 0;
145 }
146