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