Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3152imw; Tue, 12 Jul 2022 13:19:58 -0700 (PDT) X-Google-Smtp-Source: AGRyM1s7wQ0i6Wus7TysKWWF0cond48i+p2fUMebQ+sx6GU6kHLRXwcgBaVraBjl6/X1G20axGJI X-Received: by 2002:a17:90a:a404:b0:1ee:e545:288b with SMTP id y4-20020a17090aa40400b001eee545288bmr19340pjp.142.1657657197824; Tue, 12 Jul 2022 13:19:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657657197; cv=none; d=google.com; s=arc-20160816; b=tf7gJYFAd3GA/zB5sIx3A43UlwUbLwLmedfR1cJ3tzD0HXvU6y6XXiuSViT1ePh6RL +q74dSHig0I/tg4eUuSKeKhfp/jIvIKEf1UzN6bZP+88A3oiWmbtuxs4bf4JGYN2A711 rIT29tUleWQT5lx9cbHPq4joXo2Nyip2JZgALo4Jw5HzH121Hu7QfhgGrlL/wS1LiHRD wDSUo051iuYWl992Z95oUECO/B1us1F2RcFTgmDPkKnR3lnwSmSwpbSVeXVHhJh35Q4J 6KEoy5qPlJzJHWE7Flph6wcpw6n9U08kE4q8R0Rchn9+sX/hshuhVgGWBN18sCk5Hk8R /oBA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=9QZ67pslZTA1SlwQfza+vlbvBB9LmOtTcXT+uZiG09w=; b=z2nkSnwk+lD9ad0kpaPuCvJpL5pAy6Q2R6ScIpXSwLoeu/5pGz8XR9ZZ856Kk2YM7i xuuxibQFlZLZaHgtcttK8xcHwGVa88MeBPbsJLuMh5UaD+bqp+vCh+514XDL7soOjByS uzLWdEZ2l7tajyS7eH97V3XcANmNHCrDpJinl+glASV/0M3tYvkhSLQTFeyynnZeP3TY PVXqp0g6Sg9lRXwvjMtUHFYDcohR+y7G9TF97wBme4dDj8rOKmikmQ9Zxoy+q7JUe+oK 3RkSXB8XaNPr37IAx7cowduqmU/BkUIFYRmaFxRlVB1/QNT9Av2wN4m+aZmYHZguf224 PDQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=RXBUY6Ut; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bg8-20020a1709028e8800b0016c281b1de3si12579713plb.501.2022.07.12.13.19.45; Tue, 12 Jul 2022 13:19:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=RXBUY6Ut; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231243AbiGLTIt (ORCPT + 99 others); Tue, 12 Jul 2022 15:08:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235221AbiGLTIH (ORCPT ); Tue, 12 Jul 2022 15:08:07 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BD2BE52B8; Tue, 12 Jul 2022 11:51:45 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id CD91361123; Tue, 12 Jul 2022 18:51:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 98008C3411C; Tue, 12 Jul 2022 18:51:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1657651901; bh=b6ihENthbUbQ5lJ4c2//Yhi5Ha1tezAvuDGyRHTe7Vk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RXBUY6UtgfCG4aXt6dlogIDmtJk/UkXwaFMgFlomhlrwXaPE0cjhnNePnvqsx+5p0 mt9jfkBahVzu6RklY2x0QhoAlFEGKYfJeRLFpBwrljc738Z2XIli3h+Oo3h+hRiI4Q 2mrMmDWx9plA99AfsoOx2khzVC0Phg8k/tNh7rnY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Peter Zijlstra (Intel)" , Borislav Petkov , Josh Poimboeuf , Thadeu Lima de Souza Cascardo Subject: [PATCH 5.18 17/61] x86/kvm: Fix SETcc emulation for return thunks Date: Tue, 12 Jul 2022 20:39:14 +0200 Message-Id: <20220712183237.631825759@linuxfoundation.org> X-Mailer: git-send-email 2.37.0 In-Reply-To: <20220712183236.931648980@linuxfoundation.org> References: <20220712183236.931648980@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Peter Zijlstra commit af2e140f34208a5dfb6b7a8ad2d56bda88f0524d upstream. Prepare the SETcc fastop stuff for when RET can be larger still. The tricky bit here is that the expressions should not only be constant C expressions, but also absolute GAS expressions. This means no ?: and 'true' is ~0. Also ensure em_setcc() has the same alignment as the actual FOP_SETCC() ops, this ensures there cannot be an alignment hole between em_setcc() and the first op. Additionally, add a .skip directive to the FOP_SETCC() macro to fill any remaining space with INT3 traps; however the primary purpose of this directive is to generate AS warnings when the remaining space goes negative. Which is a very good indication the alignment magic went side-ways. Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Borislav Petkov Reviewed-by: Josh Poimboeuf Signed-off-by: Borislav Petkov Signed-off-by: Thadeu Lima de Souza Cascardo Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/emulate.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -325,13 +325,15 @@ static int fastop(struct x86_emulate_ctx #define FOP_RET(name) \ __FOP_RET(#name) -#define FOP_START(op) \ +#define __FOP_START(op, align) \ extern void em_##op(struct fastop *fake); \ asm(".pushsection .text, \"ax\" \n\t" \ ".global em_" #op " \n\t" \ - ".align " __stringify(FASTOP_SIZE) " \n\t" \ + ".align " __stringify(align) " \n\t" \ "em_" #op ":\n\t" +#define FOP_START(op) __FOP_START(op, FASTOP_SIZE) + #define FOP_END \ ".popsection") @@ -435,16 +437,15 @@ static int fastop(struct x86_emulate_ctx /* * Depending on .config the SETcc functions look like: * - * ENDBR [4 bytes; CONFIG_X86_KERNEL_IBT] - * SETcc %al [3 bytes] - * RET [1 byte] - * INT3 [1 byte; CONFIG_SLS] - * - * Which gives possible sizes 4, 5, 8 or 9. When rounded up to the - * next power-of-two alignment they become 4, 8 or 16 resp. + * ENDBR [4 bytes; CONFIG_X86_KERNEL_IBT] + * SETcc %al [3 bytes] + * RET | JMP __x86_return_thunk [1,5 bytes; CONFIG_RETPOLINE] + * INT3 [1 byte; CONFIG_SLS] */ -#define SETCC_LENGTH (ENDBR_INSN_SIZE + 4 + IS_ENABLED(CONFIG_SLS)) -#define SETCC_ALIGN (4 << IS_ENABLED(CONFIG_SLS) << HAS_KERNEL_IBT) +#define RET_LENGTH (1 + (4 * IS_ENABLED(CONFIG_RETPOLINE)) + \ + IS_ENABLED(CONFIG_SLS)) +#define SETCC_LENGTH (ENDBR_INSN_SIZE + 3 + RET_LENGTH) +#define SETCC_ALIGN (4 << ((SETCC_LENGTH > 4) & 1) << ((SETCC_LENGTH > 8) & 1)) static_assert(SETCC_LENGTH <= SETCC_ALIGN); #define FOP_SETCC(op) \ @@ -453,9 +454,10 @@ static_assert(SETCC_LENGTH <= SETCC_ALIG #op ": \n\t" \ ASM_ENDBR \ #op " %al \n\t" \ - __FOP_RET(#op) + __FOP_RET(#op) \ + ".skip " __stringify(SETCC_ALIGN) " - (.-" #op "), 0xcc \n\t" -FOP_START(setcc) +__FOP_START(setcc, SETCC_ALIGN) FOP_SETCC(seto) FOP_SETCC(setno) FOP_SETCC(setc)