Hardware/Opcode.cs
author moel.mich
Wed, 03 Nov 2010 22:07:46 +0000
changeset 238 bddc6e01840a
parent 236 763675f19ff4
child 239 91800d0f54de
permissions -rw-r--r--
Added CPUID support for Linux.
moel@236
     1
/*
moel@236
     2
  
moel@236
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@236
     4
moel@236
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@236
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@236
     7
  the License. You may obtain a copy of the License at
moel@236
     8
 
moel@236
     9
  http://www.mozilla.org/MPL/
moel@236
    10
moel@236
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@236
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@236
    13
  for the specific language governing rights and limitations under the License.
moel@236
    14
moel@236
    15
  The Original Code is the Open Hardware Monitor code.
moel@236
    16
moel@236
    17
  The Initial Developer of the Original Code is 
moel@236
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@236
    19
  Portions created by the Initial Developer are Copyright (C) 2010
moel@236
    20
  the Initial Developer. All Rights Reserved.
moel@236
    21
moel@236
    22
  Contributor(s):
moel@236
    23
moel@236
    24
  Alternatively, the contents of this file may be used under the terms of
moel@236
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@236
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@236
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@236
    28
  of those above. If you wish to allow use of your version of this file only
moel@236
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@236
    30
  use your version of this file under the terms of the MPL, indicate your
moel@236
    31
  decision by deleting the provisions above and replace them with the notice
moel@236
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@236
    33
  the provisions above, a recipient may use your version of this file under
moel@236
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@236
    35
 
moel@236
    36
*/
moel@236
    37
moel@236
    38
using System;
moel@236
    39
using System.Runtime.InteropServices;
moel@238
    40
using Mono.Unix.Native;
moel@236
    41
moel@236
    42
