Patchwork [19/22] machine: introduce accel option to allow selection of kvm or tcg

login
register
mail settings
Submitter Anthony Liguori
Date June 7, 2010, 11:52 p.m.
Message ID <1275954730-8196-20-git-send-email-aliguori@us.ibm.com>
Download mbox | patch
Permalink /patch/54914/
State New
Headers show

Comments

Anthony Liguori - June 7, 2010, 11:52 p.m.
Syntax is -machine accel=kvm
          -machine accel=tcg
          -machine accel=kvm|tcg

kvm|tcg tries kvm and fallbacks on tcg if kvm support isn't available.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>

Patch

diff --git a/hw/boards.h b/hw/boards.h
index 947fa33..1097561 100644
--- a/hw/boards.h
+++ b/hw/boards.h
@@ -81,6 +81,9 @@  void machine_set_default(const char *name);
     },{                                 \
         .name = "sockets",              \
         .type = QEMU_OPT_NUMBER,        \
+    },{                                 \
+        .name = "accel",                \
+        .type = QEMU_OPT_STRING,        \
     }
 
 
diff --git a/vl.c b/vl.c
index d84fc65..7cb5865 100644
--- a/vl.c
+++ b/vl.c
@@ -2654,6 +2654,8 @@  int main(int argc, char **argv, char **envp)
     int defconfig = 1;
     QemuOpts *machine_opts = NULL;
     int max_cpus = 0;
+    const char *accel;
+    int tcg_fallback = 0;
 
     error_set_progname(argv[0]);
 
@@ -3272,7 +3274,10 @@  int main(int argc, char **argv, char **envp)
                 do_smbios_option(optarg);
                 break;
             case QEMU_OPTION_enable_kvm:
-                kvm_allowed = 1;
+                if (!qemu_opts_parsef(&qemu_machine_opts, "accel=kvm")) {
+                    printf("failed\n");
+                    exit(1);
+                }
                 break;
             case QEMU_OPTION_usb:
                 usb_enabled = 1;
@@ -3629,15 +3634,36 @@  int main(int argc, char **argv, char **envp)
         exit(1);
     }
 
+    accel = qemu_opt_get(machine_opts, "accel");
+    if (accel) {
+        if (!strcmp(accel, "kvm")) {
+            kvm_allowed = 1;
+        } else if (!strcmp(accel, "tcg")) {
+            kvm_allowed = 0;
+        } else if (!strcmp(accel, "kvm|tcg")) {
+            kvm_allowed = 1;
+            tcg_fallback = 1;
+        } else {
+            fprintf(stderr, "accel: invalid option %s\n", accel);
+            exit(1);
+        }
+    }
+
     if (kvm_allowed) {
         int ret = kvm_init(smp_cpus);
         if (ret < 0) {
             if (!kvm_available()) {
                 printf("KVM not supported for this target\n");
+                exit(1);
             } else {
                 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
+
+                if (tcg_fallback) {
+                    kvm_allowed = 0;
+                } else {
+                    exit(1);
+                }
             }
-            exit(1);
         }
     }