process.h 5.9 KB

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