Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F157399645
D16945.id47487.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
10 KB
Referenced Files
None
Subscribers
None
D16945.id47487.diff
View Options
Index: stand/userboot/Makefile
===================================================================
--- stand/userboot/Makefile
+++ stand/userboot/Makefile
@@ -1,8 +1,10 @@
# $FreeBSD$
-.include <bsd.own.mk>
+.include <bsd.init.mk>
-SUBDIR= test userboot
+SUBDIR.yes= test
+SUBDIR.${MK_FORTH}+= userboot_4th
+SUBDIR.${MK_LOADER_LUA}+= userboot_lua
.include <bsd.subdir.mk>
Index: stand/userboot/userboot.h
===================================================================
--- stand/userboot/userboot.h
+++ stand/userboot/userboot.h
@@ -41,6 +41,13 @@
*/
#define USERBOOT_VERSION_4 4
+/*
+ * Version 5 added a callback for indicating that the guest
+ * should be restarted with a different interpreter. The callback
+ * structure is still backward compatible.
+ */
+#define USERBOOT_VERSION_5 5
+
/*
* Exit codes from the loader
*/
@@ -210,4 +217,9 @@
int (*vm_set_register)(void *arg, int vcpu, int reg, uint64_t val);
int (*vm_set_desc)(void *arg, int vcpu, int reg, uint64_t base,
u_int limit, u_int access);
+
+ /*
+ * Version 5 addition.
+ */
+ void (*swap_interpreter)(void *arg, const char *interp);
};
Index: stand/userboot/userboot/Makefile
===================================================================
--- stand/userboot/userboot/Makefile
+++ stand/userboot/userboot/Makefile
@@ -5,14 +5,14 @@
LOADER_CD9660_SUPPORT?= no
LOADER_EXT2FS_SUPPORT?= no
PIC=yes
-LOADER_INTERP=4th
.include <bsd.init.mk>
-SHLIB_NAME= userboot.so
+SHLIB_NAME= userboot_${LOADER_INTERP}.so
STRIP=
LIBDIR= /boot
+.PATH: ${.CURDIR}/../userboot
SRCS= autoload.c
SRCS+= bcache.c
SRCS+= biossmap.c
@@ -38,7 +38,12 @@
LDFLAGS+= -nostdlib -Wl,-Bsymbolic
-NEWVERSWHAT= "User boot" ${MACHINE_CPUARCH}
+NEWVERSWHAT= "User boot ${LOADER_INTERP}" ${MACHINE_CPUARCH}
+VERSION_FILE= ${.CURDIR}/../userboot/version
+
+.if ${LOADER_INTERP} == ${LOADER_DEFAULT_INTERP}
+LINKS+= ${BINDIR}/${SHLIB_NAME} ${BINDIR}/userboot.so
+.endif
.if ${MK_ZFS} != "no"
CFLAGS+= -DUSERBOOT_ZFS_SUPPORT
Index: stand/userboot/userboot/main.c
===================================================================
--- stand/userboot/userboot/main.c
+++ stand/userboot/userboot/main.c
@@ -47,6 +47,9 @@
/* Minimum version required */
#define USERBOOT_VERSION USERBOOT_VERSION_3
+#define USERBOOT_PATH "/boot/userboot.so"
+#define USERBOOT_VER_SEARCH "User boot "
+
#define MALLOCSZ (64*1024*1024)
struct loader_callbacks *callbacks;
@@ -57,6 +60,7 @@
struct arch_switch archsw; /* MI/MD interface boundary */
static void extract_currdev(void);
+static void check_interpreter(void);
void
delay(int usec)
@@ -73,6 +77,121 @@
longjmp(jb, 1);
}
+static char *
+grab_str(char *haystack, const char *needle, size_t haystacksz, size_t *rem)
+{
+ size_t nlen;
+ char c, sc;
+
+ c = *needle++;
+ nlen = strlen(needle);
+ do {
+ do {
+ if (haystacksz-- < 1)
+ return (NULL);
+ sc = *haystack++;
+ } while (sc != c);
+ if (nlen > haystacksz)
+ return (NULL);
+ } while (strncmp(haystack, needle, nlen) != 0);
+ haystack--;
+ *rem = ++haystacksz;
+ return (haystack);
+}
+
+/*
+ * Grab the interpreter string from a buffer. The input buffer will be
+ * modified in place...
+ */
+static char *
+grab_interpreter_string(char *haystack, size_t haystacksz)
+{
+ char *verstr, *trailer;
+ size_t searchsz, szleft;
+
+ searchsz = strlen(USERBOOT_VER_SEARCH);
+ szleft = haystacksz;
+ verstr = NULL;
+ /*
+ * Here we have a problem. If we had dlopen, this would be trivial...
+ * We need to grab the version string from the target, so we search for
+ * the start of it. In embedding the string we're searching for, we've
+ * added an additional instance of the string we're searching for, so
+ * we'll need to loop until we find the right one. Here we call the
+ * right one "one with a trailing comma".
+ */
+ while (verstr == NULL) {
+ verstr = grab_str(haystack, USERBOOT_VER_SEARCH, szleft, &szleft);
+ if (verstr == NULL)
+ return (NULL);
+
+ verstr += searchsz;
+ /* Comma immediately follows the interpreter */
+ trailer = strchr(verstr, ',');
+ if (trailer == NULL) {
+ /* Try again, for a later attempt. */
+ haystack = verstr;
+ szleft -= searchsz;
+ verstr = NULL;
+ continue;
+ }
+ }
+
+ *trailer = '\0';
+ return (verstr);
+}
+
+static void
+check_interpreter(void)
+{
+ struct stat st;
+ size_t rdsize;
+ char *buf, *guest_interp, *my_interp, *verstring;
+ int fd;
+
+ /*
+ * If we can't stat(2) or open(2) USERBOOT_PATH, then we'll fail by
+ * simply letting us roll on with whatever interpreter we were compiled
+ * with. This is likely not going to be an issue in reality.
+ */
+ buf = verstring = NULL;
+ if (stat(USERBOOT_PATH, &st) != 0)
+ return;
+ if ((fd = open(USERBOOT_PATH, O_RDONLY)) < 0)
+ return;
+
+ rdsize = st.st_size;
+ buf = malloc(rdsize);
+ if (buf == NULL)
+ goto out;
+ if (read(fd, buf, rdsize) < rdsize)
+ goto out;
+
+ verstring = strdup(bootprog_info);
+ if (verstring == NULL)
+ goto out;
+ my_interp = grab_interpreter_string(verstring, strlen(verstring));
+ guest_interp = grab_interpreter_string(buf, rdsize);
+ /*
+ * The guest interpreter may not have a version of userboot that
+ * specifies the interpreter installed. If that's the case, we'll
+ * assume it's legacy (4th) and request a swap to that if we're
+ * a Lua-userboot.
+ */
+ if (guest_interp == NULL && strcmp(my_interp, "lua") == 0)
+ CALLBACK(swap_interpreter, "4th");
+ else if (guest_interp == NULL)
+ /* We're already right; guest is likely 4th, and we're 4th */
+ goto out;
+ else if (strcmp(my_interp, guest_interp) != 0)
+ CALLBACK(swap_interpreter, guest_interp);
+out:
+ free(verstring);
+ free(buf);
+ close(fd);
+ return;
+}
+
void
loader_main(struct loader_callbacks *cb, void *arg, int version, int ndisks)
{
@@ -137,6 +256,13 @@
(devsw[i]->dv_init)();
extract_currdev();
+ /*
+ * Checking the interpreter isn't worth the overhead unless we
+ * actually have the swap_interpreter callback, so we actually version
+ * check here rather than later on.
+ */
+ if (version >= USERBOOT_VERSION_5)
+ check_interpreter();
if (setjmp(jb))
return;
Index: stand/userboot/userboot/version
===================================================================
--- stand/userboot/userboot/version
+++ stand/userboot/userboot/version
@@ -1,4 +1,5 @@
$FreeBSD$
+1.2: Userboot with lua or forth
1.1: Initial userland boot
Index: stand/userboot/userboot_4th/Makefile
===================================================================
--- /dev/null
+++ stand/userboot/userboot_4th/Makefile
@@ -0,0 +1,6 @@
+# $FreeBSD$
+
+LOADER_INTERP=4th
+
+.include "../userboot/Makefile"
+
Index: stand/userboot/userboot_lua/Makefile
===================================================================
--- /dev/null
+++ stand/userboot/userboot_lua/Makefile
@@ -0,0 +1,6 @@
+# $FreeBSD$
+
+LOADER_INTERP=lua
+
+.include "../userboot/Makefile"
+
Index: usr.sbin/bhyveload/bhyveload.c
===================================================================
--- usr.sbin/bhyveload/bhyveload.c
+++ usr.sbin/bhyveload/bhyveload.c
@@ -75,6 +75,7 @@
#include <getopt.h>
#include <libgen.h>
#include <limits.h>
+#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -86,6 +87,8 @@
#include "userboot.h"
+#define LOADER_MAXNAMELEN 32
+
#define MB (1024 * 1024UL)
#define GB (1024 * 1024 * 1024UL)
#define BSP 0
@@ -98,6 +101,13 @@
static int ndisks;
static int consin_fd, consout_fd;
+static int need_reinit;
+
+static void *loader_hdl;
+static char *loader;
+static int explicit_loader;
+static jmp_buf jb;
+
static char *vmname, *progname;
static struct vmctx *ctx;
@@ -560,6 +570,33 @@
return (vm_set_desc(ctx, vcpu, reg, base, limit, access));
}
+static void
+cb_swap_interpreter(void *arg, const char *interp_req)
+{
+
+ /*
+ * If the user specified a loader but we detected a mismatch, we should
+ * not try to pivot to a different loader on them.
+ */
+ free(loader);
+ if (explicit_loader == 1) {
+ perror("requested loader interpreter does not match guest userboot");
+ cb_exit(NULL, 1);
+ }
+ if (interp_req == NULL || *interp_req == '\0') {
+ perror("guest failed to request an interpreter");
+ cb_exit(NULL, 1);
+ }
+
+ loader = malloc(LOADER_MAXNAMELEN);
+ if (loader == NULL)
+ err(EX_OSERR, "malloc");
+ snprintf(loader, LOADER_MAXNAMELEN, "/boot/userboot_%s.so",
+ interp_req);
+ need_reinit = 1;
+ longjmp(jb, 1);
+}
+
static struct loader_callbacks cb = {
.getc = cb_getc,
.putc = cb_putc,
@@ -593,6 +630,9 @@
/* Version 4 additions */
.vm_set_register = cb_vm_set_register,
.vm_set_desc = cb_vm_set_desc,
+
+ /* Version 5 additions */
+ .swap_interpreter = cb_swap_interpreter,
};
static int
@@ -661,16 +701,12 @@
int
main(int argc, char** argv)
{
- char *loader;
- void *h;
void (*func)(struct loader_callbacks *, void *, int, int);
uint64_t mem_size;
- int opt, error, need_reinit, memflags;
+ int opt, error, memflags;
progname = basename(argv[0]);
- loader = NULL;
-
memflags = 0;
mem_size = 256 * MB;
@@ -705,6 +741,7 @@
loader = strdup(optarg);
if (loader == NULL)
err(EX_OSERR, "malloc");
+ explicit_loader = 1;
break;
case 'm':
@@ -747,6 +784,13 @@
exit(1);
}
+ /*
+ * setjmp in the case the guest wants to swap out interpreter,
+ * cb_swap_interpreter will swap out loader as appropriate and set
+ * need_reinit so that we end up in a clean state once again.
+ */
+ setjmp(jb);
+
if (need_reinit) {
error = vm_reinit(ctx);
if (error) {
@@ -767,13 +811,15 @@
if (loader == NULL)
err(EX_OSERR, "malloc");
}
- h = dlopen(loader, RTLD_LOCAL);
- if (!h) {
+ if (loader_hdl != NULL)
+ dlclose(loader_hdl);
+ loader_hdl = dlopen(loader, RTLD_LOCAL);
+ if (!loader_hdl) {
printf("%s\n", dlerror());
free(loader);
return (1);
}
- func = dlsym(h, "loader_main");
+ func = dlsym(loader_hdl, "loader_main");
if (!func) {
printf("%s\n", dlerror());
free(loader);
@@ -790,7 +836,7 @@
addenv("smbios.bios.vendor=BHYVE");
addenv("boot_serial=1");
- func(&cb, NULL, USERBOOT_VERSION_4, ndisks);
+ func(&cb, NULL, USERBOOT_VERSION_5, ndisks);
free(loader);
return (0);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, May 22, 12:07 AM (17 h, 36 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
33410171
Default Alt Text
D16945.id47487.diff (10 KB)
Attached To
Mode
D16945: userboot: handle guest interpreter mismatches more intelligently
Attached
Detach File
Event Timeline
Log In to Comment