mirror of
https://github.com/Atmosphere-NX/Atmosphere-libs.git
synced 2025-06-29 22:42:40 +02:00
pm/cfg: simplify initial process id range logic
This commit is contained in:
parent
28756234d9
commit
4390df76ad
@ -19,66 +19,42 @@ namespace ams::cfg {
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
/* Convenience definitions. */
|
|
||||||
constexpr os::ProcessId InitialProcessIdMinDeprecated = {0x00};
|
|
||||||
constexpr os::ProcessId InitialProcessIdMaxDeprecated = {0x50};
|
|
||||||
|
|
||||||
/* Privileged process globals. */
|
|
||||||
constinit os::SdkMutex g_lock;
|
constinit os::SdkMutex g_lock;
|
||||||
constinit bool g_got_privileged_process_status = false;
|
constinit bool g_got_privileged_process_status = false;
|
||||||
constinit os::ProcessId g_min_initial_process_id = os::InvalidProcessId, g_max_initial_process_id = os::InvalidProcessId;
|
constinit os::ProcessId g_min_initial_process_id = os::InvalidProcessId, g_max_initial_process_id = os::InvalidProcessId;
|
||||||
constinit os::ProcessId g_cur_process_id = os::InvalidProcessId;
|
constinit os::ProcessId g_cur_process_id = os::InvalidProcessId;
|
||||||
|
|
||||||
/* SD card helpers. */
|
ALWAYS_INLINE void EnsurePrivilegedProcessStatusCached() {
|
||||||
void GetPrivilegedProcessIdRange(os::ProcessId *out_min, os::ProcessId *out_max) {
|
if (AMS_LIKELY(g_got_privileged_process_status)) {
|
||||||
os::ProcessId min = os::InvalidProcessId, max = os::InvalidProcessId;
|
return;
|
||||||
if (hos::GetVersion() >= hos::Version_5_0_0) {
|
|
||||||
/* On 5.0.0+, we can get precise limits from svcGetSystemInfo. */
|
|
||||||
R_ABORT_UNLESS(svcGetSystemInfo(reinterpret_cast<u64 *>(&min), SystemInfoType_InitialProcessIdRange, INVALID_HANDLE, InitialProcessIdRangeInfo_Minimum));
|
|
||||||
R_ABORT_UNLESS(svcGetSystemInfo(reinterpret_cast<u64 *>(&max), SystemInfoType_InitialProcessIdRange, INVALID_HANDLE, InitialProcessIdRangeInfo_Maximum));
|
|
||||||
} else if (hos::GetVersion() >= hos::Version_4_0_0) {
|
|
||||||
/* On 4.0.0-4.1.0, we can get the precise limits from normal svcGetInfo. */
|
|
||||||
R_ABORT_UNLESS(svcGetInfo(reinterpret_cast<u64 *>(&min), InfoType_InitialProcessIdRange, INVALID_HANDLE, InitialProcessIdRangeInfo_Minimum));
|
|
||||||
R_ABORT_UNLESS(svcGetInfo(reinterpret_cast<u64 *>(&max), InfoType_InitialProcessIdRange, INVALID_HANDLE, InitialProcessIdRangeInfo_Maximum));
|
|
||||||
} else {
|
|
||||||
/* On < 4.0.0, we just use hardcoded extents. */
|
|
||||||
min = InitialProcessIdMinDeprecated;
|
|
||||||
max = InitialProcessIdMaxDeprecated;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*out_min = min;
|
|
||||||
*out_max = max;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GetPrivilegedProcessStatus() {
|
|
||||||
GetPrivilegedProcessIdRange(&g_min_initial_process_id, &g_max_initial_process_id);
|
|
||||||
g_cur_process_id = os::GetCurrentProcessId();
|
|
||||||
g_got_privileged_process_status = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Privileged Process utilities. */
|
|
||||||
bool IsInitialProcess() {
|
|
||||||
std::scoped_lock lk(g_lock);
|
std::scoped_lock lk(g_lock);
|
||||||
|
|
||||||
/* If we've not detected, do detection. */
|
if (AMS_LIKELY(!g_got_privileged_process_status)) {
|
||||||
if (!g_got_privileged_process_status) {
|
R_ABORT_UNLESS(svc::GetSystemInfo(std::addressof(g_min_initial_process_id.value), svc::SystemInfoType_InitialProcessIdRange, svc::InvalidHandle, svc::InitialProcessIdRangeInfo_Minimum));
|
||||||
GetPrivilegedProcessStatus();
|
R_ABORT_UNLESS(svc::GetSystemInfo(std::addressof(g_max_initial_process_id.value), svc::SystemInfoType_InitialProcessIdRange, svc::InvalidHandle, svc::InitialProcessIdRangeInfo_Maximum));
|
||||||
|
g_cur_process_id = os::GetCurrentProcessId();
|
||||||
|
|
||||||
|
g_got_privileged_process_status = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Determine if we're privileged, and return. */
|
}
|
||||||
|
|
||||||
|
bool IsInitialProcess() {
|
||||||
|
/* Cache initial process range and extents. */
|
||||||
|
EnsurePrivilegedProcessStatusCached();
|
||||||
|
|
||||||
|
/* Determine if we're Initial. */
|
||||||
return g_min_initial_process_id <= g_cur_process_id && g_cur_process_id <= g_max_initial_process_id;
|
return g_min_initial_process_id <= g_cur_process_id && g_cur_process_id <= g_max_initial_process_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetInitialProcessRange(os::ProcessId *out_min, os::ProcessId *out_max) {
|
void GetInitialProcessRange(os::ProcessId *out_min, os::ProcessId *out_max) {
|
||||||
std::scoped_lock lk(g_lock);
|
/* Cache initial process range and extents. */
|
||||||
|
EnsurePrivilegedProcessStatusCached();
|
||||||
/* If we've not detected, do detection. */
|
|
||||||
if (!g_got_privileged_process_status) {
|
|
||||||
GetPrivilegedProcessStatus();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/* Set output. */
|
||||||
*out_min = g_min_initial_process_id;
|
*out_min = g_min_initial_process_id;
|
||||||
*out_max = g_max_initial_process_id;
|
*out_max = g_max_initial_process_id;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user