qemu-riscv.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Christoph Müllner" <christoph.muellner@vrull.eu>
To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org,
	Alistair Francis <alistair.francis@wdc.com>,
	Bin Meng <bin.meng@windriver.com>,
	Philipp Tomsich <philipp.tomsich@vrull.eu>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Richard Henderson <richard.henderson@linaro.org>,
	Daniel Henrique Barboza <dbarboza@ventanamicro.com>,
	Andrew Jones <ajones@ventanamicro.com>
Cc: "Christoph Müllner" <christoph.muellner@vrull.eu>,
	"Laurent Vivier" <laurent@vivier.eu>,
	"Weiwei Li" <liwei1518@gmail.com>,
	"Liu Zhiwei" <zhiwei_liu@linux.alibaba.com>
Subject: [RFC PATCH v2 4/4] linux-user/riscv: Implement dynamic memory consistency model support
Date: Fri,  9 Feb 2024 07:41:17 +0100	[thread overview]
Message-ID: <20240209064117.2746701-5-christoph.muellner@vrull.eu> (raw)
In-Reply-To: <20240209064117.2746701-1-christoph.muellner@vrull.eu>

This patch implements the dynamic memory consistency model prctl calls
for RISC-V. The implementation introduces a single boolean variable to
keep the DTSO state.

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 linux-user/riscv/target_prctl.h | 76 ++++++++++++++++++++++++++++++++-
 target/riscv/cpu.c              |  5 +++
 target/riscv/cpu.h              |  1 +
 3 files changed, 81 insertions(+), 1 deletion(-)

diff --git a/linux-user/riscv/target_prctl.h b/linux-user/riscv/target_prctl.h
index eb53b31ad5..e54321d872 100644
--- a/linux-user/riscv/target_prctl.h
+++ b/linux-user/riscv/target_prctl.h
@@ -1 +1,75 @@
-/* No special prctl support required. */
+/*
+ * RISC-V specific prctl functions for linux-user
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef RISCV_TARGET_PRCTL_H
+#define RISCV_TARGET_PRCTL_H
+
+static inline void riscv_dtso_set_enable(CPURISCVState *env, bool enable)
+{
+    env->dtso_ena = enable;
+}
+
+static inline bool riscv_dtso_is_enabled(CPURISCVState *env)
+{
+    return env->dtso_ena;
+}
+
+static abi_long do_prctl_set_memory_consistency_model(CPUArchState *cpu_env,
+                                                      abi_long arg2)
+{
+    RISCVCPU *cpu = env_archcpu(cpu_env);
+    bool dtso_ena_old = riscv_dtso_is_enabled(cpu_env);
+    bool dtso_ena_new;
+    bool has_dtso = cpu->cfg.ext_ssdtso;
+
+    switch (arg2) {
+        case PR_MEMORY_CONSISTENCY_MODEL_RISCV_WMO:
+            dtso_ena_new = false;
+            break;
+        case PR_MEMORY_CONSISTENCY_MODEL_RISCV_TSO:
+	    dtso_ena_new = true;
+            break;
+        default:
+            return -TARGET_EINVAL;
+    }
+
+    /* No change requested. */
+    if (dtso_ena_old == dtso_ena_new)
+	    return 0;
+
+    /* Enabling TSO only works if DTSO is available. */
+    if (dtso_ena_new && !has_dtso)
+	    return -TARGET_EINVAL;
+
+    /* Switchin TSO->WMO is not allowed. */
+    if (!dtso_ena_new)
+	    return -TARGET_EINVAL;
+
+    riscv_dtso_set_enable(cpu_env, dtso_ena_new);
+
+    /*
+     * No need to reschedule other threads, because the emulation
+     * of DTSO is fine (from a memory model view) if they are out
+     * of sync until they will eventually reschedule.
+     */
+
+    return 0;
+}
+
+#define do_prctl_set_memory_consistency_model \
+        do_prctl_set_memory_consistency_model
+
+static abi_long do_prctl_get_memory_consistency_model(CPUArchState *cpu_env)
+{
+    if (riscv_dtso_is_enabled(cpu_env))
+	    return PR_MEMORY_CONSISTENCY_MODEL_RISCV_TSO;
+
+    return PR_MEMORY_CONSISTENCY_MODEL_RISCV_WMO;
+}
+
+#define do_prctl_get_memory_consistency_model \
+        do_prctl_get_memory_consistency_model
+
+#endif /* RISCV_TARGET_PRCTL_H */
diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c
index ee90c09600..2e2aac73dc 100644
--- a/target/riscv/cpu.c
+++ b/target/riscv/cpu.c
@@ -922,6 +922,11 @@ static void riscv_cpu_reset_hold(Object *obj)
     if (mcc->parent_phases.hold) {
         mcc->parent_phases.hold(obj);
     }
+#ifdef CONFIG_USER_ONLY
+    /* Default is true if Ztso is enabled, false otherwise. */
+    env->dtso_ena = cpu->cfg.ext_ztso;
+#endif
+
 #ifndef CONFIG_USER_ONLY
     env->misa_mxl = mcc->misa_mxl_max;
     env->priv = PRV_M;
diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h
index f52dce78ba..69420b2ae3 100644
--- a/target/riscv/cpu.h
+++ b/target/riscv/cpu.h
@@ -200,6 +200,7 @@ struct CPUArchState {
 
 #ifdef CONFIG_USER_ONLY
     uint32_t elf_flags;
+    bool dtso_ena; /* Dynamic TSO enable */
 #endif
 
 #ifndef CONFIG_USER_ONLY
-- 
2.43.0



      parent reply	other threads:[~2024-02-09  6:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-09  6:41 [RFC PATCH v2 0/4] RISC-V: Add dynamic TSO support Christoph Müllner
2024-02-09  6:41 ` [RFC PATCH v2 1/4] RISC-V: Add support for Ssdtso Christoph Müllner
2024-02-09  6:41 ` [RFC PATCH v2 2/4] linux-user/riscv: Add Ssdtso extension to hwprobe Christoph Müllner
2024-02-09  6:41 ` [RFC PATCH v2 3/4] linux-user/prctl: Add dynamic memory consistency model prctl API Christoph Müllner
2024-02-09  6:41 ` Christoph Müllner [this message]

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=20240209064117.2746701-5-christoph.muellner@vrull.eu \
    --to=christoph.muellner@vrull.eu \
    --cc=ajones@ventanamicro.com \
    --cc=alistair.francis@wdc.com \
    --cc=bin.meng@windriver.com \
    --cc=dbarboza@ventanamicro.com \
    --cc=laurent@vivier.eu \
    --cc=liwei1518@gmail.com \
    --cc=palmer@dabbelt.com \
    --cc=philipp.tomsich@vrull.eu \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-riscv@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=zhiwei_liu@linux.alibaba.com \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).