Skip to main content

vmi_core/
handler.rs

1use crate::{VmiContext, VmiEventResponse, VmiOs, VmiSession};
2
3/// A trait for handling VMI events.
4///
5/// A factory that creates a handler implementing this trait is passed to
6/// the [`VmiSession::handle`] method to handle VMI events.
7///
8/// [`VmiSession::handle`]: crate::VmiSession::handle
9pub trait VmiHandler<Os>
10where
11    Os: VmiOs,
12{
13    /// The output type of the handler.
14    type Output;
15
16    /// Called for each VMI event.
17    ///
18    /// The returned [`VmiEventResponse`] tells the hypervisor how to resume
19    /// the vCPU that triggered the event.
20    fn handle_event(&mut self, event: VmiContext<Os>) -> VmiEventResponse<Os::Architecture>;
21
22    /// Called when the event loop times out waiting for the next event.
23    ///
24    /// This is useful for periodic housekeeping while the guest is idle.
25    fn handle_timeout(&mut self, _session: &VmiSession<Os>) {}
26
27    /// Called when the event loop is interrupted by a signal.
28    ///
29    /// Typically used to initiate a graceful shutdown, by setting a flag
30    /// that causes [`poll`](Self::poll) to return `Some` on the next call.
31    fn handle_interrupted(&mut self, _session: &VmiSession<Os>) {}
32
33    /// Called once before the session tears down monitoring.
34    ///
35    /// This gives the handler an opportunity to release resources that
36    /// depend on the session (views, memory access permissions, event
37    /// monitors) before the session calls [`reset_state`].
38    ///
39    /// [`reset_state`]: crate::VmiCore::reset_state
40    fn cleanup(&mut self, _session: &VmiSession<Os>) {}
41
42    /// Checks if the handler has completed.
43    ///
44    /// This method is called after each event is handled. If the handler
45    /// has completed, this method should return the output of the handler.
46    /// Otherwise, it should return `None`.
47    fn poll(&self) -> Option<Self::Output> {
48        None
49    }
50}