summaryrefslogtreecommitdiff
path: root/sys/arch/riscv/include/asm.h
blob: c88d724e66633a4cc55e8ddc50f629d5940228fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*	$NetBSD: asm.h,v 1.7 2023/05/07 12:41:48 skrll Exp $	*/

/*-
 * Copyright (c) 2014 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Matt Thomas of 3am Software Foundry.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _RISCV_ASM_H
#define	_RISCV_ASM_H

#define	_C_LABEL(x)	x

#define	__CONCAT(x,y)	x ## y
#define	__STRING(x)	#x

#define	___CONCAT(x,y)	__CONCAT(x,y)

/*
 * Define -pg profile entry code.
 * Must always be noreorder, must never use a macro instruction
 * Final addiu to t9 must always equal the size of this _KERN_MCOUNT
 */
#define	_KERN_MCOUNT						\
	.set	push;						\
	subi	sp, sp, CALLFRAME_SIZE;				\
	REG_S	a0, CALLFRAME_S0(sp);				\
	REG_S	ra, CALLFRAME_RA(sp);				\
	move	a0, ra;						\
	call	_mcount 					\
	REG_L	ra, CALLFRAME_RA(sp);				\
	REG_L	a0, CALLFRAME_S0(sp);				\
	addi	sp, sp, CALLFRAME_SIZ;				\
	.set	pop;

#ifdef GPROF
#define	_PROF_PROLOGUE _KERN_MCOUNT
#else
#define	_PROF_PROLOGUE
#endif

#ifdef __PIC__
#define	PLT(x)	x##@plt
#else
#define PLT(x)	x
#endif

/*
 * WEAK_ALIAS: create a weak alias.
 */
#define	WEAK_ALIAS(alias,sym)						\
	.weak alias;							\
	alias = sym
/*
 * STRONG_ALIAS: create a strong alias.
 */
#define	STRONG_ALIAS(alias,sym)						\
	.globl alias;							\
	alias = sym

/*
 * WARN_REFERENCES: create a warning if the specified symbol is referenced.
 */
#define	WARN_REFERENCES(sym,msg)					\
	.pushsection __CONCAT(.gnu.warning.,sym);			\
	.ascii msg;							\
	.popsection

#define	_ENTRY(x)			\
	.globl	_C_LABEL(x);		\
	.type	_C_LABEL(x), @function;	\
	_C_LABEL(x):

#define	ENTRY_NP(x)	.text; .align 2; _ENTRY(x)
#define	ENTRY(x)	ENTRY_NP(x); _PROF_PROLOGUE
#define	ALTENTRY(x)	_ENTRY(x)
#define	END(x)		.size _C_LABEL(x), . - _C_LABEL(x)

/*
 * Macros to panic and printf from assembly language.
 */
#define	PANIC(msg)			\
	la	a0, 9f;			\
	call	_C_LABEL(panic);	\
	MSG(msg)

#define	PRINTF(msg)			\
	la	a0, 9f;			\
	call	_C_LABEL(printf);	\
	MSG(msg)

#define	MSG(msg)			\
        .pushsection .rodata.str1.8,"aMS",@progbits,1; \
9:	.asciiz	msg;			\
	.popsection

#define	ASMSTR(str)			\
	.asciiz str;			\
	.align	3

#define __RCSID(x)	.pushsection ".ident","MS",@progbits,1;		\
			.asciz x;					\
			.popsection
#define RCSID(name)	__RCSID(name)

#if defined(_LP64)
#define	SZREG	8
#else
#define	SZREG	4
#endif

#define	ALSK	15		/* stack alignment */
#define	ALMASK	-15		/* stack alignment */
#define	SZFPREG	8
#define	FP_L	fld
#define	FP_S	fsd

/*
 *  standard callframe {
 *  	register_t cf_sp;		frame pointer
 *  	register_t cf_ra;		return address
 *  };
 */
