xref: /netbsd-src/sys/arch/i386/include/frameasm.h (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: frameasm.h,v 1.26 2018/06/17 15:46:39 maxv Exp $	*/
2 
3 #ifndef _I386_FRAMEASM_H_
4 #define _I386_FRAMEASM_H_
5 
6 #ifdef _KERNEL_OPT
7 #include "opt_multiprocessor.h"
8 #include "opt_xen.h"
9 #endif
10 
11 #if !defined(XEN)
12 #define CLI(reg)	cli
13 #define STI(reg)	sti
14 #else
15 /* XXX assym.h */
16 #define TRAP_INSTR	int	$0x82
17 #define XEN_BLOCK_EVENTS(reg)	movb	$1,EVTCHN_UPCALL_MASK(reg)
18 #define XEN_UNBLOCK_EVENTS(reg)	movb	$0,EVTCHN_UPCALL_MASK(reg)
19 #define XEN_TEST_PENDING(reg)	testb	$0xFF,EVTCHN_UPCALL_PENDING(reg)
20 
21 #define CLI(reg)	movl	CPUVAR(VCPU),reg ;  \
22 			XEN_BLOCK_EVENTS(reg)
23 #define STI(reg)	movl	CPUVAR(VCPU),reg ;  \
24 			XEN_UNBLOCK_EVENTS(reg)
25 #define STIC(reg)	movl	CPUVAR(VCPU),reg ;  \
26 			XEN_UNBLOCK_EVENTS(reg)  ; \
27 			testb	$0xff,EVTCHN_UPCALL_PENDING(reg)
28 #endif
29 
30 #define HP_NAME_CLAC		1
31 #define HP_NAME_STAC		2
32 #define HP_NAME_NOLOCK		3
33 #define HP_NAME_RETFENCE	4
34 
35 #define HOTPATCH(name, size) \
36 123:						; \
37 	.pushsection	.rodata.hotpatch, "a"	; \
38 	.byte		name			; \
39 	.byte		size			; \
40 	.long		123b			; \
41 	.popsection
42 
43 #define SMAP_ENABLE \
44 	HOTPATCH(HP_NAME_CLAC, 3)		; \
45 	.byte 0x90, 0x90, 0x90
46 
47 #define SMAP_DISABLE \
48 	HOTPATCH(HP_NAME_STAC, 3)		; \
49 	.byte 0x90, 0x90, 0x90
50 
51 /*
52  * These are used on interrupt or trap entry or exit.
53  */
54 #define	INTRENTRY \
55 	SMAP_ENABLE			; \
56 	subl	$TF_PUSHSIZE,%esp	; \
57 	movw	%gs,TF_GS(%esp)		; \
58 	movw	%fs,TF_FS(%esp) 	; \
59 	movl	%eax,TF_EAX(%esp)	; \
60 	movw	%es,TF_ES(%esp) 	; \
61 	movw	%ds,TF_DS(%esp) 	; \
62 	movl	$GSEL(GDATA_SEL, SEL_KPL),%eax	; \
63 	movl	%edi,TF_EDI(%esp)	; \
64 	movl	%esi,TF_ESI(%esp)	; \
65 	movw	%ax,%ds			; \
66 	movl	%ebp,TF_EBP(%esp)	; \
67 	movw	%ax,%es			; \
68 	movl	%ebx,TF_EBX(%esp)	; \
69 	movw	%ax,%gs			; \
70 	movl	%edx,TF_EDX(%esp)	; \
71 	movl	$GSEL(GCPU_SEL, SEL_KPL),%eax	; \
72 	movl	%ecx,TF_ECX(%esp)	; \
73 	movl	%eax,%fs		; \
74 	cld
75 
76 #define	INTRFASTEXIT \
77 	jmp	intrfastexit
78 
79 #define INTR_RECURSE_HWFRAME \
80 	pushfl				; \
81 	pushl	%cs			; \
82 	pushl	%esi			;
83 
84 #define	CHECK_DEFERRED_SWITCH \
85 	cmpl	$0, CPUVAR(WANT_PMAPLOAD)
86 
87 #define	CHECK_ASTPENDING(reg)	movl	CPUVAR(CURLWP),reg	; \
88 				cmpl	$0, L_MD_ASTPENDING(reg)
89 #define	CLEAR_ASTPENDING(reg)	movl	$0, L_MD_ASTPENDING(reg)
90 
91 /*
92  * IDEPTH_INCR:
93  * increase ci_idepth and switch to the interrupt stack if necessary.
94  * note that the initial value of ci_idepth is -1.
95  *
96  * => should be called with interrupt disabled.
97  * => save the old value of %esp in %eax.
98  */
99 
100 #define	IDEPTH_INCR \
101 	incl	CPUVAR(IDEPTH); \
102 	movl	%esp, %eax; \
103 	jne	999f; \
104 	movl	CPUVAR(INTRSTACK), %esp; \
105 999:	pushl	%eax; /* eax == pointer to intrframe */ \
106 
107 /*
108  * IDEPTH_DECR:
109  * decrement ci_idepth and switch back to
110  * the original stack saved by IDEPTH_INCR.
111  *
112  * => should be called with interrupt disabled.
113  */
114 
115 #define	IDEPTH_DECR \
116 	popl	%esp; \
117 	decl	CPUVAR(IDEPTH)
118 
119 #endif /* _I386_FRAMEASM_H_ */
120