From patchwork Tue Oct 3 19:20:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 728827 Delivered-To: patch@linaro.org Received: by 2002:a05:6504:1305:b0:23f:8cfb:5ad7 with SMTP id s5csp43464lto; Tue, 3 Oct 2023 12:22:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFv9sFNh2j/h8UUVbq+ObNppMNM5I6SHaN5lrvyEYbVsgbX38j2xV91U9jHbGePKXNV6nFy X-Received: by 2002:ac8:5e4c:0:b0:413:5e4d:bf40 with SMTP id i12-20020ac85e4c000000b004135e4dbf40mr295961qtx.68.1696360975867; Tue, 03 Oct 2023 12:22:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696360975; cv=none; d=google.com; s=arc-20160816; b=OYQcvOeiSE4FwcyX5TQAAkLsVNGzmZ574V7O1ukAl2RGmz+0dF+zS3GM7WQhe25c5F 9Z5UnFftl/eE9LyFYSfy7i9Gou7lJoFsUxQpHWn4lYbNMWbu32tLrDs89ZJy2qUqj7z9 hZa8lWA3YapxQKhNX0Xn0Cd0ONsJgQ0hdnma70xkXFoe4gPA4EiO9xQ+un/mwfJKRgiP O+nh/ssK1/GcvF3eeqEwdDqSQaOEj0b4IpAWQUgvxlVpDQUnc1i1O0Gszimju3rd3JHh gQL8fW+yRgKT/5pj07V8E7SO+dXW0uvlgv2Y+ulENUTFsWdNKZUmDETvcvvDzo6sUVqQ a+wg== 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=x2cyUxn+xuVK4D2wBUl4hDzhwAR15+4cIMS3lBIQ8QQ=; fh=YRZ+qgYKjKfO+J7iYLqJ+AhmJljeW1N2qgZkMPPeD1I=; b=gg3Y2KOfe8k4jy1wA5+KR6Nn0aGABMTyZwSrR7WUHDQVSUaeT5ybzF34sz2kY+QLyx MwqfM/Imjg+eCMju+N5hr+cK2kvwXty4M2ACgdX2hYYYxU4H4qptZTbjo/9GKjgc1HSi 3RhM/cL25dG9xpUdXcEiySl9maveWKaJGtCS9H5hELmNKf7Q6FvDB49madgn4whXnDNN 6xtAr8043emmSI0bZ0Yha0W+/l61Hl89zG/rEPqPBPva/n1EQqtrpb39/UVvBYXYxRVi j7DuavsK0xr5m4RjkK9w6Dy+x9n4T74H5rkFnk4WyKnHnAUmMi3bK9M0fYGF97fK+wGR ruLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=L1JHtDSU; 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 f2-20020a05622a104200b0041983f51b2esi826318qte.67.2023.10.03.12.22.55 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 03 Oct 2023 12:22:55 -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=L1JHtDSU; 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 1qnkxL-00051E-7d; Tue, 03 Oct 2023 15:21:19 -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 1qnkwo-0004sI-2x for qemu-devel@nongnu.org; Tue, 03 Oct 2023 15:20:47 -0400 Received: from mail-oi1-x235.google.com ([2607:f8b0:4864:20::235]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qnkwT-0004e7-Uz for qemu-devel@nongnu.org; Tue, 03 Oct 2023 15:20:44 -0400 Received: by mail-oi1-x235.google.com with SMTP id 5614622812f47-3af65455e7cso864218b6e.1 for ; Tue, 03 Oct 2023 12:20:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696360820; x=1696965620; 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=x2cyUxn+xuVK4D2wBUl4hDzhwAR15+4cIMS3lBIQ8QQ=; b=L1JHtDSUHev1yQBec6ka2VC3UPilNJisSYn9X506O0sTBoHtghmDwWnTLxuqF9HdqB tVQyVPpcaMCIvAv6eu4wSvi2SWKxuf+D+w1UGABremlEJpWvo4ZsArEEwoIVCExhAES/ XygsnroxxVJrBU0newSGmDT2yhirVBLpJYakk4ZQM3UfVgpxLEpyHnql6NCmeSnyrvTI vH3mIj23JkcjDpcpgyM9n/9RhBPdNbnA9YZr2w5/zY9DT9qf6opb0CoK2rvkU0SxNGIG WYJKmBsg9jip3Y3F6xLelLaDiZUZIXEeFSVH98DxRE+KrQEkBn6SHpC2vGsK/mmfzICP Ialg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696360820; x=1696965620; 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=x2cyUxn+xuVK4D2wBUl4hDzhwAR15+4cIMS3lBIQ8QQ=; b=qg9feWb7VJAOiidUSdYKQjFbFg7okDInPOzITUNCSpdeZ4yfFnxOxPRWQ7LqsgLkuf gsn9tPOmKLx3zd1cCG+hct7KXb1GvqD8NXQ7rs0+yXV+OWnDrOG/cOaLHE/gQrKRj/3k q19rhlwzLz6lg/J1dUDhLlWg7Wf/i3PPO1j040qJcAT7H68ZstJ9/izJYM1rSqf0i6fG K/3bCCCvIJABFAfPfMs3+VJsNhrvTdj834ytqHQMKlst21bUjIgEGdiAv4s62+uW/Aco ayZFzHB/90RNmKvG8UHXyiMgrxCwQvkSHddj5As4Ch8LUUWZnF6v5eGhhgt3iwBH8DuW nL8g== X-Gm-Message-State: AOJu0YyyoQKwRwFf+uItHND8gNvEhkd3EVsDMEh8AYO9ZdvDQYGhZ4dn /96Q9E62OcIRPUwomZvY6M0nE6M8iTsVteQdScE= X-Received: by 2002:a05:6808:13c5:b0:3a8:43d5:878b with SMTP id d5-20020a05680813c500b003a843d5878bmr604634oiw.2.1696360820120; Tue, 03 Oct 2023 12:20:20 -0700 (PDT) Received: from stoup.. ([71.212.149.95]) by smtp.gmail.com with ESMTPSA id w5-20020a170902d3c500b001c5fd2a28d3sm1978497plb.28.2023.10.03.12.20.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 12:20:19 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, deller@gmx.de Subject: [PATCH v5 7/9] linux-user: Split out host_sig{segv,bus}_handler Date: Tue, 3 Oct 2023 12:20:10 -0700 Message-Id: <20231003192012.1674888-8-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::235; envelope-from=richard.henderson@linaro.org; helo=mail-oi1-x235.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 Make host_signal_handler slightly easier to read. Signed-off-by: Richard Henderson --- linux-user/signal.c | 145 ++++++++++++++++++++++++++------------------ 1 file changed, 85 insertions(+), 60 deletions(-) diff --git a/linux-user/signal.c b/linux-user/signal.c index 09840b0eb0..706b8ac7a7 100644 --- a/linux-user/signal.c +++ b/linux-user/signal.c @@ -811,6 +811,80 @@ void die_from_signal(siginfo_t *info) die_with_signal(info->si_signo); } +static void host_sigsegv_handler(CPUState *cpu, siginfo_t *info, + host_sigcontext *uc) +{ + uintptr_t host_addr = (uintptr_t)info->si_addr; + /* + * Convert forcefully to guest address space: addresses outside + * reserved_va are still valid to report via SEGV_MAPERR. + */ + bool is_valid = h2g_valid(host_addr); + abi_ptr guest_addr = h2g_nocheck(host_addr); + uintptr_t pc = host_signal_pc(uc); + bool is_write = host_signal_write(info, uc); + MMUAccessType access_type = adjust_signal_pc(&pc, is_write); + bool maperr; + + /* If this was a write to a TB protected page, restart. */ + if (is_write + && is_valid + && info->si_code == SEGV_ACCERR + && handle_sigsegv_accerr_write(cpu, host_signal_mask(uc), + pc, guest_addr)) { + return; + } + + /* + * If the access was not on behalf of the guest, within the executable + * mapping of the generated code buffer, then it is a host bug. + */ + if (access_type != MMU_INST_FETCH + && !in_code_gen_buffer((void *)(pc - tcg_splitwx_diff))) { + die_from_signal(info); + } + + maperr = true; + if (is_valid && info->si_code == SEGV_ACCERR) { + /* + * With reserved_va, the whole address space is PROT_NONE, + * which means that we may get ACCERR when we want MAPERR. + */ + if (page_get_flags(guest_addr) & PAGE_VALID) { + maperr = false; + } else { + info->si_code = SEGV_MAPERR; + } + } + + sigprocmask(SIG_SETMASK, host_signal_mask(uc), NULL); + cpu_loop_exit_sigsegv(cpu, guest_addr, access_type, maperr, pc); +} + +static void host_sigbus_handler(CPUState *cpu, siginfo_t *info, + host_sigcontext *uc) +{ + uintptr_t pc = host_signal_pc(uc); + bool is_write = host_signal_write(info, uc); + MMUAccessType access_type = adjust_signal_pc(&pc, is_write); + + /* + * If the access was not on behalf of the guest, within the executable + * mapping of the generated code buffer, then it is a host bug. + */ + if (!in_code_gen_buffer((void *)(pc - tcg_splitwx_diff))) { + die_from_signal(info); + } + + if (info->si_code == BUS_ADRALN) { + uintptr_t host_addr = (uintptr_t)info->si_addr; + abi_ptr guest_addr = h2g_nocheck(host_addr); + + sigprocmask(SIG_SETMASK, host_signal_mask(uc), NULL); + cpu_loop_exit_sigbus(cpu, guest_addr, access_type, pc); + } +} + static void host_signal_handler(int host_sig, siginfo_t *info, void *puc) { CPUState *cpu = thread_cpu; @@ -822,73 +896,23 @@ static void host_signal_handler(int host_sig, siginfo_t *info, void *puc) int guest_sig; uintptr_t pc = 0; bool sync_sig = false; - void *sigmask = host_signal_mask(uc); + void *sigmask; /* * Non-spoofed SIGSEGV and SIGBUS are synchronous, and need special * handling wrt signal blocking and unwinding. */ - if ((host_sig == SIGSEGV || host_sig == SIGBUS) && info->si_code > 0) { - MMUAccessType access_type; - uintptr_t host_addr; - abi_ptr guest_addr; - bool is_write; - - host_addr = (uintptr_t)info->si_addr; - - /* - * Convert forcefully to guest address space: addresses outside - * reserved_va are still valid to report via SEGV_MAPERR. - */ - guest_addr = h2g_nocheck(host_addr); - - pc = host_signal_pc(uc); - is_write = host_signal_write(info, uc); - access_type = adjust_signal_pc(&pc, is_write); - - /* If this was a write to a TB protected page, restart. */ - if (is_write - && host_sig == SIGSEGV - && info->si_code == SEGV_ACCERR - && h2g_valid(host_addr) - && handle_sigsegv_accerr_write(cpu, sigmask, pc, guest_addr)) { + if (info->si_code > 0) { + switch (host_sig) { + case SIGSEGV: + /* Only returns on handle_sigsegv_accerr_write success. */ + host_sigsegv_handler(cpu, info, uc); return; + case SIGBUS: + host_sigbus_handler(cpu, info, uc); + sync_sig = true; + break; } - - /* - * If the access was not on behalf of the guest, within the executable - * mapping of the generated code buffer, then it is a host bug. - */ - if (access_type != MMU_INST_FETCH - && !in_code_gen_buffer((void *)(pc - tcg_splitwx_diff))) { - die_from_signal(info); - } - - if (host_sig == SIGSEGV) { - bool maperr = true; - - if (info->si_code == SEGV_ACCERR && h2g_valid(host_addr)) { - /* - * With reserved_va, the whole address space is PROT_NONE, - * which means that we may get ACCERR when we want MAPERR. - */ - if (page_get_flags(guest_addr) & PAGE_VALID) { - maperr = false; - } else { - info->si_code = SEGV_MAPERR; - } - } - - sigprocmask(SIG_SETMASK, sigmask, NULL); - cpu_loop_exit_sigsegv(cpu, guest_addr, access_type, maperr, pc); - } else { - sigprocmask(SIG_SETMASK, sigmask, NULL); - if (info->si_code == BUS_ADRALN) { - cpu_loop_exit_sigbus(cpu, guest_addr, access_type, pc); - } - } - - sync_sig = true; } /* get target signal number */ @@ -929,6 +953,7 @@ static void host_signal_handler(int host_sig, siginfo_t *info, void *puc) * would write 0xff bytes off the end of the structure and trash * data on the struct. */ + sigmask = host_signal_mask(uc); memset(sigmask, 0xff, SIGSET_T_SIZE); sigdelset(sigmask, SIGSEGV); sigdelset(sigmask, SIGBUS);