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