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.


User space library and example tool for accessing guest physical memory from host...
[palacios.git] / linux_usr / v3_guest_mem.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <fcntl.h>
5 #include <sys/mman.h>
6
7 #include "v3_guest_mem.h"
8
9
10 #define GUEST_FILE "/proc/v3vee/v3-guests"
11 //#define GUEST_FILE "/441/pdinda/test.proc"
12 #define MAXLINE 65536
13
14 struct v3_guest_mem_map * v3_guest_mem_get_map(char *vmdev)
15 {
16   FILE *f;
17   char buf[MAXLINE];
18   char name[MAXLINE];
19   char dev[MAXLINE];
20   char state[MAXLINE];
21   uint64_t start, end;
22   
23
24   if (!(f=fopen(GUEST_FILE,"r"))) { 
25     fprintf(stderr,"Cannot open %s - is Palacios active?\n",GUEST_FILE);
26     return 0;
27   }
28   
29   // This is using the current "single memory region" model
30   // and will change when /proc convention changes to conform to
31   // multiple region model
32   while (fgets(buf,MAXLINE,f)) {
33     if (sscanf(buf,
34                "%s %s %s [0x%llx-0x%llx]",
35                name,
36                dev,
37                state,
38                &start,
39                &end)!=5) { 
40       fprintf(stderr, "Cannot parse following line\n%s\n",buf);
41       fclose(f);
42       return 0;
43     }
44     if (!strcmp(dev,vmdev)) { 
45       struct v3_guest_mem_map *m = 
46         (struct v3_guest_mem_map *) malloc(sizeof(struct v3_guest_mem_map)+1*sizeof(struct v3_guest_mem_block));
47       if (!m) { 
48         fprintf(stderr, "Cannot allocate space\n");
49         fclose(f);
50         return 0;
51       }
52
53       memset(m,0,sizeof(struct v3_guest_mem_map)+1*sizeof(struct v3_guest_mem_block));
54       
55       m->numblocks=1;
56       m->block[0].gpa=0;
57       m->block[0].hpa=(void*)start;
58       m->block[0].numpages = (end-start+1) / 4096;
59       fclose(f);
60       return m;
61     }
62   }
63   
64   fprintf(stderr,"%s not found\n",vmdev);
65   fclose(f);
66   return 0;
67
68 }
69
70 int v3_map_guest_mem(struct v3_guest_mem_map *map)
71 {
72   uint64_t i;
73   
74   if (map->fd) {
75     fprintf(stderr, "Memory appears to already be mapped\n");
76     return -1;
77   }
78
79   map->fd = open("/dev/mem", O_RDWR | O_SYNC);
80
81   if (map->fd<0) { 
82     fprintf(stderr, "Cannot open /dev/mem - are you root?\n");
83     map->fd=0;
84     return -1;
85   }
86
87   for (i=0; i<map->numblocks; i++) { 
88     fprintf(stderr,"Mapping %llu bytes of /dev/mem offset 0x%llx\n",
89             map->block[i].numpages*4096, (off_t)(map->block[i].hpa));
90     map->block[i].uva = mmap(NULL, 
91                              map->block[i].numpages*4096,
92                              PROT_READ | PROT_WRITE, 
93                              MAP_SHARED, 
94                              map->fd, 
95                              (off_t) (map->block[i].hpa));
96
97     if (map->block[i].uva == MAP_FAILED) { 
98       fprintf(stderr, "Failed to map block %llu\n",i);
99       map->block[i].uva=0;
100       v3_unmap_guest_mem(map);
101       return -1;
102     }
103   }
104    
105   return 0;
106     
107 }
108
109 int v3_unmap_guest_mem(struct v3_guest_mem_map *map)
110 {
111   uint64_t i;
112
113   for (i=0; i<map->numblocks; i++) { 
114     if (map->block[i].uva) { 
115       munmap(map->block[i].uva, map->block[i].numpages*4096);
116       map->block[i].uva=0;
117     }
118   }
119   if (map->fd) { 
120     close(map->fd);
121     map->fd=0;
122   }
123   return 0;
124 }