|
| 1 | +using IPR.Drivers.Hardware; |
| 2 | + |
| 3 | +namespace IPR.Drivers.Ring0 |
| 4 | +{ |
| 5 | + internal class LinuxRing0 : IRing0 |
| 6 | + { |
| 7 | + public bool IsOpen { get; private set; } |
| 8 | + |
| 9 | + public LinuxRing0() |
| 10 | + { |
| 11 | + Open(); |
| 12 | + } |
| 13 | + |
| 14 | + public bool Open() |
| 15 | + { |
| 16 | + IsOpen = CheckVersion(); |
| 17 | + return IsOpen; |
| 18 | + } |
| 19 | + |
| 20 | + private bool CheckVersion() |
| 21 | + { |
| 22 | + try |
| 23 | + { |
| 24 | + string rdResult = ""; |
| 25 | + using (System.Diagnostics.Process proc = new System.Diagnostics.Process()) |
| 26 | + { |
| 27 | + proc.StartInfo.FileName = "rdmsr"; |
| 28 | + proc.StartInfo.Arguments = "--version "; |
| 29 | + proc.StartInfo.UseShellExecute = false; |
| 30 | + proc.StartInfo.RedirectStandardOutput = true; |
| 31 | + proc.StartInfo.RedirectStandardError = true; |
| 32 | + proc.Start(); |
| 33 | + |
| 34 | + rdResult += proc.StandardOutput.ReadToEnd(); |
| 35 | + rdResult += proc.StandardError.ReadToEnd(); |
| 36 | + |
| 37 | + proc.WaitForExit(); |
| 38 | + } |
| 39 | + |
| 40 | + string wrResult = ""; |
| 41 | + using (System.Diagnostics.Process proc = new System.Diagnostics.Process()) |
| 42 | + { |
| 43 | + proc.StartInfo.FileName = "wrmsr"; |
| 44 | + proc.StartInfo.Arguments = "--version "; |
| 45 | + proc.StartInfo.UseShellExecute = false; |
| 46 | + proc.StartInfo.RedirectStandardOutput = true; |
| 47 | + proc.StartInfo.RedirectStandardError = true; |
| 48 | + proc.Start(); |
| 49 | + |
| 50 | + wrResult += proc.StandardOutput.ReadToEnd(); |
| 51 | + wrResult += proc.StandardError.ReadToEnd(); |
| 52 | + |
| 53 | + proc.WaitForExit(); |
| 54 | + } |
| 55 | + |
| 56 | + return !string.IsNullOrEmpty(rdResult) && !string.IsNullOrEmpty(wrResult) |
| 57 | + && rdResult.Contains("version msr-tools") && |
| 58 | + wrResult.Contains("version msr-tools"); |
| 59 | + } |
| 60 | + catch (Exception e) |
| 61 | + { |
| 62 | + return false; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + public bool ReadMsr(uint index, out uint eax, out uint edx) |
| 69 | + { |
| 70 | + eax = 0; |
| 71 | + edx = 0; |
| 72 | + if (!IsOpen) |
| 73 | + { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + string rawresuld = ""; |
| 78 | + using (System.Diagnostics.Process proc = new System.Diagnostics.Process()) |
| 79 | + { |
| 80 | + |
| 81 | + proc.StartInfo.FileName = "rdmsr"; |
| 82 | + proc.StartInfo.Arguments = "0x" + index.ToString("X"); |
| 83 | + proc.StartInfo.UseShellExecute = false; |
| 84 | + proc.StartInfo.RedirectStandardOutput = true; |
| 85 | + proc.StartInfo.RedirectStandardError = true; |
| 86 | + proc.Start(); |
| 87 | + |
| 88 | + rawresuld += proc.StandardOutput.ReadToEnd().Replace(Environment.NewLine, ""); |
| 89 | + rawresuld += proc.StandardError.ReadToEnd(); |
| 90 | + |
| 91 | + proc.WaitForExit(); |
| 92 | + } |
| 93 | + |
| 94 | + try |
| 95 | + { |
| 96 | + ulong result = Convert.ToUInt64(rawresuld, 16); |
| 97 | + edx = (uint)((result >> 32) & 0xFFFFFFFF); |
| 98 | + eax = (uint)(result & 0xFFFFFFFF); |
| 99 | + return true; |
| 100 | + } |
| 101 | + catch |
| 102 | + { |
| 103 | + return false; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public bool ReadMsr(uint index, out uint eax, out uint edx, GroupAffinity affinity) |
| 108 | + { |
| 109 | + GroupAffinity previousAffinity = ThreadAffinity.Set(affinity); |
| 110 | + bool result = ReadMsr(index, out eax, out edx); |
| 111 | + ThreadAffinity.Set(previousAffinity); |
| 112 | + return result; |
| 113 | + } |
| 114 | + |
| 115 | + public bool WriteMsr(uint index, uint eax, uint edx) |
| 116 | + { |
| 117 | + if (!IsOpen) |
| 118 | + { |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + try |
| 123 | + { |
| 124 | + string rawresuld = ""; |
| 125 | + using (System.Diagnostics.Process proc = new System.Diagnostics.Process()) |
| 126 | + { |
| 127 | + proc.StartInfo.FileName = "wrmsr"; |
| 128 | + proc.StartInfo.Arguments = "0x" + index.ToString("X") + " " + "0x" + edx.ToString("x8") + |
| 129 | + eax.ToString("x8"); |
| 130 | + proc.StartInfo.UseShellExecute = false; |
| 131 | + proc.StartInfo.RedirectStandardOutput = true; |
| 132 | + proc.StartInfo.RedirectStandardError = true; |
| 133 | + proc.Start(); |
| 134 | + |
| 135 | + |
| 136 | + rawresuld += proc.StandardOutput.ReadToEnd().Replace(Environment.NewLine, ""); |
| 137 | + rawresuld += proc.StandardError.ReadToEnd(); |
| 138 | + |
| 139 | + proc.WaitForExit(); |
| 140 | + } |
| 141 | + |
| 142 | + return string.IsNullOrEmpty(rawresuld); |
| 143 | + } |
| 144 | + catch |
| 145 | + { |
| 146 | + return false; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + public byte ReadIoPort(uint port) |
| 151 | + { |
| 152 | + throw new NotImplementedException(); |
| 153 | + } |
| 154 | + |
| 155 | + public void WriteIoPort(uint port, byte value) |
| 156 | + { |
| 157 | + throw new NotImplementedException(); |
| 158 | + } |
| 159 | + |
| 160 | + public uint GetPciAddress(byte bus, byte device, byte function) |
| 161 | + { |
| 162 | + throw new NotImplementedException(); |
| 163 | + } |
| 164 | + |
| 165 | + public bool ReadPciConfig(uint pciAddress, uint regAddress, out uint value) |
| 166 | + { |
| 167 | + throw new NotImplementedException(); |
| 168 | + } |
| 169 | + |
| 170 | + public bool WritePciConfig(uint pciAddress, uint regAddress, uint value) |
| 171 | + { |
| 172 | + throw new NotImplementedException(); |
| 173 | + } |
| 174 | + |
| 175 | + public bool WaitPciBusMutex(int millisecondsTimeout) |
| 176 | + { |
| 177 | + throw new NotImplementedException(); |
| 178 | + } |
| 179 | + |
| 180 | + public void ReleasePciBusMutex() |
| 181 | + { |
| 182 | + throw new NotImplementedException(); |
| 183 | + } |
| 184 | + |
| 185 | + public bool WaitEcMutex(int millisecondsTimeout) |
| 186 | + { |
| 187 | + throw new NotImplementedException(); |
| 188 | + } |
| 189 | + |
| 190 | + public void ReleaseEcMutex() |
| 191 | + { |
| 192 | + throw new NotImplementedException(); |
| 193 | + } |
| 194 | + |
| 195 | + public bool WaitIsaBusMutex(int millisecondsTimeout) |
| 196 | + { |
| 197 | + throw new NotImplementedException(); |
| 198 | + } |
| 199 | + |
| 200 | + public void ReleaseIsaBusMutex() |
| 201 | + { |
| 202 | + throw new NotImplementedException(); |
| 203 | + } |
| 204 | + |
| 205 | + public void Close() |
| 206 | + { |
| 207 | + throw new NotImplementedException(); |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments