Paolo Bonzini | d9f24bf | 2020-10-06 09:05:29 +0200 | [diff] [blame] | 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" |
Pierrick Bouvier | 8d535c3 | 2025-03-24 21:58:48 -0700 | [diff] [blame] | 21 | #include "cpu.h" |
Paolo Bonzini | d9f24bf | 2020-10-06 09:05:29 +0200 | [diff] [blame] | 22 | #include "qapi/error.h" |
Paolo Bonzini | d9f24bf | 2020-10-06 09:05:29 +0200 | [diff] [blame] | 23 | #include "qemu/error-report.h" |
Gavin Shan | dfa4753 | 2023-11-15 09:56:03 +1000 | [diff] [blame] | 24 | #include "qemu/qemu-print.h" |
Philippe Mathieu-Daudé | 0f66536 | 2025-01-23 13:39:05 +0100 | [diff] [blame] | 25 | #include "system/accel-ops.h" |
Philippe Mathieu-Daudé | 32cad1f | 2024-12-03 15:20:13 +0100 | [diff] [blame] | 26 | #include "system/cpus.h" |
Pierrick Bouvier | d97c3b06 | 2025-03-24 21:58:59 -0700 | [diff] [blame^] | 27 | #include "exec/cpu-common.h" |
Philippe Mathieu-Daudé | 4250826 | 2023-12-12 11:34:25 +0100 | [diff] [blame] | 28 | #include "exec/tswap.h" |
Philippe Mathieu-Daudé | 5b5968c | 2022-12-19 18:09:43 +0100 | [diff] [blame] | 29 | #include "exec/replay-core.h" |
Paolo Bonzini | d9f24bf | 2020-10-06 09:05:29 +0200 | [diff] [blame] | 30 | #include "exec/log.h" |
Philippe Mathieu-Daudé | b12a0f8 | 2025-01-23 11:11:24 +0100 | [diff] [blame] | 31 | #include "accel/accel-cpu-target.h" |
Richard Henderson | ad1a706 | 2021-07-01 08:10:53 -0700 | [diff] [blame] | 32 | #include "trace/trace-root.h" |
Paolo Bonzini | d9f24bf | 2020-10-06 09:05:29 +0200 | [diff] [blame] | 33 | |
Pierrick Bouvier | 8d535c3 | 2025-03-24 21:58:48 -0700 | [diff] [blame] | 34 | /* Validate correct placement of CPUArchState. */ |
| 35 | QEMU_BUILD_BUG_ON(offsetof(ArchCPU, parent_obj) != 0); |
| 36 | QEMU_BUILD_BUG_ON(offsetof(ArchCPU, env) != sizeof(CPUState)); |
| 37 | |
Gavin Shan | 445946f | 2023-11-15 09:56:02 +1000 | [diff] [blame] | 38 | char *cpu_model_from_type(const char *typename) |
| 39 | { |
| 40 | const char *suffix = "-" CPU_RESOLVING_TYPE; |
| 41 | |
| 42 | if (!object_class_by_name(typename)) { |
| 43 | return NULL; |
| 44 | } |
| 45 | |
| 46 | if (g_str_has_suffix(typename, suffix)) { |
| 47 | return g_strndup(typename, strlen(typename) - strlen(suffix)); |
| 48 | } |
| 49 | |
| 50 | return g_strdup(typename); |
| 51 | } |
| 52 | |
Paolo Bonzini | d9f24bf | 2020-10-06 09:05:29 +0200 | [diff] [blame] | 53 | const char *parse_cpu_option(const char *cpu_option) |
| 54 | { |
| 55 | ObjectClass *oc; |
| 56 | CPUClass *cc; |
| 57 | gchar **model_pieces; |
| 58 | const char *cpu_type; |
| 59 | |
| 60 | model_pieces = g_strsplit(cpu_option, ",", 2); |
| 61 | if (!model_pieces[0]) { |
| 62 | error_report("-cpu option cannot be empty"); |
| 63 | exit(1); |
| 64 | } |
| 65 | |
| 66 | oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]); |
| 67 | if (oc == NULL) { |
| 68 | error_report("unable to find CPU model '%s'", model_pieces[0]); |
| 69 | g_strfreev(model_pieces); |
| 70 | exit(EXIT_FAILURE); |
| 71 | } |
| 72 | |
| 73 | cpu_type = object_class_get_name(oc); |
| 74 | cc = CPU_CLASS(oc); |
| 75 | cc->parse_features(cpu_type, model_pieces[1], &error_fatal); |
| 76 | g_strfreev(model_pieces); |
| 77 | return cpu_type; |
| 78 | } |
| 79 | |
Gavin Shan | dfa4753 | 2023-11-15 09:56:03 +1000 | [diff] [blame] | 80 | #ifndef cpu_list |
| 81 | static void cpu_list_entry(gpointer data, gpointer user_data) |
| 82 | { |
| 83 | CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data)); |
| 84 | const char *typename = object_class_get_name(OBJECT_CLASS(data)); |
| 85 | g_autofree char *model = cpu_model_from_type(typename); |
| 86 | |
| 87 | if (cc->deprecation_note) { |
| 88 | qemu_printf(" %s (deprecated)\n", model); |
| 89 | } else { |
| 90 | qemu_printf(" %s\n", model); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static void cpu_list(void) |
| 95 | { |
| 96 | GSList *list; |
| 97 | |
| 98 | list = object_class_get_list_sorted(TYPE_CPU, false); |
| 99 | qemu_printf("Available CPUs:\n"); |
| 100 | g_slist_foreach(list, cpu_list_entry, NULL); |
| 101 | g_slist_free(list); |
| 102 | } |
| 103 | #endif |
| 104 | |
Thomas Huth | c138c3b | 2023-04-19 14:48:31 +0200 | [diff] [blame] | 105 | void list_cpus(void) |
Philippe Mathieu-Daudé | 377bf6f | 2022-03-14 15:01:08 +0100 | [diff] [blame] | 106 | { |
Philippe Mathieu-Daudé | 377bf6f | 2022-03-14 15:01:08 +0100 | [diff] [blame] | 107 | cpu_list(); |
Philippe Mathieu-Daudé | 377bf6f | 2022-03-14 15:01:08 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Paolo Bonzini | d9f24bf | 2020-10-06 09:05:29 +0200 | [diff] [blame] | 110 | /* enable or disable single step mode. EXCP_DEBUG is returned by the |
| 111 | CPU loop after each instruction */ |
| 112 | void cpu_single_step(CPUState *cpu, int enabled) |
| 113 | { |
| 114 | if (cpu->singlestep_enabled != enabled) { |
| 115 | cpu->singlestep_enabled = enabled; |
Mads Ynddal | 412ae12 | 2023-03-02 18:58:05 -0800 | [diff] [blame] | 116 | |
| 117 | #if !defined(CONFIG_USER_ONLY) |
| 118 | const AccelOpsClass *ops = cpus_get_accel(); |
| 119 | if (ops->update_guest_debug) { |
| 120 | ops->update_guest_debug(cpu); |
Paolo Bonzini | d9f24bf | 2020-10-06 09:05:29 +0200 | [diff] [blame] | 121 | } |
Mads Ynddal | 412ae12 | 2023-03-02 18:58:05 -0800 | [diff] [blame] | 122 | #endif |
| 123 | |
Richard Henderson | ad1a706 | 2021-07-01 08:10:53 -0700 | [diff] [blame] | 124 | trace_breakpoint_singlestep(cpu->cpu_index, enabled); |
Paolo Bonzini | d9f24bf | 2020-10-06 09:05:29 +0200 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | void cpu_abort(CPUState *cpu, const char *fmt, ...) |
| 129 | { |
| 130 | va_list ap; |
| 131 | va_list ap2; |
| 132 | |
| 133 | va_start(ap, fmt); |
| 134 | va_copy(ap2, ap); |
| 135 | fprintf(stderr, "qemu: fatal: "); |
| 136 | vfprintf(stderr, fmt, ap); |
| 137 | fprintf(stderr, "\n"); |
| 138 | cpu_dump_state(cpu, stderr, CPU_DUMP_FPU | CPU_DUMP_CCOP); |
| 139 | if (qemu_log_separate()) { |
Richard Henderson | c60f599 | 2022-04-17 11:29:47 -0700 | [diff] [blame] | 140 | FILE *logfile = qemu_log_trylock(); |
Richard Henderson | 78b5485 | 2022-04-17 11:29:49 -0700 | [diff] [blame] | 141 | if (logfile) { |
| 142 | fprintf(logfile, "qemu: fatal: "); |
| 143 | vfprintf(logfile, fmt, ap2); |
| 144 | fprintf(logfile, "\n"); |
| 145 | cpu_dump_state(cpu, logfile, CPU_DUMP_FPU | CPU_DUMP_CCOP); |
Richard Henderson | 78b5485 | 2022-04-17 11:29:49 -0700 | [diff] [blame] | 146 | qemu_log_unlock(logfile); |
| 147 | } |
Paolo Bonzini | d9f24bf | 2020-10-06 09:05:29 +0200 | [diff] [blame] | 148 | } |
| 149 | va_end(ap2); |
| 150 | va_end(ap); |
| 151 | replay_finish(); |
| 152 | #if defined(CONFIG_USER_ONLY) |
| 153 | { |
| 154 | struct sigaction act; |
| 155 | sigfillset(&act.sa_mask); |
| 156 | act.sa_handler = SIG_DFL; |
| 157 | act.sa_flags = 0; |
| 158 | sigaction(SIGABRT, &act, NULL); |
| 159 | } |
| 160 | #endif |
| 161 | abort(); |
| 162 | } |
| 163 | |
Pierrick Bouvier | cfac5cd | 2025-03-17 11:34:00 -0700 | [diff] [blame] | 164 | #undef target_words_bigendian |
Paolo Bonzini | d9f24bf | 2020-10-06 09:05:29 +0200 | [diff] [blame] | 165 | bool target_words_bigendian(void) |
| 166 | { |
Thomas Huth | ded625e | 2023-09-07 13:35:00 +0200 | [diff] [blame] | 167 | return TARGET_BIG_ENDIAN; |
Paolo Bonzini | d9f24bf | 2020-10-06 09:05:29 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Thomas Huth | 1077f50 | 2023-04-24 18:04:33 +0200 | [diff] [blame] | 170 | const char *target_name(void) |
| 171 | { |
| 172 | return TARGET_NAME; |
| 173 | } |