target/arm: Add SMEEXC_EL to TB flags
[qemu.git] / include / qemu / main-loop.h
blob5518845299ddfa8947e4c5c1bf5579dc32b1561f
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #ifndef QEMU_MAIN_LOOP_H
26 #define QEMU_MAIN_LOOP_H
28 #include "block/aio.h"
29 #include "qom/object.h"
30 #include "sysemu/event-loop-base.h"
32 #define SIG_IPI SIGUSR1
34 #define TYPE_MAIN_LOOP "main-loop"
35 OBJECT_DECLARE_TYPE(MainLoop, MainLoopClass, MAIN_LOOP)
37 struct MainLoop {
38 EventLoopBase parent_obj;
40 typedef struct MainLoop MainLoop;
42 /**
43 * qemu_init_main_loop: Set up the process so that it can run the main loop.
45 * This includes setting up signal handlers. It should be called before
46 * any other threads are created. In addition, threads other than the
47 * main one should block signals that are trapped by the main loop.
48 * For simplicity, you can consider these signals to be safe: SIGUSR1,
49 * SIGUSR2, thread signals (SIGFPE, SIGILL, SIGSEGV, SIGBUS) and real-time
50 * signals if available. Remember that Windows in practice does not have
51 * signals, though.
53 * In the case of QEMU tools, this will also start/initialize timers.
55 int qemu_init_main_loop(Error **errp);
57 /**
58 * main_loop_wait: Run one iteration of the main loop.
60 * If @nonblocking is true, poll for events, otherwise suspend until
61 * one actually occurs. The main loop usually consists of a loop that
62 * repeatedly calls main_loop_wait(false).
64 * Main loop services include file descriptor callbacks, bottom halves
65 * and timers (defined in qemu/timer.h). Bottom halves are similar to timers
66 * that execute immediately, but have a lower overhead and scheduling them
67 * is wait-free, thread-safe and signal-safe.
69 * It is sometimes useful to put a whole program in a coroutine. In this
70 * case, the coroutine actually should be started from within the main loop,
71 * so that the main loop can run whenever the coroutine yields. To do this,
72 * you can use a bottom half to enter the coroutine as soon as the main loop
73 * starts:
75 * void enter_co_bh(void *opaque) {
76 * QEMUCoroutine *co = opaque;
77 * qemu_coroutine_enter(co);
78 * }
80 * ...
81 * QEMUCoroutine *co = qemu_coroutine_create(coroutine_entry, NULL);
82 * QEMUBH *start_bh = qemu_bh_new(enter_co_bh, co);
83 * qemu_bh_schedule(start_bh);
84 * while (...) {
85 * main_loop_wait(false);
86 * }
88 * (In the future we may provide a wrapper for this).
90 * @nonblocking: Whether the caller should block until an event occurs.
92 void main_loop_wait(int nonblocking);
94 /**
95 * qemu_get_aio_context: Return the main loop's AioContext
97 AioContext *qemu_get_aio_context(void);
99 /**
100 * qemu_notify_event: Force processing of pending events.
102 * Similar to signaling a condition variable, qemu_notify_event forces
103 * main_loop_wait to look at pending events and exit. The caller of
104 * main_loop_wait will usually call it again very soon, so qemu_notify_event
105 * also has the side effect of recalculating the sets of file descriptors
106 * that the main loop waits for.
108 * Calling qemu_notify_event is rarely necessary, because main loop
109 * services (bottom halves and timers) call it themselves.
111 void qemu_notify_event(void);
113 #ifdef _WIN32
114 /* return TRUE if no sleep should be done afterwards */
115 typedef int PollingFunc(void *opaque);
118 * qemu_add_polling_cb: Register a Windows-specific polling callback
120 * Currently, under Windows some events are polled rather than waited for.
121 * Polling callbacks do not ensure that @func is called timely, because
122 * the main loop might wait for an arbitrarily long time. If possible,
123 * you should instead create a separate thread that does a blocking poll
124 * and set a Win32 event object. The event can then be passed to
125 * qemu_add_wait_object.
127 * Polling callbacks really have nothing Windows specific in them, but
128 * as they are a hack and are currently not necessary under POSIX systems,
129 * they are only available when QEMU is running under Windows.
131 * @func: The function that does the polling, and returns 1 to force
132 * immediate completion of main_loop_wait.
133 * @opaque: A pointer-size value that is passed to @func.
135 int qemu_add_polling_cb(PollingFunc *func, void *opaque);
138 * qemu_del_polling_cb: Unregister a Windows-specific polling callback
140 * This function removes a callback that was registered with
141 * qemu_add_polling_cb.
143 * @func: The function that was passed to qemu_add_polling_cb.
144 * @opaque: A pointer-size value that was passed to qemu_add_polling_cb.
146 void qemu_del_polling_cb(PollingFunc *func, void *opaque);
148 /* Wait objects handling */
149 typedef void WaitObjectFunc(void *opaque);
152 * qemu_add_wait_object: Register a callback for a Windows handle
154 * Under Windows, the iohandler mechanism can only be used with sockets.
155 * QEMU must use the WaitForMultipleObjects API to wait on other handles.
156 * This function registers a #HANDLE with QEMU, so that it will be included
157 * in the main loop's calls to WaitForMultipleObjects. When the handle
158 * is in a signaled state, QEMU will call @func.
160 * @handle: The Windows handle to be observed.
161 * @func: A function to be called when @handle is in a signaled state.
162 * @opaque: A pointer-size value that is passed to @func.
164 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
167 * qemu_del_wait_object: Unregister a callback for a Windows handle
169 * This function removes a callback that was registered with
170 * qemu_add_wait_object.
172 * @func: The function that was passed to qemu_add_wait_object.
173 * @opaque: A pointer-size value that was passed to qemu_add_wait_object.
175 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
176 #endif
178 /* async I/O support */
180 typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
183 * IOCanReadHandler: Return the number of bytes that #IOReadHandler can accept
185 * This function reports how many bytes #IOReadHandler is prepared to accept.
186 * #IOReadHandler may be invoked with up to this number of bytes. If this
187 * function returns 0 then #IOReadHandler is not invoked.
189 * This function is typically called from an event loop. If the number of
190 * bytes changes outside the event loop (e.g. because a vcpu thread drained the
191 * buffer), then it is necessary to kick the event loop so that this function
192 * is called again. aio_notify() or qemu_notify_event() can be used to kick
193 * the event loop.
195 typedef int IOCanReadHandler(void *opaque);
198 * qemu_set_fd_handler: Register a file descriptor with the main loop
200 * This function tells the main loop to wake up whenever one of the
201 * following conditions is true:
203 * 1) if @fd_write is not %NULL, when the file descriptor is writable;
205 * 2) if @fd_read is not %NULL, when the file descriptor is readable.
207 * The callbacks that are set up by qemu_set_fd_handler are level-triggered.
208 * If @fd_read does not read from @fd, or @fd_write does not write to @fd
209 * until its buffers are full, they will be called again on the next
210 * iteration.
212 * @fd: The file descriptor to be observed. Under Windows it must be
213 * a #SOCKET.
215 * @fd_read: A level-triggered callback that is fired if @fd is readable
216 * at the beginning of a main loop iteration, or if it becomes readable
217 * during one.
219 * @fd_write: A level-triggered callback that is fired when @fd is writable
220 * at the beginning of a main loop iteration, or if it becomes writable
221 * during one.
223 * @opaque: A pointer-sized value that is passed to @fd_read and @fd_write.
225 void qemu_set_fd_handler(int fd,
226 IOHandler *fd_read,
227 IOHandler *fd_write,
228 void *opaque);
232 * event_notifier_set_handler: Register an EventNotifier with the main loop
234 * This function tells the main loop to wake up whenever the
235 * #EventNotifier was set.
237 * @e: The #EventNotifier to be observed.
239 * @handler: A level-triggered callback that is fired when @e
240 * has been set. @e is passed to it as a parameter.
242 void event_notifier_set_handler(EventNotifier *e,
243 EventNotifierHandler *handler);
245 GSource *iohandler_get_g_source(void);
246 AioContext *iohandler_get_aio_context(void);
249 * qemu_mutex_iothread_locked: Return lock status of the main loop mutex.
251 * The main loop mutex is the coarsest lock in QEMU, and as such it
252 * must always be taken outside other locks. This function helps
253 * functions take different paths depending on whether the current
254 * thread is running within the main loop mutex.
256 * This function should never be used in the block layer, because
257 * unit tests, block layer tools and qemu-storage-daemon do not
258 * have a BQL.
259 * Please instead refer to qemu_in_main_thread().
261 bool qemu_mutex_iothread_locked(void);
264 * qemu_in_main_thread: return whether it's possible to safely access
265 * the global state of the block layer.
267 * Global state of the block layer is not accessible from I/O threads
268 * or worker threads; only from threads that "own" the default
269 * AioContext that qemu_get_aio_context() returns. For tests, block
270 * layer tools and qemu-storage-daemon there is a designated thread that
271 * runs the event loop for qemu_get_aio_context(), and that is the
272 * main thread.
274 * For emulators, however, any thread that holds the BQL can act
275 * as the block layer main thread; this will be any of the actual
276 * main thread, the vCPU threads or the RCU thread.
278 * For clarity, do not use this function outside the block layer.
280 bool qemu_in_main_thread(void);
282 /* Mark and check that the function is part of the global state API. */
283 #ifdef CONFIG_COCOA
285 * When using the Cocoa UI, addRemovableDevicesMenuItems() is called from
286 * a thread different from the QEMU main thread and can not take the BQL,
287 * triggering this assertions in the block layer (commit 0439c5a462).
288 * As the Cocoa fix is not trivial, disable this assertion for the v7.0.0
289 * release (when using Cocoa); we will restore it immediately after the
290 * release.
291 * This issue is tracked as https://gitlab.com/qemu-project/qemu/-/issues/926
293 #define GLOBAL_STATE_CODE()
294 #else
295 #define GLOBAL_STATE_CODE() \
296 do { \
297 assert(qemu_in_main_thread()); \
298 } while (0)
299 #endif /* CONFIG_COCOA */
301 /* Mark and check that the function is part of the I/O API. */
302 #define IO_CODE() \
303 do { \
304 /* nop */ \
305 } while (0)
307 /* Mark and check that the function is part of the "I/O OR GS" API. */
308 #define IO_OR_GS_CODE() \
309 do { \
310 /* nop */ \
311 } while (0)
314 * qemu_mutex_lock_iothread: Lock the main loop mutex.
316 * This function locks the main loop mutex. The mutex is taken by
317 * main() in vl.c and always taken except while waiting on
318 * external events (such as with select). The mutex should be taken
319 * by threads other than the main loop thread when calling
320 * qemu_bh_new(), qemu_set_fd_handler() and basically all other
321 * functions documented in this file.
323 * NOTE: tools currently are single-threaded and qemu_mutex_lock_iothread
324 * is a no-op there.
326 #define qemu_mutex_lock_iothread() \
327 qemu_mutex_lock_iothread_impl(__FILE__, __LINE__)
328 void qemu_mutex_lock_iothread_impl(const char *file, int line);
331 * qemu_mutex_unlock_iothread: Unlock the main loop mutex.
333 * This function unlocks the main loop mutex. The mutex is taken by
334 * main() in vl.c and always taken except while waiting on
335 * external events (such as with select). The mutex should be unlocked
336 * as soon as possible by threads other than the main loop thread,
337 * because it prevents the main loop from processing callbacks,
338 * including timers and bottom halves.
340 * NOTE: tools currently are single-threaded and qemu_mutex_unlock_iothread
341 * is a no-op there.
343 void qemu_mutex_unlock_iothread(void);
346 * qemu_cond_wait_iothread: Wait on condition for the main loop mutex
348 * This function atomically releases the main loop mutex and causes
349 * the calling thread to block on the condition.
351 void qemu_cond_wait_iothread(QemuCond *cond);
354 * qemu_cond_timedwait_iothread: like the previous, but with timeout
356 void qemu_cond_timedwait_iothread(QemuCond *cond, int ms);
358 /* internal interfaces */
360 void qemu_fd_register(int fd);
362 #define qemu_bh_new(cb, opaque) \
363 qemu_bh_new_full((cb), (opaque), (stringify(cb)))
364 QEMUBH *qemu_bh_new_full(QEMUBHFunc *cb, void *opaque, const char *name);
365 void qemu_bh_schedule_idle(QEMUBH *bh);
367 enum {
368 MAIN_LOOP_POLL_FILL,
369 MAIN_LOOP_POLL_ERR,
370 MAIN_LOOP_POLL_OK,
373 typedef struct MainLoopPoll {
374 int state;
375 uint32_t timeout;
376 GArray *pollfds;
377 } MainLoopPoll;
379 void main_loop_poll_add_notifier(Notifier *notify);
380 void main_loop_poll_remove_notifier(Notifier *notify);
382 #endif