/ src / ARMeilleure / Memory / IMemoryManager.cs
IMemoryManager.cs
 1  using System;
 2  
 3  namespace ARMeilleure.Memory
 4  {
 5      public interface IMemoryManager
 6      {
 7          int AddressSpaceBits { get; }
 8  
 9          IntPtr PageTablePointer { get; }
10  
11          MemoryManagerType Type { get; }
12  
13          event Action<ulong, ulong> UnmapEvent;
14  
15          /// <summary>
16          /// Reads data from CPU mapped memory.
17          /// </summary>
18          /// <typeparam name="T">Type of the data being read</typeparam>
19          /// <param name="va">Virtual address of the data in memory</param>
20          /// <returns>The data</returns>
21          T Read<T>(ulong va) where T : unmanaged;
22  
23          /// <summary>
24          /// Reads data from CPU mapped memory, with read tracking
25          /// </summary>
26          /// <typeparam name="T">Type of the data being read</typeparam>
27          /// <param name="va">Virtual address of the data in memory</param>
28          /// <returns>The data</returns>
29          T ReadTracked<T>(ulong va) where T : unmanaged;
30  
31          /// <summary>
32          /// Reads data from CPU mapped memory, from guest code. (with read tracking)
33          /// </summary>
34          /// <typeparam name="T">Type of the data being read</typeparam>
35          /// <param name="va">Virtual address of the data in memory</param>
36          /// <returns>The data</returns>
37          T ReadGuest<T>(ulong va) where T : unmanaged
38          {
39              return ReadTracked<T>(va);
40          }
41  
42          /// <summary>
43          /// Writes data to CPU mapped memory.
44          /// </summary>
45          /// <typeparam name="T">Type of the data being written</typeparam>
46          /// <param name="va">Virtual address to write the data into</param>
47          /// <param name="value">Data to be written</param>
48          void Write<T>(ulong va, T value) where T : unmanaged;
49  
50          /// <summary>
51          /// Writes data to CPU mapped memory, from guest code.
52          /// </summary>
53          /// <typeparam name="T">Type of the data being written</typeparam>
54          /// <param name="va">Virtual address to write the data into</param>
55          /// <param name="value">Data to be written</param>
56          void WriteGuest<T>(ulong va, T value) where T : unmanaged
57          {
58              Write(va, value);
59          }
60  
61          /// <summary>
62          /// Gets a read-only span of data from CPU mapped memory.
63          /// </summary>
64          /// <param name="va">Virtual address of the data</param>
65          /// <param name="size">Size of the data</param>
66          /// <param name="tracked">True if read tracking is triggered on the span</param>
67          /// <returns>A read-only span of the data</returns>
68          ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false);
69  
70          /// <summary>
71          /// Gets a reference for the given type at the specified virtual memory address.
72          /// </summary>
73          /// <remarks>
74          /// The data must be located at a contiguous memory region.
75          /// </remarks>
76          /// <typeparam name="T">Type of the data to get the reference</typeparam>
77          /// <param name="va">Virtual address of the data</param>
78          /// <returns>A reference to the data in memory</returns>
79          ref T GetRef<T>(ulong va) where T : unmanaged;
80  
81          /// <summary>
82          /// Checks if the page at a given CPU virtual address is mapped.
83          /// </summary>
84          /// <param name="va">Virtual address to check</param>
85          /// <returns>True if the address is mapped, false otherwise</returns>
86          bool IsMapped(ulong va);
87  
88          /// <summary>
89          /// Alerts the memory tracking that a given region has been read from or written to.
90          /// This should be called before read/write is performed.
91          /// </summary>
92          /// <param name="va">Virtual address of the region</param>
93          /// <param name="size">Size of the region</param>
94          /// <param name="write">True if the region was written, false if read</param>
95          /// <param name="precise">True if the access is precise, false otherwise</param>
96          /// <param name="exemptId">Optional ID of the handles that should not be signalled</param>
97          void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false, int? exemptId = null);
98      }
99  }