master
h 187 lines 6.82 KB
Raw
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 "qemu/aio.h"
19 #include "qemu/audio.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 !icount_enabled() ? (value) : \
83 (replay_mode == REPLAY_MODE_PLAY \
84 ? replay_read_clock((clock), icount_get_raw()) \
85 : replay_mode == REPLAY_MODE_RECORD \
86 ? replay_save_clock((clock), (value), icount_get_raw()) \
87 : (value))
88 #define REPLAY_CLOCK_LOCKED(clock, value) \
89 !icount_enabled() ? (value) : \
90 (replay_mode == REPLAY_MODE_PLAY \
91 ? replay_read_clock((clock), icount_get_raw_locked()) \
92 : replay_mode == REPLAY_MODE_RECORD \
93 ? replay_save_clock((clock), (value), icount_get_raw_locked()) \
94 : (value))
95
96 /* Events */
97
98 /*! Called when qemu shutdown is requested. */
99 void replay_shutdown_request(ShutdownCause cause);
100 /*! Should be called at check points in the execution.
101 These check points are skipped, if they were not met.
102 Saves checkpoint in the SAVE mode and validates in the PLAY mode.
103 Returns 0 in PLAY mode if checkpoint was not found.
104 Returns 1 in all other cases. */
105 bool replay_checkpoint(ReplayCheckpoint checkpoint);
106 /*! Used to determine that checkpoint or async event is pending.
107 Does not proceed to the next event in the log. */
108 bool replay_has_event(void);
109 /*
110 * Processes the async events added to the queue (while recording)
111 * or reads the events from the file (while replaying).
112 */
113 void replay_async_events(void);
114
115 /* Asynchronous events queue */
116
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, QemuInputEvent *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 /*
169 * Start saves/restores recorded samples of audio in operation.
170 * Must be called before replay_audio_in_sample_lr().
171 */
172 void replay_audio_in_start(size_t *recorded);
173 /* Saves/restores recorded samples. */
174 void replay_audio_in_sample_lr(uint64_t *left, uint64_t *right);
175 /* Finish saves/restores recorded samples. */
176 void replay_audio_in_finish(void);
177
178 /* VM state operations */
179
180 /*! Called at the start of execution.
181 Loads or saves initial vmstate depending on execution mode. */
182 void replay_vmstate_init(void);
183 /*! Called to ensure that replay state is consistent and VM snapshot
184 can be created */
185 bool replay_can_snapshot(void);
186
187 #endif