From nobody Thu Mar 03 07:55:54 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 795F219EA60F; Thu, 3 Mar 2022 07:55:54 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4K8NZy2z2Zz3vHl; Thu, 3 Mar 2022 07:55:54 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1646294154; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ymwc8tNciaRGwVVDpvXtF1ab8Vvvc5HM7DjLgO0Xywo=; b=EQzzfXp1pQ35KcpucCiTPs7tY7KGDudIfy4C+alFPGDIaL94+S7Il5GAF44qKt/xiC6Jl5 cLywvyRU+TPVOaiA/ymNP0eE6T+GqexlYthHQeRU1fyfOFdmcMB4PeLswuZx9aM7mD+cng 2JDjEHUq96b7jm4djC2HuxCvoPcEE2pVTd/tuiq/a9gPnMWi6RGYjvaU6WkcodwUf+hrRL 0zXmq1SexnfAkjuIZtMQ9/qqOUVRxLImCc8sZihOsi3jBprzbHiOLxpem1dcvQTGo5Vcyj wrEtBsGYcqEFFpGFwPdYRH4kvdujziCFiHk7SsdY9pC47wJhABB0h+h880KIIA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 41EC114112; Thu, 3 Mar 2022 07:55:54 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 2237tspY071670; Thu, 3 Mar 2022 07:55:54 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2237tsaZ071669; Thu, 3 Mar 2022 07:55:54 GMT (envelope-from git) Date: Thu, 3 Mar 2022 07:55:54 GMT Message-Id: <202203030755.2237tsaZ071669@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Robert Wing Subject: git: 87f6367f1061 - main - bhyve: add varfile option to nvlist of lpc device List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: rew X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 87f6367f10614f58e5f93130b7be3364d2f83068 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1646294154; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ymwc8tNciaRGwVVDpvXtF1ab8Vvvc5HM7DjLgO0Xywo=; b=am0V625+RC5x44lQk0CL4julJHRNtuDMDnxzYz6TmRoyOEJGh8KmvfegOIE4SpmmJrXwWS GXlqK82Ly+96y2s0ksoclGfZaNgQhm+gvs+cmqz9Y5srrK8jXGdOxMiC7DO07cI1Jlee6u RLCoqHkCZsF+oZ8aPxXODBXx6A15YyMA5akpgTv0RP5yjI+OSP6fGlNF5G6ZYnKDuc2Zfa FIGwwL0jPMfju0x2xA4B6527wOc0jipl5ox/NcM118MMnP3l51FuxcDdVwylRgSyfAZR6x h+hGYS4ctwF3oAyf+ZaYKno6AIWzpVB7uQQAA/XendC99aVrK+fxNU+AT3EblA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1646294154; a=rsa-sha256; cv=none; b=s93r4+R9mUHX8Ue5CnyVJuWhz5n/26pcHpF/wxeltfnLJXgMOeWnw3PYxy+DSgVMkK0g9H mYlbAavhaYgrBX3BT9yb3plOeE9N2eeHMY0fjRRbqY37O4o7mxEdCZEECti4ivAcoBx4Ng gOQioOnQ0gFKCT+T2NJ8wDZMiEfKPgEXlmAGSKJ8UH7h3F1oYfkFJq8BqEFatJgSuJdC1E sTc2Z5gyNQzwHUJvcc4uwu0jpdJ2cae+oqh3D2Us4ldOKsH1gJxGyIew7aFgje56KpYQ+a iUyHh1R6EFHB3Vp2mC+7zwrLFkhhJBYrd6r36cn9vTgouAHPS15TyniVXVY+Hg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by rew: URL: https://cgit.FreeBSD.org/src/commit/?id=87f6367f10614f58e5f93130b7be3364d2f83068 commit 87f6367f10614f58e5f93130b7be3364d2f83068 Author: Corvin Köhne AuthorDate: 2022-03-03 07:45:20 +0000 Commit: Robert Wing CommitDate: 2022-03-03 07:50:24 +0000 bhyve: add varfile option to nvlist of lpc device Use seperate nvlist entries for the romfile and the varfile. While here, don't leak varfd in bootrom_loadrom(). Reviewed by: jhb, markj Differential Revision: https://reviews.freebsd.org/D33433 --- usr.sbin/bhyve/bhyve_config.5 | 7 +++++- usr.sbin/bhyve/bootrom.c | 52 +++++++++++++++++++++++++++++-------------- usr.sbin/bhyve/bootrom.h | 4 +++- usr.sbin/bhyve/pci_lpc.c | 23 ++++++++++++++----- 4 files changed, 62 insertions(+), 24 deletions(-) diff --git a/usr.sbin/bhyve/bhyve_config.5 b/usr.sbin/bhyve/bhyve_config.5 index 125484cd4727..27b9fe7f77e3 100644 --- a/usr.sbin/bhyve/bhyve_config.5 +++ b/usr.sbin/bhyve/bhyve_config.5 @@ -23,7 +23,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd September 25, 2021 +.Dd March 2, 2022 .Dt BHYVE_CONFIG 5 .Os .Sh NAME @@ -448,6 +448,11 @@ The contents of this file are copied into the guest's memory ending just before the 4GB physical address. If a boot ROM is present, a firmware interface device is also enabled for use by the boot ROM. +.It Va bootvars Ta path Ta Ta +Path to boot VARS. +The contents of this file are copied beneath the boot ROM. +Firmware can write to it to save variables. +All variables will be persistent even on reboots of the guest. .It Va com1 Ta node Ta Ta Settings for the COM1 serial port device. .It Va com2 Ta node Ta Ta diff --git a/usr.sbin/bhyve/bootrom.c b/usr.sbin/bhyve/bootrom.c index 757ec07d4a54..2406d4539e55 100644 --- a/usr.sbin/bhyve/bootrom.c +++ b/usr.sbin/bhyve/bootrom.c @@ -191,19 +191,33 @@ bootrom_alloc(struct vmctx *ctx, size_t len, int prot, int flags, } int -bootrom_loadrom(struct vmctx *ctx, const char *romfile) +bootrom_loadrom(struct vmctx *ctx, const nvlist_t *nvl) { struct stat sbuf; ssize_t rlen; off_t rom_size, var_size, total_size; - char *ptr, *varfile; + char *ptr, *romfile; int fd, varfd, i, rv; + const char *bootrom, *varfile; rv = -1; varfd = -1; - varfile = strdup(romfile); - romfile = strsep(&varfile, ","); + bootrom = get_config_value_node(nvl, "bootrom"); + if (bootrom == NULL) { + return (-1); + } + + /* + * get_config_value_node may use a thread local buffer to return + * variables. So, when we query the second variable, the first variable + * might get overwritten. For that reason, the bootrom should be + * duplicated. + */ + romfile = strdup(bootrom); + if (romfile == NULL) { + return (-1); + } fd = open(romfile, O_RDONLY); if (fd < 0) { @@ -212,6 +226,16 @@ bootrom_loadrom(struct vmctx *ctx, const char *romfile) goto done; } + if (fstat(fd, &sbuf) < 0) { + EPRINTLN("Could not fstat bootrom file \"%s\": %s", romfile, + strerror(errno)); + goto done; + } + + rom_size = sbuf.st_size; + + varfile = get_config_value_node(nvl, "bootvars"); + var_size = 0; if (varfile != NULL) { varfd = open(varfile, O_RDWR); if (varfd < 0) { @@ -219,23 +243,14 @@ bootrom_loadrom(struct vmctx *ctx, const char *romfile) "\"%s\": %s\n", varfile, strerror(errno)); goto done; } - } - - if (fstat(fd, &sbuf) < 0) { - EPRINTLN("Could not fstat bootrom file \"%s\": %s", - romfile, strerror(errno)); - goto done; - } - rom_size = sbuf.st_size; - if (varfd < 0) { - var_size = 0; - } else { if (fstat(varfd, &sbuf) < 0) { - fprintf(stderr, "Could not fstat bootrom variable file \"%s\": %s\n", - varfile, strerror(errno)); + fprintf(stderr, + "Could not fstat bootrom variable file \"%s\": %s\n", + varfile, strerror(errno)); goto done; } + var_size = sbuf.st_size; } @@ -291,7 +306,10 @@ bootrom_loadrom(struct vmctx *ctx, const char *romfile) rv = 0; done: + if (varfd >= 0) + close(varfd); if (fd >= 0) close(fd); + free(romfile); return (rv); } diff --git a/usr.sbin/bhyve/bootrom.h b/usr.sbin/bhyve/bootrom.h index da802343eefc..f718d5728c84 100644 --- a/usr.sbin/bhyve/bootrom.h +++ b/usr.sbin/bhyve/bootrom.h @@ -36,6 +36,8 @@ #include #include +#include "config.h" + struct vmctx; void init_bootrom(struct vmctx *ctx); @@ -45,6 +47,6 @@ enum { }; int bootrom_alloc(struct vmctx *ctx, size_t len, int prot, int flags, char **region_out, uint64_t *gpa_out); -int bootrom_loadrom(struct vmctx *ctx, const char *romfile); +int bootrom_loadrom(struct vmctx *ctx, const nvlist_t *nvl); #endif diff --git a/usr.sbin/bhyve/pci_lpc.c b/usr.sbin/bhyve/pci_lpc.c index 42a6e5c2f78c..5c3aa238f43d 100644 --- a/usr.sbin/bhyve/pci_lpc.c +++ b/usr.sbin/bhyve/pci_lpc.c @@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include #include @@ -95,13 +96,24 @@ lpc_device_parse(const char *opts) { int unit, error; char *str, *cpy, *lpcdev, *node_name; + const char *romfile, *varfile; error = -1; str = cpy = strdup(opts); lpcdev = strsep(&str, ","); if (lpcdev != NULL) { if (strcasecmp(lpcdev, "bootrom") == 0) { - set_config_value("lpc.bootrom", str); + romfile = strsep(&str, ","); + if (romfile == NULL) { + errx(4, "invalid bootrom option \"%s\"", opts); + } + set_config_value("lpc.bootrom", romfile); + + varfile = strsep(&str, ","); + if (varfile != NULL) { + set_config_value("lpc.bootvars", varfile); + } + error = 0; goto done; } @@ -204,13 +216,14 @@ lpc_init(struct vmctx *ctx) { struct lpc_uart_softc *sc; struct inout_port iop; - const char *backend, *name, *romfile; + const char *backend, *name; char *node_name; int unit, error; + const nvlist_t *nvl; - romfile = get_config_value("lpc.bootrom"); - if (romfile != NULL) { - error = bootrom_loadrom(ctx, romfile); + nvl = find_config_node("lpc"); + if (nvl != NULL && nvlist_exists(nvl, "bootrom")) { + error = bootrom_loadrom(ctx, nvl); if (error) return (error); }