unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [RFC PATCH 0/2] RISC-V: Add dynamic TSO support
@ 2023-11-27 14:56 Christoph Muellner
  2023-11-27 14:56 ` [RFC PATCH 1/2] RISC-V: Move TSO check to elf_machine_matches_host() Christoph Muellner
  2023-11-27 14:56 ` [RFC PATCH 2/2] RISC-V: Attempt to enable TSO mode for TSO binaries Christoph Muellner
  0 siblings, 2 replies; 3+ messages in thread
From: Christoph Muellner @ 2023-11-27 14:56 UTC (permalink / raw
  To: libc-alpha, Palmer Dabbelt, Darius Rad, Andrew Waterman,
	Philipp Tomsich
  Cc: Christoph Müllner

From: Christoph Müllner <christoph.muellner@vrull.eu>

The upcoming RISC-V Ssdtso specification introduces a bit in the senvcfg
CSR to switch the memory consistency model at run-time from RVWMO to TSO
(and back). The active consistency model can therefore be switched on a
per-hart base and managed by the kernel on a per-process/thread base.

A kernel patchset that exposes a prctl API to let user-space processes
switch their memory consistency model has been posted recently:
  https://lore.kernel.org/all/20231124072142.2786653-1-christoph.muellner@vrull.eu/T/#t
A remote branch that includes a couple of fixes for that first patchset
can be found here (note, that new fixes will be force-pushed into this branch):
  https://github.com/cmuellner/linux/tree/ssdtso

A Ssdtso patchset for QEMU has been posted:
  https://lists.nongnu.org/archive/html/qemu-devel/2023-11/msg02962.html
Additional patches to implement the prctl API for user-mode emulation
have been developed as well and can be found here (same note as before):
  https://github.com/cmuellner/qemu/tree/ssdtso

This patchset builds upon this prctl API and extends
elf_machine_matches_host() such, that it attempts to switch to TSO mode
if the ELF header has EF_RISCV_TSO set, but the machine reports
RVWMO as current consistency model.
Of course, this is an optional feature, which will fall-back to the
current behaviour (rejecting the ELF) if there is any error.

The implementation is somewhat inspired by a similar mechanism
in the MIPS code (sysdeps/mips/dl-machine-reject-phdr.h) that
switches the FP mode if necessary.

Following the same pattern, there is also branch for this patchset:
  https://github.com/cmuellner/glibc/tree/ssdtso

Christoph Müllner (2):
  RISC-V: Move TSO check to elf_machine_matches_host()
  RISC-V: Attempt to enable TSO mode for TSO binaries

 sysdeps/riscv/dl-machine.h                 | 15 +++++++++++++++
 sysdeps/unix/sysv/linux/riscv/readelflib.c |  5 ++---
 2 files changed, 17 insertions(+), 3 deletions(-)

-- 
2.41.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [RFC PATCH 1/2] RISC-V: Move TSO check to elf_machine_matches_host()
  2023-11-27 14:56 [RFC PATCH 0/2] RISC-V: Add dynamic TSO support Christoph Muellner
@ 2023-11-27 14:56 ` Christoph Muellner
  2023-11-27 14:56 ` [RFC PATCH 2/2] RISC-V: Attempt to enable TSO mode for TSO binaries Christoph Muellner
  1 sibling, 0 replies; 3+ messages in thread
From: Christoph Muellner @ 2023-11-27 14:56 UTC (permalink / raw
  To: libc-alpha, Palmer Dabbelt, Darius Rad, Andrew Waterman,
	Philipp Tomsich
  Cc: Christoph Müllner

From: Christoph Müllner <christoph.muellner@vrull.eu>

The current implementation does not support ELF files which  have the
EF_RISCV_TSO ELF flag set.  This is implemented in process_elf64_file(),
with the assumption that no TSO-capable hosts exists.

In order to weaken this restriction, let's move the TSO flag check
into elf_machine_matches_host(), where compatibility with the host
can be checked.

This patch serves as a preparation for further changes and does
not intend to implement any functional changes.

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 sysdeps/riscv/dl-machine.h                 | 4 ++++
 sysdeps/unix/sysv/linux/riscv/readelflib.c | 5 ++---
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/sysdeps/riscv/dl-machine.h b/sysdeps/riscv/dl-machine.h
index c0c9bd93ad..ce537731dd 100644
--- a/sysdeps/riscv/dl-machine.h
+++ b/sysdeps/riscv/dl-machine.h
@@ -72,6 +72,10 @@ elf_machine_matches_host (const ElfW(Ehdr) *ehdr)
     return 0;
 #endif
 
+  /* Execution of TSO binaries is not supported at this time.  */
+  if (ehdr->e_flags & EF_RISCV_TSO)
+    return 0;
+
   return 1;
 }
 
diff --git a/sysdeps/unix/sysv/linux/riscv/readelflib.c b/sysdeps/unix/sysv/linux/riscv/readelflib.c
index 788eba8305..e2bd4d7727 100644
--- a/sysdeps/unix/sysv/linux/riscv/readelflib.c
+++ b/sysdeps/unix/sysv/linux/riscv/readelflib.c
@@ -30,10 +30,9 @@ int process_elf64_file (const char *file_name, const char *lib,
      extension, we can still support libraries compiled without that extension
      so we just ignore this flag.
    - EF_RISCV_RVE: glibc (and Linux) don't support RV32E based systems.
-   - EF_RISCV_TSO: The TSO extension isn't supported, as doing so would require
-     some mechanism to ensure that the TSO extension is enabled which doesn't
+   - EF_RISCV_TSO: Accepted with restrictions (see elf_machine_matches_host).
      currently exist.  */
-#define SUPPORTED_ELF_FLAGS (EF_RISCV_FLOAT_ABI | EF_RISCV_RVC)
+#define SUPPORTED_ELF_FLAGS (EF_RISCV_FLOAT_ABI | EF_RISCV_RVC | EF_RISCV_TSO)
 
 /* Returns 0 if everything is ok, != 0 in case of error.  */
 int
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [RFC PATCH 2/2] RISC-V: Attempt to enable TSO mode for TSO binaries
  2023-11-27 14:56 [RFC PATCH 0/2] RISC-V: Add dynamic TSO support Christoph Muellner
  2023-11-27 14:56 ` [RFC PATCH 1/2] RISC-V: Move TSO check to elf_machine_matches_host() Christoph Muellner
@ 2023-11-27 14:56 ` Christoph Muellner
  1 sibling, 0 replies; 3+ messages in thread
From: Christoph Muellner @ 2023-11-27 14:56 UTC (permalink / raw
  To: libc-alpha, Palmer Dabbelt, Darius Rad, Andrew Waterman,
	Philipp Tomsich
  Cc: Christoph Müllner

From: Christoph Müllner <christoph.muellner@vrull.eu>

The upcoming RISC-V Ssdtso specification introduces a bit in the senvcfg
CSR to switch the memory consistency model at run-time from RVWMO to TSO
(and back).  The active consistency model can therefore be switched on a
per-hart base and managed by the kernel on a per-process/thread base.

A RFC kernel patchset has been posted that provides a prctl API to get
and set the current consistency model.

This patch attempts to switch to the TSO consistency model in case
the ELF file requires a TSO machine and the machine does not run
in TSO mode.  If the attempt fails, we fall back to the old behaviour
and claim that the ELF file is not compatible with the host.

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 sysdeps/riscv/dl-machine.h | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/sysdeps/riscv/dl-machine.h b/sysdeps/riscv/dl-machine.h
index ce537731dd..65be660a76 100644
--- a/sysdeps/riscv/dl-machine.h
+++ b/sysdeps/riscv/dl-machine.h
@@ -24,6 +24,7 @@
 #include <entry.h>
 #include <elf/elf.h>
 #include <sys/asm.h>
+#include <sys/prctl.h>
 #include <dl-tls.h>
 #include <dl-irel.h>
 #include <dl-static-tls.h>
@@ -72,9 +73,19 @@ elf_machine_matches_host (const ElfW(Ehdr) *ehdr)
     return 0;
 #endif
 
-  /* Execution of TSO binaries is not supported at this time.  */
+  /* Execution of TSO binaries depends on machine's consistency model.  */
   if (ehdr->e_flags & EF_RISCV_TSO)
-    return 0;
+    {
+      /* Attempt to get current consistency model.  */
+      int ret = prctl(PR_GET_MEMORY_CONSISTENCY_MODEL);
+      if (ret == -1)
+	return 0;
+      /* If we have a mismatch, let's try to switch to TSO.  */
+      if (ret != PR_MEMORY_CONSISTENCY_MODEL_RISCV_TSO &&
+	  prctl(PR_SET_MEMORY_CONSISTENCY_MODEL,
+		PR_MEMORY_CONSISTENCY_MODEL_RISCV_TSO))
+	return 0;
+    }
 
   return 1;
 }
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-11-27 14:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-27 14:56 [RFC PATCH 0/2] RISC-V: Add dynamic TSO support Christoph Muellner
2023-11-27 14:56 ` [RFC PATCH 1/2] RISC-V: Move TSO check to elf_machine_matches_host() Christoph Muellner
2023-11-27 14:56 ` [RFC PATCH 2/2] RISC-V: Attempt to enable TSO mode for TSO binaries Christoph Muellner

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).