process.h 6.5 KB

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