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