4 * Copyright (c) 2010-2015 Institute for System Programming
5 * of the Russian Academy of Sciences.
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.
12 #include "qemu/osdep.h"
13 #include "qemu/error-report.h"
14 #include "sysemu/replay.h"
15 #include "replay-internal.h"
16 #include "block/aio.h"
18 #include "hw/core/cpu.h"
20 typedef struct Event
{
21 ReplayAsyncEventKind event_kind
;
26 QTAILQ_ENTRY(Event
) events
;
29 static QTAILQ_HEAD(, Event
) events_list
= QTAILQ_HEAD_INITIALIZER(events_list
);
30 static bool events_enabled
;
34 static void replay_run_event(Event
*event
)
36 switch (event
->event_kind
) {
37 case REPLAY_ASYNC_EVENT_BH
:
38 aio_bh_call(event
->opaque
);
40 case REPLAY_ASYNC_EVENT_BH_ONESHOT
:
41 ((QEMUBHFunc
*)event
->opaque
)(event
->opaque2
);
43 case REPLAY_ASYNC_EVENT_INPUT
:
44 qemu_input_event_send_impl(NULL
, (InputEvent
*)event
->opaque
);
45 qapi_free_InputEvent((InputEvent
*)event
->opaque
);
47 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
48 qemu_input_event_sync_impl();
50 case REPLAY_ASYNC_EVENT_CHAR_READ
:
51 replay_event_char_read_run(event
->opaque
);
53 case REPLAY_ASYNC_EVENT_BLOCK
:
54 aio_bh_call(event
->opaque
);
56 case REPLAY_ASYNC_EVENT_NET
:
57 replay_event_net_run(event
->opaque
);
60 error_report("Replay: invalid async event ID (%d) in the queue",
67 void replay_enable_events(void)
69 if (replay_mode
!= REPLAY_MODE_NONE
) {
70 events_enabled
= true;
74 bool replay_has_events(void)
76 return !QTAILQ_EMPTY(&events_list
);
79 void replay_flush_events(void)
81 if (replay_mode
== REPLAY_MODE_NONE
) {
85 g_assert(replay_mutex_locked());
87 while (!QTAILQ_EMPTY(&events_list
)) {
88 Event
*event
= QTAILQ_FIRST(&events_list
);
89 replay_run_event(event
);
90 QTAILQ_REMOVE(&events_list
, event
, events
);
95 void replay_disable_events(void)
97 if (replay_mode
!= REPLAY_MODE_NONE
) {
98 events_enabled
= false;
99 /* Flush events queue before waiting of completion */
100 replay_flush_events();
104 /*! Adds specified async event to the queue */
105 void replay_add_event(ReplayAsyncEventKind event_kind
,
107 void *opaque2
, uint64_t id
)
109 assert(event_kind
< REPLAY_ASYNC_COUNT
);
111 if (!replay_file
|| replay_mode
== REPLAY_MODE_NONE
112 || !events_enabled
) {
114 e
.event_kind
= event_kind
;
118 replay_run_event(&e
);
122 Event
*event
= g_malloc0(sizeof(Event
));
123 event
->event_kind
= event_kind
;
124 event
->opaque
= opaque
;
125 event
->opaque2
= opaque2
;
128 g_assert(replay_mutex_locked());
129 QTAILQ_INSERT_TAIL(&events_list
, event
, events
);
130 qemu_cpu_kick(first_cpu
);
133 void replay_bh_schedule_event(QEMUBH
*bh
)
135 if (events_enabled
) {
136 uint64_t id
= replay_get_current_icount();
137 replay_add_event(REPLAY_ASYNC_EVENT_BH
, bh
, NULL
, id
);
139 qemu_bh_schedule(bh
);
143 void replay_bh_schedule_oneshot_event(AioContext
*ctx
,
144 QEMUBHFunc
*cb
, void *opaque
)
146 if (events_enabled
) {
147 uint64_t id
= replay_get_current_icount();
148 replay_add_event(REPLAY_ASYNC_EVENT_BH_ONESHOT
, cb
, opaque
, id
);
150 aio_bh_schedule_oneshot(ctx
, cb
, opaque
);
154 void replay_add_input_event(struct InputEvent
*event
)
156 replay_add_event(REPLAY_ASYNC_EVENT_INPUT
, event
, NULL
, 0);
159 void replay_add_input_sync_event(void)
161 replay_add_event(REPLAY_ASYNC_EVENT_INPUT_SYNC
, NULL
, NULL
, 0);
164 void replay_block_event(QEMUBH
*bh
, uint64_t id
)
166 if (events_enabled
) {
167 replay_add_event(REPLAY_ASYNC_EVENT_BLOCK
, bh
, NULL
, id
);
169 qemu_bh_schedule(bh
);
173 static void replay_save_event(Event
*event
, int checkpoint
)
175 if (replay_mode
!= REPLAY_MODE_PLAY
) {
176 /* put the event into the file */
177 replay_put_event(EVENT_ASYNC
);
178 replay_put_byte(checkpoint
);
179 replay_put_byte(event
->event_kind
);
181 /* save event-specific data */
182 switch (event
->event_kind
) {
183 case REPLAY_ASYNC_EVENT_BH
:
184 case REPLAY_ASYNC_EVENT_BH_ONESHOT
:
185 replay_put_qword(event
->id
);
187 case REPLAY_ASYNC_EVENT_INPUT
:
188 replay_save_input_event(event
->opaque
);
190 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
192 case REPLAY_ASYNC_EVENT_CHAR_READ
:
193 replay_event_char_read_save(event
->opaque
);
195 case REPLAY_ASYNC_EVENT_BLOCK
:
196 replay_put_qword(event
->id
);
198 case REPLAY_ASYNC_EVENT_NET
:
199 replay_event_net_save(event
->opaque
);
202 error_report("Unknown ID %" PRId64
" of replay event", event
->id
);
208 /* Called with replay mutex locked */
209 void replay_save_events(int checkpoint
)
211 g_assert(replay_mutex_locked());
212 g_assert(checkpoint
!= CHECKPOINT_CLOCK_WARP_START
);
213 g_assert(checkpoint
!= CHECKPOINT_CLOCK_VIRTUAL
);
214 while (!QTAILQ_EMPTY(&events_list
)) {
215 Event
*event
= QTAILQ_FIRST(&events_list
);
216 replay_save_event(event
, checkpoint
);
217 replay_run_event(event
);
218 QTAILQ_REMOVE(&events_list
, event
, events
);
223 static Event
*replay_read_event(int checkpoint
)
226 if (replay_state
.read_event_kind
== -1) {
227 replay_state
.read_event_checkpoint
= replay_get_byte();
228 replay_state
.read_event_kind
= replay_get_byte();
229 replay_state
.read_event_id
= -1;
230 replay_check_error();
233 if (checkpoint
!= replay_state
.read_event_checkpoint
) {
237 /* Events that has not to be in the queue */
238 switch (replay_state
.read_event_kind
) {
239 case REPLAY_ASYNC_EVENT_BH
:
240 case REPLAY_ASYNC_EVENT_BH_ONESHOT
:
241 if (replay_state
.read_event_id
== -1) {
242 replay_state
.read_event_id
= replay_get_qword();
245 case REPLAY_ASYNC_EVENT_INPUT
:
246 event
= g_malloc0(sizeof(Event
));
247 event
->event_kind
= replay_state
.read_event_kind
;
248 event
->opaque
= replay_read_input_event();
250 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
251 event
= g_malloc0(sizeof(Event
));
252 event
->event_kind
= replay_state
.read_event_kind
;
255 case REPLAY_ASYNC_EVENT_CHAR_READ
:
256 event
= g_malloc0(sizeof(Event
));
257 event
->event_kind
= replay_state
.read_event_kind
;
258 event
->opaque
= replay_event_char_read_load();
260 case REPLAY_ASYNC_EVENT_BLOCK
:
261 if (replay_state
.read_event_id
== -1) {
262 replay_state
.read_event_id
= replay_get_qword();
265 case REPLAY_ASYNC_EVENT_NET
:
266 event
= g_malloc0(sizeof(Event
));
267 event
->event_kind
= replay_state
.read_event_kind
;
268 event
->opaque
= replay_event_net_load();
271 error_report("Unknown ID %d of replay event",
272 replay_state
.read_event_kind
);
277 QTAILQ_FOREACH(event
, &events_list
, events
) {
278 if (event
->event_kind
== replay_state
.read_event_kind
279 && (replay_state
.read_event_id
== -1
280 || replay_state
.read_event_id
== event
->id
)) {
286 QTAILQ_REMOVE(&events_list
, event
, events
);
291 /* Read event-specific data */
296 /* Called with replay mutex locked */
297 void replay_read_events(int checkpoint
)
299 g_assert(replay_mutex_locked());
300 while (replay_state
.data_kind
== EVENT_ASYNC
) {
301 Event
*event
= replay_read_event(checkpoint
);
305 replay_finish_event();
306 replay_state
.read_event_kind
= -1;
307 replay_run_event(event
);
313 void replay_init_events(void)
315 replay_state
.read_event_kind
= -1;
318 void replay_finish_events(void)
320 events_enabled
= false;
321 replay_flush_events();
324 bool replay_events_enabled(void)
326 return events_enabled
;
329 uint64_t blkreplay_next_id(void)
331 if (replay_events_enabled()) {
332 return replay_state
.block_request_id
++;