From patchwork Wed Jul 12 19:36:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evan Green X-Patchwork-Id: 72572 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 760BA3856DC0 for ; Wed, 12 Jul 2023 19:37:23 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by sourceware.org (Postfix) with ESMTPS id 38B4D3857716 for ; Wed, 12 Jul 2023 19:36:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 38B4D3857716 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-pf1-x42f.google.com with SMTP id d2e1a72fcca58-666eec46206so6568037b3a.3 for ; Wed, 12 Jul 2023 12:36:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1689190605; x=1691782605; 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=2v+4hVuFPCcPSWwcGERx7u1WLPoLmXsH49B9YcmvQio=; b=F0zXw5ibNJsza9YjiSVY0c4JIsu35hOH5a4BOgn/Xt3QaYN1LNV0r21MNwJdSK7Toy e1LbA5jp/kBtqUE/GWs93xCHTELnm9WnzPj9H3eFs7Y+ctpMnWQ8k78KHlDKvkrVjgAP 8N5zyYtWPe6WBOa83gWg96NprV7lIwddC6Q5Nqzsk0xrSbkqtryMlaU7XEOTW2kO465Q YkXfLkKE9b0y6CDavUs6wxQr/Vp2nx2uGf/JW2AHjg6/uKKkWS4bWZsbWR0hb14pjZ48 0B9h27QVfVqdjyulRzn66+Apsyc+j6ceTNo+Smm9tlK/g3h6ex6o1ofXGXEiia4/AnGK nnLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689190605; x=1691782605; 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=2v+4hVuFPCcPSWwcGERx7u1WLPoLmXsH49B9YcmvQio=; b=OsqYMo3MjO8zdQfq6wpJCTWIHZl2uYAC5n3aenzY2Cekceo+qQlU1OyqxCNfDzsyBQ Cs2gq8rvgnVH4tJu+CGfx/QNVWYtIq/SlNgwQoXAubZ+XlkPMgTQu4XOLmaSfBdBNyCs d5qlafHyz7M1B8wJLcgkme5bYDS3TuR6n+HPUWHDoIRlIhAYRNqZ6ZJBu6IEeJQU/Nnb wH2F58EOqf1es0Ueu/z69kONLtVkwvEX9OTHUXvhy+kNsGEeOdS7KHs5bb4NNg/tpUEt d/nFoqKnkVGLgP63TiXtitlayxgmqXY2UuVRFpmjpVSjdwiyHWQ6GDvYMW3bOSTEZzBc 5vFg== X-Gm-Message-State: ABy/qLYiAd+DtE0+FA23B80+upGiC3TLFaIBX7iMgUSAjHW2s6nOssyo uPM/F0ActsUVL+w/wygi7ORmemStfsaCMKXvP34= X-Google-Smtp-Source: APBJJlGRwdI2TTPZ6PlC47TFKOYIt7UX+MdDjfiSWoKCc7BKJXOTgpmTqpVzOlYsD60iUa5JQ9eUsg== X-Received: by 2002:a05:6a20:543:b0:12f:dc31:a71e with SMTP id 3-20020a056a20054300b0012fdc31a71emr15605416pzq.56.1689190604957; Wed, 12 Jul 2023 12:36:44 -0700 (PDT) Received: from evan.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id h20-20020aa786d4000000b0065dd1e7c2c1sm3983141pfo.63.2023.07.12.12.36.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jul 2023 12:36:44 -0700 (PDT) From: Evan Green To: libc-alpha@sourceware.org Cc: vineetg@rivosinc.com, slewis@rivosinc.com, palmer@rivosinc.com, Florian Weimer , Evan Green Subject: [PATCH v5 3/4] riscv: Add ifunc-compatible hwprobe function Date: Wed, 12 Jul 2023 12:36:27 -0700 Message-Id: <20230712193629.2880253-4-evan@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230712193629.2880253-1-evan@rivosinc.com> References: <20230712193629.2880253-1-evan@rivosinc.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.0 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" ifunc selector functions are called very early, and are not able to call external functions that require relocation. This presents a problem for applications and libraries outside of glibc that may want to use __riscv_hwprobe() in their ifunc selectors. Add a new function, __riscv_hwprobe_early(), that is compiled into libc_nonshared.a and is safe to use in application and library ifunc selectors. The _early() variant checks a weak alias of __riscv_hwprobe, __riscv_hwprobe_weak, to see if the symbol is available, and simply calls that if so. However if it's called before the symbol is available, it makes the system call directly. The early call has some caveats, primarily that it does not take advantage of the cached vDSO data, and does not set errno (another external symbol) upon failing. The original dynamic __riscv_hwprobe() function is still available for users that don't need the overhead of handling early init cases. Signed-off-by: Evan Green --- Changes in v5: - Introduced __riscv_hwprobe_early() sysdeps/unix/sysv/linux/riscv/Makefile | 3 +- sysdeps/unix/sysv/linux/riscv/Versions | 1 + sysdeps/unix/sysv/linux/riscv/hwprobe.c | 2 ++ .../unix/sysv/linux/riscv/hwprobe_static.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 | 21 +++++++++++ 7 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 sysdeps/unix/sysv/linux/riscv/hwprobe_static.c diff --git a/sysdeps/unix/sysv/linux/riscv/Makefile b/sysdeps/unix/sysv/linux/riscv/Makefile index 45cc29e40d..fd8fd48ade 100644 --- a/sysdeps/unix/sysv/linux/riscv/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/Makefile @@ -1,6 +1,7 @@ ifeq ($(subdir),misc) sysdep_headers += sys/cachectl.h sys/hwprobe.h -sysdep_routines += flush-icache hwprobe +sysdep_routines += flush-icache hwprobe hwprobe_static +static-only-routines += hwprobe_static endif ifeq ($(subdir),stdlib) diff --git a/sysdeps/unix/sysv/linux/riscv/Versions b/sysdeps/unix/sysv/linux/riscv/Versions index 0c4016382d..571d4c2046 100644 --- a/sysdeps/unix/sysv/linux/riscv/Versions +++ b/sysdeps/unix/sysv/linux/riscv/Versions @@ -10,5 +10,6 @@ libc { } GLIBC_2.38 { __riscv_hwprobe; + __riscv_hwprobe_weak; } } diff --git a/sysdeps/unix/sysv/linux/riscv/hwprobe.c b/sysdeps/unix/sysv/linux/riscv/hwprobe.c index 14f7136998..a8bd63fca4 100644 --- a/sysdeps/unix/sysv/linux/riscv/hwprobe.c +++ b/sysdeps/unix/sysv/linux/riscv/hwprobe.c @@ -29,3 +29,5 @@ int __riscv_hwprobe (struct riscv_hwprobe *pairs, size_t pair_count, /* The vDSO may be able to provide the answer without a syscall. */ return INLINE_VSYSCALL(riscv_hwprobe, 5, pairs, pair_count, cpu_count, cpus, flags); } + +weak_alias(__riscv_hwprobe, __riscv_hwprobe_weak) diff --git a/sysdeps/unix/sysv/linux/riscv/hwprobe_static.c b/sysdeps/unix/sysv/linux/riscv/hwprobe_static.c new file mode 100644 index 0000000000..b1c0f4089f --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/hwprobe_static.c @@ -0,0 +1,36 @@ +/* RISC-V hardware feature probing support on Linux, ifunc-compatible + 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 + +attribute_hidden +int __riscv_hwprobe_early (struct riscv_hwprobe *pairs, size_t pair_count, + size_t cpu_count, unsigned long int *cpus, + unsigned int flags) +{ + if (!__riscv_hwprobe_weak) { + /* Use INTERNAL_SYSCALL since errno is not available. */ + return INTERNAL_SYSCALL(riscv_hwprobe, 5, pairs, pair_count, cpu_count, cpus, flags); + } + + return __riscv_hwprobe_weak(pairs, pair_count, cpu_count, cpus, flags); +} diff --git a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist index 8fab4a606f..942e397be0 100644 --- a/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist +++ b/sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist @@ -2437,3 +2437,4 @@ GLIBC_2.38 strlcpy F GLIBC_2.38 wcslcat F GLIBC_2.38 wcslcpy F GLIBC_2.38 __riscv_hwprobe F +GLIBC_2.38 __riscv_hwprobe_weak F diff --git a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist index 1ebb91deed..bc140fa7df 100644 --- a/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist +++ b/sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist @@ -2637,3 +2637,4 @@ GLIBC_2.38 strlcpy F GLIBC_2.38 wcslcat F GLIBC_2.38 wcslcpy F GLIBC_2.38 __riscv_hwprobe F +GLIBC_2.38 __riscv_hwprobe_weak F diff --git a/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h b/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h index b27af5cb07..d739371806 100644 --- a/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h +++ b/sysdeps/unix/sysv/linux/riscv/sys/hwprobe.h @@ -67,6 +67,27 @@ 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); +extern int __riscv_hwprobe_weak (struct riscv_hwprobe *pairs, size_t pair_count, + size_t cpu_count, unsigned long int *cpus, + unsigned int flags) + __THROW __nonnull ((1)) __wur + __fortified_attr_access (__read_write__, 1, 2) + __fortified_attr_access (__read_only__, 4, 3); + +# ifdef weak_extern +weak_extern (__riscv_hwprobe_weak) +# else +# pragma weak __riscv_hwprobe_weak +# endif + +extern int __riscv_hwprobe_early (struct riscv_hwprobe *pairs, size_t pair_count, + size_t cpu_count, unsigned long int *cpus, + unsigned int flags) + __THROW __nonnull ((1)) __wur + __fortified_attr_access (__read_write__, 1, 2) + __fortified_attr_access (__read_only__, 4, 3) + __attribute__ ((__visibility__ ("hidden"))); + __END_DECLS #endif /* sys/hwprobe.h */