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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//! Traits and helpers enabling idiomatic QEMU plugin implementation

use std::sync::{Mutex, OnceLock};

use crate::{
    install::{Args, Info},
    PluginId, TranslationBlock, VCPUIndex,
};
use crate::{
    qemu_plugin_register_flush_cb, qemu_plugin_register_vcpu_exit_cb,
    qemu_plugin_register_vcpu_idle_cb, qemu_plugin_register_vcpu_init_cb,
    qemu_plugin_register_vcpu_resume_cb, qemu_plugin_register_vcpu_syscall_cb,
    qemu_plugin_register_vcpu_syscall_ret_cb, qemu_plugin_register_vcpu_tb_trans_cb,
};

extern "C" fn handle_qemu_plugin_register_vcpu_init_cb(id: PluginId, vcpu_id: VCPUIndex) {
    let Some(plugin) = PLUGIN.get() else {
        panic!("Plugin not set");
    };

    let Ok(mut plugin) = plugin.lock() else {
        panic!("Failed to lock plugin");
    };

    plugin
        .on_vcpu_init(id, vcpu_id)
        .expect("Failed running callback on_vcpu_init");
}

extern "C" fn handle_qemu_plugin_register_vcpu_exit_cb(id: PluginId, vcpu_id: VCPUIndex) {
    let Some(plugin) = PLUGIN.get() else {
        panic!("Plugin not set");
    };

    let Ok(mut plugin) = plugin.lock() else {
        panic!("Failed to lock plugin");
    };

    plugin
        .on_vcpu_exit(id, vcpu_id)
        .expect("Failed running callback on_vcpu_exit");
}

extern "C" fn handle_qemu_plugin_register_vcpu_idle_cb(id: PluginId, vcpu_id: VCPUIndex) {
    let Some(plugin) = PLUGIN.get() else {
        panic!("Plugin not set");
    };

    let Ok(mut plugin) = plugin.lock() else {
        panic!("Failed to lock plugin");
    };

    plugin
        .on_vcpu_idle(id, vcpu_id)
        .expect("Failed running callback on_vcpu_idle");
}

extern "C" fn handle_qemu_plugin_register_vcpu_resume_cb(id: PluginId, vcpu_id: VCPUIndex) {
    let Some(plugin) = PLUGIN.get() else {
        panic!("Plugin not set");
    };

    let Ok(mut plugin) = plugin.lock() else {
        panic!("Failed to lock plugin");
    };

    plugin
        .on_vcpu_resume(id, vcpu_id)
        .expect("Failed running callback on_vcpu_resume");
}

extern "C" fn handle_qemu_plugin_register_vcpu_tb_trans_cb(
    id: PluginId,
    tb: *mut crate::sys::qemu_plugin_tb,
) {
    let Some(plugin) = PLUGIN.get() else {
        panic!("Plugin not set");
    };

    let Ok(mut plugin) = plugin.lock() else {
        panic!("Failed to lock plugin");
    };

    let tb = TranslationBlock::from(tb);

    plugin
        .on_translation_block_translate(id, tb)
        .expect("Failed running callback on_translation_block_translate");
}

extern "C" fn handle_qemu_plugin_register_flush_cb(id: PluginId) {
    let Some(plugin) = PLUGIN.get() else {
        panic!("Plugin not set");
    };

    let Ok(mut plugin) = plugin.lock() else {
        panic!("Failed to lock plugin");
    };

    plugin
        .on_flush(id)
        .expect("Failed running callback on_flush");
}

extern "C" fn handle_qemu_plugin_register_syscall_cb(
    id: PluginId,
    vcpu_index: VCPUIndex,
    num: i64,
    a1: u64,
    a2: u64,
    a3: u64,
    a4: u64,
    a5: u64,
    a6: u64,
    a7: u64,
    a8: u64,
) {
    let Some(plugin) = PLUGIN.get() else {
        panic!("Plugin not set");
    };

    let Ok(mut plugin) = plugin.lock() else {
        panic!("Failed to lock plugin");
    };

    plugin
        .on_syscall(id, vcpu_index, num, a1, a2, a3, a4, a5, a6, a7, a8)
        .expect("Failed running callback on_syscall");
}

extern "C" fn handle_qemu_plugin_register_syscall_ret_cb(
    id: PluginId,
    vcpu_index: VCPUIndex,
    num: i64,
    ret: i64,
) {
    let Some(plugin) = PLUGIN.get() else {
        panic!("Plugin not set");
    };

    let Ok(mut plugin) = plugin.lock() else {
        panic!("Failed to lock plugin");
    };

    plugin
        .on_syscall_return(id, vcpu_index, num, ret)
        .expect("Failed running callback on_syscall_return");
}

