1/* -*- mode: asm -*- */
  2/*-
  3 * SPDX-License-Identifier: BSD-3-Clause
  4 *
  5 * Copyright (c) 1993 The Regents of the University of California.
  6 * All rights reserved.
  7 *
  8 * Redistribution and use in source and binary forms, with or without
  9 * modification, are permitted provided that the following conditions
 10 * are met:
 11 * 1. Redistributions of source code must retain the above copyright
 12 *    notice, this list of conditions and the following disclaimer.
 13 * 2. Redistributions in binary form must reproduce the above copyright
 14 *    notice, this list of conditions and the following disclaimer in the
 15 *    documentation and/or other materials provided with the distribution.
 16 * 3. Neither the name of the University nor the names of its contributors
 17 *    may be used to endorse or promote products derived from this software
 18 *    without specific prior written permission.
 19 *
 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 30 * SUCH DAMAGE.
 31 */
 32
 33#ifndef _MACHINE_ASMACROS_H_
 34#define _MACHINE_ASMACROS_H_
 35
 36#include <sys/cdefs.h>
 37
 38/* XXX too much duplication in various asm*.h's. */
 39
 40/*
 41 * CNAME is used to manage the relationship between symbol names in C
 42 * and the equivalent assembly language names.  CNAME is given a name as
 43 * it would be used in a C program.  It expands to the equivalent assembly
 44 * language name.
 45 */
 46#define CNAME(csym)		csym
 47
 48#define ALIGN_DATA	.p2align 2	/* 4 byte alignment, zero filled */
 49#define ALIGN_TEXT	.p2align 2,0x90	/* 4-byte alignment, nop filled */
 50#define SUPERALIGN_TEXT	.p2align 4,0x90	/* 16-byte alignment, nop filled */
 51
 52#define GEN_ENTRY(name)		ALIGN_TEXT; .globl CNAME(name); \
 53				.type CNAME(name),@function; CNAME(name):
 54#define ENTRY(name)		GEN_ENTRY(name)
 55#define ALTENTRY(name)		GEN_ENTRY(name)
 56#define	END(name)		.size name, . - name
 57
 58#ifdef LOCORE
 59
 60#define	GSEL_KPL	0x0020	/* GSEL(GCODE_SEL, SEL_KPL) */
 61#define	SEL_RPL_MASK	0x0003
 62
 63/*
 64 * Convenience macro for declaring interrupt entry points.
 65 */
 66#define	IDTVEC(name)	ALIGN_TEXT; .globl __CONCAT(X,name); \
 67			.type __CONCAT(X,name),@function; __CONCAT(X,name):
 68
 69/*
 70 * Macros to create and destroy a trap frame.
 71 */
 72	.macro	PUSH_FRAME2
 73	pushal
 74	pushl	$0
 75	movw	%ds,(%esp)
 76	pushl	$0
 77	movw	%es,(%esp)
 78	pushl	$0
 79	movw	%fs,(%esp)
 80	movl	%esp,%ebp
 81	.endm
 82
 83	.macro	PUSH_FRAME
 84	pushl	$0		/* dummy error code */
 85	pushl	$0		/* dummy trap type */
 86	PUSH_FRAME2
 87	.endm
 88
 89/*
 90 * Access per-CPU data.
 91 */
 92#define	PCPU(member)	%fs:PC_ ## member
 93
 94#define	PCPU_ADDR(member, reg)						\
 95	movl %fs:PC_PRVSPACE, reg ;					\
 96	addl $PC_ ## member, reg
 97
 98/*
 99 * Setup the kernel segment registers.
100 */
101	.macro	SET_KERNEL_SREGS
102	movl	$KDSEL, %eax	/* reload with kernel's data segment */
103	movl	%eax, %ds
104	movl	%eax, %es
105	movl	$KPSEL, %eax	/* reload with per-CPU data segment */
106	movl	%eax, %fs
107	.endm
108
109	.macro	NMOVE_STACKS
110	movl	PCPU(KESP0), %edx
111	movl	$TF_SZ, %ecx
112	testl	$PSL_VM, TF_EFLAGS(%esp)
113	jz	.L\@.1
114	addl	$VM86_STACK_SPACE, %ecx
115.L\@.1:	subl	%ecx, %edx
116	movl	%edx, %edi
117	movl	%esp, %esi
118	rep; movsb
119	movl	%edx, %esp
120	.endm
121
122	.macro	LOAD_KCR3
123	call	.L\@.1
124.L\@.1:	popl	%eax
125	movl	(tramp_idleptd - .L\@.1)(%eax), %eax
126	movl	%eax, %cr3
127	.endm
128
129	.macro	MOVE_STACKS
130	LOAD_KCR3
131	NMOVE_STACKS
132	.endm
133
134	.macro	KENTER
135	testl	$PSL_VM, TF_EFLAGS(%esp)
136	jz	.L\@.1
137	LOAD_KCR3
138	movl	PCPU(CURPCB), %eax
139	testl	$PCB_VM86CALL, PCB_FLAGS(%eax)
140	jnz	.L\@.3
141	NMOVE_STACKS
142	movl	$handle_ibrs_entry,%edx
143	call	*%edx
144	jmp	.L\@.3
145.L\@.1:	testb	$SEL_RPL_MASK, TF_CS(%esp)
146	jz	.L\@.3
147.L\@.2:	MOVE_STACKS
148	movl	$handle_ibrs_entry,%edx
149	call	*%edx
150.L\@.3:
151	.endm
152
153#endif /* LOCORE */
154
155#ifdef __STDC__
156#define ELFNOTE(name, type, desctype, descdata...) \
157.pushsection .note.name, "a", @note     ;       \
158  .align 4                              ;       \
159  .long 2f - 1f         /* namesz */    ;       \
160  .long 4f - 3f         /* descsz */    ;       \
161  .long type                            ;       \
1621:.asciz #name                          ;       \
1632:.align 4                              ;       \
1643:desctype descdata                     ;       \
1654:.align 4                              ;       \
166.popsection
167#else /* !__STDC__, i.e. -traditional */
168#define ELFNOTE(name, type, desctype, descdata) \
169.pushsection .note.name, "a", @note     ;       \
170  .align 4                              ;       \
171  .long 2f - 1f         /* namesz */    ;       \
172  .long 4f - 3f         /* descsz */    ;       \
173  .long type                            ;       \
1741:.asciz "name"                         ;       \
1752:.align 4                              ;       \
1763:desctype descdata                     ;       \
1774:.align 4                              ;       \
178.popsection
179#endif /* __STDC__ */
180
181#endif /* !_MACHINE_ASMACROS_H_ */