From nobody Tue May 03 14:04:40 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 045101AB9D64; Tue, 3 May 2022 14:04:41 +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 4Kt1tJ3LMQz3RDC; Tue, 3 May 2022 14:04:40 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1651586680; 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=CA6JIWIefuJlMZB0Bvd5jx0SovcNZStqC8QBTi3WwSw=; b=qoqLB26oXdlTr4jPdcQlKhNbdVSX2pLS02DD5i4YNa6/kDhCLALEbpmS0Fj9f7CSs4zjJa poWQh/w6+OXbC+KLtrGFHrUQffptzs5fSI919Dw/JHbJWNyCa/9Hec4dBPUa2sTPbNkwe5 +377EKzzgn8gXNp+4Nvp4GRV7Yz4D899E84eAVsTLpGhIdT+0vXb+wF4zUmIhCfjZA/9Xa GqxkEfdS8NQiZMPRhB2ZKprLMSPZkieWrc126dDlBWXkRNn6JHMXNwIbamUC+wRiI/DN1q YDJ/ITIAUA+WsKZpFHRl8c4IU1oaYDkykVqgXuzxlxE68dMVJod1XKtcjD90Uw== 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 4B4111F9C4; Tue, 3 May 2022 14:04:40 +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 243E4eHe077826; Tue, 3 May 2022 14:04:40 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 243E4eqj077825; Tue, 3 May 2022 14:04:40 GMT (envelope-from git) Date: Tue, 3 May 2022 14:04:40 GMT Message-Id: <202205031404.243E4eqj077825@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Andrew Turner Subject: git: 0edb26c3bad0 - stable/13 - Port subr_physmem to userspace and add tests 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: andrew X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 0edb26c3bad0419d1107f78cab834e165daa39fa Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1651586680; 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=CA6JIWIefuJlMZB0Bvd5jx0SovcNZStqC8QBTi3WwSw=; b=jCGwGv6ONrRjDBde7u45rBlacVnKHHSmquT1sCILlMc+TfYlkfaW8i17uFl1ry0FK7L6EH GeqGPPmTqxece0aqkD+wYim6su8U6DOug1CflfpGIIBPnv65BeOOzbeUQ5X8vqr2SwPai/ U9JGDHGFi8iGvUtO9AwBfTEk1gX4SKYttOd5ajknHdibp7QRBwwIgu8z9HX7RaKRwEKHLQ Ij2SvWE19FPAPXqqXh2Mo9CTD1UeNH43H+T7+9I05b4wJS5r1Mj27q2kwtVnlPQGCL/PYW W2vkZtxsnmqtVxMCvPqrHZCrgwzAW1IQ46qRpFsJh/IQebgtZVIbDGR34ueyPQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1651586680; a=rsa-sha256; cv=none; b=bJ17Ny4tJIRemGP8Y+NLXiaJa/KAO6Hbr3u2AqjBBvo9huKEPkPeqDRcZwA17mrVVL2XWz vXlcbdZZi7ExWNo9rz17uGSp4DpMam7dHE+TWkacbGrDEXrgF8xcuopZDltRGOY2EVpR0L OsheWdv7dNsshDppD3MRauD8Fntf+C9J+APalPYmCr41IU6uqxehMuqi/44fAacWV/CVXw tLk7cVQge6367qrTdUJOMlIaw35ulWZh3Dxv2E2KcMxsyiSJ7fssYD+SnB8BC/HoFuWv5K GlMXcZe4/MbM68sRMaETSGgrymAvV1KTMdt8wv04fexWZCjEsZ/JEP9EahSzNQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by andrew: URL: https://cgit.FreeBSD.org/src/commit/?id=0edb26c3bad0419d1107f78cab834e165daa39fa commit 0edb26c3bad0419d1107f78cab834e165daa39fa Author: Andrew Turner AuthorDate: 2022-03-28 10:20:29 +0000 Commit: Andrew Turner CommitDate: 2022-05-03 14:04:04 +0000 Port subr_physmem to userspace and add tests These give us some confidience we haven't broken anything in early boot code that may be running before the console. Reviewed by: emaste Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D34691 (cherry picked from commit 8c99dfed541263bd51cae943684bcc13768c2a36) Disable the physmem test for now It fails to build on at least i386 (cherry picked from commit d8819d88af529974bac1fe49b713dacf1f3d7239) Enable subr_physmem_test on supported architectures Only build where it's supported. While here add support for amd64 to help with testing. Sponsored by: The FreeBSD Foundation (cherry picked from commit 41e6d2091ce5653d4ace3f0b9cbde1375d458c67) --- sys/kern/subr_physmem.c | 27 +++++++- tests/sys/kern/Makefile | 6 ++ tests/sys/kern/subr_physmem_test.c | 122 +++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+), 1 deletion(-) diff --git a/sys/kern/subr_physmem.c b/sys/kern/subr_physmem.c index a4b865bed56d..106200dd814e 100644 --- a/sys/kern/subr_physmem.c +++ b/sys/kern/subr_physmem.c @@ -29,8 +29,10 @@ #include __FBSDID("$FreeBSD$"); +#ifdef _KERNEL #include "opt_acpi.h" #include "opt_ddb.h" +#endif /* * Routines for describing and initializing anything related to physical memory. @@ -40,12 +42,19 @@ __FBSDID("$FreeBSD$"); #include #include #include + +#ifdef _KERNEL #include #include #include #include #include #include +#else +#include +#include +#include +#endif /* * These structures are used internally to keep track of regions of physical @@ -64,7 +73,7 @@ __FBSDID("$FreeBSD$"); #if defined(__arm__) #define MAX_PHYS_ADDR 0xFFFFFFFFull -#elif defined(__aarch64__) || defined(__riscv) +#elif defined(__aarch64__) || defined(__amd64__) || defined(__riscv) #define MAX_PHYS_ADDR 0xFFFFFFFFFFFFFFFFull #endif @@ -87,6 +96,20 @@ static size_t excnt; long realmem; long Maxmem; +#ifndef _KERNEL +static void +panic(const char *fmt, ...) +{ + va_list va; + + va_start(va, fmt); + vfprintf(stderr, fmt, va); + fprintf(stderr, "\n"); + va_end(va); + __builtin_trap(); +} +#endif + /* * Print the contents of the physical and excluded region tables using the * provided printf-like output function (which will be either printf or @@ -389,6 +412,7 @@ physmem_avail(vm_paddr_t *avail, size_t maxavail) return (regions_to_avail(avail, EXFLAG_NOALLOC, maxavail, 0, NULL, NULL)); } +#ifdef _KERNEL /* * Process all the regions added earlier into the global avail lists. * @@ -415,6 +439,7 @@ physmem_init_kernel_globals(void) panic("No memory entries in phys_avail"); Maxmem = atop(phys_avail[nextidx - 1]); } +#endif #ifdef DDB #include diff --git a/tests/sys/kern/Makefile b/tests/sys/kern/Makefile index fe23803c2267..77e514e9e747 100644 --- a/tests/sys/kern/Makefile +++ b/tests/sys/kern/Makefile @@ -22,6 +22,9 @@ ATF_TESTS_C+= ptrace_test TEST_METADATA.ptrace_test+= timeout="15" ATF_TESTS_C+= reaper ATF_TESTS_C+= sigaltstack +.if ${MACHINE_ARCH} != "i386" && ${MACHINE_ARCH:Mpowerpc*} == "" +ATF_TESTS_C+= subr_physmem_test +.endif PLAIN_TESTS_C+= subr_unit_test ATF_TESTS_C+= sysctl_kern_proc ATF_TESTS_C+= sys_getrandom @@ -77,6 +80,9 @@ SRCS.libkern_crc32+= crc32_sse42.c SRCS.libkern_crc32+= crc32c_armv8.S .endif +CFLAGS.subr_physmem.c+= -D_WANT_FREEBSD_BITSET +SRCS.subr_physmem_test+= subr_physmem_test.c subr_physmem.c + # subr_unit.c contains functions whose prototypes lie in headers that cannot be # included in userland. But as far as subr_unit_test goes, they're effectively # static. So it's ok to disable -Wmissing-prototypes for this program. diff --git a/tests/sys/kern/subr_physmem_test.c b/tests/sys/kern/subr_physmem_test.c new file mode 100644 index 000000000000..a562bacec65c --- /dev/null +++ b/tests/sys/kern/subr_physmem_test.c @@ -0,0 +1,122 @@ +/*- + * Copyright (c) 2021 The FreeBSD Foundation + * All rights reserved. + * + * This software was developed by Andrew Turner under + * sponsorship from the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include + +#include + +ATF_TC_WITHOUT_HEAD(hwregion); +ATF_TC_BODY(hwregion, tc) +{ + vm_paddr_t avail[4]; + size_t len; + + physmem_hardware_region(2 * PAGE_SIZE, PAGE_SIZE); + + len = physmem_avail(avail, 4); + ATF_CHECK_EQ(len, 2); + ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE); + ATF_CHECK_EQ(avail[1], 3 * PAGE_SIZE); + + /* Add an overlap */ + physmem_hardware_region(2 * PAGE_SIZE, 2 * PAGE_SIZE); + len = physmem_avail(avail, 4); + ATF_CHECK_EQ(len, 2); + ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE); + ATF_CHECK_EQ(avail[1], 4 * PAGE_SIZE); + + /* Add a touching region */ + physmem_hardware_region(4 * PAGE_SIZE, PAGE_SIZE); + len = physmem_avail(avail, 4); + ATF_CHECK_EQ(len, 2); + ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE); + ATF_CHECK_EQ(avail[1], 5 * PAGE_SIZE); + + /* Add a partial overlap */ + physmem_hardware_region(4 * PAGE_SIZE, 2 * PAGE_SIZE); + len = physmem_avail(avail, 4); + ATF_CHECK_EQ(len, 2); + ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE); + ATF_CHECK_EQ(avail[1], 6 * PAGE_SIZE); + + /* Add a non-page aligned section */ + physmem_hardware_region(6 * PAGE_SIZE, PAGE_SIZE / 2); + len = physmem_avail(avail, 4); + ATF_CHECK_EQ(len, 2); + ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE); + ATF_CHECK_EQ(avail[1], 6 * PAGE_SIZE); + +#ifdef notyet /* This doesn't currently work */ + /* Add the remaining part of the page */ + physmem_hardware_region(6 * PAGE_SIZE + PAGE_SIZE / 2, PAGE_SIZE / 2); + len = physmem_avail(avail, 4); + ATF_CHECK_EQ(len, 2); + ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE); + ATF_CHECK_EQ(avail[1], 7 * PAGE_SIZE); +#endif +} + +ATF_TC_WITHOUT_HEAD(hwregion_exclude); +ATF_TC_BODY(hwregion_exclude, tc) +{ + vm_paddr_t avail[6]; + size_t len; + + physmem_hardware_region(2 * PAGE_SIZE, 5 * PAGE_SIZE); + physmem_exclude_region(4 * PAGE_SIZE, PAGE_SIZE, EXFLAG_NOALLOC); + + len = physmem_avail(avail, 6); + ATF_CHECK_EQ(len, 4); + ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE); + ATF_CHECK_EQ(avail[1], 4 * PAGE_SIZE); + ATF_CHECK_EQ(avail[2], 5 * PAGE_SIZE); + ATF_CHECK_EQ(avail[3], 7 * PAGE_SIZE); + + /* Check mis-aligned/sized excluded regions work */ + physmem_exclude_region(4 * PAGE_SIZE - 1, PAGE_SIZE + 2, + EXFLAG_NOALLOC); + len = physmem_avail(avail, 6); + ATF_CHECK_EQ(len, 4); + ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE); + ATF_CHECK_EQ(avail[1], 3 * PAGE_SIZE); + ATF_CHECK_EQ(avail[2], 6 * PAGE_SIZE); + ATF_CHECK_EQ(avail[3], 7 * PAGE_SIZE); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, hwregion); + ATF_TP_ADD_TC(tp, hwregion_exclude); + return (atf_no_error()); +}