summaryrefslogtreecommitdiff
path: root/MdePkg/Library/TdxLib/TdInfo.c
blob: 3c5689f1d8c6e4870966247c752ef3f1eb5545ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/** @file

  Fetch the Tdx info.

  Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <IndustryStandard/Tdx.h>
#include <Uefi/UefiBaseType.h>
#include <Library/TdxLib.h>
#include <Library/BaseMemoryLib.h>

UINT64   mTdSharedPageMask = 0;
UINT32   mTdMaxVCpuNum     = 0;
UINT32   mTdVCpuNum        = 0;
BOOLEAN  mTdDataReturned   = FALSE;

/**
  This function call TDCALL_TDINFO to get the TD_RETURN_DATA.
  If the TDCALL is successful, populate below variables:
   - mTdSharedPageMask
   - mTdMaxVCpunum
   - mTdVCpuNum
   - mTdDataReturned

  @return TRUE  The TDCALL is successful and above variables are populated.
  @return FALSE The TDCALL is failed. Above variables are not set.
**/
BOOLEAN
GetTdInfo (
  VOID
  )
{
  UINT64          Status;
  TD_RETURN_DATA  TdReturnData;
  UINT8           Gpaw;

  Status = TdCall (TDCALL_TDINFO, 0, 0, 0, &TdReturnData);
  if (Status == TDX_EXIT_REASON_SUCCESS) {
    Gpaw              = (UINT8)(TdReturnData.TdInfo.Gpaw & 0x3f);
    mTdSharedPageMask = 1ULL << (Gpaw - 1);
    mTdMaxVCpuNum     = TdReturnData.TdInfo.MaxVcpus;
    mTdVCpuNum        = TdReturnData.TdInfo.NumVcpus;
    mTdDataReturned   = TRUE;
  } else {
    DEBUG ((DEBUG_ERROR, "Failed call TDCALL_TDINFO. %llx\n", Status));
    mTdDataReturned = FALSE;
  }

  return mTdDataReturned;
}

/**
  This function gets the Td guest shared page mask.

  The guest indicates if a page is shared using the Guest Physical Address
  (GPA) Shared (S) bit. If the GPA Width(GPAW) is 48, the S-bit is bit-47.
  If the GPAW is 52, the S-bit is bit-51.

  @return Shared page bit mask
**/
UINT64
EFIAPI
TdSharedPageMask (
  VOID
  )
{
  if (mTdDataReturned) {
    return mTdSharedPageMask;
  }

  return GetTdInfo () ? mTdSharedPageMask : 0;
}

/**
  This function gets the maximum number of Virtual CPUs that are usable for
  Td Guest.

  @return maximum Virtual CPUs number
**/
UINT32
EFIAPI
TdMaxVCpuNum (
  VOID
  )
{
  if (mTdDataReturned) {
    return mTdMaxVCpuNum;
  }

  return GetTdInfo () ? mTdMaxVCpuNum : 0;
}

/**
  This function gets the number of Virtual CPUs that are usable for Td
  Guest.

  @return Virtual CPUs number
**/
UINT32
EFIAPI
TdVCpuNum (
  VOID
  )
{
  if (mTdDataReturned) {
    return mTdVCpuNum;
  }

  return GetTdInfo () ? mTdVCpuNum : 0;
}