Skip to contentSkip to conversation

user: add runtime switch to call safe_syscall via libc

PATCH New Balint Reczey 1 participant 1 message Nov 4, 2025 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 user: add runtime switch to call safe_syscall via libc

Add a libc-backed path for safe_syscall() that make syscalls via libc's syscall(). This enables interposing syscalls via LD_PRELOAD when running static guest binaries under a dynamically linked qemu-user.

The assembly implementation (safe_syscall_base()) remains the default. A runtime switch or a set environment variable changes the behavior:

Command line: -libc-syscall
Environment: QEMU_LIBC_SYSCALL

---
 bsd-user/main.c             | 11 +++++++
 common-user/meson.build     |  1 +
 common-user/safe-syscall.c  | 57 +++++++++++++++++++++++++++++++++++++
 docs/user/main.rst          | 28 ++++++++++++++++--
 include/user/safe-syscall.h | 25 +++++++++++-----
 linux-user/main.c           |  9 ++++++
 6 files changed, 122 insertions(+), 9 deletions(-)
 create mode 100644 common-user/safe-syscall.c
bsd-user/main.c +11 -0
index 73aae8c327..9b3ff67859 100644
@@ -38,6 +38,7 @@
 #include "qemu/plugin.h"
 #include "user/guest-base.h"
 #include "user/page-protection.h"
+#include "user/safe-syscall.h"
 #include "accel/accel-ops.h"
 #include "tcg/startup.h"
 #include "qemu/timer.h"
@@ -166,6 +167,7 @@ static void usage(void)
            "-E var=value      sets/modifies targets environment variable(s)\n"
            "-U var            unsets targets environment variable(s)\n"
            "-B address        set guest_base address to address\n"
+           "-libc-syscall     use libc syscall() instead of assembly safe-syscall\n"
            "\n"
            "Debug options:\n"
            "-d item1[,...]    enable logging of specified items\n"
@@ -183,6 +185,8 @@ static void usage(void)
            "Environment variables:\n"
            "QEMU_STRACE       Print system calls and arguments similar to the\n"
            "                  'strace' program.  Enable by setting to any value.\n"
+           "QEMU_LIBC_SYSCALL Use libc syscall() instead of assembly safe-syscall.\n"
+           "                  Enable by setting to any value.\n"
            "You can use -E and -U options to set/unset environment variables\n"
            "for target process.  It is possible to provide several variables\n"
            "by repeating the option.  For example:\n"
@@ -310,6 +314,11 @@ int main(int argc, char **argv)
     qemu_add_opts(&qemu_trace_opts);
     qemu_plugin_add_opts();
 
