2020-01-31 00:51:35 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-01-31 00:51:35 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
2020-02-08 11:18:08 +00:00
|
|
|
#include <mesosphere/kern_common.hpp>
|
2020-01-31 00:51:35 +00:00
|
|
|
#include <mesosphere/kern_select_cpu.hpp>
|
|
|
|
#include <mesosphere/kern_slab_helpers.hpp>
|
|
|
|
#include <mesosphere/kern_k_synchronization_object.hpp>
|
|
|
|
#include <mesosphere/kern_k_handle_table.hpp>
|
|
|
|
#include <mesosphere/kern_k_thread.hpp>
|
|
|
|
#include <mesosphere/kern_k_thread_local_page.hpp>
|
2020-02-18 13:04:49 +00:00
|
|
|
#include <mesosphere/kern_k_shared_memory_info.hpp>
|
2021-09-18 20:26:21 +00:00
|
|
|
#include <mesosphere/kern_k_io_region.hpp>
|
2020-02-18 13:04:49 +00:00
|
|
|
#include <mesosphere/kern_k_worker_task.hpp>
|
|
|
|
#include <mesosphere/kern_select_page_table.hpp>
|
|
|
|
#include <mesosphere/kern_k_condition_variable.hpp>
|
|
|
|
#include <mesosphere/kern_k_address_arbiter.hpp>
|
|
|
|
#include <mesosphere/kern_k_capabilities.hpp>
|
|
|
|
#include <mesosphere/kern_k_wait_object.hpp>
|
2021-09-18 05:01:58 +00:00
|
|
|
#include <mesosphere/kern_k_dynamic_resource_manager.hpp>
|
2020-02-18 13:04:49 +00:00
|
|
|
#include <mesosphere/kern_k_page_table_manager.hpp>
|
2020-01-31 00:51:35 +00:00
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
2021-10-25 03:41:38 +00:00
|
|
|
class KProcess final : public KAutoObjectWithSlabHeapAndContainer<KProcess, KWorkerTask> {
|
2020-01-31 00:51:35 +00:00
|
|
|
MESOSPHERE_AUTOOBJECT_TRAITS(KProcess, KSynchronizationObject);
|
2020-01-31 09:53:30 +00:00
|
|
|
public:
|
2020-02-18 13:04:49 +00:00
|
|
|
enum State {
|
|
|
|
State_Created = ams::svc::ProcessState_Created,
|
|
|
|
State_CreatedAttached = ams::svc::ProcessState_CreatedAttached,
|
|
|
|
State_Running = ams::svc::ProcessState_Running,
|
|
|
|
State_Crashed = ams::svc::ProcessState_Crashed,
|
|
|
|
State_RunningAttached = ams::svc::ProcessState_RunningAttached,
|
|
|
|
State_Terminating = ams::svc::ProcessState_Terminating,
|
|
|
|
State_Terminated = ams::svc::ProcessState_Terminated,
|
|
|
|
State_DebugBreak = ams::svc::ProcessState_DebugBreak,
|
|
|
|
};
|
2020-01-31 09:53:30 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
using ThreadList = util::IntrusiveListMemberTraits<&KThread::m_process_list_node>::ListType;
|
2020-07-22 01:54:08 +00:00
|
|
|
|
|
|
|
static constexpr size_t AslrAlignment = KernelAslrAlignment;
|
2020-02-18 13:04:49 +00:00
|
|
|
private:
|
|
|
|
using SharedMemoryInfoList = util::IntrusiveListBaseTraits<KSharedMemoryInfo>::ListType;
|
2021-09-18 20:26:21 +00:00
|
|
|
using IoRegionList = util::IntrusiveListMemberTraits<&KIoRegion::m_process_list_node>::ListType;
|
2020-02-18 13:04:49 +00:00
|
|
|
using TLPTree = util::IntrusiveRedBlackTreeBaseTraits<KThreadLocalPage>::TreeType<KThreadLocalPage>;
|
|
|
|
using TLPIterator = TLPTree::iterator;
|
|
|
|
private:
|
2021-10-23 22:25:20 +00:00
|
|
|
KProcessPageTable m_page_table;
|
|
|
|
util::Atomic<size_t> m_used_kernel_memory_size;
|
|
|
|
TLPTree m_fully_used_tlp_tree;
|
|
|
|
TLPTree m_partially_used_tlp_tree;
|
|
|
|
s32 m_ideal_core_id;
|
|
|
|
void *m_attached_object;
|
|
|
|
KResourceLimit *m_resource_limit;
|
|
|
|
KVirtualAddress m_system_resource_address;
|
|
|
|
size_t m_system_resource_num_pages;
|
|
|
|
size_t m_memory_release_hint;
|
|
|
|
State m_state;
|
|
|
|
KLightLock m_state_lock;
|
|
|
|
KLightLock m_list_lock;
|
|
|
|
KConditionVariable m_cond_var;
|
|
|
|
KAddressArbiter m_address_arbiter;
|
|
|
|
u64 m_entropy[4];
|
|
|
|
bool m_is_signaled;
|
|
|
|
bool m_is_initialized;
|
|
|
|
bool m_is_application;
|
|
|
|
char m_name[13];
|
|
|
|
util::Atomic<u16> m_num_running_threads;
|
|
|
|
u32 m_flags;
|
|
|
|
KMemoryManager::Pool m_memory_pool;
|
|
|
|
s64 m_schedule_count;
|
|
|
|
KCapabilities m_capabilities;
|
|
|
|
ams::svc::ProgramId m_program_id;
|
|
|
|
u64 m_process_id;
|
|
|
|
s64 m_creation_time;
|
|
|
|
KProcessAddress m_code_address;
|
|
|
|
size_t m_code_size;
|
|
|
|
size_t m_main_thread_stack_size;
|
|
|
|
size_t m_max_process_memory;
|
|
|
|
u32 m_version;
|
|
|
|
KHandleTable m_handle_table;
|
|
|
|
KProcessAddress m_plr_address;
|
|
|
|
void *m_plr_heap_address;
|
|
|
|
KThread *m_exception_thread;
|
|
|
|
ThreadList m_thread_list;
|
|
|
|
SharedMemoryInfoList m_shared_memory_list;
|
|
|
|
IoRegionList m_io_region_list;
|
|
|
|
bool m_is_suspended;
|
|
|
|
bool m_is_immortal;
|
|
|
|
bool m_is_jit_debug;
|
|
|
|
bool m_is_handle_table_initialized;
|
|
|
|
ams::svc::DebugEvent m_jit_debug_event_type;
|
|
|
|
ams::svc::DebugException m_jit_debug_exception_type;
|
|
|
|
uintptr_t m_jit_debug_params[4];
|
|
|
|
u64 m_jit_debug_thread_id;
|
|
|
|
KWaitObject m_wait_object;
|
|
|
|
KThread *m_running_threads[cpu::NumCores];
|
|
|
|
u64 m_running_thread_idle_counts[cpu::NumCores];
|
|
|
|
KThread *m_pinned_threads[cpu::NumCores];
|
|
|
|
util::Atomic<s64> m_cpu_time;
|
|
|
|
util::Atomic<s64> m_num_process_switches;
|
|
|
|
util::Atomic<s64> m_num_thread_switches;
|
|
|
|
util::Atomic<s64> m_num_fpu_switches;
|
|
|
|
util::Atomic<s64> m_num_supervisor_calls;
|
|
|
|
util::Atomic<s64> m_num_ipc_messages;
|
|
|
|
util::Atomic<s64> m_num_ipc_replies;
|
|
|
|
util::Atomic<s64> m_num_ipc_receives;
|
|
|
|
KDynamicPageManager m_dynamic_page_manager;
|
|
|
|
KMemoryBlockSlabManager m_memory_block_slab_manager;
|
|
|
|
KBlockInfoManager m_block_info_manager;
|
|
|
|
KPageTableManager m_page_table_manager;
|
|
|
|
KMemoryBlockSlabHeap m_memory_block_heap;
|
|
|
|
KBlockInfoSlabHeap m_block_info_heap;
|
|
|
|
KPageTableSlabHeap m_page_table_heap;
|
2020-02-19 09:22:27 +00:00
|
|
|
private:
|
|
|
|
Result Initialize(const ams::svc::CreateProcessParameter ¶ms);
|
2020-07-23 06:52:29 +00:00
|
|
|
|
2021-04-07 15:17:15 +00:00
|
|
|
Result StartTermination();
|
2020-07-23 06:52:29 +00:00
|
|
|
void FinishTermination();
|
2020-08-03 06:06:29 +00:00
|
|
|
|
2021-04-07 22:30:13 +00:00
|
|
|
ALWAYS_INLINE void PinThread(s32 core_id, KThread *thread) {
|
2020-08-03 06:06:29 +00:00
|
|
|
MESOSPHERE_ASSERT(0 <= core_id && core_id < static_cast<s32>(cpu::NumCores));
|
|
|
|
MESOSPHERE_ASSERT(thread != nullptr);
|
2020-12-18 01:18:47 +00:00
|
|
|
MESOSPHERE_ASSERT(m_pinned_threads[core_id] == nullptr);
|
|
|
|
m_pinned_threads[core_id] = thread;
|
2020-08-03 06:06:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 22:30:13 +00:00
|
|
|
ALWAYS_INLINE void UnpinThread(s32 core_id, KThread *thread) {
|
2021-01-08 10:13:36 +00:00
|
|
|
MESOSPHERE_UNUSED(thread);
|
2020-08-03 06:06:29 +00:00
|
|
|
MESOSPHERE_ASSERT(0 <= core_id && core_id < static_cast<s32>(cpu::NumCores));
|
|
|
|
MESOSPHERE_ASSERT(thread != nullptr);
|
2020-12-18 01:18:47 +00:00
|
|
|
MESOSPHERE_ASSERT(m_pinned_threads[core_id] == thread);
|
|
|
|
m_pinned_threads[core_id] = nullptr;
|
2020-08-03 06:06:29 +00:00
|
|
|
}
|
2020-02-18 13:04:49 +00:00
|
|
|
public:
|
2021-10-23 22:25:20 +00:00
|
|
|
explicit KProcess() : m_is_initialized(false) { /* ... */ }
|
2020-02-18 13:04:49 +00:00
|
|
|
|
2021-04-07 15:23:21 +00:00
|
|
|
Result Initialize(const ams::svc::CreateProcessParameter ¶ms, const KPageGroup &pg, const u32 *caps, s32 num_caps, KResourceLimit *res_limit, KMemoryManager::Pool pool, bool immortal);
|
2020-07-22 01:54:08 +00:00
|
|
|
Result Initialize(const ams::svc::CreateProcessParameter ¶ms, svc::KUserPointer<const u32 *> caps, s32 num_caps, KResourceLimit *res_limit, KMemoryManager::Pool pool);
|
2020-07-10 03:11:41 +00:00
|
|
|
void Exit();
|
2020-02-19 09:22:27 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr const char *GetName() const { return m_name; }
|
2020-02-20 17:05:01 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr ams::svc::ProgramId GetProgramId() const { return m_program_id; }
|
2020-05-29 07:57:25 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr u64 GetProcessId() const { return m_process_id; }
|
2020-02-18 13:04:49 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr State GetState() const { return m_state; }
|
2020-07-19 03:03:27 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr u64 GetCoreMask() const { return m_capabilities.GetCoreMask(); }
|
2022-03-23 06:02:10 +00:00
|
|
|
constexpr u64 GetPhysicalCoreMask() const { return m_capabilities.GetPhysicalCoreMask(); }
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr u64 GetPriorityMask() const { return m_capabilities.GetPriorityMask(); }
|
2020-02-05 21:02:35 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr s32 GetIdealCoreId() const { return m_ideal_core_id; }
|
|
|
|
constexpr void SetIdealCoreId(s32 core_id) { m_ideal_core_id = core_id; }
|
2020-02-19 16:07:44 +00:00
|
|
|
|
2020-07-10 01:54:09 +00:00
|
|
|
constexpr bool CheckThreadPriority(s32 prio) const { return ((1ul << prio) & this->GetPriorityMask()) != 0; }
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr u32 GetCreateProcessFlags() const { return m_flags; }
|
2020-07-19 03:03:27 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr bool Is64Bit() const { return m_flags & ams::svc::CreateProcessFlag_Is64Bit; }
|
2020-02-18 13:04:49 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KProcessAddress GetEntryPoint() const { return m_code_address; }
|
2020-02-20 03:38:20 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr size_t GetMainStackSize() const { return m_main_thread_stack_size; }
|
2020-12-11 00:31:47 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KMemoryManager::Pool GetMemoryPool() const { return m_memory_pool; }
|
2020-07-25 05:04:04 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr u64 GetRandomEntropy(size_t i) const { return m_entropy[i]; }
|
2020-02-20 03:38:20 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr bool IsApplication() const { return m_is_application; }
|
2020-07-24 12:26:59 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr bool IsSuspended() const { return m_is_suspended; }
|
|
|
|
constexpr void SetSuspended(bool suspended) { m_is_suspended = suspended; }
|
2020-07-23 10:04:43 +00:00
|
|
|
|
2020-07-22 18:15:45 +00:00
|
|
|
Result Terminate();
|
|
|
|
|
2020-07-19 03:03:27 +00:00
|
|
|
constexpr bool IsTerminated() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_state == State_Terminated;
|
2020-07-19 03:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool IsAttachedToDebugger() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_attached_object != nullptr;
|
2020-07-19 03:03:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 21:29:02 +00:00
|
|
|
constexpr bool IsPermittedSvc(svc::SvcId svc_id) const {
|
|
|
|
return m_capabilities.IsPermittedSvc(svc_id);
|
|
|
|
}
|
|
|
|
|
2020-07-14 10:26:02 +00:00
|
|
|
constexpr bool IsPermittedInterrupt(int32_t interrupt_id) const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_capabilities.IsPermittedInterrupt(interrupt_id);
|
2020-07-14 10:26:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 03:03:27 +00:00
|
|
|
constexpr bool IsPermittedDebug() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_capabilities.IsPermittedDebug();
|
2020-07-19 03:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool CanForceDebug() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_capabilities.CanForceDebug();
|
2020-07-19 03:03:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
u32 GetAllocateOption() const { return m_page_table.GetAllocateOption(); }
|
2020-07-22 09:50:19 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
ThreadList &GetThreadList() { return m_thread_list; }
|
|
|
|
const ThreadList &GetThreadList() const { return m_thread_list; }
|
2020-07-19 03:03:27 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr void *GetDebugObject() const { return m_attached_object; }
|
2020-07-19 03:03:27 +00:00
|
|
|
KProcess::State SetDebugObject(void *debug_object);
|
2020-07-20 03:06:21 +00:00
|
|
|
void ClearDebugObject(KProcess::State state);
|
|
|
|
|
2020-07-31 07:04:43 +00:00
|
|
|
bool EnterJitDebug(ams::svc::DebugEvent event, ams::svc::DebugException exception, uintptr_t param1 = 0, uintptr_t param2 = 0, uintptr_t param3 = 0, uintptr_t param4 = 0);
|
|
|
|
|
2020-07-19 03:03:27 +00:00
|
|
|
KEventInfo *GetJitDebugInfo();
|
2020-07-31 03:49:58 +00:00
|
|
|
void ClearJitDebugInfo();
|
2020-07-19 03:03:27 +00:00
|
|
|
|
2020-07-11 01:39:53 +00:00
|
|
|
bool EnterUserException();
|
|
|
|
bool LeaveUserException();
|
|
|
|
bool ReleaseUserException(KThread *thread);
|
|
|
|
|
2020-07-24 22:44:16 +00:00
|
|
|
KThread *GetPinnedThread(s32 core_id) const {
|
2020-02-18 13:04:49 +00:00
|
|
|
MESOSPHERE_ASSERT(0 <= core_id && core_id < static_cast<s32>(cpu::NumCores));
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_pinned_threads[core_id];
|
2020-02-18 13:04:49 +00:00
|
|
|
}
|
2020-02-08 10:49:32 +00:00
|
|
|
|
2022-03-23 02:59:39 +00:00
|
|
|
const svc::SvcAccessFlagSet &GetSvcPermissions() const { return m_capabilities.GetSvcPermissions(); }
|
2020-07-31 12:52:59 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KResourceLimit *GetResourceLimit() const { return m_resource_limit; }
|
2020-02-20 03:38:20 +00:00
|
|
|
|
2020-02-20 04:42:21 +00:00
|
|
|
bool ReserveResource(ams::svc::LimitableResource which, s64 value);
|
|
|
|
bool ReserveResource(ams::svc::LimitableResource which, s64 value, s64 timeout);
|
|
|
|
void ReleaseResource(ams::svc::LimitableResource which, s64 value);
|
|
|
|
void ReleaseResource(ams::svc::LimitableResource which, s64 value, s64 hint);
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KLightLock &GetStateLock() { return m_state_lock; }
|
|
|
|
constexpr KLightLock &GetListLock() { return m_list_lock; }
|
2020-07-10 01:54:09 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KProcessPageTable &GetPageTable() { return m_page_table; }
|
|
|
|
constexpr const KProcessPageTable &GetPageTable() const { return m_page_table; }
|
2020-02-19 14:46:59 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KHandleTable &GetHandleTable() { return m_handle_table; }
|
|
|
|
constexpr const KHandleTable &GetHandleTable() const { return m_handle_table; }
|
2020-02-20 03:38:20 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
KWaitObject *GetWaitObjectPointer() { return std::addressof(m_wait_object); }
|
2020-07-30 23:31:58 +00:00
|
|
|
|
2020-07-13 19:17:28 +00:00
|
|
|
size_t GetUsedUserPhysicalMemorySize() const;
|
|
|
|
size_t GetTotalUserPhysicalMemorySize() const;
|
2020-05-29 07:57:25 +00:00
|
|
|
size_t GetUsedNonSystemUserPhysicalMemorySize() const;
|
|
|
|
size_t GetTotalNonSystemUserPhysicalMemorySize() const;
|
|
|
|
|
2020-07-22 09:50:19 +00:00
|
|
|
Result AddSharedMemory(KSharedMemory *shmem, KProcessAddress address, size_t size);
|
|
|
|
void RemoveSharedMemory(KSharedMemory *shmem, KProcessAddress address, size_t size);
|
|
|
|
|
2021-09-18 20:26:21 +00:00
|
|
|
void AddIoRegion(KIoRegion *io_region);
|
|
|
|
void RemoveIoRegion(KIoRegion *io_region);
|
|
|
|
|
2020-02-19 14:46:59 +00:00
|
|
|
Result CreateThreadLocalRegion(KProcessAddress *out);
|
2020-07-21 11:58:54 +00:00
|
|
|
Result DeleteThreadLocalRegion(KProcessAddress addr);
|
2020-02-19 14:46:59 +00:00
|
|
|
void *GetThreadLocalRegionPointer(KProcessAddress addr);
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KProcessAddress GetProcessLocalRegionAddress() const { return m_plr_address; }
|
2020-05-29 07:57:25 +00:00
|
|
|
|
2021-10-19 08:19:31 +00:00
|
|
|
constexpr void *GetProcessLocalRegionHeapAddress() const { return m_plr_heap_address; }
|
|
|
|
|
2021-10-19 08:20:28 +00:00
|
|
|
KThread *GetExceptionThread() const { return m_exception_thread; }
|
|
|
|
|
2021-10-20 18:02:17 +00:00
|
|
|
void AddCpuTime(s64 diff) { m_cpu_time += diff; }
|
2021-10-19 22:24:15 +00:00
|
|
|
s64 GetCpuTime() { return m_cpu_time.Load(); }
|
2020-07-11 07:48:26 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr s64 GetScheduledCount() const { return m_schedule_count; }
|
|
|
|
void IncrementScheduledCount() { ++m_schedule_count; }
|
2020-02-20 04:42:21 +00:00
|
|
|
|
2021-04-07 15:17:15 +00:00
|
|
|
void IncrementRunningThreadCount();
|
|
|
|
void DecrementRunningThreadCount();
|
2020-02-20 03:38:20 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
size_t GetTotalSystemResourceSize() const { return m_system_resource_num_pages * PageSize; }
|
2020-07-24 12:26:59 +00:00
|
|
|
size_t GetUsedSystemResourceSize() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
if (m_system_resource_num_pages == 0) {
|
2020-07-24 12:26:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_dynamic_page_manager.GetUsed() * PageSize;
|
2020-07-24 12:26:59 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 08:27:09 +00:00
|
|
|
void SetRunningThread(s32 core, KThread *thread, u64 idle_count) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_running_threads[core] = thread;
|
|
|
|
m_running_thread_idle_counts[core] = idle_count;
|
2020-07-31 08:27:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 01:39:53 +00:00
|
|
|
void ClearRunningThread(KThread *thread) {
|
2020-12-18 01:18:47 +00:00
|
|
|
for (size_t i = 0; i < util::size(m_running_threads); ++i) {
|
|
|
|
if (m_running_threads[i] == thread) {
|
|
|
|
m_running_threads[i] = nullptr;
|
2020-07-11 01:39:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
const KDynamicPageManager &GetDynamicPageManager() const { return m_dynamic_page_manager; }
|
|
|
|
const KMemoryBlockSlabManager &GetMemoryBlockSlabManager() const { return m_memory_block_slab_manager; }
|
|
|
|
const KBlockInfoManager &GetBlockInfoManager() const { return m_block_info_manager; }
|
|
|
|
const KPageTableManager &GetPageTableManager() const { return m_page_table_manager; }
|
2020-12-10 11:31:57 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KThread *GetRunningThread(s32 core) const { return m_running_threads[core]; }
|
|
|
|
constexpr u64 GetRunningThreadIdleCount(s32 core) const { return m_running_thread_idle_counts[core]; }
|
2020-07-30 23:31:58 +00:00
|
|
|
|
2020-02-20 03:38:20 +00:00
|
|
|
void RegisterThread(KThread *thread);
|
|
|
|
void UnregisterThread(KThread *thread);
|
|
|
|
|
|
|
|
Result Run(s32 priority, size_t stack_size);
|
|
|
|
|
2020-07-14 09:45:06 +00:00
|
|
|
Result Reset();
|
|
|
|
|
2020-07-31 03:49:58 +00:00
|
|
|
void SetDebugBreak() {
|
2020-12-18 01:18:47 +00:00
|
|
|
if (m_state == State_RunningAttached) {
|
2020-07-31 03:49:58 +00:00
|
|
|
this->ChangeState(State_DebugBreak);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetAttached() {
|
2020-12-18 01:18:47 +00:00
|
|
|
if (m_state == State_DebugBreak) {
|
2020-07-31 03:49:58 +00:00
|
|
|
this->ChangeState(State_RunningAttached);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 10:04:43 +00:00
|
|
|
Result SetActivity(ams::svc::ProcessActivity activity);
|
|
|
|
|
2020-07-24 22:44:16 +00:00
|
|
|
void PinCurrentThread();
|
2020-08-03 06:06:29 +00:00
|
|
|
void UnpinCurrentThread();
|
2021-04-07 22:30:13 +00:00
|
|
|
void UnpinThread(KThread *thread);
|
2020-02-20 04:42:21 +00:00
|
|
|
|
2020-07-10 02:17:30 +00:00
|
|
|
void SignalConditionVariable(uintptr_t cv_key, int32_t count) {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_cond_var.Signal(cv_key, count);
|
2020-07-10 02:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result WaitConditionVariable(KProcessAddress address, uintptr_t cv_key, u32 tag, s64 ns) {
|
2022-02-14 22:45:32 +00:00
|
|
|
R_RETURN(m_cond_var.Wait(address, cv_key, tag, ns));
|
2020-07-10 02:17:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 16:15:49 +00:00
|
|
|
Result SignalAddressArbiter(uintptr_t address, ams::svc::SignalType signal_type, s32 value, s32 count) {
|
2022-02-14 22:45:32 +00:00
|
|
|
R_RETURN(m_address_arbiter.SignalToAddress(address, signal_type, value, count));
|
2020-07-15 16:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result WaitAddressArbiter(uintptr_t address, ams::svc::ArbitrationType arb_type, s32 value, s64 timeout) {
|
2022-02-14 22:45:32 +00:00
|
|
|
R_RETURN(m_address_arbiter.WaitForAddress(address, arb_type, value, timeout));
|
2020-07-15 16:15:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:52:11 +00:00
|
|
|
Result GetThreadList(s32 *out_num_threads, ams::kern::svc::KUserPointer<u64 *> out_thread_ids, s32 max_out_count);
|
|
|
|
|
2020-07-14 20:22:08 +00:00
|
|
|
static KProcess *GetProcessFromId(u64 process_id);
|
|
|
|
static Result GetProcessList(s32 *out_num_processes, ams::kern::svc::KUserPointer<u64 *> out_process_ids, s32 max_out_count);
|
|
|
|
|
2020-02-20 04:42:21 +00:00
|
|
|
static void Switch(KProcess *cur_process, KProcess *next_process) {
|
2020-08-21 00:29:10 +00:00
|
|
|
MESOSPHERE_UNUSED(cur_process);
|
|
|
|
|
2020-02-20 04:42:21 +00:00
|
|
|
/* Update the current page table. */
|
|
|
|
if (next_process) {
|
|
|
|
next_process->GetPageTable().Activate(next_process->GetProcessId());
|
|
|
|
} else {
|
|
|
|
Kernel::GetKernelPageTable().Activate();
|
|
|
|
}
|
|
|
|
}
|
2020-02-18 13:04:49 +00:00
|
|
|
public:
|
|
|
|
/* Overridden parent functions. */
|
2021-10-24 04:13:26 +00:00
|
|
|
bool IsInitialized() const { return m_is_initialized; }
|
2020-02-18 13:04:49 +00:00
|
|
|
|
2020-08-21 00:29:10 +00:00
|
|
|
static void PostDestroy(uintptr_t arg) { MESOSPHERE_UNUSED(arg); /* ... */ }
|
2020-02-18 13:04:49 +00:00
|
|
|
|
2021-10-24 04:13:26 +00:00
|
|
|
void Finalize();
|
2020-02-18 13:04:49 +00:00
|
|
|
|
2021-10-25 00:00:05 +00:00
|
|
|
ALWAYS_INLINE u64 GetIdImpl() const { return this->GetProcessId(); }
|
|
|
|
ALWAYS_INLINE u64 GetId() const { return this->GetIdImpl(); }
|
2020-02-18 13:04:49 +00:00
|
|
|
|
|
|
|
virtual bool IsSignaled() const override {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
MESOSPHERE_ASSERT(KScheduler::IsSchedulerLockedByCurrentThread());
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_is_signaled;
|
2020-02-18 13:04:49 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 03:41:38 +00:00
|
|
|
void DoWorkerTaskImpl();
|
2020-02-20 03:38:20 +00:00
|
|
|
private:
|
|
|
|
void ChangeState(State new_state) {
|
2020-12-18 01:18:47 +00:00
|
|
|
if (m_state != new_state) {
|
|
|
|
m_state = new_state;
|
|
|
|
m_is_signaled = true;
|
2020-02-20 03:38:20 +00:00
|
|
|
this->NotifyAvailable();
|
|
|
|
}
|
|
|
|
}
|
2021-04-07 22:16:11 +00:00
|
|
|
|
|
|
|
ALWAYS_INLINE Result InitializeHandleTable(s32 size) {
|
|
|
|
/* Try to initialize the handle table. */
|
|
|
|
R_TRY(m_handle_table.Initialize(size));
|
|
|
|
|
|
|
|
/* We succeeded, so note that we did. */
|
|
|
|
m_is_handle_table_initialized = true;
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2021-04-07 22:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE void FinalizeHandleTable() {
|
|
|
|
/* Finalize the table. */
|
|
|
|
m_handle_table.Finalize();
|
|
|
|
|
|
|
|
/* Note that the table is finalized. */
|
|
|
|
m_is_handle_table_initialized = false;
|
|
|
|
}
|
2020-01-31 00:51:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|