Skip to content

Commit

Permalink
tdx-tdcall: fix TdCallError parsing
Browse files Browse the repository at this point in the history
According to the ABI spec, TdCallError includes:

1) Flags, Class and Name (Bits 63:32)
2) Details L2 (Bits 31:0)

Fix the logic in fn from(val: u64) to correctly parse both parts of the
function completion status. Include details L2 as part of the enum.

Signed-off-by: Peter Fang <[email protected]>
  • Loading branch information
peterfang authored and jyao1 committed Jul 23, 2024
1 parent 97ed5b9 commit ea30d1c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
10 changes: 5 additions & 5 deletions tdx-tdcall/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ pub enum TdCallError {
TdxExitInvalidParameters,

// The operand is busy (e.g., it is locked in Exclusive mode)
TdxExitReasonOperandBusy,
TdxExitReasonOperandBusy(u32),

// Operand is invalid (e.g., illegal leaf number)
TdxExitReasonOperandInvalid,
TdxExitReasonOperandInvalid(u32),

// Error code defined by individual leaf function
LeafSpecific(u64),
Expand All @@ -174,9 +174,9 @@ pub enum TdCallError {
// TDCALL Completion Status Codes (Returned in RAX) Definition
impl From<u64> for TdCallError {
fn from(val: u64) -> Self {
match val {
0x8000_0200 => Self::TdxExitReasonOperandBusy,
0xC000_0100 => Self::TdxExitReasonOperandInvalid,
match val >> 32 {
0x8000_0200 => Self::TdxExitReasonOperandBusy(val as u32),
0xC000_0100 => Self::TdxExitReasonOperandInvalid(val as u32),
_ => Self::LeafSpecific(val),
}
}
Expand Down
20 changes: 11 additions & 9 deletions tdx-tdcall/src/tdx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,12 @@ pub fn tdcall_accept_page(address: u64) -> Result<(), TdCallError> {

if ret == TDCALL_STATUS_SUCCESS {
return Ok(());
} else if TdCallError::TdxExitReasonOperandBusy != ret.into() {
return Err(ret.into());
} else {
match TdCallError::from(ret) {
TdCallError::TdxExitReasonOperandBusy(_) => retry_counter += 1,
e => return Err(e),
}
}

retry_counter += 1;
}

return Err(ret.into());
Expand Down Expand Up @@ -618,7 +619,7 @@ pub fn td_accept_pages(address: u64, pages: u64, page_size: u64) {
}
}
panic!(
"Accept Page Error: 0x{:x}, page_size: {}, err {:?}\n",
"Accept Page Error: 0x{:x}, page_size: {}, err {:x?}\n",
accept_addr, page_size, e
);
}
Expand Down Expand Up @@ -928,11 +929,12 @@ pub fn tdcall_mem_page_attr_wr(

if ret == TDCALL_STATUS_SUCCESS {
return Ok((args.rcx, args.rdx));
} else if TdCallError::TdxExitReasonOperandBusy != ret.into() {
return Err(ret.into());
} else {
match TdCallError::from(ret) {
TdCallError::TdxExitReasonOperandBusy(_) => retry_counter += 1,
e => return Err(e),
}
}

retry_counter += 1;
}

return Err(ret.into());
Expand Down

0 comments on commit ea30d1c

Please sign in to comment.