+
+ return eax;
+}
+
+static void
+cpu_sig (void)
+{
+ char sig[13];
+ const uint32_t base = 0x40000000;
+ uint32_t leaf;
+
+ /* Most hypervisors only have information in leaf 0x40000000.
+ *
+ * Some hypervisors have "Viridian [HyperV] extensions", and those
+ * must appear in slot 0x40000000, but they will also have the true
+ * hypervisor in a higher slot.
+ *
+ * CPUID is supposed to return the maximum leaf offset in %eax, but
+ * this is not reliable. Instead we check the returned signatures
+ * against a known list (the others will be empty or garbage) and
+ * only print the ones we know about. This is OK because if we add
+ * a new test in virt-what we can update the list.
+ *
+ * By searching backwards we only print the highest entry, thus
+ * ignoring Viridian for Xen (and Nutanix). If we ever encounter a
+ * hypervisor that has more than 2 entries we may need to revisit
+ * this.
+ */
+ for (leaf = base + 0xff00; leaf >= base; leaf -= 0x100) {
+ memset (sig, 0, sizeof sig);
+ cpuid (leaf, sig);
+ if (known_signature (sig)) {
+ puts (sig);
+ break;
+ }
+ }