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
use crate::const_assert_eq;
use crate::granule::GRANULE_SIZE;
use crate::rmi::error::Error;

use autopadding::*;

/// The structure holds data passsed between the Host and the RMM
/// on Realm Execution Context (REC) entry and exit.
#[repr(C)]
#[derive(Default, Copy, Clone)]
pub struct Run {
    entry: Entry,
    exit: Exit,
}
const_assert_eq!(core::mem::size_of::<Run>(), GRANULE_SIZE);

pad_struct_and_impl_default!(
struct Entry {
    0x0   flags: u64,
    0x200 gprs: [u64; NR_GPRS],
    0x300 gicv3_hcr: u64,
    0x308 gicv3_lrs: [u64; NR_GIC_LRS],
    0x800 => @END,
}
);

pad_struct_and_impl_default!(
struct Exit {
    0x0   exit_reason: u8,
    0x100 esr: u64,
    0x108 far: u64,
    0x110 hpfar: u64,
    0x200 gprs: [u64; NR_GPRS],
    0x300 gicv3_hcr: u64,
    0x308 gicv3_lrs: [u64; NR_GIC_LRS],
    0x388 gicv3_misr: u64,
    0x390 gicv3_vmcr: u64,
    0x400 cntp_ctl: u64,
    0x408 cntp_cval: u64,
    0x410 cntv_ctl: u64,
    0x418 cntv_cval: u64,
    0x500 ripas_base: u64,
    0x508 ripas_size: u64,
    0x510 ripas_value: u8,
    0x600 imm: u16,
    0x700 pmu_ovf: u64,
    0x708 pmu_intr_en: u64,
    0x710 pmu_cntr_en: u64,
    0x800 => @END,
}
);

impl Run {
    pub fn entry_flags(&self) -> u64 {
        self.entry.flags
    }

    pub fn entry_gpr(&self, idx: usize) -> Result<u64, Error> {
        if idx >= NR_GPRS {
            error!("out of index: {}", idx);
            return Err(Error::RmiErrorInput);
        }
        Ok(self.entry.gprs[idx])
    }

    pub fn entry_gic_lrs(&self) -> &[u64; 16] {
        &self.entry.gicv3_lrs
    }

    pub fn entry_gic_hcr(&self) -> u64 {
        self.entry.gicv3_hcr
    }

    pub fn exit_gic_lrs_mut(&mut self) -> &mut [u64; 16] {
        &mut self.exit.gicv3_lrs
    }

    pub fn set_imm(&mut self, imm: u16) {
        self.exit.imm = imm;
    }

    pub fn set_exit_reason(&mut self, exit_reason: u8) {
        self.exit.exit_reason = exit_reason;
    }

    pub fn set_esr(&mut self, esr: u64) {
        self.exit.esr = esr;
    }

    pub fn set_far(&mut self, far: u64) {
        self.exit.far = far;
    }

    pub fn set_hpfar(&mut self, hpfar: u64) {
        self.exit.hpfar = hpfar;
    }

    pub fn set_gpr(&mut self, idx: usize, val: u64) -> Result<(), Error> {
        if idx >= NR_GPRS {
            error!("out of index: {}", idx);
            return Err(Error::RmiErrorInput);
        }
        self.exit.gprs[idx] = val;
        Ok(())
    }

    pub fn set_ripas(&mut self, base: u64, size: u64, state: u8) {
        self.exit.ripas_base = base;
        self.exit.ripas_size = size;
        self.exit.ripas_value = state;
    }

    pub fn set_gic_lrs(&mut self, src: &[u64], len: usize) {
        self.exit.gicv3_lrs.copy_from_slice(&src[..len])
    }

    pub fn set_gic_misr(&mut self, val: u64) {
        self.exit.gicv3_misr = val;
    }

    pub fn set_gic_vmcr(&mut self, val: u64) {
        self.exit.gicv3_vmcr = val;
    }

    pub fn set_gic_hcr(&mut self, val: u64) {
        self.exit.gicv3_hcr = val;
    }

    pub fn set_cntv_ctl(&mut self, val: u64) {
        self.exit.cntv_ctl = val;
    }

    pub fn set_cntv_cval(&mut self, val: u64) {
        self.exit.cntv_cval = val;
    }

    pub fn set_cntp_ctl(&mut self, val: u64) {
        self.exit.cntp_ctl = val;
    }

    pub fn set_cntp_cval(&mut self, val: u64) {
        self.exit.cntp_cval = val;
    }
}

impl core::fmt::Debug for Run {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("rec::Run")
            .field("entry::flags", &format_args!("{:#X}", &self.entry.flags))
            .field("entry::gprs", &self.entry.gprs)
            .field(
                "entry::gicv3_hcr",
                &format_args!("{:#X}", &self.entry.gicv3_hcr),
            )
            .field("entry::gicv3_lrs", &self.entry.gicv3_lrs)
            .field("exit::exit_reason", &self.exit.exit_reason)
            .field("exit::imm", &self.exit.imm)
            .field("exit::cntp_ctl", &self.exit.cntp_ctl)
            .field("exit::cntp_cval", &self.exit.cntp_cval)
            .field("exit::cntv_ctl", &self.exit.cntv_ctl)
            .field("exit::cntv_cval", &self.exit.cntv_cval)
            .finish()
    }
}

/// Whether the host has completed emulation for an Emulatable Data Abort.
///  val 0: Host has not completed emulation for an Emulatable Abort.
///  val 1: Host has completed emulation for an Emulatable Abort.
pub const REC_ENTRY_FLAG_EMUL_MMIO: u64 = 1 << 0;
/// Whether to inject a Synchronous External Abort (SEA) into the Realm.
///  val 0: Do not inject an SEA into the Realm.
///  val 1: Inject an SEA into the Realm.
#[allow(dead_code)]
pub const REC_ENTRY_FLAG_INJECT_SEA: u64 = 1 << 1;
/// Whether to trap WFI execution by the Realm.
///  val 0: Trap is disabled.
///  val 1: Trap is enabled.
#[allow(dead_code)]
pub const REC_ENTRY_FLAG_TRAP_WFI: u64 = 1 << 2;
/// Whether to trap WFE execution by the Realm.
///  val 0: Trap is disabled.
///  val 1: Trap is enabled.
#[allow(dead_code)]
pub const REC_ENTRY_FLAG_TRAP_WFE: u64 = 1 << 3;
pub const NR_GPRS: usize = 31;
const NR_GIC_LRS: usize = 16;

impl Run {
    pub fn verify_compliance(&self) -> Result<(), Error> {
        const ICH_LR_HW_OFFSET: usize = 61;
        // A6.1 Realm interrupts, HW == '0'
        for lr in &self.entry.gicv3_lrs {
            if lr & (1 << ICH_LR_HW_OFFSET) != 0 {
                return Err(Error::RmiErrorRec);
            }
        }
        Ok(())
    }
}

impl safe_abstraction::raw_ptr::RawPtr for Run {}

impl safe_abstraction::raw_ptr::SafetyChecked for Run {}

impl safe_abstraction::raw_ptr::SafetyAssured for Run {
    fn is_initialized(&self) -> bool {
        // Returns `true` to maintain safety at the level preserved by the existing approach.
        // TODO: It is crucial to re-evaluate whether this aspect could potentially
        // lead to malfunctions related to RMM's memory safety.
        true
    }

    fn verify_ownership(&self) -> bool {
        // Returns `true` to maintain safety at the level preserved by the existing approach.
        // TODO: It is crucial to re-evaluate whether this aspect could potentially
        // lead to malfunctions related to RMM's memory safety.
        true
    }
}