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