Alioth Code Coverage

sev.rs0.00%

1// Copyright 2024 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::fmt::Debug;
16use std::fs::File;
17use std::os::fd::{AsFd, BorrowedFd, OwnedFd};
18use std::path::Path;
19
20use snafu::ResultExt;
21
22use crate::arch::sev::SevStatus;
23use crate::hv::kvm::kvm_error;
24use crate::hv::{Result, error};
25use crate::sys::sev::{SevCmd, SevIssueCmd, sev_issue_cmd};
26
27#[derive(Debug)]
28pub struct SevFd {
29 fd: OwnedFd,
30}
31
32impl AsFd for SevFd {
33 fn as_fd(&self) -> BorrowedFd<'_> {
34 self.fd.as_fd()
35 }
36}
37
38impl SevFd {
39 pub fn new(path: impl AsRef<Path>) -> Result<Self> {
40 let f = File::open(&path).context(kvm_error::OpenFile {
41 path: path.as_ref(),
42 })?;
43 let sev_fd = Self { fd: f.into() };
44 Ok(sev_fd)
45 }
46
47 #[allow(dead_code)]
48 pub fn issue_cmd<T>(&self, cmd: SevCmd, data: &mut T) -> Result<()> {
49 let mut req = SevIssueCmd {
50 cmd,
51 data: data as *mut T as _,
52 error: SevStatus::SUCCESS,
53 };
54 unsafe { sev_issue_cmd(&self.fd, &mut req) }.context(error::MemEncrypt)?;
55 if req.error != SevStatus::SUCCESS {
56 return error::SevErr { code: req.error }.fail();
57 }
58 Ok(())
59 }
60}
61