Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp943535pxb; Wed, 3 Mar 2021 22:00:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJymVyOgKUFZMtAzJxLH9qcKAAJ64EcpvN2Xl2x0wWAPjxuojd2md1ZYl1GtvmV4N6JfNwbo X-Received: by 2002:a50:e183:: with SMTP id k3mr2582178edl.45.1614837623882; Wed, 03 Mar 2021 22:00:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614837623; cv=none; d=google.com; s=arc-20160816; b=oGu7+w5M6ic2wwJC5HJvkfVXuXNpgtcSTD27j2PMVE4iQinM+8BiUERiTtHybHUTE0 oGEbyAL/E95af1a/wTlKhMktigqxfTFgPS7NLcoBkNnzFofdRpBMHkplGFkohXLmM2D+ 42MFGJWGkTrqSXn2Cmj4tZAvqtWn539S/oYYOvtFFFXnV5cBWDtO1TL4M5Nz+CNM8UQg hFCEAzLHn7J4DbL+iiWpTqzT3nosJ0mkFsx94yy7rS9Sn+NkONFRzNlTkpP4SrB2frJo uEQ8cFEF4jpdJo4Kh7y+t4WwPFmjPHBopnriF28cgGyGBof/ZDv6QhlUTPi7nEPABLcR Qk2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=aF5QpyiukkBI21SbloK3u+Cas83GKy3SpzT4AciiVH4=; b=oadbiRd9cBT/XqsKudRF/aU2c1VhJwdEzbGiJDd5xWBfi2oJj7fq2hiHTCT7m6DMT9 gQxxgfsf07LCoQLjq78K2cG9/b0xaNl8TiYiJGWv8LN1Ad1xD8Q1IlQKpcEFi8Jx7YzI gr70caig3gNoIolIqZci786icjByJFl4vYh17e7GmLV/ZdM42CKMtLtwUfbtaEuP3uh0 5xtb93afT3HDlY1Aijw1XPqrSZUxw2qpwoD92txF0Ob0xFu+5wS1hn1hokaAxPbwMs1y EM9zrh8hiJP0oMNIL/eUzSMca8Q5CBJjIVZXd4GL8hkQxgBQ/yhJQeumJ65H1yc1zCij Lwig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b="kaq8D/iH"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id eb13si17984851edb.315.2021.03.03.21.59.59; Wed, 03 Mar 2021 22:00:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b="kaq8D/iH"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376935AbhCBISM (ORCPT + 99 others); Tue, 2 Mar 2021 03:18:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1837224AbhCBIII (ORCPT ); Tue, 2 Mar 2021 03:08:08 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D12DFC061756 for ; Tue, 2 Mar 2021 00:07:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=aF5QpyiukkBI21SbloK3u+Cas83GKy3SpzT4AciiVH4=; b=kaq8D/iH6Ax90VqbAwDi4i6n9Q eElkBP6Pad9RsCQzwHFdMiqPC480g/Ahrkg8HDRQpqoVdymPW6rbD31KKdb8dRyv3LhYNcLv3oQlL BqOA0idxO5Oo8bF3azC/nn6qODQ4B0/j3Z+JmPyHUTdOjHIutah2nLmCVflzle6oxDf3ikY/+EjqQ 3Uc5B83ao4KLFMr0jniTBUllH09GW3cGw2KeuPN3AOsO0SmPNqkta8Qcl1s2rI47jexB/ZtBDQUlS AXcKwSFQTUg25Ag8TdyKZ86+fhoDNRhBajQq92uoVxKyzXFLMU4Jjrcqu105cUYDo9+fcRZcUE+RD P8+EYW3w==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94 #2 (Red Hat Linux)) id 1lH03P-00GnFL-84; Tue, 02 Mar 2021 08:06:55 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id F0D633003E1; Tue, 2 Mar 2021 09:06:47 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 8612C2215BD38; Tue, 2 Mar 2021 09:06:47 +0100 (CET) Date: Tue, 2 Mar 2021 09:06:47 +0100 From: Peter Zijlstra To: Masami Hiramatsu Cc: Andy Lutomirski , Ingo Molnar , Oleg Nesterov , LKML , Anil S Keshavamurthy , "David S . Miller" , X86 ML , Andrew Cooper , Steven Rostedt Subject: Re: [RFC PATCH 1/1] x86/kprobes: Use int3 instead of debug trap for single-step Message-ID: References: <161460768474.430263.18425867006584111900.stgit@devnote2> <161460769556.430263.12936080446789384938.stgit@devnote2> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <161460769556.430263.12936080446789384938.stgit@devnote2> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Mar 01, 2021 at 11:08:15PM +0900, Masami Hiramatsu wrote: > +static void kprobe_emulate_jmp(struct kprobe *p, struct pt_regs *regs) > +{ > + unsigned long ip = regs->ip - INT3_INSN_SIZE + p->ainsn.size; > + > + ip += p->ainsn.rel32; > + int3_emulate_jmp(regs, ip); > +} > +NOKPROBE_SYMBOL(kprobe_emulate_jmp); Would it make sense to have this be something like: static void kprobe_emulate_jmp(struct kprobe *p, struct pt_regs *regs, bool cond) { unsigned long ip = regs->ip - INT3_INSN_SIZE + p->ainsn.size; if (cond) ip += p->ainsn.rel32; int3_emulate_jmp(regs, ip); } > +static void kprobe_emulate_jcc(struct kprobe *p, struct pt_regs *regs) > +{ > + bool invert = p->ainsn.jcc.type & 1; > + bool match; > + > + if (p->ainsn.jcc.type < 0xc) { > + match = regs->flags & jcc_mask[p->ainsn.jcc.type >> 1]; > + } else { > + match = ((regs->flags & X86_EFLAGS_SF) >> X86_EFLAGS_SF_BIT) ^ > + ((regs->flags & X86_EFLAGS_OF) >> X86_EFLAGS_OF_BIT); > + if (p->ainsn.jcc.type >= 0xe) > + match = match && (regs->flags & X86_EFLAGS_ZF); > + } > + if ((match && !invert) || (!match && invert)) > + kprobe_emulate_jmp(p, regs); > + else > + regs->ip = regs->ip - INT3_INSN_SIZE + p->ainsn.size; Then you can do: kprobe_emulate_jmp(p, regs, match); > +} > +NOKPROBE_SYMBOL(kprobe_emulate_jcc); > + > +static void kprobe_emulate_loop(struct kprobe *p, struct pt_regs *regs) > +{ > + bool match; > + > + if (p->ainsn.loop.type != 3) { /* LOOP* */ > + if (p->ainsn.loop.asize == 32) > + match = ((*(u32 *)®s->cx)--) != 0; > +#ifdef CONFIG_X86_64 > + else if (p->ainsn.loop.asize == 64) > + match = ((*(u64 *)®s->cx)--) != 0; > +#endif > + else > + match = ((*(u16 *)®s->cx)--) != 0; > + } else { /* JCXZ */ > + if (p->ainsn.loop.asize == 32) > + match = *(u32 *)(®s->cx) == 0; > +#ifdef CONFIG_X86_64 > + else if (p->ainsn.loop.asize == 64) > + match = *(u64 *)(®s->cx) == 0; > +#endif > + else > + match = *(u16 *)(®s->cx) == 0; > + } > + > + if (p->ainsn.loop.type == 0) /* LOOPNE */ > + match = match && !(regs->flags & X86_EFLAGS_ZF); > + else if (p->ainsn.loop.type == 1) /* LOOPE */ > + match = match && (regs->flags & X86_EFLAGS_ZF); > + > + if (match) > + kprobe_emulate_jmp(p, regs); > + else > + regs->ip = regs->ip - INT3_INSN_SIZE + p->ainsn.size; and here. > +} > +NOKPROBE_SYMBOL(kprobe_emulate_loop);