]> git.proxmox.com Git - mirror_qemu.git/blob - qom/cpu.c
9f6da0fdd86e982e09bf73448fd4b77a71511baa
[mirror_qemu.git] / qom / cpu.c
1 /*
2 * QEMU CPU model
3 *
4 * Copyright (c) 2012 SUSE LINUX Products GmbH
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see
18 * <http://www.gnu.org/licenses/gpl-2.0.html>
19 */
20
21 #include "qom/cpu.h"
22 #include "qemu-common.h"
23 #include "sysemu/kvm.h"
24 #include "qemu/notify.h"
25 #include "sysemu/sysemu.h"
26
27 typedef struct CPUExistsArgs {
28 int64_t id;
29 bool found;
30 } CPUExistsArgs;
31
32 static void cpu_exist_cb(CPUState *cpu, void *data)
33 {
34 CPUClass *klass = CPU_GET_CLASS(cpu);
35 CPUExistsArgs *arg = data;
36
37 if (klass->get_arch_id(cpu) == arg->id) {
38 arg->found = true;
39 }
40 }
41
42 bool cpu_exists(int64_t id)
43 {
44 CPUExistsArgs data = {
45 .id = id,
46 .found = false,
47 };
48
49 qemu_for_each_cpu(cpu_exist_cb, &data);
50 return data.found;
51 }
52
53 bool cpu_paging_enabled(const CPUState *cpu)
54 {
55 CPUClass *cc = CPU_GET_CLASS(cpu);
56
57 return cc->get_paging_enabled(cpu);
58 }
59
60 static bool cpu_common_get_paging_enabled(const CPUState *cpu)
61 {
62 return true;
63 }
64
65 /* CPU hot-plug notifiers */
66 static NotifierList cpu_added_notifiers =
67 NOTIFIER_LIST_INITIALIZER(cpu_add_notifiers);
68
69 void qemu_register_cpu_added_notifier(Notifier *notifier)
70 {
71 notifier_list_add(&cpu_added_notifiers, notifier);
72 }
73
74 void cpu_reset_interrupt(CPUState *cpu, int mask)
75 {
76 cpu->interrupt_request &= ~mask;
77 }
78
79 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
80 void *opaque)
81 {
82 CPUClass *cc = CPU_GET_CLASS(cpu);
83
84 return (*cc->write_elf32_qemunote)(f, cpu, opaque);
85 }
86
87 static int cpu_common_write_elf32_qemunote(WriteCoreDumpFunction f,
88 CPUState *cpu, void *opaque)
89 {
90 return -1;
91 }
92
93 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
94 int cpuid, void *opaque)
95 {
96 CPUClass *cc = CPU_GET_CLASS(cpu);
97
98 return (*cc->write_elf32_note)(f, cpu, cpuid, opaque);
99 }
100
101 static int cpu_common_write_elf32_note(WriteCoreDumpFunction f,
102 CPUState *cpu, int cpuid,
103 void *opaque)
104 {
105 return -1;
106 }
107
108 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
109 void *opaque)
110 {
111 CPUClass *cc = CPU_GET_CLASS(cpu);
112
113 return (*cc->write_elf64_qemunote)(f, cpu, opaque);
114 }
115
116 static int cpu_common_write_elf64_qemunote(WriteCoreDumpFunction f,
117 CPUState *cpu, void *opaque)
118 {
119 return -1;
120 }
121
122 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
123 int cpuid, void *opaque)
124 {
125 CPUClass *cc = CPU_GET_CLASS(cpu);
126
127 return (*cc->write_elf64_note)(f, cpu, cpuid, opaque);
128 }
129
130 static int cpu_common_write_elf64_note(WriteCoreDumpFunction f,
131 CPUState *cpu, int cpuid,
132 void *opaque)
133 {
134 return -1;
135 }
136
137
138 void cpu_reset(CPUState *cpu)
139 {
140 CPUClass *klass = CPU_GET_CLASS(cpu);
141
142 if (klass->reset != NULL) {
143 (*klass->reset)(cpu);
144 }
145 }
146
147 static void cpu_common_reset(CPUState *cpu)
148 {
149 cpu->exit_request = 0;
150 cpu->interrupt_request = 0;
151 cpu->current_tb = NULL;
152 cpu->halted = 0;
153 }
154
155 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
156 {
157 CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
158
159 return cc->class_by_name(cpu_model);
160 }
161
162 static ObjectClass *cpu_common_class_by_name(const char *cpu_model)
163 {
164 return NULL;
165 }
166
167 static void cpu_common_realizefn(DeviceState *dev, Error **errp)
168 {
169 CPUState *cpu = CPU(dev);
170
171 if (dev->hotplugged) {
172 cpu_synchronize_post_init(cpu);
173 notifier_list_notify(&cpu_added_notifiers, dev);
174 cpu_resume(cpu);
175 }
176 }
177
178 static int64_t cpu_common_get_arch_id(CPUState *cpu)
179 {
180 return cpu->cpu_index;
181 }
182
183 static void cpu_class_init(ObjectClass *klass, void *data)
184 {
185 DeviceClass *dc = DEVICE_CLASS(klass);
186 CPUClass *k = CPU_CLASS(klass);
187
188 k->class_by_name = cpu_common_class_by_name;
189 k->reset = cpu_common_reset;
190 k->get_arch_id = cpu_common_get_arch_id;
191 k->get_paging_enabled = cpu_common_get_paging_enabled;
192 k->write_elf32_qemunote = cpu_common_write_elf32_qemunote;
193 k->write_elf32_note = cpu_common_write_elf32_note;
194 k->write_elf64_qemunote = cpu_common_write_elf64_qemunote;
195 k->write_elf64_note = cpu_common_write_elf64_note;
196 dc->realize = cpu_common_realizefn;
197 dc->no_user = 1;
198 }
199
200 static const TypeInfo cpu_type_info = {
201 .name = TYPE_CPU,
202 .parent = TYPE_DEVICE,
203 .instance_size = sizeof(CPUState),
204 .abstract = true,
205 .class_size = sizeof(CPUClass),
206 .class_init = cpu_class_init,
207 };
208
209 static void cpu_register_types(void)
210 {
211 type_register_static(&cpu_type_info);
212 }
213
214 type_init(cpu_register_types)