cpu.h 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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. #include "hw/qdev-core.h"
  23. #include "disas/dis-asm.h"
  24. #include "exec/cpu-common.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. typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size,
  35. void *opaque);
  36. /**
  37. * SECTION:cpu
  38. * @section_id: QEMU-cpu
  39. * @title: CPU Class
  40. * @short_description: Base class for all CPUs
  41. */
  42. #define TYPE_CPU "cpu"
  43. /* Since this macro is used a lot in hot code paths and in conjunction with
  44. * FooCPU *foo_env_get_cpu(), we deviate from usual QOM practice by using
  45. * an unchecked cast.
  46. */
  47. #define CPU(obj) ((CPUState *)(obj))
  48. typedef struct CPUClass CPUClass;
  49. DECLARE_CLASS_CHECKERS(CPUClass, CPU,
  50. TYPE_CPU)
  51. /**
  52. * OBJECT_DECLARE_CPU_TYPE:
  53. * @CpuInstanceType: instance struct name
  54. * @CpuClassType: class struct name
  55. * @CPU_MODULE_OBJ_NAME: the CPU name in uppercase with underscore separators
  56. *
  57. * This macro is typically used in "cpu-qom.h" header file, and will:
  58. *
  59. * - create the typedefs for the CPU object and class structs
  60. * - register the type for use with g_autoptr
  61. * - provide three standard type cast functions
  62. *
  63. * The object struct and class struct need to be declared manually.
  64. */
  65. #define OBJECT_DECLARE_CPU_TYPE(CpuInstanceType, CpuClassType, CPU_MODULE_OBJ_NAME) \
  66. typedef struct ArchCPU CpuInstanceType; \
  67. OBJECT_DECLARE_TYPE(ArchCPU, CpuClassType, CPU_MODULE_OBJ_NAME);
  68. typedef enum MMUAccessType {
  69. MMU_DATA_LOAD = 0,
  70. MMU_DATA_STORE = 1,
  71. MMU_INST_FETCH = 2
  72. } MMUAccessType;
  73. typedef struct CPUWatchpoint CPUWatchpoint;
  74. /* see tcg-cpu-ops.h */
  75. struct TCGCPUOps;
  76. /* see accel-cpu.h */
  77. struct AccelCPUClass;
  78. /* see sysemu-cpu-ops.h */
  79. struct SysemuCPUOps;
  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_dump_flags: #CPUDumpFlags to use for reset logging.
  86. * @has_work: Callback for checking if there is work to do.
  87. * @memory_rw_debug: Callback for GDB memory access.
  88. * @dump_state: Callback for dumping state.
  89. * @get_arch_id: Callback for getting architecture-dependent CPU ID.
  90. * @set_pc: Callback for setting the Program Counter register. This
  91. * should have the semantics used by the target architecture when
  92. * setting the PC from a source such as an ELF file entry point;
  93. * for example on Arm it will also set the Thumb mode bit based
  94. * on the least significant bit of the new PC value.
  95. * If the target behaviour here is anything other than "set
  96. * the PC register to the value passed in" then the target must
  97. * also implement the synchronize_from_tb hook.
  98. * @gdb_read_register: Callback for letting GDB read a register.
  99. * @gdb_write_register: Callback for letting GDB write a register.
  100. * @gdb_adjust_breakpoint: Callback for adjusting the address of a
  101. * breakpoint. Used by AVR to handle a gdb mis-feature with
  102. * its Harvard architecture split code and data.
  103. * @gdb_num_core_regs: Number of core registers accessible to GDB.
  104. * @gdb_core_xml_file: File name for core registers GDB XML description.
  105. * @gdb_stop_before_watchpoint: Indicates whether GDB expects the CPU to stop
  106. * before the insn which triggers a watchpoint rather than after it.
  107. * @gdb_arch_name: Optional callback that returns the architecture name known
  108. * to GDB. The caller must free the returned string with g_free.
  109. * @gdb_get_dynamic_xml: Callback to return dynamically generated XML for the
  110. * gdb stub. Returns a pointer to the XML contents for the specified XML file
  111. * or NULL if the CPU doesn't have a dynamically generated content for it.
  112. * @disas_set_info: Setup architecture specific components of disassembly info
  113. * @adjust_watchpoint_address: Perform a target-specific adjustment to an
  114. * address before attempting to match it against watchpoints.
  115. * @deprecation_note: If this CPUClass is deprecated, this field provides
  116. * related information.
  117. *
  118. * Represents a CPU family or model.
  119. */
  120. struct CPUClass {
  121. /*< private >*/
  122. DeviceClass parent_class;
  123. /*< public >*/
  124. ObjectClass *(*class_by_name)(const char *cpu_model);
  125. void (*parse_features)(const char *typename, char *str, Error **errp);
  126. bool (*has_work)(CPUState *cpu);
  127. int (*memory_rw_debug)(CPUState *cpu, vaddr addr,
  128. uint8_t *buf, int len, bool is_write);
  129. void (*dump_state)(CPUState *cpu, FILE *, int flags);
  130. int64_t (*get_arch_id)(CPUState *cpu);
  131. void (*set_pc)(CPUState *cpu, vaddr value);
  132. int (*gdb_read_register)(CPUState *cpu, GByteArray *buf, int reg);
  133. int (*gdb_write_register)(CPUState *cpu, uint8_t *buf, int reg);
  134. vaddr (*gdb_adjust_breakpoint)(CPUState *cpu, vaddr addr);
  135. const char *gdb_core_xml_file;
  136. gchar * (*gdb_arch_name)(CPUState *cpu);
  137. const char * (*gdb_get_dynamic_xml)(CPUState *cpu, const char *xmlname);
  138. void (*disas_set_info)(CPUState *cpu, disassemble_info *info);
  139. const char *deprecation_note;
  140. struct AccelCPUClass *accel_cpu;
  141. /* when system emulation is not available, this pointer is NULL */
  142. const struct SysemuCPUOps *sysemu_ops;
  143. /* when TCG is not available, this pointer is NULL */
  144. const struct TCGCPUOps *tcg_ops;
  145. /*
  146. * if not NULL, this is called in order for the CPUClass to initialize
  147. * class data that depends on the accelerator, see accel/accel-common.c.
  148. */
  149. void (*init_accel_cpu)(struct AccelCPUClass *accel_cpu, CPUClass *cc);
  150. /*
  151. * Keep non-pointer data at the end to minimize holes.
  152. */
  153. int reset_dump_flags;
  154. int gdb_num_core_regs;
  155. bool gdb_stop_before_watchpoint;
  156. };
  157. /*
  158. * Low 16 bits: number of cycles left, used only in icount mode.
  159. * High 16 bits: Set to -1 to force TCG to stop executing linked TBs
  160. * for this CPU and return to its top level loop (even in non-icount mode).
  161. * This allows a single read-compare-cbranch-write sequence to test
  162. * for both decrementer underflow and exceptions.
  163. */
  164. typedef union IcountDecr {
  165. uint32_t u32;
  166. struct {
  167. #if HOST_BIG_ENDIAN
  168. uint16_t high;
  169. uint16_t low;
  170. #else
  171. uint16_t low;
  172. uint16_t high;
  173. #endif
  174. } u16;
  175. } IcountDecr;
  176. typedef struct CPUBreakpoint {
  177. vaddr pc;
  178. int flags; /* BP_* */
  179. QTAILQ_ENTRY(CPUBreakpoint) entry;
  180. } CPUBreakpoint;
  181. struct CPUWatchpoint {
  182. vaddr vaddr;
  183. vaddr len;
  184. vaddr hitaddr;
  185. MemTxAttrs hitattrs;
  186. int flags; /* BP_* */
  187. QTAILQ_ENTRY(CPUWatchpoint) entry;
  188. };
  189. #ifdef CONFIG_PLUGIN
  190. /*
  191. * For plugins we sometime need to save the resolved iotlb data before
  192. * the memory regions get moved around by io_writex.
  193. */
  194. typedef struct SavedIOTLB {
  195. hwaddr addr;
  196. MemoryRegionSection *section;
  197. hwaddr mr_offset;
  198. } SavedIOTLB;
  199. #endif
  200. struct KVMState;
  201. struct kvm_run;
  202. struct hax_vcpu_state;
  203. struct hvf_vcpu_state;
  204. #define TB_JMP_CACHE_BITS 12
  205. #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)
  206. /* work queue */
  207. /* The union type allows passing of 64 bit target pointers on 32 bit
  208. * hosts in a single parameter
  209. */
  210. typedef union {
  211. int host_int;
  212. unsigned long host_ulong;
  213. void *host_ptr;
  214. vaddr target_ptr;
  215. } run_on_cpu_data;
  216. #define RUN_ON_CPU_HOST_PTR(p) ((run_on_cpu_data){.host_ptr = (p)})
  217. #define RUN_ON_CPU_HOST_INT(i) ((run_on_cpu_data){.host_int = (i)})
  218. #define RUN_ON_CPU_HOST_ULONG(ul) ((run_on_cpu_data){.host_ulong = (ul)})
  219. #define RUN_ON_CPU_TARGET_PTR(v) ((run_on_cpu_data){.target_ptr = (v)})
  220. #define RUN_ON_CPU_NULL RUN_ON_CPU_HOST_PTR(NULL)
  221. typedef void (*run_on_cpu_func)(CPUState *cpu, run_on_cpu_data data);
  222. struct qemu_work_item;
  223. #define CPU_UNSET_NUMA_NODE_ID -1
  224. #define CPU_TRACE_DSTATE_MAX_EVENTS 32
  225. /**
  226. * CPUState:
  227. * @cpu_index: CPU index (informative).
  228. * @cluster_index: Identifies which cluster this CPU is in.
  229. * For boards which don't define clusters or for "loose" CPUs not assigned
  230. * to a cluster this will be UNASSIGNED_CLUSTER_INDEX; otherwise it will
  231. * be the same as the cluster-id property of the CPU object's TYPE_CPU_CLUSTER
  232. * QOM parent.
  233. * @tcg_cflags: Pre-computed cflags for this cpu.
  234. * @nr_cores: Number of cores within this CPU package.
  235. * @nr_threads: Number of threads within this CPU.
  236. * @running: #true if CPU is currently running (lockless).
  237. * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end;
  238. * valid under cpu_list_lock.
  239. * @created: Indicates whether the CPU thread has been successfully created.
  240. * @interrupt_request: Indicates a pending interrupt request.
  241. * @halted: Nonzero if the CPU is in suspended state.
  242. * @stop: Indicates a pending stop request.
  243. * @stopped: Indicates the CPU has been artificially stopped.
  244. * @unplug: Indicates a pending CPU unplug request.
  245. * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
  246. * @singlestep_enabled: Flags for single-stepping.
  247. * @icount_extra: Instructions until next timer event.
  248. * @can_do_io: Nonzero if memory-mapped IO is safe. Deterministic execution
  249. * requires that IO only be performed on the last instruction of a TB
  250. * so that interrupts take effect immediately.
  251. * @cpu_ases: Pointer to array of CPUAddressSpaces (which define the
  252. * AddressSpaces this CPU has)
  253. * @num_ases: number of CPUAddressSpaces in @cpu_ases
  254. * @as: Pointer to the first AddressSpace, for the convenience of targets which
  255. * only have a single AddressSpace
  256. * @env_ptr: Pointer to subclass-specific CPUArchState field.
  257. * @icount_decr_ptr: Pointer to IcountDecr field within subclass.
  258. * @gdb_regs: Additional GDB registers.
  259. * @gdb_num_regs: Number of total registers accessible to GDB.
  260. * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
  261. * @next_cpu: Next CPU sharing TB cache.
  262. * @opaque: User data.
  263. * @mem_io_pc: Host Program Counter at which the memory was accessed.
  264. * @kvm_fd: vCPU file descriptor for KVM.
  265. * @work_mutex: Lock to prevent multiple access to @work_list.
  266. * @work_list: List of pending asynchronous work.
  267. * @trace_dstate_delayed: Delayed changes to trace_dstate (includes all changes
  268. * to @trace_dstate).
  269. * @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask).
  270. * @plugin_mask: Plugin event bitmap. Modified only via async work.
  271. * @ignore_memory_transaction_failures: Cached copy of the MachineState
  272. * flag of the same name: allows the board to suppress calling of the
  273. * CPU do_transaction_failed hook function.
  274. * @kvm_dirty_gfns: Points to the KVM dirty ring for this CPU when KVM dirty
  275. * ring is enabled.
  276. * @kvm_fetch_index: Keeps the index that we last fetched from the per-vCPU
  277. * dirty ring structure.
  278. *
  279. * State of one CPU core or thread.
  280. */
  281. struct CPUState {
  282. /*< private >*/
  283. DeviceState parent_obj;
  284. /*< public >*/
  285. int nr_cores;
  286. int nr_threads;
  287. struct QemuThread *thread;
  288. #ifdef _WIN32
  289. HANDLE hThread;
  290. #endif
  291. int thread_id;
  292. bool running, has_waiter;
  293. struct QemuCond *halt_cond;
  294. bool thread_kicked;
  295. bool created;
  296. bool stop;
  297. bool stopped;
  298. /* Should CPU start in powered-off state? */
  299. bool start_powered_off;
  300. bool unplug;
  301. bool crash_occurred;
  302. bool exit_request;
  303. bool in_exclusive_context;
  304. uint32_t cflags_next_tb;
  305. /* updates protected by BQL */
  306. uint32_t interrupt_request;
  307. int singlestep_enabled;
  308. int64_t icount_budget;
  309. int64_t icount_extra;
  310. uint64_t random_seed;
  311. sigjmp_buf jmp_env;
  312. QemuMutex work_mutex;
  313. QSIMPLEQ_HEAD(, qemu_work_item) work_list;
  314. CPUAddressSpace *cpu_ases;
  315. int num_ases;
  316. AddressSpace *as;
  317. MemoryRegion *memory;
  318. CPUArchState *env_ptr;
  319. IcountDecr *icount_decr_ptr;
  320. /* Accessed in parallel; all accesses must be atomic */
  321. TranslationBlock *tb_jmp_cache[TB_JMP_CACHE_SIZE];
  322. struct GDBRegisterState *gdb_regs;
  323. int gdb_num_regs;
  324. int gdb_num_g_regs;
  325. QTAILQ_ENTRY(CPUState) node;
  326. /* ice debug support */
  327. QTAILQ_HEAD(, CPUBreakpoint) breakpoints;
  328. QTAILQ_HEAD(, CPUWatchpoint) watchpoints;
  329. CPUWatchpoint *watchpoint_hit;
  330. void *opaque;
  331. /* In order to avoid passing too many arguments to the MMIO helpers,
  332. * we store some rarely used information in the CPU context.
  333. */
  334. uintptr_t mem_io_pc;
  335. /* Only used in KVM */
  336. int kvm_fd;
  337. struct KVMState *kvm_state;
  338. struct kvm_run *kvm_run;
  339. struct kvm_dirty_gfn *kvm_dirty_gfns;
  340. uint32_t kvm_fetch_index;
  341. uint64_t dirty_pages;
  342. /* Used for events with 'vcpu' and *without* the 'disabled' properties */
  343. DECLARE_BITMAP(trace_dstate_delayed, CPU_TRACE_DSTATE_MAX_EVENTS);
  344. DECLARE_BITMAP(trace_dstate, CPU_TRACE_DSTATE_MAX_EVENTS);
  345. DECLARE_BITMAP(plugin_mask, QEMU_PLUGIN_EV_MAX);
  346. #ifdef CONFIG_PLUGIN
  347. GArray *plugin_mem_cbs;
  348. /* saved iotlb data from io_writex */
  349. SavedIOTLB saved_iotlb;
  350. #endif
  351. /* TODO Move common fields from CPUArchState here. */
  352. int cpu_index;
  353. int cluster_index;
  354. uint32_t tcg_cflags;
  355. uint32_t halted;
  356. uint32_t can_do_io;
  357. int32_t exception_index;
  358. /* shared by kvm, hax and hvf */
  359. bool vcpu_dirty;
  360. /* Used to keep track of an outstanding cpu throttle thread for migration
  361. * autoconverge
  362. */
  363. bool throttle_thread_scheduled;
  364. bool ignore_memory_transaction_failures;
  365. /* Used for user-only emulation of prctl(PR_SET_UNALIGN). */
  366. bool prctl_unalign_sigbus;
  367. struct hax_vcpu_state *hax_vcpu;
  368. struct hvf_vcpu_state *hvf;
  369. /* track IOMMUs whose translations we've cached in the TCG TLB */
  370. GArray *iommu_notifiers;
  371. };
  372. typedef QTAILQ_HEAD(CPUTailQ, CPUState) CPUTailQ;
  373. extern CPUTailQ cpus;
  374. #define first_cpu QTAILQ_FIRST_RCU(&cpus)
  375. #define CPU_NEXT(cpu) QTAILQ_NEXT_RCU(cpu, node)
  376. #define CPU_FOREACH(cpu) QTAILQ_FOREACH_RCU(cpu, &cpus, node)
  377. #define CPU_FOREACH_SAFE(cpu, next_cpu) \
  378. QTAILQ_FOREACH_SAFE_RCU(cpu, &cpus, node, next_cpu)
  379. extern __thread CPUState *current_cpu;
  380. static inline void cpu_tb_jmp_cache_clear(CPUState *cpu)
  381. {
  382. unsigned int i;
  383. for (i = 0; i < TB_JMP_CACHE_SIZE; i++) {
  384. qatomic_set(&cpu->tb_jmp_cache[i], NULL);
  385. }
  386. }
  387. /**
  388. * qemu_tcg_mttcg_enabled:
  389. * Check whether we are running MultiThread TCG or not.
  390. *
  391. * Returns: %true if we are in MTTCG mode %false otherwise.
  392. */
  393. extern bool mttcg_enabled;
  394. #define qemu_tcg_mttcg_enabled() (mttcg_enabled)
  395. /**
  396. * cpu_paging_enabled:
  397. * @cpu: The CPU whose state is to be inspected.
  398. *
  399. * Returns: %true if paging is enabled, %false otherwise.
  400. */
  401. bool cpu_paging_enabled(const CPUState *cpu);
  402. /**
  403. * cpu_get_memory_mapping:
  404. * @cpu: The CPU whose memory mappings are to be obtained.
  405. * @list: Where to write the memory mappings to.
  406. * @errp: Pointer for reporting an #Error.
  407. */
  408. void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
  409. Error **errp);
  410. #if !defined(CONFIG_USER_ONLY)
  411. /**
  412. * cpu_write_elf64_note:
  413. * @f: pointer to a function that writes memory to a file
  414. * @cpu: The CPU whose memory is to be dumped
  415. * @cpuid: ID number of the CPU
  416. * @opaque: pointer to the CPUState struct
  417. */
  418. int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
  419. int cpuid, void *opaque);
  420. /**
  421. * cpu_write_elf64_qemunote:
  422. * @f: pointer to a function that writes memory to a file
  423. * @cpu: The CPU whose memory is to be dumped
  424. * @cpuid: ID number of the CPU
  425. * @opaque: pointer to the CPUState struct
  426. */
  427. int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
  428. void *opaque);
  429. /**
  430. * cpu_write_elf32_note:
  431. * @f: pointer to a function that writes memory to a file
  432. * @cpu: The CPU whose memory is to be dumped
  433. * @cpuid: ID number of the CPU
  434. * @opaque: pointer to the CPUState struct
  435. */
  436. int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
  437. int cpuid, void *opaque);
  438. /**
  439. * cpu_write_elf32_qemunote:
  440. * @f: pointer to a function that writes memory to a file
  441. * @cpu: The CPU whose memory is to be dumped
  442. * @cpuid: ID number of the CPU
  443. * @opaque: pointer to the CPUState struct
  444. */
  445. int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
  446. void *opaque);
  447. /**
  448. * cpu_get_crash_info:
  449. * @cpu: The CPU to get crash information for
  450. *
  451. * Gets the previously saved crash information.
  452. * Caller is responsible for freeing the data.
  453. */
  454. GuestPanicInformation *cpu_get_crash_info(CPUState *cpu);
  455. #endif /* !CONFIG_USER_ONLY */
  456. /**
  457. * CPUDumpFlags:
  458. * @CPU_DUMP_CODE:
  459. * @CPU_DUMP_FPU: dump FPU register state, not just integer
  460. * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
  461. */
  462. enum CPUDumpFlags {
  463. CPU_DUMP_CODE = 0x00010000,
  464. CPU_DUMP_FPU = 0x00020000,
  465. CPU_DUMP_CCOP = 0x00040000,
  466. };
  467. /**
  468. * cpu_dump_state:
  469. * @cpu: The CPU whose state is to be dumped.
  470. * @f: If non-null, dump to this stream, else to current print sink.
  471. *
  472. * Dumps CPU state.
  473. */
  474. void cpu_dump_state(CPUState *cpu, FILE *f, int flags);
  475. #ifndef CONFIG_USER_ONLY
  476. /**
  477. * cpu_get_phys_page_attrs_debug:
  478. * @cpu: The CPU to obtain the physical page address for.
  479. * @addr: The virtual address.
  480. * @attrs: Updated on return with the memory transaction attributes to use
  481. * for this access.
  482. *
  483. * Obtains the physical page corresponding to a virtual one, together
  484. * with the corresponding memory transaction attributes to use for the access.
  485. * Use it only for debugging because no protection checks are done.
  486. *
  487. * Returns: Corresponding physical page address or -1 if no page found.
  488. */
  489. hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
  490. MemTxAttrs *attrs);
  491. /**
  492. * cpu_get_phys_page_debug:
  493. * @cpu: The CPU to obtain the physical page address for.
  494. * @addr: The virtual address.
  495. *
  496. * Obtains the physical page corresponding to a virtual one.
  497. * Use it only for debugging because no protection checks are done.
  498. *
  499. * Returns: Corresponding physical page address or -1 if no page found.
  500. */
  501. hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr);
  502. /** cpu_asidx_from_attrs:
  503. * @cpu: CPU
  504. * @attrs: memory transaction attributes
  505. *
  506. * Returns the address space index specifying the CPU AddressSpace
  507. * to use for a memory access with the given transaction attributes.
  508. */
  509. int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs);
  510. /**
  511. * cpu_virtio_is_big_endian:
  512. * @cpu: CPU
  513. * Returns %true if a CPU which supports runtime configurable endianness
  514. * is currently big-endian.
  515. */
  516. bool cpu_virtio_is_big_endian(CPUState *cpu);
  517. #endif /* CONFIG_USER_ONLY */
  518. /**
  519. * cpu_list_add:
  520. * @cpu: The CPU to be added to the list of CPUs.
  521. */
  522. void cpu_list_add(CPUState *cpu);
  523. /**
  524. * cpu_list_remove:
  525. * @cpu: The CPU to be removed from the list of CPUs.
  526. */
  527. void cpu_list_remove(CPUState *cpu);
  528. /**
  529. * cpu_reset:
  530. * @cpu: The CPU whose state is to be reset.
  531. */
  532. void cpu_reset(CPUState *cpu);
  533. /**
  534. * cpu_class_by_name:
  535. * @typename: The CPU base type.
  536. * @cpu_model: The model string without any parameters.
  537. *
  538. * Looks up a CPU #ObjectClass matching name @cpu_model.
  539. *
  540. * Returns: A #CPUClass or %NULL if not matching class is found.
  541. */
  542. ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
  543. /**
  544. * cpu_create:
  545. * @typename: The CPU type.
  546. *
  547. * Instantiates a CPU and realizes the CPU.
  548. *
  549. * Returns: A #CPUState or %NULL if an error occurred.
  550. */
  551. CPUState *cpu_create(const char *typename);
  552. /**
  553. * parse_cpu_option:
  554. * @cpu_option: The -cpu option including optional parameters.
  555. *
  556. * processes optional parameters and registers them as global properties
  557. *
  558. * Returns: type of CPU to create or prints error and terminates process
  559. * if an error occurred.
  560. */
  561. const char *parse_cpu_option(const char *cpu_option);
  562. /**
  563. * cpu_has_work:
  564. * @cpu: The vCPU to check.
  565. *
  566. * Checks whether the CPU has work to do.
  567. *
  568. * Returns: %true if the CPU has work, %false otherwise.
  569. */
  570. static inline bool cpu_has_work(CPUState *cpu)
  571. {
  572. CPUClass *cc = CPU_GET_CLASS(cpu);
  573. g_assert(cc->has_work);
  574. return cc->has_work(cpu);
  575. }
  576. /**
  577. * qemu_cpu_is_self:
  578. * @cpu: The vCPU to check against.
  579. *
  580. * Checks whether the caller is executing on the vCPU thread.
  581. *
  582. * Returns: %true if called from @cpu's thread, %false otherwise.
  583. */
  584. bool qemu_cpu_is_self(CPUState *cpu);
  585. /**
  586. * qemu_cpu_kick:
  587. * @cpu: The vCPU to kick.
  588. *
  589. * Kicks @cpu's thread.
  590. */
  591. void qemu_cpu_kick(CPUState *cpu);
  592. /**
  593. * cpu_is_stopped:
  594. * @cpu: The CPU to check.
  595. *
  596. * Checks whether the CPU is stopped.
  597. *
  598. * Returns: %true if run state is not running or if artificially stopped;
  599. * %false otherwise.
  600. */
  601. bool cpu_is_stopped(CPUState *cpu);
  602. /**
  603. * do_run_on_cpu:
  604. * @cpu: The vCPU to run on.
  605. * @func: The function to be executed.
  606. * @data: Data to pass to the function.
  607. * @mutex: Mutex to release while waiting for @func to run.
  608. *
  609. * Used internally in the implementation of run_on_cpu.
  610. */
  611. void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
  612. QemuMutex *mutex);
  613. /**
  614. * run_on_cpu:
  615. * @cpu: The vCPU to run on.
  616. * @func: The function to be executed.
  617. * @data: Data to pass to the function.
  618. *
  619. * Schedules the function @func for execution on the vCPU @cpu.
  620. */
  621. void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
  622. /**
  623. * async_run_on_cpu:
  624. * @cpu: The vCPU to run on.
  625. * @func: The function to be executed.
  626. * @data: Data to pass to the function.
  627. *
  628. * Schedules the function @func for execution on the vCPU @cpu asynchronously.
  629. */
  630. void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
  631. /**
  632. * async_safe_run_on_cpu:
  633. * @cpu: The vCPU to run on.
  634. * @func: The function to be executed.
  635. * @data: Data to pass to the function.
  636. *
  637. * Schedules the function @func for execution on the vCPU @cpu asynchronously,
  638. * while all other vCPUs are sleeping.
  639. *
  640. * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the
  641. * BQL.
  642. */
  643. void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
  644. /**
  645. * cpu_in_exclusive_context()
  646. * @cpu: The vCPU to check
  647. *
  648. * Returns true if @cpu is an exclusive context, for example running
  649. * something which has previously been queued via async_safe_run_on_cpu().
  650. */
  651. static inline bool cpu_in_exclusive_context(const CPUState *cpu)
  652. {
  653. return cpu->in_exclusive_context;
  654. }
  655. /**
  656. * qemu_get_cpu:
  657. * @index: The CPUState@cpu_index value of the CPU to obtain.
  658. *
  659. * Gets a CPU matching @index.
  660. *
  661. * Returns: The CPU or %NULL if there is no matching CPU.
  662. */
  663. CPUState *qemu_get_cpu(int index);
  664. /**
  665. * cpu_exists:
  666. * @id: Guest-exposed CPU ID to lookup.
  667. *
  668. * Search for CPU with specified ID.
  669. *
  670. * Returns: %true - CPU is found, %false - CPU isn't found.
  671. */
  672. bool cpu_exists(int64_t id);
  673. /**
  674. * cpu_by_arch_id:
  675. * @id: Guest-exposed CPU ID of the CPU to obtain.
  676. *
  677. * Get a CPU with matching @id.
  678. *
  679. * Returns: The CPU or %NULL if there is no matching CPU.
  680. */
  681. CPUState *cpu_by_arch_id(int64_t id);
  682. /**
  683. * cpu_interrupt:
  684. * @cpu: The CPU to set an interrupt on.
  685. * @mask: The interrupts to set.
  686. *
  687. * Invokes the interrupt handler.
  688. */
  689. void cpu_interrupt(CPUState *cpu, int mask);
  690. /**
  691. * cpu_set_pc:
  692. * @cpu: The CPU to set the program counter for.
  693. * @addr: Program counter value.
  694. *
  695. * Sets the program counter for a CPU.
  696. */
  697. static inline void cpu_set_pc(CPUState *cpu, vaddr addr)
  698. {
  699. CPUClass *cc = CPU_GET_CLASS(cpu);
  700. cc->set_pc(cpu, addr);
  701. }
  702. /**
  703. * cpu_reset_interrupt:
  704. * @cpu: The CPU to clear the interrupt on.
  705. * @mask: The interrupt mask to clear.
  706. *
  707. * Resets interrupts on the vCPU @cpu.
  708. */
  709. void cpu_reset_interrupt(CPUState *cpu, int mask);
  710. /**
  711. * cpu_exit:
  712. * @cpu: The CPU to exit.
  713. *
  714. * Requests the CPU @cpu to exit execution.
  715. */
  716. void cpu_exit(CPUState *cpu);
  717. /**
  718. * cpu_resume:
  719. * @cpu: The CPU to resume.
  720. *
  721. * Resumes CPU, i.e. puts CPU into runnable state.
  722. */
  723. void cpu_resume(CPUState *cpu);
  724. /**
  725. * cpu_remove_sync:
  726. * @cpu: The CPU to remove.
  727. *
  728. * Requests the CPU to be removed and waits till it is removed.
  729. */
  730. void cpu_remove_sync(CPUState *cpu);
  731. /**
  732. * process_queued_cpu_work() - process all items on CPU work queue
  733. * @cpu: The CPU which work queue to process.
  734. */
  735. void process_queued_cpu_work(CPUState *cpu);
  736. /**
  737. * cpu_exec_start:
  738. * @cpu: The CPU for the current thread.
  739. *
  740. * Record that a CPU has started execution and can be interrupted with
  741. * cpu_exit.
  742. */
  743. void cpu_exec_start(CPUState *cpu);
  744. /**
  745. * cpu_exec_end:
  746. * @cpu: The CPU for the current thread.
  747. *
  748. * Record that a CPU has stopped execution and exclusive sections
  749. * can be executed without interrupting it.
  750. */
  751. void cpu_exec_end(CPUState *cpu);
  752. /**
  753. * start_exclusive:
  754. *
  755. * Wait for a concurrent exclusive section to end, and then start
  756. * a section of work that is run while other CPUs are not running
  757. * between cpu_exec_start and cpu_exec_end. CPUs that are running
  758. * cpu_exec are exited immediately. CPUs that call cpu_exec_start
  759. * during the exclusive section go to sleep until this CPU calls
  760. * end_exclusive.
  761. */
  762. void start_exclusive(void);
  763. /**
  764. * end_exclusive:
  765. *
  766. * Concludes an exclusive execution section started by start_exclusive.
  767. */
  768. void end_exclusive(void);
  769. /**
  770. * qemu_init_vcpu:
  771. * @cpu: The vCPU to initialize.
  772. *
  773. * Initializes a vCPU.
  774. */
  775. void qemu_init_vcpu(CPUState *cpu);
  776. #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
  777. #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
  778. #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
  779. /**
  780. * cpu_single_step:
  781. * @cpu: CPU to the flags for.
  782. * @enabled: Flags to enable.
  783. *
  784. * Enables or disables single-stepping for @cpu.
  785. */
  786. void cpu_single_step(CPUState *cpu, int enabled);
  787. /* Breakpoint/watchpoint flags */
  788. #define BP_MEM_READ 0x01
  789. #define BP_MEM_WRITE 0x02
  790. #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
  791. #define BP_STOP_BEFORE_ACCESS 0x04
  792. /* 0x08 currently unused */
  793. #define BP_GDB 0x10
  794. #define BP_CPU 0x20
  795. #define BP_ANY (BP_GDB | BP_CPU)
  796. #define BP_WATCHPOINT_HIT_READ 0x40
  797. #define BP_WATCHPOINT_HIT_WRITE 0x80
  798. #define BP_WATCHPOINT_HIT (BP_WATCHPOINT_HIT_READ | BP_WATCHPOINT_HIT_WRITE)
  799. int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
  800. CPUBreakpoint **breakpoint);
  801. int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
  802. void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
  803. void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
  804. /* Return true if PC matches an installed breakpoint. */
  805. static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask)
  806. {
  807. CPUBreakpoint *bp;
  808. if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
  809. QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
  810. if (bp->pc == pc && (bp->flags & mask)) {
  811. return true;
  812. }
  813. }
  814. }
  815. return false;
  816. }
  817. #ifdef CONFIG_USER_ONLY
  818. static inline int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
  819. int flags, CPUWatchpoint **watchpoint)
  820. {
  821. return -ENOSYS;
  822. }
  823. static inline int cpu_watchpoint_remove(CPUState *cpu, vaddr addr,
  824. vaddr len, int flags)
  825. {
  826. return -ENOSYS;
  827. }
  828. static inline void cpu_watchpoint_remove_by_ref(CPUState *cpu,
  829. CPUWatchpoint *wp)
  830. {
  831. }
  832. static inline void cpu_watchpoint_remove_all(CPUState *cpu, int mask)
  833. {
  834. }
  835. static inline void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len,
  836. MemTxAttrs atr, int fl, uintptr_t ra)
  837. {
  838. }
  839. static inline int cpu_watchpoint_address_matches(CPUState *cpu,
  840. vaddr addr, vaddr len)
  841. {
  842. return 0;
  843. }
  844. #else
  845. int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
  846. int flags, CPUWatchpoint **watchpoint);
  847. int cpu_watchpoint_remove(CPUState *cpu, vaddr addr,
  848. vaddr len, int flags);
  849. void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint);
  850. void cpu_watchpoint_remove_all(CPUState *cpu, int mask);
  851. /**
  852. * cpu_check_watchpoint:
  853. * @cpu: cpu context
  854. * @addr: guest virtual address
  855. * @len: access length
  856. * @attrs: memory access attributes
  857. * @flags: watchpoint access type
  858. * @ra: unwind return address
  859. *
  860. * Check for a watchpoint hit in [addr, addr+len) of the type
  861. * specified by @flags. Exit via exception with a hit.
  862. */
  863. void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len,
  864. MemTxAttrs attrs, int flags, uintptr_t ra);
  865. /**
  866. * cpu_watchpoint_address_matches:
  867. * @cpu: cpu context
  868. * @addr: guest virtual address
  869. * @len: access length
  870. *
  871. * Return the watchpoint flags that apply to [addr, addr+len).
  872. * If no watchpoint is registered for the range, the result is 0.
  873. */
  874. int cpu_watchpoint_address_matches(CPUState *cpu, vaddr addr, vaddr len);
  875. #endif
  876. /**
  877. * cpu_get_address_space:
  878. * @cpu: CPU to get address space from
  879. * @asidx: index identifying which address space to get
  880. *
  881. * Return the requested address space of this CPU. @asidx
  882. * specifies which address space to read.
  883. */
  884. AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx);
  885. void QEMU_NORETURN cpu_abort(CPUState *cpu, const char *fmt, ...)
  886. G_GNUC_PRINTF(2, 3);
  887. /* $(top_srcdir)/cpu.c */
  888. void cpu_class_init_props(DeviceClass *dc);
  889. void cpu_exec_initfn(CPUState *cpu);
  890. void cpu_exec_realizefn(CPUState *cpu, Error **errp);
  891. void cpu_exec_unrealizefn(CPUState *cpu);
  892. /**
  893. * target_words_bigendian:
  894. * Returns true if the (default) endianness of the target is big endian,
  895. * false otherwise. Note that in target-specific code, you can use
  896. * TARGET_BIG_ENDIAN directly instead. On the other hand, common
  897. * code should normally never need to know about the endianness of the
  898. * target, so please do *not* use this function unless you know very well
  899. * what you are doing!
  900. */
  901. bool target_words_bigendian(void);
  902. void page_size_init(void);
  903. #ifdef NEED_CPU_H
  904. #ifdef CONFIG_SOFTMMU
  905. extern const VMStateDescription vmstate_cpu_common;
  906. #define VMSTATE_CPU() { \
  907. .name = "parent_obj", \
  908. .size = sizeof(CPUState), \
  909. .vmsd = &vmstate_cpu_common, \
  910. .flags = VMS_STRUCT, \
  911. .offset = 0, \
  912. }
  913. #endif /* CONFIG_SOFTMMU */
  914. #endif /* NEED_CPU_H */
  915. #define UNASSIGNED_CPU_INDEX -1
  916. #define UNASSIGNED_CLUSTER_INDEX -1
  917. #endif