예제 #1
0
        public static void Close()
        {
            Rdtsc = null;
            Cpuid = null;

            Kernel32.VirtualFree(_codeBuffer, UIntPtr.Zero, Kernel32.MEM.MEM_RELEASE);
        }
예제 #2
0
        public static void Open()
        {
            byte[] rdtscCode;
            byte[] cpuidCode;
            if (IntPtr.Size == 4)
            {
                rdtscCode = RDTSC_32;
                cpuidCode = CPUID_32;
            }
            else
            {
                rdtscCode = RDTSC_64;
                cpuidCode = CPUID_64_WINDOWS;
            }

            size = (ulong)(rdtscCode.Length + cpuidCode.Length);

            // Windows
            codeBuffer = VirtualAlloc(IntPtr.Zero, (UIntPtr)size, AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE);

            Marshal.Copy(rdtscCode, 0, codeBuffer, rdtscCode.Length);

            Rdtsc = Marshal.GetDelegateForFunctionPointer(
                codeBuffer, typeof(RdtscDelegate)) as RdtscDelegate;

            var cpuidAddress = (IntPtr)((long)codeBuffer + rdtscCode.Length);

            Marshal.Copy(cpuidCode, 0, cpuidAddress, cpuidCode.Length);

            Cpuid = Marshal.GetDelegateForFunctionPointer(
                cpuidAddress, typeof(CpuidDelegate)) as CpuidDelegate;
        }
예제 #3
0
        public static void Close()
        {
            Rdtsc = null;
            Cpuid = null;

            var p = (int)Environment.OSVersion.Platform;

            if ((p == 4) || (p == 128))
            {
                // Unix
                var assembly =
                    Assembly.Load("Mono.Posix, Version=2.0.0.0, Culture=neutral, " +
                                  "PublicKeyToken=0738eb9f132ed756");

                var syscall = assembly.GetType("Mono.Unix.Native.Syscall");
                var munmap  = syscall.GetMethod("munmap");
                munmap.Invoke(null, new object[] { codeBuffer, size });
            }
            else
            {
                // Windows
                NativeMethods.VirtualFree(codeBuffer, UIntPtr.Zero,
                                          FreeType.RELEASE);
            }
        }
예제 #4
0
        public static void Open()
        {
            byte[] rdTscCode;
            byte[] cpuidCode;
            if (IntPtr.Size == 4)
            {
                rdTscCode = Rdtsc32;
                cpuidCode = CpuId32;
            }
            else
            {
                rdTscCode = Rdtsc64;

                cpuidCode = CpuId64Windows;
            }

            _size = (ulong)(rdTscCode.Length + cpuidCode.Length);

            _codeBuffer = Kernel32.VirtualAlloc(IntPtr.Zero,
                                                (UIntPtr)_size,
                                                Kernel32.MEM.MEM_COMMIT | Kernel32.MEM.MEM_RESERVE,
                                                Kernel32.PAGE.PAGE_EXECUTE_READWRITE);

            Marshal.Copy(rdTscCode, 0, _codeBuffer, rdTscCode.Length);
            Rdtsc = Marshal.GetDelegateForFunctionPointer(_codeBuffer, typeof(RdtscDelegate)) as RdtscDelegate;
            IntPtr cpuidAddress = (IntPtr)((long)_codeBuffer + rdTscCode.Length);

            Marshal.Copy(cpuidCode, 0, cpuidAddress, cpuidCode.Length);
            Cpuid = Marshal.GetDelegateForFunctionPointer(cpuidAddress, typeof(CpuidDelegate)) as CpuidDelegate;
        }
