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