Skip to contentSkip to conversation

target/loongarch: Add loongarch32 mode for loongarch64-softmmu

PATCH Superseded Jiajie Chen 2 participants 8 messages Aug 7, 2023

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/5 target/loongarch: Add loongarch32 mode for loongarch64-softmmu

This commit adds loongarch32 mode to loongarch64-softmmu.

---
 target/loongarch/cpu.h | 7 +++++++
 1 file changed, 7 insertions(+)
target/loongarch/cpu.h +7 -0
index fa371ca8ba..43c73e6363 100644
@@ -272,9 +272,16 @@ struct LoongArchTLB {
 };
 typedef struct LoongArchTLB LoongArchTLB;
 
+/* Current LoongArch mode */
+typedef enum LoongArchMode {
+    LA32 = 0,
+    LA64 = 1,
+} LoongArchMode;
+
 typedef struct CPUArchState {
     uint64_t gpr[32];
     uint64_t pc;
+    LoongArchMode mode;
 
     fpr_t fpr[32];
     float_status fp_status;
Signature · 1 line
2.39.2
#1 lore raw

PATCH v2 2/5 target/loongarch: Add loongarch32 cpu la132

Add la132 as a loongarch32 cpu type and allow virt machine to be used with la132 instead of la464.

---
 hw/loongarch/virt.c    |  5 -----
 target/loongarch/cpu.c | 41 +++++++++++++++++++++++++++++++++++++++++
 target/loongarch/cpu.h | 11 +++++++++++
 3 files changed, 52 insertions(+), 5 deletions(-)
hw/loongarch/virt.c +0 -5
index e19b042ce8..af15bf5aaa 100644
@@ -798,11 +798,6 @@ static void loongarch_init(MachineState *machine)
         cpu_model = LOONGARCH_CPU_TYPE_NAME("la464");
     }
 
