Skip to contentSkip to conversation

i386 cleanup

RFC Superseded Claudio Fontana 5 participants 41 messages Nov 18, 2020 – Nov 23, 2020 Arm/Amlogic Meson SoC support

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v3 cover i386 cleanup

Hi all, in this cleanup series we are back at RFC, and back at providing the whole series together.

The last patch in particular contains a different way to provide the accelerator-specific i386 CPU functionality, compared with RFCv1.

In this series I start by moving all i386-specific accelerators into subdirs of target/i386/ away from the main target directory.

Btw do we have any minimal testing coverage for HAX? Also Xen?

After the code move I go on and strip away all the cpu dump code out of helper.c into a new file.
There is a large amount of checkpatch warnings
and errors, but this is due to the copy paste of the old code.

After that I take out tcg-specific stuff from cpu.h and from helper.c and put it in helper-tcg.h and in a new temporary tcg-cpu module.

After this I considered and implemented a few options.

Like in RFCv1, I introduced a new MODULE_INIT phase,

MODULE_INIT_ACCEL_CPU

that will trigger the registration of the cpu accel-specific functionality.

Differently than in RFCv1, the class hierarchy of TYPE_X86_CPUs has been left untouched, and the registration of cpu models has again been left untouched,

instead describing the specialized functionality in a struct.

The existing "accel" class hierarchy, which is basically used to contain machine accelerator-specific initializations, has also been left untouched, as it cannot be used in user mode.

Motivation and higher level steps:

https://lists.gnu.org/archive/html/qemu-devel/2020-05/msg04628.html

Looking forward to your comments on this proposal,

Ciao,

Claudio

Claudio Fontana (9):
i386: move kvm accel files into kvm/
i386: move whpx accel files into whpx/
i386: move hax accel files into hax/
i386: hvf: remove stale MAINTAINERS entry for old hvf stubs
i386: move TCG accel files into tcg/
i386: move cpu dump out of helper.c into cpu-dump.c
i386: move TCG cpu class initialization out of helper.c
module: introduce MODULE_INIT_ACCEL_CPU
i386: split cpu accelerators from cpu.c

 MAINTAINERS                           |  12 +-
 accel/kvm/kvm-all.c                   |  11 +-
 accel/qtest/qtest.c                   |  10 +-
 accel/tcg/tcg-all.c                   |  11 +-
 accel/xen/xen-all.c                   |  12 +-
 bsd-user/main.c                       |   1 +
 hw/i386/fw_cfg.c                      |   2 +-
 hw/i386/intel_iommu.c                 |   2 +-
 hw/i386/kvm/apic.c                    |   2 +-
 hw/i386/kvm/clock.c                   |   2 +-
 hw/i386/microvm.c                     |   2 +-
 hw/i386/pc.c                          |   2 +-
 hw/i386/pc_piix.c                     |   1 +
 hw/i386/x86.c                         |   2 +-
 include/qemu/module.h                 |   2 +
 linux-user/main.c                     |   1 +
 meson.build                           |   1 +
 softmmu/vl.c                          |   6 +
 target/i386/cpu-dump.c                | 538 +++++++++++++++++++++++++
 target/i386/cpu-qom.h                 |  23 ++
 target/i386/cpu.c                     | 420 +++-----------------
 target/i386/cpu.h                     | 120 +-----
 target/i386/{ => hax}/hax-all.c       |  12 +-
 target/i386/{ => hax}/hax-cpus.c      |   0
 target/i386/{ => hax}/hax-cpus.h      |   0
 target/i386/{ => hax}/hax-i386.h      |   6 +-
 target/i386/{ => hax}/hax-interface.h |   0
 target/i386/{ => hax}/hax-mem.c       |   0
 target/i386/{ => hax}/hax-posix.c     |   0
 target/i386/{ => hax}/hax-posix.h     |   0
 target/i386/{ => hax}/hax-windows.c   |   0
 target/i386/{ => hax}/hax-windows.h   |   0
 target/i386/hax/meson.build           |   7 +
 target/i386/helper-tcg.h              | 112 ++++++
 target/i386/helper.c                  | 539 +-------------------------
 target/i386/host-cpu.c                | 196 ++++++++++
 target/i386/host-cpu.h                |  20 +
 target/i386/hvf/cpu.c                 |  64 +++
 target/i386/hvf/hvf.c                 |  10 +-
 target/i386/hvf/meson.build           |   1 +
 target/i386/kvm/cpu.c                 | 145 +++++++
 target/i386/{ => kvm}/hyperv-proto.h  |   0
 target/i386/{ => kvm}/hyperv-stub.c   |   0
 target/i386/{ => kvm}/hyperv.c        |   0
 target/i386/{ => kvm}/hyperv.h        |   0
 target/i386/kvm/kvm-cpu.h             |  41 ++
 target/i386/{ => kvm}/kvm-stub.c      |   0
 target/i386/{ => kvm}/kvm.c           |   3 +-
 target/i386/{ => kvm}/kvm_i386.h      |   0
 target/i386/kvm/meson.build           |   8 +
 target/i386/kvm/trace-events          |   7 +
 target/i386/kvm/trace.h               |   1 +
 target/i386/machine.c                 |   4 +-
 target/i386/meson.build               |  39 +-
 target/i386/{ => tcg}/bpt_helper.c    |   1 +
 target/i386/{ => tcg}/cc_helper.c     |   1 +
 target/i386/tcg/cpu.c                 | 168 ++++++++
 target/i386/{ => tcg}/excp_helper.c   |   1 +
 target/i386/{ => tcg}/fpu_helper.c    |  33 +-
 target/i386/{ => tcg}/int_helper.c    |   1 +
 target/i386/{ => tcg}/mem_helper.c    |   1 +
 target/i386/tcg/meson.build           |  14 +
 target/i386/{ => tcg}/misc_helper.c   |   1 +
 target/i386/{ => tcg}/mpx_helper.c    |   1 +
 target/i386/{ => tcg}/seg_helper.c    |   1 +
 target/i386/{ => tcg}/smm_helper.c    |   2 +
 target/i386/{ => tcg}/svm_helper.c    |   1 +
 target/i386/{ => tcg}/tcg-stub.c      |   0
 target/i386/{ => tcg}/translate.c     |   1 +
 target/i386/trace-events              |   6 -
 target/i386/whpx/meson.build          |   4 +
 target/i386/{ => whpx}/whp-dispatch.h |   0
 target/i386/{ => whpx}/whpx-all.c     |  11 +-
 target/i386/{ => whpx}/whpx-cpus.c    |   0
 target/i386/{ => whpx}/whpx-cpus.h    |   0
 75 files changed, 1555 insertions(+), 1091 deletions(-)
 create mode 100644 target/i386/cpu-dump.c
 rename target/i386/{ => hax}/hax-all.c (99%)
 rename target/i386/{ => hax}/hax-cpus.c (100%)
 rename target/i386/{ => hax}/hax-cpus.h (100%)
 rename target/i386/{ => hax}/hax-i386.h (95%)
 rename target/i386/{ => hax}/hax-interface.h (100%)
 rename target/i386/{ => hax}/hax-mem.c (100%)
 rename target/i386/{ => hax}/hax-posix.c (100%)
 rename target/i386/{ => hax}/hax-posix.h (100%)
 rename target/i386/{ => hax}/hax-windows.c (100%)
 rename target/i386/{ => hax}/hax-windows.h (100%)
 create mode 100644 target/i386/hax/meson.build
 create mode 100644 target/i386/helper-tcg.h
 create mode 100644 target/i386/host-cpu.c
 create mode 100644 target/i386/host-cpu.h
 create mode 100644 target/i386/hvf/cpu.c
 create mode 100644 target/i386/kvm/cpu.c
 rename target/i386/{ => kvm}/hyperv-proto.h (100%)
 rename target/i386/{ => kvm}/hyperv-stub.c (100%)
 rename target/i386/{ => kvm}/hyperv.c (100%)
 rename target/i386/{ => kvm}/hyperv.h (100%)
 create mode 100644 target/i386/kvm/kvm-cpu.h
 rename target/i386/{ => kvm}/kvm-stub.c (100%)
 rename target/i386/{ => kvm}/kvm.c (99%)
 rename target/i386/{ => kvm}/kvm_i386.h (100%)
 create mode 100644 target/i386/kvm/meson.build
 create mode 100644 target/i386/kvm/trace-events
 create mode 100644 target/i386/kvm/trace.h
 rename target/i386/{ => tcg}/bpt_helper.c (99%)
 rename target/i386/{ => tcg}/cc_helper.c (99%)
 create mode 100644 target/i386/tcg/cpu.c
 rename target/i386/{ => tcg}/excp_helper.c (99%)
 rename target/i386/{ => tcg}/fpu_helper.c (99%)
 rename target/i386/{ => tcg}/int_helper.c (99%)
 rename target/i386/{ => tcg}/mem_helper.c (99%)
 create mode 100644 target/i386/tcg/meson.build
 rename target/i386/{ => tcg}/misc_helper.c (99%)
 rename target/i386/{ => tcg}/mpx_helper.c (99%)
 rename target/i386/{ => tcg}/seg_helper.c (99%)
 rename target/i386/{ => tcg}/smm_helper.c (99%)
 rename target/i386/{ => tcg}/svm_helper.c (99%)
 rename target/i386/{ => tcg}/tcg-stub.c (100%)
 rename target/i386/{ => tcg}/translate.c (99%)
 create mode 100644 target/i386/whpx/meson.build
 rename target/i386/{ => whpx}/whp-dispatch.h (100%)
 rename target/i386/{ => whpx}/whpx-all.c (99%)
 rename target/i386/{ => whpx}/whpx-cpus.c (100%)
 rename target/i386/{ => whpx}/whpx-cpus.h (100%)
Signature · 1 line
2.26.2
#1 lore raw

RFC v3 1/9 i386: move kvm accel files into kvm/

---
 MAINTAINERS                          | 2 +-
 hw/i386/fw_cfg.c                     | 2 +-
 hw/i386/intel_iommu.c                | 2 +-
 hw/i386/kvm/apic.c                   | 2 +-
 hw/i386/kvm/clock.c                  | 2 +-
 hw/i386/microvm.c                    | 2 +-
 hw/i386/pc.c                         | 2 +-
 hw/i386/x86.c                        | 2 +-
 meson.build                          | 1 +
 target/i386/cpu.c                    | 2 +-
 target/i386/cpu.h                    | 2 +-
 target/i386/helper.c                 | 2 +-
 target/i386/{ => kvm}/hyperv-proto.h | 0
 target/i386/{ => kvm}/hyperv-stub.c  | 0
 target/i386/{ => kvm}/hyperv.c       | 0
 target/i386/{ => kvm}/hyperv.h       | 0
 target/i386/{ => kvm}/kvm-stub.c     | 0
 target/i386/{ => kvm}/kvm.c          | 0
 target/i386/{ => kvm}/kvm_i386.h     | 0
 target/i386/kvm/meson.build          | 3 +++
 target/i386/kvm/trace-events         | 7 +++++++
 target/i386/kvm/trace.h              | 1 +
 target/i386/machine.c                | 4 ++--
 target/i386/meson.build              | 4 +---
 target/i386/trace-events             | 6 ------
 25 files changed, 26 insertions(+), 22 deletions(-)
 rename target/i386/{ => kvm}/hyperv-proto.h (100%)
 rename target/i386/{ => kvm}/hyperv-stub.c (100%)
 rename target/i386/{ => kvm}/hyperv.c (100%)
 rename target/i386/{ => kvm}/hyperv.h (100%)
 rename target/i386/{ => kvm}/kvm-stub.c (100%)
 rename target/i386/{ => kvm}/kvm.c (100%)
 rename target/i386/{ => kvm}/kvm_i386.h (100%)
 create mode 100644 target/i386/kvm/meson.build
 create mode 100644 target/i386/kvm/trace-events
 create mode 100644 target/i386/kvm/trace.h
MAINTAINERS +1 -1
index 2e018a0c1d..6a77faccf6 100644
@@ -424,7 +424,7 @@ M: Paolo Bonzini <[email protected]>
 M: Marcelo Tosatti <[email protected]>
 L: [email protected]
 S: Supported
-F: target/i386/kvm.c
+F: target/i386/kvm/
 F: scripts/kvm/vmxcap
 
 Guest CPU Cores (other accelerators)
hw/i386/fw_cfg.c +1 -1
index e06579490c..fae1bb380f 100644
@@ -21,7 +21,7 @@
 #include "hw/timer/hpet.h"
 #include "hw/nvram/fw_cfg.h"
 #include "e820_memory_layout.h"
-#include "kvm_i386.h"
+#include "kvm/kvm_i386.h"
 #include CONFIG_DEVICES
 
 struct hpet_fw_config hpet_cfg = {.count = UINT8_MAX};
hw/i386/intel_iommu.c +1 -1
index 70ac837733..361b6cd238 100644
@@ -37,7 +37,7 @@
 #include "sysemu/kvm.h"
 #include "sysemu/sysemu.h"
 #include "hw/i386/apic_internal.h"
-#include "kvm_i386.h"
+#include "kvm/kvm_i386.h"
 #include "migration/vmstate.h"
 #include "trace.h"
 
hw/i386/kvm/apic.c +1 -1
index dd29906061..07bebc1282 100644
@@ -17,7 +17,7 @@
 #include "hw/pci/msi.h"
 #include "sysemu/hw_accel.h"
 #include "sysemu/kvm.h"
-#include "target/i386/kvm_i386.h"
+#include "kvm/kvm_i386.h"
 
 static inline void kvm_apic_set_reg(struct kvm_lapic_state *kapic,
                                     int reg_id, uint32_t val)
hw/i386/kvm/clock.c +1 -1
index 24fe5091b6..2d8a366369 100644
@@ -20,7 +20,7 @@
 #include "sysemu/kvm.h"
 #include "sysemu/runstate.h"
 #include "sysemu/hw_accel.h"
-#include "kvm_i386.h"
+#include "kvm/kvm_i386.h"
 #include "migration/vmstate.h"
 #include "hw/sysbus.h"
 #include "hw/kvm/clock.h"
hw/i386/microvm.c +1 -1
index 5428448b70..3ed6adff83 100644
@@ -51,7 +51,7 @@
 
 #include "cpu.h"
 #include "elf.h"
-#include "kvm_i386.h"
+#include "kvm/kvm_i386.h"
 #include "hw/xen/start_info.h"
 
 #define MICROVM_QBOOT_FILENAME "qboot.rom"
hw/i386/pc.c +1 -1
index 17b514d1da..299aaba8e9 100644
@@ -61,7 +61,7 @@
 #include "sysemu/qtest.h"
 #include "sysemu/reset.h"
 #include "sysemu/runstate.h"
-#include "kvm_i386.h"
+#include "kvm/kvm_i386.h"
 #include "hw/xen/xen.h"
 #include "hw/xen/start_info.h"
 #include "ui/qemu-spice.h"
hw/i386/x86.c +1 -1
index 5944fc44ed..88d0c70e12 100644
@@ -52,7 +52,7 @@
 #include "elf.h"
 #include "standard-headers/asm-x86/bootparam.h"
 #include CONFIG_DEVICES
-#include "kvm_i386.h"
+#include "kvm/kvm_i386.h"
 
 #define BIOS_FILENAME "bios.bin"
 
