process.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 CodeSet final : public Object {
  42. static SharedPtr<CodeSet> Create(std::string name, u64 program_id);
  43. std::string GetTypeName() const override { return "CodeSet"; }
  44. std::string GetName() const override { return name; }
  45. static const HandleType HANDLE_TYPE = HandleType::CodeSet;
  46. HandleType GetHandleType() const override { return HANDLE_TYPE; }
  47. /// Name of the process
  48. std::string name;
  49. /// Title ID corresponding to the process
  50. u64 program_id;
  51. std::shared_ptr<std::vector<u8>> memory;
  52. struct Segment {
  53. size_t offset = 0;
  54. VAddr addr = 0;
  55. u32 size = 0;
  56. };
  57. Segment code, rodata, data;
  58. VAddr entrypoint;
  59. private:
  60. CodeSet();
  61. ~CodeSet() override;
  62. };
  63. class Process final : public Object {
  64. public:
  65. static SharedPtr<Process> Create(SharedPtr<CodeSet> code_set);
  66. std::string GetTypeName() const override { return "Process"; }
  67. std::string GetName() const override { return codeset->name; }
  68. static const HandleType HANDLE_TYPE = HandleType::Process;
  69. HandleType GetHandleType() const override { return HANDLE_TYPE; }
  70. static u32 next_process_id;
  71. SharedPtr<CodeSet> codeset;
  72. /// Resource limit descriptor for this process
  73. SharedPtr<ResourceLimit> resource_limit;
  74. /// The process may only call SVCs which have the corresponding bit set.
  75. std::bitset<0x80> svc_access_mask;
  76. /// Maximum size of the handle table for the process.
  77. unsigned int handle_table_size = 0x200;
  78. /// Special memory ranges mapped into this processes address space. This is used to give
  79. /// processes access to specific I/O regions and device memory.
  80. boost::container::static_vector<AddressMapping, 8> address_mappings;
  81. ProcessFlags flags;
  82. /// Kernel compatibility version for this process
  83. u16 kernel_version = 0;
  84. /// The id of this process
  85. u32 process_id = next_process_id++;
  86. /**
  87. * Parses a list of kernel capability descriptors (as found in the ExHeader) and applies them
  88. * to this process.
  89. */
  90. void ParseKernelCaps(const u32* kernel_caps, size_t len);
  91. /**
  92. * Applies address space changes and launches the process main thread.
  93. */
  94. void Run(s32 main_thread_priority, u32 stack_size);
  95. ///////////////////////////////////////////////////////////////////////////////////////////////
  96. // Memory Management
  97. VMManager vm_manager;
  98. // Memory used to back the allocations in the regular heap. A single vector is used to cover
  99. // the entire virtual address space extents that bound the allocations, including any holes.
  100. // This makes deallocation and reallocation of holes fast and keeps process memory contiguous
  101. // in the emulator address space, allowing Memory::GetPointer to be reasonably safe.
  102. std::shared_ptr<std::vector<u8>> heap_memory;
  103. // The left/right bounds of the address space covered by heap_memory.
  104. VAddr heap_start = 0, heap_end = 0;
  105. std::shared_ptr<std::vector<u8>> linear_heap_memory;
  106. /// Bitmask of the used TLS slots
  107. std::bitset<300> used_tls_slots;
  108. ResultVal<VAddr> HeapAllocate(VAddr target, u32 size, VMAPermission perms);
  109. ResultCode HeapFree(VAddr target, u32 size);
  110. ResultVal<VAddr> LinearAllocate(VAddr target, u32 size, VMAPermission perms);
  111. ResultCode LinearFree(VAddr target, u32 size);
  112. private:
  113. Process();
  114. ~Process() override;
  115. };
  116. extern SharedPtr<Process> g_current_process;
  117. }