xref: /qemu/cpu-target.c (revision b12a0f856691264bc1a8f0ed1e5e62649cea7fd2)
1 /*
2  * Target-specific parts of the CPU object
3  *
4  *  Copyright (c) 2003 Fabrice Bellard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "qapi/error.h"
22 
23 #include "exec/target_page.h"
24 #include "exec/page-protection.h"
25 #include "hw/qdev-core.h"
26 #include "hw/qdev-properties.h"
27 #include "qemu/error-report.h"
28 #include "qemu/qemu-print.h"
29 #include "migration/vmstate.h"
30 #ifdef CONFIG_USER_ONLY
31 #include "qemu.h"
32 #include "user/page-protection.h"
33 #else
34 #include "hw/core/sysemu-cpu-ops.h"
35 #include "exec/address-spaces.h"
36 #include "exec/memory.h"
37 #endif
38 #include "system/cpus.h"
39 #include "system/tcg.h"
40 #include "exec/tswap.h"
41 #include "exec/replay-core.h"
42 #include "exec/cpu-common.h"
43 #include "exec/exec-all.h"
44 #include "exec/tb-flush.h"
45 #include "exec/translation-block.h"
46 #include "exec/log.h"
47 #include "accel/accel-cpu-target.h"
48 #include "trace/trace-root.h"
49 #include "qemu/accel.h"
50 
51 #ifndef CONFIG_USER_ONLY
52 static int cpu_common_post_load(void *opaque, int version_id)
53 {
54     CPUState *cpu = opaque;
55 
56     /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
57        version_id is increased. */
58     cpu->interrupt_request &= ~0x01;
59     tlb_flush(cpu);
60 
61     /* loadvm has just updated the content of RAM, bypassing the
62      * usual mechanisms that ensure we flush TBs for writes to
63      * memory we've translated code from. So we must flush all TBs,
64      * which will now be stale.
65      */
66     tb_flush(cpu);
67 
68     return 0;
69 }
70 
71 static int cpu_common_pre_load(void *opaque)
72 {
73     CPUState *cpu = opaque;
74 
75     cpu->exception_index = -1;
76 
77     return 0;
78 }
79 
80 static bool cpu_common_exception_index_needed(void *opaque)
81 {
82     CPUState *cpu = opaque;
83 
84     return tcg_enabled() && cpu->exception_index != -1;
85 }
86 
87 static const VMStateDescription vmstate_cpu_common_exception_index = {
88     .name = "cpu_common/exception_index",
89     .version_id = 1,
90     .minimum_version_id = 1,
91     .needed = cpu_common_exception_index_needed,
92     .fields = (const VMStateField[]) {
93         VMSTATE_INT32(exception_index, CPUState),
94         VMSTATE_END_OF_LIST()
95     }
96 };
97 
98 static bool cpu_common_crash_occurred_needed(void *opaque)
99 {
100     CPUState *cpu = opaque;
101 
102     return cpu->crash_occurred;
103 }
104 
105 static const VMStateDescription vmstate_cpu_common_crash_occurred = {
106     .name = "cpu_common/crash_occurred",
107     .version_id = 1,
108     .minimum_version_id = 1,
109     .needed = cpu_common_crash_occurred_needed,
110     .fields = (const VMStateField[]) {
111         VMSTATE_BOOL(crash_occurred, CPUState),
112         VMSTATE_END_OF_LIST()
113     }
114 };
115 
116 const VMStateDescription vmstate_cpu_common = {
117     .name = "cpu_common",
118     .version_id = 1,
119     .minimum_version_id = 1,
120     .pre_load = cpu_common_pre_load,
121     .post_load = cpu_common_post_load,
122     .fields = (const VMStateField[]) {
123         VMSTATE_UINT32(halted, CPUState),
124         VMSTATE_UINT32(interrupt_request, CPUState),
125         VMSTATE_END_OF_LIST()
126     },
127     .subsections = (const VMStateDescription * const []) {
128         &vmstate_cpu_common_exception_index,
129         &vmstate_cpu_common_crash_occurred,
130         NULL
131     }
132 };
133 #endif
134 
135 bool cpu_exec_realizefn(CPUState *cpu, Error **errp)
136 {
137     if (!accel_cpu_common_realize(cpu, errp)) {
138         return false;
139     }
140 
141     /* Wait until cpu initialization complete before exposing cpu. */
142     cpu_list_add(cpu);
143 
144 #ifdef CONFIG_USER_ONLY
145     assert(qdev_get_vmsd(DEVICE(cpu)) == NULL ||
146            qdev_get_vmsd(DEVICE(cpu))->unmigratable);
147 #else
148     if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
149         vmstate_register(NULL, cpu->cpu_index, &vmstate_cpu_common, cpu);
150     }
151     if (cpu->cc->sysemu_ops->legacy_vmsd != NULL) {
152         vmstate_register(NULL, cpu->cpu_index, cpu->cc->sysemu_ops->legacy_vmsd, cpu);
153     }
154 #endif /* CONFIG_USER_ONLY */
155 
156     return true;
157 }
158 
159 void cpu_exec_unrealizefn(CPUState *cpu)
160 {
161 #ifndef CONFIG_USER_ONLY
162     CPUClass *cc = CPU_GET_CLASS(cpu);
163 
164     if (cc->sysemu_ops->legacy_vmsd != NULL) {
165         vmstate_unregister(NULL, cc->sysemu_ops->legacy_vmsd, cpu);
166     }
167     if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
168         vmstate_unregister(NULL, &vmstate_cpu_common, cpu);
169     }
170 #endif
171 
172     cpu_list_remove(cpu);
173     /*
174      * Now that the vCPU has been removed from the RCU list, we can call
175      * accel_cpu_common_unrealize, which may free fields using call_rcu.
176      */
177     accel_cpu_common_unrealize(cpu);
178 }
179 
180 /*
181  * This can't go in hw/core/cpu.c because that file is compiled only
182  * once for both user-mode and system builds.
183  */
184 static const Property cpu_common_props[] = {
185 #ifdef CONFIG_USER_ONLY
186     /*
187      * Create a property for the user-only object, so users can
188      * adjust prctl(PR_SET_UNALIGN) from the command-line.
189      * Has no effect if the target does not support the feature.
190      */
191     DEFINE_PROP_BOOL("prctl-unalign-sigbus", CPUState,
192                      prctl_unalign_sigbus, false),
193 #else
194     /*
195      * Create a memory property for system CPU object, so users can
196      * wire up its memory.  The default if no link is set up is to use
197      * the system address space.
198      */
199     DEFINE_PROP_LINK("memory", CPUState, memory, TYPE_MEMORY_REGION,
200                      MemoryRegion *),
201 #endif
202 };
203 
204 #ifndef CONFIG_USER_ONLY
205 static bool cpu_get_start_powered_off(Object *obj, Error **errp)
206 {
207     CPUState *cpu = CPU(obj);
208     return cpu->start_powered_off;
209 }
210 
211 static void cpu_set_start_powered_off(Object *obj, bool value, Error **errp)
212 {
213     CPUState *cpu = CPU(obj);
214     cpu->start_powered_off = value;
215 }
216 #endif
217 
218 void cpu_class_init_props(DeviceClass *dc)
219 {
220 #ifndef CONFIG_USER_ONLY
221     ObjectClass *oc = OBJECT_CLASS(dc);
222 
223     /*
224      * We can't use DEFINE_PROP_BOOL in the Property array for this
225      * property, because we want this to be settable after realize.
226      */
227     object_class_property_add_bool(oc, "start-powered-off",
228                                    cpu_get_start_powered_off,
229                                    cpu_set_start_powered_off);
230 #endif
231 
232     device_class_set_props(dc, cpu_common_props);
233 }
234 
235 void cpu_exec_initfn(CPUState *cpu)
236 {
237 #ifndef CONFIG_USER_ONLY
238     cpu->memory = get_system_memory();
239     object_ref(OBJECT(cpu->memory));
240 #endif
241 }
242 
243 char *cpu_model_from_type(const char *typename)
244 {
245     const char *suffix = "-" CPU_RESOLVING_TYPE;
246 
247     if (!object_class_by_name(typename)) {
248         return NULL;
249     }
250 
251     if (g_str_has_suffix(typename, suffix)) {
252         return g_strndup(typename, strlen(typename) - strlen(suffix));
253     }
254 
255     return g_strdup(typename);
256 }
257 
258 const char *parse_cpu_option(const char *cpu_option)
259 {
260     ObjectClass *oc;
261     CPUClass *cc;
262     gchar **model_pieces;
263     const char *cpu_type;
264 
265     model_pieces = g_strsplit(cpu_option, ",", 2);
266     if (!model_pieces[0]) {
267         error_report("-cpu option cannot be empty");
268         exit(1);
269     }
270 
271     oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]);
272     if (oc == NULL) {
273         error_report("unable to find CPU model '%s'", model_pieces[0]);
274         g_strfreev(model_pieces);
275         exit(EXIT_FAILURE);
276     }
277 
278     cpu_type = object_class_get_name(oc);
279     cc = CPU_CLASS(oc);
280     cc->parse_features(cpu_type, model_pieces[1], &error_fatal);
281     g_strfreev(model_pieces);
282     return cpu_type;
283 }
284 
285 #ifndef cpu_list
286 static void cpu_list_entry(gpointer data, gpointer user_data)
287 {
288     CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
289     const char *typename = object_class_get_name(OBJECT_CLASS(data));
290     g_autofree char *model = cpu_model_from_type(typename);
291 
292     if (cc->deprecation_note) {
293         qemu_printf("  %s (deprecated)\n", model);
294     } else {
295         qemu_printf("  %s\n", model);
296     }
297 }
298 
299 static void cpu_list(void)
300 {
301     GSList *list;
302 
303     list = object_class_get_list_sorted(TYPE_CPU, false);
304     qemu_printf("Available CPUs:\n");
305     g_slist_foreach(list, cpu_list_entry, NULL);
306     g_slist_free(list);
307 }
308 #endif
309 
310 void list_cpus(void)
311 {
312     cpu_list();
313 }
314 
315 /* enable or disable single step mode. EXCP_DEBUG is returned by the
316    CPU loop after each instruction */
317 void cpu_single_step(CPUState *cpu, int enabled)
318 {
319     if (cpu->singlestep_enabled != enabled) {
320         cpu->singlestep_enabled = enabled;
321 
322 #if !defined(CONFIG_USER_ONLY)
323         const AccelOpsClass *ops = cpus_get_accel();
324         if (ops->update_guest_debug) {
325             ops->update_guest_debug(cpu);
326         }
327 #endif
328 
329         trace_breakpoint_singlestep(cpu->cpu_index, enabled);
330     }
331 }
332 
333 void cpu_abort(CPUState *cpu, const char *fmt, ...)
334 {
335     va_list ap;
336     va_list ap2;
337 
338     va_start(ap, fmt);
339     va_copy(ap2, ap);
340     fprintf(stderr, "qemu: fatal: ");
341     vfprintf(stderr, fmt, ap);
342     fprintf(stderr, "\n");
343     cpu_dump_state(cpu, stderr, CPU_DUMP_FPU | CPU_DUMP_CCOP);
344     if (qemu_log_separate()) {
345         FILE *logfile = qemu_log_trylock();
346         if (logfile) {
347             fprintf(logfile, "qemu: fatal: ");
348             vfprintf(logfile, fmt, ap2);
349             fprintf(logfile, "\n");
350             cpu_dump_state(cpu, logfile, CPU_DUMP_FPU | CPU_DUMP_CCOP);
351             qemu_log_unlock(logfile);
352         }
353     }
354     va_end(ap2);
355     va_end(ap);
356     replay_finish();
357 #if defined(CONFIG_USER_ONLY)
358     {
359         struct sigaction act;
360         sigfillset(&act.sa_mask);
361         act.sa_handler = SIG_DFL;
362         act.sa_flags = 0;
363         sigaction(SIGABRT, &act, NULL);
364     }
365 #endif
366     abort();
367 }
368 
369 /* physical memory access (slow version, mainly for debug) */
370 #if defined(CONFIG_USER_ONLY)
371 int cpu_memory_rw_debug(CPUState *cpu, vaddr addr,
372                         void *ptr, size_t len, bool is_write)
373 {
374     int flags;
375     vaddr l, page;
376     void * p;
377     uint8_t *buf = ptr;
378     ssize_t written;
379     int ret = -1;
380     int fd = -1;
381 
382     while (len > 0) {
383         page = addr & TARGET_PAGE_MASK;
384         l = (page + TARGET_PAGE_SIZE) - addr;
385         if (l > len)
386             l = len;
387         flags = page_get_flags(page);
388         if (!(flags & PAGE_VALID)) {
389             goto out_close;
390         }
391         if (is_write) {
392             if (flags & PAGE_WRITE) {
393                 /* XXX: this code should not depend on lock_user */
394                 p = lock_user(VERIFY_WRITE, addr, l, 0);
395                 if (!p) {
396                     goto out_close;
397                 }
398                 memcpy(p, buf, l);
399                 unlock_user(p, addr, l);
400             } else {
401                 /* Bypass the host page protection using ptrace. */
402                 if (fd == -1) {
403                     fd = open("/proc/self/mem", O_WRONLY);
404                     if (fd == -1) {
405                         goto out;
406                     }
407                 }
408                 /*
409                  * If there is a TranslationBlock and we weren't bypassing the
410                  * host page protection, the memcpy() above would SEGV,
411                  * ultimately leading to page_unprotect(). So invalidate the
412                  * translations manually. Both invalidation and pwrite() must
413                  * be under mmap_lock() in order to prevent the creation of
414                  * another TranslationBlock in between.
415                  */
416                 mmap_lock();
417                 tb_invalidate_phys_range(addr, addr + l - 1);
418                 written = pwrite(fd, buf, l,
419                                  (off_t)(uintptr_t)g2h_untagged(addr));
420                 mmap_unlock();
421                 if (written != l) {
422                     goto out_close;
423                 }
424             }
425         } else if (flags & PAGE_READ) {
426             /* XXX: this code should not depend on lock_user */
427             p = lock_user(VERIFY_READ, addr, l, 1);
428             if (!p) {
429                 goto out_close;
430             }
431             memcpy(buf, p, l);
432             unlock_user(p, addr, 0);
433         } else {
434             /* Bypass the host page protection using ptrace. */
435             if (fd == -1) {
436                 fd = open("/proc/self/mem", O_RDONLY);
437                 if (fd == -1) {
438                     goto out;
439                 }
440             }
441             if (pread(fd, buf, l,
442                       (off_t)(uintptr_t)g2h_untagged(addr)) != l) {
443                 goto out_close;
444             }
445         }
446         len -= l;
447         buf += l;
448         addr += l;
449     }
450     ret = 0;
451 out_close:
452     if (fd != -1) {
453         close(fd);
454     }
455 out:
456     return ret;
457 }
458 #endif
459 
460 bool target_words_bigendian(void)
461 {
462     return TARGET_BIG_ENDIAN;
463 }
464 
465 const char *target_name(void)
466 {
467     return TARGET_NAME;
468 }
469