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/breakpoint.h" 26 #include "exec/hwaddr.h" 27 #include "exec/vaddr.h" 28 #include "exec/memattrs.h" 29 #include "exec/mmu-access-type.h" 30 #include "exec/tlb-common.h" 31 #include "qapi/qapi-types-machine.h" 32 #include "qapi/qapi-types-run-state.h" 33 #include "qemu/bitmap.h" 34 #include "qemu/rcu_queue.h" 35 #include "qemu/queue.h" 36 #include "qemu/lockcnt.h" 37 #include "qemu/thread.h" 38 #include "qom/object.h" 39 40 typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size, 41 void *opaque); 42 43 /** 44 * SECTION:cpu 45 * @section_id: QEMU-cpu 46 * @title: CPU Class 47 * @short_description: Base class for all CPUs 48 */ 49 50 #define TYPE_CPU "cpu" 51 52 /* Since this macro is used a lot in hot code paths and in conjunction with 53 * FooCPU *foo_env_get_cpu(), we deviate from usual QOM practice by using 54 * an unchecked cast. 55 */ 56 #define CPU(obj) ((CPUState *)(obj)) 57 58 /* 59 * The class checkers bring in CPU_GET_CLASS() which is potentially 60 * expensive given the eventual call to 61 * object_class_dynamic_cast_assert(). Because of this the CPUState 62 * has a cached value for the class in cs->cc which is set up in 63 * cpu_exec_realizefn() for use in hot code paths. 64 */ 65 typedef struct CPUClass CPUClass; 66 DECLARE_CLASS_CHECKERS(CPUClass, CPU, 67 TYPE_CPU) 68 69 /** 70 * OBJECT_DECLARE_CPU_TYPE: 71 * @CpuInstanceType: instance struct name 72 * @CpuClassType: class struct name 73 * @CPU_MODULE_OBJ_NAME: the CPU name in uppercase with underscore separators 74 * 75 * This macro is typically used in "cpu-qom.h" header file, and will: 76 * 77 * - create the typedefs for the CPU object and class structs 78 * - register the type for use with g_autoptr 79 * - provide three standard type cast functions 80 * 81 * The object struct and class struct need to be declared manually. 82 */ 83 #define OBJECT_DECLARE_CPU_TYPE(CpuInstanceType, CpuClassType, CPU_MODULE_OBJ_NAME) \ 84 typedef struct ArchCPU CpuInstanceType; \ 85 OBJECT_DECLARE_TYPE(ArchCPU, CpuClassType, CPU_MODULE_OBJ_NAME); 86 87 typedef struct CPUWatchpoint CPUWatchpoint; 88 89 /* see physmem.c */ 90 struct CPUAddressSpace; 91 92 /* see accel/tcg/tb-jmp-cache.h */ 93 struct CPUJumpCache; 94 95 /* see accel-cpu.h */ 96 struct AccelCPUClass; 97 98 /* see sysemu-cpu-ops.h */ 99 struct SysemuCPUOps; 100 101 /** 102 * CPUClass: 103 * @class_by_name: Callback to map -cpu command line model name to an 104 * instantiatable CPU type. 105 * @parse_features: Callback to parse command line arguments. 106 * @reset_dump_flags: #CPUDumpFlags to use for reset logging. 107 * @memory_rw_debug: Callback for GDB memory access. 108 * @dump_state: Callback for dumping state. 109 * @query_cpu_fast: 110 * Fill in target specific information for the "query-cpus-fast" 111 * QAPI call. 112 * @get_arch_id: Callback for getting architecture-dependent CPU ID. 113 * @set_pc: Callback for setting the Program Counter register. This 114 * should have the semantics used by the target architecture when 115 * setting the PC from a source such as an ELF file entry point; 116 * for example on Arm it will also set the Thumb mode bit based 117 * on the least significant bit of the new PC value. 118 * If the target behaviour here is anything other than "set 119 * the PC register to the value passed in" then the target must 120 * also implement the synchronize_from_tb hook. 121 * @get_pc: Callback for getting the Program Counter register. 122 * As above, with the semantics of the target architecture. 123 * @gdb_read_register: Callback for letting GDB read a register. 124 * No more than @gdb_num_core_regs registers can be read. 125 * @gdb_write_register: Callback for letting GDB write a register. 126 * No more than @gdb_num_core_regs registers can be written. 127 * @gdb_adjust_breakpoint: Callback for adjusting the address of a 128 * breakpoint. Used by AVR to handle a gdb mis-feature with 129 * its Harvard architecture split code and data. 130 * @gdb_num_core_regs: Number of core registers accessible to GDB or 0 to infer 131 * from @gdb_core_xml_file. 132 * @gdb_core_xml_file: File name for core registers GDB XML description. 133 * @gdb_stop_before_watchpoint: Indicates whether GDB expects the CPU to stop 134 * before the insn which triggers a watchpoint rather than after it. 135 * @gdb_arch_name: Optional callback that returns the architecture name known 136 * to GDB. The caller must free the returned string with g_free. 137 * @disas_set_info: Setup architecture specific components of disassembly info 138 * @adjust_watchpoint_address: Perform a target-specific adjustment to an 139 * address before attempting to match it against watchpoints. 140 * @deprecation_note: If this CPUClass is deprecated, this field provides 141 * related information. 142 * 143 * Represents a CPU family or model. 144 */ 145 struct CPUClass { 146 /*< private >*/ 147 DeviceClass parent_class; 148 /*< public >*/ 149 150 ObjectClass *(*class_by_name)(const char *cpu_model); 151 void (*parse_features)(const char *typename, char *str, Error **errp); 152 153 int (*memory_rw_debug)(CPUState *cpu, vaddr addr, 154 uint8_t *buf, size_t len, bool is_write); 155 void (*dump_state)(CPUState *cpu, FILE *, int flags); 156 void (*query_cpu_fast)(CPUState *cpu, CpuInfoFast *value); 157 int64_t (*get_arch_id)(CPUState *cpu); 158 void (*set_pc)(CPUState *cpu, vaddr value); 159 vaddr (*get_pc)(CPUState *cpu); 160 int (*gdb_read_register)(CPUState *cpu, GByteArray *buf, int reg); 161 int (*gdb_write_register)(CPUState *cpu, uint8_t *buf, int reg); 162 vaddr (*gdb_adjust_breakpoint)(CPUState *cpu, vaddr addr); 163 164 const char *gdb_core_xml_file; 165 const gchar * (*gdb_arch_name)(CPUState *cpu); 166 167 void (*disas_set_info)(CPUState *cpu, disassemble_info *info); 168 169 const char *deprecation_note; 170 struct AccelCPUClass *accel_cpu; 171 172 /* when system emulation is not available, this pointer is NULL */ 173 const struct SysemuCPUOps *sysemu_ops; 174 175 /* when TCG is not available, this pointer is NULL */ 176 const TCGCPUOps *tcg_ops; 177 178 /* 179 * if not NULL, this is called in order for the CPUClass to initialize 180 * class data that depends on the accelerator, see accel/accel-common.c. 181 */ 182 void (*init_accel_cpu)(struct AccelCPUClass *accel_cpu, CPUClass *cc); 183 184 /* 185 * Keep non-pointer data at the end to minimize holes. 186 */ 187 int reset_dump_flags; 188 int gdb_num_core_regs; 189 bool gdb_stop_before_watchpoint; 190 }; 191 192 /* 193 * Fix the number of mmu modes to 16, which is also the maximum 194 * supported by the softmmu tlb api. 195 */ 196 #define NB_MMU_MODES 16 197 198 /* Use a fully associative victim tlb of 8 entries. */ 199 #define CPU_VTLB_SIZE 8 200 201 /* 202 * The full TLB entry, which is not accessed by generated TCG code, 203 * so the layout is not as critical as that of CPUTLBEntry. This is 204 * also why we don't want to combine the two structs. 205 */ 206 struct CPUTLBEntryFull { 207 /* 208 * @xlat_section contains: 209 * - in the lower TARGET_PAGE_BITS, a physical section number 210 * - with the lower TARGET_PAGE_BITS masked off, an offset which 211 * must be added to the virtual address to obtain: 212 * + the ram_addr_t of the target RAM (if the physical section 213 * number is PHYS_SECTION_NOTDIRTY or PHYS_SECTION_ROM) 214 * + the offset within the target MemoryRegion (otherwise) 215 */ 216 hwaddr xlat_section; 217 218 /* 219 * @phys_addr contains the physical address in the address space 220 * given by cpu_asidx_from_attrs(cpu, @attrs). 221 */ 222 hwaddr phys_addr; 223 224 /* @attrs contains the memory transaction attributes for the page. */ 225 MemTxAttrs attrs; 226 227 /* @prot contains the complete protections for the page. */ 228 uint8_t prot; 229 230 /* @lg_page_size contains the log2 of the page size. */ 231 uint8_t lg_page_size; 232 233 /* Additional tlb flags requested by tlb_fill. */ 234 uint8_t tlb_fill_flags; 235 236 /* 237 * Additional tlb flags for use by the slow path. If non-zero, 238 * the corresponding CPUTLBEntry comparator must have TLB_FORCE_SLOW. 239 */ 240 uint8_t slow_flags[MMU_ACCESS_COUNT]; 241 242 /* 243 * Allow target-specific additions to this structure. 244 * This may be used to cache items from the guest cpu 245 * page tables for later use by the implementation. 246 */ 247 union { 248 /* 249 * Cache the attrs and shareability fields from the page table entry. 250 * 251 * For ARMMMUIdx_Stage2*, pte_attrs is the S2 descriptor bits [5:2]. 252 * Otherwise, pte_attrs is the same as the MAIR_EL1 8-bit format. 253 * For shareability and guarded, as in the SH and GP fields respectively 254 * of the VMSAv8-64 PTEs. 255 */ 256 struct { 257 uint8_t pte_attrs; 258 uint8_t shareability; 259 bool guarded; 260 } arm; 261 } extra; 262 }; 263 264 /* 265 * Data elements that are per MMU mode, minus the bits accessed by 266 * the TCG fast path. 267 */ 268 typedef struct CPUTLBDesc { 269 /* 270 * Describe a region covering all of the large pages allocated 271 * into the tlb. When any page within this region is flushed, 272 * we must flush the entire tlb. The region is matched if 273 * (addr & large_page_mask) == large_page_addr. 274 */ 275 vaddr large_page_addr; 276 vaddr large_page_mask; 277 /* host time (in ns) at the beginning of the time window */ 278 int64_t window_begin_ns; 279 /* maximum number of entries observed in the window */ 280 size_t window_max_entries; 281 size_t n_used_entries; 282 /* The next index to use in the tlb victim table. */ 283 size_t vindex; 284 /* The tlb victim table, in two parts. */ 285 CPUTLBEntry vtable[CPU_VTLB_SIZE]; 286 CPUTLBEntryFull vfulltlb[CPU_VTLB_SIZE]; 287 CPUTLBEntryFull *fulltlb; 288 } CPUTLBDesc; 289 290 /* 291 * Data elements that are shared between all MMU modes. 292 */ 293 typedef struct CPUTLBCommon { 294 /* Serialize updates to f.table and d.vtable, and others as noted. */ 295 QemuSpin lock; 296 /* 297 * Within dirty, for each bit N, modifications have been made to 298 * mmu_idx N since the last time that mmu_idx was flushed. 299 * Protected by tlb_c.lock. 300 */ 301 uint16_t dirty; 302 /* 303 * Statistics. These are not lock protected, but are read and 304 * written atomically. This allows the monitor to print a snapshot 305 * of the stats without interfering with the cpu. 306 */ 307 size_t full_flush_count; 308 size_t part_flush_count; 309 size_t elide_flush_count; 310 } CPUTLBCommon; 311 312 /* 313 * The entire softmmu tlb, for all MMU modes. 314 * The meaning of each of the MMU modes is defined in the target code. 315 * Since this is placed within CPUNegativeOffsetState, the smallest 316 * negative offsets are at the end of the struct. 317 */ 318 typedef struct CPUTLB { 319 #ifdef CONFIG_TCG 320 CPUTLBCommon c; 321 CPUTLBDesc d[NB_MMU_MODES]; 322 CPUTLBDescFast f[NB_MMU_MODES]; 323 #endif 324 } CPUTLB; 325 326 /* 327 * Low 16 bits: number of cycles left, used only in icount mode. 328 * High 16 bits: Set to -1 to force TCG to stop executing linked TBs 329 * for this CPU and return to its top level loop (even in non-icount mode). 330 * This allows a single read-compare-cbranch-write sequence to test 331 * for both decrementer underflow and exceptions. 332 */ 333 typedef union IcountDecr { 334 uint32_t u32; 335 struct { 336 #if HOST_BIG_ENDIAN 337 uint16_t high; 338 uint16_t low; 339 #else 340 uint16_t low; 341 uint16_t high; 342 #endif 343 } u16; 344 } IcountDecr; 345 346 /** 347 * CPUNegativeOffsetState: Elements of CPUState most efficiently accessed 348 * from CPUArchState, via small negative offsets. 349 * @can_do_io: True if memory-mapped IO is allowed. 350 * @plugin_mem_cbs: active plugin memory callbacks 351 * @plugin_mem_value_low: 64 lower bits of latest accessed mem value. 352 * @plugin_mem_value_high: 64 higher bits of latest accessed mem value. 353 */ 354 typedef struct CPUNegativeOffsetState { 355 CPUTLB tlb; 356 #ifdef CONFIG_PLUGIN 357 /* 358 * The callback pointer are accessed via TCG (see gen_empty_mem_helper). 359 */ 360 GArray *plugin_mem_cbs; 361 uint64_t plugin_mem_value_low; 362 uint64_t plugin_mem_value_high; 363 #endif 364 IcountDecr icount_decr; 365 bool can_do_io; 366 } CPUNegativeOffsetState; 367 368 struct KVMState; 369 struct kvm_run; 370 371 /* work queue */ 372 373 /* The union type allows passing of 64 bit target pointers on 32 bit 374 * hosts in a single parameter 375 */ 376 typedef union { 377 int host_int; 378 unsigned long host_ulong; 379 void *host_ptr; 380 vaddr target_ptr; 381 } run_on_cpu_data; 382 383 #define RUN_ON_CPU_HOST_PTR(p) ((run_on_cpu_data){.host_ptr = (p)}) 384 #define RUN_ON_CPU_HOST_INT(i) ((run_on_cpu_data){.host_int = (i)}) 385 #define RUN_ON_CPU_HOST_ULONG(ul) ((run_on_cpu_data){.host_ulong = (ul)}) 386 #define RUN_ON_CPU_TARGET_PTR(v) ((run_on_cpu_data){.target_ptr = (v)}) 387 #define RUN_ON_CPU_NULL RUN_ON_CPU_HOST_PTR(NULL) 388 389 typedef void (*run_on_cpu_func)(CPUState *cpu, run_on_cpu_data data); 390 391 struct qemu_work_item; 392 393 #define CPU_UNSET_NUMA_NODE_ID -1 394 395 /** 396 * struct CPUState - common state of one CPU core or thread. 397 * 398 * @cpu_index: CPU index (informative). 399 * @cluster_index: Identifies which cluster this CPU is in. 400 * For boards which don't define clusters or for "loose" CPUs not assigned 401 * to a cluster this will be UNASSIGNED_CLUSTER_INDEX; otherwise it will 402 * be the same as the cluster-id property of the CPU object's TYPE_CPU_CLUSTER 403 * QOM parent. 404 * Under TCG this value is propagated to @tcg_cflags. 405 * See TranslationBlock::TCG CF_CLUSTER_MASK. 406 * @tcg_cflags: Pre-computed cflags for this cpu. 407 * @nr_threads: Number of threads within this CPU core. 408 * @thread: Host thread details, only live once @created is #true 409 * @sem: WIN32 only semaphore used only for qtest 410 * @thread_id: native thread id of vCPU, only live once @created is #true 411 * @running: #true if CPU is currently running (lockless). 412 * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end; 413 * valid under cpu_list_lock. 414 * @created: Indicates whether the CPU thread has been successfully created. 415 * @halt_cond: condition variable sleeping threads can wait on. 416 * @interrupt_request: Indicates a pending interrupt request. 417 * @halted: Nonzero if the CPU is in suspended state. 418 * @stop: Indicates a pending stop request. 419 * @stopped: Indicates the CPU has been artificially stopped. 420 * @unplug: Indicates a pending CPU unplug request. 421 * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU 422 * @singlestep_enabled: Flags for single-stepping. 423 * @icount_extra: Instructions until next timer event. 424 * @cpu_ases: Pointer to array of CPUAddressSpaces (which define the 425 * AddressSpaces this CPU has) 426 * @num_ases: number of CPUAddressSpaces in @cpu_ases 427 * @as: Pointer to the first AddressSpace, for the convenience of targets which 428 * only have a single AddressSpace 429 * @gdb_regs: Additional GDB registers. 430 * @gdb_num_regs: Number of total registers accessible to GDB. 431 * @gdb_num_g_regs: Number of registers in GDB 'g' packets. 432 * @node: QTAILQ of CPUs sharing TB cache. 433 * @opaque: User data. 434 * @mem_io_pc: Host Program Counter at which the memory was accessed. 435 * @accel: Pointer to accelerator specific state. 436 * @kvm_fd: vCPU file descriptor for KVM. 437 * @work_mutex: Lock to prevent multiple access to @work_list. 438 * @work_list: List of pending asynchronous work. 439 * @plugin_state: per-CPU plugin state 440 * @ignore_memory_transaction_failures: Cached copy of the MachineState 441 * flag of the same name: allows the board to suppress calling of the 442 * CPU do_transaction_failed hook function. 443 * @kvm_dirty_gfns: Points to the KVM dirty ring for this CPU when KVM dirty 444 * ring is enabled. 445 * @kvm_fetch_index: Keeps the index that we last fetched from the per-vCPU 446 * dirty ring structure. 447 * 448 * @neg_align: The CPUState is the common part of a concrete ArchCPU 449 * which is allocated when an individual CPU instance is created. As 450 * such care is taken is ensure there is no gap between between 451 * CPUState and CPUArchState within ArchCPU. 452 * 453 * @neg: The architectural register state ("cpu_env") immediately follows 454 * CPUState in ArchCPU and is passed to TCG code. The @neg structure holds 455 * some common TCG CPU variables which are accessed with a negative offset 456 * from cpu_env. 457 */ 458 struct CPUState { 459 /*< private >*/ 460 DeviceState parent_obj; 461 /* cache to avoid expensive CPU_GET_CLASS */ 462 CPUClass *cc; 463 /*< public >*/ 464 465 int nr_threads; 466 467 struct QemuThread *thread; 468 #ifdef _WIN32 469 QemuSemaphore sem; 470 #endif 471 int thread_id; 472 bool running, has_waiter; 473 struct QemuCond *halt_cond; 474 bool thread_kicked; 475 bool created; 476 bool stop; 477 bool stopped; 478 479 /* Should CPU start in powered-off state? */ 480 bool start_powered_off; 481 482 bool unplug; 483 bool crash_occurred; 484 bool exit_request; 485 int exclusive_context_count; 486 uint32_t cflags_next_tb; 487 /* updates protected by BQL */ 488 uint32_t interrupt_request; 489 int singlestep_enabled; 490 int64_t icount_budget; 491 int64_t icount_extra; 492 uint64_t random_seed; 493 sigjmp_buf jmp_env; 494 495 QemuMutex work_mutex; 496 QSIMPLEQ_HEAD(, qemu_work_item) work_list; 497 498 struct CPUAddressSpace *cpu_ases; 499 int cpu_ases_count; 500 int num_ases; 501 AddressSpace *as; 502 MemoryRegion *memory; 503 504 struct CPUJumpCache *tb_jmp_cache; 505 506 GArray *gdb_regs; 507 int gdb_num_regs; 508 int gdb_num_g_regs; 509 QTAILQ_ENTRY(CPUState) node; 510 511 /* ice debug support */ 512 QTAILQ_HEAD(, CPUBreakpoint) breakpoints; 513 514 QTAILQ_HEAD(, CPUWatchpoint) watchpoints; 515 CPUWatchpoint *watchpoint_hit; 516 517 void *opaque; 518 519 /* In order to avoid passing too many arguments to the MMIO helpers, 520 * we store some rarely used information in the CPU context. 521 */ 522 uintptr_t mem_io_pc; 523 524 /* Only used in KVM */ 525 int kvm_fd; 526 struct KVMState *kvm_state; 527 struct kvm_run *kvm_run; 528 struct kvm_dirty_gfn *kvm_dirty_gfns; 529 uint32_t kvm_fetch_index; 530 uint64_t dirty_pages; 531 int kvm_vcpu_stats_fd; 532 bool vcpu_dirty; 533 534 /* Use by accel-block: CPU is executing an ioctl() */ 535 QemuLockCnt in_ioctl_lock; 536 537 #ifdef CONFIG_PLUGIN 538 CPUPluginState *plugin_state; 539 #endif 540 541 /* TODO Move common fields from CPUArchState here. */ 542 int cpu_index; 543 int cluster_index; 544 uint32_t tcg_cflags; 545 uint32_t halted; 546 int32_t exception_index; 547 548 AccelCPUState *accel; 549 550 /* Used to keep track of an outstanding cpu throttle thread for migration 551 * autoconverge 552 */ 553 bool throttle_thread_scheduled; 554 555 /* 556 * Sleep throttle_us_per_full microseconds once dirty ring is full 557 * if dirty page rate limit is enabled. 558 */ 559 int64_t throttle_us_per_full; 560 561 bool ignore_memory_transaction_failures; 562 563 /* Used for user-only emulation of prctl(PR_SET_UNALIGN). */ 564 bool prctl_unalign_sigbus; 565 566 /* track IOMMUs whose translations we've cached in the TCG TLB */ 567 GArray *iommu_notifiers; 568 569 /* 570 * MUST BE LAST in order to minimize the displacement to CPUArchState. 571 */ 572 char neg_align[-sizeof(CPUNegativeOffsetState) % 16] QEMU_ALIGNED(16); 573 CPUNegativeOffsetState neg; 574 }; 575 576 /* Validate placement of CPUNegativeOffsetState. */ 577 QEMU_BUILD_BUG_ON(offsetof(CPUState, neg) != 578 sizeof(CPUState) - sizeof(CPUNegativeOffsetState)); 579 580 static inline CPUArchState *cpu_env(CPUState *cpu) 581 { 582 /* We validate that CPUArchState follows CPUState in cpu-target.c */ 583 return (CPUArchState *)(cpu + 1); 584 } 585 586 typedef QTAILQ_HEAD(CPUTailQ, CPUState) CPUTailQ; 587 extern CPUTailQ cpus_queue; 588 589 #define first_cpu QTAILQ_FIRST_RCU(&cpus_queue) 590 #define CPU_NEXT(cpu) QTAILQ_NEXT_RCU(cpu, node) 591 #define CPU_FOREACH(cpu) QTAILQ_FOREACH_RCU(cpu, &cpus_queue, node) 592 #define CPU_FOREACH_SAFE(cpu, next_cpu) \ 593 QTAILQ_FOREACH_SAFE_RCU(cpu, &cpus_queue, node, next_cpu) 594 595 extern __thread CPUState *current_cpu; 596 597 /** 598 * cpu_paging_enabled: 599 * @cpu: The CPU whose state is to be inspected. 600 * 601 * Returns: %true if paging is enabled, %false otherwise. 602 */ 603 bool cpu_paging_enabled(const CPUState *cpu); 604 605 /** 606 * cpu_get_memory_mapping: 607 * @cpu: The CPU whose memory mappings are to be obtained. 608 * @list: Where to write the memory mappings to. 609 * @errp: Pointer for reporting an #Error. 610 * 611 * Returns: %true on success, %false otherwise. 612 */ 613 bool cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list, 614 Error **errp); 615 616 /** 617 * cpu_write_elf64_note: 618 * @f: pointer to a function that writes memory to a file 619 * @cpu: The CPU whose memory is to be dumped 620 * @cpuid: ID number of the CPU 621 * @opaque: pointer to the CPUState struct 622 */ 623 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu, 624 int cpuid, void *opaque); 625 626 /** 627 * cpu_write_elf64_qemunote: 628 * @f: pointer to a function that writes memory to a file 629 * @cpu: The CPU whose memory is to be dumped 630 * @cpuid: ID number of the CPU 631 * @opaque: pointer to the CPUState struct 632 */ 633 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu, 634 void *opaque); 635 636 /** 637 * cpu_write_elf32_note: 638 * @f: pointer to a function that writes memory to a file 639 * @cpu: The CPU whose memory is to be dumped 640 * @cpuid: ID number of the CPU 641 * @opaque: pointer to the CPUState struct 642 */ 643 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu, 644 int cpuid, void *opaque); 645 646 /** 647 * cpu_write_elf32_qemunote: 648 * @f: pointer to a function that writes memory to a file 649 * @cpu: The CPU whose memory is to be dumped 650 * @cpuid: ID number of the CPU 651 * @opaque: pointer to the CPUState struct 652 */ 653 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu, 654 void *opaque); 655 656 /** 657 * cpu_get_crash_info: 658 * @cpu: The CPU to get crash information for 659 * 660 * Gets the previously saved crash information. 661 * Caller is responsible for freeing the data. 662 */ 663 GuestPanicInformation *cpu_get_crash_info(CPUState *cpu); 664 665 /** 666 * CPUDumpFlags: 667 * @CPU_DUMP_CODE: 668 * @CPU_DUMP_FPU: dump FPU register state, not just integer 669 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state 670 * @CPU_DUMP_VPU: dump VPU registers 671 */ 672 enum CPUDumpFlags { 673 CPU_DUMP_CODE = 0x00010000, 674 CPU_DUMP_FPU = 0x00020000, 675 CPU_DUMP_CCOP = 0x00040000, 676 CPU_DUMP_VPU = 0x00080000, 677 }; 678 679 /** 680 * cpu_dump_state: 681 * @cpu: The CPU whose state is to be dumped. 682 * @f: If non-null, dump to this stream, else to current print sink. 683 * 684 * Dumps CPU state. 685 */ 686 void cpu_dump_state(CPUState *cpu, FILE *f, int flags); 687 688 /** 689 * cpu_get_phys_page_attrs_debug: 690 * @cpu: The CPU to obtain the physical page address for. 691 * @addr: The virtual address. 692 * @attrs: Updated on return with the memory transaction attributes to use 693 * for this access. 694 * 695 * Obtains the physical page corresponding to a virtual one, together 696 * with the corresponding memory transaction attributes to use for the access. 697 * Use it only for debugging because no protection checks are done. 698 * 699 * Returns: Corresponding physical page address or -1 if no page found. 700 */ 701 hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr, 702 MemTxAttrs *attrs); 703 704 /** 705 * cpu_get_phys_page_debug: 706 * @cpu: The CPU to obtain the physical page address for. 707 * @addr: The virtual address. 708 * 709 * Obtains the physical page corresponding to a virtual one. 710 * Use it only for debugging because no protection checks are done. 711 * 712 * Returns: Corresponding physical page address or -1 if no page found. 713 */ 714 hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr); 715 716 /** cpu_asidx_from_attrs: 717 * @cpu: CPU 718 * @attrs: memory transaction attributes 719 * 720 * Returns the address space index specifying the CPU AddressSpace 721 * to use for a memory access with the given transaction attributes. 722 */ 723 int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs); 724 725 /** 726 * cpu_virtio_is_big_endian: 727 * @cpu: CPU 728 729 * Returns %true if a CPU which supports runtime configurable endianness 730 * is currently big-endian. 731 */ 732 bool cpu_virtio_is_big_endian(CPUState *cpu); 733 734 /** 735 * cpu_has_work: 736 * @cpu: The vCPU to check. 737 * 738 * Checks whether the CPU has work to do. 739 * 740 * Returns: %true if the CPU has work, %false otherwise. 741 */ 742 bool cpu_has_work(CPUState *cpu); 743 744 /** 745 * cpu_list_add: 746 * @cpu: The CPU to be added to the list of CPUs. 747 */ 748 void cpu_list_add(CPUState *cpu); 749 750 /** 751 * cpu_list_remove: 752 * @cpu: The CPU to be removed from the list of CPUs. 753 */ 754 void cpu_list_remove(CPUState *cpu); 755 756 /** 757 * cpu_reset: 758 * @cpu: The CPU whose state is to be reset. 759 */ 760 void cpu_reset(CPUState *cpu); 761 762 /** 763 * cpu_class_by_name: 764 * @typename: The CPU base type. 765 * @cpu_model: The model string without any parameters. 766 * 767 * Looks up a concrete CPU #ObjectClass matching name @cpu_model. 768 * 769 * Returns: A concrete #CPUClass or %NULL if no matching class is found 770 * or if the matching class is abstract. 771 */ 772 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model); 773 774 /** 775 * cpu_model_from_type: 776 * @typename: The CPU type name 777 * 778 * Extract the CPU model name from the CPU type name. The 779 * CPU type name is either the combination of the CPU model 780 * name and suffix, or same to the CPU model name. 781 * 782 * Returns: CPU model name or NULL if the CPU class doesn't exist 783 * The user should g_free() the string once no longer needed. 784 */ 785 char *cpu_model_from_type(const char *typename); 786 787 /** 788 * cpu_create: 789 * @typename: The CPU type. 790 * 791 * Instantiates a CPU and realizes the CPU. 792 * 793 * Returns: A #CPUState or %NULL if an error occurred. 794 */ 795 CPUState *cpu_create(const char *typename); 796 797 /** 798 * parse_cpu_option: 799 * @cpu_option: The -cpu option including optional parameters. 800 * 801 * processes optional parameters and registers them as global properties 802 * 803 * Returns: type of CPU to create or prints error and terminates process 804 * if an error occurred. 805 */ 806 const char *parse_cpu_option(const char *cpu_option); 807 808 /** 809 * qemu_cpu_is_self: 810 * @cpu: The vCPU to check against. 811 * 812 * Checks whether the caller is executing on the vCPU thread. 813 * 814 * Returns: %true if called from @cpu's thread, %false otherwise. 815 */ 816 bool qemu_cpu_is_self(CPUState *cpu); 817 818 /** 819 * qemu_cpu_kick: 820 * @cpu: The vCPU to kick. 821 * 822 * Kicks @cpu's thread. 823 */ 824 void qemu_cpu_kick(CPUState *cpu); 825 826 /** 827 * cpu_is_stopped: 828 * @cpu: The CPU to check. 829 * 830 * Checks whether the CPU is stopped. 831 * 832 * Returns: %true if run state is not running or if artificially stopped; 833 * %false otherwise. 834 */ 835 bool cpu_is_stopped(CPUState *cpu); 836 837 /** 838 * do_run_on_cpu: 839 * @cpu: The vCPU to run on. 840 * @func: The function to be executed. 841 * @data: Data to pass to the function. 842 * @mutex: Mutex to release while waiting for @func to run. 843 * 844 * Used internally in the implementation of run_on_cpu. 845 */ 846 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data, 847 QemuMutex *mutex); 848 849 /** 850 * run_on_cpu: 851 * @cpu: The vCPU to run on. 852 * @func: The function to be executed. 853 * @data: Data to pass to the function. 854 * 855 * Schedules the function @func for execution on the vCPU @cpu. 856 */ 857 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data); 858 859 /** 860 * async_run_on_cpu: 861 * @cpu: The vCPU to run on. 862 * @func: The function to be executed. 863 * @data: Data to pass to the function. 864 * 865 * Schedules the function @func for execution on the vCPU @cpu asynchronously. 866 */ 867 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data); 868 869 /** 870 * async_safe_run_on_cpu: 871 * @cpu: The vCPU to run on. 872 * @func: The function to be executed. 873 * @data: Data to pass to the function. 874 * 875 * Schedules the function @func for execution on the vCPU @cpu asynchronously, 876 * while all other vCPUs are sleeping. 877 * 878 * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the 879 * BQL. 880 */ 881 void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data); 882 883 /** 884 * cpu_in_exclusive_context() 885 * @cpu: The vCPU to check 886 * 887 * Returns true if @cpu is an exclusive context, for example running 888 * something which has previously been queued via async_safe_run_on_cpu(). 889 */ 890 static inline bool cpu_in_exclusive_context(const CPUState *cpu) 891 { 892 return cpu->exclusive_context_count; 893 } 894 895 /** 896 * qemu_get_cpu: 897 * @index: The CPUState@cpu_index value of the CPU to obtain. 898 * 899 * Gets a CPU matching @index. 900 * 901 * Returns: The CPU or %NULL if there is no matching CPU. 902 */ 903 CPUState *qemu_get_cpu(int index); 904 905 /** 906 * cpu_exists: 907 * @id: Guest-exposed CPU ID to lookup. 908 * 909 * Search for CPU with specified ID. 910 * 911 * Returns: %true - CPU is found, %false - CPU isn't found. 912 */ 913 bool cpu_exists(int64_t id); 914 915 /** 916 * cpu_by_arch_id: 917 * @id: Guest-exposed CPU ID of the CPU to obtain. 918 * 919 * Get a CPU with matching @id. 920 * 921 * Returns: The CPU or %NULL if there is no matching CPU. 922 */ 923 CPUState *cpu_by_arch_id(int64_t id); 924 925 /** 926 * cpu_interrupt: 927 * @cpu: The CPU to set an interrupt on. 928 * @mask: The interrupts to set. 929 * 930 * Invokes the interrupt handler. 931 */ 932 933 void cpu_interrupt(CPUState *cpu, int mask); 934 935 /** 936 * cpu_set_pc: 937 * @cpu: The CPU to set the program counter for. 938 * @addr: Program counter value. 939 * 940 * Sets the program counter for a CPU. 941 */ 942 static inline void cpu_set_pc(CPUState *cpu, vaddr addr) 943 { 944 cpu->cc->set_pc(cpu, addr); 945 } 946 947 /** 948 * cpu_reset_interrupt: 949 * @cpu: The CPU to clear the interrupt on. 950 * @mask: The interrupt mask to clear. 951 * 952 * Resets interrupts on the vCPU @cpu. 953 */ 954 void cpu_reset_interrupt(CPUState *cpu, int mask); 955 956 /** 957 * cpu_exit: 958 * @cpu: The CPU to exit. 959 * 960 * Requests the CPU @cpu to exit execution. 961 */ 962 void cpu_exit(CPUState *cpu); 963 964 /** 965 * cpu_pause: 966 * @cpu: The CPU to pause. 967 * 968 * Pauses CPU, i.e. puts CPU into stopped state. 969 */ 970 void cpu_pause(CPUState *cpu); 971 972 /** 973 * cpu_resume: 974 * @cpu: The CPU to resume. 975 * 976 * Resumes CPU, i.e. puts CPU into runnable state. 977 */ 978 void cpu_resume(CPUState *cpu); 979 980 /** 981 * cpu_remove_sync: 982 * @cpu: The CPU to remove. 983 * 984 * Requests the CPU to be removed and waits till it is removed. 985 */ 986 void cpu_remove_sync(CPUState *cpu); 987 988 /** 989 * free_queued_cpu_work() - free all items on CPU work queue 990 * @cpu: The CPU which work queue to free. 991 */ 992 void free_queued_cpu_work(CPUState *cpu); 993 994 /** 995 * process_queued_cpu_work() - process all items on CPU work queue 996 * @cpu: The CPU which work queue to process. 997 */ 998 void process_queued_cpu_work(CPUState *cpu); 999 1000 /** 1001 * cpu_exec_start: 1002 * @cpu: The CPU for the current thread. 1003 * 1004 * Record that a CPU has started execution and can be interrupted with 1005 * cpu_exit. 1006 */ 1007 void cpu_exec_start(CPUState *cpu); 1008 1009 /** 1010 * cpu_exec_end: 1011 * @cpu: The CPU for the current thread. 1012 * 1013 * Record that a CPU has stopped execution and exclusive sections 1014 * can be executed without interrupting it. 1015 */ 1016 void cpu_exec_end(CPUState *cpu); 1017 1018 /** 1019 * start_exclusive: 1020 * 1021 * Wait for a concurrent exclusive section to end, and then start 1022 * a section of work that is run while other CPUs are not running 1023 * between cpu_exec_start and cpu_exec_end. CPUs that are running 1024 * cpu_exec are exited immediately. CPUs that call cpu_exec_start 1025 * during the exclusive section go to sleep until this CPU calls 1026 * end_exclusive. 1027 */ 1028 void start_exclusive(void); 1029 1030 /** 1031 * end_exclusive: 1032 * 1033 * Concludes an exclusive execution section started by start_exclusive. 1034 */ 1035 void end_exclusive(void); 1036 1037 /** 1038 * qemu_init_vcpu: 1039 * @cpu: The vCPU to initialize. 1040 * 1041 * Initializes a vCPU. 1042 */ 1043 void qemu_init_vcpu(CPUState *cpu); 1044 1045 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */ 1046 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */ 1047 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */ 1048 1049 /** 1050 * cpu_single_step: 1051 * @cpu: CPU to the flags for. 1052 * @enabled: Flags to enable. 1053 * 1054 * Enables or disables single-stepping for @cpu. 1055 */ 1056 void cpu_single_step(CPUState *cpu, int enabled); 1057 1058 /* Breakpoint/watchpoint flags */ 1059 #define BP_MEM_READ 0x01 1060 #define BP_MEM_WRITE 0x02 1061 #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE) 1062 #define BP_STOP_BEFORE_ACCESS 0x04 1063 /* 0x08 currently unused */ 1064 #define BP_GDB 0x10 1065 #define BP_CPU 0x20 1066 #define BP_ANY (BP_GDB | BP_CPU) 1067 #define BP_HIT_SHIFT 6 1068 #define BP_WATCHPOINT_HIT_READ (BP_MEM_READ << BP_HIT_SHIFT) 1069 #define BP_WATCHPOINT_HIT_WRITE (BP_MEM_WRITE << BP_HIT_SHIFT) 1070 #define BP_WATCHPOINT_HIT (BP_MEM_ACCESS << BP_HIT_SHIFT) 1071 1072 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags, 1073 CPUBreakpoint **breakpoint); 1074 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags); 1075 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint); 1076 void cpu_breakpoint_remove_all(CPUState *cpu, int mask); 1077 1078 /* Return true if PC matches an installed breakpoint. */ 1079 static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask) 1080 { 1081 CPUBreakpoint *bp; 1082 1083 if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) { 1084 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) { 1085 if (bp->pc == pc && (bp->flags & mask)) { 1086 return true; 1087 } 1088 } 1089 } 1090 return false; 1091 } 1092 1093 /** 1094 * cpu_get_address_space: 1095 * @cpu: CPU to get address space from 1096 * @asidx: index identifying which address space to get 1097 * 1098 * Return the requested address space of this CPU. @asidx 1099 * specifies which address space to read. 1100 */ 1101 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx); 1102 1103 G_NORETURN void cpu_abort(CPUState *cpu, const char *fmt, ...) 1104 G_GNUC_PRINTF(2, 3); 1105 1106 /* $(top_srcdir)/cpu.c */ 1107 void cpu_class_init_props(DeviceClass *dc); 1108 void cpu_exec_class_post_init(CPUClass *cc); 1109 void cpu_exec_initfn(CPUState *cpu); 1110 void cpu_vmstate_register(CPUState *cpu); 1111 void cpu_vmstate_unregister(CPUState *cpu); 1112 bool cpu_exec_realizefn(CPUState *cpu, Error **errp); 1113 void cpu_exec_unrealizefn(CPUState *cpu); 1114 void cpu_exec_reset_hold(CPUState *cpu); 1115 1116 const char *target_name(void); 1117 1118 #ifdef COMPILING_PER_TARGET 1119 1120 extern const VMStateDescription vmstate_cpu_common; 1121 1122 #define VMSTATE_CPU() { \ 1123 .name = "parent_obj", \ 1124 .size = sizeof(CPUState), \ 1125 .vmsd = &vmstate_cpu_common, \ 1126 .flags = VMS_STRUCT, \ 1127 .offset = 0, \ 1128 } 1129 1130 #endif /* COMPILING_PER_TARGET */ 1131 1132 #define UNASSIGNED_CPU_INDEX -1 1133 #define UNASSIGNED_CLUSTER_INDEX -1 1134 1135 #endif 1136