Skip to contentSkip to conversation

accel/hvf: Assorted collection of patches queued before v11 release

PATCH In review Philippe Mathieu-Daudé 7 participants 32 messages Apr 23 – Apr 29

lore
Share & tools
lore
#0 lore raw

cover accel/hvf: Assorted collection of patches queued before v11 release

Various patches related to HVF I'm carrying.
Some are already reviewed, some aren't :)

Except the last one (RFC) I plan to eventually send a PR with them once reviewed / tested.

Matheus Tavares Bernardino (1):
accel/hvf: fix double hv_vcpu_destroy() causing teardown error on ARM

Philippe Mathieu-Daudé (13):
target: Simplify env_archcpu(cpu_env(cpu)) expression hw/cxl: Define cxl_fmws_get_all_sorted() stub hw/arm/virt: Do not select Kconfig symbol PCI_EXPRESS tests/functional/test_aarch64_kvm: Stop dumping kernel ringbuf hw/core/cpu: Expose CPUState::start_powered_off docstring system/cpu: Defer memory layout changes until vCPUs are realized system/cpu: Reset vCPU %exception_index before resuming it accel/hvf: Enforce host alignment when calling hv_vm_protect() target/arm: Use generic hwaccel_enabled() to check 'host' cpu type target/arm: Only set CPU_INTERRUPT_EXITTB for TCG target/arm: Remove vcpu_dirty=true assigments in
hvf_handle_exception()
target/i386: Report TPR accesses to HVF
Reapply "target/arm/hvf: Sync CNTV_CTL_EL0 & CNTV_CVAL_EL0"

Scott J. Goldman (2):
accel/hvf: avoid executable mappings for RAM-device memory target/arm/hvf: Fix WFI halting to stop idle vCPU spinning

 include/hw/core/cpu.h                |   2 +-
 include/system/hvf_int.h             |   1 +
 accel/hvf/hvf-all.c                  |  12 ++-
 hw/cxl/cxl-host-stubs.c              |   5 ++
 system/cpus.c                        |   1 +
 system/physmem.c                     |   8 ++
 target/arm/arm-qmp-cmds.c            |   5 +-
 target/arm/cpu.c                     |   5 +-
 target/arm/helper.c                  |   2 +-
 target/arm/hvf/hvf.c                 | 118 +++++++++++++++++++++++++--
 target/hexagon/translate.c           |   2 +-
 target/i386/helper.c                 |   2 +-
 hw/arm/Kconfig                       |   1 -
 tests/functional/aarch64/test_kvm.py |   1 +
 14 files changed, 147 insertions(+), 18 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/16 target: Simplify env_archcpu(cpu_env(cpu)) expression

env_archcpu(cpu_env(CPUState)) expands to ArchCPU, available via the ARCH_CPU() macro. Use the latter to avoid using &env.

---
 target/arm/hvf/hvf.c       | 2 +-
 target/hexagon/translate.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
