diff --git a/src/core/libraries/kernel/file_system.cpp b/src/core/libraries/kernel/file_system.cpp index 3e13195c4e..5ba9976c60 100644 --- a/src/core/libraries/kernel/file_system.cpp +++ b/src/core/libraries/kernel/file_system.cpp @@ -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::Instance(); auto* file = h->GetFile(d); if (file == nullptr) { @@ -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(buf), std::min(nbytes, remaining)); + + return file.ReadRaw(buf, nbytes); +} + size_t PS4_SYSV_ABI _readv(int d, const SceKernelIovec* iov, int iovcnt) { auto* h = Common::Singleton::Instance(); auto* file = h->GetFile(d); @@ -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(iov[i].iov_base, iov[i].iov_len); + total_read += ReadFile(file->f, iov[i].iov_base, iov[i].iov_len); } return total_read; } @@ -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(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(buf), std::min(nbytes, remaining)); - return file->f.ReadRaw(buf, nbytes); + return ReadFile(file->f, buf, nbytes); } int PS4_SYSV_ABI posix_read(int d, void* buf, size_t nbytes) { @@ -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(iov[i].iov_base), iov[i].iov_len); - } return file->device->preadv(iov, iovcnt, offset); } @@ -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(iov[i].iov_base), - std::min(iov[i].iov_len, remaining)); - total_read += file->f.ReadRaw(iov[i].iov_base, iov[i].iov_len); + total_read += ReadFile(file->f, iov[i].iov_base, iov[i].iov_len); } return total_read; } diff --git a/src/core/libraries/kernel/file_system.h b/src/core/libraries/kernel/file_system.h index dcbb3957d8..6443962ffa 100644 --- a/src/core/libraries/kernel/file_system.h +++ b/src/core/libraries/kernel/file_system.h @@ -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 diff --git a/src/core/libraries/kernel/kernel.cpp b/src/core/libraries/kernel/kernel.cpp index bda4462578..b05c96fad9 100644 --- a/src/core/libraries/kernel/kernel.cpp +++ b/src/core/libraries/kernel/kernel.cpp @@ -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::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(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( - strlen(std::fgets(static_cast(buf), static_cast(nbytes), stdin))); - } - auto* h = Common::Singleton::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(buf, nbytes); + return sceKernelRead(d, buf, nbytes); } struct OrbisKernelUuid { diff --git a/src/core/memory.cpp b/src/core/memory.cpp index c130e734f0..41db7df4b5 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -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) {