Commit Graph

1622 Commits

Author SHA1 Message Date
Ryan Houdek
0913741343 Linux: Disable EFAULT handler until we find something that uses it. 2024-01-26 01:54:09 -08:00
Ryan Houdek
929193c16c Linux: Implements support for EFAULT with ppoll's timeout
Only need to handle the timeout structure, the rest is handled in the
kernel itself.
2024-01-26 01:54:09 -08:00
Ryan Houdek
c5ffc0664d Linux: Implements a fault safe memcpy routine
We are required in our syscall emulation to handle cases where pointers
are invalid. This means we need to pessimistically assume a memcpy will
fault when reading application memory.

This implements a signal handler based approach to catching the SIGSEGV
on memcpy and returning an EFAULT if it faults.
2024-01-25 13:50:55 -08:00
Ryan Houdek
be4d1a8860
Merge pull request #3365 from Sonicadvance1/pidof
Tools: Adds new FEXpidof tool
2024-01-15 05:10:56 -08:00
Ryan Houdek
ddfd393789 Tools: Adds new FEXpidof tool
This behaves exactly like pidof but only searches for FEX applications.
This fixes a long standing annoyance of mine that pidof doesn't work for
FEX. This behaves exactly like pidof but knows how to decode the command
line options to pull out the program data.

If the Linux kernel ever accepts the patches for binfmt_misc to change
how the interpreter is handled then this will become redundant, but
until that happens here is a utility that I want.
2024-01-12 19:05:37 -08:00
Billy Laws
9f311cd97e CommonWindows: Split out code invalidation logic from WOW64
This will also be used by FEX ARM64EC module.
2024-01-12 00:12:26 +00:00
Billy Laws
1115ce4a95 CommonWindows: Introduce, and split out CPU feature logic from WOW64
This can also be used by the FEX ARM64EC module.
2024-01-12 00:12:26 +00:00
Billy Laws
03b802cf8e Windows: Use MinGW importlib functions when possible for ntdll
Switch to only defining imports that are not part of MinGW in the FEX importlib
definitions, this is necessary to avoid linker problems with ARM64EC.
2024-01-12 00:12:26 +00:00
Ryan Houdek
3d5f876585 Fixes some new glibc allocations that cropped up
I guess this was handled by brk things before.
2024-01-09 13:55:04 -08:00
Ryan Houdek
d488592eda
Merge pull request #3339 from Sonicadvance1/pass_thread_unaligned_fault_handler
FEXCore: Pass thread object to HandleUnalignedAccess
2024-01-04 18:20:37 -08:00
Ryan Houdek
4b3792196f
Merge pull request #3303 from Sonicadvance1/initial_runtime_longmode_switch
OpcodeDispatcher: Initial support for runtime long-mode switch
2024-01-04 18:17:54 -08:00
Ryan Houdek
c333aac4f9
Merge pull request #3354 from Sonicadvance1/uprev_kernel_2
Linux uprev to v6.6
2024-01-03 14:25:13 -08:00
Ryan Houdek
db7d7a6bd7
Merge pull request #3349 from Sonicadvance1/revert_frontend_ownership
Revert "FEXLoader: Moves thread management to the frontend"
2024-01-03 14:25:04 -08:00
Alyssa Rosenzweig
04a88ed3ab
Merge pull request #3353 from Sonicadvance1/public_interface_cleaning
FEXCore interface cleaning
2024-01-03 15:14:54 -04:00
Alyssa Rosenzweig
9da08b40bd
Merge pull request #3344 from Sonicadvance1/xbyak_upstream
Externals: Update xbyak to v7.02 and switch away from fork
2024-01-03 15:13:58 -04:00
Ryan Houdek
d098545c20 FEXCore: Removes SRA option, it's now permanently enabled 2023-12-28 18:28:02 -08:00
Ryan Houdek
5358af7794 Revert "FEXLoader: Moves thread management to the frontend"
This reverts commit 58f2693954.
2023-12-27 04:33:50 -08:00
Ryan Houdek
f785b38e4d
Merge pull request #3352 from Sonicadvance1/remove_irloader
Removes IRLoader, unittests, and public interface
2023-12-26 11:08:26 -08:00
Ryan Houdek
b8b9b2f1a7 Linux: Update syscalls for v6.6 2023-12-25 08:40:46 -08:00
Ryan Houdek
f02bd5c9a6 Linux/drm: Update to v6.6 2023-12-25 08:28:45 -08:00
Ryan Houdek
2b39813581 Linux: Implement v6.5 syscall that was added
Fairly easy since the arguments can just be passed through.

The rest of the v6.5 changes don't seem to be anything worth caring
about.
2023-12-25 08:17:43 -08:00
Ryan Houdek
923c53c4ea Linux: New socket options 2023-12-25 08:11:15 -08:00
Ryan Houdek
66658d82c6 Linux: Uprev kernel to 6.4
Added a new prctl that we need to emulate.
Some ioctls have changed that we don't really care about or will
naturally work.
2023-12-25 07:59:03 -08:00
Ryan Houdek
b115c144fb FEXCore: Removes NetStream from public API
Only used by GDBServer.
NFC.
2023-12-25 07:07:17 -08:00
Ryan Houdek
1977747fc2 Removes IRLoader, unittests, and public interface
This unit test hasn't really served any purpose for a while now and
mostly just causes pain when reworking things in the IR.