+    /* Check QEMU_LIBC_SYSCALL environment variable */
+    if (getenv("QEMU_LIBC_SYSCALL")) {
+        qemu_use_libc_syscall = true;
+    }
+
     optind = 1;
     for (;;) {
         if (optind >= argc) {
@@ -380,6 +389,8 @@ int main(int argc, char **argv)
             have_guest_base = true;
         } else if (!strcmp(r, "drop-ld-preload")) {
             (void) envlist_unsetenv(envlist, "LD_PRELOAD");
+        } else if (!strcmp(r, "libc-syscall")) {
+            qemu_use_libc_syscall = true;
         } else if (!strcmp(r, "seed")) {
             seed_optarg = optarg;
         } else if (!strcmp(r, "one-insn-per-tb")) {
common-user/meson.build +1 -0
index ac9de5b9e3..d44ffe1f56 100644
@@ -7,4 +7,5 @@ common_user_inc += include_directories('host/' / host_arch)
 user_ss.add(files(
   'safe-syscall.S',
   'safe-syscall-error.c',
+  'safe-syscall.c',
 ))
common-user/safe-syscall.c new file +57 -0

57 lines not on this page · raw message

docs/user/main.rst +26 -2
index a8ddf91424..6b7e76dfe1 100644
@@ -70,7 +70,7 @@ Command line options
 
 ::
 
-   qemu-i386 [-h] [-d] [-L path] [-s size] [-cpu model] [-g endpoint] [-B offset] [-R size] program [arguments...]
+   qemu-i386 [-h] [-d] [-L path] [-s size] [-cpu model] [-g endpoint] [-B offset] [-R size] [-libc-syscall] program [arguments...]
 
 ``-h``
    Print the help
@@ -101,6 +101,15 @@ Command line options
    bytes). \"G\", \"M\", and \"k\" suffixes may be used when specifying
    the size.
 
+``-libc-syscall``
+   Use the host C library's ``syscall()`` entry point for guest system calls
+   instead of QEMU's built-in safe-syscall trampoline. By default this option
+   is disabled and QEMU uses its internal assembly implementation for
+   performance and precise control of signal-restart semantics. This switch is
+   primarily intended for debugging and integration scenarios (for example
+   when interposing on ``syscall()`` via ``LD_PRELOAD``). Available on Linux
+   and BSD user-mode builds.
+
 Debug options:
 
 ``-d item1,...``
@@ -135,6 +144,10 @@ QEMU_STRACE
    format are printed with information for six arguments. Many
    flag-style arguments don't have decoders and will show up as numbers.
 
+QEMU_LIBC_SYSCALL
+   When set to a non-empty value, behave as if ``-libc-syscall`` was specified
+   on the command line. Defaults to disabled.
+
 Other binaries
 ~~~~~~~~~~~~~~
 
@@ -231,7 +244,7 @@ Command line options
 
 ::
 
-   qemu-sparc64 [-h] [-d] [-L path] [-s size] [-bsd type] program [arguments...]
+   qemu-sparc64 [-h] [-d] [-L path] [-s size] [-bsd type] [-libc-syscall] program [arguments...]
 
 ``-h``
    Print the help
@@ -256,6 +269,11 @@ Command line options
    Set the type of the emulated BSD Operating system. Valid values are
    FreeBSD, NetBSD and OpenBSD (default).
 
+``-libc-syscall``
+   Use the host C library's ``syscall()`` entry point for guest system calls
+   instead of QEMU's built-in safe-syscall trampoline. See the Linux user-mode
+   option of the same name for details. Defaults to disabled.
+
 Debug options:
 
 ``-d item1,...``
@@ -266,3 +284,9 @@ Debug options:
    Run the emulation with one guest instruction per translation block.
    This slows down emulation a lot, but can be useful in some situations,
    such as when trying to analyse the logs produced by the ``-d`` option.
+
+Environment variables:
+
+QEMU_LIBC_SYSCALL
+   When set to a non-empty value, behave as if ``-libc-syscall`` was specified
+   on the command line. Defaults to disabled.
include/user/safe-syscall.h +18 -7
index aa075f4d5c..02a95c24e9 100644
@@ -125,16 +125,27 @@
  * kinds of restartability.
  */
 
-/* The core part of this function is implemented in assembly */
-long safe_syscall_base(int *pending, long number, ...);
-long safe_syscall_set_errno_tail(int value);
+/*
+ * The core part remains implemented in assembly; a C dispatcher selects
+ * runtime path.
+ */
+extern long safe_syscall_base(int *pending, long number, ...);
+extern long safe_syscall_set_errno_tail(int value);
+extern long safe_syscall_libc(int *pending, long number, ...);
+extern bool qemu_use_libc_syscall;
 
-/* These are defined by the safe-syscall.inc.S file */
+/*
+ * These symbols are defined for compatibility with signal handling code.
+ * In the C implementation, they are dummy symbols.
+ */
 extern char safe_syscall_start[];
 extern char safe_syscall_end[];
 
-#define safe_syscall(...)                                                 \
-    safe_syscall_base(&get_task_state(thread_cpu)->signal_pending,        \
-                      __VA_ARGS__)
+#define safe_syscall(...)                                               \
+    (qemu_use_libc_syscall ?                                            \
+     safe_syscall_libc(&get_task_state(thread_cpu)->signal_pending,     \
+                       __VA_ARGS__) :                                   \
+     safe_syscall_base(&get_task_state(thread_cpu)->signal_pending,     \
+                       __VA_ARGS__))
 
 #endif
linux-user/main.c +9 -0
index db751c0757..de2a20efb4 100644
@@ -40,6 +40,7 @@
 #include "qemu/plugin.h"
 #include "user/guest-base.h"
 #include "user/page-protection.h"
+#include "user/safe-syscall.h"
 #include "exec/gdbstub.h"
 #include "gdbstub/user.h"
 #include "accel/accel-ops.h"
@@ -456,6 +457,12 @@ static void handle_arg_jitdump(const char *arg)
     perf_enable_jitdump();
 }
 
+static void handle_arg_libc_syscall(const char *arg)
+{
+    /* Enable libc-backed syscall implementation */
+    qemu_use_libc_syscall = true;
+}
+
 static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
 
 #ifdef CONFIG_PLUGIN
@@ -534,6 +541,8 @@ static const struct qemu_argument arg_table[] = {
      "",           "Generate a /tmp/perf-${pid}.map file for perf"},
     {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
      "",           "Generate a jit-${pid}.dump file for perf"},
+    {"libc-syscall", "QEMU_LIBC_SYSCALL", false, handle_arg_libc_syscall,
+     "",           "use libc syscall() instead of assembly safe-syscall"},
     {NULL, NULL, false, NULL, NULL, NULL}
 };
 
Signature · 1 line
2.43.0