blob: 33a539b4cbbf2defcf3ab1163482fd3637602638 [file] [log] [blame]
Eduardo Habkosta1a9cb02014-09-26 17:45:17 -03001/*
Claudio Fontana940e43a2021-02-04 17:39:24 +01002 * QEMU accel class, components common to system emulation and user mode
Eduardo Habkosta1a9cb02014-09-26 17:45:17 -03003 *
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
Peter Maydelld38ea872016-01-29 17:50:05 +000026#include "qemu/osdep.h"
Claudio Fontana940e43a2021-02-04 17:39:24 +010027#include "qemu/accel.h"
Eduardo Habkosta1a9cb02014-09-26 17:45:17 -030028
Claudio Fontanafb6916d2021-02-04 17:39:26 +010029#include "cpu.h"
Philippe Mathieu-Daudéb12a0f82025-01-23 11:11:24 +010030#include "accel/accel-cpu-target.h"
Claudio Fontanafb6916d2021-02-04 17:39:26 +010031
Claudio Fontanab86f59c2021-02-04 17:39:25 +010032#ifndef CONFIG_USER_ONLY
Philippe Mathieu-Daudé0017c642023-10-04 11:06:21 +020033#include "accel-system.h"
Claudio Fontanab86f59c2021-02-04 17:39:25 +010034#endif /* !CONFIG_USER_ONLY */
35
Eduardo Habkostb14a0b72014-09-26 17:45:21 -030036static const TypeInfo accel_type = {
37 .name = TYPE_ACCEL,
38 .parent = TYPE_OBJECT,
39 .class_size = sizeof(AccelClass),
40 .instance_size = sizeof(AccelState),
Philippe Mathieu-Daudée92a8832020-01-28 18:28:59 +010041 .abstract = true,
Eduardo Habkosta1a9cb02014-09-26 17:45:17 -030042};
43
Eduardo Habkostb14a0b72014-09-26 17:45:21 -030044/* Lookup AccelClass from opt_name. Returns NULL if not found */
Paolo Bonzini28a09612019-11-13 09:59:04 +010045AccelClass *accel_find(const char *opt_name)
Eduardo Habkosta2246552014-09-26 17:45:20 -030046{
Eduardo Habkostb14a0b72014-09-26 17:45:21 -030047 char *class_name = g_strdup_printf(ACCEL_CLASS_NAME("%s"), opt_name);
Gerd Hoffmannf9349072021-06-24 12:38:27 +020048 AccelClass *ac = ACCEL_CLASS(module_object_class_by_name(class_name));
Eduardo Habkostb14a0b72014-09-26 17:45:21 -030049 g_free(class_name);
50 return ac;
Eduardo Habkosta2246552014-09-26 17:45:20 -030051}
52
Alexander Graf55bd4452022-06-24 15:42:56 +010053/* Return the name of the current accelerator */
54const char *current_accel_name(void)
55{
56 AccelClass *ac = ACCEL_GET_CLASS(current_accel());
57
58 return ac->name;
59}
60
Claudio Fontanafb6916d2021-02-04 17:39:26 +010061static void accel_init_cpu_int_aux(ObjectClass *klass, void *opaque)
62{
63 CPUClass *cc = CPU_CLASS(klass);
64 AccelCPUClass *accel_cpu = opaque;
65
Claudio Fontanacc3f2be2021-03-22 14:27:59 +010066 /*
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 */
Claudio Fontanafb6916d2021-02-04 17:39:26 +010076 cc->accel_cpu = accel_cpu;
77 if (accel_cpu->cpu_class_init) {
78 accel_cpu->cpu_class_init(cc);
79 }
Claudio Fontanacc3f2be2021-03-22 14:27:59 +010080 if (cc->init_accel_cpu) {
81 cc->init_accel_cpu(accel_cpu, cc);
82 }
Claudio Fontanafb6916d2021-02-04 17:39:26 +010083}
84
85/* initialize the arch-specific accel CpuClass interfaces */
86static 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
Claudio Fontanab86f59c2021-02-04 17:39:25 +0100105void accel_init_interfaces(AccelClass *ac)
106{
107#ifndef CONFIG_USER_ONLY
Philippe Mathieu-Daudé463b0062024-01-10 10:00:53 +0100108 accel_system_init_ops_interfaces(ac);
Claudio Fontanab86f59c2021-02-04 17:39:25 +0100109#endif /* !CONFIG_USER_ONLY */
Claudio Fontanafb6916d2021-02-04 17:39:26 +0100110
111 accel_init_cpu_interfaces(ac);
Claudio Fontanab86f59c2021-02-04 17:39:25 +0100112}
113
Claudio Fontanabb883fd2021-03-22 14:27:42 +0100114void accel_cpu_instance_init(CPUState *cpu)
115{
Philippe Mathieu-Daudée27fa952025-01-21 12:11:09 +0100116 if (cpu->cc->accel_cpu && cpu->cc->accel_cpu->cpu_instance_init) {
117 cpu->cc->accel_cpu->cpu_instance_init(cpu);
Claudio Fontanabb883fd2021-03-22 14:27:42 +0100118 }
119}
120
Philippe Mathieu-Daudébd684b22023-10-03 14:30:21 +0200121bool accel_cpu_common_realize(CPUState *cpu, Error **errp)
Claudio Fontanabb883fd2021-03-22 14:27:42 +0100122{
Philippe Mathieu-Daudé59851862023-10-03 14:30:23 +0200123 AccelState *accel = current_accel();
124 AccelClass *acc = ACCEL_GET_CLASS(accel);
Claudio Fontanabb883fd2021-03-22 14:27:42 +0100125
Philippe Mathieu-Daudé59851862023-10-03 14:30:23 +0200126 /* target specific realization */
Philippe Mathieu-Daudée27fa952025-01-21 12:11:09 +0100127 if (cpu->cc->accel_cpu
128 && cpu->cc->accel_cpu->cpu_target_realize
129 && !cpu->cc->accel_cpu->cpu_target_realize(cpu, errp)) {
Philippe Mathieu-Daudé59851862023-10-03 14:30:23 +0200130 return false;
Claudio Fontanabb883fd2021-03-22 14:27:42 +0100131 }
Philippe Mathieu-Daudé59851862023-10-03 14:30:23 +0200132
133 /* generic realization */
134 if (acc->cpu_common_realize && !acc->cpu_common_realize(cpu, errp)) {
135 return false;
136 }
137
Claudio Fontana9ea057d2021-03-22 14:27:44 +0100138 return true;
Claudio Fontanabb883fd2021-03-22 14:27:42 +0100139}
140
Philippe Mathieu-Daudé1aa1d832023-10-03 14:30:22 +0200141void accel_cpu_common_unrealize(CPUState *cpu)
142{
Philippe Mathieu-Daudé59851862023-10-03 14:30:23 +0200143 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 }
Philippe Mathieu-Daudé1aa1d832023-10-03 14:30:22 +0200150}
151
Alex Bennée3b7a9382022-09-29 12:42:23 +0100152int 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
Claudio Fontanafb6916d2021-02-04 17:39:26 +0100162static const TypeInfo accel_cpu_type = {
163 .name = TYPE_ACCEL_CPU,
164 .parent = TYPE_OBJECT,
165 .abstract = true,
166 .class_size = sizeof(AccelCPUClass),
167};
168
Eduardo Habkostb14a0b72014-09-26 17:45:21 -0300169static void register_accel_types(void)
170{
171 type_register_static(&accel_type);
Claudio Fontanafb6916d2021-02-04 17:39:26 +0100172 type_register_static(&accel_cpu_type);
Eduardo Habkostb14a0b72014-09-26 17:45:21 -0300173}
174
175type_init(register_accel_types);