-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathThreadManager.cpp
297 lines (258 loc) · 5.85 KB
/
ThreadManager.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "Ctu.h"
#define INSN_PER_SLICE 1000000
Thread::Thread(Ctu *_ctu, int _id) : ctu(_ctu), id(_id), started(false) {
active = false;
memset(®s, 0, sizeof(ThreadRegisters));
auto tlsSize = 0x1000;
tlsBase = (1 << 24) + tlsSize * _id;
ctu->cpu.map(tlsBase, tlsSize);
}
void Thread::assignHandle(uint32_t _handle) {
handle = _handle;
ctu->cpu.guestptr<uint32_t>(tlsBase + 0x3B8) = _handle;
}
void Thread::terminate() {
signal();
ctu->tm.terminate(id);
auto tlsSize = 0x1000;
ctu->cpu.unmap(tlsBase, tlsSize);
}
void Thread::suspend(function<void()> cb) {
if(!active)
return;
active = false;
if(id == ctu->tm.current()->id) {
freeze();
if(cb != nullptr)
cb();
ctu->tm.next(true);
} else if(cb != nullptr)
cb();
}
void Thread::resume(function<void()> cb) {
if(active)
return;
if(cb != nullptr)
onWake(cb);
started = true;
active = true;
ctu->tm.enqueue(id);
}
void Thread::freeze() {
ctu->cpu.storeRegs(regs);
LOG_DEBUG(Thread, "Froze thread 0x%x with PC 0x" ADDRFMT, id, regs.PC);
}
void Thread::thaw() {
ctu->cpu.tlsBase(tlsBase);
if(wakeCallbacks.size()) {
for(auto cb : wakeCallbacks)
cb();
wakeCallbacks.clear();
}
ctu->cpu.loadRegs(regs);
LOG_DEBUG(Thread, "Thawed thread 0x%x with PC 0x" ADDRFMT, id, regs.PC);
}
void Thread::onWake(function<void()> cb) {
wakeCallbacks.push_back(cb);
}
NativeThread::NativeThread(Ctu *_ctu, function<void()> _runner, int _id) : ctu(_ctu), runner(_runner), id(_id), active(false) {
}
void NativeThread::terminate() {
ctu->tm.terminate(id);
}
void NativeThread::suspend() {
active = false;
}
void NativeThread::resume() {
if(active)
return;
active = true;
ctu->tm.enqueue(id);
}
void NativeThread::run() {
runner();
}
ThreadManager::ThreadManager(Ctu *_ctu) : ctu(_ctu) {
threadId = 0;
_current = nullptr;
_last = nullptr;
switched = false;
first = true;
wasNativeLast = false;
}
void ThreadManager::start() {
next();
while(true) {
if(ctu->gdbStub.enabled) {
ctu->gdbStub.handlePacket();
if(ctu->gdbStub.remoteBreak) {
ctu->gdbStub.remoteBreak = false;
if(_current != nullptr)
_current->freeze();
ctu->gdbStub.sendSignal(SIGTRAP);
}
if(ctu->gdbStub.haltLoop && !ctu->gdbStub.stepLoop)
continue;
auto wasStep = ctu->gdbStub.stepLoop;
if(_current == nullptr) {
ctu->gdbStub.haltLoop = false;
next();
ctu->gdbStub.haltLoop = ctu->gdbStub.haltLoop || wasStep;
continue;
}
ctu->cpu.exec(wasStep ? 1 : INSN_PER_SLICE);
if(wasStep) {
ctu->gdbStub.haltLoop = ctu->gdbStub.stepLoop = false;
if (_current != nullptr)
_current->freeze();
ctu->gdbStub._break();
}
} else {
next();
ctu->cpu.exec(INSN_PER_SLICE);
}
}
}
shared_ptr<Thread> ThreadManager::create(gptr pc, gptr sp) {
auto thread = make_shared<Thread>(ctu, ++threadId);
thread->assignHandle(ctu->newHandle(thread));
threads[thread->id] = thread;
thread->regs.PC = pc;
thread->regs.SP = sp;
thread->regs.X30 = 0;
return thread;
}
shared_ptr<NativeThread> ThreadManager::createNative(function<void()> runner) {
auto thread = make_shared<NativeThread>(ctu, runner, ++threadId);
nativeThreads[thread->id] = thread;
return thread;
}
void ThreadManager::requeue() {
if(_current == nullptr)
return;
_current->freeze();
running.push_back(_current);
_last = _current;
_current = nullptr;
}
void ThreadManager::enqueue(int id) {
if(isNative(id)) {
for(auto other : runningNative)
if(other->id == id)
return;
runningNative.push_back(nativeThreads[id]);
return;
}
if(threads.find(id) == threads.end())
return;
enqueue(threads[id]);
}
void ThreadManager::enqueue(shared_ptr<Thread> thread) {
for(auto other : running)
if(other->id == thread->id)
return;
running.push_back(thread);
}
void ThreadManager::tryRunNative() {
if(wasNativeLast)
wasNativeLast = false;
else {
while(runningNative.size() > 0) {
auto native = runningNative.front();
runningNative.pop_front();
if(!native->active)
continue;
native->run();
if(native->active)
runningNative.push_back(native);
break;
}
wasNativeLast = true;
}
}
void ThreadManager::next(bool force) {
tryRunNative();
if(force) {
if(_current != nullptr)
_last = _current;
_current = nullptr;
}
shared_ptr<Thread> nt = nullptr;
while(true) {
if(running.size() == 0) {
tryRunNative();
if(ctu->gdbStub.enabled) {
if(ctu->gdbStub.haltLoop) {
ctu->cpu.stop();
return;
}
ctu->gdbStub.handlePacket();
if(ctu->gdbStub.haltLoop) {
ctu->cpu.stop();
return;
}
}
if(_current == nullptr) {
if(first) {
LOG_DEBUG(Thread, "No threads left to run!");
ctu->bridge.start();
}
first = false;
} else
return;
usleep(50);
continue;
}
nt = running.front();
running.pop_front();
if(nt->active)
break;
}
switched = true;
if(_current != nullptr) {
_current->freeze();
if(_current->active)
enqueue(_current);
_last = _current;
}
nt->thaw();
_current = nt;
}
void ThreadManager::terminate(int id) {
if(threads.find(id) == threads.end()) {
if(isNative(id)) {
nativeThreads[id]->active = false;
nativeThreads.erase(id);
}
return;
}
threads[id]->active = false;
threads.erase(id);
next(true);
}
shared_ptr<Thread> ThreadManager::current() {
return _current;
}
shared_ptr<Thread> ThreadManager::last() {
return _last;
}
bool ThreadManager::setCurrent(int id) {
auto thread = threads.find(id);
if (thread == threads.end())
return false;
if(_current != nullptr) {
_current->freeze();
_last = _current;
}
_current = thread->second;
_current->thaw();
return true;
}
bool ThreadManager::isNative(int id) {
return nativeThreads.find(id) != nativeThreads.end();
}
vector<shared_ptr<Thread>> ThreadManager::thread_list() {
vector<shared_ptr<Thread>> vals;
transform(threads.begin(), threads.end(), back_inserter(vals), [](auto val){return val.second;} );
return vals;
}