4 * Copyright (c) 2012 SUSE LINUX Products GmbH
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.
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.
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>
22 #include "qemu-common.h"
23 #include "sysemu/kvm.h"
24 #include "qemu/notify.h"
25 #include "sysemu/sysemu.h"
27 typedef struct CPUExistsArgs
{
32 static void cpu_exist_cb(CPUState
*cpu
, void *data
)
34 CPUClass
*klass
= CPU_GET_CLASS(cpu
);
35 CPUExistsArgs
*arg
= data
;
37 if (klass
->get_arch_id(cpu
) == arg
->id
) {
42 bool cpu_exists(int64_t id
)
44 CPUExistsArgs data
= {
49 qemu_for_each_cpu(cpu_exist_cb
, &data
);
53 bool cpu_paging_enabled(const CPUState
*cpu
)
55 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
57 return cc
->get_paging_enabled(cpu
);
60 static bool cpu_common_get_paging_enabled(const CPUState
*cpu
)
65 void cpu_get_memory_mapping(CPUState
*cpu
, MemoryMappingList
*list
,
68 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
70 return cc
->get_memory_mapping(cpu
, list
, errp
);
73 static void cpu_common_get_memory_mapping(CPUState
*cpu
,
74 MemoryMappingList
*list
,
77 error_setg(errp
, "Obtaining memory mappings is unsupported on this CPU.");
80 /* CPU hot-plug notifiers */
81 static NotifierList cpu_added_notifiers
=
82 NOTIFIER_LIST_INITIALIZER(cpu_add_notifiers
);
84 void qemu_register_cpu_added_notifier(Notifier
*notifier
)
86 notifier_list_add(&cpu_added_notifiers
, notifier
);
89 void cpu_reset_interrupt(CPUState
*cpu
, int mask
)
91 cpu
->interrupt_request
&= ~mask
;
94 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
97 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
99 return (*cc
->write_elf32_qemunote
)(f
, cpu
, opaque
);
102 static int cpu_common_write_elf32_qemunote(WriteCoreDumpFunction f
,
103 CPUState
*cpu
, void *opaque
)
108 int cpu_write_elf32_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
109 int cpuid
, void *opaque
)
111 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
113 return (*cc
->write_elf32_note
)(f
, cpu
, cpuid
, opaque
);
116 static int cpu_common_write_elf32_note(WriteCoreDumpFunction f
,
117 CPUState
*cpu
, int cpuid
,
123 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
126 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
128 return (*cc
->write_elf64_qemunote
)(f
, cpu
, opaque
);
131 static int cpu_common_write_elf64_qemunote(WriteCoreDumpFunction f
,
132 CPUState
*cpu
, void *opaque
)
137 int cpu_write_elf64_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
138 int cpuid
, void *opaque
)
140 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
142 return (*cc
->write_elf64_note
)(f
, cpu
, cpuid
, opaque
);
145 static int cpu_common_write_elf64_note(WriteCoreDumpFunction f
,
146 CPUState
*cpu
, int cpuid
,
153 void cpu_reset(CPUState
*cpu
)
155 CPUClass
*klass
= CPU_GET_CLASS(cpu
);
157 if (klass
->reset
!= NULL
) {
158 (*klass
->reset
)(cpu
);
162 static void cpu_common_reset(CPUState
*cpu
)
164 cpu
->exit_request
= 0;
165 cpu
->interrupt_request
= 0;
166 cpu
->current_tb
= NULL
;
170 ObjectClass
*cpu_class_by_name(const char *typename
, const char *cpu_model
)
172 CPUClass
*cc
= CPU_CLASS(object_class_by_name(typename
));
174 return cc
->class_by_name(cpu_model
);
177 static ObjectClass
*cpu_common_class_by_name(const char *cpu_model
)
182 static void cpu_common_realizefn(DeviceState
*dev
, Error
**errp
)
184 CPUState
*cpu
= CPU(dev
);
186 if (dev
->hotplugged
) {
187 cpu_synchronize_post_init(cpu
);
188 notifier_list_notify(&cpu_added_notifiers
, dev
);
193 static int64_t cpu_common_get_arch_id(CPUState
*cpu
)
195 return cpu
->cpu_index
;
198 static void cpu_class_init(ObjectClass
*klass
, void *data
)
200 DeviceClass
*dc
= DEVICE_CLASS(klass
);
201 CPUClass
*k
= CPU_CLASS(klass
);
203 k
->class_by_name
= cpu_common_class_by_name
;
204 k
->reset
= cpu_common_reset
;
205 k
->get_arch_id
= cpu_common_get_arch_id
;
206 k
->get_paging_enabled
= cpu_common_get_paging_enabled
;
207 k
->get_memory_mapping
= cpu_common_get_memory_mapping
;
208 k
->write_elf32_qemunote
= cpu_common_write_elf32_qemunote
;
209 k
->write_elf32_note
= cpu_common_write_elf32_note
;
210 k
->write_elf64_qemunote
= cpu_common_write_elf64_qemunote
;
211 k
->write_elf64_note
= cpu_common_write_elf64_note
;
212 dc
->realize
= cpu_common_realizefn
;
216 static const TypeInfo cpu_type_info
= {
218 .parent
= TYPE_DEVICE
,
219 .instance_size
= sizeof(CPUState
),
221 .class_size
= sizeof(CPUClass
),
222 .class_init
= cpu_class_init
,
225 static void cpu_register_types(void)
227 type_register_static(&cpu_type_info
);
230 type_init(cpu_register_types
)