forked from MeloNX/MeloNX
armeilleure: Add Android signal handler
This commit is contained in:
parent
8a8db6244d
commit
b7d08e5050
@ -111,7 +111,7 @@ namespace ARMeilleure.Signal
|
|||||||
return context.BitwiseAnd(err, Const(2ul));
|
return context.BitwiseAnd(err, Const(2ul));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (OperatingSystem.IsLinux())
|
else if (OperatingSystem.IsLinux() || OperatingSystem.IsAndroid())
|
||||||
{
|
{
|
||||||
if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
|
if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
|
||||||
{
|
{
|
||||||
|
@ -88,10 +88,16 @@ namespace Ryujinx.Cpu.Signal
|
|||||||
|
|
||||||
ref SignalHandlerConfig config = ref GetConfigRef();
|
ref SignalHandlerConfig config = ref GetConfigRef();
|
||||||
|
|
||||||
if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS() || OperatingSystem.IsAndroid())
|
||||||
{
|
{
|
||||||
_signalHandlerPtr = MapCode(NativeSignalHandlerGenerator.GenerateUnixSignalHandler(_handlerConfig, rangeStructSize, pageSize));
|
_signalHandlerPtr = MapCode(NativeSignalHandlerGenerator.GenerateUnixSignalHandler(_handlerConfig, rangeStructSize, pageSize));
|
||||||
|
|
||||||
|
if (OperatingSystem.IsAndroid())
|
||||||
|
{
|
||||||
|
config.StructAddressOffset = 16; // si_addr
|
||||||
|
config.StructWriteOffset = 8; // si_code
|
||||||
|
}
|
||||||
|
|
||||||
if (customSignalHandlerFactory != null)
|
if (customSignalHandlerFactory != null)
|
||||||
{
|
{
|
||||||
_signalHandlerPtr = customSignalHandlerFactory(UnixSignalHandlerRegistration.GetSegfaultExceptionHandler().sa_handler, _signalHandlerPtr);
|
_signalHandlerPtr = customSignalHandlerFactory(UnixSignalHandlerRegistration.GetSegfaultExceptionHandler().sa_handler, _signalHandlerPtr);
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Runtime.Versioning;
|
||||||
|
|
||||||
namespace Ryujinx.Cpu.Signal
|
namespace Ryujinx.Cpu.Signal
|
||||||
{
|
{
|
||||||
@ -20,6 +21,16 @@ namespace Ryujinx.Cpu.Signal
|
|||||||
public IntPtr sa_restorer;
|
public IntPtr sa_restorer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[SupportedOSPlatform("android")]
|
||||||
|
[StructLayout(LayoutKind.Sequential, Pack = 8)]
|
||||||
|
public struct SigActionBionic
|
||||||
|
{
|
||||||
|
public int sa_flags;
|
||||||
|
public IntPtr sa_handler;
|
||||||
|
public SigSet sa_mask;
|
||||||
|
public IntPtr sa_restorer;
|
||||||
|
}
|
||||||
|
|
||||||
private const int SIGSEGV = 11;
|
private const int SIGSEGV = 11;
|
||||||
private const int SIGBUS = 10;
|
private const int SIGBUS = 10;
|
||||||
private const int SA_SIGINFO = 0x00000004;
|
private const int SA_SIGINFO = 0x00000004;
|
||||||
@ -27,15 +38,41 @@ namespace Ryujinx.Cpu.Signal
|
|||||||
[LibraryImport("libc", SetLastError = true)]
|
[LibraryImport("libc", SetLastError = true)]
|
||||||
private static partial int sigaction(int signum, ref SigAction sigAction, out SigAction oldAction);
|
private static partial int sigaction(int signum, ref SigAction sigAction, out SigAction oldAction);
|
||||||
|
|
||||||
|
[SupportedOSPlatform("android")]
|
||||||
|
[LibraryImport("libc", SetLastError = true)]
|
||||||
|
private static partial int sigaction(int signum, ref SigActionBionic sigAction, out SigActionBionic oldAction);
|
||||||
|
|
||||||
[LibraryImport("libc", SetLastError = true)]
|
[LibraryImport("libc", SetLastError = true)]
|
||||||
private static partial int sigaction(int signum, IntPtr sigAction, out SigAction oldAction);
|
private static partial int sigaction(int signum, IntPtr sigAction, out SigAction oldAction);
|
||||||
|
|
||||||
|
[SupportedOSPlatform("android")]
|
||||||
|
[LibraryImport("libc", SetLastError = true)]
|
||||||
|
private static partial int sigaction(int signum, IntPtr sigAction, out SigActionBionic oldAction);
|
||||||
|
|
||||||
[LibraryImport("libc", SetLastError = true)]
|
[LibraryImport("libc", SetLastError = true)]
|
||||||
private static partial int sigemptyset(ref SigSet set);
|
private static partial int sigemptyset(ref SigSet set);
|
||||||
|
|
||||||
public static SigAction GetSegfaultExceptionHandler()
|
public static SigAction GetSegfaultExceptionHandler()
|
||||||
{
|
{
|
||||||
int result = sigaction(SIGSEGV, IntPtr.Zero, out SigAction old);
|
int result;
|
||||||
|
SigAction old;
|
||||||
|
|
||||||
|
if (OperatingSystem.IsAndroid())
|
||||||
|
{
|
||||||
|
result = sigaction(SIGSEGV, IntPtr.Zero, out SigActionBionic tmp);
|
||||||
|
|
||||||
|
old = new SigAction
|
||||||
|
{
|
||||||
|
sa_handler = tmp.sa_handler,
|
||||||
|
sa_mask = tmp.sa_mask,
|
||||||
|
sa_flags = tmp.sa_flags,
|
||||||
|
sa_restorer = tmp.sa_restorer
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = sigaction(SIGSEGV, IntPtr.Zero, out old);
|
||||||
|
}
|
||||||
|
|
||||||
if (result != 0)
|
if (result != 0)
|
||||||
{
|
{
|
||||||
@ -47,28 +84,69 @@ namespace Ryujinx.Cpu.Signal
|
|||||||
|
|
||||||
public static SigAction RegisterExceptionHandler(IntPtr action)
|
public static SigAction RegisterExceptionHandler(IntPtr action)
|
||||||
{
|
{
|
||||||
SigAction sig = new()
|
int result;
|
||||||
|
SigAction old;
|
||||||
|
|
||||||
|
if (Ryujinx.Common.SystemInfo.SystemInfo.IsAndroid())
|
||||||
{
|
{
|
||||||
sa_handler = action,
|
SigActionBionic sig = new()
|
||||||
sa_flags = SA_SIGINFO,
|
{
|
||||||
};
|
sa_handler = action,
|
||||||
|
sa_flags = SA_SIGINFO
|
||||||
|
};
|
||||||
|
|
||||||
sigemptyset(ref sig.sa_mask);
|
sigemptyset(ref sig.sa_mask);
|
||||||
|
|
||||||
int result = sigaction(SIGSEGV, ref sig, out SigAction old);
|
result = sigaction(SIGSEGV, ref sig, out SigActionBionic tmp);
|
||||||
|
|
||||||
if (result != 0)
|
old = new SigAction
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"Could not register SIGSEGV sigaction. Error: {result}");
|
sa_handler = tmp.sa_handler,
|
||||||
|
sa_mask = tmp.sa_mask,
|
||||||
|
sa_flags = tmp.sa_flags,
|
||||||
|
sa_restorer = tmp.sa_restorer
|
||||||
|
};
|
||||||
|
|
||||||
|
if (userSignal != -1)
|
||||||
|
{
|
||||||
|
result = sigaction(userSignal, ref sig, out SigActionBionic oldu);
|
||||||
|
|
||||||
|
if (oldu.sa_handler != IntPtr.Zero)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"SIG{userSignal} is already in use.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result != 0)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Could not register SIG{userSignal} sigaction. Error: {result}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
if (OperatingSystem.IsMacOS())
|
|
||||||
{
|
{
|
||||||
result = sigaction(SIGBUS, ref sig, out _);
|
SigAction sig = new SigAction
|
||||||
|
{
|
||||||
|
sa_handler = action,
|
||||||
|
sa_flags = SA_SIGINFO
|
||||||
|
};
|
||||||
|
|
||||||
|
sigemptyset(ref sig.sa_mask);
|
||||||
|
|
||||||
|
result = sigaction(SIGSEGV, ref sig, out old);
|
||||||
|
|
||||||
if (result != 0)
|
if (result != 0)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"Could not register SIGBUS sigaction. Error: {result}");
|
throw new InvalidOperationException($"Could not register SIGSEGV sigaction. Error: {result}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (OperatingSystem.IsMacOS())
|
||||||
|
{
|
||||||
|
result = sigaction(SIGBUS, ref sig, out _);
|
||||||
|
|
||||||
|
if (result != 0)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Could not register SIGBUS sigaction. Error: {result}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +155,29 @@ namespace Ryujinx.Cpu.Signal
|
|||||||
|
|
||||||
public static bool RestoreExceptionHandler(SigAction oldAction)
|
public static bool RestoreExceptionHandler(SigAction oldAction)
|
||||||
{
|
{
|
||||||
return sigaction(SIGSEGV, ref oldAction, out SigAction _) == 0 && (!OperatingSystem.IsMacOS() || sigaction(SIGBUS, ref oldAction, out SigAction _) == 0);
|
if (OperatingSystem.IsAndroid())
|
||||||
|
{
|
||||||
|
SigActionBionic tmp = new SigActionBionic
|
||||||
|
{
|
||||||
|
sa_handler = oldAction.sa_handler,
|
||||||
|
sa_mask = oldAction.sa_mask,
|
||||||
|
sa_flags = oldAction.sa_flags,
|
||||||
|
sa_restorer = oldAction.sa_restorer
|
||||||
|
};
|
||||||
|
|
||||||
|
return sigaction(SIGSEGV, ref tmp, out SigActionBionic _) == 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
bool success = sigaction(SIGSEGV, ref oldAction, out SigAction _) == 0;
|
||||||
|
|
||||||
|
if (success && OperatingSystem.IsMacOS())
|
||||||
|
{
|
||||||
|
success = sigaction(SIGBUS, ref oldAction, out SigAction _) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user