mbox series

[00/24] exec: Rework around CPUState user fields (part 2)

Message ID 20240428221450.26460-1-philmd@linaro.org (mailing list archive)
Headers show
Series exec: Rework around CPUState user fields (part 2) | expand

Message

Philippe Mathieu-Daudé April 28, 2024, 10:14 p.m. UTC
Finish extracting TCG fields from CPUState:
- Extract tcg_cpu_exit() from cpu_exit()
- Introduce AccelOpsClass::exit_vcpu_thread()
- cpu_exit() calls exit_vcpu_thread=tcg_cpu_exit for TCG
- Forward declare TaskState and more uses of get_task_state()
- Introduce TCG AccelCPUState
- Move TCG specific fields from CPUState to AccelCPUState
- Restrict "exec/tlb-common.h" to TCG
- Restrict iommu_notifiers, icount to system emulation

Based-on: <20240428214915.10339-1-philmd@linaro.org>

Philippe Mathieu-Daudé (24):
  exec/user: Move 'thunk.h' from 'exec/user' to 'user'
  coverity: Update user emulation regexp
  accel/tcg: Move user definition of cpu_interrupt() to user-exec.c
  accel/tcg: Duplicate cpu_exit() for user / system
  accel/tcg: Extract tcg_cpu_exit() from cpu_exit()
  accel: Introduce AccelOpsClass::exit_vcpu_thread() handler
  accel/tcg: Implement AccelOpsClass::exit_vcpu_thread() handler
  user: Forward declare TaskState type definition
  user: Declare get_task_state() once in 'accel/tcg/vcpu-state.h'
  user: Use get_task_state() helper
  accel/tcg: Allocate per-vCPU accel state in create_vcpu_thread()
  accel/tcg: Move TaskState from CPUState to TCG AccelCPUState
  accel/tcg: Update CPUNegativeOffsetState::can_do_io field
    documentation
  accel/tcg: Move plugin fields to CPUNegativeOffsetState
  accel/tcg: Restrict IcountDecr and CPUTLB to TCG
  accel/tcg: Move @jmp_env from CPUState to TCG AccelCPUState
  accel/tcg: Move @mem_io_pc from CPUState to TCG AccelCPUState
  accel/tcg: Move @cflags_next_tb from CPUState to TCG AccelCPUState
  accel/tcg: Move @iommu_notifiers from CPUState to TCG AccelCPUState
  accel/tcg: Move @tb_jmp_cache from CPUState to TCG AccelCPUState
  accel/tcg: Remove NULL check in tcg_flush_jmp_cache()
  accel/tcg: Move @tcg_cflags from CPUState to TCG AccelCPUState
  accel/tcg: Restrict icount to system emulation
  accel/tcg: Move icount fields from CPUState to TCG AccelCPUState

 accel/tcg/internal-common.h         |  3 ++
 accel/tcg/tb-jmp-cache.h            |  4 +-
 accel/tcg/tcg-accel-ops.h           |  1 +
 accel/tcg/vcpu-state.h              | 45 +++++++++++++++++++++++
 bsd-user/qemu.h                     | 12 ++----
 include/exec/tlb-common.h           |  4 ++
 include/hw/core/cpu.h               | 57 +++++++++++------------------
 include/qemu/plugin.h               |  2 +-
 include/qemu/typedefs.h             |  2 +-
 include/sysemu/accel-ops.h          |  1 +
 include/{exec => }/user/thunk.h     |  8 +++-
 linux-user/qemu.h                   | 10 ++---
 linux-user/user-internals.h         |  2 +-
 accel/tcg/cpu-exec-common.c         |  2 +-
 accel/tcg/cpu-exec.c                | 51 ++++++++++++++------------
 accel/tcg/cputlb.c                  |  4 +-
 accel/tcg/icount-common.c           |  7 ++--
 accel/tcg/plugin-gen.c              |  8 ++--
 accel/tcg/tb-maint.c                |  6 +--
 accel/tcg/tcg-accel-ops-icount.c    | 14 +++----
 accel/tcg/tcg-accel-ops-mttcg.c     |  2 +
 accel/tcg/tcg-accel-ops-rr.c        |  2 +
 accel/tcg/tcg-accel-ops.c           |  5 +++
 accel/tcg/translate-all.c           | 25 +++++--------
 accel/tcg/user-exec.c               | 14 +++++++
 accel/tcg/watchpoint.c              |  5 ++-
 bsd-user/main.c                     |  2 +-
 gdbstub/gdbstub.c                   |  3 +-
 gdbstub/user-target.c               |  4 +-
 hw/core/cpu-common.c                | 13 +------
 hw/misc/mips_itu.c                  |  3 +-
 linux-user/main.c                   |  4 +-
 linux-user/syscall.c                |  6 +--
 linux-user/thunk.c                  |  2 +-
 plugins/core.c                      |  8 ++--
 system/cpus.c                       |  8 ++++
 system/physmem.c                    | 37 ++++++++++++++-----
 target/i386/helper.c                |  3 +-
 scripts/coverity-scan/COMPONENTS.md |  2 +-
 39 files changed, 235 insertions(+), 156 deletions(-)
 create mode 100644 accel/tcg/vcpu-state.h
 rename include/{exec => }/user/thunk.h (97%)

