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.


Checkpointing and migration now support options, the first of which are: skipping...
[palacios.git] / linux_usr / v3_guest_mem_example.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_example /dev/v3-vmN read|write gpa_hex numbytes [<data]\n");
9 }
10
11 int main(int argc, char *argv[])
12 {
13   char *vmdev;
14   enum {READ, WRITE} mode;
15   uint64_t gpa;
16   uint64_t numbytes;
17   struct v3_guest_mem_map *map;
18   uint64_t i;
19     
20   if (argc!=5) { 
21     usage();
22     return -1;
23   }
24
25   vmdev=argv[1];
26   
27   if (toupper(argv[2][0])=='R') { 
28     mode=READ;
29   } else if (toupper(argv[2][0]=='W')) {
30     mode=WRITE;
31   } else {
32     fprintf(stderr,"Unknown mode %s\n", argv[2]);
33     return -1;
34   }
35   
36   if (sscanf(argv[3],"%llx",&gpa)!=1) { 
37     fprintf(stderr,"Don't understand address %s\n",argv[3]);
38     return -1;
39   }
40
41   numbytes=atol(argv[4]);
42
43   if (!(map=v3_guest_mem_get_map(vmdev))) { 
44     fprintf(stderr,"Cannot get guest memory map for %s\n",vmdev);
45     return -1;
46   }
47   
48   for (i=0; i< map->numblocks; i++) { 
49     fprintf(stderr,"Region %llu: gpa=%p, hpa=%p, numpages=%llu\n", 
50             i, map->block[i].gpa, map->block[i].hpa, map->block[i].numpages);
51   }  
52
53   if (map->numblocks!=1) { 
54     fprintf(stderr,"Don't handle multiregion map yet\n");
55     return -1;
56   }
57   
58   if (!( ((void*)(gpa) >= map->block[0].gpa) &&
59          (numbytes <= map->block[0].numpages*4096))) { 
60     fprintf(stderr,"request (%p to %p) is out of range\n",
61             gpa, gpa+numbytes-1);
62     return -1;
63   }
64
65   if (v3_map_guest_mem(map)) { 
66     fprintf(stderr, "Cannot map guest memory\n");
67     free(map);
68     return -1;
69   }
70
71   for (i=0; i<numbytes; i++) {
72     uint8_t cur;
73     if (mode==WRITE) {
74       if (read(0,&cur,1)!=1) { 
75         fprintf(stderr,"can't get data from stdin for byte %llu\n", i);
76         break;
77       }
78       *((uint8_t*)(map->block[0].uva+gpa+i))=cur;
79     } else {
80       cur = *((uint8_t *)(map->block[0].uva+gpa+i));
81       //      fprintf(stderr, "read %llu from uva=%p, ptr=%p\n",i,map->block[0].uva, map->block[0].uva+gpa+i);
82       if (write(1,&cur,1)!=1) { 
83         fprintf(stderr,"can't write data to stdout for byte %llu\n", i);
84         break;
85       }
86     }
87   }
88       
89
90   if (v3_unmap_guest_mem(map)) { 
91     fprintf(stderr, "Cannot unmap guest memory\n");
92     free(map);
93     return -1;
94   }
95 }
96