k_process.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <map>
  5. #include "core/file_sys/program_metadata.h"
  6. #include "core/hle/kernel/code_set.h"
  7. #include "core/hle/kernel/k_address_arbiter.h"
  8. #include "core/hle/kernel/k_capabilities.h"
  9. #include "core/hle/kernel/k_condition_variable.h"
  10. #include "core/hle/kernel/k_handle_table.h"
  11. #include "core/hle/kernel/k_page_table_manager.h"
  12. #include "core/hle/kernel/k_process_page_table.h"
  13. #include "core/hle/kernel/k_system_resource.h"
  14. #include "core/hle/kernel/k_thread.h"
  15. #include "core/hle/kernel/k_thread_local_page.h"
  16. namespace Kernel {
  17. enum class DebugWatchpointType : u8 {
  18. None = 0,
  19. Read = 1 << 0,
  20. Write = 1 << 1,
  21. ReadOrWrite = Read | Write,
  22. };
  23. DECLARE_ENUM_FLAG_OPERATORS(DebugWatchpointType);
  24. struct DebugWatchpoint {
  25. KProcessAddress start_address;
  26. KProcessAddress end_address;
  27. DebugWatchpointType type;
  28. };
  29. class KProcess final : public KAutoObjectWithSlabHeapAndContainer<KProcess, KWorkerTask> {
  30. KERNEL_AUTOOBJECT_TRAITS(KProcess, KSynchronizationObject);
  31. public:
  32. enum class State {
  33. Created = static_cast<u32>(Svc::ProcessState::Created),
  34. CreatedAttached = static_cast<u32>(Svc::ProcessState::CreatedAttached),
  35. Running = static_cast<u32>(Svc::ProcessState::Running),
  36. Crashed = static_cast<u32>(Svc::ProcessState::Crashed),
  37. RunningAttached = static_cast<u32>(Svc::ProcessState::RunningAttached),
  38. Terminating = static_cast<u32>(Svc::ProcessState::Terminating),
  39. Terminated = static_cast<u32>(Svc::ProcessState::Terminated),
  40. DebugBreak = static_cast<u32>(Svc::ProcessState::DebugBreak),
  41. };
  42. using ThreadList = Common::IntrusiveListMemberTraits<&KThread::m_process_list_node>::ListType;
  43. static constexpr size_t AslrAlignment = 2_MiB;
  44. public:
  45. static constexpr u64 InitialProcessIdMin = 1;
  46. static constexpr u64 InitialProcessIdMax = 0x50;
  47. static constexpr u64 ProcessIdMin = InitialProcessIdMax + 1;
  48. static constexpr u64 ProcessIdMax = std::numeric_limits<u64>::max();
  49. private:
  50. using SharedMemoryInfoList = Common::IntrusiveListBaseTraits<KSharedMemoryInfo>::ListType;
  51. using TLPTree =
  52. Common::IntrusiveRedBlackTreeBaseTraits<KThreadLocalPage>::TreeType<KThreadLocalPage>;
  53. using TLPIterator = TLPTree::iterator;
  54. private:
  55. KProcessPageTable m_page_table;
  56. std::atomic<size_t> m_used_kernel_memory_size{};
  57. TLPTree m_fully_used_tlp_tree{};
  58. TLPTree m_partially_used_tlp_tree{};
  59. s32 m_ideal_core_id{};
  60. KResourceLimit* m_resource_limit{};
  61. KSystemResource* m_system_resource{};
  62. size_t m_memory_release_hint{};
  63. State m_state{};
  64. KLightLock m_state_lock;
  65. KLightLock m_list_lock;
  66. KConditionVariable m_cond_var;
  67. KAddressArbiter m_address_arbiter;
  68. std::array<u64, 4> m_entropy{};
  69. bool m_is_signaled{};
  70. bool m_is_initialized{};
  71. bool m_is_application{};
  72. bool m_is_default_application_system_resource{};
  73. bool m_is_hbl{};
  74. std::array<char, 13> m_name{};
  75. std::atomic<u16> m_num_running_threads{};
  76. Svc::CreateProcessFlag m_flags{};
  77. KMemoryManager::Pool m_memory_pool{};
  78. s64 m_schedule_count{};
  79. KCapabilities m_capabilities{};
  80. u64 m_program_id{};
  81. u64 m_process_id{};
  82. KProcessAddress m_code_address{};
  83. size_t m_code_size{};
  84. size_t m_main_thread_stack_size{};
  85. size_t m_max_process_memory{};
  86. u32 m_version{};
  87. KHandleTable m_handle_table;
  88. KProcessAddress m_plr_address{};
  89. KThread* m_exception_thread{};
  90. ThreadList m_thread_list{};
  91. SharedMemoryInfoList m_shared_memory_list{};
  92. bool m_is_suspended{};
  93. bool m_is_immortal{};
  94. bool m_is_handle_table_initialized{};
  95. std::array<KThread*, Core::Hardware::NUM_CPU_CORES> m_running_threads{};
  96. std::array<u64, Core::Hardware::NUM_CPU_CORES> m_running_thread_idle_counts{};
  97. std::array<u64, Core::Hardware::NUM_CPU_CORES> m_running_thread_switch_counts{};
  98. std::array<KThread*, Core::Hardware::NUM_CPU_CORES> m_pinned_threads{};
  99. std::array<DebugWatchpoint, Core::Hardware::NUM_WATCHPOINTS> m_watchpoints{};
  100. std::map<KProcessAddress, u64> m_debug_page_refcounts{};
  101. std::atomic<s64> m_cpu_time{};
  102. std::atomic<s64> m_num_process_switches{};
  103. std::atomic<s64> m_num_thread_switches{};
  104. std::atomic<s64> m_num_fpu_switches{};
  105. std::atomic<s64> m_num_supervisor_calls{};
  106. std::atomic<s64> m_num_ipc_messages{};
  107. std::atomic<s64> m_num_ipc_replies{};
  108. std::atomic<s64> m_num_ipc_receives{};
  109. private:
  110. Result StartTermination();
  111. void FinishTermination();
  112. void PinThread(s32 core_id, KThread* thread) {
  113. ASSERT(0 <= core_id && core_id < static_cast<s32>(Core::Hardware::NUM_CPU_CORES));
  114. ASSERT(thread != nullptr);
  115. ASSERT(m_pinned_threads[core_id] == nullptr);
  116. m_pinned_threads[core_id] = thread;
  117. }
  118. void UnpinThread(s32 core_id, KThread* thread) {
  119. ASSERT(0 <= core_id && core_id < static_cast<s32>(Core::Hardware::NUM_CPU_CORES));
  120. ASSERT(thread != nullptr);
  121. ASSERT(m_pinned_threads[core_id] == thread);
  122. m_pinned_threads[core_id] = nullptr;
  123. }
  124. public:
  125. explicit KProcess(KernelCore& kernel);
  126. ~KProcess() override;
  127. Result Initialize(const Svc::CreateProcessParameter& params, KResourceLimit* res_limit,
  128. bool is_real);
  129. Result Initialize(const Svc::CreateProcessParameter& params, const KPageGroup& pg,
  130. std::span<const u32> caps, KResourceLimit* res_limit,
  131. KMemoryManager::Pool pool, bool immortal);
  132. Result Initialize(const Svc::CreateProcessParameter& params, std::span<const u32> user_caps,
  133. KResourceLimit* res_limit, KMemoryManager::Pool pool,
  134. KProcessAddress aslr_space_start);
  135. void Exit();
  136. const char* GetName() const {
  137. return m_name.data();
  138. }
  139. u64 GetProgramId() const {
  140. return m_program_id;
  141. }
  142. u64 GetProcessId() const {
  143. return m_process_id;
  144. }
  145. State GetState() const {
  146. return m_state;
  147. }
  148. u64 GetCoreMask() const {
  149. return m_capabilities.GetCoreMask();
  150. }
  151. u64 GetPhysicalCoreMask() const {
  152. return m_capabilities.GetPhysicalCoreMask();
  153. }
  154. u64 GetPriorityMask() const {
  155. return m_capabilities.GetPriorityMask();
  156. }
  157. s32 GetIdealCoreId() const {
  158. return m_ideal_core_id;
  159. }
  160. void SetIdealCoreId(s32 core_id) {
  161. m_ideal_core_id = core_id;
  162. }
  163. bool CheckThreadPriority(s32 prio) const {
  164. return ((1ULL << prio) & this->GetPriorityMask()) != 0;
  165. }
  166. u32 GetCreateProcessFlags() const {
  167. return static_cast<u32>(m_flags);
  168. }
  169. bool Is64Bit() const {
  170. return True(m_flags & Svc::CreateProcessFlag::Is64Bit);
  171. }
  172. KProcessAddress GetEntryPoint() const {
  173. return m_code_address;
  174. }
  175. size_t GetMainStackSize() const {
  176. return m_main_thread_stack_size;
  177. }
  178. KMemoryManager::Pool GetMemoryPool() const {
  179. return m_memory_pool;
  180. }
  181. u64 GetRandomEntropy(size_t i) const {
  182. return m_entropy[i];
  183. }
  184. bool IsApplication() const {
  185. return m_is_application;
  186. }
  187. bool IsDefaultApplicationSystemResource() const {
  188. return m_is_default_application_system_resource;
  189. }
  190. bool IsSuspended() const {
  191. return m_is_suspended;
  192. }
  193. void SetSuspended(bool suspended) {
  194. m_is_suspended = suspended;
  195. }
  196. Result Terminate();
  197. bool IsTerminated() const {
  198. return m_state == State::Terminated;
  199. }
  200. bool IsPermittedSvc(u32 svc_id) const {
  201. return m_capabilities.IsPermittedSvc(svc_id);
  202. }
  203. bool IsPermittedInterrupt(s32 interrupt_id) const {
  204. return m_capabilities.IsPermittedInterrupt(interrupt_id);
  205. }
  206. bool IsPermittedDebug() const {
  207. return m_capabilities.IsPermittedDebug();
  208. }
  209. bool CanForceDebug() const {
  210. return m_capabilities.CanForceDebug();
  211. }
  212. bool IsHbl() const {
  213. return m_is_hbl;
  214. }
  215. u32 GetAllocateOption() const {
  216. return m_page_table.GetAllocateOption();
  217. }
  218. ThreadList& GetThreadList() {
  219. return m_thread_list;
  220. }
  221. const ThreadList& GetThreadList() const {
  222. return m_thread_list;
  223. }
  224. bool EnterUserException();
  225. bool LeaveUserException();
  226. bool ReleaseUserException(KThread* thread);
  227. KThread* GetPinnedThread(s32 core_id) const {
  228. ASSERT(0 <= core_id && core_id < static_cast<s32>(Core::Hardware::NUM_CPU_CORES));
  229. return m_pinned_threads[core_id];
  230. }
  231. const Svc::SvcAccessFlagSet& GetSvcPermissions() const {
  232. return m_capabilities.GetSvcPermissions();
  233. }
  234. KResourceLimit* GetResourceLimit() const {
  235. return m_resource_limit;
  236. }
  237. bool ReserveResource(Svc::LimitableResource which, s64 value);
  238. bool ReserveResource(Svc::LimitableResource which, s64 value, s64 timeout);
  239. void ReleaseResource(Svc::LimitableResource which, s64 value);
  240. void ReleaseResource(Svc::LimitableResource which, s64 value, s64 hint);
  241. KLightLock& GetStateLock() {
  242. return m_state_lock;
  243. }
  244. KLightLock& GetListLock() {
  245. return m_list_lock;
  246. }
  247. KProcessPageTable& GetPageTable() {
  248. return m_page_table;
  249. }
  250. const KProcessPageTable& GetPageTable() const {
  251. return m_page_table;
  252. }
  253. KHandleTable& GetHandleTable() {
  254. return m_handle_table;
  255. }
  256. const KHandleTable& GetHandleTable() const {
  257. return m_handle_table;
  258. }
  259. size_t GetUsedUserPhysicalMemorySize() const;
  260. size_t GetTotalUserPhysicalMemorySize() const;
  261. size_t GetUsedNonSystemUserPhysicalMemorySize() const;
  262. size_t GetTotalNonSystemUserPhysicalMemorySize() const;
  263. Result AddSharedMemory(KSharedMemory* shmem, KProcessAddress address, size_t size);
  264. void RemoveSharedMemory(KSharedMemory* shmem, KProcessAddress address, size_t size);
  265. Result CreateThreadLocalRegion(KProcessAddress* out);
  266. Result DeleteThreadLocalRegion(KProcessAddress addr);
  267. KProcessAddress GetProcessLocalRegionAddress() const {
  268. return m_plr_address;
  269. }
  270. KThread* GetExceptionThread() const {
  271. return m_exception_thread;
  272. }
  273. void AddCpuTime(s64 diff) {
  274. m_cpu_time += diff;
  275. }
  276. s64 GetCpuTime() {
  277. return m_cpu_time.load();
  278. }
  279. s64 GetScheduledCount() const {
  280. return m_schedule_count;
  281. }
  282. void IncrementScheduledCount() {
  283. ++m_schedule_count;
  284. }
  285. void IncrementRunningThreadCount();
  286. void DecrementRunningThreadCount();
  287. size_t GetRequiredSecureMemorySizeNonDefault() const {
  288. if (!this->IsDefaultApplicationSystemResource() && m_system_resource->IsSecureResource()) {
  289. auto* secure_system_resource = static_cast<KSecureSystemResource*>(m_system_resource);
  290. return secure_system_resource->CalculateRequiredSecureMemorySize();
  291. }
  292. return 0;
  293. }
  294. size_t GetRequiredSecureMemorySize() const {
  295. if (m_system_resource->IsSecureResource()) {
  296. auto* secure_system_resource = static_cast<KSecureSystemResource*>(m_system_resource);
  297. return secure_system_resource->CalculateRequiredSecureMemorySize();
  298. }
  299. return 0;
  300. }
  301. size_t GetTotalSystemResourceSize() const {
  302. if (!this->IsDefaultApplicationSystemResource() && m_system_resource->IsSecureResource()) {
  303. auto* secure_system_resource = static_cast<KSecureSystemResource*>(m_system_resource);
  304. return secure_system_resource->GetSize();
  305. }
  306. return 0;
  307. }
  308. size_t GetUsedSystemResourceSize() const {
  309. if (!this->IsDefaultApplicationSystemResource() && m_system_resource->IsSecureResource()) {
  310. auto* secure_system_resource = static_cast<KSecureSystemResource*>(m_system_resource);
  311. return secure_system_resource->GetUsedSize();
  312. }
  313. return 0;
  314. }
  315. void SetRunningThread(s32 core, KThread* thread, u64 idle_count, u64 switch_count) {
  316. m_running_threads[core] = thread;
  317. m_running_thread_idle_counts[core] = idle_count;
  318. m_running_thread_switch_counts[core] = switch_count;
  319. }
  320. void ClearRunningThread(KThread* thread) {
  321. for (size_t i = 0; i < m_running_threads.size(); ++i) {
  322. if (m_running_threads[i] == thread) {
  323. m_running_threads[i] = nullptr;
  324. }
  325. }
  326. }
  327. const KSystemResource& GetSystemResource() const {
  328. return *m_system_resource;
  329. }
  330. const KMemoryBlockSlabManager& GetMemoryBlockSlabManager() const {
  331. return m_system_resource->GetMemoryBlockSlabManager();
  332. }
  333. const KBlockInfoManager& GetBlockInfoManager() const {
  334. return m_system_resource->GetBlockInfoManager();
  335. }
  336. const KPageTableManager& GetPageTableManager() const {
  337. return m_system_resource->GetPageTableManager();
  338. }
  339. KThread* GetRunningThread(s32 core) const {
  340. return m_running_threads[core];
  341. }
  342. u64 GetRunningThreadIdleCount(s32 core) const {
  343. return m_running_thread_idle_counts[core];
  344. }
  345. u64 GetRunningThreadSwitchCount(s32 core) const {
  346. return m_running_thread_switch_counts[core];
  347. }
  348. void RegisterThread(KThread* thread);
  349. void UnregisterThread(KThread* thread);
  350. Result Run(s32 priority, size_t stack_size);
  351. Result Reset();
  352. void SetDebugBreak() {
  353. if (m_state == State::RunningAttached) {
  354. this->ChangeState(State::DebugBreak);
  355. }
  356. }
  357. void SetAttached() {
  358. if (m_state == State::DebugBreak) {
  359. this->ChangeState(State::RunningAttached);
  360. }
  361. }
  362. Result SetActivity(Svc::ProcessActivity activity);
  363. void PinCurrentThread();
  364. void UnpinCurrentThread();
  365. void UnpinThread(KThread* thread);
  366. void SignalConditionVariable(uintptr_t cv_key, int32_t count) {
  367. return m_cond_var.Signal(cv_key, count);
  368. }
  369. Result WaitConditionVariable(KProcessAddress address, uintptr_t cv_key, u32 tag, s64 ns) {
  370. R_RETURN(m_cond_var.Wait(address, cv_key, tag, ns));
  371. }
  372. Result SignalAddressArbiter(uintptr_t address, Svc::SignalType signal_type, s32 value,
  373. s32 count) {
  374. R_RETURN(m_address_arbiter.SignalToAddress(address, signal_type, value, count));
  375. }
  376. Result WaitAddressArbiter(uintptr_t address, Svc::ArbitrationType arb_type, s32 value,
  377. s64 timeout) {
  378. R_RETURN(m_address_arbiter.WaitForAddress(address, arb_type, value, timeout));
  379. }
  380. Result GetThreadList(s32* out_num_threads, KProcessAddress out_thread_ids, s32 max_out_count);
  381. static void Switch(KProcess* cur_process, KProcess* next_process);
  382. public:
  383. // Attempts to insert a watchpoint into a free slot. Returns false if none are available.
  384. bool InsertWatchpoint(KProcessAddress addr, u64 size, DebugWatchpointType type);
  385. // Attempts to remove the watchpoint specified by the given parameters.
  386. bool RemoveWatchpoint(KProcessAddress addr, u64 size, DebugWatchpointType type);
  387. const std::array<DebugWatchpoint, Core::Hardware::NUM_WATCHPOINTS>& GetWatchpoints() const {
  388. return m_watchpoints;
  389. }
  390. public:
  391. Result LoadFromMetadata(const FileSys::ProgramMetadata& metadata, std::size_t code_size,
  392. KProcessAddress aslr_space_start, bool is_hbl);
  393. void LoadModule(CodeSet code_set, KProcessAddress base_addr);
  394. Core::Memory::Memory& GetMemory() const;
  395. public:
  396. // Overridden parent functions.
  397. bool IsInitialized() const override {
  398. return m_is_initialized;
  399. }
  400. static void PostDestroy(uintptr_t arg) {}
  401. void Finalize() override;
  402. u64 GetIdImpl() const {
  403. return this->GetProcessId();
  404. }
  405. u64 GetId() const override {
  406. return this->GetIdImpl();
  407. }
  408. virtual bool IsSignaled() const override {
  409. ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));
  410. return m_is_signaled;
  411. }
  412. void DoWorkerTaskImpl();
  413. private:
  414. void ChangeState(State new_state) {
  415. if (m_state != new_state) {
  416. m_state = new_state;
  417. m_is_signaled = true;
  418. this->NotifyAvailable();
  419. }
  420. }
  421. Result InitializeHandleTable(s32 size) {
  422. // Try to initialize the handle table.
  423. R_TRY(m_handle_table.Initialize(size));
  424. // We succeeded, so note that we did.
  425. m_is_handle_table_initialized = true;
  426. R_SUCCEED();
  427. }
  428. void FinalizeHandleTable() {
  429. // Finalize the table.
  430. m_handle_table.Finalize();
  431. // Note that the table is finalized.
  432. m_is_handle_table_initialized = false;
  433. }
  434. };
  435. } // namespace Kernel