process.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <bitset>
  7. #include <cstddef>
  8. #include <list>
  9. #include <string>
  10. #include <vector>
  11. #include <boost/container/static_vector.hpp>
  12. #include "common/common_types.h"
  13. #include "core/hle/kernel/address_arbiter.h"
  14. #include "core/hle/kernel/handle_table.h"
  15. #include "core/hle/kernel/mutex.h"
  16. #include "core/hle/kernel/process_capability.h"
  17. #include "core/hle/kernel/vm_manager.h"
  18. #include "core/hle/kernel/wait_object.h"
  19. #include "core/hle/result.h"
  20. namespace Core {
  21. class System;
  22. }
  23. namespace FileSys {
  24. class ProgramMetadata;
  25. }
  26. namespace Kernel {
  27. class KernelCore;
  28. class ResourceLimit;
  29. class Thread;
  30. struct CodeSet;
  31. enum class MemoryRegion : u16 {
  32. APPLICATION = 1,
  33. SYSTEM = 2,
  34. BASE = 3,
  35. };
  36. /**
  37. * Indicates the status of a Process instance.
  38. *
  39. * @note These match the values as used by kernel,
  40. * so new entries should only be added if RE
  41. * shows that a new value has been introduced.
  42. */
  43. enum class ProcessStatus {
  44. Created,
  45. CreatedWithDebuggerAttached,
  46. Running,
  47. WaitingForDebuggerToAttach,
  48. DebuggerAttached,
  49. Exiting,
  50. Exited,
  51. DebugBreak,
  52. };
  53. class Process final : public WaitObject {
  54. public:
  55. enum : u64 {
  56. /// Lowest allowed process ID for a kernel initial process.
  57. InitialKIPIDMin = 1,
  58. /// Highest allowed process ID for a kernel initial process.
  59. InitialKIPIDMax = 80,
  60. /// Lowest allowed process ID for a userland process.
  61. ProcessIDMin = 81,
  62. /// Highest allowed process ID for a userland process.
  63. ProcessIDMax = 0xFFFFFFFFFFFFFFFF,
  64. };
  65. static constexpr std::size_t RANDOM_ENTROPY_SIZE = 4;
  66. static SharedPtr<Process> Create(Core::System& system, std::string&& name);
  67. std::string GetTypeName() const override {
  68. return "Process";
  69. }
  70. std::string GetName() const override {
  71. return name;
  72. }
  73. static const HandleType HANDLE_TYPE = HandleType::Process;
  74. HandleType GetHandleType() const override {
  75. return HANDLE_TYPE;
  76. }
  77. /// Gets a reference to the process' memory manager.
  78. Kernel::VMManager& VMManager() {
  79. return vm_manager;
  80. }
  81. /// Gets a const reference to the process' memory manager.
  82. const Kernel::VMManager& VMManager() const {
  83. return vm_manager;
  84. }
  85. /// Gets a reference to the process' handle table.
  86. HandleTable& GetHandleTable() {
  87. return handle_table;
  88. }
  89. /// Gets a const reference to the process' handle table.
  90. const HandleTable& GetHandleTable() const {
  91. return handle_table;
  92. }
  93. /// Gets a reference to the process' address arbiter.
  94. AddressArbiter& GetAddressArbiter() {
  95. return address_arbiter;
  96. }
  97. /// Gets a const reference to the process' address arbiter.
  98. const AddressArbiter& GetAddressArbiter() const {
  99. return address_arbiter;
  100. }
  101. /// Gets a reference to the process' mutex lock.
  102. Mutex& GetMutex() {
  103. return mutex;
  104. }
  105. /// Gets a const reference to the process' mutex lock
  106. const Mutex& GetMutex() const {
  107. return mutex;
  108. }
  109. /// Gets the current status of the process
  110. ProcessStatus GetStatus() const {
  111. return status;
  112. }
  113. /// Gets the unique ID that identifies this particular process.
  114. u64 GetProcessID() const {
  115. return process_id;
  116. }
  117. /// Gets the title ID corresponding to this process.
  118. u64 GetTitleID() const {
  119. return program_id;
  120. }
  121. /// Gets the resource limit descriptor for this process
  122. SharedPtr<ResourceLimit> GetResourceLimit() const;
  123. /// Gets the ideal CPU core ID for this process
  124. u8 GetIdealCore() const {
  125. return ideal_core;
  126. }
  127. /// Gets the bitmask of allowed cores that this process' threads can run on.
  128. u64 GetCoreMask() const {
  129. return capabilities.GetCoreMask();
  130. }
  131. /// Gets the bitmask of allowed thread priorities.
  132. u64 GetPriorityMask() const {
  133. return capabilities.GetPriorityMask();
  134. }
  135. u32 IsVirtualMemoryEnabled() const {
  136. return is_virtual_address_memory_enabled;
  137. }
  138. /// Whether this process is an AArch64 or AArch32 process.
  139. bool Is64BitProcess() const {
  140. return is_64bit_process;
  141. }
  142. /// Gets the total running time of the process instance in ticks.
  143. u64 GetCPUTimeTicks() const {
  144. return total_process_running_time_ticks;
  145. }
  146. /// Updates the total running time, adding the given ticks to it.
  147. void UpdateCPUTimeTicks(u64 ticks) {
  148. total_process_running_time_ticks += ticks;
  149. }
  150. /// Gets 8 bytes of random data for svcGetInfo RandomEntropy
  151. u64 GetRandomEntropy(std::size_t index) const {
  152. return random_entropy.at(index);
  153. }
  154. /// Retrieves the total physical memory used by this process in bytes.
  155. u64 GetTotalPhysicalMemoryUsed() const;
  156. /// Gets the list of all threads created with this process as their owner.
  157. const std::list<const Thread*>& GetThreadList() const {
  158. return thread_list;
  159. }
  160. /// Registers a thread as being created under this process,
  161. /// adding it to this process' thread list.
  162. void RegisterThread(const Thread* thread);
  163. /// Unregisters a thread from this process, removing it
  164. /// from this process' thread list.
  165. void UnregisterThread(const Thread* thread);
  166. /// Clears the signaled state of the process if and only if it's signaled.
  167. ///
  168. /// @pre The process must not be already terminated. If this is called on a
  169. /// terminated process, then ERR_INVALID_STATE will be returned.
  170. ///
  171. /// @pre The process must be in a signaled state. If this is called on a
  172. /// process instance that is not signaled, ERR_INVALID_STATE will be
  173. /// returned.
  174. ResultCode ClearSignalState();
  175. /**
  176. * Loads process-specifics configuration info with metadata provided
  177. * by an executable.
  178. *
  179. * @param metadata The provided metadata to load process specific info from.
  180. *
  181. * @returns RESULT_SUCCESS if all relevant metadata was able to be
  182. * loaded and parsed. Otherwise, an error code is returned.
  183. */
  184. ResultCode LoadFromMetadata(const FileSys::ProgramMetadata& metadata);
  185. /**
  186. * Applies address space changes and launches the process main thread.
  187. */
  188. void Run(VAddr entry_point, s32 main_thread_priority, u64 stack_size);
  189. /**
  190. * Prepares a process for termination by stopping all of its threads
  191. * and clearing any other resources.
  192. */
  193. void PrepareForTermination();
  194. void LoadModule(CodeSet module_, VAddr base_addr);
  195. ///////////////////////////////////////////////////////////////////////////////////////////////
  196. // Thread-local storage management
  197. // Marks the next available region as used and returns the address of the slot.
  198. VAddr MarkNextAvailableTLSSlotAsUsed(Thread& thread);
  199. // Frees a used TLS slot identified by the given address
  200. void FreeTLSSlot(VAddr tls_address);
  201. private:
  202. explicit Process(Core::System& system);
  203. ~Process() override;
  204. /// Checks if the specified thread should wait until this process is available.
  205. bool ShouldWait(const Thread* thread) const override;
  206. /// Acquires/locks this process for the specified thread if it's available.
  207. void Acquire(Thread* thread) override;
  208. /// Changes the process status. If the status is different
  209. /// from the current process status, then this will trigger
  210. /// a process signal.
  211. void ChangeStatus(ProcessStatus new_status);
  212. /// Memory manager for this process.
  213. Kernel::VMManager vm_manager;
  214. /// Size of the main thread's stack in bytes.
  215. u64 main_thread_stack_size = 0;
  216. /// Size of the loaded code memory in bytes.
  217. u64 code_memory_size = 0;
  218. /// Current status of the process
  219. ProcessStatus status;
  220. /// The ID of this process
  221. u64 process_id = 0;
  222. /// Title ID corresponding to the process
  223. u64 program_id = 0;
  224. /// Resource limit descriptor for this process
  225. SharedPtr<ResourceLimit> resource_limit;
  226. /// The ideal CPU core for this process, threads are scheduled on this core by default.
  227. u8 ideal_core = 0;
  228. u32 is_virtual_address_memory_enabled = 0;
  229. /// The Thread Local Storage area is allocated as processes create threads,
  230. /// each TLS area is 0x200 bytes, so one page (0x1000) is split up in 8 parts, and each part
  231. /// holds the TLS for a specific thread. This vector contains which parts are in use for each
  232. /// page as a bitmask.
  233. /// This vector will grow as more pages are allocated for new threads.
  234. std::vector<std::bitset<8>> tls_slots;
  235. /// Contains the parsed process capability descriptors.
  236. ProcessCapabilities capabilities;
  237. /// Whether or not this process is AArch64, or AArch32.
  238. /// By default, we currently assume this is true, unless otherwise
  239. /// specified by metadata provided to the process during loading.
  240. bool is_64bit_process = true;
  241. /// Whether or not this process is signaled. This occurs
  242. /// upon the process changing to a different state.
  243. bool is_signaled = false;
  244. /// Total running time for the process in ticks.
  245. u64 total_process_running_time_ticks = 0;
  246. /// Per-process handle table for storing created object handles in.
  247. HandleTable handle_table;
  248. /// Per-process address arbiter.
  249. AddressArbiter address_arbiter;
  250. /// The per-process mutex lock instance used for handling various
  251. /// forms of services, such as lock arbitration, and condition
  252. /// variable related facilities.
  253. Mutex mutex;
  254. /// Random values for svcGetInfo RandomEntropy
  255. std::array<u64, RANDOM_ENTROPY_SIZE> random_entropy;
  256. /// List of threads that are running with this process as their owner.
  257. std::list<const Thread*> thread_list;
  258. /// System context
  259. Core::System& system;
  260. /// Name of this process
  261. std::string name;
  262. };
  263. } // namespace Kernel