From patchwork Mon Dec 19 10:24:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sumit Garg X-Patchwork-Id: 13076457 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 638B5C10F1B for ; Mon, 19 Dec 2022 10:27:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=/g/AtOq8WMeiKqcqMBcS0OhcRxLtdZxwqmvVFuufsMw=; b=aywAPNVJqZaEa3 HFwS+Hzw/PNf4z53SUEHpxDlcZwpZy6KehhTUNielpxOuiwBnObsm1L0Yz3WbOu57eskzu4zgAK1y SQyKbX96HmkKM4FRBDifPZYb+mOjy351h6fa4FGSGTp6+uKAXG/6vBv3Z3EWcfBjR5ssueR/KVdfs NaVqLfDNf83tk7MMIhd/aKorsmrm9srplj6e1vbJJa8AjCevS+urBagPL20kSzuowjqiCqMbPA2jL 2kkVNv3gmDzTfG5AIW2kUmSrV3qnC/DIod3JYYxCyL8Fso44jSWUKTOeNqg4DgrnZn17SOqLLMtEy 87AFDfAci7vbgiQnX+Jw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1p7DLH-00BqaF-ND; Mon, 19 Dec 2022 10:25:55 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1p7DKY-00Bpyt-PU for linux-arm-kernel@lists.infradead.org; Mon, 19 Dec 2022 10:25:13 +0000 Received: by mail-pl1-x635.google.com with SMTP id t2so8590964ply.2 for ; Mon, 19 Dec 2022 02:25:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=fi3gpq9hnnVh1G3NTQ1MirWR5bKsTeEH2CH3CJi0VpU=; b=BMft+u0nyKZnjgqVhQRPY1+VVQmfb8C1AsWz81oiPKNLTYQUhGFzZH8cgAH2veijTO a0ShS/Ivg3KNm0JS9YZHSbtwadKt0w5H2pUVpozu4jWrZXfGGc3T237J01JY21b5CZxZ 2uJElRt65V5xVRZM+0JhyTDdTqkBWxLDzZwEowpXVDrW5tBtQNcAz1FLH2L7Cocl7c+E GirmdAuk4Bflpt3iaz2hFnr4V6i2vyyMtSWK9w0FKXLm3pJTGUmiShUBwctxeYMCJZrL /ertytA7IbdjYUuyDMS4M3nuFATScKnVxV1e0TqF966LZdSEhzVL5hMVSotfY8laLLsy 3/Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=fi3gpq9hnnVh1G3NTQ1MirWR5bKsTeEH2CH3CJi0VpU=; b=fACgFVIlI11XjssxZb1MWhWlFNjUPazOZbwX8LbDt67oy2ugaQJwCnGLOgy796JHMk p8xPBbSwbOZn2TqYRr0lYMntmpaDBiiJzAsh2tJkc8vog8Y/upYbR+X2BWe0LeXW9lQa nZ1NREmQk8vivvykUJWhNsSVOw3X4UU10jaJhGKXSxmBeE2eSvHrjUVg4jQhGhgrzyPV FVWqFMkdomMJH3ed7LP5i+F0FhA+uGY56VeweYKb9ccc/aRYlg4E7Uo4ct+aMaDYD1MR aJofpTkSEL23MHQobMG+j651mmp2MghPWrj1utIg0rTmm/iUASsbFnIFEho5pnzgZIJv cCnQ== X-Gm-Message-State: ANoB5pnkvEkYaMMJu7k8XJU/eOMNpkla/9jFkrzVIyEblRiv1s2cRirf +dgMzSxFq/7BNXXeFLGSgaXWIg== X-Google-Smtp-Source: AA0mqf5Dr0aghsXn629RB3zoEJykFlGCTYQQYJJsr63zOeUXQLTuXuqjXi5xFANt90MChLGN1/o56Q== X-Received: by 2002:a17:90b:4a85:b0:21e:1c8e:f791 with SMTP id lp5-20020a17090b4a8500b0021e1c8ef791mr40781421pjb.31.1671445507740; Mon, 19 Dec 2022 02:25:07 -0800 (PST) Received: from sumit-X1.. ([223.178.213.5]) by smtp.gmail.com with ESMTPSA id 89-20020a17090a0fe200b0020087d7e778sm8832731pjz.37.2022.12.19.02.25.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Dec 2022 02:25:07 -0800 (PST) From: Sumit Garg To: will@kernel.org, catalin.marinas@arm.com, mark.rutland@arm.com, daniel.thompson@linaro.org, dianders@chromium.org Cc: liwei391@huawei.com, mhiramat@kernel.org, maz@kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Sumit Garg Subject: [PATCH v5 1/2] arm64: entry: Skip single stepping into interrupt handlers Date: Mon, 19 Dec 2022 15:54:51 +0530 Message-Id: <20221219102452.2860088-2-sumit.garg@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221219102452.2860088-1-sumit.garg@linaro.org> References: <20221219102452.2860088-1-sumit.garg@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221219_022510_923969_D22E6FE6 X-CRM114-Status: GOOD ( 16.37 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Currently on systems where the timer interrupt (or any other fast-at-human-scale periodic interrupt) is active then it is impossible to step any code with interrupts unlocked because we will always end up stepping into the timer interrupt instead of stepping the user code. The common user's goal while single stepping is that when they step then the system will stop at PC+4 or PC+I for a branch that gets taken relative to the instruction they are stepping. So, fix broken single step implementation via skipping single stepping into interrupt handlers. The methodology is when we receive an interrupt from EL1, check if we are single stepping (pstate.SS). If yes then we save MDSCR_EL1.SS and clear the register bit if it was set. Then unmask only D and leave I set. On return from the interrupt, set D and restore MDSCR_EL1.SS. Along with this skip reschedule if we were stepping. Suggested-by: Will Deacon Signed-off-by: Sumit Garg Tested-by: Douglas Anderson --- arch/arm64/kernel/entry-common.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kernel/entry-common.c b/arch/arm64/kernel/entry-common.c index cce1167199e3..688d1ef8e864 100644 --- a/arch/arm64/kernel/entry-common.c +++ b/arch/arm64/kernel/entry-common.c @@ -231,11 +231,15 @@ DEFINE_STATIC_KEY_TRUE(sk_dynamic_irqentry_exit_cond_resched); #define need_irq_preemption() (IS_ENABLED(CONFIG_PREEMPTION)) #endif -static void __sched arm64_preempt_schedule_irq(void) +static void __sched arm64_preempt_schedule_irq(struct pt_regs *regs) { if (!need_irq_preemption()) return; + /* Don't reschedule in case we are single stepping */ + if (!(regs->pstate & DBG_SPSR_SS)) + return; + /* * Note: thread_info::preempt_count includes both thread_info::count * and thread_info::need_resched, and is not equivalent to @@ -471,19 +475,33 @@ static __always_inline void __el1_irq(struct pt_regs *regs, do_interrupt_handler(regs, handler); irq_exit_rcu(); - arm64_preempt_schedule_irq(); + arm64_preempt_schedule_irq(regs); exit_to_kernel_mode(regs); } + static void noinstr el1_interrupt(struct pt_regs *regs, void (*handler)(struct pt_regs *)) { + unsigned long mdscr; + + /* Disable single stepping within interrupt handler */ + if (regs->pstate & DBG_SPSR_SS) { + mdscr = read_sysreg(mdscr_el1); + write_sysreg(mdscr & ~DBG_MDSCR_SS, mdscr_el1); + } + write_sysreg(DAIF_PROCCTX_NOIRQ, daif); if (IS_ENABLED(CONFIG_ARM64_PSEUDO_NMI) && !interrupts_enabled(regs)) __el1_pnmi(regs, handler); else __el1_irq(regs, handler); + + if (regs->pstate & DBG_SPSR_SS) { + write_sysreg(DAIF_PROCCTX_NOIRQ | PSR_D_BIT, daif); + write_sysreg(mdscr, mdscr_el1); + } } asmlinkage void noinstr el1h_64_irq_handler(struct pt_regs *regs) From patchwork Mon Dec 19 10:24:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sumit Garg X-Patchwork-Id: 13076458 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 E558FC4332F for ; Mon, 19 Dec 2022 10:27:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Si8fBL/357b+JWX2E51tD+D9YPkNLvPyqRg38pz/Es0=; b=NKXOIZYghKD/CI sV3fzVnmy7aM4MVFqlQdX8CTQ2M0p8o+58vA7AWhRsvPoD+cdTwIaMMjzYgckAFY8ozFXVQXFfp6j 2ZWGRfjezqK9HyScNP9kMCeUgOCioV65YKKV8tuYWcQzpzpRz8j93INT4jjdZhGqamyGL99V4V41t kpNW82nB31E2pV2ZkV66wLSKEkSzQROIcWskY6pnpgnFd7fYM+KjZbQqGEFG3fe3CLOj+84ryKOEJ JFoQY3Cn/KmhbHP1KUTdTpbNwQZAWuMPklPde+jPXZMhbnQ212Avml7x7Fx/ZI9FqFqNiaPWY9yDC CuYzo7oPHcc6bJNumBew==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1p7DLU-00Bqgz-1Q; Mon, 19 Dec 2022 10:26:08 +0000 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1p7DKc-00Bq3u-Jw for linux-arm-kernel@lists.infradead.org; Mon, 19 Dec 2022 10:25:16 +0000 Received: by mail-pl1-x62c.google.com with SMTP id l10so8556988plb.8 for ; Mon, 19 Dec 2022 02:25:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=FuxPSSo3Y0HNcsTRiv4LvjgP5Z2vwZbV6xJtY/KrG9Q=; b=iF+qURoE+eCvIhH+hu11xDlB1z41T3hUX0eRuYMR58bqJYsHsO6szJg+DZ0pgRz+wZ i5fI07gMXNCVwPsbIoCJjldV6Lt0jxuXmy0716NkGPWBlvmfUyZtC3AJ/nIpRwRrApm/ LjP9YyU7DMAGOKmaPhTxJyKGBaKPSFDooPoWgIBXTHa13GS8+DEN5wC7oWsNG2R0+/fJ kPb875U6gTeSe94NgDhGXPRwvzmBd7YqzJr6k+cM3sUUzigRoNQlsY/LKV84pTN7tApQ ZPGsGx0ao31/3sSiXBpLLjkFN1ED0R2Gk8fGBB09JcvLEAkQnIXpbwg7k2Vk2dhdLxzq p9yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=FuxPSSo3Y0HNcsTRiv4LvjgP5Z2vwZbV6xJtY/KrG9Q=; b=PLlRfqCqMNVA3Bw9UwqDbwM8J3kcOjXwITF7uzwFlEbduJ8OWZGaTQVhtFJP6metoU gz5av1vLizDEHqo0kZ3peOLGOF+Ew4ycqVFBmcuWYvp2FPu2RbJ3Isps6O+UPwFC6RHT ktc9qrgg5RUZUBE1QA7QEy7cDsbB0Ct5FaxgbsWJ3uAN0p3+Etq8GSRSjHB2wCcXUPZe 0pdAWHaeBje3pXb2hB4QZPrNXdqy53f6vnr9k0dUbwED/ppEMiJObhrMITfBfgY2a4Mp mKLQ5MLR93vgj1+KMrwlq7pLdNrh7L38cd/jezGOIc/oArFNLc4/fBNckvrxajD1eHdD ri6g== X-Gm-Message-State: ANoB5plqgbIs3oM3MnLg1r1F/OOvKXo1O3mpPBIwuizSfvl9sfnyM49j BrH0TsU6Zj3zMhKndrATcbdyPg== X-Google-Smtp-Source: AA0mqf4GEGyKCoOt8J5bSCi51BhHaNtnFoTfzPjm6BU4YFeOTo96NYOksytiio/BBbWX+8uXBHIM1Q== X-Received: by 2002:a17:90a:e147:b0:219:17bb:b854 with SMTP id ez7-20020a17090ae14700b0021917bbb854mr42684134pjb.29.1671445511662; Mon, 19 Dec 2022 02:25:11 -0800 (PST) Received: from sumit-X1.. ([223.178.213.5]) by smtp.gmail.com with ESMTPSA id 89-20020a17090a0fe200b0020087d7e778sm8832731pjz.37.2022.12.19.02.25.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Dec 2022 02:25:11 -0800 (PST) From: Sumit Garg To: will@kernel.org, catalin.marinas@arm.com, mark.rutland@arm.com, daniel.thompson@linaro.org, dianders@chromium.org Cc: liwei391@huawei.com, mhiramat@kernel.org, maz@kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Sumit Garg Subject: [PATCH v5 2/2] arm64: kgdb: Set PSTATE.SS to 1 to re-enable single-step Date: Mon, 19 Dec 2022 15:54:52 +0530 Message-Id: <20221219102452.2860088-3-sumit.garg@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221219102452.2860088-1-sumit.garg@linaro.org> References: <20221219102452.2860088-1-sumit.garg@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221219_022514_723430_9B7B3502 X-CRM114-Status: GOOD ( 14.00 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Currently only the first attempt to single-step has any effect. After that all further stepping remains "stuck" at the same program counter value. Refer to the ARM Architecture Reference Manual (ARM DDI 0487E.a) D2.12, PSTATE.SS=1 should be set at each step before transferring the PE to the 'Active-not-pending' state. The problem here is PSTATE.SS=1 is not set since the second single-step. After the first single-step, the PE transferes to the 'Inactive' state, with PSTATE.SS=0 and MDSCR.SS=1, thus PSTATE.SS won't be set to 1 due to kernel_active_single_step()=true. Then the PE transferes to the 'Active-pending' state when ERET and returns to the debugger by step exception. Before this patch: ================== Entering kdb (current=0xffff3376039f0000, pid 1) on processor 0 due to Keyboard Entry [0]kdb> [0]kdb> [0]kdb> bp write_sysrq_trigger Instruction(i) BP #0 at 0xffffa45c13d09290 (write_sysrq_trigger) is enabled addr at ffffa45c13d09290, hardtype=0 installed=0 [0]kdb> go $ echo h > /proc/sysrq-trigger Entering kdb (current=0xffff4f7e453f8000, pid 175) on processor 1 due to Breakpoint @ 0xffffad651a309290 [1]kdb> ss Entering kdb (current=0xffff4f7e453f8000, pid 175) on processor 1 due to SS trap @ 0xffffad651a309294 [1]kdb> ss Entering kdb (current=0xffff4f7e453f8000, pid 175) on processor 1 due to SS trap @ 0xffffad651a309294 [1]kdb> After this patch: ================= Entering kdb (current=0xffff6851c39f0000, pid 1) on processor 0 due to Keyboard Entry [0]kdb> bp write_sysrq_trigger Instruction(i) BP #0 at 0xffffc02d2dd09290 (write_sysrq_trigger) is enabled addr at ffffc02d2dd09290, hardtype=0 installed=0 [0]kdb> go $ echo h > /proc/sysrq-trigger Entering kdb (current=0xffff6851c53c1840, pid 174) on processor 1 due to Breakpoint @ 0xffffc02d2dd09290 [1]kdb> ss Entering kdb (current=0xffff6851c53c1840, pid 174) on processor 1 due to SS trap @ 0xffffc02d2dd09294 [1]kdb> ss Entering kdb (current=0xffff6851c53c1840, pid 174) on processor 1 due to SS trap @ 0xffffc02d2dd09298 [1]kdb> ss Entering kdb (current=0xffff6851c53c1840, pid 174) on processor 1 due to SS trap @ 0xffffc02d2dd0929c [1]kdb> Fixes: 44679a4f142b ("arm64: KGDB: Add step debugging support") Co-developed-by: Wei Li Signed-off-by: Wei Li Signed-off-by: Sumit Garg Tested-by: Douglas Anderson --- arch/arm64/include/asm/debug-monitors.h | 1 + arch/arm64/kernel/debug-monitors.c | 5 +++++ arch/arm64/kernel/kgdb.c | 2 ++ 3 files changed, 8 insertions(+) diff --git a/arch/arm64/include/asm/debug-monitors.h b/arch/arm64/include/asm/debug-monitors.h index 7b7e05c02691..ce3875ad5cd3 100644 --- a/arch/arm64/include/asm/debug-monitors.h +++ b/arch/arm64/include/asm/debug-monitors.h @@ -104,6 +104,7 @@ void user_regs_reset_single_step(struct user_pt_regs *regs, void kernel_enable_single_step(struct pt_regs *regs); void kernel_disable_single_step(void); int kernel_active_single_step(void); +void kernel_regs_reset_single_step(struct pt_regs *regs); #ifdef CONFIG_HAVE_HW_BREAKPOINT int reinstall_suspended_bps(struct pt_regs *regs); diff --git a/arch/arm64/kernel/debug-monitors.c b/arch/arm64/kernel/debug-monitors.c index 3da09778267e..9af898b22ed4 100644 --- a/arch/arm64/kernel/debug-monitors.c +++ b/arch/arm64/kernel/debug-monitors.c @@ -438,6 +438,11 @@ int kernel_active_single_step(void) } NOKPROBE_SYMBOL(kernel_active_single_step); +void kernel_regs_reset_single_step(struct pt_regs *regs) +{ + set_regs_spsr_ss(regs); +} + /* ptrace API */ void user_enable_single_step(struct task_struct *task) { diff --git a/arch/arm64/kernel/kgdb.c b/arch/arm64/kernel/kgdb.c index cda9c1e9864f..51f204bbcf87 100644 --- a/arch/arm64/kernel/kgdb.c +++ b/arch/arm64/kernel/kgdb.c @@ -224,6 +224,8 @@ int kgdb_arch_handle_exception(int exception_vector, int signo, */ if (!kernel_active_single_step()) kernel_enable_single_step(linux_regs); + else + kernel_regs_reset_single_step(linux_regs); err = 0; break; default: