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