0
0
Fork 0
mirror of https://github.com/ryujinx-mirror/ryujinx.git synced 2024-12-23 02:45:45 +00:00

Implement a new physical memory manager and replace DeviceMemory (#856)

* Implement a new physical memory manager and replace DeviceMemory

* Proper generic constraints

* Fix debug build

* Add memory tests

* New CPU memory manager and general code cleanup

* Remove host memory management from CPU project, use Ryujinx.Memory instead

* Fix tests

* Document exceptions on MemoryBlock

* Fix leak on unix memory allocation

* Proper disposal of some objects on tests

* Fix JitCache not being set as initialized

* GetRef without checks for 8-bits and 16-bits CAS

* Add MemoryBlock destructor

* Throw in separate method to improve codegen

* Address PR feedback

* QueryModified improvements

* Fix memory write tracking not marking all pages as modified in some cases

* Simplify MarkRegionAsModified

* Remove XML doc for ghost param

* Add back optimization to avoid useless buffer updates

* Add Ryujinx.Cpu project, move MemoryManager there and remove MemoryBlockWrapper

* Some nits

* Do not perform address translation when size is 0

* Address PR feedback and format NativeInterface class

* Remove ghost parameter description

* Update Ryujinx.Cpu to .NET Core 3.1

* Address PR feedback

* Fix build

* Return a well defined value for GetPhysicalAddress with invalid VA, and do not return unmapped ranges as modified

* Typo
This commit is contained in:
gdkchan 2020-05-03 19:54:50 -03:00 committed by GitHub
parent 1758424208
commit f77694e4f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
126 changed files with 2176 additions and 2092 deletions

View file

@ -1,52 +1,23 @@
using ARMeilleure.IntermediateRepresentation; using System.Runtime.Intrinsics.X86;
using ARMeilleure.Translation;
namespace ARMeilleure.CodeGen.X86 namespace ARMeilleure.CodeGen.X86
{ {
static class HardwareCapabilities static class HardwareCapabilities
{ {
private delegate ulong GetFeatureInfo(); public static bool SupportsSse => Sse.IsSupported;
public static bool SupportsSse2 => Sse2.IsSupported;
private static ulong _featureInfo; public static bool SupportsSse3 => Sse3.IsSupported;
public static bool SupportsSsse3 => Ssse3.IsSupported;
public static bool SupportsSse3 => (_featureInfo & (1UL << 0)) != 0; public static bool SupportsSse41 => Sse41.IsSupported;
public static bool SupportsPclmulqdq => (_featureInfo & (1UL << 1)) != 0; public static bool SupportsSse42 => Sse42.IsSupported;
public static bool SupportsSsse3 => (_featureInfo & (1UL << 9)) != 0; public static bool SupportsPclmulqdq => Pclmulqdq.IsSupported;
public static bool SupportsFma => (_featureInfo & (1UL << 12)) != 0; public static bool SupportsFma => Fma.IsSupported;
public static bool SupportsCx16 => (_featureInfo & (1UL << 13)) != 0; public static bool SupportsPopcnt => Popcnt.IsSupported;
public static bool SupportsSse41 => (_featureInfo & (1UL << 19)) != 0; public static bool SupportsAesni => Aes.IsSupported;
public static bool SupportsSse42 => (_featureInfo & (1UL << 20)) != 0; public static bool SupportsAvx => Avx.IsSupported;
public static bool SupportsPopcnt => (_featureInfo & (1UL << 23)) != 0;
public static bool SupportsAesni => (_featureInfo & (1UL << 25)) != 0;
public static bool SupportsAvx => (_featureInfo & (1UL << 28)) != 0;
public static bool SupportsF16c => (_featureInfo & (1UL << 29)) != 0;
public static bool SupportsSse => (_featureInfo & (1UL << 32 + 25)) != 0;
public static bool SupportsSse2 => (_featureInfo & (1UL << 32 + 26)) != 0;
public static bool ForceLegacySse { get; set; } public static bool ForceLegacySse { get; set; }
public static bool SupportsVexEncoding => SupportsAvx && !ForceLegacySse; public static bool SupportsVexEncoding => SupportsAvx && !ForceLegacySse;
static HardwareCapabilities()
{
EmitterContext context = new EmitterContext();
Operand featureInfo = context.CpuId();
context.Return(featureInfo);
ControlFlowGraph cfg = context.GetControlFlowGraph();
OperandType[] argTypes = new OperandType[0];
GetFeatureInfo getFeatureInfo = Compiler.Compile<GetFeatureInfo>(
cfg,
argTypes,
OperandType.I64,
CompilerOptions.HighCq);
_featureInfo = getFeatureInfo();
}
} }
} }

View file

@ -17,7 +17,7 @@ namespace ARMeilleure.Decoders
// For lower code quality translation, we set a lower limit since we're blocking execution. // For lower code quality translation, we set a lower limit since we're blocking execution.
private const int MaxInstsPerFunctionLowCq = 500; private const int MaxInstsPerFunctionLowCq = 500;
public static Block[] DecodeBasicBlock(MemoryManager memory, ulong address, ExecutionMode mode) public static Block[] DecodeBasicBlock(IMemoryManager memory, ulong address, ExecutionMode mode)
{ {
Block block = new Block(address); Block block = new Block(address);
@ -26,7 +26,7 @@ namespace ARMeilleure.Decoders
return new Block[] { block }; return new Block[] { block };
} }
public static Block[] DecodeFunction(MemoryManager memory, ulong address, ExecutionMode mode, bool highCq) public static Block[] DecodeFunction(IMemoryManager memory, ulong address, ExecutionMode mode, bool highCq)
{ {
List<Block> blocks = new List<Block>(); List<Block> blocks = new List<Block>();
@ -42,7 +42,7 @@ namespace ARMeilleure.Decoders
{ {
if (!visited.TryGetValue(blkAddress, out Block block)) if (!visited.TryGetValue(blkAddress, out Block block))
{ {
if (opsCount > instructionLimit || !memory.IsMapped((long)blkAddress)) if (opsCount > instructionLimit || !memory.IsMapped(blkAddress))
{ {
return null; return null;
} }
@ -176,10 +176,10 @@ namespace ARMeilleure.Decoders
} }
private static void FillBlock( private static void FillBlock(
MemoryManager memory, IMemoryManager memory,
ExecutionMode mode, ExecutionMode mode,
Block block, Block block,
ulong limitAddress) ulong limitAddress)
{ {
ulong address = block.Address; ulong address = block.Address;
@ -302,9 +302,9 @@ namespace ARMeilleure.Decoders
opCode.Instruction.Name == InstName.Und; opCode.Instruction.Name == InstName.Und;
} }
public static OpCode DecodeOpCode(MemoryManager memory, ulong address, ExecutionMode mode) public static OpCode DecodeOpCode(IMemoryManager memory, ulong address, ExecutionMode mode)
{ {
int opCode = memory.ReadInt32((long)address); int opCode = memory.Read<int>(address);
InstDescriptor inst; InstDescriptor inst;

View file

@ -1,6 +1,5 @@
using ARMeilleure.Decoders; using ARMeilleure.Decoders;
using ARMeilleure.IntermediateRepresentation; using ARMeilleure.IntermediateRepresentation;
using ARMeilleure.Memory;
using ARMeilleure.Translation; using ARMeilleure.Translation;
using System; using System;
@ -11,6 +10,9 @@ namespace ARMeilleure.Instructions
{ {
static class InstEmitMemoryHelper static class InstEmitMemoryHelper
{ {
private const int PageBits = 12;
private const int PageMask = (1 << PageBits) - 1;
private enum Extension private enum Extension
{ {
Zx, Zx,
@ -318,28 +320,32 @@ namespace ARMeilleure.Instructions
private static Operand EmitAddressCheck(ArmEmitterContext context, Operand address, int size) private static Operand EmitAddressCheck(ArmEmitterContext context, Operand address, int size)
{ {
long addressCheckMask = ~(context.Memory.AddressSpaceSize - 1); ulong addressCheckMask = ~((1UL << context.Memory.AddressSpaceBits) - 1);
addressCheckMask |= (1u << size) - 1; addressCheckMask |= (1u << size) - 1;
return context.BitwiseAnd(address, Const(address.Type, addressCheckMask)); return context.BitwiseAnd(address, Const(address.Type, (long)addressCheckMask));
} }
private static Operand EmitPtPointerLoad(ArmEmitterContext context, Operand address, Operand lblFallbackPath) private static Operand EmitPtPointerLoad(ArmEmitterContext context, Operand address, Operand lblSlowPath)
{ {
Operand pte = Const(context.Memory.PageTable.ToInt64()); int ptLevelBits = context.Memory.AddressSpaceBits - 12; // 12 = Number of page bits.
int ptLevelSize = 1 << ptLevelBits;
int ptLevelMask = ptLevelSize - 1;
int bit = MemoryManager.PageBits; Operand pte = Const(context.Memory.PageTablePointer.ToInt64());
int bit = PageBits;
do do
{ {
Operand addrPart = context.ShiftRightUI(address, Const(bit)); Operand addrPart = context.ShiftRightUI(address, Const(bit));
bit += context.Memory.PtLevelBits; bit += ptLevelBits;
if (bit < context.Memory.AddressSpaceBits) if (bit < context.Memory.AddressSpaceBits)
{ {
addrPart = context.BitwiseAnd(addrPart, Const(addrPart.Type, context.Memory.PtLevelMask)); addrPart = context.BitwiseAnd(addrPart, Const(addrPart.Type, ptLevelMask));
} }
Operand pteOffset = context.ShiftLeft(addrPart, Const(3)); Operand pteOffset = context.ShiftLeft(addrPart, Const(3));
@ -355,20 +361,16 @@ namespace ARMeilleure.Instructions
} }
while (bit < context.Memory.AddressSpaceBits); while (bit < context.Memory.AddressSpaceBits);
Operand hasFlagSet = context.BitwiseAnd(pte, Const((long)MemoryManager.PteFlagsMask)); context.BranchIfTrue(lblSlowPath, context.ICompareLess(pte, Const(0L)));
context.BranchIfTrue(lblFallbackPath, hasFlagSet); Operand pageOffset = context.BitwiseAnd(address, Const(address.Type, PageMask));
Operand pageOffset = context.BitwiseAnd(address, Const(address.Type, MemoryManager.PageMask));
if (pageOffset.Type == OperandType.I32) if (pageOffset.Type == OperandType.I32)
{ {
pageOffset = context.ZeroExtend32(OperandType.I64, pageOffset); pageOffset = context.ZeroExtend32(OperandType.I64, pageOffset);
} }
Operand physAddr = context.Add(pte, pageOffset); return context.Add(pte, pageOffset);
return physAddr;
} }
private static void EmitReadIntFallback(ArmEmitterContext context, Operand address, int rt, int size) private static void EmitReadIntFallback(ArmEmitterContext context, Operand address, int rt, int size)

View file

@ -3,6 +3,7 @@ using ARMeilleure.State;
using ARMeilleure.Translation; using ARMeilleure.Translation;
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading;
namespace ARMeilleure.Instructions namespace ARMeilleure.Instructions
{ {
@ -12,18 +13,18 @@ namespace ARMeilleure.Instructions
private class ThreadContext private class ThreadContext
{ {
public ExecutionContext Context { get; } public State.ExecutionContext Context { get; }
public MemoryManager Memory { get; } public IMemoryManager Memory { get; }
public Translator Translator { get; } public Translator Translator { get; }
public ulong ExclusiveAddress { get; set; } public ulong ExclusiveAddress { get; set; }
public ulong ExclusiveValueLow { get; set; } public ulong ExclusiveValueLow { get; set; }
public ulong ExclusiveValueHigh { get; set; } public ulong ExclusiveValueHigh { get; set; }
public ThreadContext(ExecutionContext context, MemoryManager memory, Translator translator) public ThreadContext(State.ExecutionContext context, IMemoryManager memory, Translator translator)
{ {
Context = context; Context = context;
Memory = memory; Memory = memory;
Translator = translator; Translator = translator;
ExclusiveAddress = ulong.MaxValue; ExclusiveAddress = ulong.MaxValue;
@ -33,7 +34,7 @@ namespace ARMeilleure.Instructions
[ThreadStatic] [ThreadStatic]
private static ThreadContext _context; private static ThreadContext _context;
public static void RegisterThread(ExecutionContext context, MemoryManager memory, Translator translator) public static void RegisterThread(State.ExecutionContext context, IMemoryManager memory, Translator translator)
{ {
_context = new ThreadContext(context, memory, translator); _context = new ThreadContext(context, memory, translator);
} }
@ -70,7 +71,7 @@ namespace ARMeilleure.Instructions
Statistics.ResumeTimer(); Statistics.ResumeTimer();
} }
#region "System registers" #region "System registers"
public static ulong GetCtrEl0() public static ulong GetCtrEl0()
{ {
return (ulong)GetContext().CtrEl0; return (ulong)GetContext().CtrEl0;
@ -93,7 +94,8 @@ namespace ARMeilleure.Instructions
public static uint GetFpscr() public static uint GetFpscr()
{ {
ExecutionContext context = GetContext(); var context = GetContext();
uint result = (uint)(context.Fpsr & FPSR.A32Mask) | (uint)(context.Fpcr & FPCR.A32Mask); uint result = (uint)(context.Fpsr & FPSR.A32Mask) | (uint)(context.Fpcr & FPCR.A32Mask);
result |= context.GetFPstateFlag(FPState.NFlag) ? (1u << 31) : 0; result |= context.GetFPstateFlag(FPState.NFlag) ? (1u << 31) : 0;
@ -146,7 +148,7 @@ namespace ARMeilleure.Instructions
public static void SetFpscr(uint value) public static void SetFpscr(uint value)
{ {
ExecutionContext context = GetContext(); var context = GetContext();
context.SetFPstateFlag(FPState.NFlag, (value & (1u << 31)) != 0); context.SetFPstateFlag(FPState.NFlag, (value & (1u << 31)) != 0);
context.SetFPstateFlag(FPState.ZFlag, (value & (1u << 30)) != 0); context.SetFPstateFlag(FPState.ZFlag, (value & (1u << 30)) != 0);
@ -171,37 +173,37 @@ namespace ARMeilleure.Instructions
#region "Read" #region "Read"
public static byte ReadByte(ulong address) public static byte ReadByte(ulong address)
{ {
return GetMemoryManager().ReadByte((long)address); return GetMemoryManager().Read<byte>(address);
} }
public static ushort ReadUInt16(ulong address) public static ushort ReadUInt16(ulong address)
{ {
return GetMemoryManager().ReadUInt16((long)address); return GetMemoryManager().Read<ushort>(address);
} }
public static uint ReadUInt32(ulong address) public static uint ReadUInt32(ulong address)
{ {
return GetMemoryManager().ReadUInt32((long)address); return GetMemoryManager().Read<uint>(address);
} }
public static ulong ReadUInt64(ulong address) public static ulong ReadUInt64(ulong address)
{ {
return GetMemoryManager().ReadUInt64((long)address); return GetMemoryManager().Read<ulong>(address);
} }
public static V128 ReadVector128(ulong address) public static V128 ReadVector128(ulong address)
{ {
return GetMemoryManager().ReadVector128((long)address); return GetMemoryManager().Read<V128>(address);
} }
#endregion #endregion
#region "Read exclusive" #region "Read exclusive"
public static byte ReadByteExclusive(ulong address) public static byte ReadByteExclusive(ulong address)
{ {
byte value = _context.Memory.ReadByte((long)address); byte value = _context.Memory.Read<byte>(address);
_context.ExclusiveAddress = GetMaskedExclusiveAddress(address); _context.ExclusiveAddress = GetMaskedExclusiveAddress(address);
_context.ExclusiveValueLow = value; _context.ExclusiveValueLow = value;
_context.ExclusiveValueHigh = 0; _context.ExclusiveValueHigh = 0;
return value; return value;
@ -209,10 +211,10 @@ namespace ARMeilleure.Instructions
public static ushort ReadUInt16Exclusive(ulong address) public static ushort ReadUInt16Exclusive(ulong address)
{ {
ushort value = _context.Memory.ReadUInt16((long)address); ushort value = _context.Memory.Read<ushort>(address);
_context.ExclusiveAddress = GetMaskedExclusiveAddress(address); _context.ExclusiveAddress = GetMaskedExclusiveAddress(address);
_context.ExclusiveValueLow = value; _context.ExclusiveValueLow = value;
_context.ExclusiveValueHigh = 0; _context.ExclusiveValueHigh = 0;
return value; return value;
@ -220,10 +222,10 @@ namespace ARMeilleure.Instructions
public static uint ReadUInt32Exclusive(ulong address) public static uint ReadUInt32Exclusive(ulong address)
{ {
uint value = _context.Memory.ReadUInt32((long)address); uint value = _context.Memory.Read<uint>(address);
_context.ExclusiveAddress = GetMaskedExclusiveAddress(address); _context.ExclusiveAddress = GetMaskedExclusiveAddress(address);
_context.ExclusiveValueLow = value; _context.ExclusiveValueLow = value;
_context.ExclusiveValueHigh = 0; _context.ExclusiveValueHigh = 0;
return value; return value;
@ -231,10 +233,10 @@ namespace ARMeilleure.Instructions
public static ulong ReadUInt64Exclusive(ulong address) public static ulong ReadUInt64Exclusive(ulong address)
{ {
ulong value = _context.Memory.ReadUInt64((long)address); ulong value = _context.Memory.Read<ulong>(address);
_context.ExclusiveAddress = GetMaskedExclusiveAddress(address); _context.ExclusiveAddress = GetMaskedExclusiveAddress(address);
_context.ExclusiveValueLow = value; _context.ExclusiveValueLow = value;
_context.ExclusiveValueHigh = 0; _context.ExclusiveValueHigh = 0;
return value; return value;
@ -242,54 +244,60 @@ namespace ARMeilleure.Instructions
public static V128 ReadVector128Exclusive(ulong address) public static V128 ReadVector128Exclusive(ulong address)
{ {
V128 value = _context.Memory.AtomicLoadInt128((long)address); V128 value = MemoryManagerPal.AtomicLoad128(ref _context.Memory.GetRef<V128>(address));
_context.ExclusiveAddress = GetMaskedExclusiveAddress(address); _context.ExclusiveAddress = GetMaskedExclusiveAddress(address);
_context.ExclusiveValueLow = value.Extract<ulong>(0); _context.ExclusiveValueLow = value.Extract<ulong>(0);
_context.ExclusiveValueHigh = value.Extract<ulong>(1); _context.ExclusiveValueHigh = value.Extract<ulong>(1);
return value; return value;
} }
#endregion #endregion
#region "Write" #region "Write"
public static void WriteByte(ulong address, byte value) public static void WriteByte(ulong address, byte value)
{ {
GetMemoryManager().WriteByte((long)address, value); GetMemoryManager().Write(address, value);
} }
public static void WriteUInt16(ulong address, ushort value) public static void WriteUInt16(ulong address, ushort value)
{ {
GetMemoryManager().WriteUInt16((long)address, value); GetMemoryManager().Write(address, value);
} }
public static void WriteUInt32(ulong address, uint value) public static void WriteUInt32(ulong address, uint value)
{ {
GetMemoryManager().WriteUInt32((long)address, value); GetMemoryManager().Write(address, value);
} }
public static void WriteUInt64(ulong address, ulong value) public static void WriteUInt64(ulong address, ulong value)
{ {
GetMemoryManager().WriteUInt64((long)address, value); GetMemoryManager().Write(address, value);
} }
public static void WriteVector128(ulong address, V128 value) public static void WriteVector128(ulong address, V128 value)
{ {
GetMemoryManager().WriteVector128((long)address, value); GetMemoryManager().Write(address, value);
} }
#endregion #endregion
#region "Write exclusive" #region "Write exclusive"
public static int WriteByteExclusive(ulong address, byte value) public static int WriteByteExclusive(ulong address, byte value)
{ {
bool success = _context.ExclusiveAddress == GetMaskedExclusiveAddress(address); bool success = _context.ExclusiveAddress == GetMaskedExclusiveAddress(address);
if (success) if (success)
{ {
success = _context.Memory.AtomicCompareExchangeByte( ref int valueRef = ref _context.Memory.GetRefNoChecks<int>(address);
(long)address,
(byte)_context.ExclusiveValueLow, int currentValue = valueRef;
(byte)value);
byte expected = (byte)_context.ExclusiveValueLow;
int expected32 = (currentValue & ~byte.MaxValue) | expected;
int desired32 = (currentValue & ~byte.MaxValue) | value;
success = Interlocked.CompareExchange(ref valueRef, desired32, expected32) == expected32;
if (success) if (success)
{ {
@ -306,10 +314,16 @@ namespace ARMeilleure.Instructions
if (success) if (success)
{ {
success = _context.Memory.AtomicCompareExchangeInt16( ref int valueRef = ref _context.Memory.GetRefNoChecks<int>(address);
(long)address,
(short)_context.ExclusiveValueLow, int currentValue = valueRef;
(short)value);
ushort expected = (ushort)_context.ExclusiveValueLow;
int expected32 = (currentValue & ~ushort.MaxValue) | expected;
int desired32 = (currentValue & ~ushort.MaxValue) | value;
success = Interlocked.CompareExchange(ref valueRef, desired32, expected32) == expected32;
if (success) if (success)
{ {
@ -326,10 +340,9 @@ namespace ARMeilleure.Instructions
if (success) if (success)
{ {
success = _context.Memory.AtomicCompareExchangeInt32( ref int valueRef = ref _context.Memory.GetRef<int>(address);
(long)address,
(int)_context.ExclusiveValueLow, success = Interlocked.CompareExchange(ref valueRef, (int)value, (int)_context.ExclusiveValueLow) == (int)_context.ExclusiveValueLow;
(int)value);
if (success) if (success)
{ {
@ -346,10 +359,9 @@ namespace ARMeilleure.Instructions
if (success) if (success)
{ {
success = _context.Memory.AtomicCompareExchangeInt64( ref long valueRef = ref _context.Memory.GetRef<long>(address);
(long)address,
(long)_context.ExclusiveValueLow, success = Interlocked.CompareExchange(ref valueRef, (long)value, (long)_context.ExclusiveValueLow) == (long)_context.ExclusiveValueLow;
(long)value);
if (success) if (success)
{ {
@ -368,7 +380,9 @@ namespace ARMeilleure.Instructions
{ {
V128 expected = new V128(_context.ExclusiveValueLow, _context.ExclusiveValueHigh); V128 expected = new V128(_context.ExclusiveValueLow, _context.ExclusiveValueHigh);
success = _context.Memory.AtomicCompareExchangeInt128((long)address, expected, value); ref V128 location = ref _context.Memory.GetRef<V128>(address);
success = MemoryManagerPal.CompareAndSwap128(ref location, expected, value) == expected;
if (success) if (success)
{ {
@ -378,7 +392,7 @@ namespace ARMeilleure.Instructions
return success ? 0 : 1; return success ? 0 : 1;
} }
#endregion #endregion
private static ulong GetMaskedExclusiveAddress(ulong address) private static ulong GetMaskedExclusiveAddress(ulong address)
{ {
@ -412,7 +426,8 @@ namespace ARMeilleure.Instructions
{ {
Statistics.PauseTimer(); Statistics.PauseTimer();
ExecutionContext context = GetContext(); var context = GetContext();
context.CheckInterrupt(); context.CheckInterrupt();
Statistics.ResumeTimer(); Statistics.ResumeTimer();
@ -420,12 +435,12 @@ namespace ARMeilleure.Instructions
return context.Running; return context.Running;
} }
public static ExecutionContext GetContext() public static State.ExecutionContext GetContext()
{ {
return _context.Context; return _context.Context;
} }
public static MemoryManager GetMemoryManager() public static IMemoryManager GetMemoryManager()
{ {
return _context.Memory; return _context.Memory;
} }

View file

@ -0,0 +1,8 @@
namespace ARMeilleure.Memory
{
public interface IJitMemoryAllocator
{
IJitMemoryBlock Allocate(ulong size);
IJitMemoryBlock Reserve(ulong size);
}
}

View file

@ -0,0 +1,14 @@
using System;
namespace ARMeilleure.Memory
{
public interface IJitMemoryBlock : IDisposable
{
IntPtr Pointer { get; }
bool Commit(ulong offset, ulong size);
void MapAsRx(ulong offset, ulong size);
void MapAsRwx(ulong offset, ulong size);
}
}

View file

@ -0,0 +1,19 @@
using System;
namespace ARMeilleure.Memory
{
public interface IMemoryManager
{
int AddressSpaceBits { get; }
IntPtr PageTablePointer { get; }
T Read<T>(ulong va) where T : unmanaged;
void Write<T>(ulong va, T value) where T : unmanaged;
ref T GetRef<T>(ulong va) where T : unmanaged;
ref T GetRefNoChecks<T>(ulong va) where T : unmanaged;
bool IsMapped(ulong va);
}
}

View file

@ -1,94 +0,0 @@
using Mono.Unix.Native;
using System;
namespace ARMeilleure.Memory
{
static class MemoryManagementUnix
{
public static IntPtr Allocate(ulong size)
{
ulong pageSize = (ulong)Syscall.sysconf(SysconfName._SC_PAGESIZE);
const MmapProts prot = MmapProts.PROT_READ | MmapProts.PROT_WRITE;
const MmapFlags flags = MmapFlags.MAP_PRIVATE | MmapFlags.MAP_ANONYMOUS;
IntPtr ptr = Syscall.mmap(IntPtr.Zero, size + pageSize, prot, flags, -1, 0);
if (ptr == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
unsafe
{
ptr = new IntPtr(ptr.ToInt64() + (long)pageSize);
*((ulong*)ptr - 1) = size;
}
return ptr;
}
public static bool Commit(IntPtr address, ulong size)
{
return Syscall.mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) == 0;
}
public static bool Reprotect(IntPtr address, ulong size, Memory.MemoryProtection protection)
{
MmapProts prot = GetProtection(protection);
return Syscall.mprotect(address, size, prot) == 0;
}
public static IntPtr Reserve(ulong size)
{
ulong pageSize = (ulong)Syscall.sysconf(SysconfName._SC_PAGESIZE);
const MmapProts prot = MmapProts.PROT_NONE;
const MmapFlags flags = MmapFlags.MAP_PRIVATE | MmapFlags.MAP_ANONYMOUS;
IntPtr ptr = Syscall.mmap(IntPtr.Zero, size + pageSize, prot, flags, -1, 0);
if (ptr == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
return ptr;
}
private static MmapProts GetProtection(Memory.MemoryProtection protection)
{
switch (protection)
{
case Memory.MemoryProtection.None: return MmapProts.PROT_NONE;
case Memory.MemoryProtection.Read: return MmapProts.PROT_READ;
case Memory.MemoryProtection.ReadAndWrite: return MmapProts.PROT_READ | MmapProts.PROT_WRITE;
case Memory.MemoryProtection.ReadAndExecute: return MmapProts.PROT_READ | MmapProts.PROT_EXEC;
case Memory.MemoryProtection.ReadWriteExecute: return MmapProts.PROT_READ | MmapProts.PROT_WRITE | MmapProts.PROT_EXEC;
case Memory.MemoryProtection.Execute: return MmapProts.PROT_EXEC;
default: throw new ArgumentException($"Invalid permission \"{protection}\".");
}
}
public static bool Free(IntPtr address)
{
ulong pageSize = (ulong)Syscall.sysconf(SysconfName._SC_PAGESIZE);
ulong size;
unsafe
{
size = *((ulong*)address - 1);
address = new IntPtr(address.ToInt64() - (long)pageSize);
}
return Syscall.munmap(address, size + pageSize) == 0;
}
}
}

View file

@ -1,142 +0,0 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace ARMeilleure.Memory
{
static class MemoryManagementWindows
{
[Flags]
private enum AllocationType : uint
{
Commit = 0x1000,
Reserve = 0x2000,
Decommit = 0x4000,
Release = 0x8000,
Reset = 0x80000,
Physical = 0x400000,
TopDown = 0x100000,
WriteWatch = 0x200000,
LargePages = 0x20000000
}
[Flags]
private enum MemoryProtection : uint
{
NoAccess = 0x01,
ReadOnly = 0x02,
ReadWrite = 0x04,
WriteCopy = 0x08,
Execute = 0x10,
ExecuteRead = 0x20,
ExecuteReadWrite = 0x40,
ExecuteWriteCopy = 0x80,
GuardModifierflag = 0x100,
NoCacheModifierflag = 0x200,
WriteCombineModifierflag = 0x400
}
[DllImport("kernel32.dll")]
private static extern IntPtr VirtualAlloc(
IntPtr lpAddress,
IntPtr dwSize,
AllocationType flAllocationType,
MemoryProtection flProtect);
[DllImport("kernel32.dll")]
private static extern bool VirtualProtect(
IntPtr lpAddress,
IntPtr dwSize,
MemoryProtection flNewProtect,
out MemoryProtection lpflOldProtect);
[DllImport("kernel32.dll")]
private static extern bool VirtualFree(
IntPtr lpAddress,
IntPtr dwSize,
AllocationType dwFreeType);
public static IntPtr Allocate(IntPtr size)
{
const AllocationType flags =
AllocationType.Reserve |
AllocationType.Commit;
IntPtr ptr = VirtualAlloc(IntPtr.Zero, size, flags, MemoryProtection.ReadWrite);
if (ptr == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
return ptr;
}
public static IntPtr AllocateWriteTracked(IntPtr size)
{
const AllocationType flags =
AllocationType.Reserve |
AllocationType.Commit |
AllocationType.WriteWatch;
IntPtr ptr = VirtualAlloc(IntPtr.Zero, size, flags, MemoryProtection.ReadWrite);
if (ptr == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
return ptr;
}
public static bool Commit(IntPtr location, IntPtr size)
{
const AllocationType flags = AllocationType.Commit;
IntPtr ptr = VirtualAlloc(location, size, flags, MemoryProtection.ReadWrite);
return ptr != IntPtr.Zero;
}
public static bool Reprotect(IntPtr address, IntPtr size, Memory.MemoryProtection protection)
{
MemoryProtection prot = GetProtection(protection);
return VirtualProtect(address, size, prot, out _);
}
public static IntPtr Reserve(IntPtr size)
{
const AllocationType flags = AllocationType.Reserve;
IntPtr ptr = VirtualAlloc(IntPtr.Zero, size, flags, MemoryProtection.ReadWrite);
if (ptr == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
return ptr;
}
private static MemoryProtection GetProtection(Memory.MemoryProtection protection)
{
switch (protection)
{
case Memory.MemoryProtection.None: return MemoryProtection.NoAccess;
case Memory.MemoryProtection.Read: return MemoryProtection.ReadOnly;
case Memory.MemoryProtection.ReadAndWrite: return MemoryProtection.ReadWrite;
case Memory.MemoryProtection.ReadAndExecute: return MemoryProtection.ExecuteRead;
case Memory.MemoryProtection.ReadWriteExecute: return MemoryProtection.ExecuteReadWrite;
case Memory.MemoryProtection.Execute: return MemoryProtection.Execute;
default: throw new ArgumentException($"Invalid permission \"{protection}\".");
}
}
public static bool Free(IntPtr address)
{
return VirtualFree(address, IntPtr.Zero, AllocationType.Release);
}
}
}

View file

@ -1,738 +0,0 @@
using ARMeilleure.State;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using static ARMeilleure.Memory.MemoryManagement;
namespace ARMeilleure.Memory
{
public unsafe class MemoryManager
{
public const int PageBits = 12;
public const int PageSize = 1 << PageBits;
public const int PageMask = PageSize - 1;
internal const long PteFlagsMask = 7;
public IntPtr Ram { get; private set; }
private byte* _ramPtr;
private IntPtr _pageTable;
internal IntPtr PageTable => _pageTable;
internal int PtLevelBits { get; }
internal int PtLevelSize { get; }
internal int PtLevelMask { get; }
public int AddressSpaceBits { get; }
public long AddressSpaceSize { get; }
public MemoryManager(
IntPtr ram,
int addressSpaceBits = 48,
bool useFlatPageTable = false)
{
Ram = ram;
_ramPtr = (byte*)ram;
AddressSpaceBits = addressSpaceBits;
AddressSpaceSize = 1L << addressSpaceBits;
// When flat page table is requested, we use a single
// array for the mappings of the entire address space.
// This has better performance, but also high memory usage.
// The multi level page table uses 9 bits per level, so
// the memory usage is lower, but the performance is also
// lower, since each address translation requires multiple reads.
if (useFlatPageTable)
{
PtLevelBits = addressSpaceBits - PageBits;
}
else
{
PtLevelBits = 9;
}
PtLevelSize = 1 << PtLevelBits;
PtLevelMask = PtLevelSize - 1;
_pageTable = Allocate((ulong)(PtLevelSize * IntPtr.Size));
}
public void Map(long va, long pa, long size)
{
SetPtEntries(va, _ramPtr + pa, size);
}
public void Unmap(long position, long size)
{
SetPtEntries(position, null, size);
}
public bool IsMapped(long position)
{
return Translate(position) != IntPtr.Zero;
}
public long GetPhysicalAddress(long virtualAddress)
{
byte* ptr = (byte*)Translate(virtualAddress);
return (long)(ptr - _ramPtr);
}
private IntPtr Translate(long position)
{
if (!IsValidPosition(position))
{
return IntPtr.Zero;
}
byte* ptr = GetPtEntry(position);
ulong ptrUlong = (ulong)ptr;
if ((ptrUlong & PteFlagsMask) != 0)
{
ptrUlong &= ~(ulong)PteFlagsMask;
ptr = (byte*)ptrUlong;
}
if (ptr == null)
{
return IntPtr.Zero;
}
return new IntPtr(ptr + (position & PageMask));
}
private IntPtr TranslateWrite(long position)
{
if (!IsValidPosition(position))
{
return IntPtr.Zero;
}
byte* ptr = GetPtEntry(position);
ulong ptrUlong = (ulong)ptr;
if ((ptrUlong & PteFlagsMask) != 0)
{
ClearPtEntryFlag(position, PteFlagsMask);
ptrUlong &= ~(ulong)PteFlagsMask;
ptr = (byte*)ptrUlong;
}
return new IntPtr(ptr + (position & PageMask));
}
private byte* GetPtEntry(long position)
{
return *(byte**)GetPtPtr(position);
}
private void SetPtEntries(long va, byte* ptr, long size)
{
long endPosition = (va + size + PageMask) & ~PageMask;
while ((ulong)va < (ulong)endPosition)
{
SetPtEntry(va, ptr);
va += PageSize;
if (ptr != null)
{
ptr += PageSize;
}
}
}
private void SetPtEntry(long position, byte* ptr)
{
*(byte**)GetPtPtr(position) = ptr;
}
private void SetPtEntryFlag(long position, long flag)
{
ModifyPtEntryFlag(position, flag, setFlag: true);
}
private void ClearPtEntryFlag(long position, long flag)
{
ModifyPtEntryFlag(position, flag, setFlag: false);
}
private void ModifyPtEntryFlag(long position, long flag, bool setFlag)
{
IntPtr* pt = (IntPtr*)_pageTable;
while (true)
{
IntPtr* ptPtr = GetPtPtr(position);
IntPtr old = *ptPtr;
long modified = old.ToInt64();
if (setFlag)
{
modified |= flag;
}
else
{
modified &= ~flag;
}
IntPtr origValue = Interlocked.CompareExchange(ref *ptPtr, new IntPtr(modified), old);
if (origValue == old)
{
break;
}
}
}
private IntPtr* GetPtPtr(long position)
{
if (!IsValidPosition(position))
{
throw new ArgumentOutOfRangeException(nameof(position));
}
IntPtr nextPtr = _pageTable;
IntPtr* ptePtr = null;
int bit = PageBits;
while (true)
{
long index = (position >> bit) & PtLevelMask;
ptePtr = &((IntPtr*)nextPtr)[index];
bit += PtLevelBits;
if (bit >= AddressSpaceBits)
{
break;
}
nextPtr = *ptePtr;
if (nextPtr == IntPtr.Zero)
{
// Entry does not yet exist, allocate a new one.
IntPtr newPtr = Allocate((ulong)(PtLevelSize * IntPtr.Size));
// Try to swap the current pointer (should be zero), with the allocated one.
nextPtr = Interlocked.CompareExchange(ref *ptePtr, newPtr, IntPtr.Zero);
// If the old pointer is not null, then another thread already has set it.
if (nextPtr != IntPtr.Zero)
{
Free(newPtr);
}
else
{
nextPtr = newPtr;
}
}
}
return ptePtr;
}
public unsafe (ulong, ulong)[] GetModifiedRanges(ulong address, ulong size, int id)
{
ulong idMask = 1UL << id;
List<(ulong, ulong)> ranges = new List<(ulong, ulong)>();
ulong endAddress = (address + size + PageMask) & ~(ulong)PageMask;
address &= ~(ulong)PageMask;
ulong currAddr = address;
ulong currSize = 0;
while (address < endAddress)
{
// If the address is invalid, we stop and consider all the remaining memory
// as not modified (since the address is invalid, we can't check, and technically
// the memory doesn't exist).
if (!IsValidPosition((long)address))
{
break;
}
byte* ptr = ((byte**)_pageTable)[address >> PageBits];
ulong ptrUlong = (ulong)ptr;
if ((ptrUlong & idMask) == 0)
{
// Modified.
currSize += PageSize;
SetPtEntryFlag((long)address, (long)idMask);
}
else
{
if (currSize != 0)
{
ranges.Add((currAddr, currSize));
}
currAddr = address + PageSize;
currSize = 0;
}
address += PageSize;
}
if (currSize != 0)
{
ranges.Add((currAddr, currSize));
}
return ranges.ToArray();
}
private bool IsContiguous(long position, long size)
{
long endPos = position + size;
position &= ~PageMask;
long expectedPa = GetPhysicalAddress(position);
while ((ulong)position < (ulong)endPos)
{
long pa = GetPhysicalAddress(position);
if (pa != expectedPa)
{
return false;
}
position += PageSize;
expectedPa += PageSize;
}
return true;
}
public bool IsValidPosition(long position)
{
return (ulong)position < (ulong)AddressSpaceSize;
}
internal V128 AtomicLoadInt128(long position)
{
if ((position & 0xf) != 0)
{
AbortWithAlignmentFault(position);
}
IntPtr ptr = TranslateWrite(position);
return MemoryManagerPal.AtomicLoad128(ptr);
}
internal bool AtomicCompareExchangeByte(long position, byte expected, byte desired)
{
int* ptr = (int*)Translate(position);
int currentValue = *ptr;
int expected32 = (currentValue & ~byte.MaxValue) | expected;
int desired32 = (currentValue & ~byte.MaxValue) | desired;
return Interlocked.CompareExchange(ref *ptr, desired32, expected32) == expected32;
}
internal bool AtomicCompareExchangeInt16(long position, short expected, short desired)
{
if ((position & 1) != 0)
{
AbortWithAlignmentFault(position);
}
int* ptr = (int*)Translate(position);
int currentValue = *ptr;
int expected32 = (currentValue & ~ushort.MaxValue) | (ushort)expected;
int desired32 = (currentValue & ~ushort.MaxValue) | (ushort)desired;
return Interlocked.CompareExchange(ref *ptr, desired32, expected32) == expected32;
}
public bool AtomicCompareExchangeInt32(long position, int expected, int desired)
{
if ((position & 3) != 0)
{
AbortWithAlignmentFault(position);
}
int* ptr = (int*)TranslateWrite(position);
return Interlocked.CompareExchange(ref *ptr, desired, expected) == expected;
}
internal bool AtomicCompareExchangeInt64(long position, long expected, long desired)
{
if ((position & 7) != 0)
{
AbortWithAlignmentFault(position);
}
long* ptr = (long*)TranslateWrite(position);
return Interlocked.CompareExchange(ref *ptr, desired, expected) == expected;
}
internal bool AtomicCompareExchangeInt128(long position, V128 expected, V128 desired)
{
if ((position & 0xf) != 0)
{
AbortWithAlignmentFault(position);
}
IntPtr ptr = TranslateWrite(position);
return MemoryManagerPal.CompareAndSwap128(ptr, expected, desired) == expected;
}
public int AtomicIncrementInt32(long position)
{
if ((position & 3) != 0)
{
AbortWithAlignmentFault(position);
}
int* ptr = (int*)TranslateWrite(position);
return Interlocked.Increment(ref *ptr);
}
public int AtomicDecrementInt32(long position)
{
if ((position & 3) != 0)
{
AbortWithAlignmentFault(position);
}
int* ptr = (int*)TranslateWrite(position);
return Interlocked.Decrement(ref *ptr);
}
private void AbortWithAlignmentFault(long position)
{
// TODO: Abort mode and exception support on the CPU.
throw new InvalidOperationException($"Tried to compare exchange a misaligned address 0x{position:X16}.");
}
public sbyte ReadSByte(long position)
{
return (sbyte)ReadByte(position);
}
public short ReadInt16(long position)
{
return (short)ReadUInt16(position);
}
public int ReadInt32(long position)
{
return (int)ReadUInt32(position);
}
public long ReadInt64(long position)
{
return (long)ReadUInt64(position);
}
public byte ReadByte(long position)
{
return *((byte*)Translate(position));
}
public ushort ReadUInt16(long position)
{
if ((position & 1) == 0)
{
return *((ushort*)Translate(position));
}
else
{
return (ushort)(ReadByte(position + 0) << 0 |
ReadByte(position + 1) << 8);
}
}
public uint ReadUInt32(long position)
{
if ((position & 3) == 0)
{
return *((uint*)Translate(position));
}
else
{
return (uint)(ReadUInt16(position + 0) << 0 |
ReadUInt16(position + 2) << 16);
}
}
public ulong ReadUInt64(long position)
{
if ((position & 7) == 0)
{
return *((ulong*)Translate(position));
}
else
{
return (ulong)ReadUInt32(position + 0) << 0 |
(ulong)ReadUInt32(position + 4) << 32;
}
}
public V128 ReadVector128(long position)
{
return new V128(ReadUInt64(position), ReadUInt64(position + 8));
}
public byte[] ReadBytes(long position, long size)
{
long endAddr = position + size;
if ((ulong)size > int.MaxValue)
{
throw new ArgumentOutOfRangeException(nameof(size));
}
if ((ulong)endAddr < (ulong)position)
{
throw new ArgumentOutOfRangeException(nameof(position));
}
byte[] data = new byte[size];
int offset = 0;
while ((ulong)position < (ulong)endAddr)
{
long pageLimit = (position + PageSize) & ~(long)PageMask;
if ((ulong)pageLimit > (ulong)endAddr)
{
pageLimit = endAddr;
}
int copySize = (int)(pageLimit - position);
Marshal.Copy(Translate(position), data, offset, copySize);
position += copySize;
offset += copySize;
}
return data;
}
public ReadOnlySpan<byte> GetSpan(ulong address, ulong size)
{
if (IsContiguous(address, size))
{
return new ReadOnlySpan<byte>((void*)Translate((long)address), (int)size);
}
else
{
return ReadBytes((long)address, (long)size);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool IsContiguous(ulong address, ulong size)
{
if (!IsValidPosition((long)address))
{
return false;
}
ulong endVa = (address + size + PageMask) & ~(ulong)PageMask;
address &= ~(ulong)PageMask;
int pages = (int)((endVa - address) / PageSize);
for (int page = 0; page < pages - 1; page++)
{
if (!IsValidPosition((long)address + PageSize))
{
return false;
}
if (GetPtEntry((long)address) + PageSize != GetPtEntry((long)address + PageSize))
{
return false;
}
address += PageSize;
}
return true;
}
public void WriteSByte(long position, sbyte value)
{
WriteByte(position, (byte)value);
}
public void WriteInt16(long position, short value)
{
WriteUInt16(position, (ushort)value);
}
public void WriteInt32(long position, int value)
{
WriteUInt32(position, (uint)value);
}
public void WriteInt64(long position, long value)
{
WriteUInt64(position, (ulong)value);
}
public void WriteByte(long position, byte value)
{
*((byte*)TranslateWrite(position)) = value;
}
public void WriteUInt16(long position, ushort value)
{
if ((position & 1) == 0)
{
*((ushort*)TranslateWrite(position)) = value;
}
else
{
WriteByte(position + 0, (byte)(value >> 0));
WriteByte(position + 1, (byte)(value >> 8));
}
}
public void WriteUInt32(long position, uint value)
{
if ((position & 3) == 0)
{
*((uint*)TranslateWrite(position)) = value;
}
else
{
WriteUInt16(position + 0, (ushort)(value >> 0));
WriteUInt16(position + 2, (ushort)(value >> 16));
}
}
public void WriteUInt64(long position, ulong value)
{
if ((position & 7) == 0)
{
*((ulong*)TranslateWrite(position)) = value;
}
else
{
WriteUInt32(position + 0, (uint)(value >> 0));
WriteUInt32(position + 4, (uint)(value >> 32));
}
}
public void WriteVector128(long position, V128 value)
{
WriteUInt64(position + 0, value.Extract<ulong>(0));
WriteUInt64(position + 8, value.Extract<ulong>(1));
}
public void WriteBytes(long position, byte[] data)
{
long endAddr = position + data.Length;
if ((ulong)endAddr < (ulong)position)
{
throw new ArgumentOutOfRangeException(nameof(position));
}
int offset = 0;
while ((ulong)position < (ulong)endAddr)
{
long pageLimit = (position + PageSize) & ~(long)PageMask;
if ((ulong)pageLimit > (ulong)endAddr)
{
pageLimit = endAddr;
}
int copySize = (int)(pageLimit - position);
Marshal.Copy(data, offset, TranslateWrite(position), copySize);
position += copySize;
offset += copySize;
}
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
IntPtr ptr = Interlocked.Exchange(ref _pageTable, IntPtr.Zero);
if (ptr != IntPtr.Zero)
{
FreePageTableEntry(ptr, PageBits);
}
}
private void FreePageTableEntry(IntPtr ptr, int levelBitEnd)
{
levelBitEnd += PtLevelBits;
if (levelBitEnd >= AddressSpaceBits)
{
Free(ptr);
return;
}
for (int index = 0; index < PtLevelSize; index++)
{
IntPtr ptePtr = ((IntPtr*)ptr)[index];
if (ptePtr != IntPtr.Zero)
{
FreePageTableEntry(ptePtr, levelBitEnd);
}
}
Free(ptr);
}
}
}

View file

@ -1,13 +1,12 @@
using ARMeilleure.IntermediateRepresentation; using ARMeilleure.IntermediateRepresentation;
using ARMeilleure.State; using ARMeilleure.State;
using ARMeilleure.Translation; using ARMeilleure.Translation;
using System;
namespace ARMeilleure.Memory namespace ARMeilleure.Memory
{ {
static class MemoryManagerPal static class MemoryManagerPal
{ {
private delegate V128 CompareExchange128(IntPtr address, V128 expected, V128 desired); private delegate V128 CompareExchange128(ref V128 location, V128 expected, V128 desired);
private static CompareExchange128 _compareExchange128; private static CompareExchange128 _compareExchange128;
@ -18,14 +17,14 @@ namespace ARMeilleure.Memory
_lock = new object(); _lock = new object();
} }
public static V128 AtomicLoad128(IntPtr address) public static V128 AtomicLoad128(ref V128 location)
{ {
return GetCompareAndSwap128()(address, V128.Zero, V128.Zero); return GetCompareAndSwap128()(ref location, V128.Zero, V128.Zero);
} }
public static V128 CompareAndSwap128(IntPtr address, V128 expected, V128 desired) public static V128 CompareAndSwap128(ref V128 location, V128 expected, V128 desired)
{ {
return GetCompareAndSwap128()(address, expected, desired); return GetCompareAndSwap128()(ref location, expected, desired);
} }
private static CompareExchange128 GetCompareAndSwap128() private static CompareExchange128 GetCompareAndSwap128()

View file

@ -1,17 +0,0 @@
using System;
namespace ARMeilleure.Memory
{
[Flags]
public enum MemoryProtection
{
None = 0,
Read = 1 << 0,
Write = 1 << 1,
Execute = 1 << 2,
ReadAndWrite = Read | Write,
ReadAndExecute = Read | Execute,
ReadWriteExecute = Read | Write | Execute
}
}

View file

@ -1,9 +0,0 @@
using System;
namespace ARMeilleure.Memory
{
class MemoryProtectionException : Exception
{
public MemoryProtectionException(MemoryProtection protection) : base($"Failed to set memory protection to \"{protection}\".") { }
}
}

View file

@ -1,6 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Text;
namespace ARMeilleure.Memory namespace ARMeilleure.Memory
{ {
@ -8,20 +6,22 @@ namespace ARMeilleure.Memory
{ {
private const int DefaultGranularity = 65536; // Mapping granularity in Windows. private const int DefaultGranularity = 65536; // Mapping granularity in Windows.
public IntPtr Pointer { get; } public IJitMemoryBlock Block { get; }
private ulong _maxSize; public IntPtr Pointer => Block.Pointer;
private ulong _sizeGranularity;
private readonly ulong _maxSize;
private readonly ulong _sizeGranularity;
private ulong _currentSize; private ulong _currentSize;
public ReservedRegion(ulong maxSize, ulong granularity = 0) public ReservedRegion(IJitMemoryAllocator allocator, ulong maxSize, ulong granularity = 0)
{ {
if (granularity == 0) if (granularity == 0)
{ {
granularity = DefaultGranularity; granularity = DefaultGranularity;
} }
Pointer = MemoryManagement.Reserve(maxSize); Block = allocator.Reserve(maxSize);
_maxSize = maxSize; _maxSize = maxSize;
_sizeGranularity = granularity; _sizeGranularity = granularity;
_currentSize = 0; _currentSize = 0;
@ -43,7 +43,7 @@ namespace ARMeilleure.Memory
{ {
ulong overflowBytes = desiredSize - _currentSize; ulong overflowBytes = desiredSize - _currentSize;
ulong moreToCommit = (((_sizeGranularity - 1) + overflowBytes) / _sizeGranularity) * _sizeGranularity; // Round up. ulong moreToCommit = (((_sizeGranularity - 1) + overflowBytes) / _sizeGranularity) * _sizeGranularity; // Round up.
MemoryManagement.Commit(new IntPtr((long)Pointer + (long)_currentSize), moreToCommit); Block.Commit(_currentSize, moreToCommit);
_currentSize += moreToCommit; _currentSize += moreToCommit;
} }
} }

View file

@ -1,3 +1,4 @@
using ARMeilleure.Memory;
using System; using System;
using System.Diagnostics; using System.Diagnostics;
@ -73,9 +74,9 @@ namespace ARMeilleure.State
_tickCounter.Start(); _tickCounter.Start();
} }
public ExecutionContext() public ExecutionContext(IJitMemoryAllocator allocator)
{ {
_nativeContext = new NativeContext(); _nativeContext = new NativeContext(allocator);
Running = true; Running = true;

View file

@ -17,11 +17,13 @@ namespace ARMeilleure.State
RegisterConsts.FlagsCount * FlagSize + RegisterConsts.FlagsCount * FlagSize +
RegisterConsts.FpFlagsCount * FlagSize + ExtraSize; RegisterConsts.FpFlagsCount * FlagSize + ExtraSize;
public IntPtr BasePtr { get; } private readonly IJitMemoryBlock _block;
public NativeContext() public IntPtr BasePtr => _block.Pointer;
public NativeContext(IJitMemoryAllocator allocator)
{ {
BasePtr = MemoryManagement.Allocate(TotalSize); _block = allocator.Allocate(TotalSize);
} }
public ulong GetX(int index) public ulong GetX(int index)
@ -193,7 +195,7 @@ namespace ARMeilleure.State
public void Dispose() public void Dispose()
{ {
MemoryManagement.Free(BasePtr); _block.Dispose();
} }
} }
} }

View file

@ -37,7 +37,7 @@ namespace ARMeilleure.Translation
public OpCode CurrOp { get; set; } public OpCode CurrOp { get; set; }
public MemoryManager Memory { get; } public IMemoryManager Memory { get; }
public Aarch32Mode Mode { get; } public Aarch32Mode Mode { get; }
@ -47,7 +47,7 @@ namespace ARMeilleure.Translation
public bool HighCq { get; } public bool HighCq { get; }
public ArmEmitterContext(MemoryManager memory, JumpTable jumpTable, long baseAddress, bool highCq, Aarch32Mode mode) public ArmEmitterContext(IMemoryManager memory, JumpTable jumpTable, long baseAddress, bool highCq, Aarch32Mode mode)
{ {
Memory = memory; Memory = memory;
JumpTable = jumpTable; JumpTable = jumpTable;

View file

@ -11,14 +11,14 @@ namespace ARMeilleure.Translation
{ {
public static T Compile<T>(ControlFlowGraph cfg, OperandType[] argTypes, OperandType retType, CompilerOptions options) public static T Compile<T>(ControlFlowGraph cfg, OperandType[] argTypes, OperandType retType, CompilerOptions options)
{ {
CompiledFunction func = CompileAndGetCf(cfg, argTypes, retType, options); CompiledFunction func = Compile(cfg, argTypes, retType, options);
IntPtr codePtr = JitCache.Map(func); IntPtr codePtr = JitCache.Map(func);
return Marshal.GetDelegateForFunctionPointer<T>(codePtr); return Marshal.GetDelegateForFunctionPointer<T>(codePtr);
} }
public static CompiledFunction CompileAndGetCf(ControlFlowGraph cfg, OperandType[] argTypes, OperandType retType, CompilerOptions options) public static CompiledFunction Compile(ControlFlowGraph cfg, OperandType[] argTypes, OperandType retType, CompilerOptions options)
{ {
Logger.StartPass(PassName.Dominance); Logger.StartPass(PassName.Dominance);

View file

@ -17,14 +17,9 @@ namespace ARMeilleure.Translation
private static GuestFunction _indirectCallStub; private static GuestFunction _indirectCallStub;
private static GuestFunction _indirectTailCallStub; private static GuestFunction _indirectTailCallStub;
private static object _lock; private static readonly object _lock = new object();
private static bool _initialized; private static bool _initialized;
static DirectCallStubs()
{
_lock = new object();
}
public static void InitializeStubs() public static void InitializeStubs()
{ {
if (_initialized) return; if (_initialized) return;
@ -85,11 +80,7 @@ namespace ARMeilleure.Translation
OperandType.I64 OperandType.I64
}; };
return Compiler.Compile<GuestFunction>( return Compiler.Compile<GuestFunction>(cfg, argTypes, OperandType.I64, CompilerOptions.HighCq);
cfg,
argTypes,
OperandType.I64,
CompilerOptions.HighCq);
} }
/// <summary> /// <summary>
@ -121,11 +112,7 @@ namespace ARMeilleure.Translation
OperandType.I64 OperandType.I64
}; };
return Compiler.Compile<GuestFunction>( return Compiler.Compile<GuestFunction>(cfg, argTypes, OperandType.I64, CompilerOptions.HighCq);
cfg,
argTypes,
OperandType.I64,
CompilerOptions.HighCq);
} }
} }
} }

View file

@ -2,6 +2,7 @@ using ARMeilleure.CodeGen;
using ARMeilleure.Memory; using ARMeilleure.Memory;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace ARMeilleure.Translation namespace ARMeilleure.Translation
@ -12,35 +13,33 @@ namespace ARMeilleure.Translation
private const int PageMask = PageSize - 1; private const int PageMask = PageSize - 1;
private const int CodeAlignment = 4; // Bytes private const int CodeAlignment = 4; // Bytes
private const int CacheSize = 2047 * 1024 * 1024; private const int CacheSize = 2047 * 1024 * 1024;
private static ReservedRegion _jitRegion; private static ReservedRegion _jitRegion;
private static IntPtr _basePointer => _jitRegion.Pointer;
private static int _offset; private static int _offset;
private static readonly List<JitCacheEntry> _cacheEntries = new List<JitCacheEntry>();
private static List<JitCacheEntry> _cacheEntries; private static readonly object _lock = new object();
private static bool _initialized;
private static object _lock; public static void Initialize(IJitMemoryAllocator allocator)
static JitCache()
{ {
_jitRegion = new ReservedRegion(CacheSize); if (_initialized) return;
lock (_lock)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{ {
_jitRegion.ExpandIfNeeded(PageSize); if (_initialized) return;
JitUnwindWindows.InstallFunctionTableHandler(_basePointer, CacheSize); _jitRegion = new ReservedRegion(allocator, CacheSize);
// The first page is used for the table based SEH structs. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
_offset = PageSize; {
_jitRegion.ExpandIfNeeded(PageSize);
JitUnwindWindows.InstallFunctionTableHandler(_jitRegion.Pointer, CacheSize);
// The first page is used for the table based SEH structs.
_offset = PageSize;
}
_initialized = true;
} }
_cacheEntries = new List<JitCacheEntry>();
_lock = new object();
} }
public static IntPtr Map(CompiledFunction func) public static IntPtr Map(CompiledFunction func)
@ -49,9 +48,11 @@ namespace ARMeilleure.Translation
lock (_lock) lock (_lock)
{ {
Debug.Assert(_initialized);
int funcOffset = Allocate(code.Length); int funcOffset = Allocate(code.Length);
IntPtr funcPtr = _basePointer + funcOffset; IntPtr funcPtr = _jitRegion.Pointer + funcOffset;
Marshal.Copy(code, 0, funcPtr, code.Length); Marshal.Copy(code, 0, funcPtr, code.Length);
@ -77,18 +78,14 @@ namespace ARMeilleure.Translation
if (fullPagesSize != 0) if (fullPagesSize != 0)
{ {
IntPtr funcPtr = _basePointer + pageStart; _jitRegion.Block.MapAsRx((ulong)pageStart, (ulong)fullPagesSize);
MemoryManagement.Reprotect(funcPtr, (ulong)fullPagesSize, MemoryProtection.ReadAndExecute);
} }
int remaining = endOffs - pageEnd; int remaining = endOffs - pageEnd;
if (remaining != 0) if (remaining != 0)
{ {
IntPtr funcPtr = _basePointer + pageEnd; _jitRegion.Block.MapAsRwx((ulong)pageEnd, (ulong)remaining);
MemoryManagement.Reprotect(funcPtr, (ulong)remaining, MemoryProtection.ReadWriteExecute);
} }
} }
@ -132,7 +129,7 @@ namespace ARMeilleure.Translation
} }
} }
entry = default(JitCacheEntry); entry = default;
return false; return false;
} }

View file

@ -9,13 +9,6 @@ namespace ARMeilleure.Translation
{ {
class JumpTable class JumpTable
{ {
public static JumpTable Instance { get; }
static JumpTable()
{
Instance = new JumpTable();
}
// The jump table is a block of (guestAddress, hostAddress) function mappings. // The jump table is a block of (guestAddress, hostAddress) function mappings.
// Each entry corresponds to one branch in a JIT compiled function. The entries are // Each entry corresponds to one branch in a JIT compiled function. The entries are
// reserved specifically for each call. // reserved specifically for each call.
@ -60,23 +53,23 @@ namespace ARMeilleure.Translation
public IntPtr JumpPointer => _jumpRegion.Pointer; public IntPtr JumpPointer => _jumpRegion.Pointer;
public IntPtr DynamicPointer => _dynamicRegion.Pointer; public IntPtr DynamicPointer => _dynamicRegion.Pointer;
public JumpTable() public JumpTable(IJitMemoryAllocator allocator)
{ {
_jumpRegion = new ReservedRegion(JumpTableByteSize); _jumpRegion = new ReservedRegion(allocator, JumpTableByteSize);
_dynamicRegion = new ReservedRegion(DynamicTableByteSize); _dynamicRegion = new ReservedRegion(allocator, DynamicTableByteSize);
_targets = new ConcurrentDictionary<ulong, TranslatedFunction>(); _targets = new ConcurrentDictionary<ulong, TranslatedFunction>();
_dependants = new ConcurrentDictionary<ulong, LinkedList<int>>(); _dependants = new ConcurrentDictionary<ulong, LinkedList<int>>();
} }
public void RegisterFunction(ulong address, TranslatedFunction func) { public void RegisterFunction(ulong address, TranslatedFunction func)
{
address &= ~3UL; address &= ~3UL;
_targets.AddOrUpdate(address, func, (key, oldFunc) => func); _targets.AddOrUpdate(address, func, (key, oldFunc) => func);
long funcPtr = func.GetPointer().ToInt64(); long funcPtr = func.GetPointer().ToInt64();
// Update all jump table entries that target this address. // Update all jump table entries that target this address.
LinkedList<int> myDependants; if (_dependants.TryGetValue(address, out LinkedList<int> myDependants))
if (_dependants.TryGetValue(address, out myDependants))
{ {
lock (myDependants) lock (myDependants)
{ {
@ -125,8 +118,7 @@ namespace ARMeilleure.Translation
// Is the address we have already registered? If so, put the function address in the jump table. // Is the address we have already registered? If so, put the function address in the jump table.
// If not, it will point to the direct call stub. // If not, it will point to the direct call stub.
long value = (long)DirectCallStubs.DirectCallStub(isJump); long value = (long)DirectCallStubs.DirectCallStub(isJump);
TranslatedFunction func; if (_targets.TryGetValue((ulong)address, out TranslatedFunction func))
if (_targets.TryGetValue((ulong)address, out func))
{ {
value = func.GetPointer().ToInt64(); value = func.GetPointer().ToInt64();
} }

View file

@ -9,6 +9,7 @@ using System.Collections.Concurrent;
using System.Threading; using System.Threading;
using static ARMeilleure.IntermediateRepresentation.OperandHelper; using static ARMeilleure.IntermediateRepresentation.OperandHelper;
using static ARMeilleure.IntermediateRepresentation.OperationHelper;
namespace ARMeilleure.Translation namespace ARMeilleure.Translation
{ {
@ -18,30 +19,31 @@ namespace ARMeilleure.Translation
private const bool AlwaysTranslateFunctions = true; // If false, only translates a single block for lowCq. private const bool AlwaysTranslateFunctions = true; // If false, only translates a single block for lowCq.
private MemoryManager _memory; private readonly IMemoryManager _memory;
private ConcurrentDictionary<ulong, TranslatedFunction> _funcs; private readonly ConcurrentDictionary<ulong, TranslatedFunction> _funcs;
private JumpTable _jumpTable; private readonly JumpTable _jumpTable;
private PriorityQueue<RejitRequest> _backgroundQueue; private readonly PriorityQueue<RejitRequest> _backgroundQueue;
private AutoResetEvent _backgroundTranslatorEvent; private readonly AutoResetEvent _backgroundTranslatorEvent;
private volatile int _threadCount; private volatile int _threadCount;
public Translator(MemoryManager memory) public Translator(IJitMemoryAllocator allocator, IMemoryManager memory)
{ {
_memory = memory; _memory = memory;
_funcs = new ConcurrentDictionary<ulong, TranslatedFunction>(); _funcs = new ConcurrentDictionary<ulong, TranslatedFunction>();
_jumpTable = JumpTable.Instance; _jumpTable = new JumpTable(allocator);
_backgroundQueue = new PriorityQueue<RejitRequest>(2); _backgroundQueue = new PriorityQueue<RejitRequest>(2);
_backgroundTranslatorEvent = new AutoResetEvent(false); _backgroundTranslatorEvent = new AutoResetEvent(false);
JitCache.Initialize(allocator);
DirectCallStubs.InitializeStubs(); DirectCallStubs.InitializeStubs();
} }
@ -146,8 +148,8 @@ namespace ARMeilleure.Translation
{ {
ArmEmitterContext context = new ArmEmitterContext(_memory, _jumpTable, (long)address, highCq, Aarch32Mode.User); ArmEmitterContext context = new ArmEmitterContext(_memory, _jumpTable, (long)address, highCq, Aarch32Mode.User);
OperandHelper.PrepareOperandPool(highCq); PrepareOperandPool(highCq);
OperationHelper.PrepareOperationPool(highCq); PrepareOperationPool(highCq);
Logger.StartPass(PassName.Decoding); Logger.StartPass(PassName.Decoding);
@ -178,14 +180,12 @@ namespace ARMeilleure.Translation
OperandType[] argTypes = new OperandType[] { OperandType.I64 }; OperandType[] argTypes = new OperandType[] { OperandType.I64 };
CompilerOptions options = highCq CompilerOptions options = highCq ? CompilerOptions.HighCq : CompilerOptions.None;
? CompilerOptions.HighCq
: CompilerOptions.None;
GuestFunction func = Compiler.Compile<GuestFunction>(cfg, argTypes, OperandType.I64, options); GuestFunction func = Compiler.Compile<GuestFunction>(cfg, argTypes, OperandType.I64, options);
OperandHelper.ResetOperandPool(highCq); ResetOperandPool(highCq);
OperationHelper.ResetOperationPool(highCq); ResetOperationPool(highCq);
return new TranslatedFunction(func, rejit: !highCq); return new TranslatedFunction(func, rejit: !highCq);
} }

19
Ryujinx.Cpu/CpuContext.cs Normal file
View file

@ -0,0 +1,19 @@
using ARMeilleure.State;
using ARMeilleure.Translation;
namespace Ryujinx.Cpu
{
public class CpuContext
{
private readonly Translator _translator;
public CpuContext(MemoryManager memory)
{
_translator = new Translator(new JitMemoryAllocator(), memory);
}
public static ExecutionContext CreateExecutionContext() => new ExecutionContext(new JitMemoryAllocator());
public void Execute(ExecutionContext context, ulong address) => _translator.Execute(context, address);
}
}

View file

@ -0,0 +1,11 @@
using ARMeilleure.Memory;
using Ryujinx.Memory;
namespace Ryujinx.Cpu
{
class JitMemoryAllocator : IJitMemoryAllocator
{
public IJitMemoryBlock Allocate(ulong size) => new JitMemoryBlock(size, MemoryAllocationFlags.None);
public IJitMemoryBlock Reserve(ulong size) => new JitMemoryBlock(size, MemoryAllocationFlags.Reserve);
}
}

View file

@ -0,0 +1,24 @@
using ARMeilleure.Memory;
using Ryujinx.Memory;
using System;
namespace Ryujinx.Cpu
{
class JitMemoryBlock : IJitMemoryBlock
{
private readonly MemoryBlock _impl;
public IntPtr Pointer => _impl.Pointer;
public JitMemoryBlock(ulong size, MemoryAllocationFlags flags)
{
_impl = new MemoryBlock(size, flags);
}
public bool Commit(ulong offset, ulong size) => _impl.Commit(offset, size);
public void MapAsRx(ulong offset, ulong size) => _impl.Reprotect(offset, size, MemoryPermission.ReadAndExecute);
public void MapAsRwx(ulong offset, ulong size) => _impl.Reprotect(offset, size, MemoryPermission.ReadWriteExecute);
public void Dispose() => _impl.Dispose();
}
}

View file

@ -1,9 +1,9 @@
using System; using System;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
namespace ARMeilleure.Memory namespace Ryujinx.Cpu
{ {
public static class MemoryHelper public static class MemoryHelper
{ {
@ -13,12 +13,12 @@ namespace ARMeilleure.Memory
for (int offs = 0; offs < size8; offs += 8) for (int offs = 0; offs < size8; offs += 8)
{ {
memory.WriteInt64(position + offs, 0); memory.Write<long>((ulong)(position + offs), 0);
} }
for (int offs = size8; offs < (size - size8); offs++) for (int offs = size8; offs < (size - size8); offs++)
{ {
memory.WriteByte(position + offs, 0); memory.Write<byte>((ulong)(position + offs), 0);
} }
} }
@ -26,7 +26,9 @@ namespace ARMeilleure.Memory
{ {
long size = Marshal.SizeOf<T>(); long size = Marshal.SizeOf<T>();
byte[] data = memory.ReadBytes(position, size); byte[] data = new byte[size];
memory.Read((ulong)position, data);
fixed (byte* ptr = data) fixed (byte* ptr = data)
{ {
@ -45,7 +47,7 @@ namespace ARMeilleure.Memory
Marshal.StructureToPtr<T>(value, (IntPtr)ptr, false); Marshal.StructureToPtr<T>(value, (IntPtr)ptr, false);
} }
memory.WriteBytes(position, data); memory.Write((ulong)position, data);
} }
public static string ReadAsciiString(MemoryManager memory, long position, long maxSize = -1) public static string ReadAsciiString(MemoryManager memory, long position, long maxSize = -1)
@ -54,7 +56,7 @@ namespace ARMeilleure.Memory
{ {
for (long offs = 0; offs < maxSize || maxSize == -1; offs++) for (long offs = 0; offs < maxSize || maxSize == -1; offs++)
{ {
byte value = (byte)memory.ReadByte(position + offs); byte value = memory.Read<byte>((ulong)(position + offs));
if (value == 0) if (value == 0)
{ {

View file

@ -0,0 +1,466 @@
using ARMeilleure.Memory;
using Ryujinx.Memory;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
namespace Ryujinx.Cpu
{
/// <summary>
/// Represents a CPU memory manager.
/// </summary>
public sealed class MemoryManager : IMemoryManager, IDisposable
{
public const int PageBits = 12;
public const int PageSize = 1 << PageBits;
public const int PageMask = PageSize - 1;
private const int PteSize = 8;
public int AddressSpaceBits { get; }
private readonly ulong _addressSpaceSize;
private readonly MemoryBlock _backingMemory;
private readonly MemoryBlock _pageTable;
public IntPtr PageTablePointer => _pageTable.Pointer;
/// <summary>
/// Creates a new instance of the memory manager.
/// </summary>
/// <param name="backingMemory">Physical backing memory where virtual memory will be mapped to</param>
/// <param name="addressSpaceSize">Size of the address space</param>
public MemoryManager(MemoryBlock backingMemory, ulong addressSpaceSize)
{
ulong asSize = PageSize;
int asBits = PageBits;
while (asSize < addressSpaceSize)
{
asSize <<= 1;
asBits++;
}
AddressSpaceBits = asBits;
_addressSpaceSize = asSize;
_backingMemory = backingMemory;
_pageTable = new MemoryBlock((asSize / PageSize) * PteSize);
}
/// <summary>
/// Maps a virtual memory range into a physical memory range.
/// </summary>
/// <remarks>
/// Addresses and size must be page aligned.
/// </remarks>
/// <param name="va">Virtual memory address</param>
/// <param name="pa">Physical memory address</param>
/// <param name="size">Size to be mapped</param>
public void Map(ulong va, ulong pa, ulong size)
{
while (size != 0)
{
_pageTable.Write((va / PageSize) * PteSize, PaToPte(pa));
va += PageSize;
pa += PageSize;
size -= PageSize;
}
}
/// <summary>
/// Unmaps a previously mapped range of virtual memory.
/// </summary>
/// <param name="va">Virtual address of the range to be unmapped</param>
/// <param name="size">Size of the range to be unmapped</param>
public void Unmap(ulong va, ulong size)
{
while (size != 0)
{
_pageTable.Write((va / PageSize) * PteSize, 0UL);
va += PageSize;
size -= PageSize;
}
}
/// <summary>
/// Reads data from CPU mapped memory.
/// </summary>
/// <typeparam name="T">Type of the data being read</typeparam>
/// <param name="va">Virtual address of the data in memory</param>
/// <returns>The data</returns>
public T Read<T>(ulong va) where T : unmanaged
{
return MemoryMarshal.Cast<byte, T>(GetSpan(va, Unsafe.SizeOf<T>()))[0];
}
/// <summary>
/// Reads data from CPU mapped memory.
/// </summary>
/// <param name="va">Virtual address of the data in memory</param>
/// <param name="data">Span to store the data being read into</param>
public void Read(ulong va, Span<byte> data)
{
ReadImpl(va, data);
}
/// <summary>
/// Writes data to CPU mapped memory.
/// </summary>
/// <typeparam name="T">Type of the data being written</typeparam>
/// <param name="va">Virtual address to write the data into</param>
/// <param name="value">Data to be written</param>
public void Write<T>(ulong va, T value) where T : unmanaged
{
Write(va, MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateSpan(ref value, 1)));
}
/// <summary>
/// Writes data to CPU mapped memory.
/// </summary>
/// <param name="va">Virtual address to write the data into</param>
/// <param name="data">Data to be written</param>
public void Write(ulong va, ReadOnlySpan<byte> data)
{
if (data.Length == 0)
{
return;
}
MarkRegionAsModified(va, (ulong)data.Length);
if (IsContiguous(va, data.Length))
{
data.CopyTo(_backingMemory.GetSpan(GetPhysicalAddressInternal(va), data.Length));
}
else
{
int offset = 0, size;
if ((va & PageMask) != 0)
{
ulong pa = GetPhysicalAddressInternal(va);
size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
data.Slice(0, size).CopyTo(_backingMemory.GetSpan(pa, size));
offset += size;
}
for (; offset < data.Length; offset += size)
{
ulong pa = GetPhysicalAddressInternal(va + (ulong)offset);
size = Math.Min(data.Length - offset, PageSize);
data.Slice(offset, size).CopyTo(_backingMemory.GetSpan(pa, size));
}
}
}
/// <summary>
/// Gets a read-only span of data from CPU mapped memory.
/// </summary>
/// <remarks>
/// This may perform a allocation if the data is not contiguous in memory.
/// For this reason, the span is read-only, you can't modify the data.
/// </remarks>
/// <param name="va">Virtual address of the data</param>
/// <param name="size">Size of the data</param>
/// <returns>A read-only span of the data</returns>
public ReadOnlySpan<byte> GetSpan(ulong va, int size)
{
if (size == 0)
{
return ReadOnlySpan<byte>.Empty;
}
if (IsContiguous(va, size))
{
return _backingMemory.GetSpan(GetPhysicalAddressInternal(va), size);
}
else
{
Span<byte> data = new byte[size];
ReadImpl(va, data);
return data;
}
}
/// <summary>
/// Gets a reference for the given type at the specified virtual memory address.
/// </summary>
/// <remarks>
/// The data must be located at a contiguous memory region.
/// </remarks>
/// <typeparam name="T">Type of the data to get the reference</typeparam>
/// <param name="va">Virtual address of the data</param>
/// <returns>A reference to the data in memory</returns>
public ref T GetRef<T>(ulong va) where T : unmanaged
{
if (!IsContiguous(va, Unsafe.SizeOf<T>()))
{
ThrowMemoryNotContiguous();
}
MarkRegionAsModified(va, (ulong)Unsafe.SizeOf<T>());
return ref _backingMemory.GetRef<T>(GetPhysicalAddressInternal(va));
}
private void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException();
// TODO: Remove that once we have proper 8-bits and 16-bits CAS.
public ref T GetRefNoChecks<T>(ulong va) where T : unmanaged
{
MarkRegionAsModified(va, (ulong)Unsafe.SizeOf<T>());
return ref _backingMemory.GetRef<T>(GetPhysicalAddressInternal(va));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool IsContiguous(ulong va, int size)
{
if (!ValidateAddress(va))
{
return false;
}
ulong endVa = (va + (ulong)size + PageMask) & ~(ulong)PageMask;
va &= ~(ulong)PageMask;
int pages = (int)((endVa - va) / PageSize);
for (int page = 0; page < pages - 1; page++)
{
if (!ValidateAddress(va + PageSize))
{
return false;
}
if (GetPhysicalAddressInternal(va) + PageSize != GetPhysicalAddressInternal(va + PageSize))
{
return false;
}
va += PageSize;
}
return true;
}
private void ReadImpl(ulong va, Span<byte> data)
{
if (data.Length == 0)
{
return;
}
int offset = 0, size;
if ((va & PageMask) != 0)
{
ulong pa = GetPhysicalAddressInternal(va);
size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
_backingMemory.GetSpan(pa, size).CopyTo(data.Slice(0, size));
offset += size;
}
for (; offset < data.Length; offset += size)
{
ulong pa = GetPhysicalAddressInternal(va + (ulong)offset);
size = Math.Min(data.Length - offset, PageSize);
_backingMemory.GetSpan(pa, size).CopyTo(data.Slice(offset, size));
}
}
/// <summary>
/// Checks if a specified virtual memory region has been modified by the CPU since the last call.
/// </summary>
/// <param name="va">Virtual address of the region</param>
/// <param name="size">Size of the region</param>
/// <param name="id">Resource identifier number (maximum is 15)</param>
/// <param name="modifiedRanges">Optional array where the modified ranges should be written</param>
/// <returns>The number of modified ranges</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int QueryModified(ulong va, ulong size, int id, (ulong, ulong)[] modifiedRanges = null)
{
if (!ValidateAddress(va))
{
return 0;
}
ulong maxSize = _addressSpaceSize - va;
if (size > maxSize)
{
size = maxSize;
}
// We need to ensure that the tagged pointer value is negative,
// JIT generated code checks that to take the slow paths and call the MemoryManager Read/Write methods.
long tag = (0x8000L | (1L << id)) << 48;
ulong endVa = (va + size + PageMask) & ~(ulong)PageMask;
va &= ~(ulong)PageMask;
ulong rgStart = va;
ulong rgSize = 0;
int rangeIndex = 0;
for (; va < endVa; va += PageSize)
{
while (true)
{
ref long pte = ref _pageTable.GetRef<long>((va >> PageBits) * PteSize);
long pteValue = pte;
// If the PTE value is 0, that means that the page is unmapped.
// We behave as if the page was not modified, since modifying a page
// that is not even mapped is impossible.
if ((pteValue & tag) == tag || pteValue == 0)
{
if (rgSize != 0)
{
if (modifiedRanges != null && rangeIndex < modifiedRanges.Length)
{
modifiedRanges[rangeIndex] = (rgStart, rgSize);
}
rangeIndex++;
rgSize = 0;
}
break;
}
else
{
if (Interlocked.CompareExchange(ref pte, pteValue | tag, pteValue) == pteValue)
{
if (rgSize == 0)
{
rgStart = va;
}
rgSize += PageSize;
break;
}
}
}
}
if (rgSize != 0)
{
if (modifiedRanges != null && rangeIndex < modifiedRanges.Length)
{
modifiedRanges[rangeIndex] = (rgStart, rgSize);
}
rangeIndex++;
}
return rangeIndex;
}
/// <summary>
/// Checks if the page at a given CPU virtual address.
/// </summary>
/// <param name="va">Virtual address to check</param>
/// <returns>True if the address is mapped, false otherwise</returns>
public bool IsMapped(ulong va)
{
if (!ValidateAddress(va))
{
return false;
}
return _pageTable.Read<ulong>((va / PageSize) * PteSize) != 0;
}
private bool ValidateAddress(ulong va)
{
return va < _addressSpaceSize;
}
/// <summary>
/// Performs address translation of the address inside a CPU mapped memory range.
/// </summary>
/// <remarks>
/// If the address is invalid or unmapped, -1 will be returned.
/// </remarks>
/// <param name="va">Virtual address to be translated</param>
/// <returns>The physical address</returns>
public ulong GetPhysicalAddress(ulong va)
{
// We return -1L if the virtual address is invalid or unmapped.
if (!ValidateAddress(va) || !IsMapped(va))
{
return ulong.MaxValue;
}
return GetPhysicalAddressInternal(va);
}
private ulong GetPhysicalAddressInternal(ulong va)
{
return PteToPa(_pageTable.Read<ulong>((va / PageSize) * PteSize) & ~(0xffffUL << 48)) + (va & PageMask);
}
private void MarkRegionAsModified(ulong va, ulong size)
{
ulong endVa = (va + size + PageMask) & ~(ulong)PageMask;
while (va < endVa)
{
ref long pageRef = ref _pageTable.GetRef<long>((va >> PageBits) * PteSize);
long pte;
do
{
pte = Volatile.Read(ref pageRef);
if (pte >= 0)
{
break;
}
}
while (Interlocked.CompareExchange(ref pageRef, pte & ~(0xffffL << 48), pte) != pte);
va += PageSize;
}
}
private ulong PaToPte(ulong pa)
{
return (ulong)_backingMemory.GetPointer(pa, PageSize).ToInt64();
}
private ulong PteToPa(ulong pte)
{
return (ulong)((long)pte - _backingMemory.Pointer.ToInt64());
}
public void Dispose()
{
_pageTable.Dispose();
}
}
}

View file

@ -0,0 +1,19 @@
using System;
namespace Ryujinx.Cpu
{
class MemoryNotContiguousException : Exception
{
public MemoryNotContiguousException() : base("The specified memory region is not contiguous.")
{
}
public MemoryNotContiguousException(string message) : base(message)
{
}
public MemoryNotContiguousException(string message, Exception innerException) : base(message, innerException)
{
}
}
}

View file

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ARMeilleure\ARMeilleure.csproj" />
<ProjectReference Include="..\Ryujinx.Memory\Ryujinx.Memory.csproj" />
</ItemGroup>
</Project>

View file

@ -108,7 +108,7 @@ namespace Ryujinx.Graphics.Gpu
/// This is required for any GPU memory access. /// This is required for any GPU memory access.
/// </summary> /// </summary>
/// <param name="cpuMemory">CPU memory manager</param> /// <param name="cpuMemory">CPU memory manager</param>
public void SetVmm(ARMeilleure.Memory.MemoryManager cpuMemory) public void SetVmm(Cpu.MemoryManager cpuMemory)
{ {
PhysicalMemory = new PhysicalMemory(cpuMemory); PhysicalMemory = new PhysicalMemory(cpuMemory);
} }

View file

@ -33,6 +33,8 @@ namespace Ryujinx.Graphics.Gpu.Image
/// </summary> /// </summary>
public ulong Size { get; } public ulong Size { get; }
private readonly (ulong, ulong)[] _modifiedRanges;
public Pool(GpuContext context, ulong address, int maximumId) public Pool(GpuContext context, ulong address, int maximumId)
{ {
Context = context; Context = context;
@ -46,6 +48,8 @@ namespace Ryujinx.Graphics.Gpu.Image
Address = address; Address = address;
Size = size; Size = size;
_modifiedRanges = new (ulong, ulong)[size / PhysicalMemory.PageSize];
} }
/// <summary> /// <summary>
@ -62,11 +66,11 @@ namespace Ryujinx.Graphics.Gpu.Image
/// </summary> /// </summary>
public void SynchronizeMemory() public void SynchronizeMemory()
{ {
(ulong, ulong)[] modifiedRanges = Context.PhysicalMemory.GetModifiedRanges(Address, Size, ResourceName.TexturePool); int count = Context.PhysicalMemory.QueryModified(Address, Size, ResourceName.TexturePool, _modifiedRanges);
for (int index = 0; index < modifiedRanges.Length; index++) for (int index = 0; index < count; index++)
{ {
(ulong mAddress, ulong mSize) = modifiedRanges[index]; (ulong mAddress, ulong mSize) = _modifiedRanges[index];
if (mAddress < Address) if (mAddress < Address)
{ {
@ -84,6 +88,11 @@ namespace Ryujinx.Graphics.Gpu.Image
} }
} }
private void InvalidateRangeInternal(ulong offset, int size)
{
InvalidateRangeImpl(Address + offset, (ulong)size);
}
/// <summary> /// <summary>
/// Invalidates a range of memory of the GPU resource pool. /// Invalidates a range of memory of the GPU resource pool.
/// Entries that falls inside the speicified range will be invalidated, /// Entries that falls inside the speicified range will be invalidated,

View file

@ -78,6 +78,8 @@ namespace Ryujinx.Graphics.Gpu.Image
/// </summary> /// </summary>
public ulong Size => (ulong)_sizeInfo.TotalSize; public ulong Size => (ulong)_sizeInfo.TotalSize;
private (ulong, ulong)[] _modifiedRanges;
private int _referenceCount; private int _referenceCount;
private int _sequenceNumber; private int _sequenceNumber;
@ -133,6 +135,8 @@ namespace Ryujinx.Graphics.Gpu.Image
_context = context; _context = context;
_sizeInfo = sizeInfo; _sizeInfo = sizeInfo;
_modifiedRanges = new (ulong, ulong)[(sizeInfo.TotalSize / PhysicalMemory.PageSize) + 1];
SetInfo(info); SetInfo(info);
_viewStorage = this; _viewStorage = this;
@ -304,9 +308,9 @@ namespace Ryujinx.Graphics.Gpu.Image
_sequenceNumber = _context.SequenceNumber; _sequenceNumber = _context.SequenceNumber;
(ulong, ulong)[] modifiedRanges = _context.PhysicalMemory.GetModifiedRanges(Address, Size, ResourceName.Texture); int modifiedCount = _context.PhysicalMemory.QueryModified(Address, Size, ResourceName.Texture, _modifiedRanges);
if (modifiedRanges.Length == 0 && _hasData) if (modifiedCount == 0 && _hasData)
{ {
return; return;
} }
@ -325,9 +329,9 @@ namespace Ryujinx.Graphics.Gpu.Image
ulong endAddress = Address + Size; ulong endAddress = Address + Size;
for (int i = 0; i < modifiedRanges.Length; i++) for (int i = 0; i < modifiedCount; i++)
{ {
(ulong modifiedAddress, ulong modifiedSize) = modifiedRanges[i]; (ulong modifiedAddress, ulong modifiedSize) = _modifiedRanges[i];
ulong endModifiedAddress = modifiedAddress + modifiedSize; ulong endModifiedAddress = modifiedAddress + modifiedSize;

View file

@ -30,7 +30,9 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// </summary> /// </summary>
public ulong EndAddress => Address + Size; public ulong EndAddress => Address + Size;
private int[] _sequenceNumbers; private readonly (ulong, ulong)[] _modifiedRanges;
private readonly int[] _sequenceNumbers;
/// <summary> /// <summary>
/// Creates a new instance of the buffer. /// Creates a new instance of the buffer.
@ -46,6 +48,8 @@ namespace Ryujinx.Graphics.Gpu.Memory
HostBuffer = context.Renderer.CreateBuffer((int)size); HostBuffer = context.Renderer.CreateBuffer((int)size);
_modifiedRanges = new (ulong, ulong)[size / PhysicalMemory.PageSize];
_sequenceNumbers = new int[size / MemoryManager.PageSize]; _sequenceNumbers = new int[size / MemoryManager.PageSize];
} }
@ -113,11 +117,11 @@ namespace Ryujinx.Graphics.Gpu.Memory
return; return;
} }
(ulong, ulong)[] modifiedRanges = _context.PhysicalMemory.GetModifiedRanges(address, size, ResourceName.Buffer); int count = _context.PhysicalMemory.QueryModified(address, size, ResourceName.Buffer, _modifiedRanges);
for (int index = 0; index < modifiedRanges.Length; index++) for (int index = 0; index < count; index++)
{ {
(ulong mAddress, ulong mSize) = modifiedRanges[index]; (ulong mAddress, ulong mSize) = _modifiedRanges[index];
int offset = (int)(mAddress - Address); int offset = (int)(mAddress - Address);

View file

@ -1,22 +1,23 @@
using System; using System;
using System.Runtime.CompilerServices;
namespace Ryujinx.Graphics.Gpu.Memory namespace Ryujinx.Graphics.Gpu.Memory
{ {
using CpuMemoryManager = ARMeilleure.Memory.MemoryManager;
/// <summary> /// <summary>
/// Represents physical memory, accessible from the GPU. /// Represents physical memory, accessible from the GPU.
/// This is actually working CPU virtual addresses, of memory mapped on the application process. /// This is actually working CPU virtual addresses, of memory mapped on the application process.
/// </summary> /// </summary>
class PhysicalMemory class PhysicalMemory
{ {
private readonly CpuMemoryManager _cpuMemory; public const int PageSize = Cpu.MemoryManager.PageSize;
private readonly Cpu.MemoryManager _cpuMemory;
/// <summary> /// <summary>
/// Creates a new instance of the physical memory. /// Creates a new instance of the physical memory.
/// </summary> /// </summary>
/// <param name="cpuMemory">CPU memory manager of the application process</param> /// <param name="cpuMemory">CPU memory manager of the application process</param>
public PhysicalMemory(CpuMemoryManager cpuMemory) public PhysicalMemory(Cpu.MemoryManager cpuMemory)
{ {
_cpuMemory = cpuMemory; _cpuMemory = cpuMemory;
} }
@ -29,7 +30,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// <returns>A read only span of the data at the specified memory location</returns> /// <returns>A read only span of the data at the specified memory location</returns>
public ReadOnlySpan<byte> GetSpan(ulong address, ulong size) public ReadOnlySpan<byte> GetSpan(ulong address, ulong size)
{ {
return _cpuMemory.GetSpan(address, size); return _cpuMemory.GetSpan(address, (int)size);
} }
/// <summary> /// <summary>
@ -39,19 +40,21 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// <param name="data">Data to be written</param> /// <param name="data">Data to be written</param>
public void Write(ulong address, ReadOnlySpan<byte> data) public void Write(ulong address, ReadOnlySpan<byte> data)
{ {
_cpuMemory.WriteBytes((long)address, data.ToArray()); _cpuMemory.Write(address, data);
} }
/// <summary> /// <summary>
/// Gets the modified ranges for a given range of the application process mapped memory. /// Checks if a specified virtual memory region has been modified by the CPU since the last call.
/// </summary> /// </summary>
/// <param name="address">Start address of the range</param> /// <param name="address">CPU virtual address of the region</param>
/// <param name="size">Size, in bytes, of the range</param> /// <param name="size">Size of the region</param>
/// <param name="name">Name of the GPU resource being checked</param> /// <param name="name">Resource name</param>
/// <returns>Ranges, composed of address and size, modified by the application process, form the CPU</returns> /// <param name="modifiedRanges">Optional array where the modified ranges should be written</param>
public (ulong, ulong)[] GetModifiedRanges(ulong address, ulong size, ResourceName name) /// <returns>The number of modified ranges</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int QueryModified(ulong address, ulong size, ResourceName name, (ulong, ulong)[] modifiedRanges = null)
{ {
return _cpuMemory.GetModifiedRanges(address, size, (int)name); return _cpuMemory.QueryModified(address, size, (int)name, modifiedRanges);
} }
} }
} }

View file

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ARMeilleure\ARMeilleure.csproj" /> <ProjectReference Include="..\Ryujinx.Cpu\Ryujinx.Cpu.csproj" />
<ProjectReference Include="..\Ryujinx.Graphics.GAL\Ryujinx.Graphics.GAL.csproj" /> <ProjectReference Include="..\Ryujinx.Graphics.GAL\Ryujinx.Graphics.GAL.csproj" />
<ProjectReference Include="..\Ryujinx.Common\Ryujinx.Common.csproj" /> <ProjectReference Include="..\Ryujinx.Common\Ryujinx.Common.csproj" />
<ProjectReference Include="..\Ryujinx.Graphics.Texture\Ryujinx.Graphics.Texture.csproj" /> <ProjectReference Include="..\Ryujinx.Graphics.Texture\Ryujinx.Graphics.Texture.csproj" />

View file

@ -1,200 +0,0 @@
using ARMeilleure.Memory;
using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
namespace Ryujinx.HLE
{
class DeviceMemory : IDisposable
{
public const long RamSize = 4L * 1024 * 1024 * 1024;
public IntPtr RamPointer { get; }
private unsafe byte* _ramPtr;
public unsafe DeviceMemory()
{
RamPointer = MemoryManagement.AllocateWriteTracked(RamSize);
_ramPtr = (byte*)RamPointer;
}
public sbyte ReadSByte(long position)
{
return (sbyte)ReadByte(position);
}
public short ReadInt16(long position)
{
return (short)ReadUInt16(position);
}
public int ReadInt32(long position)
{
return (int)ReadUInt32(position);
}
public long ReadInt64(long position)
{
return (long)ReadUInt64(position);
}
public unsafe byte ReadByte(long position)
{
return *(_ramPtr + position);
}
public unsafe ushort ReadUInt16(long position)
{
return *((ushort*)(_ramPtr + position));
}
public unsafe uint ReadUInt32(long position)
{
return *((uint*)(_ramPtr + position));
}
public unsafe ulong ReadUInt64(long position)
{
return *((ulong*)(_ramPtr + position));
}
public unsafe T ReadStruct<T>(long position)
{
return Marshal.PtrToStructure<T>((IntPtr)(_ramPtr + position));
}
public unsafe ref T GetStructRef<T>(long position)
{
return ref Unsafe.AsRef<T>((void*)(IntPtr)(_ramPtr + position));
}
public void WriteSByte(long position, sbyte value)
{
WriteByte(position, (byte)value);
}
public void WriteInt16(long position, short value)
{
WriteUInt16(position, (ushort)value);
}
public void WriteInt32(long position, int value)
{
WriteUInt32(position, (uint)value);
}
public void WriteInt64(long position, long value)
{
WriteUInt64(position, (ulong)value);
}
public unsafe void WriteByte(long position, byte value)
{
*(_ramPtr + position) = value;
}
public unsafe void WriteUInt16(long position, ushort value)
{
*((ushort*)(_ramPtr + position)) = value;
}
public unsafe void WriteUInt32(long position, uint value)
{
*((uint*)(_ramPtr + position)) = value;
}
public unsafe void WriteUInt64(long position, ulong value)
{
*((ulong*)(_ramPtr + position)) = value;
}
public unsafe void WriteStruct<T>(long position, T value)
{
Marshal.StructureToPtr(value, (IntPtr)(_ramPtr + position), false);
}
public void FillWithZeros(long position, int size)
{
int size8 = size & ~(8 - 1);
for (int offs = 0; offs < size8; offs += 8)
{
WriteInt64(position + offs, 0);
}
for (int offs = size8; offs < (size - size8); offs++)
{
WriteByte(position + offs, 0);
}
}
public void Set(ulong address, byte value, ulong size)
{
if (address + size < address)
{
throw new ArgumentOutOfRangeException(nameof(size));
}
if (address + size > RamSize)
{
throw new ArgumentOutOfRangeException(nameof(address));
}
ulong size8 = size & ~7UL;
ulong valueRep = (ulong)value * 0x0101010101010101;
for (ulong offs = 0; offs < size8; offs += 8)
{
WriteUInt64((long)(address + offs), valueRep);
}
for (ulong offs = size8; offs < (size - size8); offs++)
{
WriteByte((long)(address + offs), value);
}
}
public void Copy(ulong dst, ulong src, ulong size)
{
if (dst + size < dst || src + size < src)
{
throw new ArgumentOutOfRangeException(nameof(size));
}
if (dst + size > RamSize)
{
throw new ArgumentOutOfRangeException(nameof(dst));
}
if (src + size > RamSize)
{
throw new ArgumentOutOfRangeException(nameof(src));
}
ulong size8 = size & ~7UL;
for (ulong offs = 0; offs < size8; offs += 8)
{
WriteUInt64((long)(dst + offs), ReadUInt64((long)(src + offs)));
}
for (ulong offs = size8; offs < (size - size8); offs++)
{
WriteByte((long)(dst + offs), ReadByte((long)(src + offs)));
}
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
MemoryManagement.Free(RamPointer);
}
}
}

View file

@ -19,7 +19,7 @@ namespace Ryujinx.HLE.HOS.Font
{ {
private Switch _device; private Switch _device;
private long _physicalAddress; private ulong _physicalAddress;
private struct FontInfo private struct FontInfo
{ {
@ -35,7 +35,7 @@ namespace Ryujinx.HLE.HOS.Font
private Dictionary<SharedFontType, FontInfo> _fontData; private Dictionary<SharedFontType, FontInfo> _fontData;
public SharedFontManager(Switch device, long physicalAddress) public SharedFontManager(Switch device, ulong physicalAddress)
{ {
_physicalAddress = physicalAddress; _physicalAddress = physicalAddress;
_device = device; _device = device;
@ -52,7 +52,7 @@ namespace Ryujinx.HLE.HOS.Font
{ {
if (_fontData == null) if (_fontData == null)
{ {
_device.Memory.FillWithZeros(_physicalAddress, Horizon.FontSize); _device.Memory.ZeroFill(_physicalAddress, Horizon.FontSize);
uint fontOffset = 0; uint fontOffset = 0;
@ -67,7 +67,7 @@ namespace Ryujinx.HLE.HOS.Font
if (!string.IsNullOrWhiteSpace(fontPath)) if (!string.IsNullOrWhiteSpace(fontPath))
{ {
byte[] data; byte[] data;
using (IStorage ncaFileStream = new LocalStorage(fontPath, FileAccess.Read, FileMode.Open)) using (IStorage ncaFileStream = new LocalStorage(fontPath, FileAccess.Read, FileMode.Open))
{ {
Nca nca = new Nca(_device.System.KeySet, ncaFileStream); Nca nca = new Nca(_device.System.KeySet, ncaFileStream);
@ -77,7 +77,7 @@ namespace Ryujinx.HLE.HOS.Font
data = DecryptFont(fontFile.AsStream()); data = DecryptFont(fontFile.AsStream());
} }
FontInfo info = new FontInfo((int)fontOffset, data.Length); FontInfo info = new FontInfo((int)fontOffset, data.Length);
WriteMagicAndSize(_physicalAddress + fontOffset, data.Length); WriteMagicAndSize(_physicalAddress + fontOffset, data.Length);
@ -88,7 +88,7 @@ namespace Ryujinx.HLE.HOS.Font
for (; fontOffset - start < data.Length; fontOffset++) for (; fontOffset - start < data.Length; fontOffset++)
{ {
_device.Memory.WriteByte(_physicalAddress + fontOffset, data[fontOffset - start]); _device.Memory.Write(_physicalAddress + fontOffset, data[fontOffset - start]);
} }
return info; return info;
@ -129,15 +129,15 @@ namespace Ryujinx.HLE.HOS.Font
} }
} }
private void WriteMagicAndSize(long position, int size) private void WriteMagicAndSize(ulong address, int size)
{ {
const int decMagic = 0x18029a7f; const int decMagic = 0x18029a7f;
const int key = 0x49621806; const int key = 0x49621806;
int encryptedSize = BinaryPrimitives.ReverseEndianness(size ^ key); int encryptedSize = BinaryPrimitives.ReverseEndianness(size ^ key);
_device.Memory.WriteInt32(position + 0, decMagic); _device.Memory.Write(address + 0, decMagic);
_device.Memory.WriteInt32(position + 4, encryptedSize); _device.Memory.Write(address + 4, encryptedSize);
} }
public int GetFontSize(SharedFontType fontType) public int GetFontSize(SharedFontType fontType)

View file

@ -1,77 +0,0 @@
using ARMeilleure.Memory;
using System.Text;
namespace Ryujinx.HLE.HOS
{
static class Homebrew
{
public const string TemporaryNroSuffix = ".ryu_tmp.nro";
// http://switchbrew.org/index.php?title=Homebrew_ABI
public static void WriteHbAbiData(MemoryManager memory, long position, int mainThreadHandle, string switchPath)
{
// MainThreadHandle.
WriteConfigEntry(memory, ref position, 1, 0, mainThreadHandle);
// NextLoadPath.
WriteConfigEntry(memory, ref position, 2, 0, position + 0x200, position + 0x400);
// Argv.
long argvPosition = position + 0xC00;
memory.WriteBytes(argvPosition, Encoding.ASCII.GetBytes(switchPath + "\0"));
WriteConfigEntry(memory, ref position, 5, 0, 0, argvPosition);
// AppletType.
WriteConfigEntry(memory, ref position, 7);
// EndOfList.
WriteConfigEntry(memory, ref position, 0);
}
private static void WriteConfigEntry(
MemoryManager memory,
ref long position,
int key,
int flags = 0,
long value0 = 0,
long value1 = 0)
{
memory.WriteInt32(position + 0x00, key);
memory.WriteInt32(position + 0x04, flags);
memory.WriteInt64(position + 0x08, value0);
memory.WriteInt64(position + 0x10, value1);
position += 0x18;
}
public static string ReadHbAbiNextLoadPath(MemoryManager memory, long position)
{
string fileName = null;
while (true)
{
long key = memory.ReadInt64(position);
if (key == 2)
{
long value0 = memory.ReadInt64(position + 0x08);
long value1 = memory.ReadInt64(position + 0x10);
fileName = MemoryHelper.ReadAsciiString(memory, value0, value1 - value0);
break;
}
else if (key == 0)
{
break;
}
position += 0x18;
}
return fileName;
}
}
}

View file

@ -141,7 +141,7 @@ namespace Ryujinx.HLE.HOS
public int GlobalAccessLogMode { get; set; } public int GlobalAccessLogMode { get; set; }
internal long HidBaseAddress { get; private set; } internal ulong HidBaseAddress { get; private set; }
internal NvHostSyncpt HostSyncpoint { get; private set; } internal NvHostSyncpt HostSyncpoint { get; private set; }
@ -202,7 +202,7 @@ namespace Ryujinx.HLE.HOS
ulong iirsPa = region.Address + HidSize + FontSize; ulong iirsPa = region.Address + HidSize + FontSize;
ulong timePa = region.Address + HidSize + FontSize + IirsSize; ulong timePa = region.Address + HidSize + FontSize + IirsSize;
HidBaseAddress = (long)(hidPa - DramMemoryMap.DramBase); HidBaseAddress = hidPa - DramMemoryMap.DramBase;
KPageList hidPageList = new KPageList(); KPageList hidPageList = new KPageList();
KPageList fontPageList = new KPageList(); KPageList fontPageList = new KPageList();
@ -220,13 +220,13 @@ namespace Ryujinx.HLE.HOS
KSharedMemory timeSharedMemory = new KSharedMemory(this, timePageList, 0, 0, MemoryPermission.Read); KSharedMemory timeSharedMemory = new KSharedMemory(this, timePageList, 0, 0, MemoryPermission.Read);
TimeServiceManager.Instance.Initialize(device, this, timeSharedMemory, (long)(timePa - DramMemoryMap.DramBase), TimeSize); TimeServiceManager.Instance.Initialize(device, this, timeSharedMemory, timePa - DramMemoryMap.DramBase, TimeSize);
AppletState = new AppletStateMgr(this); AppletState = new AppletStateMgr(this);
AppletState.SetFocus(true); AppletState.SetFocus(true);
Font = new SharedFontManager(device, (long)(fontPa - DramMemoryMap.DramBase)); Font = new SharedFontManager(device, fontPa - DramMemoryMap.DramBase);
IUserInterface.InitializePort(this); IUserInterface.InitializePort(this);

View file

@ -1,4 +1,4 @@
using ARMeilleure.Memory; using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Ipc; using Ryujinx.HLE.HOS.Kernel.Ipc;
using Ryujinx.HLE.HOS.Kernel.Process; using Ryujinx.HLE.HOS.Kernel.Process;
@ -103,7 +103,7 @@ namespace Ryujinx.HLE.HOS.Ipc
throw new NotImplementedException(request.Type.ToString()); throw new NotImplementedException(request.Type.ToString());
} }
memory.WriteBytes(cmdPtr, response.GetBytes(cmdPtr)); memory.Write((ulong)cmdPtr, response.GetBytes(cmdPtr));
} }
return KernelResult.Success; return KernelResult.Success;

View file

@ -1,5 +1,5 @@
using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Kernel.Process; using Ryujinx.HLE.HOS.Kernel.Process;
using ARMeilleure.Memory;
namespace Ryujinx.HLE.HOS.Kernel.Common namespace Ryujinx.HLE.HOS.Kernel.Common
{ {
@ -9,10 +9,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Common
{ {
KProcess currentProcess = system.Scheduler.GetCurrentProcess(); KProcess currentProcess = system.Scheduler.GetCurrentProcess();
if (currentProcess.CpuMemory.IsMapped((long)address) && if (currentProcess.CpuMemory.IsMapped(address) &&
currentProcess.CpuMemory.IsMapped((long)address + 3)) currentProcess.CpuMemory.IsMapped(address + 3))
{ {
value = currentProcess.CpuMemory.ReadInt32((long)address); value = currentProcess.CpuMemory.Read<int>(address);
return true; return true;
} }
@ -28,10 +28,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Common
for (int index = 0; index < values.Length; index++, address += 4) for (int index = 0; index < values.Length; index++, address += 4)
{ {
if (currentProcess.CpuMemory.IsMapped((long)address) && if (currentProcess.CpuMemory.IsMapped(address) &&
currentProcess.CpuMemory.IsMapped((long)address + 3)) currentProcess.CpuMemory.IsMapped(address + 3))
{ {
values[index]= currentProcess.CpuMemory.ReadInt32((long)address); values[index]= currentProcess.CpuMemory.Read<int>(address);
} }
else else
{ {
@ -46,8 +46,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Common
{ {
KProcess currentProcess = system.Scheduler.GetCurrentProcess(); KProcess currentProcess = system.Scheduler.GetCurrentProcess();
if (currentProcess.CpuMemory.IsMapped((long)address) && if (currentProcess.CpuMemory.IsMapped(address) &&
currentProcess.CpuMemory.IsMapped((long)address + size - 1)) currentProcess.CpuMemory.IsMapped(address + (ulong)size - 1))
{ {
value = MemoryHelper.ReadAsciiString(currentProcess.CpuMemory, (long)address, size); value = MemoryHelper.ReadAsciiString(currentProcess.CpuMemory, (long)address, size);
@ -63,10 +63,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Common
{ {
KProcess currentProcess = system.Scheduler.GetCurrentProcess(); KProcess currentProcess = system.Scheduler.GetCurrentProcess();
if (currentProcess.CpuMemory.IsMapped((long)address) && if (currentProcess.CpuMemory.IsMapped(address) &&
currentProcess.CpuMemory.IsMapped((long)address + 3)) currentProcess.CpuMemory.IsMapped(address + 3))
{ {
currentProcess.CpuMemory.WriteInt32((long)address, value); currentProcess.CpuMemory.Write(address, value);
return true; return true;
} }
@ -78,10 +78,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Common
{ {
KProcess currentProcess = system.Scheduler.GetCurrentProcess(); KProcess currentProcess = system.Scheduler.GetCurrentProcess();
if (currentProcess.CpuMemory.IsMapped((long)address) && if (currentProcess.CpuMemory.IsMapped(address) &&
currentProcess.CpuMemory.IsMapped((long)address + 7)) currentProcess.CpuMemory.IsMapped(address + 7))
{ {
currentProcess.CpuMemory.WriteInt64((long)address, value); currentProcess.CpuMemory.Write(address, value);
return true; return true;
} }

View file

@ -322,8 +322,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
serverHeader.ReceiveListType, serverHeader.ReceiveListType,
serverHeader.ReceiveListOffset); serverHeader.ReceiveListOffset);
serverProcess.CpuMemory.WriteUInt32((long)serverMsg.Address + 0, clientHeader.Word0); serverProcess.CpuMemory.Write(serverMsg.Address + 0, clientHeader.Word0);
serverProcess.CpuMemory.WriteUInt32((long)serverMsg.Address + 4, clientHeader.Word1); serverProcess.CpuMemory.Write(serverMsg.Address + 4, clientHeader.Word1);
uint offset; uint offset;
@ -337,13 +337,13 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
return KernelResult.InvalidCombination; return KernelResult.InvalidCombination;
} }
serverProcess.CpuMemory.WriteUInt32((long)serverMsg.Address + 8, clientHeader.Word2); serverProcess.CpuMemory.Write(serverMsg.Address + 8, clientHeader.Word2);
offset = 3; offset = 3;
if (clientHeader.HasPid) if (clientHeader.HasPid)
{ {
serverProcess.CpuMemory.WriteInt64((long)serverMsg.Address + offset * 4, clientProcess.Pid); serverProcess.CpuMemory.Write(serverMsg.Address + offset * 4, clientProcess.Pid);
offset += 2; offset += 2;
} }
@ -352,14 +352,14 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
{ {
int newHandle = 0; int newHandle = 0;
int handle = System.Device.Memory.ReadInt32((long)clientMsg.DramAddress + offset * 4); int handle = System.Device.Memory.Read<int>(clientMsg.DramAddress + offset * 4);
if (clientResult == KernelResult.Success && handle != 0) if (clientResult == KernelResult.Success && handle != 0)
{ {
clientResult = GetCopyObjectHandle(clientThread, serverProcess, handle, out newHandle); clientResult = GetCopyObjectHandle(clientThread, serverProcess, handle, out newHandle);
} }
serverProcess.CpuMemory.WriteInt32((long)serverMsg.Address + offset * 4, newHandle); serverProcess.CpuMemory.Write(serverMsg.Address + offset * 4, newHandle);
offset++; offset++;
} }
@ -368,7 +368,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
{ {
int newHandle = 0; int newHandle = 0;
int handle = System.Device.Memory.ReadInt32((long)clientMsg.DramAddress + offset * 4); int handle = System.Device.Memory.Read<int>(clientMsg.DramAddress + offset * 4);
if (handle != 0) if (handle != 0)
{ {
@ -382,7 +382,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
} }
} }
serverProcess.CpuMemory.WriteInt32((long)serverMsg.Address + offset * 4, newHandle); serverProcess.CpuMemory.Write(serverMsg.Address + offset * 4, newHandle);
offset++; offset++;
} }
@ -404,7 +404,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
for (int index = 0; index < clientHeader.PointerBuffersCount; index++) for (int index = 0; index < clientHeader.PointerBuffersCount; index++)
{ {
ulong pointerDesc = System.Device.Memory.ReadUInt64((long)clientMsg.DramAddress + offset * 4); ulong pointerDesc = System.Device.Memory.Read<ulong>(clientMsg.DramAddress + offset * 4);
PointerBufferDesc descriptor = new PointerBufferDesc(pointerDesc); PointerBufferDesc descriptor = new PointerBufferDesc(pointerDesc);
@ -450,7 +450,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
descriptor.BufferAddress = 0; descriptor.BufferAddress = 0;
} }
serverProcess.CpuMemory.WriteUInt64((long)serverMsg.Address + offset * 4, descriptor.Pack()); serverProcess.CpuMemory.Write(serverMsg.Address + offset * 4, descriptor.Pack());
offset += 2; offset += 2;
} }
@ -463,11 +463,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
for (int index = 0; index < totalBuffersCount; index++) for (int index = 0; index < totalBuffersCount; index++)
{ {
long clientDescAddress = (long)clientMsg.DramAddress + offset * 4; ulong clientDescAddress = clientMsg.DramAddress + offset * 4;
uint descWord0 = System.Device.Memory.ReadUInt32(clientDescAddress + 0); uint descWord0 = System.Device.Memory.Read<uint>(clientDescAddress + 0);
uint descWord1 = System.Device.Memory.ReadUInt32(clientDescAddress + 4); uint descWord1 = System.Device.Memory.Read<uint>(clientDescAddress + 4);
uint descWord2 = System.Device.Memory.ReadUInt32(clientDescAddress + 8); uint descWord2 = System.Device.Memory.Read<uint>(clientDescAddress + 8);
bool isSendDesc = index < clientHeader.SendBuffersCount; bool isSendDesc = index < clientHeader.SendBuffersCount;
bool isExchangeDesc = index >= clientHeader.SendBuffersCount + clientHeader.ReceiveBuffersCount; bool isExchangeDesc = index >= clientHeader.SendBuffersCount + clientHeader.ReceiveBuffersCount;
@ -542,11 +542,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
descWord2 |= (uint)(dstAddress >> 34) & 0x3ffffffc; descWord2 |= (uint)(dstAddress >> 34) & 0x3ffffffc;
descWord2 |= (uint)(dstAddress >> 4) & 0xf0000000; descWord2 |= (uint)(dstAddress >> 4) & 0xf0000000;
long serverDescAddress = (long)serverMsg.Address + offset * 4; ulong serverDescAddress = serverMsg.Address + offset * 4;
serverProcess.CpuMemory.WriteUInt32(serverDescAddress + 0, descWord0); serverProcess.CpuMemory.Write(serverDescAddress + 0, descWord0);
serverProcess.CpuMemory.WriteUInt32(serverDescAddress + 4, descWord1); serverProcess.CpuMemory.Write(serverDescAddress + 4, descWord1);
serverProcess.CpuMemory.WriteUInt32(serverDescAddress + 8, descWord2); serverProcess.CpuMemory.Write(serverDescAddress + 8, descWord2);
offset += 3; offset += 3;
} }
@ -700,8 +700,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
} }
// Copy header. // Copy header.
System.Device.Memory.WriteUInt32((long)clientMsg.DramAddress + 0, serverHeader.Word0); System.Device.Memory.Write(clientMsg.DramAddress + 0, serverHeader.Word0);
System.Device.Memory.WriteUInt32((long)clientMsg.DramAddress + 4, serverHeader.Word1); System.Device.Memory.Write(clientMsg.DramAddress + 4, serverHeader.Word1);
// Copy handles. // Copy handles.
uint offset; uint offset;
@ -710,11 +710,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
{ {
offset = 3; offset = 3;
System.Device.Memory.WriteUInt32((long)clientMsg.DramAddress + 8, serverHeader.Word2); System.Device.Memory.Write(clientMsg.DramAddress + 8, serverHeader.Word2);
if (serverHeader.HasPid) if (serverHeader.HasPid)
{ {
System.Device.Memory.WriteInt64((long)clientMsg.DramAddress + offset * 4, serverProcess.Pid); System.Device.Memory.Write(clientMsg.DramAddress + offset * 4, serverProcess.Pid);
offset += 2; offset += 2;
} }
@ -723,14 +723,14 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
{ {
int newHandle = 0; int newHandle = 0;
int handle = serverProcess.CpuMemory.ReadInt32((long)serverMsg.Address + offset * 4); int handle = serverProcess.CpuMemory.Read<int>(serverMsg.Address + offset * 4);
if (handle != 0) if (handle != 0)
{ {
GetCopyObjectHandle(serverThread, clientProcess, handle, out newHandle); GetCopyObjectHandle(serverThread, clientProcess, handle, out newHandle);
} }
System.Device.Memory.WriteInt32((long)clientMsg.DramAddress + offset * 4, newHandle); System.Device.Memory.Write(clientMsg.DramAddress + offset * 4, newHandle);
offset++; offset++;
} }
@ -739,7 +739,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
{ {
int newHandle = 0; int newHandle = 0;
int handle = serverProcess.CpuMemory.ReadInt32((long)serverMsg.Address + offset * 4); int handle = serverProcess.CpuMemory.Read<int>(serverMsg.Address + offset * 4);
if (handle != 0) if (handle != 0)
{ {
@ -753,7 +753,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
} }
} }
System.Device.Memory.WriteInt32((long)clientMsg.DramAddress + offset * 4, newHandle); System.Device.Memory.Write(clientMsg.DramAddress + offset * 4, newHandle);
offset++; offset++;
} }
@ -768,7 +768,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
for (int index = 0; index < serverHeader.PointerBuffersCount; index++) for (int index = 0; index < serverHeader.PointerBuffersCount; index++)
{ {
ulong pointerDesc = serverProcess.CpuMemory.ReadUInt64((long)serverMsg.Address + offset * 4); ulong pointerDesc = serverProcess.CpuMemory.Read<ulong>(serverMsg.Address + offset * 4);
PointerBufferDesc descriptor = new PointerBufferDesc(pointerDesc); PointerBufferDesc descriptor = new PointerBufferDesc(pointerDesc);
@ -819,11 +819,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
for (int index = 0; index < totalBuffersCount; index++) for (int index = 0; index < totalBuffersCount; index++)
{ {
long dstDescAddress = (long)clientMsg.DramAddress + offset * 4; ulong dstDescAddress = clientMsg.DramAddress + offset * 4;
System.Device.Memory.WriteUInt32(dstDescAddress + 0, 0); System.Device.Memory.Write(dstDescAddress + 0, 0);
System.Device.Memory.WriteUInt32(dstDescAddress + 4, 0); System.Device.Memory.Write(dstDescAddress + 4, 0);
System.Device.Memory.WriteUInt32(dstDescAddress + 8, 0); System.Device.Memory.Write(dstDescAddress + 8, 0);
offset += 3; offset += 3;
} }
@ -878,9 +878,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
private MessageHeader GetClientMessageHeader(Message clientMsg) private MessageHeader GetClientMessageHeader(Message clientMsg)
{ {
uint word0 = System.Device.Memory.ReadUInt32((long)clientMsg.DramAddress + 0); uint word0 = System.Device.Memory.Read<uint>(clientMsg.DramAddress + 0);
uint word1 = System.Device.Memory.ReadUInt32((long)clientMsg.DramAddress + 4); uint word1 = System.Device.Memory.Read<uint>(clientMsg.DramAddress + 4);
uint word2 = System.Device.Memory.ReadUInt32((long)clientMsg.DramAddress + 8); uint word2 = System.Device.Memory.Read<uint>(clientMsg.DramAddress + 8);
return new MessageHeader(word0, word1, word2); return new MessageHeader(word0, word1, word2);
} }
@ -889,9 +889,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
{ {
KProcess currentProcess = System.Scheduler.GetCurrentProcess(); KProcess currentProcess = System.Scheduler.GetCurrentProcess();
uint word0 = currentProcess.CpuMemory.ReadUInt32((long)serverMsg.Address + 0); uint word0 = currentProcess.CpuMemory.Read<uint>(serverMsg.Address + 0);
uint word1 = currentProcess.CpuMemory.ReadUInt32((long)serverMsg.Address + 4); uint word1 = currentProcess.CpuMemory.Read<uint>(serverMsg.Address + 4);
uint word2 = currentProcess.CpuMemory.ReadUInt32((long)serverMsg.Address + 8); uint word2 = currentProcess.CpuMemory.Read<uint>(serverMsg.Address + 8);
return new MessageHeader(word0, word1, word2); return new MessageHeader(word0, word1, word2);
} }
@ -970,11 +970,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
ulong[] receiveList = new ulong[recvListSize]; ulong[] receiveList = new ulong[recvListSize];
long recvListAddress = (long)message.DramAddress + recvListOffset; ulong recvListAddress = message.DramAddress + recvListOffset;
for (int index = 0; index < recvListSize; index++) for (int index = 0; index < recvListSize; index++)
{ {
receiveList[index] = System.Device.Memory.ReadUInt64(recvListAddress + index * 8); receiveList[index] = System.Device.Memory.Read<ulong>(recvListAddress + (ulong)index * 8);
} }
return receiveList; return receiveList;
@ -1067,20 +1067,20 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
if (header.HasPid) if (header.HasPid)
{ {
process.CpuMemory.WriteInt64((long)message.Address + offset * 4, 0); process.CpuMemory.Write(message.Address + offset * 4, 0L);
offset += 2; offset += 2;
} }
for (int index = 0; index < totalHandeslCount; index++) for (int index = 0; index < totalHandeslCount; index++)
{ {
int handle = process.CpuMemory.ReadInt32((long)message.Address + offset * 4); int handle = process.CpuMemory.Read<int>(message.Address + offset * 4);
if (handle != 0) if (handle != 0)
{ {
process.HandleTable.CloseHandle(handle); process.HandleTable.CloseHandle(handle);
process.CpuMemory.WriteInt32((long)message.Address + offset * 4, 0); process.CpuMemory.Write(message.Address + offset * 4, 0);
} }
offset++; offset++;
@ -1225,8 +1225,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc
ulong address = clientProcess.MemoryManager.GetDramAddressFromVa(request.CustomCmdBuffAddr); ulong address = clientProcess.MemoryManager.GetDramAddressFromVa(request.CustomCmdBuffAddr);
System.Device.Memory.WriteInt64((long)address + 0, 0); System.Device.Memory.Write<ulong>(address, 0);
System.Device.Memory.WriteInt32((long)address + 8, (int)result); System.Device.Memory.Write(address + 8, (int)result);
clientProcess.MemoryManager.UnborrowIpcBuffer( clientProcess.MemoryManager.UnborrowIpcBuffer(
request.CustomCmdBuffAddr, request.CustomCmdBuffAddr,

View file

@ -1,5 +1,5 @@
using ARMeilleure.Memory;
using Ryujinx.Common; using Ryujinx.Common;
using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Process; using Ryujinx.HLE.HOS.Kernel.Process;
using System; using System;
@ -1843,7 +1843,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
{ {
ulong unusedSizeBefore = address - addressTruncated; ulong unusedSizeBefore = address - addressTruncated;
_system.Device.Memory.Set(dstFirstPagePa, 0, unusedSizeBefore); _system.Device.Memory.ZeroFill(dstFirstPagePa, unusedSizeBefore);
ulong copySize = addressRounded <= endAddr ? addressRounded - address : size; ulong copySize = addressRounded <= endAddr ? addressRounded - address : size;
@ -1862,7 +1862,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
if (unusedSizeAfter != 0) if (unusedSizeAfter != 0)
{ {
_system.Device.Memory.Set(firstPageFillAddress, 0, unusedSizeAfter); _system.Device.Memory.ZeroFill(firstPageFillAddress, unusedSizeAfter);
} }
KPageList pages = new KPageList(); KPageList pages = new KPageList();
@ -1922,7 +1922,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
unusedSizeAfter = PageSize; unusedSizeAfter = PageSize;
} }
_system.Device.Memory.Set(lastPageFillAddr, 0, unusedSizeAfter); _system.Device.Memory.ZeroFill(lastPageFillAddr, unusedSizeAfter);
if (pages.AddRange(dstFirstPagePa, 1) != KernelResult.Success) if (pages.AddRange(dstFirstPagePa, 1) != KernelResult.Success)
{ {
@ -3041,7 +3041,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
{ {
ulong size = pagesCount * PageSize; ulong size = pagesCount * PageSize;
_cpuMemory.Map((long)dstVa, (long)(srcPa - DramMemoryMap.DramBase), (long)size); _cpuMemory.Map(dstVa, srcPa - DramMemoryMap.DramBase, size);
result = KernelResult.Success; result = KernelResult.Success;
@ -3066,7 +3066,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
{ {
ulong size = pagesCount * PageSize; ulong size = pagesCount * PageSize;
_cpuMemory.Unmap((long)dstVa, (long)size); _cpuMemory.Unmap(dstVa, size);
result = KernelResult.Success; result = KernelResult.Success;
@ -3108,7 +3108,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
{ {
ulong size = pageNode.PagesCount * PageSize; ulong size = pageNode.PagesCount * PageSize;
_cpuMemory.Map((long)address, (long)(pageNode.Address - DramMemoryMap.DramBase), (long)size); _cpuMemory.Map(address, pageNode.Address - DramMemoryMap.DramBase, size);
address += size; address += size;
} }
@ -3118,12 +3118,12 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
public ulong GetDramAddressFromVa(ulong va) public ulong GetDramAddressFromVa(ulong va)
{ {
return (ulong)_cpuMemory.GetPhysicalAddress((long)va); return _cpuMemory.GetPhysicalAddress(va);
} }
public bool ConvertVaToPa(ulong va, out ulong pa) public bool ConvertVaToPa(ulong va, out ulong pa)
{ {
pa = DramMemoryMap.DramBase + (ulong)_cpuMemory.GetPhysicalAddress((long)va); pa = DramMemoryMap.DramBase + _cpuMemory.GetPhysicalAddress(va);
return true; return true;
} }

View file

@ -1,12 +1,9 @@
using ARMeilleure.Memory; using Ryujinx.Cpu;
using Ryujinx.Common;
using Ryujinx.HLE.HOS.Diagnostics.Demangler; using Ryujinx.HLE.HOS.Diagnostics.Demangler;
using Ryujinx.HLE.HOS.Kernel.Memory; using Ryujinx.HLE.HOS.Kernel.Memory;
using Ryujinx.HLE.Loaders.Elf; using Ryujinx.HLE.Loaders.Elf;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
@ -20,11 +17,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
private class Image private class Image
{ {
public long BaseAddress { get; private set; } public ulong BaseAddress { get; }
public ElfSymbol[] Symbols { get; private set; } public ElfSymbol[] Symbols { get; }
public Image(long baseAddress, ElfSymbol[] symbols) public Image(ulong baseAddress, ElfSymbol[] symbols)
{ {
BaseAddress = baseAddress; BaseAddress = baseAddress;
Symbols = symbols; Symbols = symbols;
@ -48,7 +45,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
StringBuilder trace = new StringBuilder(); StringBuilder trace = new StringBuilder();
void AppendTrace(long address) void AppendTrace(ulong address)
{ {
Image image = GetImage(address, out int imageIndex); Image image = GetImage(address, out int imageIndex);
@ -63,7 +60,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
if (image != null) if (image != null)
{ {
long offset = address - image.BaseAddress; ulong offset = address - image.BaseAddress;
string imageName = GetGuessedNsoNameFromIndex(imageIndex); string imageName = GetGuessedNsoNameFromIndex(imageIndex);
@ -79,7 +76,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
if (context.IsAarch32) if (context.IsAarch32)
{ {
long framePointer = (long)context.GetX(11); ulong framePointer = context.GetX(11);
while (framePointer != 0) while (framePointer != 0)
{ {
@ -90,14 +87,14 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
break; break;
} }
AppendTrace(_owner.CpuMemory.ReadInt32(framePointer + 4)); AppendTrace(_owner.CpuMemory.Read<uint>(framePointer + 4));
framePointer = _owner.CpuMemory.ReadInt32(framePointer); framePointer = _owner.CpuMemory.Read<uint>(framePointer);
} }
} }
else else
{ {
long framePointer = (long)context.GetX(29); ulong framePointer = context.GetX(29);
while (framePointer != 0) while (framePointer != 0)
{ {
@ -108,16 +105,16 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
break; break;
} }
AppendTrace(_owner.CpuMemory.ReadInt64(framePointer + 8)); AppendTrace(_owner.CpuMemory.Read<ulong>(framePointer + 8));
framePointer = _owner.CpuMemory.ReadInt64(framePointer); framePointer = _owner.CpuMemory.Read<ulong>(framePointer);
} }
} }
return trace.ToString(); return trace.ToString();
} }
private bool TryGetSubName(Image image, long address, out string name) private bool TryGetSubName(Image image, ulong address, out string name)
{ {
address -= image.BaseAddress; address -= image.BaseAddress;
@ -156,13 +153,13 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
return false; return false;
} }
private Image GetImage(long address, out int index) private Image GetImage(ulong address, out int index)
{ {
lock (_images) lock (_images)
{ {
for (index = _images.Count - 1; index >= 0; index--) for (index = _images.Count - 1; index >= 0; index--)
{ {
if ((ulong)address >= (ulong)_images[index].BaseAddress) if (address >= _images[index].BaseAddress)
{ {
return _images[index]; return _images[index];
} }
@ -229,7 +226,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
if (info.State == MemoryState.CodeStatic && info.Permission == MemoryPermission.ReadAndExecute) if (info.State == MemoryState.CodeStatic && info.Permission == MemoryPermission.ReadAndExecute)
{ {
LoadMod0Symbols(_owner.CpuMemory, (long)info.Address); LoadMod0Symbols(_owner.CpuMemory, info.Address);
} }
oldAddress = address; oldAddress = address;
@ -238,54 +235,53 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
} }
} }
private void LoadMod0Symbols(MemoryManager memory, long textOffset) private void LoadMod0Symbols(MemoryManager memory, ulong textOffset)
{ {
long mod0Offset = textOffset + memory.ReadUInt32(textOffset + 4); ulong mod0Offset = textOffset + memory.Read<uint>(textOffset + 4);
if (mod0Offset < textOffset || !memory.IsMapped(mod0Offset) || (mod0Offset & 3) != 0) if (mod0Offset < textOffset || !memory.IsMapped(mod0Offset) || (mod0Offset & 3) != 0)
{ {
return; return;
} }
Dictionary<ElfDynamicTag, long> dynamic = new Dictionary<ElfDynamicTag, long>(); Dictionary<ElfDynamicTag, ulong> dynamic = new Dictionary<ElfDynamicTag, ulong>();
int mod0Magic = memory.ReadInt32(mod0Offset + 0x0); int mod0Magic = memory.Read<int>(mod0Offset + 0x0);
if (mod0Magic != Mod0) if (mod0Magic != Mod0)
{ {
return; return;
} }
long dynamicOffset = memory.ReadInt32(mod0Offset + 0x4) + mod0Offset; ulong dynamicOffset = memory.Read<uint>(mod0Offset + 0x4) + mod0Offset;
long bssStartOffset = memory.ReadInt32(mod0Offset + 0x8) + mod0Offset; ulong bssStartOffset = memory.Read<uint>(mod0Offset + 0x8) + mod0Offset;
long bssEndOffset = memory.ReadInt32(mod0Offset + 0xc) + mod0Offset; ulong bssEndOffset = memory.Read<uint>(mod0Offset + 0xc) + mod0Offset;
long ehHdrStartOffset = memory.ReadInt32(mod0Offset + 0x10) + mod0Offset; ulong ehHdrStartOffset = memory.Read<uint>(mod0Offset + 0x10) + mod0Offset;
long ehHdrEndOffset = memory.ReadInt32(mod0Offset + 0x14) + mod0Offset; ulong ehHdrEndOffset = memory.Read<uint>(mod0Offset + 0x14) + mod0Offset;
long modObjOffset = memory.ReadInt32(mod0Offset + 0x18) + mod0Offset; ulong modObjOffset = memory.Read<uint>(mod0Offset + 0x18) + mod0Offset;
bool isAArch32 = memory.ReadUInt64(dynamicOffset) > 0xFFFFFFFF || memory.ReadUInt64(dynamicOffset + 0x10) > 0xFFFFFFFF; bool isAArch32 = memory.Read<ulong>(dynamicOffset) > 0xFFFFFFFF || memory.Read<ulong>(dynamicOffset + 0x10) > 0xFFFFFFFF;
while (true) while (true)
{ {
long tagVal; ulong tagVal;
long value; ulong value;
if (isAArch32) if (isAArch32)
{ {
tagVal = memory.ReadInt32(dynamicOffset + 0); tagVal = memory.Read<uint>(dynamicOffset + 0);
value = memory.ReadInt32(dynamicOffset + 4); value = memory.Read<uint>(dynamicOffset + 4);
dynamicOffset += 0x8; dynamicOffset += 0x8;
} }
else else
{ {
tagVal = memory.ReadInt64(dynamicOffset + 0); tagVal = memory.Read<ulong>(dynamicOffset + 0);
value = memory.ReadInt64(dynamicOffset + 8); value = memory.Read<ulong>(dynamicOffset + 8);
dynamicOffset += 0x10; dynamicOffset += 0x10;
} }
ElfDynamicTag tag = (ElfDynamicTag)tagVal; ElfDynamicTag tag = (ElfDynamicTag)tagVal;
if (tag == ElfDynamicTag.DT_NULL) if (tag == ElfDynamicTag.DT_NULL)
@ -296,19 +292,19 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
dynamic[tag] = value; dynamic[tag] = value;
} }
if (!dynamic.TryGetValue(ElfDynamicTag.DT_STRTAB, out long strTab) || if (!dynamic.TryGetValue(ElfDynamicTag.DT_STRTAB, out ulong strTab) ||
!dynamic.TryGetValue(ElfDynamicTag.DT_SYMTAB, out long symTab) || !dynamic.TryGetValue(ElfDynamicTag.DT_SYMTAB, out ulong symTab) ||
!dynamic.TryGetValue(ElfDynamicTag.DT_SYMENT, out long symEntSize)) !dynamic.TryGetValue(ElfDynamicTag.DT_SYMENT, out ulong symEntSize))
{ {
return; return;
} }
long strTblAddr = textOffset + strTab; ulong strTblAddr = textOffset + strTab;
long symTblAddr = textOffset + symTab; ulong symTblAddr = textOffset + symTab;
List<ElfSymbol> symbols = new List<ElfSymbol>(); List<ElfSymbol> symbols = new List<ElfSymbol>();
while ((ulong)symTblAddr < (ulong)strTblAddr) while (symTblAddr < strTblAddr)
{ {
ElfSymbol sym = isAArch32 ? GetSymbol32(memory, symTblAddr, strTblAddr) : GetSymbol64(memory, symTblAddr, strTblAddr); ElfSymbol sym = isAArch32 ? GetSymbol32(memory, symTblAddr, strTblAddr) : GetSymbol64(memory, symTblAddr, strTblAddr);
@ -323,42 +319,36 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
} }
} }
private ElfSymbol GetSymbol64(MemoryManager memory, long address, long strTblAddr) private ElfSymbol GetSymbol64(MemoryManager memory, ulong address, ulong strTblAddr)
{ {
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(memory.ReadBytes(address, Unsafe.SizeOf<ElfSymbol64>())))) ElfSymbol64 sym = memory.Read<ElfSymbol64>(address);
uint nameIndex = sym.NameOffset;
string name = string.Empty;
for (int chr; (chr = memory.Read<byte>(strTblAddr + nameIndex++)) != 0;)
{ {
ElfSymbol64 sym = inputStream.ReadStruct<ElfSymbol64>(); name += (char)chr;
uint nameIndex = sym.NameOffset;
string name = string.Empty;
for (int chr; (chr = memory.ReadByte(strTblAddr + nameIndex++)) != 0;)
{
name += (char)chr;
}
return new ElfSymbol(name, sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size);
} }
return new ElfSymbol(name, sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size);
} }
private ElfSymbol GetSymbol32(MemoryManager memory, long address, long strTblAddr) private ElfSymbol GetSymbol32(MemoryManager memory, ulong address, ulong strTblAddr)
{ {
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(memory.ReadBytes(address, Unsafe.SizeOf<ElfSymbol32>())))) ElfSymbol32 sym = memory.Read<ElfSymbol32>(address);
uint nameIndex = sym.NameOffset;
string name = string.Empty;
for (int chr; (chr = memory.Read<byte>(strTblAddr + nameIndex++)) != 0;)
{ {
ElfSymbol32 sym = inputStream.ReadStruct<ElfSymbol32>(); name += (char)chr;
uint nameIndex = sym.NameOffset;
string name = string.Empty;
for (int chr; (chr = memory.ReadByte(strTblAddr + nameIndex++)) != 0;)
{
name += (char)chr;
}
return new ElfSymbol(name, sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size);
} }
return new ElfSymbol(name, sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size);
} }
} }
} }

View file

@ -1,7 +1,6 @@
using ARMeilleure.Memory;
using ARMeilleure.State; using ARMeilleure.State;
using ARMeilleure.Translation;
using Ryujinx.Common; using Ryujinx.Common;
using Ryujinx.Cpu;
using Ryujinx.HLE.Exceptions; using Ryujinx.HLE.Exceptions;
using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Memory; using Ryujinx.HLE.HOS.Kernel.Memory;
@ -79,8 +78,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
public bool IsPaused { get; private set; } public bool IsPaused { get; private set; }
public MemoryManager CpuMemory { get; private set; } public MemoryManager CpuMemory { get; private set; }
public CpuContext CpuContext { get; private set; }
public Translator Translator { get; private set; }
private SvcHandler _svcHandler; private SvcHandler _svcHandler;
@ -1109,11 +1107,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
default: throw new ArgumentException(nameof(addrSpaceType)); default: throw new ArgumentException(nameof(addrSpaceType));
} }
bool useFlatPageTable = memRegion == MemoryRegion.Application; CpuMemory = new MemoryManager(_system.Device.Memory, 1UL << addrSpaceBits);
CpuContext = new CpuContext(CpuMemory);
CpuMemory = new MemoryManager(_system.Device.Memory.RamPointer, addrSpaceBits, useFlatPageTable);
Translator = new Translator(CpuMemory);
// TODO: This should eventually be removed. // TODO: This should eventually be removed.
// The GPU shouldn't depend on the CPU memory manager at all. // The GPU shouldn't depend on the CPU memory manager at all.

View file

@ -108,7 +108,9 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
private KernelResult SendSyncRequest(ulong messagePtr, ulong size, int handle) private KernelResult SendSyncRequest(ulong messagePtr, ulong size, int handle)
{ {
byte[] messageData = _process.CpuMemory.ReadBytes((long)messagePtr, (long)size); byte[] messageData = new byte[size];
_process.CpuMemory.Read(messagePtr, messageData);
KClientSession clientSession = _process.HandleTable.GetObject<KClientSession>(handle); KClientSession clientSession = _process.HandleTable.GetObject<KClientSession>(handle);

View file

@ -191,14 +191,14 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
{ {
KMemoryInfo blkInfo = _process.MemoryManager.QueryMemory(position); KMemoryInfo blkInfo = _process.MemoryManager.QueryMemory(position);
_process.CpuMemory.WriteUInt64((long)infoPtr + 0x00, blkInfo.Address); _process.CpuMemory.Write(infoPtr + 0x00, blkInfo.Address);
_process.CpuMemory.WriteUInt64((long)infoPtr + 0x08, blkInfo.Size); _process.CpuMemory.Write(infoPtr + 0x08, blkInfo.Size);
_process.CpuMemory.WriteInt32 ((long)infoPtr + 0x10, (int)blkInfo.State & 0xff); _process.CpuMemory.Write(infoPtr + 0x10, (int)blkInfo.State & 0xff);
_process.CpuMemory.WriteInt32 ((long)infoPtr + 0x14, (int)blkInfo.Attribute); _process.CpuMemory.Write(infoPtr + 0x14, (int)blkInfo.Attribute);
_process.CpuMemory.WriteInt32 ((long)infoPtr + 0x18, (int)blkInfo.Permission); _process.CpuMemory.Write(infoPtr + 0x18, (int)blkInfo.Permission);
_process.CpuMemory.WriteInt32 ((long)infoPtr + 0x1c, blkInfo.IpcRefCount); _process.CpuMemory.Write(infoPtr + 0x1c, blkInfo.IpcRefCount);
_process.CpuMemory.WriteInt32 ((long)infoPtr + 0x20, blkInfo.DeviceRefCount); _process.CpuMemory.Write(infoPtr + 0x20, blkInfo.DeviceRefCount);
_process.CpuMemory.WriteInt32 ((long)infoPtr + 0x24, 0); _process.CpuMemory.Write(infoPtr + 0x24, 0);
pageInfo = 0; pageInfo = 0;

View file

@ -1,6 +1,6 @@
using ARMeilleure.Memory;
using Ryujinx.Common; using Ryujinx.Common;
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
using Ryujinx.Cpu;
using Ryujinx.HLE.Exceptions; using Ryujinx.HLE.Exceptions;
using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Ipc; using Ryujinx.HLE.HOS.Kernel.Ipc;

View file

@ -1,5 +1,5 @@
using ARMeilleure.Memory;
using ARMeilleure.State; using ARMeilleure.State;
using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Process; using Ryujinx.HLE.HOS.Kernel.Process;
using Ryujinx.HLE.HOS.Kernel.Threading; using Ryujinx.HLE.HOS.Kernel.Threading;
@ -432,79 +432,79 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
MemoryManager memory = currentProcess.CpuMemory; MemoryManager memory = currentProcess.CpuMemory;
memory.WriteUInt64((long)address + 0x0, thread.Context.GetX(0)); memory.Write(address + 0x0, thread.Context.GetX(0));
memory.WriteUInt64((long)address + 0x8, thread.Context.GetX(1)); memory.Write(address + 0x8, thread.Context.GetX(1));
memory.WriteUInt64((long)address + 0x10, thread.Context.GetX(2)); memory.Write(address + 0x10, thread.Context.GetX(2));
memory.WriteUInt64((long)address + 0x18, thread.Context.GetX(3)); memory.Write(address + 0x18, thread.Context.GetX(3));
memory.WriteUInt64((long)address + 0x20, thread.Context.GetX(4)); memory.Write(address + 0x20, thread.Context.GetX(4));
memory.WriteUInt64((long)address + 0x28, thread.Context.GetX(5)); memory.Write(address + 0x28, thread.Context.GetX(5));
memory.WriteUInt64((long)address + 0x30, thread.Context.GetX(6)); memory.Write(address + 0x30, thread.Context.GetX(6));
memory.WriteUInt64((long)address + 0x38, thread.Context.GetX(7)); memory.Write(address + 0x38, thread.Context.GetX(7));
memory.WriteUInt64((long)address + 0x40, thread.Context.GetX(8)); memory.Write(address + 0x40, thread.Context.GetX(8));
memory.WriteUInt64((long)address + 0x48, thread.Context.GetX(9)); memory.Write(address + 0x48, thread.Context.GetX(9));
memory.WriteUInt64((long)address + 0x50, thread.Context.GetX(10)); memory.Write(address + 0x50, thread.Context.GetX(10));
memory.WriteUInt64((long)address + 0x58, thread.Context.GetX(11)); memory.Write(address + 0x58, thread.Context.GetX(11));
memory.WriteUInt64((long)address + 0x60, thread.Context.GetX(12)); memory.Write(address + 0x60, thread.Context.GetX(12));
memory.WriteUInt64((long)address + 0x68, thread.Context.GetX(13)); memory.Write(address + 0x68, thread.Context.GetX(13));
memory.WriteUInt64((long)address + 0x70, thread.Context.GetX(14)); memory.Write(address + 0x70, thread.Context.GetX(14));
memory.WriteUInt64((long)address + 0x78, thread.Context.GetX(15)); memory.Write(address + 0x78, thread.Context.GetX(15));
memory.WriteUInt64((long)address + 0x80, thread.Context.GetX(16)); memory.Write(address + 0x80, thread.Context.GetX(16));
memory.WriteUInt64((long)address + 0x88, thread.Context.GetX(17)); memory.Write(address + 0x88, thread.Context.GetX(17));
memory.WriteUInt64((long)address + 0x90, thread.Context.GetX(18)); memory.Write(address + 0x90, thread.Context.GetX(18));
memory.WriteUInt64((long)address + 0x98, thread.Context.GetX(19)); memory.Write(address + 0x98, thread.Context.GetX(19));
memory.WriteUInt64((long)address + 0xa0, thread.Context.GetX(20)); memory.Write(address + 0xa0, thread.Context.GetX(20));
memory.WriteUInt64((long)address + 0xa8, thread.Context.GetX(21)); memory.Write(address + 0xa8, thread.Context.GetX(21));
memory.WriteUInt64((long)address + 0xb0, thread.Context.GetX(22)); memory.Write(address + 0xb0, thread.Context.GetX(22));
memory.WriteUInt64((long)address + 0xb8, thread.Context.GetX(23)); memory.Write(address + 0xb8, thread.Context.GetX(23));
memory.WriteUInt64((long)address + 0xc0, thread.Context.GetX(24)); memory.Write(address + 0xc0, thread.Context.GetX(24));
memory.WriteUInt64((long)address + 0xc8, thread.Context.GetX(25)); memory.Write(address + 0xc8, thread.Context.GetX(25));
memory.WriteUInt64((long)address + 0xd0, thread.Context.GetX(26)); memory.Write(address + 0xd0, thread.Context.GetX(26));
memory.WriteUInt64((long)address + 0xd8, thread.Context.GetX(27)); memory.Write(address + 0xd8, thread.Context.GetX(27));
memory.WriteUInt64((long)address + 0xe0, thread.Context.GetX(28)); memory.Write(address + 0xe0, thread.Context.GetX(28));
memory.WriteUInt64((long)address + 0xe8, thread.Context.GetX(29)); memory.Write(address + 0xe8, thread.Context.GetX(29));
memory.WriteUInt64((long)address + 0xf0, thread.Context.GetX(30)); memory.Write(address + 0xf0, thread.Context.GetX(30));
memory.WriteUInt64((long)address + 0xf8, thread.Context.GetX(31)); memory.Write(address + 0xf8, thread.Context.GetX(31));
memory.WriteInt64((long)address + 0x100, thread.LastPc); memory.Write(address + 0x100, thread.LastPc);
memory.WriteUInt64((long)address + 0x108, (ulong)GetPsr(thread.Context)); memory.Write(address + 0x108, (ulong)GetPsr(thread.Context));
memory.WriteVector128((long)address + 0x110, thread.Context.GetV(0)); memory.Write(address + 0x110, thread.Context.GetV(0));
memory.WriteVector128((long)address + 0x120, thread.Context.GetV(1)); memory.Write(address + 0x120, thread.Context.GetV(1));
memory.WriteVector128((long)address + 0x130, thread.Context.GetV(2)); memory.Write(address + 0x130, thread.Context.GetV(2));
memory.WriteVector128((long)address + 0x140, thread.Context.GetV(3)); memory.Write(address + 0x140, thread.Context.GetV(3));
memory.WriteVector128((long)address + 0x150, thread.Context.GetV(4)); memory.Write(address + 0x150, thread.Context.GetV(4));
memory.WriteVector128((long)address + 0x160, thread.Context.GetV(5)); memory.Write(address + 0x160, thread.Context.GetV(5));
memory.WriteVector128((long)address + 0x170, thread.Context.GetV(6)); memory.Write(address + 0x170, thread.Context.GetV(6));
memory.WriteVector128((long)address + 0x180, thread.Context.GetV(7)); memory.Write(address + 0x180, thread.Context.GetV(7));
memory.WriteVector128((long)address + 0x190, thread.Context.GetV(8)); memory.Write(address + 0x190, thread.Context.GetV(8));
memory.WriteVector128((long)address + 0x1a0, thread.Context.GetV(9)); memory.Write(address + 0x1a0, thread.Context.GetV(9));
memory.WriteVector128((long)address + 0x1b0, thread.Context.GetV(10)); memory.Write(address + 0x1b0, thread.Context.GetV(10));
memory.WriteVector128((long)address + 0x1c0, thread.Context.GetV(11)); memory.Write(address + 0x1c0, thread.Context.GetV(11));
memory.WriteVector128((long)address + 0x1d0, thread.Context.GetV(12)); memory.Write(address + 0x1d0, thread.Context.GetV(12));
memory.WriteVector128((long)address + 0x1e0, thread.Context.GetV(13)); memory.Write(address + 0x1e0, thread.Context.GetV(13));
memory.WriteVector128((long)address + 0x1f0, thread.Context.GetV(14)); memory.Write(address + 0x1f0, thread.Context.GetV(14));
memory.WriteVector128((long)address + 0x200, thread.Context.GetV(15)); memory.Write(address + 0x200, thread.Context.GetV(15));
memory.WriteVector128((long)address + 0x210, thread.Context.GetV(16)); memory.Write(address + 0x210, thread.Context.GetV(16));
memory.WriteVector128((long)address + 0x220, thread.Context.GetV(17)); memory.Write(address + 0x220, thread.Context.GetV(17));
memory.WriteVector128((long)address + 0x230, thread.Context.GetV(18)); memory.Write(address + 0x230, thread.Context.GetV(18));
memory.WriteVector128((long)address + 0x240, thread.Context.GetV(19)); memory.Write(address + 0x240, thread.Context.GetV(19));
memory.WriteVector128((long)address + 0x250, thread.Context.GetV(20)); memory.Write(address + 0x250, thread.Context.GetV(20));
memory.WriteVector128((long)address + 0x260, thread.Context.GetV(21)); memory.Write(address + 0x260, thread.Context.GetV(21));
memory.WriteVector128((long)address + 0x270, thread.Context.GetV(22)); memory.Write(address + 0x270, thread.Context.GetV(22));
memory.WriteVector128((long)address + 0x280, thread.Context.GetV(23)); memory.Write(address + 0x280, thread.Context.GetV(23));
memory.WriteVector128((long)address + 0x290, thread.Context.GetV(24)); memory.Write(address + 0x290, thread.Context.GetV(24));
memory.WriteVector128((long)address + 0x2a0, thread.Context.GetV(25)); memory.Write(address + 0x2a0, thread.Context.GetV(25));
memory.WriteVector128((long)address + 0x2b0, thread.Context.GetV(26)); memory.Write(address + 0x2b0, thread.Context.GetV(26));
memory.WriteVector128((long)address + 0x2c0, thread.Context.GetV(27)); memory.Write(address + 0x2c0, thread.Context.GetV(27));
memory.WriteVector128((long)address + 0x2d0, thread.Context.GetV(28)); memory.Write(address + 0x2d0, thread.Context.GetV(28));
memory.WriteVector128((long)address + 0x2e0, thread.Context.GetV(29)); memory.Write(address + 0x2e0, thread.Context.GetV(29));
memory.WriteVector128((long)address + 0x2f0, thread.Context.GetV(30)); memory.Write(address + 0x2f0, thread.Context.GetV(30));
memory.WriteVector128((long)address + 0x300, thread.Context.GetV(31)); memory.Write(address + 0x300, thread.Context.GetV(31));
memory.WriteInt32((long)address + 0x310, (int)thread.Context.Fpcr); memory.Write(address + 0x310, (int)thread.Context.Fpcr);
memory.WriteInt32((long)address + 0x314, (int)thread.Context.Fpsr); memory.Write(address + 0x314, (int)thread.Context.Fpsr);
memory.WriteInt64((long)address + 0x318, thread.Context.Tpidr); memory.Write(address + 0x318, thread.Context.Tpidr);
return KernelResult.Success; return KernelResult.Success;
} }

View file

@ -37,7 +37,7 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
for (int index = 0; index < handlesCount; index++) for (int index = 0; index < handlesCount; index++)
{ {
int handle = _process.CpuMemory.ReadInt32((long)handlesPtr + index * 4); int handle = _process.CpuMemory.Read<int>(handlesPtr + (ulong)index * 4);
KSynchronizationObject syncObj = _process.HandleTable.GetObject<KSynchronizationObject>(handle); KSynchronizationObject syncObj = _process.HandleTable.GetObject<KSynchronizationObject>(handle);

View file

@ -2,6 +2,7 @@ using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Process; using Ryujinx.HLE.HOS.Kernel.Process;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading;
namespace Ryujinx.HLE.HOS.Kernel.Threading namespace Ryujinx.HLE.HOS.Kernel.Threading
{ {
@ -228,18 +229,22 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
KProcess currentProcess = _system.Scheduler.GetCurrentProcess(); KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
if (!currentProcess.CpuMemory.IsMapped(address))
{
// Invalid address.
requester.SignaledObj = null;
requester.ObjSyncResult = KernelResult.InvalidMemState;
return null;
}
ref int mutexRef = ref currentProcess.CpuMemory.GetRef<int>(address);
int mutexValue, newMutexValue; int mutexValue, newMutexValue;
do do
{ {
if (!KernelTransfer.UserToKernelInt32(_system, address, out mutexValue)) mutexValue = mutexRef;
{
// Invalid address.
requester.SignaledObj = null;
requester.ObjSyncResult = KernelResult.InvalidMemState;
return null;
}
if (mutexValue != 0) if (mutexValue != 0)
{ {
@ -252,7 +257,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
newMutexValue = requester.ThreadHandleForUserMutex; newMutexValue = requester.ThreadHandleForUserMutex;
} }
} }
while (!currentProcess.CpuMemory.AtomicCompareExchangeInt32((long)address, mutexValue, newMutexValue)); while (Interlocked.CompareExchange(ref mutexRef, newMutexValue, mutexValue) != mutexValue);
if (mutexValue == 0) if (mutexValue == 0)
{ {
@ -389,7 +394,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
if (shouldDecrement) if (shouldDecrement)
{ {
currentValue = currentProcess.CpuMemory.AtomicDecrementInt32((long)address) + 1; currentValue = Interlocked.Decrement(ref currentProcess.CpuMemory.GetRef<int>(address)) + 1;
} }
if (currentValue < value) if (currentValue < value)
@ -480,16 +485,20 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
KProcess currentProcess = _system.Scheduler.GetCurrentProcess(); KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
if (!currentProcess.CpuMemory.IsMapped(address))
{
_system.CriticalSection.Leave();
return KernelResult.InvalidMemState;
}
ref int valueRef = ref currentProcess.CpuMemory.GetRef<int>(address);
int currentValue; int currentValue;
do do
{ {
if (!KernelTransfer.UserToKernelInt32(_system, address, out currentValue)) currentValue = valueRef;
{
_system.CriticalSection.Leave();
return KernelResult.InvalidMemState;
}
if (currentValue != value) if (currentValue != value)
{ {
@ -498,7 +507,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
return KernelResult.InvalidState; return KernelResult.InvalidState;
} }
} }
while (!currentProcess.CpuMemory.AtomicCompareExchangeInt32((long)address, currentValue, currentValue + 1)); while (Interlocked.CompareExchange(ref valueRef, currentValue + 1, currentValue) != currentValue);
WakeArbiterThreads(address, count); WakeArbiterThreads(address, count);
@ -537,16 +546,20 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
KProcess currentProcess = _system.Scheduler.GetCurrentProcess(); KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
if (!currentProcess.CpuMemory.IsMapped(address))
{
_system.CriticalSection.Leave();
return KernelResult.InvalidMemState;
}
ref int valueRef = ref currentProcess.CpuMemory.GetRef<int>(address);
int currentValue; int currentValue;
do do
{ {
if (!KernelTransfer.UserToKernelInt32(_system, address, out currentValue)) currentValue = valueRef;
{
_system.CriticalSection.Leave();
return KernelResult.InvalidMemState;
}
if (currentValue != value) if (currentValue != value)
{ {
@ -555,7 +568,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
return KernelResult.InvalidState; return KernelResult.InvalidState;
} }
} }
while (!currentProcess.CpuMemory.AtomicCompareExchangeInt32((long)address, currentValue, currentValue + offset)); while (Interlocked.CompareExchange(ref valueRef, currentValue + offset, currentValue) != currentValue);
WakeArbiterThreads(address, count); WakeArbiterThreads(address, count);

View file

@ -1,5 +1,5 @@
using ARMeilleure.Memory;
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Process; using Ryujinx.HLE.HOS.Kernel.Process;
using System; using System;
@ -163,7 +163,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
HostThread = new Thread(customHostThreadStart ?? (() => ThreadStart(entrypoint))); HostThread = new Thread(customHostThreadStart ?? (() => ThreadStart(entrypoint)));
Context = new ARMeilleure.State.ExecutionContext(); Context = CpuContext.CreateExecutionContext();
bool isAarch32 = (Owner.MmuFlags & 1) == 0; bool isAarch32 = (Owner.MmuFlags & 1) == 0;
@ -1141,7 +1141,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading
private void ThreadStart(ulong entrypoint) private void ThreadStart(ulong entrypoint)
{ {
Owner.Translator.Execute(Context, entrypoint); Owner.CpuContext.Execute(Context, entrypoint);
ThreadExit(); ThreadExit();

View file

@ -1,7 +1,6 @@
using ARMeilleure.Memory;
using LibHac;
using Ryujinx.Common; using Ryujinx.Common;
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Memory; using Ryujinx.HLE.HOS.Kernel.Memory;
using Ryujinx.HLE.HOS.Kernel.Process; using Ryujinx.HLE.HOS.Kernel.Process;
@ -270,9 +269,9 @@ namespace Ryujinx.HLE.HOS
end = bssStart + (ulong)image.BssSize; end = bssStart + (ulong)image.BssSize;
} }
process.CpuMemory.WriteBytes((long)textStart, image.Text); process.CpuMemory.Write(textStart, image.Text);
process.CpuMemory.WriteBytes((long)roStart, image.Ro); process.CpuMemory.Write(roStart, image.Ro);
process.CpuMemory.WriteBytes((long)dataStart, image.Data); process.CpuMemory.Write(dataStart, image.Data);
MemoryHelper.FillWithZeros(process.CpuMemory, (long)bssStart, image.BssSize); MemoryHelper.FillWithZeros(process.CpuMemory, (long)bssStart, image.BssSize);

View file

@ -1,4 +1,4 @@
using ARMeilleure.Memory; using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Ipc; using Ryujinx.HLE.HOS.Ipc;
using Ryujinx.HLE.HOS.Kernel.Ipc; using Ryujinx.HLE.HOS.Kernel.Ipc;
using Ryujinx.HLE.HOS.Kernel.Process; using Ryujinx.HLE.HOS.Kernel.Process;

View file

@ -1,6 +1,6 @@
using ARMeilleure.Memory;
using Ryujinx.Common; using Ryujinx.Common;
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Services.Arp; using Ryujinx.HLE.HOS.Services.Arp;
using System.Collections.Generic; using System.Collections.Generic;
@ -75,8 +75,8 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc
break; break;
} }
context.Memory.WriteInt64(outputPosition + (long)offset, userProfile.UserId.High); context.Memory.Write((ulong)outputPosition + offset, userProfile.UserId.High);
context.Memory.WriteInt64(outputPosition + (long)offset + 8, userProfile.UserId.Low); context.Memory.Write((ulong)outputPosition + offset + 8, userProfile.UserId.Low);
offset += 0x10; offset += 0x10;
} }
@ -240,7 +240,9 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc
return ResultCode.InvalidInputBufferSize; return ResultCode.InvalidInputBufferSize;
} }
byte[] thumbnailBuffer = context.Memory.ReadBytes(inputPosition, inputSize); byte[] thumbnailBuffer = new byte[inputSize];
context.Memory.Read((ulong)inputPosition, thumbnailBuffer);
// TODO: Store thumbnailBuffer somewhere, in save data 0x8000000000000010 ? // TODO: Store thumbnailBuffer somewhere, in save data 0x8000000000000010 ?

View file

@ -1,5 +1,5 @@
using ARMeilleure.Memory;
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
using Ryujinx.Cpu;
using Ryujinx.HLE.Utilities; using Ryujinx.HLE.Utilities;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
@ -28,9 +28,9 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc
MemoryHelper.FillWithZeros(context.Memory, position, 0x80); MemoryHelper.FillWithZeros(context.Memory, position, 0x80);
context.Memory.WriteInt32(position, 0); context.Memory.Write((ulong)position, 0);
context.Memory.WriteInt32(position + 4, 1); context.Memory.Write((ulong)position + 4, 1);
context.Memory.WriteInt64(position + 8, 1); context.Memory.Write((ulong)position + 8, 1L);
return GetBase(context); return GetBase(context);
} }
@ -70,7 +70,7 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc
_profilePictureStream.Read(profilePictureData, 0, profilePictureData.Length); _profilePictureStream.Read(profilePictureData, 0, profilePictureData.Length);
context.Memory.WriteBytes(bufferPosition, profilePictureData); context.Memory.Write((ulong)bufferPosition, profilePictureData);
context.ResponseData.Write(_profilePictureStream.Length); context.ResponseData.Write(_profilePictureStream.Length);

View file

@ -44,7 +44,9 @@ namespace Ryujinx.HLE.HOS.Services.Am.AppletAE
size = maxSize; size = maxSize;
} }
byte[] data = context.Memory.ReadBytes(position, size); byte[] data = new byte[size];
context.Memory.Read((ulong)position, data);
Buffer.BlockCopy(data, 0, _storage.Data, (int)writePosition, (int)size); Buffer.BlockCopy(data, 0, _storage.Data, (int)writePosition, (int)size);
} }
@ -71,7 +73,7 @@ namespace Ryujinx.HLE.HOS.Services.Am.AppletAE
Buffer.BlockCopy(_storage.Data, (int)readPosition, data, 0, (int)size); Buffer.BlockCopy(_storage.Data, (int)readPosition, data, 0, (int)size);
context.Memory.WriteBytes(position, data); context.Memory.Write((ulong)position, data);
return ResultCode.Success; return ResultCode.Success;
} }

View file

@ -1,5 +1,5 @@
using ARMeilleure.Memory;
using Ryujinx.Audio; using Ryujinx.Audio;
using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Ipc; using Ryujinx.HLE.HOS.Ipc;
using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Threading; using Ryujinx.HLE.HOS.Kernel.Threading;
@ -106,9 +106,9 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioOutManager
context.Memory, context.Memory,
position); position);
byte[] buffer = context.Memory.ReadBytes( byte[] buffer = new byte[data.SampleBufferSize];
data.SampleBufferPtr,
data.SampleBufferSize); context.Memory.Read((ulong)data.SampleBufferPtr, buffer);
_audioOut.AppendBuffer(_track, tag, buffer); _audioOut.AppendBuffer(_track, tag, buffer);
@ -139,7 +139,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioOutManager
tag = releasedBuffers[index]; tag = releasedBuffers[index];
} }
context.Memory.WriteInt64(position + index * 8, tag); context.Memory.Write((ulong)(position + index * 8), tag);
} }
context.ResponseData.Write(releasedBuffers.Length); context.ResponseData.Write(releasedBuffers.Length);

View file

@ -44,7 +44,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager
break; break;
} }
context.Memory.WriteBytes(position, buffer); context.Memory.Write((ulong)position, buffer);
position += buffer.Length; position += buffer.Length;
} }
@ -61,7 +61,9 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager
long position = context.Request.SendBuff[0].Position; long position = context.Request.SendBuff[0].Position;
long size = context.Request.SendBuff[0].Size; long size = context.Request.SendBuff[0].Size;
byte[] deviceNameBuffer = context.Memory.ReadBytes(position, size); byte[] deviceNameBuffer = new byte[size];
context.Memory.Read((ulong)position, deviceNameBuffer);
string deviceName = Encoding.ASCII.GetString(deviceNameBuffer); string deviceName = Encoding.ASCII.GetString(deviceNameBuffer);
@ -83,7 +85,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager
if ((ulong)deviceNameBuffer.Length <= (ulong)size) if ((ulong)deviceNameBuffer.Length <= (ulong)size)
{ {
context.Memory.WriteBytes(position, deviceNameBuffer); context.Memory.Write((ulong)position, deviceNameBuffer);
} }
else else
{ {
@ -143,7 +145,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager
break; break;
} }
context.Memory.WriteBytes(position, buffer); context.Memory.Write((ulong)position, buffer);
position += buffer.Length; position += buffer.Length;
} }
@ -159,7 +161,9 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager
(long position, long size) = context.Request.GetBufferType0x21(); (long position, long size) = context.Request.GetBufferType0x21();
byte[] deviceNameBuffer = context.Memory.ReadBytes(position, size); byte[] deviceNameBuffer = new byte[size];
context.Memory.Read((ulong)position, deviceNameBuffer);
string deviceName = Encoding.UTF8.GetString(deviceNameBuffer); string deviceName = Encoding.UTF8.GetString(deviceNameBuffer);
@ -191,7 +195,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager
if ((ulong)deviceNameBuffer.Length <= (ulong)size) if ((ulong)deviceNameBuffer.Length <= (ulong)size)
{ {
context.Memory.WriteBytes(position, deviceNameBuffer); context.Memory.Write((ulong)position, deviceNameBuffer);
} }
else else
{ {

View file

@ -1,7 +1,7 @@
using ARMeilleure.Memory;
using Ryujinx.Audio; using Ryujinx.Audio;
using Ryujinx.Audio.Adpcm; using Ryujinx.Audio.Adpcm;
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Ipc; using Ryujinx.HLE.HOS.Ipc;
using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Threading; using Ryujinx.HLE.HOS.Kernel.Threading;
@ -333,7 +333,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager
for (int offset = 0; offset < size; offset += 2) for (int offset = 0; offset < size; offset += 2)
{ {
context.Coefficients[offset >> 1] = _memory.ReadInt16(position + offset); context.Coefficients[offset >> 1] = _memory.Read<short>((ulong)(position + offset));
} }
return context; return context;

View file

@ -1,5 +1,5 @@
using ARMeilleure.Memory;
using Ryujinx.Audio.Adpcm; using Ryujinx.Audio.Adpcm;
using Ryujinx.Cpu;
using System; using System;
namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager
@ -146,7 +146,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager
{ {
for (int index = 0; index < samplesCount; index++) for (int index = 0; index < samplesCount; index++)
{ {
short sample = memory.ReadInt16(wb.Position + index * 2); short sample = memory.Read<short>((ulong)(wb.Position + index * 2));
_samples[index * 2 + 0] = sample; _samples[index * 2 + 0] = sample;
_samples[index * 2 + 1] = sample; _samples[index * 2 + 1] = sample;
@ -156,13 +156,15 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager
{ {
for (int index = 0; index < samplesCount * 2; index++) for (int index = 0; index < samplesCount * 2; index++)
{ {
_samples[index] = memory.ReadInt16(wb.Position + index * 2); _samples[index] = memory.Read<short>((ulong)(wb.Position + index * 2));
} }
} }
} }
else if (SampleFormat == SampleFormat.Adpcm) else if (SampleFormat == SampleFormat.Adpcm)
{ {
byte[] buffer = memory.ReadBytes(wb.Position, wb.Size); byte[] buffer = new byte[wb.Size];
memory.Read((ulong)wb.Position, buffer);
_samples = AdpcmDecoder.Decode(buffer, AdpcmCtx); _samples = AdpcmDecoder.Decode(buffer, AdpcmCtx);
} }

View file

@ -111,7 +111,11 @@ namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
long outputPosition = context.Request.ReceiveBuff[0].Position; long outputPosition = context.Request.ReceiveBuff[0].Position;
long outputSize = context.Request.ReceiveBuff[0].Size; long outputSize = context.Request.ReceiveBuff[0].Size;
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(inPosition, inSize)))) byte[] buffer = new byte[inSize];
context.Memory.Read((ulong)inPosition, buffer);
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
{ {
result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples); result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples);
@ -119,7 +123,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
{ {
byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)]; byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length); Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
context.Memory.WriteBytes(outputPosition, pcmDataBytes); context.Memory.Write((ulong)outputPosition, pcmDataBytes);
context.ResponseData.Write(outConsumed); context.ResponseData.Write(outConsumed);
context.ResponseData.Write(outSamples); context.ResponseData.Write(outSamples);
@ -140,7 +144,11 @@ namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
long outputPosition = context.Request.ReceiveBuff[0].Position; long outputPosition = context.Request.ReceiveBuff[0].Position;
long outputSize = context.Request.ReceiveBuff[0].Size; long outputSize = context.Request.ReceiveBuff[0].Size;
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(inPosition, inSize)))) byte[] buffer = new byte[inSize];
context.Memory.Read((ulong)inPosition, buffer);
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
{ {
result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples); result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples);
@ -148,7 +156,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
{ {
byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)]; byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length); Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
context.Memory.WriteBytes(outputPosition, pcmDataBytes); context.Memory.Write((ulong)outputPosition, pcmDataBytes);
context.ResponseData.Write(outConsumed); context.ResponseData.Write(outConsumed);
context.ResponseData.Write(outSamples); context.ResponseData.Write(outSamples);
@ -174,7 +182,11 @@ namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
long outputPosition = context.Request.ReceiveBuff[0].Position; long outputPosition = context.Request.ReceiveBuff[0].Position;
long outputSize = context.Request.ReceiveBuff[0].Size; long outputSize = context.Request.ReceiveBuff[0].Size;
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(inPosition, inSize)))) byte[] buffer = new byte[inSize];
context.Memory.Read((ulong)inPosition, buffer);
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
{ {
result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples); result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples);
@ -182,7 +194,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
{ {
byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)]; byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length); Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
context.Memory.WriteBytes(outputPosition, pcmDataBytes); context.Memory.Write((ulong)outputPosition, pcmDataBytes);
context.ResponseData.Write(outConsumed); context.ResponseData.Write(outConsumed);
context.ResponseData.Write(outSamples); context.ResponseData.Write(outSamples);
@ -208,7 +220,11 @@ namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
long outputPosition = context.Request.ReceiveBuff[0].Position; long outputPosition = context.Request.ReceiveBuff[0].Position;
long outputSize = context.Request.ReceiveBuff[0].Size; long outputSize = context.Request.ReceiveBuff[0].Size;
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(inPosition, inSize)))) byte[] buffer = new byte[inSize];
context.Memory.Read((ulong)inPosition, buffer);
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
{ {
result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples); result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples);
@ -216,7 +232,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
{ {
byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)]; byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length); Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
context.Memory.WriteBytes(outputPosition, pcmDataBytes); context.Memory.Write((ulong)outputPosition, pcmDataBytes);
context.ResponseData.Write(outConsumed); context.ResponseData.Write(outConsumed);
context.ResponseData.Write(outSamples); context.ResponseData.Write(outSamples);

View file

@ -1,6 +1,6 @@
using ARMeilleure.Memory;
using Ryujinx.Audio; using Ryujinx.Audio;
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Kernel.Threading; using Ryujinx.HLE.HOS.Kernel.Threading;
using Ryujinx.HLE.HOS.Services.Audio.AudioOutManager; using Ryujinx.HLE.HOS.Services.Audio.AudioOutManager;
using System.Text; using System.Text;
@ -72,7 +72,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio
if ((ulong)deviceNameBuffer.Length <= (ulong)size) if ((ulong)deviceNameBuffer.Length <= (ulong)size)
{ {
context.Memory.WriteBytes(position, deviceNameBuffer); context.Memory.Write((ulong)position, deviceNameBuffer);
nameCount++; nameCount++;
} }
@ -109,7 +109,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio
if ((ulong)deviceNameBuffer.Length <= (ulong)receiveSize) if ((ulong)deviceNameBuffer.Length <= (ulong)receiveSize)
{ {
context.Memory.WriteBytes(receivePosition, deviceNameBuffer); context.Memory.Write((ulong)receivePosition, deviceNameBuffer);
} }
else else
{ {

View file

@ -37,7 +37,7 @@ namespace Ryujinx.HLE.HOS.Services.Bcat.ServiceCreator
Result result = _base.Read(out int entriesRead, MemoryMarshal.Cast<byte, DeliveryCacheDirectoryEntry>(data)); Result result = _base.Read(out int entriesRead, MemoryMarshal.Cast<byte, DeliveryCacheDirectoryEntry>(data));
context.Memory.WriteBytes(position, data); context.Memory.Write((ulong)position, data);
context.ResponseData.Write(entriesRead); context.ResponseData.Write(entriesRead);

View file

@ -39,7 +39,7 @@ namespace Ryujinx.HLE.HOS.Services.Bcat.ServiceCreator
Result result = _base.Read(out long bytesRead, offset, data); Result result = _base.Read(out long bytesRead, offset, data);
context.Memory.WriteBytes(position, data); context.Memory.Write((ulong)position, data);
context.ResponseData.Write(bytesRead); context.ResponseData.Write(bytesRead);

View file

@ -57,7 +57,7 @@ namespace Ryujinx.HLE.HOS.Services.Bcat.ServiceCreator
using (BinaryWriter bufferWriter = new BinaryWriter(memory)) using (BinaryWriter bufferWriter = new BinaryWriter(memory))
{ {
bufferWriter.WriteStruct(deliveryCacheProgress); bufferWriter.WriteStruct(deliveryCacheProgress);
context.Memory.WriteBytes(ipcDesc.Position, memory.ToArray()); context.Memory.Write((ulong)ipcDesc.Position, memory.ToArray());
} }
} }
} }

View file

@ -53,7 +53,7 @@ namespace Ryujinx.HLE.HOS.Services.Bcat.ServiceCreator
Result result = _base.EnumerateDeliveryCacheDirectory(out int count, MemoryMarshal.Cast<byte, DirectoryName>(data)); Result result = _base.EnumerateDeliveryCacheDirectory(out int count, MemoryMarshal.Cast<byte, DirectoryName>(data));
context.Memory.WriteBytes(position, data); context.Memory.Write((ulong)position, data);
context.ResponseData.Write(count); context.ResponseData.Write(count);

View file

@ -166,7 +166,9 @@ namespace Ryujinx.HLE.HOS.Services.Friend.ServiceCreator
long position = context.Request.PtrBuff[0].Position; long position = context.Request.PtrBuff[0].Position;
long size = context.Request.PtrBuff[0].Size; long size = context.Request.PtrBuff[0].Size;
byte[] bufferContent = context.Memory.ReadBytes(position, size); byte[] bufferContent = new byte[size];
context.Memory.Read((ulong)position, bufferContent);
if (uuid.IsNull) if (uuid.IsNull)
{ {

View file

@ -119,7 +119,9 @@ namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
long position = context.Request.PtrBuff[index].Position; long position = context.Request.PtrBuff[index].Position;
long size = context.Request.PtrBuff[index].Size; long size = context.Request.PtrBuff[index].Size;
byte[] pathBytes = context.Memory.ReadBytes(position, size); byte[] pathBytes = new byte[size];
context.Memory.Read((ulong)position, pathBytes);
return FsPath.FromSpan(out path, pathBytes); return FsPath.FromSpan(out path, pathBytes);
} }

View file

@ -26,7 +26,7 @@ namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
Result result = _baseDirectory.Read(out long entriesRead, entries); Result result = _baseDirectory.Read(out long entriesRead, entries);
context.Memory.WriteBytes(bufferPosition, entriesBytes); context.Memory.Write((ulong)bufferPosition, entriesBytes);
context.ResponseData.Write(entriesRead); context.ResponseData.Write(entriesRead);
return (ResultCode)result.Value; return (ResultCode)result.Value;

View file

@ -29,7 +29,7 @@ namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
Result result = _baseFile.Read(out long bytesRead, offset, data, readOption); Result result = _baseFile.Read(out long bytesRead, offset, data, readOption);
context.Memory.WriteBytes(position, data); context.Memory.Write((ulong)position, data);
context.ResponseData.Write(bytesRead); context.ResponseData.Write(bytesRead);
@ -48,7 +48,9 @@ namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
long offset = context.RequestData.ReadInt64(); long offset = context.RequestData.ReadInt64();
long size = context.RequestData.ReadInt64(); long size = context.RequestData.ReadInt64();
byte[] data = context.Memory.ReadBytes(position, size); byte[] data = new byte[size];
context.Memory.Read((ulong)position, data);
return (ResultCode)_baseFile.Write(offset, data, writeOption).Value; return (ResultCode)_baseFile.Write(offset, data, writeOption).Value;
} }

View file

@ -34,7 +34,7 @@ namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
Result result = _baseStorage.Read(offset, data); Result result = _baseStorage.Read(offset, data);
context.Memory.WriteBytes(buffDesc.Position, data); context.Memory.Write((ulong)buffDesc.Position, data);
return (ResultCode)result.Value; return (ResultCode)result.Value;
} }

View file

@ -333,7 +333,7 @@ namespace Ryujinx.HLE.HOS.Services.Fs
Result result = _baseFileSystemProxy.FindSaveDataWithFilter(out long count, infoBuffer, spaceId, ref filter); Result result = _baseFileSystemProxy.FindSaveDataWithFilter(out long count, infoBuffer, spaceId, ref filter);
context.Memory.WriteBytes(bufferPosition, infoBuffer); context.Memory.Write((ulong)bufferPosition, infoBuffer);
context.ResponseData.Write(count); context.ResponseData.Write(count);
return (ResultCode)result.Value; return (ResultCode)result.Value;

View file

@ -22,7 +22,7 @@ namespace Ryujinx.HLE.HOS.Services.Fs
Result result = _baseReader.ReadSaveDataInfo(out long readCount, infoBuffer); Result result = _baseReader.ReadSaveDataInfo(out long readCount, infoBuffer);
context.Memory.WriteBytes(bufferPosition, infoBuffer); context.Memory.Write((ulong)bufferPosition, infoBuffer);
context.ResponseData.Write(readCount); context.ResponseData.Write(readCount);
return (ResultCode)result.Value; return (ResultCode)result.Value;

View file

@ -7,9 +7,11 @@ namespace Ryujinx.HLE.HOS.Services.Hid
public class Hid public class Hid
{ {
private readonly Switch _device; private readonly Switch _device;
private readonly long _hidMemoryAddress;
internal ref HidSharedMemory SharedMemory => ref _device.Memory.GetStructRef<HidSharedMemory>(_hidMemoryAddress); private readonly ulong _hidMemoryAddress;
internal ref HidSharedMemory SharedMemory => ref _device.Memory.GetRef<HidSharedMemory>(_hidMemoryAddress);
internal const int SharedMemEntryCount = 17; internal const int SharedMemEntryCount = 17;
public DebugPadDevice DebugPad; public DebugPadDevice DebugPad;
@ -46,12 +48,12 @@ namespace Ryujinx.HLE.HOS.Services.Hid
} }
} }
public Hid(in Switch device, long sharedHidMemoryAddress) public Hid(in Switch device, ulong sharedHidMemoryAddress)
{ {
_device = device; _device = device;
_hidMemoryAddress = sharedHidMemoryAddress; _hidMemoryAddress = sharedHidMemoryAddress;
device.Memory.FillWithZeros(sharedHidMemoryAddress, Horizon.HidSize); device.Memory.ZeroFill(sharedHidMemoryAddress, Horizon.HidSize);
} }
public void InitDevices() public void InitDevices()

View file

@ -580,7 +580,7 @@ namespace Ryujinx.HLE.HOS.Services.Hid
for (int i = 0; i < arraySize; ++i) for (int i = 0; i < arraySize; ++i)
{ {
supportedPlayerIds[i] = (NpadIdType)context.Memory.ReadInt32(context.Request.PtrBuff[0].Position + i * 4); supportedPlayerIds[i] = context.Memory.Read<NpadIdType>((ulong)(context.Request.PtrBuff[0].Position + i * 4));
} }
Logger.PrintStub(LogClass.ServiceHid, $"{arraySize} " + string.Join(",", supportedPlayerIds)); Logger.PrintStub(LogClass.ServiceHid, $"{arraySize} " + string.Join(",", supportedPlayerIds));
@ -980,13 +980,13 @@ namespace Ryujinx.HLE.HOS.Services.Hid
{ {
long appletResourceUserId = context.RequestData.ReadInt64(); long appletResourceUserId = context.RequestData.ReadInt64();
byte[] vibrationDeviceHandleBuffer = context.Memory.ReadBytes( byte[] vibrationDeviceHandleBuffer = new byte[context.Request.PtrBuff[0].Size];
context.Request.PtrBuff[0].Position,
context.Request.PtrBuff[0].Size);
byte[] vibrationValueBuffer = context.Memory.ReadBytes( context.Memory.Read((ulong)context.Request.PtrBuff[0].Position, vibrationDeviceHandleBuffer);
context.Request.PtrBuff[1].Position,
context.Request.PtrBuff[1].Size); byte[] vibrationValueBuffer = new byte[context.Request.PtrBuff[1].Size];
context.Memory.Read((ulong)context.Request.PtrBuff[1].Position, vibrationValueBuffer);
// TODO: Read all handles and values from buffer. // TODO: Read all handles and values from buffer.

View file

@ -33,7 +33,10 @@ namespace Ryujinx.HLE.HOS.Services.Lm.LogService
public ResultCode Log(ServiceCtx context) public ResultCode Log(ServiceCtx context)
{ {
(long bufPos, long bufSize) = context.Request.GetBufferType0x21(); (long bufPos, long bufSize) = context.Request.GetBufferType0x21();
byte[] logBuffer = context.Memory.ReadBytes(bufPos, bufSize);
byte[] logBuffer = new byte[bufSize];
context.Memory.Read((ulong)bufPos, logBuffer);
using (MemoryStream ms = new MemoryStream(logBuffer)) using (MemoryStream ms = new MemoryStream(logBuffer))
{ {

View file

@ -261,7 +261,7 @@ namespace Ryujinx.HLE.HOS.Services.Mii.StaticService
ResultCode result = Export(data); ResultCode result = Export(data);
context.Memory.WriteBytes(outputBuffer.Position, data.ToArray()); context.Memory.Write((ulong)outputBuffer.Position, data.ToArray());
return result; return result;
} }
@ -350,7 +350,9 @@ namespace Ryujinx.HLE.HOS.Services.Mii.StaticService
} }
else else
{ {
rawData = context.Memory.ReadBytes(ipcBuff.Position, ipcBuff.Size); rawData = new byte[ipcBuff.Size];
context.Memory.Read((ulong)ipcBuff.Position, rawData);
} }
return new Span<byte>(rawData); return new Span<byte>(rawData);
@ -365,7 +367,7 @@ namespace Ryujinx.HLE.HOS.Services.Mii.StaticService
{ {
Span<byte> rawData = MemoryMarshal.Cast<T, byte>(span); Span<byte> rawData = MemoryMarshal.Cast<T, byte>(span);
context.Memory.WriteBytes(ipcBuff.Position, rawData.ToArray()); context.Memory.Write((ulong)ipcBuff.Position, rawData);
} }
protected abstract bool IsUpdated(SourceFlag flag); protected abstract bool IsUpdated(SourceFlag flag);

View file

@ -233,7 +233,7 @@ namespace Ryujinx.HLE.HOS.Services.Ncm.Lr.LocationResolverManager
byte[] contentPathBuffer = Encoding.UTF8.GetBytes(contentPath); byte[] contentPathBuffer = Encoding.UTF8.GetBytes(contentPath);
context.Memory.WriteBytes(position, contentPathBuffer); context.Memory.Write((ulong)position, contentPathBuffer);
} }
else else
{ {

View file

@ -31,7 +31,9 @@ namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp
long inputPosition = context.Request.SendBuff[0].Position; long inputPosition = context.Request.SendBuff[0].Position;
long inputSize = context.Request.SendBuff[0].Size; long inputSize = context.Request.SendBuff[0].Size;
byte[] unknownBuffer = context.Memory.ReadBytes(inputPosition, inputSize); byte[] unknownBuffer = new byte[inputSize];
context.Memory.Read((ulong)inputPosition, unknownBuffer);
// NOTE: appletResourceUserId, mcuVersionData and the buffer are stored inside an internal struct. // NOTE: appletResourceUserId, mcuVersionData and the buffer are stored inside an internal struct.
// The buffer seems to contains entries with a size of 0x40 bytes each. // The buffer seems to contains entries with a size of 0x40 bytes each.
@ -89,7 +91,7 @@ namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp
for (int i = 0; i < _devices.Count; i++) for (int i = 0; i < _devices.Count; i++)
{ {
context.Memory.WriteUInt32(outputPosition + (i * sizeof(long)), (uint)_devices[i].Handle); context.Memory.Write((ulong)(outputPosition + (i * sizeof(long))), (uint)_devices[i].Handle);
} }
context.ResponseData.Write(_devices.Count); context.ResponseData.Write(_devices.Count);

View file

@ -34,7 +34,7 @@ namespace Ryujinx.HLE.HOS.Services.Nifm.StaticService
long position = context.Request.RecvListBuff[0].Position; long position = context.Request.RecvListBuff[0].Position;
long size = context.Request.RecvListBuff[0].Size; long size = context.Request.RecvListBuff[0].Size;
context.Memory.WriteInt32(position, _generalServiceDetail.ClientId); context.Memory.Write((ulong)position, _generalServiceDetail.ClientId);
return ResultCode.Success; return ResultCode.Success;
} }
@ -120,7 +120,7 @@ namespace Ryujinx.HLE.HOS.Services.Nifm.StaticService
long position = context.Request.PtrBuff[0].Position; long position = context.Request.PtrBuff[0].Position;
long size = context.Request.PtrBuff[0].Size; long size = context.Request.PtrBuff[0].Size;
int clientId = context.Memory.ReadInt32(position); int clientId = context.Memory.Read<int>((ulong)position);
context.ResponseData.Write(GeneralServiceManager.Get(clientId).IsAnyInternetRequestAccepted); context.ResponseData.Write(GeneralServiceManager.Get(clientId).IsAnyInternetRequestAccepted);

View file

@ -16,7 +16,7 @@
byte[] nacpData = context.Device.System.ControlData.ByteSpan.ToArray(); byte[] nacpData = context.Device.System.ControlData.ByteSpan.ToArray();
context.Memory.WriteBytes(position, nacpData); context.Memory.Write((ulong)position, nacpData);
return ResultCode.Success; return ResultCode.Success;
} }

View file

@ -15,7 +15,7 @@
byte[] nacpData = context.Device.System.ControlData.ByteSpan.ToArray(); byte[] nacpData = context.Device.System.ControlData.ByteSpan.ToArray();
context.Memory.WriteBytes(position, nacpData); context.Memory.Write((ulong)position, nacpData);
return ResultCode.Success; return ResultCode.Success;
} }

View file

@ -1,6 +1,6 @@
using ARMeilleure.Memory;
using Ryujinx.Common; using Ryujinx.Common;
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
using Ryujinx.Cpu;
using Ryujinx.HLE.Exceptions; using Ryujinx.HLE.Exceptions;
using Ryujinx.HLE.HOS.Ipc; using Ryujinx.HLE.HOS.Ipc;
using Ryujinx.HLE.HOS.Kernel.Memory; using Ryujinx.HLE.HOS.Kernel.Memory;
@ -102,7 +102,9 @@ namespace Ryujinx.HLE.HOS.Services.Nv
byte[] outputData = new byte[outputDataSize]; byte[] outputData = new byte[outputDataSize];
byte[] temp = context.Memory.ReadBytes(inputDataPosition, inputDataSize); byte[] temp = new byte[inputDataSize];
context.Memory.Read((ulong)inputDataPosition, temp);
Buffer.BlockCopy(temp, 0, outputData, 0, temp.Length); Buffer.BlockCopy(temp, 0, outputData, 0, temp.Length);
@ -116,7 +118,11 @@ namespace Ryujinx.HLE.HOS.Services.Nv
} }
else else
{ {
arguments = new Span<byte>(context.Memory.ReadBytes(inputDataPosition, inputDataSize)); byte[] temp = new byte[inputDataSize];
context.Memory.Read((ulong)inputDataPosition, temp);
arguments = new Span<byte>(temp);
} }
return NvResult.Success; return NvResult.Success;
@ -266,7 +272,7 @@ namespace Ryujinx.HLE.HOS.Services.Nv
if ((ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0) if ((ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0)
{ {
context.Memory.WriteBytes(context.Request.GetBufferType0x22(0).Position, arguments.ToArray()); context.Memory.Write((ulong)context.Request.GetBufferType0x22(0).Position, arguments.ToArray());
} }
} }
} }
@ -435,7 +441,11 @@ namespace Ryujinx.HLE.HOS.Services.Nv
errorCode = GetIoctlArgument(context, ioctlCommand, out Span<byte> arguments); errorCode = GetIoctlArgument(context, ioctlCommand, out Span<byte> arguments);
Span<byte> inlineInBuffer = new Span<byte>(context.Memory.ReadBytes(inlineInBufferPosition, inlineInBufferSize)); byte[] temp = new byte[inlineInBufferSize];
context.Memory.Read((ulong)inlineInBufferPosition, temp);
Span<byte> inlineInBuffer = new Span<byte>(temp);
if (errorCode == NvResult.Success) if (errorCode == NvResult.Success)
{ {
@ -454,7 +464,7 @@ namespace Ryujinx.HLE.HOS.Services.Nv
if ((ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0) if ((ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0)
{ {
context.Memory.WriteBytes(context.Request.GetBufferType0x22(0).Position, arguments.ToArray()); context.Memory.Write((ulong)context.Request.GetBufferType0x22(0).Position, arguments.ToArray());
} }
} }
} }
@ -480,7 +490,11 @@ namespace Ryujinx.HLE.HOS.Services.Nv
errorCode = GetIoctlArgument(context, ioctlCommand, out Span<byte> arguments); errorCode = GetIoctlArgument(context, ioctlCommand, out Span<byte> arguments);
Span<byte> inlineOutBuffer = new Span<byte>(context.Memory.ReadBytes(inlineOutBufferPosition, inlineOutBufferSize)); byte[] temp = new byte[inlineOutBufferSize];
context.Memory.Read((ulong)inlineOutBufferPosition, temp);
Span<byte> inlineOutBuffer = new Span<byte>(temp);
if (errorCode == NvResult.Success) if (errorCode == NvResult.Success)
{ {
@ -499,8 +513,8 @@ namespace Ryujinx.HLE.HOS.Services.Nv
if ((ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0) if ((ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0)
{ {
context.Memory.WriteBytes(context.Request.GetBufferType0x22(0).Position, arguments.ToArray()); context.Memory.Write((ulong)context.Request.GetBufferType0x22(0).Position, arguments.ToArray());
context.Memory.WriteBytes(inlineOutBufferPosition, inlineOutBuffer.ToArray()); context.Memory.Write((ulong)inlineOutBufferPosition, inlineOutBuffer.ToArray());
} }
} }
} }

View file

@ -21,7 +21,7 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel
private Switch _device; private Switch _device;
private ARMeilleure.Memory.MemoryManager _memory; private Cpu.MemoryManager _memory;
public enum ResourcePolicy public enum ResourcePolicy
{ {
@ -143,7 +143,7 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel
for (int offset = 0; offset < commandBufferData.Length; offset++) for (int offset = 0; offset < commandBufferData.Length; offset++)
{ {
commandBufferData[offset] = _memory.ReadInt32(map.Address + commandBufferEntry.Offset + offset * 4); commandBufferData[offset] = _memory.Read<int>((ulong)(map.Address + commandBufferEntry.Offset + offset * 4));
} }
// TODO: Submit command to engines. // TODO: Submit command to engines.

View file

@ -95,7 +95,9 @@ namespace Ryujinx.HLE.HOS.Services.Prepo
return ResultCode.InvalidBufferSize; return ResultCode.InvalidBufferSize;
} }
byte[] inputBuffer = context.Memory.ReadBytes(inputPosition, inputSize); byte[] inputBuffer = new byte[inputSize];
context.Memory.Read((ulong)inputPosition, inputBuffer);
Logger.PrintInfo(LogClass.ServicePrepo, ReadReportBuffer(inputBuffer, gameRoom, userId)); Logger.PrintInfo(LogClass.ServicePrepo, ReadReportBuffer(inputBuffer, gameRoom, userId));

View file

@ -1,6 +1,5 @@
using ARMeilleure.Memory; using Ryujinx.Common;
using Ryujinx.Common; using Ryujinx.Cpu;
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Memory; using Ryujinx.HLE.HOS.Kernel.Memory;
using Ryujinx.HLE.HOS.Kernel.Process; using Ryujinx.HLE.HOS.Kernel.Process;
@ -69,7 +68,11 @@ namespace Ryujinx.HLE.HOS.Services.Ro
for (int i = 0; i < header.HashCount; i++) for (int i = 0; i < header.HashCount; i++)
{ {
hashes.Add(context.Memory.ReadBytes(nrrAddress + header.HashOffset + (i * 0x20), 0x20)); byte[] temp = new byte[0x20];
context.Memory.Read((ulong)(nrrAddress + header.HashOffset + (i * 0x20)), temp);
hashes.Add(temp);
} }
nrrInfo = new NrrInfo(nrrAddress, header, hashes); nrrInfo = new NrrInfo(nrrAddress, header, hashes);
@ -127,15 +130,18 @@ namespace Ryujinx.HLE.HOS.Services.Ro
return ResultCode.InvalidAddress; return ResultCode.InvalidAddress;
} }
uint magic = context.Memory.ReadUInt32((long)nroAddress + 0x10); uint magic = context.Memory.Read<uint>(nroAddress + 0x10);
uint nroFileSize = context.Memory.ReadUInt32((long)nroAddress + 0x18); uint nroFileSize = context.Memory.Read<uint>(nroAddress + 0x18);
if (magic != NroMagic || nroSize != nroFileSize) if (magic != NroMagic || nroSize != nroFileSize)
{ {
return ResultCode.InvalidNro; return ResultCode.InvalidNro;
} }
byte[] nroData = context.Memory.ReadBytes((long)nroAddress, (long)nroSize); byte[] nroData = new byte[nroSize];
context.Memory.Read(nroAddress, nroData);
byte[] nroHash = null; byte[] nroHash = null;
MemoryStream stream = new MemoryStream(nroData); MemoryStream stream = new MemoryStream(nroData);
@ -319,9 +325,9 @@ namespace Ryujinx.HLE.HOS.Services.Ro
ulong bssEnd = BitUtils.AlignUp(bssStart + (ulong)relocatableObject.BssSize, KMemoryManager.PageSize); ulong bssEnd = BitUtils.AlignUp(bssStart + (ulong)relocatableObject.BssSize, KMemoryManager.PageSize);
process.CpuMemory.WriteBytes((long)textStart, relocatableObject.Text); process.CpuMemory.Write(textStart, relocatableObject.Text);
process.CpuMemory.WriteBytes((long)roStart, relocatableObject.Ro); process.CpuMemory.Write(roStart, relocatableObject.Ro);
process.CpuMemory.WriteBytes((long)dataStart, relocatableObject.Data); process.CpuMemory.Write(dataStart, relocatableObject.Data);
MemoryHelper.FillWithZeros(process.CpuMemory, (long)bssStart, (int)(bssEnd - bssStart)); MemoryHelper.FillWithZeros(process.CpuMemory, (long)bssStart, (int)(bssEnd - bssStart));

View file

@ -1,5 +1,5 @@
using ARMeilleure.Memory; using Ryujinx.Common;
using Ryujinx.Common; using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Services.Account.Acc; using Ryujinx.HLE.HOS.Services.Account.Acc;
using Ryujinx.HLE.HOS.Services.Sdb.Pdm.QueryService.Types; using Ryujinx.HLE.HOS.Services.Sdb.Pdm.QueryService.Types;
using System; using System;
@ -37,7 +37,7 @@ namespace Ryujinx.HLE.HOS.Services.Sdb.Pdm.QueryService
for (int i = 0; i < inputSize / sizeof(ulong); i++) for (int i = 0; i < inputSize / sizeof(ulong); i++)
{ {
titleIds.Add(BitConverter.ToUInt64(context.Memory.ReadBytes(inputPosition, inputSize), 0)); titleIds.Add(context.Memory.Read<ulong>((ulong)inputPosition));
} }
if (queryCapability == PlayLogQueryCapability.WhiteList) if (queryCapability == PlayLogQueryCapability.WhiteList)

View file

@ -116,11 +116,9 @@ namespace Ryujinx.HLE.HOS.Services.Sdb.Pl
return false; return false;
} }
context.Memory.WriteInt32(typesPosition + offset, (int)fontType); context.Memory.Write((ulong)(typesPosition + offset), (int)fontType);
context.Memory.Write((ulong)(offsetsPosition + offset), context.Device.System.Font.GetSharedMemoryAddressOffset(fontType));
context.Memory.WriteInt32(offsetsPosition + offset, context.Device.System.Font.GetSharedMemoryAddressOffset(fontType)); context.Memory.Write((ulong)(fontSizeBufferPosition + offset), context.Device.System.Font.GetFontSize(fontType));
context.Memory.WriteInt32(fontSizeBufferPosition + offset, context.Device.System.Font.GetFontSize(fontType));
return true; return true;
} }

View file

@ -114,7 +114,7 @@ namespace Ryujinx.HLE.HOS.Services.Settings
for (int index = 0; index < count; index++) for (int index = 0; index < count; index++)
{ {
context.Memory.WriteInt64(position, SystemStateMgr.GetLanguageCode(index)); context.Memory.Write((ulong)position, SystemStateMgr.GetLanguageCode(index));
position += 8; position += 8;
} }

View file

@ -35,7 +35,7 @@ namespace Ryujinx.HLE.HOS.Services.Settings
if (firmwareData != null) if (firmwareData != null)
{ {
context.Memory.WriteBytes(replyPos, firmwareData); context.Memory.Write((ulong)replyPos, firmwareData);
return ResultCode.Success; return ResultCode.Success;
} }
@ -78,7 +78,7 @@ namespace Ryujinx.HLE.HOS.Services.Settings
writer.Write(Encoding.ASCII.GetBytes(build)); writer.Write(Encoding.ASCII.GetBytes(build));
context.Memory.WriteBytes(replyPos, ms.ToArray()); context.Memory.Write((ulong)replyPos, ms.ToArray());
} }
return ResultCode.Success; return ResultCode.Success;
@ -114,10 +114,15 @@ namespace Ryujinx.HLE.HOS.Services.Settings
long namePos = context.Request.PtrBuff[1].Position; long namePos = context.Request.PtrBuff[1].Position;
long nameSize = context.Request.PtrBuff[1].Size; long nameSize = context.Request.PtrBuff[1].Size;
byte[] Class = context.Memory.ReadBytes(classPos, classSize); byte[] classBuffer = new byte[classSize];
byte[] name = context.Memory.ReadBytes(namePos, nameSize);
string askedSetting = Encoding.ASCII.GetString(Class).Trim('\0') + "!" + Encoding.ASCII.GetString(name).Trim('\0'); context.Memory.Read((ulong)classPos, classBuffer);
byte[] nameBuffer = new byte[nameSize];
context.Memory.Read((ulong)namePos, nameBuffer);
string askedSetting = Encoding.ASCII.GetString(classBuffer).Trim('\0') + "!" + Encoding.ASCII.GetString(nameBuffer).Trim('\0');
NxSettings.Settings.TryGetValue(askedSetting, out object nxSetting); NxSettings.Settings.TryGetValue(askedSetting, out object nxSetting);
@ -161,10 +166,15 @@ namespace Ryujinx.HLE.HOS.Services.Settings
long replyPos = context.Request.ReceiveBuff[0].Position; long replyPos = context.Request.ReceiveBuff[0].Position;
long replySize = context.Request.ReceiveBuff[0].Size; long replySize = context.Request.ReceiveBuff[0].Size;
byte[] Class = context.Memory.ReadBytes(classPos, classSize); byte[] classBuffer = new byte[classSize];
byte[] name = context.Memory.ReadBytes(namePos, nameSize);
string askedSetting = Encoding.ASCII.GetString(Class).Trim('\0') + "!" + Encoding.ASCII.GetString(name).Trim('\0'); context.Memory.Read((ulong)classPos, classBuffer);
byte[] nameBuffer = new byte[nameSize];
context.Memory.Read((ulong)namePos, nameBuffer);
string askedSetting = Encoding.ASCII.GetString(classBuffer).Trim('\0') + "!" + Encoding.ASCII.GetString(nameBuffer).Trim('\0');
NxSettings.Settings.TryGetValue(askedSetting, out object nxSetting); NxSettings.Settings.TryGetValue(askedSetting, out object nxSetting);
@ -197,7 +207,7 @@ namespace Ryujinx.HLE.HOS.Services.Settings
throw new NotImplementedException(nxSetting.GetType().Name); throw new NotImplementedException(nxSetting.GetType().Name);
} }
context.Memory.WriteBytes(replyPos, settingBuffer); context.Memory.Write((ulong)replyPos, settingBuffer);
Logger.PrintDebug(LogClass.ServiceSet, $"{askedSetting} set value: {nxSetting} as {nxSetting.GetType()}"); Logger.PrintDebug(LogClass.ServiceSet, $"{askedSetting} set value: {nxSetting} as {nxSetting.GetType()}");
} }

View file

@ -199,21 +199,23 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
private IPEndPoint ParseSockAddr(ServiceCtx context, long bufferPosition, long bufferSize) private IPEndPoint ParseSockAddr(ServiceCtx context, long bufferPosition, long bufferSize)
{ {
int size = context.Memory.ReadByte(bufferPosition); int size = context.Memory.Read<byte>((ulong)bufferPosition);
int family = context.Memory.ReadByte(bufferPosition + 1); int family = context.Memory.Read<byte>((ulong)bufferPosition + 1);
int port = BinaryPrimitives.ReverseEndianness(context.Memory.ReadUInt16(bufferPosition + 2)); int port = BinaryPrimitives.ReverseEndianness(context.Memory.Read<ushort>((ulong)bufferPosition + 2));
byte[] rawIp = context.Memory.ReadBytes(bufferPosition + 4, 4); byte[] rawIp = new byte[4];
context.Memory.Read((ulong)bufferPosition + 4, rawIp);
return new IPEndPoint(new IPAddress(rawIp), port); return new IPEndPoint(new IPAddress(rawIp), port);
} }
private void WriteSockAddr(ServiceCtx context, long bufferPosition, IPEndPoint endPoint) private void WriteSockAddr(ServiceCtx context, long bufferPosition, IPEndPoint endPoint)
{ {
context.Memory.WriteByte(bufferPosition, 0); context.Memory.Write((ulong)bufferPosition, (byte)0);
context.Memory.WriteByte(bufferPosition + 1, (byte)endPoint.AddressFamily); context.Memory.Write((ulong)bufferPosition + 1, (byte)endPoint.AddressFamily);
context.Memory.WriteUInt16(bufferPosition + 2, BinaryPrimitives.ReverseEndianness((ushort)endPoint.Port)); context.Memory.Write((ulong)bufferPosition + 2, BinaryPrimitives.ReverseEndianness((ushort)endPoint.Port));
context.Memory.WriteBytes(bufferPosition + 4, endPoint.Address.GetAddressBytes()); context.Memory.Write((ulong)bufferPosition + 4, endPoint.Address.GetAddressBytes());
} }
private void WriteSockAddr(ServiceCtx context, long bufferPosition, BsdSocket socket, bool isRemote) private void WriteSockAddr(ServiceCtx context, long bufferPosition, BsdSocket socket, bool isRemote)
@ -281,8 +283,11 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
int flags = context.RequestData.ReadInt32(); int flags = context.RequestData.ReadInt32();
byte[] rawPath = context.Memory.ReadBytes(bufferPosition, bufferSize); byte[] rawPath = new byte[bufferSize];
string path = Encoding.ASCII.GetString(rawPath);
context.Memory.Read((ulong)bufferPosition, rawPath);
string path = Encoding.ASCII.GetString(rawPath);
WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP); WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
@ -321,7 +326,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
for (int i = 0; i < fdsCount; i++) for (int i = 0; i < fdsCount; i++)
{ {
int socketFd = context.Memory.ReadInt32(bufferPosition + i * 8); int socketFd = context.Memory.Read<int>((ulong)(bufferPosition + i * 8));
BsdSocket socket = RetrieveSocket(socketFd); BsdSocket socket = RetrieveSocket(socketFd);
@ -329,8 +334,8 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
{ {
return WriteBsdResult(context, -1, LinuxError.EBADF);} return WriteBsdResult(context, -1, LinuxError.EBADF);}
PollEvent.EventTypeMask inputEvents = (PollEvent.EventTypeMask)context.Memory.ReadInt16(bufferPosition + i * 8 + 4); PollEvent.EventTypeMask inputEvents = (PollEvent.EventTypeMask)context.Memory.Read<short>((ulong)(bufferPosition + i * 8 + 4));
PollEvent.EventTypeMask outputEvents = (PollEvent.EventTypeMask)context.Memory.ReadInt16(bufferPosition + i * 8 + 6); PollEvent.EventTypeMask outputEvents = (PollEvent.EventTypeMask)context.Memory.Read<short>((ulong)(bufferPosition + i * 8 + 6));
events[i] = new PollEvent(socketFd, socket, inputEvents, outputEvents); events[i] = new PollEvent(socketFd, socket, inputEvents, outputEvents);
} }
@ -405,8 +410,8 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
for (int i = 0; i < fdsCount; i++) for (int i = 0; i < fdsCount; i++)
{ {
PollEvent Event = events[i]; PollEvent Event = events[i];
context.Memory.WriteInt32(bufferPosition + i * 8, Event.SocketFd); context.Memory.Write((ulong)(bufferPosition + i * 8), Event.SocketFd);
context.Memory.WriteInt16(bufferPosition + i * 8 + 4, (short)Event.InputEvents); context.Memory.Write((ulong)(bufferPosition + i * 8 + 4), (short)Event.InputEvents);
PollEvent.EventTypeMask outputEvents = 0; PollEvent.EventTypeMask outputEvents = 0;
@ -435,7 +440,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
outputEvents |= PollEvent.EventTypeMask.Output; outputEvents |= PollEvent.EventTypeMask.Output;
} }
context.Memory.WriteInt16(bufferPosition + i * 8 + 6, (short)outputEvents); context.Memory.Write((ulong)(bufferPosition + i * 8 + 6), (short)outputEvents);
} }
return WriteBsdResult(context, readEvents.Count + writeEvents.Count + errorEvents.Count, LinuxError.SUCCESS); return WriteBsdResult(context, readEvents.Count + writeEvents.Count + errorEvents.Count, LinuxError.SUCCESS);
@ -481,7 +486,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
result = socket.Handle.Receive(receivedBuffer, socketFlags); result = socket.Handle.Receive(receivedBuffer, socketFlags);
errno = SetResultErrno(socket.Handle, result); errno = SetResultErrno(socket.Handle, result);
context.Memory.WriteBytes(receivePosition, receivedBuffer); context.Memory.Write((ulong)receivePosition, receivedBuffer);
} }
catch (SocketException exception) catch (SocketException exception)
{ {
@ -524,7 +529,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
result = socket.Handle.ReceiveFrom(receivedBuffer, receivedBuffer.Length, socketFlags, ref endPoint); result = socket.Handle.ReceiveFrom(receivedBuffer, receivedBuffer.Length, socketFlags, ref endPoint);
errno = SetResultErrno(socket.Handle, result); errno = SetResultErrno(socket.Handle, result);
context.Memory.WriteBytes(receivePosition, receivedBuffer); context.Memory.Write((ulong)receivePosition, receivedBuffer);
WriteSockAddr(context, sockAddrOutPosition, (IPEndPoint)endPoint); WriteSockAddr(context, sockAddrOutPosition, (IPEndPoint)endPoint);
} }
catch (SocketException exception) catch (SocketException exception)
@ -559,7 +564,9 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
return WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP); return WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
} }
byte[] sendBuffer = context.Memory.ReadBytes(sendPosition, sendSize); byte[] sendBuffer = new byte[sendSize];
context.Memory.Read((ulong)sendPosition, sendBuffer);
try try
{ {
@ -600,8 +607,11 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
return WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP); return WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP);
} }
byte[] sendBuffer = context.Memory.ReadBytes(sendPosition, sendSize); byte[] sendBuffer = new byte[sendSize];
EndPoint endPoint = ParseSockAddr(context, bufferPosition, bufferSize);
context.Memory.Read((ulong)sendPosition, sendBuffer);
EndPoint endPoint = ParseSockAddr(context, bufferPosition, bufferSize);
try try
{ {
@ -856,7 +866,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
(long bufferPosition, long bufferSize) = context.Request.GetBufferType0x22(); (long bufferPosition, long bufferSize) = context.Request.GetBufferType0x22();
// FIXME: OOB not implemented. // FIXME: OOB not implemented.
context.Memory.WriteInt32(bufferPosition, 0); context.Memory.Write((ulong)bufferPosition, 0);
break; break;
default: default:
@ -925,13 +935,13 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
case SocketOptionName.Type: case SocketOptionName.Type:
case SocketOptionName.Linger: case SocketOptionName.Linger:
socket.Handle.GetSocketOption(SocketOptionLevel.Socket, optionName, optionValue); socket.Handle.GetSocketOption(SocketOptionLevel.Socket, optionName, optionValue);
context.Memory.WriteBytes(optionValuePosition, optionValue); context.Memory.Write((ulong)optionValuePosition, optionValue);
return LinuxError.SUCCESS; return LinuxError.SUCCESS;
case (SocketOptionName)0x200: case (SocketOptionName)0x200:
socket.Handle.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, optionValue); socket.Handle.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, optionValue);
context.Memory.WriteBytes(optionValuePosition, optionValue); context.Memory.Write((ulong)optionValuePosition, optionValue);
return LinuxError.SUCCESS; return LinuxError.SUCCESS;
@ -965,18 +975,18 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
case SocketOptionName.SendTimeout: case SocketOptionName.SendTimeout:
case SocketOptionName.Type: case SocketOptionName.Type:
case SocketOptionName.ReuseAddress: case SocketOptionName.ReuseAddress:
socket.Handle.SetSocketOption(SocketOptionLevel.Socket, optionName, context.Memory.ReadInt32(optionValuePosition)); socket.Handle.SetSocketOption(SocketOptionLevel.Socket, optionName, context.Memory.Read<int>((ulong)optionValuePosition));
return LinuxError.SUCCESS; return LinuxError.SUCCESS;
case (SocketOptionName)0x200: case (SocketOptionName)0x200:
socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, context.Memory.ReadInt32(optionValuePosition)); socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, context.Memory.Read<int>((ulong)optionValuePosition));
return LinuxError.SUCCESS; return LinuxError.SUCCESS;
case SocketOptionName.Linger: case SocketOptionName.Linger:
socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger,
new LingerOption(context.Memory.ReadInt32(optionValuePosition) != 0, context.Memory.ReadInt32(optionValuePosition + 4))); new LingerOption(context.Memory.Read<int>((ulong)optionValuePosition) != 0, context.Memory.Read<int>((ulong)optionValuePosition + 4)));
return LinuxError.SUCCESS; return LinuxError.SUCCESS;
@ -1100,7 +1110,9 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
if (socket != null) if (socket != null)
{ {
byte[] sendBuffer = context.Memory.ReadBytes(sendPosition, sendSize); byte[] sendBuffer = new byte[sendSize];
context.Memory.Read((ulong)sendPosition, sendBuffer);
try try
{ {

View file

@ -44,7 +44,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Nsd
{ {
byte[] settingNameBuffer = Encoding.UTF8.GetBytes(settingName + '\0'); byte[] settingNameBuffer = Encoding.UTF8.GetBytes(settingName + '\0');
context.Memory.WriteBytes(outputPosition, settingNameBuffer); context.Memory.Write((ulong)outputPosition, settingNameBuffer);
} }
return result; return result;
@ -62,7 +62,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Nsd
{ {
byte[] identifierBuffer = Encoding.UTF8.GetBytes(identifier + '\0'); byte[] identifierBuffer = Encoding.UTF8.GetBytes(identifier + '\0');
context.Memory.WriteBytes(outputPosition, identifierBuffer); context.Memory.Write((ulong)outputPosition, identifierBuffer);
} }
return result; return result;
@ -138,7 +138,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Nsd
byte[] resolvedAddressBuffer = Encoding.UTF8.GetBytes(resolvedAddress + '\0'); byte[] resolvedAddressBuffer = Encoding.UTF8.GetBytes(resolvedAddress + '\0');
context.Memory.WriteBytes(outputPosition, resolvedAddressBuffer); context.Memory.Write((ulong)outputPosition, resolvedAddressBuffer);
return result; return result;
} }
@ -153,7 +153,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Nsd
byte[] resolvedAddressBuffer = Encoding.UTF8.GetBytes(resolvedAddress + '\0'); byte[] resolvedAddressBuffer = Encoding.UTF8.GetBytes(resolvedAddress + '\0');
context.Memory.WriteBytes(outputPosition, resolvedAddressBuffer); context.Memory.Write((ulong)outputPosition, resolvedAddressBuffer);
context.ResponseData.Write((int)errorCode); context.ResponseData.Write((int)errorCode);

View file

@ -108,8 +108,11 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Nsd.Manager
{ {
(long inputPosition, long inputSize) = context.Request.GetBufferType0x21(); (long inputPosition, long inputSize) = context.Request.GetBufferType0x21();
byte[] addressBuffer = context.Memory.ReadBytes(inputPosition, inputSize); byte[] addressBuffer = new byte[inputSize];
string address = Encoding.UTF8.GetString(addressBuffer);
context.Memory.Read((ulong)inputPosition, addressBuffer);
string address = Encoding.UTF8.GetString(addressBuffer);
resultCode = Resolve(context, address, out resolvedAddress); resultCode = Resolve(context, address, out resolvedAddress);

View file

@ -21,37 +21,37 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres
string hostName = hostEntry.HostName + '\0'; string hostName = hostEntry.HostName + '\0';
// h_name // h_name
context.Memory.WriteBytes(bufferPosition, Encoding.ASCII.GetBytes(hostName)); context.Memory.Write((ulong)bufferPosition, Encoding.ASCII.GetBytes(hostName));
bufferPosition += hostName.Length; bufferPosition += hostName.Length;
// h_aliases list size // h_aliases list size
context.Memory.WriteInt32(bufferPosition, IPAddress.HostToNetworkOrder(hostEntry.Aliases.Length)); context.Memory.Write((ulong)bufferPosition, IPAddress.HostToNetworkOrder(hostEntry.Aliases.Length));
bufferPosition += 4; bufferPosition += 4;
// Actual aliases // Actual aliases
foreach (string alias in hostEntry.Aliases) foreach (string alias in hostEntry.Aliases)
{ {
context.Memory.WriteBytes(bufferPosition, Encoding.ASCII.GetBytes(alias + '\0')); context.Memory.Write((ulong)bufferPosition, Encoding.ASCII.GetBytes(alias + '\0'));
bufferPosition += alias.Length + 1; bufferPosition += alias.Length + 1;
} }
// h_addrtype but it's a short (also only support IPv4) // h_addrtype but it's a short (also only support IPv4)
context.Memory.WriteInt16(bufferPosition, IPAddress.HostToNetworkOrder((short)2)); context.Memory.Write((ulong)bufferPosition, IPAddress.HostToNetworkOrder((short)2));
bufferPosition += 2; bufferPosition += 2;
// h_length but it's a short // h_length but it's a short
context.Memory.WriteInt16(bufferPosition, IPAddress.HostToNetworkOrder((short)4)); context.Memory.Write((ulong)bufferPosition, IPAddress.HostToNetworkOrder((short)4));
bufferPosition += 2; bufferPosition += 2;
// Ip address count, we can only support ipv4 (blame Nintendo) // Ip address count, we can only support ipv4 (blame Nintendo)
context.Memory.WriteInt32(bufferPosition, addresses != null ? IPAddress.HostToNetworkOrder(addresses.Count) : 0); context.Memory.Write((ulong)bufferPosition, addresses != null ? IPAddress.HostToNetworkOrder(addresses.Count) : 0);
bufferPosition += 4; bufferPosition += 4;
if (addresses != null) if (addresses != null)
{ {
foreach (IPAddress ip in addresses) foreach (IPAddress ip in addresses)
{ {
context.Memory.WriteInt32(bufferPosition, IPAddress.HostToNetworkOrder(BitConverter.ToInt32(ip.GetAddressBytes(), 0))); context.Memory.Write((ulong)bufferPosition, IPAddress.HostToNetworkOrder(BitConverter.ToInt32(ip.GetAddressBytes(), 0)));
bufferPosition += 4; bufferPosition += 4;
} }
} }
@ -168,8 +168,11 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres
// GetHostByName(u8, u32, u64, pid, buffer<unknown, 5, 0>) -> (u32, u32, u32, buffer<unknown, 6, 0>) // GetHostByName(u8, u32, u64, pid, buffer<unknown, 5, 0>) -> (u32, u32, u32, buffer<unknown, 6, 0>)
public ResultCode GetHostByName(ServiceCtx context) public ResultCode GetHostByName(ServiceCtx context)
{ {
byte[] rawName = context.Memory.ReadBytes(context.Request.SendBuff[0].Position, context.Request.SendBuff[0].Size); byte[] rawName = new byte[context.Request.SendBuff[0].Size];
string name = Encoding.ASCII.GetString(rawName).TrimEnd('\0');
context.Memory.Read((ulong)context.Request.SendBuff[0].Position, rawName);
string name = Encoding.ASCII.GetString(rawName).TrimEnd('\0');
// TODO: use params // TODO: use params
bool enableNsdResolve = context.RequestData.ReadInt32() == 1; bool enableNsdResolve = context.RequestData.ReadInt32() == 1;
@ -248,7 +251,9 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres
// GetHostByAddr(u32, u32, u32, u64, pid, buffer<unknown, 5, 0>) -> (u32, u32, u32, buffer<unknown, 6, 0>) // GetHostByAddr(u32, u32, u32, u64, pid, buffer<unknown, 5, 0>) -> (u32, u32, u32, buffer<unknown, 6, 0>)
public ResultCode GetHostByAddress(ServiceCtx context) public ResultCode GetHostByAddress(ServiceCtx context)
{ {
byte[] rawIp = context.Memory.ReadBytes(context.Request.SendBuff[0].Position, context.Request.SendBuff[0].Size); byte[] rawIp = new byte[context.Request.SendBuff[0].Size];
context.Memory.Read((ulong)context.Request.SendBuff[0].Position, rawIp);
// TODO: use params // TODO: use params
uint socketLength = context.RequestData.ReadUInt32(); uint socketLength = context.RequestData.ReadUInt32();
@ -325,7 +330,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres
if (errorString.Length + 1 <= context.Request.ReceiveBuff[0].Size) if (errorString.Length + 1 <= context.Request.ReceiveBuff[0].Size)
{ {
resultCode = 0; resultCode = 0;
context.Memory.WriteBytes(context.Request.ReceiveBuff[0].Position, Encoding.ASCII.GetBytes(errorString + '\0')); context.Memory.Write((ulong)context.Request.ReceiveBuff[0].Position, Encoding.ASCII.GetBytes(errorString + '\0'));
} }
return resultCode; return resultCode;
@ -342,7 +347,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres
if (errorString.Length + 1 <= context.Request.ReceiveBuff[0].Size) if (errorString.Length + 1 <= context.Request.ReceiveBuff[0].Size)
{ {
resultCode = 0; resultCode = 0;
context.Memory.WriteBytes(context.Request.ReceiveBuff[0].Position, Encoding.ASCII.GetBytes(errorString + '\0')); context.Memory.Write((ulong)context.Request.ReceiveBuff[0].Position, Encoding.ASCII.GetBytes(errorString + '\0'));
} }
return resultCode; return resultCode;

View file

@ -21,7 +21,7 @@ namespace Ryujinx.HLE.HOS.Services.Spl
_rng.GetBytes(randomBytes); _rng.GetBytes(randomBytes);
context.Memory.WriteBytes(context.Request.ReceiveBuff[0].Position, randomBytes); context.Memory.Write((ulong)context.Request.ReceiveBuff[0].Position, randomBytes);
return ResultCode.Success; return ResultCode.Success;
} }

View file

@ -24,7 +24,7 @@ namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
long replyPos = context.Request.ReceiveBuff[0].Position; long replyPos = context.Request.ReceiveBuff[0].Position;
long replySize = context.Request.ReceiveBuff[0].Size; long replySize = context.Request.ReceiveBuff[0].Size;
ReadOnlySpan<byte> inputParcel = context.Memory.GetSpan(dataPos, dataSize); ReadOnlySpan<byte> inputParcel = context.Memory.GetSpan(dataPos, (int)dataSize);
Span<byte> outputParcel = new Span<byte>(new byte[replySize]); Span<byte> outputParcel = new Span<byte>(new byte[replySize]);
@ -32,7 +32,7 @@ namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
if (result == ResultCode.Success) if (result == ResultCode.Success)
{ {
context.Memory.WriteBytes(replyPos, outputParcel.ToArray()); context.Memory.Write((ulong)replyPos, outputParcel);
} }
return result; return result;
@ -81,7 +81,7 @@ namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
(long dataPos, long dataSize) = context.Request.GetBufferType0x21(); (long dataPos, long dataSize) = context.Request.GetBufferType0x21();
(long replyPos, long replySize) = context.Request.GetBufferType0x22(); (long replyPos, long replySize) = context.Request.GetBufferType0x22();
ReadOnlySpan<byte> inputParcel = context.Memory.GetSpan((ulong)dataPos, (ulong)dataSize); ReadOnlySpan<byte> inputParcel = context.Memory.GetSpan((ulong)dataPos, (int)dataSize);
Span<byte> outputParcel = new Span<byte>(new byte[replySize]); Span<byte> outputParcel = new Span<byte>(new byte[replySize]);
@ -89,7 +89,7 @@ namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
if (result == ResultCode.Success) if (result == ResultCode.Success)
{ {
context.Memory.WriteBytes(replyPos, outputParcel.ToArray()); context.Memory.Write((ulong)replyPos, outputParcel);
} }
return result; return result;

View file

@ -401,7 +401,11 @@ namespace Ryujinx.HLE.HOS.Services.Time
{ {
Debug.Assert(ipcDesc.Size == Marshal.SizeOf<ClockSnapshot>()); Debug.Assert(ipcDesc.Size == Marshal.SizeOf<ClockSnapshot>());
using (BinaryReader bufferReader = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(ipcDesc.Position, ipcDesc.Size)))) byte[] temp = new byte[ipcDesc.Size];
context.Memory.Read((ulong)ipcDesc.Position, temp);
using (BinaryReader bufferReader = new BinaryReader(new MemoryStream(temp)))
{ {
return bufferReader.ReadStruct<ClockSnapshot>(); return bufferReader.ReadStruct<ClockSnapshot>();
} }
@ -418,7 +422,7 @@ namespace Ryujinx.HLE.HOS.Services.Time
bufferWriter.WriteStruct(clockSnapshot); bufferWriter.WriteStruct(clockSnapshot);
} }
context.Memory.WriteBytes(ipcDesc.Position, memory.ToArray()); context.Memory.Write((ulong)ipcDesc.Position, memory.ToArray());
memory.Dispose(); memory.Dispose();
} }
} }

View file

@ -123,7 +123,11 @@ namespace Ryujinx.HLE.HOS.Services.Time
(long bufferPosition, long bufferSize) = context.Request.GetBufferType0x21(); (long bufferPosition, long bufferSize) = context.Request.GetBufferType0x21();
using (MemoryStream timeZoneBinaryStream = new MemoryStream(context.Memory.ReadBytes(bufferPosition, bufferSize))) byte[] temp = new byte[bufferSize];
context.Memory.Read((ulong)bufferPosition, temp);
using (MemoryStream timeZoneBinaryStream = new MemoryStream(temp))
{ {
_timeManager.SetupTimeZoneManager(locationName, timeZoneUpdateTimePoint, totalLocationNameCount, timeZoneRuleVersion, timeZoneBinaryStream); _timeManager.SetupTimeZoneManager(locationName, timeZoneUpdateTimePoint, totalLocationNameCount, timeZoneRuleVersion, timeZoneBinaryStream);
} }

View file

@ -1,6 +1,5 @@
using ARMeilleure.Memory;
using Ryujinx.Common;
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Services.Time.TimeZone; using Ryujinx.HLE.HOS.Services.Time.TimeZone;
using System; using System;
using System.Text; using System.Text;
@ -71,7 +70,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService
return ResultCode.LocationNameTooLong; return ResultCode.LocationNameTooLong;
} }
context.Memory.WriteBytes(bufferPosition + offset, Encoding.ASCII.GetBytes(locationName)); context.Memory.Write((ulong)bufferPosition + offset, Encoding.ASCII.GetBytes(locationName));
MemoryHelper.FillWithZeros(context.Memory, bufferPosition + offset + locationName.Length, padding); MemoryHelper.FillWithZeros(context.Memory, bufferPosition + offset + locationName.Length, padding);
offset += 0x24; offset += 0x24;

Some files were not shown because too many files have changed in this diff Show more