1 /* virt-what-cpuid-helper: Are we running inside KVM or Xen HVM?
2 * Copyright (C) 2008 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* This program was suggested by Gleb Natapov and written by Paolo
20 * Bonzini, with a few modifications by Richard W.M. Jones.
26 #if defined(__i386__) || defined(__x86_64__)
29 cpuid (unsigned int eax, char *sig)
31 unsigned int *sig32 = (unsigned int *) sig;
34 "xchgl %%ebx,%1; xor %%ebx,%%ebx; cpuid; xchgl %%ebx,%1"
35 : "=a" (eax), "+r" (sig32[0]), "=c" (sig32[1]), "=d" (sig32[2])
46 unsigned int base = 0x40000000, leaf = base;
47 unsigned int max_entries;
49 memset (sig, 0, sizeof sig);
50 max_entries = cpuid (leaf, sig);
53 /* Most hypervisors only have information in leaf 0x40000000, but
54 * upstream Xen contains further leaf entries (in particular when
55 * used with Viridian [HyperV] extensions). CPUID is supposed to
56 * return the maximum leaf offset in %eax, so that's what we use,
57 * but only if it looks sensible.
59 if (max_entries > 3 && max_entries < 0x10000) {
60 for (leaf = base + 0x100; leaf <= base + max_entries; leaf += 0x100) {
61 memset (sig, 0, sizeof sig);
68 #else /* !i386, !x86_64 */
73 /* nothing for other architectures */