1 #ifndef REPLAY_INTERNAL_H 2 #define REPLAY_INTERNAL_H 3 4 /* 5 * replay-internal.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 16 enum ReplayEvents { 17 /* for instruction event */ 18 EVENT_INSTRUCTION, 19 /* for software interrupt */ 20 EVENT_INTERRUPT, 21 /* for emulated exceptions */ 22 EVENT_EXCEPTION, 23 /* for async events */ 24 EVENT_ASYNC, 25 /* for shutdown request */ 26 EVENT_SHUTDOWN, 27 /* for character device write event */ 28 EVENT_CHAR_WRITE, 29 /* for character device read all event */ 30 EVENT_CHAR_READ_ALL, 31 EVENT_CHAR_READ_ALL_ERROR, 32 /* for audio out event */ 33 EVENT_AUDIO_OUT, 34 /* for audio in event */ 35 EVENT_AUDIO_IN, 36 /* for clock read/writes */ 37 /* some of greater codes are reserved for clocks */ 38 EVENT_CLOCK, 39 EVENT_CLOCK_LAST = EVENT_CLOCK + REPLAY_CLOCK_COUNT - 1, 40 /* for checkpoint event */ 41 /* some of greater codes are reserved for checkpoints */ 42 EVENT_CHECKPOINT, 43 EVENT_CHECKPOINT_LAST = EVENT_CHECKPOINT + CHECKPOINT_COUNT - 1, 44 /* end of log event */ 45 EVENT_END, 46 EVENT_COUNT 47 }; 48 49 /* Asynchronous events IDs */ 50 51 enum ReplayAsyncEventKind { 52 REPLAY_ASYNC_EVENT_BH, 53 REPLAY_ASYNC_EVENT_INPUT, 54 REPLAY_ASYNC_EVENT_INPUT_SYNC, 55 REPLAY_ASYNC_EVENT_CHAR_READ, 56 REPLAY_ASYNC_EVENT_BLOCK, 57 REPLAY_ASYNC_EVENT_NET, 58 REPLAY_ASYNC_COUNT 59 }; 60 61 typedef enum ReplayAsyncEventKind ReplayAsyncEventKind; 62 63 typedef struct ReplayState { 64 /*! Cached clock values. */ 65 int64_t cached_clock[REPLAY_CLOCK_COUNT]; 66 /*! Current step - number of processed instructions and timer events. */ 67 uint64_t current_step; 68 /*! Number of instructions to be executed before other events happen. */ 69 int instructions_count; 70 /*! Type of the currently executed event. */ 71 unsigned int data_kind; 72 /*! Flag which indicates that event is not processed yet. */ 73 unsigned int has_unread_data; 74 /*! Temporary variable for saving current log offset. */ 75 uint64_t file_offset; 76 /*! Next block operation id. 77 This counter is global, because requests from different 78 block devices should not get overlapping ids. */ 79 uint64_t block_request_id; 80 } ReplayState; 81 extern ReplayState replay_state; 82 83 /* File for replay writing */ 84 extern FILE *replay_file; 85 86 void replay_put_byte(uint8_t byte); 87 void replay_put_event(uint8_t event); 88 void replay_put_word(uint16_t word); 89 void replay_put_dword(uint32_t dword); 90 void replay_put_qword(int64_t qword); 91 void replay_put_array(const uint8_t *buf, size_t size); 92 93 uint8_t replay_get_byte(void); 94 uint16_t replay_get_word(void); 95 uint32_t replay_get_dword(void); 96 int64_t replay_get_qword(void); 97 void replay_get_array(uint8_t *buf, size_t *size); 98 void replay_get_array_alloc(uint8_t **buf, size_t *size); 99 100 /* Mutex functions for protecting replay log file */ 101 102 void replay_mutex_init(void); 103 void replay_mutex_destroy(void); 104 void replay_mutex_lock(void); 105 void replay_mutex_unlock(void); 106 107 /*! Checks error status of the file. */ 108 void replay_check_error(void); 109 110 /*! Finishes processing of the replayed event and fetches 111 the next event from the log. */ 112 void replay_finish_event(void); 113 /*! Reads data type from the file and stores it in the 114 data_kind variable. */ 115 void replay_fetch_data_kind(void); 116 117 /*! Saves queued events (like instructions and sound). */ 118 void replay_save_instructions(void); 119 120 /*! Skips async events until some sync event will be found. 121 \return true, if event was found */ 122 bool replay_next_event_is(int event); 123 124 /*! Reads next clock value from the file. 125 If clock kind read from the file is different from the parameter, 126 the value is not used. */ 127 void replay_read_next_clock(unsigned int kind); 128 129 /* Asynchronous events queue */ 130 131 /*! Initializes events' processing internals */ 132 void replay_init_events(void); 133 /*! Clears internal data structures for events handling */ 134 void replay_finish_events(void); 135 /*! Flushes events queue */ 136 void replay_flush_events(void); 137 /*! Clears events list before loading new VM state */ 138 void replay_clear_events(void); 139 /*! Returns true if there are any unsaved events in the queue */ 140 bool replay_has_events(void); 141 /*! Saves events from queue into the file */ 142 void replay_save_events(int checkpoint); 143 /*! Read events from the file into the input queue */ 144 void replay_read_events(int checkpoint); 145 /*! Adds specified async event to the queue */ 146 void replay_add_event(ReplayAsyncEventKind event_kind, void *opaque, 147 void *opaque2, uint64_t id); 148 149 /* Input events */ 150 151 /*! Saves input event to the log */ 152 void replay_save_input_event(InputEvent *evt); 153 /*! Reads input event from the log */ 154 InputEvent *replay_read_input_event(void); 155 /*! Adds input event to the queue */ 156 void replay_add_input_event(struct InputEvent *event); 157 /*! Adds input sync event to the queue */ 158 void replay_add_input_sync_event(void); 159 160 /* Character devices */ 161 162 /*! Called to run char device read event. */ 163 void replay_event_char_read_run(void *opaque); 164 /*! Writes char read event to the file. */ 165 void replay_event_char_read_save(void *opaque); 166 /*! Reads char event read from the file. */ 167 void *replay_event_char_read_load(void); 168 169 /* Network devices */ 170 171 /*! Called to run network event. */ 172 void replay_event_net_run(void *opaque); 173 /*! Writes network event to the file. */ 174 void replay_event_net_save(void *opaque); 175 /*! Reads network from the file. */ 176 void *replay_event_net_load(void); 177 178 /* VMState-related functions */ 179 180 /* Registers replay VMState. 181 Should be called before virtual devices initialization 182 to make cached timers available for post_load functions. */ 183 void replay_vmstate_register(void); 184 185 #endif 186