Skip to content

Commit f51dd88

Browse files
authored
Merge pull request #980 from cla7aye15I4nd/riscv
Introduce “riscv” architecture to qiling
2 parents ffb4011 + 32ff7d0 commit f51dd88

File tree

20 files changed

+1221
-25
lines changed

20 files changed

+1221
-25
lines changed

examples/src/linux/hello_riscv.s

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# riscv${bit}-unknown-elf-as hello_riscv.s -o hello_riscv.o
2+
# riscv${bit}-unknown-elf-ld hello_riscv.o -o hello_riscv
3+
4+
.global _start
5+
6+
_start: addi a0, x0, 1
7+
la a1, helloriscv
8+
addi a2, x0, 13
9+
addi a7, x0, 64
10+
ecall
11+
addi a0, x0, 0
12+
addi a7, x0, 93
13+
ecall
14+
15+
.data
16+
helloriscv: .ascii "Hello RISCV!\n"

qiling/arch/riscv.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
4+
#
5+
6+
from unicorn import Uc, UC_ARCH_RISCV, UC_MODE_RISCV32
7+
from capstone import Cs
8+
from keystone import Ks
9+
10+
from qiling import Qiling
11+
from qiling.arch.arch import QlArch
12+
from qiling.arch.riscv_const import *
13+
from qiling.exception import QlErrorNotImplemented
14+
15+
16+
class QlArchRISCV(QlArch):
17+
def __init__(self, ql: Qiling):
18+
super().__init__(ql)
19+
20+
reg_maps = (
21+
reg_map,
22+
reg_csr_map,
23+
reg_float_map,
24+
)
25+
26+
for reg_maper in reg_maps:
27+
self.ql.reg.expand_mapping(reg_maper)
28+
self.ql.reg.register_sp(reg_map["sp"])
29+
self.ql.reg.register_pc(reg_map["pc"])
30+
31+
# get initialized unicorn engine
32+
def get_init_uc(self) -> Uc:
33+
return Uc(UC_ARCH_RISCV, UC_MODE_RISCV32)
34+
35+
def create_disassembler(self) -> Cs:
36+
try:
37+
from capstone import CS_ARCH_RISCV, CS_MODE_RISCV32
38+
return Cs(CS_ARCH_RISCV, CS_MODE_RISCV32)
39+
except ImportError:
40+
raise QlErrorNotImplemented("Capstone does not yet support riscv, upgrade to capstone 5.0")
41+
42+
def create_assembler(self) -> Ks:
43+
raise QlErrorNotImplemented("Keystone does not yet support riscv")
44+
45+
def enable_float(self):
46+
self.ql.reg.mstatus = self.ql.reg.mstatus | MSTATUS.FS_DIRTY

qiling/arch/riscv64.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
4+
#
5+
6+
from unicorn import Uc, UC_ARCH_RISCV, UC_MODE_RISCV64
7+
from capstone import Cs
8+
from keystone import Ks
9+
10+
from qiling import Qiling
11+
from qiling.arch.riscv_const import *
12+
from qiling.exception import QlErrorNotImplemented
13+
14+
from .riscv import QlArchRISCV
15+
16+
17+
class QlArchRISCV64(QlArchRISCV):
18+
def __init__(self, ql: Qiling):
19+
super().__init__(ql)
20+
21+
# get initialized unicorn engine
22+
def get_init_uc(self) -> Uc:
23+
return Uc(UC_ARCH_RISCV, UC_MODE_RISCV64)
24+
25+
def create_disassembler(self) -> Cs:
26+
try:
27+
from capstone import CS_ARCH_RISCV, CS_MODE_RISCV64, CS_MODE_RISCVC
28+
return Cs(CS_ARCH_RISCV, CS_MODE_RISCV64 + CS_MODE_RISCVC)
29+
except ImportError:
30+
raise QlErrorNotImplemented("Capstone does not yet support riscv, upgrade to capstone 5.0")
31+
32+
def create_assembler(self) -> Ks:
33+
raise QlErrorNotImplemented("Keystone does not yet support riscv")

