Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp4698792imw; Tue, 12 Jul 2022 12:39:29 -0700 (PDT) X-Google-Smtp-Source: AGRyM1seSfAdZOMA4Hhz2QUUlTGLmSGQfUFiM/V5dcoKeZ/+GZdjdvty7b0pyBqN3ZrIWZpwIn2s X-Received: by 2002:a17:907:1b03:b0:6ff:78d4:c140 with SMTP id mp3-20020a1709071b0300b006ff78d4c140mr26328938ejc.554.1657654768824; Tue, 12 Jul 2022 12:39:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657654768; cv=none; d=google.com; s=arc-20160816; b=QUIe1MSnrchUjIXYygkn0hlXVL86lQx1WPHpHpciGp4xCMp9WsSs48zhlwcaC0UJ2R SxI9WhXtVLZgFEh38fZ24xAJmsbtiRJORPLpjOTidH8+9uf4kUO3ubHNIwoCufXm7ore FEQvhXvImWvk/lYgy4JNs0kiMF+o3JfFUq0VFtjcBUpzG7M7ngwyNrQ9n2dNqXdgaGpU 2j8nSZCB0DNfVWwXvKMYyLi6Q4lAWOn4wTuxYRRVqkblssOQtR70GIKduCV8ojqDx3d4 j0h9JW4yUZyBaQGlqYh4+QX5bAUNmREZixbE50AWih40G4uH/L9keLM14O1p/rwD4sts OwnQ== 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=y/ah+ATGZIlfUjYp+TaM96FgoI9Rqi1rd+xq7+CCgC0=; b=Y8EdOk/JItHD72iY+xxcA/G+nMMM520sbfzEb/2Oi9euPnZ5IvYOesAYQ6fW8zUSca 0GHd6JhyKT82xNP1Dad8sHheZZLwGgONVLWjYOcg+s3QzNQyxfbBzfMnmhhGQ32ebHtv oxEj90vA10V3DA+6Vv3c8dmBJ915hwNkWqTGUudX5202l1wYt1yT+u1P8S0mHXKIA+Y/ NG9n+6QDIp3RAPke5WRkvg3nVCJgTy7JsLqNPvSZY7Y79E8qiiGHm/uKbJ4xh6Dx4Up6 Kr2Zpj1umN2GdjjPmTZBy4niEfzR2J+Lo2tH3NmHIc1cdLLiZ0dmuj6kUS2BP5HzsvBq TYgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=brW77b1Z; 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 x19-20020a50d613000000b0043a23aed655si15380920edi.386.2022.07.12.12.39.04; Tue, 12 Jul 2022 12:39:28 -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=brW77b1Z; 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 S234413AbiGLSwE (ORCPT + 99 others); Tue, 12 Jul 2022 14:52:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234134AbiGLSve (ORCPT ); Tue, 12 Jul 2022 14:51:34 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD673E5850; Tue, 12 Jul 2022 11:44: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 ams.source.kernel.org (Postfix) with ESMTPS id D4D43B81BBB; Tue, 12 Jul 2022 18:44:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2AF02C3411C; Tue, 12 Jul 2022 18:44:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1657651482; bh=9U0r2uHXT1NBk+EpHeWfpqK2tFsMJfPQcA+s6ZHAngc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=brW77b1Z+6nrhan5QDaGSfeCUaHFNWF98k/xE6qZ3lavKfsF6nRpN27jA4xObjfTe q53QxEy8mul6Lg9eZkGoibAQH+51UFXYYdWmqFwDYuPvvd09ZbkfHs3OPnFlyRW/vU YGbBX4YEKTWLhhB8K4GCMdzY3M2sNKfVFiPUHOpI= 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 , Ben Hutchings Subject: [PATCH 5.10 090/130] x86/kvm: Fix SETcc emulation for return thunks Date: Tue, 12 Jul 2022 20:38:56 +0200 Message-Id: <20220712183250.610698458@linuxfoundation.org> X-Mailer: git-send-email 2.37.0 In-Reply-To: <20220712183246.394947160@linuxfoundation.org> References: <20220712183246.394947160@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 [cascardo: ignore ENDBR when computing SETCC_LENGTH] [cascardo: conflict fixup] Signed-off-by: Thadeu Lima de Souza Cascardo Signed-off-by: Ben Hutchings Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/emulate.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -322,13 +322,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") @@ -432,15 +434,14 @@ static int fastop(struct x86_emulate_ctx /* * Depending on .config the SETcc functions look like: * - * SETcc %al [3 bytes] - * RET [1 byte] - * INT3 [1 byte; CONFIG_SLS] - * - * Which gives possible sizes 4 or 5. When rounded up to the - * next power-of-two alignment they become 4 or 8. + * SETcc %al [3 bytes] + * RET | JMP __x86_return_thunk [1,5 bytes; CONFIG_RETPOLINE] + * INT3 [1 byte; CONFIG_SLS] */ -#define SETCC_LENGTH (4 + IS_ENABLED(CONFIG_SLS)) -#define SETCC_ALIGN (4 << IS_ENABLED(CONFIG_SLS)) +#define RET_LENGTH (1 + (4 * IS_ENABLED(CONFIG_RETPOLINE)) + \ + IS_ENABLED(CONFIG_SLS)) +#define SETCC_LENGTH (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) \ @@ -448,14 +449,15 @@ static_assert(SETCC_LENGTH <= SETCC_ALIG ".type " #op ", @function \n\t" \ #op ": \n\t" \ #op " %al \n\t" \ - __FOP_RET(#op) + __FOP_RET(#op) \ + ".skip " __stringify(SETCC_ALIGN) " - (.-" #op "), 0xcc \n\t" asm(".pushsection .fixup, \"ax\"\n" ".global kvm_fastop_exception \n" "kvm_fastop_exception: xor %esi, %esi; " ASM_RET ".popsection"); -FOP_START(setcc) +__FOP_START(setcc, SETCC_ALIGN) FOP_SETCC(seto) FOP_SETCC(setno) FOP_SETCC(setc)