accel/tcg: Move CPUNegativeOffsetState into CPUState
authorRichard Henderson <richard.henderson@linaro.org>
Wed, 13 Sep 2023 00:47:56 +0000 (17:47 -0700)
committerRichard Henderson <richard.henderson@linaro.org>
Tue, 3 Oct 2023 15:01:02 +0000 (08:01 -0700)
Retain the separate structure to emphasize its importance.
Enforce CPUArchState always follows CPUState without padding.

Reviewed-by: Anton Johansson <anjo@rev.ng>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
25 files changed:
accel/tcg/translate-all.c
accel/tcg/translator.c
include/exec/cpu-all.h
include/hw/core/cpu.h
target/alpha/cpu.h
target/arm/cpu.h
target/avr/cpu.h
target/cris/cpu.h
target/hexagon/cpu.h
target/hppa/cpu.h
target/i386/cpu.h
target/loongarch/cpu.h
target/m68k/cpu.h
target/microblaze/cpu.h
target/mips/cpu.h
target/nios2/cpu.h
target/openrisc/cpu.h
target/ppc/cpu.h
target/riscv/cpu.h
target/rx/cpu.h
target/s390x/cpu.h
target/sh4/cpu.h
target/sparc/cpu.h
target/tricore/cpu.h
target/xtensa/cpu.h

index b2d4e22c17d44e3ab47d5c2edbc2854fef9565a9..098d99b5d47b8428320e57c02ff7078b31907bc9 100644 (file)
@@ -344,8 +344,8 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
     tcg_ctx->page_bits = TARGET_PAGE_BITS;
     tcg_ctx->page_mask = TARGET_PAGE_MASK;
     tcg_ctx->tlb_dyn_max_bits = CPU_TLB_DYN_MAX_BITS;
-    tcg_ctx->tlb_fast_offset =
-        (int)offsetof(ArchCPU, neg.tlb.f) - (int)offsetof(ArchCPU, env);
+    tcg_ctx->tlb_fast_offset = (int)offsetof(ArchCPU, parent_obj.neg.tlb.f)
+                             - (int)offsetof(ArchCPU, env);
 #endif
     tcg_ctx->insn_start_words = TARGET_INSN_START_WORDS;
 #ifdef TCG_GUEST_DEFAULT_MO
index 358214d5265ef42f0bdcf39eaace9ca518dfd51b..b3e12d61e998784f0fcaf76cdfd424d373b71168 100644 (file)
@@ -48,8 +48,8 @@ static TCGOp *gen_tb_start(DisasContextBase *db, uint32_t cflags)
     if ((cflags & CF_USE_ICOUNT) || !(cflags & CF_NOIRQ)) {
         count = tcg_temp_new_i32();
         tcg_gen_ld_i32(count, cpu_env,
-                       offsetof(ArchCPU, neg.icount_decr.u32) -
-                       offsetof(ArchCPU, env));
+                       offsetof(ArchCPU, parent_obj.neg.icount_decr.u32)
+                       offsetof(ArchCPU, env));
     }
 
     if (cflags & CF_USE_ICOUNT) {
@@ -78,8 +78,8 @@ static TCGOp *gen_tb_start(DisasContextBase *db, uint32_t cflags)
 
     if (cflags & CF_USE_ICOUNT) {
         tcg_gen_st16_i32(count, cpu_env,
-                         offsetof(ArchCPU, neg.icount_decr.u16.low) -
-                         offsetof(ArchCPU, env));
+                         offsetof(ArchCPU, parent_obj.neg.icount_decr.u16.low)
+                         offsetof(ArchCPU, env));
     }
 
     /*
index ed7747a5f1660a86b61e61762211e90f437771a0..0dd32cb0e99ddfbbd94a0e0b8f42dc01d8ef081f 100644 (file)
@@ -432,9 +432,13 @@ int cpu_exec(CPUState *cpu);
 static inline void cpu_set_cpustate_pointers(ArchCPU *cpu)
 {
     cpu->parent_obj.env_ptr = &cpu->env;
-    cpu->parent_obj.icount_decr_ptr = &cpu->neg.icount_decr;
+    cpu->parent_obj.icount_decr_ptr = &cpu->parent_obj.neg.icount_decr;
 }
 
+/* Validate correct placement of CPUArchState. */
+QEMU_BUILD_BUG_ON(offsetof(ArchCPU, parent_obj) != 0);
+QEMU_BUILD_BUG_ON(offsetof(ArchCPU, env) != sizeof(CPUState));
+
 /**
  * env_archcpu(env)
  * @env: The architecture environment
@@ -443,7 +447,7 @@ static inline void cpu_set_cpustate_pointers(ArchCPU *cpu)
  */
 static inline ArchCPU *env_archcpu(CPUArchState *env)
 {
-    return container_of(env, ArchCPU, env);
+    return (void *)env - sizeof(CPUState);
 }
 
 /**
@@ -454,15 +458,9 @@ static inline ArchCPU *env_archcpu(CPUArchState *env)
  */
 static inline CPUState *env_cpu(CPUArchState *env)
 {
-    return &env_archcpu(env)->parent_obj;
+    return (void *)env - sizeof(CPUState);
 }
 
