From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by mx.groups.io with SMTP id smtpd.web11.5284.1615272424317565667 for ; Mon, 08 Mar 2021 22:47:04 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@intel.onmicrosoft.com header.s=selector2-intel-onmicrosoft-com header.b=AwFhO3Mv; spf=pass (domain: intel.com, ip: 134.134.136.31, mailfrom: jiewen.yao@intel.com) IronPort-SDR: QodJJvX3Oo6Tm0h02TgEkqRqsA24/O53bNea0XOjDSwHg5IEv1wYWPkVBA8zyMkzYL6P3LfHzT oFIboPjPZYCQ== X-IronPort-AV: E=McAfee;i="6000,8403,9917"; a="249549891" X-IronPort-AV: E=Sophos;i="5.81,234,1610438400"; d="scan'208";a="249549891" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Mar 2021 22:47:03 -0800 IronPort-SDR: hCrUulFIL/jKonckDwCIOGBPzcQn3vQYG7URcVnwbGuImz7HLszv/7QUhsissbzZKKNIKTT4s3 zkYhmrKcb/Ug== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.81,234,1610438400"; d="scan'208";a="371429603" Received: from orsmsx605.amr.corp.intel.com ([10.22.229.18]) by orsmga006.jf.intel.com with ESMTP; 08 Mar 2021 22:47:01 -0800 Received: from orsmsx609.amr.corp.intel.com (10.22.229.22) by ORSMSX605.amr.corp.intel.com (10.22.229.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2106.2; Mon, 8 Mar 2021 22:46:59 -0800 Received: from orsmsx609.amr.corp.intel.com (10.22.229.22) by ORSMSX609.amr.corp.intel.com (10.22.229.22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2106.2; Mon, 8 Mar 2021 22:46:59 -0800 Received: from ORSEDG602.ED.cps.intel.com (10.7.248.7) by orsmsx609.amr.corp.intel.com (10.22.229.22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2106.2 via Frontend Transport; Mon, 8 Mar 2021 22:46:59 -0800 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (104.47.59.172) by edgegateway.intel.com (134.134.137.103) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2106.2; Mon, 8 Mar 2021 22:46:59 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=h/44H4kfxI4/uGx34UDSWUQpLchr1tWPGSkJ8DySVZs7lDejAc/a6cc5mmjPLXPTEhv5t45EO+hCfGHF0QFLJJE+1LideY4blrolHtmYEVvrtcV5gDDCj31a1BRU7pC6q2VvUzbzaPLmr5N2ZtUPFTTJOfv+mlnJ+0pVaoWH+vUM47jejKKa7TucT5qzU2v+GINR5c1n4y+KLfCPWb+xduRrSA7Z/Yeb2FQ1O6FYOeHryi+DnMBsFzcKZnWn2SO6Zwt2xTkAtTk34IwXdOsVS6IU9lJ3iNx5z+uTq37BuLYmTLOlDf9a+iLuel5oJg8rhUSIKExZ5ABpU8VvfjAIKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UNH8eTiIjPaRHmevmA/3EQVDJQbXKkpzwir7nOg3+t0=; b=DAueOXpqWJucCaKHSehxCkaZLXUwXUYZOW4M0ISXdN39XSoQoQRKfSo0L9TwaosUDbvgAVT2VR9iong1LEMY6s5F83S/eeuRIG15dLEgsuaoXSwrIqrnTZ+pm2kZlodt+CmIAnej4Ak5O4CCs0qGkj11RWKwzTawTk90E+HG4g8uxlvOONpPKeIjeqRSnevz/LeC/QPpnykuh9OAE3rgf0N3KrLYL59i6vg5EdK90QnEyy78CuHr51d2kZEJ1Wyj/+MS8GhL/xnP6piL4WGPe5YL7pP3Wm3Wsk+BNCuF1AuKvC9dn2hE+qsa/TBfYL1ezMWpqj2dKfwqXFtM6jx/ew== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.onmicrosoft.com; s=selector2-intel-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UNH8eTiIjPaRHmevmA/3EQVDJQbXKkpzwir7nOg3+t0=; b=AwFhO3Mvy9BeZ2tuK6ciOPrNN/O8juIs4JSs1VBgZKwdeETBO07kcWMDGYa2ouu+Ed52uxp/Cf0SnxsXvduZwJUjK/a1wH++27roGu20rnH6CGb6PHqh47Dq4biNrD/krFcdfBoYnvbAk2ju4r9vy3GLjntw8uqAkCNcUChOk+s= Received: from BY5PR11MB4166.namprd11.prod.outlook.com (2603:10b6:a03:191::25) by SJ0PR11MB4925.namprd11.prod.outlook.com (2603:10b6:a03:2df::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3912.26; Tue, 9 Mar 2021 06:46:57 +0000 Received: from BY5PR11MB4166.namprd11.prod.outlook.com ([fe80::5983:f233:56d6:8132]) by BY5PR11MB4166.namprd11.prod.outlook.com ([fe80::5983:f233:56d6:8132%4]) with mapi id 15.20.3912.027; Tue, 9 Mar 2021 06:46:57 +0000 From: "Yao, Jiewen" To: "Xu, Min M" , "devel@edk2.groups.io" CC: "Justen, Jordan L" , Laszlo Ersek , "Reiland, Doug" Subject: Re: [PATCH V3 3/3] OvmfPkg: Implement library support for TdxLib SEC and DXE on OVMF Thread-Topic: [PATCH V3 3/3] OvmfPkg: Implement library support for TdxLib SEC and DXE on OVMF Thread-Index: AQHXFKtm5/CAsr2vuU+FEOxtLrDyTqp7NjaQ Date: Tue, 9 Mar 2021 06:46:57 +0000 Message-ID: References: <6761377c2213ff3c9d1bfa3c3df6ce421af2688b.1615269637.git.min.m.xu@intel.com> In-Reply-To: <6761377c2213ff3c9d1bfa3c3df6ce421af2688b.1615269637.git.min.m.xu@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: dlp-version: 11.5.1.3 dlp-product: dlpe-windows dlp-reaction: no-action authentication-results: intel.com; dkim=none (message not signed) header.d=none;intel.com; dmarc=none action=none header.from=intel.com; x-originating-ip: [192.198.147.206] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 13f75bb8-aba1-4a6d-80d9-08d8e2c72262 x-ms-traffictypediagnostic: SJ0PR11MB4925: x-ld-processed: 46c98d88-e344-4ed4-8496-4ed7712e255d,ExtAddr x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:10000; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: t9OTyzfZgdelbOQ1bdJ+X1447YdT4eaWW3Wt5JYqxCgDSxpO3j1vCV5tXlZ3sGM2JC+maSKqMtWOvgYuwpU0VIpClxBzgD4F3BvPC07k5tUwxAVKunG+pnR0NSgxePwP4LoU30TZE1xOyvxbahraITqfm5bJDZUHzQbihKf39r5T6M498jnTo1GObYNCrA+5MUlbNYIadO3o+hGTHGltIvOuj/r1TtJJ6ioimvJG0uEtF83demkqqfErhfd5iBua+y03rXstEa+9HASugIqu6yTz2ovxjxZ14Q+jxSnVxYLGT6pcmNiEQnPfiTkX3c5pwZuk3xQX78kGFCHw5OwaqhKwzUgCHnRmp0+up+SUpTXRaLcVor9uyeX365VMy9eTqMGowa1EKOtkU0BAy5vZkwu6XkWLr0kvCqTZlz67nQ7Q5j7weWuoHj5646pxpt1EiA9JLzy3RbJoql2kIG8KtN+/XK30Ef194hKcsYaImBJukLH7qYee+K8xYLOFDpX6p/UFC29clg35PDNYt0y7iZxMjsRk7sRv0rkb6oUv8GqcU6mxDkLCsD4u5OHsOXzlmYwbLnIwXjfNcOv31Q+Ro1rvmypK4l14sBoztJHrLfA= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BY5PR11MB4166.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(346002)(396003)(376002)(136003)(39860400002)(366004)(16799955002)(8676002)(8936002)(2906002)(316002)(6506007)(9686003)(64756008)(66476007)(83380400001)(66446008)(66556008)(478600001)(76116006)(53546011)(4326008)(55016002)(71200400001)(54906003)(5660300002)(7696005)(26005)(186003)(30864003)(86362001)(52536014)(110136005)(15188155005)(966005)(19627235002)(107886003)(66946007)(33656002)(579004);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata: =?us-ascii?Q?jxH3cNEFSSAxaK33Qq6HCfoQUp9vJENwsfB4P8Je5kZZPichnQEYJW6mYLk8?= =?us-ascii?Q?+lNWPD6vMeecCh6TVUPIZ5y4OxREZQQ0ztnHEIoa1kTQu6IKtz6sGOTfBOcs?= =?us-ascii?Q?z4udzZ8ieXSuBPolBu/1nhoU9QHni7L3jPJIG9kVvbmui/UF7t3DLkjYEu3u?= =?us-ascii?Q?CQ121DYZbQukFlpfq3IQ0vY4fTVVU3o4DI6u1YNmB41Xn1cvnjuojpXkvvNj?= =?us-ascii?Q?zr9fFu/JrrROYWDLMwmA5ZEQC6Pm5C0c02g1E8m4CWgj7QRTixuOLii7vHfw?= =?us-ascii?Q?HwjHiNzTQCqJsy0Vc0zWyDq+dRLAOW+knLcMqf3gTFMT4E2KAobYCKl2A89Y?= =?us-ascii?Q?+0JAL2IGOHRuSx6ztH//RE6KFCdXdw/kODpyLZzfb1c2yQkR2PFWZnokpl/B?= =?us-ascii?Q?k500PIJLekO7dsslhSoZCoDUnsNQVq9oxX8Gbu/EGQ5kHskOrSHLB5Kctz4e?= =?us-ascii?Q?3bjxPrwJa77CiohjoNwmFn2oC5UeFfke+9WUWP0rx49hr2vBQ+OHmEFEyZIJ?= =?us-ascii?Q?tCcsyv8qKNagyeaaz8OtGHpOZg3nztjK418UrYHVo00hjDvkal6sc78roy4C?= =?us-ascii?Q?wpbUgB7WuNiZ0vNJb7PlkCDmv5cYzwcUALmWsAxEjbaczp91DYAD6jsiX4IZ?= =?us-ascii?Q?HBAP6NXICaiqkSKDSaMz0/k0uwVX6YeaQFX71AZhxEbA4SFc9kPeglbDNwza?= =?us-ascii?Q?FxtNj4IhQ3IiOuSWoqDoeZmMJaHlxOABM0f4q0hhcTMW5ag6FlvfkUPcPGUO?= =?us-ascii?Q?i+EVUbyGUdGIw57FW3ad1fu3SmX2yolOztOpoVahOl1kJmeYnG/VQS/+vNvX?= =?us-ascii?Q?zQF4Au0cwgvA0h0LxooMgtaUwWoLnfQfLlDyrKpUDBqxS8c27A7nB+dHQqIa?= =?us-ascii?Q?8hmI5e85AAhnwIoFKCv+Oo1mJEyvYaRmz0Ez3QdGh3eiEn5N8CIOE8ZE5rMW?= =?us-ascii?Q?NlUw2UhWs8z+hbrdfnRj31OfJoW2JyFELPTbLpfVmQFE5mq6lIbrMfjPvaRN?= =?us-ascii?Q?nLnBByQViUGXgdPt9di05bk3TlWtJrSZnbV/oDP6mmFqGvbIqKREDCbMExba?= =?us-ascii?Q?V+Lc6DlF5TfA1KB21nk67d6wJ6jR4v0hzLuvmR1yMettfBCcn8xSFFcJQ5Yn?= =?us-ascii?Q?zjmAvLuxzUlZ2XOZPhlrXOXAY4RrNVZJ6+tX+7820KYZ5Ar9y+rwEVArO4pd?= =?us-ascii?Q?k/u40GIld9MQzRzxv0EWuNBstJ93nyJozso7uA8oRo2idWujOmQzWIUy2tu4?= =?us-ascii?Q?l8GXNF1X1y0JvXENy56cefRdWFTQ6UN23EjcUM4mVxkdhJKWAquAmfMh6Cob?= =?us-ascii?Q?E1OsJl31T7KpXGY7W8HlBwzv?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: BY5PR11MB4166.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 13f75bb8-aba1-4a6d-80d9-08d8e2c72262 X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Mar 2021 06:46:57.5135 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: QD1pJhfD/F6DndfQGiz7YAGsece6jHmV/EvEJWAqKl27+5NgAVQ8EHmW0F2YiG91krSVGy+K2K2yT9f0TMp22A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR11MB4925 Return-Path: jiewen.yao@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hi Would you please try to merge TdxLib and TdxLibSec? I am not sure if we really need two instance. Also, can we remove MemoryAllocationLib ?=20 If it is just used to allocate aligned memory, can we allocate aligned memo= ry in stack instead of heap ? > -----Original Message----- > From: Xu, Min M > Sent: Tuesday, March 9, 2021 2:13 PM > To: devel@edk2.groups.io > Cc: Xu, Min M ; Justen, Jordan L > ; Laszlo Ersek ; Yao, Jiewe= n > ; Reiland, Doug > Subject: [PATCH V3 3/3] OvmfPkg: Implement library support for TdxLib SEC= and > DXE on OVMF >=20 > The base TdxLib in MdePkg/Library provides a default limited interface. > As it does not provide full support, create an OVMF version of this libra= ry > to begin the process of providing full support of TDX in OVMF. >=20 > Cc: Jordan Justen > Cc: Laszlo Ersek > Cc: Jiewen Yao >=20 > Signed-off-by: Min Xu > Signed-off-by: Doug Reiland > --- > OvmfPkg/Library/TdxLib/AcceptPages.c | 68 ++++++++ > OvmfPkg/Library/TdxLib/Rtmr.c | 80 +++++++++ > OvmfPkg/Library/TdxLib/TdReport.c | 102 +++++++++++ > OvmfPkg/Library/TdxLib/TdxLib.inf | 48 ++++++ > OvmfPkg/Library/TdxLib/TdxLibSec.inf | 45 +++++ > OvmfPkg/Library/TdxLib/X64/Tdcall.nasm | 125 ++++++++++++++ > OvmfPkg/Library/TdxLib/X64/Tdvmcall.nasm | 211 +++++++++++++++++++++++ > 7 files changed, 679 insertions(+) > create mode 100644 OvmfPkg/Library/TdxLib/AcceptPages.c > create mode 100644 OvmfPkg/Library/TdxLib/Rtmr.c > create mode 100644 OvmfPkg/Library/TdxLib/TdReport.c > create mode 100644 OvmfPkg/Library/TdxLib/TdxLib.inf > create mode 100644 OvmfPkg/Library/TdxLib/TdxLibSec.inf > create mode 100644 OvmfPkg/Library/TdxLib/X64/Tdcall.nasm > create mode 100644 OvmfPkg/Library/TdxLib/X64/Tdvmcall.nasm >=20 > diff --git a/OvmfPkg/Library/TdxLib/AcceptPages.c > b/OvmfPkg/Library/TdxLib/AcceptPages.c > new file mode 100644 > index 000000000000..3848bb6a95a4 > --- /dev/null > +++ b/OvmfPkg/Library/TdxLib/AcceptPages.c > @@ -0,0 +1,68 @@ > +/** @file > + > + There are 4 defined types in TD memory. > + Unaccepted memory is a special type of private memory. The OVMF must > + invoke TDCALL [TDG.MEM.PAGE.ACCEPT] the unaccepted memory before use > it. > + > + Copyright (c) 2020 - 2021, Intel Corporation. All rights reserved.
> + > + SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +#include > +#include > +#include > +#include > +#include > + > +UINT64 mNumberOfDuplicatedAcceptedPages; > + > +/** > + This function accept a pending private page, and initialize the page t= o > + all-0 using the TD ephemeral private key. > + > + @param[in] StartAddress Guest physical address of the priva= te > + page to accept. > + @param[in] NumberOfPages Number of the pages to be accepted. > + > + @return EFI_SUCCESS > +**/ > +EFI_STATUS > +EFIAPI > +TdAcceptPages ( > + IN UINT64 StartAddress, > + IN UINT64 NumberOfPages > + ) > +{ > + UINT64 Address; > + UINT64 Status; > + UINT64 Index; > + > + // > + // Determine if we need to accept pages before use > + // > + if (FixedPcdGetBool(PcdUseTdxAcceptPage) =3D=3D FALSE) { > + return EFI_SUCCESS; > + } > + > + Address =3D StartAddress; > + > + for( Index =3D 0; Index < NumberOfPages; Index++) { > + Status =3D TdCall(TDCALL_TDACCEPTPAGE,Address, 0, 0, 0); > + if (Status !=3D TDX_EXIT_REASON_SUCCESS) { > + if ((Status & ~0xFFULL) =3D=3D TDX_EXIT_REASON_PAGE_ALREADY_ACCE= PTED) > { > + ++mNumberOfDuplicatedAcceptedPages; > + DEBUG((DEBUG_VERBOSE, "Address %llx already accepted. Total nu= mber > of already accepted pages %ld\n", > + Address, mNumberOfDuplicatedAcceptedPages)); > + } else { > + DEBUG((DEBUG_ERROR, "Address %llx failed to be accepted. Error > =3D %ld\n", > + Address, Status)); > + ASSERT(Status =3D=3D TDX_EXIT_REASON_SUCCESS); > + } > + } > + Address +=3D EFI_PAGE_SIZE; > + } > + return EFI_SUCCESS; > +} > + > diff --git a/OvmfPkg/Library/TdxLib/Rtmr.c b/OvmfPkg/Library/TdxLib/Rtmr.= c > new file mode 100644 > index 000000000000..a4b36b6c4bef > --- /dev/null > +++ b/OvmfPkg/Library/TdxLib/Rtmr.c > @@ -0,0 +1,80 @@ > +/** @file > + > + Extends one of the RTMR measurement registers in TDCS with the provide= d > + extension data in memory. > + > + Copyright (c) 2020 - 2021, Intel Corporation. All rights reserved.
> + > + SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#define RTMR_COUNT 4 > + > +/** > + This function extends one of the RTMR measurement register > + in TDCS with the provided extension data in memory. > + RTMR extending supports SHA384 which length is 48 bytes. > + > + @param[in] Data Point to the data to be extended > + @param[in] DataLen Length of the data. Must be 48 > + @param[in] Index RTMR index > + > + @return EFI_SUCCESS > + @return EFI_INVALID_PARAMETER > + @return EFI_DEVICE_ERROR > + > +**/ > +EFI_STATUS > +EFIAPI > +TdExtendRtmr( > + IN UINT32 *Data, > + IN UINT32 DataLen, > + IN UINT8 Index > + ) > +{ > + EFI_STATUS Status; > + UINT64 *Buffer; > + UINT64 TdCallStatus; > + > + Status =3D EFI_SUCCESS; > + > + ASSERT(Index >=3D 0 && Index < RTMR_COUNT); > + ASSERT(DataLen =3D=3D SHA384_DIGEST_SIZE); > + > + // > + // Allocate 64B aligned mem to hold the sha384 hash value > + // > + Buffer =3D AllocateAlignedPages(EFI_SIZE_TO_PAGES(SHA384_DIGEST_SIZE),= 64); > + if(Data =3D=3D NULL){ > + return EFI_OUT_OF_RESOURCES; > + } > + CopyMem(Buffer, Data, SHA384_DIGEST_SIZE); > + > + TdCallStatus =3D TdCall(TDCALL_TDEXTENDRTMR, (UINT64)Buffer, Index, 0,= 0); > + > + if(TdCallStatus =3D=3D TDX_EXIT_REASON_SUCCESS){ > + Status =3D EFI_SUCCESS; > + }else if(TdCallStatus =3D=3D TDX_EXIT_REASON_OPERAND_INVALID){ > + Status =3D EFI_INVALID_PARAMETER; > + }else{ > + Status =3D EFI_DEVICE_ERROR; > + } > + > + if(Status !=3D EFI_SUCCESS){ > + DEBUG((DEBUG_ERROR, "Error returned from TdExtendRtmr call - 0x%lx\n= ", > TdCallStatus)); > + } > + > + FreeAlignedPages(Buffer, EFI_SIZE_TO_PAGES(SHA384_DIGEST_SIZE)); > + > + return Status; > +} > diff --git a/OvmfPkg/Library/TdxLib/TdReport.c > b/OvmfPkg/Library/TdxLib/TdReport.c > new file mode 100644 > index 000000000000..ace213bcf467 > --- /dev/null > +++ b/OvmfPkg/Library/TdxLib/TdReport.c > @@ -0,0 +1,102 @@ > +/** @file > + > + Retrieve TDREPORT_STRUCT structure from TDX > + > + Copyright (c) 2020 - 2021, Intel Corporation. All rights reserved.
> + > + SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +#include > +#include > +#include > +#include > +#include > +#include > + > +#define REPORT_STRUCT_SIZE 1024 > +#define ADDITIONAL_DATA_SIZE 64 > + > +/** > + This function retrieve TDREPORT_STRUCT structure from TDX. > + The struct contains the measurements/configuration information of > + the guest TD that called the function, measurements/configuratio > + information of the TDX-SEAM module and a REPORTMACSTRUCT. > + The REPORTMACSTRUCT is integrity protected with a MAC and > + contains the hash of the measurements and configuration > + as well as additional REPORTDATA provided by the TD software. > + > + AdditionalData, a 64-byte value, is provided by the guest TD > + to be included in the TDREPORT > + > + @param[in,out] Report Holds the TEREPORT_STRUCT. > + @param[in] ReportSize Size of the report. It must be > + larger than 1024B. > + @param[in] AdditionalData Point to the additional data. > + @param[in] AdditionalDataSize Size of the additional data. > + If AdditionalData !=3D NULL, then > + this value must be 64B. > + > + @return EFI_SUCCESS > + @return EFI_INVALID_PARAMETER > + @return EFI_DEVICE_ERROR > + > +**/ > +EFI_STATUS > +EFIAPI > +TdReport( > + IN OUT UINT8 *Report, > + IN UINT32 ReportSize, > + IN UINT8 *AdditionalData, > + IN UINT32 AdditionalDataSize > + ) > + > +{ > + EFI_STATUS Status; > + UINT64 *Data; > + UINT64 *Report_Struct; > + UINT64 *Report_Data; > + UINT64 TdCallStatus; > + > + if(ReportSize < REPORT_STRUCT_SIZE){ > + return EFI_INVALID_PARAMETER; > + } > + > + if(AdditionalData !=3D NULL && AdditionalDataSize !=3D ADDITIONAL_DATA= _SIZE){ > + return EFI_INVALID_PARAMETER; > + } > + > + Data =3D AllocatePages(EFI_SIZE_TO_PAGES(REPORT_STRUCT_SIZE + > ADDITIONAL_DATA_SIZE)); > + if(Data =3D=3D NULL){ > + return EFI_OUT_OF_RESOURCES; > + } > + > + Report_Struct =3D Data; > + Report_Data =3D Data + REPORT_STRUCT_SIZE; > + if(AdditionalData !=3D NULL){ > + CopyMem(Report_Data, AdditionalData, ADDITIONAL_DATA_SIZE); > + }else{ > + ZeroMem(Report_Data, ADDITIONAL_DATA_SIZE); > + } > + > + TdCallStatus =3D TdCall(TDCALL_TDREPORT, (UINT64)Report_Struct, > (UINT64)Report_Data, 0, 0); > + > + if(TdCallStatus =3D=3D TDX_EXIT_REASON_SUCCESS){ > + Status =3D EFI_SUCCESS; > + }else if(TdCallStatus =3D=3D TDX_EXIT_REASON_OPERAND_INVALID){ > + Status =3D EFI_INVALID_PARAMETER; > + }else{ > + Status =3D EFI_DEVICE_ERROR; > + } > + > + if(Status !=3D EFI_SUCCESS){ > + DEBUG((DEBUG_ERROR, "Error returned from TdReport call - 0x%lx\n", > TdCallStatus)); > + }else{ > + CopyMem(Report, Data, REPORT_STRUCT_SIZE); > + } > + > + FreePages(Data, EFI_SIZE_TO_PAGES(REPORT_STRUCT_SIZE + > ADDITIONAL_DATA_SIZE)); > + > + return Status; > +} > diff --git a/OvmfPkg/Library/TdxLib/TdxLib.inf > b/OvmfPkg/Library/TdxLib/TdxLib.inf > new file mode 100644 > index 000000000000..f642de9e3a5f > --- /dev/null > +++ b/OvmfPkg/Library/TdxLib/TdxLib.inf > @@ -0,0 +1,48 @@ > +## @file > +# Tdx library > +# > +# Copyright (c) 2020- 2021, Intel Corporation. All rights reserved.
> +# This program and the accompanying materials > +# are licensed and made available under the terms and conditions of the = BSD > License > +# which accompanies this distribution. The full text of the license may= be found > at > +# http://opensource.org/licenses/bsd-license.php. > +# > +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" > BASIS, > +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER > EXPRESS OR IMPLIED. > +# > +## > + > +[Defines] > + INF_VERSION =3D 0x00010005 > + BASE_NAME =3D TdxLib > + FILE_GUID =3D 032A8E0D-0C27-40C0-9CAA-23B731C1B22= 3 > + MODULE_TYPE =3D BASE > + VERSION_STRING =3D 1.0 > + LIBRARY_CLASS =3D TdxLib|PEI_CORE PEIM DXE_CORE DXE_D= RIVER > DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_DRIVER UEFI_APPLICATION > + > +# > +# The following information is for reference only and not required by th= e build > tools. > +# > +# VALID_ARCHITECTURES =3D X64 > +# > + > +[Sources] > + Rtmr.c > + TdReport.c > + AcceptPages.c > + X64/Tdcall.nasm > + X64/Tdvmcall.nasm > + > +[Packages] > + MdePkg/MdePkg.dec > + OvmfPkg/OvmfPkg.dec > + > +[LibraryClasses] > + BaseLib > + BaseMemoryLib > + DebugLib > + MemoryAllocationLib > + > +[Pcd] > + gUefiOvmfPkgTokenSpaceGuid.PcdUseTdxAcceptPage > + gUefiOvmfPkgTokenSpaceGuid.PcdUseTdxEmulation > diff --git a/OvmfPkg/Library/TdxLib/TdxLibSec.inf > b/OvmfPkg/Library/TdxLib/TdxLibSec.inf > new file mode 100644 > index 000000000000..82ef4f08be8c > --- /dev/null > +++ b/OvmfPkg/Library/TdxLib/TdxLibSec.inf > @@ -0,0 +1,45 @@ > +## @file > +# Tdx library for SEC phase. > +# > +# Copyright (c) 2020 - 2021, Intel Corporation. All rights reserved.
> +# This program and the accompanying materials > +# are licensed and made available under the terms and conditions of the = BSD > License > +# which accompanies this distribution. The full text of the license may= be found > at > +# http://opensource.org/licenses/bsd-license.php. > +# > +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" > BASIS, > +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER > EXPRESS OR IMPLIED. > +# > +## > + > +[Defines] > + INF_VERSION =3D 0x00010005 > + BASE_NAME =3D TdxLibSec > + FILE_GUID =3D 498E8E1E-5B11-41F3-9083-EEE3A32B009= D > + MODULE_TYPE =3D BASE > + VERSION_STRING =3D 1.0 > + LIBRARY_CLASS =3D TdxLib|SEC > + > +# > +# The following information is for reference only and not required by th= e build > tools. > +# > +# VALID_ARCHITECTURES =3D X64 > +# > + > +[Sources] > + AcceptPages.c > + X64/Tdcall.nasm > + X64/Tdvmcall.nasm > + > +[Packages] > + MdePkg/MdePkg.dec > + OvmfPkg/OvmfPkg.dec > + > +[LibraryClasses] > + BaseLib > + BaseMemoryLib > + DebugLib > + > +[Pcd] > + gUefiOvmfPkgTokenSpaceGuid.PcdUseTdxAcceptPage > + gUefiOvmfPkgTokenSpaceGuid.PcdUseTdxEmulation > diff --git a/OvmfPkg/Library/TdxLib/X64/Tdcall.nasm > b/OvmfPkg/Library/TdxLib/X64/Tdcall.nasm > new file mode 100644 > index 000000000000..d0d55e2a9443 > --- /dev/null > +++ b/OvmfPkg/Library/TdxLib/X64/Tdcall.nasm > @@ -0,0 +1,125 @@ > +;-----------------------------------------------------------------------= ------- > +;* > +;* Copyright (c) 2020, Intel Corporation. All rights reserved.
> +;* This program and the accompanying materials > +;* are licensed and made available under the terms and conditions of t= he BSD > License > +;* which accompanies this distribution. The full text of the license = may be > found at > +;* http://opensource.org/licenses/bsd-license.php > +;* > +;* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" > BASIS, > +;* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER > EXPRESS OR IMPLIED. > +;* > +;* > +;-----------------------------------------------------------------------= ------- > + > +DEFAULT REL > +SECTION .text > + > +%macro tdcall 0 > +%if (FixedPcdGet32 (PcdUseTdxEmulation) !=3D 0) > + vmcall > +%else > + db 0x66,0x0f,0x01,0xcc > +%endif > +%endmacro > + > +%macro tdcall_push_regs 0 > + push rbp > + mov rbp, rsp > + push r15 > + push r14 > + push r13 > + push r12 > + push rbx > + push rsi > + push rdi > +%endmacro > + > +%macro tdcall_pop_regs 0 > + pop rdi > + pop rsi > + pop rbx > + pop r12 > + pop r13 > + pop r14 > + pop r15 > + pop rbp > +%endmacro > + > +%define number_of_regs_pushed 8 > +%define number_of_parameters 4 > + > +; Keep these in sync for push_regs/pop_regs, code below uses them to fin= d 5th > or greater parameters > +%define first_variable_on_stack_offset (number_of_regs_pushed * 8) + > (number_of_parameters * 8) + 8 > +%define second_variable_on_stack_offset (first_variable_on_stack_offset)= + 8 > + > +%macro tdcall_regs_preamble 2 > + mov rax, %1 > + > + mov ecx, %2 > + > + ; R10 =3D 0 (standard TDVMCALL) > + > + xor r10d, r10d > + > + ; Zero out unused (for standard TDVMCALL) registers to avoid leaking > + ; secrets to the VMM. > + > + xor ebx, ebx > + xor esi, esi > + xor edi, edi > + > + xor edx, edx > + xor ebp, ebp > + xor r8d, r8d > + xor r9d, r9d > +%endmacro > + > +%macro tdcall_regs_postamble 0 > + xor ebx, ebx > + xor esi, esi > + xor edi, edi > + > + xor ecx, ecx > + xor edx, edx > + xor r8d, r8d > + xor r9d, r9d > + xor r10d, r10d > + xor r11d, r11d > +%endmacro > + > +; TdCall ( > +; UINT64 Leaf, // Rcx > +; UINT64 P1, // Rdx > +; UINT64 P2, // R8 > +; UINT64 P3, // R9 > +; UINT64 Results, // rsp + 0x28 > +; ) > +global ASM_PFX(TdCall) > +ASM_PFX(TdCall): > + tdcall_push_regs > + > + mov rax, rcx > + mov rcx, rdx > + mov rdx, r8 > + mov r8, r9 > + > + tdcall > + > + ; exit if tdcall reports failure. > + test rax, rax > + jnz .exit > + > + ; test if caller wanted results > + mov r12, [rsp + first_variable_on_stack_offset ] > + test r12, r12 > + jz .exit > + mov [r12 + 0 ], rcx > + mov [r12 + 8 ], rdx > + mov [r12 + 16], r8 > + mov [r12 + 24], r9 > + mov [r12 + 32], r10 > + mov [r12 + 40], r11 > +.exit: > + tdcall_pop_regs > + ret > diff --git a/OvmfPkg/Library/TdxLib/X64/Tdvmcall.nasm > b/OvmfPkg/Library/TdxLib/X64/Tdvmcall.nasm > new file mode 100644 > index 000000000000..e1da9b4fbdd6 > --- /dev/null > +++ b/OvmfPkg/Library/TdxLib/X64/Tdvmcall.nasm > @@ -0,0 +1,211 @@ > +;-----------------------------------------------------------------------= ------- > +;* > +;* Copyright (c) 2020, Intel Corporation. All rights reserved.
> +;* This program and the accompanying materials > +;* are licensed and made available under the terms and conditions of t= he BSD > License > +;* which accompanies this distribution. The full text of the license = may be > found at > +;* http://opensource.org/licenses/bsd-license.php > +;* > +;* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" > BASIS, > +;* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER > EXPRESS OR IMPLIED. > +;* > +;* > +;-----------------------------------------------------------------------= ------- > + > +DEFAULT REL > +SECTION .text > + > +%define TDVMCALL_EXPOSE_REGS_MASK 0xffec > +%define TDVMCALL 0x0 > +%define EXIT_REASON_CPUID 0xa > + > +%macro tdcall 0 > +%if (FixedPcdGet32 (PcdUseTdxEmulation) !=3D 0) > + vmcall > +%else > + db 0x66,0x0f,0x01,0xcc > +%endif > +%endmacro > + > +%macro tdcall_push_regs 0 > + push rbp > + mov rbp, rsp > + push r15 > + push r14 > + push r13 > + push r12 > + push rbx > + push rsi > + push rdi > +%endmacro > + > +%macro tdcall_pop_regs 0 > + pop rdi > + pop rsi > + pop rbx > + pop r12 > + pop r13 > + pop r14 > + pop r15 > + pop rbp > +%endmacro > + > +%define number_of_regs_pushed 8 > +%define number_of_parameters 4 > + > +; Keep these in sync for push_regs/pop_regs, code below uses them to fin= d 5th > or greater parameters > +%define first_variable_on_stack_offset (number_of_regs_pushed * 8) + > (number_of_parameters * 8) + 8 > +%define second_variable_on_stack_offset (first_variable_on_stack_offset)= + 8 > + > +%macro tdcall_regs_preamble 2 > + mov rax, %1 > + > + mov ecx, %2 > + > + ; R10 =3D 0 (standard TDVMCALL) > + > + xor r10d, r10d > + > + ; Zero out unused (for standard TDVMCALL) registers to avoid leaking > + ; secrets to the VMM. > + > + xor ebx, ebx > + xor esi, esi > + xor edi, edi > + > + xor edx, edx > + xor ebp, ebp > + xor r8d, r8d > + xor r9d, r9d > +%endmacro > + > +%macro tdcall_regs_postamble 0 > + xor ebx, ebx > + xor esi, esi > + xor edi, edi > + > + xor ecx, ecx > + xor edx, edx > + xor r8d, r8d > + xor r9d, r9d > + xor r10d, r10d > + xor r11d, r11d > +%endmacro > + > +;-----------------------------------------------------------------------= ------- > +; 0 =3D> RAX =3D TDCALL leaf > +; M =3D> RCX =3D TDVMCALL register behavior > +; 1 =3D> R10 =3D standard vs. vendor > +; RDI =3D> R11 =3D TDVMCALL function / nr > +; RSI =3D R12 =3D p1 > +; RDX =3D> R13 =3D p2 > +; RCX =3D> R14 =3D p3 > +; R8 =3D> R15 =3D p4 > + > +; UINT64 > +; EFIAPI > +; TdVmCall ( > +; UINT64 Leaf, // Rcx > +; UINT64 P1, // Rdx > +; UINT64 P2, // R8 > +; UINT64 P3, // R9 > +; UINT64 P4, // rsp + 0x28 > +; UINT64 *Val // rsp + 0x30 > +; ) > +global ASM_PFX(TdVmCall) > +ASM_PFX(TdVmCall): > + tdcall_push_regs > + > + mov r11, rcx > + mov r12, rdx > + mov r13, r8 > + mov r14, r9 > + mov r15, [rsp + first_variable_on_stack_offset ] > + > + tdcall_regs_preamble TDVMCALL, TDVMCALL_EXPOSE_REGS_MASK > + > + tdcall > + > + ; ignore return dataif TDCALL reports failure. > + test rax, rax > + jnz .no_return_data > + > + ; Propagate TDVMCALL success/failure to return value. > + mov rax, r10 > + > + ; Retrieve the Val pointer. > + mov r9, [rsp + second_variable_on_stack_offset ] > + test r9, r9 > + jz .no_return_data > + > + ; On success, propagate TDVMCALL output value to output param > + test rax, rax > + jnz .no_return_data > + mov [r9], r11 > +.no_return_data: > + tdcall_regs_postamble > + > + tdcall_pop_regs > + > + ret > + > +;-----------------------------------------------------------------------= ------- > +; 0 =3D> RAX =3D TDCALL leaf > +; M =3D> RCX =3D TDVMCALL register behavior > +; 1 =3D> R10 =3D standard vs. vendor > +; RDI =3D> R11 =3D TDVMCALL function / nr > +; RSI =3D R12 =3D p1 > +; RDX =3D> R13 =3D p2 > +; RCX =3D> R14 =3D p3 > +; R8 =3D> R15 =3D p4 > + > +; UINT64 > +; EFIAPI > +; TdVmCallCpuid ( > +; UINT64 EaxIn, // Rcx > +; UINT64 EcxIn, // Rdx > +; UINT64 *Results // R8 > +; ) > +global ASM_PFX(TdVmCallCpuid) > +ASM_PFX(TdVmCallCpuid): > + tdcall_push_regs > + > + mov r11, EXIT_REASON_CPUID > + mov r12, rcx > + mov r13, rdx > + > + tdcall_regs_preamble TDVMCALL, TDVMCALL_EXPOSE_REGS_MASK > + > + ; Save *results pointers > + push r8 > + > + tdcall > + > + ; Panic if TDCALL reports failure. > + test rax, rax > + jnz .no_return_data > + > + ; Propagate TDVMCALL success/failure to return value. > + mov rax, r10 > + test rax, rax > + jnz .no_return_data > + > + ; Retrieve *Results > + pop r8 > + test r8, r8 > + jnz .no_return_data > + ; Caller pass in buffer so store results r12-r15 contains eax-edx > + mov [r8 + 0], r12 > + mov [r8 + 8], r13 > + mov [r8 + 16], r14 > + mov [r8 + 24], r15 > + > +.no_return_data: > + tdcall_regs_postamble > + > + tdcall_pop_regs > + > + ret > + > +.panic: > + ud2 > -- > 2.29.2.windows.2