process.h 6.5 KB

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