ppc: Implement -host CPU
cover ppc: Implement -host CPU
This series of patches implements a -cpu host option for ppc, analagous to the version that already exists for x86. For now, only the basic framework is implement. In later patches, we will need to improve the code to override those parts of the cpu spec that can be queried from the host, and use this mechanism to subsume, for example, the current advertisement by the pseries machine of KVM CPU capabilities to the guest.
PATCH 1/4 ppc: Remove broken partial PVR matching
The ppc target contains a ppc_find_by_pvr() function, which looks up a CPU spec based on a PVR (that is, based on the value in the target cpu's Processor Version Register). PVR values contain information on both the cpu model (upper 16 bits, usually) and on the precise revision (low 16 bits, usually).
ppc_find_by_pvr, as well as making exact PVR matches, attempts to find "close" PVR matches, when we don't have a CPU spec for the exact revision specified. This sounds like a good idea, execpt that the current logic is completely nonsensical.
It seems to assume CPU families are subdivided bit by bit in the PVR in a way they just aren't. Specifically, it requires a match on all bits of the specified pvr up to the last non-zero bit. This has the bizarre effect that when the low bits are simply a sequential revision number (a common though not universal pattern), then odd specified revisions must be matched exactly, whereas even specified revisions will also match the next odd revision, likewise for powers of 4, 8 and so forth.
To correctly do inexact matching we'd need to re-organize the table of CPU specs to include a mask showing what PVR range the spec is compatible with (similar to the cputable code in the Linux kernel).
For now, just remove the bogosity by only permitting exact PVR matches. That at least makes the matching simple and consistent. If we need inexact matching we can add the necessary per-subfamily masks later.
--- target-ppc/translate_init.c | 38 +++++++------------------------------- 1 files changed, 7 insertions(+), 31 deletions(-)
target-ppc/translate_init.c +6 -30
@@ -10043,40 +10043,16 @@ int cpu_ppc_register_internal (CPUPPCState *env, const ppc_def_t *def)static const ppc_def_t *ppc_find_by_pvr (uint32_t pvr) { -const ppc_def_t *ret; -uint32_t pvr_rev; -int i, best, match, best_match, max; +int i; - ret =NULL ; - max =ARRAY_SIZE (ppc_defs); - best =-1 ; - pvr_rev = pvr &0xFFFF ; -/* We want all specified bits to match */ - best_match =32 -ctz32 (pvr_rev); -for (i =0 ; i < max; i++) { -/* We check that the 16 higher bits are the same to ensure the CPU -* model will be the choosen one. -*/ -if (((pvr ^ ppc_defs[i].pvr) >>16 ) ==0 ) { -/* We want as much as possible of the low-level 16 bits -* to be the same but we allow inexact matches. -*/ - match =clz32 (pvr_rev ^ (ppc_defs[i].pvr &0xFFFF )); -/* We check '>=' instead of '>' because the PPC_defs table -* is ordered by increasing revision. -* Then, we will match the higher revision compatible -* with the requested PVR -*/ -if (match >= best_match) { - best = i; - best_match = match; - } +for (i =0 ; i <ARRAY_SIZE (ppc_defs); i++) { +/* If we have an exact match, we're done */ +if (pvr == ppc_defs[i].pvr) { +return &ppc_defs[i]; } } -if (best !=-1 ) - ret = &ppc_defs[best]; -return ret; +return NULL ; }#include <ctype.h>
Signature · 1 line
1.7.6.3
PATCH 2/4 ppc: First cut implementation of -cpu host
In reply to David GibsonFor convenience with kvm, x86 allows the user to specify -cpu host on the qemu command line, which means make the guest cpu the same as the host cpu. This patch implements the same option for ppc targets.
For now, this just read the host PVR (Processor Version Register) and selects one of our existing CPU specs based on it. This means that the option will not work if the host cpu is not supported by TCG, even if that wouldn't matter for use under kvm.
In future, we can extend this in future to override parts of the cpu spec based on information obtained from the host (via /proc/cpuinfo, the host device tree, or explicit KVM calls). That will let us handle cases where the real kvm-virtualized CPU doesn't behave exactly like the TCG-emulated CPU. With appropriate annotation of the CPU specs we'll also then be able to use host cpus under kvm even when there isn't a matching full TCG model.
--- target-ppc/cpu.h | 1 + target-ppc/kvm.c | 19 +++++++++++++++++++ target-ppc/kvm_ppc.h | 6 ++++++ target-ppc/translate_init.c | 8 +++++++- 4 files changed, 33 insertions(+), 1 deletions(-)
target-ppc/cpu.h +1 -0
@@ -1107,6 +1107,7 @@ void ppc_store_msr (CPUPPCState *env, target_ulong value);void ppc_cpu_list (FILE *f,fprintf_function cpu_fprintf); +const ppc_def_t *ppc_find_by_pvr (uint32_t pvr);const ppc_def_t *cpu_ppc_find_by_name (const char *name);int cpu_ppc_register_internal (CPUPPCState *env,const ppc_def_t *def);
target-ppc/kvm.c +19 -0
@@ -878,6 +878,25 @@ int kvmppc_remove_spapr_tce(void *table, int fd, uint32_t window_size)return 0 ; } +static inline uint32_t mfpvr (void ) +{ +uint32_t pvr; + + asm ("mfpvr %0" + :"=r" (pvr)); +return pvr; +} + +const ppc_def_t *kvmppc_host_cpu_def (void ) +{ +uint32_t host_pvr =mfpvr (); +const ppc_def_t *base_spec; + + base_spec =ppc_find_by_pvr (host_pvr); + +return base_spec; +} +bool kvm_arch_stop_on_emulation_error (CPUState *env) {return true;
target-ppc/kvm_ppc.h +6 -0
@@ -24,6 +24,7 @@ int kvmppc_smt_threads(void);off_t kvmppc_alloc_rma (const char *name);void *kvmppc_create_spapr_tce (uint32_t liobn,uint32_t window_size,int *pfd);int kvmppc_remove_spapr_tce (void *table,int pfd,uint32_t window_size); +const ppc_def_t *kvmppc_host_cpu_def (void );#else
@@ -83,6 +84,11 @@ static inline int kvmppc_remove_spapr_tce(void *table, int pfd, return -1; } +static inline const ppc_def_t *kvmppc_host_cpu_def (void ) +{ +return NULL ; +} +#endif #ifndef CONFIG_KVM
target-ppc/translate_init.c +7 -1
@@ -24,6 +24,8 @@#include "dis-asm.h" #include "gdbstub.h" +#include <kvm.h> +#include "kvm_ppc.h" //#define PPC_DUMP_CPU //#define PPC_DEBUG_SPR
@@ -10041,7 +10043,7 @@ int cpu_ppc_register_internal (CPUPPCState *env, const ppc_def_t *def)return 0 ; } -static const ppc_def_t *ppc_find_by_pvr (uint32_t pvr) +const ppc_def_t *ppc_find_by_pvr (uint32_t pvr) {int i;
@@ -10063,6 +10065,10 @@ const ppc_def_t *cpu_ppc_find_by_name (const char *name)const char *p;int i, max, len; +if (kvm_enabled () && (strcasecmp (name,"host" ) ==0 )) { +return kvmppc_host_cpu_def (); + } +/* Check if the given name is a PVR */ len =strlen (name);if (len ==10 && name[0 ] =='0' && name[1 ] =='x' ) {
Signature · 1 line
1.7.6.3
PATCH 3/4 ppc: Add cpu defs for POWER7 revisions 2.1 and 2.3
In reply to David GibsonThis patch adds cpu specs to the table for POWER7 revisions 2.1 and 2.3. This allows -cpu host to be used on these host cpus.
--- target-ppc/translate_init.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
target-ppc/translate_init.c +4 -0
@@ -7333,6 +7333,8 @@ enum { CPU_POWERPC_POWER6A = 0x0F000002,#define CPU_POWERPC_POWER7 CPU_POWERPC_POWER7_v20 CPU_POWERPC_POWER7_v20 =0x003F0200 , + CPU_POWERPC_POWER7_v21 =0x003F0201 , + CPU_POWERPC_POWER7_v23 =0x003F0203 ,CPU_POWERPC_970 =0x00390202 , #define CPU_POWERPC_970FX CPU_POWERPC_970FX_v31 CPU_POWERPC_970FX_v10 = 0x00391100,
@@ -9139,6 +9141,8 @@ static const ppc_def_t ppc_defs[] = {/* POWER7 */ POWERPC_DEF ("POWER7" ,CPU_POWERPC_POWER7 ,POWER7 ),POWERPC_DEF ("POWER7_v2.0" , CPU_POWERPC_POWER7_v20,POWER7 ), +POWERPC_DEF ("POWER7_v2.1" , CPU_POWERPC_POWER7_v21,POWER7 ), +POWERPC_DEF ("POWER7_v2.3" , CPU_POWERPC_POWER7_v23,POWER7 ),/* PowerPC 970 */ POWERPC_DEF("970", CPU_POWERPC_970, 970),/* PowerPC 970FX (G5) */
Signature · 1 line
1.7.6.3
PATCH 4/4 pseries: Under kvm use guest cpu = host cpu by default
In reply to David GibsonNow that we've implemented -cpu host for ppc, this patch updates the pseries machine to use the host cpu as the guest cpu by default when running under KVM. This is important because under KVM Book3S-HV the guest cpu _cannot_ be of a different type to the host cpu (at the moment KVM Book3S-HV will silently virtualize the host cpu instead of whatever was requested, but in future it is likely to simply refuse to run the VM if a cpu model other than the host's is requested).
--- hw/spapr.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
hw/spapr.c +1 -1
@@ -403,7 +403,7 @@ static void ppc_spapr_init(ram_addr_t ram_size,/* init CPUs */ if (cpu_model ==NULL ) { - cpu_model ="POWER7" ; + cpu_model =kvm_enabled () ?"host" :"POWER7" ; }for (i =0 ; i < smp_cpus; i++) { env =cpu_init (cpu_model);
Signature · 1 line
1.7.6.3
Alexander Graf
AppliedQuoted 7 lines · David Gibson
This series of patches implements a -cpu host option for ppc, analagous to the version that already exists for x86. For now, only the basic framework is implement. In later patches, we will need to improve the code to override those parts of the cpu spec that can be queried from the host, and use this mechanism to subsume, for example, the current advertisement by the pseries machine of KVM CPU capabilities to the guest.
Thanks, applied all to ppc-next.
Alex