xref: /openbmc/qemu/include/sysemu/replay.h (revision 8d3031fa)
1 /*
2  * QEMU replay (system interface)
3  *
4  * Copyright (c) 2010-2015 Institute for System Programming
5  *                         of the Russian Academy of Sciences.
6  *
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.
9  *
10  */
11 #ifndef SYSEMU_REPLAY_H
12 #define SYSEMU_REPLAY_H
13 
14 #ifdef CONFIG_USER_ONLY
15 #error Cannot include this header from user emulation
16 #endif
17 
18 #include "exec/replay-core.h"
19 #include "qapi/qapi-types-misc.h"
20 #include "qapi/qapi-types-run-state.h"
21 #include "qapi/qapi-types-ui.h"
22 #include "block/aio.h"
23 
24 /* replay clock kinds */
25 enum ReplayClockKind {
26     /* host_clock */
27     REPLAY_CLOCK_HOST,
28     /* virtual_rt_clock */
29     REPLAY_CLOCK_VIRTUAL_RT,
30     REPLAY_CLOCK_COUNT
31 };
32 typedef enum ReplayClockKind ReplayClockKind;
33 
34 /* IDs of the checkpoints */
35 enum ReplayCheckpoint {
36     CHECKPOINT_CLOCK_WARP_START,
37     CHECKPOINT_CLOCK_WARP_ACCOUNT,
38     CHECKPOINT_RESET_REQUESTED,
39     CHECKPOINT_SUSPEND_REQUESTED,
40     CHECKPOINT_CLOCK_VIRTUAL,
41     CHECKPOINT_CLOCK_HOST,
42     CHECKPOINT_CLOCK_VIRTUAL_RT,
43     CHECKPOINT_INIT,
44     CHECKPOINT_RESET,
45     CHECKPOINT_COUNT
46 };
47 typedef enum ReplayCheckpoint ReplayCheckpoint;
48 
49 typedef struct ReplayNetState ReplayNetState;
50 
51 /* Name of the initial VM snapshot */
52 extern char *replay_snapshot;
53 
54 /* Replay locking
55  *
56  * The locks are needed to protect the shared structures and log file
57  * when doing record/replay. They also are the main sync-point between
58  * the main-loop thread and the vCPU thread. This was a role
59  * previously filled by the BQL which has been busy trying to reduce
60  * its impact across the code. This ensures blocks of events stay
61  * sequential and reproducible.
62  */
63 
64 void replay_mutex_lock(void);
65 void replay_mutex_unlock(void);
66 
67 /* Processing the instructions */
68 
69 /*! Returns number of executed instructions. */
70 uint64_t replay_get_current_icount(void);
71 /*! Returns number of instructions to execute in replay mode. */
72 int replay_get_instructions(void);
73 /*! Updates instructions counter in replay mode. */
74 void replay_account_executed_instructions(void);
75 
76 /* Processing clocks and other time sources */
77 
78 /*! Save the specified clock */
79 int64_t replay_save_clock(ReplayClockKind kind, int64_t clock,
80                           int64_t raw_icount);
81 /*! Read the specified clock from the log or return cached data */
82 int64_t replay_read_clock(ReplayClockKind kind, int64_t raw_icount);
83 /*! Saves or reads the clock depending on the current replay mode. */
84 #define REPLAY_CLOCK(clock, value)                                      \
85     !icount_enabled() ? (value) :                                       \
86     (replay_mode == REPLAY_MODE_PLAY                                    \
87         ? replay_read_clock((clock), icount_get_raw())                  \
88         : replay_mode == REPLAY_MODE_RECORD                             \
89             ? replay_save_clock((clock), (value), icount_get_raw())     \
90             : (value))
91 #define REPLAY_CLOCK_LOCKED(clock, value)                               \
92     !icount_enabled() ? (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 /*! Enables storing events in the queue */
121 void replay_enable_events(void);
122 /*! Returns true when saving events is enabled */
123 bool replay_events_enabled(void);
124 /* Flushes events queue */
125 void replay_flush_events(void);
126 /*! Adds bottom half event to the queue */
127 void replay_bh_schedule_event(QEMUBH *bh);
128 /* Adds oneshot bottom half event to the queue */
129 void replay_bh_schedule_oneshot_event(AioContext *ctx,
130     QEMUBHFunc *cb, void *opaque);
131 /*! Adds input event to the queue */
132 void replay_input_event(QemuConsole *src, InputEvent *evt);
133 /*! Adds input sync event to the queue */
134 void replay_input_sync_event(void);
135 /*! Adds block layer event to the queue */
136 void replay_block_event(QEMUBH *bh, uint64_t id);
137 /*! Returns ID for the next block event */
138 uint64_t blkreplay_next_id(void);
139 
140 /* Character device */
141 
142 /*! Registers char driver to save it's events */
143 void replay_register_char_driver(struct Chardev *chr);
144 /*! Saves write to char device event to the log */
145 void replay_chr_be_write(struct Chardev *s, const uint8_t *buf, int len);
146 /*! Writes char write return value to the replay log. */
147 void replay_char_write_event_save(int res, int offset);
148 /*! Reads char write return value from the replay log. */
149 void replay_char_write_event_load(int *res, int *offset);
150 /*! Reads information about read_all character event. */
151 int replay_char_read_all_load(uint8_t *buf);
152 /*! Writes character read_all error code into the replay log. */
153 void replay_char_read_all_save_error(int res);
154 /*! Writes character read_all execution result into the replay log. */
155 void replay_char_read_all_save_buf(uint8_t *buf, int offset);
156 
157 /* Network */
158 
159 /*! Registers replay network filter attached to some backend. */
160 ReplayNetState *replay_register_net(NetFilterState *nfs);
161 /*! Unregisters replay network filter. */
162 void replay_unregister_net(ReplayNetState *rns);
163 /*! Called to write network packet to the replay log. */
164 void replay_net_packet_event(ReplayNetState *rns, unsigned flags,
165                              const struct iovec *iov, int iovcnt);
166 
167 /* Audio */
168 
169 /*! Saves/restores number of played samples of audio out operation. */
170 void replay_audio_out(size_t *played);
171 /*! Saves/restores recorded samples of audio in operation. */
172 void replay_audio_in(size_t *recorded, void *samples, size_t *wpos, size_t size);
173 
174 /* VM state operations */
175 
176 /*! Called at the start of execution.
177     Loads or saves initial vmstate depending on execution mode. */
178 void replay_vmstate_init(void);
179 /*! Called to ensure that replay state is consistent and VM snapshot
180     can be created */
181 bool replay_can_snapshot(void);
182 
183 #endif
184