#define	CALLFRAME_SIZ	(SZREG * 4)
#define	CALLFRAME_S1	(CALLFRAME_SIZ - 4 * SZREG)
#define	CALLFRAME_S0	(CALLFRAME_SIZ - 3 * SZREG)
#define	CALLFRAME_SP	(CALLFRAME_SIZ - 2 * SZREG)
#define	CALLFRAME_RA	(CALLFRAME_SIZ - 1 * SZREG)

/*
 * These macros hide the use of rv32 and rv64 instructions from the
 * assembler to prevent the assembler from generating 64-bit style
 * ABI calls.
 */
#define	PTR_ADD		add
#define	PTR_ADDI	addi
#define	PTR_SUB		sub
#define	PTR_SUBI	subi
#define	PTR_LA		la
#define	PTR_SLLI	slli
#define	PTR_SLL		sll
#define	PTR_SRLI	srli
#define	PTR_SRL		srl
#define	PTR_SRAI	srai
#define	PTR_SRA		sra
#if _LP64
#define	PTR_L		ld
#define	PTR_S		sd
#define	PTR_LR		lr.d
#define	PTR_SC		sc.d
#define	PTR_WORD	.dword
#define	PTR_SCALESHIFT	3
#else
#define	PTR_L		lw
#define	PTR_S		sw
#define	PTR_LR		lr.w
#define	PTR_SC		sc.w
#define	PTR_WORD	.word
#define	PTR_SCALESHIFT	2
#endif

#define	INT_L		lw
#define	INT_LA		la
#define	INT_S		sw
#define	INT_LR		lr.w
#define	INT_SC		sc.w
#define	INT_WORD	.word
#define	INT_SCALESHIFT	2
#ifdef _LP64
#define	INT_ADD		addw
#define	INT_ADDI	addwi
#define	INT_SUB		subw
#define	INT_SUBI	subwi
#define	INT_SLL		sllwi
#define	INT_SLLV	sllw
#define	INT_SRL		srlwi
#define	INT_SRLV	srlw
#define	INT_SRA		srawi
#define	INT_SRAV	sraw
#else
#define	INT_ADD		add
#define	INT_ADDI	addi
#define	INT_SUB		sub
#define	INT_SUBI	subi
#define	INT_SLLI	slli
#define	INT_SLL		sll
#define	INT_SRLI	srli
#define	INT_SRL		srl
#define	INT_SRAI	srai
#define	INT_SRA		sra
#endif

#define	LONG_LA		la
#define	LONG_ADD	add
#define	LONG_ADDI	addi
#define	LONG_SUB	sub
#define	LONG_SUBI	subi
#define	LONG_SLLI	slli
#define	LONG_SLL	sll
#define	LONG_SRLI	srli
#define	LONG_SRL	srl
#define	LONG_SRAI	srai
#define	LONG_SRA	sra
#ifdef _LP64
#define	LONG_L		ld
#define	LONG_S		sd
#define	LONG_LR		lr.d
#define	LONG_SC		sc.d
#define	LONG_WORD	.quad
#define	LONG_SCALESHIFT	3
#else
#define	LONG_L		lw
#define	LONG_S		sw
#define	LONG_LR		lr.w
#define	LONG_SC		sc.w
#define	LONG_WORD	.word
#define	LONG_SCALESHIFT	2
#endif

#define	REG_LI		li
#define	REG_ADD		add
#define	REG_SLLI	slli
#define	REG_SLL		sll
#define	REG_SRLI	srli
#define	REG_SRL		srl
#define	REG_SRAI	srai
#define	REG_SRA		sra
#if _LP64
#define	REG_L		ld
#define	REG_S		sd
#define	REG_LR		lr.d
#define	REG_SC		sc.d
#define	REG_SCALESHIFT	3
#else
#define	REG_L		lw
#define	REG_S		sw
#define	REG_LR		lr.w
#define	REG_SC		sc.w
#define	REG_SCALESHIFT	2
#endif

#define	CPUVAR(off) _C_LABEL(cpu_info_store)+__CONCAT(CPU_INFO_,off)

#endif /* _RISCV_ASM_H */