From nobody Mon Feb 9 18:18:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1713809706; cv=none; d=zohomail.com; s=zohoarc; b=LbGhcagZQI1dki2yuVRyBbx/azbN9m7ekjHCzX/SE8/FrU2HEKV23EWhB2pTi70VbiMnaKI7go+quzkDgVCUlgovk15POshFEIOfaMBN8ubf7BVDdM6DYuBcjuMynDfdkZaukxWC06ug4c4HsCG1wR8b5rAwYKLkN7sl0EJjHC4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1713809706; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=31yUgd5YK+pN4dRkn1LMnihd6GkdbjUm1b+8i5iacEg=; b=NazNr895FMYT7pSSBVp1mJ+la2vAFuU+2JzcgMKQdrjG3FwI57i2U/BbXbSemtF8z9QazuGwbRGSW2MKM8kNccoLd65Q0plIPtVz/TF+QRcB0noBF+0/0BA77b7ey/kLVXi1PhIrfF0EgXWF1kWHvK9UfHYxpIyHo2pAqMjMnrI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1713809706779108.91001885411515; Mon, 22 Apr 2024 11:15:06 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.710192.1109306 (Exim 4.92) (envelope-from ) id 1ryyBe-0000cG-Kx; Mon, 22 Apr 2024 18:14:42 +0000 Received: by outflank-mailman (output) from mailman id 710192.1109306; Mon, 22 Apr 2024 18:14:42 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ryyBe-0000bt-E3; Mon, 22 Apr 2024 18:14:42 +0000 Received: by outflank-mailman (input) for mailman id 710192; Mon, 22 Apr 2024 18:14:41 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ryyBd-0000IX-6N for xen-devel@lists.xenproject.org; Mon, 22 Apr 2024 18:14:41 +0000 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [2a00:1450:4864:20::636]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 2f64d62a-00d4-11ef-909a-e314d9c70b13; Mon, 22 Apr 2024 20:14:40 +0200 (CEST) Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-a5872b74c44so46930866b.3 for ; Mon, 22 Apr 2024 11:14:40 -0700 (PDT) Received: from andrewcoop.citrite.net (default-46-102-197-194.interdsl.co.uk. [46.102.197.194]) by smtp.gmail.com with ESMTPSA id z15-20020a170906668f00b00a51d073da7esm5994224ejo.82.2024.04.22.11.14.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 11:14:38 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 2f64d62a-00d4-11ef-909a-e314d9c70b13 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1713809679; x=1714414479; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=31yUgd5YK+pN4dRkn1LMnihd6GkdbjUm1b+8i5iacEg=; b=R8PB8SYRh8z3v+Cn85Jjbb74Z10LovtcXj6ymwCs2nfNJZki8WLNxy/auZMP+8np1g 3UrKFthyTFQU4JYfs7JkbUYkNsTBzzy5D5aqEAA08QulrinS90Bqd6DckYwBhlL4jigh Lsn50J/FNrHi9cBv5RTpwIxvV3ENJ71hnw6ZU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713809679; x=1714414479; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=31yUgd5YK+pN4dRkn1LMnihd6GkdbjUm1b+8i5iacEg=; b=oM2Zm/nKzwWEWmg9mARcP99h3lv4HWNNCHi+Lpv2rkSFjPysZSzcb+ewU0WosV8jwn SuISFefjdc9tvZcGx/wdq26bNpM2Kf8JgMDpM+3Z0HvIAqSZ1Y/vTBrjKguUMZsTC2Ic 0iwV+POn0begZM5WI25cr7kuYyXCAxblY9syDpjiYuR8yzjxjdS1Ozpwbmr9ReeSEdH1 aXtukBXWyvvBV/yPaMF724KuQQD1YBsJ7T4XLHf5V/nO+k4qG7EHoAWhnrCOmCiZ8PQ1 1xMtvJ6b6QVQ+AbcOyz5zbLJeDRzDkbI9uoE6stycf4117DYukwDqXwP0Xpa2Z26VPle /IOQ== X-Gm-Message-State: AOJu0YzZk8fI+mWgpVG6674mu7AFPXHXoiIimJ6kfcmp1FtJlpUvbYVz VtA7kzFu3+SZAn6BnPyOxjWO4BBYhUZfXkvQU8N78rGZi5p3Kn3KmhlTJGYbRtHFz+QJW78JYcA 1Q2E= X-Google-Smtp-Source: AGHT+IG+1HMvF1SwFEh23Uw5BDoD1AN36ExhwVuxXRvPOWRV9QQwPpCghdo+Q9ydNqUU6Qfd296HkA== X-Received: by 2002:a17:906:7708:b0:a51:969e:ba0 with SMTP id q8-20020a170906770800b00a51969e0ba0mr6257278ejm.44.1713809679358; Mon, 22 Apr 2024 11:14:39 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Subject: [PATCH 3/6] x86/alternative: Intend the relocation logic Date: Mon, 22 Apr 2024 19:14:31 +0100 Message-Id: <20240422181434.3463252-4-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240422181434.3463252-1-andrew.cooper3@citrix.com> References: <20240422181434.3463252-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1713809708526100001 ... to make subsequent patches legible. No functional change. Signed-off-by: Andrew Cooper Acked-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 --- xen/arch/x86/alternative.c | 126 +++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 62 deletions(-) diff --git a/xen/arch/x86/alternative.c b/xen/arch/x86/alternative.c index 5bd256365def..2ca4dfd569bc 100644 --- a/xen/arch/x86/alternative.c +++ b/xen/arch/x86/alternative.c @@ -244,78 +244,80 @@ static void init_or_livepatch _apply_alternatives(str= uct alt_instr *start, =20 memcpy(buf, repl, a->repl_len); =20 - /* 0xe8/0xe9 are relative branches; fix the offset. */ - if ( a->repl_len >=3D 5 && (*buf & 0xfe) =3D=3D 0xe8 ) { - /* - * Detect the special case of indirect-to-direct branch patchi= ng: - * - replacement is a direct CALL/JMP (opcodes 0xE8/0xE9; alre= ady - * checked above), - * - replacement's displacement is -5 (pointing back at the ve= ry - * insn, which makes no sense in a real replacement insn), - * - original is an indirect CALL/JMP (opcodes 0xFF/2 or 0xFF/= 4) - * using RIP-relative addressing. - * Some branch destinations may still be NULL when we come here - * the first time. Defer patching of those until the post-pres= mp- - * initcalls re-invocation (with force set to true). If at that - * point the branch destination is still NULL, insert "UD2; UD= 0" - * (for ease of recognition) instead of CALL/JMP. - */ - if ( a->cpuid =3D=3D X86_FEATURE_ALWAYS && - *(int32_t *)(buf + 1) =3D=3D -5 && - a->orig_len >=3D 6 && - orig[0] =3D=3D 0xff && - orig[1] =3D=3D (*buf & 1 ? 0x25 : 0x15) ) + /* 0xe8/0xe9 are relative branches; fix the offset. */ + if ( a->repl_len >=3D 5 && (*buf & 0xfe) =3D=3D 0xe8 ) { - long disp =3D *(int32_t *)(orig + 2); - const uint8_t *dest =3D *(void **)(orig + 6 + disp); - - if ( dest ) + /* + * Detect the special case of indirect-to-direct branch pa= tching: + * - replacement is a direct CALL/JMP (opcodes 0xE8/0xE9; = already + * checked above), + * - replacement's displacement is -5 (pointing back at th= e very + * insn, which makes no sense in a real replacement insn= ), + * - original is an indirect CALL/JMP (opcodes 0xFF/2 or 0= xFF/4) + * using RIP-relative addressing. + * Some branch destinations may still be NULL when we come= here + * the first time. Defer patching of those until the post-= presmp- + * initcalls re-invocation (with force set to true). If at= that + * point the branch destination is still NULL, insert "UD2= ; UD0" + * (for ease of recognition) instead of CALL/JMP. + */ + if ( a->cpuid =3D=3D X86_FEATURE_ALWAYS && + *(int32_t *)(buf + 1) =3D=3D -5 && + a->orig_len >=3D 6 && + orig[0] =3D=3D 0xff && + orig[1] =3D=3D (*buf & 1 ? 0x25 : 0x15) ) { - /* - * When building for CET-IBT, all function pointer tar= gets - * should have an endbr64 instruction. - * - * If this is not the case, leave a warning because - * something is probably wrong with the build. A CET-= IBT - * enabled system might have exploded already. - * - * Otherwise, skip the endbr64 instruction. This is a - * marginal perf improvement which saves on instruction - * decode bandwidth. - */ - if ( IS_ENABLED(CONFIG_XEN_IBT) ) + long disp =3D *(int32_t *)(orig + 2); + const uint8_t *dest =3D *(void **)(orig + 6 + disp); + + if ( dest ) { - if ( is_endbr64(dest) ) - dest +=3D ENDBR64_LEN; - else - printk(XENLOG_WARNING - "altcall %ps dest %ps has no endbr64\n", - orig, dest); + /* + * When building for CET-IBT, all function pointer= targets + * should have an endbr64 instruction. + * + * If this is not the case, leave a warning because + * something is probably wrong with the build. A = CET-IBT + * enabled system might have exploded already. + * + * Otherwise, skip the endbr64 instruction. This = is a + * marginal perf improvement which saves on instru= ction + * decode bandwidth. + */ + if ( IS_ENABLED(CONFIG_XEN_IBT) ) + { + if ( is_endbr64(dest) ) + dest +=3D ENDBR64_LEN; + else + printk(XENLOG_WARNING + "altcall %ps dest %ps has no endbr6= 4\n", + orig, dest); + } + + disp =3D dest - (orig + 5); + ASSERT(disp =3D=3D (int32_t)disp); + *(int32_t *)(buf + 1) =3D disp; } - - disp =3D dest - (orig + 5); - ASSERT(disp =3D=3D (int32_t)disp); - *(int32_t *)(buf + 1) =3D disp; - } - else if ( force ) - { - buf[0] =3D 0x0f; - buf[1] =3D 0x0b; - buf[2] =3D 0x0f; - buf[3] =3D 0xff; - buf[4] =3D 0xff; + else if ( force ) + { + buf[0] =3D 0x0f; + buf[1] =3D 0x0b; + buf[2] =3D 0x0f; + buf[3] =3D 0xff; + buf[4] =3D 0xff; + } + else + continue; } + else if ( force && system_state < SYS_STATE_active ) + ASSERT_UNREACHABLE(); else - continue; + *(int32_t *)(buf + 1) +=3D repl - orig; } - else if ( force && system_state < SYS_STATE_active ) + else if ( force && system_state < SYS_STATE_active ) ASSERT_UNREACHABLE(); - else - *(int32_t *)(buf + 1) +=3D repl - orig; } - else if ( force && system_state < SYS_STATE_active ) - ASSERT_UNREACHABLE(); =20 a->priv =3D 1; =20 --=20 2.30.2