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