pkeys.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2023 Arm Ltd.
  4. *
  5. * Based on arch/x86/include/asm/pkeys.h
  6. */
  7. #ifndef _ASM_ARM64_PKEYS_H
  8. #define _ASM_ARM64_PKEYS_H
  9. #define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2)
  10. #define arch_max_pkey() 8
  11. int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
  12. unsigned long init_val);
  13. static inline bool arch_pkeys_enabled(void)
  14. {
  15. return system_supports_poe();
  16. }
  17. static inline int vma_pkey(struct vm_area_struct *vma)
  18. {
  19. return (vma->vm_flags & ARCH_VM_PKEY_FLAGS) >> VM_PKEY_SHIFT;
  20. }
  21. static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma,
  22. int prot, int pkey)
  23. {
  24. if (pkey != -1)
  25. return pkey;
  26. return vma_pkey(vma);
  27. }
  28. static inline int execute_only_pkey(struct mm_struct *mm)
  29. {
  30. // Execute-only mappings are handled by EPAN/FEAT_PAN3.
  31. return -1;
  32. }
  33. #define mm_pkey_allocation_map(mm) (mm)->context.pkey_allocation_map
  34. #define mm_set_pkey_allocated(mm, pkey) do { \
  35. mm_pkey_allocation_map(mm) |= (1U << pkey); \
  36. } while (0)
  37. #define mm_set_pkey_free(mm, pkey) do { \
  38. mm_pkey_allocation_map(mm) &= ~(1U << pkey); \
  39. } while (0)
  40. static inline bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
  41. {
  42. /*
  43. * "Allocated" pkeys are those that have been returned
  44. * from pkey_alloc() or pkey 0 which is allocated
  45. * implicitly when the mm is created.
  46. */
  47. if (pkey < 0 || pkey >= arch_max_pkey())
  48. return false;
  49. return mm_pkey_allocation_map(mm) & (1U << pkey);
  50. }
  51. /*
  52. * Returns a positive, 3-bit key on success, or -1 on failure.
  53. */
  54. static inline int mm_pkey_alloc(struct mm_struct *mm)
  55. {
  56. /*
  57. * Note: this is the one and only place we make sure
  58. * that the pkey is valid as far as the hardware is
  59. * concerned. The rest of the kernel trusts that
  60. * only good, valid pkeys come out of here.
  61. */
  62. u8 all_pkeys_mask = GENMASK(arch_max_pkey() - 1, 0);
  63. int ret;
  64. if (!arch_pkeys_enabled())
  65. return -1;
  66. /*
  67. * Are we out of pkeys? We must handle this specially
  68. * because ffz() behavior is undefined if there are no
  69. * zeros.
  70. */
  71. if (mm_pkey_allocation_map(mm) == all_pkeys_mask)
  72. return -1;
  73. ret = ffz(mm_pkey_allocation_map(mm));
  74. mm_set_pkey_allocated(mm, ret);
  75. return ret;
  76. }
  77. static inline int mm_pkey_free(struct mm_struct *mm, int pkey)
  78. {
  79. if (!mm_pkey_is_allocated(mm, pkey))
  80. return -EINVAL;
  81. mm_set_pkey_free(mm, pkey);
  82. return 0;
  83. }
  84. #endif /* _ASM_ARM64_PKEYS_H */