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