From patchwork Tue Oct 3 19:20:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 728823 Delivered-To: patch@linaro.org Received: by 2002:a05:6504:1305:b0:23f:8cfb:5ad7 with SMTP id s5csp43214lto; Tue, 3 Oct 2023 12:22:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGLigldF2vl5x3xcU4BtzY8BxvOHwTsgbUwa/3kXcPY2g4H6ZycdrZ5Xap0worPEbMXQPTi X-Received: by 2002:a05:6102:8a:b0:44d:4a41:8945 with SMTP id t10-20020a056102008a00b0044d4a418945mr274611vsp.8.1696360944095; Tue, 03 Oct 2023 12:22:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696360944; cv=none; d=google.com; s=arc-20160816; b=VP/U1gC1e4SqBy1sxOFGdDou1Xj7xBd4wcuJn4ueWf6B2FeSCF45sCB2byBtYb6CtI 3u81wQBYG0qaOjxtGbSmNxtlzjmGhApUhlDsAJ0mW6WEoNuhXQW/yzV+o+fLp8GYKWz1 lghai1poQ/rVC0l4UiKdE5cQ0Jj++j5ToH2SWqicU/Hpeshjh4TC922EIctFuIxGohUg 36qF1D1YTlMIoKFSJzyubHlGsarnPYqGhtvS514MDTYyr9QO/yZrvBxDv9fC7TuLGHeL 3AP3a/WoXgvfBxzTGSZ9I9y8afDwqUTZunYdPuizUiLeBDHshIPom3Nj4dASjgXnYwvs 4sbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=bQFT1MeaWl1DywCggWsF6gUy0qHwX2/cAPNMuk55ZU0=; fh=snLBdPgTZLfuTuSyp61TvDpWpPfo2RkZAUqks3lr5MQ=; b=lfte4NQicfzfvuVvFJr2p7VXR4E9fe+ehDSZMVTdfSv6DfWTZZe1IFej+eyT9jwmvA ZNYGm1BivVaMQeCCI3qI+IWMMAFMRRNWRl1q471ZksVb1M4athr5fcZd8dEI82FndVau 4zBv22MzOINySX0K0axGh9HTzvyDNCXhZoCaZ0xzJ268KEvLluH/2dpxAYMihUOaIzhI a5hR9RqbAZWhh9scH0ficOAHFOhHQp8md6AnAP8GJmmZ51rCI7I2xWWDPN2j/UnkZhm/ 8s+fHuVhvOOwB7uM9LBgF96rCmQKbq1i9XfsTcGbIzUBrhGFr1Xf/umsHqghgT+zZX6I 7BLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="pb99Swa/"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id m13-20020a0cf18d000000b0065b2d24b5bcsi782518qvl.173.2023.10.03.12.22.23 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 03 Oct 2023 12:22:24 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="pb99Swa/"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qnkxO-00052p-DP; Tue, 03 Oct 2023 15:21:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qnkwy-0004vT-LU for qemu-devel@nongnu.org; Tue, 03 Oct 2023 15:20:58 -0400 Received: from mail-pj1-x1033.google.com ([2607:f8b0:4864:20::1033]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qnkwT-0004dS-Us for qemu-devel@nongnu.org; Tue, 03 Oct 2023 15:20:56 -0400 Received: by mail-pj1-x1033.google.com with SMTP id 98e67ed59e1d1-2773f2e599cso960203a91.1 for ; Tue, 03 Oct 2023 12:20:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696360814; x=1696965614; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bQFT1MeaWl1DywCggWsF6gUy0qHwX2/cAPNMuk55ZU0=; b=pb99Swa/hbwJL/Zia60/yXd+Wn6OIj50niL5WoiCeSJf1//LuW3x5Qt94RapwsnGPz 2TtWcWhgWLPy3FqVV9sbP2YVQdvvfg7115VXvjLKupPSkAeJgaH9vIYgvpwwgRLBCsoS m2mqii+ztsUQVvAUE3Z0GN3Kyl0tejjhIRZHHDPBBFtT0xrYROkvzeqbski8iDOoUnD+ /C1Z9EYgctcMK+QfCvh+tYwi9c2wL3aF74NgGHk/Yh0HbicTOT5VTnx5SvUUirJ34P6E y9hGNrVeCnTGLMcmS1bBmVXUPEEeiqS2Zv9vgDe8d2eb18BSb677z7i2i4NoQDajj/kE SxzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696360814; x=1696965614; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bQFT1MeaWl1DywCggWsF6gUy0qHwX2/cAPNMuk55ZU0=; b=ft9N1lK8VTgpF9sxzSfw7ct56tr9xgHMfqjlhBLis/Qv6e4H+pfZUkw7/F5DqF8ymz PRE6KMbecfeiG+X1Q5tyJjdJrOHqIb1m9+4CTJ1UEJrvxyZbYVtkFz7lT1zvJAhCeFA5 UOzwySlA+aRKYNHB2e7sx6s582r8nbZvvSNMeVD/ncHG5kb5QaAUhWuWFiq6uq0INk3r dtac750LEDYsUof96uJPB8T771uTyEpyMcBA56vqxdT4ybxzFbNhusroM1xrLlR8Bux5 XL2Zlk5YWs4SBbqIhHe9p0Nz3oSNq2vzEL/uywM3iNzGvsNXZ5bbgAPg7w8ya8Y2tmCk HTfw== X-Gm-Message-State: AOJu0Yykfy3ntCj0/uP5+n/RNQse6tY/CQW1qjyR/fzdV+dWcx2FLudq 4Zx4WeBh7j0jvs8yZsBfxphQnf6Y7FuHd44e1iU= X-Received: by 2002:a17:90b:224d:b0:274:566a:3477 with SMTP id hk13-20020a17090b224d00b00274566a3477mr240985pjb.39.1696360814503; Tue, 03 Oct 2023 12:20:14 -0700 (PDT) Received: from stoup.. ([71.212.149.95]) by smtp.gmail.com with ESMTPSA id w5-20020a170902d3c500b001c5fd2a28d3sm1978497plb.28.2023.10.03.12.20.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 12:20:13 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, deller@gmx.de, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PATCH v5 1/9] linux-user: Split out die_with_signal Date: Tue, 3 Oct 2023 12:20:04 -0700 Message-Id: <20231003192012.1674888-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231003192012.1674888-1-richard.henderson@linaro.org> References: <20231003192012.1674888-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1033; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1033.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Because we trap so many signals for use by the guest, we have to take extra steps to exit properly. Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Richard Henderson --- linux-user/signal.c | 52 ++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/linux-user/signal.c b/linux-user/signal.c index a67ab47d30..b7a2c47837 100644 --- a/linux-user/signal.c +++ b/linux-user/signal.c @@ -689,13 +689,39 @@ void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr, } /* abort execution with signal */ +static G_NORETURN +void die_with_signal(int host_sig) +{ + struct sigaction act = { + .sa_handler = SIG_DFL, + }; + + /* + * The proper exit code for dying from an uncaught signal is -. + * The kernel doesn't allow exit() or _exit() to pass a negative value. + * To get the proper exit code we need to actually die from an uncaught + * signal. Here the default signal handler is installed, we send + * the signal and we wait for it to arrive. + */ + sigfillset(&act.sa_mask); + sigaction(host_sig, &act, NULL); + + kill(getpid(), host_sig); + + /* Make sure the signal isn't masked (reusing the mask inside of act). */ + sigdelset(&act.sa_mask, host_sig); + sigsuspend(&act.sa_mask); + + /* unreachable */ + abort(); +} + static G_NORETURN void dump_core_and_abort(CPUArchState *env, int target_sig) { CPUState *cpu = env_cpu(env); TaskState *ts = (TaskState *)cpu->opaque; int host_sig, core_dumped = 0; - struct sigaction act; host_sig = target_to_host_signal(target_sig); trace_user_dump_core_and_abort(env, target_sig, host_sig); @@ -719,29 +745,7 @@ void dump_core_and_abort(CPUArchState *env, int target_sig) } preexit_cleanup(env, 128 + target_sig); - - /* The proper exit code for dying from an uncaught signal is - * -. The kernel doesn't allow exit() or _exit() to pass - * a negative value. To get the proper exit code we need to - * actually die from an uncaught signal. Here the default signal - * handler is installed, we send ourself a signal and we wait for - * it to arrive. */ - sigfillset(&act.sa_mask); - act.sa_handler = SIG_DFL; - act.sa_flags = 0; - sigaction(host_sig, &act, NULL); - - /* For some reason raise(host_sig) doesn't send the signal when - * statically linked on x86-64. */ - kill(getpid(), host_sig); - - /* Make sure the signal isn't masked (just reuse the mask inside - of act) */ - sigdelset(&act.sa_mask, host_sig); - sigsuspend(&act.sa_mask); - - /* unreachable */ - abort(); + die_with_signal(host_sig); } /* queue a signal so that it will be send to the virtual CPU as soon