Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp1530711rdb; Wed, 20 Sep 2023 11:40:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHEXNcsVRZ4bcru07KUfdjeDcBWuK38K1rUPsrVoTEBZScgFhghadEqG4HlTPqpivZIyMHy X-Received: by 2002:a05:6a00:1d87:b0:68f:c2f7:cef0 with SMTP id z7-20020a056a001d8700b0068fc2f7cef0mr8286077pfw.4.1695235240342; Wed, 20 Sep 2023 11:40:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695235240; cv=none; d=google.com; s=arc-20160816; b=SoPDmdNwWbEMJK7LHTpx76leJMTsgnK3rMpOOKvbvFJuaIcL5Z+0tFSqAsdUlSwXfa e9QV4y66MfD1EFZXtxnWtzz4tyyvy31OkvmtB3B9SHEgx2flm2a/qNiPeGLdIi/dtVm7 fOAKj1EK6Kee1I8rcdAshq9GLoI6kNgvfs+or+diENn1hFrB+RfJyzySEEQif19fCLdK 1G4X+1gRp/p13O9O0upMfKUhBm64jmFJl2RF5YToENp3pIO96fSjSvFuw4xGaFX2T+GE osJ57woIvYDdhnTzda5BtpO4B19nV+FflTbFNpHRd3/E8KNCYxu7DFifK5+4+i+QEyj1 Xx0Q== 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=4hBA0mBFz86EuPBzuRyC6ZCKI5h6ySdd8K4qpgeL0oM=; fh=EK6W8cYnL56l9f6bRGJbKyUgCbPNRYHol3Lab4V6quk=; b=x9SyzGvgikGYJ+0EjC/cALblWVvCy6NWNv8vBQzx2ssZ78HQ170I2i873YHNRhLvpe XKgciaQ2+RGisdIsLMv8rUAYQ1jpVksT3CkK6sNNJC5Dkbr5L4GBluAzD44tma6GObCm HJAcpwGwX/O7Pv4ctNePE56NrPlTWc6DzPdBpts1EigfjCdC/gfxrbsNfFA0EK/Ml75B 2cD8ocfLr+hpn2MThS5l45ATSeub1JBb689yp98KmG9ilqu5zLcalzXP1gllCY3+FE1k s8DkdbiTC6LPfarlvHuJ81y/sNgCKBWnCN0DV2pKuqivuWWrPnjIBQj6hsSdWRSs5Ohs UWJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b="Jsv7w/BZ"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id u20-20020a056a00125400b006901504b6a3si12582642pfi.153.2023.09.20.11.40.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Sep 2023 11:40:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b="Jsv7w/BZ"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 837328226F58; Wed, 20 Sep 2023 07:46:50 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236480AbjITOps (ORCPT + 99 others); Wed, 20 Sep 2023 10:45:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236835AbjITOpJ (ORCPT ); Wed, 20 Sep 2023 10:45:09 -0400 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87840C6 for ; Wed, 20 Sep 2023 07:45:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; 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=4hBA0mBFz86EuPBzuRyC6ZCKI5h6ySdd8K4qpgeL0oM=; b=Jsv7w/BZMh+zkv/M4Ojx8yDNnZ Z6rZzuzcqV9vBeGcpupMFe4Yg7c34Dvkk9NY7HugdGXloG8jpgcsxjBj449PLlFWWfqFIDxpw5F8d DGU7GgKsk5pEwwGXqIevo+moJCeGwFLsfHPaOSf1atcOjN6+YRWg9NWvqBA1gvMM2K/WAvPzglpt2 w34nqJi9OBXzG1MbMlFiCc6XV+6OiBOaWQR0asifD9ycEhqxAlQP6JAj/lmaXwMWo8D0xSiSoIeNc +rjoRppbtLbusWfeA/M5HHtyJg0ODokI8yGkWERlBZ3BxIIVIpe/BujHHPph75zZABA4/bS+qpf6x re1627Dw==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1qiyRK-00EfRD-2z; Wed, 20 Sep 2023 14:44:32 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id 09E03300513; Wed, 20 Sep 2023 16:44:32 +0200 (CEST) Date: Wed, 20 Sep 2023 16:44:31 +0200 From: Peter Zijlstra To: Juergen Gross Cc: linux-kernel@vger.kernel.org, x86@kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: Re: [RFC PATCH 2/3] x86/alternative: add indirect call patching Message-ID: <20230920144431.GA6687@noisy.programming.kicks-ass.net> References: <20230608140333.4083-1-jgross@suse.com> <20230608140333.4083-3-jgross@suse.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230608140333.4083-3-jgross@suse.com> X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 20 Sep 2023 07:46:50 -0700 (PDT) On Thu, Jun 08, 2023 at 04:03:32PM +0200, Juergen Gross wrote: > In order to prepare replacing of paravirt patching with alternative > patching, add the capability to replace an indirect call with a direct > one to alternative patching. > > This is done via a new flag ALT_FLAG_CALL as the target of the call > instruction needs to be evaluated using the value of the location > addressed by the indirect call. For convenience add a macro for a > default call instruction. In case it is being used without the new > flag being set, it will result in a BUG() when being executed. As in > most cases the feature used will be X86_FEATURE_ALWAYS add another > macro ALT_CALL_ALWAYS usable for the flags parameter of the ALTERNATIVE > macros. > > For a complete replacement handle the special cases of calling a nop > function and an indirect call of NULL the same way as paravirt does. > > Signed-off-by: Juergen Gross Since I wrecked all that with 270a69c4485d ("x86/alternative: Support relocations in alternatives") I wrote you a replacement :-) --- Index: linux-2.6/arch/x86/include/asm/alternative.h =================================================================== --- linux-2.6.orig/arch/x86/include/asm/alternative.h +++ linux-2.6/arch/x86/include/asm/alternative.h @@ -10,6 +10,9 @@ #define ALT_FLAG_NOT (1 << 0) #define ALT_NOT(feature) ((ALT_FLAG_NOT << ALT_FLAGS_SHIFT) | (feature)) +#define ALT_FLAG_CALL (1 << 1) +#define ALT_CALL(feature) ((ALT_FLAG_CALL << ALT_FLAGS_SHIFT) | (feature)) +#define ALT_CALL_ALWAYS ALT_CALL(X86_FEATURE_ALWAYS) #ifndef __ASSEMBLY__ @@ -150,6 +153,8 @@ static inline int alternatives_text_rese } #endif /* CONFIG_SMP */ +#define ALT_CALL_INSTR "call x86_BUG" + #define b_replacement(num) "664"#num #define e_replacement(num) "665"#num Index: linux-2.6/arch/x86/kernel/alternative.c =================================================================== --- linux-2.6.orig/arch/x86/kernel/alternative.c +++ linux-2.6/arch/x86/kernel/alternative.c @@ -396,6 +396,40 @@ noinstr void x86_BUG(void) EXPORT_SYMBOL_GPL(x86_BUG); /* + * Rewrite the "call x86_BUG" replacement to point to the target of the + * indirect pv_ops call "call *disp(%ip)". + */ +static int alt_replace_call(u8 *instr, u8 *insn_buff, struct alt_instr *a) +{ + void *target, *bug = &x86_BUG; + + if (a->replacementlen != 5 || insn_buff[0] != CALL_INSN_OPCODE) { + pr_err("Alternative: ALT_FLAG_CALL set for a non-call replacement instruction\n"); + pr_err(" Ignoring the flag for the instruction at %pS (%px)\n", instr, instr); + return 5; + } + + if (a->instrlen != 6 || instr[0] != 0xff || instr[1] != 0x15) { + pr_err("Alternative: ALT_FLAG_CALL set for unrecognized indirect call\n"); + pr_err(" Not replacing the instruction at %pS (%px)\n", instr, instr); + return -1; + } + + /* ff 15 00 00 00 00 call *0x0(%rip) */ + target = *(void **)(instr + a->instrlen + *(s32 *)(instr + 2)); + if (!target) + target = bug; + + /* (x86_BUG - .) + (target - x86_BUG) := target - . */ + *(s32 *)(insn_buff + 1) += target - bug; + + if (target == &x86_nop) + return 0; + + return 5; +} + +/* * Replace instructions with better alternatives for this CPU type. This runs * before SMP is initialized to avoid SMP problems with self modifying code. * This implies that asymmetric systems where APs have less capabilities than @@ -451,6 +485,12 @@ void __init_or_module noinline apply_alt memcpy(insn_buff, replacement, a->replacementlen); insn_buff_sz = a->replacementlen; + if (a->flags & ALT_FLAG_CALL) { + insn_buff_sz = alt_replace_call(instr, insn_buff, a); + if (insn_buff_sz < 0) + continue; + } + for (; insn_buff_sz < a->instrlen; insn_buff_sz++) insn_buff[insn_buff_sz] = 0x90;