Skip to contentSkip to conversation

add LVZ support for TCG

PATCH Superseded SignKirigami 2 participants 36 messages Oct 9 – Nov 9

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover add LVZ support for TCG

This patch implements Loongson VirtualiZation (LVZ) extension support for LoongArch's TCG target. With this patch, it is now possible to start a nested KVM-accelerated virtual machine on a TCG-emulated virtual machine.

SignKirigami (23):
target/loongarch: select sys state by VM level target/loongarch: add get_sys() helper
target/loongarch: add LVZ feature switcher target/loongarch: add LVZ CSR metadata
target/loongarch: add vm level 1 definition target/loongarch: add definition about guest mmu_idx target/loongarch: add LVZ guest interrupt state target/loongarch: add LVZ guest timer
target/loongarch: handle return to guest in ertn target/loongarch: handle LVZ exception and vm_exit target/loongarch: add host TLBRet and handle target/loongarch: add gid management functions target/loongarch: add LVZ parameters to TLB functions target/loongarch: add LVZ to TLB functions target/loongarch: add LVZ to invtlb
target/loongarch: add LVZ to ptw
target/loongarch: add LVZ migration state target/loongarch: add gcsr instructions and vm level 1 csr management target/loongarch: add LVZ CSRs
target/loongarch: translate LVZ guest TLB instructions target/loongarch: translate LVZ guest traps and HVC target/loongarch: initialize CSR related to LVZ target/loongarch: exit to vm level 0 for fpd, sxd and asxd of vm level

    0
 target/loongarch/cpu-csr.h                    |  38 ++
 target/loongarch/cpu-mmu.h                    |  36 +-
 target/loongarch/cpu.c                        |  87 +++-
 target/loongarch/cpu.h                        | 105 ++++-
 target/loongarch/cpu_helper.c                 |  73 ++--
 target/loongarch/csr.c                        | 102 ++---
 target/loongarch/csr.h                        |   1 +
 target/loongarch/disas.c                      |  15 +
 target/loongarch/insns.decode                 |  14 +
 target/loongarch/internals.h                  |   4 +-
 target/loongarch/machine.c                    | 104 +++++
 target/loongarch/tcg/csr_helper.c             | 131 ++++--
 target/loongarch/tcg/helper.h                 |  49 ++-
 .../tcg/insn_trans/trans_extra.c.inc          |   5 +
 .../tcg/insn_trans/trans_privileged.c.inc     | 290 +++++++++++--
 target/loongarch/tcg/op_helper.c              |  37 ++
 target/loongarch/tcg/tcg_cpu.c                |  94 +++--
 target/loongarch/tcg/tcg_loongarch.h          |   3 +-
 target/loongarch/tcg/tlb_helper.c             | 395 ++++++++++++------
 target/loongarch/tcg/translate.c              |   3 +
 target/loongarch/translate.h                  |   1 +
 21 files changed, 1245 insertions(+), 342 deletions(-)
Signature · 1 line
2.55.0
#5 lore raw

PATCH v5 3/23 target/loongarch: add LVZ feature switcher

Add LVZ property and AVAIL_LVZ macro based on cpucfg.

---
 target/loongarch/cpu.c       | 33 +++++++++++++++++++++++++++++++--
 target/loongarch/cpu.h       |  6 ++++++
 target/loongarch/translate.h |  1 +
 3 files changed, 38 insertions(+), 2 deletions(-)
target/loongarch/cpu.c +31 -2
index e1245235be..664763137d 100644
@@ -267,11 +267,33 @@ static void loongarch_set_ptw(Object *obj, bool value, Error **errp)
     cpu->env.cpucfg[2] = FIELD_DP32(cpu->env.cpucfg[2], CPUCFG2, HPTW, value);
 }
 
+static bool loongarch_get_lvz(Object *obj, Error **errp)
+{
+    return LOONGARCH_CPU(obj)->lvz != ON_OFF_AUTO_OFF;
+}
+
+static void loongarch_set_lvz(Object *obj, bool value, Error **errp)
+{
+    LoongArchCPU *cpu = LOONGARCH_CPU(obj);
+
+    cpu->lvz = value ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
+
+    if (kvm_enabled()) {
+        /* lvz can not be only enabled in kvm mode currently */
+        return;
+    }
+
+    cpu->env.cpucfg[2] = FIELD_DP32(cpu->env.cpucfg[2], CPUCFG2, LVZ, value);
+    cpu->env.cpucfg[2] = FIELD_DP32(cpu->env.cpucfg[2], CPUCFG2, LVZ_VER,
+                                    value ? 1 : 0);
+}
+
 static void loongarch_cpu_post_init(Object *obj)
 {
     LoongArchCPU *cpu = LOONGARCH_CPU(obj);
 
     cpu->lbt = ON_OFF_AUTO_OFF;
+    cpu->lvz = ON_OFF_AUTO_AUTO;
     cpu->pmu = ON_OFF_AUTO_OFF;
     cpu->lsx = ON_OFF_AUTO_AUTO;
     cpu->lasx = ON_OFF_AUTO_AUTO;
@@ -283,6 +305,9 @@ static void loongarch_cpu_post_init(Object *obj)
                              loongarch_set_msgint);
     object_property_add_bool(obj, "ptw", loongarch_get_ptw,
                              loongarch_set_ptw);
+    object_property_add_bool(obj, "lvz", loongarch_get_lvz,
+                             loongarch_set_lvz);
+
     /* lbt is enabled only in kvm mode, not supported in tcg mode */
 
     if (kvm_enabled()) {
@@ -336,8 +361,12 @@ static void loongarch_la464_initfn(Object *obj)
     data = FIELD_DP32(data, CPUCFG2, FP_SP, 1);
     data = FIELD_DP32(data, CPUCFG2, FP_DP, 1);
     data = FIELD_DP32(data, CPUCFG2, FP_VER, 1);
-    data = FIELD_DP32(data, CPUCFG2, LSX, 1),
-    data = FIELD_DP32(data, CPUCFG2, LASX, 1),
+    data = FIELD_DP32(data, CPUCFG2, LSX, 1);
+    data = FIELD_DP32(data, CPUCFG2, LASX, 1);
+    if (tcg_enabled()) {
+        data = FIELD_DP32(data, CPUCFG2, LVZ, 1);
+        data = FIELD_DP32(data, CPUCFG2, LVZ_VER, 1);
+    }
     data = FIELD_DP32(data, CPUCFG2, LLFTP, 1);
     data = FIELD_DP32(data, CPUCFG2, LLFTP_VER, 1);
     data = FIELD_DP32(data, CPUCFG2, LSPW, 1);
target/loongarch/cpu.h +6 -0
index c0edcc9065..918378ee9a 100644
@@ -460,6 +460,7 @@ struct ArchCPU {
     CPULoongArchState env;
     uint32_t  phy_id;
     OnOffAuto lbt;
+    OnOffAuto lvz;
     OnOffAuto pmu;
     OnOffAuto ptw;
     OnOffAuto lsx;
@@ -520,6 +521,11 @@ static inline CPUSysState *get_sys(CPULoongArchState *env, int vm_level)
     return &env->sys_states[vm_level];
 }
 
+static inline bool cpu_has_lvz(CPULoongArchState *env)
+{
+    return FIELD_EX32(env->cpucfg[2], CPUCFG2, LVZ);
+}
+
 static inline CPUTimerState *env_timer(CPULoongArchState *env)
 {
     return &env->sys_states[0].timer_state;
target/loongarch/translate.h +1 -0
index 8aa8325dc6..a7c165982c 100644
@@ -24,6 +24,7 @@
 #define avail_FP(C)     (FIELD_EX32((C)->cpucfg2, CPUCFG2, FP))
 #define avail_FP_SP(C)  (FIELD_EX32((C)->cpucfg2, CPUCFG2, FP_SP))
 #define avail_FP_DP(C)  (FIELD_EX32((C)->cpucfg2, CPUCFG2, FP_DP))
+#define avail_LVZ(C)    (FIELD_EX32((C)->cpucfg2, CPUCFG2, LVZ))
 #define avail_LSPW(C)   (FIELD_EX32((C)->cpucfg2, CPUCFG2, LSPW))
 #define avail_LAM(C)    (FIELD_EX32((C)->cpucfg2, CPUCFG2, LAM))
 #define avail_LAM_BH(C) (FIELD_EX32((C)->cpucfg2, CPUCFG2, LAM_BH))
Signature · 1 line
2.55.0
#3 lore raw

PATCH v5 2/23 target/loongarch: add get_sys() helper

In reply to SignKirigami

Add get_sys() to retrieve a CPUSysState by VM level. Use it to access the host state in CPU reset path to handle possible reset requests under vm_level 1.

---
 target/loongarch/cpu.c | 2 +-
 target/loongarch/cpu.h | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)
target/loongarch/cpu.c +1 -1
index 6c6e1d81b8..e1245235be 100644
@@ -621,7 +621,7 @@ static void loongarch_cpu_reset_hold(Object *obj, ResetType type)
     CPUState *cs = CPU(obj);
     LoongArchCPUClass *lacc = LOONGARCH_CPU_GET_CLASS(obj);
     CPULoongArchState *env = cpu_env(cs);
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, VM_LEVEL0);
 
     if (lacc->parent_phases.hold) {
         lacc->parent_phases.hold(obj, type);
target/loongarch/cpu.h +5 -0
index 391445f614..c0edcc9065 100644
@@ -515,6 +515,11 @@ static inline void set_sys(CPULoongArchState *env, int vm_level)
     env->sys_state = &env->sys_states[vm_level];
 }
 
