1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License..

use crate::arch;
use crate::enclave::parse;
use crate::error;
use crate::feature::SysFeatures;
use core::mem::{self, MaybeUninit};
use core::ptr;
use sgx_types::marker::ContiguousMemory;
use sgx_types::types::ProtectPerm;

extern "C" {
    static __ImageBase: u8;
}

pub struct MmLayout;

impl MmLayout {
    #[inline(always)]
    pub fn image_base() -> usize {
        unsafe { &__ImageBase as *const _ as usize }
    }

    #[inline]
    pub fn image_size() -> usize {
        arch::Global::get().enclave_size
    }

    #[inline]
    pub fn elrange_base() -> usize {
        Image::get().elrange_base
    }

    #[inline]
    pub fn elrange_size() -> usize {
        Image::get().elrange_size
    }

    #[inline]
    pub fn entry_address() -> usize {
        Image::get().entry_address
    }

    #[inline]
    pub fn heap_base() -> usize {
        Heap::get_or_init().base
    }

    #[inline]
    pub fn heap_min_size() -> usize {
        Heap::get_or_init().min_size
    }

    #[inline]
    pub fn heap_size() -> usize {
        Heap::get_or_init().size
    }

    #[inline]
    pub fn rsrvmem_base() -> usize {
        RsrvMem::get_or_init().base
    }

    #[inline]
    pub fn rsrvmem_min_size() -> usize {
        RsrvMem::get_or_init().min_size
    }

    #[inline]
    pub fn rsrvmem_size() -> usize {
        RsrvMem::get_or_init().size
    }

    #[inline]
    pub fn rsrvmm_default_perm() -> ProtectPerm {
        RsrvMem::get_or_init().perm
    }
}

#[derive(Clone, Copy, Default, Debug)]
pub struct Image {
    pub image_base: usize,
    pub image_size: usize,
    pub elrange_base: usize,
    pub elrange_size: usize,
    pub entry_address: usize,
}

#[link_section = ".data.rel.ro"]
static mut IMAGE: MaybeUninit<Image> = MaybeUninit::uninit();

impl Image {
    pub fn init() {
        let mut image = unsafe { IMAGE.assume_init_mut() };
        image.image_base = Self::image_base();
        image.image_size = Self::image_size();
        image.elrange_base = Self::elrange_base();
        image.elrange_size = Self::elrange_size();
        image.entry_address = Self::entry_address();
    }

    #[inline]
    pub fn get() -> &'static Image {
        unsafe { IMAGE.assume_init_ref() }
    }

    #[inline]
    fn image_base() -> usize {
        unsafe { &__ImageBase as *const _ as usize }
    }

    #[inline]
    fn image_size() -> usize {
        arch::Global::get().enclave_size
    }

    #[inline]
    fn elrange_base() -> usize {
        let global_data = arch::Global::get();

        if global_data.enclave_image_base != 0 {
            if global_data.enclave_image_base as usize != Self::image_base() {
                error::abort();
            }
            global_data.elrange_start_base as usize
        } else {
            Self::image_base()
        }
    }

    #[inline]
    fn elrange_size() -> usize {
        arch::Global::get().elrange_size as usize
    }

    #[inline]
    fn entry_address() -> usize {
        let elf = match parse::new_elf64() {
            Ok(elf) => elf,
            Err(_) => return 0,
        };
        Self::image_base() + elf.header2.entry_point as usize
    }
}

#[derive(Clone, Copy, Default, Debug)]
pub struct Heap {
    pub base: usize,
    pub size: usize,
    pub min_size: usize,
}

static mut HEAP: Option<Heap> = None;

impl Heap {
    pub fn get_or_init() -> &'static Heap {
        unsafe {
            if let Some(ref heap) = HEAP {
                heap
            } else {
                HEAP = Some(Heap {
                    base: Self::base(),
                    size: Self::size(),
                    min_size: Self::min_size(),
                });
                HEAP.as_ref().unwrap()
            }
        }
    }

    #[inline]
    fn base() -> usize {
        MmLayout::image_base() + arch::Global::get().heap_offset
    }

    fn size() -> usize {
        let mut size = arch::Global::get().heap_size;
        if SysFeatures::get().is_edmm() {
            let layout_table = arch::Global::get().layout_table();
            size += layout_table
                .iter()
                .find(|layout| unsafe { layout.entry.id == arch::LAYOUT_ID_HEAP_MAX })
                .map(|layout| unsafe { (layout.entry.page_count as usize) << arch::SE_PAGE_SHIFT })
                .unwrap_or(0);
        }
        size
    }

    fn min_size() -> usize {
        let layout_table = arch::Global::get().layout_table();
        layout_table
            .iter()
            .find(|layout| unsafe { layout.entry.id == arch::LAYOUT_ID_HEAP_MIN })
            .map(|layout| unsafe { (layout.entry.page_count as usize) << arch::SE_PAGE_SHIFT })
            .unwrap_or(0)
    }

    #[inline]
    pub fn zero_memory(&self) {
        cfg_if! {
            if #[cfg(any(feature = "sim", feature = "hyper"))] {
                let zero_size = crate::arch::Global::get().heap_size;
            } else {
                let zero_size = if SysFeatures::get().is_edmm() {
                    self.min_size
                } else {
                    self.size
                };
            }
        }
        unsafe {
            ptr::write_bytes(self.base as *mut u8, 0, zero_size);
        }
    }
}

