From nobody Sat Sep 13 18:30:09 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 24EC4C636CC for ; Tue, 31 Jan 2023 14:23:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231815AbjAaOXl (ORCPT ); Tue, 31 Jan 2023 09:23:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231778AbjAaOXI (ORCPT ); Tue, 31 Jan 2023 09:23:08 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5876451C61; Tue, 31 Jan 2023 06:22:43 -0800 (PST) Date: Tue, 31 Jan 2023 14:22:41 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1675174961; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=i3bzBs9Bh9DlNrWvc/viADz3sFPPO+Uti50tqCqd8Yk=; b=QuSeKuN3pRhGwcM7cVlULuKuKC4/CK3+s6VZT+mMqVIFA+0NkyIryWxt8FJ+khFqvyquLh 7AvMqyS6yGkor6XbMduH5LpwEP5YuWQoa0vGnuNMgo740Rt7HWi2CKbt8yJoLfG2xDJeZI x7GwdsMdm+tUIbUKMGz3KicE4SXsC2f1ahBLUFnt7pbZX9OhGGOEjv1L3gvXTZh8LykB9b kpz8dWfxkxpQ7AvGcFPn+Oe+BOP+ui36GMvQSGoauwX2Jsqy5AOKpmCSwZCCOY6A32sxHD Ksk/fHFWhODoMDOSHa+FbtfUsCT6lbJtnHA+t0sRWj66INs69OIm7xBrm3nadQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1675174961; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=i3bzBs9Bh9DlNrWvc/viADz3sFPPO+Uti50tqCqd8Yk=; b=L26UrnZ5wEQDzrLjp39em4MCIrOg3eAVN00cEZwWTbovNAOAtlm8Cm2MXDmdC6vTLLJl4B 9DQ91rfZu0/irFDQ== From: "tip-bot2 for Peter Zijlstra" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/alternatives] x86/alternatives: Introduce int3_emulate_jcc() Cc: "Peter Zijlstra (Intel)" , Ingo Molnar , "Masami Hiramatsu (Google)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230123210607.057678245@infradead.org> References: <20230123210607.057678245@infradead.org> MIME-Version: 1.0 Message-ID: <167517496157.4906.15756539194574782958.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/alternatives branch of ti= p: Commit-ID: db7adcfd1cec4e95155e37bc066fddab302c6340 Gitweb: https://git.kernel.org/tip/db7adcfd1cec4e95155e37bc066fddab3= 02c6340 Author: Peter Zijlstra AuthorDate: Mon, 23 Jan 2023 21:59:16 +01:00 Committer: Ingo Molnar CommitterDate: Tue, 31 Jan 2023 15:05:30 +01:00 x86/alternatives: Introduce int3_emulate_jcc() Move the kprobe Jcc emulation into int3_emulate_jcc() so it can be used by more code -- specifically static_call() will need this. Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Ingo Molnar Reviewed-by: Masami Hiramatsu (Google) Link: https://lore.kernel.org/r/20230123210607.057678245@infradead.org --- arch/x86/include/asm/text-patching.h | 31 ++++++++++++++++++++++- arch/x86/kernel/kprobes/core.c | 38 +++++---------------------- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/arch/x86/include/asm/text-patching.h b/arch/x86/include/asm/te= xt-patching.h index f4b87f0..29832c3 100644 --- a/arch/x86/include/asm/text-patching.h +++ b/arch/x86/include/asm/text-patching.h @@ -184,6 +184,37 @@ void int3_emulate_ret(struct pt_regs *regs) unsigned long ip =3D int3_emulate_pop(regs); int3_emulate_jmp(regs, ip); } + +static __always_inline +void int3_emulate_jcc(struct pt_regs *regs, u8 cc, unsigned long ip, unsig= ned long disp) +{ + static const unsigned long jcc_mask[6] =3D { + [0] =3D X86_EFLAGS_OF, + [1] =3D X86_EFLAGS_CF, + [2] =3D X86_EFLAGS_ZF, + [3] =3D X86_EFLAGS_CF | X86_EFLAGS_ZF, + [4] =3D X86_EFLAGS_SF, + [5] =3D X86_EFLAGS_PF, + }; + + bool invert =3D cc & 1; + bool match; + + if (cc < 0xc) { + match =3D regs->flags & jcc_mask[cc >> 1]; + } else { + match =3D ((regs->flags & X86_EFLAGS_SF) >> X86_EFLAGS_SF_BIT) ^ + ((regs->flags & X86_EFLAGS_OF) >> X86_EFLAGS_OF_BIT); + if (cc >=3D 0xe) + match =3D match || (regs->flags & X86_EFLAGS_ZF); + } + + if ((match && !invert) || (!match && invert)) + ip +=3D disp; + + int3_emulate_jmp(regs, ip); +} + #endif /* !CONFIG_UML_X86 */ =20 #endif /* _ASM_X86_TEXT_PATCHING_H */ diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c index 6629968..d48638c 100644 --- a/arch/x86/kernel/kprobes/core.c +++ b/arch/x86/kernel/kprobes/core.c @@ -460,50 +460,26 @@ static void kprobe_emulate_call(struct kprobe *p, str= uct pt_regs *regs) } NOKPROBE_SYMBOL(kprobe_emulate_call); =20 -static nokprobe_inline -void __kprobe_emulate_jmp(struct kprobe *p, struct pt_regs *regs, bool con= d) +static void kprobe_emulate_jmp(struct kprobe *p, struct pt_regs *regs) { unsigned long ip =3D regs->ip - INT3_INSN_SIZE + p->ainsn.size; =20 - if (cond) - ip +=3D p->ainsn.rel32; + ip +=3D p->ainsn.rel32; int3_emulate_jmp(regs, ip); } - -static void kprobe_emulate_jmp(struct kprobe *p, struct pt_regs *regs) -{ - __kprobe_emulate_jmp(p, regs, true); -} NOKPROBE_SYMBOL(kprobe_emulate_jmp); =20 -static const unsigned long jcc_mask[6] =3D { - [0] =3D X86_EFLAGS_OF, - [1] =3D X86_EFLAGS_CF, - [2] =3D X86_EFLAGS_ZF, - [3] =3D X86_EFLAGS_CF | X86_EFLAGS_ZF, - [4] =3D X86_EFLAGS_SF, - [5] =3D X86_EFLAGS_PF, -}; - static void kprobe_emulate_jcc(struct kprobe *p, struct pt_regs *regs) { - bool invert =3D p->ainsn.jcc.type & 1; - bool match; + unsigned long ip =3D regs->ip - INT3_INSN_SIZE + p->ainsn.size; =20 - if (p->ainsn.jcc.type < 0xc) { - match =3D regs->flags & jcc_mask[p->ainsn.jcc.type >> 1]; - } else { - match =3D ((regs->flags & X86_EFLAGS_SF) >> X86_EFLAGS_SF_BIT) ^ - ((regs->flags & X86_EFLAGS_OF) >> X86_EFLAGS_OF_BIT); - if (p->ainsn.jcc.type >=3D 0xe) - match =3D match || (regs->flags & X86_EFLAGS_ZF); - } - __kprobe_emulate_jmp(p, regs, (match && !invert) || (!match && invert)); + int3_emulate_jcc(regs, p->ainsn.jcc.type, ip, p->ainsn.rel32); } NOKPROBE_SYMBOL(kprobe_emulate_jcc); =20 static void kprobe_emulate_loop(struct kprobe *p, struct pt_regs *regs) { + unsigned long ip =3D regs->ip - INT3_INSN_SIZE + p->ainsn.size; bool match; =20 if (p->ainsn.loop.type !=3D 3) { /* LOOP* */ @@ -531,7 +507,9 @@ static void kprobe_emulate_loop(struct kprobe *p, struc= t pt_regs *regs) else if (p->ainsn.loop.type =3D=3D 1) /* LOOPE */ match =3D match && (regs->flags & X86_EFLAGS_ZF); =20 - __kprobe_emulate_jmp(p, regs, match); + if (match) + ip +=3D p->ainsn.rel32; + int3_emulate_jmp(regs, ip); } NOKPROBE_SYMBOL(kprobe_emulate_loop);