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