vmi_core/
driver.rs

1use std::time::Duration;
2
3use crate::{
4    Architecture, Gfn, MemoryAccess, MemoryAccessOptions, VcpuId, View, VmiError, VmiEvent,
5    VmiEventResponse, VmiInfo, VmiMappedPage,
6};
7
8/// A trait for implementing a VMI driver.
9///
10// The 'static lifetime is required in order to use the driver with the VmiOs
11// enumerators.
12pub trait VmiDriver: 'static {
13    /// The architecture supported by the driver.
14    type Architecture: Architecture + ?Sized;
15
16    /// Returns information about the virtual machine.
17    fn info(&self) -> Result<VmiInfo, VmiError>;
18
19    /// Pauses the virtual machine.
20    fn pause(&self) -> Result<(), VmiError>;
21
22    /// Resumes the virtual machine.
23    fn resume(&self) -> Result<(), VmiError>;
24
25    /// Returns the registers of a specific virtual CPU.
26    fn registers(
27        &self,
28        vcpu: VcpuId,
29    ) -> Result<<Self::Architecture as Architecture>::Registers, VmiError>;
30
31    /// Sets the registers of a specific virtual CPU.
32    fn set_registers(
33        &self,
34        vcpu: VcpuId,
35        registers: <Self::Architecture as Architecture>::Registers,
36    ) -> Result<(), VmiError>;
37
38    /// Returns the memory access permissions for a specific GFN.
39    fn memory_access(&self, gfn: Gfn, view: View) -> Result<MemoryAccess, VmiError>;
40
41    /// Sets the memory access permissions for a specific GFN.
42    fn set_memory_access(&self, gfn: Gfn, view: View, access: MemoryAccess)
43    -> Result<(), VmiError>;
44
45    /// Sets the memory access permissions for a specific GFN with additional
46    /// options.
47    fn set_memory_access_with_options(
48        &self,
49        gfn: Gfn,
50        view: View,
51        access: MemoryAccess,
52        options: MemoryAccessOptions,
53    ) -> Result<(), VmiError>;
54
55    /// Reads a page of memory from the virtual machine.
56    fn read_page(&self, gfn: Gfn) -> Result<VmiMappedPage, VmiError>;
57
58    /// Writes data to a page of memory in the virtual machine.
59    fn write_page(&self, gfn: Gfn, offset: u64, content: &[u8]) -> Result<VmiMappedPage, VmiError>;
60
61    /// Allocates a specific GFN.
62    fn allocate_gfn(&self, gfn: Gfn) -> Result<(), VmiError>;
63
64    /// Frees a previously allocated GFN.
65    fn free_gfn(&self, gfn: Gfn) -> Result<(), VmiError>;
66
67    /// Returns the default view for the virtual machine.
68    fn default_view(&self) -> View;
69
70    /// Creates a new view with the specified default access permissions.
71    fn create_view(&self, default_access: MemoryAccess) -> Result<View, VmiError>;
72
73    /// Destroys a previously created view.
74    fn destroy_view(&self, view: View) -> Result<(), VmiError>;
75
76    /// Switches to a different view.
77    fn switch_to_view(&self, view: View) -> Result<(), VmiError>;
78
79    /// Changes the mapping of a GFN in a specific view.
80    fn change_view_gfn(&self, view: View, old_gfn: Gfn, new_gfn: Gfn) -> Result<(), VmiError>;
81
82    /// Resets the mapping of a GFN in a specific view to its original state.
83    fn reset_view_gfn(&self, view: View, gfn: Gfn) -> Result<(), VmiError>;
84
85    /// Enables monitoring of specific events.
86    fn monitor_enable(
87        &self,
88        option: <Self::Architecture as Architecture>::EventMonitor,
89    ) -> Result<(), VmiError>;
90
91    /// Disables monitoring of specific events.
92    fn monitor_disable(
93        &self,
94        option: <Self::Architecture as Architecture>::EventMonitor,
95    ) -> Result<(), VmiError>;
96
97    /// Injects an interrupt into a specific virtual CPU.
98    fn inject_interrupt(
99        &self,
100        vcpu: VcpuId,
101        interrupt: <Self::Architecture as Architecture>::Interrupt,
102    ) -> Result<(), VmiError>;
103
104    /// Returns the number of pending events.
105    fn events_pending(&self) -> usize;
106
107    /// Returns the time spent processing events.
108    fn event_processing_overhead(&self) -> Duration;
109
110    /// Waits for an event to occur and processes it with the provided handler.
111    fn wait_for_event(
112        &self,
113        timeout: Duration,
114        handler: impl FnMut(&VmiEvent<Self::Architecture>) -> VmiEventResponse<Self::Architecture>,
115    ) -> Result<(), VmiError>;
116
117    /// Resets the state of the VMI system.
118    fn reset_state(&self) -> Result<(), VmiError>;
119}