tdx_guest_device/
protocol.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! The module includes the definitions of data structures according to TDX specification.
5
6use bitfield_struct::bitfield;
7use zerocopy::FromBytes;
8use zerocopy::Immutable;
9use zerocopy::IntoBytes;
10use zerocopy::KnownLayout;
11
12/// Ioctl type defined by Linux.
13pub const TDX_CMD_GET_REPORT0_IOC_TYPE: u8 = b'T';
14
15/// Size of the [`TdReport`].
16pub const TDX_REPORT_SIZE: usize = 0x400;
17
18/// Size of `report_data` member in [`ReportMac`].
19pub const TDX_REPORT_DATA_SIZE: usize = 64;
20
21/// Ioctl struct defined by Linux.
22#[repr(C)]
23pub struct TdxReportReq {
24    /// Report data to be included in the report.
25    pub report_data: [u8; TDX_REPORT_DATA_SIZE],
26    /// The output report.
27    pub td_report: TdReport,
28}
29
30/// Report structure.
31/// See `TDREPORT_STRUCT` in Table 3.29, "Intel TDX Module v1.5 ABI specification", March 2024.
32#[repr(C)]
33#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
34pub struct TdReport {
35    /// An instance of [`ReportMac`]
36    pub report_mac_struct: ReportMac,
37    /// An instance of [`TeeTcbInfo`].
38    pub tee_tcb_info: TeeTcbInfo,
39    /// Reserved
40    pub _reserved: [u8; 17],
41    /// An instance of [`TdInfo`].
42    pub td_info: TdInfo,
43}
44
45static_assertions::const_assert_eq!(TDX_REPORT_SIZE, size_of::<TdReport>());
46
47/// See `REPORTMACSTRUCT` in Table 3.31, "Intel TDX Module v1.5 ABI specification", March 2024.
48#[repr(C)]
49#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
50pub struct ReportMac {
51    /// Type header structure
52    pub report_type: ReportType,
53    /// Must be zero
54    pub _reserved0: [u8; 12],
55    /// CPU SVN
56    pub cpu_svn: [u8; 16],
57    /// SHA384 of [`TeeTcbInfo`]
58    pub tee_tcb_info_hash: [u8; 48],
59    /// SHA384 of [`TdInfo`] for TDX
60    pub tee_info_hash: [u8; 48],
61    /// A set of data used for communication between the caller and the target
62    pub report_data: [u8; TDX_REPORT_DATA_SIZE],
63    /// Must be zero
64    pub _reserved1: [u8; 32],
65    /// The MAC over above data.
66    pub mac: [u8; 32],
67}
68
69/// See `REPORTTYPE` in Table 3.32, "Intel TDX Module v1.5 ABI specification", March 2024.
70#[repr(C)]
71#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
72pub struct ReportType {
73    /// TEE type
74    /// 0x00: SGX
75    /// 0x81: TDX
76    pub tee_type: u8,
77    /// TEE type-specific subtype
78    /// 0: Standard TDX report
79    pub sub_type: u8,
80    /// TEE type-specific version
81    /// For TDX
82    ///    0: `TDINFO_STRUCT.SERVTD_HASH` is not used (all 0's)
83    ///    1: `TDINFO_STRUCT.SERVTD_HASH` is used
84    pub version: u8,
85    /// Must be zero
86    pub _reserved: u8,
87}
88
89/// See `TEE_TCB_INFO` in Table 3.29, "Intel TDX Module v1.5 ABI specification", March 2024.
90#[repr(C)]
91#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
92pub struct TeeTcbInfo {
93    /// Indicates which fields are valid.
94    /// Set to 0x301ff.
95    pub valid: [u8; 8],
96    /// [`TeeTcbSvn`] of the TDX module that created the TD on the current
97    /// platform.
98    pub tee_tcb_svn: TeeTcbSvn,
99    /// The measurement of the TDX module that created the TD on the
100    /// current platform.
101    pub mr_seam: [u8; 48],
102    /// Set to all 0's.
103    pub mr_signer_seam: [u8; 48],
104    /// Set to all 0's.
105    pub attributes: [u8; 8],
106    /// [`TeeTcbSvn`] of the current TDX module on the current platform.
107    pub tee_tcb_svn2: TeeTcbSvn,
108    /// Reserved
109    pub reserved: [u8; 95],
110}
111
112/// See `TEE_TCB_SVN` in Section 3.9.4, "Intel TDX Module v1.5 ABI specification", March 2024.
113#[repr(C)]
114#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
115pub struct TeeTcbSvn {
116    /// TDX module minor SVN
117    pub tdx_module_svn_minor: u8,
118    /// TDX module major SVN
119    pub tdx_module_svn_major: u8,
120    /// Microcode SE_SVN at the time the TDX module was loaded
121    pub seam_last_patch_svn: u8,
122    /// Reserved
123    pub _reserved: [u8; 13],
124}
125
126/// See `TDINFO_STRUCT` in Table 3.33, "Intel TDX Module v1.5 ABI specification", March 2024.
127#[repr(C)]
128#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
129pub struct TdInfo {
130    /// An instance of [`TdInfoBase`]
131    pub td_info_base: TdInfoBase,
132    /// Must be zero when `version` in [`ReportType`] is 0 or 1.
133    pub td_info_extension: [u8; 64],
134}
135
136/// Run-time extendable measurement register.
137pub type Rtmr = [u8; 48];
138
139/// See `ATTRIBUTES` in Table 3.9, "Intel TDX Module v1.5 ABI specification", March 2024.
140#[bitfield(u64)]
141#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
142pub struct TdAttributes {
143    #[bits(1)]
144    pub debug: bool,
145    #[bits(3)]
146    _reserved1: u8,
147    #[bits(1)]
148    pub hgs_plus_prof: bool,
149    #[bits(1)]
150    pub perf_prof: bool,
151    #[bits(1)]
152    pub pmt_prof: bool,
153    #[bits(9)]
154    _reserved2: u16,
155    #[bits(7)]
156    _reserved_p: u8,
157    #[bits(4)]
158    _reserved_n: u8,
159    #[bits(1)]
160    pub lass: bool,
161    #[bits(1)]
162    pub sept_ve_disable: bool,
163    #[bits(1)]
164    pub migratable: bool,
165    #[bits(1)]
166    pub pks: bool,
167    #[bits(1)]
168    pub kl: bool,
169    #[bits(24)]
170    _reserved3: u32,
171    #[bits(6)]
172    _reserved4: u32,
173    #[bits(1)]
174    pub tpa: bool,
175    #[bits(1)]
176    pub perfmon: bool,
177}
178
179/// See `TDINFO_BASE` in Table 3.34, "Intel TDX Module v1.5 ABI specification", March 2024.
180#[repr(C)]
181#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
182pub struct TdInfoBase {
183    /// TD's attributes
184    pub attributes: TdAttributes,
185    /// TD's XFAM
186    pub xfam: [u8; 8],
187    /// Measurement of the initial contents of the TDX in SHA384
188    pub mr_td: [u8; 48],
189    /// Software-defined ID for non-owner-defined configuration of the guest TD
190    /// in SHA384
191    pub mr_config_id: [u8; 48],
192    /// Software-defined ID for the guest TD's owner in SHA384
193    pub mr_owner: [u8; 48],
194    /// Software-defined ID for owner-defined configuration of the guest TD
195    /// in SHA384
196    pub mr_owner_config: [u8; 48],
197    /// Array of 4 [`Rtmr`]
198    pub rtmr: [Rtmr; 4],
199    /// SHA384 of the `TDINFO_STRUCTs` of bound service TDs if there is any.
200    pub servd_hash: [u8; 48],
201}