]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libphobos: Fix regression d21 loops in getCpuInfo0B in Solaris/x86 kernel zone
authorIain Buclaw <ibuclaw@gdcproject.org>
Tue, 7 Nov 2023 13:04:07 +0000 (14:04 +0100)
committerIain Buclaw <ibuclaw@gdcproject.org>
Tue, 7 Nov 2023 13:26:09 +0000 (14:26 +0100)
This function assumes that cpuid would return "invalid domain" when a
sub-leaf index greater than what's supported is requested.  This turned
out not to always be the case when running on some virtual machines.

As the loop only does anything for levels 0 and 1, make that a hard
limit for number of times the loop is ran.

    PR d/112408

libphobos/ChangeLog:

* libdruntime/core/cpuid.d (getCpuInfo0B): Limit number of times loop
runs.

(cherry picked from commit 0b25c1295d4e84af681f4b1f4af2ad37cd270da3)

libphobos/libdruntime/core/cpuid.d

index e31f776d7eec673d351c0df743791fe909f23b82..b05db248b816ca09208b82c6f49405d6db4d90e6 100644 (file)
@@ -651,10 +651,12 @@ void getAMDcacheinfo()
 // to determine number of processors.
 void getCpuInfo0B()
 {
-    int level=0;
     int threadsPerCore;
     uint a, b, c, d;
-    do {
+    // I'm not sure about this. The docs state that there
+    // are 2 hyperthreads per core if HT is factory enabled.
+    for (int level = 0; level < 2; level++)
+    {
         version (GNU_OR_LDC) asm pure nothrow @nogc {
             "cpuid" : "=a" (a), "=b" (b), "=c" (c), "=d" (d) : "a" (0x0B), "c" (level);
         } else asm pure nothrow @nogc {
@@ -666,19 +668,20 @@ void getCpuInfo0B()
             mov c, ECX;
             mov d, EDX;
         }
-        if (b!=0) {
-           // I'm not sure about this. The docs state that there
-           // are 2 hyperthreads per core if HT is factory enabled.
-            if (level==0)
+        if (b != 0)
+        {
+            if (level == 0)
                 threadsPerCore = b & 0xFFFF;
-            else if (level==1) {
+            else if (level == 1)
+            {
                 cpuFeatures.maxThreads = b & 0xFFFF;
                 cpuFeatures.maxCores = cpuFeatures.maxThreads / threadsPerCore;
             }
-
         }
-        ++level;
-    } while (a!=0 || b!=0);
+        // Got "invalid domain" returned from cpuid
+        if (a == 0 && b == 0)
+            break;
+    }
 }
 
 void cpuidX86()