Skip to contentSkip to conversation

pull-loongarch-20261023 queue

GIT PULL Applied Bibo Mao 3 participants 14 messages Oct 23 – Nov 16

lore
Share & tools
lore

Bibo Mao

author
#0 lore raw

The following changes since commit ed698be7700160c74d24ef7830adccd2a28d840a:

Merge tag 'firmware-20261021-pull-request' of https://gitlab.com/kraxel/qemu into staging (2026-10-22 08:20:10 +0200)

are available in the Git repository at:

https://github.com/bibo-mao/qemu.git tags/pull-loongarch-20261023

for you to fetch changes up to b8cb88aaa32d8e1ff55c5bc347bae7da09f2e63f:

target/loongarch: Select sys state by VM level (2026-10-23 09:28:51 +0800)

----------------------------------------------------------------
pull-loongarch-20261023 queue

----------------------------------------------------------------
Bibo Mao (8):
target/loongarch: Replace address_space_ldq_le() -> cpu_ldq_le_data_ra() target/loongarch: Use input parameter mmu_idx rather than current mode target/loongarch: Replace address_space_ldq_le -> cpu_ldq_le_mmuidx_ra in loongarch_ptw target/loongarch: Add dynamic irq_nr field
target/loongarch: Remove TLB entry in vmstate
target/loongarch: Add paravirt preempt feature
target/loongarch: Preserve PTW dirty tracking on read fills
hw/intc: Acess LoongArch irqchip data with align method

SignKirigami (2):
target/loongarch: Delete macro CSR_OFF_FUNCS target/loongarch: Select sys state by VM level

 hw/intc/loongarch_extioi.c        | 51 ++++++++++++++++++++-----------
 hw/intc/loongarch_ipi.c           |  9 +++---
 hw/intc/loongarch_pch_pic.c       | 28 +++++++++--------
 target/loongarch/cpu.c            | 12 +++++---
 target/loongarch/cpu.h            | 10 ++++--
 target/loongarch/cpu_helper.c     | 64 +++++++++------------------------------
 target/loongarch/csr.c            | 15 +++++----
 target/loongarch/kvm/kvm.c        | 39 ++++++++++++++++++++++++
 target/loongarch/machine.c        | 35 ---------------------
 target/loongarch/tcg/tlb_helper.c | 30 +++++++++---------
 10 files changed, 145 insertions(+), 148 deletions(-)
author
#1 lore raw

[PULL 09/10] target/loongarch: Delete macro CSR_OFF_FUNCS

From: SignKirigami <[email protected]>

Now CSR helpers are set by SET_CSR_FUNC in trans_privileged.c.inc, so this macro is not needed.

---
 target/loongarch/csr.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
target/loongarch/csr.c +7 -8
index d2ac39dee9..e474941337 100644
@@ -6,21 +6,20 @@
 #include "cpu.h"
 #include "csr.h"
 
