xref: /qemu/accel/accel-target.c (revision 892b06c40e46656b07579b9f4fb7a8f2652cacf2)
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     if (cpu->cc->accel_cpu && cpu->cc->accel_cpu->cpu_instance_init) {
117         cpu->cc->accel_cpu->cpu_instance_init(cpu);
118     }
119 }
120 
121 bool accel_cpu_common_realize(CPUState *cpu, Error **errp)
122 {
123     AccelState *accel = current_accel();
124     AccelClass *acc = ACCEL_GET_CLASS(accel);
125 
126     /* target specific realization */
127     if (cpu->cc->accel_cpu
128         && cpu->cc->accel_cpu->cpu_target_realize
129         && !cpu->cc->accel_cpu->cpu_target_realize(cpu, errp)) {
130         return false;
131     }
132 
133     /* generic realization */
134     if (acc->cpu_common_realize && !acc->cpu_common_realize(cpu, errp)) {
135         return false;
136     }
137 
138     return true;
139 }
140 
141 void accel_cpu_common_unrealize(CPUState *cpu)
142 {
143     AccelState *accel = current_accel();
144     AccelClass *acc = ACCEL_GET_CLASS(accel);
145 
146     /* generic unrealization */
147     if (acc->cpu_common_unrealize) {
148         acc->cpu_common_unrealize(cpu);
149     }
150 }
151 
152 int accel_supported_gdbstub_sstep_flags(void)
153 {
154     AccelState *accel = current_accel();
155     AccelClass *acc = ACCEL_GET_CLASS(accel);
156     if (acc->gdbstub_supported_sstep_flags) {
157         return acc->gdbstub_supported_sstep_flags();
158     }
159     return 0;
160 }
161 
162 static const TypeInfo accel_cpu_type = {
163     .name = TYPE_ACCEL_CPU,
164     .parent = TYPE_OBJECT,
165     .abstract = true,
166     .class_size = sizeof(AccelCPUClass),
167 };
168 
169 static void register_accel_types(void)
170 {
171     type_register_static(&accel_type);
172     type_register_static(&accel_cpu_type);
173 }
174 
175 type_init(register_accel_types);
176