From patchwork Fri Apr 28 19:12:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evan Green X-Patchwork-Id: 68514 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C4DC63858C30 for ; Fri, 28 Apr 2023 19:12:23 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id 48BCA3858D37 for ; Fri, 28 Apr 2023 19:12:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 48BCA3858D37 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1a686260adcso3508165ad.0 for ; Fri, 28 Apr 2023 12:12:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1682709127; x=1685301127; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=iid7wyC+NKJIOVuTL+zRwYB6uJemyQvS3KT8X740c/0=; b=2YW+nox6Ns5HxLwCxGkdBgejS3zilJwpKalrWkXSJsb8f3XfSw0M7S0qrXsxdCZZTe tY+tgUy4piIRUb50XvpYrweIhLRd4RQ4hp7XKt2rqImX2DXReWxFNYjTNly6i/emn1A1 t/LZMlRdS9JwNzXPDtvEcRW6oUbSwW0eqIL+JwqVNUzdVeftGGrn2ql1/HqK/cXy6jJr 7/vBh8jh76fTC6NXgosqsqzW5S9klL6VdC+M6y1EoE++b8dBW6Q7tfpwiL5EyxesG2EB Kp7ks+dBvqNW4kscXdRDdrvCqPH3N3thR8JE3Gygp8uarLVJLQBKtMY0Ai0usGRWJKni eJJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682709127; x=1685301127; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=iid7wyC+NKJIOVuTL+zRwYB6uJemyQvS3KT8X740c/0=; b=eO4vX4UKzVWm20qrlaN+YoPZOZIltRxJ8xmZwCumLnd5ofmhtAu3Ifxr6jtEvqRe+n tzNv18zThU8B6V7wAldX377WrCfl63341hF8wukWlPMXlTq0fATC3GgSA6aaOSY8ETI4 8LuYDc1t78ybzFjgfSmCsllDVdO8gAGO81OqtUQJa0hVz4B5Vp4LrWzkMW9DkJZ889vC COtEeORYQSan6aWX9t63sF3qMndjJeGWtiGoVmE4b+3jgjuYn1u6xoI67vk1GdUvxuxc Bi0YUKAzB7c+EJhxX7u7Yk2xAmQfBTEgDmtuFpY0Szx/YctlkfFnGkx/29fbjbO2syVP mRgg== X-Gm-Message-State: AC+VfDw8wVFWsdTu1EcC1l/lfPgSTzJdLiEDV6IzIS8ZJH3yLVuM6sX2 MxcgMcwmr+X+/dKdEc4zscFAMRMF2nP7lZHsk9Q= X-Google-Smtp-Source: ACHHUZ5WyPSpLkAMW4kSEsCz62gxGcfAuf1l0gtoeUeaoCQF+SShnOwUyrFwJHmv94cuc12c3jQ02w== X-Received: by 2002:a17:902:d4c6:b0:1a6:71b1:a0b9 with SMTP id o6-20020a170902d4c600b001a671b1a0b9mr7346606plg.47.1682709126811; Fri, 28 Apr 2023 12:12:06 -0700 (PDT) Received: from evan.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id c24-20020a170902849800b001a50ae08284sm13503680plo.301.2023.04.28.12.12.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Apr 2023 12:12:06 -0700 (PDT) From: Evan Green To: libc-alpha@sourceware.org Cc: vineetg@rivosinc.com, palmer@rivosinc.com, Evan Green Subject: [PATCH] riscv: Add glibc-hwcaps support Date: Fri, 28 Apr 2023 12:12:00 -0700 Message-Id: <20230428191200.3241429-1-evan@rivosinc.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Status: No, score=-13.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Create a single subdirectory, rv64-v1, which defines a set of commonly implemented extensions in already shipping platforms: rv64gc_Zba_Zbb. This allows for easier shipping of optimized libraries for the most common subset of features above the baseline. For other architectures that use glibc-hwcaps, there's a well defined progression of architecture features, where each generation fully includes the previous generations and adds more. With RISC-V, we run the risk of this becoming a bitfield, which would get out of hand almost instantly. If we're careful in defining these generations, we can hopefully maintain a linear progression without devolving into a combinatorial mess. This patch deliberately aims low by defining a set of features that already exist in many shipping application processors. Signed-off-by: Evan Green --- Note that this series depends on the first two patches of my other series introducing the Linux hwprobe API [1]. The kernel series has been applied into Palmer's tree, so barring anything unforeseen I expect that to be fully landed shortly. It also depends on a Linux series I just posted upstream to expose the Zba and Zbb features through hwprobe [2]. [1] https://public-inbox.org/libc-alpha/20230407230711.2621614-1-evan@rivosinc.com/ [2] https://lore.kernel.org/lkml/20230428190609.3239486-1-evan@rivosinc.com/T/#t --- elf/Makefile | 2 +- elf/tst-glibc-hwcaps-cache.script | 3 ++ sysdeps/riscv/rv64/Makefile | 21 ++++++++ sysdeps/riscv/rv64/dl-hwcaps-subdirs.c | 49 +++++++++++++++++ sysdeps/riscv/rv64/tst-glibc-hwcaps.c | 58 +++++++++++++++++++++ sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h | 2 + 6 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 sysdeps/riscv/rv64/Makefile create mode 100644 sysdeps/riscv/rv64/dl-hwcaps-subdirs.c create mode 100644 sysdeps/riscv/rv64/tst-glibc-hwcaps.c diff --git a/elf/Makefile b/elf/Makefile index 396ec51424..f00061852b 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -2620,7 +2620,7 @@ $(objpfx)argv0test.out: tst-rtld-argv0.sh $(objpfx)ld.so \ # glibc-hwcaps mechanism for this architecture). Used to obtain test # coverage for some glibc-hwcaps tests for the widest possible range # of systems. -glibc-hwcaps-first-subdirs-for-tests = power9 x86-64-v2 z13 +glibc-hwcaps-first-subdirs-for-tests = power9 x86-64-v2 z13 rv-v1 # The test modules are parameterized by preprocessor macros. LDFLAGS-libmarkermod1-1.so += -Wl,-soname,libmarkermod1.so diff --git a/elf/tst-glibc-hwcaps-cache.script b/elf/tst-glibc-hwcaps-cache.script index d58fc8c5de..ea3d0186c9 100644 --- a/elf/tst-glibc-hwcaps-cache.script +++ b/elf/tst-glibc-hwcaps-cache.script @@ -36,3 +36,6 @@ mkdirp 0770 $L/glibc-hwcaps/x86-64-v4 cp $B/elf/libmarkermod4-2.so $L/glibc-hwcaps/x86-64-v2/libmarkermod4.so cp $B/elf/libmarkermod4-3.so $L/glibc-hwcaps/x86-64-v3/libmarkermod4.so cp $B/elf/libmarkermod4-4.so $L/glibc-hwcaps/x86-64-v4/libmarkermod4.so + +mkdirp 0770 $L/glibc-hwcaps/rv64-v1 +cp $B/elf/libmarkermod2-2.so $L/glibc-hwcaps/rv64-v1/libmarkermod2.so diff --git a/sysdeps/riscv/rv64/Makefile b/sysdeps/riscv/rv64/Makefile new file mode 100644 index 0000000000..15658ba26e --- /dev/null +++ b/sysdeps/riscv/rv64/Makefile @@ -0,0 +1,21 @@ + +ifeq ($(subdir),elf) + +$(objpfx)tst-glibc-hwcaps: \ + $(objpfx)libmarkermod2-1.so + +$(objpfx)tst-glibc-hwcaps.out: \ + $(objpfx)libmarkermod2.so \ + $(objpfx)glibc-hwcaps/rv64-v1/libmarkermod2.so \ + +$(objpfx)glibc-hwcaps/rv64-v1/libmarkermod2.so: $(objpfx)libmarkermod2-2.so + $(make-target-directory) + cp $< $@ + +ifeq (no,$(build-hardcoded-path-in-tests)) +# This is an ld.so.cache test, and RPATH/RUNPATH in the executable +# interferes with its test objectives. +tests-container += tst-glibc-hwcaps-cache +endif + +endif # $(subdir) == elf diff --git a/sysdeps/riscv/rv64/dl-hwcaps-subdirs.c b/sysdeps/riscv/rv64/dl-hwcaps-subdirs.c new file mode 100644 index 0000000000..2845e97eff --- /dev/null +++ b/sysdeps/riscv/rv64/dl-hwcaps-subdirs.c @@ -0,0 +1,49 @@ +/* Architecture-specific glibc-hwcaps subdirectories. RISC-V version. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +const char _dl_hwcaps_subdirs[] = "rv64-v1"; +enum { subdirs_count = 1 }; /* Number of components in _dl_hwcaps_subdirs. */ + +uint32_t +_dl_hwcaps_subdirs_active (void) +{ + int active = 0; + unsigned long int mask; + struct riscv_hwprobe pair[2]; + + /* Test in reverse preference order. */ + /* v1: RV64GC_Zba_Zbb */ + pair[0].key = RISCV_HWPROBE_KEY_BASE_BEHAVIOR; + pair[1].key = RISCV_HWPROBE_KEY_IMA_EXT_0; + if (__riscv_hwprobe(pair, 2, 0, NULL, 0) != 0) + return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); + + mask = RISCV_HWPROBE_IMA_FD | RISCV_HWPROBE_IMA_C | + RISCV_HWPROBE_EXT_ZBA | RISCV_HWPROBE_EXT_ZBB; + + if (!(pair[0].value & RISCV_HWPROBE_BASE_BEHAVIOR_IMA) || + ((pair[1].value & mask) != mask)) + return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); + ++active; + + return _dl_hwcaps_subdirs_build_bitmask (subdirs_count, active); +} diff --git a/sysdeps/riscv/rv64/tst-glibc-hwcaps.c b/sysdeps/riscv/rv64/tst-glibc-hwcaps.c new file mode 100644 index 0000000000..988d3492ef --- /dev/null +++ b/sysdeps/riscv/rv64/tst-glibc-hwcaps.c @@ -0,0 +1,58 @@ +/* glibc-hwcaps subdirectory test. RISC-V 64-bit version. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include + +extern int marker2 (void); + +/* Return the arch level, 10 for the baseline libmarkermod*.so's. */ +static int +compute_level (void) +{ + struct riscv_hwprobe pair[2]; + unsigned long int mask; + + pair[0].key = RISCV_HWPROBE_KEY_BASE_BEHAVIOR; + pair[1].key = RISCV_HWPROBE_KEY_IMA_EXT_0; + if (__riscv_hwprobe(pair, 2, 0, NULL, 0) != 0) + return 10; + + mask = RISCV_HWPROBE_IMA_FD | RISCV_HWPROBE_IMA_C | + RISCV_HWPROBE_EXT_ZBA | RISCV_HWPROBE_EXT_ZBB; + + if (!(pair[0].value & RISCV_HWPROBE_BASE_BEHAVIOR_IMA) || + ((pair[1].value & mask) != mask)) + return 10; + + return 11; +} + +static int +do_test (void) +{ + int level = compute_level (); + printf ("info: detected architecture level: arch%d\n", level); + TEST_COMPARE (marker2 (), MIN (level - 9, 2)); + return 0; +} + +#include diff --git a/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h b/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h index 49e27ee855..030939c0c6 100644 --- a/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h +++ b/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h @@ -46,6 +46,8 @@ struct riscv_hwprobe { #define RISCV_HWPROBE_KEY_IMA_EXT_0 4 #define RISCV_HWPROBE_IMA_FD (1 << 0) #define RISCV_HWPROBE_IMA_C (1 << 1) +#define RISCV_HWPROBE_EXT_ZBA (1 << 2) +#define RISCV_HWPROBE_EXT_ZBB (1 << 3) #define RISCV_HWPROBE_KEY_CPUPERF_0 5 #define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0) #define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0)