From 4f95575d418195d833a61704c54df29f4c58ec86 Mon Sep 17 00:00:00 2001
From: Hamish Milne <hamishmilne83@gmail.com>
Date: Sun, 22 Dec 2019 23:37:17 +0000
Subject: [PATCH] Serialize some more kernel objects

---
 TODO                                | 21 +++++++++++++++------
 src/common/archives.h               |  1 +
 src/core/core.cpp                   |  2 ++
 src/core/hle/kernel/event.cpp       |  7 +++++--
 src/core/hle/kernel/event.h         |  5 ++++-
 src/core/hle/kernel/kernel.cpp      |  1 +
 src/core/hle/kernel/mutex.cpp       |  3 +++
 src/core/hle/kernel/mutex.h         |  3 +++
 src/core/hle/kernel/process.cpp     |  1 +
 src/core/hle/kernel/semaphore.cpp   |  7 +++++--
 src/core/hle/kernel/semaphore.h     |  5 ++++-
 src/core/hle/kernel/shared_memory.h | 16 ++++++++++++++++
 src/core/hle/kernel/thread.cpp      |  2 ++
 src/core/hle/kernel/thread.h        |  3 +++
 src/core/hle/kernel/vm_manager.h    |  1 +
 15 files changed, 66 insertions(+), 12 deletions(-)

diff --git a/TODO b/TODO
index cad91b958..6397b4ba9 100644
--- a/TODO
+++ b/TODO
@@ -2,6 +2,10 @@
 ✔ CPU @done(19-08-13 15:41)
 ✔ Memory @done(19-08-13 15:41)
 ✔ DSP @done(19-08-13 15:41)
+☐ Service manager
+☐ App loader
+☐ Archive manager
+☐ Custom texture cache
 ☐ MMIO
 ☐ Movie
 ☐ Perf stats
@@ -24,27 +28,32 @@
         ✔ Client port @done(19-08-13 16:40)
         ✔ Client session @done(19-08-13 16:40)
         ✔ Config mem @done(19-08-13 16:40)
-        ☐ Event
+        ✔ Event @done(19-12-22 18:44)
         ✔ Handle table @done(19-08-13 16:42)
         ☐ HLE IPC
         ☐ IPC
         ✔ Memory @started(19-08-13 16:43) @done(19-12-22 18:34)
-        ☐ Mutex @started(19-08-13 16:43)
+        ✔ Mutex @done(19-08-13 16:43)
         ✔ Object @done(19-08-13 15:41)
