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

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