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