Skip to contentSkip to conversation

target/arm: hvf: add timer freq note and stubbing LORC_EL1 reads

PATCH Applied Mohamed Mediouni 3 participants 6 messages Jul 21, 2025 – Jul 25, 2025

lore
Share & tools
lore
#0 lore raw

cover target/arm: hvf: add timer freq note and stubbing LORC_EL1 reads

Hopefully will be useful to know for somebody while I keep testing HVF vGIC and nested virt patches.

Mohamed Mediouni (2):
target/arm: hvf: add timer frequency note
target/arm: hvf: stubbing reads to LORC_EL1

 target/arm/hvf/hvf.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw

PATCH 1/2 target/arm: hvf: add timer frequency note

On Apple M3 and later, the timer frequency within apps and VM guests are different from each other.

Note that app binaries compiled with older SDKs continue to retain a non-1 GHz timer frequency on Apple M3/M4.

---
 target/arm/hvf/hvf.c | 7 +++++++
 1 file changed, 7 insertions(+)
target/arm/hvf/hvf.c +7 -0
index c9cfcdc08b..4331a3386a 100644
@@ -1018,6 +1018,13 @@ int hvf_arch_init_vcpu(CPUState *cpu)
     int i;
 
     env->aarch64 = true;
+    /*
+     * TODO: This does not correspond to the exposed generic
+     * timer frequency to the guest on Apple M3/M4.
+     * This is due to a scaled 1GHz counter being exposed
+     * to applications built with newer SDKs when running
+     * on that silicon, while VMs get an unscaled counter.
+     */
     asm volatile("mrs %0, cntfrq_el0" : "=r"(arm_cpu->gt_cntfrq_hz));
 
     /* Allocate enough space for our sysreg sync */
Signature · 1 line
2.39.5 (Apple Git-154)
#2 lore raw

Re: [PATCH 1/2] target/arm: hvf: add timer frequency note

Hi Mohamed,

Quoted 1 line · Mohamed Mediouni

On Apple M3 and later, the timer frequency within apps and VM guests are different from each other.

Also M1/M2, so all Apple Silicon chipsets IMO.

This might be the issue I fixed here:
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 24 lines

Note that app binaries compiled with older SDKs continue to retain a non-1 GHz timer frequency on Apple M3/M4.

---
  target/arm/hvf/hvf.c | 7 +++++++
  1 file changed, 7 insertions(+)
target/arm/hvf/hvf.c +7 -0
index c9cfcdc08b..4331a3386a 100644
@@ -1018,6 +1018,13 @@ int hvf_arch_init_vcpu(CPUState *cpu)
      int i;
 
      env->aarch64 = true;
+    /*
+     * TODO: This does not correspond to the exposed generic
+     * timer frequency to the guest on Apple M3/M4.
+     * This is due to a scaled 1GHz counter being exposed
+     * to applications built with newer SDKs when running
+     * on that silicon, while VMs get an unscaled counter.
+     */
      asm volatile("mrs %0, cntfrq_el0" : "=r"(arm_cpu->gt_cntfrq_hz));
#3 lore raw

Re: [PATCH 1/2] target/arm: hvf: add timer frequency note

Hello,

Nevermind.

It turns out that… the timer frequency exposed is different depending on the entitlements the program has...

With this tiny test sequence:

accel/hvf/hvf-all.c +3 -1
index e67a8105a6..390afb03a4 100644
@@ -262,7 +262,9 @@ static int hvf_accel_init(AccelState *as, MachineState *ms)
             return -EINVAL;
         }
     }
