os: refactor/rewrite entire namespace.

This commit is contained in:
Michael Scire
2020-04-08 02:21:35 -07:00
parent 6193283f03
commit 065485b971
181 changed files with 5353 additions and 1929 deletions

View File

@@ -20,7 +20,7 @@ namespace ams::mitm {
namespace {
os::Mutex g_throw_lock;
os::Mutex g_throw_lock(false);
bool g_threw;
Result g_throw_result;
@@ -28,12 +28,14 @@ namespace ams::mitm {
void DebugThrowThreadFunc(void *arg);
constexpr size_t DebugThrowThreadStackSize = 0x4000;
constexpr int DebugThrowThreadPriority = 49;
os::StaticThread<DebugThrowThreadStackSize> g_debug_throw_thread(&DebugThrowThreadFunc, nullptr, DebugThrowThreadPriority);
constexpr int DebugThrowThreadPriority = 21;
os::ThreadType g_debug_throw_thread;
alignas(os::ThreadStackAlignment) u8 g_debug_throw_thread_stack[DebugThrowThreadStackSize];
void DebugThrowThreadFunc(void *arg) {
/* TODO: Better heuristic for fatal startup than sleep. */
svcSleepThread(10'000'000'000ul);
os::SleepThread(TimeSpan::FromSeconds(10));
fatalThrow(g_throw_result.GetValue());
}
@@ -48,7 +50,8 @@ namespace ams::mitm {
g_throw_result = res;
g_threw = true;
R_ABORT_UNLESS(g_debug_throw_thread.Start());
R_ABORT_UNLESS(os::CreateThread(std::addressof(g_debug_throw_thread), DebugThrowThreadFunc, nullptr, g_debug_throw_thread_stack, sizeof(g_debug_throw_thread_stack), DebugThrowThreadPriority));
os::StartThread(std::addressof(g_debug_throw_thread));
}
}

View File

@@ -50,11 +50,13 @@ namespace ams::mitm {
void InitializeThreadFunc(void *arg);
constexpr size_t InitializeThreadStackSize = 0x4000;
constexpr int InitializeThreadPriority = 0x15;
os::StaticThread<InitializeThreadStackSize> g_initialize_thread(&InitializeThreadFunc, nullptr, InitializeThreadPriority);
constexpr int InitializeThreadPriority = -7;
/* Globals. */
os::Event g_init_event(false);
os::Event g_init_event(os::EventClearMode_ManualClear);
os::ThreadType g_initialize_thread;
alignas(os::ThreadStackAlignment) u8 g_initialize_thread_stack[InitializeThreadStackSize];
/* Console-unique data backup and protection. */
constexpr size_t CalibrationBinarySize = 0x8000;
@@ -221,7 +223,8 @@ namespace ams::mitm {
}
void StartInitialize() {
R_ABORT_UNLESS(g_initialize_thread.Start());
R_ABORT_UNLESS(os::CreateThread(std::addressof(g_initialize_thread), InitializeThreadFunc, nullptr, g_initialize_thread_stack, sizeof(g_initialize_thread_stack), InitializeThreadPriority));
os::StartThread(std::addressof(g_initialize_thread));
}
bool IsInitialized() {

View File

@@ -22,12 +22,12 @@ namespace ams::mitm {
class ModuleBase {};
#define DEFINE_MITM_MODULE_CLASS(ss, prio) class MitmModule : public ::ams::mitm::ModuleBase { \
public: \
static constexpr size_t ThreadPriority = prio; \
static constexpr size_t StackSize = ss; \
alignas(os::MemoryPageSize) static inline u8 Stack[StackSize]; \
public: \
static void ThreadFunction(void *); \
public: \
static constexpr s32 ThreadPriority = prio; \
static constexpr size_t StackSize = ss; \
alignas(os::ThreadStackAlignment) static inline u8 Stack[StackSize]; \
public: \
static void ThreadFunction(void *); \
}
template<class M>
@@ -37,7 +37,7 @@ namespace ams::mitm {
static constexpr void *Stack = &M::Stack[0];
static constexpr size_t StackSize = M::StackSize;
static constexpr size_t ThreadPriority = M::ThreadPriority;
static constexpr s32 ThreadPriority = M::ThreadPriority;
static constexpr ::ThreadFunc ThreadFunction = &M::ThreadFunction;
};

View File

@@ -40,7 +40,7 @@ namespace ams::mitm {
struct ModuleDefinition {
ThreadFunc main;
void *stack_mem;
u32 priority;
s32 priority;
u32 stack_size;
};
@@ -56,7 +56,7 @@ namespace ams::mitm {
};
}
ams::os::Thread g_module_threads[ModuleId_Count];
ams::os::ThreadType g_module_threads[ModuleId_Count];
constexpr ModuleDefinition g_module_definitions[ModuleId_Count] = {
GetModuleDefinition<fs::MitmModule>(),
@@ -72,19 +72,19 @@ namespace ams::mitm {
/* Create thread for each module. */
for (u32 i = 0; i < static_cast<u32>(ModuleId_Count); i++) {
const ModuleDefinition &cur_module = g_module_definitions[i];
R_ABORT_UNLESS(g_module_threads[i].Initialize(cur_module.main, nullptr, cur_module.stack_mem, cur_module.stack_size, cur_module.priority));
R_ABORT_UNLESS(os::CreateThread(g_module_threads + i, cur_module.main, nullptr, cur_module.stack_mem, cur_module.stack_size, cur_module.priority));
}
/* Start thread for each module. */
for (u32 i = 0; i < static_cast<u32>(ModuleId_Count); i++) {
R_ABORT_UNLESS(g_module_threads[i].Start());
os::StartThread(g_module_threads + i);
}
}
void WaitAllModules() {
/* Wait on thread for each module. */
for (u32 i = 0; i < static_cast<u32>(ModuleId_Count); i++) {
g_module_threads[i].Join();
os::WaitThread(g_module_threads + i);
}
}

View File

@@ -19,6 +19,6 @@
namespace ams::mitm::bpc {
DEFINE_MITM_MODULE_CLASS(0x8000, 32);
DEFINE_MITM_MODULE_CLASS(0x8000, 4);
}

View File

@@ -30,8 +30,8 @@ namespace ams::mitm::fs {
constexpr const char AtmosphereHblWebContentDir[] = "/atmosphere/hbl_html/";
constexpr const char ProgramWebContentDir[] = "/manual_html/";
os::Mutex g_data_storage_lock;
os::Mutex g_storage_cache_lock;
os::Mutex g_data_storage_lock(false);
os::Mutex g_storage_cache_lock(false);
std::unordered_map<u64, std::weak_ptr<IStorageInterface>> g_storage_cache;
std::shared_ptr<IStorageInterface> GetStorageCacheEntry(ncm::ProgramId program_id) {

View File

@@ -21,7 +21,7 @@ namespace ams::mitm::fs {
namespace {
os::Mutex g_boot0_access_mutex;
os::Mutex g_boot0_access_mutex(false);
u8 g_boot0_bct_buffer[Boot0Storage::BctEndOffset];
}

View File

@@ -22,10 +22,11 @@ namespace ams::mitm::fs {
namespace {
os::Mutex g_mq_lock;
os::Mutex g_mq_lock(false);
bool g_started_req_thread;
os::MessageQueue g_req_mq(1);
os::MessageQueue g_ack_mq(1);
uintptr_t g_mq_storage[2];
os::MessageQueue g_req_mq(g_mq_storage + 0, 1);
os::MessageQueue g_ack_mq(g_mq_storage + 1, 1);
void RomfsInitializerThreadFunction(void *arg) {
while (true) {
@@ -38,14 +39,16 @@ namespace ams::mitm::fs {
}
constexpr size_t RomfsInitializerThreadStackSize = 0x8000;
constexpr int RomfsInitializerThreadPriority = 44;
os::StaticThread<RomfsInitializerThreadStackSize> g_romfs_initializer_thread(&RomfsInitializerThreadFunction, nullptr, RomfsInitializerThreadPriority);
constexpr int RomfsInitializerThreadPriority = 16;
os::ThreadType g_romfs_initializer_thread;
alignas(os::ThreadStackAlignment) u8 g_romfs_initializer_thread_stack[RomfsInitializerThreadStackSize];
void RequestInitializeStorage(uintptr_t storage_uptr) {
std::scoped_lock lk(g_mq_lock);
if (!g_started_req_thread) {
R_ABORT_UNLESS(g_romfs_initializer_thread.Start());
if (AMS_UNLIKELY(!g_started_req_thread)) {
R_ABORT_UNLESS(os::CreateThread(std::addressof(g_romfs_initializer_thread), RomfsInitializerThreadFunction, nullptr, g_romfs_initializer_thread_stack, sizeof(g_romfs_initializer_thread_stack), RomfsInitializerThreadPriority));
os::StartThread(std::addressof(g_romfs_initializer_thread));
g_started_req_thread = true;
}
@@ -59,7 +62,7 @@ namespace ams::mitm::fs {
using namespace ams::fs;
LayeredRomfsStorage::LayeredRomfsStorage(std::unique_ptr<IStorage> s_r, std::unique_ptr<IStorage> f_r, ncm::ProgramId pr_id) : storage_romfs(std::move(s_r)), file_romfs(std::move(f_r)), initialize_event(false, false), program_id(std::move(pr_id)), is_initialized(false), started_initialize(false) {
LayeredRomfsStorage::LayeredRomfsStorage(std::unique_ptr<IStorage> s_r, std::unique_ptr<IStorage> f_r, ncm::ProgramId pr_id) : storage_romfs(std::move(s_r)), file_romfs(std::move(f_r)), initialize_event(os::EventClearMode_ManualClear), program_id(std::move(pr_id)), is_initialized(false), started_initialize(false) {
/* ... */
}

View File

@@ -39,7 +39,7 @@ namespace ams::mitm::fs {
constexpr size_t ThreadStackSize = mitm::ModuleTraits<fs::MitmModule>::StackSize;
alignas(os::MemoryPageSize) u8 g_extra_thread_stacks[NumExtraThreads][ThreadStackSize];
os::Thread g_extra_threads[NumExtraThreads];
os::ThreadType g_extra_threads[NumExtraThreads];
void LoopServerThread(void *arg) {
/* Loop forever, servicing our services. */
@@ -49,16 +49,16 @@ namespace ams::mitm::fs {
void ProcessForServerOnAllThreads() {
/* Initialize threads. */
if constexpr (NumExtraThreads > 0) {
const s32 priority = os::GetCurrentThreadPriority();
const s32 priority = os::GetThreadCurrentPriority(os::GetCurrentThread());
for (size_t i = 0; i < NumExtraThreads; i++) {
R_ABORT_UNLESS(g_extra_threads[i].Initialize(LoopServerThread, nullptr, g_extra_thread_stacks[i], ThreadStackSize, priority));
R_ABORT_UNLESS(os::CreateThread(g_extra_threads + i, LoopServerThread, nullptr, g_extra_thread_stacks[i], ThreadStackSize, priority));
}
}
/* Start extra threads. */
if constexpr (NumExtraThreads > 0) {
for (size_t i = 0; i < NumExtraThreads; i++) {
R_ABORT_UNLESS(g_extra_threads[i].Start());
os::StartThread(g_extra_threads + i);
}
}
@@ -68,7 +68,7 @@ namespace ams::mitm::fs {
/* Wait for extra threads to finish. */
if constexpr (NumExtraThreads > 0) {
for (size_t i = 0; i < NumExtraThreads; i++) {
R_ABORT_UNLESS(g_extra_threads[i].Join());
os::WaitThread(g_extra_threads + i);
}
}
}

View File

@@ -19,6 +19,6 @@
namespace ams::mitm::fs {
DEFINE_MITM_MODULE_CLASS(0x8000, 43);
DEFINE_MITM_MODULE_CLASS(0x8000, 15);
}

View File

@@ -257,7 +257,7 @@ namespace ams::mitm::fs {
}
}
os::Mutex g_fs_romfs_path_lock;
os::Mutex g_fs_romfs_path_lock(false);
char g_fs_romfs_path_buffer[fs::EntryNameLengthMax + 1];
NOINLINE void OpenFileSystemRomfsDirectory(FsDir *out, ncm::ProgramId program_id, BuildDirectoryContext *parent, fs::OpenDirectoryMode mode, FsFileSystem *fs) {

View File

@@ -19,6 +19,6 @@
namespace ams::mitm::hid {
DEFINE_MITM_MODULE_CLASS(0x8000, 47);
DEFINE_MITM_MODULE_CLASS(0x8000, 19);
}

View File

@@ -19,6 +19,6 @@
namespace ams::mitm::ns {
DEFINE_MITM_MODULE_CLASS(0x4000, 48);
DEFINE_MITM_MODULE_CLASS(0x4000, 20);
}

View File

@@ -25,7 +25,7 @@ namespace ams::mitm::settings {
GetRegionCode = 4,
};
private:
os::Mutex lock;
os::Mutex lock{false};
cfg::OverrideLocale locale;
bool got_locale;
public:

View File

@@ -19,6 +19,6 @@
namespace ams::mitm::settings {
DEFINE_MITM_MODULE_CLASS(0x8000, 43);
DEFINE_MITM_MODULE_CLASS(0x8000, 20);
}

View File

@@ -22,7 +22,7 @@ namespace ams::mitm::settings {
namespace {
os::Mutex g_firmware_version_lock;
os::Mutex g_firmware_version_lock(false);
bool g_cached_firmware_version;
settings::FirmwareVersion g_firmware_version;
settings::FirmwareVersion g_ams_firmware_version;
@@ -30,7 +30,7 @@ namespace ams::mitm::settings {
void CacheFirmwareVersion() {
std::scoped_lock lk(g_firmware_version_lock);
if (g_cached_firmware_version) {
if (AMS_LIKELY(g_cached_firmware_version)) {
return;
}