]> git.lizzy.rs Git - rust.git/blob - library/std/src/os/fortanix_sgx/arch.rs
Rollup merge of #91192 - r00ster91:futuredocs, r=GuillaumeGomez
[rust.git] / library / std / src / os / fortanix_sgx / arch.rs
1 //! SGX-specific access to architectural features.
2 //!
3 //! The functionality in this module is further documented in the Intel
4 //! Software Developer's Manual, Volume 3, Chapter 40.
5 #![unstable(feature = "sgx_platform", issue = "56975")]
6
7 use crate::mem::MaybeUninit;
8 use core::arch::asm;
9
10 /// Wrapper struct to force 16-byte alignment.
11 #[repr(align(16))]
12 #[unstable(feature = "sgx_platform", issue = "56975")]
13 pub struct Align16<T>(pub T);
14
15 /// Wrapper struct to force 128-byte alignment.
16 #[repr(align(128))]
17 #[unstable(feature = "sgx_platform", issue = "56975")]
18 pub struct Align128<T>(pub T);
19
20 /// Wrapper struct to force 512-byte alignment.
21 #[repr(align(512))]
22 #[unstable(feature = "sgx_platform", issue = "56975")]
23 pub struct Align512<T>(pub T);
24
25 const ENCLU_EREPORT: u32 = 0;
26 const ENCLU_EGETKEY: u32 = 1;
27
28 /// Call the `EGETKEY` instruction to obtain a 128-bit secret key.
29 #[unstable(feature = "sgx_platform", issue = "56975")]
30 pub fn egetkey(request: &Align512<[u8; 512]>) -> Result<Align16<[u8; 16]>, u32> {
31     unsafe {
32         let mut out = MaybeUninit::uninit();
33         let error;
34
35         asm!(
36             // rbx is reserved by LLVM
37             "xchg %rbx, {0}",
38             "enclu",
39             "mov {0}, %rbx",
40             inout(reg) request => _,
41             inlateout("eax") ENCLU_EGETKEY => error,
42             in("rcx") out.as_mut_ptr(),
43             options(att_syntax, nostack),
44         );
45
46         match error {
47             0 => Ok(out.assume_init()),
48             err => Err(err),
49         }
50     }
51 }
52
53 /// Call the `EREPORT` instruction.
54 ///
55 /// This creates a cryptographic report describing the contents of the current
56 /// enclave. The report may be verified by the enclave described in
57 /// `targetinfo`.
58 #[unstable(feature = "sgx_platform", issue = "56975")]
59 pub fn ereport(
60     targetinfo: &Align512<[u8; 512]>,
61     reportdata: &Align128<[u8; 64]>,
62 ) -> Align512<[u8; 432]> {
63     unsafe {
64         let mut report = MaybeUninit::uninit();
65
66         asm!(
67             // rbx is reserved by LLVM
68             "xchg %rbx, {0}",
69             "enclu",
70             "mov {0}, %rbx",
71             inout(reg) targetinfo => _,
72             in("eax") ENCLU_EREPORT,
73             in("rcx") reportdata,
74             in("rdx") report.as_mut_ptr(),
75             options(att_syntax, preserves_flags, nostack),
76         );
77
78         report.assume_init()
79     }
80 }