pkeys.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #ifndef _ASM_X86_PKEYS_H
  2. #define _ASM_X86_PKEYS_H
  3. #define arch_max_pkey() (boot_cpu_has(X86_FEATURE_OSPKE) ? 16 : 1)
  4. extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
  5. unsigned long init_val);
  6. /*
  7. * Try to dedicate one of the protection keys to be used as an
  8. * execute-only protection key.
  9. */
  10. extern int __execute_only_pkey(struct mm_struct *mm);
  11. static inline int execute_only_pkey(struct mm_struct *mm)
  12. {
  13. if (!boot_cpu_has(X86_FEATURE_OSPKE))
  14. return 0;
  15. return __execute_only_pkey(mm);
  16. }
  17. extern int __arch_override_mprotect_pkey(struct vm_area_struct *vma,
  18. int prot, int pkey);
  19. static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma,
  20. int prot, int pkey)
  21. {
  22. if (!boot_cpu_has(X86_FEATURE_OSPKE))
  23. return 0;
  24. return __arch_override_mprotect_pkey(vma, prot, pkey);
  25. }
  26. extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
  27. unsigned long init_val);
  28. #define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | VM_PKEY_BIT3)
  29. #define mm_pkey_allocation_map(mm) (mm->context.pkey_allocation_map)
  30. #define mm_set_pkey_allocated(mm, pkey) do { \
  31. mm_pkey_allocation_map(mm) |= (1U << pkey); \
  32. } while (0)
  33. #define mm_set_pkey_free(mm, pkey) do { \
  34. mm_pkey_allocation_map(mm) &= ~(1U << pkey); \
  35. } while (0)
  36. static inline
  37. bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
  38. {
  39. /*
  40. * "Allocated" pkeys are those that have been returned
  41. * from pkey_alloc(). pkey 0 is special, and never
  42. * returned from pkey_alloc().
  43. */
  44. if (pkey <= 0)
  45. return false;
  46. if (pkey >= arch_max_pkey())
  47. return false;
  48. return mm_pkey_allocation_map(mm) & (1U << pkey);
  49. }
  50. /*
  51. * Returns a positive, 4-bit key on success, or -1 on failure.
  52. */
  53. static inline
  54. 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. u16 all_pkeys_mask = ((1U << arch_max_pkey()) - 1);
  63. int ret;
  64. /*
  65. * Are we out of pkeys? We must handle this specially
  66. * because ffz() behavior is undefined if there are no
  67. * zeros.
  68. */
  69. if (mm_pkey_allocation_map(mm) == all_pkeys_mask)
  70. return -1;
  71. ret = ffz(mm_pkey_allocation_map(mm));
  72. mm_set_pkey_allocated(mm, ret);
  73. return ret;
  74. }
  75. static inline
  76. int mm_pkey_free(struct mm_struct *mm, int pkey)
  77. {
  78. if (!mm_pkey_is_allocated(mm, pkey))
  79. return -EINVAL;
  80. mm_set_pkey_free(mm, pkey);
  81. return 0;
  82. }
  83. extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
  84. unsigned long init_val);
  85. extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
  86. unsigned long init_val);
  87. extern void copy_init_pkru_to_fpregs(void);
  88. #endif /*_ASM_X86_PKEYS_H */