Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp9034237ybi; Fri, 7 Jun 2019 02:24:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqwq4/3i89qHX6YuBp26t7fERB4i7bKh2fc7DnGR6OMIUSeSjTFb6MSsgzmoBTirlOsG4fSv X-Received: by 2002:aa7:8145:: with SMTP id d5mr58510901pfn.11.1559899482249; Fri, 07 Jun 2019 02:24:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559899482; cv=none; d=google.com; s=arc-20160816; b=jzcmcz5cMIDiGWumjHx/RjvqMbKdGJ/7KMVmZKlKQYvlVWu0nFKg7qPNre3XEt90o8 7HzCZ6gi9eCcija3UyVW8QuIo2chQK4aJYmYrq6wZocmbsrZ12/OirUw6eocDYisH13v +L0VgMitGRJWtIAvvpkUMfF0FsPWB0dKaeWtscpWZ1FrtLcS9wmQeHgzYOZDYIx/zOGD xhQlTQxMJ//6X4oOkAd0rnwU0SXpsQNm2BLpA5pE0+29sFImAnKIOmgDA5AmttU6ArQV BETuOfRVaYeaGfPfGRIEsE5bV/QnUzSCJ9+batQUHCh7n8C66SZafmL1DBkzwLpKoxx2 a73A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=JazDGPTciCVZpSGhyhHqtjBiLtA80UeGIT+jhVKWr9o=; b=ZvdwE6TpjGGyxwQqyfcXNJmFMG2yLSPDdTrGu3BanXIrenuPNLGTOAL7MM6LaqAS/p OuNHIHwfbujsRw9BJ4thC3dgAqIBRHCreh1eXtZcRTSEHlpJ/NO3/GcyLQD7Mz47V5lr rnz7hrYYqbWcYSRiB+1QA8MqkRpeBery4ceefdU3I0nyHTpRizIjIs2wGvQH3X7gont+ xZcLp88w+QZpHAU1t/Jungu6jxrzpwc5j87u1/b8zzVczV20unV81GZCYBCkkM+BHv2Q 6jsdsGOUtFM4laVzNxr4i/PyO4UllaSq9TdO/mopkRbqZqbRcbJhIEO/5u1xj0NJbIFD 78rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=Kme53K79; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v17si1267228pjn.37.2019.06.07.02.24.25; Fri, 07 Jun 2019 02:24:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=Kme53K79; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727343AbfFGIUp (ORCPT + 99 others); Fri, 7 Jun 2019 04:20:45 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:39662 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725978AbfFGIUo (ORCPT ); Fri, 7 Jun 2019 04:20:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Transfer-Encoding :Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Sender:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=JazDGPTciCVZpSGhyhHqtjBiLtA80UeGIT+jhVKWr9o=; b=Kme53K79YiAo8gwQa7fNoeLwvt lmUBsep1uyKFrtc32Scf/rZ4G5ZcIIBXbkjqlRYMGGDpeUdOoGHsQewbtazrvas1G4F0xalu4ytKh TPgKn9eJoxMD3lsvqwMs0YGSrnGgUt6c5coJw0AwuDzic3q9zsRJWd9bJ2HhJgKad6Tm8iqt/y347 E2xKm1R2/lC3BgaTlHJuB+DxU2GsiJdedpVRsHdYYnbGzf0wtYVj+BIKHIFoVBSwmgm+CKIJQ1JnG zpYQ0XlNm4jOuCxIEU2WLpe9syVirNmX/vFTfgaLkk4tr6Hqcfl3LlTskhUgG6r3PHUspNSXTCAbI 4zVLXuoQ==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.92 #3 (Red Hat Linux)) id 1hZA6h-0006P7-8H; Fri, 07 Jun 2019 08:20:15 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 682C520227133; Fri, 7 Jun 2019 10:20:13 +0200 (CEST) Date: Fri, 7 Jun 2019 10:20:13 +0200 From: Peter Zijlstra To: Nadav Amit Cc: the arch/x86 maintainers , LKML , Ard Biesheuvel , Andy Lutomirski , Steven Rostedt , Ingo Molnar , Thomas Gleixner , Linus Torvalds , Masami Hiramatsu , Jason Baron , Jiri Kosina , David Laight , Borislav Petkov , Julia Cartwright , Jessica Yu , "H. Peter Anvin" , Rasmus Villemoes , Edward Cree , Daniel Bristot de Oliveira Subject: Re: [PATCH 08/15] x86/alternatives: Teach text_poke_bp() to emulate instructions Message-ID: <20190607082013.GU3419@hirez.programming.kicks-ass.net> References: <20190605130753.327195108@infradead.org> <20190605131945.005681046@infradead.org> <7C13A4B6-6D5B-44C4-B238-58DC5926D7E1@vmware.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <7C13A4B6-6D5B-44C4-B238-58DC5926D7E1@vmware.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 07, 2019 at 05:41:42AM +0000, Nadav Amit wrote: > > int poke_int3_handler(struct pt_regs *regs) > > { > > + long ip = regs->ip - INT3_INSN_SIZE + CALL_INSN_SIZE; > > + struct opcode { > > + u8 insn; > > + s32 rel; > > + } __packed opcode; > > + > > /* > > * Having observed our INT3 instruction, we now must observe > > * bp_patching_in_progress. > > * > > - * in_progress = TRUE INT3 > > - * WMB RMB > > - * write INT3 if (in_progress) > > + * in_progress = TRUE INT3 > > + * WMB RMB > > + * write INT3 if (in_progress) > > I don’t see what has changed in this chunk… Whitespaces? Yep, my editor kept marking that stuff red (space before tab), which annoyed me enough to fix it. > > * > > - * Idem for bp_int3_handler. > > + * Idem for bp_int3_opcode. > > */ > > smp_rmb(); > > > > @@ -943,8 +949,21 @@ int poke_int3_handler(struct pt_regs *re > > if (user_mode(regs) || regs->ip != (unsigned long)bp_int3_addr) > > return 0; > > > > - /* set up the specified breakpoint handler */ > > - regs->ip = (unsigned long) bp_int3_handler; > > + opcode = *(struct opcode *)bp_int3_opcode; > > + > > + switch (opcode.insn) { > > + case 0xE8: /* CALL */ > > + int3_emulate_call(regs, ip + opcode.rel); > > + break; > > + > > + case 0xE9: /* JMP */ > > + int3_emulate_jmp(regs, ip + opcode.rel); > > + break; > > Consider using RELATIVECALL_OPCODE and RELATIVEJUMP_OPCODE instead of the > constants (0xE8, 0xE9), just as you do later in the patch. Those are private to kprobes.. but I can do something like so: --- a/arch/x86/include/asm/text-patching.h +++ b/arch/x86/include/asm/text-patching.h @@ -48,8 +48,14 @@ static inline void int3_emulate_jmp(stru regs->ip = ip; } -#define INT3_INSN_SIZE 1 -#define CALL_INSN_SIZE 5 +#define INT3_INSN_SIZE 1 +#define INT3_INSN_OPCODE 0xCC + +#define CALL_INSN_SIZE 5 +#define CALL_INSN_OPCODE 0xE8 + +#define JMP_INSN_SIZE 5 +#define JMP_INSN_OPCODE 0xE9 static inline void int3_emulate_push(struct pt_regs *regs, unsigned long val) { --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -952,11 +952,11 @@ int poke_int3_handler(struct pt_regs *re opcode = *(struct opcode *)bp_int3_opcode; switch (opcode.insn) { - case 0xE8: /* CALL */ + case CALL_INSN_OPCODE: int3_emulate_call(regs, ip + opcode.rel); break; - case 0xE9: /* JMP */ + case JMP_INSN_OPCODE: int3_emulate_jmp(regs, ip + opcode.rel); break;