process.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 <memory>
  9. #include <string>
  10. #include <vector>
  11. #include <boost/container/static_vector.hpp>
  12. #include "common/bit_field.h"
  13. #include "common/common_types.h"
  14. #include "core/hle/kernel/object.h"
  15. #include "core/hle/kernel/thread.h"
  16. #include "core/hle/kernel/vm_manager.h"
  17. namespace FileSys {
  18. class ProgramMetadata;
  19. }
  20. namespace Kernel {
  21. class KernelCore;
  22. struct AddressMapping {
  23. // Address and size must be page-aligned
  24. VAddr address;
  25. u64 size;
  26. bool read_only;
  27. bool unk_flag;
  28. };
  29. enum class MemoryRegion : u16 {
  30. APPLICATION = 1,
  31. SYSTEM = 2,
  32. BASE = 3,
  33. };
  34. union ProcessFlags {
  35. u16 raw;
  36. BitField<0, 1, u16>
  37. allow_debug; ///< Allows other processes to attach to and debug this process.
  38. BitField<1, 1, u16> force_debug; ///< Allows this process to attach to processes even if they
  39. /// don't have allow_debug set.
  40. BitField<2, 1, u16> allow_nonalphanum;
  41. BitField<3, 1, u16> shared_page_writable; ///< Shared page is mapped with write permissions.
  42. BitField<4, 1, u16> privileged_priority; ///< Can use priority levels higher than 24.
  43. BitField<5, 1, u16> allow_main_args;
  44. BitField<6, 1, u16> shared_device_mem;
  45. BitField<7, 1, u16> runnable_on_sleep;
  46. BitField<8, 4, MemoryRegion>
  47. memory_region; ///< Default region for memory allocations for this process
  48. BitField<12, 1, u16> loaded_high; ///< Application loaded high (not at 0x00100000).
  49. };
  50. enum class ProcessStatus { Created, Running, Exited };
  51. class ResourceLimit;
  52. struct CodeSet final : public Object {
  53. struct Segment {
  54. std::size_t offset = 0;
  55. VAddr addr = 0;
  56. u32 size = 0;
  57. };
  58. static SharedPtr<CodeSet> Create(KernelCore& kernel, std::string name);
  59. std::string GetTypeName() const override {
  60. return "CodeSet";
  61. }
  62. std::string GetName() const override {
  63. return name;
  64. }
  65. static const HandleType HANDLE_TYPE = HandleType::CodeSet;
  66. HandleType GetHandleType() const override {
  67. return HANDLE_TYPE;
  68. }
  69. Segment& CodeSegment() {
  70. return segments[0];
  71. }
  72. const Segment& CodeSegment() const {
  73. return segments[0];
  74. }
  75. Segment& RODataSegment() {
  76. return segments[1];
  77. }
  78. const Segment& RODataSegment() const {
  79. return segments[1];
  80. }
  81. Segment& DataSegment() {
  82. return segments[2];
  83. }
  84. const Segment& DataSegment() const {
  85. return segments[2];
  86. }
  87. std::shared_ptr<std::vector<u8>> memory;
  88. std::array<Segment, 3> segments;
  89. VAddr entrypoint;
  90. /// Name of the process
  91. std::string name;
  92. private:
  93. explicit CodeSet(KernelCore& kernel);
  94. ~CodeSet() override;
  95. };
  96. class Process final : public Object {
  97. public:
  98. static SharedPtr<Process> Create(KernelCore& kernel, std::string&& name);
  99. std::string GetTypeName() const override {
  100. return "Process";
  101. }
  102. std::string GetName() const override {
  103. return name;
  104. }
  105. static const HandleType HANDLE_TYPE = HandleType::Process;
  106. HandleType GetHandleType() const override {
  107. return HANDLE_TYPE;
  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. u32 GetProcessID() const {
  115. return process_id;
  116. }
  117. /**
  118. * Loads process-specifics configuration info with metadata provided
  119. * by an executable.
  120. *
  121. * @param metadata The provided metadata to load process specific info.
  122. */
  123. void LoadFromMetadata(const FileSys::ProgramMetadata& metadata);
  124. /// Title ID corresponding to the process
  125. u64 program_id;
  126. /// Resource limit descriptor for this process
  127. SharedPtr<ResourceLimit> resource_limit;
  128. /// The process may only call SVCs which have the corresponding bit set.
  129. std::bitset<0x80> svc_access_mask;
  130. /// Maximum size of the handle table for the process.
  131. unsigned int handle_table_size = 0x200;
  132. /// Special memory ranges mapped into this processes address space. This is used to give
  133. /// processes access to specific I/O regions and device memory.
  134. boost::container::static_vector<AddressMapping, 8> address_mappings;
  135. ProcessFlags flags;
  136. /// Kernel compatibility version for this process
  137. u16 kernel_version = 0;
  138. /// The default CPU for this process, threads are scheduled on this cpu by default.
  139. u8 ideal_processor = 0;
  140. /// Bitmask of allowed CPUs that this process' threads can run on. TODO(Subv): Actually parse
  141. /// this value from the process header.
  142. u32 allowed_processor_mask = THREADPROCESSORID_DEFAULT_MASK;
  143. u32 allowed_thread_priority_mask = 0xFFFFFFFF;
  144. u32 is_virtual_address_memory_enabled = 0;
  145. /**
  146. * Parses a list of kernel capability descriptors (as found in the ExHeader) and applies them
  147. * to this process.
  148. */
  149. void ParseKernelCaps(const u32* kernel_caps, std::size_t len);
  150. /**
  151. * Applies address space changes and launches the process main thread.
  152. */
  153. void Run(VAddr entry_point, s32 main_thread_priority, u32 stack_size);
  154. /**
  155. * Prepares a process for termination by stopping all of its threads
  156. * and clearing any other resources.
  157. */
  158. void PrepareForTermination();
  159. void LoadModule(SharedPtr<CodeSet> module_, VAddr base_addr);
  160. ///////////////////////////////////////////////////////////////////////////////////////////////
  161. // Memory Management
  162. // Marks the next available region as used and returns the address of the slot.
  163. VAddr MarkNextAvailableTLSSlotAsUsed(Thread& thread);
  164. // Frees a used TLS slot identified by the given address
  165. void FreeTLSSlot(VAddr tls_address);
  166. ResultVal<VAddr> HeapAllocate(VAddr target, u64 size, VMAPermission perms);
  167. ResultCode HeapFree(VAddr target, u32 size);
  168. ResultCode MirrorMemory(VAddr dst_addr, VAddr src_addr, u64 size);
  169. ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, u64 size);
  170. VMManager vm_manager;
  171. private:
  172. explicit Process(KernelCore& kernel);
  173. ~Process() override;
  174. /// Current status of the process
  175. ProcessStatus status;
  176. /// The ID of this process
  177. u32 process_id = 0;
  178. // Memory used to back the allocations in the regular heap. A single vector is used to cover
  179. // the entire virtual address space extents that bound the allocations, including any holes.
  180. // This makes deallocation and reallocation of holes fast and keeps process memory contiguous
  181. // in the emulator address space, allowing Memory::GetPointer to be reasonably safe.
  182. std::shared_ptr<std::vector<u8>> heap_memory;
  183. // The left/right bounds of the address space covered by heap_memory.
  184. VAddr heap_start = 0;
  185. VAddr heap_end = 0;
  186. u64 heap_used = 0;
  187. /// The Thread Local Storage area is allocated as processes create threads,
  188. /// each TLS area is 0x200 bytes, so one page (0x1000) is split up in 8 parts, and each part
  189. /// holds the TLS for a specific thread. This vector contains which parts are in use for each
  190. /// page as a bitmask.
  191. /// This vector will grow as more pages are allocated for new threads.
  192. std::vector<std::bitset<8>> tls_slots;
  193. std::string name;
  194. };
  195. } // namespace Kernel