asm.h revision 1.33 1 /* $NetBSD: asm.h,v 1.33 2020/04/21 11:35:02 joerg Exp $ */
2
3 /*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas of 3am Software Foundry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1990 The Regents of the University of California.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to Berkeley by
37 * William Jolitz.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * from: @(#)asm.h 5.5 (Berkeley) 5/7/91
64 */
65
66 #ifndef _ARM_ASM_H_
67 #define _ARM_ASM_H_
68
69 #include <arm/cdefs.h>
70 #if defined(_KERNEL_OPT)
71 #include "opt_cpuoptions.h"
72 #endif
73
74 #ifdef __thumb__
75 #define THUMB_INSN(n) n
76 #else
77 #define THUMB_INSN(n)
78 #endif
79
80 #define __BIT(n) (1 << (n))
81 #define __BITS(hi,lo) ((~((~0)<<((hi)+1)))&((~0)<<(lo)))
82
83 #define __LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask))
84 #define __SHIFTOUT(__x, __mask) (((__x) & (__mask)) / __LOWEST_SET_BIT(__mask))
85 #define __SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask))
86
87 #define _C_LABEL(x) x
88 #define _ASM_LABEL(x) x
89
90 #ifdef __STDC__
91 # define __CONCAT(x,y) x ## y
92 # define __STRING(x) #x
93 #else
94 # define __CONCAT(x,y) x/**/y
95 # define __STRING(x) "x"
96 #endif
97
98 #ifndef _ALIGN_TEXT
99 # define _ALIGN_TEXT .align 2
100 #endif
101
102 #ifndef _TEXT_SECTION
103 #define _TEXT_SECTION .text
104 #endif
105
106 #ifdef __arm__
107
108 .syntax unified
109
110 /*
111 * gas/arm uses @ as a single comment character and thus cannot be used here
112 * Instead it recognised the # instead of an @ symbols in .type directives
113 * We define a couple of macros so that assembly code will not be dependent
114 * on one or the other.
115 */
116 #define _ASM_TYPE_FUNCTION %function
117 #define _ASM_TYPE_OBJECT %object
118 #define _THUMB_ENTRY(x) \
119 _TEXT_SECTION; _ALIGN_TEXT; .globl x; .type x,_ASM_TYPE_FUNCTION; \
120 .thumb_func; .code 16; x:
121 #define _ARM_ENTRY(x) \
122 _TEXT_SECTION; _ALIGN_TEXT; .globl x; .type x,_ASM_TYPE_FUNCTION; \
123 .code 32; x:
124 #ifdef __thumb__
125 #define _ENTRY(x) _THUMB_ENTRY(x)
126 #else
127 #define _ENTRY(x) _ARM_ENTRY(x)
128 #endif
129 #define _END(x) .size x,.-x
130
131 #ifdef GPROF
132 # define _PROF_PROLOGUE \
133 mov ip, lr; bl __mcount
134 #else
135 # define _PROF_PROLOGUE
136 #endif
137 #endif
138
139 #ifdef __aarch64__
140 #define _ASM_TYPE_FUNCTION @function
141 #define _ASM_TYPE_OBJECT @object
142 #define _ENTRY(x) \
143 _TEXT_SECTION; _ALIGN_TEXT; .globl x; .type x,_ASM_TYPE_FUNCTION; x:
144 #define _END(x) .size x,.-x
145
146 #ifdef GPROF
147 # define _PROF_PROLOGUE \
148 mov x9, x30; bl __mcount
149 #else
150 # define _PROF_PROLOGUE
151 #endif
152
153 #ifdef __PIC__
154 #define GOTREF(x) :got:x
155 #define GOTLO12(x) :got_lo12:x
156 #else
157 #define GOTREF(x) x
158 #define GOTLO12(x) :lo12:x
159 #endif
160 #endif
161
162 #ifdef ARMV85_BTI
163 #define _BTI_PROLOGUE \
164 .byte 0x5F, 0x24, 0x03, 0xD5 /* the "bti c" instruction */
165 #else
166 #define _BTI_PROLOGUE /* nothing */
167 #endif
168
169 #define ENTRY(y) _ENTRY(_C_LABEL(y)); _BTI_PROLOGUE ; _PROF_PROLOGUE
170 #define ENTRY_NP(y) _ENTRY(_C_LABEL(y)); _BTI_PROLOGUE
171 #define ENTRY_NBTI(y) _ENTRY(_C_LABEL(y))
172 #define END(y) _END(_C_LABEL(y))
173 #define ARM_ENTRY(y) _ARM_ENTRY(_C_LABEL(y)); _PROF_PROLOGUE
174 #define ARM_ENTRY_NP(y) _ARM_ENTRY(_C_LABEL(y))
175 #define THUMB_ENTRY(y) _THUMB_ENTRY(_C_LABEL(y)); _PROF_PROLOGUE
176 #define THUMB_ENTRY_NP(y) _THUMB_ENTRY(_C_LABEL(y))
177 #define ASENTRY(y) _ENTRY(_ASM_LABEL(y)); _PROF_PROLOGUE
178 #define ASENTRY_NP(y) _ENTRY(_ASM_LABEL(y))
179 #define ASEND(y) _END(_ASM_LABEL(y))
180 #define ARM_ASENTRY(y) _ARM_ENTRY(_ASM_LABEL(y)); _PROF_PROLOGUE
181 #define ARM_ASENTRY_NP(y) _ARM_ENTRY(_ASM_LABEL(y))
182 #define THUMB_ASENTRY(y) _THUMB_ENTRY(_ASM_LABEL(y)); _PROF_PROLOGUE
183 #define THUMB_ASENTRY_NP(y) _THUMB_ENTRY(_ASM_LABEL(y))
184
185 #define ASMSTR .asciz
186
187 #ifdef __PIC__
188 #define REL_SYM(a, b) ((a) - (b))
189 #define PLT_SYM(x) x
190 #define GOT_SYM(x) PIC_SYM(x, GOT)
191 #define GOT_GET(x,got,sym) \
192 ldr x, sym; \
193 ldr x, [x, got]
194 #define GOT_INIT(got,gotsym,pclabel) \
195 ldr got, gotsym; \
196 pclabel: add got, got, pc
197 #ifdef __thumb__
198 #define GOT_INITSYM(gotsym,pclabel) \
199 .align 0; \
200 gotsym: .word _C_LABEL(_GLOBAL_OFFSET_TABLE_) - (pclabel+4)
201 #else
202 #define GOT_INITSYM(gotsym,pclabel) \
203 .align 0; \
204 gotsym: .word _C_LABEL(_GLOBAL_OFFSET_TABLE_) - (pclabel+8)
205 #endif
206
207 #ifdef __STDC__
208 #define PIC_SYM(x,y) x ## ( ## y ## )
209 #else
210 #define PIC_SYM(x,y) x/**/(/**/y/**/)
211 #endif
212
213 #else
214 #define REL_SYM(a, b) (a)
215 #define PLT_SYM(x) x
216 #define GOT_SYM(x) x
217 #define GOT_GET(x,got,sym) \
218 ldr x, sym;
219 #define GOT_INIT(got,gotsym,pclabel)
220 #define GOT_INITSYM(gotsym,pclabel)
221 #define PIC_SYM(x,y) x
222 #endif /* __PIC__ */
223
224 #define RCSID(x) .pushsection ".ident","MS",%progbits,1; \
225 .asciz x; \
226 .popsection
227
228 #define WEAK_ALIAS(alias,sym) \
229 .weak alias; \
230 alias = sym
231
232 /*
233 * STRONG_ALIAS: create a strong alias.
234 */
235 #define STRONG_ALIAS(alias,sym) \
236 .globl alias; \
237 alias = sym
238
239 #ifdef __STDC__
240 #define WARN_REFERENCES(sym,msg) \
241 .pushsection .gnu.warning. ## sym; \
242 .ascii msg; \
243 .popsection
244 #else
245 #define WARN_REFERENCES(sym,msg) \
246 .pushsection .gnu.warning./**/sym; \
247 .ascii msg; \
248 .popsection
249 #endif /* __STDC__ */
250
251 #ifdef __thumb__
252 # define XPUSH push
253 # define XPOP pop
254 # define XPOPRET pop {pc}
255 #else
256 # define XPUSH stmfd sp!,
257 # define XPOP ldmfd sp!,
258 # ifdef _ARM_ARCH_5
259 # define XPOPRET ldmfd sp!, {pc}
260 # else
261 # define XPOPRET ldmfd sp!, {lr}; mov pc, lr
262 # endif
263 #endif
264
265 #if defined(__aarch64__)
266 # define RET ret
267 #elif defined (_ARM_ARCH_4T)
268 # define RET bx lr
269 # define RETr(r) bx r
270 # if defined(__thumb__)
271 # if defined(_ARM_ARCH_7)
272 # define RETc(c) it c; __CONCAT(bx,c) lr
273 # endif
274 # else
275 # define RETc(c) __CONCAT(bx,c) lr
276 # endif
277 #else
278 # define RET mov pc, lr
279 # define RETr(r) mov pc, r
280 # define RETc(c) __CONCAT(mov,c) pc, lr
281 #endif
282
283 #ifdef _ARM_ARCH_7
284 #define KMODTRAMPOLINE(n) \
285 _ENTRY(__wrap_ ## n) \
286 movw ip, #:lower16:n; \
287 movt ip, #:upper16:n; \
288 bx ip
289 #elif defined(_ARM_ARCH_4T)
290 #define KMODTRAMPOLINE(n) \
291 _ENTRY(__wrap_ ## n) \
292 ldr ip, [pc]; \
293 bx ip; \
294 .word n
295 #else
296 #define KMODTRAMPOLINE(n) \
297 _ENTRY(__wrap_ ## n) \
298 ldr pc, [pc, #-4]; \
299 .word n
300 #endif
301
302 #endif /* !_ARM_ASM_H_ */
303