process.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <bitset>
  6. #include <memory>
  7. #include <random>
  8. #include "common/alignment.h"
  9. #include "common/assert.h"
  10. #include "common/logging/log.h"
  11. #include "core/core.h"
  12. #include "core/file_sys/program_metadata.h"
  13. #include "core/hle/kernel/code_set.h"
  14. #include "core/hle/kernel/errors.h"
  15. #include "core/hle/kernel/kernel.h"
  16. #include "core/hle/kernel/process.h"
  17. #include "core/hle/kernel/resource_limit.h"
  18. #include "core/hle/kernel/scheduler.h"
  19. #include "core/hle/kernel/thread.h"
  20. #include "core/hle/kernel/vm_manager.h"
  21. #include "core/memory.h"
  22. #include "core/settings.h"
  23. namespace Kernel {
  24. namespace {
  25. /**
  26. * Sets up the primary application thread
  27. *
  28. * @param owner_process The parent process for the main thread
  29. * @param kernel The kernel instance to create the main thread under.
  30. * @param priority The priority to give the main thread
  31. */
  32. void SetupMainThread(Process& owner_process, KernelCore& kernel, u32 priority) {
  33. const auto& vm_manager = owner_process.VMManager();
  34. const VAddr entry_point = vm_manager.GetCodeRegionBaseAddress();
  35. const VAddr stack_top = vm_manager.GetTLSIORegionEndAddress();
  36. auto thread_res = Thread::Create(kernel, "main", entry_point, priority, 0,
  37. owner_process.GetIdealCore(), stack_top, owner_process);
  38. std::shared_ptr<Thread> thread = std::move(thread_res).Unwrap();
  39. // Register 1 must be a handle to the main thread
  40. const Handle thread_handle = owner_process.GetHandleTable().Create(thread).Unwrap();
  41. thread->GetContext().cpu_registers[1] = thread_handle;
  42. // Threads by default are dormant, wake up the main thread so it runs when the scheduler fires
  43. thread->ResumeFromWait();
  44. }
  45. } // Anonymous namespace
  46. // Represents a page used for thread-local storage.
  47. //
  48. // Each TLS page contains slots that may be used by processes and threads.
  49. // Every process and thread is created with a slot in some arbitrary page
  50. // (whichever page happens to have an available slot).
  51. class TLSPage {
  52. public:
  53. static constexpr std::size_t num_slot_entries = Memory::PAGE_SIZE / Memory::TLS_ENTRY_SIZE;
  54. explicit TLSPage(VAddr address) : base_address{address} {}
  55. bool HasAvailableSlots() const {
  56. return !is_slot_used.all();
  57. }
  58. VAddr GetBaseAddress() const {
  59. return base_address;
  60. }
  61. std::optional<VAddr> ReserveSlot() {
  62. for (std::size_t i = 0; i < is_slot_used.size(); i++) {
  63. if (is_slot_used[i]) {
  64. continue;
  65. }
  66. is_slot_used[i] = true;
  67. return base_address + (i * Memory::TLS_ENTRY_SIZE);
  68. }
  69. return std::nullopt;
  70. }
  71. void ReleaseSlot(VAddr address) {
  72. // Ensure that all given addresses are consistent with how TLS pages
  73. // are intended to be used when releasing slots.
  74. ASSERT(IsWithinPage(address));
  75. ASSERT((address % Memory::TLS_ENTRY_SIZE) == 0);
  76. const std::size_t index = (address - base_address) / Memory::TLS_ENTRY_SIZE;
  77. is_slot_used[index] = false;
  78. }
  79. private:
  80. bool IsWithinPage(VAddr address) const {
  81. return base_address <= address && address < base_address + Memory::PAGE_SIZE;
  82. }
  83. VAddr base_address;
  84. std::bitset<num_slot_entries> is_slot_used;
  85. };
  86. std::shared_ptr<Process> Process::Create(Core::System& system, std::string name, ProcessType type) {
  87. auto& kernel = system.Kernel();
  88. std::shared_ptr<Process> process = std::make_shared<Process>(system);
  89. process->name = std::move(name);
  90. process->resource_limit = kernel.GetSystemResourceLimit();
  91. process->status = ProcessStatus::Created;
  92. process->program_id = 0;
  93. process->process_id = type == ProcessType::KernelInternal ? kernel.CreateNewKernelProcessID()
  94. : kernel.CreateNewUserProcessID();
  95. process->capabilities.InitializeForMetadatalessProcess();
  96. std::mt19937 rng(Settings::values.rng_seed.value_or(0));
  97. std::uniform_int_distribution<u64> distribution;
  98. std::generate(process->random_entropy.begin(), process->random_entropy.end(),
  99. [&] { return distribution(rng); });
  100. kernel.AppendNewProcess(process);
  101. return process;
  102. }
  103. std::shared_ptr<ResourceLimit> Process::GetResourceLimit() const {
  104. return resource_limit;
  105. }
  106. u64 Process::GetTotalPhysicalMemoryAvailable() const {
  107. return vm_manager.GetTotalPhysicalMemoryAvailable();
  108. }
  109. u64 Process::GetTotalPhysicalMemoryAvailableWithoutSystemResource() const {
  110. return GetTotalPhysicalMemoryAvailable() - GetSystemResourceSize();
  111. }
  112. u64 Process::GetTotalPhysicalMemoryUsed() const {
  113. return vm_manager.GetCurrentHeapSize() + main_thread_stack_size + code_memory_size +
  114. GetSystemResourceUsage();
  115. }
  116. u64 Process::GetTotalPhysicalMemoryUsedWithoutSystemResource() const {
  117. return GetTotalPhysicalMemoryUsed() - GetSystemResourceUsage();
  118. }
  119. void Process::InsertConditionVariableThread(std::shared_ptr<Thread> thread) {
  120. VAddr cond_var_addr = thread->GetCondVarWaitAddress();
  121. std::list<std::shared_ptr<Thread>>& thread_list = cond_var_threads[cond_var_addr];
  122. auto it = thread_list.begin();
  123. while (it != thread_list.end()) {
  124. const std::shared_ptr<Thread> current_thread = *it;
  125. if (current_thread->GetPriority() > thread->GetPriority()) {
  126. thread_list.insert(it, thread);
  127. return;
  128. }
  129. ++it;
  130. }
  131. thread_list.push_back(thread);
  132. }
  133. void Process::RemoveConditionVariableThread(std::shared_ptr<Thread> thread) {
  134. VAddr cond_var_addr = thread->GetCondVarWaitAddress();
  135. std::list<std::shared_ptr<Thread>>& thread_list = cond_var_threads[cond_var_addr];
  136. auto it = thread_list.begin();
  137. while (it != thread_list.end()) {
  138. const std::shared_ptr<Thread> current_thread = *it;
  139. if (current_thread.get() == thread.get()) {
  140. thread_list.erase(it);
  141. return;
  142. }
  143. ++it;
  144. }
  145. UNREACHABLE();
  146. }
  147. std::vector<std::shared_ptr<Thread>> Process::GetConditionVariableThreads(
  148. const VAddr cond_var_addr) {
  149. std::vector<std::shared_ptr<Thread>> result{};
  150. std::list<std::shared_ptr<Thread>>& thread_list = cond_var_threads[cond_var_addr];
  151. auto it = thread_list.begin();
  152. while (it != thread_list.end()) {
  153. std::shared_ptr<Thread> current_thread = *it;
  154. result.push_back(current_thread);
  155. ++it;
  156. }
  157. return result;
  158. }
  159. void Process::RegisterThread(const Thread* thread) {
  160. thread_list.push_back(thread);
  161. }
  162. void Process::UnregisterThread(const Thread* thread) {
  163. thread_list.remove(thread);
  164. }
  165. ResultCode Process::ClearSignalState() {
  166. if (status == ProcessStatus::Exited) {
  167. LOG_ERROR(Kernel, "called on a terminated process instance.");
  168. return ERR_INVALID_STATE;
  169. }
  170. if (!is_signaled) {
  171. LOG_ERROR(Kernel, "called on a process instance that isn't signaled.");
  172. return ERR_INVALID_STATE;
  173. }
  174. is_signaled = false;
  175. return RESULT_SUCCESS;
  176. }
  177. ResultCode Process::LoadFromMetadata(const FileSys::ProgramMetadata& metadata) {
  178. program_id = metadata.GetTitleID();
  179. ideal_core = metadata.GetMainThreadCore();
  180. is_64bit_process = metadata.Is64BitProgram();
  181. system_resource_size = metadata.GetSystemResourceSize();
  182. vm_manager.Reset(metadata.GetAddressSpaceType());
  183. const auto& caps = metadata.GetKernelCapabilities();
  184. const auto capability_init_result =
  185. capabilities.InitializeForUserProcess(caps.data(), caps.size(), vm_manager);
  186. if (capability_init_result.IsError()) {
  187. return capability_init_result;
  188. }
  189. return handle_table.SetSize(capabilities.GetHandleTableSize());
  190. }
  191. void Process::Run(s32 main_thread_priority, u64 stack_size) {
  192. AllocateMainThreadStack(stack_size);
  193. tls_region_address = CreateTLSRegion();
  194. vm_manager.LogLayout();
  195. ChangeStatus(ProcessStatus::Running);
  196. SetupMainThread(*this, kernel, main_thread_priority);
  197. }
  198. void Process::PrepareForTermination() {
  199. ChangeStatus(ProcessStatus::Exiting);
  200. const auto stop_threads = [this](const std::vector<std::shared_ptr<Thread>>& thread_list) {
  201. for (auto& thread : thread_list) {
  202. if (thread->GetOwnerProcess() != this)
  203. continue;
  204. if (thread.get() == system.CurrentScheduler().GetCurrentThread())
  205. continue;
  206. // TODO(Subv): When are the other running/ready threads terminated?
  207. ASSERT_MSG(thread->GetStatus() == ThreadStatus::WaitSynch,
  208. "Exiting processes with non-waiting threads is currently unimplemented");
  209. thread->Stop();
  210. }
  211. };
  212. stop_threads(system.GlobalScheduler().GetThreadList());
  213. FreeTLSRegion(tls_region_address);
  214. tls_region_address = 0;
  215. ChangeStatus(ProcessStatus::Exited);
  216. }
  217. /**
  218. * Attempts to find a TLS page that contains a free slot for
  219. * use by a thread.
  220. *
  221. * @returns If a page with an available slot is found, then an iterator
  222. * pointing to the page is returned. Otherwise the end iterator
  223. * is returned instead.
  224. */
  225. static auto FindTLSPageWithAvailableSlots(std::vector<TLSPage>& tls_pages) {
  226. return std::find_if(tls_pages.begin(), tls_pages.end(),
  227. [](const auto& page) { return page.HasAvailableSlots(); });
  228. }
  229. VAddr Process::CreateTLSRegion() {
  230. auto tls_page_iter = FindTLSPageWithAvailableSlots(tls_pages);
  231. if (tls_page_iter == tls_pages.cend()) {
  232. const auto region_address =
  233. vm_manager.FindFreeRegion(vm_manager.GetTLSIORegionBaseAddress(),
  234. vm_manager.GetTLSIORegionEndAddress(), Memory::PAGE_SIZE);
  235. ASSERT(region_address.Succeeded());
  236. const auto map_result = vm_manager.MapMemoryBlock(
  237. *region_address, std::make_shared<PhysicalMemory>(Memory::PAGE_SIZE), 0,
  238. Memory::PAGE_SIZE, MemoryState::ThreadLocal);
  239. ASSERT(map_result.Succeeded());
  240. tls_pages.emplace_back(*region_address);
  241. const auto reserve_result = tls_pages.back().ReserveSlot();
  242. ASSERT(reserve_result.has_value());
  243. return *reserve_result;
  244. }
  245. return *tls_page_iter->ReserveSlot();
  246. }
  247. void Process::FreeTLSRegion(VAddr tls_address) {
  248. const VAddr aligned_address = Common::AlignDown(tls_address, Memory::PAGE_SIZE);
  249. auto iter =
  250. std::find_if(tls_pages.begin(), tls_pages.end(), [aligned_address](const auto& page) {
  251. return page.GetBaseAddress() == aligned_address;
  252. });
  253. // Something has gone very wrong if we're freeing a region
  254. // with no actual page available.
  255. ASSERT(iter != tls_pages.cend());
  256. iter->ReleaseSlot(tls_address);
  257. }
  258. void Process::LoadModule(CodeSet module_, VAddr base_addr) {
  259. code_memory_size += module_.memory.size();
  260. const auto memory = std::make_shared<PhysicalMemory>(std::move(module_.memory));
  261. const auto MapSegment = [&](const CodeSet::Segment& segment, VMAPermission permissions,
  262. MemoryState memory_state) {
  263. const auto vma = vm_manager
  264. .MapMemoryBlock(segment.addr + base_addr, memory, segment.offset,
  265. segment.size, memory_state)
  266. .Unwrap();
  267. vm_manager.Reprotect(vma, permissions);
  268. };
  269. // Map CodeSet segments
  270. MapSegment(module_.CodeSegment(), VMAPermission::ReadExecute, MemoryState::Code);
  271. MapSegment(module_.RODataSegment(), VMAPermission::Read, MemoryState::CodeData);
  272. MapSegment(module_.DataSegment(), VMAPermission::ReadWrite, MemoryState::CodeData);
  273. }
  274. Process::Process(Core::System& system)
  275. : WaitObject{system.Kernel()}, vm_manager{system},
  276. address_arbiter{system}, mutex{system}, system{system} {}
  277. Process::~Process() = default;
  278. void Process::Acquire(Thread* thread) {
  279. ASSERT_MSG(!ShouldWait(thread), "Object unavailable!");
  280. }
  281. bool Process::ShouldWait(const Thread* thread) const {
  282. return !is_signaled;
  283. }
  284. void Process::ChangeStatus(ProcessStatus new_status) {
  285. if (status == new_status) {
  286. return;
  287. }
  288. status = new_status;
  289. is_signaled = true;
  290. WakeupAllWaitingThreads();
  291. }
  292. void Process::AllocateMainThreadStack(u64 stack_size) {
  293. // The kernel always ensures that the given stack size is page aligned.
  294. main_thread_stack_size = Common::AlignUp(stack_size, Memory::PAGE_SIZE);
  295. // Allocate and map the main thread stack
  296. const VAddr mapping_address = vm_manager.GetTLSIORegionEndAddress() - main_thread_stack_size;
  297. vm_manager
  298. .MapMemoryBlock(mapping_address, std::make_shared<PhysicalMemory>(main_thread_stack_size),
  299. 0, main_thread_stack_size, MemoryState::Stack)
  300. .Unwrap();
  301. }
  302. } // namespace Kernel