From 08831eecf77cedd3c4192ebab5a9c485fb15d51e Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Wed, 4 Jan 2023 19:15:45 -0300
Subject: IPC refactor part 3+4: New server HIPC message processor (#4188)

* IPC refactor part 3 + 4: New server HIPC message processor with source generator based serialization

* Make types match on calls to AlignUp/AlignDown

* Formatting

* Address some PR feedback

* Move BitfieldExtensions to Ryujinx.Common.Utilities and consolidate implementations

* Rename Reader/Writer to SpanReader/SpanWriter and move to Ryujinx.Common.Memory

* Implement EventType

* Address more PR feedback

* Log request processing errors since they are not normal

* Rename waitable to multiwait and add missing lock

* PR feedback

* Ac_K PR feedback
---
 Ryujinx.HLE/HOS/Kernel/Memory/KCodeMemory.cs | 43 ++++++++++++++--------------
 1 file changed, 22 insertions(+), 21 deletions(-)

(limited to 'Ryujinx.HLE/HOS/Kernel/Memory/KCodeMemory.cs')

diff --git a/Ryujinx.HLE/HOS/Kernel/Memory/KCodeMemory.cs b/Ryujinx.HLE/HOS/Kernel/Memory/KCodeMemory.cs
index 05cf4a4f..11474e49 100644
--- a/Ryujinx.HLE/HOS/Kernel/Memory/KCodeMemory.cs
+++ b/Ryujinx.HLE/HOS/Kernel/Memory/KCodeMemory.cs
@@ -1,6 +1,7 @@
 using Ryujinx.Common;
 using Ryujinx.HLE.HOS.Kernel.Common;
 using Ryujinx.HLE.HOS.Kernel.Process;
+using Ryujinx.Horizon.Common;
 using System;
 using System.Diagnostics;
 
@@ -21,13 +22,13 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
             _lock = new object();
         }
 
-        public KernelResult Initialize(ulong address, ulong size)
+        public Result Initialize(ulong address, ulong size)
         {
             Owner = KernelStatic.GetCurrentProcess();
 
-            KernelResult result = Owner.MemoryManager.BorrowCodeMemory(_pageList, address, size);
+            Result result = Owner.MemoryManager.BorrowCodeMemory(_pageList, address, size);
 
-            if (result != KernelResult.Success)
+            if (result != Result.Success)
             {
                 return result;
             }
@@ -39,10 +40,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
             _isMapped = false;
             _isOwnerMapped = false;
 
-            return KernelResult.Success;
+            return Result.Success;
         }
 
-        public KernelResult Map(ulong address, ulong size, KMemoryPermission perm)
+        public Result Map(ulong address, ulong size, KMemoryPermission perm)
         {
             if (_pageList.GetPagesCount() != BitUtils.DivRoundUp<ulong>(size, (ulong)KPageTableBase.PageSize))
             {
@@ -58,9 +59,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
 
                 KProcess process = KernelStatic.GetCurrentProcess();
 
-                KernelResult result = process.MemoryManager.MapPages(address, _pageList, MemoryState.CodeWritable, KMemoryPermission.ReadAndWrite);
+                Result result = process.MemoryManager.MapPages(address, _pageList, MemoryState.CodeWritable, KMemoryPermission.ReadAndWrite);
 
-                if (result != KernelResult.Success)
+                if (result != Result.Success)
                 {
                     return result;
                 }
@@ -68,10 +69,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
                 _isMapped = true;
             }
 
-            return KernelResult.Success;
+            return Result.Success;
         }
 
-        public KernelResult MapToOwner(ulong address, ulong size, KMemoryPermission permission)
+        public Result MapToOwner(ulong address, ulong size, KMemoryPermission permission)
         {
             if (_pageList.GetPagesCount() != BitUtils.DivRoundUp<ulong>(size, (ulong)KPageTableBase.PageSize))
             {
@@ -87,9 +88,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
 
                 Debug.Assert(permission == KMemoryPermission.Read || permission == KMemoryPermission.ReadAndExecute);
 
-                KernelResult result = Owner.MemoryManager.MapPages(address, _pageList, MemoryState.CodeReadOnly, permission);
+                Result result = Owner.MemoryManager.MapPages(address, _pageList, MemoryState.CodeReadOnly, permission);
 
-                if (result != KernelResult.Success)
+                if (result != Result.Success)
                 {
                     return result;
                 }
@@ -97,10 +98,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
                 _isOwnerMapped = true;
             }
 
-            return KernelResult.Success;
+            return Result.Success;
         }
 
-        public KernelResult Unmap(ulong address, ulong size)
+        public Result Unmap(ulong address, ulong size)
         {
             if (_pageList.GetPagesCount() != BitUtils.DivRoundUp<ulong>(size, (ulong)KPageTableBase.PageSize))
             {
@@ -111,9 +112,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
             {
                 KProcess process = KernelStatic.GetCurrentProcess();
 
-                KernelResult result = process.MemoryManager.UnmapPages(address, _pageList, MemoryState.CodeWritable);
+                Result result = process.MemoryManager.UnmapPages(address, _pageList, MemoryState.CodeWritable);
 
-                if (result != KernelResult.Success)
+                if (result != Result.Success)
                 {
                     return result;
                 }
@@ -123,10 +124,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
                 _isMapped = false;
             }
 
-            return KernelResult.Success;
+            return Result.Success;
         }
 
-        public KernelResult UnmapFromOwner(ulong address, ulong size)
+        public Result UnmapFromOwner(ulong address, ulong size)
         {
             if (_pageList.GetPagesCount() != BitUtils.DivRoundUp<ulong>(size, KPageTableBase.PageSize))
             {
@@ -135,9 +136,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
 
             lock (_lock)
             {
-                KernelResult result = Owner.MemoryManager.UnmapPages(address, _pageList, MemoryState.CodeReadOnly);
+                Result result = Owner.MemoryManager.UnmapPages(address, _pageList, MemoryState.CodeReadOnly);
 
-                if (result != KernelResult.Success)
+                if (result != Result.Success)
                 {
                     return result;
                 }
@@ -147,7 +148,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
                 _isOwnerMapped = false;
             }
 
-            return KernelResult.Success;
+            return Result.Success;
         }
 
         protected override void Destroy()
@@ -156,7 +157,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
             {
                 ulong size = _pageList.GetPagesCount() * KPageTableBase.PageSize;
 
-                if (Owner.MemoryManager.UnborrowCodeMemory(_address, size, _pageList) != KernelResult.Success)
+                if (Owner.MemoryManager.UnborrowCodeMemory(_address, size, _pageList) != Result.Success)
                 {
                     throw new InvalidOperationException("Unexpected failure restoring transfer memory attributes.");
                 }
-- 
cgit v1.2.3-70-g09d2