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
use crate::realm::vcpu::VCPU;
use crate::rmi::error::Error;
use crate::rmi::error::InternalError::*;
use crate::rmi::realm::Rd;
use crate::rmi::rec::run::Run;

use armv9a::regs::*;

pub fn init_timer(vcpu: &mut VCPU) {
    let timer = &mut vcpu.context.timer;
    timer.cnthctl_el2 = S3_4_C14_C1_0::EL1PCTEN | S3_4_C14_C1_0::EL1PTEN;
}

pub fn set_cnthctl(vcpu: &mut VCPU, val: u64) {
    let timer = &mut vcpu.context.timer;
    timer.cnthctl_el2 = val;
}

pub fn restore_state(vcpu: &VCPU) {
    let timer = &vcpu.context.timer;

    unsafe { CNTVOFF_EL2.set(timer.cntvoff_el2) };
    unsafe { S3_4_C14_C0_6.set(timer.cntpoff_el2) }; // CNTPOFF_EL2
    unsafe { CNTV_CVAL_EL0.set(timer.cntv_cval_el0) };
    unsafe { CNTV_CTL_EL0.set(timer.cntv_ctl_el0) };
    unsafe { CNTP_CVAL_EL0.set(timer.cntp_cval_el0) };
    unsafe { CNTP_CTL_EL0.set(timer.cntp_ctl_el0) };
    unsafe { S3_4_C14_C1_0.set(timer.cnthctl_el2) }; // CNTHCTL_EL2
}

pub fn save_state(vcpu: &mut VCPU) {
    let timer = &mut vcpu.context.timer;

    timer.cntvoff_el2 = unsafe { CNTVOFF_EL2.get() };
    timer.cntv_cval_el0 = unsafe { CNTV_CVAL_EL0.get() };
    timer.cntv_ctl_el0 = unsafe { CNTV_CTL_EL0.get() };
    timer.cntpoff_el2 = unsafe { S3_4_C14_C0_6.get() }; // CNTPOFF_EL2
    timer.cntp_cval_el0 = unsafe { CNTP_CVAL_EL0.get() };
    timer.cntp_ctl_el0 = unsafe { CNTP_CTL_EL0.get() };
    timer.cnthctl_el2 = unsafe { S3_4_C14_C1_0.get() };
}

pub fn send_state_to_host(rd: &mut Rd, vcpu: usize, run: &mut Run) -> Result<(), Error> {
    let vcpu = rd
        .vcpus
        .get_mut(vcpu)
        .ok_or(Error::RmiErrorOthers(NotExistVCPU))?;
    let timer = &vcpu.lock().context.timer;

    run.set_cntv_ctl(timer.cntv_ctl_el0);
    run.set_cntv_cval(timer.cntv_cval_el0 - timer.cntvoff_el2);
    run.set_cntp_ctl(timer.cntp_ctl_el0);
    run.set_cntp_cval(timer.cntp_cval_el0 - timer.cntpoff_el2);
    Ok(())
}