예제 #5
0
        public static void Open()
        {
            byte[] rdTscCode;
            byte[] cpuidCode;
            if (IntPtr.Size == 4)
            {
                rdTscCode = Rdtsc32;
                cpuidCode = CpuId32;
            }
            else
            {
                rdTscCode = Rdtsc64;

                cpuidCode = Software.OperatingSystem.IsUnix ? CpuId64Linux : CpuId64Windows;
            }

            _size = (ulong)(rdTscCode.Length + cpuidCode.Length);

            if (Software.OperatingSystem.IsUnix)
            {
#if NETFRAMEWORK
                Assembly assembly = Assembly.Load("Mono.Posix, Version=2.0.0.0, Culture=neutral, " + "PublicKeyToken=0738eb9f132ed756");
#else
                Assembly assembly = Assembly.Load("Mono.Posix.NETStandard, Version=1.0.0.0, Culture=neutral");
#endif

                Type       sysCall = assembly.GetType("Mono.Unix.Native.Syscall");
                MethodInfo mmap    = sysCall.GetMethod("mmap");

                Type   mmapProts      = assembly.GetType("Mono.Unix.Native.MmapProts");
                object mmapProtsParam = Enum.ToObject(mmapProts,
                                                      (int)mmapProts.GetField("PROT_READ").GetValue(null) |
                                                      (int)mmapProts.GetField("PROT_WRITE").GetValue(null) |
                                                      (int)mmapProts.GetField("PROT_EXEC").GetValue(null));

                Type   mmapFlags      = assembly.GetType("Mono.Unix.Native.MmapFlags");
                object mmapFlagsParam = Enum.ToObject(mmapFlags,
                                                      (int)mmapFlags.GetField("MAP_ANONYMOUS").GetValue(null) |
                                                      (int)mmapFlags.GetField("MAP_PRIVATE").GetValue(null));

                if (mmap != null)
                {
                    _codeBuffer = (IntPtr)mmap.Invoke(null, new[] { IntPtr.Zero, _size, mmapProtsParam, mmapFlagsParam, -1, 0 });
                }
            }
            else
            {
                _codeBuffer = Interop.Kernel32.VirtualAlloc(IntPtr.Zero,
                                                            (UIntPtr)_size,
                                                            Interop.Kernel32.MEM.MEM_COMMIT | Interop.Kernel32.MEM.MEM_RESERVE,
                                                            Interop.Kernel32.PAGE.PAGE_EXECUTE_READWRITE);
            }

            Marshal.Copy(rdTscCode, 0, _codeBuffer, rdTscCode.Length);
            Rdtsc = Marshal.GetDelegateForFunctionPointer(_codeBuffer, typeof(RdtscDelegate)) as RdtscDelegate;
            IntPtr cpuidAddress = (IntPtr)((long)_codeBuffer + rdTscCode.Length);
            Marshal.Copy(cpuidCode, 0, cpuidAddress, cpuidCode.Length);
            CpuId = Marshal.GetDelegateForFunctionPointer(cpuidAddress, typeof(CpuidDelegate)) as CpuidDelegate;
        }
예제 #6
0
        public static void Close()
        {
            Rdtsc = null;
            Cpuid = null;

            // Windows
            VirtualFree(codeBuffer, UIntPtr.Zero, FreeType.RELEASE);
        }
예제 #7
0
        public static void Open()
        {
            if (IsOpen)
            {
                return;
            }

            IsOpen = true;

            byte[] rdtscCode;
            byte[] cpuidCode;
            if (IntPtr.Size == 4)
            {
                rdtscCode = RDTSC_32;
                cpuidCode = CPUID_32;
            }
            else
            {
                rdtscCode = RDTSC_64;
                cpuidCode = OperatingSystem.IsLinux ? CPUID_64_LINUX : CPUID_64_WINDOWS;
            }

            size = (ulong)(rdtscCode.Length + cpuidCode.Length);

            if (OperatingSystem.IsLinux)
            {
                // Unix
                codeBuffer = Syscall.mmap(
                    IntPtr.Zero,
                    size,
                    MmapProts.PROT_READ | MmapProts.PROT_WRITE | MmapProts.PROT_EXEC,
                    MmapFlags.MAP_ANONYMOUS | MmapFlags.MAP_PRIVATE,
                    -1, 0);
            }
            else
            {
                // Windows
                codeBuffer = NativeMethods.VirtualAlloc(IntPtr.Zero,
                                                        (UIntPtr)size, AllocationType.COMMIT | AllocationType.RESERVE,
                                                        MemoryProtection.EXECUTE_READWRITE);
            }

            Marshal.Copy(rdtscCode, 0, codeBuffer, rdtscCode.Length);

            Rdtsc = Marshal.GetDelegateForFunctionPointer(
                codeBuffer, typeof(RdtscDelegate)) as RdtscDelegate;

            var cpuidAddress = (IntPtr)((long)codeBuffer + rdtscCode.Length);

            Marshal.Copy(cpuidCode, 0, cpuidAddress, cpuidCode.Length);

            Cpuid = Marshal.GetDelegateForFunctionPointer(
                cpuidAddress, typeof(CpuidDelegate)) as CpuidDelegate;
        }