#[derive(Clone, Copy, Default, Debug)]
pub struct RsrvMem {
    pub base: usize,
    pub size: usize,
    pub min_size: usize,
    pub perm: ProtectPerm,
}

static mut RSRV_MEM: Option<RsrvMem> = None;

impl RsrvMem {
    pub fn get_or_init() -> &'static RsrvMem {
        unsafe {
            if let Some(ref rsrvmem) = RSRV_MEM {
                rsrvmem
            } else {
                RSRV_MEM = Some(RsrvMem {
                    base: Self::base(),
                    size: Self::size(),
                    min_size: Self::min_size(),
                    perm: Self::default_perm(),
                });
                RSRV_MEM.as_ref().unwrap()
            }
        }
    }

    #[inline]
    fn base() -> usize {
        let offset = arch::Global::get().rsrv_offset;
        if offset != 0 {
            MmLayout::image_base() + offset
        } else {
            0
        }
    }

    fn size() -> usize {
        if arch::Global::get().rsrv_offset == 0 {
            return 0;
        }
        let mut size = arch::Global::get().rsrv_size;
        if SysFeatures::get().is_edmm() {
            let layout_table = arch::Global::get().layout_table();
            size += layout_table
                .iter()
                .find(|layout| unsafe { layout.entry.id == arch::LAYOUT_ID_RSRV_MAX })
                .map(|layout| unsafe { (layout.entry.page_count as usize) << arch::SE_PAGE_SHIFT })
                .unwrap_or(0);
        }
        size
    }

    fn min_size() -> usize {
        if arch::Global::get().rsrv_offset == 0 {
            return 0;
        }
        let layout_table = arch::Global::get().layout_table();
        layout_table
            .iter()
            .find(|layout| unsafe { layout.entry.id == arch::LAYOUT_ID_RSRV_MIN })
            .map(|layout| unsafe { (layout.entry.page_count as usize) << arch::SE_PAGE_SHIFT })
            .unwrap_or(0)
    }

    fn default_perm() -> ProtectPerm {
        if !SysFeatures::get().is_edmm() && Self::is_executable() {
            ProtectPerm::ReadWriteExec
        } else {
            ProtectPerm::ReadWrite
        }
    }

    #[inline]
    fn is_executable() -> bool {
        arch::Global::get().rsrv_executable != 0
    }

    pub fn check(&self) -> bool {
        if self.base == 0 {
            return true;
        }
        if !(is_page_aligned!(self.base)
            && is_page_aligned!(self.size)
            && is_page_aligned!(self.min_size))
        {
            return false;
        }
        if self.size > usize::MAX - self.base {
            return false;
        }
        true
    }

    #[inline]
    pub fn zero_memory(&self) {
        if self.base != 0 {
            cfg_if! {
                if #[cfg(any(feature = "sim", feature = "hyper"))] {
                    let zero_size = crate::arch::Global::get().rsrv_size;
                } else {
                    let zero_size = if SysFeatures::get().is_edmm() {
                        self.min_size
                    } else {
                        self.size
                    };
                }
            }
            unsafe {
                ptr::write_bytes(self.base as *mut u8, 0, zero_size);
            }
        }
    }
}

pub fn is_within_enclave(p: *const u8, len: usize) -> bool {
    let start = p as usize;
    let end = if len > 0 {
        if let Some(end) = start.checked_add(len - 1) {
            end
        } else {
            return false;
        }
    } else {
        start
    };
    let base = MmLayout::elrange_base();

    (start <= end) && (start >= base) && (end < base + MmLayout::elrange_size())
}

pub fn is_within_host(p: *const u8, len: usize) -> bool {
    let start = p as usize;
    let end = if len > 0 {
        if let Some(end) = start.checked_add(len - 1) {
            end
        } else {
            return false;
        }
    } else {
        start
    };
    let base = MmLayout::elrange_base();

    (start <= end) && ((end < base) || (start > base + MmLayout::elrange_size() - 1))
}

pub trait EnclaveRange {
    fn is_enclave_range(&self) -> bool;
    fn is_host_range(&self) -> bool;
}

impl<T> EnclaveRange for T
where
    T: Sized + ContiguousMemory,
{
    default fn is_enclave_range(&self) -> bool {
        is_within_enclave(self as *const _ as *const u8, mem::size_of::<T>())
    }

    default fn is_host_range(&self) -> bool {
        is_within_host(self as *const _ as *const u8, mem::size_of::<T>())
    }
}

impl<T> EnclaveRange for [T]
where
    T: Sized + ContiguousMemory,
{
    default fn is_enclave_range(&self) -> bool {
        is_within_enclave(
            self.as_ptr() as *const _ as *const u8,
            mem::size_of_val(self),
        )
    }

    default fn is_host_range(&self) -> bool {
        is_within_host(
            self.as_ptr() as *const _ as *const u8,
            mem::size_of_val(self),
        )
    }
}