xref: /qemu/hw/core/cpu-system.c (revision 30e76638eb35ffe88e95cca2b5af952c14dc336d)
1 /*
2  * QEMU CPU model (system specific)
3  *
4  * Copyright (c) 2012-2014 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 "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "exec/address-spaces.h"
24 #include "exec/cputlb.h"
25 #include "exec/memory.h"
26 #include "exec/tb-flush.h"
27 #include "exec/tswap.h"
28 #include "hw/qdev-core.h"
29 #include "hw/qdev-properties.h"
30 #include "hw/core/sysemu-cpu-ops.h"
31 #include "migration/vmstate.h"
32 #include "system/tcg.h"
33 
34 bool cpu_paging_enabled(const CPUState *cpu)
35 {
36     if (cpu->cc->sysemu_ops->get_paging_enabled) {
37         return cpu->cc->sysemu_ops->get_paging_enabled(cpu);
38     }
39 
40     return false;
41 }
42 
43 bool cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
44                             Error **errp)
45 {
46     if (cpu->cc->sysemu_ops->get_memory_mapping) {
47         return cpu->cc->sysemu_ops->get_memory_mapping(cpu, list, errp);
48     }
49 
50     error_setg(errp, "Obtaining memory mappings is unsupported on this CPU.");
51     return false;
52 }
53 
54 hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
55                                      MemTxAttrs *attrs)
56 {
57     hwaddr paddr;
58 
59     if (cpu->cc->sysemu_ops->get_phys_page_attrs_debug) {
60         paddr = cpu->cc->sysemu_ops->get_phys_page_attrs_debug(cpu, addr,
61                                                                attrs);
62     } else {
63         /* Fallback for CPUs which don't implement the _attrs_ hook */
64         *attrs = MEMTXATTRS_UNSPECIFIED;
65         paddr = cpu->cc->sysemu_ops->get_phys_page_debug(cpu, addr);
66     }
67     /* Indicate that this is a debug access. */
68     attrs->debug = 1;
69     return paddr;
70 }
71 
72 hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr)
73 {
74     MemTxAttrs attrs = {};
75 
76     return cpu_get_phys_page_attrs_debug(cpu, addr, &attrs);
77 }
78 
79 int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs)
80 {
81     int ret = 0;
82 
83     if (cpu->cc->sysemu_ops->asidx_from_attrs) {
84         ret = cpu->cc->sysemu_ops->asidx_from_attrs(cpu, attrs);
85         assert(ret < cpu->num_ases && ret >= 0);
86     }
87     return ret;
88 }
89 
90 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
91                              void *opaque)
92 {
93     if (!cpu->cc->sysemu_ops->write_elf32_qemunote) {
94         return 0;
95     }
96     return (*cpu->cc->sysemu_ops->write_elf32_qemunote)(f, cpu, opaque);
97 }
98 
99 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
100                          int cpuid, void *opaque)
101 {
102     if (!cpu->cc->sysemu_ops->write_elf32_note) {
103         return -1;
104     }
105     return (*cpu->cc->sysemu_ops->write_elf32_note)(f, cpu, cpuid, opaque);
106 }
107 
108 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
109                              void *opaque)
110 {
111     if (!cpu->cc->sysemu_ops->write_elf64_qemunote) {
112         return 0;
113     }
114     return (*cpu->cc->sysemu_ops->write_elf64_qemunote)(f, cpu, opaque);
115 }
116 
117 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
118                          int cpuid, void *opaque)
119 {
120     if (!cpu->cc->sysemu_ops->write_elf64_note) {
121         return -1;
122     }
123     return (*cpu->cc->sysemu_ops->write_elf64_note)(f, cpu, cpuid, opaque);
124 }
125 
126 bool cpu_virtio_is_big_endian(CPUState *cpu)
127 {
128     if (cpu->cc->sysemu_ops->virtio_is_big_endian) {
129         return cpu->cc->sysemu_ops->virtio_is_big_endian(cpu);
130     }
131     return target_words_bigendian();
132 }
133 
134 GuestPanicInformation *cpu_get_crash_info(CPUState *cpu)
135 {
136     GuestPanicInformation *res = NULL;
137 
138     if (cpu->cc->sysemu_ops->get_crash_info) {
139         res = cpu->cc->sysemu_ops->get_crash_info(cpu);
140     }
141     return res;
142 }
143 
144 static const Property cpu_system_props[] = {
145     /*
146      * Create a memory property for system CPU object, so users can
147      * wire up its memory.  The default if no link is set up is to use
148      * the system address space.
149      */
150     DEFINE_PROP_LINK("memory", CPUState, memory, TYPE_MEMORY_REGION,
151                      MemoryRegion *),
152 };
153 
154 static bool cpu_get_start_powered_off(Object *obj, Error **errp)
155 {
156     CPUState *cpu = CPU(obj);
157     return cpu->start_powered_off;
158 }
159 
160 static void cpu_set_start_powered_off(Object *obj, bool value, Error **errp)
161 {
162     CPUState *cpu = CPU(obj);
163     cpu->start_powered_off = value;
164 }
165 
166 void cpu_class_init_props(DeviceClass *dc)
167 {
168     ObjectClass *oc = OBJECT_CLASS(dc);
169 
170     /*
171      * We can't use DEFINE_PROP_BOOL in the Property array for this
172      * property, because we want this to be settable after realize.
173      */
174     object_class_property_add_bool(oc, "start-powered-off",
175                                    cpu_get_start_powered_off,
176                                    cpu_set_start_powered_off);
177 
178     device_class_set_props(dc, cpu_system_props);
179 }
180 
181 void cpu_exec_initfn(CPUState *cpu)
182 {
183     cpu->memory = get_system_memory();
184     object_ref(OBJECT(cpu->memory));
185 }
186 
187 static int cpu_common_post_load(void *opaque, int version_id)
188 {
189     if (tcg_enabled()) {
190         CPUState *cpu = opaque;
191 
192         /*
193          * 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
194          * version_id is increased.
195          */
196         cpu->interrupt_request &= ~0x01;
197 
198         tlb_flush(cpu);
199 
200         /*
201          * loadvm has just updated the content of RAM, bypassing the
202          * usual mechanisms that ensure we flush TBs for writes to
203          * memory we've translated code from. So we must flush all TBs,
204          * which will now be stale.
205          */
206         tb_flush(cpu);
207     }
208 
209     return 0;
210 }
211 
212 static int cpu_common_pre_load(void *opaque)
213 {
214     CPUState *cpu = opaque;
215 
216     cpu->exception_index = -1;
217 
218     return 0;
219 }
220 
221 static bool cpu_common_exception_index_needed(void *opaque)
222 {
223     CPUState *cpu = opaque;
224 
225     return tcg_enabled() && cpu->exception_index != -1;
226 }
227 
228 static const VMStateDescription vmstate_cpu_common_exception_index = {
229     .name = "cpu_common/exception_index",
230     .version_id = 1,
231     .minimum_version_id = 1,
232     .needed = cpu_common_exception_index_needed,
233     .fields = (const VMStateField[]) {
234         VMSTATE_INT32(exception_index, CPUState),
235         VMSTATE_END_OF_LIST()
236     }
237 };
238 
239 static bool cpu_common_crash_occurred_needed(void *opaque)
240 {
241     CPUState *cpu = opaque;
242 
243     return cpu->crash_occurred;
244 }
245 
246 static const VMStateDescription vmstate_cpu_common_crash_occurred = {
247     .name = "cpu_common/crash_occurred",
248     .version_id = 1,
249     .minimum_version_id = 1,
250     .needed = cpu_common_crash_occurred_needed,
251     .fields = (const VMStateField[]) {
252         VMSTATE_BOOL(crash_occurred, CPUState),
253         VMSTATE_END_OF_LIST()
254     }
255 };
256 
257 const VMStateDescription vmstate_cpu_common = {
258     .name = "cpu_common",
259     .version_id = 1,
260     .minimum_version_id = 1,
261     .pre_load = cpu_common_pre_load,
262     .post_load = cpu_common_post_load,
263     .fields = (const VMStateField[]) {
264         VMSTATE_UINT32(halted, CPUState),
265         VMSTATE_UINT32(interrupt_request, CPUState),
266         VMSTATE_END_OF_LIST()
267     },
268     .subsections = (const VMStateDescription * const []) {
269         &vmstate_cpu_common_exception_index,
270         &vmstate_cpu_common_crash_occurred,
271         NULL
272     }
273 };
274 
275 void cpu_vmstate_register(CPUState *cpu)
276 {
277     if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
278         vmstate_register(NULL, cpu->cpu_index, &vmstate_cpu_common, cpu);
279     }
280     if (cpu->cc->sysemu_ops->legacy_vmsd != NULL) {
281         vmstate_register(NULL, cpu->cpu_index,
282                          cpu->cc->sysemu_ops->legacy_vmsd, cpu);
283     }
284 }
285 
286 void cpu_vmstate_unregister(CPUState *cpu)
287 {
288     if (cpu->cc->sysemu_ops->legacy_vmsd != NULL) {
289         vmstate_unregister(NULL, cpu->cc->sysemu_ops->legacy_vmsd, cpu);
290     }
291     if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
292         vmstate_unregister(NULL, &vmstate_cpu_common, cpu);
293     }
294 }
295