dfa500
commit a803367bab167f5ec4fde1f0d0ec447707c29520
dfa500
Author: Florian Weimer <fweimer@redhat.com>
dfa500
Date:   Fri Feb 14 20:55:39 2020 +0100
dfa500
dfa500
    powerpc64: Add memory protection key support [BZ #23202]
dfa500
    
dfa500
    The 32-bit protection key behavior is somewhat unclear on 32-bit powerpc,
dfa500
    so this change is restricted to the 64-bit variants.
dfa500
    
dfa500
    Flag translation is needed because of hardware differences between the
dfa500
    POWER implementation (read and write flags) and the Intel implementation
dfa500
    (write and read+write flags).
dfa500
dfa500
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/arch-pkey.h b/sysdeps/unix/sysv/linux/powerpc/powerpc64/arch-pkey.h
dfa500
new file mode 100644
dfa500
index 0000000000000000..623b073d5a585d51
dfa500
--- /dev/null
dfa500
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/arch-pkey.h
dfa500
@@ -0,0 +1,55 @@
dfa500
+/* Helper functions for manipulating memory protection keys, for powerpc64.
dfa500
+   Copyright (C) 2017-2020 Free Software Foundation, Inc.
dfa500
+   This file is part of the GNU C Library.
dfa500
+
dfa500
+   The GNU C Library is free software; you can redistribute it and/or
dfa500
+   modify it under the terms of the GNU Lesser General Public
dfa500
+   License as published by the Free Software Foundation; either
dfa500
+   version 2.1 of the License, or (at your option) any later version.
dfa500
+
dfa500
+   The GNU C Library is distributed in the hope that it will be useful,
dfa500
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
dfa500
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
dfa500
+   Lesser General Public License for more details.
dfa500
+
dfa500
+   You should have received a copy of the GNU Lesser General Public
dfa500
+   License along with the GNU C Library; if not, see
dfa500
+   <http://www.gnu.org/licenses/>.  */
dfa500
+
dfa500
+#ifndef _ARCH_PKEY_H
dfa500
+#define _ARCH_PKEY_H
dfa500
+
dfa500
+/* Read and write access bits in the AMR register.  Needs to be
dfa500
+   translated from and to PKEY_DISABLE_* flags.  */
dfa500
+#define PKEY_AMR_READ 1UL
dfa500
+#define PKEY_AMR_WRITE 2UL
dfa500
+
dfa500
+/* Return the value of the AMR register.  */
dfa500
+static inline unsigned long int
dfa500
+pkey_read (void)
dfa500
+{
dfa500
+  unsigned long int result;
dfa500
+  __asm__ volatile ("mfspr %0, 13" : "=r" (result));
dfa500
+  return result;
dfa500
+}
dfa500
+
dfa500
+/* Overwrite the AMR register with VALUE.  */
dfa500
+static inline void
dfa500
+pkey_write (unsigned long int value)
dfa500
+{
dfa500
+  __asm__ volatile ("mtspr 13, %0" : : "r" (value));
dfa500
+}
dfa500
+
dfa500
+/* Number of the largest supported key.  This depends on the width of
dfa500
+   the AMR register.  */
dfa500
+#define PKEY_MAX (sizeof (unsigned long int) * 8 / 2 - 1)
dfa500
+_Static_assert (PKEY_MAX == 15 || PKEY_MAX == 31, "PKEY_MAX value");
dfa500
+
dfa500
+/* Translate key number into AMR index position.  */
dfa500
+static inline int
dfa500
+pkey_index (int key)
dfa500
+{
dfa500
+  return 2 * (PKEY_MAX - key);
dfa500
+}
dfa500
+
dfa500
+#endif /* _ARCH_PKEY_H */
dfa500
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/pkey_get.c b/sysdeps/unix/sysv/linux/powerpc/powerpc64/pkey_get.c
dfa500
new file mode 100644
dfa500
index 0000000000000000..856ba061b90eabd2
dfa500
--- /dev/null
dfa500
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/pkey_get.c
dfa500
@@ -0,0 +1,42 @@
dfa500
+/* Reading the per-thread memory protection key, powerpc64 version.
dfa500
+   Copyright (C) 2017-2020 Free Software Foundation, Inc.
dfa500
+   This file is part of the GNU C Library.
dfa500
+
dfa500
+   The GNU C Library is free software; you can redistribute it and/or
dfa500
+   modify it under the terms of the GNU Lesser General Public
dfa500
+   License as published by the Free Software Foundation; either
dfa500
+   version 2.1 of the License, or (at your option) any later version.
dfa500
+
dfa500
+   The GNU C Library is distributed in the hope that it will be useful,
dfa500
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
dfa500
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
dfa500
+   Lesser General Public License for more details.
dfa500
+
dfa500
+   You should have received a copy of the GNU Lesser General Public
dfa500
+   License along with the GNU C Library; if not, see
dfa500
+   <http://www.gnu.org/licenses/>.  */
dfa500
+
dfa500
+#include <arch-pkey.h>
dfa500
+#include <errno.h>
dfa500
+#include <sys/mman.h>
dfa500
+
dfa500
+int
dfa500
+pkey_get (int key)
dfa500
+{
dfa500
+  if (key < 0 || key > PKEY_MAX)
dfa500
+    {
dfa500
+      __set_errno (EINVAL);
dfa500
+      return -1;
dfa500
+    }
dfa500
+  unsigned int index = pkey_index (key);
dfa500
+  unsigned long int amr = pkey_read ();
dfa500
+  unsigned int bits = (amr >> index) & 3;
dfa500
+
dfa500
+  /* Translate from AMR values.  PKEY_AMR_READ standing alone is not
dfa500
+     currently representable.  */
dfa500
+  if (bits & PKEY_AMR_READ)
dfa500
+    return PKEY_DISABLE_ACCESS;
dfa500
+  else if (bits == PKEY_AMR_WRITE)
dfa500
+    return PKEY_DISABLE_WRITE;
dfa500
+  return 0;
dfa500
+}
dfa500
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/pkey_set.c b/sysdeps/unix/sysv/linux/powerpc/powerpc64/pkey_set.c
dfa500
new file mode 100644
dfa500
index 0000000000000000..20b372ee2983abd5
dfa500
--- /dev/null
dfa500
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/pkey_set.c
dfa500
@@ -0,0 +1,48 @@
dfa500
+/* Changing the per-thread memory protection key, powerpc64 version.
dfa500
+   Copyright (C) 2017-2020 Free Software Foundation, Inc.
dfa500
+   This file is part of the GNU C Library.
dfa500
+
dfa500
+   The GNU C Library is free software; you can redistribute it and/or
dfa500
+   modify it under the terms of the GNU Lesser General Public
dfa500
+   License as published by the Free Software Foundation; either
dfa500
+   version 2.1 of the License, or (at your option) any later version.
dfa500
+
dfa500
+   The GNU C Library is distributed in the hope that it will be useful,
dfa500
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
dfa500
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
dfa500
+   Lesser General Public License for more details.
dfa500
+
dfa500
+   You should have received a copy of the GNU Lesser General Public
dfa500
+   License along with the GNU C Library; if not, see
dfa500
+   <http://www.gnu.org/licenses/>.  */
dfa500
+
dfa500
+#include <arch-pkey.h>
dfa500
+#include <errno.h>
dfa500
+#include <sys/mman.h>
dfa500
+
dfa500
+int
dfa500
+pkey_set (int key, unsigned int rights)
dfa500
+{
dfa500
+  if (key < 0 || key > PKEY_MAX || rights > 3)
dfa500
+    {
dfa500
+      __set_errno (EINVAL);
dfa500
+      return -1;
dfa500
+    }
dfa500
+
dfa500
+  /* Translate to AMR bit values.  */
dfa500
+  unsigned long int bits;
dfa500
+  if (rights & PKEY_DISABLE_ACCESS)
dfa500
+    /* The PKEY_DISABLE_WRITE bit does not matter.  */
dfa500
+    bits = PKEY_AMR_READ | PKEY_AMR_WRITE;
dfa500
+  else if (rights == PKEY_DISABLE_WRITE)
dfa500
+    bits = PKEY_AMR_WRITE;
dfa500
+  else
dfa500
+    bits = 0;
dfa500
+
dfa500
+  unsigned int index = pkey_index (key);
dfa500
+  unsigned long int mask = 3UL << index;
dfa500
+  unsigned long int amr = pkey_read ();
dfa500
+  amr = (amr & ~mask) | (bits << index);
dfa500
+  pkey_write (amr);
dfa500
+  return 0;
dfa500
+}