aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Cpu/AppleHv/HvCodePatcher.cs
blob: 876597b78c77bc6997689c6478bd212486dbf023 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;

namespace Ryujinx.Cpu.AppleHv
{
    static class HvCodePatcher
    {
        private const uint XMask = 0x3f808000u;
        private const uint XValue = 0x8000000u;

        private const uint ZrIndex = 31u;

        public static void RewriteUnorderedExclusiveInstructions(Span<byte> code)
        {
            Span<uint> codeUint = MemoryMarshal.Cast<byte, uint>(code);
            Span<Vector128<uint>> codeVector = MemoryMarshal.Cast<byte, Vector128<uint>>(code);

            Vector128<uint> mask = Vector128.Create(XMask);
            Vector128<uint> value = Vector128.Create(XValue);

            for (int index = 0; index < codeVector.Length; index++)
            {
                Vector128<uint> v = codeVector[index];

                if (Vector128.EqualsAny(Vector128.BitwiseAnd(v, mask), value))
                {
                    int baseIndex = index * 4;

                    for (int instIndex = baseIndex; instIndex < baseIndex + 4; instIndex++)
                    {
                        ref uint inst = ref codeUint[instIndex];

                        if ((inst & XMask) != XValue)
                        {
                            continue;
                        }

                        bool isPair = (inst & (1u << 21)) != 0;
                        bool isLoad = (inst & (1u << 22)) != 0;

                        uint rt2 = (inst >> 10) & 0x1fu;
                        uint rs = (inst >> 16) & 0x1fu;

                        if (isLoad && rs != ZrIndex)
                        {
                            continue;
                        }

                        if (!isPair && rt2 != ZrIndex)
                        {
                            continue;
                        }

                        // Set the ordered flag.
                        inst |= 1u << 15;
                    }
                }
            }
        }
    }
}