asm.h revision 1.28 1 /* $NetBSD: asm.h,v 1.28 2018/04/01 04:35:04 ryo 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
71 #ifdef __thumb__
72 #define THUMB_INSN(n) n
73 #else
74 #define THUMB_INSN(n)
75 #endif
76
77 #define __BIT(n) (1 << (n))
78 #define __BITS(hi,lo) ((~((~0)<<((hi)+1)))&((~0)<<(lo)))
79
80 #define __LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask))
81 #define __SHIFTOUT(__x, __mask) (((__x) & (__mask)) / __LOWEST_SET_BIT(__mask))
82 #define __SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask))
83
84 #define _C_LABEL(x) x
85 #define _ASM_LABEL(x) x
86
87 #ifdef __STDC__
88 # define __CONCAT(x,y) x ## y
89 # define __STRING(x) #x
90 #else
91 # define __CONCAT(x,y) x/**/y
92 # define __STRING(x) "x"
93 #endif
94
95 #ifndef _ALIGN_TEXT
96 # define _ALIGN_TEXT .align 2
97 #endif
98
99 #ifndef _TEXT_SECTION
100 #define _TEXT_SECTION .text
101 #endif
102
103 #ifdef __arm__
104
105 .syntax unified
106
107 /*
108 * gas/arm uses @ as a single comment character and thus cannot be used here
109 * Instead it recognised the # instead of an @ symbols in .type directives
110 * We define a couple of macros so that assembly code will not be dependent
111 * on one or the other.
112 */
113 #define _ASM_TYPE_FUNCTION %function
114 #define _ASM_TYPE_OBJECT %object
115 #define _THUMB_ENTRY(x) \
116 _TEXT_SECTION; _ALIGN_TEXT; .globl x; .type x,_ASM_TYPE_FUNCTION; \
117 .thumb_func; .code 16; x:
118 #define _ARM_ENTRY(x) \
119 _TEXT_SECTION; _ALIGN_TEXT; .globl x; .type x,_ASM_TYPE_FUNCTION; \
120 .code 32; x:
121 #ifdef __thumb__
122 #define _ENTRY(x) _THUMB_ENTRY(x)
123 #else
124 #define _ENTRY(x) _ARM_ENTRY(x)
125 #endif
126 #define _END(x) .size x,.-x
127
128 #ifdef GPROF
129 # define _PROF_PROLOGUE \
130 mov ip, lr; bl __mcount
131 #else
132 # define _PROF_PROLOGUE
133 #endif
134 #endif
135
136 #ifdef __aarch64__
137 #define _ASM_TYPE_FUNCTION @function
138 #define _ASM_TYPE_OBJECT @object
139 #define _ENTRY(x) \
140 _TEXT_SECTION; _ALIGN_TEXT; .globl x; .type x,_ASM_TYPE_FUNCTION; x:
141 #define _END(x) .size x,.-x
142
143 #ifdef GPROF
144 # define _PROF_PROLOGUE \
145 mov x9, x30; bl __mcount
146 #else
147 # define _PROF_PROLOGUE
148 #endif
149
150 #ifdef __PIC__
151 #define GOTREF(x) :got:x
152 #define GOTLO12(x) :got_lo12:x
153 #else
154 #define GOTREF(x) x
155 #define GOTLO12(x) :lo12:x
156 #endif
157 #endif
158
159 #define ENTRY(y) _ENTRY(_C_LABEL(y)); _PROF_PROLOGUE
160 #define ENTRY_NP(y) _ENTRY(_C_LABEL(y))
161 #define END(y) _END(_C_LABEL(y))
162 #define ARM_ENTRY(y) _ARM_ENTRY(_C_LABEL(y)); _PROF_PROLOGUE
163 #define ARM_ENTRY_NP(y) _ARM_ENTRY(_C_LABEL(y))
164 #define THUMB_ENTRY(y) _THUMB_ENTRY(_C_LABEL(y)); _PROF_PROLOGUE
165 #define THUMB_ENTRY_NP(y) _THUMB_ENTRY(_C_LABEL(y))
166 #define ASENTRY(y) _ENTRY(_ASM_LABEL(y)); _PROF_PROLOGUE
167 #define ASENTRY_NP(y) _ENTRY(_ASM_LABEL(y))
168 #define ASEND(y) _END(_ASM_LABEL(y))
169 #define ARM_ASENTRY(y) _ARM_ENTRY(_ASM_LABEL(y)); _PROF_PROLOGUE
170 #define ARM_ASENTRY_NP(y) _ARM_ENTRY(_ASM_LABEL(y))
171 #define THUMB_ASENTRY(y) _THUMB_ENTRY(_ASM_LABEL(y)); _PROF_PROLOGUE
172 #define THUMB_ASENTRY_NP(y) _THUMB_ENTRY(_ASM_LABEL(y))
173
174 #define ASMSTR .asciz
175
176 #ifdef __PIC__
177 #define REL_SYM(a, b) ((a) - (b))
178 #define PLT_SYM(x) x
179 #define GOT_SYM(x) PIC_SYM(x, GOT)
180 #define GOT_GET(x,got,sym) \
181 ldr x, sym; \
182 ldr x, [x, got]
183 #define GOT_INIT(got,gotsym,pclabel) \
184 ldr got, gotsym; \
185 pclabel: add got, got, pc
186 #ifdef __thumb__
187 #define GOT_INITSYM(gotsym,pclabel) \
188 .align 0; \
189 gotsym: .word _C_LABEL(_GLOBAL_OFFSET_TABLE_) - (pclabel+4)
190 #else
191 #define GOT_INITSYM(gotsym,pclabel) \
192 .align 0; \
193 gotsym: .word _C_LABEL(_GLOBAL_OFFSET_TABLE_) - (pclabel+8)
194 #endif
195
196 #ifdef __STDC__
197 #define PIC_SYM(x,y) x ## ( ## y ## )
198 #else
199 #define PIC_SYM(x,y) x/**/(/**/y/**/)
200 #endif
201
202 #else
203 #define REL_SYM(a, b) (a)
204 #define PLT_SYM(x) x
205 #define GOT_SYM(x) x
206 #define GOT_GET(x,got,sym) \
207 ldr x, sym;
208 #define GOT_INIT(got,gotsym,pclabel)
209 #define GOT_INITSYM(gotsym,pclabel)
210 #define PIC_SYM(x,y) x
211 #endif /* __PIC__ */
212
213 #define RCSID(x) .pushsection ".ident"; .asciz x; .popsection
214
215 #define WEAK_ALIAS(alias,sym) \
216 .weak alias; \
217 alias = sym
218
219 /*
220 * STRONG_ALIAS: create a strong alias.
221 */
222 #define STRONG_ALIAS(alias,sym) \
223 .globl alias; \
224 alias = sym
225
226 #ifdef __STDC__
227 #define WARN_REFERENCES(sym,msg) \
228 .pushsection .gnu.warning. ## sym; \
229 .ascii msg; \
230 .popsection
231 #else
232 #define WARN_REFERENCES(sym,msg) \
233 .pushsection .gnu.warning./**/sym; \
234 .ascii msg; \
235 .popsection
236 #endif /* __STDC__ */
237
238 #ifdef __thumb__
239 # define XPUSH push
240 # define XPOP pop
241 # define XPOPRET pop {pc}
242 #else
243 # define XPUSH stmfd sp!,
244 # define XPOP ldmfd sp!,
245 # ifdef _ARM_ARCH_5
246 # define XPOPRET ldmfd sp!, {pc}
247 # else
248 # define XPOPRET ldmfd sp!, {lr}; mov pc, lr
249 # endif
250 #endif
251
252 #if defined(__aarch64__)
253 # define RET ret
254 #elif defined (_ARM_ARCH_4T)
255 # define RET bx lr
256 # define RETr(r) bx r
257 # if defined(__thumb__)
258 # if defined(_ARM_ARCH_7)
259 # define RETc(c) it c; __CONCAT(bx,c) lr
260 # endif
261 # else
262 # define RETc(c) __CONCAT(bx,c) lr
263 # endif
264 #else
265 # define RET mov pc, lr
266 # define RETr(r) mov pc, r
267 # define RETc(c) __CONCAT(mov,c) pc, lr
268 #endif
269
270 #ifdef _ARM_ARCH_7
271 #define KMODTRAMPOLINE(n) \
272 _ENTRY(__wrap_ ## n) \
273 movw ip, #:lower16:n; \
274 movt ip, #:upper16:n; \
275 bx ip
276 #elif defined(_ARM_ARCH_4T)
277 #define KMODTRAMPOLINE(n) \
278 _ENTRY(__wrap_ ## n) \
279 ldr ip, [pc]; \
280 bx ip; \
281 .word n
282 #else
283 #define KMODTRAMPOLINE(n) \
284 _ENTRY(__wrap_ ## n) \
285 ldr pc, [pc, #-4]; \
286 .word n
287 #endif
288
289 #endif /* !_ARM_ASM_H_ */
290