11 #include <sys/ioctl.h>
13 #include <sys/types.h>
22 * create a file-backed memory region
24 void * v3_mmap_file (const char * filename, int prot, int flags) {
29 fd = open(filename, O_RDONLY);
31 fprintf(stderr, "Error opening file for mapping: %s\n", filename);
37 if ((m = mmap(NULL, st.st_size, prot, flags, fd, 0)) == MAP_FAILED) {
38 fprintf(stderr, "Error mapping file (%s)\n", filename);
49 * read a file into a buffer
51 int v3_read_file (int fd, int size, unsigned char * buf) {
52 int left_to_read = size;
55 while (left_to_read != 0) {
56 int bytes_read = read(fd, buf + have_read, left_to_read);
58 if (bytes_read <= 0) {
62 have_read += bytes_read;
63 left_to_read -= bytes_read;
66 if (left_to_read != 0) {
67 fprintf(stderr, "Error could not finish reading file\n");
76 * perform an ioctl on v3vee device
78 int v3_dev_ioctl (int request, void * arg) {
81 fd = open(v3_dev, O_RDONLY);
83 fprintf(stderr, "Error opening V3Vee control device\n");
87 ret = ioctl(fd, request, arg);
90 fprintf(stderr, "IOCTL error on V3Vee control device (%d)\n", ret);
99 * perform an ioctl on arbitrary VM device
101 int v3_vm_ioctl (const char * filename,
106 fd = open(filename, O_RDONLY);
108 fprintf(stderr, "Error opening V3Vee VM device: %s\n", filename);
112 ret = ioctl(fd, request, arg);
115 fprintf(stderr, "IOCTL error on device %s (%d)\n", filename, ret);
123 * launch a VM with VM device path
125 int launch_vm (const char * filename) {
128 printf("Launching VM (%s)\n", filename);
129 err = v3_vm_ioctl(filename, V3_VM_LAUNCH, NULL);
132 fprintf(stderr, "Error launching VM (%s)\n", filename);
141 * stop a VM with VM device path
143 int stop_vm (const char * filename) {
146 printf("Stopping VM (%s)\n", filename);
148 if (v3_vm_ioctl(filename, V3_VM_STOP, NULL) < 0)
156 * generic ELF header buffer hash function.
157 * Mirrors internal Palacios implementation
159 unsigned long v3_hash_buffer (unsigned char * msg, unsigned int len) {
160 unsigned long hash = 0;
161 unsigned long temp = 0;
164 for (i = 0; i < len; i++) {
165 hash = (hash << 4) + *(msg + i) + i;
166 if ((temp = (hash & 0xF0000000))) {
167 hash ^= (temp >> 24);