target/loongarch: add LVZ support for TCG
v2 cover target/loongarch: 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 (4):
target/loongarch: Add LVZ feature definitions and CPU structures target/loongarch: Add guest CSR table and VM-level CSR infrastructure target/loongarch: Add LVZ MMU, TCG infrastructure and guest support target/loongarch: Add LVZ instruction decode, translation and
disassembly
target/loongarch/arch_dump.c | 6 +- target/loongarch/cpu-csr.h | 42 ++ target/loongarch/cpu-mmu.h | 39 +- target/loongarch/cpu.c | 279 ++++++--- target/loongarch/cpu.h | 62 +- target/loongarch/cpu_helper.c | 87 ++- target/loongarch/csr.c | 124 +++- target/loongarch/csr.h | 12 +- target/loongarch/disas.c | 16 + target/loongarch/insns.decode | 14 + target/loongarch/internals.h | 8 +- target/loongarch/machine.c | 125 +++- target/loongarch/tcg/constant_timer.c | 74 ++- target/loongarch/tcg/csr_helper.c | 136 ++++- target/loongarch/tcg/helper.h | 30 +- .../tcg/insn_trans/trans_extra.c.inc | 2 +- .../tcg/insn_trans/trans_privileged.c.inc | 353 ++++++++++- target/loongarch/tcg/op_helper.c | 45 +- target/loongarch/tcg/tcg_cpu.c | 65 +- target/loongarch/tcg/tcg_loongarch.h | 3 +- target/loongarch/tcg/tlb_helper.c | 577 +++++++++++++----- target/loongarch/tcg/translate.c | 6 +- target/loongarch/translate.h | 2 + 23 files changed, 1747 insertions(+), 360 deletions(-)
Signature · 1 line
2.52.0
PATCH v2 3/4 target/loongarch: Add LVZ MMU, TCG infrastructure and guest support
Implement two-stage MMU translation with guest/host page table walk support. Add guest TLB operations (gtlbwr, gtlbfill, gtlbsrch, gtlbrd, gtlbclr, gtlbflush) and extend invtlb with LVZ guest variants. Add guest interrupt delivery, constant timer callbacks, and VM entry/exit logic via ertn/hvcl/gspr. Implement guest CSR read/write helpers and LVZ-aware interrupt controller. Extend TCG CPU state with guest mode support and migration for LVZ guest state and TLB banks.
--- target/loongarch/cpu-mmu.h | 39 +- target/loongarch/cpu.c | 279 ++++++--- target/loongarch/cpu_helper.c | 87 ++- target/loongarch/internals.h | 8 +- target/loongarch/machine.c | 125 +++- target/loongarch/tcg/constant_timer.c | 74 ++- target/loongarch/tcg/csr_helper.c | 136 ++++- target/loongarch/tcg/helper.h | 30 +- .../tcg/insn_trans/trans_privileged.c.inc | 64 +- target/loongarch/tcg/op_helper.c | 45 +- target/loongarch/tcg/tcg_cpu.c | 65 +- target/loongarch/tcg/tcg_loongarch.h | 3 +- target/loongarch/tcg/tlb_helper.c | 577 +++++++++++++----- 13 files changed, 1205 insertions(+), 327 deletions(-)
target/loongarch/cpu-mmu.h +29 -10
@@ -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 {
@@ -30,18 +38,20 @@ 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,bool guest) { -CPUSysState *sys =env_sys (env); +CPUSysState *sys = &env->sys_states[guest ?LOONGARCH_VM_LEVEL_GUEST : +LOONGARCH_VM_LEVEL_HOST ];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, +bool guest) {uint8_t present; -if (cpu_has_ptw (env)) { +if (cpu_has_ptw (env, guest)) { present =FIELD_EX64 (entry,TLBENTRY ,P ); }else { present =FIELD_EX64 (entry,TLBENTRY ,V );
@@ -50,11 +60,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, +bool guest) {uint8_t writable; -if (cpu_has_ptw (env)) { +if (cpu_has_ptw (env, guest)) { writable =FIELD_EX64 (entry,TLBENTRY ,W ); }else { writable =FIELD_EX64 (entry,TLBENTRY ,D );
@@ -91,14 +102,22 @@ 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,bool guest);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,bool guest, +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,bool guest); +hwaddr loongarch_get_host_address (CPULoongArchState *env,hwaddr gpa, +uintptr_t retaddr); +TLBRet loongarch_map_host_address (CPULoongArchState *env,MMUContext *context, +MMUAccessType access_type,uintptr_t retaddr); +TLBRet loongarch_map_address (CPULoongArchState *env,MMUContext *context, +MMUAccessType access_type,int mmu_idx, +int is_debug,bool guest,uintptr_t retaddr);hwaddr loongarch_cpu_get_phys_addr_debug (CPUState *cpu,vaddr addr);uint64_t loongarch_palen_mask (CPULoongArchState *env);
target/loongarch/cpu.c +211 -68
421 lines not on this page · raw message
target/loongarch/cpu_helper.c +56 -31
204 lines not on this page · raw message
target/loongarch/internals.h +6 -2
@@ -32,14 +32,18 @@ void restore_fp_status(CPULoongArchState *env);extern const VMStateDescription vmstate_loongarch_cpu;void loongarch_cpu_set_irq (void *opaque,int irq,int level); +void loongarch_cpu_set_irq_guest (void *opaque,int irq,int level);void loongarch_constant_timer_cb (void *opaque); +void loongarch_constant_timer_cb_guest (void *opaque);uint64_t cpu_loongarch_get_constant_timer_counter (LoongArchCPU *cpu); -uint64_t cpu_loongarch_get_constant_timer_ticks (LoongArchCPU *cpu); +uint64_t cpu_loongarch_get_constant_timer_ticks (LoongArchCPU *cpu,bool guest); +void cpu_loongarch_set_guest_timer (LoongArchCPU *cpu,bool on);void cpu_loongarch_store_constant_timer_config (LoongArchCPU *cpu, -uint64_t value); +uint64_t value,bool guest);bool loongarch_cpu_has_work (CPUState *cs);bool cpu_loongarch_hw_interrupts_pending (CPULoongArchState *env); +bool loongarch_guest_has_interrupt (CPULoongArchState *env);#endif /* !CONFIG_USER_ONLY */ uint64_t read_fcc (CPULoongArchState *env);
target/loongarch/machine.c +124 -1
154 lines not on this page · raw message
target/loongarch/tcg/constant_timer.c +64 -10
110 lines not on this page · raw message
target/loongarch/tcg/csr_helper.c +134 -2
172 lines not on this page · raw message
target/loongarch/tcg/helper.h +24 -6
@@ -14,7 +14,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)
@@ -98,14 +98,23 @@ DEF_HELPER_1(rdtime_d, i64, env)#ifndef CONFIG_USER_ONLY /* CSRs helper */ DEF_HELPER_1(csrrd_pgd, i64, env) +DEF_HELPER_1(gcsrrd_pgd, i64, env) DEF_HELPER_1(csrrd_cpuid, i64, env) DEF_HELPER_1(csrrd_tval, i64, env) +DEF_HELPER_1(gcsrrd_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(gcsrwr_estat, i64, env, tl) DEF_HELPER_2(csrwr_asid, i64, env, tl) +DEF_HELPER_2(gcsrwr_asid, i64, env, tl) DEF_HELPER_2(csrwr_tcfg, i64, env, tl) +DEF_HELPER_2(gcsrwr_tcfg, i64, env, tl) DEF_HELPER_2(csrwr_ticlr, i64, env, tl) +DEF_HELPER_2(gcsrwr_ticlr, i64, env, tl) +DEF_HELPER_2(csrwr_gstat, i64, env, tl) +DEF_HELPER_2(csrwr_gtlbc, i64, env, tl) +DEF_HELPER_2(csrwr_gintc, i64, env, tl) DEF_HELPER_2(csrwr_pwcl, i64, env, tl) DEF_HELPER_2(csrwr_pwch, i64, env, tl)DEF_HELPER_2 (iocsrrd_b, i64, env, tl)
@@ -124,16 +133,25 @@ 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_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_1(gtlbwr, void, env) +DEF_HELPER_1(gtlbfill, void, env) +DEF_HELPER_1(gtlbsrch, void, env) +DEF_HELPER_1(gtlbrd, void, env) +DEF_HELPER_1(gtlbclr, void, env) +DEF_HELPER_1(gtlbflush, void, env) DEF_HELPER_4(lddir, tl, env, tl, i32, i32) DEF_HELPER_4(ldpte, void, env, tl, tl, i32) DEF_HELPER_1(ertn, void, env) DEF_HELPER_1(idle, void, env) +DEF_HELPER_2(hvcl, void, env, i32) +DEF_HELPER_1(gspr, void, env)#endif /* LoongArch LSX */
target/loongarch/tcg/insn_trans/trans_privileged.c.inc +51 -13
76 lines not on this page · raw message
target/loongarch/tcg/op_helper.c +41 -4
89 lines not on this page · raw message
target/loongarch/tcg/tcg_cpu.c +50 -15
141 lines not on this page · raw message
target/loongarch/tcg/tcg_loongarch.h +2 -1
@@ -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, +bool guest);#endif /* TARGET_LOONGARCH_TCG_LOONGARCH_H */
target/loongarch/tcg/tlb_helper.c +413 -164
1,055 lines not on this page · raw message
Signature · 1 line
2.52.0
PATCH v2 1/4 target/loongarch: Add LVZ feature definitions and CPU structures
In reply to SignKirigamiAdd LVZ (LoongArch Virtualization) CSR definitions to cpu-csr.h including guest status/configuration/interrupt/timer/TLB CSRs. Add VM level constants, guest MMU indices, guest exception codes (GSPR, HVC, GCSC, GCHC), guest interrupt type, and utility functions for VM-level switching. Extend CPUSysState with LVZ control registers and add lvz/guest_timer fields to ArchCPU. Define HW_FLAGS_GUEST_MODE for translation block guest flag and add avail_LVZ/guest_mode to translation context.
--- target/loongarch/cpu-csr.h | 42 ++++++++++++++++++++++++ target/loongarch/cpu.h | 62 +++++++++++++++++++++++++++++++++++- target/loongarch/translate.h | 2 ++ 3 files changed, 105 insertions(+), 1 deletion(-)
target/loongarch/cpu-csr.h +42 -0
@@ -180,11 +180,13 @@ FIELD(CSR_TLBREHI_64, VPPN, 13, 35)#define LOONGARCH_CSR_TLBRPRMD 0x8f/* TLB refill mode info */ FIELD(CSR_TLBRPRMD, PPLV, 0, 2) FIELD(CSR_TLBRPRMD, PIE, 2, 1) +FIELD(CSR_TLBRPRMD, PGM, 3, 1)FIELD (CSR_TLBRPRMD ,PWE ,4 ,1 ) /* Machine Error CSRs */#define LOONGARCH_CSR_MERRCTL 0x90/* ERRCTL */ FIELD(CSR_MERRCTL, ISMERR, 0, 1) +FIELD(CSR_MERRCTL, PGM, 5, 1)#define LOONGARCH_CSR_MERRINFO1 0x91#define LOONGARCH_CSR_MERRINFO2 0x92#define LOONGARCH_CSR_MERRENTRY 0x93/* MError exception base */
@@ -224,4 +226,44 @@ 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_GSTAT 0x50/* Guest status */ +FIELD(CSR_GSTAT, VM, 0, 1) +FIELD(CSR_GSTAT, PVM, 1, 1) +FIELD(CSR_GSTAT, GIDBIT, 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 */ + +#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_TRGP 0x16/* Trapped guest physical address */ +#endif /* LOONGARCH_CPU_CSR_H */
target/loongarch/cpu.h +61 -1
134 lines not on this page · raw message
target/loongarch/translate.h +2 -0
@@ -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))
@@ -66,6 +67,7 @@ typedef struct DisasContext {TCGv zero;bool la64;/* LoongArch64 mode */ bool va32;/* 32-bit virtual address */ +bool guest_mode;uint32_t cpucfg1;uint32_t cpucfg2;uint32_t cpucfg3;
Signature · 1 line
2.52.0
PATCH v2 4/4 target/loongarch: Add LVZ instruction decode, translation and disassembly
In reply to SignKirigamiAdd decode patterns for LVZ virtualization instructions:
gcsrrd, gcsrwr, gcsrxchg for guest CSR access, gtlbclr, gtlbflush, gtlbsrch, gtlbrd, gtlbwr, gtlbfill for guest TLB management, and hvcl for hypervisor call. Add TCG translation with guest-mode CSR access, GSPR trapping, GUEST_READONLY flag checking, and guest TLB operations. Finalize CSR offset mechanism with per-VM-level offsets. Add disassembly support for new CSR names.
--- target/loongarch/csr.c | 8 +- target/loongarch/csr.h | 9 +- target/loongarch/disas.c | 16 + target/loongarch/insns.decode | 14 + .../tcg/insn_trans/trans_extra.c.inc | 2 +- .../tcg/insn_trans/trans_privileged.c.inc | 289 ++++++++++++++++-- target/loongarch/tcg/translate.c | 6 +- 7 files changed, 314 insertions(+), 30 deletions(-)
target/loongarch/csr.c +4 -4
@@ -9,14 +9,14 @@#define CSR_OFF_FUNCS (NAME ,FL ,RD ,WR ) \ [LOONGARCH_CSR_##NAME] = { \ .name = (stringify(NAME)), \ - .offset = CSR_OFFSET(CSR_##NAME), \ + .offset = CSR_OFFSET(CSR_##NAME, 0), \ .flags = FL, .readfn = RD, .writefn = WR \ }#define CSR_OFF_ARRAY (NAME ,N ) \ [LOONGARCH_CSR_##NAME(N)] = { \ .name = (stringify(NAME##N)), \ - .offset = CSR_OFFSET(CSR_##NAME[N]), \ + .offset = CSR_OFFSET(CSR_##NAME[N], 0), \ .flags = CSRFL_BASIC, .readfn = NULL, .writefn = NULL \ }
@@ -25,13 +25,13 @@#define GCSR_OFF_FUNCS (NAME ,FL ,RD ,WR ) \ [LOONGARCH_CSR_##NAME] = { \ .name = (stringify(GCSR_##NAME)), \ - .offset = CPU_CSR_OFFSET(CSR_##NAME, 1), \ + .offset = CSR_OFFSET(CSR_##NAME, 1), \ .flags = FL, .readfn = RD, .writefn = WR \ }#define GCSR_OFF_ARRAY (NAME ,N ) \ [LOONGARCH_CSR_##NAME(N)] = { \ .name = (stringify(GCSR_##NAME##N)), \ - .offset = CPU_CSR_OFFSET(CSR_##NAME[N], 1), \ + .offset = CSR_OFFSET(CSR_##NAME[N], 1), \ .flags = CSRFL_BASIC, .readfn = NULL, .writefn = NULL \ }#define GCSR_OFF_FLAGS (NAME ,FL ) GCSR_OFF_FUNCS(NAME, FL, NULL, NULL)
target/loongarch/csr.h +2 -7
@@ -8,10 +8,9 @@#include "cpu-csr.h" -#define CSR_OFFSET (id) offsetof(CPUSysState, id) -#define CPU_CSR_OFFSET (id, vm_level) \ +#define CSR_OFFSET (id, vm_level) \ (offsetof(CPULoongArchState, sys_states[vm_level]) \ - + CSR_OFFSET(id)) + + offsetof(CPUSysState, id))typedef void (*GenCSRFunc )(void );enum {
@@ -35,8 +34,4 @@ typedef struct {CSRInfo *get_csr (unsigned int csr_num);CSRInfo *get_gcsr (unsigned int csr_num);bool set_csr_flag (unsigned int csr_num,int flag); -static inline unsigned int get_csr_offset (const CSRInfo *csr,int vm_level) -{ - return csr->offset + offsetof(CPULoongArchState, sys_states[vm_level]); -}#endif /* TARGET_LOONGARCH_CSR_H */
target/loongarch/disas.c +16 -0
@@ -51,6 +51,8 @@ static const char * const csr_names[] = {CSR_NAME (BADI ),CSR_NAME (EENTRY ),CSR_NAME (TLBIDX ), +CSR_NAME (GTLBC ), +CSR_NAME (TRGP ),CSR_NAME (TLBEHI ),CSR_NAME (TLBELO0 ), CSR_NAME(TLBELO1),
@@ -87,6 +89,10 @@ static const char * const csr_names[] = {CSR_NAME (TVAL ),CSR_NAME (CNTC ),CSR_NAME (TICLR ), +CSR_NAME (GSTAT ), +CSR_NAME (GCFG ), +CSR_NAME (GINTC ), +CSR_NAME (GCNTC ),CSR_NAME (LLBCTL ),CSR_NAME (IMPCTL1 ), CSR_NAME(IMPCTL2),
@@ -698,6 +704,16 @@ INSN(tlbfill, empty) INSN(tlbclr, empty) INSN(tlbflush, empty) INSN(invtlb, i_rr) +INSN(gcsrrd, r_csr) +INSN(gcsrwr, r_csr) +INSN(gcsrxchg, rr_csr) +INSN(gtlbclr, empty) +INSN(gtlbflush, empty) +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 +14 -0
@@ -493,6 +493,20 @@ 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 +} + +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 +hvcl 0000 0000 0010 1011 1 ............... @i15 + 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_extra.c.inc +1 -1
@@ -55,7 +55,7 @@ static bool gen_rdtime(DisasContext *ctx, arg_rr *a, tcg_gen_sextract_tl(dst1, dst1, high ? 32 : 0, 32); } - offset = CPU_CSR_OFFSET(CSR_TID, 0); + offset = CSR_OFFSET(CSR_TID, 0); tcg_gen_ld_i64(dst2, tcg_env, offset); return true;
target/loongarch/tcg/insn_trans/trans_privileged.c.inc +272 -17
454 lines not on this page · raw message
target/loongarch/tcg/translate.c +5 -1
@@ -122,12 +122,16 @@ static void loongarch_tr_init_disas_context(DisasContextBase *dcbase,CPULoongArchState *env =cpu_env (cs);DisasContext *ctx =container_of (dcbase, DisasContext, base); + ctx->guest_mode = (ctx->base.tb->flags &HW_FLAGS_GUEST_MODE ) !=0 ; ctx->page_start = ctx->base.pc_first &TARGET_PAGE_MASK ; ctx->plv = ctx->base.tb->flags &HW_FLAGS_PLV_MASK ;if (ctx->base.tb->flags &HW_FLAGS_CRMD_PG ) { ctx->mem_idx = ctx->plv; +if (ctx->guest_mode) { + ctx->mem_idx +=MMU_GUEST_IDX ; + } }else { - ctx->mem_idx =MMU_DA_IDX ; + ctx->mem_idx = ctx->guest_mode ?MMU_GUEST_DA_IDX :MMU_DA_IDX ; }/* Bound the number of insns to execute to those left on the page. */
Signature · 1 line
2.52.0
PATCH v2 2/4 target/loongarch: Add guest CSR table and VM-level CSR infrastructure
In reply to SignKirigamiAdd gcsr_info table for guest CSR register definitions with CSRFL_GUEST_READONLY and CSRFL_GSPR flags for sensitive register trapping. Add get_gcsr() lookup function. Update arch_dump.c to use host sys_states directly.
--- target/loongarch/arch_dump.c | 6 +- target/loongarch/csr.c | 120 ++++++++++++++++++++++++++++++++++- target/loongarch/csr.h | 3 + 3 files changed, 125 insertions(+), 4 deletions(-)
target/loongarch/arch_dump.c +3 -3
@@ -116,7 +116,7 @@ int loongarch_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs, {struct loongarch_note note;CPULoongArchState *env = &LOONGARCH_CPU (cs)->env; -CPUSysState *sys =env_sys (env); +CPUSysState *host = &env->sys_states[LOONGARCH_VM_LEVEL_HOST ];int ret, i; loongarch_note_init(¬e, s, "CORE", 5, NT_PRSTATUS,
@@ -127,8 +127,8 @@ int loongarch_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,for (i =0 ; i <32 ; ++i) { note.prstatus.pr_reg.gpr[i] =cpu_to_dump64 (s, env->gpr[i]); } - note.prstatus.pr_reg.csr_era =cpu_to_dump64 (s, sys->CSR_ERA); - note.prstatus.pr_reg.csr_badv =cpu_to_dump64 (s, sys->CSR_BADV); + note.prstatus.pr_reg.csr_era =cpu_to_dump64 (s, host->CSR_ERA); + note.prstatus.pr_reg.csr_badv =cpu_to_dump64 (s, host->CSR_BADV); ret =f (¬e,LOONGARCH_PRSTATUS_NOTE_SIZE , s);if (ret <0 ) {return -1 ;
target/loongarch/csr.c +119 -1
154 lines not on this page · raw message
target/loongarch/csr.h +3 -0
@@ -20,6 +20,8 @@ enum {CSRFL_IO = (1 <<2 ),CSRFL_UNUSED = (1 <<3 ),CSRFL_BASIC = (1 <<4 ), +CSRFL_GUEST_READONLY = (1 <<5 ), + CSRFL_GSPR = (1 << 6), }; typedef struct {
@@ -31,6 +33,7 @@ typedef struct { } CSRInfo;CSRInfo *get_csr (unsigned int csr_num); +CSRInfo *get_gcsr (unsigned int csr_num);bool set_csr_flag (unsigned int csr_num,int flag);static inline unsigned int get_csr_offset (const CSRInfo *csr,int vm_level) {
Signature · 1 line
2.52.0
On 2026年6月23日星期二 中国标准时间 14:16:11,SignKirigami wrote:
Quoted 28 lines
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.
Signed-off-by SignKirigami <[email protected]>Signed-off-by Hengyu Yu <[email protected]>SignKirigami (4):
target/loongarch: Add LVZ feature definitions and CPU structures target/loongarch: Add guest CSR table and VM-level CSR infrastructure target/loongarch: Add LVZ MMU, TCG infrastructure and guest support target/loongarch: Add LVZ instruction decode, translation and
disassemblytarget/loongarch/arch_dump.c | 6 +- target/loongarch/cpu-csr.h | 42 ++ target/loongarch/cpu-mmu.h | 39 +- target/loongarch/cpu.c | 279 ++++++--- target/loongarch/cpu.h | 62 +- target/loongarch/cpu_helper.c | 87 ++- target/loongarch/csr.c | 124 +++- target/loongarch/csr.h | 12 +- target/loongarch/disas.c | 16 + target/loongarch/insns.decode | 14 + target/loongarch/internals.h | 8 +- target/loongarch/machine.c | 125 +++- target/loongarch/tcg/constant_timer.c | 74 ++- target/loongarch/tcg/csr_helper.c | 136 ++++- target/loongarch/tcg/helper.h | 30 +- .../tcg/insn_trans/trans_extra.c.inc | 2 +-
Quoted 10 lines
.../tcg/insn_trans/trans_privileged.c.inc | 353 ++++++++++- target/loongarch/tcg/op_helper.c | 45 +- target/loongarch/tcg/tcg_cpu.c | 65 +- target/loongarch/tcg/tcg_loongarch.h | 3 +- target/loongarch/tcg/tlb_helper.c | 577 +++++++++++++----- target/loongarch/tcg/translate.c | 6 +- target/loongarch/translate.h | 2 + 23 files changed, 1747 insertions(+), 360 deletions(-)
I 'm sorry for not wrapping this cover message, and will fix it on next version. Is there any progress on this patch serie?
Regards
SignKirigami
Quoted 39 lines · SignKirigami
On 2026年6月23日星期二 中国标准时间 14:16:11,SignKirigami wrote:
Quoted 28 lines
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.
Signed-off-by SignKirigami <[email protected]>Signed-off-by Hengyu Yu <[email protected]>SignKirigami (4):
target/loongarch: Add LVZ feature definitions and CPU structures target/loongarch: Add guest CSR table and VM-level CSR infrastructure target/loongarch: Add LVZ MMU, TCG infrastructure and guest support target/loongarch: Add LVZ instruction decode, translation and
disassemblytarget/loongarch/arch_dump.c | 6 +- target/loongarch/cpu-csr.h | 42 ++ target/loongarch/cpu-mmu.h | 39 +- target/loongarch/cpu.c | 279 ++++++--- target/loongarch/cpu.h | 62 +- target/loongarch/cpu_helper.c | 87 ++- target/loongarch/csr.c | 124 +++- target/loongarch/csr.h | 12 +- target/loongarch/disas.c | 16 + target/loongarch/insns.decode | 14 + target/loongarch/internals.h | 8 +- target/loongarch/machine.c | 125 +++- target/loongarch/tcg/constant_timer.c | 74 ++- target/loongarch/tcg/csr_helper.c | 136 ++++- target/loongarch/tcg/helper.h | 30 +- .../tcg/insn_trans/trans_extra.c.inc | 2 +-Quoted 10 lines
.../tcg/insn_trans/trans_privileged.c.inc | 353 ++++++++++- target/loongarch/tcg/op_helper.c | 45 +- target/loongarch/tcg/tcg_cpu.c | 65 +- target/loongarch/tcg/tcg_loongarch.h | 3 +- target/loongarch/tcg/tlb_helper.c | 577 +++++++++++++----- target/loongarch/tcg/translate.c | 6 +- target/loongarch/translate.h | 2 + 23 files changed, 1747 insertions(+), 360 deletions(-)
Quoted 2 lines
I 'm sorry for not wrapping this cover message, and will fix it on next version. Is there any progress on this patch serie?
I suggest that split this patch into smaller patches, so that almost every patch is easy to review. It is really hard to review carefully with big patch :(
Regards
Bibo Mao
Quoted 6 lines
Regards
SignKirigami