xref: /openbmc/qemu/include/sysemu/replay.h (revision 5b5968c4)
1*5b5968c4SPhilippe Mathieu-Daudé #ifndef SYSEMU_REPLAY_H
2*5b5968c4SPhilippe Mathieu-Daudé #define SYSEMU_REPLAY_H
3d73abd6dSPavel Dovgalyuk 
4d73abd6dSPavel Dovgalyuk /*
5*5b5968c4SPhilippe Mathieu-Daudé  * QEMU replay (system interface)
6d73abd6dSPavel Dovgalyuk  *
7d73abd6dSPavel Dovgalyuk  * Copyright (c) 2010-2015 Institute for System Programming
8d73abd6dSPavel Dovgalyuk  *                         of the Russian Academy of Sciences.
9d73abd6dSPavel Dovgalyuk  *
10d73abd6dSPavel Dovgalyuk  * This work is licensed under the terms of the GNU GPL, version 2 or later.
11d73abd6dSPavel Dovgalyuk  * See the COPYING file in the top-level directory.
12d73abd6dSPavel Dovgalyuk  *
13d73abd6dSPavel Dovgalyuk  */
14d73abd6dSPavel Dovgalyuk 
15*5b5968c4SPhilippe Mathieu-Daudé #include "exec/replay-core.h"
16112ed241SMarkus Armbruster #include "qapi/qapi-types-misc.h"
17d5938f29SMarkus Armbruster #include "qapi/qapi-types-run-state.h"
18112ed241SMarkus Armbruster #include "qapi/qapi-types-ui.h"
19e4ec5ad4SPavel Dovgalyuk #include "block/aio.h"
20d73abd6dSPavel Dovgalyuk 
218eda206eSPavel Dovgalyuk /* replay clock kinds */
228eda206eSPavel Dovgalyuk enum ReplayClockKind {
238eda206eSPavel Dovgalyuk     /* host_clock */
248eda206eSPavel Dovgalyuk     REPLAY_CLOCK_HOST,
258eda206eSPavel Dovgalyuk     /* virtual_rt_clock */
268eda206eSPavel Dovgalyuk     REPLAY_CLOCK_VIRTUAL_RT,
278eda206eSPavel Dovgalyuk     REPLAY_CLOCK_COUNT
288eda206eSPavel Dovgalyuk };
298eda206eSPavel Dovgalyuk typedef enum ReplayClockKind ReplayClockKind;
308eda206eSPavel Dovgalyuk 
318bd7f71dSPavel Dovgalyuk /* IDs of the checkpoints */
328bd7f71dSPavel Dovgalyuk enum ReplayCheckpoint {
33e76d1798SPavel Dovgalyuk     CHECKPOINT_CLOCK_WARP_START,
34e76d1798SPavel Dovgalyuk     CHECKPOINT_CLOCK_WARP_ACCOUNT,
358bd7f71dSPavel Dovgalyuk     CHECKPOINT_RESET_REQUESTED,
368bd7f71dSPavel Dovgalyuk     CHECKPOINT_SUSPEND_REQUESTED,
378bd7f71dSPavel Dovgalyuk     CHECKPOINT_CLOCK_VIRTUAL,
388bd7f71dSPavel Dovgalyuk     CHECKPOINT_CLOCK_HOST,
398bd7f71dSPavel Dovgalyuk     CHECKPOINT_CLOCK_VIRTUAL_RT,
408bd7f71dSPavel Dovgalyuk     CHECKPOINT_INIT,
418bd7f71dSPavel Dovgalyuk     CHECKPOINT_RESET,
428bd7f71dSPavel Dovgalyuk     CHECKPOINT_COUNT
438bd7f71dSPavel Dovgalyuk };
448bd7f71dSPavel Dovgalyuk typedef enum ReplayCheckpoint ReplayCheckpoint;
458bd7f71dSPavel Dovgalyuk 
46646c5478SPavel Dovgalyuk typedef struct ReplayNetState ReplayNetState;
47646c5478SPavel Dovgalyuk 
489c2037d0SPavel Dovgalyuk /* Name of the initial VM snapshot */
499c2037d0SPavel Dovgalyuk extern char *replay_snapshot;
509c2037d0SPavel Dovgalyuk 
51a36544d3SAlex Bennée /* Replay locking
52a36544d3SAlex Bennée  *
53a36544d3SAlex Bennée  * The locks are needed to protect the shared structures and log file
54a36544d3SAlex Bennée  * when doing record/replay. They also are the main sync-point between
55a36544d3SAlex Bennée  * the main-loop thread and the vCPU thread. This was a role
56a36544d3SAlex Bennée  * previously filled by the BQL which has been busy trying to reduce
57a36544d3SAlex Bennée  * its impact across the code. This ensures blocks of events stay
58a36544d3SAlex Bennée  * sequential and reproducible.
59a36544d3SAlex Bennée  */
60a36544d3SAlex Bennée 
61a36544d3SAlex Bennée void replay_mutex_lock(void);
62a36544d3SAlex Bennée void replay_mutex_unlock(void);
63a36544d3SAlex Bennée 
6426bc60acSPavel Dovgalyuk /* Processing the instructions */
6526bc60acSPavel Dovgalyuk 
6626bc60acSPavel Dovgalyuk /*! Returns number of executed instructions. */
6713f26713SPavel Dovgalyuk uint64_t replay_get_current_icount(void);
688b427044SPavel Dovgalyuk /*! Returns number of instructions to execute in replay mode. */
698b427044SPavel Dovgalyuk int replay_get_instructions(void);
708b427044SPavel Dovgalyuk /*! Updates instructions counter in replay mode. */
718b427044SPavel Dovgalyuk void replay_account_executed_instructions(void);
7226bc60acSPavel Dovgalyuk 
738eda206eSPavel Dovgalyuk /* Processing clocks and other time sources */
748eda206eSPavel Dovgalyuk 
758eda206eSPavel Dovgalyuk /*! Save the specified clock */
7674c0b816SPaolo Bonzini int64_t replay_save_clock(ReplayClockKind kind, int64_t clock,
7774c0b816SPaolo Bonzini                           int64_t raw_icount);
788eda206eSPavel Dovgalyuk /*! Read the specified clock from the log or return cached data */
79366a85e4SPavel Dovgalyuk int64_t replay_read_clock(ReplayClockKind kind, int64_t raw_icount);
808eda206eSPavel Dovgalyuk /*! Saves or reads the clock depending on the current replay mode. */
818eda206eSPavel Dovgalyuk #define REPLAY_CLOCK(clock, value)                                      \
82366a85e4SPavel Dovgalyuk     (replay_mode == REPLAY_MODE_PLAY                                    \
83366a85e4SPavel Dovgalyuk         ? replay_read_clock((clock), icount_get_raw())                  \
848eda206eSPavel Dovgalyuk         : replay_mode == REPLAY_MODE_RECORD                             \
858191d368SClaudio Fontana             ? replay_save_clock((clock), (value), icount_get_raw())     \
8674c0b816SPaolo Bonzini             : (value))
8774c0b816SPaolo Bonzini #define REPLAY_CLOCK_LOCKED(clock, value)                               \
88366a85e4SPavel Dovgalyuk     (replay_mode == REPLAY_MODE_PLAY                                    \
89366a85e4SPavel Dovgalyuk         ? replay_read_clock((clock), icount_get_raw_locked())           \
9074c0b816SPaolo Bonzini         : replay_mode == REPLAY_MODE_RECORD                             \
918191d368SClaudio Fontana             ? replay_save_clock((clock), (value), icount_get_raw_locked()) \
928eda206eSPavel Dovgalyuk             : (value))
938eda206eSPavel Dovgalyuk 
94b60c48a7SPavel Dovgalyuk /* Events */
95b60c48a7SPavel Dovgalyuk 
96b60c48a7SPavel Dovgalyuk /*! Called when qemu shutdown is requested. */
97802f045aSEric Blake void replay_shutdown_request(ShutdownCause cause);
988bd7f71dSPavel Dovgalyuk /*! Should be called at check points in the execution.
998bd7f71dSPavel Dovgalyuk     These check points are skipped, if they were not met.
1008bd7f71dSPavel Dovgalyuk     Saves checkpoint in the SAVE mode and validates in the PLAY mode.
1018bd7f71dSPavel Dovgalyuk     Returns 0 in PLAY mode if checkpoint was not found.
1028bd7f71dSPavel Dovgalyuk     Returns 1 in all other cases. */
1038bd7f71dSPavel Dovgalyuk bool replay_checkpoint(ReplayCheckpoint checkpoint);
10460618e2dSPavel Dovgalyuk /*! Used to determine that checkpoint or async event is pending.
1050c08185fSPavel Dovgalyuk     Does not proceed to the next event in the log. */
10660618e2dSPavel Dovgalyuk bool replay_has_event(void);
10760618e2dSPavel Dovgalyuk /*
10860618e2dSPavel Dovgalyuk  * Processes the async events added to the queue (while recording)
10960618e2dSPavel Dovgalyuk  * or reads the events from the file (while replaying).
11060618e2dSPavel Dovgalyuk  */
11160618e2dSPavel Dovgalyuk void replay_async_events(void);
112b60c48a7SPavel Dovgalyuk 
113c0c071d0SPavel Dovgalyuk /* Asynchronous events queue */
114c0c071d0SPavel Dovgalyuk 
115c0c071d0SPavel Dovgalyuk /*! Disables storing events in the queue */
116c0c071d0SPavel Dovgalyuk void replay_disable_events(void);
1176d0ceb80SPavel Dovgalyuk /*! Enables storing events in the queue */
1186d0ceb80SPavel Dovgalyuk void replay_enable_events(void);
119c0c071d0SPavel Dovgalyuk /*! Returns true when saving events is enabled */
120c0c071d0SPavel Dovgalyuk bool replay_events_enabled(void);
121f9a9fb65SPavel Dovgalyuk /* Flushes events queue */
122f9a9fb65SPavel Dovgalyuk void replay_flush_events(void);
1238a354bd9SPavel Dovgalyuk /*! Adds bottom half event to the queue */
1248a354bd9SPavel Dovgalyuk void replay_bh_schedule_event(QEMUBH *bh);
125e4ec5ad4SPavel Dovgalyuk /* Adds oneshot bottom half event to the queue */
126e4ec5ad4SPavel Dovgalyuk void replay_bh_schedule_oneshot_event(AioContext *ctx,
127e4ec5ad4SPavel Dovgalyuk     QEMUBHFunc *cb, void *opaque);
128ee312992SPavel Dovgalyuk /*! Adds input event to the queue */
129ee312992SPavel Dovgalyuk void replay_input_event(QemuConsole *src, InputEvent *evt);
130ee312992SPavel Dovgalyuk /*! Adds input sync event to the queue */
131ee312992SPavel Dovgalyuk void replay_input_sync_event(void);
13263785678SPavel Dovgalyuk /*! Adds block layer event to the queue */
13363785678SPavel Dovgalyuk void replay_block_event(QEMUBH *bh, uint64_t id);
1346d0ceb80SPavel Dovgalyuk /*! Returns ID for the next block event */
1356d0ceb80SPavel Dovgalyuk uint64_t blkreplay_next_id(void);
1366f060969SPavel Dovgalyuk 
13733577b47SPavel Dovgalyuk /* Character device */
13833577b47SPavel Dovgalyuk 
13933577b47SPavel Dovgalyuk /*! Registers char driver to save it's events */
1400ec7b3e7SMarc-André Lureau void replay_register_char_driver(struct Chardev *chr);
14133577b47SPavel Dovgalyuk /*! Saves write to char device event to the log */
1428f9abdf5SArwed Meyer void replay_chr_be_write(struct Chardev *s, const uint8_t *buf, int len);
14333577b47SPavel Dovgalyuk /*! Writes char write return value to the replay log. */
14433577b47SPavel Dovgalyuk void replay_char_write_event_save(int res, int offset);
14533577b47SPavel Dovgalyuk /*! Reads char write return value from the replay log. */
14633577b47SPavel Dovgalyuk void replay_char_write_event_load(int *res, int *offset);
14733577b47SPavel Dovgalyuk /*! Reads information about read_all character event. */
14833577b47SPavel Dovgalyuk int replay_char_read_all_load(uint8_t *buf);
14933577b47SPavel Dovgalyuk /*! Writes character read_all error code into the replay log. */
15033577b47SPavel Dovgalyuk void replay_char_read_all_save_error(int res);
15133577b47SPavel Dovgalyuk /*! Writes character read_all execution result into the replay log. */
15233577b47SPavel Dovgalyuk void replay_char_read_all_save_buf(uint8_t *buf, int offset);
15333577b47SPavel Dovgalyuk 
154646c5478SPavel Dovgalyuk /* Network */
155646c5478SPavel Dovgalyuk 
156646c5478SPavel Dovgalyuk /*! Registers replay network filter attached to some backend. */
157646c5478SPavel Dovgalyuk ReplayNetState *replay_register_net(NetFilterState *nfs);
158646c5478SPavel Dovgalyuk /*! Unregisters replay network filter. */
159646c5478SPavel Dovgalyuk void replay_unregister_net(ReplayNetState *rns);
160646c5478SPavel Dovgalyuk /*! Called to write network packet to the replay log. */
161646c5478SPavel Dovgalyuk void replay_net_packet_event(ReplayNetState *rns, unsigned flags,
162646c5478SPavel Dovgalyuk                              const struct iovec *iov, int iovcnt);
163646c5478SPavel Dovgalyuk 
1643d4d16f4SPavel Dovgalyuk /* Audio */
1653d4d16f4SPavel Dovgalyuk 
1663d4d16f4SPavel Dovgalyuk /*! Saves/restores number of played samples of audio out operation. */
1677520462bSKővágó, Zoltán void replay_audio_out(size_t *played);
1683d4d16f4SPavel Dovgalyuk /*! Saves/restores recorded samples of audio in operation. */
1697520462bSKővágó, Zoltán void replay_audio_in(size_t *recorded, void *samples, size_t *wpos, size_t size);
1703d4d16f4SPavel Dovgalyuk 
1719c2037d0SPavel Dovgalyuk /* VM state operations */
1729c2037d0SPavel Dovgalyuk 
1739c2037d0SPavel Dovgalyuk /*! Called at the start of execution.
1749c2037d0SPavel Dovgalyuk     Loads or saves initial vmstate depending on execution mode. */
1759c2037d0SPavel Dovgalyuk void replay_vmstate_init(void);
176377b21ccSPavel Dovgalyuk /*! Called to ensure that replay state is consistent and VM snapshot
177377b21ccSPavel Dovgalyuk     can be created */
178377b21ccSPavel Dovgalyuk bool replay_can_snapshot(void);
1799c2037d0SPavel Dovgalyuk 
180d73abd6dSPavel Dovgalyuk #endif
181