xref: /qemu/hw/core/cpu-system.c (revision e3a575f5609569400da628d384b32f5e3cf58745)
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/memory.h"
24 #include "exec/tswap.h"
25 #include "hw/qdev-core.h"
26 #include "hw/qdev-properties.h"
27 #include "hw/core/sysemu-cpu-ops.h"
28 
29 bool cpu_paging_enabled(const CPUState *cpu)
30 {
31     CPUClass *cc = CPU_GET_CLASS(cpu);
32 
33     if (cc->sysemu_ops->get_paging_enabled) {
34         return cc->sysemu_ops->get_paging_enabled(cpu);
35     }
36 
37     return false;
38 }
39 
40 bool cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
41                             Error **errp)
42 {
43     CPUClass *cc = CPU_GET_CLASS(cpu);
44 
45     if (cc->sysemu_ops->get_memory_mapping) {
46         return cc->sysemu_ops->get_memory_mapping(cpu, list, errp);
47     }
48 
49     error_setg(errp, "Obtaining memory mappings is unsupported on this CPU.");
50     return false;
51 }
52 
53 hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
54                                      MemTxAttrs *attrs)
55 {
56     CPUClass *cc = CPU_GET_CLASS(cpu);
57     hwaddr paddr;
58 
59     if (cc->sysemu_ops->get_phys_page_attrs_debug) {
60         paddr = cc->sysemu_ops->get_phys_page_attrs_debug(cpu, addr, attrs);
61     } else {
62         /* Fallback for CPUs which don't implement the _attrs_ hook */
63         *attrs = MEMTXATTRS_UNSPECIFIED;
64         paddr = cc->sysemu_ops->get_phys_page_debug(cpu, addr);
65     }
66     /* Indicate that this is a debug access. */
67     attrs->debug = 1;
68     return paddr;
69 }
70 
71 hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr)
72 {
73     MemTxAttrs attrs = {};
74 
75     return cpu_get_phys_page_attrs_debug(cpu, addr, &attrs);
76 }
77 
78 int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs)
79 {
80     int ret = 0;
81 
82     if (cpu->cc->sysemu_ops->asidx_from_attrs) {
83         ret = cpu->cc->sysemu_ops->asidx_from_attrs(cpu, attrs);
84         assert(ret < cpu->num_ases && ret >= 0);
85     }
86     return ret;
87 }
88 
89 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
90                              void *opaque)
91 {
92     CPUClass *cc = CPU_GET_CLASS(cpu);
93 
94     if (!cc->sysemu_ops->write_elf32_qemunote) {
95         return 0;
96     }
97     return (*cc->sysemu_ops->write_elf32_qemunote)(f, cpu, opaque);
98 }
99 
100 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
101                          int cpuid, void *opaque)
102 {
103     CPUClass *cc = CPU_GET_CLASS(cpu);
104 
105     if (!cc->sysemu_ops->write_elf32_note) {
106         return -1;
107     }
108     return (*cc->sysemu_ops->write_elf32_note)(f, cpu, cpuid, opaque);
109 }
110 
111 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
112                              void *opaque)
113 {
114     CPUClass *cc = CPU_GET_CLASS(cpu);
115 
116     if (!cc->sysemu_ops->write_elf64_qemunote) {
117         return 0;
118     }
119     return (*cc->sysemu_ops->write_elf64_qemunote)(f, cpu, opaque);
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     if (!cc->sysemu_ops->write_elf64_note) {
128         return -1;
129     }
130     return (*cc->sysemu_ops->write_elf64_note)(f, cpu, cpuid, opaque);
131 }
132 
133 bool cpu_virtio_is_big_endian(CPUState *cpu)
134 {
135     CPUClass *cc = CPU_GET_CLASS(cpu);
136 
137     if (cc->sysemu_ops->virtio_is_big_endian) {
138         return cc->sysemu_ops->virtio_is_big_endian(cpu);
139     }
140     return target_words_bigendian();
141 }
142 
143 GuestPanicInformation *cpu_get_crash_info(CPUState *cpu)
144 {
145     CPUClass *cc = CPU_GET_CLASS(cpu);
146     GuestPanicInformation *res = NULL;
147 
148     if (cc->sysemu_ops->get_crash_info) {
149         res = cc->sysemu_ops->get_crash_info(cpu);
150     }
151     return res;
152 }
153 
154 static const Property cpu_system_props[] = {
155     /*
156      * Create a memory property for system CPU object, so users can
157      * wire up its memory.  The default if no link is set up is to use
158      * the system address space.
159      */
160     DEFINE_PROP_LINK("memory", CPUState, memory, TYPE_MEMORY_REGION,
161                      MemoryRegion *),
162 };
163 
164 static bool cpu_get_start_powered_off(Object *obj, Error **errp)
165 {
166     CPUState *cpu = CPU(obj);
167     return cpu->start_powered_off;
168 }
169 
170 static void cpu_set_start_powered_off(Object *obj, bool value, Error **errp)
171 {
172     CPUState *cpu = CPU(obj);
173     cpu->start_powered_off = value;
174 }
175 
176 void cpu_class_init_props(DeviceClass *dc)
177 {
178     ObjectClass *oc = OBJECT_CLASS(dc);
179 
180     /*
181      * We can't use DEFINE_PROP_BOOL in the Property array for this
182      * property, because we want this to be settable after realize.
183      */
184     object_class_property_add_bool(oc, "start-powered-off",
185                                    cpu_get_start_powered_off,
186                                    cpu_set_start_powered_off);
187 
188     device_class_set_props(dc, cpu_system_props);
189 }
190