diff --git a/LibreHardwareMonitorLib/Hardware/Computer.cs b/LibreHardwareMonitorLib/Hardware/Computer.cs index d51e197..8c3ef79 100644 --- a/LibreHardwareMonitorLib/Hardware/Computer.cs +++ b/LibreHardwareMonitorLib/Hardware/Computer.cs @@ -487,6 +487,7 @@ public class Computer : IComputer _smbios = new SMBios(); Ring0.Open(); + Mutexes.Open(); OpCode.Open(); AddGroups(); @@ -628,6 +629,7 @@ public class Computer : IComputer OpCode.Close(); InpOut.Close(); Ring0.Close(); + Mutexes.Close(); _smbios = null; _open = false; diff --git a/LibreHardwareMonitorLib/Hardware/Cpu/Amd0FCpu.cs b/LibreHardwareMonitorLib/Hardware/Cpu/Amd0FCpu.cs index 59b6240..cbea3ba 100644 --- a/LibreHardwareMonitorLib/Hardware/Cpu/Amd0FCpu.cs +++ b/LibreHardwareMonitorLib/Hardware/Cpu/Amd0FCpu.cs @@ -94,7 +94,7 @@ internal sealed class Amd0FCpu : AmdCpu { base.Update(); - if (Ring0.WaitPciBusMutex(10)) + if (Mutexes.WaitPciBus(10)) { if (_miscellaneousControlAddress != Interop.Ring0.INVALID_PCI_ADDRESS) { @@ -117,7 +117,7 @@ internal sealed class Amd0FCpu : AmdCpu } } - Ring0.ReleasePciBusMutex(); + Mutexes.ReleasePciBus(); } if (HasTimeStampCounter) diff --git a/LibreHardwareMonitorLib/Hardware/Cpu/Amd10Cpu.cs b/LibreHardwareMonitorLib/Hardware/Cpu/Amd10Cpu.cs index 76474fd..0688835 100644 --- a/LibreHardwareMonitorLib/Hardware/Cpu/Amd10Cpu.cs +++ b/LibreHardwareMonitorLib/Hardware/Cpu/Amd10Cpu.cs @@ -451,19 +451,19 @@ internal sealed class Amd10Cpu : AmdCpu private static bool ReadSmuRegister(uint address, out uint value) { - if (Ring0.WaitPciBusMutex(10)) + if (Mutexes.WaitPciBus(10)) { if (!Ring0.WritePciConfig(0, 0xB8, address)) { value = 0; - Ring0.ReleasePciBusMutex(); + Mutexes.ReleasePciBus(); return false; } bool result = Ring0.ReadPciConfig(0, 0xBC, out value); - Ring0.ReleasePciBusMutex(); + Mutexes.ReleasePciBus(); return result; } diff --git a/LibreHardwareMonitorLib/Hardware/Cpu/Amd17Cpu.cs b/LibreHardwareMonitorLib/Hardware/Cpu/Amd17Cpu.cs index e5e72b0..5bf5dff 100644 --- a/LibreHardwareMonitorLib/Hardware/Cpu/Amd17Cpu.cs +++ b/LibreHardwareMonitorLib/Hardware/Cpu/Amd17Cpu.cs @@ -164,7 +164,7 @@ internal sealed class Amd17Cpu : AmdCpu uint smuSvi0TelPlane0 = 0; uint smuSvi0TelPlane1 = 0; - if (Ring0.WaitPciBusMutex(10)) + if (Mutexes.WaitPciBus(10)) { // THM_TCON_CUR_TMP // CUR_TEMP [31:21] @@ -340,7 +340,7 @@ internal sealed class Amd17Cpu : AmdCpu } } - Ring0.ReleasePciBusMutex(); + Mutexes.ReleasePciBus(); } // voltage diff --git a/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/EC/WindowsEmbeddedControllerIO.cs b/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/EC/WindowsEmbeddedControllerIO.cs index 4939f1c..62d46a3 100644 --- a/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/EC/WindowsEmbeddedControllerIO.cs +++ b/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/EC/WindowsEmbeddedControllerIO.cs @@ -30,7 +30,7 @@ public class WindowsEmbeddedControllerIO : IEmbeddedControllerIO public WindowsEmbeddedControllerIO() { - if (!Ring0.WaitEcMutex(10)) + if (!Mutexes.WaitEc(10)) { throw new BusMutexLockingFailedException(); } @@ -78,7 +78,7 @@ public class WindowsEmbeddedControllerIO : IEmbeddedControllerIO if (!_disposed) { _disposed = true; - Ring0.ReleaseEcMutex(); + Mutexes.ReleaseEc(); } } @@ -245,4 +245,4 @@ public class WindowsEmbeddedControllerIO : IEmbeddedControllerIO } #endregion -} \ No newline at end of file +} diff --git a/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/F718XX.cs b/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/F718XX.cs index 838e3f1..62ee32e 100644 --- a/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/F718XX.cs +++ b/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/F718XX.cs @@ -52,7 +52,7 @@ internal class F718XX : ISuperIO if (index < 0 || index >= Controls.Length) throw new ArgumentOutOfRangeException(nameof(index)); - if (!Ring0.WaitIsaBusMutex(10)) + if (!Mutexes.WaitIsaBus(10)) return; if (value.HasValue) @@ -66,7 +66,7 @@ internal class F718XX : ISuperIO RestoreDefaultFanPwmControl(index); } - Ring0.ReleaseIsaBusMutex(); + Mutexes.ReleaseIsaBus(); } public string GetReport() @@ -79,7 +79,7 @@ internal class F718XX : ISuperIO r.AppendLine(_address.ToString("X4", CultureInfo.InvariantCulture)); r.AppendLine(); - if (!Ring0.WaitIsaBusMutex(100)) + if (!Mutexes.WaitIsaBus(100)) return r.ToString(); r.AppendLine("Hardware Monitor Registers"); @@ -103,13 +103,13 @@ internal class F718XX : ISuperIO r.AppendLine(); - Ring0.ReleaseIsaBusMutex(); + Mutexes.ReleaseIsaBus(); return r.ToString(); } public void Update() { - if (!Ring0.WaitIsaBusMutex(10)) + if (!Mutexes.WaitIsaBus(10)) return; for (int i = 0; i < Voltages.Length; i++) @@ -201,7 +201,7 @@ internal class F718XX : ISuperIO } } - Ring0.ReleaseIsaBusMutex(); + Mutexes.ReleaseIsaBus(); } private void SaveDefaultFanPwmControl(int index) diff --git a/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/GigabyteController.cs b/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/GigabyteController.cs index 9a6e1dc..ab7e33c 100644 --- a/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/GigabyteController.cs +++ b/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/GigabyteController.cs @@ -55,7 +55,7 @@ internal class GigabyteController private bool AmdEnable(bool enabled) { - if (!Ring0.WaitPciBusMutex(10)) + if (!Mutexes.WaitPciBus(10)) return false; // see D14F3x https://www.amd.com/system/files/TechDocs/55072_AMD_Family_15h_Models_70h-7Fh_BKDG.pdf @@ -99,7 +99,7 @@ internal class GigabyteController Ring0.WritePciConfig(amdIsaBridgeAddress, romAddressRange2Register, originalRomAddressRegister); } - Ring0.ReleasePciBusMutex(); + Mutexes.ReleasePciBus(); return result; } diff --git a/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/IT87XX.cs b/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/IT87XX.cs index 6b72ae7..94ebc78 100644 --- a/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/IT87XX.cs +++ b/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/IT87XX.cs @@ -257,7 +257,7 @@ internal class IT87XX : ISuperIO if (index < 0 || index >= Controls.Length) throw new ArgumentOutOfRangeException(nameof(index)); - if (!Ring0.WaitIsaBusMutex(10)) + if (!Mutexes.WaitIsaBus(10)) return; if (value.HasValue) @@ -293,7 +293,7 @@ internal class IT87XX : ISuperIO RestoreDefaultFanPwmControl(index); } - Ring0.ReleaseIsaBusMutex(); + Mutexes.ReleaseIsaBus(); } public string GetReport() @@ -312,7 +312,7 @@ internal class IT87XX : ISuperIO r.AppendLine(_gpioAddress.ToString("X4", CultureInfo.InvariantCulture)); r.AppendLine(); - if (!Ring0.WaitIsaBusMutex(100)) + if (!Mutexes.WaitIsaBus(100)) return r.ToString(); // dump memory of all banks if supported by chip @@ -361,7 +361,7 @@ internal class IT87XX : ISuperIO r.AppendLine(); r.AppendLine(); - Ring0.ReleaseIsaBusMutex(); + Mutexes.ReleaseIsaBus(); return r.ToString(); } @@ -390,7 +390,7 @@ internal class IT87XX : ISuperIO public void Update() { - if (!Ring0.WaitIsaBusMutex(10)) + if (!Mutexes.WaitIsaBus(10)) return; for (int i = 0; i < Voltages.Length; i++) @@ -491,7 +491,7 @@ internal class IT87XX : ISuperIO } } - Ring0.ReleaseIsaBusMutex(); + Mutexes.ReleaseIsaBus(); } private byte ReadByte(byte register, out bool valid) diff --git a/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/LpcIO.cs b/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/LpcIO.cs index 4b38f7b..888b95c 100644 --- a/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/LpcIO.cs +++ b/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/LpcIO.cs @@ -20,12 +20,12 @@ internal class LpcIO public LpcIO(Motherboard motherboard) { - if (!Ring0.IsOpen || !Ring0.WaitIsaBusMutex(100)) + if (!Ring0.IsOpen || !Mutexes.WaitIsaBus(100)) return; Detect(motherboard); - Ring0.ReleaseIsaBusMutex(); + Mutexes.ReleaseIsaBus(); if (Ipmi.IsBmcPresent()) _superIOs.Add(new Ipmi(motherboard.Manufacturer)); diff --git a/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/Nct677X.cs b/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/Nct677X.cs index d1b9ef1..06fe953 100644 --- a/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/Nct677X.cs +++ b/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/Nct677X.cs @@ -362,7 +362,7 @@ internal class Nct677X : ISuperIO if (index < 0 || index >= Controls.Length) throw new ArgumentOutOfRangeException(nameof(index)); - if (!Ring0.WaitIsaBusMutex(10)) + if (!Mutexes.WaitIsaBus(10)) return; if (value.HasValue) @@ -407,7 +407,7 @@ internal class Nct677X : ISuperIO RestoreDefaultFanControl(index); } - Ring0.ReleaseIsaBusMutex(); + Mutexes.ReleaseIsaBus(); } public void Update() @@ -415,7 +415,7 @@ internal class Nct677X : ISuperIO if (!_isNuvotonVendor) return; - if (!Ring0.WaitIsaBusMutex(10)) + if (!Mutexes.WaitIsaBus(10)) return; DisableIOSpaceLock(); @@ -634,7 +634,7 @@ internal class Nct677X : ISuperIO } } - Ring0.ReleaseIsaBusMutex(); + Mutexes.ReleaseIsaBus(); } public string GetReport() @@ -651,7 +651,7 @@ internal class Nct677X : ISuperIO r.AppendLine(_port.ToString("X4", CultureInfo.InvariantCulture)); r.AppendLine(); - if (!Ring0.WaitIsaBusMutex(100)) + if (!Mutexes.WaitIsaBus(100)) return r.ToString(); ushort[] addresses = @@ -799,7 +799,7 @@ internal class Nct677X : ISuperIO r.AppendLine(); - Ring0.ReleaseIsaBusMutex(); + Mutexes.ReleaseIsaBus(); return r.ToString(); } diff --git a/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/W836XX.cs b/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/W836XX.cs index 808488f..07efe72 100644 --- a/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/W836XX.cs +++ b/LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/W836XX.cs @@ -192,7 +192,7 @@ internal class W836XX : ISuperIO if (index < 0 || index >= Controls.Length) throw new ArgumentOutOfRangeException(nameof(index)); - if (!Ring0.WaitIsaBusMutex(10)) + if (!Mutexes.WaitIsaBus(10)) return; if (value.HasValue) @@ -223,7 +223,7 @@ internal class W836XX : ISuperIO RestoreDefaultFanPwmControl(index); } - Ring0.ReleaseIsaBusMutex(); + Mutexes.ReleaseIsaBus(); } private void SaveDefaultFanPwmControl(int index) //added to save initial control values @@ -295,7 +295,7 @@ internal class W836XX : ISuperIO public void Update() { - if (!Ring0.WaitIsaBusMutex(10)) + if (!Mutexes.WaitIsaBus(10)) return; for (int i = 0; i < Voltages.Length; i++) @@ -397,7 +397,7 @@ internal class W836XX : ISuperIO Controls[i] = (float)Math.Round(value * 100.0f / 0xFF); } - Ring0.ReleaseIsaBusMutex(); + Mutexes.ReleaseIsaBus(); } public string GetReport() @@ -414,7 +414,7 @@ internal class W836XX : ISuperIO r.AppendLine(_address.ToString("X4", CultureInfo.InvariantCulture)); r.AppendLine(); - if (!Ring0.WaitIsaBusMutex(100)) + if (!Mutexes.WaitIsaBus(100)) return r.ToString(); r.AppendLine("Hardware Monitor Registers"); @@ -454,7 +454,7 @@ internal class W836XX : ISuperIO } r.AppendLine(); - Ring0.ReleaseIsaBusMutex(); + Mutexes.ReleaseIsaBus(); return r.ToString(); } diff --git a/LibreHardwareMonitorLib/Hardware/Mutexes.cs b/LibreHardwareMonitorLib/Hardware/Mutexes.cs new file mode 100644 index 0000000..7f77537 --- /dev/null +++ b/LibreHardwareMonitorLib/Hardware/Mutexes.cs @@ -0,0 +1,101 @@ +using System; +using System.Threading; + +namespace LibreHardwareMonitor.Hardware; + +internal static class Mutexes +{ + private static Mutex _ecMutex; + private static Mutex _isaBusMutex; + private static Mutex _pciBusMutex; + + /// + /// Opens the mutexes. + /// + public static void Open() + { + _isaBusMutex = CreateOrOpenExistingMutex("Global\\Access_ISABUS.HTP.Method"); + _pciBusMutex = CreateOrOpenExistingMutex("Global\\Access_PCI"); + _ecMutex = CreateOrOpenExistingMutex("Global\\Access_EC"); + + static Mutex CreateOrOpenExistingMutex(string name) + { + try + { + return new Mutex(false, name); + } + catch (UnauthorizedAccessException) + { + try + { + return Mutex.OpenExisting(name); + } + catch + { + // Ignored. + } + } + + return null; + } + } + + /// + /// Closes the mutexes. + /// + public static void Close() + { + _isaBusMutex?.Close(); + _pciBusMutex?.Close(); + _ecMutex?.Close(); + } + + public static bool WaitIsaBus(int millisecondsTimeout) + { + return WaitMutex(_isaBusMutex, millisecondsTimeout); + } + + public static void ReleaseIsaBus() + { + _isaBusMutex?.ReleaseMutex(); + } + + public static bool WaitPciBus(int millisecondsTimeout) + { + return WaitMutex(_pciBusMutex, millisecondsTimeout); + } + + public static void ReleasePciBus() + { + _pciBusMutex?.ReleaseMutex(); + } + + public static bool WaitEc(int millisecondsTimeout) + { + return WaitMutex(_ecMutex, millisecondsTimeout); + } + + public static void ReleaseEc() + { + _ecMutex?.ReleaseMutex(); + } + + private static bool WaitMutex(Mutex mutex, int millisecondsTimeout) + { + if (mutex == null) + return true; + + try + { + return mutex.WaitOne(millisecondsTimeout, false); + } + catch (AbandonedMutexException) + { + return true; + } + catch (InvalidOperationException) + { + return false; + } + } +} diff --git a/LibreHardwareMonitorLib/Hardware/Ring0.cs b/LibreHardwareMonitorLib/Hardware/Ring0.cs index 6a65e1e..27765c3 100644 --- a/LibreHardwareMonitorLib/Hardware/Ring0.cs +++ b/LibreHardwareMonitorLib/Hardware/Ring0.cs @@ -4,7 +4,6 @@ // Partial Copyright (C) Michael Möller and Contributors. // All Rights Reserved. -using System; using System.Diagnostics; using System.IO; using System.IO.Compression; @@ -18,10 +17,7 @@ namespace LibreHardwareMonitor.Hardware; internal static class Ring0 { private static KernelDriver _driver; - private static Mutex _ecMutex; private static string _filePath; - private static Mutex _isaBusMutex; - private static Mutex _pciBusMutex; private static readonly StringBuilder _report = new(); @@ -92,24 +88,6 @@ internal static class Ring0 if (!_driver.IsOpen) _driver = null; - - const string isaMutexName = "Global\\Access_ISABUS.HTP.Method"; - if (!TryCreateOrOpenExistingMutex(isaMutexName, out _isaBusMutex)) - { - // Mutex could not be created or opened. - } - - const string pciMutexName = "Global\\Access_PCI"; - if (!TryCreateOrOpenExistingMutex(pciMutexName, out _pciBusMutex)) - { - // Mutex could not be created or opened. - } - - const string ecMutexName = "Global\\Access_EC"; - if (!TryCreateOrOpenExistingMutex(ecMutexName, out _ecMutex)) - { - // Mutex could not be created or opened. - } } private static bool Extract(string filePath) @@ -171,29 +149,6 @@ internal static class Ring0 } } - private static bool TryCreateOrOpenExistingMutex(string name, out Mutex mutex) - { - try - { - mutex = new Mutex(false, name); - return true; - } - catch (UnauthorizedAccessException) - { - try - { - mutex = Mutex.OpenExisting(name); - return true; - } - catch - { - mutex = null; - } - } - - return false; - } - private static void Delete() { try @@ -338,24 +293,6 @@ internal static class Ring0 _driver = null; } - if (_isaBusMutex != null) - { - _isaBusMutex.Close(); - _isaBusMutex = null; - } - - if (_pciBusMutex != null) - { - _pciBusMutex.Close(); - _pciBusMutex = null; - } - - if (_ecMutex != null) - { - _ecMutex.Close(); - _ecMutex = null; - } - // try to delete temporary driver file again if failed during open Delete(); } @@ -375,78 +312,6 @@ internal static class Ring0 return null; } - public static bool WaitIsaBusMutex(int millisecondsTimeout) - { - if (_isaBusMutex == null) - return true; - - try - { - return _isaBusMutex.WaitOne(millisecondsTimeout, false); - } - catch (AbandonedMutexException) - { - return true; - } - catch (InvalidOperationException) - { - return false; - } - } - - public static void ReleaseIsaBusMutex() - { - _isaBusMutex?.ReleaseMutex(); - } - - public static bool WaitPciBusMutex(int millisecondsTimeout) - { - if (_pciBusMutex == null) - return true; - - try - { - return _pciBusMutex.WaitOne(millisecondsTimeout, false); - } - catch (AbandonedMutexException) - { - return true; - } - catch (InvalidOperationException) - { - return false; - } - } - - public static void ReleasePciBusMutex() - { - _pciBusMutex?.ReleaseMutex(); - } - - public static bool WaitEcMutex(int millisecondsTimeout) - { - if (_ecMutex == null) - return true; - - try - { - return _ecMutex.WaitOne(millisecondsTimeout, false); - } - catch (AbandonedMutexException) - { - return true; - } - catch (InvalidOperationException) - { - return false; - } - } - - public static void ReleaseEcMutex() - { - _ecMutex?.ReleaseMutex(); - } - public static bool ReadMsr(uint index, out uint eax, out uint edx) { if (_driver == null) diff --git a/LibreHardwareMonitorLib/Hardware/RyzenSMU.cs b/LibreHardwareMonitorLib/Hardware/RyzenSMU.cs index 66481d2..f076646 100644 --- a/LibreHardwareMonitorLib/Hardware/RyzenSMU.cs +++ b/LibreHardwareMonitorLib/Hardware/RyzenSMU.cs @@ -700,12 +700,12 @@ internal class RyzenSMU private static void WriteReg(uint addr, uint data) { - if (Ring0.WaitPciBusMutex(10)) + if (Mutexes.WaitPciBus(10)) { if (Ring0.WritePciConfig(0x00, SMU_PCI_ADDR_REG, addr)) Ring0.WritePciConfig(0x00, SMU_PCI_DATA_REG, data); - Ring0.ReleasePciBusMutex(); + Mutexes.ReleasePciBus(); } } @@ -713,12 +713,12 @@ internal class RyzenSMU { bool read = false; - if (Ring0.WaitPciBusMutex(10)) + if (Mutexes.WaitPciBus(10)) { if (Ring0.WritePciConfig(0x00, SMU_PCI_ADDR_REG, addr)) read = Ring0.ReadPciConfig(0x00, SMU_PCI_DATA_REG, out data); - Ring0.ReleasePciBusMutex(); + Mutexes.ReleasePciBus(); } return read;