xen/ctrl/monitor/
mod.rs

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
mod ring;
use xen_sys::{
    vm_event_back_ring, xc_monitor_cpuid, xc_monitor_debug_exceptions,
    xc_monitor_descriptor_access, xc_monitor_disable, xc_monitor_emul_unimplemented,
    xc_monitor_emulate_each_rep, xc_monitor_enable, xc_monitor_get_capabilities,
    xc_monitor_guest_request, xc_monitor_inguest_pagefault, xc_monitor_io, xc_monitor_mov_to_msr,
    xc_monitor_privileged_call, xc_monitor_resume, xc_monitor_singlestep,
    xc_monitor_software_breakpoint, xc_monitor_vmexit, xc_monitor_write_ctrlreg,
};

pub use self::ring::VmEventRing;
use crate::{
    consts::PAGE_SIZE,
    ctrl::{VmEventCtrlReg, XenInterface},
    error::{XcError, XenError},
    evtchn::XenEventChannelPort,
    xc_check_error, XenDomainId, BACK_RING_INIT, SHARED_RING_INIT,
};

pub struct XenMonitor {
    interface: XenInterface,
    domain_id: XenDomainId,
    port: u32,
}

impl XenMonitor {
    pub(crate) fn new(
        interface: XenInterface,
        domain_id: XenDomainId,
    ) -> Result<(Self, VmEventRing), XenError> {
        let mut port: u32 = 0;
        let ring_page = unsafe { xc_monitor_enable(interface.handle.0, domain_id.0, &mut port) };

        if ring_page.is_null() {
            return Err(XcError::new(-1, 0, "Failed to enable monitor").into());
        }

        SHARED_RING_INIT!(ring_page);

        let mut back_ring = unsafe { std::mem::zeroed::<vm_event_back_ring>() };
        BACK_RING_INIT!(back_ring, ring_page, PAGE_SIZE);

        Ok((
            Self {
                interface,
                domain_id,
                port,
            },
            VmEventRing::new(ring_page, back_ring),
        ))
    }

    pub fn channel(&self) -> Result<XenEventChannelPort, XenError> {
        XenEventChannelPort::bind_interdomain(self.domain_id, self.port)
    }

    pub fn port(&self) -> u32 {
        self.port
    }

    pub fn resume(&self) -> Result<(), XenError> {
        let rc = unsafe { xc_monitor_resume(self.interface.handle.0, self.domain_id.0) };
        xc_check_error!(self.interface.handle.0, rc);
        Ok(())
    }

    pub fn get_capabilities(&self) -> Result<u32, XenError> {
        let mut capabilities = 0;
        let rc = unsafe {
            xc_monitor_get_capabilities(
                self.interface.handle.0,
                self.domain_id.0,
                &mut capabilities,
            )
        };
        xc_check_error!(self.interface.handle.0, rc);
        Ok(capabilities)
    }

    pub fn write_ctrlreg(
        &self,
        index: VmEventCtrlReg,
        enable: bool,
        sync: bool,
        bitmask: u64,
        onchangeonly: bool,
    ) -> Result<(), XenError> {
        let rc = unsafe {
            xc_monitor_write_ctrlreg(
                self.interface.handle.0,
                self.domain_id.0,
                index as u16,
                enable,
                sync,
                bitmask,
                onchangeonly,
            )
        };
        xc_check_error!(self.interface.handle.0, rc);
        Ok(())
    }

    pub fn mov_to_msr(&self, msr: u32, enable: bool, onchangeonly: bool) -> Result<(), XenError> {
        let rc = unsafe {
            xc_monitor_mov_to_msr(
                self.interface.handle.0,
                self.domain_id.0,
                msr,
                enable,
                onchangeonly,
            )
        };
        xc_check_error!(self.interface.handle.0, rc);
        Ok(())
    }

