Home | History | Annotate | Line # | Download | only in include
frameasm.h revision 1.31
      1  1.31    maxv /*	$NetBSD: frameasm.h,v 1.31 2018/01/21 11:21:40 maxv Exp $	*/
      2   1.1    fvdl 
      3   1.1    fvdl #ifndef _AMD64_MACHINE_FRAMEASM_H
      4   1.1    fvdl #define _AMD64_MACHINE_FRAMEASM_H
      5  1.13      ad 
      6  1.13      ad #ifdef _KERNEL_OPT
      7   1.8  bouyer #include "opt_xen.h"
      8  1.27    maxv #include "opt_svs.h"
      9  1.13      ad #endif
     10   1.1    fvdl 
     11   1.1    fvdl /*
     12   1.1    fvdl  * Macros to define pushing/popping frames for interrupts, traps
     13   1.1    fvdl  * and system calls. Currently all the same; will diverge later.
     14   1.1    fvdl  */
     15   1.1    fvdl 
     16   1.8  bouyer #ifdef XEN
     17   1.8  bouyer #define HYPERVISOR_iret hypercall_page + (__HYPERVISOR_iret * 32)
     18   1.8  bouyer /* Xen do not need swapgs, done by hypervisor */
     19   1.8  bouyer #define swapgs
     20   1.8  bouyer #define iretq	pushq $0 ; jmp HYPERVISOR_iret
     21  1.17     dsl #define	XEN_ONLY2(x,y)	x,y
     22  1.17     dsl #define	NOT_XEN(x)
     23  1.17     dsl 
     24  1.17     dsl #define CLI(temp_reg) \
     25  1.17     dsl  	movq CPUVAR(VCPU),%r ## temp_reg ;			\
     26  1.17     dsl 	movb $1,EVTCHN_UPCALL_MASK(%r ## temp_reg);
     27  1.17     dsl 
     28  1.17     dsl #define STI(temp_reg) \
     29  1.17     dsl  	movq CPUVAR(VCPU),%r ## temp_reg ;			\
     30  1.17     dsl 	movb $0,EVTCHN_UPCALL_MASK(%r ## temp_reg);
     31  1.17     dsl 
     32  1.17     dsl #else /* XEN */
     33  1.17     dsl #define	XEN_ONLY2(x,y)
     34  1.17     dsl #define	NOT_XEN(x)	x
     35  1.17     dsl #define CLI(temp_reg) cli
     36  1.17     dsl #define STI(temp_reg) sti
     37  1.17     dsl #endif	/* XEN */
     38   1.8  bouyer 
     39  1.26    maxv #define HP_NAME_CLAC		1
     40  1.26    maxv #define HP_NAME_STAC		2
     41  1.26    maxv #define HP_NAME_NOLOCK		3
     42  1.26    maxv #define HP_NAME_RETFENCE	4
     43  1.24    maxv 
     44  1.24    maxv #define HOTPATCH(name, size) \
     45  1.24    maxv 123:						; \
     46  1.30    maxv 	.pushsection	.rodata.hotpatch, "a"	; \
     47  1.24    maxv 	.byte		name			; \
     48  1.24    maxv 	.byte		size			; \
     49  1.24    maxv 	.quad		123b			; \
     50  1.30    maxv 	.popsection
     51  1.24    maxv 
     52  1.24    maxv #define SMAP_ENABLE \
     53  1.24    maxv 	HOTPATCH(HP_NAME_CLAC, 3)		; \
     54  1.24    maxv 	.byte 0x0F, 0x1F, 0x00			; \
     55  1.24    maxv 
     56  1.24    maxv #define SMAP_DISABLE \
     57  1.24    maxv 	HOTPATCH(HP_NAME_STAC, 3)		; \
     58  1.24    maxv 	.byte 0x0F, 0x1F, 0x00			; \
     59  1.24    maxv 
     60  1.20     dsl #define	SWAPGS	NOT_XEN(swapgs)
     61  1.20     dsl 
     62   1.1    fvdl /*
     63   1.1    fvdl  * These are used on interrupt or trap entry or exit.
     64   1.1    fvdl  */
     65   1.1    fvdl #define INTR_SAVE_GPRS \
     66   1.1    fvdl 	movq	%rdi,TF_RDI(%rsp)	; \
     67   1.1    fvdl 	movq	%rsi,TF_RSI(%rsp)	; \
     68  1.10     dsl 	movq	%rdx,TF_RDX(%rsp)	; \
     69  1.10     dsl 	movq	%rcx,TF_RCX(%rsp)	; \
     70  1.10     dsl 	movq	%r8,TF_R8(%rsp)		; \
     71  1.10     dsl 	movq	%r9,TF_R9(%rsp)		; \
     72  1.10     dsl 	movq	%r10,TF_R10(%rsp)	; \
     73  1.10     dsl 	movq	%r11,TF_R11(%rsp)	; \
     74  1.10     dsl 	movq	%r12,TF_R12(%rsp)	; \
     75  1.10     dsl 	movq	%r13,TF_R13(%rsp)	; \
     76  1.10     dsl 	movq	%r14,TF_R14(%rsp)	; \
     77  1.10     dsl 	movq	%r15,TF_R15(%rsp)	; \
     78   1.1    fvdl 	movq	%rbp,TF_RBP(%rsp)	; \
     79   1.1    fvdl 	movq	%rbx,TF_RBX(%rsp)	; \
     80  1.23    maxv 	movq	%rax,TF_RAX(%rsp)
     81   1.1    fvdl 
     82   1.1    fvdl #define	INTR_RESTORE_GPRS \
     83   1.1    fvdl 	movq	TF_RDI(%rsp),%rdi	; \
     84   1.1    fvdl 	movq	TF_RSI(%rsp),%rsi	; \
     85  1.10     dsl 	movq	TF_RDX(%rsp),%rdx	; \
     86  1.10     dsl 	movq	TF_RCX(%rsp),%rcx	; \
     87  1.10     dsl 	movq	TF_R8(%rsp),%r8		; \
     88  1.10     dsl 	movq	TF_R9(%rsp),%r9		; \
     89  1.10     dsl 	movq	TF_R10(%rsp),%r10	; \
     90  1.10     dsl 	movq	TF_R11(%rsp),%r11	; \
     91  1.10     dsl 	movq	TF_R12(%rsp),%r12	; \
     92  1.10     dsl 	movq	TF_R13(%rsp),%r13	; \
     93  1.10     dsl 	movq	TF_R14(%rsp),%r14	; \
     94  1.10     dsl 	movq	TF_R15(%rsp),%r15	; \
     95   1.1    fvdl 	movq	TF_RBP(%rsp),%rbp	; \
     96   1.1    fvdl 	movq	TF_RBX(%rsp),%rbx	; \
     97  1.10     dsl 	movq	TF_RAX(%rsp),%rax
     98   1.1    fvdl 
     99  1.31    maxv #define TEXT_USER_BEGIN	.pushsection	.text.user, "ax"
    100  1.31    maxv #define TEXT_USER_END	.popsection
    101  1.31    maxv 
    102  1.27    maxv #ifdef SVS
    103  1.29    maxv 
    104  1.29    maxv /* XXX: put this somewhere else */
    105  1.29    maxv #define SVS_UTLS		0xffffc00000000000 /* PMAP_PCPU_BASE */
    106  1.29    maxv #define UTLS_KPDIRPA		0
    107  1.29    maxv #define UTLS_SCRATCH		8
    108  1.29    maxv #define UTLS_RSP0		16
    109  1.29    maxv 
    110  1.27    maxv #define SVS_ENTER \
    111  1.29    maxv 	movq	SVS_UTLS+UTLS_KPDIRPA,%rax	; \
    112  1.29    maxv 	movq	%rax,%cr3			; \
    113  1.29    maxv 	movq	CPUVAR(KRSP0),%rsp
    114  1.29    maxv 
    115  1.27    maxv #define SVS_LEAVE \
    116  1.29    maxv 	testb	$SEL_UPL,TF_CS(%rsp)		; \
    117  1.29    maxv 	jz	1234f				; \
    118  1.29    maxv 	movq	CPUVAR(URSP0),%rsp		; \
    119  1.29    maxv 	movq	CPUVAR(UPDIRPA),%rax		; \
    120  1.29    maxv 	movq	%rax,%cr3			; \
    121  1.29    maxv 1234:
    122  1.29    maxv 
    123  1.29    maxv #define SVS_ENTER_ALTSTACK \
    124  1.29    maxv 	testb	$SEL_UPL,TF_CS(%rsp)		; \
    125  1.29    maxv 	jz	1234f				; \
    126  1.29    maxv 	movq	SVS_UTLS+UTLS_KPDIRPA,%rax	; \
    127  1.29    maxv 	movq	%rax,%cr3			; \
    128  1.29    maxv 1234:
    129  1.29    maxv 
    130  1.29    maxv #define SVS_LEAVE_ALTSTACK \
    131  1.29    maxv 	testb	$SEL_UPL,TF_CS(%rsp)		; \
    132  1.29    maxv 	jz	1234f				; \
    133  1.29    maxv 	movq	CPUVAR(UPDIRPA),%rax		; \
    134  1.29    maxv 	movq	%rax,%cr3			; \
    135  1.29    maxv 1234:
    136  1.27    maxv #else
    137  1.27    maxv #define SVS_ENTER	/* nothing */
    138  1.27    maxv #define SVS_LEAVE	/* nothing */
    139  1.29    maxv #define SVS_ENTER_ALTSTACK	/* nothing */
    140  1.29    maxv #define SVS_LEAVE_ALTSTACK	/* nothing */
    141  1.27    maxv #endif
    142  1.27    maxv 
    143  1.19     dsl #define	INTRENTRY_L(kernel_trap, usertrap) \
    144  1.10     dsl 	subq	$TF_REGSIZE,%rsp	; \
    145  1.17     dsl 	INTR_SAVE_GPRS			; \
    146  1.23    maxv 	cld				; \
    147  1.24    maxv 	SMAP_ENABLE			; \
    148  1.17     dsl 	testb	$SEL_UPL,TF_CS(%rsp)	; \
    149  1.17     dsl 	je	kernel_trap		; \
    150  1.19     dsl usertrap				; \
    151  1.20     dsl 	SWAPGS				; \
    152  1.27    maxv 	SVS_ENTER			; \
    153  1.10     dsl 	movw	%gs,TF_GS(%rsp)		; \
    154  1.10     dsl 	movw	%fs,TF_FS(%rsp)		; \
    155  1.10     dsl 	movw	%es,TF_ES(%rsp)		; \
    156  1.17     dsl 	movw	%ds,TF_DS(%rsp)
    157  1.17     dsl 
    158  1.17     dsl #define	INTRENTRY \
    159  1.19     dsl 	INTRENTRY_L(98f,)		; \
    160  1.17     dsl 98:
    161   1.1    fvdl 
    162   1.1    fvdl #define INTRFASTEXIT \
    163  1.21    maxv 	jmp	intrfastexit
    164   1.1    fvdl 
    165   1.1    fvdl #define INTR_RECURSE_HWFRAME \
    166   1.1    fvdl 	movq	%rsp,%r10		; \
    167   1.1    fvdl 	movl	%ss,%r11d		; \
    168   1.1    fvdl 	pushq	%r11			; \
    169   1.1    fvdl 	pushq	%r10			; \
    170   1.1    fvdl 	pushfq				; \
    171   1.1    fvdl 	movl	%cs,%r11d		; \
    172   1.1    fvdl 	pushq	%r11			; \
    173  1.17     dsl /* XEN: We must fixup CS, as even kernel mode runs at CPL 3 */ \
    174  1.18     dsl  	XEN_ONLY2(andb	$0xfc,(%rsp);)	  \
    175   1.1    fvdl 	pushq	%r13			;
    176   1.1    fvdl 
    177   1.6    yamt #define	DO_DEFERRED_SWITCH \
    178  1.14     chs 	cmpl	$0, CPUVAR(WANT_PMAPLOAD)		; \
    179   1.6    yamt 	jz	1f					; \
    180   1.6    yamt 	call	_C_LABEL(do_pmap_load)			; \
    181  1.17     dsl 1:
    182   1.6    yamt 
    183   1.6    yamt #define	CHECK_DEFERRED_SWITCH \
    184  1.14     chs 	cmpl	$0, CPUVAR(WANT_PMAPLOAD)
    185   1.1    fvdl 
    186  1.11    yamt #define CHECK_ASTPENDING(reg)	cmpl	$0, L_MD_ASTPENDING(reg)
    187   1.2      ad #define CLEAR_ASTPENDING(reg)	movl	$0, L_MD_ASTPENDING(reg)
    188   1.1    fvdl 
    189   1.1    fvdl #endif /* _AMD64_MACHINE_FRAMEASM_H */
    190