process.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. u32 size;
  19. bool writable;
  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 code, rodata, data;
  68. VAddr entrypoint;
  69. private:
  70. CodeSet();
  71. ~CodeSet() override;
  72. };
  73. class Process final : public Object {
  74. public:
  75. static SharedPtr<Process> Create(SharedPtr<CodeSet> code_set);
  76. std::string GetTypeName() const override {
  77. return "Process";
  78. }
  79. std::string GetName() const override {
  80. return codeset->name;
  81. }
  82. static const HandleType HANDLE_TYPE = HandleType::Process;
  83. HandleType GetHandleType() const override {
  84. return HANDLE_TYPE;
  85. }
  86. static u32 next_process_id;
  87. SharedPtr<CodeSet> codeset;
  88. /// Resource limit descriptor for this process
  89. SharedPtr<ResourceLimit> resource_limit;
  90. /// The process may only call SVCs which have the corresponding bit set.
  91. std::bitset<0x80> svc_access_mask;
  92. /// Maximum size of the handle table for the process.
  93. unsigned int handle_table_size = 0x200;
  94. /// Special memory ranges mapped into this processes address space. This is used to give
  95. /// processes access to specific I/O regions and device memory.
  96. boost::container::static_vector<AddressMapping, 8> address_mappings;
  97. ProcessFlags flags;
  98. /// Kernel compatibility version for this process
  99. u16 kernel_version = 0;
  100. /// The default CPU for this process, threads are scheduled on this cpu by default.
  101. u8 ideal_processor = 0;
  102. /// The id of this process
  103. u32 process_id = next_process_id++;
  104. /**
  105. * Parses a list of kernel capability descriptors (as found in the ExHeader) and applies them
  106. * to this process.
  107. */
  108. void ParseKernelCaps(const u32* kernel_caps, size_t len);
  109. /**
  110. * Applies address space changes and launches the process main thread.
  111. */
  112. void Run(s32 main_thread_priority, u32 stack_size);
  113. ///////////////////////////////////////////////////////////////////////////////////////////////
  114. // Memory Management
  115. VMManager vm_manager;
  116. // Memory used to back the allocations in the regular heap. A single vector is used to cover
  117. // the entire virtual address space extents that bound the allocations, including any holes.
  118. // This makes deallocation and reallocation of holes fast and keeps process memory contiguous
  119. // in the emulator address space, allowing Memory::GetPointer to be reasonably safe.
  120. std::shared_ptr<std::vector<u8>> heap_memory;
  121. // The left/right bounds of the address space covered by heap_memory.
  122. VAddr heap_start = 0, heap_end = 0;
  123. u32 heap_used = 0, linear_heap_used = 0, misc_memory_used = 0;
  124. MemoryRegionInfo* memory_region = nullptr;
  125. /// The Thread Local Storage area is allocated as processes create threads,
  126. /// each TLS area is 0x200 bytes, so one page (0x1000) is split up in 8 parts, and each part
  127. /// holds the TLS for a specific thread. This vector contains which parts are in use for each
  128. /// page as a bitmask.
  129. /// This vector will grow as more pages are allocated for new threads.
  130. std::vector<std::bitset<8>> tls_slots;
  131. VAddr GetLinearHeapAreaAddress() const;
  132. VAddr GetLinearHeapBase() const;
  133. VAddr GetLinearHeapLimit() const;
  134. ResultVal<VAddr> HeapAllocate(VAddr target, u32 size, VMAPermission perms);
  135. ResultCode HeapFree(VAddr target, u32 size);
  136. ResultVal<VAddr> LinearAllocate(VAddr target, u32 size, VMAPermission perms);
  137. ResultCode LinearFree(VAddr target, u32 size);
  138. private:
  139. Process();
  140. ~Process() override;
  141. };
  142. extern SharedPtr<Process> g_current_process;
  143. }