6f4c8ef46881228b2bfff1960e37d1db05dc375c
[pandora-kernel.git] / arch / x86 / include / asm / kaiser.h
1 #ifndef _ASM_X86_KAISER_H
2 #define _ASM_X86_KAISER_H
3
4 #include <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 __ASSEMBLY__
23 #ifdef CONFIG_KAISER
24
25 .macro _SWITCH_TO_KERNEL_CR3 reg
26 movq %cr3, \reg
27 andq $(~(X86_CR3_PCID_ASID_MASK | KAISER_SHADOW_PGD_OFFSET)), \reg
28 orq  x86_cr3_pcid_noflush, \reg
29 movq \reg, %cr3
30 .endm
31
32 .macro _SWITCH_TO_USER_CR3 reg regb
33 /*
34  * regb must be the low byte portion of reg: because we have arranged
35  * for the low byte of the user PCID to serve as the high byte of NOFLUSH
36  * (0x80 for each when PCID is enabled, or 0x00 when PCID and NOFLUSH are
37  * not enabled): so that the one register can update both memory and cr3.
38  */
39 movq %cr3, \reg
40 orq  PER_CPU_VAR(x86_cr3_pcid_user), \reg
41 js   9f
42 /* FLUSH this time, reset to NOFLUSH for next time (if PCID enabled) */
43 movb \regb, PER_CPU_VAR(x86_cr3_pcid_user+7)
44 9:
45 movq \reg, %cr3
46 .endm
47
48 .macro SWITCH_KERNEL_CR3
49 pushq %rax
50 _SWITCH_TO_KERNEL_CR3 %rax
51 popq %rax
52 .endm
53
54 .macro SWITCH_USER_CR3
55 pushq %rax
56 _SWITCH_TO_USER_CR3 %rax %al
57 popq %rax
58 .endm
59
60 .macro SWITCH_KERNEL_CR3_NO_STACK
61 movq %rax, PER_CPU_VAR(unsafe_stack_register_backup)
62 _SWITCH_TO_KERNEL_CR3 %rax
63 movq PER_CPU_VAR(unsafe_stack_register_backup), %rax
64 .endm
65
66 #else /* CONFIG_KAISER */
67
68 .macro SWITCH_KERNEL_CR3 reg
69 .endm
70 .macro SWITCH_USER_CR3 reg regb
71 .endm
72 .macro SWITCH_KERNEL_CR3_NO_STACK
73 .endm
74
75 #endif /* CONFIG_KAISER */
76
77 #else /* __ASSEMBLY__ */
78
79 #ifdef CONFIG_KAISER
80 /*
81  * Upon kernel/user mode switch, it may happen that the address
82  * space has to be switched before the registers have been
83  * stored.  To change the address space, another register is
84  * needed.  A register therefore has to be stored/restored.
85 */
86 DECLARE_PER_CPU_USER_MAPPED(unsigned long, unsafe_stack_register_backup);
87
88 extern unsigned long x86_cr3_pcid_noflush;
89 DECLARE_PER_CPU(unsigned long, x86_cr3_pcid_user);
90
91 extern char __per_cpu_user_mapped_start[], __per_cpu_user_mapped_end[];
92
93 /**
94  *  kaiser_add_mapping - map a virtual memory part to the shadow (user) mapping
95  *  @addr: the start address of the range
96  *  @size: the size of the range
97  *  @flags: The mapping flags of the pages
98  *
99  *  The mapping is done on a global scope, so no bigger
100  *  synchronization has to be done.  the pages have to be
101  *  manually unmapped again when they are not needed any longer.
102  */
103 extern int kaiser_add_mapping(unsigned long addr, unsigned long size, unsigned long flags);
104
105 /**
106  *  kaiser_remove_mapping - unmap a virtual memory part of the shadow mapping
107  *  @addr: the start address of the range
108  *  @size: the size of the range
109  */
110 extern void kaiser_remove_mapping(unsigned long start, unsigned long size);
111
112 /**
113  *  kaiser_init - Initialize the shadow mapping
114  *
115  *  Most parts of the shadow mapping can be mapped upon boot
116  *  time.  Only per-process things like the thread stacks
117  *  or a new LDT have to be mapped at runtime.  These boot-
118  *  time mappings are permanent and never unmapped.
119  */
120 extern void kaiser_init(void);
121
122 #endif /* CONFIG_KAISER */
123
124 #endif /* __ASSEMBLY */
125
126 #endif /* _ASM_X86_KAISER_H */