xref: /qemu/accel/accel-target.c (revision 6ff5da16000f908140723e164d33a0b51a6c4162)
1 /*
2  * QEMU accel class, components common to system emulation and user mode
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/accel.h"
28 
29 #include "cpu.h"
30 #include "accel/accel-cpu-target.h"
31 
32 #ifndef CONFIG_USER_ONLY
33 #include "accel-system.h"
34 #endif /* !CONFIG_USER_ONLY */
35 
36 static const TypeInfo accel_type = {
37     .name = TYPE_ACCEL,
38     .parent = TYPE_OBJECT,
39     .class_size = sizeof(AccelClass),
40     .instance_size = sizeof(AccelState),
41     .abstract = true,
42 };
43 
44 /* Lookup AccelClass from opt_name. Returns NULL if not found */
45 AccelClass *accel_find(const char *opt_name)
46 {
47     char *class_name = g_strdup_printf(ACCEL_CLASS_NAME("%s"), opt_name);
48     AccelClass *ac = ACCEL_CLASS(module_object_class_by_name(class_name));
49     g_free(class_name);
50     return ac;
51 }
52 
53 /* Return the name of the current accelerator */
54 const char *current_accel_name(void)
55 {
56     AccelClass *ac = ACCEL_GET_CLASS(current_accel());
57 
58     return ac->name;
59 }
60 
61 static void accel_init_cpu_int_aux(ObjectClass *klass, void *opaque)
62 {
63     CPUClass *cc = CPU_CLASS(klass);
64     AccelCPUClass *accel_cpu = opaque;
65 
66     /*
67      * The first callback allows accel-cpu to run initializations
68      * for the CPU, customizing CPU behavior according to the accelerator.
69      *
70      * The second one allows the CPU to customize the accel-cpu
71      * behavior according to the CPU.
72      *
73      * The second is currently only used by TCG, to specialize the
74      * TCGCPUOps depending on the CPU type.
75      */
76     cc->accel_cpu = accel_cpu;
77     if (accel_cpu->cpu_class_init) {
78         accel_cpu->cpu_class_init(cc);
79     }
80     if (cc->init_accel_cpu) {
81         cc->init_accel_cpu(accel_cpu, cc);
82     }
83 }
84 
85 /* initialize the arch-specific accel CpuClass interfaces */
86 static void accel_init_cpu_interfaces(AccelClass *ac)
87 {
88     const char *ac_name; /* AccelClass name */
89     char *acc_name;      /* AccelCPUClass name */
90     ObjectClass *acc;    /* AccelCPUClass */
91 
92     ac_name = object_class_get_name(OBJECT_CLASS(ac));
93     g_assert(ac_name != NULL);
94 
95     acc_name = g_strdup_printf("%s-%s", ac_name, CPU_RESOLVING_TYPE);
96     acc = object_class_by_name(acc_name);
97     g_free(acc_name);
98 
99     if (acc) {
100         object_class_foreach(accel_init_cpu_int_aux,
101                              CPU_RESOLVING_TYPE, false, acc);
102     }
103 }
104 
105 void accel_init_interfaces(AccelClass *ac)
106 {
107 #ifndef CONFIG_USER_ONLY
108     accel_system_init_ops_interfaces(ac);
109 #endif /* !CONFIG_USER_ONLY */
110 
111     accel_init_cpu_interfaces(ac);
112 }
113 
114 void accel_cpu_instance_init(CPUState *cpu)
115 {
116     CPUClass *cc = CPU_GET_CLASS(cpu);
117 
118     if (cc->accel_cpu && cc->accel_cpu->cpu_instance_init) {
119         cc->accel_cpu->cpu_instance_init(cpu);
120     }
121 }
122 
123 bool accel_cpu_common_realize(CPUState *cpu, Error **errp)
124 {
125     CPUClass *cc = CPU_GET_CLASS(cpu);
126     AccelState *accel = current_accel();
127     AccelClass *acc = ACCEL_GET_CLASS(accel);
128 
129     /* target specific realization */
130     if (cc->accel_cpu && cc->accel_cpu->cpu_target_realize
131         && !cc->accel_cpu->cpu_target_realize(cpu, errp)) {
132         return false;
133     }
134 
135     /* generic realization */
136     if (acc->cpu_common_realize && !acc->cpu_common_realize(cpu, errp)) {
137         return false;
138     }
139 
140     return true;
141 }
142 
143 void accel_cpu_common_unrealize(CPUState *cpu)
144 {
145     AccelState *accel = current_accel();
146     AccelClass *acc = ACCEL_GET_CLASS(accel);
147 
148     /* generic unrealization */
149     if (acc->cpu_common_unrealize) {
150         acc->cpu_common_unrealize(cpu);
151     }
152 }
153 
154 int accel_supported_gdbstub_sstep_flags(void)
155 {
156     AccelState *accel = current_accel();
157     AccelClass *acc = ACCEL_GET_CLASS(accel);
158     if (acc->gdbstub_supported_sstep_flags) {
159         return acc->gdbstub_supported_sstep_flags();
160     }
161     return 0;
162 }
163 
164 static const TypeInfo accel_cpu_type = {
165     .name = TYPE_ACCEL_CPU,
166     .parent = TYPE_OBJECT,
167     .abstract = true,
168     .class_size = sizeof(AccelCPUClass),
169 };
170 
171 static void register_accel_types(void)
172 {
173     type_register_static(&accel_type);
174     type_register_static(&accel_cpu_type);
175 }
176 
177 type_init(register_accel_types);
178