From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (NAM02-SN1-obe.outbound.protection.outlook.com [40.107.96.87]) by mx.groups.io with SMTP id smtpd.web10.186.1626973673960417669 for ; Thu, 22 Jul 2021 10:07:54 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amd.com header.s=selector1 header.b=2om/YHy/; spf=permerror, err=parse error for token &{10 18 %{i}._ip.%{h}._ehlo.%{d}._spf.vali.email}: invalid domain name (domain: amd.com, ip: 40.107.96.87, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CXy68RpZKUi44hnZV8G36QekkguqpkJGed2p29hM4a/GUNLXonaZdawMks1iNl+EMAtbYblE1NRk608F27RAujeTTqnGn3uuXmZRC/TM+eYn2HoCdAaEKrqOTzQQ24CCkQbGguFpo/f5uK2EpzSZoH88oN/2ZIh1hd7BmAwyIYXLIy9ZNoCVPkepqzZBmztvhvpihlyLc8/crcxslxaAzmOkFBxLVdEnhh8zfH55noakz6loI/vzOJ3WIXYxIrHN47jrvU1H7QueN5RLixyB7cueOpc46xzuDS6KSs7IJVznlR0GOBdJTkAIAhuEFyjoukSqwMgDe3PFhEKfAG0gdw== 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=Fh/feBKql3IM85KdHpFPvUbHCMn4HuSc7/9bPGd3rvY=; b=DzWJ3Bg96eEJGFFYxMDQzs8gIlZHSUl3ovUHAi4CCK5INfQAri/LDC39mtvTH30gCYHQhJnPFfgr4oGtk4+XjZ3eiiDSP3H4yuQY3JtxooioaNmZ3dbatzTH7fX2/0to2Rt8vwJYgl1UeejvZS50HZa4ZFMMgVHUtZq6baSoQjFkDoT36Kx7yI2fwbFKvVpR9bYvihQeIOC7etEOG6tzRnCXxkIHCooSfuccx+9TazFE6OnS5iqeezGKSlFeZuTwD9NDurhLS01oLI/2/UNDCq5W7G9r0bsNI+pBlk+AaycIOPuTz37tdyWstHAGz2zfn0D6nve16zEU5lJ6JBmmMg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Fh/feBKql3IM85KdHpFPvUbHCMn4HuSc7/9bPGd3rvY=; b=2om/YHy/xk0mYl3EWPDjzKFF4KXSapxUwTSTU25PeRiBqesKYidvkHnPNmw0+/ShnH2pXZkENbrE+qkB6Og6OjmbHjNAgVI5Wj8oYZCE1wWz29QVTzdny3Ryd2Onn3n+4aiz+TlFqOPdMpzJ7K1ctAZvwrLxgyraZJ3vBo6aWRk= Authentication-Results: intel.com; dkim=none (message not signed) header.d=none;intel.com; dmarc=none action=none header.from=amd.com; Received: from DM4PR12MB5229.namprd12.prod.outlook.com (2603:10b6:5:398::12) by DM6PR12MB5535.namprd12.prod.outlook.com (2603:10b6:5:20a::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4352.25; Thu, 22 Jul 2021 17:07:52 +0000 Received: from DM4PR12MB5229.namprd12.prod.outlook.com ([fe80::73:2581:970b:3208]) by DM4PR12MB5229.namprd12.prod.outlook.com ([fe80::73:2581:970b:3208%3]) with mapi id 15.20.4352.026; Thu, 22 Jul 2021 17:07:52 +0000 Subject: Re: [PATCH V2 4/4] OvmfPkg/ResetVector: Update ResetVector to support Tdx To: Min Xu , devel@edk2.groups.io Cc: Ard Biesheuvel , Brijesh Singh , Erdem Aktas , James Bottomley , Jiewen Yao References: <0e28e0d01b2db776c5c00469bac5097a326c3ed9.1626931332.git.min.m.xu@intel.com> From: "Lendacky, Thomas" Message-ID: <9da89b5e-0092-d349-3f05-8329dfdf3917@amd.com> Date: Thu, 22 Jul 2021 12:07:49 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 In-Reply-To: <0e28e0d01b2db776c5c00469bac5097a326c3ed9.1626931332.git.min.m.xu@intel.com> X-ClientProxiedBy: SN6PR04CA0093.namprd04.prod.outlook.com (2603:10b6:805:f2::34) To DM4PR12MB5229.namprd12.prod.outlook.com (2603:10b6:5:398::12) Return-Path: thomas.lendacky@amd.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from [10.236.30.241] (165.204.77.1) by SN6PR04CA0093.namprd04.prod.outlook.com (2603:10b6:805:f2::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4352.24 via Frontend Transport; Thu, 22 Jul 2021 17:07:51 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 352249fc-ad53-424e-a46e-08d94d333d76 X-MS-TrafficTypeDiagnostic: DM6PR12MB5535: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4714; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: aEoCitSw+YxZOfZIlCjdTOgK3Cv+YaVbqpKiE2uxGyAdrvnb2N5qF4VQCw2vOlWm9XGMB3NNRK023ozFTKmvQAvyqT1hb049umCKg7S0qn5xDgZIsWeS0VfNTlAvZY5Zkq+Rr4BAfq9hWlXOdFkNPCYLTV6FHP2B578hT5iTzWLg7/0/W9hyiRqfq95p5X+j/ifkdAuQc2Q5wV+N5W0NgaVzLrzfv5ILXXqduoSMO2/0b1j7c935H6C+wdgV0gctXVVfdTauQDB+EIof8D/a5ws7TwhcQw9yMeaYhhK2v95+7vziG8lStqAr8+f42+vjgNplhd+Hd/DTDXjcMNik+7IzeYyGH3doEi/naGQQtKizI3sIuVM03/has7NLgFE7gT61sB/TOmE0cMMBWKEafyx11EAanWatrBCFrDwsm8eYO85vbZKAFdzqybwHVNLGRFGqt+V9ofq8Bw8xI0SxOUCtt9RmS2sySekIZETIOxwrDS9fA7UcWTWflAlxv/n/cZBumN6USP7hwKGeZlGT5/P/IHmG0lVpwe0LDxl37bT3L/cxe6PQOl69dYv97CqNzT5ym9UJ8QqSDLJ9FCxETwQ3a6aht96CZCK921Sb1QGB295Sr2cycrPV4Cm4omlY8dLPrit3WIVJjOP3JDZMNPiknw8zmJ1UIMJQ/skJ6svekRcRP/6rvWI9g24BMQnP2MZXOfKm0O3FKaR7u2fwhnA24z3/AsTt+Pj7mDbmqHoQnedoL0khHBHcI0/JnVp6LZbUO6Jbl9FPEfZJGf/GpAPdJfOjAz+6wUH9F+zxtnwSN1BcI0dBJMyEDoO5XJic X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM4PR12MB5229.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(19627235002)(508600001)(966005)(8936002)(6486002)(186003)(36756003)(53546011)(2616005)(8676002)(26005)(16576012)(4326008)(83380400001)(956004)(86362001)(38100700002)(2906002)(31696002)(31686004)(66556008)(66476007)(66946007)(5660300002)(316002)(54906003)(15650500001)(43740500002)(45980500001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?WnNZWDlBMWdYUER0MC9meDNhekxQdG9ia0hTM1ZQazNDNXR0bHRFaGdCckV6?= =?utf-8?B?K2MxTjlQTnNWZWUwd2NtbXF5SEQ2dFBOSTgyczhoT280WnZORDc0Ymx5ajQ5?= =?utf-8?B?NTlZM3huRjFMRm5pTnBDd2swcnY3OTR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QW5kVHM3aVU5Z1ZFbkF6U2didDdHWWd2VUFHTzdzcVBN?= =?utf-8?B?NWFWYnpSc3RDckdXZWNsZzlWdTMyeVoyQjhiUGg2TDlldVZWNm9OTlBTSnZl?= =?utf-8?B?d0h0SzJXVUc4MkszY1hPL3hkMVkyVWh5dFcvRXJpQUJ0UGgveDlkZW5qVW9W?= =?utf-8?B?eDFoMHVKS2F1RnJHTjBOVEhwV0wxb2RxSGM3UHNGMHBBTnlsUlFqaFg5Vy8r?= =?utf-8?B?dHZPNWtFTk9NUU40WjBKc1Q2Y3BZZWRpakw3bWNObTFMT2ZBZzBvazRZbFJt?= =?utf-8?B?dnI2MUo5QU5yMUN6V0NMMTUrVkVac01aKzJEOTQ1TTNTTmxnZW14eWwzWWIy?= =?utf-8?B?WVNjRDNjakJsMitTRloza2p5V3Q3a2Rxb2RXT0d2dGEzQ0JTMzg1cHFLUTJ4?= =?utf-8?B?VUJuWTBDY1RKdHFTZlVlOWdBQjIzdUFwaHA0OHYwcnhZTVpxUzRpUU8wcmZS?= =?utf-8?B?ejJMbkhPSUtKc2ZQRzltcnhFN0tzT2ZyT3dLeUJhalRqL01UMDVKWHZPeDVY?= =?utf-8?B?djVLNTBuTzZ4WFJlY1BwU0xNamZEOGloNndPUFRJME5Dek41T282Syt0ZDEw?= =?utf-8?B?M1NOOHR0TW56Y3Z6WTcxUmRIZC8rc2JJRkk4RHVCanZZS0wrczkwRmdDWGlj?= =?utf-8?B?WWh0T25ValNOMDlseUNNRStGYjA1NnVFQXE0OU4wK08xdzdiS012VTJFQmwz?= =?utf-8?Q?LeV5AJliSYzdYS5hOgGgalINbC4LUeSQBLRv6qP?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 352249fc-ad53-424e-a46e-08d94d333d76 X-MS-Exchange-CrossTenant-AuthSource: DM4PR12MB5229.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2021 17:07:52.1493 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: zucsBpYE1zbGwxQ4XiGkoxDT3XHjeaBb7zi6sdRCxfQWzSux1xmppXtYAFD3q1Q5zFsIBqy+OuBvxyFU9XGYZQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB5535 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit On 7/22/21 12:52 AM, Min Xu wrote: > RFC: https://bugzilla.tianocore.org/show_bug.cgi?id=3429 > > In Tdx all CPUs "reset" to run on 32-bit protected mode with flat > descriptor (paging disabled). But in Non-Td guest the initial state of > CPUs is 16-bit real mode. To resolve this conflict, BITS 16/32 is used > in the very beginning of ResetVector. It will check the 32-bit protected > mode or 16-bit real mode, then jump to the corresponding entry point. > This is done in OvmfPkg/ResetVector/Ia16/ResetVectorVtf0.asm. > > ReloadFlat32.asm load the GDT and set the CR0, then jump to Flat-32 mode. > > InitTdx.asm is called to record the Tdx signature ('TDXG') and other tdx > information in a TDX_WORK_AREA which can be used by the other routines in > ResetVector. > > Init32.asm is 32-bit initialization code in OvmfPkg. It puts above > ReloadFlat32 and InitTdx together to do the initializaiton for Tdx. > > After that Tdx jumps to 64-bit long mode by doing following tasks: > 1. SetCr3ForPageTables64 > For OVMF, some initial page tables is built at: > PcdOvmfSecPageTablesBase - (PcdOvmfSecPageTablesBase + 0x6000) > This page table supports the 4-level page table. > But Tdx support 4-level and 5-level page table based on the CPU GPA width. > 48bit is 4-level paging, 52-bit is 5-level paging. > If 5-level page table is supported (GPAW is 52), then a top level > page directory pointers (1 * 256TB entry) is generated in the > TdxPageTable. > 2. Set Cr4 > Enable PAE. > 3. Adjust Cr3 > If GPAW is 48, then Cr3 is PT_ADDR (0). If GPAW is 52, then Cr3 is > TDX_PT_ADDR (0). > > Tdx MailBox [0x10, 0x800] is reserved for OS. So we initialize piece of this > area ([0x10, 0x20]) to record the Tdx flag ('TDXG') and other Tdx info so that > they can be used in the following flow. > > After all above is successfully done, Tdx jump to SecEntry. > > Cc: Ard Biesheuvel > Cc: Brijesh Singh > Cc: Erdem Aktas > Cc: James Bottomley > Cc: Jiewen Yao > Cc: Tom Lendacky > Signed-off-by: Min Xu > --- > OvmfPkg/ResetVector/Ia16/ResetVectorVtf0.asm | 21 ++++++++ > OvmfPkg/ResetVector/Ia32/Flat32ToFlat64.asm | 47 ++++++++++++++++ > OvmfPkg/ResetVector/Ia32/Init32.asm | 34 ++++++++++++ > OvmfPkg/ResetVector/Ia32/InitTdx.asm | 57 ++++++++++++++++++++ > OvmfPkg/ResetVector/Ia32/PageTables64.asm | 41 ++++++++++++++ > OvmfPkg/ResetVector/Ia32/ReloadFlat32.asm | 44 +++++++++++++++ > OvmfPkg/ResetVector/ResetVector.nasmb | 18 +++++++ > 7 files changed, 262 insertions(+) > create mode 100644 OvmfPkg/ResetVector/Ia32/Init32.asm > create mode 100644 OvmfPkg/ResetVector/Ia32/InitTdx.asm > create mode 100644 OvmfPkg/ResetVector/Ia32/ReloadFlat32.asm > > diff --git a/OvmfPkg/ResetVector/Ia16/ResetVectorVtf0.asm b/OvmfPkg/ResetVector/Ia16/ResetVectorVtf0.asm > index ac86ce69ebe8..a390ed81d021 100644 > --- a/OvmfPkg/ResetVector/Ia16/ResetVectorVtf0.asm > +++ b/OvmfPkg/ResetVector/Ia16/ResetVectorVtf0.asm > @@ -155,10 +155,31 @@ resetVector: > ; > ; This is where the processor will begin execution > ; > +; In IA32 we follow the standard reset vector flow. While in X64, Td guest > +; may be supported. Td guest requires the startup mode to be 32-bit > +; protected mode but the legacy VM startup mode is 16-bit real mode. > +; To make NASM generate such shared entry code that behaves correctly in > +; both 16-bit and 32-bit mode, more BITS directives are added. > +; > +%ifdef ARCH_IA32 > + > nop > nop > jmp EarlyBspInitReal16 > > +%else > + > + smsw ax > + test al, 1 > + jz .Real > +BITS 32 > + jmp Main32 > +BITS 16 > +.Real: > + jmp EarlyBspInitReal16 > + > +%endif > + > ALIGN 16 > > fourGigabytes: > diff --git a/OvmfPkg/ResetVector/Ia32/Flat32ToFlat64.asm b/OvmfPkg/ResetVector/Ia32/Flat32ToFlat64.asm > index c6d0d898bcd1..2206ca719593 100644 > --- a/OvmfPkg/ResetVector/Ia32/Flat32ToFlat64.asm > +++ b/OvmfPkg/ResetVector/Ia32/Flat32ToFlat64.asm > @@ -17,6 +17,9 @@ Transition32FlatTo64Flat: > > OneTimeCall SetCr3ForPageTables64 > > + cmp dword[TDX_WORK_AREA], 0x47584454 ; 'TDXG' > + jz TdxTransition32FlatTo64Flat > + Is the memory area guaranteed to be zeroed for legacy guests? Hopefully, this won't trip up a non-TDX guest with a false match (highly unlikely, though). > mov eax, cr4 > bts eax, 5 ; enable PAE > mov cr4, eax > @@ -65,10 +68,54 @@ EnablePaging: > bts eax, 31 ; set PG > mov cr0, eax ; enable paging > > + jmp _jumpTo64Bit > + > +; > +; Tdx Transition from 32Flat to 64Flat > +; > +TdxTransition32FlatTo64Flat: > + > + mov eax, cr4 > + bts eax, 5 ; enable PAE > + > + ; > + ; byte[TDX_WORK_AREA_PAGELEVEL5] holds the indicator whether 52bit is supported. > + ; if it is the case, need to set LA57 and use 5-level paging > + ; > + cmp byte[TDX_WORK_AREA_PAGELEVEL5], 0 > + jz .set_cr4 > + bts eax, 12 > +.set_cr4: > + mov cr4, eax > + mov ebx, cr3 > + > + ; > + ; if la57 is not set, we are ok > + ; if using 5-level paging, adjust top-level page directory > + ; > + bt eax, 12 > + jnc .set_cr3 > + mov ebx, TDX_PT_ADDR (0) > +.set_cr3: > + mov cr3, ebx > + > + mov eax, cr0 > + bts eax, 31 ; set PG > + mov cr0, eax ; enable paging If you clear ebx here... > + > +_jumpTo64Bit: > jmp LINEAR_CODE64_SEL:ADDR_OF(jumpTo64BitAndLandHere) > + > BITS 64 > jumpTo64BitAndLandHere: > > + ; > + ; For Td guest we are done and jump to the end > + ; > + mov eax, TDX_WORK_AREA > + cmp dword [eax], 0x47584454 ; 'TDXG' > + jz GoodCompare > + ... you can remove these instructions. You'll jump to InsnCompare and that check should succeed, right? Thanks, Tom > ; > ; Check if the second step of the SEV-ES mitigation is to be performed. > ;