process.h 10.0 KB

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