public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Palmer Dabbelt <palmer@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc] riscv: Add ifunc helper method to hwprobe.h
Date: Fri,  1 Mar 2024 15:17:55 +0000 (GMT)	[thread overview]
Message-ID: <20240301151755.10385385841A@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=a2b47f7d4679a01d56827e1aff2a40de173fab23

commit a2b47f7d4679a01d56827e1aff2a40de173fab23
Author: Evan Green <evan@rivosinc.com>
Date:   Tue Feb 27 14:56:42 2024 -0800

    riscv: Add ifunc helper method to hwprobe.h
    
    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 <evan@rivosinc.com>
    Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>

Diff:
---
 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 <features.h>
 #include <stddef.h>
+#include <errno.h>
 #ifdef __has_include
 # if __has_include (<asm/hwprobe.h>)
 #  include <asm/hwprobe.h>
@@ -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 */

                 reply	other threads:[~2024-03-01 15:17 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240301151755.10385385841A@sourceware.org \
    --to=palmer@sourceware.org \
    --cc=glibc-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).