2015-09-17 19:24:22 +03:00
|
|
|
/*
|
|
|
|
* replay-events.c
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010-2015 Institute for System Programming
|
|
|
|
* of the Russian Academy of Sciences.
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-29 20:50:05 +03:00
|
|
|
#include "qemu/osdep.h"
|
2015-09-17 19:24:22 +03:00
|
|
|
#include "qemu/error-report.h"
|
|
|
|
#include "sysemu/replay.h"
|
|
|
|
#include "replay-internal.h"
|
2015-09-17 19:24:56 +03:00
|
|
|
#include "block/aio.h"
|
2015-09-17 19:25:24 +03:00
|
|
|
#include "ui/input.h"
|
2021-04-01 11:19:51 +03:00
|
|
|
#include "hw/core/cpu.h"
|
2015-09-17 19:24:22 +03:00
|
|
|
|
|
|
|
typedef struct Event {
|
|
|
|
ReplayAsyncEventKind event_kind;
|
|
|
|
void *opaque;
|
|
|
|
void *opaque2;
|
|
|
|
uint64_t id;
|
|
|
|
|
|
|
|
QTAILQ_ENTRY(Event) events;
|
|
|
|
} Event;
|
|
|
|
|
|
|
|
static QTAILQ_HEAD(, Event) events_list = QTAILQ_HEAD_INITIALIZER(events_list);
|
|
|
|
static bool events_enabled;
|
|
|
|
|
|
|
|
/* Functions */
|
|
|
|
|
|
|
|
static void replay_run_event(Event *event)
|
|
|
|
{
|
|
|
|
switch (event->event_kind) {
|
2015-09-17 19:24:56 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_BH:
|
|
|
|
aio_bh_call(event->opaque);
|
|
|
|
break;
|
2019-09-17 14:58:19 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_BH_ONESHOT:
|
|
|
|
((QEMUBHFunc *)event->opaque)(event->opaque2);
|
|
|
|
break;
|
2015-09-17 19:25:24 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_INPUT:
|
|
|
|
qemu_input_event_send_impl(NULL, (InputEvent *)event->opaque);
|
|
|
|
qapi_free_InputEvent((InputEvent *)event->opaque);
|
|
|
|
break;
|
|
|
|
case REPLAY_ASYNC_EVENT_INPUT_SYNC:
|
|
|
|
qemu_input_event_sync_impl();
|
|
|
|
break;
|
2016-03-14 10:44:36 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_CHAR_READ:
|
|
|
|
replay_event_char_read_run(event->opaque);
|
|
|
|
break;
|
2016-03-14 10:45:10 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_BLOCK:
|
|
|
|
aio_bh_call(event->opaque);
|
|
|
|
break;
|
2016-09-26 11:08:21 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_NET:
|
|
|
|
replay_event_net_run(event->opaque);
|
|
|
|
break;
|
2015-09-17 19:24:22 +03:00
|
|
|
default:
|
|
|
|
error_report("Replay: invalid async event ID (%d) in the queue",
|
|
|
|
event->event_kind);
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void replay_enable_events(void)
|
|
|
|
{
|
2018-02-27 12:52:09 +03:00
|
|
|
if (replay_mode != REPLAY_MODE_NONE) {
|
|
|
|
events_enabled = true;
|
|
|
|
}
|
2015-09-17 19:24:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool replay_has_events(void)
|
|
|
|
{
|
|
|
|
return !QTAILQ_EMPTY(&events_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void replay_flush_events(void)
|
|
|
|
{
|
2020-10-03 20:13:37 +03:00
|
|
|
if (replay_mode == REPLAY_MODE_NONE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-27 12:52:48 +03:00
|
|
|
g_assert(replay_mutex_locked());
|
|
|
|
|
2015-09-17 19:24:22 +03:00
|
|
|
while (!QTAILQ_EMPTY(&events_list)) {
|
|
|
|
Event *event = QTAILQ_FIRST(&events_list);
|
|
|
|
replay_run_event(event);
|
|
|
|
QTAILQ_REMOVE(&events_list, event, events);
|
|
|
|
g_free(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void replay_disable_events(void)
|
|
|
|
{
|
|
|
|
if (replay_mode != REPLAY_MODE_NONE) {
|
|
|
|
events_enabled = false;
|
|
|
|
/* Flush events queue before waiting of completion */
|
|
|
|
replay_flush_events();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! Adds specified async event to the queue */
|
2016-03-14 10:44:36 +03:00
|
|
|
void replay_add_event(ReplayAsyncEventKind event_kind,
|
|
|
|
void *opaque,
|
|
|
|
void *opaque2, uint64_t id)
|
2015-09-17 19:24:22 +03:00
|
|
|
{
|
|
|
|
assert(event_kind < REPLAY_ASYNC_COUNT);
|
|
|
|
|
|
|
|
if (!replay_file || replay_mode == REPLAY_MODE_NONE
|
|
|
|
|| !events_enabled) {
|
|
|
|
Event e;
|
|
|
|
e.event_kind = event_kind;
|
|
|
|
e.opaque = opaque;
|
|
|
|
e.opaque2 = opaque2;
|
|
|
|
e.id = id;
|
|
|
|
replay_run_event(&e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-15 17:41:56 +03:00
|
|
|
Event *event = g_new0(Event, 1);
|
2015-09-17 19:24:22 +03:00
|
|
|
event->event_kind = event_kind;
|
|
|
|
event->opaque = opaque;
|
|
|
|
event->opaque2 = opaque2;
|
|
|
|
event->id = id;
|
|
|
|
|
2018-02-27 12:52:48 +03:00
|
|
|
g_assert(replay_mutex_locked());
|
2015-09-17 19:24:22 +03:00
|
|
|
QTAILQ_INSERT_TAIL(&events_list, event, events);
|
2021-04-01 11:19:51 +03:00
|
|
|
qemu_cpu_kick(first_cpu);
|
2015-09-17 19:24:22 +03:00
|
|
|
}
|
2015-09-17 19:24:56 +03:00
|
|
|
|
|
|
|
void replay_bh_schedule_event(QEMUBH *bh)
|
|
|
|
{
|
2018-02-27 12:52:09 +03:00
|
|
|
if (events_enabled) {
|
2019-07-25 11:44:43 +03:00
|
|
|
uint64_t id = replay_get_current_icount();
|
2015-09-17 19:24:56 +03:00
|
|
|
replay_add_event(REPLAY_ASYNC_EVENT_BH, bh, NULL, id);
|
|
|
|
} else {
|
|
|
|
qemu_bh_schedule(bh);
|
|
|
|
}
|
|
|
|
}
|
2015-09-17 19:24:22 +03:00
|
|
|
|
2019-09-17 14:58:19 +03:00
|
|
|
void replay_bh_schedule_oneshot_event(AioContext *ctx,
|
|
|
|
QEMUBHFunc *cb, void *opaque)
|
|
|
|
{
|
|
|
|
if (events_enabled) {
|
|
|
|
uint64_t id = replay_get_current_icount();
|
|
|
|
replay_add_event(REPLAY_ASYNC_EVENT_BH_ONESHOT, cb, opaque, id);
|
|
|
|
} else {
|
|
|
|
aio_bh_schedule_oneshot(ctx, cb, opaque);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 19:25:24 +03:00
|
|
|
void replay_add_input_event(struct InputEvent *event)
|
|
|
|
{
|
|
|
|
replay_add_event(REPLAY_ASYNC_EVENT_INPUT, event, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void replay_add_input_sync_event(void)
|
|
|
|
{
|
|
|
|
replay_add_event(REPLAY_ASYNC_EVENT_INPUT_SYNC, NULL, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2016-03-14 10:45:10 +03:00
|
|
|
void replay_block_event(QEMUBH *bh, uint64_t id)
|
|
|
|
{
|
2018-02-27 12:52:09 +03:00
|
|
|
if (events_enabled) {
|
2016-03-14 10:45:10 +03:00
|
|
|
replay_add_event(REPLAY_ASYNC_EVENT_BLOCK, bh, NULL, id);
|
|
|
|
} else {
|
|
|
|
qemu_bh_schedule(bh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 19:24:22 +03:00
|
|
|
static void replay_save_event(Event *event, int checkpoint)
|
|
|
|
{
|
|
|
|
if (replay_mode != REPLAY_MODE_PLAY) {
|
|
|
|
/* put the event into the file */
|
|
|
|
replay_put_event(EVENT_ASYNC);
|
|
|
|
replay_put_byte(checkpoint);
|
|
|
|
replay_put_byte(event->event_kind);
|
|
|
|
|
|
|
|
/* save event-specific data */
|
|
|
|
switch (event->event_kind) {
|
2015-09-17 19:24:56 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_BH:
|
2019-09-17 14:58:19 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_BH_ONESHOT:
|
2015-09-17 19:24:56 +03:00
|
|
|
replay_put_qword(event->id);
|
|
|
|
break;
|
2015-09-17 19:25:24 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_INPUT:
|
|
|
|
replay_save_input_event(event->opaque);
|
|
|
|
break;
|
|
|
|
case REPLAY_ASYNC_EVENT_INPUT_SYNC:
|
|
|
|
break;
|
2016-03-14 10:44:36 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_CHAR_READ:
|
|
|
|
replay_event_char_read_save(event->opaque);
|
|
|
|
break;
|
2016-03-14 10:45:10 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_BLOCK:
|
|
|
|
replay_put_qword(event->id);
|
|
|
|
break;
|
2016-09-26 11:08:21 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_NET:
|
|
|
|
replay_event_net_save(event->opaque);
|
|
|
|
break;
|
2015-09-17 19:24:22 +03:00
|
|
|
default:
|
2016-03-14 10:45:04 +03:00
|
|
|
error_report("Unknown ID %" PRId64 " of replay event", event->id);
|
2015-09-17 19:24:22 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called with replay mutex locked */
|
|
|
|
void replay_save_events(int checkpoint)
|
|
|
|
{
|
2018-02-27 12:52:48 +03:00
|
|
|
g_assert(replay_mutex_locked());
|
2018-02-27 12:53:16 +03:00
|
|
|
g_assert(checkpoint != CHECKPOINT_CLOCK_WARP_START);
|
2018-10-18 09:33:45 +03:00
|
|
|
g_assert(checkpoint != CHECKPOINT_CLOCK_VIRTUAL);
|
2015-09-17 19:24:22 +03:00
|
|
|
while (!QTAILQ_EMPTY(&events_list)) {
|
|
|
|
Event *event = QTAILQ_FIRST(&events_list);
|
|
|
|
replay_save_event(event, checkpoint);
|
|
|
|
replay_run_event(event);
|
|
|
|
QTAILQ_REMOVE(&events_list, event, events);
|
|
|
|
g_free(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Event *replay_read_event(int checkpoint)
|
|
|
|
{
|
|
|
|
Event *event;
|
2018-02-27 12:53:22 +03:00
|
|
|
if (replay_state.read_event_kind == -1) {
|
|
|
|
replay_state.read_event_checkpoint = replay_get_byte();
|
|
|
|
replay_state.read_event_kind = replay_get_byte();
|
|
|
|
replay_state.read_event_id = -1;
|
2015-09-17 19:24:22 +03:00
|
|
|
replay_check_error();
|
|
|
|
}
|
|
|
|
|
2018-02-27 12:53:22 +03:00
|
|
|
if (checkpoint != replay_state.read_event_checkpoint) {
|
2015-09-17 19:24:22 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Events that has not to be in the queue */
|
2018-02-27 12:53:22 +03:00
|
|
|
switch (replay_state.read_event_kind) {
|
2015-09-17 19:24:56 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_BH:
|
2019-09-17 14:58:19 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_BH_ONESHOT:
|
2018-02-27 12:53:22 +03:00
|
|
|
if (replay_state.read_event_id == -1) {
|
|
|
|
replay_state.read_event_id = replay_get_qword();
|
2015-09-17 19:24:56 +03:00
|
|
|
}
|
|
|
|
break;
|
2015-09-17 19:25:24 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_INPUT:
|
2022-03-15 17:41:56 +03:00
|
|
|
event = g_new0(Event, 1);
|
2018-02-27 12:53:22 +03:00
|
|
|
event->event_kind = replay_state.read_event_kind;
|
2015-09-17 19:25:24 +03:00
|
|
|
event->opaque = replay_read_input_event();
|
|
|
|
return event;
|
|
|
|
case REPLAY_ASYNC_EVENT_INPUT_SYNC:
|
2022-03-15 17:41:56 +03:00
|
|
|
event = g_new0(Event, 1);
|
2018-02-27 12:53:22 +03:00
|
|
|
event->event_kind = replay_state.read_event_kind;
|
2015-09-17 19:25:24 +03:00
|
|
|
event->opaque = 0;
|
|
|
|
return event;
|
2016-03-14 10:44:36 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_CHAR_READ:
|
2022-03-15 17:41:56 +03:00
|
|
|
event = g_new0(Event, 1);
|
2018-02-27 12:53:22 +03:00
|
|
|
event->event_kind = replay_state.read_event_kind;
|
2016-03-14 10:44:36 +03:00
|
|
|
event->opaque = replay_event_char_read_load();
|
|
|
|
return event;
|
2016-03-14 10:45:10 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_BLOCK:
|
2018-02-27 12:53:22 +03:00
|
|
|
if (replay_state.read_event_id == -1) {
|
|
|
|
replay_state.read_event_id = replay_get_qword();
|
2016-03-14 10:45:10 +03:00
|
|
|
}
|
|
|
|
break;
|
2016-09-26 11:08:21 +03:00
|
|
|
case REPLAY_ASYNC_EVENT_NET:
|
2022-03-15 17:41:56 +03:00
|
|
|
event = g_new0(Event, 1);
|
2018-02-27 12:53:22 +03:00
|
|
|
event->event_kind = replay_state.read_event_kind;
|
2016-09-26 11:08:21 +03:00
|
|
|
event->opaque = replay_event_net_load();
|
|
|
|
return event;
|
2015-09-17 19:24:22 +03:00
|
|
|
default:
|
2018-02-27 12:53:22 +03:00
|
|
|
error_report("Unknown ID %d of replay event",
|
|
|
|
replay_state.read_event_kind);
|
2015-09-17 19:24:22 +03:00
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTAILQ_FOREACH(event, &events_list, events) {
|
2018-02-27 12:53:22 +03:00
|
|
|
if (event->event_kind == replay_state.read_event_kind
|
|
|
|
&& (replay_state.read_event_id == -1
|
|
|
|
|| replay_state.read_event_id == event->id)) {
|
2015-09-17 19:24:22 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
QTAILQ_REMOVE(&events_list, event, events);
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read event-specific data */
|
|
|
|
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called with replay mutex locked */
|
|
|
|
void replay_read_events(int checkpoint)
|
|
|
|
{
|
2018-02-27 12:52:48 +03:00
|
|
|
g_assert(replay_mutex_locked());
|
2016-09-26 11:08:04 +03:00
|
|
|
while (replay_state.data_kind == EVENT_ASYNC) {
|
2015-09-17 19:24:22 +03:00
|
|
|
Event *event = replay_read_event(checkpoint);
|
|
|
|
if (!event) {
|
|
|
|
break;
|
|
|
|
}
|
2018-02-27 12:52:03 +03:00
|
|
|
replay_finish_event();
|
2018-02-27 12:53:22 +03:00
|
|
|
replay_state.read_event_kind = -1;
|
2015-09-17 19:24:22 +03:00
|
|
|
replay_run_event(event);
|
|
|
|
|
|
|
|
g_free(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void replay_init_events(void)
|
|
|
|
{
|
2018-02-27 12:53:22 +03:00
|
|
|
replay_state.read_event_kind = -1;
|
2015-09-17 19:24:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void replay_finish_events(void)
|
|
|
|
{
|
|
|
|
events_enabled = false;
|
2018-09-12 11:18:59 +03:00
|
|
|
replay_flush_events();
|
2015-09-17 19:24:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool replay_events_enabled(void)
|
|
|
|
{
|
|
|
|
return events_enabled;
|
|
|
|
}
|
2016-09-26 11:08:16 +03:00
|
|
|
|
|
|
|
uint64_t blkreplay_next_id(void)
|
|
|
|
{
|
|
|
|
if (replay_events_enabled()) {
|
|
|
|
return replay_state.block_request_id++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|