]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyveload/bhyveload.c
bhyveload: use a dirfd to support -h
[FreeBSD/FreeBSD.git] / usr.sbin / bhyveload / bhyveload.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
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
55 #include <sys/cdefs.h>
56 #include <sys/ioctl.h>
57 #include <sys/stat.h>
58 #include <sys/disk.h>
59 #include <sys/queue.h>
60
61 #include <machine/specialreg.h>
62 #include <machine/vmm.h>
63
64 #include <assert.h>
65 #include <dirent.h>
66 #include <dlfcn.h>
67 #include <errno.h>
68 #include <err.h>
69 #include <fcntl.h>
70 #include <getopt.h>
71 #include <libgen.h>
72 #include <limits.h>
73 #include <setjmp.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <sysexits.h>
78 #include <termios.h>
79 #include <unistd.h>
80
81 #include <vmmapi.h>
82
83 #include "userboot.h"
84
85 #define MB      (1024 * 1024UL)
86 #define GB      (1024 * 1024 * 1024UL)
87 #define BSP     0
88
89 #define NDISKS  32
90
91 static struct termios term, oldterm;
92 static int disk_fd[NDISKS];
93 static int ndisks;
94 static int consin_fd, consout_fd;
95 static int hostbase_fd = -1;
96
97 static int need_reinit;
98
99 static void *loader_hdl;
100 static char *loader;
101 static int explicit_loader;
102 static jmp_buf jb;
103
104 static char *vmname, *progname;
105 static struct vmctx *ctx;
106 static struct vcpu *vcpu;
107
108 static uint64_t gdtbase, cr3, rsp;
109
110 static void cb_exit(void *arg, int v);
111
112 /*
113  * Console i/o callbacks
114  */
115
116 static void
117 cb_putc(void *arg __unused, int ch)
118 {
119         char c = ch;
120
121         (void) write(consout_fd, &c, 1);
122 }
123
124 static int
125 cb_getc(void *arg __unused)
126 {
127         char c;
128
129         if (read(consin_fd, &c, 1) == 1)
130                 return (c);
131         return (-1);
132 }
133
134 static int
135 cb_poll(void *arg __unused)
136 {
137         int n;
138
139         if (ioctl(consin_fd, FIONREAD, &n) >= 0)
140                 return (n > 0);
141         return (0);
142 }
143
144 /*
145  * Host filesystem i/o callbacks
146  */
147
148 struct cb_file {
149         int cf_isdir;
150         size_t cf_size;
151         struct stat cf_stat;
152         union {
153                 int fd;
154                 DIR *dir;
155         } cf_u;
156 };
157
158 static int
159 cb_open(void *arg __unused, const char *filename, void **hp)
160 {
161         struct cb_file *cf;
162         struct stat sb;
163         int fd, flags;
164
165         cf = NULL;
166         fd = -1;
167         flags = O_RDONLY | O_RESOLVE_BENEATH;
168         if (hostbase_fd == -1)
169                 return (ENOENT);
170
171         /* Absolute paths are relative to our hostbase, chop off leading /. */
172         if (filename[0] == '/')
173                 filename++;
174
175         /* Lookup of /, use . instead. */
176         if (filename[0] == '\0')
177                 filename = ".";
178
179         if (fstatat(hostbase_fd, filename, &sb, AT_RESOLVE_BENEATH) < 0)
180                 return (errno);
181
182         if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode))
183                 return (EINVAL);
184
185         if (S_ISDIR(sb.st_mode))
186                 flags |= O_DIRECTORY;
187
188         /* May be opening the root dir */
189         fd = openat(hostbase_fd, filename, flags);
190         if (fd < 0)
191                 return (errno);
192
193         cf = malloc(sizeof(struct cb_file));
194         if (cf == NULL) {
195                 close(fd);
196                 return (ENOMEM);
197         }
198
199         cf->cf_stat = sb;
200         cf->cf_size = cf->cf_stat.st_size;
201
202         if (S_ISDIR(cf->cf_stat.st_mode)) {
203                 cf->cf_isdir = 1;
204                 cf->cf_u.dir = fdopendir(fd);
205                 if (cf->cf_u.dir == NULL) {
206                         close(fd);
207                         free(cf);
208                         return (ENOMEM);
209                 }
210         } else {
211                 assert(S_ISREG(cf->cf_stat.st_mode));
212                 cf->cf_isdir = 0;
213                 cf->cf_u.fd = fd;
214         }
215         *hp = cf;
216         return (0);
217 }
218
219 static int
220 cb_close(void *arg __unused, void *h)
221 {
222         struct cb_file *cf = h;
223
224         if (cf->cf_isdir)
225                 closedir(cf->cf_u.dir);
226         else
227                 close(cf->cf_u.fd);
228         free(cf);
229
230         return (0);
231 }
232
233 static int
234 cb_isdir(void *arg __unused, void *h)
235 {
236         struct cb_file *cf = h;
237
238         return (cf->cf_isdir);
239 }
240
241 static int
242 cb_read(void *arg __unused, void *h, void *buf, size_t size, size_t *resid)
243 {
244         struct cb_file *cf = h;
245         ssize_t sz;
246
247         if (cf->cf_isdir)
248                 return (EINVAL);
249         sz = read(cf->cf_u.fd, buf, size);
250         if (sz < 0)
251                 return (EINVAL);
252         *resid = size - sz;
253         return (0);
254 }
255
256 static int
257 cb_readdir(void *arg __unused, void *h, uint32_t *fileno_return,
258     uint8_t *type_return, size_t *namelen_return, char *name)
259 {
260         struct cb_file *cf = h;
261         struct dirent *dp;
262
263         if (!cf->cf_isdir)
264                 return (EINVAL);
265
266         dp = readdir(cf->cf_u.dir);
267         if (!dp)
268                 return (ENOENT);
269
270         /*
271          * Note: d_namlen is in the range 0..255 and therefore less
272          * than PATH_MAX so we don't need to test before copying.
273          */
274         *fileno_return = dp->d_fileno;
275         *type_return = dp->d_type;
276         *namelen_return = dp->d_namlen;
277         memcpy(name, dp->d_name, dp->d_namlen);
278         name[dp->d_namlen] = 0;
279
280         return (0);
281 }
282
283 static int
284 cb_seek(void *arg __unused, void *h, uint64_t offset, int whence)
285 {
286         struct cb_file *cf = h;
287
288         if (cf->cf_isdir)
289                 return (EINVAL);
290         if (lseek(cf->cf_u.fd, offset, whence) < 0)
291                 return (errno);
292         return (0);
293 }
294
295 static int
296 cb_stat(void *arg __unused, void *h, struct stat *sbp)
297 {
298         struct cb_file *cf = h;
299
300         memset(sbp, 0, sizeof(struct stat));
301         sbp->st_mode = cf->cf_stat.st_mode;
302         sbp->st_uid = cf->cf_stat.st_uid;
303         sbp->st_gid = cf->cf_stat.st_gid;
304         sbp->st_size = cf->cf_stat.st_size;
305         sbp->st_mtime = cf->cf_stat.st_mtime;
306         sbp->st_dev = cf->cf_stat.st_dev;
307         sbp->st_ino = cf->cf_stat.st_ino;
308         
309         return (0);
310 }
311
312 /*
313  * Disk image i/o callbacks
314  */
315
316 static int
317 cb_diskread(void *arg __unused, int unit, uint64_t from, void *to, size_t size,
318     size_t *resid)
319 {
320         ssize_t n;
321
322         if (unit < 0 || unit >= ndisks)
323                 return (EIO);
324         n = pread(disk_fd[unit], to, size, from);
325         if (n < 0)
326                 return (errno);
327         *resid = size - n;
328         return (0);
329 }
330
331 static int
332 cb_diskwrite(void *arg __unused, int unit, uint64_t offset, void *src,
333     size_t size, size_t *resid)
334 {
335         ssize_t n;
336
337         if (unit < 0 || unit >= ndisks)
338                 return (EIO);
339         n = pwrite(disk_fd[unit], src, size, offset);
340         if (n < 0)
341                 return (errno);
342         *resid = size - n;
343         return (0);
344 }
345
346 static int
347 cb_diskioctl(void *arg __unused, int unit, u_long cmd, void *data)
348 {
349         struct stat sb;
350
351         if (unit < 0 || unit >= ndisks)
352                 return (EBADF);
353
354         switch (cmd) {
355         case DIOCGSECTORSIZE:
356                 *(u_int *)data = 512;
357                 break;
358         case DIOCGMEDIASIZE:
359                 if (fstat(disk_fd[unit], &sb) != 0)
360                         return (ENOTTY);
361                 if (S_ISCHR(sb.st_mode) &&
362                     ioctl(disk_fd[unit], DIOCGMEDIASIZE, &sb.st_size) != 0)
363                                 return (ENOTTY);
364                 *(off_t *)data = sb.st_size;
365                 break;
366         default:
367                 return (ENOTTY);
368         }
369
370         return (0);
371 }
372
373 /*
374  * Guest virtual machine i/o callbacks
375  */
376 static int
377 cb_copyin(void *arg __unused, const void *from, uint64_t to, size_t size)
378 {
379         char *ptr;
380
381         to &= 0x7fffffff;
382
383         ptr = vm_map_gpa(ctx, to, size);
384         if (ptr == NULL)
385                 return (EFAULT);
386
387         memcpy(ptr, from, size);
388         return (0);
389 }
390
391 static int
392 cb_copyout(void *arg __unused, uint64_t from, void *to, size_t size)
393 {
394         char *ptr;
395
396         from &= 0x7fffffff;
397
398         ptr = vm_map_gpa(ctx, from, size);
399         if (ptr == NULL)
400                 return (EFAULT);
401
402         memcpy(to, ptr, size);
403         return (0);
404 }
405
406 static void
407 cb_setreg(void *arg __unused, int r, uint64_t v)
408 {
409         int error;
410         enum vm_reg_name vmreg;
411
412         vmreg = VM_REG_LAST;
413
414         switch (r) {
415         case 4:
416                 vmreg = VM_REG_GUEST_RSP;
417                 rsp = v;
418                 break;
419         default:
420                 break;
421         }
422
423         if (vmreg == VM_REG_LAST) {
424                 printf("test_setreg(%d): not implemented\n", r);
425                 cb_exit(NULL, USERBOOT_EXIT_QUIT);
426         }
427
428         error = vm_set_register(vcpu, vmreg, v);
429         if (error) {
430                 perror("vm_set_register");
431                 cb_exit(NULL, USERBOOT_EXIT_QUIT);
432         }
433 }
434
435 static void
436 cb_setmsr(void *arg __unused, int r, uint64_t v)
437 {
438         int error;
439         enum vm_reg_name vmreg;
440         
441         vmreg = VM_REG_LAST;
442
443         switch (r) {
444         case MSR_EFER:
445                 vmreg = VM_REG_GUEST_EFER;
446                 break;
447         default:
448                 break;
449         }
450
451         if (vmreg == VM_REG_LAST) {
452                 printf("test_setmsr(%d): not implemented\n", r);
453                 cb_exit(NULL, USERBOOT_EXIT_QUIT);
454         }
455
456         error = vm_set_register(vcpu, vmreg, v);
457         if (error) {
458                 perror("vm_set_msr");
459                 cb_exit(NULL, USERBOOT_EXIT_QUIT);
460         }
461 }
462
463 static void
464 cb_setcr(void *arg __unused, int r, uint64_t v)
465 {
466         int error;
467         enum vm_reg_name vmreg;
468         
469         vmreg = VM_REG_LAST;
470
471         switch (r) {
472         case 0:
473                 vmreg = VM_REG_GUEST_CR0;
474                 break;
475         case 3:
476                 vmreg = VM_REG_GUEST_CR3;
477                 cr3 = v;
478                 break;
479         case 4:
480                 vmreg = VM_REG_GUEST_CR4;
481                 break;
482         default:
483                 break;
484         }
485
486         if (vmreg == VM_REG_LAST) {
487                 printf("test_setcr(%d): not implemented\n", r);
488                 cb_exit(NULL, USERBOOT_EXIT_QUIT);
489         }
490
491         error = vm_set_register(vcpu, vmreg, v);
492         if (error) {
493                 perror("vm_set_cr");
494                 cb_exit(NULL, USERBOOT_EXIT_QUIT);
495         }
496 }
497
498 static void
499 cb_setgdt(void *arg __unused, uint64_t base, size_t size)
500 {
501         int error;
502
503         error = vm_set_desc(vcpu, VM_REG_GUEST_GDTR, base, size - 1, 0);
504         if (error != 0) {
505                 perror("vm_set_desc(gdt)");
506                 cb_exit(NULL, USERBOOT_EXIT_QUIT);
507         }
508
509         gdtbase = base;
510 }
511
512 static void
513 cb_exec(void *arg __unused, uint64_t rip)
514 {
515         int error;
516
517         if (cr3 == 0)
518                 error = vm_setup_freebsd_registers_i386(vcpu, rip, gdtbase,
519                     rsp);
520         else
521                 error = vm_setup_freebsd_registers(vcpu, rip, cr3, gdtbase,
522                     rsp);
523         if (error) {
524                 perror("vm_setup_freebsd_registers");
525                 cb_exit(NULL, USERBOOT_EXIT_QUIT);
526         }
527
528         cb_exit(NULL, 0);
529 }
530
531 /*
532  * Misc
533  */
534
535 static void
536 cb_delay(void *arg __unused, int usec)
537 {
538
539         usleep(usec);
540 }
541
542 static void
543 cb_exit(void *arg __unused, int v)
544 {
545
546         tcsetattr(consout_fd, TCSAFLUSH, &oldterm);
547         exit(v);
548 }
549
550 static void
551 cb_getmem(void *arg __unused, uint64_t *ret_lowmem, uint64_t *ret_highmem)
552 {
553
554         *ret_lowmem = vm_get_lowmem_size(ctx);
555         *ret_highmem = vm_get_highmem_size(ctx);
556 }
557
558 struct env {
559         char *str;      /* name=value */
560         SLIST_ENTRY(env) next;
561 };
562
563 static SLIST_HEAD(envhead, env) envhead;
564
565 static void
566 addenv(const char *str)
567 {
568         struct env *env;
569
570         env = malloc(sizeof(struct env));
571         if (env == NULL)
572                 err(EX_OSERR, "malloc");
573         env->str = strdup(str);
574         if (env->str == NULL)
575                 err(EX_OSERR, "strdup");
576         SLIST_INSERT_HEAD(&envhead, env, next);
577 }
578
579 static char *
580 cb_getenv(void *arg __unused, int num)
581 {
582         int i;
583         struct env *env;
584
585         i = 0;
586         SLIST_FOREACH(env, &envhead, next) {
587                 if (i == num)
588                         return (env->str);
589                 i++;
590         }
591
592         return (NULL);
593 }
594
595 static int
596 cb_vm_set_register(void *arg __unused, int vcpuid, int reg, uint64_t val)
597 {
598
599         assert(vcpuid == BSP);
600         return (vm_set_register(vcpu, reg, val));
601 }
602
603 static int
604 cb_vm_set_desc(void *arg __unused, int vcpuid, int reg, uint64_t base,
605     u_int limit, u_int access)
606 {
607
608         assert(vcpuid == BSP);
609         return (vm_set_desc(vcpu, reg, base, limit, access));
610 }
611
612 static void
613 cb_swap_interpreter(void *arg __unused, const char *interp_req)
614 {
615
616         /*
617          * If the user specified a loader but we detected a mismatch, we should
618          * not try to pivot to a different loader on them.
619          */
620         free(loader);
621         if (explicit_loader == 1) {
622                 perror("requested loader interpreter does not match guest userboot");
623                 cb_exit(NULL, 1);
624         }
625         if (interp_req == NULL || *interp_req == '\0') {
626                 perror("guest failed to request an interpreter");
627                 cb_exit(NULL, 1);
628         }
629
630         if (asprintf(&loader, "/boot/userboot_%s.so", interp_req) == -1)
631                 err(EX_OSERR, "malloc");
632         need_reinit = 1;
633         longjmp(jb, 1);
634 }
635
636 static struct loader_callbacks cb = {
637         .getc = cb_getc,
638         .putc = cb_putc,
639         .poll = cb_poll,
640
641         .open = cb_open,
642         .close = cb_close,
643         .isdir = cb_isdir,
644         .read = cb_read,
645         .readdir = cb_readdir,
646         .seek = cb_seek,
647         .stat = cb_stat,
648
649         .diskread = cb_diskread,
650         .diskwrite = cb_diskwrite,
651         .diskioctl = cb_diskioctl,
652
653         .copyin = cb_copyin,
654         .copyout = cb_copyout,
655         .setreg = cb_setreg,
656         .setmsr = cb_setmsr,
657         .setcr = cb_setcr,
658         .setgdt = cb_setgdt,
659         .exec = cb_exec,
660
661         .delay = cb_delay,
662         .exit = cb_exit,
663         .getmem = cb_getmem,
664
665         .getenv = cb_getenv,
666
667         /* Version 4 additions */
668         .vm_set_register = cb_vm_set_register,
669         .vm_set_desc = cb_vm_set_desc,
670
671         /* Version 5 additions */
672         .swap_interpreter = cb_swap_interpreter,
673 };
674
675 static int
676 altcons_open(char *path)
677 {
678         struct stat sb;
679         int err;
680         int fd;
681
682         /*
683          * Allow stdio to be passed in so that the same string
684          * can be used for the bhyveload console and bhyve com-port
685          * parameters
686          */
687         if (!strcmp(path, "stdio"))
688                 return (0);
689
690         err = stat(path, &sb);
691         if (err == 0) {
692                 if (!S_ISCHR(sb.st_mode))
693                         err = ENOTSUP;
694                 else {
695                         fd = open(path, O_RDWR | O_NONBLOCK);
696                         if (fd < 0)
697                                 err = errno;
698                         else
699                                 consin_fd = consout_fd = fd;
700                 }
701         }
702
703         return (err);
704 }
705
706 static int
707 disk_open(char *path)
708 {
709         int fd;
710
711         if (ndisks >= NDISKS)
712                 return (ERANGE);
713
714         fd = open(path, O_RDWR);
715         if (fd < 0)
716                 return (errno);
717
718         disk_fd[ndisks] = fd;
719         ndisks++;
720
721         return (0);
722 }
723
724 static void
725 usage(void)
726 {
727
728         fprintf(stderr,
729             "usage: %s [-S][-c <console-device>] [-d <disk-path>] [-e <name=value>]\n"
730             "       %*s [-h <host-path>] [-m memsize[K|k|M|m|G|g|T|t]] <vmname>\n",
731             progname,
732             (int)strlen(progname), "");
733         exit(1);
734 }
735
736 static void
737 hostbase_open(const char *base)
738 {
739
740         if (hostbase_fd != -1)
741                 close(hostbase_fd);
742         hostbase_fd = open(base, O_DIRECTORY | O_PATH);
743         if (hostbase_fd == -1)
744                 err(EX_OSERR, "open");
745 }
746
747 int
748 main(int argc, char** argv)
749 {
750         void (*func)(struct loader_callbacks *, void *, int, int);
751         uint64_t mem_size;
752         int opt, error, memflags;
753
754         progname = basename(argv[0]);
755
756         memflags = 0;
757         mem_size = 256 * MB;
758
759         consin_fd = STDIN_FILENO;
760         consout_fd = STDOUT_FILENO;
761
762         while ((opt = getopt(argc, argv, "CSc:d:e:h:l:m:")) != -1) {
763                 switch (opt) {
764                 case 'c':
765                         error = altcons_open(optarg);
766                         if (error != 0)
767                                 errx(EX_USAGE, "Could not open '%s'", optarg);
768                         break;
769
770                 case 'd':
771                         error = disk_open(optarg);
772                         if (error != 0)
773                                 errx(EX_USAGE, "Could not open '%s'", optarg);
774                         break;
775
776                 case 'e':
777                         addenv(optarg);
778                         break;
779
780                 case 'h':
781                         hostbase_open(optarg);
782                         break;
783
784                 case 'l':
785                         if (loader != NULL)
786                                 errx(EX_USAGE, "-l can only be given once");
787                         loader = strdup(optarg);
788                         if (loader == NULL)
789                                 err(EX_OSERR, "malloc");
790                         explicit_loader = 1;
791                         break;
792
793                 case 'm':
794                         error = vm_parse_memsize(optarg, &mem_size);
795                         if (error != 0)
796                                 errx(EX_USAGE, "Invalid memsize '%s'", optarg);
797                         break;
798                 case 'C':
799                         memflags |= VM_MEM_F_INCORE;
800                         break;
801                 case 'S':
802                         memflags |= VM_MEM_F_WIRED;
803                         break;
804                 case '?':
805                         usage();
806                 }
807         }
808
809         argc -= optind;
810         argv += optind;
811
812         if (argc != 1)
813                 usage();
814
815         vmname = argv[0];
816
817         need_reinit = 0;
818         error = vm_create(vmname);
819         if (error) {
820                 if (errno != EEXIST) {
821                         perror("vm_create");
822                         exit(1);
823                 }
824                 need_reinit = 1;
825         }
826
827         ctx = vm_open(vmname);
828         if (ctx == NULL) {
829                 perror("vm_open");
830                 exit(1);
831         }
832
833         vcpu = vm_vcpu_open(ctx, BSP);
834
835         /*
836          * setjmp in the case the guest wants to swap out interpreter,
837          * cb_swap_interpreter will swap out loader as appropriate and set
838          * need_reinit so that we end up in a clean state once again.
839          */
840         setjmp(jb);
841
842         if (need_reinit) {
843                 error = vm_reinit(ctx);
844                 if (error) {
845                         perror("vm_reinit");
846                         exit(1);
847                 }
848         }
849
850         vm_set_memflags(ctx, memflags);
851         error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL);
852         if (error) {
853                 perror("vm_setup_memory");
854                 exit(1);
855         }
856
857         if (loader == NULL) {
858                 loader = strdup("/boot/userboot.so");
859                 if (loader == NULL)
860                         err(EX_OSERR, "malloc");
861         }
862         if (loader_hdl != NULL)
863                 dlclose(loader_hdl);
864         loader_hdl = dlopen(loader, RTLD_LOCAL);
865         if (!loader_hdl) {
866                 printf("%s\n", dlerror());
867                 free(loader);
868                 return (1);
869         }
870         func = dlsym(loader_hdl, "loader_main");
871         if (!func) {
872                 printf("%s\n", dlerror());
873                 free(loader);
874                 return (1);
875         }
876
877         tcgetattr(consout_fd, &term);
878         oldterm = term;
879         cfmakeraw(&term);
880         term.c_cflag |= CLOCAL;
881
882         tcsetattr(consout_fd, TCSAFLUSH, &term);
883
884         addenv("smbios.bios.vendor=BHYVE");
885         addenv("boot_serial=1");
886
887         func(&cb, NULL, USERBOOT_VERSION_5, ndisks);
888
889         free(loader);
890         return (0);
891 }