]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
aarch64: add __ifunc_hwcap function to be used in ifunc resolvers
authorYury Khrustalev <yury.khrustalev@arm.com>
Thu, 24 Apr 2025 15:58:46 +0000 (16:58 +0100)
committerYury Khrustalev <yury.khrustalev@arm.com>
Thu, 5 Jun 2025 13:38:51 +0000 (14:38 +0100)
Add a new helper function __ifunc_hwcap() as a portable way to
access HWCAP elements via the parameter(s) passed to an ifunc
resolver checking the _IFUNC_ARG_HWCAP bit in the first parameter
and size of the buffer in the second parameter.

Note that 0 is returned when the requested element is not available
or does not correspond to a valid AT_HWCAP{,2,...} value.

Also add relevant tests.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
sysdeps/aarch64/Makefile
sysdeps/aarch64/sys/ifunc.h
sysdeps/aarch64/tst-ifunc-arg-1.c
sysdeps/aarch64/tst-ifunc-arg-2.c
sysdeps/aarch64/tst-ifunc-arg-3.c [new file with mode: 0644]
sysdeps/aarch64/tst-ifunc-arg-4.c [new file with mode: 0644]

index 0fc6cf16932673118ab97711b2d7951d59945ba9..021a35682b7593285a3326dbe974a6a4bcf85531 100644 (file)
@@ -41,7 +41,12 @@ gen-as-const-headers += \
   dl-link.sym \
   rtld-global-offsets.sym
 
-tests-internal += tst-ifunc-arg-1 tst-ifunc-arg-2
+tests-internal += \
+  tst-ifunc-arg-1 \
+  tst-ifunc-arg-2 \
+  tst-ifunc-arg-3 \
+  tst-ifunc-arg-4 \
+  #tests-internal
 
 tests += tst-vpcs
 modules-names += tst-vpcs-mod
index 42d173195fd116882c197718ecae1837e2d8e786..a3322a9cfcfb4afc99de2ada57b46e124e782b10 100644 (file)
@@ -19,6 +19,8 @@
 #ifndef _SYS_IFUNC_H
 #define _SYS_IFUNC_H
 
+#include <sys/cdefs.h>
+
 /* A second argument is passed to the ifunc resolver.  */
 #define _IFUNC_ARG_HWCAP       (1ULL << 62)
 
@@ -61,4 +63,33 @@ struct __ifunc_arg_t
 
 typedef struct __ifunc_arg_t __ifunc_arg_t;
 
+/* Constants for IDs of HWCAP elements to be used with the
+   __ifunc_hwcap function below.  */
+enum
+{
+  _IFUNC_ARG_AT_HWCAP = 1,
+  _IFUNC_ARG_AT_HWCAP2 = 2,
+  _IFUNC_ARG_AT_HWCAP3 = 3,
+  _IFUNC_ARG_AT_HWCAP4 = 4,
+};
+
+/* A helper function to obtain HWCAP element by its ID from the
+   parameters ARG0 and ARG1 passed to the ifunc resolver.  Note that
+   ID 1 corresponds to AT_HWCAP, ID 2 corresponds to AT_HWCAP2, etc.
+   If there is no element available for the requested ID then 0 is
+   returned.  If ID doesn't much any supported AT_HWCAP{,2,...} value,
+   then 0 is also returned.  */
+static __inline unsigned long __attribute__ ((unused, always_inline))
+__ifunc_hwcap (unsigned long __id,
+              unsigned long __arg0, const unsigned long *__arg1)
+{
+  if (__glibc_likely (__arg0 & _IFUNC_ARG_HWCAP))
+    {
+      const unsigned long size = __arg1[0];
+      const unsigned long offset = __id * sizeof (unsigned long);
+      return offset < size && __id > 0 ? __arg1[__id] : 0;
+    }
+  return __id == 1 ? __arg0 : 0;
+}
+
 #endif
