From patchwork Sat Sep 18 18:45:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 514231 Delivered-To: patch@linaro.org Received: by 2002:a02:c816:0:0:0:0:0 with SMTP id p22csp670438jao; Sat, 18 Sep 2021 11:57:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzR3Vn52J8OyuhC3o+lbPvBqsq9HPH8yOstJ9Xqe3qEuDD//YTbqIZKPWDr2MeP6VcPYnBh X-Received: by 2002:a25:2485:: with SMTP id k127mr13175428ybk.71.1631991430535; Sat, 18 Sep 2021 11:57:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631991430; cv=none; d=google.com; s=arc-20160816; b=1DNYPHY+qoLz+MCcYYw9u1nC+8slMG2VxOtiX4P+L+Wc0kBFgfTZ4ZnXf6R9+xIVdd ThHM2g/vE+Biy1RnkeTdCLMr+RpHP3xq1y1rvERGbtS82HKxI9QaSXW6G86nQ/yzAZJQ U0f46X6FEJLNiVQh+YuVjeHO5+Et3AzWZI2T1MXX4R7HJhOPzjrv6LHQFp5gNFz009yA Nrg5bNIWTBjYt/vmbbEdFmZwJ0eEhgOcrzYLT98OKL6U/3YkW3luby+cKZw9gs04cHsl zw1wWCFRYARz+sVseA+Av0lb8ST+vQDz0N04FmNRnSPdBgkzbQlKbHDelm2jP0cDxuKV ui0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc: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:to:from :dkim-signature; bh=bUABOOKdTsHd1I4YhOWD+WaOacOY2c/QLq79HLsoZBA=; b=Iv8mQ3I7pfpnaFUAd5oW+7ZGl6gBZRQoTf39KKPLsnMDXZ4T0dX9bc1P5l/puQ++7G z9fY70mmwDTow2/iKtW1U/YpwE/sYe025tEl1OHt2GPRBg75PFCX3Xe+tV3l1GtGsrww idXnft+HhVi/SiMatKZTkcD/GTkyfcV6psZ9xqzOOMBMGTEjVwjaIa97bsQfSdgrT4GZ an3QNFEp9IGpyT1hjtTb5ZVl9GhlzFSri7xE5PVN+cJLwWJiw3kfima6wXuo/szzzyHa +iJnRGoA/9zv1x/Cl3IBPgNW4mIju9RERnKvzGwF3HNu1iD7bOhG8jvx4Fv9Vjkya6aI HLvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=xBJjH7Xq; 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 g132si4891774ybb.122.2021.09.18.11.57.10 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 18 Sep 2021 11:57:10 -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=xBJjH7Xq; 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]:33522 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mRfWP-0005Ru-T0 for patch@linaro.org; Sat, 18 Sep 2021 14:57:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54504) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mRfLf-0005Cz-PF for qemu-devel@nongnu.org; Sat, 18 Sep 2021 14:46:04 -0400 Received: from mail-pg1-x531.google.com ([2607:f8b0:4864:20::531]:41489) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mRfLP-0006r8-LB for qemu-devel@nongnu.org; Sat, 18 Sep 2021 14:45:59 -0400 Received: by mail-pg1-x531.google.com with SMTP id k24so13081116pgh.8 for ; Sat, 18 Sep 2021 11:45:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=bUABOOKdTsHd1I4YhOWD+WaOacOY2c/QLq79HLsoZBA=; b=xBJjH7Xq4e8UymD4tS49FIHDlu3ITrRgnX0MoCCaDJX9km+bpGD7H+nkVYJsyekaX3 5xXIVAnrVyP3vMQqDVSL8o/Rm+U81kpfLhVUUHT7SvG2p6AiV9mDjX9U4CWZu+ZgT/y5 1iCNIvK+jhn5nBadtwdUg0/rLrNiBTFlSbwOCQqx5j5JGxEMIZkfyQWqomZk62H1EwEl xGj7FO39MZBXoI3eRxnD3J8D3H6ky3HqFOAWE1DbB+2pmMCc+nTrS3+NPzi2UpH3CK27 loNxkt9tKZC4mrHrtwJ63X2QQseK+rcqqzSXyc4BblH9EBWkor1JTE+kdxJVMv2966DJ bpDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bUABOOKdTsHd1I4YhOWD+WaOacOY2c/QLq79HLsoZBA=; b=aoWYhbz5Kb2BOYyavEAFLHekDeLX4Kzb6vq7Ndxeme5o1lRNctOIoGfOA+6xQjcitW +i4LgHF1i0MxWeNGbqFmsFnPQ8h0EodNdBBhc++334M1UtC/JCU1zi30Y+oTb28Dlxuw qW0F1/5UN/RBppd71Gme1x6lI10ksZCbVwPz7T03BvIq683NUXOSiBWeux1pgKiAa1L2 q+OlOjtlnqgD9LftjbzbtXHIgHzF8Wn1wY21piSQ6DCotoTVS6gRZ0R30ERI5s+QfokP oUBDZpcZMtYB3FYXNRcpCY2mnCL58eAdSUvwE+rjYeun8wPX4BoaUCFd3t1N9rWSngsy JzIg== X-Gm-Message-State: AOAM533gLsd4PV2KGPDlFn/qTMpPtK34Vql9a8N1QZxCHrxG4HkZZkew 42vdaPLwEerjzKUwWq5h2FH105CchJJo8w== X-Received: by 2002:a62:2cd8:0:b0:43d:e6c0:1725 with SMTP id s207-20020a622cd8000000b0043de6c01725mr17193710pfs.55.1631990745424; Sat, 18 Sep 2021 11:45:45 -0700 (PDT) Received: from localhost.localdomain ([71.212.134.125]) by smtp.gmail.com with ESMTPSA id y26sm9555858pfe.69.2021.09.18.11.45.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Sep 2021 11:45:45 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v2 20/41] linux-user: Add raise_sigsegv Date: Sat, 18 Sep 2021 11:45:06 -0700 Message-Id: <20210918184527.408540-21-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210918184527.408540-1-richard.henderson@linaro.org> References: <20210918184527.408540-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::531; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x531.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_PASS=-0.001, T_SPF_HELO_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: laurent@vivier.eu Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" This is a new interface to be provided by the os emulator for raising SIGSEGV on fault. Use the new record_sigsegv target hook. Signed-off-by: Richard Henderson --- include/exec/exec-all.h | 15 +++++++++++++++ accel/tcg/user-exec.c | 33 ++++++++++++++++++--------------- linux-user/signal.c | 30 ++++++++++++++++++++++-------- 3 files changed, 55 insertions(+), 23 deletions(-) -- 2.25.1 Reviewed-by: Philippe Mathieu-Daudé diff --git a/include/exec/exec-all.h b/include/exec/exec-all.h index 5dd663c153..2091c1bf1a 100644 --- a/include/exec/exec-all.h +++ b/include/exec/exec-all.h @@ -685,6 +685,21 @@ MMUAccessType adjust_signal_pc(uintptr_t *pc, bool is_write); bool handle_sigsegv_accerr_write(CPUState *cpu, sigset_t *old_set, uintptr_t host_pc, abi_ptr guest_addr); +/** + * raise_sigsegv: + * @cpu: the cpu context + * @addr: the guest address of the fault + * @access_type: access was read/write/execute + * @maperr: true for invalid page, false for permission fault + * @ra: host pc for unwinding + * + * Use the TCGCPUOps hook to record cpu state, do guest operating system + * specific things to raise SIGSEGV, and jump to the main cpu loop. + */ +void QEMU_NORETURN raise_sigsegv(CPUState *cpu, target_ulong addr, + MMUAccessType access_type, + bool maperr, uintptr_t ra); + #else static inline void mmap_lock(void) {} static inline void mmap_unlock(void) {} diff --git a/accel/tcg/user-exec.c b/accel/tcg/user-exec.c index 01e7e69e7f..ab9cc6686d 100644 --- a/accel/tcg/user-exec.c +++ b/accel/tcg/user-exec.c @@ -143,35 +143,38 @@ static int probe_access_internal(CPUArchState *env, target_ulong addr, int fault_size, MMUAccessType access_type, bool nonfault, uintptr_t ra) { - int flags; + bool maperr = true; + int acc_flag; switch (access_type) { case MMU_DATA_STORE: - flags = PAGE_WRITE; + acc_flag = PAGE_WRITE_ORG; break; case MMU_DATA_LOAD: - flags = PAGE_READ; + acc_flag = PAGE_READ; break; case MMU_INST_FETCH: - flags = PAGE_EXEC; + acc_flag = PAGE_EXEC; break; default: g_assert_not_reached(); } - if (!guest_addr_valid_untagged(addr) || - page_check_range(addr, 1, flags) < 0) { - if (nonfault) { - return TLB_INVALID_MASK; - } else { - CPUState *cpu = env_cpu(env); - CPUClass *cc = CPU_GET_CLASS(cpu); - cc->tcg_ops->tlb_fill(cpu, addr, fault_size, access_type, - MMU_USER_IDX, false, ra); - g_assert_not_reached(); + if (guest_addr_valid_untagged(addr)) { + int page_flags = page_get_flags(addr); + if (page_flags & acc_flag) { + return 0; /* success */ + } + if (page_flags & PAGE_VALID) { + maperr = false; } } - return 0; + + if (nonfault) { + return TLB_INVALID_MASK; + } + + raise_sigsegv(env_cpu(env), addr, access_type, maperr, ra); } int probe_access_flags(CPUArchState *env, target_ulong addr, diff --git a/linux-user/signal.c b/linux-user/signal.c index e6531fdfa0..ae31b46be0 100644 --- a/linux-user/signal.c +++ b/linux-user/signal.c @@ -681,9 +681,27 @@ void force_sigsegv(int oldsig) } force_sig(TARGET_SIGSEGV); } - #endif +void raise_sigsegv(CPUState *cpu, target_ulong addr, + MMUAccessType access_type, bool maperr, uintptr_t ra) +{ + const struct TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops; + + if (tcg_ops->record_sigsegv) { + tcg_ops->record_sigsegv(cpu, addr, access_type, maperr, ra); + } else if (tcg_ops->tlb_fill) { + tcg_ops->tlb_fill(cpu, addr, 0, access_type, MMU_USER_IDX, false, ra); + g_assert_not_reached(); + } + + force_sig_fault(TARGET_SIGSEGV, + maperr ? TARGET_SEGV_MAPERR : TARGET_SEGV_ACCERR, + addr); + cpu->exception_index = EXCP_INTERRUPT; + cpu_loop_exit_restore(cpu, ra); +} + /* abort execution with signal */ static void QEMU_NORETURN dump_core_and_abort(int target_sig) { @@ -799,7 +817,7 @@ static void host_signal_handler(int host_sig, siginfo_t *info, void *puc) access_type = adjust_signal_pc(&pc, is_write); if (host_sig == SIGSEGV) { - const struct TCGCPUOps *tcg_ops; + bool maperr = true; if (info->si_code == SEGV_ACCERR && h2g_valid(host_addr)) { /* If this was a write to a TB protected page, restart. */ @@ -814,18 +832,14 @@ static void host_signal_handler(int host_sig, siginfo_t *info, void *puc) * which means that we may get ACCERR when we want MAPERR. */ if (page_get_flags(guest_addr) & PAGE_VALID) { - /* maperr = false; */ + maperr = false; } else { info->si_code = SEGV_MAPERR; } } sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL); - - tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops; - tcg_ops->tlb_fill(cpu, guest_addr, 0, access_type, - MMU_USER_IDX, false, pc); - g_assert_not_reached(); + raise_sigsegv(cpu, guest_addr, access_type, maperr, pc); } else { sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL); }