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.


Avoid strict-aliasing related issues when compiling with optimization
[palacios.git] / linux_usr / v3_ctrl.c
1 /* 
2  * V3 Control Library
3  * (c) Jack lange, 2010
4  *     Kyle Hale,  2012
5  */
6
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <fcntl.h> 
11 #include <sys/ioctl.h> 
12 #include <sys/stat.h> 
13 #include <sys/types.h> 
14 #include <sys/mman.h>
15 #include <unistd.h> 
16 #include <string.h>
17  
18 #include "v3_ctrl.h"
19
20
21 /*
22  * create a file-backed memory region 
23  */
24 void * v3_mmap_file (const char * filename, int prot, int flags) {
25     int fd;
26     struct stat st;
27     void * m;
28
29     fd = open(filename, O_RDONLY);
30     if (!fd) {
31         fprintf(stderr, "Error opening file for mapping: %s\n", filename);
32         return NULL;
33     }
34
35     fstat(fd, &st);
36
37     if ((m = mmap(NULL, st.st_size, prot, flags, fd, 0)) == MAP_FAILED) {
38         fprintf(stderr, "Error mapping file (%s)\n", filename);
39         close(fd);
40         return NULL;
41     }
42
43     close(fd);
44     return m;
45 }
46
47
48 /*
49  * read a file into a buffer
50  */
51 int v3_read_file (int fd, int size, unsigned char * buf) {
52     int left_to_read = size;
53     int have_read = 0;
54
55     while (left_to_read != 0) {
56         int bytes_read = read(fd, buf + have_read, left_to_read);
57
58         if (bytes_read <= 0) {
59             break;
60         }
61
62         have_read += bytes_read;
63         left_to_read -= bytes_read;
64     }
65
66     if (left_to_read != 0) {
67         fprintf(stderr, "Error could not finish reading file\n");
68         return -1;
69     }
70     
71     return 0;
72 }
73
74
75 /*
76  * perform an ioctl on v3vee device
77  */
78 int v3_dev_ioctl (int request, void * arg) {
79     int fd, ret;
80
81     fd = open(v3_dev, O_RDONLY);
82     if (fd == -1) {
83         fprintf(stderr, "Error opening V3Vee control device\n");
84         return -1;
85     }
86
87     ret = ioctl(fd, request, arg);
88
89     if (ret < 0) 
90         fprintf(stderr, "IOCTL error on V3Vee control device (%d)\n", ret);
91
92
93     close(fd);
94     return ret;
95 }
96
97
98 /*
99  * perform an ioctl on arbitrary VM device
100  */
101 int v3_vm_ioctl (const char * filename, 
102                  int request, 
103                  void * arg) { 
104     int fd, ret;
105
106     fd = open(filename, O_RDONLY);
107     if (fd == -1) {
108         fprintf(stderr, "Error opening V3Vee VM device: %s\n", filename);
109         return -1;
110     }
111
112     ret = ioctl(fd, request, arg);
113
114     if (ret < 0) 
115         fprintf(stderr, "IOCTL error on device %s (%d)\n", filename, ret);
116
117     close(fd);
118     return ret;
119 }
120
121
122 /* 
123  * launch a VM with VM device path
124  */
125 int launch_vm (const char * filename) {
126     int err;
127
128     printf("Launching VM (%s)\n", filename);
129     err = v3_vm_ioctl(filename, V3_VM_LAUNCH, NULL);
130
131     if (err < 0) {
132         fprintf(stderr, "Error launching VM (%s)\n", filename);
133         return -1;
134     }
135
136     return 0;
137 }
138
139
140 /*
141  * stop a VM with VM device path
142  */
143 int stop_vm (const char * filename) {
144     int err;
145
146     printf("Stopping VM (%s)\n", filename);
147
148     if (v3_vm_ioctl(filename, V3_VM_STOP, NULL) < 0) 
149         return -1;
150
151     return 0;
152 }
153
154
155 /*
156  * generic ELF header buffer hash function. 
157  * Mirrors internal Palacios implementation
158  */
159 unsigned long v3_hash_buffer (unsigned char * msg, unsigned int len) {
160     unsigned long hash = 0;
161     unsigned long temp = 0;
162     unsigned int i;
163
164     for (i = 0; i < len; i++) {
165         hash = (hash << 4) + *(msg + i) + i;
166         if ((temp = (hash & 0xF0000000))) {
167             hash ^= (temp >> 24);
168         }
169         hash &= ~temp;
170     }
171     return hash;
172 }
173