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
use crate::gic;
use crate::realm::context::Context;
use crate::realm::rd::Rd;
use crate::realm::timer;
use crate::rmi::error::Error;
use crate::rmi::rec::params::NR_AUX;
use crate::rmm_exit;
use crate::rsi::attestation::MAX_CHALLENGE_SIZE;

use aarch64_cpu::registers::*;

use core::cell::OnceCell;
use vmsa::guard::Content;

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum RmmRecAttestState {
    AttestInProgress,
    NoAttestInProgress,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum RmmRecEmulatableAbort {
    EmulatableAbort,
    NotEmulatableAbort,
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum State {
    Ready = 1,
    Running = 2,
}

#[derive(Debug)]
struct Ripas {
    start: u64,
    end: u64,
    addr: u64,
    state: u8,
    flags: u64,
}

#[repr(C)]
#[derive(Debug)]
pub struct Rec<'a> {
    pub context: Context,
    attest_state: RmmRecAttestState,
    // TODO: Create consts for both numbers
    attest_challenge: [u8; MAX_CHALLENGE_SIZE],
    attest_token_offset: usize,
    aux: [u64; NR_AUX], // Addresses of auxiliary Granules
    emulatable_abort: RmmRecEmulatableAbort,
    /// PA of RD of Realm which owns this REC
    ///
    /// Safety:
    /// Only immutable fields of Rd must be dereferenced by owner
    /// by making getter method for the safety
    owner: OnceCell<&'a Rd>,
    vcpuid: usize,
    runnable: bool,
    psci_pending: bool,
    state: State,
    ripas: Ripas,
    vtcr: u64,
    host_call_pending: bool,
}

impl Rec<'_> {
    pub fn new() -> Self {
        Self {
            context: Context::new(),
            attest_state: RmmRecAttestState::NoAttestInProgress,
            attest_challenge: [0; MAX_CHALLENGE_SIZE],
            attest_token_offset: 0,
            aux: [0; NR_AUX],
            emulatable_abort: RmmRecEmulatableAbort::NotEmulatableAbort,
            owner: OnceCell::new(),
            vcpuid: 0,
            runnable: false,
            psci_pending: false,
            state: State::Ready,
            ripas: Ripas {
                start: 0,
                end: 0,
                addr: 0,
                state: 0,
                flags: 0,
            },
            vtcr: 0,
            host_call_pending: false,
        }
    }

    pub fn init(
        &mut self,
        owner: usize,
        vcpuid: usize,
        flags: u64,
        aux: [u64; NR_AUX],
        vttbr: u64,
        vmpidr: u64,
    ) -> Result<(), Error> {
        if owner == 0 {
            error!("owner should be non-zero");
            return Err(Error::RmiErrorInput);
        }

        if let Err(input_owner) = self.owner.set(unsafe { &*(owner as *const Rd) }) {
            error!(
                "Rec::init() called twice. cur owner: {:x}, input owner: {:x}",
                self.get_owner()? as *const Rd as usize,
                input_owner as *const Rd as usize
            );
            return Err(Error::RmiErrorRec);
        }

        self.vcpuid = vcpuid;
        self.set_runnable(flags);
        self.context.sys_regs.vttbr = vttbr;
        self.context.sys_regs.vmpidr = vmpidr;
        self.aux.copy_from_slice(&aux);
        timer::init_timer(self);
        gic::init_gic(self);

        Ok(())
    }

    pub fn attest_state(&self) -> RmmRecAttestState {
        self.attest_state
    }

    pub fn attest_challenge(&self) -> &[u8] {
        &self.attest_challenge
    }

    pub fn attest_token_offset(&self) -> usize {
        self.attest_token_offset
    }

    pub fn aux(&self, index: usize) -> u64 {
        self.aux[index]
    }

    pub fn emulatable_abort(&self) -> RmmRecEmulatableAbort {
        self.emulatable_abort
    }

    pub fn runnable(&self) -> bool {
        self.runnable
    }

    pub fn vcpuid(&self) -> usize {
        self.vcpuid
    }

    fn get_owner(&self) -> Result<&Rd, Error> {
        match self.owner.get() {
            Some(owner) => Ok(owner),
            None => Err(Error::RmiErrorRec),
        }
    }

    pub fn owner(&self) -> Result<usize, Error> {
        let owner = self.get_owner()?;
        Ok(owner as *const Rd as usize)
    }

    pub fn host_call_pending(&self) -> bool {
        self.host_call_pending
    }

    pub fn psci_pending(&self) -> bool {
        self.psci_pending
    }

    pub fn set_attest_state(&mut self, state: RmmRecAttestState) {
        self.attest_state = state;
    }

    pub fn set_attest_challenge(&mut self, challenge: &[u8]) {
        self.attest_challenge.copy_from_slice(challenge);
    }

    pub fn set_attest_offset(&mut self, offset: usize) {
        self.attest_token_offset = offset;
    }

    pub fn set_emulatable_abort(&mut self, val: RmmRecEmulatableAbort) {
        self.emulatable_abort = val;
    }

    pub fn set_host_call_pending(&mut self, val: bool) {
        self.host_call_pending = val;
    }

    pub fn set_psci_pending(&mut self, val: bool) {
        self.psci_pending = val;
    }

    pub fn set_ripas(&mut self, start: u64, end: u64, state: u8, flags: u64) {
        self.ripas.start = start;
        self.ripas.end = end;
        self.ripas.state = state;
        self.ripas.flags = flags;
        self.ripas.addr = start; // reset addr to the start
    }

    pub fn set_vtcr(&mut self, vtcr: u64) {
        self.vtcr = vtcr;
    }

    //TODO: change interface. A Rec state can be set by other Recs in the same Rd.
    pub fn set_runnable(&mut self, flags: u64) {
        const RUNNABLE_OFFSET: u64 = 1;
        self.runnable = match flags & RUNNABLE_OFFSET {
            0 => false,
            _ => true,
        }
    }

    pub fn set_state(&mut self, state: State) {
        self.state = state;
    }

    pub fn get_state(&self) -> State {
        self.state
    }

    pub fn set_ripas_addr(&mut self, addr: u64) {
        self.ripas.addr = addr;
    }

    pub fn ripas_addr(&self) -> u64 {
        self.ripas.addr
    }

    pub fn ripas_start(&self) -> u64 {
        self.ripas.start
    }

    pub fn ripas_end(&self) -> u64 {
        self.ripas.end
    }

    pub fn ripas_state(&self) -> u8 {
        self.ripas.state
    }

    pub fn ripas_flags(&self) -> u64 {
        self.ripas.flags
    }

    pub fn vtcr(&self) -> u64 {
        self.vtcr
    }

    pub fn realmid(&self) -> Result<usize, Error> {
        let owner = self.get_owner()?;
        Ok(owner.id())
    }

    pub fn ipa_bits(&self) -> Result<usize, Error> {
        let owner = self.get_owner()?;
        Ok(owner.ipa_bits())
    }

    pub fn from_current(&mut self) {
        unsafe {
            Context::from_current(self);
        }
    }

    pub fn into_current(&self) {
        unsafe {
            Context::into_current(self);
        }
    }

    pub fn reset_ctx(&mut self) {
        self.context.spsr = (SPSR_EL2::D.mask << SPSR_EL2::D.shift)
            | (SPSR_EL2::A.mask << SPSR_EL2::A.shift)
            | (SPSR_EL2::I.mask << SPSR_EL2::I.shift)
            | (SPSR_EL2::F.mask << SPSR_EL2::F.shift)
            | (SPSR_EL2::M.mask & u64::from(SPSR_EL2::M::EL1h)) << SPSR_EL2::M.shift;

        self.context.sys_regs.sctlr = 0;
    }
}