+static inline CPUSysState *get_sys(CPULoongArchState *env, int vm_level)
+{
+    return &env->sys_states[vm_level];
+}
+
 static inline CPUTimerState *env_timer(CPULoongArchState *env)
 {
     return &env->sys_states[0].timer_state;
Signature · 1 line
2.55.0
#8 lore raw

PATCH v5 5/23 target/loongarch: add vm level 1 definition

In reply to SignKirigami

Add VM_LEVEL1 macro, env_vm_level() and trigger_vm_exit() functions, preparing for later interrupt virtualization.

---
 target/loongarch/cpu.h         | 24 ++++++++++++++++++++++--
 target/loongarch/tcg/tcg_cpu.c |  3 +++
 2 files changed, 25 insertions(+), 2 deletions(-)
target/loongarch/cpu.h +22 -2
index 0b7d63d8f3..183210efd8 100644
@@ -323,7 +323,8 @@ typedef struct CPUTimerState {
 } CPUTimerState;
 
 #define VM_LEVEL0   0
-#define VM_LEVELS   1
+#define VM_LEVEL1   1
+#define VM_LEVELS   2
 
 #define CPU_VENDOR_LOONGSON   "Loongson"
 #define CPU_MODEL_3A5000      "3A5000"
@@ -434,7 +435,7 @@ typedef struct CPUArchState {
     /* Fields up to this point are cleared by a CPU reset */
     struct {} end_reset_fields;
 
-    CPUSysState sys_states[1];
+    CPUSysState sys_states[VM_LEVELS];
     uint32_t cpucfg[21];
     uint32_t pv_features;
     uint64_t vendor_id;
@@ -446,6 +447,7 @@ typedef struct CPUArchState {
     uint32_t mp_state;
 #endif
     CPUSysState *sys_state;
+    bool vm_exit;
 } CPULoongArchState;
 
 typedef struct LoongArchCPUTopo {
@@ -517,6 +519,11 @@ static inline CPUSysState *env_sys(CPULoongArchState *env)
     return env->sys_state;
 }
 
+static inline int env_vm_level(CPULoongArchState *env)
+{
+    return env_sys(env) - env->sys_states;
+}
+
 static inline void set_sys(CPULoongArchState *env, int vm_level)
 {
     env->sys_state = &env->sys_states[vm_level];
@@ -532,6 +539,18 @@ static inline bool cpu_has_lvz(CPULoongArchState *env)
     return FIELD_EX32(env->cpucfg[2], CPUCFG2, LVZ);
 }
 
+static inline void trigger_vm_exit(CPULoongArchState *env)
+{
+    CPUSysState *host = get_sys(env, VM_LEVEL0);
+
+    if (env_vm_level(env) != VM_LEVEL1) {
+        return;
+    }
+
+    host->CSR_GSTAT = FIELD_DP64(host->CSR_GSTAT, CSR_GSTAT, PVM, 1);
+    env->vm_exit = true;
+}
+
 static inline CPUTimerState *env_timer(CPULoongArchState *env)
 {
     return &env->sys_states[0].timer_state;
@@ -572,6 +591,7 @@ static inline void set_pc(CPULoongArchState *env, uint64_t value)
 #define HW_FLAGS_CRMD_PG    R_CSR_CRMD_PG_MASK   /* 0x10 */
 #define HW_FLAGS_VA32       0x20
 #define HW_FLAGS_EUEN_ASXE  0x40
+#define HW_FLAGS_GUEST_MODE 0x80
 
 #define CPU_RESOLVING_TYPE TYPE_LOONGARCH_CPU
 
target/loongarch/tcg/tcg_cpu.c +3 -0
index 4b1d44a164..14fa5f5809 100644
@@ -279,6 +279,9 @@ static TCGTBCPUState loongarch_get_tb_cpu_state(CPUState *cs)
     uint32_t flags;
 
     flags = sys->CSR_CRMD & (R_CSR_CRMD_PLV_MASK | R_CSR_CRMD_PG_MASK);
+    if (env_vm_level(env) == VM_LEVEL1) {
+        flags |= HW_FLAGS_GUEST_MODE;
+    }
     flags |= FIELD_EX64(sys->CSR_EUEN, CSR_EUEN, FPE) * HW_FLAGS_EUEN_FPE;
     flags |= FIELD_EX64(sys->CSR_EUEN, CSR_EUEN, SXE) * HW_FLAGS_EUEN_SXE;
     flags |= FIELD_EX64(sys->CSR_EUEN, CSR_EUEN, ASXE) * HW_FLAGS_EUEN_ASXE;
Signature · 1 line
2.55.0
#1 lore raw

PATCH v5 1/23 target/loongarch: select sys state by VM level

In reply to SignKirigami

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 54c2699897..6c6e1d81b8 100644
@@ -298,7 +298,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;
     }
@@ -428,7 +428,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 c592f69885..391445f614 100644
@@ -321,6 +321,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"
@@ -507,9 +510,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.55.0
#11 lore raw

PATCH v5 7/23 target/loongarch: add LVZ guest interrupt state

In reply to SignKirigami

Route configured hardware interrupts into the guest CSR bank, track guest interrupt requests separately, and expose guest pending and enable checks to the TCG runtime.

---
 target/loongarch/cpu.c            | 35 +++++++++++++++++++--------
 target/loongarch/cpu.h            |  3 +++
 target/loongarch/internals.h      |  4 ++--
 target/loongarch/tcg/csr_helper.c |  2 +-
 target/loongarch/tcg/tcg_cpu.c    | 39 ++++++++++++++++++++++---------
 5 files changed, 59 insertions(+), 24 deletions(-)
target/loongarch/cpu.c +25 -10
index 664763137d..5cef63a778 100644
@@ -58,16 +58,20 @@ static vaddr loongarch_cpu_get_pc(CPUState *cs)
 #ifndef CONFIG_USER_ONLY
 #include "hw/loongarch/virt.h"
 
-void loongarch_cpu_update_irq(LoongArchCPU *cpu)
+void loongarch_cpu_update_irq(LoongArchCPU *cpu, int vm_level)
 {
     CPULoongArchState *env = &cpu->env;
     CPUState *cs = CPU(cpu);
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
+    int interrupt = vm_level ? CPU_INTERRUPT_GUEST : CPU_INTERRUPT_HARD;
 
+    if (vm_level == VM_LEVEL1 && env_vm_level(env) != VM_LEVEL1) {
+        return;
+    }
     if (FIELD_EX64(sys->CSR_ESTAT, CSR_ESTAT, IS)) {
-        cpu_interrupt(cs, CPU_INTERRUPT_HARD);
+        cpu_interrupt(cs, interrupt);
     } else {
-        cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
+        cpu_reset_interrupt(cs, interrupt);
     }
 }
 
@@ -75,21 +79,28 @@ static void loongarch_cpu_self_set_irq(CPUState *cs, run_on_cpu_data data)
 {
     LoongArchCPU *cpu = LOONGARCH_CPU(cs);
     CPULoongArchState *env = cpu_env(cs);
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys;
+    int vm_level = 0;
     int irq, level;
 
     irq = data.host_int & ~BIT(31);
     level = (data.host_int >> 31) & 1;
+    if (irq >= N_IRQS) {
+        irq -= N_IRQS;
+        vm_level = 1;
+    }
+    sys = get_sys(env, vm_level);
     sys->CSR_ESTAT = deposit64(sys->CSR_ESTAT, irq, 1, level != 0);
-    loongarch_cpu_update_irq(cpu);
+    loongarch_cpu_update_irq(cpu, vm_level);
 }
 
 void loongarch_cpu_set_irq(void *opaque, int irq, int level)
 {
     LoongArchCPU *cpu = opaque;
     CPUState *cs = CPU(cpu);
+    CPULoongArchState *env = cpu_env(cs);
 
-    if (irq < 0 || irq >= N_IRQS) {
+    if (irq < 0 || irq >= (cpu_has_lvz(env) ? N_VIRQS : N_IRQS)) {
         return;
     }
 
@@ -103,11 +114,11 @@ void loongarch_cpu_set_irq(void *opaque, int irq, int level)
 }
 
 /* Check if there is pending and not masked out interrupt */
-bool cpu_loongarch_hw_interrupts_pending(CPULoongArchState *env)
+bool cpu_loongarch_hw_interrupts_pending(CPULoongArchState *env, int vm_level)
 {
     uint32_t pending;
     uint32_t status;
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
 
     pending = FIELD_EX64(sys->CSR_ESTAT, CSR_ESTAT, IS);
     status  = FIELD_EX64(sys->CSR_ECFG, CSR_ECFG, LIE);
@@ -122,7 +133,11 @@ bool loongarch_cpu_has_work(CPUState *cs)
     bool has_work = false;
 
     if (cpu_test_interrupt(cs, CPU_INTERRUPT_HARD) &&
-        cpu_loongarch_hw_interrupts_pending(cpu_env(cs))) {
+        cpu_loongarch_hw_interrupts_pending(cpu_env(cs), VM_LEVEL0)) {
+        has_work = true;
+    }
+    if (cpu_test_interrupt(cs, CPU_INTERRUPT_GUEST) &&
+        cpu_loongarch_hw_interrupts_pending(cpu_env(cs), VM_LEVEL1)) {
         has_work = true;
     }
 
target/loongarch/cpu.h +3 -0
index 9eea1badaf..8939dbc748 100644
@@ -20,6 +20,8 @@
 #include "cpu-csr.h"
 #include "cpu-qom.h"
 
+#define CPU_INTERRUPT_GUEST CPU_INTERRUPT_TGT_EXT_0
+
 #define FCSR0_M1    0x1f         /* FCSR1 mask, Enables */
 #define FCSR0_M2    0x1f1f0000   /* FCSR2 mask, Cause and Flags */
 #define FCSR0_M3    0x300        /* FCSR3 mask, Round Mode */
@@ -242,6 +244,7 @@ extern const char * const fregnames[32];
 #define IRQ_TIMER   11
 #define IRQ_IPI     12
 #define INT_DMSI    14
+#define N_VIRQS     (N_IRQS * 2)
 
 #define MAX_PERF_EVENTS        16
 #define LOONGARCH_STLB         2048 /* 2048 STLB */
target/loongarch/internals.h +2 -2
index ff956823fd..c11cefc4ee 100644
@@ -31,7 +31,7 @@ void restore_fp_status(CPULoongArchState *env);
 #ifndef CONFIG_USER_ONLY
 extern const VMStateDescription vmstate_loongarch_cpu;
 
-void loongarch_cpu_update_irq(LoongArchCPU *cpu);
+void loongarch_cpu_update_irq(LoongArchCPU *cpu, int vm_level);
 void loongarch_cpu_set_irq(void *opaque, int irq, int level);
 
 void cpu_loongarch_timer_cb(void *opaque);
@@ -39,7 +39,7 @@ uint64_t cpu_loongarch_get_timer_counter(CPUTimerState *timer);
 uint64_t cpu_loongarch_get_timer_ticks(CPUTimerState *timer);
 void cpu_loongarch_set_timer_config(CPUTimerState *timer, uint64_t value);
 bool loongarch_cpu_has_work(CPUState *cs);
-bool cpu_loongarch_hw_interrupts_pending(CPULoongArchState *env);
+bool cpu_loongarch_hw_interrupts_pending(CPULoongArchState *env, int vm_level);
 #endif /* !CONFIG_USER_ONLY */
 
 uint64_t read_fcc(CPULoongArchState *env);
target/loongarch/tcg/csr_helper.c +1 -1
index 1a6919417f..ecfba63228 100644
@@ -111,7 +111,7 @@ target_ulong helper_csrwr_estat(CPULoongArchState *env, target_ulong val)
      */
     if (sys->CSR_ESTAT != old_v) {
         bql_lock();
-        loongarch_cpu_update_irq(env_archcpu(env));
+        loongarch_cpu_update_irq(env_archcpu(env), false);
         bql_unlock();
     }
 
target/loongarch/tcg/tcg_cpu.c +28 -11
index b60d9e7cf8..101fe6fc7a 100644
@@ -238,30 +238,47 @@ static void loongarch_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
     }
 }
 
-static inline bool cpu_loongarch_hw_interrupts_enabled(CPULoongArchState *env)
+static inline bool cpu_loongarch_hw_interrupts_enabled(CPULoongArchState *env, int vm_level)
 {
     bool ret = 0;
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
+    CPUSysState *host = get_sys(env, VM_LEVEL0);
 
     ret = (FIELD_EX64(sys->CSR_CRMD, CSR_CRMD, IE) &&
-          !(FIELD_EX64(sys->CSR_DBG, CSR_DBG, DST)));
+          !(FIELD_EX64(host->CSR_DBG, CSR_DBG, DST)));
 
     return ret;
 }
 
 static bool loongarch_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
 {
+    CPULoongArchState *env = cpu_env(cs);
+    bool has_interrupt = false;
+
     if (interrupt_request & CPU_INTERRUPT_HARD) {
-        CPULoongArchState *env = cpu_env(cs);
-
-        if (cpu_loongarch_hw_interrupts_enabled(env) &&
-            cpu_loongarch_hw_interrupts_pending(env)) {
-            /* Raise it */
-            cs->exception_index = EXCCODE_INT;
-            loongarch_cpu_do_interrupt(cs);
-            return true;
+
+        if (cpu_loongarch_hw_interrupts_enabled(env, VM_LEVEL0) &&
+            cpu_loongarch_hw_interrupts_pending(env, VM_LEVEL0)) {
+            if (env_vm_level(env) != VM_LEVEL0) {
+                trigger_vm_exit(env);
+            }
+            has_interrupt = true;
         }
+    } else if (interrupt_request & CPU_INTERRUPT_GUEST) {
+        if (cpu_loongarch_hw_interrupts_enabled(env, VM_LEVEL1) &&
+            cpu_loongarch_hw_interrupts_pending(env, VM_LEVEL1) &&
+            env_vm_level(env) == VM_LEVEL1) {
+            has_interrupt = true;
+        }
+    }
+
+    if (has_interrupt) {
+        /* Raise it */
+        cs->exception_index = EXCCODE_INT;
+        loongarch_cpu_do_interrupt(cs);
+        return true;
     }
+
     return false;
 }
 
Signature · 1 line
2.55.0
#18 lore raw

PATCH v5 11/23 target/loongarch: add host TLBRet and handle

In reply to SignKirigami

Add host value in enum TLBRet, and use it in raise_mmu_exception().

---
 target/loongarch/cpu-mmu.h        |  8 ++++++++
 target/loongarch/tcg/tlb_helper.c | 18 ++++++++++++++++--
 2 files changed, 24 insertions(+), 2 deletions(-)
target/loongarch/cpu-mmu.h +8 -0
index 54fb732d62..2b46e481bd 100644
@@ -17,6 +17,14 @@ typedef enum TLBRet {
     TLBRET_RI,
     TLBRET_XI,
     TLBRET_PE,
+    TLBRET_HOST_MATCH,
+    TLBRET_HOST_BADADDR,
+    TLBRET_HOST_NOMATCH,
+    TLBRET_HOST_INVALID,
+    TLBRET_HOST_DIRTY,
+    TLBRET_HOST_RI,
+    TLBRET_HOST_XI,
+    TLBRET_HOST_PE,
 } TLBRet;
 
 typedef struct MMUContext {
target/loongarch/tcg/tlb_helper.c +16 -2
index 40cc076424..d1f1d233fd 100644
@@ -49,15 +49,24 @@ static void raise_mmu_exception(CPULoongArchState *env, vaddr address,
                                 MMUAccessType access_type, TLBRet tlb_error)
 {
     CPUState *cs = env_cpu(env);
-    CPUSysState *sys = env_sys(env);
+    bool real_guest;
+
+    if (env_vm_level(env) == VM_LEVEL1 && tlb_error > TLBRET_HOST_MATCH) {
+        trigger_vm_exit(env);
+    }
+
+    real_guest = !env->vm_exit && env_vm_level(env);
+    CPUSysState *sys = get_sys(env, real_guest);
 
     switch (tlb_error) {
     default:
     case TLBRET_BADADDR:
+    case TLBRET_HOST_BADADDR:
         cs->exception_index = access_type == MMU_INST_FETCH
                               ? EXCCODE_ADEF : EXCCODE_ADEM;
         break;
     case TLBRET_NOMATCH:
+    case TLBRET_HOST_NOMATCH:
         /* No TLB match for a mapped address */
         if (access_type == MMU_DATA_LOAD) {
             cs->exception_index = EXCCODE_PIL;
@@ -69,6 +78,7 @@ static void raise_mmu_exception(CPULoongArchState *env, vaddr address,
         sys->CSR_TLBRERA = FIELD_DP64(sys->CSR_TLBRERA, CSR_TLBRERA, ISTLBR, 1);
         break;
     case TLBRET_INVALID:
+    case TLBRET_HOST_INVALID:
         /* TLB match with no valid bit */
         if (access_type == MMU_DATA_LOAD) {
             cs->exception_index = EXCCODE_PIL;
@@ -79,24 +89,28 @@ static void raise_mmu_exception(CPULoongArchState *env, vaddr address,
         }
         break;
     case TLBRET_DIRTY:
+    case TLBRET_HOST_DIRTY:
         /* TLB match but 'D' bit is cleared */
         cs->exception_index = EXCCODE_PME;
         break;
     case TLBRET_XI:
+    case TLBRET_HOST_XI:
         /* Execute-Inhibit Exception */
         cs->exception_index = EXCCODE_PNX;
         break;
     case TLBRET_RI:
+    case TLBRET_HOST_RI:
         /* Read-Inhibit Exception */
         cs->exception_index = EXCCODE_PNR;
         break;
     case TLBRET_PE:
+    case TLBRET_HOST_PE:
         /* Privileged Exception */
         cs->exception_index = EXCCODE_PPI;
         break;
     }
 
-    if (tlb_error == TLBRET_NOMATCH) {
+    if (tlb_error == TLBRET_NOMATCH || tlb_error == TLBRET_HOST_NOMATCH) {
         sys->CSR_TLBRBADV = address;
         if (is_la64(env)) {
             sys->CSR_TLBREHI = FIELD_DP64(sys->CSR_TLBREHI, CSR_TLBREHI_64,
Signature · 1 line
2.55.0
#16 lore raw

PATCH v5 9/23 target/loongarch: handle return to guest in ertn

In reply to SignKirigami

Add will_return_to_guest() function, and handle process of return to guest at end of helper_ertn().

---
 target/loongarch/cpu.h           |  8 ++++++++
 target/loongarch/tcg/op_helper.c | 14 ++++++++++++++
 2 files changed, 22 insertions(+)
target/loongarch/cpu.h +8 -0
index ba28d80272..891eea47bd 100644
@@ -573,6 +573,14 @@ static inline void trigger_vm_exit(CPULoongArchState *env)
     env->vm_exit = true;
 }
 
+static inline bool will_return_to_guest(CPULoongArchState *env)
+{
+    if (!cpu_has_lvz(env) || env_vm_level(env) == VM_LEVEL1) {
+        return false;
+    }
+    return FIELD_EX64(get_sys(env, VM_LEVEL0)->CSR_GSTAT, CSR_GSTAT, PVM);
+}
+
 static inline CPUTimerState *env_timer(CPULoongArchState *env)
 {
     return &env->sys_states[0].timer_state;
target/loongarch/tcg/op_helper.c +14 -0
index f98fe4d4a1..3035dfa08d 100644
@@ -12,6 +12,7 @@
 #include "exec/helper-proto.h"
 #include "accel/tcg/cpu-ldst.h"
 #include "internals.h"
+#include "qemu/main-loop.h"
 #include "qemu/crc32c.h"
 #include <zlib.h> /* for crc32 */
 #include "cpu-csr.h"
@@ -90,6 +91,8 @@ void helper_ertn(CPULoongArchState *env)
 {
     uint64_t csr_pplv, csr_pie;
     CPUSysState *sys = env_sys(env);
+    CPUSysState *host = get_sys(env, VM_LEVEL0);
+    CPUSysState *guest = get_sys(env, VM_LEVEL1);
 
     if (FIELD_EX64(sys->CSR_TLBRERA, CSR_TLBRERA, ISTLBR)) {
         csr_pplv = FIELD_EX64(sys->CSR_TLBRPRMD, CSR_TLBRPRMD, PPLV);
@@ -113,6 +116,17 @@ void helper_ertn(CPULoongArchState *env)
     sys->CSR_CRMD = FIELD_DP64(sys->CSR_CRMD, CSR_CRMD, IE, csr_pie);
 
     env->lladdr = 1;
+    if (will_return_to_guest(env)) {
+        host->CSR_GSTAT = FIELD_DP64(host->CSR_GSTAT, CSR_GSTAT, VM, 1);
+        set_sys(env, VM_LEVEL1);
+        bql_lock();
+        if (FIELD_EX64(guest->CSR_ESTAT, CSR_ESTAT, IS)) {
+            cpu_interrupt(env_cpu(env), CPU_INTERRUPT_GUEST);
+        } else {
+            cpu_reset_interrupt(env_cpu(env), CPU_INTERRUPT_GUEST);
+        }
+        bql_unlock();
+    }
 }
 
 void helper_idle(CPULoongArchState *env)
Signature · 1 line
2.55.0
#14 lore raw

PATCH v5 8/23 target/loongarch: add LVZ guest timer

In reply to SignKirigami

Add a separate guest timer and IRQ_VTIMER.

---
 target/loongarch/cpu.c | 8 +++++++-
 target/loongarch/cpu.h | 6 ++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
target/loongarch/cpu.c +7 -1
index 5cef63a778..8e759a2987 100644
@@ -770,7 +770,7 @@ static void loongarch_cpu_init(Object *obj)
     LoongArchCPU *cpu = LOONGARCH_CPU(obj);
 #ifdef CONFIG_TCG
     CPULoongArchState *env = &cpu->env;
-    CPUTimerState *timer;
+    CPUTimerState *timer, *guest_timer;
 #endif
 
     qdev_init_gpio_in(DEVICE(cpu), loongarch_cpu_set_irq, N_IRQS);
@@ -778,8 +778,14 @@ static void loongarch_cpu_init(Object *obj)
     timer = env_timer(env);
     timer->irq = IRQ_TIMER;
     timer->cs  = CPU(obj);
+    guest_timer = env_guest_timer(env);
+    guest_timer->irq = IRQ_VTIMER;
+    guest_timer->cs = CPU(obj);
+
     timer_init_ns(&timer->timer, QEMU_CLOCK_VIRTUAL,
                   &cpu_loongarch_timer_cb, timer);
+    timer_init_ns(&guest_timer->timer, QEMU_CLOCK_VIRTUAL,
+                  &cpu_loongarch_timer_cb, guest_timer);
 #endif
 #endif
 }
target/loongarch/cpu.h +6 -0
index 8939dbc748..ba28d80272 100644
@@ -245,6 +245,7 @@ extern const char * const fregnames[32];
 #define IRQ_IPI     12
 #define INT_DMSI    14
 #define N_VIRQS     (N_IRQS * 2)
+#define IRQ_VTIMER  (N_IRQS + IRQ_TIMER)
 
 #define MAX_PERF_EVENTS        16
 #define LOONGARCH_STLB         2048 /* 2048 STLB */
@@ -577,6 +578,11 @@ static inline CPUTimerState *env_timer(CPULoongArchState *env)
     return &env->sys_states[0].timer_state;
 }
 
+static inline CPUTimerState *env_guest_timer(CPULoongArchState *env)
+{
+    return &env->sys_states[1].timer_state;
+}
+
 static inline bool is_la64(CPULoongArchState *env)
 {
     return FIELD_EX32(env->cpucfg[1], CPUCFG1, ARCH) == CPUCFG1_ARCH_LA64;
Signature · 1 line
2.55.0
#9 lore raw

PATCH v5 6/23 target/loongarch: add definition about guest mmu_idx

In reply to SignKirigami

Add MMU_GUEST_IDX and MMU_GUEST_DA_IDX. Provide is_guest_mmu idx() and mmu_idx_to_plv to calculate plv with new mmu_idx.

---
 target/loongarch/cpu.h                         | 18 ++++++++++++++++++
 .../tcg/insn_trans/trans_privileged.c.inc      |  2 +-
 target/loongarch/tcg/tcg_cpu.c                 |  7 +++++++
 target/loongarch/tcg/translate.c               |  3 +++
 4 files changed, 29 insertions(+), 1 deletion(-)
target/loongarch/cpu.h +18 -0
index 183210efd8..9eea1badaf 100644
@@ -513,6 +513,24 @@ struct LoongArchCPUClass {
 #define MMU_KERNEL_IDX   MMU_PLV_KERNEL
 #define MMU_USER_IDX     MMU_PLV_USER
 #define MMU_DA_IDX       4
+#define MMU_GUEST_IDX    5
+#define MMU_GUEST_DA_IDX 9
+
+static inline bool is_guest_mmu_idx(int mmu_idx)
+{
+    return mmu_idx >= MMU_GUEST_IDX;
+}
+
+static inline int mmu_idx_to_plv(int mmu_idx)
+{
+    if (mmu_idx == MMU_DA_IDX || mmu_idx == MMU_GUEST_DA_IDX) {
+        return 0;
+    }
+    if (is_guest_mmu_idx(mmu_idx)) {
+        return mmu_idx - MMU_GUEST_IDX;
+    }
+    return mmu_idx;
+}
 
 static inline CPUSysState *env_sys(CPULoongArchState *env)
 {
target/loongarch/tcg/insn_trans/trans_privileged.c.inc +1 -1
index 6728ce5ec9..7ee3bb8e27 100644
@@ -249,7 +249,7 @@ TRANS64(iocsrwr_d, IOCSR, gen_iocsrwr, gen_helper_iocsrwr_d)
 
 static void check_mmu_idx(DisasContext *ctx)
 {
-    if (ctx->mem_idx != MMU_DA_IDX) {
+    if (ctx->mem_idx != MMU_DA_IDX && ctx->mem_idx != MMU_GUEST_DA_IDX) {
         tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next + 4);
         ctx->base.is_jmp = DISAS_EXIT;
     }
target/loongarch/tcg/tcg_cpu.c +7 -0
index 14fa5f5809..b60d9e7cf8 100644
@@ -309,6 +309,13 @@ static int loongarch_cpu_mmu_index(CPUState *cs, bool ifetch)
     CPULoongArchState *env = cpu_env(cs);
     CPUSysState *sys = env_sys(env);
 
+    if (env_vm_level(env) == VM_LEVEL1) {
+        if (FIELD_EX64(sys->CSR_CRMD, CSR_CRMD, PG)) {
+            return MMU_GUEST_IDX + FIELD_EX64(sys->CSR_CRMD, CSR_CRMD, PLV);
+        }
+        return MMU_GUEST_DA_IDX;
+    }
+
     if (FIELD_EX64(sys->CSR_CRMD, CSR_CRMD, PG)) {
         return FIELD_EX64(sys->CSR_CRMD, CSR_CRMD, PLV);
     }
target/loongarch/tcg/translate.c +3 -0
index 124dce6269..4ab06b48da 100644
@@ -129,6 +129,9 @@ static void loongarch_tr_init_disas_context(DisasContextBase *dcbase,
     } else {
         ctx->mem_idx = MMU_DA_IDX;
     }
+    if (ctx->base.tb->flags & HW_FLAGS_GUEST_MODE) {
+        ctx->mem_idx += MMU_GUEST_IDX;
+    }
 
     /* Bound the number of insns to execute to those left on the page.  */
     bound = -(ctx->base.pc_first | TARGET_PAGE_MASK) / 4;
Signature · 1 line
2.55.0
#34 lore raw

PATCH v5 22/23 target/loongarch: initialize CSR related to LVZ

In reply to SignKirigami

Initialize CSR.GSTAT.GIDBITS and GCSR.ASID.ASIDBITS at cpu init function.

---
 target/loongarch/cpu.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
target/loongarch/cpu.c +4 -1
index 8e759a2987..1522e64b9c 100644
@@ -334,7 +334,7 @@ static void loongarch_la464_initfn(Object *obj)
 {
     LoongArchCPU *cpu = LOONGARCH_CPU(obj);
     CPULoongArchState *env = &cpu->env;
-    CPUSysState *sys;
+    CPUSysState *sys, *guest;
     uint32_t data = 0, field;
     int i;
 
@@ -448,6 +448,9 @@ static void loongarch_la464_initfn(Object *obj)
 
     sys = env_sys(env);
     sys->CSR_ASID = FIELD_DP64(0, CSR_ASID, ASIDBITS, 0xa);
+    sys->CSR_GSTAT = FIELD_DP64(0, CSR_GSTAT, GIDBITS, 8);
+    guest = get_sys(env, VM_LEVEL1);
+    guest->CSR_ASID = FIELD_DP64(0, CSR_ASID, ASIDBITS, 0xa);
 
     sys->CSR_PRCFG1 = FIELD_DP64(sys->CSR_PRCFG1, CSR_PRCFG1, SAVE_NUM, 8);
     sys->CSR_PRCFG1 = FIELD_DP64(sys->CSR_PRCFG1, CSR_PRCFG1, TIMER_BITS, 0x2f);
Signature · 1 line
2.55.0
#33 lore raw

PATCH v5 21/23 target/loongarch: translate LVZ guest traps and HVC

In reply to SignKirigami

Raise GSPR when a guest accesses IOCSR, debug, idle and cpucfg resources, and decode HVC for guest PLV0.

---
 target/loongarch/disas.c                      |  1 +
 target/loongarch/insns.decode                 |  1 +
 target/loongarch/tcg/helper.h                 |  2 +-
 .../tcg/insn_trans/trans_extra.c.inc          |  5 +++
 .../tcg/insn_trans/trans_privileged.c.inc     | 32 +++++++++++++++++++
 target/loongarch/tcg/op_helper.c              |  3 ++
 6 files changed, 43 insertions(+), 1 deletion(-)
target/loongarch/disas.c +1 -0
index 6425dea17c..7d47fc0151 100644
@@ -706,6 +706,7 @@ INSN(gtlbsrch,     empty)
 INSN(gtlbrd,       empty)
 INSN(gtlbwr,       empty)
 INSN(gtlbfill,     empty)
+INSN(hvcl,         i)
 INSN(cacop,        cop_r_i)
 INSN(lddir,        rr_i)
 INSN(ldpte,        j_i)
target/loongarch/insns.decode +1 -0
index cbcc30c31f..b40fabe9d7 100644
@@ -505,6 +505,7 @@ gtlbsrch         0000 01100100 10000 01010 00000 00001    @empty
 gtlbrd           0000 01100100 10000 01011 00000 00001    @empty
 gtlbwr           0000 01100100 10000 01100 00000 00001    @empty
 gtlbfill         0000 01100100 10000 01101 00000 00001    @empty
+hvcl             0000 0000 0010 1011 1 ...............    @i15
 
 iocsrrd_b        0000 01100100 10000 00000 ..... .....    @rr
 iocsrrd_h        0000 01100100 10000 00001 ..... .....    @rr
target/loongarch/tcg/helper.h +1 -1
index 84eaee208c..be1573af9d 100644
@@ -10,7 +10,7 @@ DEF_HELPER_FLAGS_3(asrtgt_d, TCG_CALL_NO_WG, void, env, tl, tl)
 
 DEF_HELPER_FLAGS_3(crc32, TCG_CALL_NO_RWG_SE, tl, tl, tl, tl)
 DEF_HELPER_FLAGS_3(crc32c, TCG_CALL_NO_RWG_SE, tl, tl, tl, tl)
-DEF_HELPER_FLAGS_2(cpucfg, TCG_CALL_NO_RWG_SE, tl, env, tl)
+DEF_HELPER_FLAGS_2(cpucfg, TCG_CALL_NO_WG_SE, tl, env, tl)
 
 /* Floating-point helper */
 DEF_HELPER_FLAGS_3(fadd_s, TCG_CALL_NO_WG, i64, env, i64, i64)
target/loongarch/tcg/insn_trans/trans_extra.c.inc +5 -0
index 655dce329e..275755a1f0 100644
@@ -82,6 +82,11 @@ static bool trans_rdtime_d(DisasContext *ctx, arg_rdtime_d *a)
 
 static bool trans_cpucfg(DisasContext *ctx, arg_cpucfg *a)
 {
+    if (is_guest_mmu_idx(ctx->mem_idx)) {
+        generate_exception(ctx, EXCCODE_GSPR);
+        return true;
+    }
+
     TCGv dest = gpr_dst(ctx, a->rd, EXT_NONE);
     TCGv src1 = gpr_src(ctx, a->rj, EXT_NONE);
 
target/loongarch/tcg/insn_trans/trans_privileged.c.inc +32 -0
index ae481aa83f..bd50e91422 100644
@@ -48,6 +48,7 @@ GEN_FALSE_TRANS(gtlbsrch)
 GEN_FALSE_TRANS(gtlbrd)
 GEN_FALSE_TRANS(gtlbwr)
 GEN_FALSE_TRANS(gtlbfill)
+GEN_FALSE_TRANS(hvcl)
 
 #else
 
@@ -301,6 +302,10 @@ static bool gen_iocsrrd(DisasContext *ctx, arg_rr *a,
     if (check_plv(ctx)) {
         return false;
     }
+    if (is_guest_mmu_idx(ctx->mem_idx)) {
+        generate_exception(ctx, EXCCODE_GSPR);
+        return true;
+    }
     func(dest, tcg_env, src1);
     return true;
 }
@@ -314,6 +319,10 @@ static bool gen_iocsrwr(DisasContext *ctx, arg_rr *a,
     if (check_plv(ctx)) {
         return false;
     }
+    if (is_guest_mmu_idx(ctx->mem_idx)) {
+        generate_exception(ctx, EXCCODE_GSPR);
+        return true;
+    }
     func(tcg_env, addr, val);
     return true;
 }
@@ -584,6 +593,10 @@ static bool trans_dbcl(DisasContext *ctx, arg_dbcl *a)
     if (check_plv(ctx)) {
         return false;
     }
+    if (is_guest_mmu_idx(ctx->mem_idx)) {
+        generate_exception(ctx, EXCCODE_GSPR);
+        return true;
+    }
     generate_exception(ctx, EXCCODE_DBP);
     return true;
 }
@@ -594,9 +607,28 @@ static bool trans_idle(DisasContext *ctx, arg_idle *a)
         return false;
     }
 
+    if (is_guest_mmu_idx(ctx->mem_idx)) {
+        generate_exception(ctx, EXCCODE_GSPR);
+        return true;
+    }
+
     tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next + 4);
     gen_helper_idle(tcg_env);
     ctx->base.is_jmp = DISAS_NORETURN;
     return true;
 }
+
+static bool trans_hvcl(DisasContext *ctx, arg_hvcl *a)
+{
+    if (!avail_LVZ(ctx) || !is_guest_mmu_idx(ctx->mem_idx)) {
+        return false;
+    }
+
+    if (check_plv(ctx)) {
+        return false;
+    }
+
+    generate_exception(ctx, EXCCODE_HVC);
+    return true;
+}
 #endif
target/loongarch/tcg/op_helper.c +3 -0
index 3035dfa08d..e2fe4b212f 100644
@@ -20,6 +20,9 @@
 /* Exceptions helpers */
 void helper_raise_exception(CPULoongArchState *env, uint32_t exception)
 {
+    if (exception == EXCCODE_GSPR || exception == EXCCODE_HVC) {
+        trigger_vm_exit(env);
+    }
     do_raise_exception(env, exception, GETPC());
 }
 
Signature · 1 line
2.55.0
#17 lore raw

PATCH v5 10/23 target/loongarch: handle LVZ exception and vm_exit

In reply to SignKirigami

Add EXCCODE_GSPR and EXCCODE_HVC macros. Execute real vm exit at end of loongarch_cpu_do_interrupt().

---
 target/loongarch/cpu.h         |  2 ++
 target/loongarch/tcg/tcg_cpu.c | 28 ++++++++++++++++++++--------
 2 files changed, 22 insertions(+), 8 deletions(-)
target/loongarch/cpu.h +2 -0
index 891eea47bd..3e3ae2bfa7 100644
@@ -95,6 +95,8 @@ FIELD(FCSR0, CAUSE, 24, 5)
 #define  EXCCODE_WPEM                EXCODE(19, 1)
 #define  EXCCODE_BTD                 EXCODE(20, 0)
 #define  EXCCODE_BTE                 EXCODE(21, 0)
+#define  EXCCODE_GSPR                EXCODE(22, 0)
+#define  EXCCODE_HVC                 EXCODE(23, 0)
 #define  EXCCODE_DBP                 EXCODE(26, 0) /* Reserved subcode used for debug */
 
 /* cpucfg[0] bits */
target/loongarch/tcg/tcg_cpu.c +20 -8
index 101fe6fc7a..6f1ba12edb 100644
@@ -44,6 +44,8 @@ static const struct TypeExcp excp_names[] = {
     {EXCCODE_BCE, "Bound Check Exception"},
     {EXCCODE_SXD, "128 bit vector instructions Disable exception"},
     {EXCCODE_ASXD, "256 bit vector instructions Disable exception"},
+    {EXCCODE_GSPR, "Guest Sensitive and Privileged Resources"},
+    {EXCCODE_HVC, "Hypervisor call"},
     {EXCP_HLT, "EXCP_HLT"},
 };
 
@@ -78,9 +80,11 @@ void G_NORETURN do_raise_exception(CPULoongArchState *env,
 static void loongarch_cpu_do_interrupt(CPUState *cs)
 {
     CPULoongArchState *env = cpu_env(cs);
-    CPUSysState *sys = env_sys(env);
     bool update_badinstr = 1;
     int cause = -1;
+    bool real_guest = !env->vm_exit && env_vm_level(env);
+    CPUSysState *sys = get_sys(env, real_guest);
+    CPUSysState *host = get_sys(env, VM_LEVEL0);
     bool tlbfill = FIELD_EX64(sys->CSR_TLBRERA, CSR_TLBRERA, ISTLBR);
     uint32_t vec_size = FIELD_EX64(sys->CSR_ECFG, CSR_ECFG, VS);
     uint64_t last_pc = env->pc;
@@ -96,17 +100,17 @@ static void loongarch_cpu_do_interrupt(CPUState *cs)
 
     switch (cs->exception_index) {
     case EXCCODE_DBP:
-        sys->CSR_DBG = FIELD_DP64(sys->CSR_DBG, CSR_DBG, DCL, 1);
-        sys->CSR_DBG = FIELD_DP64(sys->CSR_DBG, CSR_DBG, ECODE, 0xC);
+        host->CSR_DBG = FIELD_DP64(sys->CSR_DBG, CSR_DBG, DCL, 1);
+        host->CSR_DBG = FIELD_DP64(sys->CSR_DBG, CSR_DBG, ECODE, 0xC);
         goto set_DERA;
     set_DERA:
-        sys->CSR_DERA = env->pc;
-        sys->CSR_DBG = FIELD_DP64(sys->CSR_DBG, CSR_DBG, DST, 1);
-        set_pc(env, sys->CSR_EENTRY + 0x480);
+        host->CSR_DERA = env->pc;
+        host->CSR_DBG = FIELD_DP64(sys->CSR_DBG, CSR_DBG, DST, 1);
+        set_pc(env, host->CSR_EENTRY + 0x480);
         break;
     case EXCCODE_INT:
-        if (FIELD_EX64(sys->CSR_DBG, CSR_DBG, DST)) {
-            sys->CSR_DBG = FIELD_DP64(sys->CSR_DBG, CSR_DBG, DEI, 1);
+        if (FIELD_EX64(host->CSR_DBG, CSR_DBG, DST)) {
+            host->CSR_DBG = FIELD_DP64(host->CSR_DBG, CSR_DBG, DEI, 1);
             goto set_DERA;
         }
         QEMU_FALLTHROUGH;
@@ -117,6 +121,8 @@ static void loongarch_cpu_do_interrupt(CPUState *cs)
         update_badinstr = 0;
         break;
     case EXCCODE_BCE:
+    case EXCCODE_GSPR:
+    case EXCCODE_HVC:
         sys->CSR_BADV = env->pc;
         QEMU_FALLTHROUGH;
     case EXCCODE_SYS:
@@ -218,6 +224,12 @@ static void loongarch_cpu_do_interrupt(CPUState *cs)
         qemu_plugin_vcpu_exception_cb(cs, last_pc);
     }
     cs->exception_index = -1;
+    if (env->vm_exit) {
+        host->CSR_GSTAT = FIELD_DP64(host->CSR_GSTAT, CSR_GSTAT, VM, 0);
+        set_sys(env, VM_LEVEL0);
+        cpu_reset_interrupt(cs, CPU_INTERRUPT_GUEST);
+    }
+    env->vm_exit = false;
 }
 
 static void loongarch_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
Signature · 1 line
2.55.0
#26 lore raw

PATCH v5 16/23 target/loongarch: add LVZ to ptw

In reply to SignKirigami

Add function loongarch_try_ptw() and use it in both vm level 0 and 1 address mapping.

---
 target/loongarch/cpu-mmu.h        |  2 +-
 target/loongarch/cpu_helper.c     | 23 ++++++---
 target/loongarch/tcg/tlb_helper.c | 82 +++++++++++++++++--------------
 3 files changed, 64 insertions(+), 43 deletions(-)
target/loongarch/cpu-mmu.h +1 -1
index 29b0c109fb..bad05a5a9c 100644
@@ -40,7 +40,7 @@ typedef struct MMUContext {
 
 static inline bool cpu_has_ptw(CPULoongArchState *env, int vm_level)
 {
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
 
     return !!FIELD_EX64(sys->CSR_PWCH, CSR_PWCH, HPTW_EN);
 }
target/loongarch/cpu_helper.c +17 -6
index 23d26385c6..4ba991aaff 100644
@@ -160,7 +160,7 @@ TLBRet loongarch_ptw(CPULoongArchState *env, MMUContext *context,
     vaddr address;
     TLBRet ret;
     MemTxResult ret1;
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
 
     address = context->addr;
     if ((address >> 63) & 0x1) {
@@ -180,7 +180,10 @@ 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 = address_space_ldq_le(
+            cs->as,
+            vm_level ? loongarch_get_host_address(env, phys, retaddr) : phys,
+            attrs, NULL);
         if (level) {
             if (FIELD_EX64(base, TLBENTRY, HUGE)) {
                 /* base is a huge pte */
@@ -214,8 +217,11 @@ restart:
 
         index &= 1;
         context->pte_buddy[index] = base;
-        val = address_space_ldq_le(cs->as, phys + 8 * (1 - 2 * index),
-                                   attrs, NULL);
+        val = address_space_ldq_le(
+            cs->as,
+            (vm_level ? loongarch_get_host_address(env, phys, retaddr) : phys) +
+                8 * (1 - 2 * index),
+            attrs, NULL);
         context->pte_buddy[1 - index] = val;
     }
 
@@ -245,10 +251,15 @@ restart:
         if (access_type == MMU_DATA_STORE) {
             base = pte_mkdirty(base);
         }
-        ret1 = loongarch_cmpxchg_phys(cs, phys, pte, base);
+        ret1 = loongarch_cmpxchg_phys(
+            cs, vm_level ? loongarch_get_host_address(env, phys, retaddr) : phys,
+            pte, base);
         /* PTE updated by other CPU, reload PTE entry */
         if (ret1 == MEMTX_DECODE_ERROR) {
-            base = address_space_ldq_le(cs->as, phys, attrs, NULL);
+            base = address_space_ldq_le(
+                cs->as,
+                vm_level ? loongarch_get_host_address(env, phys, retaddr) : phys,
+                attrs, NULL);
             goto restart;
         }
 
target/loongarch/tcg/tlb_helper.c +46 -36

106 lines not on this page · raw message

Signature · 1 line
2.55.0
#7 lore raw

PATCH v5 4/23 target/loongarch: add LVZ CSR metadata

In reply to SignKirigami

Add LVZ host and guest CSR access metadata to the existing CSR table. Mark guest read-only and unavailable CSRs for later LVZ instruction translation.

---
 target/loongarch/cpu-csr.h |  38 ++++++++++++++
 target/loongarch/cpu.h     |   6 +++
 target/loongarch/csr.c     | 102 ++++++++++++++++++++-----------------
 target/loongarch/csr.h     |   1 +
 target/loongarch/disas.c   |   5 ++
 5 files changed, 104 insertions(+), 48 deletions(-)
target/loongarch/cpu-csr.h +38 -0
index d860417af2..e0061e57c3 100644
@@ -224,4 +224,42 @@ FIELD(CSR_DBG, ECODE, 16, 6)
 #define LOONGARCH_CSR_DERA           0x501 /* Debug era */
 #define LOONGARCH_CSR_DSAVE          0x502 /* Debug save */
 
+/* LVZ (LoongArch Virtualization) CSRs */
+#define LOONGARCH_CSR_GTLBC          0x15 /* Guest TLB control */
+FIELD(CSR_GTLBC, GMTLBSZ, 0, 6)
+FIELD(CSR_GTLBC, USETGID, 12, 1)
+FIELD(CSR_GTLBC, TOTI, 13, 1)
+FIELD(CSR_GTLBC, TGID, 16, 8)
+
+#define LOONGARCH_CSR_GSTAT          0x50 /* Guest status */
+FIELD(CSR_GSTAT, VM, 0, 1)
+FIELD(CSR_GSTAT, PVM, 1, 1)
+FIELD(CSR_GSTAT, GIDBITS, 4, 6)
+FIELD(CSR_GSTAT, GID, 16, 8)
+
+#define LOONGARCH_CSR_GCFG           0x51 /* Guest config */
+FIELD(CSR_GCFG, MATP, 0, 4)
+FIELD(CSR_GCFG, MATC, 4, 2)
+FIELD(CSR_GCFG, TOPIP, 6, 1)
+FIELD(CSR_GCFG, TOPI, 7, 1)
+FIELD(CSR_GCFG, TOTIP, 8, 1)
+FIELD(CSR_GCFG, TOTI, 9, 1)
+FIELD(CSR_GCFG, TOEP, 10, 1)
+FIELD(CSR_GCFG, TOE, 11, 1)
+FIELD(CSR_GCFG, TOPP, 12, 1)
+FIELD(CSR_GCFG, TOP, 13, 1)
+FIELD(CSR_GCFG, TOHUP, 14, 1)
+FIELD(CSR_GCFG, TOHU, 15, 1)
+FIELD(CSR_GCFG, TOCIP, 16, 4)
+FIELD(CSR_GCFG, TOCI, 20, 2)
+FIELD(CSR_GCFG, GPMP, 23, 1)
+FIELD(CSR_GCFG, GPMNUM, 24, 3)
+
+#define LOONGARCH_CSR_GINTC          0x52 /* Guest interrupt config */
+FIELD(CSR_GINTC, HWIS, 0, 8)
+FIELD(CSR_GINTC, HWIP, 8, 8)
+FIELD(CSR_GINTC, HWIC, 16, 8)
+
+#define LOONGARCH_CSR_GCNTC          0x53 /* Guest counter compensation */
+
 #endif /* LOONGARCH_CPU_CSR_H */
target/loongarch/cpu.h +6 -0
index 918378ee9a..0b7d63d8f3 100644
@@ -255,6 +255,7 @@ FIELD(TLB_MISC, E, 0, 1)
 FIELD(TLB_MISC, ASID, 1, 10)
 FIELD(TLB_MISC, VPPN, 13, 35)
 FIELD(TLB_MISC, PS, 48, 6)
+FIELD(TLB_MISC, GID, 54, 8)
 
 /*Msg interrupt registers */
 #define N_MSGIS                4
@@ -381,6 +382,10 @@ typedef struct CPUSysState {
     uint64_t CSR_DBG;
     uint64_t CSR_DERA;
     uint64_t CSR_DSAVE;
+    uint64_t CSR_GCFG;
+    uint64_t CSR_GINTC;
+    uint64_t CSR_GCNTC;
+    uint64_t CSR_GTLBC;
     /* Msg interrupt registers */
     uint64_t CSR_MSGIS[N_MSGIS];
     uint64_t CSR_MSGIR;
@@ -389,6 +394,7 @@ typedef struct CPUSysState {
     /* Fields up to this point are cleared by a CPU reset */
     struct {} end_reset_fields;
 
+    uint64_t CSR_GSTAT;
     uint64_t CSR_ASID;
     uint64_t CSR_PRCFG1;
     uint64_t CSR_PRCFG2;
target/loongarch/csr.c +54 -48

124 lines not on this page · raw message

target/loongarch/csr.h +1 -0
index c2b6b882bc..393ac09b61 100644
@@ -20,6 +20,7 @@ enum {
     CSRFL_IO       = (1 << 2),
     CSRFL_UNUSED   = (1 << 3),
     CSRFL_BASIC    = (1 << 4),
+    CSRFL_GSPR     = (1 << 5),
 };
 
 typedef struct {
target/loongarch/disas.c +5 -0
index 928b8b94c2..03d1e560dd 100644
@@ -87,6 +87,11 @@ static const char * const csr_names[] = {
     CSR_NAME(TVAL),
     CSR_NAME(CNTC),
     CSR_NAME(TICLR),
+    CSR_NAME(GTLBC),
+    CSR_NAME(GSTAT),
+    CSR_NAME(GCFG),
+    CSR_NAME(GINTC),
+    CSR_NAME(GCNTC),
     CSR_NAME(LLBCTL),
     CSR_NAME(IMPCTL1),
     CSR_NAME(IMPCTL2),
Signature · 1 line
2.55.0
#30 lore raw

PATCH v5 19/23 target/loongarch: add LVZ CSRs

In reply to SignKirigami

Add gstat, gtlbc and gintc definitions and corresponding helpers.

---
 target/loongarch/tcg/csr_helper.c             | 53 +++++++++++++++++++
 target/loongarch/tcg/helper.h                 |  3 ++
 .../tcg/insn_trans/trans_privileged.c.inc     |  3 ++
 3 files changed, 59 insertions(+)
target/loongarch/tcg/csr_helper.c +53 -0
index 7ef0032f5d..f113097046 100644
@@ -217,3 +217,56 @@ target_ulong helper_csrwr_pwch(CPULoongArchState *env,
     sys->CSR_PWCH = val;
     return old_v;
 }
+
+target_ulong helper_csrwr_gstat(CPULoongArchState *env,
+                                target_ulong val, uint32_t vm_level)
+{
+    CPUSysState *sys = get_sys(env, VM_LEVEL0);
+    int64_t old_v = sys->CSR_GSTAT;
+    uint8_t old_gid = FIELD_EX64(sys->CSR_GSTAT, CSR_GSTAT, GID);
+
+    sys->CSR_GSTAT = FIELD_DP64(sys->CSR_GSTAT, CSR_GSTAT, PVM,
+                                 FIELD_EX64(val, CSR_GSTAT, PVM));
+    sys->CSR_GSTAT = FIELD_DP64(sys->CSR_GSTAT, CSR_GSTAT, GID,
+                                 FIELD_EX64(val, CSR_GSTAT, GID));
+
+    if (old_gid != FIELD_EX64(sys->CSR_GSTAT, CSR_GSTAT, GID)) {
+        tlb_flush(env_cpu(env));
+    }
+
+    return old_v;
+}
+
+target_ulong helper_csrwr_gtlbc(CPULoongArchState *env,
+                                target_ulong val, uint32_t vm_level)
+{
+    CPUSysState *sys = get_sys(env, VM_LEVEL0);
+    int64_t old_v = sys->CSR_GTLBC;
+    uint8_t old_use_tgid = FIELD_EX64(old_v, CSR_GTLBC, USETGID);
+    uint8_t old_tgid = FIELD_EX64(old_v, CSR_GTLBC, TGID);
+
+    sys->CSR_GTLBC = val;
+    if (old_use_tgid != FIELD_EX64(sys->CSR_GTLBC, CSR_GTLBC, USETGID) ||
+        old_tgid != FIELD_EX64(sys->CSR_GTLBC, CSR_GTLBC, TGID)) {
+        tlb_flush(env_cpu(env));
+    }
+
+    return old_v;
+}
+
+target_ulong helper_csrwr_gintc(CPULoongArchState *env,
+                                target_ulong val, uint32_t vm_level)
+{
+    CPUSysState *host = get_sys(env, VM_LEVEL0);
+    CPUSysState *guest = get_sys(env, VM_LEVEL1);
+    int64_t old_v = host->CSR_GINTC;
+    uint8_t hwis = FIELD_EX64(val, CSR_GINTC, HWIS);
+    /* TODO: hwip and hwic support */
+
+    host->CSR_GINTC = val & 0xffff00;
+    guest->CSR_ESTAT = deposit64(guest->CSR_ESTAT, 2, 8,
+                                 hwis);
+
+    return old_v;
+}
+
target/loongarch/tcg/helper.h +3 -0
index bce5f46dc2..84eaee208c 100644
@@ -104,6 +104,9 @@ DEF_HELPER_3(csrwr_tcfg, i64, env, tl, i32)
 DEF_HELPER_3(csrwr_ticlr, i64, env, tl, i32)
 DEF_HELPER_3(csrwr_pwcl, i64, env, tl, i32)
 DEF_HELPER_3(csrwr_pwch, i64, env, tl, i32)
+DEF_HELPER_3(csrwr_gstat, i64, env, tl, i32)
+DEF_HELPER_3(csrwr_gtlbc, i64, env, tl, i32)
+DEF_HELPER_3(csrwr_gintc, i64, env, tl, i32)
 DEF_HELPER_2(iocsrrd_b, i64, env, tl)
 DEF_HELPER_2(iocsrrd_h, i64, env, tl)
 DEF_HELPER_2(iocsrrd_w, i64, env, tl)
target/loongarch/tcg/insn_trans/trans_privileged.c.inc +3 -0
index 139dbd6e2a..37b7ef13dc 100644
@@ -88,6 +88,9 @@ void loongarch_csr_translate_init(void)
     SET_CSR_FUNC(TVAL,  gen_helper_csrrd_tval, NULL);
     SET_CSR_FUNC(TICLR, NULL, gen_helper_csrwr_ticlr);
     SET_CSR_FUNC(MSGIR, gen_helper_csrrd_msgir, NULL);
+    SET_CSR_FUNC(GSTAT, NULL, gen_helper_csrwr_gstat);
+    SET_CSR_FUNC(GTLBC, NULL, gen_helper_csrwr_gtlbc);
+    SET_CSR_FUNC(GINTC, NULL, gen_helper_csrwr_gintc);
 }
 #undef SET_CSR_FUNC
 
Signature · 1 line
2.55.0
#31 lore raw

PATCH v5 20/23 target/loongarch: translate LVZ guest TLB instructions

In reply to SignKirigami

Decode, disassemble and translate GTLBSRCH, GTLBRD, GTLBWR, GTLBFILL, GTLBCLR and GTLBFLUSH for host LVZ execution.

---
 target/loongarch/disas.c                      |  6 ++
 target/loongarch/insns.decode                 |  7 +++
 .../tcg/insn_trans/trans_privileged.c.inc     | 60 +++++++++++++++++++
 3 files changed, 73 insertions(+)
target/loongarch/disas.c +6 -0
index 2436c9eec3..6425dea17c 100644
@@ -700,6 +700,12 @@ INSN(tlbfill,      empty)
 INSN(tlbclr,       empty)
 INSN(tlbflush,     empty)
 INSN(invtlb,       i_rr)
+INSN(gtlbclr,      empty)
+INSN(gtlbflush,    empty)
+INSN(gtlbsrch,     empty)
+INSN(gtlbrd,       empty)
+INSN(gtlbwr,       empty)
+INSN(gtlbfill,     empty)
 INSN(cacop,        cop_r_i)
 INSN(lddir,        rr_i)
 INSN(ldpte,        j_i)
target/loongarch/insns.decode +7 -0
index 4612af9433..cbcc30c31f 100644
@@ -499,6 +499,13 @@ bgeu            0110 11 ................ ..... .....     @rr_offs16
   gcsrxchg          0000 0101 .............. ..... .....     @rr_csr
 }
 
+gtlbclr          0000 01100100 10000 01000 00000 00001    @empty
+gtlbflush        0000 01100100 10000 01001 00000 00001    @empty
+gtlbsrch         0000 01100100 10000 01010 00000 00001    @empty
+gtlbrd           0000 01100100 10000 01011 00000 00001    @empty
+gtlbwr           0000 01100100 10000 01100 00000 00001    @empty
+gtlbfill         0000 01100100 10000 01101 00000 00001    @empty
+
 iocsrrd_b        0000 01100100 10000 00000 ..... .....    @rr
 iocsrrd_h        0000 01100100 10000 00001 ..... .....    @rr
 iocsrrd_w        0000 01100100 10000 00010 ..... .....    @rr
target/loongarch/tcg/insn_trans/trans_privileged.c.inc +60 -0

78 lines not on this page · raw message

Signature · 1 line
2.55.0
#27 lore raw

PATCH v5 17/23 target/loongarch: add LVZ migration state

In reply to SignKirigami

Migrate the host LVZ controls, guest CSR and TLB banks, and restore the active VM level, guest timer and interrupt request after loading state.

---
 target/loongarch/machine.c | 104 +++++++++++++++++++++++++++++++++++++
 1 file changed, 104 insertions(+)
target/loongarch/machine.c +104 -0
index 931a5ca5ba..7c14f7d92e 100644
@@ -7,6 +7,7 @@
 
 #include "qemu/osdep.h"
 #include "cpu.h"
+#include "internals.h"
 #include "migration/vmstate.h"
 #include "system/tcg.h"
 #include "vec.h"
@@ -205,13 +206,115 @@ static const VMStateDescription vmstate_tlb = {
         VMSTATE_END_OF_LIST()
     }
 };
+
+static const VMStateDescription vmstate_lvz_sys = {
+    .name = "cpu/lvz-sys",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (const VMStateField[]) {
+        VMSTATE_UINT64(CSR_CRMD, CPUSysState),
+        VMSTATE_UINT64(CSR_PRMD, CPUSysState),
+        VMSTATE_UINT64(CSR_EUEN, CPUSysState),
+        VMSTATE_UINT64(CSR_MISC, CPUSysState),
+        VMSTATE_UINT64(CSR_ECFG, CPUSysState),
+        VMSTATE_UINT64(CSR_ESTAT, CPUSysState),
+        VMSTATE_UINT64(CSR_ERA, CPUSysState),
+        VMSTATE_UINT64(CSR_BADV, CPUSysState),
+        VMSTATE_UINT64(CSR_BADI, CPUSysState),
+        VMSTATE_UINT64(CSR_EENTRY, CPUSysState),
+        VMSTATE_UINT64(CSR_TLBIDX, CPUSysState),
+        VMSTATE_UINT64(CSR_TLBEHI, CPUSysState),
+        VMSTATE_UINT64(CSR_TLBELO0, CPUSysState),
+        VMSTATE_UINT64(CSR_TLBELO1, CPUSysState),
+        VMSTATE_UINT64(CSR_ASID, CPUSysState),
+        VMSTATE_UINT64(CSR_PGDL, CPUSysState),
+        VMSTATE_UINT64(CSR_PGDH, CPUSysState),
+        VMSTATE_UINT64(CSR_PGD, CPUSysState),
+        VMSTATE_UINT64(CSR_PWCL, CPUSysState),
+        VMSTATE_UINT64(CSR_PWCH, CPUSysState),
+        VMSTATE_UINT64(CSR_STLBPS, CPUSysState),
+        VMSTATE_UINT64(CSR_RVACFG, CPUSysState),
+        VMSTATE_UINT64(CSR_CPUID, CPUSysState),
+        VMSTATE_UINT64(CSR_PRCFG1, CPUSysState),
+        VMSTATE_UINT64(CSR_PRCFG2, CPUSysState),
+        VMSTATE_UINT64(CSR_PRCFG3, CPUSysState),
+        VMSTATE_UINT64_ARRAY(CSR_SAVE, CPUSysState, 16),
+        VMSTATE_UINT64(CSR_TID, CPUSysState),
+        VMSTATE_UINT64(CSR_TCFG, CPUSysState),
+        VMSTATE_UINT64(CSR_TVAL, CPUSysState),
+        VMSTATE_UINT64(CSR_CNTC, CPUSysState),
+        VMSTATE_UINT64(CSR_TICLR, CPUSysState),
+        VMSTATE_UINT64(CSR_LLBCTL, CPUSysState),
+        VMSTATE_UINT64(CSR_IMPCTL1, CPUSysState),
+        VMSTATE_UINT64(CSR_IMPCTL2, CPUSysState),
+        VMSTATE_UINT64(CSR_TLBRENTRY, CPUSysState),
+        VMSTATE_UINT64(CSR_TLBRBADV, CPUSysState),
+        VMSTATE_UINT64(CSR_TLBRERA, CPUSysState),
+        VMSTATE_UINT64(CSR_TLBRSAVE, CPUSysState),
+        VMSTATE_UINT64(CSR_TLBRELO0, CPUSysState),
+        VMSTATE_UINT64(CSR_TLBRELO1, CPUSysState),
+        VMSTATE_UINT64(CSR_TLBREHI, CPUSysState),
+        VMSTATE_UINT64(CSR_TLBRPRMD, CPUSysState),
+        VMSTATE_UINT64(CSR_MERRCTL, CPUSysState),
+        VMSTATE_UINT64(CSR_MERRINFO1, CPUSysState),
+        VMSTATE_UINT64(CSR_MERRINFO2, CPUSysState),
+        VMSTATE_UINT64(CSR_MERRENTRY, CPUSysState),
+        VMSTATE_UINT64(CSR_MERRERA, CPUSysState),
+        VMSTATE_UINT64(CSR_MERRSAVE, CPUSysState),
+        VMSTATE_UINT64(CSR_CTAG, CPUSysState),
+        VMSTATE_UINT64_ARRAY(CSR_DMW, CPUSysState, 4),
+        VMSTATE_UINT64(CSR_DBG, CPUSysState),
+        VMSTATE_UINT64(CSR_DERA, CPUSysState),
+        VMSTATE_UINT64(CSR_DSAVE, CPUSysState),
+        VMSTATE_UINT64(CSR_GSTAT, CPUSysState),
+        VMSTATE_UINT64(CSR_GCFG, CPUSysState),
+        VMSTATE_UINT64(CSR_GINTC, CPUSysState),
+        VMSTATE_UINT64(CSR_GCNTC, CPUSysState),
+        VMSTATE_UINT64(CSR_GTLBC, CPUSysState),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
+static bool lvz_needed(void *opaque)
+{
+    LoongArchCPU *cpu = opaque;
+    return FIELD_EX64(cpu->env.cpucfg[2], CPUCFG2, LVZ);
+}
+
+static const VMStateDescription vmstate_lvz = {
+    .name = "cpu/lvz",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = lvz_needed,
+    .fields = (const VMStateField[]) {
+        VMSTATE_UINT64(env.sys_states[0].CSR_GSTAT, LoongArchCPU),
+        VMSTATE_UINT64(env.sys_states[0].CSR_GCFG, LoongArchCPU),
+        VMSTATE_UINT64(env.sys_states[0].CSR_GINTC, LoongArchCPU),
+        VMSTATE_UINT64(env.sys_states[0].CSR_GCNTC, LoongArchCPU),
+        VMSTATE_UINT64(env.sys_states[0].CSR_GTLBC, LoongArchCPU),
+        VMSTATE_STRUCT(env.sys_states[1], LoongArchCPU, 0,
+                       vmstate_lvz_sys, CPUSysState),
+        VMSTATE_END_OF_LIST()
+    },
+};
 #endif
 
+static int loongarch_cpu_post_load(void *opaque, int version_id)
+{
+    LoongArchCPU *cpu = opaque;
+    CPULoongArchState *env = &cpu->env;
+    CPUSysState *host = get_sys(env, VM_LEVEL0);
+    bool guest = FIELD_EX64(host->CSR_GSTAT, CSR_GSTAT, VM);
+    set_sys(env, guest);
+    return 0;
+}
+
 /* LoongArch CPU state */
 const VMStateDescription vmstate_loongarch_cpu = {
     .name = "cpu",
     .version_id = 4,
     .minimum_version_id = 4,
+    .post_load = loongarch_cpu_post_load,
     .fields = (const VMStateField[]) {
         VMSTATE_UINT64_ARRAY(env.gpr, LoongArchCPU, 32),
         VMSTATE_UINT64(env.pc, LoongArchCPU),
@@ -285,6 +388,7 @@ const VMStateDescription vmstate_loongarch_cpu = {
         &vmstate_lasx,
 #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
         &vmstate_tlb,
+        &vmstate_lvz,
 #endif
         &vmstate_lbt,
         &vmstate_msgint,
Signature · 1 line
2.55.0
#28 lore raw

PATCH v5 18/23 target/loongarch: add gcsr instructions and vm level 1 csr management

In reply to SignKirigami

Add gcsrrd, gcsrwr and gcsrxchg. Make all csr helper be used by both vm level 0 and 1.

---
 target/loongarch/disas.c                      |   3 +
 target/loongarch/insns.decode                 |   6 +
 target/loongarch/tcg/csr_helper.c             |  78 +++++++-----
 target/loongarch/tcg/helper.h                 |  22 ++--
 .../tcg/insn_trans/trans_privileged.c.inc     | 111 ++++++++++++++----
 5 files changed, 161 insertions(+), 59 deletions(-)
target/loongarch/disas.c +3 -0
index 03d1e560dd..2436c9eec3 100644
@@ -682,6 +682,9 @@ INSN(bgeu,         rr_offs)
 INSN(csrrd,        r_csr)
 INSN(csrwr,        r_csr)
 INSN(csrxchg,      rr_csr)
+INSN(gcsrrd,        r_csr)
+INSN(gcsrwr,        r_csr)
+INSN(gcsrxchg,      rr_csr)
 INSN(iocsrrd_b,    rr)
 INSN(iocsrrd_h,    rr)
 INSN(iocsrrd_w,    rr)
target/loongarch/insns.decode +6 -0
index 3089d42044..4612af9433 100644
@@ -493,6 +493,12 @@ bgeu            0110 11 ................ ..... .....     @rr_offs16
   csrxchg           0000 0100 .............. ..... .....     @rr_csr
 }
 
+{
+  gcsrrd            0000 0101 .............. 00000 .....     @r_csr
+  gcsrwr            0000 0101 .............. 00001 .....     @r_csr
+  gcsrxchg          0000 0101 .............. ..... .....     @rr_csr
+}
+
 iocsrrd_b        0000 01100100 10000 00000 ..... .....    @rr
 iocsrrd_h        0000 01100100 10000 00001 ..... .....    @rr
 iocsrrd_w        0000 01100100 10000 00010 ..... .....    @rr
target/loongarch/tcg/csr_helper.c +50 -28

169 lines not on this page · raw message

target/loongarch/tcg/helper.h +11 -11
index a27b80b087..bce5f46dc2 100644
@@ -93,17 +93,17 @@ DEF_HELPER_1(rdtime_d, i64, env)
 
 #ifndef CONFIG_USER_ONLY
 /* CSRs helper */
-DEF_HELPER_1(csrrd_pgd, i64, env)
-DEF_HELPER_1(csrrd_cpuid, i64, env)
-DEF_HELPER_1(csrrd_tval, i64, env)
-DEF_HELPER_1(csrrd_msgir, i64, env)
-DEF_HELPER_2(csrwr_stlbps, i64, env, tl)
-DEF_HELPER_2(csrwr_estat, i64, env, tl)
-DEF_HELPER_2(csrwr_asid, i64, env, tl)
-DEF_HELPER_2(csrwr_tcfg, i64, env, tl)
-DEF_HELPER_2(csrwr_ticlr, i64, env, tl)
-DEF_HELPER_2(csrwr_pwcl, i64, env, tl)
-DEF_HELPER_2(csrwr_pwch, i64, env, tl)
+DEF_HELPER_2(csrrd_pgd, i64, env, i32)
+DEF_HELPER_2(csrrd_cpuid, i64, env, i32)
+DEF_HELPER_2(csrrd_tval, i64, env, i32)
+DEF_HELPER_2(csrrd_msgir, i64, env, i32)
+DEF_HELPER_3(csrwr_stlbps, i64, env, tl, i32)
+DEF_HELPER_3(csrwr_estat, i64, env, tl, i32)
+DEF_HELPER_3(csrwr_asid, i64, env, tl, i32)
+DEF_HELPER_3(csrwr_tcfg, i64, env, tl, i32)
+DEF_HELPER_3(csrwr_ticlr, i64, env, tl, i32)
+DEF_HELPER_3(csrwr_pwcl, i64, env, tl, i32)
+DEF_HELPER_3(csrwr_pwch, i64, env, tl, i32)
 DEF_HELPER_2(iocsrrd_b, i64, env, tl)
 DEF_HELPER_2(iocsrrd_h, i64, env, tl)
 DEF_HELPER_2(iocsrrd_w, i64, env, tl)
target/loongarch/tcg/insn_trans/trans_privileged.c.inc +91 -20

196 lines not on this page · raw message

Signature · 1 line
2.55.0
#21 lore raw

PATCH v5 13/23 target/loongarch: add LVZ parameters to TLB functions

In reply to SignKirigami

Add parameters related to LVZ (mainly vm_level) to statements and definitions of TLB, so that LVZ patches to TLB can be divided into parts later.

---
 target/loongarch/cpu-mmu.h                    |  24 ++--
 target/loongarch/cpu_helper.c                 |  40 +++---
 target/loongarch/tcg/helper.h                 |  22 +--
 .../tcg/insn_trans/trans_privileged.c.inc     |  48 ++++---
 target/loongarch/tcg/tcg_loongarch.h          |   3 +-
 target/loongarch/tcg/tlb_helper.c             | 133 ++++++++++--------
 6 files changed, 153 insertions(+), 117 deletions(-)
target/loongarch/cpu-mmu.h +15 -9
index 2b46e481bd..4d76d96ee5 100644
@@ -38,18 +38,19 @@ typedef struct MMUContext {
     uint64_t      pte_buddy[2];
 } MMUContext;
 
-static inline bool cpu_has_ptw(CPULoongArchState *env)
+static inline bool cpu_has_ptw(CPULoongArchState *env, int vm_level)
 {
     CPUSysState *sys = env_sys(env);
 
     return !!FIELD_EX64(sys->CSR_PWCH, CSR_PWCH, HPTW_EN);
 }
 
-static inline bool pte_present(CPULoongArchState *env, uint64_t entry)
+static inline bool pte_present(CPULoongArchState *env, uint64_t entry,
+                               int vm_level)
 {
     uint8_t present;
 
-    if (cpu_has_ptw(env)) {
+    if (cpu_has_ptw(env, vm_level)) {
         present = FIELD_EX64(entry, TLBENTRY, P);
     } else {
         present = FIELD_EX64(entry, TLBENTRY, V);
@@ -58,11 +59,12 @@ static inline bool pte_present(CPULoongArchState *env, uint64_t entry)
     return !!present;
 }
 
-static inline bool pte_write(CPULoongArchState *env, uint64_t entry)
+static inline bool pte_write(CPULoongArchState *env, uint64_t entry,
+                             int vm_level)
 {
     uint8_t writable;
 
-    if (cpu_has_ptw(env)) {
+    if (cpu_has_ptw(env, vm_level)) {
         writable = FIELD_EX64(entry, TLBENTRY, W);
     } else {
         writable = FIELD_EX64(entry, TLBENTRY, D);
@@ -99,14 +101,18 @@ static inline bool pte_dirty(uint64_t entry)
 
 bool check_ps(CPULoongArchState *ent, uint8_t ps);
 TLBRet loongarch_check_pte(CPULoongArchState *env, MMUContext *context,
-                           MMUAccessType access_type, int mmu_idx);
+                           MMUAccessType access_type, int mmu_idx, int vm_level);
 TLBRet get_physical_address(CPULoongArchState *env, MMUContext *context,
                             MMUAccessType access_type, int mmu_idx,
-                            int is_debug);
+                            int is_debug, uintptr_t retaddr);
 TLBRet loongarch_ptw(CPULoongArchState *env, MMUContext *context,
-                     int access_type, int mmu_idx, int debug);
+                     int access_type, int mmu_idx, int debug, int vm_level,
+                     uintptr_t retaddr);
 void get_dir_base_width(CPULoongArchState *env, uint64_t *dir_base,
-                        uint64_t *dir_width, unsigned int level);
+                        uint64_t *dir_width, unsigned int level, int vm_level);
+TLBRet loongarch_map_address(CPULoongArchState *env, MMUContext *context,
+                             MMUAccessType access_type, int mmu_idx,
+                             int is_debug, int vm_level, uintptr_t retaddr);
 hwaddr loongarch_cpu_get_phys_addr_debug(CPUState *cpu, vaddr addr);
 uint64_t loongarch_palen_mask(CPULoongArchState *env);
 
target/loongarch/cpu_helper.c +22 -18
index 123cad3d93..8eabb4a9dc 100644
@@ -19,7 +19,7 @@
 #include "tcg/tcg_loongarch.h"
 
 void get_dir_base_width(CPULoongArchState *env, uint64_t *dir_base,
-                        uint64_t *dir_width, unsigned int level)
+                        uint64_t *dir_width, unsigned int level, int vm_level)
 {
     CPUSysState *sys = env_sys(env);
 
@@ -49,7 +49,7 @@ void get_dir_base_width(CPULoongArchState *env, uint64_t *dir_base,
 }
 
 TLBRet loongarch_check_pte(CPULoongArchState *env, MMUContext *context,
-                           MMUAccessType access_type, int mmu_idx)
+                           MMUAccessType access_type, int mmu_idx, int vm_level)
 {
     uint64_t plv = mmu_idx;
     uint64_t tlb_entry, tlb_ppn;
@@ -58,8 +58,8 @@ TLBRet loongarch_check_pte(CPULoongArchState *env, MMUContext *context,
 
     tlb_entry = context->pte;
     tlb_ps = context->ps;
-    tlb_v = pte_present(env, tlb_entry);
-    tlb_d = pte_write(env, tlb_entry);
+    tlb_v = pte_present(env, tlb_entry, vm_level);
+    tlb_d = pte_write(env, tlb_entry, vm_level);
     tlb_plv = FIELD_EX64(tlb_entry, TLBENTRY, PLV);
     if (is_la64(env)) {
         tlb_ppn = FIELD_EX64(tlb_entry, TLBENTRY_64, PPN);
@@ -147,7 +147,8 @@ static MemTxResult loongarch_cmpxchg_phys(CPUState *cs, hwaddr phys,
 }
 
 TLBRet loongarch_ptw(CPULoongArchState *env, MMUContext *context,
-                     int access_type, int mmu_idx, int debug)
+                     int access_type, int mmu_idx, int debug, int vm_level,
+                     uintptr_t retaddr)
 {
     const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED;
     CPUState *cs = env_cpu(env);
@@ -170,7 +171,7 @@ TLBRet loongarch_ptw(CPULoongArchState *env, MMUContext *context,
     base &= palen_mask;
 
     for (level = 4; level >= 0; level--) {
-        get_dir_base_width(env, &dir_base, &dir_width, level);
+        get_dir_base_width(env, &dir_base, &dir_width, level, vm_level);
 
         if (dir_width == 0) {
             continue;
@@ -208,7 +209,7 @@ restart:
         context->pte_buddy[index] = base;
         context->pte_buddy[1 - index] = base + BIT_ULL(dir_base);
         base += (BIT_ULL(dir_base) & address);
-    } else if (cpu_has_ptw(env)) {
+    } else if (cpu_has_ptw(env, vm_level)) {
         uint64_t val;
 
         index &= 1;
@@ -220,7 +221,7 @@ restart:
 
     context->ps = dir_base;
     context->pte = base;
-    ret = loongarch_check_pte(env, context, access_type, mmu_idx);
+    ret = loongarch_check_pte(env, context, access_type, mmu_idx, vm_level);
     if (debug) {
         return ret;
     }
@@ -231,7 +232,7 @@ restart:
      * Need atomic compchxg operation with pte update, other vCPUs may
      * update pte at the same time.
      */
-    if (ret == TLBRET_MATCH && cpu_has_ptw(env)) {
+    if (ret == TLBRET_MATCH && cpu_has_ptw(env, vm_level)) {
         if (access_type == MMU_DATA_STORE && pte_dirty(base)) {
             return ret;
         }
@@ -273,15 +274,15 @@ restart:
     return ret;
 }
 
-static TLBRet loongarch_map_address(CPULoongArchState *env,
-                                    MMUContext *context,
-                                    MMUAccessType access_type, int mmu_idx,
-                                    int is_debug)
+TLBRet loongarch_map_address(CPULoongArchState *env, MMUContext *context,
+                             MMUAccessType access_type, int mmu_idx,
+                             int is_debug, int vm_level, uintptr_t retaddr)
 {
     TLBRet ret;
 
     if (tcg_enabled()) {
-        ret = loongarch_get_addr_from_tlb(env, context, access_type, mmu_idx);
+        ret = loongarch_get_addr_from_tlb(env, context, access_type, mmu_idx,
+                                          vm_level);
         if (ret != TLBRET_NOMATCH) {
             return ret;
         }
@@ -293,7 +294,8 @@ static TLBRet loongarch_map_address(CPULoongArchState *env,
          * legal mapping, even if the mapping is not yet in TLB. return 0 if
          * there is a valid map, else none zero.
          */
-        return loongarch_ptw(env, context, access_type, mmu_idx, is_debug);
+        return loongarch_ptw(env, context, access_type, mmu_idx, is_debug,
+                             vm_level, retaddr);
     }
 
     return TLBRET_NOMATCH;
@@ -312,10 +314,11 @@ static hwaddr dmw_va2pa(CPULoongArchState *env, vaddr va, uint64_t dmw)
 
 TLBRet get_physical_address(CPULoongArchState *env, MMUContext *context,
                             MMUAccessType access_type, int mmu_idx,
-                            int is_debug)
+                            int is_debug, uintptr_t retaddr)
 {
     int user_mode = mmu_idx == MMU_USER_IDX;
     int kernel_mode = mmu_idx == MMU_KERNEL_IDX;
+    int vm_level = env_vm_level(env);
     uint32_t plv, base_c, base_v;
     int64_t addr_high;
     CPUSysState *sys = env_sys(env);
@@ -360,7 +363,8 @@ TLBRet get_physical_address(CPULoongArchState *env, MMUContext *context,
     }
 
     /* Mapped address */
-    return loongarch_map_address(env, context, access_type, mmu_idx, is_debug);
+    return loongarch_map_address(env, context, access_type, mmu_idx, is_debug,
+                                 vm_level, retaddr);
 }
 
 hwaddr loongarch_cpu_get_phys_addr_debug(CPUState *cs, vaddr addr)
@@ -370,7 +374,7 @@ hwaddr loongarch_cpu_get_phys_addr_debug(CPUState *cs, vaddr addr)
 
     context.addr = addr;
     if (get_physical_address(env, &context, MMU_DATA_LOAD,
-                             cpu_mmu_index(cs, false), 1) != TLBRET_MATCH) {
+                             cpu_mmu_index(cs, false), 1, 0) != TLBRET_MATCH) {
         return -1;
     }
     return context.physical;
target/loongarch/tcg/helper.h +11 -11
index e76c73c775..a27b80b087 100644
@@ -114,17 +114,17 @@ DEF_HELPER_3(iocsrwr_w, void, env, tl, tl)
 DEF_HELPER_3(iocsrwr_d, void, env, tl, tl)
 
 /* TLB helper */
-DEF_HELPER_1(tlbwr, void, env)
-DEF_HELPER_1(tlbfill, void, env)
-DEF_HELPER_1(tlbsrch, void, env)
-DEF_HELPER_1(tlbrd, void, env)
-DEF_HELPER_1(tlbclr, void, env)
-DEF_HELPER_1(tlbflush, void, env)
-DEF_HELPER_1(invtlb_all, void, env)
-DEF_HELPER_2(invtlb_all_g, void, env, i32)
-DEF_HELPER_2(invtlb_all_asid, void, env, tl)
-DEF_HELPER_3(invtlb_page_asid, void, env, tl, tl)
-DEF_HELPER_3(invtlb_page_asid_or_g, void, env, tl, tl)
+DEF_HELPER_2(tlbwr, void, env, i32)
+DEF_HELPER_2(tlbfill, void, env, i32)
+DEF_HELPER_2(tlbsrch, void, env, i32)
+DEF_HELPER_2(tlbrd, void, env, i32)
+DEF_HELPER_2(tlbclr, void, env, i32)
+DEF_HELPER_2(tlbflush, void, env, i32)
+DEF_HELPER_4(invtlb_all, void, env, tl, i32, i32)
+DEF_HELPER_4(invtlb_all_g, void, env, tl, i32, i32)
+DEF_HELPER_3(invtlb_all_asid, void, env, tl, i32)
+DEF_HELPER_4(invtlb_page_asid, void, env, tl, tl, i32)
+DEF_HELPER_4(invtlb_page_asid_or_g, void, env, tl, tl, i32)
 
 DEF_HELPER_4(lddir, tl, env, tl, i32, i32)
 DEF_HELPER_4(ldpte, void, env, tl, tl, i32)
target/loongarch/tcg/insn_trans/trans_privileged.c.inc +29 -19

95 lines not on this page · raw message

target/loongarch/tcg/tcg_loongarch.h +2 -1
index 7fb627f2d6..6c0080c2b3 100644
@@ -18,6 +18,7 @@ bool loongarch_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
 
 TLBRet loongarch_get_addr_from_tlb(CPULoongArchState *env,
                                    MMUContext *context,
-                                   MMUAccessType access_type, int mmu_idx);
+                                   MMUAccessType access_type, int mmu_idx,
+                                   int vm_level);
 
 #endif  /* TARGET_LOONGARCH_TCG_LOONGARCH_H */
target/loongarch/tcg/tlb_helper.c +74 -59

372 lines not on this page · raw message

Signature · 1 line
2.55.0
#35 lore raw

PATCH v5 23/23 target/loongarch: exit to vm level 0 for fpd, sxd and asxd of vm level 0

In reply to SignKirigami

When vm level 0 fpe, sxe and asxe is set to 0, although on vm level 1 these are enabled, in TCGTBCPUState flags corresponding bit should not be set, and when handling exceptions, executing trigger_vm_exit() in this situation.

---
 target/loongarch/tcg/op_helper.c | 20 ++++++++++++++++++++
 target/loongarch/tcg/tcg_cpu.c   | 17 ++++++++++++-----
 2 files changed, 32 insertions(+), 5 deletions(-)
target/loongarch/tcg/op_helper.c +20 -0
index e2fe4b212f..a24a9b8b6a 100644
@@ -20,9 +20,29 @@
 /* Exceptions helpers */
 void helper_raise_exception(CPULoongArchState *env, uint32_t exception)
 {
+    CPUSysState *host;
+    int vm_exit = 0;
+
     if (exception == EXCCODE_GSPR || exception == EXCCODE_HVC) {
+        vm_exit = 1;
+    } else if (env_vm_level(env)) {
+        host = get_sys(env, VM_LEVEL0);
+        if (exception == EXCCODE_FPD &&
+            FIELD_EX64(host->CSR_EUEN, CSR_EUEN, FPE) == 0) {
+            vm_exit = 1;
+        } else if (exception == EXCCODE_SXD &&
+            FIELD_EX64(host->CSR_EUEN, CSR_EUEN, SXE) == 0) {
+            vm_exit = 1;
+        }
+        if (exception == EXCCODE_ASXD &&
+            FIELD_EX64(host->CSR_EUEN, CSR_EUEN, ASXE) == 0) {
+            vm_exit = 1;
+        }
+    }
+    if (vm_exit) {
         trigger_vm_exit(env);
     }
+
     do_raise_exception(env, exception, GETPC());
 }
 
target/loongarch/tcg/tcg_cpu.c +12 -5
index 6f1ba12edb..f70efaad78 100644
@@ -304,16 +304,23 @@ static vaddr loongarch_pointer_wrap(CPUState *cs, int mmu_idx,
 static TCGTBCPUState loongarch_get_tb_cpu_state(CPUState *cs)
 {
     CPULoongArchState *env = cpu_env(cs);
-    CPUSysState *sys = env_sys(env);
-    uint32_t flags;
+    CPUSysState *sys = env_sys(env), *host;
+    uint32_t flags, fpe, sxe, asxe;
 
     flags = sys->CSR_CRMD & (R_CSR_CRMD_PLV_MASK | R_CSR_CRMD_PG_MASK);
+    fpe = FIELD_EX64(sys->CSR_EUEN, CSR_EUEN, FPE);
+    sxe = FIELD_EX64(sys->CSR_EUEN, CSR_EUEN, SXE);
+    asxe = FIELD_EX64(sys->CSR_EUEN, CSR_EUEN, ASXE);
     if (env_vm_level(env) == VM_LEVEL1) {
         flags |= HW_FLAGS_GUEST_MODE;
+        host = get_sys(env, VM_LEVEL0);
+        fpe &= FIELD_EX64(host->CSR_EUEN, CSR_EUEN, FPE);
+        sxe &= FIELD_EX64(host->CSR_EUEN, CSR_EUEN, SXE);
+        asxe &= FIELD_EX64(host->CSR_EUEN, CSR_EUEN, ASXE);
     }
-    flags |= FIELD_EX64(sys->CSR_EUEN, CSR_EUEN, FPE) * HW_FLAGS_EUEN_FPE;
-    flags |= FIELD_EX64(sys->CSR_EUEN, CSR_EUEN, SXE) * HW_FLAGS_EUEN_SXE;
-    flags |= FIELD_EX64(sys->CSR_EUEN, CSR_EUEN, ASXE) * HW_FLAGS_EUEN_ASXE;
+    flags |= fpe * HW_FLAGS_EUEN_FPE;
+    flags |= sxe * HW_FLAGS_EUEN_SXE;
+    flags |= asxe * HW_FLAGS_EUEN_ASXE;
     flags |= is_va32(env) * HW_FLAGS_VA32;
 
     return (TCGTBCPUState){ .pc = env->pc, .flags = flags };
Signature · 1 line
2.55.0
#19 lore raw

PATCH v5 12/23 target/loongarch: add gid management functions

In reply to SignKirigami

Add get_gid(), get_tgid() and tlb_entry_matches_gid(), to get and use gid in lvz functions.

---
 target/loongarch/cpu.h            | 21 +++++++++++++++++++++
 target/loongarch/tcg/tlb_helper.c |  5 +++++
 2 files changed, 26 insertions(+)
target/loongarch/cpu.h +21 -0
index 3e3ae2bfa7..a42c5fe170 100644
@@ -583,6 +583,27 @@ static inline bool will_return_to_guest(CPULoongArchState *env)
     return FIELD_EX64(get_sys(env, VM_LEVEL0)->CSR_GSTAT, CSR_GSTAT, PVM);
 }
 
+static inline uint8_t get_gid(CPULoongArchState *env)
+{
+    return FIELD_EX64(get_sys(env, VM_LEVEL0)->CSR_GSTAT, CSR_GSTAT, GID);
+}
+
+static inline uint8_t get_tgid(CPULoongArchState *env)
+{
+    CPUSysState *host = get_sys(env, VM_LEVEL0);
+
+    if (env_vm_level(env) == VM_LEVEL1) {
+        return get_gid(env);
+    }
+
+    if (FIELD_EX64(host->CSR_GTLBC, CSR_GTLBC, USETGID)) {
+        return FIELD_EX64(host->CSR_GTLBC, CSR_GTLBC, TGID);
+    } else if (will_return_to_guest(env)) {
+        return get_gid(env);
+    }
+    return 0;
+}
+
 static inline CPUTimerState *env_timer(CPULoongArchState *env)
 {
     return &env->sys_states[0].timer_state;
target/loongarch/tcg/tlb_helper.c +5 -0
index d1f1d233fd..821aa26328 100644
@@ -35,6 +35,11 @@ static bool tlb_match_asid(bool global, int asid, int tlb_asid)
     return !global && tlb_asid == asid;
 }
 
+static inline bool tlb_entry_matches_gid(const LoongArchTLB *tlb, uint8_t gid)
+{
+    return FIELD_EX64(tlb->tlb_misc, TLB_MISC, GID) == gid;
+}
+
 bool check_ps(CPULoongArchState *env, uint8_t tlb_ps)
 {
     CPUSysState *sys = env_sys(env);
Signature · 1 line
2.55.0
#23 lore raw

PATCH v5 15/23 target/loongarch: add LVZ to invtlb

In reply to SignKirigami

Add op 0x9, 0xa-0xf to instruction invtlb to support vm level 1.

---
 .../tcg/insn_trans/trans_privileged.c.inc     | 34 +++++++++++++
 target/loongarch/tcg/tlb_helper.c             | 51 ++++++++++++++++---
 2 files changed, 77 insertions(+), 8 deletions(-)
target/loongarch/tcg/insn_trans/trans_privileged.c.inc +34 -0
index 493ed7f24b..00e8f61e42 100644
@@ -328,6 +328,10 @@ static bool trans_invtlb(DisasContext *ctx, arg_invtlb *a)
         return false;
     }
 
+    if (!avail_LVZ(ctx) && a->imm > 0x6) {
+        return false;
+    }
+
     switch (a->imm) {
     case 0x0:
     case 0x1:
@@ -352,6 +356,36 @@ static bool trans_invtlb(DisasContext *ctx, arg_invtlb *a)
         gen_helper_invtlb_page_asid_or_g(tcg_env, rj, rk,
                                          tcg_constant_i32(0));
         break;
+    case 0x9:
+    case 0x10:
+    case 0x11:
+    case 0x12:
+    case 0x13:
+    case 0x14:
+    case 0x15:
+    case 0x16:
+        //TODO: refine 0x9-0x16
+        gen_helper_invtlb_all(tcg_env, rj, tcg_constant_i32(0),
+                              tcg_constant_i32(0));
+        break;
+    case 0xa:
+        gen_helper_invtlb_all_g(tcg_env, rj, tcg_constant_i32(1),
+                                tcg_constant_i32(1));
+        break;
+    case 0xb:
+        gen_helper_invtlb_all_g(tcg_env, rj, tcg_constant_i32(0),
+                                tcg_constant_i32(1));
+        break;
+    case 0xc:
+        gen_helper_invtlb_all_asid(tcg_env, rj, tcg_constant_i32(1));
+        break;
+    case 0xd:
+        gen_helper_invtlb_page_asid(tcg_env, rj, rk, tcg_constant_i32(1));
+        break;
+    case 0xe:
+        gen_helper_invtlb_page_asid_or_g(tcg_env, rj, rk,
+                                         tcg_constant_i32(1));
+        break;
     default:
         return false;
     }
target/loongarch/tcg/tlb_helper.c +43 -8

101 lines not on this page · raw message

Signature · 1 line
2.55.0
#22 lore raw

PATCH v5 14/23 target/loongarch: add LVZ to TLB functions

In reply to SignKirigami

Add LVZ to most TLB handler, except invtlb and ptw.

---
 target/loongarch/cpu-mmu.h        |   2 +
 target/loongarch/cpu.h            |   1 +
 target/loongarch/cpu_helper.c     |  10 +--
 target/loongarch/tcg/tlb_helper.c | 132 ++++++++++++++++++++++++------
 4 files changed, 115 insertions(+), 30 deletions(-)
target/loongarch/cpu-mmu.h +2 -0
index 4d76d96ee5..29b0c109fb 100644
@@ -110,6 +110,8 @@ TLBRet loongarch_ptw(CPULoongArchState *env, MMUContext *context,
                      uintptr_t retaddr);
 void get_dir_base_width(CPULoongArchState *env, uint64_t *dir_base,
                         uint64_t *dir_width, unsigned int level, int vm_level);
+hwaddr loongarch_get_host_address(CPULoongArchState *env, hwaddr gpa,
+                                  uintptr_t retaddr);
 TLBRet loongarch_map_address(CPULoongArchState *env, MMUContext *context,
                              MMUAccessType access_type, int mmu_idx,
                              int is_debug, int vm_level, uintptr_t retaddr);
target/loongarch/cpu.h +1 -0
index a42c5fe170..995e8f5a7f 100644
@@ -262,6 +262,7 @@ FIELD(TLB_MISC, ASID, 1, 10)
 FIELD(TLB_MISC, VPPN, 13, 35)
 FIELD(TLB_MISC, PS, 48, 6)
 FIELD(TLB_MISC, GID, 54, 8)
+FIELD(TLB_MISC, VM_LEVEL, 62, 1)
 
 /*Msg interrupt registers */
 #define N_MSGIS                4
target/loongarch/cpu_helper.c +5 -5
index 8eabb4a9dc..23d26385c6 100644
@@ -21,7 +21,7 @@
 void get_dir_base_width(CPULoongArchState *env, uint64_t *dir_base,
                         uint64_t *dir_width, unsigned int level, int vm_level)
 {
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
 
     switch (level) {
     case 1:
@@ -51,7 +51,7 @@ void get_dir_base_width(CPULoongArchState *env, uint64_t *dir_base,
 TLBRet loongarch_check_pte(CPULoongArchState *env, MMUContext *context,
                            MMUAccessType access_type, int mmu_idx, int vm_level)
 {
-    uint64_t plv = mmu_idx;
+    uint64_t plv = mmu_idx_to_plv(mmu_idx);
     uint64_t tlb_entry, tlb_ppn;
     uint8_t tlb_ps, tlb_plv, tlb_nx, tlb_nr, tlb_rplv;
     bool tlb_v, tlb_d;
@@ -316,12 +316,12 @@ TLBRet get_physical_address(CPULoongArchState *env, MMUContext *context,
                             MMUAccessType access_type, int mmu_idx,
                             int is_debug, uintptr_t retaddr)
 {
-    int user_mode = mmu_idx == MMU_USER_IDX;
-    int kernel_mode = mmu_idx == MMU_KERNEL_IDX;
+    int user_mode = mmu_idx_to_plv(mmu_idx) == MMU_USER_IDX;
+    int kernel_mode = mmu_idx_to_plv(mmu_idx) == MMU_KERNEL_IDX;
     int vm_level = env_vm_level(env);
     uint32_t plv, base_c, base_v;
     int64_t addr_high;
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
     uint8_t da = FIELD_EX64(sys->CSR_CRMD, CSR_CRMD, DA);
     uint8_t pg = FIELD_EX64(sys->CSR_CRMD, CSR_CRMD, PG);
     vaddr address;
target/loongarch/tcg/tlb_helper.c +107 -25

307 lines not on this page · raw message

Signature · 1 line
2.55.0
#6 lore raw

Re: [PATCH v5 03/23] target/loongarch: add LVZ feature switcher

In reply to SignKirigami
Quoted 23 lines · SignKirigami

Add LVZ property and AVAIL_LVZ macro based on cpucfg.

---
  target/loongarch/cpu.c       | 33 +++++++++++++++++++++++++++++++--
  target/loongarch/cpu.h       |  6 ++++++
  target/loongarch/translate.h |  1 +
  3 files changed, 38 insertions(+), 2 deletions(-)
target/loongarch/cpu.c +5 -0
index e1245235be..664763137d 100644
@@ -267,11 +267,33 @@ static void loongarch_set_ptw(Object *obj, bool value, Error **errp)
      cpu->env.cpucfg[2] = FIELD_DP32(cpu->env.cpucfg[2], CPUCFG2, HPTW, value);
  }
 
+static bool loongarch_get_lvz(Object *obj, Error **errp)
+{
+    return LOONGARCH_CPU(obj)->lvz != ON_OFF_AUTO_OFF;
+}
+

target/loongarch/cpu.c static void loongarch_set_ptw(Object *obj, bool value, Error **errp) · line 284

Quoted 10 lines
target/loongarch/cpu.c +10 -0
+static void loongarch_set_lvz(Object *obj, bool value, Error **errp)
+{
+    LoongArchCPU *cpu = LOONGARCH_CPU(obj);
+
+    cpu->lvz = value ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
+
+    if (kvm_enabled()) {
+        /* lvz can not be only enabled in kvm mode currently */
+        return;
+    }

This can be removed if tcg_enabled() is added lvz option.

Quoted 16 lines
target/loongarch/cpu.c +7 -0
+
+    cpu->env.cpucfg[2] = FIELD_DP32(cpu->env.cpucfg[2], CPUCFG2, LVZ, value);
+    cpu->env.cpucfg[2] = FIELD_DP32(cpu->env.cpucfg[2], CPUCFG2, LVZ_VER,
+                                    value ? 1 : 0);
+}
+
  static void loongarch_cpu_post_init(Object *obj)
  {
      LoongArchCPU *cpu = LOONGARCH_CPU(obj);
 
      cpu->lbt = ON_OFF_AUTO_OFF;
+    cpu->lvz = ON_OFF_AUTO_AUTO;
      cpu->pmu = ON_OFF_AUTO_OFF;
      cpu->lsx = ON_OFF_AUTO_AUTO;
      cpu->lasx = ON_OFF_AUTO_AUTO;

target/loongarch/cpu.c static void loongarch_cpu_post_init(Object *obj) · line 310

Quoted 6 lines
target/loongarch/cpu.c +3 -0
@@ -283,6 +305,9 @@ static void loongarch_cpu_post_init(Object *obj)
                               loongarch_set_msgint);
      object_property_add_bool(obj, "ptw", loongarch_get_ptw,
                               loongarch_set_ptw);
+    object_property_add_bool(obj, "lvz", loongarch_get_lvz,
+                             loongarch_set_lvz);
+

how about adding if (tcg_enabled()) with lvz property.

target/loongarch/cpu.c static void loongarch_la464_initfn(Object *obj) · line 365

Quoted 11 lines
target/loongarch/cpu.c +2 -2
      /* lbt is enabled only in kvm mode, not supported in tcg mode */
 
      if (kvm_enabled()) {
@@ -336,8 +361,12 @@ static void loongarch_la464_initfn(Object *obj)
      data = FIELD_DP32(data, CPUCFG2, FP_SP, 1);
      data = FIELD_DP32(data, CPUCFG2, FP_DP, 1);
      data = FIELD_DP32(data, CPUCFG2, FP_VER, 1);
-    data = FIELD_DP32(data, CPUCFG2, LSX, 1),
-    data = FIELD_DP32(data, CPUCFG2, LASX, 1),
+    data = FIELD_DP32(data, CPUCFG2, LSX, 1);
+    data = FIELD_DP32(data, CPUCFG2, LASX, 1);

Good catch, thanks for change , with ; :=)

The others look good to me.

Regards
Bibo Mao

Quoted 44 lines
target/loongarch/cpu.c +4 -0
+    if (tcg_enabled()) {
+        data = FIELD_DP32(data, CPUCFG2, LVZ, 1);
+        data = FIELD_DP32(data, CPUCFG2, LVZ_VER, 1);
+    }
      data = FIELD_DP32(data, CPUCFG2, LLFTP, 1);
      data = FIELD_DP32(data, CPUCFG2, LLFTP_VER, 1);
      data = FIELD_DP32(data, CPUCFG2, LSPW, 1);
target/loongarch/cpu.h +6 -0
index c0edcc9065..918378ee9a 100644
@@ -460,6 +460,7 @@ struct ArchCPU {
      CPULoongArchState env;
      uint32_t  phy_id;
      OnOffAuto lbt;
+    OnOffAuto lvz;
      OnOffAuto pmu;
      OnOffAuto ptw;
      OnOffAuto lsx;
@@ -520,6 +521,11 @@ static inline CPUSysState *get_sys(CPULoongArchState *env, int vm_level)
      return &env->sys_states[vm_level];
  }
 
+static inline bool cpu_has_lvz(CPULoongArchState *env)
+{
+    return FIELD_EX32(env->cpucfg[2], CPUCFG2, LVZ);
+}
+
  static inline CPUTimerState *env_timer(CPULoongArchState *env)
  {
      return &env->sys_states[0].timer_state;
target/loongarch/translate.h +1 -0
index 8aa8325dc6..a7c165982c 100644
@@ -24,6 +24,7 @@
  #define avail_FP(C)     (FIELD_EX32((C)->cpucfg2, CPUCFG2, FP))
  #define avail_FP_SP(C)  (FIELD_EX32((C)->cpucfg2, CPUCFG2, FP_SP))
  #define avail_FP_DP(C)  (FIELD_EX32((C)->cpucfg2, CPUCFG2, FP_DP))
+#define avail_LVZ(C)    (FIELD_EX32((C)->cpucfg2, CPUCFG2, LVZ))
  #define avail_LSPW(C)   (FIELD_EX32((C)->cpucfg2, CPUCFG2, LSPW))
  #define avail_LAM(C)    (FIELD_EX32((C)->cpucfg2, CPUCFG2, LAM))
  #define avail_LAM_BH(C) (FIELD_EX32((C)->cpucfg2, CPUCFG2, LAM_BH))
Reviewed-by on v5 1/23
#2 lore raw

Re: [PATCH v5 01/23] target/loongarch: select sys state by VM level

In reply to SignKirigami
Quoted 48 lines · SignKirigami

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 54c2699897..6c6e1d81b8 100644
@@ -298,7 +298,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;
      }
@@ -428,7 +428,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 +3 -0
index c592f69885..391445f614 100644
@@ -321,6 +321,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"
@@ -507,9 +510,9 @@ static inline CPUSysState *env_sys(CPULoongArchState *env)
      return env->sys_state;

target/loongarch/cpu.h static inline CPUSysState *env_sys(CPULoongArchState *env) · line 518

Quoted 10 lines
target/loongarch/cpu.h +2 -2
  }
 
-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)
Reviewed-by on v5 2/23
#4 lore raw

Re: [PATCH v5 02/23] target/loongarch: add get_sys() helper

In reply to SignKirigami
Quoted 31 lines · SignKirigami

Add get_sys() to retrieve a CPUSysState by VM level. Use it to access the host state in CPU reset path to handle possible reset requests under vm_level 1.

---
  target/loongarch/cpu.c | 2 +-
  target/loongarch/cpu.h | 5 +++++
  2 files changed, 6 insertions(+), 1 deletion(-)
target/loongarch/cpu.c +1 -1
index 6c6e1d81b8..e1245235be 100644
@@ -621,7 +621,7 @@ static void loongarch_cpu_reset_hold(Object *obj, ResetType type)
      CPUState *cs = CPU(obj);
      LoongArchCPUClass *lacc = LOONGARCH_CPU_GET_CLASS(obj);
      CPULoongArchState *env = cpu_env(cs);
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, VM_LEVEL0);
 
      if (lacc->parent_phases.hold) {
          lacc->parent_phases.hold(obj, type);
target/loongarch/cpu.h +0 -0
index 391445f614..c0edcc9065 100644
@@ -515,6 +515,11 @@ static inline void set_sys(CPULoongArchState *env, int vm_level)
      env->sys_state = &env->sys_states[vm_level];

target/loongarch/cpu.h static inline void set_sys(CPULoongArchState *env, int vm_level) · line 525

Quoted 10 lines
target/loongarch/cpu.h +5 -0
  }
 
+static inline CPUSysState *get_sys(CPULoongArchState *env, int vm_level)
+{
+    return &env->sys_states[vm_level];
+}
+
  static inline CPUTimerState *env_timer(CPULoongArchState *env)
  {
      return &env->sys_states[0].timer_state;
#10 lore raw

Re: [PATCH v5 06/23] target/loongarch: add definition about guest mmu_idx

In reply to SignKirigami
Quoted 17 lines · SignKirigami

Add MMU_GUEST_IDX and MMU_GUEST_DA_IDX. Provide is_guest_mmu idx() and mmu_idx_to_plv to calculate plv with new mmu_idx.

---
  target/loongarch/cpu.h                         | 18 ++++++++++++++++++
  .../tcg/insn_trans/trans_privileged.c.inc      |  2 +-
  target/loongarch/tcg/tcg_cpu.c                 |  7 +++++++
  target/loongarch/tcg/translate.c               |  3 +++
  4 files changed, 29 insertions(+), 1 deletion(-)
target/loongarch/cpu.h +0 -0
index 183210efd8..9eea1badaf 100644

target/loongarch/cpu.h struct LoongArchCPUClass { · line 517

Quoted 5 lines
target/loongarch/cpu.h +2 -0
@@ -513,6 +513,24 @@ struct LoongArchCPUClass {
  #define MMU_KERNEL_IDX   MMU_PLV_KERNEL
  #define MMU_USER_IDX     MMU_PLV_USER
  #define MMU_DA_IDX       4
+#define MMU_GUEST_IDX    5
+#define MMU_GUEST_DA_IDX 9

how about replace constant 9 with (MMU_DA_IDX + MMU_GUEST_DA_IDX).

Quoted 65 lines
target/loongarch/cpu.h +16 -0
+
+static inline bool is_guest_mmu_idx(int mmu_idx)
+{
+    return mmu_idx >= MMU_GUEST_IDX;
+}
+
+static inline int mmu_idx_to_plv(int mmu_idx)
+{
+    if (mmu_idx == MMU_DA_IDX || mmu_idx == MMU_GUEST_DA_IDX) {
+        return 0;
+    }
+    if (is_guest_mmu_idx(mmu_idx)) {
+        return mmu_idx - MMU_GUEST_IDX;
+    }
+    return mmu_idx;
+}
 
  static inline CPUSysState *env_sys(CPULoongArchState *env)
  {
target/loongarch/tcg/insn_trans/trans_privileged.c.inc +1 -1
index 6728ce5ec9..7ee3bb8e27 100644
@@ -249,7 +249,7 @@ TRANS64(iocsrwr_d, IOCSR, gen_iocsrwr, gen_helper_iocsrwr_d)
 
  static void check_mmu_idx(DisasContext *ctx)
  {
-    if (ctx->mem_idx != MMU_DA_IDX) {
+    if (ctx->mem_idx != MMU_DA_IDX && ctx->mem_idx != MMU_GUEST_DA_IDX) {
          tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next + 4);
          ctx->base.is_jmp = DISAS_EXIT;
      }
target/loongarch/tcg/tcg_cpu.c +7 -0
index 14fa5f5809..b60d9e7cf8 100644
@@ -309,6 +309,13 @@ static int loongarch_cpu_mmu_index(CPUState *cs, bool ifetch)
      CPULoongArchState *env = cpu_env(cs);
      CPUSysState *sys = env_sys(env);
 
+    if (env_vm_level(env) == VM_LEVEL1) {
+        if (FIELD_EX64(sys->CSR_CRMD, CSR_CRMD, PG)) {
+            return MMU_GUEST_IDX + FIELD_EX64(sys->CSR_CRMD, CSR_CRMD, PLV);
+        }
+        return MMU_GUEST_DA_IDX;
+    }
+
      if (FIELD_EX64(sys->CSR_CRMD, CSR_CRMD, PG)) {
          return FIELD_EX64(sys->CSR_CRMD, CSR_CRMD, PLV);
      }
target/loongarch/tcg/translate.c +3 -0
index 124dce6269..4ab06b48da 100644
@@ -129,6 +129,9 @@ static void loongarch_tr_init_disas_context(DisasContextBase *dcbase,
      } else {
          ctx->mem_idx = MMU_DA_IDX;
      }
+    if (ctx->base.tb->flags & HW_FLAGS_GUEST_MODE) {
+        ctx->mem_idx += MMU_GUEST_IDX;
+    }
 
      /* Bound the number of insns to execute to those left on the page.  */
      bound = -(ctx->base.pc_first | TARGET_PAGE_MASK) / 4;
#12 lore raw

Re: [PATCH v5 07/23] target/loongarch: add LVZ guest interrupt state

In reply to SignKirigami
Quoted 150 lines · SignKirigami

Route configured hardware interrupts into the guest CSR bank, track guest interrupt requests separately, and expose guest pending and enable checks to the TCG runtime.

---
  target/loongarch/cpu.c            | 35 +++++++++++++++++++--------
  target/loongarch/cpu.h            |  3 +++
  target/loongarch/internals.h      |  4 ++--
  target/loongarch/tcg/csr_helper.c |  2 +-
  target/loongarch/tcg/tcg_cpu.c    | 39 ++++++++++++++++++++++---------
  5 files changed, 59 insertions(+), 24 deletions(-)
target/loongarch/cpu.c +25 -10
index 664763137d..5cef63a778 100644
@@ -58,16 +58,20 @@ static vaddr loongarch_cpu_get_pc(CPUState *cs)
  #ifndef CONFIG_USER_ONLY
  #include "hw/loongarch/virt.h"
 
-void loongarch_cpu_update_irq(LoongArchCPU *cpu)
+void loongarch_cpu_update_irq(LoongArchCPU *cpu, int vm_level)
  {
      CPULoongArchState *env = &cpu->env;
      CPUState *cs = CPU(cpu);
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
+    int interrupt = vm_level ? CPU_INTERRUPT_GUEST : CPU_INTERRUPT_HARD;
 
+    if (vm_level == VM_LEVEL1 && env_vm_level(env) != VM_LEVEL1) {
+        return;
+    }
      if (FIELD_EX64(sys->CSR_ESTAT, CSR_ESTAT, IS)) {
-        cpu_interrupt(cs, CPU_INTERRUPT_HARD);
+        cpu_interrupt(cs, interrupt);
      } else {
-        cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
+        cpu_reset_interrupt(cs, interrupt);
      }
  }
 
@@ -75,21 +79,28 @@ static void loongarch_cpu_self_set_irq(CPUState *cs, run_on_cpu_data data)
  {
      LoongArchCPU *cpu = LOONGARCH_CPU(cs);
      CPULoongArchState *env = cpu_env(cs);
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys;
+    int vm_level = 0;
      int irq, level;
 
      irq = data.host_int & ~BIT(31);
      level = (data.host_int >> 31) & 1;
+    if (irq >= N_IRQS) {
+        irq -= N_IRQS;
+        vm_level = 1;
+    }
+    sys = get_sys(env, vm_level);
      sys->CSR_ESTAT = deposit64(sys->CSR_ESTAT, irq, 1, level != 0);
-    loongarch_cpu_update_irq(cpu);
+    loongarch_cpu_update_irq(cpu, vm_level);
  }
 
  void loongarch_cpu_set_irq(void *opaque, int irq, int level)
  {
      LoongArchCPU *cpu = opaque;
      CPUState *cs = CPU(cpu);
+    CPULoongArchState *env = cpu_env(cs);
 
-    if (irq < 0 || irq >= N_IRQS) {
+    if (irq < 0 || irq >= (cpu_has_lvz(env) ? N_VIRQS : N_IRQS)) {
          return;
      }
 
@@ -103,11 +114,11 @@ void loongarch_cpu_set_irq(void *opaque, int irq, int level)
  }
 
  /* Check if there is pending and not masked out interrupt */
-bool cpu_loongarch_hw_interrupts_pending(CPULoongArchState *env)
+bool cpu_loongarch_hw_interrupts_pending(CPULoongArchState *env, int vm_level)
  {
      uint32_t pending;
      uint32_t status;
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
 
      pending = FIELD_EX64(sys->CSR_ESTAT, CSR_ESTAT, IS);
      status  = FIELD_EX64(sys->CSR_ECFG, CSR_ECFG, LIE);
@@ -122,7 +133,11 @@ bool loongarch_cpu_has_work(CPUState *cs)
      bool has_work = false;
 
      if (cpu_test_interrupt(cs, CPU_INTERRUPT_HARD) &&
-        cpu_loongarch_hw_interrupts_pending(cpu_env(cs))) {
+        cpu_loongarch_hw_interrupts_pending(cpu_env(cs), VM_LEVEL0)) {
+        has_work = true;
+    }
+    if (cpu_test_interrupt(cs, CPU_INTERRUPT_GUEST) &&
+        cpu_loongarch_hw_interrupts_pending(cpu_env(cs), VM_LEVEL1)) {
          has_work = true;
      }
 
target/loongarch/cpu.h +3 -0
index 9eea1badaf..8939dbc748 100644
@@ -20,6 +20,8 @@
  #include "cpu-csr.h"
  #include "cpu-qom.h"
 
+#define CPU_INTERRUPT_GUEST CPU_INTERRUPT_TGT_EXT_0
+
  #define FCSR0_M1    0x1f         /* FCSR1 mask, Enables */
  #define FCSR0_M2    0x1f1f0000   /* FCSR2 mask, Cause and Flags */
  #define FCSR0_M3    0x300        /* FCSR3 mask, Round Mode */
@@ -242,6 +244,7 @@ extern const char * const fregnames[32];
  #define IRQ_TIMER   11
  #define IRQ_IPI     12
  #define INT_DMSI    14
+#define N_VIRQS     (N_IRQS * 2)
 
  #define MAX_PERF_EVENTS        16
  #define LOONGARCH_STLB         2048 /* 2048 STLB */
target/loongarch/internals.h +2 -2
index ff956823fd..c11cefc4ee 100644
@@ -31,7 +31,7 @@ void restore_fp_status(CPULoongArchState *env);
  #ifndef CONFIG_USER_ONLY
  extern const VMStateDescription vmstate_loongarch_cpu;
 
-void loongarch_cpu_update_irq(LoongArchCPU *cpu);
+void loongarch_cpu_update_irq(LoongArchCPU *cpu, int vm_level);
  void loongarch_cpu_set_irq(void *opaque, int irq, int level);
 
  void cpu_loongarch_timer_cb(void *opaque);
@@ -39,7 +39,7 @@ uint64_t cpu_loongarch_get_timer_counter(CPUTimerState *timer);
  uint64_t cpu_loongarch_get_timer_ticks(CPUTimerState *timer);
  void cpu_loongarch_set_timer_config(CPUTimerState *timer, uint64_t value);
  bool loongarch_cpu_has_work(CPUState *cs);
-bool cpu_loongarch_hw_interrupts_pending(CPULoongArchState *env);
+bool cpu_loongarch_hw_interrupts_pending(CPULoongArchState *env, int vm_level);
  #endif /* !CONFIG_USER_ONLY */
 
  uint64_t read_fcc(CPULoongArchState *env);
target/loongarch/tcg/csr_helper.c +0 -0
index 1a6919417f..ecfba63228 100644

target/loongarch/tcg/csr_helper.c target_ulong helper_csrwr_estat(CPULoongArchState *env, target_ulong val) · line 114

Quoted 5 lines
target/loongarch/tcg/csr_helper.c +1 -1
@@ -111,7 +111,7 @@ target_ulong helper_csrwr_estat(CPULoongArchState *env, target_ulong val)
       */
      if (sys->CSR_ESTAT != old_v) {
          bql_lock();
-        loongarch_cpu_update_irq(env_archcpu(env));
+        loongarch_cpu_update_irq(env_archcpu(env), false);

why is it false, I think it should be current VM_LEVEL.

Quoted 12 lines
target/loongarch/tcg/csr_helper.c +0 -0
          bql_unlock();
      }
 
target/loongarch/tcg/tcg_cpu.c +0 -1
index b60d9e7cf8..101fe6fc7a 100644
@@ -238,30 +238,47 @@ static void loongarch_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
      }
  }
 
-static inline bool cpu_loongarch_hw_interrupts_enabled(CPULoongArchState *env)

target/loongarch/tcg/tcg_cpu.c static void loongarch_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr, · line 248

Quoted 10 lines
target/loongarch/tcg/tcg_cpu.c +4 -2
+static inline bool cpu_loongarch_hw_interrupts_enabled(CPULoongArchState *env, int vm_level)
  {
      bool ret = 0;
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
+    CPUSysState *host = get_sys(env, VM_LEVEL0);
 
      ret = (FIELD_EX64(sys->CSR_CRMD, CSR_CRMD, IE) &&
-          !(FIELD_EX64(sys->CSR_DBG, CSR_DBG, DST)));
+          !(FIELD_EX64(host->CSR_DBG, CSR_DBG, DST)));

why checking CSR_DBG of host here, it is a little strange.

Regards
Bibo Mao

Quoted 45 lines
target/loongarch/tcg/tcg_cpu.c +24 -8
      return ret;
  }
 
  static bool loongarch_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
  {
+    CPULoongArchState *env = cpu_env(cs);
+    bool has_interrupt = false;
+
      if (interrupt_request & CPU_INTERRUPT_HARD) {
-        CPULoongArchState *env = cpu_env(cs);
-
-        if (cpu_loongarch_hw_interrupts_enabled(env) &&
-            cpu_loongarch_hw_interrupts_pending(env)) {
-            /* Raise it */
-            cs->exception_index = EXCCODE_INT;
-            loongarch_cpu_do_interrupt(cs);
-            return true;
+
+        if (cpu_loongarch_hw_interrupts_enabled(env, VM_LEVEL0) &&
+            cpu_loongarch_hw_interrupts_pending(env, VM_LEVEL0)) {
+            if (env_vm_level(env) != VM_LEVEL0) {
+                trigger_vm_exit(env);
+            }
+            has_interrupt = true;
          }
+    } else if (interrupt_request & CPU_INTERRUPT_GUEST) {
+        if (cpu_loongarch_hw_interrupts_enabled(env, VM_LEVEL1) &&
+            cpu_loongarch_hw_interrupts_pending(env, VM_LEVEL1) &&
+            env_vm_level(env) == VM_LEVEL1) {
+            has_interrupt = true;
+        }
+    }
+
+    if (has_interrupt) {
+        /* Raise it */
+        cs->exception_index = EXCCODE_INT;
+        loongarch_cpu_do_interrupt(cs);
+        return true;
      }
+
      return false;
  }
 
 
#15 lore raw

Re: [PATCH v5 08/23] target/loongarch: add LVZ guest timer

In reply to SignKirigami
Quoted 14 lines · SignKirigami

Add a separate guest timer and IRQ_VTIMER.

---
  target/loongarch/cpu.c | 8 +++++++-
  target/loongarch/cpu.h | 6 ++++++
  2 files changed, 13 insertions(+), 1 deletion(-)
target/loongarch/cpu.c +0 -0
index 5cef63a778..8e759a2987 100644

target/loongarch/cpu.c static void loongarch_cpu_init(Object *obj) · line 776

Quoted 8 lines
target/loongarch/cpu.c +1 -1
@@ -770,7 +770,7 @@ static void loongarch_cpu_init(Object *obj)
      LoongArchCPU *cpu = LOONGARCH_CPU(obj);
  #ifdef CONFIG_TCG
      CPULoongArchState *env = &cpu->env;
-    CPUTimerState *timer;
+    CPUTimerState *timer, *guest_timer;
  #endif
 
      qdev_init_gpio_in(DEVICE(cpu), loongarch_cpu_set_irq, N_IRQS);

If cpu_has_lvz(), I think here N_IRQS should be N_VIRQS.

target/loongarch/cpu.c static void loongarch_cpu_init(Object *obj) · line 788

Quoted 12 lines
target/loongarch/cpu.c +6 -0
@@ -778,8 +778,14 @@ static void loongarch_cpu_init(Object *obj)
      timer = env_timer(env);
      timer->irq = IRQ_TIMER;
      timer->cs  = CPU(obj);
+    guest_timer = env_guest_timer(env);
+    guest_timer->irq = IRQ_VTIMER;
+    guest_timer->cs = CPU(obj);
+
      timer_init_ns(&timer->timer, QEMU_CLOCK_VIRTUAL,
                    &cpu_loongarch_timer_cb, timer);
+    timer_init_ns(&guest_timer->timer, QEMU_CLOCK_VIRTUAL,
+                  &cpu_loongarch_timer_cb, guest_timer);

Had better add cpu_has_lvz() when create guest timer.

Regards
Bibo Mao

Quoted 28 lines
target/loongarch/cpu.c +0 -0
  #endif
  #endif
  }
target/loongarch/cpu.h +6 -0
index 8939dbc748..ba28d80272 100644
@@ -245,6 +245,7 @@ extern const char * const fregnames[32];
  #define IRQ_IPI     12
  #define INT_DMSI    14
  #define N_VIRQS     (N_IRQS * 2)
+#define IRQ_VTIMER  (N_IRQS + IRQ_TIMER)
 
  #define MAX_PERF_EVENTS        16
  #define LOONGARCH_STLB         2048 /* 2048 STLB */
@@ -577,6 +578,11 @@ static inline CPUTimerState *env_timer(CPULoongArchState *env)
      return &env->sys_states[0].timer_state;
  }
 
+static inline CPUTimerState *env_guest_timer(CPULoongArchState *env)
+{
+    return &env->sys_states[1].timer_state;
+}
+
  static inline bool is_la64(CPULoongArchState *env)
  {
      return FIELD_EX32(env->cpucfg[1], CPUCFG1, ARCH) == CPUCFG1_ARCH_LA64;
#20 lore raw

Re: [PATCH v5 12/23] target/loongarch: add gid management functions

In reply to SignKirigami
Quoted 26 lines · SignKirigami

Add get_gid(), get_tgid() and tlb_entry_matches_gid(), to get and use gid in lvz functions.

---
  target/loongarch/cpu.h            | 21 +++++++++++++++++++++
  target/loongarch/tcg/tlb_helper.c |  5 +++++
  2 files changed, 26 insertions(+)
target/loongarch/cpu.h +8 -0
index 3e3ae2bfa7..a42c5fe170 100644
@@ -583,6 +583,27 @@ static inline bool will_return_to_guest(CPULoongArchState *env)
      return FIELD_EX64(get_sys(env, VM_LEVEL0)->CSR_GSTAT, CSR_GSTAT, PVM);
  }
 
+static inline uint8_t get_gid(CPULoongArchState *env)
+{
+    return FIELD_EX64(get_sys(env, VM_LEVEL0)->CSR_GSTAT, CSR_GSTAT, GID);
+}
+
+static inline uint8_t get_tgid(CPULoongArchState *env)
+{
+    CPUSysState *host = get_sys(env, VM_LEVEL0);

target/loongarch/cpu.h static inline bool will_return_to_guest(CPULoongArchState *env) · line 603

Quoted 10 lines
target/loongarch/cpu.h +10 -0
+
+    if (env_vm_level(env) == VM_LEVEL1) {
+        return get_gid(env);
+    }
+
+    if (FIELD_EX64(host->CSR_GTLBC, CSR_GTLBC, USETGID)) {
+        return FIELD_EX64(host->CSR_GTLBC, CSR_GTLBC, TGID);
+    } else if (will_return_to_guest(env)) {
+        return get_gid(env);
+    }why there is such checking with tgid()?, I think it is simple to come 

from TGID field of CSR_GTLBC even in host mode, such as:
return FIELD_EX64(host->CSR_GTLBC, CSR_GTLBC, TGID);

Regards
Bibo Mao

Quoted 23 lines
target/loongarch/cpu.h +3 -0
+    return 0;
+}
+
  static inline CPUTimerState *env_timer(CPULoongArchState *env)
  {
      return &env->sys_states[0].timer_state;
target/loongarch/tcg/tlb_helper.c +5 -0
index d1f1d233fd..821aa26328 100644
@@ -35,6 +35,11 @@ static bool tlb_match_asid(bool global, int asid, int tlb_asid)
      return !global && tlb_asid == asid;
  }
 
+static inline bool tlb_entry_matches_gid(const LoongArchTLB *tlb, uint8_t gid)
+{
+    return FIELD_EX64(tlb->tlb_misc, TLB_MISC, GID) == gid;
+}
+
  bool check_ps(CPULoongArchState *env, uint8_t tlb_ps)
  {
      CPUSysState *sys = env_sys(env);
#32 lore raw

Re: [PATCH v5 20/23] target/loongarch: translate LVZ guest TLB instructions

In reply to SignKirigami
Quoted 33 lines · SignKirigami

Decode, disassemble and translate GTLBSRCH, GTLBRD, GTLBWR, GTLBFILL, GTLBCLR and GTLBFLUSH for host LVZ execution.

---
  target/loongarch/disas.c                      |  6 ++
  target/loongarch/insns.decode                 |  7 +++
  .../tcg/insn_trans/trans_privileged.c.inc     | 60 +++++++++++++++++++
  3 files changed, 73 insertions(+)
target/loongarch/disas.c +6 -0
index 2436c9eec3..6425dea17c 100644
@@ -700,6 +700,12 @@ INSN(tlbfill,      empty)
  INSN(tlbclr,       empty)
  INSN(tlbflush,     empty)
  INSN(invtlb,       i_rr)
+INSN(gtlbclr,      empty)
+INSN(gtlbflush,    empty)
+INSN(gtlbsrch,     empty)
+INSN(gtlbrd,       empty)
+INSN(gtlbwr,       empty)
+INSN(gtlbfill,     empty)
  INSN(cacop,        cop_r_i)
  INSN(lddir,        rr_i)
  INSN(ldpte,        j_i)
target/loongarch/insns.decode +0 -0
index 4612af9433..cbcc30c31f 100644

target/loongarch/insns.decode bgeu 0110 11 ................ ..... ..... @rr_offs16 · line 507

Quoted 9 lines
target/loongarch/insns.decode +6 -0
@@ -499,6 +499,13 @@ bgeu            0110 11 ................ ..... .....     @rr_offs16
    gcsrxchg          0000 0101 .............. ..... .....     @rr_csr
  }
 
+gtlbclr          0000 01100100 10000 01000 00000 00001    @empty
+gtlbflush        0000 01100100 10000 01001 00000 00001    @empty
+gtlbsrch         0000 01100100 10000 01010 00000 00001    @empty
+gtlbrd           0000 01100100 10000 01011 00000 00001    @empty
+gtlbwr           0000 01100100 10000 01100 00000 00001    @empty
+gtlbfill         0000 01100100 10000 01101 00000 00001    @empty

This series instruction for gtlbxxx is not used by KVM, I suggest removing these instruction. It make things complicated.

Regards
Bibo Mao

Quoted 90 lines
target/loongarch/insns.decode +1 -0
+
  iocsrrd_b        0000 01100100 10000 00000 ..... .....    @rr
  iocsrrd_h        0000 01100100 10000 00001 ..... .....    @rr
  iocsrrd_w        0000 01100100 10000 00010 ..... .....    @rr
target/loongarch/tcg/insn_trans/trans_privileged.c.inc +60 -0
index 37b7ef13dc..ae481aa83f 100644
@@ -42,6 +42,12 @@ GEN_FALSE_TRANS(idle)
  GEN_FALSE_TRANS(gcsrrd)
  GEN_FALSE_TRANS(gcsrwr)
  GEN_FALSE_TRANS(gcsrxchg)
+GEN_FALSE_TRANS(gtlbclr)
+GEN_FALSE_TRANS(gtlbflush)
+GEN_FALSE_TRANS(gtlbsrch)
+GEN_FALSE_TRANS(gtlbrd)
+GEN_FALSE_TRANS(gtlbwr)
+GEN_FALSE_TRANS(gtlbfill)
 
  #else
 
@@ -339,6 +345,15 @@ static bool trans_tlbsrch(DisasContext *ctx, arg_tlbsrch *a)
      return true;
  }
 
+static bool trans_gtlbsrch(DisasContext *ctx, arg_gtlbsrch *a)
+{
+    if (check_plv(ctx) || !avail_LVZ(ctx) || is_guest_mmu_idx(ctx->mem_idx)) {
+        return false;
+    }
+    gen_helper_tlbsrch(tcg_env, tcg_constant_i32(1));
+    return true;
+}
+
  static bool trans_tlbrd(DisasContext *ctx, arg_tlbrd *a)
  {
      if (check_plv(ctx)) {
@@ -393,6 +408,51 @@ static bool trans_tlbflush(DisasContext *ctx, arg_tlbflush *a)
      return true;
  }
 
+static bool trans_gtlbrd(DisasContext *ctx, arg_gtlbrd *a)
+{
+    if (check_plv(ctx) || !avail_LVZ(ctx) || is_guest_mmu_idx(ctx->mem_idx)) {
+        return false;
+    }
+    gen_helper_tlbrd(tcg_env, tcg_constant_i32(1));
+    return true;
+}
+
+static bool trans_gtlbwr(DisasContext *ctx, arg_gtlbwr *a)
+{
+    if (check_plv(ctx) || !avail_LVZ(ctx) || is_guest_mmu_idx(ctx->mem_idx)) {
+        return false;
+    }
+    gen_helper_tlbwr(tcg_env, tcg_constant_i32(1));
+    return true;
+}
+
+static bool trans_gtlbfill(DisasContext *ctx, arg_gtlbfill *a)
+{
+    if (check_plv(ctx) || !avail_LVZ(ctx) || is_guest_mmu_idx(ctx->mem_idx)) {
+        return false;
+    }
+    gen_helper_tlbfill(tcg_env, tcg_constant_i32(1));
+    return true;
+}
+
+static bool trans_gtlbclr(DisasContext *ctx, arg_gtlbclr *a)
+{
+    if (check_plv(ctx) || !avail_LVZ(ctx) || is_guest_mmu_idx(ctx->mem_idx)) {
+        return false;
+    }
+    gen_helper_tlbclr(tcg_env, tcg_constant_i32(1));
+    return true;
+}
+
+static bool trans_gtlbflush(DisasContext *ctx, arg_gtlbflush *a)
+{
+    if (check_plv(ctx) || !avail_LVZ(ctx) || is_guest_mmu_idx(ctx->mem_idx)) {
+        return false;
+    }
+    gen_helper_tlbflush(tcg_env, tcg_constant_i32(1));
+    return true;
+}
+
  static bool trans_invtlb(DisasContext *ctx, arg_invtlb *a)
  {
      TCGv rj = gpr_src(ctx, a->rj, EXT_NONE);
#29 lore raw

Re: [PATCH v5 18/23] target/loongarch: add gcsr instructions and vm level 1 csr management

In reply to SignKirigami
Quoted 323 lines · SignKirigami

Add gcsrrd, gcsrwr and gcsrxchg. Make all csr helper be used by both vm level 0 and 1.

---
  target/loongarch/disas.c                      |   3 +
  target/loongarch/insns.decode                 |   6 +
  target/loongarch/tcg/csr_helper.c             |  78 +++++++-----
  target/loongarch/tcg/helper.h                 |  22 ++--
  .../tcg/insn_trans/trans_privileged.c.inc     | 111 ++++++++++++++----
  5 files changed, 161 insertions(+), 59 deletions(-)
target/loongarch/disas.c +3 -0
index 03d1e560dd..2436c9eec3 100644
@@ -682,6 +682,9 @@ INSN(bgeu,         rr_offs)
  INSN(csrrd,        r_csr)
  INSN(csrwr,        r_csr)
  INSN(csrxchg,      rr_csr)
+INSN(gcsrrd,        r_csr)
+INSN(gcsrwr,        r_csr)
+INSN(gcsrxchg,      rr_csr)
  INSN(iocsrrd_b,    rr)
  INSN(iocsrrd_h,    rr)
  INSN(iocsrrd_w,    rr)
target/loongarch/insns.decode +6 -0
index 3089d42044..4612af9433 100644
@@ -493,6 +493,12 @@ bgeu            0110 11 ................ ..... .....     @rr_offs16
    csrxchg           0000 0100 .............. ..... .....     @rr_csr
  }
 
+{
+  gcsrrd            0000 0101 .............. 00000 .....     @r_csr
+  gcsrwr            0000 0101 .............. 00001 .....     @r_csr
+  gcsrxchg          0000 0101 .............. ..... .....     @rr_csr
+}
+
  iocsrrd_b        0000 01100100 10000 00000 ..... .....    @rr
  iocsrrd_h        0000 01100100 10000 00001 ..... .....    @rr
  iocsrrd_w        0000 01100100 10000 00010 ..... .....    @rr
target/loongarch/tcg/csr_helper.c +50 -28
index ecfba63228..7ef0032f5d 100644
@@ -18,9 +18,10 @@
  #include "cpu-csr.h"
  #include "cpu-mmu.h"
 
-target_ulong helper_csrwr_stlbps(CPULoongArchState *env, target_ulong val)
+target_ulong helper_csrwr_stlbps(CPULoongArchState *env,
+                                 target_ulong val, uint32_t vm_level)
  {
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
      int64_t old_v = sys->CSR_STLBPS;
 
      /*
@@ -40,10 +41,10 @@ target_ulong helper_csrwr_stlbps(CPULoongArchState *env, target_ulong val)
      return old_v;
  }
 
-target_ulong helper_csrrd_pgd(CPULoongArchState *env)
+target_ulong helper_csrrd_pgd(CPULoongArchState *env, uint32_t vm_level)
  {
      int64_t v;
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
 
      if (sys->CSR_TLBRERA & 0x1) {
          v = sys->CSR_TLBRBADV;
@@ -60,25 +61,30 @@ target_ulong helper_csrrd_pgd(CPULoongArchState *env)
      return v;
  }
 
-target_ulong helper_csrrd_cpuid(CPULoongArchState *env)
+target_ulong helper_csrrd_cpuid(CPULoongArchState *env, uint32_t vm_level)
  {
      LoongArchCPU *lac = env_archcpu(env);
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
+
+    if (vm_level) {
+        return sys->CSR_CPUID;
+    }
 
      sys->CSR_CPUID = CPU(lac)->cpu_index;
 
      return sys->CSR_CPUID;
  }
 
-target_ulong helper_csrrd_tval(CPULoongArchState *env)
+target_ulong helper_csrrd_tval(CPULoongArchState *env, uint32_t vm_level)
  {
-    return cpu_loongarch_get_timer_ticks(env_timer(env));
+    CPUTimerState *timer = vm_level ? env_guest_timer(env) : env_timer(env);
+    return cpu_loongarch_get_timer_ticks(timer);
  }
 
-target_ulong helper_csrrd_msgir(CPULoongArchState *env)
+target_ulong helper_csrrd_msgir(CPULoongArchState *env, uint32_t vm_level)
  {
      int irq, new;
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
 
      irq = find_first_bit((unsigned long *)sys->CSR_MSGIS, 256);
      if (irq < 256) {
@@ -97,13 +103,23 @@ target_ulong helper_csrrd_msgir(CPULoongArchState *env)
      return irq;
  }
 
-target_ulong helper_csrwr_estat(CPULoongArchState *env, target_ulong val)
+target_ulong helper_csrwr_estat(CPULoongArchState *env,
+                                target_ulong val, uint32_t vm_level)
  {
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
      int64_t old_v = sys->CSR_ESTAT;
 
-    /* Only IS[1:0] can be written */
+    /* When guest = 0, only IS[1:0] can be written.
+     * When guest = 1, ecode and esubcode of
+     * VM_LEVEL1 can be written by VM_LEVEL0.
+     */
      sys->CSR_ESTAT = deposit64(sys->CSR_ESTAT, 0, 2, val);
+    if (vm_level) {
+        sys->CSR_ESTAT = deposit64(sys->CSR_ESTAT, 2, 11,
+                                   extract64(val, 2, 11));
+        sys->CSR_ESTAT = deposit64(sys->CSR_ESTAT, 16, 15,
+                                   extract64(val, 16, 15));
+    }
      /*
       * Software interrupts (SWI0/SWI1) are latched in CSR.ESTAT.IS[1:0].
       * Make sure the CPU interrupt request state tracks the pending bits,
@@ -111,16 +127,17 @@ target_ulong helper_csrwr_estat(CPULoongArchState *env, target_ulong val)
       */
      if (sys->CSR_ESTAT != old_v) {
          bql_lock();
-        loongarch_cpu_update_irq(env_archcpu(env), false);
+        loongarch_cpu_update_irq(env_archcpu(env), vm_level);
          bql_unlock();
      }
 
      return old_v;
  }
 
-target_ulong helper_csrwr_asid(CPULoongArchState *env, target_ulong val)
+target_ulong helper_csrwr_asid(CPULoongArchState *env,
+                               target_ulong val, uint32_t vm_level)
  {
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
      int64_t old_v = sys->CSR_ASID;
 
      /* Only ASID filed of CSR_ASID can be written */
@@ -131,9 +148,10 @@ target_ulong helper_csrwr_asid(CPULoongArchState *env, target_ulong val)
      return old_v;
  }
 
-target_ulong helper_csrwr_tcfg(CPULoongArchState *env, target_ulong val)
+target_ulong helper_csrwr_tcfg(CPULoongArchState *env,
+                               target_ulong val, uint32_t vm_level)
  {
-    CPUTimerState *timer = env_timer(env);
+    CPUTimerState *timer = vm_level ? env_guest_timer(env) : env_timer(env);
      CPUSysState *sys = container_of(timer, CPUSysState, timer_state);
      int64_t old_v = sys->CSR_TCFG;
 
@@ -142,9 +160,10 @@ target_ulong helper_csrwr_tcfg(CPULoongArchState *env, target_ulong val)
      return old_v;
  }
 
-target_ulong helper_csrwr_ticlr(CPULoongArchState *env, target_ulong val)
+target_ulong helper_csrwr_ticlr(CPULoongArchState *env,
+                                target_ulong val, uint32_t vm_level)
  {
-    CPUTimerState *timer = env_timer(env);
+    CPUTimerState *timer = vm_level ? env_guest_timer(env) : env_timer(env);
      int64_t old_v = 0;
 
      if (val & 0x1) {
@@ -155,10 +174,11 @@ target_ulong helper_csrwr_ticlr(CPULoongArchState *env, target_ulong val)
      return old_v;
  }
 
-target_ulong helper_csrwr_pwcl(CPULoongArchState *env, target_ulong val)
+target_ulong helper_csrwr_pwcl(CPULoongArchState *env,
+                               target_ulong val, uint32_t vm_level)
  {
      uint8_t shift, ptbase;
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
      int64_t old_v = sys->CSR_PWCL;
 
      /*
@@ -180,18 +200,20 @@ target_ulong helper_csrwr_pwcl(CPULoongArchState *env, target_ulong val)
      return old_v;
  }
 
-target_ulong helper_csrwr_pwch(CPULoongArchState *env, target_ulong val)
+target_ulong helper_csrwr_pwch(CPULoongArchState *env,
+                               target_ulong val, uint32_t vm_level)
  {
-    uint8_t has_ptw;
-    CPUSysState *sys = env_sys(env);
+    uint8_t host_has_ptw;
+    CPUSysState *sys = get_sys(env, vm_level);
      int64_t old_v = sys->CSR_PWCH;
 
      val = FIELD_DP64(val, CSR_PWCH, RESERVE, 0);
-    has_ptw = FIELD_EX32(env->cpucfg[2], CPUCFG2, HPTW);
-    if (!has_ptw) {
+    //TODO: host can't read guest cpucfg
+    host_has_ptw = FIELD_EX32(env->cpucfg[2], CPUCFG2, HPTW);
+    if (!host_has_ptw) {
          val = FIELD_DP64(val, CSR_PWCH, HPTW_EN, 0);
      }
 
      sys->CSR_PWCH = val;
      return old_v;
- }
+}
target/loongarch/tcg/helper.h +11 -11
index a27b80b087..bce5f46dc2 100644
@@ -93,17 +93,17 @@ DEF_HELPER_1(rdtime_d, i64, env)
 
  #ifndef CONFIG_USER_ONLY
  /* CSRs helper */
-DEF_HELPER_1(csrrd_pgd, i64, env)
-DEF_HELPER_1(csrrd_cpuid, i64, env)
-DEF_HELPER_1(csrrd_tval, i64, env)
-DEF_HELPER_1(csrrd_msgir, i64, env)
-DEF_HELPER_2(csrwr_stlbps, i64, env, tl)
-DEF_HELPER_2(csrwr_estat, i64, env, tl)
-DEF_HELPER_2(csrwr_asid, i64, env, tl)
-DEF_HELPER_2(csrwr_tcfg, i64, env, tl)
-DEF_HELPER_2(csrwr_ticlr, i64, env, tl)
-DEF_HELPER_2(csrwr_pwcl, i64, env, tl)
-DEF_HELPER_2(csrwr_pwch, i64, env, tl)
+DEF_HELPER_2(csrrd_pgd, i64, env, i32)
+DEF_HELPER_2(csrrd_cpuid, i64, env, i32)
+DEF_HELPER_2(csrrd_tval, i64, env, i32)
+DEF_HELPER_2(csrrd_msgir, i64, env, i32)
+DEF_HELPER_3(csrwr_stlbps, i64, env, tl, i32)
+DEF_HELPER_3(csrwr_estat, i64, env, tl, i32)
+DEF_HELPER_3(csrwr_asid, i64, env, tl, i32)
+DEF_HELPER_3(csrwr_tcfg, i64, env, tl, i32)
+DEF_HELPER_3(csrwr_ticlr, i64, env, tl, i32)
+DEF_HELPER_3(csrwr_pwcl, i64, env, tl, i32)
+DEF_HELPER_3(csrwr_pwch, i64, env, tl, i32)
  DEF_HELPER_2(iocsrrd_b, i64, env, tl)
  DEF_HELPER_2(iocsrrd_h, i64, env, tl)
  DEF_HELPER_2(iocsrrd_w, i64, env, tl)
target/loongarch/tcg/insn_trans/trans_privileged.c.inc +19 -10
index 00e8f61e42..139dbd6e2a 100644
@@ -39,11 +39,14 @@ GEN_FALSE_TRANS(lddir)
  GEN_FALSE_TRANS(ertn)
  GEN_FALSE_TRANS(dbcl)
  GEN_FALSE_TRANS(idle)
+GEN_FALSE_TRANS(gcsrrd)
+GEN_FALSE_TRANS(gcsrwr)
+GEN_FALSE_TRANS(gcsrxchg)
 
  #else
 
-typedef void (*GenCSRRead)(TCGv dest, TCGv_ptr env);
-typedef void (*GenCSRWrite)(TCGv dest, TCGv_ptr env, TCGv src);
+typedef void (*GenCSRRead)(TCGv dest, TCGv_ptr env, TCGv_i32 vm_level);
+typedef void (*GenCSRWrite)(TCGv dest, TCGv_ptr env, TCGv src, TCGv_i32 vm_level);
 
  static bool check_plv(DisasContext *ctx)
  {
@@ -88,10 +91,14 @@ void loongarch_csr_translate_init(void)
  }
  #undef SET_CSR_FUNC
 
-static bool check_csr_flags(DisasContext *ctx, const CSRInfo *csr, bool write)
+static bool check_csr_flags(DisasContext *ctx, const CSRInfo *csr,
+                            bool write, int vm_level)
  {
      if ((csr->flags & CSRFL_READONLY) && write) {
-        return false;
+        /* host writes guest read-only CSRs is allowed */
+        if (is_guest_mmu_idx(ctx->mem_idx) || vm_level == VM_LEVEL0) {
+            return false;
+        }
      }
      if ((csr->flags & CSRFL_IO) && translator_io_start(&ctx->base)) {
          ctx->base.is_jmp = DISAS_EXIT_UPDATE;
@@ -101,7 +108,7 @@ static bool check_csr_flags(DisasContext *ctx, const CSRInfo *csr, bool write)
      return true;
  }
 
-static bool trans_csrrd(DisasContext *ctx, arg_csrrd *a)
+static bool trans_csrrd_impl(DisasContext *ctx, arg_r_csr *a, int vm_level)
  {
      TCGv dest;
      const CSRInfo *csr;
@@ -116,21 +123,29 @@ static bool trans_csrrd(DisasContext *ctx, arg_csrrd *a)
          /* CSR is undefined: read as 0. */
          dest = tcg_constant_tl(0);
      } else {
-        check_csr_flags(ctx, csr, false);
-        dest = gpr_dst(ctx, a->rd, EXT_NONE);
-        readfn = (GenCSRRead)csr->readfn;
-        if (readfn) {
-            readfn(dest, tcg_env);
+        check_csr_flags(ctx, csr, false, vm_level);
+        if (vm_level && (csr->flags & CSRFL_GSPR)) {
+            if (is_guest_mmu_idx(ctx->mem_idx)) {
+                generate_exception(ctx, EXCCODE_GSPR);
+                return true;
+            }
+            dest = tcg_constant_tl(0);
          } else {

target/loongarch/tcg/insn_trans/trans_privileged.c.inc static bool trans_csrrd(DisasContext *ctx, arg_csrrd *a) · line 141

Quoted 10 lines
target/loongarch/tcg/insn_trans/trans_privileged.c.inc +8 -2
-            offset = get_csr_offset(csr, 0);
-            tcg_gen_ld_tl(dest, tcg_env, offset);
+            dest = gpr_dst(ctx, a->rd, EXT_NONE);
+            readfn = (GenCSRRead)csr->readfn;
+            if (readfn) {
+                readfn(dest, tcg_env, tcg_constant_i32(vm_level));
+            } else {
+                offset = get_csr_offset(csr, vm_level);
+                tcg_gen_ld_tl(dest, tcg_env, offset);
+            }

Can this patch be split into three smaller verson? such as:
1. lvz support with csrrd/csrwr/csrxchg instruction.
2. lvz support with new gcsrrd/gcsrwr/gcsrxchg instruction.
3. Common API for csrrd/gcsrrd.
or
1. lvz support with csrrd/csrwr/csrxchg instruction.
2. Common API extraction for csrrd/gcsrr.
3. lvz support with new gcsrrd/gcsrwr/gcsrxchg instruction.

With the smaller versions, it is easier to review and find the potential issues.

Regards
Bibo Mao

Quoted 137 lines
target/loongarch/tcg/insn_trans/trans_privileged.c.inc +64 -8
          }
      }
      gen_set_gpr(a->rd, dest, EXT_NONE);
      return true;
  }
 
-static bool trans_csrwr(DisasContext *ctx, arg_csrwr *a)
+static bool trans_csrwr_impl(DisasContext *ctx, arg_r_csr *a, int vm_level)
  {
      TCGv dest, src1;
      const CSRInfo *csr;
@@ -146,18 +161,26 @@ static bool trans_csrwr(DisasContext *ctx, arg_csrwr *a)
          gen_set_gpr(a->rd, tcg_constant_tl(0), EXT_NONE);
          return true;
      }
-    if (!check_csr_flags(ctx, csr, true)) {
+    if (!check_csr_flags(ctx, csr, true, vm_level)) {
          /* CSR is readonly: trap. */
          return false;
      }
+    if (vm_level && (csr->flags & CSRFL_GSPR)) {
+        if (is_guest_mmu_idx(ctx->mem_idx)) {
+            generate_exception(ctx, EXCCODE_GSPR);
+            return true;
+        }
+        gen_set_gpr(a->rd, tcg_constant_tl(0), EXT_NONE);
+        return true;
+    }
      src1 = gpr_src(ctx, a->rd, EXT_NONE);
      writefn = (GenCSRWrite)csr->writefn;
      if (writefn) {
          dest = gpr_dst(ctx, a->rd, EXT_NONE);
-        writefn(dest, tcg_env, src1);
+        writefn(dest, tcg_env, src1, tcg_constant_i32(vm_level));
      } else {
          dest = tcg_temp_new();
-        offset = get_csr_offset(csr, 0);
+        offset = get_csr_offset(csr, vm_level);
          tcg_gen_ld_tl(dest, tcg_env, offset);
          tcg_gen_st_tl(src1, tcg_env, offset);
      }
@@ -165,7 +188,7 @@ static bool trans_csrwr(DisasContext *ctx, arg_csrwr *a)
      return true;
  }
 
-static bool trans_csrxchg(DisasContext *ctx, arg_csrxchg *a)
+static bool trans_csrxchg_impl(DisasContext *ctx, arg_rr_csr *a, int vm_level)
  {
      TCGv src1, mask, oldv, newv, temp;
      const CSRInfo *csr;
@@ -182,11 +205,20 @@ static bool trans_csrxchg(DisasContext *ctx, arg_csrxchg *a)
          return true;
      }
 
-    if (!check_csr_flags(ctx, csr, true)) {
+    if (!check_csr_flags(ctx, csr, true, vm_level)) {
          /* CSR is readonly: trap. */
          return false;
      }
 
+    if (vm_level && (csr->flags & CSRFL_GSPR)) {
+        if (is_guest_mmu_idx(ctx->mem_idx)) {
+            generate_exception(ctx, EXCCODE_GSPR);
+            return true;
+        }
+        gen_set_gpr(a->rd, tcg_constant_tl(0), EXT_NONE);
+        return true;
+    }
+
      /* So far only readonly csrs have readfn. */
      assert(csr->readfn == NULL);
 
@@ -196,7 +228,7 @@ static bool trans_csrxchg(DisasContext *ctx, arg_csrxchg *a)
      newv = tcg_temp_new();
      temp = tcg_temp_new();
 
-    offset = get_csr_offset(csr, 0);
+    offset = get_csr_offset(csr, vm_level);
      tcg_gen_ld_tl(oldv, tcg_env, offset);
      tcg_gen_and_tl(newv, src1, mask);
      tcg_gen_andc_tl(temp, oldv, mask);
@@ -204,7 +236,7 @@ static bool trans_csrxchg(DisasContext *ctx, arg_csrxchg *a)
 
      writefn = (GenCSRWrite)csr->writefn;
      if (writefn) {
-        writefn(oldv, tcg_env, newv);
+        writefn(oldv, tcg_env, newv, tcg_constant_i32(vm_level));
      } else {
          tcg_gen_st_tl(newv, tcg_env, offset);
      }
@@ -212,6 +244,45 @@ static bool trans_csrxchg(DisasContext *ctx, arg_csrxchg *a)
      return true;
  }
 
+static bool trans_csrrd(DisasContext *ctx, arg_csrrd *a)
+{
+    return trans_csrrd_impl(ctx, a, is_guest_mmu_idx(ctx->mem_idx));
+}
+
+static bool trans_csrwr(DisasContext *ctx, arg_csrwr *a)
+{
+    return trans_csrwr_impl(ctx, a, is_guest_mmu_idx(ctx->mem_idx));
+}
+
+static bool trans_csrxchg(DisasContext *ctx, arg_csrxchg *a)
+{
+    return trans_csrxchg_impl(ctx, a, is_guest_mmu_idx(ctx->mem_idx));
+}
+
+static bool trans_gcsrrd(DisasContext *ctx, arg_gcsrrd *a)
+{
+    if (!avail_LVZ(ctx) || is_guest_mmu_idx(ctx->mem_idx)) {
+        return false;
+    }
+    return trans_csrrd_impl(ctx, a, VM_LEVEL1);
+}
+
+static bool trans_gcsrwr(DisasContext *ctx, arg_gcsrwr *a)
+{
+    if (!avail_LVZ(ctx) || is_guest_mmu_idx(ctx->mem_idx)) {
+        return false;
+    }
+    return trans_csrwr_impl(ctx, a, VM_LEVEL1);
+}
+
+static bool trans_gcsrxchg(DisasContext *ctx, arg_gcsrxchg *a)
+{
+    if (!avail_LVZ(ctx) || is_guest_mmu_idx(ctx->mem_idx)) {
+        return false;
+    }
+    return trans_csrxchg_impl(ctx, a, VM_LEVEL1);
+}
+
  static bool gen_iocsrrd(DisasContext *ctx, arg_rr *a,
                          void (*func)(TCGv, TCGv_ptr, TCGv))
  {
#24 lore raw

Re: [PATCH v5 15/23] target/loongarch: add LVZ to invtlb

In reply to SignKirigami
Quoted 76 lines · SignKirigami

Add op 0x9, 0xa-0xf to instruction invtlb to support vm level 1.

---
  .../tcg/insn_trans/trans_privileged.c.inc     | 34 +++++++++++++
  target/loongarch/tcg/tlb_helper.c             | 51 ++++++++++++++++---
  2 files changed, 77 insertions(+), 8 deletions(-)
target/loongarch/tcg/insn_trans/trans_privileged.c.inc +34 -0
index 493ed7f24b..00e8f61e42 100644
@@ -328,6 +328,10 @@ static bool trans_invtlb(DisasContext *ctx, arg_invtlb *a)
          return false;
      }
 
+    if (!avail_LVZ(ctx) && a->imm > 0x6) {
+        return false;
+    }
+
      switch (a->imm) {
      case 0x0:
      case 0x1:
@@ -352,6 +356,36 @@ static bool trans_invtlb(DisasContext *ctx, arg_invtlb *a)
          gen_helper_invtlb_page_asid_or_g(tcg_env, rj, rk,
                                           tcg_constant_i32(0));
          break;
+    case 0x9:
+    case 0x10:
+    case 0x11:
+    case 0x12:
+    case 0x13:
+    case 0x14:
+    case 0x15:
+    case 0x16:
+        //TODO: refine 0x9-0x16
+        gen_helper_invtlb_all(tcg_env, rj, tcg_constant_i32(0),
+                              tcg_constant_i32(0));
+        break;
+    case 0xa:
+        gen_helper_invtlb_all_g(tcg_env, rj, tcg_constant_i32(1),
+                                tcg_constant_i32(1));
+        break;
+    case 0xb:
+        gen_helper_invtlb_all_g(tcg_env, rj, tcg_constant_i32(0),
+                                tcg_constant_i32(1));
+        break;
+    case 0xc:
+        gen_helper_invtlb_all_asid(tcg_env, rj, tcg_constant_i32(1));
+        break;
+    case 0xd:
+        gen_helper_invtlb_page_asid(tcg_env, rj, rk, tcg_constant_i32(1));
+        break;
+    case 0xe:
+        gen_helper_invtlb_page_asid_or_g(tcg_env, rj, rk,
+                                         tcg_constant_i32(1));
+        break;
      default:
          return false;
      }
target/loongarch/tcg/tlb_helper.c +7 -0
index 60c7193283..c02b7c2472 100644
@@ -599,9 +599,18 @@ void helper_tlbflush(CPULoongArchState *env, uint32_t vm_level)
  void helper_invtlb_all(CPULoongArchState *env, target_ulong info, uint32_t op,
                         uint32_t to_guest)
  {
+    uint16_t gid = to_guest ? (info & 0xff) : get_tgid(env);
+
+    if (to_guest && env_vm_level(env) == VM_LEVEL1) {
+        do_raise_exception(env, EXCCODE_IPE, GETPC());
+    }
+
+    to_guest |= env_vm_level(env) == VM_LEVEL1;

target/loongarch/tcg/tlb_helper.c void helper_tlbflush(CPULoongArchState *env, uint32_t vm_level) · line 616

Quoted 10 lines
target/loongarch/tcg/tlb_helper.c +4 -2
+
      for (int i = 0; i < LOONGARCH_TLB_MAX; i++) {
-        env->tlb[i].tlb_misc = FIELD_DP64(env->tlb[i].tlb_misc,
-                                          TLB_MISC, E, 0);
+        LoongArchTLB *tlb = &env->tlb[i];
+        if ((op == 0 && to_guest == 0) || tlb_entry_matches_gid(tlb, gid))
+            tlb->tlb_misc = FIELD_DP64(tlb->tlb_misc, TLB_MISC, E, 0);
      }
      tlb_flush(env_cpu(env));
  }

if gtlb instruction is removed, I think helper_invtlb_xxx APIs can be simplified and remove parameter to_guest.

Regards
Bibo Mao

Quoted 86 lines
target/loongarch/tcg/tlb_helper.c +32 -6
@@ -609,11 +618,19 @@ void helper_invtlb_all(CPULoongArchState *env, target_ulong info, uint32_t op,
  void helper_invtlb_all_g(CPULoongArchState *env, target_ulong info, uint32_t g,
                           uint32_t to_guest)
  {
+    uint16_t gid = to_guest ? (info & 0xff) : get_tgid(env);
+
+    if (to_guest && env_vm_level(env) == VM_LEVEL1) {
+        do_raise_exception(env, EXCCODE_IPE, GETPC());
+    }
+
+    to_guest |= env_vm_level(env) == VM_LEVEL1;
+
      for (int i = 0; i < LOONGARCH_TLB_MAX; i++) {
          LoongArchTLB *tlb = &env->tlb[i];
          uint8_t tlb_g = FIELD_EX64(tlb->tlb_entry0, TLBENTRY, G);
 
-        if (tlb_g == g) {
+        if (tlb_g == g && tlb_entry_matches_gid(tlb, gid)) {
              tlb->tlb_misc = FIELD_DP64(tlb->tlb_misc, TLB_MISC, E, 0);
          }
      }
@@ -624,13 +641,19 @@ void helper_invtlb_all_asid(CPULoongArchState *env, target_ulong info,
                              uint32_t to_guest)
  {
      uint16_t asid = info & R_CSR_ASID_ASID_MASK;
+    uint16_t gid = to_guest ? ((info >> 16) & 0xff) : get_tgid(env);
+
+    if (to_guest) {
+        do_raise_exception(env, EXCCODE_IPE, GETPC());
+    }
+    to_guest |= env_vm_level(env);
 
      for (int i = 0; i < LOONGARCH_TLB_MAX; i++) {
          LoongArchTLB *tlb = &env->tlb[i];
          uint8_t tlb_g = FIELD_EX64(tlb->tlb_entry0, TLBENTRY, G);
          uint16_t tlb_asid = FIELD_EX64(tlb->tlb_misc, TLB_MISC, ASID);
 
-        if (!tlb_g && (tlb_asid == asid)) {
+        if (!tlb_g && tlb_asid == asid && tlb_entry_matches_gid(tlb, gid)) {
              tlb->tlb_misc = FIELD_DP64(tlb->tlb_misc, TLB_MISC, E, 0);
          }
      }
@@ -640,12 +663,19 @@ void helper_invtlb_all_asid(CPULoongArchState *env, target_ulong info,
  void helper_invtlb_page_asid(CPULoongArchState *env, target_ulong info,
                               target_ulong addr, uint32_t to_guest)
  {
-    int asid = info & 0x3ff;
+
+    uint16_t asid = info & R_CSR_ASID_ASID_MASK;
+    uint16_t gid = to_guest ? ((info >> 16) & 0xff) : get_tgid(env);
      const LoongArchTLB *tlb;
      tlb_match func;
 
+    if (to_guest && env_vm_level(env)) {
+        do_raise_exception(env, EXCCODE_IPE, GETPC());
+    }
+    to_guest |= env_vm_level(env);
+
      func = tlb_match_asid;
-    tlb = loongarch_tlb_search_cb(env, addr, asid, func, to_guest, 0);
+    tlb = loongarch_tlb_search_cb(env, addr, asid, func, to_guest, gid);
      if (tlb) {
          invalidate_tlb(env, tlb - env->tlb, to_guest);
      }
@@ -654,13 +684,18 @@ void helper_invtlb_page_asid(CPULoongArchState *env, target_ulong info,
  void helper_invtlb_page_asid_or_g(CPULoongArchState *env, target_ulong info,
                                    target_ulong addr, uint32_t to_guest)
  {
-    int asid = info & 0x3ff;
+    uint16_t asid = info & R_CSR_ASID_ASID_MASK;
+    uint16_t gid = to_guest ? ((info >> 16) & 0xff) : get_tgid(env);
      const LoongArchTLB *tlb;
      tlb_match func;
 
      func = tlb_match_any;
+    if (to_guest) {
+        do_raise_exception(env, EXCCODE_IPE, GETPC());
+    }
+    to_guest |= env_vm_level(env);
      tlb = loongarch_tlb_search_cb(env, addr, asid, func,
-                                  to_guest, 0);
+                                  to_guest, gid);
      if (tlb) {
          invalidate_tlb(env, tlb - env->tlb, to_guest);
      }
#25 lore raw

Re: [PATCH v5 15/23] target/loongarch: add LVZ to invtlb

Quoted 86 lines · Bibo Mao
Quoted 83 lines · SignKirigami

Add op 0x9, 0xa-0xf to instruction invtlb to support vm level 1.

---
  .../tcg/insn_trans/trans_privileged.c.inc     | 34 +++++++++++++ target/loongarch/tcg/tlb_helper.c             | 51 ++++++++++++++++---

  2 files changed, 77 insertions(+), 8 deletions(-)
target/loongarch/tcg/insn_trans/trans_privileged.c.inc +4 -0
index 493ed7f24b..00e8f61e42 100644
@@ -328,6 +328,10 @@ static bool trans_invtlb(DisasContext *ctx, 
 arg_invtlb *a)
           return false;
       }
+    if (!avail_LVZ(ctx) && a->imm > 0x6) {
+        return false;
+    }
+
       switch (a->imm) {

      case 0x0:
      case 0x1:

target/loongarch/tcg/insn_trans/trans_privileged.c.inc +30 -0
@@ -352,6 +356,36 @@ static bool trans_invtlb(DisasContext *ctx, 
 arg_invtlb *a)
           gen_helper_invtlb_page_asid_or_g(tcg_env, rj, rk,
                                            tcg_constant_i32(0));
           break;
+    case 0x9:
+    case 0x10:
+    case 0x11:
+    case 0x12:
+    case 0x13:
+    case 0x14:
+    case 0x15:
+    case 0x16:
+        //TODO: refine 0x9-0x16
+        gen_helper_invtlb_all(tcg_env, rj, tcg_constant_i32(0),
+                              tcg_constant_i32(0));
+        break;
+    case 0xa:
+        gen_helper_invtlb_all_g(tcg_env, rj, tcg_constant_i32(1),
+                                tcg_constant_i32(1));
+        break;
+    case 0xb:
+        gen_helper_invtlb_all_g(tcg_env, rj, tcg_constant_i32(0),
+                                tcg_constant_i32(1));
+        break;
+    case 0xc:
+        gen_helper_invtlb_all_asid(tcg_env, rj, tcg_constant_i32(1));
+        break;
+    case 0xd:
+        gen_helper_invtlb_page_asid(tcg_env, rj, rk, 
 tcg_constant_i32(1));
+        break;
+    case 0xe:
+        gen_helper_invtlb_page_asid_or_g(tcg_env, rj, rk,
+                                         tcg_constant_i32(1));
+        break;

      default:
          return false;
      }

target/loongarch/tcg/tlb_helper.c +8 -2
index 60c7193283..c02b7c2472 100644
@@ -599,9 +599,18 @@ void helper_tlbflush(CPULoongArchState *env, 
 uint32_t vm_level)
   void helper_invtlb_all(CPULoongArchState *env, target_ulong info, uint32_t op,
                          uint32_t to_guest)
   {
+    uint16_t gid = to_guest ? (info & 0xff) : get_tgid(env);
+
+    if (to_guest && env_vm_level(env) == VM_LEVEL1) {
+        do_raise_exception(env, EXCCODE_IPE, GETPC());
+    }
+
+    to_guest |= env_vm_level(env) == VM_LEVEL1;
+
       for (int i = 0; i < LOONGARCH_TLB_MAX; i++) {
-        env->tlb[i].tlb_misc = FIELD_DP64(env->tlb[i].tlb_misc,
-                                          TLB_MISC, E, 0);
Quoted 8 lines
Quoted 7 lines
target/loongarch/tcg/tlb_helper.c +3 -0
+        LoongArchTLB *tlb = &env->tlb[i];
+        if ((op == 0 && to_guest == 0) || tlb_entry_matches_gid(tlb, 
 gid))
+            tlb->tlb_misc = FIELD_DP64(tlb->tlb_misc, TLB_MISC, E, 0);
       }

      tlb_flush(env_cpu(env));
  }

if gtlb instruction is removed, I think helper_invtlb_xxx APIs can be simplified and remove parameter to_guest.

Sorry, to_guest is useful even if gtlb instruction is removed. I think that parameter to_guest should be changed as vm_level, the same name API set_sys(CPULoongArchState *env, int vm_level).

Also I think that this patch should be split as smaller parts like comments with csrrd instruction, such as:
1. Existing invtlb() instruction with LVZ support.
2. More op code with invtlb() instruction.
3. Add common API or new parameter vm_level with helper_intlb_xxx
function.

Regards
Bibo Mao

Quoted 89 lines

Regards
Bibo Mao

Quoted 86 lines
target/loongarch/tcg/tlb_helper.c +32 -6
@@ -609,11 +618,19 @@ void helper_invtlb_all(CPULoongArchState *env, 
 target_ulong info, uint32_t op,
   void helper_invtlb_all_g(CPULoongArchState *env, target_ulong info, uint32_t g,
                            uint32_t to_guest)
   {
+    uint16_t gid = to_guest ? (info & 0xff) : get_tgid(env);
+
+    if (to_guest && env_vm_level(env) == VM_LEVEL1) {
+        do_raise_exception(env, EXCCODE_IPE, GETPC());
+    }
+
+    to_guest |= env_vm_level(env) == VM_LEVEL1;
+
       for (int i = 0; i < LOONGARCH_TLB_MAX; i++) {
           LoongArchTLB *tlb = &env->tlb[i];
           uint8_t tlb_g = FIELD_EX64(tlb->tlb_entry0, TLBENTRY, G);
-        if (tlb_g == g) {
+        if (tlb_g == g && tlb_entry_matches_gid(tlb, gid)) {
               tlb->tlb_misc = FIELD_DP64(tlb->tlb_misc, TLB_MISC, E, 0);
           }
       }
@@ -624,13 +641,19 @@ void helper_invtlb_all_asid(CPULoongArchState 
 *env, target_ulong info,
                               uint32_t to_guest)
   {
       uint16_t asid = info & R_CSR_ASID_ASID_MASK;
+    uint16_t gid = to_guest ? ((info >> 16) & 0xff) : get_tgid(env);
+
+    if (to_guest) {
+        do_raise_exception(env, EXCCODE_IPE, GETPC());
+    }
+    to_guest |= env_vm_level(env);
       for (int i = 0; i < LOONGARCH_TLB_MAX; i++) {
           LoongArchTLB *tlb = &env->tlb[i];
           uint8_t tlb_g = FIELD_EX64(tlb->tlb_entry0, TLBENTRY, G);
           uint16_t tlb_asid = FIELD_EX64(tlb->tlb_misc, TLB_MISC, ASID);
-        if (!tlb_g && (tlb_asid == asid)) {
+        if (!tlb_g && tlb_asid == asid && tlb_entry_matches_gid(tlb, 
 gid)) {
               tlb->tlb_misc = FIELD_DP64(tlb->tlb_misc, TLB_MISC, E, 0);
           }
       }
@@ -640,12 +663,19 @@ void helper_invtlb_all_asid(CPULoongArchState 
 *env, target_ulong info,
   void helper_invtlb_page_asid(CPULoongArchState *env, target_ulong info,
                                target_ulong addr, uint32_t to_guest)
   {
-    int asid = info & 0x3ff;
+
+    uint16_t asid = info & R_CSR_ASID_ASID_MASK;
+    uint16_t gid = to_guest ? ((info >> 16) & 0xff) : get_tgid(env);
       const LoongArchTLB *tlb;
       tlb_match func;
+    if (to_guest && env_vm_level(env)) {
+        do_raise_exception(env, EXCCODE_IPE, GETPC());
+    }
+    to_guest |= env_vm_level(env);
+
       func = tlb_match_asid;
-    tlb = loongarch_tlb_search_cb(env, addr, asid, func, to_guest, 0);
+    tlb = loongarch_tlb_search_cb(env, addr, asid, func, to_guest, gid);
       if (tlb) {
           invalidate_tlb(env, tlb - env->tlb, to_guest);
       }
@@ -654,13 +684,18 @@ void helper_invtlb_page_asid(CPULoongArchState 
 *env, target_ulong info,
   void helper_invtlb_page_asid_or_g(CPULoongArchState *env, target_ulong info,
                                     target_ulong addr, uint32_t to_guest)
   {
-    int asid = info & 0x3ff;
+    uint16_t asid = info & R_CSR_ASID_ASID_MASK;
+    uint16_t gid = to_guest ? ((info >> 16) & 0xff) : get_tgid(env);
       const LoongArchTLB *tlb;
       tlb_match func;
       func = tlb_match_any;
+    if (to_guest) {
+        do_raise_exception(env, EXCCODE_IPE, GETPC());
+    }
+    to_guest |= env_vm_level(env);
       tlb = loongarch_tlb_search_cb(env, addr, asid, func,
-                                  to_guest, 0);
+                                  to_guest, gid);
       if (tlb) {
           invalidate_tlb(env, tlb - env->tlb, to_guest);
       }
#13 lore raw

Re: [PATCH v5 07/23] target/loongarch: add LVZ guest interrupt state

In reply to Bibo Mao

On 2026年10月10日星期六 中国标准时间 12:03:33,Bibo Mao wrote:

Quoted 146 lines · SignKirigami
Quoted 144 lines

Route configured hardware interrupts into the guest CSR bank, track guest interrupt requests separately, and expose guest pending and enable checks to the TCG runtime.

---
  target/loongarch/cpu.c            | 35 +++++++++++++++++++--------
  target/loongarch/cpu.h            |  3 +++
  target/loongarch/internals.h      |  4 ++--
  target/loongarch/tcg/csr_helper.c |  2 +-
  target/loongarch/tcg/tcg_cpu.c    | 39 ++++++++++++++++++++++---------
  5 files changed, 59 insertions(+), 24 deletions(-)
target/loongarch/cpu.c +25 -10
index 664763137d..5cef63a778 100644
@@ -58,16 +58,20 @@ static vaddr loongarch_cpu_get_pc(CPUState *cs)
  #ifndef CONFIG_USER_ONLY
  #include "hw/loongarch/virt.h"
 
-void loongarch_cpu_update_irq(LoongArchCPU *cpu)
+void loongarch_cpu_update_irq(LoongArchCPU *cpu, int vm_level)
  {
      CPULoongArchState *env = &cpu->env;
      CPUState *cs = CPU(cpu);
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
+    int interrupt = vm_level ? CPU_INTERRUPT_GUEST : CPU_INTERRUPT_HARD;
 
+    if (vm_level == VM_LEVEL1 && env_vm_level(env) != VM_LEVEL1) {
+        return;
+    }
      if (FIELD_EX64(sys->CSR_ESTAT, CSR_ESTAT, IS)) {
-        cpu_interrupt(cs, CPU_INTERRUPT_HARD);
+        cpu_interrupt(cs, interrupt);
      } else {
-        cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
+        cpu_reset_interrupt(cs, interrupt);
      }
  }
 
@@ -75,21 +79,28 @@ static void loongarch_cpu_self_set_irq(CPUState *cs, run_on_cpu_data data)
  {
      LoongArchCPU *cpu = LOONGARCH_CPU(cs);
      CPULoongArchState *env = cpu_env(cs);
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys;
+    int vm_level = 0;
      int irq, level;
 
      irq = data.host_int & ~BIT(31);
      level = (data.host_int >> 31) & 1;
+    if (irq >= N_IRQS) {
+        irq -= N_IRQS;
+        vm_level = 1;
+    }
+    sys = get_sys(env, vm_level);
      sys->CSR_ESTAT = deposit64(sys->CSR_ESTAT, irq, 1, level != 0);
-    loongarch_cpu_update_irq(cpu);
+    loongarch_cpu_update_irq(cpu, vm_level);
  }
 
  void loongarch_cpu_set_irq(void *opaque, int irq, int level)
  {
      LoongArchCPU *cpu = opaque;
      CPUState *cs = CPU(cpu);
+    CPULoongArchState *env = cpu_env(cs);
 
-    if (irq < 0 || irq >= N_IRQS) {
+    if (irq < 0 || irq >= (cpu_has_lvz(env) ? N_VIRQS : N_IRQS)) {
          return;
      }
 
@@ -103,11 +114,11 @@ void loongarch_cpu_set_irq(void *opaque, int irq, int level)
  }
 
  /* Check if there is pending and not masked out interrupt */
-bool cpu_loongarch_hw_interrupts_pending(CPULoongArchState *env)
+bool cpu_loongarch_hw_interrupts_pending(CPULoongArchState *env, int vm_level)
  {
      uint32_t pending;
      uint32_t status;
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
 
      pending = FIELD_EX64(sys->CSR_ESTAT, CSR_ESTAT, IS);
      status  = FIELD_EX64(sys->CSR_ECFG, CSR_ECFG, LIE);
@@ -122,7 +133,11 @@ bool loongarch_cpu_has_work(CPUState *cs)
      bool has_work = false;
 
      if (cpu_test_interrupt(cs, CPU_INTERRUPT_HARD) &&
-        cpu_loongarch_hw_interrupts_pending(cpu_env(cs))) {
+        cpu_loongarch_hw_interrupts_pending(cpu_env(cs), VM_LEVEL0)) {
+        has_work = true;
+    }
+    if (cpu_test_interrupt(cs, CPU_INTERRUPT_GUEST) &&
+        cpu_loongarch_hw_interrupts_pending(cpu_env(cs), VM_LEVEL1)) {
          has_work = true;
      }
 
target/loongarch/cpu.h +3 -0
index 9eea1badaf..8939dbc748 100644
@@ -20,6 +20,8 @@
  #include "cpu-csr.h"
  #include "cpu-qom.h"
 
+#define CPU_INTERRUPT_GUEST CPU_INTERRUPT_TGT_EXT_0
+
  #define FCSR0_M1    0x1f         /* FCSR1 mask, Enables */
  #define FCSR0_M2    0x1f1f0000   /* FCSR2 mask, Cause and Flags */
  #define FCSR0_M3    0x300        /* FCSR3 mask, Round Mode */
@@ -242,6 +244,7 @@ extern const char * const fregnames[32];
  #define IRQ_TIMER   11
  #define IRQ_IPI     12
  #define INT_DMSI    14
+#define N_VIRQS     (N_IRQS * 2)
 
  #define MAX_PERF_EVENTS        16
  #define LOONGARCH_STLB         2048 /* 2048 STLB */
target/loongarch/internals.h +2 -2
index ff956823fd..c11cefc4ee 100644
@@ -31,7 +31,7 @@ void restore_fp_status(CPULoongArchState *env);
  #ifndef CONFIG_USER_ONLY
  extern const VMStateDescription vmstate_loongarch_cpu;
 
-void loongarch_cpu_update_irq(LoongArchCPU *cpu);
+void loongarch_cpu_update_irq(LoongArchCPU *cpu, int vm_level);
  void loongarch_cpu_set_irq(void *opaque, int irq, int level);
 
  void cpu_loongarch_timer_cb(void *opaque);
@@ -39,7 +39,7 @@ uint64_t cpu_loongarch_get_timer_counter(CPUTimerState *timer);
  uint64_t cpu_loongarch_get_timer_ticks(CPUTimerState *timer);
  void cpu_loongarch_set_timer_config(CPUTimerState *timer, uint64_t value);
  bool loongarch_cpu_has_work(CPUState *cs);
-bool cpu_loongarch_hw_interrupts_pending(CPULoongArchState *env);
+bool cpu_loongarch_hw_interrupts_pending(CPULoongArchState *env, int vm_level);
  #endif /* !CONFIG_USER_ONLY */
 
Quoted 12 lines

target/loongarch/tcg/csr_helper.c target_ulong helper_csrwr_estat(CPULoongArchState *env, target_ulong val) · line 114

Quoted 11 lines
target/loongarch/internals.h +0 -0
  uint64_t read_fcc(CPULoongArchState *env);
target/loongarch/tcg/csr_helper.c +1 -1
index 1a6919417f..ecfba63228 100644
@@ -111,7 +111,7 @@ target_ulong helper_csrwr_estat(CPULoongArchState *env, target_ulong val)
       */
      if (sys->CSR_ESTAT != old_v) {
          bql_lock();
-        loongarch_cpu_update_irq(env_archcpu(env));
+        loongarch_cpu_update_irq(env_archcpu(env), false);

why is it false, I think it should be current VM_LEVEL.

Because helper for gcsr has not been implied yet. Following patch will add vm_level argument to specify whether helper uses host or guest csr.
Regards
SignKirigami

Quoted 74 lines
Quoted 13 lines
target/loongarch/tcg/csr_helper.c +0 -0
          bql_unlock();
      }
 
target/loongarch/tcg/tcg_cpu.c +0 -1
index b60d9e7cf8..101fe6fc7a 100644
@@ -238,30 +238,47 @@ static void loongarch_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
      }
  }
 
-static inline bool cpu_loongarch_hw_interrupts_enabled(CPULoongArchState *env)

target/loongarch/tcg/tcg_cpu.c static void loongarch_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr, · line 248

Quoted 10 lines
target/loongarch/tcg/tcg_cpu.c +4 -2
+static inline bool cpu_loongarch_hw_interrupts_enabled(CPULoongArchState *env, int vm_level)
  {
      bool ret = 0;
-    CPUSysState *sys = env_sys(env);
+    CPUSysState *sys = get_sys(env, vm_level);
+    CPUSysState *host = get_sys(env, VM_LEVEL0);
 
      ret = (FIELD_EX64(sys->CSR_CRMD, CSR_CRMD, IE) &&
-          !(FIELD_EX64(sys->CSR_DBG, CSR_DBG, DST)));
+          !(FIELD_EX64(host->CSR_DBG, CSR_DBG, DST)));

why checking CSR_DBG of host here, it is a little strange.

Regards
Bibo Mao

Quoted 45 lines
target/loongarch/tcg/tcg_cpu.c +24 -8
      return ret;
  }
 
  static bool loongarch_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
  {
+    CPULoongArchState *env = cpu_env(cs);
+    bool has_interrupt = false;
+
      if (interrupt_request & CPU_INTERRUPT_HARD) {
-        CPULoongArchState *env = cpu_env(cs);
-
-        if (cpu_loongarch_hw_interrupts_enabled(env) &&
-            cpu_loongarch_hw_interrupts_pending(env)) {
-            /* Raise it */
-            cs->exception_index = EXCCODE_INT;
-            loongarch_cpu_do_interrupt(cs);
-            return true;
+
+        if (cpu_loongarch_hw_interrupts_enabled(env, VM_LEVEL0) &&
+            cpu_loongarch_hw_interrupts_pending(env, VM_LEVEL0)) {
+            if (env_vm_level(env) != VM_LEVEL0) {
+                trigger_vm_exit(env);
+            }
+            has_interrupt = true;
          }
+    } else if (interrupt_request & CPU_INTERRUPT_GUEST) {
+        if (cpu_loongarch_hw_interrupts_enabled(env, VM_LEVEL1) &&
+            cpu_loongarch_hw_interrupts_pending(env, VM_LEVEL1) &&
+            env_vm_level(env) == VM_LEVEL1) {
+            has_interrupt = true;
+        }
+    }
+
+    if (has_interrupt) {
+        /* Raise it */
+        cs->exception_index = EXCCODE_INT;
+        loongarch_cpu_do_interrupt(cs);
+        return true;
      }
+
      return false;
  }