[rpcsx-os] vm: implement virtual query

This commit is contained in:
DH 2023-10-17 23:25:06 +03:00
parent 1743d6ebf2
commit 288f7fcc7a
8 changed files with 278 additions and 124 deletions

View file

@ -17,19 +17,17 @@ struct AllocateDirectMemoryArgs {
std::uint32_t memoryType;
};
static constexpr auto dmemSize = 8ul * 1024 * 1024 * 1024;
static constexpr auto dmemSize = 8ull * 1024 * 1024 * 1024;
// static const std::uint64_t nextOffset = 0;
// static const std::uint64_t memBeginAddress = 0xfe0000000;
orbis::ErrorCode DmemDevice::mmap(void **address, std::uint64_t len,
std::int32_t memoryType, std::int32_t prot,
std::int32_t flags,
std::int32_t prot, std::int32_t flags,
std::int64_t directMemoryStart) {
auto result =
rx::vm::map(*address, len, prot, flags, 0, this, directMemoryStart);
auto result = rx::vm::map(*address, len, prot, flags);
ORBIS_LOG_WARNING("dmem mmap", index, directMemoryStart, prot, flags,
memoryType, result);
ORBIS_LOG_WARNING("dmem mmap", index, directMemoryStart, prot, flags, result);
if (result == (void *)-1) {
return orbis::ErrorCode::NOMEM; // TODO
}
@ -45,8 +43,8 @@ static orbis::ErrorCode dmem_ioctl(orbis::File *file, std::uint64_t request,
std::lock_guard lock(device->mtx);
switch (request) {
case 0x4008800a: // get size
ORBIS_LOG_ERROR("dmem getTotalSize", device->index, argp);
*(std::uint64_t *)argp = dmemSize;
ORBIS_LOG_WARNING("dmem getTotalSize", device->index, argp);
*(std::uint64_t *)argp = device->dmemTotalSize;
return {};
case 0xc0208016: { // get available size
@ -59,32 +57,21 @@ static orbis::ErrorCode dmem_ioctl(orbis::File *file, std::uint64_t request,
auto args = reinterpret_cast<Args *>(argp);
ORBIS_LOG_ERROR("dmem getAvaiableSize", device->index, argp, dmemSize,
device->nextOffset, dmemSize - device->nextOffset);
args->searchStart = device->nextOffset;
args->size = dmemSize - device->nextOffset;
return device->queryMaxFreeChunkSize(&args->searchStart, args->searchEnd, args->alignment, &args->size);
ORBIS_LOG_WARNING("dmem getAvailableSize", device->index, argp, dmemSize);
// args->searchStart = device->nextOffset;
// args->size = dmemSize - device->nextOffset;
return {};
}
case 0xc0288011:
case 0xc0288001: { // sceKernelAllocateDirectMemory
auto args = reinterpret_cast<AllocateDirectMemoryArgs *>(argp);
auto alignedOffset =
(device->nextOffset + args->alignment - 1) & ~(args->alignment - 1);
ORBIS_LOG_ERROR("dmem allocateDirectMemory", device->index,
args->searchStart, args->searchEnd, args->len,
args->alignment, args->memoryType, alignedOffset);
if (alignedOffset + args->len > dmemSize) {
ORBIS_LOG_ERROR("dmem allocateDirectMemory: out of memory", alignedOffset,
args->len, alignedOffset + args->len);
return orbis::ErrorCode::NOMEM;
}
args->searchStart = alignedOffset;
device->nextOffset = alignedOffset + args->len;
return {};
return device->allocate(&args->searchStart, args->searchEnd, args->len,
args->alignment, args->memoryType);
}
case 0x80108002: { // sceKernelReleaseDirectMemory
@ -95,29 +82,11 @@ static orbis::ErrorCode dmem_ioctl(orbis::File *file, std::uint64_t request,
auto args = reinterpret_cast<Args *>(argp);
ORBIS_LOG_TODO("dmem releaseDirectMemory", device->index, args->address,
args->size);
// std::fflush(stdout);
//__builtin_trap();
return {};
}
ORBIS_LOG_WARNING("dmem releaseDirectMemory", device->index, args->address,
args->size);
case 0xc0288011: {
auto args = reinterpret_cast<AllocateDirectMemoryArgs *>(argp);
// TODO
auto alignedOffset =
(device->nextOffset + args->alignment - 1) & ~(args->alignment - 1);
ORBIS_LOG_ERROR("dmem allocateMainDirectMemory", device->index,
args->searchStart, args->searchEnd, args->len,
args->alignment, args->memoryType, alignedOffset);
if (alignedOffset + args->len > dmemSize) {
return orbis::ErrorCode::NOMEM;
}
args->searchStart = alignedOffset;
device->nextOffset = alignedOffset + args->len;
device->allocations.map(args->address, args->address + args->size,
{.memoryType = 0});
return {};
}
}
@ -132,18 +101,7 @@ static orbis::ErrorCode dmem_mmap(orbis::File *file, void **address,
std::int32_t flags, std::int64_t offset,
orbis::Thread *thread) {
auto device = static_cast<DmemDevice *>(file->device.get());
auto target = device->memBeginAddress + offset;
ORBIS_LOG_WARNING("dmem mmap", device->index, offset, target);
auto result =
rx::vm::map(reinterpret_cast<void *>(target), size, prot, flags);
if (result == (void *)-1) {
return orbis::ErrorCode::INVAL; // TODO
}
*address = result;
return {};
return device->mmap(address, size, prot, flags, offset);
}
static const orbis::FileOps ops = {
@ -151,6 +109,113 @@ static const orbis::FileOps ops = {
.mmap = dmem_mmap,
};
orbis::ErrorCode DmemDevice::allocate(std::uint64_t *start,
std::uint64_t searchEnd,
std::uint64_t len,
std::uint64_t alignment,
std::uint32_t memoryType) {
std::size_t offset = *start;
while (offset < searchEnd) {
offset += alignment - 1;
offset &= ~(alignment - 1);
if (offset + len > dmemTotalSize) {
ORBIS_LOG_ERROR("dmem: failed to allocate direct memory: out of memory",
*start, searchEnd, len, alignment, memoryType, offset);
return orbis::ErrorCode::AGAIN;
}
auto it = allocations.lowerBound(offset);
if (it != allocations.end()) {
auto allocation = *it;
if (allocation.payload.memoryType == 0) {
if (offset < allocation.beginAddress) {
offset = allocation.beginAddress + alignment - 1;
offset &= ~(alignment - 1);
}
if (offset + len >= allocation.endAddress) {
offset = allocation.endAddress;
continue;
}
} else {
if (offset + len > allocation.beginAddress) {
offset = allocation.endAddress;
continue;
}
}
}
allocations.map(offset, offset + len,
{
.memoryType = memoryType,
});
ORBIS_LOG_WARNING("dmem: allocated direct memory", *start, searchEnd, len,
alignment, memoryType, offset);
*start = offset;
return {};
}
ORBIS_LOG_ERROR("dmem: failed to allocate direct memory", *start, searchEnd,
len, alignment, memoryType, offset);
return orbis::ErrorCode::AGAIN;
}
orbis::ErrorCode DmemDevice::queryMaxFreeChunkSize(std::uint64_t *start,
std::uint64_t searchEnd,
std::uint64_t alignment,
std::uint64_t *size) {
std::size_t offset = *start;
std::size_t resultSize = 0;
std::size_t resultOffset = 0;
while (offset < searchEnd) {
offset += alignment - 1;
offset &= ~(alignment - 1);
if (offset >= dmemTotalSize) {
break;
}
auto it = allocations.lowerBound(offset);
if (it == allocations.end()) {
if (resultSize < dmemTotalSize - offset) {
resultSize = dmemTotalSize - offset;
resultOffset = offset;
}
break;
}
auto allocation = *it;
if (allocation.payload.memoryType == 0) {
if (offset < allocation.beginAddress) {
offset = allocation.beginAddress + alignment - 1;
offset &= ~(alignment - 1);
}
if (allocation.endAddress > offset &&
resultSize < allocation.endAddress - offset) {
resultSize = allocation.endAddress - offset;
resultOffset = offset;
}
} else if (offset > allocation.beginAddress &&
resultSize < offset - allocation.beginAddress) {
resultSize = offset - allocation.beginAddress;
resultOffset = offset;
}
offset = allocation.endAddress;
}
*start = resultOffset;
*size = resultSize;
ORBIS_LOG_WARNING("dmem queryMaxFreeChunkSize", resultOffset, resultSize);
return{};
}
orbis::ErrorCode DmemDevice::open(orbis::Ref<orbis::File> *file,
const char *path, std::uint32_t flags,
std::uint32_t mode, orbis::Thread *thread) {
@ -164,7 +229,6 @@ orbis::ErrorCode DmemDevice::open(orbis::Ref<orbis::File> *file,
IoDevice *createDmemCharacterDevice(int index) {
auto *newDevice = orbis::knew<DmemDevice>();
newDevice->index = index;
newDevice->nextOffset = 0;
newDevice->memBeginAddress = 0xf'e000'0000 + dmemSize * index;
newDevice->dmemTotalSize = dmemSize;
return newDevice;
}