k_process.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. // SPDX-FileCopyrightText: 2015 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <bitset>
  5. #include <ctime>
  6. #include <memory>
  7. #include <random>
  8. #include "common/alignment.h"
  9. #include "common/assert.h"
  10. #include "common/logging/log.h"
  11. #include "common/scope_exit.h"
  12. #include "common/settings.h"
  13. #include "core/core.h"
  14. #include "core/file_sys/program_metadata.h"
  15. #include "core/hle/kernel/code_set.h"
  16. #include "core/hle/kernel/k_memory_block_manager.h"
  17. #include "core/hle/kernel/k_page_table.h"
  18. #include "core/hle/kernel/k_process.h"
  19. #include "core/hle/kernel/k_resource_limit.h"
  20. #include "core/hle/kernel/k_scheduler.h"
  21. #include "core/hle/kernel/k_scoped_resource_reservation.h"
  22. #include "core/hle/kernel/k_shared_memory.h"
  23. #include "core/hle/kernel/k_shared_memory_info.h"
  24. #include "core/hle/kernel/k_thread.h"
  25. #include "core/hle/kernel/kernel.h"
  26. #include "core/hle/kernel/svc_results.h"
  27. #include "core/memory.h"
  28. namespace Kernel {
  29. namespace {
  30. /**
  31. * Sets up the primary application thread
  32. *
  33. * @param system The system instance to create the main thread under.
  34. * @param owner_process The parent process for the main thread
  35. * @param priority The priority to give the main thread
  36. */
  37. void SetupMainThread(Core::System& system, KProcess& owner_process, u32 priority,
  38. KProcessAddress stack_top) {
  39. const KProcessAddress entry_point = owner_process.GetEntryPoint();
  40. ASSERT(owner_process.GetResourceLimit()->Reserve(LimitableResource::ThreadCountMax, 1));
  41. KThread* thread = KThread::Create(system.Kernel());
  42. SCOPE_EXIT({ thread->Close(); });
  43. ASSERT(KThread::InitializeUserThread(system, thread, entry_point, 0, stack_top, priority,
  44. owner_process.GetIdealCoreId(),
  45. std::addressof(owner_process))
  46. .IsSuccess());
  47. // Register 1 must be a handle to the main thread
  48. Handle thread_handle{};
  49. owner_process.GetHandleTable().Add(std::addressof(thread_handle), thread);
  50. thread->GetContext32().cpu_registers[0] = 0;
  51. thread->GetContext64().cpu_registers[0] = 0;
  52. thread->GetContext32().cpu_registers[1] = thread_handle;
  53. thread->GetContext64().cpu_registers[1] = thread_handle;
  54. if (system.DebuggerEnabled()) {
  55. thread->RequestSuspend(SuspendType::Debug);
  56. }
  57. // Run our thread.
  58. void(thread->Run());
  59. }
  60. } // Anonymous namespace
  61. Result KProcess::Initialize(KProcess* process, Core::System& system, std::string process_name,
  62. ProcessType type, KResourceLimit* res_limit) {
  63. auto& kernel = system.Kernel();
  64. process->name = std::move(process_name);
  65. process->m_resource_limit = res_limit;
  66. process->m_system_resource_address = 0;
  67. process->m_state = State::Created;
  68. process->m_program_id = 0;
  69. process->m_process_id = type == ProcessType::KernelInternal ? kernel.CreateNewKernelProcessID()
  70. : kernel.CreateNewUserProcessID();
  71. process->m_capabilities.InitializeForMetadatalessProcess();
  72. process->m_is_initialized = true;
  73. std::mt19937 rng(Settings::values.rng_seed_enabled ? Settings::values.rng_seed.GetValue()
  74. : static_cast<u32>(std::time(nullptr)));
  75. std::uniform_int_distribution<u64> distribution;
  76. std::generate(process->m_random_entropy.begin(), process->m_random_entropy.end(),
  77. [&] { return distribution(rng); });
  78. kernel.AppendNewProcess(process);
  79. // Clear remaining fields.
  80. process->m_num_running_threads = 0;
  81. process->m_is_signaled = false;
  82. process->m_exception_thread = nullptr;
  83. process->m_is_suspended = false;
  84. process->m_schedule_count = 0;
  85. process->m_is_handle_table_initialized = false;
  86. process->m_is_hbl = false;
  87. // Open a reference to the resource limit.
  88. process->m_resource_limit->Open();
  89. R_SUCCEED();
  90. }
  91. void KProcess::DoWorkerTaskImpl() {
  92. UNIMPLEMENTED();
  93. }
  94. KResourceLimit* KProcess::GetResourceLimit() const {
  95. return m_resource_limit;
  96. }
  97. void KProcess::IncrementRunningThreadCount() {
  98. ASSERT(m_num_running_threads.load() >= 0);
  99. ++m_num_running_threads;
  100. }
  101. void KProcess::DecrementRunningThreadCount() {
  102. ASSERT(m_num_running_threads.load() > 0);
  103. if (const auto prev = m_num_running_threads--; prev == 1) {
  104. // TODO(bunnei): Process termination to be implemented when multiprocess is supported.
  105. }
  106. }
  107. u64 KProcess::GetTotalPhysicalMemoryAvailable() {
  108. const u64 capacity{m_resource_limit->GetFreeValue(LimitableResource::PhysicalMemoryMax) +
  109. m_page_table.GetNormalMemorySize() + GetSystemResourceSize() + m_image_size +
  110. m_main_thread_stack_size};
  111. if (const auto pool_size = m_kernel.MemoryManager().GetSize(KMemoryManager::Pool::Application);
  112. capacity != pool_size) {
  113. LOG_WARNING(Kernel, "capacity {} != application pool size {}", capacity, pool_size);
  114. }
  115. if (capacity < m_memory_usage_capacity) {
  116. return capacity;
  117. }
  118. return m_memory_usage_capacity;
  119. }
  120. u64 KProcess::GetTotalPhysicalMemoryAvailableWithoutSystemResource() {
  121. return this->GetTotalPhysicalMemoryAvailable() - this->GetSystemResourceSize();
  122. }
  123. u64 KProcess::GetTotalPhysicalMemoryUsed() {
  124. return m_image_size + m_main_thread_stack_size + m_page_table.GetNormalMemorySize() +
  125. this->GetSystemResourceSize();
  126. }
  127. u64 KProcess::GetTotalPhysicalMemoryUsedWithoutSystemResource() {
  128. return this->GetTotalPhysicalMemoryUsed() - this->GetSystemResourceSize();
  129. }
  130. bool KProcess::ReleaseUserException(KThread* thread) {
  131. KScopedSchedulerLock sl{m_kernel};
  132. if (m_exception_thread == thread) {
  133. m_exception_thread = nullptr;
  134. // Remove waiter thread.
  135. bool has_waiters{};
  136. if (KThread* next = thread->RemoveKernelWaiterByKey(
  137. std::addressof(has_waiters),
  138. reinterpret_cast<uintptr_t>(std::addressof(m_exception_thread)));
  139. next != nullptr) {
  140. next->EndWait(ResultSuccess);
  141. }
  142. KScheduler::SetSchedulerUpdateNeeded(m_kernel);
  143. return true;
  144. } else {
  145. return false;
  146. }
  147. }
  148. void KProcess::PinCurrentThread(s32 core_id) {
  149. ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));
  150. // Get the current thread.
  151. KThread* cur_thread =
  152. m_kernel.Scheduler(static_cast<std::size_t>(core_id)).GetSchedulerCurrentThread();
  153. // If the thread isn't terminated, pin it.
  154. if (!cur_thread->IsTerminationRequested()) {
  155. // Pin it.
  156. this->PinThread(core_id, cur_thread);
  157. cur_thread->Pin(core_id);
  158. // An update is needed.
  159. KScheduler::SetSchedulerUpdateNeeded(m_kernel);
  160. }
  161. }
  162. void KProcess::UnpinCurrentThread(s32 core_id) {
  163. ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));
  164. // Get the current thread.
  165. KThread* cur_thread =
  166. m_kernel.Scheduler(static_cast<std::size_t>(core_id)).GetSchedulerCurrentThread();
  167. // Unpin it.
  168. cur_thread->Unpin();
  169. this->UnpinThread(core_id, cur_thread);
  170. // An update is needed.
  171. KScheduler::SetSchedulerUpdateNeeded(m_kernel);
  172. }
  173. void KProcess::UnpinThread(KThread* thread) {
  174. ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));
  175. // Get the thread's core id.
  176. const auto core_id = thread->GetActiveCore();
  177. // Unpin it.
  178. this->UnpinThread(core_id, thread);
  179. thread->Unpin();
  180. // An update is needed.
  181. KScheduler::SetSchedulerUpdateNeeded(m_kernel);
  182. }
  183. Result KProcess::AddSharedMemory(KSharedMemory* shmem, [[maybe_unused]] KProcessAddress address,
  184. [[maybe_unused]] size_t size) {
  185. // Lock ourselves, to prevent concurrent access.
  186. KScopedLightLock lk(m_state_lock);
  187. // Try to find an existing info for the memory.
  188. KSharedMemoryInfo* shemen_info = nullptr;
  189. const auto iter = std::find_if(
  190. m_shared_memory_list.begin(), m_shared_memory_list.end(),
  191. [shmem](const KSharedMemoryInfo* info) { return info->GetSharedMemory() == shmem; });
  192. if (iter != m_shared_memory_list.end()) {
  193. shemen_info = *iter;
  194. }
  195. if (shemen_info == nullptr) {
  196. shemen_info = KSharedMemoryInfo::Allocate(m_kernel);
  197. R_UNLESS(shemen_info != nullptr, ResultOutOfMemory);
  198. shemen_info->Initialize(shmem);
  199. m_shared_memory_list.push_back(shemen_info);
  200. }
  201. // Open a reference to the shared memory and its info.
  202. shmem->Open();
  203. shemen_info->Open();
  204. R_SUCCEED();
  205. }
  206. void KProcess::RemoveSharedMemory(KSharedMemory* shmem, [[maybe_unused]] KProcessAddress address,
  207. [[maybe_unused]] size_t size) {
  208. // Lock ourselves, to prevent concurrent access.
  209. KScopedLightLock lk(m_state_lock);
  210. KSharedMemoryInfo* shemen_info = nullptr;
  211. const auto iter = std::find_if(
  212. m_shared_memory_list.begin(), m_shared_memory_list.end(),
  213. [shmem](const KSharedMemoryInfo* info) { return info->GetSharedMemory() == shmem; });
  214. if (iter != m_shared_memory_list.end()) {
  215. shemen_info = *iter;
  216. }
  217. ASSERT(shemen_info != nullptr);
  218. if (shemen_info->Close()) {
  219. m_shared_memory_list.erase(iter);
  220. KSharedMemoryInfo::Free(m_kernel, shemen_info);
  221. }
  222. // Close a reference to the shared memory.
  223. shmem->Close();
  224. }
  225. void KProcess::RegisterThread(KThread* thread) {
  226. KScopedLightLock lk{m_list_lock};
  227. m_thread_list.push_back(thread);
  228. }
  229. void KProcess::UnregisterThread(KThread* thread) {
  230. KScopedLightLock lk{m_list_lock};
  231. m_thread_list.remove(thread);
  232. }
  233. u64 KProcess::GetFreeThreadCount() const {
  234. if (m_resource_limit != nullptr) {
  235. const auto current_value =
  236. m_resource_limit->GetCurrentValue(LimitableResource::ThreadCountMax);
  237. const auto limit_value = m_resource_limit->GetLimitValue(LimitableResource::ThreadCountMax);
  238. return limit_value - current_value;
  239. } else {
  240. return 0;
  241. }
  242. }
  243. Result KProcess::Reset() {
  244. // Lock the process and the scheduler.
  245. KScopedLightLock lk(m_state_lock);
  246. KScopedSchedulerLock sl{m_kernel};
  247. // Validate that we're in a state that we can reset.
  248. R_UNLESS(m_state != State::Terminated, ResultInvalidState);
  249. R_UNLESS(m_is_signaled, ResultInvalidState);
  250. // Clear signaled.
  251. m_is_signaled = false;
  252. R_SUCCEED();
  253. }
  254. Result KProcess::SetActivity(ProcessActivity activity) {
  255. // Lock ourselves and the scheduler.
  256. KScopedLightLock lk{m_state_lock};
  257. KScopedLightLock list_lk{m_list_lock};
  258. KScopedSchedulerLock sl{m_kernel};
  259. // Validate our state.
  260. R_UNLESS(m_state != State::Terminating, ResultInvalidState);
  261. R_UNLESS(m_state != State::Terminated, ResultInvalidState);
  262. // Either pause or resume.
  263. if (activity == ProcessActivity::Paused) {
  264. // Verify that we're not suspended.
  265. R_UNLESS(!m_is_suspended, ResultInvalidState);
  266. // Suspend all threads.
  267. for (auto* thread : this->GetThreadList()) {
  268. thread->RequestSuspend(SuspendType::Process);
  269. }
  270. // Set ourselves as suspended.
  271. this->SetSuspended(true);
  272. } else {
  273. ASSERT(activity == ProcessActivity::Runnable);
  274. // Verify that we're suspended.
  275. R_UNLESS(m_is_suspended, ResultInvalidState);
  276. // Resume all threads.
  277. for (auto* thread : this->GetThreadList()) {
  278. thread->Resume(SuspendType::Process);
  279. }
  280. // Set ourselves as resumed.
  281. this->SetSuspended(false);
  282. }
  283. R_SUCCEED();
  284. }
  285. Result KProcess::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, std::size_t code_size,
  286. bool is_hbl) {
  287. m_program_id = metadata.GetTitleID();
  288. m_ideal_core = metadata.GetMainThreadCore();
  289. m_is_64bit_process = metadata.Is64BitProgram();
  290. m_system_resource_size = metadata.GetSystemResourceSize();
  291. m_image_size = code_size;
  292. m_is_hbl = is_hbl;
  293. if (metadata.GetAddressSpaceType() == FileSys::ProgramAddressSpaceType::Is39Bit) {
  294. // For 39-bit processes, the ASLR region starts at 0x800'0000 and is ~512GiB large.
  295. // However, some (buggy) programs/libraries like skyline incorrectly depend on the
  296. // existence of ASLR pages before the entry point, so we will adjust the load address
  297. // to point to about 2GiB into the ASLR region.
  298. m_code_address = 0x8000'0000;
  299. } else {
  300. // All other processes can be mapped at the beginning of the code region.
  301. if (metadata.GetAddressSpaceType() == FileSys::ProgramAddressSpaceType::Is36Bit) {
  302. m_code_address = 0x800'0000;
  303. } else {
  304. m_code_address = 0x20'0000;
  305. }
  306. }
  307. KScopedResourceReservation memory_reservation(
  308. m_resource_limit, LimitableResource::PhysicalMemoryMax, code_size + m_system_resource_size);
  309. if (!memory_reservation.Succeeded()) {
  310. LOG_ERROR(Kernel, "Could not reserve process memory requirements of size {:X} bytes",
  311. code_size + m_system_resource_size);
  312. R_RETURN(ResultLimitReached);
  313. }
  314. // Initialize process address space
  315. if (const Result result{m_page_table.InitializeForProcess(
  316. metadata.GetAddressSpaceType(), false, false, false, KMemoryManager::Pool::Application,
  317. this->GetEntryPoint(), code_size, std::addressof(m_kernel.GetAppSystemResource()),
  318. m_resource_limit, m_kernel.System().ApplicationMemory())};
  319. result.IsError()) {
  320. R_RETURN(result);
  321. }
  322. // Map process code region
  323. if (const Result result{m_page_table.MapProcessCode(this->GetEntryPoint(), code_size / PageSize,
  324. KMemoryState::Code,
  325. KMemoryPermission::None)};
  326. result.IsError()) {
  327. R_RETURN(result);
  328. }
  329. // Initialize process capabilities
  330. const auto& caps{metadata.GetKernelCapabilities()};
  331. if (const Result result{
  332. m_capabilities.InitializeForUserProcess(caps.data(), caps.size(), m_page_table)};
  333. result.IsError()) {
  334. R_RETURN(result);
  335. }
  336. // Set memory usage capacity
  337. switch (metadata.GetAddressSpaceType()) {
  338. case FileSys::ProgramAddressSpaceType::Is32Bit:
  339. case FileSys::ProgramAddressSpaceType::Is36Bit:
  340. case FileSys::ProgramAddressSpaceType::Is39Bit:
  341. m_memory_usage_capacity =
  342. m_page_table.GetHeapRegionEnd() - m_page_table.GetHeapRegionStart();
  343. break;
  344. case FileSys::ProgramAddressSpaceType::Is32BitNoMap:
  345. m_memory_usage_capacity =
  346. (m_page_table.GetHeapRegionEnd() - m_page_table.GetHeapRegionStart()) +
  347. (m_page_table.GetAliasRegionEnd() - m_page_table.GetAliasRegionStart());
  348. break;
  349. default:
  350. ASSERT(false);
  351. break;
  352. }
  353. // Create TLS region
  354. R_TRY(this->CreateThreadLocalRegion(std::addressof(m_plr_address)));
  355. memory_reservation.Commit();
  356. R_RETURN(m_handle_table.Initialize(m_capabilities.GetHandleTableSize()));
  357. }
  358. void KProcess::Run(s32 main_thread_priority, u64 stack_size) {
  359. ASSERT(this->AllocateMainThreadStack(stack_size) == ResultSuccess);
  360. m_resource_limit->Reserve(LimitableResource::ThreadCountMax, 1);
  361. const std::size_t heap_capacity{m_memory_usage_capacity -
  362. (m_main_thread_stack_size + m_image_size)};
  363. ASSERT(!m_page_table.SetMaxHeapSize(heap_capacity).IsError());
  364. this->ChangeState(State::Running);
  365. SetupMainThread(m_kernel.System(), *this, main_thread_priority, m_main_thread_stack_top);
  366. }
  367. void KProcess::PrepareForTermination() {
  368. this->ChangeState(State::Terminating);
  369. const auto stop_threads = [this](const std::vector<KThread*>& in_thread_list) {
  370. for (auto* thread : in_thread_list) {
  371. if (thread->GetOwnerProcess() != this)
  372. continue;
  373. if (thread == GetCurrentThreadPointer(m_kernel))
  374. continue;
  375. // TODO(Subv): When are the other running/ready threads terminated?
  376. ASSERT_MSG(thread->GetState() == ThreadState::Waiting,
  377. "Exiting processes with non-waiting threads is currently unimplemented");
  378. thread->Exit();
  379. }
  380. };
  381. stop_threads(m_kernel.System().GlobalSchedulerContext().GetThreadList());
  382. this->DeleteThreadLocalRegion(m_plr_address);
  383. m_plr_address = 0;
  384. if (m_resource_limit) {
  385. m_resource_limit->Release(LimitableResource::PhysicalMemoryMax,
  386. m_main_thread_stack_size + m_image_size);
  387. }
  388. this->ChangeState(State::Terminated);
  389. }
  390. void KProcess::Finalize() {
  391. // Free all shared memory infos.
  392. {
  393. auto it = m_shared_memory_list.begin();
  394. while (it != m_shared_memory_list.end()) {
  395. KSharedMemoryInfo* info = *it;
  396. KSharedMemory* shmem = info->GetSharedMemory();
  397. while (!info->Close()) {
  398. shmem->Close();
  399. }
  400. shmem->Close();
  401. it = m_shared_memory_list.erase(it);
  402. KSharedMemoryInfo::Free(m_kernel, info);
  403. }
  404. }
  405. // Release memory to the resource limit.
  406. if (m_resource_limit != nullptr) {
  407. m_resource_limit->Close();
  408. m_resource_limit = nullptr;
  409. }
  410. // Finalize the page table.
  411. m_page_table.Finalize();
  412. // Perform inherited finalization.
  413. KSynchronizationObject::Finalize();
  414. }
  415. Result KProcess::CreateThreadLocalRegion(KProcessAddress* out) {
  416. KThreadLocalPage* tlp = nullptr;
  417. KProcessAddress tlr = 0;
  418. // See if we can get a region from a partially used TLP.
  419. {
  420. KScopedSchedulerLock sl{m_kernel};
  421. if (auto it = m_partially_used_tlp_tree.begin(); it != m_partially_used_tlp_tree.end()) {
  422. tlr = it->Reserve();
  423. ASSERT(tlr != 0);
  424. if (it->IsAllUsed()) {
  425. tlp = std::addressof(*it);
  426. m_partially_used_tlp_tree.erase(it);
  427. m_fully_used_tlp_tree.insert(*tlp);
  428. }
  429. *out = tlr;
  430. R_SUCCEED();
  431. }
  432. }
  433. // Allocate a new page.
  434. tlp = KThreadLocalPage::Allocate(m_kernel);
  435. R_UNLESS(tlp != nullptr, ResultOutOfMemory);
  436. auto tlp_guard = SCOPE_GUARD({ KThreadLocalPage::Free(m_kernel, tlp); });
  437. // Initialize the new page.
  438. R_TRY(tlp->Initialize(m_kernel, this));
  439. // Reserve a TLR.
  440. tlr = tlp->Reserve();
  441. ASSERT(tlr != 0);
  442. // Insert into our tree.
  443. {
  444. KScopedSchedulerLock sl{m_kernel};
  445. if (tlp->IsAllUsed()) {
  446. m_fully_used_tlp_tree.insert(*tlp);
  447. } else {
  448. m_partially_used_tlp_tree.insert(*tlp);
  449. }
  450. }
  451. // We succeeded!
  452. tlp_guard.Cancel();
  453. *out = tlr;
  454. R_SUCCEED();
  455. }
  456. Result KProcess::DeleteThreadLocalRegion(KProcessAddress addr) {
  457. KThreadLocalPage* page_to_free = nullptr;
  458. // Release the region.
  459. {
  460. KScopedSchedulerLock sl{m_kernel};
  461. // Try to find the page in the partially used list.
  462. auto it = m_partially_used_tlp_tree.find_key(Common::AlignDown(GetInteger(addr), PageSize));
  463. if (it == m_partially_used_tlp_tree.end()) {
  464. // If we don't find it, it has to be in the fully used list.
  465. it = m_fully_used_tlp_tree.find_key(Common::AlignDown(GetInteger(addr), PageSize));
  466. R_UNLESS(it != m_fully_used_tlp_tree.end(), ResultInvalidAddress);
  467. // Release the region.
  468. it->Release(addr);
  469. // Move the page out of the fully used list.
  470. KThreadLocalPage* tlp = std::addressof(*it);
  471. m_fully_used_tlp_tree.erase(it);
  472. if (tlp->IsAllFree()) {
  473. page_to_free = tlp;
  474. } else {
  475. m_partially_used_tlp_tree.insert(*tlp);
  476. }
  477. } else {
  478. // Release the region.
  479. it->Release(addr);
  480. // Handle the all-free case.
  481. KThreadLocalPage* tlp = std::addressof(*it);
  482. if (tlp->IsAllFree()) {
  483. m_partially_used_tlp_tree.erase(it);
  484. page_to_free = tlp;
  485. }
  486. }
  487. }
  488. // If we should free the page it was in, do so.
  489. if (page_to_free != nullptr) {
  490. page_to_free->Finalize();
  491. KThreadLocalPage::Free(m_kernel, page_to_free);
  492. }
  493. R_SUCCEED();
  494. }
  495. bool KProcess::InsertWatchpoint(KProcessAddress addr, u64 size, DebugWatchpointType type) {
  496. const auto watch{std::find_if(m_watchpoints.begin(), m_watchpoints.end(), [&](const auto& wp) {
  497. return wp.type == DebugWatchpointType::None;
  498. })};
  499. if (watch == m_watchpoints.end()) {
  500. return false;
  501. }
  502. watch->start_address = addr;
  503. watch->end_address = addr + size;
  504. watch->type = type;
  505. for (KProcessAddress page = Common::AlignDown(GetInteger(addr), PageSize); page < addr + size;
  506. page += PageSize) {
  507. m_debug_page_refcounts[page]++;
  508. this->GetMemory().MarkRegionDebug(page, PageSize, true);
  509. }
  510. return true;
  511. }
  512. bool KProcess::RemoveWatchpoint(KProcessAddress addr, u64 size, DebugWatchpointType type) {
  513. const auto watch{std::find_if(m_watchpoints.begin(), m_watchpoints.end(), [&](const auto& wp) {
  514. return wp.start_address == addr && wp.end_address == addr + size && wp.type == type;
  515. })};
  516. if (watch == m_watchpoints.end()) {
  517. return false;
  518. }
  519. watch->start_address = 0;
  520. watch->end_address = 0;
  521. watch->type = DebugWatchpointType::None;
  522. for (KProcessAddress page = Common::AlignDown(GetInteger(addr), PageSize); page < addr + size;
  523. page += PageSize) {
  524. m_debug_page_refcounts[page]--;
  525. if (!m_debug_page_refcounts[page]) {
  526. this->GetMemory().MarkRegionDebug(page, PageSize, false);
  527. }
  528. }
  529. return true;
  530. }
  531. void KProcess::LoadModule(CodeSet code_set, KProcessAddress base_addr) {
  532. const auto ReprotectSegment = [&](const CodeSet::Segment& segment,
  533. Svc::MemoryPermission permission) {
  534. m_page_table.SetProcessMemoryPermission(segment.addr + base_addr, segment.size, permission);
  535. };
  536. this->GetMemory().WriteBlock(base_addr, code_set.memory.data(), code_set.memory.size());
  537. ReprotectSegment(code_set.CodeSegment(), Svc::MemoryPermission::ReadExecute);
  538. ReprotectSegment(code_set.RODataSegment(), Svc::MemoryPermission::Read);
  539. ReprotectSegment(code_set.DataSegment(), Svc::MemoryPermission::ReadWrite);
  540. }
  541. bool KProcess::IsSignaled() const {
  542. ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));
  543. return m_is_signaled;
  544. }
  545. KProcess::KProcess(KernelCore& kernel)
  546. : KAutoObjectWithSlabHeapAndContainer{kernel}, m_page_table{m_kernel.System()},
  547. m_handle_table{m_kernel}, m_address_arbiter{m_kernel.System()},
  548. m_condition_var{m_kernel.System()}, m_state_lock{m_kernel}, m_list_lock{m_kernel} {}
  549. KProcess::~KProcess() = default;
  550. void KProcess::ChangeState(State new_state) {
  551. if (m_state == new_state) {
  552. return;
  553. }
  554. m_state = new_state;
  555. m_is_signaled = true;
  556. this->NotifyAvailable();
  557. }
  558. Result KProcess::AllocateMainThreadStack(std::size_t stack_size) {
  559. // Ensure that we haven't already allocated stack.
  560. ASSERT(m_main_thread_stack_size == 0);
  561. // Ensure that we're allocating a valid stack.
  562. stack_size = Common::AlignUp(stack_size, PageSize);
  563. // R_UNLESS(stack_size + image_size <= m_max_process_memory, ResultOutOfMemory);
  564. R_UNLESS(stack_size + m_image_size >= m_image_size, ResultOutOfMemory);
  565. // Place a tentative reservation of memory for our new stack.
  566. KScopedResourceReservation mem_reservation(this, Svc::LimitableResource::PhysicalMemoryMax,
  567. stack_size);
  568. R_UNLESS(mem_reservation.Succeeded(), ResultLimitReached);
  569. // Allocate and map our stack.
  570. if (stack_size) {
  571. KProcessAddress stack_bottom;
  572. R_TRY(m_page_table.MapPages(std::addressof(stack_bottom), stack_size / PageSize,
  573. KMemoryState::Stack, KMemoryPermission::UserReadWrite));
  574. m_main_thread_stack_top = stack_bottom + stack_size;
  575. m_main_thread_stack_size = stack_size;
  576. }
  577. // We succeeded! Commit our memory reservation.
  578. mem_reservation.Commit();
  579. R_SUCCEED();
  580. }
  581. Core::Memory::Memory& KProcess::GetMemory() const {
  582. // TODO: per-process memory
  583. return m_kernel.System().ApplicationMemory();
  584. }
  585. } // namespace Kernel