pkey-helpers.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #ifndef _PKEYS_HELPER_H
  2. #define _PKEYS_HELPER_H
  3. #define _GNU_SOURCE
  4. #include <string.h>
  5. #include <stdarg.h>
  6. #include <stdio.h>
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include <signal.h>
  10. #include <assert.h>
  11. #include <stdlib.h>
  12. #include <ucontext.h>
  13. #include <sys/mman.h>
  14. #define NR_PKEYS 16
  15. #define PKRU_BITS_PER_PKEY 2
  16. #ifndef DEBUG_LEVEL
  17. #define DEBUG_LEVEL 0
  18. #endif
  19. #define DPRINT_IN_SIGNAL_BUF_SIZE 4096
  20. extern int dprint_in_signal;
  21. extern char dprint_in_signal_buffer[DPRINT_IN_SIGNAL_BUF_SIZE];
  22. static inline void sigsafe_printf(const char *format, ...)
  23. {
  24. va_list ap;
  25. va_start(ap, format);
  26. if (!dprint_in_signal) {
  27. vprintf(format, ap);
  28. } else {
  29. int len = vsnprintf(dprint_in_signal_buffer,
  30. DPRINT_IN_SIGNAL_BUF_SIZE,
  31. format, ap);
  32. /*
  33. * len is amount that would have been printed,
  34. * but actual write is truncated at BUF_SIZE.
  35. */
  36. if (len > DPRINT_IN_SIGNAL_BUF_SIZE)
  37. len = DPRINT_IN_SIGNAL_BUF_SIZE;
  38. write(1, dprint_in_signal_buffer, len);
  39. }
  40. va_end(ap);
  41. }
  42. #define dprintf_level(level, args...) do { \
  43. if (level <= DEBUG_LEVEL) \
  44. sigsafe_printf(args); \
  45. fflush(NULL); \
  46. } while (0)
  47. #define dprintf0(args...) dprintf_level(0, args)
  48. #define dprintf1(args...) dprintf_level(1, args)
  49. #define dprintf2(args...) dprintf_level(2, args)
  50. #define dprintf3(args...) dprintf_level(3, args)
  51. #define dprintf4(args...) dprintf_level(4, args)
  52. extern unsigned int shadow_pkru;
  53. static inline unsigned int __rdpkru(void)
  54. {
  55. unsigned int eax, edx;
  56. unsigned int ecx = 0;
  57. unsigned int pkru;
  58. asm volatile(".byte 0x0f,0x01,0xee\n\t"
  59. : "=a" (eax), "=d" (edx)
  60. : "c" (ecx));
  61. pkru = eax;
  62. return pkru;
  63. }
  64. static inline unsigned int _rdpkru(int line)
  65. {
  66. unsigned int pkru = __rdpkru();
  67. dprintf4("rdpkru(line=%d) pkru: %x shadow: %x\n",
  68. line, pkru, shadow_pkru);
  69. assert(pkru == shadow_pkru);
  70. return pkru;
  71. }
  72. #define rdpkru() _rdpkru(__LINE__)
  73. static inline void __wrpkru(unsigned int pkru)
  74. {
  75. unsigned int eax = pkru;
  76. unsigned int ecx = 0;
  77. unsigned int edx = 0;
  78. dprintf4("%s() changing %08x to %08x\n", __func__, __rdpkru(), pkru);
  79. asm volatile(".byte 0x0f,0x01,0xef\n\t"
  80. : : "a" (eax), "c" (ecx), "d" (edx));
  81. assert(pkru == __rdpkru());
  82. }
  83. static inline void wrpkru(unsigned int pkru)
  84. {
  85. dprintf4("%s() changing %08x to %08x\n", __func__, __rdpkru(), pkru);
  86. /* will do the shadow check for us: */
  87. rdpkru();
  88. __wrpkru(pkru);
  89. shadow_pkru = pkru;
  90. dprintf4("%s(%08x) pkru: %08x\n", __func__, pkru, __rdpkru());
  91. }
  92. /*
  93. * These are technically racy. since something could
  94. * change PKRU between the read and the write.
  95. */
  96. static inline void __pkey_access_allow(int pkey, int do_allow)
  97. {
  98. unsigned int pkru = rdpkru();
  99. int bit = pkey * 2;
  100. if (do_allow)
  101. pkru &= (1<<bit);
  102. else
  103. pkru |= (1<<bit);
  104. dprintf4("pkru now: %08x\n", rdpkru());
  105. wrpkru(pkru);
  106. }
  107. static inline void __pkey_write_allow(int pkey, int do_allow_write)
  108. {
  109. long pkru = rdpkru();
  110. int bit = pkey * 2 + 1;
  111. if (do_allow_write)
  112. pkru &= (1<<bit);
  113. else
  114. pkru |= (1<<bit);
  115. wrpkru(pkru);
  116. dprintf4("pkru now: %08x\n", rdpkru());
  117. }
  118. #define PROT_PKEY0 0x10 /* protection key value (bit 0) */
  119. #define PROT_PKEY1 0x20 /* protection key value (bit 1) */
  120. #define PROT_PKEY2 0x40 /* protection key value (bit 2) */
  121. #define PROT_PKEY3 0x80 /* protection key value (bit 3) */
  122. #define PAGE_SIZE 4096
  123. #define MB (1<<20)
  124. static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
  125. unsigned int *ecx, unsigned int *edx)
  126. {
  127. /* ecx is often an input as well as an output. */
  128. asm volatile(
  129. "cpuid;"
  130. : "=a" (*eax),
  131. "=b" (*ebx),
  132. "=c" (*ecx),
  133. "=d" (*edx)
  134. : "0" (*eax), "2" (*ecx));
  135. }
  136. /* Intel-defined CPU features, CPUID level 0x00000007:0 (ecx) */
  137. #define X86_FEATURE_PKU (1<<3) /* Protection Keys for Userspace */
  138. #define X86_FEATURE_OSPKE (1<<4) /* OS Protection Keys Enable */
  139. static inline int cpu_has_pku(void)
  140. {
  141. unsigned int eax;
  142. unsigned int ebx;
  143. unsigned int ecx;
  144. unsigned int edx;
  145. eax = 0x7;
  146. ecx = 0x0;
  147. __cpuid(&eax, &ebx, &ecx, &edx);
  148. if (!(ecx & X86_FEATURE_PKU)) {
  149. dprintf2("cpu does not have PKU\n");
  150. return 0;
  151. }
  152. if (!(ecx & X86_FEATURE_OSPKE)) {
  153. dprintf2("cpu does not have OSPKE\n");
  154. return 0;
  155. }
  156. return 1;
  157. }
  158. #define XSTATE_PKRU_BIT (9)
  159. #define XSTATE_PKRU 0x200
  160. int pkru_xstate_offset(void)
  161. {
  162. unsigned int eax;
  163. unsigned int ebx;
  164. unsigned int ecx;
  165. unsigned int edx;
  166. int xstate_offset;
  167. int xstate_size;
  168. unsigned long XSTATE_CPUID = 0xd;
  169. int leaf;
  170. /* assume that XSTATE_PKRU is set in XCR0 */
  171. leaf = XSTATE_PKRU_BIT;
  172. {
  173. eax = XSTATE_CPUID;
  174. ecx = leaf;
  175. __cpuid(&eax, &ebx, &ecx, &edx);
  176. if (leaf == XSTATE_PKRU_BIT) {
  177. xstate_offset = ebx;
  178. xstate_size = eax;
  179. }
  180. }
  181. if (xstate_size == 0) {
  182. printf("could not find size/offset of PKRU in xsave state\n");
  183. return 0;
  184. }
  185. return xstate_offset;
  186. }
  187. #endif /* _PKEYS_HELPER_H */