pkey-x86.h 3.3 KB

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