Skip to main content

csv_rs/api/guest/
ioctl.rs

1// Copyright (C) Hygon Info Technologies Ltd.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6use iocuddle::{Group, Ioctl, WriteRead};
7
8use std::marker::PhantomData;
9
10use super::rtmr::CsvGuestUserRtmrSubcmd;
11
12pub enum CsvGuestIoctl {
13    GetReport = 0x1,
14    RtmrReq = 0x2,
15    _Undefined,
16}
17
18const CSV: Group = Group::new(b'D');
19
20/// # Attestation report ioctl interface
21pub const CSV_GET_REPORT: Ioctl<WriteRead, &GuestReportRequest> =
22    unsafe { CSV.write_read(CsvGuestIoctl::GetReport as u8) };
23
24/// The structure used for making guest report request to the PSP as a guest owner.
25/// This struct is defined in the Linux kernel: drivers/virt/coco/csv-guest/csv-guest.h
26#[repr(C)]
27pub struct GuestReportRequest<'a> {
28    /// Address of the data buffer with user REPORT_DATA included,
29    /// and CSV_REPORT output from PSP to be saved.
30    pub addr: u64,
31
32    /// The page aligned length of the buffer start from [`req_rsp_addr`]
33    pub len: u32,
34    _phantom: PhantomData<&'a ()>,
35}
36
37impl<'a> GuestReportRequest<'a> {
38    /// Creates a new report request from the adresses provided.
39    pub fn new(data: &'a [u8]) -> Self {
40        Self {
41            addr: data.as_ptr() as _,
42            len: data.len() as _,
43            _phantom: PhantomData,
44        }
45    }
46}
47
48/// Rtmr operations ioctl interface
49pub const CSV_RTMR_REQ: Ioctl<WriteRead, &GuestRtmrRequest> =
50    unsafe { CSV.write_read(CsvGuestIoctl::RtmrReq as u8) };
51
52/// The structure used for making guest rtmr request to the PSP as a guest owner.
53/// This struct is defined in the Linux kernel: drivers/virt/coco/csv-guest/csv-guest.h
54#[repr(C, packed)]
55pub struct GuestRtmrRequest<'a> {
56    /// Address of the rtmr subcommand buffer. This subcommand buffers request
57    /// info and saves the response data returned by the firmware.
58    pub buf: u64,
59    /// The length of the subcommand buffer.
60    pub len: u64,
61    /// The identifier of the rtmr subcommand.
62    pub subcmd_id: u16,
63    /// The reserved field, just for alignment.
64    pub rsvd: u16,
65    /// The return code that a rtmr subcommand returned by the firmware.
66    pub fw_error_code: u32,
67    _phantom: PhantomData<&'a ()>,
68}
69
70impl<'a> GuestRtmrRequest<'a> {
71    /// Create a new rtmr request.
72    pub fn new(subcmd_buf: &'a [u8], subcmd_id: CsvGuestUserRtmrSubcmd) -> Self {
73        Self {
74            buf: subcmd_buf.as_ptr() as _,
75            len: subcmd_buf.len() as _,
76            subcmd_id: subcmd_id as u16,
77            rsvd: 0,
78            fw_error_code: 0,
79            _phantom: PhantomData,
80        }
81    }
82
83    /// Return fw_error_code to the caller
84    pub fn get_fw_error_code(&self) -> u32 {
85        self.fw_error_code
86    }
87}