Home | History | Annotate | Line # | Download | only in include
asm.h revision 1.2.20.3
      1  1.2.20.2    martin /*	$NetBSD: asm.h,v 1.2.20.3 2020/04/21 18:42:11 martin Exp $	*/
      2       1.1      matt 
      3       1.1      matt /*-
      4       1.1      matt  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5       1.1      matt  * All rights reserved.
      6       1.1      matt  *
      7       1.1      matt  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1      matt  * by Matt Thomas of 3am Software Foundry.
      9       1.1      matt  *
     10       1.1      matt  * Redistribution and use in source and binary forms, with or without
     11       1.1      matt  * modification, are permitted provided that the following conditions
     12       1.1      matt  * are met:
     13       1.1      matt  * 1. Redistributions of source code must retain the above copyright
     14       1.1      matt  *    notice, this list of conditions and the following disclaimer.
     15       1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     17       1.1      matt  *    documentation and/or other materials provided with the distribution.
     18       1.1      matt  *
     19       1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1      matt  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1      matt  */
     31       1.1      matt 
     32       1.1      matt #ifndef _RISCV_ASM_H
     33       1.1      matt #define	_RISCV_ASM_H
     34       1.1      matt 
     35       1.1      matt #define	_C_LABEL(x)	x
     36       1.1      matt 
     37       1.1      matt /*
     38       1.1      matt  * Define -pg profile entry code.
     39       1.1      matt  * Must always be noreorder, must never use a macro instruction
     40       1.1      matt  * Final addiu to t9 must always equal the size of this _KERN_MCOUNT
     41       1.1      matt  */
     42       1.1      matt #define	_KERN_MCOUNT						\
     43       1.1      matt 	.set	push;						\
     44       1.1      matt 	subi	sp, sp, CALLFRAME_SIZE;				\
     45       1.1      matt 	REG_S	a0, CALLFRAME_S0(sp);				\
     46       1.1      matt 	REG_S	ra, CALLFRAME_RA(sp);				\
     47       1.1      matt 	move	a0, ra;						\
     48       1.1      matt 	call	_mcount 					\
     49       1.1      matt 	REG_L	ra, CALLFRAME_RA(sp);				\
     50       1.1      matt 	REG_L	a0, CALLFRAME_S0(sp);				\
     51       1.1      matt 	addi	sp, sp, CALLFRAME_SIZ;				\
     52  1.2.20.2    martin 	.set	pop;
     53       1.1      matt 
     54       1.1      matt #ifdef GPROF
     55       1.1      matt #define	_PROF_PROLOGUE _KERN_MCOUNT
     56       1.1      matt #else
     57       1.1      matt #define	_PROF_PROLOGUE
     58       1.1      matt #endif
     59       1.1      matt 
     60       1.1      matt #ifdef __PIC__
     61  1.2.20.1  christos #define	PLT(x)	x##@plt
     62       1.1      matt #else
     63       1.1      matt #define PLT(x)	x
     64       1.1      matt #endif
     65       1.1      matt 
     66       1.1      matt /*
     67       1.1      matt  * WEAK_ALIAS: create a weak alias.
     68       1.1      matt  */
     69       1.1      matt #define	WEAK_ALIAS(alias,sym)						\
     70       1.1      matt 	.weak alias;							\
     71       1.1      matt 	alias = sym
     72       1.1      matt /*
     73       1.1      matt  * STRONG_ALIAS: create a strong alias.
     74       1.1      matt  */
     75       1.1      matt #define	STRONG_ALIAS(alias,sym)						\
     76       1.1      matt 	.globl alias;							\
     77       1.1      matt 	alias = sym
     78       1.1      matt 
     79       1.1      matt /*
     80       1.1      matt  * WARN_REFERENCES: create a warning if the specified symbol is referenced.
     81       1.1      matt  */
     82       1.1      matt #define	WARN_REFERENCES(sym,msg)					\
     83       1.1      matt 	.pushsection __CONCAT(.gnu.warning.,sym);			\
     84       1.1      matt 	.ascii msg;							\
     85       1.1      matt 	.popsection
     86       1.1      matt 
     87       1.1      matt #define	_ENTRY(x)			\
     88       1.1      matt 	.globl	_C_LABEL(x);		\
     89       1.1      matt 	.type	_C_LABEL(x), @function;	\
     90       1.1      matt 	_C_LABEL(x):
     91       1.1      matt 
     92       1.1      matt #define	ENTRY_NP(x)	.text; .align 2; _ENTRY(x)
     93       1.1      matt #define	ENTRY(x)	ENTRY_NP(x); _PROF_PROLOGUE
     94       1.1      matt #define	END(x)		.size _C_LABEL(x), . - _C_LABEL(x)
     95       1.1      matt 
     96       1.1      matt /*
     97       1.1      matt  * Macros to panic and printf from assembly language.
     98       1.1      matt  */
     99       1.1      matt #define	PANIC(msg)			\
    100       1.1      matt 	la	a0, 9f;			\
    101       1.1      matt 	call	_C_LABEL(panic);	\
    102       1.1      matt 	MSG(msg)
    103       1.1      matt 
    104       1.1      matt #define	PRINTF(msg)			\
    105       1.1      matt 	la	a0, 9f;			\
    106       1.1      matt 	call	_C_LABEL(printf);	\
    107       1.1      matt 	MSG(msg)
    108       1.1      matt 
    109       1.1      matt #define	MSG(msg)			\
    110       1.1      matt         .pushsection .rodata.str1.8,"aMS",@progbits,1; \
    111       1.1      matt 9:	.asciiz	msg;			\
    112       1.1      matt 	.popsection
    113       1.1      matt 
    114       1.1      matt #define	ASMSTR(str)			\
    115       1.1      matt 	.asciiz str;			\
    116       1.1      matt 	.align	3
    117       1.1      matt 
    118  1.2.20.3    martin #define __RCSID(x)	.pushsection ".ident","MS",@progbits,1;		\
    119  1.2.20.3    martin 			.asciz x;					\
    120  1.2.20.3    martin 			.popsection
    121       1.1      matt #define RCSID(name)	__RCSID(name)
    122       1.1      matt 
    123       1.1      matt #if defined(_LP64)
    124       1.2      matt #define	SZREG	8
    125       1.2      matt #else
    126       1.1      matt #define	SZREG	4
    127       1.1      matt #endif
    128       1.1      matt 
    129       1.1      matt #define	ALSK	15		/* stack alignment */
    130       1.1      matt #define	ALMASK	-15		/* stack alignment */
    131       1.1      matt #define	SZFPREG	8
    132       1.1      matt #define	FP_L	fld
    133       1.1      matt #define	FP_S	fsd
    134       1.1      matt 
    135       1.1      matt /*
    136       1.1      matt  *  standard callframe {
    137       1.1      matt  *  	register_t cf_sp;		frame pointer
    138       1.1      matt  *  	register_t cf_ra;		return address
    139       1.1      matt  *  };
    140       1.1      matt  */
    141       1.1      matt #define	CALLFRAME_SIZ	(SZREG * 4)
    142       1.1      matt #define	CALLFRAME_S1	(CALLFRAME_SIZ - 4 * SZREG)
    143       1.1      matt #define	CALLFRAME_S0	(CALLFRAME_SIZ - 3 * SZREG)
    144       1.1      matt #define	CALLFRAME_SP	(CALLFRAME_SIZ - 2 * SZREG)
    145       1.1      matt #define	CALLFRAME_RA	(CALLFRAME_SIZ - 1 * SZREG)
    146       1.1      matt 
    147       1.1      matt /*
    148       1.1      matt  * These macros hide the use of rv32 and rv64 instructions from the
    149       1.1      matt  * assembler to prevent the assembler from generating 64-bit style
    150       1.1      matt  * ABI calls.
    151       1.1      matt  */
    152       1.1      matt #define	PTR_ADD		add
    153       1.1      matt #define	PTR_ADDI	addi
    154       1.1      matt #define	PTR_SUB		sub
    155       1.1      matt #define	PTR_SUBI	subi
    156       1.1      matt #define	PTR_LA		la
    157       1.1      matt #define	PTR_SLLI	slli
    158       1.1      matt #define	PTR_SLL		sll
    159       1.1      matt #define	PTR_SRLI	srli
    160       1.1      matt #define	PTR_SRL		srl
    161       1.1      matt #define	PTR_SRAI	srai
    162       1.1      matt #define	PTR_SRA		sra
    163       1.1      matt #if _LP64
    164       1.1      matt #define	PTR_L		ld
    165       1.1      matt #define	PTR_S		sd
    166       1.1      matt #define	PTR_LR		lr.d
    167       1.1      matt #define	PTR_SC		sc.d
    168       1.1      matt #define	PTR_WORD	.dword
    169       1.1      matt #define	PTR_SCALESHIFT	3
    170       1.1      matt #else
    171       1.1      matt #define	PTR_L		lw
    172       1.1      matt #define	PTR_S		sw
    173       1.1      matt #define	PTR_LR		lr.w
    174       1.1      matt #define	PTR_SC		sc.w
    175       1.1      matt #define	PTR_WORD	.word
    176       1.1      matt #define	PTR_SCALESHIFT	2
    177       1.1      matt #endif
    178       1.1      matt 
    179       1.1      matt #define	INT_L		lw
    180       1.1      matt #define	INT_LA		la
    181       1.1      matt #define	INT_S		sw
    182       1.1      matt #define	INT_LR		lr.w
    183       1.1      matt #define	INT_SC		sc.w
    184       1.1      matt #define	INT_WORD	.word
    185       1.1      matt #define	INT_SCALESHIFT	2
    186       1.1      matt #ifdef _LP64
    187       1.1      matt #define	INT_ADD		addw
    188       1.1      matt #define	INT_ADDI	addwi
    189       1.1      matt #define	INT_SUB		subw
    190       1.1      matt #define	INT_SUBI	subwi
    191       1.1      matt #define	INT_SLL		sllwi
    192       1.1      matt #define	INT_SLLV	sllw
    193       1.1      matt #define	INT_SRL		srlwi
    194       1.1      matt #define	INT_SRLV	srlw
    195       1.1      matt #define	INT_SRA		srawi
    196       1.1      matt #define	INT_SRAV	sraw
    197       1.1      matt #else
    198       1.1      matt #define	INT_ADD		add
    199       1.1      matt #define	INT_ADDI	addi
    200       1.1      matt #define	INT_SUB		sub
    201       1.1      matt #define	INT_SUBI	subi
    202       1.1      matt #define	INT_SLLI	slli
    203       1.1      matt #define	INT_SLL		sll
    204       1.1      matt #define	INT_SRLI	srli
    205       1.1      matt #define	INT_SRL		srl
    206       1.1      matt #define	INT_SRAI	srai
    207       1.1      matt #define	INT_SRA		sra
    208       1.1      matt #endif
    209       1.1      matt 
    210       1.1      matt #define	LONG_LA		la
    211       1.1      matt #define	LONG_ADD	add
    212       1.1      matt #define	LONG_ADDI	addi
    213       1.1      matt #define	LONG_SUB	sub
    214       1.1      matt #define	LONG_SUBI	subi
    215       1.1      matt #define	LONG_SLLI	slli
    216       1.1      matt #define	LONG_SLL	sll
    217       1.1      matt #define	LONG_SRLI	srli
    218       1.1      matt #define	LONG_SRL	srl
    219       1.1      matt #define	LONG_SRAI	srai
    220       1.1      matt #define	LONG_SRA	sra
    221       1.1      matt #ifdef _LP64
    222       1.1      matt #define	LONG_L		ld
    223       1.1      matt #define	LONG_S		sd
    224       1.1      matt #define	LONG_LR		lr.d
    225       1.1      matt #define	LONG_SC		sc.d
    226       1.1      matt #define	LONG_WORD	.quad
    227       1.1      matt #define	LONG_SCALESHIFT	3
    228       1.1      matt #else
    229       1.1      matt #define	LONG_L		lw
    230       1.1      matt #define	LONG_S		sw
    231       1.1      matt #define	LONG_LR		lr.w
    232       1.1      matt #define	LONG_SC		sc.w
    233       1.1      matt #define	LONG_WORD	.word
    234       1.1      matt #define	LONG_SCALESHIFT	2
    235       1.1      matt #endif
    236       1.1      matt 
    237       1.1      matt #define	REG_LI		li
    238       1.1      matt #define	REG_ADD		add
    239       1.1      matt #define	REG_SLLI	slli
    240       1.1      matt #define	REG_SLL		sll
    241       1.1      matt #define	REG_SRLI	srli
    242       1.1      matt #define	REG_SRL		srl
    243       1.1      matt #define	REG_SRAI	srai
    244       1.1      matt #define	REG_SRA		sra
    245       1.1      matt #if _LP64
    246       1.1      matt #define	REG_L		ld
    247       1.1      matt #define	REG_S		sd
    248       1.1      matt #define	REG_LR		lr.d
    249       1.1      matt #define	REG_SC		sc.d
    250       1.1      matt #define	REG_SCALESHIFT	3
    251       1.1      matt #else
    252       1.1      matt #define	REG_L		lw
    253       1.1      matt #define	REG_S		sw
    254       1.1      matt #define	REG_LR		lr.w
    255       1.1      matt #define	REG_SC		sc.w
    256       1.1      matt #define	REG_SCALESHIFT	2
    257       1.1      matt #endif
    258       1.1      matt 
    259       1.1      matt #define	CPUVAR(off) _C_LABEL(cpu_info_store)+__CONCAT(CPU_INFO_,off)
    260       1.1      matt 
    261       1.1      matt #endif /* _RISCV_ASM_H */
    262