]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
aarch64: GCS: use internal struct in __alloc_gcs
authorYury Khrustalev <yury.khrustalev@arm.com>
Wed, 16 Apr 2025 18:08:47 +0000 (19:08 +0100)
committerYury Khrustalev <yury.khrustalev@arm.com>
Wed, 18 Jun 2025 08:37:13 +0000 (09:37 +0100)
No functional change here, just a small refactoring to simplify
using __alloc_gcs() for allocating shadow stacks.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
sysdeps/aarch64/__alloc_gcs.c
sysdeps/aarch64/aarch64-gcs.h
sysdeps/unix/sysv/linux/aarch64/makecontext.c

index e70b4594a2638d723f090801e00ec1407628fe87..b98e5fcdbad64fe251863755e9f6b3434dba62ec 100644 (file)
@@ -15,6 +15,8 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include "aarch64-gcs.h"
+
 #include <sysdep.h>
 #include <unistd.h>
 #include <sys/mman.h>
@@ -34,7 +36,7 @@ map_shadow_stack (void *addr, size_t size, unsigned long flags)
 #define GCS_ALTSTACK_RESERVE 160
 
 void *
-__alloc_gcs (size_t stack_size, void **ss_base, size_t *ss_size)
+__alloc_gcs (size_t stack_size, struct gcs_record *gcs)
 {
   size_t size = (stack_size / 2 + GCS_ALTSTACK_RESERVE) & -8UL;
   if (size > GCS_MAX_SIZE)
@@ -45,9 +47,6 @@ __alloc_gcs (size_t stack_size, void **ss_base, size_t *ss_size)
   if (base == MAP_FAILED)
     return NULL;
 
-  *ss_base = base;
-  *ss_size = size;
-
   uint64_t *gcsp = (uint64_t *) ((char *) base + size);
   /* Skip end of GCS token.  */
   gcsp--;
@@ -58,6 +57,14 @@ __alloc_gcs (size_t stack_size, void **ss_base, size_t *ss_size)
       __munmap (base, size);
       return NULL;
     }
+
+  if (gcs != NULL)
+    {
+      gcs->gcs_base = base;
+      gcs->gcs_token = gcsp;
+      gcs->gcs_size = size;
+    }
+
   /* Return the target GCS pointer for context switch.  */
   return gcsp + 1;
 }
index 162ef187268e51a9316a1dfb20c21b461a4348a9..8e253ed32fd7a5c44ebedef159ac7da900897f91 100644 (file)
 #include <stddef.h>
 #include <stdbool.h>
 
-void *__alloc_gcs (size_t, void **, size_t *) attribute_hidden;
+struct gcs_record
+{
+  void *gcs_base;
+  void *gcs_token;
+  size_t gcs_size;
+};
+
+void *__alloc_gcs (size_t, struct gcs_record *) attribute_hidden;
+
+static inline bool
+has_gcs (void)
+{
+  register unsigned long x16 asm ("x16") = 1;
+  asm ("hint   40" /* chkfeat x16 */ : "+r" (x16));
+  return x16 == 0;
+}
 
 #endif
index a2eab9efc6cf1076ce99069a1917d51c5b7bfb61..4485723cfb5386fe21f267c5bb0c61f4b8afd03c 100644 (file)
@@ -36,9 +36,7 @@ static struct _aarch64_ctx *extension (void *p)
 static void *
 alloc_makecontext_gcs (size_t stack_size)
 {
-  void *base;
-  size_t size;
-  void *gcsp = __alloc_gcs (stack_size, &base, &size);
+  void *gcsp = __alloc_gcs (stack_size, NULL);
   if (gcsp == NULL)
     /* ENOSYS, bad size or OOM.  */
     abort ();