2
0

runstate.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. /*
  2. * QEMU main system emulation loop
  3. *
  4. * Copyright (c) 2003-2020 QEMU contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "audio/audio.h"
  26. #include "block/block.h"
  27. #include "block/export.h"
  28. #include "chardev/char.h"
  29. #include "crypto/cipher.h"
  30. #include "crypto/init.h"
  31. #include "exec/cpu-common.h"
  32. #include "gdbstub/syscalls.h"
  33. #include "hw/boards.h"
  34. #include "hw/resettable.h"
  35. #include "migration/misc.h"
  36. #include "migration/postcopy-ram.h"
  37. #include "monitor/monitor.h"
  38. #include "net/net.h"
  39. #include "net/vhost_net.h"
  40. #include "qapi/error.h"
  41. #include "qapi/qapi-commands-run-state.h"
  42. #include "qapi/qapi-events-run-state.h"
  43. #include "qemu/accel.h"
  44. #include "qemu/error-report.h"
  45. #include "qemu/job.h"
  46. #include "qemu/log.h"
  47. #include "qemu/module.h"
  48. #include "qemu/sockets.h"
  49. #include "qemu/timer.h"
  50. #include "qemu/thread.h"
  51. #include "qom/object.h"
  52. #include "qom/object_interfaces.h"
  53. #include "system/cpus.h"
  54. #include "system/qtest.h"
  55. #include "system/replay.h"
  56. #include "system/reset.h"
  57. #include "system/runstate.h"
  58. #include "system/runstate-action.h"
  59. #include "system/system.h"
  60. #include "system/tpm.h"
  61. #include "trace.h"
  62. static NotifierList exit_notifiers =
  63. NOTIFIER_LIST_INITIALIZER(exit_notifiers);
  64. static RunState current_run_state = RUN_STATE_PRELAUNCH;
  65. /* We use RUN_STATE__MAX but any invalid value will do */
  66. static RunState vmstop_requested = RUN_STATE__MAX;
  67. static QemuMutex vmstop_lock;
  68. typedef struct {
  69. RunState from;
  70. RunState to;
  71. } RunStateTransition;
  72. static const RunStateTransition runstate_transitions_def[] = {
  73. { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
  74. { RUN_STATE_PRELAUNCH, RUN_STATE_SUSPENDED },
  75. { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
  76. { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
  77. { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
  78. { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
  79. { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
  80. { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
  81. { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
  82. { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
  83. { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
  84. { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
  85. { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
  86. { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
  87. { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
  88. { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
  89. { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
  90. { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
  91. { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
  92. { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
  93. { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
  94. { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
  95. { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
  96. { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
  97. { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
  98. { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
  99. { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
  100. { RUN_STATE_PAUSED, RUN_STATE_COLO},
  101. { RUN_STATE_PAUSED, RUN_STATE_SUSPENDED},
  102. { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
  103. { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
  104. { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
  105. { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
  106. { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
  107. { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
  108. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
  109. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
  110. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
  111. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
  112. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO },
  113. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_INTERNAL_ERROR },
  114. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_IO_ERROR },
  115. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_SHUTDOWN },
  116. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_SUSPENDED },
  117. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_WATCHDOG },
  118. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_GUEST_PANICKED },
  119. { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
  120. { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
  121. { RUN_STATE_RESTORE_VM, RUN_STATE_SUSPENDED },
  122. { RUN_STATE_COLO, RUN_STATE_RUNNING },
  123. { RUN_STATE_COLO, RUN_STATE_PRELAUNCH },
  124. { RUN_STATE_COLO, RUN_STATE_SHUTDOWN},
  125. { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
  126. { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
  127. { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
  128. { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
  129. { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
  130. { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
  131. { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
  132. { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
  133. { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
  134. { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
  135. { RUN_STATE_RUNNING, RUN_STATE_COLO},
  136. { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
  137. { RUN_STATE_SAVE_VM, RUN_STATE_SUSPENDED },
  138. { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
  139. { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
  140. { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
  141. { RUN_STATE_SHUTDOWN, RUN_STATE_COLO },
  142. { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
  143. { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
  144. { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
  145. { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
  146. { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
  147. { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
  148. { RUN_STATE_SUSPENDED, RUN_STATE_PAUSED},
  149. { RUN_STATE_SUSPENDED, RUN_STATE_SAVE_VM },
  150. { RUN_STATE_SUSPENDED, RUN_STATE_RESTORE_VM },
  151. { RUN_STATE_SUSPENDED, RUN_STATE_SHUTDOWN },
  152. { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
  153. { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
  154. { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
  155. { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
  156. { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
  157. { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
  158. { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
  159. { RUN_STATE__MAX, RUN_STATE__MAX },
  160. };
  161. static const RunStateTransition replay_play_runstate_transitions_def[] = {
  162. { RUN_STATE_SHUTDOWN, RUN_STATE_RUNNING},
  163. { RUN_STATE__MAX, RUN_STATE__MAX },
  164. };
  165. static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
  166. bool runstate_check(RunState state)
  167. {
  168. return current_run_state == state;
  169. }
  170. static void transitions_set_valid(const RunStateTransition *rst)
  171. {
  172. const RunStateTransition *p;
  173. for (p = rst; p->from != RUN_STATE__MAX; p++) {
  174. runstate_valid_transitions[p->from][p->to] = true;
  175. }
  176. }
  177. void runstate_replay_enable(void)
  178. {
  179. assert(replay_mode != REPLAY_MODE_NONE);
  180. if (replay_mode == REPLAY_MODE_PLAY) {
  181. /*
  182. * When reverse-debugging, it is possible to move state from
  183. * shutdown to running.
  184. */
  185. transitions_set_valid(&replay_play_runstate_transitions_def[0]);
  186. }
  187. }
  188. static void runstate_init(void)
  189. {
  190. memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
  191. transitions_set_valid(&runstate_transitions_def[0]);
  192. qemu_mutex_init(&vmstop_lock);
  193. }
  194. /* This function will abort() on invalid state transitions */
  195. void runstate_set(RunState new_state)
  196. {
  197. assert(new_state < RUN_STATE__MAX);
  198. trace_runstate_set(current_run_state, RunState_str(current_run_state),
  199. new_state, RunState_str(new_state));
  200. if (current_run_state == new_state) {
  201. return;
  202. }
  203. if (!runstate_valid_transitions[current_run_state][new_state]) {
  204. error_report("invalid runstate transition: '%s' -> '%s'",
  205. RunState_str(current_run_state),
  206. RunState_str(new_state));
  207. abort();
  208. }
  209. current_run_state = new_state;
  210. }
  211. RunState runstate_get(void)
  212. {
  213. return current_run_state;
  214. }
  215. bool runstate_is_running(void)
  216. {
  217. return runstate_check(RUN_STATE_RUNNING);
  218. }
  219. bool runstate_needs_reset(void)
  220. {
  221. return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
  222. runstate_check(RUN_STATE_SHUTDOWN);
  223. }
  224. StatusInfo *qmp_query_status(Error **errp)
  225. {
  226. StatusInfo *info = g_malloc0(sizeof(*info));
  227. info->running = runstate_is_running();
  228. info->status = current_run_state;
  229. return info;
  230. }
  231. bool qemu_vmstop_requested(RunState *r)
  232. {
  233. qemu_mutex_lock(&vmstop_lock);
  234. *r = vmstop_requested;
  235. vmstop_requested = RUN_STATE__MAX;
  236. qemu_mutex_unlock(&vmstop_lock);
  237. return *r < RUN_STATE__MAX;
  238. }
  239. void qemu_system_vmstop_request_prepare(void)
  240. {
  241. qemu_mutex_lock(&vmstop_lock);
  242. }
  243. void qemu_system_vmstop_request(RunState state)
  244. {
  245. vmstop_requested = state;
  246. qemu_mutex_unlock(&vmstop_lock);
  247. qemu_notify_event();
  248. }
  249. struct VMChangeStateEntry {
  250. VMChangeStateHandler *cb;
  251. VMChangeStateHandler *prepare_cb;
  252. void *opaque;
  253. QTAILQ_ENTRY(VMChangeStateEntry) entries;
  254. int priority;
  255. };
  256. static QTAILQ_HEAD(, VMChangeStateEntry) vm_change_state_head =
  257. QTAILQ_HEAD_INITIALIZER(vm_change_state_head);
  258. /**
  259. * qemu_add_vm_change_state_handler_prio:
  260. * @cb: the callback to invoke
  261. * @opaque: user data passed to the callback
  262. * @priority: low priorities execute first when the vm runs and the reverse is
  263. * true when the vm stops
  264. *
  265. * Register a callback function that is invoked when the vm starts or stops
  266. * running.
  267. *
  268. * Returns: an entry to be freed using qemu_del_vm_change_state_handler()
  269. */
  270. VMChangeStateEntry *qemu_add_vm_change_state_handler_prio(
  271. VMChangeStateHandler *cb, void *opaque, int priority)
  272. {
  273. return qemu_add_vm_change_state_handler_prio_full(cb, NULL, opaque,
  274. priority);
  275. }
  276. /**
  277. * qemu_add_vm_change_state_handler_prio_full:
  278. * @cb: the main callback to invoke
  279. * @prepare_cb: a callback to invoke before the main callback
  280. * @opaque: user data passed to the callbacks
  281. * @priority: low priorities execute first when the vm runs and the reverse is
  282. * true when the vm stops
  283. *
  284. * Register a main callback function and an optional prepare callback function
  285. * that are invoked when the vm starts or stops running. The main callback and
  286. * the prepare callback are called in two separate phases: First all prepare
  287. * callbacks are called and only then all main callbacks are called. As its
  288. * name suggests, the prepare callback can be used to do some preparatory work
  289. * before invoking the main callback.
  290. *
  291. * Returns: an entry to be freed using qemu_del_vm_change_state_handler()
  292. */
  293. VMChangeStateEntry *
  294. qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler *cb,
  295. VMChangeStateHandler *prepare_cb,
  296. void *opaque, int priority)
  297. {
  298. VMChangeStateEntry *e;
  299. VMChangeStateEntry *other;
  300. e = g_malloc0(sizeof(*e));
  301. e->cb = cb;
  302. e->prepare_cb = prepare_cb;
  303. e->opaque = opaque;
  304. e->priority = priority;
  305. /* Keep list sorted in ascending priority order */
  306. QTAILQ_FOREACH(other, &vm_change_state_head, entries) {
  307. if (priority < other->priority) {
  308. QTAILQ_INSERT_BEFORE(other, e, entries);
  309. return e;
  310. }
  311. }
  312. QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries);
  313. return e;
  314. }
  315. VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
  316. void *opaque)
  317. {
  318. return qemu_add_vm_change_state_handler_prio(cb, opaque, 0);
  319. }
  320. void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
  321. {
  322. QTAILQ_REMOVE(&vm_change_state_head, e, entries);
  323. g_free(e);
  324. }
  325. void vm_state_notify(bool running, RunState state)
  326. {
  327. VMChangeStateEntry *e, *next;
  328. trace_vm_state_notify(running, state, RunState_str(state));
  329. if (running) {
  330. QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
  331. if (e->prepare_cb) {
  332. e->prepare_cb(e->opaque, running, state);
  333. }
  334. }
  335. QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
  336. e->cb(e->opaque, running, state);
  337. }
  338. } else {
  339. QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) {
  340. if (e->prepare_cb) {
  341. e->prepare_cb(e->opaque, running, state);
  342. }
  343. }
  344. QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) {
  345. e->cb(e->opaque, running, state);
  346. }
  347. }
  348. }
  349. static ShutdownCause reset_requested;
  350. static ShutdownCause shutdown_requested;
  351. static int shutdown_exit_code = EXIT_SUCCESS;
  352. static int shutdown_signal;
  353. static pid_t shutdown_pid;
  354. static int powerdown_requested;
  355. static int debug_requested;
  356. static int suspend_requested;
  357. static WakeupReason wakeup_reason;
  358. static NotifierList powerdown_notifiers =
  359. NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
  360. static NotifierList suspend_notifiers =
  361. NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
  362. static NotifierList wakeup_notifiers =
  363. NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
  364. static NotifierList shutdown_notifiers =
  365. NOTIFIER_LIST_INITIALIZER(shutdown_notifiers);
  366. static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
  367. ShutdownCause qemu_shutdown_requested_get(void)
  368. {
  369. return shutdown_requested;
  370. }
  371. ShutdownCause qemu_reset_requested_get(void)
  372. {
  373. return reset_requested;
  374. }
  375. static int qemu_shutdown_requested(void)
  376. {
  377. return qatomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
  378. }
  379. static void qemu_kill_report(void)
  380. {
  381. if (!qtest_driver() && shutdown_signal) {
  382. if (shutdown_pid == 0) {
  383. /* This happens for eg ^C at the terminal, so it's worth
  384. * avoiding printing an odd message in that case.
  385. */
  386. error_report("terminating on signal %d", shutdown_signal);
  387. } else {
  388. char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
  389. error_report("terminating on signal %d from pid " FMT_pid " (%s)",
  390. shutdown_signal, shutdown_pid,
  391. shutdown_cmd ? shutdown_cmd : "<unknown process>");
  392. g_free(shutdown_cmd);
  393. }
  394. shutdown_signal = 0;
  395. }
  396. }
  397. static ShutdownCause qemu_reset_requested(void)
  398. {
  399. ShutdownCause r = reset_requested;
  400. if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
  401. reset_requested = SHUTDOWN_CAUSE_NONE;
  402. return r;
  403. }
  404. return SHUTDOWN_CAUSE_NONE;
  405. }
  406. static int qemu_suspend_requested(void)
  407. {
  408. int r = suspend_requested;
  409. if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
  410. suspend_requested = 0;
  411. return r;
  412. }
  413. return false;
  414. }
  415. static WakeupReason qemu_wakeup_requested(void)
  416. {
  417. return wakeup_reason;
  418. }
  419. static int qemu_powerdown_requested(void)
  420. {
  421. int r = powerdown_requested;
  422. powerdown_requested = 0;
  423. return r;
  424. }
  425. static int qemu_debug_requested(void)
  426. {
  427. int r = debug_requested;
  428. debug_requested = 0;
  429. return r;
  430. }
  431. /*
  432. * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
  433. */
  434. void qemu_system_reset(ShutdownCause reason)
  435. {
  436. MachineClass *mc;
  437. ResetType type;
  438. mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
  439. cpu_synchronize_all_states();
  440. switch (reason) {
  441. case SHUTDOWN_CAUSE_SNAPSHOT_LOAD:
  442. type = RESET_TYPE_SNAPSHOT_LOAD;
  443. break;
  444. default:
  445. type = RESET_TYPE_COLD;
  446. }
  447. if (mc && mc->reset) {
  448. mc->reset(current_machine, type);
  449. } else {
  450. qemu_devices_reset(type);
  451. }
  452. switch (reason) {
  453. case SHUTDOWN_CAUSE_NONE:
  454. case SHUTDOWN_CAUSE_SUBSYSTEM_RESET:
  455. case SHUTDOWN_CAUSE_SNAPSHOT_LOAD:
  456. break;
  457. default:
  458. qapi_event_send_reset(shutdown_caused_by_guest(reason), reason);
  459. }
  460. /*
  461. * Some boards use the machine reset callback to point CPUs to the firmware
  462. * entry point. Assume that this is not the case for boards that support
  463. * non-resettable CPUs (currently used only for confidential guests), in
  464. * which case cpu_synchronize_all_post_init() is enough because
  465. * it does _more_ than cpu_synchronize_all_post_reset().
  466. */
  467. if (cpus_are_resettable()) {
  468. cpu_synchronize_all_post_reset();
  469. } else {
  470. assert(runstate_check(RUN_STATE_PRELAUNCH));
  471. }
  472. vm_set_suspended(false);
  473. }
  474. /*
  475. * Wake the VM after suspend.
  476. */
  477. static void qemu_system_wakeup(void)
  478. {
  479. MachineClass *mc;
  480. mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
  481. if (mc && mc->wakeup) {
  482. mc->wakeup(current_machine);
  483. }
  484. }
  485. void qemu_system_guest_panicked(GuestPanicInformation *info)
  486. {
  487. qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
  488. if (current_cpu) {
  489. current_cpu->crash_occurred = true;
  490. }
  491. /*
  492. * TODO: Currently the available panic actions are: none, pause, and
  493. * shutdown, but in principle debug and reset could be supported as well.
  494. * Investigate any potential use cases for the unimplemented actions.
  495. */
  496. if (panic_action == PANIC_ACTION_PAUSE
  497. || (panic_action == PANIC_ACTION_SHUTDOWN && shutdown_action == SHUTDOWN_ACTION_PAUSE)) {
  498. qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, info);
  499. vm_stop(RUN_STATE_GUEST_PANICKED);
  500. } else if (panic_action == PANIC_ACTION_SHUTDOWN ||
  501. panic_action == PANIC_ACTION_EXIT_FAILURE) {
  502. qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF, info);
  503. vm_stop(RUN_STATE_GUEST_PANICKED);
  504. qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
  505. } else {
  506. qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_RUN, info);
  507. }
  508. if (info) {
  509. if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
  510. qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
  511. " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
  512. info->u.hyper_v.arg1,
  513. info->u.hyper_v.arg2,
  514. info->u.hyper_v.arg3,
  515. info->u.hyper_v.arg4,
  516. info->u.hyper_v.arg5);
  517. } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
  518. qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
  519. "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
  520. info->u.s390.core,
  521. S390CrashReason_str(info->u.s390.reason),
  522. info->u.s390.psw_mask,
  523. info->u.s390.psw_addr);
  524. }
  525. qapi_free_GuestPanicInformation(info);
  526. }
  527. }
  528. void qemu_system_guest_crashloaded(GuestPanicInformation *info)
  529. {
  530. qemu_log_mask(LOG_GUEST_ERROR, "Guest crash loaded");
  531. qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN, info);
  532. qapi_free_GuestPanicInformation(info);
  533. }
  534. void qemu_system_guest_pvshutdown(void)
  535. {
  536. qapi_event_send_guest_pvshutdown();
  537. qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
  538. }
  539. void qemu_system_reset_request(ShutdownCause reason)
  540. {
  541. if (reboot_action == REBOOT_ACTION_SHUTDOWN &&
  542. reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
  543. shutdown_requested = reason;
  544. } else if (!cpus_are_resettable()) {
  545. error_report("cpus are not resettable, terminating");
  546. shutdown_requested = reason;
  547. } else {
  548. reset_requested = reason;
  549. }
  550. cpu_stop_current();
  551. qemu_notify_event();
  552. }
  553. static void qemu_system_suspend(void)
  554. {
  555. pause_all_vcpus();
  556. notifier_list_notify(&suspend_notifiers, NULL);
  557. runstate_set(RUN_STATE_SUSPENDED);
  558. qapi_event_send_suspend();
  559. }
  560. void qemu_system_suspend_request(void)
  561. {
  562. if (runstate_check(RUN_STATE_SUSPENDED)) {
  563. return;
  564. }
  565. suspend_requested = 1;
  566. cpu_stop_current();
  567. qemu_notify_event();
  568. }
  569. void qemu_register_suspend_notifier(Notifier *notifier)
  570. {
  571. notifier_list_add(&suspend_notifiers, notifier);
  572. }
  573. void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
  574. {
  575. trace_system_wakeup_request(reason);
  576. if (!runstate_check(RUN_STATE_SUSPENDED)) {
  577. error_setg(errp,
  578. "Unable to wake up: guest is not in suspended state");
  579. return;
  580. }
  581. if (!(wakeup_reason_mask & (1 << reason))) {
  582. return;
  583. }
  584. runstate_set(RUN_STATE_RUNNING);
  585. wakeup_reason = reason;
  586. qemu_notify_event();
  587. }
  588. void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
  589. {
  590. if (enabled) {
  591. wakeup_reason_mask |= (1 << reason);
  592. } else {
  593. wakeup_reason_mask &= ~(1 << reason);
  594. }
  595. }
  596. void qemu_register_wakeup_notifier(Notifier *notifier)
  597. {
  598. notifier_list_add(&wakeup_notifiers, notifier);
  599. }
  600. static bool wakeup_suspend_enabled;
  601. void qemu_register_wakeup_support(void)
  602. {
  603. wakeup_suspend_enabled = true;
  604. }
  605. bool qemu_wakeup_suspend_enabled(void)
  606. {
  607. return wakeup_suspend_enabled;
  608. }
  609. void qemu_system_killed(int signal, pid_t pid)
  610. {
  611. shutdown_signal = signal;
  612. shutdown_pid = pid;
  613. shutdown_action = SHUTDOWN_ACTION_POWEROFF;
  614. /* Cannot call qemu_system_shutdown_request directly because
  615. * we are in a signal handler.
  616. */
  617. shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
  618. qemu_notify_event();
  619. }
  620. void qemu_system_shutdown_request_with_code(ShutdownCause reason,
  621. int exit_code)
  622. {
  623. shutdown_exit_code = exit_code;
  624. qemu_system_shutdown_request(reason);
  625. }
  626. void qemu_system_shutdown_request(ShutdownCause reason)
  627. {
  628. trace_qemu_system_shutdown_request(reason);
  629. replay_shutdown_request(reason);
  630. shutdown_requested = reason;
  631. qemu_notify_event();
  632. }
  633. static void qemu_system_powerdown(void)
  634. {
  635. qapi_event_send_powerdown();
  636. notifier_list_notify(&powerdown_notifiers, NULL);
  637. }
  638. static void qemu_system_shutdown(ShutdownCause cause)
  639. {
  640. qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause);
  641. notifier_list_notify(&shutdown_notifiers, &cause);
  642. }
  643. void qemu_system_powerdown_request(void)
  644. {
  645. trace_qemu_system_powerdown_request();
  646. powerdown_requested = 1;
  647. qemu_notify_event();
  648. }
  649. void qemu_register_powerdown_notifier(Notifier *notifier)
  650. {
  651. notifier_list_add(&powerdown_notifiers, notifier);
  652. }
  653. void qemu_register_shutdown_notifier(Notifier *notifier)
  654. {
  655. notifier_list_add(&shutdown_notifiers, notifier);
  656. }
  657. void qemu_system_debug_request(void)
  658. {
  659. debug_requested = 1;
  660. qemu_notify_event();
  661. }
  662. static bool main_loop_should_exit(int *status)
  663. {
  664. RunState r;
  665. ShutdownCause request;
  666. if (qemu_debug_requested()) {
  667. vm_stop(RUN_STATE_DEBUG);
  668. }
  669. if (qemu_suspend_requested()) {
  670. qemu_system_suspend();
  671. }
  672. request = qemu_shutdown_requested();
  673. if (request) {
  674. qemu_kill_report();
  675. qemu_system_shutdown(request);
  676. if (shutdown_action == SHUTDOWN_ACTION_PAUSE) {
  677. vm_stop(RUN_STATE_SHUTDOWN);
  678. } else {
  679. if (shutdown_exit_code != EXIT_SUCCESS) {
  680. *status = shutdown_exit_code;
  681. } else if (request == SHUTDOWN_CAUSE_GUEST_PANIC &&
  682. panic_action == PANIC_ACTION_EXIT_FAILURE) {
  683. *status = EXIT_FAILURE;
  684. }
  685. return true;
  686. }
  687. }
  688. request = qemu_reset_requested();
  689. if (request) {
  690. pause_all_vcpus();
  691. qemu_system_reset(request);
  692. resume_all_vcpus();
  693. /*
  694. * runstate can change in pause_all_vcpus()
  695. * as iothread mutex is unlocked
  696. */
  697. if (!runstate_check(RUN_STATE_RUNNING) &&
  698. !runstate_check(RUN_STATE_INMIGRATE) &&
  699. !runstate_check(RUN_STATE_FINISH_MIGRATE)) {
  700. runstate_set(RUN_STATE_PRELAUNCH);
  701. }
  702. }
  703. if (qemu_wakeup_requested()) {
  704. pause_all_vcpus();
  705. qemu_system_wakeup();
  706. notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
  707. wakeup_reason = QEMU_WAKEUP_REASON_NONE;
  708. resume_all_vcpus();
  709. qapi_event_send_wakeup();
  710. }
  711. if (qemu_powerdown_requested()) {
  712. qemu_system_powerdown();
  713. }
  714. if (qemu_vmstop_requested(&r)) {
  715. vm_stop(r);
  716. }
  717. return false;
  718. }
  719. int qemu_main_loop(void)
  720. {
  721. int status = EXIT_SUCCESS;
  722. while (!main_loop_should_exit(&status)) {
  723. main_loop_wait(false);
  724. }
  725. return status;
  726. }
  727. void qemu_add_exit_notifier(Notifier *notify)
  728. {
  729. notifier_list_add(&exit_notifiers, notify);
  730. }
  731. void qemu_remove_exit_notifier(Notifier *notify)
  732. {
  733. notifier_remove(notify);
  734. }
  735. static void qemu_run_exit_notifiers(void)
  736. {
  737. BQL_LOCK_GUARD();
  738. notifier_list_notify(&exit_notifiers, NULL);
  739. }
  740. void qemu_init_subsystems(void)
  741. {
  742. Error *err = NULL;
  743. os_set_line_buffering();
  744. module_call_init(MODULE_INIT_TRACE);
  745. qemu_init_cpu_list();
  746. qemu_init_cpu_loop();
  747. bql_lock();
  748. atexit(qemu_run_exit_notifiers);
  749. module_call_init(MODULE_INIT_QOM);
  750. module_call_init(MODULE_INIT_MIGRATION);
  751. runstate_init();
  752. precopy_infrastructure_init();
  753. postcopy_infrastructure_init();
  754. monitor_init_globals();
  755. if (qcrypto_init(&err) < 0) {
  756. error_reportf_err(err, "cannot initialize crypto: ");
  757. exit(1);
  758. }
  759. os_setup_early_signal_handling();
  760. bdrv_init_with_whitelist();
  761. socket_init();
  762. }
  763. void qemu_cleanup(int status)
  764. {
  765. gdb_exit(status);
  766. /*
  767. * cleaning up the migration object cancels any existing migration
  768. * try to do this early so that it also stops using devices.
  769. */
  770. migration_shutdown();
  771. /*
  772. * Close the exports before draining the block layer. The export
  773. * drivers may have coroutines yielding on it, so we need to clean
  774. * them up before the drain, as otherwise they may be get stuck in
  775. * blk_wait_while_drained().
  776. */
  777. blk_exp_close_all();
  778. /* No more vcpu or device emulation activity beyond this point */
  779. vm_shutdown();
  780. replay_finish();
  781. /*
  782. * We must cancel all block jobs while the block layer is drained,
  783. * or cancelling will be affected by throttling and thus may block
  784. * for an extended period of time.
  785. * Begin the drained section after vm_shutdown() to avoid requests being
  786. * stuck in the BlockBackend's request queue.
  787. * We do not need to end this section, because we do not want any
  788. * requests happening from here on anyway.
  789. */
  790. bdrv_drain_all_begin();
  791. job_cancel_sync_all();
  792. bdrv_close_all();
  793. /* vhost-user must be cleaned up before chardevs. */
  794. tpm_cleanup();
  795. net_cleanup();
  796. audio_cleanup();
  797. monitor_cleanup();
  798. qemu_chr_cleanup();
  799. user_creatable_cleanup();
  800. /* TODO: unref root container, check all devices are ok */
  801. }