From nobody Mon May 08 08:25:30 2023 X-Original-To: dev-commits-src-branches@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 4QFDrB6JgBz49Z8H; Mon, 8 May 2023 08:25:30 +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 4QFDrB4NwKz3lDs; Mon, 8 May 2023 08:25:30 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1683534330; 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=ocVc9dUK2rN8dhPfS56RsW3xl34He6gw+TRYWsamAgc=; b=ud3YrVlSDwaELs0kGg/E0gfEDw6G9RTkBG6AHROx9upDr+YImMFTZ5tERtq+6krwzJFWju QBqt8roi8sskGhiSs8DKXtAHopoBynFDESidr5x2Ns5f72+GI+xq6RL5FiHIW3x3iv91td Wh6fT+G1JmMRWFaS5GSHxTeJj0TSuc05TZfnplyluhC9P8Vs00KGeNCILjSZWih0pSyu9w aZix955aBYzxviLzZl1cMWZ5ZAYICj9AJPIg7j78G+lav+50qyLuB3LxMKUAXNgMS9AemS gV79LZy2hPOzf1Pdz5C9q0FtLHDEHECJlmUyBbi8ZYs/XrqCYnRAKZFOJHEIgw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1683534330; 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=ocVc9dUK2rN8dhPfS56RsW3xl34He6gw+TRYWsamAgc=; b=wAsSiJt7Tnt+tSMqSDHvzdWFNoK/hb+QLOQptHLprhIkAFe1ixfsrM/mrazVALDMuvqSqt tNcWid7+5KHvPZVWNAfAb0foFSFhaXdUZVsfJATS2OKvB0EHE1bCjgSI/Y/9LxNMCjbe+L f2MWstXdG5gp6TXIg8boG6zym2s7Sz05DeXv7i44/m0Y9wiJRh7CdNxXebyKy2yKy30LJ/ GKdjJhJ+ppQdxNuQlFxpb1noffnIuH73dey8DuYBPbYqW13LlOUUoK/bvIPnyatCyn2LGd 3MNXhyCazliUYWqKUWG3Bnu0IcRsgpAV829n8psGCh7Wq5INNy74rGpl0LjvfA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1683534330; a=rsa-sha256; cv=none; b=tIj0ijyCjckbF1ApCYVMg/tfCHJ7zTrC5bcq4mQg9abxLJkWXKh5tIuQJ8VamCnOJsR135 taUySLB1du0zQkIX7/4dzwzEXlqQpzQoUyK2SnWqOe6UL8fU6QqB4ArVYpqAzvRWRvTLLy 0kgz+Yn9l3ktYFyPXuFXeo+yr1IBcXZxWQkB4cYdP3GPO+242pd7u7F7+8uXUyoGzeqUpn WCA2Yj1CAjrdr8OXXWvka2FCC/MPbZrdWnTIuEp+4bZ+eEQ1BHNkqC7kiwXy70/qfPuRIX XZR5as9H/zdC0qklDfTFlnMeOcbuIq2UpdKyVQU+5pnCnESfbTB/tjqeK2ottg== 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 4QFDrB29kMztJ1; Mon, 8 May 2023 08:25:30 +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 3488PUDv050411; Mon, 8 May 2023 08:25:30 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 3488PUep050410; Mon, 8 May 2023 08:25:30 GMT (envelope-from git) Date: Mon, 8 May 2023 08:25:30 GMT Message-Id: <202305080825.3488PUep050410@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: 0f8a17795d21 - stable/13 - bhyve: add allocation function to E820 List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@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: 0f8a17795d21bbd90eda7e17e98847adca011176 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by corvink: URL: https://cgit.FreeBSD.org/src/commit/?id=0f8a17795d21bbd90eda7e17e98847adca011176 commit 0f8a17795d21bbd90eda7e17e98847adca011176 Author: Corvin Köhne AuthorDate: 2021-09-09 09:37:03 +0000 Commit: Corvin Köhne CommitDate: 2023-05-08 08:21:31 +0000 bhyve: add allocation function to E820 This function makes it easy to allocate new E820 entries. It will be used to allocate graphics memory for Intel integrated graphic devices. Reviewed by: markj MFC after: 1 week Sponsored by: Beckhoff Automation GmbH & Co. KG Differential Revision: https://reviews.freebsd.org/D39547 (cherry picked from commit 5597f564870e94d56111dec638b8859423c936a9) --- usr.sbin/bhyve/e820.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++ usr.sbin/bhyve/e820.h | 16 ++++++++ 2 files changed, 120 insertions(+) diff --git a/usr.sbin/bhyve/e820.c b/usr.sbin/bhyve/e820.c index 95b83c056b9b..922381d032ce 100644 --- a/usr.sbin/bhyve/e820.c +++ b/usr.sbin/bhyve/e820.c @@ -20,6 +20,14 @@ #include "e820.h" #include "qemu_fwcfg.h" +/* + * E820 always uses 64 bit entries. Emulation code will use vm_paddr_t since it + * works on physical addresses. If vm_paddr_t is larger than uint64_t E820 can't + * hold all possible physical addresses and we can get into trouble. + */ +static_assert(sizeof(vm_paddr_t) <= sizeof(uint64_t), + "Unable to represent physical memory by E820 table"); + #define E820_FWCFG_FILE_NAME "etc/e820" #define KB (1024UL) @@ -282,6 +290,102 @@ e820_add_memory_hole(const uint64_t base, const uint64_t end) return (0); } +static uint64_t +e820_alloc_highest(const uint64_t max_address, const uint64_t length, + const uint64_t alignment, const enum e820_memory_type type) +{ + struct e820_element *element; + + TAILQ_FOREACH_REVERSE(element, &e820_table, e820_table, chain) { + uint64_t address, base, end; + + end = MIN(max_address, element->end); + base = roundup2(element->base, alignment); + + /* + * If end - length == 0, we would allocate memory at address 0. This + * address is mostly unusable and we should avoid allocating it. + * Therefore, search for another block in that case. + */ + if (element->type != E820_TYPE_MEMORY || end < base || + end - base < length || end - length == 0) { + continue; + } + + address = rounddown2(end - length, alignment); + + if (e820_add_entry(address, address + length, type) != 0) { + return (0); + } + + return (address); + } + + return (0); +} + +static uint64_t +e820_alloc_lowest(const uint64_t min_address, const uint64_t length, + const uint64_t alignment, const enum e820_memory_type type) +{ + struct e820_element *element; + + TAILQ_FOREACH(element, &e820_table, chain) { + uint64_t base, end; + + end = element->end; + base = MAX(min_address, roundup2(element->base, alignment)); + + /* + * If base == 0, we would allocate memory at address 0. This + * address is mostly unusable and we should avoid allocating it. + * Therefore, search for another block in that case. + */ + if (element->type != E820_TYPE_MEMORY || end < base || + end - base < length || base == 0) { + continue; + } + + if (e820_add_entry(base, base + length, type) != 0) { + return (0); + } + + return (base); + } + + return (0); +} + +uint64_t +e820_alloc(const uint64_t address, const uint64_t length, + const uint64_t alignment, const enum e820_memory_type type, + const enum e820_allocation_strategy strategy) +{ + assert(powerof2(alignment)); + assert((address & (alignment - 1)) == 0); + + switch (strategy) { + case E820_ALLOCATE_ANY: + /* + * Allocate any address. Therefore, ignore the address parameter + * and reuse the code path for allocating the lowest address. + */ + return (e820_alloc_lowest(0, length, alignment, type)); + case E820_ALLOCATE_LOWEST: + return (e820_alloc_lowest(address, length, alignment, type)); + case E820_ALLOCATE_HIGHEST: + return (e820_alloc_highest(address, length, alignment, type)); + case E820_ALLOCATE_SPECIFIC: + if (e820_add_entry(address, address + length, type) != 0) { + return (0); + } + + return (address); + } + + return (0); +} + int e820_init(struct vmctx *const ctx) { diff --git a/usr.sbin/bhyve/e820.h b/usr.sbin/bhyve/e820.h index 6843ad5dc736..8b8e23422e1f 100644 --- a/usr.sbin/bhyve/e820.h +++ b/usr.sbin/bhyve/e820.h @@ -18,11 +18,27 @@ enum e820_memory_type { E820_TYPE_NVS = 4 }; +enum e820_allocation_strategy { + /* allocate any address */ + E820_ALLOCATE_ANY, + /* allocate lowest address larger than address */ + E820_ALLOCATE_LOWEST, + /* allocate highest address lower than address */ + E820_ALLOCATE_HIGHEST, + /* allocate a specific address */ + E820_ALLOCATE_SPECIFIC +}; + struct e820_entry { uint64_t base; uint64_t length; uint32_t type; } __packed; +#define E820_ALIGNMENT_NONE 1 + +uint64_t e820_alloc(const uint64_t address, const uint64_t length, + const uint64_t alignment, const enum e820_memory_type type, + const enum e820_allocation_strategy strategy); struct qemu_fwcfg_item *e820_get_fwcfg_item(void); int e820_init(struct vmctx *const ctx);