aio: fix walking_handlers in the presence of nested qemu_aio_wait calls

When the io_read handler calls qemu_aio_wait, the nested
call to qemu_aio_wait will set walking_handlers to zero.
Then, if the io_read handler also disables itself aio.c
will end up using a freed pointer.  Fix this by keeping
a count of nested qemu_aio_wait calls.

Reported-by: Bharata B Rao <bharata@linux.vnet.ibm.com>
Tested-by: Bharata B Rao <bharata@linux.vnet.ibm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
aio: fix the fix

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
tools: split monitor-dummy.c, clean up qemu-tool.c

qemu-ga uses glib mostly, so it does not need a large part of the dummy
functions in qemu-tool.o (which are more related to the main loop and/or
the block layer).  Split the functions required by QAPI out into
monitor-dummy.c.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
block: move QEMUIOVector functions to iov.c

They do not really belong in cutils.c, and this way we can remove
iov.c from user-mode emulation.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
build: move cutils.c/iov.c to oslib-obj-y and block-obj-y respectively

cutils.c and iov.c appeared in the Makefile in multiple spots.  Clean
this up by moving them respectively to oslib-obj-y and block-obj-y.
Notably, qemu-ga and user-mode emulation both need cutils.c but not iov.c.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-ga: do not include the QEMU main loop and dependencies

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
coroutine: always use pooling

It makes sense to use it for other implementations than ucontext, too.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
build: do not rely on indirect inclusion of qemu-config.h

Some files in the block layer rely on qemu-char.h including qemu-config.h,
but the block layer does not need qemu-char.h at all.  Clean this up.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
event_notifier: enable it to use pipes

This takes the eventfd emulation code from the main loop.  When the
EventNotifier is used for the main loop too, we need this compatibility
code.

Without CONFIG_EVENTFD, event_notifier_get_fd is only usable for the
"read" side of the notifier, for example to set a select() handler.

Reviewed-by: Anthony Liguori <anthony@codemonkey.ws>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
event_notifier: add Win32 implementation

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
aio: change qemu_aio_set_fd_handler to return void

Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
aio: provide platform-independent API

This adds to aio.c a platform-independent API based on EventNotifiers, that
can be used by both POSIX and Win32.

Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
aio: introduce AioContext, move bottom halves there

Start introducing AioContext, which will let us remove globals from
aio.c/async.c, and introduce multiple I/O threads.

The bottom half functions now take an additional AioContext argument.
A bottom half is created with a specific AioContext that remains the
same throughout the lifetime.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
aio: add I/O handlers to the AioContext interface

With this patch, I/O handlers (including event notifier handlers) can be
attached to a single AioContext.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
aio: test node->deleted before calling io_flush

Otherwise, there could be a case where io_flush accesses freed
memory because it should not have been called.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
aio: add non-blocking variant of aio_wait

This will be used when polling the GSource attached to an AioContext.

Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
aio: prepare for introducing GSource-based dispatch

This adds a GPollFD to each AioHandler.  It will then be possible to
attach these GPollFDs to a GSource, and from there to the main loop.
aio_wait examines the GPollFDs and avoids calling select() if any
is set (similar to what it does if bottom halves are available).

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
aio: add Win32 implementation

The Win32 implementation will only accept EventNotifiers, thus a few
drivers are disabled under Windows.  EventNotifiers are a good match
for the GSource implementation, too, because the Win32 port of glib
allows to place their HANDLEs in a GPollFD.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
aio: make AioContexts GSources

This lets AioContexts be used (optionally) with a glib main loop.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
aio: add aio_notify

With this change async.c does not rely anymore on any service from
main-loop.c, i.e. it is completely self-contained.

Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>