impl Content for Rec<'_> {}

impl safe_abstraction::raw_ptr::RawPtr for Rec<'_> {}

impl safe_abstraction::raw_ptr::SafetyChecked for Rec<'_> {}

impl safe_abstraction::raw_ptr::SafetyAssured for Rec<'_> {
    fn is_initialized(&self) -> bool {
        // The initialization of this memory is guaranteed
        // according to the RMM Specification A2.2.4 Granule Wiping.
        // This instance belongs to a REC Granule and has been initialized.
        true
    }

    fn verify_ownership(&self) -> bool {
        // The ownership of this instance is exclusively ensured by the RMM.
        // under the following conditions:
        //
        // 1. A lock on the given address is obtained using the `get_granule*` macros.
        // 2. The instance is converted from a raw pointer through the `content*` functions.
        // 3. The instance is accessed only within the lock scope.
        //
        // Ownership verification is guaranteed because these criteria are satisfied
        // in all cases where this object is accessed.
        true
    }
}

// XXX: is using 'static okay here?
unsafe fn current() -> Option<&'static mut Rec<'static>> {
    match TPIDR_EL2.get() {
        0 => None,
        current => Some(&mut *(current as *mut Rec<'_>)),
    }
}

fn enter() -> [usize; 4] {
    unsafe {
        if let Some(_rec) = current() {
            // TODO: add code equivalent to the previous clean()
            return rmm_exit([0; 4]);
        }
        [0, 0, 0, 0]
    }
}

fn exit() {
    unsafe {
        if let Some(rec) = current() {
            rec.from_current();
        }
    }
}

// TODO: check the below again
pub fn run_prepare(rd: &Rd, vcpu: usize, rec: &mut Rec<'_>, incr_pc: usize) -> Result<(), Error> {
    if incr_pc == 1 {
        rec.context.elr += 4;
    }
    debug!("resuming: {:#x}", rec.context.elr);
    rec.into_current();

    trace!("Switched to VCPU {} on Realm {}", vcpu, rd.id());
    Ok(())
}

pub fn run() -> Result<[usize; 4], Error> {
    let ret = enter();

    exit();
    Ok(ret)
}