GNU Linux-libre 4.4.296-gnu1
[releases.git] / arch / x86 / include / asm / kaiser.h
1 #ifndef _ASM_X86_KAISER_H
2 #define _ASM_X86_KAISER_H
3
4 #include <uapi/asm/processor-flags.h> /* For PCID constants */
5
6 /*
7  * This file includes the definitions for the KAISER feature.
8  * KAISER is a counter measure against x86_64 side channel attacks on
9  * the kernel virtual memory.  It has a shadow pgd for every process: the
10  * shadow pgd has a minimalistic kernel-set mapped, but includes the whole
11  * user memory. Within a kernel context switch, or when an interrupt is handled,
12  * the pgd is switched to the normal one. When the system switches to user mode,
13  * the shadow pgd is enabled. By this, the virtual memory caches are freed,
14  * and the user may not attack the whole kernel memory.
15  *
16  * A minimalistic kernel mapping holds the parts needed to be mapped in user
17  * mode, such as the entry/exit functions of the user space, or the stacks.
18  */
19
20 #define KAISER_SHADOW_PGD_OFFSET 0x1000
21
22 #ifdef CONFIG_PAGE_TABLE_ISOLATION
23 /*
24  *  A page table address must have this alignment to stay the same when
25  *  KAISER_SHADOW_PGD_OFFSET mask is applied
26  */
27 #define KAISER_KERNEL_PGD_ALIGNMENT (KAISER_SHADOW_PGD_OFFSET << 1)
28 #else
29 #define KAISER_KERNEL_PGD_ALIGNMENT PAGE_SIZE
30 #endif
31
32 #ifdef __ASSEMBLY__
33 #ifdef CONFIG_PAGE_TABLE_ISOLATION
34
35 .macro _SWITCH_TO_KERNEL_CR3 reg
36 movq %cr3, \reg
37 andq $(~(X86_CR3_PCID_ASID_MASK | KAISER_SHADOW_PGD_OFFSET)), \reg
38 /* If PCID enabled, set X86_CR3_PCID_NOFLUSH_BIT */
39 ALTERNATIVE "", "bts $63, \reg", X86_FEATURE_PCID
40 movq \reg, %cr3
41 .endm
42
43 .macro _SWITCH_TO_USER_CR3 reg regb
44 /*
45  * regb must be the low byte portion of reg: because we have arranged
46  * for the low byte of the user PCID to serve as the high byte of NOFLUSH
47  * (0x80 for each when PCID is enabled, or 0x00 when PCID and NOFLUSH are
48  * not enabled): so that the one register can update both memory and cr3.
49  */
50 movq %cr3, \reg
51 orq  PER_CPU_VAR(x86_cr3_pcid_user), \reg
52 js   9f
53 /* If PCID enabled, FLUSH this time, reset to NOFLUSH for next time */
54 movb \regb, PER_CPU_VAR(x86_cr3_pcid_user+7)
55 9:
56 movq \reg, %cr3
57 .endm
58
59 .macro SWITCH_KERNEL_CR3
60 ALTERNATIVE "jmp 8f", "pushq %rax", X86_FEATURE_KAISER
61 _SWITCH_TO_KERNEL_CR3 %rax
62 popq %rax
63 8:
64 .endm
65
66 .macro SWITCH_USER_CR3
67 ALTERNATIVE "jmp 8f", "pushq %rax", X86_FEATURE_KAISER
68 _SWITCH_TO_USER_CR3 %rax %al
69 popq %rax
70 8:
71 .endm
72
73 .macro SWITCH_KERNEL_CR3_NO_STACK
74 ALTERNATIVE "jmp 8f", \
75         __stringify(movq %rax, PER_CPU_VAR(unsafe_stack_register_backup)), \
76         X86_FEATURE_KAISER
77 _SWITCH_TO_KERNEL_CR3 %rax
78 movq PER_CPU_VAR(unsafe_stack_register_backup), %rax
79 8:
80 .endm
81
82 #else /* CONFIG_PAGE_TABLE_ISOLATION */
83
84 .macro SWITCH_KERNEL_CR3
85 .endm
86 .macro SWITCH_USER_CR3
87 .endm
88 .macro SWITCH_KERNEL_CR3_NO_STACK
89 .endm
90
91 #endif /* CONFIG_PAGE_TABLE_ISOLATION */
92
93 #else /* __ASSEMBLY__ */
94
95 #ifdef CONFIG_PAGE_TABLE_ISOLATION
96 /*
97  * Upon kernel/user mode switch, it may happen that the address
98  * space has to be switched before the registers have been
99  * stored.  To change the address space, another register is
100  * needed.  A register therefore has to be stored/restored.
101 */
102 DECLARE_PER_CPU_USER_MAPPED(unsigned long, unsafe_stack_register_backup);
103
104 DECLARE_PER_CPU(unsigned long, x86_cr3_pcid_user);
105
106 extern char __per_cpu_user_mapped_start[], __per_cpu_user_mapped_end[];
107
108 extern int kaiser_enabled;
109 extern void __init kaiser_check_boottime_disable(void);
110 #else
111 #define kaiser_enabled  0
112 static inline void __init kaiser_check_boottime_disable(void) {}
113 #endif /* CONFIG_PAGE_TABLE_ISOLATION */
114
115 /*
116  * Kaiser function prototypes are needed even when CONFIG_PAGE_TABLE_ISOLATION is not set,
117  * so as to build with tests on kaiser_enabled instead of #ifdefs.
118  */
119
120 /**
121  *  kaiser_add_mapping - map a virtual memory part to the shadow (user) mapping
122  *  @addr: the start address of the range
123  *  @size: the size of the range
124  *  @flags: The mapping flags of the pages
125  *
126  *  The mapping is done on a global scope, so no bigger
127  *  synchronization has to be done.  the pages have to be
128  *  manually unmapped again when they are not needed any longer.
129  */
130 extern int kaiser_add_mapping(unsigned long addr, unsigned long size, unsigned long flags);
131
132 /**
133  *  kaiser_remove_mapping - unmap a virtual memory part of the shadow mapping
134  *  @addr: the start address of the range
135  *  @size: the size of the range
136  */
137 extern void kaiser_remove_mapping(unsigned long start, unsigned long size);
138
139 /**
140  *  kaiser_init - Initialize the shadow mapping
141  *
142  *  Most parts of the shadow mapping can be mapped upon boot
143  *  time.  Only per-process things like the thread stacks
144  *  or a new LDT have to be mapped at runtime.  These boot-
145  *  time mappings are permanent and never unmapped.
146  */
147 extern void kaiser_init(void);
148
149 #endif /* __ASSEMBLY */
150
151 #endif /* _ASM_X86_KAISER_H */