-    if (!strstr(cpu_model, "la464")) {
-        error_report("LoongArch/TCG needs cpu type la464");
-        exit(1);
-    }
-
     if (ram_size < 1 * GiB) {
         error_report("ram_size must be greater than 1G.");
         exit(1);
target/loongarch/cpu.c +41 -0

62 lines not on this page · raw message

target/loongarch/cpu.h +11 -0
index 43c73e6363..f1907cddc5 100644
@@ -404,6 +404,17 @@ struct LoongArchCPUClass {
     ResettablePhases parent_phases;
 };
 
+#define TYPE_LOONGARCH32_CPU "loongarch32-cpu"
+typedef struct LoongArch32CPUClass LoongArch32CPUClass;
+DECLARE_CLASS_CHECKERS(LoongArch32CPUClass, LOONGARCH32_CPU,
+                       TYPE_LOONGARCH32_CPU)
+
+struct LoongArch32CPUClass {
+    /*< private >*/
+    LoongArchCPUClass parent_class;
+    /*< public >*/
+};
+
 /*
  * LoongArch CPUs has 4 privilege levels.
  * 0 for kernel mode, 3 for user mode.
Signature · 1 line
2.39.2
#2 lore raw

PATCH v2 3/5 target/loongarch: Add GDB support for loongarch32 mode

In reply to Jiajie Chen

GPRs and PC are 32-bit wide in loongarch32 mode.

---
 configs/targets/loongarch64-softmmu.mak |  2 +-
 gdb-xml/loongarch-base32.xml            | 45 +++++++++++++++++++++++++
 target/loongarch/cpu.c                  | 10 +++++-
 target/loongarch/gdbstub.c              | 32 ++++++++++++++----
 4 files changed, 80 insertions(+), 9 deletions(-)
 create mode 100644 gdb-xml/loongarch-base32.xml
configs/targets/loongarch64-softmmu.mak +1 -1
index 9abc99056f..f23780fdd8 100644
@@ -1,5 +1,5 @@
 TARGET_ARCH=loongarch64
 TARGET_BASE_ARCH=loongarch
 TARGET_SUPPORTS_MTTCG=y
-TARGET_XML_FILES= gdb-xml/loongarch-base64.xml gdb-xml/loongarch-fpu.xml
+TARGET_XML_FILES= gdb-xml/loongarch-base32.xml gdb-xml/loongarch-base64.xml gdb-xml/loongarch-fpu.xml
 TARGET_NEED_FDT=y
gdb-xml/loongarch-base32.xml new file +45 -0

45 lines not on this page · raw message

target/loongarch/cpu.c +9 -1
index d31efe86da..ee6d45f1b0 100644
@@ -710,7 +710,13 @@ static const struct SysemuCPUOps loongarch_sysemu_ops = {
 
 static gchar *loongarch_gdb_arch_name(CPUState *cs)
 {
-    return g_strdup("loongarch64");
+    LoongArchCPU *cpu = LOONGARCH_CPU(cs);
+    CPULoongArchState *env = &cpu->env;
+    if (env->mode == LA64) {
+        return g_strdup("loongarch64");
+    } else {
+        return g_strdup("loongarch32");
+    }
 }
 
 static void loongarch_cpu_class_init(ObjectClass *c, void *data)
@@ -750,6 +756,8 @@ static void loongarch_cpu_class_init(ObjectClass *c, void *data)
 
 static void loongarch32_cpu_class_init(ObjectClass *c, void *data)
 {
+    CPUClass *cc = CPU_CLASS(c);
+    cc->gdb_core_xml_file = "loongarch-base32.xml";
 }
 
 #define DEFINE_LOONGARCH_CPU_TYPE(model, initfn) \
target/loongarch/gdbstub.c +25 -7
index 0752fff924..7c82204e92 100644
@@ -34,16 +34,25 @@ int loongarch_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
 {
     LoongArchCPU *cpu = LOONGARCH_CPU(cs);
     CPULoongArchState *env = &cpu->env;
+    uint64_t val;
 
     if (0 <= n && n < 32) {
-        return gdb_get_regl(mem_buf, env->gpr[n]);
+        val = env->gpr[n];
     } else if (n == 32) {
         /* orig_a0 */
-        return gdb_get_regl(mem_buf, 0);
+        val = 0;
     } else if (n == 33) {
-        return gdb_get_regl(mem_buf, env->pc);
+        val = env->pc;
     } else if (n == 34) {
-        return gdb_get_regl(mem_buf, env->CSR_BADV);
+        val = env->CSR_BADV;
+    }
+
+    if (0 <= n && n <= 34) {
+        if (env->mode == LA64) {
+            return gdb_get_reg64(mem_buf, val);
+        } else {
+            return gdb_get_reg32(mem_buf, val);
+        }
     }
     return 0;
 }
@@ -52,15 +61,24 @@ int loongarch_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
 {
     LoongArchCPU *cpu = LOONGARCH_CPU(cs);
     CPULoongArchState *env = &cpu->env;
-    target_ulong tmp = ldtul_p(mem_buf);
+    target_ulong tmp;
+    int read_length;
     int length = 0;
 
+    if (env->mode == LA64) {
+        tmp = ldq_p(mem_buf);
+        read_length = 8;
+    } else {
+        tmp = ldl_p(mem_buf);
+        read_length = 4;
+    }
+
     if (0 <= n && n < 32) {
         env->gpr[n] = tmp;
-        length = sizeof(target_ulong);
+        length = read_length;
     } else if (n == 33) {
         env->pc = tmp;
-        length = sizeof(target_ulong);
+        length = read_length;
     }
     return length;
 }
Signature · 1 line
2.39.2
#3 lore raw

PATCH v2 4/5 target/loongarch: Support LoongArch32 TLB entry

In reply to Jiajie Chen

The TLB entry of LA32 lacks NR, NX and RPLV and they are hardwired to zero in LoongArch32.

---
 target/loongarch/cpu-csr.h    |  9 +++++----
 target/loongarch/tlb_helper.c | 17 ++++++++++++-----
 2 files changed, 17 insertions(+), 9 deletions(-)
