pkeys.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright 2020, Sandipan Das, IBM Corp.
  4. */
  5. #ifndef _SELFTESTS_POWERPC_PKEYS_H
  6. #define _SELFTESTS_POWERPC_PKEYS_H
  7. #include <sys/mman.h>
  8. #include "reg.h"
  9. #include "utils.h"
  10. /*
  11. * Older versions of libc use the Intel-specific access rights.
  12. * Hence, override the definitions as they might be incorrect.
  13. */
  14. #undef PKEY_DISABLE_ACCESS
  15. #define PKEY_DISABLE_ACCESS 0x3
  16. #undef PKEY_DISABLE_WRITE
  17. #define PKEY_DISABLE_WRITE 0x2
  18. #undef PKEY_DISABLE_EXECUTE
  19. #define PKEY_DISABLE_EXECUTE 0x4
  20. /* Older versions of libc do not not define this */
  21. #ifndef SEGV_PKUERR
  22. #define SEGV_PKUERR 4
  23. #endif
  24. #define SI_PKEY_OFFSET 0x20
  25. #define __NR_pkey_mprotect 386
  26. #define __NR_pkey_alloc 384
  27. #define __NR_pkey_free 385
  28. #define PKEY_BITS_PER_PKEY 2
  29. #define NR_PKEYS 32
  30. #define PKEY_BITS_MASK ((1UL << PKEY_BITS_PER_PKEY) - 1)
  31. inline unsigned long pkeyreg_get(void)
  32. {
  33. return mfspr(SPRN_AMR);
  34. }
  35. inline void pkeyreg_set(unsigned long amr)
  36. {
  37. set_amr(amr);
  38. }
  39. void pkey_set_rights(int pkey, unsigned long rights)
  40. {
  41. unsigned long amr, shift;
  42. shift = (NR_PKEYS - pkey - 1) * PKEY_BITS_PER_PKEY;
  43. amr = pkeyreg_get();
  44. amr &= ~(PKEY_BITS_MASK << shift);
  45. amr |= (rights & PKEY_BITS_MASK) << shift;
  46. pkeyreg_set(amr);
  47. }
  48. int sys_pkey_mprotect(void *addr, size_t len, int prot, int pkey)
  49. {
  50. return syscall(__NR_pkey_mprotect, addr, len, prot, pkey);
  51. }
  52. int sys_pkey_alloc(unsigned long flags, unsigned long rights)
  53. {
  54. return syscall(__NR_pkey_alloc, flags, rights);
  55. }
  56. int sys_pkey_free(int pkey)
  57. {
  58. return syscall(__NR_pkey_free, pkey);
  59. }
  60. int pkeys_unsupported(void)
  61. {
  62. bool hash_mmu = false;
  63. int pkey;
  64. /* Protection keys are currently supported on Hash MMU only */
  65. FAIL_IF(using_hash_mmu(&hash_mmu));
  66. SKIP_IF(!hash_mmu);
  67. /* Check if the system call is supported */
  68. pkey = sys_pkey_alloc(0, 0);
  69. SKIP_IF(pkey < 0);
  70. sys_pkey_free(pkey);
  71. return 0;
  72. }
  73. int siginfo_pkey(siginfo_t *si)
  74. {
  75. /*
  76. * In older versions of libc, siginfo_t does not have si_pkey as
  77. * a member.
  78. */
  79. #ifdef si_pkey
  80. return si->si_pkey;
  81. #else
  82. return *((int *)(((char *) si) + SI_PKEY_OFFSET));
  83. #endif
  84. }
  85. #define pkey_rights(r) ({ \
  86. static char buf[4] = "rwx"; \
  87. unsigned int amr_bits; \
  88. if ((r) & PKEY_DISABLE_EXECUTE) \
  89. buf[2] = '-'; \
  90. amr_bits = (r) & PKEY_BITS_MASK; \
  91. if (amr_bits & PKEY_DISABLE_WRITE) \
  92. buf[1] = '-'; \
  93. if (amr_bits & PKEY_DISABLE_ACCESS & ~PKEY_DISABLE_WRITE) \
  94. buf[0] = '-'; \
  95. buf; \
  96. })
  97. unsigned long next_pkey_rights(unsigned long rights)
  98. {
  99. if (rights == PKEY_DISABLE_ACCESS)
  100. return PKEY_DISABLE_EXECUTE;
  101. else if (rights == (PKEY_DISABLE_ACCESS | PKEY_DISABLE_EXECUTE))
  102. return 0;
  103. if ((rights & PKEY_BITS_MASK) == 0)
  104. rights |= PKEY_DISABLE_WRITE;
  105. else if ((rights & PKEY_BITS_MASK) == PKEY_DISABLE_WRITE)
  106. rights |= PKEY_DISABLE_ACCESS;
  107. return rights;
  108. }
  109. #endif /* _SELFTESTS_POWERPC_PKEYS_H */