shared_memory.h 5.6 KB

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