aboutsummaryrefslogtreecommitdiff
path: root/ChocolArm64/Memory/CompareExchange128.cs
blob: 1618ff0fbcccb4893f5f6edf357cb43f7b9b912d (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Runtime.InteropServices;

namespace ChocolArm64.Memory
{
    static class CompareExchange128
    {
        private struct Int128
        {
            public ulong Low  { get; }
            public ulong High { get; }

            public Int128(ulong low, ulong high)
            {
                Low  = low;
                High = high;
            }
        }

        private delegate Int128 InterlockedCompareExchange(IntPtr address, Int128 expected, Int128 desired);

        private delegate int GetCpuId();

        private static InterlockedCompareExchange _interlockedCompareExchange;

        static CompareExchange128()
        {
            if (RuntimeInformation.OSArchitecture != Architecture.X64 || !IsCmpxchg16bSupported())
            {
                throw new PlatformNotSupportedException();
            }

            byte[] interlockedCompareExchange128Code;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                interlockedCompareExchange128Code = new byte[]
                {
                    0x53,                         // push rbx
                    0x49, 0x8b, 0x00,             // mov  rax, [r8]
                    0x49, 0x8b, 0x19,             // mov  rbx, [r9]
                    0x49, 0x89, 0xca,             // mov  r10, rcx
                    0x49, 0x89, 0xd3,             // mov  r11, rdx
                    0x49, 0x8b, 0x49, 0x08,       // mov  rcx, [r9+8]
                    0x49, 0x8b, 0x50, 0x08,       // mov  rdx, [r8+8]
                    0xf0, 0x49, 0x0f, 0xc7, 0x0b, // lock cmpxchg16b [r11]
                    0x49, 0x89, 0x02,             // mov  [r10], rax
                    0x4c, 0x89, 0xd0,             // mov  rax, r10
                    0x49, 0x89, 0x52, 0x08,       // mov  [r10+8], rdx
                    0x5b,                         // pop  rbx
                    0xc3                          // ret
                };
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
                     RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                interlockedCompareExchange128Code = new byte[]
                {
                    0x53,                         // push rbx
                    0x49, 0x89, 0xd1,             // mov  r9, rdx
                    0x48, 0x89, 0xcb,             // mov  rbx, rcx
                    0x48, 0x89, 0xf0,             // mov  rax, rsi
                    0x4c, 0x89, 0xca,             // mov  rdx, r9
                    0x4c, 0x89, 0xc1,             // mov  rcx, r8
                    0xf0, 0x48, 0x0f, 0xc7, 0x0f, // lock cmpxchg16b [rdi]
                    0x5b,                         // pop  rbx
                    0xc3                          // ret
                };
            }
            else
            {
                throw new PlatformNotSupportedException();
            }

            IntPtr funcPtr = MapCodeAsExecutable(interlockedCompareExchange128Code);

            _interlockedCompareExchange = Marshal.GetDelegateForFunctionPointer<InterlockedCompareExchange>(funcPtr);
        }

        private static bool IsCmpxchg16bSupported()
        {
            byte[] getCpuIdCode = new byte[]
            {
                0x53,                         // push rbx
                0xb8, 0x01, 0x00, 0x00, 0x00, // mov eax, 0x1
                0x0f, 0xa2,                   // cpuid
                0x89, 0xc8,                   // mov eax, ecx
                0x5b,                         // pop rbx
                0xc3                          // ret
            };

            IntPtr funcPtr = MapCodeAsExecutable(getCpuIdCode);

            GetCpuId getCpuId = Marshal.GetDelegateForFunctionPointer<GetCpuId>(funcPtr);

            int cpuId = getCpuId();

            MemoryManagement.Free(funcPtr);

            return (cpuId & (1 << 13)) != 0;
        }

        private static IntPtr MapCodeAsExecutable(byte[] code)
        {
            ulong codeLength = (ulong)code.Length;

            IntPtr funcPtr = MemoryManagement.Allocate(codeLength);

            unsafe
            {
                fixed (byte* codePtr = code)
                {
                    byte* dest = (byte*)funcPtr;

                    long size = (long)codeLength;

                    Buffer.MemoryCopy(codePtr, dest, size, size);
                }
            }

            MemoryManagement.Reprotect(funcPtr, codeLength, MemoryProtection.Execute);

            return funcPtr;
        }

        public static bool InterlockedCompareExchange128(
            IntPtr address,
            ulong  expectedLow,
            ulong  expectedHigh,
            ulong  desiredLow,
            ulong  desiredHigh)
        {
            Int128 expected = new Int128(expectedLow, expectedHigh);
            Int128 desired  = new Int128(desiredLow,  desiredHigh);

            Int128 old = _interlockedCompareExchange(address, expected, desired);

            return old.Low == expected.Low && old.High == expected.High;
        }

        public static void InterlockedRead128(IntPtr address, out ulong low, out ulong high)
        {
            Int128 zero = new Int128(0, 0);

            Int128 old = _interlockedCompareExchange(address, zero, zero);

            low  = old.Low;
            high = old.High;
        }
    }
}