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