^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Supervisor Mode Access Prevention support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2012 Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: H. Peter Anvin <hpa@linux.intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #ifndef _ASM_X86_SMAP_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define _ASM_X86_SMAP_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/nops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <asm/cpufeatures.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /* "Raw" instruction opcodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define __ASM_CLAC ".byte 0x0f,0x01,0xca"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define __ASM_STAC ".byte 0x0f,0x01,0xcb"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #ifdef __ASSEMBLY__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <asm/alternative-asm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #ifdef CONFIG_X86_SMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define ASM_CLAC \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) ALTERNATIVE "", __ASM_CLAC, X86_FEATURE_SMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define ASM_STAC \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) ALTERNATIVE "", __ASM_STAC, X86_FEATURE_SMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #else /* CONFIG_X86_SMAP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define ASM_CLAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define ASM_STAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #endif /* CONFIG_X86_SMAP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #else /* __ASSEMBLY__ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <asm/alternative.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #ifdef CONFIG_X86_SMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static __always_inline void clac(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* Note: a barrier is implicit in alternative() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) alternative("", __ASM_CLAC, X86_FEATURE_SMAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static __always_inline void stac(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /* Note: a barrier is implicit in alternative() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) alternative("", __ASM_STAC, X86_FEATURE_SMAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static __always_inline unsigned long smap_save(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) asm volatile ("# smap_save\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) ALTERNATIVE("jmp 1f", "", X86_FEATURE_SMAP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) "pushf; pop %0; " __ASM_CLAC "\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) "1:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) : "=rm" (flags) : : "memory", "cc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static __always_inline void smap_restore(unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) asm volatile ("# smap_restore\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) ALTERNATIVE("jmp 1f", "", X86_FEATURE_SMAP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) "push %0; popf\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) "1:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) : : "g" (flags) : "memory", "cc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* These macros can be used in asm() statements */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define ASM_CLAC \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) ALTERNATIVE("", __ASM_CLAC, X86_FEATURE_SMAP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define ASM_STAC \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) ALTERNATIVE("", __ASM_STAC, X86_FEATURE_SMAP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #else /* CONFIG_X86_SMAP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static inline void clac(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static inline void stac(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static inline unsigned long smap_save(void) { return 0; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static inline void smap_restore(unsigned long flags) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #define ASM_CLAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #define ASM_STAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #endif /* CONFIG_X86_SMAP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #endif /* __ASSEMBLY__ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #endif /* _ASM_X86_SMAP_H */