Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/core/cpu-system.c
Line
Count
Source
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 "system/address-spaces.h"
24
#include "exec/cputlb.h"
25
#include "exec/target_page.h"
26
#include "system/memory.h"
27
#include "qemu/target-info.h"
28
#include "hw/core/boards.h"
29
#include "hw/core/qdev.h"
30
#include "hw/core/qdev-properties.h"
31
#include "hw/core/sysemu-cpu-ops.h"
32
#include "migration/vmstate.h"
33
#include "system/hw_accel.h"
34
#include "system/tcg.h"
35
#include "cpu-internal.h"
36
37
bool cpu_has_work(CPUState *cpu)
38
0
{
39
0
    return cpu->cc->sysemu_ops->has_work(cpu);
40
0
}
41
42
bool cpu_paging_enabled(const CPUState *cpu)
43
0
{
44
0
    if (cpu->cc->sysemu_ops->get_paging_enabled) {
45
0
        return cpu->cc->sysemu_ops->get_paging_enabled(cpu);
46
0
    }
47
48
0
    return false;
49
0
}
50
51
bool cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
52
                            Error **errp)
53
0
{
54
0
    if (cpu->cc->sysemu_ops->get_memory_mapping) {
55
0
        return cpu->cc->sysemu_ops->get_memory_mapping(cpu, list, errp);
56
0
    }
57
58
0
    error_setg(errp, "Obtaining memory mappings is unsupported on this CPU.");
59
0
    return false;
60
0
}
61
62
bool cpu_translate_for_debug(CPUState *cpu, vaddr addr,
63
                             TranslateForDebugResult *result)
64
0
{
65
0
    if (cpu->cc->sysemu_ops->translate_for_debug) {
66
0
        return cpu->cc->sysemu_ops->translate_for_debug(cpu, addr, result);
67
0
    } else {
68
        /* Fallbacks for CPUs which don't implement translate_for_debug */
69
0
        result->physaddr = cpu->cc->sysemu_ops->get_phys_addr_debug(cpu, addr);
70
0
        result->attrs = MEMTXATTRS_UNSPECIFIED;
71
0
        if (result->physaddr == -1) {
72
0
            return false;
73
0
        }
74
        /* Indicate that this is a debug access. */
75
0
        result->attrs.debug = 1;
76
        /*
77
         * Assume memory access permissions are valid for the whole page.
78
         * Targets where this isn't true should implement the
79
         * translate_for_debug method.
80
         */
81
0
        result->lg_page_size = TARGET_PAGE_BITS;
82
0
        return true;
83
0
    }
84
0
}
85
86
int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs)
87
0
{
88
0
    int ret = 0;
89
90
0
    if (cpu->cc->sysemu_ops->asidx_from_attrs) {
91
0
        ret = cpu->cc->sysemu_ops->asidx_from_attrs(cpu, attrs);
92
0
        assert(ret <= cpu->cc->max_as && ret >= 0);
93
0
    }
94
0
    return ret;
95
0
}
96
97
int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
98
                             void *opaque)
99
0
{
100
0
    if (!cpu->cc->sysemu_ops->write_elf32_qemunote) {
101
0
        return 0;
102
0
    }
103
0
    return (*cpu->cc->sysemu_ops->write_elf32_qemunote)(f, cpu, opaque);
104
0
}
105
106
int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
107
                         int cpuid, void *opaque)
108
0
{
109
0
    if (!cpu->cc->sysemu_ops->write_elf32_note) {
110
0
        return -1;
111
0
    }
112
0
    return (*cpu->cc->sysemu_ops->write_elf32_note)(f, cpu, cpuid, opaque);
113
0
}
114
115
int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
116
                             void *opaque)
117
0
{
118
0
    if (!cpu->cc->sysemu_ops->write_elf64_qemunote) {
119
0
        return 0;
120
0
    }
121
0
    return (*cpu->cc->sysemu_ops->write_elf64_qemunote)(f, cpu, opaque);
122
0
}
123
124
int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
125
                         int cpuid, void *opaque)
