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 */