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