xref: /openbmc/qemu/replay/replay-internal.h (revision effd60c8)
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 /* Asynchronous events IDs */
16 
17 typedef enum ReplayAsyncEventKind {
18     REPLAY_ASYNC_EVENT_BH,
19     REPLAY_ASYNC_EVENT_BH_ONESHOT,
20     REPLAY_ASYNC_EVENT_INPUT,
21     REPLAY_ASYNC_EVENT_INPUT_SYNC,
22     REPLAY_ASYNC_EVENT_CHAR_READ,
23     REPLAY_ASYNC_EVENT_BLOCK,
24     REPLAY_ASYNC_EVENT_NET,
25     REPLAY_ASYNC_COUNT
26 } ReplayAsyncEventKind;
27 
28 /*
29  * Any changes to order/number of events will need to bump
30  * REPLAY_VERSION to prevent confusion with old logs. Also don't
31  * forget to update replay_event_name() to make your debugging life
32  * easier.
33  */
34 enum ReplayEvents {
35     /* for instruction event */
36     EVENT_INSTRUCTION,
37     /* for software interrupt */
38     EVENT_INTERRUPT,
39     /* for emulated exceptions */
40     EVENT_EXCEPTION,
41     /* for async events */
42     EVENT_ASYNC,
43     EVENT_ASYNC_LAST = EVENT_ASYNC + REPLAY_ASYNC_COUNT - 1,
44     /* for shutdown requests, range allows recovery of ShutdownCause */
45     EVENT_SHUTDOWN,
46     EVENT_SHUTDOWN_LAST = EVENT_SHUTDOWN + SHUTDOWN_CAUSE__MAX,
47     /* for character device write event */
48     EVENT_CHAR_WRITE,
49     /* for character device read all event */
50     EVENT_CHAR_READ_ALL,
51     EVENT_CHAR_READ_ALL_ERROR,
52     /* for audio out event */
53     EVENT_AUDIO_OUT,
54     /* for audio in event */
55     EVENT_AUDIO_IN,
56     /* for random number generator */
57     EVENT_RANDOM,
58     /* for clock read/writes */
59     /* some of greater codes are reserved for clocks */
60     EVENT_CLOCK,
61     EVENT_CLOCK_LAST = EVENT_CLOCK + REPLAY_CLOCK_COUNT - 1,
62     /* for checkpoint event */
63     /* some of greater codes are reserved for checkpoints */
64     EVENT_CHECKPOINT,
65     EVENT_CHECKPOINT_LAST = EVENT_CHECKPOINT + CHECKPOINT_COUNT - 1,
66     /* end of log event */
67     EVENT_END,
68     EVENT_COUNT
69 };
70 
71 /**
72  * typedef ReplayState - global tracking Replay state
73  *
74  * This structure tracks where we are in the current ReplayState
75  * including the logged events from the recorded replay stream. Some
76  * of the data is also stored/restored from VMStateDescription when VM
77  * save/restore events take place.
78  *
79  * @cached_clock: Cached clocks values
80  * @current_icount: number of processed instructions
81  * @instruction_count: number of instructions until next event
82  * @current_event: current event index
83  * @data_kind: current event
84  * @has_unread_data: true if event not yet processed
85  * @file_offset: offset into replay log at replay snapshot
86  * @block_request_id: current serialised block request id
87  * @read_event_id: current async read event id
88  */
89 typedef struct ReplayState {
90     int64_t cached_clock[REPLAY_CLOCK_COUNT];
91     uint64_t current_icount;
92     int instruction_count;
93     unsigned int current_event;
94     unsigned int data_kind;
95     bool has_unread_data;
96     uint64_t file_offset;
97     uint64_t block_request_id;
98     uint64_t read_event_id;
99 } ReplayState;
100 extern ReplayState replay_state;
101 
102 /* File for replay writing */
103 extern FILE *replay_file;
104 /* Instruction count of the replay breakpoint */
105 extern uint64_t replay_break_icount;
106 /* Timer for the replay breakpoint callback */
107 extern QEMUTimer *replay_break_timer;
108 
109 void replay_put_byte(uint8_t byte);
110 void replay_put_event(uint8_t event);
111 void replay_put_word(uint16_t word);
112 void replay_put_dword(uint32_t dword);
113 void replay_put_qword(int64_t qword);
114 void replay_put_array(const uint8_t *buf, size_t size);
115 
116 uint8_t replay_get_byte(void);
117 uint16_t replay_get_word(void);
118 uint32_t replay_get_dword(void);
119 int64_t replay_get_qword(void);
120 void replay_get_array(uint8_t *buf, size_t *size);
121 void replay_get_array_alloc(uint8_t **buf, size_t *size);
122 
123 /* Mutex functions for protecting replay log file and ensuring
124  * synchronisation between vCPU and main-loop threads. */
125 
126 void replay_mutex_init(void);
127 bool replay_mutex_locked(void);
128 
129 /*! Checks error status of the file. */
130 void replay_check_error(void);
131 
132 /*! Finishes processing of the replayed event and fetches
133     the next event from the log. */
134 void replay_finish_event(void);
135 /*! Reads data type from the file and stores it in the
136     data_kind variable. */
137 void replay_fetch_data_kind(void);
138 
139 /*! Advance replay_state.current_icount to the specified value. */
140 void replay_advance_current_icount(uint64_t current_icount);
141 /*! Saves queued events (like instructions and sound). */
142 void replay_save_instructions(void);
143 
144 /*! Skips async events until some sync event will be found.
145     \return true, if event was found */
146 bool replay_next_event_is(int event);
147 
148 /*! Reads next clock value from the file.
149     If clock kind read from the file is different from the parameter,
150     the value is not used. */
151 void replay_read_next_clock(ReplayClockKind kind);
152 
153 /* Asynchronous events queue */
154 
155 /*! Initializes events' processing internals */
156 void replay_init_events(void);
157 /*! Clears internal data structures for events handling */
158 void replay_finish_events(void);
159 /*! Returns true if there are any unsaved events in the queue */
160 bool replay_has_events(void);
161 /*! Saves events from queue into the file */
162 void replay_save_events(void);
163 /*! Read events from the file into the input queue */
164 void replay_read_events(void);
165 /*! Adds specified async event to the queue */
166 void replay_add_event(ReplayAsyncEventKind event_kind, void *opaque,
167                       void *opaque2, uint64_t id);
168 
169 /* Input events */
170 
171 /*! Saves input event to the log */
172 void replay_save_input_event(InputEvent *evt);
173 /*! Reads input event from the log */
174 InputEvent *replay_read_input_event(void);
175 /*! Adds input event to the queue */
176 void replay_add_input_event(struct InputEvent *event);
177 /*! Adds input sync event to the queue */
178 void replay_add_input_sync_event(void);
179 
180 /* Character devices */
181 
182 /*! Called to run char device read event. */
183 void replay_event_char_read_run(void *opaque);
184 /*! Writes char read event to the file. */
185 void replay_event_char_read_save(void *opaque);
186 /*! Reads char event read from the file. */
187 void *replay_event_char_read_load(void);
188 
189 /* Network devices */
190 
191 /*! Called to run network event. */
192 void replay_event_net_run(void *opaque);
193 /*! Writes network event to the file. */
194 void replay_event_net_save(void *opaque);
195 /*! Reads network from the file. */
196 void *replay_event_net_load(void);
197 
198 /* Diagnostics */
199 
200 /**
201  * replay_sync_error(): report sync error and exit
202  *
203  * When we reach an error condition we want to report it centrally so
204  * we can also dump some useful information into the logs.
205  */
206 G_NORETURN void replay_sync_error(const char *error);
207 
208 /* VMState-related functions */
209 
210 /* Registers replay VMState.
211    Should be called before virtual devices initialization
212    to make cached timers available for post_load functions. */
213 void replay_vmstate_register(void);
214 
215 #endif
216