-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSoftwareSingleStep.cpp
More file actions
177 lines (166 loc) · 6.4 KB
/
Copy pathSoftwareSingleStep.cpp
File metadata and controls
177 lines (166 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2022 Saleem Abdulrasool <compnerd@compnerd.org>
#include "DebugServer2/Architecture/RISCV/SoftwareSingleStep.h"
#include "DebugServer2/Utils/Log.h"
namespace ds2 {
namespace Architecture {
namespace RISCV {
namespace {
template <size_t Width>
uintptr_t sext(uintptr_t value) {
return ((value & (1 << Width)) ? ~((value & (1 << Width)) - 1) : 0) |
(value & ((1 << Width + 1) - 1));
}
template <size_t Begin, size_t End>
uintptr_t bits(uintptr_t value) {
uintptr_t mask = ((1 << ((End + 1) - Begin)) - 1) << Begin;
return (value & mask) >> Begin;
}
}
ErrorCode PrepareSoftwareSingleStep(Target::Process *process,
BreakpointManager *manager,
CPUState const &state,
Address const &address) {
uintptr_t location = address.valid() ? address.value() : state.pc();
uint16_t instruction;
CHK(process->readMemory(location, &instruction, sizeof(instruction)));
uintptr_t destination;
if ((instruction & 0x3) == 0x3) { // RVI
uint32_t instruction;
CHK(process->readMemory(location, &instruction, sizeof(instruction)));
switch (bits<0, 6>(instructions)) {
default:
destination = location + 4;
break;
case 0x00000063: { // BRANCH
uint8_t rs1 = bits<15, 19>(instruction);
uint8_t rs2 = bits<20, 24>(instruction);
uintptr_t immediate = (bits< 8, 11>(instruction) << 1)
| (bits<25, 30>(instruction) << 5)
| (bits< 7, 7>(instruction) << 11)
| (bits<31, 31>(instruction) << 12);
uintptr_t lhs = rs1 ? state.gp.regs[rs1] : 0;
uintptr_t rhs = rs2 ? state.gp.regs[rs2] : 0;
switch (bits<12, 14>(instruction)) {
case 0: // EQ
destination = location + (lhs == rhs ? sext<12>(immediate) : 4);
break;
case 1: // NE
destination = location + (lhs == rhs ? 4 : sext<12>(immediate));
break;
case 2:
DS2LOG(Debug, "unknown branch condition funct3=2");
DS2BUG("unknown branch condition 2");
case 3:
DS2LOG(Debug, "unknown branch condition funct3=3");
DS2BUG("unknown branch condition 3");
case 4: // LT
destination =
location + (static_cast<intptr_t>(lhs) < static_cast<intptr_t>(rhs)
? sext<12>(immediate)
: 4);
break;
case 5: // GE
destination =
location + (static_cast<intptr_t>(lhs) >= static_cast<intptr_t>(rhs)
? sext<12>(immediate)
: 4);
break;
case 6: // LTU
destination = location + (lhs < rhs ? sext<12>(immediate) : 4);
break;
case 7: // GTU
destination = location + (lhs >= rhs ? sext<12>(immediate) : 4);
break;
}
break;
}
case 0x00000067: {
switch (bits<12, 14>(instruction)) {
default:
destination = location + 4;
break;
case 0: { // JALR
uint8_t rs = bits<15, 19>(instruction);
uint8_t rd = bits< 7, 11>(instruction);
uintptr_t immediate = bits<20, 31>(instruction);
(void)rd;
uintptr_t base = rs ? state.gp.regs[rs] : 0;
destination = base + sext<11>(immediate);
break;
}
}
break;
}
case 0x0000006f: { // JAL
uintptr_t immediate = (bits<21, 30>(instruction) << 1)
| (bits<20, 20>(instruction) << 11)
| (bits<12, 19>(instruction) << 12)
| (bits<31, 31>(instruction) << 20);
destination = location + sext<20>(immediate);
break;
}
}
} else { // RVC
destination = location + 2;
switch (bits<0, 1>(instruction)) {
default: break;
case 0x01:
switch (bits<13, 15>(instruction)) {
default: break;
#if __riscv_len == 32
case 1: // C.JAL
[[fallthrough]];
#endif
case 5: { // C.J
uintptr_t immediate = (bits< 3, 5>(instruction) << 1)
| (bits<11, 11>(instruction) << 4)
| (bits< 2, 2>(instruction) << 5)
| (bits< 7, 7>(instruction) << 6)
| (bits< 6, 6>(instruction) << 7)
| (bits< 9, 10>(instruction) << 8)
| (bits< 8, 8>(instruction) << 10)
| (bits<12, 12>(instruction) << 11);
destination = location + sext<11>(immediate);
break;
}
case 6: // C.BEQZ
[[fallthrough]];
case 7: { // C.BNEZ
uintptr_t immediate = (bits< 3, 4>(instruction) << 1)
| (bits<10, 11>(instruction) << 3)
| (bits< 2, 2>(instruction) << 5)
| (bits< 5, 6>(instruction) << 6)
| (bits<12, 12>(instruction) << 8);
uint8_t rs = bits<7, 9>(instruction);
bool eq = bits<13, 15>(instruction) == 6;
destination = location +
((eq ? state.gp.regs[rs + 8] == 0 : state.gp.regs[rs + 8])
? sext<8>(immediate)
: 2);
break;
}
}
case 0x02:
switch (bits<12, 15>(instruction)) {
default: break;
case 8: // C.JR
[[fallthrough]];
case 9: { // C.JALR
if (bits<2, 6>(instruction)) // rs2 MBZ
break;
uint8_t rs1 = bits<7, 11>(instruction);
destination = state.gp.regs[rs1];
break;
}
}
}
}
CHK(process->readMemory(destination, &instruction, sizeof(instruction)));
CHK(manager->add(destination, BreakpointManager::Lifetime::TemporaryOneShot,
(instruction & 0x0003) == 0x0003 ? 4 : 2,
BreakpointManager::kModeExec));
return kSuccess;
}
} // namespace RISCV
} // namespace Architecture
} // namespace ds2