Commit Graph

375 Commits

Author SHA1 Message Date
Marcin Mikołajczyk
53181b005c Handle null event flags in cancel and clear (#3530) 2025-09-06 00:05:43 +03:00
Stephen Miller
bcea7a02c3 Return EINVAL if mmap is called with length 0 (#3496)
Hit by some multimedia apps
2025-08-31 16:14:51 -07:00
Marcin Mikołajczyk
be8c35eef1 Implement send/recvmsg (#3487)
* Implement send/recvmsg

* Windows

* fixed windows

* cleanups

* updated

* suggestions

---------

Co-authored-by: georgemoralis <giorgosmrls@gmail.com>
2025-09-01 01:02:26 +03:00
squidbus
af9947a862 semaphore: Fix determining wait status when canceled/deleted (#3490) 2025-08-31 14:39:57 -07:00
squidbus
52d2c4ddc1 semaphore: Invert priority order. (#3488)
* semaphore: Invert priority order.

* playgo: Lower logs for some spammed functions.
2025-08-30 19:24:58 -07:00
Stephen Miller
1b2ac916f1 Filesystem: Abstract handling of directory files (#3455)
* Rename SceKernelIovec to OrbisKernelIovec

Fixes the naming scheme to match the rest of the project.

* Type fixups

Noticed this while working on the actual thing this PR is about.

* More type fixups

* Update file_system.cpp

* Abstract directory handling

* Clang

* Directory fstat

* Fix dirent loading logic

* PfsDirectory size

Seemed to be hardcoded when I ran tests, so I've hardcoded it here.
Also fixed up the reclen-aligning logic based on hardware observations.

* GetDents cleanup

Bring back the bytes < 512 error return from before, as that's still something that can be checked for out here.
I've also swapped the file type stuff to run from a switch case, so that the check for invalid file type can be used as the default case

* Support reading directories

* getdents

For PfsDirectory, getdents behaves like read does on normal directories. Since dirents are stored internally with a different struct, this means getdents has to convert the dirents before returning data.
For NormalDirectory, getdents is identical to read, so it can just call read and set the basep output.

* Directory readv

* Directory preadv

Since the file mutex is locked before these calls, messing with dirents_index like this shouldn't cause any weird side effects.

* return ORBIS_OK instead of 0

to better align with our coding standards.

* Directory lseek

* Un-modify CMakePresets.json

I keep this modified locally for Linux, but accidentally pushed it.

* Clang

* Fix mac compile

* Potential windows compile fix?

* Filename fix

* Fix normal directory d_reclen

* Comment cleanup

* Remove unnecessary dirent conversion logic

On real hardware, the records are still returned with the same reclen, despite the change in structure.

* PfsDirectory dirents_index fixes

Some weird stuff happens once you reach eof on directories.
Thankfully, PfsDirectories are rather tame in this regard.

* Change comment

* Rewrite normal directory reads

The logic for these seems to behave like a normal file, so instead of tracking a dirents_index, keep an internal buffer representing the file contents, and copy that to output buffers as needed.

* Update pfs_directory.cpp

* Clang

* Fix normal dirents

When rewriting the code, I forgot to account for the increased reclen value for the last dirent in the buffer.

* PfsDirectory::lseek fixes

Based on some additional tests, it seems like lseek unconditionally returns dirents_index, not the actual file position.
Also fixed some bugs with the logic for calculating the proper offset when games do wonky things, and fixed a potential area where games could crash.

* Downgrade stat and fstat log to debug

These functions can get pretty spammy.

* PfsDirectory: Properly track if end of file is reached

Using the metric `dirents_index < directory_content_size` fails when `directory_content_size` is larger than the actual directory size we report.
Since, from what I can tell, PfsDirectories shouldn't ever report more than 0x10000 bytes for size, this change is necessary.

* Revert "PfsDirectory: Properly track if end of file is reached"

I need to do some hardware tests to see if all this excess logic is actually necessary.

* Fix PfsDirectory directory_size

Turns out, if your game has over 1000 files in a folder, it will actually cause the size to increase.

* Update copyright date

* Move devices and directories into file_sys

I've also updated the copyright dates on all these files.

* C++ style type casts

* Remove unnecessary memset

* Use a vector for the data buffer

Simplifies logic for freeing the buffer, based on review suggestions.

* Fix potential oob array access

* Change type casts in Create function

* Clang

* std::memcpy instead of memcpy

* NormalDirectory::lseek cleanup

* Create constants for directory alignment values.

* Use const wherever possible

* Includes cleanup
2025-08-25 23:41:24 +03:00
Stephen Miller
1c0a5c60ea Memory: Align size and address in posix_munmap (#3418)
* Properly align address and size in munmap

Based on observations of FreeBSD source code, fixes a Windows-related memory issue in War Thunder (CUSA00224)

* Format len and phys_addr in mmap

This should make logs slightly easier to understand, since we format these parameters in other memory calls.

* Update memory.cpp
2025-08-15 12:21:59 +03:00
Stephen Miller
a285543134 Filesystem: Directory-related fixes (#3416)
* Various GetDents fixes

Fixed return and parameter types, and made the function return all the entries that will fit in nbytes during one call.

* Fstat dir stub changes

Changes the returned statistics to match what my PS4 tests generally show.

* Stat dir stub changes

To match my fstat changes
2025-08-14 14:50:20 +03:00
Marcin Mikołajczyk
befc5ec17b select (#3353)
* select

* select for windows

* fixed windows only function

* windows error converts

* fixed up

* fixed compile

* another implementation for windows

* draft rewrite for windows

* implementation for windows

* added some debugging info

* more debugging

* extensive log

* Windows: Add device files to output fd_sets

Cyberpunk 2077 breaks without this.
Also added some formatting changes, to bring the implementation closer to our typical coding standards.

* Clang

* Formatting + cleanup

Removed some excessive logging used for debugging, and did some cleanup on the non-Windows implementation.

---------

Co-authored-by: georgemoralis <giorgosmrls@gmail.com>
Co-authored-by: Stephen Miller <millerste004@gmail.com>
2025-08-08 00:20:49 +03:00
kalaposfos13
5b46216bae Make libSceRtc fully HLE (#3330)
* Add stubbed libkernel time functions

* Implement remaining stubbed Rtc functions

* Move Rtc from the optionally HLE loading part to the always loaded part

* Remove Rtc from the README list of LLE modules

* Mfw last second hotfix I noticed while double checking that everything's good before opening the PR
2025-08-06 20:08:26 +03:00
Stephen Miller
074dfe2571 Update file_system.cpp (#3388) 2025-08-05 19:58:23 +03:00
psucien
afb9e220aa libkernel: more network functions for OpenOrbis compatibility (#3373) 2025-08-05 11:25:18 +03:00
Stephen Miller
6c34b86add Net: Fix various socket-related issues (#3347)
* Store platform-specific level in a separate variable

So the level logged in the getsockopt/setsockopt unreachable is actually useful in cases where the level is unknown.

* Define ORBIS_NET_IPPROTO_IPV6

Not implemented yet, but since it's known we might as well add it.

* Fix error codes

Our libSceNet code expects accurate ORBIS_NET_E* errors, while the sys_net code returns ORBIS_NET_ERROR_* errors.

* Remove duplicate getsockname implementation

* Use separate mutex for ReceivePacket calls

Calls to ReceivePacket shouldn't block other socket functions, and allowing them to block these functions frequently causes deadlocks in games that use multiple threads for socket behaviors.
That said, concurrent receives are still a potential issue, so the function should still have a mutex.

* Add missing error codes

* Clang

* Minor nit

Not sure why these were left separate from the rest of the net errnos

* Set __Error() in ConvertReturnErrorCode

Because the new error values are positive, the logic of "negative return is an error" doesn't work anymore. The easiest fix, while retaining corrected error values, is to just set __Error() in ConvertReturnErrorCode, and have that return -1 instead.
I also added some formatting fixes here too.

* Set errno on stubbed P2P socket error returns.

Otherwise the errno is just whatever was set by a previous failing function, which may cause issues in some games.
I used EAGAIN here since it appears to be valid for all three of these functions, but this can be changed if requested.

* Fix missed error returns

* Fix socket methods in file_system

Missed these
2025-07-30 21:54:28 +03:00
Marcin Mikołajczyk
26a92d97fa Sockets are now files (#3319)
* Socket support for read/write/fstat

* Sockets are now files

* Fix ssize_t for windows

* Return posix error codes in net functions
2025-07-29 00:20:10 +03:00
Marcin Mikołajczyk
fb5ac912cd Wiring misc functions (#3293)
* Register some posix functions

* Stub sceKernelIsAddressSanitizerEnabled

* Wire more pthread_attr functions

* Register sys_getsockopt to libScePosix

* Register sys_getpeername to libScePosix

* getpid() returns tid now

* Log sceKernelIsAddressSanitizerEnabled
2025-07-24 00:34:13 +03:00
kalaposfos13
95a386308a Implement sceKernelError (#3282)
* Implement sceKernelError

* Oh come on
2025-07-20 22:52:44 +03:00
Marcin Mikołajczyk
fd03fe2b5a Register posix_rename (#3281) 2025-07-20 22:07:37 +03:00
Stephen Miller
aeab525a7f Fix create flag handling in open (#3255)
If the create flag is specified, but the file already exists, then the file should open successfully, regardless of permissions.
This fixes a crash seen in Phantasy Star Online 2 New Genesis (CUSA29813)
2025-07-16 12:30:20 +03:00
kalaposfos13
499451bb80 Standardize RegisterLib names for HLE libraries (#3234) 2025-07-16 12:23:03 +03:00
kalaposfos13
b68ca43166 Implement sceKernelGetSystemSwVersion (#3243)
* Implement sceKernelGetSystemSwVersion

* Set the reported firmware version to that of the game executable
2025-07-14 23:44:13 +03:00
Marcin Mikołajczyk
5eef2fd28a mmap executable memory (#3201) 2025-07-07 12:26:27 +03:00
Stephen Miller
d1f5a7e8fb libkernel mprotect export (#3199) 2025-07-06 22:03:59 +02:00
Stephen Miller
12198f9255 libkernel: Check returned module in sceKernelGetModuleInfoFromAddr (#3147)
* Error if the module doesn't exist

Fixes another thing kalaposfos found

* Fix error returns

Based on 11.00 libkernel decomp.
2025-06-23 01:32:43 -07:00
Stephen Miller
d9dac05db2 Core: MapMemory fixes (#3142)
* Validate requested dmem range in MapMemory

Handles a rare edge case that only comes up when modding Driveclub

* Specify type

auto has failed us once again.

* Types cleanup

Just some basic tidying up.

* Clang
2025-06-21 19:22:03 -07:00
kalaposfos13
2d335f436c Stub out SetGPO and GetGPI (#3135) 2025-06-21 05:23:14 -07:00
Stephen Miller
213ca72fa1 Filesystem: Fixes for posix_rename and write (#3099)
* Fix rename

We shouldn't be leaving a copy of the original filename laying around.
This fixes one of a few broken savedata checks in DRAGON BALL XENOVERSE (CUSA01341)

* sceKernelWrite hack

Seems like std::fwrite has some weird edge cases we aren't handling properly.
Until we get to the bottom of this issue, here's a hack that bypasses it.
This fixes saves in DRAGON BALL XENOVERSE (CUSA01341)

* hack fix

* Improved "hack"

* Fix rename for Windows users

Turns out, we're using copy instead of rename for a reason, and that same reason came up when adding the remove call.
Also adds a log for the sceKernelWrite issue, since that's definitely a hack that needs to be debugged.

* A real fix for the sceKernelWrite issue

Turns out, some data was just buffered.
Running Flush fixes that problem.

* Move fflush call to WriteRaw

To prevent future cases of this issue.
2025-06-15 22:43:39 +03:00
Fire Cube
de69f2b40b Equeue: HrTimer fixes (#2987)
* initial changes

* tmp

* impl

* support wait for multiple timers

* cleanup
2025-06-15 19:03:57 +03:00
Stephen Miller
3e0ec9ebef Core: Merge Direct Memory Areas (#3084)
* Merge dmem areas

* Fix DirectMemoryArea::CanMergeWith

Don't merge dmem areas if the memory types are different.

* Reduce some warnings to info

Both functions should behave properly now, there's no reason to warn about their use.

* Clang
2025-06-11 17:34:00 +03:00
Stephen Miller
b49340dff8 libSceVideodec2: Update structs to match newer firmwares (#3077)
* Update file_system.cpp

* libSceVideodec2 struct fixes

Our code was based on an old version of the libSceVideodec2 library. Based on what I've decompiled, these structs changed somewhere around firmware 6.50, and newer versions of the library have these flexible checks to accommodate both variants of the structs.

* Static assert for AvcPictureInfo struct

All the other Videodec2 structs have static asserts, might as well use one here too.

* Initialize new values

Set proper values for frameFormat and framePitchInBytes.
`frame->linesize[0]` appears to be in bytes already, I'm not sure if that means framePitch is being set wrong though.
2025-06-10 13:22:50 -07:00
TheTurtle
d7051f15f4 texture_cache: Basic handling of partially resident images (#3066)
* texture_cache: Avoid gpu tracking assert on sparse image

At the moment just take the easy way of creating the entire image normally and uploading unmapped subresources are zero

* tile_manager: Downgrade assert to error

* fix macos
2025-06-09 01:26:10 -07:00
Stephen Miller
5edd9ff54b Improved sceKernelMapNamedFlexibleMemory logging (#3050)
* More descriptive sceKernelMapNamedFlexibleMemory logging

* Misc exports

These functions are used by Overwatch: Origins Edition

* Clang

* Function parameter cleanup

Changes the parameters on our sceKernelMapNamedFlexibleMemory and sceKernelMapFlexibleMemory functions to better align with our current standards.
2025-06-08 00:17:45 +03:00
Marcin Mikołajczyk
5b6fc788b3 Fix passing user data in user-triggered equeue events (#2948) 2025-06-03 11:42:20 -07:00
Stephen Miller
8cdd8dd725 Core: Pthread affinity fixups (#3021)
* posix_pthread_attr_getschedparam

* Fixes for scePthreadGetAffinity and scePthreadSetAffinity

Looking at FreeBSD source, and our other pthread functions, we should be using our FindThread function to get the appropriate thread if thread != g_curthread.
2025-06-03 03:43:56 -07:00
Stephen Miller
bb3f8af81a Core: Protect fixes (#3029)
* Swap do-while to while

If we use a do-while loop, we waste time if `aligned_size = 0`.  This is also still accurate to FreeBSD behavior, where it returns success if `start == end` during mprotect.
This also effectively prevents the memory assert seen in updated versions of RESIDENT EVIL 2 (CUSA09193)

* Move prot validation outside loop

The prot variable shouldn't change during a mprotect call, so we can check the flags before protecting instead.
Also cleans up the code for prot validation.
This should improve performance, and is more accurate to FreeBSD code.

* Add logging for protect calls

This will help in debugging future problems
2025-06-03 09:29:25 +03:00
Stephen Miller
6cdc52cdde Core: More Memory Cleanup & Fixes (#2997)
* Only perform GPU memory mapping when GPU can access it

This better aligns with hardware observations, and should also speed up unmaps and decommits, since they don't need to be compared with the GPU max address anymore.

* Reserve fixes

ReserveVirtualRange seems to follow the 0x200000000 base address like MemoryPoolReserve does.
Both also need checks in their flags Fixed path to ensure we're mapping in-bounds. If we're not in mapping to our address space, we'll end up reserving and returning the wrong address, which could lead to weird memory issues in games.

I'll need to test on real hardware to verify if such changes are appropriate.

* Better sceKernelMmap

Handles errors where we would previously throw exceptions. Also moves the file logic to MapFile, since that way all the possible errors are in one place.
Also fixes some function parameters to align with our current standards.

* Major refactor

MapDirectMemory, MapFlexibleMemory, ReserveVirtualRange, and MemoryPoolReserve all internally use mmap to perform their mappings. Naturally, this means that all functions have similar behaviors, and a lot of duplicate code.
This add necessary conditional behavior to MapMemory so MemoryPoolReserve and ReserveVirtualRange can use it, without disrupting the behavior of MapDirectMemory or MapFlexibleMemory calls.

* Accurate phys_addr for non-direct mappings

* Properly handle GPU access rights

Since my first commit restricts GPU mappings to memory areas with GPU access permissions, we also need to be updating the GPU mappings appropriately during Protect calls too.

* Update memory.cpp

* Update memory.h

* Update memory.cpp

* Update memory.cpp

* Update memory.cpp

* Revert "Update memory.cpp"

This reverts commit 2c55d014c0.

* Coalesce dmem map

Aligns with hardware observations, hopefully shouldn't break anything since nothing should change hardware-wise when release dmem calls and unmap calls are performed?
Either that or Windows breaks because Windows, will need to test.

* Implement posix_mprotect

Unity calls this
Also fixes the names of sceKernelMprotect and sceKernelMtypeprotect, though that's more of a style change and can be reverted if requested.

* Fix sceKernelSetVirtualRangeName

Partially addresses a "regression" introduced when I fixed up some asserts.
As noted in the code, this implementation is still slightly inaccurate, as handling this properly could cause regressions on Windows.

* Unconditional assert in MapFile

* Remove protect warning

This is expected behavior, shouldn't need any logging.

* Respect alignment

Forgot to properly do this when updating ReserveVirtualRange and MemoryPoolReserve

* Fix Mprotect on free memory

On real hardware, this just does nothing. If something did get protected, there's no way to query that information.
Therefore, it seems pretty safe to just behave like munmap and return size here.

* Minor tidy-up

No functional difference, but looks better.
2025-05-29 18:56:03 +03:00
squidbus
95f04b746d equeue: Move small timer check to WaitForEvents. (#3000) 2025-05-28 19:54:47 +03:00
Fire Cube
e0309a4b01 Equeue: fix WaitEqueue assert on nullptr (#2994)
* fix

* fix infinite call of waitforsmalltimer

* fix wrong nullptr check

* add comment back

* fix discrepancy

* remove assert

---------

Co-authored-by: georgemoralis <giorgosmrls@gmail.com>
2025-05-27 14:47:54 -07:00
Stephen Miller
18ff65efc8 Implement sceKernelMapDirectMemory2 (#2940)
* Implement sceKernelMapDirectMemory2

Behaves similarly to sceKernelMapDirectMemory, but has a type parameter.

* Simplify

No need to copy all the MapDirectMemory code over, can just call the function, then do the SetDirectMemoryType call

* Clang
2025-05-16 05:38:40 -07:00
squidbus
aeb4536988 externals: Remove winpthreads. (#2932) 2025-05-15 13:59:34 -07:00
Stephen Miller
6d38100a41 Avoid logging nulls in sceKernelIsStack (#2933)
Games would crash if providing nullptr to start or end.
Also don't need the :# part when logging pointers, as they're automatically formatted.
2025-05-14 17:09:23 +03:00
squidbus
26c965cf4a equeue: Fix timer cancel error code check. 2025-05-13 17:31:23 -07:00
squidbus
1639640902 event_flag: Lower error logs to debug. 2025-05-13 14:46:59 -07:00
squidbus
3ab69e24db fix: Compiling with newer Boost 2025-05-13 14:22:44 -07:00
squidbus
0d127a82dd equeue: Clean up timers implementation. (#2925) 2025-05-13 14:05:29 -07:00
Marcin Mikołajczyk
1832ec2ac2 Implement sceKernelIsStack (#2917) 2025-05-13 13:54:22 -07:00
Fire Cube
7334fb620b sceKernelAddTimerEvent implementation (#2906)
* implementation

* add backend (WIP)

* now should be good
- fix implementation based on homebrew tests
- demote log to debug
- make squidbus happy (hopefully)

* fix moved m_name

* fix clang

* replace existing event when its same id and filter

* run timercallback after addEvent and remove useless code

* move KernelSignalRequest to the end

* clang (i hate you)
2025-05-13 12:16:53 -07:00
georgemoralis
e6a144ddb0 [Libs] sceNet IV (#2867)
* dummy returns in p2p sockets

* added logging for sceNetSetsockopt

* possible fix for ORBIS_NET_SO_LINGER set

* logging for getsockoption as well

* disable kernel getsockname (seems to create issues with cyberpunk)

* some fixes with SetSocketOptions

* arggg

* posix_getsockname try

* mutex protection

* removed duplicated include (diegolix29)

* posix_getsockname appears to have issues in cyberpunk , comment it for now
2025-05-13 08:54:38 +03:00
squidbus
3a10fda008 kernel: Simplify sceKernelUuidCreate 2025-05-12 22:15:04 -07:00
Roman
2a3a701115 kernel: macos/linux Implement sceKernelUuidCreate (#2923)
* kernel: macos/linux Implement sceKernelUuidCreate

* Fix clang-format

* Fix Linux build

* Fix Linux build (2)

---------

Co-authored-by: squidbus <175574877+squidbus@users.noreply.github.com>
2025-05-12 22:10:33 -07:00
squidbus
f94c7e52b7 kernel: Implement scePthreadGetaffinity (#2916) 2025-05-12 10:46:53 -07:00