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