transfer_memory.h 3.1 KB

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