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