]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/TdxLib/TdInfo.c
MdePkg: Add TdxLib to wrap Tdx operations
[mirror_edk2.git] / MdePkg / Library / TdxLib / TdInfo.c
CommitLineData
c3001cb7
MX
1/** @file\r
2\r
3 Fetch the Tdx info.\r
4\r
5 Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
7\r
8**/\r
9\r
10#include <Library/BaseLib.h>\r
11#include <Library/DebugLib.h>\r
12#include <IndustryStandard/Tdx.h>\r
13#include <Uefi/UefiBaseType.h>\r
14#include <Library/TdxLib.h>\r
15#include <Library/BaseMemoryLib.h>\r
16\r
17UINT64 mTdSharedPageMask = 0;\r
18UINT32 mTdMaxVCpuNum = 0;\r
19UINT32 mTdVCpuNum = 0;\r
20BOOLEAN mTdDataReturned = FALSE;\r
21\r
22/**\r
23 This function call TDCALL_TDINFO to get the TD_RETURN_DATA.\r
24 If the TDCALL is successful, populate below variables:\r
25 - mTdSharedPageMask\r
26 - mTdMaxVCpunum\r
27 - mTdVCpuNum\r
28 - mTdDataReturned\r
29\r
30 @return TRUE The TDCALL is successful and above variables are populated.\r
31 @return FALSE The TDCALL is failed. Above variables are not set.\r
32**/\r
33BOOLEAN\r
34GetTdInfo (\r
35 VOID\r
36 )\r
37{\r
38 UINT64 Status;\r
39 TD_RETURN_DATA TdReturnData;\r
40 UINT8 Gpaw;\r
41\r
42 Status = TdCall (TDCALL_TDINFO, 0, 0, 0, &TdReturnData);\r
43 if (Status == TDX_EXIT_REASON_SUCCESS) {\r
44 Gpaw = (UINT8)(TdReturnData.TdInfo.Gpaw & 0x3f);\r
45 mTdSharedPageMask = 1ULL << (Gpaw - 1);\r
46 mTdMaxVCpuNum = TdReturnData.TdInfo.MaxVcpus;\r
47 mTdVCpuNum = TdReturnData.TdInfo.NumVcpus;\r
48 mTdDataReturned = TRUE;\r
49 } else {\r
50 DEBUG ((DEBUG_ERROR, "Failed call TDCALL_TDINFO. %llx\n", Status));\r
51 mTdDataReturned = FALSE;\r
52 }\r
53\r
54 return mTdDataReturned;\r
55}\r
56\r
57/**\r
58 This function gets the Td guest shared page mask.\r
59\r
60 The guest indicates if a page is shared using the Guest Physical Address\r
61 (GPA) Shared (S) bit. If the GPA Width(GPAW) is 48, the S-bit is bit-47.\r
62 If the GPAW is 52, the S-bit is bit-51.\r
63\r
64 @return Shared page bit mask\r
65**/\r
66UINT64\r
67EFIAPI\r
68TdSharedPageMask (\r
69 VOID\r
70 )\r
71{\r
72 if (mTdDataReturned) {\r
73 return mTdSharedPageMask;\r
74 }\r
75\r
76 return GetTdInfo () ? mTdSharedPageMask : 0;\r
77}\r
78\r
79/**\r
80 This function gets the maximum number of Virtual CPUs that are usable for\r
81 Td Guest.\r
82\r
83 @return maximum Virtual CPUs number\r
84**/\r
85UINT32\r
86EFIAPI\r
87TdMaxVCpuNum (\r
88 VOID\r
89 )\r
90{\r
91 if (mTdDataReturned) {\r
92 return mTdMaxVCpuNum;\r
93 }\r
94\r
95 return GetTdInfo () ? mTdMaxVCpuNum : 0;\r
96}\r
97\r
98/**\r
99 This function gets the number of Virtual CPUs that are usable for Td\r
100 Guest.\r
101\r
102 @return Virtual CPUs number\r
103**/\r
104UINT32\r
105EFIAPI\r
106TdVCpuNum (\r
107 VOID\r
108 )\r
109{\r
110 if (mTdDataReturned) {\r
111 return mTdVCpuNum;\r
112 }\r
113\r
114 return GetTdInfo () ? mTdVCpuNum : 0;\r
115}\r