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
// 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..

#[cfg(feature = "hyper")]
use crate::call::MsbufInfo;
use crate::enclave::EnclaveRange;
use crate::fence::lfence;
use crate::xsave;
use core::cmp;
use core::convert::TryFrom;
use core::mem;
use core::ptr;
use core::ptr::NonNull;
use sgx_types::cpu_features::*;
use sgx_types::error::{SgxResult, SgxStatus};
use sgx_types::marker::ContiguousMemory;
use sgx_types::types;

impl_enum! {
    #[repr(u32)]
    #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
    pub enum Version {
        Sdk1_5 = 0,
        Sdk2_0 = 1,
        Sdk2_1 = 2,
        Sdk2_2 = 3,
        Sdk2_3 = 4,
    }
}

#[repr(C, packed)]
#[derive(Clone, Copy, Debug)]
pub struct SystemFeatures {
    pub cpu_features: u64,
    pub version: u32,
    pub system_feature_set: [u64; 1],
    pub cpuinfo_table: [[u32; 4]; 8],
    pub sealed_key: usize,
    pub size: usize,
    pub cpu_features_ext: u64,
    pub cpu_core_num: u32,
    #[cfg(feature = "hyper")]
    pub msbuf_info: MsbufInfo,
}

unsafe impl ContiguousMemory for SystemFeatures {}

impl SystemFeatures {
    const SYS_FEATURE_EXTEND: u64 = 62;
    const EDMM_ENABLE_BIT: u64 = 1;

    pub unsafe fn from_raw(features: NonNull<SystemFeatures>) -> SgxResult<SystemFeatures> {
        ensure!(features.as_ref().is_host_range(), SgxStatus::Unexpected);
        lfence();

        let mut features = *features.as_ptr();
        features.clear_unused_fileds();

        #[cfg(feature = "hyper")]
        {
            use crate::inst::GlobalHyper;
            let msbuf_info = ptr::addr_of!(features.msbuf_info);
            GlobalHyper::get_mut().set_msbuf_info(&ptr::read_unaligned(msbuf_info))?;
        }

        Ok(features)
    }

    unsafe fn clear_unused_fileds(&mut self) {
        let offset =
            if (self.system_feature_set[0] & (1_u64 << SystemFeatures::SYS_FEATURE_EXTEND)) != 0 {
                cmp::min(self.size, mem::size_of::<Self>())
            } else {
                let b = self as *const _ as usize;
                let p = ptr::addr_of!(self.size) as usize;
                p - b
            };

        let remaining = mem::size_of::<Self>() - offset;
        if remaining > 0 {
            let p = self as *mut _ as *mut u8;
            ptr::write_bytes(p.add(offset), 0, remaining);
        }
    }

    pub fn is_edmm(&self) -> bool {
        match self.version {
            0 => false,
            _ => (self.system_feature_set[0] & SystemFeatures::EDMM_ENABLE_BIT) != 0,
        }
    }

