memory.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. // SPDX-FileCopyrightText: 2014 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <cstddef>
  5. #include <memory>
  6. #include <optional>
  7. #include <span>
  8. #include <string>
  9. #include <vector>
  10. #include "common/scratch_buffer.h"
  11. #include "common/typed_address.h"
  12. #include "core/hle/result.h"
  13. namespace Common {
  14. struct PageTable;
  15. }
  16. namespace Core {
  17. class System;
  18. class GPUDirtyMemoryManager;
  19. } // namespace Core
  20. namespace Kernel {
  21. class KProcess;
  22. } // namespace Kernel
  23. namespace Tegra {
  24. class MemoryManager;
  25. }
  26. namespace Core::Memory {
  27. /**
  28. * Page size used by the ARM architecture. This is the smallest granularity with which memory can
  29. * be mapped.
  30. */
  31. constexpr std::size_t YUZU_PAGEBITS = 12;
  32. constexpr u64 YUZU_PAGESIZE = 1ULL << YUZU_PAGEBITS;
  33. constexpr u64 YUZU_PAGEMASK = YUZU_PAGESIZE - 1;
  34. /// Virtual user-space memory regions
  35. enum : u64 {
  36. /// TLS (Thread-Local Storage) related.
  37. TLS_ENTRY_SIZE = 0x200,
  38. /// Application stack
  39. DEFAULT_STACK_SIZE = 0x100000,
  40. };
  41. /// Central class that handles all memory operations and state.
  42. class Memory {
  43. public:
  44. explicit Memory(Core::System& system);
  45. ~Memory();
  46. Memory(const Memory&) = delete;
  47. Memory& operator=(const Memory&) = delete;
  48. Memory(Memory&&) = default;
  49. Memory& operator=(Memory&&) = delete;
  50. /**
  51. * Resets the state of the Memory system.
  52. */
  53. void Reset();
  54. /**
  55. * Changes the currently active page table to that of the given process instance.
  56. *
  57. * @param process The process to use the page table of.
  58. */
  59. void SetCurrentPageTable(Kernel::KProcess& process, u32 core_id);
  60. /**
  61. * Maps an allocated buffer onto a region of the emulated process address space.
  62. *
  63. * @param page_table The page table of the emulated process.
  64. * @param base The address to start mapping at. Must be page-aligned.
  65. * @param size The amount of bytes to map. Must be page-aligned.
  66. * @param target Buffer with the memory backing the mapping. Must be of length at least
  67. * `size`.
  68. */
  69. void MapMemoryRegion(Common::PageTable& page_table, Common::ProcessAddress base, u64 size,
  70. Common::PhysicalAddress target);
  71. /**
  72. * Unmaps a region of the emulated process address space.
  73. *
  74. * @param page_table The page table of the emulated process.
  75. * @param base The address to begin unmapping at.
  76. * @param size The amount of bytes to unmap.
  77. */
  78. void UnmapRegion(Common::PageTable& page_table, Common::ProcessAddress base, u64 size);
  79. /**
  80. * Checks whether or not the supplied address is a valid virtual
  81. * address for the current process.
  82. *
  83. * @param vaddr The virtual address to check the validity of.
  84. *
  85. * @returns True if the given virtual address is valid, false otherwise.
  86. */
  87. [[nodiscard]] bool IsValidVirtualAddress(Common::ProcessAddress vaddr) const;
  88. /**
  89. * Checks whether or not the supplied range of addresses are all valid
  90. * virtual addresses for the current process.
  91. *
  92. * @param base The address to begin checking.
  93. * @param size The amount of bytes to check.
  94. *
  95. * @returns True if all bytes in the given range are valid, false otherwise.
  96. */
  97. [[nodiscard]] bool IsValidVirtualAddressRange(Common::ProcessAddress base, u64 size) const;
  98. /**
  99. * Gets a pointer to the given address.
  100. *
  101. * @param vaddr Virtual address to retrieve a pointer to.
  102. *
  103. * @returns The pointer to the given address, if the address is valid.
  104. * If the address is not valid, nullptr will be returned.
  105. */
  106. u8* GetPointer(Common::ProcessAddress vaddr);
  107. u8* GetPointerSilent(Common::ProcessAddress vaddr);
  108. template <typename T>
  109. T* GetPointer(Common::ProcessAddress vaddr) {
  110. return reinterpret_cast<T*>(GetPointer(vaddr));
  111. }
  112. /**
  113. * Gets a pointer to the given address.
  114. *
  115. * @param vaddr Virtual address to retrieve a pointer to.
  116. *
  117. * @returns The pointer to the given address, if the address is valid.
  118. * If the address is not valid, nullptr will be returned.
  119. */
  120. [[nodiscard]] const u8* GetPointer(Common::ProcessAddress vaddr) const;
  121. template <typename T>
  122. const T* GetPointer(Common::ProcessAddress vaddr) const {
  123. return reinterpret_cast<T*>(GetPointer(vaddr));
  124. }
  125. /**
  126. * Reads an 8-bit unsigned value from the current process' address space
  127. * at the given virtual address.
  128. *
  129. * @param addr The virtual address to read the 8-bit value from.
  130. *
  131. * @returns the read 8-bit unsigned value.
  132. */
  133. u8 Read8(Common::ProcessAddress addr);
  134. /**
  135. * Reads a 16-bit unsigned value from the current process' address space
  136. * at the given virtual address.
  137. *
  138. * @param addr The virtual address to read the 16-bit value from.
  139. *
  140. * @returns the read 16-bit unsigned value.
  141. */
  142. u16 Read16(Common::ProcessAddress addr);
  143. /**
  144. * Reads a 32-bit unsigned value from the current process' address space
  145. * at the given virtual address.
  146. *
  147. * @param addr The virtual address to read the 32-bit value from.
  148. *
  149. * @returns the read 32-bit unsigned value.
  150. */
  151. u32 Read32(Common::ProcessAddress addr);
  152. /**
  153. * Reads a 64-bit unsigned value from the current process' address space
  154. * at the given virtual address.
  155. *
  156. * @param addr The virtual address to read the 64-bit value from.
  157. *
  158. * @returns the read 64-bit value.
  159. */
  160. u64 Read64(Common::ProcessAddress addr);
  161. /**
  162. * Writes an 8-bit unsigned integer to the given virtual address in
  163. * the current process' address space.
  164. *
  165. * @param addr The virtual address to write the 8-bit unsigned integer to.
  166. * @param data The 8-bit unsigned integer to write to the given virtual address.
  167. *
  168. * @post The memory at the given virtual address contains the specified data value.
  169. */
  170. void Write8(Common::ProcessAddress addr, u8 data);
  171. /**
  172. * Writes a 16-bit unsigned integer to the given virtual address in
  173. * the current process' address space.
  174. *
  175. * @param addr The virtual address to write the 16-bit unsigned integer to.
  176. * @param data The 16-bit unsigned integer to write to the given virtual address.
  177. *
  178. * @post The memory range [addr, sizeof(data)) contains the given data value.
  179. */
  180. void Write16(Common::ProcessAddress addr, u16 data);
  181. /**
  182. * Writes a 32-bit unsigned integer to the given virtual address in
  183. * the current process' address space.
  184. *
  185. * @param addr The virtual address to write the 32-bit unsigned integer to.
  186. * @param data The 32-bit unsigned integer to write to the given virtual address.
  187. *
  188. * @post The memory range [addr, sizeof(data)) contains the given data value.
  189. */
  190. void Write32(Common::ProcessAddress addr, u32 data);
  191. /**
  192. * Writes a 64-bit unsigned integer to the given virtual address in
  193. * the current process' address space.
  194. *
  195. * @param addr The virtual address to write the 64-bit unsigned integer to.
  196. * @param data The 64-bit unsigned integer to write to the given virtual address.
  197. *
  198. * @post The memory range [addr, sizeof(data)) contains the given data value.
  199. */
  200. void Write64(Common::ProcessAddress addr, u64 data);
  201. /**
  202. * Writes a 8-bit unsigned integer to the given virtual address in
  203. * the current process' address space if and only if the address contains
  204. * the expected value. This operation is atomic.
  205. *
  206. * @param addr The virtual address to write the 8-bit unsigned integer to.
  207. * @param data The 8-bit unsigned integer to write to the given virtual address.
  208. * @param expected The 8-bit unsigned integer to check against the given virtual address.
  209. *
  210. * @post The memory range [addr, sizeof(data)) contains the given data value.
  211. */
  212. bool WriteExclusive8(Common::ProcessAddress addr, u8 data, u8 expected);
  213. /**
  214. * Writes a 16-bit unsigned integer to the given virtual address in
  215. * the current process' address space if and only if the address contains
  216. * the expected value. This operation is atomic.
  217. *
  218. * @param addr The virtual address to write the 16-bit unsigned integer to.
  219. * @param data The 16-bit unsigned integer to write to the given virtual address.
  220. * @param expected The 16-bit unsigned integer to check against the given virtual address.
  221. *
  222. * @post The memory range [addr, sizeof(data)) contains the given data value.
  223. */
  224. bool WriteExclusive16(Common::ProcessAddress addr, u16 data, u16 expected);
  225. /**
  226. * Writes a 32-bit unsigned integer to the given virtual address in
  227. * the current process' address space if and only if the address contains
  228. * the expected value. This operation is atomic.
  229. *
  230. * @param addr The virtual address to write the 32-bit unsigned integer to.
  231. * @param data The 32-bit unsigned integer to write to the given virtual address.
  232. * @param expected The 32-bit unsigned integer to check against the given virtual address.
  233. *
  234. * @post The memory range [addr, sizeof(data)) contains the given data value.
  235. */
  236. bool WriteExclusive32(Common::ProcessAddress addr, u32 data, u32 expected);
  237. /**
  238. * Writes a 64-bit unsigned integer to the given virtual address in
  239. * the current process' address space if and only if the address contains
  240. * the expected value. This operation is atomic.
  241. *
  242. * @param addr The virtual address to write the 64-bit unsigned integer to.
  243. * @param data The 64-bit unsigned integer to write to the given virtual address.
  244. * @param expected The 64-bit unsigned integer to check against the given virtual address.
  245. *
  246. * @post The memory range [addr, sizeof(data)) contains the given data value.
  247. */
  248. bool WriteExclusive64(Common::ProcessAddress addr, u64 data, u64 expected);
  249. /**
  250. * Writes a 128-bit unsigned integer to the given virtual address in
  251. * the current process' address space if and only if the address contains
  252. * the expected value. This operation is atomic.
  253. *
  254. * @param addr The virtual address to write the 128-bit unsigned integer to.
  255. * @param data The 128-bit unsigned integer to write to the given virtual address.
  256. * @param expected The 128-bit unsigned integer to check against the given virtual address.
  257. *
  258. * @post The memory range [addr, sizeof(data)) contains the given data value.
  259. */
  260. bool WriteExclusive128(Common::ProcessAddress addr, u128 data, u128 expected);
  261. /**
  262. * Reads a null-terminated string from the given virtual address.
  263. * This function will continually read characters until either:
  264. *
  265. * - A null character ('\0') is reached.
  266. * - max_length characters have been read.
  267. *
  268. * @note The final null-terminating character (if found) is not included
  269. * in the returned string.
  270. *
  271. * @param vaddr The address to begin reading the string from.
  272. * @param max_length The maximum length of the string to read in characters.
  273. *
  274. * @returns The read string.
  275. */
  276. std::string ReadCString(Common::ProcessAddress vaddr, std::size_t max_length);
  277. /**
  278. * Reads a contiguous block of bytes from the current process' address space.
  279. *
  280. * @param src_addr The virtual address to begin reading from.
  281. * @param dest_buffer The buffer to place the read bytes into.
  282. * @param size The amount of data to read, in bytes.
  283. *
  284. * @note If a size of 0 is specified, then this function reads nothing and
  285. * no attempts to access memory are made at all.
  286. *
  287. * @pre dest_buffer must be at least size bytes in length, otherwise a
  288. * buffer overrun will occur.
  289. *
  290. * @post The range [dest_buffer, size) contains the read bytes from the
  291. * current process' address space.
  292. */
  293. bool ReadBlock(Common::ProcessAddress src_addr, void* dest_buffer, std::size_t size);
  294. /**
  295. * Reads a contiguous block of bytes from the current process' address space.
  296. * This unsafe version does not trigger GPU flushing.
  297. *
  298. * @param src_addr The virtual address to begin reading from.
  299. * @param dest_buffer The buffer to place the read bytes into.
  300. * @param size The amount of data to read, in bytes.
  301. *
  302. * @note If a size of 0 is specified, then this function reads nothing and
  303. * no attempts to access memory are made at all.
  304. *
  305. * @pre dest_buffer must be at least size bytes in length, otherwise a
  306. * buffer overrun will occur.
  307. *
  308. * @post The range [dest_buffer, size) contains the read bytes from the
  309. * current process' address space.
  310. */
  311. bool ReadBlockUnsafe(Common::ProcessAddress src_addr, void* dest_buffer, std::size_t size);
  312. const u8* GetSpan(const VAddr src_addr, const std::size_t size) const;
  313. u8* GetSpan(const VAddr src_addr, const std::size_t size);
  314. /**
  315. * Writes a range of bytes into the current process' address space at the specified
  316. * virtual address.
  317. *
  318. * @param dest_addr The destination virtual address to begin writing the data at.
  319. * @param src_buffer The data to write into the current process' address space.
  320. * @param size The size of the data to write, in bytes.
  321. *
  322. * @post The address range [dest_addr, size) in the current process' address space
  323. * contains the data that was within src_buffer.
  324. *
  325. * @post If an attempt is made to write into an unmapped region of memory, the writes
  326. * will be ignored and an error will be logged.
  327. *
  328. * @post If a write is performed into a region of memory that is considered cached
  329. * rasterizer memory, will cause the currently active rasterizer to be notified
  330. * and will mark that region as invalidated to caches that the active
  331. * graphics backend may be maintaining over the course of execution.
  332. */
  333. bool WriteBlock(Common::ProcessAddress dest_addr, const void* src_buffer, std::size_t size);
  334. /**
  335. * Writes a range of bytes into the current process' address space at the specified
  336. * virtual address.
  337. * This unsafe version does not invalidate GPU Memory.
  338. *
  339. * @param dest_addr The destination virtual address to begin writing the data at.
  340. * @param src_buffer The data to write into the current process' address space.
  341. * @param size The size of the data to write, in bytes.
  342. *
  343. * @post The address range [dest_addr, size) in the current process' address space
  344. * contains the data that was within src_buffer.
  345. *
  346. * @post If an attempt is made to write into an unmapped region of memory, the writes
  347. * will be ignored and an error will be logged.
  348. *
  349. */
  350. bool WriteBlockUnsafe(Common::ProcessAddress dest_addr, const void* src_buffer,
  351. std::size_t size);
  352. /**
  353. * Copies data within a process' address space to another location within the
  354. * same address space.
  355. *
  356. * @param dest_addr The destination virtual address to begin copying the data into.
  357. * @param src_addr The source virtual address to begin copying the data from.
  358. * @param size The size of the data to copy, in bytes.
  359. *
  360. * @post The range [dest_addr, size) within the process' address space contains the
  361. * same data within the range [src_addr, size).
  362. */
  363. bool CopyBlock(Common::ProcessAddress dest_addr, Common::ProcessAddress src_addr,
  364. std::size_t size);
  365. /**
  366. * Zeros a range of bytes within the current process' address space at the specified
  367. * virtual address.
  368. *
  369. * @param dest_addr The destination virtual address to zero the data from.
  370. * @param size The size of the range to zero out, in bytes.
  371. *
  372. * @post The range [dest_addr, size) within the process' address space contains the
  373. * value 0.
  374. */
  375. bool ZeroBlock(Common::ProcessAddress dest_addr, std::size_t size);
  376. /**
  377. * Invalidates a range of bytes within the current process' address space at the specified
  378. * virtual address.
  379. *
  380. * @param dest_addr The destination virtual address to invalidate the data from.
  381. * @param size The size of the range to invalidate, in bytes.
  382. *
  383. */
  384. Result InvalidateDataCache(Common::ProcessAddress dest_addr, std::size_t size);
  385. /**
  386. * Stores a range of bytes within the current process' address space at the specified
  387. * virtual address.
  388. *
  389. * @param dest_addr The destination virtual address to store the data from.
  390. * @param size The size of the range to store, in bytes.
  391. *
  392. */
  393. Result StoreDataCache(Common::ProcessAddress dest_addr, std::size_t size);
  394. /**
  395. * Flushes a range of bytes within the current process' address space at the specified
  396. * virtual address.
  397. *
  398. * @param dest_addr The destination virtual address to flush the data from.
  399. * @param size The size of the range to flush, in bytes.
  400. *
  401. */
  402. Result FlushDataCache(Common::ProcessAddress dest_addr, std::size_t size);
  403. /**
  404. * Marks each page within the specified address range as cached or uncached.
  405. *
  406. * @param vaddr The virtual address indicating the start of the address range.
  407. * @param size The size of the address range in bytes.
  408. * @param cached Whether or not any pages within the address range should be
  409. * marked as cached or uncached.
  410. */
  411. void RasterizerMarkRegionCached(Common::ProcessAddress vaddr, u64 size, bool cached);
  412. /**
  413. * Marks each page within the specified address range as debug or non-debug.
  414. * Debug addresses are not accessible from fastmem pointers.
  415. *
  416. * @param vaddr The virtual address indicating the start of the address range.
  417. * @param size The size of the address range in bytes.
  418. * @param debug Whether or not any pages within the address range should be
  419. * marked as debug or non-debug.
  420. */
  421. void MarkRegionDebug(Common::ProcessAddress vaddr, u64 size, bool debug);
  422. void SetGPUDirtyManagers(std::span<Core::GPUDirtyMemoryManager> managers);
  423. void InvalidateRegion(Common::ProcessAddress dest_addr, size_t size);
  424. void FlushRegion(Common::ProcessAddress dest_addr, size_t size);
  425. private:
  426. Core::System& system;
  427. struct Impl;
  428. std::unique_ptr<Impl> impl;
  429. };
  430. enum GuestMemoryFlags : u32 {
  431. Read = 1 << 0,
  432. Write = 1 << 1,
  433. Safe = 1 << 2,
  434. Cached = 1 << 3,
  435. SafeRead = Read | Safe,
  436. SafeWrite = Write | Safe,
  437. SafeReadWrite = SafeRead | SafeWrite,
  438. SafeReadCachedWrite = SafeReadWrite | Cached,
  439. UnsafeRead = Read,
  440. UnsafeWrite = Write,
  441. UnsafeReadWrite = UnsafeRead | UnsafeWrite,
  442. UnsafeReadCachedWrite = UnsafeReadWrite | Cached,
  443. };
  444. namespace {
  445. template <typename M, typename T, GuestMemoryFlags FLAGS>
  446. class GuestMemory {
  447. using iterator = T*;
  448. using const_iterator = const T*;
  449. using value_type = T;
  450. using element_type = T;
  451. using iterator_category = std::contiguous_iterator_tag;
  452. public:
  453. GuestMemory() = delete;
  454. explicit GuestMemory(M& memory, u64 addr, std::size_t size,
  455. Common::ScratchBuffer<T>* backup = nullptr)
  456. : m_memory{memory}, m_addr{addr}, m_size{size} {
  457. static_assert(FLAGS & GuestMemoryFlags::Read || FLAGS & GuestMemoryFlags::Write);
  458. if constexpr (FLAGS & GuestMemoryFlags::Read) {
  459. Read(addr, size, backup);
  460. }
  461. }
  462. ~GuestMemory() = default;
  463. T* data() noexcept {
  464. return m_data_span.data();
  465. }
  466. const T* data() const noexcept {
  467. return m_data_span.data();
  468. }
  469. size_t size() const noexcept {
  470. return m_size;
  471. }
  472. size_t size_bytes() const noexcept {
  473. return this->size() * sizeof(T);
  474. }
  475. [[nodiscard]] T* begin() noexcept {
  476. return this->data();
  477. }
  478. [[nodiscard]] const T* begin() const noexcept {
  479. return this->data();
  480. }
  481. [[nodiscard]] T* end() noexcept {
  482. return this->data() + this->size();
  483. }
  484. [[nodiscard]] const T* end() const noexcept {
  485. return this->data() + this->size();
  486. }
  487. T& operator[](size_t index) noexcept {
  488. return m_data_span[index];
  489. }
  490. const T& operator[](size_t index) const noexcept {
  491. return m_data_span[index];
  492. }
  493. void SetAddressAndSize(u64 addr, std::size_t size) noexcept {
  494. m_addr = addr;
  495. m_size = size;
  496. m_addr_changed = true;
  497. }
  498. std::span<T> Read(u64 addr, std::size_t size,
  499. Common::ScratchBuffer<T>* backup = nullptr) noexcept {
  500. m_addr = addr;
  501. m_size = size;
  502. if (m_size == 0) {
  503. m_is_data_copy = true;
  504. return {};
  505. }
  506. if (this->TrySetSpan()) {
  507. if constexpr (FLAGS & GuestMemoryFlags::Safe) {
  508. m_memory.FlushRegion(m_addr, this->size_bytes());
  509. }
  510. } else {
  511. if (backup) {
  512. backup->resize_destructive(this->size());
  513. m_data_span = *backup;
  514. } else {
  515. m_data_copy.resize(this->size());
  516. m_data_span = std::span(m_data_copy);
  517. }
  518. m_is_data_copy = true;
  519. m_span_valid = true;
  520. if constexpr (FLAGS & GuestMemoryFlags::Safe) {
  521. m_memory.ReadBlock(m_addr, this->data(), this->size_bytes());
  522. } else {
  523. m_memory.ReadBlockUnsafe(m_addr, this->data(), this->size_bytes());
  524. }
  525. }
  526. return m_data_span;
  527. }
  528. void Write(std::span<T> write_data) noexcept {
  529. if constexpr (FLAGS & GuestMemoryFlags::Cached) {
  530. m_memory.WriteBlockCached(m_addr, write_data.data(), this->size_bytes());
  531. } else if constexpr (FLAGS & GuestMemoryFlags::Safe) {
  532. m_memory.WriteBlock(m_addr, write_data.data(), this->size_bytes());
  533. } else {
  534. m_memory.WriteBlockUnsafe(m_addr, write_data.data(), this->size_bytes());
  535. }
  536. }
  537. bool TrySetSpan() noexcept {
  538. if (u8* ptr = m_memory.GetSpan(m_addr, this->size_bytes()); ptr) {
  539. m_data_span = {reinterpret_cast<T*>(ptr), this->size()};
  540. m_span_valid = true;
  541. return true;
  542. }
  543. return false;
  544. }
  545. protected:
  546. bool IsDataCopy() const noexcept {
  547. return m_is_data_copy;
  548. }
  549. bool AddressChanged() const noexcept {
  550. return m_addr_changed;
  551. }
  552. M& m_memory;
  553. u64 m_addr{};
  554. size_t m_size{};
  555. std::span<T> m_data_span{};
  556. std::vector<T> m_data_copy{};
  557. bool m_span_valid{false};
  558. bool m_is_data_copy{false};
  559. bool m_addr_changed{false};
  560. };
  561. template <typename M, typename T, GuestMemoryFlags FLAGS>
  562. class GuestMemoryScoped : public GuestMemory<M, T, FLAGS> {
  563. public:
  564. GuestMemoryScoped() = delete;
  565. explicit GuestMemoryScoped(M& memory, u64 addr, std::size_t size,
  566. Common::ScratchBuffer<T>* backup = nullptr)
  567. : GuestMemory<M, T, FLAGS>(memory, addr, size, backup) {
  568. if constexpr (!(FLAGS & GuestMemoryFlags::Read)) {
  569. if (!this->TrySetSpan()) {
  570. if (backup) {
  571. this->m_data_span = *backup;
  572. this->m_span_valid = true;
  573. this->m_is_data_copy = true;
  574. }
  575. }
  576. }
  577. }
  578. ~GuestMemoryScoped() {
  579. if constexpr (FLAGS & GuestMemoryFlags::Write) {
  580. if (this->size() == 0) [[unlikely]] {
  581. return;
  582. }
  583. if (this->AddressChanged() || this->IsDataCopy()) {
  584. ASSERT(this->m_span_valid);
  585. if constexpr (FLAGS & GuestMemoryFlags::Cached) {
  586. this->m_memory.WriteBlockCached(this->m_addr, this->data(), this->size_bytes());
  587. } else if constexpr (FLAGS & GuestMemoryFlags::Safe) {
  588. this->m_memory.WriteBlock(this->m_addr, this->data(), this->size_bytes());
  589. } else {
  590. this->m_memory.WriteBlockUnsafe(this->m_addr, this->data(), this->size_bytes());
  591. }
  592. } else if constexpr (FLAGS & GuestMemoryFlags::Safe) {
  593. this->m_memory.InvalidateRegion(this->m_addr, this->size_bytes());
  594. }
  595. }
  596. }
  597. };
  598. } // namespace
  599. template <typename T, GuestMemoryFlags FLAGS>
  600. using CpuGuestMemory = GuestMemory<Memory, T, FLAGS>;
  601. template <typename T, GuestMemoryFlags FLAGS>
  602. using CpuGuestMemoryScoped = GuestMemoryScoped<Memory, T, FLAGS>;
  603. template <typename T, GuestMemoryFlags FLAGS>
  604. using GpuGuestMemory = GuestMemory<Tegra::MemoryManager, T, FLAGS>;
  605. template <typename T, GuestMemoryFlags FLAGS>
  606. using GpuGuestMemoryScoped = GuestMemoryScoped<Tegra::MemoryManager, T, FLAGS>;
  607. } // namespace Core::Memory