xref: /qemu/include/sysemu/replay.h (revision abff1abf)
1 #ifndef REPLAY_H
2 #define REPLAY_H
3 
4 /*
5  * replay.h
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 "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 extern ReplayMode replay_mode;
48 
49 /* Name of the initial VM snapshot */
50 extern char *replay_snapshot;
51 
52 /* Replay locking
53  *
54  * The locks are needed to protect the shared structures and log file
55  * when doing record/replay. They also are the main sync-point between
56  * the main-loop thread and the vCPU thread. This was a role
57  * previously filled by the BQL which has been busy trying to reduce
58  * its impact across the code. This ensures blocks of events stay
59  * sequential and reproducible.
60  */
61 
62 void replay_mutex_lock(void);
63 void replay_mutex_unlock(void);
64 
65 /* Replay process control functions */
66 
67 /*! Enables recording or saving event log with specified parameters */
68 void replay_configure(struct QemuOpts *opts);
69 /*! Initializes timers used for snapshotting and enables events recording */
70 void replay_start(void);
71 /*! Closes replay log file and frees other resources. */
72 void replay_finish(void);
73 /*! Adds replay blocker with the specified error description */
74 void replay_add_blocker(Error *reason);
75 
76 /* Processing the instructions */
77 
78 /*! Returns number of executed instructions. */
79 uint64_t replay_get_current_icount(void);
80 /*! Returns number of instructions to execute in replay mode. */
81 int replay_get_instructions(void);
82 /*! Updates instructions counter in replay mode. */
83 void replay_account_executed_instructions(void);
84 
85 /* Interrupts and exceptions */
86 
87 /*! Called by exception handler to write or read
88     exception processing events. */
89 bool replay_exception(void);
90 /*! Used to determine that exception is pending.
91     Does not proceed to the next event in the log. */
92 bool replay_has_exception(void);
93 /*! Called by interrupt handlers to write or read
94     interrupt processing events.
95     \return true if interrupt should be processed */
96 bool replay_interrupt(void);
97 /*! Tries to read interrupt event from the file.
98     Returns true, when interrupt request is pending */
99 bool replay_has_interrupt(void);
100 
101 /* Processing clocks and other time sources */
102 
103 /*! Save the specified clock */
104 int64_t replay_save_clock(ReplayClockKind kind, int64_t clock,
105                           int64_t raw_icount);
106 /*! Read the specified clock from the log or return cached data */
107 int64_t replay_read_clock(ReplayClockKind kind);
108 /*! Saves or reads the clock depending on the current replay mode. */
109 #define REPLAY_CLOCK(clock, value)                                      \
110     (replay_mode == REPLAY_MODE_PLAY ? replay_read_clock((clock))       \
111         : replay_mode == REPLAY_MODE_RECORD                             \
112             ? replay_save_clock((clock), (value), cpu_get_icount_raw()) \
113         : (value))
114 #define REPLAY_CLOCK_LOCKED(clock, value)                               \
115     (replay_mode == REPLAY_MODE_PLAY ? replay_read_clock((clock))       \
116         : replay_mode == REPLAY_MODE_RECORD                             \
117             ? replay_save_clock((clock), (value), cpu_get_icount_raw_locked()) \
118         : (value))
119 
120 /* Processing data from random generators */
121 
122 /* Saves the values from the random number generator */
123 void replay_save_random(int ret, void *buf, size_t len);
124 /* Loads the saved values for the random number generator */
125 int replay_read_random(void *buf, size_t len);
126 
127 /* Events */
128 
129 /*! Called when qemu shutdown is requested. */
130 void replay_shutdown_request(ShutdownCause cause);
131 /*! Should be called at check points in the execution.
132     These check points are skipped, if they were not met.
133     Saves checkpoint in the SAVE mode and validates in the PLAY mode.
134     Returns 0 in PLAY mode if checkpoint was not found.
135     Returns 1 in all other cases. */
136 bool replay_checkpoint(ReplayCheckpoint checkpoint);
137 /*! Used to determine that checkpoint is pending.
138     Does not proceed to the next event in the log. */
139 bool replay_has_checkpoint(void);
140 
141 /* Asynchronous events queue */
142 
143 /*! Disables storing events in the queue */
144 void replay_disable_events(void);
145 /*! Enables storing events in the queue */
146 void replay_enable_events(void);
147 /*! Returns true when saving events is enabled */
148 bool replay_events_enabled(void);
149 /*! Adds bottom half event to the queue */
150 void replay_bh_schedule_event(QEMUBH *bh);
151 /* Adds oneshot bottom half event to the queue */
152 void replay_bh_schedule_oneshot_event(AioContext *ctx,
153     QEMUBHFunc *cb, void *opaque);
154 /*! Adds input event to the queue */
155 void replay_input_event(QemuConsole *src, InputEvent *evt);
156 /*! Adds input sync event to the queue */
157 void replay_input_sync_event(void);
158 /*! Adds block layer event to the queue */
159 void replay_block_event(QEMUBH *bh, uint64_t id);
160 /*! Returns ID for the next block event */
161 uint64_t blkreplay_next_id(void);
162 
163 /* Character device */
164 
165 /*! Registers char driver to save it's events */
166 void replay_register_char_driver(struct Chardev *chr);
167 /*! Saves write to char device event to the log */
168 void replay_chr_be_write(struct Chardev *s, uint8_t *buf, int len);
169 /*! Writes char write return value to the replay log. */
170 void replay_char_write_event_save(int res, int offset);
171 /*! Reads char write return value from the replay log. */
172 void replay_char_write_event_load(int *res, int *offset);
173 /*! Reads information about read_all character event. */
174 int replay_char_read_all_load(uint8_t *buf);
175 /*! Writes character read_all error code into the replay log. */
176 void replay_char_read_all_save_error(int res);
177 /*! Writes character read_all execution result into the replay log. */
178 void replay_char_read_all_save_buf(uint8_t *buf, int offset);
179 
180 /* Network */
181 
182 /*! Registers replay network filter attached to some backend. */
183 ReplayNetState *replay_register_net(NetFilterState *nfs);
184 /*! Unregisters replay network filter. */
185 void replay_unregister_net(ReplayNetState *rns);
186 /*! Called to write network packet to the replay log. */
187 void replay_net_packet_event(ReplayNetState *rns, unsigned flags,
188                              const struct iovec *iov, int iovcnt);
189 
190 /* Audio */
191 
192 /*! Saves/restores number of played samples of audio out operation. */
193 void replay_audio_out(size_t *played);
194 /*! Saves/restores recorded samples of audio in operation. */
195 void replay_audio_in(size_t *recorded, void *samples, size_t *wpos, size_t size);
196 
197 /* VM state operations */
198 
199 /*! Called at the start of execution.
200     Loads or saves initial vmstate depending on execution mode. */
201 void replay_vmstate_init(void);
202 /*! Called to ensure that replay state is consistent and VM snapshot
203     can be created */
204 bool replay_can_snapshot(void);
205 
206 #endif
207