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