Rizin
unix-like reverse engineering framework and cli tools
riscv.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011-2017 Free Software Foundation, Inc.
2 // SPDX-License-Identifier: GPL-3.0-or-later
3 
4 /* riscv.h. RISC-V opcode list for GDB, the GNU debugger.
5  Copyright (C) 2011-2017 Free Software Foundation, Inc.
6  Contributed by Andrew Waterman
7 
8  This file is part of GDB, GAS, and the GNU binutils.
9 
10  GDB, GAS, and the GNU binutils are free software; you can redistribute
11  them and/or modify them under the terms of the GNU General Public
12  License as published by the Free Software Foundation; either version
13  3, or (at your option) any later version.
14 
15  GDB, GAS, and the GNU binutils are distributed in the hope that they
16  will be useful, but WITHOUT ANY WARRANTY; without even the implied
17  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18  the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; see the file COPYING3. If not,
22  see <http://www.gnu.org/licenses/>. */
23 
24 #ifndef _RISCV_H_
25 #define _RISCV_H_
26 
27 #include "riscv-opc.h"
28 #include <stdlib.h>
29 #include <stdint.h>
30 
31 typedef uint64_t insn_t;
32 
33 static inline unsigned int riscv_insn_length (insn_t insn)
34 {
35  if ((insn & 0x3) != 0x3) /* RVC. */
36  return 2;
37  if ((insn & 0x1f) != 0x1f) /* Base ISA and extensions in 32-bit space. */
38  return 4;
39  if ((insn & 0x3f) == 0x1f) /* 48-bit extensions. */
40  return 6;
41  if ((insn & 0x7f) == 0x3f) /* 64-bit extensions. */
42  return 8;
43  /* Longer instructions not supported at the moment. */
44  return 2;
45 }
46 
47 static const char * const riscv_rm[8] =
48 {
49  "rne", "rtz", "rdn", "rup", "rmm", 0, 0, "dyn"
50 };
51 
52 static const char * const riscv_pred_succ[16] =
53 {
54  0, "w", "r", "rw", "o", "ow", "or", "orw",
55  "i", "iw", "ir", "irw", "io", "iow", "ior", "iorw"
56 };
57 
58 #define RVC_JUMP_BITS 11
59 #define RVC_JUMP_REACH ((1ULL << RVC_JUMP_BITS) * RISCV_JUMP_ALIGN)
60 
61 #define RVC_BRANCH_BITS 8
62 #define RVC_BRANCH_REACH ((1ULL << RVC_BRANCH_BITS) * RISCV_BRANCH_ALIGN)
63 
64 #define RV_X(x, s, n) (((x) >> (s)) & ((1 << (n)) - 1))
65 #define RV_IMM_SIGN(x) (-(((x) >> 31) & 1))
66 
67 #define EXTRACT_ITYPE_IMM(x) \
68  (RV_X(x, 20, 12) | (RV_IMM_SIGN(x) << 12))
69 #define EXTRACT_STYPE_IMM(x) \
70  (RV_X(x, 7, 5) | (RV_X(x, 25, 7) << 5) | (RV_IMM_SIGN(x) << 12))
71 #define EXTRACT_SBTYPE_IMM(x) \
72  ((RV_X(x, 8, 4) << 1) | (RV_X(x, 25, 6) << 5) | (RV_X(x, 7, 1) << 11) | (RV_IMM_SIGN(x) << 12))
73 #define EXTRACT_UTYPE_IMM(x) \
74  ((RV_X(x, 12, 20) << 12) | (RV_IMM_SIGN(x) << 32))
75 #define EXTRACT_UJTYPE_IMM(x) \
76  ((RV_X(x, 21, 10) << 1) | (RV_X(x, 20, 1) << 11) | (RV_X(x, 12, 8) << 12) | (RV_IMM_SIGN(x) << 20))
77 #define EXTRACT_RVC_IMM(x) \
78  (RV_X(x, 2, 5) | (-RV_X(x, 12, 1) << 5))
79 #define EXTRACT_RVC_LUI_IMM(x) \
80  (EXTRACT_RVC_IMM (x) << RISCV_IMM_BITS)
81 #define EXTRACT_RVC_SIMM3(x) \
82  (RV_X(x, 10, 2) | (-RV_X(x, 12, 1) << 2))
83 #define EXTRACT_RVC_ADDI4SPN_IMM(x) \
84  ((RV_X(x, 6, 1) << 2) | (RV_X(x, 5, 1) << 3) | (RV_X(x, 11, 2) << 4) | (RV_X(x, 7, 4) << 6))
85 #define EXTRACT_RVC_ADDI16SP_IMM(x) \
86  ((RV_X(x, 6, 1) << 4) | (RV_X(x, 2, 1) << 5) | (RV_X(x, 5, 1) << 6) | (RV_X(x, 3, 2) << 7) | (-RV_X(x, 12, 1) << 9))
87 #define EXTRACT_RVC_LW_IMM(x) \
88  ((RV_X(x, 6, 1) << 2) | (RV_X(x, 10, 3) << 3) | (RV_X(x, 5, 1) << 6))
89 #define EXTRACT_RVC_LD_IMM(x) \
90  ((RV_X(x, 10, 3) << 3) | (RV_X(x, 5, 2) << 6))
91 #define EXTRACT_RVC_LWSP_IMM(x) \
92  ((RV_X(x, 4, 3) << 2) | (RV_X(x, 12, 1) << 5) | (RV_X(x, 2, 2) << 6))
93 #define EXTRACT_RVC_LDSP_IMM(x) \
94  ((RV_X(x, 5, 2) << 3) | (RV_X(x, 12, 1) << 5) | (RV_X(x, 2, 3) << 6))
95 #define EXTRACT_RVC_SWSP_IMM(x) \
96  ((RV_X(x, 9, 4) << 2) | (RV_X(x, 7, 2) << 6))
97 #define EXTRACT_RVC_SDSP_IMM(x) \
98  ((RV_X(x, 10, 3) << 3) | (RV_X(x, 7, 3) << 6))
99 #define EXTRACT_RVC_B_IMM(x) \
100  ((RV_X(x, 3, 2) << 1) | (RV_X(x, 10, 2) << 3) | (RV_X(x, 2, 1) << 5) | (RV_X(x, 5, 2) << 6) | (-RV_X(x, 12, 1) << 8))
101 #define EXTRACT_RVC_J_IMM(x) \
102  ((RV_X(x, 3, 3) << 1) | (RV_X(x, 11, 1) << 4) | (RV_X(x, 2, 1) << 5) | (RV_X(x, 7, 1) << 6) | (RV_X(x, 6, 1) << 7) | (RV_X(x, 9, 2) << 8) | (RV_X(x, 8, 1) << 10) | (-RV_X(x, 12, 1) << 11))
103 
104 #define ENCODE_ITYPE_IMM(x) \
105  (RV_X(x, 0, 12) << 20)
106 #define ENCODE_STYPE_IMM(x) \
107  ((RV_X(x, 0, 5) << 7) | (RV_X(x, 5, 7) << 25))
108 #define ENCODE_SBTYPE_IMM(x) \
109  ((RV_X(x, 1, 4) << 8) | (RV_X(x, 5, 6) << 25) | (RV_X(x, 11, 1) << 7) | (RV_X(x, 12, 1) << 31))
110 #define ENCODE_UTYPE_IMM(x) \
111  (RV_X(x, 12, 20) << 12)
112 #define ENCODE_UJTYPE_IMM(x) \
113  ((RV_X(x, 1, 10) << 21) | (RV_X(x, 11, 1) << 20) | (RV_X(x, 12, 8) << 12) | (RV_X(x, 20, 1) << 31))
114 #define ENCODE_RVC_IMM(x) \
115  ((RV_X(x, 0, 5) << 2) | (RV_X(x, 5, 1) << 12))
116 #define ENCODE_RVC_LUI_IMM(x) \
117  ENCODE_RVC_IMM ((x) >> RISCV_IMM_BITS)
118 #define ENCODE_RVC_SIMM3(x) \
119  (RV_X(x, 0, 3) << 10)
120 #define ENCODE_RVC_ADDI4SPN_IMM(x) \
121  ((RV_X(x, 2, 1) << 6) | (RV_X(x, 3, 1) << 5) | (RV_X(x, 4, 2) << 11) | (RV_X(x, 6, 4) << 7))
122 #define ENCODE_RVC_ADDI16SP_IMM(x) \
123  ((RV_X(x, 4, 1) << 6) | (RV_X(x, 5, 1) << 2) | (RV_X(x, 6, 1) << 5) | (RV_X(x, 7, 2) << 3) | (RV_X(x, 9, 1) << 12))
124 #define ENCODE_RVC_LW_IMM(x) \
125  ((RV_X(x, 2, 1) << 6) | (RV_X(x, 3, 3) << 10) | (RV_X(x, 6, 1) << 5))
126 #define ENCODE_RVC_LD_IMM(x) \
127  ((RV_X(x, 3, 3) << 10) | (RV_X(x, 6, 2) << 5))
128 #define ENCODE_RVC_LWSP_IMM(x) \
129  ((RV_X(x, 2, 3) << 4) | (RV_X(x, 5, 1) << 12) | (RV_X(x, 6, 2) << 2))
130 #define ENCODE_RVC_LDSP_IMM(x) \
131  ((RV_X(x, 3, 2) << 5) | (RV_X(x, 5, 1) << 12) | (RV_X(x, 6, 3) << 2))
132 #define ENCODE_RVC_SWSP_IMM(x) \
133  ((RV_X(x, 2, 4) << 9) | (RV_X(x, 6, 2) << 7))
134 #define ENCODE_RVC_SDSP_IMM(x) \
135  ((RV_X(x, 3, 3) << 10) | (RV_X(x, 6, 3) << 7))
136 #define ENCODE_RVC_B_IMM(x) \
137  ((RV_X(x, 1, 2) << 3) | (RV_X(x, 3, 2) << 10) | (RV_X(x, 5, 1) << 2) | (RV_X(x, 6, 2) << 5) | (RV_X(x, 8, 1) << 12))
138 #define ENCODE_RVC_J_IMM(x) \
139  ((RV_X(x, 1, 3) << 3) | (RV_X(x, 4, 1) << 11) | (RV_X(x, 5, 1) << 2) | (RV_X(x, 6, 1) << 7) | (RV_X(x, 7, 1) << 6) | (RV_X(x, 8, 2) << 9) | (RV_X(x, 10, 1) << 8) | (RV_X(x, 11, 1) << 12))
140 
141 #define VALID_ITYPE_IMM(x) (EXTRACT_ITYPE_IMM(ENCODE_ITYPE_IMM(x)) == (x))
142 #define VALID_STYPE_IMM(x) (EXTRACT_STYPE_IMM(ENCODE_STYPE_IMM(x)) == (x))
143 #define VALID_SBTYPE_IMM(x) (EXTRACT_SBTYPE_IMM(ENCODE_SBTYPE_IMM(x)) == (x))
144 #define VALID_UTYPE_IMM(x) (EXTRACT_UTYPE_IMM(ENCODE_UTYPE_IMM(x)) == (x))
145 #define VALID_UJTYPE_IMM(x) (EXTRACT_UJTYPE_IMM(ENCODE_UJTYPE_IMM(x)) == (x))
146 #define VALID_RVC_IMM(x) (EXTRACT_RVC_IMM(ENCODE_RVC_IMM(x)) == (x))
147 #define VALID_RVC_LUI_IMM(x) (EXTRACT_RVC_LUI_IMM(ENCODE_RVC_LUI_IMM(x)) == (x))
148 #define VALID_RVC_SIMM3(x) (EXTRACT_RVC_SIMM3(ENCODE_RVC_SIMM3(x)) == (x))
149 #define VALID_RVC_ADDI4SPN_IMM(x) (EXTRACT_RVC_ADDI4SPN_IMM(ENCODE_RVC_ADDI4SPN_IMM(x)) == (x))
150 #define VALID_RVC_ADDI16SP_IMM(x) (EXTRACT_RVC_ADDI16SP_IMM(ENCODE_RVC_ADDI16SP_IMM(x)) == (x))
151 #define VALID_RVC_LW_IMM(x) (EXTRACT_RVC_LW_IMM(ENCODE_RVC_LW_IMM(x)) == (x))
152 #define VALID_RVC_LD_IMM(x) (EXTRACT_RVC_LD_IMM(ENCODE_RVC_LD_IMM(x)) == (x))
153 #define VALID_RVC_LWSP_IMM(x) (EXTRACT_RVC_LWSP_IMM(ENCODE_RVC_LWSP_IMM(x)) == (x))
154 #define VALID_RVC_LDSP_IMM(x) (EXTRACT_RVC_LDSP_IMM(ENCODE_RVC_LDSP_IMM(x)) == (x))
155 #define VALID_RVC_SWSP_IMM(x) (EXTRACT_RVC_SWSP_IMM(ENCODE_RVC_SWSP_IMM(x)) == (x))
156 #define VALID_RVC_SDSP_IMM(x) (EXTRACT_RVC_SDSP_IMM(ENCODE_RVC_SDSP_IMM(x)) == (x))
157 #define VALID_RVC_B_IMM(x) (EXTRACT_RVC_B_IMM(ENCODE_RVC_B_IMM(x)) == (x))
158 #define VALID_RVC_J_IMM(x) (EXTRACT_RVC_J_IMM(ENCODE_RVC_J_IMM(x)) == (x))
159 
160 #define RISCV_RTYPE(insn, rd, rs1, rs2) \
161  ((MATCH_ ## insn) | ((rd) << OP_SH_RD) | ((rs1) << OP_SH_RS1) | ((rs2) << OP_SH_RS2))
162 #define RISCV_ITYPE(insn, rd, rs1, imm) \
163  ((MATCH_ ## insn) | ((rd) << OP_SH_RD) | ((rs1) << OP_SH_RS1) | ENCODE_ITYPE_IMM(imm))
164 #define RISCV_STYPE(insn, rs1, rs2, imm) \
165  ((MATCH_ ## insn) | ((rs1) << OP_SH_RS1) | ((rs2) << OP_SH_RS2) | ENCODE_STYPE_IMM(imm))
166 #define RISCV_SBTYPE(insn, rs1, rs2, target) \
167  ((MATCH_ ## insn) | ((rs1) << OP_SH_RS1) | ((rs2) << OP_SH_RS2) | ENCODE_SBTYPE_IMM(target))
168 #define RISCV_UTYPE(insn, rd, bigimm) \
169  ((MATCH_ ## insn) | ((rd) << OP_SH_RD) | ENCODE_UTYPE_IMM(bigimm))
170 #define RISCV_UJTYPE(insn, rd, target) \
171  ((MATCH_ ## insn) | ((rd) << OP_SH_RD) | ENCODE_UJTYPE_IMM(target))
172 
173 #define RISCV_NOP RISCV_ITYPE(ADDI, 0, 0, 0)
174 #define RVC_NOP MATCH_C_ADDI
175 
176 #define RISCV_CONST_HIGH_PART(VALUE) \
177  (((VALUE) + (RISCV_IMM_REACH/2)) & ~(RISCV_IMM_REACH-1))
178 #define RISCV_CONST_LOW_PART(VALUE) ((VALUE) - RISCV_CONST_HIGH_PART (VALUE))
179 #define RISCV_PCREL_HIGH_PART(VALUE, PC) RISCV_CONST_HIGH_PART((VALUE) - (PC))
180 #define RISCV_PCREL_LOW_PART(VALUE, PC) RISCV_CONST_LOW_PART((VALUE) - (PC))
181 
182 #define RISCV_JUMP_BITS RISCV_BIGIMM_BITS
183 #define RISCV_JUMP_ALIGN_BITS 1
184 #define RISCV_JUMP_ALIGN (1 << RISCV_JUMP_ALIGN_BITS)
185 #define RISCV_JUMP_REACH ((1ULL << RISCV_JUMP_BITS) * RISCV_JUMP_ALIGN)
186 
187 #define RISCV_IMM_BITS 12
188 #define RISCV_BIGIMM_BITS (32 - RISCV_IMM_BITS)
189 #define RISCV_IMM_REACH (1LL << RISCV_IMM_BITS)
190 #define RISCV_BIGIMM_REACH (1LL << RISCV_BIGIMM_BITS)
191 #define RISCV_RVC_IMM_REACH (1LL << 6)
192 #define RISCV_BRANCH_BITS RISCV_IMM_BITS
193 #define RISCV_BRANCH_ALIGN_BITS RISCV_JUMP_ALIGN_BITS
194 #define RISCV_BRANCH_ALIGN (1 << RISCV_BRANCH_ALIGN_BITS)
195 #define RISCV_BRANCH_REACH (RISCV_IMM_REACH * RISCV_BRANCH_ALIGN)
196 
197 /* RV fields. */
198 
199 #define OP_MASK_OP 0x7f
200 #define OP_SH_OP 0
201 #define OP_MASK_RS2 0x1f
202 #define OP_SH_RS2 20
203 #define OP_MASK_RS1 0x1f
204 #define OP_SH_RS1 15
205 #define OP_MASK_RS3 0x1f
206 #define OP_SH_RS3 27
207 #define OP_MASK_RD 0x1f
208 #define OP_SH_RD 7
209 #define OP_MASK_SHAMT 0x3f
210 #define OP_SH_SHAMT 20
211 #define OP_MASK_SHAMTW 0x1f
212 #define OP_SH_SHAMTW 20
213 #define OP_MASK_RM 0x7
214 #define OP_SH_RM 12
215 #define OP_MASK_PRED 0xf
216 #define OP_SH_PRED 24
217 #define OP_MASK_SUCC 0xf
218 #define OP_SH_SUCC 20
219 #define OP_MASK_AQ 0x1
220 #define OP_SH_AQ 26
221 #define OP_MASK_RL 0x1
222 #define OP_SH_RL 25
223 
224 #define OP_MASK_CUSTOM_IMM 0x7f
225 #define OP_SH_CUSTOM_IMM 25
226 #define OP_MASK_CSR 0xfff
227 #define OP_SH_CSR 20
228 
229 /* RVC fields. */
230 
231 #define OP_MASK_CRS2 0x1f
232 #define OP_SH_CRS2 2
233 #define OP_MASK_CRS1S 0x7
234 #define OP_SH_CRS1S 7
235 #define OP_MASK_CRS2S 0x7
236 #define OP_SH_CRS2S 2
237 
238 /* ABI names for selected x-registers. */
239 
240 #define X_RA 1
241 #define X_SP 2
242 #define X_GP 3
243 #define X_TP 4
244 #define X_T0 5
245 #define X_T1 6
246 #define X_T2 7
247 #define X_T3 28
248 
249 #define NGPR 32
250 #define NFPR 32
251 
252 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
253  VALUE << SHIFT. VALUE is evaluated exactly once. */
254 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
255  (STRUCT) = (((STRUCT) & ~((insn_t)(MASK) << (SHIFT))) \
256  | ((insn_t)((VALUE) & (MASK)) << (SHIFT)))
257 
258 /* Extract bits MASK << SHIFT from STRUCT and shift them right
259  SHIFT places. */
260 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
261  (((STRUCT) >> (SHIFT)) & (MASK))
262 
263 /* Extract the operand given by FIELD from integer INSN. */
264 #define EXTRACT_OPERAND(FIELD, INSN) \
265  EXTRACT_BITS ((INSN), OP_MASK_##FIELD, OP_SH_##FIELD)
266 
267 /* This structure holds information for a particular instruction. */
268 
270 {
271  /* The name of the instruction. */
272  const char *name;
273  /* The ISA subset name (I, M, A, F, D, Xextension). */
274  const char *subset;
275  /* A string describing the arguments for this instruction. */
276  const char *args;
277  /* The basic opcode for the instruction. When assembling, this
278  opcode is modified by the arguments to produce the actual opcode
279  that is used. If pinfo is INSN_MACRO, then this is 0. */
281  /* If pinfo is not INSN_MACRO, then this is a bit mask for the
282  relevant portions of the opcode when disassembling. If the
283  actual opcode anded with the match field equals the opcode field,
284  then we have found the correct instruction. If pinfo is
285  INSN_MACRO, then this field is the macro identifier. */
287  /* A function to determine if a word corresponds to this instruction.
288  Usually, this computes ((word & mask) == match). */
289  int (*match_func) (const struct riscv_opcode *op, insn_t word);
290  /* For a macro, this is INSN_MACRO. Otherwise, it is a collection
291  of bits describing the instruction, notably any relevant hazard
292  information. */
293  unsigned long pinfo;
294 };
295 
296 /* Instruction is a simple alias (e.g. "mv" for "addi"). */
297 #define INSN_ALIAS 0x00000001
298 /* Instruction is actually a macro. It should be ignored by the
299  disassembler, and requires special treatment by the assembler. */
300 #define INSN_MACRO 0xffffffff
301 
302 /* This is a list of macro expanded instructions.
303 
304  _I appended means immediate
305  _A appended means address
306  _AB appended means address with base register
307  _D appended means 64 bit floating point constant
308  _S appended means 32 bit floating point constant. */
309 
310 enum
311 {
337 };
338 
339 
340 #if 0 || _MSC_VER
341 extern const char * const riscv_gpr_names_numeric[NGPR];
342 extern const char * const riscv_gpr_names_abi[NGPR];
343 extern const char * const riscv_fpr_names_numeric[NFPR];
344 extern const char * const riscv_fpr_names_abi[NFPR];
345 extern struct riscv_opcode *riscv_opcodes;
346 //extern const struct riscv_opcode riscv_opcodes[];
347 #ifdef _MSC_VER
348 extern int bfd_riscv_num_opcodes;
349 #endif
350 #endif
351 #define NUMOPCODES bfd_riscv_num_opcodes
352 
353 #endif /* _RISCV_H_ */
ut8 op
Definition: 6502dis.c:13
static const char *const riscv_gpr_names_numeric[NGPR]
Definition: riscv-opc.c:31
static const char *const riscv_gpr_names_abi[NGPR]
Definition: riscv-opc.c:39
static int bfd_riscv_num_opcodes
Definition: riscv-opc.c:687
static const char *const riscv_fpr_names_abi[NFPR]
Definition: riscv-opc.c:54
static struct riscv_opcode * riscv_opcodes
Definition: riscv-opc.c:685
static const char *const riscv_fpr_names_numeric[NFPR]
Definition: riscv-opc.c:46
@ M_LWU
Definition: riscv.h:321
@ M_SD
Definition: riscv.h:326
@ M_LLA
Definition: riscv.h:313
@ M_FLQ
Definition: riscv.h:329
@ M_FSQ
Definition: riscv.h:332
@ M_CALL
Definition: riscv.h:333
@ M_LHU
Definition: riscv.h:319
@ M_SW
Definition: riscv.h:325
@ M_LW
Definition: riscv.h:320
@ M_FSD
Definition: riscv.h:331
@ M_FLD
Definition: riscv.h:328
@ M_LA
Definition: riscv.h:312
@ M_LD
Definition: riscv.h:322
@ M_LH
Definition: riscv.h:318
@ M_FLW
Definition: riscv.h:327
@ M_SH
Definition: riscv.h:324
@ M_LBU
Definition: riscv.h:317
@ M_J
Definition: riscv.h:334
@ M_LB
Definition: riscv.h:316
@ M_FSW
Definition: riscv.h:330
@ M_LA_TLS_IE
Definition: riscv.h:315
@ M_NUM_MACROS
Definition: riscv.h:336
@ M_LA_TLS_GD
Definition: riscv.h:314
@ M_SB
Definition: riscv.h:323
@ M_LI
Definition: riscv.h:335
#define NGPR
Definition: riscv.h:249
static const char *const riscv_rm[8]
Definition: riscv.h:47
#define NFPR
Definition: riscv.h:250
static const char *const riscv_pred_succ[16]
Definition: riscv.h:52
uint64_t insn_t
Definition: riscv.h:31
static unsigned int riscv_insn_length(insn_t insn)
Definition: riscv.h:33
static int
Definition: sfsocketcall.h:114
unsigned long uint64_t
Definition: sftypes.h:28
const char * args
Definition: riscv.h:276
const char * name
Definition: riscv.h:272
insn_t match
Definition: riscv.h:280
insn_t mask
Definition: riscv.h:286
unsigned long pinfo
Definition: riscv.h:293
int(* match_func)(const struct riscv_opcode *op, insn_t word)
Definition: riscv.h:289
const char * subset
Definition: riscv.h:274