Commit Graph

438 Commits

Author SHA1 Message Date
kalaposfos13
0579569f13
Improve signal emulation (#4108)
* improve signal emulation

* make the sce function use the new posix ones

* ifdefing away the issues

* fix me being very tired yesterday night

* let macOS handle SIGRT signals with the native sigaction call instead of an early error return

* windows still has no clue what the fuck is going on

* the loathsome clang-formatter

* fix oact

* return the guest handler, not the host one

* Clear any existing signal mask for game threads.

* don't rely on implementation specific things

* Fix Windows support and sceKernelRaiseException bug

* Review suggestions

@kalaposfos13 suggested I push these.

---------

Co-authored-by: Stephen Miller <56742918+StevenMiller123@users.noreply.github.com>
2026-03-09 14:20:14 +02:00
Stephen Miller
cc6af03adf
Lib.SysModule: Proper HLE implementation (#4102)
* Replace sysmodule enums with table

Dumped this from the 12.52 module, using a script I created.

* Better documentation

* Separate from system libraries

That system folder is going to be getting quite large if I left all the sysmodule stuff in there.

* More arrays from library

* Found another preload list

Ghidra really hates decompiling libSceSysmodule, so I didn't notice this one at first.
Also documented specific versions tied to each preload list.

* Start work on implementation

* Some basic implementations

* Initial stub for module loading

Just enough to see if the logic functions appropriately.

* Clang

* sceSysmoduleLoadModule

Now I need to get sceSysmodulePreloadModuleForLibkernel done so that we don't have bugs from not loading internal LLEs.

* sceSysmoduleLoadModuleInternal

* sceSysmodulePreloadModuleForLibkernel

I have successfully broken quite literally everything.
I shall debug this tomorrow.

* Slight fix

* Maybe fix?

* Change log

Enjoy the log spam 😄

* Increased defined stub count

Now that libc and libSceLibcInternal loads later, all the auto stubs are getting consumed by it.

* sceSysmoduleUnloadModule stub

Also a couple fixes. Sysmodule does pass argc and argv to game modules, but only after loading them once to check  binaries.
Shouldn't matter for the most part.

* Clang

* Less stubs

2 thousand is seemingly enough.

* sceSysmoduleLoadModuleInternalWithArg

Doesn't hurt to have, since Apex Legends calls it.

* Oops

* Oops 2

* Rename isModuleLoaded to getModuleHandle

Review comment

* Remove debug game module loads

These cases only trigger when specific sceRegMgr key values are set, and for our purposes, we can treat that case as false.

* Allow preloading to fail

For kalaposfos

* Clang
2026-03-08 21:48:03 +02:00
georgemoralis
50d5ce9a83
fixed an issue in sceKernelRemoveExceptionHandler (#4086)
* fixed an issue in sceKernelRemoveExceptionHandler

* fixed comment
2026-03-07 08:55:17 +02:00
Vladislav Mikhalin
e1ecd8e98c
threads: initialize tls on thread creation (take 2) (#4070) 2026-02-24 20:35:05 +03:00
kalaposfos13
c265a39227 fix typo 2026-02-23 09:08:39 +01:00
Stephen Miller
6cbab87745
Kernel.Event: Implement kqueue and kevent (#4065)
* Remove dead code from EqueueInternal::WaitForEvents

No longer necessary now that we avoid using small timers when falling back on equeue logic.

* Refactor type names

Might as well

* Properly define OrbisKernelEqueue as a handle

Most of the functions using an "OrbisKernelEqueue" call directly into kevent. Therefore, OrbisKernelEqueue should be a equeue handle.

* Clang

* Widen OrbisKernelEqueue type

On real hardware, it's some value that contains the handle, as opposed to just the handle itself.

* kqueue implementation

The easy part

* Hardware-accurate timer data

Needed to make kevent simpler for these uses.

* Move callback scheduling to EqueueInternal::AddEvent

kevent would become excessively bloated if I needed to deal with that in there.

* posix_kevent

kevent is a bit of a pain, for now I've implemented as much as libkernel actually uses for it's wrappers, and left error logs to skip behavior when necessary.

* Log calls

* Apple, why are you calling fstat on an equeue?
2026-02-22 23:41:05 +02:00
kalaposfos13
fb5c36fa11
Implement guest signal handlers (#4064)
* Change thread pausing to use SIGTRMIN on UNIX

* Allow handling of the rest of the signals

* Add orbis-native signal number conversion and fix a few bugs

* ifdefing away the issues

* add check for mac for the signal that's used for thread pausing there

* Add a few more registers

* Don't break HLE memory tracking
Now, if a guest app installs a handler for SIGSEGV/SIGBUS/SIGILL, that'll be handled by keeping the original signal handler, and if we can't handle the signal ourselves (as in it didn't come from HLE memory tracking), we pass it on to the guest

* copyright 2026

* +
2026-02-22 18:56:54 +02:00
kalaposfos13
b37e0a6ea6
Miscallenous function exports and implementations (#4068)
* _nanosleep

* pthread_mutexattr_setpshared

* pthread_attr_setschedpolicy

* getuid

* copyright 2026
2026-02-22 17:08:06 +02:00
Vladislav Mikhalin
9241ebd4dd
Revert "threads: initialize TLS on thread creation (#4048)" (#4062)
This reverts commit f6d71646c0.
2026-02-21 14:10:25 +03:00
Stephen Miller
1eb09ab440
Kernel.Equeue: Various event fixes (#4059)
* Fix vblank event data

* Various logical fixes for timer events

Store timer timeouts in nanoseconds now, properly handle event "replacement", avoid employing small timers when adding events to equeues, fix stored timer data

* Clang
2026-02-21 09:37:59 +02:00
Vladislav Mikhalin
f6d71646c0
threads: initialize TLS on thread creation (#4048)
* initialize TLS on thread creation
* initialize tls in dimensions toypad writer thread
* clear most of the stack on thread init with some black magic
2026-02-19 21:26:33 +03:00
Stephen Miller
98af227a8d
More hotfixes (#4036)
User events don't increment fflags, so don't increment it.
Also added a bugfix for dce events, as static_cast wasn't properly unpacking event.data.
2026-02-14 22:47:28 +02:00
Stephen Miller
e0850d5cfc
Kernel.Equeue: Only reset trigger state on events that clear. (#4032)
Some checks failed
Build and Release / reuse (push) Has been cancelled
Build and Release / clang-format (push) Has been cancelled
Build and Release / get-info (push) Has been cancelled
Build and Release / windows-sdl (push) Has been cancelled
Build and Release / macos-sdl (push) Has been cancelled
Build and Release / linux-sdl (push) Has been cancelled
Build and Release / linux-sdl-gcc (push) Has been cancelled
Build and Release / pre-release (push) Has been cancelled
* Don't clear events that don't need clearing

Unless the event has the clear flag, it will be returned multiple times after triggering.

* Fix event flags

As older code suggests, the PS4 kernel does append the clear flag to various event types internally. This is visible when observing the returned event data from sceKernelWaitEqueue (or kevent, if you're feeling ambitious)

Add flag is also removed from events internally.
2026-02-14 01:59:23 +02:00
Stephen Miller
1a06020f70
Core: Mount system fonts (#4028)
* Mount system fonts

Now that font LLE works, these are necessary for some titles.

* Make fonts path configurable

By user request.
2026-02-13 09:02:30 +02:00
Stephen Miller
6a7f8a9e5a
Libs.LibcInternal: _Fofind, _Lockfilelock, _Unlockfilelock, _Foprep, _Fopen, fopen, fflush, _Nnl, _Fspos, fseek, _Frprep, fread, _Fofree, fclose, _Mtxinit, _Mtxlock, _Mtxunlock, _Mtxdst implementations (#4019)
* Initial definitions

* internal__Fofind

* Libcinternal threads

fopen stores a valid pthread mutex in the FILE struct. Since this is exposed to the game/app, we need to handle this accurately.

* internal__Foprep (and various other functions called in it)

* Actual fopen implementation

At long last, an actual function I'm supposed to implement.

* fflush + compile fixes

* fseek implementation

Comes with functions fseek calls, aside from fflush which I pushed earlier.

* fread, _Frprep

Also changed some parameter names a tad to match how I named things in my decomp.
And fixed some bugs with how I was handling the weird offseted mode thing

* fclose, _Fofree

Not confident on this one, but we'll see I guess.

* Bug fixing

No more crashes at least, fread seems to be broken though.

* fopen bugfixes

Behavior now matches LLE, at least in how LLE font seems to use it.

* Fix _Frprep

Seems like everything works now?

* Logging

Probably going to need to swap lseek and read logs to debug/trace later but this is for debugging.

* Remove alignment check

Seems I must've misinterpreted some of what Ghidra spat out, since libSceNgs2 is calling with size 1, nmemb 4.

* Reduce fseek, fread logs to trace

* Clang
2026-02-11 08:13:28 +02:00
Stephen Miller
2a61851a88
Kernel.Process: Implement sceKernelGetModuleInfo2, sceKernelGetModuleList2 (#4001)
* twos

* Fixes

Still can't test properly, but this seems to hide system libs, which I'm pretty sure is the necessary difference here.

* Clang

* Extra export for sceKernelGetModuleInfo2
2026-02-08 20:07:17 +02:00
Stephen Miller
be86b5fe32
Kernel.Pthreads: Remove unreachable in posix_pthread_mutex_timedlock (#4005)
* Fix assert

Just a typical day of me pushing something a month ago, nobody testing/reviewing it, then finding out it's broken when that code inevitably makes it into production.

* Remove unreachable in posix_pthread_mutex_timedlock

It's apparently something that was added during pthreads rewrite, but the actual code for this function seems to be fully implemented?
2026-02-08 09:33:20 +02:00
kalaposfos13
1fe72cfe1f
filesystem: fill in timespec values for fstat (#3994)
* filesystem: fill in timespec values for fstat

* w*ndows

* fix windows and mac
2026-02-05 22:39:37 +02:00
Spirtix
b43573112c
Prevent posix_pthread_mutexattr_settype from setting invalid mutex types (#3991)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
2026-02-03 17:05:45 +02:00
Stephen Miller
d3c6abac4e
Fix default pthread attributes (#3987)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
This matches libkernel decomp + old fpPS4 code.
Fixes Attack On Titan 2 audio
2026-02-02 08:16:59 +02:00
Stephen Miller
3bd6a0f9f8
Skip stdin fd for want_read in select (#3983)
Some checks failed
Build and Release / reuse (push) Has been cancelled
Build and Release / clang-format (push) Has been cancelled
Build and Release / get-info (push) Has been cancelled
Build and Release / windows-sdl (push) Has been cancelled
Build and Release / macos-sdl (push) Has been cancelled
Build and Release / linux-sdl (push) Has been cancelled
Build and Release / linux-sdl-gcc (push) Has been cancelled
Build and Release / pre-release (push) Has been cancelled
UE uses this to signal if it should read from stdin, real hardware never marks it as as readable
2026-01-31 23:31:58 +02:00
kalaposfos13
cc70fa8bb5
Fix thread names not updating correctly (#3968)
Also, apply consistency to thread names
Also also, copyright 2026
2026-01-28 13:25:48 +02:00
Stephen Miller
46a7c4e1f5
Core: Miscellaneous memory fixes and slight optimizations (#3946)
* Optimizations

Microsoft allows you to coalesce multiple free placeholders in one VirtualFreeEx call, so we can perform the VirtualFreeEx after coalescing with neighboring regions to eliminate a VirtualFreeEx call in some situations.

* Remove unnecessary VirtualProtect call

As far as I can tell, this call wastes a bunch of time, and is completely unnecessary.
With our current codebase, simply supplying prot to MapViewOfFile3 works properly.

* Properly handle file mmaps with offsets

Pretty easy fix to perform while I'm here, so I might as well include it.

* Oops

Leftover stuff from local things + clang

* Disable tracy memory tracking

Tracy's memory tracking is built around a typical malloc/free API, so each individual alloc must correspond to a free.
Moving these to address space would fix issues on Windows, but Linux/Mac would have the same issues with our current code.
Disabling VMA merging is technically a fix, but since that's hardware-accurate behavior, I'd rather not disable it.

I'm sure there's a simple solution I'm missing, but unless other devs have a better idea of how this should be handled, the best I can do is disable it so we can keep using Tracy to trace performance.

* Update address_space.cpp

* Debug logging

Should give a decent idea of how nasty these AddressSpace calls are in games that lost perf.

* test removing thread safety

Just for testing, will revert afterwards.

* Check name before merging

Fixes a regression in Apex Legends

* Revert "test removing thread safety"

This reverts commit ab897f4b1c.

* Move mutex locks before IsValidMapping calls

These aren't thread safe, this fixes a rare race condition that I ran into with Apex Legends.

* Revert "Debug logging"

This reverts commit eb2b12a46c.

* Proper VMA splitting in ProtectBytes, SetDirectMemoryType, and NameVirtualRange

Also slight optimization by eliminating AddressSpace protect calls when requested prot matches the previous prot.
Fixes a regression in God of War: Ragnarok

* Clang

* Fixes to SetDirectMemoryType logic

Fixes some regressions in Marvel's Spider-Man that occurred with my previous commits to this PR.

* Fix Genshin Impact again

* Assert on out-of-bounds protect calls

Our page tracking code is prone to causing this.

* test mutex again

This time, remember all mutex stuff

* Revert hack

I'll work on a better way to deal with mutexes in a bit, first I'm pushing up some extra fixes

* Proper logic for checked ReleaseDirectMemory, added bounds checks

Should help some games.

* Better logging for ReleaseDirectMemory errors.

* Only perform region coalescing after all unmap operations.

A small optimization for unmapping multiple regions. Since Microsoft lets you coalesce multiple placeholders at once, we can save doing any VirtualFreeEx calls for coalescing until after we unmap everything in the requested range.

* Separate VMA creation logic into a separate method, update MapFile to use it

MapFile is technically another "emulation" of MapMemory, both should follow similar logic.
To avoid duplicating code, move shared logic to a different function that both MapMemory and MapFile can call.

This fixes memory asserts in a couple of online-only apps I have.

* Clang

* Fix TryWriteBacking

This fixes a lot of regressions that got misattributed

Co-Authored-By: TheTurtle <47210458+raphaelthegreat@users.noreply.github.com>

* Fix again

Fixes device lost crashes with some games after my last commit.

* Oops

* Mutex cleanup

Avoided changing anything in MapMemory, UnmapMemory, PoolCommit, or PoolDecommit since those all need a little extra granularity to prevent GPU deadlocking.

Everything else now uses standard library locks to make things a little simpler.

* Swap MapMemory and PoolCommit to use scoped lock

GPU maps are safe, so this is fine. Unmaps are the primary issue.

---------

Co-authored-by: TheTurtle <47210458+raphaelthegreat@users.noreply.github.com>
2026-01-24 00:17:57 +02:00
Stephen Miller
3e1f5a0bfb
Kernel.Vmm: Handle sparse physical memory usage + other fixes (#3932)
* Initial work

* Bug fixing

deadlocks and broken unmaps

* Fix more bugs

broken memory pools

* More bug fixing

Still plenty more to fix though

* Even more bug fixing

Finally got Final Fantasy XV back to running, haven't found anymore bugs yet.

* More bugfixing

* Update memory.cpp

* Rewrite start

* Fix for oversized unmaps

* Oops

* Update address_space.cpp

* Clang

* Mac fix?

* Track VMA physical areas based on start in VMA

Allows me to simplify some logic, and should (finally) allow merging VMAs in memory code.

* Merge VMAs, fix some bugs

Finally possible thanks to address space + phys tracking changes

* Clang

* Oops

* Oops2

* Oops3

* Bugfixing

* SDK check for coalescing

Just to rule out any issues from games that wouldn't see coalescing in the first place.

* More ReleaseDirectMemory fixes

I really suck at logic some days

* Merge physical areas within VMAs

In games that perform a lot of similar mappings, you can wind up with 1000+ phys areas in one vma.
This should reduce some of the overhead that might cause.

* Hopefully fix Mac compile

Why must their uint64_t be different?

* Mac pt.2

Oops
2026-01-21 17:36:09 +02:00
Vladislav Mikhalin
f7a473b391
read compiled SDK version from eboot (#3905) 2026-01-09 18:29:09 +03:00
marecl
2bbb04ff55
ENAMETOOLONG, posix_rename fix (#3869)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
* ENAMETOOLONG on paths > 255 characters
Corrected posix_rename behaviour:
* handles errors only if dst exists (shouldn't error out if doesn't)
* recursively removes "old name" (would otherwise fail if it's a not-empty dir)
* actually creates target directory

* Updated detection

* destubbed unlink()

* proper error returned
2025-12-19 14:58:07 +02:00
Stephen Miller
65f0b07c34
libkernel: Implement sceKernelEnableDmemAliasing, proper mapping type checks in posix_mmap (#3859)
* Basic handling for MAP_VOID, MAP_STACK, and MAP_ANON in mmap.

* Update memory.cpp

* Update memory.cpp

* Dmem aliasing check

* Oops
2025-12-08 13:46:32 +02:00
kalaposfos13
2a5910ed51
New translations en_us.ts (OpenOrbis) (#3858) 2025-12-07 23:56:51 +02:00
kalaposfos13
98fd0689ac
Revert non-Linux parts of #3819 (#3852)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
* Revert non-Linux parts of #3819

* More OpenOrbis stuff that I couldn't be bothered to put in a new PR
2025-12-03 15:05:19 +02:00
Stephen Miller
a5f9280841
Return CPU mode based on param.sfo attributes (#3846)
Some checks failed
Build and Release / reuse (push) Has been cancelled
Build and Release / clang-format (push) Has been cancelled
Build and Release / get-info (push) Has been cancelled
Build and Release / windows-sdl (push) Has been cancelled
Build and Release / macos-sdl (push) Has been cancelled
Build and Release / linux-sdl (push) Has been cancelled
Build and Release / linux-sdl-gcc (push) Has been cancelled
Build and Release / pre-release (push) Has been cancelled
Values are based on hardware observations.
2025-12-01 10:21:19 +02:00
oltolm
4922d526fe
msys2: fix build (#3818)
* cmake: fix mingw-w64 build

* time.cpp: fix build with Clang on Windows

* tls.h: include malloc.h for alloca
2025-11-22 10:32:29 +02:00
marecl
6612a32523
Prevent writing to directories (#3820)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
* Prevent writing to directories

* Prevent writing to directories
2025-11-20 19:41:01 +02:00
Stephen Miller
ee2bc97248
Windows: Limit address space maximum when higher addresses are not needed (#3775)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
* Earlier initialization of elf info.

Everything used for elf info initialization comes from the param.sfo, so we can initialize this earlier to have this information accessible during memory init.

* Extract compiled SDK version from pubtoolinfo string

Up until now, we've been using the game's reported "firmware version" as our compiled SDK version. This behavior is inaccurate, and is something that has come up in my hardware tests before.

For the actual compiled SDK version, we should use the SDK version in the PUBTOOLINFO string of the param.sfo, only falling back on the firmware version when that the sdk_ver component isn't present.

* Store compiled SDK version in ElfInfo

* Limit address space for compiled SDK version at or above FW 3

Sony placed a hard cap at 0xfc00000000, with a slight extension for stack mappings. For now, though stack mappings aren't implemented, there's no harm in keeping a slightly extended address space (since this cap is lower than our old user max).

Limiting the max through address space is necessary for Windows due to performance issues, in the future I plan to properly implement checks in memory manager code to properly handle this behavior for all platforms.

* Use compiled SDK version for sceKernelGetCompiledSdkVersion

I think this is pretty self explanatory.

* Log SDK version

Since this value is what most internal firmware version checks are against, logging the value will help with debugging.

* Update address_space.cpp

* Update emulator.cpp

* Backwards compatible logging

Because that's apparently an issue now
2025-11-10 17:07:17 +02:00
Emma
2f022a462d
filesystem: return st_mtim in posix_stat (fixes RB4 / CUSA02901 DLC crash) (#3781)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
* filesystem: return st_mtim in posix_stat

* filesystem: stat - remove reliance on clock_cast

* filesystem: stat - remove reliance on to_time_t
2025-11-06 22:47:40 +02:00
kalaposfos13
604f8d9398
More OpenOrbis stuff (#3776)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
2025-11-06 13:22:26 +02:00
Stephen Miller
19e974bf21
Libkernel: Implement/stub some functions (#3774)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
* Define latest released firmware version, use that for sceKernelGetSystemSwVersion

I feel this is less hacky and error-prone than just returning the game firmware.

* sceKernelGetAllowedSdkVersionOnSystem

* sceKernelHasNeoMode

* sceKernelGetAppInfo stub

* sceKernelGetCurrentCpu

* fixups

* sceKernelGetMainSocId

Used by libSceAvPlayer to determine if console is a pro or not.

* Update process.cpp

* Set has_param_sfo to true

* Clang
2025-11-05 16:58:15 -08:00
Stephen Miller
683e5f3b04
Core: Simulate write-only file access with read-write access (#3360)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
* Swap write access mode for read write

Opening with access mode w will erase the opened file. We do not want this.

* Create mode

Opening with write access was previously the only way to create a file through open, so add a separate FileAccessMode that uses the write access mode to create files.

* Update file_system.cpp

Remove a hack added to posix_rename to bypass the file clearing behaviors of FileAccessMode::Write

* Check access mode in read functions

Write-only files cause the EBADF return on the various read functions. Now that we're opening files differently, properly handling this is necessary.

* Separate appends into proper modes

Fixes a potential regression from one of my prior PRs, and ensures the Write | Append flag combo also behaves properly in read-related functions.

* Move IsWriteOnly check after device/socket reads

file->f is only valid for files, so checking this before checking for sockets/devices will cause access violations.

* Fix issues

Now that Write is identical to ReadWrite, internal uses of Write need to be swapped to my new Create mode

* Fix remaining uses of FileAccessMode write to create files

Missed these before.

* Fix rebase

* Add stubbed get_authinfo (#3722)

* mostly stubbed get_authinfo

* Return value observed on console if get_authinfo was called for the current thread, esrch otherwise

---------

Co-authored-by: kalaposfos13 <153381648+kalaposfos13@users.noreply.github.com>
Co-authored-by: georgemoralis <giorgosmrls@gmail.com>
2025-11-04 10:57:26 +02:00
Pavel
6c7c5eb59c
get_authinfo (#3760) 2025-10-31 15:56:11 +02:00
oltolm
493cda07c0
fix divide by zero (#3759) 2025-10-31 15:36:27 +02:00
Stephen Miller
6b6294a750
Filesystem: Always log mode as an octal (#3729)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / windows-qt (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / macos-qt (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-qt (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / linux-qt-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
Mode is an octal, print it as one.
2025-10-11 20:22:35 -07:00
kalaposfos13
6805baffb2
posix_pthread_mutex_destroy (#3720) 2025-10-08 01:44:17 -07:00
Stephen Miller
794d593a02
Fix alignment for mmap (#3716) 2025-10-07 16:22:21 -07:00
kalaposfos13
4fa435490c
Add missing function exports for OpenOrbis (#3708)
* Add missing function exports for OpenOrbis

* copyright 2025

* review comment
2025-10-05 19:31:05 +03:00
Stephen Miller
e7194af881
Core: Increase address space limits and rework Windows address space initialization. (#3697)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / windows-qt (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / macos-qt (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-qt (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / linux-qt-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
* SearchFree adjustments

* Robust address validation

I've adjusted IsValidAddress to take in a size, and check whether the whole range is contained in vma map.
If no size is provided, the function reverts to the old form of address validation instead.

* Map around gaps

As is, this should work mostly.
Only remaining issue is adding logic to pass the "mapped regions" to the guest vma map (and make such logic cross-platform).

* Initialize vma_map using gaps

This should allow memory code to catch any issues from address space gaps, and prevent non-fixed mappings from jumping to a location that isn't actually available.

* Clang

* Fix compile

* Clang

* Fix compile again

* Set system_managed_base and system_managed_size based on

Many places in our code use system_managed_base as the minimum mappable address, ensure this fact remains the same  on Windows to prevent potential bugs.

* Reduce address validation in SearchFree

Allows SearchFree to function when a certain Windows GPU driver goes and reserves the whole system managed area.

Since SearchFree is only called on flexible addresses, allowing this particular case, where addresses are in bounds, but there's not enough space to map, should be safe enough.

* Bump address space size further

To handle Madden NFL 16 (and any games like it)

* More thorough logging of available memory regions

Should help with spotting weirdness.

* Formatting fixes

* Clang

* Slight reduction of user space

Still large enough to handle EA's shenanigans, but small enough that Linux doesn't break.

* Assert on VirtualQuery failure

* Extra debugging information

* Further reduce user space

This will unfix most of EA's titles, but UFC will still work.
Older windows versions support the high addresses, but trying to actually use them causes significant performance issues.

* Extra debugging info

Just in case other testers still run into issues.

* Remove debug logging

* Revert user space increases

Technically this constant is still higher than before, but weird side effects of our old logic resulted in a max address somewhere around this in main.

* address_space: Support expanded virtual memory space on macOS.

Co-Authored-By: squidbus <175574877+squidbus@users.noreply.github.com>

* Move address space constants to address_space.cpp

This ensures that all code must use the calculated address space memory values instead of the constants, since the calculated values can differ based on the platform.

This does require slight modification to thread state and gnmdriver code, since both were already using these constants directly.

* Workaround Windows 10 limitations

If a Windows 10 device is detected, use a lower value for USER_MAX to prevent system-wide hangs in VirtualAlloc2 calls.

* Fix compile for Windows-Qt

* Move tessellation_factors_ring_addr initialization to sceGnmGetTheTessellationFactorRingBufferBaseAddress

* Set image base address on Windows

This seems to work fine on Windows 11, needs testing from Windows 10 due to the previously discussed bugs.

* Set Linux executable base to 0x700000000000

This allows Linux to map the full user space without any workarounds.

Co-Authored-By: Marcin Mikołajczyk <2052578+mikusp@users.noreply.github.com>

* Basic formatting changes

* Reduce USER_MAX on Linux

Seems like finding a reliable way to move shadPS4's position in memory is difficult, for now limit the user size so we aren't trying to overwrite ourselves.

* Move memory and address_space variables.

---------

Co-authored-by: squidbus <175574877+squidbus@users.noreply.github.com>
Co-authored-by: Marcin Mikołajczyk <2052578+mikusp@users.noreply.github.com>
2025-10-04 14:52:50 -07:00
kalaposfos13
5fedf9daea
add missing pthread_condvar_signal export (#3701)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / windows-qt (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / macos-qt (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-qt (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / linux-qt-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
2025-10-04 12:09:03 +03:00
Stephen Miller
6fb64a8054
Kernel.Fs: Device file cleanup and /dev/rng implementation (#3682)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / windows-qt (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / macos-qt (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-qt (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / linux-qt-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
* Add RNG device

* rng device implementation

Tailored around libSceSsl2's usage, and based on fpPS4's implementation.

* Device file function types and log fixups

* Updated creates

Updates device file create functions to be the same as the directory file create functions.

* Fix compile

* Includes cleanup

Generally preferred to have full paths. Also removed some unused imports too.

* Fix buffer size

* Bring back cstdlib imports

Needed for Mac OS.
2025-10-01 12:20:58 -07:00
Stephen Miller
6c5a84dc99
Core: Handle various edge cases related to executable permissions. (#3660)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / windows-qt (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / macos-qt (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-qt (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / linux-qt-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
* Fix flag handling on Windows

Fixes a weird homebrew kalaposfos made

* Fix backing protects

Windows requires that protections on areas committed through MapViewOfFile functions are less than the original mapping.
The best way to make sure everything works is to VirtualProtect the code area with the requested protection instead of applying prot directly.

* Fix error code for sceKernelMapDirectMemory2

Real hardware returns EINVAL instead of EACCES here

* Fix prot setting in ProtectBytes

* Handle some extra protection-related edge cases.

Real hardware treats read and write as separate perms, but appends read if you call with write-only (this is visible in VirtualQuery calls)

Additionally, execute permissions are ignored when protecting dmem mappings.

* Properly handle exec permission behavior for memory pools

Calling sceKernelMemoryPoolCommit with executable permissions returns EINVAL, mprotect on pooled mappings ignores the exec protection.

* Clang

* Allow execution protection for direct memory

Further hardware tests show that the dmem area is actually executable, this permission is just hidden from the end user.

* Clang

* More descriptive assert message

* Align address and size in mmap

Like most POSIX functions, mmap aligns address down to the nearest page boundary, and aligns address up to the nearest page boundary.
Since mmap is the only memory mapping function that doesn't error early on misaligned length or size, handle the alignment in the libkernel code.

* Clang

* Fix valid flags

After changing the value, games that specify just CpuWrite would hit the error return.

* Fix prot conversion functions

The True(bool) function returns true whenever value is greater than 0. While this rarely manifested before because of our wrongly defined CpuReadWrite prot, it's now causing trouble with the corrected values.
Technically this could've also caused trouble with games mapping GpuRead permissions, but that seems to be a rare enough use case that I guess it never happened?

I've also added a warning for the case where `write & !read`, since we don't properly handle write-only permissions, and I'm not entirely sure what it would take to deal with that.

* Fix some lingering dmem issues

ReleaseDirectMemory was always unmapping with the size parameter, which could cause it to unmap too much. Since multiple mappings can reference the same dmem area, I've calculated how much of each VMA we're supposed to unmap.
Additionally, I've adjusted the logic for carving out the free dmem area to properly work if ReleaseDirectMemory is called over multiple dmem areas.

Finally, I've patched a bug with my code in UnmapMemory.
2025-09-28 11:36:12 +03:00
Stephen Miller
528a060709
Core: Memory code cleanup and further direct memory fixes (#3655)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / windows-qt (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / macos-qt (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-qt (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / linux-qt-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
* Remove mapped dmem type

Since physical addresses can be mapped multiple times, tracking mapped pages is not necessary.
This also allows me to significantly simplify the MapMemory physical address validation logic.

* Proper implementation for sceKernelMtypeprotect

I've rewritten SetDirectMemoryType to use virtual addresses instead of physical addresses, allowing it to be used in sceKernelMtypeprotect.

To accommodate this change, I've also moved address and size alignment out of MemoryManager::Protect

* Apply memory type in sceKernelMemoryPoolCommit

* Organization

Some potentially important missing mutexes, removed some unnecessary mutexes, moved some mutexes after early error returns, and updated copyright dates

* Iterator logic cleanup

Missing end check in ClampRangeSize, and adjusted VirtualQuery and DirectMemoryQuery.

* Clang

* Adjustments

* Properly account for behavior differences in MapDirectMemory2

Undid the changes to direct memory areas, added more robust logic for changing dma types, and fixed DirectMemoryQuery to return hardware-accurate direct memory information in cases where dmas split here, but not on real hardware.

I've also changed MapMemory's is_exec flag to a validate_dmem flag, used to handle alternate behavior in MapDirectMemory2. is_exec is now determined by the use of MemoryProt::CpuExec instead.

* Clang

* Add execute permissions to physical backing

Needed for executable mappings to work properly on Windows, fixes regression in RE2 with prior commit.

* Minor variable cleanup

* Update memory.h

* Prohibit direct memory mappings with exec protections

Did a quick hardware test to confirm, only seems to be prohibited for dmem mappings though.

* Update memory.cpp
2025-09-26 02:28:32 -07:00
Stephen Miller
eeee6ad0ee
Memory: Implement sceKernelMemoryPoolGetBlockStats (#3646)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / windows-qt (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / macos-qt (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-qt (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / linux-qt-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
* Implement sceKernelMemoryPoolGetBlockStats

Not entirely sure on the logic behind the cached blocks work, but flushed blocks seems to just be based on committed direct memory.

* Fix comment
2025-09-24 02:40:26 -07:00
Stephen Miller
419ea140ab
Core: physical backing for flexible and pooled memory allocations (#3639)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / windows-qt (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / macos-qt (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-qt (push) Blocked by required conditions
Build and Release / linux-sdl-gcc (push) Blocked by required conditions
Build and Release / linux-qt-gcc (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions
* Fix isDevKit

Previously, isDevKit could increase the physical memory used above the length we reserve in the backing file.

* Physical backing for flexible allocations

I took the simple approach here, creating a separate map for flexible allocations and pretty much just copying over the logic used in the direct memory map.

* Various fixups

* Fix mistake #1

* Assert + clang

* Fix 2

* Clang

* Fix CanMergeWith

Validate physical base for flexible mappings

* Clang

* Physical backing for pooled memory

* Allow VMA splitting in NameVirtualRange

This should be safe, since with the changes in this PR, the only issues that come from discrepancies between address space and vma_map are issues related to vmas being larger than address space mappings. NameVirtualRange will only ever shrink VMAs by naming part of one.

* Fix

* Fix NameVirtualRange

* Revert NameVirtualRange changes

Seems like it doesn't play nice for Windows

* Clean up isDevKit logic

We already log both isNeo and isDevKit in Emulator::Run, so the additional logging in MemoryManager::SetupMemoryRegions isn't really necessary.

I've also added a separate constant for non-pro devkit memory, as suggested.

Finally I've changed a couple constants to use the ORBIS prefix we generally follow here, instead of the SCE prefix.

* Erase flexible memory contents from physical memory on unmap

Flexible memory should not be preserved on unmap, so erase flexible contents from the physical backing when unmapping.

* Expand flexible memory map

Some games will end up fragmenting the physical backing space used for flexible memory. To reduce the frequency of this happening under normal circumstances, allocate the entirety of the remaining physical backing to the flexible memory map.

This is effectively a workaround to the problem, but at the moment I think this should suffice.

* Clang
2025-09-23 17:24:37 +03:00