smap.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Supervisor Mode Access Prevention support
  4. *
  5. * Copyright (C) 2012 Intel Corporation
  6. * Author: H. Peter Anvin <[email protected]>
  7. */
  8. #ifndef _ASM_X86_SMAP_H
  9. #define _ASM_X86_SMAP_H
  10. #include <asm/nops.h>
  11. #include <asm/cpufeatures.h>
  12. #include <asm/alternative.h>
  13. /* "Raw" instruction opcodes */
  14. #define __ASM_CLAC ".byte 0x0f,0x01,0xca"
  15. #define __ASM_STAC ".byte 0x0f,0x01,0xcb"
  16. #ifdef __ASSEMBLY__
  17. #define ASM_CLAC \
  18. ALTERNATIVE "", __ASM_CLAC, X86_FEATURE_SMAP
  19. #define ASM_STAC \
  20. ALTERNATIVE "", __ASM_STAC, X86_FEATURE_SMAP
  21. #else /* __ASSEMBLY__ */
  22. static __always_inline void clac(void)
  23. {
  24. /* Note: a barrier is implicit in alternative() */
  25. alternative("", __ASM_CLAC, X86_FEATURE_SMAP);
  26. }
  27. static __always_inline void stac(void)
  28. {
  29. /* Note: a barrier is implicit in alternative() */
  30. alternative("", __ASM_STAC, X86_FEATURE_SMAP);
  31. }
  32. static __always_inline unsigned long smap_save(void)
  33. {
  34. unsigned long flags;
  35. asm volatile ("# smap_save\n\t"
  36. ALTERNATIVE("", "pushf; pop %0; " __ASM_CLAC "\n\t",
  37. X86_FEATURE_SMAP)
  38. : "=rm" (flags) : : "memory", "cc");
  39. return flags;
  40. }
  41. static __always_inline void smap_restore(unsigned long flags)
  42. {
  43. asm volatile ("# smap_restore\n\t"
  44. ALTERNATIVE("", "push %0; popf\n\t",
  45. X86_FEATURE_SMAP)
  46. : : "g" (flags) : "memory", "cc");
  47. }
  48. /* These macros can be used in asm() statements */
  49. #define ASM_CLAC \
  50. ALTERNATIVE("", __ASM_CLAC, X86_FEATURE_SMAP)
  51. #define ASM_STAC \
  52. ALTERNATIVE("", __ASM_STAC, X86_FEATURE_SMAP)
  53. #endif /* __ASSEMBLY__ */
  54. #endif /* _ASM_X86_SMAP_H */