|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License.. |
| 17 | + |
| 18 | +use sgx_types::error::SgxResult; |
| 19 | + |
| 20 | +use crate::arch::{Layout, LayoutEntry}; |
| 21 | +use crate::edmm::mem::init_segment_emas; |
| 22 | +use crate::edmm::{PageInfo, PageType, ProtFlags}; |
| 23 | +use crate::emm::flags::AllocFlags; |
| 24 | +use crate::emm::interior::Alloc; |
| 25 | +use crate::emm::range::{RangeType, EMA_PROT_MASK}; |
| 26 | +use crate::enclave::MmLayout; |
| 27 | +use crate::{arch, emm::range::RM}; |
| 28 | + |
| 29 | +use super::{interior::init_alloc, range::init_range_manage, user::init_user_range}; |
| 30 | + |
| 31 | +pub fn init_emm(user_start: usize, user_end: usize) { |
| 32 | + // check user_start not equals to 0 |
| 33 | + init_user_range(user_start, user_end); |
| 34 | + init_range_manage(); |
| 35 | + init_alloc(); |
| 36 | +} |
| 37 | + |
| 38 | +pub fn init_rts_emas() -> SgxResult { |
| 39 | + init_segment_emas()?; |
| 40 | + init_rts_contexts_emas(arch::Global::get().layout_table(), 0)?; |
| 41 | + Ok(()) |
| 42 | +} |
| 43 | + |
| 44 | +fn init_rts_contexts_emas(table: &[Layout], offset: usize) -> SgxResult { |
| 45 | + unsafe { |
| 46 | + for (i, layout) in table.iter().enumerate() { |
| 47 | + if is_group_id!(layout.group.id) { |
| 48 | + let mut step = 0_usize; |
| 49 | + for _ in 0..layout.group.load_times { |
| 50 | + step += layout.group.load_step as usize; |
| 51 | + init_rts_contexts_emas(&table[i - layout.group.entry_count as usize..i], step)?; |
| 52 | + } |
| 53 | + } else { |
| 54 | + build_rts_context_emas(&layout.entry, offset)?; |
| 55 | + } |
| 56 | + } |
| 57 | + Ok(()) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +fn build_rts_context_emas(entry: &LayoutEntry, offset: usize) -> SgxResult { |
| 62 | + let rva = offset + (entry.rva as usize); |
| 63 | + assert!(is_page_aligned!(rva)); |
| 64 | + |
| 65 | + // TODO: not sure get_enclave_base() equal to elrange_base or image_base |
| 66 | + let addr = MmLayout::elrange_base() + (rva as usize); |
| 67 | + let size = (entry.page_count << arch::SE_PAGE_SHIFT) as usize; |
| 68 | + let mut range_manage = RM.get().unwrap().lock(); |
| 69 | + |
| 70 | + // entry is guard page or has EREMOVE, build a reserved ema |
| 71 | + if (entry.si_flags == 0) || (entry.attributes & arch::PAGE_ATTR_EREMOVE != 0) { |
| 72 | + range_manage.init_static_region( |
| 73 | + addr, |
| 74 | + size, |
| 75 | + AllocFlags::RESERVED | AllocFlags::SYSTEM, |
| 76 | + PageInfo { |
| 77 | + typ: PageType::None, |
| 78 | + prot: ProtFlags::NONE, |
| 79 | + }, |
| 80 | + None, |
| 81 | + None, |
| 82 | + )?; |
| 83 | + return Ok(()); |
| 84 | + } |
| 85 | + |
| 86 | + let post_remove = (entry.attributes & arch::PAGE_ATTR_POST_REMOVE) != 0; |
| 87 | + let post_add = (entry.attributes & arch::PAGE_ATTR_POST_ADD) != 0; |
| 88 | + let static_min = (entry.attributes & arch::PAGE_ATTR_EADD) != 0; |
| 89 | + |
| 90 | + if post_remove { |
| 91 | + // TODO: maybe AllocFlags need more flags or PageType is not None |
| 92 | + range_manage.init_static_region( |
| 93 | + addr, |
| 94 | + size, |
| 95 | + AllocFlags::SYSTEM, |
| 96 | + PageInfo { |
| 97 | + typ: PageType::None, |
| 98 | + prot: ProtFlags::R | ProtFlags::W, |
| 99 | + }, |
| 100 | + None, |
| 101 | + None, |
| 102 | + )?; |
| 103 | + |
| 104 | + range_manage.dealloc(addr, size, RangeType::Rts)?; |
| 105 | + } |
| 106 | + |
| 107 | + if post_add { |
| 108 | + let commit_direction = if entry.id == arch::LAYOUT_ID_STACK_MAX |
| 109 | + || entry.id == arch::LAYOUT_ID_STACK_DYN_MAX |
| 110 | + || entry.id == arch::LAYOUT_ID_STACK_DYN_MIN |
| 111 | + { |
| 112 | + AllocFlags::GROWSDOWN |
| 113 | + } else { |
| 114 | + AllocFlags::GROWSUP |
| 115 | + }; |
| 116 | + |
| 117 | + // TODO: revise alloc and not use int |
| 118 | + range_manage.alloc_inner( |
| 119 | + Some(addr), |
| 120 | + size, |
| 121 | + AllocFlags::COMMIT_ON_DEMAND |
| 122 | + | commit_direction |
| 123 | + | AllocFlags::SYSTEM |
| 124 | + | AllocFlags::FIXED, |
| 125 | + PageInfo { |
| 126 | + typ: PageType::Reg, |
| 127 | + prot: ProtFlags::R | ProtFlags::W, |
| 128 | + }, |
| 129 | + None, |
| 130 | + None, |
| 131 | + RangeType::Rts, |
| 132 | + Alloc::Reserve, |
| 133 | + )?; |
| 134 | + } else if static_min { |
| 135 | + let info = if entry.id == arch::LAYOUT_ID_TCS { |
| 136 | + PageInfo { |
| 137 | + typ: PageType::Tcs, |
| 138 | + prot: ProtFlags::NONE, |
| 139 | + } |
| 140 | + } else { |
| 141 | + PageInfo { |
| 142 | + typ: PageType::Reg, |
| 143 | + prot: ProtFlags::from_bits_truncate( |
| 144 | + (entry.si_flags as usize & EMA_PROT_MASK) as u8, |
| 145 | + ), |
| 146 | + } |
| 147 | + }; |
| 148 | + range_manage.init_static_region(addr, size, AllocFlags::SYSTEM, info, None, None)?; |
| 149 | + } |
| 150 | + |
| 151 | + Ok(()) |
| 152 | +} |
0 commit comments