Skip to main content

sgxs/
loader.rs

1/* Copyright (c) Fortanix, Inc.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7use std::fmt::Debug;
8use std::os::raw::c_void;
9
10use abi::{Attributes, Miscselect, Sigstruct};
11use sgxs::SgxsRead;
12
13/// An owned `Tcs` is the only reference to a particular Thread Control
14/// Structure (TCS) in an enclave.
15pub trait Tcs: 'static + Debug + Send {
16    /// The address of the TCS.
17    fn address(&self) -> *mut c_void;
18}
19
20/// An enclave that's been loaded into memory.
21pub trait MappingInfo: 'static + Debug {
22    /// The base address of the enclave.
23    fn address(&self) -> *mut c_void;
24
25    /// The size of the enclave (ELRANGE).
26    fn size(&self) -> usize;
27}
28
29pub struct Mapping<T: Load + ?Sized> {
30    pub info: T::MappingInfo,
31    pub tcss: Vec<T::Tcs>,
32}
33
34/// An interface that is able to load an enclave into memory.
35pub trait Load {
36    type MappingInfo: MappingInfo;
37    type Tcs: Tcs;
38
39    /// Load an enclave.
40    ///
41    /// The enclave will be unloaded once all returned values are dropped.
42    fn load<R: SgxsRead>(
43        &mut self,
44        reader: &mut R,
45        sigstruct: &Sigstruct,
46        attributes: Attributes,
47        miscselect: Miscselect,
48    ) -> Result<Mapping<Self>, anyhow::Error>;
49}
50
51impl<T: Load> Load for &mut T {
52    type MappingInfo = T::MappingInfo;
53    type Tcs = T::Tcs;
54
55    /// Load an enclave.
56    ///
57    /// The enclave will be unloaded once all returned values are dropped.
58    fn load<R: SgxsRead>(
59        &mut self,
60        reader: &mut R,
61        sigstruct: &Sigstruct,
62        attributes: Attributes,
63        miscselect: Miscselect,
64    ) -> Result<Mapping<Self>, anyhow::Error> {
65        T::load(self, reader, sigstruct, attributes, miscselect).map(|Mapping { info, tcss }| Mapping { info, tcss } )
66    }
67}