meson.build +1 -0
index 7ddf983ff7..9e1f02f713 100644
@@ -1463,6 +1463,7 @@ trace_events_subdirs += [
   'target/arm',
   'target/hppa',
   'target/i386',
+  'target/i386/kvm',
   'target/mips',
   'target/ppc',
   'target/riscv',
target/i386/cpu.c +1 -1
index 5a8c96072e..b9bd249c8f 100644
@@ -30,7 +30,7 @@
 #include "sysemu/hvf.h"
 #include "sysemu/cpus.h"
 #include "sysemu/xen.h"
-#include "kvm_i386.h"
+#include "kvm/kvm_i386.h"
 #include "sev_i386.h"
 
 #include "qemu/error-report.h"
target/i386/cpu.h +1 -1
index 88e8586f8f..9ecda75aec 100644
@@ -22,7 +22,7 @@
 
 #include "sysemu/tcg.h"
 #include "cpu-qom.h"
-#include "hyperv-proto.h"
+#include "kvm/hyperv-proto.h"
 #include "exec/cpu-defs.h"
 #include "qapi/qapi-types-common.h"
 
target/i386/helper.c +1 -1
index 034f46bcc2..a1b3367ab2 100644
@@ -24,7 +24,7 @@
 #include "qemu/qemu-print.h"
 #include "sysemu/kvm.h"
 #include "sysemu/runstate.h"
-#include "kvm_i386.h"
+#include "kvm/kvm_i386.h"
 #ifndef CONFIG_USER_ONLY
 #include "sysemu/tcg.h"
 #include "sysemu/hw_accel.h"
target/i386/kvm/hyperv-proto.h renamed from target/i386/hyperv-proto.h +0 -0
similarity index 100%
rename from target/i386/hyperv-proto.h
rename to target/i386/kvm/hyperv-proto.h
target/i386/kvm/hyperv-stub.c renamed from target/i386/hyperv-stub.c +0 -0
similarity index 100%
rename from target/i386/hyperv-stub.c
rename to target/i386/kvm/hyperv-stub.c
target/i386/kvm/hyperv.c renamed from target/i386/hyperv.c +0 -0
similarity index 100%
rename from target/i386/hyperv.c
rename to target/i386/kvm/hyperv.c
target/i386/kvm/hyperv.h renamed from target/i386/hyperv.h +0 -0
similarity index 100%
rename from target/i386/hyperv.h
rename to target/i386/kvm/hyperv.h
target/i386/kvm/kvm-stub.c renamed from target/i386/kvm-stub.c +0 -0
similarity index 100%
rename from target/i386/kvm-stub.c
rename to target/i386/kvm/kvm-stub.c
target/i386/kvm/kvm.c renamed from target/i386/kvm.c +0 -0
similarity index 100%
rename from target/i386/kvm.c
rename to target/i386/kvm/kvm.c
target/i386/kvm/kvm_i386.h renamed from target/i386/kvm_i386.h +0 -0
similarity index 100%
rename from target/i386/kvm_i386.h
rename to target/i386/kvm/kvm_i386.h
target/i386/kvm/meson.build new file +3 -0
new file mode 100644
index 0000000000..1d66559187
@@ -0,0 +1,3 @@
+i386_ss.add(when: 'CONFIG_KVM', if_false: files('kvm-stub.c'))
+i386_softmmu_ss.add(when: 'CONFIG_KVM', if_true: files('kvm.c'))
+i386_softmmu_ss.add(when: 'CONFIG_HYPERV', if_true: files('hyperv.c'), if_false: files('hyperv-stub.c'))
target/i386/kvm/trace-events new file +7 -0
new file mode 100644
index 0000000000..b4e2d9e4ea
@@ -0,0 +1,7 @@
+# See docs/devel/tracing.txt for syntax documentation.
+
+# kvm.c
+kvm_x86_fixup_msi_error(uint32_t gsi) "VT-d failed to remap interrupt for GSI %" PRIu32
+kvm_x86_add_msi_route(int virq) "Adding route entry for virq %d"
+kvm_x86_remove_msi_route(int virq) "Removing route entry for virq %d"
+kvm_x86_update_msi_routes(int num) "Updated %d MSI routes"
target/i386/kvm/trace.h new file +1 -0
new file mode 100644
index 0000000000..46b75c6942
@@ -0,0 +1 @@
+#include "trace/trace-target_i386_kvm.h"
target/i386/machine.c +2 -2
index 233e46bb70..1614e8c2f8 100644
@@ -3,9 +3,9 @@
 #include "exec/exec-all.h"
 #include "hw/isa/isa.h"
 #include "migration/cpu.h"
-#include "hyperv.h"
+#include "kvm/hyperv.h"
 #include "hw/i386/x86.h"
-#include "kvm_i386.h"
+#include "kvm/kvm_i386.h"
 
 #include "sysemu/kvm.h"
 #include "sysemu/tcg.h"
target/i386/meson.build +1 -3
index a1a02f3e99..0209542a8a 100644
@@ -18,7 +18,6 @@ i386_ss.add(when: 'CONFIG_TCG', if_true: files(
   'smm_helper.c',
   'svm_helper.c',
   'translate.c'), if_false: files('tcg-stub.c'))
-i386_ss.add(when: 'CONFIG_KVM', if_false: files('kvm-stub.c'))
 i386_ss.add(when: 'CONFIG_SEV', if_true: files('sev.c'), if_false: files('sev-stub.c'))
 
 i386_softmmu_ss = ss.source_set()
@@ -28,8 +27,6 @@ i386_softmmu_ss.add(files(
   'machine.c',
   'monitor.c',
 ))
-i386_softmmu_ss.add(when: 'CONFIG_HYPERV', if_true: files('hyperv.c'), if_false: files('hyperv-stub.c'))
-i386_softmmu_ss.add(when: 'CONFIG_KVM', if_true: files('kvm.c'))
 i386_softmmu_ss.add(when: 'CONFIG_WHPX', if_true: files(
   'whpx-all.c',
   'whpx-cpus.c',
@@ -42,6 +39,7 @@ i386_softmmu_ss.add(when: 'CONFIG_HAX', if_true: files(
 i386_softmmu_ss.add(when: ['CONFIG_HAX', 'CONFIG_POSIX'], if_true: files('hax-posix.c'))
 i386_softmmu_ss.add(when: ['CONFIG_HAX', 'CONFIG_WIN32'], if_true: files('hax-windows.c'))
 
+subdir('kvm')
 subdir('hvf')
 
 target_arch += {'i386': i386_ss}
target/i386/trace-events +0 -6
index 789c700d4a..d166f9d5e0 100644
@@ -1,11 +1,5 @@
 # See docs/devel/tracing.txt for syntax documentation.
 
-# kvm.c
-kvm_x86_fixup_msi_error(uint32_t gsi) "VT-d failed to remap interrupt for GSI %" PRIu32
-kvm_x86_add_msi_route(int virq) "Adding route entry for virq %d"
-kvm_x86_remove_msi_route(int virq) "Removing route entry for virq %d"
-kvm_x86_update_msi_routes(int num) "Updated %d MSI routes"
-
 # sev.c
 kvm_sev_init(void) ""
 kvm_memcrypt_register_region(void *addr, size_t len) "addr %p len 0x%zu"
Signature · 1 line
2.26.2
#2 lore raw

RFC v3 2/9 i386: move whpx accel files into whpx/

In reply to Claudio Fontana
---
 MAINTAINERS                           | 5 +----
 target/i386/meson.build               | 5 +----
 target/i386/whpx/meson.build          | 4 ++++
 target/i386/{ => whpx}/whp-dispatch.h | 0
 target/i386/{ => whpx}/whpx-all.c     | 0
 target/i386/{ => whpx}/whpx-cpus.c    | 0
 target/i386/{ => whpx}/whpx-cpus.h    | 0
 7 files changed, 6 insertions(+), 8 deletions(-)
 create mode 100644 target/i386/whpx/meson.build
 rename target/i386/{ => whpx}/whp-dispatch.h (100%)
 rename target/i386/{ => whpx}/whpx-all.c (100%)
 rename target/i386/{ => whpx}/whpx-cpus.c (100%)
 rename target/i386/{ => whpx}/whpx-cpus.h (100%)
MAINTAINERS +1 -4
index 6a77faccf6..02bc29ea2b 100644
@@ -450,10 +450,7 @@ F: include/sysemu/hvf.h
 WHPX CPUs
 M: Sunil Muthuswamy <[email protected]>
 S: Supported
-F: target/i386/whpx-all.c
-F: target/i386/whpx-cpus.c
-F: target/i386/whp-dispatch.h
-F: accel/stubs/whpx-stub.c
+F: target/i386/whpx/
 F: include/sysemu/whpx.h
 
 Guest CPU Cores (Xen)
target/i386/meson.build +1 -4
index 0209542a8a..62cd042915 100644
@@ -27,10 +27,6 @@ i386_softmmu_ss.add(files(
   'machine.c',
   'monitor.c',
 ))
-i386_softmmu_ss.add(when: 'CONFIG_WHPX', if_true: files(
-  'whpx-all.c',
-  'whpx-cpus.c',
-))
 i386_softmmu_ss.add(when: 'CONFIG_HAX', if_true: files(
   'hax-all.c',
   'hax-mem.c',
@@ -40,6 +36,7 @@ i386_softmmu_ss.add(when: ['CONFIG_HAX', 'CONFIG_POSIX'], if_true: files('hax-po
 i386_softmmu_ss.add(when: ['CONFIG_HAX', 'CONFIG_WIN32'], if_true: files('hax-windows.c'))
 
 subdir('kvm')
+subdir('whpx')
 subdir('hvf')
 
 target_arch += {'i386': i386_ss}
target/i386/whpx/meson.build new file +4 -0
new file mode 100644
index 0000000000..94a72c8efc
@@ -0,0 +1,4 @@
+i386_softmmu_ss.add(when: 'CONFIG_WHPX', if_true: files(
+  'whpx-all.c',
+  'whpx-cpus.c',
+))
target/i386/whpx/whp-dispatch.h renamed from target/i386/whp-dispatch.h +0 -0
similarity index 100%
rename from target/i386/whp-dispatch.h
rename to target/i386/whpx/whp-dispatch.h
target/i386/whpx/whpx-all.c renamed from target/i386/whpx-all.c +0 -0
similarity index 100%
rename from target/i386/whpx-all.c
rename to target/i386/whpx/whpx-all.c
target/i386/whpx/whpx-cpus.c renamed from target/i386/whpx-cpus.c +0 -0
similarity index 100%
rename from target/i386/whpx-cpus.c
rename to target/i386/whpx/whpx-cpus.c
target/i386/whpx/whpx-cpus.h renamed from target/i386/whpx-cpus.h +0 -0
similarity index 100%
rename from target/i386/whpx-cpus.h
rename to target/i386/whpx/whpx-cpus.h
Signature · 1 line
2.26.2
#3 lore raw

RFC v3 3/9 i386: move hax accel files into hax/

In reply to Claudio Fontana
---
 MAINTAINERS                           | 2 +-
 target/i386/{ => hax}/hax-all.c       | 0
 target/i386/{ => hax}/hax-cpus.c      | 0
 target/i386/{ => hax}/hax-cpus.h      | 0
 target/i386/{ => hax}/hax-i386.h      | 6 +++---
 target/i386/{ => hax}/hax-interface.h | 0
 target/i386/{ => hax}/hax-mem.c       | 0
 target/i386/{ => hax}/hax-posix.c     | 0
 target/i386/{ => hax}/hax-posix.h     | 0
 target/i386/{ => hax}/hax-windows.c   | 0
 target/i386/{ => hax}/hax-windows.h   | 0
 target/i386/hax/meson.build           | 7 +++++++
 target/i386/meson.build               | 8 +-------
 13 files changed, 12 insertions(+), 11 deletions(-)
 rename target/i386/{ => hax}/hax-all.c (100%)
 rename target/i386/{ => hax}/hax-cpus.c (100%)
 rename target/i386/{ => hax}/hax-cpus.h (100%)
 rename target/i386/{ => hax}/hax-i386.h (95%)
 rename target/i386/{ => hax}/hax-interface.h (100%)
 rename target/i386/{ => hax}/hax-mem.c (100%)
 rename target/i386/{ => hax}/hax-posix.c (100%)
 rename target/i386/{ => hax}/hax-posix.h (100%)
 rename target/i386/{ => hax}/hax-windows.c (100%)
 rename target/i386/{ => hax}/hax-windows.h (100%)
 create mode 100644 target/i386/hax/meson.build
MAINTAINERS +1 -1
index 02bc29ea2b..463317784c 100644
@@ -490,7 +490,7 @@ W: https://github.com/intel/haxm/issues
 S: Maintained
 F: accel/stubs/hax-stub.c
 F: include/sysemu/hax.h
-F: target/i386/hax-*
+F: target/i386/hax/
 
 Hosts
 -----
target/i386/hax/hax-all.c renamed from target/i386/hax-all.c +0 -0
similarity index 100%
rename from target/i386/hax-all.c
rename to target/i386/hax/hax-all.c
target/i386/hax/hax-cpus.c renamed from target/i386/hax-cpus.c +0 -0
similarity index 100%
rename from target/i386/hax-cpus.c
rename to target/i386/hax/hax-cpus.c
target/i386/hax/hax-cpus.h renamed from target/i386/hax-cpus.h +0 -0
similarity index 100%
rename from target/i386/hax-cpus.h
rename to target/i386/hax/hax-cpus.h
target/i386/hax/hax-i386.h renamed from target/i386/hax-i386.h +3 -3
similarity index 95%
rename from target/i386/hax-i386.h
rename to target/i386/hax/hax-i386.h
index 48c4abe14e..efbb346238 100644
@@ -84,13 +84,13 @@ void hax_memory_init(void);
 
 
 #ifdef CONFIG_POSIX
-#include "target/i386/hax-posix.h"
+#include "hax-posix.h"
 #endif
 
 #ifdef CONFIG_WIN32
-#include "target/i386/hax-windows.h"
+#include "hax-windows.h"
 #endif
 
-#include "target/i386/hax-interface.h"
+#include "hax-interface.h"
 
 #endif
target/i386/hax/hax-interface.h renamed from target/i386/hax-interface.h +0 -0
similarity index 100%
rename from target/i386/hax-interface.h
rename to target/i386/hax/hax-interface.h
target/i386/hax/hax-mem.c renamed from target/i386/hax-mem.c +0 -0
similarity index 100%
rename from target/i386/hax-mem.c
rename to target/i386/hax/hax-mem.c
target/i386/hax/hax-posix.c renamed from target/i386/hax-posix.c +0 -0
similarity index 100%
rename from target/i386/hax-posix.c
rename to target/i386/hax/hax-posix.c
target/i386/hax/hax-posix.h renamed from target/i386/hax-posix.h +0 -0
similarity index 100%
rename from target/i386/hax-posix.h
rename to target/i386/hax/hax-posix.h
target/i386/hax/hax-windows.c renamed from target/i386/hax-windows.c +0 -0
similarity index 100%
rename from target/i386/hax-windows.c
rename to target/i386/hax/hax-windows.c
target/i386/hax/hax-windows.h renamed from target/i386/hax-windows.h +0 -0
similarity index 100%
rename from target/i386/hax-windows.h
rename to target/i386/hax/hax-windows.h
target/i386/hax/meson.build new file +7 -0
new file mode 100644
index 0000000000..77ea431b30
@@ -0,0 +1,7 @@
+i386_softmmu_ss.add(when: 'CONFIG_HAX', if_true: files(
+  'hax-all.c',
+  'hax-mem.c',
+  'hax-cpus.c',
+))
+i386_softmmu_ss.add(when: ['CONFIG_HAX', 'CONFIG_POSIX'], if_true: files('hax-posix.c'))
+i386_softmmu_ss.add(when: ['CONFIG_HAX', 'CONFIG_WIN32'], if_true: files('hax-windows.c'))
target/i386/meson.build +1 -7
index 62cd042915..284d52ab81 100644
@@ -27,15 +27,9 @@ i386_softmmu_ss.add(files(
   'machine.c',
   'monitor.c',
 ))
-i386_softmmu_ss.add(when: 'CONFIG_HAX', if_true: files(
-  'hax-all.c',
-  'hax-mem.c',
-  'hax-cpus.c',
-))
-i386_softmmu_ss.add(when: ['CONFIG_HAX', 'CONFIG_POSIX'], if_true: files('hax-posix.c'))
-i386_softmmu_ss.add(when: ['CONFIG_HAX', 'CONFIG_WIN32'], if_true: files('hax-windows.c'))
 
 subdir('kvm')
+subdir('hax')
 subdir('whpx')
 subdir('hvf')
 
Signature · 1 line
2.26.2
#4 lore raw

RFC v3 4/9 i386: hvf: remove stale MAINTAINERS entry for old hvf stubs

In reply to Claudio Fontana
---
 MAINTAINERS | 1 -
 1 file changed, 1 deletion(-)
MAINTAINERS +0 -1
index 463317784c..e892dd2220 100644
@@ -443,7 +443,6 @@ M: Cameron Esfahani <[email protected]>
 M: Roman Bolshakov <[email protected]>
 W: https://wiki.qemu.org/Features/HVF
 S: Maintained
-F: accel/stubs/hvf-stub.c
 F: target/i386/hvf/
 F: include/sysemu/hvf.h
 
Signature · 1 line
2.26.2
Reviewed-by on v3 4/9
#5 lore raw

Re: [RFC v3 4/9] i386: hvf: remove stale MAINTAINERS entry for old hvf stubs

Quoted 20 lines · Claudio Fontana
---
 MAINTAINERS | 1 -
 1 file changed, 1 deletion(-)
MAINTAINERS +0 -1
index 463317784c..e892dd2220 100644
@@ -443,7 +443,6 @@ M: Cameron Esfahani <[email protected]>
 M: Roman Bolshakov <[email protected]>
 W: https://wiki.qemu.org/Features/HVF
 S: Maintained
-F: accel/stubs/hvf-stub.c
 F: target/i386/hvf/
 F: include/sysemu/hvf.h
 
Signature · 1 line
2.26.2

Thanks,
Roman

#6 lore raw

RFC v3 5/9 i386: move TCG accel files into tcg/

In reply to Claudio Fontana
---
 target/i386/meson.build             | 14 +-------------
 target/i386/{ => tcg}/bpt_helper.c  |  0
 target/i386/{ => tcg}/cc_helper.c   |  0
 target/i386/{ => tcg}/excp_helper.c |  0
 target/i386/{ => tcg}/fpu_helper.c  |  0
 target/i386/{ => tcg}/int_helper.c  |  0
 target/i386/{ => tcg}/mem_helper.c  |  0
 target/i386/tcg/meson.build         | 13 +++++++++++++
 target/i386/{ => tcg}/misc_helper.c |  0
 target/i386/{ => tcg}/mpx_helper.c  |  0
 target/i386/{ => tcg}/seg_helper.c  |  0
 target/i386/{ => tcg}/smm_helper.c  |  0
 target/i386/{ => tcg}/svm_helper.c  |  0
 target/i386/{ => tcg}/tcg-stub.c    |  0
 target/i386/{ => tcg}/translate.c   |  0
 15 files changed, 14 insertions(+), 13 deletions(-)
 rename target/i386/{ => tcg}/bpt_helper.c (100%)
 rename target/i386/{ => tcg}/cc_helper.c (100%)
 rename target/i386/{ => tcg}/excp_helper.c (100%)
 rename target/i386/{ => tcg}/fpu_helper.c (100%)
 rename target/i386/{ => tcg}/int_helper.c (100%)
 rename target/i386/{ => tcg}/mem_helper.c (100%)
 create mode 100644 target/i386/tcg/meson.build
 rename target/i386/{ => tcg}/misc_helper.c (100%)
 rename target/i386/{ => tcg}/mpx_helper.c (100%)
 rename target/i386/{ => tcg}/seg_helper.c (100%)
 rename target/i386/{ => tcg}/smm_helper.c (100%)
 rename target/i386/{ => tcg}/svm_helper.c (100%)
 rename target/i386/{ => tcg}/tcg-stub.c (100%)
 rename target/i386/{ => tcg}/translate.c (100%)
target/i386/meson.build +1 -13
index 284d52ab81..750471c9f3 100644
@@ -5,19 +5,6 @@ i386_ss.add(files(
   'helper.c',
   'xsave_helper.c',
 ))
-i386_ss.add(when: 'CONFIG_TCG', if_true: files(
-  'bpt_helper.c',
-  'cc_helper.c',
-  'excp_helper.c',
-  'fpu_helper.c',
-  'int_helper.c',
-  'mem_helper.c',
-  'misc_helper.c',
-  'mpx_helper.c',
-  'seg_helper.c',
-  'smm_helper.c',
-  'svm_helper.c',
-  'translate.c'), if_false: files('tcg-stub.c'))
 i386_ss.add(when: 'CONFIG_SEV', if_true: files('sev.c'), if_false: files('sev-stub.c'))
 
 i386_softmmu_ss = ss.source_set()
@@ -32,6 +19,7 @@ subdir('kvm')
 subdir('hax')
 subdir('whpx')
 subdir('hvf')
+subdir('tcg')
 
 target_arch += {'i386': i386_ss}
 target_softmmu_arch += {'i386': i386_softmmu_ss}
target/i386/tcg/bpt_helper.c renamed from target/i386/bpt_helper.c +0 -0
similarity index 100%
rename from target/i386/bpt_helper.c
rename to target/i386/tcg/bpt_helper.c
target/i386/tcg/cc_helper.c renamed from target/i386/cc_helper.c +0 -0
similarity index 100%
rename from target/i386/cc_helper.c
rename to target/i386/tcg/cc_helper.c
target/i386/tcg/excp_helper.c renamed from target/i386/excp_helper.c +0 -0
similarity index 100%
rename from target/i386/excp_helper.c
rename to target/i386/tcg/excp_helper.c
target/i386/tcg/fpu_helper.c renamed from target/i386/fpu_helper.c +0 -0
similarity index 100%
rename from target/i386/fpu_helper.c
rename to target/i386/tcg/fpu_helper.c
target/i386/tcg/int_helper.c renamed from target/i386/int_helper.c +0 -0
similarity index 100%
rename from target/i386/int_helper.c
rename to target/i386/tcg/int_helper.c
target/i386/tcg/mem_helper.c renamed from target/i386/mem_helper.c +0 -0
similarity index 100%
rename from target/i386/mem_helper.c
rename to target/i386/tcg/mem_helper.c
target/i386/tcg/meson.build new file +13 -0
new file mode 100644
index 0000000000..02794226c2
@@ -0,0 +1,13 @@
+i386_ss.add(when: 'CONFIG_TCG', if_true: files(
+  'bpt_helper.c',
+  'cc_helper.c',
+  'excp_helper.c',
+  'fpu_helper.c',
+  'int_helper.c',
+  'mem_helper.c',
+  'misc_helper.c',
+  'mpx_helper.c',
+  'seg_helper.c',
+  'smm_helper.c',
+  'svm_helper.c',
+  'translate.c'), if_false: files('tcg-stub.c'))
target/i386/tcg/misc_helper.c renamed from target/i386/misc_helper.c +0 -0
similarity index 100%
rename from target/i386/misc_helper.c
rename to target/i386/tcg/misc_helper.c
target/i386/tcg/mpx_helper.c renamed from target/i386/mpx_helper.c +0 -0
similarity index 100%
rename from target/i386/mpx_helper.c
rename to target/i386/tcg/mpx_helper.c
target/i386/tcg/seg_helper.c renamed from target/i386/seg_helper.c +0 -0
similarity index 100%
rename from target/i386/seg_helper.c
rename to target/i386/tcg/seg_helper.c
target/i386/tcg/smm_helper.c renamed from target/i386/smm_helper.c +0 -0
similarity index 100%
rename from target/i386/smm_helper.c
rename to target/i386/tcg/smm_helper.c
target/i386/tcg/svm_helper.c renamed from target/i386/svm_helper.c +0 -0
similarity index 100%
rename from target/i386/svm_helper.c
rename to target/i386/tcg/svm_helper.c
target/i386/tcg/tcg-stub.c renamed from target/i386/tcg-stub.c +0 -0
similarity index 100%
rename from target/i386/tcg-stub.c
rename to target/i386/tcg/tcg-stub.c
target/i386/tcg/translate.c renamed from target/i386/translate.c +0 -0
similarity index 100%
rename from target/i386/translate.c
rename to target/i386/tcg/translate.c
Signature · 1 line
2.26.2
#7 lore raw

RFC v3 6/9 i386: move cpu dump out of helper.c into cpu-dump.c

In reply to Claudio Fontana
---
 target/i386/cpu-dump.c  | 538 ++++++++++++++++++++++++++++++++++++++++
 target/i386/cpu.h       |   1 +
 target/i386/helper.c    | 514 --------------------------------------
 target/i386/meson.build |   1 +
 4 files changed, 540 insertions(+), 514 deletions(-)
 create mode 100644 target/i386/cpu-dump.c
target/i386/cpu-dump.c new file +538 -0
new file mode 100644
index 0000000000..1ddc47fb0c
@@ -0,0 +1,538 @@
+/*
+ *  i386 CPU dump to FILE
+ *
+ *  Copyright (c) 2003 Fabrice Bellard
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "qemu/osdep.h"
+#include "cpu.h"
+#include "qemu/qemu-print.h"
+#ifndef CONFIG_USER_ONLY
+#include "hw/i386/apic_internal.h"
+#endif
+
+/***********************************************************/
+/* x86 debug */
+
+static const char *cc_op_str[CC_OP_NB] = {
+    "DYNAMIC",
+    "EFLAGS",
+
+    "MULB",
+    "MULW",
+    "MULL",
+    "MULQ",
+
+    "ADDB",
+    "ADDW",
+    "ADDL",
+    "ADDQ",
+
+    "ADCB",
+    "ADCW",
+    "ADCL",
+    "ADCQ",
+
+    "SUBB",
+    "SUBW",
+    "SUBL",
+    "SUBQ",
+
+    "SBBB",
+    "SBBW",
+    "SBBL",
+    "SBBQ",
+
+    "LOGICB",
+    "LOGICW",
+    "LOGICL",
+    "LOGICQ",
+
+    "INCB",
+    "INCW",
+    "INCL",
+    "INCQ",
+
+    "DECB",
+    "DECW",
+    "DECL",
+    "DECQ",
+
+    "SHLB",
+    "SHLW",
+    "SHLL",
+    "SHLQ",
+
+    "SARB",
+    "SARW",
+    "SARL",
+    "SARQ",
+
+    "BMILGB",
+    "BMILGW",
+    "BMILGL",
+    "BMILGQ",
+
+    "ADCX",
+    "ADOX",
+    "ADCOX",
+
+    "CLR",
+};
+
+static void
+cpu_x86_dump_seg_cache(CPUX86State *env, FILE *f,
+                       const char *name, struct SegmentCache *sc)
+{
+#ifdef TARGET_X86_64
+    if (env->hflags & HF_CS64_MASK) {
+        qemu_fprintf(f, "%-3s=%04x %016" PRIx64 " %08x %08x", name,
+                     sc->selector, sc->base, sc->limit,
+                     sc->flags & 0x00ffff00);
+    } else
+#endif
+    {
+        qemu_fprintf(f, "%-3s=%04x %08x %08x %08x", name, sc->selector,
+                     (uint32_t)sc->base, sc->limit,
+                     sc->flags & 0x00ffff00);
+    }
+
+    if (!(env->hflags & HF_PE_MASK) || !(sc->flags & DESC_P_MASK))
+        goto done;
+
+    qemu_fprintf(f, " DPL=%d ",
+                 (sc->flags & DESC_DPL_MASK) >> DESC_DPL_SHIFT);
+    if (sc->flags & DESC_S_MASK) {
+        if (sc->flags & DESC_CS_MASK) {
+            qemu_fprintf(f, (sc->flags & DESC_L_MASK) ? "CS64" :
+                         ((sc->flags & DESC_B_MASK) ? "CS32" : "CS16"));
+            qemu_fprintf(f, " [%c%c", (sc->flags & DESC_C_MASK) ? 'C' : '-',
+                         (sc->flags & DESC_R_MASK) ? 'R' : '-');
+        } else {
+            qemu_fprintf(f, (sc->flags & DESC_B_MASK
+                             || env->hflags & HF_LMA_MASK)
+                         ? "DS  " : "DS16");
+            qemu_fprintf(f, " [%c%c", (sc->flags & DESC_E_MASK) ? 'E' : '-',
+                         (sc->flags & DESC_W_MASK) ? 'W' : '-');
+        }
+        qemu_fprintf(f, "%c]", (sc->flags & DESC_A_MASK) ? 'A' : '-');
+    } else {
+        static const char *sys_type_name[2][16] = {
+            { /* 32 bit mode */
+                "Reserved", "TSS16-avl", "LDT", "TSS16-busy",
+                "CallGate16", "TaskGate", "IntGate16", "TrapGate16",
+                "Reserved", "TSS32-avl", "Reserved", "TSS32-busy",
+                "CallGate32", "Reserved", "IntGate32", "TrapGate32"
+            },
+            { /* 64 bit mode */
+                "<hiword>", "Reserved", "LDT", "Reserved", "Reserved",
+                "Reserved", "Reserved", "Reserved", "Reserved",
+                "TSS64-avl", "Reserved", "TSS64-busy", "CallGate64",
+                "Reserved", "IntGate64", "TrapGate64"
+            }
+        };
+        qemu_fprintf(f, "%s",
+                     sys_type_name[(env->hflags & HF_LMA_MASK) ? 1 : 0]
+                     [(sc->flags & DESC_TYPE_MASK) >> DESC_TYPE_SHIFT]);
+    }
+done:
+    qemu_fprintf(f, "\n");
+}
+
+#ifndef CONFIG_USER_ONLY
+
+/* ARRAY_SIZE check is not required because
+ * DeliveryMode(dm) has a size of 3 bit.
+ */
+static inline const char *dm2str(uint32_t dm)
+{
+    static const char *str[] = {
+        "Fixed",
+        "...",
+        "SMI",
+        "...",
+        "NMI",
+        "INIT",
+        "...",
+        "ExtINT"
+    };
+    return str[dm];
+}
+
+static void dump_apic_lvt(const char *name, uint32_t lvt, bool is_timer)
+{
+    uint32_t dm = (lvt & APIC_LVT_DELIV_MOD) >> APIC_LVT_DELIV_MOD_SHIFT;
+    qemu_printf("%s\t 0x%08x %s %-5s %-6s %-7s %-12s %-6s",
+                name, lvt,
+                lvt & APIC_LVT_INT_POLARITY ? "active-lo" : "active-hi",
+                lvt & APIC_LVT_LEVEL_TRIGGER ? "level" : "edge",
+                lvt & APIC_LVT_MASKED ? "masked" : "",
+                lvt & APIC_LVT_DELIV_STS ? "pending" : "",
+                !is_timer ?
+                    "" : lvt & APIC_LVT_TIMER_PERIODIC ?
+                            "periodic" : lvt & APIC_LVT_TIMER_TSCDEADLINE ?
+                                            "tsc-deadline" : "one-shot",
+                dm2str(dm));
+    if (dm != APIC_DM_NMI) {
+        qemu_printf(" (vec %u)\n", lvt & APIC_VECTOR_MASK);
+    } else {
+        qemu_printf("\n");
+    }
+}
+
+/* ARRAY_SIZE check is not required because
+ * destination shorthand has a size of 2 bit.
+ */
+static inline const char *shorthand2str(uint32_t shorthand)
+{
+    const char *str[] = {
+        "no-shorthand", "self", "all-self", "all"
+    };
+    return str[shorthand];
+}
+
+static inline uint8_t divider_conf(uint32_t divide_conf)
+{
+    uint8_t divide_val = ((divide_conf & 0x8) >> 1) | (divide_conf & 0x3);
+
+    return divide_val == 7 ? 1 : 2 << divide_val;
+}
+
+static inline void mask2str(char *str, uint32_t val, uint8_t size)
+{
+    while (size--) {
+        *str++ = (val >> size) & 1 ? '1' : '0';
+    }
+    *str = 0;
+}
+
+#define MAX_LOGICAL_APIC_ID_MASK_SIZE 16
+
+static void dump_apic_icr(APICCommonState *s, CPUX86State *env)
+{
+    uint32_t icr = s->icr[0], icr2 = s->icr[1];
+    uint8_t dest_shorthand = \
+        (icr & APIC_ICR_DEST_SHORT) >> APIC_ICR_DEST_SHORT_SHIFT;
+    bool logical_mod = icr & APIC_ICR_DEST_MOD;
+    char apic_id_str[MAX_LOGICAL_APIC_ID_MASK_SIZE + 1];
+    uint32_t dest_field;
+    bool x2apic;
+
+    qemu_printf("ICR\t 0x%08x %s %s %s %s\n",
+                icr,
+                logical_mod ? "logical" : "physical",
+                icr & APIC_ICR_TRIGGER_MOD ? "level" : "edge",
+                icr & APIC_ICR_LEVEL ? "assert" : "de-assert",
+                shorthand2str(dest_shorthand));
+
+    qemu_printf("ICR2\t 0x%08x", icr2);
+    if (dest_shorthand != 0) {
+        qemu_printf("\n");
+        return;
+    }
+    x2apic = env->features[FEAT_1_ECX] & CPUID_EXT_X2APIC;
+    dest_field = x2apic ? icr2 : icr2 >> APIC_ICR_DEST_SHIFT;
+
+    if (!logical_mod) {
+        if (x2apic) {
+            qemu_printf(" cpu %u (X2APIC ID)\n", dest_field);
+        } else {
+            qemu_printf(" cpu %u (APIC ID)\n",
+                        dest_field & APIC_LOGDEST_XAPIC_ID);
+        }
+        return;
+    }
+
+    if (s->dest_mode == 0xf) { /* flat mode */
+        mask2str(apic_id_str, icr2 >> APIC_ICR_DEST_SHIFT, 8);
+        qemu_printf(" mask %s (APIC ID)\n", apic_id_str);
+    } else if (s->dest_mode == 0) { /* cluster mode */
+        if (x2apic) {
+            mask2str(apic_id_str, dest_field & APIC_LOGDEST_X2APIC_ID, 16);
+            qemu_printf(" cluster %u mask %s (X2APIC ID)\n",
+                        dest_field >> APIC_LOGDEST_X2APIC_SHIFT, apic_id_str);
+        } else {
+            mask2str(apic_id_str, dest_field & APIC_LOGDEST_XAPIC_ID, 4);
+            qemu_printf(" cluster %u mask %s (APIC ID)\n",
+                        dest_field >> APIC_LOGDEST_XAPIC_SHIFT, apic_id_str);
+        }
+    }
+}
+
+static void dump_apic_interrupt(const char *name, uint32_t *ireg_tab,
+                                uint32_t *tmr_tab)
+{
+    int i, empty = true;
+
+    qemu_printf("%s\t ", name);
+    for (i = 0; i < 256; i++) {
+        if (apic_get_bit(ireg_tab, i)) {
+            qemu_printf("%u%s ", i,
+                        apic_get_bit(tmr_tab, i) ? "(level)" : "");
+            empty = false;
+        }
+    }
+    qemu_printf("%s\n", empty ? "(none)" : "");
+}
+
+void x86_cpu_dump_local_apic_state(CPUState *cs, int flags)
+{
+    X86CPU *cpu = X86_CPU(cs);
+    APICCommonState *s = APIC_COMMON(cpu->apic_state);
+    if (!s) {
+        qemu_printf("local apic state not available\n");
+        return;
+    }
+    uint32_t *lvt = s->lvt;
+
+    qemu_printf("dumping local APIC state for CPU %-2u\n\n",
+                CPU(cpu)->cpu_index);
+    dump_apic_lvt("LVT0", lvt[APIC_LVT_LINT0], false);
+    dump_apic_lvt("LVT1", lvt[APIC_LVT_LINT1], false);
+    dump_apic_lvt("LVTPC", lvt[APIC_LVT_PERFORM], false);
+    dump_apic_lvt("LVTERR", lvt[APIC_LVT_ERROR], false);
+    dump_apic_lvt("LVTTHMR", lvt[APIC_LVT_THERMAL], false);
+    dump_apic_lvt("LVTT", lvt[APIC_LVT_TIMER], true);
+
+    qemu_printf("Timer\t DCR=0x%x (divide by %u) initial_count = %u"
+                " current_count = %u\n",
+                s->divide_conf & APIC_DCR_MASK,
+                divider_conf(s->divide_conf),
+                s->initial_count, apic_get_current_count(s));
+
+    qemu_printf("SPIV\t 0x%08x APIC %s, focus=%s, spurious vec %u\n",
+                s->spurious_vec,
+                s->spurious_vec & APIC_SPURIO_ENABLED ? "enabled" : "disabled",
+                s->spurious_vec & APIC_SPURIO_FOCUS ? "on" : "off",
+                s->spurious_vec & APIC_VECTOR_MASK);
+
+    dump_apic_icr(s, &cpu->env);
+
+    qemu_printf("ESR\t 0x%08x\n", s->esr);
+
+    dump_apic_interrupt("ISR", s->isr, s->tmr);
+    dump_apic_interrupt("IRR", s->irr, s->tmr);
+
+    qemu_printf("\nAPR 0x%02x TPR 0x%02x DFR 0x%02x LDR 0x%02x",
+                s->arb_id, s->tpr, s->dest_mode, s->log_dest);
+    if (s->dest_mode == 0) {
+        qemu_printf("(cluster %u: id %u)",
+                    s->log_dest >> APIC_LOGDEST_XAPIC_SHIFT,
+                    s->log_dest & APIC_LOGDEST_XAPIC_ID);
+    }
+    qemu_printf(" PPR 0x%02x\n", apic_get_ppr(s));
+}
+#else
+void x86_cpu_dump_local_apic_state(CPUState *cs, int flags)
+{
+}
+#endif /* !CONFIG_USER_ONLY */
+
+#define DUMP_CODE_BYTES_TOTAL    50
+#define DUMP_CODE_BYTES_BACKWARD 20
+
+void x86_cpu_dump_state(CPUState *cs, FILE *f, int flags)
+{
+    X86CPU *cpu = X86_CPU(cs);
+    CPUX86State *env = &cpu->env;
+    int eflags, i, nb;
+    char cc_op_name[32];
+    static const char *seg_name[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
+
+    eflags = cpu_compute_eflags(env);
+#ifdef TARGET_X86_64
+    if (env->hflags & HF_CS64_MASK) {
+        qemu_fprintf(f, "RAX=%016" PRIx64 " RBX=%016" PRIx64 " RCX=%016" PRIx64 " RDX=%016" PRIx64 "\n"
+                     "RSI=%016" PRIx64 " RDI=%016" PRIx64 " RBP=%016" PRIx64 " RSP=%016" PRIx64 "\n"
+                     "R8 =%016" PRIx64 " R9 =%016" PRIx64 " R10=%016" PRIx64 " R11=%016" PRIx64 "\n"
+                     "R12=%016" PRIx64 " R13=%016" PRIx64 " R14=%016" PRIx64 " R15=%016" PRIx64 "\n"
+                     "RIP=%016" PRIx64 " RFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
+                     env->regs[R_EAX],
+                     env->regs[R_EBX],
+                     env->regs[R_ECX],
+                     env->regs[R_EDX],
+                     env->regs[R_ESI],
+                     env->regs[R_EDI],
+                     env->regs[R_EBP],
+                     env->regs[R_ESP],
+                     env->regs[8],
+                     env->regs[9],
+                     env->regs[10],
+                     env->regs[11],
+                     env->regs[12],
+                     env->regs[13],
+                     env->regs[14],
+                     env->regs[15],
+                     env->eip, eflags,
+                     eflags & DF_MASK ? 'D' : '-',
+                     eflags & CC_O ? 'O' : '-',
+                     eflags & CC_S ? 'S' : '-',
+                     eflags & CC_Z ? 'Z' : '-',
+                     eflags & CC_A ? 'A' : '-',
+                     eflags & CC_P ? 'P' : '-',
+                     eflags & CC_C ? 'C' : '-',
+                     env->hflags & HF_CPL_MASK,
+                     (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
+                     (env->a20_mask >> 20) & 1,
+                     (env->hflags >> HF_SMM_SHIFT) & 1,
+                     cs->halted);
+    } else
+#endif
+    {
+        qemu_fprintf(f, "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
+                     "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
+                     "EIP=%08x EFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
+                     (uint32_t)env->regs[R_EAX],
+                     (uint32_t)env->regs[R_EBX],
+                     (uint32_t)env->regs[R_ECX],
+                     (uint32_t)env->regs[R_EDX],
+                     (uint32_t)env->regs[R_ESI],
+                     (uint32_t)env->regs[R_EDI],
+                     (uint32_t)env->regs[R_EBP],
+                     (uint32_t)env->regs[R_ESP],
+                     (uint32_t)env->eip, eflags,
+                     eflags & DF_MASK ? 'D' : '-',
+                     eflags & CC_O ? 'O' : '-',
+                     eflags & CC_S ? 'S' : '-',
+                     eflags & CC_Z ? 'Z' : '-',
+                     eflags & CC_A ? 'A' : '-',
+                     eflags & CC_P ? 'P' : '-',
+                     eflags & CC_C ? 'C' : '-',
+                     env->hflags & HF_CPL_MASK,
+                     (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
+                     (env->a20_mask >> 20) & 1,
+                     (env->hflags >> HF_SMM_SHIFT) & 1,
+                     cs->halted);
+    }
+
+    for(i = 0; i < 6; i++) {
+        cpu_x86_dump_seg_cache(env, f, seg_name[i], &env->segs[i]);
+    }
+    cpu_x86_dump_seg_cache(env, f, "LDT", &env->ldt);
+    cpu_x86_dump_seg_cache(env, f, "TR", &env->tr);
+
+#ifdef TARGET_X86_64
+    if (env->hflags & HF_LMA_MASK) {
+        qemu_fprintf(f, "GDT=     %016" PRIx64 " %08x\n",
+                     env->gdt.base, env->gdt.limit);
+        qemu_fprintf(f, "IDT=     %016" PRIx64 " %08x\n",
+                     env->idt.base, env->idt.limit);
+        qemu_fprintf(f, "CR0=%08x CR2=%016" PRIx64 " CR3=%016" PRIx64 " CR4=%08x\n",
+                     (uint32_t)env->cr[0],
+                     env->cr[2],
+                     env->cr[3],
+                     (uint32_t)env->cr[4]);
+        for(i = 0; i < 4; i++)
+            qemu_fprintf(f, "DR%d=%016" PRIx64 " ", i, env->dr[i]);
+        qemu_fprintf(f, "\nDR6=%016" PRIx64 " DR7=%016" PRIx64 "\n",
+                     env->dr[6], env->dr[7]);
+    } else
+#endif
+    {
+        qemu_fprintf(f, "GDT=     %08x %08x\n",
+                     (uint32_t)env->gdt.base, env->gdt.limit);
+        qemu_fprintf(f, "IDT=     %08x %08x\n",
+                     (uint32_t)env->idt.base, env->idt.limit);
+        qemu_fprintf(f, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n",
+                     (uint32_t)env->cr[0],
+                     (uint32_t)env->cr[2],
+                     (uint32_t)env->cr[3],
+                     (uint32_t)env->cr[4]);
+        for(i = 0; i < 4; i++) {
+            qemu_fprintf(f, "DR%d=" TARGET_FMT_lx " ", i, env->dr[i]);
+        }
+        qemu_fprintf(f, "\nDR6=" TARGET_FMT_lx " DR7=" TARGET_FMT_lx "\n",
+                     env->dr[6], env->dr[7]);
+    }
+    if (flags & CPU_DUMP_CCOP) {
+        if ((unsigned)env->cc_op < CC_OP_NB)
+            snprintf(cc_op_name, sizeof(cc_op_name), "%s", cc_op_str[env->cc_op]);
+        else
+            snprintf(cc_op_name, sizeof(cc_op_name), "[%d]", env->cc_op);
+#ifdef TARGET_X86_64
+        if (env->hflags & HF_CS64_MASK) {
+            qemu_fprintf(f, "CCS=%016" PRIx64 " CCD=%016" PRIx64 " CCO=%-8s\n",
+                         env->cc_src, env->cc_dst,
+                         cc_op_name);
+        } else
+#endif
+        {
+            qemu_fprintf(f, "CCS=%08x CCD=%08x CCO=%-8s\n",
+                         (uint32_t)env->cc_src, (uint32_t)env->cc_dst,
+                         cc_op_name);
+        }
+    }
+    qemu_fprintf(f, "EFER=%016" PRIx64 "\n", env->efer);
+    if (flags & CPU_DUMP_FPU) {
+        int fptag;
+        fptag = 0;
+        for(i = 0; i < 8; i++) {
+            fptag |= ((!env->fptags[i]) << i);
+        }
+        update_mxcsr_from_sse_status(env);
+        qemu_fprintf(f, "FCW=%04x FSW=%04x [ST=%d] FTW=%02x MXCSR=%08x\n",
+                     env->fpuc,
+                     (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11,
+                     env->fpstt,
+                     fptag,
+                     env->mxcsr);
+        for(i=0;i<8;i++) {
+            CPU_LDoubleU u;
+            u.d = env->fpregs[i].d;
+            qemu_fprintf(f, "FPR%d=%016" PRIx64 " %04x",
+                         i, u.l.lower, u.l.upper);
+            if ((i & 1) == 1)
+                qemu_fprintf(f, "\n");
+            else
+                qemu_fprintf(f, " ");
+        }
+        if (env->hflags & HF_CS64_MASK)
+            nb = 16;
+        else
+            nb = 8;
+        for(i=0;i<nb;i++) {
+            qemu_fprintf(f, "XMM%02d=%08x%08x%08x%08x",
+                         i,
+                         env->xmm_regs[i].ZMM_L(3),
+                         env->xmm_regs[i].ZMM_L(2),
+                         env->xmm_regs[i].ZMM_L(1),
+                         env->xmm_regs[i].ZMM_L(0));
+            if ((i & 1) == 1)
+                qemu_fprintf(f, "\n");
+            else
+                qemu_fprintf(f, " ");
+        }
+    }
+    if (flags & CPU_DUMP_CODE) {
+        target_ulong base = env->segs[R_CS].base + env->eip;
+        target_ulong offs = MIN(env->eip, DUMP_CODE_BYTES_BACKWARD);
+        uint8_t code;
+        char codestr[3];
+
+        qemu_fprintf(f, "Code=");
+        for (i = 0; i < DUMP_CODE_BYTES_TOTAL; i++) {
+            if (cpu_memory_rw_debug(cs, base - offs + i, &code, 1, 0) == 0) {
+                snprintf(codestr, sizeof(codestr), "%02x", code);
+            } else {
+                snprintf(codestr, sizeof(codestr), "??");
+            }
+            qemu_fprintf(f, "%s%s%s%s", i > 0 ? " " : "",
+                         i == offs ? "<" : "", codestr, i == offs ? ">" : "");
+        }
+        qemu_fprintf(f, "\n");
+    }
+}
+
target/i386/cpu.h +1 -0
index 9ecda75aec..d6ed45c5d7 100644
@@ -2232,6 +2232,7 @@ void enable_compat_apic_id_mode(void);
 #define APIC_DEFAULT_ADDRESS 0xfee00000
 #define APIC_SPACE_SIZE      0x100000
 
+/* cpu-dump.c */
 void x86_cpu_dump_local_apic_state(CPUState *cs, int flags);
 
 /* cpu.c */
target/i386/helper.c +0 -514

526 lines not on this page · raw message

target/i386/meson.build +1 -0
index 750471c9f3..c4bf20b319 100644
@@ -4,6 +4,7 @@ i386_ss.add(files(
   'gdbstub.c',
   'helper.c',
   'xsave_helper.c',
+  'cpu-dump.c',
 ))
 i386_ss.add(when: 'CONFIG_SEV', if_true: files('sev.c'), if_false: files('sev-stub.c'))
 
Signature · 1 line
2.26.2
#8 lore raw

RFC v3 7/9 i386: move TCG cpu class initialization out of helper.c

In reply to Claudio Fontana
---
 target/i386/cpu.c             |  33 ++++------
 target/i386/cpu.h             |  97 ++---------------------------
 target/i386/helper-tcg.h      | 112 ++++++++++++++++++++++++++++++++++
 target/i386/helper.c          |  23 -------
 target/i386/meson.build       |   1 +
 target/i386/tcg-cpu.c         |  71 +++++++++++++++++++++
 target/i386/tcg-cpu.h         |  15 +++++
 target/i386/tcg/bpt_helper.c  |   1 +
 target/i386/tcg/cc_helper.c   |   1 +
 target/i386/tcg/excp_helper.c |   1 +
 target/i386/tcg/fpu_helper.c  |  33 +++++-----
 target/i386/tcg/int_helper.c  |   1 +
 target/i386/tcg/mem_helper.c  |   1 +
 target/i386/tcg/misc_helper.c |   1 +
 target/i386/tcg/mpx_helper.c  |   1 +
 target/i386/tcg/seg_helper.c  |   1 +
 target/i386/tcg/smm_helper.c  |   2 +
 target/i386/tcg/svm_helper.c  |   1 +
 target/i386/tcg/translate.c   |   1 +
 19 files changed, 244 insertions(+), 153 deletions(-)
 create mode 100644 target/i386/helper-tcg.h
 create mode 100644 target/i386/tcg-cpu.c
 create mode 100644 target/i386/tcg-cpu.h
target/i386/cpu.c +11 -22
index b9bd249c8f..3462d0143f 100644
@@ -24,6 +24,8 @@
 #include "qemu/qemu-print.h"
 
 #include "cpu.h"
+#include "tcg-cpu.h"
+#include "helper-tcg.h"
 #include "exec/exec-all.h"
 #include "sysemu/kvm.h"
 #include "sysemu/reset.h"
@@ -1495,7 +1497,8 @@ static inline uint64_t x86_cpu_xsave_components(X86CPU *cpu)
            cpu->env.features[FEAT_XSAVE_COMP_LO];
 }
 
-const char *get_register_name_32(unsigned int reg)
+/* Return name of 32-bit register, from a R_* constant */
+static const char *get_register_name_32(unsigned int reg)
 {
     if (reg >= CPU_NB_REGS32) {
         return NULL;
@@ -7012,13 +7015,6 @@ static void x86_cpu_set_pc(CPUState *cs, vaddr value)
     cpu->env.eip = value;
 }
 
-static void x86_cpu_synchronize_from_tb(CPUState *cs, TranslationBlock *tb)
-{
-    X86CPU *cpu = X86_CPU(cs);
-
-    cpu->env.eip = tb->pc - tb->cs_base;
-}
-
 int x86_cpu_pending_interrupt(CPUState *cs, int interrupt_request)
 {
     X86CPU *cpu = X86_CPU(cs);
@@ -7252,17 +7248,18 @@ static void x86_cpu_common_class_init(ObjectClass *oc, void *data)
     cc->class_by_name = x86_cpu_class_by_name;
     cc->parse_features = x86_cpu_parse_featurestr;
     cc->has_work = x86_cpu_has_work;
+
 #ifdef CONFIG_TCG
-    cc->do_interrupt = x86_cpu_do_interrupt;
-    cc->cpu_exec_interrupt = x86_cpu_exec_interrupt;
-#endif
+    tcg_cpu_common_class_init(cc);
+#endif /* CONFIG_TCG */
+
     cc->dump_state = x86_cpu_dump_state;
     cc->set_pc = x86_cpu_set_pc;
-    cc->synchronize_from_tb = x86_cpu_synchronize_from_tb;
     cc->gdb_read_register = x86_cpu_gdb_read_register;
     cc->gdb_write_register = x86_cpu_gdb_write_register;
     cc->get_arch_id = x86_cpu_get_arch_id;
     cc->get_paging_enabled = x86_cpu_get_paging_enabled;
+
 #ifndef CONFIG_USER_ONLY
     cc->asidx_from_attrs = x86_asidx_from_attrs;
     cc->get_memory_mapping = x86_cpu_get_memory_mapping;
@@ -7273,7 +7270,8 @@ static void x86_cpu_common_class_init(ObjectClass *oc, void *data)
     cc->write_elf32_note = x86_cpu_write_elf32_note;
     cc->write_elf32_qemunote = x86_cpu_write_elf32_qemunote;
     cc->vmsd = &vmstate_x86_cpu;
-#endif
+#endif /* !CONFIG_USER_ONLY */
+
     cc->gdb_arch_name = x86_gdb_arch_name;
 #ifdef TARGET_X86_64
     cc->gdb_core_xml_file = "i386-64bit.xml";
@@ -7281,15 +7279,6 @@ static void x86_cpu_common_class_init(ObjectClass *oc, void *data)
 #else
     cc->gdb_core_xml_file = "i386-32bit.xml";
     cc->gdb_num_core_regs = 50;
-#endif
-#if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
-    cc->debug_excp_handler = breakpoint_handler;
-#endif
-    cc->cpu_exec_enter = x86_cpu_exec_enter;
-    cc->cpu_exec_exit = x86_cpu_exec_exit;
-#ifdef CONFIG_TCG
-    cc->tcg_initialize = tcg_x86_init;
-    cc->tlb_fill = x86_cpu_tlb_fill;
 #endif
     cc->disas_set_info = x86_disas_set_info;
 
target/i386/cpu.h +6 -91

169 lines not on this page · raw message

target/i386/helper-tcg.h new file +112 -0

112 lines not on this page · raw message

target/i386/helper.c +0 -23
index 6e7e0f507c..6bb0c53182 100644
@@ -24,10 +24,8 @@
 #include "sysemu/runstate.h"
 #include "kvm/kvm_i386.h"
 #ifndef CONFIG_USER_ONLY
-#include "sysemu/tcg.h"
 #include "sysemu/hw_accel.h"
 #include "monitor/monitor.h"
-#include "hw/i386/apic_internal.h"
 #endif
 
 void cpu_sync_bndcs_hflags(CPUX86State *env)
@@ -572,27 +570,6 @@ void do_cpu_sipi(X86CPU *cpu)
 }
 #endif
 
-/* Frob eflags into and out of the CPU temporary format.  */
-
-void x86_cpu_exec_enter(CPUState *cs)
-{
-    X86CPU *cpu = X86_CPU(cs);
-    CPUX86State *env = &cpu->env;
-
-    CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
-    env->df = 1 - (2 * ((env->eflags >> 10) & 1));
-    CC_OP = CC_OP_EFLAGS;
-    env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
-}
-
-void x86_cpu_exec_exit(CPUState *cs)
-{
-    X86CPU *cpu = X86_CPU(cs);
-    CPUX86State *env = &cpu->env;
-
-    env->eflags = cpu_compute_eflags(env);
-}
-
 #ifndef CONFIG_USER_ONLY
 uint8_t x86_ldub_phys(CPUState *cs, hwaddr addr)
 {
target/i386/meson.build +1 -0
index c4bf20b319..9c20208e5a 100644
@@ -6,6 +6,7 @@ i386_ss.add(files(
   'xsave_helper.c',
   'cpu-dump.c',
 ))
+i386_ss.add(when: 'CONFIG_TCG', if_true: files('tcg-cpu.c'))
 i386_ss.add(when: 'CONFIG_SEV', if_true: files('sev.c'), if_false: files('sev-stub.c'))
 
 i386_softmmu_ss = ss.source_set()
target/i386/tcg-cpu.c new file +71 -0

71 lines not on this page · raw message

target/i386/tcg-cpu.h new file +15 -0
new file mode 100644
index 0000000000..81f02e562e
@@ -0,0 +1,15 @@
+/*
+ * i386 TCG CPU class initialization
+ *
+ * Copyright 2020 SUSE LLC
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef TCG_CPU_H
+#define TCG_CPU_H
+
+void tcg_cpu_common_class_init(CPUClass *cc);
+
+#endif /* TCG_CPU_H */
target/i386/tcg/bpt_helper.c +1 -0
index e6cc2921e2..979230ac12 100644
@@ -21,6 +21,7 @@
 #include "cpu.h"
 #include "exec/exec-all.h"
 #include "exec/helper-proto.h"
+#include "helper-tcg.h"
 
 
 #ifndef CONFIG_USER_ONLY
target/i386/tcg/cc_helper.c +1 -0
index 924dd3cd57..cc7ea9e8b9 100644
@@ -20,6 +20,7 @@
 #include "qemu/osdep.h"
 #include "cpu.h"
 #include "exec/helper-proto.h"
+#include "helper-tcg.h"
 
 const uint8_t parity_table[256] = {
     CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
target/i386/tcg/excp_helper.c +1 -0
index 191471749f..a0f44431fe 100644
@@ -23,6 +23,7 @@
 #include "qemu/log.h"
 #include "sysemu/runstate.h"
 #include "exec/helper-proto.h"
+#include "helper-tcg.h"
 
 void helper_raise_interrupt(CPUX86State *env, int intno, int next_eip_addend)
 {
target/i386/tcg/fpu_helper.c +16 -17
index 03b35443a6..13f31b6ac7 100644
@@ -26,6 +26,7 @@
 #include "exec/cpu_ldst.h"
 #include "fpu/softfloat.h"
 #include "fpu/softfloat-macros.h"
+#include "helper-tcg.h"
 
 #ifdef CONFIG_SOFTMMU
 #include "hw/irq.h"
@@ -2986,23 +2987,21 @@ void update_mxcsr_status(CPUX86State *env)
 
 void update_mxcsr_from_sse_status(CPUX86State *env)
 {
-    if (tcg_enabled()) {
-        uint8_t flags = get_float_exception_flags(&env->sse_status);
-        /*
-         * The MXCSR denormal flag has opposite semantics to
-         * float_flag_input_denormal (the softfloat code sets that flag
-         * only when flushing input denormals to zero, but SSE sets it
-         * only when not flushing them to zero), so is not converted
-         * here.
-         */
-        env->mxcsr |= ((flags & float_flag_invalid ? FPUS_IE : 0) |
-                       (flags & float_flag_divbyzero ? FPUS_ZE : 0) |
-                       (flags & float_flag_overflow ? FPUS_OE : 0) |
-                       (flags & float_flag_underflow ? FPUS_UE : 0) |
-                       (flags & float_flag_inexact ? FPUS_PE : 0) |
-                       (flags & float_flag_output_denormal ? FPUS_UE | FPUS_PE :
-                        0));
-    }
+    uint8_t flags = get_float_exception_flags(&env->sse_status);
+    /*
+     * The MXCSR denormal flag has opposite semantics to
+     * float_flag_input_denormal (the softfloat code sets that flag
+     * only when flushing input denormals to zero, but SSE sets it
+     * only when not flushing them to zero), so is not converted
+     * here.
+     */
+    env->mxcsr |= ((flags & float_flag_invalid ? FPUS_IE : 0) |
+                   (flags & float_flag_divbyzero ? FPUS_ZE : 0) |
+                   (flags & float_flag_overflow ? FPUS_OE : 0) |
+                   (flags & float_flag_underflow ? FPUS_UE : 0) |
+                   (flags & float_flag_inexact ? FPUS_PE : 0) |
+                   (flags & float_flag_output_denormal ? FPUS_UE | FPUS_PE :
+                    0));
 }
 
 void helper_update_mxcsr(CPUX86State *env)
target/i386/tcg/int_helper.c +1 -0
index 4f89436b53..87fa7280ee 100644
@@ -24,6 +24,7 @@
 #include "exec/helper-proto.h"
 #include "qapi/error.h"
 #include "qemu/guest-random.h"
+#include "helper-tcg.h"
 
 //#define DEBUG_MULDIV
 
target/i386/tcg/mem_helper.c +1 -0
index 21ca3e3e88..e5cd2de1bf 100644
@@ -25,6 +25,7 @@
 #include "qemu/int128.h"
 #include "qemu/atomic128.h"
 #include "tcg/tcg.h"
+#include "helper-tcg.h"
 
 void helper_cmpxchg8b_unlocked(CPUX86State *env, target_ulong a0)
 {
target/i386/tcg/misc_helper.c +1 -0
index ae259d9145..c99370e5e3 100644
@@ -24,6 +24,7 @@
 #include "exec/exec-all.h"
 #include "exec/cpu_ldst.h"
 #include "exec/address-spaces.h"
+#include "helper-tcg.h"
 
 void helper_outb(CPUX86State *env, uint32_t port, uint32_t data)
 {
target/i386/tcg/mpx_helper.c +1 -0
index fd966174b4..22423eedcd 100644
@@ -22,6 +22,7 @@
 #include "exec/helper-proto.h"
 #include "exec/cpu_ldst.h"
 #include "exec/exec-all.h"
+#include "helper-tcg.h"
 
 
 void helper_bndck(CPUX86State *env, uint32_t fail)
target/i386/tcg/seg_helper.c +1 -0
index 09b6554660..ed3e04a187 100644
@@ -25,6 +25,7 @@
 #include "exec/exec-all.h"
 #include "exec/cpu_ldst.h"
 #include "exec/log.h"
+#include "helper-tcg.h"
 
 //#define DEBUG_PCALL
 
target/i386/tcg/smm_helper.c +2 -0
index d20e8edfdf..62d027abd3 100644
@@ -22,6 +22,8 @@
 #include "cpu.h"
 #include "exec/helper-proto.h"
 #include "exec/log.h"
+#include "helper-tcg.h"
+
 
 /* SMM support */
 
target/i386/tcg/svm_helper.c +1 -0
index 38931586e5..097bb9b83d 100644
@@ -22,6 +22,7 @@
 #include "exec/helper-proto.h"
 #include "exec/exec-all.h"
 #include "exec/cpu_ldst.h"
+#include "helper-tcg.h"
 
 /* Secure Virtual Machine helpers */
 
target/i386/tcg/translate.c +1 -0
index 4c57307e42..5988ea0289 100644
@@ -28,6 +28,7 @@
 
 #include "exec/helper-proto.h"
 #include "exec/helper-gen.h"
+#include "helper-tcg.h"
 
 #include "trace-tcg.h"
 #include "exec/log.h"
Signature · 1 line
2.26.2
#9 lore raw

RFC v3 8/9 module: introduce MODULE_INIT_ACCEL_CPU

In reply to Claudio Fontana

apply this to the registration of the cpus accel interfaces,

but this will be also in preparation for later use of this new module init step to also defer the registration of the cpu models, in order to make them subclasses of a per-accel cpu type.

---
 accel/kvm/kvm-all.c         | 11 +++++++++--
 accel/qtest/qtest.c         | 10 +++++++++-
 accel/tcg/tcg-all.c         | 11 +++++++++--
 accel/xen/xen-all.c         | 12 +++++++++---
 include/qemu/module.h       |  2 ++
 softmmu/vl.c                |  6 ++++++
 target/i386/hax/hax-all.c   | 12 +++++++++---
 target/i386/hvf/hvf.c       | 10 +++++++++-
 target/i386/whpx/whpx-all.c | 11 +++++++++--
 9 files changed, 71 insertions(+), 14 deletions(-)
accel/kvm/kvm-all.c +9 -2
index 9ef5daf4c5..509b249f52 100644
@@ -2251,8 +2251,6 @@ static int kvm_init(MachineState *ms)
         ret = ram_block_discard_disable(true);
         assert(!ret);
     }
-
-    cpus_register_accel(&kvm_cpus);
     return 0;
 
 err:
@@ -3236,3 +3234,12 @@ static void kvm_type_init(void)
 }
 
 type_init(kvm_type_init);
+
+static void kvm_accel_cpu_init(void)
+{
+    if (kvm_enabled()) {
+        cpus_register_accel(&kvm_cpus);
+    }
+}
+
+accel_cpu_init(kvm_accel_cpu_init);
accel/qtest/qtest.c +9 -1
index b282cea5cf..8d14059e32 100644
@@ -32,7 +32,6 @@ const CpusAccel qtest_cpus = {
 
 static int qtest_init_accel(MachineState *ms)
 {
-    cpus_register_accel(&qtest_cpus);
     return 0;
 }
 
@@ -58,3 +57,12 @@ static void qtest_type_init(void)
 }
 
 type_init(qtest_type_init);
+
+static void qtest_accel_cpu_init(void)
+{
+    if (qtest_enabled()) {
+        cpus_register_accel(&qtest_cpus);
+    }
+}
+
+accel_cpu_init(qtest_accel_cpu_init);
accel/tcg/tcg-all.c +9 -2
index fa1208158f..9ffedc8151 100644
@@ -104,8 +104,6 @@ static int tcg_init(MachineState *ms)
 
     tcg_exec_init(s->tb_size * 1024 * 1024);
     mttcg_enabled = s->mttcg_enabled;
-    cpus_register_accel(&tcg_cpus);
-
     return 0;
 }
 
@@ -201,3 +199,12 @@ static void register_accel_types(void)
 }
 
 type_init(register_accel_types);
+
+static void tcg_accel_cpu_init(void)
+{
+    if (tcg_enabled()) {
+        cpus_register_accel(&tcg_cpus);
+    }
+}
+
+accel_cpu_init(tcg_accel_cpu_init);
accel/xen/xen-all.c +9 -3
index 878a4089d9..6932a9f364 100644
@@ -185,9 +185,6 @@ static int xen_init(MachineState *ms)
      * opt out of system RAM being allocated by generic code
      */
     mc->default_ram_id = NULL;
-
-    cpus_register_accel(&xen_cpus);
-
     return 0;
 }
 
@@ -228,3 +225,12 @@ static void xen_type_init(void)
 }
 
 type_init(xen_type_init);
+
+static void xen_accel_cpu_init(void)
+{
+    if (xen_enabled()) {
+        cpus_register_accel(&xen_cpus);
+    }
+}
+
+accel_cpu_init(xen_accel_cpu_init);
include/qemu/module.h +2 -0
index 944d403cbd..485eda986a 100644
@@ -44,6 +44,7 @@ typedef enum {
     MODULE_INIT_BLOCK,
     MODULE_INIT_OPTS,
     MODULE_INIT_QOM,
+    MODULE_INIT_ACCEL_CPU,
     MODULE_INIT_TRACE,
     MODULE_INIT_XEN_BACKEND,
     MODULE_INIT_LIBQOS,
@@ -54,6 +55,7 @@ typedef enum {
 #define block_init(function) module_init(function, MODULE_INIT_BLOCK)
 #define opts_init(function) module_init(function, MODULE_INIT_OPTS)
 #define type_init(function) module_init(function, MODULE_INIT_QOM)
+#define accel_cpu_init(function) module_init(function, MODULE_INIT_ACCEL_CPU)
 #define trace_init(function) module_init(function, MODULE_INIT_TRACE)
 #define xen_backend_init(function) module_init(function, \
                                                MODULE_INIT_XEN_BACKEND)
softmmu/vl.c +6 -0
index e6e0ad5a92..df4bed056a 100644
@@ -4173,6 +4173,12 @@ void qemu_init(int argc, char **argv, char **envp)
      */
     configure_accelerators(argv[0]);
 
+    /*
+     * accelerator has been chosen and initialized, now it is time to
+     * register the cpu accel interface.
+     */
+    module_call_init(MODULE_INIT_ACCEL_CPU);
+
     /*
      * Beware, QOM objects created before this point miss global and
      * compat properties.
target/i386/hax/hax-all.c +9 -3
index fecfe8cd6e..3bada019f5 100644
@@ -364,9 +364,6 @@ static int hax_accel_init(MachineState *ms)
                 !ret ? "working" : "not working",
                 !ret ? "fast virt" : "emulation");
     }
-    if (ret == 0) {
-        cpus_register_accel(&hax_cpus);
-    }
     return ret;
 }
 
@@ -1141,3 +1138,12 @@ static void hax_type_init(void)
 }
 
 type_init(hax_type_init);
+
+static void hax_accel_cpu_init(void)
+{
+    if (hax_enabled()) {
+        cpus_register_accel(&hax_cpus);
+    }
+}
+
+accel_cpu_init(hax_accel_cpu_init);
target/i386/hvf/hvf.c +9 -1
index ed9356565c..249b77797f 100644
@@ -887,7 +887,6 @@ static int hvf_accel_init(MachineState *ms)
   
     hvf_state = s;
     memory_listener_register(&hvf_memory_listener, &address_space_memory);
-    cpus_register_accel(&hvf_cpus);
     return 0;
 }
 
@@ -911,3 +910,12 @@ static void hvf_type_init(void)
 }
 
 type_init(hvf_type_init);
+
+static void hvf_accel_cpu_init(void)
+{
+    if (hvf_enabled()) {
+        cpus_register_accel(&hvf_cpus);
+    }
+}
+
+accel_cpu_init(hvf_accel_cpu_init);
target/i386/whpx/whpx-all.c +9 -2
index f4f3e33eac..2e715e2bc6 100644
@@ -1642,8 +1642,6 @@ static int whpx_accel_init(MachineState *ms)
 
     whpx_memory_init();
 
-    cpus_register_accel(&whpx_cpus);
-
     printf("Windows Hypervisor Platform accelerator is operational\n");
     return 0;
 
@@ -1713,3 +1711,12 @@ error:
 }
 
 type_init(whpx_type_init);
+
+static void whpx_accel_cpu_init(void)
+{
+    if (whpx_enabled()) {
+        cpus_register_accel(&whpx_cpus);
+    }
+}
+
+accel_cpu_init(whpx_accel_cpu_init);
Signature · 1 line
2.26.2
#10 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 4 lines · Claudio Fontana

apply this to the registration of the cpus accel interfaces,

but this will be also in preparation for later use of this new module init step to also defer the registration of the cpu models,

this is not true anymore, so my commit message here needs fixing.

Quoted 235 lines

in order to make them subclasses of a per-accel cpu type.

---
 accel/kvm/kvm-all.c         | 11 +++++++++--
 accel/qtest/qtest.c         | 10 +++++++++-
 accel/tcg/tcg-all.c         | 11 +++++++++--
 accel/xen/xen-all.c         | 12 +++++++++---
 include/qemu/module.h       |  2 ++
 softmmu/vl.c                |  6 ++++++
 target/i386/hax/hax-all.c   | 12 +++++++++---
 target/i386/hvf/hvf.c       | 10 +++++++++-
 target/i386/whpx/whpx-all.c | 11 +++++++++--
 9 files changed, 71 insertions(+), 14 deletions(-)
accel/kvm/kvm-all.c +9 -2
index 9ef5daf4c5..509b249f52 100644
@@ -2251,8 +2251,6 @@ static int kvm_init(MachineState *ms)
         ret = ram_block_discard_disable(true);
         assert(!ret);
     }
-
-    cpus_register_accel(&kvm_cpus);
     return 0;
 
 err:
@@ -3236,3 +3234,12 @@ static void kvm_type_init(void)
 }
 
 type_init(kvm_type_init);
+
+static void kvm_accel_cpu_init(void)
+{
+    if (kvm_enabled()) {
+        cpus_register_accel(&kvm_cpus);
+    }
+}
+
+accel_cpu_init(kvm_accel_cpu_init);
accel/qtest/qtest.c +9 -1
index b282cea5cf..8d14059e32 100644
@@ -32,7 +32,6 @@ const CpusAccel qtest_cpus = {
 
 static int qtest_init_accel(MachineState *ms)
 {
-    cpus_register_accel(&qtest_cpus);
     return 0;
 }
 
@@ -58,3 +57,12 @@ static void qtest_type_init(void)
 }
 
 type_init(qtest_type_init);
+
+static void qtest_accel_cpu_init(void)
+{
+    if (qtest_enabled()) {
+        cpus_register_accel(&qtest_cpus);
+    }
+}
+
+accel_cpu_init(qtest_accel_cpu_init);
accel/tcg/tcg-all.c +9 -2
index fa1208158f..9ffedc8151 100644
@@ -104,8 +104,6 @@ static int tcg_init(MachineState *ms)
 
     tcg_exec_init(s->tb_size * 1024 * 1024);
     mttcg_enabled = s->mttcg_enabled;
-    cpus_register_accel(&tcg_cpus);
-
     return 0;
 }
 
@@ -201,3 +199,12 @@ static void register_accel_types(void)
 }
 
 type_init(register_accel_types);
+
+static void tcg_accel_cpu_init(void)
+{
+    if (tcg_enabled()) {
+        cpus_register_accel(&tcg_cpus);
+    }
+}
+
+accel_cpu_init(tcg_accel_cpu_init);
accel/xen/xen-all.c +9 -3
index 878a4089d9..6932a9f364 100644
@@ -185,9 +185,6 @@ static int xen_init(MachineState *ms)
      * opt out of system RAM being allocated by generic code
      */
     mc->default_ram_id = NULL;
-
-    cpus_register_accel(&xen_cpus);
-
     return 0;
 }
 
@@ -228,3 +225,12 @@ static void xen_type_init(void)
 }
 
 type_init(xen_type_init);
+
+static void xen_accel_cpu_init(void)
+{
+    if (xen_enabled()) {
+        cpus_register_accel(&xen_cpus);
+    }
+}
+
+accel_cpu_init(xen_accel_cpu_init);
include/qemu/module.h +2 -0
index 944d403cbd..485eda986a 100644
@@ -44,6 +44,7 @@ typedef enum {
     MODULE_INIT_BLOCK,
     MODULE_INIT_OPTS,
     MODULE_INIT_QOM,
+    MODULE_INIT_ACCEL_CPU,
     MODULE_INIT_TRACE,
     MODULE_INIT_XEN_BACKEND,
     MODULE_INIT_LIBQOS,
@@ -54,6 +55,7 @@ typedef enum {
 #define block_init(function) module_init(function, MODULE_INIT_BLOCK)
 #define opts_init(function) module_init(function, MODULE_INIT_OPTS)
 #define type_init(function) module_init(function, MODULE_INIT_QOM)
+#define accel_cpu_init(function) module_init(function, MODULE_INIT_ACCEL_CPU)
 #define trace_init(function) module_init(function, MODULE_INIT_TRACE)
 #define xen_backend_init(function) module_init(function, \
                                                MODULE_INIT_XEN_BACKEND)
softmmu/vl.c +6 -0
index e6e0ad5a92..df4bed056a 100644
@@ -4173,6 +4173,12 @@ void qemu_init(int argc, char **argv, char **envp)
      */
     configure_accelerators(argv[0]);
 
+    /*
+     * accelerator has been chosen and initialized, now it is time to
+     * register the cpu accel interface.
+     */
+    module_call_init(MODULE_INIT_ACCEL_CPU);
+
     /*
      * Beware, QOM objects created before this point miss global and
      * compat properties.
target/i386/hax/hax-all.c +9 -3
index fecfe8cd6e..3bada019f5 100644
@@ -364,9 +364,6 @@ static int hax_accel_init(MachineState *ms)
                 !ret ? "working" : "not working",
                 !ret ? "fast virt" : "emulation");
     }
-    if (ret == 0) {
-        cpus_register_accel(&hax_cpus);
-    }
     return ret;
 }
 
@@ -1141,3 +1138,12 @@ static void hax_type_init(void)
 }
 
 type_init(hax_type_init);
+
+static void hax_accel_cpu_init(void)
+{
+    if (hax_enabled()) {
+        cpus_register_accel(&hax_cpus);
+    }
+}
+
+accel_cpu_init(hax_accel_cpu_init);
target/i386/hvf/hvf.c +9 -1
index ed9356565c..249b77797f 100644
@@ -887,7 +887,6 @@ static int hvf_accel_init(MachineState *ms)
 
     hvf_state = s;
     memory_listener_register(&hvf_memory_listener, &address_space_memory);
-    cpus_register_accel(&hvf_cpus);
     return 0;
 }
 
@@ -911,3 +910,12 @@ static void hvf_type_init(void)
 }
 
 type_init(hvf_type_init);
+
+static void hvf_accel_cpu_init(void)
+{
+    if (hvf_enabled()) {
+        cpus_register_accel(&hvf_cpus);
+    }
+}
+
+accel_cpu_init(hvf_accel_cpu_init);
target/i386/whpx/whpx-all.c +9 -2
index f4f3e33eac..2e715e2bc6 100644
@@ -1642,8 +1642,6 @@ static int whpx_accel_init(MachineState *ms)
 
     whpx_memory_init();
 
-    cpus_register_accel(&whpx_cpus);
-
     printf("Windows Hypervisor Platform accelerator is operational\n");
     return 0;
 
@@ -1713,3 +1711,12 @@ error:
 }
 
 type_init(whpx_type_init);
+
+static void whpx_accel_cpu_init(void)
+{
+    if (whpx_enabled()) {
+        cpus_register_accel(&whpx_cpus);
+    }
+}
+
+accel_cpu_init(whpx_accel_cpu_init);
#11 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

In reply to Claudio Fontana
Quoted 8 lines · Claudio Fontana

apply this to the registration of the cpus accel interfaces,

but this will be also in preparation for later use of this new module init step to also defer the registration of the cpu models, in order to make them subclasses of a per-accel cpu type.

---

[...]

softmmu/vl.c void qemu_init(int argc, char **argv, char **envp) · line 4180

Quoted 5 lines
softmmu/vl.c +5 -0
+    /*
+     * accelerator has been chosen and initialized, now it is time to
+     * register the cpu accel interface.
+     */
+    module_call_init(MODULE_INIT_ACCEL_CPU);

I don't get why we would use a new module initialization level for this. If the accelerator object was already created, we can just ask the existing accel object to do whatever initialization step is necessary.

e.g. we can add a AccelClass.cpu_accel_ops field, and call:

   cpus_register_accel(current_machine->accelerator->cpu_accel_ops);
Signature · 1 line
Eduardo
#12 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 9 lines · Eduardo Habkost
Quoted 8 lines · Claudio Fontana

apply this to the registration of the cpus accel interfaces,

but this will be also in preparation for later use of this new module init step to also defer the registration of the cpu models, in order to make them subclasses of a per-accel cpu type.

---

Quoted 8 lines

[...]

softmmu/vl.c void qemu_init(int argc, char **argv, char **envp) · line 4180

Quoted 5 lines
softmmu/vl.c +5 -0
+    /*
+     * accelerator has been chosen and initialized, now it is time to
+     * register the cpu accel interface.
+     */
+    module_call_init(MODULE_INIT_ACCEL_CPU);

I don't get why we would use a new module initialization level

To have a clear point in time after which all accelerator interface initialization is done. It avoids to have to hunt down the registration points spread around the code base. I'd turn it around, why not?

Quoted 8 lines

for this. If the accelerator object was already created, we can just ask the existing accel object to do whatever initialization step is necessary.

e.g. we can add a AccelClass.cpu_accel_ops field, and call:

   cpus_register_accel(current_machine->accelerator->cpu_accel_ops);

_When_ this is done is the question, in my view, where the call to the registration is placed.

After adding additonal operations that have to be done at "accelerator-chosen" time, it becomes more and more difficult to trace them around the codebase.

Thanks,

Claudio

#13 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 17 lines · Claudio Fontana
Quoted 16 lines · Eduardo Habkost
Quoted 8 lines · Claudio Fontana

apply this to the registration of the cpus accel interfaces,

but this will be also in preparation for later use of this new module init step to also defer the registration of the cpu models, in order to make them subclasses of a per-accel cpu type.

---

[...]

Quoted 5 lines
softmmu/vl.c +5 -0
+    /*
+     * accelerator has been chosen and initialized, now it is time to
+     * register the cpu accel interface.
+     */
+    module_call_init(MODULE_INIT_ACCEL_CPU);
Quoted 5 lines
Quoted 1 line

I don't get why we would use a new module initialization level

To have a clear point in time after which all accelerator interface initialization is done. It avoids to have to hunt down the registration points spread around the code base. I'd turn it around, why not?

I see two disadvantages:

1) you have to hunt down accel_cpu_inits instead of looking at accelerator classes. :)

2) all callbacks have an "if (*_enabled())" around the actual meat. Another related issue is that usually the module_call_init are unconditional.

I think the idea of using module_call_init is good however. What about:

static void kvm_cpu_accel_init(void)

{
    x86_cpu_accel_init(&kvm_cpu_accel);
}

static void kvm_cpu_accel_register(void)

{
    accel_register_call(TYPE_KVM, kvm_cpu_accel_init);
}

accel_cpu_init(kvm_cpu_accel_register);

...

void
accel_register_call(const char *qom_type, void (*fn)(void))

{
    AccelClass *acc = ACCEL_CLASS(object_class_by_name(qom_type));

    acc->setup_calls = g_slist_append(acc->setup_calls, (void *)fn);
}

void
accel_do_call(void *data, void *unused)

{
    void (*fn)(void) = data;

    data();
}

int accel_init_machine(AccelState *accel, MachineState *ms) {
...

    if (ret < 0) {
        ms->accelerator = NULL;
        *(acc->allowed) = false;
        object_unref(OBJECT(accel));
    } else {
        object_set_accelerator_compat_props(acc->compat_props);
        g_slist_foreach(acc->setup_calls, accel_do_call, NULL);
    }
    return ret;
}

where the module_call_init would be right after MODULE_INIT_QOM

Paolo

Quoted 20 lines
Quoted 8 lines

for this. If the accelerator object was already created, we can just ask the existing accel object to do whatever initialization step is necessary.

e.g. we can add a AccelClass.cpu_accel_ops field, and call:

    cpus_register_accel(current_machine->accelerator->cpu_accel_ops);

_When_ this is done is the question, in my view, where the call to the registration is placed.

After adding additonal operations that have to be done at "accelerator-chosen" time, it becomes more and more difficult to trace them around the codebase.

Thanks,

Claudio

#14 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 29 lines · Paolo Bonzini
Quoted 17 lines · Claudio Fontana
Quoted 16 lines · Eduardo Habkost
Quoted 8 lines · Claudio Fontana

apply this to the registration of the cpus accel interfaces,

but this will be also in preparation for later use of this new module init step to also defer the registration of the cpu models, in order to make them subclasses of a per-accel cpu type.

---

[...]

Quoted 5 lines
softmmu/vl.c +5 -0
+    /*
+     * accelerator has been chosen and initialized, now it is time to
+     * register the cpu accel interface.
+     */
+    module_call_init(MODULE_INIT_ACCEL_CPU);
Quoted 5 lines
Quoted 1 line

I don't get why we would use a new module initialization level

To have a clear point in time after which all accelerator interface initialization is done. It avoids to have to hunt down the registration points spread around the code base. I'd turn it around, why not?

I see two disadvantages:

1) you have to hunt down accel_cpu_inits instead of looking at accelerator classes. :)

Quoted 8 lines

2) all callbacks have an "if (*_enabled())" around the actual meat. Another related issue is that usually the module_call_init are unconditional.

I think the idea of using module_call_init is good however. What about:

static void kvm_cpu_accel_init(void)

{
    x86_cpu_accel_init(&kvm_cpu_accel);

What do you expect x86_cpu_accel_init() to do?

Quoted 29 lines

}

static void kvm_cpu_accel_register(void)

{
    accel_register_call(TYPE_KVM, kvm_cpu_accel_init);
}

accel_cpu_init(kvm_cpu_accel_register);

...

void
accel_register_call(const char *qom_type, void (*fn)(void))

{
    AccelClass *acc = ACCEL_CLASS(object_class_by_name(qom_type));

    acc->setup_calls = g_slist_append(acc->setup_calls, (void *)fn);
}

void
accel_do_call(void *data, void *unused)

{
    void (*fn)(void) = data;

    data();
}

int accel_init_machine(AccelState *accel, MachineState *ms) {
...

Quoted 7 lines
    if (ret < 0) {
        ms->accelerator = NULL;
        *(acc->allowed) = false;
        object_unref(OBJECT(accel));
    } else {
        object_set_accelerator_compat_props(acc->compat_props);
        g_slist_foreach(acc->setup_calls, accel_do_call, NULL);

Why all this extra complexity if you can simply do:

  ACCEL_GET_CLASS(acc)->finish_arch_specific_init();

?

Quoted 4 lines
    }
    return ret;
}
Quoted 18 lines

where the module_call_init would be right after MODULE_INIT_QOM

Paolo

Quoted 14 lines
Quoted 8 lines

for this. If the accelerator object was already created, we can just ask the existing accel object to do whatever initialization step is necessary.

e.g. we can add a AccelClass.cpu_accel_ops field, and call:

    cpus_register_accel(current_machine->accelerator->cpu_accel_ops);

_When_ this is done is the question, in my view, where the call to the registration is placed.

After adding additonal operations that have to be done at "accelerator-chosen" time, it becomes more and more difficult to trace them around the codebase.

I don't understand why a separate module init level is necessary here.

Making sure module_call_init() is called at the correct moment is not easier or safer than just making sure accel_init_machine() (or another init function you create) is called at the correct moment.

Signature · 1 line
Eduardo
#15 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 17 lines · Eduardo Habkost
Quoted 16 lines · Paolo Bonzini
Quoted 6 lines · Claudio Fontana
Quoted 1 line · Eduardo Habkost

I don't get why we would use a new module initialization level

To have a clear point in time after which all accelerator interface initialization is done. It avoids to have to hunt down the registration points spread around the code base. I'd turn it around, why not?

I see two disadvantages:

1) you have to hunt down accel_cpu_inits instead of looking at accelerator classes. :)

2) all callbacks have an "if (*_enabled())" around the actual meat. Another related issue is that usually the module_call_init are unconditional.

Quoted 7 lines
Quoted 5 lines

I think the idea of using module_call_init is good however. What about:

static void kvm_cpu_accel_init(void)

{
     x86_cpu_accel_init(&kvm_cpu_accel);

What do you expect x86_cpu_accel_init() to do?

I don't know, the same that it was doing in Claudio's patches. :)

He had

	if (kvm_enabled()) {
	    x86_cpu_accel_init(&kvm_cpu_accel);
	}

and I'm calling only the function that is registered on the enabled accelerator.

Quoted 2 lines

I don't understand why a separate module init level is necessary here.

Because you must call accel_register_call after the TYPE_KVM type has been registered, or object_class_by_name fails:

void
accel_register_call(const char *qom_type, void (*fn)(void))

{
    AccelClass *acc = ACCEL_CLASS(object_class_by_name(qom_type));

    acc->setup_calls = g_slist_append(acc->setup_calls, (void *)fn);
}

The alternative is to store the (type, function) tuple directly, with the type as a string. Then you can just use type_init.

Quoted 4 lines

Making sure module_call_init() is called at the correct moment is not easier or safer than just making sure accel_init_machine() (or another init function you create) is called at the correct moment.

Since there is a way to do it without a new level, that would of course be fine for me too. Let me explain however why I think Claudio's design had module_call_init() misplaced and what the fundamental difference is.
The basic phases in qemu_init() are:

- initialize stuff
- parse command line
- create machine
- create accelerator
- initialize machine
- create devices
- start

with a mess of other object creation sprinkled between the various phases (but we don't care about those).

What I object to, is calling module_call_init() after the "initialize stuff" phase. Claudio was using it to call the function directly, so it had to be exactly at "create accelerator". This is different from all other module_call_init() calls, which are done very early.

With the implementation I sketched, accel_register_call must still be done after type_init, so there's still an ordering constraint, but all it's doing is registering a callback in the "initialize stuff" phase.

Thanks,

Paolo

#16 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 45 lines · Paolo Bonzini
Quoted 17 lines · Eduardo Habkost
Quoted 16 lines · Paolo Bonzini
Quoted 6 lines · Claudio Fontana
Quoted 1 line · Eduardo Habkost

I don't get why we would use a new module initialization level

To have a clear point in time after which all accelerator interface initialization is done. It avoids to have to hunt down the registration points spread around the code base. I'd turn it around, why not?

I see two disadvantages:

1) you have to hunt down accel_cpu_inits instead of looking at accelerator classes. :)

2) all callbacks have an "if (*_enabled())" around the actual meat. Another related issue is that usually the module_call_init are unconditional.

Quoted 7 lines
Quoted 5 lines

I think the idea of using module_call_init is good however. What about:

static void kvm_cpu_accel_init(void)

{
     x86_cpu_accel_init(&kvm_cpu_accel);

What do you expect x86_cpu_accel_init() to do?

I don't know, the same that it was doing in Claudio's patches. :)

He had

	if (kvm_enabled()) {
	    x86_cpu_accel_init(&kvm_cpu_accel);
	}

and I'm calling only the function that is registered on the enabled accelerator.

Quoted 2 lines

I don't understand why a separate module init level is necessary here.

Because you must call accel_register_call after the TYPE_KVM type has been registered, or object_class_by_name fails:

void
accel_register_call(const char *qom_type, void (*fn)(void))

Quoted 8 lines
{
    AccelClass *acc = ACCEL_CLASS(object_class_by_name(qom_type));

    acc->setup_calls = g_slist_append(acc->setup_calls, (void *)fn);
}

The alternative is to store the (type, function) tuple directly, with the type as a string. Then you can just use type_init.

Right. Let's build on top of that:

Another alternative would be to store a (type, X86CPUAccel) tuple directly, with the type as string. This would save the extra indirection of the x86_cpu_accel_init() call.

It turns out we already have a mechanism to register and store (type, StructContainingFunctionPointers) tuples at initialization
time: QOM.

X86CPUAccel can become X86CPUAccelClass, and be registered as a QOM type. It could be a subtype of TYPE_ACCEL or not, it shouldn't matter.

I remember this was suggested in a previous thread, but I don't remember if there were any objections.

Quoted 19 lines
Quoted 5 lines

Making sure module_call_init() is called at the correct moment is not easier or safer than just making sure accel_init_machine() (or another init function you create) is called at the correct moment.

Since there is a way to do it without a new level, that would of course be fine for me too. Let me explain however why I think Claudio's design had module_call_init() misplaced and what the fundamental difference is. The basic phases in qemu_init() are:

- initialize stuff
- parse command line
- create machine
- create accelerator
- initialize machine
- create devices
- start

Quoted 7 lines

with a mess of other object creation sprinkled between the various phases (but we don't care about those).

What I object to, is calling module_call_init() after the "initialize stuff" phase. Claudio was using it to call the function directly, so it had to be exactly at "create accelerator". This is different from all other module_call_init() calls, which are done very early.

I agree.

Quoted 4 lines

With the implementation I sketched, accel_register_call must still be done after type_init, so there's still an ordering constraint, but all it's doing is registering a callback in the "initialize stuff" phase.

Makes sense, if we really want to introduce a new accel_register_call() abstraction. I don't think we need it, though.

Signature · 1 line
Eduardo
#17 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Il mer 18 nov 2020, 16:26 Eduardo Habkost <[email protected]> ha scritto:

Quoted 10 lines
Quoted 3 lines

The alternative is to store the (type, function) tuple directly, with the type as a string. Then you can just use type_init.

Right. Let's build on top of that:

Another alternative would be to store a (type, X86CPUAccel) tuple directly, with the type as string. This would save the extra indirection of the x86_cpu_accel_init() call.

Quoted 8 lines

It turns out we already have a mechanism to register and store (type, StructContainingFunctionPointers) tuples at initialization
time: QOM.

X86CPUAccel can become X86CPUAccelClass, and be registered as a QOM type. It could be a subtype of TYPE_ACCEL or not, it shouldn't matter.

It would be a weird type that isn't instantiated, and/or that does nothing but monkey patching other classes. I don't think it's a good fit.

Yet another possibility is to use GHashTable. It is limited to one value per key, but it's enough if everything is kept local to {hw,target}/i386. If needed a new function pointer can be added to MachineClass, implemented in X86MachineState (where the GHashTable would also be) and called in accel.c.

Paolo

Paolo

Quoted 49 lines

I remember this was suggested in a previous thread, but I don't remember if there were any objections.

Quoted 7 lines
Quoted 5 lines

Making sure module_call_init() is called at the correct moment is not easier or safer than just making sure accel_init_machine() (or another init function you create) is called at the correct moment.

Since there is a way to do it without a new level, that would of course

be

Quoted 12 lines

fine for me too. Let me explain however why I think Claudio's design had module_call_init() misplaced and what the fundamental difference is. The basic phases in qemu_init() are:

- initialize stuff
- parse command line
- create machine
- create accelerator
- initialize machine
- create devices
- start

Quoted 4 lines

with a mess of other object creation sprinkled between the various phases (but we don't care about those).

What I object to, is calling module_call_init() after the "initialize

stuff"

Quoted 1 line

phase. Claudio was using it to call the function directly, so it had to

be

Quoted 2 lines

exactly at "create accelerator". This is different from all other module_call_init() calls, which are done very early.

I agree.

Quoted 2 lines

With the implementation I sketched, accel_register_call must still be

done

Quoted 1 line

after type_init, so there's still an ordering constraint, but all it's

doing

Quoted 1 line

is registering a callback in the "initialize stuff" phase.

Makes sense, if we really want to introduce a new accel_register_call() abstraction. I don't think we need it, though.

--
Eduardo

#18 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 16 lines · Paolo Bonzini

Il mer 18 nov 2020, 16:26 Eduardo Habkost <[email protected]> ha scritto:

Quoted 14 lines
Quoted 3 lines

The alternative is to store the (type, function) tuple directly, with the type as a string. Then you can just use type_init.

Right. Let's build on top of that:

Another alternative would be to store a (type, X86CPUAccel) tuple directly, with the type as string. This would save the extra indirection of the x86_cpu_accel_init() call.

It turns out we already have a mechanism to register and store (type, StructContainingFunctionPointers) tuples at initialization
time: QOM.

Quoted 7 lines
Quoted 4 lines

X86CPUAccel can become X86CPUAccelClass, and be registered as a QOM type. It could be a subtype of TYPE_ACCEL or not, it shouldn't matter.

It would be a weird type that isn't instantiated, and/or that does nothing but monkey patching other classes. I don't think it's a good fit.

The whole point of this would be to avoid monkey patching other classes.

Why wouldn't we instantiate it? There's a huge number of static variables in target/i386/kvm.c that could be moved to that object. Sounds like a perfect fit for me.

I won't try to stop you if you really want to invent a brand new (name => CollectionOfFunctionPointers) registry, but it seems unnecessary.

Quoted 61 lines

Yet another possibility is to use GHashTable. It is limited to one value per key, but it's enough if everything is kept local to {hw,target}/i386. If needed a new function pointer can be added to MachineClass, implemented in X86MachineState (where the GHashTable would also be) and called in accel.c.

Paolo

Paolo

Quoted 49 lines

I remember this was suggested in a previous thread, but I don't remember if there were any objections.

Quoted 7 lines
Quoted 5 lines

Making sure module_call_init() is called at the correct moment is not easier or safer than just making sure accel_init_machine() (or another init function you create) is called at the correct moment.

Since there is a way to do it without a new level, that would of course

be

Quoted 12 lines

fine for me too. Let me explain however why I think Claudio's design had module_call_init() misplaced and what the fundamental difference is. The basic phases in qemu_init() are:

- initialize stuff
- parse command line
- create machine
- create accelerator
- initialize machine
- create devices
- start

Quoted 4 lines

with a mess of other object creation sprinkled between the various phases (but we don't care about those).

What I object to, is calling module_call_init() after the "initialize

stuff"

Quoted 1 line

phase. Claudio was using it to call the function directly, so it had to

be

Quoted 2 lines

exactly at "create accelerator". This is different from all other module_call_init() calls, which are done very early.

I agree.

Quoted 2 lines

With the implementation I sketched, accel_register_call must still be

done

Quoted 1 line

after type_init, so there's still an ordering constraint, but all it's

doing

Quoted 1 line

is registering a callback in the "initialize stuff" phase.

Makes sense, if we really want to introduce a new accel_register_call() abstraction. I don't think we need it, though.

--
Eduardo

Signature · 1 line
Eduardo
#19 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Il mer 18 nov 2020, 17:11 Eduardo Habkost <[email protected]> ha scritto:

Quoted 24 lines · Paolo Bonzini
Quoted 1 line

Il mer 18 nov 2020, 16:26 Eduardo Habkost <[email protected]> ha

scritto:

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

The alternative is to store the (type, function) tuple directly,

with the

Quoted 17 lines
Quoted 16 lines
Quoted 1 line

type as a string. Then you can just use type_init.

Right. Let's build on top of that:

Another alternative would be to store a (type, X86CPUAccel) tuple directly, with the type as string. This would save the extra indirection of the x86_cpu_accel_init() call.

It turns out we already have a mechanism to register and store (type, StructContainingFunctionPointers) tuples at initialization
time: QOM.

X86CPUAccel can become X86CPUAccelClass, and be registered as a QOM type. It could be a subtype of TYPE_ACCEL or not, it shouldn't matter.

Quoted 7 lines
Quoted 1 line

It would be a weird type that isn't instantiated, and/or that does

nothing

Quoted 1 line

but monkey patching other classes. I don't think it's a good fit.

The whole point of this would be to avoid monkey patching other classes.

Adding a layer of indirect calls is not very different from monkey patching though.

You also have to consider that accel currently does not exist in usermode emulators, so that's an issue too. I would rather get a simple change in quickly, instead of designing the perfect class hierarchy.

Perhaps another idea would be to allow adding interfaces to classes *separately from the registration of the types*. Then we can use it to add SOFTMMU_ACCEL and I386_ACCEL interfaces to a bare bones accel class, and add the accel object to usermode emulators.

Why wouldn't we instantiate it? There's a huge number of static

Quoted 3 lines

variables in target/i386/kvm.c that could be moved to that object. Sounds like a perfect fit for me.

Most of those are properties of the running kernel so there's no need to move them inside an object.

Paolo

I won't try to stop you if you really want to invent a brand new

Quoted 76 lines

(name => CollectionOfFunctionPointers) registry, but it seems unnecessary.

Quoted 4 lines

Yet another possibility is to use GHashTable. It is limited to one value per key, but it's enough if everything is kept local to {hw,target}/i386. If needed a new function pointer can be added to MachineClass,

implemented

Quoted 18 lines

in X86MachineState (where the GHashTable would also be) and called in accel.c.

Paolo

Paolo

Quoted 10 lines

I remember this was suggested in a previous thread, but I don't remember if there were any objections.

Quoted 7 lines
Quoted 5 lines

Making sure module_call_init() is called at the correct moment is not easier or safer than just making sure accel_init_machine() (or another init function you create) is called at the correct moment.

Since there is a way to do it without a new level, that would of

course

Quoted 2 lines
Quoted 2 lines

be

Quoted 1 line

fine for me too. Let me explain however why I think Claudio's

design had

Quoted 1 line
Quoted 1 line
Quoted 1 line

module_call_init() misplaced and what the fundamental difference

is. The

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

basic phases in qemu_init() are:

- initialize stuff
- parse command line
- create machine
- create accelerator
- initialize machine
- create devices
- start

with a mess of other object creation sprinkled between the various

phases

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

(but we don't care about those).

What I object to, is calling module_call_init() after the "initialize

stuff"

Quoted 1 line

phase. Claudio was using it to call the function directly, so it

had to

Quoted 10 lines
Quoted 10 lines

be

Quoted 2 lines

exactly at "create accelerator". This is different from all other module_call_init() calls, which are done very early.

I agree.

Quoted 2 lines

With the implementation I sketched, accel_register_call must still be

done

Quoted 1 line

after type_init, so there's still an ordering constraint, but all

it's

Quoted 10 lines
Quoted 10 lines

doing

Quoted 1 line

is registering a callback in the "initialize stuff" phase.

Makes sense, if we really want to introduce a new accel_register_call() abstraction. I don't think we need it, though.

--
Eduardo

--
Eduardo

#20 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 28 lines · Paolo Bonzini

Il mer 18 nov 2020, 17:11 Eduardo Habkost <[email protected]> ha scritto:

Quoted 26 lines · Paolo Bonzini
Quoted 1 line

Il mer 18 nov 2020, 16:26 Eduardo Habkost <[email protected]> ha

scritto:

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

The alternative is to store the (type, function) tuple directly,

with the

Quoted 12 lines
Quoted 12 lines
Quoted 1 line

type as a string. Then you can just use type_init.

Right. Let's build on top of that:

Another alternative would be to store a (type, X86CPUAccel) tuple directly, with the type as string. This would save the extra indirection of the x86_cpu_accel_init() call.

It turns out we already have a mechanism to register and store (type, StructContainingFunctionPointers) tuples at initialization
time: QOM.

Quoted 6 lines
Quoted 4 lines

X86CPUAccel can become X86CPUAccelClass, and be registered as a QOM type. It could be a subtype of TYPE_ACCEL or not, it shouldn't matter.

It would be a weird type that isn't instantiated, and/or that does

nothing

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

but monkey patching other classes. I don't think it's a good fit.

The whole point of this would be to avoid monkey patching other classes.

Adding a layer of indirect calls is not very different from monkey patching though.

I'm a little bothered by monkey patching, but I'm more bothered by having to:

(1) register (module_init()) a function (kvm_cpu_accel_register()) that
(2) register (accel_register_call()) a function (kvm_cpu_accel_init()) that
(3) register (x86_cpu_accel_init()) a data structure (X86CPUAccel kvm_cpu_accel) that
(4) will be saved in multiple QOM classes, so that
(5) we will call the right X86CPUClass.accel method at the right moment (common_class_init(), instance_init(), realizefn()),
where:
step 4 must be done before any CPU object is created
(otherwise X86CPUAccel.instance_init & X86CPUAccel.realizefn
will be silently ignored), and
step 3 must be done after all QOM types were registered.

Quoted 4 lines

You also have to consider that accel currently does not exist in usermode emulators, so that's an issue too. I would rather get a simple change in quickly, instead of designing the perfect class hierarchy.

It doesn't have to be perfect. I agree that simple is better.

To me, registering a QOM type and looking it up when necessary is simpler than the above. Even if it's a weird class having no object instances. It probably could be an interface type.

Quoted 5 lines

Perhaps another idea would be to allow adding interfaces to classes *separately from the registration of the types*. Then we can use it to add SOFTMMU_ACCEL and I386_ACCEL interfaces to a bare bones accel class, and add the accel object to usermode emulators.

I'm not sure I follow. What do you mean by bare bones accel class, and when exactly would you add the new interfaces to the classes?

Quoted 8 lines

Why wouldn't we instantiate it? There's a huge number of static

Quoted 3 lines

variables in target/i386/kvm.c that could be moved to that object. Sounds like a perfect fit for me.

Most of those are properties of the running kernel so there's no need to move them inside an object.

There's no need, correct. Some consistency would be nice, though. All kernel capabilities in kvm-all.c are saved in KVMState.

Quoted 80 lines

Paolo

I won't try to stop you if you really want to invent a brand new

Quoted 76 lines

(name => CollectionOfFunctionPointers) registry, but it seems unnecessary.

Quoted 4 lines

Yet another possibility is to use GHashTable. It is limited to one value per key, but it's enough if everything is kept local to {hw,target}/i386. If needed a new function pointer can be added to MachineClass,

implemented

Quoted 18 lines

in X86MachineState (where the GHashTable would also be) and called in accel.c.

Paolo

Paolo

Quoted 10 lines

I remember this was suggested in a previous thread, but I don't remember if there were any objections.

Quoted 7 lines
Quoted 5 lines

Making sure module_call_init() is called at the correct moment is not easier or safer than just making sure accel_init_machine() (or another init function you create) is called at the correct moment.

Since there is a way to do it without a new level, that would of

course

Quoted 2 lines
Quoted 2 lines

be

Quoted 1 line

fine for me too. Let me explain however why I think Claudio's

design had

Quoted 1 line
Quoted 1 line
Quoted 1 line

module_call_init() misplaced and what the fundamental difference

is. The

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

basic phases in qemu_init() are:

- initialize stuff
- parse command line
- create machine
- create accelerator
- initialize machine
- create devices
- start

with a mess of other object creation sprinkled between the various

phases

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

(but we don't care about those).

What I object to, is calling module_call_init() after the "initialize

stuff"

Quoted 1 line

phase. Claudio was using it to call the function directly, so it

had to

Quoted 10 lines
Quoted 10 lines

be

Quoted 2 lines

exactly at "create accelerator". This is different from all other module_call_init() calls, which are done very early.

I agree.

Quoted 2 lines

With the implementation I sketched, accel_register_call must still be

done

Quoted 1 line

after type_init, so there's still an ordering constraint, but all

it's

Quoted 10 lines
Quoted 10 lines

doing

Quoted 1 line

is registering a callback in the "initialize stuff" phase.

Makes sense, if we really want to introduce a new accel_register_call() abstraction. I don't think we need it, though.

--
Eduardo

--
Eduardo

Signature · 1 line
Eduardo
#21 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 22 lines · Eduardo Habkost
Quoted 2 lines

Adding a layer of indirect calls is not very different from monkey patching though.

I'm a little bothered by monkey patching, but I'm more bothered by having to:

(1) register (module_init()) a function (kvm_cpu_accel_register()) that
(2) register (accel_register_call()) a function (kvm_cpu_accel_init()) that
(3) register (x86_cpu_accel_init()) a data structure (X86CPUAccel kvm_cpu_accel) that
(4) will be saved in multiple QOM classes, so that
(5) we will call the right X86CPUClass.accel method at the right moment (common_class_init(), instance_init(), realizefn()),
where:
step 4 must be done before any CPU object is created
(otherwise X86CPUAccel.instance_init & X86CPUAccel.realizefn
will be silently ignored), and
step 3 must be done after all QOM types were registered.

Quoted 3 lines

You also have to consider that accel currently does not exist in usermode emulators, so that's an issue too. I would rather get a simple change in quickly, instead of designing the perfect class hierarchy.

Quoted 5 lines

It doesn't have to be perfect. I agree that simple is better.

To me, registering a QOM type and looking it up when necessary is simpler than the above. Even if it's a weird class having no object instances. It probably could be an interface type.

Registering a QOM type still has quite some boilerplate. Also registering a QOM type has a public side effect (shows up in qom-list-types). In general I don't look at QOM unless I want its property mechanism, but maybe that's just me.

Quoted 8 lines
Quoted 4 lines

Perhaps another idea would be to allow adding interfaces to classes *separately from the registration of the types*. Then we can use it to add SOFTMMU_ACCEL and I386_ACCEL interfaces to a bare bones accel class, and add the accel object to usermode emulators.

I'm not sure I follow. What do you mean by bare bones accel class, and when exactly would you add the new interfaces to the classes?

A bare bones accel class would not have init_machine and setup_post methods; those would be in a TYPE_SOFTMMU_ACCEL interface. It would still have properties (such as tb-size for TCG) and would be able to register compat properties.

Where would I add it, I don't know. It could be a simple public wrapper around type_initialize_interface() if we add a new MODULE_INIT_* phase after QOM.

Or without adding a new phase, it could be a class_type->array of (interface_type, init_fn) hash table. type_initialize would look up the class_type by name, add the interfaces would to the class with type_initialize_interface, and then call the init_fn to fill in the vtable.

Paolo

#22 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 23 lines · Paolo Bonzini
Quoted 22 lines · Eduardo Habkost
Quoted 2 lines

Adding a layer of indirect calls is not very different from monkey patching though.

I'm a little bothered by monkey patching, but I'm more bothered by having to:

(1) register (module_init()) a function (kvm_cpu_accel_register()) that
(2) register (accel_register_call()) a function (kvm_cpu_accel_init()) that
(3) register (x86_cpu_accel_init()) a data structure (X86CPUAccel kvm_cpu_accel) that
(4) will be saved in multiple QOM classes, so that
(5) we will call the right X86CPUClass.accel method at the right moment (common_class_init(), instance_init(), realizefn()),
where:
step 4 must be done before any CPU object is created
(otherwise X86CPUAccel.instance_init & X86CPUAccel.realizefn
will be silently ignored), and
step 3 must be done after all QOM types were registered.

Quoted 3 lines

You also have to consider that accel currently does not exist in usermode emulators, so that's an issue too. I would rather get a simple change in quickly, instead of designing the perfect class hierarchy.

Quoted 7 lines
Quoted 5 lines

It doesn't have to be perfect. I agree that simple is better.

To me, registering a QOM type and looking it up when necessary is simpler than the above. Even if it's a weird class having no object instances. It probably could be an interface type.

Registering a QOM type still has quite some boilerplate. [...]

We're working on that. :)

Quoted 4 lines

[...] Also registering a
QOM type has a public side effect (shows up in qom-list-types). In general I don't look at QOM unless I want its property mechanism, but maybe that's just me.

We have lots of internal-use-only types returned by qom-list-types, I don't think it's a big deal.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Perhaps another idea would be to allow adding interfaces to classes *separately from the registration of the types*. Then we can use it to add SOFTMMU_ACCEL and I386_ACCEL interfaces to a bare bones accel class, and add the accel object to usermode emulators.

Quoted 8 lines
Quoted 3 lines

I'm not sure I follow. What do you mean by bare bones accel class, and when exactly would you add the new interfaces to the classes?

A bare bones accel class would not have init_machine and setup_post methods; those would be in a TYPE_SOFTMMU_ACCEL interface. It would still have properties (such as tb-size for TCG) and would be able to register compat properties.

Oh, I think I see. This could save us having a lot of parallel type hierarchies.

Quoted 9 lines

Where would I add it, I don't know. It could be a simple public wrapper around type_initialize_interface() if we add a new MODULE_INIT_* phase after QOM.

Or without adding a new phase, it could be a class_type->array of (interface_type, init_fn) hash table. type_initialize would look up the class_type by name, add the interfaces would to the class with type_initialize_interface, and then call the init_fn to fill in the vtable.

That sounds nice. I don't think Claudio's cleanup should be blocked until this new mechanism is ready, though.

We don't really need the type representing X86CPUAccel to be a subtype of TYPE_ACCEL or an interface implemented by current_machine->accelerator, in the first version. We just need a simple way for the CPU initialization code to find the correct X86CPUAccel struct.

While we don't have the new mechanism, it can be just a:
object_class_by_name("%s-x86-cpu-accel" % (accel->name))
call.

Below is a rough draft of what I mean. There's still lots of room for cleaning it up (especially getting rid of the X86CPUClass.common_class_init and X86CPUClass.accel fields).

git tree at https://gitlab.com/ehabkost/qemu/-/commits/work/qom-based-x86-cpu-accel

---

include/qemu/module.h +0 -2
index 485eda986a..944d403cbd 100644
@@ -44,7 +44,6 @@ typedef enum {
     MODULE_INIT_BLOCK,
     MODULE_INIT_OPTS,
     MODULE_INIT_QOM,
-    MODULE_INIT_ACCEL_CPU,
     MODULE_INIT_TRACE,
     MODULE_INIT_XEN_BACKEND,
     MODULE_INIT_LIBQOS,
@@ -55,7 +54,6 @@ typedef enum {
 #define block_init(function) module_init(function, MODULE_INIT_BLOCK)
 #define opts_init(function) module_init(function, MODULE_INIT_OPTS)
 #define type_init(function) module_init(function, MODULE_INIT_QOM)
-#define accel_cpu_init(function) module_init(function, MODULE_INIT_ACCEL_CPU)
 #define trace_init(function) module_init(function, MODULE_INIT_TRACE)
 #define xen_backend_init(function) module_init(function, \
                                                MODULE_INIT_XEN_BACKEND)
include/sysemu/cpus.h +3 -0
index 032169ccd3..14491297bb 100644
@@ -25,6 +25,9 @@ typedef struct CpuAccelOps {
 /* register accel-specific cpus interface implementation */
 void cpus_register_accel(const CpuAccelOps *i);
 
+/* Call arch-specific accel initialization */
+void cpu_accel_arch_init(const char *accel_name);
+
 /* Create a dummy vcpu for CpuAccelOps->create_vcpu_thread */
 void dummy_start_vcpu_thread(CPUState *);
 
target/i386/cpu-qom.h +9 -9
index 79fcbd3b9b..eafd86dc22 100644
@@ -34,7 +34,7 @@ OBJECT_DECLARE_TYPE(X86CPU, X86CPUClass,
                     X86_CPU)
 
 typedef struct X86CPUModel X86CPUModel;
-typedef struct X86CPUAccel X86CPUAccel;
+typedef struct X86CPUAccelInterface X86CPUAccelInterface;
 
 /**
  * X86CPUClass:
@@ -71,13 +71,11 @@ struct X86CPUClass {
     DeviceUnrealize parent_unrealize;
     DeviceReset parent_reset;
 
-    const X86CPUAccel *accel;
+    const X86CPUAccelInterface *accel;
 };
 
 /**
- * X86CPUAccel:
- * @name: string name of the X86 CPU Accelerator
- *
+ * X86CPUAccelInterface:
  * @common_class_init: initializer for the common cpu
  * @instance_init: cpu instance initialization
  * @realizefn: realize function, called first in x86 cpu realize
@@ -85,14 +83,16 @@ struct X86CPUClass {
  * X86 CPU accelerator-specific CPU initializations
  */
 
-struct X86CPUAccel {
-    const char *name;
-
+struct X86CPUAccelInterface {
+    ObjectClass parent_class;
     void (*common_class_init)(X86CPUClass *xcc);
     void (*instance_init)(X86CPU *cpu);
     void (*realizefn)(X86CPU *cpu, Error **errp);
 };
 
-void x86_cpu_accel_init(const X86CPUAccel *accel);
+#define TYPE_X86_CPU_ACCEL "x86-cpu-accel"
+OBJECT_DECLARE_INTERFACE(X86CPUAccel, X86CPUAccelInterface, X86_CPU_ACCEL);
+
+#define X86_CPU_ACCEL_NAME(acc) (acc "-x86-cpu-accel")
 
 #endif
bsd-user/main.c +2 -1
index 9f88ae952a..6107c8ca24 100644
@@ -909,7 +909,8 @@ int main(int argc, char **argv)
 
     /* init tcg before creating CPUs and to get qemu_host_page_size */
     tcg_exec_init(0);
-    module_call_init(MODULE_INIT_ACCEL_CPU);
+    cpu_accel_arch_init("tcg");
+
 
     cpu_type = parse_cpu_option(cpu_model);
     cpu = cpu_create(cpu_type);
linux-user/main.c +1 -1
index a745901d86..c36564fd61 100644
@@ -704,7 +704,7 @@ int main(int argc, char **argv, char **envp)
 
     /* init tcg before creating CPUs and to get qemu_host_page_size */
     tcg_exec_init(0);
-    module_call_init(MODULE_INIT_ACCEL_CPU);
+    cpu_accel_arch_init("tcg");
 
     cpu = cpu_create(cpu_type);
     env = cpu->env_ptr;
softmmu/vl.c +1 -6
index df4bed056a..b90d107475 100644
@@ -2744,6 +2744,7 @@ static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
         return 0;
     }
 
+    cpu_accel_arch_init(acc);
     return 1;
 }
 
@@ -4173,12 +4174,6 @@ void qemu_init(int argc, char **argv, char **envp)
      */
     configure_accelerators(argv[0]);
 
-    /*
-     * accelerator has been chosen and initialized, now it is time to
-     * register the cpu accel interface.
-     */
-    module_call_init(MODULE_INIT_ACCEL_CPU);
-
     /*
      * Beware, QOM objects created before this point miss global and
      * compat properties.
stubs/cpu_accel_arch_init.c new file +6 -0
new file mode 100644
index 0000000000..b80cbdd847
@@ -0,0 +1,6 @@
+#include "qemu/osdep.h"
+#include "sysemu/cpus.h"
+
+void cpu_accel_arch_init(const char *accel_name)
+{
+}
target/i386/cpu.c +18 -2
index b53e958926..b91e0b44ca 100644
@@ -7041,6 +7041,12 @@ static const TypeInfo x86_base_cpu_type_info = {
         .class_init = x86_cpu_base_class_init,
 };
 
+static const TypeInfo x86_cpu_accel_type_info = {
+    .name = TYPE_X86_CPU_ACCEL,
+    .parent = TYPE_INTERFACE,
+    .class_size = sizeof(X86CPUAccelInterface),
+};
+
 static void x86_cpu_register_types(void)
 {
     int i;
@@ -7051,6 +7057,7 @@ static void x86_cpu_register_types(void)
     }
     type_register_static(&max_x86_cpu_type_info);
     type_register_static(&x86_base_cpu_type_info);
+    type_register_static(&x86_cpu_accel_type_info);
 }
 
 type_init(x86_cpu_register_types)
@@ -7058,13 +7065,22 @@ type_init(x86_cpu_register_types)
 static void x86_cpu_accel_init_aux(ObjectClass *klass, void *opaque)
 {
     X86CPUClass *xcc = X86_CPU_CLASS(klass);
-    const X86CPUAccel **accel = opaque;
+    const X86CPUAccelInterface **accel = opaque;
 
     xcc->accel = *accel;
     xcc->accel->common_class_init(xcc);
 }
 
-void x86_cpu_accel_init(const X86CPUAccel *accel)
+static void x86_cpu_accel_init(const X86CPUAccelInterface *accel)
 {
     object_class_foreach(x86_cpu_accel_init_aux, TYPE_X86_CPU, false, &accel);
 }
+
+void cpu_accel_arch_init(const char *accel_name)
+{
+    g_autofree char *cpu_accel_name =
+        g_strdup_printf(X86_CPU_ACCEL_NAME("%s"), accel_name);
+    X86CPUAccelInterface *acc = X86_CPU_ACCEL_CLASS(object_class_by_name(cpu_accel_name));
+    assert(acc);
+    x86_cpu_accel_init(acc);
+}
target/i386/hvf/cpu.c +13 -9
index 29e672191f..358351018f 100644
@@ -46,19 +46,23 @@ static void hvf_cpu_instance_init(X86CPU *cpu)
     }
 }
 
-static const X86CPUAccel hvf_cpu_accel = {
-    .name = TYPE_X86_CPU "-hvf",
+static void hvf_cpu_accel_interface_init(ObjectClass *oc, void *data)
+{
+    X86CPUAccelInterface *acc = X86_CPU_ACCEL_CLASS(oc);
+    acc->realizefn = host_cpu_realizefn;
+    acc->common_class_init = hvf_cpu_common_class_init;
+    acc->instance_init = hvf_cpu_instance_init;
+};
 
-    .realizefn = host_cpu_realizefn,
-    .common_class_init = hvf_cpu_common_class_init,
-    .instance_init = hvf_cpu_instance_init,
+static const TypeInfo hvf_cpu_accel_type_info = {
+    .name = X86_CPU_ACCEL_NAME("hvf"),
+    .parent = TYPE_X86_CPU_ACCEL,
+    .class_init = hvf_cpu_accel_interface_init,
 };
 
 static void hvf_cpu_accel_init(void)
 {
-    if (hvf_enabled()) {
-        x86_cpu_accel_init(&hvf_cpu_accel);
-    }
+    type_register_static(&hvf_cpu_accel_type_info);
 }
 
-accel_cpu_init(hvf_cpu_accel_init);
+type_init(hvf_cpu_accel_init);
target/i386/kvm/cpu.c +14 -9
index 76982865eb..b6a1a4d200 100644
@@ -128,18 +128,23 @@ static void kvm_cpu_instance_init(X86CPU *cpu)
     }
 }
 
-static const X86CPUAccel kvm_cpu_accel = {
-    .name = TYPE_X86_CPU "-kvm",
+static void kvm_cpu_accel_interface_init(ObjectClass *oc, void *data)
+{
+    X86CPUAccelInterface *acc = X86_CPU_ACCEL_CLASS(oc);
+    acc->realizefn = kvm_cpu_realizefn;
+    acc->common_class_init = kvm_cpu_common_class_init;
+    acc->instance_init = kvm_cpu_instance_init;
+};
 
-    .realizefn = kvm_cpu_realizefn,
-    .common_class_init = kvm_cpu_common_class_init,
-    .instance_init = kvm_cpu_instance_init,
+static const TypeInfo kvm_cpu_accel_type_info = {
+    .name = X86_CPU_ACCEL_NAME("kvm"),
+    .parent = TYPE_X86_CPU_ACCEL,
+    .class_init = kvm_cpu_accel_interface_init,
 };
 
 static void kvm_cpu_accel_init(void)
 {
-    if (kvm_enabled()) {
-        x86_cpu_accel_init(&kvm_cpu_accel);
-    }
+    type_register_static(&kvm_cpu_accel_type_info);
 }
-accel_cpu_init(kvm_cpu_accel_init);
+
+type_init(kvm_cpu_accel_init);
target/i386/tcg/cpu.c +13 -9
index 25cf4cfb46..0321688cd3 100644
@@ -150,19 +150,23 @@ static void tcg_cpu_instance_init(X86CPU *cpu)
     x86_cpu_apply_props(cpu, tcg_default_props);
 }
 
-static const X86CPUAccel tcg_cpu_accel = {
-    .name = TYPE_X86_CPU "-tcg",
+static void tcg_cpu_accel_interface_init(ObjectClass *oc, void *data)
+{
+    X86CPUAccelInterface *acc = X86_CPU_ACCEL_CLASS(oc);
+    acc->realizefn = tcg_cpu_realizefn;
+    acc->common_class_init = tcg_cpu_common_class_init;
+    acc->instance_init = tcg_cpu_instance_init;
+};
 
-    .realizefn = tcg_cpu_realizefn,
-    .common_class_init = tcg_cpu_common_class_init,
-    .instance_init = tcg_cpu_instance_init,
+static const TypeInfo tcg_cpu_accel_type_info = {
+    .name = X86_CPU_ACCEL_NAME("tcg"),
+    .parent = TYPE_X86_CPU_ACCEL,
+    .class_init = tcg_cpu_accel_interface_init,
 };
 
 static void tcg_cpu_accel_init(void)
 {
-    if (tcg_enabled()) {
-        x86_cpu_accel_init(&tcg_cpu_accel);
-    }
+    type_register_static(&tcg_cpu_accel_type_info);
 }
 
-accel_cpu_init(tcg_cpu_accel_init);
+type_init(tcg_cpu_accel_init);
stubs/meson.build +1 -0
index 82b7ba60ab..1d66de1fae 100644
@@ -1,4 +1,5 @@
 stub_ss.add(files('arch_type.c'))
+stub_ss.add(files('cpu_accel_arch_init.c'))
 stub_ss.add(files('bdrv-next-monitor-owned.c'))
 stub_ss.add(files('blk-commit-all.c'))
 stub_ss.add(files('blk-exp-close-all.c'))
Signature · 1 line
Eduardo
#23 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 161 lines · Eduardo Habkost
Quoted 23 lines · Paolo Bonzini
Quoted 22 lines · Eduardo Habkost
Quoted 2 lines

Adding a layer of indirect calls is not very different from monkey patching though.

I'm a little bothered by monkey patching, but I'm more bothered by having to:

(1) register (module_init()) a function (kvm_cpu_accel_register()) that
(2) register (accel_register_call()) a function (kvm_cpu_accel_init()) that
(3) register (x86_cpu_accel_init()) a data structure (X86CPUAccel kvm_cpu_accel) that
(4) will be saved in multiple QOM classes, so that
(5) we will call the right X86CPUClass.accel method at the right moment (common_class_init(), instance_init(), realizefn()),
where:
step 4 must be done before any CPU object is created
(otherwise X86CPUAccel.instance_init & X86CPUAccel.realizefn
will be silently ignored), and
step 3 must be done after all QOM types were registered.

Quoted 3 lines

You also have to consider that accel currently does not exist in usermode emulators, so that's an issue too. I would rather get a simple change in quickly, instead of designing the perfect class hierarchy.

Quoted 7 lines
Quoted 5 lines

It doesn't have to be perfect. I agree that simple is better.

To me, registering a QOM type and looking it up when necessary is simpler than the above. Even if it's a weird class having no object instances. It probably could be an interface type.

Registering a QOM type still has quite some boilerplate. [...]

We're working on that. :)

Quoted 4 lines

[...] Also registering a
QOM type has a public side effect (shows up in qom-list-types). In general I don't look at QOM unless I want its property mechanism, but maybe that's just me.

We have lots of internal-use-only types returned by qom-list-types, I don't think it's a big deal.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Perhaps another idea would be to allow adding interfaces to classes *separately from the registration of the types*. Then we can use it to add SOFTMMU_ACCEL and I386_ACCEL interfaces to a bare bones accel class, and add the accel object to usermode emulators.

Quoted 8 lines
Quoted 3 lines

I'm not sure I follow. What do you mean by bare bones accel class, and when exactly would you add the new interfaces to the classes?

A bare bones accel class would not have init_machine and setup_post methods; those would be in a TYPE_SOFTMMU_ACCEL interface. It would still have properties (such as tb-size for TCG) and would be able to register compat properties.

Oh, I think I see. This could save us having a lot of parallel type hierarchies.

Quoted 9 lines

Where would I add it, I don't know. It could be a simple public wrapper around type_initialize_interface() if we add a new MODULE_INIT_* phase after QOM.

Or without adding a new phase, it could be a class_type->array of (interface_type, init_fn) hash table. type_initialize would look up the class_type by name, add the interfaces would to the class with type_initialize_interface, and then call the init_fn to fill in the vtable.

That sounds nice. I don't think Claudio's cleanup should be blocked until this new mechanism is ready, though.

We don't really need the type representing X86CPUAccel to be a subtype of TYPE_ACCEL or an interface implemented by current_machine->accelerator, in the first version. We just need a simple way for the CPU initialization code to find the correct X86CPUAccel struct.

While we don't have the new mechanism, it can be just a:
object_class_by_name("%s-x86-cpu-accel" % (accel->name))
call.

Below is a rough draft of what I mean. There's still lots of room for cleaning it up (especially getting rid of the X86CPUClass.common_class_init and X86CPUClass.accel fields).

git tree at https://gitlab.com/ehabkost/qemu/-/commits/work/qom-based-x86-cpu-accel

---

include/qemu/module.h +0 -2
index 485eda986a..944d403cbd 100644
@@ -44,7 +44,6 @@ typedef enum {
     MODULE_INIT_BLOCK,
     MODULE_INIT_OPTS,
     MODULE_INIT_QOM,
-    MODULE_INIT_ACCEL_CPU,
     MODULE_INIT_TRACE,
     MODULE_INIT_XEN_BACKEND,
     MODULE_INIT_LIBQOS,
@@ -55,7 +54,6 @@ typedef enum {
 #define block_init(function) module_init(function, MODULE_INIT_BLOCK)
 #define opts_init(function) module_init(function, MODULE_INIT_OPTS)
 #define type_init(function) module_init(function, MODULE_INIT_QOM)
-#define accel_cpu_init(function) module_init(function, MODULE_INIT_ACCEL_CPU)
 #define trace_init(function) module_init(function, MODULE_INIT_TRACE)
 #define xen_backend_init(function) module_init(function, \
                                                MODULE_INIT_XEN_BACKEND)
include/sysemu/cpus.h +3 -0
index 032169ccd3..14491297bb 100644
@@ -25,6 +25,9 @@ typedef struct CpuAccelOps {
 /* register accel-specific cpus interface implementation */
 void cpus_register_accel(const CpuAccelOps *i);
 
+/* Call arch-specific accel initialization */
+void cpu_accel_arch_init(const char *accel_name);
+
 /* Create a dummy vcpu for CpuAccelOps->create_vcpu_thread */
 void dummy_start_vcpu_thread(CPUState *);
 
target/i386/cpu-qom.h +3 -8
index 79fcbd3b9b..eafd86dc22 100644
@@ -34,7 +34,7 @@ OBJECT_DECLARE_TYPE(X86CPU, X86CPUClass,
                     X86_CPU)
 
 typedef struct X86CPUModel X86CPUModel;
-typedef struct X86CPUAccel X86CPUAccel;
+typedef struct X86CPUAccelInterface X86CPUAccelInterface;
 
 /**
  * X86CPUClass:
@@ -71,13 +71,11 @@ struct X86CPUClass {
     DeviceUnrealize parent_unrealize;
     DeviceReset parent_reset;
 
-    const X86CPUAccel *accel;
+    const X86CPUAccelInterface *accel;
 };
 
 /**
- * X86CPUAccel:
- * @name: string name of the X86 CPU Accelerator
- *
+ * X86CPUAccelInterface:
  * @common_class_init: initializer for the common cpu
  * @instance_init: cpu instance initialization
  * @realizefn: realize function, called first in x86 cpu realize
@@ -85,14 +83,16 @@ struct X86CPUClass {
  * X86 CPU accelerator-specific CPU initializations
  */
 
-struct X86CPUAccel {
-    const char *name;
-

target/i386/cpu-qom.h struct X86CPUClass { · line 94

Quoted 10 lines
target/i386/cpu-qom.h +4 -1
+struct X86CPUAccelInterface {
+    ObjectClass parent_class;
     void (*common_class_init)(X86CPUClass *xcc);
     void (*instance_init)(X86CPU *cpu);
     void (*realizefn)(X86CPU *cpu, Error **errp);
 };
 
-void x86_cpu_accel_init(const X86CPUAccel *accel);
+#define TYPE_X86_CPU_ACCEL "x86-cpu-accel"
+OBJECT_DECLARE_INTERFACE(X86CPUAccel, X86CPUAccelInterface, X86_CPU_ACCEL);

I am not exactly sure what precisely you are doing here,

I get the general intention to use the existing interface-related "stuff" in QOM,
but I do not see any OBJECT_DECLARE_INTERFACE around, and does not seem like the other boilerplate used for interfaces.

Could you clarify what happens here? Should we just use a normal object, call it "Interface" and call it a day?

Thanks,

Claudio

Quoted 242 lines
target/i386/cpu-qom.h +2 -0
+
+#define X86_CPU_ACCEL_NAME(acc) (acc "-x86-cpu-accel")
 
 #endif
bsd-user/main.c +2 -1
index 9f88ae952a..6107c8ca24 100644
@@ -909,7 +909,8 @@ int main(int argc, char **argv)
 
     /* init tcg before creating CPUs and to get qemu_host_page_size */
     tcg_exec_init(0);
-    module_call_init(MODULE_INIT_ACCEL_CPU);
+    cpu_accel_arch_init("tcg");
+
 
     cpu_type = parse_cpu_option(cpu_model);
     cpu = cpu_create(cpu_type);
linux-user/main.c +1 -1
index a745901d86..c36564fd61 100644
@@ -704,7 +704,7 @@ int main(int argc, char **argv, char **envp)
 
     /* init tcg before creating CPUs and to get qemu_host_page_size */
     tcg_exec_init(0);
-    module_call_init(MODULE_INIT_ACCEL_CPU);
+    cpu_accel_arch_init("tcg");
 
     cpu = cpu_create(cpu_type);
     env = cpu->env_ptr;
softmmu/vl.c +1 -6
index df4bed056a..b90d107475 100644
@@ -2744,6 +2744,7 @@ static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
         return 0;
     }
 
+    cpu_accel_arch_init(acc);
     return 1;
 }
 
@@ -4173,12 +4174,6 @@ void qemu_init(int argc, char **argv, char **envp)
      */
     configure_accelerators(argv[0]);
 
-    /*
-     * accelerator has been chosen and initialized, now it is time to
-     * register the cpu accel interface.
-     */
-    module_call_init(MODULE_INIT_ACCEL_CPU);
-
     /*
      * Beware, QOM objects created before this point miss global and
      * compat properties.
stubs/cpu_accel_arch_init.c new file +6 -0
new file mode 100644
index 0000000000..b80cbdd847
@@ -0,0 +1,6 @@
+#include "qemu/osdep.h"
+#include "sysemu/cpus.h"
+
+void cpu_accel_arch_init(const char *accel_name)
+{
+}
target/i386/cpu.c +18 -2
index b53e958926..b91e0b44ca 100644
@@ -7041,6 +7041,12 @@ static const TypeInfo x86_base_cpu_type_info = {
         .class_init = x86_cpu_base_class_init,
 };
 
+static const TypeInfo x86_cpu_accel_type_info = {
+    .name = TYPE_X86_CPU_ACCEL,
+    .parent = TYPE_INTERFACE,
+    .class_size = sizeof(X86CPUAccelInterface),
+};
+
 static void x86_cpu_register_types(void)
 {
     int i;
@@ -7051,6 +7057,7 @@ static void x86_cpu_register_types(void)
     }
     type_register_static(&max_x86_cpu_type_info);
     type_register_static(&x86_base_cpu_type_info);
+    type_register_static(&x86_cpu_accel_type_info);
 }
 
 type_init(x86_cpu_register_types)
@@ -7058,13 +7065,22 @@ type_init(x86_cpu_register_types)
 static void x86_cpu_accel_init_aux(ObjectClass *klass, void *opaque)
 {
     X86CPUClass *xcc = X86_CPU_CLASS(klass);
-    const X86CPUAccel **accel = opaque;
+    const X86CPUAccelInterface **accel = opaque;
 
     xcc->accel = *accel;
     xcc->accel->common_class_init(xcc);
 }
 
-void x86_cpu_accel_init(const X86CPUAccel *accel)
+static void x86_cpu_accel_init(const X86CPUAccelInterface *accel)
 {
     object_class_foreach(x86_cpu_accel_init_aux, TYPE_X86_CPU, false, &accel);
 }
+
+void cpu_accel_arch_init(const char *accel_name)
+{
+    g_autofree char *cpu_accel_name =
+        g_strdup_printf(X86_CPU_ACCEL_NAME("%s"), accel_name);
+    X86CPUAccelInterface *acc = X86_CPU_ACCEL_CLASS(object_class_by_name(cpu_accel_name));
+    assert(acc);
+    x86_cpu_accel_init(acc);
+}
target/i386/hvf/cpu.c +13 -9
index 29e672191f..358351018f 100644
@@ -46,19 +46,23 @@ static void hvf_cpu_instance_init(X86CPU *cpu)
     }
 }
 
-static const X86CPUAccel hvf_cpu_accel = {
-    .name = TYPE_X86_CPU "-hvf",
+static void hvf_cpu_accel_interface_init(ObjectClass *oc, void *data)
+{
+    X86CPUAccelInterface *acc = X86_CPU_ACCEL_CLASS(oc);
+    acc->realizefn = host_cpu_realizefn;
+    acc->common_class_init = hvf_cpu_common_class_init;
+    acc->instance_init = hvf_cpu_instance_init;
+};
 
-    .realizefn = host_cpu_realizefn,
-    .common_class_init = hvf_cpu_common_class_init,
-    .instance_init = hvf_cpu_instance_init,
+static const TypeInfo hvf_cpu_accel_type_info = {
+    .name = X86_CPU_ACCEL_NAME("hvf"),
+    .parent = TYPE_X86_CPU_ACCEL,
+    .class_init = hvf_cpu_accel_interface_init,
 };
 
 static void hvf_cpu_accel_init(void)
 {
-    if (hvf_enabled()) {
-        x86_cpu_accel_init(&hvf_cpu_accel);
-    }
+    type_register_static(&hvf_cpu_accel_type_info);
 }
 
-accel_cpu_init(hvf_cpu_accel_init);
+type_init(hvf_cpu_accel_init);
target/i386/kvm/cpu.c +14 -9
index 76982865eb..b6a1a4d200 100644
@@ -128,18 +128,23 @@ static void kvm_cpu_instance_init(X86CPU *cpu)
     }
 }
 
-static const X86CPUAccel kvm_cpu_accel = {
-    .name = TYPE_X86_CPU "-kvm",
+static void kvm_cpu_accel_interface_init(ObjectClass *oc, void *data)
+{
+    X86CPUAccelInterface *acc = X86_CPU_ACCEL_CLASS(oc);
+    acc->realizefn = kvm_cpu_realizefn;
+    acc->common_class_init = kvm_cpu_common_class_init;
+    acc->instance_init = kvm_cpu_instance_init;
+};
 
-    .realizefn = kvm_cpu_realizefn,
-    .common_class_init = kvm_cpu_common_class_init,
-    .instance_init = kvm_cpu_instance_init,
+static const TypeInfo kvm_cpu_accel_type_info = {
+    .name = X86_CPU_ACCEL_NAME("kvm"),
+    .parent = TYPE_X86_CPU_ACCEL,
+    .class_init = kvm_cpu_accel_interface_init,
 };
 
 static void kvm_cpu_accel_init(void)
 {
-    if (kvm_enabled()) {
-        x86_cpu_accel_init(&kvm_cpu_accel);
-    }
+    type_register_static(&kvm_cpu_accel_type_info);
 }
-accel_cpu_init(kvm_cpu_accel_init);
+
+type_init(kvm_cpu_accel_init);
target/i386/tcg/cpu.c +13 -9
index 25cf4cfb46..0321688cd3 100644
@@ -150,19 +150,23 @@ static void tcg_cpu_instance_init(X86CPU *cpu)
     x86_cpu_apply_props(cpu, tcg_default_props);
 }
 
-static const X86CPUAccel tcg_cpu_accel = {
-    .name = TYPE_X86_CPU "-tcg",
+static void tcg_cpu_accel_interface_init(ObjectClass *oc, void *data)
+{
+    X86CPUAccelInterface *acc = X86_CPU_ACCEL_CLASS(oc);
+    acc->realizefn = tcg_cpu_realizefn;
+    acc->common_class_init = tcg_cpu_common_class_init;
+    acc->instance_init = tcg_cpu_instance_init;
+};
 
-    .realizefn = tcg_cpu_realizefn,
-    .common_class_init = tcg_cpu_common_class_init,
-    .instance_init = tcg_cpu_instance_init,
+static const TypeInfo tcg_cpu_accel_type_info = {
+    .name = X86_CPU_ACCEL_NAME("tcg"),
+    .parent = TYPE_X86_CPU_ACCEL,
+    .class_init = tcg_cpu_accel_interface_init,
 };
 
 static void tcg_cpu_accel_init(void)
 {
-    if (tcg_enabled()) {
-        x86_cpu_accel_init(&tcg_cpu_accel);
-    }
+    type_register_static(&tcg_cpu_accel_type_info);
 }
 
-accel_cpu_init(tcg_cpu_accel_init);
+type_init(tcg_cpu_accel_init);
stubs/meson.build +1 -0
index 82b7ba60ab..1d66de1fae 100644
@@ -1,4 +1,5 @@
 stub_ss.add(files('arch_type.c'))
+stub_ss.add(files('cpu_accel_arch_init.c'))
 stub_ss.add(files('bdrv-next-monitor-owned.c'))
 stub_ss.add(files('blk-commit-all.c'))
 stub_ss.add(files('blk-exp-close-all.c'))
#24 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 49 lines · Claudio Fontana
Quoted 48 lines · Eduardo Habkost
Quoted 23 lines · Paolo Bonzini
Quoted 22 lines · Eduardo Habkost
Quoted 2 lines

Adding a layer of indirect calls is not very different from monkey patching though.

I'm a little bothered by monkey patching, but I'm more bothered by having to:

(1) register (module_init()) a function (kvm_cpu_accel_register()) that
(2) register (accel_register_call()) a function (kvm_cpu_accel_init()) that
(3) register (x86_cpu_accel_init()) a data structure (X86CPUAccel kvm_cpu_accel) that
(4) will be saved in multiple QOM classes, so that
(5) we will call the right X86CPUClass.accel method at the right moment (common_class_init(), instance_init(), realizefn()),
where:
step 4 must be done before any CPU object is created
(otherwise X86CPUAccel.instance_init & X86CPUAccel.realizefn
will be silently ignored), and
step 3 must be done after all QOM types were registered.

Quoted 3 lines

You also have to consider that accel currently does not exist in usermode emulators, so that's an issue too. I would rather get a simple change in quickly, instead of designing the perfect class hierarchy.

Quoted 7 lines
Quoted 5 lines

It doesn't have to be perfect. I agree that simple is better.

To me, registering a QOM type and looking it up when necessary is simpler than the above. Even if it's a weird class having no object instances. It probably could be an interface type.

Registering a QOM type still has quite some boilerplate. [...]

We're working on that. :)

Quoted 4 lines

[...] Also registering a
QOM type has a public side effect (shows up in qom-list-types). In general I don't look at QOM unless I want its property mechanism, but maybe that's just me.

We have lots of internal-use-only types returned by qom-list-types, I don't think it's a big deal.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Perhaps another idea would be to allow adding interfaces to classes *separately from the registration of the types*. Then we can use it to add SOFTMMU_ACCEL and I386_ACCEL interfaces to a bare bones accel class, and add the accel object to usermode emulators.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

I'm not sure I follow. What do you mean by bare bones accel class, and when exactly would you add the new interfaces to the classes?

A bare bones accel class would not have init_machine and setup_post methods; those would be in a TYPE_SOFTMMU_ACCEL interface. It would still have properties (such as tb-size for TCG) and would be able to register compat properties.

[1]

Quoted 34 lines
Quoted 34 lines

Oh, I think I see. This could save us having a lot of parallel type hierarchies.

Quoted 9 lines

Where would I add it, I don't know. It could be a simple public wrapper around type_initialize_interface() if we add a new MODULE_INIT_* phase after QOM.

Or without adding a new phase, it could be a class_type->array of (interface_type, init_fn) hash table. type_initialize would look up the class_type by name, add the interfaces would to the class with type_initialize_interface, and then call the init_fn to fill in the vtable.

That sounds nice. I don't think Claudio's cleanup should be blocked until this new mechanism is ready, though.

We don't really need the type representing X86CPUAccel to be a subtype of TYPE_ACCEL or an interface implemented by current_machine->accelerator, in the first version. We just need a simple way for the CPU initialization code to find the correct X86CPUAccel struct.

While we don't have the new mechanism, it can be just a:
object_class_by_name("%s-x86-cpu-accel" % (accel->name))
call.

Below is a rough draft of what I mean. There's still lots of room for cleaning it up (especially getting rid of the X86CPUClass.common_class_init and X86CPUClass.accel fields).

git tree at https://gitlab.com/ehabkost/qemu/-/commits/work/qom-based-x86-cpu-accel

[...]

Quoted 29 lines
Quoted 15 lines

/**
- * X86CPUAccel:
- * @name: string name of the X86 CPU Accelerator
- *
+ * X86CPUAccelInterface:
* @common_class_init: initializer for the common cpu
* @instance_init: cpu instance initialization
* @realizefn: realize function, called first in x86 cpu realize

target/i386/cpu-qom.h +0 -3
@@ -85,14 +83,16 @@ struct X86CPUClass {
  * X86 CPU accelerator-specific CPU initializations
  */
 
-struct X86CPUAccel {
-    const char *name;
-

target/i386/cpu-qom.h struct X86CPUClass { · line 94

Quoted 10 lines
target/i386/cpu-qom.h +4 -1
+struct X86CPUAccelInterface {
+    ObjectClass parent_class;
     void (*common_class_init)(X86CPUClass *xcc);
     void (*instance_init)(X86CPU *cpu);
     void (*realizefn)(X86CPU *cpu, Error **errp);
 };
 
-void x86_cpu_accel_init(const X86CPUAccel *accel);
+#define TYPE_X86_CPU_ACCEL "x86-cpu-accel"
+OBJECT_DECLARE_INTERFACE(X86CPUAccel, X86CPUAccelInterface, X86_CPU_ACCEL);

I am not exactly sure what precisely you are doing here,

Quoted 2 lines

I get the general intention to use the existing interface-related "stuff" in QOM,
but I do not see any OBJECT_DECLARE_INTERFACE around, and does not seem like the other boilerplate used for interfaces.

See the git URL I sent above, for other related changes:

https://gitlab.com/ehabkost/qemu/-/commits/work/qom-based-x86-cpu-accel

Quoted 2 lines

Could you clarify what happens here? Should we just use a normal object, call it "Interface" and call it a day?

An interface is declared in a very similar way, but instance_size and the instance type cast macro is a bit different (because instances of interface types are never created directly).

For the draft we have here, it shouldn't make any difference if you use OBJECT_DECLARE_TYPE, because the instance type cast macros are left unused.

Normally the use case for interfaces is not like what I did here. Interfaces are usually attached to other classes (to declare that object instances of that class implement the methods of that interface). Using interfaces would be just an intermediate step to the solution Paolo was mentioning (dynamically adding interface to classes, see [1] above).

Signature · 1 line
Eduardo
#25 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 125 lines · Eduardo Habkost
Quoted 49 lines · Claudio Fontana
Quoted 48 lines · Eduardo Habkost
Quoted 23 lines · Paolo Bonzini
Quoted 22 lines · Eduardo Habkost
Quoted 2 lines

Adding a layer of indirect calls is not very different from monkey patching though.

I'm a little bothered by monkey patching, but I'm more bothered by having to:

(1) register (module_init()) a function (kvm_cpu_accel_register()) that
(2) register (accel_register_call()) a function (kvm_cpu_accel_init()) that
(3) register (x86_cpu_accel_init()) a data structure (X86CPUAccel kvm_cpu_accel) that
(4) will be saved in multiple QOM classes, so that
(5) we will call the right X86CPUClass.accel method at the right moment (common_class_init(), instance_init(), realizefn()),
where:
step 4 must be done before any CPU object is created
(otherwise X86CPUAccel.instance_init & X86CPUAccel.realizefn
will be silently ignored), and
step 3 must be done after all QOM types were registered.

Quoted 3 lines

You also have to consider that accel currently does not exist in usermode emulators, so that's an issue too. I would rather get a simple change in quickly, instead of designing the perfect class hierarchy.

Quoted 7 lines
Quoted 5 lines

It doesn't have to be perfect. I agree that simple is better.

To me, registering a QOM type and looking it up when necessary is simpler than the above. Even if it's a weird class having no object instances. It probably could be an interface type.

Registering a QOM type still has quite some boilerplate. [...]

We're working on that. :)

Quoted 4 lines

[...] Also registering a
QOM type has a public side effect (shows up in qom-list-types). In general I don't look at QOM unless I want its property mechanism, but maybe that's just me.

We have lots of internal-use-only types returned by qom-list-types, I don't think it's a big deal.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Perhaps another idea would be to allow adding interfaces to classes *separately from the registration of the types*. Then we can use it to add SOFTMMU_ACCEL and I386_ACCEL interfaces to a bare bones accel class, and add the accel object to usermode emulators.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

I'm not sure I follow. What do you mean by bare bones accel class, and when exactly would you add the new interfaces to the classes?

A bare bones accel class would not have init_machine and setup_post methods; those would be in a TYPE_SOFTMMU_ACCEL interface. It would still have properties (such as tb-size for TCG) and would be able to register compat properties.

[1]

Quoted 34 lines
Quoted 34 lines

Oh, I think I see. This could save us having a lot of parallel type hierarchies.

Quoted 9 lines

Where would I add it, I don't know. It could be a simple public wrapper around type_initialize_interface() if we add a new MODULE_INIT_* phase after QOM.

Or without adding a new phase, it could be a class_type->array of (interface_type, init_fn) hash table. type_initialize would look up the class_type by name, add the interfaces would to the class with type_initialize_interface, and then call the init_fn to fill in the vtable.

That sounds nice. I don't think Claudio's cleanup should be blocked until this new mechanism is ready, though.

We don't really need the type representing X86CPUAccel to be a subtype of TYPE_ACCEL or an interface implemented by current_machine->accelerator, in the first version. We just need a simple way for the CPU initialization code to find the correct X86CPUAccel struct.

While we don't have the new mechanism, it can be just a:
object_class_by_name("%s-x86-cpu-accel" % (accel->name))
call.

Below is a rough draft of what I mean. There's still lots of room for cleaning it up (especially getting rid of the X86CPUClass.common_class_init and X86CPUClass.accel fields).

git tree at https://gitlab.com/ehabkost/qemu/-/commits/work/qom-based-x86-cpu-accel

[...]

Quoted 29 lines
Quoted 15 lines

/**
- * X86CPUAccel:
- * @name: string name of the X86 CPU Accelerator
- *
+ * X86CPUAccelInterface:
* @common_class_init: initializer for the common cpu
* @instance_init: cpu instance initialization
* @realizefn: realize function, called first in x86 cpu realize

target/i386/cpu-qom.h +0 -3
@@ -85,14 +83,16 @@ struct X86CPUClass {
  * X86 CPU accelerator-specific CPU initializations
  */
 
-struct X86CPUAccel {
-    const char *name;
-

target/i386/cpu-qom.h struct X86CPUClass { · line 94

Quoted 10 lines
target/i386/cpu-qom.h +4 -1
+struct X86CPUAccelInterface {
+    ObjectClass parent_class;
     void (*common_class_init)(X86CPUClass *xcc);
     void (*instance_init)(X86CPU *cpu);
     void (*realizefn)(X86CPU *cpu, Error **errp);
 };
 
-void x86_cpu_accel_init(const X86CPUAccel *accel);
+#define TYPE_X86_CPU_ACCEL "x86-cpu-accel"
+OBJECT_DECLARE_INTERFACE(X86CPUAccel, X86CPUAccelInterface, X86_CPU_ACCEL);

I am not exactly sure what precisely you are doing here,

Quoted 6 lines
Quoted 2 lines

I get the general intention to use the existing interface-related "stuff" in QOM,
but I do not see any OBJECT_DECLARE_INTERFACE around, and does not seem like the other boilerplate used for interfaces.

See the git URL I sent above, for other related changes:

https://gitlab.com/ehabkost/qemu/-/commits/work/qom-based-x86-cpu-accel

Aaah I missed this, there are quite a few more changes there;

for me it's great if you take it from there, I see you are developing a solution on top of the previous series.

Quoted 12 lines
Quoted 3 lines

Could you clarify what happens here? Should we just use a normal object, call it "Interface" and call it a day?

An interface is declared in a very similar way, but instance_size and the instance type cast macro is a bit different (because instances of interface types are never created directly).

For the draft we have here, it shouldn't make any difference if you use OBJECT_DECLARE_TYPE, because the instance type cast macros are left unused.

Quoted 7 lines

Normally the use case for interfaces is not like what I did here. Interfaces are usually attached to other classes (to declare that object instances of that class implement the methods of that interface). Using interfaces would be just an intermediate step to the solution Paolo was mentioning (dynamically adding interface to classes, see [1] above).

Makes sense to me,
let me know how you guys would like to proceed from here.

The thing I am still uncertain about, looking at your tree at:

https://gitlab.com/ehabkost/qemu/-/commits/work/qom-based-x86-cpu-accel

is the removal of MODULE_INIT_ACCEL_CPU, it would be way simpler to understand I think, both for CpuAccelOps and X86CPUAccel, and is actualy in my view a perfect fit for the problem that module_call_init is supposed to solve.

But, my 2c of course,

Ciao,

Claudio

#26 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 131 lines · Claudio Fontana
Quoted 130 lines · Eduardo Habkost
Quoted 49 lines · Claudio Fontana
Quoted 48 lines · Eduardo Habkost
Quoted 23 lines · Paolo Bonzini
Quoted 22 lines · Eduardo Habkost
Quoted 2 lines

Adding a layer of indirect calls is not very different from monkey patching though.

I'm a little bothered by monkey patching, but I'm more bothered by having to:

(1) register (module_init()) a function (kvm_cpu_accel_register()) that
(2) register (accel_register_call()) a function (kvm_cpu_accel_init()) that
(3) register (x86_cpu_accel_init()) a data structure (X86CPUAccel kvm_cpu_accel) that
(4) will be saved in multiple QOM classes, so that
(5) we will call the right X86CPUClass.accel method at the right moment (common_class_init(), instance_init(), realizefn()),
where:
step 4 must be done before any CPU object is created
(otherwise X86CPUAccel.instance_init & X86CPUAccel.realizefn
will be silently ignored), and
step 3 must be done after all QOM types were registered.

Quoted 3 lines

You also have to consider that accel currently does not exist in usermode emulators, so that's an issue too. I would rather get a simple change in quickly, instead of designing the perfect class hierarchy.

Quoted 7 lines
Quoted 5 lines

It doesn't have to be perfect. I agree that simple is better.

To me, registering a QOM type and looking it up when necessary is simpler than the above. Even if it's a weird class having no object instances. It probably could be an interface type.

Registering a QOM type still has quite some boilerplate. [...]

We're working on that. :)

Quoted 4 lines

[...] Also registering a
QOM type has a public side effect (shows up in qom-list-types). In general I don't look at QOM unless I want its property mechanism, but maybe that's just me.

We have lots of internal-use-only types returned by qom-list-types, I don't think it's a big deal.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Perhaps another idea would be to allow adding interfaces to classes *separately from the registration of the types*. Then we can use it to add SOFTMMU_ACCEL and I386_ACCEL interfaces to a bare bones accel class, and add the accel object to usermode emulators.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

I'm not sure I follow. What do you mean by bare bones accel class, and when exactly would you add the new interfaces to the classes?

A bare bones accel class would not have init_machine and setup_post methods; those would be in a TYPE_SOFTMMU_ACCEL interface. It would still have properties (such as tb-size for TCG) and would be able to register compat properties.

[1]

Quoted 34 lines
Quoted 34 lines

Oh, I think I see. This could save us having a lot of parallel type hierarchies.

Quoted 9 lines

Where would I add it, I don't know. It could be a simple public wrapper around type_initialize_interface() if we add a new MODULE_INIT_* phase after QOM.

Or without adding a new phase, it could be a class_type->array of (interface_type, init_fn) hash table. type_initialize would look up the class_type by name, add the interfaces would to the class with type_initialize_interface, and then call the init_fn to fill in the vtable.

That sounds nice. I don't think Claudio's cleanup should be blocked until this new mechanism is ready, though.

We don't really need the type representing X86CPUAccel to be a subtype of TYPE_ACCEL or an interface implemented by current_machine->accelerator, in the first version. We just need a simple way for the CPU initialization code to find the correct X86CPUAccel struct.

While we don't have the new mechanism, it can be just a:
object_class_by_name("%s-x86-cpu-accel" % (accel->name))
call.

Below is a rough draft of what I mean. There's still lots of room for cleaning it up (especially getting rid of the X86CPUClass.common_class_init and X86CPUClass.accel fields).

git tree at https://gitlab.com/ehabkost/qemu/-/commits/work/qom-based-x86-cpu-accel

[...]

Quoted 29 lines
Quoted 15 lines

/**
- * X86CPUAccel:
- * @name: string name of the X86 CPU Accelerator
- *
+ * X86CPUAccelInterface:
* @common_class_init: initializer for the common cpu
* @instance_init: cpu instance initialization
* @realizefn: realize function, called first in x86 cpu realize

target/i386/cpu-qom.h +0 -3
@@ -85,14 +83,16 @@ struct X86CPUClass {
  * X86 CPU accelerator-specific CPU initializations
  */
 
-struct X86CPUAccel {
-    const char *name;
-

target/i386/cpu-qom.h struct X86CPUClass { · line 94

Quoted 10 lines
target/i386/cpu-qom.h +4 -1
+struct X86CPUAccelInterface {
+    ObjectClass parent_class;
     void (*common_class_init)(X86CPUClass *xcc);
     void (*instance_init)(X86CPU *cpu);
     void (*realizefn)(X86CPU *cpu, Error **errp);
 };
 
-void x86_cpu_accel_init(const X86CPUAccel *accel);
+#define TYPE_X86_CPU_ACCEL "x86-cpu-accel"
+OBJECT_DECLARE_INTERFACE(X86CPUAccel, X86CPUAccelInterface, X86_CPU_ACCEL);

I am not exactly sure what precisely you are doing here,

Quoted 2 lines

I get the general intention to use the existing interface-related "stuff" in QOM,
but I do not see any OBJECT_DECLARE_INTERFACE around, and does not seem like the other boilerplate used for interfaces.

See the git URL I sent above, for other related changes:

Quoted 7 lines
Quoted 1 line

https://gitlab.com/ehabkost/qemu/-/commits/work/qom-based-x86-cpu-accel

Aaah I missed this, there are quite a few more changes there;

for me it's great if you take it from there, I see you are developing a solution on top of the previous series.

I'm a bit busy with other stuff, so I'm probably not going to be able to make sure the patches are in a good shape to be submitted soon.

I don't want to impose any obstacles for the work you are doing,
either. Please consider the patch I sent (and the git tree
above) as just an example of a possible solution to the two issues
Paolo raised at https://lore.kernel.org/qemu-devel/[email protected]

Quoted 22 lines
Quoted 14 lines
Quoted 5 lines

Could you clarify what happens here? Should we just use a normal object, call it "Interface" and call it a day?

An interface is declared in a very similar way, but instance_size and the instance type cast macro is a bit different (because instances of interface types are never created directly).

For the draft we have here, it shouldn't make any difference if you use OBJECT_DECLARE_TYPE, because the instance type cast macros are left unused.

Quoted 7 lines

Normally the use case for interfaces is not like what I did here. Interfaces are usually attached to other classes (to declare that object instances of that class implement the methods of that interface). Using interfaces would be just an intermediate step to the solution Paolo was mentioning (dynamically adding interface to classes, see [1] above).

Quoted 3 lines

Makes sense to me,
let me know how you guys would like to proceed from here.

To me, the main issue (also raised by Paolo above) is the fact that you are doing *_enabled() checks in the module init functions. Every single use case we have for module init functions today is for unconditionally registering code or data structures provided by a code module (config group names, QOM types, block backends, multifd methods, etc.), and none of them depend on runtime options (like machine or accelerator options).

The x86_cpu_accel_init() calls, on the other hand, are not module initialization, but just one additional step of machine/accelerator/cpu initialization.

Quoted 7 lines

The thing I am still uncertain about, looking at your tree at:

https://gitlab.com/ehabkost/qemu/-/commits/work/qom-based-x86-cpu-accel

is the removal of MODULE_INIT_ACCEL_CPU, it would be way simpler to understand I think, both for CpuAccelOps and X86CPUAccel, and is actualy in my view a perfect fit for the problem that module_call_init is supposed to solve.

That was one of my goals. My first goal was the removal of the (hvm|kvm|tcg)_enabled() checks in the accel init functions. My secondary goal (and a side effect of the first goal) was making MODULE_INIT_ACCEL_CPU unnecessary.

If we are not trying to remove the *_enabled() checks in the accel init functions (nor trying to make MODULE_INIT_ACCEL_CPU unnecessary), my suggestion of using QOM doesn't make things simpler.

Let's hear what Paolo thinks.

If you want to proceed with the accel_register_call() solution suggested by Paolo, that's OK to me. I just don't think we really need it, because QOM already solves the problem for us.

Signature · 1 line
Eduardo
#27 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 140 lines · Eduardo Habkost
Quoted 131 lines · Claudio Fontana
Quoted 130 lines · Eduardo Habkost
Quoted 49 lines · Claudio Fontana
Quoted 48 lines · Eduardo Habkost
Quoted 23 lines · Paolo Bonzini
Quoted 22 lines · Eduardo Habkost
Quoted 2 lines

Adding a layer of indirect calls is not very different from monkey patching though.

I'm a little bothered by monkey patching, but I'm more bothered by having to:

(1) register (module_init()) a function (kvm_cpu_accel_register()) that
(2) register (accel_register_call()) a function (kvm_cpu_accel_init()) that
(3) register (x86_cpu_accel_init()) a data structure (X86CPUAccel kvm_cpu_accel) that
(4) will be saved in multiple QOM classes, so that
(5) we will call the right X86CPUClass.accel method at the right moment (common_class_init(), instance_init(), realizefn()),
where:
step 4 must be done before any CPU object is created
(otherwise X86CPUAccel.instance_init & X86CPUAccel.realizefn
will be silently ignored), and
step 3 must be done after all QOM types were registered.

Quoted 3 lines

You also have to consider that accel currently does not exist in usermode emulators, so that's an issue too. I would rather get a simple change in quickly, instead of designing the perfect class hierarchy.

Quoted 7 lines
Quoted 5 lines

It doesn't have to be perfect. I agree that simple is better.

To me, registering a QOM type and looking it up when necessary is simpler than the above. Even if it's a weird class having no object instances. It probably could be an interface type.

Registering a QOM type still has quite some boilerplate. [...]

We're working on that. :)

Quoted 4 lines

[...] Also registering a
QOM type has a public side effect (shows up in qom-list-types). In general I don't look at QOM unless I want its property mechanism, but maybe that's just me.

We have lots of internal-use-only types returned by qom-list-types, I don't think it's a big deal.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Perhaps another idea would be to allow adding interfaces to classes *separately from the registration of the types*. Then we can use it to add SOFTMMU_ACCEL and I386_ACCEL interfaces to a bare bones accel class, and add the accel object to usermode emulators.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

I'm not sure I follow. What do you mean by bare bones accel class, and when exactly would you add the new interfaces to the classes?

A bare bones accel class would not have init_machine and setup_post methods; those would be in a TYPE_SOFTMMU_ACCEL interface. It would still have properties (such as tb-size for TCG) and would be able to register compat properties.

[1]

Quoted 34 lines
Quoted 34 lines

Oh, I think I see. This could save us having a lot of parallel type hierarchies.

Quoted 9 lines

Where would I add it, I don't know. It could be a simple public wrapper around type_initialize_interface() if we add a new MODULE_INIT_* phase after QOM.

Or without adding a new phase, it could be a class_type->array of (interface_type, init_fn) hash table. type_initialize would look up the class_type by name, add the interfaces would to the class with type_initialize_interface, and then call the init_fn to fill in the vtable.

That sounds nice. I don't think Claudio's cleanup should be blocked until this new mechanism is ready, though.

We don't really need the type representing X86CPUAccel to be a subtype of TYPE_ACCEL or an interface implemented by current_machine->accelerator, in the first version. We just need a simple way for the CPU initialization code to find the correct X86CPUAccel struct.

While we don't have the new mechanism, it can be just a:
object_class_by_name("%s-x86-cpu-accel" % (accel->name))
call.

Below is a rough draft of what I mean. There's still lots of room for cleaning it up (especially getting rid of the X86CPUClass.common_class_init and X86CPUClass.accel fields).

git tree at https://gitlab.com/ehabkost/qemu/-/commits/work/qom-based-x86-cpu-accel

[...]

Quoted 29 lines
Quoted 15 lines

/**
- * X86CPUAccel:
- * @name: string name of the X86 CPU Accelerator
- *
+ * X86CPUAccelInterface:
* @common_class_init: initializer for the common cpu
* @instance_init: cpu instance initialization
* @realizefn: realize function, called first in x86 cpu realize

target/i386/cpu-qom.h +0 -3
@@ -85,14 +83,16 @@ struct X86CPUClass {
  * X86 CPU accelerator-specific CPU initializations
  */
 
-struct X86CPUAccel {
-    const char *name;
-

target/i386/cpu-qom.h struct X86CPUClass { · line 94

Quoted 10 lines
target/i386/cpu-qom.h +4 -1
+struct X86CPUAccelInterface {
+    ObjectClass parent_class;
     void (*common_class_init)(X86CPUClass *xcc);
     void (*instance_init)(X86CPU *cpu);
     void (*realizefn)(X86CPU *cpu, Error **errp);
 };
 
-void x86_cpu_accel_init(const X86CPUAccel *accel);
+#define TYPE_X86_CPU_ACCEL "x86-cpu-accel"
+OBJECT_DECLARE_INTERFACE(X86CPUAccel, X86CPUAccelInterface, X86_CPU_ACCEL);

I am not exactly sure what precisely you are doing here,

Quoted 2 lines

I get the general intention to use the existing interface-related "stuff" in QOM,
but I do not see any OBJECT_DECLARE_INTERFACE around, and does not seem like the other boilerplate used for interfaces.

See the git URL I sent above, for other related changes:

Quoted 7 lines
Quoted 1 line

https://gitlab.com/ehabkost/qemu/-/commits/work/qom-based-x86-cpu-accel

Aaah I missed this, there are quite a few more changes there;

for me it's great if you take it from there, I see you are developing a solution on top of the previous series.

Quoted 8 lines

I'm a bit busy with other stuff, so I'm probably not going to be able to make sure the patches are in a good shape to be submitted soon.

I don't want to impose any obstacles for the work you are doing,
either. Please consider the patch I sent (and the git tree
above) as just an example of a possible solution to the two issues
Paolo raised at https://lore.kernel.org/qemu-devel/[email protected]

Ok, thanks for the clarification,
will take those two issues up in my next attempt.

Quoted 27 lines
Quoted 23 lines
Quoted 15 lines
Quoted 6 lines

Could you clarify what happens here? Should we just use a normal object, call it "Interface" and call it a day?

An interface is declared in a very similar way, but instance_size and the instance type cast macro is a bit different (because instances of interface types are never created directly).

For the draft we have here, it shouldn't make any difference if you use OBJECT_DECLARE_TYPE, because the instance type cast macros are left unused.

Quoted 7 lines

Normally the use case for interfaces is not like what I did here. Interfaces are usually attached to other classes (to declare that object instances of that class implement the methods of that interface). Using interfaces would be just an intermediate step to the solution Paolo was mentioning (dynamically adding interface to classes, see [1] above).

Quoted 3 lines

Makes sense to me,
let me know how you guys would like to proceed from here.

Quoted 7 lines

To me, the main issue (also raised by Paolo above) is the fact that you are doing *_enabled() checks in the module init functions. Every single use case we have for module init functions today is for unconditionally registering code or data structures provided by a code module (config group names, QOM types, block backends, multifd methods, etc.), and none of them depend on runtime options (like machine or accelerator options).

Ok, no _enabled() checks, got it.

Just to note, since _enabled() has multiple meanings depending on configuration (CONFIG_KVM), the _enabled() used in kvm/cpu.c has actually the meaning of:

if (accelerator_finally_chosen == KVM).

But we can refactor this implicit check out, and make it instead a

accel_cpu_init("kvm"), like you suggest, so that the ifs disappear.

Quoted 24 lines

The x86_cpu_accel_init() calls, on the other hand, are not module initialization, but just one additional step of machine/accelerator/cpu initialization.

Quoted 7 lines

The thing I am still uncertain about, looking at your tree at:

https://gitlab.com/ehabkost/qemu/-/commits/work/qom-based-x86-cpu-accel

is the removal of MODULE_INIT_ACCEL_CPU, it would be way simpler to understand I think, both for CpuAccelOps and X86CPUAccel, and is actualy in my view a perfect fit for the problem that module_call_init is supposed to solve.

That was one of my goals. My first goal was the removal of the (hvm|kvm|tcg)_enabled() checks in the accel init functions. My secondary goal (and a side effect of the first goal) was making MODULE_INIT_ACCEL_CPU unnecessary.

If we are not trying to remove the *_enabled() checks in the accel init functions (nor trying to make MODULE_INIT_ACCEL_CPU unnecessary), my suggestion of using QOM doesn't make things simpler.

Quoted 6 lines

Let's hear what Paolo thinks.

If you want to proceed with the accel_register_call() solution suggested by Paolo, that's OK to me. I just don't think we really need it, because QOM already solves the problem for us.

Ok, thanks for all the input, will need some time to process,

thanks,

Claudio

#28 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 235 lines · Claudio Fontana
Quoted 140 lines · Eduardo Habkost
Quoted 131 lines · Claudio Fontana
Quoted 130 lines · Eduardo Habkost
Quoted 49 lines · Claudio Fontana
Quoted 48 lines · Eduardo Habkost
Quoted 23 lines · Paolo Bonzini
Quoted 22 lines · Eduardo Habkost
Quoted 2 lines

Adding a layer of indirect calls is not very different from monkey patching though.

I'm a little bothered by monkey patching, but I'm more bothered by having to:

(1) register (module_init()) a function (kvm_cpu_accel_register()) that
(2) register (accel_register_call()) a function (kvm_cpu_accel_init()) that
(3) register (x86_cpu_accel_init()) a data structure (X86CPUAccel kvm_cpu_accel) that
(4) will be saved in multiple QOM classes, so that
(5) we will call the right X86CPUClass.accel method at the right moment (common_class_init(), instance_init(), realizefn()),
where:
step 4 must be done before any CPU object is created
(otherwise X86CPUAccel.instance_init & X86CPUAccel.realizefn
will be silently ignored), and
step 3 must be done after all QOM types were registered.

Quoted 3 lines

You also have to consider that accel currently does not exist in usermode emulators, so that's an issue too. I would rather get a simple change in quickly, instead of designing the perfect class hierarchy.

Quoted 7 lines
Quoted 5 lines

It doesn't have to be perfect. I agree that simple is better.

To me, registering a QOM type and looking it up when necessary is simpler than the above. Even if it's a weird class having no object instances. It probably could be an interface type.

Registering a QOM type still has quite some boilerplate. [...]

We're working on that. :)

Quoted 4 lines

[...] Also registering a
QOM type has a public side effect (shows up in qom-list-types). In general I don't look at QOM unless I want its property mechanism, but maybe that's just me.

We have lots of internal-use-only types returned by qom-list-types, I don't think it's a big deal.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Perhaps another idea would be to allow adding interfaces to classes *separately from the registration of the types*. Then we can use it to add SOFTMMU_ACCEL and I386_ACCEL interfaces to a bare bones accel class, and add the accel object to usermode emulators.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

I'm not sure I follow. What do you mean by bare bones accel class, and when exactly would you add the new interfaces to the classes?

A bare bones accel class would not have init_machine and setup_post methods; those would be in a TYPE_SOFTMMU_ACCEL interface. It would still have properties (such as tb-size for TCG) and would be able to register compat properties.

[1]

Quoted 34 lines
Quoted 34 lines

Oh, I think I see. This could save us having a lot of parallel type hierarchies.

Quoted 9 lines

Where would I add it, I don't know. It could be a simple public wrapper around type_initialize_interface() if we add a new MODULE_INIT_* phase after QOM.

Or without adding a new phase, it could be a class_type->array of (interface_type, init_fn) hash table. type_initialize would look up the class_type by name, add the interfaces would to the class with type_initialize_interface, and then call the init_fn to fill in the vtable.

That sounds nice. I don't think Claudio's cleanup should be blocked until this new mechanism is ready, though.

We don't really need the type representing X86CPUAccel to be a subtype of TYPE_ACCEL or an interface implemented by current_machine->accelerator, in the first version. We just need a simple way for the CPU initialization code to find the correct X86CPUAccel struct.

While we don't have the new mechanism, it can be just a:
object_class_by_name("%s-x86-cpu-accel" % (accel->name))
call.

Below is a rough draft of what I mean. There's still lots of room for cleaning it up (especially getting rid of the X86CPUClass.common_class_init and X86CPUClass.accel fields).

git tree at https://gitlab.com/ehabkost/qemu/-/commits/work/qom-based-x86-cpu-accel

[...]

Quoted 29 lines
Quoted 15 lines

/**
- * X86CPUAccel:
- * @name: string name of the X86 CPU Accelerator
- *
+ * X86CPUAccelInterface:
* @common_class_init: initializer for the common cpu
* @instance_init: cpu instance initialization
* @realizefn: realize function, called first in x86 cpu realize

target/i386/cpu-qom.h +0 -3
@@ -85,14 +83,16 @@ struct X86CPUClass {
  * X86 CPU accelerator-specific CPU initializations
  */
 
-struct X86CPUAccel {
-    const char *name;
-

target/i386/cpu-qom.h struct X86CPUClass { · line 94

Quoted 10 lines
target/i386/cpu-qom.h +4 -1
+struct X86CPUAccelInterface {
+    ObjectClass parent_class;
     void (*common_class_init)(X86CPUClass *xcc);
     void (*instance_init)(X86CPU *cpu);
     void (*realizefn)(X86CPU *cpu, Error **errp);
 };
 
-void x86_cpu_accel_init(const X86CPUAccel *accel);
+#define TYPE_X86_CPU_ACCEL "x86-cpu-accel"
+OBJECT_DECLARE_INTERFACE(X86CPUAccel, X86CPUAccelInterface, X86_CPU_ACCEL);

I am not exactly sure what precisely you are doing here,

Quoted 2 lines

I get the general intention to use the existing interface-related "stuff" in QOM,
but I do not see any OBJECT_DECLARE_INTERFACE around, and does not seem like the other boilerplate used for interfaces.

See the git URL I sent above, for other related changes:

Quoted 7 lines
Quoted 1 line

https://gitlab.com/ehabkost/qemu/-/commits/work/qom-based-x86-cpu-accel

Aaah I missed this, there are quite a few more changes there;

for me it's great if you take it from there, I see you are developing a solution on top of the previous series.

Quoted 8 lines

I'm a bit busy with other stuff, so I'm probably not going to be able to make sure the patches are in a good shape to be submitted soon.

I don't want to impose any obstacles for the work you are doing,
either. Please consider the patch I sent (and the git tree
above) as just an example of a possible solution to the two issues
Paolo raised at https://lore.kernel.org/qemu-devel/[email protected]

Ok, thanks for the clarification,
will take those two issues up in my next attempt.

Quoted 27 lines
Quoted 23 lines
Quoted 15 lines
Quoted 6 lines

Could you clarify what happens here? Should we just use a normal object, call it "Interface" and call it a day?

An interface is declared in a very similar way, but instance_size and the instance type cast macro is a bit different (because instances of interface types are never created directly).

For the draft we have here, it shouldn't make any difference if you use OBJECT_DECLARE_TYPE, because the instance type cast macros are left unused.

Quoted 7 lines

Normally the use case for interfaces is not like what I did here. Interfaces are usually attached to other classes (to declare that object instances of that class implement the methods of that interface). Using interfaces would be just an intermediate step to the solution Paolo was mentioning (dynamically adding interface to classes, see [1] above).

Quoted 3 lines

Makes sense to me,
let me know how you guys would like to proceed from here.

Quoted 7 lines

To me, the main issue (also raised by Paolo above) is the fact that you are doing *_enabled() checks in the module init functions. Every single use case we have for module init functions today is for unconditionally registering code or data structures provided by a code module (config group names, QOM types, block backends, multifd methods, etc.), and none of them depend on runtime options (like machine or accelerator options).

Ok, no _enabled() checks, got it.

Just to note, since _enabled() has multiple meanings depending on configuration (CONFIG_KVM), the _enabled() used in kvm/cpu.c has actually the meaning of:

if (accelerator_finally_chosen == KVM).

But we can refactor this implicit check out, and make it instead a

accel_cpu_init("kvm"), like you suggest, so that the ifs disappear.

Quoted 24 lines

The x86_cpu_accel_init() calls, on the other hand, are not module initialization, but just one additional step of machine/accelerator/cpu initialization.

Quoted 7 lines

The thing I am still uncertain about, looking at your tree at:

https://gitlab.com/ehabkost/qemu/-/commits/work/qom-based-x86-cpu-accel

is the removal of MODULE_INIT_ACCEL_CPU, it would be way simpler to understand I think, both for CpuAccelOps and X86CPUAccel, and is actualy in my view a perfect fit for the problem that module_call_init is supposed to solve.

That was one of my goals. My first goal was the removal of the (hvm|kvm|tcg)_enabled() checks in the accel init functions. My secondary goal (and a side effect of the first goal) was making MODULE_INIT_ACCEL_CPU unnecessary.

If we are not trying to remove the *_enabled() checks in the accel init functions (nor trying to make MODULE_INIT_ACCEL_CPU unnecessary), my suggestion of using QOM doesn't make things simpler.

Quoted 6 lines

Let's hear what Paolo thinks.

If you want to proceed with the accel_register_call() solution suggested by Paolo, that's OK to me. I just don't think we really need it, because QOM already solves the problem for us.

Quoted 6 lines

Ok, thanks for all the input, will need some time to process,

thanks,

Claudio

One idea that came to mind is, why not extend accel.h to user mode?

It already contains

#ifndef CONFIG_USER_ONLY

parts, so maybe it was meant to be used by both, and just happened to end up confined to include/softmmu ?

Basically I was thinking, we could have an AccelState and an AccelClass for user mode as well (without bringing in the whole machine thing), and from there we could use current_accel() to build up the right name for the chosen accelerator?

Ciao,

Claudio

#29 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 6 lines · Claudio Fontana

One idea that came to mind is, why not extend accel.h to user mode?

It already contains

#ifndef CONFIG_USER_ONLY

Quoted 7 lines

parts, so maybe it was meant to be used by both, and just happened to end up confined to include/softmmu ?

Basically I was thinking, we could have an AccelState and an AccelClass for user mode as well (without bringing in the whole machine thing), and from there we could use current_accel() to build up the right name for the chosen accelerator?

Yes, extending the accelerator class to usermode emulation is certainly a good idea.

Paolo

#30 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 15 lines · Paolo Bonzini
Quoted 6 lines · Claudio Fontana

One idea that came to mind is, why not extend accel.h to user mode?

It already contains

#ifndef CONFIG_USER_ONLY

Quoted 7 lines

parts, so maybe it was meant to be used by both, and just happened to end up confined to include/softmmu ?

Basically I was thinking, we could have an AccelState and an AccelClass for user mode as well (without bringing in the whole machine thing), and from there we could use current_accel() to build up the right name for the chosen accelerator?

Quoted 5 lines

Yes, extending the accelerator class to usermode emulation is certainly a good idea.

Paolo

Thanks, I'll work on this option.

Btw considering that CpusAccel for tcg is actually three different interfaces (for mttcg, for icount, and plain RR), it will be tough to, in the stated objective, "remove all conditionals", even after removing the tcg_enabled().

I wonder how you see this issue (patches for 3 TCG split are in Richard's queue atm).

static void tcg_accel_cpu_init(void)

{
    if (tcg_enabled()) {
        TCGState *s = TCG_STATE(current_accel());

        if (s->mttcg_enabled) {
            cpus_register_accel(&tcg_cpus_mttcg);
        } else if (icount_enabled()) {
            cpus_register_accel(&tcg_cpus_icount);
        } else {
            cpus_register_accel(&tcg_cpus_rr);
        }
    }
}

Ciao,

Claudio

#31 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

Quoted 4 lines · Claudio Fontana

Thanks, I'll work on this option.

Btw considering that CpusAccel for tcg is actually three different interfaces (for mttcg, for icount, and plain RR), it will be tough to, in the stated objective, "remove all conditionals", even after removing the tcg_enabled().

I'm not sure removing all conditionals is a goal in and of itself, but of course keeping the conditionals more local should be a good.

Paolo

Quoted 15 lines

I wonder how you see this issue (patches for 3 TCG split are in Richard's queue atm).

static void tcg_accel_cpu_init(void)

{
     if (tcg_enabled()) {
         TCGState *s = TCG_STATE(current_accel());

         if (s->mttcg_enabled) {
             cpus_register_accel(&tcg_cpus_mttcg);
         } else if (icount_enabled()) {
             cpus_register_accel(&tcg_cpus_icount);
         } else {
             cpus_register_accel(&tcg_cpus_rr);
         }
     }
#32 lore raw

Re: [RFC v3 8/9] module: introduce MODULE_INIT_ACCEL_CPU

In reply to Claudio Fontana
Quoted 19 lines · Claudio Fontana
Quoted 18 lines · Paolo Bonzini
Quoted 6 lines · Claudio Fontana

One idea that came to mind is, why not extend accel.h to user mode?

It already contains

#ifndef CONFIG_USER_ONLY

Quoted 7 lines

parts, so maybe it was meant to be used by both, and just happened to end up confined to include/softmmu ?

Basically I was thinking, we could have an AccelState and an AccelClass for user mode as well (without bringing in the whole machine thing), and from there we could use current_accel() to build up the right name for the chosen accelerator?

Yes, extending the accelerator class to usermode emulation is certainly a good idea.

Quoted 7 lines
Quoted 2 lines

Paolo

Thanks, I'll work on this option.

Btw considering that CpusAccel for tcg is actually three different interfaces (for mttcg, for icount, and plain RR), it will be tough to, in the stated objective, "remove all conditionals", even after removing the tcg_enabled().

The main issue were the conditionals inside module init function. They are completely OK inside accel-specific methods.

Quoted 4 lines

I wonder how you see this issue (patches for 3 TCG split are in Richard's queue atm).

static void tcg_accel_cpu_init(void)

Quoted 13 lines
{
    if (tcg_enabled()) {
        TCGState *s = TCG_STATE(current_accel());

        if (s->mttcg_enabled) {
            cpus_register_accel(&tcg_cpus_mttcg);
        } else if (icount_enabled()) {
            cpus_register_accel(&tcg_cpus_icount);
        } else {
            cpus_register_accel(&tcg_cpus_rr);
        }
    }
}

Probably what we are missing here is a non-softmmu-specific AccelClass.init() method?

Signature · 1 line
Eduardo
#33 lore raw

RFC v3 9/9 i386: split cpu accelerators from cpu.c

In reply to Claudio Fontana

split cpu.c into:

cpu.c            cpuid and common x86 cpu functionality
host-cpu.c       host x86 cpu functions and "host" cpu type
kvm/cpu.c        KVM x86 cpu type
hvf/cpu.c        HVF x86 cpu type
tcg/cpu.c        TCG x86 cpu type

The accel interface of the X86CPUClass is set at MODULE_INIT_ACCEL_CPU time, when the accelerator is known.

---
 MAINTAINERS                 |   2 +-
 bsd-user/main.c             |   1 +
 hw/i386/pc_piix.c           |   1 +
 linux-user/main.c           |   1 +
 target/i386/cpu-qom.h       |  23 +++
 target/i386/cpu.c           | 395 +++++-------------------------------
 target/i386/cpu.h           |  20 +-
 target/i386/host-cpu.c      | 196 ++++++++++++++++++
 target/i386/host-cpu.h      |  20 ++
 target/i386/hvf/cpu.c       |  64 ++++++
 target/i386/hvf/meson.build |   1 +
 target/i386/kvm/cpu.c       | 145 +++++++++++++
 target/i386/kvm/kvm-cpu.h   |  41 ++++
 target/i386/kvm/kvm.c       |   3 +-
 target/i386/kvm/meson.build |   7 +-
 target/i386/meson.build     |   8 +-
 target/i386/tcg-cpu.c       |  71 -------
 target/i386/tcg-cpu.h       |  15 --
 target/i386/tcg/cpu.c       | 168 +++++++++++++++
 target/i386/tcg/meson.build |   3 +-
 20 files changed, 736 insertions(+), 449 deletions(-)
 create mode 100644 target/i386/host-cpu.c
 create mode 100644 target/i386/host-cpu.h
 create mode 100644 target/i386/hvf/cpu.c
 create mode 100644 target/i386/kvm/cpu.c
 create mode 100644 target/i386/kvm/kvm-cpu.h
 delete mode 100644 target/i386/tcg-cpu.c
 delete mode 100644 target/i386/tcg-cpu.h
 create mode 100644 target/i386/tcg/cpu.c
MAINTAINERS +1 -1
index e892dd2220..9782728e0c 100644
@@ -336,7 +336,7 @@ M: Paolo Bonzini <[email protected]>
 M: Richard Henderson <[email protected]>
 M: Eduardo Habkost <[email protected]>
 S: Maintained
-F: target/i386/
+F: target/i386/tcg/
 F: tests/tcg/i386/
 F: tests/tcg/x86_64/
 F: hw/i386/
bsd-user/main.c +1 -0
index 0a918e8f74..9f88ae952a 100644
@@ -909,6 +909,7 @@ int main(int argc, char **argv)
 
     /* init tcg before creating CPUs and to get qemu_host_page_size */
     tcg_exec_init(0);
+    module_call_init(MODULE_INIT_ACCEL_CPU);
 
     cpu_type = parse_cpu_option(cpu_model);
     cpu = cpu_create(cpu_type);
hw/i386/pc_piix.c +1 -0
index 13d1628f13..d3f013f3a1 100644
@@ -64,6 +64,7 @@
 #include "hw/hyperv/vmbus-bridge.h"
 #include "hw/mem/nvdimm.h"
 #include "hw/i386/acpi-build.h"
+#include "kvm/kvm-cpu.h"
 
 #define MAX_IDE_BUS 2
 
linux-user/main.c +1 -0
index 24d1eb73ad..a745901d86 100644
@@ -704,6 +704,7 @@ int main(int argc, char **argv, char **envp)
 
     /* init tcg before creating CPUs and to get qemu_host_page_size */
     tcg_exec_init(0);
+    module_call_init(MODULE_INIT_ACCEL_CPU);
 
     cpu = cpu_create(cpu_type);
     env = cpu->env_ptr;
target/i386/cpu-qom.h +23 -0
index f9923cee04..79fcbd3b9b 100644
@@ -34,6 +34,7 @@ OBJECT_DECLARE_TYPE(X86CPU, X86CPUClass,
                     X86_CPU)
 
 typedef struct X86CPUModel X86CPUModel;
+typedef struct X86CPUAccel X86CPUAccel;
 
 /**
  * X86CPUClass:
@@ -69,7 +70,29 @@ struct X86CPUClass {
     DeviceRealize parent_realize;
     DeviceUnrealize parent_unrealize;
     DeviceReset parent_reset;
+
+    const X86CPUAccel *accel;
+};
+
+/**
+ * X86CPUAccel:
+ * @name: string name of the X86 CPU Accelerator
+ *
+ * @common_class_init: initializer for the common cpu
+ * @instance_init: cpu instance initialization
+ * @realizefn: realize function, called first in x86 cpu realize
+ *
+ * X86 CPU accelerator-specific CPU initializations
+ */
+
+struct X86CPUAccel {
+    const char *name;
+
+    void (*common_class_init)(X86CPUClass *xcc);
+    void (*instance_init)(X86CPU *cpu);
+    void (*realizefn)(X86CPU *cpu, Error **errp);
 };
 
+void x86_cpu_accel_init(const X86CPUAccel *accel);
 
 #endif
target/i386/cpu.c +50 -345

545 lines not on this page · raw message

target/i386/cpu.h +8 -12
index a0d64613dc..b3e39fc631 100644
@@ -1905,13 +1905,20 @@ int cpu_x86_signal_handler(int host_signum, void *pinfo,
                            void *puc);
 
 /* cpu.c */
+void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,
+                              uint32_t vendor2, uint32_t vendor3);
+typedef struct PropValue {
+    const char *prop, *value;
+} PropValue;
+void x86_cpu_apply_props(X86CPU *cpu, PropValue *props);
+
+/* cpu.c other functions (cpuid) */
 void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
                    uint32_t *eax, uint32_t *ebx,
                    uint32_t *ecx, uint32_t *edx);
 void cpu_clear_apic_feature(CPUX86State *env);
 void host_cpuid(uint32_t function, uint32_t count,
                 uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx);
-void host_vendor_fms(char *vendor, int *family, int *model, int *stepping);
 
 /* helper.c */
 void x86_cpu_set_a20(X86CPU *cpu, int a20_state);
@@ -2111,17 +2118,6 @@ void cpu_report_tpr_access(CPUX86State *env, TPRAccess access);
 void apic_handle_tpr_access_report(DeviceState *d, target_ulong ip,
                                    TPRAccess access);
 
-
-/* Change the value of a KVM-specific default
- *
- * If value is NULL, no default will be set and the original
- * value from the CPU model table will be kept.
- *
- * It is valid to call this function only for properties that
- * are already present in the kvm_default_props table.
- */
-void x86_cpu_change_kvm_default(const char *prop, const char *value);
-
 /* Special values for X86CPUVersion: */
 
 /* Resolve to latest CPU version */
target/i386/host-cpu.c new file +196 -0

196 lines not on this page · raw message

target/i386/host-cpu.h new file +20 -0
new file mode 100644
index 0000000000..5cebb415eb
@@ -0,0 +1,20 @@
+/*
+ * x86 host CPU type initialization and host CPU functions
+ *
+ * Copyright 2020 SUSE LLC
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef HOST_CPU_H
+#define HOST_CPU_H
+
+void host_cpu_class_init(X86CPUClass *xcc);
+void host_cpu_instance_init(X86CPU *cpu);
+void host_cpu_max_instance_init(X86CPU *cpu);
+void host_cpu_realizefn(X86CPU *cpu, Error **errp);
+
+void host_cpu_vendor_fms(char *vendor, int *family, int *model, int *stepping);
+
+#endif /* HOST_CPU_H */
target/i386/hvf/cpu.c new file +64 -0

64 lines not on this page · raw message

target/i386/hvf/meson.build +1 -0
index 409c9a3f14..a7fba5724c 100644
@@ -10,4 +10,5 @@ i386_softmmu_ss.add(when: [hvf, 'CONFIG_HVF'], if_true: files(
   'x86_mmu.c',
   'x86_task.c',
   'x86hvf.c',
+  'cpu.c',
 ))
target/i386/kvm/cpu.c new file +145 -0

145 lines not on this page · raw message

target/i386/kvm/kvm-cpu.h new file +41 -0

41 lines not on this page · raw message

target/i386/kvm/kvm.c +2 -1
index a2934dda02..35c86fdba6 100644
@@ -22,6 +22,7 @@
 #include "standard-headers/asm-x86/kvm_para.h"
 
 #include "cpu.h"
+#include "host-cpu.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/hw_accel.h"
 #include "sysemu/kvm_int.h"
@@ -285,7 +286,7 @@ static bool host_tsx_broken(void)
     int family, model, stepping;\
     char vendor[CPUID_VENDOR_SZ + 1];
 
-    host_vendor_fms(vendor, &family, &model, &stepping);
+    host_cpu_vendor_fms(vendor, &family, &model, &stepping);
 
     /* Check if we are running on a Haswell host known to have broken TSX */
     return !strcmp(vendor, CPUID_VENDOR_INTEL) &&
target/i386/kvm/meson.build +6 -1
index 1d66559187..0bc3724eb3 100644
@@ -1,3 +1,8 @@
 i386_ss.add(when: 'CONFIG_KVM', if_false: files('kvm-stub.c'))
-i386_softmmu_ss.add(when: 'CONFIG_KVM', if_true: files('kvm.c'))
+
+i386_softmmu_ss.add(when: 'CONFIG_KVM', if_true: files(
+  'kvm.c',
+  'cpu.c',
+))
+
 i386_softmmu_ss.add(when: 'CONFIG_HYPERV', if_true: files('hyperv.c'), if_false: files('hyperv-stub.c'))
target/i386/meson.build +6 -2
index 9c20208e5a..4e6e915e7f 100644
@@ -6,8 +6,12 @@ i386_ss.add(files(
   'xsave_helper.c',
   'cpu-dump.c',
 ))
-i386_ss.add(when: 'CONFIG_TCG', if_true: files('tcg-cpu.c'))
-i386_ss.add(when: 'CONFIG_SEV', if_true: files('sev.c'), if_false: files('sev-stub.c'))
+
+i386_ss.add(when: 'CONFIG_SEV', if_true: files('host-cpu.c', 'sev.c'), if_false: files('sev-stub.c'))
+
+# x86 cpu type
+i386_ss.add(when: 'CONFIG_KVM', if_true: files('host-cpu.c'))
+i386_ss.add(when: 'CONFIG_HVF', if_true: files('host-cpu.c'))
 
 i386_softmmu_ss = ss.source_set()
 i386_softmmu_ss.add(files(
target/i386/tcg-cpu.c deleted +0 -71

71 lines not on this page · raw message

target/i386/tcg-cpu.h deleted +0 -15
deleted file mode 100644
index 81f02e562e..0000000000
@@ -1,15 +0,0 @@
-/*
- * i386 TCG CPU class initialization
- *
- * Copyright 2020 SUSE LLC
- *
- * This work is licensed under the terms of the GNU GPL, version 2 or later.
- * See the COPYING file in the top-level directory.
- */
-
-#ifndef TCG_CPU_H
-#define TCG_CPU_H
-
-void tcg_cpu_common_class_init(CPUClass *cc);
-
-#endif /* TCG_CPU_H */
target/i386/tcg/cpu.c new file +168 -0

168 lines not on this page · raw message

target/i386/tcg/meson.build +2 -1
index 02794226c2..9e439df9c7 100644
@@ -10,4 +10,5 @@ i386_ss.add(when: 'CONFIG_TCG', if_true: files(
   'seg_helper.c',
   'smm_helper.c',
   'svm_helper.c',
-  'translate.c'), if_false: files('tcg-stub.c'))
+  'translate.c',
+  'cpu.c'), if_false: files('tcg-stub.c'))
Signature · 1 line
2.26.2
#34 lore raw

Re: [RFC v3 9/9] i386: split cpu accelerators from cpu.c

Quoted 13 lines · Claudio Fontana

split cpu.c into:

cpu.c            cpuid and common x86 cpu functionality
host-cpu.c       host x86 cpu functions and "host" cpu type
kvm/cpu.c        KVM x86 cpu type
hvf/cpu.c        HVF x86 cpu type
tcg/cpu.c        TCG x86 cpu type

The accel interface of the X86CPUClass is set at MODULE_INIT_ACCEL_CPU time, when the accelerator is known.

---

[...]

Quoted 5 lines

+/**
+ * X86CPUAccel:
+ * @name: string name of the X86 CPU Accelerator
+ *
+ * @common_class_init: initializer for the common cpu

So this will be called for every single CPU class.

Quoted 15 lines

+ * @instance_init: cpu instance initialization
+ * @realizefn: realize function, called first in x86 cpu realize
+ *
+ * X86 CPU accelerator-specific CPU initializations
+ */

target/i386/cpu-qom.h +8 -0
+
+struct X86CPUAccel {
+    const char *name;
+
+    void (*common_class_init)(X86CPUClass *xcc);
+    void (*instance_init)(X86CPU *cpu);
+    void (*realizefn)(X86CPU *cpu, Error **errp);
 };
 
+void x86_cpu_accel_init(const X86CPUAccel *accel);

[...]

target/i386/cpu.c static void x86_cpu_register_types(void) · line 7070

Quoted 13 lines
target/i386/cpu.c +13 -0
+static void x86_cpu_accel_init_aux(ObjectClass *klass, void *opaque)
+{
+    X86CPUClass *xcc = X86_CPU_CLASS(klass);
+    const X86CPUAccel **accel = opaque;
+
+    xcc->accel = *accel;
+    xcc->accel->common_class_init(xcc);
+}
+
+void x86_cpu_accel_init(const X86CPUAccel *accel)
+{
+    object_class_foreach(x86_cpu_accel_init_aux, TYPE_X86_CPU, false, &accel);
+}

This matches the documented behavior.

[...]

Quoted 8 lines
target/i386/host-cpu.c new file +8 -0
+void host_cpu_class_init(X86CPUClass *xcc)
+{
+    xcc->host_cpuid_required = true;
+    xcc->ordering = 8;
+    xcc->model_description =
+        g_strdup_printf("%s processor with all supported host features ",
+                        xcc->accel->name);
+}

[...]

target/i386/hvf/cpu.c line 20

Quoted 3 lines
target/i386/hvf/cpu.c new file +3 -0
+static void hvf_cpu_common_class_init(X86CPUClass *xcc)
+{
+    host_cpu_class_init(xcc);

Why are you calling host_cpu_class_init() for all CPU types?

Quoted 1 line

+}

[...]

target/i386/kvm/cpu.c line 48

Quoted 4 lines
target/i386/kvm/cpu.c new file +4 -0
+static void kvm_cpu_common_class_init(X86CPUClass *xcc)
+{
+    host_cpu_class_init(xcc);
+}

Same question as above.

Signature · 1 line
Eduardo
#35 lore raw

Re: [RFC v3 9/9] i386: split cpu accelerators from cpu.c

Hi,

Quoted 14 lines · Eduardo Habkost
Quoted 8 lines · Claudio Fontana

split cpu.c into:

cpu.c            cpuid and common x86 cpu functionality
host-cpu.c       host x86 cpu functions and "host" cpu type
kvm/cpu.c        KVM x86 cpu type
hvf/cpu.c        HVF x86 cpu type
tcg/cpu.c        TCG x86 cpu type
Quoted 5 lines

The accel interface of the X86CPUClass is set at MODULE_INIT_ACCEL_CPU time, when the accelerator is known.

---

Quoted 8 lines

[...]

Quoted 5 lines

+/**
+ * X86CPUAccel:
+ * @name: string name of the X86 CPU Accelerator
+ *
+ * @common_class_init: initializer for the common cpu

So this will be called for every single CPU class.

Not really, it's called for every TYPE_X86_CPU cpu class (if an accel interface is registered).

This function extends the existing x86_cpu_common_class_init (target/i386/cpu.c), where some methods of the base class CPUClass are set.

Quoted 43 lines
Quoted 16 lines

+ * @instance_init: cpu instance initialization
+ * @realizefn: realize function, called first in x86 cpu realize
+ *
+ * X86 CPU accelerator-specific CPU initializations
+ */

target/i386/cpu-qom.h +8 -0
+
+struct X86CPUAccel {
+    const char *name;
+
+    void (*common_class_init)(X86CPUClass *xcc);
+    void (*instance_init)(X86CPU *cpu);
+    void (*realizefn)(X86CPU *cpu, Error **errp);
 };
 
+void x86_cpu_accel_init(const X86CPUAccel *accel);

[...]

target/i386/cpu.c static void x86_cpu_register_types(void) · line 7070

Quoted 13 lines
target/i386/cpu.c +13 -0
+static void x86_cpu_accel_init_aux(ObjectClass *klass, void *opaque)
+{
+    X86CPUClass *xcc = X86_CPU_CLASS(klass);
+    const X86CPUAccel **accel = opaque;
+
+    xcc->accel = *accel;
+    xcc->accel->common_class_init(xcc);
+}
+
+void x86_cpu_accel_init(const X86CPUAccel *accel)
+{
+    object_class_foreach(x86_cpu_accel_init_aux, TYPE_X86_CPU, false, &accel);
+}

This matches the documented behavior.

[...]

Quoted 8 lines
target/i386/host-cpu.c new file +8 -0
+void host_cpu_class_init(X86CPUClass *xcc)
+{
+    xcc->host_cpuid_required = true;
+    xcc->ordering = 8;
+    xcc->model_description =
+        g_strdup_printf("%s processor with all supported host features ",
+                        xcc->accel->name);
+}

[...]

Quoted 5 lines

target/i386/hvf/cpu.c line 20

Quoted 3 lines
target/i386/hvf/cpu.c new file +3 -0
+static void hvf_cpu_common_class_init(X86CPUClass *xcc)
+{
+    host_cpu_class_init(xcc);

Why are you calling host_cpu_class_init() for all CPU types?

I am not..

Quoted 10 lines
Quoted 2 lines

+}

[...]

target/i386/kvm/cpu.c line 48

Quoted 4 lines
target/i386/kvm/cpu.c new file +4 -0
+static void kvm_cpu_common_class_init(X86CPUClass *xcc)
+{
+    host_cpu_class_init(xcc);
+}

Same question as above.

Ciao,

Claudio

#36 lore raw

Re: [RFC v3 9/9] i386: split cpu accelerators from cpu.c

Quoted 24 lines · Claudio Fontana

Hi,

Quoted 21 lines · Eduardo Habkost
Quoted 13 lines · Claudio Fontana

split cpu.c into:

cpu.c            cpuid and common x86 cpu functionality
host-cpu.c       host x86 cpu functions and "host" cpu type
kvm/cpu.c        KVM x86 cpu type
hvf/cpu.c        HVF x86 cpu type
tcg/cpu.c        TCG x86 cpu type

The accel interface of the X86CPUClass is set at MODULE_INIT_ACCEL_CPU time, when the accelerator is known.

---

[...]

Quoted 5 lines

+/**
+ * X86CPUAccel:
+ * @name: string name of the X86 CPU Accelerator
+ *
+ * @common_class_init: initializer for the common cpu

Quoted 3 lines
Quoted 1 line

So this will be called for every single CPU class.

Not really, it's called for every TYPE_X86_CPU cpu class (if an accel interface is registered).

This means every single non-abstract CPU class in
qemu-system-x86_64, correct?

Quoted 47 lines

This function extends the existing x86_cpu_common_class_init (target/i386/cpu.c), where some methods of the base class CPUClass are set.

Quoted 43 lines
Quoted 16 lines

+ * @instance_init: cpu instance initialization
+ * @realizefn: realize function, called first in x86 cpu realize
+ *
+ * X86 CPU accelerator-specific CPU initializations
+ */

target/i386/cpu-qom.h +8 -0
+
+struct X86CPUAccel {
+    const char *name;
+
+    void (*common_class_init)(X86CPUClass *xcc);
+    void (*instance_init)(X86CPU *cpu);
+    void (*realizefn)(X86CPU *cpu, Error **errp);
 };
 
+void x86_cpu_accel_init(const X86CPUAccel *accel);

[...]

target/i386/cpu.c static void x86_cpu_register_types(void) · line 7070

Quoted 13 lines
target/i386/cpu.c +13 -0
+static void x86_cpu_accel_init_aux(ObjectClass *klass, void *opaque)
+{
+    X86CPUClass *xcc = X86_CPU_CLASS(klass);
+    const X86CPUAccel **accel = opaque;
+
+    xcc->accel = *accel;
+    xcc->accel->common_class_init(xcc);
+}
+
+void x86_cpu_accel_init(const X86CPUAccel *accel)
+{
+    object_class_foreach(x86_cpu_accel_init_aux, TYPE_X86_CPU, false, &accel);
+}

This matches the documented behavior.

[...]

Quoted 8 lines
target/i386/host-cpu.c new file +8 -0
+void host_cpu_class_init(X86CPUClass *xcc)
+{
+    xcc->host_cpuid_required = true;
+    xcc->ordering = 8;
+    xcc->model_description =
+        g_strdup_printf("%s processor with all supported host features ",
+                        xcc->accel->name);
+}

[...]

Quoted 7 lines
Quoted 5 lines

target/i386/hvf/cpu.c line 20

Quoted 3 lines
target/i386/hvf/cpu.c new file +3 -0
+static void hvf_cpu_common_class_init(X86CPUClass *xcc)
+{
+    host_cpu_class_init(xcc);

Why are you calling host_cpu_class_init() for all CPU types?

I am not..

I don't get it. You are calling host_cpu_class_init() for every single non-abstract TYPE_X86_CPU subclass (which includes all CPU models in qemu-system-x86_64), and I don't understand why, or if this is really intentional.

Quoted 16 lines
Quoted 11 lines
Quoted 3 lines

+}

[...]

target/i386/kvm/cpu.c line 48

Quoted 4 lines
target/i386/kvm/cpu.c new file +4 -0
+static void kvm_cpu_common_class_init(X86CPUClass *xcc)
+{
+    host_cpu_class_init(xcc);
+}

Same question as above.

Ciao,

Claudio

Signature · 1 line
Eduardo
#37 lore raw

Re: [RFC v3 9/9] i386: split cpu accelerators from cpu.c

Quoted 85 lines · Eduardo Habkost
Quoted 24 lines · Claudio Fontana

Hi,

Quoted 21 lines · Eduardo Habkost
Quoted 13 lines · Claudio Fontana

split cpu.c into:

cpu.c            cpuid and common x86 cpu functionality
host-cpu.c       host x86 cpu functions and "host" cpu type
kvm/cpu.c        KVM x86 cpu type
hvf/cpu.c        HVF x86 cpu type
tcg/cpu.c        TCG x86 cpu type

The accel interface of the X86CPUClass is set at MODULE_INIT_ACCEL_CPU time, when the accelerator is known.

---

[...]

Quoted 5 lines

+/**
+ * X86CPUAccel:
+ * @name: string name of the X86 CPU Accelerator
+ *
+ * @common_class_init: initializer for the common cpu

Quoted 3 lines
Quoted 1 line

So this will be called for every single CPU class.

Not really, it's called for every TYPE_X86_CPU cpu class (if an accel interface is registered).

This means every single non-abstract CPU class in
qemu-system-x86_64, correct?

Quoted 53 lines

This function extends the existing x86_cpu_common_class_init (target/i386/cpu.c), where some methods of the base class CPUClass are set.

Quoted 48 lines
Quoted 16 lines

+ * @instance_init: cpu instance initialization
+ * @realizefn: realize function, called first in x86 cpu realize
+ *
+ * X86 CPU accelerator-specific CPU initializations
+ */

target/i386/cpu-qom.h +8 -0
+
+struct X86CPUAccel {
+    const char *name;
+
+    void (*common_class_init)(X86CPUClass *xcc);
+    void (*instance_init)(X86CPU *cpu);
+    void (*realizefn)(X86CPU *cpu, Error **errp);
 };
 
+void x86_cpu_accel_init(const X86CPUAccel *accel);

[...]

target/i386/cpu.c static void x86_cpu_register_types(void) · line 7070

Quoted 13 lines
target/i386/cpu.c +13 -0
+static void x86_cpu_accel_init_aux(ObjectClass *klass, void *opaque)
+{
+    X86CPUClass *xcc = X86_CPU_CLASS(klass);
+    const X86CPUAccel **accel = opaque;
+
+    xcc->accel = *accel;
+    xcc->accel->common_class_init(xcc);
+}
+
+void x86_cpu_accel_init(const X86CPUAccel *accel)
+{
+    object_class_foreach(x86_cpu_accel_init_aux, TYPE_X86_CPU, false, &accel);
+}

This matches the documented behavior.

[...]

Quoted 8 lines
target/i386/host-cpu.c new file +8 -0
+void host_cpu_class_init(X86CPUClass *xcc)
+{
+    xcc->host_cpuid_required = true;
+    xcc->ordering = 8;
+    xcc->model_description =
+        g_strdup_printf("%s processor with all supported host features ",
+                        xcc->accel->name);
+}

[...]

target/i386/hvf/cpu.c line 20

Quoted 3 lines
target/i386/hvf/cpu.c new file +3 -0
+static void hvf_cpu_common_class_init(X86CPUClass *xcc)
+{
+    host_cpu_class_init(xcc);

Why are you calling host_cpu_class_init() for all CPU types?

Quoted 6 lines
Quoted 1 line

I am not..

I don't get it. You are calling host_cpu_class_init() for every single non-abstract TYPE_X86_CPU subclass (which includes all CPU models in qemu-system-x86_64), and I don't understand why, or if this is really intentional.

It is really intentional what is done here,

when HVF accelerator is enabled, and only when the HVF accelerator is enabled,

all X86 CPU classes and subclasses (cpu models, which have been implemented as subclasses of TYPE_X86_CPU), are updated with a link to the accelerator-specific HVF interface.

Quoted 18 lines
Quoted 17 lines
Quoted 12 lines
Quoted 4 lines

+}

[...]

target/i386/kvm/cpu.c line 48

Quoted 4 lines
target/i386/kvm/cpu.c new file +4 -0
+static void kvm_cpu_common_class_init(X86CPUClass *xcc)
+{
+    host_cpu_class_init(xcc);
+}

Same question as above.

Ciao,

Claudio

#38 lore raw

Re: [RFC v3 9/9] i386: split cpu accelerators from cpu.c

Quoted 93 lines · Claudio Fontana
Quoted 85 lines · Eduardo Habkost
Quoted 24 lines · Claudio Fontana

Hi,

Quoted 21 lines · Eduardo Habkost
Quoted 13 lines · Claudio Fontana

split cpu.c into:

cpu.c            cpuid and common x86 cpu functionality
host-cpu.c       host x86 cpu functions and "host" cpu type
kvm/cpu.c        KVM x86 cpu type
hvf/cpu.c        HVF x86 cpu type
tcg/cpu.c        TCG x86 cpu type

The accel interface of the X86CPUClass is set at MODULE_INIT_ACCEL_CPU time, when the accelerator is known.

---

[...]

Quoted 5 lines

+/**
+ * X86CPUAccel:
+ * @name: string name of the X86 CPU Accelerator
+ *
+ * @common_class_init: initializer for the common cpu

Quoted 3 lines
Quoted 1 line

So this will be called for every single CPU class.

Not really, it's called for every TYPE_X86_CPU cpu class (if an accel interface is registered).

This means every single non-abstract CPU class in
qemu-system-x86_64, correct?

Quoted 53 lines

This function extends the existing x86_cpu_common_class_init (target/i386/cpu.c), where some methods of the base class CPUClass are set.

Quoted 48 lines
Quoted 16 lines

+ * @instance_init: cpu instance initialization
+ * @realizefn: realize function, called first in x86 cpu realize
+ *
+ * X86 CPU accelerator-specific CPU initializations
+ */

target/i386/cpu-qom.h +8 -0
+
+struct X86CPUAccel {
+    const char *name;
+
+    void (*common_class_init)(X86CPUClass *xcc);
+    void (*instance_init)(X86CPU *cpu);
+    void (*realizefn)(X86CPU *cpu, Error **errp);
 };
 
+void x86_cpu_accel_init(const X86CPUAccel *accel);

[...]

target/i386/cpu.c static void x86_cpu_register_types(void) · line 7070

Quoted 13 lines
target/i386/cpu.c +13 -0
+static void x86_cpu_accel_init_aux(ObjectClass *klass, void *opaque)
+{
+    X86CPUClass *xcc = X86_CPU_CLASS(klass);
+    const X86CPUAccel **accel = opaque;
+
+    xcc->accel = *accel;
+    xcc->accel->common_class_init(xcc);
+}
+
+void x86_cpu_accel_init(const X86CPUAccel *accel)
+{
+    object_class_foreach(x86_cpu_accel_init_aux, TYPE_X86_CPU, false, &accel);
+}

This matches the documented behavior.

[...]

Quoted 8 lines
target/i386/host-cpu.c new file +8 -0
+void host_cpu_class_init(X86CPUClass *xcc)
+{
+    xcc->host_cpuid_required = true;
+    xcc->ordering = 8;
+    xcc->model_description =
+        g_strdup_printf("%s processor with all supported host features ",
+                        xcc->accel->name);
+}

[...]

target/i386/hvf/cpu.c line 20

Quoted 3 lines
target/i386/hvf/cpu.c new file +3 -0
+static void hvf_cpu_common_class_init(X86CPUClass *xcc)
+{
+    host_cpu_class_init(xcc);

Why are you calling host_cpu_class_init() for all CPU types?

Quoted 6 lines
Quoted 1 line

I am not..

I don't get it. You are calling host_cpu_class_init() for every single non-abstract TYPE_X86_CPU subclass (which includes all CPU models in qemu-system-x86_64), and I don't understand why, or if this is really intentional.

Quoted 7 lines

It is really intentional what is done here,

when HVF accelerator is enabled, and only when the HVF accelerator is enabled,

all X86 CPU classes and subclasses (cpu models, which have been implemented as subclasses of TYPE_X86_CPU), are updated with a link to the accelerator-specific HVF interface.

I'm confused. That (updating the class with a link) is not what host_cpu_class_init() does.

This is what host_cpu_class_init() does:

void host_cpu_class_init(X86CPUClass *xcc)

{
    xcc->host_cpuid_required = true;
    xcc->ordering = 8;
    xcc->model_description =
        g_strdup_printf("%s processor with all supported host features ",
                        xcc->accel->name);
}

Why do you want to call host_cpu_class_init() for every non-abstract TYPE_X86_CPU subclass?

Signature · 1 line
Eduardo
#39 lore raw

Re: [RFC v3 9/9] i386: split cpu accelerators from cpu.c

Quoted 116 lines · Eduardo Habkost
Quoted 93 lines · Claudio Fontana
Quoted 85 lines · Eduardo Habkost
Quoted 24 lines · Claudio Fontana

Hi,

Quoted 21 lines · Eduardo Habkost
Quoted 13 lines · Claudio Fontana

split cpu.c into:

cpu.c            cpuid and common x86 cpu functionality
host-cpu.c       host x86 cpu functions and "host" cpu type
kvm/cpu.c        KVM x86 cpu type
hvf/cpu.c        HVF x86 cpu type
tcg/cpu.c        TCG x86 cpu type

The accel interface of the X86CPUClass is set at MODULE_INIT_ACCEL_CPU time, when the accelerator is known.

---

[...]

Quoted 5 lines

+/**
+ * X86CPUAccel:
+ * @name: string name of the X86 CPU Accelerator
+ *
+ * @common_class_init: initializer for the common cpu

Quoted 3 lines
Quoted 1 line

So this will be called for every single CPU class.

Not really, it's called for every TYPE_X86_CPU cpu class (if an accel interface is registered).

This means every single non-abstract CPU class in
qemu-system-x86_64, correct?

Quoted 53 lines

This function extends the existing x86_cpu_common_class_init (target/i386/cpu.c), where some methods of the base class CPUClass are set.

Quoted 48 lines
Quoted 16 lines

+ * @instance_init: cpu instance initialization
+ * @realizefn: realize function, called first in x86 cpu realize
+ *
+ * X86 CPU accelerator-specific CPU initializations
+ */

target/i386/cpu-qom.h +8 -0
+
+struct X86CPUAccel {
+    const char *name;
+
+    void (*common_class_init)(X86CPUClass *xcc);
+    void (*instance_init)(X86CPU *cpu);
+    void (*realizefn)(X86CPU *cpu, Error **errp);
 };
 
+void x86_cpu_accel_init(const X86CPUAccel *accel);

[...]

target/i386/cpu.c static void x86_cpu_register_types(void) · line 7070

Quoted 13 lines
target/i386/cpu.c +13 -0
+static void x86_cpu_accel_init_aux(ObjectClass *klass, void *opaque)
+{
+    X86CPUClass *xcc = X86_CPU_CLASS(klass);
+    const X86CPUAccel **accel = opaque;
+
+    xcc->accel = *accel;
+    xcc->accel->common_class_init(xcc);
+}
+
+void x86_cpu_accel_init(const X86CPUAccel *accel)
+{
+    object_class_foreach(x86_cpu_accel_init_aux, TYPE_X86_CPU, false, &accel);
+}

This matches the documented behavior.

[...]

Quoted 8 lines
target/i386/host-cpu.c new file +8 -0
+void host_cpu_class_init(X86CPUClass *xcc)
+{
+    xcc->host_cpuid_required = true;
+    xcc->ordering = 8;
+    xcc->model_description =
+        g_strdup_printf("%s processor with all supported host features ",
+                        xcc->accel->name);
+}

[...]

target/i386/hvf/cpu.c line 20

Quoted 3 lines
target/i386/hvf/cpu.c new file +3 -0
+static void hvf_cpu_common_class_init(X86CPUClass *xcc)
+{
+    host_cpu_class_init(xcc);

Why are you calling host_cpu_class_init() for all CPU types?

Quoted 6 lines
Quoted 1 line

I am not..

I don't get it. You are calling host_cpu_class_init() for every single non-abstract TYPE_X86_CPU subclass (which includes all CPU models in qemu-system-x86_64), and I don't understand why, or if this is really intentional.

Quoted 7 lines

It is really intentional what is done here,

when HVF accelerator is enabled, and only when the HVF accelerator is enabled,

all X86 CPU classes and subclasses (cpu models, which have been implemented as subclasses of TYPE_X86_CPU), are updated with a link to the accelerator-specific HVF interface.

I'm confused. That (updating the class with a link) is not what host_cpu_class_init() does.

This is what host_cpu_class_init() does:

void host_cpu_class_init(X86CPUClass *xcc)

{
    xcc->host_cpuid_required = true;
    xcc->ordering = 8;
    xcc->model_description =
        g_strdup_printf("%s processor with all supported host features ",
                        xcc->accel->name);
}
Quoted 3 lines

Why do you want to call host_cpu_class_init() for every non-abstract TYPE_X86_CPU subclass?

Only now I got your point, sorry for not seeing it before. It is clearly a mistake, this should be referenced by the host cpu type!

Thanks a lot!

Claudio

#40 lore raw
In reply to Claudio Fontana
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [RFC v3 0/9] i386 cleanup

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
f6ca212 i386: split cpu accelerators from cpu.c
cc955b2 module: introduce MODULE_INIT_ACCEL_CPU
2d7e19f i386: move TCG cpu class initialization out of helper.c 147af3d i386: move cpu dump out of helper.c into cpu-dump.c 87b7d59 i386: move TCG accel files into tcg/
cc16a3c i386: hvf: remove stale MAINTAINERS entry for old hvf stubs 7ac7172 i386: move hax accel files into hax/
7bf9e93 i386: move whpx accel files into whpx/
a77578d i386: move kvm accel files into kvm/

=== OUTPUT BEGIN ===
1/9 Checking commit a77578dcc245 (i386: move kvm accel files into kvm/)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #167:
rename from target/i386/hyperv-proto.h

total: 0 errors, 1 warnings, 150 lines checked

Patch 1/9 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
2/9 Checking commit 7bf9e934972b (i386: move whpx accel files into whpx/)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #50:
new file mode 100644

total: 0 errors, 1 warnings, 32 lines checked

Patch 2/9 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
3/9 Checking commit 7ac71728106a (i386: move hax accel files into hax/)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #25:
rename from target/i386/hax-all.c

total: 0 errors, 1 warnings, 47 lines checked

Patch 3/9 has style problems, please review. If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
4/9 Checking commit cc16a3c13543 (i386: hvf: remove stale MAINTAINERS entry for old hvf stubs) 5/9 Checking commit 87b7d59c263a (i386: move TCG accel files into tcg/)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
#44:
rename from target/i386/bpt_helper.c

total: 0 errors, 1 warnings, 39 lines checked

Patch 5/9 has style problems, please review. If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
6/9 Checking commit 147af3d785ba (i386: move cpu dump out of helper.c into cpu-dump.c)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #11:
new file mode 100644

ERROR: braces {} are necessary for all arms of this statement #128: FILE: target/i386/cpu-dump.c:113:
+ if (!(env->hflags & HF_PE_MASK) || !(sc->flags & DESC_P_MASK))
[...]

WARNING: Block comments use a leading /* on a separate line #172: FILE: target/i386/cpu-dump.c:157:
+/* ARRAY_SIZE check is not required because

WARNING: Block comments use a leading /* on a separate line #211: FILE: target/i386/cpu-dump.c:196:
+/* ARRAY_SIZE check is not required because

ERROR: line over 90 characters
#373: FILE: target/i386/cpu-dump.c:358:
+ qemu_fprintf(f, "RAX=%016" PRIx64 " RBX=%016" PRIx64 " RCX=%016" PRIx64 " RDX=%016" PRIx64 "\n"

ERROR: line over 90 characters
#374: FILE: target/i386/cpu-dump.c:359:
+ "RSI=%016" PRIx64 " RDI=%016" PRIx64 " RBP=%016" PRIx64 " RSP=%016" PRIx64 "\n"

ERROR: line over 90 characters
#375: FILE: target/i386/cpu-dump.c:360:
+ "R8 =%016" PRIx64 " R9 =%016" PRIx64 " R10=%016" PRIx64 " R11=%016" PRIx64 "\n"

ERROR: line over 90 characters
#376: FILE: target/i386/cpu-dump.c:361:
+ "R12=%016" PRIx64 " R13=%016" PRIx64 " R14=%016" PRIx64 " R15=%016" PRIx64 "\n"

ERROR: line over 90 characters
#377: FILE: target/i386/cpu-dump.c:362:
+ "RIP=%016" PRIx64 " RFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",

ERROR: space required before the open parenthesis '(' #436: FILE: target/i386/cpu-dump.c:421:
+ for(i = 0; i < 6; i++) {

WARNING: line over 80 characters
#448: FILE: target/i386/cpu-dump.c:433:
+ qemu_fprintf(f, "CR0=%08x CR2=%016" PRIx64 " CR3=%016" PRIx64 " CR4=%08x\n",

ERROR: space required before the open parenthesis '(' #453: FILE: target/i386/cpu-dump.c:438:
+ for(i = 0; i < 4; i++)

ERROR: braces {} are necessary for all arms of this statement #453: FILE: target/i386/cpu-dump.c:438:
+ for(i = 0; i < 4; i++)
[...]

ERROR: space required before the open parenthesis '(' #469: FILE: target/i386/cpu-dump.c:454:
+ for(i = 0; i < 4; i++) {

ERROR: braces {} are necessary for all arms of this statement #476: FILE: target/i386/cpu-dump.c:461:
+ if ((unsigned)env->cc_op < CC_OP_NB)
[...]
+ else
[...]

WARNING: line over 80 characters
#477: FILE: target/i386/cpu-dump.c:462:
+ snprintf(cc_op_name, sizeof(cc_op_name), "%s", cc_op_str[env->cc_op]);

ERROR: space required before the open parenthesis '(' #497: FILE: target/i386/cpu-dump.c:482:
+ for(i = 0; i < 8; i++) {

ERROR: spaces required around that '=' (ctx:VxV)
#507: FILE: target/i386/cpu-dump.c:492:

+        for(i=0;i<8;i++) {
              ^

ERROR: space required after that ';' (ctx:VxV)
#507: FILE: target/i386/cpu-dump.c:492:

+        for(i=0;i<8;i++) {
                ^

ERROR: spaces required around that '<' (ctx:VxV)
#507: FILE: target/i386/cpu-dump.c:492:

+        for(i=0;i<8;i++) {
                  ^

ERROR: space required after that ';' (ctx:VxV)
#507: FILE: target/i386/cpu-dump.c:492:

+        for(i=0;i<8;i++) {
                    ^

ERROR: space required before the open parenthesis '(' #507: FILE: target/i386/cpu-dump.c:492:
+ for(i=0;i<8;i++) {

ERROR: braces {} are necessary for all arms of this statement #512: FILE: target/i386/cpu-dump.c:497:
+ if ((i & 1) == 1)
[...]
+ else
[...]

ERROR: braces {} are necessary for all arms of this statement #517: FILE: target/i386/cpu-dump.c:502:
+ if (env->hflags & HF_CS64_MASK)
[...]
+ else
[...]

ERROR: spaces required around that '=' (ctx:VxV)
#521: FILE: target/i386/cpu-dump.c:506:

+        for(i=0;i<nb;i++) {
              ^

ERROR: space required after that ';' (ctx:VxV)
#521: FILE: target/i386/cpu-dump.c:506:

+        for(i=0;i<nb;i++) {
                ^

ERROR: spaces required around that '<' (ctx:VxV)
#521: FILE: target/i386/cpu-dump.c:506:

+        for(i=0;i<nb;i++) {
                  ^

ERROR: space required after that ';' (ctx:VxV)
#521: FILE: target/i386/cpu-dump.c:506:

+        for(i=0;i<nb;i++) {
                     ^

ERROR: space required before the open parenthesis '(' #521: FILE: target/i386/cpu-dump.c:506:
+ for(i=0;i<nb;i++) {

ERROR: braces {} are necessary for all arms of this statement #528: FILE: target/i386/cpu-dump.c:513:
+ if ((i & 1) == 1)
[...]
+ else
[...]

total: 25 errors, 5 warnings, 1078 lines checked

Patch 6/9 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

7/9 Checking commit 2d7e19fb114a (i386: move TCG cpu class initialization out of helper.c)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #282:
new file mode 100644

total: 0 errors, 1 warnings, 610 lines checked

Patch 7/9 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
8/9 Checking commit cc955b2b3858 (module: introduce MODULE_INIT_ACCEL_CPU) 9/9 Checking commit f6ca212625cb (i386: split cpu accelerators from cpu.c)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #727:
new file mode 100644

WARNING: line over 80 characters
#1491: FILE: target/i386/tcg/cpu.c:102:
+ memory_region_add_subregion_overlap(cpu->cpu_as_root, 0, cpu->cpu_as_mem, 0);

total: 0 errors, 2 warnings, 1331 lines checked

Patch 9/9 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]