Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 259419 - app-emulation/kvm-83 - guest cannot access host serial port
Summary: app-emulation/kvm-83 - guest cannot access host serial port
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: x86 Linux
: High normal (vote)
Assignee: Daniel Gryniewicz (RETIRED)
URL: http://patchwork.kernel.org/patch/5694/
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-02-17 21:59 UTC by Christof Baur
Modified: 2009-05-14 14:06 UTC (History)
1 user (show)

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Christof Baur 2009-02-17 21:59:10 UTC
The issue is described under 
http://patchwork.kernel.org/patch/5694/

as follows:

Submitter
Michael Tokarev
  Date
2009-02-05 18:33:30
  Message ID
<498B30FA.40800@msgid.tls.msk.ru>
  Download
mbox | patch 
  Permalink
/patch/5694/ 
  State 
New
  Headers
show 
 
Comments
Michael Tokarev - 2009-02-05 18:33:30
Michael Tokarev wrote:
> After some debugging and debugging, with a help
> Hollis Blanchard on #kvm@freenode, I discovered
> that kvm (or, rather, qemu) does not work correctly
> with serial ports, at least on linux.  One problem
> report has already here, author Cc'd -- see e.g.
> http://marc.info/?l=kvm&m=122995568009533&w=2 .

... [quoted in full below]...

Ok, It's a real shame to see SO many wrong attempts to
do it all, with so many idiotic mistakes all over...
But c'est la vie, it seems... ;)

So here we go.  Attached is a patch that fixes two
problems with serial ports &qemu (yes it's a qemu
issue, and, as far as I can see, both probs were
here for a long time).

First is completely f*cked up flags reporting and
setup for TIOCMGET and TIOCMSET ioctls, where ALL
known flags were reported and set in case at least
one flag is set, misusing "if(a|b) foo" instead of
"if(a&b) foo" -- just a typo I assume, but heck of
a typo... ;)

And second - for TIOCMSET it preserves other, unknown
flags.  Which fixes the problem that started it all,
since there was a bit set internally in kernel which,
when unset, makes serial port non-working, but TIOCMSET
dropped all "other" bits on the floor.

