From 0151f593ad9d486a48495c24a81621deb7c801f2 Mon Sep 17 00:00:00 2001
From: Liam <byteslice@airmail.cc>
Date: Sat, 17 Feb 2024 02:05:26 -0500
Subject: [PATCH] service: remove strong dependency on kernel headers

---
 src/core/hle/service/am/applet.h              |  1 +
 .../hle/service/am/applet_message_queue.cpp   |  1 +
 .../am/service/applet_common_functions.cpp    |  1 +
 .../am/service/application_functions.h        |  3 +-
 .../am/service/library_applet_creator.h       |  4 ++
 src/core/hle/service/audio/audio_device.cpp   |  2 +
 src/core/hle/service/audio/audio_in.cpp       |  1 +
 src/core/hle/service/audio/audio_renderer.cpp |  2 +
 .../bcat/news/newly_arrived_event_holder.cpp  | 12 ++---
 .../bcat/news/newly_arrived_event_holder.h    |  3 +-
 .../bcat/news/overwrite_event_holder.cpp      | 11 ++---
 .../bcat/news/overwrite_event_holder.h        |  3 +-
 src/core/hle/service/bcat/service_creator.cpp |  1 +
 src/core/hle/service/cmif_serialization.h     |  3 +-
 src/core/hle/service/cmif_types.h             |  1 +
 src/core/hle/service/glue/notif.cpp           | 15 +++----
 src/core/hle/service/hid/hid_server.cpp       |  6 +--
 src/core/hle/service/hle_ipc.cpp              | 45 ++++++++++++++++++-
 src/core/hle/service/hle_ipc.h                | 23 +++-------
 src/core/hle/service/ipc_helpers.h            | 18 +-------
 src/core/hle/service/ldn/ldn.cpp              |  1 +
 .../ns/application_manager_interface.cpp      |  1 +
 ...nly_application_control_data_interface.cpp |  1 +
 .../service/nvdrv/devices/nvhost_as_gpu.cpp   |  1 +
 .../hle/service/nvdrv/nvdrv_interface.cpp     |  2 +-
 src/core/hle/service/psc/time/alarms.cpp      |  1 +
 .../service/set/system_settings_server.cpp    |  1 +
 27 files changed, 98 insertions(+), 66 deletions(-)

diff --git a/src/core/hle/service/am/applet.h b/src/core/hle/service/am/applet.h
index ad602153e5..6f465d8dad 100644
--- a/src/core/hle/service/am/applet.h
+++ b/src/core/hle/service/am/applet.h
@@ -6,6 +6,7 @@
 #include <mutex>
 
 #include "common/math_util.h"
+#include "core/hle/result.h"
 #include "core/hle/service/apm/apm_controller.h"
 #include "core/hle/service/caps/caps_types.h"
 #include "core/hle/service/kernel_helpers.h"
diff --git a/src/core/hle/service/am/applet_message_queue.cpp b/src/core/hle/service/am/applet_message_queue.cpp
index 83c3c5a551..d5b5d01613 100644
--- a/src/core/hle/service/am/applet_message_queue.cpp
+++ b/src/core/hle/service/am/applet_message_queue.cpp
@@ -1,6 +1,7 @@
 // SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
 // SPDX-License-Identifier: GPL-2.0-or-later
 
+#include "core/hle/kernel/k_event.h"
 #include "core/hle/service/am/applet_message_queue.h"
 #include "core/hle/service/ipc_helpers.h"
 
diff --git a/src/core/hle/service/am/service/applet_common_functions.cpp b/src/core/hle/service/am/service/applet_common_functions.cpp
index 0f29ab285b..e4fb082948 100644
--- a/src/core/hle/service/am/service/applet_common_functions.cpp
+++ b/src/core/hle/service/am/service/applet_common_functions.cpp
@@ -1,6 +1,7 @@
 // SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
 // SPDX-License-Identifier: GPL-2.0-or-later
 
