[9102] | 1 | ///
|
---|
| 2 | /// This file is part of ILNumerics Community Edition.
|
---|
| 3 | ///
|
---|
| 4 | /// ILNumerics Community Edition - high performance computing for applications.
|
---|
| 5 | /// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
|
---|
| 6 | ///
|
---|
| 7 | /// ILNumerics Community Edition is free software: you can redistribute it and/or modify
|
---|
| 8 | /// it under the terms of the GNU General Public License version 3 as published by
|
---|
| 9 | /// the Free Software Foundation.
|
---|
| 10 | ///
|
---|
| 11 | /// ILNumerics Community Edition is distributed in the hope that it will be useful,
|
---|
| 12 | /// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | /// GNU General Public License for more details.
|
---|
| 15 | ///
|
---|
| 16 | /// You should have received a copy of the GNU General Public License
|
---|
| 17 | /// along with ILNumerics Community Edition. See the file License.txt in the root
|
---|
| 18 | /// of your distribution package. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | ///
|
---|
| 20 | /// In addition this software uses the following components and/or licenses:
|
---|
| 21 | ///
|
---|
| 22 | /// =================================================================================
|
---|
| 23 | /// The Open Toolkit Library License
|
---|
| 24 | ///
|
---|
| 25 | /// Copyright (c) 2006 - 2009 the Open Toolkit library.
|
---|
| 26 | ///
|
---|
| 27 | /// Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
| 28 | /// of this software and associated documentation files (the "Software"), to deal
|
---|
| 29 | /// in the Software without restriction, including without limitation the rights to
|
---|
| 30 | /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
---|
| 31 | /// the Software, and to permit persons to whom the Software is furnished to do
|
---|
| 32 | /// so, subject to the following conditions:
|
---|
| 33 | ///
|
---|
| 34 | /// The above copyright notice and this permission notice shall be included in all
|
---|
| 35 | /// copies or substantial portions of the Software.
|
---|
| 36 | ///
|
---|
| 37 | /// =================================================================================
|
---|
| 38 | ///
|
---|
| 39 |
|
---|
| 40 | using System;
|
---|
| 41 | using System.Diagnostics;
|
---|
| 42 | using System.Threading;
|
---|
| 43 | using System.Runtime.InteropServices;
|
---|
| 44 | using System.Reflection;
|
---|
| 45 | using ILNumerics;
|
---|
| 46 |
|
---|
| 47 | namespace ILNumerics.Misc {
|
---|
| 48 | /// <summary>
|
---|
| 49 | /// This class will support the runtime evaluation of the current processort. This is preliminary work and not used by the current ILNumerics release.
|
---|
| 50 | /// </summary>
|
---|
| 51 | public class ILCPUID {
|
---|
| 52 |
|
---|
| 53 | #region attributes
|
---|
| 54 | bool? m_supportsCPUID = null;
|
---|
| 55 | #endregion
|
---|
| 56 |
|
---|
| 57 | #region properties
|
---|
| 58 | public bool Supported {
|
---|
| 59 | get {
|
---|
| 60 | if (!m_supportsCPUID.HasValue) {
|
---|
| 61 | checkSupport();
|
---|
| 62 | }
|
---|
| 63 | return m_supportsCPUID.Value; }
|
---|
| 64 | }
|
---|
| 65 | #endregion
|
---|
| 66 | delegate uint cpuid_support_delegate();
|
---|
| 67 | static uint cpuid_support_dummy() {
|
---|
| 68 | return 0;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | //[UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.Cdecl)]
|
---|
| 72 | delegate void cpuid_delegate(int[] buffer);
|
---|
| 73 | static void cpuid_dummy(int[] buffer) {
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | static byte[] cpuid_asmprog = new byte[]{
|
---|
| 77 | 0x53, // push ebx
|
---|
| 78 | 0x56, // push esi
|
---|
| 79 | 0x8B, 0xF2, // mov esi,edx
|
---|
| 80 | 0x83, 0xC6, 0x08, // add esi,8
|
---|
| 81 | 0x8B, 0x06, // mov eax,dword ptr [esi]
|
---|
| 82 | 0x0F, 0xA2, // cpuid
|
---|
| 83 | 0x89, 0x06, // mov dword ptr [esi],eax
|
---|
| 84 | 0x89, 0x5E, 0x04, // mov dword ptr [esi+4],ebx
|
---|
| 85 | 0x89, 0x4E, 0x08, // mov dword ptr [esi+8],ecx
|
---|
| 86 | 0x89, 0x56, 0x0C, // mov dword ptr [esi+0Ch],edx
|
---|
| 87 | 0x5E, // pop esi
|
---|
| 88 | 0x5B, // pop ebx
|
---|
| 89 | 0xC3
|
---|
| 90 |
|
---|
| 91 | //0x55, // push ebp
|
---|
| 92 | //0x8B, 0xEC, // mov ebp,esp
|
---|
| 93 | //0x50, // push eax
|
---|
| 94 | //0x56, // push esi
|
---|
| 95 | //0x8B, 0x75, 0x08, // mov esi,dword ptr [ebp+8]
|
---|
| 96 | //0x8B, 0x06, // mov eax,dword ptr [esi]
|
---|
| 97 | //0x0F, 0xA2, // cpuid
|
---|
| 98 | //0x89, 0x06, // mov dword ptr [esi],eax
|
---|
| 99 | //0x89, 0x5E, 0x04, // mov dword ptr [esi+4],ebx
|
---|
| 100 | //0x89, 0x4E, 0x08, // mov dword ptr [esi+8],ecx
|
---|
| 101 | //0x89, 0x56, 0x0C, // mov dword ptr [esi+0Ch],edx
|
---|
| 102 | //0x5E, // pop esi
|
---|
| 103 | //0x58, // pop eax
|
---|
| 104 | //0x8B, 0xE5, // mov esp,ebp
|
---|
| 105 | //0x5D, // pop ebp
|
---|
| 106 | //0xC3
|
---|
| 107 | };
|
---|
| 108 |
|
---|
| 109 | static byte[] support_cpuid_asmprog = new byte[]{
|
---|
| 110 | 0x9C, // pushfd
|
---|
| 111 | 0x58, // pop eax
|
---|
| 112 | 0x8B, 0xC8, // mov ecx,eax
|
---|
| 113 | 0x35, 0x00, 0x00, 0x20, 0x00, // xor eax,200000h
|
---|
| 114 | 0x50, // push eax
|
---|
| 115 | 0x9D, // popfd
|
---|
| 116 | 0x9C, // pushfd
|
---|
| 117 | 0x58, // pop eax
|
---|
| 118 | 0x33, 0xC1, // xor eax,ecx
|
---|
| 119 | 0xC3
|
---|
| 120 | };
|
---|
| 121 |
|
---|
| 122 | [DllImport("kernel32.dll", SetLastError = true)]
|
---|
| 123 | static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
|
---|
| 124 | [DllImport("kernel32")]
|
---|
| 125 | private static extern bool VirtualFree(IntPtr lpAddress, UInt32 dwSize, UInt32 dwFreeType);
|
---|
| 126 |
|
---|
| 127 | [Flags()]
|
---|
| 128 | public enum AllocationType : uint {
|
---|
| 129 | COMMIT = 0x1000,
|
---|
| 130 | RESERVE = 0x2000,
|
---|
| 131 | RESET = 0x80000,
|
---|
| 132 | LARGE_PAGES = 0x20000000,
|
---|
| 133 | PHYSICAL = 0x400000,
|
---|
| 134 | TOP_DOWN = 0x100000,
|
---|
| 135 | WRITE_WATCH = 0x200000
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | [Flags()]
|
---|
| 139 | public enum MemoryProtection : uint {
|
---|
| 140 | EXECUTE = 0x10,
|
---|
| 141 | EXECUTE_READ = 0x20,
|
---|
| 142 | EXECUTE_READWRITE = 0x40,
|
---|
| 143 | EXECUTE_WRITECOPY = 0x80,
|
---|
| 144 | NOACCESS = 0x01,
|
---|
| 145 | READONLY = 0x02,
|
---|
| 146 | READWRITE = 0x04,
|
---|
| 147 | WRITECOPY = 0x08,
|
---|
| 148 | GUARD_Modifierflag = 0x100,
|
---|
| 149 | NOCACHE_Modifierflag = 0x200,
|
---|
| 150 | WRITECOMBINE_Modifierflag = 0x400
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | unsafe public ILCPUID() {
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | unsafe public void Query(int[] buffer) {
|
---|
| 157 | // the actual cpuid request
|
---|
| 158 | if (buffer == null || buffer.Length < 4)
|
---|
| 159 | throw new Exception("argument 'buffer' must have length >= 4");
|
---|
| 160 | IntPtr instructions = default(IntPtr);
|
---|
| 161 | try {
|
---|
| 162 | instructions = VirtualAlloc(IntPtr.Zero, new UIntPtr((uint)cpuid_asmprog.Length), AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE);
|
---|
| 163 | Marshal.Copy(cpuid_asmprog,0,instructions,cpuid_asmprog.Length);
|
---|
| 164 |
|
---|
| 165 | cpuid_delegate cpuid = new cpuid_delegate(cpuid_dummy);
|
---|
| 166 | Type funcType = typeof(Delegate);
|
---|
| 167 | FieldInfo funcPointer = funcType.GetField("_methodPtr", BindingFlags.NonPublic | BindingFlags.Instance);
|
---|
| 168 | funcPointer.SetValue(cpuid,instructions);
|
---|
| 169 | //fixed (int* buffer_fixed = buffer) {
|
---|
| 170 | cpuid(buffer);
|
---|
| 171 | //}
|
---|
| 172 |
|
---|
| 173 | } finally {
|
---|
| 174 | if (instructions != IntPtr.Zero) {
|
---|
| 175 | VirtualFree(instructions, 0, 0x8000); // MEM_RELEASE
|
---|
| 176 | instructions = IntPtr.Zero;
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | unsafe private IntPtr checkSupport() {
|
---|
| 182 | IntPtr instructions = IntPtr.Zero;
|
---|
| 183 | try {
|
---|
| 184 | //'write' the asm - must be in an instruction section! (data area would not work)
|
---|
| 185 |
|
---|
| 186 | instructions = VirtualAlloc(IntPtr.Zero, new UIntPtr((ulong)support_cpuid_asmprog.Length), AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE);
|
---|
| 187 | Marshal.Copy(support_cpuid_asmprog,0,instructions,support_cpuid_asmprog.Length);
|
---|
| 188 |
|
---|
| 189 | Type funcType = typeof(Delegate);
|
---|
| 190 | FieldInfo funcPointer = funcType.GetField("_methodPtr", BindingFlags.NonPublic | BindingFlags.Instance);
|
---|
| 191 |
|
---|
| 192 | // setup test for cpuid support
|
---|
| 193 | cpuid_support_delegate natcpuid_support = new cpuid_support_delegate(cpuid_support_dummy);
|
---|
| 194 | funcPointer.SetValue(natcpuid_support, instructions);
|
---|
| 195 | if (natcpuid_support() == 0) {
|
---|
| 196 | Console.WriteLine("No support for cpuid!");
|
---|
| 197 | m_supportsCPUID = false;
|
---|
| 198 | } else {
|
---|
| 199 | m_supportsCPUID = true;
|
---|
| 200 | }
|
---|
| 201 | } finally {
|
---|
| 202 | if (instructions != IntPtr.Zero) {
|
---|
| 203 | VirtualFree(instructions, 0, 0x8000); // MEM_RELEASE
|
---|
| 204 | instructions = IntPtr.Zero;
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | return instructions;
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | }
|
---|