-/*
- * Validate placement of CPUNegativeOffsetState.
- */
-QEMU_BUILD_BUG_ON(offsetof(ArchCPU, env) - offsetof(ArchCPU, neg) >=
-                  sizeof(CPUNegativeOffsetState) + __alignof(CPUArchState));
-
 /**
  * env_neg(env)
  * @env: The architecture environment
@@ -471,8 +469,7 @@ QEMU_BUILD_BUG_ON(offsetof(ArchCPU, env) - offsetof(ArchCPU, neg) >=
  */
 static inline CPUNegativeOffsetState *env_neg(CPUArchState *env)
 {
-    ArchCPU *arch_cpu = container_of(env, ArchCPU, env);
-    return &arch_cpu->neg;
+    return &env_cpu(env)->neg;
 }
 
 /**
@@ -483,8 +480,7 @@ static inline CPUNegativeOffsetState *env_neg(CPUArchState *env)
  */
 static inline CPUNegativeOffsetState *cpu_neg(CPUState *cpu)
 {
-    ArchCPU *arch_cpu = container_of(cpu, ArchCPU, parent_obj);
-    return &arch_cpu->neg;
+    return &cpu->neg;
 }
 
 /**
index 04baa5063cff1d6eb5671f499daac604061c8517..115ddf6d8ac4284c32f23b04a60747db704df3a6 100644 (file)
@@ -345,8 +345,8 @@ typedef union IcountDecr {
 } IcountDecr;
 
 /*
- * This structure must be placed in ArchCPU immediately
- * before CPUArchState, as a field named "neg".
+ * Elements of CPUState most efficiently accessed from CPUArchState,
+ * via small negative offsets.
  */
 typedef struct CPUNegativeOffsetState {
     CPUTLB tlb;
@@ -453,6 +453,9 @@ struct qemu_work_item;
  *    dirty ring structure.
  *
  * State of one CPU core or thread.
+ *
+ * Align, in order to match possible alignment required by CPUArchState,
+ * and eliminate a hole between CPUState and CPUArchState within ArchCPU.
  */
 struct CPUState {
     /*< private >*/
@@ -571,8 +574,18 @@ struct CPUState {
 
     /* track IOMMUs whose translations we've cached in the TCG TLB */
     GArray *iommu_notifiers;
+
+    /*
+     * MUST BE LAST in order to minimize the displacement to CPUArchState.
+     */
+    char neg_align[-sizeof(CPUNegativeOffsetState) % 16] QEMU_ALIGNED(16);
+    CPUNegativeOffsetState neg;
 };
 
+/* Validate placement of CPUNegativeOffsetState. */
+QEMU_BUILD_BUG_ON(offsetof(CPUState, neg) !=
+                  sizeof(CPUState) - sizeof(CPUNegativeOffsetState));
+
 typedef QTAILQ_HEAD(CPUTailQ, CPUState) CPUTailQ;
 extern CPUTailQ cpus;
 
index 13306665aff1360e2b6dbf9d5835785d064530a7..e2a467ec1728ef0b30c86684dc1440c8eb449cb2 100644 (file)
@@ -263,7 +263,6 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
-    CPUNegativeOffsetState neg;
     CPUAlphaState env;
 
     /* This alarm doesn't exist in real hardware; we wish it did.  */
index bd55c5dabfd3f1e49fd3b4165212bb24a4e49e7f..a9edfb8353e0a4718699e11489751bcdab2b35cf 100644 (file)
@@ -856,7 +856,6 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
-    CPUNegativeOffsetState neg;
     CPUARMState env;
 
     /* Coprocessor information */
index 722517466818097033ea5bd69818ff667a50f634..4ce22d8e4f176aa58ed03520da6685d183370cee 100644 (file)
@@ -148,7 +148,6 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
-    CPUNegativeOffsetState neg;
     CPUAVRState env;
 };
 
index 8e37c6e50d1696e06f7c4531f25aa9223cfd351d..676b8e93ca89dad7cd5632f2a1dc324a67d5d45c 100644 (file)
@@ -178,7 +178,6 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
-    CPUNegativeOffsetState neg;
     CPUCRISState env;
 };
 
index daef5c3f006fa3f34d3822a43641a63e13fdfaf3..10cd1efd57f8f9b6316cdf7130a8da4c49522710 100644 (file)
@@ -141,7 +141,7 @@ struct ArchCPU {
     /*< private >*/
     CPUState parent_obj;
     /*< public >*/
-    CPUNegativeOffsetState neg;
+
     CPUHexagonState env;
 
     bool lldb_compat;
index 730f35231af87168b1845514c366bc50ca902af6..798d0c26d754a0ddb888f9fbed6b2f8f1a00ee78 100644 (file)
@@ -237,7 +237,6 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
-    CPUNegativeOffsetState neg;
     CPUHPPAState env;
     QEMUTimer *alarm_timer;
 };
