1 /* 2 * Copyright (c) 2025 Linaro Ltd 3 * 4 * Test the reset/uninstall cycle of a plugin. 5 * 6 * SPDX-License-Identifier: GPL-2.0-or-later 7 */ 8 #include <glib.h> 9 10 #include <qemu-plugin.h> 11 12 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION; 13 static qemu_plugin_id_t plugin_id; 14 static bool was_reset; 15 static bool was_uninstalled; 16 17 static void after_uninstall(qemu_plugin_id_t id) 18 { 19 g_assert(was_reset && !was_uninstalled); 20 qemu_plugin_outs("uninstall done\n"); 21 was_uninstalled = true; 22 } 23 24 static void tb_exec_after_reset(unsigned int vcpu_index, void *userdata) 25 { 26 g_assert(was_reset && !was_uninstalled); 27 qemu_plugin_uninstall(plugin_id, after_uninstall); 28 } 29 30 static void tb_trans_after_reset(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) 31 { 32 g_assert(was_reset && !was_uninstalled); 33 qemu_plugin_register_vcpu_tb_exec_cb(tb, tb_exec_after_reset, 34 QEMU_PLUGIN_CB_NO_REGS, NULL); 35 } 36 37 static void after_reset(qemu_plugin_id_t id) 38 { 39 g_assert(!was_reset && !was_uninstalled); 40 qemu_plugin_outs("reset done\n"); 41 was_reset = true; 42 qemu_plugin_register_vcpu_tb_trans_cb(id, tb_trans_after_reset); 43 } 44 45 static void tb_exec_before_reset(unsigned int vcpu_index, void *userdata) 46 { 47 g_assert(!was_reset && !was_uninstalled); 48 qemu_plugin_reset(plugin_id, after_reset); 49 } 50 51 static void tb_trans_before_reset(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) 52 { 53 g_assert(!was_reset && !was_uninstalled); 54 qemu_plugin_register_vcpu_tb_exec_cb(tb, tb_exec_before_reset, 55 QEMU_PLUGIN_CB_NO_REGS, NULL); 56 } 57 58 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, 59 const qemu_info_t *info, 60 int argc, char **argv) 61 { 62 plugin_id = id; 63 qemu_plugin_register_vcpu_tb_trans_cb(id, tb_trans_before_reset); 64 return 0; 65 } 66 67 /* Since we uninstall the plugin, we can't use qemu_plugin_register_atexit_cb, 68 * so we use destructor attribute instead. */ 69 static void __attribute__((destructor)) on_plugin_exit(void) 70 { 71 g_assert(was_reset && was_uninstalled); 72 qemu_plugin_outs("plugin exit\n"); 73 } 74