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