sev.rs0.00%
1
// Copyright 2024 Google LLC2
//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 at6
//7
// https://www.apache.org/licenses/LICENSE-2.08
//9
// Unless required by applicable law or agreed to in writing, software10
// 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 and13
// limitations under the License.14
15
use std::fmt::Debug;16
use std::fs::File;17
use std::os::fd::{AsFd, BorrowedFd, OwnedFd};18
use std::path::Path;19
20
use snafu::ResultExt;21
22
use crate::arch::sev::SevStatus;23
use crate::hv::kvm::kvm_error;24
use crate::hv::{Result, error};25
use crate::sys::sev::{SevCmd, SevIssueCmd, sev_issue_cmd};26
27
#[derive(Debug)]28
pub struct SevFd {29
fd: OwnedFd,30
}31
32
impl AsFd for SevFd {33
fn as_fd(&self) -> BorrowedFd<'_> {34
self.fd.as_fd()35
}36
}37
38
impl 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