index 9cc3d40c48084439d459f5beeb6394ed5e51c4c6..292c5ae9e3708e66910c2c203f18bacbafcef4c6 100644 (file)
@@ -60,6 +60,18 @@ do_test (void)
   TEST_COMPARE (saved_arg2._hwcap3, getauxval (AT_HWCAP3));
   TEST_COMPARE (saved_arg2._hwcap4, getauxval (AT_HWCAP4));
 
+  const unsigned long *saved_arg2_ptr = (const unsigned long *)&saved_arg2;
+
+  TEST_COMPARE (__ifunc_hwcap (1, saved_arg1, saved_arg2_ptr),
+               getauxval (AT_HWCAP));
+  TEST_COMPARE (__ifunc_hwcap (2, saved_arg1, saved_arg2_ptr),
+                getauxval (AT_HWCAP2));
+  TEST_COMPARE (__ifunc_hwcap (3, saved_arg1, saved_arg2_ptr),
+                getauxval (AT_HWCAP3));
+  TEST_COMPARE (__ifunc_hwcap (4, saved_arg1, saved_arg2_ptr),
+                getauxval (AT_HWCAP4));
+
+
   return 0;
 }
 
index d9950f0e37a42666d261362ed426b33555d776f2..c05129a132aae9e8cf95d89761c57b0e0ba6b870 100644 (file)
@@ -63,6 +63,17 @@ do_test (void)
   TEST_COMPARE (saved_arg2._hwcap3, getauxval (AT_HWCAP3));
   TEST_COMPARE (saved_arg2._hwcap4, getauxval (AT_HWCAP4));
 
+  const unsigned long *saved_arg2_ptr = (const unsigned long *)&saved_arg2;
+
+  TEST_COMPARE (__ifunc_hwcap (1, saved_arg1, saved_arg2_ptr),
+                getauxval (AT_HWCAP));
+  TEST_COMPARE (__ifunc_hwcap (2, saved_arg1, saved_arg2_ptr),
+                getauxval (AT_HWCAP2));
+  TEST_COMPARE (__ifunc_hwcap (3, saved_arg1, saved_arg2_ptr),
+                getauxval (AT_HWCAP3));
+  TEST_COMPARE (__ifunc_hwcap (4, saved_arg1, saved_arg2_ptr),
+                getauxval (AT_HWCAP4));
+
   return 0;
 }
 
