1 #ifndef SYSTEM_RUNSTATE_H 2 #define SYSTEM_RUNSTATE_H 3 4 #include "qapi/qapi-types-run-state.h" 5 #include "qemu/notify.h" 6 7 bool runstate_check(RunState state); 8 void runstate_set(RunState new_state); 9 RunState runstate_get(void); 10 bool runstate_is_running(void); 11 bool runstate_needs_reset(void); 12 void runstate_replay_enable(void); 13 14 typedef void VMChangeStateHandler(void *opaque, bool running, RunState state); 15 typedef int VMChangeStateHandlerWithRet(void *opaque, bool running, RunState state); 16 17 /** 18 * qemu_add_vm_change_state_handler: 19 * @cb: the callback to invoke 20 * @opaque: user data passed to the callback 21 * 22 * Register a callback function that is invoked when the vm starts or stops 23 * running. 24 * 25 * Returns: an entry to be freed using qemu_del_vm_change_state_handler() 26 */ 27 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, 28 void *opaque); 29 /** 30 * qemu_add_vm_change_state_handler_prio: 31 * @cb: the callback to invoke 32 * @opaque: user data passed to the callback 33 * @priority: low priorities execute first when the vm runs and the reverse is 34 * true when the vm stops 35 * 36 * Register a callback function that is invoked when the vm starts or stops 37 * running. 38 * 39 * Returns: an entry to be freed using qemu_del_vm_change_state_handler() 40 */ 41 VMChangeStateEntry *qemu_add_vm_change_state_handler_prio( 42 VMChangeStateHandler *cb, void *opaque, int priority); 43 VMChangeStateEntry * 44 /** 45 * qemu_add_vm_change_state_handler_prio_full: 46 * @cb: the main callback to invoke 47 * @prepare_cb: a callback to invoke before the main callback 48 * @cb_ret: the main callback to invoke with return value 49 * @opaque: user data passed to the callbacks 50 * @priority: low priorities execute first when the vm runs and the reverse is 51 * true when the vm stops 52 * 53 * Register a main callback function and an optional prepare callback function 54 * that are invoked when the vm starts or stops running. The main callback and 55 * the prepare callback are called in two separate phases: First all prepare 56 * callbacks are called and only then all main callbacks are called. As its 57 * name suggests, the prepare callback can be used to do some preparatory work 58 * before invoking the main callback. 59 * 60 * Returns: an entry to be freed using qemu_del_vm_change_state_handler() 61 */ 62 qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler *cb, 63 VMChangeStateHandler *prepare_cb, 64 VMChangeStateHandlerWithRet *cb_ret, 65 void *opaque, int priority); 66 VMChangeStateEntry *qdev_add_vm_change_state_handler(DeviceState *dev, 67 VMChangeStateHandler *cb, 68 VMChangeStateHandlerWithRet *cb_ret, 69 void *opaque); 70 VMChangeStateEntry *qdev_add_vm_change_state_handler_full( 71 DeviceState *dev, VMChangeStateHandler *cb, VMChangeStateHandler *prepare_cb, 72 VMChangeStateHandlerWithRet *cb_ret, void *opaque); 73 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e); 74 /** 75 * vm_state_notify: Notify the state of the VM 76 * 77 * @running: whether the VM is running or not. 78 * @state: the #RunState of the VM. 79 * 80 * Return the result of the callback which has return value. 81 * If no callback has return value, still return 0 and the 82 * upper layer should not do additional processing. 83 */ 84 int vm_state_notify(bool running, RunState state); 85 86 static inline bool shutdown_caused_by_guest(ShutdownCause cause) 87 { 88 return cause >= SHUTDOWN_CAUSE_GUEST_SHUTDOWN; 89 } 90 91 /* 92 * In a "live" state, the vcpu clock is ticking, and the runstate notifiers 93 * think we are running. 94 */ 95 static inline bool runstate_is_live(RunState state) 96 { 97 return state == RUN_STATE_RUNNING || state == RUN_STATE_SUSPENDED; 98 } 99 100 void vm_start(void); 101 102 /** 103 * vm_prepare_start: Prepare for starting/resuming the VM 104 * 105 * @step_pending: whether any of the CPUs is about to be single-stepped by gdb 106 */ 107 int vm_prepare_start(bool step_pending); 108 109 /** 110 * vm_resume: If @state is a live state, start the vm and set the state, 111 * else just set the state. 112 * 113 * @state: the state to restore 114 */ 115 void vm_resume(RunState state); 116 117 int vm_stop(RunState state); 118 int vm_stop_force_state(RunState state); 119 int vm_shutdown(void); 120 void vm_set_suspended(bool suspended); 121 bool vm_get_suspended(void); 122 123 typedef enum WakeupReason { 124 /* Always keep QEMU_WAKEUP_REASON_NONE = 0 */ 125 QEMU_WAKEUP_REASON_NONE = 0, 126 QEMU_WAKEUP_REASON_RTC, 127 QEMU_WAKEUP_REASON_PMTIMER, 128 QEMU_WAKEUP_REASON_OTHER, 129 } WakeupReason; 130 131 void qemu_system_reset_request(ShutdownCause reason); 132 void qemu_system_suspend_request(void); 133 void qemu_register_suspend_notifier(Notifier *notifier); 134 bool qemu_wakeup_suspend_enabled(void); 135 void qemu_system_wakeup_request(WakeupReason reason, Error **errp); 136 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled); 137 void qemu_register_wakeup_notifier(Notifier *notifier); 138 void qemu_register_wakeup_support(void); 139 void qemu_system_shutdown_request_with_code(ShutdownCause reason, 140 int exit_code); 141 void qemu_system_shutdown_request(ShutdownCause reason); 142 void qemu_system_powerdown_request(void); 143 void qemu_register_powerdown_notifier(Notifier *notifier); 144 void qemu_register_shutdown_notifier(Notifier *notifier); 145 void qemu_system_debug_request(void); 146 void qemu_system_vmstop_request(RunState reason); 147 void qemu_system_vmstop_request_prepare(void); 148 bool qemu_vmstop_requested(RunState *r); 149 ShutdownCause qemu_shutdown_requested_get(void); 150 bool qemu_force_shutdown_requested(void); 151 ShutdownCause qemu_reset_requested_get(void); 152 void qemu_system_killed(int signal, pid_t pid); 153 void qemu_system_reset(ShutdownCause reason); 154 void qemu_system_guest_panicked(GuestPanicInformation *info); 155 void qemu_system_guest_crashloaded(GuestPanicInformation *info); 156 void qemu_system_guest_pvshutdown(void); 157 bool qemu_system_dump_in_progress(void); 158 159 #endif 160 161