qiling/arch/riscv_const.py

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
4+
#
5+
6+
from unicorn.riscv_const import *
7+
from enum import IntEnum
8+
9+
10+
reg_general_map = {
11+
"x0": UC_RISCV_REG_X0,
12+
"x1": UC_RISCV_REG_X1,
13+
"x2": UC_RISCV_REG_X2,
14+
"x3": UC_RISCV_REG_X3,
15+
"x4": UC_RISCV_REG_X4,
16+
"x5": UC_RISCV_REG_X5,
17+
"x6": UC_RISCV_REG_X6,
18+
"x7": UC_RISCV_REG_X7,
19+
"x8": UC_RISCV_REG_X8,
20+
"x9": UC_RISCV_REG_X9,
21+
"x10": UC_RISCV_REG_X10,
22+
"x11": UC_RISCV_REG_X11,
23+
"x12": UC_RISCV_REG_X12,
24+
"x13": UC_RISCV_REG_X13,
25+
"x14": UC_RISCV_REG_X14,
26+
"x15": UC_RISCV_REG_X15,
27+
"x16": UC_RISCV_REG_X16,
28+
"x17": UC_RISCV_REG_X17,
29+
"x18": UC_RISCV_REG_X18,
30+
"x19": UC_RISCV_REG_X19,
31+
"x20": UC_RISCV_REG_X20,
32+
"x21": UC_RISCV_REG_X21,
33+
"x22": UC_RISCV_REG_X22,
34+
"x23": UC_RISCV_REG_X23,
35+
"x24": UC_RISCV_REG_X24,
36+
"x25": UC_RISCV_REG_X25,
37+
"x26": UC_RISCV_REG_X26,
38+
"x27": UC_RISCV_REG_X27,
39+
"x28": UC_RISCV_REG_X28,
40+
"x29": UC_RISCV_REG_X29,
41+
"x30": UC_RISCV_REG_X30,
42+
"x31": UC_RISCV_REG_X31,
43+
}
44+
45+
reg_csr_map = {
46+
"ustatus": UC_RISCV_REG_USTATUS,
47+
"uie": UC_RISCV_REG_UIE,
48+
"utvec": UC_RISCV_REG_UTVEC,
49+
"uscratch": UC_RISCV_REG_USCRATCH,
50+
"uepc": UC_RISCV_REG_UEPC,
51+
"ucause": UC_RISCV_REG_UCAUSE,
52+
"utval": UC_RISCV_REG_UTVAL,
53+
"uip": UC_RISCV_REG_UIP,
54+
"fflags": UC_RISCV_REG_FFLAGS,
55+
"frm": UC_RISCV_REG_FRM,
56+
"fcsr": UC_RISCV_REG_FCSR,
57+
"cycle": UC_RISCV_REG_CYCLE,
58+
"time": UC_RISCV_REG_TIME,
59+
"instret": UC_RISCV_REG_INSTRET,
60+
"hpmcounter3": UC_RISCV_REG_HPMCOUNTER3,
61+
"hpmcounter4": UC_RISCV_REG_HPMCOUNTER4,
62+
"hpmcounter5": UC_RISCV_REG_HPMCOUNTER5,
63+
"hpmcounter6": UC_RISCV_REG_HPMCOUNTER6,
64+
"hpmcounter7": UC_RISCV_REG_HPMCOUNTER7,
65+
"hpmcounter8": UC_RISCV_REG_HPMCOUNTER8,
66+
"hpmcounter9": UC_RISCV_REG_HPMCOUNTER9,
67+
"hpmcounter10": UC_RISCV_REG_HPMCOUNTER10,
68+
"hpmcounter11": UC_RISCV_REG_HPMCOUNTER11,
69+
"hpmcounter12": UC_RISCV_REG_HPMCOUNTER12,
70+
"hpmcounter13": UC_RISCV_REG_HPMCOUNTER13,
71+
"hpmcounter14": UC_RISCV_REG_HPMCOUNTER14,
72+
"hpmcounter15": UC_RISCV_REG_HPMCOUNTER15,
73+
"hpmcounter16": UC_RISCV_REG_HPMCOUNTER16,
74+
"hpmcounter17": UC_RISCV_REG_HPMCOUNTER17,
75+
"hpmcounter18": UC_RISCV_REG_HPMCOUNTER18,
76+
"hpmcounter19": UC_RISCV_REG_HPMCOUNTER19,
77+
"hpmcounter20": UC_RISCV_REG_HPMCOUNTER20,
78+
"hpmcounter21": UC_RISCV_REG_HPMCOUNTER21,
79+
"hpmcounter22": UC_RISCV_REG_HPMCOUNTER22,
80+
"hpmcounter23": UC_RISCV_REG_HPMCOUNTER23,
81+
"hpmcounter24": UC_RISCV_REG_HPMCOUNTER24,
82+
"hpmcounter25": UC_RISCV_REG_HPMCOUNTER25,
83+
"hpmcounter26": UC_RISCV_REG_HPMCOUNTER26,
84+
"hpmcounter27": UC_RISCV_REG_HPMCOUNTER27,
85+
"hpmcounter28": UC_RISCV_REG_HPMCOUNTER28,
86+
"hpmcounter29": UC_RISCV_REG_HPMCOUNTER29,
87+
"hpmcounter30": UC_RISCV_REG_HPMCOUNTER30,
88+
"hpmcounter31": UC_RISCV_REG_HPMCOUNTER31,
89+
"cycleh": UC_RISCV_REG_CYCLEH,
90+
"timeh": UC_RISCV_REG_TIMEH,
91+
"instreth": UC_RISCV_REG_INSTRETH,
92+
"hpmcounter3h": UC_RISCV_REG_HPMCOUNTER3H,
93+
"hpmcounter4h": UC_RISCV_REG_HPMCOUNTER4H,
94+
"hpmcounter5h": UC_RISCV_REG_HPMCOUNTER5H,
95+
"hpmcounter6h": UC_RISCV_REG_HPMCOUNTER6H,
96+
"hpmcounter7h": UC_RISCV_REG_HPMCOUNTER7H,
97+
"hpmcounter8h": UC_RISCV_REG_HPMCOUNTER8H,
98+
"hpmcounter9h": UC_RISCV_REG_HPMCOUNTER9H,
99+
"hpmcounter10h": UC_RISCV_REG_HPMCOUNTER10H,
100+
"hpmcounter11h": UC_RISCV_REG_HPMCOUNTER11H,
101+
"hpmcounter12h": UC_RISCV_REG_HPMCOUNTER12H,
102+
"hpmcounter13h": UC_RISCV_REG_HPMCOUNTER13H,
103+
"hpmcounter14h": UC_RISCV_REG_HPMCOUNTER14H,
104+
"hpmcounter15h": UC_RISCV_REG_HPMCOUNTER15H,
105+
"hpmcounter16h": UC_RISCV_REG_HPMCOUNTER16H,
106+
"hpmcounter17h": UC_RISCV_REG_HPMCOUNTER17H,
107+
"hpmcounter18h": UC_RISCV_REG_HPMCOUNTER18H,
108+
"hpmcounter19h": UC_RISCV_REG_HPMCOUNTER19H,
109+
"hpmcounter20h": UC_RISCV_REG_HPMCOUNTER20H,
110+
"hpmcounter21h": UC_RISCV_REG_HPMCOUNTER21H,
111+
"hpmcounter22h": UC_RISCV_REG_HPMCOUNTER22H,
112+
"hpmcounter23h": UC_RISCV_REG_HPMCOUNTER23H,
113+
"hpmcounter24h": UC_RISCV_REG_HPMCOUNTER24H,
114+
"hpmcounter25h": UC_RISCV_REG_HPMCOUNTER25H,
115+
"hpmcounter26h": UC_RISCV_REG_HPMCOUNTER26H,
116+
"hpmcounter27h": UC_RISCV_REG_HPMCOUNTER27H,
117+
"hpmcounter28h": UC_RISCV_REG_HPMCOUNTER28H,
118+
"hpmcounter29h": UC_RISCV_REG_HPMCOUNTER29H,
119+
"hpmcounter30h": UC_RISCV_REG_HPMCOUNTER30H,
120+
"hpmcounter31h": UC_RISCV_REG_HPMCOUNTER31H,
121+
"mcycle": UC_RISCV_REG_MCYCLE,
122+
"minstret": UC_RISCV_REG_MINSTRET,
123+
"mcycleh": UC_RISCV_REG_MCYCLEH,
124+
"minstreth": UC_RISCV_REG_MINSTRETH,
125+
"mvendorid": UC_RISCV_REG_MVENDORID,
126+
"marchid": UC_RISCV_REG_MARCHID,
127+
"mimpid": UC_RISCV_REG_MIMPID,
128+
"mhartid": UC_RISCV_REG_MHARTID,
129+
"mstatus": UC_RISCV_REG_MSTATUS,
130+
"misa": UC_RISCV_REG_MISA,
131+
"medeleg": UC_RISCV_REG_MEDELEG,
132+
"mideleg": UC_RISCV_REG_MIDELEG,
133+
"mie": UC_RISCV_REG_MIE,
134+
"mtvec": UC_RISCV_REG_MTVEC,
135+
"mcounteren": UC_RISCV_REG_MCOUNTEREN,
136+
"mstatush": UC_RISCV_REG_MSTATUSH,
137+
"mucounteren": UC_RISCV_REG_MUCOUNTEREN,
138+
"mscounteren": UC_RISCV_REG_MSCOUNTEREN,
139+
"mhcounteren": UC_RISCV_REG_MHCOUNTEREN,
140+
"mscratch": UC_RISCV_REG_MSCRATCH,
141+
"mepc": UC_RISCV_REG_MEPC,
142+
"mcause": UC_RISCV_REG_MCAUSE,
143+
"mtval": UC_RISCV_REG_MTVAL,
144+
"mip": UC_RISCV_REG_MIP,
145+
"mbadaddr": UC_RISCV_REG_MBADADDR,
146+
"sstatus": UC_RISCV_REG_SSTATUS,
147+
"sedeleg": UC_RISCV_REG_SEDELEG,
148+
"sideleg": UC_RISCV_REG_SIDELEG,
149+
"sie": UC_RISCV_REG_SIE,
150+
"stvec": UC_RISCV_REG_STVEC,
151+
"scounteren": UC_RISCV_REG_SCOUNTEREN,
152+
"sscratch": UC_RISCV_REG_SSCRATCH,
153+
"sepc": UC_RISCV_REG_SEPC,
154+
"scause": UC_RISCV_REG_SCAUSE,
155+
"stval": UC_RISCV_REG_STVAL,
156+
"sip": UC_RISCV_REG_SIP,
157+
"sbadaddr": UC_RISCV_REG_SBADADDR,
158+
"sptbr": UC_RISCV_REG_SPTBR,
159+
"satp": UC_RISCV_REG_SATP,
160+
"hstatus": UC_RISCV_REG_HSTATUS,
161+
"hedeleg": UC_RISCV_REG_HEDELEG,
162+
"hideleg": UC_RISCV_REG_HIDELEG,
163+
"hie": UC_RISCV_REG_HIE,
164+
"hcounteren": UC_RISCV_REG_HCOUNTEREN,
165+
"htval": UC_RISCV_REG_HTVAL,
166+
"hip": UC_RISCV_REG_HIP,
167+
"htinst": UC_RISCV_REG_HTINST,
168+
"hgatp": UC_RISCV_REG_HGATP,
169+
"htimedelta": UC_RISCV_REG_HTIMEDELTA,
170+
"htimedeltah": UC_RISCV_REG_HTIMEDELTAH,
171+
}
172+
173+
reg_float_map = {
174+
"f0": UC_RISCV_REG_F0,
175+
"f1": UC_RISCV_REG_F1,
176+
"f2": UC_RISCV_REG_F2,
177+
"f3": UC_RISCV_REG_F3,
178+
"f4": UC_RISCV_REG_F4,
179+
"f5": UC_RISCV_REG_F5,
180+
"f6": UC_RISCV_REG_F6,
181+
"f7": UC_RISCV_REG_F7,
182+
"f8": UC_RISCV_REG_F8,
183+
"f9": UC_RISCV_REG_F9,
184+
"f10": UC_RISCV_REG_F10,
185+
"f11": UC_RISCV_REG_F11,
186+
"f12": UC_RISCV_REG_F12,
187+
"f13": UC_RISCV_REG_F13,
188+
"f14": UC_RISCV_REG_F14,
189+
"f15": UC_RISCV_REG_F15,
190+
"f16": UC_RISCV_REG_F16,
191+
"f17": UC_RISCV_REG_F17,
192+
"f18": UC_RISCV_REG_F18,
193+
"f19": UC_RISCV_REG_F19,
194+
"f20": UC_RISCV_REG_F20,
195+
"f21": UC_RISCV_REG_F21,
196+
"f22": UC_RISCV_REG_F22,
197+
"f23": UC_RISCV_REG_F23,
198+
"f24": UC_RISCV_REG_F24,
199+
"f25": UC_RISCV_REG_F25,
200+
"f26": UC_RISCV_REG_F26,
201+
"f27": UC_RISCV_REG_F27,
202+
"f28": UC_RISCV_REG_F28,
203+
"f29": UC_RISCV_REG_F29,
204+
"f30": UC_RISCV_REG_F30,
205+
"f31": UC_RISCV_REG_F31,
206+
}
207+
208+
reg_map = {
209+
"pc": UC_RISCV_REG_PC,
210+
"zero": UC_RISCV_REG_ZERO,
211+
"ra": UC_RISCV_REG_RA,
212+
"sp": UC_RISCV_REG_SP,
213+
"gp": UC_RISCV_REG_GP,
214+
"tp": UC_RISCV_REG_TP,
215+
"t0": UC_RISCV_REG_T0,
216+
"t1": UC_RISCV_REG_T1,
217+
"t2": UC_RISCV_REG_T2,
218+
"s0": UC_RISCV_REG_S0,
219+
"fp": UC_RISCV_REG_FP,
220+
"s1": UC_RISCV_REG_S1,
221+
"a0": UC_RISCV_REG_A0,
222+
"a1": UC_RISCV_REG_A1,
223+
"a2": UC_RISCV_REG_A2,
224+
"a3": UC_RISCV_REG_A3,
225+
"a4": UC_RISCV_REG_A4,
226+
"a5": UC_RISCV_REG_A5,
227+
"a6": UC_RISCV_REG_A6,
228+
"a7": UC_RISCV_REG_A7,
229+
"s2": UC_RISCV_REG_S2,
230+
"s3": UC_RISCV_REG_S3,
231+
"s4": UC_RISCV_REG_S4,
232+
"s5": UC_RISCV_REG_S5,
233+
"s6": UC_RISCV_REG_S6,
234+
"s7": UC_RISCV_REG_S7,
235+
"s8": UC_RISCV_REG_S8,
236+
"s9": UC_RISCV_REG_S9,
237+
"s10": UC_RISCV_REG_S10,
238+
"s11": UC_RISCV_REG_S11,
239+
"t3": UC_RISCV_REG_T3,
240+
"t4": UC_RISCV_REG_T4,
241+
"t5": UC_RISCV_REG_T5,
242+
"t6": UC_RISCV_REG_T6,
243+
"ft0": UC_RISCV_REG_FT0,
244+
"ft1": UC_RISCV_REG_FT1,
245+
"ft2": UC_RISCV_REG_FT2,
246+
"ft3": UC_RISCV_REG_FT3,
247+
"ft4": UC_RISCV_REG_FT4,
248+
"ft5": UC_RISCV_REG_FT5,
249+
"ft6": UC_RISCV_REG_FT6,
250+
"ft7": UC_RISCV_REG_FT7,
251+
"fs0": UC_RISCV_REG_FS0,
252+
"fs1": UC_RISCV_REG_FS1,
253+
"fa0": UC_RISCV_REG_FA0,
254+
"fa1": UC_RISCV_REG_FA1,
255+
"fa2": UC_RISCV_REG_FA2,
256+
"fa3": UC_RISCV_REG_FA3,
257+
"fa4": UC_RISCV_REG_FA4,
258+
"fa5": UC_RISCV_REG_FA5,
259+
"fa6": UC_RISCV_REG_FA6,
260+
"fa7": UC_RISCV_REG_FA7,
261+
"fs2": UC_RISCV_REG_FS2,
262+
"fs3": UC_RISCV_REG_FS3,
263+
"fs4": UC_RISCV_REG_FS4,
264+
"fs5": UC_RISCV_REG_FS5,
265+
"fs6": UC_RISCV_REG_FS6,
266+
"fs7": UC_RISCV_REG_FS7,
267+
"fs8": UC_RISCV_REG_FS8,
268+
"fs9": UC_RISCV_REG_FS9,
269+
"fs10": UC_RISCV_REG_FS10,
270+
"fs11": UC_RISCV_REG_FS11,
271+
"ft8": UC_RISCV_REG_FT8,
272+
"ft9": UC_RISCV_REG_FT9,
273+
"ft10": UC_RISCV_REG_FT10,
274+
"ft11": UC_RISCV_REG_FT11,
275+
}
276+
277+
class MSTATUS(IntEnum):
278+
FS_OFF = 0
279+
FS_INITIAL = 1 << 13
280+
FS_CLEAN = 2 << 13
281+
FS_DIRTY = 3 << 13

0 commit comments

Comments
 (0)