qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Huth <thuth@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
	qemu-stable@nongnu.org, Song Gao <gaosong@loongson.cn>
Subject: [PULL 15/16] target/loongarch: Fix the CSRRD CPUID instruction on big endian hosts
Date: Mon, 24 Jul 2023 18:53:48 +0200	[thread overview]
Message-ID: <20230724165349.55714-16-thuth@redhat.com> (raw)
In-Reply-To: <20230724165349.55714-1-thuth@redhat.com>

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.

Cc: qemu-stable@nongnu.org
Message-Id: <20230720175307.854460-1-thuth@redhat.com>
Reviewed-by: Song Gao <gaosong@loongson.cn>
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 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(-)

diff --git a/target/loongarch/cpu.h b/target/loongarch/cpu.h
index ed04027af1..fa371ca8ba 100644
--- a/target/loongarch/cpu.h
+++ b/target/loongarch/cpu.h
@@ -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];
diff --git a/target/loongarch/helper.h b/target/loongarch/helper.h
index b9de77d926..ffb1e0b0bf 100644
--- a/target/loongarch/helper.h
+++ b/target/loongarch/helper.h
@@ -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)
diff --git a/target/loongarch/csr_helper.c b/target/loongarch/csr_helper.c
index 6526367946..55341551a5 100644
--- a/target/loongarch/csr_helper.c
+++ b/target/loongarch/csr_helper.c
@@ -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);
diff --git a/target/loongarch/insn_trans/trans_privileged.c.inc b/target/loongarch/insn_trans/trans_privileged.c.inc
index 02bca7ca23..9c9de090f0 100644
--- a/target/loongarch/insn_trans/trans_privileged.c.inc
+++ b/target/loongarch/insn_trans/trans_privileged.c.inc
@@ -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),
-- 
2.39.3



  parent reply	other threads:[~2023-07-24 16:55 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-24 16:53 [PULL 00/16] s390x fixes Thomas Huth
2023-07-24 16:53 ` [PULL 01/16] target/s390x: Make CKSM raise an exception if R2 is odd Thomas Huth
2023-07-24 16:53 ` [PULL 02/16] target/s390x: Fix CLM with M3=0 Thomas Huth
2023-07-24 16:53 ` [PULL 03/16] target/s390x: Fix CONVERT TO LOGICAL/FIXED with out-of-range inputs Thomas Huth
2023-07-24 16:53 ` [PULL 04/16] target/s390x: Fix ICM with M3=0 Thomas Huth
2023-07-24 16:53 ` [PULL 05/16] target/s390x: Make MC raise specification exception when class >= 16 Thomas Huth
2023-07-24 16:53 ` [PULL 06/16] target/s390x: Fix assertion failure in VFMIN/VFMAX with type 13 Thomas Huth
2023-07-24 16:53 ` [PULL 07/16] tests/tcg/s390x: Test CKSM Thomas Huth
2023-07-24 16:53 ` [PULL 08/16] tests/tcg/s390x: Test CLGEBR and CGEBRA Thomas Huth
2023-07-24 16:53 ` [PULL 09/16] tests/tcg/s390x: Test CLM Thomas Huth
2023-07-24 16:53 ` [PULL 10/16] tests/tcg/s390x: Test ICM Thomas Huth
2023-07-24 16:53 ` [PULL 11/16] tests/tcg/s390x: Test MC Thomas Huth
2023-07-24 16:53 ` [PULL 12/16] tests/tcg/s390x: Test STPQ Thomas Huth
2023-07-24 16:53 ` [PULL 13/16] tests/tcg/s390x: Test VCKSM Thomas Huth
2023-07-24 16:53 ` [PULL 14/16] tests/avocado/migration: Remove the malfunctioning s390x tests Thomas Huth
2023-07-24 16:53 ` Thomas Huth [this message]
2023-07-24 16:53 ` [PULL 16/16] tests/avocado/machine_s390_ccw_virtio: Skip the flaky virtio-gpu test by default Thomas Huth
2023-07-25 11:44 ` [PULL 00/16] s390x fixes Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230724165349.55714-16-thuth@redhat.com \
    --to=thuth@redhat.com \
    --cc=gaosong@loongson.cn \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-stable@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).