From nobody Thu Sep 19 01:32:56 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=cloud.com ARC-Seal: i=1; a=rsa-sha256; t=1719419354; cv=none; d=zohomail.com; s=zohoarc; b=I19+MtXuU0SUtwjNd+6wKZ2ejoNt7Ya3JQHKyEgun+JJezeCur5lQglcJOKqwsta/tnCjYg1kiYGMGRAmIeLjOarGt7wkgaM07LD4Fzgsjz782h0rmEEHaEaIuk7UyXRRVgtVF7Cul67+6korpIPvpp4YcWc5Jx70tDFoJuq2wQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1719419354; h=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=qRrrYvX+yDttN2Gg1qTkgVmajhbWR2NVsVybiYM7F5A=; b=DtyBrTK/1Pp6QQKEH6bYqnrPFBcWt+CScaoBHL0PYfw2YqGYS0PAr3UVC82YVeBOem4era2YGyEP0+yInPEe5TzhtcJ3usL2Npntz41+Jgln15HOlxpPAhslK8Fuj5CaJo8UMV26NI/FFiaD75xShlo+1D/Lxb9tVQVl/doRgic= 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 1719419354025344.8244874939568; Wed, 26 Jun 2024 09:29:14 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.749274.1157354 (Exim 4.92) (envelope-from ) id 1sMVVp-0007IS-AL; Wed, 26 Jun 2024 16:28:49 +0000 Received: by outflank-mailman (output) from mailman id 749274.1157354; Wed, 26 Jun 2024 16:28:49 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sMVVp-0007Gv-5l; Wed, 26 Jun 2024 16:28:49 +0000 Received: by outflank-mailman (input) for mailman id 749274; Wed, 26 Jun 2024 16:28:47 +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 1sMVVn-0005pK-QZ for xen-devel@lists.xenproject.org; Wed, 26 Jun 2024 16:28:47 +0000 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [2a00:1450:4864:20::536]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 28ede50e-33d9-11ef-b4bb-af5377834399; Wed, 26 Jun 2024 18:28:46 +0200 (CEST) Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-57d1782679fso678725a12.0 for ; Wed, 26 Jun 2024 09:28:46 -0700 (PDT) Received: from EMEAENGAAD19049.citrite.net ([160.101.139.1]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a7291af7912sm42791866b.128.2024.06.26.09.28.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jun 2024 09:28:44 -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: 28ede50e-33d9-11ef-b4bb-af5377834399 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1719419325; x=1720024125; 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=qRrrYvX+yDttN2Gg1qTkgVmajhbWR2NVsVybiYM7F5A=; b=DWQdEei9NajR8t828BZaK624uBUwS+bF4ZJsH+bScdVMgcVRu5WJ5PKXgUDRZYSw+W tU0KpEFvfBXemiGKGzVSFivApjOkk5I2XHaGPkAZJ/tt1oGQce+z0osWpUHLwjXL3G12 Rx3DtFin2FACzpN2mgqZYpOkXI8ifOND+TtVo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719419325; x=1720024125; 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=qRrrYvX+yDttN2Gg1qTkgVmajhbWR2NVsVybiYM7F5A=; b=SZ7EzqBXya2sGW/v4Crw8yA2dT4jj3wfdlSvAlYreAJB5a1y8ALjJC0Vsveih2lYai fHgxyIGlZnq1TSdvIyZcaeku7xTxw/8J3XbieMPzo2NdOGnfCF62ZjmflCTWbX3/PDBd gZWvsghivRPjFqfxaNq2GWNcUefCkKNxp+ud/iKdZHc14fiCmNRvKWuzoG0xg8kqlT68 fxvO2MpKV+P/ERVn96rAy7ffrgd8ay2Q+FjiequLs/ykUtY70L8TdW68+ojUHi8YoSvI DI6icUe0YyhBN7m1+BR9KYzEQVFIPg2a2OAJixLoDoVolAqjYlR7jfFrcqOmhkIBkoUV grEw== X-Gm-Message-State: AOJu0YyKFYzENZvewJXN2uUO88SDTDjdmKYPHXUdzfRYHlyV+Za7pak5 h6jfb+/IM2b245vxsEMwQXwJxlhE9BesIAQCG5D84IcMrQpe2beyygvpbH6IEoexmygUKoZYX6c NftY= X-Google-Smtp-Source: AGHT+IGIMJSmFVWpKQ8YVYg3rvYvGdWCd5kBCwEUCLXVW0MyhCXi56UMacCQ9aoBAV6VOB6qERmO2g== X-Received: by 2002:a17:907:c81b:b0:a72:4bf2:e16 with SMTP id a640c23a62f3a-a727f65e329mr261218066b.16.1719419325348; Wed, 26 Jun 2024 09:28:45 -0700 (PDT) From: Alejandro Vallejo To: Xen-devel Cc: Alejandro Vallejo , Anthony PERARD , Juergen Gross Subject: [PATCH v4 06/10] tools/libguest: Make setting MTRR registers unconditional Date: Wed, 26 Jun 2024 17:28:33 +0100 Message-Id: <2c55d486bb0c54a3e813abc66d32f321edd28b81.1719416329.git.alejandro.vallejo@cloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @cloud.com) X-ZM-MESSAGEID: 1719419354745100014 Content-Type: text/plain; charset="utf-8" This greatly simplifies a later patch that makes use of HVM contexts to upl= oad LAPIC data. The idea is to reuse MTRR setting procedure to avoid code duplication. It's currently only used for PVH, but there's no real reason to overcomplicate the toolstack preventing them being set for HVM too when hvmloader will override them anyway. While at it, add a missing "goto out" to what was the error condition in the loop. Signed-off-by: Alejandro Vallejo --- v4: * New patch --- tools/libs/guest/xg_dom_x86.c | 83 ++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/tools/libs/guest/xg_dom_x86.c b/tools/libs/guest/xg_dom_x86.c index cba01384ae75..82ea3e2aab0b 100644 --- a/tools/libs/guest/xg_dom_x86.c +++ b/tools/libs/guest/xg_dom_x86.c @@ -989,6 +989,7 @@ const static void *hvm_get_save_record(const void *ctx,= unsigned int type, =20 static int vcpu_hvm(struct xc_dom_image *dom) { + /* Initialises the BSP */ struct { struct hvm_save_descriptor header_d; HVM_SAVE_TYPE(HEADER) header; @@ -997,6 +998,18 @@ static int vcpu_hvm(struct xc_dom_image *dom) struct hvm_save_descriptor end_d; HVM_SAVE_TYPE(END) end; } bsp_ctx; + /* Initialises APICs and MTRRs of every vCPU */ + struct { + struct hvm_save_descriptor header_d; + HVM_SAVE_TYPE(HEADER) header; + struct hvm_save_descriptor mtrr_d; + HVM_SAVE_TYPE(MTRR) mtrr; + struct hvm_save_descriptor end_d; + HVM_SAVE_TYPE(END) end; + } vcpu_ctx; + /* Context from full_ctx */ + const HVM_SAVE_TYPE(MTRR) *mtrr_record; + /* Raw context as taken from Xen */ uint8_t *full_ctx =3D NULL; int rc; =20 @@ -1083,51 +1096,41 @@ static int vcpu_hvm(struct xc_dom_image *dom) bsp_ctx.end_d.instance =3D 0; bsp_ctx.end_d.length =3D HVM_SAVE_LENGTH(END); =20 - /* TODO: maybe this should be a firmware option instead? */ - if ( !dom->device_model ) + /* TODO: maybe setting MTRRs should be a firmware option instead? */ + mtrr_record =3D hvm_get_save_record(full_ctx, HVM_SAVE_CODE(MTRR), 0); + + if ( !mtrr_record) { - struct { - struct hvm_save_descriptor header_d; - HVM_SAVE_TYPE(HEADER) header; - struct hvm_save_descriptor mtrr_d; - HVM_SAVE_TYPE(MTRR) mtrr; - struct hvm_save_descriptor end_d; - HVM_SAVE_TYPE(END) end; - } mtrr =3D { - .header_d =3D bsp_ctx.header_d, - .header =3D bsp_ctx.header, - .mtrr_d.typecode =3D HVM_SAVE_CODE(MTRR), - .mtrr_d.length =3D HVM_SAVE_LENGTH(MTRR), - .end_d =3D bsp_ctx.end_d, - .end =3D bsp_ctx.end, - }; - const HVM_SAVE_TYPE(MTRR) *mtrr_record =3D - hvm_get_save_record(full_ctx, HVM_SAVE_CODE(MTRR), 0); - unsigned int i; - - if ( !mtrr_record ) - { - xc_dom_panic(dom->xch, XC_INTERNAL_ERROR, - "%s: unable to get MTRR save record", __func__); - goto out; - } + xc_dom_panic(dom->xch, XC_INTERNAL_ERROR, + "%s: unable to get MTRR save record", __func__); + goto out; + } =20 - memcpy(&mtrr.mtrr, mtrr_record, sizeof(mtrr.mtrr)); + vcpu_ctx.header_d =3D bsp_ctx.header_d; + vcpu_ctx.header =3D bsp_ctx.header; + vcpu_ctx.mtrr_d.typecode =3D HVM_SAVE_CODE(MTRR); + vcpu_ctx.mtrr_d.length =3D HVM_SAVE_LENGTH(MTRR); + vcpu_ctx.mtrr =3D *mtrr_record; + vcpu_ctx.end_d =3D bsp_ctx.end_d; + vcpu_ctx.end =3D bsp_ctx.end; =20 - /* - * Enable MTRR, set default type to WB. - * TODO: add MMIO areas as UC when passthrough is supported. - */ - mtrr.mtrr.msr_mtrr_def_type =3D MTRR_TYPE_WRBACK | MTRR_DEF_TYPE_E= NABLE; + /* + * Enable MTRR, set default type to WB. + * TODO: add MMIO areas as UC when passthrough is supported in PVH + */ + vcpu_ctx.mtrr.msr_mtrr_def_type =3D MTRR_TYPE_WRBACK | MTRR_DEF_TYPE_E= NABLE; =20 - for ( i =3D 0; i < dom->max_vcpus; i++ ) + for ( unsigned int i =3D 0; i < dom->max_vcpus; i++ ) + { + vcpu_ctx.mtrr_d.instance =3D i; + + rc =3D xc_domain_hvm_setcontext(dom->xch, dom->guest_domid, + (uint8_t *)&vcpu_ctx, sizeof(vcp= u_ctx)); + if ( rc !=3D 0 ) { - mtrr.mtrr_d.instance =3D i; - rc =3D xc_domain_hvm_setcontext(dom->xch, dom->guest_domid, - (uint8_t *)&mtrr, sizeof(mtrr)); - if ( rc !=3D 0 ) - xc_dom_panic(dom->xch, XC_INTERNAL_ERROR, - "%s: SETHVMCONTEXT failed (rc=3D%d)", __func_= _, rc); + xc_dom_panic(dom->xch, XC_INTERNAL_ERROR, + "%s: SETHVMCONTEXT failed (rc=3D%d)", __func__, r= c); + goto out; } } =20 --=20 2.34.1