xref: /openbmc/qemu/accel/tcg/tcg-all.c (revision 70b75667)
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 "qemu/osdep.h"
27 #include "qemu-common.h"
28 #include "sysemu/tcg.h"
29 #include "sysemu/cpu-timers.h"
30 #include "tcg/tcg.h"
31 #include "qapi/error.h"
32 #include "qemu/error-report.h"
33 #include "hw/boards.h"
34 #include "qapi/qapi-builtin-visit.h"
35 #include "tcg-cpus.h"
36 
37 struct TCGState {
38     AccelState parent_obj;
39 
40     bool mttcg_enabled;
41     unsigned long tb_size;
42 };
43 typedef struct TCGState TCGState;
44 
45 #define TYPE_TCG_ACCEL ACCEL_CLASS_NAME("tcg")
46 
47 DECLARE_INSTANCE_CHECKER(TCGState, TCG_STATE,
48                          TYPE_TCG_ACCEL)
49 
50 /*
51  * We default to false if we know other options have been enabled
52  * which are currently incompatible with MTTCG. Otherwise when each
53  * guest (target) has been updated to support:
54  *   - atomic instructions
55  *   - memory ordering primitives (barriers)
56  * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
57  *
58  * Once a guest architecture has been converted to the new primitives
59  * there are two remaining limitations to check.
60  *
61  * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
62  * - The host must have a stronger memory order than the guest
63  *
64  * It may be possible in future to support strong guests on weak hosts
65  * but that will require tagging all load/stores in a guest with their
66  * implicit memory order requirements which would likely slow things
67  * down a lot.
68  */
69 
70 static bool check_tcg_memory_orders_compatible(void)
71 {
72 #if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
73     return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0;
74 #else
75     return false;
76 #endif
77 }
78 
79 static bool default_mttcg_enabled(void)
80 {
81     if (icount_enabled() || TCG_OVERSIZED_GUEST) {
82         return false;
83     } else {
84 #ifdef TARGET_SUPPORTS_MTTCG
85         return check_tcg_memory_orders_compatible();
86 #else
87         return false;
88 #endif
89     }
90 }
91 
92 static void tcg_accel_instance_init(Object *obj)
93 {
94     TCGState *s = TCG_STATE(obj);
95 
96     s->mttcg_enabled = default_mttcg_enabled();
97 }
98 
99 bool mttcg_enabled;
100 
101 static int tcg_init(MachineState *ms)
102 {
103     TCGState *s = TCG_STATE(current_accel());
104 
105     tcg_exec_init(s->tb_size * 1024 * 1024);
106     mttcg_enabled = s->mttcg_enabled;
107 
108     /*
109      * Initialize TCG regions
110      */
111     tcg_region_init();
112 
113     if (mttcg_enabled) {
114         cpus_register_accel(&tcg_cpus_mttcg);
115     } else if (icount_enabled()) {
116         cpus_register_accel(&tcg_cpus_icount);
117     } else {
118         cpus_register_accel(&tcg_cpus_rr);
119     }
120     return 0;
121 }
122 
123 static char *tcg_get_thread(Object *obj, Error **errp)
124 {
125     TCGState *s = TCG_STATE(obj);
126 
127     return g_strdup(s->mttcg_enabled ? "multi" : "single");
128 }
129 
130 static void tcg_set_thread(Object *obj, const char *value, Error **errp)
131 {
132     TCGState *s = TCG_STATE(obj);
133 
134     if (strcmp(value, "multi") == 0) {
135         if (TCG_OVERSIZED_GUEST) {
136             error_setg(errp, "No MTTCG when guest word size > hosts");
137         } else if (icount_enabled()) {
138             error_setg(errp, "No MTTCG when icount is enabled");
139         } else {
140 #ifndef TARGET_SUPPORTS_MTTCG
141             warn_report("Guest not yet converted to MTTCG - "
142                         "you may get unexpected results");
143 #endif
144             if (!check_tcg_memory_orders_compatible()) {
145                 warn_report("Guest expects a stronger memory ordering "
146                             "than the host provides");
147                 error_printf("This may cause strange/hard to debug errors\n");
148             }
149             s->mttcg_enabled = true;
150         }
151     } else if (strcmp(value, "single") == 0) {
152         s->mttcg_enabled = false;
153     } else {
154         error_setg(errp, "Invalid 'thread' setting %s", value);
155     }
156 }
157 
158 static void tcg_get_tb_size(Object *obj, Visitor *v,
159                             const char *name, void *opaque,
160                             Error **errp)
161 {
162     TCGState *s = TCG_STATE(obj);
163     uint32_t value = s->tb_size;
164 
165     visit_type_uint32(v, name, &value, errp);
166 }
167 
168 static void tcg_set_tb_size(Object *obj, Visitor *v,
169                             const char *name, void *opaque,
170                             Error **errp)
171 {
172     TCGState *s = TCG_STATE(obj);
173     uint32_t value;
174 
175     if (!visit_type_uint32(v, name, &value, errp)) {
176         return;
177     }
178 
179     s->tb_size = value;
180 }
181 
182 static void tcg_accel_class_init(ObjectClass *oc, void *data)
183 {
184     AccelClass *ac = ACCEL_CLASS(oc);
185     ac->name = "tcg";
186     ac->init_machine = tcg_init;
187     ac->allowed = &tcg_allowed;
188 
189     object_class_property_add_str(oc, "thread",
190                                   tcg_get_thread,
191                                   tcg_set_thread);
192 
193     object_class_property_add(oc, "tb-size", "int",
194         tcg_get_tb_size, tcg_set_tb_size,
195         NULL, NULL);
196     object_class_property_set_description(oc, "tb-size",
197         "TCG translation block cache size");
198 
199 }
200 
201 static const TypeInfo tcg_accel_type = {
202     .name = TYPE_TCG_ACCEL,
203     .parent = TYPE_ACCEL,
204     .instance_init = tcg_accel_instance_init,
205     .class_init = tcg_accel_class_init,
206     .instance_size = sizeof(TCGState),
207 };
208 
209 static void register_accel_types(void)
210 {
211     type_register_static(&tcg_accel_type);
212 }
213 
214 type_init(register_accel_types);
215