+#include "core/core.h"
 #include "core/hle/service/am/applet.h"
 #include "core/hle/service/am/service/applet_common_functions.h"
 #include "core/hle/service/cmif_serialization.h"
diff --git a/src/core/hle/service/am/service/application_functions.h b/src/core/hle/service/am/service/application_functions.h
index 10025a152b..1522f27a79 100644
--- a/src/core/hle/service/am/service/application_functions.h
+++ b/src/core/hle/service/am/service/application_functions.h
@@ -14,7 +14,8 @@ enum class SaveDataType : u8;
 
 namespace Kernel {
 class KReadableEvent;
-}
+class KTransferMemory;
+} // namespace Kernel
 
 namespace Service::AM {
 
diff --git a/src/core/hle/service/am/service/library_applet_creator.h b/src/core/hle/service/am/service/library_applet_creator.h
index fe6d40eb33..d629a7019a 100644
--- a/src/core/hle/service/am/service/library_applet_creator.h
+++ b/src/core/hle/service/am/service/library_applet_creator.h
@@ -7,6 +7,10 @@
 #include "core/hle/service/cmif_types.h"
 #include "core/hle/service/service.h"
 
+namespace Kernel {
+class KTransferMemory;
+}
+
 namespace Service::AM {
 
 struct Applet;
diff --git a/src/core/hle/service/audio/audio_device.cpp b/src/core/hle/service/audio/audio_device.cpp
index 438f3cccdf..40074fb249 100644
--- a/src/core/hle/service/audio/audio_device.cpp
+++ b/src/core/hle/service/audio/audio_device.cpp
@@ -3,6 +3,8 @@
 
 #include "audio_core/audio_core.h"
 #include "common/string_util.h"
+#include "core/core.h"
+#include "core/hle/kernel/k_event.h"
 #include "core/hle/service/audio/audio_device.h"
 #include "core/hle/service/cmif_serialization.h"
 
diff --git a/src/core/hle/service/audio/audio_in.cpp b/src/core/hle/service/audio/audio_in.cpp
index 416803acc3..a1113b9bfd 100644
--- a/src/core/hle/service/audio/audio_in.cpp
+++ b/src/core/hle/service/audio/audio_in.cpp
@@ -1,6 +1,7 @@
 // SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
 // SPDX-License-Identifier: GPL-2.0-or-later
 
+#include "core/hle/kernel/k_process.h"
 #include "core/hle/service/audio/audio_in.h"
 #include "core/hle/service/cmif_serialization.h"
 #include "core/hle/service/ipc_helpers.h"
diff --git a/src/core/hle/service/audio/audio_renderer.cpp b/src/core/hle/service/audio/audio_renderer.cpp
index fc4aad2339..6d1011ec32 100644
--- a/src/core/hle/service/audio/audio_renderer.cpp
+++ b/src/core/hle/service/audio/audio_renderer.cpp
@@ -1,6 +1,8 @@
 // SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
 // SPDX-License-Identifier: GPL-2.0-or-later
 
+#include "core/hle/kernel/k_event.h"
+#include "core/hle/kernel/k_process.h"
 #include "core/hle/service/audio/audio_renderer.h"
 #include "core/hle/service/cmif_serialization.h"
 
diff --git a/src/core/hle/service/bcat/news/newly_arrived_event_holder.cpp b/src/core/hle/service/bcat/news/newly_arrived_event_holder.cpp
index ed393f7a26..b3e49a357f 100644
--- a/src/core/hle/service/bcat/news/newly_arrived_event_holder.cpp
+++ b/src/core/hle/service/bcat/news/newly_arrived_event_holder.cpp
@@ -7,9 +7,8 @@
 namespace Service::News {
 
 INewlyArrivedEventHolder::INewlyArrivedEventHolder(Core::System& system_)
-    : ServiceFramework{system_, "INewlyArrivedEventHolder"}, service_context{
-                                                                 system_,
-                                                                 "INewlyArrivedEventHolder"} {
+    : ServiceFramework{system_, "INewlyArrivedEventHolder"},
+      service_context{system_, "INewlyArrivedEventHolder"}, arrived_event{service_context} {
     // clang-format off
     static const FunctionInfo functions[] = {
         {0, D<&INewlyArrivedEventHolder::Get>, "Get"},
@@ -17,17 +16,14 @@ INewlyArrivedEventHolder::INewlyArrivedEventHolder(Core::System& system_)
     // clang-format on
 
     RegisterHandlers(functions);
-    arrived_event = service_context.CreateEvent("INewlyArrivedEventHolder::ArrivedEvent");
 }
 
-INewlyArrivedEventHolder::~INewlyArrivedEventHolder() {
-    service_context.CloseEvent(arrived_event);
-}
+INewlyArrivedEventHolder::~INewlyArrivedEventHolder() = default;
 
 Result INewlyArrivedEventHolder::Get(OutCopyHandle<Kernel::KReadableEvent> out_event) {
     LOG_INFO(Service_BCAT, "called");
 
-    *out_event = &arrived_event->GetReadableEvent();
+    *out_event = arrived_event.GetHandle();
     R_SUCCEED();
 }
 
diff --git a/src/core/hle/service/bcat/news/newly_arrived_event_holder.h b/src/core/hle/service/bcat/news/newly_arrived_event_holder.h
index 6cc9ae0997..21a0a518af 100644
--- a/src/core/hle/service/bcat/news/newly_arrived_event_holder.h
+++ b/src/core/hle/service/bcat/news/newly_arrived_event_holder.h
@@ -5,6 +5,7 @@
 
 #include "core/hle/service/cmif_types.h"
 #include "core/hle/service/kernel_helpers.h"
+#include "core/hle/service/os/event.h"
 #include "core/hle/service/service.h"
 
 namespace Core {
@@ -26,8 +27,8 @@ public:
 private:
     Result Get(OutCopyHandle<Kernel::KReadableEvent> out_event);
 
-    Kernel::KEvent* arrived_event;
     KernelHelpers::ServiceContext service_context;
+    Event arrived_event;
 };
 
 } // namespace Service::News
diff --git a/src/core/hle/service/bcat/news/overwrite_event_holder.cpp b/src/core/hle/service/bcat/news/overwrite_event_holder.cpp
index 1712971e4d..dfdcb49351 100644
--- a/src/core/hle/service/bcat/news/overwrite_event_holder.cpp
+++ b/src/core/hle/service/bcat/news/overwrite_event_holder.cpp
@@ -7,8 +7,8 @@
 namespace Service::News {
 
 IOverwriteEventHolder::IOverwriteEventHolder(Core::System& system_)
-    : ServiceFramework{system_, "IOverwriteEventHolder"}, service_context{system_,
-                                                                          "IOverwriteEventHolder"} {
+    : ServiceFramework{system_, "IOverwriteEventHolder"},
+      service_context{system_, "IOverwriteEventHolder"}, overwrite_event{service_context} {
     // clang-format off
     static const FunctionInfo functions[] = {
         {0, D<&IOverwriteEventHolder::Get>, "Get"},
@@ -16,17 +16,14 @@ IOverwriteEventHolder::IOverwriteEventHolder(Core::System& system_)
     // clang-format on
 
     RegisterHandlers(functions);
-    overwrite_event = service_context.CreateEvent("IOverwriteEventHolder::OverwriteEvent");
 }
 
-IOverwriteEventHolder::~IOverwriteEventHolder() {
-    service_context.CloseEvent(overwrite_event);
-}
+IOverwriteEventHolder::~IOverwriteEventHolder() = default;
 
 Result IOverwriteEventHolder::Get(OutCopyHandle<Kernel::KReadableEvent> out_event) {
     LOG_INFO(Service_BCAT, "called");
 
-    *out_event = &overwrite_event->GetReadableEvent();
+    *out_event = overwrite_event.GetHandle();
     R_SUCCEED();
 }
 
diff --git a/src/core/hle/service/bcat/news/overwrite_event_holder.h b/src/core/hle/service/bcat/news/overwrite_event_holder.h
index cdc87d782c..14495b06fb 100644
--- a/src/core/hle/service/bcat/news/overwrite_event_holder.h
+++ b/src/core/hle/service/bcat/news/overwrite_event_holder.h
@@ -5,6 +5,7 @@
 
 #include "core/hle/service/cmif_types.h"
 #include "core/hle/service/kernel_helpers.h"
+#include "core/hle/service/os/event.h"
 #include "core/hle/service/service.h"
 
 namespace Core {
@@ -26,8 +27,8 @@ public:
 private:
     Result Get(OutCopyHandle<Kernel::KReadableEvent> out_event);
 
-    Kernel::KEvent* overwrite_event;
     KernelHelpers::ServiceContext service_context;
+    Event overwrite_event;
 };
 
 } // namespace Service::News
diff --git a/src/core/hle/service/bcat/service_creator.cpp b/src/core/hle/service/bcat/service_creator.cpp
index ca339e5a68..d911824801 100644
--- a/src/core/hle/service/bcat/service_creator.cpp
+++ b/src/core/hle/service/bcat/service_creator.cpp
@@ -1,6 +1,7 @@
 // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
 // SPDX-License-Identifier: GPL-2.0-or-later
 
+#include "core/core.h"
 #include "core/hle/service/bcat/bcat_service.h"
 #include "core/hle/service/bcat/delivery_cache_storage_service.h"
 #include "core/hle/service/bcat/service_creator.h"
diff --git a/src/core/hle/service/cmif_serialization.h b/src/core/hle/service/cmif_serialization.h
index 5a5f610f34..c8973413cf 100644
--- a/src/core/hle/service/cmif_serialization.h
+++ b/src/core/hle/service/cmif_serialization.h
@@ -3,6 +3,7 @@
 
 #pragma once
 
+#include "common/alignment.h"
 #include "common/div_ceil.h"
 
 #include "core/hle/service/cmif_types.h"
@@ -284,7 +285,7 @@ void ReadInArgument(bool is_domain, CallArguments& args, const u8* raw_data, HLE
 
             return ReadInArgument<MethodArguments, CallArguments, ArgAlign, ArgEnd, HandleIndex, InBufferIndex, OutBufferIndex, true, ArgIndex + 1>(is_domain, args, raw_data, ctx, temp);
         } else if constexpr (ArgumentTraits<ArgType>::Type == ArgumentType::InCopyHandle) {
-            std::get<ArgIndex>(args) = ctx.GetObjectFromHandle<typename ArgType::Type>(ctx.GetCopyHandle(HandleIndex)).GetPointerUnsafe();
+            std::get<ArgIndex>(args) = ctx.GetObjectFromHandle<typename ArgType::Type>(ctx.GetCopyHandle(HandleIndex));
 
             return ReadInArgument<MethodArguments, CallArguments, PrevAlign, DataOffset, HandleIndex + 1, InBufferIndex, OutBufferIndex, RawDataFinished, ArgIndex + 1>(is_domain, args, raw_data, ctx, temp);
         } else if constexpr (ArgumentTraits<ArgType>::Type == ArgumentType::InLargeData) {
diff --git a/src/core/hle/service/cmif_types.h b/src/core/hle/service/cmif_types.h
index dad358b87c..c42197bc6e 100644
--- a/src/core/hle/service/cmif_types.h
+++ b/src/core/hle/service/cmif_types.h
@@ -7,6 +7,7 @@
 #include <span>
 
 #include "common/common_types.h"
+#include "core/hle/result.h"
 
 namespace Service {
 
diff --git a/src/core/hle/service/glue/notif.cpp b/src/core/hle/service/glue/notif.cpp
index 5a03d34c12..790eb77149 100644
--- a/src/core/hle/service/glue/notif.cpp
+++ b/src/core/hle/service/glue/notif.cpp
@@ -10,6 +10,7 @@
 #include "core/hle/service/glue/notif.h"
 #include "core/hle/service/ipc_helpers.h"
 #include "core/hle/service/kernel_helpers.h"
+#include "core/hle/service/os/event.h"
 
 namespace Service::Glue {
 
@@ -178,7 +179,8 @@ class INotificationSystemEventAccessor final
 public:
     explicit INotificationSystemEventAccessor(Core::System& system_)
         : ServiceFramework{system_, "INotificationSystemEventAccessor"},
-          service_context{system_, "INotificationSystemEventAccessor"} {
+          service_context{system_, "INotificationSystemEventAccessor"}, notification_event{
+                                                                            service_context} {
         // clang-format off
         static const FunctionInfo functions[] = {
             {0, D<&INotificationSystemEventAccessor::GetSystemEvent>, "GetSystemEvent"},
@@ -186,25 +188,20 @@ public:
         // clang-format on
 
         RegisterHandlers(functions);
-
-        notification_event =
-            service_context.CreateEvent("INotificationSystemEventAccessor:NotificationEvent");
     }
 
-    ~INotificationSystemEventAccessor() {
-        service_context.CloseEvent(notification_event);
-    }
+    ~INotificationSystemEventAccessor() = default;
 
 private:
     Result GetSystemEvent(OutCopyHandle<Kernel::KReadableEvent> out_readable_event) {
         LOG_WARNING(Service_NOTIF, "(STUBBED) called");
 
-        *out_readable_event = &notification_event->GetReadableEvent();
+        *out_readable_event = notification_event.GetHandle();
         R_SUCCEED();
     }
 
     KernelHelpers::ServiceContext service_context;
-    Kernel::KEvent* notification_event;
+    Event notification_event;
 };
 
 INotificationServices::INotificationServices(Core::System& system_)
diff --git a/src/core/hle/service/hid/hid_server.cpp b/src/core/hle/service/hid/hid_server.cpp
index 3603d8ccf4..8a32d5e804 100644
--- a/src/core/hle/service/hid/hid_server.cpp
+++ b/src/core/hle/service/hid/hid_server.cpp
@@ -1982,7 +1982,7 @@ void IHidServer::InitializeSevenSixAxisSensor(HLERequestContext& ctx) {
 
     auto t_mem_1 = ctx.GetObjectFromHandle<Kernel::KTransferMemory>(t_mem_1_handle);
 
-    if (t_mem_1.IsNull()) {
+    if (!t_mem_1) {
         LOG_ERROR(Service_HID, "t_mem_1 is a nullptr for handle=0x{:08X}", t_mem_1_handle);
         IPC::ResponseBuilder rb{ctx, 2};
         rb.Push(ResultUnknown);
@@ -1991,7 +1991,7 @@ void IHidServer::InitializeSevenSixAxisSensor(HLERequestContext& ctx) {
 
     auto t_mem_2 = ctx.GetObjectFromHandle<Kernel::KTransferMemory>(t_mem_2_handle);
 
-    if (t_mem_2.IsNull()) {
+    if (!t_mem_2) {
         LOG_ERROR(Service_HID, "t_mem_2 is a nullptr for handle=0x{:08X}", t_mem_2_handle);
         IPC::ResponseBuilder rb{ctx, 2};
         rb.Push(ResultUnknown);
@@ -2272,7 +2272,7 @@ void IHidServer::WritePalmaWaveEntry(HLERequestContext& ctx) {
 
     auto t_mem = ctx.GetObjectFromHandle<Kernel::KTransferMemory>(t_mem_handle);
 
-    if (t_mem.IsNull()) {
+    if (!t_mem) {
         LOG_ERROR(Service_HID, "t_mem is a nullptr for handle=0x{:08X}", t_mem_handle);
         IPC::ResponseBuilder rb{ctx, 2};
         rb.Push(ResultUnknown);
diff --git a/src/core/hle/service/hle_ipc.cpp b/src/core/hle/service/hle_ipc.cpp
index e0367e774c..0b20afea6b 100644
--- a/src/core/hle/service/hle_ipc.cpp
+++ b/src/core/hle/service/hle_ipc.cpp
@@ -11,17 +11,21 @@
 #include "common/common_funcs.h"
 #include "common/common_types.h"
 #include "common/logging/log.h"
-#include "common/scratch_buffer.h"
 #include "core/guest_memory.h"
 #include "core/hle/kernel/k_auto_object.h"
+#include "core/hle/kernel/k_code_memory.h"
 #include "core/hle/kernel/k_handle_table.h"
 #include "core/hle/kernel/k_process.h"
 #include "core/hle/kernel/k_server_port.h"
 #include "core/hle/kernel/k_server_session.h"
+#include "core/hle/kernel/k_session.h"
+#include "core/hle/kernel/k_shared_memory.h"
 #include "core/hle/kernel/k_thread.h"
+#include "core/hle/kernel/k_transfer_memory.h"
 #include "core/hle/kernel/kernel.h"
 #include "core/hle/service/hle_ipc.h"
 #include "core/hle/service/ipc_helpers.h"
+#include "core/hle/service/server_manager.h"
 #include "core/memory.h"
 
 namespace Service {
@@ -134,6 +138,45 @@ HLERequestContext::HLERequestContext(Kernel::KernelCore& kernel_, Core::Memory::
 
 HLERequestContext::~HLERequestContext() = default;
 
+template <typename T>
+T* HLERequestContext::GetObjectFromHandle(u32 handle) {
+    auto obj = client_handle_table->GetObjectForIpc(handle, thread);
+    if (obj.IsNotNull()) {
+        return obj->DynamicCast<T*>();
+    }
+    return nullptr;
+}
+
+template <typename T>
+void HLERequestContext::AddCopyObject(T* object) {
+    outgoing_copy_objects.push_back(object);
+}
+
+template <typename T>
+void HLERequestContext::AddMoveObject(T* object) {
+    outgoing_move_objects.push_back(object);
+}
+
+template Kernel::KCodeMemory* HLERequestContext::GetObjectFromHandle<Kernel::KCodeMemory>(
+    u32 handle);
+template Kernel::KProcess* HLERequestContext::GetObjectFromHandle<Kernel::KProcess>(u32 handle);
+template Kernel::KReadableEvent* HLERequestContext::GetObjectFromHandle<Kernel::KReadableEvent>(
+    u32 handle);
+template Kernel::KTransferMemory* HLERequestContext::GetObjectFromHandle<Kernel::KTransferMemory>(
+    u32 handle);
+
+template void HLERequestContext::AddCopyObject<Kernel::KProcess>(Kernel::KProcess* object);
+template void HLERequestContext::AddCopyObject<Kernel::KReadableEvent>(
+    Kernel::KReadableEvent* object);
+template void HLERequestContext::AddCopyObject<Kernel::KSharedMemory>(
+    Kernel::KSharedMemory* object);
+template void HLERequestContext::AddCopyObject<Kernel::KTransferMemory>(
+    Kernel::KTransferMemory* object);
+
+template void HLERequestContext::AddMoveObject<Kernel::KClientSession>(
+    Kernel::KClientSession* object);
+template void HLERequestContext::AddMoveObject<Kernel::KServerPort>(Kernel::KServerPort* object);
+
 void HLERequestContext::ParseCommandBuffer(u32_le* src_cmdbuf, bool incoming) {
     IPC::RequestParser rp(src_cmdbuf);
     command_header = rp.PopRaw<IPC::CommandHeader>();
diff --git a/src/core/hle/service/hle_ipc.h b/src/core/hle/service/hle_ipc.h
index c2e0e5e8c4..93a5c54b15 100644
--- a/src/core/hle/service/hle_ipc.h
+++ b/src/core/hle/service/hle_ipc.h
@@ -15,9 +15,9 @@
 #include "common/assert.h"
 #include "common/common_types.h"
 #include "common/concepts.h"
+#include "common/scratch_buffer.h"
 #include "common/swap.h"
 #include "core/hle/ipc.h"
-#include "core/hle/kernel/k_handle_table.h"
 #include "core/hle/kernel/svc_common.h"
 
 union Result;
@@ -40,9 +40,8 @@ class KAutoObject;
 class KernelCore;
 class KHandleTable;
 class KProcess;
+class KReadableEvent;
 class KServerSession;
-template <typename T>
-class KScopedAutoObject;
 class KThread;
 } // namespace Kernel
 
@@ -335,15 +334,13 @@ public:
         return incoming_move_handles.at(index);
     }
 
-    void AddMoveObject(Kernel::KAutoObject* object) {
-        outgoing_move_objects.emplace_back(object);
-    }
+    template <typename T>
+    void AddMoveObject(T* object);
 
     void AddMoveInterface(SessionRequestHandlerPtr s);
 
-    void AddCopyObject(Kernel::KAutoObject* object) {
-        outgoing_copy_objects.emplace_back(object);
-    }
+    template <typename T>
+    void AddCopyObject(T* object);
 
     void AddDomainObject(SessionRequestHandlerPtr object) {
         outgoing_domain_objects.emplace_back(std::move(object));
@@ -369,13 +366,7 @@ public:
     }
 
     template <typename T>
-    Kernel::KScopedAutoObject<T> GetObjectFromHandle(u32 handle) {
-        auto obj = client_handle_table->GetObjectForIpc(handle, thread);
-        if (obj.IsNotNull()) {
-            return obj->DynamicCast<T*>();
-        }
-        return nullptr;
-    }
+    T* GetObjectFromHandle(u32 handle);
 
     [[nodiscard]] std::shared_ptr<SessionRequestManager> GetManager() const {
         return manager.lock();
diff --git a/src/core/hle/service/ipc_helpers.h b/src/core/hle/service/ipc_helpers.h
index 4b02872fba..87f476210d 100644
--- a/src/core/hle/service/ipc_helpers.h
+++ b/src/core/hle/service/ipc_helpers.h
@@ -10,12 +10,8 @@
 #include "common/assert.h"
 #include "common/common_types.h"
 #include "core/hle/ipc.h"
-#include "core/hle/kernel/k_process.h"
-#include "core/hle/kernel/k_resource_limit.h"
-#include "core/hle/kernel/k_session.h"
 #include "core/hle/result.h"
 #include "core/hle/service/hle_ipc.h"
-#include "core/hle/service/server_manager.h"
 
 namespace IPC {
 
@@ -151,19 +147,7 @@ public:
         if (manager->IsDomain()) {
             context->AddDomainObject(std::move(iface));
         } else {
-            ASSERT(Kernel::GetCurrentProcess(kernel).GetResourceLimit()->Reserve(
-                Kernel::LimitableResource::SessionCountMax, 1));
-
-            auto* session = Kernel::KSession::Create(kernel);
-            session->Initialize(nullptr, 0);
-            Kernel::KSession::Register(kernel, session);
-
-            auto next_manager = std::make_shared<Service::SessionRequestManager>(
-                kernel, manager->GetServerManager());
-            next_manager->SetSessionHandler(iface);
-            manager->GetServerManager().RegisterSession(&session->GetServerSession(), next_manager);
-
-            context->AddMoveObject(&session->GetClientSession());
+            context->AddMoveInterface(std::move(iface));
         }
     }
 
diff --git a/src/core/hle/service/ldn/ldn.cpp b/src/core/hle/service/ldn/ldn.cpp
index f2d638c30e..34dce0cd2f 100644
--- a/src/core/hle/service/ldn/ldn.cpp
+++ b/src/core/hle/service/ldn/ldn.cpp
@@ -10,6 +10,7 @@
 #include "core/hle/service/ldn/sf_service_monitor.h"
 #include "core/hle/service/ldn/system_local_communication_service.h"
 #include "core/hle/service/ldn/user_local_communication_service.h"
+#include "core/hle/service/server_manager.h"
 
 namespace Service::LDN {
 
diff --git a/src/core/hle/service/ns/application_manager_interface.cpp b/src/core/hle/service/ns/application_manager_interface.cpp
index 7a91727f97..4e51d70d4a 100644
--- a/src/core/hle/service/ns/application_manager_interface.cpp
+++ b/src/core/hle/service/ns/application_manager_interface.cpp
@@ -1,6 +1,7 @@
 // SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
 // SPDX-License-Identifier: GPL-2.0-or-later
 
+#include "core/core.h"
 #include "core/file_sys/nca_metadata.h"
 #include "core/file_sys/registered_cache.h"
 #include "core/hle/service/cmif_serialization.h"
diff --git a/src/core/hle/service/ns/read_only_application_control_data_interface.cpp b/src/core/hle/service/ns/read_only_application_control_data_interface.cpp
index 9b2ca94a4f..9f71fdf9ee 100644
--- a/src/core/hle/service/ns/read_only_application_control_data_interface.cpp
+++ b/src/core/hle/service/ns/read_only_application_control_data_interface.cpp
@@ -2,6 +2,7 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 
 #include "common/settings.h"
+#include "core/core.h"
 #include "core/file_sys/control_metadata.h"
 #include "core/file_sys/patch_manager.h"
 #include "core/file_sys/vfs/vfs.h"
diff --git a/src/core/hle/service/nvdrv/devices/nvhost_as_gpu.cpp b/src/core/hle/service/nvdrv/devices/nvhost_as_gpu.cpp
index 68fe388741..643e48d797 100644
--- a/src/core/hle/service/nvdrv/devices/nvhost_as_gpu.cpp
+++ b/src/core/hle/service/nvdrv/devices/nvhost_as_gpu.cpp
@@ -7,6 +7,7 @@
 
 #include "common/alignment.h"
 #include "common/assert.h"
+#include "common/bit_util.h"
 #include "common/logging/log.h"
 #include "core/core.h"
 #include "core/hle/service/nvdrv/core/container.h"
diff --git a/src/core/hle/service/nvdrv/nvdrv_interface.cpp b/src/core/hle/service/nvdrv/nvdrv_interface.cpp
index 258970fd53..f341621d5a 100644
--- a/src/core/hle/service/nvdrv/nvdrv_interface.cpp
+++ b/src/core/hle/service/nvdrv/nvdrv_interface.cpp
@@ -173,7 +173,7 @@ void NVDRV::Initialize(HLERequestContext& ctx) {
 
     auto& container = nvdrv->GetContainer();
     auto process = ctx.GetObjectFromHandle<Kernel::KProcess>(process_handle);
-    session_id = container.OpenSession(process.GetPointerUnsafe());
+    session_id = container.OpenSession(process);
 
     is_initialized = true;
 }
diff --git a/src/core/hle/service/psc/time/alarms.cpp b/src/core/hle/service/psc/time/alarms.cpp
index 5e52c19f82..d3ec43b90f 100644
--- a/src/core/hle/service/psc/time/alarms.cpp
+++ b/src/core/hle/service/psc/time/alarms.cpp
@@ -1,6 +1,7 @@
 // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
 // SPDX-License-Identifier: GPL-2.0-or-later
 
+#include "common/alignment.h"
 #include "core/core.h"
 #include "core/hle/service/psc/time/alarms.h"
 #include "core/hle/service/psc/time/manager.h"
diff --git a/src/core/hle/service/set/system_settings_server.cpp b/src/core/hle/service/set/system_settings_server.cpp
index 93925f7832..d7ec4654fe 100644
--- a/src/core/hle/service/set/system_settings_server.cpp
+++ b/src/core/hle/service/set/system_settings_server.cpp
@@ -10,6 +10,7 @@
 #include "common/logging/log.h"
 #include "common/settings.h"
 #include "common/string_util.h"
+#include "common/thread.h"
 #include "core/core.h"
 #include "core/file_sys/content_archive.h"
 #include "core/file_sys/errors.h"