/// Trait which implemenents registering the callbacks implemented on a struct which
/// `HasCallbacks` with QEMU
pub trait Register: HasCallbacks + Send + Sync + 'static {
    #[allow(unused)]
    /// Called by QEMu when registering the plugin. This method should only be overridden if no
    /// default callbacks are desired, and will require re-implementing handlers which is not
    /// recommended.
    fn register_default(
        &mut self,
        id: PluginId,
        args: &Args,
        info: &Info,
    ) -> Result<(), anyhow::Error> {
        qemu_plugin_register_vcpu_init_cb(id, Some(handle_qemu_plugin_register_vcpu_init_cb))?;

        qemu_plugin_register_vcpu_exit_cb(id, Some(handle_qemu_plugin_register_vcpu_exit_cb))?;

        qemu_plugin_register_vcpu_idle_cb(id, Some(handle_qemu_plugin_register_vcpu_idle_cb))?;

        qemu_plugin_register_vcpu_resume_cb(id, Some(handle_qemu_plugin_register_vcpu_resume_cb))?;

        qemu_plugin_register_vcpu_tb_trans_cb(
            id,
            Some(handle_qemu_plugin_register_vcpu_tb_trans_cb),
        )?;

        qemu_plugin_register_flush_cb(id, Some(handle_qemu_plugin_register_flush_cb));

        qemu_plugin_register_vcpu_syscall_cb(id, Some(handle_qemu_plugin_register_syscall_cb));

        qemu_plugin_register_vcpu_syscall_ret_cb(
            id,
            Some(handle_qemu_plugin_register_syscall_ret_cb),
        );

        self.register(id, args, info)?;

        Ok(())
    }

    #[allow(unused)]
    /// Called when registering the plugin. User definition of on-registration behavior should
    /// be implemented here.
    fn register(&mut self, id: PluginId, args: &Args, info: &Info) -> Result<(), anyhow::Error> {
        Ok(())
    }
}

/// Trait implemented by structs which have callbacks which should be registered with QEMU
pub trait HasCallbacks: Send + Sync + 'static {
    #[allow(unused)]
    /// Callback triggered on vCPU init
    ///
    /// # Arguments
    ///
    /// * `id` - The ID of the plugin
    /// * `vcpu_id` - The ID of the vCPU
    fn on_vcpu_init(&mut self, id: PluginId, vcpu_id: VCPUIndex) -> Result<(), anyhow::Error> {
        Ok(())
    }

    #[allow(unused)]
    /// Callback triggered on vCPU exit
    ///
    /// # Arguments
    ///
    /// * `id` - The ID of the plugin
    /// * `vcpu_id` - The ID of the vCPU
    fn on_vcpu_exit(&mut self, id: PluginId, vcpu_id: VCPUIndex) -> Result<(), anyhow::Error> {
        Ok(())
    }

    #[allow(unused)]
    /// Callback triggered on vCPU idle
    ///
    /// # Arguments
    ///
    /// * `id` - The ID of the plugin
    /// * `vcpu_id` - The ID of the vCPU
    fn on_vcpu_idle(&mut self, id: PluginId, vcpu_id: VCPUIndex) -> Result<(), anyhow::Error> {
        Ok(())
    }

    #[allow(unused)]
    /// Callback triggered on vCPU resume
    ///
    /// # Arguments
    ///
    /// * `id` - The ID of the plugin
    /// * `vcpu_id` - The ID of the vCPU
    fn on_vcpu_resume(&mut self, id: PluginId, vcpu_id: VCPUIndex) -> Result<(), anyhow::Error> {
        Ok(())
    }

    #[allow(unused)]
    /// Callback triggered on translation block translation
    ///
    /// # Arguments
    ///
    /// * `id` - The ID of the plugin
    /// * `tb` - The translation block
    fn on_translation_block_translate(
        &mut self,
        id: PluginId,
        tb: TranslationBlock,
    ) -> Result<(), anyhow::Error> {
        Ok(())
    }

    #[allow(unused)]
    /// Callback triggered on flush
    ///
    /// # Arguments
    ///
    /// * `id` - The ID of the plugin
    fn on_flush(&mut self, id: PluginId) -> Result<(), anyhow::Error> {
        Ok(())
    }

    #[allow(unused, clippy::too_many_arguments)]
    /// Callback triggered on syscall
    ///
    /// # Arguments
    ///
    /// * `id` - The ID of the plugin
    /// * `vcpu_index` - The ID of the vCPU
    /// * `num` - The syscall number
    /// * `a1` - The first syscall argument
    /// * `a2` - The second syscall argument
    /// * `a3` - The third syscall argument
    /// * `a4` - The fourth syscall argument
    /// * `a5` - The fifth syscall argument
    /// * `a6` - The sixth syscall argument
    /// * `a7` - The seventh syscall argument
    /// * `a8` - The eighth syscall argument
    fn on_syscall(
        &mut self,
        id: PluginId,
        vcpu_index: VCPUIndex,
        num: i64,
        a1: u64,
        a2: u64,
        a3: u64,
        a4: u64,
        a5: u64,
        a6: u64,
        a7: u64,
        a8: u64,
    ) -> Result<(), anyhow::Error> {
        Ok(())
    }

    #[allow(unused)]
    /// Callback triggered on syscall return
    ///
    /// # Arguments
    ///
    /// * `id` - The ID of the plugin
    /// * `vcpu_index` - The ID of the vCPU
    /// * `num` - The syscall number
    /// * `ret` - The return value of the syscall
    fn on_syscall_return(
        &mut self,
        id: PluginId,
        vcpu_index: VCPUIndex,
        num: i64,
        ret: i64,
    ) -> Result<(), anyhow::Error> {
        Ok(())
    }
}

/// Trait implemented by structs which are QEMU plugin contexts
pub trait Plugin: Register + HasCallbacks {}

/// The global plugin item
pub static PLUGIN: OnceLock<Mutex<Box<dyn Plugin>>> = OnceLock::new();