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