126
0
{
127
0
    if (!cpu->cc->sysemu_ops->write_elf64_note) {
128
0
        return -1;
129
0
    }
130
0
    return (*cpu->cc->sysemu_ops->write_elf64_note)(f, cpu, cpuid, opaque);
131
0
}
132
133
int cpu_get_dump_info(const CPUState *cpu, ArchDumpInfo *info,
134
                      const GuestPhysBlockList *guest_phys_blocks)
135
0
{
136
0
    if (!cpu->cc->sysemu_ops->get_dump_info) {
137
0
        return -1;
138
0
    }
139
0
    cpu->cc->sysemu_ops->get_dump_info(cpu, info, guest_phys_blocks);
140
0
    return 0;
141
0
}
142
143
ssize_t cpu_get_note_size(const CPUState *cpu, int class, int machine,
144
                          int nr_cpus)
145
0
{
146
0
    if (!cpu->cc->sysemu_ops->get_note_size) {
147
0
        return -1;
148
0
    }
149
0
    return cpu->cc->sysemu_ops->get_note_size(cpu, class, machine, nr_cpus);
150
0
}
151
152
bool cpu_internal_is_big_endian(CPUState *cpu)
153
0
{
154
0
    if (cpu->cc->sysemu_ops->internal_is_big_endian) {
155
0
        return cpu->cc->sysemu_ops->internal_is_big_endian(cpu);
156
0
    }
157
0
    return target_big_endian();
158
0
}
159
160
GuestPanicInformation *cpu_get_crash_info(CPUState *cpu)
161
0
{
162
0
    GuestPanicInformation *res = NULL;
163
164
0
    if (cpu->cc->sysemu_ops->get_crash_info) {
165
0
        res = cpu->cc->sysemu_ops->get_crash_info(cpu);
166
0
    }
167
0
    return res;
168
0
}
169
170
static const Property cpu_system_props[] = {
171
    /*
172
     * Create a memory property for system CPU object, so users can
173
     * wire up its memory.  The default if no link is set up is to use
174
     * the system address space.
175
     */
176
    DEFINE_PROP_LINK("memory", CPUState, memory, TYPE_MEMORY_REGION,
177
                     MemoryRegion *),
178
};
179
180
static bool cpu_get_start_powered_off(Object *obj, Error **errp)
181
0
{
182
0
    CPUState *cpu = CPU(obj);
183
0
    return cpu->start_powered_off;
184
0
}
185
186
static void cpu_set_start_powered_off(Object *obj, bool value, Error **errp)
187
0
{
188
0
    CPUState *cpu = CPU(obj);
189
0
    cpu->start_powered_off = value;
190
0
}
191
192
void cpu_class_init_props(DeviceClass *dc)
193
61
{
194
61
    ObjectClass *oc = OBJECT_CLASS(dc);
195
196
    /*
197
     * We can't use DEFINE_PROP_BOOL in the Property array for this
198
     * property, because we want this to be settable after realize.
199
     */
200
61
    object_class_property_add_bool(oc, "start-powered-off",
201
61
                                   cpu_get_start_powered_off,
202
61
                                   cpu_set_start_powered_off);
203
204
61
    device_class_set_props(dc, cpu_system_props);
205
61
}
206
207
void cpu_exec_class_post_init(CPUClass *cc)
208
61
{
209
    /* Check mandatory SysemuCPUOps handlers */
210
61
    g_assert(cc->sysemu_ops->has_work);
211
61
}
212
213
void cpu_exec_init(CPUState *cpu)
214
61
{
215
61
    cpu->memory = get_system_memory();
216
61
    object_ref(OBJECT(cpu->memory));
217
61
}
218
219
static int cpu_common_post_load(void *opaque, int version_id)
220
0
{
221
0
    if (tcg_enabled()) {
222
0
        CPUState *cpu = opaque;
223
224
        /*
225
         * 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
226
         * version_id is increased.
227
         */
228
0
        cpu_reset_interrupt(cpu, 0x01);
229
230
0
        tlb_flush(cpu);
231
0
    }
232
233
0
    return 0;
234
0
}
235
236
static int cpu_common_pre_load(void *opaque)
237
0
{
238
0
    CPUState *cpu = opaque;
239
240
0
    cpu->exception_index = -1;
241
242
0
    return 0;
243
0
}
244
245
void cpu_exec_realize(CPUState *cpu, Error **errp)
246
61
{
247
61
    Object *machine = qdev_get_machine();
248
249
    /*
250
     * qdev_get_machine() can return something that's not TYPE_MACHINE
251
     * if this is one of the user-only emulators; in that case there's
252
     * no need to check the ignore_memory_transaction_failures board flag.
253
     */
254
61
    if (object_dynamic_cast(machine, TYPE_MACHINE)) {
255
61
        MachineClass *mc = MACHINE_GET_CLASS(machine);
256
257
61
        if (mc) {
258
61
            cpu->ignore_memory_transaction_failures =
259
61
                mc->ignore_memory_transaction_failures;
260
61
        }
261
61
    }
262
263
61
    if (DEVICE(cpu)->hotplugged) {
264
0
        cpu_synchronize_post_init(cpu);
265
0
        cpu_resume(cpu);
266
0
    }
267
268
61
}
269
270
static bool cpu_common_exception_index_needed(void *opaque)
271
0
{
272
0
    CPUState *cpu = opaque;
273
274
0
    return tcg_enabled() && cpu->exception_index != -1;
275
0
}
276
277
static const VMStateDescription vmstate_cpu_common_exception_index = {
278
    .name = "cpu_common/exception_index",
279
    .version_id = 1,
280
    .minimum_version_id = 1,
281
    .needed = cpu_common_exception_index_needed,
282
    .fields = (const VMStateField[]) {
283
        VMSTATE_INT32(exception_index, CPUState),
284
        VMSTATE_END_OF_LIST()
285
    }
286
};
287
288
static bool cpu_common_crash_occurred_needed(void *opaque)
289
0
{
290
0
    CPUState *cpu = opaque;
291
292
0
    return cpu->crash_occurred;
293
0
}
294
295
static const VMStateDescription vmstate_cpu_common_crash_occurred = {
296
    .name = "cpu_common/crash_occurred",
297
    .version_id = 1,
298
    .minimum_version_id = 1,
299
    .needed = cpu_common_crash_occurred_needed,
300
    .fields = (const VMStateField[]) {
301
        VMSTATE_BOOL(crash_occurred, CPUState),
302
        VMSTATE_END_OF_LIST()
303
    }
304
};
305
306
const VMStateDescription vmstate_cpu_common = {
307
    .name = "cpu_common",
308
    .version_id = 1,
309
    .minimum_version_id = 1,
310
    .pre_load = cpu_common_pre_load,
311
    .post_load = cpu_common_post_load,
312
    .fields = (const VMStateField[]) {
313
        VMSTATE_UINT32(halted, CPUState),
314
        VMSTATE_UINT32(interrupt_request, CPUState),
315
        VMSTATE_END_OF_LIST()
316
    },
317
    .subsections = (const VMStateDescription * const []) {
318
        &vmstate_cpu_common_exception_index,
319
        &vmstate_cpu_common_crash_occurred,
320
        NULL
321
    }
322
};
323
324
void cpu_vmstate_register(CPUState *cpu)
325
61
{
326
61
    if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
327
61
        vmstate_register(NULL, cpu->cpu_index, &vmstate_cpu_common, cpu);
328
61
    }
329
61
    if (cpu->cc->sysemu_ops->legacy_vmsd != NULL) {
330
61
        vmstate_register(NULL, cpu->cpu_index,
331
61
                         cpu->cc->sysemu_ops->legacy_vmsd, cpu);
332
61
    }
333
61
}
334
335
void cpu_vmstate_unregister(CPUState *cpu)
336
0
{
337
0
    if (cpu->cc->sysemu_ops->legacy_vmsd != NULL) {
338
0
        vmstate_unregister(NULL, cpu->cc->sysemu_ops->legacy_vmsd, cpu);
339
0
    }
340
0
    if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
341
        vmstate_unregister(NULL, &vmstate_cpu_common, cpu);
342
0
    }
343
0
}