target/loongarch: Fix the CSRRD CPUID instruction on big endian hosts
PATCH 1/1 target/loongarch: Fix the CSRRD CPUID instruction on big endian hosts
The test in tests/avocado/machine_loongarch.py is currently failing on big endian hosts like s390x. By comparing the traces between running the QEMU_EFI.fd bios on a s390x and on a x86 host, it's quickly obvious that the CSRRD instruction for the CPUID is behaving differently. And
indeed: The code currently does a long read (i.e. 64 bit) from the address that points to the CPUState->cpu_index field (with tcg_gen_ld_tl() in the trans_csrrd() function). But this cpu_index field is only an "int" (i.e. 32 bit). While this dirty pointer magic works on little endian hosts, it of course fails on big endian hosts. Fix it by using a proper helper function instead.
---
Note: There is another bug preventing tests/avocado/machine_loongarch.py from working correctly on big endian hosts (Linux fails to run the shell) but that problem is harder to debug since it happens way later in the boot process...
target/loongarch/cpu.h | 1 + target/loongarch/helper.h | 1 + target/loongarch/csr_helper.c | 9 +++++++++ target/loongarch/insn_trans/trans_privileged.c.inc | 8 +------- 4 files changed, 12 insertions(+), 7 deletions(-)
target/loongarch/cpu.h +1 -0
@@ -342,6 +342,7 @@ typedef struct CPUArchState {uint64_t CSR_DBG ;uint64_t CSR_DERA ;uint64_t CSR_DSAVE ; +uint64_t CSR_CPUID ;#ifndef CONFIG_USER_ONLY LoongArchTLB tlb[LOONGARCH_TLB_MAX ];
target/loongarch/helper.h +1 -0
@@ -98,6 +98,7 @@ 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_2(csrwr_estat, i64, env, tl)DEF_HELPER_2 (csrwr_asid, i64, env, tl)
target/loongarch/csr_helper.c +9 -0
@@ -35,6 +35,15 @@ target_ulong helper_csrrd_pgd(CPULoongArchState *env)return v; } +target_ulong helper_csrrd_cpuid (CPULoongArchState *env) +{ +LoongArchCPU *lac =env_archcpu (env); + + env->CSR_CPUID =CPU (lac)->cpu_index; + +return env->CSR_CPUID; +} +target_ulong helper_csrrd_tval (CPULoongArchState *env) {LoongArchCPU *cpu =env_archcpu (env);
target/loongarch/insn_trans/trans_privileged.c.inc +1 -7
@@ -99,13 +99,7 @@ static const CSRInfo csr_info[] = { CSR_OFF(PWCH), CSR_OFF(STLBPS), CSR_OFF(RVACFG), - [LOONGARCH_CSR_CPUID] = { - .offset = (int)offsetof(CPUState, cpu_index) - - (int)offsetof(LoongArchCPU, env), - .flags = CSRFL_READONLY, - .readfn = NULL, - .writefn = NULL - }, + CSR_OFF_FUNCS(CPUID, CSRFL_READONLY, gen_helper_csrrd_cpuid, NULL), CSR_OFF_FLAGS(PRCFG1, CSRFL_READONLY), CSR_OFF_FLAGS(PRCFG2, CSRFL_READONLY), CSR_OFF_FLAGS(PRCFG3, CSRFL_READONLY),
Signature · 1 line
2.39.3
Quoted 18 lines · Thomas Huth
The test in
tests/avocado/machine_loongarch.pyis currently failing on big endian hosts like s390x. By comparing the traces between running the QEMU_EFI.fd bios on a s390x and on a x86 host, it's quickly obvious that the CSRRD instruction for the CPUID is behaving differently. And
indeed: The code currently does a long read (i.e. 64 bit) from the address that points to theCPUState->cpu_indexfield (withtcg_gen_ld_tl()in thetrans_csrrd()function). But thiscpu_indexfield is only an "int" (i.e. 32 bit). While this dirty pointer magic works on little endian hosts, it of course fails on big endian hosts. Fix it by using a proper helper function instead.Signed-off-by Thomas Huth <[email protected]>---
Note: There is another bug preventingtests/avocado/machine_loongarch.pyfrom working correctly on big endian hosts (Linux fails to run the shell) but that problem is harder to debug since it happens way later in the boot process...
Quoted 6 lines
target/loongarch/cpu.h | 1 + target/loongarch/helper.h | 1 + target/loongarch/csr_helper.c | 9 +++++++++ target/loongarch/insn_trans/trans_privileged.c.inc | 8 +------- 4 files changed, 12 insertions(+), 7 deletions(-)
Thanks.
Song Gao
Quoted 64 lines
target/loongarch/cpu.h +1
-0@@ -342,6 +342,7 @@ typedef struct CPUArchState {uint64_t CSR_DBG ;uint64_t CSR_DERA ;uint64_t CSR_DSAVE ; +uint64_t CSR_CPUID ;#ifndef CONFIG_USER_ONLY LoongArchTLB tlb[LOONGARCH_TLB_MAX ];target/loongarch/helper.h +1
-0@@ -98,6 +98,7 @@ 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_2(csrwr_estat, i64, env, tl)DEF_HELPER_2 (csrwr_asid, i64, env, tl)target/loongarch/csr_helper.c +9
-0@@ -35,6 +35,15 @@ target_ulong helper_csrrd_pgd(CPULoongArchState *env)return v; } +target_ulong helper_csrrd_cpuid (CPULoongArchState *env) +{ +LoongArchCPU *lac =env_archcpu (env); + + env->CSR_CPUID =CPU (lac)->cpu_index; + +return env->CSR_CPUID; +} +target_ulong helper_csrrd_tval (CPULoongArchState *env) {LoongArchCPU *cpu =env_archcpu (env);target/loongarch/insn_trans/trans_privileged.c.inc +1
-7@@ -99,13 +99,7 @@ static const CSRInfo csr_info[] = { CSR_OFF(PWCH), CSR_OFF(STLBPS), CSR_OFF(RVACFG), - [LOONGARCH_CSR_CPUID] = { - .offset = (int)offsetof(CPUState, cpu_index) - - (int)offsetof(LoongArchCPU, env), - .flags = CSRFL_READONLY, - .readfn = NULL, - .writefn = NULL - }, + CSR_OFF_FUNCS(CPUID, CSRFL_READONLY, gen_helper_csrrd_cpuid, NULL), CSR_OFF_FLAGS(PRCFG1, CSRFL_READONLY), CSR_OFF_FLAGS(PRCFG2, CSRFL_READONLY), CSR_OFF_FLAGS(PRCFG3, CSRFL_READONLY),