aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Cpu/AppleHv/HvExecutionContext.cs
blob: fc2b76d1514c7b7d9ce579018ad620d79cc0380e (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using ARMeilleure.State;
using Ryujinx.Cpu.AppleHv.Arm;
using Ryujinx.Memory.Tracking;
using System;
using System.Runtime.Versioning;
using System.Threading;

namespace Ryujinx.Cpu.AppleHv
{
    [SupportedOSPlatform("macos")]
    class HvExecutionContext : IExecutionContext
    {
        /// <inheritdoc/>
        public ulong Pc => _impl.ElrEl1;

        /// <inheritdoc/>
        public long TpidrEl0
        {
            get => _impl.TpidrEl0;
            set => _impl.TpidrEl0 = value;
        }

        /// <inheritdoc/>
        public long TpidrroEl0
        {
            get => _impl.TpidrroEl0;
            set => _impl.TpidrroEl0 = value;
        }

        /// <inheritdoc/>
        public uint Pstate
        {
            get => _impl.Pstate;
            set => _impl.Pstate = value;
        }

        /// <inheritdoc/>
        public uint Fpcr
        {
            get => _impl.Fpcr;
            set => _impl.Fpcr = value;
        }

        /// <inheritdoc/>
        public uint Fpsr
        {
            get => _impl.Fpsr;
            set => _impl.Fpsr = value;
        }

        /// <inheritdoc/>
        public bool IsAarch32
        {
            get => false;
            set
            {
                if (value)
                {
                    throw new NotSupportedException();
                }
            }
        }

        /// <inheritdoc/>
        public bool Running { get; private set; }

        private readonly ICounter _counter;
        private readonly IHvExecutionContext _shadowContext;
        private IHvExecutionContext _impl;

        private readonly ExceptionCallbacks _exceptionCallbacks;

        private int _interruptRequested;

        public HvExecutionContext(ICounter counter, ExceptionCallbacks exceptionCallbacks)
        {
            _counter = counter;
            _shadowContext = new HvExecutionContextShadow();
            _impl = _shadowContext;
            _exceptionCallbacks = exceptionCallbacks;
            Running = true;
        }

        /// <inheritdoc/>
        public ulong GetX(int index) => _impl.GetX(index);

        /// <inheritdoc/>
        public void SetX(int index, ulong value) => _impl.SetX(index, value);

        /// <inheritdoc/>
        public V128 GetV(int index) => _impl.GetV(index);

        /// <inheritdoc/>
        public void SetV(int index, V128 value) => _impl.SetV(index, value);

        private void InterruptHandler()
        {
            _exceptionCallbacks.InterruptCallback?.Invoke(this);
        }

        private void BreakHandler(ulong address, int imm)
        {
            _exceptionCallbacks.BreakCallback?.Invoke(this, address, imm);
        }

        private void SupervisorCallHandler(ulong address, int imm)
        {
            _exceptionCallbacks.SupervisorCallback?.Invoke(this, address, imm);
        }

        private void UndefinedHandler(ulong address, int opCode)
        {
            _exceptionCallbacks.UndefinedCallback?.Invoke(this, address, opCode);
        }

        /// <inheritdoc/>
        public void RequestInterrupt()
        {
            if (Interlocked.Exchange(ref _interruptRequested, 1) == 0 && _impl is HvExecutionContextVcpu impl)
            {
                impl.RequestInterrupt();
            }
        }

        private bool GetAndClearInterruptRequested()
        {
            return Interlocked.Exchange(ref _interruptRequested, 0) != 0;
        }

        /// <inheritdoc/>
        public void StopRunning()
        {
            Running = false;
            RequestInterrupt();
        }

        public unsafe void Execute(HvMemoryManager memoryManager, ulong address)
        {
            HvVcpu vcpu = HvVcpuPool.Instance.Create(memoryManager.AddressSpace, _shadowContext, SwapContext);

            HvApi.hv_vcpu_set_reg(vcpu.Handle, HvReg.PC, address).ThrowOnError();

            while (Running)
            {
                HvApi.hv_vcpu_run(vcpu.Handle).ThrowOnError();

                HvExitReason reason = vcpu.ExitInfo->Reason;

                if (reason == HvExitReason.Exception)
                {
                    uint hvEsr = (uint)vcpu.ExitInfo->Exception.Syndrome;
                    ExceptionClass hvEc = (ExceptionClass)(hvEsr >> 26);

                    if (hvEc != ExceptionClass.HvcAarch64)
                    {
                        throw new Exception($"Unhandled exception from guest kernel with ESR 0x{hvEsr:X} ({hvEc}).");
                    }

                    address = SynchronousException(memoryManager, ref vcpu);
                    HvApi.hv_vcpu_set_reg(vcpu.Handle, HvReg.PC, address).ThrowOnError();
                }
                else if (reason == HvExitReason.Canceled || reason == HvExitReason.VTimerActivated)
                {
                    if (GetAndClearInterruptRequested())
                    {
                        ReturnToPool(vcpu);
                        InterruptHandler();
                        vcpu = RentFromPool(memoryManager.AddressSpace, vcpu);
                    }

                    if (reason == HvExitReason.VTimerActivated)
                    {
                        vcpu.EnableAndUpdateVTimer();

                        // Unmask VTimer interrupts.
                        HvApi.hv_vcpu_set_vtimer_mask(vcpu.Handle, false).ThrowOnError();
                    }
                }
                else
                {
                    throw new Exception($"Unhandled exit reason {reason}.");
                }
            }

            HvVcpuPool.Instance.Destroy(vcpu, SwapContext);
        }

        private ulong SynchronousException(HvMemoryManager memoryManager, ref HvVcpu vcpu)
        {
            ulong vcpuHandle = vcpu.Handle;

            HvApi.hv_vcpu_get_sys_reg(vcpuHandle, HvSysReg.ELR_EL1, out ulong elr).ThrowOnError();
            HvApi.hv_vcpu_get_sys_reg(vcpuHandle, HvSysReg.ESR_EL1, out ulong esr).ThrowOnError();

            ExceptionClass ec = (ExceptionClass)((uint)esr >> 26);

            switch (ec)
            {
                case ExceptionClass.DataAbortLowerEl:
                    DataAbort(memoryManager.Tracking, vcpuHandle, (uint)esr);
                    break;
                case ExceptionClass.TrappedMsrMrsSystem:
                    InstructionTrap((uint)esr);
                    HvApi.hv_vcpu_set_sys_reg(vcpuHandle, HvSysReg.ELR_EL1, elr + 4UL).ThrowOnError();
                    break;
                case ExceptionClass.SvcAarch64:
                    ReturnToPool(vcpu);
                    ushort id = (ushort)esr;
                    SupervisorCallHandler(elr - 4UL, id);
                    vcpu = RentFromPool(memoryManager.AddressSpace, vcpu);
                    break;
                default:
                    throw new Exception($"Unhandled guest exception {ec}.");
            }

            // Make sure we will continue running at EL0.
            if (memoryManager.AddressSpace.GetAndClearUserTlbInvalidationPending())
            {
                // TODO: Invalidate only the range that was modified?
                return HvAddressSpace.KernelRegionTlbiEretAddress;
            }
            else
            {
                return HvAddressSpace.KernelRegionEretAddress;
            }
        }

        private static void DataAbort(MemoryTracking tracking, ulong vcpu, uint esr)
        {
            bool write = (esr & (1u << 6)) != 0;
            bool farValid = (esr & (1u << 10)) == 0;
            int accessSizeLog2 = (int)((esr >> 22) & 3);

            if (farValid)
            {
                HvApi.hv_vcpu_get_sys_reg(vcpu, HvSysReg.FAR_EL1, out ulong far).ThrowOnError();

                ulong size = 1UL << accessSizeLog2;

                if (!tracking.VirtualMemoryEvent(far, size, write))
                {
                    string rw = write ? "write" : "read";
                    throw new Exception($"Unhandled invalid memory access at VA 0x{far:X} with size 0x{size:X} ({rw}).");
                }
            }
            else
            {
                throw new Exception($"Unhandled invalid memory access at unknown VA with ESR 0x{esr:X}.");
            }
        }

        private void InstructionTrap(uint esr)
        {
            bool read = (esr & 1) != 0;
            uint rt = (esr >> 5) & 0x1f;

            if (read)
            {
                // Op0 Op2 Op1 CRn 00000 CRm
                switch ((esr >> 1) & 0x1ffe0f)
                {
                    case 0b11_000_011_1110_00000_0000: // CNTFRQ_EL0
                        WriteRt(rt, _counter.Frequency);
                        break;
                    case 0b11_001_011_1110_00000_0000: // CNTPCT_EL0
                        WriteRt(rt, _counter.Counter);
                        break;
                    default:
                        throw new Exception($"Unhandled system register read with ESR 0x{esr:X}");
                }
            }
            else
            {
                throw new Exception($"Unhandled system register write with ESR 0x{esr:X}");
            }
        }

        private void WriteRt(uint rt, ulong value)
        {
            if (rt < 31)
            {
                SetX((int)rt, value);
            }
        }

        private void ReturnToPool(HvVcpu vcpu)
        {
            HvVcpuPool.Instance.Return(vcpu, SwapContext);
        }

        private HvVcpu RentFromPool(HvAddressSpace addressSpace, HvVcpu vcpu)
        {
            return HvVcpuPool.Instance.Rent(addressSpace, _shadowContext, vcpu, SwapContext);
        }

        private void SwapContext(IHvExecutionContext newContext)
        {
            _impl = newContext;
        }

        public void Dispose()
        {
        }
    }
}