From patchwork Fri Apr 8 14:12:50 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asias He X-Patchwork-Id: 692551 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p37EEHfO001672 for ; Thu, 7 Apr 2011 14:14:17 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752155Ab1DGOON (ORCPT ); Thu, 7 Apr 2011 10:14:13 -0400 Received: from mail-yi0-f46.google.com ([209.85.218.46]:55859 "EHLO mail-yi0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751562Ab1DGOOM (ORCPT ); Thu, 7 Apr 2011 10:14:12 -0400 Received: by yia27 with SMTP id 27so1029222yia.19 for ; Thu, 07 Apr 2011 07:14:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references; bh=TEYtltcRW8HmUHgyqal8ymwK12P+5ySzBij8+3lfaL4=; b=pY/zG2qgzMi77E9LlTrYLC+OTVHFzZAGL8wfdz3bosTYx4HBAcw9aE1dXFWlbVlrQU WzklHLHfUIL7s5BCibwhU2CiU6DXgsXdzGb5EsHvsbUuxRJ/GVRwNM0ehh3TJwRiJliQ 9jtaBEQ5Wlp6gY0ybLXgCTj1SZcJZPwvUHyQ8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=Y7zpKRua2nIT2dGnFAqNaulPtQFgRC7Fjt6lWDelMwbztZZTe95kdRWVPr9UeQu6Nf KTQMZ59PcezyJ/xh7JvBdaKY71PpWT7HWxnQjFzS6jiIU9/rbIjU7CZp6w/IHVT+2I0t 3Q455M0YjOz9XRV0m58OHVNJJQf03Ee1Xo+OU= Received: by 10.42.18.4 with SMTP id v4mr1580527ica.399.1302185651979; Thu, 07 Apr 2011 07:14:11 -0700 (PDT) Received: from localhost.localdomain ([219.224.169.130]) by mx.google.com with ESMTPS id m10sm2277615wfl.11.2011.04.07.07.14.07 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 07 Apr 2011 07:14:11 -0700 (PDT) From: Asias He To: Pekka Enberg , Cyrill Gorcunov Cc: kvm@vger.kernel.org, Ingo Molnar , Asias He Subject: [PATCH 3/6] kvm tools: move terminal related code to a new file term.c Date: Fri, 8 Apr 2011 22:12:50 +0800 Message-Id: <1302271973-7604-3-git-send-email-asias.hejun@gmail.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1302271973-7604-1-git-send-email-asias.hejun@gmail.com> References: <1302271973-7604-1-git-send-email-asias.hejun@gmail.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Thu, 07 Apr 2011 14:14:17 +0000 (UTC) preparing the virtio console and 8250 serial console consolidation Signed-off-by: Asias He --- tools/kvm/Makefile | 1 + tools/kvm/include/kvm/term.h | 17 ++++++++ tools/kvm/main.c | 35 +--------------- tools/kvm/term.c | 89 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 32 deletions(-) create mode 100644 tools/kvm/include/kvm/term.h create mode 100644 tools/kvm/term.c diff --git a/tools/kvm/Makefile b/tools/kvm/Makefile index 747aa51..0244f9b 100644 --- a/tools/kvm/Makefile +++ b/tools/kvm/Makefile @@ -24,6 +24,7 @@ OBJS += main.o OBJS += mmio.o OBJS += pci.o OBJS += util.o +OBJS += term.o DEPS := $(patsubst %.o,%.d,$(OBJS)) diff --git a/tools/kvm/include/kvm/term.h b/tools/kvm/include/kvm/term.h new file mode 100644 index 0000000..4d580e1 --- /dev/null +++ b/tools/kvm/include/kvm/term.h @@ -0,0 +1,17 @@ +#ifndef KVM__TERM_H +#define KVM__TERM_H + +#include + +#define CONSOLE_8250 1 +#define CONSOLE_VIRTIO 2 + +int term_putc_iov(int who, struct iovec *iov, int iovcnt); +int term_getc_iov(int who, struct iovec *iov, int iovcnt); +int term_putc(int who, char *addr, int cnt); +int term_getc(int who); + +bool term_readable(int who); +void term_init(void); + +#endif /* KVM__TERM_H */ diff --git a/tools/kvm/main.c b/tools/kvm/main.c index 7beb6a6..46fb9fd 100644 --- a/tools/kvm/main.c +++ b/tools/kvm/main.c @@ -5,9 +5,9 @@ #include "kvm/disk-image.h" #include "kvm/util.h" #include "kvm/pci.h" +#include "kvm/term.h" #include -#include #include #include #include @@ -29,32 +29,6 @@ static void usage(char *argv[]) static struct kvm *kvm; -static struct termios orig_term; - -static void setup_console(void) -{ - struct termios term; - - if (tcgetattr(STDIN_FILENO, &orig_term) < 0) - die("unable to save initial standard input settings"); - - term = orig_term; - - term.c_lflag &= ~(ICANON|ECHO); - - tcsetattr(STDIN_FILENO, TCSANOW, &term); -} - -static void cleanup_console(void) -{ - tcsetattr(STDIN_FILENO, TCSANOW, &orig_term); -} - -static void shutdown(void) -{ - cleanup_console(); -} - static void handle_sigint(int sig) { exit(1); @@ -65,7 +39,6 @@ static void handle_sigquit(int sig) kvm__show_registers(kvm); kvm__show_code(kvm); kvm__show_page_tables(kvm); - kvm__delete(kvm); exit(1); @@ -92,10 +65,6 @@ int main(int argc, char *argv[]) signal(SIGQUIT, handle_sigquit); signal(SIGINT, handle_sigint); - setup_console(); - - atexit(shutdown); - for (i = 1; i < argc; i++) { if (option_matches(argv[i], "--kernel=")) { kernel_filename = &argv[i][9]; @@ -138,6 +107,8 @@ int main(int argc, char *argv[]) if (!kernel_filename) usage(argv); + term_init(); + kvm = kvm__init(kvm_dev, ram_size); if (image_filename) { diff --git a/tools/kvm/term.c b/tools/kvm/term.c new file mode 100644 index 0000000..0ac53c0 --- /dev/null +++ b/tools/kvm/term.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include + +#include "kvm/read-write.h" +#include "kvm/term.h" +#include "kvm/util.h" + +static struct termios orig_term; + +int active_console = CONSOLE_8250; + +int term_getc(int who) +{ + int c; + + if (who != active_console) + return -1; + + if (read_in_full(STDIN_FILENO, &c, 1) < 0) + return -1; + return c; +} + +int term_putc(int who, char *addr, int cnt) +{ + if (who != active_console) + return -1; + + while (cnt--) { + fprintf(stdout, "%c", *addr++); + } + + fflush(stdout); + return cnt; +} + +int term_getc_iov(int who, struct iovec *iov, int iovcnt) +{ + if (who != active_console) + return -1; + + return readv(STDIN_FILENO, iov, iovcnt); +} + +int term_putc_iov(int who, struct iovec *iov, int iovcnt) +{ + if (who != active_console) + return -1; + + return writev(STDOUT_FILENO, iov, iovcnt); +} + +bool term_readable(int who) +{ + struct pollfd pollfd = (struct pollfd) { + .fd = STDIN_FILENO, + .events = POLLIN, + .revents = 0, + }; + + if (who != active_console) + return false; + + return poll(&pollfd, 1, 0) > 0; +} + +static void term_cleanup(void) +{ + tcsetattr(STDIN_FILENO, TCSANOW, &orig_term); +} + +void term_init(void) +{ + struct termios term; + + if (tcgetattr(STDIN_FILENO, &orig_term) < 0) + die("unable to save initial standard input settings"); + + term = orig_term; + term.c_lflag &= ~(ICANON |ECHO | ISIG); + tcsetattr(STDIN_FILENO, TCSANOW, &term); + + atexit(term_cleanup); +} +