: Introduce multi-core and multi-thread support for guests
cover : Introduce multi-core and multi-thread support for guests
Hi,
currently QEMU's -smp <n> option only injects multiple CPU sockets into the guest. Some operating systems(TM) restrict the number of "real" CPU sockets for licensing reasons, but allow multi-core CPUs. This patch extends the -smp option to let the user specify a topology:
-smp <smp_value>[,cores=<nrcores>][,threads=<nrthreads>]
[,sockets=<nrsockets>][,maxcpus=<max_hotplug_cpus>]
The <smp_value> is the legacy number of virtual CPUs QEMU emulates. If any of the cores, threads or sockets option is given, this value can be omitted. Missing values are calculated to fulfill:smp_value = nrsockets * nrcores * nrthreads
Where in doubt it will favor multiple sockets over multiple cores over multiple threads (to mimic the current behavior).
The entered values will be propagated to the CPUID emulation, where the corresponding bits will be set (both for Intel and AMD processors). These patches are againt QEMU HEAD, but should apply against qemu-kvm, too.
Please review, comment and apply!
Thanks,
Andre.
Signature · 9 lines
Andre Przywara AMD-Operating System Research Center (OSRC), Dresden, Germany Tel: +49 351 448 3567 12 ----to satisfy European Law for business letters: Advanced Micro Devices GmbH Karl-Hammerschmidt-Str. 34, 85609 Dornach b. Muenchen Geschaeftsfuehrer: Thomas M. McCoy; Giuliano Meroni Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen Registergericht Muenchen, HRB Nr. 43632
PATCH 2/3 push CPUID level to 4 to allow Intel multicore decoding
Intel CPUs store the number of cores in CPUID leaf 4. So push the maxleaf value to 4 to allow the guests access to this leaf.
--- target-i386/helper.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
target-i386/helper.c +2 -2
@@ -119,7 +119,7 @@ static x86_def_t x86_defs[] = {#ifdef TARGET_X86_64 { .name = "qemu64", - .level = 2, + .level = 4, .vendor1 = CPUID_VENDOR_AMD_1, .vendor2 = CPUID_VENDOR_AMD_2, .vendor3 = CPUID_VENDOR_AMD_3,
@@ -190,7 +190,7 @@ static x86_def_t x86_defs[] = {#endif { .name = "qemu32", - .level = 2, + .level = 4, .family = 6, .model = 3, .stepping = 3,
Signature · 1 line
1.6.1.3
PATCH 1/3 extend -smp parsing to include cores= and threads= options
In reply to Andre PrzywaraFor injecting multi-core and multi-threading CPU topology into guests extend the -smp syntax to accommodate cores and threads specification.
Syntax: -smp smp_value[,cores=nr_cores][,threads=nr_threads]\
[,socket=nr_sockets][,maxcpus=max_cpus]smp_value is the legacy value specifying the total number of vCPUs for the guest. If you specify one of cores, threads or sockets this value can be omitted. Missing values will be computed to fulfill:smp_value = nr_cores * nr_threads * nr_sockets
where it will favour sockets over cores over threads (to mimic the current behavior, which will only inject multiple sockets.) So -smp 4,threads=2 will inject two sockets with 2 threads each, -smp cores=4 is an abbreviation for -smp 4,cores=4,threads=1,sockets=1. If max_cpus (the number of hotpluggable CPUs) is omitted, it will be set to smp_value.
--- cpu-defs.h | 2 + vl.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 9 deletions(-)
cpu-defs.h +2 -0
@@ -185,6 +185,8 @@ typedef struct CPUWatchpoint {int cpu_index;/* CPU index (informative) */ \uint32_t host_tid;/* host thread ID */ \int numa_node;/* NUMA node this cpu is belonging to */ \ +int nr_cores;/* number of cores within this CPU package */ \ +int nr_threads;/* number of threads within this CPU */ \int running;/* Nonzero if cpu is currently running(usermode). */ \/* user data */ \void *opaque; \
vl.c +57 -9
106 lines not on this page · raw message
Signature · 1 line
1.6.1.3
PATCH 3/3 set CPUID bits to present cores and threads topology
In reply to Andre PrzywaraControlled by the enhanced -smp option set the CPUID bits to present the guest the desired topology. This is vendor specific, but (with the exception of the CMP_LEGACY bit) not conflicting, so we set all bits everytime. There is no real multithreading support for AMD CPUs, so report cores instead.
--- target-i386/helper.c | 28 +++++++++++++++++++++++++--- 1 files changed, 25 insertions(+), 3 deletions(-)
target-i386/helper.c +25 -3
@@ -1631,6 +1631,10 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *ebx = (env->cpuid_apic_id <<24 ) |8 <<8 ;/* CLFLUSH size in quad words, Linux wants it. */ *ecx = env->cpuid_ext_features; *edx = env->cpuid_features; +if (env->nr_cores * env->nr_threads >1 ) { + *ebx |= (env->nr_cores * env->nr_threads) <<16 ; + *edx |=1 <<28 ;/* HTT bit */ + }break ;case 2 :/* cache info: needed for Pentium Pro compatibility */
@@ -1641,21 +1645,29 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,break ;case 4 :/* cache info: needed for Core compatibility */ +if (env->nr_cores >1 ) { + *eax = (env->nr_cores -1 ) <<26 ; + }else { + *eax =0 ; + }switch (count) {case 0 :/* L1 dcache info */ - *eax =0x0000121 ; + *eax |=0x0000121 ; *ebx =0x1c0003f ; *ecx =0x000003f ; *edx =0x0000001 ;break ;case 1 :/* L1 icache info */ - *eax =0x0000122 ; + *eax |=0x0000122 ; *ebx =0x1c0003f ; *ecx =0x000003f ; *edx =0x0000001 ;break ;case 2 :/* L2 cache info */ - *eax =0x0000143 ; + *eax |=0x0000143 ; +if (env->nr_threads >1 ) { + *eax |= (env->nr_threads -1 ) <<14 ; + } *ebx =0x3c0003f ; *ecx =0x0000fff ; *edx =0x0000001 ;
@@ -1708,6 +1720,13 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *ecx = env->cpuid_ext3_features; *edx = env->cpuid_ext2_features; +if (env->nr_cores * env->nr_threads >1 && + env->cpuid_vendor1 ==CPUID_VENDOR_AMD_1 && + env->cpuid_vendor2 ==CPUID_VENDOR_AMD_2 && + env->cpuid_vendor3 ==CPUID_VENDOR_AMD_3 ) { + *ecx |=1 <<1 ;/* CmpLegacy bit */ + } +if (kvm_enabled ()) {/* Nested SVM not yet supported in KVM */ *ecx &= ~CPUID_EXT3_SVM ;
@@ -1762,6 +1781,9 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *ebx =0 ; *ecx =0 ; *edx =0 ; +if (env->nr_cores * env->nr_threads >1 ) { + *ecx |= (env->nr_cores * env->nr_threads) -1 ; + }break ;case 0x8000000A : *eax =0x00000001 ;/* SVM Revision */
Signature · 1 line
1.6.1.3
Re: [Qemu-devel] [PATCH 2/3] push CPUID level to 4 to allow Intel multicore decoding
In reply to Andre PrzywaraQuoted 3 lines · Andre Przywara
Intel CPUs store the number of cores in CPUID leaf 4. So push the maxleaf value to 4 to allow the guests access to this leaf.
There's a slight compatibility risk here. If a guest has broken handling for cpuid level 4, then upgrading qemu would cause it to behave differently.
I don't think that's an issue for this patch, just highlighting the need for a systematic treatment of backward compatibility.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [Qemu-devel] [PATCH 2/3] push CPUID level to 4 to allow Intel multicore decoding
Quoted 8 lines · Avi Kivity
Quoted 3 lines · Andre Przywara
Intel CPUs store the number of cores in CPUID leaf 4. So push the maxleaf value to 4 to allow the guests access to this leaf.
There's a slight compatibility risk here. If a guest has broken handling for cpuid level 4, then upgrading qemu would cause it to behave differently.
I don't think that's an issue for this patch, just highlighting the need for a systematic treatment of backward compatibility.
If you have real headaches about it, I have two options:
What about allowing level to be specified at -cpu command line? This would allow users to say -cpu qemu64,level=2 if they experience problems. The default would stay at level 4.
The other option would be to push the level only to four if we use more than one thread or core.
In my research it turned out that Intel pushed the level beyond 4 with Pentium4 Prescott (probably with the introduction of real dual core chips to differentiate threads and cores), so this is quite some time ago. So I doubt that there are serious issues out there. The only problem I can think of is that the advertised cache topology is somehow bogus and could confuse OSes.
Regards,
Andre.
Signature · 9 lines
Andre Przywara AMD-Operating System Research Center (OSRC), Dresden, Germany Tel: +49 351 448 3567 12 ----to satisfy European Law for business letters: Advanced Micro Devices GmbH Karl-Hammerschmidt-Str. 34, 85609 Dornach b. Muenchen Geschaeftsfuehrer: Thomas M. McCoy; Giuliano Meroni Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen Registergericht Muenchen, HRB Nr. 43632
Re: [Qemu-devel] [PATCH 2/3] push CPUID level to 4 to allow Intel multicore decoding
Quoted 10 lines · Andre Przywara
Quoted 7 lines · Avi Kivity
Quoted 2 lines · Andre Przywara
Intel CPUs store the number of cores in CPUID leaf 4. So push the maxleaf value to 4 to allow the guests access to this leaf.
There's a slight compatibility risk here. If a guest has broken handling for cpuid level 4, then upgrading qemu would cause it to behave differently.
I don't think that's an issue for this patch, just highlighting the need for a systematic treatment of backward compatibility.
If you have real headaches about it, I have two options:
It's really an imaginary headache.
Quoted 2 lines
What about allowing level to be specified at -cpu command line? This would allow users to say -cpu qemu64,level=2 if they experience problems. The default would stay at level 4.
I think this is the best option.
Quoted 3 lines
The other option would be to push the level only to four if we use more than one thread or core.
In my research it turned out that Intel pushed the level beyond 4 with Pentium4 Prescott (probably with the introduction of real dual core chips to differentiate threads and cores), so this is quite some time ago. So I doubt that there are serious issues out there.
I only pointed this out as an example of a new feature that has an effect even if it is not used, something we should beware of.
Quoted 1 line
The only problem I can think of is that the advertised cache topology is somehow bogus and could confuse OSes.
So long as it's smaller than contemporary caches we should be fine.
btw, does -cpu host use the host cpu cache information?
Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH 1/1 allow overriding of CPUID level on command line
The CPUID level determines how many CPUID leafs are exposed to the guest. Some features (like multi-core) cannot be propagated without the proper level, but guests maybe confused by bogus entries in some leafs. So add level= and xlevel= to the list of -cpu options to allow the user to override the default settings. While at it, merge unnecessary local variables into one and allow hexadecimal arguments.
--- target-i386/helper.c | 39 +++++++++++++++++++++++++++++---------- 1 files changed, 29 insertions(+), 10 deletions(-)
target-i386/helper.c +29 -10
@@ -349,7 +349,7 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)char *featurestr, *name =strtok (s,"," );uint32_t plus_features =0 , plus_ext_features =0 , plus_ext2_features =0 , plus_ext3_features =0 ;uint32_t minus_features =0 , minus_ext_features =0 , minus_ext2_features =0 , minus_ext3_features =0 ; -int family =-1 , model =-1 , stepping =-1 ; +uint32_t numvalue; def =NULL ;for (i =0 ; i <ARRAY_SIZE (x86_defs); i++) {
@@ -381,28 +381,47 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model) *val =0 ; val++;if (!strcmp (featurestr,"family" )) {char *err; - family =strtol (val, &err,10 ); -if (!*val || *err || family <0 ) { + numvalue =strtoul (val, &err,0 ); +if (!*val || *err) {fprintf (stderr,"bad numerical value %s\n" , val); goto error; } - x86_cpu_def->family = family; + x86_cpu_def->family = numvalue; }else if (!strcmp (featurestr,"model" )) {char *err; - model =strtol (val, &err,10 ); -if (!*val || *err || model <0 || model >0xff ) { + numvalue =strtoul (val, &err,0 ); +if (!*val || *err || numvalue >0xff ) {fprintf (stderr,"bad numerical value %s\n" , val); goto error; } - x86_cpu_def->model = model; + x86_cpu_def->model = numvalue; }else if (!strcmp (featurestr,"stepping" )) {char *err; - stepping =strtol (val, &err,10 ); -if (!*val || *err || stepping <0 || stepping >0xf ) { + numvalue =strtoul (val, &err,0 ); +if (!*val || *err || numvalue >0xf ) {fprintf (stderr,"bad numerical value %s\n" , val); goto error; } - x86_cpu_def->stepping = stepping; + x86_cpu_def->stepping = numvalue ; + }else if (!strcmp (featurestr,"level" )) { +char *err; + numvalue =strtoul (val, &err,0 ); +if (!*val || *err) { +fprintf (stderr,"bad numerical value %s\n" , val); + goto error; + } + x86_cpu_def->level = numvalue; + }else if (!strcmp (featurestr,"xlevel" )) { +char *err; + numvalue =strtoul (val, &err,0 ); +if (!*val || *err) { +fprintf (stderr,"bad numerical value %s\n" , val); + goto error; + } +if (numvalue <0x80000000 ) { + numvalue +=0x80000000 ; + } + x86_cpu_def->xlevel = numvalue; }else if (!strcmp (featurestr,"vendor" )) {if (strlen (val) !=12 ) {fprintf (stderr,"vendor string must be 12 chars long\n" );
Signature · 1 line
1.6.1.3
Re: [Qemu-devel] [PATCH 2/3] push CPUID level to 4 to allow Intel multicore decoding
In reply to Andre PrzywaraQuoted 7 lines · Andre Przywara
The other option would be to push the level only to four if we use more than one thread or core.
In my research it turned out that Intel pushed the level beyond 4 with Pentium4 Prescott (probably with the introduction of real dual core chips to differentiate threads and cores), so this is quite some time ago.
Pentium 4 wasn't that long ago, from a point of view of running legacy guests.
I see quite a lot of code in Linux (to pick an example - presumably also other OSes) which checks for things by looking at model and family number, so I wonder if it's wise to depart a long way from CPUIDs which resemble real hardware which has existed.
We've already seen a problem along these lines, which was the x86 CPUID_SEP feature not getting used properly, but that was quite well hidden, only affecting one application (Skype) on Windows.
Could it make more sense to, in effect, upgrade the default CPUID to Pentium 4 (instead of Pentium Pro) when cores > 1?
-- Jamie
Re: [Qemu-devel] [PATCH 2/3] push CPUID level to 4 to allow Intel multicore decoding
Quoted 10 lines · Jamie Lokier
Quoted 6 lines · Andre Przywara
The other option would be to push the level only to four if we use more than one thread or core.
In my research it turned out that Intel pushed the level beyond 4 with Pentium4 Prescott (probably with the introduction of real dual core chips to differentiate threads and cores), so this is quite some time ago.
Pentium 4 wasn't that long ago, from a point of view of running legacy guests.
Well, but long enough to get those software issues ruled out. If any serious OS would have had problems, somebody would have noticed and fixed it.
Quoted 4 lines
I see quite a lot of code in Linux (to pick an example - presumably also other OSes) which checks for things by looking at model and family number, so I wonder if it's wise to depart a long way from CPUIDs which resemble real hardware which has existed.
Yeah, but most of the stuff Linux checks are bugs and workarounds, so I am not 100% sure whether it's an advantage. We should be careful with adjusting the working default QEMU/KVM has right now.
Quoted 6 lines
We've already seen a problem along these lines, which was the x86
CPUID_SEPfeature not getting used properly, but that was quite well hidden, only affecting one application (Skype) on Windows.Could it make more sense to, in effect, upgrade the default CPUID to Pentium 4 (instead of Pentium Pro) when cores > 1?
OK, OK, I will finally send this patch out (following in another mail).
This is a kvm64 CPU (which should become the new KVM's default, if it proves to work well). It looks like a P4 Prescott, that seems to be a good base, since it has a decent base line of features and is among the first CPUs which are supported by KVM. If someone has such a real beast (best would be a Pentium 4 662 or other Prescotts with x86_64, HT and VT-x), I would love to see a complete CPUID dump (including the 0000_0004 subleafs, which x86info omits).
Regards,
Andre.
Signature · 9 lines
Andre Przywara AMD-Operating System Research Center (OSRC), Dresden, Germany Tel: +49 351 488-3567-12 ----to satisfy European Law for business letters: Advanced Micro Devices GmbH Karl-Hammerschmidt-Str. 34, 85609 Dornach b. Muenchen Geschaeftsfuehrer: Jochen Polster; Thomas M. McCoy; Giuliano Meroni Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen Registergericht Muenchen, HRB Nr. 43632
Re: [Qemu-devel] [PATCH 2/3] push CPUID level to 4 to allow Intel multicore decoding
In reply to Avi KivityQuoted 4 lines · Avi Kivity
Quoted 1 line · Andre Przywara
The only problem I can think of is that the advertised cache topology is somehow bogus and could confuse OSes.
So long as it's smaller than contemporary caches we should be fine.
Oh, I see now that Alex' Core2Duo (on which QEMU's leaf 4 values are based on) has 4 MB of L2-Cache. Although that is not gigantic for Intel CPUs, it's certainly too high for many host CPUs. I will lower this value to say 1MB or 512K to better match the majority of host. I will send a patch together with some more CPUID reworks I am working on.
Quoted 2 lines
btw, does -cpu host use the host cpu cache information?
Good point. It does not, it only deals with leaves 0, 1, 8000_0000 and 8000_0001. I have created a white list of further CPUID leaves which are safe to propagate to the guest and will also send this in a while.
Thanks for these hints,
Andre.
Signature · 9 lines
Andre Przywara AMD-Operating System Research Center (OSRC), Dresden, Germany Tel: +49 351 448 3567 12 ----to satisfy European Law for business letters: Advanced Micro Devices GmbH Karl-Hammerschmidt-Str. 34, 85609 Dornach b. Muenchen Geschaeftsfuehrer: Thomas M. McCoy; Giuliano Meroni Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen Registergericht Muenchen, HRB Nr. 43632