1 /* 2 * QEMU CPU model 3 * 4 * Copyright (c) 2012 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 #ifndef QEMU_CPU_H 21 #define QEMU_CPU_H 22 23 #include "hw/qdev-core.h" 24 #include "disas/dis-asm.h" 25 #include "exec/hwaddr.h" 26 #include "exec/memattrs.h" 27 #include "qapi/qapi-types-run-state.h" 28 #include "qemu/bitmap.h" 29 #include "qemu/rcu_queue.h" 30 #include "qemu/queue.h" 31 #include "qemu/thread.h" 32 #include "qemu/plugin.h" 33 #include "qom/object.h" 34 35 typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size, 36 void *opaque); 37 38 /** 39 * vaddr: 40 * Type wide enough to contain any #target_ulong virtual address. 41 */ 42 typedef uint64_t vaddr; 43 #define VADDR_PRId PRId64 44 #define VADDR_PRIu PRIu64 45 #define VADDR_PRIo PRIo64 46 #define VADDR_PRIx PRIx64 47 #define VADDR_PRIX PRIX64 48 #define VADDR_MAX UINT64_MAX 49 50 /** 51 * SECTION:cpu 52 * @section_id: QEMU-cpu 53 * @title: CPU Class 54 * @short_description: Base class for all CPUs 55 */ 56 57 #define TYPE_CPU "cpu" 58 59 /* Since this macro is used a lot in hot code paths and in conjunction with 60 * FooCPU *foo_env_get_cpu(), we deviate from usual QOM practice by using 61 * an unchecked cast. 62 */ 63 #define CPU(obj) ((CPUState *)(obj)) 64 65 typedef struct CPUClass CPUClass; 66 DECLARE_CLASS_CHECKERS(CPUClass, CPU, 67 TYPE_CPU) 68 69 typedef enum MMUAccessType { 70 MMU_DATA_LOAD = 0, 71 MMU_DATA_STORE = 1, 72 MMU_INST_FETCH = 2 73 } MMUAccessType; 74 75 typedef struct CPUWatchpoint CPUWatchpoint; 76 77 struct TranslationBlock; 78 79 /** 80 * struct TcgCpuOperations: TCG operations specific to a CPU class 81 */ 82 typedef struct TcgCpuOperations { 83 /** 84 * @initialize: Initalize TCG state 85 * 86 * Called when the first CPU is realized. 87 */ 88 void (*initialize)(void); 89 90 } TcgCpuOperations; 91 92 /** 93 * CPUClass: 94 * @class_by_name: Callback to map -cpu command line model name to an 95 * instantiatable CPU type. 96 * @parse_features: Callback to parse command line arguments. 97 * @reset_dump_flags: #CPUDumpFlags to use for reset logging. 98 * @has_work: Callback for checking if there is work to do. 99 * @do_interrupt: Callback for interrupt handling. 100 * @do_unaligned_access: Callback for unaligned access handling, if 101 * the target defines #TARGET_ALIGNED_ONLY. 102 * @do_transaction_failed: Callback for handling failed memory transactions 103 * (ie bus faults or external aborts; not MMU faults) 104 * @virtio_is_big_endian: Callback to return %true if a CPU which supports 105 * runtime configurable endianness is currently big-endian. Non-configurable 106 * CPUs can use the default implementation of this method. This method should 107 * not be used by any callers other than the pre-1.0 virtio devices. 108 * @memory_rw_debug: Callback for GDB memory access. 109 * @dump_state: Callback for dumping state. 110 * @dump_statistics: Callback for dumping statistics. 111 * @get_arch_id: Callback for getting architecture-dependent CPU ID. 112 * @get_paging_enabled: Callback for inquiring whether paging is enabled. 113 * @get_memory_mapping: Callback for obtaining the memory mappings. 114 * @set_pc: Callback for setting the Program Counter register. This 115 * should have the semantics used by the target architecture when 116 * setting the PC from a source such as an ELF file entry point; 117 * for example on Arm it will also set the Thumb mode bit based 118 * on the least significant bit of the new PC value. 119 * If the target behaviour here is anything other than "set 120 * the PC register to the value passed in" then the target must 121 * also implement the synchronize_from_tb hook. 122 * @synchronize_from_tb: Callback for synchronizing state from a TCG 123 * #TranslationBlock. This is called when we abandon execution 124 * of a TB before starting it, and must set all parts of the CPU 125 * state which the previous TB in the chain may not have updated. 126 * This always includes at least the program counter; some targets 127 * will need to do more. If this hook is not implemented then the 128 * default is to call @set_pc(tb->pc). 129 * @tlb_fill: Callback for handling a softmmu tlb miss or user-only 130 * address fault. For system mode, if the access is valid, call 131 * tlb_set_page and return true; if the access is invalid, and 132 * probe is true, return false; otherwise raise an exception and 133 * do not return. For user-only mode, always raise an exception 134 * and do not return. 135 * @get_phys_page_debug: Callback for obtaining a physical address. 136 * @get_phys_page_attrs_debug: Callback for obtaining a physical address and the 137 * associated memory transaction attributes to use for the access. 138 * CPUs which use memory transaction attributes should implement this 139 * instead of get_phys_page_debug. 140 * @asidx_from_attrs: Callback to return the CPU AddressSpace to use for 141 * a memory access with the specified memory transaction attributes. 142 * @gdb_read_register: Callback for letting GDB read a register. 143 * @gdb_write_register: Callback for letting GDB write a register. 144 * @debug_check_watchpoint: Callback: return true if the architectural 145 * watchpoint whose address has matched should really fire. 146 * @debug_excp_handler: Callback for handling debug exceptions. 147 * @write_elf64_note: Callback for writing a CPU-specific ELF note to a 148 * 64-bit VM coredump. 149 * @write_elf32_qemunote: Callback for writing a CPU- and QEMU-specific ELF 150 * note to a 32-bit VM coredump. 151 * @write_elf32_note: Callback for writing a CPU-specific ELF note to a 152 * 32-bit VM coredump. 153 * @write_elf32_qemunote: Callback for writing a CPU- and QEMU-specific ELF 154 * note to a 32-bit VM coredump. 155 * @vmsd: State description for migration. 156 * @gdb_num_core_regs: Number of core registers accessible to GDB. 157 * @gdb_core_xml_file: File name for core registers GDB XML description. 158 * @gdb_stop_before_watchpoint: Indicates whether GDB expects the CPU to stop 159 * before the insn which triggers a watchpoint rather than after it. 160 * @gdb_arch_name: Optional callback that returns the architecture name known 161 * to GDB. The caller must free the returned string with g_free. 162 * @gdb_get_dynamic_xml: Callback to return dynamically generated XML for the 163 * gdb stub. Returns a pointer to the XML contents for the specified XML file 164 * or NULL if the CPU doesn't have a dynamically generated content for it. 165 * @cpu_exec_enter: Callback for cpu_exec preparation. 166 * @cpu_exec_exit: Callback for cpu_exec cleanup. 167 * @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec. 168 * @disas_set_info: Setup architecture specific components of disassembly info 169 * @adjust_watchpoint_address: Perform a target-specific adjustment to an 170 * address before attempting to match it against watchpoints. 171 * @deprecation_note: If this CPUClass is deprecated, this field provides 172 * related information. 173 * 174 * Represents a CPU family or model. 175 */ 176 struct CPUClass { 177 /*< private >*/ 178 DeviceClass parent_class; 179 /*< public >*/ 180 181 ObjectClass *(*class_by_name)(const char *cpu_model); 182 void (*parse_features)(const char *typename, char *str, Error **errp); 183 184 int reset_dump_flags; 185 bool (*has_work)(CPUState *cpu); 186 void (*do_interrupt)(CPUState *cpu); 187 void (*do_unaligned_access)(CPUState *cpu, vaddr addr, 188 MMUAccessType access_type, 189 int mmu_idx, uintptr_t retaddr); 190 void (*do_transaction_failed)(CPUState *cpu, hwaddr physaddr, vaddr addr, 191 unsigned size, MMUAccessType access_type, 192 int mmu_idx, MemTxAttrs attrs, 193 MemTxResult response, uintptr_t retaddr); 194 bool (*virtio_is_big_endian)(CPUState *cpu); 195 int (*memory_rw_debug)(CPUState *cpu, vaddr addr, 196 uint8_t *buf, int len, bool is_write); 197 void (*dump_state)(CPUState *cpu, FILE *, int flags); 198 GuestPanicInformation* (*get_crash_info)(CPUState *cpu); 199 void (*dump_statistics)(CPUState *cpu, int flags); 200 int64_t (*get_arch_id)(CPUState *cpu); 201 bool (*get_paging_enabled)(const CPUState *cpu); 202 void (*get_memory_mapping)(CPUState *cpu, MemoryMappingList *list, 203 Error **errp); 204 void (*set_pc)(CPUState *cpu, vaddr value); 205 void (*synchronize_from_tb)(CPUState *cpu, 206 const struct TranslationBlock *tb); 207 bool (*tlb_fill)(CPUState *cpu, vaddr address, int size, 208 MMUAccessType access_type, int mmu_idx, 209 bool probe, uintptr_t retaddr); 210 hwaddr (*get_phys_page_debug)(CPUState *cpu, vaddr addr); 211 hwaddr (*get_phys_page_attrs_debug)(CPUState *cpu, vaddr addr, 212 MemTxAttrs *attrs); 213 int (*asidx_from_attrs)(CPUState *cpu, MemTxAttrs attrs); 214 int (*gdb_read_register)(CPUState *cpu, GByteArray *buf, int reg); 215 int (*gdb_write_register)(CPUState *cpu, uint8_t *buf, int reg); 216 bool (*debug_check_watchpoint)(CPUState *cpu, CPUWatchpoint *wp); 217 void (*debug_excp_handler)(CPUState *cpu); 218 219 int (*write_elf64_note)(WriteCoreDumpFunction f, CPUState *cpu, 220 int cpuid, void *opaque); 221 int (*write_elf64_qemunote)(WriteCoreDumpFunction f, CPUState *cpu, 222 void *opaque); 223 int (*write_elf32_note)(WriteCoreDumpFunction f, CPUState *cpu, 224 int cpuid, void *opaque); 225 int (*write_elf32_qemunote)(WriteCoreDumpFunction f, CPUState *cpu, 226 void *opaque); 227 228 const VMStateDescription *vmsd; 229 const char *gdb_core_xml_file; 230 gchar * (*gdb_arch_name)(CPUState *cpu); 231 const char * (*gdb_get_dynamic_xml)(CPUState *cpu, const char *xmlname); 232 void (*cpu_exec_enter)(CPUState *cpu); 233 void (*cpu_exec_exit)(CPUState *cpu); 234 bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request); 235 236 void (*disas_set_info)(CPUState *cpu, disassemble_info *info); 237 vaddr (*adjust_watchpoint_address)(CPUState *cpu, vaddr addr, int len); 238 239 const char *deprecation_note; 240 /* Keep non-pointer data at the end to minimize holes. */ 241 int gdb_num_core_regs; 242 bool gdb_stop_before_watchpoint; 243 244 TcgCpuOperations tcg_ops; 245 }; 246 247 /* 248 * Low 16 bits: number of cycles left, used only in icount mode. 249 * High 16 bits: Set to -1 to force TCG to stop executing linked TBs 250 * for this CPU and return to its top level loop (even in non-icount mode). 251 * This allows a single read-compare-cbranch-write sequence to test 252 * for both decrementer underflow and exceptions. 253 */ 254 typedef union IcountDecr { 255 uint32_t u32; 256 struct { 257 #ifdef HOST_WORDS_BIGENDIAN 258 uint16_t high; 259 uint16_t low; 260 #else 261 uint16_t low; 262 uint16_t high; 263 #endif 264 } u16; 265 } IcountDecr; 266 267 typedef struct CPUBreakpoint { 268 vaddr pc; 269 int flags; /* BP_* */ 270 QTAILQ_ENTRY(CPUBreakpoint) entry; 271 } CPUBreakpoint; 272 273 struct CPUWatchpoint { 274 vaddr vaddr; 275 vaddr len; 276 vaddr hitaddr; 277 MemTxAttrs hitattrs; 278 int flags; /* BP_* */ 279 QTAILQ_ENTRY(CPUWatchpoint) entry; 280 }; 281 282 #ifdef CONFIG_PLUGIN 283 /* 284 * For plugins we sometime need to save the resolved iotlb data before 285 * the memory regions get moved around by io_writex. 286 */ 287 typedef struct SavedIOTLB { 288 hwaddr addr; 289 MemoryRegionSection *section; 290 hwaddr mr_offset; 291 } SavedIOTLB; 292 #endif 293 294 struct KVMState; 295 struct kvm_run; 296 297 struct hax_vcpu_state; 298 299 #define TB_JMP_CACHE_BITS 12 300 #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS) 301 302 /* work queue */ 303 304 /* The union type allows passing of 64 bit target pointers on 32 bit 305 * hosts in a single parameter 306 */ 307 typedef union { 308 int host_int; 309 unsigned long host_ulong; 310 void *host_ptr; 311 vaddr target_ptr; 312 } run_on_cpu_data; 313 314 #define RUN_ON_CPU_HOST_PTR(p) ((run_on_cpu_data){.host_ptr = (p)}) 315 #define RUN_ON_CPU_HOST_INT(i) ((run_on_cpu_data){.host_int = (i)}) 316 #define RUN_ON_CPU_HOST_ULONG(ul) ((run_on_cpu_data){.host_ulong = (ul)}) 317 #define RUN_ON_CPU_TARGET_PTR(v) ((run_on_cpu_data){.target_ptr = (v)}) 318 #define RUN_ON_CPU_NULL RUN_ON_CPU_HOST_PTR(NULL) 319 320 typedef void (*run_on_cpu_func)(CPUState *cpu, run_on_cpu_data data); 321 322 struct qemu_work_item; 323 324 #define CPU_UNSET_NUMA_NODE_ID -1 325 #define CPU_TRACE_DSTATE_MAX_EVENTS 32 326 327 /** 328 * CPUState: 329 * @cpu_index: CPU index (informative). 330 * @cluster_index: Identifies which cluster this CPU is in. 331 * For boards which don't define clusters or for "loose" CPUs not assigned 332 * to a cluster this will be UNASSIGNED_CLUSTER_INDEX; otherwise it will 333 * be the same as the cluster-id property of the CPU object's TYPE_CPU_CLUSTER 334 * QOM parent. 335 * @nr_cores: Number of cores within this CPU package. 336 * @nr_threads: Number of threads within this CPU. 337 * @running: #true if CPU is currently running (lockless). 338 * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end; 339 * valid under cpu_list_lock. 340 * @created: Indicates whether the CPU thread has been successfully created. 341 * @interrupt_request: Indicates a pending interrupt request. 342 * @halted: Nonzero if the CPU is in suspended state. 343 * @stop: Indicates a pending stop request. 344 * @stopped: Indicates the CPU has been artificially stopped. 345 * @unplug: Indicates a pending CPU unplug request. 346 * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU 347 * @singlestep_enabled: Flags for single-stepping. 348 * @icount_extra: Instructions until next timer event. 349 * @can_do_io: Nonzero if memory-mapped IO is safe. Deterministic execution 350 * requires that IO only be performed on the last instruction of a TB 351 * so that interrupts take effect immediately. 352 * @cpu_ases: Pointer to array of CPUAddressSpaces (which define the 353 * AddressSpaces this CPU has) 354 * @num_ases: number of CPUAddressSpaces in @cpu_ases 355 * @as: Pointer to the first AddressSpace, for the convenience of targets which 356 * only have a single AddressSpace 357 * @env_ptr: Pointer to subclass-specific CPUArchState field. 358 * @icount_decr_ptr: Pointer to IcountDecr field within subclass. 359 * @gdb_regs: Additional GDB registers. 360 * @gdb_num_regs: Number of total registers accessible to GDB. 361 * @gdb_num_g_regs: Number of registers in GDB 'g' packets. 362 * @next_cpu: Next CPU sharing TB cache. 363 * @opaque: User data. 364 * @mem_io_pc: Host Program Counter at which the memory was accessed. 365 * @kvm_fd: vCPU file descriptor for KVM. 366 * @work_mutex: Lock to prevent multiple access to @work_list. 367 * @work_list: List of pending asynchronous work. 368 * @trace_dstate_delayed: Delayed changes to trace_dstate (includes all changes 369 * to @trace_dstate). 370 * @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask). 371 * @plugin_mask: Plugin event bitmap. Modified only via async work. 372 * @ignore_memory_transaction_failures: Cached copy of the MachineState 373 * flag of the same name: allows the board to suppress calling of the 374 * CPU do_transaction_failed hook function. 375 * 376 * State of one CPU core or thread. 377 */ 378 struct CPUState { 379 /*< private >*/ 380 DeviceState parent_obj; 381 /*< public >*/ 382 383 int nr_cores; 384 int nr_threads; 385 386 struct QemuThread *thread; 387 #ifdef _WIN32 388 HANDLE hThread; 389 #endif 390 int thread_id; 391 bool running, has_waiter; 392 struct QemuCond *halt_cond; 393 bool thread_kicked; 394 bool created; 395 bool stop; 396 bool stopped; 397 398 /* Should CPU start in powered-off state? */ 399 bool start_powered_off; 400 401 bool unplug; 402 bool crash_occurred; 403 bool exit_request; 404 bool in_exclusive_context; 405 uint32_t cflags_next_tb; 406 /* updates protected by BQL */ 407 uint32_t interrupt_request; 408 int singlestep_enabled; 409 int64_t icount_budget; 410 int64_t icount_extra; 411 uint64_t random_seed; 412 sigjmp_buf jmp_env; 413 414 QemuMutex work_mutex; 415 QSIMPLEQ_HEAD(, qemu_work_item) work_list; 416 417 CPUAddressSpace *cpu_ases; 418 int num_ases; 419 AddressSpace *as; 420 MemoryRegion *memory; 421 422 void *env_ptr; /* CPUArchState */ 423 IcountDecr *icount_decr_ptr; 424 425 /* Accessed in parallel; all accesses must be atomic */ 426 struct TranslationBlock *tb_jmp_cache[TB_JMP_CACHE_SIZE]; 427 428 struct GDBRegisterState *gdb_regs; 429 int gdb_num_regs; 430 int gdb_num_g_regs; 431 QTAILQ_ENTRY(CPUState) node; 432 433 /* ice debug support */ 434 QTAILQ_HEAD(, CPUBreakpoint) breakpoints; 435 436 QTAILQ_HEAD(, CPUWatchpoint) watchpoints; 437 CPUWatchpoint *watchpoint_hit; 438 439 void *opaque; 440 441 /* In order to avoid passing too many arguments to the MMIO helpers, 442 * we store some rarely used information in the CPU context. 443 */ 444 uintptr_t mem_io_pc; 445 446 int kvm_fd; 447 struct KVMState *kvm_state; 448 struct kvm_run *kvm_run; 449 450 /* Used for events with 'vcpu' and *without* the 'disabled' properties */ 451 DECLARE_BITMAP(trace_dstate_delayed, CPU_TRACE_DSTATE_MAX_EVENTS); 452 DECLARE_BITMAP(trace_dstate, CPU_TRACE_DSTATE_MAX_EVENTS); 453 454 DECLARE_BITMAP(plugin_mask, QEMU_PLUGIN_EV_MAX); 455 456 #ifdef CONFIG_PLUGIN 457 GArray *plugin_mem_cbs; 458 /* saved iotlb data from io_writex */ 459 SavedIOTLB saved_iotlb; 460 #endif 461 462 /* TODO Move common fields from CPUArchState here. */ 463 int cpu_index; 464 int cluster_index; 465 uint32_t halted; 466 uint32_t can_do_io; 467 int32_t exception_index; 468 469 /* shared by kvm, hax and hvf */ 470 bool vcpu_dirty; 471 472 /* Used to keep track of an outstanding cpu throttle thread for migration 473 * autoconverge 474 */ 475 bool throttle_thread_scheduled; 476 477 bool ignore_memory_transaction_failures; 478 479 struct hax_vcpu_state *hax_vcpu; 480 481 int hvf_fd; 482 483 /* track IOMMUs whose translations we've cached in the TCG TLB */ 484 GArray *iommu_notifiers; 485 }; 486 487 typedef QTAILQ_HEAD(CPUTailQ, CPUState) CPUTailQ; 488 extern CPUTailQ cpus; 489 490 #define first_cpu QTAILQ_FIRST_RCU(&cpus) 491 #define CPU_NEXT(cpu) QTAILQ_NEXT_RCU(cpu, node) 492 #define CPU_FOREACH(cpu) QTAILQ_FOREACH_RCU(cpu, &cpus, node) 493 #define CPU_FOREACH_SAFE(cpu, next_cpu) \ 494 QTAILQ_FOREACH_SAFE_RCU(cpu, &cpus, node, next_cpu) 495 496 extern __thread CPUState *current_cpu; 497 498 static inline void cpu_tb_jmp_cache_clear(CPUState *cpu) 499 { 500 unsigned int i; 501 502 for (i = 0; i < TB_JMP_CACHE_SIZE; i++) { 503 qatomic_set(&cpu->tb_jmp_cache[i], NULL); 504 } 505 } 506 507 /** 508 * qemu_tcg_mttcg_enabled: 509 * Check whether we are running MultiThread TCG or not. 510 * 511 * Returns: %true if we are in MTTCG mode %false otherwise. 512 */ 513 extern bool mttcg_enabled; 514 #define qemu_tcg_mttcg_enabled() (mttcg_enabled) 515 516 /** 517 * cpu_paging_enabled: 518 * @cpu: The CPU whose state is to be inspected. 519 * 520 * Returns: %true if paging is enabled, %false otherwise. 521 */ 522 bool cpu_paging_enabled(const CPUState *cpu); 523 524 /** 525 * cpu_get_memory_mapping: 526 * @cpu: The CPU whose memory mappings are to be obtained. 527 * @list: Where to write the memory mappings to. 528 * @errp: Pointer for reporting an #Error. 529 */ 530 void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list, 531 Error **errp); 532 533 #if !defined(CONFIG_USER_ONLY) 534 535 /** 536 * cpu_write_elf64_note: 537 * @f: pointer to a function that writes memory to a file 538 * @cpu: The CPU whose memory is to be dumped 539 * @cpuid: ID number of the CPU 540 * @opaque: pointer to the CPUState struct 541 */ 542 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu, 543 int cpuid, void *opaque); 544 545 /** 546 * cpu_write_elf64_qemunote: 547 * @f: pointer to a function that writes memory to a file 548 * @cpu: The CPU whose memory is to be dumped 549 * @cpuid: ID number of the CPU 550 * @opaque: pointer to the CPUState struct 551 */ 552 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu, 553 void *opaque); 554 555 /** 556 * cpu_write_elf32_note: 557 * @f: pointer to a function that writes memory to a file 558 * @cpu: The CPU whose memory is to be dumped 559 * @cpuid: ID number of the CPU 560 * @opaque: pointer to the CPUState struct 561 */ 562 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu, 563 int cpuid, void *opaque); 564 565 /** 566 * cpu_write_elf32_qemunote: 567 * @f: pointer to a function that writes memory to a file 568 * @cpu: The CPU whose memory is to be dumped 569 * @cpuid: ID number of the CPU 570 * @opaque: pointer to the CPUState struct 571 */ 572 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu, 573 void *opaque); 574 575 /** 576 * cpu_get_crash_info: 577 * @cpu: The CPU to get crash information for 578 * 579 * Gets the previously saved crash information. 580 * Caller is responsible for freeing the data. 581 */ 582 GuestPanicInformation *cpu_get_crash_info(CPUState *cpu); 583 584 #endif /* !CONFIG_USER_ONLY */ 585 586 /** 587 * CPUDumpFlags: 588 * @CPU_DUMP_CODE: 589 * @CPU_DUMP_FPU: dump FPU register state, not just integer 590 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state 591 */ 592 enum CPUDumpFlags { 593 CPU_DUMP_CODE = 0x00010000, 594 CPU_DUMP_FPU = 0x00020000, 595 CPU_DUMP_CCOP = 0x00040000, 596 }; 597 598 /** 599 * cpu_dump_state: 600 * @cpu: The CPU whose state is to be dumped. 601 * @f: If non-null, dump to this stream, else to current print sink. 602 * 603 * Dumps CPU state. 604 */ 605 void cpu_dump_state(CPUState *cpu, FILE *f, int flags); 606 607 /** 608 * cpu_dump_statistics: 609 * @cpu: The CPU whose state is to be dumped. 610 * @flags: Flags what to dump. 611 * 612 * Dump CPU statistics to the current monitor if we have one, else to 613 * stdout. 614 */ 615 void cpu_dump_statistics(CPUState *cpu, int flags); 616 617 #ifndef CONFIG_USER_ONLY 618 /** 619 * cpu_get_phys_page_attrs_debug: 620 * @cpu: The CPU to obtain the physical page address for. 621 * @addr: The virtual address. 622 * @attrs: Updated on return with the memory transaction attributes to use 623 * for this access. 624 * 625 * Obtains the physical page corresponding to a virtual one, together 626 * with the corresponding memory transaction attributes to use for the access. 627 * Use it only for debugging because no protection checks are done. 628 * 629 * Returns: Corresponding physical page address or -1 if no page found. 630 */ 631 static inline hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr, 632 MemTxAttrs *attrs) 633 { 634 CPUClass *cc = CPU_GET_CLASS(cpu); 635 636 if (cc->get_phys_page_attrs_debug) { 637 return cc->get_phys_page_attrs_debug(cpu, addr, attrs); 638 } 639 /* Fallback for CPUs which don't implement the _attrs_ hook */ 640 *attrs = MEMTXATTRS_UNSPECIFIED; 641 return cc->get_phys_page_debug(cpu, addr); 642 } 643 644 /** 645 * cpu_get_phys_page_debug: 646 * @cpu: The CPU to obtain the physical page address for. 647 * @addr: The virtual address. 648 * 649 * Obtains the physical page corresponding to a virtual one. 650 * Use it only for debugging because no protection checks are done. 651 * 652 * Returns: Corresponding physical page address or -1 if no page found. 653 */ 654 static inline hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr) 655 { 656 MemTxAttrs attrs = {}; 657 658 return cpu_get_phys_page_attrs_debug(cpu, addr, &attrs); 659 } 660 661 /** cpu_asidx_from_attrs: 662 * @cpu: CPU 663 * @attrs: memory transaction attributes 664 * 665 * Returns the address space index specifying the CPU AddressSpace 666 * to use for a memory access with the given transaction attributes. 667 */ 668 static inline int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs) 669 { 670 CPUClass *cc = CPU_GET_CLASS(cpu); 671 int ret = 0; 672 673 if (cc->asidx_from_attrs) { 674 ret = cc->asidx_from_attrs(cpu, attrs); 675 assert(ret < cpu->num_ases && ret >= 0); 676 } 677 return ret; 678 } 679 680 #endif /* CONFIG_USER_ONLY */ 681 682 /** 683 * cpu_list_add: 684 * @cpu: The CPU to be added to the list of CPUs. 685 */ 686 void cpu_list_add(CPUState *cpu); 687 688 /** 689 * cpu_list_remove: 690 * @cpu: The CPU to be removed from the list of CPUs. 691 */ 692 void cpu_list_remove(CPUState *cpu); 693 694 /** 695 * cpu_reset: 696 * @cpu: The CPU whose state is to be reset. 697 */ 698 void cpu_reset(CPUState *cpu); 699 700 /** 701 * cpu_class_by_name: 702 * @typename: The CPU base type. 703 * @cpu_model: The model string without any parameters. 704 * 705 * Looks up a CPU #ObjectClass matching name @cpu_model. 706 * 707 * Returns: A #CPUClass or %NULL if not matching class is found. 708 */ 709 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model); 710 711 /** 712 * cpu_create: 713 * @typename: The CPU type. 714 * 715 * Instantiates a CPU and realizes the CPU. 716 * 717 * Returns: A #CPUState or %NULL if an error occurred. 718 */ 719 CPUState *cpu_create(const char *typename); 720 721 /** 722 * parse_cpu_option: 723 * @cpu_option: The -cpu option including optional parameters. 724 * 725 * processes optional parameters and registers them as global properties 726 * 727 * Returns: type of CPU to create or prints error and terminates process 728 * if an error occurred. 729 */ 730 const char *parse_cpu_option(const char *cpu_option); 731 732 /** 733 * cpu_has_work: 734 * @cpu: The vCPU to check. 735 * 736 * Checks whether the CPU has work to do. 737 * 738 * Returns: %true if the CPU has work, %false otherwise. 739 */ 740 static inline bool cpu_has_work(CPUState *cpu) 741 { 742 CPUClass *cc = CPU_GET_CLASS(cpu); 743 744 g_assert(cc->has_work); 745 return cc->has_work(cpu); 746 } 747 748 /** 749 * qemu_cpu_is_self: 750 * @cpu: The vCPU to check against. 751 * 752 * Checks whether the caller is executing on the vCPU thread. 753 * 754 * Returns: %true if called from @cpu's thread, %false otherwise. 755 */ 756 bool qemu_cpu_is_self(CPUState *cpu); 757 758 /** 759 * qemu_cpu_kick: 760 * @cpu: The vCPU to kick. 761 * 762 * Kicks @cpu's thread. 763 */ 764 void qemu_cpu_kick(CPUState *cpu); 765 766 /** 767 * cpu_is_stopped: 768 * @cpu: The CPU to check. 769 * 770 * Checks whether the CPU is stopped. 771 * 772 * Returns: %true if run state is not running or if artificially stopped; 773 * %false otherwise. 774 */ 775 bool cpu_is_stopped(CPUState *cpu); 776 777 /** 778 * do_run_on_cpu: 779 * @cpu: The vCPU to run on. 780 * @func: The function to be executed. 781 * @data: Data to pass to the function. 782 * @mutex: Mutex to release while waiting for @func to run. 783 * 784 * Used internally in the implementation of run_on_cpu. 785 */ 786 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data, 787 QemuMutex *mutex); 788 789 /** 790 * run_on_cpu: 791 * @cpu: The vCPU to run on. 792 * @func: The function to be executed. 793 * @data: Data to pass to the function. 794 * 795 * Schedules the function @func for execution on the vCPU @cpu. 796 */ 797 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data); 798 799 /** 800 * async_run_on_cpu: 801 * @cpu: The vCPU to run on. 802 * @func: The function to be executed. 803 * @data: Data to pass to the function. 804 * 805 * Schedules the function @func for execution on the vCPU @cpu asynchronously. 806 */ 807 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data); 808 809 /** 810 * async_safe_run_on_cpu: 811 * @cpu: The vCPU to run on. 812 * @func: The function to be executed. 813 * @data: Data to pass to the function. 814 * 815 * Schedules the function @func for execution on the vCPU @cpu asynchronously, 816 * while all other vCPUs are sleeping. 817 * 818 * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the 819 * BQL. 820 */ 821 void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data); 822 823 /** 824 * cpu_in_exclusive_context() 825 * @cpu: The vCPU to check 826 * 827 * Returns true if @cpu is an exclusive context, for example running 828 * something which has previously been queued via async_safe_run_on_cpu(). 829 */ 830 static inline bool cpu_in_exclusive_context(const CPUState *cpu) 831 { 832 return cpu->in_exclusive_context; 833 } 834 835 /** 836 * qemu_get_cpu: 837 * @index: The CPUState@cpu_index value of the CPU to obtain. 838 * 839 * Gets a CPU matching @index. 840 * 841 * Returns: The CPU or %NULL if there is no matching CPU. 842 */ 843 CPUState *qemu_get_cpu(int index); 844 845 /** 846 * cpu_exists: 847 * @id: Guest-exposed CPU ID to lookup. 848 * 849 * Search for CPU with specified ID. 850 * 851 * Returns: %true - CPU is found, %false - CPU isn't found. 852 */ 853 bool cpu_exists(int64_t id); 854 855 /** 856 * cpu_by_arch_id: 857 * @id: Guest-exposed CPU ID of the CPU to obtain. 858 * 859 * Get a CPU with matching @id. 860 * 861 * Returns: The CPU or %NULL if there is no matching CPU. 862 */ 863 CPUState *cpu_by_arch_id(int64_t id); 864 865 /** 866 * cpu_interrupt: 867 * @cpu: The CPU to set an interrupt on. 868 * @mask: The interrupts to set. 869 * 870 * Invokes the interrupt handler. 871 */ 872 873 void cpu_interrupt(CPUState *cpu, int mask); 874 875 #ifdef NEED_CPU_H 876 877 #ifdef CONFIG_SOFTMMU 878 static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr, 879 MMUAccessType access_type, 880 int mmu_idx, uintptr_t retaddr) 881 { 882 CPUClass *cc = CPU_GET_CLASS(cpu); 883 884 cc->do_unaligned_access(cpu, addr, access_type, mmu_idx, retaddr); 885 } 886 887 static inline void cpu_transaction_failed(CPUState *cpu, hwaddr physaddr, 888 vaddr addr, unsigned size, 889 MMUAccessType access_type, 890 int mmu_idx, MemTxAttrs attrs, 891 MemTxResult response, 892 uintptr_t retaddr) 893 { 894 CPUClass *cc = CPU_GET_CLASS(cpu); 895 896 if (!cpu->ignore_memory_transaction_failures && cc->do_transaction_failed) { 897 cc->do_transaction_failed(cpu, physaddr, addr, size, access_type, 898 mmu_idx, attrs, response, retaddr); 899 } 900 } 901 #endif 902 903 #endif /* NEED_CPU_H */ 904 905 /** 906 * cpu_set_pc: 907 * @cpu: The CPU to set the program counter for. 908 * @addr: Program counter value. 909 * 910 * Sets the program counter for a CPU. 911 */ 912 static inline void cpu_set_pc(CPUState *cpu, vaddr addr) 913 { 914 CPUClass *cc = CPU_GET_CLASS(cpu); 915 916 cc->set_pc(cpu, addr); 917 } 918 919 /** 920 * cpu_reset_interrupt: 921 * @cpu: The CPU to clear the interrupt on. 922 * @mask: The interrupt mask to clear. 923 * 924 * Resets interrupts on the vCPU @cpu. 925 */ 926 void cpu_reset_interrupt(CPUState *cpu, int mask); 927 928 /** 929 * cpu_exit: 930 * @cpu: The CPU to exit. 931 * 932 * Requests the CPU @cpu to exit execution. 933 */ 934 void cpu_exit(CPUState *cpu); 935 936 /** 937 * cpu_resume: 938 * @cpu: The CPU to resume. 939 * 940 * Resumes CPU, i.e. puts CPU into runnable state. 941 */ 942 void cpu_resume(CPUState *cpu); 943 944 /** 945 * cpu_remove_sync: 946 * @cpu: The CPU to remove. 947 * 948 * Requests the CPU to be removed and waits till it is removed. 949 */ 950 void cpu_remove_sync(CPUState *cpu); 951 952 /** 953 * process_queued_cpu_work() - process all items on CPU work queue 954 * @cpu: The CPU which work queue to process. 955 */ 956 void process_queued_cpu_work(CPUState *cpu); 957 958 /** 959 * cpu_exec_start: 960 * @cpu: The CPU for the current thread. 961 * 962 * Record that a CPU has started execution and can be interrupted with 963 * cpu_exit. 964 */ 965 void cpu_exec_start(CPUState *cpu); 966 967 /** 968 * cpu_exec_end: 969 * @cpu: The CPU for the current thread. 970 * 971 * Record that a CPU has stopped execution and exclusive sections 972 * can be executed without interrupting it. 973 */ 974 void cpu_exec_end(CPUState *cpu); 975 976 /** 977 * start_exclusive: 978 * 979 * Wait for a concurrent exclusive section to end, and then start 980 * a section of work that is run while other CPUs are not running 981 * between cpu_exec_start and cpu_exec_end. CPUs that are running 982 * cpu_exec are exited immediately. CPUs that call cpu_exec_start 983 * during the exclusive section go to sleep until this CPU calls 984 * end_exclusive. 985 */ 986 void start_exclusive(void); 987 988 /** 989 * end_exclusive: 990 * 991 * Concludes an exclusive execution section started by start_exclusive. 992 */ 993 void end_exclusive(void); 994 995 /** 996 * qemu_init_vcpu: 997 * @cpu: The vCPU to initialize. 998 * 999 * Initializes a vCPU. 1000 */ 1001 void qemu_init_vcpu(CPUState *cpu); 1002 1003 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */ 1004 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */ 1005 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */ 1006 1007 /** 1008 * cpu_single_step: 1009 * @cpu: CPU to the flags for. 1010 * @enabled: Flags to enable. 1011 * 1012 * Enables or disables single-stepping for @cpu. 1013 */ 1014 void cpu_single_step(CPUState *cpu, int enabled); 1015 1016 /* Breakpoint/watchpoint flags */ 1017 #define BP_MEM_READ 0x01 1018 #define BP_MEM_WRITE 0x02 1019 #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE) 1020 #define BP_STOP_BEFORE_ACCESS 0x04 1021 /* 0x08 currently unused */ 1022 #define BP_GDB 0x10 1023 #define BP_CPU 0x20 1024 #define BP_ANY (BP_GDB | BP_CPU) 1025 #define BP_WATCHPOINT_HIT_READ 0x40 1026 #define BP_WATCHPOINT_HIT_WRITE 0x80 1027 #define BP_WATCHPOINT_HIT (BP_WATCHPOINT_HIT_READ | BP_WATCHPOINT_HIT_WRITE) 1028 1029 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags, 1030 CPUBreakpoint **breakpoint); 1031 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags); 1032 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint); 1033 void cpu_breakpoint_remove_all(CPUState *cpu, int mask); 1034 1035 /* Return true if PC matches an installed breakpoint. */ 1036 static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask) 1037 { 1038 CPUBreakpoint *bp; 1039 1040 if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) { 1041 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) { 1042 if (bp->pc == pc && (bp->flags & mask)) { 1043 return true; 1044 } 1045 } 1046 } 1047 return false; 1048 } 1049 1050 #ifdef CONFIG_USER_ONLY 1051 static inline int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len, 1052 int flags, CPUWatchpoint **watchpoint) 1053 { 1054 return -ENOSYS; 1055 } 1056 1057 static inline int cpu_watchpoint_remove(CPUState *cpu, vaddr addr, 1058 vaddr len, int flags) 1059 { 1060 return -ENOSYS; 1061 } 1062 1063 static inline void cpu_watchpoint_remove_by_ref(CPUState *cpu, 1064 CPUWatchpoint *wp) 1065 { 1066 } 1067 1068 static inline void cpu_watchpoint_remove_all(CPUState *cpu, int mask) 1069 { 1070 } 1071 1072 static inline void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len, 1073 MemTxAttrs atr, int fl, uintptr_t ra) 1074 { 1075 } 1076 1077 static inline int cpu_watchpoint_address_matches(CPUState *cpu, 1078 vaddr addr, vaddr len) 1079 { 1080 return 0; 1081 } 1082 #else 1083 int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len, 1084 int flags, CPUWatchpoint **watchpoint); 1085 int cpu_watchpoint_remove(CPUState *cpu, vaddr addr, 1086 vaddr len, int flags); 1087 void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint); 1088 void cpu_watchpoint_remove_all(CPUState *cpu, int mask); 1089 1090 /** 1091 * cpu_check_watchpoint: 1092 * @cpu: cpu context 1093 * @addr: guest virtual address 1094 * @len: access length 1095 * @attrs: memory access attributes 1096 * @flags: watchpoint access type 1097 * @ra: unwind return address 1098 * 1099 * Check for a watchpoint hit in [addr, addr+len) of the type 1100 * specified by @flags. Exit via exception with a hit. 1101 */ 1102 void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len, 1103 MemTxAttrs attrs, int flags, uintptr_t ra); 1104 1105 /** 1106 * cpu_watchpoint_address_matches: 1107 * @cpu: cpu context 1108 * @addr: guest virtual address 1109 * @len: access length 1110 * 1111 * Return the watchpoint flags that apply to [addr, addr+len). 1112 * If no watchpoint is registered for the range, the result is 0. 1113 */ 1114 int cpu_watchpoint_address_matches(CPUState *cpu, vaddr addr, vaddr len); 1115 #endif 1116 1117 /** 1118 * cpu_get_address_space: 1119 * @cpu: CPU to get address space from 1120 * @asidx: index identifying which address space to get 1121 * 1122 * Return the requested address space of this CPU. @asidx 1123 * specifies which address space to read. 1124 */ 1125 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx); 1126 1127 void QEMU_NORETURN cpu_abort(CPUState *cpu, const char *fmt, ...) 1128 GCC_FMT_ATTR(2, 3); 1129 1130 /* $(top_srcdir)/cpu.c */ 1131 void cpu_exec_initfn(CPUState *cpu); 1132 void cpu_exec_realizefn(CPUState *cpu, Error **errp); 1133 void cpu_exec_unrealizefn(CPUState *cpu); 1134 1135 /** 1136 * target_words_bigendian: 1137 * Returns true if the (default) endianness of the target is big endian, 1138 * false otherwise. Note that in target-specific code, you can use 1139 * TARGET_WORDS_BIGENDIAN directly instead. On the other hand, common 1140 * code should normally never need to know about the endianness of the 1141 * target, so please do *not* use this function unless you know very well 1142 * what you are doing! 1143 */ 1144 bool target_words_bigendian(void); 1145 1146 #ifdef NEED_CPU_H 1147 1148 #ifdef CONFIG_SOFTMMU 1149 extern const VMStateDescription vmstate_cpu_common; 1150 #else 1151 #define vmstate_cpu_common vmstate_dummy 1152 #endif 1153 1154 #define VMSTATE_CPU() { \ 1155 .name = "parent_obj", \ 1156 .size = sizeof(CPUState), \ 1157 .vmsd = &vmstate_cpu_common, \ 1158 .flags = VMS_STRUCT, \ 1159 .offset = 0, \ 1160 } 1161 1162 #endif /* NEED_CPU_H */ 1163 1164 #define UNASSIGNED_CPU_INDEX -1 1165 #define UNASSIGNED_CLUSTER_INDEX -1 1166 1167 #endif 1168