diff --git a/sysdeps/aarch64/tst-ifunc-arg-3.c b/sysdeps/aarch64/tst-ifunc-arg-3.c
new file mode 100644 (file)
index 0000000..49d8866
--- /dev/null
@@ -0,0 +1,97 @@
+/* Tests for __ifunc_hwcap helper function.
+   Copyright (C) 2025 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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <stdint.h>
+#include <sys/ifunc.h>
+#include <support/check.h>
+
+#define CHECK_VALUES_WITH_ARG(p1, p2, p3, p4) \
+  ({ \
+    TEST_COMPARE (__ifunc_hwcap (0, _IFUNC_ARG_HWCAP, arg), 0); \
+    TEST_COMPARE (__ifunc_hwcap (_IFUNC_ARG_AT_HWCAP, _IFUNC_ARG_HWCAP, arg), p1); \
+    TEST_COMPARE (__ifunc_hwcap (_IFUNC_ARG_AT_HWCAP2, _IFUNC_ARG_HWCAP, arg), p2); \
+    TEST_COMPARE (__ifunc_hwcap (_IFUNC_ARG_AT_HWCAP3, _IFUNC_ARG_HWCAP, arg), p3); \
+    TEST_COMPARE (__ifunc_hwcap (_IFUNC_ARG_AT_HWCAP4, _IFUNC_ARG_HWCAP, arg), p4); \
+    TEST_COMPARE (__ifunc_hwcap (5, _IFUNC_ARG_HWCAP, arg), 0); \
+  })
+
+#define CHECK_VALUES_WITHOUT_ARG(p1) \
+  ({ \
+    TEST_COMPARE (__ifunc_hwcap (0, p1, arg), 0); \
+    TEST_COMPARE (__ifunc_hwcap (_IFUNC_ARG_AT_HWCAP, p1, arg), p1); \
+    TEST_COMPARE (__ifunc_hwcap (_IFUNC_ARG_AT_HWCAP2, p1, arg), 0); \
+    TEST_COMPARE (__ifunc_hwcap (_IFUNC_ARG_AT_HWCAP3, p1, arg), 0); \
+    TEST_COMPARE (__ifunc_hwcap (_IFUNC_ARG_AT_HWCAP4, p1, arg), 0); \
+    TEST_COMPARE (__ifunc_hwcap (5, p1, arg), 0); \
+  })
+
+static void
+test_one (const unsigned long *arg)
+{
+  uint64_t size = arg[0] / sizeof (uint64_t);
+
+  switch (size)
+    {
+      case 1:
+       CHECK_VALUES_WITH_ARG (0, 0, 0, 0);
+       CHECK_VALUES_WITHOUT_ARG (0);
+       break;
+      case 2:
+       CHECK_VALUES_WITH_ARG (1, 0, 0, 0);
+       CHECK_VALUES_WITHOUT_ARG (1);
+       break;
+      case 3:
+       CHECK_VALUES_WITH_ARG (1, 2, 0, 0);
+       CHECK_VALUES_WITHOUT_ARG (1);
+       break;
+      case 4:
+       CHECK_VALUES_WITH_ARG (1, 2, 3, 0);
+       CHECK_VALUES_WITHOUT_ARG (1);
+       break;
+      case 5:
+       CHECK_VALUES_WITH_ARG (1, 2, 3, 4);
+       CHECK_VALUES_WITHOUT_ARG (1);
+       break;
+      default:
+       TEST_VERIFY (0); // unexpected size
+       break;
+    }
+}
+
+static int
+do_test (void)
+{
+  uint64_t arg[_IFUNC_HWCAP_MAX + 1] = {
+    0, /* Placeholder for size */
+    _IFUNC_ARG_AT_HWCAP, /* AT_HWCAP */
+    _IFUNC_ARG_AT_HWCAP2, /* AT_HWCAP2 */
+    _IFUNC_ARG_AT_HWCAP3, /* AT_HWCAP3 */
+    _IFUNC_ARG_AT_HWCAP4, /* AT_HWCAP4 */
+  };
+
+  for (int k = 0; k <= _IFUNC_HWCAP_MAX; k++)
+    {
+      /* Update size */
+      arg[0] = (k + 1) * sizeof (uint64_t);
+      test_one (arg);
+    }
+
+  return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/sysdeps/aarch64/tst-ifunc-arg-4.c b/sysdeps/aarch64/tst-ifunc-arg-4.c
new file mode 100644 (file)
index 0000000..c95ef9e
--- /dev/null
@@ -0,0 +1,67 @@
+/* Test for ifunc resolver that uses __ifunc_hwcap helper function.
+   Copyright (C) 2025 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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <sys/auxv.h>
+#include <sys/ifunc.h>
+#include <support/check.h>
+
+static int
+one (void)
+{
+  return 1;
+}
+
+static int
+two (void)
+{
+  return 2;
+}
+
+/* Resolver function.  */
+static void *
+resolver (uint64_t arg0, const uint64_t arg1[])
+{
+  uint64_t hwcap2 = __ifunc_hwcap (_IFUNC_ARG_AT_HWCAP2, arg0, arg1);
+  if (hwcap2 & HWCAP2_POE)
+    return (void *)one;
+  else
+    return (void *)two;
+}
+
+/* An extern visible ifunc symbol.  */
+int fun (void) __attribute__((ifunc ("resolver")));
+
+static int
+do_test (void)
+{
+  if (getauxval (AT_HWCAP2) & HWCAP2_POE)
+    {
+      printf ("using 1st implementation\n");
+      TEST_VERIFY (fun () == 1);
+    }
+  else
+    {
+      printf ("using 2nd implementation\n");
+      TEST_VERIFY (fun () == 2);
+    }
+  return 0;
+}
+
+#include <support/test-driver.c>