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 12*d38ea87aSPeter 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; 51c0c071d0SPavel Dovgalyuk default: 52c0c071d0SPavel Dovgalyuk error_report("Replay: invalid async event ID (%d) in the queue", 53c0c071d0SPavel Dovgalyuk event->event_kind); 54c0c071d0SPavel Dovgalyuk exit(1); 55c0c071d0SPavel Dovgalyuk break; 56c0c071d0SPavel Dovgalyuk } 57c0c071d0SPavel Dovgalyuk } 58c0c071d0SPavel Dovgalyuk 59c0c071d0SPavel Dovgalyuk void replay_enable_events(void) 60c0c071d0SPavel Dovgalyuk { 61c0c071d0SPavel Dovgalyuk events_enabled = true; 62c0c071d0SPavel Dovgalyuk } 63c0c071d0SPavel Dovgalyuk 64c0c071d0SPavel Dovgalyuk bool replay_has_events(void) 65c0c071d0SPavel Dovgalyuk { 66c0c071d0SPavel Dovgalyuk return !QTAILQ_EMPTY(&events_list); 67c0c071d0SPavel Dovgalyuk } 68c0c071d0SPavel Dovgalyuk 69c0c071d0SPavel Dovgalyuk void replay_flush_events(void) 70c0c071d0SPavel Dovgalyuk { 71c0c071d0SPavel Dovgalyuk replay_mutex_lock(); 72c0c071d0SPavel Dovgalyuk while (!QTAILQ_EMPTY(&events_list)) { 73c0c071d0SPavel Dovgalyuk Event *event = QTAILQ_FIRST(&events_list); 74c0c071d0SPavel Dovgalyuk replay_mutex_unlock(); 75c0c071d0SPavel Dovgalyuk replay_run_event(event); 76c0c071d0SPavel Dovgalyuk replay_mutex_lock(); 77c0c071d0SPavel Dovgalyuk QTAILQ_REMOVE(&events_list, event, events); 78c0c071d0SPavel Dovgalyuk g_free(event); 79c0c071d0SPavel Dovgalyuk } 80c0c071d0SPavel Dovgalyuk replay_mutex_unlock(); 81c0c071d0SPavel Dovgalyuk } 82c0c071d0SPavel Dovgalyuk 83c0c071d0SPavel Dovgalyuk void replay_disable_events(void) 84c0c071d0SPavel Dovgalyuk { 85c0c071d0SPavel Dovgalyuk if (replay_mode != REPLAY_MODE_NONE) { 86c0c071d0SPavel Dovgalyuk events_enabled = false; 87c0c071d0SPavel Dovgalyuk /* Flush events queue before waiting of completion */ 88c0c071d0SPavel Dovgalyuk replay_flush_events(); 89c0c071d0SPavel Dovgalyuk } 90c0c071d0SPavel Dovgalyuk } 91c0c071d0SPavel Dovgalyuk 92c0c071d0SPavel Dovgalyuk void replay_clear_events(void) 93c0c071d0SPavel Dovgalyuk { 94c0c071d0SPavel Dovgalyuk replay_mutex_lock(); 95c0c071d0SPavel Dovgalyuk while (!QTAILQ_EMPTY(&events_list)) { 96c0c071d0SPavel Dovgalyuk Event *event = QTAILQ_FIRST(&events_list); 97c0c071d0SPavel Dovgalyuk QTAILQ_REMOVE(&events_list, event, events); 98c0c071d0SPavel Dovgalyuk 99c0c071d0SPavel Dovgalyuk g_free(event); 100c0c071d0SPavel Dovgalyuk } 101c0c071d0SPavel Dovgalyuk replay_mutex_unlock(); 102c0c071d0SPavel Dovgalyuk } 103c0c071d0SPavel Dovgalyuk 104c0c071d0SPavel Dovgalyuk /*! Adds specified async event to the queue */ 105c0c071d0SPavel Dovgalyuk static void replay_add_event(ReplayAsyncEventKind event_kind, 106c0c071d0SPavel Dovgalyuk void *opaque, 107c0c071d0SPavel Dovgalyuk void *opaque2, uint64_t id) 108c0c071d0SPavel Dovgalyuk { 109c0c071d0SPavel Dovgalyuk assert(event_kind < REPLAY_ASYNC_COUNT); 110c0c071d0SPavel Dovgalyuk 111c0c071d0SPavel Dovgalyuk if (!replay_file || replay_mode == REPLAY_MODE_NONE 112c0c071d0SPavel Dovgalyuk || !events_enabled) { 113c0c071d0SPavel Dovgalyuk Event e; 114c0c071d0SPavel Dovgalyuk e.event_kind = event_kind; 115c0c071d0SPavel Dovgalyuk e.opaque = opaque; 116c0c071d0SPavel Dovgalyuk e.opaque2 = opaque2; 117c0c071d0SPavel Dovgalyuk e.id = id; 118c0c071d0SPavel Dovgalyuk replay_run_event(&e); 119c0c071d0SPavel Dovgalyuk return; 120c0c071d0SPavel Dovgalyuk } 121c0c071d0SPavel Dovgalyuk 122c0c071d0SPavel Dovgalyuk Event *event = g_malloc0(sizeof(Event)); 123c0c071d0SPavel Dovgalyuk event->event_kind = event_kind; 124c0c071d0SPavel Dovgalyuk event->opaque = opaque; 125c0c071d0SPavel Dovgalyuk event->opaque2 = opaque2; 126c0c071d0SPavel Dovgalyuk event->id = id; 127c0c071d0SPavel Dovgalyuk 128c0c071d0SPavel Dovgalyuk replay_mutex_lock(); 129c0c071d0SPavel Dovgalyuk QTAILQ_INSERT_TAIL(&events_list, event, events); 130c0c071d0SPavel Dovgalyuk replay_mutex_unlock(); 131c0c071d0SPavel Dovgalyuk } 1328a354bd9SPavel Dovgalyuk 1338a354bd9SPavel Dovgalyuk void replay_bh_schedule_event(QEMUBH *bh) 1348a354bd9SPavel Dovgalyuk { 1358a354bd9SPavel Dovgalyuk if (replay_mode != REPLAY_MODE_NONE) { 1368a354bd9SPavel Dovgalyuk uint64_t id = replay_get_current_step(); 1378a354bd9SPavel Dovgalyuk replay_add_event(REPLAY_ASYNC_EVENT_BH, bh, NULL, id); 1388a354bd9SPavel Dovgalyuk } else { 1398a354bd9SPavel Dovgalyuk qemu_bh_schedule(bh); 1408a354bd9SPavel Dovgalyuk } 1418a354bd9SPavel Dovgalyuk } 142c0c071d0SPavel Dovgalyuk 143ee312992SPavel Dovgalyuk void replay_add_input_event(struct InputEvent *event) 144ee312992SPavel Dovgalyuk { 145ee312992SPavel Dovgalyuk replay_add_event(REPLAY_ASYNC_EVENT_INPUT, event, NULL, 0); 146ee312992SPavel Dovgalyuk } 147ee312992SPavel Dovgalyuk 148ee312992SPavel Dovgalyuk void replay_add_input_sync_event(void) 149ee312992SPavel Dovgalyuk { 150ee312992SPavel Dovgalyuk replay_add_event(REPLAY_ASYNC_EVENT_INPUT_SYNC, NULL, NULL, 0); 151ee312992SPavel Dovgalyuk } 152ee312992SPavel Dovgalyuk 153c0c071d0SPavel Dovgalyuk static void replay_save_event(Event *event, int checkpoint) 154c0c071d0SPavel Dovgalyuk { 155c0c071d0SPavel Dovgalyuk if (replay_mode != REPLAY_MODE_PLAY) { 156c0c071d0SPavel Dovgalyuk /* put the event into the file */ 157c0c071d0SPavel Dovgalyuk replay_put_event(EVENT_ASYNC); 158c0c071d0SPavel Dovgalyuk replay_put_byte(checkpoint); 159c0c071d0SPavel Dovgalyuk replay_put_byte(event->event_kind); 160c0c071d0SPavel Dovgalyuk 161c0c071d0SPavel Dovgalyuk /* save event-specific data */ 162c0c071d0SPavel Dovgalyuk switch (event->event_kind) { 1638a354bd9SPavel Dovgalyuk case REPLAY_ASYNC_EVENT_BH: 1648a354bd9SPavel Dovgalyuk replay_put_qword(event->id); 1658a354bd9SPavel Dovgalyuk break; 166ee312992SPavel Dovgalyuk case REPLAY_ASYNC_EVENT_INPUT: 167ee312992SPavel Dovgalyuk replay_save_input_event(event->opaque); 168ee312992SPavel Dovgalyuk break; 169ee312992SPavel Dovgalyuk case REPLAY_ASYNC_EVENT_INPUT_SYNC: 170ee312992SPavel Dovgalyuk break; 171c0c071d0SPavel Dovgalyuk default: 172c0c071d0SPavel Dovgalyuk error_report("Unknown ID %d of replay event", read_event_kind); 173c0c071d0SPavel Dovgalyuk exit(1); 174c0c071d0SPavel Dovgalyuk } 175c0c071d0SPavel Dovgalyuk } 176c0c071d0SPavel Dovgalyuk } 177c0c071d0SPavel Dovgalyuk 178c0c071d0SPavel Dovgalyuk /* Called with replay mutex locked */ 179c0c071d0SPavel Dovgalyuk void replay_save_events(int checkpoint) 180c0c071d0SPavel Dovgalyuk { 181c0c071d0SPavel Dovgalyuk while (!QTAILQ_EMPTY(&events_list)) { 182c0c071d0SPavel Dovgalyuk Event *event = QTAILQ_FIRST(&events_list); 183c0c071d0SPavel Dovgalyuk replay_save_event(event, checkpoint); 184c0c071d0SPavel Dovgalyuk 185c0c071d0SPavel Dovgalyuk replay_mutex_unlock(); 186c0c071d0SPavel Dovgalyuk replay_run_event(event); 187c0c071d0SPavel Dovgalyuk replay_mutex_lock(); 188c0c071d0SPavel Dovgalyuk QTAILQ_REMOVE(&events_list, event, events); 189c0c071d0SPavel Dovgalyuk g_free(event); 190c0c071d0SPavel Dovgalyuk } 191c0c071d0SPavel Dovgalyuk } 192c0c071d0SPavel Dovgalyuk 193c0c071d0SPavel Dovgalyuk static Event *replay_read_event(int checkpoint) 194c0c071d0SPavel Dovgalyuk { 195c0c071d0SPavel Dovgalyuk Event *event; 196c0c071d0SPavel Dovgalyuk if (read_event_kind == -1) { 197c0c071d0SPavel Dovgalyuk read_checkpoint = replay_get_byte(); 198c0c071d0SPavel Dovgalyuk read_event_kind = replay_get_byte(); 199c0c071d0SPavel Dovgalyuk read_id = -1; 200c0c071d0SPavel Dovgalyuk replay_check_error(); 201c0c071d0SPavel Dovgalyuk } 202c0c071d0SPavel Dovgalyuk 203c0c071d0SPavel Dovgalyuk if (checkpoint != read_checkpoint) { 204c0c071d0SPavel Dovgalyuk return NULL; 205c0c071d0SPavel Dovgalyuk } 206c0c071d0SPavel Dovgalyuk 207c0c071d0SPavel Dovgalyuk /* Events that has not to be in the queue */ 208c0c071d0SPavel Dovgalyuk switch (read_event_kind) { 2098a354bd9SPavel Dovgalyuk case REPLAY_ASYNC_EVENT_BH: 2108a354bd9SPavel Dovgalyuk if (read_id == -1) { 2118a354bd9SPavel Dovgalyuk read_id = replay_get_qword(); 2128a354bd9SPavel Dovgalyuk } 2138a354bd9SPavel Dovgalyuk break; 214ee312992SPavel Dovgalyuk case REPLAY_ASYNC_EVENT_INPUT: 215ee312992SPavel Dovgalyuk event = g_malloc0(sizeof(Event)); 216ee312992SPavel Dovgalyuk event->event_kind = read_event_kind; 217ee312992SPavel Dovgalyuk event->opaque = replay_read_input_event(); 218ee312992SPavel Dovgalyuk return event; 219ee312992SPavel Dovgalyuk case REPLAY_ASYNC_EVENT_INPUT_SYNC: 220ee312992SPavel Dovgalyuk event = g_malloc0(sizeof(Event)); 221ee312992SPavel Dovgalyuk event->event_kind = read_event_kind; 222ee312992SPavel Dovgalyuk event->opaque = 0; 223ee312992SPavel Dovgalyuk return event; 224c0c071d0SPavel Dovgalyuk default: 225c0c071d0SPavel Dovgalyuk error_report("Unknown ID %d of replay event", read_event_kind); 226c0c071d0SPavel Dovgalyuk exit(1); 227c0c071d0SPavel Dovgalyuk break; 228c0c071d0SPavel Dovgalyuk } 229c0c071d0SPavel Dovgalyuk 230c0c071d0SPavel Dovgalyuk QTAILQ_FOREACH(event, &events_list, events) { 231c0c071d0SPavel Dovgalyuk if (event->event_kind == read_event_kind 232c0c071d0SPavel Dovgalyuk && (read_id == -1 || read_id == event->id)) { 233c0c071d0SPavel Dovgalyuk break; 234c0c071d0SPavel Dovgalyuk } 235c0c071d0SPavel Dovgalyuk } 236c0c071d0SPavel Dovgalyuk 237c0c071d0SPavel Dovgalyuk if (event) { 238c0c071d0SPavel Dovgalyuk QTAILQ_REMOVE(&events_list, event, events); 239c0c071d0SPavel Dovgalyuk } else { 240c0c071d0SPavel Dovgalyuk return NULL; 241c0c071d0SPavel Dovgalyuk } 242c0c071d0SPavel Dovgalyuk 243c0c071d0SPavel Dovgalyuk /* Read event-specific data */ 244c0c071d0SPavel Dovgalyuk 245c0c071d0SPavel Dovgalyuk return event; 246c0c071d0SPavel Dovgalyuk } 247c0c071d0SPavel Dovgalyuk 248c0c071d0SPavel Dovgalyuk /* Called with replay mutex locked */ 249c0c071d0SPavel Dovgalyuk void replay_read_events(int checkpoint) 250c0c071d0SPavel Dovgalyuk { 251c0c071d0SPavel Dovgalyuk while (replay_data_kind == EVENT_ASYNC) { 252c0c071d0SPavel Dovgalyuk Event *event = replay_read_event(checkpoint); 253c0c071d0SPavel Dovgalyuk if (!event) { 254c0c071d0SPavel Dovgalyuk break; 255c0c071d0SPavel Dovgalyuk } 256c0c071d0SPavel Dovgalyuk replay_mutex_unlock(); 257c0c071d0SPavel Dovgalyuk replay_run_event(event); 258c0c071d0SPavel Dovgalyuk replay_mutex_lock(); 259c0c071d0SPavel Dovgalyuk 260c0c071d0SPavel Dovgalyuk g_free(event); 261c0c071d0SPavel Dovgalyuk replay_finish_event(); 262c0c071d0SPavel Dovgalyuk read_event_kind = -1; 263c0c071d0SPavel Dovgalyuk } 264c0c071d0SPavel Dovgalyuk } 265c0c071d0SPavel Dovgalyuk 266c0c071d0SPavel Dovgalyuk void replay_init_events(void) 267c0c071d0SPavel Dovgalyuk { 268c0c071d0SPavel Dovgalyuk read_event_kind = -1; 269c0c071d0SPavel Dovgalyuk } 270c0c071d0SPavel Dovgalyuk 271c0c071d0SPavel Dovgalyuk void replay_finish_events(void) 272c0c071d0SPavel Dovgalyuk { 273c0c071d0SPavel Dovgalyuk events_enabled = false; 274c0c071d0SPavel Dovgalyuk replay_clear_events(); 275c0c071d0SPavel Dovgalyuk } 276c0c071d0SPavel Dovgalyuk 277c0c071d0SPavel Dovgalyuk bool replay_events_enabled(void) 278c0c071d0SPavel Dovgalyuk { 279c0c071d0SPavel Dovgalyuk return events_enabled; 280c0c071d0SPavel Dovgalyuk } 281