OSDN Git Service

kaiser: merged update
[android-x86/kernel.git] / arch / x86 / include / asm / kaiser.h
1 #ifndef _ASM_X86_KAISER_H
2 #define _ASM_X86_KAISER_H
3
4 /* This file includes the definitions for the KAISER feature.
5  * KAISER is a counter measure against x86_64 side channel attacks on the kernel virtual memory.
6  * It has a shodow-pgd for every process. the shadow-pgd has a minimalistic kernel-set mapped,
7  * but includes the whole user memory. Within a kernel context switch, or when an interrupt is handled,
8  * the pgd is switched to the normal one. When the system switches to user mode, the shadow pgd is enabled.
9  * By this, the virtual memory chaches are freed, and the user may not attack the whole kernel memory.
10  *
11  * A minimalistic kernel mapping holds the parts needed to be mapped in user mode, as the entry/exit functions
12  * of the user space, or the stacks.
13  */
14 #ifdef __ASSEMBLY__
15 #ifdef CONFIG_KAISER
16
17 .macro _SWITCH_TO_KERNEL_CR3 reg
18 movq %cr3, \reg
19 #ifdef CONFIG_KAISER_REAL_SWITCH
20 andq $(~0x1000), \reg
21 #endif
22 movq \reg, %cr3
23 .endm
24
25 .macro _SWITCH_TO_USER_CR3 reg
26 movq %cr3, \reg
27 #ifdef CONFIG_KAISER_REAL_SWITCH
28 orq $(0x1000), \reg
29 #endif
30 movq \reg, %cr3
31 .endm
32
33 .macro SWITCH_KERNEL_CR3
34 pushq %rax
35 _SWITCH_TO_KERNEL_CR3 %rax
36 popq %rax
37 .endm
38
39 .macro SWITCH_USER_CR3
40 pushq %rax
41 _SWITCH_TO_USER_CR3 %rax
42 popq %rax
43 .endm
44
45 .macro SWITCH_KERNEL_CR3_NO_STACK
46 movq %rax, PER_CPU_VAR(unsafe_stack_register_backup)
47 _SWITCH_TO_KERNEL_CR3 %rax
48 movq PER_CPU_VAR(unsafe_stack_register_backup), %rax
49 .endm
50
51
52 .macro SWITCH_USER_CR3_NO_STACK
53
54 movq %rax, PER_CPU_VAR(unsafe_stack_register_backup)
55 _SWITCH_TO_USER_CR3 %rax
56 movq PER_CPU_VAR(unsafe_stack_register_backup), %rax
57
58 .endm
59
60 #else /* CONFIG_KAISER */
61
62 .macro SWITCH_KERNEL_CR3 reg
63 .endm
64 .macro SWITCH_USER_CR3 reg
65 .endm
66 .macro SWITCH_USER_CR3_NO_STACK
67 .endm
68 .macro SWITCH_KERNEL_CR3_NO_STACK
69 .endm
70
71 #endif /* CONFIG_KAISER */
72
73 #else /* __ASSEMBLY__ */
74
75
76 #ifdef CONFIG_KAISER
77 /*
78  * Upon kernel/user mode switch, it may happen that the address
79  * space has to be switched before the registers have been
80  * stored.  To change the address space, another register is
81  * needed.  A register therefore has to be stored/restored.
82 */
83
84 DECLARE_PER_CPU_USER_MAPPED(unsigned long, unsafe_stack_register_backup);
85
86 /**
87  *  kaiser_add_mapping - map a virtual memory part to the shadow (user) mapping
88  *  @addr: the start address of the range
89  *  @size: the size of the range
90  *  @flags: The mapping flags of the pages
91  *
92  *  The mapping is done on a global scope, so no bigger
93  *  synchronization has to be done.  the pages have to be
94  *  manually unmapped again when they are not needed any longer.
95  */
96 extern int kaiser_add_mapping(unsigned long addr, unsigned long size, unsigned long flags);
97
98
99 /**
100  *  kaiser_remove_mapping - unmap a virtual memory part of the shadow mapping
101  *  @addr: the start address of the range
102  *  @size: the size of the range
103  */
104 extern void kaiser_remove_mapping(unsigned long start, unsigned long size);
105
106 /**
107  *  kaiser_initialize_mapping - Initalize the shadow mapping
108  *
109  *  Most parts of the shadow mapping can be mapped upon boot
110  *  time.  Only per-process things like the thread stacks
111  *  or a new LDT have to be mapped at runtime.  These boot-
112  *  time mappings are permanent and nevertunmapped.
113  */
114 extern void kaiser_init(void);
115
116 #endif /* CONFIG_KAISER */
117
118 #endif /* __ASSEMBLY */
119
120
121
122 #endif /* _ASM_X86_KAISER_H */