And for this second one, I'm still unsure.  The patch
I'm sending only tries to remove TIOCM_DTR and _RTS
bits (RTS is useless since it's controlled by the connected
device, isn't it?), leaving all others, incl., say,
CAR, RI, CTS etc, in place.  The question is -- some
of those bits are "input" lines, i.e., the ones controlled
by the attached device, and I don't know if all platforms
will ignore those instead of reporting error.  I.e, maybe
filter also those who are known "inputs"?  And while we're
at it, still, how about RTS?

Signed-off-By: Michael Tokarev <mjt@tls.msk.ru>

Thanks!

/ashamed mjt

--- original content follows ---
> Here's what's going on.
> 
> When opening a host's port, kvm resets the status
> lines, doing this:
> 
> ioctl(13, TIOCMGET, [TIOCM_DTR|TIOCM_RTS|TIOCM_CTS|TIOCM_DSR|0x4000])
> ioctl(13, TIOCMSET, [TIOCM_DTR|TIOCM_RTS])
> 
> which results in the following set
> 
> ioctl(13, TIOCMGET, [TIOCM_DTR|TIOCM_RTS|TIOCM_CTS|TIOCM_DSR])
> 
> Note the difference between the default set and new one: the
> missing bit, 0x4000, which is unknown to strace(1) but is defined
> as TIOCM_OUT2 in linux headers.
> 
> After that change (resetting the TIOCM_OUT2 bit), no writes
> to the serial port works anymore, they're all gets accepted
> by host kernel and are buffered in the kernel.
> 
> After some time, when the kernel buffer fills up, and since
> the port (on host side) is opened in non-blocking mode, the
> writes starts returning EAGAIN, and kvm process starts
> endless loop, which were seen by David.
> 
> Here's the trivial program to demonstrate the idea:
> 
> ---- cut ---
> #include <sys/types.h>
> #include <unistd.h>
> #include <fcntl.h>
> #include <sys/ioctl.h>
> #include <termios.h>
> 
> int main(int argc, char **argv) {
>   fd = open("/dev/ttyS0", O_RDWR|O_NONBLOCK);
>   fcntl(fd, F_SETFL, O_RDWR);
>   x = TIOCM_DTR|TIOCM_RTS|TIOCM_CTS|TIOCM_DSR // |0x4000
>   ;
>   ioctl(fd, TIOCMSET, &x);
>   ioctl(fd, TIOCMGET, &x);
>   write(fd, "at\r", 3);
>   read(fd, buf, 20);
>   close(fd);
> 
>   return 0;
> }
> --- cut ---
> 
> 
> Run it under strace while a dialup modem is connected to /dev/ttyS0
> (i used this way for testing).  It will stuck at read, and nothing
> will be written, even when write() will happily return 3.  Un-comment
> the |0x4000 thing, and it will work.
> 
> I'm not sure what should be done with this, and how much this is
> linux-specific.  But it is obvious that bit (TIOCM_OUT2) should
> be left in-place (after which the thing works), at least on linux.
> 
> Note that this bit is NOT shown in /proc/tty/driver/serial file
> (which shows other bits).
> 
> Note also that this file (/proc/tty/driver/serial) helps to see
> if any write were performed: compare the counters.  In 'tx'
> there's number of bytes actually sent to device, as opposed to
> accepted by the kernel.  When you write something to /dev/ttyS0,
> that number increases, IF that something actually reached the
> device.
> 
> Thanks.
> 
> /mjt




Reproducible: Always

Steps to Reproduce:
1. Connect null modem to serial port and check correct function with komport 
2. Call kvm with parameter -serial /dev/sttyS0 ; use Windows XP as a host
3. Open Hyperterm and start typing => no characters are displayed
4. Call kvm with parameter -serial file:fromKVM.txt ; use Windows XP as a host
5. Open Hyperterm and start typing => the characters appear in the file

Actual Results:  
No output to serial port

Expected Results:  
Output to serial port

This patch was submitted by Michael Tokarev to patchwork.kernel.org:


--- kvm-83/qemu/qemu-char.c.orig        2009-01-13 16:29:42.000000000 +0300
+++ kvm-83/qemu/qemu-char.c     2009-02-05 21:19:35.972015110 +0300
@@ -1067,17 +1067,17 @@  static int tty_serial_ioctl(CharDriverSt
             int *targ = (int *)arg;
             ioctl(s->fd_in, TIOCMGET, &sarg);
             *targ = 0;
-            if (sarg | TIOCM_CTS)
+            if (sarg & TIOCM_CTS)
                 *targ |= CHR_TIOCM_CTS;
-            if (sarg | TIOCM_CAR)
+            if (sarg & TIOCM_CAR)
                 *targ |= CHR_TIOCM_CAR;
-            if (sarg | TIOCM_DSR)
+            if (sarg & TIOCM_DSR)
                 *targ |= CHR_TIOCM_DSR;
-            if (sarg | TIOCM_RI)
+            if (sarg & TIOCM_RI)
                 *targ |= CHR_TIOCM_RI;
-            if (sarg | TIOCM_DTR)
+            if (sarg & TIOCM_DTR)
                 *targ |= CHR_TIOCM_DTR;
-            if (sarg | TIOCM_RTS)
+            if (sarg & TIOCM_RTS)
                 *targ |= CHR_TIOCM_RTS;
         }
         break;
@@ -1085,9 +1085,11 @@  static int tty_serial_ioctl(CharDriverSt
         {
             int sarg = *(int *)arg;
             int targ = 0;
-            if (sarg | CHR_TIOCM_DTR)
+            ioctl(s->fd_in, TIOCMGET, &targ);
+            targ &= ~(TIOCM_DTR|TIOCM_RTS);
+            if (sarg & CHR_TIOCM_DTR)
                 targ |= TIOCM_DTR;
-            if (sarg | CHR_TIOCM_RTS)
+            if (sarg & CHR_TIOCM_RTS)
                 targ |= TIOCM_RTS;
             ioctl(s->fd_in, TIOCMSET, &targ);
         }
Comment 1 Daniel Gryniewicz (RETIRED) gentoo-dev 2009-02-19 02:40:55 UTC
Please try 84.
Comment 2 Christof Baur 2009-02-19 20:32:26 UTC
Tried kvm-84. This did not work either.

Inserted line 108 (see below) in the kvm-83 ebuild to apply this patch:



--- kvm-83/qemu/qemu-char.c.orig	2009-01-13 16:29:42.000000000 +0300
+++ kvm-83/qemu/qemu-char.c	2009-02-05 21:19:35.972015110 +0300
@@ -1067,17 +1067,17 @@ static int tty_serial_ioctl(CharDriverSt
             int *targ = (int *)arg;
             ioctl(s->fd_in, TIOCMGET, &sarg);
             *targ = 0;
-            if (sarg | TIOCM_CTS)
+            if (sarg & TIOCM_CTS)
                 *targ |= CHR_TIOCM_CTS;
-            if (sarg | TIOCM_CAR)
+            if (sarg & TIOCM_CAR)
                 *targ |= CHR_TIOCM_CAR;
-            if (sarg | TIOCM_DSR)
+            if (sarg & TIOCM_DSR)
                 *targ |= CHR_TIOCM_DSR;
-            if (sarg | TIOCM_RI)
+            if (sarg & TIOCM_RI)
                 *targ |= CHR_TIOCM_RI;
-            if (sarg | TIOCM_DTR)
+            if (sarg & TIOCM_DTR)
                 *targ |= CHR_TIOCM_DTR;
-            if (sarg | TIOCM_RTS)
+            if (sarg & TIOCM_RTS)
                 *targ |= CHR_TIOCM_RTS;
         }
         break;
@@ -1085,9 +1085,11 @@ static int tty_serial_ioctl(CharDriverSt
         {
             int sarg = *(int *)arg;
             int targ = 0;
-            if (sarg | CHR_TIOCM_DTR)
+            ioctl(s->fd_in, TIOCMGET, &targ);
+            targ &= ~(TIOCM_DTR|TIOCM_RTS);
+            if (sarg & CHR_TIOCM_DTR)
                 targ |= TIOCM_DTR;
-            if (sarg | CHR_TIOCM_RTS)
+            if (sarg & CHR_TIOCM_RTS)
                 targ |= TIOCM_RTS;
             ioctl(s->fd_in, TIOCMSET, &targ);
         }




ebuild:


# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/app-emulation/kvm/kvm-83.ebuild,v 1.2 2009/02/08 19:36:43 dang Exp $

inherit eutils flag-o-matic toolchain-funcs linux-mod

EAPI="1"

# Patchset git repo is at http://github.com/dang/kvm-patches/tree/master
PATCHSET="kvm-patches-20090126"
SRC_URI="mirror://sourceforge/${PN}/${P}.tar.gz
	http://apollo.fprintf.net/downloads/${PATCHSET}.tar.gz"

DESCRIPTION="Kernel-based Virtual Machine userland tools"
HOMEPAGE="http://kvm.qumranet.com/kvmwiki"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="-* ~amd64 ~x86"
# Add bios back when it builds again
IUSE="alsa esd gnutls havekernel +modules ncurses pulseaudio +sdl test vde"
RESTRICT="test"

RDEPEND="sys-libs/zlib
	alsa? ( >=media-libs/alsa-lib-1.0.13 )
	esd? ( media-sound/esound )
	pulseaudio? ( media-sound/pulseaudio )
	gnutls? ( net-libs/gnutls )
	ncurses? ( sys-libs/ncurses )
	sdl? ( >=media-libs/libsdl-1.2.11 )
	vde? ( net-misc/vde )"

#    bios? (
#        sys-devel/dev86
#        dev-lang/perl
#        sys-power/iasl
#    )
DEPEND="${RDEPEND}
	gnutls? ( dev-util/pkgconfig )
	app-text/texi2html"

QA_TEXTRELS="usr/bin/kvm"

pkg_setup() {
	linux-info_pkg_setup
	if use havekernel && use modules ; then
		ewarn "You have the 'havekernel' and 'modules' use flags enabled."
		ewarn "'havekernel' trumps 'modules'; the kvm modules will not"
		ewarn "be built.  You must ensure you have a compatible kernel"
		ewarn "with the kvm modules on your own"
	elif use havekernel ; then
		ewarn "You have the 'havekernel' use flag set.  This means you"
		ewarn "must ensure you have a compatible kernel on your own."
	elif use modules ; then
		if ! linux_chkconfig_present KVM; then
			eerror "KVM now needs CONFIG_KVM built into your kernel, even"
			eerror "if you're using the external modules from this package."
			eerror "Please enable KVM support in your kernel, found at:"
			eerror
			eerror "  Virtualization"
			eerror "    Kernel-based Virtual Machine (KVM) support"
			eerror
			die "KVM support not detected!"
		fi
		BUILD_TARGETS="all"
		MODULE_NAMES="kvm(kvm:${S}/kernel:${S}/kernel/x86)"
		MODULE_NAMES="${MODULE_NAMES} kvm-intel(kvm:${S}/kernel:${S}/kernel/x86)"
		MODULE_NAMES="${MODULE_NAMES} kvm-amd(kvm:${S}/kernel:${S}/kernel/x86)"
		linux-mod_pkg_setup
	elif kernel_is lt 2 6 25; then
		eerror "This version of KVM requres a host kernel of 2.6.25 or higher."
		eerror "Either upgrade your kernel, or enable the 'modules' USE flag."
		die "kvm version not compatible"
	elif ! linux_chkconfig_present KVM; then
		eerror "Please enable KVM support in your kernel, found at:"
		eerror
		eerror "  Virtualization"
		eerror "    Kernel-based Virtual Machine (KVM) support"
		eerror
		eerror "or enable the 'modules' USE flag."
		die "KVM support not detected!"
	fi

	if use sdl && ! built_with_use media-libs/libsdl X ; then
		die "You need to rebuild media-libs/libsdl with the X use flag"
	fi

	enewgroup kvm
}

src_unpack() {
	unpack ${A}

	cd "${S}"
	# prevent docs to get automatically installed
	sed -i '/$(DESTDIR)$(docdir)/d' qemu/Makefile
	# Alter target makefiles to accept CFLAGS set via flag-o
	sed -i 's/^\(C\|OP_C\|HELPER_C\)FLAGS=/\1FLAGS+=/' \
		qemu/Makefile qemu/Makefile.target
	[[ -x /sbin/paxctl ]] && \
		sed -i 's/^VL_LDFLAGS=$/VL_LDFLAGS=-Wl,-z,execheap/' \
			qemu/Makefile.target
	# avoid strip
	sed -i 's/$(INSTALL) -m 755 -s/$(INSTALL) -m 755/' qemu/Makefile

	# Fix building with vtd; bug #256685
	epatch "${FILESDIR}"/${P}-vtd.patch
	epatch "${FILESDIR}"/${P}-serial.patch

	# apply patchset
	EPATCH_SOURCE="${WORKDIR}/${PATCHSET}"
	EPATCH_SUFFIX="patch"
	epatch
}

src_compile() {
	local mycc conf_opts audio_opts

	audio_opts="oss"
	use gnutls || conf_opts="$conf_opts --disable-vnc-tls"
	use ncurses || conf_opts="$conf_opts --disable-curses"
	use sdl || conf_opts="$conf_opts --disable-gfx-check --disable-sdl"
	use vde || conf_opts="$conf_opts --disable-vde"
	use alsa && audio_opts="alsa $audio_opts"
	use esd && audio_opts="esd $audio_opts"
	use pulseaudio && audio_opts="pa $audio_opts"
	use sdl && audio_opts="sdl $audio_opts"
	use modules && conf_opts="$conf_opts --kerneldir=$KV_DIR"
	conf_opts="$conf_opts --prefix=/usr"
	#conf_opts="$conf_opts --audio-drv-list=\"$audio_opts\""

	./configure ${conf_opts} --audio-drv-list="$audio_opts" || die "econf failed"

	emake libkvm || die "emake libkvm failed"

	if use test; then
		emake user || die "emake user failed"
	fi

	mycc=$(cat qemu/config-host.mak | egrep "^CC=" | cut -d "=" -f 2)

	filter-flags -fpie -fstack-protector

	# If using gentoo's compiler set the SPEC to non-hardened
	if [ ! -z ${GCC_SPECS} -a -f ${GCC_SPECS} ]; then
		local myccver=$(${mycc} -dumpversion)
		local gccver=$($(tc-getBUILD_CC) -dumpversion)

		#Is this a SPEC for the right compiler version?
		myspec="${GCC_SPECS/${gccver}/${myccver}}"
		if [ "${myspec}" == "${GCC_SPECS}" ]; then
			shopt -s extglob
			GCC_SPECS="${GCC_SPECS/%hardened*specs/vanilla.specs}"
			shopt -u extglob
		else
			unset GCC_SPECS
		fi
	fi

#    if use bios; then
#        emake bios || die "emake bios failed"
#        emake vgabios || die "emake vgabios failed"
#    fi

	emake qemu || die "emake qemu failed"

	if use modules && ! use havekernel ; then
		linux-mod_src_compile
	fi
}

src_install() {
	# kcmd so we don't install kernel modules which weren't build
	emake DESTDIR="${D}" kcmd='#' install || die "make install failed"

	exeinto /usr/bin/
	doexe "${S}/kvm_stat"

	mv "${D}"/usr/share/man/man1/qemu.1 "${D}"/usr/share/man/man1/kvm.1
	mv "${D}"/usr/share/man/man1/qemu-img.1 "${D}"/usr/share/man/man1/kvm-img.1
	mv "${D}"/usr/share/man/man8/qemu-nbd.8 "${D}"/usr/share/man/man8/kvm-nbd.8
	mv "${D}"/usr/bin/qemu-img "${D}"/usr/bin/kvm-img
	mv "${D}"/usr/bin/qemu-nbd "${D}"/usr/bin/kvm-nbd

	insinto /etc/udev/rules.d/
	doins scripts/65-kvm.rules

	insinto /etc/kvm/
	insopts -m0755
	newins scripts/qemu-ifup kvm-ifup
	newins scripts/qemu-ifdown kvm-ifdown

	dodoc qemu/pc-bios/README
	newdoc qemu/qemu-doc.html kvm-doc.html
	newdoc qemu/qemu-tech.html kvm-tech.html

	if use modules && ! use havekernel ; then
		linux-mod_src_install
	fi
}

pkg_postinst() {
	elog "If you don't have kvm compiled into the kernel, make sure you have"
	elog "the kernel module loaded before running kvm. The easiest way to"
	elog "ensure that the kernel module is loaded is to load it on boot."
	elog "For AMD CPUs the module is called 'kvm-amd'"
	elog "For Intel CPUs the module is called 'kvm-intel'"
	elog "Please review /etc/conf.d/modules for how to load these"
	elog
	elog "Make sure your user is in the 'kvm' group"
	elog "Just run 'gpasswd -a <USER> kvm', then have <USER> re-login."
	elog
	elog "You will need the Universal TUN/TAP driver compiled into your"
	elog "kernel or loaded as a module to use the virtual network device"
	elog "if using -net tap.  You will also need support for 802.1d"
	elog "Ethernet Bridging and a configured bridge if using the provided"
	elog "kvm-ifup script from /etc/kvm."
	echo
}


=> SUCCESS !!!
(the serial port works in the guest)




Comment 3 Daniel Gryniewicz (RETIRED) gentoo-dev 2009-02-19 21:08:23 UTC
Hrm.  I'd thought that had been fixed in 84.  Just revert to 83 for now, and I'll add the patch to the 84 patchset too.
Comment 4 Daniel Gryniewicz (RETIRED) gentoo-dev 2009-05-14 14:06:48 UTC
I just manually verified that this patch has been applied to kvm 85.  Please re-open if it's still broken somehow.