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