Comments

Philippe Mathieu-Daudé April 28, 2024, 10:22 p.m. UTC | #1
On 29/4/24 00:14, Philippe Mathieu-Daudé wrote:
> Finish extracting TCG fields from CPUState:
> - Extract tcg_cpu_exit() from cpu_exit()
> - Introduce AccelOpsClass::exit_vcpu_thread()
> - cpu_exit() calls exit_vcpu_thread=tcg_cpu_exit for TCG
> - Forward declare TaskState and more uses of get_task_state()
> - Introduce TCG AccelCPUState
> - Move TCG specific fields from CPUState to AccelCPUState
> - Restrict "exec/tlb-common.h" to TCG
> - Restrict iommu_notifiers, icount to system emulation
> 
> Based-on: <20240428214915.10339-1-philmd@linaro.org>

The CPUState changes (part 1 & 2) can be resumed as:

$ git diff master.. -- include/hw/core/cpu.h accel/tcg/vcpu-state.h
-- >8 --
diff --git a/accel/tcg/vcpu-state.h b/accel/tcg/vcpu-state.h
new file mode 100644
index 0000000000..9bb8afac57
--- /dev/null
+++ b/accel/tcg/vcpu-state.h
@@ -0,0 +1,45 @@
+/**
+ * AccelCPUState:
+ * @cflags: Pre-computed cflags for this cpu.
+ * @icount_extra: Instructions until next timer event.
+ * @mem_io_pc: Host Program Counter at which the memory was accessed.
+ */
+struct AccelCPUState {
+    uint32_t cflags;
+    uint32_t cflags_next_tb;
+
+    sigjmp_buf jmp_env;
+    CPUJumpCache tb_jmp_cache;
+
+#ifdef CONFIG_USER_ONLY
+    TaskState *ts;
+#else
+    int64_t icount_budget;
+    int64_t icount_extra;
+
+    uintptr_t mem_io_pc;
+
+    /* track IOMMUs whose translations we've cached in the TCG TLB */
+    GArray *iommu_notifiers;
+#endif
+};
+#endif
diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
index 46b99a7ea5..bdcb09b464 100644
--- a/include/hw/core/cpu.h
+++ b/include/hw/core/cpu.h
@@ -337,15 +337,28 @@ typedef union IcountDecr {
-/*
- * Elements of CPUState most efficiently accessed from CPUArchState,
- * via small negative offsets.
+/**
+ * CPUNegativeOffsetState: Elements of CPUState most efficiently accessed
+ *                         from CPUArchState, via small negative offsets.
+ * @can_do_io: True if memory-mapped IO is allowed.
+ * @plugin_mem_cbs: active plugin memory callbacks
+ * @plugin_state: per-CPU plugin state
   */
  typedef struct CPUNegativeOffsetState {
+#ifdef CONFIG_TCG
      CPUTLB tlb;
+#ifdef CONFIG_PLUGIN
+    /*
+     * The callback pointer are accessed via TCG (see 
gen_empty_mem_helper).
+     */
+    GArray *plugin_mem_cbs;
+    CPUPluginState *plugin_state;
+#endif
      IcountDecr icount_decr;
      bool can_do_io;
+#endif
  } CPUNegativeOffsetState;

  struct KVMState;
@@ -383,9 +396,8 @@ struct qemu_work_item;
   *   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.
- *   Under TCG this value is propagated to @tcg_cflags.
+ *   Under TCG this value is propagated to @accel->cflags.
   *   See TranslationBlock::TCG CF_CLUSTER_MASK.
- * @tcg_cflags: Pre-computed cflags for this cpu.
   * @nr_cores: Number of cores within this CPU package.
   * @nr_threads: Number of threads within this CPU core.
   * @running: #true if CPU is currently running (lockless).
@@ -399,8 +411,6 @@ struct qemu_work_item;
   * @unplug: Indicates a pending CPU unplug request.
   * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
   * @singlestep_enabled: Flags for single-stepping.
- * @icount_extra: Instructions until next timer event.
- * @neg.can_do_io: True if memory-mapped IO is allowed.
   * @cpu_ases: Pointer to array of CPUAddressSpaces (which define the
   *            AddressSpaces this CPU has)
   * @num_ases: number of CPUAddressSpaces in @cpu_ases
@@ -411,13 +421,10 @@ struct qemu_work_item;
   * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
   * @node: QTAILQ of CPUs sharing TB cache.
   * @opaque: User data.
- * @mem_io_pc: Host Program Counter at which the memory was accessed.
   * @accel: Pointer to accelerator specific state.
   * @kvm_fd: vCPU file descriptor for KVM.
   * @work_mutex: Lock to prevent multiple access to @work_list.
   * @work_list: List of pending asynchronous work.
- * @plugin_mem_cbs: active plugin memory callbacks
- * @plugin_state: per-CPU plugin state
   * @ignore_memory_transaction_failures: Cached copy of the MachineState
   *    flag of the same name: allows the board to suppress calling of the
   *    CPU do_transaction_failed hook function.
@@ -460,14 +467,10 @@ struct CPUState {
      bool crash_occurred;
      bool exit_request;
      int exclusive_context_count;
-    uint32_t cflags_next_tb;
      /* updates protected by BQL */
      uint32_t interrupt_request;
      int singlestep_enabled;
-    int64_t icount_budget;
-    int64_t icount_extra;
      uint64_t random_seed;
-    sigjmp_buf jmp_env;

      QemuMutex work_mutex;
      QSIMPLEQ_HEAD(, qemu_work_item) work_list;
@@ -477,8 +480,6 @@ struct CPUState {
      AddressSpace *as;
      MemoryRegion *memory;

-    CPUJumpCache *tb_jmp_cache;
-
      GArray *gdb_regs;
      int gdb_num_regs;
      int gdb_num_g_regs;
@@ -490,12 +491,9 @@ struct CPUState {
      QTAILQ_HEAD(, CPUWatchpoint) watchpoints;
      CPUWatchpoint *watchpoint_hit;

-    void *opaque;
-
      /* In order to avoid passing too many arguments to the MMIO helpers,
       * we store some rarely used information in the CPU context.
       */
-    uintptr_t mem_io_pc;

      /* Only used in KVM */
      int kvm_fd;
@@ -510,19 +508,9 @@ struct CPUState {
      /* Use by accel-block: CPU is executing an ioctl() */
      QemuLockCnt in_ioctl_lock;

-#ifdef CONFIG_PLUGIN
-    /*
-     * The callback pointer stays in the main CPUState as it is
-     * accessed via TCG (see gen_empty_mem_helper).
-     */
-    GArray *plugin_mem_cbs;
-    CPUPluginState *plugin_state;
-#endif
-
      /* TODO Move common fields from CPUArchState here. */
      int cpu_index;
      int cluster_index;
-    uint32_t tcg_cflags;
      uint32_t halted;
      int32_t exception_index;

@@ -544,9 +532,6 @@ struct CPUState {
      /* Used for user-only emulation of prctl(PR_SET_UNALIGN). */
      bool prctl_unalign_sigbus;

-    /* 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.
       */
---
Philippe Mathieu-Daudé April 29, 2024, 9:04 p.m. UTC | #2
On 29/4/24 00:14, Philippe Mathieu-Daudé wrote:

> Philippe Mathieu-Daudé (24):
>    exec/user: Move 'thunk.h' from 'exec/user' to 'user'
>    coverity: Update user emulation regexp
>    accel/tcg: Move user definition of cpu_interrupt() to user-exec.c
>    accel/tcg: Duplicate cpu_exit() for user / system
>    accel/tcg: Extract tcg_cpu_exit() from cpu_exit()
>    accel: Introduce AccelOpsClass::exit_vcpu_thread() handler
>    accel/tcg: Implement AccelOpsClass::exit_vcpu_thread() handler
>    user: Forward declare TaskState type definition
>    user: Declare get_task_state() once in 'accel/tcg/vcpu-state.h'
>    user: Use get_task_state() helper
>    accel/tcg: Allocate per-vCPU accel state in create_vcpu_thread()
>    accel/tcg: Move TaskState from CPUState to TCG AccelCPUState
>    accel/tcg: Update CPUNegativeOffsetState::can_do_io field
>      documentation

Queuing patches 1-13, thanks!