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
// SPDX-License-Identifier: Apache-2.0

use super::super::types::Argv;
use super::Alloc;
use crate::guest::alloc::{Allocator, Collect, Collector, Output};
use crate::item::enarxcall::Number;
use crate::Result;

use core::arch::x86_64::CpuidResult;

/// Cpuid Enarx call, which writes the [result](CpuidResult) of the `cpuid` instruction
/// for a given `leaf` (`EAX`) and `sub_leaf` (`ECX`) in `result` field.
pub struct Cpuid<'a> {
    pub leaf: u32,
    pub sub_leaf: u32,
    pub result: &'a mut CpuidResult,
}

impl<'a> Alloc<'a> for Cpuid<'a> {
    const NUM: Number = Number::Cpuid;

    type Argv = Argv<3>;
    type Ret = ();

    type Staged = Output<'a, CpuidResult, &'a mut CpuidResult>;
    type Committed = Self::Staged;
    type Collected = Result<()>;

    fn stage(self, alloc: &mut impl Allocator) -> Result<(Self::Argv, Self::Staged)> {
        let result = Output::stage(alloc, self.result)?;
        Ok((
            Argv([self.leaf as _, self.sub_leaf as _, result.offset()]),
            result,
        ))
    }

    fn collect(
        result: Self::Committed,
        ret: Result<Self::Ret>,
        col: &impl Collector,
    ) -> Self::Collected {
        if ret.is_ok() {
            result.collect(col);
        }
        ret
    }
}