namespace OpenHardwareMonitor.Hardware {
moel@236
    43
  internal static class Opcode {
moel@238
    44
    
moel@236
    45
    private static IntPtr codeBuffer;
moel@238
    46
    private static ulong size;
moel@236
    47
moel@238
    48
    public static void Open() {  
moel@236
    49
      int p = (int)Environment.OSVersion.Platform;
moel@238
    50
            
moel@236
    51
      byte[] rdtscCode;
moel@236
    52
      byte[] cpuidCode;
moel@236
    53
      if (IntPtr.Size == 4) {
moel@236
    54
        rdtscCode = RDTSC_32;
moel@236
    55
        cpuidCode = CPUID_32;
moel@236
    56
      } else {
moel@236
    57
        rdtscCode = RDTSC_64;
moel@238
    58
        
moel@238
    59
        if ((p == 4) || (p == 128)) { // Unix
moel@238
    60
          cpuidCode = CPUID_64_LINUX;
moel@238
    61
        } else { // Windows
moel@238
    62
          cpuidCode = CPUID_64_WINDOWS;
moel@238
    63
        }
moel@236
    64
      }
moel@238
    65
      
moel@238
    66
      size = (ulong)(rdtscCode.Length + cpuidCode.Length);
moel@238
    67
      
moel@238
    68
      if ((p == 4) || (p == 128)) { // Unix
moel@238
    69
        codeBuffer = Syscall.mmap(IntPtr.Zero, size, 
moel@238
    70
          MmapProts.PROT_READ | MmapProts.PROT_WRITE | MmapProts.PROT_EXEC, 
moel@238
    71
          MmapFlags.MAP_ANONYMOUS | MmapFlags.MAP_PRIVATE, -1, 0);
moel@238
    72
      } else { // Windows
moel@238
    73
        codeBuffer = NativeMethods.VirtualAlloc(IntPtr.Zero,
moel@238
    74
          (UIntPtr)size, AllocationType.COMMIT | AllocationType.RESERVE, 
moel@238
    75
          MemoryProtection.EXECUTE_READWRITE);
moel@238
    76
      }
moel@236
    77
moel@236
    78
      Marshal.Copy(rdtscCode, 0, codeBuffer, rdtscCode.Length);
moel@236
    79
moel@236
    80
      Rdtsc = Marshal.GetDelegateForFunctionPointer(
moel@236
    81
        codeBuffer, typeof(RdtscDelegate)) as RdtscDelegate;
moel@236
    82
moel@236
    83
      IntPtr cpuidAddress = (IntPtr)((long)codeBuffer + rdtscCode.Length);
moel@236
    84
      Marshal.Copy(cpuidCode, 0, cpuidAddress, cpuidCode.Length);
moel@236
    85
moel@236
    86
      Cpuid = Marshal.GetDelegateForFunctionPointer(
moel@238
    87
        cpuidAddress, typeof(CpuidDelegate)) as CpuidDelegate;         
moel@236
    88
    }
moel@236
    89
moel@236
    90
    public static void Close() {
moel@236
    91
      Rdtsc = null;
moel@236
    92
      Cpuid = null;
moel@238
    93
      
moel@238
    94
      int p = (int)Environment.OSVersion.Platform;
moel@238
    95
      if ((p == 4) || (p == 128)) { // Unix
moel@238
    96
        Syscall.munmap(codeBuffer, size);
moel@238
    97
      } else { // Windows
moel@238
    98
        NativeMethods.VirtualFree(codeBuffer, UIntPtr.Zero, 
moel@238
    99
          FreeType.RELEASE);        
moel@238
   100
      }
moel@236
   101
    }
moel@236
   102
moel@236
   103
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
moel@236
   104
    public delegate ulong RdtscDelegate();
moel@236
   105
moel@236
   106
    public static RdtscDelegate Rdtsc;
moel@236
   107
moel@236
   108
    // unsigned __int64 __stdcall rdtsc() {
moel@236
   109
    //   return __rdtsc();
moel@236
   110
    // }
moel@236
   111
moel@236
   112
    private static readonly byte[] RDTSC_32 = new byte[] {
moel@236
   113
      0x0F, 0x31,                     // rdtsc   
moel@236
   114
      0xC3                            // ret  
moel@236
   115
    };
moel@236
   116
moel@236
   117
    private static readonly byte[] RDTSC_64 = new byte[] {
moel@236
   118
      0x0F, 0x31,                     // rdtsc  
moel@238
   119
      0x48, 0xC1, 0xE2, 0x20,         // shl rdx, 20h  
moel@238
   120
      0x48, 0x0B, 0xC2,               // or rax, rdx  
moel@236
   121
      0xC3                            // ret  
moel@236
   122
    };
moel@236
   123
    
moel@236
   124
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
moel@236
   125
    public delegate bool CpuidDelegate(uint index, uint ecxValue,
moel@236
   126
      out uint eax, out uint ebx, out uint ecx, out uint edx);
moel@236
   127
moel@236
   128
    public static CpuidDelegate Cpuid;
moel@236
   129
moel@236
   130
moel@236
   131
    // void __stdcall cpuidex(unsigned int index, unsigned int ecxValue, 
moel@236
   132
    //   unsigned int* eax, unsigned int* ebx, unsigned int* ecx, 
moel@236
   133
    //   unsigned int* edx)
moel@236
   134
    // {
moel@236
   135
    //   int info[4];	
moel@236
   136
    //   __cpuidex(info, index, ecxValue);
moel@236
   137
    //   *eax = info[0];
moel@236
   138
    //   *ebx = info[1];
moel@236
   139
    //   *ecx = info[2];
moel@236
   140
    //   *edx = info[3];
moel@236
   141
    // }
moel@236
   142
moel@236
   143
    private static readonly byte[] CPUID_32 = new byte[] {
moel@236
   144
      0x55,                           // push ebp  
moel@238
   145
      0x8B, 0xEC,                     // mov ebp, esp  
moel@238
   146
      0x83, 0xEC, 0x10,               // sub esp, 10h  
moel@238
   147
      0x8B, 0x45, 0x08,               // mov eax, dword ptr [ebp+8]  
moel@238
   148
      0x8B, 0x4D, 0x0C,               // mov ecx, dword ptr [ebp+0Ch]  
moel@236
   149
      0x53,                           // push ebx  
moel@236
   150
      0x0F, 0xA2,                     // cpuid  
moel@236
   151
      0x56,                           // push esi  
moel@238
   152
      0x8D, 0x75, 0xF0,               // lea esi, [info]  
moel@238
   153
      0x89, 0x06,                     // mov dword ptr [esi],eax  
moel@238
   154
      0x8B, 0x45, 0x10,               // mov eax, dword ptr [eax]  
moel@238
   155
      0x89, 0x5E, 0x04,               // mov dword ptr [esi+4], ebx  
moel@238
   156
      0x89, 0x4E, 0x08,               // mov dword ptr [esi+8], ecx  
moel@238
   157
      0x89, 0x56, 0x0C,               // mov dword ptr [esi+0Ch], edx  
moel@238
   158
      0x8B, 0x4D, 0xF0,               // mov ecx, dword ptr [info]  
moel@238
   159
      0x89, 0x08,                     // mov dword ptr [eax], ecx  
moel@238
   160
      0x8B, 0x45, 0x14,               // mov eax, dword ptr [ebx]  
moel@238
   161
      0x8B, 0x4D, 0xF4,               // mov ecx, dword ptr [ebp-0Ch]  
moel@238
   162
      0x89, 0x08,                     // mov dword ptr [eax], ecx  
moel@238
   163
      0x8B, 0x45, 0x18,               // mov eax, dword ptr [ecx]  
moel@238
   164
      0x8B, 0x4D, 0xF8,               // mov ecx, dword ptr [ebp-8]  
moel@238
   165
      0x89, 0x08,                     // mov dword ptr [eax], ecx  
moel@238
   166
      0x8B, 0x45, 0x1C,               // mov eax, dword ptr [edx]  
moel@238
   167
      0x8B, 0x4D, 0xFC,               // mov ecx, dword ptr [ebp-4]  
moel@236
   168
      0x5E,                           // pop esi  
moel@238
   169
      0x89, 0x08,                     // mov dword ptr [eax], ecx  
moel@236
   170
      0x5B,                           // pop ebx  
moel@236
   171
      0xC9,                           // leave  
moel@236
   172
      0xC2, 0x18, 0x00                // ret 18h  
moel@236
   173
    };
moel@236
   174
             
moel@238
   175
    private static readonly byte[] CPUID_64_WINDOWS = new byte[] {
moel@238
   176
      0x48, 0x89, 0x5C, 0x24, 0x08,   // mov qword ptr [rsp+8], rbx  
moel@238
   177
      0x8B, 0xC1,                     // mov eax, ecx  
moel@238
   178
      0x8B, 0xCA,                     // mov ecx, edx        
moel@238
   179
      0x0F, 0xA2,                     // cpuid        
moel@238
   180
      0x41, 0x89, 0x00,               // mov dword ptr [r8], eax        
moel@238
   181
      0x48, 0x8B, 0x44, 0x24, 0x28,   // mov rax, qword ptr [rsp+28h]       
moel@238
   182
      0x41, 0x89, 0x19,               // mov dword ptr [r9], ebx        
moel@238
   183
      0x48, 0x8B, 0x5C, 0x24, 0x08,   // mov rbx, qword ptr [rsp+8]      
moel@238
   184
      0x89, 0x08,                     // mov dword ptr [rax], ecx        
moel@238
   185
      0x48, 0x8B, 0x44, 0x24, 0x30,   // mov rax, qword ptr [rsp+30h]  
moel@238
   186
      0x89, 0x10,                     // mov dword ptr [rax], edx  
moel@236
   187
      0xC3                            // ret  
moel@236
   188
    };
moel@238
   189
    
moel@238
   190
    private static readonly byte[] CPUID_64_LINUX = new byte[] {
moel@238
   191
      0x49, 0x89, 0xD2,               // mov r10, rdx
moel@238
   192
      0x49, 0x89, 0xCB,               // mov r11, rcx
moel@238
   193
      0x53,                           // push rbx
moel@238
   194
      0x89, 0xF8,                     // mov eax, edi
moel@238
   195
      0x89, 0xF1,                     // mov ecx, esi
moel@238
   196
      0x0F, 0xA2,                     // cpuid
moel@238
   197
      0x41, 0x89, 0x02,               // mov dword ptr [r10], eax
moel@238
   198
      0x41, 0x89, 0x1B,               // mov dword ptr [r11], ebx
moel@238
   199
      0x41, 0x89, 0x08,               // mov dword ptr [r8], ecx
moel@238
   200
      0x41, 0x89, 0x11,               // mov dword ptr [r9], edx
moel@238
   201
      0x5B,                           // pop rbx
moel@238
   202
      0xC3,                           // ret
moel@238
   203
    };
moel@236
   204
moel@236
   205
    public static bool CpuidTx(uint index, uint ecxValue, 
moel@236
   206
      out uint eax, out uint ebx, out uint ecx, out uint edx, 
moel@238
   207
      ulong threadAffinityMask) {
moel@238
   208
      
moel@238
   209
      ulong mask = ThreadAffinity.Set(threadAffinityMask);
moel@236
   210
moel@238
   211
      if (mask == 0) {
moel@236
   212
        eax = ebx = ecx = edx = 0;
moel@236
   213
        return false;
moel@238
   214
      } 
moel@236
   215
moel@236
   216
      Cpuid(index, ecxValue, out eax, out ebx, out ecx, out edx);
moel@236
   217
moel@238
   218
      ThreadAffinity.Set(mask);      
moel@236
   219
      return true;
moel@236
   220
    }
moel@238
   221
    
moel@236
   222
    [Flags()]
moel@236
   223
    public enum AllocationType : uint {
moel@236
   224
      COMMIT = 0x1000,
moel@236
   225
      RESERVE = 0x2000,
moel@236
   226
      RESET = 0x80000,
moel@236
   227
      LARGE_PAGES = 0x20000000,
moel@236
   228
      PHYSICAL = 0x400000,
moel@236
   229
      TOP_DOWN = 0x100000,
moel@236
   230
      WRITE_WATCH = 0x200000
moel@236
   231
    }
moel@236
   232
moel@236
   233
    [Flags()]
moel@236
   234
    public enum MemoryProtection : uint {
moel@236
   235
      EXECUTE = 0x10,
moel@236
   236
      EXECUTE_READ = 0x20,
moel@236
   237
      EXECUTE_READWRITE = 0x40,
moel@236
   238
      EXECUTE_WRITECOPY = 0x80,
moel@236
   239
      NOACCESS = 0x01,
moel@236
   240
      READONLY = 0x02,
moel@236
   241
      READWRITE = 0x04,
moel@236
   242
      WRITECOPY = 0x08,
moel@236
   243
      GUARD = 0x100,
moel@236
   244
      NOCACHE = 0x200,
moel@236
   245
      WRITECOMBINE = 0x400
moel@236
   246
    }
moel@236
   247
moel@236
   248
    [Flags]
moel@236
   249
    enum FreeType {
moel@236
   250
      DECOMMIT = 0x4000,
moel@236
   251
      RELEASE = 0x8000
moel@236
   252
    }
moel@236
   253
moel@238
   254
    private static class NativeMethods {      
moel@236
   255
      private const string KERNEL = "kernel32.dll";
moel@236
   256
moel@236
   257
      [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
moel@236
   258
      public static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize,
moel@236
   259
        AllocationType flAllocationType, MemoryProtection flProtect);
moel@236
   260
moel@236
   261
      [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)]
moel@236
   262
      public static extern bool VirtualFree(IntPtr lpAddress, UIntPtr dwSize,
moel@238
   263
        FreeType dwFreeType);                 
moel@236
   264
    }
moel@236
   265
  }
moel@236
   266
}