From nobody Tue Nov 15 07:28:03 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 4NBHpC23wlz4hBfm; Tue, 15 Nov 2022 07:28:03 +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 4NBHpC1dZ2z3jQq; Tue, 15 Nov 2022 07:28:03 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1668497283; 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=nZ4zD52pSa0LA8qt4/sMZnjTklG+4hEVxUP0HsClNgk=; b=blGJpiZquTPgBy2uarnIw44aPSwsMMbIXMhHZPODkG//peYadywNSOlJDj1Fylu6xRRrrN Y7U4/atA9TnkRlMC5BrPe6yXVMznJzqEyg/5eYPIrsj5hQ/4U3gnToyhlKKG5dPz3xWlO8 nGxu+6DoluO8CWQqtJbJ2g1aZRDRSFgXjVtZ/RmLLjEWL9wlKXpmKsplI3H6NGgen1NvRM ToAtj0m6yQjdBIFI4nfmhQ4K5ZvZJcTFr5vpyBpFJRhE51ZR7IIHUm2crT6vUxxahfpYk2 ge3ou57wiEHG21qr+frFNTHIEjA5nXQSpoIC/SNwtSVbGCjFBHnGsAHJB46WLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1668497283; 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=nZ4zD52pSa0LA8qt4/sMZnjTklG+4hEVxUP0HsClNgk=; b=VMmZn7dEySTJ/jMG54HUeFa/19Dd/5KeXR0V9ctZ2FMh3holb14bLOJOlpWGNnmrEqGhXI gbQ2UzPCyzJK4teiGVNOhWu1O7mg7UaWqc56hCglrlr6xyHyot2ZvunRpdsXi0V8IsWut4 zIeY3vJyBkfOIYjHmeVa1657INzK6FAUeqpxNZRkOLYAqEvvf5xtP+pZxRHXqNZJyvffpw 1IS2tO5cWzvRRMg7CAiOPe5rabV8ftb2cNoi3EnohGX8SVlzSY525/I0s/XyiOXX4H4Bgb enJQSLxaJ5zbY/eJtHyBWtW1cjy+9nQ3UOpdp6xPFM2rbRLpW2mxRaI7YZnOjA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1668497283; a=rsa-sha256; cv=none; b=aLvWxbDHpqXZny0dB2nmFiH0kMg4C75jwqBIBZM2P/hKY54maGdguSlKEuQeiuFL9BprLY r2nbXSmJKd9TcXLYCmTRrl1awug4QdKwn2erogaVOiWBGloLntpLF2WXhKr7W5MNDHYu9x 807nnvNjDEGdGwTOWOL8z4tHQ00PQ1U9VOx+DzgAcxSqtCTDT2d2R4Jy8S3qLm4L2F6uSG RGf5mb71gDPCMRbaIwbpxWCgOLJW1BwlGJAby+1iAUEmRDVtkup4dW08ZhV2G4OTCFeQ7w EF66JN7m3jrsan1qN9WKw1iaoZGTnp/1zAV6aG8EDvBxGGjyge3LoiUXq4aSZQ== 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 4NBHpC0lG5z1Cft; Tue, 15 Nov 2022 07:28:03 +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 2AF7S3sB046633; Tue, 15 Nov 2022 07:28:03 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2AF7S3Pi046632; Tue, 15 Nov 2022 07:28:03 GMT (envelope-from git) Date: Tue, 15 Nov 2022 07:28:03 GMT Message-Id: <202211150728.2AF7S3Pi046632@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: =?utf-8?Q?Corvin=20K=C3=B6hne?= Subject: git: 21bbc28426d6 - main - bhyve: add basic basl implementation 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/main X-Git-Reftype: branch X-Git-Commit: 21bbc28426d6b88adfdd8c66d0bc936652e0191d Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by corvink: URL: https://cgit.FreeBSD.org/src/commit/?id=21bbc28426d6b88adfdd8c66d0bc936652e0191d commit 21bbc28426d6b88adfdd8c66d0bc936652e0191d Author: Corvin Köhne AuthorDate: 2022-11-04 11:24:49 +0000 Commit: Corvin Köhne CommitDate: 2022-11-15 07:27:01 +0000 bhyve: add basic basl implementation Basl is the bhyve ASL compiler. At the moment, it's just a small wrapper to call iasl, the Intel ASL compiler. As bhyve will gain support for qemu's ACPI table loader in the future, it has to create ACPI tables on it's own. Therefore, it makes sense to create a new file which keeps the code for basl. This first implementation of basl supports creating an ACPI table by appending raw bytes to it. It's also capable of loading all tables into guest memory. Reviewed by: jhb, markj (older version) Approved by: manu (mentor) MFC after: 2 weeks Sponsored by: Beckhoff Automation GmbH & Co. KG Differential Revision: https://reviews.freebsd.org/D36984 --- usr.sbin/bhyve/Makefile | 1 + usr.sbin/bhyve/acpi.c | 2 +- usr.sbin/bhyve/basl.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++ usr.sbin/bhyve/basl.h | 36 +++++++++++++ 4 files changed, 178 insertions(+), 1 deletion(-) diff --git a/usr.sbin/bhyve/Makefile b/usr.sbin/bhyve/Makefile index 185568d3ea56..1f2e1031bf8a 100644 --- a/usr.sbin/bhyve/Makefile +++ b/usr.sbin/bhyve/Makefile @@ -18,6 +18,7 @@ SRCS= \ atkbdc.c \ acpi.c \ audio.c \ + basl.c \ bhyvegc.c \ bhyverun.c \ block_if.c \ diff --git a/usr.sbin/bhyve/acpi.c b/usr.sbin/bhyve/acpi.c index 695354b2ac0e..55c8c9cb35fc 100644 --- a/usr.sbin/bhyve/acpi.c +++ b/usr.sbin/bhyve/acpi.c @@ -73,6 +73,7 @@ __FBSDID("$FreeBSD$"); #include "bhyverun.h" #include "acpi.h" +#include "basl.h" #include "pci_emul.h" #include "vmgenc.h" @@ -80,7 +81,6 @@ __FBSDID("$FreeBSD$"); * Define the base address of the ACPI tables, the sizes of some tables, * and the offsets to the individual tables, */ -#define BHYVE_ACPI_BASE 0xf2400 #define RSDT_OFFSET 0x040 #define XSDT_OFFSET 0x080 #define MADT_OFFSET 0x100 diff --git a/usr.sbin/bhyve/basl.c b/usr.sbin/bhyve/basl.c new file mode 100644 index 000000000000..f24d8c009110 --- /dev/null +++ b/usr.sbin/bhyve/basl.c @@ -0,0 +1,140 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2022 Beckhoff Automation GmbH & Co. KG + */ + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +#include "basl.h" + +struct basl_table { + STAILQ_ENTRY(basl_table) chain; + struct vmctx *ctx; + uint8_t fwcfg_name[QEMU_FWCFG_MAX_NAME]; + void *data; + uint32_t len; + uint32_t off; + uint32_t alignment; +}; +static STAILQ_HEAD(basl_table_list, basl_table) basl_tables = STAILQ_HEAD_INITIALIZER( + basl_tables); + +static int +basl_finish_install_guest_tables(struct basl_table *const table) +{ + void *gva; + + /* + * Install ACPI tables directly in guest memory for use by guests which + * do not boot via EFI. EFI ROMs provide a pointer to the firmware + * generated ACPI tables instead, but it doesn't hurt to install the + * tables always. + */ + gva = vm_map_gpa(table->ctx, BHYVE_ACPI_BASE + table->off, table->len); + if (gva == NULL) { + warnx("%s: could not map gpa [ 0x%16lx, 0x%16lx ]", __func__, + (uint64_t)BHYVE_ACPI_BASE + table->off, + (uint64_t)BHYVE_ACPI_BASE + table->off + table->len); + return (ENOMEM); + } + memcpy(gva, table->data, table->len); + + return (0); +} + +int +basl_finish(void) +{ + struct basl_table *table; + + if (STAILQ_EMPTY(&basl_tables)) { + warnx("%s: no ACPI tables found", __func__); + return (EINVAL); + } + + STAILQ_FOREACH(table, &basl_tables, chain) { + BASL_EXEC(basl_finish_install_guest_tables(table)); + } + + return (0); +} + +int +basl_init(void) +{ + return (0); +} + +int +basl_table_append_bytes(struct basl_table *const table, const void *const bytes, + const uint32_t len) +{ + void *end; + + assert(table != NULL); + assert(bytes != NULL); + + if (table->len + len <= table->len) { + warnx("%s: table too large (table->len 0x%8x len 0x%8x)", + __func__, table->len, len); + return (EFAULT); + } + + table->data = reallocf(table->data, table->len + len); + if (table->data == NULL) { + warnx("%s: failed to realloc table to length 0x%8x", __func__, + table->len + len); + table->len = 0; + return (ENOMEM); + } + + end = (uint8_t *)table->data + table->len; + table->len += len; + + memcpy(end, bytes, len); + + return (0); +} + +int +basl_table_create(struct basl_table **const table, struct vmctx *ctx, + const uint8_t *const name, const uint32_t alignment, + const uint32_t off) +{ + struct basl_table *new_table; + + assert(table != NULL); + + new_table = calloc(1, sizeof(struct basl_table)); + if (new_table == NULL) { + warnx("%s: failed to allocate table", __func__); + return (ENOMEM); + } + + new_table->ctx = ctx; + + snprintf(new_table->fwcfg_name, sizeof(new_table->fwcfg_name), + "etc/acpi/%s", name); + + new_table->alignment = alignment; + new_table->off = off; + + STAILQ_INSERT_TAIL(&basl_tables, new_table, chain); + + *table = new_table; + + return (0); +} diff --git a/usr.sbin/bhyve/basl.h b/usr.sbin/bhyve/basl.h new file mode 100644 index 000000000000..f6809a473ff4 --- /dev/null +++ b/usr.sbin/bhyve/basl.h @@ -0,0 +1,36 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2022 Beckhoff Automation GmbH & Co. KG + */ + +#pragma once + +#include + +#define BHYVE_ACPI_BASE 0xf2400 + +#define BASL_TABLE_ALIGNMENT 0x10 +#define BASL_TABLE_ALIGNMENT_FACS 0x40 + +#define BASL_EXEC(x) \ + do { \ + const int error = (x); \ + if (error) { \ + warnc(error, \ + "BASL failed @ %s:%d\n Failed to execute %s", \ + __func__, __LINE__, #x); \ + return (error); \ + } \ + } while (0) + +#define QEMU_FWCFG_MAX_NAME 56 + +struct basl_table; + +int basl_finish(void); +int basl_init(void); +int basl_table_append_bytes(struct basl_table *table, const void *bytes, + uint32_t len); +int basl_table_create(struct basl_table **table, struct vmctx *ctx, + const uint8_t *name, uint32_t alignment, uint32_t off);