smap.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Supervisor Mode Access Prevention support
  3. *
  4. * Copyright (C) 2012 Intel Corporation
  5. * Author: H. Peter Anvin <hpa@linux.intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; version 2
  10. * of the License.
  11. */
  12. #ifndef _ASM_X86_SMAP_H
  13. #define _ASM_X86_SMAP_H
  14. #include <linux/stringify.h>
  15. #include <asm/nops.h>
  16. #include <asm/cpufeature.h>
  17. /* "Raw" instruction opcodes */
  18. #define __ASM_CLAC .byte 0x0f,0x01,0xca
  19. #define __ASM_STAC .byte 0x0f,0x01,0xcb
  20. #ifdef __ASSEMBLY__
  21. #include <asm/alternative-asm.h>
  22. #ifdef CONFIG_X86_SMAP
  23. #define ASM_CLAC \
  24. 661: ASM_NOP3 ; \
  25. .pushsection .altinstr_replacement, "ax" ; \
  26. 662: __ASM_CLAC ; \
  27. .popsection ; \
  28. .pushsection .altinstructions, "a" ; \
  29. altinstruction_entry 661b, 662b, X86_FEATURE_SMAP, 3, 3 ; \
  30. .popsection
  31. #define ASM_STAC \
  32. 661: ASM_NOP3 ; \
  33. .pushsection .altinstr_replacement, "ax" ; \
  34. 662: __ASM_STAC ; \
  35. .popsection ; \
  36. .pushsection .altinstructions, "a" ; \
  37. altinstruction_entry 661b, 662b, X86_FEATURE_SMAP, 3, 3 ; \
  38. .popsection
  39. #else /* CONFIG_X86_SMAP */
  40. #define ASM_CLAC
  41. #define ASM_STAC
  42. #endif /* CONFIG_X86_SMAP */
  43. #else /* __ASSEMBLY__ */
  44. #include <asm/alternative.h>
  45. #ifdef CONFIG_X86_SMAP
  46. static __always_inline void clac(void)
  47. {
  48. /* Note: a barrier is implicit in alternative() */
  49. alternative(ASM_NOP3, __stringify(__ASM_CLAC), X86_FEATURE_SMAP);
  50. }
  51. static __always_inline void stac(void)
  52. {
  53. /* Note: a barrier is implicit in alternative() */
  54. alternative(ASM_NOP3, __stringify(__ASM_STAC), X86_FEATURE_SMAP);
  55. }
  56. /* These macros can be used in asm() statements */
  57. #define ASM_CLAC \
  58. ALTERNATIVE(ASM_NOP3, __stringify(__ASM_CLAC), X86_FEATURE_SMAP)
  59. #define ASM_STAC \
  60. ALTERNATIVE(ASM_NOP3, __stringify(__ASM_STAC), X86_FEATURE_SMAP)
  61. #else /* CONFIG_X86_SMAP */
  62. static inline void clac(void) { }
  63. static inline void stac(void) { }
  64. #define ASM_CLAC
  65. #define ASM_STAC
  66. #endif /* CONFIG_X86_SMAP */
  67. #endif /* __ASSEMBLY__ */
  68. #endif /* _ASM_X86_SMAP_H */