From patchwork Tue Aug 22 20:34:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 715788 Delivered-To: patch@linaro.org Received: by 2002:adf:f747:0:b0:317:ecd7:513f with SMTP id z7csp468181wrp; Tue, 22 Aug 2023 13:35:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGGSZAfoUQa8I1LodzdkWqb3kaxWrDI0lxCWg3shTgaVzZPVElT7HAVJQe9JOJJhLUyXBxw X-Received: by 2002:a05:620a:c53:b0:767:3fb8:12dd with SMTP id u19-20020a05620a0c5300b007673fb812ddmr12940947qki.38.1692736511647; Tue, 22 Aug 2023 13:35:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692736511; cv=none; d=google.com; s=arc-20160816; b=kACXtkhIXQHS6muSWRBsIRMEupTzB7K3219EjqHIX5xQxZ2CTJqiiNti0DmfgYajcd PvK6WTfa79NCX+8PssHRz71wj+c1zC1CWIKIKENRqKgHlbfnnyD5CshjaaYAigBgBQAW v0ixiy7/I9WyMPXXSabZmhHvDMOO7/Xsb9GkQF1gf8W6862nWyRmKEgB9FH4ZaBPOt7w SSmEOZ67OCxI4wqfITTzrRMJ1wvlJ1TgNQTAuuqvWQ/u7xxSfOKlqWzXBvqenTuzoZgs w6WZc5xIu4oK3+at1BKrsVnJIC+OYDPuC5OjOXvhCYjxxE3E4Yhffz3UOG43PIJ6cX/U HDDw== 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=dcsO2dVLbPUl+GizLMpXodQLIxyTwrwgsESLGfn+SH8=; fh=uta8CuNdWwEUIsmWLQQIuZWY0GOyjYTd4DbN32nR8cE=; b=GqUk6HFO6oHjEnFuaPD4+75LP3JIu6yRvum7EtXCkF8DAdfPSHO20cQTLRbpGCi42z 7huossT67GNwpxBWKxlqpDjUe9jrPZTxtnn98u77Ezf1xUA0APmSs0R59TnLidjIQwDS 0uV3CT1OkqcTWuqazR1SJuabFRTrWOo9JZlmcml1zGUvOYuui8ZS8U7sfGniZqjdP8ub McwBHG/mfthgaFgRn048WUmyv5ZY0OTrJ/gFpQcara5xqJs3HFvXY2YHqpnaC2C8nquw r/oqi9I2G2CtxRgFEQlj3kTgKvu+0/0EaVnCPvs76ctFkJwdC4U0b+HRM68rKktpV7Sv K9Bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=VLZUFW0p; 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 ay10-20020a05620a178a00b0076db12d14c2si1965815qkb.719.2023.08.22.13.35.11 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 22 Aug 2023 13:35:11 -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=VLZUFW0p; 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 1qYY51-0002dD-E9; Tue, 22 Aug 2023 16:34:23 -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 1qYY4z-0002bF-C2 for qemu-devel@nongnu.org; Tue, 22 Aug 2023 16:34:21 -0400 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qYY4x-0005EA-0x for qemu-devel@nongnu.org; Tue, 22 Aug 2023 16:34:21 -0400 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1bee82fad0fso31914915ad.2 for ; Tue, 22 Aug 2023 13:34:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1692736457; x=1693341257; 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=dcsO2dVLbPUl+GizLMpXodQLIxyTwrwgsESLGfn+SH8=; b=VLZUFW0puMsbd5bEY4Jg2KILmem0iqqISnxTFNRXVTPGqFgmCli0q03YRfVJKAsMHH ktpNrL6fBoIOuQrj62rOb0TgrlT3N7+NJU+hZSs7Qlf8IlJLyfhjbgDRa2EZxhlJertR 5K0J/JS6dqz+aojWgPW6kLW4ByNQlI7k1HR+QRE/J99vDxbLyc5uwHnFhtr8Ca6M2n64 /HSsRKd5PKS8w4xfTux0az6O8TgIeOBBS3BdFhKtSNeHnvWv6it1E3fCvqVqDd1AW3M6 BP2U0DikWSlwb3GoMKJcIT2ZJLMMFKi+5YLKa49bzO2vak9+TAlYmCRymO6xXsdP4hxt 7cWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692736457; x=1693341257; 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=dcsO2dVLbPUl+GizLMpXodQLIxyTwrwgsESLGfn+SH8=; b=UpE97oomXayjV2KhBuX//lmika8xKqNvhCMHjw6Vq/2MmJPWDhJmFhAqcmFBIVma3d LMYMMeFQ5uOjx9IKPkFgW8PNQeD48hzYc21kOuAZqlLOcpylDdz9o4guWBVOQ0J5mq00 mpyc6HvVgSRFskzdSEkv9jAnZVk2hORM0pGF70Hiop/ghcTqpUbDj58NbcM2ooKZQx/+ 8drVb8uFDO5I5QhkJwm3rbpiXXatNhbqPb/zMAKJa8ZTFKVxx4BsWd6kJRztV3o67t+a fbuWCNF5dSRjKUPgbWweUtf1mdb88hLvbKul/XO6/9gJwbvd3LgRGp0m/b6WNTD5DzCF Ducg== X-Gm-Message-State: AOJu0Yw01fffsG93pGAapYJOYc3qUTpsj8xksobwKjEFFDFZCexqnC3i sKpX0HKPNngz7PPKBYKEF+eXGBULBtDKt+ibw4Q= X-Received: by 2002:a17:902:ab54:b0:1bc:2bd:8523 with SMTP id ij20-20020a170902ab5400b001bc02bd8523mr7431103plb.24.1692736457507; Tue, 22 Aug 2023 13:34:17 -0700 (PDT) Received: from stoup.. ([2602:47:d483:7301:4e3c:f4a4:b92a:b5ab]) by smtp.gmail.com with ESMTPSA id i25-20020a633c59000000b00564aee22f33sm8311823pgn.14.2023.08.22.13.34.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 13:34:17 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: deller@gmx.de Subject: [PATCH v3 1/3] linux-user: Split out die_with_signal Date: Tue, 22 Aug 2023 13:34:13 -0700 Message-Id: <20230822203415.1184491-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230822203415.1184491-1-richard.henderson@linaro.org> References: <20230822203415.1184491-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62b; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62b.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, RCVD_IN_DNSWL_NONE=-0.0001, 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. Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé --- 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 748a98f3e5..e1cd111a1b 100644 --- a/linux-user/signal.c +++ b/linux-user/signal.c @@ -694,6 +694,33 @@ void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr, } /* abort execution with signal */ +static G_NORETURN +void die_with_signal(int 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(sig, &act, NULL); + + kill(getpid(), sig); + + /* Make sure the signal isn't masked (reusing the mask inside of act). */ + sigdelset(&act.sa_mask, sig); + sigsuspend(&act.sa_mask); + + /* unreachable */ + abort(); +} + static G_NORETURN void dump_core_and_abort(CPUArchState *cpu_env, int target_sig) { @@ -701,7 +728,6 @@ void dump_core_and_abort(CPUArchState *cpu_env, int target_sig) CPUArchState *env = cpu->env_ptr; 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); @@ -725,29 +751,7 @@ void dump_core_and_abort(CPUArchState *cpu_env, int target_sig) } preexit_cleanup(cpu_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