예제 #8
0
        internal static void Close()
        {
            Rdtsc = null;
            Cpuid = null;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                // Unix
                NativeMethods.munmap(codeBuffer, size);
            }
            else
            {
                // Windows
                NativeMethods.VirtualFree(codeBuffer, UIntPtr.Zero,
                                          FreeType.RELEASE);
            }
        }
예제 #9
0
        public static void Close()
        {
            Rdtsc = null;
            CpuId = null;

            if (Software.OperatingSystem.IsUnix)
            {
                Assembly   assembly = Assembly.Load("Mono.Posix, Version=2.0.0.0, Culture=neutral, " + "PublicKeyToken=0738eb9f132ed756");
                Type       sysCall  = assembly.GetType("Mono.Unix.Native.Syscall");
                MethodInfo method   = sysCall.GetMethod("munmap");
                method?.Invoke(null, new object[] { _codeBuffer, _size });
            }
            else
            {
                Interop.Kernel32.VirtualFree(_codeBuffer, UIntPtr.Zero, Interop.Kernel32.MEM.MEM_RELEASE);
            }
        }
예제 #10
0
        public static void Close()
        {
            Rdtsc = null;
            Cpuid = null;


            if (OperatingSystem.IsLinux)
            {
                // Unix
                Syscall.munmap(codeBuffer, size);
            }
            else
            {
                // Windows
                NativeMethods.VirtualFree(codeBuffer, UIntPtr.Zero,
                                          FreeType.RELEASE);
            }
        }
예제 #11
0
파일: OpCode.cs 프로젝트: jasenkin/netmq
        public static void Close()
        {
            Rdtsc = null;

            int p = (int)Environment.OSVersion.Platform;
            if ((p == 4) || (p == 128))
            { // Unix
                Assembly assembly =
                    Assembly.Load("Mono.Posix, Version=2.0.0.0, Culture=neutral, " +
                    "PublicKeyToken=0738eb9f132ed756");

                Type syscall = assembly.GetType("Mono.Unix.Native.Syscall");
                MethodInfo munmap = syscall.GetMethod("munmap");
                munmap.Invoke(null, new object[] { codeBuffer, size });

            }
            else
            { // Windows
                NativeMethods.VirtualFree(codeBuffer, UIntPtr.Zero,
                    FreeType.RELEASE);
            }
        }
예제 #12
0
파일: Opcode.cs 프로젝트: wind959/ntminer
        public static void Close()
        {
            Rdtsc = null;
            Cpuid = null;


            if (OperatingSystem.IsLinux)
            {
                // Unix
                var assembly =
                    Assembly.Load("Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756");

                var syscall = assembly.GetType("Mono.Unix.Native.Syscall");
                var munmap  = syscall.GetMethod("munmap");
                munmap.Invoke(null, new object[] { codeBuffer, size });
            }
            else
            {
                // Windows
                NativeMethods.VirtualFree(codeBuffer, UIntPtr.Zero,
                                          FreeType.RELEASE);
            }
        }
예제 #13
0
파일: Opcode.cs 프로젝트: adamsp/beastmon
        public static void Open()
        {
            int p = (int)Environment.OSVersion.Platform;

              byte[] rdtscCode;
              byte[] cpuidCode;
              if (IntPtr.Size == 4) {
            rdtscCode = RDTSC_32;
            cpuidCode = CPUID_32;
              } else {
            rdtscCode = RDTSC_64;

            if ((p == 4) || (p == 128)) { // Unix
              cpuidCode = CPUID_64_LINUX;
            } else { // Windows
              cpuidCode = CPUID_64_WINDOWS;
            }
              }

              size = (ulong)(rdtscCode.Length + cpuidCode.Length);

              if ((p == 4) || (p == 128)) { // Unix
            Assembly assembly =
              Assembly.Load("Mono.Posix, Version=2.0.0.0, Culture=neutral, " +
              "PublicKeyToken=0738eb9f132ed756");

            Type syscall = assembly.GetType("Mono.Unix.Native.Syscall");
            MethodInfo mmap = syscall.GetMethod("mmap");

            Type mmapProts = assembly.GetType("Mono.Unix.Native.MmapProts");
            object mmapProtsParam = Enum.ToObject(mmapProts,
              (int)mmapProts.GetField("PROT_READ").GetValue(null) |
              (int)mmapProts.GetField("PROT_WRITE").GetValue(null) |
              (int)mmapProts.GetField("PROT_EXEC").GetValue(null));

            Type mmapFlags = assembly.GetType("Mono.Unix.Native.MmapFlags");
            object mmapFlagsParam = Enum.ToObject(mmapFlags,
              (int)mmapFlags.GetField("MAP_ANONYMOUS").GetValue(null) |
              (int)mmapFlags.GetField("MAP_PRIVATE").GetValue(null));

            codeBuffer = (IntPtr)mmap.Invoke(null, new object[] { IntPtr.Zero,
              size, mmapProtsParam, mmapFlagsParam, -1, 0 });
              } else { // Windows
            codeBuffer = NativeMethods.VirtualAlloc(IntPtr.Zero,
              (UIntPtr)size, AllocationType.COMMIT | AllocationType.RESERVE,
              MemoryProtection.EXECUTE_READWRITE);
              }

              Marshal.Copy(rdtscCode, 0, codeBuffer, rdtscCode.Length);

              Rdtsc = Marshal.GetDelegateForFunctionPointer(
            codeBuffer, typeof(RdtscDelegate)) as RdtscDelegate;

              IntPtr cpuidAddress = (IntPtr)((long)codeBuffer + rdtscCode.Length);
              Marshal.Copy(cpuidCode, 0, cpuidAddress, cpuidCode.Length);

              Cpuid = Marshal.GetDelegateForFunctionPointer(
            cpuidAddress, typeof(CpuidDelegate)) as CpuidDelegate;
        }
