xref: /openbmc/qemu/include/sysemu/replay.h (revision 6620801f)
1 #ifndef REPLAY_H
2 #define REPLAY_H
3 
4 /*
5  * replay.h
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 "qapi/qapi-types-misc.h"
16 #include "qapi/qapi-types-run-state.h"
17 #include "qapi/qapi-types-ui.h"
18 #include "block/aio.h"
19 
20 /* replay clock kinds */
21 enum ReplayClockKind {
22     /* host_clock */
23     REPLAY_CLOCK_HOST,
24     /* virtual_rt_clock */
25     REPLAY_CLOCK_VIRTUAL_RT,
26     REPLAY_CLOCK_COUNT
27 };
28 typedef enum ReplayClockKind ReplayClockKind;
29 
30 /* IDs of the checkpoints */
31 enum ReplayCheckpoint {
32     CHECKPOINT_CLOCK_WARP_START,
33     CHECKPOINT_CLOCK_WARP_ACCOUNT,
34     CHECKPOINT_RESET_REQUESTED,
35     CHECKPOINT_SUSPEND_REQUESTED,
36     CHECKPOINT_CLOCK_VIRTUAL,
37     CHECKPOINT_CLOCK_HOST,
38     CHECKPOINT_CLOCK_VIRTUAL_RT,
39     CHECKPOINT_INIT,
40     CHECKPOINT_RESET,
41     CHECKPOINT_COUNT
42 };
43 typedef enum ReplayCheckpoint ReplayCheckpoint;
44 
45 typedef struct ReplayNetState ReplayNetState;
46 
47 extern ReplayMode replay_mode;
48 
49 /* Name of the initial VM snapshot */
50 extern char *replay_snapshot;
51 
52 /* Replay locking
53  *
54  * The locks are needed to protect the shared structures and log file
55  * when doing record/replay. They also are the main sync-point between
56  * the main-loop thread and the vCPU thread. This was a role
57  * previously filled by the BQL which has been busy trying to reduce
58  * its impact across the code. This ensures blocks of events stay
59  * sequential and reproducible.
60  */
61 
62 void replay_mutex_lock(void);
63 void replay_mutex_unlock(void);
64 
65 /* Replay process control functions */
66 
67 /*! Enables recording or saving event log with specified parameters */
68 void replay_configure(struct QemuOpts *opts);
69 /*! Initializes timers used for snapshotting and enables events recording */
70 void replay_start(void);
71 /*! Closes replay log file and frees other resources. */
72 void replay_finish(void);
73 /*! Adds replay blocker with the specified error description */
74 void replay_add_blocker(Error *reason);
75 
76 /* Processing the instructions */
77 
78 /*! Returns number of executed instructions. */
79 uint64_t replay_get_current_icount(void);
80 /*! Returns number of instructions to execute in replay mode. */
81 int replay_get_instructions(void);
82 /*! Updates instructions counter in replay mode. */
83 void replay_account_executed_instructions(void);
84 
85 /* Interrupts and exceptions */
86 
87 /*! Called by exception handler to write or read
88     exception processing events. */
89 bool replay_exception(void);
90 /*! Used to determine that exception is pending.
91     Does not proceed to the next event in the log. */
92 bool replay_has_exception(void);
93 /*! Called by interrupt handlers to write or read
94     interrupt processing events.
95     \return true if interrupt should be processed */
96 bool replay_interrupt(void);
97 /*! Tries to read interrupt event from the file.
98     Returns true, when interrupt request is pending */
99 bool replay_has_interrupt(void);
100 
101 /* Processing clocks and other time sources */
102 
103 /*! Save the specified clock */
104 int64_t replay_save_clock(ReplayClockKind kind, int64_t clock,
105                           int64_t raw_icount);
106 /*! Read the specified clock from the log or return cached data */
107 int64_t replay_read_clock(ReplayClockKind kind);
108 /*! Saves or reads the clock depending on the current replay mode. */
109 #define REPLAY_CLOCK(clock, value)                                      \
110     (replay_mode == REPLAY_MODE_PLAY ? replay_read_clock((clock))       \
111         : replay_mode == REPLAY_MODE_RECORD                             \
112             ? replay_save_clock((clock), (value), cpu_get_icount_raw()) \
113         : (value))
114 #define REPLAY_CLOCK_LOCKED(clock, value)                               \
115     (replay_mode == REPLAY_MODE_PLAY ? replay_read_clock((clock))       \
116         : replay_mode == REPLAY_MODE_RECORD                             \
117             ? replay_save_clock((clock), (value), cpu_get_icount_raw_locked()) \
118         : (value))
119 
120 /* Events */
121 
122 /*! Called when qemu shutdown is requested. */
123 void replay_shutdown_request(ShutdownCause cause);
124 /*! Should be called at check points in the execution.
125     These check points are skipped, if they were not met.
126     Saves checkpoint in the SAVE mode and validates in the PLAY mode.
127     Returns 0 in PLAY mode if checkpoint was not found.
128     Returns 1 in all other cases. */
129 bool replay_checkpoint(ReplayCheckpoint checkpoint);
130 /*! Used to determine that checkpoint is pending.
131     Does not proceed to the next event in the log. */
132 bool replay_has_checkpoint(void);
133 
134 /* Asynchronous events queue */
135 
136 /*! Disables storing events in the queue */
137 void replay_disable_events(void);
138 /*! Enables storing events in the queue */
139 void replay_enable_events(void);
140 /*! Returns true when saving events is enabled */
141 bool replay_events_enabled(void);
142 /*! Adds bottom half event to the queue */
143 void replay_bh_schedule_event(QEMUBH *bh);
144 /* Adds oneshot bottom half event to the queue */
145 void replay_bh_schedule_oneshot_event(AioContext *ctx,
146     QEMUBHFunc *cb, void *opaque);
147 /*! Adds input event to the queue */
148 void replay_input_event(QemuConsole *src, InputEvent *evt);
149 /*! Adds input sync event to the queue */
150 void replay_input_sync_event(void);
151 /*! Adds block layer event to the queue */
152 void replay_block_event(QEMUBH *bh, uint64_t id);
153 /*! Returns ID for the next block event */
154 uint64_t blkreplay_next_id(void);
155 
156 /* Character device */
157 
158 /*! Registers char driver to save it's events */
159 void replay_register_char_driver(struct Chardev *chr);
160 /*! Saves write to char device event to the log */
161 void replay_chr_be_write(struct Chardev *s, uint8_t *buf, int len);
162 /*! Writes char write return value to the replay log. */
163 void replay_char_write_event_save(int res, int offset);
164 /*! Reads char write return value from the replay log. */
165 void replay_char_write_event_load(int *res, int *offset);
166 /*! Reads information about read_all character event. */
167 int replay_char_read_all_load(uint8_t *buf);
168 /*! Writes character read_all error code into the replay log. */
169 void replay_char_read_all_save_error(int res);
170 /*! Writes character read_all execution result into the replay log. */
171 void replay_char_read_all_save_buf(uint8_t *buf, int offset);
172 
173 /* Network */
174 
175 /*! Registers replay network filter attached to some backend. */
176 ReplayNetState *replay_register_net(NetFilterState *nfs);
177 /*! Unregisters replay network filter. */
178 void replay_unregister_net(ReplayNetState *rns);
179 /*! Called to write network packet to the replay log. */
180 void replay_net_packet_event(ReplayNetState *rns, unsigned flags,
181                              const struct iovec *iov, int iovcnt);
182 
183 /* Audio */
184 
185 /*! Saves/restores number of played samples of audio out operation. */
186 void replay_audio_out(size_t *played);
187 /*! Saves/restores recorded samples of audio in operation. */
188 void replay_audio_in(size_t *recorded, void *samples, size_t *wpos, size_t size);
189 
190 /* VM state operations */
191 
192 /*! Called at the start of execution.
193     Loads or saves initial vmstate depending on execution mode. */
194 void replay_vmstate_init(void);
195 /*! Called to ensure that replay state is consistent and VM snapshot
196     can be created */
197 bool replay_can_snapshot(void);
198 
199 #endif
200