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 /* Returns name of the replay log file */ 76 const char *replay_get_filename(void); 77 78 /* Processing the instructions */ 79 80 /*! Returns number of executed instructions. */ 81 uint64_t replay_get_current_icount(void); 82 /*! Returns number of instructions to execute in replay mode. */ 83 int replay_get_instructions(void); 84 /*! Updates instructions counter in replay mode. */ 85 void replay_account_executed_instructions(void); 86 87 /* Interrupts and exceptions */ 88 89 /*! Called by exception handler to write or read 90 exception processing events. */ 91 bool replay_exception(void); 92 /*! Used to determine that exception is pending. 93 Does not proceed to the next event in the log. */ 94 bool replay_has_exception(void); 95 /*! Called by interrupt handlers to write or read 96 interrupt processing events. 97 \return true if interrupt should be processed */ 98 bool replay_interrupt(void); 99 /*! Tries to read interrupt event from the file. 100 Returns true, when interrupt request is pending */ 101 bool replay_has_interrupt(void); 102 103 /* Processing clocks and other time sources */ 104 105 /*! Save the specified clock */ 106 int64_t replay_save_clock(ReplayClockKind kind, int64_t clock, 107 int64_t raw_icount); 108 /*! Read the specified clock from the log or return cached data */ 109 int64_t replay_read_clock(ReplayClockKind kind); 110 /*! Saves or reads the clock depending on the current replay mode. */ 111 #define REPLAY_CLOCK(clock, value) \ 112 (replay_mode == REPLAY_MODE_PLAY ? replay_read_clock((clock)) \ 113 : replay_mode == REPLAY_MODE_RECORD \ 114 ? replay_save_clock((clock), (value), icount_get_raw()) \ 115 : (value)) 116 #define REPLAY_CLOCK_LOCKED(clock, value) \ 117 (replay_mode == REPLAY_MODE_PLAY ? replay_read_clock((clock)) \ 118 : replay_mode == REPLAY_MODE_RECORD \ 119 ? replay_save_clock((clock), (value), icount_get_raw_locked()) \ 120 : (value)) 121 122 /* Processing data from random generators */ 123 124 /* Saves the values from the random number generator */ 125 void replay_save_random(int ret, void *buf, size_t len); 126 /* Loads the saved values for the random number generator */ 127 int replay_read_random(void *buf, size_t len); 128 129 /* Events */ 130 131 /*! Called when qemu shutdown is requested. */ 132 void replay_shutdown_request(ShutdownCause cause); 133 /*! Should be called at check points in the execution. 134 These check points are skipped, if they were not met. 135 Saves checkpoint in the SAVE mode and validates in the PLAY mode. 136 Returns 0 in PLAY mode if checkpoint was not found. 137 Returns 1 in all other cases. */ 138 bool replay_checkpoint(ReplayCheckpoint checkpoint); 139 /*! Used to determine that checkpoint is pending. 140 Does not proceed to the next event in the log. */ 141 bool replay_has_checkpoint(void); 142 143 /* Asynchronous events queue */ 144 145 /*! Disables storing events in the queue */ 146 void replay_disable_events(void); 147 /*! Enables storing events in the queue */ 148 void replay_enable_events(void); 149 /*! Returns true when saving events is enabled */ 150 bool replay_events_enabled(void); 151 /*! Adds bottom half event to the queue */ 152 void replay_bh_schedule_event(QEMUBH *bh); 153 /* Adds oneshot bottom half event to the queue */ 154 void replay_bh_schedule_oneshot_event(AioContext *ctx, 155 QEMUBHFunc *cb, void *opaque); 156 /*! Adds input event to the queue */ 157 void replay_input_event(QemuConsole *src, InputEvent *evt); 158 /*! Adds input sync event to the queue */ 159 void replay_input_sync_event(void); 160 /*! Adds block layer event to the queue */ 161 void replay_block_event(QEMUBH *bh, uint64_t id); 162 /*! Returns ID for the next block event */ 163 uint64_t blkreplay_next_id(void); 164 165 /* Character device */ 166 167 /*! Registers char driver to save it's events */ 168 void replay_register_char_driver(struct Chardev *chr); 169 /*! Saves write to char device event to the log */ 170 void replay_chr_be_write(struct Chardev *s, uint8_t *buf, int len); 171 /*! Writes char write return value to the replay log. */ 172 void replay_char_write_event_save(int res, int offset); 173 /*! Reads char write return value from the replay log. */ 174 void replay_char_write_event_load(int *res, int *offset); 175 /*! Reads information about read_all character event. */ 176 int replay_char_read_all_load(uint8_t *buf); 177 /*! Writes character read_all error code into the replay log. */ 178 void replay_char_read_all_save_error(int res); 179 /*! Writes character read_all execution result into the replay log. */ 180 void replay_char_read_all_save_buf(uint8_t *buf, int offset); 181 182 /* Network */ 183 184 /*! Registers replay network filter attached to some backend. */ 185 ReplayNetState *replay_register_net(NetFilterState *nfs); 186 /*! Unregisters replay network filter. */ 187 void replay_unregister_net(ReplayNetState *rns); 188 /*! Called to write network packet to the replay log. */ 189 void replay_net_packet_event(ReplayNetState *rns, unsigned flags, 190 const struct iovec *iov, int iovcnt); 191 192 /* Audio */ 193 194 /*! Saves/restores number of played samples of audio out operation. */ 195 void replay_audio_out(size_t *played); 196 /*! Saves/restores recorded samples of audio in operation. */ 197 void replay_audio_in(size_t *recorded, void *samples, size_t *wpos, size_t size); 198 199 /* VM state operations */ 200 201 /*! Called at the start of execution. 202 Loads or saves initial vmstate depending on execution mode. */ 203 void replay_vmstate_init(void); 204 /*! Called to ensure that replay state is consistent and VM snapshot 205 can be created */ 206 bool replay_can_snapshot(void); 207 208 #endif 209