Just remove the IRLoader, its unit tests, the github action steps and
the public FEXCore interface to it. Since it isn't used by anything
other than Thunks.

Also moves some IR definitions from the public API to the backend.
2023-12-25 07:00:29 -08:00
Ryan Houdek
bce694ebb5 FEXCore: Moves BitUtils to FHU
No functional change
2023-12-25 06:38:51 -08:00
Ryan Houdek
4d109c9ce0 Config: Fixes parsing strenum inside of json files
This wasn't wired up before.
2023-12-23 22:32:59 -08:00
Ryan Houdek
1c34b25538 FEX: Removes legacy kernel 32-bit allocator
We only used this so that our Xavier CI system which were running old
kernels could run unit tests. We have now removed the Xaviers from CI
and this is no longer necessary.

Stop pretending that we support kernels older than 5.0 and allowing this
fallback.

The 32-bit allocator is still used for the MAP_32BIT mmap flag, so the
load bearing code can't be fully removed. Just remove the config and the
frontend things using it.
2023-12-21 06:21:01 -08:00
Ryan Houdek
38ad3f0e05 FEXCore: Pass thread object to HandleUnalignedAccess
Currently no functional change but public API breaks should come early.

The thread state object will be used for looking up thread specific
codebuffers in the future when we support MDWE with code mirrors.
2023-12-21 01:55:25 -08:00
Ryan Houdek
ae2f98e017 Mingw: Update for GDT 2023-12-21 01:54:19 -08:00
Ryan Houdek
8648fb1485 FEXCore: Accurately store segment descriptors
Previously we were only storing the 32-bit base address which isn't
actually how segment descriptors work.

In reality segment descriptors are 64-bit descriptors that are laid out
in a particular layout depending on the 4-bit type value. In reality we
only care about code and data segment layouts since the rest are
bonkers.

Describe these descriptors correctly and setup a default code descriptor
for the operating mode that FEX is starting in.
2023-12-21 01:54:18 -08:00
Ryan Houdek
8b24f7fc26 Externals: Update xbyak to v7.02 and switch away from fork
The last few patches we need have been upstreamed so we shouldn't need
our downstream fork anymore.
2023-12-21 01:52:05 -08:00
Ryan Houdek
b1c3737aef
Merge pull request #3342 from Sonicadvance1/fix_proton_3
Fixes Proton again
2023-12-20 23:15:10 -08:00
Ryan Houdek
58f2693954 FEXLoader: Moves thread management to the frontend
Lots going on here.

This moves OS thread object lifetime management and internal thread
state lifetime management to the frontend. This causes a bunch of thread
handling to move from the FEXCore Context to the frontend.

Looking at `FEXCore/include/FEXCore/Core/Context.h` really shows how
much of the API has moved to the frontend that FEXCore no longer needs
to manage. Primarily this makes FEXCore itself no longer need to care
about most of the management of the emulation state.

A large amount of the behaviour moved wholesale from Core.cpp to
LinuxEmulation's ThreadManager.cpp. Which this manages the lifetimes of
both the OS threads and the FEXCore thread state objects.

One feature lost was the instruction capability, but this was already
buggy and is going to be rewritten/fixed when gdbserver work continues.

Now that all of this management is moved to the frontend, the gdbserver
can start improving since it can start managing all thread state
directly.
2023-12-19 17:43:04 -08:00
Ryan Houdek
362cccc210 Syscalls: Fixes invalid register invalidation
FEX was incorrectly invalidating RBX and RBP when a new
thread/clone/fork was created. Only RAX should be set to zero to
signify that it is the child process.

I tried to find some reference as to why this was being done in FEX, and
I seem to recall some code somewhere that was setting up this state.

Sadly I can't seem to find any reference to this being required now by
the linux kernel when it creates new processes, so maybe I was just
gaslighting myself.

Fixes crash in bubblewrap where it was expecting a valid RBP to exist
after it does a clone syscall.

I have unit tests coming for this but it is uncovering some more bugs in
FEX's thread handling. Some of which are getting fixed with the current
PRs that are moving the thread handling to the frontend.

But since proton is broken now, this needs to get in.
2023-12-19 16:01:21 -08:00
Ryan Houdek
c45dc6dd48 Syscalls: Fixes StackSize argument usage
This was left over and was using the wrong argument.
This would cause the stack for the child thread/process to be at the
start instead of the end.

Fixes a crash in bubblewrap.
2023-12-19 15:56:43 -08:00
Ryan Houdek
aa2e8704bc FEXCore: Changes ParentThread ownership from the CTX to the frontend, take 2
Similar to #3284 but works around some of the bugs that one introduced.

This is the minimal amount of changes to move the ownership from FEXCore
to the frontend. Since the frontends don't yet have a full thread state
tracking, there is an opaque pointer that needs to be managed.

