xref: /qemu/include/system/replay.h (revision a3d40b5effafdd299d1850f0c9956f60199b5b56)
1 /*
2  * QEMU replay (system interface)
3  *
4  * Copyright (c) 2010-2015 Institute for System Programming
5  *                         of the Russian Academy of Sciences.
6  *
7  * This work is licensed under the terms of the GNU GPL, version 2 or later.
8  * See the COPYING file in the top-level directory.
9  *
10  */
11 #ifndef SYSTEM_REPLAY_H
12 #define SYSTEM_REPLAY_H
13 
14 #include "exec/replay-core.h"
15 #include "qapi/qapi-types-misc.h"
16 #include "qapi/qapi-types-run-state.h"
17 #include "qapi/qapi-types-ui.h"
18 #include "block/aio.h"
19 
20 /* replay clock kinds */
21 enum ReplayClockKind {
22     /* host_clock */
23     REPLAY_CLOCK_HOST,
24     /* virtual_rt_clock */
25     REPLAY_CLOCK_VIRTUAL_RT,
26     REPLAY_CLOCK_COUNT
27 };
28 typedef enum ReplayClockKind ReplayClockKind;
29 
30 /* IDs of the checkpoints */
31 enum ReplayCheckpoint {
32     CHECKPOINT_CLOCK_WARP_START,
33     CHECKPOINT_CLOCK_WARP_ACCOUNT,
34     CHECKPOINT_RESET_REQUESTED,
35     CHECKPOINT_SUSPEND_REQUESTED,
36     CHECKPOINT_CLOCK_VIRTUAL,
37     CHECKPOINT_CLOCK_HOST,
38     CHECKPOINT_CLOCK_VIRTUAL_RT,
39     CHECKPOINT_INIT,
40     CHECKPOINT_RESET,
41     CHECKPOINT_COUNT
42 };
43 typedef enum ReplayCheckpoint ReplayCheckpoint;
44 
45 typedef struct ReplayNetState ReplayNetState;
46 
47 /* Name of the initial VM snapshot */
48 extern char *replay_snapshot;
49 
50 /* Replay locking
51  *
52  * The locks are needed to protect the shared structures and log file
53  * when doing record/replay. They also are the main sync-point between
54  * the main-loop thread and the vCPU thread. This was a role
55  * previously filled by the BQL which has been busy trying to reduce
56  * its impact across the code. This ensures blocks of events stay
57  * sequential and reproducible.
58  */
59 
60 void replay_mutex_lock(void);
61 void replay_mutex_unlock(void);
62 
63 /* Processing the instructions */
64 
65 /*! Returns number of executed instructions. */
66 uint64_t replay_get_current_icount(void);
67 /*! Returns number of instructions to execute in replay mode. */
68 int replay_get_instructions(void);
69 /*! Updates instructions counter in replay mode. */
70 void replay_account_executed_instructions(void);
71 
72 /* Processing clocks and other time sources */
73 
74 /*! Save the specified clock */
75 int64_t replay_save_clock(ReplayClockKind kind, int64_t clock,
76                           int64_t raw_icount);
77 /*! Read the specified clock from the log or return cached data */
78 int64_t replay_read_clock(ReplayClockKind kind, int64_t raw_icount);
79 /*! Saves or reads the clock depending on the current replay mode. */
80 #define REPLAY_CLOCK(clock, value)                                      \
81     !icount_enabled() ? (value) :                                       \
82     (replay_mode == REPLAY_MODE_PLAY                                    \
83         ? replay_read_clock((clock), icount_get_raw())                  \
84         : replay_mode == REPLAY_MODE_RECORD                             \
85             ? replay_save_clock((clock), (value), icount_get_raw())     \
86             : (value))
87 #define REPLAY_CLOCK_LOCKED(clock, value)                               \
88     !icount_enabled() ? (value) :                                       \
89     (replay_mode == REPLAY_MODE_PLAY                                    \
90         ? replay_read_clock((clock), icount_get_raw_locked())           \
91         : replay_mode == REPLAY_MODE_RECORD                             \
92             ? replay_save_clock((clock), (value), icount_get_raw_locked()) \
93             : (value))
94 
95 /* Events */
96 
97 /*! Called when qemu shutdown is requested. */
98 void replay_shutdown_request(ShutdownCause cause);
99 /*! Should be called at check points in the execution.
100     These check points are skipped, if they were not met.
101     Saves checkpoint in the SAVE mode and validates in the PLAY mode.
102     Returns 0 in PLAY mode if checkpoint was not found.
103     Returns 1 in all other cases. */
104 bool replay_checkpoint(ReplayCheckpoint checkpoint);
105 /*! Used to determine that checkpoint or async event is pending.
106     Does not proceed to the next event in the log. */
107 bool replay_has_event(void);
108 /*
109  * Processes the async events added to the queue (while recording)
110  * or reads the events from the file (while replaying).
111  */
112 void replay_async_events(void);
113 
114 /* Asynchronous events queue */
115 
116 /*! Enables storing events in the queue */
117 void replay_enable_events(void);
118 /*! Returns true when saving events is enabled */
119 bool replay_events_enabled(void);
120 /* Flushes events queue */
121 void replay_flush_events(void);
122 /*! Adds bottom half event to the queue */
123 void replay_bh_schedule_event(QEMUBH *bh);
124 /* Adds oneshot bottom half event to the queue */
125 void replay_bh_schedule_oneshot_event(AioContext *ctx,
126     QEMUBHFunc *cb, void *opaque);
127 /*! Adds input event to the queue */
128 void replay_input_event(QemuConsole *src, InputEvent *evt);
129 /*! Adds input sync event to the queue */
130 void replay_input_sync_event(void);
131 /*! Adds block layer event to the queue */
132 void replay_block_event(QEMUBH *bh, uint64_t id);
133 /*! Returns ID for the next block event */
134 uint64_t blkreplay_next_id(void);
135 
136 /* Character device */
137 
138 /*! Registers char driver to save it's events */
139 void replay_register_char_driver(struct Chardev *chr);
140 /*! Saves write to char device event to the log */
141 void replay_chr_be_write(struct Chardev *s, const uint8_t *buf, int len);
142 /*! Writes char write return value to the replay log. */
143 void replay_char_write_event_save(int res, int offset);
144 /*! Reads char write return value from the replay log. */
145 void replay_char_write_event_load(int *res, int *offset);
146 /*! Reads information about read_all character event. */
147 int replay_char_read_all_load(uint8_t *buf);
148 /*! Writes character read_all error code into the replay log. */
149 void replay_char_read_all_save_error(int res);
150 /*! Writes character read_all execution result into the replay log. */
151 void replay_char_read_all_save_buf(uint8_t *buf, int offset);
152 
153 /* Network */
154 
155 /*! Registers replay network filter attached to some backend. */
156 ReplayNetState *replay_register_net(NetFilterState *nfs);
157 /*! Unregisters replay network filter. */
158 void replay_unregister_net(ReplayNetState *rns);
159 /*! Called to write network packet to the replay log. */
160 void replay_net_packet_event(ReplayNetState *rns, unsigned flags,
161                              const struct iovec *iov, int iovcnt);
162 
163 /* Audio */
164 
165 /*! Saves/restores number of played samples of audio out operation. */
166 void replay_audio_out(size_t *played);
167 /*! Saves/restores recorded samples of audio in operation. */
168 void replay_audio_in(size_t *recorded, void *samples, size_t *wpos, size_t size);
169 
170 /* VM state operations */
171 
172 /*! Called at the start of execution.
173     Loads or saves initial vmstate depending on execution mode. */
174 void replay_vmstate_init(void);
175 /*! Called to ensure that replay state is consistent and VM snapshot
176     can be created */
177 bool replay_can_snapshot(void);
178 
179 #endif
180