xref: /openbmc/qemu/replay/replay-events.c (revision 33577b47c64435fcc2a1bc01c7e82534256f1fc3)
1c0c071d0SPavel Dovgalyuk /*
2c0c071d0SPavel Dovgalyuk  * replay-events.c
3c0c071d0SPavel Dovgalyuk  *
4c0c071d0SPavel Dovgalyuk  * Copyright (c) 2010-2015 Institute for System Programming
5c0c071d0SPavel Dovgalyuk  *                         of the Russian Academy of Sciences.
6c0c071d0SPavel Dovgalyuk  *
7c0c071d0SPavel Dovgalyuk  * This work is licensed under the terms of the GNU GPL, version 2 or later.
8c0c071d0SPavel Dovgalyuk  * See the COPYING file in the top-level directory.
9c0c071d0SPavel Dovgalyuk  *
10c0c071d0SPavel Dovgalyuk  */
11c0c071d0SPavel Dovgalyuk 
12d38ea87aSPeter Maydell #include "qemu/osdep.h"
13c0c071d0SPavel Dovgalyuk #include "qemu-common.h"
14c0c071d0SPavel Dovgalyuk #include "qemu/error-report.h"
15c0c071d0SPavel Dovgalyuk #include "sysemu/replay.h"
16c0c071d0SPavel Dovgalyuk #include "replay-internal.h"
178a354bd9SPavel Dovgalyuk #include "block/aio.h"
18ee312992SPavel Dovgalyuk #include "ui/input.h"
19c0c071d0SPavel Dovgalyuk 
20c0c071d0SPavel Dovgalyuk typedef struct Event {
21c0c071d0SPavel Dovgalyuk     ReplayAsyncEventKind event_kind;
22c0c071d0SPavel Dovgalyuk     void *opaque;
23c0c071d0SPavel Dovgalyuk     void *opaque2;
24c0c071d0SPavel Dovgalyuk     uint64_t id;
25c0c071d0SPavel Dovgalyuk 
26c0c071d0SPavel Dovgalyuk     QTAILQ_ENTRY(Event) events;
27c0c071d0SPavel Dovgalyuk } Event;
28c0c071d0SPavel Dovgalyuk 
29c0c071d0SPavel Dovgalyuk static QTAILQ_HEAD(, Event) events_list = QTAILQ_HEAD_INITIALIZER(events_list);
30c0c071d0SPavel Dovgalyuk static unsigned int read_event_kind = -1;
31c0c071d0SPavel Dovgalyuk static uint64_t read_id = -1;
32c0c071d0SPavel Dovgalyuk static int read_checkpoint = -1;
33c0c071d0SPavel Dovgalyuk 
34c0c071d0SPavel Dovgalyuk static bool events_enabled;
35c0c071d0SPavel Dovgalyuk 
36c0c071d0SPavel Dovgalyuk /* Functions */
37c0c071d0SPavel Dovgalyuk 
38c0c071d0SPavel Dovgalyuk static void replay_run_event(Event *event)
39c0c071d0SPavel Dovgalyuk {
40c0c071d0SPavel Dovgalyuk     switch (event->event_kind) {
418a354bd9SPavel Dovgalyuk     case REPLAY_ASYNC_EVENT_BH:
428a354bd9SPavel Dovgalyuk         aio_bh_call(event->opaque);
438a354bd9SPavel Dovgalyuk         break;
44ee312992SPavel Dovgalyuk     case REPLAY_ASYNC_EVENT_INPUT:
45ee312992SPavel Dovgalyuk         qemu_input_event_send_impl(NULL, (InputEvent *)event->opaque);
46ee312992SPavel Dovgalyuk         qapi_free_InputEvent((InputEvent *)event->opaque);
47ee312992SPavel Dovgalyuk         break;
48ee312992SPavel Dovgalyuk     case REPLAY_ASYNC_EVENT_INPUT_SYNC:
49ee312992SPavel Dovgalyuk         qemu_input_event_sync_impl();
50ee312992SPavel Dovgalyuk         break;
51*33577b47SPavel Dovgalyuk     case REPLAY_ASYNC_EVENT_CHAR_READ:
52*33577b47SPavel Dovgalyuk         replay_event_char_read_run(event->opaque);
53*33577b47SPavel Dovgalyuk         break;
54c0c071d0SPavel Dovgalyuk     default:
55c0c071d0SPavel Dovgalyuk         error_report("Replay: invalid async event ID (%d) in the queue",
56c0c071d0SPavel Dovgalyuk                     event->event_kind);
57c0c071d0SPavel Dovgalyuk         exit(1);
58c0c071d0SPavel Dovgalyuk         break;
59c0c071d0SPavel Dovgalyuk     }
60c0c071d0SPavel Dovgalyuk }
61c0c071d0SPavel Dovgalyuk 
62c0c071d0SPavel Dovgalyuk void replay_enable_events(void)
63c0c071d0SPavel Dovgalyuk {
64c0c071d0SPavel Dovgalyuk     events_enabled = true;
65c0c071d0SPavel Dovgalyuk }
66c0c071d0SPavel Dovgalyuk 
67c0c071d0SPavel Dovgalyuk bool replay_has_events(void)
68c0c071d0SPavel Dovgalyuk {
69c0c071d0SPavel Dovgalyuk     return !QTAILQ_EMPTY(&events_list);
70c0c071d0SPavel Dovgalyuk }
71c0c071d0SPavel Dovgalyuk 
72c0c071d0SPavel Dovgalyuk void replay_flush_events(void)
73c0c071d0SPavel Dovgalyuk {
74c0c071d0SPavel Dovgalyuk     replay_mutex_lock();
75c0c071d0SPavel Dovgalyuk     while (!QTAILQ_EMPTY(&events_list)) {
76c0c071d0SPavel Dovgalyuk         Event *event = QTAILQ_FIRST(&events_list);
77c0c071d0SPavel Dovgalyuk         replay_mutex_unlock();
78c0c071d0SPavel Dovgalyuk         replay_run_event(event);
79c0c071d0SPavel Dovgalyuk         replay_mutex_lock();
80c0c071d0SPavel Dovgalyuk         QTAILQ_REMOVE(&events_list, event, events);
81c0c071d0SPavel Dovgalyuk         g_free(event);
82c0c071d0SPavel Dovgalyuk     }
83c0c071d0SPavel Dovgalyuk     replay_mutex_unlock();
84c0c071d0SPavel Dovgalyuk }
85c0c071d0SPavel Dovgalyuk 
86c0c071d0SPavel Dovgalyuk void replay_disable_events(void)
87c0c071d0SPavel Dovgalyuk {
88c0c071d0SPavel Dovgalyuk     if (replay_mode != REPLAY_MODE_NONE) {
89c0c071d0SPavel Dovgalyuk         events_enabled = false;
90c0c071d0SPavel Dovgalyuk         /* Flush events queue before waiting of completion */
91c0c071d0SPavel Dovgalyuk         replay_flush_events();
92c0c071d0SPavel Dovgalyuk     }
93c0c071d0SPavel Dovgalyuk }
94c0c071d0SPavel Dovgalyuk 
95c0c071d0SPavel Dovgalyuk void replay_clear_events(void)
96c0c071d0SPavel Dovgalyuk {
97c0c071d0SPavel Dovgalyuk     replay_mutex_lock();
98c0c071d0SPavel Dovgalyuk     while (!QTAILQ_EMPTY(&events_list)) {
99c0c071d0SPavel Dovgalyuk         Event *event = QTAILQ_FIRST(&events_list);
100c0c071d0SPavel Dovgalyuk         QTAILQ_REMOVE(&events_list, event, events);
101c0c071d0SPavel Dovgalyuk 
102c0c071d0SPavel Dovgalyuk         g_free(event);
103c0c071d0SPavel Dovgalyuk     }
104c0c071d0SPavel Dovgalyuk     replay_mutex_unlock();
105c0c071d0SPavel Dovgalyuk }
106c0c071d0SPavel Dovgalyuk 
107c0c071d0SPavel Dovgalyuk /*! Adds specified async event to the queue */
108*33577b47SPavel Dovgalyuk void replay_add_event(ReplayAsyncEventKind event_kind,
109c0c071d0SPavel Dovgalyuk                       void *opaque,
110c0c071d0SPavel Dovgalyuk                       void *opaque2, uint64_t id)
111c0c071d0SPavel Dovgalyuk {
112c0c071d0SPavel Dovgalyuk     assert(event_kind < REPLAY_ASYNC_COUNT);
113c0c071d0SPavel Dovgalyuk 
114c0c071d0SPavel Dovgalyuk     if (!replay_file || replay_mode == REPLAY_MODE_NONE
115c0c071d0SPavel Dovgalyuk         || !events_enabled) {
116c0c071d0SPavel Dovgalyuk         Event e;
117c0c071d0SPavel Dovgalyuk         e.event_kind = event_kind;
118c0c071d0SPavel Dovgalyuk         e.opaque = opaque;
119c0c071d0SPavel Dovgalyuk         e.opaque2 = opaque2;
120c0c071d0SPavel Dovgalyuk         e.id = id;
121c0c071d0SPavel Dovgalyuk         replay_run_event(&e);
122c0c071d0SPavel Dovgalyuk         return;
123c0c071d0SPavel Dovgalyuk     }
124c0c071d0SPavel Dovgalyuk 
125c0c071d0SPavel Dovgalyuk     Event *event = g_malloc0(sizeof(Event));
126c0c071d0SPavel Dovgalyuk     event->event_kind = event_kind;
127c0c071d0SPavel Dovgalyuk     event->opaque = opaque;
128c0c071d0SPavel Dovgalyuk     event->opaque2 = opaque2;
129c0c071d0SPavel Dovgalyuk     event->id = id;
130c0c071d0SPavel Dovgalyuk 
131c0c071d0SPavel Dovgalyuk     replay_mutex_lock();
132c0c071d0SPavel Dovgalyuk     QTAILQ_INSERT_TAIL(&events_list, event, events);
133c0c071d0SPavel Dovgalyuk     replay_mutex_unlock();
134c0c071d0SPavel Dovgalyuk }
1358a354bd9SPavel Dovgalyuk 
1368a354bd9SPavel Dovgalyuk void replay_bh_schedule_event(QEMUBH *bh)
1378a354bd9SPavel Dovgalyuk {
1388a354bd9SPavel Dovgalyuk     if (replay_mode != REPLAY_MODE_NONE) {
1398a354bd9SPavel Dovgalyuk         uint64_t id = replay_get_current_step();
1408a354bd9SPavel Dovgalyuk         replay_add_event(REPLAY_ASYNC_EVENT_BH, bh, NULL, id);
1418a354bd9SPavel Dovgalyuk     } else {
1428a354bd9SPavel Dovgalyuk         qemu_bh_schedule(bh);
1438a354bd9SPavel Dovgalyuk     }
1448a354bd9SPavel Dovgalyuk }
145c0c071d0SPavel Dovgalyuk 
146ee312992SPavel Dovgalyuk void replay_add_input_event(struct InputEvent *event)
147ee312992SPavel Dovgalyuk {
148ee312992SPavel Dovgalyuk     replay_add_event(REPLAY_ASYNC_EVENT_INPUT, event, NULL, 0);
149ee312992SPavel Dovgalyuk }
150ee312992SPavel Dovgalyuk 
151ee312992SPavel Dovgalyuk void replay_add_input_sync_event(void)
152ee312992SPavel Dovgalyuk {
153ee312992SPavel Dovgalyuk     replay_add_event(REPLAY_ASYNC_EVENT_INPUT_SYNC, NULL, NULL, 0);
154ee312992SPavel Dovgalyuk }
155ee312992SPavel Dovgalyuk 
156c0c071d0SPavel Dovgalyuk static void replay_save_event(Event *event, int checkpoint)
157c0c071d0SPavel Dovgalyuk {
158c0c071d0SPavel Dovgalyuk     if (replay_mode != REPLAY_MODE_PLAY) {
159c0c071d0SPavel Dovgalyuk         /* put the event into the file */
160c0c071d0SPavel Dovgalyuk         replay_put_event(EVENT_ASYNC);
161c0c071d0SPavel Dovgalyuk         replay_put_byte(checkpoint);
162c0c071d0SPavel Dovgalyuk         replay_put_byte(event->event_kind);
163c0c071d0SPavel Dovgalyuk 
164c0c071d0SPavel Dovgalyuk         /* save event-specific data */
165c0c071d0SPavel Dovgalyuk         switch (event->event_kind) {
1668a354bd9SPavel Dovgalyuk         case REPLAY_ASYNC_EVENT_BH:
1678a354bd9SPavel Dovgalyuk             replay_put_qword(event->id);
1688a354bd9SPavel Dovgalyuk             break;
169ee312992SPavel Dovgalyuk         case REPLAY_ASYNC_EVENT_INPUT:
170ee312992SPavel Dovgalyuk             replay_save_input_event(event->opaque);
171ee312992SPavel Dovgalyuk             break;
172ee312992SPavel Dovgalyuk         case REPLAY_ASYNC_EVENT_INPUT_SYNC:
173ee312992SPavel Dovgalyuk             break;
174*33577b47SPavel Dovgalyuk         case REPLAY_ASYNC_EVENT_CHAR_READ:
175*33577b47SPavel Dovgalyuk             replay_event_char_read_save(event->opaque);
176*33577b47SPavel Dovgalyuk             break;
177c0c071d0SPavel Dovgalyuk         default:
178c0c071d0SPavel Dovgalyuk             error_report("Unknown ID %d of replay event", read_event_kind);
179c0c071d0SPavel Dovgalyuk             exit(1);
180c0c071d0SPavel Dovgalyuk         }
181c0c071d0SPavel Dovgalyuk     }
182c0c071d0SPavel Dovgalyuk }
183c0c071d0SPavel Dovgalyuk 
184c0c071d0SPavel Dovgalyuk /* Called with replay mutex locked */
185c0c071d0SPavel Dovgalyuk void replay_save_events(int checkpoint)
186c0c071d0SPavel Dovgalyuk {
187c0c071d0SPavel Dovgalyuk     while (!QTAILQ_EMPTY(&events_list)) {
188c0c071d0SPavel Dovgalyuk         Event *event = QTAILQ_FIRST(&events_list);
189c0c071d0SPavel Dovgalyuk         replay_save_event(event, checkpoint);
190c0c071d0SPavel Dovgalyuk 
191c0c071d0SPavel Dovgalyuk         replay_mutex_unlock();
192c0c071d0SPavel Dovgalyuk         replay_run_event(event);
193c0c071d0SPavel Dovgalyuk         replay_mutex_lock();
194c0c071d0SPavel Dovgalyuk         QTAILQ_REMOVE(&events_list, event, events);
195c0c071d0SPavel Dovgalyuk         g_free(event);
196c0c071d0SPavel Dovgalyuk     }
197c0c071d0SPavel Dovgalyuk }
198c0c071d0SPavel Dovgalyuk 
199c0c071d0SPavel Dovgalyuk static Event *replay_read_event(int checkpoint)
200c0c071d0SPavel Dovgalyuk {
201c0c071d0SPavel Dovgalyuk     Event *event;
202c0c071d0SPavel Dovgalyuk     if (read_event_kind == -1) {
203c0c071d0SPavel Dovgalyuk         read_checkpoint = replay_get_byte();
204c0c071d0SPavel Dovgalyuk         read_event_kind = replay_get_byte();
205c0c071d0SPavel Dovgalyuk         read_id = -1;
206c0c071d0SPavel Dovgalyuk         replay_check_error();
207c0c071d0SPavel Dovgalyuk     }
208c0c071d0SPavel Dovgalyuk 
209c0c071d0SPavel Dovgalyuk     if (checkpoint != read_checkpoint) {
210c0c071d0SPavel Dovgalyuk         return NULL;
211c0c071d0SPavel Dovgalyuk     }
212c0c071d0SPavel Dovgalyuk 
213c0c071d0SPavel Dovgalyuk     /* Events that has not to be in the queue */
214c0c071d0SPavel Dovgalyuk     switch (read_event_kind) {
2158a354bd9SPavel Dovgalyuk     case REPLAY_ASYNC_EVENT_BH:
2168a354bd9SPavel Dovgalyuk         if (read_id == -1) {
2178a354bd9SPavel Dovgalyuk             read_id = replay_get_qword();
2188a354bd9SPavel Dovgalyuk         }
2198a354bd9SPavel Dovgalyuk         break;
220ee312992SPavel Dovgalyuk     case REPLAY_ASYNC_EVENT_INPUT:
221ee312992SPavel Dovgalyuk         event = g_malloc0(sizeof(Event));
222ee312992SPavel Dovgalyuk         event->event_kind = read_event_kind;
223ee312992SPavel Dovgalyuk         event->opaque = replay_read_input_event();
224ee312992SPavel Dovgalyuk         return event;
225ee312992SPavel Dovgalyuk     case REPLAY_ASYNC_EVENT_INPUT_SYNC:
226ee312992SPavel Dovgalyuk         event = g_malloc0(sizeof(Event));
227ee312992SPavel Dovgalyuk         event->event_kind = read_event_kind;
228ee312992SPavel Dovgalyuk         event->opaque = 0;
229ee312992SPavel Dovgalyuk         return event;
230*33577b47SPavel Dovgalyuk     case REPLAY_ASYNC_EVENT_CHAR_READ:
231*33577b47SPavel Dovgalyuk         event = g_malloc0(sizeof(Event));
232*33577b47SPavel Dovgalyuk         event->event_kind = read_event_kind;
233*33577b47SPavel Dovgalyuk         event->opaque = replay_event_char_read_load();
234*33577b47SPavel Dovgalyuk         return event;
235c0c071d0SPavel Dovgalyuk     default:
236c0c071d0SPavel Dovgalyuk         error_report("Unknown ID %d of replay event", read_event_kind);
237c0c071d0SPavel Dovgalyuk         exit(1);
238c0c071d0SPavel Dovgalyuk         break;
239c0c071d0SPavel Dovgalyuk     }
240c0c071d0SPavel Dovgalyuk 
241c0c071d0SPavel Dovgalyuk     QTAILQ_FOREACH(event, &events_list, events) {
242c0c071d0SPavel Dovgalyuk         if (event->event_kind == read_event_kind
243c0c071d0SPavel Dovgalyuk             && (read_id == -1 || read_id == event->id)) {
244c0c071d0SPavel Dovgalyuk             break;
245c0c071d0SPavel Dovgalyuk         }
246c0c071d0SPavel Dovgalyuk     }
247c0c071d0SPavel Dovgalyuk 
248c0c071d0SPavel Dovgalyuk     if (event) {
249c0c071d0SPavel Dovgalyuk         QTAILQ_REMOVE(&events_list, event, events);
250c0c071d0SPavel Dovgalyuk     } else {
251c0c071d0SPavel Dovgalyuk         return NULL;
252c0c071d0SPavel Dovgalyuk     }
253c0c071d0SPavel Dovgalyuk 
254c0c071d0SPavel Dovgalyuk     /* Read event-specific data */
255c0c071d0SPavel Dovgalyuk 
256c0c071d0SPavel Dovgalyuk     return event;
257c0c071d0SPavel Dovgalyuk }
258c0c071d0SPavel Dovgalyuk 
259c0c071d0SPavel Dovgalyuk /* Called with replay mutex locked */
260c0c071d0SPavel Dovgalyuk void replay_read_events(int checkpoint)
261c0c071d0SPavel Dovgalyuk {
262c0c071d0SPavel Dovgalyuk     while (replay_data_kind == EVENT_ASYNC) {
263c0c071d0SPavel Dovgalyuk         Event *event = replay_read_event(checkpoint);
264c0c071d0SPavel Dovgalyuk         if (!event) {
265c0c071d0SPavel Dovgalyuk             break;
266c0c071d0SPavel Dovgalyuk         }
267c0c071d0SPavel Dovgalyuk         replay_mutex_unlock();
268c0c071d0SPavel Dovgalyuk         replay_run_event(event);
269c0c071d0SPavel Dovgalyuk         replay_mutex_lock();
270c0c071d0SPavel Dovgalyuk 
271c0c071d0SPavel Dovgalyuk         g_free(event);
272c0c071d0SPavel Dovgalyuk         replay_finish_event();
273c0c071d0SPavel Dovgalyuk         read_event_kind = -1;
274c0c071d0SPavel Dovgalyuk     }
275c0c071d0SPavel Dovgalyuk }
276c0c071d0SPavel Dovgalyuk 
277c0c071d0SPavel Dovgalyuk void replay_init_events(void)
278c0c071d0SPavel Dovgalyuk {
279c0c071d0SPavel Dovgalyuk     read_event_kind = -1;
280c0c071d0SPavel Dovgalyuk }
281c0c071d0SPavel Dovgalyuk 
282c0c071d0SPavel Dovgalyuk void replay_finish_events(void)
283c0c071d0SPavel Dovgalyuk {
284c0c071d0SPavel Dovgalyuk     events_enabled = false;
285c0c071d0SPavel Dovgalyuk     replay_clear_events();
286c0c071d0SPavel Dovgalyuk }
287c0c071d0SPavel Dovgalyuk 
288c0c071d0SPavel Dovgalyuk bool replay_events_enabled(void)
289c0c071d0SPavel Dovgalyuk {
290c0c071d0SPavel Dovgalyuk     return events_enabled;
291c0c071d0SPavel Dovgalyuk }
292