shared_memory.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <memory>
  6. #include <string>
  7. #include <vector>
  8. #include "common/common_types.h"
  9. #include "core/hle/kernel/object.h"
  10. #include "core/hle/kernel/physical_memory.h"
  11. #include "core/hle/kernel/process.h"
  12. #include "core/hle/result.h"
  13. namespace Kernel {
  14. class KernelCore;
  15. /// Permissions for mapped shared memory blocks
  16. enum class MemoryPermission : u32 {
  17. None = 0,
  18. Read = (1u << 0),
  19. Write = (1u << 1),
  20. ReadWrite = (Read | Write),
  21. Execute = (1u << 2),
  22. ReadExecute = (Read | Execute),
  23. WriteExecute = (Write | Execute),
  24. ReadWriteExecute = (Read | Write | Execute),
  25. DontCare = (1u << 28)
  26. };
  27. class SharedMemory final : public Object {
  28. public:
  29. /**
  30. * Creates a shared memory object.
  31. * @param kernel The kernel instance to create a shared memory instance under.
  32. * @param owner_process Process that created this shared memory object.
  33. * @param size Size of the memory block. Must be page-aligned.
  34. * @param permissions Permission restrictions applied to the process which created the block.
  35. * @param other_permissions Permission restrictions applied to other processes mapping the
  36. * block.
  37. * @param address The address from which to map the Shared Memory.
  38. * @param region If the address is 0, the shared memory will be allocated in this region of the
  39. * linear heap.
  40. * @param name Optional object name, used for debugging purposes.
  41. */
  42. static SharedPtr<SharedMemory> Create(KernelCore& kernel, Process* owner_process, u64 size,
  43. MemoryPermission permissions,
  44. MemoryPermission other_permissions, VAddr address = 0,
  45. MemoryRegion region = MemoryRegion::BASE,
  46. std::string name = "Unknown");
  47. /**
  48. * Creates a shared memory object from a block of memory managed by an HLE applet.
  49. * @param kernel The kernel instance to create a shared memory instance under.
  50. * @param heap_block Heap block of the HLE applet.
  51. * @param offset The offset into the heap block that the SharedMemory will map.
  52. * @param size Size of the memory block. Must be page-aligned.
  53. * @param permissions Permission restrictions applied to the process which created the block.
  54. * @param other_permissions Permission restrictions applied to other processes mapping the
  55. * block.
  56. * @param name Optional object name, used for debugging purposes.
  57. */
  58. static SharedPtr<SharedMemory> CreateForApplet(
  59. KernelCore& kernel, std::shared_ptr<Kernel::PhysicalMemory> heap_block, std::size_t offset,
  60. u64 size, MemoryPermission permissions, MemoryPermission other_permissions,
  61. std::string name = "Unknown Applet");
  62. std::string GetTypeName() const override {
  63. return "SharedMemory";
  64. }
  65. std::string GetName() const override {
  66. return name;
  67. }
  68. static constexpr HandleType HANDLE_TYPE = HandleType::SharedMemory;
  69. HandleType GetHandleType() const override {
  70. return HANDLE_TYPE;
  71. }
  72. /// Gets the size of the underlying memory block in bytes.
  73. u64 GetSize() const {
  74. return size;
  75. }
  76. /**
  77. * Converts the specified MemoryPermission into the equivalent VMAPermission.
  78. * @param permission The MemoryPermission to convert.
  79. */
  80. static VMAPermission ConvertPermissions(MemoryPermission permission);
  81. /**
  82. * Maps a shared memory block to an address in the target process' address space
  83. * @param target_process Process on which to map the memory block.
  84. * @param address Address in system memory to map shared memory block to
  85. * @param permissions Memory block map permissions (specified by SVC field)
  86. * @param other_permissions Memory block map other permissions (specified by SVC field)
  87. */
  88. ResultCode Map(Process& target_process, VAddr address, MemoryPermission permissions,
  89. MemoryPermission other_permissions);
  90. /**
  91. * Unmaps a shared memory block from the specified address in system memory
  92. *
  93. * @param target_process Process from which to unmap the memory block.
  94. * @param address Address in system memory where the shared memory block is mapped.
  95. * @param unmap_size The amount of bytes to unmap from this shared memory instance.
  96. *
  97. * @return Result code of the unmap operation
  98. *
  99. * @pre The given size to unmap must be the same size as the amount of memory managed by
  100. * the SharedMemory instance itself, otherwise ERR_INVALID_SIZE will be returned.
  101. */
  102. ResultCode Unmap(Process& target_process, VAddr address, u64 unmap_size);
  103. /**
  104. * Gets a pointer to the shared memory block
  105. * @param offset Offset from the start of the shared memory block to get pointer
  106. * @return A pointer to the shared memory block from the specified offset
  107. */
  108. u8* GetPointer(std::size_t offset = 0);
  109. /**
  110. * Gets a constant pointer to the shared memory block
  111. * @param offset Offset from the start of the shared memory block to get pointer
  112. * @return A constant pointer to the shared memory block from the specified offset
  113. */
  114. const u8* GetPointer(std::size_t offset = 0) const;
  115. private:
  116. explicit SharedMemory(KernelCore& kernel);
  117. ~SharedMemory() override;
  118. /// Backing memory for this shared memory block.
  119. std::shared_ptr<PhysicalMemory> backing_block;
  120. /// Offset into the backing block for this shared memory.
  121. std::size_t backing_block_offset = 0;
  122. /// Size of the memory block. Page-aligned.
  123. u64 size = 0;
  124. /// Permission restrictions applied to the process which created the block.
  125. MemoryPermission permissions{};
  126. /// Permission restrictions applied to other processes mapping the block.
  127. MemoryPermission other_permissions{};
  128. /// Process that created this shared memory block.
  129. Process* owner_process;
  130. /// Address of shared memory block in the owner process if specified.
  131. VAddr base_address = 0;
  132. /// Name of shared memory object.
  133. std::string name;
  134. };
  135. } // namespace Kernel