From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.5 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A119BC35280 for ; Fri, 1 May 2020 20:32:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7B02D21835 for ; Fri, 1 May 2020 20:32:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="WpKZrF34" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727879AbgEAUcA (ORCPT ); Fri, 1 May 2020 16:32:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726914AbgEAUbh (ORCPT ); Fri, 1 May 2020 16:31:37 -0400 Received: from merlin.infradead.org (unknown [IPv6:2001:8b0:10b:1231::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79A91C08E934 for ; Fri, 1 May 2020 13:31:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-Id:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=AZ9gMrfy7tII+1yB9euI/yxsmZUgWasRxZVXQg2YzCc=; b=WpKZrF348CpQuGlYGbMwqQe+JI da8brayydUzonyxrQlwn8WcLLU+hWDc54ZLfMEVNJV7HyDRyXqso+hLwlyQdRlFAa6hMxgYF2E1jX HKWMvXd1RhO7KbYcOY8U5SqzRimls2lBeUK0I2MBsxMNAS5e3mKDCnKbSEu0SIOQpYFhjUcZKv17K dK2BY7QWnwiYh812sBOo9E+Skce4qHEmB26Yaovz/5KvXoaAdNwqrNf+2UGHk4PnNJssLJJRELH7J KIUKZO4eqSyMvi6fBUT5kp6L6siNboO4uLECOWo48yEta+nXmCrnCTsIR/kOQOjve4vO8Q94QH+eI tYummDfw==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1jUcJN-0007RF-Un; Fri, 01 May 2020 20:31:06 +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 967C33075F9; Fri, 1 May 2020 22:31:01 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id F405329BF6267; Fri, 1 May 2020 22:31:00 +0200 (CEST) Message-Id: <20200501202944.536135282@infradead.org> User-Agent: quilt/0.65 Date: Fri, 01 May 2020 22:29:02 +0200 From: Peter Zijlstra To: x86@kernel.org Cc: peterz@infradead.org, linux-kernel@vger.kernel.org, rostedt@goodmis.org, mhiramat@kernel.org, bristot@redhat.com, jbaron@akamai.com, torvalds@linux-foundation.org, tglx@linutronix.de, mingo@kernel.org, namit@vmware.com, hpa@zytor.com, luto@kernel.org, ard.biesheuvel@linaro.org, jpoimboe@redhat.com, pbonzini@redhat.com, mathieu.desnoyers@efficios.com Subject: [PATCH v4 13/18] x86/alternatives: Teach text_poke_bp() to emulate RET References: <20200501202849.647891881@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Future patches will need to poke a RET instruction, provide the infrastructure required for this. Signed-off-by: Peter Zijlstra (Intel) --- arch/x86/include/asm/text-patching.h | 17 +++++++++++++++++ arch/x86/kernel/alternative.c | 5 +++++ 2 files changed, 22 insertions(+) --- a/arch/x86/include/asm/text-patching.h +++ b/arch/x86/include/asm/text-patching.h @@ -53,6 +53,9 @@ extern void text_poke_finish(void); #define INT3_INSN_SIZE 1 #define INT3_INSN_OPCODE 0xCC +#define RET_INSN_SIZE 1 +#define RET_INSN_OPCODE 0xC3 + #define CALL_INSN_SIZE 5 #define CALL_INSN_OPCODE 0xE8 @@ -73,6 +76,7 @@ static inline int text_opcode_size(u8 op switch(opcode) { __CASE(INT3); + __CASE(RET); __CASE(CALL); __CASE(JMP32); __CASE(JMP8); @@ -138,11 +142,24 @@ static inline void int3_emulate_push(str *(unsigned long *)regs->sp = val; } +static inline unsigned long int3_emulate_pop(struct pt_regs *regs) +{ + unsigned long val = *(unsigned long *)regs->sp; + regs->sp += sizeof(unsigned long); + return val; +} + static inline void int3_emulate_call(struct pt_regs *regs, unsigned long func) { int3_emulate_push(regs, regs->ip - INT3_INSN_SIZE + CALL_INSN_SIZE); int3_emulate_jmp(regs, func); } + +static inline void int3_emulate_ret(struct pt_regs *regs) +{ + unsigned long ip = int3_emulate_pop(regs); + int3_emulate_jmp(regs, ip); +} #endif /* !CONFIG_UML_X86 */ #endif /* _ASM_X86_TEXT_PATCHING_H */ --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -1045,6 +1045,10 @@ int notrace poke_int3_handler(struct pt_ */ goto out_put; + case RET_INSN_OPCODE: + int3_emulate_ret(regs); + break; + case CALL_INSN_OPCODE: int3_emulate_call(regs, (long)ip + tp->rel32); break; @@ -1187,6 +1191,7 @@ void text_poke_loc_init(struct text_poke switch (tp->opcode) { case INT3_INSN_OPCODE: + case RET_INSN_OPCODE: break; case CALL_INSN_OPCODE: