Browse Source

UserspaceEmulator: Add some convenient SoftMMU APIs for copying data

We'll soon want to copy data in and out of the SoftMMU memory space.
Andreas Kling 5 years ago
parent
commit
94f07660e9
2 changed files with 24 additions and 0 deletions
  1. 20 0
      DevTools/UserspaceEmulator/SoftMMU.cpp
  2. 4 0
      DevTools/UserspaceEmulator/SoftMMU.h

+ 20 - 0
DevTools/UserspaceEmulator/SoftMMU.cpp

@@ -25,6 +25,7 @@
  */
 
 #include "SoftMMU.h"
+#include <AK/ByteBuffer.h>
 
 namespace UserspaceEmulator {
 
@@ -119,4 +120,23 @@ void SoftMMU::write32(X86::LogicalAddress address, u32 value)
     region->write32(address.offset() - region->base(), value);
 }
 
+void SoftMMU::copy_to_vm(FlatPtr destination, const void* source, size_t size)
+{
+    for (size_t i = 0; i < size; ++i)
+        write8({ 0x20, destination + i }, ((const u8*)source)[i]);
+}
+
+void SoftMMU::copy_from_vm(void* destination, const FlatPtr source, size_t size)
+{
+    for (size_t i = 0; i < size; ++i)
+        ((u8*)destination)[i] = read8({ 0x20, source + i });
+}
+
+ByteBuffer SoftMMU::copy_buffer_from_vm(const FlatPtr source, size_t size)
+{
+    auto buffer = ByteBuffer::create_uninitialized(size);
+    copy_from_vm(buffer.data(), source, size);
+    return buffer;
+}
+
 }

+ 4 - 0
DevTools/UserspaceEmulator/SoftMMU.h

@@ -78,6 +78,10 @@ public:
     void add_region(NonnullOwnPtr<Region>);
     void set_tls_region(NonnullOwnPtr<Region>);
 
+    void copy_to_vm(FlatPtr destination, const void* source, size_t);
+    void copy_from_vm(void* destination, const FlatPtr source, size_t);
+    ByteBuffer copy_buffer_from_vm(const FlatPtr source, size_t);
+
 private:
     OwnPtr<Region> m_tls_region;
     NonnullOwnPtrVector<Region> m_regions;