0
0
Fork 0
This repository has been archived on 2024-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
ryujinx-final/Ryujinx.HLE/HOS/Services/Nv/NvDrvServices/NvHostAsGpu/Types/AddressSpaceContext.cs
Thomas Guillemard 9426ef3f06 Rewrite nvservices (#800)
* Start rewriting nvservices internals

TODO:

- nvgpu device interface
- nvhost generic device interface

* Some clean up and fixes

- Make sure to remove the fd of a closed channel.
- NvFileDevice now doesn't implement Disposable as it was never used.
- Rename NvHostCtrlGetConfigurationArgument to GetConfigurationArguments
to follow calling convention.
- Make sure to check every ioctls magic.

* Finalize migration for ioctl standard variant

TODO: ioctl2 migration

* Implement SubmitGpfifoEx and fix nvdec

* Implement Ioctl3

* Implement some ioctl3 required by recent games

* Remove unused code and outdated comments

* Return valid event handles with QueryEvent

Also add an exception for unimplemented event ids.

This commit doesn't implement accurately the events, this only define
different events for different event ids.

* Rename all occurance of FileDevice to DeviceFile

* Restub SetClientPid to not cause regressions

* Address comments

* Remove GlobalStateTable

* Address comments

* Align variables in ioctl3

* Some missing alignments

* GetVaRegionsArguments realign

* Make Owner public in NvDeviceFile

* Address LDj3SNuD's comments
2019-11-03 09:47:56 +11:00

204 lines
5.2 KiB
C#

using ARMeilleure.Memory;
using Ryujinx.Graphics.Memory;
using Ryujinx.HLE.HOS.Kernel.Process;
using System;
using System.Collections.Generic;
using System.Text;
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostAsGpu.Types
{
class AddressSpaceContext
{
public NvGpuVmm Vmm { get; private set; }
private class Range
{
public ulong Start { get; private set; }
public ulong End { get; private set; }
public Range(long position, long size)
{
Start = (ulong)position;
End = (ulong)size + Start;
}
}
private class MappedMemory : Range
{
public long PhysicalAddress { get; private set; }
public bool VaAllocated { get; private set; }
public MappedMemory(
long position,
long size,
long physicalAddress,
bool vaAllocated) : base(position, size)
{
PhysicalAddress = physicalAddress;
VaAllocated = vaAllocated;
}
}
private SortedList<long, Range> _maps;
private SortedList<long, Range> _reservations;
public AddressSpaceContext(KProcess process)
{
Vmm = new NvGpuVmm(process.CpuMemory);
_maps = new SortedList<long, Range>();
_reservations = new SortedList<long, Range>();
}
public bool ValidateFixedBuffer(long position, long size)
{
long mapEnd = position + size;
// Check if size is valid (0 is also not allowed).
if ((ulong)mapEnd <= (ulong)position)
{
return false;
}
// Check if address is page aligned.
if ((position & NvGpuVmm.PageMask) != 0)
{
return false;
}
// Check if region is reserved.
if (BinarySearch(_reservations, position) == null)
{
return false;
}
// Check for overlap with already mapped buffers.
Range map = BinarySearchLt(_maps, mapEnd);
if (map != null && map.End > (ulong)position)
{
return false;
}
return true;
}
public void AddMap(
long position,
long size,
long physicalAddress,
bool vaAllocated)
{
_maps.Add(position, new MappedMemory(position, size, physicalAddress, vaAllocated));
}
public bool RemoveMap(long position, out long size)
{
size = 0;
if (_maps.Remove(position, out Range value))
{
MappedMemory map = (MappedMemory)value;
if (map.VaAllocated)
{
size = (long)(map.End - map.Start);
}
return true;
}
return false;
}
public bool TryGetMapPhysicalAddress(long position, out long physicalAddress)
{
Range map = BinarySearch(_maps, position);
if (map != null)
{
physicalAddress = ((MappedMemory)map).PhysicalAddress;
return true;
}
physicalAddress = 0;
return false;
}
public void AddReservation(long position, long size)
{
_reservations.Add(position, new Range(position, size));
}
public bool RemoveReservation(long position)
{
return _reservations.Remove(position);
}
private Range BinarySearch(SortedList<long, Range> lst, long position)
{
int left = 0;
int right = lst.Count - 1;
while (left <= right)
{
int size = right - left;
int middle = left + (size >> 1);
Range rg = lst.Values[middle];
if ((ulong)position >= rg.Start && (ulong)position < rg.End)
{
return rg;
}
if ((ulong)position < rg.Start)
{
right = middle - 1;
}
else
{
left = middle + 1;
}
}
return null;
}
private Range BinarySearchLt(SortedList<long, Range> lst, long position)
{
Range ltRg = null;
int left = 0;
int right = lst.Count - 1;
while (left <= right)
{
int size = right - left;
int middle = left + (size >> 1);
Range rg = lst.Values[middle];
if ((ulong)position < rg.Start)
{
right = middle - 1;
}
else
{
left = middle + 1;
if ((ulong)position > rg.Start)
{
ltRg = rg;
}
}
}
return ltRg;
}
}
}