Home | History | Annotate | Line # | Download | only in include
asm.h revision 1.11
      1  1.11  martin /*	$NetBSD: asm.h,v 1.11 2025/01/06 10:46:43 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.6   skrll #define	__CONCAT(x,y)	x ## y
     38   1.6   skrll #define	__STRING(x)	#x
     39   1.6   skrll 
     40   1.6   skrll #define	___CONCAT(x,y)	__CONCAT(x,y)
     41   1.6   skrll 
     42   1.1    matt /*
     43   1.1    matt  * Define -pg profile entry code.
     44   1.1    matt  * Must always be noreorder, must never use a macro instruction
     45   1.1    matt  * Final addiu to t9 must always equal the size of this _KERN_MCOUNT
     46   1.1    matt  */
     47   1.1    matt #define	_KERN_MCOUNT						\
     48   1.1    matt 	.set	push;						\
     49   1.1    matt 	subi	sp, sp, CALLFRAME_SIZE;				\
     50   1.1    matt 	REG_S	a0, CALLFRAME_S0(sp);				\
     51   1.1    matt 	REG_S	ra, CALLFRAME_RA(sp);				\
     52   1.1    matt 	move	a0, ra;						\
     53   1.1    matt 	call	_mcount 					\
     54   1.1    matt 	REG_L	ra, CALLFRAME_RA(sp);				\
     55   1.1    matt 	REG_L	a0, CALLFRAME_S0(sp);				\
     56   1.1    matt 	addi	sp, sp, CALLFRAME_SIZ;				\
     57   1.4   skrll 	.set	pop;
     58   1.1    matt 
     59   1.1    matt #ifdef GPROF
     60   1.1    matt #define	_PROF_PROLOGUE _KERN_MCOUNT
     61   1.1    matt #else
     62   1.1    matt #define	_PROF_PROLOGUE
     63   1.1    matt #endif
     64   1.1    matt 
     65   1.1    matt #ifdef __PIC__
     66   1.3    maya #define	PLT(x)	x##@plt
     67   1.1    matt #else
     68   1.1    matt #define PLT(x)	x
     69   1.1    matt #endif
     70   1.1    matt 
     71   1.1    matt /*
     72   1.1    matt  * WEAK_ALIAS: create a weak alias.
     73   1.1    matt  */
     74   1.1    matt #define	WEAK_ALIAS(alias,sym)						\
     75   1.1    matt 	.weak alias;							\
     76   1.1    matt 	alias = sym
     77   1.1    matt /*
     78   1.1    matt  * STRONG_ALIAS: create a strong alias.
     79   1.1    matt  */
     80   1.1    matt #define	STRONG_ALIAS(alias,sym)						\
     81   1.1    matt 	.globl alias;							\
     82   1.1    matt 	alias = sym
     83   1.1    matt 
     84   1.1    matt /*
     85   1.1    matt  * WARN_REFERENCES: create a warning if the specified symbol is referenced.
     86   1.1    matt  */
     87   1.1    matt #define	WARN_REFERENCES(sym,msg)					\
     88   1.1    matt 	.pushsection __CONCAT(.gnu.warning.,sym);			\
     89   1.1    matt 	.ascii msg;							\
     90   1.1    matt 	.popsection
     91   1.1    matt 
     92   1.1    matt #define	_ENTRY(x)			\
     93   1.1    matt 	.globl	_C_LABEL(x);		\
     94   1.1    matt 	.type	_C_LABEL(x), @function;	\
     95   1.1    matt 	_C_LABEL(x):
     96   1.1    matt 
     97   1.1    matt #define	ENTRY_NP(x)	.text; .align 2; _ENTRY(x)
     98   1.1    matt #define	ENTRY(x)	ENTRY_NP(x); _PROF_PROLOGUE
     99   1.7   skrll #define	ALTENTRY(x)	_ENTRY(x)
    100   1.1    matt #define	END(x)		.size _C_LABEL(x), . - _C_LABEL(x)
    101   1.1    matt 
    102   1.1    matt /*
    103   1.1    matt  * Macros to panic and printf from assembly language.
    104   1.1    matt  */
    105   1.1    matt #define	PANIC(msg)			\
    106   1.1    matt 	la	a0, 9f;			\
    107   1.1    matt 	call	_C_LABEL(panic);	\
    108   1.1    matt 	MSG(msg)
    109   1.1    matt 
    110   1.1    matt #define	PRINTF(msg)			\
    111   1.1    matt 	la	a0, 9f;			\
    112   1.1    matt 	call	_C_LABEL(printf);	\
    113   1.1    matt 	MSG(msg)
    114   1.1    matt 
    115   1.1    matt #define	MSG(msg)			\
    116   1.8   skrll 	.pushsection .rodata.str1.8,"aMS",@progbits,1; \
    117   1.1    matt 9:	.asciiz	msg;			\
    118   1.1    matt 	.popsection
    119   1.1    matt 
    120   1.1    matt #define	ASMSTR(str)			\
    121   1.1    matt 	.asciiz str;			\
    122   1.1    matt 	.align	3
    123   1.1    matt 
    124   1.9   skrll #ifdef _NETBSD_REVISIONID
    125   1.5   joerg #define __RCSID(x)	.pushsection ".ident","MS",@progbits,1;		\
    126  1.11  martin 			.asciz x;					\
    127  1.11  martin 			.ascii "$"; .ascii "NetBSD: "; .ascii __FILE__;	\
    128  1.11  martin 			.ascii " "; .ascii _NETBSD_REVISIONID;		\
    129  1.11  martin 			.asciz " $";					\
    130   1.5   joerg 			.popsection
    131   1.9   skrll #else
    132   1.9   skrll #define __RCSID(x)	.pushsection ".ident","MS",@progbits,1;		\
    133   1.9   skrll 			.asciz x;					\
    134   1.9   skrll 			.popsection
    135   1.9   skrll #endif
    136   1.1    matt #define RCSID(name)	__RCSID(name)
    137   1.1    matt 
    138   1.1    matt #if defined(_LP64)
    139   1.2    matt #define	SZREG	8
    140   1.2    matt #else
    141   1.1    matt #define	SZREG	4
    142   1.1    matt #endif
    143   1.1    matt 
    144   1.1    matt #define	ALSK	15		/* stack alignment */
    145   1.1    matt #define	ALMASK	-15		/* stack alignment */
    146   1.1    matt #define	SZFPREG	8
    147   1.1    matt #define	FP_L	fld
    148   1.1    matt #define	FP_S	fsd
    149   1.1    matt 
    150   1.1    matt /*
    151   1.1    matt  *  standard callframe {
    152   1.1    matt  *  	register_t cf_sp;		frame pointer
    153   1.1    matt  *  	register_t cf_ra;		return address
    154   1.1    matt  *  };
    155   1.1    matt  */
    156   1.1    matt #define	CALLFRAME_SIZ	(SZREG * 4)
    157   1.1    matt #define	CALLFRAME_S1	(CALLFRAME_SIZ - 4 * SZREG)
    158   1.1    matt #define	CALLFRAME_S0	(CALLFRAME_SIZ - 3 * SZREG)
    159   1.1    matt #define	CALLFRAME_SP	(CALLFRAME_SIZ - 2 * SZREG)
    160   1.1    matt #define	CALLFRAME_RA	(CALLFRAME_SIZ - 1 * SZREG)
    161   1.1    matt 
    162   1.1    matt /*
    163   1.1    matt  * These macros hide the use of rv32 and rv64 instructions from the
    164   1.1    matt  * assembler to prevent the assembler from generating 64-bit style
    165   1.1    matt  * ABI calls.
    166   1.1    matt  */
    167   1.1    matt #define	PTR_ADD		add
    168   1.1    matt #define	PTR_ADDI	addi
    169   1.1    matt #define	PTR_SUB		sub
    170   1.1    matt #define	PTR_SUBI	subi
    171   1.1    matt #define	PTR_LA		la
    172   1.1    matt #define	PTR_SLLI	slli
    173   1.1    matt #define	PTR_SLL		sll
    174   1.1    matt #define	PTR_SRLI	srli
    175   1.1    matt #define	PTR_SRL		srl
    176   1.1    matt #define	PTR_SRAI	srai
    177   1.1    matt #define	PTR_SRA		sra
    178   1.1    matt #if _LP64
    179   1.1    matt #define	PTR_L		ld
    180   1.1    matt #define	PTR_S		sd
    181   1.1    matt #define	PTR_LR		lr.d
    182   1.1    matt #define	PTR_SC		sc.d
    183   1.1    matt #define	PTR_WORD	.dword
    184   1.1    matt #define	PTR_SCALESHIFT	3
    185   1.1    matt #else
    186   1.1    matt #define	PTR_L		lw
    187   1.1    matt #define	PTR_S		sw
    188   1.1    matt #define	PTR_LR		lr.w
    189   1.1    matt #define	PTR_SC		sc.w
    190   1.1    matt #define	PTR_WORD	.word
    191   1.1    matt #define	PTR_SCALESHIFT	2
    192   1.1    matt #endif
    193   1.1    matt 
    194   1.1    matt #define	INT_L		lw
    195   1.1    matt #define	INT_LA		la
    196   1.1    matt #define	INT_S		sw
    197   1.1    matt #define	INT_LR		lr.w
    198   1.1    matt #define	INT_SC		sc.w
    199   1.1    matt #define	INT_WORD	.word
    200   1.1    matt #define	INT_SCALESHIFT	2
    201   1.1    matt #ifdef _LP64
    202   1.1    matt #define	INT_ADD		addw
    203   1.1    matt #define	INT_ADDI	addwi
    204   1.1    matt #define	INT_SUB		subw
    205   1.1    matt #define	INT_SUBI	subwi
    206   1.1    matt #define	INT_SLL		sllwi
    207   1.1    matt #define	INT_SLLV	sllw
    208   1.1    matt #define	INT_SRL		srlwi
    209   1.1    matt #define	INT_SRLV	srlw
    210   1.1    matt #define	INT_SRA		srawi
    211   1.1    matt #define	INT_SRAV	sraw
    212   1.1    matt #else
    213   1.1    matt #define	INT_ADD		add
    214   1.1    matt #define	INT_ADDI	addi
    215   1.1    matt #define	INT_SUB		sub
    216   1.1    matt #define	INT_SUBI	subi
    217   1.1    matt #define	INT_SLLI	slli
    218   1.1    matt #define	INT_SLL		sll
    219   1.1    matt #define	INT_SRLI	srli
    220   1.1    matt #define	INT_SRL		srl
    221   1.1    matt #define	INT_SRAI	srai
    222   1.1    matt #define	INT_SRA		sra
    223   1.1    matt #endif
    224   1.1    matt 
    225   1.1    matt #define	LONG_LA		la
    226   1.1    matt #define	LONG_ADD	add
    227   1.1    matt #define	LONG_ADDI	addi
    228   1.1    matt #define	LONG_SUB	sub
    229   1.1    matt #define	LONG_SUBI	subi
    230   1.1    matt #define	LONG_SLLI	slli
    231   1.1    matt #define	LONG_SLL	sll
    232   1.1    matt #define	LONG_SRLI	srli
    233   1.1    matt #define	LONG_SRL	srl
    234   1.1    matt #define	LONG_SRAI	srai
    235   1.1    matt #define	LONG_SRA	sra
    236   1.1    matt #ifdef _LP64
    237   1.1    matt #define	LONG_L		ld
    238   1.1    matt #define	LONG_S		sd
    239   1.1    matt #define	LONG_LR		lr.d
    240   1.1    matt #define	LONG_SC		sc.d
    241   1.1    matt #define	LONG_WORD	.quad
    242   1.1    matt #define	LONG_SCALESHIFT	3
    243   1.1    matt #else
    244   1.1    matt #define	LONG_L		lw
    245   1.1    matt #define	LONG_S		sw
    246   1.1    matt #define	LONG_LR		lr.w
    247   1.1    matt #define	LONG_SC		sc.w
    248   1.1    matt #define	LONG_WORD	.word
    249   1.1    matt #define	LONG_SCALESHIFT	2
    250   1.1    matt #endif
    251   1.1    matt 
    252   1.1    matt #define	REG_LI		li
    253   1.1    matt #define	REG_ADD		add
    254   1.1    matt #define	REG_SLLI	slli
    255   1.1    matt #define	REG_SLL		sll
    256   1.1    matt #define	REG_SRLI	srli
    257   1.1    matt #define	REG_SRL		srl
    258   1.1    matt #define	REG_SRAI	srai
    259   1.1    matt #define	REG_SRA		sra
    260   1.1    matt #if _LP64
    261   1.1    matt #define	REG_L		ld
    262   1.1    matt #define	REG_S		sd
    263   1.1    matt #define	REG_LR		lr.d
    264   1.1    matt #define	REG_SC		sc.d
    265   1.1    matt #define	REG_SCALESHIFT	3
    266   1.1    matt #else
    267   1.1    matt #define	REG_L		lw
    268   1.1    matt #define	REG_S		sw
    269   1.1    matt #define	REG_LR		lr.w
    270   1.1    matt #define	REG_SC		sc.w
    271   1.1    matt #define	REG_SCALESHIFT	2
    272   1.1    matt #endif
    273   1.1    matt 
    274   1.1    matt #define	CPUVAR(off) _C_LABEL(cpu_info_store)+__CONCAT(CPU_INFO_,off)
    275   1.1    matt 
    276   1.1    matt #endif /* _RISCV_ASM_H */
    277