Skip to content

Commit

Permalink
tdx-tdcall: Add tdcall to support memory attribute write
Browse files Browse the repository at this point in the history
Add `tdcall_mem_page_attr_wr` interface to write memory attributes
of a private page. It is also used to create or remove L2 page
aliases as required.

Signed-off-by: Chuanxiao Dong <[email protected]>
Signed-off-by: Vijay Dhanraj <[email protected]>
  • Loading branch information
vijaydhanraj committed Jun 18, 2024
1 parent 5741b67 commit 1c8106f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions tdx-tdcall/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const TDCALL_VP_WR: u64 = 10;
const TDCALL_SYS_RD: u64 = 11;
const TDCALL_SERVTD_RD: u64 = 18;
const TDCALL_SERVTD_WR: u64 = 20;
const TDCALL_MEM_PAGE_ATTR_WR: u64 = 24;
const TDCALL_VP_ENTER: u64 = 25;
const TDCALL_VP_INVEPT: u64 = 26;
const TDCALL_VP_INVVPID: u64 = 27;
Expand Down
35 changes: 35 additions & 0 deletions tdx-tdcall/src/tdx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,41 @@ pub fn tdcall_vm_write(field: u64, value: u64, mask: u64) -> Result<u64, TdCallE
Ok(args.r8)
}

/// Write the attributes of a private page. Create or remove L2 page aliases as required.
///
/// Details can be found in TDX Module v1.5 ABI spec section 'TDG.MEM.PAGE.ATTR.WR Leaf'.
pub fn tdcall_mem_page_attr_wr(
gpa_mapping: u64,
gpa_attr: u64,
attr_flags: u64,
) -> Result<(u64, u64), TdCallError> {
let mut args = TdcallArgs {
rax: TDCALL_MEM_PAGE_ATTR_WR,
rcx: gpa_mapping,
rdx: gpa_attr,
r8: attr_flags,
..Default::default()
};

const MAX_RETRIES_ATTR_WR: usize = 5;
let mut retry_counter = 0;
let mut ret = 0;

while retry_counter < MAX_RETRIES_ATTR_WR {
ret = td_call(&mut args);

if ret == TDCALL_STATUS_SUCCESS {
return Ok((args.rcx, args.rdx));
} else if TdCallError::TdxExitReasonOperandBusy != ret.into() {
return Err(ret.into());
}

retry_counter += 1;
}

return Err(ret.into());
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 1c8106f

Please sign in to comment.