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