user: add runtime switch to call safe_syscall via libc
PATCH v2 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 controls the behavior, which can be enabled in ./configure.
Configure option: --enable-libc-syscalls
Command line: -libc-syscalls
Environment: QEMU_LIBC_SYSCALLS
This preserves the existing signal-rewind semantics via the assembly path by default, while enabling optional libc-based integration when requested.
Signed-off-by Balint Reczey <[email protected]>
--- bsd-user/main.c | 20 +++++++++++ common-user/meson.build | 4 +++ common-user/safe-syscall.c | 66 +++++++++++++++++++++++++++++++++++ docs/user/main.rst | 30 ++++++++++++++-- include/user/safe-syscall.h | 19 +++++++++- linux-user/main.c | 15 ++++++++ meson.build | 1 + meson_options.txt | 4 +++ scripts/meson-buildoptions.sh | 4 +++ 9 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 common-user/safe-syscall.c
bsd-user/main.c +20 -0
@@ -38,6 +38,9 @@#include "qemu/plugin.h" #include "user/guest-base.h" #include "user/page-protection.h" +#ifdef CONFIG_LIBC_SYSCALLS +#include "user/safe-syscall.h" +#endif #include "accel/accel-ops.h" #include "tcg/startup.h" #include "qemu/timer.h"
@@ -166,6 +169,9 @@ 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" +#ifdef CONFIG_LIBC_SYSCALLS + "-libc-syscalls use libc syscall() instead of assembly safe-syscall\n" +#endif"\n" "Debug options:\n" "-d item1[,...] enable logging of specified items\n"
@@ -183,6 +189,10 @@ 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" +#ifdef CONFIG_LIBC_SYSCALLS + "QEMU_LIBC_SYSCALLS Use libc syscall() instead of assembly safe-syscall.\n" + " Enable by setting to any value.\n" +#endif"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 +320,12 @@ int main(int argc, char **argv)qemu_add_opts (&qemu_trace_opts);qemu_plugin_add_opts (); +#ifdef CONFIG_LIBC_SYSCALLS +if (getenv ("QEMU_LIBC_SYSCALLS" )) { + qemu_use_libc_syscalls = true; + } +#endif + optind =1 ;for (;;) {if (optind >= argc) {
@@ -380,6 +396,10 @@ int main(int argc, char **argv) have_guest_base = true; }else if (!strcmp (r,"drop-ld-preload" )) { (void )envlist_unsetenv (envlist,"LD_PRELOAD" ); +#ifdef CONFIG_LIBC_SYSCALLS + }else if (!strcmp (r,"libc-syscalls" )) { + qemu_use_libc_syscalls = true; +#endif }else if (!strcmp (r,"seed" )) { seed_optarg = optarg; }else if (!strcmp (r,"one-insn-per-tb" )) {
common-user/meson.build +4 -0
@@ -8,3 +8,7 @@ user_ss.add(files( 'safe-syscall.S', 'safe-syscall-error.c', )) + +if get_option('libc_syscalls').enabled() + user_ss.add(files('safe-syscall.c')) +endif
common-user/safe-syscall.c new file +66 -0
66 lines not on this page · raw message
docs/user/main.rst +28 -2
@@ -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-syscalls] program [arguments...] ``-h`` Print the help
@@ -101,6 +101,16 @@ Command line options bytes). \"G\", \"M\", and \"k\" suffixes may be used when specifying the size. +``-libc-syscalls`` + Use the host C library's ``syscall()`` entry point for guest system calls + instead of QEMU's built-in assembly safe-syscall. 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 other special scenarios (for example + when interposing on ``syscall()`` via ``LD_PRELOAD``). Available on Linux + and BSD user-mode builds. This option is only present if QEMU was configured + with ``-Dlibc_syscalls=enabled``. + Debug options: ``-d item1,...``
@@ -135,6 +145,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_SYSCALLS + When set to a non-empty value, behave as if ``-libc-syscalls`` was specified + on the command line. Defaults to disabled. + Other binaries ~~~~~~~~~~~~~~
@@ -231,7 +245,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-syscalls] program [arguments...] ``-h`` Print the help
@@ -256,6 +270,12 @@ Command line options Set the type of the emulated BSD Operating system. Valid values are FreeBSD, NetBSD and OpenBSD (default). +``-libc-syscalls`` + Use the host C library's ``syscall()`` entry point for guest system calls + instead of QEMU's built-in assembly safe-syscall. See the Linux user-mode + option of the same name for details. Defaults to disabled. This option is + only present if QEMU was configured with ``-Dlibc_syscalls=enabled``. + Debug options: ``-d item1,...``
@@ -266,3 +286,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_SYSCALLS + When set to a non-empty value, behave as if ``-libc-syscalls`` was specified + on the command line. Defaults to disabled.
include/user/safe-syscall.h +18 -1
@@ -128,13 +128,30 @@/* 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); +#ifdef CONFIG_LIBC_SYSCALLS +/* This is implemented in C.*/ +long safe_syscall_libc (int *pending,long number, ...); +extern bool qemu_use_libc_syscalls; +#endif -/* These are defined by the safe-syscall.inc.S file */ +/* +* These are defined by the safe-syscall.inc.S file. +* In the C implementation, they are dummy symbols. +*/ extern char safe_syscall_start[];extern char safe_syscall_end[]; +#ifdef CONFIG_LIBC_SYSCALLS +#define safe_syscall (...) \ + (qemu_use_libc_syscalls ? \ + safe_syscall_libc(&get_task_state(thread_cpu)->signal_pending, \ + __VA_ARGS__) : \ + safe_syscall_base(&get_task_state(thread_cpu)->signal_pending, \ + __VA_ARGS__)) +#else #define safe_syscall (...) \ safe_syscall_base(&get_task_state(thread_cpu)->signal_pending, \ __VA_ARGS__) +#endif #endif
linux-user/main.c +15 -0
@@ -40,6 +40,9 @@#include "qemu/plugin.h" #include "user/guest-base.h" #include "user/page-protection.h" +#ifdef CONFIG_LIBC_SYSCALLS +#include "user/safe-syscall.h" +#endif #include "exec/gdbstub.h" #include "gdbstub/user.h" #include "accel/accel-ops.h"
@@ -456,6 +459,14 @@ static void handle_arg_jitdump(const char *arg)perf_enable_jitdump (); } +#ifdef CONFIG_LIBC_SYSCALLS +static void handle_arg_libc_syscalls (const char *arg) +{ +/* Enable libc-backed syscall implementation */ + qemu_use_libc_syscalls = true; +} +#endif +static QemuPluginList plugins =QTAILQ_HEAD_INITIALIZER (plugins);#ifdef CONFIG_PLUGIN
@@ -534,6 +545,10 @@ 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"}, +#ifdef CONFIG_LIBC_SYSCALLS + {"libc-syscalls", "QEMU_LIBC_SYSCALLS", false, handle_arg_libc_syscalls, + "", "use libc syscall() instead of assembly safe-syscall"}, +#endif {NULL, NULL, false, NULL, NULL, NULL} };
meson.build +1 -0
@@ -2508,6 +2508,7 @@ if numa.found() endif config_host_data.set('CONFIG_OPENGL', opengl.found()) config_host_data.set('CONFIG_PLUGIN', get_option('plugins')) +config_host_data.set('CONFIG_LIBC_SYSCALLS', get_option('libc_syscalls').enabled()) config_host_data.set('CONFIG_RBD', rbd.found()) config_host_data.set('CONFIG_RDMA', rdma.found()) config_host_data.set('CONFIG_RELOCATABLE', get_option('relocatable'))
meson_options.txt +4 -0
@@ -66,6 +66,10 @@ option('tools', type : 'feature', value : 'auto', option('qga_vss', type : 'feature', value: 'auto', description: 'build QGA VSS support (broken with MinGW)') +# Enable libc-backed safe_syscall in user-mode (-libc-syscalls option) +option('libc_syscalls', type: 'feature', value: 'disabled', + description: 'Enable libc-backed safe_syscall() and -libc-syscalls in user-mode (POTENTIALLY UNSAFE SIGNAL HANDLING)') + option('malloc_trim', type : 'feature', value : 'auto', description: 'enable libc malloc_trim() for memory optimization') option('malloc', type : 'combo', choices : ['system', 'tcmalloc', 'jemalloc'],
scripts/meson-buildoptions.sh +4 -0
@@ -136,6 +136,8 @@ meson_options_help() { printf "%s\n" ' keyring Linux keyring support' printf "%s\n" ' kvm KVM acceleration support' printf "%s\n" ' l2tpv3 l2tpv3 network backend support' + printf "%s\n" ' libc-syscalls Enable libc-backed safe_syscall() and -libc-syscalls in' + printf "%s\n" ' user-mode (POTENTIALLY UNSAFE SIGNAL HANDLING)' printf "%s\n" ' libcbor libcbor support' printf "%s\n" ' libdaxctl libdaxctl support' printf "%s\n" ' libdw debuginfo support'
@@ -365,6 +367,8 @@ _meson_option_parse() { --disable-kvm) printf "%s" -Dkvm=disabled ;; --enable-l2tpv3) printf "%s" -Dl2tpv3=enabled ;; --disable-l2tpv3) printf "%s" -Dl2tpv3=disabled ;; + --enable-libc-syscalls) printf "%s" -Dlibc_syscalls=enabled ;; + --disable-libc-syscalls) printf "%s" -Dlibc_syscalls=disabled ;; --enable-libcbor) printf "%s" -Dlibcbor=enabled ;; --disable-libcbor) printf "%s" -Dlibcbor=disabled ;; --enable-libdaxctl) printf "%s" -Dlibdaxctl=enabled ;;
Signature · 1 line
2.43.0