]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
aarch64: Add sysv specific enabling code for memory tagging
authorRichard Earnshaw <rearnsha@arm.com>
Fri, 20 Nov 2020 17:20:10 +0000 (17:20 +0000)
committerRichard Earnshaw <rearnsha@arm.com>
Fri, 20 Nov 2020 18:13:52 +0000 (18:13 +0000)
Add various defines and stubs for enabling MTE on AArch64 sysv-like
systems such as Linux.  The HWCAP feature bit is copied over in the
same way as other feature bits.  Similarly we add a new wrapper header
for mman.h to define the PROT_MTE flag that can be used with mmap and
related functions.

We add a new field to struct cpu_features that can be used, for
example, to check whether or not certain ifunc'd routines should be
bound to MTE-safe versions.

Finally, if we detect that MTE should be enabled (ie via the glibc
tunable); we enable MTE during startup as required.

sysdeps/unix/sysv/linux/aarch64/bits/hwcap.h
sysdeps/unix/sysv/linux/aarch64/bits/mman.h
sysdeps/unix/sysv/linux/aarch64/cpu-features.c
sysdeps/unix/sysv/linux/aarch64/cpu-features.h

index af90d8a626e17fcb667ac944c94366eec61c121d..389852f1d97bdad24eee052d41dcb6ce4f0946b1 100644 (file)
@@ -73,3 +73,4 @@
 #define HWCAP2_DGH             (1 << 15)
 #define HWCAP2_RNG             (1 << 16)
 #define HWCAP2_BTI             (1 << 17)
+#define HWCAP2_MTE             (1 << 18)
index ecae04634495ec5987b6e5a0f0b6ee84f51eb1dd..3658b958b531fc374146360ebbf06f4ab8f2ec0c 100644 (file)
@@ -1,4 +1,5 @@
 /* Definitions for POSIX memory map interface.  Linux/AArch64 version.
+
    Copyright (C) 2020 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
 
 #define PROT_BTI       0x10
 
+/* The following definitions basically come from the kernel headers.
+   But the kernel header is not namespace clean.  */
+
+/* Other flags.  */
+#define PROT_MTE       0x20            /* Normal Tagged mapping.  */
+
 #include <bits/mman-map-flags-generic.h>
 
 /* Include generic Linux declarations.  */
index b9ab827acafb9c62521437bb59fa89ca73e2a8fa..aa4a82c6e81c86804dba6b9ac95ecf7e91f8eea1 100644 (file)
@@ -19,6 +19,7 @@
 #include <cpu-features.h>
 #include <sys/auxv.h>
 #include <elf/dl-hwcaps.h>
+#include <sys/prctl.h>
 
 #define DCZID_DZP_MASK (1 << 4)
 #define DCZID_BS_MASK (0xf)
@@ -86,4 +87,31 @@ init_cpu_features (struct cpu_features *cpu_features)
 
   /* Check if BTI is supported.  */
   cpu_features->bti = GLRO (dl_hwcap2) & HWCAP2_BTI;
+
+  /* Setup memory tagging support if the HW and kernel support it, and if
+     the user has requested it.  */
+  cpu_features->mte_state = 0;
+
+#ifdef _LIBC_MTAG
+# if HAVE_TUNABLES
+  int mte_state = TUNABLE_GET (glibc, memtag, enable, unsigned, 0);
+  cpu_features->mte_state = (GLRO (dl_hwcap2) & HWCAP2_MTE) ? mte_state : 0;
+  /* If we lack the MTE feature, disable the tunable, since it will
+     otherwise cause instructions that won't run on this CPU to be used.  */
+  TUNABLE_SET (glibc, memtag, enable, unsigned, cpu_features->mte_state);
+# endif
+
+  /* For now, disallow tag 0, so that we can clearly see when tagged
+     addresses are being allocated.  */
+  if (cpu_features->mte_state & 2)
+    __prctl (PR_SET_TAGGED_ADDR_CTRL,
+            (PR_TAGGED_ADDR_ENABLE | PR_MTE_TCF_SYNC
+             | (0xfffe << PR_MTE_TAG_SHIFT)),
+            0, 0, 0);
+  else if (cpu_features->mte_state)
+    __prctl (PR_SET_TAGGED_ADDR_CTRL,
+            (PR_TAGGED_ADDR_ENABLE | PR_MTE_TCF_ASYNC
+             | (0xfffe << PR_MTE_TAG_SHIFT)),
+            0, 0, 0);
+#endif
 }
index 00a4d0c8e75f5554120da6da9c079ed65904d394..838d5c9aba5a15026a3725ad1cbc6b1fb2485ae8 100644 (file)
@@ -70,6 +70,7 @@ struct cpu_features
   uint64_t midr_el1;
   unsigned zva_size;
   bool bti;
+  unsigned mte_state;
 };
 
 #endif /* _CPU_FEATURES_AARCH64_H  */