* [PATCH v3 1/2] linux-user: Allow custom rt signal mappings
2024-10-29 23:17 [PATCH v3 0/2] linux-user: Allow mapping low priority rt signals Ilya Leoshkevich
@ 2024-10-29 23:17 ` Ilya Leoshkevich
2024-10-31 14:35 ` Richard Henderson
2024-10-29 23:17 ` [PATCH v3 2/2] tests/tcg: Add SIGRTMIN/SIGRTMAX test Ilya Leoshkevich
2024-11-04 20:52 ` [PATCH v3 0/2] linux-user: Allow mapping low priority rt signals Richard Henderson
2 siblings, 1 reply; 5+ messages in thread
From: Ilya Leoshkevich @ 2024-10-29 23:17 UTC (permalink / raw)
To: Richard Henderson, Philippe Mathieu-Daudé, Alex Bennée,
Laurent Vivier
Cc: qemu-devel, Michael Tokarev, Brian Cain,
ltaylorsimpson @ gmail . com, Ilya Leoshkevich
Some applications want to use low priority realtime signals (e.g.,
SIGRTMAX). Currently QEMU cannot map all target realtime signals to
host realtime signals, and chooses to sacrifice the end of the target
realtime signal range.
Allow users to choose how to map target realtime signals to host
realtime signals using the new -t option, the new QEMU_RTSIG_MAP
environment variable, and the new -Drtsig_map=\"...\" meson flag.
To simplify things, the meson flag is not per-target, because the
intended use case is app-specific qemu-user builds.
The mapping is specified using the "tsig hsig count[,...]" syntax.
Target realtime signals [tsig,tsig+count) are mapped to host realtime
signals [hsig,hsig+count). Care is taken to avoid double and
out-of-range mappings.
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
linux-user/main.c | 12 +++-
linux-user/signal-common.h | 2 +-
linux-user/signal.c | 108 +++++++++++++++++++++++++++-------
meson.build | 3 +-
meson_options.txt | 2 +
scripts/meson-buildoptions.sh | 2 +
6 files changed, 106 insertions(+), 23 deletions(-)
diff --git a/linux-user/main.c b/linux-user/main.c
index 8143a0d4b02..b09af8d4365 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -412,6 +412,13 @@ static void handle_arg_reserved_va(const char *arg)
reserved_va = val ? val - 1 : 0;
}
+static const char *rtsig_map = CONFIG_QEMU_RTSIG_MAP;
+
+static void handle_arg_rtsig_map(const char *arg)
+{
+ rtsig_map = arg;
+}
+
static void handle_arg_one_insn_per_tb(const char *arg)
{
opt_one_insn_per_tb = true;
@@ -494,6 +501,9 @@ static const struct qemu_argument arg_table[] = {
"address", "set guest_base address to 'address'"},
{"R", "QEMU_RESERVED_VA", true, handle_arg_reserved_va,
"size", "reserve 'size' bytes for guest virtual address space"},
+ {"t", "QEMU_RTSIG_MAP", true, handle_arg_rtsig_map,
+ "tsig hsig n[,...]",
+ "map target rt signals [tsig,tsig+n) to [hsig,hsig+n]"},
{"d", "QEMU_LOG", true, handle_arg_log,
"item[,...]", "enable logging of specified items "
"(use '-d help' for a list of items)"},
@@ -1002,7 +1012,7 @@ int main(int argc, char **argv, char **envp)
target_set_brk(info->brk);
syscall_init();
- signal_init();
+ signal_init(rtsig_map);
/* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
generating the prologue until now so that the prologue can take
diff --git a/linux-user/signal-common.h b/linux-user/signal-common.h
index f4cbe6185e1..8584d9ecc2a 100644
--- a/linux-user/signal-common.h
+++ b/linux-user/signal-common.h
@@ -56,7 +56,7 @@ void setup_rt_frame(int sig, struct target_sigaction *ka,
target_sigset_t *set, CPUArchState *env);
void process_pending_signals(CPUArchState *cpu_env);
-void signal_init(void);
+void signal_init(const char *rtsig_map);
void queue_signal(CPUArchState *env, int sig, int si_type,
target_siginfo_t *info);
void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
diff --git a/linux-user/signal.c b/linux-user/signal.c
index 63ac2df53b7..9b6d772882d 100644
--- a/linux-user/signal.c
+++ b/linux-user/signal.c
@@ -18,6 +18,7 @@
*/
#include "qemu/osdep.h"
#include "qemu/bitops.h"
+#include "qemu/cutils.h"
#include "gdbstub/user.h"
#include "exec/page-protection.h"
#include "hw/core/tcg-cpu-ops.h"
@@ -513,20 +514,81 @@ static int core_dump_signal(int sig)
}
}
-static void signal_table_init(void)
+static void signal_table_init(const char *rtsig_map)
{
int hsig, tsig, count;
+ if (rtsig_map) {
+ /*
+ * Map host RT signals to target RT signals according to the
+ * user-provided specification.
+ */
+ const char *s = rtsig_map;
+
+ while (true) {
+ int i;
+
+ if (qemu_strtoi(s, &s, 10, &tsig) || *s++ != ' ') {
+ fprintf(stderr, "Malformed target signal in QEMU_RTSIG_MAP\n");
+ exit(EXIT_FAILURE);
+ }
+ if (qemu_strtoi(s, &s, 10, &hsig) || *s++ != ' ') {
+ fprintf(stderr, "Malformed host signal in QEMU_RTSIG_MAP\n");
+ exit(EXIT_FAILURE);
+ }
+ if (qemu_strtoi(s, &s, 10, &count) || (*s && *s != ',')) {
+ fprintf(stderr, "Malformed signal count in QEMU_RTSIG_MAP\n");
+ exit(EXIT_FAILURE);
+ }
+
+ for (i = 0; i < count; i++, tsig++, hsig++) {
+ if (tsig < TARGET_SIGRTMIN || tsig > TARGET_NSIG) {
+ fprintf(stderr, "%d is not a target rt signal\n", tsig);
+ exit(EXIT_FAILURE);
+ }
+ if (hsig < SIGRTMIN || hsig > SIGRTMAX) {
+ fprintf(stderr, "%d is not a host rt signal\n", hsig);
+ exit(EXIT_FAILURE);
+ }
+ if (host_to_target_signal_table[hsig]) {
+ fprintf(stderr, "%d already maps %d\n",
+ hsig, host_to_target_signal_table[hsig]);
+ exit(EXIT_FAILURE);
+ }
+ host_to_target_signal_table[hsig] = tsig;
+ }
+
+ if (*s) {
+ s++;
+ } else {
+ break;
+ }
+ }
+ } else {
+ /*
+ * Default host-to-target RT signal mapping.
+ *
+ * Signals are supported starting from TARGET_SIGRTMIN and going up
+ * until we run out of host realtime signals. Glibc uses the lower 2
+ * RT signals and (hopefully) nobody uses the upper ones.
+ * This is why SIGRTMIN (34) is generally greater than __SIGRTMIN (32).
+ * To fix this properly we would need to do manual signal delivery
+ * multiplexed over a single host signal.
+ * Attempts for configure "missing" signals via sigaction will be
+ * silently ignored.
+ *
+ * Reserve one signal for internal usage (see below).
+ */
+
+ hsig = SIGRTMIN + 1;
+ for (tsig = TARGET_SIGRTMIN;
+ hsig <= SIGRTMAX && tsig <= TARGET_NSIG;
+ hsig++, tsig++) {
+ host_to_target_signal_table[hsig] = tsig;
+ }
+ }
+
/*
- * Signals are supported starting from TARGET_SIGRTMIN and going up
- * until we run out of host realtime signals. Glibc uses the lower 2
- * RT signals and (hopefully) nobody uses the upper ones.
- * This is why SIGRTMIN (34) is generally greater than __SIGRTMIN (32).
- * To fix this properly we would need to do manual signal delivery
- * multiplexed over a single host signal.
- * Attempts for configure "missing" signals via sigaction will be
- * silently ignored.
- *
* Remap the target SIGABRT, so that we can distinguish host abort
* from guest abort. When the guest registers a signal handler or
* calls raise(SIGABRT), the host will raise SIG_RTn. If the guest
@@ -536,21 +598,27 @@ static void signal_table_init(void)
* parent sees the correct mapping from wait status.
*/
- hsig = SIGRTMIN;
host_to_target_signal_table[SIGABRT] = 0;
- host_to_target_signal_table[hsig++] = TARGET_SIGABRT;
-
- for (tsig = TARGET_SIGRTMIN;
- hsig <= SIGRTMAX && tsig <= TARGET_NSIG;
- hsig++, tsig++) {
- host_to_target_signal_table[hsig] = tsig;
+ for (hsig = SIGRTMIN; hsig <= SIGRTMAX; hsig++) {
+ if (!host_to_target_signal_table[hsig]) {
+ host_to_target_signal_table[hsig] = TARGET_SIGABRT;
+ break;
+ }
+ }
+ if (hsig > SIGRTMAX) {
+ fprintf(stderr, "No rt signals left for SIGABRT mapping\n");
+ exit(EXIT_FAILURE);
}
/* Invert the mapping that has already been assigned. */
for (hsig = 1; hsig < _NSIG; hsig++) {
tsig = host_to_target_signal_table[hsig];
if (tsig) {
- assert(target_to_host_signal_table[tsig] == 0);
+ if (target_to_host_signal_table[tsig]) {
+ fprintf(stderr, "%d is already mapped to %d\n",
+ tsig, target_to_host_signal_table[tsig]);
+ exit(EXIT_FAILURE);
+ }
target_to_host_signal_table[tsig] = hsig;
}
}
@@ -573,13 +641,13 @@ static void signal_table_init(void)
trace_signal_table_init(count);
}
-void signal_init(void)
+void signal_init(const char *rtsig_map)
{
TaskState *ts = get_task_state(thread_cpu);
struct sigaction act, oact;
/* initialize signal conversion tables */
- signal_table_init();
+ signal_table_init(rtsig_map);
/* Set the signal mask from the host mask. */
sigprocmask(0, 0, &ts->signal_mask);
diff --git a/meson.build b/meson.build
index 85594fd3f18..bc282c8d187 100644
--- a/meson.build
+++ b/meson.build
@@ -3167,7 +3167,8 @@ foreach target : target_dirs
config_target += {
'CONFIG_USER_ONLY': 'y',
'CONFIG_QEMU_INTERP_PREFIX':
- get_option('interp_prefix').replace('%M', config_target['TARGET_NAME'])
+ get_option('interp_prefix').replace('%M', config_target['TARGET_NAME']),
+ 'CONFIG_QEMU_RTSIG_MAP': get_option('rtsig_map'),
}
endif
diff --git a/meson_options.txt b/meson_options.txt
index 0ee4d7bb86b..b61808b46a6 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -27,6 +27,8 @@ option('block_drv_ro_whitelist', type : 'string', value : '',
description: 'set block driver read-only whitelist (by default affects only QEMU, not tools like qemu-img)')
option('interp_prefix', type : 'string', value : '/usr/gnemul/qemu-%M',
description: 'where to find shared libraries etc., use %M for cpu name')
+option('rtsig_map', type : 'string', value : 'NULL',
+ description: 'default value of QEMU_RTSIG_MAP')
option('fuzzing_engine', type : 'string', value : '',
description: 'fuzzing engine library for OSS-Fuzz')
option('trace_file', type: 'string', value: 'trace',
diff --git a/scripts/meson-buildoptions.sh b/scripts/meson-buildoptions.sh
index 6d08605b771..9684f82ffbb 100644
--- a/scripts/meson-buildoptions.sh
+++ b/scripts/meson-buildoptions.sh
@@ -72,6 +72,7 @@ meson_options_help() {
printf "%s\n" ' "manufacturer" name for qemu-ga registry entries'
printf "%s\n" ' [QEMU]'
printf "%s\n" ' --qemu-ga-version=VALUE version number for qemu-ga installer'
+ printf "%s\n" ' --rtsig-map=VALUE default value of QEMU_RTSIG_MAP [NULL]'
printf "%s\n" ' --smbd=VALUE Path to smbd for slirp networking'
printf "%s\n" ' --sysconfdir=VALUE Sysconf data directory [etc]'
printf "%s\n" ' --tls-priority=VALUE Default TLS protocol/cipher priority string'
@@ -457,6 +458,7 @@ _meson_option_parse() {
--disable-replication) printf "%s" -Dreplication=disabled ;;
--enable-rng-none) printf "%s" -Drng_none=true ;;
--disable-rng-none) printf "%s" -Drng_none=false ;;
+ --rtsig-map=*) quote_sh "-Drtsig_map=$2" ;;
--enable-rust) printf "%s" -Drust=enabled ;;
--disable-rust) printf "%s" -Drust=disabled ;;
--enable-rutabaga-gfx) printf "%s" -Drutabaga_gfx=enabled ;;
--
2.47.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v3 2/2] tests/tcg: Add SIGRTMIN/SIGRTMAX test
2024-10-29 23:17 [PATCH v3 0/2] linux-user: Allow mapping low priority rt signals Ilya Leoshkevich
2024-10-29 23:17 ` [PATCH v3 1/2] linux-user: Allow custom rt signal mappings Ilya Leoshkevich
@ 2024-10-29 23:17 ` Ilya Leoshkevich
2024-11-04 20:52 ` [PATCH v3 0/2] linux-user: Allow mapping low priority rt signals Richard Henderson
2 siblings, 0 replies; 5+ messages in thread
From: Ilya Leoshkevich @ 2024-10-29 23:17 UTC (permalink / raw)
To: Richard Henderson, Philippe Mathieu-Daudé, Alex Bennée,
Laurent Vivier
Cc: qemu-devel, Michael Tokarev, Brian Cain,
ltaylorsimpson @ gmail . com, Ilya Leoshkevich
Test the lowest and the highest real-time signals. This requires
configuring the real-time signal mapping, and therefore some knowledge
about the host. To this end, pass the emulator path in the QEMU
environment variable to all tests (this should not disturb the existing
ones), and assume that all hosts have signals 36-39 available.
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
tests/tcg/Makefile.target | 4 +-
tests/tcg/multiarch/linux/linux-sigrtminmax.c | 74 +++++++++++++++++++
2 files changed, 76 insertions(+), 2 deletions(-)
create mode 100644 tests/tcg/multiarch/linux/linux-sigrtminmax.c
diff --git a/tests/tcg/Makefile.target b/tests/tcg/Makefile.target
index 9722145b976..95ff76ea44d 100644
--- a/tests/tcg/Makefile.target
+++ b/tests/tcg/Makefile.target
@@ -179,10 +179,10 @@ run-plugin-%-with-libmem.so: PLUGIN_ARGS=$(COMMA)inline=true
ifeq ($(filter %-softmmu, $(TARGET)),)
run-%: %
- $(call run-test, $<, $(QEMU) $(QEMU_OPTS) $<)
+ $(call run-test, $<, env QEMU=$(QEMU) $(QEMU) $(QEMU_OPTS) $<)
run-plugin-%:
- $(call run-test, $@, $(QEMU) $(QEMU_OPTS) \
+ $(call run-test, $@, env QEMU=$(QEMU) $(QEMU) $(QEMU_OPTS) \
-plugin $(PLUGIN_LIB)/$(call extract-plugin,$@)$(PLUGIN_ARGS) \
-d plugin -D $*.pout \
$(call strip-plugin,$<))
diff --git a/tests/tcg/multiarch/linux/linux-sigrtminmax.c b/tests/tcg/multiarch/linux/linux-sigrtminmax.c
new file mode 100644
index 00000000000..a7059aacd9c
--- /dev/null
+++ b/tests/tcg/multiarch/linux/linux-sigrtminmax.c
@@ -0,0 +1,74 @@
+/*
+ * Test the lowest and the highest real-time signals.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include <assert.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+/* For hexagon and microblaze. */
+#ifndef __SIGRTMIN
+#define __SIGRTMIN 32
+#endif
+
+extern char **environ;
+
+static bool seen_sigrtmin, seen_sigrtmax;
+
+static void handle_signal(int sig)
+{
+ if (sig == SIGRTMIN) {
+ seen_sigrtmin = true;
+ } else if (sig == SIGRTMAX) {
+ seen_sigrtmax = true;
+ } else {
+ _exit(1);
+ }
+}
+
+int main(int argc, char **argv)
+{
+ char *qemu = getenv("QEMU");
+ struct sigaction act;
+
+ assert(qemu);
+
+ if (!getenv("QEMU_RTSIG_MAP")) {
+ char **new_argv = malloc((argc + 2) + sizeof(char *));
+ int tsig1, hsig1, count1, tsig2, hsig2, count2;
+ char rt_sigmap[64];
+
+ /* Re-exec with a mapping that includes SIGRTMIN and SIGRTMAX. */
+ new_argv[0] = qemu;
+ memcpy(&new_argv[1], argv, (argc + 1) * sizeof(char *));
+ tsig1 = __SIGRTMIN;
+ /* The host must have a few signals starting from this one. */
+ hsig1 = 36;
+ count1 = SIGRTMIN - __SIGRTMIN + 1;
+ tsig2 = SIGRTMAX;
+ hsig2 = hsig1 + count1;
+ count2 = 1;
+ snprintf(rt_sigmap, sizeof(rt_sigmap), "%d %d %d,%d %d %d",
+ tsig1, hsig1, count1, tsig2, hsig2, count2);
+ setenv("QEMU_RTSIG_MAP", rt_sigmap, 0);
+ assert(execve(new_argv[0], new_argv, environ) == 0);
+ return EXIT_FAILURE;
+ }
+
+ memset(&act, 0, sizeof(act));
+ act.sa_handler = handle_signal;
+ assert(sigaction(SIGRTMIN, &act, NULL) == 0);
+ assert(sigaction(SIGRTMAX, &act, NULL) == 0);
+
+ assert(kill(getpid(), SIGRTMIN) == 0);
+ assert(seen_sigrtmin);
+ assert(kill(getpid(), SIGRTMAX) == 0);
+ assert(seen_sigrtmax);
+
+ return EXIT_SUCCESS;
+}
--
2.47.0
^ permalink raw reply related [flat|nested] 5+ messages in thread