memory.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <cstddef>
  6. #include <string>
  7. #include "common/common_types.h"
  8. namespace Common {
  9. struct PageTable;
  10. }
  11. namespace Kernel {
  12. class Process;
  13. }
  14. namespace Memory {
  15. /**
  16. * Page size used by the ARM architecture. This is the smallest granularity with which memory can
  17. * be mapped.
  18. */
  19. constexpr std::size_t PAGE_BITS = 12;
  20. constexpr u64 PAGE_SIZE = 1ULL << PAGE_BITS;
  21. constexpr u64 PAGE_MASK = PAGE_SIZE - 1;
  22. /// Virtual user-space memory regions
  23. enum : VAddr {
  24. /// TLS (Thread-Local Storage) related.
  25. TLS_ENTRY_SIZE = 0x200,
  26. /// Application stack
  27. DEFAULT_STACK_SIZE = 0x100000,
  28. /// Kernel Virtual Address Range
  29. KERNEL_REGION_VADDR = 0xFFFFFF8000000000,
  30. KERNEL_REGION_SIZE = 0x7FFFE00000,
  31. KERNEL_REGION_END = KERNEL_REGION_VADDR + KERNEL_REGION_SIZE,
  32. };
  33. /// Changes the currently active page table to that of
  34. /// the given process instance.
  35. void SetCurrentPageTable(Kernel::Process& process);
  36. /// Determines if the given VAddr is valid for the specified process.
  37. bool IsValidVirtualAddress(const Kernel::Process& process, VAddr vaddr);
  38. bool IsValidVirtualAddress(VAddr vaddr);
  39. /// Determines if the given VAddr is a kernel address
  40. bool IsKernelVirtualAddress(VAddr vaddr);
  41. u8 Read8(VAddr addr);
  42. u16 Read16(VAddr addr);
  43. u32 Read32(VAddr addr);
  44. u64 Read64(VAddr addr);
  45. void Write8(VAddr addr, u8 data);
  46. void Write16(VAddr addr, u16 data);
  47. void Write32(VAddr addr, u32 data);
  48. void Write64(VAddr addr, u64 data);
  49. void ReadBlock(const Kernel::Process& process, VAddr src_addr, void* dest_buffer, std::size_t size);
  50. void ReadBlock(VAddr src_addr, void* dest_buffer, std::size_t size);
  51. void WriteBlock(const Kernel::Process& process, VAddr dest_addr, const void* src_buffer,
  52. std::size_t size);
  53. void WriteBlock(VAddr dest_addr, const void* src_buffer, std::size_t size);
  54. void ZeroBlock(const Kernel::Process& process, VAddr dest_addr, std::size_t size);
  55. void CopyBlock(VAddr dest_addr, VAddr src_addr, std::size_t size);
  56. u8* GetPointer(VAddr vaddr);
  57. std::string ReadCString(VAddr vaddr, std::size_t max_length);
  58. enum class FlushMode {
  59. /// Write back modified surfaces to RAM
  60. Flush,
  61. /// Remove region from the cache
  62. Invalidate,
  63. /// Write back modified surfaces to RAM, and also remove them from the cache
  64. FlushAndInvalidate,
  65. };
  66. /**
  67. * Mark each page touching the region as cached.
  68. */
  69. void RasterizerMarkRegionCached(VAddr vaddr, u64 size, bool cached);
  70. } // namespace Memory