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
//! Blocking time

use {
    crate::{
        console::Writer,
        events::{bind_virq, unmask_event_channel},
        hypercall,
        scheduler::{schedule_operation, Command},
    },
    log::trace,
    xen_sys::{__HYPERVISOR_set_timer_op, VIRQ_TIMER},
};

pub use crate::platform::time::get_system_time;

/// Initialise time
pub fn init() {
    let port = bind_virq(
        VIRQ_TIMER,
        |_, _, _| set_timer_op(get_system_time() + 1_000_000),
        0,
    );

    trace!("time virq port: {}", port);
    unmask_event_channel(port);
}

/// Block for the supplied number of nanoseconds
pub fn block(until: u64) {
    if get_system_time() < until {
        log::trace!("here0");
        Writer::flush();

        log::trace!("here1");
        Writer::flush();

        schedule_operation(Command::Block);

        log::trace!("here2");
        Writer::flush();

        //local_irq_disable();
        set_timer_op(0);

        log::trace!("here3");
        Writer::flush();
    }
}

fn set_timer_op(until: u64) {
    unsafe {
        hypercall!(__HYPERVISOR_set_timer_op, until).expect("failed to set timer");
    }
}