transfer_memory.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2019 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <memory>
  6. #include "core/hle/kernel/object.h"
  7. #include "core/hle/kernel/physical_memory.h"
  8. union ResultCode;
  9. namespace Memory {
  10. class Memory;
  11. }
  12. namespace Kernel {
  13. class KernelCore;
  14. class Process;
  15. enum class MemoryPermission : u32;
  16. /// Defines the interface for transfer memory objects.
  17. ///
  18. /// Transfer memory is typically used for the purpose of
  19. /// transferring memory between separate process instances,
  20. /// thus the name.
  21. ///
  22. class TransferMemory final : public Object {
  23. public:
  24. explicit TransferMemory(KernelCore& kernel, Memory::Memory& memory);
  25. ~TransferMemory() override;
  26. static constexpr HandleType HANDLE_TYPE = HandleType::TransferMemory;
  27. static std::shared_ptr<TransferMemory> Create(KernelCore& kernel, Memory::Memory& memory,
  28. VAddr base_address, u64 size,
  29. MemoryPermission permissions);
  30. TransferMemory(const TransferMemory&) = delete;
  31. TransferMemory& operator=(const TransferMemory&) = delete;
  32. TransferMemory(TransferMemory&&) = delete;
  33. TransferMemory& operator=(TransferMemory&&) = delete;
  34. std::string GetTypeName() const override {
  35. return "TransferMemory";
  36. }
  37. std::string GetName() const override {
  38. return GetTypeName();
  39. }
  40. HandleType GetHandleType() const override {
  41. return HANDLE_TYPE;
  42. }
  43. /// Gets a pointer to the backing block of this instance.
  44. const u8* GetPointer() const;
  45. /// Gets the size of the memory backing this instance in bytes.
  46. u64 GetSize() const;
  47. /// Attempts to map transfer memory with the given range and memory permissions.
  48. ///
  49. /// @param address The base address to being mapping memory at.
  50. /// @param size The size of the memory to map, in bytes.
  51. /// @param permissions The memory permissions to check against when mapping memory.
  52. ///
  53. /// @pre The given address, size, and memory permissions must all match
  54. /// the same values that were given when creating the transfer memory
  55. /// instance.
  56. ///
  57. ResultCode MapMemory(VAddr address, u64 size, MemoryPermission permissions);
  58. /// Unmaps the transfer memory with the given range
  59. ///
  60. /// @param address The base address to begin unmapping memory at.
  61. /// @param size The size of the memory to unmap, in bytes.
  62. ///
  63. /// @pre The given address and size must be the same as the ones used
  64. /// to create the transfer memory instance.
  65. ///
  66. ResultCode UnmapMemory(VAddr address, u64 size);
  67. /// Reserves the region to be used for the transfer memory, called after the transfer memory is
  68. /// created.
  69. ResultCode Reserve();
  70. /// Resets the region previously used for the transfer memory, called after the transfer memory
  71. /// is closed.
  72. ResultCode Reset();
  73. private:
  74. /// Memory block backing this instance.
  75. std::shared_ptr<PhysicalMemory> backing_block;
  76. /// The base address for the memory managed by this instance.
  77. VAddr base_address = 0;
  78. /// Size of the memory, in bytes, that this instance manages.
  79. u64 memory_size = 0;
  80. /// The memory permissions that are applied to this instance.
  81. MemoryPermission owner_permissions{};
  82. /// The process that this transfer memory instance was created under.
  83. Process* owner_process = nullptr;
  84. /// Whether or not this transfer memory instance has mapped memory.
  85. bool is_mapped = false;
  86. Memory::Memory& memory;
  87. };
  88. } // namespace Kernel