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/vaddr.h" 27 #include "exec/memattrs.h" 28 #include "exec/tlb-common.h" 29 #include "qapi/qapi-types-run-state.h" 30 #include "qemu/bitmap.h" 31 #include "qemu/rcu_queue.h" 32 #include "qemu/queue.h" 33 #include "qemu/thread.h" 34 #include "qom/object.h" 35 36 typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size, 37 void *opaque); 38 39 /** 40 * SECTION:cpu 41 * @section_id: QEMU-cpu 42 * @title: CPU Class 43 * @short_description: Base class for all CPUs 44 */ 45 46 #define TYPE_CPU "cpu" 47 48 /* Since this macro is used a lot in hot code paths and in conjunction with 49 * FooCPU *foo_env_get_cpu(), we deviate from usual QOM practice by using 50 * an unchecked cast. 51 */ 52 #define CPU(obj) ((CPUState *)(obj)) 53 54 /* 55 * The class checkers bring in CPU_GET_CLASS() which is potentially 56 * expensive given the eventual call to 57 * object_class_dynamic_cast_assert(). Because of this the CPUState 58 * has a cached value for the class in cs->cc which is set up in 59 * cpu_exec_realizefn() for use in hot code paths. 60 */ 61 typedef struct CPUClass CPUClass; 62 DECLARE_CLASS_CHECKERS(CPUClass, CPU, 63 TYPE_CPU) 64 65 /** 66 * OBJECT_DECLARE_CPU_TYPE: 67 * @CpuInstanceType: instance struct name 68 * @CpuClassType: class struct name 69 * @CPU_MODULE_OBJ_NAME: the CPU name in uppercase with underscore separators 70 * 71 * This macro is typically used in "cpu-qom.h" header file, and will: 72 * 73 * - create the typedefs for the CPU object and class structs 74 * - register the type for use with g_autoptr 75 * - provide three standard type cast functions 76 * 77 * The object struct and class struct need to be declared manually. 78 */ 79 #define OBJECT_DECLARE_CPU_TYPE(CpuInstanceType, CpuClassType, CPU_MODULE_OBJ_NAME) \ 80 typedef struct ArchCPU CpuInstanceType; \ 81 OBJECT_DECLARE_TYPE(ArchCPU, CpuClassType, CPU_MODULE_OBJ_NAME); 82 83 typedef enum MMUAccessType { 84 MMU_DATA_LOAD = 0, 85 MMU_DATA_STORE = 1, 86 MMU_INST_FETCH = 2 87 #define MMU_ACCESS_COUNT 3 88 } MMUAccessType; 89 90 typedef struct CPUWatchpoint CPUWatchpoint; 91 92 /* see accel-cpu.h */ 93 struct AccelCPUClass; 94 95 /* see sysemu-cpu-ops.h */ 96 struct SysemuCPUOps; 97 98 /** 99 * CPUClass: 100 * @class_by_name: Callback to map -cpu command line model name to an 101 * instantiatable CPU type. 102 * @parse_features: Callback to parse command line arguments. 103 * @reset_dump_flags: #CPUDumpFlags to use for reset logging. 104 * @has_work: Callback for checking if there is work to do. 105 * @mmu_index: Callback for choosing softmmu mmu index; 106 * may be used internally by memory_rw_debug without TCG. 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 * @gdb_write_register: Callback for letting GDB write a register. 125 * @gdb_adjust_breakpoint: Callback for adjusting the address of a 126 * breakpoint. Used by AVR to handle a gdb mis-feature with 127 * its Harvard architecture split code and data. 128 * @gdb_num_core_regs: Number of core registers accessible to GDB or 0 to infer 129 * from @gdb_core_xml_file. 130 * @gdb_core_xml_file: File name for core registers GDB XML description. 131 * @gdb_stop_before_watchpoint: Indicates whether GDB expects the CPU to stop 132 * before the insn which triggers a watchpoint rather than after it. 133 * @gdb_arch_name: Optional callback that returns the architecture name known 134 * to GDB. The caller must free the returned string with g_free. 135 * @disas_set_info: Setup architecture specific components of disassembly info 136 * @adjust_watchpoint_address: Perform a target-specific adjustment to an 137 * address before attempting to match it against watchpoints. 138 * @deprecation_note: If this CPUClass is deprecated, this field provides 139 * related information. 140 * 141 * Represents a CPU family or model. 142 */ 143 struct CPUClass { 144 /*< private >*/ 145 DeviceClass parent_class; 146 /*< public >*/ 147 148 ObjectClass *(*class_by_name)(const char *cpu_model); 149 void (*parse_features)(const char *typename, char *str, Error **errp); 150 151 bool (*has_work)(CPUState *cpu); 152 int (*mmu_index)(CPUState *cpu, bool ifetch); 153 int (*memory_rw_debug)(CPUState *cpu, vaddr addr, 154 uint8_t *buf, int 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 typedef 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 } CPUTLBEntryFull; 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 * Elements of CPUState most efficiently accessed from CPUArchState, 348 * via small negative offsets. 349 */ 350 typedef struct CPUNegativeOffsetState { 351 CPUTLB tlb; 352 IcountDecr icount_decr; 353 bool can_do_io; 354 } CPUNegativeOffsetState; 355 356 typedef struct CPUBreakpoint { 357 vaddr pc; 358 int flags; /* BP_* */ 359 QTAILQ_ENTRY(CPUBreakpoint) entry; 360 } CPUBreakpoint; 361 362 struct CPUWatchpoint { 363 vaddr vaddr; 364 vaddr len; 365 vaddr hitaddr; 366 MemTxAttrs hitattrs; 367 int flags; /* BP_* */ 368 QTAILQ_ENTRY(CPUWatchpoint) entry; 369 }; 370 371 struct KVMState; 372 struct kvm_run; 373 374 /* work queue */ 375 376 /* The union type allows passing of 64 bit target pointers on 32 bit 377 * hosts in a single parameter 378 */ 379 typedef union { 380 int host_int; 381 unsigned long host_ulong; 382 void *host_ptr; 383 vaddr target_ptr; 384 } run_on_cpu_data; 385 386 #define RUN_ON_CPU_HOST_PTR(p) ((run_on_cpu_data){.host_ptr = (p)}) 387 #define RUN_ON_CPU_HOST_INT(i) ((run_on_cpu_data){.host_int = (i)}) 388 #define RUN_ON_CPU_HOST_ULONG(ul) ((run_on_cpu_data){.host_ulong = (ul)}) 389 #define RUN_ON_CPU_TARGET_PTR(v) ((run_on_cpu_data){.target_ptr = (v)}) 390 #define RUN_ON_CPU_NULL RUN_ON_CPU_HOST_PTR(NULL) 391 392 typedef void (*run_on_cpu_func)(CPUState *cpu, run_on_cpu_data data); 393 394 struct qemu_work_item; 395 396 #define CPU_UNSET_NUMA_NODE_ID -1 397 398 /** 399 * CPUState: 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_cores: Number of cores within this CPU package. 410 * @nr_threads: Number of threads within this CPU core. 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 * @interrupt_request: Indicates a pending interrupt request. 416 * @halted: Nonzero if the CPU is in suspended state. 417 * @stop: Indicates a pending stop request. 418 * @stopped: Indicates the CPU has been artificially stopped. 419 * @unplug: Indicates a pending CPU unplug request. 420 * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU 421 * @singlestep_enabled: Flags for single-stepping. 422 * @icount_extra: Instructions until next timer event. 423 * @neg.can_do_io: True if memory-mapped IO is allowed. 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_mem_cbs: active plugin memory callbacks 440 * @plugin_state: per-CPU plugin state 441 * @ignore_memory_transaction_failures: Cached copy of the MachineState 442 * flag of the same name: allows the board to suppress calling of the 443 * CPU do_transaction_failed hook function. 444 * @kvm_dirty_gfns: Points to the KVM dirty ring for this CPU when KVM dirty 445 * ring is enabled. 446 * @kvm_fetch_index: Keeps the index that we last fetched from the per-vCPU 447 * dirty ring structure. 448 * 449 * State of one CPU core or thread. 450 * 451 * Align, in order to match possible alignment required by CPUArchState, 452 * and eliminate a hole between CPUState and CPUArchState within ArchCPU. 453 */ 454 struct CPUState { 455 /*< private >*/ 456 DeviceState parent_obj; 457 /* cache to avoid expensive CPU_GET_CLASS */ 458 CPUClass *cc; 459 /*< public >*/ 460 461 int nr_cores; 462 int nr_threads; 463 464 struct QemuThread *thread; 465 #ifdef _WIN32 466 QemuSemaphore sem; 467 #endif 468 int thread_id; 469 bool running, has_waiter; 470 struct QemuCond *halt_cond; 471 bool thread_kicked; 472 bool created; 473 bool stop; 474 bool stopped; 475 476 /* Should CPU start in powered-off state? */ 477 bool start_powered_off; 478 479 bool unplug; 480 bool crash_occurred; 481 bool exit_request; 482 int exclusive_context_count; 483 uint32_t cflags_next_tb; 484 /* updates protected by BQL */ 485 uint32_t interrupt_request; 486 int singlestep_enabled; 487 int64_t icount_budget; 488 int64_t icount_extra; 489 uint64_t random_seed; 490 sigjmp_buf jmp_env; 491 492 QemuMutex work_mutex; 493 QSIMPLEQ_HEAD(, qemu_work_item) work_list; 494 495 CPUAddressSpace *cpu_ases; 496 int num_ases; 497 AddressSpace *as; 498 MemoryRegion *memory; 499 500 CPUJumpCache *tb_jmp_cache; 501 502 GArray *gdb_regs; 503 int gdb_num_regs; 504 int gdb_num_g_regs; 505 QTAILQ_ENTRY(CPUState) node; 506 507 /* ice debug support */ 508 QTAILQ_HEAD(, CPUBreakpoint) breakpoints; 509 510 QTAILQ_HEAD(, CPUWatchpoint) watchpoints; 511 CPUWatchpoint *watchpoint_hit; 512 513 void *opaque; 514 515 /* In order to avoid passing too many arguments to the MMIO helpers, 516 * we store some rarely used information in the CPU context. 517 */ 518 uintptr_t mem_io_pc; 519 520 /* Only used in KVM */ 521 int kvm_fd; 522 struct KVMState *kvm_state; 523 struct kvm_run *kvm_run; 524 struct kvm_dirty_gfn *kvm_dirty_gfns; 525 uint32_t kvm_fetch_index; 526 uint64_t dirty_pages; 527 int kvm_vcpu_stats_fd; 528 529 /* Use by accel-block: CPU is executing an ioctl() */ 530 QemuLockCnt in_ioctl_lock; 531 532 #ifdef CONFIG_PLUGIN 533 /* 534 * The callback pointer stays in the main CPUState as it is 535 * accessed via TCG (see gen_empty_mem_helper). 536 */ 537 GArray *plugin_mem_cbs; 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 /* shared by kvm and hvf */ 550 bool vcpu_dirty; 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-all.h. */ 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 * qemu_tcg_mttcg_enabled: 601 * Check whether we are running MultiThread TCG or not. 602 * 603 * Returns: %true if we are in MTTCG mode %false otherwise. 604 */ 605 extern bool mttcg_enabled; 606 #define qemu_tcg_mttcg_enabled() (mttcg_enabled) 607 608 /** 609 * cpu_paging_enabled: 610 * @cpu: The CPU whose state is to be inspected. 611 * 612 * Returns: %true if paging is enabled, %false otherwise. 613 */ 614 bool cpu_paging_enabled(const CPUState *cpu); 615 616 /** 617 * cpu_get_memory_mapping: 618 * @cpu: The CPU whose memory mappings are to be obtained. 619 * @list: Where to write the memory mappings to. 620 * @errp: Pointer for reporting an #Error. 621 * 622 * Returns: %true on success, %false otherwise. 623 */ 624 bool cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list, 625 Error **errp); 626 627 #if !defined(CONFIG_USER_ONLY) 628 629 /** 630 * cpu_write_elf64_note: 631 * @f: pointer to a function that writes memory to a file 632 * @cpu: The CPU whose memory is to be dumped 633 * @cpuid: ID number of the CPU 634 * @opaque: pointer to the CPUState struct 635 */ 636 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu, 637 int cpuid, void *opaque); 638 639 /** 640 * cpu_write_elf64_qemunote: 641 * @f: pointer to a function that writes memory to a file 642 * @cpu: The CPU whose memory is to be dumped 643 * @cpuid: ID number of the CPU 644 * @opaque: pointer to the CPUState struct 645 */ 646 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu, 647 void *opaque); 648 649 /** 650 * cpu_write_elf32_note: 651 * @f: pointer to a function that writes memory to a file 652 * @cpu: The CPU whose memory is to be dumped 653 * @cpuid: ID number of the CPU 654 * @opaque: pointer to the CPUState struct 655 */ 656 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu, 657 int cpuid, void *opaque); 658 659 /** 660 * cpu_write_elf32_qemunote: 661 * @f: pointer to a function that writes memory to a file 662 * @cpu: The CPU whose memory is to be dumped 663 * @cpuid: ID number of the CPU 664 * @opaque: pointer to the CPUState struct 665 */ 666 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu, 667 void *opaque); 668 669 /** 670 * cpu_get_crash_info: 671 * @cpu: The CPU to get crash information for 672 * 673 * Gets the previously saved crash information. 674 * Caller is responsible for freeing the data. 675 */ 676 GuestPanicInformation *cpu_get_crash_info(CPUState *cpu); 677 678 #endif /* !CONFIG_USER_ONLY */ 679 680 /** 681 * CPUDumpFlags: 682 * @CPU_DUMP_CODE: 683 * @CPU_DUMP_FPU: dump FPU register state, not just integer 684 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state 685 * @CPU_DUMP_VPU: dump VPU registers 686 */ 687 enum CPUDumpFlags { 688 CPU_DUMP_CODE = 0x00010000, 689 CPU_DUMP_FPU = 0x00020000, 690 CPU_DUMP_CCOP = 0x00040000, 691 CPU_DUMP_VPU = 0x00080000, 692 }; 693 694 /** 695 * cpu_dump_state: 696 * @cpu: The CPU whose state is to be dumped. 697 * @f: If non-null, dump to this stream, else to current print sink. 698 * 699 * Dumps CPU state. 700 */ 701 void cpu_dump_state(CPUState *cpu, FILE *f, int flags); 702 703 #ifndef CONFIG_USER_ONLY 704 /** 705 * cpu_get_phys_page_attrs_debug: 706 * @cpu: The CPU to obtain the physical page address for. 707 * @addr: The virtual address. 708 * @attrs: Updated on return with the memory transaction attributes to use 709 * for this access. 710 * 711 * Obtains the physical page corresponding to a virtual one, together 712 * with the corresponding memory transaction attributes to use for the access. 713 * Use it only for debugging because no protection checks are done. 714 * 715 * Returns: Corresponding physical page address or -1 if no page found. 716 */ 717 hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr, 718 MemTxAttrs *attrs); 719 720 /** 721 * cpu_get_phys_page_debug: 722 * @cpu: The CPU to obtain the physical page address for. 723 * @addr: The virtual address. 724 * 725 * Obtains the physical page corresponding to a virtual one. 726 * Use it only for debugging because no protection checks are done. 727 * 728 * Returns: Corresponding physical page address or -1 if no page found. 729 */ 730 hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr); 731 732 /** cpu_asidx_from_attrs: 733 * @cpu: CPU 734 * @attrs: memory transaction attributes 735 * 736 * Returns the address space index specifying the CPU AddressSpace 737 * to use for a memory access with the given transaction attributes. 738 */ 739 int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs); 740 741 /** 742 * cpu_virtio_is_big_endian: 743 * @cpu: CPU 744 745 * Returns %true if a CPU which supports runtime configurable endianness 746 * is currently big-endian. 747 */ 748 bool cpu_virtio_is_big_endian(CPUState *cpu); 749 750 #endif /* CONFIG_USER_ONLY */ 751 752 /** 753 * cpu_list_add: 754 * @cpu: The CPU to be added to the list of CPUs. 755 */ 756 void cpu_list_add(CPUState *cpu); 757 758 /** 759 * cpu_list_remove: 760 * @cpu: The CPU to be removed from the list of CPUs. 761 */ 762 void cpu_list_remove(CPUState *cpu); 763 764 /** 765 * cpu_reset: 766 * @cpu: The CPU whose state is to be reset. 767 */ 768 void cpu_reset(CPUState *cpu); 769 770 /** 771 * cpu_class_by_name: 772 * @typename: The CPU base type. 773 * @cpu_model: The model string without any parameters. 774 * 775 * Looks up a concrete CPU #ObjectClass matching name @cpu_model. 776 * 777 * Returns: A concrete #CPUClass or %NULL if no matching class is found 778 * or if the matching class is abstract. 779 */ 780 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model); 781 782 /** 783 * cpu_model_from_type: 784 * @typename: The CPU type name 785 * 786 * Extract the CPU model name from the CPU type name. The 787 * CPU type name is either the combination of the CPU model 788 * name and suffix, or same to the CPU model name. 789 * 790 * Returns: CPU model name or NULL if the CPU class doesn't exist 791 * The user should g_free() the string once no longer needed. 792 */ 793 char *cpu_model_from_type(const char *typename); 794 795 /** 796 * cpu_create: 797 * @typename: The CPU type. 798 * 799 * Instantiates a CPU and realizes the CPU. 800 * 801 * Returns: A #CPUState or %NULL if an error occurred. 802 */ 803 CPUState *cpu_create(const char *typename); 804 805 /** 806 * parse_cpu_option: 807 * @cpu_option: The -cpu option including optional parameters. 808 * 809 * processes optional parameters and registers them as global properties 810 * 811 * Returns: type of CPU to create or prints error and terminates process 812 * if an error occurred. 813 */ 814 const char *parse_cpu_option(const char *cpu_option); 815 816 /** 817 * cpu_has_work: 818 * @cpu: The vCPU to check. 819 * 820 * Checks whether the CPU has work to do. 821 * 822 * Returns: %true if the CPU has work, %false otherwise. 823 */ 824 static inline bool cpu_has_work(CPUState *cpu) 825 { 826 CPUClass *cc = CPU_GET_CLASS(cpu); 827 828 g_assert(cc->has_work); 829 return cc->has_work(cpu); 830 } 831 832 /** 833 * qemu_cpu_is_self: 834 * @cpu: The vCPU to check against. 835 * 836 * Checks whether the caller is executing on the vCPU thread. 837 * 838 * Returns: %true if called from @cpu's thread, %false otherwise. 839 */ 840 bool qemu_cpu_is_self(CPUState *cpu); 841 842 /** 843 * qemu_cpu_kick: 844 * @cpu: The vCPU to kick. 845 * 846 * Kicks @cpu's thread. 847 */ 848 void qemu_cpu_kick(CPUState *cpu); 849 850 /** 851 * cpu_is_stopped: 852 * @cpu: The CPU to check. 853 * 854 * Checks whether the CPU is stopped. 855 * 856 * Returns: %true if run state is not running or if artificially stopped; 857 * %false otherwise. 858 */ 859 bool cpu_is_stopped(CPUState *cpu); 860 861 /** 862 * do_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 * @mutex: Mutex to release while waiting for @func to run. 867 * 868 * Used internally in the implementation of run_on_cpu. 869 */ 870 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data, 871 QemuMutex *mutex); 872 873 /** 874 * run_on_cpu: 875 * @cpu: The vCPU to run on. 876 * @func: The function to be executed. 877 * @data: Data to pass to the function. 878 * 879 * Schedules the function @func for execution on the vCPU @cpu. 880 */ 881 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data); 882 883 /** 884 * async_run_on_cpu: 885 * @cpu: The vCPU to run on. 886 * @func: The function to be executed. 887 * @data: Data to pass to the function. 888 * 889 * Schedules the function @func for execution on the vCPU @cpu asynchronously. 890 */ 891 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data); 892 893 /** 894 * async_safe_run_on_cpu: 895 * @cpu: The vCPU to run on. 896 * @func: The function to be executed. 897 * @data: Data to pass to the function. 898 * 899 * Schedules the function @func for execution on the vCPU @cpu asynchronously, 900 * while all other vCPUs are sleeping. 901 * 902 * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the 903 * BQL. 904 */ 905 void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data); 906 907 /** 908 * cpu_in_exclusive_context() 909 * @cpu: The vCPU to check 910 * 911 * Returns true if @cpu is an exclusive context, for example running 912 * something which has previously been queued via async_safe_run_on_cpu(). 913 */ 914 static inline bool cpu_in_exclusive_context(const CPUState *cpu) 915 { 916 return cpu->exclusive_context_count; 917 } 918 919 /** 920 * qemu_get_cpu: 921 * @index: The CPUState@cpu_index value of the CPU to obtain. 922 * 923 * Gets a CPU matching @index. 924 * 925 * Returns: The CPU or %NULL if there is no matching CPU. 926 */ 927 CPUState *qemu_get_cpu(int index); 928 929 /** 930 * cpu_exists: 931 * @id: Guest-exposed CPU ID to lookup. 932 * 933 * Search for CPU with specified ID. 934 * 935 * Returns: %true - CPU is found, %false - CPU isn't found. 936 */ 937 bool cpu_exists(int64_t id); 938 939 /** 940 * cpu_by_arch_id: 941 * @id: Guest-exposed CPU ID of the CPU to obtain. 942 * 943 * Get a CPU with matching @id. 944 * 945 * Returns: The CPU or %NULL if there is no matching CPU. 946 */ 947 CPUState *cpu_by_arch_id(int64_t id); 948 949 /** 950 * cpu_interrupt: 951 * @cpu: The CPU to set an interrupt on. 952 * @mask: The interrupts to set. 953 * 954 * Invokes the interrupt handler. 955 */ 956 957 void cpu_interrupt(CPUState *cpu, int mask); 958 959 /** 960 * cpu_set_pc: 961 * @cpu: The CPU to set the program counter for. 962 * @addr: Program counter value. 963 * 964 * Sets the program counter for a CPU. 965 */ 966 static inline void cpu_set_pc(CPUState *cpu, vaddr addr) 967 { 968 CPUClass *cc = CPU_GET_CLASS(cpu); 969 970 cc->set_pc(cpu, addr); 971 } 972 973 /** 974 * cpu_reset_interrupt: 975 * @cpu: The CPU to clear the interrupt on. 976 * @mask: The interrupt mask to clear. 977 * 978 * Resets interrupts on the vCPU @cpu. 979 */ 980 void cpu_reset_interrupt(CPUState *cpu, int mask); 981 982 /** 983 * cpu_exit: 984 * @cpu: The CPU to exit. 985 * 986 * Requests the CPU @cpu to exit execution. 987 */ 988 void cpu_exit(CPUState *cpu); 989 990 /** 991 * cpu_resume: 992 * @cpu: The CPU to resume. 993 * 994 * Resumes CPU, i.e. puts CPU into runnable state. 995 */ 996 void cpu_resume(CPUState *cpu); 997 998 /** 999 * cpu_remove_sync: 1000 * @cpu: The CPU to remove. 1001 * 1002 * Requests the CPU to be removed and waits till it is removed. 1003 */ 1004 void cpu_remove_sync(CPUState *cpu); 1005 1006 /** 1007 * process_queued_cpu_work() - process all items on CPU work queue 1008 * @cpu: The CPU which work queue to process. 1009 */ 1010 void process_queued_cpu_work(CPUState *cpu); 1011 1012 /** 1013 * cpu_exec_start: 1014 * @cpu: The CPU for the current thread. 1015 * 1016 * Record that a CPU has started execution and can be interrupted with 1017 * cpu_exit. 1018 */ 1019 void cpu_exec_start(CPUState *cpu); 1020 1021 /** 1022 * cpu_exec_end: 1023 * @cpu: The CPU for the current thread. 1024 * 1025 * Record that a CPU has stopped execution and exclusive sections 1026 * can be executed without interrupting it. 1027 */ 1028 void cpu_exec_end(CPUState *cpu); 1029 1030 /** 1031 * start_exclusive: 1032 * 1033 * Wait for a concurrent exclusive section to end, and then start 1034 * a section of work that is run while other CPUs are not running 1035 * between cpu_exec_start and cpu_exec_end. CPUs that are running 1036 * cpu_exec are exited immediately. CPUs that call cpu_exec_start 1037 * during the exclusive section go to sleep until this CPU calls 1038 * end_exclusive. 1039 */ 1040 void start_exclusive(void); 1041 1042 /** 1043 * end_exclusive: 1044 * 1045 * Concludes an exclusive execution section started by start_exclusive. 1046 */ 1047 void end_exclusive(void); 1048 1049 /** 1050 * qemu_init_vcpu: 1051 * @cpu: The vCPU to initialize. 1052 * 1053 * Initializes a vCPU. 1054 */ 1055 void qemu_init_vcpu(CPUState *cpu); 1056 1057 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */ 1058 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */ 1059 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */ 1060 1061 /** 1062 * cpu_single_step: 1063 * @cpu: CPU to the flags for. 1064 * @enabled: Flags to enable. 1065 * 1066 * Enables or disables single-stepping for @cpu. 1067 */ 1068 void cpu_single_step(CPUState *cpu, int enabled); 1069 1070 /* Breakpoint/watchpoint flags */ 1071 #define BP_MEM_READ 0x01 1072 #define BP_MEM_WRITE 0x02 1073 #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE) 1074 #define BP_STOP_BEFORE_ACCESS 0x04 1075 /* 0x08 currently unused */ 1076 #define BP_GDB 0x10 1077 #define BP_CPU 0x20 1078 #define BP_ANY (BP_GDB | BP_CPU) 1079 #define BP_HIT_SHIFT 6 1080 #define BP_WATCHPOINT_HIT_READ (BP_MEM_READ << BP_HIT_SHIFT) 1081 #define BP_WATCHPOINT_HIT_WRITE (BP_MEM_WRITE << BP_HIT_SHIFT) 1082 #define BP_WATCHPOINT_HIT (BP_MEM_ACCESS << BP_HIT_SHIFT) 1083 1084 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags, 1085 CPUBreakpoint **breakpoint); 1086 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags); 1087 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint); 1088 void cpu_breakpoint_remove_all(CPUState *cpu, int mask); 1089 1090 /* Return true if PC matches an installed breakpoint. */ 1091 static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask) 1092 { 1093 CPUBreakpoint *bp; 1094 1095 if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) { 1096 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) { 1097 if (bp->pc == pc && (bp->flags & mask)) { 1098 return true; 1099 } 1100 } 1101 } 1102 return false; 1103 } 1104 1105 #if defined(CONFIG_USER_ONLY) 1106 static inline int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len, 1107 int flags, CPUWatchpoint **watchpoint) 1108 { 1109 return -ENOSYS; 1110 } 1111 1112 static inline int cpu_watchpoint_remove(CPUState *cpu, vaddr addr, 1113 vaddr len, int flags) 1114 { 1115 return -ENOSYS; 1116 } 1117 1118 static inline void cpu_watchpoint_remove_by_ref(CPUState *cpu, 1119 CPUWatchpoint *wp) 1120 { 1121 } 1122 1123 static inline void cpu_watchpoint_remove_all(CPUState *cpu, int mask) 1124 { 1125 } 1126 #else 1127 int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len, 1128 int flags, CPUWatchpoint **watchpoint); 1129 int cpu_watchpoint_remove(CPUState *cpu, vaddr addr, 1130 vaddr len, int flags); 1131 void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint); 1132 void cpu_watchpoint_remove_all(CPUState *cpu, int mask); 1133 #endif 1134 1135 /** 1136 * cpu_plugin_mem_cbs_enabled() - are plugin memory callbacks enabled? 1137 * @cs: CPUState pointer 1138 * 1139 * The memory callbacks are installed if a plugin has instrumented an 1140 * instruction for memory. This can be useful to know if you want to 1141 * force a slow path for a series of memory accesses. 1142 */ 1143 static inline bool cpu_plugin_mem_cbs_enabled(const CPUState *cpu) 1144 { 1145 #ifdef CONFIG_PLUGIN 1146 return !!cpu->plugin_mem_cbs; 1147 #else 1148 return false; 1149 #endif 1150 } 1151 1152 /** 1153 * cpu_get_address_space: 1154 * @cpu: CPU to get address space from 1155 * @asidx: index identifying which address space to get 1156 * 1157 * Return the requested address space of this CPU. @asidx 1158 * specifies which address space to read. 1159 */ 1160 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx); 1161 1162 G_NORETURN void cpu_abort(CPUState *cpu, const char *fmt, ...) 1163 G_GNUC_PRINTF(2, 3); 1164 1165 /* $(top_srcdir)/cpu.c */ 1166 void cpu_class_init_props(DeviceClass *dc); 1167 void cpu_exec_initfn(CPUState *cpu); 1168 bool cpu_exec_realizefn(CPUState *cpu, Error **errp); 1169 void cpu_exec_unrealizefn(CPUState *cpu); 1170 void cpu_exec_reset_hold(CPUState *cpu); 1171 1172 /** 1173 * target_words_bigendian: 1174 * Returns true if the (default) endianness of the target is big endian, 1175 * false otherwise. Note that in target-specific code, you can use 1176 * TARGET_BIG_ENDIAN directly instead. On the other hand, common 1177 * code should normally never need to know about the endianness of the 1178 * target, so please do *not* use this function unless you know very well 1179 * what you are doing! 1180 */ 1181 bool target_words_bigendian(void); 1182 1183 const char *target_name(void); 1184 1185 #ifdef COMPILING_PER_TARGET 1186 1187 #ifndef CONFIG_USER_ONLY 1188 1189 extern const VMStateDescription vmstate_cpu_common; 1190 1191 #define VMSTATE_CPU() { \ 1192 .name = "parent_obj", \ 1193 .size = sizeof(CPUState), \ 1194 .vmsd = &vmstate_cpu_common, \ 1195 .flags = VMS_STRUCT, \ 1196 .offset = 0, \ 1197 } 1198 #endif /* !CONFIG_USER_ONLY */ 1199 1200 #endif /* COMPILING_PER_TARGET */ 1201 1202 #define UNASSIGNED_CPU_INDEX -1 1203 #define UNASSIGNED_CLUSTER_INDEX -1 1204 1205 #endif 1206