shared_memory.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <string>
  6. #include "common/common_types.h"
  7. #include "core/hle/kernel/kernel.h"
  8. #include "core/hle/kernel/process.h"
  9. #include "core/hle/result.h"
  10. namespace Kernel {
  11. /// Permissions for mapped shared memory blocks
  12. enum class MemoryPermission : u32 {
  13. None = 0,
  14. Read = (1u << 0),
  15. Write = (1u << 1),
  16. ReadWrite = (Read | Write),
  17. Execute = (1u << 2),
  18. ReadExecute = (Read | Execute),
  19. WriteExecute = (Write | Execute),
  20. ReadWriteExecute = (Read | Write | Execute),
  21. DontCare = (1u << 28)
  22. };
  23. class SharedMemory final : public Object {
  24. public:
  25. /**
  26. * Creates a shared memory object.
  27. * @param owner_process Process that created this shared memory object.
  28. * @param size Size of the memory block. Must be page-aligned.
  29. * @param permissions Permission restrictions applied to the process which created the block.
  30. * @param other_permissions Permission restrictions applied to other processes mapping the
  31. * block.
  32. * @param address The address from which to map the Shared Memory.
  33. * @param region If the address is 0, the shared memory will be allocated in this region of the
  34. * linear heap.
  35. * @param name Optional object name, used for debugging purposes.
  36. */
  37. static SharedPtr<SharedMemory> Create(SharedPtr<Process> owner_process, u32 size,
  38. MemoryPermission permissions,
  39. MemoryPermission other_permissions, VAddr address = 0,
  40. MemoryRegion region = MemoryRegion::BASE,
  41. std::string name = "Unknown");
  42. /**
  43. * Creates a shared memory object from a block of memory managed by an HLE applet.
  44. * @param heap_block Heap block of the HLE applet.
  45. * @param offset The offset into the heap block that the SharedMemory will map.
  46. * @param size Size of the memory block. Must be page-aligned.
  47. * @param permissions Permission restrictions applied to the process which created the block.
  48. * @param other_permissions Permission restrictions applied to other processes mapping the
  49. * block.
  50. * @param name Optional object name, used for debugging purposes.
  51. */
  52. static SharedPtr<SharedMemory> CreateForApplet(std::shared_ptr<std::vector<u8>> heap_block,
  53. u32 offset, u32 size,
  54. MemoryPermission permissions,
  55. MemoryPermission other_permissions,
  56. std::string name = "Unknown Applet");
  57. std::string GetTypeName() const override {
  58. return "SharedMemory";
  59. }
  60. std::string GetName() const override {
  61. return name;
  62. }
  63. static const HandleType HANDLE_TYPE = HandleType::SharedMemory;
  64. HandleType GetHandleType() const override {
  65. return HANDLE_TYPE;
  66. }
  67. /**
  68. * Converts the specified MemoryPermission into the equivalent VMAPermission.
  69. * @param permission The MemoryPermission to convert.
  70. */
  71. static VMAPermission ConvertPermissions(MemoryPermission permission);
  72. /**
  73. * Maps a shared memory block to an address in the target process' address space
  74. * @param target_process Process on which to map the memory block.
  75. * @param address Address in system memory to map shared memory block to
  76. * @param permissions Memory block map permissions (specified by SVC field)
  77. * @param other_permissions Memory block map other permissions (specified by SVC field)
  78. */
  79. ResultCode Map(Process* target_process, VAddr address, MemoryPermission permissions,
  80. MemoryPermission other_permissions);
  81. /**
  82. * Unmaps a shared memory block from the specified address in system memory
  83. * @param target_process Process from which to umap the memory block.
  84. * @param address Address in system memory where the shared memory block is mapped
  85. * @return Result code of the unmap operation
  86. */
  87. ResultCode Unmap(Process* target_process, VAddr address);
  88. /**
  89. * Gets a pointer to the shared memory block
  90. * @param offset Offset from the start of the shared memory block to get pointer
  91. * @return Pointer to the shared memory block from the specified offset
  92. */
  93. u8* GetPointer(u32 offset = 0);
  94. /// Process that created this shared memory block.
  95. SharedPtr<Process> owner_process;
  96. /// Address of shared memory block in the owner process if specified.
  97. VAddr base_address;
  98. /// Physical address of the shared memory block in the linear heap if no address was specified
  99. /// during creation.
  100. PAddr linear_heap_phys_address;
  101. /// Backing memory for this shared memory block.
  102. std::shared_ptr<std::vector<u8>> backing_block;
  103. /// Offset into the backing block for this shared memory.
  104. u32 backing_block_offset;
  105. /// Size of the memory block. Page-aligned.
  106. u32 size;
  107. /// Permission restrictions applied to the process which created the block.
  108. MemoryPermission permissions;
  109. /// Permission restrictions applied to other processes mapping the block.
  110. MemoryPermission other_permissions;
  111. /// Name of shared memory object.
  112. std::string name;
  113. private:
  114. SharedMemory();
  115. ~SharedMemory() override;
  116. };
  117. } // namespace