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