xref: /qemu/hw/core/cpu-system.c (revision 1405d7e60d8c98a28b29885f70da4f2e4407fbc6)
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/memory.h"
25 #include "exec/tswap.h"
26 #include "hw/qdev-core.h"
27 #include "hw/qdev-properties.h"
28 #include "hw/core/sysemu-cpu-ops.h"
29 
30 bool cpu_paging_enabled(const CPUState *cpu)
31 {
32     CPUClass *cc = CPU_GET_CLASS(cpu);
33 
34     if (cc->sysemu_ops->get_paging_enabled) {
35         return cc->sysemu_ops->get_paging_enabled(cpu);
36     }
37 
38     return false;
39 }
40 
41 bool cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
42                             Error **errp)
43 {
44     CPUClass *cc = CPU_GET_CLASS(cpu);
45 
46     if (cc->sysemu_ops->get_memory_mapping) {
47         return 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     CPUClass *cc = CPU_GET_CLASS(cpu);
58     hwaddr paddr;
59 
60     if (cc->sysemu_ops->get_phys_page_attrs_debug) {
61         paddr = cc->sysemu_ops->get_phys_page_attrs_debug(cpu, addr, attrs);
62     } else {
63         /* Fallback for CPUs which don't implement the _attrs_ hook */
64         *attrs = MEMTXATTRS_UNSPECIFIED;
65         paddr = 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     CPUClass *cc = CPU_GET_CLASS(cpu);
94 
95     if (!cc->sysemu_ops->write_elf32_qemunote) {
96         return 0;
97     }
98     return (*cc->sysemu_ops->write_elf32_qemunote)(f, cpu, opaque);
99 }
100 
101 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
102                          int cpuid, void *opaque)
103 {
104     CPUClass *cc = CPU_GET_CLASS(cpu);
105 
106     if (!cc->sysemu_ops->write_elf32_note) {
107         return -1;
108     }
109     return (*cc->sysemu_ops->write_elf32_note)(f, cpu, cpuid, opaque);
110 }
111 
112 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
113                              void *opaque)
114 {
115     CPUClass *cc = CPU_GET_CLASS(cpu);
116 
117     if (!cc->sysemu_ops->write_elf64_qemunote) {
118         return 0;
119     }
120     return (*cc->sysemu_ops->write_elf64_qemunote)(f, cpu, opaque);
121 }
122 
123 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
124                          int cpuid, void *opaque)
125 {
126     CPUClass *cc = CPU_GET_CLASS(cpu);
127 
128     if (!cc->sysemu_ops->write_elf64_note) {
129         return -1;
130     }
131     return (*cc->sysemu_ops->write_elf64_note)(f, cpu, cpuid, opaque);
132 }
133 
134 bool cpu_virtio_is_big_endian(CPUState *cpu)
135 {
136     CPUClass *cc = CPU_GET_CLASS(cpu);
137 
138     if (cc->sysemu_ops->virtio_is_big_endian) {
139         return cc->sysemu_ops->virtio_is_big_endian(cpu);
140     }
141     return target_words_bigendian();
142 }
143 
144 GuestPanicInformation *cpu_get_crash_info(CPUState *cpu)
145 {
146     CPUClass *cc = CPU_GET_CLASS(cpu);
147     GuestPanicInformation *res = NULL;
148 
149     if (cc->sysemu_ops->get_crash_info) {
150         res = cc->sysemu_ops->get_crash_info(cpu);
151     }
152     return res;
153 }
154 
155 static const Property cpu_system_props[] = {
156     /*
157      * Create a memory property for system CPU object, so users can
158      * wire up its memory.  The default if no link is set up is to use
159      * the system address space.
160      */
161     DEFINE_PROP_LINK("memory", CPUState, memory, TYPE_MEMORY_REGION,
162                      MemoryRegion *),
163 };
164 
165 static bool cpu_get_start_powered_off(Object *obj, Error **errp)
166 {
167     CPUState *cpu = CPU(obj);
168     return cpu->start_powered_off;
169 }
170 
171 static void cpu_set_start_powered_off(Object *obj, bool value, Error **errp)
172 {
173     CPUState *cpu = CPU(obj);
174     cpu->start_powered_off = value;
175 }
176 
177 void cpu_class_init_props(DeviceClass *dc)
178 {
179     ObjectClass *oc = OBJECT_CLASS(dc);
180 
181     /*
182      * We can't use DEFINE_PROP_BOOL in the Property array for this
183      * property, because we want this to be settable after realize.
184      */
185     object_class_property_add_bool(oc, "start-powered-off",
186                                    cpu_get_start_powered_off,
187                                    cpu_set_start_powered_off);
188 
189     device_class_set_props(dc, cpu_system_props);
190 }
191 
192 void cpu_exec_initfn(CPUState *cpu)
193 {
194     cpu->memory = get_system_memory();
195     object_ref(OBJECT(cpu->memory));
196 }
197