target/loongarch/cpu-csr.h +5 -4
index f8f24032cb..faf76a589b 100644
@@ -66,10 +66,11 @@ FIELD(TLBENTRY, D, 1, 1)
 FIELD(TLBENTRY, PLV, 2, 2)
 FIELD(TLBENTRY, MAT, 4, 2)
 FIELD(TLBENTRY, G, 6, 1)
-FIELD(TLBENTRY, PPN, 12, 36)
-FIELD(TLBENTRY, NR, 61, 1)
-FIELD(TLBENTRY, NX, 62, 1)
-FIELD(TLBENTRY, RPLV, 63, 1)
+FIELD(TLBENTRY_32, PPN, 12, 24)
+FIELD(TLBENTRY_64, PPN, 12, 36)
+FIELD(TLBENTRY_64, NR, 61, 1)
+FIELD(TLBENTRY_64, NX, 62, 1)
+FIELD(TLBENTRY_64, RPLV, 63, 1)
 
 #define LOONGARCH_CSR_ASID           0x18 /* Address space identifier */
 FIELD(CSR_ASID, ASID, 0, 10)
target/loongarch/tlb_helper.c +12 -5
index 6e00190547..690c6ef25f 100644
@@ -48,10 +48,17 @@ static int loongarch_map_tlb_entry(CPULoongArchState *env, hwaddr *physical,
     tlb_v = FIELD_EX64(tlb_entry, TLBENTRY, V);
     tlb_d = FIELD_EX64(tlb_entry, TLBENTRY, D);
     tlb_plv = FIELD_EX64(tlb_entry, TLBENTRY, PLV);
-    tlb_ppn = FIELD_EX64(tlb_entry, TLBENTRY, PPN);
-    tlb_nx = FIELD_EX64(tlb_entry, TLBENTRY, NX);
-    tlb_nr = FIELD_EX64(tlb_entry, TLBENTRY, NR);
-    tlb_rplv = FIELD_EX64(tlb_entry, TLBENTRY, RPLV);
+    if (env->mode == LA64) {
+        tlb_ppn = FIELD_EX64(tlb_entry, TLBENTRY_64, PPN);
+        tlb_nx = FIELD_EX64(tlb_entry, TLBENTRY_64, NX);
+        tlb_nr = FIELD_EX64(tlb_entry, TLBENTRY_64, NR);
+        tlb_rplv = FIELD_EX64(tlb_entry, TLBENTRY_64, RPLV);
+    } else {
+        tlb_ppn = FIELD_EX64(tlb_entry, TLBENTRY_32, PPN);
+        tlb_nx = 0;
+        tlb_nr = 0;
+        tlb_rplv = 0;
+    }
 
     /* Check access rights */
     if (!tlb_v) {
@@ -79,7 +86,7 @@ static int loongarch_map_tlb_entry(CPULoongArchState *env, hwaddr *physical,
      * tlb_entry contains ppn[47:12] while 16KiB ppn is [47:15]
      * need adjust.
      */
-    *physical = (tlb_ppn << R_TLBENTRY_PPN_SHIFT) |
+    *physical = (tlb_ppn << R_TLBENTRY_64_PPN_SHIFT) |
                 (address & MAKE_64BIT_MASK(0, tlb_ps));
     *prot = PAGE_READ;
     if (tlb_d) {
Signature · 1 line
2.39.2
#4 lore raw

Re: [PATCH v2 4/5] target/loongarch: Support LoongArch32 TLB entry

Quoted 14 lines · Jiajie Chen

The TLB entry of LA32 lacks NR, NX and RPLV and they are hardwired to zero in LoongArch32.

---
  target/loongarch/cpu-csr.h    |  9 +++++----
  target/loongarch/tlb_helper.c | 17 ++++++++++++-----
  2 files changed, 17 insertions(+), 9 deletions(-)
target/loongarch/cpu-csr.h +0 -0
index f8f24032cb..faf76a589b 100644

target/loongarch/cpu-csr.h FIELD(TLBENTRY, D, 1, 1) · line 69

Quoted 8 lines
target/loongarch/cpu-csr.h +1 -4
@@ -66,10 +66,11 @@ FIELD(TLBENTRY, D, 1, 1)
  FIELD(TLBENTRY, PLV, 2, 2)
  FIELD(TLBENTRY, MAT, 4, 2)
  FIELD(TLBENTRY, G, 6, 1)
-FIELD(TLBENTRY, PPN, 12, 36)
-FIELD(TLBENTRY, NR, 61, 1)
-FIELD(TLBENTRY, NX, 62, 1)
-FIELD(TLBENTRY, RPLV, 63, 1)
+FIELD(TLBENTRY_32, PPN, 12, 24)

Sorry, the starting bit of TLBENTRY_32_PPN should be 8 instead of 12. Will be corrected in v3.

Quoted 42 lines
target/loongarch/cpu-csr.h +4 -0
+FIELD(TLBENTRY_64, PPN, 12, 36)
+FIELD(TLBENTRY_64, NR, 61, 1)
+FIELD(TLBENTRY_64, NX, 62, 1)
+FIELD(TLBENTRY_64, RPLV, 63, 1)
 
  #define LOONGARCH_CSR_ASID           0x18 /* Address space identifier */
  FIELD(CSR_ASID, ASID, 0, 10)
target/loongarch/tlb_helper.c +12 -5
index 6e00190547..690c6ef25f 100644
@@ -48,10 +48,17 @@ static int loongarch_map_tlb_entry(CPULoongArchState *env, hwaddr *physical,
      tlb_v = FIELD_EX64(tlb_entry, TLBENTRY, V);
      tlb_d = FIELD_EX64(tlb_entry, TLBENTRY, D);
      tlb_plv = FIELD_EX64(tlb_entry, TLBENTRY, PLV);
-    tlb_ppn = FIELD_EX64(tlb_entry, TLBENTRY, PPN);
-    tlb_nx = FIELD_EX64(tlb_entry, TLBENTRY, NX);
-    tlb_nr = FIELD_EX64(tlb_entry, TLBENTRY, NR);
-    tlb_rplv = FIELD_EX64(tlb_entry, TLBENTRY, RPLV);
+    if (env->mode == LA64) {
+        tlb_ppn = FIELD_EX64(tlb_entry, TLBENTRY_64, PPN);
+        tlb_nx = FIELD_EX64(tlb_entry, TLBENTRY_64, NX);
+        tlb_nr = FIELD_EX64(tlb_entry, TLBENTRY_64, NR);
+        tlb_rplv = FIELD_EX64(tlb_entry, TLBENTRY_64, RPLV);
+    } else {
+        tlb_ppn = FIELD_EX64(tlb_entry, TLBENTRY_32, PPN);
+        tlb_nx = 0;
+        tlb_nr = 0;
+        tlb_rplv = 0;
+    }
 
      /* Check access rights */
      if (!tlb_v) {
@@ -79,7 +86,7 @@ static int loongarch_map_tlb_entry(CPULoongArchState *env, hwaddr *physical,
       * tlb_entry contains ppn[47:12] while 16KiB ppn is [47:15]
       * need adjust.
       */
-    *physical = (tlb_ppn << R_TLBENTRY_PPN_SHIFT) |
+    *physical = (tlb_ppn << R_TLBENTRY_64_PPN_SHIFT) |
                  (address & MAKE_64BIT_MASK(0, tlb_ps));
      *prot = PAGE_READ;
      if (tlb_d) {
#5 lore raw

Re: [PATCH v2 4/5] target/loongarch: Support LoongArch32 TLB entry

Hi, Jiajie

Quoted 11 lines · Jiajie Chen
Quoted 9 lines · Jiajie Chen

The TLB entry of LA32 lacks NR, NX and RPLV and they are hardwired to zero in LoongArch32.

---
  target/loongarch/cpu-csr.h    |  9 +++++---- target/loongarch/tlb_helper.c | 17 ++++++++++++-----

  2 files changed, 17 insertions(+), 9 deletions(-)

Please
Cc: Richard Henderson <[email protected]>

And
Cc: Jun Yi <[email protected]>
CC: [email protected]>
Their are also interested with Loongarch32 softmmu.

It would be better use the parameter '--cover-letter' create a patch0. Add some Change logs and introduction about this series in patch0.

Thanks.
Song Gao

Quoted 59 lines

target/loongarch/cpu-csr.h FIELD(TLBENTRY, D, 1, 1) · line 69

Quoted 13 lines
target/loongarch/cpu-csr.h +1 -4
index f8f24032cb..faf76a589b 100644
@@ -66,10 +66,11 @@ FIELD(TLBENTRY, D, 1, 1)
   FIELD(TLBENTRY, PLV, 2, 2)
   FIELD(TLBENTRY, MAT, 4, 2)
   FIELD(TLBENTRY, G, 6, 1)
-FIELD(TLBENTRY, PPN, 12, 36)
-FIELD(TLBENTRY, NR, 61, 1)
-FIELD(TLBENTRY, NX, 62, 1)
-FIELD(TLBENTRY, RPLV, 63, 1)
+FIELD(TLBENTRY_32, PPN, 12, 24)

Sorry, the starting bit of TLBENTRY_32_PPN should be 8 instead of 12. Will be corrected in v3.

Quoted 42 lines
target/loongarch/cpu-csr.h +4 -0
+FIELD(TLBENTRY_64, PPN, 12, 36)
+FIELD(TLBENTRY_64, NR, 61, 1)
+FIELD(TLBENTRY_64, NX, 62, 1)
+FIELD(TLBENTRY_64, RPLV, 63, 1)
   #define LOONGARCH_CSR_ASID           0x18 /* Address space identifier */
   FIELD(CSR_ASID, ASID, 0, 10)
target/loongarch/tlb_helper.c +12 -5
index 6e00190547..690c6ef25f 100644
@@ -48,10 +48,17 @@ static int 
 loongarch_map_tlb_entry(CPULoongArchState *env, hwaddr *physical,
       tlb_v = FIELD_EX64(tlb_entry, TLBENTRY, V);
       tlb_d = FIELD_EX64(tlb_entry, TLBENTRY, D);
       tlb_plv = FIELD_EX64(tlb_entry, TLBENTRY, PLV);
-    tlb_ppn = FIELD_EX64(tlb_entry, TLBENTRY, PPN);
-    tlb_nx = FIELD_EX64(tlb_entry, TLBENTRY, NX);
-    tlb_nr = FIELD_EX64(tlb_entry, TLBENTRY, NR);
-    tlb_rplv = FIELD_EX64(tlb_entry, TLBENTRY, RPLV);
+    if (env->mode == LA64) {
+        tlb_ppn = FIELD_EX64(tlb_entry, TLBENTRY_64, PPN);
+        tlb_nx = FIELD_EX64(tlb_entry, TLBENTRY_64, NX);
+        tlb_nr = FIELD_EX64(tlb_entry, TLBENTRY_64, NR);
+        tlb_rplv = FIELD_EX64(tlb_entry, TLBENTRY_64, RPLV);
+    } else {
+        tlb_ppn = FIELD_EX64(tlb_entry, TLBENTRY_32, PPN);
+        tlb_nx = 0;
+        tlb_nr = 0;
+        tlb_rplv = 0;
+    }
       /* Check access rights */
       if (!tlb_v) {
@@ -79,7 +86,7 @@ static int loongarch_map_tlb_entry(CPULoongArchState 
 *env, hwaddr *physical,
        * tlb_entry contains ppn[47:12] while 16KiB ppn is [47:15]
        * need adjust.
        */
-    *physical = (tlb_ppn << R_TLBENTRY_PPN_SHIFT) |
+    *physical = (tlb_ppn << R_TLBENTRY_64_PPN_SHIFT) |
                   (address & MAKE_64BIT_MASK(0, tlb_ps));
       *prot = PAGE_READ;

      if (tlb_d) {

#6 lore raw

Re: [PATCH v2 4/5] target/loongarch: Support LoongArch32 TLB entry

Quoted 17 lines · gaosong

Hi, Jiajie

Quoted 11 lines · Jiajie Chen
Quoted 9 lines · Jiajie Chen

The TLB entry of LA32 lacks NR, NX and RPLV and they are hardwired to zero in LoongArch32.

---
  target/loongarch/cpu-csr.h    |  9 +++++---- target/loongarch/tlb_helper.c | 17 ++++++++++++-----

  2 files changed, 17 insertions(+), 9 deletions(-)

Please
Cc: Richard Henderson <[email protected]>

Quoted 7 lines

And
Cc: Jun Yi <[email protected]>
CC: [email protected]>
Their are also interested with Loongarch32 softmmu.

It would be better use the parameter '--cover-letter' create a patch0. Add some Change logs and introduction about this series in patch0.

Thanks, I will add this in my next version of patch series.

Quoted 62 lines

Thanks.
Song Gao

Quoted 59 lines

target/loongarch/cpu-csr.h FIELD(TLBENTRY, D, 1, 1) · line 69

Quoted 13 lines
target/loongarch/cpu-csr.h +1 -4
index f8f24032cb..faf76a589b 100644
@@ -66,10 +66,11 @@ FIELD(TLBENTRY, D, 1, 1)
   FIELD(TLBENTRY, PLV, 2, 2)
   FIELD(TLBENTRY, MAT, 4, 2)
   FIELD(TLBENTRY, G, 6, 1)
-FIELD(TLBENTRY, PPN, 12, 36)
-FIELD(TLBENTRY, NR, 61, 1)
-FIELD(TLBENTRY, NX, 62, 1)
-FIELD(TLBENTRY, RPLV, 63, 1)
+FIELD(TLBENTRY_32, PPN, 12, 24)

Sorry, the starting bit of TLBENTRY_32_PPN should be 8 instead of 12. Will be corrected in v3.

Quoted 42 lines
target/loongarch/cpu-csr.h +4 -0
+FIELD(TLBENTRY_64, PPN, 12, 36)
+FIELD(TLBENTRY_64, NR, 61, 1)
+FIELD(TLBENTRY_64, NX, 62, 1)
+FIELD(TLBENTRY_64, RPLV, 63, 1)
   #define LOONGARCH_CSR_ASID           0x18 /* Address space identifier */
   FIELD(CSR_ASID, ASID, 0, 10)
target/loongarch/tlb_helper.c +12 -5
index 6e00190547..690c6ef25f 100644
@@ -48,10 +48,17 @@ static int 
 loongarch_map_tlb_entry(CPULoongArchState *env, hwaddr *physical,
       tlb_v = FIELD_EX64(tlb_entry, TLBENTRY, V);
       tlb_d = FIELD_EX64(tlb_entry, TLBENTRY, D);
       tlb_plv = FIELD_EX64(tlb_entry, TLBENTRY, PLV);
-    tlb_ppn = FIELD_EX64(tlb_entry, TLBENTRY, PPN);
-    tlb_nx = FIELD_EX64(tlb_entry, TLBENTRY, NX);
-    tlb_nr = FIELD_EX64(tlb_entry, TLBENTRY, NR);
-    tlb_rplv = FIELD_EX64(tlb_entry, TLBENTRY, RPLV);
+    if (env->mode == LA64) {
+        tlb_ppn = FIELD_EX64(tlb_entry, TLBENTRY_64, PPN);
+        tlb_nx = FIELD_EX64(tlb_entry, TLBENTRY_64, NX);
+        tlb_nr = FIELD_EX64(tlb_entry, TLBENTRY_64, NR);
+        tlb_rplv = FIELD_EX64(tlb_entry, TLBENTRY_64, RPLV);
+    } else {
+        tlb_ppn = FIELD_EX64(tlb_entry, TLBENTRY_32, PPN);
+        tlb_nx = 0;
+        tlb_nr = 0;
+        tlb_rplv = 0;
+    }
       /* Check access rights */
       if (!tlb_v) {
@@ -79,7 +86,7 @@ static int 
 loongarch_map_tlb_entry(CPULoongArchState *env, hwaddr *physical,
        * tlb_entry contains ppn[47:12] while 16KiB ppn is [47:15]
        * need adjust.
        */
-    *physical = (tlb_ppn << R_TLBENTRY_PPN_SHIFT) |
+    *physical = (tlb_ppn << R_TLBENTRY_64_PPN_SHIFT) |
                   (address & MAKE_64BIT_MASK(0, tlb_ps));
       *prot = PAGE_READ;

      if (tlb_d) {

#7 lore raw

PATCH v2 5/5 target/loongarch: Support LoongArch32 DMW

In reply to Jiajie Chen

LA32 uses a different encoding for CSR.DMW and a new direct mapping mechanism.

---
 target/loongarch/cpu-csr.h    |  7 +++----
 target/loongarch/tlb_helper.c | 26 +++++++++++++++++++++++---
 2 files changed, 26 insertions(+), 7 deletions(-)
target/loongarch/cpu-csr.h +3 -4
index faf76a589b..72cac5fff9 100644
@@ -188,10 +188,9 @@ FIELD(CSR_DMW, PLV1, 1, 1)
 FIELD(CSR_DMW, PLV2, 2, 1)
 FIELD(CSR_DMW, PLV3, 3, 1)
 FIELD(CSR_DMW, MAT, 4, 2)
-FIELD(CSR_DMW, VSEG, 60, 4)
-
-#define dmw_va2pa(va) \
-    (va & MAKE_64BIT_MASK(0, TARGET_VIRT_ADDR_SPACE_BITS))
+FIELD(CSR_DMW_32, PSEG, 25, 3)
+FIELD(CSR_DMW_32, VSEG, 29, 3)
+FIELD(CSR_DMW_64, VSEG, 60, 4)
 
 /* Debug CSRs */
 #define LOONGARCH_CSR_DBG            0x500 /* debug config */
target/loongarch/tlb_helper.c +23 -3
index 690c6ef25f..ea78108aff 100644
@@ -173,6 +173,18 @@ static int loongarch_map_address(CPULoongArchState *env, hwaddr *physical,
     return TLBRET_NOMATCH;
 }
 
+static hwaddr dmw_va2pa(CPULoongArchState *env, target_ulong va,
+                        target_ulong dmw)
+{
+    if (env->mode == LA64) {
+        return va & TARGET_PHYS_MASK;
+    } else {
+        uint32_t pseg = FIELD_EX32(dmw, CSR_DMW_32, PSEG);
+        return (va & MAKE_64BIT_MASK(0, R_CSR_DMW_32_VSEG_SHIFT)) | \
+            (pseg << R_CSR_DMW_32_VSEG_SHIFT);
+    }
+}
+
 static int get_physical_address(CPULoongArchState *env, hwaddr *physical,
                                 int *prot, target_ulong address,
                                 MMUAccessType access_type, int mmu_idx)
@@ -192,12 +204,20 @@ static int get_physical_address(CPULoongArchState *env, hwaddr *physical,
     }
 
     plv = kernel_mode | (user_mode << R_CSR_DMW_PLV3_SHIFT);
-    base_v = address >> R_CSR_DMW_VSEG_SHIFT;
+    if (env->mode == LA64) {
+        base_v = address >> R_CSR_DMW_64_VSEG_SHIFT;
+    } else {
+        base_v = (uint32_t)address >> R_CSR_DMW_32_VSEG_SHIFT;
+    }
     /* Check direct map window */
     for (int i = 0; i < 4; i++) {
-        base_c = FIELD_EX64(env->CSR_DMW[i], CSR_DMW, VSEG);
+        if (env->mode == LA64) {
+            base_c = FIELD_EX64(env->CSR_DMW[i], CSR_DMW_64, VSEG);
+        } else {
+            base_c = FIELD_EX64(env->CSR_DMW[i], CSR_DMW_32, VSEG);
+        }
         if ((plv & env->CSR_DMW[i]) && (base_c == base_v)) {
-            *physical = dmw_va2pa(address);
+            *physical = dmw_va2pa(env, address, env->CSR_DMW[i]);
             *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
             return TLBRET_MATCH;
         }
Signature · 1 line
2.39.2