target/arm/hvf/hvf.c +1 -1
index 5fc8f6bbbd9..2fb768048b8 100644
@@ -2303,7 +2303,7 @@ static int hvf_handle_exception(CPUState *cpu, hv_vcpu_exit_exception_t *excp)
 
 static int hvf_handle_vmexit(CPUState *cpu, hv_vcpu_exit_t *exit)
 {
-    ARMCPU *arm_cpu = env_archcpu(cpu_env(cpu));
+    ARMCPU *arm_cpu = ARM_CPU(cpu);
     int ret = 0;
 
     switch (exit->reason) {
target/hexagon/translate.c +1 -1
index 633401451d8..825b236dd24 100644
@@ -978,7 +978,7 @@ static void hexagon_tr_init_disas_context(DisasContextBase *dcbase,
                                           CPUState *cs)
 {
     DisasContext *ctx = container_of(dcbase, DisasContext, base);
-    HexagonCPU *hex_cpu = env_archcpu(cpu_env(cs));
+    HexagonCPU *hex_cpu = HEXAGON_CPU(cs);
     uint32_t hex_flags = dcbase->tb->flags;
 
     ctx->mem_idx = MMU_USER_IDX;
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 01/16] target: Simplify env_archcpu(cpu_env(cpu)) expression

Quoted 15 lines · Philippe Mathieu-Daudé

env_archcpu(cpu_env(CPUState)) expands to ArchCPU, available via the ARCH_CPU() macro. Use the latter to avoid using &env.

---
  target/arm/hvf/hvf.c       | 2 +-
  target/hexagon/translate.c | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
target/arm/hvf/hvf.c +0 -0
index 5fc8f6bbbd9..2fb768048b8 100644

target/arm/hvf/hvf.c static int hvf_handle_exception(CPUState *cpu, hv_vcpu_exit_exception_t *excp) · line 2307

Quoted 6 lines
target/arm/hvf/hvf.c +1 -1
@@ -2303,7 +2303,7 @@ static int hvf_handle_exception(CPUState *cpu, hv_vcpu_exit_exception_t *excp)
 
  static int hvf_handle_vmexit(CPUState *cpu, hv_vcpu_exit_t *exit)
  {
-    ARMCPU *arm_cpu = env_archcpu(cpu_env(cpu));
+    ARMCPU *arm_cpu = ARM_CPU(cpu);
      int ret = 0;

ARM_CPU has an object_dynamic_cast_assert() inside; the env* converters do not. You should be making a considered choice about this change, not just "simpler".

r~

Quoted 15 lines
target/arm/hvf/hvf.c +0 -0
      switch (exit->reason) {
target/hexagon/translate.c +1 -1
index 633401451d8..825b236dd24 100644
@@ -978,7 +978,7 @@ static void hexagon_tr_init_disas_context(DisasContextBase *dcbase,
                                            CPUState *cs)
  {
      DisasContext *ctx = container_of(dcbase, DisasContext, base);
-    HexagonCPU *hex_cpu = env_archcpu(cpu_env(cs));
+    HexagonCPU *hex_cpu = HEXAGON_CPU(cs);
      uint32_t hex_flags = dcbase->tb->flags;
 
      ctx->mem_idx = MMU_USER_IDX;
#3 lore raw

Re: [PATCH 01/16] target: Simplify env_archcpu(cpu_env(cpu)) expression

Quoted 11 lines · Richard Henderson
Quoted 10 lines · Philippe Mathieu-Daudé

env_archcpu(cpu_env(CPUState)) expands to ArchCPU, available via the ARCH_CPU() macro. Use the latter to avoid using &env.

---
  target/arm/hvf/hvf.c       | 2 +-
  target/hexagon/translate.c | 2 +-

  2 files changed, 2 insertions(+), 2 deletions(-)
Quoted 14 lines

target/arm/hvf/hvf.c static int hvf_handle_exception(CPUState *cpu, · line 2307

Quoted 11 lines
target/arm/hvf/hvf.c +1 -1
index 5fc8f6bbbd9..2fb768048b8 100644
@@ -2303,7 +2303,7 @@ static int hvf_handle_exception(CPUState *cpu, 
 hv_vcpu_exit_exception_t *excp)
   static int hvf_handle_vmexit(CPUState *cpu, hv_vcpu_exit_t *exit)
   {
-    ARMCPU *arm_cpu = env_archcpu(cpu_env(cpu));
+    ARMCPU *arm_cpu = ARM_CPU(cpu);
       int ret = 0;

ARM_CPU has an object_dynamic_cast_assert() inside; the env* converters do not. You should be making a considered choice about this change, not just "simpler".

If not calling object_dynamic_cast_assert() is what we want here, then maybe we could add yet another QOM cast macro with explicit name, otherwise at least add a comment why we don't use the same pattern.

Having to think about hidden knowledge of such performance penalty is not great from an API point of view. I'd expect most new developers to copy/paste working patterns without looking at macro expansion involvments, WDYT?

#4 lore raw

PATCH 2/16 hw/cxl: Define cxl_fmws_get_all_sorted() stub

In reply to Philippe Mathieu-Daudé

Add a stub for cxl_fmws_get_all_sorted() to fix building a QEMU configured with the '--without-default-devices' option:

Undefined symbols for architecture arm64:
"_cxl_fmws_get_all_sorted", referenced from:
_cxl_build_cedt in hw_acpi_cxl.c.o

Note, the CXL <-> ACPI Kconfig relationship is a bit convoluted.

---
 hw/cxl/cxl-host-stubs.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/cxl/cxl-host-stubs.c +5 -0
index c015baac813..9b515913ea4 100644
@@ -18,3 +18,8 @@ hwaddr cxl_fmws_set_memmap(hwaddr base, hwaddr max_addr)
 void cxl_fmws_update_mmio(void) {};
 
 const MemoryRegionOps cfmws_ops;
+
+GSList *cxl_fmws_get_all_sorted(void)
+{
+    g_assert_not_reached();
+}
Signature · 1 line
2.53.0
Reviewed-by on v1 2/16
#5 lore raw

Re: [PATCH 02/16] hw/cxl: Define cxl_fmws_get_all_sorted() stub

Quoted 19 lines · Philippe Mathieu-Daudé

Add a stub for cxl_fmws_get_all_sorted() to fix building a QEMU configured with the '--without-default-devices' option:

Undefined symbols for architecture arm64:
"_cxl_fmws_get_all_sorted", referenced from:
_cxl_build_cedt in hw_acpi_cxl.c.o

Note, the CXL <-> ACPI Kconfig relationship is a bit convoluted.

---
  hw/cxl/cxl-host-stubs.c | 5 +++++
  1 file changed, 5 insertions(+)
hw/cxl/cxl-host-stubs.c +0 -0
index c015baac813..9b515913ea4 100644

hw/cxl/cxl-host-stubs.c hwaddr cxl_fmws_set_memmap(hwaddr base, hwaddr max_addr) · line 25

Quoted 8 lines
hw/cxl/cxl-host-stubs.c +5 -0
@@ -18,3 +18,8 @@ hwaddr cxl_fmws_set_memmap(hwaddr base, hwaddr max_addr)
  void cxl_fmws_update_mmio(void) {};
 
  const MemoryRegionOps cfmws_ops;
+
+GSList *cxl_fmws_get_all_sorted(void)
+{
+    g_assert_not_reached();
+}

r~

Acked-by on v1 2/16
#6 lore raw

Re: [PATCH 02/16] hw/cxl: Define cxl_fmws_get_all_sorted() stub

In reply to Philippe Mathieu-Daudé
Quoted 11 lines · On Thu, 23 Apr 2026 19:02:14 +0200 Philippe Mathieu-Daudé <[email protected]>…

Add a stub for cxl_fmws_get_all_sorted() to fix building a QEMU configured with the '--without-default-devices' option:

Undefined symbols for architecture arm64:
"_cxl_fmws_get_all_sorted", referenced from:
_cxl_build_cedt in hw_acpi_cxl.c.o

Note, the CXL <-> ACPI Kconfig relationship is a bit convoluted.

---

Quoted 16 lines
 hw/cxl/cxl-host-stubs.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/cxl/cxl-host-stubs.c +5 -0
index c015baac813..9b515913ea4 100644
@@ -18,3 +18,8 @@ hwaddr cxl_fmws_set_memmap(hwaddr base, hwaddr max_addr)
 void cxl_fmws_update_mmio(void) {};
 
 const MemoryRegionOps cfmws_ops;
+
+GSList *cxl_fmws_get_all_sorted(void)
+{
+    g_assert_not_reached();
+}
#7 lore raw

PATCH 3/16 hw/arm/virt: Do not select Kconfig symbol PCI_EXPRESS

In reply to Philippe Mathieu-Daudé

The PCIe bus is not provided by the Virt machine but by its GPEX host bridge (selected via PCI_EXPRESS_GENERIC_BRIDGE).

---
 hw/arm/Kconfig | 1 -
 1 file changed, 1 deletion(-)
hw/arm/Kconfig +0 -1
index 4e50fb1111f..41d5e968c86 100644
@@ -15,7 +15,6 @@ config ARM_VIRT
     select GPIO_KEY
     select DEVICE_TREE
     select FW_CFG_DMA
-    select PCI_EXPRESS
     select PCI_EXPRESS_GENERIC_BRIDGE
     select PFLASH_CFI01
     select PL011 # UART
Signature · 1 line
2.53.0
Reviewed-by on v1 3/16
#8 lore raw

Re: [PATCH 03/16] hw/arm/virt: Do not select Kconfig symbol PCI_EXPRESS

Quoted 13 lines · Philippe Mathieu-Daudé

The PCIe bus is not provided by the Virt machine but by its GPEX host bridge (selected via PCI_EXPRESS_GENERIC_BRIDGE).

---
  hw/arm/Kconfig | 1 -
  1 file changed, 1 deletion(-)
hw/arm/Kconfig +0 -0
index 4e50fb1111f..41d5e968c86 100644

hw/arm/Kconfig config ARM_VIRT · line 20

Quoted 7 lines
hw/arm/Kconfig +0 -1
@@ -15,7 +15,6 @@ config ARM_VIRT
      select GPIO_KEY
      select DEVICE_TREE
      select FW_CFG_DMA
-    select PCI_EXPRESS
      select PCI_EXPRESS_GENERIC_BRIDGE
      select PFLASH_CFI01
      select PL011 # UART

r~

#9 lore raw

PATCH 4/16 tests/functional/test_aarch64_kvm: Stop dumping kernel ringbuf

In reply to Philippe Mathieu-Daudé

Avoid the following kernel message to interfere our stdout parsing:

platform gpio-keys: deferred probe pending: gpio-keys: failed to get gpio

---
 tests/functional/aarch64/test_kvm.py | 1 +
 1 file changed, 1 insertion(+)
tests/functional/aarch64/test_kvm.py +1 -0
index 7545f5ed554..a03a6134626 100755
@@ -48,6 +48,7 @@ def _launch_guest(self, kvm_mode="nvhe"):
 
         self.wait_for_console_pattern('buildroot login:')
         ec_and_wait(self, 'root', '#')
+        ec_and_wait(self, 'dmesg -n 1', '#')
         ec_and_wait(self, f"export PS1='{self.PS1}'", self.OK_CMD)
 
     # this is just a smoketest, we don't run all the tests in the image
Signature · 1 line
2.53.0
#10 lore raw

Re: [PATCH 04/16] tests/functional/test_aarch64_kvm: Stop dumping kernel ringbuf

Quoted 14 lines · Philippe Mathieu-Daudé

Avoid the following kernel message to interfere our stdout parsing:

platform gpio-keys: deferred probe pending: gpio-keys: failed to get gpio

---
  tests/functional/aarch64/test_kvm.py | 1 +
  1 file changed, 1 insertion(+)
tests/functional/aarch64/test_kvm.py +0 -0
index 7545f5ed554..a03a6134626 100755

tests/functional/aarch64/test_kvm.py def _launch_guest(self, kvm_mode="nvhe"): · line 52

Quoted 5 lines
tests/functional/aarch64/test_kvm.py +1 -0
@@ -48,6 +48,7 @@ def _launch_guest(self, kvm_mode="nvhe"):
 
          self.wait_for_console_pattern('buildroot login:')
          ec_and_wait(self, 'root', '#')
+        ec_and_wait(self, 'dmesg -n 1', '#')
          ec_and_wait(self, f"export PS1='{self.PS1}'", self.OK_CMD)

In other tests, we are adding "quiet" to the kernel_command_line to avoid this problem - that way the kernel messages also cannot interfere with the login prompt. Could you check whether that works here, too? If so, I think I'd rather prefer that solution instead.

Thanks,
Thomas

#11 lore raw

Re: [PATCH 04/16] tests/functional/test_aarch64_kvm: Stop dumping kernel ringbuf

Quoted 10 lines · Thomas Huth
Quoted 9 lines · Philippe Mathieu-Daudé

Avoid the following kernel message to interfere our stdout parsing:

   platform gpio-keys: deferred probe pending: gpio-keys: failed to get gpio

---
  tests/functional/aarch64/test_kvm.py | 1 +

  1 file changed, 1 insertion(+)
Quoted 11 lines

tests/functional/aarch64/test_kvm.py def _launch_guest(self, kvm_mode="nvhe"): · line 51

Quoted 9 lines
tests/functional/aarch64/test_kvm.py +1 -0
index 7545f5ed554..a03a6134626 100755
@@ -48,6 +48,7 @@ def _launch_guest(self, kvm_mode="nvhe"):
           self.wait_for_console_pattern('buildroot login:')
           ec_and_wait(self, 'root', '#')
+        ec_and_wait(self, 'dmesg -n 1', '#')
           ec_and_wait(self, f"export PS1='{self.PS1}'", self.OK_CMD)

In other tests, we are adding "quiet" to the kernel_command_line to avoid this problem - that way the kernel messages also cannot interfere with the login prompt. Could you check whether that works here, too? If so, I think I'd rather prefer that solution instead.

I tried:

-- >8 --
kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
- f"console=ttyAMA0 kvm-arm.mode={kvm_mode}")
+ f" quiet console=ttyAMA0 kvm-arm.mode={kvm_mode}")

---

but it didn't help. Anyway no worries, I'll figure that later. Dropping this patch for now.

#12 lore raw

PATCH 5/16 hw/core/cpu: Expose CPUState::start_powered_off docstring

In reply to Philippe Mathieu-Daudé

The comment about @start_powered_off is buried within the CPUState structure. Hoist it to the structure docstring comment.

---
 include/hw/core/cpu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/core/cpu.h +1 -1
index 04e1f970caf..bc719d51b3a 100644
@@ -419,6 +419,7 @@ struct qemu_work_item;
  *   QOM parent.
  *   Under TCG this value is propagated to @tcg_cflags.
  *   See TranslationBlock::TCG CF_CLUSTER_MASK.
+ * @start_powered_off: Indicates whether the CPU starts in powered-off state.
  * @tcg_cflags: Pre-computed cflags for this cpu.
  * @nr_threads: Number of threads within this CPU core.
  * @thread: Host thread details, only live once @created is #true
@@ -502,7 +503,6 @@ struct CPUState {
     bool stop;
     bool stopped;
 
-    /* Should CPU start in powered-off state? */
     bool start_powered_off;
 
     bool unplug;
Signature · 1 line
2.53.0
Reviewed-by on v1 5/16
#13 lore raw

Re: [PATCH 05/16] hw/core/cpu: Expose CPUState::start_powered_off docstring

Quoted 22 lines · Philippe Mathieu-Daudé

The comment about @start_powered_off is buried within the CPUState structure. Hoist it to the structure docstring comment.

---
  include/hw/core/cpu.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/core/cpu.h +1 -0
index 04e1f970caf..bc719d51b3a 100644
@@ -419,6 +419,7 @@ struct qemu_work_item;
   *   QOM parent.
   *   Under TCG this value is propagated to @tcg_cflags.
   *   See TranslationBlock::TCG CF_CLUSTER_MASK.
+ * @start_powered_off: Indicates whether the CPU starts in powered-off state.
   * @tcg_cflags: Pre-computed cflags for this cpu.
   * @nr_threads: Number of threads within this CPU core.
   * @thread: Host thread details, only live once @created is #true

include/hw/core/cpu.h struct CPUState { · line 508

Quoted 7 lines
include/hw/core/cpu.h +0 -1
@@ -502,7 +503,6 @@ struct CPUState {
      bool stop;
      bool stopped;
 
-    /* Should CPU start in powered-off state? */
      bool start_powered_off;
 
      bool unplug;

r~

#14 lore raw

PATCH 6/16 system/cpu: Defer memory layout changes until vCPUs are realized

In reply to Philippe Mathieu-Daudé

vCPUs are not really usable until fully realized. Do not attempt to commit memory changes in the middle of vCPU realization. Defer until realization is completed and vCPU fully operational.

---
 system/physmem.c | 8 ++++++++
 1 file changed, 8 insertions(+)
system/physmem.c +8 -0
index 4e26f1a1d42..091a1da3458 100644
@@ -3089,6 +3089,14 @@ static void tcg_commit(MemoryListener *listener)
     cpuas = container_of(listener, CPUAddressSpace, tcg_as_listener);
     cpu = cpuas->cpu;
 
+    if (!qdev_is_realized(DEVICE(cpu))) {
+        /*
+         * The listener is also called during realize, before
+         * all of the tcg machinery for run-on is initialized.
+         */
+        return;
+    }
+
     /*
      * Queueing the work function will kick the cpu back to
      * the main loop, which will end the RCU critical section and reclaim
Signature · 1 line
2.53.0
#15 lore raw

PATCH 7/16 system/cpu: Reset vCPU %exception_index before resuming it

In reply to Philippe Mathieu-Daudé
---
 system/cpus.c | 1 +
 1 file changed, 1 insertion(+)
system/cpus.c +1 -0
index bded87feb1c..b4c7f94332d 100644
@@ -621,6 +621,7 @@ void cpu_pause(CPUState *cpu)
 
 void cpu_resume(CPUState *cpu)
 {
+    cpu->exception_index = -1;
     cpu->stop = false;
     cpu->stopped = false;
     qemu_cpu_kick(cpu);
Signature · 1 line
2.53.0
Reviewed-by on v1 7/16
#16 lore raw

Re: [PATCH 07/16] system/cpu: Reset vCPU %exception_index before resuming it

Quoted 10 lines · Philippe Mathieu-Daudé
---
  system/cpus.c | 1 +
  1 file changed, 1 insertion(+)
system/cpus.c +0 -0
index bded87feb1c..b4c7f94332d 100644

system/cpus.c void cpu_pause(CPUState *cpu) · line 627

Quoted 7 lines
system/cpus.c +1 -0
@@ -621,6 +621,7 @@ void cpu_pause(CPUState *cpu)
 
  void cpu_resume(CPUState *cpu)
  {
+    cpu->exception_index = -1;
      cpu->stop = false;
      cpu->stopped = false;
      qemu_cpu_kick(cpu);

r~

#17 lore raw

PATCH 8/16 accel/hvf: Enforce host alignment when calling hv_vm_protect()

In reply to Philippe Mathieu-Daudé
---
 accel/hvf/hvf-all.c | 2 ++
 1 file changed, 2 insertions(+)
accel/hvf/hvf-all.c +2 -0
index 5f357c6d198..82cb3717750 100644
@@ -56,6 +56,8 @@ static void do_hv_vm_protect(hwaddr start, size_t size,
     intptr_t page_mask = qemu_real_host_page_mask();
     hv_return_t ret;
 
+    start &= qemu_real_host_page_mask();
+    size = REAL_HOST_PAGE_ALIGN(size);
     trace_hvf_vm_protect(start, size, flags,
                          flags & HV_MEMORY_READ  ? 'R' : '-',
                          flags & HV_MEMORY_WRITE ? 'W' : '-',
Signature · 1 line
2.53.0
#18 lore raw

Re: [PATCH 08/16] accel/hvf: Enforce host alignment when calling hv_vm_protect()

Quoted 15 lines · Philippe Mathieu-Daudé

---
accel/hvf/hvf-all.c | 2 ++

1 file changed, 2 insertions(+)
accel/hvf/hvf-all.c +0 -0
index 5f357c6d198..82cb3717750 100644

accel/hvf/hvf-all.c static void do_hv_vm_protect(hwaddr start, size_t size, · line 60

Quoted 5 lines
accel/hvf/hvf-all.c +2 -0
@@ -56,6 +56,8 @@ static void do_hv_vm_protect(hwaddr start, size_t size,
    intptr_t page_mask = qemu_real_host_page_mask();
    hv_return_t ret;
 
+    start &= qemu_real_host_page_mask();
+    size = REAL_HOST_PAGE_ALIGN(size);

Hi,

I think the patches to support 4K IPAs (which are already out there) should go first and then an adaptation of this with that in mind afterwards.

With HVF 4KB page size IPAs are supported so the check in this form would have to be altered anyways when that gets merged so...

#19 lore raw

Re: [PATCH 08/16] accel/hvf: Enforce host alignment when calling hv_vm_protect()

In reply to Philippe Mathieu-Daudé
Quoted 11 lines · Philippe Mathieu-Daudé
---
  accel/hvf/hvf-all.c | 2 ++
  1 file changed, 2 insertions(+)
accel/hvf/hvf-all.c +0 -0
index 5f357c6d198..82cb3717750 100644

accel/hvf/hvf-all.c static void do_hv_vm_protect(hwaddr start, size_t size, · line 60

Quoted 5 lines
accel/hvf/hvf-all.c +2 -0
@@ -56,6 +56,8 @@ static void do_hv_vm_protect(hwaddr start, size_t size,
      intptr_t page_mask = qemu_real_host_page_mask();
      hv_return_t ret;
 
+    start &= qemu_real_host_page_mask();
+    size = REAL_HOST_PAGE_ALIGN(size);

We have read the page mask into a local variable.
Use that instead of reading it twice more.

Also, the existing uses of page_mask are to assert what you're now enforcing. So, you can remove those asserts.

r~

Quoted 3 lines
accel/hvf/hvf-all.c +0 -0
      trace_hvf_vm_protect(start, size, flags,
                           flags & HV_MEMORY_READ  ? 'R' : '-',
                           flags & HV_MEMORY_WRITE ? 'W' : '-',
#20 lore raw

PATCH 9/16 accel/hvf: avoid executable mappings for RAM-device memory

In reply to Philippe Mathieu-Daudé

From: "Scott J. Goldman" <[email protected]>

On macOS, HVF can panic the host kernel if a guest accesses device-backed memory through an executable mapping. Leave RAM-device/MMIO regions mapped read/write only and keep EXEC for ordinary guest RAM.

This works around the immediate crash seen with passthrough BAR mappings. There are still platform-specific performance issues with guest write-combining mappings, but uncached mappings behave much more like the host-side mapping and this at least avoids the panic.

Message-ID: <[email protected]>

---
 accel/hvf/hvf-all.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
accel/hvf/hvf-all.c +9 -1
index 82cb3717750..0b431fab72a 100644
@@ -116,7 +116,15 @@ static void hvf_set_phys_mem(MemoryRegionSection *section, bool add)
         return;
     }
 
-    flags = HV_MEMORY_READ | HV_MEMORY_EXEC | (writable ? HV_MEMORY_WRITE : 0);
+    flags = HV_MEMORY_READ | (writable ? HV_MEMORY_WRITE : 0);
+    /*
+     * Leave RAM-device/MMIO mappings RW-only: on macOS, accessing them through
+     * executable HVF mappings can panic the host kernel. Ordinary guest RAM
+     * still needs EXEC.
+     */
+    if (!memory_region_is_ram_device(area)) {
+        flags |= HV_MEMORY_EXEC;
+    }
     mem = memory_region_get_ram_ptr(area) + section->offset_within_region;
 
     trace_hvf_vm_map(gpa, size, mem, flags,
Signature · 1 line
2.53.0
#21 lore raw

PATCH 10/16 target/arm: Use generic hwaccel_enabled() to check 'host' cpu type

In reply to Philippe Mathieu-Daudé

We should be able to use the 'host' CPU with any hardware accelerator.

---
 target/arm/arm-qmp-cmds.c | 5 +++--
 target/arm/cpu.c          | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)
target/arm/arm-qmp-cmds.c +3 -2
index 83ec95c290f..ee17261c9fe 100644
@@ -31,6 +31,7 @@
 #include "qapi/qapi-commands-misc-arm.h"
 #include "qobject/qdict.h"
 #include "qom/qom-qobject.h"
+#include "system/hw_accel.h"
 #include "cpu.h"
 
 static GICCapability *gic_cap_new(int version)
@@ -96,8 +97,8 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
         return NULL;
     }
 
-    if (!kvm_enabled() && !strcmp(model->name, "host")) {
-        error_setg(errp, "The CPU type '%s' requires KVM", model->name);
+    if (!hwaccel_enabled() && !strcmp(model->name, "host")) {
+        error_setg(errp, "The CPU type 'host' requires hardware accelerator");
         return NULL;
     }
 
target/arm/cpu.c +3 -2
index ccc47c8a9ad..4f98121be0b 100644
@@ -1629,8 +1629,9 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
      * this is the first point where we can report it.
      */
     if (cpu->host_cpu_probe_failed) {
-        if (!kvm_enabled() && !hvf_enabled() && !whpx_enabled()) {
-            error_setg(errp, "The 'host' CPU type can only be used with KVM, HVF or WHPX");
+        if (!hwaccel_enabled()) {
+            error_setg(errp, "The 'host' CPU type can only be used with "
+                             "hardware accelator such KVM/HVF/WHPX");
         } else {
             error_setg(errp, "Failed to retrieve host CPU features");
         }
Signature · 1 line
2.53.0
#22 lore raw

Re: [PATCH 10/16] target/arm: Use generic hwaccel_enabled() to check 'host' cpu type

Quoted 13 lines · Philippe Mathieu-Daudé

We should be able to use the 'host' CPU with any hardware accelerator.

---
target/arm/arm-qmp-cmds.c | 5 +++--
target/arm/cpu.c | 5 +++--

2 files changed, 6 insertions(+), 4 deletions(-)

[…]

target/arm/cpu.c static void arm_cpu_realizefn(DeviceState *dev, Error **errp) · line 1634

Quoted 13 lines
target/arm/cpu.c +3 -2
index ccc47c8a9ad..4f98121be0b 100644
@@ -1629,8 +1629,9 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
     * this is the first point where we can report it.
     */
    if (cpu->host_cpu_probe_failed) {
-        if (!kvm_enabled() && !hvf_enabled() && !whpx_enabled()) {
-            error_setg(errp, "The 'host' CPU type can only be used with KVM, HVF or WHPX");
+        if (!hwaccel_enabled()) {
+            error_setg(errp, "The 'host' CPU type can only be used with "
+                             "hardware accelator such KVM/HVF/WHPX”);

accelerator such as

Quoted 7 lines
target/arm/cpu.c +0 -0
        } else {
            error_setg(errp, "Failed to retrieve host CPU features");
        }
Signature · 1 line
2.53.0
#23 lore raw

PATCH 11/16 target/arm: Only set CPU_INTERRUPT_EXITTB for TCG

In reply to Philippe Mathieu-Daudé

Commit 34c45d53026 ("target-arm: kvm - re-inject guest debug exceptions") removed CPU_INTERRUPT_EXITTB from KVM, but it also appears on HVF. Better to restrict it to TCG.

Message-Id: <[email protected]>

---
 target/arm/helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/helper.c +1 -1
index 7389f2988c4..45183df4449 100644
@@ -9577,7 +9577,7 @@ void arm_cpu_do_interrupt(CPUState *cs)
 
     arm_call_el_change_hook(cpu);
 
-    if (!kvm_enabled()) {
+    if (tcg_enabled()) {
         cpu_set_interrupt(cs, CPU_INTERRUPT_EXITTB);
     }
 
Signature · 1 line
2.53.0
Reviewed-by on v1 11/16
#24 lore raw

Re: [PATCH 11/16] target/arm: Only set CPU_INTERRUPT_EXITTB for TCG

Quoted 4 lines · Philippe Mathieu-Daudé

Commit 34c45d53026 ("target-arm: kvm - re-inject guest debug exceptions") removed CPU_INTERRUPT_EXITTB from KVM, but it also appears on HVF. Better to restrict it to TCG.

Quoted 9 lines

Message-Id: <[email protected]>

---
  target/arm/helper.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
#25 lore raw

PATCH 12/16 target/arm: Remove vcpu_dirty=true assigments in hvf_handle_exception()

In reply to Philippe Mathieu-Daudé

Commit 2a425aae0b5 ("target/arm: ensure PSCI register updates are flushed") manually sets %vcpu_dirty in hvf_handle_exception(), but these calls follow calls to cpu_synchronize_state() which itself sets %vcpu_dirty. Better have the generic CPU API handle this, but add a pair of assertions when serializing the accelerator state to be safe.

---
 target/arm/hvf/hvf.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
target/arm/hvf/hvf.c +4 -2
index 2fb768048b8..f2c5bde5983 100644
@@ -737,6 +737,8 @@ int hvf_arch_get_registers(CPUState *cpu)
     hv_simd_fp_uchar16_t fpval;
     int i, n;
 
+    assert(!cpu->vcpu_dirty);
+
     for (i = 0; i < ARRAY_SIZE(hvf_reg_match); i++) {
         ret = hv_vcpu_get_reg(cpu->accel->fd, hvf_reg_match[i].reg, &val);
         *(uint64_t *)((void *)env + hvf_reg_match[i].offset) = val;
@@ -888,6 +890,8 @@ int hvf_arch_put_registers(CPUState *cpu)
     hv_simd_fp_uchar16_t fpval;
     int i, n;
 
+    assert(cpu->vcpu_dirty);
+
     /*
      * Set SVCR first because changing it will zero out Z/P (including NEON)
      * regs
@@ -2239,7 +2243,6 @@ static int hvf_handle_exception(CPUState *cpu, hv_vcpu_exit_exception_t *excp)
                 /* SMCCC 1.3 section 5.2 says every unknown SMCCC call returns -1 */
                 env->xregs[0] = -1;
             }
-            cpu->vcpu_dirty = true;
         } else {
             trace_hvf_unknown_hvc(env->pc, env->xregs[0]);
             hvf_raise_exception(cpu, EXCP_UDEF, syn_uncategorized(), 1);
@@ -2256,7 +2259,6 @@ static int hvf_handle_exception(CPUState *cpu, hv_vcpu_exit_exception_t *excp)
                 /* SMCCC 1.3 section 5.2 says every unknown SMCCC call returns -1 */
                 env->xregs[0] = -1;
             }
-            cpu->vcpu_dirty = true;
         } else {
             trace_hvf_unknown_smc(env->xregs[0]);
             hvf_raise_exception(cpu, EXCP_UDEF, syn_uncategorized(), 1);
Signature · 1 line
2.53.0
Reviewed-by on v1 12/16
#26 lore raw

Re: [PATCH 12/16] target/arm: Remove vcpu_dirty=true assigments in hvf_handle_exception()

Quoted 7 lines · Philippe Mathieu-Daudé

Commit 2a425aae0b5 ("target/arm: ensure PSCI register updates are flushed") manually sets %vcpu_dirty in hvf_handle_exception(), but these calls follow calls to cpu_synchronize_state() which itself sets %vcpu_dirty. Better have the generic CPU API handle this, but add a pair of assertions when serializing the accelerator state to be safe.

Quoted 5 lines
---
  target/arm/hvf/hvf.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)

r~

#27 lore raw

PATCH 13/16 target/arm/hvf: Fix WFI halting to stop idle vCPU spinning

In reply to Philippe Mathieu-Daudé

From: "Scott J. Goldman" <[email protected]>

Commit b5f8f77271 ("accel/hvf: Implement WFI without using pselect()") changed hvf_wfi() from blocking the vCPU thread with pselect() to returning EXCP_HLT, intending QEMU's main event loop to handle the idle wait. However, cpu->halted was never set, so cpu_thread_is_idle() always returns false and the vCPU thread spins at 100% CPU per core while the guest is idle.

Fix this by:

1. Setting cpu->halted = 1 in hvf_wfi() so the vCPU thread sleeps on halt_cond in qemu_process_cpu_events().

2. Arming a host-side QEMU_CLOCK_HOST timer to fire when the guest's virtual timer (CNTV_CVAL_EL0) would expire. This is necessary because HVF only delivers HV_EXIT_REASON_VTIMER_ACTIVATED during hv_vcpu_run(), which is not called while the CPU is halted. The timer callback mirrors the VTIMER_ACTIVATED handler: it raises the vtimer IRQ through the GIC and marks vtimer_masked, causing the interrupt delivery chain to wake the vCPU via qemu_cpu_kick().

3. Clearing cpu->halted in hvf_arch_vcpu_exec() when cpu_has_work() indicates a pending interrupt, and cancelling the WFI timer.

Fixes b5f8f77271 ("accel/hvf: Implement WFI without using pselect()")

Message-ID: <[email protected]>

---
 include/system/hvf_int.h |  1 +
 target/arm/hvf/hvf.c     | 65 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 65 insertions(+), 1 deletion(-)
include/system/hvf_int.h +1 -0
index 2621164cb22..df13cc650fd 100644
@@ -48,6 +48,7 @@ struct AccelCPUState {
     hv_vcpu_exit_t *exit;
     bool vtimer_masked;
     bool guest_debug_enabled;
+    QEMUTimer *wfi_timer;
 #endif
 };
 
target/arm/hvf/hvf.c +64 -1

109 lines not on this page · raw message

Signature · 1 line
2.53.0
#28 lore raw

PATCH 14/16 target/i386: Report TPR accesses to HVF

In reply to Philippe Mathieu-Daudé

HVF should be able to handle task priority register accesses.

Message-Id: <[email protected]>

---
 target/i386/helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/helper.c +1 -1
index c397a6fde5a..28bc37966c0 100644
@@ -563,7 +563,7 @@ void cpu_report_tpr_access(CPUX86State *env, TPRAccess access)
     X86CPU *cpu = env_archcpu(env);
     CPUState *cs = env_cpu(env);
 
-    if (kvm_enabled() || whpx_enabled() || nvmm_enabled()) {
+    if (kvm_enabled() || whpx_enabled() || nvmm_enabled() || hvf_enabled()) {
         env->tpr_access_type = access;
 
         cpu_interrupt(cs, CPU_INTERRUPT_TPR);
Signature · 1 line
2.53.0
#29 lore raw

PATCH 15/16 accel/hvf: fix double hv_vcpu_destroy() causing teardown error on ARM

In reply to Philippe Mathieu-Daudé

From: Matheus Tavares Bernardino <[email protected]>

The following callstack causes hv_vcpu_destroy() to be called twice, producing HV_BAD_ARGUMENT on the already-destroyed handler:

hvf_vcpu_destroy

 |
 |_ hv_vcpu_destroy
 |
 |_ hvf_arch_vcpu_destroy
     |
     |_ hv_vcpu_destroy

The first hv_vcpu_destroy call covers both x86 and arm. Let's remove the second one, as it is redundant.

This reverts commit feee55d36a1c5d494ee73812d279b439bb05137c.

Message-ID: <ee6f642af1dab29aaf99f86ac9254ddd25765bf8.1776172276.git.matheus.bernardino@oss.qualcomm.com>

---
 target/arm/hvf/hvf.c | 5 -----
 1 file changed, 5 deletions(-)
target/arm/hvf/hvf.c +0 -5
index 33595bac72c..a905d9bc370 100644
@@ -1219,13 +1219,8 @@ void hvf_arm_set_cpu_features_from_host(ARMCPU *cpu)
 
 void hvf_arch_vcpu_destroy(CPUState *cpu)
 {
-    hv_return_t ret;
-
     timer_free(cpu->accel->wfi_timer);
     cpu->accel->wfi_timer = NULL;
-
-    ret = hv_vcpu_destroy(cpu->accel->fd);
-    assert_hvf_ok(ret);
 }
 
 hv_return_t hvf_arch_vm_create(MachineState *ms, uint32_t pa_range)
Signature · 1 line
2.53.0
#30 lore raw

RFC PATCH 16/16 Reapply "target/arm/hvf: Sync CNTV_CTL_EL0 & CNTV_CVAL_EL0"

In reply to Philippe Mathieu-Daudé

This reverts commit 28b0ed32b32c7e5094cf2f1ec9c0645c65fad2aa.

Still not working...

**
ERROR:../../target/arm/machine.c:1045:cpu_pre_load: assertion failed: (!cpu->cpreg_vmstate_indexes) Bail out! ERROR:../../target/arm/machine.c:1045:cpu_pre_load: assertion failed: (!cpu->cpreg_vmstate_indexes)

---
 target/arm/hvf/hvf.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
target/arm/hvf/hvf.c +42 -0
index a905d9bc370..01b2a904c3c 100644
@@ -201,6 +201,9 @@ void hvf_arm_init_debug(void)
 #define SYSREG_PMCEID0_EL0    SYSREG(3, 3, 9, 12, 6)
 #define SYSREG_PMCEID1_EL0    SYSREG(3, 3, 9, 12, 7)
 #define SYSREG_PMCCNTR_EL0    SYSREG(3, 3, 9, 13, 0)
+
+#define SYSREG_CNTV_CTL_EL0   SYSREG(3, 3, 14, 3, 1)
+#define SYSREG_CNTV_CVAL_EL0  SYSREG(3, 3, 14, 3, 2)
 #define SYSREG_PMCCFILTR_EL0  SYSREG(3, 3, 14, 15, 7)
 
 #define SYSREG_ICC_AP0R0_EL1     SYSREG(3, 0, 12, 8, 4)
@@ -731,6 +734,40 @@ static bool hvf_sysreg_write_cp(CPUState *cpu, const char *cpname,
     return false;
 }
 
+static void hvf_arch_get_vtimer(CPUState *cpu)
+{
+    hv_return_t r;
+    uint64_t val;
+    bool b;
+
+    r = hv_vcpu_get_sys_reg(cpu->accel->fd, HV_SYS_REG_CNTV_CVAL_EL0, &val);
+    assert_hvf_ok(r);
+    b = hvf_sysreg_write_cp(cpu, "VTimer", SYSREG_CNTV_CVAL_EL0, val);
+    assert(b);
+
+    r = hv_vcpu_get_sys_reg(cpu->accel->fd, HV_SYS_REG_CNTV_CTL_EL0, &val);
+    assert_hvf_ok(r);
+    b = hvf_sysreg_write_cp(cpu, "VTimer", SYSREG_CNTV_CTL_EL0, val);
+    assert(b);
+}
+
+static void hvf_arch_put_vtimer(CPUState *cpu)
+{
+    hv_return_t r;
+    uint64_t val;
+    bool b;
+
+    b = hvf_sysreg_read_cp(cpu, "VTimer", SYSREG_CNTV_CVAL_EL0, &val);
+    assert(b);
+    r = hv_vcpu_set_sys_reg(cpu->accel->fd, HV_SYS_REG_CNTV_CVAL_EL0, val);
+    assert_hvf_ok(r);
+
+    b = hvf_sysreg_read_cp(cpu, "VTimer", SYSREG_CNTV_CTL_EL0, &val);
+    assert(b);
+    r = hv_vcpu_set_sys_reg(cpu->accel->fd, HV_SYS_REG_CNTV_CTL_EL0, val);
+    assert_hvf_ok(r);
+}
+
 int hvf_arch_get_registers(CPUState *cpu)
 {
     ARMCPU *arm_cpu = ARM_CPU(cpu);
@@ -767,6 +804,9 @@ int hvf_arch_get_registers(CPUState *cpu)
 
     ret = hv_vcpu_get_reg(cpu->accel->fd, HV_REG_CPSR, &val);
     assert_hvf_ok(ret);
+
+    hvf_arch_get_vtimer(cpu);
+
     pstate_write(env, val);
 
     for (i = 0, n = arm_cpu->cpreg_array_len; i < n; i++) {
@@ -929,6 +969,8 @@ int hvf_arch_put_registers(CPUState *cpu)
     ret = hv_vcpu_set_reg(cpu->accel->fd, HV_REG_CPSR, pstate_read(env));
     assert_hvf_ok(ret);
 
+    hvf_arch_put_vtimer(cpu);
+
     aarch64_save_sp(env, arm_current_el(env));
 
     assert(write_cpustate_to_list(arm_cpu, false));
Signature · 1 line
2.53.0
#31 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 8 lines · Philippe Mathieu-Daudé

Various patches related to HVF I'm carrying.
Some are already reviewed, some aren't :)

Except the last one (RFC) I plan to eventually send a PR with them once reviewed / tested.

target/arm/hvf: Fix WFI halting to stop idle vCPU spinning

I didn't notice you picked this one up in this omnibus patchset; I just sent some review comments on it to the v2 thread:

https://lore.kernel.org/qemu-devel/CAFEAcA9RHQ+7++=kLn2goJwcgzDnaqdkQtQBtxQ2Rw1-uiKY=g@mail.gmail.com/T/#u

thanks
-- PMM