process.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 available to this process in bytes.
  154. u64 GetTotalPhysicalMemoryAvailable() const;
  155. /// Retrieves the total physical memory available to this process in bytes,
  156. /// without the size of the personal heap added to it.
  157. u64 GetTotalPhysicalMemoryAvailableWithoutMmHeap() const;
  158. /// Retrieves the total physical memory used by this process in bytes.
  159. u64 GetTotalPhysicalMemoryUsed() const;
  160. /// Retrieves the total physical memory used by this process in bytes,
  161. /// without the size of the personal heap added to it.
  162. u64 GetTotalPhysicalMemoryUsedWithoutMmHeap() const;
  163. /// Gets the list of all threads created with this process as their owner.
  164. const std::list<const Thread*>& GetThreadList() const {
  165. return thread_list;
  166. }
  167. /// Registers a thread as being created under this process,
  168. /// adding it to this process' thread list.
  169. void RegisterThread(const Thread* thread);
  170. /// Unregisters a thread from this process, removing it
  171. /// from this process' thread list.
  172. void UnregisterThread(const Thread* thread);
  173. /// Clears the signaled state of the process if and only if it's signaled.
  174. ///
  175. /// @pre The process must not be already terminated. If this is called on a
  176. /// terminated process, then ERR_INVALID_STATE will be returned.
  177. ///
  178. /// @pre The process must be in a signaled state. If this is called on a
  179. /// process instance that is not signaled, ERR_INVALID_STATE will be
  180. /// returned.
  181. ResultCode ClearSignalState();
  182. /**
  183. * Loads process-specifics configuration info with metadata provided
  184. * by an executable.
  185. *
  186. * @param metadata The provided metadata to load process specific info from.
  187. *
  188. * @returns RESULT_SUCCESS if all relevant metadata was able to be
  189. * loaded and parsed. Otherwise, an error code is returned.
  190. */
  191. ResultCode LoadFromMetadata(const FileSys::ProgramMetadata& metadata);
  192. /**
  193. * Starts the main application thread for this process.
  194. *
  195. * @param main_thread_priority The priority for the main thread.
  196. * @param stack_size The stack size for the main thread in bytes.
  197. */
  198. void Run(s32 main_thread_priority, u64 stack_size);
  199. /**
  200. * Prepares a process for termination by stopping all of its threads
  201. * and clearing any other resources.
  202. */
  203. void PrepareForTermination();
  204. void LoadModule(CodeSet module_, VAddr base_addr);
  205. ///////////////////////////////////////////////////////////////////////////////////////////////
  206. // Thread-local storage management
  207. // Marks the next available region as used and returns the address of the slot.
  208. VAddr MarkNextAvailableTLSSlotAsUsed(Thread& thread);
  209. // Frees a used TLS slot identified by the given address
  210. void FreeTLSSlot(VAddr tls_address);
  211. private:
  212. explicit Process(Core::System& system);
  213. ~Process() override;
  214. /// Checks if the specified thread should wait until this process is available.
  215. bool ShouldWait(const Thread* thread) const override;
  216. /// Acquires/locks this process for the specified thread if it's available.
  217. void Acquire(Thread* thread) override;
  218. /// Changes the process status. If the status is different
  219. /// from the current process status, then this will trigger
  220. /// a process signal.
  221. void ChangeStatus(ProcessStatus new_status);
  222. /// Memory manager for this process.
  223. Kernel::VMManager vm_manager;
  224. /// Size of the main thread's stack in bytes.
  225. u64 main_thread_stack_size = 0;
  226. /// Size of the loaded code memory in bytes.
  227. u64 code_memory_size = 0;
  228. /// Current status of the process
  229. ProcessStatus status;
  230. /// The ID of this process
  231. u64 process_id = 0;
  232. /// Title ID corresponding to the process
  233. u64 program_id = 0;
  234. /// Resource limit descriptor for this process
  235. SharedPtr<ResourceLimit> resource_limit;
  236. /// The ideal CPU core for this process, threads are scheduled on this core by default.
  237. u8 ideal_core = 0;
  238. u32 is_virtual_address_memory_enabled = 0;
  239. /// The Thread Local Storage area is allocated as processes create threads,
  240. /// each TLS area is 0x200 bytes, so one page (0x1000) is split up in 8 parts, and each part
  241. /// holds the TLS for a specific thread. This vector contains which parts are in use for each
  242. /// page as a bitmask.
  243. /// This vector will grow as more pages are allocated for new threads.
  244. std::vector<std::bitset<8>> tls_slots;
  245. /// Contains the parsed process capability descriptors.
  246. ProcessCapabilities capabilities;
  247. /// Whether or not this process is AArch64, or AArch32.
  248. /// By default, we currently assume this is true, unless otherwise
  249. /// specified by metadata provided to the process during loading.
  250. bool is_64bit_process = true;
  251. /// Whether or not this process is signaled. This occurs
  252. /// upon the process changing to a different state.
  253. bool is_signaled = false;
  254. /// Total running time for the process in ticks.
  255. u64 total_process_running_time_ticks = 0;
  256. /// Per-process handle table for storing created object handles in.
  257. HandleTable handle_table;
  258. /// Per-process address arbiter.
  259. AddressArbiter address_arbiter;
  260. /// The per-process mutex lock instance used for handling various
  261. /// forms of services, such as lock arbitration, and condition
  262. /// variable related facilities.
  263. Mutex mutex;
  264. /// Random values for svcGetInfo RandomEntropy
  265. std::array<u64, RANDOM_ENTROPY_SIZE> random_entropy;
  266. /// List of threads that are running with this process as their owner.
  267. std::list<const Thread*> thread_list;
  268. /// System context
  269. Core::System& system;
  270. /// Name of this process
  271. std::string name;
  272. };
  273. } // namespace Kernel