1 ;------------------------------------------------------------------------------
5 ; Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
6 ; SPDX-License-Identifier: BSD-2-Clause-Patent
8 ;------------------------------------------------------------------------------
10 %define VM_GUEST_TDX 2
15 ; Check if it is Intel Tdx
17 ; Modified: EAX, EBX, ECX, EDX
19 ; If it is Intel Tdx, EAX is 1
20 ; If it is not Intel Tdx, EAX is 0
28 cmp ebx, 0x756e6547 ; "Genu"
30 cmp edx, 0x49656e69 ; "ineI"
32 cmp ecx, 0x6c65746e ; "ntel"
44 ; CPUID[0].EAX >= 0x21?
58 cmp ebx, 0x65746E49 ; "Inte"
60 cmp edx, 0x5844546C ; "lTDX"
62 cmp ecx, 0x20202020 ; " "
76 ; Initialize work area if it is Tdx guest. Detailed definition is in
77 ; OvmfPkg/Include/WorkArea.h.
78 ; BSP and APs all go here. Only BSP initialize this work area.
80 ; Param[in] EBX[5:0] CPU Supported GPAW (48 or 52)
81 ; Param[in] ESI[31:0] vCPU ID (BSP is 0, others are AP)
88 ; First check if it is Tdx
93 jz ExitInitTdxWorkarea
99 ; In Td guest, BSP/AP shares the same entry point
100 ; BSP builds up the page table, while APs shouldn't do the same task.
101 ; Instead, APs just leverage the page table which is built by BSP.
102 ; APs will wait until the page table is ready.
105 cmp byte[TDX_WORK_AREA_PGTBL_READY], 0
107 jmp ExitInitTdxWorkarea
111 ; Set Type of WORK_AREA_GUEST_TYPE so that the following code can use
114 mov byte[WORK_AREA_GUEST_TYPE], VM_GUEST_TDX
117 ; EBX[5:0] CPU supported GPA width
120 mov DWORD[TDX_WORK_AREA_GPAW], ebx
123 OneTimeCallRet InitTdxWorkarea
126 ; Load the GDT and set the CS/DS/ES/FS/GS/SS.
128 ; Modified: EAX, DS, ES, FS, GS, SS, CS
133 mov eax, ADDR_OF(gdtr)
136 jmp LINEAR_CODE_SEL:dword ADDR_OF(jumpToFlat32BitAndLandHere)
138 jumpToFlat32BitAndLandHere:
140 debugShowPostCode POSTCODE_32BIT_MODE
149 OneTimeCallRet ReloadFlat32
152 ; Tdx initialization after entering into ResetVector
154 ; Modified: EAX, EBX, ECX, EDX, EBP, EDI, ESP
158 ; First load the GDT and jump to Flat32 mode
160 OneTimeCall ReloadFlat32
163 ; Initialization of Tdx work area
165 OneTimeCall InitTdxWorkarea
167 OneTimeCallRet InitTdx
170 ; Check TDX features, TDX or TDX-BSP or TDX-APs?
172 ; By design TDX BSP is reponsible for initializing the PageTables.
173 ; After PageTables are ready, byte[TDX_WORK_AREA_PGTBL_READY] is set to 1.
174 ; APs will spin when byte[TDX_WORK_AREA_PGTBL_READY] is 0 until it is set to 1.
176 ; When this routine is run on TDX BSP, byte[TDX_WORK_AREA_PGTBL_READY] should be 0.
177 ; When this routine is run on TDX APs, byte[TDX_WORK_AREA_PGTBL_READY] should be 1.
182 ; 0-NonTdx, 1-TdxBsp, 2-TdxAps
184 CheckTdxFeaturesBeforeBuildPagetables:
186 cmp byte[WORK_AREA_GUEST_TYPE], VM_GUEST_TDX
190 mov al, byte[TDX_WORK_AREA_PGTBL_READY]
194 OneTimeCallRet CheckTdxFeaturesBeforeBuildPagetables
197 ; Set byte[TDX_WORK_AREA_PGTBL_READY] to 1
199 TdxPostBuildPageTables:
200 cmp byte[WORK_AREA_GUEST_TYPE], VM_GUEST_TDX
201 jne ExitTdxPostBuildPageTables
202 mov byte[TDX_WORK_AREA_PGTBL_READY], 1
204 ExitTdxPostBuildPageTables:
205 OneTimeCallRet TdxPostBuildPageTables
208 ; Check if TDX is enabled
212 ; If TDX is enabled then EAX will be 1
213 ; If TDX is disabled then EAX will be 0.
217 cmp byte[WORK_AREA_GUEST_TYPE], VM_GUEST_TDX
222 OneTimeCallRet IsTdxEnabled