clean up runtime helpers

This commit is contained in:
nmzik
2026-09-17 19:50:21 +04:00
parent 218e7200bb
commit 1211958b4e
12 changed files with 74 additions and 143 deletions
+3 -6
View File
@@ -13,6 +13,7 @@
#include <algorithm>
#include <array>
#include <atomic>
#include <bit>
#include <cstddef>
#include <cstdlib>
#include <cstring>
@@ -3879,10 +3880,6 @@ static bool IsAligned(uint64_t value, uint64_t alignment) {
return alignment == 0 || (value & (alignment - 1u)) == 0;
}
static bool IsPowerOfTwo(uint64_t value) {
return value != 0 && (value & (value - 1u)) == 0;
}
static void MemoryPoolSubtractCommitted(uint64_t len) {
auto current = g_memory_pool_committed.load(std::memory_order_relaxed);
while (current != 0) {
@@ -3904,7 +3901,7 @@ int KYTY_SYSV_ABI KernelMemoryPoolExpand(int64_t search_start, int64_t search_en
if (search_start < 0 || search_end <= search_start || len == 0 ||
(len & (POOL_PAGE_SIZE - 1u)) != 0 || phys_addr_out == nullptr ||
(alignment != 0 &&
(!IsPowerOfTwo(alignment) || (alignment & (POOL_PAGE_SIZE - 1u)) != 0))) {
(!std::has_single_bit(alignment) || (alignment & (POOL_PAGE_SIZE - 1u)) != 0))) {
return KERNEL_ERROR_EINVAL;
}
if (static_cast<uint64_t>(search_end - search_start) < len) {
@@ -3953,7 +3950,7 @@ int KYTY_SYSV_ABI KernelMemoryPoolReserve(void* addr_in, size_t len, size_t alig
return KERNEL_ERROR_EINVAL;
}
if (alignment != 0 &&
(!IsPowerOfTwo(alignment) || !IsAligned(alignment, POOL_RESERVE_ALIGNMENT))) {
(!std::has_single_bit(alignment) || !IsAligned(alignment, POOL_RESERVE_ALIGNMENT))) {
return KERNEL_ERROR_EINVAL;
}
-7
View File
@@ -1202,13 +1202,6 @@ static int32_t GetDstSeconds() {
#endif
}
#if KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS
static void sec_to_timeval(KernelTimeval* ts, double sec) {
ts->tv_sec = static_cast<int64_t>(sec);
ts->tv_usec = static_cast<int64_t>((sec - static_cast<double>(ts->tv_sec)) * 1000000.0);
}
#endif
static bool GetPosixClockId(KernelClockid clock_id, clockid_t* out) {
EXIT_IF(out == nullptr);
+26 -29
View File
@@ -7,6 +7,7 @@
#include <algorithm>
#include <atomic>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <deque>
#include <mutex>
@@ -18,35 +19,31 @@
namespace Libs::Ime {
namespace {
constexpr int Error(uint32_t value) {
return static_cast<int32_t>(value);
}
constexpr int ERROR_BUSY = Error(0x80bc0001);
constexpr int ERROR_NOT_OPENED = Error(0x80bc0002);
constexpr int ERROR_CONNECTION_FAILED = Error(0x80bc0004);
constexpr int ERROR_EVENT_OVERFLOW = Error(0x80bc0007);
constexpr int ERROR_INVALID_TEXT = Error(0x80bc0006);
constexpr int ERROR_INVALID_USER_ID = Error(0x80bc0010);
constexpr int ERROR_INVALID_TYPE = Error(0x80bc0011);
constexpr int ERROR_INVALID_LANGUAGES = Error(0x80bc0012);
constexpr int ERROR_INVALID_ENTER_LABEL = Error(0x80bc0013);
constexpr int ERROR_INVALID_INPUT_METHOD = Error(0x80bc0014);
constexpr int ERROR_INVALID_OPTION = Error(0x80bc0015);
constexpr int ERROR_INVALID_MAX_TEXT_LENGTH = Error(0x80bc0016);
constexpr int ERROR_INVALID_TEXT_BUFFER = Error(0x80bc0017);
constexpr int ERROR_INVALID_POSX = Error(0x80bc0018);
constexpr int ERROR_INVALID_POSY = Error(0x80bc0019);
constexpr int ERROR_INVALID_HALIGN = Error(0x80bc001a);
constexpr int ERROR_INVALID_VALIGN = Error(0x80bc001b);
constexpr int ERROR_INVALID_EXTENDED = Error(0x80bc001c);
constexpr int ERROR_INVALID_WORK = Error(0x80bc0020);
constexpr int ERROR_INVALID_HANDLER = Error(0x80bc0022);
constexpr int ERROR_NO_RESOURCE_ID = Error(0x80bc0023);
constexpr int ERROR_INVALID_MODE = Error(0x80bc0024);
constexpr int ERROR_INVALID_PARAM = Error(0x80bc0030);
constexpr int ERROR_INVALID_ADDRESS = Error(0x80bc0031);
constexpr int ERROR_INVALID_RESERVED = Error(0x80bc0032);
constexpr int ERROR_BUSY = static_cast<int32_t>(0x80bc0001);
constexpr int ERROR_NOT_OPENED = static_cast<int32_t>(0x80bc0002);
constexpr int ERROR_CONNECTION_FAILED = static_cast<int32_t>(0x80bc0004);
constexpr int ERROR_EVENT_OVERFLOW = static_cast<int32_t>(0x80bc0007);
constexpr int ERROR_INVALID_TEXT = static_cast<int32_t>(0x80bc0006);
constexpr int ERROR_INVALID_USER_ID = static_cast<int32_t>(0x80bc0010);
constexpr int ERROR_INVALID_TYPE = static_cast<int32_t>(0x80bc0011);
constexpr int ERROR_INVALID_LANGUAGES = static_cast<int32_t>(0x80bc0012);
constexpr int ERROR_INVALID_ENTER_LABEL = static_cast<int32_t>(0x80bc0013);
constexpr int ERROR_INVALID_INPUT_METHOD = static_cast<int32_t>(0x80bc0014);
constexpr int ERROR_INVALID_OPTION = static_cast<int32_t>(0x80bc0015);
constexpr int ERROR_INVALID_MAX_TEXT_LENGTH = static_cast<int32_t>(0x80bc0016);
constexpr int ERROR_INVALID_TEXT_BUFFER = static_cast<int32_t>(0x80bc0017);
constexpr int ERROR_INVALID_POSX = static_cast<int32_t>(0x80bc0018);
constexpr int ERROR_INVALID_POSY = static_cast<int32_t>(0x80bc0019);
constexpr int ERROR_INVALID_HALIGN = static_cast<int32_t>(0x80bc001a);
constexpr int ERROR_INVALID_VALIGN = static_cast<int32_t>(0x80bc001b);
constexpr int ERROR_INVALID_EXTENDED = static_cast<int32_t>(0x80bc001c);
constexpr int ERROR_INVALID_WORK = static_cast<int32_t>(0x80bc0020);
constexpr int ERROR_INVALID_HANDLER = static_cast<int32_t>(0x80bc0022);
constexpr int ERROR_NO_RESOURCE_ID = static_cast<int32_t>(0x80bc0023);
constexpr int ERROR_INVALID_MODE = static_cast<int32_t>(0x80bc0024);
constexpr int ERROR_INVALID_PARAM = static_cast<int32_t>(0x80bc0030);
constexpr int ERROR_INVALID_ADDRESS = static_cast<int32_t>(0x80bc0031);
constexpr int ERROR_INVALID_RESERVED = static_cast<int32_t>(0x80bc0032);
constexpr uint32_t VALID_OPTIONS = 0x00007bff;
constexpr uint32_t VALID_EXTENDED_OPTIONS = 0x00004fde;
+22 -25
View File
@@ -5,6 +5,7 @@
#include <algorithm>
#include <atomic>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <mutex>
#include <vector>
@@ -13,31 +14,27 @@ namespace Libs::Dialog::ImeDialog {
namespace {
constexpr int Error(uint32_t value) {
return static_cast<int32_t>(value);
}
constexpr int ERROR_BUSY = Error(0x80bc0001);
constexpr int ERROR_INVALID_USER_ID = Error(0x80bc0010);
constexpr int ERROR_INVALID_TYPE = Error(0x80bc0011);
constexpr int ERROR_INVALID_LANGUAGES = Error(0x80bc0012);
constexpr int ERROR_INVALID_ENTER_LABEL = Error(0x80bc0013);
constexpr int ERROR_INVALID_INPUT_METHOD = Error(0x80bc0014);
constexpr int ERROR_INVALID_OPTION = Error(0x80bc0015);
constexpr int ERROR_INVALID_MAX_TEXT_LENGTH = Error(0x80bc0016);
constexpr int ERROR_INVALID_TEXT_BUFFER = Error(0x80bc0017);
constexpr int ERROR_INVALID_POSX = Error(0x80bc0018);
constexpr int ERROR_INVALID_POSY = Error(0x80bc0019);
constexpr int ERROR_INVALID_HALIGN = Error(0x80bc001a);
constexpr int ERROR_INVALID_VALIGN = Error(0x80bc001b);
constexpr int ERROR_INVALID_EXTENDED = Error(0x80bc001c);
constexpr int ERROR_INVALID_PARAM = Error(0x80bc0030);
constexpr int ERROR_INVALID_ADDRESS = Error(0x80bc0031);
constexpr int ERROR_INVALID_RESERVED = Error(0x80bc0032);
constexpr int ERROR_INVALID_TITLE = Error(0x80bc0101);
constexpr int ERROR_NOT_RUNNING = Error(0x80bc0105);
constexpr int ERROR_NOT_FINISHED = Error(0x80bc0106);
constexpr int ERROR_NOT_IN_USE = Error(0x80bc0107);
constexpr int ERROR_BUSY = static_cast<int32_t>(0x80bc0001);
constexpr int ERROR_INVALID_USER_ID = static_cast<int32_t>(0x80bc0010);
constexpr int ERROR_INVALID_TYPE = static_cast<int32_t>(0x80bc0011);
constexpr int ERROR_INVALID_LANGUAGES = static_cast<int32_t>(0x80bc0012);
constexpr int ERROR_INVALID_ENTER_LABEL = static_cast<int32_t>(0x80bc0013);
constexpr int ERROR_INVALID_INPUT_METHOD = static_cast<int32_t>(0x80bc0014);
constexpr int ERROR_INVALID_OPTION = static_cast<int32_t>(0x80bc0015);
constexpr int ERROR_INVALID_MAX_TEXT_LENGTH = static_cast<int32_t>(0x80bc0016);
constexpr int ERROR_INVALID_TEXT_BUFFER = static_cast<int32_t>(0x80bc0017);
constexpr int ERROR_INVALID_POSX = static_cast<int32_t>(0x80bc0018);
constexpr int ERROR_INVALID_POSY = static_cast<int32_t>(0x80bc0019);
constexpr int ERROR_INVALID_HALIGN = static_cast<int32_t>(0x80bc001a);
constexpr int ERROR_INVALID_VALIGN = static_cast<int32_t>(0x80bc001b);
constexpr int ERROR_INVALID_EXTENDED = static_cast<int32_t>(0x80bc001c);
constexpr int ERROR_INVALID_PARAM = static_cast<int32_t>(0x80bc0030);
constexpr int ERROR_INVALID_ADDRESS = static_cast<int32_t>(0x80bc0031);
constexpr int ERROR_INVALID_RESERVED = static_cast<int32_t>(0x80bc0032);
constexpr int ERROR_INVALID_TITLE = static_cast<int32_t>(0x80bc0101);
constexpr int ERROR_NOT_RUNNING = static_cast<int32_t>(0x80bc0105);
constexpr int ERROR_NOT_FINISHED = static_cast<int32_t>(0x80bc0106);
constexpr int ERROR_NOT_IN_USE = static_cast<int32_t>(0x80bc0107);
constexpr uint32_t VALID_OPTIONS = 0x00007bff;
constexpr uint32_t VALID_EXTENDED_OPTIONS = 0x00005fde;
+4 -7
View File
@@ -24,6 +24,7 @@
#include <algorithm>
#include <array>
#include <atomic>
#include <bit>
#include <chrono>
#include <cstddef>
#include <cstdio>
@@ -68,10 +69,6 @@ using KernelModule = int32_t;
using get_thread_atexit_count_func_t = KYTY_SYSV_ABI int (*)(KernelModule);
using thread_atexit_report_func_t = KYTY_SYSV_ABI void (*)(KernelModule);
static uint32_t sha1_rol(uint32_t value, uint32_t bits) {
return (value << bits) | (value >> (32u - bits));
}
static std::array<uint8_t, 20> sha1_digest(const uint8_t* data, size_t size) {
uint32_t h0 = 0x67452301u;
uint32_t h1 = 0xefcdab89u;
@@ -99,7 +96,7 @@ static std::array<uint8_t, 20> sha1_digest(const uint8_t* data, size_t size) {
(static_cast<uint32_t>(msg[j + 2]) << 8u) | static_cast<uint32_t>(msg[j + 3]);
}
for (int i = 16; i < 80; i++) {
w[i] = sha1_rol(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
w[i] = std::rotl(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
}
uint32_t a = h0;
@@ -125,10 +122,10 @@ static std::array<uint8_t, 20> sha1_digest(const uint8_t* data, size_t size) {
k = 0xca62c1d6u;
}
const auto temp = sha1_rol(a, 5) + f + e + k + w[i];
const auto temp = std::rotl(a, 5) + f + e + k + w[i];
e = d;
d = c;
c = sha1_rol(b, 30);
c = std::rotl(b, 30);
b = a;
a = temp;
}
-13
View File
@@ -3118,16 +3118,6 @@ static std::map<int64_t, NpWebApi2Request>& NpWebApi2Requests() {
return requests;
}
[[maybe_unused]] static std::string NpWebApi2MakeResponse(const NpWebApi2Request& request) {
if (request.api_group.find("sessionManager") != std::string::npos ||
request.path.find("sessions") != std::string::npos ||
request.path.find("Sessions") != std::string::npos) {
return R"({"gameSessions":[],"playerSessions":[]})";
}
return "{}";
}
static int KYTY_SYSV_ABI NpWebApi2Initialize(int lib_http_ctx_id, size_t pool_size) {
PRINT_NAME();
@@ -3224,12 +3214,10 @@ NpWebApi2SendRequest(int64_t request_id, const void* data, size_t data_size,
return NP_WEBAPI2_ERROR_REQUEST_NOT_FOUND;
}
// request->second.response = NpWebApi2MakeResponse(request->second);
request->second.response.clear();
request->second.read_offset = 0;
if (response_info_option != nullptr) {
// response_info_option->http_status = 200;
response_info_option->http_status = 0;
response_info_option->response_data_size = request->second.response.size();
if (response_info_option->error_object != nullptr &&
@@ -3238,7 +3226,6 @@ NpWebApi2SendRequest(int64_t request_id, const void* data, size_t data_size,
}
}
// return 0;
return NP_WEBAPI2_ERROR_NOT_SIGNED_IN;
}
+5 -9
View File
@@ -13,15 +13,11 @@ LIB_VERSION("Psml", 1, "Psml", 1, 1);
namespace Psml {
constexpr int Error(uint32_t value) {
return static_cast<int>(value);
}
constexpr int PSML_ERROR_NOT_INITIALIZED = Error(0x8a810001u);
constexpr int PSML_ERROR_INVALID_OBJECT = Error(0x8a810005u);
constexpr int PSML_ERROR_INVALID_POINTER = Error(0x8a810009u);
constexpr int PSML_ERROR_INVALID_VALUE = Error(0x8a81000du);
constexpr int PSML_ERROR_NULL_OBJECT = Error(0x8a810014u);
constexpr int PSML_ERROR_NOT_INITIALIZED = static_cast<int>(0x8a810001u);
constexpr int PSML_ERROR_INVALID_OBJECT = static_cast<int>(0x8a810005u);
constexpr int PSML_ERROR_INVALID_POINTER = static_cast<int>(0x8a810009u);
constexpr int PSML_ERROR_INVALID_VALUE = static_cast<int>(0x8a81000du);
constexpr int PSML_ERROR_NULL_OBJECT = static_cast<int>(0x8a810014u);
constexpr uint32_t SHARED_RESOURCES_MAGIC = 0xa9c4;
constexpr uint32_t CONTEXT_MAGIC = 0x9231;
-9
View File
@@ -3103,17 +3103,10 @@ static HostNetworkInfo QueryHostNetworkInfo() {
#endif
}
[[maybe_unused]] static bool HostNetworkConnected() {
return QueryHostNetworkInfo().connected;
}
static bool NetCtlConnected() {
if (!g_net_ctl_status_initialized.load()) {
// g_net_ctl_connected = HostNetworkConnected();
g_net_ctl_connected = false;
g_net_ctl_status_initialized = true;
// LOGF("\t host network connected = %s\n", (g_net_ctl_connected.load() ? "true" :
// "false"));
LOGF("\t host network connected = false (forced offline)\n");
}
@@ -3123,10 +3116,8 @@ static bool NetCtlConnected() {
int KYTY_SYSV_ABI NetCtlInit() {
PRINT_NAME();
// g_net_ctl_connected = HostNetworkConnected();
g_net_ctl_connected = false;
g_net_ctl_status_initialized = true;
// LOGF("\t host network connected = %s\n", (g_net_ctl_connected.load() ? "true" : "false"));
LOGF("\t host network connected = false (forced offline)\n");
return OK;
+3 -7
View File
@@ -103,11 +103,6 @@ static ZydisDecoder& GetDecoder() {
return decoder;
}
static ZyanStatus DecodeInstruction(ZydisDecodedInstruction& instruction,
ZydisDecodedOperand* operands, void* data, u64 size) {
return ZydisDecoderDecodeFull(&GetDecoder(), data, size, &instruction, operands);
}
#endif
#if defined(_WIN32)
@@ -177,8 +172,9 @@ uintptr_t GetRelativeTarget(const DecodedCodeInstruction& decoded) {
DecodedCodeInstruction DecodeCodeInstruction(uintptr_t address, uintptr_t end) {
DecodedCodeInstruction decoded {.address = address};
const auto status = DecodeInstruction(decoded.instruction, decoded.operands.data(),
reinterpret_cast<void*>(address), end - address);
const auto status = ZydisDecoderDecodeFull(&GetDecoder(), reinterpret_cast<void*>(address),
end - address, &decoded.instruction,
decoded.operands.data());
if (!ZYAN_SUCCESS(status)) {
decoded.instruction.length = 0;
return decoded;
-8
View File
@@ -20,12 +20,4 @@ Common::Time GetTime() {
return Common::Time(static_cast<int>(GetTimeMs()));
}
uint64_t GetCounter() {
return g_timer.GetTicks();
}
uint64_t GetFrequency() {
return g_timer.GetFrequency();
}
} // namespace Loader::Timer
-2
View File
@@ -9,8 +9,6 @@ namespace Loader::Timer {
void Start();
double GetTimeMs();
Common::Time GetTime();
uint64_t GetCounter();
uint64_t GetFrequency();
struct Lifecycle {
static constexpr const char* name = "Timer";
+11 -21
View File
@@ -70,16 +70,6 @@ struct XmmWords {
uint32_t w[4];
};
static uint32_t Rol32(uint32_t value, unsigned int shift) {
shift &= 31u;
return (value << shift) | (value >> (32u - shift));
}
static uint32_t Rotr32(uint32_t value, unsigned int shift) {
shift &= 31u;
return (value >> shift) | (value << (32u - shift));
}
static void Sha1Msg1(XmmWords& dest, const XmmWords& src2) {
const uint32_t w0 = dest.w[3];
const uint32_t w1 = dest.w[2];
@@ -97,10 +87,10 @@ static void Sha1Msg2(XmmWords& dest, const XmmWords& src2) {
const uint32_t w13 = src2.w[2];
const uint32_t w14 = src2.w[1];
const uint32_t w15 = src2.w[0];
const uint32_t w16 = Rol32(dest.w[3] ^ w13, 1u);
const uint32_t w17 = Rol32(dest.w[2] ^ w14, 1u);
const uint32_t w18 = Rol32(dest.w[1] ^ w15, 1u);
const uint32_t w19 = Rol32(dest.w[0] ^ w16, 1u);
const uint32_t w16 = std::rotl(dest.w[3] ^ w13, 1);
const uint32_t w17 = std::rotl(dest.w[2] ^ w14, 1);
const uint32_t w18 = std::rotl(dest.w[1] ^ w15, 1);
const uint32_t w19 = std::rotl(dest.w[0] ^ w16, 1);
dest.w[3] = w16;
dest.w[2] = w17;
dest.w[1] = w18;
@@ -108,7 +98,7 @@ static void Sha1Msg2(XmmWords& dest, const XmmWords& src2) {
}
static void Sha1Nexte(XmmWords& dest, const XmmWords& src2) {
const uint32_t tmp = Rol32(dest.w[3], 30u);
const uint32_t tmp = std::rotl(dest.w[3], 30);
dest.w[3] = src2.w[3] + tmp;
dest.w[2] = src2.w[2];
dest.w[1] = src2.w[1];
@@ -145,14 +135,14 @@ static void Sha1Rnds4(XmmWords& dest, const XmmWords& src2, uint8_t imm8) {
uint32_t e = 0;
for (unsigned int round = 0; round < 4u; round++) {
uint32_t term = Sha1RoundFunc(group, b, c, d) + Rol32(a, 5u) + w[round] + k;
uint32_t term = Sha1RoundFunc(group, b, c, d) + std::rotl(a, 5) + w[round] + k;
if (round > 0u) {
term += e;
}
const uint32_t a1 = term;
e = d;
d = c;
c = Rol32(b, 30u);
c = std::rotl(b, 30);
b = a;
a = a1;
}
@@ -164,19 +154,19 @@ static void Sha1Rnds4(XmmWords& dest, const XmmWords& src2, uint8_t imm8) {
}
static uint32_t Sha256Sigma0(uint32_t x) {
return Rotr32(x, 7u) ^ Rotr32(x, 18u) ^ (x >> 3u);
return std::rotr(x, 7) ^ std::rotr(x, 18) ^ (x >> 3u);
}
static uint32_t Sha256Sigma1(uint32_t x) {
return Rotr32(x, 17u) ^ Rotr32(x, 19u) ^ (x >> 10u);
return std::rotr(x, 17) ^ std::rotr(x, 19) ^ (x >> 10u);
}
static uint32_t Sha256Sum0(uint32_t x) {
return Rotr32(x, 2u) ^ Rotr32(x, 13u) ^ Rotr32(x, 22u);
return std::rotr(x, 2) ^ std::rotr(x, 13) ^ std::rotr(x, 22);
}
static uint32_t Sha256Sum1(uint32_t x) {
return Rotr32(x, 6u) ^ Rotr32(x, 11u) ^ Rotr32(x, 25u);
return std::rotr(x, 6) ^ std::rotr(x, 11) ^ std::rotr(x, 25);
}
static uint32_t Sha256Ch(uint32_t e, uint32_t f, uint32_t g) {