process.h 5.4 KB

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