From patchwork Thu Apr 7 19:47:26 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prasad Joshi X-Patchwork-Id: 693491 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 p388EDrY003611 for ; Fri, 8 Apr 2011 08:14:24 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756853Ab1DGTro (ORCPT ); Thu, 7 Apr 2011 15:47:44 -0400 Received: from mail-ww0-f44.google.com ([74.125.82.44]:43875 "EHLO mail-ww0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756826Ab1DGTrk (ORCPT ); Thu, 7 Apr 2011 15:47:40 -0400 Received: by wwa36 with SMTP id 36so3441654wwa.1 for ; Thu, 07 Apr 2011 12:47:38 -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=Yfg+KgiBg2KOgCQK3Qvirsn+7m5Zey6yY5CUGzh81KA=; b=Urf/7xxAGGEPJYKV976rGhL23I18WpZ3+CPrVknKDWqp4DOK7WeQbsb70Iqif4+oYZ +vAdh59CsAmOgV7GkYT+jpSmk8Ynj13PfeIHptCA/WunPn1OY2VjkD3YwHn7j6EYMM6A M17KO89zxriVKOQhMtCjHGFpO6HDzi0RiVMvw= 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=qDclIJdfVohmvJNIDmo+Ibkm4LEA8cBEViVehwtZxmElMK+e/PJChgdCkGZFjZonhB g7NpMN8SJtPiG8EGWdcBxN0hmg0k6OXr7/huC7j3TNZqZVEgUUtzkquzNyZluz/D8J0X nXT2yIhr1zP2myW17Fu3jw9ywg7ZitXJnqVE4= Received: by 10.227.0.152 with SMTP id 24mr1314008wbb.126.1302205658782; Thu, 07 Apr 2011 12:47:38 -0700 (PDT) Received: from prasad-kvm.localdomain (pineapple.rdg.ac.uk [134.225.206.123]) by mx.google.com with ESMTPS id bs4sm1258004wbb.35.2011.04.07.12.47.36 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 07 Apr 2011 12:47:37 -0700 (PDT) Received: by prasad-kvm.localdomain (Postfix, from userid 1000) id 5C2A926E006F; Thu, 7 Apr 2011 20:47:36 +0100 (BST) From: Prasad Joshi To: prasadjoshi124@gmail.com Cc: mingo@elte.hu, kvm@vger.kernel.org, penberg@kernel.org, asias.hejun@gmail.com, gorcunov@gmail.com, oswaldo.cadenas@gmail.com Subject: [PATCH 4/4] Using the Gitish freamwork to run the virtual machine Date: Thu, 7 Apr 2011 20:47:26 +0100 Message-Id: <1302205647-7435-4-git-send-email-prasadjoshi124@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1302205647-7435-1-git-send-email-prasadjoshi124@gmail.com> References: <1302205647-7435-1-git-send-email-prasadjoshi124@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]); Fri, 08 Apr 2011 08:14:24 +0000 (UTC) - kvm-run.[ch] Adds a new kvm command called 'run'. The most of the code is copied from main.c. - main.c is modified to use the functionality provided by framework. The old code from main.c is moved to kvm-run.c. Signed-off-by: Prasad Joshi --- tools/kvm/Makefile | 19 +++- tools/kvm/include/kvm/kvm-run.h | 6 + tools/kvm/kvm-run.c | 231 +++++++++++++++++++++++++++++++++++++ tools/kvm/main.c | 243 ++------------------------------------- 4 files changed, 267 insertions(+), 232 deletions(-) create mode 100644 tools/kvm/include/kvm/kvm-run.h create mode 100644 tools/kvm/kvm-run.c diff --git a/tools/kvm/Makefile b/tools/kvm/Makefile index 747aa51..3224cee 100644 --- a/tools/kvm/Makefile +++ b/tools/kvm/Makefile @@ -24,6 +24,11 @@ OBJS += main.o OBJS += mmio.o OBJS += pci.o OBJS += util.o +OBJS += util/parse-options.o +OBJS += util/strbuf.o +OBJS += kvm-help.o +OBJS += kvm-cmd.o +OBJS += kvm-run.o DEPS := $(patsubst %.o,%.d,$(OBJS)) @@ -53,7 +58,8 @@ endif DEFINES += -D_FILE_OFFSET_BITS=64 DEFINES += -D_GNU_SOURCE -CFLAGS += $(CPPFLAGS) $(DEFINES) -Iinclude -I../../include -I../../arch/$(ARCH)/include/ -Os -g +KVM_INCLUDE := include +CFLAGS += $(CPPFLAGS) $(DEFINES) -I$(KVM_INCLUDE) -I../../include -I../../arch/$(ARCH)/include/ -Os -g WARNINGS += -Werror WARNINGS += -Wall @@ -85,12 +91,22 @@ $(DEPS): %.d: %.c $(Q) $(CC) -M -MT $(patsubst %.d,%.o,$@) $(CFLAGS) $< -o $@ +# The header file common-cmds.h is needed for compilation of kvm-help.c. +kvm-help.d: $(KVM_INCLUDE)/common-cmds.h + $(OBJS): %.o: %.c $(E) " CC " $@ $(Q) $(CC) -c $(CFLAGS) $< -o $@ + +$(KVM_INCLUDE)/common-cmds.h: util/generate-cmdlist.sh command-list.txt + +$(KVM_INCLUDE)/common-cmds.h: $(wildcard Documentation/kvm-*.txt) + $(E) " GEN " $@ + $(Q) util/generate-cmdlist.sh > $@+ && mv $@+ $@ + # # BIOS assembly weirdness # @@ -127,6 +143,7 @@ clean: $(Q) rm -f bios/bios-rom.h $(Q) rm -f $(DEPS) $(OBJS) $(PROGRAM) $(Q) rm -f cscope.* + $(Q) rm -f $(KVM_INCLUDE)/common-cmds.h .PHONY: clean KVM_DEV ?= /dev/kvm diff --git a/tools/kvm/include/kvm/kvm-run.h b/tools/kvm/include/kvm/kvm-run.h new file mode 100644 index 0000000..13104e2 --- /dev/null +++ b/tools/kvm/include/kvm/kvm-run.h @@ -0,0 +1,6 @@ +#ifndef __KVM_RUN_H__ +#define __KVM_RUN_H__ + +int kvm_cmd_run(int argc, const char **argv, const char *prefix); + +#endif diff --git a/tools/kvm/kvm-run.c b/tools/kvm/kvm-run.c new file mode 100644 index 0000000..832e811 --- /dev/null +++ b/tools/kvm/kvm-run.c @@ -0,0 +1,231 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +/* user defined header files */ +#include +#include +#include +#include +#include +#include +#include + +/* header files for gitish interface */ +#include +#include + +#define MB_SHIFT (20) +#define MIN_RAM_SIZE_MB (64UL) +#define MIN_RAM_SIZE_BYTE (MIN_RAM_SIZE_MB << MB_SHIFT) + +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); +} + +static void handle_sigquit(int sig) +{ + kvm__show_registers(kvm); + kvm__show_code(kvm); + kvm__show_page_tables(kvm); + + kvm__delete(kvm); + + exit(1); +} + +static u64 ram_size; +static const char *kernel_cmdline; +static const char *kernel_filename; +static const char *initrd_filename; +static const char *image_filename; +static bool single_step = false; +extern bool ioport_debug; + +static const char * const run_usage[] = { + "kvm run []", + NULL +}; + +static const struct option options[] = { + OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."), + OPT_STRING('p', "params", &kernel_cmdline, "params", + "Kernel command line arguments"), + OPT_STRING('i', "initrd", &initrd_filename, "initrd", + "Initial RAM disk image"), + OPT_STRING('k', "kernel", &kernel_filename, "kernel", + "Kernel to boot in virtual machine"), + OPT_STRING('d', "image", &image_filename, "image", "Disk image"), + OPT_BOOLEAN('s', "single-step", &single_step, "Enable single stepping"), + OPT_BOOLEAN('g', "ioport-debug", &ioport_debug, + "Enable ioport debugging"), + OPT_END() +}; + +int kvm_cmd_run(int argc, const char **argv, const char *prefix) +{ + const char *kvm_dev = "/dev/kvm"; + static char real_cmdline[2048]; + + argc = parse_options(argc, argv, options, run_usage, + PARSE_OPT_STOP_AT_NON_OPTION); + if (!argc && (!kernel_filename || !image_filename)) { + /* no argument specified or manadatory argument not specified + display the usage message + */ + usage_with_options(run_usage, options); + } + + if (ram_size < MIN_RAM_SIZE_MB) { + die("Not enough memory specified: %luMB (min %luMB)", ram_size, + MIN_RAM_SIZE_MB); + } + ram_size <<= MB_SHIFT; + + signal(SIGQUIT, handle_sigquit); + signal(SIGINT, handle_sigint); + + setup_console(); + + atexit(shutdown); + + + kvm = kvm__init(kvm_dev, ram_size); + + if (image_filename) { + kvm->disk_image = disk_image__open(image_filename); + if (!kvm->disk_image) + die("unable to load disk image %s", image_filename); + } + + kvm__setup_cpuid(kvm); + + strcpy(real_cmdline, "notsc nolapic noacpi pci=conf1 console=ttyS0 "); + if (!kernel_cmdline || !strstr(kernel_cmdline, "root=")) { + strlcat(real_cmdline, "root=/dev/vda rw ", + sizeof(real_cmdline)); + } + + if (kernel_cmdline) { + strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline)); + real_cmdline[sizeof(real_cmdline)-1] = '\0'; + } + + if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename, + real_cmdline)) + die("unable to load kernel %s", kernel_filename); + + kvm__reset_vcpu(kvm); + + kvm__setup_bios(kvm); + + if (single_step) + kvm__enable_singlestep(kvm); + + serial8250__init(kvm); + pci__init(); + + blk_virtio__init(kvm); + + kvm__start_timer(kvm); + + for (;;) { + kvm__run(kvm); + + switch (kvm->kvm_run->exit_reason) { + case KVM_EXIT_DEBUG: + kvm__show_registers(kvm); + kvm__show_code(kvm); + break; + case KVM_EXIT_IO: { + bool ret; + + ret = kvm__emulate_io(kvm, + kvm->kvm_run->io.port, + (uint8_t *)kvm->kvm_run + + kvm->kvm_run->io.data_offset, + kvm->kvm_run->io.direction, + kvm->kvm_run->io.size, + kvm->kvm_run->io.count); + + if (!ret) + goto panic_kvm; + break; + } + case KVM_EXIT_MMIO: { + bool ret; + + ret = kvm__emulate_mmio(kvm, + kvm->kvm_run->mmio.phys_addr, + kvm->kvm_run->mmio.data, + kvm->kvm_run->mmio.len, + kvm->kvm_run->mmio.is_write); + + if (!ret) + goto panic_kvm; + break; + } + case KVM_EXIT_INTR: { + serial8250__interrupt(kvm); + break; + } + case KVM_EXIT_SHUTDOWN: + goto exit_kvm; + default: + goto panic_kvm; + } + } +exit_kvm: + disk_image__close(kvm->disk_image); + kvm__delete(kvm); + + return 0; + +panic_kvm: + fprintf(stderr, "KVM exit reason: %" PRIu32 " (\"%s\")\n", + kvm->kvm_run->exit_reason, + kvm_exit_reasons[kvm->kvm_run->exit_reason]); + if (kvm->kvm_run->exit_reason == KVM_EXIT_UNKNOWN) + fprintf(stderr, "KVM exit code: 0x%" PRIu64 "\n", + kvm->kvm_run->hw.hardware_exit_reason); + disk_image__close(kvm->disk_image); + kvm__show_registers(kvm); + kvm__show_code(kvm); + kvm__show_page_tables(kvm); + kvm__delete(kvm); + + return 1; +} diff --git a/tools/kvm/main.c b/tools/kvm/main.c index 7beb6a6..4fbb268 100644 --- a/tools/kvm/main.c +++ b/tools/kvm/main.c @@ -1,241 +1,22 @@ -#include "kvm/kvm.h" - -#include "kvm/8250-serial.h" -#include "kvm/blk-virtio.h" -#include "kvm/disk-image.h" -#include "kvm/util.h" -#include "kvm/pci.h" - -#include -#include -#include -#include -#include -#include -#include #include -extern bool ioport_debug; - -static void usage(char *argv[]) -{ - fprintf(stderr, " usage: %s " - "[--single-step] [--ioport-debug] " - "[--kvm-dev=] [--mem=] [--params=] " - "[--initrd=] [--kernel=] [--image=]\n", - argv[0]); - exit(1); -} - -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); -} +/* user defined header files */ +#include +#include +#include -static void cleanup_console(void) +static int handle_kvm_command(int argc, char **argv) { - tcsetattr(STDIN_FILENO, TCSANOW, &orig_term); -} - -static void shutdown(void) -{ - cleanup_console(); -} - -static void handle_sigint(int sig) -{ - exit(1); -} - -static void handle_sigquit(int sig) -{ - kvm__show_registers(kvm); - kvm__show_code(kvm); - kvm__show_page_tables(kvm); - - kvm__delete(kvm); - - exit(1); -} - -static char real_cmdline[2048]; + struct cmd_struct command[] = { + { "help", kvm_cmd_help, 0 }, + { "run", kvm_cmd_run, 0 }, + { NULL, NULL, 0 }, + }; -static bool option_matches(char *arg, const char *option) -{ - return !strncmp(arg, option, strlen(option)); + return handle_command(command, argc, (const char **) &argv[0]); } int main(int argc, char *argv[]) { - const char *kernel_filename = NULL; - const char *initrd_filename = NULL; - const char *image_filename = NULL; - const char *kernel_cmdline = NULL; - const char *kvm_dev = "/dev/kvm"; - unsigned long ram_size = 64UL << 20; - bool single_step = false; - int i; - - 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]; - continue; - } else if (option_matches(argv[i], "--image=")) { - image_filename = &argv[i][8]; - continue; - } else if (option_matches(argv[i], "--initrd=")) { - initrd_filename = &argv[i][9]; - continue; - } else if (option_matches(argv[i], "--params=")) { - kernel_cmdline = &argv[i][9]; - continue; - } else if (option_matches(argv[i], "--kvm-dev=")) { - kvm_dev = &argv[i][10]; - continue; - } else if (option_matches(argv[i], "--single-step")) { - single_step = true; - continue; - } else if (option_matches(argv[i], "--mem=")) { - unsigned long val = atol(&argv[i][6]) << 20; - if (val < ram_size) - die("Not enough memory specified: %sMB (min %luMB)", - argv[i], ram_size >> 20); - ram_size = val; - continue; - } else if (option_matches(argv[i], "--ioport-debug")) { - ioport_debug = true; - continue; - } else { - /* any unspecified arg is kernel image */ - if (argv[i][0] != '-') - kernel_filename = argv[i]; - else - warning("Unknown option: %s", argv[i]); - } - } - - /* at least we should have kernel image passed */ - if (!kernel_filename) - usage(argv); - - kvm = kvm__init(kvm_dev, ram_size); - - if (image_filename) { - kvm->disk_image = disk_image__open(image_filename); - if (!kvm->disk_image) - die("unable to load disk image %s", image_filename); - } - - kvm__setup_cpuid(kvm); - - strcpy(real_cmdline, "notsc nolapic noacpi pci=conf1 console=ttyS0 "); - if (!kernel_cmdline || !strstr(kernel_cmdline, "root=")) - strlcat(real_cmdline, "root=/dev/vda rw ", sizeof(real_cmdline)); - - if (kernel_cmdline) { - strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline)); - real_cmdline[sizeof(real_cmdline)-1] = '\0'; - } - - if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename, real_cmdline)) - die("unable to load kernel %s", kernel_filename); - - kvm__reset_vcpu(kvm); - - kvm__setup_bios(kvm); - - if (single_step) - kvm__enable_singlestep(kvm); - - serial8250__init(kvm); - pci__init(); - - blk_virtio__init(kvm); - - kvm__start_timer(kvm); - - for (;;) { - kvm__run(kvm); - - switch (kvm->kvm_run->exit_reason) { - case KVM_EXIT_DEBUG: - kvm__show_registers(kvm); - kvm__show_code(kvm); - break; - case KVM_EXIT_IO: { - bool ret; - - ret = kvm__emulate_io(kvm, - kvm->kvm_run->io.port, - (uint8_t *)kvm->kvm_run + kvm->kvm_run->io.data_offset, - kvm->kvm_run->io.direction, - kvm->kvm_run->io.size, - kvm->kvm_run->io.count); - - if (!ret) - goto panic_kvm; - break; - } - case KVM_EXIT_MMIO: { - bool ret; - - ret = kvm__emulate_mmio(kvm, - kvm->kvm_run->mmio.phys_addr, - kvm->kvm_run->mmio.data, - kvm->kvm_run->mmio.len, - kvm->kvm_run->mmio.is_write); - - if (!ret) - goto panic_kvm; - break; - } - case KVM_EXIT_INTR: { - serial8250__interrupt(kvm); - break; - } - case KVM_EXIT_SHUTDOWN: - goto exit_kvm; - default: - goto panic_kvm; - } - } -exit_kvm: - disk_image__close(kvm->disk_image); - kvm__delete(kvm); - - return 0; - -panic_kvm: - fprintf(stderr, "KVM exit reason: %" PRIu32 " (\"%s\")\n", - kvm->kvm_run->exit_reason, kvm_exit_reasons[kvm->kvm_run->exit_reason]); - if (kvm->kvm_run->exit_reason == KVM_EXIT_UNKNOWN) - fprintf(stderr, "KVM exit code: 0x%" PRIu64 "\n", - kvm->kvm_run->hw.hardware_exit_reason); - disk_image__close(kvm->disk_image); - kvm__show_registers(kvm); - kvm__show_code(kvm); - kvm__show_page_tables(kvm); - kvm__delete(kvm); - - return 1; + return handle_kvm_command(argc - 1, &argv[1]); }