예제 #14
0
파일: Opcode.cs 프로젝트: wind959/ntminer
        public static void Open()
        {
            if (IsOpen)
            {
                return;
            }

            IsOpen = true;

            byte[] rdtscCode;
            byte[] cpuidCode;
            if (IntPtr.Size == 4)
            {
                rdtscCode = RDTSC_32;
                cpuidCode = CPUID_32;
            }
            else
            {
                rdtscCode = RDTSC_64;
                cpuidCode = OperatingSystem.IsLinux ? CPUID_64_LINUX : CPUID_64_WINDOWS;
            }

            size = (ulong)(rdtscCode.Length + cpuidCode.Length);

            if (OperatingSystem.IsLinux)
            {
                // Unix
                var assembly =
                    Assembly.Load("Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756");

                var syscall = assembly.GetType("Mono.Unix.Native.Syscall");
                var mmap    = syscall.GetMethod("mmap");

                var mmapProts      = assembly.GetType("Mono.Unix.Native.MmapProts");
                var mmapProtsParam = Enum.ToObject(mmapProts,
                                                   (int)mmapProts.GetField("PROT_READ").GetValue(null) |
                                                   (int)mmapProts.GetField("PROT_WRITE").GetValue(null) |
                                                   (int)mmapProts.GetField("PROT_EXEC").GetValue(null));

                var mmapFlags      = assembly.GetType("Mono.Unix.Native.MmapFlags");
                var mmapFlagsParam = Enum.ToObject(mmapFlags,
                                                   (int)mmapFlags.GetField("MAP_ANONYMOUS").GetValue(null) |
                                                   (int)mmapFlags.GetField("MAP_PRIVATE").GetValue(null));

                codeBuffer = (IntPtr)mmap.Invoke(null, new[]
                {
                    IntPtr.Zero,
                    size, mmapProtsParam, mmapFlagsParam, -1, 0
                });
            }
            else
            {
                // Windows
                codeBuffer = NativeMethods.VirtualAlloc(IntPtr.Zero,
                                                        (UIntPtr)size, AllocationType.COMMIT | AllocationType.RESERVE,
                                                        MemoryProtection.EXECUTE_READWRITE);
            }

            Marshal.Copy(rdtscCode, 0, codeBuffer, rdtscCode.Length);

            Rdtsc = Marshal.GetDelegateForFunctionPointer(
                codeBuffer, typeof(RdtscDelegate)) as RdtscDelegate;

            var cpuidAddress = (IntPtr)((long)codeBuffer + rdtscCode.Length);

            Marshal.Copy(cpuidCode, 0, cpuidAddress, cpuidCode.Length);

            Cpuid = Marshal.GetDelegateForFunctionPointer(
                cpuidAddress, typeof(CpuidDelegate)) as CpuidDelegate;
        }
