xref: /openbsd-src/sys/arch/arm64/include/asm.h (revision aa60921aa22cd2c4d517dfbcb04731f5614b8130)
1 /*	$OpenBSD: asm.h,v 1.12 2023/03/27 19:02:48 kettenis Exp $	*/
2 /*	$NetBSD: asm.h,v 1.4 2001/07/16 05:43:32 matt Exp $	*/
3 
4 /*
5  * Copyright (c) 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * William Jolitz.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	from: @(#)asm.h	5.5 (Berkeley) 5/7/91
36  */
37 
38 #ifndef _MACHINE_ASM_H_
39 #define _MACHINE_ASM_H_
40 
41 #define _C_LABEL(x)	x
42 #define _ASM_LABEL(x)	x
43 
44 #ifdef __STDC__
45 # define __CONCAT(x,y)	x ## y
46 # define __STRING(x)	#x
47 #else
48 # define __CONCAT(x,y)	x/**/y
49 # define __STRING(x)	"x"
50 #endif
51 
52 #ifndef _ALIGN_TEXT
53 # define _ALIGN_TEXT .align 0
54 #endif
55 
56 /*
57  * gas/arm uses @ as a single comment character and thus cannot be used here
58  * Instead it recognised the # instead of an @ symbols in .type directives
59  * We define a couple of macros so that assembly code will not be dependant
60  * on one or the other.
61  */
62 #define _ASM_TYPE_FUNCTION	#function
63 #define _ASM_TYPE_OBJECT	#object
64 /* NB == No Binding: use .globl or .weak as necessary */
65 #define _ENTRY_NB(x) \
66 	.text; _ALIGN_TEXT; .type x,_ASM_TYPE_FUNCTION; x:
67 #define _ENTRY(x)	.globl x; _ENTRY_NB(x)
68 
69 #if defined(PROF) || defined(GPROF)
70 #  define _PROF_PROLOGUE	\
71 	stp	x29, x30, [sp, #-16]!; \
72 	mov fp, sp;		\
73 	bl __mcount; 		\
74 	ldp	x29, x30, [sp], #16;
75 #else
76 # define _PROF_PROLOGUE
77 #endif
78 
79 #if defined(_RET_PROTECTOR)
80 # define RETGUARD_CALC_COOKIE(reg) \
81 	eor     reg, reg, x30
82 
83 # define RETGUARD_LOAD_RANDOM(x, reg) \
84 	adrp    reg, __CONCAT(__retguard_, x); \
85 	ldr     reg, [reg, :lo12:__CONCAT(__retguard_, x)]
86 
87 # define RETGUARD_SETUP(x, reg) \
88 	RETGUARD_SYMBOL(x); \
89 	RETGUARD_LOAD_RANDOM(x, reg); \
90 	RETGUARD_CALC_COOKIE(reg)
91 
92 # define RETGUARD_CHECK(x, reg) \
93 	RETGUARD_CALC_COOKIE(reg); \
94 	RETGUARD_LOAD_RANDOM(x, x9); \
95 	subs    reg, reg, x9; \
96 	cbz     reg, 66f; \
97 	brk     #0x1; \
98 66:
99 
100 # define RETGUARD_PUSH(reg) \
101 	str     reg, [sp, #-16]!
102 
103 # define RETGUARD_POP(reg) \
104 	ldr     reg, [sp, #16]!
105 
106 # define RETGUARD_SYMBOL(x) \
107 	.ifndef __CONCAT(__retguard_, x); \
108 	.hidden __CONCAT(__retguard_, x); \
109 	.type   __CONCAT(__retguard_, x),@object; \
110 	.pushsection .openbsd.randomdata.retguard,"aw",@progbits; \
111 	.weak   __CONCAT(__retguard_, x); \
112 	.p2align 3; \
113 	__CONCAT(__retguard_, x): ; \
114 	.xword 0; \
115 	.size __CONCAT(__retguard_, x), 8; \
116 	.popsection; \
117 	.endif
118 #else
119 # define RETGUARD_CALC_COOKIE(reg)
120 # define RETGUARD_LOAD_RANDOM(x, reg)
121 # define RETGUARD_SETUP(x, reg)
122 # define RETGUARD_CHECK(x, reg)
123 # define RETGUARD_PUSH(reg)
124 # define RETGUARD_POP(reg)
125 # define RETGUARD_SYMBOL(x)
126 #endif
127 
128 #define	ENTRY(y)	_ENTRY(y); bti c; _PROF_PROLOGUE
129 #define	ENTRY_NP(y)	_ENTRY(y); bti c
130 #define	ENTRY_NB(y)	_ENTRY_NB(y); bti c; _PROF_PROLOGUE
131 #define	ASENTRY(y)	_ENTRY(y); bti c; _PROF_PROLOGUE
132 #define	ASENTRY_NP(y)	_ENTRY(y); bti c
133 #define	END(y)		.size y, . - y
134 #define EENTRY(sym)	 .globl  sym; sym:
135 #define EEND(sym)
136 
137 #ifdef __PIC__
138 #define	PIC_SYM(x,y)	x(y)
139 #else
140 #define	PIC_SYM(x,y)	x
141 #endif
142 
143 #define	STRONG_ALIAS(alias,sym)						\
144 	.global alias;							\
145 	alias = sym
146 #define	WEAK_ALIAS(alias,sym)						\
147 	.weak alias;							\
148 	alias = sym
149 
150 #endif /* !_MACHINE_ASM_H_ */
151