    pub fn cpu_features_bit(&self, xfrm: u64) -> SgxResult<u64> {
        const XFEATURE_ENABLED_AVX: u64 = 0x06;
        const XFEATURE_ENABLED_AVX3: u64 = 0xE0;

        // Unset conflict cpu feature bits for legacy cpu features.
        let mut features_bit = self.cpu_features & (!INCOMPAT_FEATURE_BIT);
        if (self.system_feature_set[0] & (1_u64 << SystemFeatures::SYS_FEATURE_EXTEND)) != 0 {
            // The sys_features structure is collected by updated uRTS, so use the updated cpu features instead.
            features_bit = self.cpu_features_ext;
        }

        // Confirm the reserved bits and the unset bits by uRTS must be 0.
        if (features_bit & RESERVED_CPU_FEATURE_BIT) != 0 {
            // clear the reserved bits
            features_bit &= !RESERVED_CPU_FEATURE_BIT;
        }

        if (features_bit & (!(CPU_FEATURE_SSE4_1 - 1))) == 0 {
            bail!(SgxStatus::Unexpected);
        }

        // Check for inconsistencies in the CPUID feature mask.
        if (((features_bit & CPU_FEATURE_SSE) == CPU_FEATURE_SSE)
            && ((features_bit & (CPU_FEATURE_SSE - 1)) != (CPU_FEATURE_SSE - 1)))
            || (((features_bit & CPU_FEATURE_SSE2) == CPU_FEATURE_SSE2)
                && ((features_bit & (CPU_FEATURE_SSE2 - 1)) != (CPU_FEATURE_SSE2 - 1)))
            || (((features_bit & CPU_FEATURE_SSE3) == CPU_FEATURE_SSE3)
                && ((features_bit & (CPU_FEATURE_SSE3 - 1)) != (CPU_FEATURE_SSE3 - 1)))
            || (((features_bit & CPU_FEATURE_SSSE3) == CPU_FEATURE_SSSE3)
                && ((features_bit & (CPU_FEATURE_SSSE3 - 1)) != (CPU_FEATURE_SSSE3 - 1)))
            || (((features_bit & CPU_FEATURE_SSE4_1) == CPU_FEATURE_SSE4_1)
                && ((features_bit & (CPU_FEATURE_SSE4_1 - 1)) != (CPU_FEATURE_SSE4_1 - 1)))
            || (((features_bit & CPU_FEATURE_SSE4_2) == CPU_FEATURE_SSE4_2)
                && ((features_bit & (CPU_FEATURE_SSE4_2 - 1)) != (CPU_FEATURE_SSE4_2 - 1)))
        {
            bail!(SgxStatus::Unexpected);
        }

        // Determine whether the OS & ENCLAVE support SAVE/RESTORE of the AVX register set
        // IF NOT, clear the advanced feature set bits corresponding to AVX and beyond.
        if xfrm & XFEATURE_ENABLED_AVX != XFEATURE_ENABLED_AVX {
            // AVX is disabled by OS, so clear the AVX related feature bits
            features_bit &= !(CPU_FEATURE_AVX
                | CPU_FEATURE_VAES
                | CPU_FEATURE_VPCLMULQDQ
                | CPU_FEATURE_F16C
                | CPU_FEATURE_AVX2
                | CPU_FEATURE_FMA
                | CPU_FEATURE_MPX
                | CPU_FEATURE_RTM
                | CPU_FEATURE_HLE
                | CPU_FEATURE_BMI
                | CPU_FEATURE_RDSEED
                | CPU_FEATURE_ADX
                | CPU_FEATURE_AVX512F
                | CPU_FEATURE_AVX512CD
                | CPU_FEATURE_AVX512ER
                | CPU_FEATURE_AVX512PF
                | CPU_FEATURE_AVX512DQ
                | CPU_FEATURE_AVX512BW
                | CPU_FEATURE_AVX512VL
                | CPU_FEATURE_AVX512IFMA52
                | CPU_FEATURE_AVX512VBMI
                | CPU_FEATURE_AVX512_VPOPCNTDQ
                | CPU_FEATURE_AVX512_4VNNIW
                | CPU_FEATURE_AVX512_4FMAPS
                | CPU_FEATURE_AVX512_BITALG
                | CPU_FEATURE_AVX512_VBMI2
                | CPU_FEATURE_AVX512_VNNI);
        } else if xfrm & XFEATURE_ENABLED_AVX3 != XFEATURE_ENABLED_AVX3 {
            // AVX3 is disabled by OS, so clear the AVX related feature bits
            features_bit &= !(CPU_FEATURE_AVX512F
                | CPU_FEATURE_AVX512CD
                | CPU_FEATURE_AVX512ER
                | CPU_FEATURE_AVX512PF
                | CPU_FEATURE_AVX512DQ
                | CPU_FEATURE_AVX512BW
                | CPU_FEATURE_AVX512VL
                | CPU_FEATURE_AVX512IFMA52
                | CPU_FEATURE_AVX512VBMI
                | CPU_FEATURE_AVX512_VPOPCNTDQ
                | CPU_FEATURE_AVX512_4VNNIW
                | CPU_FEATURE_AVX512_4FMAPS
                | CPU_FEATURE_AVX512_BITALG
                | CPU_FEATURE_AVX512_VBMI2
                | CPU_FEATURE_AVX512_VNNI);
        }

        Ok(features_bit)
    }
}

#[derive(Debug)]
pub struct SysFeatures {
    version: Version,
    xfrm: u64,
    cpu_features: u64,
    cpu_core_num: u32,
    cpuinfo_table: [[u32; 4]; 8],
    is_edmm: bool,
}

unsafe impl ContiguousMemory for SysFeatures {}

#[link_section = ".data.rel.ro"]
static mut SYS_FEATURES: SysFeatures = SysFeatures {
    version: Version::Sdk1_5,
    xfrm: types::XFRM_LEGACY,
    cpu_features: 0,
    cpu_core_num: 0,
    cpuinfo_table: [[0; 4]; 8],
    is_edmm: false,
};

// Improve compatibility
// e.g. intel-sgx-ssl handles CPUID with this global variable.
#[link_section = ".data.rel.ro"]
#[no_mangle]
pub static mut g_cpu_feature_indicator: u64 = 0;

impl SysFeatures {
    pub fn init(raw: NonNull<SystemFeatures>) -> SgxResult<&'static SysFeatures> {
        let raw = unsafe { SystemFeatures::from_raw(raw) }?;
        let version = Version::try_from(raw.version).map_err(|_| SgxStatus::Unexpected)?;
        let feature = unsafe { SysFeatures::get_mut() };

        feature.version = version;
        feature.xfrm = xsave::get_xfrm();
        feature.cpu_core_num = raw.cpu_core_num;
        feature.cpuinfo_table = raw.cpuinfo_table;
        feature.is_edmm = raw.is_edmm();
        feature.cpu_features = raw.cpu_features_bit(feature.xfrm)?;

        unsafe {
            g_cpu_feature_indicator = feature.cpu_features;
        }
        Ok(SysFeatures::get())
    }

    #[inline]
    pub fn get() -> &'static SysFeatures {
        unsafe { &SYS_FEATURES }
    }

    #[inline]
    pub unsafe fn get_mut() -> &'static mut SysFeatures {
        &mut SYS_FEATURES
    }

    #[inline]
    pub fn is_edmm(&self) -> bool {
        self.is_edmm
    }

    #[inline]
    pub fn version(&self) -> Version {
        self.version
    }

    #[inline]
    pub fn xfrm(&self) -> u64 {
        self.xfrm
    }

    #[inline]
    pub fn cpu_features(&self) -> u64 {
        self.cpu_features
    }

    #[inline]
    pub fn core_mum(&self) -> u32 {
        self.cpu_core_num
    }

    #[inline]
    pub fn cpuinfo_table(&self) -> &[[u32; 4]; 8] {
        &self.cpuinfo_table
    }
}