Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp250485ybi; Fri, 7 Jun 2019 07:29:43 -0700 (PDT) X-Google-Smtp-Source: APXvYqxqf6wkmkXB7+Z0KryEJnRjQx5Qaim53zdzdcSI/9STijGzLhn7YSqq5FmiLPF8JxUAqNi7 X-Received: by 2002:a17:90a:af8b:: with SMTP id w11mr5677424pjq.135.1559917783444; Fri, 07 Jun 2019 07:29:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559917783; cv=none; d=google.com; s=arc-20160816; b=ghLUgo7a/jsOph8oB2wopVfX7vQUZJyFrR/k1MVwOZlAVlO+dRewowUhd9Seac4G4x 6og6pdqpWklDiAd1ZnYeeFJdzPVncEP74aSfFNsQgJ1CaqVsAEwXdp3zeYyWew1Vxtgu M2ixniJ5RBothrF+2QVklU3dEr9WCwrKtZwxx6pQckleMP4g4ORTS5sgcDLJJ/8i/HYY v9g7vogidXg+NLu/epfqlihhUCMcBeCSa1yNbH8/nb8cqHW+xYV+6w1RefKnlEpPbATx tAORuHE+A5zvxV0pGd7ZKHD4qJwO8GOwSUSkesRPOjYYcXmgaO1GaM5FFzGYom6/pe5g 086Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=XVrQ9Q8fdX8fhxFzFibRC9Z3OFPCDAEqepZj31KwVDQ=; b=NgUJkkTW7yK5IXq1wn3KKt/as2UuImDdK2qZulSTBLrjLMl1n4yj55fHdtjQvni+m5 O2kSPHVpYyS616wKwV2KrZ1JfWPFkSLXCVjhDurjW0IqvcBMjJJJbDk84yu7nVPuzt1M qcOCK2sNpiDpVkWHWVe0FUv3CxLA2LfWoFp+2cV7xtCz0A5qej+UJTkzAk/cI74QrKbz jW1BOoNPefOZdE2vw1+IogAzxYOaRJ2RRGHns3juj1HGhg3oG/duI5r962/xc+EOkxx/ MArg4P7g2IMhEtRrEhuXRi2H7mL5vtfScX6V3eZeEhARqSzzJEdeP7ASDUxGtKQsqjMN YH8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=k+p8AstV; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u1si1784785pgo.595.2019.06.07.07.29.26; Fri, 07 Jun 2019 07:29:43 -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=pass header.i=@kernel.org header.s=default header.b=k+p8AstV; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729065AbfFGO1t (ORCPT + 99 others); Fri, 7 Jun 2019 10:27:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:48336 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728311AbfFGO1t (ORCPT ); Fri, 7 Jun 2019 10:27:49 -0400 Received: from devnote2 (NE2965lan1.rev.em-net.ne.jp [210.141.244.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6F38B20657; Fri, 7 Jun 2019 14:27:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1559917668; bh=gpoMFWooueTQcAcnu+53x5cQNpLlXmAEYoHjD6z/m7k=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=k+p8AstVzyP+536XgnpxVRsJ1+fQ6U6uluFSXbFgndysnWO2qt+6W9yZrploqhoh0 tMqO8iIrJxr2F23+bCY10J+2Dh4tz/zXKyKE8zwQpGmtPUhNdNANPe5FCtCYqCbIiU LoZisGMJ/GbJosPoe3/A8mEUB0bC4TOmJcEW3Ntg= Date: Fri, 7 Jun 2019 23:27:42 +0900 From: Masami Hiramatsu To: Peter Zijlstra Cc: Nadav Amit , 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: <20190607232742.d331359f0d511c78d06e1703@kernel.org> In-Reply-To: <20190607082013.GU3419@hirez.programming.kicks-ass.net> References: <20190605130753.327195108@infradead.org> <20190605131945.005681046@infradead.org> <7C13A4B6-6D5B-44C4-B238-58DC5926D7E1@vmware.com> <20190607082013.GU3419@hirez.programming.kicks-ass.net> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.32; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-2022-JP Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 7 Jun 2019 10:20:13 +0200 Peter Zijlstra wrote: > 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; > This looks good. I don't want to make those opcode as private. I would like to share it. Thank you, -- Masami Hiramatsu