pkey-x86.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _PKEYS_X86_H
  3. #define _PKEYS_X86_H
  4. #ifdef __i386__
  5. #ifndef SYS_mprotect_key
  6. # define SYS_mprotect_key 380
  7. #endif
  8. #ifndef SYS_pkey_alloc
  9. # define SYS_pkey_alloc 381
  10. # define SYS_pkey_free 382
  11. #endif
  12. #define REG_IP_IDX REG_EIP
  13. #define si_pkey_offset 0x14
  14. #else
  15. #ifndef SYS_mprotect_key
  16. # define SYS_mprotect_key 329
  17. #endif
  18. #ifndef SYS_pkey_alloc
  19. # define SYS_pkey_alloc 330
  20. # define SYS_pkey_free 331
  21. #endif
  22. #define REG_IP_IDX REG_RIP
  23. #define si_pkey_offset 0x20
  24. #endif
  25. #ifndef PKEY_DISABLE_ACCESS
  26. # define PKEY_DISABLE_ACCESS 0x1
  27. #endif
  28. #ifndef PKEY_DISABLE_WRITE
  29. # define PKEY_DISABLE_WRITE 0x2
  30. #endif
  31. #define NR_PKEYS 16
  32. #define NR_RESERVED_PKEYS 2 /* pkey-0 and exec-only-pkey */
  33. #define PKEY_BITS_PER_PKEY 2
  34. #define HPAGE_SIZE (1UL<<21)
  35. #define PAGE_SIZE 4096
  36. #define MB (1<<20)
  37. static inline void __page_o_noops(void)
  38. {
  39. /* 8-bytes of instruction * 512 bytes = 1 page */
  40. asm(".rept 512 ; nopl 0x7eeeeeee(%eax) ; .endr");
  41. }
  42. static inline u64 __read_pkey_reg(void)
  43. {
  44. unsigned int eax, edx;
  45. unsigned int ecx = 0;
  46. unsigned pkey_reg;
  47. asm volatile(".byte 0x0f,0x01,0xee\n\t"
  48. : "=a" (eax), "=d" (edx)
  49. : "c" (ecx));
  50. pkey_reg = eax;
  51. return pkey_reg;
  52. }
  53. static inline void __write_pkey_reg(u64 pkey_reg)
  54. {
  55. unsigned int eax = pkey_reg;
  56. unsigned int ecx = 0;
  57. unsigned int edx = 0;
  58. dprintf4("%s() changing %016llx to %016llx\n", __func__,
  59. __read_pkey_reg(), pkey_reg);
  60. asm volatile(".byte 0x0f,0x01,0xef\n\t"
  61. : : "a" (eax), "c" (ecx), "d" (edx));
  62. assert(pkey_reg == __read_pkey_reg());
  63. }
  64. /* Intel-defined CPU features, CPUID level 0x00000007:0 (ecx) */
  65. #define X86_FEATURE_PKU (1<<3) /* Protection Keys for Userspace */
  66. #define X86_FEATURE_OSPKE (1<<4) /* OS Protection Keys Enable */
  67. static inline int cpu_has_pkeys(void)
  68. {
  69. unsigned int eax;
  70. unsigned int ebx;
  71. unsigned int ecx;
  72. unsigned int edx;
  73. __cpuid_count(0x7, 0x0, eax, ebx, ecx, edx);
  74. if (!(ecx & X86_FEATURE_PKU)) {
  75. dprintf2("cpu does not have PKU\n");
  76. return 0;
  77. }
  78. if (!(ecx & X86_FEATURE_OSPKE)) {
  79. dprintf2("cpu does not have OSPKE\n");
  80. return 0;
  81. }
  82. return 1;
  83. }
  84. static inline int cpu_max_xsave_size(void)
  85. {
  86. unsigned long XSTATE_CPUID = 0xd;
  87. unsigned int eax;
  88. unsigned int ebx;
  89. unsigned int ecx;
  90. unsigned int edx;
  91. __cpuid_count(XSTATE_CPUID, 0, eax, ebx, ecx, edx);
  92. return ecx;
  93. }
  94. static inline u32 pkey_bit_position(int pkey)
  95. {
  96. return pkey * PKEY_BITS_PER_PKEY;
  97. }
  98. #define XSTATE_PKEY_BIT (9)
  99. #define XSTATE_PKEY 0x200
  100. #define XSTATE_BV_OFFSET 512
  101. int pkey_reg_xstate_offset(void)
  102. {
  103. unsigned int eax;
  104. unsigned int ebx;
  105. unsigned int ecx;
  106. unsigned int edx;
  107. int xstate_offset;
  108. int xstate_size;
  109. unsigned long XSTATE_CPUID = 0xd;
  110. int leaf;
  111. /* assume that XSTATE_PKEY is set in XCR0 */
  112. leaf = XSTATE_PKEY_BIT;
  113. {
  114. __cpuid_count(XSTATE_CPUID, leaf, eax, ebx, ecx, edx);
  115. if (leaf == XSTATE_PKEY_BIT) {
  116. xstate_offset = ebx;
  117. xstate_size = eax;
  118. }
  119. }
  120. if (xstate_size == 0) {
  121. printf("could not find size/offset of PKEY in xsave state\n");
  122. return 0;
  123. }
  124. return xstate_offset;
  125. }
  126. static inline int get_arch_reserved_keys(void)
  127. {
  128. return NR_RESERVED_PKEYS;
  129. }
  130. void expect_fault_on_read_execonly_key(void *p1, int pkey)
  131. {
  132. int ptr_contents;
  133. ptr_contents = read_ptr(p1);
  134. dprintf2("ptr (%p) contents@%d: %x\n", p1, __LINE__, ptr_contents);
  135. expected_pkey_fault(pkey);
  136. }
  137. void *malloc_pkey_with_mprotect_subpage(long size, int prot, u16 pkey)
  138. {
  139. return PTR_ERR_ENOTSUP;
  140. }
  141. #endif /* _PKEYS_X86_H */