Skip to main content

csv_rs/api/platform/
ioctl.rs

1// Copyright (C) Hygon Info Technologies Ltd.
2//
3// SPDX-License-Identifier: Apache-2.0
4
5//! A collection of type-safe ioctl implementations for the Hygon China Secure Virtualization
6//! (CSV) platform. These ioctls are exported by the Linux kernel.
7
8use super::types::*;
9use crate::impl_const_id;
10use iocuddle::{Group, Ioctl, WriteRead};
11use std::marker::PhantomData;
12
13// These enum ordinal values are defined in the Linux kernel
14// source code: include/uapi/linux/psp-sev.h
15impl_const_id! {
16    pub Id => u32;
17
18    PlatformReset = 0x0,
19    PlatformStatus = 0x1,
20    PekGen = 0x2,
21    PekCsr<'_> = 0x3,
22    PdhGen = 0x4,
23    PdhCertExport<'_> = 0x5,
24    PekCertImport<'_> = 0x6,
25    GetId<'_> = 0x8, /* GET_ID2 is 0x8, the deprecated GET_ID ioctl is 0x7 */
26}
27
28const CSV: Group = Group::new(b'S');
29
30/// Resets the CSV platform's persistent state.
31pub const PLATFORM_RESET: Ioctl<WriteRead, &Command<PlatformReset>> = unsafe { CSV.write_read(0) };
32
33/// Gathers a status report from the CSV firmware.
34pub const PLATFORM_STATUS: Ioctl<WriteRead, &Command<PlatformStatus>> =
35    unsafe { CSV.write_read(0) };
36
37/// Generate a new Platform Endorsement Key (PEK).
38pub const PEK_GEN: Ioctl<WriteRead, &Command<PekGen>> = unsafe { CSV.write_read(0) };
39
40/// Take ownership of the platform.
41pub const PEK_CSR: Ioctl<WriteRead, &Command<PekCsr<'_>>> = unsafe { CSV.write_read(0) };
42
43/// (Re)generate the Platform Diffie-Hellman (PDH).
44pub const PDH_GEN: Ioctl<WriteRead, &Command<PdhGen>> = unsafe { CSV.write_read(0) };
45
46/// Retrieve the PDH and the platform certificate chain.
47pub const PDH_CERT_EXPORT: Ioctl<WriteRead, &Command<PdhCertExport<'_>>> =
48    unsafe { CSV.write_read(0) };
49
50/// Join the platform to the domain.
51pub const PEK_CERT_IMPORT: Ioctl<WriteRead, &Command<PekCertImport<'_>>> =
52    unsafe { CSV.write_read(0) };
53
54/// Get the CPU's unique ID that can be used for getting a certificate for the CEK public key.
55pub const GET_ID: Ioctl<WriteRead, &Command<GetId<'_>>> = unsafe { CSV.write_read(0) };
56
57/// The Rust-flavored, FFI-friendly version of `struct sev_issue_cmd` which is
58/// used to pass arguments to the CSV ioctl implementation.
59///
60/// This struct is defined in the Linux kernel: include/uapi/linux/psp-sev.h
61#[repr(C, packed)]
62pub struct Command<'a, T: Id> {
63    pub code: u32,
64    pub data: u64,
65    pub error: u32,
66    _phantom: PhantomData<&'a T>,
67}
68
69impl<'a, T: Id> Command<'a, T> {
70    /// Create an CSV command with the expectation that the host platform/kernel will write to
71    /// the caller's address space either to the data held in the `Command.subcmd` field or some
72    /// other region specified by the `Command.subcmd` field.
73    pub fn from_mut(subcmd: &'a mut T) -> Self {
74        Command {
75            code: T::ID,
76            data: subcmd as *mut T as u64,
77            error: 0,
78            _phantom: PhantomData,
79        }
80    }
81
82    /// Create an CSV command with the expectation that the host platform/kernel *WILL NOT* mutate
83    /// the caller's address space in its response. Note: this does not actually prevent the host
84    /// platform/kernel from writing to the caller's address space if it wants to. This is primarily
85    /// a semantic tool for programming against the CSV ioctl API.
86    pub fn from(subcmd: &'a T) -> Self {
87        Command {
88            code: T::ID,
89            data: subcmd as *const T as u64,
90            error: 0,
91            _phantom: PhantomData,
92        }
93    }
94}