From patchwork Wed Jan 10 20:51:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evan Green X-Patchwork-Id: 83785 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 E08073861804 for ; Wed, 10 Jan 2024 20:52:34 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-il1-x132.google.com (mail-il1-x132.google.com [IPv6:2607:f8b0:4864:20::132]) by sourceware.org (Postfix) with ESMTPS id 7B191385E839 for ; Wed, 10 Jan 2024 20:51:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7B191385E839 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7B191385E839 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::132 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704919916; cv=none; b=YvxgMvtdkRKnjPaKkC4OVO6Q89x6gmqtHwStwR22gHU9+zLnoSPGve3vmPDA/SldnsUCwIVVB/T0HeHBpq37nVQIWunm3nvJpfRiNQ+EQ7skOLY0NjB8hm22pSXprKQoAOTsIqMNPrdc+u46FkgkgmeBR4pBkSet2+S72zTFucA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704919916; c=relaxed/simple; bh=XBrMHYpNp61wKULEJZ7L+vf1ymLkv4yebPHUQkl6E9M=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=KlsifnX6rL9RegAHDGqY2vysdnzA8y7MTiw/BBB4g/pzbUl15qxP4idgUrPK72cmeWfXyVkK/8+tzZBe0/WSKEu8i6RCTH+8B2TgqnMxzH1lal1kR8J0xShhfB+AHsdPv1YcQP0E/8nyYRXBY1ITDX5rQ4wdRV1FkFPewsnQ9Zk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x132.google.com with SMTP id e9e14a558f8ab-36082f3cb06so23836955ab.1 for ; Wed, 10 Jan 2024 12:51:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1704919912; x=1705524712; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7xk4633Y/geS9ZSTE6zCWoIa7CtUtbUIEgmjWx+0EvY=; b=Fw98YiACuXJS0E1VzrwXHj/OfgJbJiTDCsT4lruxrNq2g3+6kUNMcvgErJy0ixja8Z Dk8VgpoWIC0xOUw6hYbGrW43nkOMHRX26hYLz8Kp5HJLwqgPCTVNQr7b5A6DypmmXQCL eIjjZb+6Sd/QstSbw8xyT9Nq7nVE2yYMyEKybbgSAvRzVxgSPlWBzcrKHVKKtOjY7RCi RRphvz9JLIYQw0AM/AtDZ8GwBXH8xRXKe9pw46A+5brUkKis4RI5aQQ7Hel2MJOy9UoW eYMri8ZBpjRX9YHAiMB2ps4qauKBbwlVbxcySIu/GcxcGbN9h+Eja5Ovyfk/K6STYSYJ TZkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704919912; x=1705524712; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7xk4633Y/geS9ZSTE6zCWoIa7CtUtbUIEgmjWx+0EvY=; b=uBcKVie9gmJCdBT+2/jIU+XKwczCscYZeezhhDZRQ2C0UFaCXNpDUEaKKhxrm6cumn 9aB/VrxDaD9Ns2vTmpOdbRx4ZL8kJFAztYcq9Y6QeaL7gowC9219sO7938Yx+2qC8fEx SvuJRnz8CpezfzCTswUDLNNDzZRhAEwyhAqxI0pizJ/4XfaZYpQW/DlG84jHUvpCftjc iConFm6IgzPj1ays4BVidiWF/AEcjioP58lzVNWZsmOA8fMT3FXISGcusjH7lUnFw+iJ 4NkO/NJFMQsZkojxqMAt335akD5OkuW/jFuy6PvVOv09BzgtBLb2kUU9lVdyqpp3fQ9C aqBw== X-Gm-Message-State: AOJu0YyJBakdLIgN8nShUxpopC98d/dJAk1I4NDP2f0jWS20rvJbW1jK imvSH0+OO79zGTUO+vGOQbeqeS+ckM4FWQr6OFkYlALaKbk= X-Google-Smtp-Source: AGHT+IGHieuzQ37RfOC/CyEZy0dMdx3de1/1Mqof2yNTX5O6Ic3YciFnsTplaod4SOW9xuMVzW+Fmg== X-Received: by 2002:a05:6e02:1ca6:b0:360:780c:d2a2 with SMTP id x6-20020a056e021ca600b00360780cd2a2mr163345ill.30.1704919911779; Wed, 10 Jan 2024 12:51:51 -0800 (PST) Received: from evan.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id y4-20020a92c984000000b0035ff9825323sm1452018iln.30.2024.01.10.12.51.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 12:51:51 -0800 (PST) From: Evan Green To: libc-alpha@sourceware.org Cc: palmer@rivosinc.com, vineetg@rivosinc.com, slewis@rivosinc.com, Florian Weimer , Evan Green Subject: [PATCH v11 1/7] riscv: Add Linux hwprobe syscall support Date: Wed, 10 Jan 2024 12:51:34 -0800 Message-Id: <20240110205140.754849-2-evan@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240110205140.754849-1-evan@rivosinc.com> References: <20240110205140.754849-1-evan@rivosinc.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.2 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.30 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 Add awareness and a thin wrapper function around a new Linux system call that allows callers to get architecture and microarchitecture information about the CPUs from the kernel. This can be used to do things like dynamically choose a memcpy implementation. Signed-off-by: Evan Green --- Changes in v11: - Update copyright year (Adhemerval) Changes in v10: - Remove spurious 5 from syscall patch (Adhemerval) - Use one item per line in Makefile (Adhemerval) - Remove double underscores from __riscv_hwprobe definition (Adhemerval) - Use only spaces in macro definitions of hwprobe.h (Adhemerval) Changes in v9: - Alphabetize new entries in libc.abilist (to pass checks) Changes in v8: - Fix missed 2.39 in abilists (Joseph) - Just return -r (Florian) Changes in v7: - Bumped Versions up to 2.39 (Joseph) - Used INTERNAL_SYSCALL_CALL, and return positive errno to match pthreads API (Florian). - Remove __THROW since it creates a warning in combination with the fortified access decorators. Changes in v6: - Prefixed __riscv_hwprobe() parameters names with __ to avoid user macro namespace pollution (Joseph) Changes in v4: - Remove __USE_GNU (Florian) - __nonnull, __wur, __THROW, and __fortified_attr_access decorations (Florian) - change long to long int (Florian) - Fix comment formatting (Florian) - Update backup kernel header content copy. - Fix function declaration formatting (Florian) - Changed export versions to 2.38 Changes in v3: - Update argument types to match v4 kernel interface Changes in v2: - hwprobe.h: Use __has_include and duplicate Linux content to make compilation work when Linux headers are absent (Adhemerval) - hwprobe.h: Put declaration under __USE_GNU (Adhemerval) - Use INLINE_SYSCALL_CALL (Adhemerval) - Update versions - Update UNALIGNED_MASK to match kernel v3 series. sysdeps/unix/sysv/linux/riscv/Makefile | 12 ++- sysdeps/unix/sysv/linux/riscv/Versions | 3 + sysdeps/unix/sysv/linux/riscv/hwprobe.c | 36 +++++++++ .../unix/sysv/linux/riscv/rv32/libc.abilist | 1 + .../unix/sysv/linux/riscv/rv64/libc.abilist | 1 + sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h | 74 +++++++++++++++++++ 6 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 sysdeps/unix/sysv/linux/riscv/hwprobe.c create mode 100644 sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h diff --git a/sysdeps/unix/sysv/linux/riscv/Makefile b/sysdeps/unix/sysv/linux/riscv/Makefile index 4b6eacb32f..04abf226ad 100644 --- a/sysdeps/unix/sysv/linux/riscv/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/Makefile @@ -1,6 +1,14 @@ ifeq ($(subdir),misc) -sysdep_headers += sys/cachectl.h -sysdep_routines += flush-icache +sysdep_headers += \ + sys/cachectl.h \ + sys/hwprobe.h \ + # sysdep_headers + +sysdep_routines += \ + flush-icache \ + hwprobe \ + # sysdep_routines + endif ifeq ($(subdir),stdlib) diff --git a/sysdeps/unix/sysv/linux/riscv/Versions b/sysdeps/unix/sysv/linux/riscv/Versions index 5625d2a0b8..8717b62a4a 100644 --- a/sysdeps/unix/sysv/linux/riscv/Versions +++ b/sysdeps/unix/sysv/linux/riscv/Versions @@ -8,4 +8,7 @@ libc { GLIBC_2.27 { __riscv_flush_icache; } + GLIBC_2.39 { + __riscv_hwprobe; + } } diff --git a/sysdeps/unix/sysv/linux/riscv/hwprobe.c b/sysdeps/unix/sysv/linux/riscv/hwprobe.c new file mode 100644 index 0000000000..28a448175b --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/hwprobe.c @@ -0,0 +1,36 @@ +/* RISC-V hardware feature probing support on Linux + Copyright (C) 2024 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 + +int __riscv_hwprobe (struct riscv_hwprobe *pairs, size_t pair_count, + size_t cpu_count, unsigned long int *cpus, + unsigned int flags) +{ + int r; + + r = INTERNAL_SYSCALL_CALL (riscv_hwprobe, pairs, pair_count, + cpu_count, cpus, flags); + + /* Negate negative errno values to match pthreads API. */ + return -r; +} diff --git a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist index f90c94bc35..5794869589 100644 --- a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist +++ b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist @@ -2436,6 +2436,7 @@ GLIBC_2.38 strlcat F GLIBC_2.38 strlcpy F GLIBC_2.38 wcslcat F GLIBC_2.38 wcslcpy F +GLIBC_2.39 __riscv_hwprobe F GLIBC_2.39 pidfd_getpid F GLIBC_2.39 pidfd_spawn F GLIBC_2.39 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist index e04ff93bd2..d27a5e6744 100644 --- a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist +++ b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist @@ -2636,6 +2636,7 @@ GLIBC_2.38 strlcat F GLIBC_2.38 strlcpy F GLIBC_2.38 wcslcat F GLIBC_2.38 wcslcpy F +GLIBC_2.39 __riscv_hwprobe F GLIBC_2.39 pidfd_getpid F GLIBC_2.39 pidfd_spawn F GLIBC_2.39 pidfd_spawnp F diff --git a/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h b/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h new file mode 100644 index 0000000000..5592b9e100 --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h @@ -0,0 +1,74 @@ +/* RISC-V architecture probe interface + Copyright (C) 2024 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 + . */ + +#ifndef _SYS_HWPROBE_H +#define _SYS_HWPROBE_H 1 + +#include +#include +#ifdef __has_include +# if __has_include () +# include +# endif +#endif + +/* Define a (probably stale) version of the interface if the Linux headers + aren't present. */ +#ifndef RISCV_HWPROBE_KEY_MVENDORID +struct riscv_hwprobe { + signed long long int key; + unsigned long long int value; +}; + +#define RISCV_HWPROBE_KEY_MVENDORID 0 +#define RISCV_HWPROBE_KEY_MARCHID 1 +#define RISCV_HWPROBE_KEY_MIMPID 2 +#define RISCV_HWPROBE_KEY_BASE_BEHAVIOR 3 +#define RISCV_HWPROBE_BASE_BEHAVIOR_IMA (1 << 0) +#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_IMA_V (1 << 2) +#define RISCV_HWPROBE_EXT_ZBA (1 << 3) +#define RISCV_HWPROBE_EXT_ZBB (1 << 4) +#define RISCV_HWPROBE_EXT_ZBS (1 << 5) +#define RISCV_HWPROBE_EXT_ZICBOZ (1 << 6) +#define RISCV_HWPROBE_KEY_CPUPERF_0 5 +#define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0) +#define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0) +#define RISCV_HWPROBE_MISALIGNED_SLOW (2 << 0) +#define RISCV_HWPROBE_MISALIGNED_FAST (3 << 0) +#define RISCV_HWPROBE_MISALIGNED_UNSUPPORTED (4 << 0) +#define RISCV_HWPROBE_MISALIGNED_MASK (7 << 0) +#define RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE 6 + +#endif /* RISCV_HWPROBE_KEY_MVENDORID */ + +__BEGIN_DECLS + +extern int __riscv_hwprobe (struct riscv_hwprobe *__pairs, size_t __pair_count, + size_t __cpu_count, unsigned long int *__cpus, + unsigned int __flags) + __nonnull ((1)) __wur + __fortified_attr_access (__read_write__, 1, 2) + __fortified_attr_access (__read_only__, 4, 3); + +__END_DECLS + +#endif /* sys/hwprobe.h */ From patchwork Wed Jan 10 20:51:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evan Green X-Patchwork-Id: 83790 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 14DB138582B7 for ; Wed, 10 Jan 2024 20:54:36 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-il1-x12a.google.com (mail-il1-x12a.google.com [IPv6:2607:f8b0:4864:20::12a]) by sourceware.org (Postfix) with ESMTPS id 9FEB23860C2E for ; Wed, 10 Jan 2024 20:51:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9FEB23860C2E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9FEB23860C2E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::12a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704919918; cv=none; b=U4mDshCYZz5ciRh5/gqCxEvPC344ClCluo5dy4dlK6T8Io4/0ufnaBwk9L3V1rc4C9KEdLbMwLgYYlSxBgXURXEoZRQR+003KvY1D26HsJg3tOPaBLD5U7Z0CE4ufHF5AYGEG3355ZIxTd7NqiYk3zw5j3RKPq5rPilde8pEwvM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704919918; c=relaxed/simple; bh=1wuQLv8CmSDMOXjtH1MNMaPK7zKggX9VA0d9GfH+IqM=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=UGFlptw4DvnLz//jcUUym0UXXhxLcrsZr/3Gdvdg1vDNLXXwT3tbv7MTak9ZkuDMeBEyoD62fUfVPJ++QATsTLygktgvoqhm98WBzfdhsEOvdxAKAPR7WHhnCLJOu1FntZA370ReNS0UaiPn/G2SGskEcyrptWVyrBRDmEA3Dpw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x12a.google.com with SMTP id e9e14a558f8ab-3606e211e5cso26487885ab.3 for ; Wed, 10 Jan 2024 12:51:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1704919915; x=1705524715; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=p5uSuckPc0H6i5FwW8PtNnyo41Zsuwt/Ty+Wr6YA8Jc=; b=wV3SG4PkBy4O1fwuBLVgL6rNPt0mzWQQi5Oh5KcikRm6QRPbSXDdGQqoGB+2bONDqL E2VtCzlXXsX3mBDjpmRGalq2AmQgs/Dwx3yJ7lpunJnTved2m7/Lt5khTFUAYwEA+eFh mBF0qNXIMV4FZbSSpkLO0SpjSr6ENcV6r63R61YZFt7pV6EH+jJTZjDeSpafF1VN0ioJ /e9mYJN4hyAaSSOqCCadrriwaU9OsWHPkTqSwRh6SM5rJoVt/mBVLJnwyvoDmoPi7OCt lkpfR9rqoOXoO629SllSdjm2Uh2gFTl72yjd7/KhYxfAqHm8MPE13jBl3S+weKjlAwk8 ZPuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704919915; x=1705524715; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=p5uSuckPc0H6i5FwW8PtNnyo41Zsuwt/Ty+Wr6YA8Jc=; b=hQxu+hQFiLolngbhqU1b1h+icVCPnNexz1D4nSdJquPqde0tsAshlOAKcZA/vBzelU JZebRijpY4yAavAmulTtlb3i0NagzVoofHHa7Ywba1arIzY0nEx1vsmeqMfQd2KSjcdn 3kOZd4gWBEO62KO3Mosai4SZzK3AvwBc01tftGRSb8prD+rlecmp+bSQYxE1QOpm9+OA iVsRT3fVIMhm9olJimP+EP1etYVso8e4Zw9kXBcl6smZ+8M9PKrtERMsBX1KtammWuf8 b8M7TuCTy4bwvIwrYFQeQqBySi0kY0IxyGvpPzMsYDwLnhKeClIFT7A/Fwebu0nKkRU8 W9wA== X-Gm-Message-State: AOJu0YzXDGl86TRass7rClNSurPDKifsLhqzb66FDtXXy/sUgQJNf2ck 53VmF+cF/NHFOhdo7x4Wo62jcIz9p4tlum277OUGiNxNdoo= X-Google-Smtp-Source: AGHT+IGBERE8mzX/eOyshV/p0hcp5fgX1D6uOitH3LP05DTOJmjTpntcjlfIBz5eHzXZFnVyL3MOXw== X-Received: by 2002:a05:6e02:1b84:b0:35f:e355:bc2a with SMTP id h4-20020a056e021b8400b0035fe355bc2amr131253ili.42.1704919915042; Wed, 10 Jan 2024 12:51:55 -0800 (PST) Received: from evan.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id y4-20020a92c984000000b0035ff9825323sm1452018iln.30.2024.01.10.12.51.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 12:51:54 -0800 (PST) From: Evan Green To: libc-alpha@sourceware.org Cc: palmer@rivosinc.com, vineetg@rivosinc.com, slewis@rivosinc.com, Florian Weimer , Evan Green Subject: [PATCH v11 2/7] linux: Introduce INTERNAL_VSYSCALL Date: Wed, 10 Jan 2024 12:51:35 -0800 Message-Id: <20240110205140.754849-3-evan@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240110205140.754849-1-evan@rivosinc.com> References: <20240110205140.754849-1-evan@rivosinc.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, 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.30 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 Add an INTERNAL_VSYSCALL() macro that makes a vDSO call, falling back to a regular syscall, but without setting errno. Instead, the return value is plumbed straight out of the macro. Signed-off-by: Evan Green --- (no changes since v10) Changes in v10: - Introduced INTERNAL_VSYSCALL patch sysdeps/unix/sysv/linux/sysdep-vdso.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sysdeps/unix/sysv/linux/sysdep-vdso.h b/sysdeps/unix/sysv/linux/sysdep-vdso.h index 189319ad98..f7d4b29b25 100644 --- a/sysdeps/unix/sysv/linux/sysdep-vdso.h +++ b/sysdeps/unix/sysv/linux/sysdep-vdso.h @@ -53,4 +53,23 @@ sc_ret; \ }) +#define INTERNAL_VSYSCALL(name, nr, args...) \ + ({ \ + __label__ out; \ + long int sc_ret; \ + \ + __typeof (GLRO(dl_vdso_##name)) vdsop = GLRO(dl_vdso_##name); \ + if (vdsop != NULL) \ + { \ + sc_ret = INTERNAL_VSYSCALL_CALL (vdsop, nr, ##args); \ + if ((!INTERNAL_SYSCALL_ERROR_P (sc_ret)) || \ + (INTERNAL_SYSCALL_ERRNO (sc_ret) != ENOSYS)) \ + goto out; \ + } \ + \ + sc_ret = INTERNAL_SYSCALL_CALL (name, ##args); \ + out: \ + sc_ret; \ + }) + #endif /* SYSDEP_VDSO_LINUX_H */ From patchwork Wed Jan 10 20:51:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evan Green X-Patchwork-Id: 83786 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 ACA8A386102B for ; Wed, 10 Jan 2024 20:53:02 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-il1-x132.google.com (mail-il1-x132.google.com [IPv6:2607:f8b0:4864:20::132]) by sourceware.org (Postfix) with ESMTPS id 96EEE385E45C for ; Wed, 10 Jan 2024 20:51:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 96EEE385E45C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 96EEE385E45C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::132 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704919922; cv=none; b=MBfkIlBguV47iOpV2dE722uPR5HBU+WmPBziYxROjI7amtvF+T7jJZGVrxvk4bTpBH6HiRGf6eoUCQFTHzM/Z4jwkKNYL1KKFETOX6Q8bMpjkogYBD/ICchZ0BaFf26/woV7YSHqD63hDlhQ4l0WEys2hS5340PhlDh0tQDVnlc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704919922; c=relaxed/simple; bh=mB2uCA7GFf8IcHf8n94d1wmGmYoiXZHMQQ84vEGbFaw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=jcOaVEIf3uBeGSxYNUZOcXN/cr/TIziWt5Qao0AEjqCXGkVoTU8XMMXfNNTl7ZzPZp3dagOEmiuSeILvchwCb3EDrO7EX4eswdwpf3/QE7MOyjp+NtGHkI/naiue3Q8Awp7AI3+NG5a5VQ2/CuNHxfJR5MU/ADYTcUOGDG6EtIc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x132.google.com with SMTP id e9e14a558f8ab-3606dc4fdf2so24633015ab.0 for ; Wed, 10 Jan 2024 12:51:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1704919918; x=1705524718; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=N3idKZzOv0gJ2EL0bSki58RkN41BRlAo5gY4kORw7kQ=; b=E1IyAsEuVn8GbSiDSvbrn+ejSJG/W2GPOTkgexHBhoLkanKtHbHw14WZMyF6bjkgCh DB9Upk+K3uUGNW62POaKP+wD3eZxPA2uWHix0MQc5NQU9Op9gvCQoHTLH2LPNdGIpwka xXQCQQymouzhezFp8dde8WfA9PRyFJ2AvFh0qfamP12k/YJ9C7wqDgbXi76olvuQ1EkA ktbvl6QeCuGPT/D5kdgWtA/HI2qL21R06TPkLIOjeXcHeOKWLICyYLHnd9RB0/yfDXUE 2hQw3lFv0soXxPVpmh1+ZDqp0cqXFeHP5AFwV9d5WQPmmu5+HCFphIkbv0DU8QDcm831 xhjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704919918; x=1705524718; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=N3idKZzOv0gJ2EL0bSki58RkN41BRlAo5gY4kORw7kQ=; b=iYpoCGaSurbyFubgv8btXNARAUbhO0hA5O39hzMLCHZOb+wKgohF05dRRzEiLkOSSg KB0GgKRZP3iYKvetFB4CTRDCk62jMRP5eSkf9Z1x7zKF4+FpB/pZxlRbei4afgQPJyni HL1ERDoBNH4DEgHyhSEOs33LR/qJ/TMKDSW9Pj6535BIKyPfbQ/wJEv5zzyy6Z9zuHN6 agofHK7NG+sy15KczHHr7OaoIKj/ZLNaZR7+EH+4potDeXfaiZOp+/fRF6QQYlu2uWQj ZHoGg6xgaevA4WzuRahgV9RiXyNT4rTlzQh8uIBx3SqgdEWDoRDAryNJryvjYnsISuDh d80Q== X-Gm-Message-State: AOJu0Yz9erQ+NEyYbrJDjR1n6czoFTtybcZZVmQ10TZDtLjqOGUlDJOY z4dQcDmQ4zk5D9LgSB759fV8ZNW3k5aRhY7YI1wkjfBCxK8= X-Google-Smtp-Source: AGHT+IHn+6dgTMyk7RDaUuSN+UQWilXWlBlDpm2Oy8lsjJnCoqJQh7ujbbKRqsmdsS72evBDZlN2zg== X-Received: by 2002:a05:6e02:12a2:b0:360:2db:b47c with SMTP id f2-20020a056e0212a200b0036002dbb47cmr128195ilr.19.1704919917870; Wed, 10 Jan 2024 12:51:57 -0800 (PST) Received: from evan.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id y4-20020a92c984000000b0035ff9825323sm1452018iln.30.2024.01.10.12.51.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 12:51:57 -0800 (PST) From: Evan Green To: libc-alpha@sourceware.org Cc: palmer@rivosinc.com, vineetg@rivosinc.com, slewis@rivosinc.com, Florian Weimer , Evan Green Subject: [PATCH v11 3/7] riscv: Add hwprobe vdso call support Date: Wed, 10 Jan 2024 12:51:36 -0800 Message-Id: <20240110205140.754849-4-evan@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240110205140.754849-1-evan@rivosinc.com> References: <20240110205140.754849-1-evan@rivosinc.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, 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.30 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 The new riscv_hwprobe syscall also comes with a vDSO for faster answers to your most common questions. Call in today to speak with a kernel representative near you! Signed-off-by: Evan Green --- (no changes since v10) Changes in v10: - Remove leading underscores in definition (Adhemerval) - Remove spurious 5 from INTERNAL_SYSCALL_CALL (Adhemerval) - Use new INTERNAL_VSYSCALL macro instead (Adhemerval) Changes in v7: - Use INTERNAL_VSYSCALL_CALL (Florian) Changes in v3: - Add the "return" to the vsyscall - Fix up vdso arg types to match kernel v4 version - Remove ifdef around INLINE_VSYSCALL (Adhemerval) Changes in v2: - Add vDSO interface sysdeps/unix/sysv/linux/dl-vdso-setup.c | 10 ++++++++++ sysdeps/unix/sysv/linux/dl-vdso-setup.h | 3 +++ sysdeps/unix/sysv/linux/riscv/hwprobe.c | 4 ++-- sysdeps/unix/sysv/linux/riscv/sysdep.h | 1 + 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/sysdeps/unix/sysv/linux/dl-vdso-setup.c b/sysdeps/unix/sysv/linux/dl-vdso-setup.c index 5dd7ed9d12..3a44944dbb 100644 --- a/sysdeps/unix/sysv/linux/dl-vdso-setup.c +++ b/sysdeps/unix/sysv/linux/dl-vdso-setup.c @@ -71,6 +71,16 @@ PROCINFO_CLASS int (*_dl_vdso_clock_getres_time64) (clockid_t, # ifdef HAVE_GET_TBFREQ PROCINFO_CLASS uint64_t (*_dl_vdso_get_tbfreq)(void) RELRO; # endif + +/* RISC-V specific ones. */ +# ifdef HAVE_RISCV_HWPROBE +PROCINFO_CLASS int (*_dl_vdso_riscv_hwprobe)(void *, + size_t, + size_t, + unsigned long *, + unsigned int) RELRO; +# endif + #endif #undef RELRO diff --git a/sysdeps/unix/sysv/linux/dl-vdso-setup.h b/sysdeps/unix/sysv/linux/dl-vdso-setup.h index e87d886940..8aee5a8212 100644 --- a/sysdeps/unix/sysv/linux/dl-vdso-setup.h +++ b/sysdeps/unix/sysv/linux/dl-vdso-setup.h @@ -47,6 +47,9 @@ setup_vdso_pointers (void) #ifdef HAVE_GET_TBFREQ GLRO(dl_vdso_get_tbfreq) = dl_vdso_vsym (HAVE_GET_TBFREQ); #endif +#ifdef HAVE_RISCV_HWPROBE + GLRO(dl_vdso_riscv_hwprobe) = dl_vdso_vsym (HAVE_RISCV_HWPROBE); +#endif } #endif diff --git a/sysdeps/unix/sysv/linux/riscv/hwprobe.c b/sysdeps/unix/sysv/linux/riscv/hwprobe.c index 28a448175b..e64c159eb3 100644 --- a/sysdeps/unix/sysv/linux/riscv/hwprobe.c +++ b/sysdeps/unix/sysv/linux/riscv/hwprobe.c @@ -28,8 +28,8 @@ int __riscv_hwprobe (struct riscv_hwprobe *pairs, size_t pair_count, { int r; - r = INTERNAL_SYSCALL_CALL (riscv_hwprobe, pairs, pair_count, - cpu_count, cpus, flags); + r = INTERNAL_VSYSCALL (riscv_hwprobe, 5, pairs, pair_count, + cpu_count, cpus, flags); /* Negate negative errno values to match pthreads API. */ return -r; diff --git a/sysdeps/unix/sysv/linux/riscv/sysdep.h b/sysdeps/unix/sysv/linux/riscv/sysdep.h index 5583b96d23..ee015dfeb6 100644 --- a/sysdeps/unix/sysv/linux/riscv/sysdep.h +++ b/sysdeps/unix/sysv/linux/riscv/sysdep.h @@ -156,6 +156,7 @@ /* List of system calls which are supported as vsyscalls (for RV32 and RV64). */ # define HAVE_GETCPU_VSYSCALL "__vdso_getcpu" +# define HAVE_RISCV_HWPROBE "__vdso_riscv_hwprobe" # undef HAVE_INTERNAL_BRK_ADDR_SYMBOL # define HAVE_INTERNAL_BRK_ADDR_SYMBOL 1 From patchwork Wed Jan 10 20:51:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evan Green X-Patchwork-Id: 83789 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 1F8CA3860C2E for ; Wed, 10 Jan 2024 20:53:45 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-il1-x130.google.com (mail-il1-x130.google.com [IPv6:2607:f8b0:4864:20::130]) by sourceware.org (Postfix) with ESMTPS id D3163385E032 for ; Wed, 10 Jan 2024 20:52:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D3163385E032 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D3163385E032 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::130 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704919926; cv=none; b=syvhB/cOCSz6mFtZ46L0CCpoSP3Q19EMQ8WoFHlIm8EU1AbTpUtKR3daqo/TyKu9eCZiBEfSaBHiAxwn7qmOul8IpjUdUnCxNa/bzpxZbj5rGORNP3I3wDWjmIqvO8iYN2XufmbQBD8ABTJ4FgHouPKTHKajzAJRN9aIhvwBVTY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704919926; c=relaxed/simple; bh=3KEb2aY1qvkZ9DfZSgGgFnctwp76uRNqePMZFCzhuPg=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=gp6fm7cdaVGDmkzeUPgmfgBVUQ1lsk7NFRKfxUWzdztPs+xsn2yWtagOQ1N9Zp9IC6Ug5tltk1URDxbWkg8R9XRHkcIpEACcB8xb3fmrCsK1nXTFyrjxusw6mZBJAxEDOhNTCJTTL3ecg3O12MGCIU1UBDZ+lzJShB3zQ7dQNvU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x130.google.com with SMTP id e9e14a558f8ab-3606f57e08aso21011315ab.3 for ; Wed, 10 Jan 2024 12:52:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1704919920; x=1705524720; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wVONlSWH7rFsPXRa1lJYOvQx3bPsSIZvkL8AKiDA2+0=; b=xbkhVKF60UbkIgLuUlY3nZU7Y0yjCojPAX/ueCnCWC4n3ERyYSWcJy3AfPnv0z3Pwb h+fMp2yctWCKjPbLjOY7S9gJmg4pcSDvfWtT3Q+9pi2P+/Z0VPjq92T6G/HpniXS4sqT /vUNE93TGVMeVGtylHUN6zIRCZWzcSeEaF4AfArvTntHn7l4d6/TcsedELsyXM8n2zaG zHO19kmPNuuLE1ML1owWG8POYlMdFaTjr3Vacbso6e8cKy3/acrXxx1KwbELdP+10avA 5Xj94KtGhiQ0js6AnzFc12iD6o6oqquA2/HK1flzwDMXf/04Z4lKuAGTyatbfJ4N6eIb HYnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704919920; x=1705524720; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wVONlSWH7rFsPXRa1lJYOvQx3bPsSIZvkL8AKiDA2+0=; b=DJoH9/aVB8pt3gHjIIcG7bV/BotHrCGsRGZSHsEAPSw0G6kL1ut0rHLdIzHNYkUc8g vBco36qImr/tGyzU5X62qxqKpHneSkimz1JVYJ98B+o6DjdeYyB8P04Dgsn+9ZVO7/CQ gzrdZCe4VLx9FbsdUnSVCPymEDKXZ61a3JQIh68cu1L2NWnkArma2nqatNyl9Wwfjnyp gobjZPU8dSqKIxcC3h3y/ySWu8ldWsobkX3pi/Q8+6llGIZzV5bsiHvqQpj5pr22F37Q c/85BzBzDOZwmRcuvTCP5BmkE0GCaFLJ4FinIglNc1aX3AF+KSxsnnJRSEv7tUmZkfbf +QIQ== X-Gm-Message-State: AOJu0YyY6BlaVskWYmxUekHHdBGyAblrFetpu70WN6d9+Ay8QB8+j2mM 7mqpsdPQHQhC0ubt1dcqvbwZQp/Pys4uqG8IiJTzPgVeizU= X-Google-Smtp-Source: AGHT+IEnJCIpLtYE9y3i7yb9LnSYYfR4x8nmELZ3/ZjZI+yT7LkqwIgkCnagt/aHBKTiGCXfdx1W8Q== X-Received: by 2002:a92:cf4e:0:b0:35f:d1ed:d189 with SMTP id c14-20020a92cf4e000000b0035fd1edd189mr117401ilr.58.1704919920023; Wed, 10 Jan 2024 12:52:00 -0800 (PST) Received: from evan.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id y4-20020a92c984000000b0035ff9825323sm1452018iln.30.2024.01.10.12.51.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 12:51:59 -0800 (PST) From: Evan Green To: libc-alpha@sourceware.org Cc: palmer@rivosinc.com, vineetg@rivosinc.com, slewis@rivosinc.com, Florian Weimer , Evan Green Subject: [PATCH v11 4/7] riscv: Add __riscv_hwprobe pointer to ifunc calls Date: Wed, 10 Jan 2024 12:51:37 -0800 Message-Id: <20240110205140.754849-5-evan@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240110205140.754849-1-evan@rivosinc.com> References: <20240110205140.754849-1-evan@rivosinc.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, 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.30 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 The new __riscv_hwprobe() function is designed to be used by ifunc selector functions. This presents a challenge for applications and libraries, as ifunc selectors are invoked before all relocations have been performed, so an external call to __riscv_hwprobe() from an ifunc selector won't work. To address this, pass a pointer to the __riscv_hwprobe() function into ifunc selectors as the second argument (alongside dl_hwcap, which was already being passed). Include a typedef as well for convenience, so that ifunc users don't have to go through contortions to call this routine. Users will need to remember to check the second argument for NULL, to account for older glibcs that don't pass the function. Signed-off-by: Evan Green --- (no changes since v10) Changes in v10: - Hand in pointer to __riscv_hwprobe(), not vDSO function Changes in v7: - Remove __THROW from function pointer type, as it creates warnings together with __fortified_attr_access. sysdeps/riscv/dl-irel.h | 9 +++++---- sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h | 10 ++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/sysdeps/riscv/dl-irel.h b/sysdeps/riscv/dl-irel.h index e6ab51ccd4..61b3511c96 100644 --- a/sysdeps/riscv/dl-irel.h +++ b/sysdeps/riscv/dl-irel.h @@ -24,6 +24,7 @@ #include #include #include +#include #define ELF_MACHINE_IRELA 1 @@ -31,10 +32,10 @@ static inline ElfW(Addr) __attribute ((always_inline)) elf_ifunc_invoke (ElfW(Addr) addr) { - /* The second argument is a void pointer to preserve the extension - fexibility. */ - return ((ElfW(Addr) (*) (uint64_t, void *)) (addr)) - (GLRO(dl_hwcap), NULL); + /* The third argument is a void pointer to preserve the extension + flexibility. */ + return ((ElfW(Addr) (*) (uint64_t, void *, void *)) (addr)) + (GLRO(dl_hwcap), __riscv_hwprobe, NULL); } static inline void diff --git a/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h b/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h index 5592b9e100..34a2e3dbc2 100644 --- a/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h +++ b/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h @@ -69,6 +69,16 @@ extern int __riscv_hwprobe (struct riscv_hwprobe *__pairs, size_t __pair_count, __fortified_attr_access (__read_write__, 1, 2) __fortified_attr_access (__read_only__, 4, 3); +/* A pointer to the __riscv_hwprobe vDSO function is passed as the second + argument to ifunc selector routines. Include a function pointer type for + convenience in calling the function in those settings. */ +typedef int (*__riscv_hwprobe_t) (struct riscv_hwprobe *__pairs, size_t __pair_count, + size_t __cpu_count, unsigned long int *__cpus, + unsigned int __flags) + __nonnull ((1)) __wur + __fortified_attr_access (__read_write__, 1, 2) + __fortified_attr_access (__read_only__, 4, 3); + __END_DECLS #endif /* sys/hwprobe.h */ From patchwork Wed Jan 10 20:51:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evan Green X-Patchwork-Id: 83788 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 BF553386103C for ; Wed, 10 Jan 2024 20:53:28 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-il1-x135.google.com (mail-il1-x135.google.com [IPv6:2607:f8b0:4864:20::135]) by sourceware.org (Postfix) with ESMTPS id 684CD386102E for ; Wed, 10 Jan 2024 20:52:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 684CD386102E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 684CD386102E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::135 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704919927; cv=none; b=uz8L6wfpKu7xDoM2gAVRpw0OH4Q57UbDCyM5xAkpbdu0AjhGw2i8X9DeJYL522jcmPtQhvrUjjZVmVpf16TIjpl4tc6DikGUitAp7tiumwFCvNKEiGJ+iPTf3mTpD0UjgcqHVzOnmjIMuQhSlvTmE5ywgOfslDt+Hm2GzZNn62U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704919927; c=relaxed/simple; bh=IfLYqtBpEfYI7JQqz5y5r1WnlGLUBaT3M0kT3Ds/las=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=YUjgNY3/XElbTqdGD5sQ+gznS367NIYM3Db37JxXlt+wIvJQ0BoWZYXo8YlaAdxYWuDP3/Er/fdV6Nw01/DlInE9ttg+XPtd0Fk3r3qaNyy1+Le44EzWNFb+8Y9Qt82GoKWzu+bOIFd2BDK7WOIHwYKKt/Of2tmTTEWTRUQXr04= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x135.google.com with SMTP id e9e14a558f8ab-36082f3cb06so23837505ab.1 for ; Wed, 10 Jan 2024 12:52:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1704919922; x=1705524722; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NMF6KqBo+n+H2+rK2NA+ZSE+9dr6A+cr61ZFqHsTubw=; b=MdI2WbvP9PkU7hCGtyzXbgSvkiQfSxBEdtXFkqFw7cVhKHMhnWneioU8ty7QKKFbXT lLKc8KUOd3gOa9CHIOhtvPo5oNuFa0GWOatDQpYANN+HzgORPHVOPzV+8UlHvSm4ZDov jAxcWopq50dHHdf/1sVrqayq35trxtUe/LnoSRlffYXws+d6Rgo7dNiKvGhbJUlz0h7A uepPVrV2B0feXMANnQZv2CDOPuT94MfD9HHJiv1J+5CxaRNsXfNZHZC6etWzIZTOKDFN MNe+v0G1xwB7pRdOySdPvbmN+8Z9zvyYuLl1pr/BxbMO+5sTCCCN3pThpdrROAGwOr89 2Cyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704919922; x=1705524722; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NMF6KqBo+n+H2+rK2NA+ZSE+9dr6A+cr61ZFqHsTubw=; b=VES8YxznJXnjYn4IhIkXXA406H/jzMosplYz6gBkjugQHHSU3OkWif03t+UJ5uoU4t nTC67wCsKJpN6Ic8eJvEQIl4Xaktt6dNeWhVLRw43AGySUhGjBsQaBspdAfwn1w3oaIV vISLpj9a1++mGaUuS0z06PGXplrzuEfPzE/+0BImOMwy+P5nYCq79D+PCSdBUtytjwo2 w5b8KgexVs99E6pyvuajgowxM28SWcjM/1sgjFbfs+O8wA1Ppocxto7q9qmjvbs4JydG vGYMa3QJvWcRR9rt86dA70oVLu9uKH66JVEEvPWr/4NS+uJQrn+XM/GLY/oFlGVyl95V EZAg== X-Gm-Message-State: AOJu0YzSrjDu7VX9KSYXwy+mFDI+53/pNSscqZ953BYy/BsWjz9cHQZk jLAzSttPHoQetCMlNIrhuQJweHmP4mt8WnZJ88qtrK8AFpM= X-Google-Smtp-Source: AGHT+IEEbO+j5wLtmWN2zfnO6RSzWUcbs/xrofnuUjmJMkIji7iItH0mKDF9pEDBdw1UjONJl4hwEg== X-Received: by 2002:a05:6e02:1a89:b0:35f:a33f:a181 with SMTP id k9-20020a056e021a8900b0035fa33fa181mr143948ilv.15.1704919922217; Wed, 10 Jan 2024 12:52:02 -0800 (PST) Received: from evan.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id y4-20020a92c984000000b0035ff9825323sm1452018iln.30.2024.01.10.12.52.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 12:52:01 -0800 (PST) From: Evan Green To: libc-alpha@sourceware.org Cc: palmer@rivosinc.com, vineetg@rivosinc.com, slewis@rivosinc.com, Florian Weimer , Evan Green Subject: [PATCH v11 5/7] riscv: Enable multi-arg ifunc resolvers Date: Wed, 10 Jan 2024 12:51:38 -0800 Message-Id: <20240110205140.754849-6-evan@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240110205140.754849-1-evan@rivosinc.com> References: <20240110205140.754849-1-evan@rivosinc.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.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, URIBL_BLACK 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.30 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 RISC-V is apparently the first architecture to pass more than one argument to ifunc resolvers. The helper macros in libc-symbols.h, __ifunc_resolver(), __ifunc(), and __ifunc_hidden(), are incompatible with this. These macros have an "arg" (non-final) parameter that represents the parameter signature of the ifunc resolver. The result is an inability to pass the required comma through in a single preprocessor argument. Rearrange the __ifunc_resolver() macro to be variadic, and pass the types as those variable parameters. Move the guts of __ifunc() and __ifunc_hidden() into new macros, __ifunc_args(), and __ifunc_args_hidden(), that pass the variable arguments down through to __ifunc_resolver(). Then redefine __ifunc() and __ifunc_hidden(), which are used in a bunch of places, to simply shuffle the arguments down into __ifunc_args[_hidden]. Finally, define a riscv-ifunc.h header, which provides convenience macros to those looking to write ifunc selectors that use both arguments. Signed-off-by: Evan Green --- Changes in v11: - Update copyright year (Adhemerval) Changes in v9: - Fix a couple of typos causing powerpc not to build (build-many-glibcs) Changes in v6: - Introduced riscv-ifunc.h for multi-arg ifunc selectors. --- include/libc-symbols.h | 28 +++++++++++++++++----------- sysdeps/riscv/riscv-ifunc.h | 27 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 sysdeps/riscv/riscv-ifunc.h diff --git a/include/libc-symbols.h b/include/libc-symbols.h index e21bb599b3..4367aa6740 100644 --- a/include/libc-symbols.h +++ b/include/libc-symbols.h @@ -667,9 +667,9 @@ for linking") #endif /* Helper / base macros for indirect function symbols. */ -#define __ifunc_resolver(type_name, name, expr, arg, init, classifier) \ +#define __ifunc_resolver(type_name, name, expr, init, classifier, ...) \ classifier inhibit_stack_protector \ - __typeof (type_name) *name##_ifunc (arg) \ + __typeof (type_name) *name##_ifunc (__VA_ARGS__) \ { \ init (); \ __typeof (type_name) *res = expr; \ @@ -677,13 +677,13 @@ for linking") } #ifdef HAVE_GCC_IFUNC -# define __ifunc(type_name, name, expr, arg, init) \ +# define __ifunc_args(type_name, name, expr, init, ...) \ extern __typeof (type_name) name __attribute__ \ ((ifunc (#name "_ifunc"))); \ - __ifunc_resolver (type_name, name, expr, arg, init, static) + __ifunc_resolver (type_name, name, expr, init, static, __VA_ARGS__) -# define __ifunc_hidden(type_name, name, expr, arg, init) \ - __ifunc (type_name, name, expr, arg, init) +# define __ifunc_args_hidden(type_name, name, expr, init, ...) \ + __ifunc_args (type_name, name, expr, init, __VA_ARGS__) #else /* Gcc does not support __attribute__ ((ifunc (...))). Use the old behaviour as fallback. But keep in mind that the debug information for the ifunc @@ -694,18 +694,24 @@ for linking") different signatures. (Gcc support is disabled at least on a ppc64le Ubuntu 14.04 system.) */ -# define __ifunc(type_name, name, expr, arg, init) \ +# define __ifunc_args(type_name, name, expr, init, ...) \ extern __typeof (type_name) name; \ - __typeof (type_name) *name##_ifunc (arg) __asm__ (#name); \ - __ifunc_resolver (type_name, name, expr, arg, init,) \ + __typeof (type_name) *name##_ifunc (__VA_ARGS__) __asm__ (#name); \ + __ifunc_resolver (type_name, name, expr, init, , __VA_ARGS__) \ __asm__ (".type " #name ", %gnu_indirect_function"); -# define __ifunc_hidden(type_name, name, expr, arg, init) \ +# define __ifunc_args_hidden(type_name, name, expr, init, ...) \ extern __typeof (type_name) __libc_##name; \ - __ifunc (type_name, __libc_##name, expr, arg, init) \ + __ifunc (type_name, __libc_##name, expr, __VA_ARGS__, init) \ strong_alias (__libc_##name, name); #endif /* !HAVE_GCC_IFUNC */ +#define __ifunc(type_name, name, expr, arg, init) \ + __ifunc_args (type_name, name, expr, init, arg) + +#define __ifunc_hidden(type_name, name, expr, arg, init) \ + __ifunc_args_hidden (type_name, name, expr, init, arg) + /* The following macros are used for indirect function symbols in libc.so. First of all, you need to have the function prototyped somewhere, say in foo.h: diff --git a/sysdeps/riscv/riscv-ifunc.h b/sysdeps/riscv/riscv-ifunc.h new file mode 100644 index 0000000000..c77ab51548 --- /dev/null +++ b/sysdeps/riscv/riscv-ifunc.h @@ -0,0 +1,27 @@ +/* Common definition for ifunc resolvers. Linux/RISC-V version. + This file is part of the GNU C Library. + Copyright (C) 2024 Free Software Foundation, Inc. + + 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 + +#define INIT_ARCH() + +#define riscv_libc_ifunc(name, expr) \ + __ifunc_args (name, name, expr(hwcap, hwprobe), INIT_ARCH, \ + uint64_t hwcap, __riscv_hwprobe_t hwprobe) From patchwork Wed Jan 10 20:51:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evan Green X-Patchwork-Id: 83791 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 29FBF385801C for ; Wed, 10 Jan 2024 20:55:14 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-il1-x12f.google.com (mail-il1-x12f.google.com [IPv6:2607:f8b0:4864:20::12f]) by sourceware.org (Postfix) with ESMTPS id 4D3733861024 for ; Wed, 10 Jan 2024 20:52:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4D3733861024 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4D3733861024 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::12f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704919928; cv=none; b=YfZuo7lKvdUwRNJ4MnWTzC0UtN+ozkF8ID8s6gkSdlPs5tKMjFWPBH/Ja59RQrfhC0B0zA//sW8uRMjVmzITUlsjPpvHX2RzLObO46U8SrJNeciIc7DWZaJU9jrAp5ZR7LuWgvZC6PBPPMZ+3DJMHugkakjZwEwVbUlyqMrftf4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704919928; c=relaxed/simple; bh=z6VXoFBA+32jBCa47aqApyIZUiDB2KrrUX8TgpuGbfY=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=LZTR6EchC8QJFVCfZEHydPXxFQdgchc7qsJViTGxEqSzT01RodFB19tARzzLXR5ajSQRf1ZdIUQF3isdzq3zF2h4gCFNspchR8xUeWLa8kWeiP5i5wczNQ1viZO3s+ZeIQhC9kzelYO8RoMK5EDBSFsueznTnftJDsSm5Q/hWtY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x12f.google.com with SMTP id e9e14a558f8ab-36066190a99so24120985ab.3 for ; Wed, 10 Jan 2024 12:52:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1704919924; x=1705524724; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lt2XVN3bXGyFsWISuKinXHEQKwDeC3LYI5DtiHZ/o4E=; b=x/oQ+GyeHVxk6gu/ma7qkqGYwj+l7OfkKD1Zw2dImYgxicXsdMLEp1RafLtyaVTaVK FJPDkqRlCm13eW1S/BTuPlYCMH0JgoGj1beCypsi3iQSMzNaPXJE1i03XQYdS1y9HiZM 3ioeiom/RA4OIVSOEk2MATZ/Lhdj6HCPzYFwxMxglGcjqDtDEgn7JM7VmjftVLWXop1C c9MHh3lGRCMMxdNBYj/PnqQO1ckGyEN4xo5KV/i82dQ5OZP6Wu5gTlqouHMvcpxonV86 2BwLwFYGoRjCbFcq3tBKxdaTaahBMRSdkRq+X70E7qlGKbAq4CYPBFNXMffAt9M2oVn6 /wPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704919924; x=1705524724; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lt2XVN3bXGyFsWISuKinXHEQKwDeC3LYI5DtiHZ/o4E=; b=wWPbP6zInVEXPP7xiLP5Mne0960FYdzNn2uzmbv+KbNtfqI7JYHHpSGUljykFOtyU9 uCehTv4Ivl8LgIxqoeu/hNXa8LaXyIXxF8InGf03DAx5T+NsXmy+IdXuSV1y5JmdtBBW kN10TlVwjt69cWPtvrLJdwoUDw3ue7uISmvqgbGqYZK3B0YkAPeRchhIko+fvQaR6p4+ OxDAXd8Dd5MoFj2ujTnypNUKYH9jESlokP0fXOd43Wr0mt80Gu/6gPeiT1Lh6VPWoTLL 41z0J98n8wYxogPeNEJM4XRvVjpt83B88wRKNM/lyoK64zzsYMDfWiyVYHDWYkqQDf+q z8jQ== X-Gm-Message-State: AOJu0YysEpDH2xmCwd1DZ7x5SuR6bDh4M53bLW/K8zcGI1snhnZ751zb GthCn7izI9smbNqWCKTRVvmRXvAJdxFXsy005r3Al7Yy3nI= X-Google-Smtp-Source: AGHT+IGfdItD12QziXLFtf/Efgnx2FJA3bbo+xw9VYOQRSNuEq6e3oW5rwtFy/v3HN9zhMqVi3RmuA== X-Received: by 2002:a05:6e02:156a:b0:360:197:55d1 with SMTP id k10-20020a056e02156a00b00360019755d1mr148401ilu.23.1704919924674; Wed, 10 Jan 2024 12:52:04 -0800 (PST) Received: from evan.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id y4-20020a92c984000000b0035ff9825323sm1452018iln.30.2024.01.10.12.52.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 12:52:04 -0800 (PST) From: Evan Green To: libc-alpha@sourceware.org Cc: palmer@rivosinc.com, vineetg@rivosinc.com, slewis@rivosinc.com, Florian Weimer , Evan Green Subject: [PATCH v11 6/7] riscv: Add ifunc helper method to hwprobe.h Date: Wed, 10 Jan 2024 12:51:39 -0800 Message-Id: <20240110205140.754849-7-evan@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240110205140.754849-1-evan@rivosinc.com> References: <20240110205140.754849-1-evan@rivosinc.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, 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.30 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 Add a little helper method so it's easier to fetch a single value from the hwprobe function when used within an ifunc selector. Signed-off-by: Evan Green --- Changes in v11: - Remove superfluous 'signed' (Adhemerval) - Put helper before __END_DECLS (Adhemerval) - Add comment about +1 on non-Linux systems (Stefan) Changes in v10: - Avoid implicit comparisons (Adhemerval) Changes in v9: - Use __inline rather than inline so c89 compiles (build-many-glibcs) Changes in v7: - Introduced static inline helper (Richard) sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h b/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h index 34a2e3dbc2..8ecb43bb69 100644 --- a/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h +++ b/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h @@ -22,6 +22,7 @@ #include #include +#include #ifdef __has_include # if __has_include () # include @@ -79,6 +80,34 @@ typedef int (*__riscv_hwprobe_t) (struct riscv_hwprobe *__pairs, size_t __pair_c __fortified_attr_access (__read_write__, 1, 2) __fortified_attr_access (__read_only__, 4, 3); +/* Helper function usable from ifunc selectors that probes a single key. */ +static __inline int +__riscv_hwprobe_one(__riscv_hwprobe_t hwprobe_func, + long long int key, + unsigned long long int *value) +{ + struct riscv_hwprobe pair; + int rc; + + /* Earlier versions of glibc pass NULL as the second ifunc parameter. Other C + libraries on non-Linux systems may pass +1 as this function pointer to + indicate no support. Users copying this function to exotic worlds + (non-Linux non-glibc) may want to do additional validity checks here. */ + if (hwprobe_func == NULL) + return ENOSYS; + + pair.key = key; + rc = hwprobe_func (&pair, 1, 0, NULL, 0); + if (rc != 0) + return rc; + + if (pair.key < 0) + return ENOENT; + + *value = pair.value; + return 0; +} + __END_DECLS #endif /* sys/hwprobe.h */ From patchwork Wed Jan 10 20:51:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evan Green X-Patchwork-Id: 83787 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 BDFF7386180D for ; Wed, 10 Jan 2024 20:53:28 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-il1-x131.google.com (mail-il1-x131.google.com [IPv6:2607:f8b0:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id E93A63860C3D for ; Wed, 10 Jan 2024 20:52:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E93A63860C3D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E93A63860C3D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704919933; cv=none; b=PqH4PGARi72yDHCiuWOJnTybSNaPTRBsLR/sCpkVSpQDEkKVBTDmgKlc73baaJsCG7Rqmr37en6DEQF/bLLRgsmb6fUbcLXghEo6XaARUej5ortdH3N07Vokhraw4imn65kPPRG3lp6r4ISPcylUXDANRTiwSNTm+oy3Np++MgE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704919933; c=relaxed/simple; bh=M22v+ZKz77sEfwldN5alhyIMZMql9QLmbuE0uZWzHgU=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=dk0cmd0Q/3TdXYAnWjcDbELQO1uhESKR7eS8Iu5+2ChIWkL1LLBEmMWGcV6bFPwMJsthes37rbGPJ51uEUf+q9wC4VUwKWmaWs5inmAp4rEpDDUINXt/w+NovdNqWnddyB2TEA5NR/ctsryML+1FMhFE4IqBKJbHB70Ppnw4zxI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x131.google.com with SMTP id e9e14a558f8ab-36074b286d6so25957575ab.1 for ; Wed, 10 Jan 2024 12:52:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1704919927; x=1705524727; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=M1WjxRgtlqeTVsBTUGgh48eVyYg592xv8z13qtwuTaA=; b=y9zGfK+fV5iiA/fVexbwzp9BDbexWr9+PYXMRwlpQOVIYhDnvdj9shL27EeaLsxKnA fwRdz1YN14RqRezemoBXG8c23coGnR3hNf/d7er0irkU2kbjISYb7/+mzhpy2qNxENbT cfNE4D+bKjHuW7v1JtGPBI6w6iHy5ZRX1wAjV6bXos0KlT1s0RTojuibRD/45RKVrSvL NJuMvHuH8G5ZrHkB1tDiIJ01JCEX8q0+9Icx0ITRld1KT6tugCcFJO+/m9vG7FpeY92C KcY/EWxkwxZDUB6vukAsISsljL94wKceNIi1yiZpyEM8rccy/O0FCbwUQvfovtWK0/sD RM3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704919927; x=1705524727; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=M1WjxRgtlqeTVsBTUGgh48eVyYg592xv8z13qtwuTaA=; b=ZZNz4V/2rz/d0tKe0uJSFqOw/ceDlDvt79J52Loz1H3LZPS9KsaLmGz17hjvg9Ewb2 9mwpZ8ce6SShBcWwLn5nsU39mMblut++40IkNTA5TW2NEYuyeKJxpUF3tFNg670uYYPQ yVy+rZyXylyHKZm5oazC57jvbVI0PfMOasya+U9vfF/pyYWsSyyQQL00WvWrm3rWa8Ex r99+vUqp5V9swr7c09x/HV7mkXup4uH7Sc/tCG+15gw9bGJr/aIXZXVSpWRolikZOipl N/t63euwV6WYrACBVDm4NQlh/yCfnHW9ikZNBISKv2Gr/8w4sne0wwDQ90OJtWXzhyey jSfw== X-Gm-Message-State: AOJu0YyLGBzO5t4rX2QOxDT21jrPf3dJ4+/HxPZzrVAz8i8RwDCBIaiX 4pYuP7GNs6G/J0pT5HoPbE79mMHMBPH1si7mAjRhzVw7Qqc= X-Google-Smtp-Source: AGHT+IHtjhMNdPcZHj0q2QhPmLU/rWjvxjYCiD2M+odmSsRhl7iYKefeXjgT834qGtrv9mDKic6xvQ== X-Received: by 2002:a05:6e02:1809:b0:35f:8205:7b06 with SMTP id a9-20020a056e02180900b0035f82057b06mr153898ilv.4.1704919927200; Wed, 10 Jan 2024 12:52:07 -0800 (PST) Received: from evan.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id y4-20020a92c984000000b0035ff9825323sm1452018iln.30.2024.01.10.12.52.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 12:52:06 -0800 (PST) From: Evan Green To: libc-alpha@sourceware.org Cc: palmer@rivosinc.com, vineetg@rivosinc.com, slewis@rivosinc.com, Florian Weimer , Evan Green Subject: [PATCH v11 7/7] riscv: Add and use alignment-ignorant memcpy Date: Wed, 10 Jan 2024 12:51:40 -0800 Message-Id: <20240110205140.754849-8-evan@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240110205140.754849-1-evan@rivosinc.com> References: <20240110205140.754849-1-evan@rivosinc.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 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.30 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 For CPU implementations that can perform unaligned accesses with little or no performance penalty, create a memcpy implementation that does not bother aligning buffers. It will use a block of integer registers, a single integer register, and fall back to bytewise copy for the remainder. Signed-off-by: Evan Green Reviewed-by: Palmer Dabbelt --- Changes in v11: - Update copyrights to 2024 (Adhemerval) - Avoid implicit check in select_memcpy_ifunc (Adhemerval) - Remove hidden_def (__memcpy_noalignment) (Adhemerval) Changes in v10: - One line per function in Makefile for memcpy (Adhemerval) - Space before argument-like things (Adhemerval) Changes in v7: - Use new helper function in memcpy ifunc selector (Richard) Changes in v6: - Fix a couple regressions in the assembly from v5 :/ - Use passed hwprobe pointer in memcpy ifunc selector. Changes in v5: - Do unaligned word access for final trailing bytes (Richard) Changes in v4: - Fixed comment style (Florian) Changes in v3: - Word align dest for large memcpy()s. - Add tags - Remove spurious blank line from sysdeps/riscv/memcpy.c Changes in v2: - Used _MASK instead of _FAST value itself. --- sysdeps/riscv/memcopy.h | 26 ++++ sysdeps/riscv/memcpy.c | 63 ++++++++ sysdeps/riscv/memcpy_noalignment.S | 136 ++++++++++++++++++ sysdeps/unix/sysv/linux/riscv/Makefile | 9 ++ .../unix/sysv/linux/riscv/memcpy-generic.c | 24 ++++ 5 files changed, 258 insertions(+) create mode 100644 sysdeps/riscv/memcopy.h create mode 100644 sysdeps/riscv/memcpy.c create mode 100644 sysdeps/riscv/memcpy_noalignment.S create mode 100644 sysdeps/unix/sysv/linux/riscv/memcpy-generic.c diff --git a/sysdeps/riscv/memcopy.h b/sysdeps/riscv/memcopy.h new file mode 100644 index 0000000000..27675964b0 --- /dev/null +++ b/sysdeps/riscv/memcopy.h @@ -0,0 +1,26 @@ +/* memcopy.h -- definitions for memory copy functions. RISC-V version. + Copyright (C) 2024 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 + +/* Redefine the generic memcpy implementation to __memcpy_generic, so + the memcpy ifunc can select between generic and special versions. + In rtld, don't bother with all the ifunciness. */ +#if IS_IN (libc) +#define MEMCPY __memcpy_generic +#endif diff --git a/sysdeps/riscv/memcpy.c b/sysdeps/riscv/memcpy.c new file mode 100644 index 0000000000..20f9548c44 --- /dev/null +++ b/sysdeps/riscv/memcpy.c @@ -0,0 +1,63 @@ +/* Multiple versions of memcpy. + All versions must be listed in ifunc-impl-list.c. + Copyright (C) 2017-2024 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 + . */ + +#if IS_IN (libc) +/* Redefine memcpy so that the compiler won't complain about the type + mismatch with the IFUNC selector in strong_alias, below. */ +# undef memcpy +# define memcpy __redirect_memcpy +# include +# include +# include +# include +# include + +# define INIT_ARCH() + +extern __typeof (__redirect_memcpy) __libc_memcpy; + +extern __typeof (__redirect_memcpy) __memcpy_generic attribute_hidden; +extern __typeof (__redirect_memcpy) __memcpy_noalignment attribute_hidden; + +static inline __typeof (__redirect_memcpy) * +select_memcpy_ifunc (uint64_t dl_hwcap, __riscv_hwprobe_t hwprobe_func) +{ + unsigned long long int value; + + INIT_ARCH (); + + if (__riscv_hwprobe_one (hwprobe_func, RISCV_HWPROBE_KEY_CPUPERF_0, &value) != 0) + return __memcpy_generic; + + if ((value & RISCV_HWPROBE_MISALIGNED_MASK) == RISCV_HWPROBE_MISALIGNED_FAST) + return __memcpy_noalignment; + + return __memcpy_generic; +} + +riscv_libc_ifunc (__libc_memcpy, select_memcpy_ifunc); + +# undef memcpy +strong_alias (__libc_memcpy, memcpy); +# ifdef SHARED +__hidden_ver1 (memcpy, __GI_memcpy, __redirect_memcpy) + __attribute__ ((visibility ("hidden"))) __attribute_copy__ (memcpy); +# endif + +#endif diff --git a/sysdeps/riscv/memcpy_noalignment.S b/sysdeps/riscv/memcpy_noalignment.S new file mode 100644 index 0000000000..621f8d028f --- /dev/null +++ b/sysdeps/riscv/memcpy_noalignment.S @@ -0,0 +1,136 @@ +/* memcpy for RISC-V, ignoring buffer alignment + Copyright (C) 2024 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 + +/* void *memcpy(void *, const void *, size_t) */ +ENTRY (__memcpy_noalignment) + move t6, a0 /* Preserve return value */ + + /* Bail if 0 */ + beqz a2, 7f + + /* Jump to byte copy if size < SZREG */ + li a4, SZREG + bltu a2, a4, 5f + + /* Round down to the nearest "page" size */ + andi a4, a2, ~((16*SZREG)-1) + beqz a4, 2f + add a3, a1, a4 + + /* Copy the first word to get dest word aligned */ + andi a5, t6, SZREG-1 + beqz a5, 1f + REG_L a6, (a1) + REG_S a6, (t6) + + /* Align dst up to a word, move src and size as well. */ + addi t6, t6, SZREG-1 + andi t6, t6, ~(SZREG-1) + sub a5, t6, a0 + add a1, a1, a5 + sub a2, a2, a5 + + /* Recompute page count */ + andi a4, a2, ~((16*SZREG)-1) + beqz a4, 2f + +1: + /* Copy "pages" (chunks of 16 registers) */ + REG_L a4, 0(a1) + REG_L a5, SZREG(a1) + REG_L a6, 2*SZREG(a1) + REG_L a7, 3*SZREG(a1) + REG_L t0, 4*SZREG(a1) + REG_L t1, 5*SZREG(a1) + REG_L t2, 6*SZREG(a1) + REG_L t3, 7*SZREG(a1) + REG_L t4, 8*SZREG(a1) + REG_L t5, 9*SZREG(a1) + REG_S a4, 0(t6) + REG_S a5, SZREG(t6) + REG_S a6, 2*SZREG(t6) + REG_S a7, 3*SZREG(t6) + REG_S t0, 4*SZREG(t6) + REG_S t1, 5*SZREG(t6) + REG_S t2, 6*SZREG(t6) + REG_S t3, 7*SZREG(t6) + REG_S t4, 8*SZREG(t6) + REG_S t5, 9*SZREG(t6) + REG_L a4, 10*SZREG(a1) + REG_L a5, 11*SZREG(a1) + REG_L a6, 12*SZREG(a1) + REG_L a7, 13*SZREG(a1) + REG_L t0, 14*SZREG(a1) + REG_L t1, 15*SZREG(a1) + addi a1, a1, 16*SZREG + REG_S a4, 10*SZREG(t6) + REG_S a5, 11*SZREG(t6) + REG_S a6, 12*SZREG(t6) + REG_S a7, 13*SZREG(t6) + REG_S t0, 14*SZREG(t6) + REG_S t1, 15*SZREG(t6) + addi t6, t6, 16*SZREG + bltu a1, a3, 1b + andi a2, a2, (16*SZREG)-1 /* Update count */ + +2: + /* Remainder is smaller than a page, compute native word count */ + beqz a2, 7f + andi a5, a2, ~(SZREG-1) + andi a2, a2, (SZREG-1) + add a3, a1, a5 + /* Jump directly to last word if no words. */ + beqz a5, 4f + +3: + /* Use single native register copy */ + REG_L a4, 0(a1) + addi a1, a1, SZREG + REG_S a4, 0(t6) + addi t6, t6, SZREG + bltu a1, a3, 3b + + /* Jump directly out if no more bytes */ + beqz a2, 7f + +4: + /* Copy the last word unaligned */ + add a3, a1, a2 + add a4, t6, a2 + REG_L a5, -SZREG(a3) + REG_S a5, -SZREG(a4) + ret + +5: + /* Copy bytes when the total copy is . */ + +#include + +extern __typeof (memcpy) __memcpy_generic; +hidden_proto (__memcpy_generic) + +#include