2016-12-12 15:38:21 +01:00
|
|
|
/*
|
|
|
|
* QEMU System Emulator
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2019-05-23 16:35:07 +02:00
|
|
|
|
2016-12-12 15:38:21 +01:00
|
|
|
#include "qemu/osdep.h"
|
Include qemu/main-loop.h less
In my "build everything" tree, changing qemu/main-loop.h triggers a
recompile of some 5600 out of 6600 objects (not counting tests and
objects that don't depend on qemu/osdep.h). It includes block/aio.h,
which in turn includes qemu/event_notifier.h, qemu/notify.h,
qemu/processor.h, qemu/qsp.h, qemu/queue.h, qemu/thread-posix.h,
qemu/thread.h, qemu/timer.h, and a few more.
Include qemu/main-loop.h only where it's needed. Touching it now
recompiles only some 1700 objects. For block/aio.h and
qemu/event_notifier.h, these numbers drop from 5600 to 2800. For the
others, they shrink only slightly.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20190812052359.30071-21-armbru@redhat.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Tested-by: Philippe Mathieu-Daudé <philmd@redhat.com>
2019-08-12 07:23:50 +02:00
|
|
|
#include "qemu/main-loop.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2016-12-12 15:38:21 +01:00
|
|
|
#include "qapi/error.h"
|
2017-01-26 14:19:46 +01:00
|
|
|
#include "chardev/char-win.h"
|
2016-12-12 15:38:21 +01:00
|
|
|
|
2017-01-04 20:03:08 +01:00
|
|
|
static void win_chr_read(Chardev *chr, DWORD len)
|
2016-12-12 15:38:21 +01:00
|
|
|
{
|
|
|
|
WinChardev *s = WIN_CHARDEV(chr);
|
2017-01-04 19:37:19 +01:00
|
|
|
int max_size = qemu_chr_be_can_write(chr);
|
2016-12-12 15:38:21 +01:00
|
|
|
int ret, err;
|
|
|
|
uint8_t buf[CHR_READ_BUF_LEN];
|
|
|
|
DWORD size;
|
|
|
|
|
2017-01-04 20:03:08 +01:00
|
|
|
if (len > max_size) {
|
|
|
|
len = max_size;
|
2017-01-04 19:37:19 +01:00
|
|
|
}
|
2017-01-04 20:03:08 +01:00
|
|
|
if (len == 0) {
|
2017-01-04 19:37:19 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-12 15:38:21 +01:00
|
|
|
ZeroMemory(&s->orecv, sizeof(s->orecv));
|
|
|
|
s->orecv.hEvent = s->hrecv;
|
2017-01-04 21:37:01 +01:00
|
|
|
ret = ReadFile(s->file, buf, len, &size, &s->orecv);
|
2016-12-12 15:38:21 +01:00
|
|
|
if (!ret) {
|
|
|
|
err = GetLastError();
|
|
|
|
if (err == ERROR_IO_PENDING) {
|
2017-01-04 21:37:01 +01:00
|
|
|
ret = GetOverlappedResult(s->file, &s->orecv, &size, TRUE);
|
2016-12-12 15:38:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > 0) {
|
|
|
|
qemu_chr_be_write(chr, buf, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 19:51:46 +01:00
|
|
|
static int win_chr_serial_poll(void *opaque)
|
2016-12-12 15:38:21 +01:00
|
|
|
{
|
|
|
|
Chardev *chr = CHARDEV(opaque);
|
|
|
|
WinChardev *s = WIN_CHARDEV(opaque);
|
|
|
|
COMSTAT status;
|
|
|
|
DWORD comerr;
|
|
|
|
|
2017-01-04 21:37:01 +01:00
|
|
|
ClearCommError(s->file, &comerr, &status);
|
2016-12-12 15:38:21 +01:00
|
|
|
if (status.cbInQue > 0) {
|
2017-01-04 20:03:08 +01:00
|
|
|
win_chr_read(chr, status.cbInQue);
|
2016-12-12 15:38:21 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-04 19:51:46 +01:00
|
|
|
int win_chr_serial_init(Chardev *chr, const char *filename, Error **errp)
|
2016-12-12 15:38:21 +01:00
|
|
|
{
|
|
|
|
WinChardev *s = WIN_CHARDEV(chr);
|
|
|
|
COMMCONFIG comcfg;
|
|
|
|
COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
|
|
|
|
COMSTAT comstat;
|
|
|
|
DWORD size;
|
|
|
|
DWORD err;
|
|
|
|
|
|
|
|
s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!s->hsend) {
|
|
|
|
error_setg(errp, "Failed CreateEvent");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!s->hrecv) {
|
|
|
|
error_setg(errp, "Failed CreateEvent");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:37:01 +01:00
|
|
|
s->file = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
|
2016-12-12 15:38:21 +01:00
|
|
|
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
|
2017-01-04 21:37:01 +01:00
|
|
|
if (s->file == INVALID_HANDLE_VALUE) {
|
2020-02-28 11:07:23 +01:00
|
|
|
error_setg_win32(errp, GetLastError(), "Failed CreateFile");
|
2017-01-04 21:37:01 +01:00
|
|
|
s->file = NULL;
|
2016-12-12 15:38:21 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:37:01 +01:00
|
|
|
if (!SetupComm(s->file, NRECVBUF, NSENDBUF)) {
|
2016-12-12 15:38:21 +01:00
|
|
|
error_setg(errp, "Failed SetupComm");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZeroMemory(&comcfg, sizeof(COMMCONFIG));
|
|
|
|
size = sizeof(COMMCONFIG);
|
|
|
|
GetDefaultCommConfig(filename, &comcfg, &size);
|
|
|
|
comcfg.dcb.DCBlength = sizeof(DCB);
|
|
|
|
CommConfigDialog(filename, NULL, &comcfg);
|
|
|
|
|
2017-01-04 21:37:01 +01:00
|
|
|
if (!SetCommState(s->file, &comcfg.dcb)) {
|
2016-12-12 15:38:21 +01:00
|
|
|
error_setg(errp, "Failed SetCommState");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:37:01 +01:00
|
|
|
if (!SetCommMask(s->file, EV_ERR)) {
|
2016-12-12 15:38:21 +01:00
|
|
|
error_setg(errp, "Failed SetCommMask");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
cto.ReadIntervalTimeout = MAXDWORD;
|
2017-01-04 21:37:01 +01:00
|
|
|
if (!SetCommTimeouts(s->file, &cto)) {
|
2016-12-12 15:38:21 +01:00
|
|
|
error_setg(errp, "Failed SetCommTimeouts");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:37:01 +01:00
|
|
|
if (!ClearCommError(s->file, &err, &comstat)) {
|
2016-12-12 15:38:21 +01:00
|
|
|
error_setg(errp, "Failed ClearCommError");
|
|
|
|
goto fail;
|
|
|
|
}
|
2017-01-04 19:51:46 +01:00
|
|
|
qemu_add_polling_cb(win_chr_serial_poll, chr);
|
2016-12-12 15:38:21 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int win_chr_pipe_poll(void *opaque)
|
|
|
|
{
|
|
|
|
Chardev *chr = CHARDEV(opaque);
|
|
|
|
WinChardev *s = WIN_CHARDEV(opaque);
|
|
|
|
DWORD size;
|
|
|
|
|
2017-01-04 21:37:01 +01:00
|
|
|
PeekNamedPipe(s->file, NULL, 0, NULL, &size, NULL);
|
2016-12-12 15:38:21 +01:00
|
|
|
if (size > 0) {
|
2017-01-04 20:03:08 +01:00
|
|
|
win_chr_read(chr, size);
|
2016-12-12 15:38:21 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called with chr_write_lock held. */
|
|
|
|
static int win_chr_write(Chardev *chr, const uint8_t *buf, int len1)
|
|
|
|
{
|
|
|
|
WinChardev *s = WIN_CHARDEV(chr);
|
|
|
|
DWORD len, ret, size, err;
|
|
|
|
|
|
|
|
len = len1;
|
|
|
|
ZeroMemory(&s->osend, sizeof(s->osend));
|
|
|
|
s->osend.hEvent = s->hsend;
|
|
|
|
while (len > 0) {
|
|
|
|
if (s->hsend) {
|
2017-01-04 21:37:01 +01:00
|
|
|
ret = WriteFile(s->file, buf, len, &size, &s->osend);
|
2016-12-12 15:38:21 +01:00
|
|
|
} else {
|
2017-01-04 21:37:01 +01:00
|
|
|
ret = WriteFile(s->file, buf, len, &size, NULL);
|
2016-12-12 15:38:21 +01:00
|
|
|
}
|
|
|
|
if (!ret) {
|
|
|
|
err = GetLastError();
|
|
|
|
if (err == ERROR_IO_PENDING) {
|
2017-01-04 21:37:01 +01:00
|
|
|
ret = GetOverlappedResult(s->file, &s->osend, &size, TRUE);
|
2016-12-12 15:38:21 +01:00
|
|
|
if (ret) {
|
|
|
|
buf += size;
|
|
|
|
len -= size;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf += size;
|
|
|
|
len -= size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len1 - len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void char_win_finalize(Object *obj)
|
|
|
|
{
|
|
|
|
Chardev *chr = CHARDEV(obj);
|
|
|
|
WinChardev *s = WIN_CHARDEV(chr);
|
|
|
|
|
|
|
|
if (s->hsend) {
|
|
|
|
CloseHandle(s->hsend);
|
|
|
|
}
|
|
|
|
if (s->hrecv) {
|
|
|
|
CloseHandle(s->hrecv);
|
|
|
|
}
|
2017-01-04 21:34:42 +01:00
|
|
|
if (!s->keep_open && s->file) {
|
2017-01-04 21:37:01 +01:00
|
|
|
CloseHandle(s->file);
|
2016-12-12 15:38:21 +01:00
|
|
|
}
|
|
|
|
if (s->fpipe) {
|
|
|
|
qemu_del_polling_cb(win_chr_pipe_poll, chr);
|
|
|
|
} else {
|
2017-01-04 19:51:46 +01:00
|
|
|
qemu_del_polling_cb(win_chr_serial_poll, chr);
|
2016-12-12 15:38:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:34:42 +01:00
|
|
|
void win_chr_set_file(Chardev *chr, HANDLE file, bool keep_open)
|
2016-12-12 15:38:21 +01:00
|
|
|
{
|
|
|
|
WinChardev *s = WIN_CHARDEV(chr);
|
|
|
|
|
2017-01-04 21:34:42 +01:00
|
|
|
s->keep_open = keep_open;
|
|
|
|
s->file = file;
|
2016-12-12 15:38:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void char_win_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
|
|
|
ChardevClass *cc = CHARDEV_CLASS(oc);
|
|
|
|
|
|
|
|
cc->chr_write = win_chr_write;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo char_win_type_info = {
|
|
|
|
.name = TYPE_CHARDEV_WIN,
|
|
|
|
.parent = TYPE_CHARDEV,
|
|
|
|
.instance_size = sizeof(WinChardev),
|
|
|
|
.instance_finalize = char_win_finalize,
|
|
|
|
.class_init = char_win_class_init,
|
|
|
|
.abstract = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&char_win_type_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(register_types);
|