From nobody Mon Mar 27 06:40:08 2023 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 4PlNV11wsmz41qbh; Mon, 27 Mar 2023 06:40:09 +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 4PlNV11M0Vz41Gl; Mon, 27 Mar 2023 06:40:09 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1679899209; 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=c7ZyfrT89U1zUtMzDlAyv+fqD3ufeC/IqsqG7m07nx4=; b=ZgNS3Y5ETlG+sROhkNt31QppYbLkjyPD3huls5QpeMsOi54oLnCSrKsiLDWDbKFEvXr4+a NI04oNEbFYqOCYKWGmN8XHiZlfOpj3CxpkTLY8nOv9E+nGk88q1L+rwVDOpVUAPozW6Ek9 OlCWhdcmIJhZMDq35JwqGYAhNZ0oBKFdnUXXqIli7xlrHxAAjpl0OPnQGIy/WBkD3ZqrXK 7wJIprX0hhZyNVuEkrC6gSf8W5ZZgPUjPfLLF4jIc2K5RdWIuD5irJ2ueOantYvzVgmnJS G8Fw4rkzt3I77WWYgCnp01RehB3yyDn+WCooTvlSvJufCKtGaHz+Q29qwlCdbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1679899209; 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=c7ZyfrT89U1zUtMzDlAyv+fqD3ufeC/IqsqG7m07nx4=; b=hDSDgo6WoIR3E/wOjGJI8QvmJVhWIYiXq7WXnLbpREkqcYOhoTK//MOxnDOyA1uX3k0L8K EDjTa2xIUf1ZokWWVxckIBbyzy0O7xBsaERMLDIy/pUuuKU6PWEuBftbvO0gMfuGtsF+ND CiJUtBf6zVk4JLa1HYmjURdXYd+SIKQcGCwYU+Z+xgM3AT5KUgyGXp85RZdt2Y+H0UgSkv FSz6v6X1x3MqWTgxFrc0xUNg9bMYI63Y0uYJhAVJINjT45sTNZEaX3i4RMDrF42FUGJg54 s4n9QT823Q8dpi7azVDr/ewehiwGU2UnpOm+CmReGfQ1kYZBlEXTJtienwzkiw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1679899209; a=rsa-sha256; cv=none; b=FdFMdTV4LJt1ROOcEW5iuYrhQizlLS/ysSRM+FxJTIMCCTzzRvdmIJKdl7XAWVlYtz+nVL 0a7pPrss3pmKF6cNK1h6COTML+/SJTesuAp/ByWntapxK7eM8ob14/sZfuYbhrSLzpkhbA YjnUPJ5l3AR4mIp6Y2k98ITjY+T5E3gj2zI5/zlxmDoyY3Kci1gRjZfwYv7kL/G/8H7CYX Y7rQol1eZe1OEyIASWj5e9ZRCXY+VnWvifoMGRB23mZm1HNx3rswBgb96F124URGk4ne76 GSEBHeNSvZ77X1vsgtr0ur1GswtROfVU9HO72RYbF+9FOcY3dFtGsv7RQMMqYw== 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 4PlNV10Q1wz1BSD; Mon, 27 Mar 2023 06:40:09 +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 32R6e86d085321; Mon, 27 Mar 2023 06:40:08 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 32R6e8Q1085315; Mon, 27 Mar 2023 06:40:08 GMT (envelope-from git) Date: Mon, 27 Mar 2023 06:40:08 GMT Message-Id: <202303270640.32R6e8Q1085315@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: =?utf-8?Q?Corvin=20K=C3=B6hne?= Subject: git: 35bdc8b79e2a - stable/13 - bhyve: add helper for adding fwcfg files 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: corvink X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 35bdc8b79e2af6565520eea81cf6be431ed5fd3f Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by corvink: URL: https://cgit.FreeBSD.org/src/commit/?id=35bdc8b79e2af6565520eea81cf6be431ed5fd3f commit 35bdc8b79e2af6565520eea81cf6be431ed5fd3f Author: Corvin Köhne AuthorDate: 2021-08-11 08:04:36 +0000 Commit: Corvin Köhne CommitDate: 2023-03-27 06:32:59 +0000 bhyve: add helper for adding fwcfg files Fwcfg items without a fixed index are reported by the file_dir. They have an index of 0x20 and above. This helper simplifies the addition of such fwcfg items. It selects a new free index, assigns it to the fwcfg items and creates an proper entry in the file_dir. Reviewed by: markj MFC after: 1 week Sponsored by: Beckhoff Automation GmbH & Co. KG Differential Revision: https://reviews.freebsd.org/D38336 (cherry picked from commit 6f9ebb3d0fed2b0ae604dd6daf17f1fe1d8df216) --- usr.sbin/bhyve/qemu_fwcfg.c | 90 +++++++++++++++++++++++++++++++++++++++++++++ usr.sbin/bhyve/qemu_fwcfg.h | 2 + 2 files changed, 92 insertions(+) diff --git a/usr.sbin/bhyve/qemu_fwcfg.c b/usr.sbin/bhyve/qemu_fwcfg.c index e1a67fd0d8ef..5ee35d6764e6 100644 --- a/usr.sbin/bhyve/qemu_fwcfg.c +++ b/usr.sbin/bhyve/qemu_fwcfg.c @@ -43,6 +43,8 @@ #define QEMU_FWCFG_INDEX_ID 0x01 #define QEMU_FWCFG_INDEX_FILE_DIR 0x19 +#define QEMU_FWCFG_FIRST_FILE_INDEX 0x20 + #define QEMU_FWCFG_MIN_FILES 10 #pragma pack(1) @@ -87,6 +89,7 @@ struct qemu_fwcfg_softc { union qemu_fwcfg_selector selector; struct qemu_fwcfg_item items[QEMU_FWCFG_MAX_ARCHS] [QEMU_FWCFG_MAX_ENTRIES]; + struct qemu_fwcfg_directory *directory; }; static struct qemu_fwcfg_softc fwcfg_sc; @@ -256,6 +259,93 @@ qemu_fwcfg_register_port(const char *const name, const int port, const int size, return (register_inout(&iop)); } +int +qemu_fwcfg_add_file(const uint8_t name[QEMU_FWCFG_MAX_NAME], + const uint32_t size, void *const data) +{ + /* + * QEMU specifies count as big endian. + * Convert it to host endian to work with it. + */ + const uint32_t count = be32toh(fwcfg_sc.directory->be_count) + 1; + + /* add file to items list */ + const uint32_t index = QEMU_FWCFG_FIRST_FILE_INDEX + count - 1; + const int error = qemu_fwcfg_add_item(QEMU_FWCFG_ARCHITECTURE_GENERIC, + index, size, data); + if (error != 0) { + return (error); + } + + /* + * files should be sorted alphabetical, get index for new file + */ + uint32_t file_index; + for (file_index = 0; file_index < count - 1; ++file_index) { + if (strcmp(name, fwcfg_sc.directory->files[file_index].name) < + 0) + break; + } + + if (count > QEMU_FWCFG_MIN_FILES) { + /* alloc new file directory */ + const uint64_t new_size = sizeof(struct qemu_fwcfg_directory) + + count * sizeof(struct qemu_fwcfg_file); + struct qemu_fwcfg_directory *const new_directory = calloc(1, + new_size); + if (new_directory == NULL) { + warnx( + "%s: Unable to allocate a new qemu fwcfg files directory (count %d)", + __func__, count); + return (-ENOMEM); + } + + /* copy files below file_index to new directory */ + memcpy(new_directory->files, fwcfg_sc.directory->files, + file_index * sizeof(struct qemu_fwcfg_file)); + + /* copy files above file_index to directory */ + memcpy(&new_directory->files[file_index + 1], + &fwcfg_sc.directory->files[file_index], + (count - file_index) * sizeof(struct qemu_fwcfg_file)); + + /* free old directory */ + free(fwcfg_sc.directory); + + /* set directory pointer to new directory */ + fwcfg_sc.directory = new_directory; + + /* adjust directory pointer */ + fwcfg_sc.items[0][QEMU_FWCFG_INDEX_FILE_DIR].data = + (uint8_t *)fwcfg_sc.directory; + } else { + /* shift files behind file_index */ + for (uint32_t i = QEMU_FWCFG_MIN_FILES - 1; i > file_index; + --i) { + memcpy(&fwcfg_sc.directory->files[i], + &fwcfg_sc.directory->files[i - 1], + sizeof(struct qemu_fwcfg_file)); + } + } + + /* + * QEMU specifies count, size and index as big endian. + * Save these values in big endian to simplify guest reads of these + * values. + */ + fwcfg_sc.directory->be_count = htobe32(count); + fwcfg_sc.directory->files[file_index].be_size = htobe32(size); + fwcfg_sc.directory->files[file_index].be_selector = htobe16(index); + strcpy(fwcfg_sc.directory->files[file_index].name, name); + + /* set new size for the fwcfg_file_directory */ + fwcfg_sc.items[0][QEMU_FWCFG_INDEX_FILE_DIR].size = + sizeof(struct qemu_fwcfg_directory) + + count * sizeof(struct qemu_fwcfg_file); + + return (0); +} + int qemu_fwcfg_init(struct vmctx *const ctx) { diff --git a/usr.sbin/bhyve/qemu_fwcfg.h b/usr.sbin/bhyve/qemu_fwcfg.h index b5454fa9977d..f59087250816 100644 --- a/usr.sbin/bhyve/qemu_fwcfg.h +++ b/usr.sbin/bhyve/qemu_fwcfg.h @@ -18,4 +18,6 @@ struct qemu_fwcfg_item { uint8_t *data; }; +int qemu_fwcfg_add_file(const uint8_t name[QEMU_FWCFG_MAX_NAME], + const uint32_t size, void *const data); int qemu_fwcfg_init(struct vmctx *const ctx);