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.


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