Alioth Code Coverage

tdx.rs0.00%

1// Copyright 2026 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::os::fd::OwnedFd;
16
17use snafu::ResultExt;
18
19use crate::hv::{Result, error};
20use crate::sys::kvm::kvm_memory_encrypt_op;
21use crate::sys::tdx::{KvmTdxCmd, KvmTdxCmdId};
22
23pub fn tdx_op<T>(fd: &OwnedFd, cmd: KvmTdxCmdId, flags: u32, data: Option<&mut T>) -> Result<()> {
24 let mut req = KvmTdxCmd {
25 id: cmd,
26 flags,
27 data: data.map(|d| d as *mut _ as _).unwrap_or(0),
28 hw_error: 0,
29 };
30 unsafe { kvm_memory_encrypt_op(fd, &mut req) }.context(error::MemEncrypt)?;
31 if req.hw_error != 0 {
32 return error::TdxErr { code: req.hw_error }.fail();
33 }
34 Ok(())
35}
36