    pub fn singlestep(&self, singlestep: bool) -> Result<(), XenError> {
        let rc =
            unsafe { xc_monitor_singlestep(self.interface.handle.0, self.domain_id.0, singlestep) };
        xc_check_error!(self.interface.handle.0, rc);
        Ok(())
    }

    pub fn software_breakpoint(&self, enable: bool) -> Result<(), XenError> {
        let rc = unsafe {
            xc_monitor_software_breakpoint(self.interface.handle.0, self.domain_id.0, enable)
        };
        xc_check_error!(self.interface.handle.0, rc);
        Ok(())
    }

    pub fn descriptor_access(&self, enable: bool) -> Result<(), XenError> {
        let rc = unsafe {
            xc_monitor_descriptor_access(self.interface.handle.0, self.domain_id.0, enable)
        };
        xc_check_error!(self.interface.handle.0, rc);
        Ok(())
    }

    pub fn guest_request(
        &self,
        enable: bool,
        sync: bool,
        allow_userspace: bool,
    ) -> Result<(), XenError> {
        let rc = unsafe {
            xc_monitor_guest_request(
                self.interface.handle.0,
                self.domain_id.0,
                enable,
                sync,
                allow_userspace,
            )
        };
        xc_check_error!(self.interface.handle.0, rc);
        Ok(())
    }

    pub fn inguest_pagefault(&self, disable: bool) -> Result<(), XenError> {
        let rc = unsafe {
            xc_monitor_inguest_pagefault(self.interface.handle.0, self.domain_id.0, disable)
        };
        xc_check_error!(self.interface.handle.0, rc);
        Ok(())
    }

    pub fn debug_exceptions(&self, enable: bool, sync: bool) -> Result<(), XenError> {
        let rc = unsafe {
            xc_monitor_debug_exceptions(self.interface.handle.0, self.domain_id.0, enable, sync)
        };
        xc_check_error!(self.interface.handle.0, rc);
        Ok(())
    }

    pub fn cpuid(&self, enable: bool) -> Result<(), XenError> {
        let rc = unsafe { xc_monitor_cpuid(self.interface.handle.0, self.domain_id.0, enable) };
        xc_check_error!(self.interface.handle.0, rc);
        Ok(())
    }

    pub fn privileged_call(&self, enable: bool) -> Result<(), XenError> {
        let rc = unsafe {
            xc_monitor_privileged_call(self.interface.handle.0, self.domain_id.0, enable)
        };
        xc_check_error!(self.interface.handle.0, rc);
        Ok(())
    }

    pub fn emul_unimplemented(&self, enable: bool) -> Result<(), XenError> {
        let rc = unsafe {
            xc_monitor_emul_unimplemented(self.interface.handle.0, self.domain_id.0, enable)
        };
        xc_check_error!(self.interface.handle.0, rc);
        Ok(())
    }

    pub fn vmexit(&self, enable: bool, sync: bool) -> Result<(), XenError> {
        let rc =
            unsafe { xc_monitor_vmexit(self.interface.handle.0, self.domain_id.0, enable, sync) };
        xc_check_error!(self.interface.handle.0, rc);
        Ok(())
    }

    pub fn io(&self, enable: bool) -> Result<(), XenError> {
        let rc = unsafe { xc_monitor_io(self.interface.handle.0, self.domain_id.0, enable) };
        xc_check_error!(self.interface.handle.0, rc);
        Ok(())
    }

    pub fn emulate_each_rep(&self, enable: bool) -> Result<(), XenError> {
        let rc = unsafe {
            xc_monitor_emulate_each_rep(self.interface.handle.0, self.domain_id.0, enable)
        };
        xc_check_error!(self.interface.handle.0, rc);
        Ok(())
    }
}

impl Drop for XenMonitor {
    fn drop(&mut self) {
        tracing::trace!(?self.domain_id, "disabling monitor");
        unsafe {
            xc_monitor_disable(self.interface.handle.0, self.domain_id.0);
        }
    }
}