Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2468055rdg; Mon, 16 Oct 2023 05:41:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH11pg4BHt6/FtOrVmVSVF1Jl24CdzNriG3cI8pQ9/H/uEpEPpUar6Ey0qg2zOFPH3zEL16 X-Received: by 2002:a05:6a00:9aa:b0:690:f877:aa1e with SMTP id u42-20020a056a0009aa00b00690f877aa1emr12677317pfg.12.1697460085934; Mon, 16 Oct 2023 05:41:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697460085; cv=none; d=google.com; s=arc-20160816; b=IyIb3tos2GBOyUw+6c5E7XJt7gX0lQ1UAq/Nfoo1PWD36e5MvTG9ufYYoW4RBzp8oE PUnXM1DCIiej/aygS4WKmRYEGNIKnbGRM0zvPCG2taXtEBWeHpiPTisTavas7hqklJB5 dWQVBDSj6dx1Fo4Au85L+KNS1ag2GZUz8fK9AgstFMXp8OpgnEiwf06MBmtiB5cTtkzS Ze9kj1ox0WOlnEf223LGl2FA072QMo4Hywo4p9DpFdVWxwRs2/4Ns2Ja25ALSLJllLPx 0cNsmtNg5MCAMc26WdG3MnY5zdd0Yshce093oHN4qcwAuvSYoYVkNrCR//UlGFNqbqnu 7KTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4Dn2km2xrB+B5vKWOSjXaC3oIb3VD9PnFa2PlRKN8Us=; fh=K5ySZDV/TdCnC1wjLaFJQh766XBNuyvNPaLGSM9+pco=; b=Ga11bjs7nVXX0aHi3JpwUBumPMvCRJClHZVpJoXzEkS3OrlbYfR17E18qir3oec6Bv zBJywOD0MQGxw/o5gAnEkeWewgsHFecSGd6yOwQ7pDNQaq4u10NuVcAMhg/ytVI0UGq/ MW/1dPyYG3Get95qNrDIL32aSRB/fhZA9B5uihpEJAq+USfe6c9TdN4h3TO7A/TtrqH9 vr1uiZ6W7nu5eaq1PRQcb5aWAh3g+yqmvoph48aqPNpCa0sYkSJ+s3TUi9njEKdBH7/g vJylfCIbBgSaRv+PhaIM6rsSk2uJQzX884EQnAQnFS/I5UZ2TlfWB5bm7ypc1wAqef63 6Qfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=M7Ov8DqI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id f38-20020a635566000000b005a0193c9328si10153618pgm.839.2023.10.16.05.41.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 05:41:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=M7Ov8DqI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 1F6C2804C6C1; Mon, 16 Oct 2023 05:40:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233212AbjJPMjy (ORCPT + 99 others); Mon, 16 Oct 2023 08:39:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233276AbjJPMjv (ORCPT ); Mon, 16 Oct 2023 08:39:51 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FB1BB4 for ; Mon, 16 Oct 2023 05:39:49 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 4DD4421958; Mon, 16 Oct 2023 12:39:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1697459987; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4Dn2km2xrB+B5vKWOSjXaC3oIb3VD9PnFa2PlRKN8Us=; b=M7Ov8DqINADiD52TRl8/PYXfFhhBpU4i2WSWbMKAH+IiBF5C4W3TJ3t+AQ9ShNRFnVpH1P iurcbVMyuY847AEFOrrEtHbm9FtBGujgkm8gkLAEiyauSvnCMh4SZqUwh+focrVE6ewdKc 0vkAnN76iDyYl7PzC6MCHWxPpVqjHgo= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 0A8E5133B7; Mon, 16 Oct 2023 12:39:47 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id O5JZARMvLWUbIwAAMHmgww (envelope-from ); Mon, 16 Oct 2023 12:39:47 +0000 From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Juergen Gross , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Peter Zijlstra Subject: [PATCH v2 2/4] x86/alternative: add indirect call patching Date: Mon, 16 Oct 2023 14:39:31 +0200 Message-Id: <20231016123933.17284-3-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231016123933.17284-1-jgross@suse.com> References: <20231016123933.17284-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Authentication-Results: smtp-out1.suse.de; none X-Spam-Level: X-Spam-Score: -0.10 X-Spamd-Result: default: False [-0.10 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; R_MISSING_CHARSET(2.50)[]; MIME_GOOD(-0.10)[text/plain]; REPLY(-4.00)[]; BROKEN_CONTENT_TYPE(1.50)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; NEURAL_HAM_SHORT(-1.00)[-0.999]; NEURAL_SPAM_LONG(3.00)[1.000]; RCPT_COUNT_SEVEN(0.00)[9]; MID_CONTAINS_FROM(1.00)[]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Status: No, score=-0.8 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 howler.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 (howler.vger.email [0.0.0.0]); Mon, 16 Oct 2023 05:40:21 -0700 (PDT) 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 Signed-off-by: Peter Zijlstra (Intel) --- arch/x86/include/asm/alternative.h | 5 ++++ arch/x86/kernel/alternative.c | 40 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/arch/x86/include/asm/alternative.h b/arch/x86/include/asm/alternative.h index 67e50bd40bb8..dd63b96577e9 100644 --- a/arch/x86/include/asm/alternative.h +++ b/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_reserved(void *start, void *end) } #endif /* CONFIG_SMP */ +#define ALT_CALL_INSTR "call x86_BUG" + #define b_replacement(num) "664"#num #define e_replacement(num) "665"#num diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 1c8dd8e05f3f..01b89a10d219 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -395,6 +395,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. @@ -462,6 +496,12 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start, 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; -- 2.35.3