Blame sysdeps/unix/sysv/linux/powerpc/powerpc64/pkey_set.c

Packit Bot 4f7983
/* Changing the per-thread memory protection key, powerpc64 version.
Packit Bot 4f7983
   Copyright (C) 2017-2020 Free Software Foundation, Inc.
Packit Bot 4f7983
   This file is part of the GNU C Library.
Packit Bot 4f7983
Packit Bot 4f7983
   The GNU C Library is free software; you can redistribute it and/or
Packit Bot 4f7983
   modify it under the terms of the GNU Lesser General Public
Packit Bot 4f7983
   License as published by the Free Software Foundation; either
Packit Bot 4f7983
   version 2.1 of the License, or (at your option) any later version.
Packit Bot 4f7983
Packit Bot 4f7983
   The GNU C Library is distributed in the hope that it will be useful,
Packit Bot 4f7983
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Bot 4f7983
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Bot 4f7983
   Lesser General Public License for more details.
Packit Bot 4f7983
Packit Bot 4f7983
   You should have received a copy of the GNU Lesser General Public
Packit Bot 4f7983
   License along with the GNU C Library; if not, see
Packit Bot 4f7983
   <http://www.gnu.org/licenses/>.  */
Packit Bot 4f7983
Packit Bot 4f7983
#include <arch-pkey.h>
Packit Bot 4f7983
#include <errno.h>
Packit Bot 4f7983
#include <sys/mman.h>
Packit Bot 4f7983
Packit Bot 4f7983
int
Packit Bot 4f7983
pkey_set (int key, unsigned int rights)
Packit Bot 4f7983
{
Packit Bot 4f7983
  if (key < 0 || key > PKEY_MAX || rights > 3)
Packit Bot 4f7983
    {
Packit Bot 4f7983
      __set_errno (EINVAL);
Packit Bot 4f7983
      return -1;
Packit Bot 4f7983
    }
Packit Bot 4f7983
Packit Bot 4f7983
  /* Translate to AMR bit values.  */
Packit Bot 4f7983
  unsigned long int bits;
Packit Bot 4f7983
  if (rights & PKEY_DISABLE_ACCESS)
Packit Bot 4f7983
    /* The PKEY_DISABLE_WRITE bit does not matter.  */
Packit Bot 4f7983
    bits = PKEY_AMR_READ | PKEY_AMR_WRITE;
Packit Bot 4f7983
  else if (rights == PKEY_DISABLE_WRITE)
Packit Bot 4f7983
    bits = PKEY_AMR_WRITE;
Packit Bot 4f7983
  else
Packit Bot 4f7983
    bits = 0;
Packit Bot 4f7983
Packit Bot 4f7983
  unsigned int index = pkey_index (key);
Packit Bot 4f7983
  unsigned long int mask = 3UL << index;
Packit Bot 4f7983
  unsigned long int amr = pkey_read ();
Packit Bot 4f7983
  amr = (amr & ~mask) | (bits << index);
Packit Bot 4f7983
  pkey_write (amr);
Packit Bot 4f7983
  return 0;
Packit Bot 4f7983
}