diff options
143 files changed, 7049 insertions, 6028 deletions
diff --git a/.gitignore b/.gitignore index 78f180a020..c563dc175f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,18 +6,12 @@ /config.status /config-temp /trace-events-all -/trace/generated-tracers.h -/trace/generated-tracers.c -/trace/generated-tracers-dtrace.h -/trace/generated-tracers.dtrace /trace/generated-events.h /trace/generated-events.c /trace/generated-helpers-wrappers.h /trace/generated-helpers.h /trace/generated-helpers.c /trace/generated-tcg-tracers.h -/trace/generated-ust-provider.h -/trace/generated-ust.c /ui/shader/texture-blit-frag.h /ui/shader/texture-blit-vert.h *-timestamp @@ -120,3 +114,19 @@ tags TAGS docker-src.* *~ +trace.h +trace.c +trace-ust.h +trace-ust.h +trace-dtrace.h +trace-dtrace.dtrace +trace-root.h +trace-root.c +trace-ust-root.h +trace-ust-root.h +trace-ust-all.h +trace-ust-all.c +trace-dtrace-root.h +trace-dtrace-root.dtrace +trace-ust-all.h +trace-ust-all.c diff --git a/MAINTAINERS b/MAINTAINERS index b1f4d9d4c2..00b81f1432 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1197,8 +1197,9 @@ T: git git://github.com/jnsnow/qemu.git bitmaps Character device backends M: Paolo Bonzini <pbonzini@redhat.com> +M: Marc-André Lureau <marcandre.lureau@redhat.com> S: Maintained -F: qemu-char.c +F: chardev/ F: backends/msmouse.c F: backends/testdev.c @@ -56,25 +56,136 @@ GENERATED_SOURCES += qmp-marshal.c qapi-types.c qapi-visit.c qapi-event.c GENERATED_HEADERS += qmp-introspect.h GENERATED_SOURCES += qmp-introspect.c -GENERATED_HEADERS += trace/generated-tracers.h -ifeq ($(findstring dtrace,$(TRACE_BACKENDS)),dtrace) -GENERATED_HEADERS += trace/generated-tracers-dtrace.h -endif -GENERATED_SOURCES += trace/generated-tracers.c - GENERATED_HEADERS += trace/generated-tcg-tracers.h GENERATED_HEADERS += trace/generated-helpers-wrappers.h GENERATED_HEADERS += trace/generated-helpers.h GENERATED_SOURCES += trace/generated-helpers.c -ifeq ($(findstring ust,$(TRACE_BACKENDS)),ust) -GENERATED_HEADERS += trace/generated-ust-provider.h -GENERATED_SOURCES += trace/generated-ust.c +ifdef CONFIG_TRACE_UST +GENERATED_HEADERS += trace-ust-all.h +GENERATED_SOURCES += trace-ust-all.c endif GENERATED_HEADERS += module_block.h +TRACE_HEADERS = trace-root.h $(trace-events-subdirs:%=%/trace.h) +TRACE_SOURCES = trace-root.c $(trace-events-subdirs:%=%/trace.c) +TRACE_DTRACE = +ifdef CONFIG_TRACE_DTRACE +TRACE_HEADERS += trace-dtrace-root.h $(trace-events-subdirs:%=%/trace-dtrace.h) +TRACE_DTRACE += trace-dtrace-root.dtrace $(trace-events-subdirs:%=%/trace-dtrace.dtrace) +endif +ifdef CONFIG_TRACE_UST +TRACE_HEADERS += trace-ust-root.h $(trace-events-subdirs:%=%/trace-ust.h) +endif + +GENERATED_HEADERS += $(TRACE_HEADERS) +GENERATED_SOURCES += $(TRACE_SOURCES) + +trace-group-name = $(shell dirname $1 | sed -e 's/[^a-zA-Z0-9]/_/g') + +%/trace.h: %/trace.h-timestamp + @cmp $< $@ >/dev/null 2>&1 || cp $< $@ +%/trace.h-timestamp: $(SRC_PATH)/%/trace-events $(tracetool-y) + $(call quiet-command,$(TRACETOOL) \ + --group=$(call trace-group-name,$@) \ + --format=h \ + --backends=$(TRACE_BACKENDS) \ + $< > $@,"GEN","$(@:%-timestamp=%)") + +%/trace.c: %/trace.c-timestamp + @cmp $< $@ >/dev/null 2>&1 || cp $< $@ +%/trace.c-timestamp: $(SRC_PATH)/%/trace-events $(tracetool-y) + $(call quiet-command,$(TRACETOOL) \ + --group=$(call trace-group-name,$@) \ + --format=c \ + --backends=$(TRACE_BACKENDS) \ + $< > $@,"GEN","$(@:%-timestamp=%)") + +%/trace-ust.h: %/trace-ust.h-timestamp + @cmp $< $@ >/dev/null 2>&1 || cp $< $@ +%/trace-ust.h-timestamp: $(SRC_PATH)/%/trace-events $(tracetool-y) + $(call quiet-command,$(TRACETOOL) \ + --group=$(call trace-group-name,$@) \ + --format=ust-events-h \ + --backends=$(TRACE_BACKENDS) \ + $< > $@,"GEN","$(@:%-timestamp=%)") + +%/trace-dtrace.dtrace: %/trace-dtrace.dtrace-timestamp + @cmp $< $@ >/dev/null 2>&1 || cp $< $@ +%/trace-dtrace.dtrace-timestamp: $(SRC_PATH)/%/trace-events $(BUILD_DIR)/config-host.mak $(tracetool-y) + $(call quiet-command,$(TRACETOOL) \ + --group=$(call trace-group-name,$@) \ + --format=d \ + --backends=$(TRACE_BACKENDS) \ + $< > $@,"GEN","$(@:%-timestamp=%)") + +%/trace-dtrace.h: %/trace-dtrace.dtrace $(tracetool-y) + $(call quiet-command,dtrace -o $@ -h -s $<, "GEN","$@") + +%/trace-dtrace.o: %/trace-dtrace.dtrace $(tracetool-y) + + +trace-root.h: trace-root.h-timestamp + @cmp $< $@ >/dev/null 2>&1 || cp $< $@ +trace-root.h-timestamp: $(SRC_PATH)/trace-events $(tracetool-y) + $(call quiet-command,$(TRACETOOL) \ + --group=root \ + --format=h \ + --backends=$(TRACE_BACKENDS) \ + $< > $@,"GEN","$(@:%-timestamp=%)") + +trace-root.c: trace-root.c-timestamp + @cmp $< $@ >/dev/null 2>&1 || cp $< $@ +trace-root.c-timestamp: $(SRC_PATH)/trace-events $(tracetool-y) + $(call quiet-command,$(TRACETOOL) \ + --group=root \ + --format=c \ + --backends=$(TRACE_BACKENDS) \ + $< > $@,"GEN","$(@:%-timestamp=%)") + +trace-ust-root.h: trace-ust-root.h-timestamp + @cmp $< $@ >/dev/null 2>&1 || cp $< $@ +trace-ust-root.h-timestamp: $(SRC_PATH)/trace-events $(tracetool-y) + $(call quiet-command,$(TRACETOOL) \ + --group=root \ + --format=ust-events-h \ + --backends=$(TRACE_BACKENDS) \ + $< > $@,"GEN","$(@:%-timestamp=%)") + +trace-ust-all.h: trace-ust-all.h-timestamp + @cmp $< $@ >/dev/null 2>&1 || cp $< $@ +trace-ust-all.h-timestamp: $(trace-events-files) $(tracetool-y) + $(call quiet-command,$(TRACETOOL) \ + --group=all \ + --format=ust-events-h \ + --backends=$(TRACE_BACKENDS) \ + $(trace-events-files) > $@,"GEN","$(@:%-timestamp=%)") + +trace-ust-all.c: trace-ust-all.c-timestamp + @cmp $< $@ >/dev/null 2>&1 || cp $< $@ +trace-ust-all.c-timestamp: $(trace-events-files) $(tracetool-y) + $(call quiet-command,$(TRACETOOL) \ + --group=all \ + --format=ust-events-c \ + --backends=$(TRACE_BACKENDS) \ + $(trace-events-files) > $@,"GEN","$(@:%-timestamp=%)") + +trace-dtrace-root.dtrace: trace-dtrace-root.dtrace-timestamp + @cmp $< $@ >/dev/null 2>&1 || cp $< $@ +trace-dtrace-root.dtrace-timestamp: $(SRC_PATH)/trace-events $(BUILD_DIR)/config-host.mak $(tracetool-y) + $(call quiet-command,$(TRACETOOL) \ + --group=root \ + --format=d \ + --backends=$(TRACE_BACKENDS) \ + $< > $@,"GEN","$(@:%-timestamp=%)") + +trace-dtrace-root.h: trace-dtrace-root.dtrace + $(call quiet-command,dtrace -o $@ -h -s $<, "GEN","$@") + +trace-dtrace-root.o: trace-dtrace-root.dtrace + # Don't try to regenerate Makefile or configure # We don't generate any of them Makefile: ; @@ -147,6 +258,7 @@ endif dummy := $(call unnest-vars,, \ stub-obj-y \ + chardev-obj-y \ util-obj-y \ qga-obj-y \ ivshmem-client-obj-y \ @@ -160,7 +272,8 @@ dummy := $(call unnest-vars,, \ qom-obj-y \ io-obj-y \ common-obj-y \ - common-obj-m) + common-obj-m \ + trace-obj-y) ifneq ($(wildcard config-host.mak),) include $(SRC_PATH)/tests/Makefile.include @@ -223,7 +336,8 @@ subdir-dtc:dtc/libfdt dtc/tests dtc/%: mkdir -p $@ -$(SUBDIR_RULES): libqemuutil.a libqemustub.a $(common-obj-y) $(qom-obj-y) $(crypto-aes-obj-$(CONFIG_USER_ONLY)) +$(SUBDIR_RULES): libqemuutil.a libqemustub.a $(common-obj-y) $(chardev-obj-y) \ + $(qom-obj-y) $(crypto-aes-obj-$(CONFIG_USER_ONLY)) $(trace-obj-y) ROMSUBDIR_RULES=$(patsubst %,romsubdir-%, $(ROMS)) # Only keep -O and -g cflags @@ -247,15 +361,17 @@ libqemuutil.a: $(util-obj-y) ###################################################################### +COMMON_LDADDS = $(trace-obj-y) libqemuutil.a libqemustub.a + qemu-img.o: qemu-img-cmds.h -qemu-img$(EXESUF): qemu-img.o $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) libqemuutil.a libqemustub.a -qemu-nbd$(EXESUF): qemu-nbd.o $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) libqemuutil.a libqemustub.a -qemu-io$(EXESUF): qemu-io.o $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) libqemuutil.a libqemustub.a +qemu-img$(EXESUF): qemu-img.o $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS) +qemu-nbd$(EXESUF): qemu-nbd.o $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS) +qemu-io$(EXESUF): qemu-io.o $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS) -qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o libqemuutil.a libqemustub.a +qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o $(COMMON_LDADDS) -fsdev/virtfs-proxy-helper$(EXESUF): fsdev/virtfs-proxy-helper.o fsdev/9p-marshal.o fsdev/9p-iov-marshal.o libqemuutil.a libqemustub.a +fsdev/virtfs-proxy-helper$(EXESUF): fsdev/virtfs-proxy-helper.o fsdev/9p-marshal.o fsdev/9p-iov-marshal.o $(COMMON_LDADDS) fsdev/virtfs-proxy-helper$(EXESUF): LIBS += -lcap qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx $(SRC_PATH)/scripts/hxtool @@ -320,7 +436,7 @@ $(qapi-modules) $(SRC_PATH)/scripts/qapi-introspect.py $(qapi-py) QGALIB_GEN=$(addprefix qga/qapi-generated/, qga-qapi-types.h qga-qapi-visit.h qga-qmp-commands.h) $(qga-obj-y) qemu-ga.o: $(QGALIB_GEN) -qemu-ga$(EXESUF): $(qga-obj-y) libqemuutil.a libqemustub.a +qemu-ga$(EXESUF): $(qga-obj-y) $(COMMON_LDADDS) $(call LINK, $^) ifdef QEMU_GA_MSI_ENABLED @@ -345,9 +461,9 @@ ifneq ($(EXESUF),) qemu-ga: qemu-ga$(EXESUF) $(QGA_VSS_PROVIDER) $(QEMU_GA_MSI) endif -ivshmem-client$(EXESUF): $(ivshmem-client-obj-y) libqemuutil.a libqemustub.a +ivshmem-client$(EXESUF): $(ivshmem-client-obj-y) $(COMMON_LDADDS) $(call LINK, $^) -ivshmem-server$(EXESUF): $(ivshmem-server-obj-y) libqemuutil.a libqemustub.a +ivshmem-server$(EXESUF): $(ivshmem-server-obj-y) $(COMMON_LDADDS) $(call LINK, $^) module_block.h: $(SRC_PATH)/scripts/modules/module_block.py config-host.mak @@ -664,6 +780,10 @@ ifneq ($(filter-out $(UNCHECKED_GOALS),$(MAKECMDGOALS)),$(if $(MAKECMDGOALS),,fa Makefile: $(GENERATED_HEADERS) endif +.SECONDARY: $(TRACE_HEADERS) $(TRACE_HEADERS:%=%-timestamp) \ + $(TRACE_SOURCES) $(TRACE_SOURCES:%=%-timestamp) \ + $(TRACE_DTRACE) $(TRACE_DTRACE:%=%-timestamp) + # Include automatically generated dependency files # Dependencies in Makefile.objs files come from our recursive subdir rules -include $(wildcard *.d tests/*.d) diff --git a/Makefile.objs b/Makefile.objs index 01cef866e4..431fc59264 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -4,6 +4,8 @@ stub-obj-y = stubs/ crypto/ util-obj-y = util/ qobject/ qapi/ util-obj-y += qmp-introspect.o qapi-types.o qapi-visit.o qapi-event.o +chardev-obj-y = chardev/ + ####################################################################### # block-obj-y is code used by both qemu system emulation and qemu-img @@ -51,8 +53,7 @@ common-obj-$(CONFIG_POSIX) += os-posix.o common-obj-$(CONFIG_LINUX) += fsdev/ common-obj-y += migration/ -common-obj-y += qemu-char.o #aio.o -common-obj-y += page_cache.o +common-obj-y += page_cache.o #aio.o common-obj-$(CONFIG_SPICE) += spice-qemu-char.o @@ -118,47 +119,58 @@ ivshmem-server-obj-y = contrib/ivshmem-server/ libvhost-user-obj-y = contrib/libvhost-user/ ###################################################################### -trace-events-y = trace-events -trace-events-y += util/trace-events -trace-events-y += crypto/trace-events -trace-events-y += io/trace-events -trace-events-y += migration/trace-events -trace-events-y += block/trace-events -trace-events-y += hw/block/trace-events -trace-events-y += hw/char/trace-events -trace-events-y += hw/intc/trace-events -trace-events-y += hw/net/trace-events -trace-events-y += hw/virtio/trace-events -trace-events-y += hw/audio/trace-events -trace-events-y += hw/misc/trace-events -trace-events-y += hw/usb/trace-events -trace-events-y += hw/scsi/trace-events -trace-events-y += hw/nvram/trace-events -trace-events-y += hw/display/trace-events -trace-events-y += hw/input/trace-events -trace-events-y += hw/timer/trace-events -trace-events-y += hw/dma/trace-events -trace-events-y += hw/sparc/trace-events -trace-events-y += hw/sd/trace-events -trace-events-y += hw/isa/trace-events -trace-events-y += hw/mem/trace-events -trace-events-y += hw/i386/trace-events -trace-events-y += hw/9pfs/trace-events -trace-events-y += hw/ppc/trace-events -trace-events-y += hw/pci/trace-events -trace-events-y += hw/s390x/trace-events -trace-events-y += hw/vfio/trace-events -trace-events-y += hw/acpi/trace-events -trace-events-y += hw/arm/trace-events -trace-events-y += hw/alpha/trace-events -trace-events-y += ui/trace-events -trace-events-y += audio/trace-events -trace-events-y += net/trace-events -trace-events-y += target/arm/trace-events -trace-events-y += target/i386/trace-events -trace-events-y += target/sparc/trace-events -trace-events-y += target/s390x/trace-events -trace-events-y += target/ppc/trace-events -trace-events-y += qom/trace-events -trace-events-y += linux-user/trace-events -trace-events-y += qapi/trace-events +trace-events-subdirs = +trace-events-subdirs += util +trace-events-subdirs += crypto +trace-events-subdirs += io +trace-events-subdirs += migration +trace-events-subdirs += block +trace-events-subdirs += hw/block +trace-events-subdirs += hw/block/dataplane +trace-events-subdirs += hw/char +trace-events-subdirs += hw/intc +trace-events-subdirs += hw/net +trace-events-subdirs += hw/virtio +trace-events-subdirs += hw/audio +trace-events-subdirs += hw/misc +trace-events-subdirs += hw/usb +trace-events-subdirs += hw/scsi +trace-events-subdirs += hw/nvram +trace-events-subdirs += hw/display +trace-events-subdirs += hw/input +trace-events-subdirs += hw/timer +trace-events-subdirs += hw/dma +trace-events-subdirs += hw/sparc +trace-events-subdirs += hw/sd +trace-events-subdirs += hw/isa +trace-events-subdirs += hw/mem +trace-events-subdirs += hw/i386 +trace-events-subdirs += hw/i386/xen +trace-events-subdirs += hw/9pfs +trace-events-subdirs += hw/ppc +trace-events-subdirs += hw/pci +trace-events-subdirs += hw/s390x +trace-events-subdirs += hw/vfio +trace-events-subdirs += hw/acpi +trace-events-subdirs += hw/arm +trace-events-subdirs += hw/alpha +trace-events-subdirs += hw/xen +trace-events-subdirs += ui +trace-events-subdirs += audio +trace-events-subdirs += net +trace-events-subdirs += target/arm +trace-events-subdirs += target/i386 +trace-events-subdirs += target/sparc +trace-events-subdirs += target/s390x +trace-events-subdirs += target/ppc +trace-events-subdirs += qom +trace-events-subdirs += linux-user +trace-events-subdirs += qapi + +trace-events-files = $(SRC_PATH)/trace-events $(trace-events-subdirs:%=$(SRC_PATH)/%/trace-events) + +trace-obj-y = trace-root.o +trace-obj-y += $(trace-events-subdirs:%=%/trace.o) +trace-obj-$(CONFIG_TRACE_UST) += trace-ust-all.o +trace-obj-$(CONFIG_TRACE_DTRACE) += trace-dtrace-root.o +trace-obj-$(CONFIG_TRACE_DTRACE) += $(trace-events-subdirs:%=%/trace-dtrace.o) diff --git a/Makefile.target b/Makefile.target index fa2b151caa..924304c9e6 100644 --- a/Makefile.target +++ b/Makefile.target @@ -50,6 +50,7 @@ endif $(QEMU_PROG).stp-installed: $(BUILD_DIR)/trace-events-all $(call quiet-command,$(TRACETOOL) \ + --group=all \ --format=stap \ --backends=$(TRACE_BACKENDS) \ --binary=$(bindir)/$(QEMU_PROG) \ @@ -59,6 +60,7 @@ $(QEMU_PROG).stp-installed: $(BUILD_DIR)/trace-events-all $(QEMU_PROG).stp: $(BUILD_DIR)/trace-events-all $(call quiet-command,$(TRACETOOL) \ + --group=all \ --format=stap \ --backends=$(TRACE_BACKENDS) \ --binary=$(realpath .)/$(QEMU_PROG) \ @@ -68,6 +70,7 @@ $(QEMU_PROG).stp: $(BUILD_DIR)/trace-events-all $(QEMU_PROG)-simpletrace.stp: $(BUILD_DIR)/trace-events-all $(call quiet-command,$(TRACETOOL) \ + --group=all \ --format=simpletrace-stap \ --backends=$(TRACE_BACKENDS) \ --probe-prefix=qemu.$(TARGET_TYPE).$(TARGET_NAME) \ @@ -172,31 +175,36 @@ all-obj-y := $(obj-y) target-obj-y := block-obj-y := common-obj-y := +chardev-obj-y := include $(SRC_PATH)/Makefile.objs dummy := $(call unnest-vars,,target-obj-y) target-obj-y-save := $(target-obj-y) dummy := $(call unnest-vars,.., \ block-obj-y \ block-obj-m \ + chardev-obj-y \ crypto-obj-y \ crypto-aes-obj-y \ qom-obj-y \ io-obj-y \ common-obj-y \ - common-obj-m) + common-obj-m \ + trace-obj-y) target-obj-y := $(target-obj-y-save) all-obj-y += $(common-obj-y) all-obj-y += $(target-obj-y) all-obj-y += $(qom-obj-y) -all-obj-$(CONFIG_SOFTMMU) += $(block-obj-y) +all-obj-$(CONFIG_SOFTMMU) += $(block-obj-y) $(chardev-obj-y) all-obj-$(CONFIG_USER_ONLY) += $(crypto-aes-obj-y) all-obj-$(CONFIG_SOFTMMU) += $(crypto-obj-y) all-obj-$(CONFIG_SOFTMMU) += $(io-obj-y) $(QEMU_PROG_BUILD): config-devices.mak +COMMON_LDADDS = $(trace-obj-y) ../libqemuutil.a ../libqemustub.a + # build either PROG or PROGW -$(QEMU_PROG_BUILD): $(all-obj-y) ../libqemuutil.a ../libqemustub.a +$(QEMU_PROG_BUILD): $(all-obj-y) $(COMMON_LDADDS) $(call LINK, $(filter-out %.mak, $^)) ifdef CONFIG_DARWIN $(call quiet-command,Rez -append $(SRC_PATH)/pc-bios/qemu.rsrc -o $@,"REZ","$(TARGET_DIR)$@") diff --git a/aio-posix.c b/aio-posix.c index a8d7090bd8..577527fda5 100644 --- a/aio-posix.c +++ b/aio-posix.c @@ -19,7 +19,7 @@ #include "qemu/rcu_queue.h" #include "qemu/sockets.h" #include "qemu/cutils.h" -#include "trace.h" +#include "trace-root.h" #ifdef CONFIG_EPOLL_CREATE1 #include <sys/epoll.h> #endif diff --git a/backends/baum.c b/backends/baum.c index 0f418ed358..2eddcae119 100644 --- a/backends/baum.c +++ b/backends/baum.c @@ -616,9 +616,9 @@ static void baum_chr_read(void *opaque) } } -static void baum_chr_free(Chardev *chr) +static void char_braille_finalize(Object *obj) { - BaumChardev *baum = BAUM_CHARDEV(chr); + BaumChardev *baum = BAUM_CHARDEV(obj); timer_free(baum->cellCount_timer); if (baum->brlapi) { @@ -659,23 +659,18 @@ static void char_braille_class_init(ObjectClass *oc, void *data) cc->open = baum_chr_open; cc->chr_write = baum_chr_write; cc->chr_accept_input = baum_chr_accept_input; - cc->chr_free = baum_chr_free; } static const TypeInfo char_braille_type_info = { .name = TYPE_CHARDEV_BRAILLE, .parent = TYPE_CHARDEV, .instance_size = sizeof(BaumChardev), + .instance_finalize = char_braille_finalize, .class_init = char_braille_class_init, }; static void register_types(void) { - static const CharDriver driver = { - .kind = CHARDEV_BACKEND_KIND_BRAILLE, - }; - - register_char_driver(&driver); type_register_static(&char_braille_type_info); } diff --git a/backends/msmouse.c b/backends/msmouse.c index 936a5476d5..d2c3162f1e 100644 --- a/backends/msmouse.c +++ b/backends/msmouse.c @@ -139,9 +139,9 @@ static int msmouse_chr_write(struct Chardev *s, const uint8_t *buf, int len) return len; } -static void msmouse_chr_free(struct Chardev *chr) +static void char_msmouse_finalize(Object *obj) { - MouseChardev *mouse = MOUSE_CHARDEV(chr); + MouseChardev *mouse = MOUSE_CHARDEV(obj); qemu_input_handler_unregister(mouse->hs); } @@ -172,23 +172,18 @@ static void char_msmouse_class_init(ObjectClass *oc, void *data) cc->open = msmouse_chr_open; cc->chr_write = msmouse_chr_write; cc->chr_accept_input = msmouse_chr_accept_input; - cc->chr_free = msmouse_chr_free; } static const TypeInfo char_msmouse_type_info = { .name = TYPE_CHARDEV_MSMOUSE, .parent = TYPE_CHARDEV, .instance_size = sizeof(MouseChardev), + .instance_finalize = char_msmouse_finalize, .class_init = char_msmouse_class_init, }; static void register_types(void) { - static const CharDriver driver = { - .kind = CHARDEV_BACKEND_KIND_MSMOUSE, - }; - - register_char_driver(&driver); type_register_static(&char_msmouse_type_info); } diff --git a/backends/testdev.c b/backends/testdev.c index ea15143713..7df9248a13 100644 --- a/backends/testdev.c +++ b/backends/testdev.c @@ -123,11 +123,6 @@ static const TypeInfo char_testdev_type_info = { static void register_types(void) { - static const CharDriver driver = { - .kind = CHARDEV_BACKEND_KIND_TESTDEV, - }; - - register_char_driver(&driver); type_register_static(&char_testdev_type_info); } @@ -29,7 +29,7 @@ #include "exec/cpu-common.h" #include "sysemu/kvm.h" #include "sysemu/balloon.h" -#include "trace.h" +#include "trace-root.h" #include "qmp-commands.h" #include "qapi/qmp/qerror.h" #include "qapi/qmp/qjson.h" @@ -22,7 +22,7 @@ * THE SOFTWARE. */ #include "qemu/osdep.h" -#include "trace.h" +#include "block/trace.h" #include "block/block_int.h" #include "block/blockjob.h" #include "block/nbd.h" diff --git a/block/sheepdog.c b/block/sheepdog.c index 5637e0cd37..f757157cea 100644 --- a/block/sheepdog.c +++ b/block/sheepdog.c @@ -306,6 +306,7 @@ static inline size_t count_data_objs(const struct SheepdogInode *inode) } while (0) typedef struct SheepdogAIOCB SheepdogAIOCB; +typedef struct BDRVSheepdogState BDRVSheepdogState; typedef struct AIOReq { SheepdogAIOCB *aiocb; @@ -334,7 +335,7 @@ enum AIOCBState { || y->max_affect_data_idx < x->min_affect_data_idx)) struct SheepdogAIOCB { - BlockAIOCB common; + BDRVSheepdogState *s; QEMUIOVector *qiov; @@ -345,9 +346,6 @@ struct SheepdogAIOCB { enum AIOCBState aiocb_type; Coroutine *coroutine; - void (*aio_done_func)(SheepdogAIOCB *); - - bool cancelable; int nr_pending; uint32_t min_affect_data_idx; @@ -365,7 +363,7 @@ struct SheepdogAIOCB { QLIST_ENTRY(SheepdogAIOCB) aiocb_siblings; }; -typedef struct BDRVSheepdogState { +struct BDRVSheepdogState { BlockDriverState *bs; AioContext *aio_context; @@ -392,7 +390,7 @@ typedef struct BDRVSheepdogState { CoQueue overlapping_queue; QLIST_HEAD(inflight_aiocb_head, SheepdogAIOCB) inflight_aiocb_head; -} BDRVSheepdogState; +}; typedef struct BDRVSheepdogReopenState { int fd; @@ -450,14 +448,13 @@ static const char * sd_strerror(int err) * * 1. In sd_co_rw_vector, we send the I/O requests to the server and * link the requests to the inflight_list in the - * BDRVSheepdogState. The function exits without waiting for + * BDRVSheepdogState. The function yields while waiting for * receiving the response. * * 2. We receive the response in aio_read_response, the fd handler to - * the sheepdog connection. If metadata update is needed, we send - * the write request to the vdi object in sd_write_done, the write - * completion function. We switch back to sd_co_readv/writev after - * all the requests belonging to the AIOCB are finished. + * the sheepdog connection. We switch back to sd_co_readv/sd_writev + * after all the requests belonging to the AIOCB are finished. If + * needed, sd_co_writev will send another requests for the vdi object. */ static inline AIOReq *alloc_aio_req(BDRVSheepdogState *s, SheepdogAIOCB *acb, @@ -482,94 +479,34 @@ static inline AIOReq *alloc_aio_req(BDRVSheepdogState *s, SheepdogAIOCB *acb, return aio_req; } -static inline void free_aio_req(BDRVSheepdogState *s, AIOReq *aio_req) -{ - SheepdogAIOCB *acb = aio_req->aiocb; - - acb->cancelable = false; - QLIST_REMOVE(aio_req, aio_siblings); - g_free(aio_req); - - acb->nr_pending--; -} - -static void coroutine_fn sd_finish_aiocb(SheepdogAIOCB *acb) +static void wait_for_overlapping_aiocb(BDRVSheepdogState *s, SheepdogAIOCB *acb) { - qemu_coroutine_enter(acb->coroutine); - qemu_aio_unref(acb); -} - -/* - * Check whether the specified acb can be canceled - * - * We can cancel aio when any request belonging to the acb is: - * - Not processed by the sheepdog server. - * - Not linked to the inflight queue. - */ -static bool sd_acb_cancelable(const SheepdogAIOCB *acb) -{ - BDRVSheepdogState *s = acb->common.bs->opaque; - AIOReq *aioreq; - - if (!acb->cancelable) { - return false; - } - - QLIST_FOREACH(aioreq, &s->inflight_aio_head, aio_siblings) { - if (aioreq->aiocb == acb) { - return false; - } - } - - return true; -} - -static void sd_aio_cancel(BlockAIOCB *blockacb) -{ - SheepdogAIOCB *acb = (SheepdogAIOCB *)blockacb; - BDRVSheepdogState *s = acb->common.bs->opaque; - AIOReq *aioreq, *next; - - if (sd_acb_cancelable(acb)) { - /* Remove outstanding requests from failed queue. */ - QLIST_FOREACH_SAFE(aioreq, &s->failed_aio_head, aio_siblings, - next) { - if (aioreq->aiocb == acb) { - free_aio_req(s, aioreq); - } - } + SheepdogAIOCB *cb; - assert(acb->nr_pending == 0); - if (acb->common.cb) { - acb->common.cb(acb->common.opaque, -ECANCELED); +retry: + QLIST_FOREACH(cb, &s->inflight_aiocb_head, aiocb_siblings) { + if (AIOCBOverlapping(acb, cb)) { + qemu_co_queue_wait(&s->overlapping_queue); + goto retry; } - sd_finish_aiocb(acb); } } -static const AIOCBInfo sd_aiocb_info = { - .aiocb_size = sizeof(SheepdogAIOCB), - .cancel_async = sd_aio_cancel, -}; - -static SheepdogAIOCB *sd_aio_setup(BlockDriverState *bs, QEMUIOVector *qiov, - int64_t sector_num, int nb_sectors) +static void sd_aio_setup(SheepdogAIOCB *acb, BDRVSheepdogState *s, + QEMUIOVector *qiov, int64_t sector_num, int nb_sectors, + int type) { - SheepdogAIOCB *acb; uint32_t object_size; - BDRVSheepdogState *s = bs->opaque; object_size = (UINT32_C(1) << s->inode.block_size_shift); - acb = qemu_aio_get(&sd_aiocb_info, bs, NULL, NULL); + acb->s = s; acb->qiov = qiov; acb->sector_num = sector_num; acb->nb_sectors = nb_sectors; - acb->aio_done_func = NULL; - acb->cancelable = true; acb->coroutine = qemu_coroutine_self(); acb->ret = 0; acb->nr_pending = 0; @@ -580,8 +517,14 @@ static SheepdogAIOCB *sd_aio_setup(BlockDriverState *bs, QEMUIOVector *qiov, acb->min_dirty_data_idx = UINT32_MAX; acb->max_dirty_data_idx = 0; + acb->aiocb_type = type; + + if (type == AIOCB_FLUSH_CACHE) { + return; + } - return acb; + wait_for_overlapping_aiocb(s, acb); + QLIST_INSERT_HEAD(&s->inflight_aiocb_head, acb, aiocb_siblings); } /* Return -EIO in case of error, file descriptor on success */ @@ -797,7 +740,6 @@ static coroutine_fn void reconnect_to_sdog(void *opaque) while (!QLIST_EMPTY(&s->failed_aio_head)) { aio_req = QLIST_FIRST(&s->failed_aio_head); QLIST_REMOVE(aio_req, aio_siblings); - QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); resend_aioreq(s, aio_req); } } @@ -840,9 +782,6 @@ static void coroutine_fn aio_read_response(void *opaque) switch (acb->aiocb_type) { case AIOCB_WRITE_UDATA: - /* this coroutine context is no longer suitable for co_recv - * because we may send data to update vdi objects */ - s->co_recv = NULL; if (!is_data_obj(aio_req->oid)) { break; } @@ -890,6 +829,12 @@ static void coroutine_fn aio_read_response(void *opaque) } } + /* No more data for this aio_req (reload_inode below uses its own file + * descriptor handler which doesn't use co_recv). + */ + s->co_recv = NULL; + + QLIST_REMOVE(aio_req, aio_siblings); switch (rsp.result) { case SD_RES_SUCCESS: break; @@ -907,26 +852,26 @@ static void coroutine_fn aio_read_response(void *opaque) aio_req->oid = vid_to_vdi_oid(s->inode.vdi_id); } resend_aioreq(s, aio_req); - goto out; + return; default: acb->ret = -EIO; error_report("%s", sd_strerror(rsp.result)); break; } - free_aio_req(s, aio_req); - if (!acb->nr_pending) { + g_free(aio_req); + + if (!--acb->nr_pending) { /* * We've finished all requests which belong to the AIOCB, so * we can switch back to sd_co_readv/writev now. */ - acb->aio_done_func(acb); + qemu_coroutine_enter(acb->coroutine); } -out: - s->co_recv = NULL; + return; + err: - s->co_recv = NULL; reconnect_to_sdog(opaque); } @@ -1176,6 +1121,8 @@ static void coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req, uint64_t old_oid = aio_req->base_oid; bool create = aio_req->create; + QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); + if (!nr_copies) { error_report("bug"); } @@ -2025,11 +1972,10 @@ static int sd_truncate(BlockDriverState *bs, int64_t offset) /* * This function is called after writing data objects. If we need to * update metadata, this sends a write request to the vdi object. - * Otherwise, this switches back to sd_co_readv/writev. */ static void coroutine_fn sd_write_done(SheepdogAIOCB *acb) { - BDRVSheepdogState *s = acb->common.bs->opaque; + BDRVSheepdogState *s = acb->s; struct iovec iov; AIOReq *aio_req; uint32_t offset, data_len, mn, mx; @@ -2038,6 +1984,7 @@ static void coroutine_fn sd_write_done(SheepdogAIOCB *acb) mx = acb->max_dirty_data_idx; if (mn <= mx) { /* we need to update the vdi object. */ + ++acb->nr_pending; offset = sizeof(s->inode) - sizeof(s->inode.data_vdi_id) + mn * sizeof(s->inode.data_vdi_id[0]); data_len = (mx - mn + 1) * sizeof(s->inode.data_vdi_id[0]); @@ -2049,15 +1996,11 @@ static void coroutine_fn sd_write_done(SheepdogAIOCB *acb) iov.iov_len = sizeof(s->inode); aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id), data_len, offset, 0, false, 0, offset); - QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); add_aio_request(s, aio_req, &iov, 1, AIOCB_WRITE_UDATA); - - acb->aio_done_func = sd_finish_aiocb; - acb->aiocb_type = AIOCB_WRITE_UDATA; - return; + if (--acb->nr_pending) { + qemu_coroutine_yield(); + } } - - sd_finish_aiocb(acb); } /* Delete current working VDI on the snapshot chain */ @@ -2169,16 +2112,15 @@ out: * Returns 1 when we need to wait a response, 0 when there is no sent * request and -errno in error cases. */ -static int coroutine_fn sd_co_rw_vector(void *p) +static void coroutine_fn sd_co_rw_vector(SheepdogAIOCB *acb) { - SheepdogAIOCB *acb = p; int ret = 0; unsigned long len, done = 0, total = acb->nb_sectors * BDRV_SECTOR_SIZE; unsigned long idx; uint32_t object_size; uint64_t oid; uint64_t offset; - BDRVSheepdogState *s = acb->common.bs->opaque; + BDRVSheepdogState *s = acb->s; SheepdogInode *inode = &s->inode; AIOReq *aio_req; @@ -2190,7 +2132,7 @@ static int coroutine_fn sd_co_rw_vector(void *p) ret = sd_create_branch(s); if (ret) { acb->ret = -EIO; - goto out; + return; } } @@ -2255,8 +2197,6 @@ static int coroutine_fn sd_co_rw_vector(void *p) old_oid, acb->aiocb_type == AIOCB_DISCARD_OBJ ? 0 : done); - QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); - add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov, acb->aiocb_type); done: @@ -2264,31 +2204,25 @@ static int coroutine_fn sd_co_rw_vector(void *p) idx++; done += len; } -out: - if (!--acb->nr_pending) { - return acb->ret; + if (--acb->nr_pending) { + qemu_coroutine_yield(); } - return 1; } -static bool check_overlapping_aiocb(BDRVSheepdogState *s, SheepdogAIOCB *aiocb) +static void sd_aio_complete(SheepdogAIOCB *acb) { - SheepdogAIOCB *cb; - - QLIST_FOREACH(cb, &s->inflight_aiocb_head, aiocb_siblings) { - if (AIOCBOverlapping(aiocb, cb)) { - return true; - } + if (acb->aiocb_type == AIOCB_FLUSH_CACHE) { + return; } - QLIST_INSERT_HEAD(&s->inflight_aiocb_head, aiocb, aiocb_siblings); - return false; + QLIST_REMOVE(acb, aiocb_siblings); + qemu_co_queue_restart_all(&acb->s->overlapping_queue); } static coroutine_fn int sd_co_writev(BlockDriverState *bs, int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) { - SheepdogAIOCB *acb; + SheepdogAIOCB acb; int ret; int64_t offset = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE; BDRVSheepdogState *s = bs->opaque; @@ -2300,85 +2234,50 @@ static coroutine_fn int sd_co_writev(BlockDriverState *bs, int64_t sector_num, } } - acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors); - acb->aio_done_func = sd_write_done; - acb->aiocb_type = AIOCB_WRITE_UDATA; - -retry: - if (check_overlapping_aiocb(s, acb)) { - qemu_co_queue_wait(&s->overlapping_queue); - goto retry; - } - - ret = sd_co_rw_vector(acb); - if (ret <= 0) { - QLIST_REMOVE(acb, aiocb_siblings); - qemu_co_queue_restart_all(&s->overlapping_queue); - qemu_aio_unref(acb); - return ret; - } - - qemu_coroutine_yield(); - - QLIST_REMOVE(acb, aiocb_siblings); - qemu_co_queue_restart_all(&s->overlapping_queue); + sd_aio_setup(&acb, s, qiov, sector_num, nb_sectors, AIOCB_WRITE_UDATA); + sd_co_rw_vector(&acb); + sd_write_done(&acb); + sd_aio_complete(&acb); - return acb->ret; + return acb.ret; } static coroutine_fn int sd_co_readv(BlockDriverState *bs, int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) { - SheepdogAIOCB *acb; - int ret; + SheepdogAIOCB acb; BDRVSheepdogState *s = bs->opaque; - acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors); - acb->aiocb_type = AIOCB_READ_UDATA; - acb->aio_done_func = sd_finish_aiocb; + sd_aio_setup(&acb, s, qiov, sector_num, nb_sectors, AIOCB_READ_UDATA); + sd_co_rw_vector(&acb); + sd_aio_complete(&acb); -retry: - if (check_overlapping_aiocb(s, acb)) { - qemu_co_queue_wait(&s->overlapping_queue); - goto retry; - } - - ret = sd_co_rw_vector(acb); - if (ret <= 0) { - QLIST_REMOVE(acb, aiocb_siblings); - qemu_co_queue_restart_all(&s->overlapping_queue); - qemu_aio_unref(acb); - return ret; - } - - qemu_coroutine_yield(); - - QLIST_REMOVE(acb, aiocb_siblings); - qemu_co_queue_restart_all(&s->overlapping_queue); - return acb->ret; + return acb.ret; } static int coroutine_fn sd_co_flush_to_disk(BlockDriverState *bs) { BDRVSheepdogState *s = bs->opaque; - SheepdogAIOCB *acb; + SheepdogAIOCB acb; AIOReq *aio_req; if (s->cache_flags != SD_FLAG_CMD_CACHE) { return 0; } - acb = sd_aio_setup(bs, NULL, 0, 0); - acb->aiocb_type = AIOCB_FLUSH_CACHE; - acb->aio_done_func = sd_finish_aiocb; + sd_aio_setup(&acb, s, NULL, 0, 0, AIOCB_FLUSH_CACHE); - aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id), + acb.nr_pending++; + aio_req = alloc_aio_req(s, &acb, vid_to_vdi_oid(s->inode.vdi_id), 0, 0, 0, false, 0, 0); - QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings); - add_aio_request(s, aio_req, NULL, 0, acb->aiocb_type); + add_aio_request(s, aio_req, NULL, 0, acb.aiocb_type); - qemu_coroutine_yield(); - return acb->ret; + if (--acb.nr_pending) { + qemu_coroutine_yield(); + } + + sd_aio_complete(&acb); + return acb.ret; } static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) @@ -2812,9 +2711,8 @@ static int sd_load_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, static coroutine_fn int sd_co_pdiscard(BlockDriverState *bs, int64_t offset, int count) { - SheepdogAIOCB *acb; + SheepdogAIOCB acb; BDRVSheepdogState *s = bs->opaque; - int ret; QEMUIOVector discard_iov; struct iovec iov; uint32_t zero = 0; @@ -2832,31 +2730,12 @@ static coroutine_fn int sd_co_pdiscard(BlockDriverState *bs, int64_t offset, if (!QEMU_IS_ALIGNED(offset | count, BDRV_SECTOR_SIZE)) { return -ENOTSUP; } - acb = sd_aio_setup(bs, &discard_iov, offset >> BDRV_SECTOR_BITS, - count >> BDRV_SECTOR_BITS); - acb->aiocb_type = AIOCB_DISCARD_OBJ; - acb->aio_done_func = sd_finish_aiocb; - -retry: - if (check_overlapping_aiocb(s, acb)) { - qemu_co_queue_wait(&s->overlapping_queue); - goto retry; - } - - ret = sd_co_rw_vector(acb); - if (ret <= 0) { - QLIST_REMOVE(acb, aiocb_siblings); - qemu_co_queue_restart_all(&s->overlapping_queue); - qemu_aio_unref(acb); - return ret; - } - - qemu_coroutine_yield(); - - QLIST_REMOVE(acb, aiocb_siblings); - qemu_co_queue_restart_all(&s->overlapping_queue); + sd_aio_setup(&acb, s, &discard_iov, offset >> BDRV_SECTOR_BITS, + count >> BDRV_SECTOR_BITS, AIOCB_DISCARD_OBJ); + sd_co_rw_vector(&acb); + sd_aio_complete(&acb); - return acb->ret; + return acb.ret; } static coroutine_fn int64_t diff --git a/block/trace-events b/block/trace-events index 671a6a851c..0bc5c0adf1 100644 --- a/block/trace-events +++ b/block/trace-events @@ -35,8 +35,6 @@ mirror_one_iteration(void *s, int64_t sector_num, int nb_sectors) "s %p sector_n mirror_iteration_done(void *s, int64_t sector_num, int nb_sectors, int ret) "s %p sector_num %"PRId64" nb_sectors %d ret %d" mirror_yield(void *s, int64_t cnt, int buf_free_count, int in_flight) "s %p dirty count %"PRId64" free buffers %d in_flight %d" mirror_yield_in_flight(void *s, int64_t sector_num, int in_flight) "s %p sector_num %"PRId64" in_flight %d" -mirror_yield_buf_busy(void *s, int nb_chunks, int in_flight) "s %p requested chunks %d in_flight %d" -mirror_break_buf_busy(void *s, int nb_chunks, int in_flight) "s %p requested chunks %d in_flight %d" # block/backup.c backup_do_cow_enter(void *job, int64_t start, int64_t sector_num, int nb_sectors) "job %p start %"PRId64" sector_num %"PRId64" nb_sectors %d" diff --git a/blockdev-nbd.c b/blockdev-nbd.c index 81bca1760f..7ea836b46e 100644 --- a/blockdev-nbd.c +++ b/blockdev-nbd.c @@ -16,7 +16,6 @@ #include "qapi/qmp/qerror.h" #include "sysemu/sysemu.h" #include "qmp-commands.h" -#include "trace.h" #include "block/nbd.h" #include "io/channel-socket.h" diff --git a/blockdev.c b/blockdev.c index 245e1e1d17..db82ac97e5 100644 --- a/blockdev.c +++ b/blockdev.c @@ -48,7 +48,7 @@ #include "sysemu/sysemu.h" #include "block/block_int.h" #include "qmp-commands.h" -#include "trace.h" +#include "block/trace.h" #include "sysemu/arch_init.h" #include "qemu/cutils.h" #include "qemu/help_option.h" diff --git a/blockjob.c b/blockjob.c index 513620c199..abee11bb08 100644 --- a/blockjob.c +++ b/blockjob.c @@ -25,7 +25,6 @@ #include "qemu/osdep.h" #include "qemu-common.h" -#include "trace.h" #include "block/block.h" #include "block/blockjob_int.h" #include "block/block_int.h" diff --git a/chardev/Makefile.objs b/chardev/Makefile.objs new file mode 100644 index 0000000000..1feda0f0ed --- /dev/null +++ b/chardev/Makefile.objs @@ -0,0 +1,17 @@ +chardev-obj-y += char.o +chardev-obj-$(CONFIG_WIN32) += char-console.o +chardev-obj-$(CONFIG_POSIX) += char-fd.o +chardev-obj-y += char-file.o +chardev-obj-y += char-io.o +chardev-obj-y += char-mux.o +chardev-obj-y += char-null.o +chardev-obj-$(CONFIG_POSIX) += char-parallel.o +chardev-obj-y += char-pipe.o +chardev-obj-$(CONFIG_POSIX) += char-pty.o +chardev-obj-y += char-ringbuf.o +chardev-obj-y += char-serial.o +chardev-obj-y += char-socket.o +chardev-obj-y += char-stdio.o +chardev-obj-y += char-udp.o +chardev-obj-$(CONFIG_WIN32) += char-win.o +chardev-obj-$(CONFIG_WIN32) += char-win-stdio.o diff --git a/chardev/char-console.c b/chardev/char-console.c new file mode 100644 index 0000000000..c824937fe6 --- /dev/null +++ b/chardev/char-console.c @@ -0,0 +1,53 @@ +/* + * 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. + */ +#include "qemu/osdep.h" +#include "char-win.h" + +static void qemu_chr_open_win_con(Chardev *chr, + ChardevBackend *backend, + bool *be_opened, + Error **errp) +{ + qemu_chr_open_win_file(chr, GetStdHandle(STD_OUTPUT_HANDLE)); +} + +static void char_console_class_init(ObjectClass *oc, void *data) +{ + ChardevClass *cc = CHARDEV_CLASS(oc); + + cc->open = qemu_chr_open_win_con; +} + +static const TypeInfo char_console_type_info = { + .name = TYPE_CHARDEV_CONSOLE, + .parent = TYPE_CHARDEV_WIN, + .class_init = char_console_class_init, +}; + +static void register_types(void) +{ + type_register_static(&char_console_type_info); +} + +type_init(register_types); diff --git a/chardev/char-fd.c b/chardev/char-fd.c new file mode 100644 index 0000000000..fb51ab4234 --- /dev/null +++ b/chardev/char-fd.c @@ -0,0 +1,170 @@ +/* + * 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. + */ +#include "qemu/osdep.h" +#include "qemu/sockets.h" +#include "qapi/error.h" +#include "qemu-common.h" +#include "sysemu/char.h" +#include "io/channel-file.h" + +#include "char-fd.h" +#include "char-io.h" + +/* Called with chr_write_lock held. */ +static int fd_chr_write(Chardev *chr, const uint8_t *buf, int len) +{ + FDChardev *s = FD_CHARDEV(chr); + + return io_channel_send(s->ioc_out, buf, len); +} + +static gboolean fd_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque) +{ + Chardev *chr = CHARDEV(opaque); + FDChardev *s = FD_CHARDEV(opaque); + int len; + uint8_t buf[CHR_READ_BUF_LEN]; + ssize_t ret; + + len = sizeof(buf); + if (len > s->max_size) { + len = s->max_size; + } + if (len == 0) { + return TRUE; + } + + ret = qio_channel_read( + chan, (gchar *)buf, len, NULL); + if (ret == 0) { + remove_fd_in_watch(chr); + qemu_chr_be_event(chr, CHR_EVENT_CLOSED); + return FALSE; + } + if (ret > 0) { + qemu_chr_be_write(chr, buf, ret); + } + + return TRUE; +} + +static int fd_chr_read_poll(void *opaque) +{ + Chardev *chr = CHARDEV(opaque); + FDChardev *s = FD_CHARDEV(opaque); + + s->max_size = qemu_chr_be_can_write(chr); + return s->max_size; +} + +static GSource *fd_chr_add_watch(Chardev *chr, GIOCondition cond) +{ + FDChardev *s = FD_CHARDEV(chr); + return qio_channel_create_watch(s->ioc_out, cond); +} + +static void fd_chr_update_read_handler(Chardev *chr, + GMainContext *context) +{ + FDChardev *s = FD_CHARDEV(chr); + + remove_fd_in_watch(chr); + if (s->ioc_in) { + chr->fd_in_tag = io_add_watch_poll(chr, s->ioc_in, + fd_chr_read_poll, + fd_chr_read, chr, + context); + } +} + +static void char_fd_finalize(Object *obj) +{ + Chardev *chr = CHARDEV(obj); + FDChardev *s = FD_CHARDEV(obj); + + remove_fd_in_watch(chr); + if (s->ioc_in) { + object_unref(OBJECT(s->ioc_in)); + } + if (s->ioc_out) { + object_unref(OBJECT(s->ioc_out)); + } + + qemu_chr_be_event(chr, CHR_EVENT_CLOSED); +} + +int qmp_chardev_open_file_source(char *src, int flags, Error **errp) +{ + int fd = -1; + + TFR(fd = qemu_open(src, flags, 0666)); + if (fd == -1) { + error_setg_file_open(errp, errno, src); + } + return fd; +} + +/* open a character device to a unix fd */ +void qemu_chr_open_fd(Chardev *chr, + int fd_in, int fd_out) +{ + FDChardev *s = FD_CHARDEV(chr); + char *name; + + s->ioc_in = QIO_CHANNEL(qio_channel_file_new_fd(fd_in)); + name = g_strdup_printf("chardev-file-in-%s", chr->label); + qio_channel_set_name(QIO_CHANNEL(s->ioc_in), name); + g_free(name); + s->ioc_out = QIO_CHANNEL(qio_channel_file_new_fd(fd_out)); + name = g_strdup_printf("chardev-file-out-%s", chr->label); + qio_channel_set_name(QIO_CHANNEL(s->ioc_out), name); + g_free(name); + qemu_set_nonblock(fd_out); + s->chr = chr; +} + +static void char_fd_class_init(ObjectClass *oc, void *data) +{ + ChardevClass *cc = CHARDEV_CLASS(oc); + + cc->chr_add_watch = fd_chr_add_watch; + cc->chr_write = fd_chr_write; + cc->chr_update_read_handler = fd_chr_update_read_handler; +} + +static const TypeInfo char_fd_type_info = { + .name = TYPE_CHARDEV_FD, + .parent = TYPE_CHARDEV, + .instance_size = sizeof(FDChardev), + .instance_finalize = char_fd_finalize, + .class_init = char_fd_class_init, + .abstract = true, +}; + +static void register_types(void) +{ + type_register_static(&char_fd_type_info); +} + +type_init(register_types); diff --git a/chardev/char-fd.h b/chardev/char-fd.h new file mode 100644 index 0000000000..d8327982fb --- /dev/null +++ b/chardev/char-fd.h @@ -0,0 +1,44 @@ +/* + * 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. + */ +#ifndef CHAR_FD_H +#define CHAR_FD_H + +#include "io/channel.h" +#include "sysemu/char.h" + +typedef struct FDChardev { + Chardev parent; + Chardev *chr; + QIOChannel *ioc_in, *ioc_out; + int max_size; +} FDChardev; + +#define TYPE_CHARDEV_FD "chardev-fd" + +#define FD_CHARDEV(obj) OBJECT_CHECK(FDChardev, (obj), TYPE_CHARDEV_FD) + +void qemu_chr_open_fd(Chardev *chr, int fd_in, int fd_out); +int qmp_chardev_open_file_source(char *src, int flags, Error **errp); + +#endif /* CHAR_FD_H */ diff --git a/chardev/char-file.c b/chardev/char-file.c new file mode 100644 index 0000000000..8bae25350d --- /dev/null +++ b/chardev/char-file.c @@ -0,0 +1,139 @@ +/* + * 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. + */ +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu-common.h" +#include "sysemu/char.h" + +#ifdef _WIN32 +#include "char-win.h" +#else +#include "char-fd.h" +#endif + +static void qmp_chardev_open_file(Chardev *chr, + ChardevBackend *backend, + bool *be_opened, + Error **errp) +{ + ChardevFile *file = backend->u.file.data; +#ifdef _WIN32 + HANDLE out; + DWORD accessmode; + DWORD flags; + + if (file->has_in) { + error_setg(errp, "input file not supported"); + return; + } + + if (file->has_append && file->append) { + /* Append to file if it already exists. */ + accessmode = FILE_GENERIC_WRITE & ~FILE_WRITE_DATA; + flags = OPEN_ALWAYS; + } else { + /* Truncate file if it already exists. */ + accessmode = GENERIC_WRITE; + flags = CREATE_ALWAYS; + } + + out = CreateFile(file->out, accessmode, FILE_SHARE_READ, NULL, flags, + FILE_ATTRIBUTE_NORMAL, NULL); + if (out == INVALID_HANDLE_VALUE) { + error_setg(errp, "open %s failed", file->out); + return; + } + + qemu_chr_open_win_file(chr, out); +#else + int flags, in = -1, out; + + flags = O_WRONLY | O_CREAT | O_BINARY; + if (file->has_append && file->append) { + flags |= O_APPEND; + } else { + flags |= O_TRUNC; + } + + out = qmp_chardev_open_file_source(file->out, flags, errp); + if (out < 0) { + return; + } + + if (file->has_in) { + flags = O_RDONLY; + in = qmp_chardev_open_file_source(file->in, flags, errp); + if (in < 0) { + qemu_close(out); + return; + } + } + + qemu_chr_open_fd(chr, in, out); +#endif +} + +static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend, + Error **errp) +{ + const char *path = qemu_opt_get(opts, "path"); + ChardevFile *file; + + backend->type = CHARDEV_BACKEND_KIND_FILE; + if (path == NULL) { + error_setg(errp, "chardev: file: no filename given"); + return; + } + file = backend->u.file.data = g_new0(ChardevFile, 1); + qemu_chr_parse_common(opts, qapi_ChardevFile_base(file)); + file->out = g_strdup(path); + + file->has_append = true; + file->append = qemu_opt_get_bool(opts, "append", false); +} + +static void char_file_class_init(ObjectClass *oc, void *data) +{ + ChardevClass *cc = CHARDEV_CLASS(oc); + + cc->parse = qemu_chr_parse_file_out; + cc->open = qmp_chardev_open_file; +} + +static const TypeInfo char_file_type_info = { + .name = TYPE_CHARDEV_FILE, +#ifdef _WIN32 + .parent = TYPE_CHARDEV_WIN, +#else + .parent = TYPE_CHARDEV_FD, +#endif + .class_init = char_file_class_init, +}; + +static void register_types(void) +{ + type_register_static(&char_file_type_info); +} + +type_init(register_types); diff --git a/chardev/char-io.c b/chardev/char-io.c new file mode 100644 index 0000000000..7dfc3f22ba --- /dev/null +++ b/chardev/char-io.c @@ -0,0 +1,192 @@ +/* + * 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. + */ +#include "qemu/osdep.h" +#include "char-io.h" + +typedef struct IOWatchPoll { + GSource parent; + + QIOChannel *ioc; + GSource *src; + + IOCanReadHandler *fd_can_read; + GSourceFunc fd_read; + void *opaque; + GMainContext *context; +} IOWatchPoll; + +static IOWatchPoll *io_watch_poll_from_source(GSource *source) +{ + return container_of(source, IOWatchPoll, parent); +} + +static gboolean io_watch_poll_prepare(GSource *source, + gint *timeout) +{ + IOWatchPoll *iwp = io_watch_poll_from_source(source); + bool now_active = iwp->fd_can_read(iwp->opaque) > 0; + bool was_active = iwp->src != NULL; + if (was_active == now_active) { + return FALSE; + } + + if (now_active) { + iwp->src = qio_channel_create_watch( + iwp->ioc, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL); + g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL); + g_source_attach(iwp->src, iwp->context); + } else { + g_source_destroy(iwp->src); + g_source_unref(iwp->src); + iwp->src = NULL; + } + return FALSE; +} + +static gboolean io_watch_poll_check(GSource *source) +{ + return FALSE; +} + +static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback, + gpointer user_data) +{ + abort(); +} + +static void io_watch_poll_finalize(GSource *source) +{ + /* Due to a glib bug, removing the last reference to a source + * inside a finalize callback causes recursive locking (and a + * deadlock). This is not a problem inside other callbacks, + * including dispatch callbacks, so we call io_remove_watch_poll + * to remove this source. At this point, iwp->src must + * be NULL, or we would leak it. + * + * This would be solved much more elegantly by child sources, + * but we support older glib versions that do not have them. + */ + IOWatchPoll *iwp = io_watch_poll_from_source(source); + assert(iwp->src == NULL); +} + +static GSourceFuncs io_watch_poll_funcs = { + .prepare = io_watch_poll_prepare, + .check = io_watch_poll_check, + .dispatch = io_watch_poll_dispatch, + .finalize = io_watch_poll_finalize, +}; + +guint io_add_watch_poll(Chardev *chr, + QIOChannel *ioc, + IOCanReadHandler *fd_can_read, + QIOChannelFunc fd_read, + gpointer user_data, + GMainContext *context) +{ + IOWatchPoll *iwp; + int tag; + char *name; + + iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, + sizeof(IOWatchPoll)); + iwp->fd_can_read = fd_can_read; + iwp->opaque = user_data; + iwp->ioc = ioc; + iwp->fd_read = (GSourceFunc) fd_read; + iwp->src = NULL; + iwp->context = context; + + name = g_strdup_printf("chardev-iowatch-%s", chr->label); + g_source_set_name((GSource *)iwp, name); + g_free(name); + + tag = g_source_attach(&iwp->parent, context); + g_source_unref(&iwp->parent); + return tag; +} + +static void io_remove_watch_poll(guint tag) +{ + GSource *source; + IOWatchPoll *iwp; + + g_return_if_fail(tag > 0); + + source = g_main_context_find_source_by_id(NULL, tag); + g_return_if_fail(source != NULL); + + iwp = io_watch_poll_from_source(source); + if (iwp->src) { + g_source_destroy(iwp->src); + g_source_unref(iwp->src); + iwp->src = NULL; + } + g_source_destroy(&iwp->parent); +} + +void remove_fd_in_watch(Chardev *chr) +{ + if (chr->fd_in_tag) { + io_remove_watch_poll(chr->fd_in_tag); + chr->fd_in_tag = 0; + } +} + +int io_channel_send_full(QIOChannel *ioc, + const void *buf, size_t len, + int *fds, size_t nfds) +{ + size_t offset = 0; + + while (offset < len) { + ssize_t ret = 0; + struct iovec iov = { .iov_base = (char *)buf + offset, + .iov_len = len - offset }; + + ret = qio_channel_writev_full( + ioc, &iov, 1, + fds, nfds, NULL); + if (ret == QIO_CHANNEL_ERR_BLOCK) { + if (offset) { + return offset; + } + + errno = EAGAIN; + return -1; + } else if (ret < 0) { + errno = EINVAL; + return -1; + } + + offset += ret; + } + + return offset; +} + +int io_channel_send(QIOChannel *ioc, const void *buf, size_t len) +{ + return io_channel_send_full(ioc, buf, len, NULL, 0); +} diff --git a/chardev/char-io.h b/chardev/char-io.h new file mode 100644 index 0000000000..d7ae5f1585 --- /dev/null +++ b/chardev/char-io.h @@ -0,0 +1,46 @@ +/* + * 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. + */ +#ifndef CHAR_IO_H +#define CHAR_IO_H + +#include "qemu-common.h" +#include "io/channel.h" +#include "sysemu/char.h" + +/* Can only be used for read */ +guint io_add_watch_poll(Chardev *chr, + QIOChannel *ioc, + IOCanReadHandler *fd_can_read, + QIOChannelFunc fd_read, + gpointer user_data, + GMainContext *context); + +void remove_fd_in_watch(Chardev *chr); + +int io_channel_send(QIOChannel *ioc, const void *buf, size_t len); + +int io_channel_send_full(QIOChannel *ioc, const void *buf, size_t len, + int *fds, size_t nfds); + +#endif /* CHAR_IO_H */ diff --git a/chardev/char-mux.c b/chardev/char-mux.c new file mode 100644 index 0000000000..5547a36a0a --- /dev/null +++ b/chardev/char-mux.c @@ -0,0 +1,358 @@ +/* + * 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. + */ +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu-common.h" +#include "sysemu/char.h" +#include "sysemu/block-backend.h" +#include "char-mux.h" + +/* MUX driver for serial I/O splitting */ + +/* Called with chr_write_lock held. */ +static int mux_chr_write(Chardev *chr, const uint8_t *buf, int len) +{ + MuxChardev *d = MUX_CHARDEV(chr); + int ret; + if (!d->timestamps) { + ret = qemu_chr_fe_write(&d->chr, buf, len); + } else { + int i; + + ret = 0; + for (i = 0; i < len; i++) { + if (d->linestart) { + char buf1[64]; + int64_t ti; + int secs; + + ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); + if (d->timestamps_start == -1) { + d->timestamps_start = ti; + } + ti -= d->timestamps_start; + secs = ti / 1000; + snprintf(buf1, sizeof(buf1), + "[%02d:%02d:%02d.%03d] ", + secs / 3600, + (secs / 60) % 60, + secs % 60, + (int)(ti % 1000)); + /* XXX this blocks entire thread. Rewrite to use + * qemu_chr_fe_write and background I/O callbacks */ + qemu_chr_fe_write_all(&d->chr, + (uint8_t *)buf1, strlen(buf1)); + d->linestart = 0; + } + ret += qemu_chr_fe_write(&d->chr, buf + i, 1); + if (buf[i] == '\n') { + d->linestart = 1; + } + } + } + return ret; +} + +static const char * const mux_help[] = { + "% h print this help\n\r", + "% x exit emulator\n\r", + "% s save disk data back to file (if -snapshot)\n\r", + "% t toggle console timestamps\n\r", + "% b send break (magic sysrq)\n\r", + "% c switch between console and monitor\n\r", + "% % sends %\n\r", + NULL +}; + +int term_escape_char = 0x01; /* ctrl-a is used for escape */ +static void mux_print_help(Chardev *chr) +{ + int i, j; + char ebuf[15] = "Escape-Char"; + char cbuf[50] = "\n\r"; + + if (term_escape_char > 0 && term_escape_char < 26) { + snprintf(cbuf, sizeof(cbuf), "\n\r"); + snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a'); + } else { + snprintf(cbuf, sizeof(cbuf), + "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", + term_escape_char); + } + /* XXX this blocks entire thread. Rewrite to use + * qemu_chr_fe_write and background I/O callbacks */ + qemu_chr_write_all(chr, (uint8_t *)cbuf, strlen(cbuf)); + for (i = 0; mux_help[i] != NULL; i++) { + for (j = 0; mux_help[i][j] != '\0'; j++) { + if (mux_help[i][j] == '%') { + qemu_chr_write_all(chr, (uint8_t *)ebuf, strlen(ebuf)); + } else { + qemu_chr_write_all(chr, (uint8_t *)&mux_help[i][j], 1); + } + } + } +} + +void mux_chr_send_event(MuxChardev *d, int mux_nr, int event) +{ + CharBackend *be = d->backends[mux_nr]; + + if (be && be->chr_event) { + be->chr_event(be->opaque, event); + } +} + +static int mux_proc_byte(Chardev *chr, MuxChardev *d, int ch) +{ + if (d->term_got_escape) { + d->term_got_escape = 0; + if (ch == term_escape_char) { + goto send_char; + } + switch (ch) { + case '?': + case 'h': + mux_print_help(chr); + break; + case 'x': + { + const char *term = "QEMU: Terminated\n\r"; + qemu_chr_write_all(chr, (uint8_t *)term, strlen(term)); + exit(0); + break; + } + case 's': + blk_commit_all(); + break; + case 'b': + qemu_chr_be_event(chr, CHR_EVENT_BREAK); + break; + case 'c': + assert(d->mux_cnt > 0); /* handler registered with first fe */ + /* Switch to the next registered device */ + mux_set_focus(chr, (d->focus + 1) % d->mux_cnt); + break; + case 't': + d->timestamps = !d->timestamps; + d->timestamps_start = -1; + d->linestart = 0; + break; + } + } else if (ch == term_escape_char) { + d->term_got_escape = 1; + } else { + send_char: + return 1; + } + return 0; +} + +static void mux_chr_accept_input(Chardev *chr) +{ + MuxChardev *d = MUX_CHARDEV(chr); + int m = d->focus; + CharBackend *be = d->backends[m]; + + while (be && d->prod[m] != d->cons[m] && + be->chr_can_read && be->chr_can_read(be->opaque)) { + be->chr_read(be->opaque, + &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1); + } +} + +static int mux_chr_can_read(void *opaque) +{ + MuxChardev *d = MUX_CHARDEV(opaque); + int m = d->focus; + CharBackend *be = d->backends[m]; + + if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE) { + return 1; + } + + if (be && be->chr_can_read) { + return be->chr_can_read(be->opaque); + } + + return 0; +} + +static void mux_chr_read(void *opaque, const uint8_t *buf, int size) +{ + Chardev *chr = CHARDEV(opaque); + MuxChardev *d = MUX_CHARDEV(opaque); + int m = d->focus; + CharBackend *be = d->backends[m]; + int i; + + mux_chr_accept_input(opaque); + + for (i = 0; i < size; i++) + if (mux_proc_byte(chr, d, buf[i])) { + if (d->prod[m] == d->cons[m] && + be && be->chr_can_read && + be->chr_can_read(be->opaque)) { + be->chr_read(be->opaque, &buf[i], 1); + } else { + d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i]; + } + } +} + +bool muxes_realized; + +static void mux_chr_event(void *opaque, int event) +{ + MuxChardev *d = MUX_CHARDEV(opaque); + int i; + + if (!muxes_realized) { + return; + } + + /* Send the event to all registered listeners */ + for (i = 0; i < d->mux_cnt; i++) { + mux_chr_send_event(d, i, event); + } +} + +static GSource *mux_chr_add_watch(Chardev *s, GIOCondition cond) +{ + MuxChardev *d = MUX_CHARDEV(s); + Chardev *chr = qemu_chr_fe_get_driver(&d->chr); + ChardevClass *cc = CHARDEV_GET_CLASS(chr); + + if (!cc->chr_add_watch) { + return NULL; + } + + return cc->chr_add_watch(chr, cond); +} + +static void char_mux_finalize(Object *obj) +{ + MuxChardev *d = MUX_CHARDEV(obj); + int i; + + for (i = 0; i < d->mux_cnt; i++) { + CharBackend *be = d->backends[i]; + if (be) { + be->chr = NULL; + } + } + qemu_chr_fe_deinit(&d->chr); +} + +void mux_chr_set_handlers(Chardev *chr, GMainContext *context) +{ + MuxChardev *d = MUX_CHARDEV(chr); + + /* Fix up the real driver with mux routines */ + qemu_chr_fe_set_handlers(&d->chr, + mux_chr_can_read, + mux_chr_read, + mux_chr_event, + chr, + context, true); +} + +void mux_set_focus(Chardev *chr, int focus) +{ + MuxChardev *d = MUX_CHARDEV(chr); + + assert(focus >= 0); + assert(focus < d->mux_cnt); + + if (d->focus != -1) { + mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); + } + + d->focus = focus; + mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); +} + +static void qemu_chr_open_mux(Chardev *chr, + ChardevBackend *backend, + bool *be_opened, + Error **errp) +{ + ChardevMux *mux = backend->u.mux.data; + Chardev *drv; + MuxChardev *d = MUX_CHARDEV(chr); + + drv = qemu_chr_find(mux->chardev); + if (drv == NULL) { + error_setg(errp, "mux: base chardev %s not found", mux->chardev); + return; + } + + d->focus = -1; + /* only default to opened state if we've realized the initial + * set of muxes + */ + *be_opened = muxes_realized; + qemu_chr_fe_init(&d->chr, drv, errp); +} + +static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend, + Error **errp) +{ + const char *chardev = qemu_opt_get(opts, "chardev"); + ChardevMux *mux; + + if (chardev == NULL) { + error_setg(errp, "chardev: mux: no chardev given"); + return; + } + backend->type = CHARDEV_BACKEND_KIND_MUX; + mux = backend->u.mux.data = g_new0(ChardevMux, 1); + qemu_chr_parse_common(opts, qapi_ChardevMux_base(mux)); + mux->chardev = g_strdup(chardev); +} + +static void char_mux_class_init(ObjectClass *oc, void *data) +{ + ChardevClass *cc = CHARDEV_CLASS(oc); + + cc->parse = qemu_chr_parse_mux; + cc->open = qemu_chr_open_mux; + cc->chr_write = mux_chr_write; + cc->chr_accept_input = mux_chr_accept_input; + cc->chr_add_watch = mux_chr_add_watch; +} + +static const TypeInfo char_mux_type_info = { + .name = TYPE_CHARDEV_MUX, + .parent = TYPE_CHARDEV, + .class_init = char_mux_class_init, + .instance_size = sizeof(MuxChardev), + .instance_finalize = char_mux_finalize, +}; + +static void register_types(void) +{ + type_register_static(&char_mux_type_info); +} + +type_init(register_types); diff --git a/chardev/char-mux.h b/chardev/char-mux.h new file mode 100644 index 0000000000..9a2fffce91 --- /dev/null +++ b/chardev/char-mux.h @@ -0,0 +1,63 @@ +/* + * 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. + */ +#ifndef CHAR_MUX_H +#define CHAR_MUX_H + +#include "sysemu/char.h" + +extern bool muxes_realized; + +#define MAX_MUX 4 +#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ +#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) +typedef struct MuxChardev { + Chardev parent; + CharBackend *backends[MAX_MUX]; + CharBackend chr; + int focus; + int mux_cnt; + int term_got_escape; + int max_size; + /* Intermediate input buffer catches escape sequences even if the + currently active device is not accepting any input - but only until it + is full as well. */ + unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE]; + int prod[MAX_MUX]; + int cons[MAX_MUX]; + int timestamps; + + /* Protected by the Chardev chr_write_lock. */ + int linestart; + int64_t timestamps_start; +} MuxChardev; + +#define MUX_CHARDEV(obj) OBJECT_CHECK(MuxChardev, (obj), TYPE_CHARDEV_MUX) +#define CHARDEV_IS_MUX(chr) \ + object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_MUX) + +void mux_chr_set_handlers(Chardev *chr, GMainContext *context); +void mux_set_focus(Chardev *chr, int focus); +void mux_chr_send_event(MuxChardev *d, int mux_nr, int event); + +#endif /* CHAR_MUX_H */ diff --git a/chardev/char-null.c b/chardev/char-null.c new file mode 100644 index 0000000000..dc0d68ab2d --- /dev/null +++ b/chardev/char-null.c @@ -0,0 +1,54 @@ +/* + * 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. + */ +#include "qemu/osdep.h" +#include "sysemu/char.h" + +static void null_chr_open(Chardev *chr, + ChardevBackend *backend, + bool *be_opened, + Error **errp) +{ + *be_opened = false; +} + +static void char_null_class_init(ObjectClass *oc, void *data) +{ + ChardevClass *cc = CHARDEV_CLASS(oc); + + cc->open = null_chr_open; +} + +static const TypeInfo char_null_type_info = { + .name = TYPE_CHARDEV_NULL, + .parent = TYPE_CHARDEV, + .instance_size = sizeof(Chardev), + .class_init = char_null_class_init, +}; + +static void register_types(void) +{ + type_register_static(&char_null_type_info); +} + +type_init(register_types); diff --git a/chardev/char-parallel.c b/chardev/char-parallel.c new file mode 100644 index 0000000000..3fa22ce29d --- /dev/null +++ b/chardev/char-parallel.c @@ -0,0 +1,316 @@ +/* + * 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. + */ +#include "qemu/osdep.h" +#include "sysemu/char.h" +#include "qapi/error.h" +#include <sys/ioctl.h> + +#ifdef CONFIG_BSD +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) +#include <dev/ppbus/ppi.h> +#include <dev/ppbus/ppbconf.h> +#elif defined(__DragonFly__) +#include <dev/misc/ppi/ppi.h> +#include <bus/ppbus/ppbconf.h> +#endif +#else +#ifdef __linux__ +#include <linux/ppdev.h> +#include <linux/parport.h> +#endif +#endif + +#include "char-fd.h" +#include "char-parallel.h" + +#if defined(__linux__) + +typedef struct { + Chardev parent; + int fd; + int mode; +} ParallelChardev; + +#define PARALLEL_CHARDEV(obj) \ + OBJECT_CHECK(ParallelChardev, (obj), TYPE_CHARDEV_PARALLEL) + +static int pp_hw_mode(ParallelChardev *s, uint16_t mode) +{ + if (s->mode != mode) { + int m = mode; + if (ioctl(s->fd, PPSETMODE, &m) < 0) { + return 0; + } + s->mode = mode; + } + return 1; +} + +static int pp_ioctl(Chardev *chr, int cmd, void *arg) +{ + ParallelChardev *drv = PARALLEL_CHARDEV(chr); + int fd = drv->fd; + uint8_t b; + + switch (cmd) { + case CHR_IOCTL_PP_READ_DATA: + if (ioctl(fd, PPRDATA, &b) < 0) { + return -ENOTSUP; + } + *(uint8_t *)arg = b; + break; + case CHR_IOCTL_PP_WRITE_DATA: + b = *(uint8_t *)arg; + if (ioctl(fd, PPWDATA, &b) < 0) { + return -ENOTSUP; + } + break; + case CHR_IOCTL_PP_READ_CONTROL: + if (ioctl(fd, PPRCONTROL, &b) < 0) { + return -ENOTSUP; + } + /* Linux gives only the lowest bits, and no way to know data + direction! For better compatibility set the fixed upper + bits. */ + *(uint8_t *)arg = b | 0xc0; + break; + case CHR_IOCTL_PP_WRITE_CONTROL: + b = *(uint8_t *)arg; + if (ioctl(fd, PPWCONTROL, &b) < 0) { + return -ENOTSUP; + } + break; + case CHR_IOCTL_PP_READ_STATUS: + if (ioctl(fd, PPRSTATUS, &b) < 0) { + return -ENOTSUP; + } + *(uint8_t *)arg = b; + break; + case CHR_IOCTL_PP_DATA_DIR: + if (ioctl(fd, PPDATADIR, (int *)arg) < 0) { + return -ENOTSUP; + } + break; + case CHR_IOCTL_PP_EPP_READ_ADDR: + if (pp_hw_mode(drv, IEEE1284_MODE_EPP | IEEE1284_ADDR)) { + struct ParallelIOArg *parg = arg; + int n = read(fd, parg->buffer, parg->count); + if (n != parg->count) { + return -EIO; + } + } + break; + case CHR_IOCTL_PP_EPP_READ: + if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { + struct ParallelIOArg *parg = arg; + int n = read(fd, parg->buffer, parg->count); + if (n != parg->count) { + return -EIO; + } + } + break; + case CHR_IOCTL_PP_EPP_WRITE_ADDR: + if (pp_hw_mode(drv, IEEE1284_MODE_EPP | IEEE1284_ADDR)) { + struct ParallelIOArg *parg = arg; + int n = write(fd, parg->buffer, parg->count); + if (n != parg->count) { + return -EIO; + } + } + break; + case CHR_IOCTL_PP_EPP_WRITE: + if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { + struct ParallelIOArg *parg = arg; + int n = write(fd, parg->buffer, parg->count); + if (n != parg->count) { + return -EIO; + } + } + break; + default: + return -ENOTSUP; + } + return 0; +} + +static void qemu_chr_open_pp_fd(Chardev *chr, + int fd, + bool *be_opened, + Error **errp) +{ + ParallelChardev *drv = PARALLEL_CHARDEV(chr); + + if (ioctl(fd, PPCLAIM) < 0) { + error_setg_errno(errp, errno, "not a parallel port"); + close(fd); + return; + } + + drv->fd = fd; + drv->mode = IEEE1284_MODE_COMPAT; +} +#endif /* __linux__ */ + +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) + +typedef struct { + Chardev parent; + int fd; +} ParallelChardev; + +#define PARALLEL_CHARDEV(obj) \ + OBJECT_CHECK(ParallelChardev, (obj), TYPE_CHARDEV_PARALLEL) + +static int pp_ioctl(Chardev *chr, int cmd, void *arg) +{ + ParallelChardev *drv = PARALLEL_CHARDEV(chr); + uint8_t b; + + switch (cmd) { + case CHR_IOCTL_PP_READ_DATA: + if (ioctl(drv->fd, PPIGDATA, &b) < 0) { + return -ENOTSUP; + } + *(uint8_t *)arg = b; + break; + case CHR_IOCTL_PP_WRITE_DATA: + b = *(uint8_t *)arg; + if (ioctl(drv->fd, PPISDATA, &b) < 0) { + return -ENOTSUP; + } + break; + case CHR_IOCTL_PP_READ_CONTROL: + if (ioctl(drv->fd, PPIGCTRL, &b) < 0) { + return -ENOTSUP; + } + *(uint8_t *)arg = b; + break; + case CHR_IOCTL_PP_WRITE_CONTROL: + b = *(uint8_t *)arg; + if (ioctl(drv->fd, PPISCTRL, &b) < 0) { + return -ENOTSUP; + } + break; + case CHR_IOCTL_PP_READ_STATUS: + if (ioctl(drv->fd, PPIGSTATUS, &b) < 0) { + return -ENOTSUP; + } + *(uint8_t *)arg = b; + break; + default: + return -ENOTSUP; + } + return 0; +} + +static void qemu_chr_open_pp_fd(Chardev *chr, + int fd, + bool *be_opened, + Error **errp) +{ + ParallelChardev *drv = PARALLEL_CHARDEV(chr); + drv->fd = fd; + *be_opened = false; +} +#endif + +#ifdef HAVE_CHARDEV_PARPORT +static void qmp_chardev_open_parallel(Chardev *chr, + ChardevBackend *backend, + bool *be_opened, + Error **errp) +{ + ChardevHostdev *parallel = backend->u.parallel.data; + int fd; + + fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp); + if (fd < 0) { + return; + } + qemu_chr_open_pp_fd(chr, fd, be_opened, errp); +} + +static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend, + Error **errp) +{ + const char *device = qemu_opt_get(opts, "path"); + ChardevHostdev *parallel; + + if (device == NULL) { + error_setg(errp, "chardev: parallel: no device path given"); + return; + } + backend->type = CHARDEV_BACKEND_KIND_PARALLEL; + parallel = backend->u.parallel.data = g_new0(ChardevHostdev, 1); + qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(parallel)); + parallel->device = g_strdup(device); +} + +static void char_parallel_class_init(ObjectClass *oc, void *data) +{ + ChardevClass *cc = CHARDEV_CLASS(oc); + + cc->parse = qemu_chr_parse_parallel; + cc->open = qmp_chardev_open_parallel; +#if defined(__linux__) + cc->chr_ioctl = pp_ioctl; +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ + defined(__DragonFly__) + cc->chr_ioctl = pp_ioctl; +#endif +} + +static void char_parallel_finalize(Object *obj) +{ +#if defined(__linux__) + Chardev *chr = CHARDEV(obj); + ParallelChardev *drv = PARALLEL_CHARDEV(chr); + int fd = drv->fd; + + pp_hw_mode(drv, IEEE1284_MODE_COMPAT); + ioctl(fd, PPRELEASE); + close(fd); + qemu_chr_be_event(chr, CHR_EVENT_CLOSED); +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ + defined(__DragonFly__) + /* FIXME: close fd? */ +#endif +} + +static const TypeInfo char_parallel_type_info = { + .name = TYPE_CHARDEV_PARALLEL, + .parent = TYPE_CHARDEV, + .instance_size = sizeof(ParallelChardev), + .instance_finalize = char_parallel_finalize, + .class_init = char_parallel_class_init, +}; + +static void register_types(void) +{ + type_register_static(&char_parallel_type_info); +} + +type_init(register_types); + +#endif diff --git a/chardev/char-parallel.h b/chardev/char-parallel.h new file mode 100644 index 0000000000..26742f9d5c --- /dev/null +++ b/chardev/char-parallel.h @@ -0,0 +1,32 @@ +/* + * 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. + */ +#ifndef CHAR_PARALLEL_H +#define CHAR_PARALLEL_H + +#if defined(__linux__) || defined(__FreeBSD__) || \ + defined(__FreeBSD_kernel__) || defined(__DragonFly__) +#define HAVE_CHARDEV_PARPORT 1 +#endif + +#endif /* CHAR_PARALLEL_H */ diff --git a/chardev/char-pipe.c b/chardev/char-pipe.c new file mode 100644 index 0000000000..54240c863d --- /dev/null +++ b/chardev/char-pipe.c @@ -0,0 +1,191 @@ +/* + * 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. + */ +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "sysemu/char.h" + +#ifdef _WIN32 +#include "char-win.h" +#else +#include "char-fd.h" +#endif + +#ifdef _WIN32 +#define MAXCONNECT 1 +#define NTIMEOUT 5000 + +static int win_chr_pipe_init(Chardev *chr, const char *filename, + Error **errp) +{ + WinChardev *s = WIN_CHARDEV(chr); + OVERLAPPED ov; + int ret; + DWORD size; + char *openname; + + s->fpipe = TRUE; + + 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; + } + + openname = g_strdup_printf("\\\\.\\pipe\\%s", filename); + s->hcom = CreateNamedPipe(openname, + PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, + PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | + PIPE_WAIT, + MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL); + g_free(openname); + if (s->hcom == INVALID_HANDLE_VALUE) { + error_setg(errp, "Failed CreateNamedPipe (%lu)", GetLastError()); + s->hcom = NULL; + goto fail; + } + + ZeroMemory(&ov, sizeof(ov)); + ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); + ret = ConnectNamedPipe(s->hcom, &ov); + if (ret) { + error_setg(errp, "Failed ConnectNamedPipe"); + goto fail; + } + + ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); + if (!ret) { + error_setg(errp, "Failed GetOverlappedResult"); + if (ov.hEvent) { + CloseHandle(ov.hEvent); + ov.hEvent = NULL; + } + goto fail; + } + + if (ov.hEvent) { + CloseHandle(ov.hEvent); + ov.hEvent = NULL; + } + qemu_add_polling_cb(win_chr_pipe_poll, chr); + return 0; + + fail: + return -1; +} + +static void qemu_chr_open_pipe(Chardev *chr, + ChardevBackend *backend, + bool *be_opened, + Error **errp) +{ + ChardevHostdev *opts = backend->u.pipe.data; + const char *filename = opts->device; + + if (win_chr_pipe_init(chr, filename, errp) < 0) { + return; + } +} + +#else + +static void qemu_chr_open_pipe(Chardev *chr, + ChardevBackend *backend, + bool *be_opened, + Error **errp) +{ + ChardevHostdev *opts = backend->u.pipe.data; + int fd_in, fd_out; + char *filename_in; + char *filename_out; + const char *filename = opts->device; + + filename_in = g_strdup_printf("%s.in", filename); + filename_out = g_strdup_printf("%s.out", filename); + TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY)); + TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY)); + g_free(filename_in); + g_free(filename_out); + if (fd_in < 0 || fd_out < 0) { + if (fd_in >= 0) { + close(fd_in); + } + if (fd_out >= 0) { + close(fd_out); + } + TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY)); + if (fd_in < 0) { + error_setg_file_open(errp, errno, filename); + return; + } + } + qemu_chr_open_fd(chr, fd_in, fd_out); +} + +#endif /* !_WIN32 */ + +static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend, + Error **errp) +{ + const char *device = qemu_opt_get(opts, "path"); + ChardevHostdev *dev; + + if (device == NULL) { + error_setg(errp, "chardev: pipe: no device path given"); + return; + } + backend->type = CHARDEV_BACKEND_KIND_PIPE; + dev = backend->u.pipe.data = g_new0(ChardevHostdev, 1); + qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(dev)); + dev->device = g_strdup(device); +} + +static void char_pipe_class_init(ObjectClass *oc, void *data) +{ + ChardevClass *cc = CHARDEV_CLASS(oc); + + cc->parse = qemu_chr_parse_pipe; + cc->open = qemu_chr_open_pipe; +} + +static const TypeInfo char_pipe_type_info = { + .name = TYPE_CHARDEV_PIPE, +#ifdef _WIN32 + .parent = TYPE_CHARDEV_WIN, +#else + .parent = TYPE_CHARDEV_FD, +#endif + .class_init = char_pipe_class_init, +}; + +static void register_types(void) +{ + type_register_static(&char_pipe_type_info); +} + +type_init(register_types); diff --git a/chardev/char-pty.c b/chardev/char-pty.c new file mode 100644 index 0000000000..27eb85f505 --- /dev/null +++ b/chardev/char-pty.c @@ -0,0 +1,300 @@ +/* + * 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. + */ +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu-common.h" +#include "sysemu/char.h" +#include "io/channel-file.h" +#include "qemu/sockets.h" +#include "qemu/error-report.h" + +#include "char-io.h" + +#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ + || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \ + || defined(__GLIBC__) + +typedef struct { + Chardev parent; + QIOChannel *ioc; + int read_bytes; + + /* Protected by the Chardev chr_write_lock. */ + int connected; + guint timer_tag; + guint open_tag; +} PtyChardev; + +#define PTY_CHARDEV(obj) OBJECT_CHECK(PtyChardev, (obj), TYPE_CHARDEV_PTY) + +static void pty_chr_update_read_handler_locked(Chardev *chr); +static void pty_chr_state(Chardev *chr, int connected); + +static gboolean pty_chr_timer(gpointer opaque) +{ + struct Chardev *chr = CHARDEV(opaque); + PtyChardev *s = PTY_CHARDEV(opaque); + + qemu_mutex_lock(&chr->chr_write_lock); + s->timer_tag = 0; + s->open_tag = 0; + if (!s->connected) { + /* Next poll ... */ + pty_chr_update_read_handler_locked(chr); + } + qemu_mutex_unlock(&chr->chr_write_lock); + return FALSE; +} + +/* Called with chr_write_lock held. */ +static void pty_chr_rearm_timer(Chardev *chr, int ms) +{ + PtyChardev *s = PTY_CHARDEV(chr); + char *name; + + if (s->timer_tag) { + g_source_remove(s->timer_tag); + s->timer_tag = 0; + } + + if (ms == 1000) { + name = g_strdup_printf("pty-timer-secs-%s", chr->label); + s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr); + } else { + name = g_strdup_printf("pty-timer-ms-%s", chr->label); + s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr); + } + g_source_set_name_by_id(s->timer_tag, name); + g_free(name); +} + +/* Called with chr_write_lock held. */ +static void pty_chr_update_read_handler_locked(Chardev *chr) +{ + PtyChardev *s = PTY_CHARDEV(chr); + GPollFD pfd; + int rc; + QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc); + + pfd.fd = fioc->fd; + pfd.events = G_IO_OUT; + pfd.revents = 0; + do { + rc = g_poll(&pfd, 1, 0); + } while (rc == -1 && errno == EINTR); + assert(rc >= 0); + + if (pfd.revents & G_IO_HUP) { + pty_chr_state(chr, 0); + } else { + pty_chr_state(chr, 1); + } +} + +static void pty_chr_update_read_handler(Chardev *chr, + GMainContext *context) +{ + qemu_mutex_lock(&chr->chr_write_lock); + pty_chr_update_read_handler_locked(chr); + qemu_mutex_unlock(&chr->chr_write_lock); +} + +/* Called with chr_write_lock held. */ +static int char_pty_chr_write(Chardev *chr, const uint8_t *buf, int len) +{ + PtyChardev *s = PTY_CHARDEV(chr); + + if (!s->connected) { + /* guest sends data, check for (re-)connect */ + pty_chr_update_read_handler_locked(chr); + if (!s->connected) { + return 0; + } + } + return io_channel_send(s->ioc, buf, len); +} + +static GSource *pty_chr_add_watch(Chardev *chr, GIOCondition cond) +{ + PtyChardev *s = PTY_CHARDEV(chr); + if (!s->connected) { + return NULL; + } + return qio_channel_create_watch(s->ioc, cond); +} + +static int pty_chr_read_poll(void *opaque) +{ + Chardev *chr = CHARDEV(opaque); + PtyChardev *s = PTY_CHARDEV(opaque); + + s->read_bytes = qemu_chr_be_can_write(chr); + return s->read_bytes; +} + +static gboolean pty_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque) +{ + Chardev *chr = CHARDEV(opaque); + PtyChardev *s = PTY_CHARDEV(opaque); + gsize len; + uint8_t buf[CHR_READ_BUF_LEN]; + ssize_t ret; + + len = sizeof(buf); + if (len > s->read_bytes) { + len = s->read_bytes; + } + if (len == 0) { + return TRUE; + } + ret = qio_channel_read(s->ioc, (char *)buf, len, NULL); + if (ret <= 0) { + pty_chr_state(chr, 0); + return FALSE; + } else { + pty_chr_state(chr, 1); + qemu_chr_be_write(chr, buf, ret); + } + return TRUE; +} + +static gboolean qemu_chr_be_generic_open_func(gpointer opaque) +{ + Chardev *chr = CHARDEV(opaque); + PtyChardev *s = PTY_CHARDEV(opaque); + + s->open_tag = 0; + qemu_chr_be_generic_open(chr); + return FALSE; +} + +/* Called with chr_write_lock held. */ +static void pty_chr_state(Chardev *chr, int connected) +{ + PtyChardev *s = PTY_CHARDEV(chr); + + if (!connected) { + if (s->open_tag) { + g_source_remove(s->open_tag); + s->open_tag = 0; + } + remove_fd_in_watch(chr); + s->connected = 0; + /* (re-)connect poll interval for idle guests: once per second. + * We check more frequently in case the guests sends data to + * the virtual device linked to our pty. */ + pty_chr_rearm_timer(chr, 1000); + } else { + if (s->timer_tag) { + g_source_remove(s->timer_tag); + s->timer_tag = 0; + } + if (!s->connected) { + g_assert(s->open_tag == 0); + s->connected = 1; + s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr); + } + if (!chr->fd_in_tag) { + chr->fd_in_tag = io_add_watch_poll(chr, s->ioc, + pty_chr_read_poll, + pty_chr_read, + chr, NULL); + } + } +} + +static void char_pty_finalize(Object *obj) +{ + Chardev *chr = CHARDEV(obj); + PtyChardev *s = PTY_CHARDEV(obj); + + qemu_mutex_lock(&chr->chr_write_lock); + pty_chr_state(chr, 0); + object_unref(OBJECT(s->ioc)); + if (s->timer_tag) { + g_source_remove(s->timer_tag); + s->timer_tag = 0; + } + qemu_mutex_unlock(&chr->chr_write_lock); + qemu_chr_be_event(chr, CHR_EVENT_CLOSED); +} + +static void char_pty_open(Chardev *chr, + ChardevBackend *backend, + bool *be_opened, + Error **errp) +{ + PtyChardev *s; + int master_fd, slave_fd; + char pty_name[PATH_MAX]; + char *name; + + master_fd = qemu_openpty_raw(&slave_fd, pty_name); + if (master_fd < 0) { + error_setg_errno(errp, errno, "Failed to create PTY"); + return; + } + + close(slave_fd); + qemu_set_nonblock(master_fd); + + chr->filename = g_strdup_printf("pty:%s", pty_name); + error_report("char device redirected to %s (label %s)", + pty_name, chr->label); + + s = PTY_CHARDEV(chr); + s->ioc = QIO_CHANNEL(qio_channel_file_new_fd(master_fd)); + name = g_strdup_printf("chardev-pty-%s", chr->label); + qio_channel_set_name(QIO_CHANNEL(s->ioc), name); + g_free(name); + s->timer_tag = 0; + *be_opened = false; +} + +static void char_pty_class_init(ObjectClass *oc, void *data) +{ + ChardevClass *cc = CHARDEV_CLASS(oc); + + cc->open = char_pty_open; + cc->chr_write = char_pty_chr_write; + cc->chr_update_read_handler = pty_chr_update_read_handler; + cc->chr_add_watch = pty_chr_add_watch; +} + +static const TypeInfo char_pty_type_info = { + .name = TYPE_CHARDEV_PTY, + .parent = TYPE_CHARDEV, + .instance_size = sizeof(PtyChardev), + .instance_finalize = char_pty_finalize, + .class_init = char_pty_class_init, +}; + +static void register_types(void) +{ + type_register_static(&char_pty_type_info); +} + +type_init(register_types); + +#endif diff --git a/chardev/char-ringbuf.c b/chardev/char-ringbuf.c new file mode 100644 index 0000000000..d130069e88 --- /dev/null +++ b/chardev/char-ringbuf.c @@ -0,0 +1,249 @@ +/* + * 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. + */ +#include "qemu/osdep.h" +#include "sysemu/char.h" +#include "qmp-commands.h" +#include "qemu/base64.h" + +/* Ring buffer chardev */ + +typedef struct { + Chardev parent; + size_t size; + size_t prod; + size_t cons; + uint8_t *cbuf; +} RingBufChardev; + +#define RINGBUF_CHARDEV(obj) \ + OBJECT_CHECK(RingBufChardev, (obj), TYPE_CHARDEV_RINGBUF) + +static size_t ringbuf_count(const Chardev *chr) +{ + const RingBufChardev *d = RINGBUF_CHARDEV(chr); + + return d->prod - d->cons; +} + +static int ringbuf_chr_write(Chardev *chr, const uint8_t *buf, int len) +{ + RingBufChardev *d = RINGBUF_CHARDEV(chr); + int i; + + if (!buf || (len < 0)) { + return -1; + } + + for (i = 0; i < len; i++) { + d->cbuf[d->prod++ & (d->size - 1)] = buf[i]; + if (d->prod - d->cons > d->size) { + d->cons = d->prod - d->size; + } + } + + return len; +} + +static int ringbuf_chr_read(Chardev *chr, uint8_t *buf, int len) +{ + RingBufChardev *d = RINGBUF_CHARDEV(chr); + int i; + + qemu_mutex_lock(&chr->chr_write_lock); + for (i = 0; i < len && d->cons != d->prod; i++) { + buf[i] = d->cbuf[d->cons++ & (d->size - 1)]; + } + qemu_mutex_unlock(&chr->chr_write_lock); + + return i; +} + +static void char_ringbuf_finalize(Object *obj) +{ + RingBufChardev *d = RINGBUF_CHARDEV(obj); + + g_free(d->cbuf); +} + +static void qemu_chr_open_ringbuf(Chardev *chr, + ChardevBackend *backend, + bool *be_opened, + Error **errp) +{ + ChardevRingbuf *opts = backend->u.ringbuf.data; + RingBufChardev *d = RINGBUF_CHARDEV(chr); + + d->size = opts->has_size ? opts->size : 65536; + + /* The size must be power of 2 */ + if (d->size & (d->size - 1)) { + error_setg(errp, "size of ringbuf chardev must be power of two"); + return; + } + + d->prod = 0; + d->cons = 0; + d->cbuf = g_malloc0(d->size); +} + +void qmp_ringbuf_write(const char *device, const char *data, + bool has_format, enum DataFormat format, + Error **errp) +{ + Chardev *chr; + const uint8_t *write_data; + int ret; + gsize write_count; + + chr = qemu_chr_find(device); + if (!chr) { + error_setg(errp, "Device '%s' not found", device); + return; + } + + if (!CHARDEV_IS_RINGBUF(chr)) { + error_setg(errp, "%s is not a ringbuf device", device); + return; + } + + if (has_format && (format == DATA_FORMAT_BASE64)) { + write_data = qbase64_decode(data, -1, + &write_count, + errp); + if (!write_data) { + return; + } + } else { + write_data = (uint8_t *)data; + write_count = strlen(data); + } + + ret = ringbuf_chr_write(chr, write_data, write_count); + + if (write_data != (uint8_t *)data) { + g_free((void *)write_data); + } + + if (ret < 0) { + error_setg(errp, "Failed to write to device %s", device); + return; + } +} + +char *qmp_ringbuf_read(const char *device, int64_t size, + bool has_format, enum DataFormat format, + Error **errp) +{ + Chardev *chr; + uint8_t *read_data; + size_t count; + char *data; + + chr = qemu_chr_find(device); + if (!chr) { + error_setg(errp, "Device '%s' not found", device); + return NULL; + } + + if (!CHARDEV_IS_RINGBUF(chr)) { + error_setg(errp, "%s is not a ringbuf device", device); + return NULL; + } + + if (size <= 0) { + error_setg(errp, "size must be greater than zero"); + return NULL; + } + + count = ringbuf_count(chr); + size = size > count ? count : size; + read_data = g_malloc(size + 1); + + ringbuf_chr_read(chr, read_data, size); + + if (has_format && (format == DATA_FORMAT_BASE64)) { + data = g_base64_encode(read_data, size); + g_free(read_data); + } else { + /* + * FIXME should read only complete, valid UTF-8 characters up + * to @size bytes. Invalid sequences should be replaced by a + * suitable replacement character. Except when (and only + * when) ring buffer lost characters since last read, initial + * continuation characters should be dropped. + */ + read_data[size] = 0; + data = (char *)read_data; + } + + return data; +} + +static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend, + Error **errp) +{ + int val; + ChardevRingbuf *ringbuf; + + backend->type = CHARDEV_BACKEND_KIND_RINGBUF; + ringbuf = backend->u.ringbuf.data = g_new0(ChardevRingbuf, 1); + qemu_chr_parse_common(opts, qapi_ChardevRingbuf_base(ringbuf)); + + val = qemu_opt_get_size(opts, "size", 0); + if (val != 0) { + ringbuf->has_size = true; + ringbuf->size = val; + } +} + +static void char_ringbuf_class_init(ObjectClass *oc, void *data) +{ + ChardevClass *cc = CHARDEV_CLASS(oc); + + cc->parse = qemu_chr_parse_ringbuf; + cc->open = qemu_chr_open_ringbuf; + cc->chr_write = ringbuf_chr_write; +} + +static const TypeInfo char_ringbuf_type_info = { + .name = TYPE_CHARDEV_RINGBUF, + .parent = TYPE_CHARDEV, + .class_init = char_ringbuf_class_init, + .instance_size = sizeof(RingBufChardev), + .instance_finalize = char_ringbuf_finalize, +}; + +/* Bug-compatibility: */ +static const TypeInfo char_memory_type_info = { + .name = TYPE_CHARDEV_MEMORY, + .parent = TYPE_CHARDEV_RINGBUF, +}; + +static void register_types(void) +{ + type_register_static(&char_ringbuf_type_info); + type_register_static(&char_memory_type_info); +} + +type_init(register_types); diff --git a/chardev/char-serial.c b/chardev/char-serial.c new file mode 100644 index 0000000000..094e08dca5 --- /dev/null +++ b/chardev/char-serial.c @@ -0,0 +1,318 @@ +/* + * 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. + */ +#include "qemu/osdep.h" +#include "qemu/sockets.h" +#include "io/channel-file.h" +#include "qapi/error.h" + +#ifdef _WIN32 +#include "char-win.h" +#else +#include <sys/ioctl.h> +#include <termios.h> +#include "char-fd.h" +#endif + +#include "char-serial.h" + +#ifdef _WIN32 + +static void qmp_chardev_open_serial(Chardev *chr, + ChardevBackend *backend, + bool *be_opened, + Error **errp) +{ + ChardevHostdev *serial = backend->u.serial.data; + + win_chr_init(chr, serial->device, errp); +} + +#elif defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ + || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \ + || defined(__GLIBC__) + +static void tty_serial_init(int fd, int speed, + int parity, int data_bits, int stop_bits) +{ + struct termios tty; + speed_t spd; + +#if 0 + printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n", + speed, parity, data_bits, stop_bits); +#endif + tcgetattr(fd, &tty); + +#define check_speed(val) if (speed <= val) { spd = B##val; break; } + speed = speed * 10 / 11; + do { + check_speed(50); + check_speed(75); + check_speed(110); + check_speed(134); + check_speed(150); + check_speed(200); + check_speed(300); + check_speed(600); + check_speed(1200); + check_speed(1800); + check_speed(2400); + check_speed(4800); + check_speed(9600); + check_speed(19200); + check_speed(38400); + /* Non-Posix values follow. They may be unsupported on some systems. */ + check_speed(57600); + check_speed(115200); +#ifdef B230400 + check_speed(230400); +#endif +#ifdef B460800 + check_speed(460800); +#endif +#ifdef B500000 + check_speed(500000); +#endif +#ifdef B576000 + check_speed(576000); +#endif +#ifdef B921600 + check_speed(921600); +#endif +#ifdef B1000000 + check_speed(1000000); +#endif +#ifdef B1152000 + check_speed(1152000); +#endif +#ifdef B1500000 + check_speed(1500000); +#endif +#ifdef B2000000 + check_speed(2000000); +#endif +#ifdef B2500000 + check_speed(2500000); +#endif +#ifdef B3000000 + check_speed(3000000); +#endif +#ifdef B3500000 + check_speed(3500000); +#endif +#ifdef B4000000 + check_speed(4000000); +#endif + spd = B115200; + } while (0); + + cfsetispeed(&tty, spd); + cfsetospeed(&tty, spd); + + tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP + | INLCR | IGNCR | ICRNL | IXON); + tty.c_oflag |= OPOST; + tty.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG); + tty.c_cflag &= ~(CSIZE | PARENB | PARODD | CRTSCTS | CSTOPB); + switch (data_bits) { + default: + case 8: + tty.c_cflag |= CS8; + break; + case 7: + tty.c_cflag |= CS7; + break; + case 6: + tty.c_cflag |= CS6; + break; + case 5: + tty.c_cflag |= CS5; + break; + } + switch (parity) { + default: + case 'N': + break; + case 'E': + tty.c_cflag |= PARENB; + break; + case 'O': + tty.c_cflag |= PARENB | PARODD; + break; + } + if (stop_bits == 2) { + tty.c_cflag |= CSTOPB; + } + + tcsetattr(fd, TCSANOW, &tty); +} + +static int tty_serial_ioctl(Chardev *chr, int cmd, void *arg) +{ + FDChardev *s = FD_CHARDEV(chr); + QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc_in); + + switch (cmd) { + case CHR_IOCTL_SERIAL_SET_PARAMS: + { + QEMUSerialSetParams *ssp = arg; + tty_serial_init(fioc->fd, + ssp->speed, ssp->parity, + ssp->data_bits, ssp->stop_bits); + } + break; + case CHR_IOCTL_SERIAL_SET_BREAK: + { + int enable = *(int *)arg; + if (enable) { + tcsendbreak(fioc->fd, 1); + } + } + break; + case CHR_IOCTL_SERIAL_GET_TIOCM: + { + int sarg = 0; + int *targ = (int *)arg; + ioctl(fioc->fd, TIOCMGET, &sarg); + *targ = 0; + if (sarg & TIOCM_CTS) { + *targ |= CHR_TIOCM_CTS; + } + if (sarg & TIOCM_CAR) { + *targ |= CHR_TIOCM_CAR; + } + if (sarg & TIOCM_DSR) { + *targ |= CHR_TIOCM_DSR; + } + if (sarg & TIOCM_RI) { + *targ |= CHR_TIOCM_RI; + } + if (sarg & TIOCM_DTR) { + *targ |= CHR_TIOCM_DTR; + } + if (sarg & TIOCM_RTS) { + *targ |= CHR_TIOCM_RTS; + } + } + break; + case CHR_IOCTL_SERIAL_SET_TIOCM: + { + int sarg = *(int *)arg; + int targ = 0; + ioctl(fioc->fd, TIOCMGET, &targ); + targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR + | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS); + if (sarg & CHR_TIOCM_CTS) { + targ |= TIOCM_CTS; + } + if (sarg & CHR_TIOCM_CAR) { + targ |= TIOCM_CAR; + } + if (sarg & CHR_TIOCM_DSR) { + targ |= TIOCM_DSR; + } + if (sarg & CHR_TIOCM_RI) { + targ |= TIOCM_RI; + } + if (sarg & CHR_TIOCM_DTR) { + targ |= TIOCM_DTR; + } + if (sarg & CHR_TIOCM_RTS) { + targ |= TIOCM_RTS; + } + ioctl(fioc->fd, TIOCMSET, &targ); + } + break; + default: + return -ENOTSUP; + } + return 0; +} + +static void qmp_chardev_open_serial(Chardev *chr, + ChardevBackend *backend, + bool *be_opened, + Error **errp) +{ + ChardevHostdev *serial = backend->u.serial.data; + int fd; + + fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp); + if (fd < 0) { + return; + } + qemu_set_nonblock(fd); + tty_serial_init(fd, 115200, 'N', 8, 1); + + qemu_chr_open_fd(chr, fd, fd); +} +#endif /* __linux__ || __sun__ */ + +#ifdef HAVE_CHARDEV_SERIAL +static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend, + Error **errp) +{ + const char *device = qemu_opt_get(opts, "path"); + ChardevHostdev *serial; + + if (device == NULL) { + error_setg(errp, "chardev: serial/tty: no device path given"); + return; + } + backend->type = CHARDEV_BACKEND_KIND_SERIAL; + serial = backend->u.serial.data = g_new0(ChardevHostdev, 1); + qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(serial)); + serial->device = g_strdup(device); +} + +static void char_serial_class_init(ObjectClass *oc, void *data) +{ + ChardevClass *cc = CHARDEV_CLASS(oc); + + cc->parse = qemu_chr_parse_serial; + cc->open = qmp_chardev_open_serial; +#ifndef _WIN32 + cc->chr_ioctl = tty_serial_ioctl; +#endif +} + + +static const TypeInfo char_serial_type_info = { + .name = TYPE_CHARDEV_SERIAL, +#ifdef _WIN32 + .parent = TYPE_CHARDEV_WIN, +#else + .parent = TYPE_CHARDEV_FD, +#endif + .class_init = char_serial_class_init, +}; + +static void register_types(void) +{ + type_register_static(&char_serial_type_info); +} + +type_init(register_types); + +#endif diff --git a/chardev/char-serial.h b/chardev/char-serial.h new file mode 100644 index 0000000000..64a27f63b1 --- /dev/null +++ b/chardev/char-serial.h @@ -0,0 +1,35 @@ +/* + * 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. + */ +#ifndef CHAR_SERIAL_H +#define CHAR_SERIAL_H + +#ifdef _WIN32 +#define HAVE_CHARDEV_SERIAL 1 +#elif defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ + || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \ + || defined(__GLIBC__) +#define HAVE_CHARDEV_SERIAL 1 +#endif + +#endif diff --git a/chardev/char-socket.c b/chardev/char-socket.c new file mode 100644 index 0000000000..4068dc5e52 --- /dev/null +++ b/chardev/char-socket.c @@ -0,0 +1,1017 @@ +/* + * 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. + */ +#include "qemu/osdep.h" +#include "sysemu/char.h" +#include "io/channel-socket.h" +#include "io/channel-tls.h" +#include "qemu/error-report.h" +#include "qapi/error.h" +#include "qapi/clone-visitor.h" + +#include "char-io.h" + +/***********************************************************/ +/* TCP Net console */ + +#define TCP_MAX_FDS 16 + +typedef struct { + Chardev parent; + QIOChannel *ioc; /* Client I/O channel */ + QIOChannelSocket *sioc; /* Client master channel */ + QIOChannelSocket *listen_ioc; + guint listen_tag; + QCryptoTLSCreds *tls_creds; + int connected; + int max_size; + int do_telnetopt; + int do_nodelay; + int is_unix; + int *read_msgfds; + size_t read_msgfds_num; + int *write_msgfds; + size_t write_msgfds_num; + + SocketAddress *addr; + bool is_listen; + bool is_telnet; + + guint reconnect_timer; + int64_t reconnect_time; + bool connect_err_reported; +} SocketChardev; + +#define SOCKET_CHARDEV(obj) \ + OBJECT_CHECK(SocketChardev, (obj), TYPE_CHARDEV_SOCKET) + +static gboolean socket_reconnect_timeout(gpointer opaque); + +static void qemu_chr_socket_restart_timer(Chardev *chr) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + char *name; + + assert(s->connected == 0); + s->reconnect_timer = g_timeout_add_seconds(s->reconnect_time, + socket_reconnect_timeout, chr); + name = g_strdup_printf("chardev-socket-reconnect-%s", chr->label); + g_source_set_name_by_id(s->reconnect_timer, name); + g_free(name); +} + +static void check_report_connect_error(Chardev *chr, + Error *err) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + + if (!s->connect_err_reported) { + error_report("Unable to connect character device %s: %s", + chr->label, error_get_pretty(err)); + s->connect_err_reported = true; + } + qemu_chr_socket_restart_timer(chr); +} + +static gboolean tcp_chr_accept(QIOChannel *chan, + GIOCondition cond, + void *opaque); + +/* Called with chr_write_lock held. */ +static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + + if (s->connected) { + int ret = io_channel_send_full(s->ioc, buf, len, + s->write_msgfds, + s->write_msgfds_num); + + /* free the written msgfds, no matter what */ + if (s->write_msgfds_num) { + g_free(s->write_msgfds); + s->write_msgfds = 0; + s->write_msgfds_num = 0; + } + + return ret; + } else { + /* XXX: indicate an error ? */ + return len; + } +} + +static int tcp_chr_read_poll(void *opaque) +{ + Chardev *chr = CHARDEV(opaque); + SocketChardev *s = SOCKET_CHARDEV(opaque); + if (!s->connected) { + return 0; + } + s->max_size = qemu_chr_be_can_write(chr); + return s->max_size; +} + +#define IAC 255 +#define IAC_BREAK 243 +static void tcp_chr_process_IAC_bytes(Chardev *chr, + SocketChardev *s, + uint8_t *buf, int *size) +{ + /* Handle any telnet client's basic IAC options to satisfy char by + * char mode with no echo. All IAC options will be removed from + * the buf and the do_telnetopt variable will be used to track the + * state of the width of the IAC information. + * + * IAC commands come in sets of 3 bytes with the exception of the + * "IAC BREAK" command and the double IAC. + */ + + int i; + int j = 0; + + for (i = 0; i < *size; i++) { + if (s->do_telnetopt > 1) { + if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { + /* Double IAC means send an IAC */ + if (j != i) { + buf[j] = buf[i]; + } + j++; + s->do_telnetopt = 1; + } else { + if ((unsigned char)buf[i] == IAC_BREAK + && s->do_telnetopt == 2) { + /* Handle IAC break commands by sending a serial break */ + qemu_chr_be_event(chr, CHR_EVENT_BREAK); + s->do_telnetopt++; + } + s->do_telnetopt++; + } + if (s->do_telnetopt >= 4) { + s->do_telnetopt = 1; + } + } else { + if ((unsigned char)buf[i] == IAC) { + s->do_telnetopt = 2; + } else { + if (j != i) { + buf[j] = buf[i]; + } + j++; + } + } + } + *size = j; +} + +static int tcp_get_msgfds(Chardev *chr, int *fds, int num) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + + int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num; + + assert(num <= TCP_MAX_FDS); + + if (to_copy) { + int i; + + memcpy(fds, s->read_msgfds, to_copy * sizeof(int)); + + /* Close unused fds */ + for (i = to_copy; i < s->read_msgfds_num; i++) { + close(s->read_msgfds[i]); + } + + g_free(s->read_msgfds); + s->read_msgfds = 0; + s->read_msgfds_num = 0; + } + + return to_copy; +} + +static int tcp_set_msgfds(Chardev *chr, int *fds, int num) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + + /* clear old pending fd array */ + g_free(s->write_msgfds); + s->write_msgfds = NULL; + s->write_msgfds_num = 0; + + if (!s->connected || + !qio_channel_has_feature(s->ioc, + QIO_CHANNEL_FEATURE_FD_PASS)) { + return -1; + } + + if (num) { + s->write_msgfds = g_new(int, num); + memcpy(s->write_msgfds, fds, num * sizeof(int)); + } + + s->write_msgfds_num = num; + + return 0; +} + +static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + struct iovec iov = { .iov_base = buf, .iov_len = len }; + int ret; + size_t i; + int *msgfds = NULL; + size_t msgfds_num = 0; + + if (qio_channel_has_feature(s->ioc, QIO_CHANNEL_FEATURE_FD_PASS)) { + ret = qio_channel_readv_full(s->ioc, &iov, 1, + &msgfds, &msgfds_num, + NULL); + } else { + ret = qio_channel_readv_full(s->ioc, &iov, 1, + NULL, NULL, + NULL); + } + + if (ret == QIO_CHANNEL_ERR_BLOCK) { + errno = EAGAIN; + ret = -1; + } else if (ret == -1) { + errno = EIO; + } + + if (msgfds_num) { + /* close and clean read_msgfds */ + for (i = 0; i < s->read_msgfds_num; i++) { + close(s->read_msgfds[i]); + } + + if (s->read_msgfds_num) { + g_free(s->read_msgfds); + } + + s->read_msgfds = msgfds; + s->read_msgfds_num = msgfds_num; + } + + for (i = 0; i < s->read_msgfds_num; i++) { + int fd = s->read_msgfds[i]; + if (fd < 0) { + continue; + } + + /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */ + qemu_set_block(fd); + +#ifndef MSG_CMSG_CLOEXEC + qemu_set_cloexec(fd); +#endif + } + + return ret; +} + +static GSource *tcp_chr_add_watch(Chardev *chr, GIOCondition cond) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + return qio_channel_create_watch(s->ioc, cond); +} + +static void tcp_chr_free_connection(Chardev *chr) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + int i; + + if (!s->connected) { + return; + } + + if (s->read_msgfds_num) { + for (i = 0; i < s->read_msgfds_num; i++) { + close(s->read_msgfds[i]); + } + g_free(s->read_msgfds); + s->read_msgfds = NULL; + s->read_msgfds_num = 0; + } + + tcp_set_msgfds(chr, NULL, 0); + remove_fd_in_watch(chr); + object_unref(OBJECT(s->sioc)); + s->sioc = NULL; + object_unref(OBJECT(s->ioc)); + s->ioc = NULL; + g_free(chr->filename); + chr->filename = NULL; + s->connected = 0; +} + +static char *SocketAddress_to_str(const char *prefix, SocketAddress *addr, + bool is_listen, bool is_telnet) +{ + switch (addr->type) { + case SOCKET_ADDRESS_KIND_INET: + return g_strdup_printf("%s%s:%s:%s%s", prefix, + is_telnet ? "telnet" : "tcp", + addr->u.inet.data->host, + addr->u.inet.data->port, + is_listen ? ",server" : ""); + break; + case SOCKET_ADDRESS_KIND_UNIX: + return g_strdup_printf("%sunix:%s%s", prefix, + addr->u.q_unix.data->path, + is_listen ? ",server" : ""); + break; + case SOCKET_ADDRESS_KIND_FD: + return g_strdup_printf("%sfd:%s%s", prefix, addr->u.fd.data->str, + is_listen ? ",server" : ""); + break; + default: + abort(); + } +} + +static void tcp_chr_disconnect(Chardev *chr) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + + if (!s->connected) { + return; + } + + tcp_chr_free_connection(chr); + + if (s->listen_ioc) { + s->listen_tag = qio_channel_add_watch( + QIO_CHANNEL(s->listen_ioc), G_IO_IN, tcp_chr_accept, chr, NULL); + } + chr->filename = SocketAddress_to_str("disconnected:", s->addr, + s->is_listen, s->is_telnet); + qemu_chr_be_event(chr, CHR_EVENT_CLOSED); + if (s->reconnect_time) { + qemu_chr_socket_restart_timer(chr); + } +} + +static gboolean tcp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque) +{ + Chardev *chr = CHARDEV(opaque); + SocketChardev *s = SOCKET_CHARDEV(opaque); + uint8_t buf[CHR_READ_BUF_LEN]; + int len, size; + + if (!s->connected || s->max_size <= 0) { + return TRUE; + } + len = sizeof(buf); + if (len > s->max_size) { + len = s->max_size; + } + size = tcp_chr_recv(chr, (void *)buf, len); + if (size == 0 || size == -1) { + /* connection closed */ + tcp_chr_disconnect(chr); + } else if (size > 0) { + if (s->do_telnetopt) { + tcp_chr_process_IAC_bytes(chr, s, buf, &size); + } + if (size > 0) { + qemu_chr_be_write(chr, buf, size); + } + } + + return TRUE; +} + +static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + int size; + + if (!s->connected) { + return 0; + } + + size = tcp_chr_recv(chr, (void *) buf, len); + if (size == 0) { + /* connection closed */ + tcp_chr_disconnect(chr); + } + + return size; +} + +static char *sockaddr_to_str(struct sockaddr_storage *ss, socklen_t ss_len, + struct sockaddr_storage *ps, socklen_t ps_len, + bool is_listen, bool is_telnet) +{ + char shost[NI_MAXHOST], sserv[NI_MAXSERV]; + char phost[NI_MAXHOST], pserv[NI_MAXSERV]; + const char *left = "", *right = ""; + + switch (ss->ss_family) { +#ifndef _WIN32 + case AF_UNIX: + return g_strdup_printf("unix:%s%s", + ((struct sockaddr_un *)(ss))->sun_path, + is_listen ? ",server" : ""); +#endif + case AF_INET6: + left = "["; + right = "]"; + /* fall through */ + case AF_INET: + getnameinfo((struct sockaddr *) ss, ss_len, shost, sizeof(shost), + sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV); + getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost), + pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV); + return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s", + is_telnet ? "telnet" : "tcp", + left, shost, right, sserv, + is_listen ? ",server" : "", + left, phost, right, pserv); + + default: + return g_strdup_printf("unknown"); + } +} + +static void tcp_chr_connect(void *opaque) +{ + Chardev *chr = CHARDEV(opaque); + SocketChardev *s = SOCKET_CHARDEV(opaque); + + g_free(chr->filename); + chr->filename = sockaddr_to_str( + &s->sioc->localAddr, s->sioc->localAddrLen, + &s->sioc->remoteAddr, s->sioc->remoteAddrLen, + s->is_listen, s->is_telnet); + + s->connected = 1; + if (s->ioc) { + chr->fd_in_tag = io_add_watch_poll(chr, s->ioc, + tcp_chr_read_poll, + tcp_chr_read, + chr, NULL); + } + qemu_chr_be_generic_open(chr); +} + +static void tcp_chr_update_read_handler(Chardev *chr, + GMainContext *context) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + + if (!s->connected) { + return; + } + + remove_fd_in_watch(chr); + if (s->ioc) { + chr->fd_in_tag = io_add_watch_poll(chr, s->ioc, + tcp_chr_read_poll, + tcp_chr_read, chr, + context); + } +} + +typedef struct { + Chardev *chr; + char buf[12]; + size_t buflen; +} TCPChardevTelnetInit; + +static gboolean tcp_chr_telnet_init_io(QIOChannel *ioc, + GIOCondition cond G_GNUC_UNUSED, + gpointer user_data) +{ + TCPChardevTelnetInit *init = user_data; + ssize_t ret; + + ret = qio_channel_write(ioc, init->buf, init->buflen, NULL); + if (ret < 0) { + if (ret == QIO_CHANNEL_ERR_BLOCK) { + ret = 0; + } else { + tcp_chr_disconnect(init->chr); + return FALSE; + } + } + init->buflen -= ret; + + if (init->buflen == 0) { + tcp_chr_connect(init->chr); + return FALSE; + } + + memmove(init->buf, init->buf + ret, init->buflen); + + return TRUE; +} + +static void tcp_chr_telnet_init(Chardev *chr) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + TCPChardevTelnetInit *init = g_new0(TCPChardevTelnetInit, 1); + size_t n = 0; + + init->chr = chr; + init->buflen = 12; + +#define IACSET(x, a, b, c) \ + do { \ + x[n++] = a; \ + x[n++] = b; \ + x[n++] = c; \ + } while (0) + + /* Prep the telnet negotion to put telnet in binary, + * no echo, single char mode */ + IACSET(init->buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ + IACSET(init->buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ + IACSET(init->buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ + IACSET(init->buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ + +#undef IACSET + + qio_channel_add_watch( + s->ioc, G_IO_OUT, + tcp_chr_telnet_init_io, + init, NULL); +} + + +static void tcp_chr_tls_handshake(QIOTask *task, + gpointer user_data) +{ + Chardev *chr = user_data; + SocketChardev *s = user_data; + + if (qio_task_propagate_error(task, NULL)) { + tcp_chr_disconnect(chr); + } else { + if (s->do_telnetopt) { + tcp_chr_telnet_init(chr); + } else { + tcp_chr_connect(chr); + } + } +} + + +static void tcp_chr_tls_init(Chardev *chr) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + QIOChannelTLS *tioc; + Error *err = NULL; + gchar *name; + + if (s->is_listen) { + tioc = qio_channel_tls_new_server( + s->ioc, s->tls_creds, + NULL, /* XXX Use an ACL */ + &err); + } else { + tioc = qio_channel_tls_new_client( + s->ioc, s->tls_creds, + s->addr->u.inet.data->host, + &err); + } + if (tioc == NULL) { + error_free(err); + tcp_chr_disconnect(chr); + return; + } + name = g_strdup_printf("chardev-tls-%s-%s", + s->is_listen ? "server" : "client", + chr->label); + qio_channel_set_name(QIO_CHANNEL(tioc), name); + g_free(name); + object_unref(OBJECT(s->ioc)); + s->ioc = QIO_CHANNEL(tioc); + + qio_channel_tls_handshake(tioc, + tcp_chr_tls_handshake, + chr, + NULL); +} + + +static void tcp_chr_set_client_ioc_name(Chardev *chr, + QIOChannelSocket *sioc) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + char *name; + name = g_strdup_printf("chardev-tcp-%s-%s", + s->is_listen ? "server" : "client", + chr->label); + qio_channel_set_name(QIO_CHANNEL(sioc), name); + g_free(name); + +} + +static int tcp_chr_new_client(Chardev *chr, QIOChannelSocket *sioc) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + + if (s->ioc != NULL) { + return -1; + } + + s->ioc = QIO_CHANNEL(sioc); + object_ref(OBJECT(sioc)); + s->sioc = sioc; + object_ref(OBJECT(sioc)); + + qio_channel_set_blocking(s->ioc, false, NULL); + + if (s->do_nodelay) { + qio_channel_set_delay(s->ioc, false); + } + if (s->listen_tag) { + g_source_remove(s->listen_tag); + s->listen_tag = 0; + } + + if (s->tls_creds) { + tcp_chr_tls_init(chr); + } else { + if (s->do_telnetopt) { + tcp_chr_telnet_init(chr); + } else { + tcp_chr_connect(chr); + } + } + + return 0; +} + + +static int tcp_chr_add_client(Chardev *chr, int fd) +{ + int ret; + QIOChannelSocket *sioc; + + sioc = qio_channel_socket_new_fd(fd, NULL); + if (!sioc) { + return -1; + } + tcp_chr_set_client_ioc_name(chr, sioc); + ret = tcp_chr_new_client(chr, sioc); + object_unref(OBJECT(sioc)); + return ret; +} + +static gboolean tcp_chr_accept(QIOChannel *channel, + GIOCondition cond, + void *opaque) +{ + Chardev *chr = CHARDEV(opaque); + QIOChannelSocket *sioc; + + sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(channel), + NULL); + if (!sioc) { + return TRUE; + } + + tcp_chr_new_client(chr, sioc); + + object_unref(OBJECT(sioc)); + + return TRUE; +} + +static int tcp_chr_wait_connected(Chardev *chr, Error **errp) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + QIOChannelSocket *sioc; + + /* It can't wait on s->connected, since it is set asynchronously + * in TLS and telnet cases, only wait for an accepted socket */ + while (!s->ioc) { + if (s->is_listen) { + error_report("QEMU waiting for connection on: %s", + chr->filename); + qio_channel_set_blocking(QIO_CHANNEL(s->listen_ioc), true, NULL); + tcp_chr_accept(QIO_CHANNEL(s->listen_ioc), G_IO_IN, chr); + qio_channel_set_blocking(QIO_CHANNEL(s->listen_ioc), false, NULL); + } else { + sioc = qio_channel_socket_new(); + tcp_chr_set_client_ioc_name(chr, sioc); + if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) { + object_unref(OBJECT(sioc)); + return -1; + } + tcp_chr_new_client(chr, sioc); + object_unref(OBJECT(sioc)); + } + } + + return 0; +} + +static void char_socket_finalize(Object *obj) +{ + Chardev *chr = CHARDEV(obj); + SocketChardev *s = SOCKET_CHARDEV(obj); + + tcp_chr_free_connection(chr); + + if (s->reconnect_timer) { + g_source_remove(s->reconnect_timer); + s->reconnect_timer = 0; + } + qapi_free_SocketAddress(s->addr); + if (s->listen_tag) { + g_source_remove(s->listen_tag); + s->listen_tag = 0; + } + if (s->listen_ioc) { + object_unref(OBJECT(s->listen_ioc)); + } + if (s->tls_creds) { + object_unref(OBJECT(s->tls_creds)); + } + + qemu_chr_be_event(chr, CHR_EVENT_CLOSED); +} + +static void qemu_chr_socket_connected(QIOTask *task, void *opaque) +{ + QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task)); + Chardev *chr = CHARDEV(opaque); + SocketChardev *s = SOCKET_CHARDEV(chr); + Error *err = NULL; + + if (qio_task_propagate_error(task, &err)) { + check_report_connect_error(chr, err); + error_free(err); + goto cleanup; + } + + s->connect_err_reported = false; + tcp_chr_new_client(chr, sioc); + +cleanup: + object_unref(OBJECT(sioc)); +} + +static gboolean socket_reconnect_timeout(gpointer opaque) +{ + Chardev *chr = CHARDEV(opaque); + SocketChardev *s = SOCKET_CHARDEV(opaque); + QIOChannelSocket *sioc; + + s->reconnect_timer = 0; + + if (chr->be_open) { + return false; + } + + sioc = qio_channel_socket_new(); + tcp_chr_set_client_ioc_name(chr, sioc); + qio_channel_socket_connect_async(sioc, s->addr, + qemu_chr_socket_connected, + chr, NULL); + + return false; +} + +static void qmp_chardev_open_socket(Chardev *chr, + ChardevBackend *backend, + bool *be_opened, + Error **errp) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + ChardevSocket *sock = backend->u.socket.data; + SocketAddress *addr = sock->addr; + bool do_nodelay = sock->has_nodelay ? sock->nodelay : false; + bool is_listen = sock->has_server ? sock->server : true; + bool is_telnet = sock->has_telnet ? sock->telnet : false; + bool is_waitconnect = sock->has_wait ? sock->wait : false; + int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0; + QIOChannelSocket *sioc = NULL; + + s->is_unix = addr->type == SOCKET_ADDRESS_KIND_UNIX; + s->is_listen = is_listen; + s->is_telnet = is_telnet; + s->do_nodelay = do_nodelay; + if (sock->tls_creds) { + Object *creds; + creds = object_resolve_path_component( + object_get_objects_root(), sock->tls_creds); + if (!creds) { + error_setg(errp, "No TLS credentials with id '%s'", + sock->tls_creds); + goto error; + } + s->tls_creds = (QCryptoTLSCreds *) + object_dynamic_cast(creds, + TYPE_QCRYPTO_TLS_CREDS); + if (!s->tls_creds) { + error_setg(errp, "Object with id '%s' is not TLS credentials", + sock->tls_creds); + goto error; + } + object_ref(OBJECT(s->tls_creds)); + if (is_listen) { + if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { + error_setg(errp, "%s", + "Expected TLS credentials for server endpoint"); + goto error; + } + } else { + if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) { + error_setg(errp, "%s", + "Expected TLS credentials for client endpoint"); + goto error; + } + } + } + + s->addr = QAPI_CLONE(SocketAddress, sock->addr); + + qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE); + if (s->is_unix) { + qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_FD_PASS); + } + + /* be isn't opened until we get a connection */ + *be_opened = false; + + chr->filename = SocketAddress_to_str("disconnected:", + addr, is_listen, is_telnet); + + if (is_listen) { + if (is_telnet) { + s->do_telnetopt = 1; + } + } else if (reconnect > 0) { + s->reconnect_time = reconnect; + } + + if (s->reconnect_time) { + sioc = qio_channel_socket_new(); + tcp_chr_set_client_ioc_name(chr, sioc); + qio_channel_socket_connect_async(sioc, s->addr, + qemu_chr_socket_connected, + chr, NULL); + } else { + if (s->is_listen) { + char *name; + sioc = qio_channel_socket_new(); + + name = g_strdup_printf("chardev-tcp-listener-%s", chr->label); + qio_channel_set_name(QIO_CHANNEL(sioc), name); + g_free(name); + + if (qio_channel_socket_listen_sync(sioc, s->addr, errp) < 0) { + goto error; + } + s->listen_ioc = sioc; + if (is_waitconnect && + qemu_chr_wait_connected(chr, errp) < 0) { + return; + } + if (!s->ioc) { + s->listen_tag = qio_channel_add_watch( + QIO_CHANNEL(s->listen_ioc), G_IO_IN, + tcp_chr_accept, chr, NULL); + } + } else if (qemu_chr_wait_connected(chr, errp) < 0) { + goto error; + } + } + + return; + +error: + if (sioc) { + object_unref(OBJECT(sioc)); + } +} + +static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, + Error **errp) +{ + bool is_listen = qemu_opt_get_bool(opts, "server", false); + bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true); + bool is_telnet = qemu_opt_get_bool(opts, "telnet", false); + bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true); + int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0); + const char *path = qemu_opt_get(opts, "path"); + const char *host = qemu_opt_get(opts, "host"); + const char *port = qemu_opt_get(opts, "port"); + const char *tls_creds = qemu_opt_get(opts, "tls-creds"); + SocketAddress *addr; + ChardevSocket *sock; + + backend->type = CHARDEV_BACKEND_KIND_SOCKET; + if (!path) { + if (!host) { + error_setg(errp, "chardev: socket: no host given"); + return; + } + if (!port) { + error_setg(errp, "chardev: socket: no port given"); + return; + } + } else { + if (tls_creds) { + error_setg(errp, "TLS can only be used over TCP socket"); + return; + } + } + + sock = backend->u.socket.data = g_new0(ChardevSocket, 1); + qemu_chr_parse_common(opts, qapi_ChardevSocket_base(sock)); + + sock->has_nodelay = true; + sock->nodelay = do_nodelay; + sock->has_server = true; + sock->server = is_listen; + sock->has_telnet = true; + sock->telnet = is_telnet; + sock->has_wait = true; + sock->wait = is_waitconnect; + sock->has_reconnect = true; + sock->reconnect = reconnect; + sock->tls_creds = g_strdup(tls_creds); + + addr = g_new0(SocketAddress, 1); + if (path) { + UnixSocketAddress *q_unix; + addr->type = SOCKET_ADDRESS_KIND_UNIX; + q_unix = addr->u.q_unix.data = g_new0(UnixSocketAddress, 1); + q_unix->path = g_strdup(path); + } else { + addr->type = SOCKET_ADDRESS_KIND_INET; + addr->u.inet.data = g_new(InetSocketAddress, 1); + *addr->u.inet.data = (InetSocketAddress) { + .host = g_strdup(host), + .port = g_strdup(port), + .has_to = qemu_opt_get(opts, "to"), + .to = qemu_opt_get_number(opts, "to", 0), + .has_ipv4 = qemu_opt_get(opts, "ipv4"), + .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0), + .has_ipv6 = qemu_opt_get(opts, "ipv6"), + .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0), + }; + } + sock->addr = addr; +} + +static void char_socket_class_init(ObjectClass *oc, void *data) +{ + ChardevClass *cc = CHARDEV_CLASS(oc); + + cc->parse = qemu_chr_parse_socket; + cc->open = qmp_chardev_open_socket; + cc->chr_wait_connected = tcp_chr_wait_connected; + cc->chr_write = tcp_chr_write; + cc->chr_sync_read = tcp_chr_sync_read; + cc->chr_disconnect = tcp_chr_disconnect; + cc->get_msgfds = tcp_get_msgfds; + cc->set_msgfds = tcp_set_msgfds; + cc->chr_add_client = tcp_chr_add_client; + cc->chr_add_watch = tcp_chr_add_watch; + cc->chr_update_read_handler = tcp_chr_update_read_handler; +} + +static const TypeInfo char_socket_type_info = { + .name = TYPE_CHARDEV_SOCKET, + .parent = TYPE_CHARDEV, + .instance_size = sizeof(SocketChardev), + .instance_finalize = char_socket_finalize, + .class_init = char_socket_class_init, +}; + +static void register_types(void) +{ + type_register_static(&char_socket_type_info); +} + +type_init(register_types); diff --git a/chardev/char-stdio.c b/chardev/char-stdio.c new file mode 100644 index 0000000000..be4a65962c --- /dev/null +++ b/chardev/char-stdio.c @@ -0,0 +1,164 @@ +/* + * 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. + */ +#include "qemu/osdep.h" +#include "qemu/sockets.h" +#include "qapi/error.h" +#include "qemu-common.h" +#include "sysemu/char.h" + +#ifdef _WIN32 +#include "char-win.h" +#include "char-win-stdio.h" +#else +#include <termios.h> +#include "char-fd.h" +#endif + +#ifndef _WIN32 +/* init terminal so that we can grab keys */ +static struct termios oldtty; +static int old_fd0_flags; +static bool stdio_in_use; +static bool stdio_allow_signal; +static bool stdio_echo_state; + +static void term_exit(void) +{ + tcsetattr(0, TCSANOW, &oldtty); + fcntl(0, F_SETFL, old_fd0_flags); +} + +static void qemu_chr_set_echo_stdio(Chardev *chr, bool echo) +{ + struct termios tty; + + stdio_echo_state = echo; + tty = oldtty; + if (!echo) { + tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP + | INLCR | IGNCR | ICRNL | IXON); + tty.c_oflag |= OPOST; + tty.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN); + tty.c_cflag &= ~(CSIZE | PARENB); + tty.c_cflag |= CS8; + tty.c_cc[VMIN] = 1; + tty.c_cc[VTIME] = 0; + } + if (!stdio_allow_signal) { + tty.c_lflag &= ~ISIG; + } + + tcsetattr(0, TCSANOW, &tty); +} + +static void term_stdio_handler(int sig) +{ + /* restore echo after resume from suspend. */ + qemu_chr_set_echo_stdio(NULL, stdio_echo_state); +} + +static void qemu_chr_open_stdio(Chardev *chr, + ChardevBackend *backend, + bool *be_opened, + Error **errp) +{ + ChardevStdio *opts = backend->u.stdio.data; + struct sigaction act; + + if (is_daemonized()) { + error_setg(errp, "cannot use stdio with -daemonize"); + return; + } + + if (stdio_in_use) { + error_setg(errp, "cannot use stdio by multiple character devices"); + return; + } + + stdio_in_use = true; + old_fd0_flags = fcntl(0, F_GETFL); + tcgetattr(0, &oldtty); + qemu_set_nonblock(0); + atexit(term_exit); + + memset(&act, 0, sizeof(act)); + act.sa_handler = term_stdio_handler; + sigaction(SIGCONT, &act, NULL); + + qemu_chr_open_fd(chr, 0, 1); + + if (opts->has_signal) { + stdio_allow_signal = opts->signal; + } + qemu_chr_set_echo_stdio(chr, false); +} +#endif + +static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend, + Error **errp) +{ + ChardevStdio *stdio; + + backend->type = CHARDEV_BACKEND_KIND_STDIO; + stdio = backend->u.stdio.data = g_new0(ChardevStdio, 1); + qemu_chr_parse_common(opts, qapi_ChardevStdio_base(stdio)); + stdio->has_signal = true; + stdio->signal = qemu_opt_get_bool(opts, "signal", true); +} + +static void char_stdio_class_init(ObjectClass *oc, void *data) +{ + ChardevClass *cc = CHARDEV_CLASS(oc); + + cc->parse = qemu_chr_parse_stdio; +#ifndef _WIN32 + cc->open = qemu_chr_open_stdio; + cc->chr_set_echo = qemu_chr_set_echo_stdio; +#endif +} + +static void char_stdio_finalize(Object *obj) +{ +#ifndef _WIN32 + term_exit(); +#endif +} + +static const TypeInfo char_stdio_type_info = { + .name = TYPE_CHARDEV_STDIO, +#ifdef _WIN32 + .parent = TYPE_CHARDEV_WIN_STDIO, +#else + .parent = TYPE_CHARDEV_FD, +#endif + .instance_finalize = char_stdio_finalize, + .class_init = char_stdio_class_init, +}; + +static void register_types(void) +{ + type_register_static(&char_stdio_type_info); +} + +type_init(register_types); diff --git a/chardev/char-udp.c b/chardev/char-udp.c new file mode 100644 index 0000000000..2c6c7ddd73 --- /dev/null +++ b/chardev/char-udp.c @@ -0,0 +1,233 @@ +/* + * 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. + */ +#include "qemu/osdep.h" +#include "sysemu/char.h" +#include "io/channel-socket.h" +#include "qapi/error.h" + +#include "char-io.h" + +/***********************************************************/ +/* UDP Net console */ + +typedef struct { + Chardev parent; + QIOChannel *ioc; + uint8_t buf[CHR_READ_BUF_LEN]; + int bufcnt; + int bufptr; + int max_size; +} UdpChardev; + +#define UDP_CHARDEV(obj) OBJECT_CHECK(UdpChardev, (obj), TYPE_CHARDEV_UDP) + +/* Called with chr_write_lock held. */ +static int udp_chr_write(Chardev *chr, const uint8_t *buf, int len) +{ + UdpChardev *s = UDP_CHARDEV(chr); + + return qio_channel_write( + s->ioc, (const char *)buf, len, NULL); +} + +static int udp_chr_read_poll(void *opaque) +{ + Chardev *chr = CHARDEV(opaque); + UdpChardev *s = UDP_CHARDEV(opaque); + + s->max_size = qemu_chr_be_can_write(chr); + + /* If there were any stray characters in the queue process them + * first + */ + while (s->max_size > 0 && s->bufptr < s->bufcnt) { + qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); + s->bufptr++; + s->max_size = qemu_chr_be_can_write(chr); + } + return s->max_size; +} + +static gboolean udp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque) +{ + Chardev *chr = CHARDEV(opaque); + UdpChardev *s = UDP_CHARDEV(opaque); + ssize_t ret; + + if (s->max_size == 0) { + return TRUE; + } + ret = qio_channel_read( + s->ioc, (char *)s->buf, sizeof(s->buf), NULL); + if (ret <= 0) { + remove_fd_in_watch(chr); + return FALSE; + } + s->bufcnt = ret; + + s->bufptr = 0; + while (s->max_size > 0 && s->bufptr < s->bufcnt) { + qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); + s->bufptr++; + s->max_size = qemu_chr_be_can_write(chr); + } + + return TRUE; +} + +static void udp_chr_update_read_handler(Chardev *chr, + GMainContext *context) +{ + UdpChardev *s = UDP_CHARDEV(chr); + + remove_fd_in_watch(chr); + if (s->ioc) { + chr->fd_in_tag = io_add_watch_poll(chr, s->ioc, + udp_chr_read_poll, + udp_chr_read, chr, + context); + } +} + +static void char_udp_finalize(Object *obj) +{ + Chardev *chr = CHARDEV(obj); + UdpChardev *s = UDP_CHARDEV(obj); + + remove_fd_in_watch(chr); + if (s->ioc) { + object_unref(OBJECT(s->ioc)); + } + qemu_chr_be_event(chr, CHR_EVENT_CLOSED); +} + +static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend, + Error **errp) +{ + const char *host = qemu_opt_get(opts, "host"); + const char *port = qemu_opt_get(opts, "port"); + const char *localaddr = qemu_opt_get(opts, "localaddr"); + const char *localport = qemu_opt_get(opts, "localport"); + bool has_local = false; + SocketAddress *addr; + ChardevUdp *udp; + + backend->type = CHARDEV_BACKEND_KIND_UDP; + if (host == NULL || strlen(host) == 0) { + host = "localhost"; + } + if (port == NULL || strlen(port) == 0) { + error_setg(errp, "chardev: udp: remote port not specified"); + return; + } + if (localport == NULL || strlen(localport) == 0) { + localport = "0"; + } else { + has_local = true; + } + if (localaddr == NULL || strlen(localaddr) == 0) { + localaddr = ""; + } else { + has_local = true; + } + + udp = backend->u.udp.data = g_new0(ChardevUdp, 1); + qemu_chr_parse_common(opts, qapi_ChardevUdp_base(udp)); + + addr = g_new0(SocketAddress, 1); + addr->type = SOCKET_ADDRESS_KIND_INET; + addr->u.inet.data = g_new(InetSocketAddress, 1); + *addr->u.inet.data = (InetSocketAddress) { + .host = g_strdup(host), + .port = g_strdup(port), + .has_ipv4 = qemu_opt_get(opts, "ipv4"), + .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0), + .has_ipv6 = qemu_opt_get(opts, "ipv6"), + .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0), + }; + udp->remote = addr; + + if (has_local) { + udp->has_local = true; + addr = g_new0(SocketAddress, 1); + addr->type = SOCKET_ADDRESS_KIND_INET; + addr->u.inet.data = g_new(InetSocketAddress, 1); + *addr->u.inet.data = (InetSocketAddress) { + .host = g_strdup(localaddr), + .port = g_strdup(localport), + }; + udp->local = addr; + } +} + +static void qmp_chardev_open_udp(Chardev *chr, + ChardevBackend *backend, + bool *be_opened, + Error **errp) +{ + ChardevUdp *udp = backend->u.udp.data; + QIOChannelSocket *sioc = qio_channel_socket_new(); + char *name; + UdpChardev *s = UDP_CHARDEV(chr); + + if (qio_channel_socket_dgram_sync(sioc, + udp->local, udp->remote, + errp) < 0) { + object_unref(OBJECT(sioc)); + return; + } + + name = g_strdup_printf("chardev-udp-%s", chr->label); + qio_channel_set_name(QIO_CHANNEL(sioc), name); + g_free(name); + + s->ioc = QIO_CHANNEL(sioc); + /* be isn't opened until we get a connection */ + *be_opened = false; +} + +static void char_udp_class_init(ObjectClass *oc, void *data) +{ + ChardevClass *cc = CHARDEV_CLASS(oc); + + cc->parse = qemu_chr_parse_udp; + cc->open = qmp_chardev_open_udp; + cc->chr_write = udp_chr_write; + cc->chr_update_read_handler = udp_chr_update_read_handler; +} + +static const TypeInfo char_udp_type_info = { + .name = TYPE_CHARDEV_UDP, + .parent = TYPE_CHARDEV, + .instance_size = sizeof(UdpChardev), + .instance_finalize = char_udp_finalize, + .class_init = char_udp_class_init, +}; + +static void register_types(void) +{ + type_register_static(&char_udp_type_info); +} + +type_init(register_types); diff --git a/chardev/char-win-stdio.c b/chardev/char-win-stdio.c new file mode 100644 index 0000000000..eb44afc17a --- /dev/null +++ b/chardev/char-win-stdio.c @@ -0,0 +1,266 @@ +/* + * 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. + */ +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "char-win.h" +#include "char-win-stdio.h" + +typedef struct { + Chardev parent; + HANDLE hStdIn; + HANDLE hInputReadyEvent; + HANDLE hInputDoneEvent; + HANDLE hInputThread; + uint8_t win_stdio_buf; +} WinStdioChardev; + +#define WIN_STDIO_CHARDEV(obj) \ + OBJECT_CHECK(WinStdioChardev, (obj), TYPE_CHARDEV_WIN_STDIO) + +static void win_stdio_wait_func(void *opaque) +{ + Chardev *chr = CHARDEV(opaque); + WinStdioChardev *stdio = WIN_STDIO_CHARDEV(opaque); + INPUT_RECORD buf[4]; + int ret; + DWORD dwSize; + int i; + + ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize); + + if (!ret) { + /* Avoid error storm */ + qemu_del_wait_object(stdio->hStdIn, NULL, NULL); + return; + } + + for (i = 0; i < dwSize; i++) { + KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent; + + if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) { + int j; + if (kev->uChar.AsciiChar != 0) { + for (j = 0; j < kev->wRepeatCount; j++) { + if (qemu_chr_be_can_write(chr)) { + uint8_t c = kev->uChar.AsciiChar; + qemu_chr_be_write(chr, &c, 1); + } + } + } + } + } +} + +static DWORD WINAPI win_stdio_thread(LPVOID param) +{ + WinStdioChardev *stdio = WIN_STDIO_CHARDEV(param); + int ret; + DWORD dwSize; + + while (1) { + + /* Wait for one byte */ + ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL); + + /* Exit in case of error, continue if nothing read */ + if (!ret) { + break; + } + if (!dwSize) { + continue; + } + + /* Some terminal emulator returns \r\n for Enter, just pass \n */ + if (stdio->win_stdio_buf == '\r') { + continue; + } + + /* Signal the main thread and wait until the byte was eaten */ + if (!SetEvent(stdio->hInputReadyEvent)) { + break; + } + if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE) + != WAIT_OBJECT_0) { + break; + } + } + + qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL); + return 0; +} + +static void win_stdio_thread_wait_func(void *opaque) +{ + Chardev *chr = CHARDEV(opaque); + WinStdioChardev *stdio = WIN_STDIO_CHARDEV(opaque); + + if (qemu_chr_be_can_write(chr)) { + qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1); + } + + SetEvent(stdio->hInputDoneEvent); +} + +static void qemu_chr_set_echo_win_stdio(Chardev *chr, bool echo) +{ + WinStdioChardev *stdio = WIN_STDIO_CHARDEV(chr); + DWORD dwMode = 0; + + GetConsoleMode(stdio->hStdIn, &dwMode); + + if (echo) { + SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT); + } else { + SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT); + } +} + +static void qemu_chr_open_stdio(Chardev *chr, + ChardevBackend *backend, + bool *be_opened, + Error **errp) +{ + WinStdioChardev *stdio = WIN_STDIO_CHARDEV(chr); + DWORD dwMode; + int is_console = 0; + + stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE); + if (stdio->hStdIn == INVALID_HANDLE_VALUE) { + error_setg(errp, "cannot open stdio: invalid handle"); + return; + } + + is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0; + + if (is_console) { + if (qemu_add_wait_object(stdio->hStdIn, + win_stdio_wait_func, chr)) { + error_setg(errp, "qemu_add_wait_object: failed"); + goto err1; + } + } else { + DWORD dwId; + + stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL); + stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL); + if (stdio->hInputReadyEvent == INVALID_HANDLE_VALUE + || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) { + error_setg(errp, "cannot create event"); + goto err2; + } + if (qemu_add_wait_object(stdio->hInputReadyEvent, + win_stdio_thread_wait_func, chr)) { + error_setg(errp, "qemu_add_wait_object: failed"); + goto err2; + } + stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread, + chr, 0, &dwId); + + if (stdio->hInputThread == INVALID_HANDLE_VALUE) { + error_setg(errp, "cannot create stdio thread"); + goto err3; + } + } + + dwMode |= ENABLE_LINE_INPUT; + + if (is_console) { + /* set the terminal in raw mode */ + /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */ + dwMode |= ENABLE_PROCESSED_INPUT; + } + + SetConsoleMode(stdio->hStdIn, dwMode); + + qemu_chr_set_echo_win_stdio(chr, false); + + return; + +err3: + qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL); +err2: + CloseHandle(stdio->hInputReadyEvent); + CloseHandle(stdio->hInputDoneEvent); +err1: + qemu_del_wait_object(stdio->hStdIn, NULL, NULL); +} + +static void char_win_stdio_finalize(Object *obj) +{ + WinStdioChardev *stdio = WIN_STDIO_CHARDEV(obj); + + if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) { + CloseHandle(stdio->hInputReadyEvent); + } + if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) { + CloseHandle(stdio->hInputDoneEvent); + } + if (stdio->hInputThread != INVALID_HANDLE_VALUE) { + TerminateThread(stdio->hInputThread, 0); + } +} + +static int win_stdio_write(Chardev *chr, const uint8_t *buf, int len) +{ + HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); + DWORD dwSize; + int len1; + + len1 = len; + + while (len1 > 0) { + if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) { + break; + } + buf += dwSize; + len1 -= dwSize; + } + + return len - len1; +} + +static void char_win_stdio_class_init(ObjectClass *oc, void *data) +{ + ChardevClass *cc = CHARDEV_CLASS(oc); + + cc->open = qemu_chr_open_stdio; + cc->chr_write = win_stdio_write; + cc->chr_set_echo = qemu_chr_set_echo_win_stdio; +} + +static const TypeInfo char_win_stdio_type_info = { + .name = TYPE_CHARDEV_WIN_STDIO, + .parent = TYPE_CHARDEV, + .instance_size = sizeof(WinStdioChardev), + .instance_finalize = char_win_stdio_finalize, + .class_init = char_win_stdio_class_init, + .abstract = true, +}; + +static void register_types(void) +{ + type_register_static(&char_win_stdio_type_info); +} + +type_init(register_types); diff --git a/chardev/char-win-stdio.h b/chardev/char-win-stdio.h new file mode 100644 index 0000000000..d7314f734d --- /dev/null +++ b/chardev/char-win-stdio.h @@ -0,0 +1,29 @@ +/* + * 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. + */ +#ifndef CHAR_WIN_STDIO_H +#define CHAR_WIN_STDIO_H + +#define TYPE_CHARDEV_WIN_STDIO "chardev-win-stdio" + +#endif /* CHAR_WIN_STDIO_H */ diff --git a/chardev/char-win.c b/chardev/char-win.c new file mode 100644 index 0000000000..e4b6957ded --- /dev/null +++ b/chardev/char-win.c @@ -0,0 +1,265 @@ +/* + * 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. + */ +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "qapi/error.h" +#include "char-win.h" + +static void win_chr_readfile(Chardev *chr) +{ + WinChardev *s = WIN_CHARDEV(chr); + + int ret, err; + uint8_t buf[CHR_READ_BUF_LEN]; + DWORD size; + + ZeroMemory(&s->orecv, sizeof(s->orecv)); + s->orecv.hEvent = s->hrecv; + ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); + if (!ret) { + err = GetLastError(); + if (err == ERROR_IO_PENDING) { + ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); + } + } + + if (size > 0) { + qemu_chr_be_write(chr, buf, size); + } +} + +static void win_chr_read(Chardev *chr) +{ + WinChardev *s = WIN_CHARDEV(chr); + + if (s->len > s->max_size) { + s->len = s->max_size; + } + if (s->len == 0) { + return; + } + + win_chr_readfile(chr); +} + +static int win_chr_read_poll(Chardev *chr) +{ + WinChardev *s = WIN_CHARDEV(chr); + + s->max_size = qemu_chr_be_can_write(chr); + return s->max_size; +} + +static int win_chr_poll(void *opaque) +{ + Chardev *chr = CHARDEV(opaque); + WinChardev *s = WIN_CHARDEV(opaque); + COMSTAT status; + DWORD comerr; + + ClearCommError(s->hcom, &comerr, &status); + if (status.cbInQue > 0) { + s->len = status.cbInQue; + win_chr_read_poll(chr); + win_chr_read(chr); + return 1; + } + return 0; +} + +int win_chr_init(Chardev *chr, const char *filename, Error **errp) +{ + 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; + } + + s->hcom = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, + OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); + if (s->hcom == INVALID_HANDLE_VALUE) { + error_setg(errp, "Failed CreateFile (%lu)", GetLastError()); + s->hcom = NULL; + goto fail; + } + + if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) { + 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); + + if (!SetCommState(s->hcom, &comcfg.dcb)) { + error_setg(errp, "Failed SetCommState"); + goto fail; + } + + if (!SetCommMask(s->hcom, EV_ERR)) { + error_setg(errp, "Failed SetCommMask"); + goto fail; + } + + cto.ReadIntervalTimeout = MAXDWORD; + if (!SetCommTimeouts(s->hcom, &cto)) { + error_setg(errp, "Failed SetCommTimeouts"); + goto fail; + } + + if (!ClearCommError(s->hcom, &err, &comstat)) { + error_setg(errp, "Failed ClearCommError"); + goto fail; + } + qemu_add_polling_cb(win_chr_poll, chr); + return 0; + + fail: + return -1; +} + +int win_chr_pipe_poll(void *opaque) +{ + Chardev *chr = CHARDEV(opaque); + WinChardev *s = WIN_CHARDEV(opaque); + DWORD size; + + PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); + if (size > 0) { + s->len = size; + win_chr_read_poll(chr); + win_chr_read(chr); + 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) { + ret = WriteFile(s->hcom, buf, len, &size, &s->osend); + } else { + ret = WriteFile(s->hcom, buf, len, &size, NULL); + } + if (!ret) { + err = GetLastError(); + if (err == ERROR_IO_PENDING) { + ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); + 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->skip_free) { + return; + } + + if (s->hsend) { + CloseHandle(s->hsend); + } + if (s->hrecv) { + CloseHandle(s->hrecv); + } + if (s->hcom) { + CloseHandle(s->hcom); + } + if (s->fpipe) { + qemu_del_polling_cb(win_chr_pipe_poll, chr); + } else { + qemu_del_polling_cb(win_chr_poll, chr); + } + + qemu_chr_be_event(chr, CHR_EVENT_CLOSED); +} + +void qemu_chr_open_win_file(Chardev *chr, HANDLE fd_out) +{ + WinChardev *s = WIN_CHARDEV(chr); + + s->skip_free = true; + s->hcom = fd_out; +} + +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); diff --git a/chardev/char-win.h b/chardev/char-win.h new file mode 100644 index 0000000000..d78a7d7972 --- /dev/null +++ b/chardev/char-win.h @@ -0,0 +1,53 @@ +/* + * 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. + */ +#ifndef CHAR_WIN_H +#define CHAR_WIN_H + +#include "sysemu/char.h" + +typedef struct { + Chardev parent; + int max_size; + HANDLE hcom, hrecv, hsend; + OVERLAPPED orecv; + BOOL fpipe; + DWORD len; + + /* Protected by the Chardev chr_write_lock. */ + OVERLAPPED osend; + /* FIXME: file/console do not finalize */ + bool skip_free; +} WinChardev; + +#define NSENDBUF 2048 +#define NRECVBUF 2048 + +#define TYPE_CHARDEV_WIN "chardev-win" +#define WIN_CHARDEV(obj) OBJECT_CHECK(WinChardev, (obj), TYPE_CHARDEV_WIN) + +void qemu_chr_open_win_file(Chardev *chr, HANDLE fd_out); +int win_chr_init(Chardev *chr, const char *filename, Error **errp); +int win_chr_pipe_poll(void *opaque); + +#endif /* CHAR_WIN_H */ diff --git a/chardev/char.c b/chardev/char.c new file mode 100644 index 0000000000..abd525f75e --- /dev/null +++ b/chardev/char.c @@ -0,0 +1,1334 @@ +/* + * 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. + */ +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "qemu/cutils.h" +#include "monitor/monitor.h" +#include "sysemu/sysemu.h" +#include "qemu/config-file.h" +#include "qemu/error-report.h" +#include "sysemu/char.h" +#include "qmp-commands.h" +#include "qapi-visit.h" +#include "sysemu/replay.h" +#include "qemu/help_option.h" + +#include "char-mux.h" +#include "char-io.h" +#include "char-parallel.h" +#include "char-serial.h" + +/***********************************************************/ +/* character device */ + +static QTAILQ_HEAD(ChardevHead, Chardev) chardevs = + QTAILQ_HEAD_INITIALIZER(chardevs); + +void qemu_chr_be_event(Chardev *s, int event) +{ + CharBackend *be = s->be; + + /* Keep track if the char device is open */ + switch (event) { + case CHR_EVENT_OPENED: + s->be_open = 1; + break; + case CHR_EVENT_CLOSED: + s->be_open = 0; + break; + } + + if (!be || !be->chr_event) { + return; + } + + be->chr_event(be->opaque, event); +} + +void qemu_chr_be_generic_open(Chardev *s) +{ + qemu_chr_be_event(s, CHR_EVENT_OPENED); +} + + +/* Not reporting errors from writing to logfile, as logs are + * defined to be "best effort" only */ +static void qemu_chr_fe_write_log(Chardev *s, + const uint8_t *buf, size_t len) +{ + size_t done = 0; + ssize_t ret; + + if (s->logfd < 0) { + return; + } + + while (done < len) { + retry: + ret = write(s->logfd, buf + done, len - done); + if (ret == -1 && errno == EAGAIN) { + g_usleep(100); + goto retry; + } + + if (ret <= 0) { + return; + } + done += ret; + } +} + +static int qemu_chr_fe_write_buffer(Chardev *s, + const uint8_t *buf, int len, int *offset) +{ + ChardevClass *cc = CHARDEV_GET_CLASS(s); + int res = 0; + *offset = 0; + + qemu_mutex_lock(&s->chr_write_lock); + while (*offset < len) { + retry: + res = cc->chr_write(s, buf + *offset, len - *offset); + if (res < 0 && errno == EAGAIN) { + g_usleep(100); + goto retry; + } + + if (res <= 0) { + break; + } + + *offset += res; + } + if (*offset > 0) { + qemu_chr_fe_write_log(s, buf, *offset); + } + qemu_mutex_unlock(&s->chr_write_lock); + + return res; +} + +static bool qemu_chr_replay(Chardev *chr) +{ + return qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_REPLAY); +} + +int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len) +{ + Chardev *s = be->chr; + ChardevClass *cc; + int ret; + + if (!s) { + return 0; + } + + if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) { + int offset; + replay_char_write_event_load(&ret, &offset); + assert(offset <= len); + qemu_chr_fe_write_buffer(s, buf, offset, &offset); + return ret; + } + + cc = CHARDEV_GET_CLASS(s); + qemu_mutex_lock(&s->chr_write_lock); + ret = cc->chr_write(s, buf, len); + + if (ret > 0) { + qemu_chr_fe_write_log(s, buf, ret); + } + + qemu_mutex_unlock(&s->chr_write_lock); + + if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { + replay_char_write_event_save(ret, ret < 0 ? 0 : ret); + } + + return ret; +} + +int qemu_chr_write_all(Chardev *s, const uint8_t *buf, int len) +{ + int offset; + int res; + + if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) { + replay_char_write_event_load(&res, &offset); + assert(offset <= len); + qemu_chr_fe_write_buffer(s, buf, offset, &offset); + return res; + } + + res = qemu_chr_fe_write_buffer(s, buf, len, &offset); + + if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { + replay_char_write_event_save(res, offset); + } + + if (res < 0) { + return res; + } + return offset; +} + +int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len) +{ + Chardev *s = be->chr; + + if (!s) { + return 0; + } + + return qemu_chr_write_all(s, buf, len); +} + +int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len) +{ + Chardev *s = be->chr; + int offset = 0, counter = 10; + int res; + + if (!s || !CHARDEV_GET_CLASS(s)->chr_sync_read) { + return 0; + } + + if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) { + return replay_char_read_all_load(buf); + } + + while (offset < len) { + retry: + res = CHARDEV_GET_CLASS(s)->chr_sync_read(s, buf + offset, + len - offset); + if (res == -1 && errno == EAGAIN) { + g_usleep(100); + goto retry; + } + + if (res == 0) { + break; + } + + if (res < 0) { + if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { + replay_char_read_all_save_error(res); + } + return res; + } + + offset += res; + + if (!counter--) { + break; + } + } + + if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { + replay_char_read_all_save_buf(buf, offset); + } + return offset; +} + +int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg) +{ + Chardev *s = be->chr; + int res; + + if (!s || !CHARDEV_GET_CLASS(s)->chr_ioctl || qemu_chr_replay(s)) { + res = -ENOTSUP; + } else { + res = CHARDEV_GET_CLASS(s)->chr_ioctl(s, cmd, arg); + } + + return res; +} + +int qemu_chr_be_can_write(Chardev *s) +{ + CharBackend *be = s->be; + + if (!be || !be->chr_can_read) { + return 0; + } + + return be->chr_can_read(be->opaque); +} + +void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len) +{ + CharBackend *be = s->be; + + if (be && be->chr_read) { + be->chr_read(be->opaque, buf, len); + } +} + +void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len) +{ + if (qemu_chr_replay(s)) { + if (replay_mode == REPLAY_MODE_PLAY) { + return; + } + replay_chr_be_write(s, buf, len); + } else { + qemu_chr_be_write_impl(s, buf, len); + } +} + +int qemu_chr_fe_get_msgfd(CharBackend *be) +{ + Chardev *s = be->chr; + int fd; + int res = (qemu_chr_fe_get_msgfds(be, &fd, 1) == 1) ? fd : -1; + if (s && qemu_chr_replay(s)) { + error_report("Replay: get msgfd is not supported " + "for serial devices yet"); + exit(1); + } + return res; +} + +int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int len) +{ + Chardev *s = be->chr; + + if (!s) { + return -1; + } + + return CHARDEV_GET_CLASS(s)->get_msgfds ? + CHARDEV_GET_CLASS(s)->get_msgfds(s, fds, len) : -1; +} + +int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num) +{ + Chardev *s = be->chr; + + if (!s) { + return -1; + } + + return CHARDEV_GET_CLASS(s)->set_msgfds ? + CHARDEV_GET_CLASS(s)->set_msgfds(s, fds, num) : -1; +} + +int qemu_chr_add_client(Chardev *s, int fd) +{ + return CHARDEV_GET_CLASS(s)->chr_add_client ? + CHARDEV_GET_CLASS(s)->chr_add_client(s, fd) : -1; +} + +void qemu_chr_fe_accept_input(CharBackend *be) +{ + Chardev *s = be->chr; + + if (!s) { + return; + } + + if (CHARDEV_GET_CLASS(s)->chr_accept_input) { + CHARDEV_GET_CLASS(s)->chr_accept_input(s); + } + qemu_notify_event(); +} + +void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...) +{ + char buf[CHR_READ_BUF_LEN]; + va_list ap; + va_start(ap, fmt); + vsnprintf(buf, sizeof(buf), fmt, ap); + /* XXX this blocks entire thread. Rewrite to use + * qemu_chr_fe_write and background I/O callbacks */ + qemu_chr_fe_write_all(be, (uint8_t *)buf, strlen(buf)); + va_end(ap); +} + +static void qemu_char_open(Chardev *chr, ChardevBackend *backend, + bool *be_opened, Error **errp) +{ + ChardevClass *cc = CHARDEV_GET_CLASS(chr); + /* Any ChardevCommon member would work */ + ChardevCommon *common = backend ? backend->u.null.data : NULL; + + if (common && common->has_logfile) { + int flags = O_WRONLY | O_CREAT; + if (common->has_logappend && + common->logappend) { + flags |= O_APPEND; + } else { + flags |= O_TRUNC; + } + chr->logfd = qemu_open(common->logfile, flags, 0666); + if (chr->logfd < 0) { + error_setg_errno(errp, errno, + "Unable to open logfile %s", + common->logfile); + return; + } + } + + if (cc->open) { + cc->open(chr, backend, be_opened, errp); + } +} + +static void char_init(Object *obj) +{ + Chardev *chr = CHARDEV(obj); + + chr->logfd = -1; + qemu_mutex_init(&chr->chr_write_lock); +} + +static int null_chr_write(Chardev *chr, const uint8_t *buf, int len) +{ + return len; +} + +static void char_class_init(ObjectClass *oc, void *data) +{ + ChardevClass *cc = CHARDEV_CLASS(oc); + + cc->chr_write = null_chr_write; +} + +static void char_finalize(Object *obj) +{ + Chardev *chr = CHARDEV(obj); + + if (chr->be) { + chr->be->chr = NULL; + } + g_free(chr->filename); + g_free(chr->label); + if (chr->logfd != -1) { + close(chr->logfd); + } + qemu_mutex_destroy(&chr->chr_write_lock); +} + +static const TypeInfo char_type_info = { + .name = TYPE_CHARDEV, + .parent = TYPE_OBJECT, + .instance_size = sizeof(Chardev), + .instance_init = char_init, + .instance_finalize = char_finalize, + .abstract = true, + .class_size = sizeof(ChardevClass), + .class_init = char_class_init, +}; + +/** + * Called after processing of default and command-line-specified + * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached + * to a mux chardev. This is done here to ensure that + * output/prompts/banners are only displayed for the FE that has + * focus when initial command-line processing/machine init is + * completed. + * + * After this point, any new FE attached to any new or existing + * mux will receive CHR_EVENT_OPENED notifications for the BE + * immediately. + */ +static void muxes_realize_done(Notifier *notifier, void *unused) +{ + Chardev *chr; + + QTAILQ_FOREACH(chr, &chardevs, next) { + if (CHARDEV_IS_MUX(chr)) { + MuxChardev *d = MUX_CHARDEV(chr); + int i; + + /* send OPENED to all already-attached FEs */ + for (i = 0; i < d->mux_cnt; i++) { + mux_chr_send_event(d, i, CHR_EVENT_OPENED); + } + /* mark mux as OPENED so any new FEs will immediately receive + * OPENED event + */ + qemu_chr_be_generic_open(chr); + } + } + muxes_realized = true; +} + +static Notifier muxes_realize_notify = { + .notify = muxes_realize_done, +}; + +Chardev *qemu_chr_fe_get_driver(CharBackend *be) +{ + return be->chr; +} + +bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp) +{ + int tag = 0; + + if (CHARDEV_IS_MUX(s)) { + MuxChardev *d = MUX_CHARDEV(s); + + if (d->mux_cnt >= MAX_MUX) { + goto unavailable; + } + + d->backends[d->mux_cnt] = b; + tag = d->mux_cnt++; + } else if (s->be) { + goto unavailable; + } else { + s->be = b; + } + + b->fe_open = false; + b->tag = tag; + b->chr = s; + return true; + +unavailable: + error_setg(errp, QERR_DEVICE_IN_USE, s->label); + return false; +} + +static bool qemu_chr_is_busy(Chardev *s) +{ + if (CHARDEV_IS_MUX(s)) { + MuxChardev *d = MUX_CHARDEV(s); + return d->mux_cnt >= 0; + } else { + return s->be != NULL; + } +} + +void qemu_chr_fe_deinit(CharBackend *b) +{ + assert(b); + + if (b->chr) { + qemu_chr_fe_set_handlers(b, NULL, NULL, NULL, NULL, NULL, true); + if (b->chr->be == b) { + b->chr->be = NULL; + } + if (CHARDEV_IS_MUX(b->chr)) { + MuxChardev *d = MUX_CHARDEV(b->chr); + d->backends[b->tag] = NULL; + } + b->chr = NULL; + } +} + +void qemu_chr_fe_set_handlers(CharBackend *b, + IOCanReadHandler *fd_can_read, + IOReadHandler *fd_read, + IOEventHandler *fd_event, + void *opaque, + GMainContext *context, + bool set_open) +{ + Chardev *s; + ChardevClass *cc; + int fe_open; + + s = b->chr; + if (!s) { + return; + } + + cc = CHARDEV_GET_CLASS(s); + if (!opaque && !fd_can_read && !fd_read && !fd_event) { + fe_open = 0; + remove_fd_in_watch(s); + } else { + fe_open = 1; + } + b->chr_can_read = fd_can_read; + b->chr_read = fd_read; + b->chr_event = fd_event; + b->opaque = opaque; + if (cc->chr_update_read_handler) { + cc->chr_update_read_handler(s, context); + } + + if (set_open) { + qemu_chr_fe_set_open(b, fe_open); + } + + if (fe_open) { + qemu_chr_fe_take_focus(b); + /* We're connecting to an already opened device, so let's make sure we + also get the open event */ + if (s->be_open) { + qemu_chr_be_generic_open(s); + } + } + + if (CHARDEV_IS_MUX(s)) { + mux_chr_set_handlers(s, context); + } +} + +void qemu_chr_fe_take_focus(CharBackend *b) +{ + if (!b->chr) { + return; + } + + if (CHARDEV_IS_MUX(b->chr)) { + mux_set_focus(b->chr, b->tag); + } +} + +int qemu_chr_wait_connected(Chardev *chr, Error **errp) +{ + ChardevClass *cc = CHARDEV_GET_CLASS(chr); + + if (cc->chr_wait_connected) { + return cc->chr_wait_connected(chr, errp); + } + + return 0; +} + +int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp) +{ + if (!be->chr) { + error_setg(errp, "missing associated backend"); + return -1; + } + + return qemu_chr_wait_connected(be->chr, errp); +} + +QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename) +{ + char host[65], port[33], width[8], height[8]; + int pos; + const char *p; + QemuOpts *opts; + Error *local_err = NULL; + + opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err); + if (local_err) { + error_report_err(local_err); + return NULL; + } + + if (strstart(filename, "mon:", &p)) { + filename = p; + qemu_opt_set(opts, "mux", "on", &error_abort); + if (strcmp(filename, "stdio") == 0) { + /* Monitor is muxed to stdio: do not exit on Ctrl+C by default + * but pass it to the guest. Handle this only for compat syntax, + * for -chardev syntax we have special option for this. + * This is what -nographic did, redirecting+muxing serial+monitor + * to stdio causing Ctrl+C to be passed to guest. */ + qemu_opt_set(opts, "signal", "off", &error_abort); + } + } + + if (strcmp(filename, "null") == 0 || + strcmp(filename, "pty") == 0 || + strcmp(filename, "msmouse") == 0 || + strcmp(filename, "braille") == 0 || + strcmp(filename, "testdev") == 0 || + strcmp(filename, "stdio") == 0) { + qemu_opt_set(opts, "backend", filename, &error_abort); + return opts; + } + if (strstart(filename, "vc", &p)) { + qemu_opt_set(opts, "backend", "vc", &error_abort); + if (*p == ':') { + if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) { + /* pixels */ + qemu_opt_set(opts, "width", width, &error_abort); + qemu_opt_set(opts, "height", height, &error_abort); + } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) { + /* chars */ + qemu_opt_set(opts, "cols", width, &error_abort); + qemu_opt_set(opts, "rows", height, &error_abort); + } else { + goto fail; + } + } + return opts; + } + if (strcmp(filename, "con:") == 0) { + qemu_opt_set(opts, "backend", "console", &error_abort); + return opts; + } + if (strstart(filename, "COM", NULL)) { + qemu_opt_set(opts, "backend", "serial", &error_abort); + qemu_opt_set(opts, "path", filename, &error_abort); + return opts; + } + if (strstart(filename, "file:", &p)) { + qemu_opt_set(opts, "backend", "file", &error_abort); + qemu_opt_set(opts, "path", p, &error_abort); + return opts; + } + if (strstart(filename, "pipe:", &p)) { + qemu_opt_set(opts, "backend", "pipe", &error_abort); + qemu_opt_set(opts, "path", p, &error_abort); + return opts; + } + if (strstart(filename, "tcp:", &p) || + strstart(filename, "telnet:", &p)) { + if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { + host[0] = 0; + if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) + goto fail; + } + qemu_opt_set(opts, "backend", "socket", &error_abort); + qemu_opt_set(opts, "host", host, &error_abort); + qemu_opt_set(opts, "port", port, &error_abort); + if (p[pos] == ',') { + qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err); + if (local_err) { + error_report_err(local_err); + goto fail; + } + } + if (strstart(filename, "telnet:", &p)) + qemu_opt_set(opts, "telnet", "on", &error_abort); + return opts; + } + if (strstart(filename, "udp:", &p)) { + qemu_opt_set(opts, "backend", "udp", &error_abort); + if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) { + host[0] = 0; + if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) { + goto fail; + } + } + qemu_opt_set(opts, "host", host, &error_abort); + qemu_opt_set(opts, "port", port, &error_abort); + if (p[pos] == '@') { + p += pos + 1; + if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { + host[0] = 0; + if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) { + goto fail; + } + } + qemu_opt_set(opts, "localaddr", host, &error_abort); + qemu_opt_set(opts, "localport", port, &error_abort); + } + return opts; + } + if (strstart(filename, "unix:", &p)) { + qemu_opt_set(opts, "backend", "socket", &error_abort); + qemu_opts_do_parse(opts, p, "path", &local_err); + if (local_err) { + error_report_err(local_err); + goto fail; + } + return opts; + } + if (strstart(filename, "/dev/parport", NULL) || + strstart(filename, "/dev/ppi", NULL)) { + qemu_opt_set(opts, "backend", "parport", &error_abort); + qemu_opt_set(opts, "path", filename, &error_abort); + return opts; + } + if (strstart(filename, "/dev/", NULL)) { + qemu_opt_set(opts, "backend", "tty", &error_abort); + qemu_opt_set(opts, "path", filename, &error_abort); + return opts; + } + +fail: + qemu_opts_del(opts); + return NULL; +} + +void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend) +{ + const char *logfile = qemu_opt_get(opts, "logfile"); + + backend->has_logfile = logfile != NULL; + backend->logfile = logfile ? g_strdup(logfile) : NULL; + + backend->has_logappend = true; + backend->logappend = qemu_opt_get_bool(opts, "logappend", false); +} + +static const ChardevClass *char_get_class(const char *driver, Error **errp) +{ + ObjectClass *oc; + const ChardevClass *cc; + char *typename = g_strdup_printf("chardev-%s", driver); + + oc = object_class_by_name(typename); + g_free(typename); + + if (!object_class_dynamic_cast(oc, TYPE_CHARDEV)) { + error_setg(errp, "'%s' is not a valid char driver name", driver); + return NULL; + } + + if (object_class_is_abstract(oc)) { + error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver", + "abstract device type"); + return NULL; + } + + cc = CHARDEV_CLASS(oc); + if (cc->internal) { + error_setg(errp, "'%s' is not a valid char driver name", driver); + return NULL; + } + + return cc; +} + +static Chardev *qemu_chardev_add(const char *id, const char *typename, + ChardevBackend *backend, Error **errp) +{ + Chardev *chr; + + chr = qemu_chr_find(id); + if (chr) { + error_setg(errp, "Chardev '%s' already exists", id); + return NULL; + } + + chr = qemu_chardev_new(id, typename, backend, errp); + if (!chr) { + return NULL; + } + + QTAILQ_INSERT_TAIL(&chardevs, chr, next); + return chr; +} + +static const struct ChardevAlias { + const char *typename; + const char *alias; +} chardev_alias_table[] = { +#ifdef HAVE_CHARDEV_PARPORT + { "parallel", "parport" }, +#endif +#ifdef HAVE_CHARDEV_SERIAL + { "serial", "tty" }, +#endif +}; + +typedef struct ChadevClassFE { + void (*fn)(const char *name, void *opaque); + void *opaque; +} ChadevClassFE; + +static void +chardev_class_foreach(ObjectClass *klass, void *opaque) +{ + ChadevClassFE *fe = opaque; + + assert(g_str_has_prefix(object_class_get_name(klass), "chardev-")); + if (CHARDEV_CLASS(klass)->internal) { + return; + } + + fe->fn(object_class_get_name(klass) + 8, fe->opaque); +} + +static void +chardev_name_foreach(void (*fn)(const char *name, void *opaque), void *opaque) +{ + ChadevClassFE fe = { .fn = fn, .opaque = opaque }; + int i; + + object_class_foreach(chardev_class_foreach, TYPE_CHARDEV, false, &fe); + + for (i = 0; i < ARRAY_SIZE(chardev_alias_table); i++) { + fn(chardev_alias_table[i].alias, opaque); + } +} + +static void +help_string_append(const char *name, void *opaque) +{ + GString *str = opaque; + + g_string_append_printf(str, "\n%s", name); +} + +Chardev *qemu_chr_new_from_opts(QemuOpts *opts, + Error **errp) +{ + Error *local_err = NULL; + const ChardevClass *cc; + Chardev *chr; + int i; + ChardevBackend *backend = NULL; + const char *name = qemu_opt_get(opts, "backend"); + const char *id = qemu_opts_id(opts); + char *bid = NULL; + + if (name == NULL) { + error_setg(errp, "chardev: \"%s\" missing backend", + qemu_opts_id(opts)); + return NULL; + } + + if (is_help_option(name)) { + GString *str = g_string_new(""); + + chardev_name_foreach(help_string_append, str); + + error_report("Available chardev backend types: %s", str->str); + g_string_free(str, true); + exit(0); + } + + if (id == NULL) { + error_setg(errp, "chardev: no id specified"); + return NULL; + } + + for (i = 0; i < ARRAY_SIZE(chardev_alias_table); i++) { + if (g_strcmp0(chardev_alias_table[i].alias, name) == 0) { + name = chardev_alias_table[i].typename; + break; + } + } + + cc = char_get_class(name, errp); + if (cc == NULL) { + return NULL; + } + + backend = g_new0(ChardevBackend, 1); + backend->type = CHARDEV_BACKEND_KIND_NULL; + + if (qemu_opt_get_bool(opts, "mux", 0)) { + bid = g_strdup_printf("%s-base", id); + } + + chr = NULL; + if (cc->parse) { + cc->parse(opts, backend, &local_err); + if (local_err) { + error_propagate(errp, local_err); + goto out; + } + } else { + ChardevCommon *ccom = g_new0(ChardevCommon, 1); + qemu_chr_parse_common(opts, ccom); + backend->u.null.data = ccom; /* Any ChardevCommon member would work */ + } + + chr = qemu_chardev_add(bid ? bid : id, + object_class_get_name(OBJECT_CLASS(cc)), + backend, errp); + if (chr == NULL) { + goto out; + } + + if (bid) { + Chardev *mux; + qapi_free_ChardevBackend(backend); + backend = g_new0(ChardevBackend, 1); + backend->type = CHARDEV_BACKEND_KIND_MUX; + backend->u.mux.data = g_new0(ChardevMux, 1); + backend->u.mux.data->chardev = g_strdup(bid); + mux = qemu_chardev_add(id, TYPE_CHARDEV_MUX, backend, errp); + if (mux == NULL) { + qemu_chr_delete(chr); + chr = NULL; + goto out; + } + chr = mux; + } + +out: + qapi_free_ChardevBackend(backend); + g_free(bid); + return chr; +} + +Chardev *qemu_chr_new_noreplay(const char *label, const char *filename) +{ + const char *p; + Chardev *chr; + QemuOpts *opts; + Error *err = NULL; + + if (strstart(filename, "chardev:", &p)) { + return qemu_chr_find(p); + } + + opts = qemu_chr_parse_compat(label, filename); + if (!opts) + return NULL; + + chr = qemu_chr_new_from_opts(opts, &err); + if (err) { + error_report_err(err); + } + if (chr && qemu_opt_get_bool(opts, "mux", 0)) { + monitor_init(chr, MONITOR_USE_READLINE); + } + qemu_opts_del(opts); + return chr; +} + +Chardev *qemu_chr_new(const char *label, const char *filename) +{ + Chardev *chr; + chr = qemu_chr_new_noreplay(label, filename); + if (chr) { + if (replay_mode != REPLAY_MODE_NONE) { + qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_REPLAY); + } + if (qemu_chr_replay(chr) && CHARDEV_GET_CLASS(chr)->chr_ioctl) { + error_report("Replay: ioctl is not supported " + "for serial devices yet"); + } + replay_register_char_driver(chr); + } + return chr; +} + +void qemu_chr_fe_set_echo(CharBackend *be, bool echo) +{ + Chardev *chr = be->chr; + + if (chr && CHARDEV_GET_CLASS(chr)->chr_set_echo) { + CHARDEV_GET_CLASS(chr)->chr_set_echo(chr, echo); + } +} + +void qemu_chr_fe_set_open(CharBackend *be, int fe_open) +{ + Chardev *chr = be->chr; + + if (!chr) { + return; + } + + if (be->fe_open == fe_open) { + return; + } + be->fe_open = fe_open; + if (CHARDEV_GET_CLASS(chr)->chr_set_fe_open) { + CHARDEV_GET_CLASS(chr)->chr_set_fe_open(chr, fe_open); + } +} + +guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond, + GIOFunc func, void *user_data) +{ + Chardev *s = be->chr; + GSource *src; + guint tag; + + if (!s || CHARDEV_GET_CLASS(s)->chr_add_watch == NULL) { + return 0; + } + + src = CHARDEV_GET_CLASS(s)->chr_add_watch(s, cond); + if (!src) { + return 0; + } + + g_source_set_callback(src, (GSourceFunc)func, user_data, NULL); + tag = g_source_attach(src, NULL); + g_source_unref(src); + + return tag; +} + +void qemu_chr_fe_disconnect(CharBackend *be) +{ + Chardev *chr = be->chr; + + if (chr && CHARDEV_GET_CLASS(chr)->chr_disconnect) { + CHARDEV_GET_CLASS(chr)->chr_disconnect(chr); + } +} + +void qemu_chr_delete(Chardev *chr) +{ + QTAILQ_REMOVE(&chardevs, chr, next); + object_unref(OBJECT(chr)); +} + +ChardevInfoList *qmp_query_chardev(Error **errp) +{ + ChardevInfoList *chr_list = NULL; + Chardev *chr; + + QTAILQ_FOREACH(chr, &chardevs, next) { + ChardevInfoList *info = g_malloc0(sizeof(*info)); + info->value = g_malloc0(sizeof(*info->value)); + info->value->label = g_strdup(chr->label); + info->value->filename = g_strdup(chr->filename); + info->value->frontend_open = chr->be && chr->be->fe_open; + + info->next = chr_list; + chr_list = info; + } + + return chr_list; +} + +static void +qmp_prepend_backend(const char *name, void *opaque) +{ + ChardevBackendInfoList **list = opaque; + ChardevBackendInfoList *info = g_malloc0(sizeof(*info)); + + info->value = g_malloc0(sizeof(*info->value)); + info->value->name = g_strdup(name); + info->next = *list; + *list = info; +} + +ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp) +{ + ChardevBackendInfoList *backend_list = NULL; + + chardev_name_foreach(qmp_prepend_backend, &backend_list); + + return backend_list; +} + +Chardev *qemu_chr_find(const char *name) +{ + Chardev *chr; + + QTAILQ_FOREACH(chr, &chardevs, next) { + if (strcmp(chr->label, name) != 0) + continue; + return chr; + } + return NULL; +} + +QemuOptsList qemu_chardev_opts = { + .name = "chardev", + .implied_opt_name = "backend", + .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head), + .desc = { + { + .name = "backend", + .type = QEMU_OPT_STRING, + },{ + .name = "path", + .type = QEMU_OPT_STRING, + },{ + .name = "host", + .type = QEMU_OPT_STRING, + },{ + .name = "port", + .type = QEMU_OPT_STRING, + },{ + .name = "localaddr", + .type = QEMU_OPT_STRING, + },{ + .name = "localport", + .type = QEMU_OPT_STRING, + },{ + .name = "to", + .type = QEMU_OPT_NUMBER, + },{ + .name = "ipv4", + .type = QEMU_OPT_BOOL, + },{ + .name = "ipv6", + .type = QEMU_OPT_BOOL, + },{ + .name = "wait", + .type = QEMU_OPT_BOOL, + },{ + .name = "server", + .type = QEMU_OPT_BOOL, + },{ + .name = "delay", + .type = QEMU_OPT_BOOL, + },{ + .name = "reconnect", + .type = QEMU_OPT_NUMBER, + },{ + .name = "telnet", + .type = QEMU_OPT_BOOL, + },{ + .name = "tls-creds", + .type = QEMU_OPT_STRING, + },{ + .name = "width", + .type = QEMU_OPT_NUMBER, + },{ + .name = "height", + .type = QEMU_OPT_NUMBER, + },{ + .name = "cols", + .type = QEMU_OPT_NUMBER, + },{ + .name = "rows", + .type = QEMU_OPT_NUMBER, + },{ + .name = "mux", + .type = QEMU_OPT_BOOL, + },{ + .name = "signal", + .type = QEMU_OPT_BOOL, + },{ + .name = "name", + .type = QEMU_OPT_STRING, + },{ + .name = "debug", + .type = QEMU_OPT_NUMBER, + },{ + .name = "size", + .type = QEMU_OPT_SIZE, + },{ + .name = "chardev", + .type = QEMU_OPT_STRING, + },{ + .name = "append", + .type = QEMU_OPT_BOOL, + },{ + .name = "logfile", + .type = QEMU_OPT_STRING, + },{ + .name = "logappend", + .type = QEMU_OPT_BOOL, + }, + { /* end of list */ } + }, +}; + +bool qemu_chr_has_feature(Chardev *chr, + ChardevFeature feature) +{ + return test_bit(feature, chr->features); +} + +void qemu_chr_set_feature(Chardev *chr, + ChardevFeature feature) +{ + return set_bit(feature, chr->features); +} + +Chardev *qemu_chardev_new(const char *id, const char *typename, + ChardevBackend *backend, Error **errp) +{ + Chardev *chr = NULL; + Error *local_err = NULL; + bool be_opened = true; + + assert(g_str_has_prefix(typename, "chardev-")); + + chr = CHARDEV(object_new(typename)); + chr->label = g_strdup(id); + + qemu_char_open(chr, backend, &be_opened, &local_err); + if (local_err) { + error_propagate(errp, local_err); + object_unref(OBJECT(chr)); + return NULL; + } + + if (!chr->filename) { + chr->filename = g_strdup(typename + 8); + } + if (be_opened) { + qemu_chr_be_event(chr, CHR_EVENT_OPENED); + } + + return chr; +} + +ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, + Error **errp) +{ + const ChardevClass *cc; + ChardevReturn *ret; + Chardev *chr; + + cc = char_get_class(ChardevBackendKind_lookup[backend->type], errp); + if (!cc) { + return NULL; + } + + chr = qemu_chardev_add(id, object_class_get_name(OBJECT_CLASS(cc)), + backend, errp); + if (!chr) { + return NULL; + } + + ret = g_new0(ChardevReturn, 1); + if (CHARDEV_IS_PTY(chr)) { + ret->pty = g_strdup(chr->filename + 4); + ret->has_pty = true; + } + + return ret; +} + +void qmp_chardev_remove(const char *id, Error **errp) +{ + Chardev *chr; + + chr = qemu_chr_find(id); + if (chr == NULL) { + error_setg(errp, "Chardev '%s' not found", id); + return; + } + if (qemu_chr_is_busy(chr)) { + error_setg(errp, "Chardev '%s' is busy", id); + return; + } + if (qemu_chr_replay(chr)) { + error_setg(errp, + "Chardev '%s' cannot be unplugged in record/replay mode", id); + return; + } + qemu_chr_delete(chr); +} + +void qemu_chr_cleanup(void) +{ + Chardev *chr, *tmp; + + QTAILQ_FOREACH_SAFE(chr, &chardevs, next, tmp) { + qemu_chr_delete(chr); + } +} + +static void register_types(void) +{ + type_register_static(&char_type_info); + + /* this must be done after machine init, since we register FEs with muxes + * as part of realize functions like serial_isa_realizefn when -nographic + * is specified + */ + qemu_add_machine_init_done_notifier(&muxes_realize_notify); +} + +type_init(register_types); diff --git a/cpu-exec.c b/cpu-exec.c index fa08c733da..57583f16a0 100644 --- a/cpu-exec.c +++ b/cpu-exec.c @@ -18,7 +18,7 @@ */ #include "qemu/osdep.h" #include "cpu.h" -#include "trace.h" +#include "trace-root.h" #include "disas/disas.h" #include "exec/exec-all.h" #include "tcg.h" diff --git a/default-configs/arm-softmmu.mak b/default-configs/arm-softmmu.mak index 6de3e16a3e..6f2a180985 100644 --- a/default-configs/arm-softmmu.mak +++ b/default-configs/arm-softmmu.mak @@ -108,6 +108,7 @@ CONFIG_FSL_IMX25=y CONFIG_IMX_I2C=y +CONFIG_PCIE_PORT=y CONFIG_XIO3130=y CONFIG_IOH3420=y CONFIG_I82801B11=y diff --git a/default-configs/i386-softmmu.mak b/default-configs/i386-softmmu.mak index 6c52d26091..384cefb612 100644 --- a/default-configs/i386-softmmu.mak +++ b/default-configs/i386-softmmu.mak @@ -51,6 +51,7 @@ CONFIG_PVPANIC=y CONFIG_MEM_HOTPLUG=y CONFIG_NVDIMM=y CONFIG_ACPI_NVDIMM=y +CONFIG_PCIE_PORT=y CONFIG_XIO3130=y CONFIG_IOH3420=y CONFIG_I82801B11=y diff --git a/default-configs/x86_64-softmmu.mak b/default-configs/x86_64-softmmu.mak index 2d0341c3c4..491a191b9d 100644 --- a/default-configs/x86_64-softmmu.mak +++ b/default-configs/x86_64-softmmu.mak @@ -51,6 +51,7 @@ CONFIG_PVPANIC=y CONFIG_MEM_HOTPLUG=y CONFIG_NVDIMM=y CONFIG_ACPI_NVDIMM=y +CONFIG_PCIE_PORT=y CONFIG_XIO3130=y CONFIG_IOH3420=y CONFIG_I82801B11=y diff --git a/dma-helpers.c b/dma-helpers.c index 6f9d47ca50..97157cc2ec 100644 --- a/dma-helpers.c +++ b/dma-helpers.c @@ -10,7 +10,7 @@ #include "qemu/osdep.h" #include "sysemu/block-backend.h" #include "sysemu/dma.h" -#include "trace.h" +#include "trace-root.h" #include "qemu/thread.h" #include "qemu/main-loop.h" diff --git a/docs/tracing.txt b/docs/tracing.txt index f351998a4e..e14bb6dccc 100644 --- a/docs/tracing.txt +++ b/docs/tracing.txt @@ -27,18 +27,44 @@ for debugging, profiling, and observing execution. == Trace events == -Each directory in the source tree can declare a set of static trace events -in a "trace-events" file. Each trace event declaration names the event, its -arguments, and the format string which can be used for pretty-printing: +=== Sub-directory setup === - qemu_vmalloc(size_t size, void *ptr) "size %zu ptr %p" - qemu_vfree(void *ptr) "ptr %p" - -All "trace-events" files must be listed in the "trace-event-y" make variable -in the top level Makefile.objs. During build the individual files are combined -to create a "trace-events-all" file, which is processed by the "tracetool" -script during build to generate code for the trace events. The -"trace-events-all" file is also installed into "/usr/share/qemu". +Each directory in the source tree can declare a set of static trace events +in a local "trace-events" file. All directories which contain "trace-events" +files must be listed in the "trace-events-subdirs" make variable in the top +level Makefile.objs. During build, the "trace-events" file in each listed +subdirectory will be processed by the "tracetool" script to generate code for +the trace events. + +The individual "trace-events" files are merged into a "trace-events-all" file, +which is also installed into "/usr/share/qemu" with the name "trace-events". +This merged file is to be used by the "simpletrace.py" script to later analyse +traces in the simpletrace data format. + +In the sub-directory the following files will be automatically generated + + - trace.c - the trace event state declarations + - trace.h - the trace event enums and probe functions + - trace-dtrace.h - DTrace event probe specification + - trace-dtrace.dtrace - DTrace event probe helper declaration + - trace-dtrace.o - binary DTrace provider (generated by dtrace) + - trace-ust.h - UST event probe helper declarations + +Source files in the sub-directory should #include the local 'trace.h' file, +without any sub-directory path prefix. eg io/channel-buffer.c would do + + #include "trace.h" + +To access the 'io/trace.h' file. While it is possible to include a trace.h +file from outside a source files' own sub-directory, this is discouraged in +general. It is strongly preferred that all events be declared directly in +the sub-directory that uses them. The only exception is where there are some +shared trace events defined in the top level directory trace-events file. +The top level directory generates trace files with a filename prefix of +"trace-root" instead of just "trace". This is to avoid ambiguity between +a trace.h in the current directory, vs the top level directory. + +=== Using trace events === Trace events are invoked directly from source code like this: @@ -83,6 +109,13 @@ Format strings should reflect the types defined in the trace event. Take special care to use PRId64 and PRIu64 for int64_t and uint64_t types, respectively. This ensures portability between 32- and 64-bit platforms. +Each event declaration will start with the event name, then its arguments, +finally a format string for pretty-printing. For example: + + qemu_vmalloc(size_t size, void *ptr) "size %zu ptr %p" + qemu_vfree(void *ptr) "ptr %p" + + === Hints for adding new trace events === 1. Trace state changes in the code. Interesting points in the code usually @@ -44,7 +44,7 @@ #include "sysemu/dma.h" #include "exec/address-spaces.h" #include "sysemu/xen-mapcache.h" -#include "trace.h" +#include "trace-root.h" #endif #include "exec/cpu-all.h" #include "qemu/rcu_queue.h" @@ -19,6 +19,7 @@ #include "net/eth.h" #include "sysemu/char.h" #include "sysemu/block-backend.h" +#include "qemu/config-file.h" #include "qemu/option.h" #include "qemu/timer.h" #include "qmp-commands.h" diff --git a/hw/block/dataplane/trace-events b/hw/block/dataplane/trace-events new file mode 100644 index 0000000000..e07673ab1f --- /dev/null +++ b/hw/block/dataplane/trace-events @@ -0,0 +1,5 @@ +# See docs/tracing.txt for syntax documentation. + +# hw/block/dataplane/virtio-blk.c +virtio_blk_data_plane_start(void *s) "dataplane %p" +virtio_blk_data_plane_stop(void *s) "dataplane %p" diff --git a/hw/block/nvme.c b/hw/block/nvme.c index d479fd22f5..ae91a18f17 100644 --- a/hw/block/nvme.c +++ b/hw/block/nvme.c @@ -872,7 +872,7 @@ static int nvme_init(PCIDevice *pci_dev) pci_register_bar(&n->parent_obj, 0, PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_64, &n->iomem); - msix_init_exclusive_bar(&n->parent_obj, n->num_queues, 4); + msix_init_exclusive_bar(&n->parent_obj, n->num_queues, 4, NULL); id->vid = cpu_to_le16(pci_get_word(pci_conf + PCI_VENDOR_ID)); id->ssvid = cpu_to_le16(pci_get_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID)); diff --git a/hw/block/trace-events b/hw/block/trace-events index d0dd94ff05..65e83dc258 100644 --- a/hw/block/trace-events +++ b/hw/block/trace-events @@ -7,11 +7,6 @@ virtio_blk_handle_write(void *req, uint64_t sector, size_t nsectors) "req %p sec virtio_blk_handle_read(void *req, uint64_t sector, size_t nsectors) "req %p sector %"PRIu64" nsectors %zu" virtio_blk_submit_multireq(void *mrb, int start, int num_reqs, uint64_t offset, size_t size, bool is_write) "mrb %p start %d num_reqs %d offset %"PRIu64" size %zu is_write %d" -# hw/block/dataplane/virtio-blk.c -virtio_blk_data_plane_start(void *s) "dataplane %p" -virtio_blk_data_plane_stop(void *s) "dataplane %p" -virtio_blk_data_plane_process_request(void *s, unsigned int out_num, unsigned int in_num, unsigned int head) "dataplane %p out_num %u in_num %u head %u" - # hw/block/hd-geometry.c hd_geometry_lchs_guess(void *blk, int cyls, int heads, int secs) "blk %p LCHS %d %d %d" hd_geometry_guess(void *blk, uint32_t cyls, uint32_t heads, uint32_t secs, int trans) "blk %p CHS %u %u %u trans %d" diff --git a/hw/display/qxl.c b/hw/display/qxl.c index 62d0c80dcf..af4c0ca002 100644 --- a/hw/display/qxl.c +++ b/hw/display/qxl.c @@ -306,12 +306,11 @@ void qxl_spice_reset_cursor(PCIQXLDevice *qxl) static ram_addr_t qxl_rom_size(void) { - uint32_t required_rom_size = sizeof(QXLRom) + sizeof(QXLModes) + - sizeof(qxl_modes); - uint32_t rom_size = 8192; /* two pages */ +#define QXL_REQUIRED_SZ (sizeof(QXLRom) + sizeof(QXLModes) + sizeof(qxl_modes)) +#define QXL_ROM_SZ 8192 - QEMU_BUILD_BUG_ON(required_rom_size > rom_size); - return rom_size; + QEMU_BUILD_BUG_ON(QXL_REQUIRED_SZ > QXL_ROM_SZ); + return QXL_ROM_SZ; } static void init_qxl_rom(PCIQXLDevice *d) diff --git a/hw/display/trace-events b/hw/display/trace-events index 332ababd8e..aadb612dcb 100644 --- a/hw/display/trace-events +++ b/hw/display/trace-events @@ -34,7 +34,6 @@ vmware_setmode(uint32_t w, uint32_t h, uint32_t bpp) "%dx%d @ %d bpp" # hw/display/virtio-gpu.c virtio_gpu_features(bool virgl) "virgl %d" virtio_gpu_cmd_get_display_info(void) "" -virtio_gpu_cmd_get_caps(void) "" virtio_gpu_cmd_set_scanout(uint32_t id, uint32_t res, uint32_t w, uint32_t h, uint32_t x, uint32_t y) "id %d, res 0x%x, w %d, h %d, x %d, y %d" virtio_gpu_cmd_res_create_2d(uint32_t res, uint32_t fmt, uint32_t w, uint32_t h) "res 0x%x, fmt 0x%x, w %d, h %d" virtio_gpu_cmd_res_create_3d(uint32_t res, uint32_t fmt, uint32_t w, uint32_t h, uint32_t d) "res 0x%x, fmt 0x%x, w %d, h %d, d %d" diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index ec62239aba..3270fb9162 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -1485,8 +1485,16 @@ static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s, goto done; } + /* According to ATS spec table 2.4: + * S = 0, bits 15:12 = xxxx range size: 4K + * S = 1, bits 15:12 = xxx0 range size: 8K + * S = 1, bits 15:12 = xx01 range size: 16K + * S = 1, bits 15:12 = x011 range size: 32K + * S = 1, bits 15:12 = 0111 range size: 64K + * ... + */ if (size) { - sz = 1 << (ctz64(~(addr | (VTD_PAGE_MASK_4K - 1))) + 1); + sz = (VTD_PAGE_SIZE * 2) << cto64(addr >> VTD_PAGE_SHIFT); addr &= ~(sz - 1); } else { sz = VTD_PAGE_SIZE; diff --git a/hw/i386/pc.c b/hw/i386/pc.c index 706e2330ac..e3fcd514dd 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -1708,6 +1708,11 @@ static void pc_dimm_plug(HotplugHandler *hotplug_dev, } if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) { + if (!pcms->acpi_nvdimm_state.is_enabled) { + error_setg(&local_err, + "nvdimm is not enabled: missing 'nvdimm' in '-M'"); + goto out; + } nvdimm_plug(&pcms->acpi_nvdimm_state); } diff --git a/hw/i386/trace-events b/hw/i386/trace-events index d2b497327e..1cc4a10a07 100644 --- a/hw/i386/trace-events +++ b/hw/i386/trace-events @@ -1,12 +1,5 @@ # See docs/tracing.txt for syntax documentation. -# hw/i386/xen/xen_platform.c -xen_platform_log(char *s) "xen platform: %s" - -# hw/i386/xen/xen_pvdevice.c -xen_pv_mmio_read(uint64_t addr) "WARNING: read from Xen PV Device MMIO space (address %"PRIx64")" -xen_pv_mmio_write(uint64_t addr) "WARNING: write to Xen PV Device MMIO space (address %"PRIx64")" - # hw/i386/x86-iommu.c x86_iommu_iec_notify(bool global, uint32_t index, uint32_t mask) "Notify IEC invalidation: global=%d index=%" PRIu32 " mask=%" PRIu32 @@ -30,7 +23,6 @@ amdvi_devtab_inval(uint8_t bus, uint8_t slot, uint8_t func) "device table entry amdvi_completion_wait(uint64_t addr, uint64_t data) "completion wait requested with store address 0x%"PRIx64" and store data 0x%"PRIx64 amdvi_control_status(uint64_t val) "MMIO_STATUS state 0x%"PRIx64 amdvi_iotlb_reset(void) "IOTLB exceed size limit - reset " -amdvi_completion_wait_exec(uint64_t addr, uint64_t data) "completion wait requested with store address 0x%"PRIx64" and store data 0x%"PRIx64 amdvi_dte_get_fail(uint64_t addr, uint32_t offset) "error: failed to access Device Entry devtab 0x%"PRIx64" offset 0x%"PRIx32 amdvi_invalid_dte(uint64_t addr) "PTE entry at 0x%"PRIx64" is invalid " amdvi_get_pte_hwerror(uint64_t addr) "hardware error eccessing PTE at addr 0x%"PRIx64 diff --git a/hw/i386/xen/trace-events b/hw/i386/xen/trace-events new file mode 100644 index 0000000000..321fe60fed --- /dev/null +++ b/hw/i386/xen/trace-events @@ -0,0 +1,6 @@ +# hw/i386/xen/xen_platform.c +xen_platform_log(char *s) "xen platform: %s" + +# hw/i386/xen/xen_pvdevice.c +xen_pv_mmio_read(uint64_t addr) "WARNING: read from Xen PV Device MMIO space (address %"PRIx64")" +xen_pv_mmio_write(uint64_t addr) "WARNING: write to Xen PV Device MMIO space (address %"PRIx64")" diff --git a/hw/input/ps2.c b/hw/input/ps2.c index 8485a4edaf..1d3a440bbd 100644 --- a/hw/input/ps2.c +++ b/hw/input/ps2.c @@ -881,9 +881,11 @@ static void ps2_mouse_event(DeviceState *dev, QemuConsole *src, InputEvent *evt) { static const int bmap[INPUT_BUTTON__MAX] = { - [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON, - [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON, - [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON, + [INPUT_BUTTON_LEFT] = PS2_MOUSE_BUTTON_LEFT, + [INPUT_BUTTON_MIDDLE] = PS2_MOUSE_BUTTON_MIDDLE, + [INPUT_BUTTON_RIGHT] = PS2_MOUSE_BUTTON_RIGHT, + [INPUT_BUTTON_SIDE] = PS2_MOUSE_BUTTON_SIDE, + [INPUT_BUTTON_EXTRA] = PS2_MOUSE_BUTTON_EXTRA, }; PS2MouseState *s = (PS2MouseState *)dev; InputMoveEvent *move; diff --git a/hw/input/trace-events b/hw/input/trace-events index 8c4003f361..f3bfbede5c 100644 --- a/hw/input/trace-events +++ b/hw/input/trace-events @@ -8,8 +8,6 @@ ps2_reset_keyboard(void *s) "%p" ps2_write_keyboard(void *opaque, int val) "%p val %d" ps2_keyboard_set_translation(void *opaque, int mode) "%p mode %d" ps2_mouse_send_packet(void *s, int dx1, int dy1, int dz1, int b) "%p x %d y %d z %d bs %#x" -ps2_mouse_event_disabled(void *opaque, int dx, int dy, int dz, int buttons_state, int mouse_dx, int mouse_dy, int mouse_dz) "%p x %d y %d z %d bs %#x mx %d my %d mz %d " -ps2_mouse_event(void *opaque, int dx, int dy, int dz, int buttons_state, int mouse_dx, int mouse_dy, int mouse_dz) "%p x %d y %d z %d bs %#x mx %d my %d mz %d " ps2_mouse_fake_event(void *opaque) "%p" ps2_write_mouse(void *opaque, int val) "%p val %d" ps2_kbd_reset(void *opaque) "%p" diff --git a/hw/intc/trace-events b/hw/intc/trace-events index 92a6171692..39a538d048 100644 --- a/hw/intc/trace-events +++ b/hw/intc/trace-events @@ -67,7 +67,6 @@ xics_alloc(int irq) "irq %d" xics_alloc_block(int first, int num, bool lsi, int align) "first irq %d, %d irqs, lsi=%d, alignnum %d" xics_ics_free(int src, int irq, int num) "Source#%d, first irq %d, %d irqs" xics_ics_free_warn(int src, int irq) "Source#%d, irq %d is already free" -xics_icp_post_load(uint32_t server_no, uint32_t xirr, uint64_t addr, uint8_t pend) "server_no %d, xirr %#x, xirr_owner 0x%" PRIx64 ", pending %d" # hw/intc/s390_flic_kvm.c flic_create_device(int err) "flic: create device failed %d" diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c index 846e903eb2..bf57e635d6 100644 --- a/hw/misc/ivshmem.c +++ b/hw/misc/ivshmem.c @@ -749,13 +749,13 @@ static void ivshmem_reset(DeviceState *d) } } -static int ivshmem_setup_interrupts(IVShmemState *s) +static int ivshmem_setup_interrupts(IVShmemState *s, Error **errp) { /* allocate QEMU callback data for receiving interrupts */ s->msi_vectors = g_malloc0(s->vectors * sizeof(MSIVector)); if (ivshmem_has_feature(s, IVSHMEM_MSI)) { - if (msix_init_exclusive_bar(PCI_DEVICE(s), s->vectors, 1)) { + if (msix_init_exclusive_bar(PCI_DEVICE(s), s->vectors, 1, errp)) { return -1; } @@ -898,8 +898,8 @@ static void ivshmem_common_realize(PCIDevice *dev, Error **errp) qemu_chr_fe_set_handlers(&s->server_chr, ivshmem_can_receive, ivshmem_read, NULL, s, NULL, true); - if (ivshmem_setup_interrupts(s) < 0) { - error_setg(errp, "failed to initialize interrupts"); + if (ivshmem_setup_interrupts(s, errp) < 0) { + error_prepend(errp, "Failed to initialize interrupts: "); return; } } diff --git a/hw/net/e1000e.c b/hw/net/e1000e.c index 0e9a25b7ab..b0f429b8e5 100644 --- a/hw/net/e1000e.c +++ b/hw/net/e1000e.c @@ -292,7 +292,7 @@ e1000e_init_msix(E1000EState *s) E1000E_MSIX_IDX, E1000E_MSIX_TABLE, &s->msix, E1000E_MSIX_IDX, E1000E_MSIX_PBA, - 0xA0); + 0xA0, NULL); if (res < 0) { trace_e1000e_msix_init_fail(res); diff --git a/hw/net/fsl_etsec/etsec.c b/hw/net/fsl_etsec/etsec.c index fadf9c8faf..aa2b0d5a85 100644 --- a/hw/net/fsl_etsec/etsec.c +++ b/hw/net/fsl_etsec/etsec.c @@ -29,7 +29,6 @@ #include "qemu/osdep.h" #include "sysemu/sysemu.h" #include "hw/sysbus.h" -#include "trace.h" #include "hw/ptimer.h" #include "etsec.h" #include "registers.h" diff --git a/hw/net/rocker/rocker.c b/hw/net/rocker/rocker.c index e9d215aa4d..6e70fddee3 100644 --- a/hw/net/rocker/rocker.c +++ b/hw/net/rocker/rocker.c @@ -1256,14 +1256,16 @@ static int rocker_msix_init(Rocker *r) { PCIDevice *dev = PCI_DEVICE(r); int err; + Error *local_err = NULL; err = msix_init(dev, ROCKER_MSIX_VEC_COUNT(r->fp_ports), &r->msix_bar, ROCKER_PCI_MSIX_BAR_IDX, ROCKER_PCI_MSIX_TABLE_OFFSET, &r->msix_bar, ROCKER_PCI_MSIX_BAR_IDX, ROCKER_PCI_MSIX_PBA_OFFSET, - 0); + 0, &local_err); if (err) { + error_report_err(local_err); return err; } diff --git a/hw/net/trace-events b/hw/net/trace-events index 1a5c909939..c71480535e 100644 --- a/hw/net/trace-events +++ b/hw/net/trace-events @@ -63,10 +63,6 @@ net_rx_pkt_l4_csum_validate_entry(void) "Starting L4 checksum validation" net_rx_pkt_l4_csum_validate_not_xxp(void) "Not a TCP/UDP packet" net_rx_pkt_l4_csum_validate_udp_with_no_checksum(void) "UDP packet without checksum" net_rx_pkt_l4_csum_validate_ip4_fragment(void) "IP4 fragment" -net_rx_pkt_l4_csum_validate_ip4_udp(void) "IP4/UDP packet" -net_rx_pkt_l4_csum_validate_ip4_tcp(void) "IP4/TCP packet" -net_rx_pkt_l4_csum_validate_ip6_udp(void) "IP6/UDP packet" -net_rx_pkt_l4_csum_validate_ip6_tcp(void) "IP6/TCP packet" net_rx_pkt_l4_csum_validate_csum(bool csum_valid) "Checksum valid: %d" net_rx_pkt_l4_csum_calc_entry(void) "Starting L4 checksum calculation" @@ -117,7 +113,6 @@ e1000e_core_mdic_read(uint8_t page, uint32_t addr, uint32_t data) "MDIC READ: PH e1000e_core_mdic_read_unhandled(uint8_t page, uint32_t addr) "MDIC READ: PHY[%u][%u] UNHANDLED" e1000e_core_mdic_write(uint8_t page, uint32_t addr, uint32_t data) "MDIC WRITE: PHY[%u][%u] = 0x%x" e1000e_core_mdic_write_unhandled(uint8_t page, uint32_t addr) "MDIC WRITE: PHY[%u][%u] UNHANDLED" -e1000e_core_eeeprom_write(uint16_t bit_in, uint16_t bit_out, uint16_t reading) "eeprom bitnum in %d out %d, reading %d" e1000e_core_ctrl_write(uint64_t index, uint32_t val) "Write CTRL register 0x%"PRIx64", value: 0x%X" e1000e_core_ctrl_sw_reset(void) "Doing SW reset" e1000e_core_ctrl_phy_reset(void) "Doing PHY reset" @@ -159,7 +154,6 @@ e1000e_rx_desc_buff_write(uint8_t idx, uint64_t addr, uint16_t offset, const voi e1000e_rx_descr(int ridx, uint64_t base, uint8_t len) "Next RX descriptor: ring #%d, PA: 0x%"PRIx64", length: %u" e1000e_rx_set_rctl(uint32_t rctl) "RCTL = 0x%x" e1000e_rx_receive_iov(int iovcnt) "Received vector of %d fragments" -e1000e_rx_packet_size(size_t full, size_t vhdr, size_t data) "Received packet of %zu bytes total, %zu virt header, %zu data" e1000e_rx_flt_dropped(void) "Received packet dropped by RX filter" e1000e_rx_written_to_guest(uint32_t causes) "Received packet written to guest (ICR causes %u)" e1000e_rx_not_written_to_guest(uint32_t causes) "Received packet NOT written to guest (ICR causes %u)" @@ -196,14 +190,12 @@ e1000e_rx_metadata_ipv6_filtering_disabled(void) "IPv6 RX filtering disabled by e1000e_vlan_vet(uint16_t vet) "Setting VLAN ethernet type 0x%X" -e1000e_irq_set_cause(uint32_t cause) "IRQ cause set 0x%x" e1000e_irq_msi_notify(uint32_t cause) "MSI notify 0x%x" e1000e_irq_throttling_no_pending_interrupts(void) "No pending interrupts to notify" e1000e_irq_msi_notify_postponed(void) "Sending MSI postponed by ITR" e1000e_irq_legacy_notify_postponed(void) "Raising legacy IRQ postponed by ITR" e1000e_irq_throttling_no_pending_vec(int idx) "No pending interrupts for vector %d" e1000e_irq_msix_notify_postponed_vec(int idx) "Sending MSI-X postponed by EITR[%d]" -e1000e_irq_msix_notify(uint32_t cause) "MSI-X notify 0x%x" e1000e_irq_legacy_notify(bool level) "IRQ line state: %d" e1000e_irq_msix_notify_vec(uint32_t vector) "MSI-X notify vector 0x%x" e1000e_irq_postponed_by_xitr(uint32_t reg) "Interrupt postponed by [E]ITR register 0x%x" diff --git a/hw/net/vmxnet3.c b/hw/net/vmxnet3.c index 2cb2731e29..7dd456551c 100644 --- a/hw/net/vmxnet3.c +++ b/hw/net/vmxnet3.c @@ -2191,7 +2191,7 @@ vmxnet3_init_msix(VMXNET3State *s) VMXNET3_MSIX_BAR_IDX, VMXNET3_OFF_MSIX_TABLE, &s->msix_bar, VMXNET3_MSIX_BAR_IDX, VMXNET3_OFF_MSIX_PBA(s), - VMXNET3_MSIX_OFFSET(s)); + VMXNET3_MSIX_OFFSET(s), NULL); if (0 > res) { VMW_WRPRN("Failed to initialize MSI-X, error %d", res); diff --git a/hw/pci-bridge/Makefile.objs b/hw/pci-bridge/Makefile.objs index 5612bd75e5..c4683cf5c1 100644 --- a/hw/pci-bridge/Makefile.objs +++ b/hw/pci-bridge/Makefile.objs @@ -1,4 +1,5 @@ common-obj-y += pci_bridge_dev.o +common-obj-$(CONFIG_PCIE_PORT) += pcie_root_port.o gen_pcie_root_port.o common-obj-$(CONFIG_PXB) += pci_expander_bridge.o common-obj-$(CONFIG_XIO3130) += xio3130_upstream.o xio3130_downstream.o common-obj-$(CONFIG_IOH3420) += ioh3420.o diff --git a/hw/pci-bridge/gen_pcie_root_port.c b/hw/pci-bridge/gen_pcie_root_port.c new file mode 100644 index 0000000000..8ebffa8bb0 --- /dev/null +++ b/hw/pci-bridge/gen_pcie_root_port.c @@ -0,0 +1,87 @@ +/* + * Generic PCI Express Root Port emulation + * + * Copyright (C) 2017 Red Hat Inc + * + * Authors: + * Marcel Apfelbaum <marcel@redhat.com> + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "hw/pci/msix.h" +#include "hw/pci/pcie_port.h" + +#define TYPE_GEN_PCIE_ROOT_PORT "pcie-root-port" + +#define GEN_PCIE_ROOT_PORT_AER_OFFSET 0x100 +#define GEN_PCIE_ROOT_PORT_MSIX_NR_VECTOR 1 + +static uint8_t gen_rp_aer_vector(const PCIDevice *d) +{ + return 0; +} + +static int gen_rp_interrupts_init(PCIDevice *d, Error **errp) +{ + int rc; + + rc = msix_init_exclusive_bar(d, GEN_PCIE_ROOT_PORT_MSIX_NR_VECTOR, 0, errp); + + if (rc < 0) { + assert(rc == -ENOTSUP); + } else { + msix_vector_use(d, 0); + } + + return rc; +} + +static void gen_rp_interrupts_uninit(PCIDevice *d) +{ + msix_uninit_exclusive_bar(d); +} + +static const VMStateDescription vmstate_rp_dev = { + .name = "pcie-root-port", + .version_id = 1, + .minimum_version_id = 1, + .post_load = pcie_cap_slot_post_load, + .fields = (VMStateField[]) { + VMSTATE_PCI_DEVICE(parent_obj.parent_obj.parent_obj, PCIESlot), + VMSTATE_STRUCT(parent_obj.parent_obj.parent_obj.exp.aer_log, + PCIESlot, 0, vmstate_pcie_aer_log, PCIEAERLog), + VMSTATE_END_OF_LIST() + } +}; + +static void gen_rp_dev_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); + PCIERootPortClass *rpc = PCIE_ROOT_PORT_CLASS(klass); + + k->vendor_id = PCI_VENDOR_ID_REDHAT; + k->device_id = PCI_DEVICE_ID_REDHAT_PCIE_RP; + dc->desc = "PCI Express Root Port"; + dc->vmsd = &vmstate_rp_dev; + rpc->aer_vector = gen_rp_aer_vector; + rpc->interrupts_init = gen_rp_interrupts_init; + rpc->interrupts_uninit = gen_rp_interrupts_uninit; + rpc->aer_offset = GEN_PCIE_ROOT_PORT_AER_OFFSET; +} + +static const TypeInfo gen_rp_dev_info = { + .name = TYPE_GEN_PCIE_ROOT_PORT, + .parent = TYPE_PCIE_ROOT_PORT, + .class_init = gen_rp_dev_class_init, +}; + + static void gen_rp_register_types(void) + { + type_register_static(&gen_rp_dev_info); + } + type_init(gen_rp_register_types) diff --git a/hw/pci-bridge/ioh3420.c b/hw/pci-bridge/ioh3420.c index 0eef87a4f8..da4e5bdf04 100644 --- a/hw/pci-bridge/ioh3420.c +++ b/hw/pci-bridge/ioh3420.c @@ -61,119 +61,28 @@ static uint8_t ioh3420_aer_vector(const PCIDevice *d) return 0; } -static void ioh3420_aer_vector_update(PCIDevice *d) +static int ioh3420_interrupts_init(PCIDevice *d, Error **errp) { - pcie_aer_root_set_vector(d, ioh3420_aer_vector(d)); -} - -static void ioh3420_write_config(PCIDevice *d, - uint32_t address, uint32_t val, int len) -{ - uint32_t root_cmd = - pci_get_long(d->config + d->exp.aer_cap + PCI_ERR_ROOT_COMMAND); - - pci_bridge_write_config(d, address, val, len); - ioh3420_aer_vector_update(d); - pcie_cap_slot_write_config(d, address, val, len); - pcie_aer_write_config(d, address, val, len); - pcie_aer_root_write_config(d, address, val, len, root_cmd); -} - -static void ioh3420_reset(DeviceState *qdev) -{ - PCIDevice *d = PCI_DEVICE(qdev); - - ioh3420_aer_vector_update(d); - pcie_cap_root_reset(d); - pcie_cap_deverr_reset(d); - pcie_cap_slot_reset(d); - pcie_cap_arifwd_reset(d); - pcie_aer_root_reset(d); - pci_bridge_reset(qdev); - pci_bridge_disable_base_limit(d); -} - -static int ioh3420_initfn(PCIDevice *d) -{ - PCIEPort *p = PCIE_PORT(d); - PCIESlot *s = PCIE_SLOT(d); int rc; - Error *err = NULL; - - pci_config_set_interrupt_pin(d->config, 1); - pci_bridge_initfn(d, TYPE_PCIE_BUS); - pcie_port_init_reg(d); - - rc = pci_bridge_ssvid_init(d, IOH_EP_SSVID_OFFSET, - IOH_EP_SSVID_SVID, IOH_EP_SSVID_SSID); - if (rc < 0) { - goto err_bridge; - } + Error *local_err = NULL; rc = msi_init(d, IOH_EP_MSI_OFFSET, IOH_EP_MSI_NR_VECTOR, IOH_EP_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_64BIT, - IOH_EP_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_MASKBIT, &err); + IOH_EP_MSI_SUPPORTED_FLAGS & PCI_MSI_FLAGS_MASKBIT, + &local_err); if (rc < 0) { assert(rc == -ENOTSUP); - error_report_err(err); - goto err_bridge; + error_propagate(errp, local_err); } - rc = pcie_cap_init(d, IOH_EP_EXP_OFFSET, PCI_EXP_TYPE_ROOT_PORT, p->port); - if (rc < 0) { - goto err_msi; - } - - pcie_cap_arifwd_init(d); - pcie_cap_deverr_init(d); - pcie_cap_slot_init(d, s->slot); - pcie_cap_root_init(d); - - pcie_chassis_create(s->chassis); - rc = pcie_chassis_add_slot(s); - if (rc < 0) { - goto err_pcie_cap; - } - - rc = pcie_aer_init(d, PCI_ERR_VER, IOH_EP_AER_OFFSET, - PCI_ERR_SIZEOF, &err); - if (rc < 0) { - error_report_err(err); - goto err; - } - pcie_aer_root_init(d); - ioh3420_aer_vector_update(d); - - return 0; - -err: - pcie_chassis_del_slot(s); -err_pcie_cap: - pcie_cap_exit(d); -err_msi: - msi_uninit(d); -err_bridge: - pci_bridge_exitfn(d); return rc; } -static void ioh3420_exitfn(PCIDevice *d) +static void ioh3420_interrupts_uninit(PCIDevice *d) { - PCIESlot *s = PCIE_SLOT(d); - - pcie_aer_exit(d); - pcie_chassis_del_slot(s); - pcie_cap_exit(d); msi_uninit(d); - pci_bridge_exitfn(d); } -static Property ioh3420_props[] = { - DEFINE_PROP_BIT(COMPAT_PROP_PCP, PCIDevice, cap_present, - QEMU_PCIE_SLTCAP_PCP_BITNR, true), - DEFINE_PROP_END_OF_LIST() -}; - static const VMStateDescription vmstate_ioh3420 = { .name = "ioh-3240-express-root-port", .version_id = 1, @@ -191,25 +100,25 @@ static void ioh3420_class_init(ObjectClass *klass, void *data) { DeviceClass *dc = DEVICE_CLASS(klass); PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); + PCIERootPortClass *rpc = PCIE_ROOT_PORT_CLASS(klass); - k->is_express = 1; - k->is_bridge = 1; - k->config_write = ioh3420_write_config; - k->init = ioh3420_initfn; - k->exit = ioh3420_exitfn; k->vendor_id = PCI_VENDOR_ID_INTEL; k->device_id = PCI_DEVICE_ID_IOH_EPORT; k->revision = PCI_DEVICE_ID_IOH_REV; - set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); dc->desc = "Intel IOH device id 3420 PCIE Root Port"; - dc->reset = ioh3420_reset; dc->vmsd = &vmstate_ioh3420; - dc->props = ioh3420_props; + rpc->aer_vector = ioh3420_aer_vector; + rpc->interrupts_init = ioh3420_interrupts_init; + rpc->interrupts_uninit = ioh3420_interrupts_uninit; + rpc->exp_offset = IOH_EP_EXP_OFFSET; + rpc->aer_offset = IOH_EP_AER_OFFSET; + rpc->ssvid_offset = IOH_EP_SSVID_OFFSET; + rpc->ssid = IOH_EP_SSVID_SSID; } static const TypeInfo ioh3420_info = { .name = "ioh3420", - .parent = TYPE_PCIE_SLOT, + .parent = TYPE_PCIE_ROOT_PORT, .class_init = ioh3420_class_init, }; diff --git a/hw/pci-bridge/pci_bridge_dev.c b/hw/pci-bridge/pci_bridge_dev.c index 5dbd933cc1..647ad80155 100644 --- a/hw/pci-bridge/pci_bridge_dev.c +++ b/hw/pci-bridge/pci_bridge_dev.c @@ -163,7 +163,7 @@ static Property pci_bridge_dev_properties[] = { DEFINE_PROP_ON_OFF_AUTO(PCI_BRIDGE_DEV_PROP_MSI, PCIBridgeDev, msi, ON_OFF_AUTO_AUTO), DEFINE_PROP_BIT(PCI_BRIDGE_DEV_PROP_SHPC, PCIBridgeDev, flags, - PCI_BRIDGE_DEV_F_SHPC_REQ, true), + PCI_BRIDGE_DEV_F_SHPC_REQ, false), DEFINE_PROP_END_OF_LIST(), }; diff --git a/hw/pci-bridge/pcie_root_port.c b/hw/pci-bridge/pcie_root_port.c new file mode 100644 index 0000000000..cf3631806f --- /dev/null +++ b/hw/pci-bridge/pcie_root_port.c @@ -0,0 +1,171 @@ +/* + * Base class for PCI Express Root Ports + * + * Copyright (C) 2017 Red Hat Inc + * + * Authors: + * Marcel Apfelbaum <marcel@redhat.com> + * + * Most of the code was migrated from hw/pci-bridge/ioh3420. + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "hw/pci/pcie_port.h" + +static void rp_aer_vector_update(PCIDevice *d) +{ + PCIERootPortClass *rpc = PCIE_ROOT_PORT_GET_CLASS(d); + + if (rpc->aer_vector) { + pcie_aer_root_set_vector(d, rpc->aer_vector(d)); + } +} + +static void rp_write_config(PCIDevice *d, uint32_t address, + uint32_t val, int len) +{ + uint32_t root_cmd = + pci_get_long(d->config + d->exp.aer_cap + PCI_ERR_ROOT_COMMAND); + + pci_bridge_write_config(d, address, val, len); + rp_aer_vector_update(d); + pcie_cap_slot_write_config(d, address, val, len); + pcie_aer_write_config(d, address, val, len); + pcie_aer_root_write_config(d, address, val, len, root_cmd); +} + +static void rp_reset(DeviceState *qdev) +{ + PCIDevice *d = PCI_DEVICE(qdev); + + rp_aer_vector_update(d); + pcie_cap_root_reset(d); + pcie_cap_deverr_reset(d); + pcie_cap_slot_reset(d); + pcie_cap_arifwd_reset(d); + pcie_aer_root_reset(d); + pci_bridge_reset(qdev); + pci_bridge_disable_base_limit(d); +} + +static void rp_realize(PCIDevice *d, Error **errp) +{ + PCIEPort *p = PCIE_PORT(d); + PCIESlot *s = PCIE_SLOT(d); + PCIDeviceClass *dc = PCI_DEVICE_GET_CLASS(d); + PCIERootPortClass *rpc = PCIE_ROOT_PORT_GET_CLASS(d); + int rc; + Error *local_err = NULL; + + pci_config_set_interrupt_pin(d->config, 1); + pci_bridge_initfn(d, TYPE_PCIE_BUS); + pcie_port_init_reg(d); + + rc = pci_bridge_ssvid_init(d, rpc->ssvid_offset, dc->vendor_id, rpc->ssid); + if (rc < 0) { + error_setg(errp, "Can't init SSV ID, error %d", rc); + goto err_bridge; + } + + if (rpc->interrupts_init) { + rc = rpc->interrupts_init(d, &local_err); + if (rc < 0) { + error_propagate(errp, local_err); + goto err_bridge; + } + } + + rc = pcie_cap_init(d, rpc->exp_offset, PCI_EXP_TYPE_ROOT_PORT, p->port); + if (rc < 0) { + error_setg(errp, "Can't add Root Port capability, error %d", rc); + goto err_int; + } + + pcie_cap_arifwd_init(d); + pcie_cap_deverr_init(d); + pcie_cap_slot_init(d, s->slot); + pcie_cap_root_init(d); + + pcie_chassis_create(s->chassis); + rc = pcie_chassis_add_slot(s); + if (rc < 0) { + error_setg(errp, "Can't add chassis slot, error %d", rc); + goto err_pcie_cap; + } + + rc = pcie_aer_init(d, PCI_ERR_VER, rpc->aer_offset, + PCI_ERR_SIZEOF, &local_err); + if (rc < 0) { + error_propagate(errp, local_err); + goto err; + } + pcie_aer_root_init(d); + rp_aer_vector_update(d); + + return; + +err: + pcie_chassis_del_slot(s); +err_pcie_cap: + pcie_cap_exit(d); +err_int: + if (rpc->interrupts_uninit) { + rpc->interrupts_uninit(d); + } +err_bridge: + pci_bridge_exitfn(d); +} + +static void rp_exit(PCIDevice *d) +{ + PCIERootPortClass *rpc = PCIE_ROOT_PORT_GET_CLASS(d); + PCIESlot *s = PCIE_SLOT(d); + + pcie_aer_exit(d); + pcie_chassis_del_slot(s); + pcie_cap_exit(d); + if (rpc->interrupts_uninit) { + rpc->interrupts_uninit(d); + } + pci_bridge_exitfn(d); +} + +static Property rp_props[] = { + DEFINE_PROP_BIT(COMPAT_PROP_PCP, PCIDevice, cap_present, + QEMU_PCIE_SLTCAP_PCP_BITNR, true), + DEFINE_PROP_END_OF_LIST() +}; + +static void rp_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); + + k->is_express = 1; + k->is_bridge = 1; + k->config_write = rp_write_config; + k->realize = rp_realize; + k->exit = rp_exit; + set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); + dc->reset = rp_reset; + dc->props = rp_props; +} + +static const TypeInfo rp_info = { + .name = TYPE_PCIE_ROOT_PORT, + .parent = TYPE_PCIE_SLOT, + .class_init = rp_class_init, + .abstract = true, + .class_size = sizeof(PCIERootPortClass), +}; + +static void rp_register_types(void) +{ + type_register_static(&rp_info); +} + +type_init(rp_register_types) diff --git a/hw/pci/msix.c b/hw/pci/msix.c index ee1714d2cf..bb54e8b0ac 100644 --- a/hw/pci/msix.c +++ b/hw/pci/msix.c @@ -21,6 +21,7 @@ #include "hw/pci/pci.h" #include "hw/xen/xen.h" #include "qemu/range.h" +#include "qapi/error.h" #define MSIX_CAP_LENGTH 12 @@ -238,11 +239,31 @@ static void msix_mask_all(struct PCIDevice *dev, unsigned nentries) } } -/* Initialize the MSI-X structures */ +/* + * Make PCI device @dev MSI-X capable + * @nentries is the max number of MSI-X vectors that the device support. + * @table_bar is the MemoryRegion that MSI-X table structure resides. + * @table_bar_nr is number of base address register corresponding to @table_bar. + * @table_offset indicates the offset that the MSI-X table structure starts with + * in @table_bar. + * @pba_bar is the MemoryRegion that the Pending Bit Array structure resides. + * @pba_bar_nr is number of base address register corresponding to @pba_bar. + * @pba_offset indicates the offset that the Pending Bit Array structure + * starts with in @pba_bar. + * Non-zero @cap_pos puts capability MSI-X at that offset in PCI config space. + * @errp is for returning errors. + * + * Return 0 on success; set @errp and return -errno on error: + * -ENOTSUP means lacking msi support for a msi-capable platform. + * -EINVAL means capability overlap, happens when @cap_pos is non-zero, + * also means a programming error, except device assignment, which can check + * if a real HW is broken. + */ int msix_init(struct PCIDevice *dev, unsigned short nentries, MemoryRegion *table_bar, uint8_t table_bar_nr, unsigned table_offset, MemoryRegion *pba_bar, - uint8_t pba_bar_nr, unsigned pba_offset, uint8_t cap_pos) + uint8_t pba_bar_nr, unsigned pba_offset, uint8_t cap_pos, + Error **errp) { int cap; unsigned table_size, pba_size; @@ -250,10 +271,12 @@ int msix_init(struct PCIDevice *dev, unsigned short nentries, /* Nothing to do if MSI is not supported by interrupt controller */ if (!msi_nonbroken) { + error_setg(errp, "MSI-X is not supported by interrupt controller"); return -ENOTSUP; } if (nentries < 1 || nentries > PCI_MSIX_FLAGS_QSIZE + 1) { + error_setg(errp, "The number of MSI-X vectors is invalid"); return -EINVAL; } @@ -266,10 +289,13 @@ int msix_init(struct PCIDevice *dev, unsigned short nentries, table_offset + table_size > memory_region_size(table_bar) || pba_offset + pba_size > memory_region_size(pba_bar) || (table_offset | pba_offset) & PCI_MSIX_FLAGS_BIRMASK) { + error_setg(errp, "table & pba overlap, or they don't fit in BARs," + " or don't align"); return -EINVAL; } - cap = pci_add_capability(dev, PCI_CAP_ID_MSIX, cap_pos, MSIX_CAP_LENGTH); + cap = pci_add_capability2(dev, PCI_CAP_ID_MSIX, + cap_pos, MSIX_CAP_LENGTH, errp); if (cap < 0) { return cap; } @@ -306,7 +332,7 @@ int msix_init(struct PCIDevice *dev, unsigned short nentries, } int msix_init_exclusive_bar(PCIDevice *dev, unsigned short nentries, - uint8_t bar_nr) + uint8_t bar_nr, Error **errp) { int ret; char *name; @@ -338,7 +364,7 @@ int msix_init_exclusive_bar(PCIDevice *dev, unsigned short nentries, ret = msix_init(dev, nentries, &dev->msix_exclusive_bar, bar_nr, 0, &dev->msix_exclusive_bar, bar_nr, bar_pba_offset, - 0); + 0, errp); if (ret) { return ret; } @@ -447,8 +473,10 @@ void msix_notify(PCIDevice *dev, unsigned vector) { MSIMessage msg; - if (vector >= dev->msix_entries_nr || !dev->msix_entry_used[vector]) + if (vector >= dev->msix_entries_nr || !dev->msix_entry_used[vector]) { return; + } + if (msix_is_masked(dev, vector)) { msix_set_pending(dev, vector); return; @@ -483,8 +511,10 @@ void msix_reset(PCIDevice *dev) /* Mark vector as used. */ int msix_vector_use(PCIDevice *dev, unsigned vector) { - if (vector >= dev->msix_entries_nr) + if (vector >= dev->msix_entries_nr) { return -EINVAL; + } + dev->msix_entry_used[vector]++; return 0; } diff --git a/hw/pci/pci.c b/hw/pci/pci.c index 47ca3af69a..a563555e7d 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -2195,7 +2195,7 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom, snprintf(name, sizeof(name), "%s.rom", object_get_typename(OBJECT(pdev))); } pdev->has_rom = true; - memory_region_init_ram(&pdev->rom, OBJECT(pdev), name, size, &error_fatal); + memory_region_init_rom(&pdev->rom, OBJECT(pdev), name, size, &error_fatal); vmstate_register_ram(&pdev->rom, &pdev->qdev); ptr = memory_region_get_ram_ptr(&pdev->rom); load_image(path, ptr); diff --git a/hw/s390x/s390-pci-bus.h b/hw/s390x/s390-pci-bus.h index 0aad9cc272..dcbf4820c9 100644 --- a/hw/s390x/s390-pci-bus.h +++ b/hw/s390x/s390-pci-bus.h @@ -279,7 +279,7 @@ typedef struct S390PCIIOMMUTable { S390PCIIOMMU *iommu[PCI_SLOT_MAX]; } S390PCIIOMMUTable; -typedef struct S390PCIBusDevice { +struct S390PCIBusDevice { DeviceState qdev; PCIDevice *pdev; ZpciState state; @@ -301,7 +301,7 @@ typedef struct S390PCIBusDevice { IndAddr *indicator; QEMUTimer *release_timer; QTAILQ_ENTRY(S390PCIBusDevice) link; -} S390PCIBusDevice; +}; typedef struct S390PCIBus { BusState qbus; diff --git a/hw/s390x/s390-virtio.c b/hw/s390x/s390-virtio.c index 0a963473ad..7a3a7fe5fd 100644 --- a/hw/s390x/s390-virtio.c +++ b/hw/s390x/s390-virtio.c @@ -204,8 +204,8 @@ void s390_machine_reset(void) { S390CPU *ipl_cpu = S390_CPU(qemu_get_cpu(0)); - qemu_devices_reset(); s390_cmma_reset(); + qemu_devices_reset(); s390_crypto_reset(); /* all cpus are stopped - configure and start the ipl cpu only */ diff --git a/hw/scsi/megasas.c b/hw/scsi/megasas.c index 6aad7c9a06..1a8b04c6d7 100644 --- a/hw/scsi/megasas.c +++ b/hw/scsi/megasas.c @@ -2367,9 +2367,11 @@ static void megasas_scsi_realize(PCIDevice *dev, Error **errp) if (megasas_use_msix(s) && msix_init(dev, 15, &s->mmio_io, b->mmio_bar, 0x2000, - &s->mmio_io, b->mmio_bar, 0x3800, 0x68)) { + &s->mmio_io, b->mmio_bar, 0x3800, 0x68, NULL)) { + /* TODO: check msix_init's error, and should fail on msix=on */ s->msix = ON_OFF_AUTO_OFF; } + if (pci_is_express(dev)) { pcie_endpoint_cap_init(dev, 0xa0); } diff --git a/hw/usb/hcd-xhci.c b/hw/usb/hcd-xhci.c index e0b516987f..f8106789d8 100644 --- a/hw/usb/hcd-xhci.c +++ b/hw/usb/hcd-xhci.c @@ -3627,25 +3627,6 @@ static void usb_xhci_realize(struct PCIDevice *dev, Error **errp) dev->config[PCI_CACHE_LINE_SIZE] = 0x10; dev->config[0x60] = 0x30; /* release number */ - usb_xhci_init(xhci); - - if (xhci->msi != ON_OFF_AUTO_OFF) { - ret = msi_init(dev, 0x70, xhci->numintrs, true, false, &err); - /* Any error other than -ENOTSUP(board's MSI support is broken) - * is a programming error */ - assert(!ret || ret == -ENOTSUP); - if (ret && xhci->msi == ON_OFF_AUTO_ON) { - /* Can't satisfy user's explicit msi=on request, fail */ - error_append_hint(&err, "You have to use msi=auto (default) or " - "msi=off with this machine type.\n"); - error_propagate(errp, err); - return; - } - assert(!err || xhci->msi == ON_OFF_AUTO_AUTO); - /* With msi=auto, we fall back to MSI off silently */ - error_free(err); - } - if (xhci->numintrs > MAXINTRS) { xhci->numintrs = MAXINTRS; } @@ -3667,6 +3648,24 @@ static void usb_xhci_realize(struct PCIDevice *dev, Error **errp) xhci->max_pstreams_mask = 0; } + if (xhci->msi != ON_OFF_AUTO_OFF) { + ret = msi_init(dev, 0x70, xhci->numintrs, true, false, &err); + /* Any error other than -ENOTSUP(board's MSI support is broken) + * is a programming error */ + assert(!ret || ret == -ENOTSUP); + if (ret && xhci->msi == ON_OFF_AUTO_ON) { + /* Can't satisfy user's explicit msi=on request, fail */ + error_append_hint(&err, "You have to use msi=auto (default) or " + "msi=off with this machine type.\n"); + error_propagate(errp, err); + return; + } + assert(!err || xhci->msi == ON_OFF_AUTO_AUTO); + /* With msi=auto, we fall back to MSI off silently */ + error_free(err); + } + + usb_xhci_init(xhci); xhci->mfwrap_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, xhci_mfwrap_timer, xhci); memory_region_init(&xhci->mem, OBJECT(xhci), "xhci", LEN_REGS); @@ -3704,11 +3703,11 @@ static void usb_xhci_realize(struct PCIDevice *dev, Error **errp) } if (xhci->msix != ON_OFF_AUTO_OFF) { - /* TODO check for errors */ + /* TODO check for errors, and should fail when msix=on */ msix_init(dev, xhci->numintrs, &xhci->mem, 0, OFF_MSIX_TABLE, &xhci->mem, 0, OFF_MSIX_PBA, - 0x90); + 0x90, NULL); } } diff --git a/hw/usb/trace-events b/hw/usb/trace-events index 2d42fd45da..fdd1d29030 100644 --- a/hw/usb/trace-events +++ b/hw/usb/trace-events @@ -60,7 +60,6 @@ usb_ohci_mem_read_bad_offset(uint32_t addr) "%x" usb_ohci_mem_write_unaligned(uint32_t addr) "at %x" usb_ohci_mem_write_bad_offset(uint32_t addr) "%x" usb_ohci_process_lists(uint32_t head, uint32_t cur) "head %x, cur %x" -usb_ohci_bus_eof_timer_failed(const char *name) "%s: timer_new_ns failed" usb_ohci_set_frame_interval(const char *name, uint16_t fi_x, uint16_t fi_u) "%s: FrameInterval = 0x%x (%u)" usb_ohci_hub_power_up(void) "powered up all ports" usb_ohci_hub_power_down(void) "powered down all ports" diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 882d3a91b6..332f41d662 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -1432,6 +1432,7 @@ static void vfio_msix_early_setup(VFIOPCIDevice *vdev, Error **errp) static int vfio_msix_setup(VFIOPCIDevice *vdev, int pos, Error **errp) { int ret; + Error *err = NULL; vdev->msix->pending = g_malloc0(BITS_TO_LONGS(vdev->msix->entries) * sizeof(unsigned long)); @@ -1439,12 +1440,15 @@ static int vfio_msix_setup(VFIOPCIDevice *vdev, int pos, Error **errp) vdev->bars[vdev->msix->table_bar].region.mem, vdev->msix->table_bar, vdev->msix->table_offset, vdev->bars[vdev->msix->pba_bar].region.mem, - vdev->msix->pba_bar, vdev->msix->pba_offset, pos); + vdev->msix->pba_bar, vdev->msix->pba_offset, pos, + &err); if (ret < 0) { if (ret == -ENOTSUP) { + error_report_err(err); return 0; } - error_setg(errp, "msix_init failed"); + + error_propagate(errp, err); return ret; } diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events index ef81609b98..8de8281357 100644 --- a/hw/vfio/trace-events +++ b/hw/vfio/trace-events @@ -46,7 +46,7 @@ vfio_pci_emulated_device_id(const char *name, uint16_t val) "%s %04x" vfio_pci_emulated_sub_vendor_id(const char *name, uint16_t val) "%s %04x" vfio_pci_emulated_sub_device_id(const char *name, uint16_t val) "%s %04x" -# hw/vfio/pci-quirks. +# hw/vfio/pci-quirks.c vfio_quirk_rom_blacklisted(const char *name, uint16_t vid, uint16_t did) "%s %04x:%04x" vfio_quirk_generic_window_address_write(const char *name, const char * region_name, uint64_t data) "%s %s 0x%"PRIx64 vfio_quirk_generic_window_data_read(const char *name, const char * region_name, uint64_t data) "%s %s 0x%"PRIx64 diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index b124d97d7c..febe519bbd 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -612,7 +612,8 @@ static void vhost_set_memory(MemoryListener *listener, static bool vhost_section(MemoryRegionSection *section) { - return memory_region_is_ram(section->mr); + return memory_region_is_ram(section->mr) && + !memory_region_is_rom(section->mr); } static void vhost_begin(MemoryListener *listener) diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c index b5af2a00f3..5ce42af9d4 100644 --- a/hw/virtio/virtio-pci.c +++ b/hw/virtio/virtio-pci.c @@ -1686,9 +1686,9 @@ static void virtio_pci_device_plugged(DeviceState *d, Error **errp) if (proxy->nvectors) { int err = msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, - proxy->msix_bar_idx); + proxy->msix_bar_idx, NULL); if (err) { - /* Notice when a system that supports MSIx can't initialize it. */ + /* Notice when a system that supports MSIx can't initialize it */ if (err != -ENOTSUP) { error_report("unable to init msix vectors to %" PRIu32, proxy->nvectors); diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index f292a53940..63657066e7 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -1383,7 +1383,7 @@ static void virtio_set_isr(VirtIODevice *vdev, int value) } } -bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq) +static bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq) { uint16_t old, new; bool v; diff --git a/hw/xen/trace-events b/hw/xen/trace-events new file mode 100644 index 0000000000..c4fb6f1aea --- /dev/null +++ b/hw/xen/trace-events @@ -0,0 +1,13 @@ +# See docs/tracing.txt for syntax documentation. + +# include/hw/xen/xen_common.h +xen_default_ioreq_server(void) "" +xen_ioreq_server_create(uint32_t id) "id: %u" +xen_ioreq_server_destroy(uint32_t id) "id: %u" +xen_ioreq_server_state(uint32_t id, bool enable) "id: %u: enable: %i" +xen_map_mmio_range(uint32_t id, uint64_t start_addr, uint64_t end_addr) "id: %u start: %#"PRIx64" end: %#"PRIx64 +xen_unmap_mmio_range(uint32_t id, uint64_t start_addr, uint64_t end_addr) "id: %u start: %#"PRIx64" end: %#"PRIx64 +xen_map_portio_range(uint32_t id, uint64_t start_addr, uint64_t end_addr) "id: %u start: %#"PRIx64" end: %#"PRIx64 +xen_unmap_portio_range(uint32_t id, uint64_t start_addr, uint64_t end_addr) "id: %u start: %#"PRIx64" end: %#"PRIx64 +xen_map_pcidev(uint32_t id, uint8_t bus, uint8_t dev, uint8_t func) "id: %u bdf: %02x.%02x.%02x" +xen_unmap_pcidev(uint32_t id, uint8_t bus, uint8_t dev, uint8_t func) "id: %u bdf: %02x.%02x.%02x" diff --git a/include/exec/cpu_ldst_template.h b/include/exec/cpu_ldst_template.h index eaf69a1ad4..4db2302962 100644 --- a/include/exec/cpu_ldst_template.h +++ b/include/exec/cpu_ldst_template.h @@ -25,7 +25,7 @@ */ #if !defined(SOFTMMU_CODE_ACCESS) -#include "trace.h" +#include "trace-root.h" #endif #include "trace/mem.h" diff --git a/include/exec/cpu_ldst_useronly_template.h b/include/exec/cpu_ldst_useronly_template.h index b1378bfae8..7b8c7c506e 100644 --- a/include/exec/cpu_ldst_useronly_template.h +++ b/include/exec/cpu_ldst_useronly_template.h @@ -24,7 +24,7 @@ */ #if !defined(CODE_ACCESS) -#include "trace.h" +#include "trace-root.h" #endif #include "trace/mem.h" diff --git a/include/hw/compat.h b/include/hw/compat.h index ee0dd1b5df..b7db43803c 100644 --- a/include/hw/compat.h +++ b/include/hw/compat.h @@ -14,6 +14,10 @@ .driver = "pflash_cfi01",\ .property = "old-multiple-chip-handling",\ .value = "on",\ + },{\ + .driver = "pci-bridge",\ + .property = "shpc",\ + .value = "on",\ }, #define HW_COMPAT_2_7 \ diff --git a/include/hw/input/ps2.h b/include/hw/input/ps2.h index b9ceee4154..0fec91cdb0 100644 --- a/include/hw/input/ps2.h +++ b/include/hw/input/ps2.h @@ -25,6 +25,12 @@ #ifndef HW_PS2_H #define HW_PS2_H +#define PS2_MOUSE_BUTTON_LEFT 0x01 +#define PS2_MOUSE_BUTTON_MIDDLE 0x02 +#define PS2_MOUSE_BUTTON_RIGHT 0x04 +#define PS2_MOUSE_BUTTON_SIDE 0x08 +#define PS2_MOUSE_BUTTON_EXTRA 0x10 + /* ps2.c */ void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg); void *ps2_mouse_init(void (*update_irq)(void *, int), void *update_arg); diff --git a/include/hw/pci/msix.h b/include/hw/pci/msix.h index 048a29dd2f..1f27658d35 100644 --- a/include/hw/pci/msix.h +++ b/include/hw/pci/msix.h @@ -9,9 +9,10 @@ MSIMessage msix_get_message(PCIDevice *dev, unsigned int vector); int msix_init(PCIDevice *dev, unsigned short nentries, MemoryRegion *table_bar, uint8_t table_bar_nr, unsigned table_offset, MemoryRegion *pba_bar, - uint8_t pba_bar_nr, unsigned pba_offset, uint8_t cap_pos); + uint8_t pba_bar_nr, unsigned pba_offset, uint8_t cap_pos, + Error **errp); int msix_init_exclusive_bar(PCIDevice *dev, unsigned short nentries, - uint8_t bar_nr); + uint8_t bar_nr, Error **errp); void msix_write_config(PCIDevice *dev, uint32_t address, uint32_t val, int len); diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h index 772692f1b2..cbc1fdfb5b 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -96,6 +96,7 @@ #define PCI_DEVICE_ID_REDHAT_PXB 0x0009 #define PCI_DEVICE_ID_REDHAT_BRIDGE_SEAT 0x000a #define PCI_DEVICE_ID_REDHAT_PXB_PCIE 0x000b +#define PCI_DEVICE_ID_REDHAT_PCIE_RP 0x000c #define PCI_DEVICE_ID_REDHAT_QXL 0x0100 #define FMT_PCIBUS PRIx64 diff --git a/include/hw/pci/pcie_port.h b/include/hw/pci/pcie_port.h index f7b64db00c..13332668e8 100644 --- a/include/hw/pci/pcie_port.h +++ b/include/hw/pci/pcie_port.h @@ -57,4 +57,23 @@ PCIESlot *pcie_chassis_find_slot(uint8_t chassis, uint16_t slot); int pcie_chassis_add_slot(struct PCIESlot *slot); void pcie_chassis_del_slot(PCIESlot *s); +#define TYPE_PCIE_ROOT_PORT "pcie-root-port-base" +#define PCIE_ROOT_PORT_CLASS(klass) \ + OBJECT_CLASS_CHECK(PCIERootPortClass, (klass), TYPE_PCIE_ROOT_PORT) +#define PCIE_ROOT_PORT_GET_CLASS(obj) \ + OBJECT_GET_CLASS(PCIERootPortClass, (obj), TYPE_PCIE_ROOT_PORT) + +typedef struct PCIERootPortClass { + PCIDeviceClass parent_class; + + uint8_t (*aer_vector)(const PCIDevice *dev); + int (*interrupts_init)(PCIDevice *dev, Error **errp); + void (*interrupts_uninit)(PCIDevice *dev); + + int exp_offset; + int aer_offset; + int ssvid_offset; + int ssid; +} PCIERootPortClass; + #endif /* QEMU_PCIE_PORT_H */ diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index 6523bacd2f..525da24222 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -182,7 +182,6 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes, unsigned int *out_bytes, unsigned max_in_bytes, unsigned max_out_bytes); -bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq); void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq); void virtio_notify(VirtIODevice *vdev, VirtQueue *vq); diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h index 8e1580d526..dce76ee162 100644 --- a/include/hw/xen/xen_common.h +++ b/include/hw/xen/xen_common.h @@ -18,7 +18,7 @@ #include "hw/xen/xen.h" #include "hw/pci/pci.h" #include "qemu/queue.h" -#include "trace.h" +#include "hw/xen/trace.h" /* * We don't support Xen prior to 4.2.0. diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h index 157698bfa9..e0ce9ffb28 100644 --- a/include/qemu/compiler.h +++ b/include/qemu/compiler.h @@ -85,8 +85,20 @@ #define typeof_field(type, field) typeof(((type *)0)->field) #define type_check(t1,t2) ((t1*)0 - (t2*)0) -#define QEMU_BUILD_BUG_ON(x) \ - typedef char glue(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused)); +#define QEMU_BUILD_BUG_ON_STRUCT(x) \ + struct { \ + int:(x) ? -1 : 1; \ + } + +#ifdef __COUNTER__ +#define QEMU_BUILD_BUG_ON(x) typedef QEMU_BUILD_BUG_ON_STRUCT(x) \ + glue(qemu_build_bug_on__, __COUNTER__) __attribute__((unused)) +#else +#define QEMU_BUILD_BUG_ON(x) +#endif + +#define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \ + sizeof(QEMU_BUILD_BUG_ON_STRUCT(x))) #if defined __GNUC__ # if !QEMU_GNUC_PREREQ(4, 4) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 689f253ea7..56c9e22405 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -198,8 +198,15 @@ extern int daemon(int, int); #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) #endif +/* + * &(x)[0] is always a pointer - if it's same type as x then the argument is a + * pointer, not an array. + */ +#define QEMU_IS_ARRAY(x) (!__builtin_types_compatible_p(typeof(x), \ + typeof(&(x)[0]))) #ifndef ARRAY_SIZE -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#define ARRAY_SIZE(x) ((sizeof(x) / sizeof((x)[0])) + \ + QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(x))) #endif int qemu_daemon(int nochdir, int noclose); diff --git a/include/sysemu/char.h b/include/sysemu/char.h index da0e7dd494..450881d42c 100644 --- a/include/sysemu/char.h +++ b/include/sysemu/char.h @@ -2,12 +2,7 @@ #define QEMU_CHAR_H #include "qemu-common.h" -#include "qemu/queue.h" #include "qemu/option.h" -#include "qemu/config-file.h" -#include "block/aio.h" -#include "qapi/qmp/qobject.h" -#include "qapi/qmp/qstring.h" #include "qemu/main-loop.h" #include "qemu/bitmap.h" #include "qom/object.h" @@ -22,6 +17,7 @@ typedef enum { CHR_EVENT_CLOSED /* connection closed */ } QEMUChrEvent; +#define CHR_READ_BUF_LEN 4096 #define CHR_IOCTL_SERIAL_SET_PARAMS 1 typedef struct { @@ -74,7 +70,7 @@ typedef enum { QEMU_CHAR_FEATURE_REPLAY, QEMU_CHAR_FEATURE_LAST, -} CharDriverFeature; +} ChardevFeature; /* This is the backend as seen by frontend, the actual backend is * Chardev */ @@ -88,8 +84,6 @@ typedef struct CharBackend { int fe_open; } CharBackend; -typedef struct CharDriver CharDriver; - struct Chardev { Object parent_obj; @@ -143,7 +137,7 @@ Chardev *qemu_chr_new(const char *label, const char *filename); * @qemu_chr_fe_disconnect: * * Close a fd accpeted by character backend. - * Without associated CharDriver, do nothing. + * Without associated Chardev, do nothing. */ void qemu_chr_fe_disconnect(CharBackend *be); @@ -158,7 +152,7 @@ void qemu_chr_cleanup(void); * @qemu_chr_fe_wait_connected: * * Wait for characted backend to be connected, return < 0 on error or - * if no assicated CharDriver. + * if no assicated Chardev. */ int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp); @@ -185,19 +179,12 @@ Chardev *qemu_chr_new_noreplay(const char *label, const char *filename); void qemu_chr_delete(Chardev *chr); /** - * @qemu_chr_free: - * - * Destroy a character backend. - */ -void qemu_chr_free(Chardev *chr); - -/** * @qemu_chr_fe_set_echo: * * Ask the backend to override its normal echo setting. This only really * applies to the stdio backend and is used by the QMP server such that you * can see what you type if you try to type QMP commands. - * Without associated CharDriver, do nothing. + * Without associated Chardev, do nothing. * * @echo true to enable echo, false to disable echo */ @@ -208,7 +195,7 @@ void qemu_chr_fe_set_echo(CharBackend *be, bool echo); * * Set character frontend open status. This is an indication that the * front end is ready (or not) to begin doing I/O. - * Without associated CharDriver, do nothing. + * Without associated Chardev, do nothing. */ void qemu_chr_fe_set_open(CharBackend *be, int fe_open); @@ -217,7 +204,7 @@ void qemu_chr_fe_set_open(CharBackend *be, int fe_open); * * Write to a character backend using a printf style interface. This * function is thread-safe. It does nothing without associated - * CharDriver. + * Chardev. * * @fmt see #printf */ @@ -230,7 +217,7 @@ void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...) * If the backend is connected, create and add a #GSource that fires * when the given condition (typically G_IO_OUT|G_IO_HUP or G_IO_HUP) * is active; return the #GSource's tag. If it is disconnected, - * or without associated CharDriver, return 0. + * or without associated Chardev, return 0. * * @cond the condition to poll for * @func the function to call when the condition happens @@ -251,7 +238,7 @@ guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond, * @buf the data * @len the number of bytes to send * - * Returns: the number of bytes consumed (0 if no assicated CharDriver) + * Returns: the number of bytes consumed (0 if no assicated Chardev) */ int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len); @@ -266,7 +253,7 @@ int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len); * @buf the data * @len the number of bytes to send * - * Returns: the number of bytes consumed (0 if no assicated CharDriver) + * Returns: the number of bytes consumed (0 if no assicated Chardev) */ int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len); @@ -278,7 +265,7 @@ int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len); * @buf the data buffer * @len the number of bytes to read * - * Returns: the number of bytes read (0 if no assicated CharDriver) + * Returns: the number of bytes read (0 if no assicated Chardev) */ int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len); @@ -291,7 +278,7 @@ int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len); * @arg the data associated with @cmd * * Returns: if @cmd is not supported by the backend or there is no - * associated CharDriver, -ENOTSUP, otherwise the return + * associated Chardev, -ENOTSUP, otherwise the return * value depends on the semantics of @cmd */ int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg); @@ -331,7 +318,7 @@ int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int num); * result in overwriting the fd array with the new value without being send. * Upon writing the message the fd array is freed. * - * Returns: -1 if fd passing isn't supported or no associated CharDriver. + * Returns: -1 if fd passing isn't supported or no associated Chardev. */ int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num); @@ -382,7 +369,7 @@ void qemu_chr_be_event(Chardev *s, int event); * @qemu_chr_fe_init: * * Initializes a front end for the given CharBackend and - * CharDriver. Call qemu_chr_fe_deinit() to remove the association and + * Chardev. Call qemu_chr_fe_deinit() to remove the association and * release the driver. * * Returns: false on error. @@ -393,16 +380,16 @@ bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp); * @qemu_chr_fe_get_driver: * * Returns the driver associated with a CharBackend or NULL if no - * associated CharDriver. + * associated Chardev. */ Chardev *qemu_chr_fe_get_driver(CharBackend *be); /** * @qemu_chr_fe_deinit: * - * Dissociate the CharBackend from the CharDriver. + * Dissociate the CharBackend from the Chardev. * - * Safe to call without associated CharDriver. + * Safe to call without associated Chardev. */ void qemu_chr_fe_deinit(CharBackend *b); @@ -421,7 +408,7 @@ void qemu_chr_fe_deinit(CharBackend *b); * Set the front end char handlers. The front end takes the focus if * any of the handler is non-NULL. * - * Without associated CharDriver, nothing is changed. + * Without associated Chardev, nothing is changed. */ void qemu_chr_fe_set_handlers(CharBackend *b, IOCanReadHandler *fd_can_read, @@ -436,7 +423,7 @@ void qemu_chr_fe_set_handlers(CharBackend *b, * * Take the focus (if the front end is muxed). * - * Without associated CharDriver, nothing is changed. + * Without associated Chardev, nothing is changed. */ void qemu_chr_fe_take_focus(CharBackend *b); @@ -446,10 +433,12 @@ int qemu_chr_add_client(Chardev *s, int fd); Chardev *qemu_chr_find(const char *name); bool qemu_chr_has_feature(Chardev *chr, - CharDriverFeature feature); + ChardevFeature feature); void qemu_chr_set_feature(Chardev *chr, - CharDriverFeature feature); + ChardevFeature feature); QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename); +int qemu_chr_write_all(Chardev *s, const uint8_t *buf, int len); +int qemu_chr_wait_connected(Chardev *chr, Error **errp); #define TYPE_CHARDEV "chardev" #define CHARDEV(obj) OBJECT_CHECK(Chardev, (obj), TYPE_CHARDEV) @@ -472,8 +461,6 @@ QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename); #define TYPE_CHARDEV_SOCKET "chardev-socket" #define TYPE_CHARDEV_UDP "chardev-udp" -#define CHARDEV_IS_MUX(chr) \ - object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_MUX) #define CHARDEV_IS_RINGBUF(chr) \ object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_RINGBUF) #define CHARDEV_IS_PTY(chr) \ @@ -483,6 +470,7 @@ typedef struct ChardevClass { ObjectClass parent_class; bool internal; /* TODO: eventually use TYPE_USER_CREATABLE */ + void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp); void (*open)(Chardev *chr, ChardevBackend *backend, bool *be_opened, Error **errp); @@ -496,24 +484,15 @@ typedef struct ChardevClass { int (*set_msgfds)(Chardev *s, int *fds, int num); int (*chr_add_client)(Chardev *chr, int fd); int (*chr_wait_connected)(Chardev *chr, Error **errp); - void (*chr_free)(Chardev *chr); void (*chr_disconnect)(Chardev *chr); void (*chr_accept_input)(Chardev *chr); void (*chr_set_echo)(Chardev *chr, bool echo); void (*chr_set_fe_open)(Chardev *chr, int fe_open); } ChardevClass; -struct CharDriver { - ChardevBackendKind kind; - const char *alias; - void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp); -}; - Chardev *qemu_chardev_new(const char *id, const char *typename, ChardevBackend *backend, Error **errp); -void register_char_driver(const CharDriver *driver); - extern int term_escape_char; /* console.c */ diff --git a/include/trace.h b/include/trace.h deleted file mode 100644 index ac9ff3dddd..0000000000 --- a/include/trace.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef TRACE_H -#define TRACE_H - -#include "trace/generated-tracers.h" - -#endif /* TRACE_H */ @@ -29,7 +29,7 @@ #include "qemu-common.h" #include "cpu.h" #include "exec/ioport.h" -#include "trace.h" +#include "trace-root.h" #include "exec/memory.h" #include "exec/address-spaces.h" @@ -34,7 +34,7 @@ #include "exec/ram_addr.h" #include "exec/address-spaces.h" #include "qemu/event_notifier.h" -#include "trace.h" +#include "trace-root.h" #include "hw/irq.h" #include "hw/boards.h" diff --git a/linux-user/main.c b/linux-user/main.c index 30049581ef..e588f58f2a 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -4322,6 +4322,8 @@ int main(int argc, char **argv, char **envp) # endif #elif defined TARGET_SH4 cpu_model = TYPE_SH7785_CPU; +#elif defined TARGET_S390X + cpu_model = "qemu"; #else cpu_model = "any"; #endif @@ -24,7 +24,7 @@ #include "qemu/bitops.h" #include "qemu/error-report.h" #include "qom/object.h" -#include "trace.h" +#include "trace-root.h" #include "exec/memory-internal.h" #include "exec/ram_addr.h" diff --git a/migration/trace-events b/migration/trace-events index 48e531d3b8..fa660e35b1 100644 --- a/migration/trace-events +++ b/migration/trace-events @@ -4,8 +4,6 @@ qemu_loadvm_state_section(unsigned int section_type) "%d" qemu_loadvm_state_section_command(int ret) "%d" qemu_loadvm_state_section_partend(uint32_t section_id) "%u" -qemu_loadvm_state_main(void) "" -qemu_loadvm_state_main_quit_parent(void) "" qemu_loadvm_state_post_main(int ret) "%d" qemu_loadvm_state_section_startfull(uint32_t section_id, const char *idstr, uint32_t instance_id, uint32_t version_id) "%u(%s) %u %u" qemu_savevm_send_packaged(void) "" @@ -118,7 +116,6 @@ qemu_rdma_accept_incoming_migration_accepted(void) "" qemu_rdma_accept_pin_state(bool pin) "%d" qemu_rdma_accept_pin_verbsc(void *verbs) "Verbs context after listen: %p" qemu_rdma_block_for_wrid_miss(const char *wcompstr, int wcomp, const char *gcompstr, uint64_t req) "A Wanted wrid %s (%d) but got %s (%" PRIu64 ")" -qemu_rdma_block_for_wrid_miss_b(const char *wcompstr, int wcomp, const char *gcompstr, uint64_t req) "B Wanted wrid %s (%d) but got %s (%" PRIu64 ")" qemu_rdma_cleanup_disconnect(void) "" qemu_rdma_cleanup_waiting_for_disconnect(void) "" qemu_rdma_close(void) "" @@ -40,6 +40,7 @@ #include "sysemu/sysemu.h" #include "sysemu/numa.h" #include "monitor/monitor.h" +#include "qemu/config-file.h" #include "qemu/readline.h" #include "ui/console.h" #include "ui/input.h" @@ -59,7 +60,7 @@ #include "qapi/qmp/json-streamer.h" #include "qapi/qmp/json-parser.h" #include "qom/object_interfaces.h" -#include "trace.h" +#include "trace-root.h" #include "trace/control.h" #include "monitor/hmp-target.h" #ifdef CONFIG_TRACE_SIMPLE diff --git a/net/vhost-user.c b/net/vhost-user.c index b0f0ab6cc8..77b8110f8c 100644 --- a/net/vhost-user.c +++ b/net/vhost-user.c @@ -151,7 +151,10 @@ static void vhost_user_cleanup(NetClientState *nc) s->vhost_net = NULL; } if (nc->queue_index == 0) { + Chardev *chr = qemu_chr_fe_get_driver(&s->chr); + qemu_chr_fe_deinit(&s->chr); + qemu_chr_delete(chr); } qemu_purge_queued_packets(nc); diff --git a/qapi-schema.json b/qapi-schema.json index 82fabc6e24..cbdffddbc6 100644 --- a/qapi-schema.json +++ b/qapi-schema.json @@ -5390,10 +5390,15 @@ # # Button of a pointer input device (mouse, tablet). # +# @side: front side button of a 5-button mouse (since 2.9) +# +# @extra: rear side button of a 5-button mouse (since 2.9) +# # Since: 2.0 ## { 'enum' : 'InputButton', - 'data' : [ 'left', 'middle', 'right', 'wheel-up', 'wheel-down' ] } + 'data' : [ 'left', 'middle', 'right', 'wheel-up', 'wheel-down', 'side', + 'extra' ] } ## # @InputAxis: diff --git a/qapi/qapi-visit-core.c b/qapi/qapi-visit-core.c index 63bd97b341..e6e93f02e6 100644 --- a/qapi/qapi-visit-core.c +++ b/qapi/qapi-visit-core.c @@ -374,6 +374,7 @@ void visit_type_enum(Visitor *v, const char *name, int *obj, const char *const strings[], Error **errp) { assert(obj && strings); + trace_visit_type_enum(v, name, obj); switch (v->type) { case VISITOR_INPUT: input_type_enum(v, name, obj, strings, errp); diff --git a/qapi/trace-events b/qapi/trace-events index 2c5d3bc7d7..9cbb61b2bd 100644 --- a/qapi/trace-events +++ b/qapi/trace-events @@ -1,4 +1,4 @@ -# qapi-visit-core.c +# qapi/qapi-visit-core.c visit_free(void *v) "v=%p" visit_complete(void *v, void *opaque) "v=%p opaque=%p" diff --git a/qemu-char.c b/qemu-char.c deleted file mode 100644 index 6b4a299702..0000000000 --- a/qemu-char.c +++ /dev/null @@ -1,5171 +0,0 @@ -/* - * 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. - */ -#include "qemu/osdep.h" -#include "qemu-common.h" -#include "qemu/cutils.h" -#include "monitor/monitor.h" -#include "sysemu/sysemu.h" -#include "sysemu/block-backend.h" -#include "qemu/error-report.h" -#include "qemu/timer.h" -#include "sysemu/char.h" -#include "hw/usb.h" -#include "qmp-commands.h" -#include "qapi/clone-visitor.h" -#include "qapi-visit.h" -#include "qemu/base64.h" -#include "io/channel-socket.h" -#include "io/channel-file.h" -#include "io/channel-tls.h" -#include "sysemu/replay.h" -#include "qemu/help_option.h" - -#include <zlib.h> - -#ifndef _WIN32 -#include <sys/times.h> -#include <sys/wait.h> -#include <termios.h> -#include <sys/ioctl.h> -#include <sys/resource.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <net/if.h> -#include <arpa/inet.h> -#include <netdb.h> -#include <sys/select.h> -#ifdef CONFIG_BSD -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) -#include <dev/ppbus/ppi.h> -#include <dev/ppbus/ppbconf.h> -#elif defined(__DragonFly__) -#include <dev/misc/ppi/ppi.h> -#include <bus/ppbus/ppbconf.h> -#endif -#else -#ifdef __linux__ -#include <linux/ppdev.h> -#include <linux/parport.h> -#endif -#ifdef __sun__ -#include <sys/ethernet.h> -#include <sys/sockio.h> -#include <netinet/arp.h> -#include <netinet/in.h> -#include <netinet/in_systm.h> -#include <netinet/ip.h> -#include <netinet/ip_icmp.h> // must come after ip.h -#include <netinet/udp.h> -#include <netinet/tcp.h> -#endif -#endif -#endif - -#include "qemu/sockets.h" -#include "ui/qemu-spice.h" - -#define READ_BUF_LEN 4096 -#define READ_RETRIES 10 -#define TCP_MAX_FDS 16 - -typedef struct MuxChardev MuxChardev; - -/***********************************************************/ -/* Socket address helpers */ - -static char *SocketAddress_to_str(const char *prefix, SocketAddress *addr, - bool is_listen, bool is_telnet) -{ - switch (addr->type) { - case SOCKET_ADDRESS_KIND_INET: - return g_strdup_printf("%s%s:%s:%s%s", prefix, - is_telnet ? "telnet" : "tcp", - addr->u.inet.data->host, - addr->u.inet.data->port, - is_listen ? ",server" : ""); - break; - case SOCKET_ADDRESS_KIND_UNIX: - return g_strdup_printf("%sunix:%s%s", prefix, - addr->u.q_unix.data->path, - is_listen ? ",server" : ""); - break; - case SOCKET_ADDRESS_KIND_FD: - return g_strdup_printf("%sfd:%s%s", prefix, addr->u.fd.data->str, - is_listen ? ",server" : ""); - break; - default: - abort(); - } -} - -static char *sockaddr_to_str(struct sockaddr_storage *ss, socklen_t ss_len, - struct sockaddr_storage *ps, socklen_t ps_len, - bool is_listen, bool is_telnet) -{ - char shost[NI_MAXHOST], sserv[NI_MAXSERV]; - char phost[NI_MAXHOST], pserv[NI_MAXSERV]; - const char *left = "", *right = ""; - - switch (ss->ss_family) { -#ifndef _WIN32 - case AF_UNIX: - return g_strdup_printf("unix:%s%s", - ((struct sockaddr_un *)(ss))->sun_path, - is_listen ? ",server" : ""); -#endif - case AF_INET6: - left = "["; - right = "]"; - /* fall through */ - case AF_INET: - getnameinfo((struct sockaddr *) ss, ss_len, shost, sizeof(shost), - sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV); - getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost), - pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV); - return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s", - is_telnet ? "telnet" : "tcp", - left, shost, right, sserv, - is_listen ? ",server" : "", - left, phost, right, pserv); - - default: - return g_strdup_printf("unknown"); - } -} - -/***********************************************************/ -/* character device */ - -static QTAILQ_HEAD(ChardevHead, Chardev) chardevs = - QTAILQ_HEAD_INITIALIZER(chardevs); - -void qemu_chr_be_event(Chardev *s, int event) -{ - CharBackend *be = s->be; - - /* Keep track if the char device is open */ - switch (event) { - case CHR_EVENT_OPENED: - s->be_open = 1; - break; - case CHR_EVENT_CLOSED: - s->be_open = 0; - break; - } - - if (!be || !be->chr_event) { - return; - } - - be->chr_event(be->opaque, event); -} - -void qemu_chr_be_generic_open(Chardev *s) -{ - qemu_chr_be_event(s, CHR_EVENT_OPENED); -} - - -/* Not reporting errors from writing to logfile, as logs are - * defined to be "best effort" only */ -static void qemu_chr_fe_write_log(Chardev *s, - const uint8_t *buf, size_t len) -{ - size_t done = 0; - ssize_t ret; - - if (s->logfd < 0) { - return; - } - - while (done < len) { - retry: - ret = write(s->logfd, buf + done, len - done); - if (ret == -1 && errno == EAGAIN) { - g_usleep(100); - goto retry; - } - - if (ret <= 0) { - return; - } - done += ret; - } -} - -static int qemu_chr_fe_write_buffer(Chardev *s, - const uint8_t *buf, int len, int *offset) -{ - ChardevClass *cc = CHARDEV_GET_CLASS(s); - int res = 0; - *offset = 0; - - qemu_mutex_lock(&s->chr_write_lock); - while (*offset < len) { - retry: - res = cc->chr_write(s, buf + *offset, len - *offset); - if (res < 0 && errno == EAGAIN) { - g_usleep(100); - goto retry; - } - - if (res <= 0) { - break; - } - - *offset += res; - } - if (*offset > 0) { - qemu_chr_fe_write_log(s, buf, *offset); - } - qemu_mutex_unlock(&s->chr_write_lock); - - return res; -} - -static bool qemu_chr_replay(Chardev *chr) -{ - return qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_REPLAY); -} - -int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len) -{ - Chardev *s = be->chr; - ChardevClass *cc; - int ret; - - if (!s) { - return 0; - } - - if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) { - int offset; - replay_char_write_event_load(&ret, &offset); - assert(offset <= len); - qemu_chr_fe_write_buffer(s, buf, offset, &offset); - return ret; - } - - cc = CHARDEV_GET_CLASS(s); - qemu_mutex_lock(&s->chr_write_lock); - ret = cc->chr_write(s, buf, len); - - if (ret > 0) { - qemu_chr_fe_write_log(s, buf, ret); - } - - qemu_mutex_unlock(&s->chr_write_lock); - - if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { - replay_char_write_event_save(ret, ret < 0 ? 0 : ret); - } - - return ret; -} - -static int qemu_chr_write_all(Chardev *s, const uint8_t *buf, int len) -{ - int offset; - int res; - - if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) { - replay_char_write_event_load(&res, &offset); - assert(offset <= len); - qemu_chr_fe_write_buffer(s, buf, offset, &offset); - return res; - } - - res = qemu_chr_fe_write_buffer(s, buf, len, &offset); - - if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { - replay_char_write_event_save(res, offset); - } - - if (res < 0) { - return res; - } - return offset; -} - -int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len) -{ - Chardev *s = be->chr; - - if (!s) { - return 0; - } - - return qemu_chr_write_all(s, buf, len); -} - -int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len) -{ - Chardev *s = be->chr; - int offset = 0, counter = 10; - int res; - - if (!s || !CHARDEV_GET_CLASS(s)->chr_sync_read) { - return 0; - } - - if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) { - return replay_char_read_all_load(buf); - } - - while (offset < len) { - retry: - res = CHARDEV_GET_CLASS(s)->chr_sync_read(s, buf + offset, - len - offset); - if (res == -1 && errno == EAGAIN) { - g_usleep(100); - goto retry; - } - - if (res == 0) { - break; - } - - if (res < 0) { - if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { - replay_char_read_all_save_error(res); - } - return res; - } - - offset += res; - - if (!counter--) { - break; - } - } - - if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { - replay_char_read_all_save_buf(buf, offset); - } - return offset; -} - -int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg) -{ - Chardev *s = be->chr; - int res; - - if (!s || !CHARDEV_GET_CLASS(s)->chr_ioctl || qemu_chr_replay(s)) { - res = -ENOTSUP; - } else { - res = CHARDEV_GET_CLASS(s)->chr_ioctl(s, cmd, arg); - } - - return res; -} - -int qemu_chr_be_can_write(Chardev *s) -{ - CharBackend *be = s->be; - - if (!be || !be->chr_can_read) { - return 0; - } - - return be->chr_can_read(be->opaque); -} - -void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len) -{ - CharBackend *be = s->be; - - if (be && be->chr_read) { - be->chr_read(be->opaque, buf, len); - } -} - -void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len) -{ - if (qemu_chr_replay(s)) { - if (replay_mode == REPLAY_MODE_PLAY) { - return; - } - replay_chr_be_write(s, buf, len); - } else { - qemu_chr_be_write_impl(s, buf, len); - } -} - -int qemu_chr_fe_get_msgfd(CharBackend *be) -{ - Chardev *s = be->chr; - int fd; - int res = (qemu_chr_fe_get_msgfds(be, &fd, 1) == 1) ? fd : -1; - if (s && qemu_chr_replay(s)) { - error_report("Replay: get msgfd is not supported " - "for serial devices yet"); - exit(1); - } - return res; -} - -int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int len) -{ - Chardev *s = be->chr; - - if (!s) { - return -1; - } - - return CHARDEV_GET_CLASS(s)->get_msgfds ? - CHARDEV_GET_CLASS(s)->get_msgfds(s, fds, len) : -1; -} - -int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num) -{ - Chardev *s = be->chr; - - if (!s) { - return -1; - } - - return CHARDEV_GET_CLASS(s)->set_msgfds ? - CHARDEV_GET_CLASS(s)->set_msgfds(s, fds, num) : -1; -} - -int qemu_chr_add_client(Chardev *s, int fd) -{ - return CHARDEV_GET_CLASS(s)->chr_add_client ? - CHARDEV_GET_CLASS(s)->chr_add_client(s, fd) : -1; -} - -void qemu_chr_fe_accept_input(CharBackend *be) -{ - Chardev *s = be->chr; - - if (!s) { - return; - } - - if (CHARDEV_GET_CLASS(s)->chr_accept_input) { - CHARDEV_GET_CLASS(s)->chr_accept_input(s); - } - qemu_notify_event(); -} - -void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...) -{ - char buf[READ_BUF_LEN]; - va_list ap; - va_start(ap, fmt); - vsnprintf(buf, sizeof(buf), fmt, ap); - /* XXX this blocks entire thread. Rewrite to use - * qemu_chr_fe_write and background I/O callbacks */ - qemu_chr_fe_write_all(be, (uint8_t *)buf, strlen(buf)); - va_end(ap); -} - -static void remove_fd_in_watch(Chardev *chr); -static void mux_chr_set_handlers(Chardev *chr, GMainContext *context); -static void mux_set_focus(Chardev *chr, int focus); - -static void qemu_char_open(Chardev *chr, ChardevBackend *backend, - bool *be_opened, Error **errp) -{ - ChardevClass *cc = CHARDEV_GET_CLASS(chr); - /* Any ChardevCommon member would work */ - ChardevCommon *common = backend ? backend->u.null.data : NULL; - - if (common && common->has_logfile) { - int flags = O_WRONLY | O_CREAT; - if (common->has_logappend && - common->logappend) { - flags |= O_APPEND; - } else { - flags |= O_TRUNC; - } - chr->logfd = qemu_open(common->logfile, flags, 0666); - if (chr->logfd < 0) { - error_setg_errno(errp, errno, - "Unable to open logfile %s", - common->logfile); - return; - } - } - - if (cc->open) { - cc->open(chr, backend, be_opened, errp); - } -} - -static void char_init(Object *obj) -{ - Chardev *chr = CHARDEV(obj); - - chr->logfd = -1; - qemu_mutex_init(&chr->chr_write_lock); -} - -static void char_finalize(Object *obj) -{ - Chardev *chr = CHARDEV(obj); - - if (chr->be) { - chr->be->chr = NULL; - } - g_free(chr->filename); - g_free(chr->label); - if (chr->logfd != -1) { - close(chr->logfd); - } - qemu_mutex_destroy(&chr->chr_write_lock); -} - -static const TypeInfo char_type_info = { - .name = TYPE_CHARDEV, - .parent = TYPE_OBJECT, - .instance_size = sizeof(Chardev), - .instance_init = char_init, - .instance_finalize = char_finalize, - .abstract = true, - .class_size = sizeof(ChardevClass), -}; - -static int null_chr_write(Chardev *chr, const uint8_t *buf, int len) -{ - return len; -} - -static void null_chr_open(Chardev *chr, - ChardevBackend *backend, - bool *be_opened, - Error **errp) -{ - *be_opened = false; -} - -static const CharDriver null_driver = { - .kind = CHARDEV_BACKEND_KIND_NULL, -}; - -static void char_null_class_init(ObjectClass *oc, void *data) -{ - ChardevClass *cc = CHARDEV_CLASS(oc); - - cc->open = null_chr_open; - cc->chr_write = null_chr_write; -} - -static const TypeInfo char_null_type_info = { - .name = TYPE_CHARDEV_NULL, - .parent = TYPE_CHARDEV, - .instance_size = sizeof(Chardev), - .class_init = char_null_class_init, -}; - -/* MUX driver for serial I/O splitting */ -#define MAX_MUX 4 -#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ -#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) -struct MuxChardev { - Chardev parent; - CharBackend *backends[MAX_MUX]; - CharBackend chr; - int focus; - int mux_cnt; - int term_got_escape; - int max_size; - /* Intermediate input buffer allows to catch escape sequences even if the - currently active device is not accepting any input - but only until it - is full as well. */ - unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE]; - int prod[MAX_MUX]; - int cons[MAX_MUX]; - int timestamps; - - /* Protected by the Chardev chr_write_lock. */ - int linestart; - int64_t timestamps_start; -}; - -#define MUX_CHARDEV(obj) OBJECT_CHECK(MuxChardev, (obj), TYPE_CHARDEV_MUX) - -/* Called with chr_write_lock held. */ -static int mux_chr_write(Chardev *chr, const uint8_t *buf, int len) -{ - MuxChardev *d = MUX_CHARDEV(chr); - int ret; - if (!d->timestamps) { - ret = qemu_chr_fe_write(&d->chr, buf, len); - } else { - int i; - - ret = 0; - for (i = 0; i < len; i++) { - if (d->linestart) { - char buf1[64]; - int64_t ti; - int secs; - - ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); - if (d->timestamps_start == -1) - d->timestamps_start = ti; - ti -= d->timestamps_start; - secs = ti / 1000; - snprintf(buf1, sizeof(buf1), - "[%02d:%02d:%02d.%03d] ", - secs / 3600, - (secs / 60) % 60, - secs % 60, - (int)(ti % 1000)); - /* XXX this blocks entire thread. Rewrite to use - * qemu_chr_fe_write and background I/O callbacks */ - qemu_chr_fe_write_all(&d->chr, - (uint8_t *)buf1, strlen(buf1)); - d->linestart = 0; - } - ret += qemu_chr_fe_write(&d->chr, buf + i, 1); - if (buf[i] == '\n') { - d->linestart = 1; - } - } - } - return ret; -} - -static const char * const mux_help[] = { - "% h print this help\n\r", - "% x exit emulator\n\r", - "% s save disk data back to file (if -snapshot)\n\r", - "% t toggle console timestamps\n\r", - "% b send break (magic sysrq)\n\r", - "% c switch between console and monitor\n\r", - "% % sends %\n\r", - NULL -}; - -int term_escape_char = 0x01; /* ctrl-a is used for escape */ -static void mux_print_help(Chardev *chr) -{ - int i, j; - char ebuf[15] = "Escape-Char"; - char cbuf[50] = "\n\r"; - - if (term_escape_char > 0 && term_escape_char < 26) { - snprintf(cbuf, sizeof(cbuf), "\n\r"); - snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a'); - } else { - snprintf(cbuf, sizeof(cbuf), - "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", - term_escape_char); - } - /* XXX this blocks entire thread. Rewrite to use - * qemu_chr_fe_write and background I/O callbacks */ - qemu_chr_write_all(chr, (uint8_t *)cbuf, strlen(cbuf)); - for (i = 0; mux_help[i] != NULL; i++) { - for (j=0; mux_help[i][j] != '\0'; j++) { - if (mux_help[i][j] == '%') - qemu_chr_write_all(chr, (uint8_t *)ebuf, strlen(ebuf)); - else - qemu_chr_write_all(chr, (uint8_t *)&mux_help[i][j], 1); - } - } -} - -static void mux_chr_send_event(MuxChardev *d, int mux_nr, int event) -{ - CharBackend *be = d->backends[mux_nr]; - - if (be && be->chr_event) { - be->chr_event(be->opaque, event); - } -} - -static int mux_proc_byte(Chardev *chr, MuxChardev *d, int ch) -{ - if (d->term_got_escape) { - d->term_got_escape = 0; - if (ch == term_escape_char) - goto send_char; - switch(ch) { - case '?': - case 'h': - mux_print_help(chr); - break; - case 'x': - { - const char *term = "QEMU: Terminated\n\r"; - qemu_chr_write_all(chr, (uint8_t *)term, strlen(term)); - exit(0); - break; - } - case 's': - blk_commit_all(); - break; - case 'b': - qemu_chr_be_event(chr, CHR_EVENT_BREAK); - break; - case 'c': - assert(d->mux_cnt > 0); /* handler registered with first fe */ - /* Switch to the next registered device */ - mux_set_focus(chr, (d->focus + 1) % d->mux_cnt); - break; - case 't': - d->timestamps = !d->timestamps; - d->timestamps_start = -1; - d->linestart = 0; - break; - } - } else if (ch == term_escape_char) { - d->term_got_escape = 1; - } else { - send_char: - return 1; - } - return 0; -} - -static void mux_chr_accept_input(Chardev *chr) -{ - MuxChardev *d = MUX_CHARDEV(chr); - int m = d->focus; - CharBackend *be = d->backends[m]; - - while (be && d->prod[m] != d->cons[m] && - be->chr_can_read && be->chr_can_read(be->opaque)) { - be->chr_read(be->opaque, - &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1); - } -} - -static int mux_chr_can_read(void *opaque) -{ - MuxChardev *d = MUX_CHARDEV(opaque); - int m = d->focus; - CharBackend *be = d->backends[m]; - - if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE) { - return 1; - } - - if (be && be->chr_can_read) { - return be->chr_can_read(be->opaque); - } - - return 0; -} - -static void mux_chr_read(void *opaque, const uint8_t *buf, int size) -{ - Chardev *chr = CHARDEV(opaque); - MuxChardev *d = MUX_CHARDEV(opaque); - int m = d->focus; - CharBackend *be = d->backends[m]; - int i; - - mux_chr_accept_input(opaque); - - for (i = 0; i < size; i++) - if (mux_proc_byte(chr, d, buf[i])) { - if (d->prod[m] == d->cons[m] && - be && be->chr_can_read && - be->chr_can_read(be->opaque)) - be->chr_read(be->opaque, &buf[i], 1); - else - d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i]; - } -} - -static bool muxes_realized; - -static void mux_chr_event(void *opaque, int event) -{ - MuxChardev *d = MUX_CHARDEV(opaque); - int i; - - if (!muxes_realized) { - return; - } - - /* Send the event to all registered listeners */ - for (i = 0; i < d->mux_cnt; i++) - mux_chr_send_event(d, i, event); -} - -/** - * Called after processing of default and command-line-specified - * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached - * to a mux chardev. This is done here to ensure that - * output/prompts/banners are only displayed for the FE that has - * focus when initial command-line processing/machine init is - * completed. - * - * After this point, any new FE attached to any new or existing - * mux will receive CHR_EVENT_OPENED notifications for the BE - * immediately. - */ -static void muxes_realize_done(Notifier *notifier, void *unused) -{ - Chardev *chr; - - QTAILQ_FOREACH(chr, &chardevs, next) { - if (CHARDEV_IS_MUX(chr)) { - MuxChardev *d = MUX_CHARDEV(chr); - int i; - - /* send OPENED to all already-attached FEs */ - for (i = 0; i < d->mux_cnt; i++) { - mux_chr_send_event(d, i, CHR_EVENT_OPENED); - } - /* mark mux as OPENED so any new FEs will immediately receive - * OPENED event - */ - qemu_chr_be_generic_open(chr); - } - } - muxes_realized = true; -} - -static Notifier muxes_realize_notify = { - .notify = muxes_realize_done, -}; - -static GSource *mux_chr_add_watch(Chardev *s, GIOCondition cond) -{ - MuxChardev *d = MUX_CHARDEV(s); - Chardev *chr = qemu_chr_fe_get_driver(&d->chr); - ChardevClass *cc = CHARDEV_GET_CLASS(chr); - - if (!cc->chr_add_watch) { - return NULL; - } - - return cc->chr_add_watch(chr, cond); -} - -static void mux_chr_free(struct Chardev *chr) -{ - MuxChardev *d = MUX_CHARDEV(chr); - int i; - - for (i = 0; i < d->mux_cnt; i++) { - CharBackend *be = d->backends[i]; - if (be) { - be->chr = NULL; - } - } - qemu_chr_fe_deinit(&d->chr); -} - -static void mux_chr_set_handlers(Chardev *chr, GMainContext *context) -{ - MuxChardev *d = MUX_CHARDEV(chr); - - /* Fix up the real driver with mux routines */ - qemu_chr_fe_set_handlers(&d->chr, - mux_chr_can_read, - mux_chr_read, - mux_chr_event, - chr, - context, true); -} - -static void mux_set_focus(Chardev *chr, int focus) -{ - MuxChardev *d = MUX_CHARDEV(chr); - - assert(focus >= 0); - assert(focus < d->mux_cnt); - - if (d->focus != -1) { - mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); - } - - d->focus = focus; - chr->be = d->backends[focus]; - mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); -} - -static void qemu_chr_open_mux(Chardev *chr, - ChardevBackend *backend, - bool *be_opened, - Error **errp) -{ - ChardevMux *mux = backend->u.mux.data; - Chardev *drv; - MuxChardev *d = MUX_CHARDEV(chr); - - drv = qemu_chr_find(mux->chardev); - if (drv == NULL) { - error_setg(errp, "mux: base chardev %s not found", mux->chardev); - return; - } - - d->focus = -1; - /* only default to opened state if we've realized the initial - * set of muxes - */ - *be_opened = muxes_realized; - qemu_chr_fe_init(&d->chr, drv, errp); -} - -Chardev *qemu_chr_fe_get_driver(CharBackend *be) -{ - return be->chr; -} - -bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp) -{ - int tag = 0; - - if (CHARDEV_IS_MUX(s)) { - MuxChardev *d = MUX_CHARDEV(s); - - if (d->mux_cnt >= MAX_MUX) { - goto unavailable; - } - - d->backends[d->mux_cnt] = b; - tag = d->mux_cnt++; - } else if (s->be) { - goto unavailable; - } else { - s->be = b; - } - - b->fe_open = false; - b->tag = tag; - b->chr = s; - return true; - -unavailable: - error_setg(errp, QERR_DEVICE_IN_USE, s->label); - return false; -} - -static bool qemu_chr_is_busy(Chardev *s) -{ - if (CHARDEV_IS_MUX(s)) { - MuxChardev *d = MUX_CHARDEV(s); - return d->mux_cnt >= 0; - } else { - return s->be != NULL; - } -} - -void qemu_chr_fe_deinit(CharBackend *b) -{ - assert(b); - - if (b->chr) { - qemu_chr_fe_set_handlers(b, NULL, NULL, NULL, NULL, NULL, true); - if (b->chr->be == b) { - b->chr->be = NULL; - } - if (CHARDEV_IS_MUX(b->chr)) { - MuxChardev *d = MUX_CHARDEV(b->chr); - d->backends[b->tag] = NULL; - } - b->chr = NULL; - } -} - -void qemu_chr_fe_set_handlers(CharBackend *b, - IOCanReadHandler *fd_can_read, - IOReadHandler *fd_read, - IOEventHandler *fd_event, - void *opaque, - GMainContext *context, - bool set_open) -{ - Chardev *s; - ChardevClass *cc; - int fe_open; - - s = b->chr; - if (!s) { - return; - } - - cc = CHARDEV_GET_CLASS(s); - if (!opaque && !fd_can_read && !fd_read && !fd_event) { - fe_open = 0; - remove_fd_in_watch(s); - } else { - fe_open = 1; - } - b->chr_can_read = fd_can_read; - b->chr_read = fd_read; - b->chr_event = fd_event; - b->opaque = opaque; - if (cc->chr_update_read_handler) { - cc->chr_update_read_handler(s, context); - } - - if (set_open) { - qemu_chr_fe_set_open(b, fe_open); - } - - if (fe_open) { - qemu_chr_fe_take_focus(b); - /* We're connecting to an already opened device, so let's make sure we - also get the open event */ - if (s->be_open) { - qemu_chr_be_generic_open(s); - } - } - - if (CHARDEV_IS_MUX(s)) { - mux_chr_set_handlers(s, context); - } -} - -void qemu_chr_fe_take_focus(CharBackend *b) -{ - if (!b->chr) { - return; - } - - if (CHARDEV_IS_MUX(b->chr)) { - mux_set_focus(b->chr, b->tag); - } -} - -typedef struct IOWatchPoll -{ - GSource parent; - - QIOChannel *ioc; - GSource *src; - - IOCanReadHandler *fd_can_read; - GSourceFunc fd_read; - void *opaque; - GMainContext *context; -} IOWatchPoll; - -static IOWatchPoll *io_watch_poll_from_source(GSource *source) -{ - return container_of(source, IOWatchPoll, parent); -} - -static gboolean io_watch_poll_prepare(GSource *source, - gint *timeout_) -{ - IOWatchPoll *iwp = io_watch_poll_from_source(source); - bool now_active = iwp->fd_can_read(iwp->opaque) > 0; - bool was_active = iwp->src != NULL; - if (was_active == now_active) { - return FALSE; - } - - if (now_active) { - iwp->src = qio_channel_create_watch( - iwp->ioc, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL); - g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL); - g_source_attach(iwp->src, iwp->context); - } else { - g_source_destroy(iwp->src); - g_source_unref(iwp->src); - iwp->src = NULL; - } - return FALSE; -} - -static gboolean io_watch_poll_check(GSource *source) -{ - return FALSE; -} - -static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback, - gpointer user_data) -{ - abort(); -} - -static void io_watch_poll_finalize(GSource *source) -{ - /* Due to a glib bug, removing the last reference to a source - * inside a finalize callback causes recursive locking (and a - * deadlock). This is not a problem inside other callbacks, - * including dispatch callbacks, so we call io_remove_watch_poll - * to remove this source. At this point, iwp->src must - * be NULL, or we would leak it. - * - * This would be solved much more elegantly by child sources, - * but we support older glib versions that do not have them. - */ - IOWatchPoll *iwp = io_watch_poll_from_source(source); - assert(iwp->src == NULL); -} - -static GSourceFuncs io_watch_poll_funcs = { - .prepare = io_watch_poll_prepare, - .check = io_watch_poll_check, - .dispatch = io_watch_poll_dispatch, - .finalize = io_watch_poll_finalize, -}; - -/* Can only be used for read */ -static guint io_add_watch_poll(Chardev *chr, - QIOChannel *ioc, - IOCanReadHandler *fd_can_read, - QIOChannelFunc fd_read, - gpointer user_data, - GMainContext *context) -{ - IOWatchPoll *iwp; - int tag; - char *name; - - iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, - sizeof(IOWatchPoll)); - iwp->fd_can_read = fd_can_read; - iwp->opaque = user_data; - iwp->ioc = ioc; - iwp->fd_read = (GSourceFunc) fd_read; - iwp->src = NULL; - iwp->context = context; - - name = g_strdup_printf("chardev-iowatch-%s", chr->label); - g_source_set_name((GSource *)iwp, name); - g_free(name); - - tag = g_source_attach(&iwp->parent, context); - g_source_unref(&iwp->parent); - return tag; -} - -static void io_remove_watch_poll(guint tag) -{ - GSource *source; - IOWatchPoll *iwp; - - g_return_if_fail (tag > 0); - - source = g_main_context_find_source_by_id(NULL, tag); - g_return_if_fail (source != NULL); - - iwp = io_watch_poll_from_source(source); - if (iwp->src) { - g_source_destroy(iwp->src); - g_source_unref(iwp->src); - iwp->src = NULL; - } - g_source_destroy(&iwp->parent); -} - -static void remove_fd_in_watch(Chardev *chr) -{ - if (chr->fd_in_tag) { - io_remove_watch_poll(chr->fd_in_tag); - chr->fd_in_tag = 0; - } -} - - -static int io_channel_send_full(QIOChannel *ioc, - const void *buf, size_t len, - int *fds, size_t nfds) -{ - size_t offset = 0; - - while (offset < len) { - ssize_t ret = 0; - struct iovec iov = { .iov_base = (char *)buf + offset, - .iov_len = len - offset }; - - ret = qio_channel_writev_full( - ioc, &iov, 1, - fds, nfds, NULL); - if (ret == QIO_CHANNEL_ERR_BLOCK) { - if (offset) { - return offset; - } - - errno = EAGAIN; - return -1; - } else if (ret < 0) { - errno = EINVAL; - return -1; - } - - offset += ret; - } - - return offset; -} - - -#ifndef _WIN32 -static int io_channel_send(QIOChannel *ioc, const void *buf, size_t len) -{ - return io_channel_send_full(ioc, buf, len, NULL, 0); -} - -typedef struct FDChardev { - Chardev parent; - Chardev *chr; - QIOChannel *ioc_in, *ioc_out; - int max_size; -} FDChardev; - -#define TYPE_CHARDEV_FD "chardev-fd" -#define FD_CHARDEV(obj) OBJECT_CHECK(FDChardev, (obj), TYPE_CHARDEV_FD) - -/* Called with chr_write_lock held. */ -static int fd_chr_write(Chardev *chr, const uint8_t *buf, int len) -{ - FDChardev *s = FD_CHARDEV(chr); - - return io_channel_send(s->ioc_out, buf, len); -} - -static gboolean fd_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque) -{ - Chardev *chr = CHARDEV(opaque); - FDChardev *s = FD_CHARDEV(opaque); - int len; - uint8_t buf[READ_BUF_LEN]; - ssize_t ret; - - len = sizeof(buf); - if (len > s->max_size) { - len = s->max_size; - } - if (len == 0) { - return TRUE; - } - - ret = qio_channel_read( - chan, (gchar *)buf, len, NULL); - if (ret == 0) { - remove_fd_in_watch(chr); - qemu_chr_be_event(chr, CHR_EVENT_CLOSED); - return FALSE; - } - if (ret > 0) { - qemu_chr_be_write(chr, buf, ret); - } - - return TRUE; -} - -static int fd_chr_read_poll(void *opaque) -{ - Chardev *chr = CHARDEV(opaque); - FDChardev *s = FD_CHARDEV(opaque); - - s->max_size = qemu_chr_be_can_write(chr); - return s->max_size; -} - -static GSource *fd_chr_add_watch(Chardev *chr, GIOCondition cond) -{ - FDChardev *s = FD_CHARDEV(chr); - return qio_channel_create_watch(s->ioc_out, cond); -} - -static void fd_chr_update_read_handler(Chardev *chr, - GMainContext *context) -{ - FDChardev *s = FD_CHARDEV(chr); - - remove_fd_in_watch(chr); - if (s->ioc_in) { - chr->fd_in_tag = io_add_watch_poll(chr, s->ioc_in, - fd_chr_read_poll, - fd_chr_read, chr, - context); - } -} - -static void fd_chr_free(struct Chardev *chr) -{ - FDChardev *s = FD_CHARDEV(chr); - - remove_fd_in_watch(chr); - if (s->ioc_in) { - object_unref(OBJECT(s->ioc_in)); - } - if (s->ioc_out) { - object_unref(OBJECT(s->ioc_out)); - } - - qemu_chr_be_event(chr, CHR_EVENT_CLOSED); -} - -/* open a character device to a unix fd */ -static void qemu_chr_open_fd(Chardev *chr, - int fd_in, int fd_out) -{ - FDChardev *s = FD_CHARDEV(chr); - char *name; - - s->ioc_in = QIO_CHANNEL(qio_channel_file_new_fd(fd_in)); - name = g_strdup_printf("chardev-file-in-%s", chr->label); - qio_channel_set_name(QIO_CHANNEL(s->ioc_in), name); - g_free(name); - s->ioc_out = QIO_CHANNEL(qio_channel_file_new_fd(fd_out)); - name = g_strdup_printf("chardev-file-out-%s", chr->label); - qio_channel_set_name(QIO_CHANNEL(s->ioc_out), name); - g_free(name); - qemu_set_nonblock(fd_out); - s->chr = chr; -} - -static void char_fd_class_init(ObjectClass *oc, void *data) -{ - ChardevClass *cc = CHARDEV_CLASS(oc); - - cc->chr_add_watch = fd_chr_add_watch; - cc->chr_write = fd_chr_write; - cc->chr_update_read_handler = fd_chr_update_read_handler; - cc->chr_free = fd_chr_free; -} - -static const TypeInfo char_fd_type_info = { - .name = TYPE_CHARDEV_FD, - .parent = TYPE_CHARDEV, - .instance_size = sizeof(FDChardev), - .class_init = char_fd_class_init, - .abstract = true, -}; - -static void qemu_chr_open_pipe(Chardev *chr, - ChardevBackend *backend, - bool *be_opened, - Error **errp) -{ - ChardevHostdev *opts = backend->u.pipe.data; - int fd_in, fd_out; - char *filename_in; - char *filename_out; - const char *filename = opts->device; - - filename_in = g_strdup_printf("%s.in", filename); - filename_out = g_strdup_printf("%s.out", filename); - TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY)); - TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY)); - g_free(filename_in); - g_free(filename_out); - if (fd_in < 0 || fd_out < 0) { - if (fd_in >= 0) - close(fd_in); - if (fd_out >= 0) - close(fd_out); - TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY)); - if (fd_in < 0) { - error_setg_file_open(errp, errno, filename); - return; - } - } - qemu_chr_open_fd(chr, fd_in, fd_out); -} - -/* init terminal so that we can grab keys */ -static struct termios oldtty; -static int old_fd0_flags; -static bool stdio_in_use; -static bool stdio_allow_signal; -static bool stdio_echo_state; - -static void qemu_chr_set_echo_stdio(Chardev *chr, bool echo); - -static void term_exit(void) -{ - tcsetattr (0, TCSANOW, &oldtty); - fcntl(0, F_SETFL, old_fd0_flags); -} - -static void term_stdio_handler(int sig) -{ - /* restore echo after resume from suspend. */ - qemu_chr_set_echo_stdio(NULL, stdio_echo_state); -} - -static void qemu_chr_set_echo_stdio(Chardev *chr, bool echo) -{ - struct termios tty; - - stdio_echo_state = echo; - tty = oldtty; - if (!echo) { - tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP - |INLCR|IGNCR|ICRNL|IXON); - tty.c_oflag |= OPOST; - tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); - tty.c_cflag &= ~(CSIZE|PARENB); - tty.c_cflag |= CS8; - tty.c_cc[VMIN] = 1; - tty.c_cc[VTIME] = 0; - } - if (!stdio_allow_signal) - tty.c_lflag &= ~ISIG; - - tcsetattr (0, TCSANOW, &tty); -} - -static void qemu_chr_free_stdio(struct Chardev *chr) -{ - term_exit(); - fd_chr_free(chr); -} - -static void qemu_chr_open_stdio(Chardev *chr, - ChardevBackend *backend, - bool *be_opened, - Error **errp) -{ - ChardevStdio *opts = backend->u.stdio.data; - struct sigaction act; - - if (is_daemonized()) { - error_setg(errp, "cannot use stdio with -daemonize"); - return; - } - - if (stdio_in_use) { - error_setg(errp, "cannot use stdio by multiple character devices"); - return; - } - - stdio_in_use = true; - old_fd0_flags = fcntl(0, F_GETFL); - tcgetattr(0, &oldtty); - qemu_set_nonblock(0); - atexit(term_exit); - - memset(&act, 0, sizeof(act)); - act.sa_handler = term_stdio_handler; - sigaction(SIGCONT, &act, NULL); - - qemu_chr_open_fd(chr, 0, 1); - - if (opts->has_signal) { - stdio_allow_signal = opts->signal; - } - qemu_chr_set_echo_stdio(chr, false); -} - -#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ - || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \ - || defined(__GLIBC__) - -#define HAVE_CHARDEV_SERIAL 1 -#define HAVE_CHARDEV_PTY 1 - -typedef struct { - Chardev parent; - QIOChannel *ioc; - int read_bytes; - - /* Protected by the Chardev chr_write_lock. */ - int connected; - guint timer_tag; - guint open_tag; -} PtyChardev; - -#define PTY_CHARDEV(obj) OBJECT_CHECK(PtyChardev, (obj), TYPE_CHARDEV_PTY) - -static void pty_chr_update_read_handler_locked(Chardev *chr); -static void pty_chr_state(Chardev *chr, int connected); - -static gboolean pty_chr_timer(gpointer opaque) -{ - struct Chardev *chr = CHARDEV(opaque); - PtyChardev *s = PTY_CHARDEV(opaque); - - qemu_mutex_lock(&chr->chr_write_lock); - s->timer_tag = 0; - s->open_tag = 0; - if (!s->connected) { - /* Next poll ... */ - pty_chr_update_read_handler_locked(chr); - } - qemu_mutex_unlock(&chr->chr_write_lock); - return FALSE; -} - -/* Called with chr_write_lock held. */ -static void pty_chr_rearm_timer(Chardev *chr, int ms) -{ - PtyChardev *s = PTY_CHARDEV(chr); - char *name; - - if (s->timer_tag) { - g_source_remove(s->timer_tag); - s->timer_tag = 0; - } - - if (ms == 1000) { - name = g_strdup_printf("pty-timer-secs-%s", chr->label); - s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr); - } else { - name = g_strdup_printf("pty-timer-ms-%s", chr->label); - s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr); - } - g_source_set_name_by_id(s->timer_tag, name); - g_free(name); -} - -/* Called with chr_write_lock held. */ -static void pty_chr_update_read_handler_locked(Chardev *chr) -{ - PtyChardev *s = PTY_CHARDEV(chr); - GPollFD pfd; - int rc; - QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc); - - pfd.fd = fioc->fd; - pfd.events = G_IO_OUT; - pfd.revents = 0; - do { - rc = g_poll(&pfd, 1, 0); - } while (rc == -1 && errno == EINTR); - assert(rc >= 0); - - if (pfd.revents & G_IO_HUP) { - pty_chr_state(chr, 0); - } else { - pty_chr_state(chr, 1); - } -} - -static void pty_chr_update_read_handler(Chardev *chr, - GMainContext *context) -{ - qemu_mutex_lock(&chr->chr_write_lock); - pty_chr_update_read_handler_locked(chr); - qemu_mutex_unlock(&chr->chr_write_lock); -} - -/* Called with chr_write_lock held. */ -static int char_pty_chr_write(Chardev *chr, const uint8_t *buf, int len) -{ - PtyChardev *s = PTY_CHARDEV(chr); - - if (!s->connected) { - /* guest sends data, check for (re-)connect */ - pty_chr_update_read_handler_locked(chr); - if (!s->connected) { - return 0; - } - } - return io_channel_send(s->ioc, buf, len); -} - -static GSource *pty_chr_add_watch(Chardev *chr, GIOCondition cond) -{ - PtyChardev *s = PTY_CHARDEV(chr); - if (!s->connected) { - return NULL; - } - return qio_channel_create_watch(s->ioc, cond); -} - -static int pty_chr_read_poll(void *opaque) -{ - Chardev *chr = CHARDEV(opaque); - PtyChardev *s = PTY_CHARDEV(opaque); - - s->read_bytes = qemu_chr_be_can_write(chr); - return s->read_bytes; -} - -static gboolean pty_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque) -{ - Chardev *chr = CHARDEV(opaque); - PtyChardev *s = PTY_CHARDEV(opaque); - gsize len; - uint8_t buf[READ_BUF_LEN]; - ssize_t ret; - - len = sizeof(buf); - if (len > s->read_bytes) - len = s->read_bytes; - if (len == 0) { - return TRUE; - } - ret = qio_channel_read(s->ioc, (char *)buf, len, NULL); - if (ret <= 0) { - pty_chr_state(chr, 0); - return FALSE; - } else { - pty_chr_state(chr, 1); - qemu_chr_be_write(chr, buf, ret); - } - return TRUE; -} - -static gboolean qemu_chr_be_generic_open_func(gpointer opaque) -{ - Chardev *chr = CHARDEV(opaque); - PtyChardev *s = PTY_CHARDEV(opaque); - - s->open_tag = 0; - qemu_chr_be_generic_open(chr); - return FALSE; -} - -/* Called with chr_write_lock held. */ -static void pty_chr_state(Chardev *chr, int connected) -{ - PtyChardev *s = PTY_CHARDEV(chr); - - if (!connected) { - if (s->open_tag) { - g_source_remove(s->open_tag); - s->open_tag = 0; - } - remove_fd_in_watch(chr); - s->connected = 0; - /* (re-)connect poll interval for idle guests: once per second. - * We check more frequently in case the guests sends data to - * the virtual device linked to our pty. */ - pty_chr_rearm_timer(chr, 1000); - } else { - if (s->timer_tag) { - g_source_remove(s->timer_tag); - s->timer_tag = 0; - } - if (!s->connected) { - g_assert(s->open_tag == 0); - s->connected = 1; - s->open_tag = g_idle_add(qemu_chr_be_generic_open_func, chr); - } - if (!chr->fd_in_tag) { - chr->fd_in_tag = io_add_watch_poll(chr, s->ioc, - pty_chr_read_poll, - pty_chr_read, - chr, NULL); - } - } -} - -static void pty_chr_free(struct Chardev *chr) -{ - PtyChardev *s = PTY_CHARDEV(chr); - - qemu_mutex_lock(&chr->chr_write_lock); - pty_chr_state(chr, 0); - object_unref(OBJECT(s->ioc)); - if (s->timer_tag) { - g_source_remove(s->timer_tag); - s->timer_tag = 0; - } - qemu_mutex_unlock(&chr->chr_write_lock); - qemu_chr_be_event(chr, CHR_EVENT_CLOSED); -} - -static void char_pty_open(Chardev *chr, - ChardevBackend *backend, - bool *be_opened, - Error **errp) -{ - PtyChardev *s; - int master_fd, slave_fd; - char pty_name[PATH_MAX]; - char *name; - - master_fd = qemu_openpty_raw(&slave_fd, pty_name); - if (master_fd < 0) { - error_setg_errno(errp, errno, "Failed to create PTY"); - return; - } - - close(slave_fd); - qemu_set_nonblock(master_fd); - - chr->filename = g_strdup_printf("pty:%s", pty_name); - error_report("char device redirected to %s (label %s)", - pty_name, chr->label); - - s = PTY_CHARDEV(chr); - s->ioc = QIO_CHANNEL(qio_channel_file_new_fd(master_fd)); - name = g_strdup_printf("chardev-pty-%s", chr->label); - qio_channel_set_name(QIO_CHANNEL(s->ioc), name); - g_free(name); - s->timer_tag = 0; - *be_opened = false; -} - -static const CharDriver pty_driver = { - .kind = CHARDEV_BACKEND_KIND_PTY, -}; - -static void char_pty_class_init(ObjectClass *oc, void *data) -{ - ChardevClass *cc = CHARDEV_CLASS(oc); - - cc->open = char_pty_open; - cc->chr_write = char_pty_chr_write; - cc->chr_update_read_handler = pty_chr_update_read_handler; - cc->chr_add_watch = pty_chr_add_watch; - cc->chr_free = pty_chr_free; -} - -static const TypeInfo char_pty_type_info = { - .name = TYPE_CHARDEV_PTY, - .parent = TYPE_CHARDEV, - .instance_size = sizeof(PtyChardev), - .class_init = char_pty_class_init, -}; - -static void tty_serial_init(int fd, int speed, - int parity, int data_bits, int stop_bits) -{ - struct termios tty; - speed_t spd; - -#if 0 - printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n", - speed, parity, data_bits, stop_bits); -#endif - tcgetattr (fd, &tty); - -#define check_speed(val) if (speed <= val) { spd = B##val; break; } - speed = speed * 10 / 11; - do { - check_speed(50); - check_speed(75); - check_speed(110); - check_speed(134); - check_speed(150); - check_speed(200); - check_speed(300); - check_speed(600); - check_speed(1200); - check_speed(1800); - check_speed(2400); - check_speed(4800); - check_speed(9600); - check_speed(19200); - check_speed(38400); - /* Non-Posix values follow. They may be unsupported on some systems. */ - check_speed(57600); - check_speed(115200); -#ifdef B230400 - check_speed(230400); -#endif -#ifdef B460800 - check_speed(460800); -#endif -#ifdef B500000 - check_speed(500000); -#endif -#ifdef B576000 - check_speed(576000); -#endif -#ifdef B921600 - check_speed(921600); -#endif -#ifdef B1000000 - check_speed(1000000); -#endif -#ifdef B1152000 - check_speed(1152000); -#endif -#ifdef B1500000 - check_speed(1500000); -#endif -#ifdef B2000000 - check_speed(2000000); -#endif -#ifdef B2500000 - check_speed(2500000); -#endif -#ifdef B3000000 - check_speed(3000000); -#endif -#ifdef B3500000 - check_speed(3500000); -#endif -#ifdef B4000000 - check_speed(4000000); -#endif - spd = B115200; - } while (0); - - cfsetispeed(&tty, spd); - cfsetospeed(&tty, spd); - - tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP - |INLCR|IGNCR|ICRNL|IXON); - tty.c_oflag |= OPOST; - tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); - tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); - switch(data_bits) { - default: - case 8: - tty.c_cflag |= CS8; - break; - case 7: - tty.c_cflag |= CS7; - break; - case 6: - tty.c_cflag |= CS6; - break; - case 5: - tty.c_cflag |= CS5; - break; - } - switch(parity) { - default: - case 'N': - break; - case 'E': - tty.c_cflag |= PARENB; - break; - case 'O': - tty.c_cflag |= PARENB | PARODD; - break; - } - if (stop_bits == 2) - tty.c_cflag |= CSTOPB; - - tcsetattr (fd, TCSANOW, &tty); -} - -static int tty_serial_ioctl(Chardev *chr, int cmd, void *arg) -{ - FDChardev *s = FD_CHARDEV(chr); - QIOChannelFile *fioc = QIO_CHANNEL_FILE(s->ioc_in); - - switch(cmd) { - case CHR_IOCTL_SERIAL_SET_PARAMS: - { - QEMUSerialSetParams *ssp = arg; - tty_serial_init(fioc->fd, - ssp->speed, ssp->parity, - ssp->data_bits, ssp->stop_bits); - } - break; - case CHR_IOCTL_SERIAL_SET_BREAK: - { - int enable = *(int *)arg; - if (enable) { - tcsendbreak(fioc->fd, 1); - } - } - break; - case CHR_IOCTL_SERIAL_GET_TIOCM: - { - int sarg = 0; - int *targ = (int *)arg; - ioctl(fioc->fd, TIOCMGET, &sarg); - *targ = 0; - if (sarg & TIOCM_CTS) - *targ |= CHR_TIOCM_CTS; - if (sarg & TIOCM_CAR) - *targ |= CHR_TIOCM_CAR; - if (sarg & TIOCM_DSR) - *targ |= CHR_TIOCM_DSR; - if (sarg & TIOCM_RI) - *targ |= CHR_TIOCM_RI; - if (sarg & TIOCM_DTR) - *targ |= CHR_TIOCM_DTR; - if (sarg & TIOCM_RTS) - *targ |= CHR_TIOCM_RTS; - } - break; - case CHR_IOCTL_SERIAL_SET_TIOCM: - { - int sarg = *(int *)arg; - int targ = 0; - ioctl(fioc->fd, TIOCMGET, &targ); - targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR - | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS); - if (sarg & CHR_TIOCM_CTS) - targ |= TIOCM_CTS; - if (sarg & CHR_TIOCM_CAR) - targ |= TIOCM_CAR; - if (sarg & CHR_TIOCM_DSR) - targ |= TIOCM_DSR; - if (sarg & CHR_TIOCM_RI) - targ |= TIOCM_RI; - if (sarg & CHR_TIOCM_DTR) - targ |= TIOCM_DTR; - if (sarg & CHR_TIOCM_RTS) - targ |= TIOCM_RTS; - ioctl(fioc->fd, TIOCMSET, &targ); - } - break; - default: - return -ENOTSUP; - } - return 0; -} - -static void qemu_chr_free_tty(Chardev *chr) -{ - fd_chr_free(chr); -} -#endif /* __linux__ || __sun__ */ - -#if defined(__linux__) - -#define HAVE_CHARDEV_PARPORT 1 - -typedef struct { - Chardev parent; - int fd; - int mode; -} ParallelChardev; - -#define PARALLEL_CHARDEV(obj) \ - OBJECT_CHECK(ParallelChardev, (obj), TYPE_CHARDEV_PARALLEL) - -static int pp_hw_mode(ParallelChardev *s, uint16_t mode) -{ - if (s->mode != mode) { - int m = mode; - if (ioctl(s->fd, PPSETMODE, &m) < 0) - return 0; - s->mode = mode; - } - return 1; -} - -static int pp_ioctl(Chardev *chr, int cmd, void *arg) -{ - ParallelChardev *drv = PARALLEL_CHARDEV(chr); - int fd = drv->fd; - uint8_t b; - - switch(cmd) { - case CHR_IOCTL_PP_READ_DATA: - if (ioctl(fd, PPRDATA, &b) < 0) - return -ENOTSUP; - *(uint8_t *)arg = b; - break; - case CHR_IOCTL_PP_WRITE_DATA: - b = *(uint8_t *)arg; - if (ioctl(fd, PPWDATA, &b) < 0) - return -ENOTSUP; - break; - case CHR_IOCTL_PP_READ_CONTROL: - if (ioctl(fd, PPRCONTROL, &b) < 0) - return -ENOTSUP; - /* Linux gives only the lowest bits, and no way to know data - direction! For better compatibility set the fixed upper - bits. */ - *(uint8_t *)arg = b | 0xc0; - break; - case CHR_IOCTL_PP_WRITE_CONTROL: - b = *(uint8_t *)arg; - if (ioctl(fd, PPWCONTROL, &b) < 0) - return -ENOTSUP; - break; - case CHR_IOCTL_PP_READ_STATUS: - if (ioctl(fd, PPRSTATUS, &b) < 0) - return -ENOTSUP; - *(uint8_t *)arg = b; - break; - case CHR_IOCTL_PP_DATA_DIR: - if (ioctl(fd, PPDATADIR, (int *)arg) < 0) - return -ENOTSUP; - break; - case CHR_IOCTL_PP_EPP_READ_ADDR: - if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { - struct ParallelIOArg *parg = arg; - int n = read(fd, parg->buffer, parg->count); - if (n != parg->count) { - return -EIO; - } - } - break; - case CHR_IOCTL_PP_EPP_READ: - if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { - struct ParallelIOArg *parg = arg; - int n = read(fd, parg->buffer, parg->count); - if (n != parg->count) { - return -EIO; - } - } - break; - case CHR_IOCTL_PP_EPP_WRITE_ADDR: - if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { - struct ParallelIOArg *parg = arg; - int n = write(fd, parg->buffer, parg->count); - if (n != parg->count) { - return -EIO; - } - } - break; - case CHR_IOCTL_PP_EPP_WRITE: - if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { - struct ParallelIOArg *parg = arg; - int n = write(fd, parg->buffer, parg->count); - if (n != parg->count) { - return -EIO; - } - } - break; - default: - return -ENOTSUP; - } - return 0; -} - -static void pp_free(Chardev *chr) -{ - ParallelChardev *drv = PARALLEL_CHARDEV(chr); - int fd = drv->fd; - - pp_hw_mode(drv, IEEE1284_MODE_COMPAT); - ioctl(fd, PPRELEASE); - close(fd); - qemu_chr_be_event(chr, CHR_EVENT_CLOSED); -} - -static void qemu_chr_open_pp_fd(Chardev *chr, - int fd, - bool *be_opened, - Error **errp) -{ - ParallelChardev *drv = PARALLEL_CHARDEV(chr); - - if (ioctl(fd, PPCLAIM) < 0) { - error_setg_errno(errp, errno, "not a parallel port"); - close(fd); - return; - } - - drv->fd = fd; - drv->mode = IEEE1284_MODE_COMPAT; -} -#endif /* __linux__ */ - -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) - -#define HAVE_CHARDEV_PARPORT 1 - -typedef struct { - Chardev parent; - int fd; -} ParallelChardev; - -#define PARALLEL_CHARDEV(obj) \ - OBJECT_CHECK(ParallelChardev, (obj), TYPE_CHARDEV_PARALLEL) - -static int pp_ioctl(Chardev *chr, int cmd, void *arg) -{ - ParallelChardev *drv = PARALLEL_CHARDEV(chr); - uint8_t b; - - switch (cmd) { - case CHR_IOCTL_PP_READ_DATA: - if (ioctl(drv->fd, PPIGDATA, &b) < 0) { - return -ENOTSUP; - } - *(uint8_t *)arg = b; - break; - case CHR_IOCTL_PP_WRITE_DATA: - b = *(uint8_t *)arg; - if (ioctl(drv->fd, PPISDATA, &b) < 0) { - return -ENOTSUP; - } - break; - case CHR_IOCTL_PP_READ_CONTROL: - if (ioctl(drv->fd, PPIGCTRL, &b) < 0) { - return -ENOTSUP; - } - *(uint8_t *)arg = b; - break; - case CHR_IOCTL_PP_WRITE_CONTROL: - b = *(uint8_t *)arg; - if (ioctl(drv->fd, PPISCTRL, &b) < 0) { - return -ENOTSUP; - } - break; - case CHR_IOCTL_PP_READ_STATUS: - if (ioctl(drv->fd, PPIGSTATUS, &b) < 0) { - return -ENOTSUP; - } - *(uint8_t *)arg = b; - break; - default: - return -ENOTSUP; - } - return 0; -} - -static void qemu_chr_open_pp_fd(Chardev *chr, - int fd, - bool *be_opened, - Error **errp) -{ - ParallelChardev *drv = PARALLEL_CHARDEV(chr); - drv->fd = fd; - *be_opened = false; -} -#endif - -#else /* _WIN32 */ - -#define HAVE_CHARDEV_SERIAL 1 - -typedef struct { - Chardev parent; - int max_size; - HANDLE hcom, hrecv, hsend; - OVERLAPPED orecv; - BOOL fpipe; - DWORD len; - - /* Protected by the Chardev chr_write_lock. */ - OVERLAPPED osend; -} WinChardev; - -#define TYPE_CHARDEV_WIN "chardev-win" -#define WIN_CHARDEV(obj) OBJECT_CHECK(WinChardev, (obj), TYPE_CHARDEV_WIN) - -typedef struct { - Chardev parent; - HANDLE hStdIn; - HANDLE hInputReadyEvent; - HANDLE hInputDoneEvent; - HANDLE hInputThread; - uint8_t win_stdio_buf; -} WinStdioChardev; - -#define TYPE_CHARDEV_WIN_STDIO "chardev-win-stdio" -#define WIN_STDIO_CHARDEV(obj) \ - OBJECT_CHECK(WinStdioChardev, (obj), TYPE_CHARDEV_WIN_STDIO) - -#define NSENDBUF 2048 -#define NRECVBUF 2048 -#define MAXCONNECT 1 -#define NTIMEOUT 5000 - -static int win_chr_poll(void *opaque); -static int win_chr_pipe_poll(void *opaque); - -static void win_chr_free(Chardev *chr) -{ - WinChardev *s = WIN_CHARDEV(chr); - - if (s->hsend) { - CloseHandle(s->hsend); - s->hsend = NULL; - } - if (s->hrecv) { - CloseHandle(s->hrecv); - s->hrecv = NULL; - } - if (s->hcom) { - CloseHandle(s->hcom); - s->hcom = NULL; - } - if (s->fpipe) - qemu_del_polling_cb(win_chr_pipe_poll, chr); - else - qemu_del_polling_cb(win_chr_poll, chr); - - qemu_chr_be_event(chr, CHR_EVENT_CLOSED); -} - -static int win_chr_init(Chardev *chr, const char *filename, Error **errp) -{ - 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; - } - - s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, - OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); - if (s->hcom == INVALID_HANDLE_VALUE) { - error_setg(errp, "Failed CreateFile (%lu)", GetLastError()); - s->hcom = NULL; - goto fail; - } - - if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) { - 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); - - if (!SetCommState(s->hcom, &comcfg.dcb)) { - error_setg(errp, "Failed SetCommState"); - goto fail; - } - - if (!SetCommMask(s->hcom, EV_ERR)) { - error_setg(errp, "Failed SetCommMask"); - goto fail; - } - - cto.ReadIntervalTimeout = MAXDWORD; - if (!SetCommTimeouts(s->hcom, &cto)) { - error_setg(errp, "Failed SetCommTimeouts"); - goto fail; - } - - if (!ClearCommError(s->hcom, &err, &comstat)) { - error_setg(errp, "Failed ClearCommError"); - goto fail; - } - qemu_add_polling_cb(win_chr_poll, chr); - return 0; - - fail: - win_chr_free(chr); - return -1; -} - -/* 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) - ret = WriteFile(s->hcom, buf, len, &size, &s->osend); - else - ret = WriteFile(s->hcom, buf, len, &size, NULL); - if (!ret) { - err = GetLastError(); - if (err == ERROR_IO_PENDING) { - ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); - if (ret) { - buf += size; - len -= size; - } else { - break; - } - } else { - break; - } - } else { - buf += size; - len -= size; - } - } - return len1 - len; -} - -static int win_chr_read_poll(Chardev *chr) -{ - WinChardev *s = WIN_CHARDEV(chr); - - s->max_size = qemu_chr_be_can_write(chr); - return s->max_size; -} - -static void win_chr_readfile(Chardev *chr) -{ - WinChardev *s = WIN_CHARDEV(chr); - - int ret, err; - uint8_t buf[READ_BUF_LEN]; - DWORD size; - - ZeroMemory(&s->orecv, sizeof(s->orecv)); - s->orecv.hEvent = s->hrecv; - ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); - if (!ret) { - err = GetLastError(); - if (err == ERROR_IO_PENDING) { - ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); - } - } - - if (size > 0) { - qemu_chr_be_write(chr, buf, size); - } -} - -static void win_chr_read(Chardev *chr) -{ - WinChardev *s = WIN_CHARDEV(chr); - - if (s->len > s->max_size) - s->len = s->max_size; - if (s->len == 0) - return; - - win_chr_readfile(chr); -} - -static int win_chr_poll(void *opaque) -{ - Chardev *chr = CHARDEV(opaque); - WinChardev *s = WIN_CHARDEV(opaque); - COMSTAT status; - DWORD comerr; - - ClearCommError(s->hcom, &comerr, &status); - if (status.cbInQue > 0) { - s->len = status.cbInQue; - win_chr_read_poll(chr); - win_chr_read(chr); - return 1; - } - return 0; -} - -static int win_chr_pipe_poll(void *opaque) -{ - Chardev *chr = CHARDEV(opaque); - WinChardev *s = WIN_CHARDEV(opaque); - DWORD size; - - PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); - if (size > 0) { - s->len = size; - win_chr_read_poll(chr); - win_chr_read(chr); - return 1; - } - return 0; -} - -static int win_chr_pipe_init(Chardev *chr, const char *filename, - Error **errp) -{ - WinChardev *s = WIN_CHARDEV(chr); - OVERLAPPED ov; - int ret; - DWORD size; - char *openname; - - s->fpipe = TRUE; - - 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; - } - - openname = g_strdup_printf("\\\\.\\pipe\\%s", filename); - s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, - PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | - PIPE_WAIT, - MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL); - g_free(openname); - if (s->hcom == INVALID_HANDLE_VALUE) { - error_setg(errp, "Failed CreateNamedPipe (%lu)", GetLastError()); - s->hcom = NULL; - goto fail; - } - - ZeroMemory(&ov, sizeof(ov)); - ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); - ret = ConnectNamedPipe(s->hcom, &ov); - if (ret) { - error_setg(errp, "Failed ConnectNamedPipe"); - goto fail; - } - - ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); - if (!ret) { - error_setg(errp, "Failed GetOverlappedResult"); - if (ov.hEvent) { - CloseHandle(ov.hEvent); - ov.hEvent = NULL; - } - goto fail; - } - - if (ov.hEvent) { - CloseHandle(ov.hEvent); - ov.hEvent = NULL; - } - qemu_add_polling_cb(win_chr_pipe_poll, chr); - return 0; - - fail: - win_chr_free(chr); - return -1; -} - - -static void qemu_chr_open_pipe(Chardev *chr, - ChardevBackend *backend, - bool *be_opened, - Error **errp) -{ - ChardevHostdev *opts = backend->u.pipe.data; - const char *filename = opts->device; - - if (win_chr_pipe_init(chr, filename, errp) < 0) { - return; - } -} - -static void qemu_chr_open_win_file(Chardev *chr, HANDLE fd_out) -{ - WinChardev *s = WIN_CHARDEV(chr); - - s->hcom = fd_out; -} - -static void char_win_class_init(ObjectClass *oc, void *data) -{ - ChardevClass *cc = CHARDEV_CLASS(oc); - - cc->chr_write = win_chr_write; - cc->chr_free = win_chr_free; -} - -static const TypeInfo char_win_type_info = { - .name = TYPE_CHARDEV_WIN, - .parent = TYPE_CHARDEV, - .instance_size = sizeof(WinChardev), - .class_init = char_win_class_init, - .abstract = true, -}; - -static void qemu_chr_open_win_con(Chardev *chr, - ChardevBackend *backend, - bool *be_opened, - Error **errp) -{ - qemu_chr_open_win_file(chr, GetStdHandle(STD_OUTPUT_HANDLE)); -} - -static const CharDriver console_driver = { - .kind = CHARDEV_BACKEND_KIND_CONSOLE, -}; - -static void char_console_class_init(ObjectClass *oc, void *data) -{ - ChardevClass *cc = CHARDEV_CLASS(oc); - - cc->open = qemu_chr_open_win_con; - cc->chr_free = NULL; -} - -static const TypeInfo char_console_type_info = { - .name = TYPE_CHARDEV_CONSOLE, - .parent = TYPE_CHARDEV_WIN, - .class_init = char_console_class_init, -}; - -static int win_stdio_write(Chardev *chr, const uint8_t *buf, int len) -{ - HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); - DWORD dwSize; - int len1; - - len1 = len; - - while (len1 > 0) { - if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) { - break; - } - buf += dwSize; - len1 -= dwSize; - } - - return len - len1; -} - -static void win_stdio_wait_func(void *opaque) -{ - Chardev *chr = CHARDEV(opaque); - WinStdioChardev *stdio = WIN_STDIO_CHARDEV(opaque); - INPUT_RECORD buf[4]; - int ret; - DWORD dwSize; - int i; - - ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize); - - if (!ret) { - /* Avoid error storm */ - qemu_del_wait_object(stdio->hStdIn, NULL, NULL); - return; - } - - for (i = 0; i < dwSize; i++) { - KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent; - - if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) { - int j; - if (kev->uChar.AsciiChar != 0) { - for (j = 0; j < kev->wRepeatCount; j++) { - if (qemu_chr_be_can_write(chr)) { - uint8_t c = kev->uChar.AsciiChar; - qemu_chr_be_write(chr, &c, 1); - } - } - } - } - } -} - -static DWORD WINAPI win_stdio_thread(LPVOID param) -{ - WinStdioChardev *stdio = WIN_STDIO_CHARDEV(param); - int ret; - DWORD dwSize; - - while (1) { - - /* Wait for one byte */ - ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL); - - /* Exit in case of error, continue if nothing read */ - if (!ret) { - break; - } - if (!dwSize) { - continue; - } - - /* Some terminal emulator returns \r\n for Enter, just pass \n */ - if (stdio->win_stdio_buf == '\r') { - continue; - } - - /* Signal the main thread and wait until the byte was eaten */ - if (!SetEvent(stdio->hInputReadyEvent)) { - break; - } - if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE) - != WAIT_OBJECT_0) { - break; - } - } - - qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL); - return 0; -} - -static void win_stdio_thread_wait_func(void *opaque) -{ - Chardev *chr = CHARDEV(opaque); - WinStdioChardev *stdio = WIN_STDIO_CHARDEV(opaque); - - if (qemu_chr_be_can_write(chr)) { - qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1); - } - - SetEvent(stdio->hInputDoneEvent); -} - -static void qemu_chr_set_echo_win_stdio(Chardev *chr, bool echo) -{ - WinStdioChardev *stdio = WIN_STDIO_CHARDEV(chr); - DWORD dwMode = 0; - - GetConsoleMode(stdio->hStdIn, &dwMode); - - if (echo) { - SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT); - } else { - SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT); - } -} - -static void win_stdio_free(Chardev *chr) -{ - WinStdioChardev *stdio = WIN_STDIO_CHARDEV(chr); - - if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) { - CloseHandle(stdio->hInputReadyEvent); - } - if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) { - CloseHandle(stdio->hInputDoneEvent); - } - if (stdio->hInputThread != INVALID_HANDLE_VALUE) { - TerminateThread(stdio->hInputThread, 0); - } -} - -static const TypeInfo char_win_stdio_type_info = { - .name = TYPE_CHARDEV_WIN_STDIO, - .parent = TYPE_CHARDEV, - .instance_size = sizeof(WinStdioChardev), - .abstract = true, -}; - -static void qemu_chr_open_stdio(Chardev *chr, - ChardevBackend *backend, - bool *be_opened, - Error **errp) -{ - WinStdioChardev *stdio = WIN_STDIO_CHARDEV(chr); - DWORD dwMode; - int is_console = 0; - - stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE); - if (stdio->hStdIn == INVALID_HANDLE_VALUE) { - error_setg(errp, "cannot open stdio: invalid handle"); - return; - } - - is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0; - - if (is_console) { - if (qemu_add_wait_object(stdio->hStdIn, - win_stdio_wait_func, chr)) { - error_setg(errp, "qemu_add_wait_object: failed"); - goto err1; - } - } else { - DWORD dwId; - - stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL); - stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL); - if (stdio->hInputReadyEvent == INVALID_HANDLE_VALUE - || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) { - error_setg(errp, "cannot create event"); - goto err2; - } - if (qemu_add_wait_object(stdio->hInputReadyEvent, - win_stdio_thread_wait_func, chr)) { - error_setg(errp, "qemu_add_wait_object: failed"); - goto err2; - } - stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread, - chr, 0, &dwId); - - if (stdio->hInputThread == INVALID_HANDLE_VALUE) { - error_setg(errp, "cannot create stdio thread"); - goto err3; - } - } - - dwMode |= ENABLE_LINE_INPUT; - - if (is_console) { - /* set the terminal in raw mode */ - /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */ - dwMode |= ENABLE_PROCESSED_INPUT; - } - - SetConsoleMode(stdio->hStdIn, dwMode); - - qemu_chr_set_echo_win_stdio(chr, false); - - return; - -err3: - qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL); -err2: - CloseHandle(stdio->hInputReadyEvent); - CloseHandle(stdio->hInputDoneEvent); -err1: - qemu_del_wait_object(stdio->hStdIn, NULL, NULL); -} -#endif /* !_WIN32 */ - -/***********************************************************/ -/* UDP Net console */ - -typedef struct { - Chardev parent; - QIOChannel *ioc; - uint8_t buf[READ_BUF_LEN]; - int bufcnt; - int bufptr; - int max_size; -} UdpChardev; - -#define UDP_CHARDEV(obj) OBJECT_CHECK(UdpChardev, (obj), TYPE_CHARDEV_UDP) - -/* Called with chr_write_lock held. */ -static int udp_chr_write(Chardev *chr, const uint8_t *buf, int len) -{ - UdpChardev *s = UDP_CHARDEV(chr); - - return qio_channel_write( - s->ioc, (const char *)buf, len, NULL); -} - -static int udp_chr_read_poll(void *opaque) -{ - Chardev *chr = CHARDEV(opaque); - UdpChardev *s = UDP_CHARDEV(opaque); - - s->max_size = qemu_chr_be_can_write(chr); - - /* If there were any stray characters in the queue process them - * first - */ - while (s->max_size > 0 && s->bufptr < s->bufcnt) { - qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); - s->bufptr++; - s->max_size = qemu_chr_be_can_write(chr); - } - return s->max_size; -} - -static gboolean udp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque) -{ - Chardev *chr = CHARDEV(opaque); - UdpChardev *s = UDP_CHARDEV(opaque); - ssize_t ret; - - if (s->max_size == 0) { - return TRUE; - } - ret = qio_channel_read( - s->ioc, (char *)s->buf, sizeof(s->buf), NULL); - if (ret <= 0) { - remove_fd_in_watch(chr); - return FALSE; - } - s->bufcnt = ret; - - s->bufptr = 0; - while (s->max_size > 0 && s->bufptr < s->bufcnt) { - qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); - s->bufptr++; - s->max_size = qemu_chr_be_can_write(chr); - } - - return TRUE; -} - -static void udp_chr_update_read_handler(Chardev *chr, - GMainContext *context) -{ - UdpChardev *s = UDP_CHARDEV(chr); - - remove_fd_in_watch(chr); - if (s->ioc) { - chr->fd_in_tag = io_add_watch_poll(chr, s->ioc, - udp_chr_read_poll, - udp_chr_read, chr, - context); - } -} - -static void udp_chr_free(Chardev *chr) -{ - UdpChardev *s = UDP_CHARDEV(chr); - - remove_fd_in_watch(chr); - if (s->ioc) { - object_unref(OBJECT(s->ioc)); - } - qemu_chr_be_event(chr, CHR_EVENT_CLOSED); -} - -/***********************************************************/ -/* TCP Net console */ - -typedef struct { - Chardev parent; - QIOChannel *ioc; /* Client I/O channel */ - QIOChannelSocket *sioc; /* Client master channel */ - QIOChannelSocket *listen_ioc; - guint listen_tag; - QCryptoTLSCreds *tls_creds; - int connected; - int max_size; - int do_telnetopt; - int do_nodelay; - int is_unix; - int *read_msgfds; - size_t read_msgfds_num; - int *write_msgfds; - size_t write_msgfds_num; - - SocketAddress *addr; - bool is_listen; - bool is_telnet; - - guint reconnect_timer; - int64_t reconnect_time; - bool connect_err_reported; -} SocketChardev; - -#define SOCKET_CHARDEV(obj) \ - OBJECT_CHECK(SocketChardev, (obj), TYPE_CHARDEV_SOCKET) - -static gboolean socket_reconnect_timeout(gpointer opaque); - -static void qemu_chr_socket_restart_timer(Chardev *chr) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - char *name; - - assert(s->connected == 0); - s->reconnect_timer = g_timeout_add_seconds(s->reconnect_time, - socket_reconnect_timeout, chr); - name = g_strdup_printf("chardev-socket-reconnect-%s", chr->label); - g_source_set_name_by_id(s->reconnect_timer, name); - g_free(name); -} - -static void check_report_connect_error(Chardev *chr, - Error *err) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - - if (!s->connect_err_reported) { - error_report("Unable to connect character device %s: %s", - chr->label, error_get_pretty(err)); - s->connect_err_reported = true; - } - qemu_chr_socket_restart_timer(chr); -} - -static gboolean tcp_chr_accept(QIOChannel *chan, - GIOCondition cond, - void *opaque); - -/* Called with chr_write_lock held. */ -static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - - if (s->connected) { - int ret = io_channel_send_full(s->ioc, buf, len, - s->write_msgfds, - s->write_msgfds_num); - - /* free the written msgfds, no matter what */ - if (s->write_msgfds_num) { - g_free(s->write_msgfds); - s->write_msgfds = 0; - s->write_msgfds_num = 0; - } - - return ret; - } else { - /* XXX: indicate an error ? */ - return len; - } -} - -static int tcp_chr_read_poll(void *opaque) -{ - Chardev *chr = CHARDEV(opaque); - SocketChardev *s = SOCKET_CHARDEV(opaque); - if (!s->connected) - return 0; - s->max_size = qemu_chr_be_can_write(chr); - return s->max_size; -} - -#define IAC 255 -#define IAC_BREAK 243 -static void tcp_chr_process_IAC_bytes(Chardev *chr, - SocketChardev *s, - uint8_t *buf, int *size) -{ - /* Handle any telnet client's basic IAC options to satisfy char by - * char mode with no echo. All IAC options will be removed from - * the buf and the do_telnetopt variable will be used to track the - * state of the width of the IAC information. - * - * IAC commands come in sets of 3 bytes with the exception of the - * "IAC BREAK" command and the double IAC. - */ - - int i; - int j = 0; - - for (i = 0; i < *size; i++) { - if (s->do_telnetopt > 1) { - if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { - /* Double IAC means send an IAC */ - if (j != i) - buf[j] = buf[i]; - j++; - s->do_telnetopt = 1; - } else { - if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { - /* Handle IAC break commands by sending a serial break */ - qemu_chr_be_event(chr, CHR_EVENT_BREAK); - s->do_telnetopt++; - } - s->do_telnetopt++; - } - if (s->do_telnetopt >= 4) { - s->do_telnetopt = 1; - } - } else { - if ((unsigned char)buf[i] == IAC) { - s->do_telnetopt = 2; - } else { - if (j != i) - buf[j] = buf[i]; - j++; - } - } - } - *size = j; -} - -static int tcp_get_msgfds(Chardev *chr, int *fds, int num) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - - int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num; - - assert(num <= TCP_MAX_FDS); - - if (to_copy) { - int i; - - memcpy(fds, s->read_msgfds, to_copy * sizeof(int)); - - /* Close unused fds */ - for (i = to_copy; i < s->read_msgfds_num; i++) { - close(s->read_msgfds[i]); - } - - g_free(s->read_msgfds); - s->read_msgfds = 0; - s->read_msgfds_num = 0; - } - - return to_copy; -} - -static int tcp_set_msgfds(Chardev *chr, int *fds, int num) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - - /* clear old pending fd array */ - g_free(s->write_msgfds); - s->write_msgfds = NULL; - s->write_msgfds_num = 0; - - if (!s->connected || - !qio_channel_has_feature(s->ioc, - QIO_CHANNEL_FEATURE_FD_PASS)) { - return -1; - } - - if (num) { - s->write_msgfds = g_new(int, num); - memcpy(s->write_msgfds, fds, num * sizeof(int)); - } - - s->write_msgfds_num = num; - - return 0; -} - -static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - struct iovec iov = { .iov_base = buf, .iov_len = len }; - int ret; - size_t i; - int *msgfds = NULL; - size_t msgfds_num = 0; - - if (qio_channel_has_feature(s->ioc, QIO_CHANNEL_FEATURE_FD_PASS)) { - ret = qio_channel_readv_full(s->ioc, &iov, 1, - &msgfds, &msgfds_num, - NULL); - } else { - ret = qio_channel_readv_full(s->ioc, &iov, 1, - NULL, NULL, - NULL); - } - - if (ret == QIO_CHANNEL_ERR_BLOCK) { - errno = EAGAIN; - ret = -1; - } else if (ret == -1) { - errno = EIO; - } - - if (msgfds_num) { - /* close and clean read_msgfds */ - for (i = 0; i < s->read_msgfds_num; i++) { - close(s->read_msgfds[i]); - } - - if (s->read_msgfds_num) { - g_free(s->read_msgfds); - } - - s->read_msgfds = msgfds; - s->read_msgfds_num = msgfds_num; - } - - for (i = 0; i < s->read_msgfds_num; i++) { - int fd = s->read_msgfds[i]; - if (fd < 0) { - continue; - } - - /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */ - qemu_set_block(fd); - -#ifndef MSG_CMSG_CLOEXEC - qemu_set_cloexec(fd); -#endif - } - - return ret; -} - -static GSource *tcp_chr_add_watch(Chardev *chr, GIOCondition cond) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - return qio_channel_create_watch(s->ioc, cond); -} - -static void tcp_chr_free_connection(Chardev *chr) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - int i; - - if (!s->connected) { - return; - } - - if (s->read_msgfds_num) { - for (i = 0; i < s->read_msgfds_num; i++) { - close(s->read_msgfds[i]); - } - g_free(s->read_msgfds); - s->read_msgfds = NULL; - s->read_msgfds_num = 0; - } - - tcp_set_msgfds(chr, NULL, 0); - remove_fd_in_watch(chr); - object_unref(OBJECT(s->sioc)); - s->sioc = NULL; - object_unref(OBJECT(s->ioc)); - s->ioc = NULL; - g_free(chr->filename); - chr->filename = NULL; - s->connected = 0; -} - -static void tcp_chr_disconnect(Chardev *chr) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - - if (!s->connected) { - return; - } - - tcp_chr_free_connection(chr); - - if (s->listen_ioc) { - s->listen_tag = qio_channel_add_watch( - QIO_CHANNEL(s->listen_ioc), G_IO_IN, tcp_chr_accept, chr, NULL); - } - chr->filename = SocketAddress_to_str("disconnected:", s->addr, - s->is_listen, s->is_telnet); - qemu_chr_be_event(chr, CHR_EVENT_CLOSED); - if (s->reconnect_time) { - qemu_chr_socket_restart_timer(chr); - } -} - -static gboolean tcp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque) -{ - Chardev *chr = CHARDEV(opaque); - SocketChardev *s = SOCKET_CHARDEV(opaque); - uint8_t buf[READ_BUF_LEN]; - int len, size; - - if (!s->connected || s->max_size <= 0) { - return TRUE; - } - len = sizeof(buf); - if (len > s->max_size) - len = s->max_size; - size = tcp_chr_recv(chr, (void *)buf, len); - if (size == 0 || size == -1) { - /* connection closed */ - tcp_chr_disconnect(chr); - } else if (size > 0) { - if (s->do_telnetopt) - tcp_chr_process_IAC_bytes(chr, s, buf, &size); - if (size > 0) - qemu_chr_be_write(chr, buf, size); - } - - return TRUE; -} - -static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - int size; - - if (!s->connected) { - return 0; - } - - size = tcp_chr_recv(chr, (void *) buf, len); - if (size == 0) { - /* connection closed */ - tcp_chr_disconnect(chr); - } - - return size; -} - -static void tcp_chr_connect(void *opaque) -{ - Chardev *chr = CHARDEV(opaque); - SocketChardev *s = SOCKET_CHARDEV(opaque); - - g_free(chr->filename); - chr->filename = sockaddr_to_str( - &s->sioc->localAddr, s->sioc->localAddrLen, - &s->sioc->remoteAddr, s->sioc->remoteAddrLen, - s->is_listen, s->is_telnet); - - s->connected = 1; - if (s->ioc) { - chr->fd_in_tag = io_add_watch_poll(chr, s->ioc, - tcp_chr_read_poll, - tcp_chr_read, - chr, NULL); - } - qemu_chr_be_generic_open(chr); -} - -static void tcp_chr_update_read_handler(Chardev *chr, - GMainContext *context) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - - if (!s->connected) { - return; - } - - remove_fd_in_watch(chr); - if (s->ioc) { - chr->fd_in_tag = io_add_watch_poll(chr, s->ioc, - tcp_chr_read_poll, - tcp_chr_read, chr, - context); - } -} - -typedef struct { - Chardev *chr; - char buf[12]; - size_t buflen; -} TCPCharDriverTelnetInit; - -static gboolean tcp_chr_telnet_init_io(QIOChannel *ioc, - GIOCondition cond G_GNUC_UNUSED, - gpointer user_data) -{ - TCPCharDriverTelnetInit *init = user_data; - ssize_t ret; - - ret = qio_channel_write(ioc, init->buf, init->buflen, NULL); - if (ret < 0) { - if (ret == QIO_CHANNEL_ERR_BLOCK) { - ret = 0; - } else { - tcp_chr_disconnect(init->chr); - return FALSE; - } - } - init->buflen -= ret; - - if (init->buflen == 0) { - tcp_chr_connect(init->chr); - return FALSE; - } - - memmove(init->buf, init->buf + ret, init->buflen); - - return TRUE; -} - -static void tcp_chr_telnet_init(Chardev *chr) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - TCPCharDriverTelnetInit *init = - g_new0(TCPCharDriverTelnetInit, 1); - size_t n = 0; - - init->chr = chr; - init->buflen = 12; - -#define IACSET(x, a, b, c) \ - do { \ - x[n++] = a; \ - x[n++] = b; \ - x[n++] = c; \ - } while (0) - - /* Prep the telnet negotion to put telnet in binary, - * no echo, single char mode */ - IACSET(init->buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ - IACSET(init->buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ - IACSET(init->buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ - IACSET(init->buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ - -#undef IACSET - - qio_channel_add_watch( - s->ioc, G_IO_OUT, - tcp_chr_telnet_init_io, - init, NULL); -} - - -static void tcp_chr_tls_handshake(QIOTask *task, - gpointer user_data) -{ - Chardev *chr = user_data; - SocketChardev *s = user_data; - - if (qio_task_propagate_error(task, NULL)) { - tcp_chr_disconnect(chr); - } else { - if (s->do_telnetopt) { - tcp_chr_telnet_init(chr); - } else { - tcp_chr_connect(chr); - } - } -} - - -static void tcp_chr_tls_init(Chardev *chr) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - QIOChannelTLS *tioc; - Error *err = NULL; - gchar *name; - - if (s->is_listen) { - tioc = qio_channel_tls_new_server( - s->ioc, s->tls_creds, - NULL, /* XXX Use an ACL */ - &err); - } else { - tioc = qio_channel_tls_new_client( - s->ioc, s->tls_creds, - s->addr->u.inet.data->host, - &err); - } - if (tioc == NULL) { - error_free(err); - tcp_chr_disconnect(chr); - return; - } - name = g_strdup_printf("chardev-tls-%s-%s", - s->is_listen ? "server" : "client", - chr->label); - qio_channel_set_name(QIO_CHANNEL(tioc), name); - g_free(name); - object_unref(OBJECT(s->ioc)); - s->ioc = QIO_CHANNEL(tioc); - - qio_channel_tls_handshake(tioc, - tcp_chr_tls_handshake, - chr, - NULL); -} - - -static void tcp_chr_set_client_ioc_name(Chardev *chr, - QIOChannelSocket *sioc) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - char *name; - name = g_strdup_printf("chardev-tcp-%s-%s", - s->is_listen ? "server" : "client", - chr->label); - qio_channel_set_name(QIO_CHANNEL(sioc), name); - g_free(name); - -} - -static int tcp_chr_new_client(Chardev *chr, QIOChannelSocket *sioc) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - - if (s->ioc != NULL) { - return -1; - } - - s->ioc = QIO_CHANNEL(sioc); - object_ref(OBJECT(sioc)); - s->sioc = sioc; - object_ref(OBJECT(sioc)); - - qio_channel_set_blocking(s->ioc, false, NULL); - - if (s->do_nodelay) { - qio_channel_set_delay(s->ioc, false); - } - if (s->listen_tag) { - g_source_remove(s->listen_tag); - s->listen_tag = 0; - } - - if (s->tls_creds) { - tcp_chr_tls_init(chr); - } else { - if (s->do_telnetopt) { - tcp_chr_telnet_init(chr); - } else { - tcp_chr_connect(chr); - } - } - - return 0; -} - - -static int tcp_chr_add_client(Chardev *chr, int fd) -{ - int ret; - QIOChannelSocket *sioc; - - sioc = qio_channel_socket_new_fd(fd, NULL); - if (!sioc) { - return -1; - } - tcp_chr_set_client_ioc_name(chr, sioc); - ret = tcp_chr_new_client(chr, sioc); - object_unref(OBJECT(sioc)); - return ret; -} - -static gboolean tcp_chr_accept(QIOChannel *channel, - GIOCondition cond, - void *opaque) -{ - Chardev *chr = CHARDEV(opaque); - QIOChannelSocket *sioc; - - sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(channel), - NULL); - if (!sioc) { - return TRUE; - } - - tcp_chr_new_client(chr, sioc); - - object_unref(OBJECT(sioc)); - - return TRUE; -} - -static int tcp_chr_wait_connected(Chardev *chr, Error **errp) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - QIOChannelSocket *sioc; - - /* It can't wait on s->connected, since it is set asynchronously - * in TLS and telnet cases, only wait for an accepted socket */ - while (!s->ioc) { - if (s->is_listen) { - error_report("QEMU waiting for connection on: %s", - chr->filename); - qio_channel_set_blocking(QIO_CHANNEL(s->listen_ioc), true, NULL); - tcp_chr_accept(QIO_CHANNEL(s->listen_ioc), G_IO_IN, chr); - qio_channel_set_blocking(QIO_CHANNEL(s->listen_ioc), false, NULL); - } else { - sioc = qio_channel_socket_new(); - tcp_chr_set_client_ioc_name(chr, sioc); - if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) { - object_unref(OBJECT(sioc)); - return -1; - } - tcp_chr_new_client(chr, sioc); - object_unref(OBJECT(sioc)); - } - } - - return 0; -} - -static int qemu_chr_wait_connected(Chardev *chr, Error **errp) -{ - ChardevClass *cc = CHARDEV_GET_CLASS(chr); - - if (cc->chr_wait_connected) { - return cc->chr_wait_connected(chr, errp); - } - - return 0; -} - -int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp) -{ - if (!be->chr) { - error_setg(errp, "missing associated backend"); - return -1; - } - - return qemu_chr_wait_connected(be->chr, errp); -} - -static void tcp_chr_free(Chardev *chr) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - - tcp_chr_free_connection(chr); - - if (s->reconnect_timer) { - g_source_remove(s->reconnect_timer); - s->reconnect_timer = 0; - } - qapi_free_SocketAddress(s->addr); - if (s->listen_tag) { - g_source_remove(s->listen_tag); - s->listen_tag = 0; - } - if (s->listen_ioc) { - object_unref(OBJECT(s->listen_ioc)); - } - if (s->tls_creds) { - object_unref(OBJECT(s->tls_creds)); - } - - qemu_chr_be_event(chr, CHR_EVENT_CLOSED); -} - - -static void qemu_chr_socket_connected(QIOTask *task, void *opaque) -{ - QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task)); - Chardev *chr = CHARDEV(opaque); - SocketChardev *s = SOCKET_CHARDEV(chr); - Error *err = NULL; - - if (qio_task_propagate_error(task, &err)) { - check_report_connect_error(chr, err); - error_free(err); - goto cleanup; - } - - s->connect_err_reported = false; - tcp_chr_new_client(chr, sioc); - - cleanup: - object_unref(OBJECT(sioc)); -} - - -/*********************************************************/ -/* Ring buffer chardev */ - -typedef struct { - Chardev parent; - size_t size; - size_t prod; - size_t cons; - uint8_t *cbuf; -} RingBufChardev; - -#define RINGBUF_CHARDEV(obj) \ - OBJECT_CHECK(RingBufChardev, (obj), TYPE_CHARDEV_RINGBUF) - -static size_t ringbuf_count(const Chardev *chr) -{ - const RingBufChardev *d = RINGBUF_CHARDEV(chr); - - return d->prod - d->cons; -} - -/* Called with chr_write_lock held. */ -static int ringbuf_chr_write(Chardev *chr, const uint8_t *buf, int len) -{ - RingBufChardev *d = RINGBUF_CHARDEV(chr); - int i; - - if (!buf || (len < 0)) { - return -1; - } - - for (i = 0; i < len; i++ ) { - d->cbuf[d->prod++ & (d->size - 1)] = buf[i]; - if (d->prod - d->cons > d->size) { - d->cons = d->prod - d->size; - } - } - - return len; -} - -static int ringbuf_chr_read(Chardev *chr, uint8_t *buf, int len) -{ - RingBufChardev *d = RINGBUF_CHARDEV(chr); - int i; - - qemu_mutex_lock(&chr->chr_write_lock); - for (i = 0; i < len && d->cons != d->prod; i++) { - buf[i] = d->cbuf[d->cons++ & (d->size - 1)]; - } - qemu_mutex_unlock(&chr->chr_write_lock); - - return i; -} - -static void ringbuf_chr_free(struct Chardev *chr) -{ - RingBufChardev *d = RINGBUF_CHARDEV(chr); - - g_free(d->cbuf); -} - -static void qemu_chr_open_ringbuf(Chardev *chr, - ChardevBackend *backend, - bool *be_opened, - Error **errp) -{ - ChardevRingbuf *opts = backend->u.ringbuf.data; - RingBufChardev *d = RINGBUF_CHARDEV(chr); - - d->size = opts->has_size ? opts->size : 65536; - - /* The size must be power of 2 */ - if (d->size & (d->size - 1)) { - error_setg(errp, "size of ringbuf chardev must be power of two"); - return; - } - - d->prod = 0; - d->cons = 0; - d->cbuf = g_malloc0(d->size); -} - -void qmp_ringbuf_write(const char *device, const char *data, - bool has_format, enum DataFormat format, - Error **errp) -{ - Chardev *chr; - const uint8_t *write_data; - int ret; - gsize write_count; - - chr = qemu_chr_find(device); - if (!chr) { - error_setg(errp, "Device '%s' not found", device); - return; - } - - if (!CHARDEV_IS_RINGBUF(chr)) { - error_setg(errp,"%s is not a ringbuf device", device); - return; - } - - if (has_format && (format == DATA_FORMAT_BASE64)) { - write_data = qbase64_decode(data, -1, - &write_count, - errp); - if (!write_data) { - return; - } - } else { - write_data = (uint8_t *)data; - write_count = strlen(data); - } - - ret = ringbuf_chr_write(chr, write_data, write_count); - - if (write_data != (uint8_t *)data) { - g_free((void *)write_data); - } - - if (ret < 0) { - error_setg(errp, "Failed to write to device %s", device); - return; - } -} - -char *qmp_ringbuf_read(const char *device, int64_t size, - bool has_format, enum DataFormat format, - Error **errp) -{ - Chardev *chr; - uint8_t *read_data; - size_t count; - char *data; - - chr = qemu_chr_find(device); - if (!chr) { - error_setg(errp, "Device '%s' not found", device); - return NULL; - } - - if (!CHARDEV_IS_RINGBUF(chr)) { - error_setg(errp,"%s is not a ringbuf device", device); - return NULL; - } - - if (size <= 0) { - error_setg(errp, "size must be greater than zero"); - return NULL; - } - - count = ringbuf_count(chr); - size = size > count ? count : size; - read_data = g_malloc(size + 1); - - ringbuf_chr_read(chr, read_data, size); - - if (has_format && (format == DATA_FORMAT_BASE64)) { - data = g_base64_encode(read_data, size); - g_free(read_data); - } else { - /* - * FIXME should read only complete, valid UTF-8 characters up - * to @size bytes. Invalid sequences should be replaced by a - * suitable replacement character. Except when (and only - * when) ring buffer lost characters since last read, initial - * continuation characters should be dropped. - */ - read_data[size] = 0; - data = (char *)read_data; - } - - return data; -} - -QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename) -{ - char host[65], port[33], width[8], height[8]; - int pos; - const char *p; - QemuOpts *opts; - Error *local_err = NULL; - - opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err); - if (local_err) { - error_report_err(local_err); - return NULL; - } - - if (strstart(filename, "mon:", &p)) { - filename = p; - qemu_opt_set(opts, "mux", "on", &error_abort); - if (strcmp(filename, "stdio") == 0) { - /* Monitor is muxed to stdio: do not exit on Ctrl+C by default - * but pass it to the guest. Handle this only for compat syntax, - * for -chardev syntax we have special option for this. - * This is what -nographic did, redirecting+muxing serial+monitor - * to stdio causing Ctrl+C to be passed to guest. */ - qemu_opt_set(opts, "signal", "off", &error_abort); - } - } - - if (strcmp(filename, "null") == 0 || - strcmp(filename, "pty") == 0 || - strcmp(filename, "msmouse") == 0 || - strcmp(filename, "braille") == 0 || - strcmp(filename, "testdev") == 0 || - strcmp(filename, "stdio") == 0) { - qemu_opt_set(opts, "backend", filename, &error_abort); - return opts; - } - if (strstart(filename, "vc", &p)) { - qemu_opt_set(opts, "backend", "vc", &error_abort); - if (*p == ':') { - if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) { - /* pixels */ - qemu_opt_set(opts, "width", width, &error_abort); - qemu_opt_set(opts, "height", height, &error_abort); - } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) { - /* chars */ - qemu_opt_set(opts, "cols", width, &error_abort); - qemu_opt_set(opts, "rows", height, &error_abort); - } else { - goto fail; - } - } - return opts; - } - if (strcmp(filename, "con:") == 0) { - qemu_opt_set(opts, "backend", "console", &error_abort); - return opts; - } - if (strstart(filename, "COM", NULL)) { - qemu_opt_set(opts, "backend", "serial", &error_abort); - qemu_opt_set(opts, "path", filename, &error_abort); - return opts; - } - if (strstart(filename, "file:", &p)) { - qemu_opt_set(opts, "backend", "file", &error_abort); - qemu_opt_set(opts, "path", p, &error_abort); - return opts; - } - if (strstart(filename, "pipe:", &p)) { - qemu_opt_set(opts, "backend", "pipe", &error_abort); - qemu_opt_set(opts, "path", p, &error_abort); - return opts; - } - if (strstart(filename, "tcp:", &p) || - strstart(filename, "telnet:", &p)) { - if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { - host[0] = 0; - if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) - goto fail; - } - qemu_opt_set(opts, "backend", "socket", &error_abort); - qemu_opt_set(opts, "host", host, &error_abort); - qemu_opt_set(opts, "port", port, &error_abort); - if (p[pos] == ',') { - qemu_opts_do_parse(opts, p+pos+1, NULL, &local_err); - if (local_err) { - error_report_err(local_err); - goto fail; - } - } - if (strstart(filename, "telnet:", &p)) - qemu_opt_set(opts, "telnet", "on", &error_abort); - return opts; - } - if (strstart(filename, "udp:", &p)) { - qemu_opt_set(opts, "backend", "udp", &error_abort); - if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) { - host[0] = 0; - if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) { - goto fail; - } - } - qemu_opt_set(opts, "host", host, &error_abort); - qemu_opt_set(opts, "port", port, &error_abort); - if (p[pos] == '@') { - p += pos + 1; - if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { - host[0] = 0; - if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) { - goto fail; - } - } - qemu_opt_set(opts, "localaddr", host, &error_abort); - qemu_opt_set(opts, "localport", port, &error_abort); - } - return opts; - } - if (strstart(filename, "unix:", &p)) { - qemu_opt_set(opts, "backend", "socket", &error_abort); - qemu_opts_do_parse(opts, p, "path", &local_err); - if (local_err) { - error_report_err(local_err); - goto fail; - } - return opts; - } - if (strstart(filename, "/dev/parport", NULL) || - strstart(filename, "/dev/ppi", NULL)) { - qemu_opt_set(opts, "backend", "parport", &error_abort); - qemu_opt_set(opts, "path", filename, &error_abort); - return opts; - } - if (strstart(filename, "/dev/", NULL)) { - qemu_opt_set(opts, "backend", "tty", &error_abort); - qemu_opt_set(opts, "path", filename, &error_abort); - return opts; - } - -fail: - qemu_opts_del(opts); - return NULL; -} - -void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend) -{ - const char *logfile = qemu_opt_get(opts, "logfile"); - - backend->has_logfile = logfile != NULL; - backend->logfile = logfile ? g_strdup(logfile) : NULL; - - backend->has_logappend = true; - backend->logappend = qemu_opt_get_bool(opts, "logappend", false); -} - - -static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend, - Error **errp) -{ - const char *path = qemu_opt_get(opts, "path"); - ChardevFile *file; - - if (path == NULL) { - error_setg(errp, "chardev: file: no filename given"); - return; - } - file = backend->u.file.data = g_new0(ChardevFile, 1); - qemu_chr_parse_common(opts, qapi_ChardevFile_base(file)); - file->out = g_strdup(path); - - file->has_append = true; - file->append = qemu_opt_get_bool(opts, "append", false); -} - -static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend, - Error **errp) -{ - ChardevStdio *stdio; - - stdio = backend->u.stdio.data = g_new0(ChardevStdio, 1); - qemu_chr_parse_common(opts, qapi_ChardevStdio_base(stdio)); - stdio->has_signal = true; - stdio->signal = qemu_opt_get_bool(opts, "signal", true); -} - -static const CharDriver stdio_driver = { - .kind = CHARDEV_BACKEND_KIND_STDIO, - .parse = qemu_chr_parse_stdio, -}; - -static void char_stdio_class_init(ObjectClass *oc, void *data) -{ - ChardevClass *cc = CHARDEV_CLASS(oc); - - cc->open = qemu_chr_open_stdio; -#ifdef _WIN32 - cc->chr_write = win_stdio_write; - cc->chr_set_echo = qemu_chr_set_echo_win_stdio; - cc->chr_free = win_stdio_free; -#else - cc->chr_set_echo = qemu_chr_set_echo_stdio; - cc->chr_free = qemu_chr_free_stdio; -#endif -} - -static const TypeInfo char_stdio_type_info = { - .name = TYPE_CHARDEV_STDIO, -#ifdef _WIN32 - .parent = TYPE_CHARDEV_WIN_STDIO, -#else - .parent = TYPE_CHARDEV_FD, -#endif - .class_init = char_stdio_class_init, -}; - -#ifdef HAVE_CHARDEV_SERIAL -static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend, - Error **errp) -{ - const char *device = qemu_opt_get(opts, "path"); - ChardevHostdev *serial; - - if (device == NULL) { - error_setg(errp, "chardev: serial/tty: no device path given"); - return; - } - serial = backend->u.serial.data = g_new0(ChardevHostdev, 1); - qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(serial)); - serial->device = g_strdup(device); -} -#endif - -#ifdef HAVE_CHARDEV_PARPORT -static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend, - Error **errp) -{ - const char *device = qemu_opt_get(opts, "path"); - ChardevHostdev *parallel; - - if (device == NULL) { - error_setg(errp, "chardev: parallel: no device path given"); - return; - } - parallel = backend->u.parallel.data = g_new0(ChardevHostdev, 1); - qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(parallel)); - parallel->device = g_strdup(device); -} -#endif - -static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend, - Error **errp) -{ - const char *device = qemu_opt_get(opts, "path"); - ChardevHostdev *dev; - - if (device == NULL) { - error_setg(errp, "chardev: pipe: no device path given"); - return; - } - dev = backend->u.pipe.data = g_new0(ChardevHostdev, 1); - qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(dev)); - dev->device = g_strdup(device); -} - -static const CharDriver pipe_driver = { - .kind = CHARDEV_BACKEND_KIND_PIPE, - .parse = qemu_chr_parse_pipe, -}; - -static void char_pipe_class_init(ObjectClass *oc, void *data) -{ - ChardevClass *cc = CHARDEV_CLASS(oc); - - cc->open = qemu_chr_open_pipe; -} - -static const TypeInfo char_pipe_type_info = { - .name = TYPE_CHARDEV_PIPE, -#ifdef _WIN32 - .parent = TYPE_CHARDEV_WIN, -#else - .parent = TYPE_CHARDEV_FD, -#endif - .class_init = char_pipe_class_init, -}; - -static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend, - Error **errp) -{ - int val; - ChardevRingbuf *ringbuf; - - ringbuf = backend->u.ringbuf.data = g_new0(ChardevRingbuf, 1); - qemu_chr_parse_common(opts, qapi_ChardevRingbuf_base(ringbuf)); - - val = qemu_opt_get_size(opts, "size", 0); - if (val != 0) { - ringbuf->has_size = true; - ringbuf->size = val; - } -} - -static const CharDriver ringbuf_driver = { - .kind = CHARDEV_BACKEND_KIND_RINGBUF, - .parse = qemu_chr_parse_ringbuf, -}; - -static void char_ringbuf_class_init(ObjectClass *oc, void *data) -{ - ChardevClass *cc = CHARDEV_CLASS(oc); - - cc->open = qemu_chr_open_ringbuf; - cc->chr_write = ringbuf_chr_write; - cc->chr_free = ringbuf_chr_free; -} - -static const TypeInfo char_ringbuf_type_info = { - .name = TYPE_CHARDEV_RINGBUF, - .parent = TYPE_CHARDEV, - .class_init = char_ringbuf_class_init, - .instance_size = sizeof(RingBufChardev), -}; - -/* Bug-compatibility: */ -static const CharDriver memory_driver = { - .kind = CHARDEV_BACKEND_KIND_MEMORY, - .parse = qemu_chr_parse_ringbuf, -}; - -static const TypeInfo char_memory_type_info = { - .name = TYPE_CHARDEV_MEMORY, - .parent = TYPE_CHARDEV_RINGBUF, -}; - -static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend, - Error **errp) -{ - const char *chardev = qemu_opt_get(opts, "chardev"); - ChardevMux *mux; - - if (chardev == NULL) { - error_setg(errp, "chardev: mux: no chardev given"); - return; - } - mux = backend->u.mux.data = g_new0(ChardevMux, 1); - qemu_chr_parse_common(opts, qapi_ChardevMux_base(mux)); - mux->chardev = g_strdup(chardev); -} - -static const CharDriver mux_driver = { - .kind = CHARDEV_BACKEND_KIND_MUX, - .parse = qemu_chr_parse_mux, -}; - -static void char_mux_class_init(ObjectClass *oc, void *data) -{ - ChardevClass *cc = CHARDEV_CLASS(oc); - - cc->open = qemu_chr_open_mux; - cc->chr_free = mux_chr_free; - cc->chr_write = mux_chr_write; - cc->chr_accept_input = mux_chr_accept_input; - cc->chr_add_watch = mux_chr_add_watch; -} - -static const TypeInfo char_mux_type_info = { - .name = TYPE_CHARDEV_MUX, - .parent = TYPE_CHARDEV, - .class_init = char_mux_class_init, - .instance_size = sizeof(MuxChardev), -}; - -static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, - Error **errp) -{ - bool is_listen = qemu_opt_get_bool(opts, "server", false); - bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true); - bool is_telnet = qemu_opt_get_bool(opts, "telnet", false); - bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true); - int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0); - const char *path = qemu_opt_get(opts, "path"); - const char *host = qemu_opt_get(opts, "host"); - const char *port = qemu_opt_get(opts, "port"); - const char *tls_creds = qemu_opt_get(opts, "tls-creds"); - SocketAddress *addr; - ChardevSocket *sock; - - if (!path) { - if (!host) { - error_setg(errp, "chardev: socket: no host given"); - return; - } - if (!port) { - error_setg(errp, "chardev: socket: no port given"); - return; - } - } else { - if (tls_creds) { - error_setg(errp, "TLS can only be used over TCP socket"); - return; - } - } - - sock = backend->u.socket.data = g_new0(ChardevSocket, 1); - qemu_chr_parse_common(opts, qapi_ChardevSocket_base(sock)); - - sock->has_nodelay = true; - sock->nodelay = do_nodelay; - sock->has_server = true; - sock->server = is_listen; - sock->has_telnet = true; - sock->telnet = is_telnet; - sock->has_wait = true; - sock->wait = is_waitconnect; - sock->has_reconnect = true; - sock->reconnect = reconnect; - sock->tls_creds = g_strdup(tls_creds); - - addr = g_new0(SocketAddress, 1); - if (path) { - UnixSocketAddress *q_unix; - addr->type = SOCKET_ADDRESS_KIND_UNIX; - q_unix = addr->u.q_unix.data = g_new0(UnixSocketAddress, 1); - q_unix->path = g_strdup(path); - } else { - addr->type = SOCKET_ADDRESS_KIND_INET; - addr->u.inet.data = g_new(InetSocketAddress, 1); - *addr->u.inet.data = (InetSocketAddress) { - .host = g_strdup(host), - .port = g_strdup(port), - .has_to = qemu_opt_get(opts, "to"), - .to = qemu_opt_get_number(opts, "to", 0), - .has_ipv4 = qemu_opt_get(opts, "ipv4"), - .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0), - .has_ipv6 = qemu_opt_get(opts, "ipv6"), - .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0), - }; - } - sock->addr = addr; -} - -static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend, - Error **errp) -{ - const char *host = qemu_opt_get(opts, "host"); - const char *port = qemu_opt_get(opts, "port"); - const char *localaddr = qemu_opt_get(opts, "localaddr"); - const char *localport = qemu_opt_get(opts, "localport"); - bool has_local = false; - SocketAddress *addr; - ChardevUdp *udp; - - if (host == NULL || strlen(host) == 0) { - host = "localhost"; - } - if (port == NULL || strlen(port) == 0) { - error_setg(errp, "chardev: udp: remote port not specified"); - return; - } - if (localport == NULL || strlen(localport) == 0) { - localport = "0"; - } else { - has_local = true; - } - if (localaddr == NULL || strlen(localaddr) == 0) { - localaddr = ""; - } else { - has_local = true; - } - - udp = backend->u.udp.data = g_new0(ChardevUdp, 1); - qemu_chr_parse_common(opts, qapi_ChardevUdp_base(udp)); - - addr = g_new0(SocketAddress, 1); - addr->type = SOCKET_ADDRESS_KIND_INET; - addr->u.inet.data = g_new(InetSocketAddress, 1); - *addr->u.inet.data = (InetSocketAddress) { - .host = g_strdup(host), - .port = g_strdup(port), - .has_ipv4 = qemu_opt_get(opts, "ipv4"), - .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0), - .has_ipv6 = qemu_opt_get(opts, "ipv6"), - .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0), - }; - udp->remote = addr; - - if (has_local) { - udp->has_local = true; - addr = g_new0(SocketAddress, 1); - addr->type = SOCKET_ADDRESS_KIND_INET; - addr->u.inet.data = g_new(InetSocketAddress, 1); - *addr->u.inet.data = (InetSocketAddress) { - .host = g_strdup(localaddr), - .port = g_strdup(localport), - }; - udp->local = addr; - } -} - -static const CharDriver *backends[CHARDEV_BACKEND_KIND__MAX]; - -void register_char_driver(const CharDriver *driver) -{ - backends[driver->kind] = driver; -} - -Chardev *qemu_chr_new_from_opts(QemuOpts *opts, - Error **errp) -{ - Error *local_err = NULL; - const CharDriver *cd = NULL; - Chardev *chr; - int i; - ChardevReturn *ret = NULL; - ChardevBackend *backend; - const char *name = qemu_opt_get(opts, "backend"); - const char *id = qemu_opts_id(opts); - char *bid = NULL; - - if (name == NULL) { - error_setg(errp, "chardev: \"%s\" missing backend", - qemu_opts_id(opts)); - goto err; - } - - if (is_help_option(name)) { - GString *str = g_string_new(""); - for (i = 0; i < ARRAY_SIZE(backends); i++) { - cd = backends[i]; - if (cd) { - g_string_append_printf(str, "\n%s", ChardevBackendKind_lookup[cd->kind]); - if (cd->alias) { - g_string_append_printf(str, "\n%s", cd->alias); - } - } - } - - error_report("Available chardev backend types: %s", str->str); - g_string_free(str, true); - exit(0); - } - - if (id == NULL) { - error_setg(errp, "chardev: no id specified"); - goto err; - } - - for (i = 0; i < ARRAY_SIZE(backends); i++) { - cd = backends[i]; - if (!cd) { - continue; - } - if (g_strcmp0(ChardevBackendKind_lookup[cd->kind], name) == 0 || - g_strcmp0(cd->alias, name) == 0) { - break; - } - } - if (i == ARRAY_SIZE(backends)) { - error_setg(errp, "chardev: backend \"%s\" not found", name); - goto err; - } - - backend = g_new0(ChardevBackend, 1); - - if (qemu_opt_get_bool(opts, "mux", 0)) { - bid = g_strdup_printf("%s-base", id); - } - - chr = NULL; - backend->type = cd->kind; - if (cd->parse) { - cd->parse(opts, backend, &local_err); - if (local_err) { - error_propagate(errp, local_err); - goto qapi_out; - } - } else { - ChardevCommon *cc = g_new0(ChardevCommon, 1); - qemu_chr_parse_common(opts, cc); - backend->u.null.data = cc; /* Any ChardevCommon member would work */ - } - - ret = qmp_chardev_add(bid ? bid : id, backend, errp); - if (!ret) { - goto qapi_out; - } - - if (bid) { - qapi_free_ChardevBackend(backend); - qapi_free_ChardevReturn(ret); - backend = g_new0(ChardevBackend, 1); - backend->u.mux.data = g_new0(ChardevMux, 1); - backend->type = CHARDEV_BACKEND_KIND_MUX; - backend->u.mux.data->chardev = g_strdup(bid); - ret = qmp_chardev_add(id, backend, errp); - if (!ret) { - chr = qemu_chr_find(bid); - qemu_chr_delete(chr); - chr = NULL; - goto qapi_out; - } - } - - chr = qemu_chr_find(id); - -qapi_out: - qapi_free_ChardevBackend(backend); - qapi_free_ChardevReturn(ret); - g_free(bid); - return chr; - -err: - return NULL; -} - -Chardev *qemu_chr_new_noreplay(const char *label, const char *filename) -{ - const char *p; - Chardev *chr; - QemuOpts *opts; - Error *err = NULL; - - if (strstart(filename, "chardev:", &p)) { - return qemu_chr_find(p); - } - - opts = qemu_chr_parse_compat(label, filename); - if (!opts) - return NULL; - - chr = qemu_chr_new_from_opts(opts, &err); - if (err) { - error_report_err(err); - } - if (chr && qemu_opt_get_bool(opts, "mux", 0)) { - monitor_init(chr, MONITOR_USE_READLINE); - } - qemu_opts_del(opts); - return chr; -} - -Chardev *qemu_chr_new(const char *label, const char *filename) -{ - Chardev *chr; - chr = qemu_chr_new_noreplay(label, filename); - if (chr) { - if (replay_mode != REPLAY_MODE_NONE) { - qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_REPLAY); - } - if (qemu_chr_replay(chr) && CHARDEV_GET_CLASS(chr)->chr_ioctl) { - error_report("Replay: ioctl is not supported " - "for serial devices yet"); - } - replay_register_char_driver(chr); - } - return chr; -} - -void qemu_chr_fe_set_echo(CharBackend *be, bool echo) -{ - Chardev *chr = be->chr; - - if (chr && CHARDEV_GET_CLASS(chr)->chr_set_echo) { - CHARDEV_GET_CLASS(chr)->chr_set_echo(chr, echo); - } -} - -void qemu_chr_fe_set_open(CharBackend *be, int fe_open) -{ - Chardev *chr = be->chr; - - if (!chr) { - return; - } - - if (be->fe_open == fe_open) { - return; - } - be->fe_open = fe_open; - if (CHARDEV_GET_CLASS(chr)->chr_set_fe_open) { - CHARDEV_GET_CLASS(chr)->chr_set_fe_open(chr, fe_open); - } -} - -guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond, - GIOFunc func, void *user_data) -{ - Chardev *s = be->chr; - GSource *src; - guint tag; - - if (!s || CHARDEV_GET_CLASS(s)->chr_add_watch == NULL) { - return 0; - } - - src = CHARDEV_GET_CLASS(s)->chr_add_watch(s, cond); - if (!src) { - return 0; - } - - g_source_set_callback(src, (GSourceFunc)func, user_data, NULL); - tag = g_source_attach(src, NULL); - g_source_unref(src); - - return tag; -} - -void qemu_chr_fe_disconnect(CharBackend *be) -{ - Chardev *chr = be->chr; - - if (chr && CHARDEV_GET_CLASS(chr)->chr_disconnect) { - CHARDEV_GET_CLASS(chr)->chr_disconnect(chr); - } -} - -void qemu_chr_free(Chardev *chr) -{ - if (CHARDEV_GET_CLASS(chr)->chr_free) { - CHARDEV_GET_CLASS(chr)->chr_free(chr); - } - object_unref(OBJECT(chr)); -} - -void qemu_chr_delete(Chardev *chr) -{ - QTAILQ_REMOVE(&chardevs, chr, next); - qemu_chr_free(chr); -} - -ChardevInfoList *qmp_query_chardev(Error **errp) -{ - ChardevInfoList *chr_list = NULL; - Chardev *chr; - - QTAILQ_FOREACH(chr, &chardevs, next) { - ChardevInfoList *info = g_malloc0(sizeof(*info)); - info->value = g_malloc0(sizeof(*info->value)); - info->value->label = g_strdup(chr->label); - info->value->filename = g_strdup(chr->filename); - info->value->frontend_open = chr->be && chr->be->fe_open; - - info->next = chr_list; - chr_list = info; - } - - return chr_list; -} - -static ChardevBackendInfoList * -qmp_prepend_backend(ChardevBackendInfoList *list, const char *name) -{ - ChardevBackendInfoList *info = g_malloc0(sizeof(*info)); - info->value = g_malloc0(sizeof(*info->value)); - info->value->name = g_strdup(name); - info->next = list; - return info; -} - -ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp) -{ - ChardevBackendInfoList *backend_list = NULL; - const CharDriver *c; - int i; - - for (i = 0; i < ARRAY_SIZE(backends); i++) { - c = backends[i]; - if (!c) { - continue; - } - - backend_list = qmp_prepend_backend(backend_list, - ChardevBackendKind_lookup[c->kind]); - if (c->alias) { - backend_list = qmp_prepend_backend(backend_list, c->alias); - } - } - - return backend_list; -} - -Chardev *qemu_chr_find(const char *name) -{ - Chardev *chr; - - QTAILQ_FOREACH(chr, &chardevs, next) { - if (strcmp(chr->label, name) != 0) - continue; - return chr; - } - return NULL; -} - -QemuOptsList qemu_chardev_opts = { - .name = "chardev", - .implied_opt_name = "backend", - .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head), - .desc = { - { - .name = "backend", - .type = QEMU_OPT_STRING, - },{ - .name = "path", - .type = QEMU_OPT_STRING, - },{ - .name = "host", - .type = QEMU_OPT_STRING, - },{ - .name = "port", - .type = QEMU_OPT_STRING, - },{ - .name = "localaddr", - .type = QEMU_OPT_STRING, - },{ - .name = "localport", - .type = QEMU_OPT_STRING, - },{ - .name = "to", - .type = QEMU_OPT_NUMBER, - },{ - .name = "ipv4", - .type = QEMU_OPT_BOOL, - },{ - .name = "ipv6", - .type = QEMU_OPT_BOOL, - },{ - .name = "wait", - .type = QEMU_OPT_BOOL, - },{ - .name = "server", - .type = QEMU_OPT_BOOL, - },{ - .name = "delay", - .type = QEMU_OPT_BOOL, - },{ - .name = "reconnect", - .type = QEMU_OPT_NUMBER, - },{ - .name = "telnet", - .type = QEMU_OPT_BOOL, - },{ - .name = "tls-creds", - .type = QEMU_OPT_STRING, - },{ - .name = "width", - .type = QEMU_OPT_NUMBER, - },{ - .name = "height", - .type = QEMU_OPT_NUMBER, - },{ - .name = "cols", - .type = QEMU_OPT_NUMBER, - },{ - .name = "rows", - .type = QEMU_OPT_NUMBER, - },{ - .name = "mux", - .type = QEMU_OPT_BOOL, - },{ - .name = "signal", - .type = QEMU_OPT_BOOL, - },{ - .name = "name", - .type = QEMU_OPT_STRING, - },{ - .name = "debug", - .type = QEMU_OPT_NUMBER, - },{ - .name = "size", - .type = QEMU_OPT_SIZE, - },{ - .name = "chardev", - .type = QEMU_OPT_STRING, - },{ - .name = "append", - .type = QEMU_OPT_BOOL, - },{ - .name = "logfile", - .type = QEMU_OPT_STRING, - },{ - .name = "logappend", - .type = QEMU_OPT_BOOL, - }, - { /* end of list */ } - }, -}; - -#ifdef _WIN32 - -static void qmp_chardev_open_file(Chardev *chr, - ChardevBackend *backend, - bool *be_opened, - Error **errp) -{ - ChardevFile *file = backend->u.file.data; - HANDLE out; - DWORD accessmode; - DWORD flags; - - if (file->has_in) { - error_setg(errp, "input file not supported"); - return; - } - - if (file->has_append && file->append) { - /* Append to file if it already exists. */ - accessmode = FILE_GENERIC_WRITE & ~FILE_WRITE_DATA; - flags = OPEN_ALWAYS; - } else { - /* Truncate file if it already exists. */ - accessmode = GENERIC_WRITE; - flags = CREATE_ALWAYS; - } - - out = CreateFile(file->out, accessmode, FILE_SHARE_READ, NULL, flags, - FILE_ATTRIBUTE_NORMAL, NULL); - if (out == INVALID_HANDLE_VALUE) { - error_setg(errp, "open %s failed", file->out); - return; - } - - qemu_chr_open_win_file(chr, out); -} - -static void qmp_chardev_open_serial(Chardev *chr, - ChardevBackend *backend, - bool *be_opened, - Error **errp) -{ - ChardevHostdev *serial = backend->u.serial.data; - - win_chr_init(chr, serial->device, errp); -} - -#else /* WIN32 */ - -static int qmp_chardev_open_file_source(char *src, int flags, - Error **errp) -{ - int fd = -1; - - TFR(fd = qemu_open(src, flags, 0666)); - if (fd == -1) { - error_setg_file_open(errp, errno, src); - } - return fd; -} - -static void qmp_chardev_open_file(Chardev *chr, - ChardevBackend *backend, - bool *be_opened, - Error **errp) -{ - ChardevFile *file = backend->u.file.data; - int flags, in = -1, out; - - flags = O_WRONLY | O_CREAT | O_BINARY; - if (file->has_append && file->append) { - flags |= O_APPEND; - } else { - flags |= O_TRUNC; - } - - out = qmp_chardev_open_file_source(file->out, flags, errp); - if (out < 0) { - return; - } - - if (file->has_in) { - flags = O_RDONLY; - in = qmp_chardev_open_file_source(file->in, flags, errp); - if (in < 0) { - qemu_close(out); - return; - } - } - - qemu_chr_open_fd(chr, in, out); -} - -#ifdef HAVE_CHARDEV_SERIAL -static void qmp_chardev_open_serial(Chardev *chr, - ChardevBackend *backend, - bool *be_opened, - Error **errp) -{ - ChardevHostdev *serial = backend->u.serial.data; - int fd; - - fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp); - if (fd < 0) { - return; - } - qemu_set_nonblock(fd); - tty_serial_init(fd, 115200, 'N', 8, 1); - - qemu_chr_open_fd(chr, fd, fd); -} -#endif - -#ifdef HAVE_CHARDEV_PARPORT -static void qmp_chardev_open_parallel(Chardev *chr, - ChardevBackend *backend, - bool *be_opened, - Error **errp) -{ - ChardevHostdev *parallel = backend->u.parallel.data; - int fd; - - fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp); - if (fd < 0) { - return; - } - qemu_chr_open_pp_fd(chr, fd, be_opened, errp); -} - -static const CharDriver parallel_driver = { - .kind = CHARDEV_BACKEND_KIND_PARALLEL, - .alias = "parport", - .parse = qemu_chr_parse_parallel, -}; - -static void char_parallel_class_init(ObjectClass *oc, void *data) -{ - ChardevClass *cc = CHARDEV_CLASS(oc); - - cc->open = qmp_chardev_open_parallel; -#if defined(__linux__) - cc->chr_write = null_chr_write; - cc->chr_ioctl = pp_ioctl; - cc->chr_free = pp_free; -#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) - /* FIXME: no chr_free */ - cc->chr_write = null_chr_write; - cc->chr_ioctl = pp_ioctl; -#endif -} - -static const TypeInfo char_parallel_type_info = { - .name = TYPE_CHARDEV_PARALLEL, - .parent = TYPE_CHARDEV, - .instance_size = sizeof(ParallelChardev), - .class_init = char_parallel_class_init, -}; -#endif - -#endif /* WIN32 */ - -static const CharDriver file_driver = { - .kind = CHARDEV_BACKEND_KIND_FILE, - .parse = qemu_chr_parse_file_out, -}; - -static void char_file_class_init(ObjectClass *oc, void *data) -{ - ChardevClass *cc = CHARDEV_CLASS(oc); - - cc->open = qmp_chardev_open_file; -#ifdef _WIN32 - /* FIXME: no chr_free */ - cc->chr_free = NULL; -#endif -} - -static const TypeInfo char_file_type_info = { - .name = TYPE_CHARDEV_FILE, -#ifdef _WIN32 - .parent = TYPE_CHARDEV_WIN, -#else - .parent = TYPE_CHARDEV_FD, -#endif - .class_init = char_file_class_init, -}; - -#ifdef HAVE_CHARDEV_SERIAL - -static const CharDriver serial_driver = { - .kind = CHARDEV_BACKEND_KIND_SERIAL, - .alias = "tty", - .parse = qemu_chr_parse_serial, -}; - -static void char_serial_class_init(ObjectClass *oc, void *data) -{ - ChardevClass *cc = CHARDEV_CLASS(oc); - - cc->open = qmp_chardev_open_serial; -#ifndef _WIN32 - cc->chr_ioctl = tty_serial_ioctl; - cc->chr_free = qemu_chr_free_tty; -#endif -} - -static const TypeInfo char_serial_type_info = { - .name = TYPE_CHARDEV_SERIAL, -#ifdef _WIN32 - .parent = TYPE_CHARDEV_WIN, -#else - .parent = TYPE_CHARDEV_FD, -#endif - .class_init = char_serial_class_init, -}; -#endif - -static gboolean socket_reconnect_timeout(gpointer opaque) -{ - Chardev *chr = CHARDEV(opaque); - SocketChardev *s = SOCKET_CHARDEV(opaque); - QIOChannelSocket *sioc; - - s->reconnect_timer = 0; - - if (chr->be_open) { - return false; - } - - sioc = qio_channel_socket_new(); - tcp_chr_set_client_ioc_name(chr, sioc); - qio_channel_socket_connect_async(sioc, s->addr, - qemu_chr_socket_connected, - chr, NULL); - - return false; -} - -static void qmp_chardev_open_socket(Chardev *chr, - ChardevBackend *backend, - bool *be_opened, - Error **errp) -{ - SocketChardev *s = SOCKET_CHARDEV(chr); - ChardevSocket *sock = backend->u.socket.data; - SocketAddress *addr = sock->addr; - bool do_nodelay = sock->has_nodelay ? sock->nodelay : false; - bool is_listen = sock->has_server ? sock->server : true; - bool is_telnet = sock->has_telnet ? sock->telnet : false; - bool is_waitconnect = sock->has_wait ? sock->wait : false; - int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0; - QIOChannelSocket *sioc = NULL; - - s->is_unix = addr->type == SOCKET_ADDRESS_KIND_UNIX; - s->is_listen = is_listen; - s->is_telnet = is_telnet; - s->do_nodelay = do_nodelay; - if (sock->tls_creds) { - Object *creds; - creds = object_resolve_path_component( - object_get_objects_root(), sock->tls_creds); - if (!creds) { - error_setg(errp, "No TLS credentials with id '%s'", - sock->tls_creds); - goto error; - } - s->tls_creds = (QCryptoTLSCreds *) - object_dynamic_cast(creds, - TYPE_QCRYPTO_TLS_CREDS); - if (!s->tls_creds) { - error_setg(errp, "Object with id '%s' is not TLS credentials", - sock->tls_creds); - goto error; - } - object_ref(OBJECT(s->tls_creds)); - if (is_listen) { - if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { - error_setg(errp, "%s", - "Expected TLS credentials for server endpoint"); - goto error; - } - } else { - if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) { - error_setg(errp, "%s", - "Expected TLS credentials for client endpoint"); - goto error; - } - } - } - - s->addr = QAPI_CLONE(SocketAddress, sock->addr); - - qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE); - if (s->is_unix) { - qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_FD_PASS); - } - - /* be isn't opened until we get a connection */ - *be_opened = false; - - chr->filename = SocketAddress_to_str("disconnected:", - addr, is_listen, is_telnet); - - if (is_listen) { - if (is_telnet) { - s->do_telnetopt = 1; - } - } else if (reconnect > 0) { - s->reconnect_time = reconnect; - } - - if (s->reconnect_time) { - sioc = qio_channel_socket_new(); - tcp_chr_set_client_ioc_name(chr, sioc); - qio_channel_socket_connect_async(sioc, s->addr, - qemu_chr_socket_connected, - chr, NULL); - } else { - if (s->is_listen) { - char *name; - sioc = qio_channel_socket_new(); - - name = g_strdup_printf("chardev-tcp-listener-%s", chr->label); - qio_channel_set_name(QIO_CHANNEL(sioc), name); - g_free(name); - - if (qio_channel_socket_listen_sync(sioc, s->addr, errp) < 0) { - goto error; - } - s->listen_ioc = sioc; - if (is_waitconnect && - qemu_chr_wait_connected(chr, errp) < 0) { - goto error; - } - if (!s->ioc) { - s->listen_tag = qio_channel_add_watch( - QIO_CHANNEL(s->listen_ioc), G_IO_IN, - tcp_chr_accept, chr, NULL); - } - } else if (qemu_chr_wait_connected(chr, errp) < 0) { - goto error; - } - } - - return; - -error: - if (sioc) { - object_unref(OBJECT(sioc)); - } - if (s->tls_creds) { - object_unref(OBJECT(s->tls_creds)); - } -} - -static const CharDriver socket_driver = { - .kind = CHARDEV_BACKEND_KIND_SOCKET, - .parse = qemu_chr_parse_socket, -}; - -static void char_socket_class_init(ObjectClass *oc, void *data) -{ - ChardevClass *cc = CHARDEV_CLASS(oc); - - cc->open = qmp_chardev_open_socket; - cc->chr_wait_connected = tcp_chr_wait_connected; - cc->chr_write = tcp_chr_write; - cc->chr_sync_read = tcp_chr_sync_read; - cc->chr_disconnect = tcp_chr_disconnect; - cc->get_msgfds = tcp_get_msgfds; - cc->set_msgfds = tcp_set_msgfds; - cc->chr_add_client = tcp_chr_add_client; - cc->chr_add_watch = tcp_chr_add_watch; - cc->chr_update_read_handler = tcp_chr_update_read_handler; - cc->chr_free = tcp_chr_free; -} - -static const TypeInfo char_socket_type_info = { - .name = TYPE_CHARDEV_SOCKET, - .parent = TYPE_CHARDEV, - .instance_size = sizeof(SocketChardev), - .class_init = char_socket_class_init, -}; - -static void qmp_chardev_open_udp(Chardev *chr, - ChardevBackend *backend, - bool *be_opened, - Error **errp) -{ - ChardevUdp *udp = backend->u.udp.data; - QIOChannelSocket *sioc = qio_channel_socket_new(); - char *name; - UdpChardev *s = UDP_CHARDEV(chr); - - if (qio_channel_socket_dgram_sync(sioc, - udp->local, udp->remote, - errp) < 0) { - object_unref(OBJECT(sioc)); - return; - } - - name = g_strdup_printf("chardev-udp-%s", chr->label); - qio_channel_set_name(QIO_CHANNEL(sioc), name); - g_free(name); - - s->ioc = QIO_CHANNEL(sioc); - /* be isn't opened until we get a connection */ - *be_opened = false; -} - -static const CharDriver udp_driver = { - .kind = CHARDEV_BACKEND_KIND_UDP, - .parse = qemu_chr_parse_udp, -}; - -static void char_udp_class_init(ObjectClass *oc, void *data) -{ - ChardevClass *cc = CHARDEV_CLASS(oc); - - cc->open = qmp_chardev_open_udp; - cc->chr_write = udp_chr_write; - cc->chr_update_read_handler = udp_chr_update_read_handler; - cc->chr_free = udp_chr_free; -} - -static const TypeInfo char_udp_type_info = { - .name = TYPE_CHARDEV_UDP, - .parent = TYPE_CHARDEV, - .instance_size = sizeof(UdpChardev), - .class_init = char_udp_class_init, -}; - -bool qemu_chr_has_feature(Chardev *chr, - CharDriverFeature feature) -{ - return test_bit(feature, chr->features); -} - -void qemu_chr_set_feature(Chardev *chr, - CharDriverFeature feature) -{ - return set_bit(feature, chr->features); -} - -static const ChardevClass *char_get_class(const char *driver, Error **errp) -{ - ObjectClass *oc; - const ChardevClass *cc; - char *typename = g_strdup_printf("chardev-%s", driver); - - oc = object_class_by_name(typename); - g_free(typename); - - if (!object_class_dynamic_cast(oc, TYPE_CHARDEV)) { - error_setg(errp, "'%s' is not a valid char driver name", driver); - return NULL; - } - - if (object_class_is_abstract(oc)) { - error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver", - "abstract device type"); - return NULL; - } - - cc = CHARDEV_CLASS(oc); - if (cc->internal) { - error_setg(errp, "'%s' is not a valid char driver name", driver); - return NULL; - } - - return cc; -} - -Chardev *qemu_chardev_new(const char *id, const char *typename, - ChardevBackend *backend, Error **errp) -{ - Chardev *chr = NULL; - Error *local_err = NULL; - bool be_opened = true; - - assert(g_str_has_prefix(typename, "chardev-")); - - chr = CHARDEV(object_new(typename)); - chr->label = g_strdup(id); - - qemu_char_open(chr, backend, &be_opened, &local_err); - if (local_err) { - error_propagate(errp, local_err); - object_unref(OBJECT(chr)); - return NULL; - } - - if (!chr->filename) { - chr->filename = g_strdup(typename + 8); - } - if (be_opened) { - qemu_chr_be_event(chr, CHR_EVENT_OPENED); - } - - return chr; -} - -ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, - Error **errp) -{ - const ChardevClass *cc; - ChardevReturn *ret; - Chardev *chr; - - chr = qemu_chr_find(id); - if (chr) { - error_setg(errp, "Chardev '%s' already exists", id); - return NULL; - } - - cc = char_get_class(ChardevBackendKind_lookup[backend->type], errp); - if (!cc) { - return NULL; - } - - chr = qemu_chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)), - backend, errp); - if (!chr) { - return NULL; - } - - ret = g_new0(ChardevReturn, 1); - if (CHARDEV_IS_PTY(chr)) { - ret->pty = g_strdup(chr->filename + 4); - ret->has_pty = true; - } - - QTAILQ_INSERT_TAIL(&chardevs, chr, next); - return ret; -} - -void qmp_chardev_remove(const char *id, Error **errp) -{ - Chardev *chr; - - chr = qemu_chr_find(id); - if (chr == NULL) { - error_setg(errp, "Chardev '%s' not found", id); - return; - } - if (qemu_chr_is_busy(chr)) { - error_setg(errp, "Chardev '%s' is busy", id); - return; - } - if (qemu_chr_replay(chr)) { - error_setg(errp, - "Chardev '%s' cannot be unplugged in record/replay mode", id); - return; - } - qemu_chr_delete(chr); -} - -void qemu_chr_cleanup(void) -{ - Chardev *chr, *tmp; - - QTAILQ_FOREACH_SAFE(chr, &chardevs, next, tmp) { - qemu_chr_delete(chr); - } -} - -static void register_types(void) -{ - static const struct { - const CharDriver *driver; - const TypeInfo *type; - } chardevs[] = { - { &null_driver, &char_null_type_info }, - { &socket_driver, &char_socket_type_info }, - { &udp_driver, &char_udp_type_info }, - { &ringbuf_driver, &char_ringbuf_type_info }, - { &file_driver, &char_file_type_info }, - { &stdio_driver, &char_stdio_type_info }, -#ifdef HAVE_CHARDEV_SERIAL - { &serial_driver, &char_serial_type_info }, -#endif -#ifdef HAVE_CHARDEV_PARPORT - { ¶llel_driver, &char_parallel_type_info }, -#endif -#ifdef HAVE_CHARDEV_PTY - { &pty_driver, &char_pty_type_info }, -#endif -#ifdef _WIN32 - { &console_driver, &char_console_type_info }, -#endif - { &pipe_driver, &char_pipe_type_info }, - { &mux_driver, &char_mux_type_info }, - { &memory_driver, &char_memory_type_info } - }; - int i; - - type_register_static(&char_type_info); -#ifndef _WIN32 - type_register_static(&char_fd_type_info); -#else - type_register_static(&char_win_type_info); - type_register_static(&char_win_stdio_type_info); -#endif - for (i = 0; i < ARRAY_SIZE(chardevs); i++) { - type_register_static(chardevs[i].type); - register_char_driver(chardevs[i].driver); - } - - /* this must be done after machine init, since we register FEs with muxes - * as part of realize functions like serial_isa_realizefn when -nographic - * is specified - */ - qemu_add_machine_init_done_notifier(&muxes_realize_notify); -} - -type_init(register_types); @@ -18,6 +18,7 @@ #include "qemu/cutils.h" #include "monitor/monitor.h" #include "sysemu/sysemu.h" +#include "qemu/config-file.h" #include "qemu/uuid.h" #include "qmp-commands.h" #include "sysemu/char.h" @@ -29,7 +29,7 @@ #include "qemu/error-report.h" #include "sysemu/sysemu.h" #include "hw/qdev-properties.h" -#include "trace.h" +#include "trace-root.h" bool cpu_exists(int64_t id) { @@ -26,8 +26,13 @@ QEMU_CXXFLAGS = -D__STDC_LIMIT_MACROS $(filter-out -Wstrict-prototypes -Wmissing # Flags for dependency generation QEMU_DGFLAGS += -MMD -MP -MT $@ -MF $(@D)/$(*F).d -# Same as -I$(SRC_PATH) -I., but for the nested source/object directories -QEMU_INCLUDES += -I$(<D) -I$(@D) +# Compiler searches the source file dir first, but in vpath builds +# we need to make it search the build dir too, before any other +# explicit search paths. There are two search locations in the build +# dir, one absolute and the other relative to the compiler working +# directory. These are the same for target-independent files, but +# different for target-dependent ones. +QEMU_LOCAL_INCLUDES = -I$(BUILD_DIR)/$(@D) -I$(@D) WL_U := -Wl,-u, find-symbols = $(if $1, $(sort $(shell $(NM) -P -g $1 | $2))) @@ -61,7 +66,9 @@ expand-objs = $(strip $(sort $(filter %.o,$1)) \ $(filter-out %.o %.mo,$1)) %.o: %.c - $(call quiet-command,$(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<,"CC","$(TARGET_DIR)$@") + $(call quiet-command,$(CC) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \ + $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) \ + -c -o $@ $<,"CC","$(TARGET_DIR)$@") %.o: %.rc $(call quiet-command,$(WINDRES) -I. -o $@ $<,"RC","$(TARGET_DIR)$@") @@ -74,16 +81,24 @@ LINK = $(call quiet-command, $(LINKPROG) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $(version-obj-y) $(call extract-libs,$1) $(LIBS),"LINK","$(TARGET_DIR)$@") %.o: %.S - $(call quiet-command,$(CCAS) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -c -o $@ $<,"CCAS","$(TARGET_DIR)$@") + $(call quiet-command,$(CCAS) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \ + $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) \ + -c -o $@ $<,"CCAS","$(TARGET_DIR)$@") %.o: %.cc - $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<,"CXX","$(TARGET_DIR)$@") + $(call quiet-command,$(CXX) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \ + $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) \ + -c -o $@ $<,"CXX","$(TARGET_DIR)$@") %.o: %.cpp - $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<,"CXX","$(TARGET_DIR)$@") + $(call quiet-command,$(CXX) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \ + $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) \ + -c -o $@ $<,"CXX","$(TARGET_DIR)$@") %.o: %.m - $(call quiet-command,$(OBJCC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<,"OBJC","$(TARGET_DIR)$@") + $(call quiet-command,$(OBJCC) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \ + $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) \ + -c -o $@ $<,"OBJC","$(TARGET_DIR)$@") %.o: %.dtrace $(call quiet-command,dtrace -o $@ -G -s $<,"GEN","$(TARGET_DIR)$@") @@ -359,6 +374,7 @@ define unnest-vars $(eval $(o:%.mo=%$(DSOSUF)): module-common.o $($o-objs)), $(error $o added in $v but $o-objs is not set))) $(shell mkdir -p ./ $(sort $(dir $($v)))) + $(shell cd $(BUILD_DIR) && mkdir -p ./ $(sort $(dir $($v)))) # Include all the .d files $(eval -include $(patsubst %.o,%.d,$(patsubst %.mo,%.d,$($v)))) $(eval $v := $(filter-out %/,$($v)))) diff --git a/scripts/simpletrace.py b/scripts/simpletrace.py index 4ca903dc0c..4c990047b6 100755 --- a/scripts/simpletrace.py +++ b/scripts/simpletrace.py @@ -73,10 +73,14 @@ def read_record(edict, idtoname, fobj): def read_trace_header(fobj): """Read and verify trace file header""" header = read_header(fobj, log_header_fmt) - if header is None or \ - header[0] != header_event_id or \ - header[1] != header_magic: + if header is None: raise ValueError('Not a valid trace file!') + if header[0] != header_event_id: + raise ValueError('Not a valid trace file, header id %d != %d' % + (header[0], header_event_id)) + if header[1] != header_magic: + raise ValueError('Not a valid trace file, header magic %d != %d' % + (header[1], header_magic)) log_version = header[2] if log_version not in [0, 2, 3, 4]: diff --git a/scripts/tracetool.py b/scripts/tracetool.py index c9e47371d3..c55a21518b 100755 --- a/scripts/tracetool.py +++ b/scripts/tracetool.py @@ -49,6 +49,7 @@ Options: --binary <path> Full path to QEMU binary. --target-type <type> QEMU emulator target type ('system' or 'user'). --target-name <name> QEMU emulator target name. + --group <name> Name of the event group --probe-prefix <prefix> Prefix for dtrace probe names (default: qemu-<target-type>-<target-name>).\ """ % { @@ -62,22 +63,12 @@ Options: else: sys.exit(1) -def make_group_name(filename): - dirname = os.path.realpath(os.path.dirname(filename)) - basedir = os.path.join(os.path.dirname(__file__), os.pardir) - basedir = os.path.realpath(os.path.abspath(basedir)) - dirname = dirname[len(basedir) + 1:] - - if dirname == "": - return "common" - return "_" + re.sub(r"[^A-Za-z0-9]", "_", dirname) - def main(args): global _SCRIPT _SCRIPT = args[0] long_opts = ["backends=", "format=", "help", "list-backends", - "check-backends"] + "check-backends", "group="] long_opts += ["binary=", "target-type=", "target-name=", "probe-prefix="] try: @@ -88,6 +79,7 @@ def main(args): check_backends = False arg_backends = [] arg_format = "" + arg_group = None binary = None target_type = None target_name = None @@ -98,6 +90,8 @@ def main(args): elif opt == "--backends": arg_backends = arg.split(",") + elif opt == "--group": + arg_group = arg elif opt == "--format": arg_format = arg @@ -129,6 +123,9 @@ def main(args): sys.exit(1) sys.exit(0) + if arg_group is None: + error_opt("group name is required") + if arg_format == "stap": if binary is None: error_opt("--binary is required for SystemTAP tapset generator") @@ -140,15 +137,15 @@ def main(args): if probe_prefix is None: probe_prefix = ".".join(["qemu", target_type, target_name]) - if len(args) != 1: + if len(args) < 1: error_opt("missing trace-events filepath") - with open(args[0], "r") as fh: - events = tracetool.read_events(fh) - - group = make_group_name(args[0]) + events = [] + for arg in args: + with open(arg, "r") as fh: + events.extend(tracetool.read_events(fh)) try: - tracetool.generate(events, group, arg_format, arg_backends, + tracetool.generate(events, arg_group, arg_format, arg_backends, binary=binary, probe_prefix=probe_prefix) except tracetool.TracetoolError as e: error_opt(str(e)) diff --git a/scripts/tracetool/backend/dtrace.py b/scripts/tracetool/backend/dtrace.py index 79505c6b1a..c469cbd1a3 100644 --- a/scripts/tracetool/backend/dtrace.py +++ b/scripts/tracetool/backend/dtrace.py @@ -36,7 +36,12 @@ def binary(): def generate_h_begin(events, group): - out('#include "trace/generated-tracers-dtrace.h"', + if group == "root": + header = "trace-dtrace-root.h" + else: + header = "trace-dtrace.h" + + out('#include "%s"' % header, '') diff --git a/scripts/tracetool/backend/simple.py b/scripts/tracetool/backend/simple.py index 85f61028e2..4acc06e81c 100644 --- a/scripts/tracetool/backend/simple.py +++ b/scripts/tracetool/backend/simple.py @@ -44,7 +44,6 @@ def generate_h(event, group): def generate_c_begin(events, group): out('#include "qemu/osdep.h"', - '#include "trace.h"', '#include "trace/control.h"', '#include "trace/simple.h"', '') diff --git a/scripts/tracetool/backend/ust.py b/scripts/tracetool/backend/ust.py index 4594db6128..52ce892478 100644 --- a/scripts/tracetool/backend/ust.py +++ b/scripts/tracetool/backend/ust.py @@ -20,8 +20,13 @@ PUBLIC = True def generate_h_begin(events, group): + if group == "root": + header = "trace-ust-root.h" + else: + header = "trace-ust.h" + out('#include <lttng/tracepoint.h>', - '#include "trace/generated-ust-provider.h"', + '#include "%s"' % header, '') diff --git a/scripts/tracetool/format/c.py b/scripts/tracetool/format/c.py index 47115ed8af..833c05a022 100644 --- a/scripts/tracetool/format/c.py +++ b/scripts/tracetool/format/c.py @@ -20,10 +20,15 @@ def generate(events, backend, group): active_events = [e for e in events if "disable" not in e.properties] + if group == "root": + header = "trace-root.h" + else: + header = "trace.h" + out('/* This file is autogenerated by tracetool, do not edit. */', '', '#include "qemu/osdep.h"', - '#include "trace.h"', + '#include "%s"' % header, '') for e in events: diff --git a/scripts/tracetool/format/tcg_h.py b/scripts/tracetool/format/tcg_h.py index 5f213f6cba..7ddc4a52ce 100644 --- a/scripts/tracetool/format/tcg_h.py +++ b/scripts/tracetool/format/tcg_h.py @@ -28,13 +28,17 @@ def vcpu_transform_args(args): def generate(events, backend, group): + if group == "root": + header = "trace-root.h" + else: + header = "trace.h" + out('/* This file is autogenerated by tracetool, do not edit. */', '/* You must include this file after the inclusion of helper.h */', '', '#ifndef TRACE_%s_GENERATED_TCG_TRACERS_H' % group.upper(), '#define TRACE_%s_GENERATED_TCG_TRACERS_H' % group.upper(), '', - '#include "trace.h"', '#include "exec/helper-proto.h"', '', ) diff --git a/scripts/tracetool/format/tcg_helper_c.py b/scripts/tracetool/format/tcg_helper_c.py index cc26e03008..7dccd8c5ec 100644 --- a/scripts/tracetool/format/tcg_helper_c.py +++ b/scripts/tracetool/format/tcg_helper_c.py @@ -41,6 +41,11 @@ def vcpu_transform_args(args, mode): def generate(events, backend, group): + if group == "root": + header = "trace-root.h" + else: + header = "trace.h" + events = [e for e in events if "disable" not in e.properties] @@ -49,7 +54,6 @@ def generate(events, backend, group): '#include "qemu/osdep.h"', '#include "qemu-common.h"', '#include "cpu.h"', - '#include "trace.h"', '#include "exec/helper-proto.h"', '', ) diff --git a/scripts/tracetool/format/ust_events_c.py b/scripts/tracetool/format/ust_events_c.py index cd87d8ab8f..264784cdf2 100644 --- a/scripts/tracetool/format/ust_events_c.py +++ b/scripts/tracetool/format/ust_events_c.py @@ -32,4 +32,4 @@ def generate(events, backend, group): ' */', '#pragma GCC diagnostic ignored "-Wredundant-decls"', '', - '#include "generated-ust-provider.h"') + '#include "trace-ust-all.h"') diff --git a/scripts/tracetool/format/ust_events_h.py b/scripts/tracetool/format/ust_events_h.py index d853155d21..514294c2cc 100644 --- a/scripts/tracetool/format/ust_events_h.py +++ b/scripts/tracetool/format/ust_events_h.py @@ -20,13 +20,18 @@ def generate(events, backend, group): events = [e for e in events if "disabled" not in e.properties] + if group == "all": + include = "trace-ust-all.h" + else: + include = "trace-ust.h" + out('/* This file is autogenerated by tracetool, do not edit. */', '', '#undef TRACEPOINT_PROVIDER', '#define TRACEPOINT_PROVIDER qemu', '', '#undef TRACEPOINT_INCLUDE_FILE', - '#define TRACEPOINT_INCLUDE_FILE ./generated-ust-provider.h', + '#define TRACEPOINT_INCLUDE_FILE ./%s' % include, '', '#if !defined (TRACE_%s_GENERATED_UST_H) || \\' % group.upper(), ' defined(TRACEPOINT_HEADER_MULTI_READ)', diff --git a/spice-qemu-char.c b/spice-qemu-char.c index dd97c17fca..6f46f46b25 100644 --- a/spice-qemu-char.c +++ b/spice-qemu-char.c @@ -1,5 +1,5 @@ #include "qemu/osdep.h" -#include "trace.h" +#include "trace-root.h" #include "ui/qemu-spice.h" #include "sysemu/char.h" #include "qemu/error-report.h" @@ -210,9 +210,9 @@ static int spice_chr_write(Chardev *chr, const uint8_t *buf, int len) return read_bytes; } -static void spice_chr_free(struct Chardev *chr) +static void char_spice_finalize(Object *obj) { - SpiceChardev *s = SPICE_CHARDEV(chr); + SpiceChardev *s = SPICE_CHARDEV(obj); vmc_unregister_interface(s); QLIST_REMOVE(s, next); @@ -338,6 +338,7 @@ static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend, error_setg(errp, "chardev: spice channel: no name given"); return; } + backend->type = CHARDEV_BACKEND_KIND_SPICEVMC; spicevmc = backend->u.spicevmc.data = g_new0(ChardevSpiceChannel, 1); qemu_chr_parse_common(opts, qapi_ChardevSpiceChannel_base(spicevmc)); spicevmc->type = g_strdup(name); @@ -353,6 +354,7 @@ static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend, error_setg(errp, "chardev: spice port: no name given"); return; } + backend->type = CHARDEV_BACKEND_KIND_SPICEPORT; spiceport = backend->u.spiceport.data = g_new0(ChardevSpicePort, 1); qemu_chr_parse_common(opts, qapi_ChardevSpicePort_base(spiceport)); spiceport->fqdn = g_strdup(name); @@ -365,13 +367,13 @@ static void char_spice_class_init(ObjectClass *oc, void *data) cc->chr_write = spice_chr_write; cc->chr_add_watch = spice_chr_add_watch; cc->chr_accept_input = spice_chr_accept_input; - cc->chr_free = spice_chr_free; } static const TypeInfo char_spice_type_info = { .name = TYPE_CHARDEV_SPICE, .parent = TYPE_CHARDEV, .instance_size = sizeof(SpiceChardev), + .instance_finalize = char_spice_finalize, .class_init = char_spice_class_init, .abstract = true, }; @@ -380,6 +382,7 @@ static void char_spicevmc_class_init(ObjectClass *oc, void *data) { ChardevClass *cc = CHARDEV_CLASS(oc); + cc->parse = qemu_chr_parse_spice_vmc; cc->open = qemu_chr_open_spice_vmc; cc->chr_set_fe_open = spice_vmc_set_fe_open; } @@ -394,6 +397,7 @@ static void char_spiceport_class_init(ObjectClass *oc, void *data) { ChardevClass *cc = CHARDEV_CLASS(oc); + cc->parse = qemu_chr_parse_spice_port; cc->open = qemu_chr_open_spice_port; cc->chr_set_fe_open = spice_port_set_fe_open; } @@ -406,17 +410,6 @@ static const TypeInfo char_spiceport_type_info = { static void register_types(void) { - static const CharDriver vmc_driver = { - .kind = CHARDEV_BACKEND_KIND_SPICEVMC, - .parse = qemu_chr_parse_spice_vmc, - }; - static const CharDriver port_driver = { - .kind = CHARDEV_BACKEND_KIND_SPICEPORT, - .parse = qemu_chr_parse_spice_port, - }; - register_char_driver(&vmc_driver); - register_char_driver(&port_driver); - type_register_static(&char_spice_type_info); type_register_static(&char_spicevmc_type_info); type_register_static(&char_spiceport_type_info); diff --git a/target/arm/kvm-consts.h b/target/arm/kvm-consts.h index a2c9518592..aad28258a3 100644 --- a/target/arm/kvm-consts.h +++ b/target/arm/kvm-consts.h @@ -21,7 +21,9 @@ #define MISMATCH_CHECK(X, Y) QEMU_BUILD_BUG_ON(X != Y) #else -#define MISMATCH_CHECK(X, Y) + +#define MISMATCH_CHECK(X, Y) QEMU_BUILD_BUG_ON(0) + #endif #define CP_REG_SIZE_SHIFT 52 @@ -31,12 +33,12 @@ #define CP_REG_ARM 0x4000000000000000ULL #define CP_REG_ARCH_MASK 0xff00000000000000ULL -MISMATCH_CHECK(CP_REG_SIZE_SHIFT, KVM_REG_SIZE_SHIFT) -MISMATCH_CHECK(CP_REG_SIZE_MASK, KVM_REG_SIZE_MASK) -MISMATCH_CHECK(CP_REG_SIZE_U32, KVM_REG_SIZE_U32) -MISMATCH_CHECK(CP_REG_SIZE_U64, KVM_REG_SIZE_U64) -MISMATCH_CHECK(CP_REG_ARM, KVM_REG_ARM) -MISMATCH_CHECK(CP_REG_ARCH_MASK, KVM_REG_ARCH_MASK) +MISMATCH_CHECK(CP_REG_SIZE_SHIFT, KVM_REG_SIZE_SHIFT); +MISMATCH_CHECK(CP_REG_SIZE_MASK, KVM_REG_SIZE_MASK); +MISMATCH_CHECK(CP_REG_SIZE_U32, KVM_REG_SIZE_U32); +MISMATCH_CHECK(CP_REG_SIZE_U64, KVM_REG_SIZE_U64); +MISMATCH_CHECK(CP_REG_ARM, KVM_REG_ARM); +MISMATCH_CHECK(CP_REG_ARCH_MASK, KVM_REG_ARCH_MASK); #define QEMU_PSCI_0_1_FN_BASE 0x95c1ba5e #define QEMU_PSCI_0_1_FN(n) (QEMU_PSCI_0_1_FN_BASE + (n)) @@ -45,10 +47,10 @@ MISMATCH_CHECK(CP_REG_ARCH_MASK, KVM_REG_ARCH_MASK) #define QEMU_PSCI_0_1_FN_CPU_ON QEMU_PSCI_0_1_FN(2) #define QEMU_PSCI_0_1_FN_MIGRATE QEMU_PSCI_0_1_FN(3) -MISMATCH_CHECK(QEMU_PSCI_0_1_FN_CPU_SUSPEND, KVM_PSCI_FN_CPU_SUSPEND) -MISMATCH_CHECK(QEMU_PSCI_0_1_FN_CPU_OFF, KVM_PSCI_FN_CPU_OFF) -MISMATCH_CHECK(QEMU_PSCI_0_1_FN_CPU_ON, KVM_PSCI_FN_CPU_ON) -MISMATCH_CHECK(QEMU_PSCI_0_1_FN_MIGRATE, KVM_PSCI_FN_MIGRATE) +MISMATCH_CHECK(QEMU_PSCI_0_1_FN_CPU_SUSPEND, KVM_PSCI_FN_CPU_SUSPEND); +MISMATCH_CHECK(QEMU_PSCI_0_1_FN_CPU_OFF, KVM_PSCI_FN_CPU_OFF); +MISMATCH_CHECK(QEMU_PSCI_0_1_FN_CPU_ON, KVM_PSCI_FN_CPU_ON); +MISMATCH_CHECK(QEMU_PSCI_0_1_FN_MIGRATE, KVM_PSCI_FN_MIGRATE); #define QEMU_PSCI_0_2_FN_BASE 0x84000000 #define QEMU_PSCI_0_2_FN(n) (QEMU_PSCI_0_2_FN_BASE + (n)) @@ -75,13 +77,13 @@ MISMATCH_CHECK(QEMU_PSCI_0_1_FN_MIGRATE, KVM_PSCI_FN_MIGRATE) #define QEMU_PSCI_0_2_FN64_AFFINITY_INFO QEMU_PSCI_0_2_FN64(4) #define QEMU_PSCI_0_2_FN64_MIGRATE QEMU_PSCI_0_2_FN64(5) -MISMATCH_CHECK(QEMU_PSCI_0_2_FN_CPU_SUSPEND, PSCI_0_2_FN_CPU_SUSPEND) -MISMATCH_CHECK(QEMU_PSCI_0_2_FN_CPU_OFF, PSCI_0_2_FN_CPU_OFF) -MISMATCH_CHECK(QEMU_PSCI_0_2_FN_CPU_ON, PSCI_0_2_FN_CPU_ON) -MISMATCH_CHECK(QEMU_PSCI_0_2_FN_MIGRATE, PSCI_0_2_FN_MIGRATE) -MISMATCH_CHECK(QEMU_PSCI_0_2_FN64_CPU_SUSPEND, PSCI_0_2_FN64_CPU_SUSPEND) -MISMATCH_CHECK(QEMU_PSCI_0_2_FN64_CPU_ON, PSCI_0_2_FN64_CPU_ON) -MISMATCH_CHECK(QEMU_PSCI_0_2_FN64_MIGRATE, PSCI_0_2_FN64_MIGRATE) +MISMATCH_CHECK(QEMU_PSCI_0_2_FN_CPU_SUSPEND, PSCI_0_2_FN_CPU_SUSPEND); +MISMATCH_CHECK(QEMU_PSCI_0_2_FN_CPU_OFF, PSCI_0_2_FN_CPU_OFF); +MISMATCH_CHECK(QEMU_PSCI_0_2_FN_CPU_ON, PSCI_0_2_FN_CPU_ON); +MISMATCH_CHECK(QEMU_PSCI_0_2_FN_MIGRATE, PSCI_0_2_FN_MIGRATE); +MISMATCH_CHECK(QEMU_PSCI_0_2_FN64_CPU_SUSPEND, PSCI_0_2_FN64_CPU_SUSPEND); +MISMATCH_CHECK(QEMU_PSCI_0_2_FN64_CPU_ON, PSCI_0_2_FN64_CPU_ON); +MISMATCH_CHECK(QEMU_PSCI_0_2_FN64_MIGRATE, PSCI_0_2_FN64_MIGRATE); /* PSCI v0.2 return values used by TCG emulation of PSCI */ @@ -91,9 +93,9 @@ MISMATCH_CHECK(QEMU_PSCI_0_2_FN64_MIGRATE, PSCI_0_2_FN64_MIGRATE) /* We implement version 0.2 only */ #define QEMU_PSCI_0_2_RET_VERSION_0_2 2 -MISMATCH_CHECK(QEMU_PSCI_0_2_RET_TOS_MIGRATION_NOT_REQUIRED, PSCI_0_2_TOS_MP) +MISMATCH_CHECK(QEMU_PSCI_0_2_RET_TOS_MIGRATION_NOT_REQUIRED, PSCI_0_2_TOS_MP); MISMATCH_CHECK(QEMU_PSCI_0_2_RET_VERSION_0_2, - (PSCI_VERSION_MAJOR(0) | PSCI_VERSION_MINOR(2))) + (PSCI_VERSION_MAJOR(0) | PSCI_VERSION_MINOR(2))); /* PSCI return values (inclusive of all PSCI versions) */ #define QEMU_PSCI_RET_SUCCESS 0 @@ -106,15 +108,15 @@ MISMATCH_CHECK(QEMU_PSCI_0_2_RET_VERSION_0_2, #define QEMU_PSCI_RET_NOT_PRESENT -7 #define QEMU_PSCI_RET_DISABLED -8 -MISMATCH_CHECK(QEMU_PSCI_RET_SUCCESS, PSCI_RET_SUCCESS) -MISMATCH_CHECK(QEMU_PSCI_RET_NOT_SUPPORTED, PSCI_RET_NOT_SUPPORTED) -MISMATCH_CHECK(QEMU_PSCI_RET_INVALID_PARAMS, PSCI_RET_INVALID_PARAMS) -MISMATCH_CHECK(QEMU_PSCI_RET_DENIED, PSCI_RET_DENIED) -MISMATCH_CHECK(QEMU_PSCI_RET_ALREADY_ON, PSCI_RET_ALREADY_ON) -MISMATCH_CHECK(QEMU_PSCI_RET_ON_PENDING, PSCI_RET_ON_PENDING) -MISMATCH_CHECK(QEMU_PSCI_RET_INTERNAL_FAILURE, PSCI_RET_INTERNAL_FAILURE) -MISMATCH_CHECK(QEMU_PSCI_RET_NOT_PRESENT, PSCI_RET_NOT_PRESENT) -MISMATCH_CHECK(QEMU_PSCI_RET_DISABLED, PSCI_RET_DISABLED) +MISMATCH_CHECK(QEMU_PSCI_RET_SUCCESS, PSCI_RET_SUCCESS); +MISMATCH_CHECK(QEMU_PSCI_RET_NOT_SUPPORTED, PSCI_RET_NOT_SUPPORTED); +MISMATCH_CHECK(QEMU_PSCI_RET_INVALID_PARAMS, PSCI_RET_INVALID_PARAMS); +MISMATCH_CHECK(QEMU_PSCI_RET_DENIED, PSCI_RET_DENIED); +MISMATCH_CHECK(QEMU_PSCI_RET_ALREADY_ON, PSCI_RET_ALREADY_ON); +MISMATCH_CHECK(QEMU_PSCI_RET_ON_PENDING, PSCI_RET_ON_PENDING); +MISMATCH_CHECK(QEMU_PSCI_RET_INTERNAL_FAILURE, PSCI_RET_INTERNAL_FAILURE); +MISMATCH_CHECK(QEMU_PSCI_RET_NOT_PRESENT, PSCI_RET_NOT_PRESENT); +MISMATCH_CHECK(QEMU_PSCI_RET_DISABLED, PSCI_RET_DISABLED); /* Note that KVM uses overlapping values for AArch32 and AArch64 * target CPU numbers. AArch32 targets: @@ -135,14 +137,14 @@ MISMATCH_CHECK(QEMU_PSCI_RET_DISABLED, PSCI_RET_DISABLED) #define QEMU_KVM_ARM_TARGET_NONE UINT_MAX #ifdef TARGET_AARCH64 -MISMATCH_CHECK(QEMU_KVM_ARM_TARGET_AEM_V8, KVM_ARM_TARGET_AEM_V8) -MISMATCH_CHECK(QEMU_KVM_ARM_TARGET_FOUNDATION_V8, KVM_ARM_TARGET_FOUNDATION_V8) -MISMATCH_CHECK(QEMU_KVM_ARM_TARGET_CORTEX_A57, KVM_ARM_TARGET_CORTEX_A57) -MISMATCH_CHECK(QEMU_KVM_ARM_TARGET_XGENE_POTENZA, KVM_ARM_TARGET_XGENE_POTENZA) -MISMATCH_CHECK(QEMU_KVM_ARM_TARGET_CORTEX_A53, KVM_ARM_TARGET_CORTEX_A53) +MISMATCH_CHECK(QEMU_KVM_ARM_TARGET_AEM_V8, KVM_ARM_TARGET_AEM_V8); +MISMATCH_CHECK(QEMU_KVM_ARM_TARGET_FOUNDATION_V8, KVM_ARM_TARGET_FOUNDATION_V8); +MISMATCH_CHECK(QEMU_KVM_ARM_TARGET_CORTEX_A57, KVM_ARM_TARGET_CORTEX_A57); +MISMATCH_CHECK(QEMU_KVM_ARM_TARGET_XGENE_POTENZA, KVM_ARM_TARGET_XGENE_POTENZA); +MISMATCH_CHECK(QEMU_KVM_ARM_TARGET_CORTEX_A53, KVM_ARM_TARGET_CORTEX_A53); #else -MISMATCH_CHECK(QEMU_KVM_ARM_TARGET_CORTEX_A15, KVM_ARM_TARGET_CORTEX_A15) -MISMATCH_CHECK(QEMU_KVM_ARM_TARGET_CORTEX_A7, KVM_ARM_TARGET_CORTEX_A7) +MISMATCH_CHECK(QEMU_KVM_ARM_TARGET_CORTEX_A15, KVM_ARM_TARGET_CORTEX_A15); +MISMATCH_CHECK(QEMU_KVM_ARM_TARGET_CORTEX_A7, KVM_ARM_TARGET_CORTEX_A7); #endif #define CP_REG_ARM64 0x6000000000000000ULL @@ -164,20 +166,20 @@ MISMATCH_CHECK(QEMU_KVM_ARM_TARGET_CORTEX_A7, KVM_ARM_TARGET_CORTEX_A7) #define CP_REG_ARM64_SYSREG_CP (CP_REG_ARM64_SYSREG >> CP_REG_ARM_COPROC_SHIFT) #ifdef TARGET_AARCH64 -MISMATCH_CHECK(CP_REG_ARM64, KVM_REG_ARM64) -MISMATCH_CHECK(CP_REG_ARM_COPROC_MASK, KVM_REG_ARM_COPROC_MASK) -MISMATCH_CHECK(CP_REG_ARM_COPROC_SHIFT, KVM_REG_ARM_COPROC_SHIFT) -MISMATCH_CHECK(CP_REG_ARM64_SYSREG, KVM_REG_ARM64_SYSREG) -MISMATCH_CHECK(CP_REG_ARM64_SYSREG_OP0_MASK, KVM_REG_ARM64_SYSREG_OP0_MASK) -MISMATCH_CHECK(CP_REG_ARM64_SYSREG_OP0_SHIFT, KVM_REG_ARM64_SYSREG_OP0_SHIFT) -MISMATCH_CHECK(CP_REG_ARM64_SYSREG_OP1_MASK, KVM_REG_ARM64_SYSREG_OP1_MASK) -MISMATCH_CHECK(CP_REG_ARM64_SYSREG_OP1_SHIFT, KVM_REG_ARM64_SYSREG_OP1_SHIFT) -MISMATCH_CHECK(CP_REG_ARM64_SYSREG_CRN_MASK, KVM_REG_ARM64_SYSREG_CRN_MASK) -MISMATCH_CHECK(CP_REG_ARM64_SYSREG_CRN_SHIFT, KVM_REG_ARM64_SYSREG_CRN_SHIFT) -MISMATCH_CHECK(CP_REG_ARM64_SYSREG_CRM_MASK, KVM_REG_ARM64_SYSREG_CRM_MASK) -MISMATCH_CHECK(CP_REG_ARM64_SYSREG_CRM_SHIFT, KVM_REG_ARM64_SYSREG_CRM_SHIFT) -MISMATCH_CHECK(CP_REG_ARM64_SYSREG_OP2_MASK, KVM_REG_ARM64_SYSREG_OP2_MASK) -MISMATCH_CHECK(CP_REG_ARM64_SYSREG_OP2_SHIFT, KVM_REG_ARM64_SYSREG_OP2_SHIFT) +MISMATCH_CHECK(CP_REG_ARM64, KVM_REG_ARM64); +MISMATCH_CHECK(CP_REG_ARM_COPROC_MASK, KVM_REG_ARM_COPROC_MASK); +MISMATCH_CHECK(CP_REG_ARM_COPROC_SHIFT, KVM_REG_ARM_COPROC_SHIFT); +MISMATCH_CHECK(CP_REG_ARM64_SYSREG, KVM_REG_ARM64_SYSREG); +MISMATCH_CHECK(CP_REG_ARM64_SYSREG_OP0_MASK, KVM_REG_ARM64_SYSREG_OP0_MASK); +MISMATCH_CHECK(CP_REG_ARM64_SYSREG_OP0_SHIFT, KVM_REG_ARM64_SYSREG_OP0_SHIFT); +MISMATCH_CHECK(CP_REG_ARM64_SYSREG_OP1_MASK, KVM_REG_ARM64_SYSREG_OP1_MASK); +MISMATCH_CHECK(CP_REG_ARM64_SYSREG_OP1_SHIFT, KVM_REG_ARM64_SYSREG_OP1_SHIFT); +MISMATCH_CHECK(CP_REG_ARM64_SYSREG_CRN_MASK, KVM_REG_ARM64_SYSREG_CRN_MASK); +MISMATCH_CHECK(CP_REG_ARM64_SYSREG_CRN_SHIFT, KVM_REG_ARM64_SYSREG_CRN_SHIFT); +MISMATCH_CHECK(CP_REG_ARM64_SYSREG_CRM_MASK, KVM_REG_ARM64_SYSREG_CRM_MASK); +MISMATCH_CHECK(CP_REG_ARM64_SYSREG_CRM_SHIFT, KVM_REG_ARM64_SYSREG_CRM_SHIFT); +MISMATCH_CHECK(CP_REG_ARM64_SYSREG_OP2_MASK, KVM_REG_ARM64_SYSREG_OP2_MASK); +MISMATCH_CHECK(CP_REG_ARM64_SYSREG_OP2_SHIFT, KVM_REG_ARM64_SYSREG_OP2_SHIFT); #endif #undef MISMATCH_CHECK diff --git a/tests/Makefile.include b/tests/Makefile.include index 1f6b732d13..634394aecf 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -9,7 +9,7 @@ SYSEMU_TARGET_LIST := $(subst -softmmu.mak,,$(notdir \ check-unit-y = tests/check-qdict$(EXESUF) gcov-files-check-qdict-y = qobject/qdict.c check-unit-y += tests/test-char$(EXESUF) -gcov-files-check-qdict-y = qemu-char.c +gcov-files-check-qdict-y = chardev/char.c check-unit-y += tests/check-qfloat$(EXESUF) gcov-files-check-qfloat-y = qobject/qfloat.c check-unit-y += tests/check-qint$(EXESUF) @@ -498,7 +498,7 @@ QEMU_CFLAGS += -I$(SRC_PATH)/tests # Deps that are common to various different sets of tests below -test-util-obj-y = libqemuutil.a libqemustub.a +test-util-obj-y = $(trace-obj-y) libqemuutil.a libqemustub.a test-qom-obj-y = $(qom-obj-y) $(test-util-obj-y) test-qapi-obj-y = tests/test-qapi-visit.o tests/test-qapi-types.o \ tests/test-qapi-event.o tests/test-qmp-introspect.o \ @@ -517,7 +517,8 @@ tests/check-qjson$(EXESUF): tests/check-qjson.o $(test-util-obj-y) tests/check-qom-interface$(EXESUF): tests/check-qom-interface.o $(test-qom-obj-y) tests/check-qom-proplist$(EXESUF): tests/check-qom-proplist.o $(test-qom-obj-y) -tests/test-char$(EXESUF): tests/test-char.o qemu-char.o qemu-timer.o $(test-util-obj-y) $(qtest-obj-y) $(test-block-obj-y) +tests/test-char$(EXESUF): tests/test-char.o qemu-timer.o \ + $(test-util-obj-y) $(qtest-obj-y) $(test-block-obj-y) $(chardev-obj-y) tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(test-block-obj-y) tests/test-aio$(EXESUF): tests/test-aio.o $(test-block-obj-y) tests/test-throttle$(EXESUF): tests/test-throttle.o $(test-block-obj-y) @@ -711,7 +712,9 @@ tests/usb-hcd-ehci-test$(EXESUF): tests/usb-hcd-ehci-test.o $(libqos-usb-obj-y) tests/usb-hcd-xhci-test$(EXESUF): tests/usb-hcd-xhci-test.o $(libqos-usb-obj-y) tests/pc-cpu-test$(EXESUF): tests/pc-cpu-test.o tests/postcopy-test$(EXESUF): tests/postcopy-test.o -tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o qemu-char.o qemu-timer.o $(qtest-obj-y) $(test-io-obj-y) $(libqos-virtio-obj-y) $(libqos-pc-obj-y) +tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o qemu-timer.o \ + $(qtest-obj-y) $(test-io-obj-y) $(libqos-virtio-obj-y) $(libqos-pc-obj-y) \ + $(chardev-obj-y) tests/qemu-iotests/socket_scm_helper$(EXESUF): tests/qemu-iotests/socket_scm_helper.o tests/test-qemu-opts$(EXESUF): tests/test-qemu-opts.o $(test-util-obj-y) tests/test-write-threshold$(EXESUF): tests/test-write-threshold.o $(test-block-obj-y) diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c index f3ac6ea21a..2c45c7b29f 100644 --- a/tests/vhost-user-test.c +++ b/tests/vhost-user-test.c @@ -12,6 +12,7 @@ #include "libqtest.h" #include "qapi/error.h" +#include "qemu/config-file.h" #include "qemu/option.h" #include "qemu/range.h" #include "qemu/sockets.h" diff --git a/thread-pool.c b/thread-pool.c index 6fba913529..3847969a6a 100644 --- a/thread-pool.c +++ b/thread-pool.c @@ -19,7 +19,7 @@ #include "qemu/queue.h" #include "qemu/thread.h" #include "qemu/coroutine.h" -#include "trace.h" +#include "trace-root.h" #include "block/thread-pool.h" #include "qemu/main-loop.h" diff --git a/trace-events b/trace-events index 839a9d0fba..756a9472dd 100644 --- a/trace-events +++ b/trace-events @@ -62,16 +62,6 @@ spice_vmc_event(int event) "spice vmc event %d" # xen-hvm.c xen_ram_alloc(unsigned long ram_addr, unsigned long size) "requested: %#lx, size %#lx" xen_client_set_memory(uint64_t start_addr, unsigned long size, bool log_dirty) "%#"PRIx64" size %#lx, log_dirty %i" -xen_default_ioreq_server(void) "" -xen_ioreq_server_create(uint32_t id) "id: %u" -xen_ioreq_server_destroy(uint32_t id) "id: %u" -xen_ioreq_server_state(uint32_t id, bool enable) "id: %u: enable: %i" -xen_map_mmio_range(uint32_t id, uint64_t start_addr, uint64_t end_addr) "id: %u start: %#"PRIx64" end: %#"PRIx64 -xen_unmap_mmio_range(uint32_t id, uint64_t start_addr, uint64_t end_addr) "id: %u start: %#"PRIx64" end: %#"PRIx64 -xen_map_portio_range(uint32_t id, uint64_t start_addr, uint64_t end_addr) "id: %u start: %#"PRIx64" end: %#"PRIx64 -xen_unmap_portio_range(uint32_t id, uint64_t start_addr, uint64_t end_addr) "id: %u start: %#"PRIx64" end: %#"PRIx64 -xen_map_pcidev(uint32_t id, uint8_t bus, uint8_t dev, uint8_t func) "id: %u bdf: %02x.%02x.%02x" -xen_unmap_pcidev(uint32_t id, uint8_t bus, uint8_t dev, uint8_t func) "id: %u bdf: %02x.%02x.%02x" handle_ioreq(void *req, uint32_t type, uint32_t dir, uint32_t df, uint32_t data_is_ptr, uint64_t addr, uint64_t data, uint32_t count, uint32_t size) "I/O=%p type=%d dir=%d df=%d ptr=%d port=%#"PRIx64" data=%#"PRIx64" count=%d size=%d" handle_ioreq_read(void *req, uint32_t type, uint32_t df, uint32_t data_is_ptr, uint64_t addr, uint64_t data, uint32_t count, uint32_t size) "I/O=%p read type=%d df=%d ptr=%d port=%#"PRIx64" data=%#"PRIx64" count=%d size=%d" handle_ioreq_write(void *req, uint32_t type, uint32_t df, uint32_t data_is_ptr, uint64_t addr, uint64_t data, uint32_t count, uint32_t size) "I/O=%p write type=%d df=%d ptr=%d port=%#"PRIx64" data=%#"PRIx64" count=%d size=%d" @@ -90,7 +80,6 @@ handle_qmp_command(void *mon, const char *cmd_name) "mon %p cmd_name \"%s\"" monitor_protocol_event_handler(uint32_t event, void *qdict) "event=%d data=%p" monitor_protocol_event_emit(uint32_t event, void *data) "event=%d data=%p" monitor_protocol_event_queue(uint32_t event, void *qdict, uint64_t rate) "event=%d data=%p rate=%" PRId64 -monitor_protocol_event_throttle(uint32_t event, uint64_t rate) "event=%d rate=%" PRId64 # dma-helpers.c dma_blk_io(void *dbs, void *bs, int64_t offset, bool to_dev) "dbs=%p bs=%p offset=%" PRId64 " to_dev=%d" diff --git a/trace/Makefile.objs b/trace/Makefile.objs index 1e1ce7479d..7de840ad7e 100644 --- a/trace/Makefile.objs +++ b/trace/Makefile.objs @@ -8,110 +8,36 @@ tracetool-y = $(SRC_PATH)/scripts/tracetool.py tracetool-y += $(shell find $(SRC_PATH)/scripts/tracetool -name "*.py") -$(BUILD_DIR)/trace-events-all: $(trace-events-y:%=$(SRC_PATH)/%) +$(BUILD_DIR)/trace-events-all: $(trace-events-files) $(call quiet-command,cat $^ > $@) -###################################################################### -# Auto-generated event descriptions for LTTng ust code - -ifeq ($(findstring ust,$(TRACE_BACKENDS)),ust) - -$(obj)/generated-ust-provider.h: $(obj)/generated-ust-provider.h-timestamp - @cmp $< $@ >/dev/null 2>&1 || cp $< $@ -$(obj)/generated-ust-provider.h-timestamp: $(BUILD_DIR)/trace-events-all $(tracetool-y) - $(call quiet-command,$(TRACETOOL) \ - --format=ust-events-h \ - --backends=$(TRACE_BACKENDS) \ - $< > $@,"GEN","$(patsubst %-timestamp,%,$@)") - -$(obj)/generated-ust.c: $(obj)/generated-ust.c-timestamp $(BUILD_DIR)/config-host.mak - @cmp $< $@ >/dev/null 2>&1 || cp $< $@ -$(obj)/generated-ust.c-timestamp: $(BUILD_DIR)/trace-events-all $(tracetool-y) - $(call quiet-command,$(TRACETOOL) \ - --format=ust-events-c \ - --backends=$(TRACE_BACKENDS) \ - $< > $@,"GEN","$(patsubst %-timestamp,%,$@)") - -$(obj)/generated-tracers.h: $(obj)/generated-ust-provider.h -$(obj)/generated-tracers.c: $(obj)/generated-ust.c - -endif - - -###################################################################### -# Auto-generated tracing routines - -################################################## -# Execution level - -$(obj)/generated-tracers.h: $(obj)/generated-tracers.h-timestamp - @cmp -s $< $@ || cp $< $@ -$(obj)/generated-tracers.h-timestamp: $(BUILD_DIR)/trace-events-all $(BUILD_DIR)/config-host.mak $(tracetool-y) - $(call quiet-command,$(TRACETOOL) \ - --format=h \ - --backends=$(TRACE_BACKENDS) \ - $< > $@,"GEN","$(patsubst %-timestamp,%,$@)") - -############################## -# non-DTrace - -$(obj)/generated-tracers.c: $(obj)/generated-tracers.c-timestamp - @cmp -s $< $@ || cp $< $@ -$(obj)/generated-tracers.c-timestamp: $(BUILD_DIR)/trace-events-all $(BUILD_DIR)/config-host.mak $(tracetool-y) - $(call quiet-command,$(TRACETOOL) \ - --format=c \ - --backends=$(TRACE_BACKENDS) \ - $< > $@,"GEN","$(patsubst %-timestamp,%,$@)") - -$(obj)/generated-tracers.o: $(obj)/generated-tracers.c $(obj)/generated-tracers.h - -############################## -# DTrace - -# Normal practice is to name DTrace probe file with a '.d' extension -# but that gets picked up by QEMU's Makefile as an external dependency -# rule file. So we use '.dtrace' instead -ifeq ($(findstring dtrace,$(TRACE_BACKENDS)),dtrace) - -$(obj)/generated-tracers-dtrace.dtrace: $(obj)/generated-tracers-dtrace.dtrace-timestamp - @cmp $< $@ >/dev/null 2>&1 || cp $< $@ -$(obj)/generated-tracers-dtrace.dtrace-timestamp: $(BUILD_DIR)/trace-events-all $(BUILD_DIR)/config-host.mak $(tracetool-y) - $(call quiet-command,$(TRACETOOL) \ - --format=d \ - --backends=$(TRACE_BACKENDS) \ - $< > $@,"GEN","$(patsubst %-timestamp,%,$@)") - -$(obj)/generated-tracers-dtrace.h: $(obj)/generated-tracers-dtrace.dtrace - $(call quiet-command,dtrace -o $@ -h -s $<,"GEN","$@") - -$(obj)/generated-tracers-dtrace.o: $(obj)/generated-tracers-dtrace.dtrace - -util-obj-y += generated-tracers-dtrace.o -endif ################################################## # Translation level $(obj)/generated-helpers-wrappers.h: $(obj)/generated-helpers-wrappers.h-timestamp @cmp $< $@ >/dev/null 2>&1 || cp $< $@ -$(obj)/generated-helpers-wrappers.h-timestamp: $(BUILD_DIR)/trace-events-all $(BUILD_DIR)/config-host.mak $(tracetool-y) +$(obj)/generated-helpers-wrappers.h-timestamp: $(trace-events-files) $(BUILD_DIR)/config-host.mak $(tracetool-y) $(call quiet-command,$(TRACETOOL) \ + --group=all \ --format=tcg-helper-wrapper-h \ --backend=$(TRACE_BACKENDS) \ $< > $@,"GEN","$(patsubst %-timestamp,%,$@)") $(obj)/generated-helpers.h: $(obj)/generated-helpers.h-timestamp @cmp $< $@ >/dev/null 2>&1 || cp $< $@ -$(obj)/generated-helpers.h-timestamp: $(BUILD_DIR)/trace-events-all $(BUILD_DIR)/config-host.mak $(tracetool-y) +$(obj)/generated-helpers.h-timestamp: $(trace-events-files) $(BUILD_DIR)/config-host.mak $(tracetool-y) $(call quiet-command,$(TRACETOOL) \ + --group=all \ --format=tcg-helper-h \ --backend=$(TRACE_BACKENDS) \ $< > $@,"GEN","$(patsubst %-timestamp,%,$@)") $(obj)/generated-helpers.c: $(obj)/generated-helpers.c-timestamp @cmp $< $@ >/dev/null 2>&1 || cp $< $@ -$(obj)/generated-helpers.c-timestamp: $(BUILD_DIR)/trace-events-all $(BUILD_DIR)/config-host.mak $(tracetool-y) +$(obj)/generated-helpers.c-timestamp: $(trace-events-files) $(BUILD_DIR)/config-host.mak $(tracetool-y) $(call quiet-command,$(TRACETOOL) \ + --group=all \ --format=tcg-helper-c \ --backend=$(TRACE_BACKENDS) \ $< > $@,"GEN","$(patsubst %-timestamp,%,$@)") @@ -123,8 +49,9 @@ target-obj-y += generated-helpers.o $(obj)/generated-tcg-tracers.h: $(obj)/generated-tcg-tracers.h-timestamp @cmp $< $@ >/dev/null 2>&1 || cp $< $@ -$(obj)/generated-tcg-tracers.h-timestamp: $(BUILD_DIR)/trace-events-all $(BUILD_DIR)/config-host.mak $(tracetool-y) +$(obj)/generated-tcg-tracers.h-timestamp: $(trace-events-files) $(BUILD_DIR)/config-host.mak $(tracetool-y) $(call quiet-command,$(TRACETOOL) \ + --group=all \ --format=tcg-h \ --backend=$(TRACE_BACKENDS) \ $< > $@,"GEN","$(patsubst %-timestamp,%,$@)") @@ -133,10 +60,8 @@ $(obj)/generated-tcg-tracers.h-timestamp: $(BUILD_DIR)/trace-events-all $(BUILD_ ###################################################################### # Backend code -util-obj-y += generated-tracers.o util-obj-$(CONFIG_TRACE_SIMPLE) += simple.o util-obj-$(CONFIG_TRACE_FTRACE) += ftrace.o -util-obj-$(CONFIG_TRACE_UST) += generated-ust.o util-obj-y += control.o target-obj-y += control-target.o util-obj-y += qmp.o diff --git a/trace/control-target.c b/trace/control-target.c index e2e138a3f0..6266e6380d 100644 --- a/trace/control-target.c +++ b/trace/control-target.c @@ -9,7 +9,7 @@ #include "qemu/osdep.h" #include "cpu.h" -#include "trace.h" +#include "trace-root.h" #include "trace/control.h" #include "translate-all.h" diff --git a/trace/control.c b/trace/control.c index 56a2632584..9b157b0ca7 100644 --- a/trace/control.c +++ b/trace/control.c @@ -26,7 +26,7 @@ #include "qemu/error-report.h" #include "qemu/config-file.h" #include "monitor/monitor.h" -#include "trace.h" +#include "trace-root.h" int trace_events_enabled_count; diff --git a/trace/ftrace.c b/trace/ftrace.c index 3588bb0eb4..7de104deba 100644 --- a/trace/ftrace.c +++ b/trace/ftrace.c @@ -10,8 +10,8 @@ */ #include "qemu/osdep.h" -#include "trace.h" #include "trace/control.h" +#include "trace/ftrace.h" int trace_marker_fd; diff --git a/trace/simple.c b/trace/simple.c index b263622fa9..a221a3f703 100644 --- a/trace/simple.c +++ b/trace/simple.c @@ -13,7 +13,6 @@ #include <pthread.h> #endif #include "qemu/timer.h" -#include "trace.h" #include "trace/control.h" #include "trace/simple.h" diff --git a/translate-all.c b/translate-all.c index 6d2fcabca7..5f44ec844e 100644 --- a/translate-all.c +++ b/translate-all.c @@ -25,7 +25,7 @@ #include "qemu-common.h" #define NO_CPU_IO_DEFS #include "cpu.h" -#include "trace.h" +#include "trace-root.h" #include "disas/disas.h" #include "exec/exec-all.h" #include "tcg.h" diff --git a/ui/console.c b/ui/console.c index fe03a666f7..49d0740b40 100644 --- a/ui/console.c +++ b/ui/console.c @@ -2059,8 +2059,6 @@ static void text_console_do_init(Chardev *chr, DisplayState *ds) qemu_chr_be_generic_open(chr); } -static const CharDriver vc_driver; - static void vc_chr_open(Chardev *chr, ChardevBackend *backend, bool *be_opened, @@ -2116,7 +2114,7 @@ void qemu_console_resize(QemuConsole *s, int width, int height) assert(s->console_type == GRAPHIC_CONSOLE); - if (s->surface && + if (s->surface && (s->surface->flags & QEMU_ALLOCATED_FLAG) && pixman_image_get_width(s->surface->image) == width && pixman_image_get_height(s->surface->image) == height) { return; @@ -2150,6 +2148,7 @@ void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp) int val; ChardevVC *vc; + backend->type = CHARDEV_BACKEND_KIND_VC; vc = backend->u.vc.data = g_new0(ChardevVC, 1); qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc)); @@ -2189,6 +2188,7 @@ static void char_vc_class_init(ObjectClass *oc, void *data) { ChardevClass *cc = CHARDEV_CLASS(oc); + cc->parse = qemu_chr_parse_vc; cc->open = vc_chr_open; cc->chr_write = vc_chr_write; cc->chr_set_echo = vc_chr_set_echo; @@ -2206,15 +2206,9 @@ void qemu_console_early_init(void) /* set the default vc driver */ if (!object_class_by_name(TYPE_CHARDEV_VC)) { type_register(&char_vc_type_info); - register_char_driver(&vc_driver); } } -static const CharDriver vc_driver = { - .kind = CHARDEV_BACKEND_KIND_VC, - .parse = qemu_chr_parse_vc, -}; - static void register_types(void) { type_register_static(&qemu_console_info); @@ -105,6 +105,7 @@ #define GDK_KEY_g GDK_g #define GDK_KEY_q GDK_q #define GDK_KEY_plus GDK_plus +#define GDK_KEY_equal GDK_equal #define GDK_KEY_minus GDK_minus #define GDK_KEY_Pause GDK_Pause #define GDK_KEY_Delete GDK_Delete @@ -1007,6 +1008,10 @@ static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button, btn = INPUT_BUTTON_MIDDLE; } else if (button->button == 3) { btn = INPUT_BUTTON_RIGHT; + } else if (button->button == 8) { + btn = INPUT_BUTTON_SIDE; + } else if (button->button == 9) { + btn = INPUT_BUTTON_EXTRA; } else { return TRUE; } @@ -1027,6 +1032,19 @@ static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll, btn = INPUT_BUTTON_WHEEL_UP; } else if (scroll->direction == GDK_SCROLL_DOWN) { btn = INPUT_BUTTON_WHEEL_DOWN; +#if GTK_CHECK_VERSION(3, 4, 0) + } else if (scroll->direction == GDK_SCROLL_SMOOTH) { + gdouble delta_x, delta_y; + if (!gdk_event_get_scroll_deltas((GdkEvent *)scroll, + &delta_x, &delta_y)) { + return TRUE; + } + if (delta_y > 0) { + btn = INPUT_BUTTON_WHEEL_DOWN; + } else { + btn = INPUT_BUTTON_WHEEL_UP; + } +#endif } else { return TRUE; } @@ -1325,6 +1343,12 @@ static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque) gd_update_windowsize(vc); } +static void gd_accel_zoom_in(void *opaque) +{ + GtkDisplayState *s = opaque; + gtk_menu_item_activate(GTK_MENU_ITEM(s->zoom_in_item)); +} + static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque) { GtkDisplayState *s = opaque; @@ -1715,8 +1739,6 @@ static void gd_vc_chr_set_echo(Chardev *chr, bool echo) static int nb_vcs; static Chardev *vcs[MAX_VCS]; -static const CharDriver gd_vc_driver; - static void gd_vc_open(Chardev *chr, ChardevBackend *backend, bool *be_opened, @@ -1739,6 +1761,7 @@ static void char_gd_vc_class_init(ObjectClass *oc, void *data) { ChardevClass *cc = CHARDEV_CLASS(oc); + cc->parse = qemu_chr_parse_vc; cc->open = gd_vc_open; cc->chr_write = gd_vc_chr_write; cc->chr_set_echo = gd_vc_chr_set_echo; @@ -1751,11 +1774,6 @@ static const TypeInfo char_gd_vc_type_info = { .class_init = char_gd_vc_class_init, }; -static const CharDriver gd_vc_driver = { - .kind = CHARDEV_BACKEND_KIND_VC, - .parse = qemu_chr_parse_vc, -}; - static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size, gpointer user_data) { @@ -2092,6 +2110,8 @@ static GtkWidget *gd_create_menu_view(GtkDisplayState *s) "<QEMU>/View/Zoom In"); gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus, HOTKEY_MODIFIERS); + gtk_accel_group_connect(s->accel_group, GDK_KEY_equal, HOTKEY_MODIFIERS, 0, + g_cclosure_new_swap(G_CALLBACK(gd_accel_zoom_in), s, NULL)); gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item); s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out")); @@ -2232,8 +2252,12 @@ void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover) s->free_scale = FALSE; - /* LC_MESSAGES only. See early_gtk_display_init() for details */ + /* Mostly LC_MESSAGES only. See early_gtk_display_init() for details. For + * LC_CTYPE, we need to make sure that non-ASCII characters are considered + * printable, but without changing any of the character classes to make + * sure that we don't accidentally break implicit assumptions. */ setlocale(LC_MESSAGES, ""); + setlocale(LC_CTYPE, "C.UTF-8"); bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR); textdomain("qemu"); @@ -2353,6 +2377,5 @@ void early_gtk_display_init(int opengl) #if defined(CONFIG_VTE) type_register(&char_gd_vc_type_info); - register_char_driver(&gd_vc_driver); #endif } diff --git a/ui/input-linux.c b/ui/input-linux.c index f345317794..ac31f47719 100644 --- a/ui/input-linux.c +++ b/ui/input-linux.c @@ -291,6 +291,12 @@ static void input_linux_handle_mouse(InputLinux *il, struct input_event *event) qemu_input_queue_btn(NULL, INPUT_BUTTON_WHEEL_DOWN, event->value); break; + case BTN_SIDE: + qemu_input_queue_btn(NULL, INPUT_BUTTON_SIDE, event->value); + break; + case BTN_EXTRA: + qemu_input_queue_btn(NULL, INPUT_BUTTON_EXTRA, event->value); + break; }; break; case EV_REL: diff --git a/ui/spice-display.c b/ui/spice-display.c index 5e6f78a219..64e472eeb0 100644 --- a/ui/spice-display.c +++ b/ui/spice-display.c @@ -769,6 +769,7 @@ static void display_mouse_set(DisplayChangeListener *dcl, g_free(ssd->ptr_move); ssd->ptr_move = qemu_spice_create_cursor_update(ssd, NULL, on); qemu_mutex_unlock(&ssd->lock); + qemu_spice_wakeup(ssd); } static void display_mouse_define(DisplayChangeListener *dcl, @@ -787,6 +788,7 @@ static void display_mouse_define(DisplayChangeListener *dcl, g_free(ssd->ptr_define); ssd->ptr_define = qemu_spice_create_cursor_update(ssd, c, 0); qemu_mutex_unlock(&ssd->lock); + qemu_spice_wakeup(ssd); } static const DisplayChangeListenerOps display_listener_ops = { @@ -1231,8 +1231,6 @@ void vnc_disconnect_finish(VncState *vs) vnc_update_server_surface(vs->vd); } - if (vs->vd->lock_key_sync) - qemu_remove_led_event_handler(vs->led); vnc_unlock_output(vs); qemu_mutex_destroy(&vs->output_mutex); @@ -1259,7 +1257,7 @@ ssize_t vnc_client_io_error(VncState *vs, ssize_t ret, Error **errp) if (ret == 0) { VNC_DEBUG("Closing down client sock: EOF\n"); } else if (ret != QIO_CHANNEL_ERR_BLOCK) { - VNC_DEBUG("Closing down client sock: ret %d (%s)\n", + VNC_DEBUG("Closing down client sock: ret %zd (%s)\n", ret, errp ? error_get_pretty(*errp) : "Unknown"); } @@ -1665,69 +1663,39 @@ static void press_key(VncState *vs, int keysym) qemu_input_event_send_key_delay(vs->vd->key_delay_ms); } -static int current_led_state(VncState *vs) -{ - int ledstate = 0; - - if (vs->modifiers_state[0x46]) { - ledstate |= QEMU_SCROLL_LOCK_LED; - } - if (vs->modifiers_state[0x45]) { - ledstate |= QEMU_NUM_LOCK_LED; - } - if (vs->modifiers_state[0x3a]) { - ledstate |= QEMU_CAPS_LOCK_LED; - } - - return ledstate; -} - static void vnc_led_state_change(VncState *vs) { - int ledstate = 0; - if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) { return; } - ledstate = current_led_state(vs); vnc_lock_output(vs); vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); vnc_write_u8(vs, 0); vnc_write_u16(vs, 1); vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE); - vnc_write_u8(vs, ledstate); + vnc_write_u8(vs, vs->vd->ledstate); vnc_unlock_output(vs); vnc_flush(vs); } static void kbd_leds(void *opaque, int ledstate) { - VncState *vs = opaque; - int caps, num, scr; - bool has_changed = (ledstate != current_led_state(vs)); + VncDisplay *vd = opaque; + VncState *client; trace_vnc_key_guest_leds((ledstate & QEMU_CAPS_LOCK_LED), (ledstate & QEMU_NUM_LOCK_LED), (ledstate & QEMU_SCROLL_LOCK_LED)); - caps = ledstate & QEMU_CAPS_LOCK_LED ? 1 : 0; - num = ledstate & QEMU_NUM_LOCK_LED ? 1 : 0; - scr = ledstate & QEMU_SCROLL_LOCK_LED ? 1 : 0; - - if (vs->modifiers_state[0x3a] != caps) { - vs->modifiers_state[0x3a] = caps; - } - if (vs->modifiers_state[0x45] != num) { - vs->modifiers_state[0x45] = num; - } - if (vs->modifiers_state[0x46] != scr) { - vs->modifiers_state[0x46] = scr; + if (ledstate == vd->ledstate) { + return; } - /* Sending the current led state message to the client */ - if (has_changed) { - vnc_led_state_change(vs); + vd->ledstate = ledstate; + + QTAILQ_FOREACH(client, &vd->clients, next) { + vnc_led_state_change(client); } } @@ -2756,8 +2724,10 @@ static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y) static int vnc_update_stats(VncDisplay *vd, struct timeval * tv) { - int width = pixman_image_get_width(vd->guest.fb); - int height = pixman_image_get_height(vd->guest.fb); + int width = MIN(pixman_image_get_width(vd->guest.fb), + pixman_image_get_width(vd->server)); + int height = MIN(pixman_image_get_height(vd->guest.fb), + pixman_image_get_height(vd->server)); int x, y; struct timeval res; int has_dirty = 0; @@ -3087,8 +3057,6 @@ void vnc_start_protocol(VncState *vs) vnc_write(vs, "RFB 003.008\n", 12); vnc_flush(vs); vnc_read_when(vs, protocol_version, 12); - if (vs->vd->lock_key_sync) - vs->led = qemu_add_led_event_handler(kbd_leds, vs); vs->mouse_mode_notifier.notify = check_pointer_type_change; qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier); @@ -3195,6 +3163,9 @@ static void vnc_display_close(VncDisplay *vd) } g_free(vd->tlsaclname); vd->tlsaclname = NULL; + if (vd->lock_key_sync) { + qemu_remove_led_event_handler(vd->led); + } } int vnc_display_password(const char *id, const char *password) @@ -3762,6 +3733,10 @@ void vnc_display_open(const char *id, Error **errp) } #endif vd->lock_key_sync = lock_key_sync; + if (lock_key_sync) { + vd->led = qemu_add_led_event_handler(kbd_leds, vd); + } + vd->ledstate = 0; vd->key_delay_ms = key_delay_ms; device_id = qemu_opt_get(opts, "display"); @@ -154,6 +154,8 @@ struct VncDisplay DisplayChangeListener dcl; kbd_layout_t *kbd_layout; int lock_key_sync; + QEMUPutLEDEntry *led; + int ledstate; int key_delay_ms; QemuMutex mutex; @@ -304,7 +306,6 @@ struct VncState size_t read_handler_expect; /* input */ uint8_t modifiers_state[256]; - QEMUPutLEDEntry *led; bool abort; QemuMutex output_mutex; @@ -110,7 +110,7 @@ int main(int argc, char **argv) #include "slirp/libslirp.h" -#include "trace.h" +#include "trace-root.h" #include "trace/control.h" #include "qemu/queue.h" #include "sysemu/arch_init.h" @@ -22,7 +22,7 @@ #include "qemu/error-report.h" #include "qemu/range.h" #include "sysemu/xen-mapcache.h" -#include "trace.h" +#include "trace-root.h" #include "exec/address-spaces.h" #include <xen/hvm/ioreq.h> diff --git a/xen-mapcache.c b/xen-mapcache.c index 31debdfb2c..1a96d2e5db 100644 --- a/xen-mapcache.c +++ b/xen-mapcache.c @@ -19,7 +19,7 @@ #include <xen/hvm/params.h> #include "sysemu/xen-mapcache.h" -#include "trace.h" +#include "trace-root.h" //#define MAPCACHE_DEBUG |