]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyveload/bhyveload.c
IFC @ r224187
[FreeBSD/FreeBSD.git] / usr.sbin / bhyveload / bhyveload.c
1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 /*-
30  * Copyright (c) 2011 Google, Inc.
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  *
54  * $FreeBSD$
55  */
56
57 #include <sys/cdefs.h>
58 __FBSDID("$FreeBSD$");
59
60 #include <sys/ioctl.h>
61 #include <sys/stat.h>
62
63 #include <machine/specialreg.h>
64 #include <machine/vmm.h>
65
66 #include <dirent.h>
67 #include <dlfcn.h>
68 #include <errno.h>
69 #include <fcntl.h>
70 #include <getopt.h>
71 #include <limits.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <termios.h>
76 #include <unistd.h>
77
78 #include <vmmapi.h>
79
80 #include "userboot.h"
81
82 #define MB      (1024 * 1024UL)
83 #define GB      (1024 * 1024 * 1024UL)
84 #define BSP     0
85
86 static char *host_base = "/";
87 static struct termios term, oldterm;
88 static int disk_fd = -1;
89
90 static char *vmname, *progname, *membase;
91 static uint64_t lowmem, highmem;
92 static struct vmctx *ctx;
93
94 static uint64_t gdtbase, cr3, rsp;
95
96 static void cb_exit(void *arg, int v);
97
98 /*
99  * Console i/o callbacks
100  */
101
102 static void
103 cb_putc(void *arg, int ch)
104 {
105         char c = ch;
106
107         write(1, &c, 1);
108 }
109
110 static int
111 cb_getc(void *arg)
112 {
113         char c;
114
115         if (read(0, &c, 1) == 1)
116                 return (c);
117         return (-1);
118 }
119
120 static int
121 cb_poll(void *arg)
122 {
123         int n;
124
125         if (ioctl(0, FIONREAD, &n) >= 0)
126                 return (n > 0);
127         return (0);
128 }
129
130 /*
131  * Host filesystem i/o callbacks
132  */
133
134 struct cb_file {
135         int cf_isdir;
136         size_t cf_size;
137         struct stat cf_stat;
138         union {
139                 int fd;
140                 DIR *dir;
141         } cf_u;
142 };
143
144 static int
145 cb_open(void *arg, const char *filename, void **hp)
146 {
147         struct stat st;
148         struct cb_file *cf;
149         char path[PATH_MAX];
150
151         if (!host_base)
152                 return (ENOENT);
153
154         strlcpy(path, host_base, PATH_MAX);
155         if (path[strlen(path) - 1] == '/')
156                 path[strlen(path) - 1] = 0;
157         strlcat(path, filename, PATH_MAX);
158         cf = malloc(sizeof(struct cb_file));
159         if (stat(path, &cf->cf_stat) < 0) {
160                 free(cf);
161                 return (errno);
162         }
163
164         cf->cf_size = st.st_size;
165         if (S_ISDIR(cf->cf_stat.st_mode)) {
166                 cf->cf_isdir = 1;
167                 cf->cf_u.dir = opendir(path);
168                 if (!cf->cf_u.dir)
169                         goto out;
170                 *hp = cf;
171                 return (0);
172         }
173         if (S_ISREG(cf->cf_stat.st_mode)) {
174                 cf->cf_isdir = 0;
175                 cf->cf_u.fd = open(path, O_RDONLY);
176                 if (cf->cf_u.fd < 0)
177                         goto out;
178                 *hp = cf;
179                 return (0);
180         }
181
182 out:
183         free(cf);
184         return (EINVAL);
185 }
186
187 static int
188 cb_close(void *arg, void *h)
189 {
190         struct cb_file *cf = h;
191
192         if (cf->cf_isdir)
193                 closedir(cf->cf_u.dir);
194         else
195                 close(cf->cf_u.fd);
196         free(cf);
197
198         return (0);
199 }
200
201 static int
202 cb_isdir(void *arg, void *h)
203 {
204         struct cb_file *cf = h;
205
206         return (cf->cf_isdir);
207 }
208
209 static int
210 cb_read(void *arg, void *h, void *buf, size_t size, size_t *resid)
211 {
212         struct cb_file *cf = h;
213         ssize_t sz;
214
215         if (cf->cf_isdir)
216                 return (EINVAL);
217         sz = read(cf->cf_u.fd, buf, size);
218         if (sz < 0)
219                 return (EINVAL);
220         *resid = size - sz;
221         return (0);
222 }
223
224 static int
225 cb_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
226            size_t *namelen_return, char *name)
227 {
228         struct cb_file *cf = h;
229         struct dirent *dp;
230
231         if (!cf->cf_isdir)
232                 return (EINVAL);
233
234         dp = readdir(cf->cf_u.dir);
235         if (!dp)
236                 return (ENOENT);
237
238         /*
239          * Note: d_namlen is in the range 0..255 and therefore less
240          * than PATH_MAX so we don't need to test before copying.
241          */
242         *fileno_return = dp->d_fileno;
243         *type_return = dp->d_type;
244         *namelen_return = dp->d_namlen;
245         memcpy(name, dp->d_name, dp->d_namlen);
246         name[dp->d_namlen] = 0;
247
248         return (0);
249 }
250
251 static int
252 cb_seek(void *arg, void *h, uint64_t offset, int whence)
253 {
254         struct cb_file *cf = h;
255
256         if (cf->cf_isdir)
257                 return (EINVAL);
258         if (lseek(cf->cf_u.fd, offset, whence) < 0)
259                 return (errno);
260         return (0);
261 }
262
263 static int
264 cb_stat(void *arg, void *h, int *mode, int *uid, int *gid, uint64_t *size)
265 {
266         struct cb_file *cf = h;
267
268         *mode = cf->cf_stat.st_mode;
269         *uid = cf->cf_stat.st_uid;
270         *gid = cf->cf_stat.st_gid;
271         *size = cf->cf_stat.st_size;
272         return (0);
273 }
274
275 /*
276  * Disk image i/o callbacks
277  */
278
279 static int
280 cb_diskread(void *arg, int unit, uint64_t from, void *to, size_t size,
281             size_t *resid)
282 {
283         ssize_t n;
284
285         if (unit != 0 || disk_fd == -1)
286                 return (EIO);
287         n = pread(disk_fd, to, size, from);
288         if (n < 0)
289                 return (errno);
290         *resid = size - n;
291         return (0);
292 }
293
294 /*
295  * Guest virtual machine i/o callbacks
296  */
297 static int
298 cb_copyin(void *arg, const void *from, uint64_t to, size_t size)
299 {
300
301         to &= 0x7fffffff;
302         if (to > lowmem)
303                 return (EFAULT);
304         if (to + size > lowmem)
305                 size = lowmem - to;
306
307         memcpy(&membase[to], from, size);
308
309         return (0);
310 }
311
312 static int
313 cb_copyout(void *arg, uint64_t from, void *to, size_t size)
314 {
315
316         from &= 0x7fffffff;
317         if (from > lowmem)
318                 return (EFAULT);
319         if (from + size > lowmem)
320                 size = lowmem - from;
321
322         memcpy(to, &membase[from], size);
323
324         return (0);
325 }
326
327 static void
328 cb_setreg(void *arg, int r, uint64_t v)
329 {
330         int error;
331         enum vm_reg_name vmreg;
332         
333         vmreg = VM_REG_LAST;
334
335         switch (r) {
336         case 4:
337                 vmreg = VM_REG_GUEST_RSP;
338                 rsp = v;
339                 break;
340         default:
341                 break;
342         }
343
344         if (vmreg == VM_REG_LAST) {
345                 printf("test_setreg(%d): not implemented\n", r);
346                 cb_exit(NULL, USERBOOT_EXIT_QUIT);
347         }
348
349         error = vm_set_register(ctx, BSP, vmreg, v);
350         if (error) {
351                 perror("vm_set_register");
352                 cb_exit(NULL, USERBOOT_EXIT_QUIT);
353         }
354 }
355
356 static void
357 cb_setmsr(void *arg, int r, uint64_t v)
358 {
359         int error;
360         enum vm_reg_name vmreg;
361         
362         vmreg = VM_REG_LAST;
363
364         switch (r) {
365         case MSR_EFER:
366                 vmreg = VM_REG_GUEST_EFER;
367                 break;
368         default:
369                 break;
370         }
371
372         if (vmreg == VM_REG_LAST) {
373                 printf("test_setmsr(%d): not implemented\n", r);
374                 cb_exit(NULL, USERBOOT_EXIT_QUIT);
375         }
376
377         error = vm_set_register(ctx, BSP, vmreg, v);
378         if (error) {
379                 perror("vm_set_msr");
380                 cb_exit(NULL, USERBOOT_EXIT_QUIT);
381         }
382 }
383
384 static void
385 cb_setcr(void *arg, int r, uint64_t v)
386 {
387         int error;
388         enum vm_reg_name vmreg;
389         
390         vmreg = VM_REG_LAST;
391
392         switch (r) {
393         case 0:
394                 vmreg = VM_REG_GUEST_CR0;
395                 break;
396         case 3:
397                 vmreg = VM_REG_GUEST_CR3;
398                 cr3 = v;
399                 break;
400         case 4:
401                 vmreg = VM_REG_GUEST_CR4;
402                 break;
403         default:
404                 break;
405         }
406
407         if (vmreg == VM_REG_LAST) {
408                 printf("test_setcr(%d): not implemented\n", r);
409                 cb_exit(NULL, USERBOOT_EXIT_QUIT);
410         }
411
412         error = vm_set_register(ctx, BSP, vmreg, v);
413         if (error) {
414                 perror("vm_set_cr");
415                 cb_exit(NULL, USERBOOT_EXIT_QUIT);
416         }
417 }
418
419 static void
420 cb_setgdt(void *arg, uint64_t base, size_t size)
421 {
422         int error;
423
424         error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0);
425         if (error != 0) {
426                 perror("vm_set_desc(gdt)");
427                 cb_exit(NULL, USERBOOT_EXIT_QUIT);
428         }
429
430         gdtbase = base;
431 }
432
433 static void
434 cb_exec(void *arg, uint64_t rip)
435 {
436         int error;
437
438         error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase, rsp);
439         if (error) {
440                 perror("vm_setup_freebsd_registers");
441                 cb_exit(NULL, USERBOOT_EXIT_QUIT);
442         }
443
444         cb_exit(NULL, 0);
445 }
446
447 /*
448  * Misc
449  */
450
451 static void
452 cb_delay(void *arg, int usec)
453 {
454
455         usleep(usec);
456 }
457
458 static void
459 cb_exit(void *arg, int v)
460 {
461
462         tcsetattr(0, TCSAFLUSH, &oldterm);
463         exit(v);
464 }
465
466 static void
467 cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem)
468 {
469
470         *ret_lowmem = lowmem;
471         *ret_highmem = highmem;
472 }
473
474 static struct loader_callbacks_v1 cb = {
475         .getc = cb_getc,
476         .putc = cb_putc,
477         .poll = cb_poll,
478
479         .open = cb_open,
480         .close = cb_close,
481         .isdir = cb_isdir,
482         .read = cb_read,
483         .readdir = cb_readdir,
484         .seek = cb_seek,
485         .stat = cb_stat,
486
487         .diskread = cb_diskread,
488
489         .copyin = cb_copyin,
490         .copyout = cb_copyout,
491         .setreg = cb_setreg,
492         .setmsr = cb_setmsr,
493         .setcr = cb_setcr,
494         .setgdt = cb_setgdt,
495         .exec = cb_exec,
496
497         .delay = cb_delay,
498         .exit = cb_exit,
499         .getmem = cb_getmem,
500 };
501
502 static void
503 usage(void)
504 {
505
506         printf("usage: %s [-d <disk image path>] [-h <host filesystem path>] "
507                "[-m <lowmem>][-M <highmem>] "
508                "<vmname>\n", progname);
509         exit(1);
510 }
511
512 int
513 main(int argc, char** argv)
514 {
515         void *h;
516         void (*func)(struct loader_callbacks_v1 *, void *, int, int);
517         int opt, error;
518         char *disk_image;
519
520         progname = argv[0];
521
522         lowmem = 768 * MB;
523         highmem = 0;
524         disk_image = NULL;
525
526         while ((opt = getopt(argc, argv, "d:h:m:M:")) != -1) {
527                 switch (opt) {
528                 case 'd':
529                         disk_image = optarg;
530                         break;
531
532                 case 'h':
533                         host_base = optarg;
534                         break;
535
536                 case 'm':
537                         lowmem = strtoul(optarg, NULL, 0) * MB;
538                         break;
539                 
540                 case 'M':
541                         highmem = strtoul(optarg, NULL, 0) * MB;
542                         break;
543
544                 case '?':
545                         usage();
546                 }
547         }
548
549         argc -= optind;
550         argv += optind;
551
552         if (argc != 1)
553                 usage();
554
555         vmname = argv[0];
556
557         error = vm_create(vmname);
558         if (error != 0 && errno != EEXIST) {
559                 perror("vm_create");
560                 exit(1);
561
562         }
563
564         ctx = vm_open(vmname);
565         if (ctx == NULL) {
566                 perror("vm_open");
567                 exit(1);
568         }
569
570         error = vm_setup_memory(ctx, 0, lowmem, &membase);
571         if (error) {
572                 perror("vm_setup_memory(lowmem)");
573                 exit(1);
574         }
575
576         if (highmem != 0) {
577                 error = vm_setup_memory(ctx, 4 * GB, highmem, NULL);
578                 if (error) {
579                         perror("vm_setup_memory(highmem)");
580                         exit(1);
581                 }
582         }
583
584         tcgetattr(0, &term);
585         oldterm = term;
586         term.c_lflag &= ~(ICANON|ECHO);
587         term.c_iflag &= ~ICRNL;
588         tcsetattr(0, TCSAFLUSH, &term);
589         h = dlopen("./userboot.so", RTLD_LOCAL);
590         if (!h) {
591                 printf("%s\n", dlerror());
592                 return (1);
593         }
594         func = dlsym(h, "loader_main");
595         if (!func) {
596                 printf("%s\n", dlerror());
597                 return (1);
598         }
599
600         if (disk_image) {
601                 disk_fd = open(disk_image, O_RDONLY);
602         }
603         func(&cb, NULL, USERBOOT_VERSION_1, disk_fd >= 0);
604 }