frameasm.h revision 1.37.2.1 1 /* $NetBSD: frameasm.h,v 1.37.2.1 2018/03/30 06:20:11 pgoyette Exp $ */
2
3 #ifndef _AMD64_MACHINE_FRAMEASM_H
4 #define _AMD64_MACHINE_FRAMEASM_H
5
6 #ifdef _KERNEL_OPT
7 #include "opt_xen.h"
8 #include "opt_svs.h"
9 #endif
10
11 /*
12 * Macros to define pushing/popping frames for interrupts, traps
13 * and system calls. Currently all the same; will diverge later.
14 */
15
16 #ifdef XEN
17 #define HYPERVISOR_iret hypercall_page + (__HYPERVISOR_iret * 32)
18 /* Xen do not need swapgs, done by hypervisor */
19 #define swapgs
20 #define iretq pushq $0 ; jmp HYPERVISOR_iret
21 #define XEN_ONLY2(x,y) x,y
22 #define NOT_XEN(x)
23
24 #define CLI(temp_reg) \
25 movq CPUVAR(VCPU),%r ## temp_reg ; \
26 movb $1,EVTCHN_UPCALL_MASK(%r ## temp_reg);
27
28 #define STI(temp_reg) \
29 movq CPUVAR(VCPU),%r ## temp_reg ; \
30 movb $0,EVTCHN_UPCALL_MASK(%r ## temp_reg);
31
32 #else /* XEN */
33 #define XEN_ONLY2(x,y)
34 #define NOT_XEN(x) x
35 #define CLI(temp_reg) cli
36 #define STI(temp_reg) sti
37 #endif /* XEN */
38
39 #define HP_NAME_CLAC 1
40 #define HP_NAME_STAC 2
41 #define HP_NAME_NOLOCK 3
42 #define HP_NAME_RETFENCE 4
43 #define HP_NAME_SVS_ENTER 5
44 #define HP_NAME_SVS_LEAVE 6
45 #define HP_NAME_SVS_ENTER_ALT 7
46 #define HP_NAME_SVS_LEAVE_ALT 8
47 #define HP_NAME_IBRS_ENTER 9
48 #define HP_NAME_IBRS_LEAVE 10
49
50 #define HOTPATCH(name, size) \
51 123: ; \
52 .pushsection .rodata.hotpatch, "a" ; \
53 .byte name ; \
54 .byte size ; \
55 .quad 123b ; \
56 .popsection
57
58 #define SMAP_ENABLE \
59 HOTPATCH(HP_NAME_CLAC, 3) ; \
60 .byte 0x0F, 0x1F, 0x00 ; \
61
62 #define SMAP_DISABLE \
63 HOTPATCH(HP_NAME_STAC, 3) ; \
64 .byte 0x0F, 0x1F, 0x00 ; \
65
66 /*
67 * IBRS
68 */
69
70 #define IBRS_ENTER_BYTES 17
71 #define IBRS_ENTER \
72 HOTPATCH(HP_NAME_IBRS_ENTER, IBRS_ENTER_BYTES) ; \
73 NOIBRS_ENTER
74 #define NOIBRS_ENTER \
75 .byte 0xEB, (IBRS_ENTER_BYTES-2) /* jmp */ ; \
76 .fill (IBRS_ENTER_BYTES-2),1,0xCC
77
78 #define IBRS_LEAVE_BYTES 21
79 #define IBRS_LEAVE \
80 HOTPATCH(HP_NAME_IBRS_LEAVE, IBRS_LEAVE_BYTES) ; \
81 NOIBRS_LEAVE
82 #define NOIBRS_LEAVE \
83 .byte 0xEB, (IBRS_LEAVE_BYTES-2) /* jmp */ ; \
84 .fill (IBRS_LEAVE_BYTES-2),1,0xCC
85
86 #define SWAPGS NOT_XEN(swapgs)
87
88 /*
89 * These are used on interrupt or trap entry or exit.
90 */
91 #define INTR_SAVE_GPRS \
92 movq %rdi,TF_RDI(%rsp) ; \
93 movq %rsi,TF_RSI(%rsp) ; \
94 movq %rdx,TF_RDX(%rsp) ; \
95 movq %rcx,TF_RCX(%rsp) ; \
96 movq %r8,TF_R8(%rsp) ; \
97 movq %r9,TF_R9(%rsp) ; \
98 movq %r10,TF_R10(%rsp) ; \
99 movq %r11,TF_R11(%rsp) ; \
100 movq %r12,TF_R12(%rsp) ; \
101 movq %r13,TF_R13(%rsp) ; \
102 movq %r14,TF_R14(%rsp) ; \
103 movq %r15,TF_R15(%rsp) ; \
104 movq %rbp,TF_RBP(%rsp) ; \
105 movq %rbx,TF_RBX(%rsp) ; \
106 movq %rax,TF_RAX(%rsp)
107
108 #define INTR_RESTORE_GPRS \
109 movq TF_RDI(%rsp),%rdi ; \
110 movq TF_RSI(%rsp),%rsi ; \
111 movq TF_RDX(%rsp),%rdx ; \
112 movq TF_RCX(%rsp),%rcx ; \
113 movq TF_R8(%rsp),%r8 ; \
114 movq TF_R9(%rsp),%r9 ; \
115 movq TF_R10(%rsp),%r10 ; \
116 movq TF_R11(%rsp),%r11 ; \
117 movq TF_R12(%rsp),%r12 ; \
118 movq TF_R13(%rsp),%r13 ; \
119 movq TF_R14(%rsp),%r14 ; \
120 movq TF_R15(%rsp),%r15 ; \
121 movq TF_RBP(%rsp),%rbp ; \
122 movq TF_RBX(%rsp),%rbx ; \
123 movq TF_RAX(%rsp),%rax
124
125 #define TEXT_USER_BEGIN .pushsection .text.user, "ax"
126 #define TEXT_USER_END .popsection
127
128 #ifdef SVS
129
130 /* XXX: put this somewhere else */
131 #define SVS_UTLS 0xffffc00000000000 /* PMAP_PCPU_BASE */
132 #define UTLS_KPDIRPA 0
133 #define UTLS_SCRATCH 8
134 #define UTLS_RSP0 16
135
136 #define SVS_ENTER_BYTES 22
137 #define NOSVS_ENTER \
138 .byte 0xEB, (SVS_ENTER_BYTES-2) /* jmp */ ; \
139 .fill (SVS_ENTER_BYTES-2),1,0xCC
140 #define SVS_ENTER \
141 HOTPATCH(HP_NAME_SVS_ENTER, SVS_ENTER_BYTES) ; \
142 NOSVS_ENTER
143
144 #define SVS_LEAVE_BYTES 31
145 #define NOSVS_LEAVE \
146 .byte 0xEB, (SVS_LEAVE_BYTES-2) /* jmp */ ; \
147 .fill (SVS_LEAVE_BYTES-2),1,0xCC
148 #define SVS_LEAVE \
149 HOTPATCH(HP_NAME_SVS_LEAVE, SVS_LEAVE_BYTES) ; \
150 NOSVS_LEAVE
151
152 #define SVS_ENTER_ALT_BYTES 23
153 #define NOSVS_ENTER_ALTSTACK \
154 .byte 0xEB, (SVS_ENTER_ALT_BYTES-2) /* jmp */ ; \
155 .fill (SVS_ENTER_ALT_BYTES-2),1,0xCC
156 #define SVS_ENTER_ALTSTACK \
157 HOTPATCH(HP_NAME_SVS_ENTER_ALT, SVS_ENTER_ALT_BYTES) ; \
158 NOSVS_ENTER_ALTSTACK
159
160 #define SVS_LEAVE_ALT_BYTES 22
161 #define NOSVS_LEAVE_ALTSTACK \
162 .byte 0xEB, (SVS_LEAVE_ALT_BYTES-2) /* jmp */ ; \
163 .fill (SVS_LEAVE_ALT_BYTES-2),1,0xCC
164 #define SVS_LEAVE_ALTSTACK \
165 HOTPATCH(HP_NAME_SVS_LEAVE_ALT, SVS_LEAVE_ALT_BYTES) ; \
166 NOSVS_LEAVE_ALTSTACK
167
168 #else
169 #define SVS_ENTER /* nothing */
170 #define SVS_LEAVE /* nothing */
171 #define SVS_ENTER_ALTSTACK /* nothing */
172 #define SVS_LEAVE_ALTSTACK /* nothing */
173 #endif
174
175 #define INTRENTRY \
176 subq $TF_REGSIZE,%rsp ; \
177 INTR_SAVE_GPRS ; \
178 cld ; \
179 SMAP_ENABLE ; \
180 testb $SEL_UPL,TF_CS(%rsp) ; \
181 je 98f ; \
182 SWAPGS ; \
183 IBRS_ENTER ; \
184 SVS_ENTER ; \
185 movw %gs,TF_GS(%rsp) ; \
186 movw %fs,TF_FS(%rsp) ; \
187 movw %es,TF_ES(%rsp) ; \
188 movw %ds,TF_DS(%rsp) ; \
189 98:
190
191 #define INTRFASTEXIT \
192 jmp intrfastexit
193
194 #define INTR_RECURSE_HWFRAME \
195 movq %rsp,%r10 ; \
196 movl %ss,%r11d ; \
197 pushq %r11 ; \
198 pushq %r10 ; \
199 pushfq ; \
200 pushq $GSEL(GCODE_SEL,SEL_KPL); \
201 /* XEN: We must fixup CS, as even kernel mode runs at CPL 3 */ \
202 XEN_ONLY2(andb $0xfc,(%rsp);) \
203 pushq %r13 ;
204
205 #define INTR_RECURSE_ENTRY \
206 subq $TF_REGSIZE,%rsp ; \
207 INTR_SAVE_GPRS ; \
208 cld
209
210 #define CHECK_DEFERRED_SWITCH \
211 cmpl $0, CPUVAR(WANT_PMAPLOAD)
212
213 #define CHECK_ASTPENDING(reg) cmpl $0, L_MD_ASTPENDING(reg)
214 #define CLEAR_ASTPENDING(reg) movl $0, L_MD_ASTPENDING(reg)
215
216 #endif /* _AMD64_MACHINE_FRAMEASM_H */
217