-
+    uint64_t frq;
+    asm volatile("mrs %0, cntfrq_el0" : "=r"(frq));
+    printf("Timer frequency before hvf_arch_vm_create: %" PRIu64 "\n", frq);
     ret = hvf_arch_vm_create(ms, (uint32_t)pa_range);
     if (ret == HV_DENIED) {
         error_report("Could not access HVF. Is the executable signed"

It turns out that:

% ./qemu-system-aarch64-unsigned -M virt,accel=hvf,gic-version=3,nested=on,its=off […]
Timer frequency before hvf_arch_vm_create: 1000000000
qemu-system-aarch64-unsigned: Could not access HVF. Is the executable signed with com.apple.security.hypervisor entitlement? % ./qemu-system-aarch64-unsigned -M virt,accel=hvf,gic-version=3,nested=on,its=off […]
Timer frequency before hvf_arch_vm_create: 24000000
[…]

Sigh.

Quoted 34 lines · Philippe Mathieu-Daudé

Hi Mohamed,

Quoted 1 line · Mohamed Mediouni

On Apple M3 and later, the timer frequency within apps and VM guests are different from each other.

Also M1/M2, so all Apple Silicon chipsets IMO.

This might be the issue I fixed here:
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 20 lines

Note that app binaries compiled with older SDKs continue to retain a non-1 GHz timer frequency on Apple M3/M4.

---
 target/arm/hvf/hvf.c | 7 +++++++
 1 file changed, 7 insertions(+)
target/arm/hvf/hvf.c +7 -0
index c9cfcdc08b..4331a3386a 100644
@@ -1018,6 +1018,13 @@ int hvf_arch_init_vcpu(CPUState *cpu)
     int i;
       env->aarch64 = true;
+    /*
+     * TODO: This does not correspond to the exposed generic
+     * timer frequency to the guest on Apple M3/M4.
+     * This is due to a scaled 1GHz counter being exposed
+     * to applications built with newer SDKs when running
+     * on that silicon, while VMs get an unscaled counter.
+     */
     asm volatile("mrs %0, cntfrq_el0" : "=r"(arm_cpu->gt_cntfrq_hz));
#4 lore raw

PATCH 2/2 target/arm: hvf: stubbing reads to LORC_EL1

In reply to Mohamed Mediouni

Linux zeroes LORC_EL1 on boot at EL2, without further interaction with FEAT_LOR afterwards.

Stub out LORC_EL1 accesses as FEAT_LOR is a mandatory extension on Armv8.1+.

---
 target/arm/hvf/hvf.c | 4 ++++
 1 file changed, 4 insertions(+)
target/arm/hvf/hvf.c +4 -0
index 4331a3386a..2730c9793d 100644
@@ -186,6 +186,7 @@ void hvf_arm_init_debug(void)
 #define SYSREG_OSLAR_EL1      SYSREG(2, 0, 1, 0, 4)
 #define SYSREG_OSLSR_EL1      SYSREG(2, 0, 1, 1, 4)
 #define SYSREG_OSDLR_EL1      SYSREG(2, 0, 1, 3, 4)
+#define SYSREG_LORC_EL1       SYSREG(3, 0, 10, 4, 3)
 #define SYSREG_CNTPCT_EL0     SYSREG(3, 3, 14, 0, 1)
 #define SYSREG_CNTP_CTL_EL0   SYSREG(3, 3, 14, 2, 1)
 #define SYSREG_PMCR_EL0       SYSREG(3, 3, 9, 12, 0)
@@ -1657,6 +1658,9 @@ static int hvf_sysreg_write(CPUState *cpu, uint32_t reg, uint64_t val)
     case SYSREG_OSDLR_EL1:
         /* Dummy register */
         return 0;
+    case SYSREG_LORC_EL1:
+        /* Dummy register */
+        return 0;
     case SYSREG_ICC_AP0R0_EL1:
     case SYSREG_ICC_AP0R1_EL1:
     case SYSREG_ICC_AP0R2_EL1:
Signature · 1 line
2.39.5 (Apple Git-154)
#5 lore raw

Re: [PATCH 2/2] target/arm: hvf: stubbing reads to LORC_EL1

Quoted 9 lines · Mohamed Mediouni

Linux zeroes LORC_EL1 on boot at EL2, without further interaction with FEAT_LOR afterwards.

Stub out LORC_EL1 accesses as FEAT_LOR is a mandatory extension on Armv8.1+.

---
 target/arm/hvf/hvf.c | 4 ++++
 1 file changed, 4 insertions(+)

Applied to target-arm.next, thanks (since it's a bugfix even if it happens not to be triggered by Linux in normal booting-into-EL1 config).

-- PMM