From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0BF58399039 for ; Mon, 27 Apr 2026 12:28:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777292917; cv=none; b=OuNgIm+Idx9GtuL3ZkoC+9dagp57Q7LyhjZyQZPVP8IVoUrQ0VuTNTvLPxOhQk8TiA4+yIyFRrhEvc4/uV17Mek6rlovTi/HwrwRjq746B3XFitu+fWAXeWcxlNy0xDfJ/aWKZGlI6zQBDBcASO4HGGCI5AV5wlpj73vm9lQnFg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777292917; c=relaxed/simple; bh=0jqlpacgYhlTSHMmkteTI+x4Cq70RDtzQewaneuhuQc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QJKUNPcbSIaeiKhnVMSpMH5ivDbuY/g0f4YsDvHhJm0D1MWFymkeFVT3gj2WCD7wZCC5eGV//Je4pvdsyzR97OGh7UBf/s/tj5JXTN6aloD1EHcr14A5UTvWh0jMb+83N1ZlIbJN1r40OJNvGnN0BnpQr9RS1W2hkUGK+iMXKUk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=QY+wWD3R; arc=none smtp.client-ip=209.85.210.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QY+wWD3R" Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-8296d553142so5586758b3a.3 for ; Mon, 27 Apr 2026 05:28:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777292915; x=1777897715; darn=vger.kernel.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=raVDKr4Vko7RlTRFhwZDaBEgYPbFcEnfldAQ2J2hXNQ=; b=QY+wWD3RwX1Pm3r5AvbLwA2UU+cvHKXV6Al/p9lUFsBt7LiovMs0Yne+PFB1KKog5g dc5K0ZEbgidRsoRgIJNRXrsXMSk3oPgF1TKwhCRhuaVLllnUak5zUQkD4Mk7uPlgEf9G uNnBNoq1ao8dGJnAkyCwY32sWkEzuImpN++J0T6DyaiHFgB5h485fwBcK4NQf8USTZKi ZWTMY86n1vbgJQyEVlyXCgUAt9KvO1CHHkpC6ss1OVxiOHwevukxgaRaMDfkGH7EE5// twK7yasgPJ2Hhz2obYuttGj4Cb1ZjE4DPIfbKDZGf408zOpIMoJUGrKH6hjIFj+uZHtk yqag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777292915; x=1777897715; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=raVDKr4Vko7RlTRFhwZDaBEgYPbFcEnfldAQ2J2hXNQ=; b=GY/1ZTXQXUe0+QcdcoAIRQuK71y+oCDd8APslNbqxftqXR0U7beG3Beqge2iKxFwKO uH5GOYkHykWAxHJ6I7r8uVuUfWdmBsFTqB599eHlIieGQ8e9D4DpHuxc3MA7y3f75r1O PSl+CYrBn355/XvhMPmHsJEEqi8qSofCtD9IPfmqhkzwu3TM5PkieYsClquxu894t/h3 MLkgwUzpXV7zmGWeS5H1iFxUyjm1pRfZpTYZ1sVDOA68yhwoW9lnmmrBR/YoBKaJyRY3 b+keZs83PXAEoQ5Pumk5hBxvCBj7JLv3srAq5Iuk2kVRFBcOOus5iuzaeY5mYEket2Jl rmuw== X-Forwarded-Encrypted: i=1; AFNElJ9dLKJs3DLXiMEb88XGCmJo2duUq8vu2uTXQA0OKtwdOdiHh+uK60jqGOBOLCT/zEm3cR2eqUS28q2QXjY=@vger.kernel.org X-Gm-Message-State: AOJu0Yy+qp9ndFj3S+RBx4TaOoTYCn2Sgyy00q1Y/a5Ddmv64qpRPW08 aZJpv/wY+HcmTmjM8ALIJLC3BO5XpC6TjZpDE7BXrqIh9RtW0Q6LeLB6 X-Gm-Gg: AeBDieu+JlwuBm8ccuTomRS2aHzgSd6lA843CPQoN7fDEspCeWlq6US+CTGIVnqgWj/ vnaN0qooFqA+y1uleKvCoqp0JOPWDbfDbJTUt/mgkti7dHdGXr9AC5mQC7KlZeb8R9LAZJpimp6 j71/9d7Rb8q6Ubuvtwpj5hKPcvECpxqKgLLaZakKqlKjEixoOd8PHjfH0k02iOpUe/PmbRq89et y6vl3CfSleFdb5pq5I6ivv6Nx0lzspxBo7yml26ZKejvSTbjQ0WpDuLqJvRo0Wf0bWmEesepAUJ i06kqb+z2JDz+usWPgHCGzBA3ZsTzk0LtvlmywkVQy4+hlsOuVdr7KDKF6lOH0laidi8OVxaW+d KmmVJguhI5Z5nUdLAE/BofQabu8IdzBqE4j8kr6lwty6J9UjfaSPukhPwH34O88yvyxrZPwgewu Tn06E1a7FgLPY1Y/Js5JgH6fFQYYoGLOgRp7O+i1pQh9CqBE9lgtwI01iacCtSoLXf5t5xGtQjK Ot4CtlZ2dFd0S6K X-Received: by 2002:a05:6a00:4214:b0:81f:3fbd:ccf with SMTP id d2e1a72fcca58-82f8c8bcc27mr42452795b3a.23.1777292915109; Mon, 27 Apr 2026 05:28:35 -0700 (PDT) Received: from li-1a3e774c-28e4-11b2-a85c-acc9f2883e29.ibm.com ([129.41.58.4]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82f8e9f7735sm32733466b3a.21.2026.04.27.05.28.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2026 05:28:34 -0700 (PDT) From: "Mukesh Kumar Chaurasiya (IBM)" To: maddy@linux.ibm.com, mpe@ellerman.id.au, npiggin@gmail.com, chleroy@kernel.org, ryabinin.a.a@gmail.com, glider@google.com, andreyknvl@gmail.com, dvyukov@google.com, vincenzo.frascino@arm.com, oleg@redhat.com, kees@kernel.org, luto@amacapital.net, wad@chromium.org, mchauras@linux.ibm.com, sshegde@linux.ibm.com, thuth@redhat.com, ruanjinjie@huawei.com, akpm@linux-foundation.org, macro@orcam.me.uk, ldv@strace.io, charlie@rivosinc.com, deller@gmx.de, kevin.brodsky@arm.com, ritesh.list@gmail.com, yeoreum.yun@arm.com, agordeev@linux.ibm.com, segher@kernel.crashing.org, mark.rutland@arm.com, ryan.roberts@arm.com, pmladek@suse.com, feng.tang@linux.alibaba.com, peterz@infradead.org, kan.liang@linux.intel.com, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com Cc: Samir M , David Gow , Venkat Rao Bagalkote Subject: [PATCH v5 3/8] powerpc: introduce arch_enter_from_user_mode Date: Mon, 27 Apr 2026 17:57:37 +0530 Message-ID: <20260427122742.210074-4-mkchauras@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260427122742.210074-1-mkchauras@gmail.com> References: <20260427122742.210074-1-mkchauras@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Mukesh Kumar Chaurasiya Implement the arch_enter_from_user_mode() hook required by the generic entry/exit framework. This helper prepares the CPU state when entering the kernel from userspace, ensuring correct handling of KUAP/KUEP, transactional memory, and debug register state. This patch contains no functional changes, it is purely preparatory for enabling the generic syscall and interrupt entry path on PowerPC. Signed-off-by: Mukesh Kumar Chaurasiya Tested-by: Samir M Tested-by: David Gow Tested-by: Venkat Rao Bagalkote Reviewed-by: Shrikanth Hegde --- arch/powerpc/include/asm/entry-common.h | 118 ++++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/arch/powerpc/include/asm/entry-common.h b/arch/powerpc/include/asm/entry-common.h index 05ce0583b600..837a7e020e82 100644 --- a/arch/powerpc/include/asm/entry-common.h +++ b/arch/powerpc/include/asm/entry-common.h @@ -3,6 +3,124 @@ #ifndef _ASM_PPC_ENTRY_COMMON_H #define _ASM_PPC_ENTRY_COMMON_H +#include +#include #include +#include + +static __always_inline void booke_load_dbcr0(void) +{ +#ifdef CONFIG_PPC_ADV_DEBUG_REGS + unsigned long dbcr0 = current->thread.debug.dbcr0; + + if (likely(!(dbcr0 & DBCR0_IDM))) + return; + + /* + * Check to see if the dbcr0 register is set up to debug. + * Use the internal debug mode bit to do this. + */ + mtmsr(mfmsr() & ~MSR_DE); + if (IS_ENABLED(CONFIG_PPC32)) { + isync(); + global_dbcr0[smp_processor_id()] = mfspr(SPRN_DBCR0); + } + mtspr(SPRN_DBCR0, dbcr0); + mtspr(SPRN_DBSR, -1); +#endif +} + +static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs) +{ + kuap_lock(); + + if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG)) + BUG_ON(irq_soft_mask_return() != IRQS_ALL_DISABLED); + + BUG_ON(regs_is_unrecoverable(regs)); + BUG_ON(!user_mode(regs)); + BUG_ON(regs_irqs_disabled(regs)); + +#ifdef CONFIG_PPC_PKEY + if (mmu_has_feature(MMU_FTR_PKEY) && trap_is_syscall(regs)) { + unsigned long amr, iamr; + bool flush_needed = false; + /* + * When entering from userspace we mostly have the AMR/IAMR + * different from kernel default values. Hence don't compare. + */ + amr = mfspr(SPRN_AMR); + iamr = mfspr(SPRN_IAMR); + regs->amr = amr; + regs->iamr = iamr; + if (mmu_has_feature(MMU_FTR_KUAP)) { + mtspr(SPRN_AMR, AMR_KUAP_BLOCKED); + flush_needed = true; + } + if (mmu_has_feature(MMU_FTR_BOOK3S_KUEP)) { + mtspr(SPRN_IAMR, AMR_KUEP_BLOCKED); + flush_needed = true; + } + if (flush_needed) + isync(); + } +#endif + kuap_assert_locked(); + booke_restore_dbcr0(); + account_cpu_user_entry(); + account_stolen_time(); + + /* + * This is not required for the syscall exit path, but makes the + * stack frame look nicer. If this was initialised in the first stack + * frame, or if the unwinder was taught the first stack frame always + * returns to user with IRQS_ENABLED, this store could be avoided! + */ + irq_soft_mask_regs_set_state(regs, IRQS_ENABLED); + + /* + * If system call is called with TM active, set _TIF_RESTOREALL to + * prevent RFSCV being used to return to userspace, because POWER9 + * TM implementation has problems with this instruction returning to + * transactional state. Final register values are not relevant because + * the transaction will be aborted upon return anyway. Or in the case + * of unsupported_scv SIGILL fault, the return state does not much + * matter because it's an edge case. + */ + if (IS_ENABLED(CONFIG_PPC_TRANSACTIONAL_MEM) && + unlikely(MSR_TM_TRANSACTIONAL(regs->msr))) + set_bits(_TIF_RESTOREALL, ¤t_thread_info()->flags); + + /* + * If the system call was made with a transaction active, doom it and + * return without performing the system call. Unless it was an + * unsupported scv vector, in which case it's treated like an illegal + * instruction. + */ +#ifdef CONFIG_PPC_TRANSACTIONAL_MEM + if (unlikely(MSR_TM_TRANSACTIONAL(regs->msr)) && + !trap_is_unsupported_scv(regs)) { + /* Enable TM in the kernel, and disable EE (for scv) */ + hard_irq_disable(); + mtmsr(mfmsr() | MSR_TM); + + /* tabort, this dooms the transaction, nothing else */ + asm volatile(".long 0x7c00071d | ((%0) << 16)" + :: "r"(TM_CAUSE_SYSCALL | TM_CAUSE_PERSISTENT)); + + /* + * Userspace will never see the return value. Execution will + * resume after the tbegin. of the aborted transaction with the + * checkpointed register state. A context switch could occur + * or signal delivered to the process before resuming the + * doomed transaction context, but that should all be handled + * as expected. + */ + return; + } +#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */ +} + +#define arch_enter_from_user_mode arch_enter_from_user_mode #endif /* _ASM_PPC_ENTRY_COMMON_H */ -- 2.53.0