asm.h revision 1.9 1 1.9 skrll /* $NetBSD: asm.h,v 1.9 2025/01/04 21:02:01 skrll 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.9 skrll .asciz "$" "NetBSD: " __FILE__ \
127 1.9 skrll " " _NETBSD_REVISIONID " $"; \
128 1.5 joerg .asciz x; \
129 1.5 joerg .popsection
130 1.9 skrll #else
131 1.9 skrll #define __RCSID(x) .pushsection ".ident","MS",@progbits,1; \
132 1.9 skrll .asciz x; \
133 1.9 skrll .popsection
134 1.9 skrll #endif
135 1.1 matt #define RCSID(name) __RCSID(name)
136 1.1 matt
137 1.1 matt #if defined(_LP64)
138 1.2 matt #define SZREG 8
139 1.2 matt #else
140 1.1 matt #define SZREG 4
141 1.1 matt #endif
142 1.1 matt
143 1.1 matt #define ALSK 15 /* stack alignment */
144 1.1 matt #define ALMASK -15 /* stack alignment */
145 1.1 matt #define SZFPREG 8
146 1.1 matt #define FP_L fld
147 1.1 matt #define FP_S fsd
148 1.1 matt
149 1.1 matt /*
150 1.1 matt * standard callframe {
151 1.1 matt * register_t cf_sp; frame pointer
152 1.1 matt * register_t cf_ra; return address
153 1.1 matt * };
154 1.1 matt */
155 1.1 matt #define CALLFRAME_SIZ (SZREG * 4)
156 1.1 matt #define CALLFRAME_S1 (CALLFRAME_SIZ - 4 * SZREG)
157 1.1 matt #define CALLFRAME_S0 (CALLFRAME_SIZ - 3 * SZREG)
158 1.1 matt #define CALLFRAME_SP (CALLFRAME_SIZ - 2 * SZREG)
159 1.1 matt #define CALLFRAME_RA (CALLFRAME_SIZ - 1 * SZREG)
160 1.1 matt
161 1.1 matt /*
162 1.1 matt * These macros hide the use of rv32 and rv64 instructions from the
163 1.1 matt * assembler to prevent the assembler from generating 64-bit style
164 1.1 matt * ABI calls.
165 1.1 matt */
166 1.1 matt #define PTR_ADD add
167 1.1 matt #define PTR_ADDI addi
168 1.1 matt #define PTR_SUB sub
169 1.1 matt #define PTR_SUBI subi
170 1.1 matt #define PTR_LA la
171 1.1 matt #define PTR_SLLI slli
172 1.1 matt #define PTR_SLL sll
173 1.1 matt #define PTR_SRLI srli
174 1.1 matt #define PTR_SRL srl
175 1.1 matt #define PTR_SRAI srai
176 1.1 matt #define PTR_SRA sra
177 1.1 matt #if _LP64
178 1.1 matt #define PTR_L ld
179 1.1 matt #define PTR_S sd
180 1.1 matt #define PTR_LR lr.d
181 1.1 matt #define PTR_SC sc.d
182 1.1 matt #define PTR_WORD .dword
183 1.1 matt #define PTR_SCALESHIFT 3
184 1.1 matt #else
185 1.1 matt #define PTR_L lw
186 1.1 matt #define PTR_S sw
187 1.1 matt #define PTR_LR lr.w
188 1.1 matt #define PTR_SC sc.w
189 1.1 matt #define PTR_WORD .word
190 1.1 matt #define PTR_SCALESHIFT 2
191 1.1 matt #endif
192 1.1 matt
193 1.1 matt #define INT_L lw
194 1.1 matt #define INT_LA la
195 1.1 matt #define INT_S sw
196 1.1 matt #define INT_LR lr.w
197 1.1 matt #define INT_SC sc.w
198 1.1 matt #define INT_WORD .word
199 1.1 matt #define INT_SCALESHIFT 2
200 1.1 matt #ifdef _LP64
201 1.1 matt #define INT_ADD addw
202 1.1 matt #define INT_ADDI addwi
203 1.1 matt #define INT_SUB subw
204 1.1 matt #define INT_SUBI subwi
205 1.1 matt #define INT_SLL sllwi
206 1.1 matt #define INT_SLLV sllw
207 1.1 matt #define INT_SRL srlwi
208 1.1 matt #define INT_SRLV srlw
209 1.1 matt #define INT_SRA srawi
210 1.1 matt #define INT_SRAV sraw
211 1.1 matt #else
212 1.1 matt #define INT_ADD add
213 1.1 matt #define INT_ADDI addi
214 1.1 matt #define INT_SUB sub
215 1.1 matt #define INT_SUBI subi
216 1.1 matt #define INT_SLLI slli
217 1.1 matt #define INT_SLL sll
218 1.1 matt #define INT_SRLI srli
219 1.1 matt #define INT_SRL srl
220 1.1 matt #define INT_SRAI srai
221 1.1 matt #define INT_SRA sra
222 1.1 matt #endif
223 1.1 matt
224 1.1 matt #define LONG_LA la
225 1.1 matt #define LONG_ADD add
226 1.1 matt #define LONG_ADDI addi
227 1.1 matt #define LONG_SUB sub
228 1.1 matt #define LONG_SUBI subi
229 1.1 matt #define LONG_SLLI slli
230 1.1 matt #define LONG_SLL sll
231 1.1 matt #define LONG_SRLI srli
232 1.1 matt #define LONG_SRL srl
233 1.1 matt #define LONG_SRAI srai
234 1.1 matt #define LONG_SRA sra
235 1.1 matt #ifdef _LP64
236 1.1 matt #define LONG_L ld
237 1.1 matt #define LONG_S sd
238 1.1 matt #define LONG_LR lr.d
239 1.1 matt #define LONG_SC sc.d
240 1.1 matt #define LONG_WORD .quad
241 1.1 matt #define LONG_SCALESHIFT 3
242 1.1 matt #else
243 1.1 matt #define LONG_L lw
244 1.1 matt #define LONG_S sw
245 1.1 matt #define LONG_LR lr.w
246 1.1 matt #define LONG_SC sc.w
247 1.1 matt #define LONG_WORD .word
248 1.1 matt #define LONG_SCALESHIFT 2
249 1.1 matt #endif
250 1.1 matt
251 1.1 matt #define REG_LI li
252 1.1 matt #define REG_ADD add
253 1.1 matt #define REG_SLLI slli
254 1.1 matt #define REG_SLL sll
255 1.1 matt #define REG_SRLI srli
256 1.1 matt #define REG_SRL srl
257 1.1 matt #define REG_SRAI srai
258 1.1 matt #define REG_SRA sra
259 1.1 matt #if _LP64
260 1.1 matt #define REG_L ld
261 1.1 matt #define REG_S sd
262 1.1 matt #define REG_LR lr.d
263 1.1 matt #define REG_SC sc.d
264 1.1 matt #define REG_SCALESHIFT 3
265 1.1 matt #else
266 1.1 matt #define REG_L lw
267 1.1 matt #define REG_S sw
268 1.1 matt #define REG_LR lr.w
269 1.1 matt #define REG_SC sc.w
270 1.1 matt #define REG_SCALESHIFT 2
271 1.1 matt #endif
272 1.1 matt
273 1.1 matt #define CPUVAR(off) _C_LABEL(cpu_info_store)+__CONCAT(CPU_INFO_,off)
274 1.1 matt
275 1.1 matt #endif /* _RISCV_ASM_H */
276