Repository navigation
Expand file tree
/
Copy pathSystemConfiguration.cpp
More file actions
536 lines (515 loc) · 19.2 KB
/
Copy pathSystemConfiguration.cpp
File metadata and controls
536 lines (515 loc) · 19.2 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#include "prx/libc/include/GuestArena.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libkernel/DirectMemory/DirectMemory.hpp"
#include "prx/libkernel/KernelErrors.hpp"
#include "prx/libkernel/System/include/HostCpu.hpp"
#include "SceTypes.hpp"
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <random>
#include <stdexcept>
#include <string>
#include <vector>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <unistd.h>
#endif
extern "C" int* APS5_VABI __error_nid_postfix();
extern "C" int APS5_VABI getpid_nid_postfix();
extern "C" std::uint64_t APS5_VABI sceKernelGetTscFrequency(void);
struct GuestRlimit {
std::int64_t rlim_cur;
std::int64_t rlim_max;
};
extern "C" int APS5_VABI getrlimit_nid_postfix(int resource, GuestRlimit* limit);
struct SceKernelSystemSwVersion {
std::uint64_t size;
char versionString[0x1C];
std::uint32_t version;
};
extern "C" int APS5_VABI sceKernelVirtualQuery(const void* addr, int flags, VirtualQueryInfo* info, uint64_t info_size);
namespace {
int CopyOut(const void* value, std::size_t size, void* oldValue, std::size_t* oldLength) {
if (oldValue == nullptr) {
if (oldLength != nullptr) *oldLength = size;
return 0;
}
const std::size_t copied = std::min(oldLength != nullptr ? *oldLength : 0, size);
std::memcpy(oldValue, value, copied);
if (oldLength != nullptr) *oldLength = copied;
if (copied < size) {
*__error_nid_postfix() = 12;
return -1;
}
return 0;
}
struct SystemString {
int mib[2];
const char* name;
const char* value;
};
constexpr SystemString SystemStrings[] = {
{{1, 1}, "kern.ostype", "FreeBSD"},
{{1, 2}, "kern.osrelease", "11.4-RELEASE"},
{{1, 4}, "kern.version", "FreeBSD 11.4-RELEASE\n"},
{{1, 10}, "kern.hostname", "AnyPS5"},
{{6, 1}, "hw.machine", "amd64"},
};
struct SystemInteger {
int mib[2];
const char* name;
std::int32_t value;
};
constexpr SystemInteger SystemIntegers[] = {
{{1, 24}, "kern.osreldate", 1104001},
};
const SystemString* FindSystemString(const int* name) {
for (const auto& entry : SystemStrings) {
if (name[0] == entry.mib[0] && name[1] == entry.mib[1]) return &entry;
}
return nullptr;
}
const SystemInteger* FindSystemInteger(const int* name) {
for (const auto& entry : SystemIntegers) {
if (name[0] == entry.mib[0] && name[1] == entry.mib[1]) return &entry;
}
return nullptr;
}
bool IsLeafMib(const int* name) {
return FindSystemInteger(name) != nullptr || (name[0] == 6 && (name[1] == 2 || name[1] == 3 || name[1] == 5 || name[1] == 7)) || (name[0] == 1 && (name[1] == 37 || name[1] == 50));
}
bool IsMeasuredAbsentMib(const int* name, std::uint32_t nameLength) {
static constexpr struct {
int mib[3];
std::uint32_t length;
} absent[] = {
{{1, 2147482816, 2147482814}, 3},
{{1, 2147483151}, 2},
{{1, 2147483182, 2147483181}, 3},
{{1, 2147482838, 2147482834}, 3},
};
for (const auto& entry : absent) {
if (nameLength == entry.length && std::memcmp(name, entry.mib, entry.length * sizeof(int)) == 0) return true;
}
return false;
}
constexpr std::int32_t VmEntryDefaultType = 1;
struct GuestVmEntry {
std::int32_t structSize;
std::int32_t type;
std::uint64_t start;
std::uint64_t end;
std::uint64_t offset;
std::uint64_t vnodeFileId;
std::uint32_t vnodeFsid;
std::int32_t flags;
std::int32_t resident;
std::int32_t privateResident;
std::int32_t protection;
std::int32_t referenceCount;
std::int32_t shadowCount;
std::int32_t vnodeType;
std::uint64_t vnodeSize;
std::uint32_t vnodeDevice;
std::uint16_t vnodeMode;
std::uint16_t status;
std::int32_t spare[12];
char path[8];
};
static_assert(offsetof(GuestVmEntry, start) == 8 && offsetof(GuestVmEntry, protection) == 56 && offsetof(GuestVmEntry, path) == 136 && sizeof(GuestVmEntry) == 144);
std::vector<GuestVmEntry> GuestVmEntries() {
std::vector<GuestVmEntry> entries;
std::uintptr_t start = 0;
std::uintptr_t end = 0;
for (std::uintptr_t cursor = 0; GuestArena::GuestArenaNextUsed_nid_no_patch(cursor, &start, &end); cursor = end) {
GuestVmEntry entry{};
entry.structSize = sizeof(entry);
entry.type = VmEntryDefaultType;
entry.start = start;
entry.end = end;
VirtualQueryInfo info{};
if (sceKernelVirtualQuery(reinterpret_cast<const void*>(start), 0, &info, sizeof(info)) == 0) entry.protection = info.protection & 7;
entries.push_back(entry);
}
return entries;
}
bool IsUnansweredMib(const int* name, std::uint32_t nameLength) {
if (nameLength != 2) return false;
return (name[0] == 1 && (name[1] == 6 || name[1] == 7 || name[1] == 12 || name[1] == 27 || name[1] == 32)) || (name[0] == 6 && (name[1] == 4 || name[1] == 11));
}
bool IsUnansweredName(const char* name) {
static constexpr const char* unanswered[] = {
"hw.byteorder",
"hw.cacheline_size",
"hw.machine_arch",
"hw.usermem",
"kern.clockrate",
"kern.maxfiles",
"kern.maxfilesperproc",
"kern.maxproc",
"kern.ps_strings",
"kern.sched.name",
"kern.smp.maxcpus",
"kern.stackprot",
"kern.timecounter.hardware",
};
return std::any_of(std::begin(unanswered), std::end(unanswered), [name](const char* entry) { return std::strcmp(name, entry) == 0; });
}
}
extern "C" {
// Guest long is 64 bits, including when the Windows host long is 32 bits.
std::int64_t APS5_VABI sysconf_nid_postfix(int name) {
const int saved = *__error_nid_postfix();
std::int64_t result = -1;
switch (name) {
case 1: // _SC_ARG_MAX
result = 262144;
break;
case 3: // _SC_CLK_TCK
result = 128;
break;
case 5: { // _SC_OPEN_MAX
GuestRlimit limit{};
if (getrlimit_nid_postfix(8, &limit) == 0) {
result = limit.rlim_cur;
}
break;
}
case 47: // _SC_PAGESIZE
result = PS5_PAGE_SIZE;
break;
case 56: // _SC_IOV_MAX
result = 1024;
break;
case 57: // _SC_NPROCESSORS_CONF
case 58: // _SC_NPROCESSORS_ONLN
#ifdef _WIN32
result = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
#else
result = ::sysconf(name == 57 ? _SC_NPROCESSORS_CONF : _SC_NPROCESSORS_ONLN);
#endif
break;
case 121: { // _SC_PHYS_PAGES: report host physical capacity in guest pages
#ifdef _WIN32
MEMORYSTATUSEX memory{};
memory.dwLength = sizeof(memory);
if (GlobalMemoryStatusEx(&memory)) result = memory.ullTotalPhys / PS5_PAGE_SIZE;
#else
const auto pages = ::sysconf(_SC_PHYS_PAGES);
const auto pageSize = ::sysconf(_SC_PAGESIZE);
if (pages > 0 && pageSize > 0)
result = static_cast<std::uint64_t>(pages) * pageSize / PS5_PAGE_SIZE;
#endif
break;
}
default:
*__error_nid_postfix() = 22;
return -1;
}
if (result <= 0) { *__error_nid_postfix() = 5; return -1; }
*__error_nid_postfix() = saved;
return result;
}
int APS5_VABI sysctl_nid_postfix(const int* name, std::uint32_t nameLength, void* oldValue, std::size_t* oldLength, const void* newValue, std::size_t newLength) {
(void)newLength;
if (nameLength < 2 || nameLength > 24) {
*__error_nid_postfix() = 22;
return -1;
}
if (name == nullptr) {
*__error_nid_postfix() = 14;
return -1;
}
if (IsMeasuredAbsentMib(name, nameLength)) {
if (newValue != nullptr) throw std::runtime_error(std::string(__func__) + ": writing an unmeasured absent MIB");
*__error_nid_postfix() = 2;
return -1;
}
if (IsUnansweredMib(name, nameLength)) {
if (newValue != nullptr) throw std::runtime_error(std::string(__func__) + ": writing MIB " + std::to_string(name[0]) + "." + std::to_string(name[1]));
*__error_nid_postfix() = 2;
return -1;
}
if (nameLength == 2 && name[0] == 6 && name[1] == 7) {
if (newValue != nullptr) {
*__error_nid_postfix() = 1;
return -1;
}
constexpr int pagesize = PS5_PAGE_SIZE;
return CopyOut(&pagesize, sizeof(pagesize), oldValue, oldLength);
}
if (nameLength == 2 && name[0] == 6 && name[1] == 3) {
if (newValue != nullptr) {
*__error_nid_postfix() = 1;
return -1;
}
const std::int64_t processors = sysconf_nid_postfix(58);
if (processors <= 0) return -1;
const int value = static_cast<int>(processors);
return CopyOut(&value, sizeof(value), oldValue, oldLength);
}
if (nameLength == 2 && name[0] == 6 && name[1] == 2) {
if (newValue != nullptr) {
*__error_nid_postfix() = 1;
return -1;
}
static const std::string model = HostCpuModel();
if (model.empty()) throw std::runtime_error(std::string(__func__) + ": the host CPU has no CPUID brand string for hw.model");
return CopyOut(model.c_str(), model.size() + 1, oldValue, oldLength);
}
if (nameLength == 2 && name[0] == 6 && (name[1] == 5 || name[1] == 12)) {
if (newValue != nullptr) {
*__error_nid_postfix() = 1;
return -1;
}
const std::int64_t pages = sysconf_nid_postfix(121);
if (pages <= 0) return -1;
const std::uint64_t physmem = static_cast<std::uint64_t>(pages) * PS5_PAGE_SIZE;
return CopyOut(&physmem, sizeof(physmem), oldValue, oldLength);
}
if (nameLength == 2) {
if (const auto* entry = FindSystemString(name)) {
if (newValue != nullptr) {
*__error_nid_postfix() = 1;
return -1;
}
return CopyOut(entry->value, std::strlen(entry->value) + 1, oldValue, oldLength);
}
if (const auto* entry = FindSystemInteger(name)) {
if (newValue != nullptr) {
*__error_nid_postfix() = 1;
return -1;
}
return CopyOut(&entry->value, sizeof(entry->value), oldValue, oldLength);
}
}
if (nameLength >= 3 && name[0] == 1 && name[1] == 14 && name[2] == 12) {
if (newValue != nullptr) {
*__error_nid_postfix() = 1;
return -1;
}
if (nameLength != 4) {
*__error_nid_postfix() = 22;
return -1;
}
const int pid = name[3];
if (pid != -1 && pid != getpid_nid_postfix()) {
*__error_nid_postfix() = 3;
return -1;
}
static constexpr char path[] = "/app0/eboot.bin";
return CopyOut(path, sizeof(path), oldValue, oldLength);
}
if (nameLength >= 3 && name[0] == 1 && name[1] == 14 && name[2] == 35) {
if (newValue != nullptr) {
*__error_nid_postfix() = 1;
return -1;
}
if (nameLength != 4) {
*__error_nid_postfix() = 22;
return -1;
}
if (name[3] != getpid_nid_postfix()) {
*__error_nid_postfix() = 3;
return -1;
}
return CopyOut(nullptr, 0, oldValue, oldLength);
}
if (nameLength >= 3 && name[0] == 1 && name[1] == 14 && name[2] == 32) {
if (newValue != nullptr) {
*__error_nid_postfix() = 1;
return -1;
}
if (nameLength != 4) {
*__error_nid_postfix() = 22;
return -1;
}
if (name[3] != getpid_nid_postfix()) {
*__error_nid_postfix() = 3;
return -1;
}
const auto entries = GuestVmEntries();
return CopyOut(entries.data(), entries.size() * sizeof(GuestVmEntry), oldValue, oldLength);
}
if (nameLength == 2 && name[0] == 1 && name[1] == 37) {
if (newValue != nullptr) {
*__error_nid_postfix() = 1;
return -1;
}
if (oldLength == nullptr) return 0;
const std::size_t count = std::min(*oldLength, static_cast<std::size_t>(256));
if (oldValue != nullptr) {
static std::random_device device;
auto* bytes = static_cast<std::uint8_t*>(oldValue);
for (std::size_t i = 0; i < count; ++i) bytes[i] = static_cast<std::uint8_t>(device());
}
*oldLength = count;
return 0;
}
if (nameLength == 2 && name[0] == 1 && name[1] == 50) {
if (newValue != nullptr) throw std::runtime_error(std::string(__func__) + ": writing kern.rng_pseudo is unverified");
std::array<std::uint8_t, 64> bytes{};
if (oldValue != nullptr && oldLength != nullptr) {
static std::random_device device;
for (auto& byte : bytes) byte = static_cast<std::uint8_t>(device());
}
return CopyOut(bytes.data(), bytes.size(), oldValue, oldLength);
}
if (nameLength > 2 && (IsLeafMib(name) || FindSystemString(name) != nullptr)) {
*__error_nid_postfix() = 20;
return -1;
}
std::string mib;
for (std::uint32_t i = 0; i < nameLength; ++i) mib += (i == 0 ? "" : ".") + std::to_string(name[i]);
throw std::runtime_error(std::string(__func__) + ": unsupported MIB " + mib);
}
int APS5_VABI sysctlbyname_nid_postfix(const char* name, void* oldValue, std::size_t* oldLength, const void* newValue, std::size_t newLength) {
if (name == nullptr) {
*__error_nid_postfix() = 14;
return -1;
}
if (std::strcmp(name, "machdep.cacheline_size") == 0) {
*__error_nid_postfix() = 2;
return -1;
}
for (const auto& entry : SystemStrings) {
if (std::strcmp(name, entry.name) == 0) return sysctl_nid_postfix(entry.mib, 2, oldValue, oldLength, newValue, newLength);
}
for (const auto& entry : SystemIntegers) {
if (std::strcmp(name, entry.name) == 0) return sysctl_nid_postfix(entry.mib, 2, oldValue, oldLength, newValue, newLength);
}
if (IsUnansweredName(name)) {
if (newValue != nullptr) throw std::runtime_error(std::string(__func__) + ": writing " + name);
*__error_nid_postfix() = 2;
return -1;
}
if (std::strcmp(name, "kern.proc.pathname") == 0) {
static constexpr int mib[] = {1, 14, 12};
return sysctl_nid_postfix(mib, 3, oldValue, oldLength, newValue, newLength);
}
if (std::strcmp(name, "kern.arandom") == 0) {
static constexpr int mib[] = {1, 37};
return sysctl_nid_postfix(mib, 2, oldValue, oldLength, newValue, newLength);
}
if (std::strcmp(name, "kern.rng_pseudo") == 0) {
if (newValue != nullptr) {
*__error_nid_postfix() = 1;
return -1;
}
static std::random_device device;
std::uint8_t block[64]{};
if (oldValue != nullptr && oldLength != nullptr)
for (auto& byte : block) byte = static_cast<std::uint8_t>(device());
return CopyOut(block, sizeof(block), oldValue, oldLength);
}
if (std::strcmp(name, "vm.overcommit") == 0) {
if (newValue != nullptr) throw std::runtime_error(std::string(__func__) + ": setting vm.overcommit");
static constexpr int overcommit = 0;
return CopyOut(&overcommit, sizeof(overcommit), oldValue, oldLength);
}
if (std::strcmp(name, "kern.sdk_version") == 0) {
if (newValue != nullptr) {
*__error_nid_postfix() = 1;
return -1;
}
static constexpr std::uint32_t sdkVersion = 0x01000000;
return CopyOut(&sdkVersion, sizeof(sdkVersion), oldValue, oldLength);
}
if (std::strcmp(name, "machdep.tsc_freq") == 0) {
if (newValue != nullptr) {
*__error_nid_postfix() = 1;
return -1;
}
const std::uint64_t frequency = sceKernelGetTscFrequency();
return CopyOut(&frequency, sizeof(frequency), oldValue, oldLength);
}
if (std::strcmp(name, "hw.pagesize") == 0) {
static constexpr int mib[] = {6, 7};
return sysctl_nid_postfix(mib, 2, oldValue, oldLength, newValue, newLength);
}
if (std::strcmp(name, "hw.ncpu") == 0) {
static constexpr int mib[] = {6, 3};
return sysctl_nid_postfix(mib, 2, oldValue, oldLength, newValue, newLength);
}
if (std::strcmp(name, "hw.model") == 0) {
static constexpr int mib[] = {6, 2};
return sysctl_nid_postfix(mib, 2, oldValue, oldLength, newValue, newLength);
}
if (std::strcmp(name, "hw.physmem") == 0) {
static constexpr int mib[] = {6, 5};
return sysctl_nid_postfix(mib, 2, oldValue, oldLength, newValue, newLength);
}
if (std::strcmp(name, "hw.realmem") == 0) {
static constexpr int mib[] = {6, 12};
return sysctl_nid_postfix(mib, 2, oldValue, oldLength, newValue, newLength);
}
*__error_nid_postfix() = 2;
return -1;
}
int APS5_VABI sceKernelGetSystemSwVersion_nid_postfix(SceKernelSystemSwVersion* version) {
if (version == nullptr) {
return SCE_KERNEL_ERROR_EFAULT;
}
std::memset(version->versionString, 0, sizeof(version->versionString));
std::memcpy(version->versionString, "01.000.000", sizeof("01.000.000"));
version->version = 0x01000000;
return 0;
}
int APS5_VABI sysctlnametomib_nid_postfix(const char* name, int* mib, std::size_t* size) {
struct KnownName {
const char* name;
int mib[3];
std::size_t length;
};
static constexpr KnownName known[] = {
{"hw.byteorder", {6, 4}, 2},
{"hw.machine_arch", {6, 11}, 2},
{"hw.model", {6, 2}, 2},
{"hw.ncpu", {6, 3}, 2},
{"hw.pagesize", {6, 7}, 2},
{"hw.physmem", {6, 5}, 2},
{"hw.realmem", {6, 12}, 2},
{"kern.arandom", {1, 37}, 2},
{"kern.clockrate", {1, 12}, 2},
{"kern.maxfiles", {1, 7}, 2},
{"kern.maxfilesperproc", {1, 27}, 2},
{"kern.maxproc", {1, 6}, 2},
{"kern.proc.pathname", {1, 14, 12}, 3},
{"kern.ps_strings", {1, 32}, 2},
{"kern.rng_pseudo", {1, 50}, 2},
};
constexpr std::size_t MaximumPath = 1024;
if (name == nullptr || size == nullptr) throw std::invalid_argument(std::string(__func__) + ": null name or size");
const std::size_t nameLength = strnlen(name, MaximumPath);
if (nameLength == MaximumPath) {
*__error_nid_postfix() = 63;
return -1;
}
if (nameLength == 0 || std::strcmp(name, "hw.cacheline_size") == 0 || std::strcmp(name, "hw.usermem") == 0 || std::strcmp(name, "machdep.cacheline_size") == 0) {
*__error_nid_postfix() = 2;
return -1;
}
const auto copyMib = [&](const int* found, std::size_t length) {
std::size_t bytes = *size * sizeof(int);
const int result = CopyOut(found, length * sizeof(int), mib, &bytes);
*size = bytes / sizeof(int);
return result;
};
for (const auto& entry : known) {
if (std::strcmp(name, entry.name) == 0) return copyMib(entry.mib, entry.length);
}
for (const auto& entry : SystemStrings) {
if (std::strcmp(name, entry.name) == 0) return copyMib(entry.mib, 2);
}
for (const auto& entry : SystemIntegers) {
if (std::strcmp(name, entry.name) == 0) return copyMib(entry.mib, 2);
}
*__error_nid_postfix() = 2;
return -1;
}
}