process.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 <cstddef>
  7. #include <list>
  8. #include <string>
  9. #include <vector>
  10. #include "common/common_types.h"
  11. #include "core/hle/kernel/address_arbiter.h"
  12. #include "core/hle/kernel/handle_table.h"
  13. #include "core/hle/kernel/mutex.h"
  14. #include "core/hle/kernel/process_capability.h"
  15. #include "core/hle/kernel/vm_manager.h"
  16. #include "core/hle/kernel/wait_object.h"
  17. #include "core/hle/result.h"
  18. namespace Core {
  19. class System;
  20. }
  21. namespace FileSys {
  22. class ProgramMetadata;
  23. }
  24. namespace Kernel {
  25. class KernelCore;
  26. class ResourceLimit;
  27. class Thread;
  28. class TLSPage;
  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. // Used to determine how process IDs are assigned.
  65. enum class ProcessType {
  66. KernelInternal,
  67. Userland,
  68. };
  69. static constexpr std::size_t RANDOM_ENTROPY_SIZE = 4;
  70. static SharedPtr<Process> Create(Core::System& system, std::string name, ProcessType type);
  71. std::string GetTypeName() const override {
  72. return "Process";
  73. }
  74. std::string GetName() const override {
  75. return name;
  76. }
  77. static constexpr HandleType HANDLE_TYPE = HandleType::Process;
  78. HandleType GetHandleType() const override {
  79. return HANDLE_TYPE;
  80. }
  81. /// Gets a reference to the process' memory manager.
  82. Kernel::VMManager& VMManager() {
  83. return vm_manager;
  84. }
  85. /// Gets a const reference to the process' memory manager.
  86. const Kernel::VMManager& VMManager() const {
  87. return vm_manager;
  88. }
  89. /// Gets a reference to the process' handle table.
  90. HandleTable& GetHandleTable() {
  91. return handle_table;
  92. }
  93. /// Gets a const reference to the process' handle table.
  94. const HandleTable& GetHandleTable() const {
  95. return handle_table;
  96. }
  97. /// Gets a reference to the process' address arbiter.
  98. AddressArbiter& GetAddressArbiter() {
  99. return address_arbiter;
  100. }
  101. /// Gets a const reference to the process' address arbiter.
  102. const AddressArbiter& GetAddressArbiter() const {
  103. return address_arbiter;
  104. }
  105. /// Gets a reference to the process' mutex lock.
  106. Mutex& GetMutex() {
  107. return mutex;
  108. }
  109. /// Gets a const reference to the process' mutex lock
  110. const Mutex& GetMutex() const {
  111. return mutex;
  112. }
  113. /// Gets the address to the process' dedicated TLS region.
  114. VAddr GetTLSRegionAddress() const {
  115. return tls_region_address;
  116. }
  117. /// Gets the current status of the process
  118. ProcessStatus GetStatus() const {
  119. return status;
  120. }
  121. /// Gets the unique ID that identifies this particular process.
  122. u64 GetProcessID() const {
  123. return process_id;
  124. }
  125. /// Gets the title ID corresponding to this process.
  126. u64 GetTitleID() const {
  127. return program_id;
  128. }
  129. /// Gets the resource limit descriptor for this process
  130. SharedPtr<ResourceLimit> GetResourceLimit() const;
  131. /// Gets the ideal CPU core ID for this process
  132. u8 GetIdealCore() const {
  133. return ideal_core;
  134. }
  135. /// Gets the bitmask of allowed cores that this process' threads can run on.
  136. u64 GetCoreMask() const {
  137. return capabilities.GetCoreMask();
  138. }
  139. /// Gets the bitmask of allowed thread priorities.
  140. u64 GetPriorityMask() const {
  141. return capabilities.GetPriorityMask();
  142. }
  143. /// Gets the amount of secure memory to allocate for memory management.
  144. u32 GetSystemResourceSize() const {
  145. return system_resource_size;
  146. }
  147. /// Gets the amount of secure memory currently in use for memory management.
  148. u32 GetSystemResourceUsage() const {
  149. // On hardware, this returns the amount of system resource memory that has
  150. // been used by the kernel. This is problematic for Yuzu to emulate, because
  151. // system resource memory is used for page tables -- and yuzu doesn't really
  152. // have a way to calculate how much memory is required for page tables for
  153. // the current process at any given time.
  154. // TODO: Is this even worth implementing? Games may retrieve this value via
  155. // an SDK function that gets used + available system resource size for debug
  156. // or diagnostic purposes. However, it seems unlikely that a game would make
  157. // decisions based on how much system memory is dedicated to its page tables.
  158. // Is returning a value other than zero wise?
  159. return 0;
  160. }
  161. /// Whether this process is an AArch64 or AArch32 process.
  162. bool Is64BitProcess() const {
  163. return is_64bit_process;
  164. }
  165. /// Gets the total running time of the process instance in ticks.
  166. u64 GetCPUTimeTicks() const {
  167. return total_process_running_time_ticks;
  168. }
  169. /// Updates the total running time, adding the given ticks to it.
  170. void UpdateCPUTimeTicks(u64 ticks) {
  171. total_process_running_time_ticks += ticks;
  172. }
  173. /// Gets 8 bytes of random data for svcGetInfo RandomEntropy
  174. u64 GetRandomEntropy(std::size_t index) const {
  175. return random_entropy.at(index);
  176. }
  177. /// Retrieves the total physical memory available to this process in bytes.
  178. u64 GetTotalPhysicalMemoryAvailable() const;
  179. /// Retrieves the total physical memory available to this process in bytes,
  180. /// without the size of the personal system resource heap added to it.
  181. u64 GetTotalPhysicalMemoryAvailableWithoutSystemResource() const;
  182. /// Retrieves the total physical memory used by this process in bytes.
  183. u64 GetTotalPhysicalMemoryUsed() const;
  184. /// Retrieves the total physical memory used by this process in bytes,
  185. /// without the size of the personal system resource heap added to it.
  186. u64 GetTotalPhysicalMemoryUsedWithoutSystemResource() const;
  187. /// Gets the list of all threads created with this process as their owner.
  188. const std::list<const Thread*>& GetThreadList() const {
  189. return thread_list;
  190. }
  191. /// Registers a thread as being created under this process,
  192. /// adding it to this process' thread list.
  193. void RegisterThread(const Thread* thread);
  194. /// Unregisters a thread from this process, removing it
  195. /// from this process' thread list.
  196. void UnregisterThread(const Thread* thread);
  197. /// Clears the signaled state of the process if and only if it's signaled.
  198. ///
  199. /// @pre The process must not be already terminated. If this is called on a
  200. /// terminated process, then ERR_INVALID_STATE will be returned.
  201. ///
  202. /// @pre The process must be in a signaled state. If this is called on a
  203. /// process instance that is not signaled, ERR_INVALID_STATE will be
  204. /// returned.
  205. ResultCode ClearSignalState();
  206. /**
  207. * Loads process-specifics configuration info with metadata provided
  208. * by an executable.
  209. *
  210. * @param metadata The provided metadata to load process specific info from.
  211. *
  212. * @returns RESULT_SUCCESS if all relevant metadata was able to be
  213. * loaded and parsed. Otherwise, an error code is returned.
  214. */
  215. ResultCode LoadFromMetadata(const FileSys::ProgramMetadata& metadata);
  216. /**
  217. * Starts the main application thread for this process.
  218. *
  219. * @param main_thread_priority The priority for the main thread.
  220. * @param stack_size The stack size for the main thread in bytes.
  221. */
  222. void Run(s32 main_thread_priority, u64 stack_size);
  223. /**
  224. * Prepares a process for termination by stopping all of its threads
  225. * and clearing any other resources.
  226. */
  227. void PrepareForTermination();
  228. void LoadModule(CodeSet module_, VAddr base_addr);
  229. ///////////////////////////////////////////////////////////////////////////////////////////////
  230. // Thread-local storage management
  231. // Marks the next available region as used and returns the address of the slot.
  232. [[nodiscard]] VAddr CreateTLSRegion();
  233. // Frees a used TLS slot identified by the given address
  234. void FreeTLSRegion(VAddr tls_address);
  235. private:
  236. explicit Process(Core::System& system);
  237. ~Process() override;
  238. /// Checks if the specified thread should wait until this process is available.
  239. bool ShouldWait(const Thread* thread) const override;
  240. /// Acquires/locks this process for the specified thread if it's available.
  241. void Acquire(Thread* thread) override;
  242. /// Changes the process status. If the status is different
  243. /// from the current process status, then this will trigger
  244. /// a process signal.
  245. void ChangeStatus(ProcessStatus new_status);
  246. /// Allocates the main thread stack for the process, given the stack size in bytes.
  247. void AllocateMainThreadStack(u64 stack_size);
  248. /// Memory manager for this process.
  249. Kernel::VMManager vm_manager;
  250. /// Size of the main thread's stack in bytes.
  251. u64 main_thread_stack_size = 0;
  252. /// Size of the loaded code memory in bytes.
  253. u64 code_memory_size = 0;
  254. /// Current status of the process
  255. ProcessStatus status{};
  256. /// The ID of this process
  257. u64 process_id = 0;
  258. /// Title ID corresponding to the process
  259. u64 program_id = 0;
  260. /// Specifies additional memory to be reserved for the process's memory management by the
  261. /// system. When this is non-zero, secure memory is allocated and used for page table allocation
  262. /// instead of using the normal global page tables/memory block management.
  263. u32 system_resource_size = 0;
  264. /// Resource limit descriptor for this process
  265. SharedPtr<ResourceLimit> resource_limit;
  266. /// The ideal CPU core for this process, threads are scheduled on this core by default.
  267. u8 ideal_core = 0;
  268. /// The Thread Local Storage area is allocated as processes create threads,
  269. /// each TLS area is 0x200 bytes, so one page (0x1000) is split up in 8 parts, and each part
  270. /// holds the TLS for a specific thread. This vector contains which parts are in use for each
  271. /// page as a bitmask.
  272. /// This vector will grow as more pages are allocated for new threads.
  273. std::vector<TLSPage> tls_pages;
  274. /// Contains the parsed process capability descriptors.
  275. ProcessCapabilities capabilities;
  276. /// Whether or not this process is AArch64, or AArch32.
  277. /// By default, we currently assume this is true, unless otherwise
  278. /// specified by metadata provided to the process during loading.
  279. bool is_64bit_process = true;
  280. /// Whether or not this process is signaled. This occurs
  281. /// upon the process changing to a different state.
  282. bool is_signaled = false;
  283. /// Total running time for the process in ticks.
  284. u64 total_process_running_time_ticks = 0;
  285. /// Per-process handle table for storing created object handles in.
  286. HandleTable handle_table;
  287. /// Per-process address arbiter.
  288. AddressArbiter address_arbiter;
  289. /// The per-process mutex lock instance used for handling various
  290. /// forms of services, such as lock arbitration, and condition
  291. /// variable related facilities.
  292. Mutex mutex;
  293. /// Address indicating the location of the process' dedicated TLS region.
  294. VAddr tls_region_address = 0;
  295. /// Random values for svcGetInfo RandomEntropy
  296. std::array<u64, RANDOM_ENTROPY_SIZE> random_entropy{};
  297. /// List of threads that are running with this process as their owner.
  298. std::list<const Thread*> thread_list;
  299. /// System context
  300. Core::System& system;
  301. /// Name of this process
  302. std::string name;
  303. };
  304. } // namespace Kernel