From nobody Fri Nov 22 07:24:12 2024 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=1727882880; cv=none; d=zohomail.com; s=zohoarc; b=Q+jccXGxNfSBBsZ/1K2TKLCRgFe8Rt6ViqS+jUlgxZDthHxQFs3nPXynRhEGWMhL3wKbirN9hWgAbpFPn3S6CN0Do45AEqixxM4Ve/wRI3Hxj11m+Rd2aGEBe7DOQEKnDQjm5GrfVPmrH2xgtK2qydL320TT8B3/OhZ8LsC0qHA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1727882880; 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=/OYNqhpKRgZXD8+6+AhbIpT8QNeVjNGqQsR7u8ru59I=; b=G7k0q2NZ3CQe3xuA638N94RtFh7KzqujOj+YxJDQ5Y1VpUx7vVxXHgEkOSU7wUXz9ZwuWDW3iLQiod2ZgMJZbZ2YWwbpUEGptZj4jpDuz70QNwJzb1MHAVTwprvGmCWLAF4vGyqZlNkkzguoyVh+15g0SEX76UULf5GMOPSEIiI= 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 1727882880220945.5733740269502; Wed, 2 Oct 2024 08:28:00 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.808902.1220998 (Exim 4.92) (envelope-from ) id 1sw1GP-0008RA-K4; Wed, 02 Oct 2024 15:27:41 +0000 Received: by outflank-mailman (output) from mailman id 808902.1220998; Wed, 02 Oct 2024 15:27:41 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sw1GP-0008Pb-Dr; Wed, 02 Oct 2024 15:27:41 +0000 Received: by outflank-mailman (input) for mailman id 808902; Wed, 02 Oct 2024 15:27:40 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sw1GN-0007h7-Vx for xen-devel@lists.xenproject.org; Wed, 02 Oct 2024 15:27:39 +0000 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [2a00:1450:4864:20::62e]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id db2f3685-80d2-11ef-99a2-01e77a169b0f; Wed, 02 Oct 2024 17:27:38 +0200 (CEST) Received: by mail-ej1-x62e.google.com with SMTP id a640c23a62f3a-a7a843bef98so986424266b.2 for ; Wed, 02 Oct 2024 08:27:38 -0700 (PDT) Received: from andrewcoop.eng.citrite.net ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a93c299ac8esm880024566b.222.2024.10.02.08.27.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2024 08:27:36 -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: db2f3685-80d2-11ef-99a2-01e77a169b0f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1727882857; x=1728487657; 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=/OYNqhpKRgZXD8+6+AhbIpT8QNeVjNGqQsR7u8ru59I=; b=bgZ6vCv6CL3Yk4zQrlPy9O+4COEFiqNa2NUzpA3sziLKHuovyQmdlHA/8ge3XwFeQw SOs2o4DqyIXezOuCPbnD17FpHtV3J1iZzuxunOsCQNJLPQBbQ1/yDWy+P4f8L0eKCnNx 4wLCaaiSVI8uebUwmjj0nOCtrxAUJx+jNosuk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727882857; x=1728487657; 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=/OYNqhpKRgZXD8+6+AhbIpT8QNeVjNGqQsR7u8ru59I=; b=lyqkjF9C4IY0B5hxgilOpGRA3nCV5MkZWzvbOldh7PTWzLVe0BFvjtW/LijU78NKnH ugg0uJP/49KHp77yY75+h68x8n0aRSsAV23rodPSK6QMAd+NRWE+3o/IH6fP8YqmxFxP R0Xz6GKqALvfq8XHTcaX7YsmGtu/ZsiKVzjZ5pNMU+uoSpZxhGvaR8UAcix8YPbrrwN9 kjyhVyaGABxBjcuL5lvR62w3RAtBqxJGtqe33k0/vvf0/qRNTJWjv5Fw8f7Aih3W7C+H IfJ4Ry34huY15OYVP3jhKfDNN6DwvuQ9MhERN/d+LxSWl+RUd4XoJaCrkyWGSJNrV6tF peMw== X-Gm-Message-State: AOJu0YzR3Ujw5VrCSr8lX1RAS3R8Sy7OublKndq3DFW7k+IdsmH0xhkH zoIQkOD4uytE5E092WTHxA3FJhk+K93KEG/NzXBduznnZRkoNjytybv3Ni9fdbLgxgSunV6cvmp 0dDc= X-Google-Smtp-Source: AGHT+IGyps52t9eHsJjMQR/F6NQEncBxc2igwnQUXEn+2dI/dBTnAiVOxq0yisZwo57aulhs1Sa6DQ== X-Received: by 2002:a17:907:703:b0:a8b:6ee7:ba17 with SMTP id a640c23a62f3a-a98f83ff431mr349107466b.55.1727882856976; Wed, 02 Oct 2024 08:27:36 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Subject: [PATCH v2 3/7] x86/alternative: Walk all replacements during self tests Date: Wed, 2 Oct 2024 16:27:21 +0100 Message-Id: <20241002152725.1841575-4-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241002152725.1841575-1-andrew.cooper3@citrix.com> References: <20241002152725.1841575-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: 1727882880679116600 When self tests are active, walk all alternative replacements with x86_decode_lite(). This checks that we can decode all instructions, and also lets us check that disp8's don't leave the replacement block as such a case will definitely malfunction. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 v2: * Rebase over API changes in patch 1 * Use +%lu and drop casts * Swap to CONFIG_SELF_TESTS --- xen/arch/x86/alternative.c | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/xen/arch/x86/alternative.c b/xen/arch/x86/alternative.c index 1ba35cb9ede9..2dc759abd858 100644 --- a/xen/arch/x86/alternative.c +++ b/xen/arch/x86/alternative.c @@ -15,6 +15,7 @@ #include #include #include +#include #include =20 #define MAX_PATCH_LEN (255-1) @@ -490,6 +491,57 @@ static void __init _alternative_instructions(bool forc= e) void __init alternative_instructions(void) { arch_init_ideal_nops(); + + /* + * Walk all replacement instructions with x86_decode_lite(). This che= cks + * both that we can decode all instructions within the replacement, and + * that any near branch with a disp8 stays within the alternative itse= lf. + */ + if ( IS_ENABLED(CONFIG_SELF_TESTS) ) + { + struct alt_instr *a; + + for ( a =3D __alt_instructions; + a < __alt_instructions_end; ++a ) + { + void *repl =3D ALT_REPL_PTR(a); + void *ip =3D repl, *end =3D ip + a->repl_len; + + if ( !a->repl_len ) + continue; + + for ( x86_decode_lite_t res; ip < end; ip +=3D res.len ) + { + const int8_t *d8; + const void *target; + + res =3D x86_decode_lite(ip, end); + + if ( res.len =3D=3D 0 ) + { + printk("Alt for %ps [%*ph]\n", + ALT_ORIG_PTR(a), a->repl_len, repl); + panic(" Unable to decode instruction at +%lu in alter= native\n", + ip - repl); + } + + if ( res.rel_sz !=3D 1 ) + continue; + + d8 =3D res.rel; + target =3D ip + res.len + *d8; + + if ( target < repl || target > end ) + { + printk("Alt for %ps [%*ph]\n", + ALT_ORIG_PTR(a), a->repl_len, repl); + panic(" 'JMP/Jcc disp8' at +%lu leaves alternative bl= ock\n", + ip - repl); + } + } + } + } + _alternative_instructions(false); } =20 --=20 2.39.5