tcg: support heterogenous CPU clusters
v3 cover tcg: support heterogenous CPU clusters
In the process of debugging my board code that uses this feature, I realized I needed to support the CPU object not being a direct child of the cluster object, which also lead me to the object_child_foreach_recursive() function which is a neater way to iterate children anyway. I also figured out we could add an assert which catches most "parenting CPU vs cluster realize ordering" bugs.
Only patch 2 has changed here, others are already reviewed.
Changes v2->v3:
* allow CPU objects to be indirect children of the cluster; this is useful for ARMv7M, where the CPU object is a child of the armv7m container and the board code that sets up the cluster object only has the armv7m container object:
this is done by using object_child_foreach_recursive() rather than an open-coded child iteration
* add an assertion that the cluster has at least one CPU, which catches the easiest-to-make errors when creating and populating the cluster
Changes v1->v2:
* just fixing a bug in patch 3 that meant we were accidentally never reusing TBs. Patch 3 is the only one that needs review.
Original cover letter included below for context.
thanks
-- PMM
Currently, TCG implicitly assumes that all CPUs are alike, because we have a single cache of generated TBs and we don't account for which CPU generated the code or is looking for the TB when adding or searching for generated TBs. This can go wrong in two situations:
(1) two CPUs have different physical address spaces (eg CPU 1 has one lot of RAM/ROM, and CPU 2 has different RAM/ROM): the physical address alone is then not sufficient to distinguish what code to run
(2) two CPUs have different features (eg FPU vs no FPU):
since our TCG frontends bake assumptions into the generated code about the presence/absence of features, if a CPU with FPU picks up a TB for one generated without an FPU it will behave wrongly
This is unfortunate, because we already have one board in the tree which has a heterogenous setup: the xlnx-zcu102. This board has 4xCortex-A53 and 2xCortex-R5F. Although most "normal" guest code doesn't run into this, it is possible to demonstrate the bug with it. There's a test case at http://people.linaro.org/~peter.maydell/xlnx-het.tgz
which arranges to run a fragment of code in AArch32 which should behave differently on the two CPUs, but does not (either the A53 gets the behaviour the R5 should have, or vice-versa).
This patchset adds the "cluster ID" to the set of things we include in the TCG TB hash, which means that the two sets of CPUs in this board can no longer accidentally get TBs generated by the other cluster. It fixes the bug demonstrated by the test case.
Design notes:
* Adding cluster ID to the hash is RTH's suggestion. It might also be possible to make each cluster have a TCGContext code generation context, and have the CPUs use the right one for the cluster, but that would be a lot more code rework compared to this approach.
* I put the cluster number in the existing cflags, since we have plenty of spare bits there. If in future we need either more than 256 clusters (unlikely) or want to reclaim bits in the cflags field for some other purpose we can always extend our xxhash from 28 to 32 bytes. (I actually wrote the patch to do that before realising I didn't need it...)
* The cluster object now fills in the CPU object's cluster_index field when the cluster object is realized. This imposes an ordering constraint that all CPUs must be added to a cluster before realizing the cluster. That's not a big deal, except that unfortunately QOM provides no way that I could find to enforce this. If anybody has a suggestion for a better approach that would be great. Patch 1 therefore makes sure our only current user of the cluster feature obeys the ordering constraint.
* Patch 4 is a tidyup to the gdbstub code which is possible now that the CPUState struct has the cluster ID in it.
I believe that this fix is basically all we need to support heterogenous setups (assuming of course that you just mean "within an architecture family" like Arm, rather than systems with say both an Arm and a Microblaze core in them). The other things I have considered are:
* code that calls cpu_physical_memory_read() and friends, uses address_space_memory, or otherwise assumes there's only a single view of "CPU memory": I sent some patches out before Christmas that fixed these in generic code like the monitor and disassembler. There are also some uses in target-arch or device-specific code, which I think we can in practice ignore unless/until we come to implement a board that is heterogenous and also uses those devices or target architectures.
* handling of TB invalidation: I think this should Just Work, because tb_invalidate_phys_addr() takes an AddressSpace+hwaddr, which it converts into a ramaddr for (the badly misnamed) tb_invalidate_phys_page_range(). So the TB caching all works on ramaddrs, and if CPU 1 writes to some ram X that's mapped at physaddr A for it but at physaddr B for CPU 2, we will still correctly invalidate any TBs that CPU 2 had for code that from its point of view lives at physaddr B. Note that translate-all.c has a single l1_map[] structure which will have PageDesc entries for pages for all CPUs in the system. I believe this is OK because the only thing we use them for is TB invalidation.
* dirty memory tracking: like TB invalidation, this is OK because it all works either on MemoryRegions or on ramaddrs, not on physaddrs.
Have I missed anything that we also need to fix ?
thanks
-- PMM
Peter Maydell (4):
hw/arm/xlx-zynqmp: Realize cluster after putting RPUs in it qom/cpu: Add cluster_index to CPUState
accel/tcg: Add cluster number to TCG TB hash
gdbstub: Simplify gdb_get_cpu_pid() to use cpu->cluster_index
include/exec/exec-all.h | 4 +++- include/hw/cpu/cluster.h | 24 ++++++++++++++++++++ include/qom/cpu.h | 7 ++++++ accel/tcg/cpu-exec.c | 3 +++ accel/tcg/translate-all.c | 3 +++ gdbstub.c | 48 ++++----------------------------------- hw/arm/xlnx-zynqmp.c | 4 ++-- hw/cpu/cluster.c | 46 +++++++++++++++++++++++++++++++++++++ qom/cpu.c | 1 + 9 files changed, 94 insertions(+), 46 deletions(-)
Signature · 1 line
2.20.1
PATCH v3 1/4 hw/arm/xlx-zynqmp: Realize cluster after putting RPUs in it
Currently the cluster implementation doesn't have any constraints on the ordering of realizing the TYPE_CPU_CLUSTER and populating it with child objects. We want to impose a constraint that realize must happen only after all the child objects are added, so move the realize of rpu_cluster. (The apu_cluster is already realized after child population.)
--- hw/arm/xlnx-zynqmp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
hw/arm/xlnx-zynqmp.c +2 -2
@@ -183,8 +183,6 @@ static void xlnx_zynqmp_create_rpu(XlnxZynqMPState *s, const char *boot_cpu, &error_abort, NULL);qdev_prop_set_uint32 (DEVICE (&s->rpu_cluster),"cluster-id" ,1 ); -qdev_init_nofail (DEVICE (&s->rpu_cluster)); -for (i =0 ; i < num_rpus; i++) {char *name;
@@ -212,6 +210,8 @@ static void xlnx_zynqmp_create_rpu(XlnxZynqMPState *s, const char *boot_cpu,return ; } } + +qdev_init_nofail (DEVICE (&s->rpu_cluster)); }static void xlnx_zynqmp_init (Object *obj)
Signature · 1 line
2.20.1
Re: [Qemu-arm] [PATCH v3 1/4] hw/arm/xlx-zynqmp: Realize cluster after putting RPUs in it
Quoted 11 lines · Peter Maydell
Currently the cluster implementation doesn't have any constraints on the ordering of realizing the
TYPE_CPU_CLUSTERand populating it with child objects. We want to impose a constraint that realize must happen only after all the child objects are added, so move the realize ofrpu_cluster. (Theapu_clusteris already realized after child population.)Signed-off-by Peter Maydell <[email protected]>Reviewed-by Richard Henderson <[email protected]>Reviewed-by Luc Michel <[email protected]>Reviewed-by Alistair Francis <[email protected]>
Quoted 29 lines
--- hw/arm/xlnx-zynqmp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)hw/arm/xlnx-zynqmp.c +2
-2@@ -183,8 +183,6 @@ static void xlnx_zynqmp_create_rpu(XlnxZynqMPState *s, const char *boot_cpu, &error_abort, NULL);qdev_prop_set_uint32 (DEVICE (&s->rpu_cluster),"cluster-id" ,1 ); -qdev_init_nofail (DEVICE (&s->rpu_cluster)); -for (i =0 ; i < num_rpus; i++) {char *name;@@ -212,6 +210,8 @@ static void xlnx_zynqmp_create_rpu(XlnxZynqMPState *s, const char *boot_cpu,return ; } } + +qdev_init_nofail (DEVICE (&s->rpu_cluster)); }static void xlnx_zynqmp_init (Object *obj)Signature · 1 line
2.20.1
PATCH v3 2/4 qom/cpu: Add cluster_index to CPUState
In reply to Peter MaydellFor TCG we want to distinguish which cluster a CPU is in, and we need to do it quickly. Cache the cluster index in the CPUState struct, by having the cluster object set cpu->cluster_index for each CPU child when it is realized.
This means that board/SoC code must add all CPUs to the cluster before realizing the cluster object. Regrettably QOM provides no way to prevent adding children to a realized object and no way for the parent to be notified when a new child is added to it, so we don't have any way to enforce/assert this constraint; all we can do is document it in a comment. We can at least put in a check that the cluster contains at least one CPU, which should catch the typical cases of "realized cluster too early" or "forgot to parent the CPUs into it".
The restriction on how many clusters can exist in the system is imposed by TCG code which will be added in a subsequent commit, but the check to enforce it in cluster.c fits better in this one.
---
Changes v2->v3:
* allow CPU objects to be indirect children of the cluster; this is useful for ARMv7M, where the CPU object is a child of the armv7m container and the board code that sets up the cluster object only has the armv7m container object:
this is done by using object_child_foreach_recursive() rather than an open-coded child iteration
* add an assertion that the cluster has at least one CPU, which catches the easiest-to-make errors when creating and populating the cluster
--- include/hw/cpu/cluster.h | 24 +++++++++++++++++++++ include/qom/cpu.h | 7 ++++++ hw/cpu/cluster.c | 46 ++++++++++++++++++++++++++++++++++++++++ qom/cpu.c | 1 + 4 files changed, 78 insertions(+)
include/hw/cpu/cluster.h +24 -0
@@ -34,12 +34,36 @@* Arm big.LITTLE system) they should be in different clusters. If the CPUs do * not have the same view of memory (for example the main CPU and a management * controller processor) they should be in different clusters. +* +* A cluster is created by creating an object of TYPE_CPU_CLUSTER, and then +* adding the CPUs to it as QOM child objects (e.g. using the +* object_initialize_child() or object_property_add_child() functions). +* The CPUs may be either direct children of the cluster object, or indirect +* children (e.g. children of children of the cluster object). +* +* All CPUs must be added as children before the cluster is realized. +* (Regrettably QOM provides no way to prevent adding children to a realized +* object and no way for the parent to be notified when a new child is added +* to it, so this restriction is not checked for, but the system will not +* behave correctly if it is not adhered to. The cluster will assert that +* it contains at least one CPU, which should catch most inadvertent +* violations of this constraint.) +* +* A CPU which is not put into any cluster will be considered implicitly +* to be in a cluster with all the other "loose" CPUs, so all CPUs that are +* not assigned to clusters must be identical. */ #define TYPE_CPU_CLUSTER "cpu-cluster"#define CPU_CLUSTER (obj) \ OBJECT_CHECK(CPUClusterState, (obj), TYPE_CPU_CLUSTER) +/* +* This limit is imposed by TCG, which puts the cluster ID into an +* 8 bit field (and uses all-1s for the default "not in any cluster"). +*/ +#define MAX_CLUSTERS 255 +/** * CPUClusterState: * @cluster_id: The cluster ID. This value is for internal use only and should
include/qom/cpu.h +7 -0
@@ -280,6 +280,11 @@ struct qemu_work_item;/** * CPUState: * @cpu_index: CPU index (informative). +* @cluster_index: Identifies which cluster this CPU is in. +* For boards which don't define clusters or for "loose" CPUs not assigned +* to a cluster this will be UNASSIGNED_CLUSTER_INDEX; otherwise it will +* be the same as the cluster-id property of the CPU object's TYPE_CPU_CLUSTER +* QOM parent. * @nr_cores: Number of cores within this CPU package. * @nr_threads: Number of threads within this CPU. * @running: #true if CPU is currently running (lockless).
@@ -405,6 +410,7 @@ struct CPUState {/* TODO Move common fields from CPUArchState here. */ int cpu_index; +int cluster_index;uint32_t halted;uint32_t can_do_io;int32_t exception_index;
@@ -1111,5 +1117,6 @@ extern const struct VMStateDescription vmstate_cpu_common;#endif /* NEED_CPU_H */ #define UNASSIGNED_CPU_INDEX -1 +#define UNASSIGNED_CLUSTER_INDEX -1#endif
hw/cpu/cluster.c +46 -0
65 lines not on this page · raw message
qom/cpu.c +1 -0
@@ -364,6 +364,7 @@ static void cpu_common_initfn(Object *obj)CPUClass *cc =CPU_GET_CLASS (obj); cpu->cpu_index =UNASSIGNED_CPU_INDEX ; + cpu->cluster_index =UNASSIGNED_CLUSTER_INDEX ; cpu->gdb_num_regs = cpu->gdb_num_g_regs = cc->gdb_num_core_regs;/* *-user doesn't have configurable SMP topology */ /* the default value is changed by qemu_init_vcpu() for softmmu */
Signature · 1 line
2.20.1
Re: [Qemu-arm] [PATCH v3 2/4] qom/cpu: Add cluster_index to CPUState
Quoted 15 lines · Peter Maydell
For TCG we want to distinguish which cluster a CPU is in, and we need to do it quickly. Cache the cluster index in the CPUState struct, by having the cluster object set
cpu->cluster_indexfor each CPU child when it is realized.This means that board/SoC code must add all CPUs to the cluster before realizing the cluster object. Regrettably QOM provides no way to prevent adding children to a realized object and no way for the parent to be notified when a new child is added to it, so we don't have any way to enforce/assert this constraint; all we can do is document it in a comment. We can at least put in a check that the cluster contains at least one CPU, which should catch the typical cases of "realized cluster too early" or "forgot to parent the CPUs into it".
Quoted 5 lines
The restriction on how many clusters can exist in the system is imposed by TCG code which will be added in a subsequent commit, but the check to enforce it in
cluster.cfits better in this one.Signed-off-by Peter Maydell <[email protected]>
Quoted 175 lines
---
Changesv2->v3:
* allow CPU objects to be indirect children of the cluster; this is useful for ARMv7M, where the CPU object is a child of the armv7m container and the board code that sets up the cluster object only has the armv7m container object:
this is done by usingobject_child_foreach_recursive()rather than an open-coded child iteration
* add an assertion that the cluster has at least one CPU, which catches the easiest-to-make errors when creating and populating the cluster--- include/hw/cpu/cluster.h | 24 +++++++++++++++++++++ include/qom/cpu.h | 7 ++++++ hw/cpu/cluster.c | 46 ++++++++++++++++++++++++++++++++++++++++ qom/cpu.c | 1 + 4 files changed, 78 insertions(+)include/hw/cpu/cluster.h +24
-0@@ -34,12 +34,36 @@* Arm big.LITTLE system) they should be in different clusters. If the CPUs do * not have the same view of memory (for example the main CPU and a management * controller processor) they should be in different clusters. +* +* A cluster is created by creating an object of TYPE_CPU_CLUSTER, and then +* adding the CPUs to it as QOM child objects (e.g. using the +* object_initialize_child() or object_property_add_child() functions). +* The CPUs may be either direct children of the cluster object, or indirect +* children (e.g. children of children of the cluster object). +* +* All CPUs must be added as children before the cluster is realized. +* (Regrettably QOM provides no way to prevent adding children to a realized +* object and no way for the parent to be notified when a new child is added +* to it, so this restriction is not checked for, but the system will not +* behave correctly if it is not adhered to. The cluster will assert that +* it contains at least one CPU, which should catch most inadvertent +* violations of this constraint.) +* +* A CPU which is not put into any cluster will be considered implicitly +* to be in a cluster with all the other "loose" CPUs, so all CPUs that are +* not assigned to clusters must be identical. */ #define TYPE_CPU_CLUSTER "cpu-cluster"#define CPU_CLUSTER (obj) \ OBJECT_CHECK(CPUClusterState, (obj), TYPE_CPU_CLUSTER) +/* +* This limit is imposed by TCG, which puts the cluster ID into an +* 8 bit field (and uses all-1s for the default "not in any cluster"). +*/ +#define MAX_CLUSTERS 255 +/** * CPUClusterState: * @cluster_id: The cluster ID. This value is for internal use only and should include/qom/cpu.h +7
-0@@ -280,6 +280,11 @@ struct qemu_work_item;/** * CPUState: * @cpu_index: CPU index (informative). +* @cluster_index: Identifies which cluster this CPU is in. +* For boards which don't define clusters or for "loose" CPUs not assigned +* to a cluster this will be UNASSIGNED_CLUSTER_INDEX; otherwise it will +* be the same as the cluster-id property of the CPU object's TYPE_CPU_CLUSTER +* QOM parent. * @nr_cores: Number of cores within this CPU package. * @nr_threads: Number of threads within this CPU. * @running: #true if CPU is currently running (lockless). @@ -405,6 +410,7 @@ struct CPUState {/* TODO Move common fields from CPUArchState here. */ int cpu_index; +int cluster_index;uint32_t halted;uint32_t can_do_io;int32_t exception_index;@@ -1111,5 +1117,6 @@ extern const struct VMStateDescription vmstate_cpu_common;#endif /* NEED_CPU_H */ #define UNASSIGNED_CPU_INDEX -1 +#define UNASSIGNED_CLUSTER_INDEX -1#endif hw/cpu/cluster.c +46
-0@@ -20,19 +20,65 @@#include "qemu/osdep.h" #include "hw/cpu/cluster.h" +#include "qom/cpu.h" #include "qapi/error.h" #include "qemu/module.h" +#include "qemu/cutils.h" static Property cpu_cluster_properties[] = {DEFINE_PROP_UINT32 ("cluster-id" , CPUClusterState, cluster_id,0 ),DEFINE_PROP_END_OF_LIST () }; +typedef struct CallbackData { +CPUClusterState *cluster; +int cpu_count; +}CallbackData ; + +static int add_cpu_to_cluster (Object *obj,void *opaque) +{ +CallbackData *cbdata = opaque; +CPUState *cpu = (CPUState *)object_dynamic_cast (obj,TYPE_CPU ); + +if (cpu) { + cpu->cluster_index = cbdata->cluster->cluster_id; + cbdata->cpu_count++; + } +return 0 ; +} + +static void cpu_cluster_realize (DeviceState *dev,Error **errp) +{ +/* Iterate through all our CPU children and set their cluster_index */ +CPUClusterState *cluster =CPU_CLUSTER (dev); +Object *cluster_obj =OBJECT (dev); +CallbackData cbdata = { + .cluster = cluster, + .cpu_count =0 , + }; + +if (cluster->cluster_id >=MAX_CLUSTERS ) { +error_setg (errp,"cluster-id must be less than %d" ,MAX_CLUSTERS ); +return ; + } + +object_child_foreach_recursive (cluster_obj, add_cpu_to_cluster, &cbdata); + +/* +* A cluster with no CPUs is a bug in the board/SoC code that created it; +* if you hit this during development of new code, check that you have +* created the CPUs and parented them into the cluster object before +* realizing the cluster object. +*/ +assert (cbdata.cpu_count >0 ); +} +static void cpu_cluster_class_init (ObjectClass *klass,void *data) {DeviceClass *dc =DEVICE_CLASS (klass); dc->props = cpu_cluster_properties; + dc->realize = cpu_cluster_realize; }static const TypeInfo cpu_cluster_type_info = {qom/cpu.c +1
-0@@ -364,6 +364,7 @@ static void cpu_common_initfn(Object *obj)CPUClass *cc =CPU_GET_CLASS (obj); cpu->cpu_index =UNASSIGNED_CPU_INDEX ; + cpu->cluster_index =UNASSIGNED_CLUSTER_INDEX ; cpu->gdb_num_regs = cpu->gdb_num_g_regs = cc->gdb_num_core_regs;/* *-user doesn't have configurable SMP topology */ /* the default value is changed by qemu_init_vcpu() for softmmu */ Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH v3 2/4] qom/cpu: Add cluster_index to CPUState
In reply to Peter MaydellQuoted 15 lines · Peter Maydell
For TCG we want to distinguish which cluster a CPU is in, and we need to do it quickly. Cache the cluster index in the CPUState struct, by having the cluster object set
cpu->cluster_indexfor each CPU child when it is realized.This means that board/SoC code must add all CPUs to the cluster before realizing the cluster object. Regrettably QOM provides no way to prevent adding children to a realized object and no way for the parent to be notified when a new child is added to it, so we don't have any way to enforce/assert this constraint; all we can do is document it in a comment. We can at least put in a check that the cluster contains at least one CPU, which should catch the typical cases of "realized cluster too early" or "forgot to parent the CPUs into it".
Quoted 5 lines
The restriction on how many clusters can exist in the system is imposed by TCG code which will be added in a subsequent commit, but the check to enforce it in
cluster.cfits better in this one.Signed-off-by Peter Maydell <[email protected]>
Alistair
Quoted 173 lines
---
Changesv2->v3:
* allow CPU objects to be indirect children of the cluster; this is useful for ARMv7M, where the CPU object is a child of the armv7m container and the board code that sets up the cluster object only has the armv7m container object:
this is done by usingobject_child_foreach_recursive()rather than an open-coded child iteration
* add an assertion that the cluster has at least one CPU, which catches the easiest-to-make errors when creating and populating the cluster--- include/hw/cpu/cluster.h | 24 +++++++++++++++++++++ include/qom/cpu.h | 7 ++++++ hw/cpu/cluster.c | 46 ++++++++++++++++++++++++++++++++++++++++ qom/cpu.c | 1 + 4 files changed, 78 insertions(+)include/hw/cpu/cluster.h +24
-0@@ -34,12 +34,36 @@* Arm big.LITTLE system) they should be in different clusters. If the CPUs do * not have the same view of memory (for example the main CPU and a management * controller processor) they should be in different clusters. +* +* A cluster is created by creating an object of TYPE_CPU_CLUSTER, and then +* adding the CPUs to it as QOM child objects (e.g. using the +* object_initialize_child() or object_property_add_child() functions). +* The CPUs may be either direct children of the cluster object, or indirect +* children (e.g. children of children of the cluster object). +* +* All CPUs must be added as children before the cluster is realized. +* (Regrettably QOM provides no way to prevent adding children to a realized +* object and no way for the parent to be notified when a new child is added +* to it, so this restriction is not checked for, but the system will not +* behave correctly if it is not adhered to. The cluster will assert that +* it contains at least one CPU, which should catch most inadvertent +* violations of this constraint.) +* +* A CPU which is not put into any cluster will be considered implicitly +* to be in a cluster with all the other "loose" CPUs, so all CPUs that are +* not assigned to clusters must be identical. */ #define TYPE_CPU_CLUSTER "cpu-cluster"#define CPU_CLUSTER (obj) \ OBJECT_CHECK(CPUClusterState, (obj), TYPE_CPU_CLUSTER) +/* +* This limit is imposed by TCG, which puts the cluster ID into an +* 8 bit field (and uses all-1s for the default "not in any cluster"). +*/ +#define MAX_CLUSTERS 255 +/** * CPUClusterState: * @cluster_id: The cluster ID. This value is for internal use only and should include/qom/cpu.h +7
-0@@ -280,6 +280,11 @@ struct qemu_work_item;/** * CPUState: * @cpu_index: CPU index (informative). +* @cluster_index: Identifies which cluster this CPU is in. +* For boards which don't define clusters or for "loose" CPUs not assigned +* to a cluster this will be UNASSIGNED_CLUSTER_INDEX; otherwise it will +* be the same as the cluster-id property of the CPU object's TYPE_CPU_CLUSTER +* QOM parent. * @nr_cores: Number of cores within this CPU package. * @nr_threads: Number of threads within this CPU. * @running: #true if CPU is currently running (lockless). @@ -405,6 +410,7 @@ struct CPUState {/* TODO Move common fields from CPUArchState here. */ int cpu_index; +int cluster_index;uint32_t halted;uint32_t can_do_io;int32_t exception_index;@@ -1111,5 +1117,6 @@ extern const struct VMStateDescription vmstate_cpu_common;#endif /* NEED_CPU_H */ #define UNASSIGNED_CPU_INDEX -1 +#define UNASSIGNED_CLUSTER_INDEX -1#endif hw/cpu/cluster.c +46
-0@@ -20,19 +20,65 @@#include "qemu/osdep.h" #include "hw/cpu/cluster.h" +#include "qom/cpu.h" #include "qapi/error.h" #include "qemu/module.h" +#include "qemu/cutils.h" static Property cpu_cluster_properties[] = {DEFINE_PROP_UINT32 ("cluster-id" , CPUClusterState, cluster_id,0 ),DEFINE_PROP_END_OF_LIST () }; +typedef struct CallbackData { +CPUClusterState *cluster; +int cpu_count; +}CallbackData ; + +static int add_cpu_to_cluster (Object *obj,void *opaque) +{ +CallbackData *cbdata = opaque; +CPUState *cpu = (CPUState *)object_dynamic_cast (obj,TYPE_CPU ); + +if (cpu) { + cpu->cluster_index = cbdata->cluster->cluster_id; + cbdata->cpu_count++; + } +return 0 ; +} + +static void cpu_cluster_realize (DeviceState *dev,Error **errp) +{ +/* Iterate through all our CPU children and set their cluster_index */ +CPUClusterState *cluster =CPU_CLUSTER (dev); +Object *cluster_obj =OBJECT (dev); +CallbackData cbdata = { + .cluster = cluster, + .cpu_count =0 , + }; + +if (cluster->cluster_id >=MAX_CLUSTERS ) { +error_setg (errp,"cluster-id must be less than %d" ,MAX_CLUSTERS ); +return ; + } + +object_child_foreach_recursive (cluster_obj, add_cpu_to_cluster, &cbdata); + +/* +* A cluster with no CPUs is a bug in the board/SoC code that created it; +* if you hit this during development of new code, check that you have +* created the CPUs and parented them into the cluster object before +* realizing the cluster object. +*/ +assert (cbdata.cpu_count >0 ); +} +static void cpu_cluster_class_init (ObjectClass *klass,void *data) {DeviceClass *dc =DEVICE_CLASS (klass); dc->props = cpu_cluster_properties; + dc->realize = cpu_cluster_realize; }static const TypeInfo cpu_cluster_type_info = {qom/cpu.c +1
-0@@ -364,6 +364,7 @@ static void cpu_common_initfn(Object *obj)CPUClass *cc =CPU_GET_CLASS (obj); cpu->cpu_index =UNASSIGNED_CPU_INDEX ; + cpu->cluster_index =UNASSIGNED_CLUSTER_INDEX ; cpu->gdb_num_regs = cpu->gdb_num_g_regs = cc->gdb_num_core_regs;/* *-user doesn't have configurable SMP topology */ /* the default value is changed by qemu_init_vcpu() for softmmu */
PATCH v3 3/4 accel/tcg: Add cluster number to TCG TB hash
In reply to Peter MaydellInclude the cluster number in the hash we use to look up TBs. This is important because a TB that is valid for one cluster at a given physical address and set of CPU flags is not necessarily valid for another:
the two clusters may have different views of physical memory, or may have different CPU features (eg FPU present or absent).
We put the cluster number in the high 8 bits of the TB cflags. This gives us up to 256 clusters, which should be enough for anybody. If we ever need more, or need more bits in cflags for other purposes, we could make tb_hash_func() take more data (and expand qemu_xxhash7() to qemu_xxhash8()).
---v1->v2: move the setting of the cluster index in cf_mask in tb_htable_lookup() up to before we set desc.cf_mask from it...
--- include/exec/exec-all.h | 4 +++- accel/tcg/cpu-exec.c | 3 +++ accel/tcg/translate-all.c | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-)
include/exec/exec-all.h +3 -1
@@ -351,9 +351,11 @@ struct TranslationBlock {#define CF_USE_ICOUNT 0x00020000#define CF_INVALID 0x00040000/* TB is stale. Set with @jmp_lock held */ #define CF_PARALLEL 0x00080000/* Generate code for a parallel context */ +#define CF_CLUSTER_MASK 0xff000000/* Top 8 bits are cluster ID */ +#define CF_CLUSTER_SHIFT 24/* cflags' mask for hashing/comparison */ #define CF_HASH_MASK \ - (CF_COUNT_MASK | CF_LAST_IO | CF_USE_ICOUNT | CF_PARALLEL) + (CF_COUNT_MASK | CF_LAST_IO | CF_USE_ICOUNT | CF_PARALLEL | CF_CLUSTER_MASK)/* Per-vCPU dynamic tracing state used to generate this TB */ uint32_t trace_vcpu_dstate;
accel/tcg/cpu-exec.c +3 -0
@@ -325,6 +325,9 @@ TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc,struct tb_desc desc;uint32_t h; + cf_mask &= ~CF_CLUSTER_MASK ; + cf_mask |= cpu->cluster_index <<CF_CLUSTER_SHIFT ; + desc.env = (CPUArchState *)cpu->env_ptr; desc.cs_base = cs_base; desc.flags = flags;
accel/tcg/translate-all.c +3 -0
@@ -1688,6 +1688,9 @@ TranslationBlock *tb_gen_code(CPUState *cpu, cflags |=CF_NOCACHE |1 ; } + cflags &= ~CF_CLUSTER_MASK ; + cflags |= cpu->cluster_index <<CF_CLUSTER_SHIFT ; + buffer_overflow: tb =tb_alloc (pc);if (unlikely (!tb)) {
Signature · 1 line
2.20.1
Re: [Qemu-arm] [PATCH v3 3/4] accel/tcg: Add cluster number to TCG TB hash
Quoted 15 lines · Peter Maydell
Include the cluster number in the hash we use to look up TBs. This is important because a TB that is valid for one cluster at a given physical address and set of CPU flags is not necessarily valid for another:
the two clusters may have different views of physical memory, or may have different CPU features (eg FPU present or absent).We put the cluster number in the high 8 bits of the TB cflags. This gives us up to 256 clusters, which should be enough for anybody. If we ever need more, or need more bits in cflags for other purposes, we could make
tb_hash_func()take more data (and expandqemu_xxhash7()toqemu_xxhash8()).
Quoted 2 lines
Signed-off-by Peter Maydell <[email protected]>Reviewed-by Richard Henderson <[email protected]>
Quoted 58 lines
---
v1->v2: move the setting of the cluster index incf_maskintb_htable_lookup()up to before we set desc.cf_mask from it...--- include/exec/exec-all.h | 4 +++- accel/tcg/cpu-exec.c | 3 +++ accel/tcg/translate-all.c | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-)include/exec/exec-all.h +3
-1@@ -351,9 +351,11 @@ struct TranslationBlock {#define CF_USE_ICOUNT 0x00020000#define CF_INVALID 0x00040000/* TB is stale. Set with @jmp_lock held */ #define CF_PARALLEL 0x00080000/* Generate code for a parallel context */ +#define CF_CLUSTER_MASK 0xff000000/* Top 8 bits are cluster ID */ +#define CF_CLUSTER_SHIFT 24/* cflags' mask for hashing/comparison */ #define CF_HASH_MASK \ - (CF_COUNT_MASK | CF_LAST_IO | CF_USE_ICOUNT | CF_PARALLEL) + (CF_COUNT_MASK | CF_LAST_IO | CF_USE_ICOUNT | CF_PARALLEL | CF_CLUSTER_MASK)/* Per-vCPU dynamic tracing state used to generate this TB */ uint32_t trace_vcpu_dstate;accel/tcg/cpu-exec.c +3
-0@@ -325,6 +325,9 @@ TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc,struct tb_desc desc;uint32_t h; + cf_mask &= ~CF_CLUSTER_MASK ; + cf_mask |= cpu->cluster_index <<CF_CLUSTER_SHIFT ; + desc.env = (CPUArchState *)cpu->env_ptr; desc.cs_base = cs_base; desc.flags = flags;accel/tcg/translate-all.c +3
-0@@ -1688,6 +1688,9 @@ TranslationBlock *tb_gen_code(CPUState *cpu, cflags |=CF_NOCACHE |1 ; } + cflags &= ~CF_CLUSTER_MASK ; + cflags |= cpu->cluster_index <<CF_CLUSTER_SHIFT ; + buffer_overflow: tb =tb_alloc (pc);if (unlikely (!tb)) {Signature · 1 line
2.20.1
Re: [Qemu-arm] [Qemu-devel] [PATCH v3 3/4] accel/tcg: Add cluster number to TCG TB hash
In reply to Peter MaydellQuoted 15 lines · Peter Maydell
Include the cluster number in the hash we use to look up TBs. This is important because a TB that is valid for one cluster at a given physical address and set of CPU flags is not necessarily valid for another:
the two clusters may have different views of physical memory, or may have different CPU features (eg FPU present or absent).We put the cluster number in the high 8 bits of the TB cflags. This gives us up to 256 clusters, which should be enough for anybody. If we ever need more, or need more bits in cflags for other purposes, we could make
tb_hash_func()take more data (and expandqemu_xxhash7()toqemu_xxhash8()).
Quoted 2 lines
Signed-off-by Peter Maydell <[email protected]>Reviewed-by Richard Henderson <[email protected]>
Alistair
Quoted 56 lines
---
v1->v2: move the setting of the cluster index incf_maskintb_htable_lookup()up to before we set desc.cf_mask from it...--- include/exec/exec-all.h | 4 +++- accel/tcg/cpu-exec.c | 3 +++ accel/tcg/translate-all.c | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-)include/exec/exec-all.h +3
-1@@ -351,9 +351,11 @@ struct TranslationBlock {#define CF_USE_ICOUNT 0x00020000#define CF_INVALID 0x00040000/* TB is stale. Set with @jmp_lock held */ #define CF_PARALLEL 0x00080000/* Generate code for a parallel context */ +#define CF_CLUSTER_MASK 0xff000000/* Top 8 bits are cluster ID */ +#define CF_CLUSTER_SHIFT 24/* cflags' mask for hashing/comparison */ #define CF_HASH_MASK \ - (CF_COUNT_MASK | CF_LAST_IO | CF_USE_ICOUNT | CF_PARALLEL) + (CF_COUNT_MASK | CF_LAST_IO | CF_USE_ICOUNT | CF_PARALLEL | CF_CLUSTER_MASK)/* Per-vCPU dynamic tracing state used to generate this TB */ uint32_t trace_vcpu_dstate;accel/tcg/cpu-exec.c +3
-0@@ -325,6 +325,9 @@ TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc,struct tb_desc desc;uint32_t h; + cf_mask &= ~CF_CLUSTER_MASK ; + cf_mask |= cpu->cluster_index <<CF_CLUSTER_SHIFT ; + desc.env = (CPUArchState *)cpu->env_ptr; desc.cs_base = cs_base; desc.flags = flags;accel/tcg/translate-all.c +3
-0@@ -1688,6 +1688,9 @@ TranslationBlock *tb_gen_code(CPUState *cpu, cflags |=CF_NOCACHE |1 ; } + cflags &= ~CF_CLUSTER_MASK ; + cflags |= cpu->cluster_index <<CF_CLUSTER_SHIFT ; + buffer_overflow: tb =tb_alloc (pc);if (unlikely (!tb)) {
PATCH v3 4/4 gdbstub: Simplify gdb_get_cpu_pid() to use cpu->cluster_index
In reply to Peter MaydellNow we're keeping the cluster index in the CPUState, we don't need to jump through hoops in gdb_get_cpu_pid() to find the associated cluster object.
--- gdbstub.c | 48 +++++------------------------------------------- 1 file changed, 5 insertions(+), 43 deletions(-)
gdbstub.c +5 -43
@@ -644,50 +644,12 @@ static int memtox(char *buf, const char *mem, int len)static uint32_t gdb_get_cpu_pid (const GDBState *s,CPUState *cpu) { -#ifndef CONFIG_USER_ONLY -gchar *path, *name =NULL ; -Object *obj; -CPUClusterState *cluster; -uint32_t ret; - - path =object_get_canonical_path (OBJECT (cpu)); - -if (path ==NULL ) { -/* Return the default process' PID */ - ret = s->processes[s->process_num -1 ].pid; - goto out; - } - - name =object_get_canonical_path_component (OBJECT (cpu)); -assert (name !=NULL ); - -/* -* Retrieve the CPU parent path by removing the last '/' and the CPU name -* from the CPU canonical path. -*/ - path[strlen (path) -strlen (name) -1 ] ='\0' ; - - obj =object_resolve_path_type (path,TYPE_CPU_CLUSTER ,NULL ); - -if (obj ==NULL ) { -/* Return the default process' PID */ - ret = s->processes[s->process_num -1 ].pid; - goto out; - } - - cluster =CPU_CLUSTER (obj); - ret = cluster->cluster_id +1 ; - -out: -g_free (name); -g_free (path); - -return ret; - -#else /* TODO: In user mode, we should use the task state PID */ -return s->processes[s->process_num -1 ].pid; -#endif +if (cpu->cluster_index ==UNASSIGNED_CLUSTER_INDEX ) { +/* Return the default process' PID */ +return s->processes[s->process_num -1 ].pid; + } +return cpu->cluster_index +1 ; }static GDBProcess *gdb_get_process (const GDBState *s,uint32_t pid)
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH v3 4/4] gdbstub: Simplify gdb_get_cpu_pid() to use cpu->cluster_index
Quoted 6 lines · Peter Maydell
Now we're keeping the cluster index in the CPUState, we don't need to jump through hoops in
gdb_get_cpu_pid()to find the associated cluster object.Signed-off-by Peter Maydell <[email protected]>Reviewed-by Luc Michel <[email protected]>
Quoted 67 lines
--- gdbstub.c | 48 +++++------------------------------------------- 1 file changed, 5 insertions(+), 43 deletions(-)gdbstub.c +5
-43@@ -644,50 +644,12 @@ static int memtox(char *buf, const char *mem, int len)static uint32_t gdb_get_cpu_pid (const GDBState *s,CPUState *cpu) { -#ifndef CONFIG_USER_ONLY -gchar *path, *name =NULL ; -Object *obj; -CPUClusterState *cluster; -uint32_t ret; - - path =object_get_canonical_path (OBJECT (cpu)); - -if (path ==NULL ) { -/* Return the default process' PID */ - ret = s->processes[s->process_num -1 ].pid; - goto out; - } - - name =object_get_canonical_path_component (OBJECT (cpu)); -assert (name !=NULL ); - -/* -* Retrieve the CPU parent path by removing the last '/' and the CPU name -* from the CPU canonical path. -*/ - path[strlen (path) -strlen (name) -1 ] ='\0' ; - - obj =object_resolve_path_type (path,TYPE_CPU_CLUSTER ,NULL ); - -if (obj ==NULL ) { -/* Return the default process' PID */ - ret = s->processes[s->process_num -1 ].pid; - goto out; - } - - cluster =CPU_CLUSTER (obj); - ret = cluster->cluster_id +1 ; - -out: -g_free (name); -g_free (path); - -return ret; - -#else /* TODO: In user mode, we should use the task state PID */ -return s->processes[s->process_num -1 ].pid; -#endif +if (cpu->cluster_index ==UNASSIGNED_CLUSTER_INDEX ) { +/* Return the default process' PID */ +return s->processes[s->process_num -1 ].pid; + } +return cpu->cluster_index +1 ; }static GDBProcess *gdb_get_process (const GDBState *s,uint32_t pid)Signature · 1 line
2.20.1
Quoted 13 lines · Peter Maydell
In the process of debugging my board code that uses this feature, I realized I needed to support the CPU object not being a direct child of the cluster object, which also lead me to the
object_child_foreach_recursive()function which is a neater way to iterate children anyway. I also figured out we could add an assert which catches most "parenting CPU vs cluster realize ordering" bugs.Only patch 2 has changed here, others are already reviewed.
Changes
v2->v3:
Quoted 9 lines
* allow CPU objects to be indirect children of the cluster; this is useful for ARMv7M, where the CPU object is a child of the armv7m container and the board code that sets up the cluster object only has the armv7m container object:
this is done by usingobject_child_foreach_recursive()rather than an open-coded child iteration
* add an assertion that the cluster has at least one CPU, which catches the easiest-to-make errors when creating and populating the cluster
Oops, I should have tested this a bit more carefully before sending it out. It needs an extra "patch 0" adding at the start which makes xlnx_zynqmp_create_rpu() not create the rpu-cluster object if there aren't any RPUs to put in it. Otherwise we hit the new assertion when starting the xlnx-zcu102 board with fewer than 5 CPUs. I'll send that patch out in a second (but won't bother to re-roll the entire patchset).
thanks
-- PMM