예제 #15
0
        public static void Open()
        {
            var p = (int)Environment.OSVersion.Platform;

            byte[] rdtscCode = IntPtr.Size == 4 ? RDTSC_32 : RDTSC_64;

            s_size = (ulong)(rdtscCode.Length);

            if ((p == 4) || (p == 128))
            {
                // Unix
                s_isArm = IsARMArchitecture();
                if (s_isArm)
                {
                    Rdtsc = RdtscOnArm;
                    return;
                }
                Assembly assembly = Assembly.Load("Mono.Posix");

                Type syscall = assembly.GetType("Mono.Unix.Native.Syscall");
                MethodInfo mmap = syscall.GetMethod("mmap");

                Type mmapProts = assembly.GetType("Mono.Unix.Native.MmapProts");
                object mmapProtsParam = Enum.ToObject(mmapProts,
                    (int)mmapProts.GetField("PROT_READ").GetValue(null) |
                    (int)mmapProts.GetField("PROT_WRITE").GetValue(null) |
                    (int)mmapProts.GetField("PROT_EXEC").GetValue(null));

                Type mmapFlags = assembly.GetType("Mono.Unix.Native.MmapFlags");
                object mmapFlagsParam = Enum.ToObject(mmapFlags,
                    (int)mmapFlags.GetField("MAP_ANONYMOUS").GetValue(null) |
                    (int)mmapFlags.GetField("MAP_PRIVATE").GetValue(null));

                s_codeBuffer = (IntPtr)mmap.Invoke(null,
                    new[] { IntPtr.Zero, s_size, mmapProtsParam, mmapFlagsParam, -1, 0 });

                if (s_codeBuffer == IntPtr.Zero || s_codeBuffer == (IntPtr)(-1))
                {
                    throw new InvalidOperationException("Mmap failed");
                }
            }
            else
            {
                // Windows
                s_codeBuffer = NativeMethods.VirtualAlloc(IntPtr.Zero,
                    (UIntPtr)s_size, AllocationType.COMMIT | AllocationType.RESERVE,
                    MemoryProtection.EXECUTE_READWRITE);
            }

            Marshal.Copy(rdtscCode, 0, s_codeBuffer, rdtscCode.Length);

            Rdtsc = Marshal.GetDelegateForFunctionPointer(
                s_codeBuffer, typeof(RdtscDelegate)) as RdtscDelegate;
        }
예제 #16
0
        internal static void Open()
        {
            if (IsOpen)
            {
                return;
            }

            IsOpen = true;

            byte[] rdtscCode;
            byte[] cpuidCode;
            if (IntPtr.Size == 4)
            {
                rdtscCode = RDTSC_32;
                cpuidCode = CPUID_32;
            }
            else
            {
                rdtscCode = RDTSC_64;
                cpuidCode = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
                            RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
                                        ? CPUID_64_LINUX
                                        : CPUID_64_WINDOWS;
            }

            size = (ulong)(rdtscCode.Length + cpuidCode.Length);

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                // Unix
                codeBuffer = NativeMethods.mmap(IntPtr.Zero, size,
                                                MmapProts.PROT_READ | MmapProts.PROT_WRITE | MmapProts.PROT_EXEC,
                                                MmapFlags.MAP_ANONYMOUS | MmapFlags.MAP_PRIVATE, -1, 0);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                // OSX
                // OSX is NOT POSIX compliant, in the way that MAP_ANON is a different value on OSX (0x1000) than Unix (0x20), which breaks just about every program making use of it
                // Good job Apple. Good job.
                codeBuffer = NativeMethods.mmap(IntPtr.Zero, size,
                                                MmapProts.PROT_READ | MmapProts.PROT_WRITE | MmapProts.PROT_EXEC,
                                                MmapFlags.MAP_ANON | MmapFlags.MAP_PRIVATE, -1, 0);
            }
            else
            {
                // Windows
                codeBuffer = NativeMethods.VirtualAlloc(IntPtr.Zero,
                                                        (UIntPtr)size, AllocationType.COMMIT | AllocationType.RESERVE,
                                                        MemoryProtection.EXECUTE_READWRITE);
            }

            Marshal.Copy(rdtscCode, 0, codeBuffer, rdtscCode.Length);

            Rdtsc = Marshal.GetDelegateForFunctionPointer(
                codeBuffer, typeof(RdtscDelegate)) as RdtscDelegate;

            var cpuidAddress = (IntPtr)((long)codeBuffer + rdtscCode.Length);

            Marshal.Copy(cpuidCode, 0, cpuidAddress, cpuidCode.Length);

            Cpuid = Marshal.GetDelegateForFunctionPointer(
                cpuidAddress, typeof(CpuidDelegate)) as CpuidDelegate;
        }