process.h 9.0 KB

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