aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Cpu/AppleHv/HvVm.cs
blob: c4f107532e4ebe5577577e9388944fa0c87f0a7c (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
using Ryujinx.Memory;
using System;
using System.Runtime.Versioning;

namespace Ryujinx.Cpu.AppleHv
{
    [SupportedOSPlatform("macos")]
    static class HvVm
    {
        // This alignment allows us to use larger blocks on the page table.
        private const ulong AsIpaAlignment = 1UL << 30;

        private static int _addressSpaces;
        private static HvIpaAllocator _ipaAllocator;
        private static readonly object _lock = new();

        public static (ulong, HvIpaAllocator) CreateAddressSpace(MemoryBlock block)
        {
            HvIpaAllocator ipaAllocator;

            lock (_lock)
            {
                if (++_addressSpaces == 1)
                {
                    HvApi.hv_vm_create(IntPtr.Zero).ThrowOnError();
                    _ipaAllocator = ipaAllocator = new HvIpaAllocator();
                }
                else
                {
                    ipaAllocator = _ipaAllocator;
                }
            }

            ulong baseAddress;

            lock (ipaAllocator)
            {
                baseAddress = ipaAllocator.Allocate(block.Size, AsIpaAlignment);
            }

            var rwx = HvMemoryFlags.Read | HvMemoryFlags.Write | HvMemoryFlags.Exec;

            HvApi.hv_vm_map((ulong)block.Pointer, baseAddress, block.Size, rwx).ThrowOnError();

            return (baseAddress, ipaAllocator);
        }

        public static void DestroyAddressSpace(ulong address, ulong size)
        {
            HvApi.hv_vm_unmap(address, size);

            HvIpaAllocator ipaAllocator;

            lock (_lock)
            {
                if (--_addressSpaces == 0)
                {
                    HvApi.hv_vm_destroy().ThrowOnError();
                }

                ipaAllocator = _ipaAllocator;
            }

            lock (ipaAllocator)
            {
                ipaAllocator.Free(address, size);
            }
        }
    }
}