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