-        ☐ Process @started(19-08-13 16:43)
+        ✔ Process @started(19-08-13 16:43) @done(19-12-22 18:41)
+        ☐ Code set @started(19-12-22 18:41)
+            Needs a way to reference loaded images (so we don't serialize the entire ROM as well)
         ✔ Resource limit @done(19-08-13 16:43)
-        ☐ Semaphore @started(19-08-13 16:44)
+        ✔ Semaphore @done(19-08-13 16:44)
         ✔ Server port @done(19-08-13 16:44)
         ✔ Server session @done(19-08-13 16:44)
         ✔ Session @done(19-08-13 16:44)
-        ☐ Shared memory
+        ☐ Shared memory @started(19-12-22 21:20)
+            Need to figure out backing memory (a u8*)
         ✘ Shared page @started(19-08-13 16:44) @cancelled(19-12-22 11:19)
             Not needed right now as shared_page is read-only and derived from other data
-        ☐ SVC
+        ✔ SVC @done(19-12-22 21:32)
+            Nothing to do - all data is constant
         ☐ Thread @started(19-08-13 16:45)
             This requires refactoring wakeup_callback to be an object ref
         ✔ Timer @done(19-08-13 16:45)
         ☐ VM Manager @started(19-08-13 16:46)
+            Just need to figure out backing_mem (a u8*)
         ✔ Wait object @done(19-08-13 16:46)
     ☐ Service
         ☐ AC
diff --git a/src/common/archives.h b/src/common/archives.h
index f30886d90..a27afe80c 100644
--- a/src/common/archives.h
+++ b/src/common/archives.h
@@ -1,5 +1,6 @@
 #include "boost/archive/binary_iarchive.hpp"
 #include "boost/archive/binary_oarchive.hpp"
+#include "boost/serialization/export.hpp"
 
 using iarchive = boost::archive::binary_iarchive;
 using oarchive = boost::archive::binary_oarchive;
diff --git a/src/core/core.cpp b/src/core/core.cpp
index 902340d41..9aecf84c9 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -398,6 +398,8 @@ void System::Reset() {
 template<class Archive>
 void System::serialize(Archive & ar, const unsigned int file_version)
 {
+    ar & *cpu_core.get();
+    //ar & *service_manager.get();
     ar & GPU::g_regs;
     ar & LCD::g_regs;
     ar & dsp_core->GetDspMemory();
diff --git a/src/core/hle/kernel/event.cpp b/src/core/hle/kernel/event.cpp
index f31162e35..7af667739 100644
--- a/src/core/hle/kernel/event.cpp
+++ b/src/core/hle/kernel/event.cpp
@@ -6,17 +6,20 @@
 #include <map>
 #include <vector>
 #include "common/assert.h"
+#include "common/archives.h"
 #include "core/hle/kernel/event.h"
 #include "core/hle/kernel/kernel.h"
 #include "core/hle/kernel/thread.h"
 
+SERIALIZE_EXPORT_IMPL(Kernel::Event)
+
 namespace Kernel {
 
-Event::Event(KernelSystem& kernel) : WaitObject(kernel) {}
+Event::Event() : WaitObject() {}
 Event::~Event() {}
 
 std::shared_ptr<Event> KernelSystem::CreateEvent(ResetType reset_type, std::string name) {
-    auto evt{std::make_shared<Event>(*this)};
+    auto evt{std::make_shared<Event>()};
 
     evt->signaled = false;
     evt->reset_type = reset_type;
diff --git a/src/core/hle/kernel/event.h b/src/core/hle/kernel/event.h
index 9e9da267a..bb97f6eb1 100644
--- a/src/core/hle/kernel/event.h
+++ b/src/core/hle/kernel/event.h
@@ -4,6 +4,7 @@
 
 #pragma once
 
+#include <boost/serialization/export.hpp>
 #include "common/common_types.h"
 #include "core/hle/kernel/object.h"
 #include "core/hle/kernel/wait_object.h"
@@ -12,7 +13,7 @@ namespace Kernel {
 
 class Event final : public WaitObject {
 public:
-    explicit Event(KernelSystem& kernel);
+    explicit Event();
     ~Event() override;
 
     std::string GetTypeName() const override {
@@ -62,3 +63,5 @@ private:
 };
 
 } // namespace Kernel
+
+BOOST_CLASS_EXPORT_KEY(Kernel::Event)
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp
index 6248311b9..4f9a02410 100644
--- a/src/core/hle/kernel/kernel.cpp
+++ b/src/core/hle/kernel/kernel.cpp
@@ -121,6 +121,7 @@ void KernelSystem::serialize(Archive& ar, const unsigned int file_version)
     ar & *thread_manager.get();
     ar & *config_mem_handler.get();
     // Shared page data is read-only at the moment, so doesn't need serializing
+    // Deliberately don't include debugger info to allow debugging through loads
 }
 
 SERIALIZE_IMPL(KernelSystem)
diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp
index b9a32ef23..b8a3d143b 100644
--- a/src/core/hle/kernel/mutex.cpp
+++ b/src/core/hle/kernel/mutex.cpp
@@ -4,6 +4,7 @@
 
 #include <map>
 #include <vector>
+#include "common/archives.h"
 #include "common/assert.h"
 #include "core/core.h"
 #include "core/hle/kernel/errors.h"
@@ -12,6 +13,8 @@
 #include "core/hle/kernel/object.h"
 #include "core/hle/kernel/thread.h"
 
+SERIALIZE_EXPORT_IMPL(Kernel::Mutex)
+
 namespace Kernel {
 
 void ReleaseThreadMutexes(Thread* thread) {
diff --git a/src/core/hle/kernel/mutex.h b/src/core/hle/kernel/mutex.h
index 4f0c2c2b5..f4449c0f2 100644
--- a/src/core/hle/kernel/mutex.h
+++ b/src/core/hle/kernel/mutex.h
@@ -6,6 +6,7 @@
 
 #include <memory>
 #include <string>
+#include <boost/serialization/export.hpp>
 #include "common/common_types.h"
 #include "core/hle/kernel/kernel.h"
 #include "core/hle/kernel/wait_object.h"
@@ -78,3 +79,5 @@ private:
 void ReleaseThreadMutexes(Thread* thread);
 
 } // namespace Kernel
+
+BOOST_CLASS_EXPORT_KEY(Kernel::Mutex)
diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp
index 49de1e69e..5fad4fd85 100644
--- a/src/core/hle/kernel/process.cpp
+++ b/src/core/hle/kernel/process.cpp
@@ -34,6 +34,7 @@ void Process::serialize(Archive& ar, const unsigned int file_version)
     ar & flags.raw;
     ar & kernel_version;
     ar & ideal_processor;
+    ar & status;
     ar & process_id;
     ar & vm_manager;
     ar & memory_used;
diff --git a/src/core/hle/kernel/semaphore.cpp b/src/core/hle/kernel/semaphore.cpp
index bbc8a385f..f60a653e5 100644
--- a/src/core/hle/kernel/semaphore.cpp
+++ b/src/core/hle/kernel/semaphore.cpp
@@ -3,14 +3,17 @@
 // Refer to the license.txt file included.
 
 #include "common/assert.h"
+#include "common/archives.h"
 #include "core/hle/kernel/errors.h"
 #include "core/hle/kernel/kernel.h"
 #include "core/hle/kernel/semaphore.h"
 #include "core/hle/kernel/thread.h"
 
+SERIALIZE_EXPORT_IMPL(Kernel::Semaphore)
+
 namespace Kernel {
 
-Semaphore::Semaphore(KernelSystem& kernel) : WaitObject(kernel) {}
+Semaphore::Semaphore() : WaitObject() {}
 Semaphore::~Semaphore() {}
 
 ResultVal<std::shared_ptr<Semaphore>> KernelSystem::CreateSemaphore(s32 initial_count,
@@ -20,7 +23,7 @@ ResultVal<std::shared_ptr<Semaphore>> KernelSystem::CreateSemaphore(s32 initial_
     if (initial_count > max_count)
         return ERR_INVALID_COMBINATION_KERNEL;
 
-    auto semaphore{std::make_shared<Semaphore>(*this)};
+    auto semaphore{std::make_shared<Semaphore>()};
 
     // When the semaphore is created, some slots are reserved for other threads,
     // and the rest is reserved for the caller thread
diff --git a/src/core/hle/kernel/semaphore.h b/src/core/hle/kernel/semaphore.h
index 526be6812..ff6a4434a 100644
--- a/src/core/hle/kernel/semaphore.h
+++ b/src/core/hle/kernel/semaphore.h
@@ -6,6 +6,7 @@
 
 #include <string>
 #include <queue>
+#include <boost/serialization/export.hpp>
 #include "common/common_types.h"
 #include "core/hle/kernel/object.h"
 #include "core/hle/kernel/wait_object.h"
@@ -15,7 +16,7 @@ namespace Kernel {
 
 class Semaphore final : public WaitObject {
 public:
-    explicit Semaphore(KernelSystem& kernel);
+    explicit Semaphore();
     ~Semaphore() override;
 
     std::string GetTypeName() const override {
@@ -57,3 +58,5 @@ private:
 };
 
 } // namespace Kernel
+
+BOOST_CLASS_EXPORT_KEY(Kernel::Semaphore)
diff --git a/src/core/hle/kernel/shared_memory.h b/src/core/hle/kernel/shared_memory.h
index 0d781cfcc..42d783513 100644
--- a/src/core/hle/kernel/shared_memory.h
+++ b/src/core/hle/kernel/shared_memory.h
@@ -6,6 +6,7 @@
 
 #include <string>
 #include <utility>
+#include <boost/serialization/export.hpp>
 #include "common/common_types.h"
 #include "core/hle/kernel/object.h"
 #include "core/hle/kernel/process.h"
@@ -104,6 +105,21 @@ private:
 
     friend class KernelSystem;
     KernelSystem& kernel;
+
+    template <class Archive>
+    void serialize(Archive& ar, const unsigned int file_version)
+    {
+        ar & linear_heap_phys_offset;
+        // TODO: backing blocks u8* (this is always FCRAM I think)
+        ar & size;
+        ar & permissions;
+        ar & other_permissions;
+        ar & owner_process;
+        ar & base_address;
+        ar & name;
+        ar & *(reinterpret_cast<MemoryRegionInfo::IntervalSet::ImplSetT*>(&holding_memory));;
+    }
+    friend class boost::serialization::access;
 };
 
 } // namespace Kernel
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index 9fa5df33a..409dcc886 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -25,6 +25,8 @@
 #include "core/hle/result.h"
 #include "core/memory.h"
 
+SERIALIZE_EXPORT_IMPL(Kernel::Thread)
+
 namespace Kernel {
 
 template <class Archive>
diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h
index fc9170675..b423392e3 100644
--- a/src/core/hle/kernel/thread.h
+++ b/src/core/hle/kernel/thread.h
@@ -12,6 +12,7 @@
 #include <boost/serialization/shared_ptr.hpp>
 #include <boost/serialization/unordered_map.hpp>
 #include <boost/serialization/vector.hpp>
+#include <boost/serialization/export.hpp>
 #include "common/common_types.h"
 #include "common/thread_queue_list.h"
 #include "core/arm/arm_interface.h"
@@ -337,3 +338,5 @@ std::shared_ptr<Thread> SetupMainThread(KernelSystem& kernel, u32 entry_point, u
                                         std::shared_ptr<Process> owner_process);
 
 } // namespace Kernel
+
+BOOST_CLASS_EXPORT_KEY(Kernel::Thread)
diff --git a/src/core/hle/kernel/vm_manager.h b/src/core/hle/kernel/vm_manager.h
index 6711c9b4d..e930fc64a 100644
--- a/src/core/hle/kernel/vm_manager.h
+++ b/src/core/hle/kernel/vm_manager.h
@@ -94,6 +94,7 @@ private:
         ar & permissions;
         ar & meminfo_state;
         // TODO: backing memory ref
+        // backing memory can be: Physical/FCRAM pointer, config mem, shared page
         ar & paddr;
         ar & mmio_handler;
     }