index d3f377d48a53efb56f7c18fb0699eb6599b851ac..e1875466b9ddb2c6e32f8baf8dd6c6229fa95795 100644 (file)
@@ -1901,7 +1901,6 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
-    CPUNegativeOffsetState neg;
     CPUX86State env;
     VMChangeStateEntry *vmsentry;
 
index f125a8e49b9ccfa95eb538232fb884c3f257e018..40e70a8119f23d7a172a3e8bfa54bd05befd6ecd 100644 (file)
@@ -375,7 +375,6 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
-    CPUNegativeOffsetState neg;
     CPULoongArchState env;
     QEMUTimer timer;
     uint32_t  phy_id;
index cf70282717189f89ec26037181606401f960f62e..20afb0c94d9a203ab2e9f286c3316eb499a32c3f 100644 (file)
@@ -168,7 +168,6 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
-    CPUNegativeOffsetState neg;
     CPUM68KState env;
 };
 
index f6cab6ce1919dc2f5bbf1707ed5be491a2ea0b7e..e43c49d4af8ac9ab539241ae9d54de26ca82b6cc 100644 (file)
@@ -345,15 +345,15 @@ typedef struct {
 struct ArchCPU {
     /*< private >*/
     CPUState parent_obj;
-
     /*< public >*/
+
+    CPUMBState env;
+
     bool ns_axi_dp;
     bool ns_axi_ip;
     bool ns_axi_dc;
     bool ns_axi_ic;
 
-    CPUNegativeOffsetState neg;
-    CPUMBState env;
     MicroBlazeCPUConfig cfg;
 };
 
index 6d6af1f2a86ce08be299f04baff2e419f2658563..67f8e8b988c020105cb31600ca05a2671c2daf59 100644 (file)
@@ -1213,10 +1213,10 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
+    CPUMIPSState env;
+
     Clock *clock;
     Clock *count_div; /* Divider for CP0_Count clock */
-    CPUNegativeOffsetState neg;
-    CPUMIPSState env;
 };
 
 
index 477a3161fde5d171674c04a0af92c6d249464647..70b6377a4f8c947fd4685e92811ad566fd481c23 100644 (file)
@@ -218,7 +218,6 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
-    CPUNegativeOffsetState neg;
     CPUNios2State env;
 
     bool diverr_present;
index ce4d605eb754bf77ae509f7b5f207d058c9e01df..334997e9a103d016c90b33505d9ff0312bdb8689 100644 (file)
@@ -305,7 +305,6 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
-    CPUNegativeOffsetState neg;
     CPUOpenRISCState env;
 };
 
index d703a5f3c6f8d8565ef715bd6f17e98261f57246..30392ebeeef8f0c4bb9fcd79a85aedeea8916071 100644 (file)
@@ -1317,7 +1317,6 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
-    CPUNegativeOffsetState neg;
     CPUPPCState env;
 
     int vcpu_id;
index 6316cbcc230ed8504bf3ed57ab801cbf19a8fdc4..ef9cf21c0c9318884e2824d57a0b335e2f79f643 100644 (file)
@@ -388,7 +388,7 @@ struct ArchCPU {
     /* < private > */
     CPUState parent_obj;
     /* < public > */
-    CPUNegativeOffsetState neg;
+
     CPURISCVState env;
 
     char *dyn_csr_xml;
index 7f03ffcfed23955236be04339734e8fb190fec6d..f66754eb8af29bf96acf2340f47de81c7cdfc76c 100644 (file)
@@ -111,7 +111,6 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
-    CPUNegativeOffsetState neg;
     CPURXState env;
 };
 
index 304029e57cfb251e5f25529000e1fbaedbbf5c04..7bea7075e145913daae929e9c848dc63f73bc1b4 100644 (file)
@@ -170,7 +170,6 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
-    CPUNegativeOffsetState neg;
     CPUS390XState env;
     S390CPUModel *model;
     /* needed for live migration */
index 1399d3840fd0b4561d51855ce577f0900ffb4981..f75a235973d8a14837e4c5d0c10cc3149f43c39a 100644 (file)
@@ -208,7 +208,6 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
-    CPUNegativeOffsetState neg;
     CPUSH4State env;
 };
 
index 98044572f26458087260eacee72e7d90f935bd56..b3a98f1d74cc39db0b127cb1fc77c24d124a24be 100644 (file)
@@ -561,7 +561,6 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
-    CPUNegativeOffsetState neg;
     CPUSPARCState env;
 };
 
index 1cace96b0136b741641a51b651ffdb8b69175201..a357b573f28132e454e0e8293469862913a8d57c 100644 (file)
@@ -67,7 +67,6 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
-    CPUNegativeOffsetState neg;
     CPUTriCoreState env;
 };
 
index 87fe992ba68c726d574449f8acfbc2b410aaea93..c6bbef1e5da9b2f45176e8d2225485c19bd4e54d 100644 (file)
@@ -560,9 +560,8 @@ struct ArchCPU {
     CPUState parent_obj;
     /*< public >*/
 
-    Clock *clock;
-    CPUNegativeOffsetState neg;
     CPUXtensaState env;
+    Clock *clock;
 };