2019-01-18 17:26:39 -05:00
|
|
|
using Ryujinx.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Memory;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
|
|
|
{
|
|
|
|
class KServerSession : KSynchronizationObject
|
|
|
|
{
|
|
|
|
private static readonly MemoryState[] IpcMemoryStates = new MemoryState[]
|
|
|
|
{
|
|
|
|
MemoryState.IpcBuffer3,
|
|
|
|
MemoryState.IpcBuffer0,
|
|
|
|
MemoryState.IpcBuffer1,
|
|
|
|
(MemoryState)0xfffce5d4 //This is invalid, shouldn't be accessed.
|
|
|
|
};
|
|
|
|
|
|
|
|
private struct Message
|
|
|
|
{
|
|
|
|
public ulong Address { get; }
|
|
|
|
public ulong DramAddress { get; }
|
|
|
|
public ulong Size { get; }
|
|
|
|
public bool IsCustom { get; }
|
|
|
|
|
|
|
|
public Message(KThread thread, ulong customCmdBuffAddress, ulong customCmdBuffSize)
|
|
|
|
{
|
|
|
|
IsCustom = customCmdBuffAddress != 0;
|
|
|
|
|
|
|
|
if (IsCustom)
|
|
|
|
{
|
|
|
|
Address = customCmdBuffAddress;
|
|
|
|
Size = customCmdBuffSize;
|
|
|
|
|
|
|
|
KProcess process = thread.Owner;
|
|
|
|
|
|
|
|
DramAddress = process.MemoryManager.GetDramAddressFromVa(Address);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Address = thread.TlsAddress;
|
|
|
|
DramAddress = thread.TlsDramAddress;
|
|
|
|
Size = 0x100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Message(KSessionRequest request) : this(
|
|
|
|
request.ClientThread,
|
|
|
|
request.CustomCmdBuffAddr,
|
|
|
|
request.CustomCmdBuffSize) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct MessageHeader
|
|
|
|
{
|
|
|
|
public uint Word0 { get; }
|
|
|
|
public uint Word1 { get; }
|
|
|
|
public uint Word2 { get; }
|
|
|
|
|
|
|
|
public uint PointerBuffersCount { get; }
|
|
|
|
public uint SendBuffersCount { get; }
|
|
|
|
public uint ReceiveBuffersCount { get; }
|
|
|
|
public uint ExchangeBuffersCount { get; }
|
|
|
|
|
|
|
|
public uint RawDataSizeInWords { get; }
|
|
|
|
|
|
|
|
public uint ReceiveListType { get; }
|
|
|
|
|
|
|
|
public uint MessageSizeInWords { get; }
|
|
|
|
public uint ReceiveListOffsetInWords { get; }
|
|
|
|
public uint ReceiveListOffset { get; }
|
|
|
|
|
|
|
|
public bool HasHandles { get; }
|
|
|
|
|
|
|
|
public bool HasPid { get; }
|
|
|
|
|
|
|
|
public uint CopyHandlesCount { get; }
|
|
|
|
public uint MoveHandlesCount { get; }
|
|
|
|
|
|
|
|
public MessageHeader(uint word0, uint word1, uint word2)
|
|
|
|
{
|
|
|
|
Word0 = word0;
|
|
|
|
Word1 = word1;
|
|
|
|
Word2 = word2;
|
|
|
|
|
|
|
|
HasHandles = word1 >> 31 != 0;
|
|
|
|
|
|
|
|
uint handleDescSizeInWords = 0;
|
|
|
|
|
|
|
|
if (HasHandles)
|
|
|
|
{
|
|
|
|
uint pidSize = (word2 & 1) * 8;
|
|
|
|
|
|
|
|
HasPid = pidSize != 0;
|
|
|
|
|
|
|
|
CopyHandlesCount = (word2 >> 1) & 0xf;
|
|
|
|
MoveHandlesCount = (word2 >> 5) & 0xf;
|
|
|
|
|
|
|
|
handleDescSizeInWords = (pidSize + CopyHandlesCount * 4 + MoveHandlesCount * 4) / 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HasPid = false;
|
|
|
|
|
|
|
|
CopyHandlesCount = 0;
|
|
|
|
MoveHandlesCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerBuffersCount = (word0 >> 16) & 0xf;
|
|
|
|
SendBuffersCount = (word0 >> 20) & 0xf;
|
|
|
|
ReceiveBuffersCount = (word0 >> 24) & 0xf;
|
|
|
|
ExchangeBuffersCount = word0 >> 28;
|
|
|
|
|
|
|
|
uint pointerDescSizeInWords = PointerBuffersCount * 2;
|
|
|
|
uint sendDescSizeInWords = SendBuffersCount * 3;
|
|
|
|
uint receiveDescSizeInWords = ReceiveBuffersCount * 3;
|
|
|
|
uint exchangeDescSizeInWords = ExchangeBuffersCount * 3;
|
|
|
|
|
|
|
|
RawDataSizeInWords = word1 & 0x3ff;
|
|
|
|
|
|
|
|
ReceiveListType = (word1 >> 10) & 0xf;
|
|
|
|
|
|
|
|
ReceiveListOffsetInWords = (word1 >> 20) & 0x7ff;
|
|
|
|
|
|
|
|
uint paddingSizeInWords = HasHandles ? 3u : 2u;
|
|
|
|
|
|
|
|
MessageSizeInWords = pointerDescSizeInWords +
|
|
|
|
sendDescSizeInWords +
|
|
|
|
receiveDescSizeInWords +
|
|
|
|
exchangeDescSizeInWords +
|
|
|
|
RawDataSizeInWords +
|
|
|
|
paddingSizeInWords +
|
|
|
|
handleDescSizeInWords;
|
|
|
|
|
|
|
|
if (ReceiveListOffsetInWords == 0)
|
|
|
|
{
|
|
|
|
ReceiveListOffsetInWords = MessageSizeInWords;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReceiveListOffset = ReceiveListOffsetInWords * 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct PointerBufferDesc
|
|
|
|
{
|
|
|
|
public uint ReceiveIndex { get; }
|
|
|
|
|
|
|
|
public uint BufferSize { get; }
|
|
|
|
public ulong BufferAddress { get; set; }
|
|
|
|
|
|
|
|
public PointerBufferDesc(ulong dword)
|
|
|
|
{
|
|
|
|
ReceiveIndex = (uint)dword & 0xf;
|
|
|
|
BufferSize = (uint)dword >> 16;
|
|
|
|
|
|
|
|
BufferAddress = (dword >> 2) & 0x70;
|
|
|
|
BufferAddress |= (dword >> 12) & 0xf;
|
|
|
|
|
|
|
|
BufferAddress = (BufferAddress << 32) | (dword >> 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ulong Pack()
|
|
|
|
{
|
|
|
|
ulong dword = (ReceiveIndex & 0xf) | ((BufferSize & 0xffff) << 16);
|
|
|
|
|
|
|
|
dword |= BufferAddress << 32;
|
|
|
|
dword |= (BufferAddress >> 20) & 0xf000;
|
|
|
|
dword |= (BufferAddress >> 30) & 0xffc0;
|
|
|
|
|
|
|
|
return dword;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private KSession _parent;
|
|
|
|
|
|
|
|
private LinkedList<KSessionRequest> _requests;
|
|
|
|
|
|
|
|
private KSessionRequest _activeRequest;
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
public KServerSession(KernelContext context, KSession parent) : base(context)
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
|
|
|
_parent = parent;
|
|
|
|
|
|
|
|
_requests = new LinkedList<KSessionRequest>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult EnqueueRequest(KSessionRequest request)
|
|
|
|
{
|
|
|
|
if (_parent.ClientSession.State != ChannelState.Open)
|
|
|
|
{
|
|
|
|
return KernelResult.PortRemoteClosed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.AsyncEvent == null)
|
|
|
|
{
|
|
|
|
if (request.ClientThread.ShallBeTerminated ||
|
|
|
|
request.ClientThread.SchedFlags == ThreadSchedState.TerminationPending)
|
|
|
|
{
|
|
|
|
return KernelResult.ThreadTerminating;
|
|
|
|
}
|
|
|
|
|
|
|
|
request.ClientThread.Reschedule(ThreadSchedState.Paused);
|
|
|
|
}
|
|
|
|
|
|
|
|
_requests.AddLast(request);
|
|
|
|
|
|
|
|
if (_requests.Count == 1)
|
|
|
|
{
|
|
|
|
Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult Receive(ulong customCmdBuffAddr = 0, ulong customCmdBuffSize = 0)
|
|
|
|
{
|
2020-12-09 17:20:05 -05:00
|
|
|
KThread serverThread = KernelStatic.GetCurrentThread();
|
2019-01-18 17:26:39 -05:00
|
|
|
KProcess serverProcess = serverThread.Owner;
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Enter();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
if (_parent.ClientSession.State != ChannelState.Open)
|
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Leave();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
return KernelResult.PortRemoteClosed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_activeRequest != null || !DequeueRequest(out KSessionRequest request))
|
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Leave();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
return KernelResult.NotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.ClientThread == null)
|
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Leave();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
return KernelResult.PortRemoteClosed;
|
|
|
|
}
|
|
|
|
|
|
|
|
KThread clientThread = request.ClientThread;
|
|
|
|
KProcess clientProcess = clientThread.Owner;
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Leave();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
_activeRequest = request;
|
|
|
|
|
|
|
|
request.ServerProcess = serverProcess;
|
|
|
|
|
|
|
|
Message clientMsg = new Message(request);
|
|
|
|
Message serverMsg = new Message(serverThread, customCmdBuffAddr, customCmdBuffSize);
|
|
|
|
|
|
|
|
MessageHeader clientHeader = GetClientMessageHeader(clientMsg);
|
|
|
|
MessageHeader serverHeader = GetServerMessageHeader(serverMsg);
|
|
|
|
|
|
|
|
KernelResult serverResult = KernelResult.NotFound;
|
|
|
|
KernelResult clientResult = KernelResult.Success;
|
|
|
|
|
|
|
|
void CleanUpForError()
|
|
|
|
{
|
|
|
|
if (request.BufferDescriptorTable.UnmapServerBuffers(serverProcess.MemoryManager) == KernelResult.Success)
|
|
|
|
{
|
|
|
|
request.BufferDescriptorTable.RestoreClientBuffers(clientProcess.MemoryManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseAllHandles(serverMsg, clientHeader, serverProcess);
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Enter();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
_activeRequest = null;
|
|
|
|
|
|
|
|
if (_requests.Count != 0)
|
|
|
|
{
|
|
|
|
Signal();
|
|
|
|
}
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Leave();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
WakeClientThread(request, clientResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clientHeader.ReceiveListType < 2 &&
|
|
|
|
clientHeader.ReceiveListOffset > clientMsg.Size)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
else if (clientHeader.ReceiveListType == 2 &&
|
|
|
|
clientHeader.ReceiveListOffset + 8 > clientMsg.Size)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
else if (clientHeader.ReceiveListType > 2 &&
|
|
|
|
clientHeader.ReceiveListType * 8 - 0x10 + clientHeader.ReceiveListOffset > clientMsg.Size)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clientHeader.ReceiveListOffsetInWords < clientHeader.MessageSizeInWords)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clientHeader.MessageSizeInWords * 4 > clientMsg.Size)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.CmdBufferTooSmall;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong[] receiveList = GetReceiveList(
|
|
|
|
serverMsg,
|
|
|
|
serverHeader.ReceiveListType,
|
|
|
|
serverHeader.ReceiveListOffset);
|
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
serverProcess.CpuMemory.Write(serverMsg.Address + 0, clientHeader.Word0);
|
|
|
|
serverProcess.CpuMemory.Write(serverMsg.Address + 4, clientHeader.Word1);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
uint offset;
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Copy handles.
|
2019-01-18 17:26:39 -05:00
|
|
|
if (clientHeader.HasHandles)
|
|
|
|
{
|
|
|
|
if (clientHeader.MoveHandlesCount != 0)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
serverProcess.CpuMemory.Write(serverMsg.Address + 8, clientHeader.Word2);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
offset = 3;
|
|
|
|
|
|
|
|
if (clientHeader.HasPid)
|
|
|
|
{
|
2020-05-03 18:54:50 -04:00
|
|
|
serverProcess.CpuMemory.Write(serverMsg.Address + offset * 4, clientProcess.Pid);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
offset += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int index = 0; index < clientHeader.CopyHandlesCount; index++)
|
|
|
|
{
|
|
|
|
int newHandle = 0;
|
2020-05-03 23:41:29 -04:00
|
|
|
int handle = KernelContext.Memory.Read<int>(clientMsg.DramAddress + offset * 4);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
if (clientResult == KernelResult.Success && handle != 0)
|
|
|
|
{
|
|
|
|
clientResult = GetCopyObjectHandle(clientThread, serverProcess, handle, out newHandle);
|
|
|
|
}
|
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
serverProcess.CpuMemory.Write(serverMsg.Address + offset * 4, newHandle);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int index = 0; index < clientHeader.MoveHandlesCount; index++)
|
|
|
|
{
|
|
|
|
int newHandle = 0;
|
2020-05-03 23:41:29 -04:00
|
|
|
int handle = KernelContext.Memory.Read<int>(clientMsg.DramAddress + offset * 4);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
if (handle != 0)
|
|
|
|
{
|
|
|
|
if (clientResult == KernelResult.Success)
|
|
|
|
{
|
|
|
|
clientResult = GetMoveObjectHandle(clientProcess, serverProcess, handle, out newHandle);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clientProcess.HandleTable.CloseHandle(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
serverProcess.CpuMemory.Write(serverMsg.Address + offset * 4, newHandle);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clientResult != KernelResult.Success)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return serverResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
offset = 2;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Copy pointer/receive list buffers.
|
2019-01-18 17:26:39 -05:00
|
|
|
uint recvListDstOffset = 0;
|
|
|
|
|
|
|
|
for (int index = 0; index < clientHeader.PointerBuffersCount; index++)
|
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
ulong pointerDesc = KernelContext.Memory.Read<ulong>(clientMsg.DramAddress + offset * 4);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
PointerBufferDesc descriptor = new PointerBufferDesc(pointerDesc);
|
|
|
|
|
|
|
|
if (descriptor.BufferSize != 0)
|
|
|
|
{
|
|
|
|
clientResult = GetReceiveListAddress(
|
|
|
|
descriptor,
|
|
|
|
serverMsg,
|
|
|
|
serverHeader.ReceiveListType,
|
|
|
|
clientHeader.MessageSizeInWords,
|
|
|
|
receiveList,
|
|
|
|
ref recvListDstOffset,
|
|
|
|
out ulong recvListBufferAddress);
|
|
|
|
|
|
|
|
if (clientResult != KernelResult.Success)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return serverResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
clientResult = clientProcess.MemoryManager.CopyDataToCurrentProcess(
|
|
|
|
recvListBufferAddress,
|
|
|
|
descriptor.BufferSize,
|
|
|
|
descriptor.BufferAddress,
|
|
|
|
MemoryState.IsPoolAllocated,
|
|
|
|
MemoryState.IsPoolAllocated,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.Read,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute.Uncached,
|
|
|
|
MemoryAttribute.None);
|
|
|
|
|
|
|
|
if (clientResult != KernelResult.Success)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return serverResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
descriptor.BufferAddress = recvListBufferAddress;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
descriptor.BufferAddress = 0;
|
|
|
|
}
|
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
serverProcess.CpuMemory.Write(serverMsg.Address + offset * 4, descriptor.Pack());
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
offset += 2;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Copy send, receive and exchange buffers.
|
2019-01-18 17:26:39 -05:00
|
|
|
uint totalBuffersCount =
|
|
|
|
clientHeader.SendBuffersCount +
|
|
|
|
clientHeader.ReceiveBuffersCount +
|
|
|
|
clientHeader.ExchangeBuffersCount;
|
|
|
|
|
|
|
|
for (int index = 0; index < totalBuffersCount; index++)
|
|
|
|
{
|
2020-05-03 18:54:50 -04:00
|
|
|
ulong clientDescAddress = clientMsg.DramAddress + offset * 4;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
uint descWord0 = KernelContext.Memory.Read<uint>(clientDescAddress + 0);
|
|
|
|
uint descWord1 = KernelContext.Memory.Read<uint>(clientDescAddress + 4);
|
|
|
|
uint descWord2 = KernelContext.Memory.Read<uint>(clientDescAddress + 8);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
bool isSendDesc = index < clientHeader.SendBuffersCount;
|
|
|
|
bool isExchangeDesc = index >= clientHeader.SendBuffersCount + clientHeader.ReceiveBuffersCount;
|
|
|
|
|
|
|
|
bool notReceiveDesc = isSendDesc || isExchangeDesc;
|
|
|
|
bool isReceiveDesc = !notReceiveDesc;
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permission = index >= clientHeader.SendBuffersCount
|
|
|
|
? KMemoryPermission.ReadAndWrite
|
|
|
|
: KMemoryPermission.Read;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
uint sizeHigh4 = (descWord2 >> 24) & 0xf;
|
|
|
|
|
|
|
|
ulong bufferSize = descWord0 | (ulong)sizeHigh4 << 32;
|
|
|
|
|
|
|
|
ulong dstAddress = 0;
|
|
|
|
|
|
|
|
if (bufferSize != 0)
|
|
|
|
{
|
|
|
|
ulong bufferAddress;
|
|
|
|
|
|
|
|
bufferAddress = descWord2 >> 28;
|
|
|
|
bufferAddress |= ((descWord2 >> 2) & 7) << 4;
|
|
|
|
|
|
|
|
bufferAddress = (bufferAddress << 32) | descWord1;
|
|
|
|
|
|
|
|
MemoryState state = IpcMemoryStates[(descWord2 + 1) & 3];
|
|
|
|
|
|
|
|
clientResult = serverProcess.MemoryManager.MapBufferFromClientProcess(
|
|
|
|
bufferSize,
|
|
|
|
bufferAddress,
|
|
|
|
clientProcess.MemoryManager,
|
|
|
|
permission,
|
|
|
|
state,
|
|
|
|
notReceiveDesc,
|
|
|
|
out dstAddress);
|
|
|
|
|
|
|
|
if (clientResult != KernelResult.Success)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return serverResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSendDesc)
|
|
|
|
{
|
|
|
|
clientResult = request.BufferDescriptorTable.AddSendBuffer(bufferAddress, dstAddress, bufferSize, state);
|
|
|
|
}
|
|
|
|
else if (isReceiveDesc)
|
|
|
|
{
|
|
|
|
clientResult = request.BufferDescriptorTable.AddReceiveBuffer(bufferAddress, dstAddress, bufferSize, state);
|
|
|
|
}
|
|
|
|
else /* if (isExchangeDesc) */
|
|
|
|
{
|
|
|
|
clientResult = request.BufferDescriptorTable.AddExchangeBuffer(bufferAddress, dstAddress, bufferSize, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clientResult != KernelResult.Success)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return serverResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
descWord1 = (uint)dstAddress;
|
|
|
|
|
|
|
|
descWord2 &= 3;
|
|
|
|
|
|
|
|
descWord2 |= sizeHigh4 << 24;
|
|
|
|
|
|
|
|
descWord2 |= (uint)(dstAddress >> 34) & 0x3ffffffc;
|
|
|
|
descWord2 |= (uint)(dstAddress >> 4) & 0xf0000000;
|
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
ulong serverDescAddress = serverMsg.Address + offset * 4;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
serverProcess.CpuMemory.Write(serverDescAddress + 0, descWord0);
|
|
|
|
serverProcess.CpuMemory.Write(serverDescAddress + 4, descWord1);
|
|
|
|
serverProcess.CpuMemory.Write(serverDescAddress + 8, descWord2);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
offset += 3;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Copy raw data.
|
2019-01-18 17:26:39 -05:00
|
|
|
if (clientHeader.RawDataSizeInWords != 0)
|
|
|
|
{
|
|
|
|
ulong copySrc = clientMsg.Address + offset * 4;
|
|
|
|
ulong copyDst = serverMsg.Address + offset * 4;
|
|
|
|
|
|
|
|
ulong copySize = clientHeader.RawDataSizeInWords * 4;
|
|
|
|
|
|
|
|
if (serverMsg.IsCustom || clientMsg.IsCustom)
|
|
|
|
{
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permission = clientMsg.IsCustom
|
|
|
|
? KMemoryPermission.None
|
|
|
|
: KMemoryPermission.Read;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
clientResult = clientProcess.MemoryManager.CopyDataToCurrentProcess(
|
|
|
|
copyDst,
|
|
|
|
copySize,
|
|
|
|
copySrc,
|
|
|
|
MemoryState.IsPoolAllocated,
|
|
|
|
MemoryState.IsPoolAllocated,
|
|
|
|
permission,
|
|
|
|
MemoryAttribute.Uncached,
|
|
|
|
MemoryAttribute.None);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
copySrc = clientProcess.MemoryManager.GetDramAddressFromVa(copySrc);
|
|
|
|
copyDst = serverProcess.MemoryManager.GetDramAddressFromVa(copyDst);
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.Memory.Copy(copyDst, copySrc, copySize);
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (clientResult != KernelResult.Success)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return serverResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult Reply(ulong customCmdBuffAddr = 0, ulong customCmdBuffSize = 0)
|
|
|
|
{
|
2020-12-09 17:20:05 -05:00
|
|
|
KThread serverThread = KernelStatic.GetCurrentThread();
|
2019-01-18 17:26:39 -05:00
|
|
|
KProcess serverProcess = serverThread.Owner;
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Enter();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
if (_activeRequest == null)
|
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Leave();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
return KernelResult.InvalidState;
|
|
|
|
}
|
|
|
|
|
|
|
|
KSessionRequest request = _activeRequest;
|
|
|
|
|
|
|
|
_activeRequest = null;
|
|
|
|
|
|
|
|
if (_requests.Count != 0)
|
|
|
|
{
|
|
|
|
Signal();
|
|
|
|
}
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Leave();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
KThread clientThread = request.ClientThread;
|
|
|
|
KProcess clientProcess = clientThread.Owner;
|
|
|
|
|
|
|
|
Message clientMsg = new Message(request);
|
|
|
|
Message serverMsg = new Message(serverThread, customCmdBuffAddr, customCmdBuffSize);
|
|
|
|
|
|
|
|
MessageHeader clientHeader = GetClientMessageHeader(clientMsg);
|
|
|
|
MessageHeader serverHeader = GetServerMessageHeader(serverMsg);
|
|
|
|
|
|
|
|
KernelResult clientResult = KernelResult.Success;
|
|
|
|
KernelResult serverResult = KernelResult.Success;
|
|
|
|
|
|
|
|
void CleanUpForError()
|
|
|
|
{
|
|
|
|
CloseAllHandles(clientMsg, serverHeader, clientProcess);
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
FinishRequest(request, clientResult);
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (clientHeader.ReceiveListType < 2 &&
|
|
|
|
clientHeader.ReceiveListOffset > clientMsg.Size)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
else if (clientHeader.ReceiveListType == 2 &&
|
|
|
|
clientHeader.ReceiveListOffset + 8 > clientMsg.Size)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
else if (clientHeader.ReceiveListType > 2 &&
|
|
|
|
clientHeader.ReceiveListType * 8 - 0x10 + clientHeader.ReceiveListOffset > clientMsg.Size)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clientHeader.ReceiveListOffsetInWords < clientHeader.MessageSizeInWords)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (serverHeader.MessageSizeInWords * 4 > clientMsg.Size)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.CmdBufferTooSmall;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (serverHeader.SendBuffersCount != 0 ||
|
|
|
|
serverHeader.ReceiveBuffersCount != 0 ||
|
|
|
|
serverHeader.ExchangeBuffersCount != 0)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Read receive list.
|
2019-01-18 17:26:39 -05:00
|
|
|
ulong[] receiveList = GetReceiveList(
|
|
|
|
clientMsg,
|
|
|
|
clientHeader.ReceiveListType,
|
|
|
|
clientHeader.ReceiveListOffset);
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Copy receive and exchange buffers.
|
2019-01-18 17:26:39 -05:00
|
|
|
clientResult = request.BufferDescriptorTable.CopyBuffersToClient(clientProcess.MemoryManager);
|
|
|
|
|
|
|
|
if (clientResult != KernelResult.Success)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return serverResult;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Copy header.
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.Memory.Write(clientMsg.DramAddress + 0, serverHeader.Word0);
|
|
|
|
KernelContext.Memory.Write(clientMsg.DramAddress + 4, serverHeader.Word1);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Copy handles.
|
2019-01-18 17:26:39 -05:00
|
|
|
uint offset;
|
|
|
|
|
|
|
|
if (serverHeader.HasHandles)
|
|
|
|
{
|
|
|
|
offset = 3;
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.Memory.Write(clientMsg.DramAddress + 8, serverHeader.Word2);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
if (serverHeader.HasPid)
|
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.Memory.Write(clientMsg.DramAddress + offset * 4, serverProcess.Pid);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
offset += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int index = 0; index < serverHeader.CopyHandlesCount; index++)
|
|
|
|
{
|
|
|
|
int newHandle = 0;
|
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
int handle = serverProcess.CpuMemory.Read<int>(serverMsg.Address + offset * 4);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
if (handle != 0)
|
|
|
|
{
|
|
|
|
GetCopyObjectHandle(serverThread, clientProcess, handle, out newHandle);
|
|
|
|
}
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.Memory.Write(clientMsg.DramAddress + offset * 4, newHandle);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int index = 0; index < serverHeader.MoveHandlesCount; index++)
|
|
|
|
{
|
|
|
|
int newHandle = 0;
|
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
int handle = serverProcess.CpuMemory.Read<int>(serverMsg.Address + offset * 4);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
if (handle != 0)
|
|
|
|
{
|
|
|
|
if (clientResult == KernelResult.Success)
|
|
|
|
{
|
|
|
|
clientResult = GetMoveObjectHandle(serverProcess, clientProcess, handle, out newHandle);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
serverProcess.HandleTable.CloseHandle(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.Memory.Write(clientMsg.DramAddress + offset * 4, newHandle);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
offset = 2;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Copy pointer/receive list buffers.
|
2019-01-18 17:26:39 -05:00
|
|
|
uint recvListDstOffset = 0;
|
|
|
|
|
|
|
|
for (int index = 0; index < serverHeader.PointerBuffersCount; index++)
|
|
|
|
{
|
2020-05-03 18:54:50 -04:00
|
|
|
ulong pointerDesc = serverProcess.CpuMemory.Read<ulong>(serverMsg.Address + offset * 4);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
PointerBufferDesc descriptor = new PointerBufferDesc(pointerDesc);
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
ulong recvListBufferAddress = 0;
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (descriptor.BufferSize != 0)
|
|
|
|
{
|
|
|
|
clientResult = GetReceiveListAddress(
|
|
|
|
descriptor,
|
|
|
|
clientMsg,
|
|
|
|
clientHeader.ReceiveListType,
|
|
|
|
serverHeader.MessageSizeInWords,
|
|
|
|
receiveList,
|
2020-07-17 00:19:07 -04:00
|
|
|
ref recvListDstOffset,
|
|
|
|
out recvListBufferAddress);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
if (clientResult != KernelResult.Success)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return serverResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
clientResult = clientProcess.MemoryManager.CopyDataFromCurrentProcess(
|
|
|
|
recvListBufferAddress,
|
|
|
|
descriptor.BufferSize,
|
|
|
|
MemoryState.IsPoolAllocated,
|
|
|
|
MemoryState.IsPoolAllocated,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.Read,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute.Uncached,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
descriptor.BufferAddress);
|
|
|
|
|
|
|
|
if (clientResult != KernelResult.Success)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return serverResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
ulong dstDescAddress = clientMsg.DramAddress + offset * 4;
|
|
|
|
|
|
|
|
ulong clientPointerDesc =
|
|
|
|
(recvListBufferAddress << 32) |
|
|
|
|
((recvListBufferAddress >> 20) & 0xf000) |
|
|
|
|
((recvListBufferAddress >> 30) & 0xffc0);
|
|
|
|
|
|
|
|
clientPointerDesc |= pointerDesc & 0xffff000f;
|
|
|
|
|
|
|
|
KernelContext.Memory.Write(dstDescAddress + 0, clientPointerDesc);
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
offset += 2;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Set send, receive and exchange buffer descriptors to zero.
|
2019-01-18 17:26:39 -05:00
|
|
|
uint totalBuffersCount =
|
|
|
|
serverHeader.SendBuffersCount +
|
|
|
|
serverHeader.ReceiveBuffersCount +
|
|
|
|
serverHeader.ExchangeBuffersCount;
|
|
|
|
|
|
|
|
for (int index = 0; index < totalBuffersCount; index++)
|
|
|
|
{
|
2020-05-03 18:54:50 -04:00
|
|
|
ulong dstDescAddress = clientMsg.DramAddress + offset * 4;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.Memory.Write(dstDescAddress + 0, 0);
|
|
|
|
KernelContext.Memory.Write(dstDescAddress + 4, 0);
|
|
|
|
KernelContext.Memory.Write(dstDescAddress + 8, 0);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
offset += 3;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Copy raw data.
|
2019-01-18 17:26:39 -05:00
|
|
|
if (serverHeader.RawDataSizeInWords != 0)
|
|
|
|
{
|
|
|
|
ulong copyDst = clientMsg.Address + offset * 4;
|
|
|
|
ulong copySrc = serverMsg.Address + offset * 4;
|
|
|
|
|
|
|
|
ulong copySize = serverHeader.RawDataSizeInWords * 4;
|
|
|
|
|
|
|
|
if (serverMsg.IsCustom || clientMsg.IsCustom)
|
|
|
|
{
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permission = clientMsg.IsCustom
|
|
|
|
? KMemoryPermission.None
|
|
|
|
: KMemoryPermission.Read;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
clientResult = clientProcess.MemoryManager.CopyDataFromCurrentProcess(
|
|
|
|
copyDst,
|
|
|
|
copySize,
|
|
|
|
MemoryState.IsPoolAllocated,
|
|
|
|
MemoryState.IsPoolAllocated,
|
|
|
|
permission,
|
|
|
|
MemoryAttribute.Uncached,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
copySrc);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
copyDst = clientProcess.MemoryManager.GetDramAddressFromVa(copyDst);
|
|
|
|
copySrc = serverProcess.MemoryManager.GetDramAddressFromVa(copySrc);
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.Memory.Copy(copyDst, copySrc, copySize);
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Unmap buffers from server.
|
2020-07-17 00:19:07 -04:00
|
|
|
FinishRequest(request, clientResult);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
return serverResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
private MessageHeader GetClientMessageHeader(Message clientMsg)
|
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
uint word0 = KernelContext.Memory.Read<uint>(clientMsg.DramAddress + 0);
|
|
|
|
uint word1 = KernelContext.Memory.Read<uint>(clientMsg.DramAddress + 4);
|
|
|
|
uint word2 = KernelContext.Memory.Read<uint>(clientMsg.DramAddress + 8);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
return new MessageHeader(word0, word1, word2);
|
|
|
|
}
|
|
|
|
|
|
|
|
private MessageHeader GetServerMessageHeader(Message serverMsg)
|
|
|
|
{
|
2020-12-09 17:20:05 -05:00
|
|
|
KProcess currentProcess = KernelStatic.GetCurrentProcess();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
uint word0 = currentProcess.CpuMemory.Read<uint>(serverMsg.Address + 0);
|
|
|
|
uint word1 = currentProcess.CpuMemory.Read<uint>(serverMsg.Address + 4);
|
|
|
|
uint word2 = currentProcess.CpuMemory.Read<uint>(serverMsg.Address + 8);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
return new MessageHeader(word0, word1, word2);
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
private KernelResult GetCopyObjectHandle(KThread srcThread, KProcess dstProcess, int srcHandle, out int dstHandle)
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
|
|
|
dstHandle = 0;
|
|
|
|
|
|
|
|
KProcess srcProcess = srcThread.Owner;
|
|
|
|
|
|
|
|
KAutoObject obj;
|
|
|
|
|
|
|
|
if (srcHandle == KHandleTable.SelfProcessHandle)
|
|
|
|
{
|
|
|
|
obj = srcProcess;
|
|
|
|
}
|
|
|
|
else if (srcHandle == KHandleTable.SelfThreadHandle)
|
|
|
|
{
|
|
|
|
obj = srcThread;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
obj = srcProcess.HandleTable.GetObject<KAutoObject>(srcHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj != null)
|
|
|
|
{
|
|
|
|
return dstProcess.HandleTable.GenerateHandle(obj, out dstHandle);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
private KernelResult GetMoveObjectHandle(KProcess srcProcess, KProcess dstProcess, int srcHandle, out int dstHandle)
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
|
|
|
dstHandle = 0;
|
|
|
|
|
|
|
|
KAutoObject obj = srcProcess.HandleTable.GetObject<KAutoObject>(srcHandle);
|
|
|
|
|
|
|
|
if (obj != null)
|
|
|
|
{
|
|
|
|
KernelResult result = dstProcess.HandleTable.GenerateHandle(obj, out dstHandle);
|
|
|
|
|
|
|
|
srcProcess.HandleTable.CloseHandle(srcHandle);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private ulong[] GetReceiveList(Message message, uint recvListType, uint recvListOffset)
|
|
|
|
{
|
|
|
|
int recvListSize = 0;
|
|
|
|
|
|
|
|
if (recvListType >= 3)
|
|
|
|
{
|
|
|
|
recvListSize = (int)recvListType - 2;
|
|
|
|
}
|
|
|
|
else if (recvListType == 2)
|
|
|
|
{
|
|
|
|
recvListSize = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong[] receiveList = new ulong[recvListSize];
|
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
ulong recvListAddress = message.DramAddress + recvListOffset;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
for (int index = 0; index < recvListSize; index++)
|
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
receiveList[index] = KernelContext.Memory.Read<ulong>(recvListAddress + (ulong)index * 8);
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return receiveList;
|
|
|
|
}
|
|
|
|
|
|
|
|
private KernelResult GetReceiveListAddress(
|
|
|
|
PointerBufferDesc descriptor,
|
|
|
|
Message message,
|
|
|
|
uint recvListType,
|
|
|
|
uint messageSizeInWords,
|
|
|
|
ulong[] receiveList,
|
|
|
|
ref uint dstOffset,
|
|
|
|
out ulong address)
|
|
|
|
{
|
|
|
|
ulong recvListBufferAddress = address = 0;
|
|
|
|
|
|
|
|
if (recvListType == 0)
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
else if (recvListType == 1 || recvListType == 2)
|
|
|
|
{
|
|
|
|
ulong recvListBaseAddr;
|
|
|
|
ulong recvListEndAddr;
|
|
|
|
|
|
|
|
if (recvListType == 1)
|
|
|
|
{
|
|
|
|
recvListBaseAddr = message.Address + messageSizeInWords * 4;
|
|
|
|
recvListEndAddr = message.Address + message.Size;
|
|
|
|
}
|
|
|
|
else /* if (recvListType == 2) */
|
|
|
|
{
|
|
|
|
ulong packed = receiveList[0];
|
|
|
|
|
|
|
|
recvListBaseAddr = packed & 0x7fffffffff;
|
|
|
|
|
|
|
|
uint size = (uint)(packed >> 48);
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
recvListEndAddr = recvListBaseAddr + size;
|
|
|
|
}
|
|
|
|
|
|
|
|
recvListBufferAddress = BitUtils.AlignUp(recvListBaseAddr + dstOffset, 0x10);
|
|
|
|
|
|
|
|
ulong endAddress = recvListBufferAddress + descriptor.BufferSize;
|
|
|
|
|
|
|
|
dstOffset = (uint)endAddress - (uint)recvListBaseAddr;
|
|
|
|
|
|
|
|
if (recvListBufferAddress + descriptor.BufferSize <= recvListBufferAddress ||
|
|
|
|
recvListBufferAddress + descriptor.BufferSize > recvListEndAddr)
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* if (recvListType > 2) */
|
|
|
|
{
|
|
|
|
if (descriptor.ReceiveIndex >= receiveList.Length)
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong packed = receiveList[descriptor.ReceiveIndex];
|
|
|
|
|
|
|
|
recvListBufferAddress = packed & 0x7fffffffff;
|
|
|
|
|
|
|
|
uint size = (uint)(packed >> 48);
|
|
|
|
|
|
|
|
if (recvListBufferAddress == 0 || size == 0 || size < descriptor.BufferSize)
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
address = recvListBufferAddress;
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CloseAllHandles(Message message, MessageHeader header, KProcess process)
|
|
|
|
{
|
|
|
|
if (header.HasHandles)
|
|
|
|
{
|
|
|
|
uint totalHandeslCount = header.CopyHandlesCount + header.MoveHandlesCount;
|
|
|
|
|
|
|
|
uint offset = 3;
|
|
|
|
|
|
|
|
if (header.HasPid)
|
|
|
|
{
|
2020-05-03 18:54:50 -04:00
|
|
|
process.CpuMemory.Write(message.Address + offset * 4, 0L);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
offset += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int index = 0; index < totalHandeslCount; index++)
|
|
|
|
{
|
2020-05-03 18:54:50 -04:00
|
|
|
int handle = process.CpuMemory.Read<int>(message.Address + offset * 4);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
if (handle != 0)
|
|
|
|
{
|
|
|
|
process.HandleTable.CloseHandle(handle);
|
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
process.CpuMemory.Write(message.Address + offset * 4, 0);
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool IsSignaled()
|
|
|
|
{
|
|
|
|
if (_parent.ClientSession.State != ChannelState.Open)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _requests.Count != 0 && _activeRequest == null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Destroy()
|
|
|
|
{
|
|
|
|
_parent.DisconnectServer();
|
|
|
|
|
|
|
|
CancelAllRequestsServerDisconnected();
|
|
|
|
|
|
|
|
_parent.DecrementReferenceCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CancelAllRequestsServerDisconnected()
|
|
|
|
{
|
|
|
|
foreach (KSessionRequest request in IterateWithRemovalOfAllRequests())
|
|
|
|
{
|
2020-07-17 00:19:07 -04:00
|
|
|
FinishRequest(request, KernelResult.PortRemoteClosed);
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CancelAllRequestsClientDisconnected()
|
|
|
|
{
|
|
|
|
foreach (KSessionRequest request in IterateWithRemovalOfAllRequests())
|
|
|
|
{
|
|
|
|
if (request.ClientThread.ShallBeTerminated ||
|
|
|
|
request.ClientThread.SchedFlags == ThreadSchedState.TerminationPending)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Client sessions can only be disconnected on async requests (because
|
|
|
|
// the client would be otherwise blocked waiting for the response), so
|
|
|
|
// we only need to handle the async case here.
|
2019-01-18 17:26:39 -05:00
|
|
|
if (request.AsyncEvent != null)
|
|
|
|
{
|
|
|
|
SendResultToAsyncRequestClient(request, KernelResult.PortRemoteClosed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WakeServerThreads(KernelResult.PortRemoteClosed);
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerable<KSessionRequest> IterateWithRemovalOfAllRequests()
|
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Enter();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
if (_activeRequest != null)
|
|
|
|
{
|
|
|
|
KSessionRequest request = _activeRequest;
|
|
|
|
|
|
|
|
_activeRequest = null;
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Leave();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
yield return request;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Leave();
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
while (DequeueRequest(out KSessionRequest request))
|
|
|
|
{
|
|
|
|
yield return request;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool DequeueRequest(out KSessionRequest request)
|
|
|
|
{
|
|
|
|
request = null;
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Enter();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
bool hasRequest = _requests.First != null;
|
|
|
|
|
|
|
|
if (hasRequest)
|
|
|
|
{
|
|
|
|
request = _requests.First.Value;
|
|
|
|
|
|
|
|
_requests.RemoveFirst();
|
|
|
|
}
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Leave();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
return hasRequest;
|
|
|
|
}
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
private void FinishRequest(KSessionRequest request, KernelResult result)
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
|
|
|
KProcess clientProcess = request.ClientThread.Owner;
|
|
|
|
KProcess serverProcess = request.ServerProcess;
|
|
|
|
|
|
|
|
KernelResult unmapResult = KernelResult.Success;
|
|
|
|
|
|
|
|
if (serverProcess != null)
|
|
|
|
{
|
|
|
|
unmapResult = request.BufferDescriptorTable.UnmapServerBuffers(serverProcess.MemoryManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unmapResult == KernelResult.Success)
|
|
|
|
{
|
|
|
|
request.BufferDescriptorTable.RestoreClientBuffers(clientProcess.MemoryManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
WakeClientThread(request, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WakeClientThread(KSessionRequest request, KernelResult result)
|
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// Wait client thread waiting for a response for the given request.
|
2019-01-18 17:26:39 -05:00
|
|
|
if (request.AsyncEvent != null)
|
|
|
|
{
|
|
|
|
SendResultToAsyncRequestClient(request, result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Enter();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
WakeAndSetResult(request.ClientThread, result);
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Leave();
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SendResultToAsyncRequestClient(KSessionRequest request, KernelResult result)
|
|
|
|
{
|
|
|
|
KProcess clientProcess = request.ClientThread.Owner;
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
ulong address = clientProcess.MemoryManager.GetDramAddressFromVa(request.CustomCmdBuffAddr);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
KernelContext.Memory.Write<ulong>(address, 0);
|
|
|
|
KernelContext.Memory.Write(address + 8, (int)result);
|
|
|
|
}
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
clientProcess.MemoryManager.UnborrowIpcBuffer(request.CustomCmdBuffAddr, request.CustomCmdBuffSize);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
request.AsyncEvent.Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WakeServerThreads(KernelResult result)
|
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// Wake all server threads waiting for requests.
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Enter();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
foreach (KThread thread in WaitingThreads)
|
|
|
|
{
|
|
|
|
WakeAndSetResult(thread, result);
|
|
|
|
}
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
KernelContext.CriticalSection.Leave();
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private void WakeAndSetResult(KThread thread, KernelResult result)
|
|
|
|
{
|
|
|
|
if ((thread.SchedFlags & ThreadSchedState.LowMask) == ThreadSchedState.Paused)
|
|
|
|
{
|
|
|
|
thread.SignaledObj = null;
|
|
|
|
thread.ObjSyncResult = result;
|
|
|
|
|
|
|
|
thread.Reschedule(ThreadSchedState.Running);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|