]> Git Repo - qemu.git/blob - accel.c
accel: Simplify configure_accelerator() using AccelType *acc variable
[qemu.git] / accel.c
1 /*
2  * QEMU System Emulator, accelerator interfaces
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  * Copyright (c) 2014 Red Hat Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25
26 #include "sysemu/accel.h"
27 #include "qemu-common.h"
28 #include "sysemu/arch_init.h"
29 #include "sysemu/sysemu.h"
30 #include "sysemu/kvm.h"
31 #include "sysemu/qtest.h"
32 #include "hw/xen/xen.h"
33
34 int tcg_tb_size;
35 static bool tcg_allowed = true;
36
37 static int tcg_init(MachineClass *mc)
38 {
39     tcg_exec_init(tcg_tb_size * 1024 * 1024);
40     return 0;
41 }
42
43 typedef struct AccelType {
44     const char *opt_name;
45     const char *name;
46     int (*available)(void);
47     int (*init)(MachineClass *mc);
48     bool *allowed;
49 } AccelType;
50
51 static AccelType accel_list[] = {
52     { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
53     { "xen", "Xen", xen_available, xen_init, &xen_allowed },
54     { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
55     { "qtest", "QTest", qtest_available, qtest_init_accel, &qtest_allowed },
56 };
57
58 int configure_accelerator(MachineClass *mc)
59 {
60     const char *p;
61     char buf[10];
62     int i, ret;
63     bool accel_initialised = false;
64     bool init_failed = false;
65     AccelType *acc = NULL;
66
67     p = qemu_opt_get(qemu_get_machine_opts(), "accel");
68     if (p == NULL) {
69         /* Use the default "accelerator", tcg */
70         p = "tcg";
71     }
72
73     while (!accel_initialised && *p != '\0') {
74         if (*p == ':') {
75             p++;
76         }
77         p = get_opt_name(buf, sizeof(buf), p, ':');
78         for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
79             acc = &accel_list[i];
80             if (strcmp(acc->opt_name, buf) == 0) {
81                 if (!acc->available()) {
82                     printf("%s not supported for this target\n",
83                            acc->name);
84                     break;
85                 }
86                 *(acc->allowed) = true;
87                 ret = acc->init(mc);
88                 if (ret < 0) {
89                     init_failed = true;
90                     fprintf(stderr, "failed to initialize %s: %s\n",
91                             acc->name,
92                             strerror(-ret));
93                     *(acc->allowed) = false;
94                 } else {
95                     accel_initialised = true;
96                 }
97                 break;
98             }
99         }
100         if (i == ARRAY_SIZE(accel_list)) {
101             fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
102         }
103     }
104
105     if (!accel_initialised) {
106         if (!init_failed) {
107             fprintf(stderr, "No accelerator found!\n");
108         }
109         exit(1);
110     }
111
112     if (init_failed) {
113         fprintf(stderr, "Back to %s accelerator.\n", acc->name);
114     }
115
116     return !accel_initialised;
117 }
This page took 0.029249 seconds and 4 git commands to generate.