| 1 | /*	$NetBSD: asm.h,v 1.39.2.1 2026/04/02 19:12:03 martin 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 | #define _INSN_SIZE	(2) |
| 77 | #else |
| 78 | #define THUMB_INSN(n) |
| 79 | #define _INSN_SIZE	(4) |
| 80 | #endif |
| 81 | |
| 82 | #define	__BIT(n)	(1 << (n)) |
| 83 | #define __BITS(hi,lo)	((~((~0)<<((hi)+1)))&((~0)<<(lo))) |
| 84 | |
| 85 | #define __LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask)) |
| 86 | #define __SHIFTOUT(__x, __mask) (((__x) & (__mask)) / __LOWEST_SET_BIT(__mask)) |
| 87 | #define __SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask)) |
| 88 | |
| 89 | #define _C_LABEL(x)	x |
| 90 | #define	_ASM_LABEL(x)	x |
| 91 | |
| 92 | #ifdef __STDC__ |
| 93 | # define __CONCAT(x,y)	x ## y |
| 94 | # define __STRING(x)	#x |
| 95 | #else |
| 96 | # define __CONCAT(x,y)	x/**/y |
| 97 | # define __STRING(x)	"x" |
| 98 | #endif |
| 99 | |
| 100 | #ifndef _ALIGN_TEXT |
| 101 | # define _ALIGN_TEXT .align 2 |
| 102 | #endif |
| 103 | |
| 104 | #ifndef _TEXT_SECTION |
| 105 | #define _TEXT_SECTION	.text |
| 106 | #endif |
| 107 | |
| 108 | #ifdef __arm__ |
| 109 | |
| 110 | 	.syntax		unified |
| 111 | |
| 112 | /* |
| 113 | * gas/arm uses @ as a single comment character and thus cannot be used here |
| 114 | * Instead it recognised the # instead of an @ symbols in .type directives |
| 115 | * We define a couple of macros so that assembly code will not be dependent |
| 116 | * on one or the other. |
| 117 | */ |
| 118 | #define _ASM_TYPE_FUNCTION	%function |
| 119 | #define _ASM_TYPE_OBJECT	%object |
| 120 | #define _THUMB_ENTRY(x) \ |
| 121 | 	_TEXT_SECTION; _ALIGN_TEXT; .globl x; .type x,_ASM_TYPE_FUNCTION; \ |
| 122 | 	.thumb_func; .code 16; x: |
| 123 | #define _ARM_ENTRY(x) \ |
| 124 | 	_TEXT_SECTION; _ALIGN_TEXT; .globl x; .type x,_ASM_TYPE_FUNCTION; \ |
| 125 | 	.code 32; x: |
| 126 | #ifdef __thumb__ |
| 127 | #define	_ENTRY(x)	_THUMB_ENTRY(x) |
| 128 | #else |
| 129 | #define	_ENTRY(x)	_ARM_ENTRY(x) |
| 130 | #endif |
| 131 | #define	_END(x)		.size x,.-x |
| 132 | |
| 133 | #ifdef GPROF |
| 134 | # define _PROF_PROLOGUE	\ |
| 135 | 	push {lr}; bl __gnu_mcount_nc |
| 136 | #else |
| 137 | # define _PROF_PROLOGUE |
| 138 | #endif |
| 139 | #endif |
| 140 | |
| 141 | #ifdef __aarch64__ |
| 142 | #define _ASM_TYPE_FUNCTION	@function |
| 143 | #define _ASM_TYPE_OBJECT	@object |
| 144 | #define _ENTRY(x) \ |
| 145 | 	_TEXT_SECTION; _ALIGN_TEXT; .globl x; .type x,_ASM_TYPE_FUNCTION; x: |
| 146 | #define	_END(x)		.size x,.-x |
| 147 | |
| 148 | #ifdef GPROF |
| 149 | # define _PROF_PROLOGUE	\ |
| 150 | 	stp	x29, x30, [sp, #-16]!;	\ |
| 151 | 	mov	fp, sp;			\ |
| 152 | 	bl	__mcount; 		\ |
| 153 | 	ldp	x29, x30, [sp], #16; |
| 154 | #else |
| 155 | # define _PROF_PROLOGUE |
| 156 | #endif |
| 157 | |
| 158 | #ifdef __PIC__ |
| 159 | #define GOTREF(x)		:got:x |
| 160 | #define GOTLO12(x)		:got_lo12:x |
| 161 | #else |
| 162 | #define GOTREF(x)		x |
| 163 | #define GOTLO12(x)		:lo12:x |
| 164 | #endif |
| 165 | #endif |
| 166 | |
| 167 | #ifdef ARMV85_BTI |
| 168 | #define	_BTI_PROLOGUE \ |
| 169 | 	.byte	0x5F, 0x24, 0x03, 0xD5	/* the "bti c" instruction */ |
| 170 | #else |
| 171 | #define	_BTI_PROLOGUE		/* nothing */ |
| 172 | #endif |
| 173 | |
| 174 | #define	ENTRY(y)		_ENTRY(_C_LABEL(y)); _BTI_PROLOGUE ; _PROF_PROLOGUE |
| 175 | #define	ENTRY_NP(y)		_ENTRY(_C_LABEL(y)); _BTI_PROLOGUE |
| 176 | #define	ENTRY_NBTI(y)		_ENTRY(_C_LABEL(y)) |
| 177 | #define	END(y)			_END(_C_LABEL(y)) |
| 178 | #define	ARM_ENTRY(y)		_ARM_ENTRY(_C_LABEL(y)); _PROF_PROLOGUE |
| 179 | #define	ARM_ENTRY_NP(y)		_ARM_ENTRY(_C_LABEL(y)) |
| 180 | #define	THUMB_ENTRY(y)		_THUMB_ENTRY(_C_LABEL(y)); _PROF_PROLOGUE |
| 181 | #define	THUMB_ENTRY_NP(y)	_THUMB_ENTRY(_C_LABEL(y)) |
| 182 | #define	ASENTRY(y)		_ENTRY(_ASM_LABEL(y)); _PROF_PROLOGUE |
| 183 | #define	ASENTRY_NP(y)		_ENTRY(_ASM_LABEL(y)) |
| 184 | #define	ASEND(y)		_END(_ASM_LABEL(y)) |
| 185 | #define	ARM_ASENTRY(y)		_ARM_ENTRY(_ASM_LABEL(y)); _PROF_PROLOGUE |
| 186 | #define	ARM_ASENTRY_NP(y)	_ARM_ENTRY(_ASM_LABEL(y)) |
| 187 | #define	THUMB_ASENTRY(y)	_THUMB_ENTRY(_ASM_LABEL(y)); _PROF_PROLOGUE |
| 188 | #define	THUMB_ASENTRY_NP(y)	_THUMB_ENTRY(_ASM_LABEL(y)) |
| 189 | |
| 190 | #define	ASMSTR		.asciz |
| 191 | |
| 192 | #ifdef __PIC__ |
| 193 | #define	REL_SYM(a, b)	((a) - (b)) |
| 194 | #define	PLT_SYM(x)	x |
| 195 | #define	GOT_SYM(x)	PIC_SYM(x, GOT) |
| 196 | #define	GOT_GET(x,got,sym)	\ |
| 197 | 	ldr	x, sym;		\ |
| 198 | 	ldr	x, [x, got] |
| 199 | |
| 200 | /* |
| 201 | * Load _GLOBAL_OFFSET_TABLE_ address into register: |
| 202 | * |
| 203 | * 0:	GOT_INIT(rX, .Lgot) |
| 204 | *	... |
| 205 | * |
| 206 | * // and in the data after the function |
| 207 | *	GOT_INITSYM(.Lgot, 0b) |
| 208 | */ |
| 209 | #define	GOT_INIT(Rgot, gotsym)	 \ |
| 210 | 	ldr	Rgot, gotsym	; \ |
| 211 | 	add	Rgot, Rgot, pc |
| 212 | #define	GOT_INITSYM(gotsym, initlabel)	\ |
| 213 | 	.align 0;		\ |
| 214 | 	gotsym:	.word _C_LABEL(_GLOBAL_OFFSET_TABLE_) - (initlabel+(1+2)*_INSN_SIZE) |
| 215 | |
| 216 | #ifdef __STDC__ |
| 217 | #define	PIC_SYM(x,y)	x(y) |
| 218 | #else |
| 219 | #define	PIC_SYM(x,y)	x/**/(/**/y/**/) |
| 220 | #endif |
| 221 | |
| 222 | #else |
| 223 | #define	REL_SYM(a, b)	(a) |
| 224 | #define	PLT_SYM(x)	x |
| 225 | #define	GOT_SYM(x)	x |
| 226 | #define	GOT_GET(x,got,sym)	\ |
| 227 | 	ldr	x, sym; |
| 228 | #define	GOT_INIT(Rgot, gotsym) |
| 229 | #define	GOT_INITSYM(gotsym, initlabel) |
| 230 | #define	PIC_SYM(x,y)	x |
| 231 | #endif	/* __PIC__ */ |
| 232 | |
| 233 | /* |
| 234 | * Annoyingly, gas on arm seems to generate _two_ NUL-terminated |
| 235 | * strings for |
| 236 | * |
| 237 | *	.asciz "foo" "bar" |
| 238 | * |
| 239 | * instead of concatenating it into a single NUL-terminated string as |
| 240 | * on other architectures. |
| 241 | * |
| 242 | * To work around this, we concatenate into a single NUL-terminated by: |
| 243 | * |
| 244 | *	.ascii "foo" |
| 245 | *	.asciz "bar" |
| 246 | */ |
| 247 | #define _IDENTSTR(x)	.pushsection ".ident","MS",%progbits,1;		\ |
| 248 | 			x;						\ |
| 249 | 			.popsection |
| 250 | |
| 251 | #ifdef _NETBSD_REVISIONID |
| 252 | #define	RCSID(_s)							\ |
| 253 | 	_IDENTSTR(.asciz _s);						\ |
| 254 | 	_IDENTSTR(.ascii "$"; .ascii "NetBSD: "; .ascii __FILE__;	\ |
| 255 | 	 .ascii " "; .ascii _NETBSD_REVISIONID; .asciz " $") |
| 256 | #else |
| 257 | #define	RCSID(_s)	_IDENTSTR(.asciz _s) |
| 258 | #endif |
| 259 | |
| 260 | #define	WEAK_ALIAS(alias,sym)						\ |
| 261 | 	.weak alias;							\ |
| 262 | 	alias = sym |
| 263 | |
| 264 | /* |
| 265 | * STRONG_ALIAS: create a strong alias. |
| 266 | */ |
| 267 | #define STRONG_ALIAS(alias,sym)						\ |
| 268 | 	.globl alias;							\ |
| 269 | 	alias = sym |
| 270 | |
| 271 | #ifdef __STDC__ |
| 272 | #define	WARN_REFERENCES(sym,msg)					\ |
| 273 | 	.pushsection .gnu.warning.sym;					\ |
| 274 | 	.ascii msg;							\ |
| 275 | 	.popsection |
| 276 | #else |
| 277 | #define	WARN_REFERENCES(sym,msg)					\ |
| 278 | 	.pushsection .gnu.warning./**/sym;				\ |
| 279 | 	.ascii msg;							\ |
| 280 | 	.popsection |
| 281 | #endif /* __STDC__ */ |
| 282 | |
| 283 | #ifdef __thumb__ |
| 284 | # define XPUSH		push |
| 285 | # define XPOP		pop |
| 286 | # define XPOPRET	pop	{pc} |
| 287 | #else |
| 288 | # define XPUSH		stmfd	sp!, |
| 289 | # define XPOP		ldmfd	sp!, |
| 290 | # ifdef _ARM_ARCH_5 |
| 291 | # define XPOPRET	ldmfd	sp!, {pc} |
| 292 | # else |
| 293 | # define XPOPRET	ldmfd	sp!, {lr}; mov pc, lr |
| 294 | # endif |
| 295 | #endif |
| 296 | |
| 297 | #if defined(__aarch64__) |
| 298 | # define RET		ret |
| 299 | #elif defined (_ARM_ARCH_4T) |
| 300 | # define RET		bx		lr |
| 301 | # define RETr(r)	bx		r |
| 302 | # if defined(__thumb__) |
| 303 | # if defined(_ARM_ARCH_7) |
| 304 | # define RETc(c)	it c; __CONCAT(bx,c)	lr |
| 305 | # endif |
| 306 | # else |
| 307 | # define RETc(c)	__CONCAT(bx,c)	lr |
| 308 | # endif |
| 309 | #else |
| 310 | # define RET		mov		pc, lr |
| 311 | # define RETr(r)	mov		pc, r |
| 312 | # define RETc(c)	__CONCAT(mov,c)	pc, lr |
| 313 | #endif |
| 314 | |
| 315 | #ifdef _ARM_ARCH_7 |
| 316 | #define KMODTRAMPOLINE(n)			\ |
| 317 | _ENTRY(__wrap_ ## n)				\ |
| 318 | 	movw	ip, #:lower16:n;	\ |
| 319 | 	movt	ip, #:upper16:n;	\ |
| 320 | 	bx	ip |
| 321 | #elif defined(_ARM_ARCH_4T) |
| 322 | #define KMODTRAMPOLINE(n)	\ |
| 323 | _ENTRY(__wrap_ ## n)		\ |
| 324 | 	ldr	ip, [pc];	\ |
| 325 | 	bx	ip;		\ |
| 326 | 	.word	n |
| 327 | #else |
| 328 | #define KMODTRAMPOLINE(n)	\ |
| 329 | _ENTRY(__wrap_ ## n)		\ |
| 330 | 	ldr	pc, [pc, #-4];	\ |
| 331 | 	.word	n |
| 332 | #endif |
| 333 | |
| 334 | #endif /* !_ARM_ASM_H_ */ |