xref: /freebsd-src/usr.sbin/bhyveload/bhyveload.c (revision b6afa84b8cc54b8ed3637dbb45a2659961d18abf)
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>
63*b6afa84bSNeel Natu #include <sys/queue.h>
64c487da1eSNeel Natu 
65c487da1eSNeel Natu #include <machine/specialreg.h>
66c487da1eSNeel Natu #include <machine/vmm.h>
67c487da1eSNeel Natu 
68c487da1eSNeel Natu #include <dirent.h>
69c487da1eSNeel Natu #include <dlfcn.h>
70c487da1eSNeel Natu #include <errno.h>
71200758f1SNeel Natu #include <err.h>
72c487da1eSNeel Natu #include <fcntl.h>
73c487da1eSNeel Natu #include <getopt.h>
74c487da1eSNeel Natu #include <limits.h>
75c487da1eSNeel Natu #include <stdio.h>
76c487da1eSNeel Natu #include <stdlib.h>
77c487da1eSNeel Natu #include <string.h>
78200758f1SNeel Natu #include <sysexits.h>
79c487da1eSNeel Natu #include <termios.h>
80c487da1eSNeel Natu #include <unistd.h>
81c487da1eSNeel Natu 
82c487da1eSNeel Natu #include <vmmapi.h>
83c487da1eSNeel Natu 
84c487da1eSNeel Natu #include "userboot.h"
85c487da1eSNeel Natu 
86c487da1eSNeel Natu #define	MB	(1024 * 1024UL)
87c487da1eSNeel Natu #define	GB	(1024 * 1024 * 1024UL)
88c487da1eSNeel Natu #define	BSP	0
89c487da1eSNeel Natu 
90c487da1eSNeel Natu static char *host_base = "/";
91c487da1eSNeel Natu static struct termios term, oldterm;
92c487da1eSNeel Natu static int disk_fd = -1;
93c487da1eSNeel Natu 
94b060ba50SNeel Natu static char *vmname, *progname;
95c487da1eSNeel Natu static struct vmctx *ctx;
96c487da1eSNeel Natu 
97c487da1eSNeel Natu static uint64_t gdtbase, cr3, rsp;
98c487da1eSNeel Natu 
99c487da1eSNeel Natu static void cb_exit(void *arg, int v);
100c487da1eSNeel Natu 
101c487da1eSNeel Natu /*
102c487da1eSNeel Natu  * Console i/o callbacks
103c487da1eSNeel Natu  */
104c487da1eSNeel Natu 
105c487da1eSNeel Natu static void
106c487da1eSNeel Natu cb_putc(void *arg, int ch)
107c487da1eSNeel Natu {
108c487da1eSNeel Natu 	char c = ch;
109c487da1eSNeel Natu 
110c487da1eSNeel Natu 	write(1, &c, 1);
111c487da1eSNeel Natu }
112c487da1eSNeel Natu 
113c487da1eSNeel Natu static int
114c487da1eSNeel Natu cb_getc(void *arg)
115c487da1eSNeel Natu {
116c487da1eSNeel Natu 	char c;
117c487da1eSNeel Natu 
118c487da1eSNeel Natu 	if (read(0, &c, 1) == 1)
119c487da1eSNeel Natu 		return (c);
120c487da1eSNeel Natu 	return (-1);
121c487da1eSNeel Natu }
122c487da1eSNeel Natu 
123c487da1eSNeel Natu static int
124c487da1eSNeel Natu cb_poll(void *arg)
125c487da1eSNeel Natu {
126c487da1eSNeel Natu 	int n;
127c487da1eSNeel Natu 
128c487da1eSNeel Natu 	if (ioctl(0, FIONREAD, &n) >= 0)
129c487da1eSNeel Natu 		return (n > 0);
130c487da1eSNeel Natu 	return (0);
131c487da1eSNeel Natu }
132c487da1eSNeel Natu 
133c487da1eSNeel Natu /*
134c487da1eSNeel Natu  * Host filesystem i/o callbacks
135c487da1eSNeel Natu  */
136c487da1eSNeel Natu 
137c487da1eSNeel Natu struct cb_file {
138c487da1eSNeel Natu 	int cf_isdir;
139c487da1eSNeel Natu 	size_t cf_size;
140c487da1eSNeel Natu 	struct stat cf_stat;
141c487da1eSNeel Natu 	union {
142c487da1eSNeel Natu 		int fd;
143c487da1eSNeel Natu 		DIR *dir;
144c487da1eSNeel Natu 	} cf_u;
145c487da1eSNeel Natu };
146c487da1eSNeel Natu 
147c487da1eSNeel Natu static int
148c487da1eSNeel Natu cb_open(void *arg, const char *filename, void **hp)
149c487da1eSNeel Natu {
150c487da1eSNeel Natu 	struct stat st;
151c487da1eSNeel Natu 	struct cb_file *cf;
152c487da1eSNeel Natu 	char path[PATH_MAX];
153c487da1eSNeel Natu 
154c487da1eSNeel Natu 	if (!host_base)
155c487da1eSNeel Natu 		return (ENOENT);
156c487da1eSNeel Natu 
157c487da1eSNeel Natu 	strlcpy(path, host_base, PATH_MAX);
158c487da1eSNeel Natu 	if (path[strlen(path) - 1] == '/')
159c487da1eSNeel Natu 		path[strlen(path) - 1] = 0;
160c487da1eSNeel Natu 	strlcat(path, filename, PATH_MAX);
161c487da1eSNeel Natu 	cf = malloc(sizeof(struct cb_file));
162c487da1eSNeel Natu 	if (stat(path, &cf->cf_stat) < 0) {
163c487da1eSNeel Natu 		free(cf);
164c487da1eSNeel Natu 		return (errno);
165c487da1eSNeel Natu 	}
166c487da1eSNeel Natu 
167c487da1eSNeel Natu 	cf->cf_size = st.st_size;
168c487da1eSNeel Natu 	if (S_ISDIR(cf->cf_stat.st_mode)) {
169c487da1eSNeel Natu 		cf->cf_isdir = 1;
170c487da1eSNeel Natu 		cf->cf_u.dir = opendir(path);
171c487da1eSNeel Natu 		if (!cf->cf_u.dir)
172c487da1eSNeel Natu 			goto out;
173c487da1eSNeel Natu 		*hp = cf;
174c487da1eSNeel Natu 		return (0);
175c487da1eSNeel Natu 	}
176c487da1eSNeel Natu 	if (S_ISREG(cf->cf_stat.st_mode)) {
177c487da1eSNeel Natu 		cf->cf_isdir = 0;
178c487da1eSNeel Natu 		cf->cf_u.fd = open(path, O_RDONLY);
179c487da1eSNeel Natu 		if (cf->cf_u.fd < 0)
180c487da1eSNeel Natu 			goto out;
181c487da1eSNeel Natu 		*hp = cf;
182c487da1eSNeel Natu 		return (0);
183c487da1eSNeel Natu 	}
184c487da1eSNeel Natu 
185c487da1eSNeel Natu out:
186c487da1eSNeel Natu 	free(cf);
187c487da1eSNeel Natu 	return (EINVAL);
188c487da1eSNeel Natu }
189c487da1eSNeel Natu 
190c487da1eSNeel Natu static int
191c487da1eSNeel Natu cb_close(void *arg, void *h)
192c487da1eSNeel Natu {
193c487da1eSNeel Natu 	struct cb_file *cf = h;
194c487da1eSNeel Natu 
195c487da1eSNeel Natu 	if (cf->cf_isdir)
196c487da1eSNeel Natu 		closedir(cf->cf_u.dir);
197c487da1eSNeel Natu 	else
198c487da1eSNeel Natu 		close(cf->cf_u.fd);
199c487da1eSNeel Natu 	free(cf);
200c487da1eSNeel Natu 
201c487da1eSNeel Natu 	return (0);
202c487da1eSNeel Natu }
203c487da1eSNeel Natu 
204c487da1eSNeel Natu static int
205c487da1eSNeel Natu cb_isdir(void *arg, void *h)
206c487da1eSNeel Natu {
207c487da1eSNeel Natu 	struct cb_file *cf = h;
208c487da1eSNeel Natu 
209c487da1eSNeel Natu 	return (cf->cf_isdir);
210c487da1eSNeel Natu }
211c487da1eSNeel Natu 
212c487da1eSNeel Natu static int
213c487da1eSNeel Natu cb_read(void *arg, void *h, void *buf, size_t size, size_t *resid)
214c487da1eSNeel Natu {
215c487da1eSNeel Natu 	struct cb_file *cf = h;
216c487da1eSNeel Natu 	ssize_t sz;
217c487da1eSNeel Natu 
218c487da1eSNeel Natu 	if (cf->cf_isdir)
219c487da1eSNeel Natu 		return (EINVAL);
220c487da1eSNeel Natu 	sz = read(cf->cf_u.fd, buf, size);
221c487da1eSNeel Natu 	if (sz < 0)
222c487da1eSNeel Natu 		return (EINVAL);
223c487da1eSNeel Natu 	*resid = size - sz;
224c487da1eSNeel Natu 	return (0);
225c487da1eSNeel Natu }
226c487da1eSNeel Natu 
227c487da1eSNeel Natu static int
228c487da1eSNeel Natu cb_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
229c487da1eSNeel Natu 	   size_t *namelen_return, char *name)
230c487da1eSNeel Natu {
231c487da1eSNeel Natu 	struct cb_file *cf = h;
232c487da1eSNeel Natu 	struct dirent *dp;
233c487da1eSNeel Natu 
234c487da1eSNeel Natu 	if (!cf->cf_isdir)
235c487da1eSNeel Natu 		return (EINVAL);
236c487da1eSNeel Natu 
237c487da1eSNeel Natu 	dp = readdir(cf->cf_u.dir);
238c487da1eSNeel Natu 	if (!dp)
239c487da1eSNeel Natu 		return (ENOENT);
240c487da1eSNeel Natu 
241c487da1eSNeel Natu 	/*
242c487da1eSNeel Natu 	 * Note: d_namlen is in the range 0..255 and therefore less
243c487da1eSNeel Natu 	 * than PATH_MAX so we don't need to test before copying.
244c487da1eSNeel Natu 	 */
245c487da1eSNeel Natu 	*fileno_return = dp->d_fileno;
246c487da1eSNeel Natu 	*type_return = dp->d_type;
247c487da1eSNeel Natu 	*namelen_return = dp->d_namlen;
248c487da1eSNeel Natu 	memcpy(name, dp->d_name, dp->d_namlen);
249c487da1eSNeel Natu 	name[dp->d_namlen] = 0;
250c487da1eSNeel Natu 
251c487da1eSNeel Natu 	return (0);
252c487da1eSNeel Natu }
253c487da1eSNeel Natu 
254c487da1eSNeel Natu static int
255c487da1eSNeel Natu cb_seek(void *arg, void *h, uint64_t offset, int whence)
256c487da1eSNeel Natu {
257c487da1eSNeel Natu 	struct cb_file *cf = h;
258c487da1eSNeel Natu 
259c487da1eSNeel Natu 	if (cf->cf_isdir)
260c487da1eSNeel Natu 		return (EINVAL);
261c487da1eSNeel Natu 	if (lseek(cf->cf_u.fd, offset, whence) < 0)
262c487da1eSNeel Natu 		return (errno);
263c487da1eSNeel Natu 	return (0);
264c487da1eSNeel Natu }
265c487da1eSNeel Natu 
266c487da1eSNeel Natu static int
267c487da1eSNeel Natu cb_stat(void *arg, void *h, int *mode, int *uid, int *gid, uint64_t *size)
268c487da1eSNeel Natu {
269c487da1eSNeel Natu 	struct cb_file *cf = h;
270c487da1eSNeel Natu 
271c487da1eSNeel Natu 	*mode = cf->cf_stat.st_mode;
272c487da1eSNeel Natu 	*uid = cf->cf_stat.st_uid;
273c487da1eSNeel Natu 	*gid = cf->cf_stat.st_gid;
274c487da1eSNeel Natu 	*size = cf->cf_stat.st_size;
275c487da1eSNeel Natu 	return (0);
276c487da1eSNeel Natu }
277c487da1eSNeel Natu 
278c487da1eSNeel Natu /*
279c487da1eSNeel Natu  * Disk image i/o callbacks
280c487da1eSNeel Natu  */
281c487da1eSNeel Natu 
282c487da1eSNeel Natu static int
283c487da1eSNeel Natu cb_diskread(void *arg, int unit, uint64_t from, void *to, size_t size,
284c487da1eSNeel Natu 	    size_t *resid)
285c487da1eSNeel Natu {
286c487da1eSNeel Natu 	ssize_t n;
287c487da1eSNeel Natu 
288c487da1eSNeel Natu 	if (unit != 0 || disk_fd == -1)
289c487da1eSNeel Natu 		return (EIO);
290c487da1eSNeel Natu 	n = pread(disk_fd, to, size, from);
291c487da1eSNeel Natu 	if (n < 0)
292c487da1eSNeel Natu 		return (errno);
293c487da1eSNeel Natu 	*resid = size - n;
294c487da1eSNeel Natu 	return (0);
295c487da1eSNeel Natu }
296c487da1eSNeel Natu 
297a10c6f55SNeel Natu static int
298a10c6f55SNeel Natu cb_diskioctl(void *arg, int unit, u_long cmd, void *data)
299a10c6f55SNeel Natu {
300a10c6f55SNeel Natu 	struct stat sb;
301a10c6f55SNeel Natu 
302a10c6f55SNeel Natu 	if (unit != 0 || disk_fd == -1)
303a10c6f55SNeel Natu 		return (EBADF);
304a10c6f55SNeel Natu 
305a10c6f55SNeel Natu 	switch (cmd) {
306a10c6f55SNeel Natu 	case DIOCGSECTORSIZE:
307a10c6f55SNeel Natu 		*(u_int *)data = 512;
308a10c6f55SNeel Natu 		break;
309a10c6f55SNeel Natu 	case DIOCGMEDIASIZE:
310a10c6f55SNeel Natu 		if (fstat(disk_fd, &sb) == 0)
311a10c6f55SNeel Natu 			*(off_t *)data = sb.st_size;
312a10c6f55SNeel Natu 		else
313a10c6f55SNeel Natu 			return (ENOTTY);
314a10c6f55SNeel Natu 		break;
315a10c6f55SNeel Natu 	default:
316a10c6f55SNeel Natu 		return (ENOTTY);
317a10c6f55SNeel Natu 	}
318a10c6f55SNeel Natu 
319a10c6f55SNeel Natu 	return (0);
320a10c6f55SNeel Natu }
321a10c6f55SNeel Natu 
322c487da1eSNeel Natu /*
323c487da1eSNeel Natu  * Guest virtual machine i/o callbacks
324c487da1eSNeel Natu  */
325c487da1eSNeel Natu static int
326c487da1eSNeel Natu cb_copyin(void *arg, const void *from, uint64_t to, size_t size)
327c487da1eSNeel Natu {
328b060ba50SNeel Natu 	char *ptr;
329c487da1eSNeel Natu 
330c487da1eSNeel Natu 	to &= 0x7fffffff;
331b060ba50SNeel Natu 
332b060ba50SNeel Natu 	ptr = vm_map_gpa(ctx, to, size);
333b060ba50SNeel Natu 	if (ptr == NULL)
334c487da1eSNeel Natu 		return (EFAULT);
335c487da1eSNeel Natu 
336b060ba50SNeel Natu 	memcpy(ptr, from, size);
337c487da1eSNeel Natu 	return (0);
338c487da1eSNeel Natu }
339c487da1eSNeel Natu 
340c487da1eSNeel Natu static int
341c487da1eSNeel Natu cb_copyout(void *arg, uint64_t from, void *to, size_t size)
342c487da1eSNeel Natu {
343b060ba50SNeel Natu 	char *ptr;
344c487da1eSNeel Natu 
345c487da1eSNeel Natu 	from &= 0x7fffffff;
346b060ba50SNeel Natu 
347b060ba50SNeel Natu 	ptr = vm_map_gpa(ctx, from, size);
348b060ba50SNeel Natu 	if (ptr == NULL)
349c487da1eSNeel Natu 		return (EFAULT);
350c487da1eSNeel Natu 
351b060ba50SNeel Natu 	memcpy(to, ptr, size);
352c487da1eSNeel Natu 	return (0);
353c487da1eSNeel Natu }
354c487da1eSNeel Natu 
355c487da1eSNeel Natu static void
356c487da1eSNeel Natu cb_setreg(void *arg, int r, uint64_t v)
357c487da1eSNeel Natu {
358c487da1eSNeel Natu 	int error;
359c487da1eSNeel Natu 	enum vm_reg_name vmreg;
360c487da1eSNeel Natu 
361c487da1eSNeel Natu 	vmreg = VM_REG_LAST;
362c487da1eSNeel Natu 
363c487da1eSNeel Natu 	switch (r) {
364c487da1eSNeel Natu 	case 4:
365c487da1eSNeel Natu 		vmreg = VM_REG_GUEST_RSP;
366c487da1eSNeel Natu 		rsp = v;
367c487da1eSNeel Natu 		break;
368c487da1eSNeel Natu 	default:
369c487da1eSNeel Natu 		break;
370c487da1eSNeel Natu 	}
371c487da1eSNeel Natu 
372c487da1eSNeel Natu 	if (vmreg == VM_REG_LAST) {
373c487da1eSNeel Natu 		printf("test_setreg(%d): not implemented\n", r);
374c487da1eSNeel Natu 		cb_exit(NULL, USERBOOT_EXIT_QUIT);
375c487da1eSNeel Natu 	}
376c487da1eSNeel Natu 
377c487da1eSNeel Natu 	error = vm_set_register(ctx, BSP, vmreg, v);
378c487da1eSNeel Natu 	if (error) {
379c487da1eSNeel Natu 		perror("vm_set_register");
380c487da1eSNeel Natu 		cb_exit(NULL, USERBOOT_EXIT_QUIT);
381c487da1eSNeel Natu 	}
382c487da1eSNeel Natu }
383c487da1eSNeel Natu 
384c487da1eSNeel Natu static void
385c487da1eSNeel Natu cb_setmsr(void *arg, int r, uint64_t v)
386c487da1eSNeel Natu {
387c487da1eSNeel Natu 	int error;
388c487da1eSNeel Natu 	enum vm_reg_name vmreg;
389c487da1eSNeel Natu 
390c487da1eSNeel Natu 	vmreg = VM_REG_LAST;
391c487da1eSNeel Natu 
392c487da1eSNeel Natu 	switch (r) {
393c487da1eSNeel Natu 	case MSR_EFER:
394c487da1eSNeel Natu 		vmreg = VM_REG_GUEST_EFER;
395c487da1eSNeel Natu 		break;
396c487da1eSNeel Natu 	default:
397c487da1eSNeel Natu 		break;
398c487da1eSNeel Natu 	}
399c487da1eSNeel Natu 
400c487da1eSNeel Natu 	if (vmreg == VM_REG_LAST) {
401c487da1eSNeel Natu 		printf("test_setmsr(%d): not implemented\n", r);
402c487da1eSNeel Natu 		cb_exit(NULL, USERBOOT_EXIT_QUIT);
403c487da1eSNeel Natu 	}
404c487da1eSNeel Natu 
405c487da1eSNeel Natu 	error = vm_set_register(ctx, BSP, vmreg, v);
406c487da1eSNeel Natu 	if (error) {
407c487da1eSNeel Natu 		perror("vm_set_msr");
408c487da1eSNeel Natu 		cb_exit(NULL, USERBOOT_EXIT_QUIT);
409c487da1eSNeel Natu 	}
410c487da1eSNeel Natu }
411c487da1eSNeel Natu 
412c487da1eSNeel Natu static void
413c487da1eSNeel Natu cb_setcr(void *arg, int r, uint64_t v)
414c487da1eSNeel Natu {
415c487da1eSNeel Natu 	int error;
416c487da1eSNeel Natu 	enum vm_reg_name vmreg;
417c487da1eSNeel Natu 
418c487da1eSNeel Natu 	vmreg = VM_REG_LAST;
419c487da1eSNeel Natu 
420c487da1eSNeel Natu 	switch (r) {
421c487da1eSNeel Natu 	case 0:
422c487da1eSNeel Natu 		vmreg = VM_REG_GUEST_CR0;
423c487da1eSNeel Natu 		break;
424c487da1eSNeel Natu 	case 3:
425c487da1eSNeel Natu 		vmreg = VM_REG_GUEST_CR3;
426c487da1eSNeel Natu 		cr3 = v;
427c487da1eSNeel Natu 		break;
428c487da1eSNeel Natu 	case 4:
429c487da1eSNeel Natu 		vmreg = VM_REG_GUEST_CR4;
430c487da1eSNeel Natu 		break;
431c487da1eSNeel Natu 	default:
432c487da1eSNeel Natu 		break;
433c487da1eSNeel Natu 	}
434c487da1eSNeel Natu 
435c487da1eSNeel Natu 	if (vmreg == VM_REG_LAST) {
436c487da1eSNeel Natu 		printf("test_setcr(%d): not implemented\n", r);
437c487da1eSNeel Natu 		cb_exit(NULL, USERBOOT_EXIT_QUIT);
438c487da1eSNeel Natu 	}
439c487da1eSNeel Natu 
440c487da1eSNeel Natu 	error = vm_set_register(ctx, BSP, vmreg, v);
441c487da1eSNeel Natu 	if (error) {
442c487da1eSNeel Natu 		perror("vm_set_cr");
443c487da1eSNeel Natu 		cb_exit(NULL, USERBOOT_EXIT_QUIT);
444c487da1eSNeel Natu 	}
445c487da1eSNeel Natu }
446c487da1eSNeel Natu 
447c487da1eSNeel Natu static void
448c487da1eSNeel Natu cb_setgdt(void *arg, uint64_t base, size_t size)
449c487da1eSNeel Natu {
450c487da1eSNeel Natu 	int error;
451c487da1eSNeel Natu 
452c487da1eSNeel Natu 	error = vm_set_desc(ctx, BSP, VM_REG_GUEST_GDTR, base, size - 1, 0);
453c487da1eSNeel Natu 	if (error != 0) {
454c487da1eSNeel Natu 		perror("vm_set_desc(gdt)");
455c487da1eSNeel Natu 		cb_exit(NULL, USERBOOT_EXIT_QUIT);
456c487da1eSNeel Natu 	}
457c487da1eSNeel Natu 
458c487da1eSNeel Natu 	gdtbase = base;
459c487da1eSNeel Natu }
460c487da1eSNeel Natu 
461c487da1eSNeel Natu static void
462c487da1eSNeel Natu cb_exec(void *arg, uint64_t rip)
463c487da1eSNeel Natu {
464c487da1eSNeel Natu 	int error;
465c487da1eSNeel Natu 
466c487da1eSNeel Natu 	error = vm_setup_freebsd_registers(ctx, BSP, rip, cr3, gdtbase, rsp);
467c487da1eSNeel Natu 	if (error) {
468c487da1eSNeel Natu 		perror("vm_setup_freebsd_registers");
469c487da1eSNeel Natu 		cb_exit(NULL, USERBOOT_EXIT_QUIT);
470c487da1eSNeel Natu 	}
471c487da1eSNeel Natu 
472c487da1eSNeel Natu 	cb_exit(NULL, 0);
473c487da1eSNeel Natu }
474c487da1eSNeel Natu 
475c487da1eSNeel Natu /*
476c487da1eSNeel Natu  * Misc
477c487da1eSNeel Natu  */
478c487da1eSNeel Natu 
479c487da1eSNeel Natu static void
480c487da1eSNeel Natu cb_delay(void *arg, int usec)
481c487da1eSNeel Natu {
482c487da1eSNeel Natu 
483c487da1eSNeel Natu 	usleep(usec);
484c487da1eSNeel Natu }
485c487da1eSNeel Natu 
486c487da1eSNeel Natu static void
487c487da1eSNeel Natu cb_exit(void *arg, int v)
488c487da1eSNeel Natu {
489c487da1eSNeel Natu 
490c487da1eSNeel Natu 	tcsetattr(0, TCSAFLUSH, &oldterm);
491c487da1eSNeel Natu 	exit(v);
492c487da1eSNeel Natu }
493c487da1eSNeel Natu 
494c487da1eSNeel Natu static void
495c487da1eSNeel Natu cb_getmem(void *arg, uint64_t *ret_lowmem, uint64_t *ret_highmem)
496c487da1eSNeel Natu {
497c487da1eSNeel Natu 
498318224bbSNeel Natu 	vm_get_memory_seg(ctx, 0, ret_lowmem, NULL);
499318224bbSNeel Natu 	vm_get_memory_seg(ctx, 4 * GB, ret_highmem, NULL);
500c487da1eSNeel Natu }
501c487da1eSNeel Natu 
502*b6afa84bSNeel Natu struct env {
503*b6afa84bSNeel Natu 	const char *str;	/* name=value */
504*b6afa84bSNeel Natu 	SLIST_ENTRY(env) next;
505*b6afa84bSNeel Natu };
506*b6afa84bSNeel Natu 
507*b6afa84bSNeel Natu static SLIST_HEAD(envhead, env) envhead;
508*b6afa84bSNeel Natu 
509*b6afa84bSNeel Natu static void
510*b6afa84bSNeel Natu addenv(const char *str)
511*b6afa84bSNeel Natu {
512*b6afa84bSNeel Natu 	struct env *env;
513*b6afa84bSNeel Natu 
514*b6afa84bSNeel Natu 	env = malloc(sizeof(struct env));
515*b6afa84bSNeel Natu 	env->str = str;
516*b6afa84bSNeel Natu 	SLIST_INSERT_HEAD(&envhead, env, next);
517*b6afa84bSNeel Natu }
518*b6afa84bSNeel Natu 
519c3e9ce33SNeel Natu static const char *
520c3e9ce33SNeel Natu cb_getenv(void *arg, int num)
521c3e9ce33SNeel Natu {
522*b6afa84bSNeel Natu 	int i;
523*b6afa84bSNeel Natu 	struct env *env;
524c3e9ce33SNeel Natu 
525*b6afa84bSNeel Natu 	i = 0;
526*b6afa84bSNeel Natu 	SLIST_FOREACH(env, &envhead, next) {
527*b6afa84bSNeel Natu 		if (i == num)
528*b6afa84bSNeel Natu 			return (env->str);
529*b6afa84bSNeel Natu 		i++;
530*b6afa84bSNeel Natu 	}
531c3e9ce33SNeel Natu 
532c3e9ce33SNeel Natu 	return (NULL);
533c3e9ce33SNeel Natu }
534c3e9ce33SNeel Natu 
535a10c6f55SNeel Natu static struct loader_callbacks cb = {
536c487da1eSNeel Natu 	.getc = cb_getc,
537c487da1eSNeel Natu 	.putc = cb_putc,
538c487da1eSNeel Natu 	.poll = cb_poll,
539c487da1eSNeel Natu 
540c487da1eSNeel Natu 	.open = cb_open,
541c487da1eSNeel Natu 	.close = cb_close,
542c487da1eSNeel Natu 	.isdir = cb_isdir,
543c487da1eSNeel Natu 	.read = cb_read,
544c487da1eSNeel Natu 	.readdir = cb_readdir,
545c487da1eSNeel Natu 	.seek = cb_seek,
546c487da1eSNeel Natu 	.stat = cb_stat,
547c487da1eSNeel Natu 
548c487da1eSNeel Natu 	.diskread = cb_diskread,
549a10c6f55SNeel Natu 	.diskioctl = cb_diskioctl,
550c487da1eSNeel Natu 
551c487da1eSNeel Natu 	.copyin = cb_copyin,
552c487da1eSNeel Natu 	.copyout = cb_copyout,
553c487da1eSNeel Natu 	.setreg = cb_setreg,
554c487da1eSNeel Natu 	.setmsr = cb_setmsr,
555c487da1eSNeel Natu 	.setcr = cb_setcr,
556c487da1eSNeel Natu 	.setgdt = cb_setgdt,
557c487da1eSNeel Natu 	.exec = cb_exec,
558c487da1eSNeel Natu 
559c487da1eSNeel Natu 	.delay = cb_delay,
560c487da1eSNeel Natu 	.exit = cb_exit,
561c487da1eSNeel Natu 	.getmem = cb_getmem,
562c3e9ce33SNeel Natu 
563c3e9ce33SNeel Natu 	.getenv = cb_getenv,
564c487da1eSNeel Natu };
565c487da1eSNeel Natu 
566c487da1eSNeel Natu static void
567c487da1eSNeel Natu usage(void)
568c487da1eSNeel Natu {
569c487da1eSNeel Natu 
570b060ba50SNeel Natu 	fprintf(stderr,
571b060ba50SNeel Natu 	    "usage: %s [-m mem-size][-d <disk-path>] [-h <host-path>] "
572*b6afa84bSNeel Natu 	    "[-e <name=value>] <vmname>\n", progname);
573c487da1eSNeel Natu 	exit(1);
574c487da1eSNeel Natu }
575c487da1eSNeel Natu 
576c487da1eSNeel Natu int
577c487da1eSNeel Natu main(int argc, char** argv)
578c487da1eSNeel Natu {
579c487da1eSNeel Natu 	void *h;
580a10c6f55SNeel Natu 	void (*func)(struct loader_callbacks *, void *, int, int);
581b060ba50SNeel Natu 	uint64_t mem_size;
582c487da1eSNeel Natu 	int opt, error;
583c487da1eSNeel Natu 	char *disk_image;
584c487da1eSNeel Natu 
585c487da1eSNeel Natu 	progname = argv[0];
586c487da1eSNeel Natu 
587b060ba50SNeel Natu 	mem_size = 256 * MB;
588c487da1eSNeel Natu 	disk_image = NULL;
589c487da1eSNeel Natu 
590*b6afa84bSNeel Natu 	while ((opt = getopt(argc, argv, "d:e:h:m:")) != -1) {
591c487da1eSNeel Natu 		switch (opt) {
592c487da1eSNeel Natu 		case 'd':
593c487da1eSNeel Natu 			disk_image = optarg;
594c487da1eSNeel Natu 			break;
595c487da1eSNeel Natu 
596*b6afa84bSNeel Natu 		case 'e':
597*b6afa84bSNeel Natu 			addenv(optarg);
598*b6afa84bSNeel Natu 			break;
599*b6afa84bSNeel Natu 
600c487da1eSNeel Natu 		case 'h':
601c487da1eSNeel Natu 			host_base = optarg;
602c487da1eSNeel Natu 			break;
603c487da1eSNeel Natu 
604c487da1eSNeel Natu 		case 'm':
605200758f1SNeel Natu 			error = vm_parse_memsize(optarg, &mem_size);
606200758f1SNeel Natu 			if (error != 0)
607200758f1SNeel Natu 				errx(EX_USAGE, "Invalid memsize '%s'", optarg);
608c487da1eSNeel Natu 			break;
609c487da1eSNeel Natu 		case '?':
610c487da1eSNeel Natu 			usage();
611c487da1eSNeel Natu 		}
612c487da1eSNeel Natu 	}
613c487da1eSNeel Natu 
614c487da1eSNeel Natu 	argc -= optind;
615c487da1eSNeel Natu 	argv += optind;
616c487da1eSNeel Natu 
617c487da1eSNeel Natu 	if (argc != 1)
618c487da1eSNeel Natu 		usage();
619c487da1eSNeel Natu 
620c487da1eSNeel Natu 	vmname = argv[0];
621c487da1eSNeel Natu 
622c487da1eSNeel Natu 	error = vm_create(vmname);
623c487da1eSNeel Natu 	if (error != 0 && errno != EEXIST) {
624c487da1eSNeel Natu 		perror("vm_create");
625c487da1eSNeel Natu 		exit(1);
626c487da1eSNeel Natu 
627c487da1eSNeel Natu 	}
628c487da1eSNeel Natu 
629c487da1eSNeel Natu 	ctx = vm_open(vmname);
630c487da1eSNeel Natu 	if (ctx == NULL) {
631c487da1eSNeel Natu 		perror("vm_open");
632c487da1eSNeel Natu 		exit(1);
633c487da1eSNeel Natu 	}
634c487da1eSNeel Natu 
635b060ba50SNeel Natu 	error = vm_setup_memory(ctx, mem_size, VM_MMAP_ALL);
636c487da1eSNeel Natu 	if (error) {
637b060ba50SNeel Natu 		perror("vm_setup_memory");
638c487da1eSNeel Natu 		exit(1);
639c487da1eSNeel Natu 	}
640c487da1eSNeel Natu 
641c487da1eSNeel Natu 	tcgetattr(0, &term);
642c487da1eSNeel Natu 	oldterm = term;
643c487da1eSNeel Natu 	term.c_lflag &= ~(ICANON|ECHO);
644c487da1eSNeel Natu 	term.c_iflag &= ~ICRNL;
645c487da1eSNeel Natu 	tcsetattr(0, TCSAFLUSH, &term);
64638f1b189SPeter Grehan 	h = dlopen("/boot/userboot.so", RTLD_LOCAL);
647c487da1eSNeel Natu 	if (!h) {
648c487da1eSNeel Natu 		printf("%s\n", dlerror());
649c487da1eSNeel Natu 		return (1);
650c487da1eSNeel Natu 	}
651c487da1eSNeel Natu 	func = dlsym(h, "loader_main");
652c487da1eSNeel Natu 	if (!func) {
653c487da1eSNeel Natu 		printf("%s\n", dlerror());
654c487da1eSNeel Natu 		return (1);
655c487da1eSNeel Natu 	}
656c487da1eSNeel Natu 
657c487da1eSNeel Natu 	if (disk_image) {
658c487da1eSNeel Natu 		disk_fd = open(disk_image, O_RDONLY);
659c487da1eSNeel Natu 	}
660*b6afa84bSNeel Natu 
661*b6afa84bSNeel Natu 	addenv("smbios.bios.vendor=BHYVE");
662*b6afa84bSNeel Natu 	addenv("boot_serial=1");
663*b6afa84bSNeel Natu 
664c3e9ce33SNeel Natu 	func(&cb, NULL, USERBOOT_VERSION_3, disk_fd >= 0);
665c487da1eSNeel Natu }
666