Skip to content

Commit

Permalink
file_system: Simplify memory invalidation and add a few missed cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
squidbus committed Dec 11, 2024
1 parent 1178c14 commit 6f79221
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 44 deletions.
32 changes: 13 additions & 19 deletions src/core/libraries/kernel/file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ int PS4_SYSV_ABI posix_close(int d) {
return result;
}

size_t PS4_SYSV_ABI sceKernelWrite(int d, const void* buf, size_t nbytes) {
s64 PS4_SYSV_ABI sceKernelWrite(int d, const void* buf, size_t nbytes) {
auto* h = Common::Singleton<Core::FileSys::HandleTable>::Instance();
auto* file = h->GetFile(d);
if (file == nullptr) {
Expand Down Expand Up @@ -246,6 +246,15 @@ int PS4_SYSV_ABI sceKernelUnlink(const char* path) {
return ORBIS_OK;
}

size_t ReadFile(Common::FS::IOFile& file, void* buf, size_t nbytes) {
const auto* memory = Core::Memory::Instance();
// Invalidate up to the actual number of bytes that could be read.
const auto remaining = file.GetSize() - file.Tell();
memory->InvalidateMemory(reinterpret_cast<VAddr>(buf), std::min<u64>(nbytes, remaining));

return file.ReadRaw<u8>(buf, nbytes);
}

size_t PS4_SYSV_ABI _readv(int d, const SceKernelIovec* iov, int iovcnt) {
auto* h = Common::Singleton<Core::FileSys::HandleTable>::Instance();
auto* file = h->GetFile(d);
Expand All @@ -264,7 +273,7 @@ size_t PS4_SYSV_ABI _readv(int d, const SceKernelIovec* iov, int iovcnt) {
}
size_t total_read = 0;
for (int i = 0; i < iovcnt; i++) {
total_read += file->f.ReadRaw<u8>(iov[i].iov_base, iov[i].iov_len);
total_read += ReadFile(file->f, iov[i].iov_base, iov[i].iov_len);
}
return total_read;
}
Expand Down Expand Up @@ -348,16 +357,10 @@ s64 PS4_SYSV_ABI sceKernelRead(int d, void* buf, size_t nbytes) {
}

std::scoped_lock lk{file->m_mutex};
const auto* memory = Core::Memory::Instance();
if (file->type == Core::FileSys::FileType::Device) {
// Size is not known, invalidate the whole buffer.
memory->InvalidateMemory(reinterpret_cast<VAddr>(buf), nbytes);
return file->device->read(buf, nbytes);
}
// Invalidate up to the actual number of bytes that could be read.
const auto remaining = file->f.GetSize() - file->f.Tell();
memory->InvalidateMemory(reinterpret_cast<VAddr>(buf), std::min<u64>(nbytes, remaining));
return file->f.ReadRaw<u8>(buf, nbytes);
return ReadFile(file->f, buf, nbytes);
}

int PS4_SYSV_ABI posix_read(int d, void* buf, size_t nbytes) {
Expand Down Expand Up @@ -533,12 +536,7 @@ s64 PS4_SYSV_ABI sceKernelPreadv(int d, SceKernelIovec* iov, int iovcnt, s64 off
}

std::scoped_lock lk{file->m_mutex};
const auto* memory = Core::Memory::Instance();
if (file->type == Core::FileSys::FileType::Device) {
// Size is not known, invalidate all buffers.
for (int i = 0; i < iovcnt; i++) {
memory->InvalidateMemory(reinterpret_cast<VAddr>(iov[i].iov_base), iov[i].iov_len);
}
return file->device->preadv(iov, iovcnt, offset);
}

Expand All @@ -552,11 +550,7 @@ s64 PS4_SYSV_ABI sceKernelPreadv(int d, SceKernelIovec* iov, int iovcnt, s64 off
}
size_t total_read = 0;
for (int i = 0; i < iovcnt; i++) {
// Invalidate up to the actual number of bytes that could be read.
const auto remaining = file->f.GetSize() - file->f.Tell();
memory->InvalidateMemory(reinterpret_cast<VAddr>(iov[i].iov_base),
std::min<u64>(iov[i].iov_len, remaining));
total_read += file->f.ReadRaw<u8>(iov[i].iov_base, iov[i].iov_len);
total_read += ReadFile(file->f, iov[i].iov_base, iov[i].iov_len);
}
return total_read;
}
Expand Down
3 changes: 3 additions & 0 deletions src/core/libraries/kernel/file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ constexpr int ORBIS_KERNEL_O_DSYNC = 0x1000;
constexpr int ORBIS_KERNEL_O_DIRECT = 0x00010000;
constexpr int ORBIS_KERNEL_O_DIRECTORY = 0x00020000;

s64 PS4_SYSV_ABI sceKernelWrite(int d, const void* buf, size_t nbytes);
s64 PS4_SYSV_ABI sceKernelRead(int d, void* buf, size_t nbytes);

void RegisterFileSystem(Core::Loader::SymbolsResolver* sym);

} // namespace Libraries::Kernel
26 changes: 2 additions & 24 deletions src/core/libraries/kernel/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,33 +133,11 @@ void PS4_SYSV_ABI sceLibcHeapGetTraceInfo(HeapInfoInfo* info) {
}

s64 PS4_SYSV_ABI ps4__write(int d, const char* buf, std::size_t nbytes) {
auto* h = Common::Singleton<Core::FileSys::HandleTable>::Instance();
auto* file = h->GetFile(d);
if (file == nullptr) {
return ORBIS_KERNEL_ERROR_EBADF;
}
std::scoped_lock lk{file->m_mutex};
if (file->type == Core::FileSys::FileType::Device) {
return file->device->write(buf, nbytes);
}
return file->f.WriteRaw<u8>(buf, nbytes);
return sceKernelWrite(d, buf, nbytes);
}

s64 PS4_SYSV_ABI ps4__read(int d, void* buf, u64 nbytes) {
if (d == 0) {
return static_cast<s64>(
strlen(std::fgets(static_cast<char*>(buf), static_cast<int>(nbytes), stdin)));
}
auto* h = Common::Singleton<Core::FileSys::HandleTable>::Instance();
auto* file = h->GetFile(d);
if (file == nullptr) {
return ORBIS_KERNEL_ERROR_EBADF;
}
std::scoped_lock lk{file->m_mutex};
if (file->type == Core::FileSys::FileType::Device) {
return file->device->read(buf, nbytes);
}
return file->f.ReadRaw<u8>(buf, nbytes);
return sceKernelRead(d, buf, nbytes);
}

struct OrbisKernelUuid {
Expand Down
4 changes: 3 additions & 1 deletion src/core/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,9 @@ void MemoryManager::NameVirtualRange(VAddr virtual_addr, size_t size, std::strin
}

void MemoryManager::InvalidateMemory(const VAddr addr, const u64 size) const {
rasterizer->InvalidateMemory(addr, size);
if (rasterizer) {
rasterizer->InvalidateMemory(addr, size);
}
}

VAddr MemoryManager::SearchFree(VAddr virtual_addr, size_t size, u32 alignment) {
Expand Down

0 comments on commit 6f79221

Please sign in to comment.