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