-#define CSR_OFF_FUNCS(NAME, FL, RD, WR)                    \
+#define CSR_OFF_FLAGS(NAME, FL)                            \
     [LOONGARCH_CSR_##NAME] = {                             \
         .name   = (stringify(NAME)),                       \
         .offset = CSR_OFFSET(CSR_##NAME),                  \
-        .flags = FL, .readfn = RD, .writefn = WR           \
+        .flags  = FL                                       \
     }
 
-#define CSR_OFF_ARRAY(NAME, N)                                \
-    [LOONGARCH_CSR_##NAME(N)] = {                             \
-        .name   = (stringify(NAME##N)),                       \
-        .offset = CSR_OFFSET(CSR_##NAME[N]),                  \
-        .flags = CSRFL_BASIC, .readfn = NULL, .writefn = NULL           \
+#define CSR_OFF_ARRAY(NAME, N)                             \
+    [LOONGARCH_CSR_##NAME(N)] = {                          \
+        .name   = (stringify(NAME##N)),                    \
+        .offset = CSR_OFFSET(CSR_##NAME[N]),               \
+        .flags  = CSRFL_BASIC                              \
     }
 
-#define CSR_OFF_FLAGS(NAME, FL)   CSR_OFF_FUNCS(NAME, FL, NULL, NULL)
 #define CSR_OFF(NAME)             CSR_OFF_FLAGS(NAME, CSRFL_BASIC)
 
 static CSRInfo csr_info[] = {
Signature · 1 line
2.54.0
author
#2 lore raw

[PULL 01/10] target/loongarch: Replace address_space_ldq_le() -> cpu_ldq_le_data_ra()

In reply to Bibo Mao

With LDDIR/LDPTE instruction, it accesses page table entry with physical address, so it can only be used in DA mode. Replace API address_space_ldq_le() with cpu_ldq_le_data_ra(), since there is QEMU CPU TLB with the latter. And in future if LVZ is supported, the latter API will call the second MMU page table walk.

---
 target/loongarch/tcg/tlb_helper.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
target/loongarch/tcg/tlb_helper.c +2 -6
index 40cc076424..b6381f7cd3 100644
@@ -21,7 +21,6 @@
 #include "exec/log.h"
 #include "cpu-csr.h"
 #include "tcg/tcg_loongarch.h"
-#include "system/memory.h"
 
 typedef bool (*tlb_match)(bool global, int asid, int tlb_asid);
 
@@ -721,7 +720,6 @@ static inline uint64_t loongarch_sanitize_hw_pte(CPULoongArchState *env,
 target_ulong helper_lddir(CPULoongArchState *env, target_ulong base,
                           uint32_t level, uint32_t mem_idx)
 {
-    CPUState *cs = env_cpu(env);
     uint64_t badvaddr;
     hwaddr index, phys;
     uint64_t palen_mask = loongarch_palen_mask(env);
@@ -754,7 +752,7 @@ target_ulong helper_lddir(CPULoongArchState *env, target_ulong base,
     get_dir_base_width(env, &dir_base, &dir_width, level);
     index = (badvaddr >> dir_base) & ((1 << dir_width) - 1);
     phys = base | index << 3;
-    val = address_space_ldq_le(cs->as, phys, MEMTXATTRS_UNSPECIFIED, NULL);
+    val = cpu_ldq_le_data_ra(env, phys, GETPC());
 
     return val & palen_mask;
 }
@@ -762,7 +760,6 @@ target_ulong helper_lddir(CPULoongArchState *env, target_ulong base,
 void helper_ldpte(CPULoongArchState *env, target_ulong base, target_ulong odd,
                   uint32_t mem_idx)
 {
-    CPUState *cs = env_cpu(env);
     hwaddr phys, tmp0, ptindex, ptoffset0, ptoffset1;
     CPUSysState *sys = env_sys(env);
     uint64_t pte_raw;
@@ -823,8 +820,7 @@ void helper_ldpte(CPULoongArchState *env, target_ulong base, target_ulong odd,
         ptoffset0 = ptindex << 3;
         ptoffset1 = (ptindex + 1) << 3;
         phys = base | (odd ? ptoffset1 : ptoffset0);
-        pte_raw = address_space_ldq_le(cs->as, phys,
-                                       MEMTXATTRS_UNSPECIFIED, NULL);
+        pte_raw = cpu_ldq_le_data_ra(env, phys, GETPC());
         tmp0 = loongarch_sanitize_hw_pte(env, pte_raw);
         ps = ptbase;
     }
Signature · 1 line
2.54.0
author
#3 lore raw

[PULL 02/10] target/loongarch: Use input parameter mmu_idx rather than current mode

In reply to Bibo Mao

With PTW enabled, hardware accesses page table entry with physical address, which equals to MMU_DA_IDX mode, even if current CPU runs in PAGE enabled. Here use input parameter mmu_idx rather than checking current CPU running mode.

---
 target/loongarch/cpu_helper.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
target/loongarch/cpu_helper.c +1 -3
index 123cad3d93..8a4f8ebc4d 100644
@@ -319,13 +319,11 @@ TLBRet get_physical_address(CPULoongArchState *env, MMUContext *context,
     uint32_t plv, base_c, base_v;
     int64_t addr_high;
     CPUSysState *sys = env_sys(env);
-    uint8_t da = FIELD_EX64(sys->CSR_CRMD, CSR_CRMD, DA);
-    uint8_t pg = FIELD_EX64(sys->CSR_CRMD, CSR_CRMD, PG);
     vaddr address;
 
     /* Check PG and DA */
     address = context->addr;
-    if (da & !pg) {
+    if (mmu_idx == MMU_DA_IDX) {
         context->physical = address & loongarch_palen_mask(env);
         context->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
         context->mmu_index = MMU_DA_IDX;
Signature · 1 line
2.54.0
author
#4 lore raw

[PULL 04/10] target/loongarch: Add dynamic irq_nr field

In reply to Bibo Mao

By default, the number of IRQs on the CPU side is N_IRQS. It will become N_IRQS * 2 if LVZ is enabled in the future. This patch adds a CPUArchState::irq_nr field to record the supported IRQ count.

---
 target/loongarch/cpu.c | 8 +++++---
 target/loongarch/cpu.h | 1 +
 2 files changed, 6 insertions(+), 3 deletions(-)
target/loongarch/cpu.c +5 -3
index 54c2699897..9cf566f0f6 100644
@@ -88,8 +88,9 @@ void loongarch_cpu_set_irq(void *opaque, int irq, int level)
 {
     LoongArchCPU *cpu = opaque;
     CPUState *cs = CPU(cpu);
+    CPULoongArchState *env = &cpu->env;
 
-    if (irq < 0 || irq >= N_IRQS) {
+    if (irq < 0 || irq >= env->irq_nr) {
         return;
     }
 
@@ -724,12 +725,13 @@ static void loongarch_cpu_init(Object *obj)
 {
 #ifndef CONFIG_USER_ONLY
     LoongArchCPU *cpu = LOONGARCH_CPU(obj);
-#ifdef CONFIG_TCG
     CPULoongArchState *env = &cpu->env;
+#ifdef CONFIG_TCG
     CPUTimerState *timer;
 #endif
 
-    qdev_init_gpio_in(DEVICE(cpu), loongarch_cpu_set_irq, N_IRQS);
+    env->irq_nr = N_IRQS;
+    qdev_init_gpio_in(DEVICE(cpu), loongarch_cpu_set_irq, env->irq_nr);
 #ifdef CONFIG_TCG
     timer = env_timer(env);
     timer->irq = IRQ_TIMER;
target/loongarch/cpu.h +1 -0
index c592f69885..77ea8f03f9 100644
@@ -437,6 +437,7 @@ typedef struct CPUArchState {
     uint32_t mp_state;
 #endif
     CPUSysState *sys_state;
+    unsigned int irq_nr;
 } CPULoongArchState;
 
 typedef struct LoongArchCPUTopo {
Signature · 1 line
2.54.0
author
#5 lore raw

[PULL 05/10] target/loongarch: Remove TLB entry in vmstate

In reply to Bibo Mao

The content of TLB entry may change if LVZ is added, TLB table can re-establish from empty state after VM migration. There will be compatible issue if TLB entry is removed in vmstate, however it is only affect TCG mode.

---
 target/loongarch/machine.c | 35 -----------------------------------
 1 file changed, 35 deletions(-)
target/loongarch/machine.c +0 -35
index 931a5ca5ba..952c5497af 100644
@@ -175,38 +175,6 @@ static const VMStateDescription vmstate_pmu = {
     },
 };
 
-#if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
-static bool tlb_needed(void *opaque)
-{
-    return tcg_enabled();
-}
-
-/* TLB state */
-static const VMStateDescription vmstate_tlb_entry = {
-    .name = "cpu/tlb_entry",
-    .version_id = 0,
-    .minimum_version_id = 0,
-    .fields = (const VMStateField[]) {
-        VMSTATE_UINT64(tlb_misc, LoongArchTLB),
-        VMSTATE_UINT64(tlb_entry0, LoongArchTLB),
-        VMSTATE_UINT64(tlb_entry1, LoongArchTLB),
-        VMSTATE_END_OF_LIST()
-    }
-};
-
-static const VMStateDescription vmstate_tlb = {
-    .name = "cpu/tlb",
-    .version_id = 0,
-    .minimum_version_id = 0,
-    .needed = tlb_needed,
-    .fields = (const VMStateField[]) {
-        VMSTATE_STRUCT_ARRAY(env.tlb, LoongArchCPU, LOONGARCH_TLB_MAX,
-                             0, vmstate_tlb_entry, LoongArchTLB),
-        VMSTATE_END_OF_LIST()
-    }
-};
-#endif
-
 /* LoongArch CPU state */
 const VMStateDescription vmstate_loongarch_cpu = {
     .name = "cpu",
@@ -283,9 +251,6 @@ const VMStateDescription vmstate_loongarch_cpu = {
         &vmstate_fpu,
         &vmstate_lsx,
         &vmstate_lasx,
-#if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
-        &vmstate_tlb,
-#endif
         &vmstate_lbt,
         &vmstate_msgint,
         &vmstate_pmu,
Signature · 1 line
2.54.0
#6 lore raw

Re: [PULL 05/10] target/loongarch: Remove TLB entry in vmstate

Quoted 11 lines · Bibo Mao

The content of TLB entry may change if LVZ is added, TLB table can re-establish from empty state after VM migration. There will be compatible issue if TLB entry is removed in vmstate, however it is only affect TCG mode.

---
 target/loongarch/machine.c | 35 -----------------------------------
 1 file changed, 35 deletions(-)

Hi; as pointed out in https://gitlab.com/qemu-project/qemu/-/work_items/4879
this change breaks migration of a loongarch "virt-11.1" machine from QEMU 11.1 to current head-of-git. Since loongarch supports versioned machine types that means you must preserve migration compatibility across QEMU versions.

In this case if the destination QEMU can reconstruct its TLB state without the incoming information, that means you need to accept and ignore these vmstate sections in the incoming migration data.

Quoted 44 lines
target/loongarch/machine.c +0 -32
index 931a5ca5ba..952c5497af 100644
@@ -175,38 +175,6 @@ static const VMStateDescription vmstate_pmu = {
     },
 };
 
-#if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
-static bool tlb_needed(void *opaque)
-{
-    return tcg_enabled();
-}
-
-/* TLB state */
-static const VMStateDescription vmstate_tlb_entry = {
-    .name = "cpu/tlb_entry",
-    .version_id = 0,
-    .minimum_version_id = 0,
-    .fields = (const VMStateField[]) {
-        VMSTATE_UINT64(tlb_misc, LoongArchTLB),
-        VMSTATE_UINT64(tlb_entry0, LoongArchTLB),
-        VMSTATE_UINT64(tlb_entry1, LoongArchTLB),
-        VMSTATE_END_OF_LIST()
-    }
-};
-
-static const VMStateDescription vmstate_tlb = {
-    .name = "cpu/tlb",
-    .version_id = 0,
-    .minimum_version_id = 0,
-    .needed = tlb_needed,
-    .fields = (const VMStateField[]) {
-        VMSTATE_STRUCT_ARRAY(env.tlb, LoongArchCPU, LOONGARCH_TLB_MAX,
-                             0, vmstate_tlb_entry, LoongArchTLB),
-        VMSTATE_END_OF_LIST()
-    }
-};
-#endif
-
 /* LoongArch CPU state */
 const VMStateDescription vmstate_loongarch_cpu = {
     .name = "cpu",

target/loongarch/machine.c const VMStateDescription vmstate_loongarch_cpu = { · line 256

Quoted 9 lines
target/loongarch/machine.c +0 -3
@@ -283,9 +251,6 @@ const VMStateDescription vmstate_loongarch_cpu = {
         &vmstate_fpu,
         &vmstate_lsx,
         &vmstate_lasx,
-#if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
-        &vmstate_tlb,
-#endif
         &vmstate_lbt,
         &vmstate_msgint,
         &vmstate_pmu,

thanks
-- PMM

author
#7 lore raw

Re: [PULL 05/10] target/loongarch: Remove TLB entry in vmstate

Quoted 19 lines · Peter Maydell
Quoted 11 lines · Bibo Mao

The content of TLB entry may change if LVZ is added, TLB table can re-establish from empty state after VM migration. There will be compatible issue if TLB entry is removed in vmstate, however it is only affect TCG mode.

---
  target/loongarch/machine.c | 35 -----------------------------------
  1 file changed, 35 deletions(-)

Hi; as pointed out in https://gitlab.com/qemu-project/qemu/-/work_items/4879
this change breaks migration of a loongarch "virt-11.1" machine from QEMU 11.1 to current head-of-git. Since loongarch supports versioned machine types that means you must preserve migration compatibility across QEMU versions.

Quoted 4 lines

In this case if the destination QEMU can reconstruct its TLB state without the incoming information, that means you need to accept and ignore these vmstate sections in the incoming migration data.

Hi Peter,

Thanks for pointing out the migration issue. Will revert this patch, and add another patch to accept and ignore these vmstate_tlb sections.

Regards
Bibo Mao

Quoted 58 lines
Quoted 45 lines
target/loongarch/machine.c +0 -32
index 931a5ca5ba..952c5497af 100644
@@ -175,38 +175,6 @@ static const VMStateDescription vmstate_pmu = {
      },
  };
 
-#if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
-static bool tlb_needed(void *opaque)
-{
-    return tcg_enabled();
-}
-
-/* TLB state */
-static const VMStateDescription vmstate_tlb_entry = {
-    .name = "cpu/tlb_entry",
-    .version_id = 0,
-    .minimum_version_id = 0,
-    .fields = (const VMStateField[]) {
-        VMSTATE_UINT64(tlb_misc, LoongArchTLB),
-        VMSTATE_UINT64(tlb_entry0, LoongArchTLB),
-        VMSTATE_UINT64(tlb_entry1, LoongArchTLB),
-        VMSTATE_END_OF_LIST()
-    }
-};
-
-static const VMStateDescription vmstate_tlb = {
-    .name = "cpu/tlb",
-    .version_id = 0,
-    .minimum_version_id = 0,
-    .needed = tlb_needed,
-    .fields = (const VMStateField[]) {
-        VMSTATE_STRUCT_ARRAY(env.tlb, LoongArchCPU, LOONGARCH_TLB_MAX,
-                             0, vmstate_tlb_entry, LoongArchTLB),
-        VMSTATE_END_OF_LIST()
-    }
-};
-#endif
-
  /* LoongArch CPU state */
  const VMStateDescription vmstate_loongarch_cpu = {
      .name = "cpu",

target/loongarch/machine.c const VMStateDescription vmstate_loongarch_cpu = { · line 256

Quoted 9 lines
target/loongarch/machine.c +0 -3
@@ -283,9 +251,6 @@ const VMStateDescription vmstate_loongarch_cpu = {
          &vmstate_fpu,
          &vmstate_lsx,
          &vmstate_lasx,
-#if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
-        &vmstate_tlb,
-#endif
          &vmstate_lbt,
          &vmstate_msgint,
          &vmstate_pmu,

thanks
-- PMM

author
#8 lore raw

[PULL 06/10] target/loongarch: Add paravirt preempt feature

In reply to Bibo Mao

Paravirt preempt feature is OnOffAuto type, it is disabled by default, also the feature detection is added to check whether it is supported on KVM host.

---
 target/loongarch/cpu.h     |  2 ++
 target/loongarch/kvm/kvm.c | 39 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
target/loongarch/cpu.h +2 -0
index 77ea8f03f9..4ab81e0f7b 100644
@@ -302,6 +302,7 @@ enum loongarch_features {
     LOONGARCH_FEATURE_STEALTIME,
     LOONGARCH_FEATURE_PTW,
     LOONGARCH_FEATURE_MSGINT,
+    LOONGARCH_FEATURE_PV_PREEMPT,
 };
 
 typedef struct  LoongArchBT {
@@ -465,6 +466,7 @@ struct ArchCPU {
     OnOffAuto msgint;
     OnOffAuto kvm_pv_ipi;
     OnOffAuto kvm_steal_time;
+    OnOffAuto kvm_pv_preempt;
     int32_t socket_id;  /* socket-id of this CPU */
     int32_t core_id;    /* core-id of this CPU */
     int32_t thread_id;  /* thread-id of this CPU */
target/loongarch/kvm/kvm.c +39 -0
index 941142305e..7ca95181a1 100644
@@ -1039,6 +1039,12 @@ static bool kvm_feature_supported(CPUState *cs, enum loongarch_features feature)
         ret = kvm_vm_ioctl(kvm_state, KVM_HAS_DEVICE_ATTR, &attr);
         return (ret == 0);
 
+    case LOONGARCH_FEATURE_PV_PREEMPT:
+        attr.group = KVM_LOONGARCH_VM_FEAT_CTRL;
+        attr.attr = KVM_LOONGARCH_VM_FEAT_PV_PREEMPT,
+        ret = kvm_vm_ioctl(kvm_state, KVM_HAS_DEVICE_ATTR, &attr);
+        return (ret == 0);
+
     default:
         return false;
     }
@@ -1203,6 +1209,21 @@ static int kvm_cpu_check_pv_features(CPUState *cs, Error **errp)
         env->pv_features |= BIT(KVM_FEATURE_STEAL_TIME);
     }
 
+    kvm_supported = kvm_feature_supported(cs, LOONGARCH_FEATURE_PV_PREEMPT);
+    if (cpu->kvm_pv_preempt == ON_OFF_AUTO_ON) {
+        if (!kvm_supported) {
+            error_setg(errp, "'pv preempt' feature not supported by KVM host");
+            return -ENOTSUP;
+        }
+    } else if (cpu->kvm_pv_preempt != ON_OFF_AUTO_AUTO) {
+        kvm_supported = false;
+    }
+
+    /* Feature preempt_hint depends on steal_time */
+    if (kvm_supported && (cpu->kvm_pv_preempt == ON_OFF_AUTO_ON)) {
+        env->pv_features |= BIT(KVM_FEATURE_PREEMPT);
+     }
+
     if (object_dynamic_cast(OBJECT(ms), TYPE_LOONGARCH_VIRT_MACHINE)) {
         LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(ms);
 
@@ -1412,6 +1433,18 @@ static void kvm_steal_time_set(Object *obj, bool value, Error **errp)
     cpu->kvm_steal_time = value ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
 }
 
+static bool kvm_pv_preempt_get(Object *obj, Error **errp)
+{
+    return LOONGARCH_CPU(obj)->kvm_pv_preempt != ON_OFF_AUTO_OFF;
+}
+
+static void kvm_pv_preempt_set(Object *obj, bool value, Error **errp)
+{
+    LoongArchCPU *cpu = LOONGARCH_CPU(obj);
+
+    cpu->kvm_pv_preempt = value ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
+}
+
 void kvm_loongarch_cpu_post_init(LoongArchCPU *cpu)
 {
     cpu->lbt = ON_OFF_AUTO_AUTO;
@@ -1437,6 +1470,12 @@ void kvm_loongarch_cpu_post_init(LoongArchCPU *cpu)
                              kvm_steal_time_set);
     object_property_set_description(OBJECT(cpu), "kvm-steal-time",
                                     "Set off to disable KVM steal time.");
+
+    cpu->kvm_pv_preempt = ON_OFF_AUTO_OFF;
+    object_property_add_bool(OBJECT(cpu), "kvm-pv-preempt", kvm_pv_preempt_get,
+                             kvm_pv_preempt_set);
+    object_property_set_description(OBJECT(cpu), "kvm-pv-preempt",
+                                    "Set off to disable KVM paravirt preempt.");
 }
 
 int kvm_arch_destroy_vcpu(CPUState *cs)
Signature · 1 line
2.54.0
author
#9 lore raw

[PULL 03/10] target/loongarch: Replace address_space_ldq_le -> cpu_ldq_le_mmuidx_ra in loongarch_ptw

In reply to Bibo Mao

In function loongarch_ptw(), page table entry is accessed with physical address, so it is similar with DA mode. Replace API address_space_ldq_le() with cpu_ldq_le_mmuidx_ra(), and mmu_idx is MMU_DA_IDX rather than current CPU running mode, since there is QEMU CPU TLB with the latter API. And in future if LVZ is supported, the latter API will call the second MMU page table walk.

---
 target/loongarch/cpu_helper.c | 60 ++++++++---------------------------
 1 file changed, 13 insertions(+), 47 deletions(-)
target/loongarch/cpu_helper.c +13 -47
index 8a4f8ebc4d..d1d66742d8 100644
@@ -7,10 +7,11 @@
  */
 
 #include "qemu/osdep.h"
-#include "system/memory.h"
 #include "system/tcg.h"
 #include "cpu.h"
 #include "accel/tcg/cpu-mmu-index.h"
+#include "accel/tcg/cpu-ldst.h"
+#include "accel/tcg/getpc.h"
 #include "exec/page-protection.h"
 #include "exec/target_page.h"
 #include "internals.h"
@@ -111,55 +112,18 @@ TLBRet loongarch_check_pte(CPULoongArchState *env, MMUContext *context,
     return TLBRET_MATCH;
 }
 
-static MemTxResult loongarch_cmpxchg_phys(CPUState *cs, hwaddr phys,
-                                          uint64_t old, uint64_t new)
-{
-    hwaddr addr1, l = 8;
-    MemoryRegion *mr;
-    uint8_t *ram_ptr;
-    uint64_t old1;
-    MemTxResult ret;
-
-    rcu_read_lock();
-    mr = address_space_translate(cs->as, phys, &addr1, &l,
-                                 false, MEMTXATTRS_UNSPECIFIED);
-    if (!memory_region_is_ram(mr)) {
-        /*
-         * Misconfigured PTE in ROM (AD bits are not preset) or
-         * PTE is in IO space and can't be updated atomically.
-         */
-         rcu_read_unlock();
-         return MEMTX_ACCESS_ERROR;
-    }
-
-    ram_ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
-    old1 = qatomic_cmpxchg((uint64_t *)ram_ptr, cpu_to_le64(old),
-                           cpu_to_le64(new));
-    old1 = le64_to_cpu(old1);
-    if (old1 == old) {
-        ret = MEMTX_OK;
-    } else {
-        ret = MEMTX_DECODE_ERROR;
-    }
-    rcu_read_unlock();
-
-    return ret;
-}
-
 TLBRet loongarch_ptw(CPULoongArchState *env, MMUContext *context,
                      int access_type, int mmu_idx, int debug)
 {
-    const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED;
-    CPUState *cs = env_cpu(env);
     hwaddr index = 0, phys = 0;
     uint64_t palen_mask = loongarch_palen_mask(env);
     uint64_t dir_base, dir_width;
-    uint64_t base, pte;
-    int level;
+    uint64_t base, pte, old;
+    int level, ptw_idx = MMU_DA_IDX;
     vaddr address;
     TLBRet ret;
-    MemTxResult ret1;
     CPUSysState *sys = env_sys(env);
+    MemOpIdx oi;
 
     address = context->addr;
     if ((address >> 63) & 0x1) {
@@ -179,7 +143,7 @@ TLBRet loongarch_ptw(CPULoongArchState *env, MMUContext *context,
         /* get next level page directory */
         index = (address >> dir_base) & ((1 << dir_width) - 1);
         phys = base | index << 3;
-        base = address_space_ldq_le(cs->as, phys, attrs, NULL);
+        base = cpu_ldq_le_mmuidx_ra(env, phys, ptw_idx, GETPC());
         if (level) {
             if (FIELD_EX64(base, TLBENTRY, HUGE)) {
                 /* base is a huge pte */
@@ -213,8 +177,8 @@ restart:
 
         index &= 1;
         context->pte_buddy[index] = base;
-        val = address_space_ldq_le(cs->as, phys + 8 * (1 - 2 * index),
-                                   attrs, NULL);
+        val = cpu_ldq_le_mmuidx_ra(env, phys + 8 * (1 - 2 * index),
+                                   ptw_idx, GETPC());
         context->pte_buddy[1 - index] = val;
     }
 
@@ -244,10 +208,12 @@ restart:
         if (access_type == MMU_DATA_STORE) {
             base = pte_mkdirty(base);
         }
-        ret1 = loongarch_cmpxchg_phys(cs, phys, pte, base);
+
+        oi = make_memop_idx(MO_LEUQ | MO_64, ptw_idx);
+        old = cpu_atomic_cmpxchgq_le_mmu(env, phys, pte, base, oi, GETPC());
         /* PTE updated by other CPU, reload PTE entry */
-        if (ret1 == MEMTX_DECODE_ERROR) {
-            base = address_space_ldq_le(cs->as, phys, attrs, NULL);
+        if (le64_to_cpu(old) != pte) {
+            base = cpu_ldq_le_mmuidx_ra(env, phys, ptw_idx, GETPC());
             goto restart;
         }
 
Signature · 1 line
2.54.0
author
#10 lore raw

[PULL 08/10] hw/intc: Acess LoongArch irqchip data with align method

In reply to Bibo Mao

With the clang sanitizer configured with
"--enable-ubsan --cc=clang --cxx=clang++" and run 'make check'. The test-hmp test falls over with this backtrace:

    ../../hw/intc/loongarch_extioi.c:439:33: runtime error: load of

misaligned address 0x5b0a281448b4 for type 'uint64_t' (aka 'unsigned long'), which requires 8 byte alignment.

Here LoongArch irqchip information is accessed by its natual aligned method, and here is output of command 'info pic' loongarch_ipi:
CPU 0 status=0x0 en=0xffffffff

          MBX[6] 0x00082270
  CPU 1   status=0x0 en=0xffffffff
  CPU 2   status=0x0 en=0xffffffff
  CPU 3   status=0x0 en=0xffffffff

loongarch_pic:
mask 0xffffffffffffff7b msi_en 0xffffffffffffffff edge 0x0000000000000080 clr 0x0000000000000000
last_irr 0x0000000000000040 irr 0x0000000000000040 isr 0x0000000000000000 polary 0x0000000000000000
MSI map:

    [ 0 -  3] 0x00 0x01 0x02 0x03
    ...
    [60 - 63] 0x3c 0x3d 0x3e 0x3f
loongarch.extioi:
  ISR     0x00000000 0x00000000 0x00000000 0x00000000
          0x00000000 0x00000000 0x00000000 0x00000000
  ENABLE  0xffffffff 0xffffffff 0xffffffff 0xffffffff
          0xffffffff 0xffffffff 0xffffffff 0xffffffff
  IPMAP   0x02020202 0x02020202
  IRQ route:
    [0   - 15 ] 0x01010101 0x01010101 0x01010101 0x01010101
    ...
    [240 - 255] 0x01010101 0x01010101 0x01010101 0x01010101
Fixes 970657e490c0 ("hw/intc/loongarch_extioi: Add extioi interrupt status display")
Fixes 761eb76a3bc7 ("hw/intc/loongarch_ipi: Add ipi interrupt status display")
Fixes 82f472fa380a ("hw/intc/loongarch_pch_pic: Add pic interrupt status display")
---
 hw/intc/loongarch_extioi.c  | 51 ++++++++++++++++++++++++-------------
 hw/intc/loongarch_ipi.c     |  9 ++++---
 hw/intc/loongarch_pch_pic.c | 28 +++++++++++---------
 3 files changed, 55 insertions(+), 33 deletions(-)
hw/intc/loongarch_extioi.c +34 -17
index 06f2aa7b14..ccc4eed199 100644
@@ -425,7 +425,7 @@ static void loongarch_extioi_print_info(InterruptStatsProvider *obj,
                                         GString *buf)
 {
     LoongArchExtIOICommonState *s = LOONGARCH_EXTIOI_COMMON(obj);
-    uint64_t *val;
+    uint32_t *val;
     unsigned long irq;
     ExtIOICore *core;
     int i;
@@ -433,22 +433,35 @@ static void loongarch_extioi_print_info(InterruptStatsProvider *obj,
     vmstate_extioi_pre_save(s);
     g_string_append_printf(buf, "%s:\n", object_get_typename(OBJECT(obj)));
 
-    val = (uint64_t *)s->isr;
-    g_string_append_printf(buf, "  ISR     0x%016"PRIx64" 0x%016"PRIx64
-                                " 0x%016"PRIx64" 0x%016"PRIx64"\n",
+    val = s->isr;
+    g_string_append_printf(buf, "  ISR     0x%08"PRIx32" 0x%08"PRIx32
+                                " 0x%08"PRIx32" 0x%08"PRIx32"\n",
                                 *val, *(val + 1), *(val + 2), *(val + 3));
-    val = (uint64_t *)s->enable;
-    g_string_append_printf(buf, "  ENABLE  0x%016"PRIx64" 0x%016"PRIx64
-                                " 0x%016"PRIx64" 0x%016"PRIx64"\n",
+    val += 4;
+    g_string_append_printf(buf, "          0x%08"PRIx32" 0x%08"PRIx32
+                                " 0x%08"PRIx32" 0x%08"PRIx32"\n",
+                                *val, *(val + 1), *(val + 2), *(val + 3));
+    val = s->enable;
+    g_string_append_printf(buf, "  ENABLE  0x%08"PRIx32" 0x%08"PRIx32
+                                " 0x%08"PRIx32" 0x%08"PRIx32"\n",
+                                *val, *(val + 1), *(val + 2), *(val + 3));
+    val += 4;
+    g_string_append_printf(buf, "          0x%08"PRIx32" 0x%08"PRIx32
+                                " 0x%08"PRIx32" 0x%08"PRIx32"\n",
                                 *val, *(val + 1), *(val + 2), *(val + 3));
-    val = (uint64_t *)s->ipmap;
-    g_string_append_printf(buf, "  IPMAP   0x%016"PRIx64"\n", *val);
 
-    val = (uint64_t *)s->coremap;
-    for (i = 0; i < EXTIOI_IRQS; i += 32) {
-        g_string_append_printf(buf, "  COREMAP 0x%016"PRIx64" 0x%016"PRIx64
-                                    " 0x%016"PRIx64" 0x%016"PRIx64"\n",
-                                    *val, *(val + 1), *(val + 2), *(val + 3));
+    val = s->ipmap;
+    g_string_append_printf(buf, "  IPMAP   0x%08"PRIx32" 0x%08"PRIx32"\n",
+                                *val, *(val + 1));
+
+    val = s->coremap;
+    g_string_append_printf(buf, "  IRQ route:\n");
+    for (i = 0; i < (EXTIOI_IRQS / 4); i += 4) {
+        g_string_append_printf(buf, "    [%-3u - %-3u] 0x%08"PRIx32
+                                    " 0x%08"PRIx32" 0x%08"PRIx32
+                                    " 0x%08"PRIx32"\n",
+                                    i * 4, i * 4 + 15, *val, *(val + 1),
+                                    *(val + 2), *(val + 3));
         val += 4;
     }
 
@@ -464,11 +477,15 @@ static void loongarch_extioi_print_info(InterruptStatsProvider *obj,
             continue;
         }
 
-        val = (uint64_t *)core->coreisr;
-        g_string_append_printf(buf, "  CPU[%d] ISR 0x%016"PRIx64" 0x%016"PRIx64
-                                    " 0x%016"PRIx64" 0x%016"PRIx64"\n",
+        val = core->coreisr;
+        g_string_append_printf(buf, "  CPU[%-3u] ISR 0x%08"PRIx32" 0x%08"PRIx32
+                                    " 0x%08"PRIx32" 0x%08"PRIx32"\n",
                                     i, *val, *(val + 1), *(val + 2),
                                     *(val + 3));
+        val += 4;
+        g_string_append_printf(buf, "              0x%08"PRIx32" 0x%08"PRIx32
+                                    " 0x%08"PRIx32" 0x%08"PRIx32"\n",
+                                    *val, *(val + 1), *(val + 2), *(val + 3));
     }
 }
 
hw/intc/loongarch_ipi.c +5 -4
index c8f775a93e..7efdfc904d 100644
@@ -199,7 +199,7 @@ static void loongarch_ipi_print_info(InterruptStatsProvider *obj, GString *buf)
     LoongsonIPICommonState *lics = LOONGSON_IPI_COMMON(obj);
     int i, j;
     IPICore *core;
-    uint64_t mbx;
+    uint32_t mbx;
     unsigned long pos;
 
     loongarch_ipi_pre_save(lics);
@@ -221,10 +221,11 @@ static void loongarch_ipi_print_info(InterruptStatsProvider *obj, GString *buf)
             continue;
         }
 
-        for (j = 0; j < IPI_MBX_NUM; j++) {
-            mbx = *(uint64_t *)&core->buf[2 * j];
+        for (j = 0; j < (IPI_MBX_NUM * 2); j++) {
+            mbx = core->buf[j];
             if (mbx) {
-                g_string_append_printf(buf, "  MBX[%-1u] 0x%016"PRIx64, j, mbx);
+                g_string_append_printf(buf, "          MBX[%-1u] 0x%08"PRIx32,
+                                            j, mbx);
             }
         }
 
hw/intc/loongarch_pch_pic.c +16 -12
index 34090514ec..555575a377 100644
@@ -335,7 +335,8 @@ static int loongarch_pic_post_load(LoongArchPICCommonState *opaque,
 static void loongarch_pic_print_info(InterruptStatsProvider *obj, GString *buf)
 {
     LoongArchPICCommonState *s = LOONGARCH_PIC_COMMON(obj);
-    uint64_t *vector;
+    uint8_t *vector, val;
+    int i;
 
     loongarch_pic_pre_save(s);
     g_string_append_printf(buf, "%s:\n", object_get_typename(OBJECT(obj)));
@@ -350,17 +351,20 @@ static void loongarch_pic_print_info(InterruptStatsProvider *obj, GString *buf)
                                 s->last_intirr, s->intirr, s->intisr,
                                 s->int_polarity);
 
-    vector = (uint64_t *)s->htmsi_vector;
-    g_string_append_printf(buf, "  MSI map  0x%016"PRIx64" 0x%016"PRIx64
-                                " 0x%016"PRIx64" 0x%016"PRIx64"\n",
-                                *vector, *(vector + 1), *(vector + 2),
-                                *(vector + 3));
-
-    vector += 4;
-    g_string_append_printf(buf, "           0x%016"PRIx64" 0x%016"PRIx64
-                                " 0x%016"PRIx64" 0x%016"PRIx64"\n",
-                                *vector, *(vector + 1), *(vector + 2),
-                                *(vector + 3));
+    vector = s->htmsi_vector;
+    g_string_append_printf(buf, "  MSI map:\n");
+    for (i = 0; i < 64; i += 4, vector += 4) {
+        val = *vector | *(vector + 1) | *(vector + 2) | *(vector + 3);
+        /* skip zero mapping */
+        if (val == 0) {
+            continue;
+        }
+
+        g_string_append_printf(buf, "    [%2d - %2d] 0x%02"PRIx8" 0x%02"PRIx8
+                                    " 0x%02"PRIx8" 0x%02"PRIx8"\n",
+                                    i, i + 3, *vector, *(vector + 1),
+                                    *(vector + 2), *(vector + 3));
+    }
 }
 
 static void loongarch_pic_class_init(ObjectClass *klass, const void *data)
Signature · 1 line
2.54.0
author
#11 lore raw

[PULL 07/10] target/loongarch: Preserve PTW dirty tracking on read fills

In reply to Bibo Mao

with LoongArch hardware page table walking (PTW) enabled, a PTE can be writable (W=1) but still clean (D=0). A load or instruction fetch can install PAGE_WRITE in QEMU's software TLB based on W alone. A later store then bypasses loongarch_cpu_tlb_fill() and PTW, dirty bit of PTE will not be set with the later store operation.

Here remove PAGE_WRITE for clean PTEs on read/fetch fills at the common software TLB fill point. The next store must re-enter PTW to set D before using a writable cached translation.

---
 target/loongarch/tcg/tlb_helper.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)
target/loongarch/tcg/tlb_helper.c +13 -9
index b6381f7cd3..f5b4dc5c58 100644
@@ -651,15 +651,6 @@ bool loongarch_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
             need_update = false;
         } else if (access_type != MMU_DATA_STORE && pte_access(context.pte)) {
             need_update = false;
-
-            /*
-             * FIXME: should context.prot be set without PAGE_WRITE with
-             * pte_write(context.pte) && !pte_dirty(context.pte)??
-             *
-             * Otherwise there will be no loongarch_cpu_tlb_fill() function call
-             * for MMU_DATA_STORE access_type in future since QEMU TLB with
-             * prot PAGE_WRITE is added already
-             */
         }
 
         if (need_update) {
@@ -683,6 +674,19 @@ bool loongarch_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
     if (ret == TLBRET_MATCH) {
         physical = context.physical;
         prot = context.prot;
+
+        /*
+         * Loads or instruction fetches must not cache write access to a
+         * clean PTE. The first store must enter PTW to set D, whether this
+         * translation came from the LoongArch TLB or a page table walk.
+         * Store walks have already handled D, but context.pte may still
+         * contain its old value. Direct mappings have no PTE to check.
+         */
+        if (cpu_has_ptw(env) && context.mmu_index != MMU_DA_IDX &&
+            access_type != MMU_DATA_STORE && !pte_dirty(context.pte)) {
+            prot &= ~PAGE_WRITE;
+        }
+
         tlb_set_page(cs, address & TARGET_PAGE_MASK,
                      physical & TARGET_PAGE_MASK, prot,
                      mmu_idx, TARGET_PAGE_SIZE);
Signature · 1 line
2.54.0
author
#12 lore raw

[PULL 10/10] target/loongarch: Select sys state by VM level

In reply to Bibo Mao

From: SignKirigami <[email protected]>

Define VM_LEVEL0 and VM_LEVELS for the available VM levels. Replace set_sys_state(), which accepted a CPUSysState pointer, with set_sys(), which selects a state by VM level. There is no function change in this patch.

---
 target/loongarch/cpu.c | 4 ++--
 target/loongarch/cpu.h | 7 +++++--
 2 files changed, 7 insertions(+), 4 deletions(-)
target/loongarch/cpu.c +2 -2
index 9cf566f0f6..809f0bb509 100644
@@ -299,7 +299,7 @@ static void loongarch_la464_initfn(Object *obj)
     uint32_t data = 0, field;
     int i;
 
-    set_sys_state(env, &env->sys_states[0]);
+    set_sys(env, VM_LEVEL0);
     for (i = 0; i < 21; i++) {
         env->cpucfg[i] = 0x0;
     }
@@ -429,7 +429,7 @@ static void loongarch_la132_initfn(Object *obj)
     uint32_t data = 0;
     int i;
 
-    set_sys_state(env, &env->sys_states[0]);
+    set_sys(env, VM_LEVEL0);
     for (i = 0; i < 21; i++) {
         env->cpucfg[i] = 0x0;
     }
target/loongarch/cpu.h +5 -2
index 4ab81e0f7b..5cc2370da2 100644
@@ -322,6 +322,9 @@ typedef struct CPUTimerState {
     CPUState *cs;
 } CPUTimerState;
 
+#define VM_LEVEL0   0
+#define VM_LEVELS   1
+
 #define CPU_VENDOR_LOONGSON   "Loongson"
 #define CPU_MODEL_3A5000      "3A5000"
 #define CPU_MODEL_1C101       "1C101"
@@ -510,9 +513,9 @@ static inline CPUSysState *env_sys(CPULoongArchState *env)
     return env->sys_state;
 }
 
-static inline void set_sys_state(CPULoongArchState *env, CPUSysState *sys)
+static inline void set_sys(CPULoongArchState *env, int vm_level)
 {
-    env->sys_state = sys;
+    env->sys_state = &env->sys_states[vm_level];
 }
 
 static inline CPUTimerState *env_timer(CPULoongArchState *env)
Signature · 1 line
2.54.0
#13 lore raw
In reply to Bibo Mao
Quoted 6 lines · Bibo Mao

The following changes since commit ed698be7700160c74d24ef7830adccd2a28d840a:

   Merge tag 'firmware-20261021-pull-request' ofhttps://gitlab.com/kraxel/qemu into staging (2026-10-22 08:20:10 +0200)

are available in the Git repository at:

Quoted 8 lines
   https://github.com/bibo-mao/qemu.git tags/pull-loongarch-20261023

for you to fetch changes up to b8cb88aaa32d8e1ff55c5bc347bae7da09f2e63f:

target/loongarch: Select sys state by VM level (2026-10-23 09:28:51 +0800)

----------------------------------------------------------------
pull-loongarch-20261023 queue

Applied, thanks.

r~