From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F3A39C982D2 for ; Fri, 18 Sep 2026 04:27:36 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1x7QA1-0005K5-48; Fri, 18 Sep 2026 00:25:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1x7Q9z-0005JD-7u for qemu-devel@nongnu.org; Fri, 18 Sep 2026 00:25:15 -0400 Received: from mail-oi2-x10.google.com ([2607:f8b0:4864:32::10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1x7Q9x-00049Q-7v for qemu-devel@nongnu.org; Fri, 18 Sep 2026 00:25:15 -0400 Received: by mail-oi2-x10.google.com with SMTP id 46e09a7af769-7f4f0d37f9dso102671a34.1 for ; Thu, 17 Sep 2026 21:25:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20251104.gappssmtp.com; s=20251104; t=1789705512; x=1790310312; darn=nongnu.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=XelgfWwdeNzcqaiPuyQIjxp/lcvMRmayPY+XsptkFtA=; b=KRHXfqBVbMa/qAx2veIBo34IMddBE82Y80G3d+8f43N4L+rbpDGhLSc+mB3rdyr8dr /q4dXpF/GSgZiZnVtnS959Dv3IhRJ5HNwJT6gMyHiH1WD20MC3Y8s4Kpe7JgfuD7rT+K TiW89Xymoz083LC6NdkFzQnrLZww3rbo+KU5BfN1p42Fm/V+/TAtC3OaKlqD5tyuduOd F0rsroEpRsCVDTv8hCgolpbOCzzOE5imE8Qk7zAdNreInQ0O+PYho+idYsE/2fvk39Tf 6TlZ+akQ1FnLdAtdWDoUKgYJGolrK8uK1xrs3sFVLSWB/u/GNXBOX2t29uoq44boaCLY 5ZUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789705512; x=1790310312; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=XelgfWwdeNzcqaiPuyQIjxp/lcvMRmayPY+XsptkFtA=; b=fs0xtgv9jjckxYkO1wdtRvRWJ3itbi084UITDCe8rPtG/ay/UMdnUVOyiQslKeLEBr Hhgr4ICDbWlZ0esXurxhGYBRbEeeBF65DJsdaBRnXAgA8rGvEX9BWfm78u73ZBmuOX8j PxAYQnCnmF2E9eNaA+A/rROX1yJBZS3cS4NP7lGKYQZSbmrHu7RXj33zlS2VVAP9tyhl N5ZhA4ghob2XMxTHZR8tvxminksFPo033bVvd8tFerFV3e1lyDPqsGUQLG4+cQCcFrsI ga9FTjOssBn5NVNikcccZi6moSeeYRi7cf/SYAjYDjM8UynWvK9NwoA3cldKRtcrt1dE w0ug== X-Gm-Message-State: AFuF++kajDNedU1B50Aa0VWwfjH0QbbNGx/qputnYOhwqJJ8ZBxlNoeP KvcdYwXKgIhsjobQsGwulnbcbX7VjU3wV7NhYoH/1iXALcNJGWO6LtwfMYoAnd3oshw9QSZzv6C e9dxtsK0= X-Gm-Gg: AYBFou15ccKOkLiUwu363NPH5OPt9cstoWnpfThfku+iq1q3z8tsHlep01LBkCOdbAH kNXMC+UDZImdbsRQm+9V94zrhrsWTZyCrdrZy+QlOcfNctklLz8Npy9wwbYliZcvQw15CtPGBQv 0QJ43armIZyBL9SZgJ2vDYleu7V2XkGsuaVE4/3foxOo5fxD0PQmul+94o710+QoDsVo9O/I+Th TruuTgJChC6W80clWYExHdB4HBcrd7RN+o5uid6ABHg2GRBiMNgJ2oqvV6ijl8NwoUiWR5XWs1N MZWDpzuiVlVVIuIANasJcrFuLHn5iXu11Z8RyF5A3aybg9rSfH+CKqs30X9+YRvgtP/NYt1uSvR hhZgz7YTskOmcI7MVIwnNGgCHX/jSXx256xjScr5Rsl/P1YoRnbd8joCKIkBHmkN61btpRdMZYV GmnYQz5cWbJcZ3xPC50YYL5ZhD/g7WnPARyPzo8Y7q2aXzIGVCmcaRzphrsw== X-Received: by 2002:a05:6830:2116:b0:7fa:5c49:5251 with SMTP id 46e09a7af769-80de34ee1e3mr1455728a34.24.1789705511996; Thu, 17 Sep 2026 21:25:11 -0700 (PDT) Received: from rebo.bsdimp.com ([50.253.99.174]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-80e51d627b5sm489718a34.20.2026.09.17.21.25.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Sep 2026 21:25:10 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: Kyle Evans , Warner Losh , Stacey Son , Kyle Evans , Jessica Clarke , =?UTF-8?q?Mika=C3=ABl=20Urankar?= , Pierrick Bouvier , Richard Henderson Subject: [PULL 04/41] bsd-user: Add bsd-signal.h with sigaction through sigreturn Date: Thu, 17 Sep 2026 22:23:45 -0600 Message-ID: <20260918042422.38713-5-imp@bsdimp.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260918042422.38713-1-imp@bsdimp.com> References: <20260918042422.38713-1-imp@bsdimp.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: none client-ip=2607:f8b0:4864:32::10; envelope-from=imp@bsdimp.com; helo=mail-oi2-x10.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Add the first set of signal-related system call shims: sigaction, sigprocmask, sigpending, sigsuspend, and sigreturn. Also add the do_sigprocmask and target_to_host_sigevent helper functions to signal.c and their declarations to signal-common.h. Signed-off-by: Stacey Son Signed-off-by: Kyle Evans Signed-off-by: Jessica Clarke Signed-off-by: Mikaƫl Urankar Assisted-by: Claude Opus 4.6 (1M context) Reviewed-by: Pierrick Bouvier Reviewed-by: Richard Henderson Acked-by: Richard Henderson Signed-off-by: Warner Losh --- bsd-user/bsd-signal.h | 151 ++++++++++++++++++++++++++++++++++ bsd-user/freebsd/os-syscall.c | 1 + bsd-user/signal-common.h | 4 + bsd-user/signal.c | 67 +++++++++++++++ 4 files changed, 223 insertions(+) create mode 100644 bsd-user/bsd-signal.h diff --git a/bsd-user/bsd-signal.h b/bsd-user/bsd-signal.h new file mode 100644 index 0000000000..49e58d7436 --- /dev/null +++ b/bsd-user/bsd-signal.h @@ -0,0 +1,151 @@ +/* + * signal related system call shims + * + * Copyright (c) 2013 Stacey D. Son + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#ifndef BSD_SIGNAL_H +#define BSD_SIGNAL_H + +#include + +/* sigaction(2) */ +static inline abi_long do_bsd_sigaction(abi_long arg1, abi_long arg2, + abi_long arg3) +{ + abi_long ret; + struct target_sigaction *old_act, act, oact, *pact; + + if (arg2) { + if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1)) { + return -TARGET_EFAULT; + } + act._sa_handler = old_act->_sa_handler; + act.sa_flags = old_act->sa_flags; + memcpy(&act.sa_mask, &old_act->sa_mask, sizeof(target_sigset_t)); + unlock_user_struct(old_act, arg2, 0); + pact = &act; + } else { + pact = NULL; + } + ret = get_errno(do_sigaction(arg1, pact, &oact)); + if (!is_error(ret) && arg3) { + if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0)) { + return -TARGET_EFAULT; + } + old_act->_sa_handler = oact._sa_handler; + old_act->sa_flags = oact.sa_flags; + memcpy(&old_act->sa_mask, &oact.sa_mask, sizeof(target_sigset_t)); + unlock_user_struct(old_act, arg3, 1); + } + return ret; +} + + +/* sigprocmask(2) */ +static inline abi_long do_bsd_sigprocmask(abi_long arg1, abi_ulong arg2, + abi_ulong arg3) +{ + abi_long ret; + void *p; + sigset_t set, oldset, *set_ptr; + int how; + + ret = 0; + if (arg2) { + p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1); + if (p == NULL) { + return -TARGET_EFAULT; + } + target_to_host_sigset(&set, p); + unlock_user(p, arg2, 0); + set_ptr = &set; + switch (arg1) { + case TARGET_SIG_BLOCK: + how = SIG_BLOCK; + break; + case TARGET_SIG_UNBLOCK: + how = SIG_UNBLOCK; + break; + case TARGET_SIG_SETMASK: + how = SIG_SETMASK; + break; + default: + return -TARGET_EINVAL; + } + } else { + how = 0; + set_ptr = NULL; + } + ret = do_sigprocmask(how, set_ptr, &oldset); + if (!is_error(ret) && arg3) { + p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0); + if (p == NULL) { + return -TARGET_EFAULT; + } + host_to_target_sigset(p, &oldset); + unlock_user(p, arg3, sizeof(target_sigset_t)); + } + return ret; +} + +/* sigpending(2) */ +static inline abi_long do_bsd_sigpending(abi_long arg1) +{ + abi_long ret; + void *p; + sigset_t set; + + ret = get_errno(sigpending(&set)); + if (!is_error(ret)) { + p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0); + if (p == NULL) { + return -TARGET_EFAULT; + } + host_to_target_sigset(p, &set); + unlock_user(p, arg1, sizeof(target_sigset_t)); + } + return ret; +} + +/* sigsuspend(2) */ +static inline abi_long do_bsd_sigsuspend(CPUArchState *env, abi_long arg1, + abi_long arg2) +{ + CPUState *cpu = env_cpu(env); + TaskState *ts = cpu->opaque; + void *p; + abi_long ret; + + p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1); + if (p == NULL) { + return -TARGET_EFAULT; + } + target_to_host_sigset(&ts->sigsuspend_mask, p); + unlock_user(p, arg1, 0); + + ret = get_errno(sigsuspend(&ts->sigsuspend_mask)); + /* XXX Trivially true until safe_syscall */ + if (ret != -TARGET_ERESTART) { + ts->in_sigsuspend = true; + } + + return ret; +} + +/* sigreturn(2) */ +static inline abi_long do_bsd_sigreturn(CPUArchState *env, abi_long arg1) +{ + if (block_signals()) { + return -TARGET_ERESTART; + } + return do_sigreturn(env, arg1); +} + +/* sigvec(2) - not defined */ +/* sigblock(2) - not defined */ +/* sigsetmask(2) - not defined */ +/* sigstack(2) - not defined */ + +#endif /* BSD_SIGNAL_H */ diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c index d402c64726..7fefdb569f 100644 --- a/bsd-user/freebsd/os-syscall.c +++ b/bsd-user/freebsd/os-syscall.c @@ -38,6 +38,7 @@ #include "bsd-mem.h" #include "bsd-proc.h" #include "bsd-misc.h" +#include "bsd-signal.h" /* BSD dependent syscall shims */ #include "os-stat.h" diff --git a/bsd-user/signal-common.h b/bsd-user/signal-common.h index 4e634e04a3..9071757873 100644 --- a/bsd-user/signal-common.h +++ b/bsd-user/signal-common.h @@ -32,6 +32,7 @@ int block_signals(void); /* Returns non zero if signal pending */ long do_rt_sigreturn(CPUArchState *env); int do_sigaction(int sig, const struct target_sigaction *act, struct target_sigaction *oact); +int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset); abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp); long do_sigreturn(CPUArchState *env, abi_ulong addr); void force_sig_fault(int sig, int code, abi_ulong addr); @@ -42,6 +43,9 @@ void process_pending_signals(CPUArchState *env); void queue_signal(CPUArchState *env, int sig, int si_type, target_siginfo_t *info); void signal_init(void); +abi_long target_to_host_sigevent(struct sigevent *host_sevp, + abi_ulong target_addr); +int target_to_host_signal(int sig); void target_to_host_sigset(sigset_t *d, const target_sigset_t *s); /* diff --git a/bsd-user/signal.c b/bsd-user/signal.c index 3e5e41e1b1..da7cf1d60b 100644 --- a/bsd-user/signal.c +++ b/bsd-user/signal.c @@ -346,6 +346,33 @@ int block_signals(void) return qatomic_xchg(&ts->signal_pending, 1); } +abi_long target_to_host_sigevent(struct sigevent *host_sevp, + abi_ulong target_addr) +{ + struct target_sigevent *target_sevp; + + if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) { + return -TARGET_EFAULT; + } + + /* + * This union is awkward on 64 bit systems because it has a 32 bit + * integer and a pointer in it; we follow the conversion approach + * used for handling sigval types in signal.c so the guest should get + * the correct value back even if we did a 64 bit byteswap and it's + * using the 32 bit integer. + */ + host_sevp->sigev_value.sival_ptr = + (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr); + host_sevp->sigev_signo = + target_to_host_signal(tswap32(target_sevp->sigev_signo)); + host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify); + host_sevp->_sigev_un._threadid = tswap32(target_sevp->_sigev_un._threadid); + + unlock_user_struct(target_sevp, target_addr, 0); + return 0; +} + /* Returns 1 if given signal should dump core if not handled. */ static int core_dump_signal(int sig) { @@ -725,6 +752,46 @@ int do_sigaction(int sig, const struct target_sigaction *act, return ret; } +int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset) +{ + TaskState *ts = get_task_state(thread_cpu); + + if (oldset) { + *oldset = ts->signal_mask; + } + + if (set) { + int i; + + if (block_signals()) { + return -TARGET_ERESTART; + } + + switch (how) { + case SIG_BLOCK: + sigorset(&ts->signal_mask, &ts->signal_mask, set); + break; + case SIG_UNBLOCK: + for (i = 1; i <= NSIG; ++i) { + if (sigismember(set, i)) { + sigdelset(&ts->signal_mask, i); + } + } + break; + case SIG_SETMASK: + ts->signal_mask = *set; + break; + default: + g_assert_not_reached(); + } + + /* Silently ignore attempts to change blocking status of KILL or STOP */ + sigdelset(&ts->signal_mask, SIGKILL); + sigdelset(&ts->signal_mask, SIGSTOP); + } + return 0; +} + static inline abi_ulong get_sigframe(struct target_sigaction *ka, CPUArchState *env, size_t frame_size) { -- 2.55.0