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
// 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 super::ecall::EntryTable;
use crate::arch::OCallContext;
use crate::tcs;
use core::convert::{From, Into, TryFrom};
use core::ffi::c_void;
use core::mem;
use core::num;
use core::ptr;
use sgx_types::error::{SgxResult, SgxStatus};

pub const OCALL_FLAG: usize = 0x4F434944;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum OCallIndex {
    OCall(i32),
    Trim,
    TrimCommit,
    Modpr,
    Mprotect,
}

impl OCallIndex {
    pub fn is_builtin_index(index: i32) -> bool {
        (-5..=-2).contains(&index)
    }

    pub fn is_builtin(&self) -> bool {
        !matches!(*self, OCallIndex::OCall(_))
    }

    pub fn is_ocall(&self) -> bool {
        match self {
            OCallIndex::OCall(n) => *n >= 0,
            _ => false,
        }
    }
}

impl TryFrom<i32> for OCallIndex {
    type Error = num::TryFromIntError;
    fn try_from(value: i32) -> Result<Self, Self::Error> {
        match value {
            v if v >= 0 => Ok(OCallIndex::OCall(v)),
            -2 => Ok(OCallIndex::Trim),
            -3 => Ok(OCallIndex::TrimCommit),
            -4 => Ok(OCallIndex::Modpr),
            -5 => Ok(OCallIndex::Mprotect),
            _ => Err(u8::try_from(256_u16).unwrap_err()),
        }
    }
}

impl From<OCallIndex> for i32 {
    #[link_section = ".nipx"]
    fn from(idx: OCallIndex) -> i32 {
        match idx {
            OCallIndex::OCall(n) => n,
            OCallIndex::Trim => -2,
            OCallIndex::TrimCommit => -3,
            OCallIndex::Modpr => -4,
            OCallIndex::Mprotect => -5,
        }
    }
}

pub fn ocall<T>(idx: OCallIndex, ms: Option<&mut T>) -> SgxResult {
    extern "C" {
        fn do_ocall(index: i32, ms: *mut c_void) -> u32;
    }

    let index = Into::<i32>::into(idx);
    if index > 0 && index as usize >= EntryTable::get().nr_ocall() {
        bail!(SgxStatus::InvalidFunction);
    }

    let ms = ms.map(|t| t as *mut _ as *mut _).unwrap_or(ptr::null_mut());
    let ret = unsafe { do_ocall(index, ms) };
    let status = SgxStatus::try_from(ret).unwrap_or(SgxStatus::Unexpected);
    if status.is_success() {
        Ok(())
    } else {
        Err(status)
    }
}

#[no_mangle]
pub unsafe extern "C" fn update_ocall_lastsp(context: &mut OCallContext) -> usize {
    let mut tc = tcs::current();
    let tds = tc.tds_mut();
    let last_sp = tds.last_sp;
    context.pre_last_sp = last_sp;

    if context.pre_last_sp == tds.stack_base {
        context.ocall_depth = 1;
    } else {
        // thread_data->last_sp is only set when ocall or exception handling occurs
        // ocall is block during exception handling, so last_sp is always ocall frame here
        let context_pre = &*(context.pre_last_sp as *const OCallContext);
        context.ocall_depth = context_pre.ocall_depth + 1;
    }
    tds.last_sp = context as *const _ as usize;

    last_sp
}

pub fn oret(ret: usize) -> SgxResult {
    extern "C" {
        fn asm_oret(sp: usize, ret: usize) -> u32;
    }

    let mut tc = tcs::current();
    let tds = tc.tds_mut();
    let last_sp = tds.last_sp;
    let context = unsafe { &*(tds.last_sp as *const OCallContext) };
    if last_sp == 0 || last_sp <= &context as *const _ as usize {
        bail!(SgxStatus::Unexpected);
    }

    // At least 1 ecall frame and 1 ocall frame are expected on stack.
    // 30 is an estimated value: 8 for enclave_entry and 22 for do_ocall.
    if last_sp > tds.stack_base - 30 * mem::size_of::<usize>() {
        bail!(SgxStatus::Unexpected);
    }
    if context.ocall_flag != OCALL_FLAG {
        bail!(SgxStatus::Unexpected);
    }

    if context.pre_last_sp > tds.stack_base || context.pre_last_sp <= context as *const _ as usize {
        bail!(SgxStatus::Unexpected);
    }

    tds.last_sp = context.pre_last_sp;

    unsafe { asm_oret(last_sp, ret) };

    // Should not come here
    Err(SgxStatus::Unexpected)
}