xref: /openbmc/qemu/include/sysemu/replay.h (revision efb91426)
1 #ifndef SYSEMU_REPLAY_H
2 #define SYSEMU_REPLAY_H
3 
4 /*
5  * QEMU replay (system interface)
6  *
7  * Copyright (c) 2010-2015 Institute for System Programming
8  *                         of the Russian Academy of Sciences.
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2 or later.
11  * See the COPYING file in the top-level directory.
12  *
13  */
14 
15 #include "exec/replay-core.h"
16 #include "qapi/qapi-types-misc.h"
17 #include "qapi/qapi-types-run-state.h"
18 #include "qapi/qapi-types-ui.h"
19 #include "block/aio.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 /**
74  * replay_can_wait: check if we should pause for wait-io
75  */
76 bool replay_can_wait(void);
77 
78 /* Processing clocks and other time sources */
79 
80 /*! Save the specified clock */
81 int64_t replay_save_clock(ReplayClockKind kind, int64_t clock,
82                           int64_t raw_icount);
83 /*! Read the specified clock from the log or return cached data */
84 int64_t replay_read_clock(ReplayClockKind kind, int64_t raw_icount);
85 /*! Saves or reads the clock depending on the current replay mode. */
86 #define REPLAY_CLOCK(clock, value)                                      \
87     (replay_mode == REPLAY_MODE_PLAY                                    \
88         ? replay_read_clock((clock), icount_get_raw())                  \
89         : replay_mode == REPLAY_MODE_RECORD                             \
90             ? replay_save_clock((clock), (value), icount_get_raw())     \
91             : (value))
92 #define REPLAY_CLOCK_LOCKED(clock, value)                               \
93     (replay_mode == REPLAY_MODE_PLAY                                    \
94         ? replay_read_clock((clock), icount_get_raw_locked())           \
95         : replay_mode == REPLAY_MODE_RECORD                             \
96             ? replay_save_clock((clock), (value), icount_get_raw_locked()) \
97             : (value))
98 
99 /* Events */
100 
101 /*! Called when qemu shutdown is requested. */
102 void replay_shutdown_request(ShutdownCause cause);
103 /*! Should be called at check points in the execution.
104     These check points are skipped, if they were not met.
105     Saves checkpoint in the SAVE mode and validates in the PLAY mode.
106     Returns 0 in PLAY mode if checkpoint was not found.
107     Returns 1 in all other cases. */
108 bool replay_checkpoint(ReplayCheckpoint checkpoint);
109 /*! Used to determine that checkpoint or async event is pending.
110     Does not proceed to the next event in the log. */
111 bool replay_has_event(void);
112 /*
113  * Processes the async events added to the queue (while recording)
114  * or reads the events from the file (while replaying).
115  */
116 void replay_async_events(void);
117 
118 /* Asynchronous events queue */
119 
120 /*! Disables storing events in the queue */
121 void replay_disable_events(void);
122 /*! Enables storing events in the queue */
123 void replay_enable_events(void);
124 /*! Returns true when saving events is enabled */
125 bool replay_events_enabled(void);
126 /* Flushes events queue */
127 void replay_flush_events(void);
128 /*! Adds bottom half event to the queue */
129 void replay_bh_schedule_event(QEMUBH *bh);
130 /* Adds oneshot bottom half event to the queue */
131 void replay_bh_schedule_oneshot_event(AioContext *ctx,
132     QEMUBHFunc *cb, void *opaque);
133 /*! Adds input event to the queue */
134 void replay_input_event(QemuConsole *src, InputEvent *evt);
135 /*! Adds input sync event to the queue */
136 void replay_input_sync_event(void);
137 /*! Adds block layer event to the queue */
138 void replay_block_event(QEMUBH *bh, uint64_t id);
139 /*! Returns ID for the next block event */
140 uint64_t blkreplay_next_id(void);
141 
142 /* Character device */
143 
144 /*! Registers char driver to save it's events */
145 void replay_register_char_driver(struct Chardev *chr);
146 /*! Saves write to char device event to the log */
147 void replay_chr_be_write(struct Chardev *s, const uint8_t *buf, int len);
148 /*! Writes char write return value to the replay log. */
149 void replay_char_write_event_save(int res, int offset);
150 /*! Reads char write return value from the replay log. */
151 void replay_char_write_event_load(int *res, int *offset);
152 /*! Reads information about read_all character event. */
153 int replay_char_read_all_load(uint8_t *buf);
154 /*! Writes character read_all error code into the replay log. */
155 void replay_char_read_all_save_error(int res);
156 /*! Writes character read_all execution result into the replay log. */
157 void replay_char_read_all_save_buf(uint8_t *buf, int offset);
158 
159 /* Network */
160 
161 /*! Registers replay network filter attached to some backend. */
162 ReplayNetState *replay_register_net(NetFilterState *nfs);
163 /*! Unregisters replay network filter. */
164 void replay_unregister_net(ReplayNetState *rns);
165 /*! Called to write network packet to the replay log. */
166 void replay_net_packet_event(ReplayNetState *rns, unsigned flags,
167                              const struct iovec *iov, int iovcnt);
168 
169 /* Audio */
170 
171 /*! Saves/restores number of played samples of audio out operation. */
172 void replay_audio_out(size_t *played);
173 /*! Saves/restores recorded samples of audio in operation. */
174 void replay_audio_in(size_t *recorded, void *samples, size_t *wpos, size_t size);
175 
176 /* VM state operations */
177 
178 /*! Called at the start of execution.
179     Loads or saves initial vmstate depending on execution mode. */
180 void replay_vmstate_init(void);
181 /*! Called to ensure that replay state is consistent and VM snapshot
182     can be created */
183 bool replay_can_snapshot(void);
184 
185 #endif
186