-
Notifications
You must be signed in to change notification settings - Fork 19
/
Ipc.h
258 lines (223 loc) · 6.34 KB
/
Ipc.h
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
#pragma once
#include "Ctu.h"
// Used to clarify IPC stubs
#define IN
#define OUT
#define buildInterface(cls, ...) make_shared<cls>(ctu, ##__VA_ARGS__)
string bufferToString(uint8_t *buf, uint size);
class IPipe : public Waitable {
public:
virtual ~IPipe() {}
virtual bool isAsync() = 0;
virtual uint32_t messageSync(shared_ptr<array<uint8_t, 0x100>> buf, bool& closeHandle) { return 0; }
virtual void messageAsync(shared_ptr<array<uint8_t, 0x100>> buf, function<void(uint32_t, bool closeHandle)> cb) {}
};
class IPort : public Waitable {
public:
virtual ~IPort() {}
virtual bool isAsync() = 0;
virtual shared_ptr<IPipe> connectSync() { return nullptr; }
virtual void connectAsync(function<void(shared_ptr<IPipe>)> cb) {}
};
class PipeEnd : public Waitable {
public:
PipeEnd() : closed(false) {}
void close() override {
if(closed)
return;
closed = true;
signal();
}
void push(shared_ptr<array<uint8_t, 0x100>> message) {
acquire();
messages.push(message);
release();
}
shared_ptr<array<uint8_t, 0x100>> pop() {
acquire();
if(closed) {
release();
return nullptr;
}
auto temp = messages.front();
messages.pop();
release();
return temp;
}
bool closed;
private:
queue<shared_ptr<array<uint8_t, 0x100>>> messages;
};
class NPipe : public IPipe {
public:
NPipe(Ctu *_ctu, string _name) : ctu(_ctu), name(_name), closed(false) {}
bool isAsync() override { return true; }
void messageAsync(shared_ptr<array<uint8_t, 0x100>> buf, function<void(uint32_t, bool closeHandle)> cb) override;
void close() override {
if(closed)
return;
closed = true;
server.close();
client.close();
signal();
}
string name;
PipeEnd server, client;
bool closed;
private:
Ctu *ctu;
};
class NPort : public IPort {
public:
NPort(Ctu *_ctu, string _name) : ctu(_ctu), name(_name) {}
~NPort() override {}
bool isAsync() override { return false; }
shared_ptr<IPipe> connectSync() override;
shared_ptr<NPipe> accept();
string name;
private:
Ctu *ctu;
queue<shared_ptr<NPipe>> available;
};
class IncomingIpcMessage {
public:
IncomingIpcMessage(uint8_t *_ptr, bool isDomainObject);
template<typename T>
T getData(uint offset) {
return *((T *) (ptr + sfciOffset + 8 + offset));
}
template<typename T>
T getDataPointer(uint offset) {
return (T) (ptr + sfciOffset + 8 + offset);
}
gptr getBuffer(int btype, int num, guint& size) {
if(btype & 0x20) {
auto buf = getBuffer((btype & ~0x20) | 4, num, size);
if(size != 0)
return buf;
return getBuffer((btype & ~0x20) | 8, num, size);
}
size = 0;
auto ax = (btype & 3) == 1;
auto flags_ = btype & 0xC0;
auto flags = flags_ == 0x80 ? 3 : (flags_ == 0x40 ? 1 : 0);
auto cx = (btype & 0xC) == 8;
switch((ax << 1) | cx) {
case 0: { // B
auto t = (uint32_t *) (ptr + descOffset + xCount * 8 + aCount * 12 + num * 12);
gptr a = t[0], b = t[1], c = t[2];
size = (guint) (a | (((c >> 24) & 0xF) << 32));
if((c & 0x3) != flags)
LOG_ERROR(Ipc, "B descriptor flags don't match: %u vs expected %u", (uint) (c & 0x3), flags);
return b | (((((c >> 2) << 4) & 0x70) | ((c >> 28) & 0xF)) << 32);
}
case 1: { // C
assert(num == 0);
auto t = (uint32_t *) (ptr + rawOffset + wlen * 4);
gptr a = t[0], b = t[1];
size = b >> 16;
return a | ((b & 0xFFFF) << 32);
}
case 2: { // A
auto t = (uint32_t *) (ptr + descOffset + xCount * 8 + num * 12);
gptr a = t[0], b = t[1], c = t[2];
size = (guint) (a | (((c >> 24) & 0xF) << 32));
if((c & 0x3) != flags)
LOG_ERROR(Ipc, "A descriptor flags don't match: %u vs expected %u", (uint) (c & 0x3), flags);
return b | (((((c >> 2) << 4) & 0x70) | ((c >> 28) & 0xF)) << 32);
}
case 3: { // X
auto t = (uint32_t *) (ptr + descOffset + num * 8);
gptr a = t[0], b = t[1];
size = (guint) (a >> 16);
return b | ((((a >> 12) & 0xF) | ((a >> 2) & 0x70)) << 32);
}
}
return 0;
}
ghandle getMoved(int off) {
return *(ghandle *) (ptr + moveOffset + off * 4);
}
ghandle getCopied(int off) {
return *(ghandle *) (ptr + copyOffset + off * 4);
}
uint cmdId, type;
uint aCount, bCount, cCount, hasC, xCount, hasPid, moveCount, copyCount;
gpid pid;
ghandle domainHandle;
uint domainCommand;
private:
uint wlen, rawOffset, sfciOffset, descOffset, copyOffset, moveOffset;
uint8_t *ptr;
};
class OutgoingIpcMessage {
public:
OutgoingIpcMessage(uint8_t *_ptr, bool _isDomainObject);
void initialize(uint _moveCount, uint _copyCount, uint dataBytes);
void commit();
template<typename T>
T getDataPointer(uint offset) {
return (T) (ptr + sfcoOffset + 8 + offset + (offset < 8 ? 0 : realDataOffset));
}
void copy(int offset, ghandle hnd) {
auto buf = (uint32_t *) ptr;
buf[3 + offset] = hnd;
}
void move(int offset, ghandle hnd) {
auto buf = (uint32_t *) ptr;
if(isDomainObject)
buf[(sfcoOffset >> 2) + 4 + offset] = hnd;
else
buf[3 + copyCount + offset] = hnd;
}
uint32_t errCode;
uint moveCount, copyCount;
bool isDomainObject;
private:
uint sfcoOffset;
uint realDataOffset;
uint8_t *ptr;
};
class IpcService : public IPipe {
public:
IpcService(Ctu *_ctu) : ctu(_ctu), domainOwner(nullptr), isDomainObject(false), domainHandleIter(0xf001), thisHandle(0xf000) {}
~IpcService() override {}
bool isAsync() override { return false; }
uint32_t messageSync(shared_ptr<array<uint8_t, 0x100>> buf, bool& closeHandle) override;
virtual uint32_t dispatch(IncomingIpcMessage &req, OutgoingIpcMessage &resp) { return 0xF601; }
protected:
Ctu *ctu;
IpcService *domainOwner;
bool isDomainObject;
int domainHandleIter, thisHandle;
unordered_map<uint32_t, shared_ptr<KObject>> domainHandles;
template<typename T>
ghandle createHandle(shared_ptr<T> obj) {
static_assert(std::is_base_of<KObject, T>::value, "T must derive from KObject");
if(domainOwner)
return domainOwner->createHandle(obj);
else if(isDomainObject) {
auto hnd = domainHandleIter++;
auto temp = dynamic_pointer_cast<IpcService>(obj);
if(temp != nullptr)
temp->domainOwner = this;
domainHandles[hnd] = dynamic_pointer_cast<KObject>(obj);
return hnd;
} else
return fauxNewHandle(dynamic_pointer_cast<KObject>(obj));
}
ghandle fauxNewHandle(shared_ptr<KObject> obj);
};
class IUnknown : public IpcService {
};
namespace nn::sm::detail {
class IUserInterface;
}
class Ipc {
public:
Ipc(Ctu *_ctu);
ghandle ConnectToPort(string name);
shared_ptr<nn::sm::detail::IUserInterface> sm;
private:
Ctu *ctu;
};