special_insns.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_SPECIAL_INSNS_H
  3. #define _ASM_X86_SPECIAL_INSNS_H
  4. #ifdef __KERNEL__
  5. #include <asm/nops.h>
  6. #include <asm/processor-flags.h>
  7. #include <linux/irqflags.h>
  8. #include <linux/jump_label.h>
  9. /*
  10. * The compiler should not reorder volatile asm statements with respect to each
  11. * other: they should execute in program order. However GCC 4.9.x and 5.x have
  12. * a bug (which was fixed in 8.1, 7.3 and 6.5) where they might reorder
  13. * volatile asm. The write functions are not affected since they have memory
  14. * clobbers preventing reordering. To prevent reads from being reordered with
  15. * respect to writes, use a dummy memory operand.
  16. */
  17. #define __FORCE_ORDER "m"(*(unsigned int *)0x1000UL)
  18. void native_write_cr0(unsigned long val);
  19. static inline unsigned long native_read_cr0(void)
  20. {
  21. unsigned long val;
  22. asm volatile("mov %%cr0,%0\n\t" : "=r" (val) : __FORCE_ORDER);
  23. return val;
  24. }
  25. static __always_inline unsigned long native_read_cr2(void)
  26. {
  27. unsigned long val;
  28. asm volatile("mov %%cr2,%0\n\t" : "=r" (val) : __FORCE_ORDER);
  29. return val;
  30. }
  31. static __always_inline void native_write_cr2(unsigned long val)
  32. {
  33. asm volatile("mov %0,%%cr2": : "r" (val) : "memory");
  34. }
  35. static inline unsigned long __native_read_cr3(void)
  36. {
  37. unsigned long val;
  38. asm volatile("mov %%cr3,%0\n\t" : "=r" (val) : __FORCE_ORDER);
  39. return val;
  40. }
  41. static inline void native_write_cr3(unsigned long val)
  42. {
  43. asm volatile("mov %0,%%cr3": : "r" (val) : "memory");
  44. }
  45. static inline unsigned long native_read_cr4(void)
  46. {
  47. unsigned long val;
  48. #ifdef CONFIG_X86_32
  49. /*
  50. * This could fault if CR4 does not exist. Non-existent CR4
  51. * is functionally equivalent to CR4 == 0. Keep it simple and pretend
  52. * that CR4 == 0 on CPUs that don't have CR4.
  53. */
  54. asm volatile("1: mov %%cr4, %0\n"
  55. "2:\n"
  56. _ASM_EXTABLE(1b, 2b)
  57. : "=r" (val) : "0" (0), __FORCE_ORDER);
  58. #else
  59. /* CR4 always exists on x86_64. */
  60. asm volatile("mov %%cr4,%0\n\t" : "=r" (val) : __FORCE_ORDER);
  61. #endif
  62. return val;
  63. }
  64. void native_write_cr4(unsigned long val);
  65. #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
  66. static inline u32 rdpkru(void)
  67. {
  68. u32 ecx = 0;
  69. u32 edx, pkru;
  70. /*
  71. * "rdpkru" instruction. Places PKRU contents in to EAX,
  72. * clears EDX and requires that ecx=0.
  73. */
  74. asm volatile(".byte 0x0f,0x01,0xee\n\t"
  75. : "=a" (pkru), "=d" (edx)
  76. : "c" (ecx));
  77. return pkru;
  78. }
  79. static inline void wrpkru(u32 pkru)
  80. {
  81. u32 ecx = 0, edx = 0;
  82. /*
  83. * "wrpkru" instruction. Loads contents in EAX to PKRU,
  84. * requires that ecx = edx = 0.
  85. */
  86. asm volatile(".byte 0x0f,0x01,0xef\n\t"
  87. : : "a" (pkru), "c"(ecx), "d"(edx));
  88. }
  89. #else
  90. static inline u32 rdpkru(void)
  91. {
  92. return 0;
  93. }
  94. static inline void wrpkru(u32 pkru)
  95. {
  96. }
  97. #endif
  98. static inline void native_wbinvd(void)
  99. {
  100. asm volatile("wbinvd": : :"memory");
  101. }
  102. extern asmlinkage void asm_load_gs_index(unsigned int selector);
  103. static inline void native_load_gs_index(unsigned int selector)
  104. {
  105. unsigned long flags;
  106. local_irq_save(flags);
  107. asm_load_gs_index(selector);
  108. local_irq_restore(flags);
  109. }
  110. static inline unsigned long __read_cr4(void)
  111. {
  112. return native_read_cr4();
  113. }
  114. #ifdef CONFIG_PARAVIRT_XXL
  115. #include <asm/paravirt.h>
  116. #else
  117. static inline unsigned long read_cr0(void)
  118. {
  119. return native_read_cr0();
  120. }
  121. static inline void write_cr0(unsigned long x)
  122. {
  123. native_write_cr0(x);
  124. }
  125. static __always_inline unsigned long read_cr2(void)
  126. {
  127. return native_read_cr2();
  128. }
  129. static __always_inline void write_cr2(unsigned long x)
  130. {
  131. native_write_cr2(x);
  132. }
  133. /*
  134. * Careful! CR3 contains more than just an address. You probably want
  135. * read_cr3_pa() instead.
  136. */
  137. static inline unsigned long __read_cr3(void)
  138. {
  139. return __native_read_cr3();
  140. }
  141. static inline void write_cr3(unsigned long x)
  142. {
  143. native_write_cr3(x);
  144. }
  145. static inline void __write_cr4(unsigned long x)
  146. {
  147. native_write_cr4(x);
  148. }
  149. static inline void wbinvd(void)
  150. {
  151. native_wbinvd();
  152. }
  153. static inline void load_gs_index(unsigned int selector)
  154. {
  155. #ifdef CONFIG_X86_64
  156. native_load_gs_index(selector);
  157. #else
  158. loadsegment(gs, selector);
  159. #endif
  160. }
  161. #endif /* CONFIG_PARAVIRT_XXL */
  162. static inline void clflush(volatile void *__p)
  163. {
  164. asm volatile("clflush %0" : "+m" (*(volatile char __force *)__p));
  165. }
  166. static inline void clflushopt(volatile void *__p)
  167. {
  168. alternative_io(".byte 0x3e; clflush %P0",
  169. ".byte 0x66; clflush %P0",
  170. X86_FEATURE_CLFLUSHOPT,
  171. "+m" (*(volatile char __force *)__p));
  172. }
  173. static inline void clwb(volatile void *__p)
  174. {
  175. volatile struct { char x[64]; } *p = __p;
  176. asm volatile(ALTERNATIVE_2(
  177. ".byte 0x3e; clflush (%[pax])",
  178. ".byte 0x66; clflush (%[pax])", /* clflushopt (%%rax) */
  179. X86_FEATURE_CLFLUSHOPT,
  180. ".byte 0x66, 0x0f, 0xae, 0x30", /* clwb (%%rax) */
  181. X86_FEATURE_CLWB)
  182. : [p] "+m" (*p)
  183. : [pax] "a" (p));
  184. }
  185. #define nop() asm volatile ("nop")
  186. static inline void serialize(void)
  187. {
  188. /* Instruction opcode for SERIALIZE; supported in binutils >= 2.35. */
  189. asm volatile(".byte 0xf, 0x1, 0xe8" ::: "memory");
  190. }
  191. /* The dst parameter must be 64-bytes aligned */
  192. static inline void movdir64b(void __iomem *dst, const void *src)
  193. {
  194. const struct { char _[64]; } *__src = src;
  195. struct { char _[64]; } __iomem *__dst = dst;
  196. /*
  197. * MOVDIR64B %(rdx), rax.
  198. *
  199. * Both __src and __dst must be memory constraints in order to tell the
  200. * compiler that no other memory accesses should be reordered around
  201. * this one.
  202. *
  203. * Also, both must be supplied as lvalues because this tells
  204. * the compiler what the object is (its size) the instruction accesses.
  205. * I.e., not the pointers but what they point to, thus the deref'ing '*'.
  206. */
  207. asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
  208. : "+m" (*__dst)
  209. : "m" (*__src), "a" (__dst), "d" (__src));
  210. }
  211. /**
  212. * enqcmds - Enqueue a command in supervisor (CPL0) mode
  213. * @dst: destination, in MMIO space (must be 512-bit aligned)
  214. * @src: 512 bits memory operand
  215. *
  216. * The ENQCMDS instruction allows software to write a 512-bit command to
  217. * a 512-bit-aligned special MMIO region that supports the instruction.
  218. * A return status is loaded into the ZF flag in the RFLAGS register.
  219. * ZF = 0 equates to success, and ZF = 1 indicates retry or error.
  220. *
  221. * This function issues the ENQCMDS instruction to submit data from
  222. * kernel space to MMIO space, in a unit of 512 bits. Order of data access
  223. * is not guaranteed, nor is a memory barrier performed afterwards. It
  224. * returns 0 on success and -EAGAIN on failure.
  225. *
  226. * Warning: Do not use this helper unless your driver has checked that the
  227. * ENQCMDS instruction is supported on the platform and the device accepts
  228. * ENQCMDS.
  229. */
  230. static inline int enqcmds(void __iomem *dst, const void *src)
  231. {
  232. const struct { char _[64]; } *__src = src;
  233. struct { char _[64]; } __iomem *__dst = dst;
  234. bool zf;
  235. /*
  236. * ENQCMDS %(rdx), rax
  237. *
  238. * See movdir64b()'s comment on operand specification.
  239. */
  240. asm volatile(".byte 0xf3, 0x0f, 0x38, 0xf8, 0x02, 0x66, 0x90"
  241. CC_SET(z)
  242. : CC_OUT(z) (zf), "+m" (*__dst)
  243. : "m" (*__src), "a" (__dst), "d" (__src));
  244. /* Submission failure is indicated via EFLAGS.ZF=1 */
  245. if (zf)
  246. return -EAGAIN;
  247. return 0;
  248. }
  249. static __always_inline void tile_release(void)
  250. {
  251. /*
  252. * Instruction opcode for TILERELEASE; supported in binutils
  253. * version >= 2.36.
  254. */
  255. asm volatile(".byte 0xc4, 0xe2, 0x78, 0x49, 0xc0");
  256. }
  257. #endif /* __KERNEL__ */
  258. #endif /* _ASM_X86_SPECIAL_INSNS_H */