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