In the followup commits this will be changed to have the syscall handler
to be the thread object manager.
2023-12-18 14:54:07 -08:00
Ryan Houdek
c7bc15785b FEXLoader: Moves mingw check up one level
Now that only FEXLoader is under the FEXLoader folder, the mingw build
check can be moved up one level.

No functional change, just moving the check and re-aligning. Might be
good to view without whitespace changes.
2023-12-18 12:16:11 -08:00
Ryan Houdek
c3d2d01c1f TestHarnessRunner: Move to its own tool folder
Needs #3331 merged first

No functional change, just code movement.
2023-12-18 12:16:11 -08:00
Ryan Houdek
f2bcc14cda Tools: Moves IRLoader to independent folder
Which requires moving LinuxEmulation to its own independent folder as
well. Since both IRLoader and FEXLoader rely on it.

No functional change, just moves the the code around.
2023-12-18 12:04:28 -08:00
Ryan Houdek
c4c31d4e80 VDSO: Fixes symbol fetching for a few symbols
When originally written I was testing symbol loading on an x86 host
assuming that the vdso symbol names were the same between x86 and
aarch64.

Turns out this is not true and aarch64 uses `__kernel_` prefix instead
of x86's `__vdso_`.

AArch64 VDSO only provides us with four symbols right now
- __kernel_rt_sigreturn
- __kernel_gettimeofday
- __kernel_clock_gettime
- __kernel_clock_getres

This means that currently AArch64 is missing getcpu and time.
time is unlikely to ever be implemented so the vdso scanning for that is
unlikely to ever get a pointer and will need to go down the glibc path.

getcpu is likely to get an implementation at some point. There's even an
old patch series that implemented it[1]. Currently the patch has been
dropped on the floor for whatever reason but hopefully they will come
back to it.

Benchmarks on Cortex-X1C:
- Native AArch64 clock_gettime bench
   - VDSO: 33.3ns per call
   - glibc (which uses vdso): 33.3ns per call
   - Roughly equivalent
- Emulated x86_64 clock_gettime bench
   - Prior to fix:
      - 49ns per call for both VDSO and glibc
   - After fix:
      - VDSO: ~46ns per call
      - glibc: ~49ns per call
- Emulated i386 clock_gettime bench
   - Only tested after fix
   - VDSO: 50ns per call
   - glibc: 65ns per call

The only way to improve this further would be to optimize thunks to push
certain function signature arguments in registers instead of stack.
Which is a future optimization as time goes on.

[1] https://patchwork.kernel.org/project/linux-kselftest/list/?series=335203
2023-12-15 10:56:58 -08:00
Ryan Houdek
bd13052708
Merge pull request #3315 from Sonicadvance1/clone_fix_allocation
Linux: Adjust when clone allocates stack memory
2023-12-12 07:31:04 -08:00
Ryan Houdek
0a4e064da4
Merge pull request #3296 from Sonicadvance1/mov_thread_creation
FEXCore: Moves OS thread creation to the frontend
2023-12-11 06:35:26 -08:00
Ryan Houdek
5660065eea FEXCore: Moves OS thread creation to the frontend
Fairly lightweight since it is almost 1:1 transplanting the code from
FEXCore in to the SyscallHandler's thread creation code.

Minor changes:
- ExecutionThreadHandler gets freed before executing the thread
   - Saves 16-bytes of memory per thread
- Start all threads paused by default
   - Since I moved the code to the frontend, I noticed we needed to do
     some post thread-creation setup.
   - Without the pause we were racing code execution with TLS setup and
     a few other things.
2023-12-11 06:22:50 -08:00
Ryan Houdek
131bf48f4b Linux: Adjust when clone allocates stack memory
clone needs to allocate memory before the fork locks are held, otherwise
they will hang forever waiting on a locked mutex.

This wasn't previously seen since nothing was using fork with clone3.
2023-12-11 06:21:14 -08:00
Ryan Houdek
499a40ecb5 GdbServer: Fixes crash on gdb detach
GdbServer object was getting deleted before the socket thread was
shutdown which was causing a crash on detach.

Now on destructor it will wait for the thread to thread to exit.
2023-12-08 06:03:58 -08:00
Ryan Houdek
7524029a06
Merge pull request #3294 from Sonicadvance1/mov_xid_check
FEXCore: Moves XID check to the frontend
2023-12-07 01:17:45 -08:00
Ryan Houdek
26c9d5d427
Merge pull request #3310 from Sonicadvance1/disable_clear_sighand
FEXLoader: Temporarily disable CLONE_CLEAR_SIGHAND
2023-12-06 00:04:16 -08:00
Ryan Houdek
60b0852cde
Merge pull request #3309 from neobrain/fix_thunks_32bit_addresses
Thunks: Add workarounds for pointers not readable by 32-bit guests
2023-12-05 23:52:59 -08:00
Ryan Houdek
301310329b FEXLoader: Temporarily disable CLONE_CLEAR_SIGHAND
This flag breaks FEX heavily for now.
glibc 2.38 started using this flag as an optimization for posix_spawn.
It will fall back to a "non-optimized" implementation if the clone
syscall returns EINVAL. For now do this while we investigate a more
proper implementation.

Should be backported to 2312.1.
2023-12-05 23:46:48 -08:00