cpu.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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/breakpoint.h"
  25. #include "exec/hwaddr.h"
  26. #include "exec/vaddr.h"
  27. #include "exec/memattrs.h"
  28. #include "exec/mmu-access-type.h"
  29. #include "exec/tlb-common.h"
  30. #include "qapi/qapi-types-machine.h"
  31. #include "qapi/qapi-types-run-state.h"
  32. #include "qemu/bitmap.h"
  33. #include "qemu/rcu_queue.h"
  34. #include "qemu/queue.h"
  35. #include "qemu/lockcnt.h"
  36. #include "qemu/thread.h"
  37. #include "qom/object.h"
  38. typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size,
  39. void *opaque);
  40. /**
  41. * SECTION:cpu
  42. * @section_id: QEMU-cpu
  43. * @title: CPU Class
  44. * @short_description: Base class for all CPUs
  45. */
  46. #define TYPE_CPU "cpu"
  47. /* Since this macro is used a lot in hot code paths and in conjunction with
  48. * FooCPU *foo_env_get_cpu(), we deviate from usual QOM practice by using
  49. * an unchecked cast.
  50. */
  51. #define CPU(obj) ((CPUState *)(obj))
  52. /*
  53. * The class checkers bring in CPU_GET_CLASS() which is potentially
  54. * expensive given the eventual call to
  55. * object_class_dynamic_cast_assert(). Because of this the CPUState
  56. * has a cached value for the class in cs->cc which is set up in
  57. * cpu_exec_realizefn() for use in hot code paths.
  58. */
  59. typedef struct CPUClass CPUClass;
  60. DECLARE_CLASS_CHECKERS(CPUClass, CPU,
  61. TYPE_CPU)
  62. /**
  63. * OBJECT_DECLARE_CPU_TYPE:
  64. * @CpuInstanceType: instance struct name
  65. * @CpuClassType: class struct name
  66. * @CPU_MODULE_OBJ_NAME: the CPU name in uppercase with underscore separators
  67. *
  68. * This macro is typically used in "cpu-qom.h" header file, and will:
  69. *
  70. * - create the typedefs for the CPU object and class structs
  71. * - register the type for use with g_autoptr
  72. * - provide three standard type cast functions
  73. *
  74. * The object struct and class struct need to be declared manually.
  75. */
  76. #define OBJECT_DECLARE_CPU_TYPE(CpuInstanceType, CpuClassType, CPU_MODULE_OBJ_NAME) \
  77. typedef struct ArchCPU CpuInstanceType; \
  78. OBJECT_DECLARE_TYPE(ArchCPU, CpuClassType, CPU_MODULE_OBJ_NAME);
  79. typedef struct CPUWatchpoint CPUWatchpoint;
  80. /* see physmem.c */
  81. struct CPUAddressSpace;
  82. /* see accel/tcg/tb-jmp-cache.h */
  83. struct CPUJumpCache;
  84. /* see accel-cpu.h */
  85. struct AccelCPUClass;
  86. /* see sysemu-cpu-ops.h */
  87. struct SysemuCPUOps;
  88. /**
  89. * CPUClass:
  90. * @class_by_name: Callback to map -cpu command line model name to an
  91. * instantiatable CPU type.
  92. * @parse_features: Callback to parse command line arguments.
  93. * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
  94. * @mmu_index: Callback for choosing softmmu mmu index;
  95. * may be used internally by memory_rw_debug without TCG.
  96. * @memory_rw_debug: Callback for GDB memory access.
  97. * @dump_state: Callback for dumping state.
  98. * @query_cpu_fast:
  99. * Fill in target specific information for the "query-cpus-fast"
  100. * QAPI call.
  101. * @get_arch_id: Callback for getting architecture-dependent CPU ID.
  102. * @set_pc: Callback for setting the Program Counter register. This
  103. * should have the semantics used by the target architecture when
  104. * setting the PC from a source such as an ELF file entry point;
  105. * for example on Arm it will also set the Thumb mode bit based
  106. * on the least significant bit of the new PC value.
  107. * If the target behaviour here is anything other than "set
  108. * the PC register to the value passed in" then the target must
  109. * also implement the synchronize_from_tb hook.
  110. * @get_pc: Callback for getting the Program Counter register.
  111. * As above, with the semantics of the target architecture.
  112. * @gdb_read_register: Callback for letting GDB read a register.
  113. * No more than @gdb_num_core_regs registers can be read.
  114. * @gdb_write_register: Callback for letting GDB write a register.
  115. * No more than @gdb_num_core_regs registers can be written.
  116. * @gdb_adjust_breakpoint: Callback for adjusting the address of a
  117. * breakpoint. Used by AVR to handle a gdb mis-feature with
  118. * its Harvard architecture split code and data.
  119. * @gdb_num_core_regs: Number of core registers accessible to GDB or 0 to infer
  120. * from @gdb_core_xml_file.
  121. * @gdb_core_xml_file: File name for core registers GDB XML description.
  122. * @gdb_stop_before_watchpoint: Indicates whether GDB expects the CPU to stop
  123. * before the insn which triggers a watchpoint rather than after it.
  124. * @gdb_arch_name: Optional callback that returns the architecture name known
  125. * to GDB. The returned value is expected to be a simple constant string:
  126. * the caller will not g_free() it.
  127. * @disas_set_info: Setup architecture specific components of disassembly info
  128. * @adjust_watchpoint_address: Perform a target-specific adjustment to an
  129. * address before attempting to match it against watchpoints.
  130. * @deprecation_note: If this CPUClass is deprecated, this field provides
  131. * related information.
  132. *
  133. * Represents a CPU family or model.
  134. */
  135. struct CPUClass {
  136. /*< private >*/
  137. DeviceClass parent_class;
  138. /*< public >*/
  139. ObjectClass *(*class_by_name)(const char *cpu_model);
  140. void (*parse_features)(const char *typename, char *str, Error **errp);
  141. int (*mmu_index)(CPUState *cpu, bool ifetch);
  142. int (*memory_rw_debug)(CPUState *cpu, vaddr addr,
  143. uint8_t *buf, size_t len, bool is_write);
  144. void (*dump_state)(CPUState *cpu, FILE *, int flags);
  145. void (*query_cpu_fast)(CPUState *cpu, CpuInfoFast *value);
  146. int64_t (*get_arch_id)(CPUState *cpu);
  147. void (*set_pc)(CPUState *cpu, vaddr value);
  148. vaddr (*get_pc)(CPUState *cpu);
  149. int (*gdb_read_register)(CPUState *cpu, GByteArray *buf, int reg);
  150. int (*gdb_write_register)(CPUState *cpu, uint8_t *buf, int reg);
  151. vaddr (*gdb_adjust_breakpoint)(CPUState *cpu, vaddr addr);
  152. const char *gdb_core_xml_file;
  153. const gchar * (*gdb_arch_name)(CPUState *cpu);
  154. void (*disas_set_info)(CPUState *cpu, disassemble_info *info);
  155. const char *deprecation_note;
  156. struct AccelCPUClass *accel_cpu;
  157. /* when system emulation is not available, this pointer is NULL */
  158. const struct SysemuCPUOps *sysemu_ops;
  159. /* when TCG is not available, this pointer is NULL */
  160. const TCGCPUOps *tcg_ops;
  161. /*
  162. * if not NULL, this is called in order for the CPUClass to initialize
  163. * class data that depends on the accelerator, see accel/accel-common.c.
  164. */
  165. void (*init_accel_cpu)(struct AccelCPUClass *accel_cpu, CPUClass *cc);
  166. /*
  167. * Keep non-pointer data at the end to minimize holes.
  168. */
  169. int reset_dump_flags;
  170. int gdb_num_core_regs;
  171. bool gdb_stop_before_watchpoint;
  172. };
  173. /*
  174. * Fix the number of mmu modes to 16, which is also the maximum
  175. * supported by the softmmu tlb api.
  176. */
  177. #define NB_MMU_MODES 16
  178. /* Use a fully associative victim tlb of 8 entries. */
  179. #define CPU_VTLB_SIZE 8
  180. /*
  181. * The full TLB entry, which is not accessed by generated TCG code,
  182. * so the layout is not as critical as that of CPUTLBEntry. This is
  183. * also why we don't want to combine the two structs.
  184. */
  185. struct CPUTLBEntryFull {
  186. /*
  187. * @xlat_section contains:
  188. * - in the lower TARGET_PAGE_BITS, a physical section number
  189. * - with the lower TARGET_PAGE_BITS masked off, an offset which
  190. * must be added to the virtual address to obtain:
  191. * + the ram_addr_t of the target RAM (if the physical section
  192. * number is PHYS_SECTION_NOTDIRTY or PHYS_SECTION_ROM)
  193. * + the offset within the target MemoryRegion (otherwise)
  194. */
  195. hwaddr xlat_section;
  196. /*
  197. * @phys_addr contains the physical address in the address space
  198. * given by cpu_asidx_from_attrs(cpu, @attrs).
  199. */
  200. hwaddr phys_addr;
  201. /* @attrs contains the memory transaction attributes for the page. */
  202. MemTxAttrs attrs;
  203. /* @prot contains the complete protections for the page. */
  204. uint8_t prot;
  205. /* @lg_page_size contains the log2 of the page size. */
  206. uint8_t lg_page_size;
  207. /* Additional tlb flags requested by tlb_fill. */
  208. uint8_t tlb_fill_flags;
  209. /*
  210. * Additional tlb flags for use by the slow path. If non-zero,
  211. * the corresponding CPUTLBEntry comparator must have TLB_FORCE_SLOW.
  212. */
  213. uint8_t slow_flags[MMU_ACCESS_COUNT];
  214. /*
  215. * Allow target-specific additions to this structure.
  216. * This may be used to cache items from the guest cpu
  217. * page tables for later use by the implementation.
  218. */
  219. union {
  220. /*
  221. * Cache the attrs and shareability fields from the page table entry.
  222. *
  223. * For ARMMMUIdx_Stage2*, pte_attrs is the S2 descriptor bits [5:2].
  224. * Otherwise, pte_attrs is the same as the MAIR_EL1 8-bit format.
  225. * For shareability and guarded, as in the SH and GP fields respectively
  226. * of the VMSAv8-64 PTEs.
  227. */
  228. struct {
  229. uint8_t pte_attrs;
  230. uint8_t shareability;
  231. bool guarded;
  232. } arm;
  233. } extra;
  234. };
  235. /*
  236. * Data elements that are per MMU mode, minus the bits accessed by
  237. * the TCG fast path.
  238. */
  239. typedef struct CPUTLBDesc {
  240. /*
  241. * Describe a region covering all of the large pages allocated
  242. * into the tlb. When any page within this region is flushed,
  243. * we must flush the entire tlb. The region is matched if
  244. * (addr & large_page_mask) == large_page_addr.
  245. */
  246. vaddr large_page_addr;
  247. vaddr large_page_mask;
  248. /* host time (in ns) at the beginning of the time window */
  249. int64_t window_begin_ns;
  250. /* maximum number of entries observed in the window */
  251. size_t window_max_entries;
  252. size_t n_used_entries;
  253. /* The next index to use in the tlb victim table. */
  254. size_t vindex;
  255. /* The tlb victim table, in two parts. */
  256. CPUTLBEntry vtable[CPU_VTLB_SIZE];
  257. CPUTLBEntryFull vfulltlb[CPU_VTLB_SIZE];
  258. CPUTLBEntryFull *fulltlb;
  259. } CPUTLBDesc;
  260. /*
  261. * Data elements that are shared between all MMU modes.
  262. */
  263. typedef struct CPUTLBCommon {
  264. /* Serialize updates to f.table and d.vtable, and others as noted. */
  265. QemuSpin lock;
  266. /*
  267. * Within dirty, for each bit N, modifications have been made to
  268. * mmu_idx N since the last time that mmu_idx was flushed.
  269. * Protected by tlb_c.lock.
  270. */
  271. uint16_t dirty;
  272. /*
  273. * Statistics. These are not lock protected, but are read and
  274. * written atomically. This allows the monitor to print a snapshot
  275. * of the stats without interfering with the cpu.
  276. */
  277. size_t full_flush_count;
  278. size_t part_flush_count;
  279. size_t elide_flush_count;
  280. } CPUTLBCommon;
  281. /*
  282. * The entire softmmu tlb, for all MMU modes.
  283. * The meaning of each of the MMU modes is defined in the target code.
  284. * Since this is placed within CPUNegativeOffsetState, the smallest
  285. * negative offsets are at the end of the struct.
  286. */
  287. typedef struct CPUTLB {
  288. #ifdef CONFIG_TCG
  289. CPUTLBCommon c;
  290. CPUTLBDesc d[NB_MMU_MODES];
  291. CPUTLBDescFast f[NB_MMU_MODES];
  292. #endif
  293. } CPUTLB;
  294. /*
  295. * Low 16 bits: number of cycles left, used only in icount mode.
  296. * High 16 bits: Set to -1 to force TCG to stop executing linked TBs
  297. * for this CPU and return to its top level loop (even in non-icount mode).
  298. * This allows a single read-compare-cbranch-write sequence to test
  299. * for both decrementer underflow and exceptions.
  300. */
  301. typedef union IcountDecr {
  302. uint32_t u32;
  303. struct {
  304. #if HOST_BIG_ENDIAN
  305. uint16_t high;
  306. uint16_t low;
  307. #else
  308. uint16_t low;
  309. uint16_t high;
  310. #endif
  311. } u16;
  312. } IcountDecr;
  313. /**
  314. * CPUNegativeOffsetState: Elements of CPUState most efficiently accessed
  315. * from CPUArchState, via small negative offsets.
  316. * @can_do_io: True if memory-mapped IO is allowed.
  317. * @plugin_mem_cbs: active plugin memory callbacks
  318. * @plugin_mem_value_low: 64 lower bits of latest accessed mem value.
  319. * @plugin_mem_value_high: 64 higher bits of latest accessed mem value.
  320. */
  321. typedef struct CPUNegativeOffsetState {
  322. CPUTLB tlb;
  323. #ifdef CONFIG_PLUGIN
  324. /*
  325. * The callback pointer are accessed via TCG (see gen_empty_mem_helper).
  326. */
  327. GArray *plugin_mem_cbs;
  328. uint64_t plugin_mem_value_low;
  329. uint64_t plugin_mem_value_high;
  330. #endif
  331. IcountDecr icount_decr;
  332. bool can_do_io;
  333. } CPUNegativeOffsetState;
  334. struct KVMState;
  335. struct kvm_run;
  336. /* work queue */
  337. /* The union type allows passing of 64 bit target pointers on 32 bit
  338. * hosts in a single parameter
  339. */
  340. typedef union {
  341. int host_int;
  342. unsigned long host_ulong;
  343. void *host_ptr;
  344. vaddr target_ptr;
  345. } run_on_cpu_data;
  346. #define RUN_ON_CPU_HOST_PTR(p) ((run_on_cpu_data){.host_ptr = (p)})
  347. #define RUN_ON_CPU_HOST_INT(i) ((run_on_cpu_data){.host_int = (i)})
  348. #define RUN_ON_CPU_HOST_ULONG(ul) ((run_on_cpu_data){.host_ulong = (ul)})
  349. #define RUN_ON_CPU_TARGET_PTR(v) ((run_on_cpu_data){.target_ptr = (v)})
  350. #define RUN_ON_CPU_NULL RUN_ON_CPU_HOST_PTR(NULL)
  351. typedef void (*run_on_cpu_func)(CPUState *cpu, run_on_cpu_data data);
  352. struct qemu_work_item;
  353. #define CPU_UNSET_NUMA_NODE_ID -1
  354. /**
  355. * struct CPUState - common state of one CPU core or thread.
  356. *
  357. * @cpu_index: CPU index (informative).
  358. * @cluster_index: Identifies which cluster this CPU is in.
  359. * For boards which don't define clusters or for "loose" CPUs not assigned
  360. * to a cluster this will be UNASSIGNED_CLUSTER_INDEX; otherwise it will
  361. * be the same as the cluster-id property of the CPU object's TYPE_CPU_CLUSTER
  362. * QOM parent.
  363. * Under TCG this value is propagated to @tcg_cflags.
  364. * See TranslationBlock::TCG CF_CLUSTER_MASK.
  365. * @tcg_cflags: Pre-computed cflags for this cpu.
  366. * @nr_threads: Number of threads within this CPU core.
  367. * @thread: Host thread details, only live once @created is #true
  368. * @sem: WIN32 only semaphore used only for qtest
  369. * @thread_id: native thread id of vCPU, only live once @created is #true
  370. * @running: #true if CPU is currently running (lockless).
  371. * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end;
  372. * valid under cpu_list_lock.
  373. * @created: Indicates whether the CPU thread has been successfully created.
  374. * @halt_cond: condition variable sleeping threads can wait on.
  375. * @interrupt_request: Indicates a pending interrupt request.
  376. * @halted: Nonzero if the CPU is in suspended state.
  377. * @stop: Indicates a pending stop request.
  378. * @stopped: Indicates the CPU has been artificially stopped.
  379. * @unplug: Indicates a pending CPU unplug request.
  380. * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
  381. * @singlestep_enabled: Flags for single-stepping.
  382. * @icount_extra: Instructions until next timer event.
  383. * @cpu_ases: Pointer to array of CPUAddressSpaces (which define the
  384. * AddressSpaces this CPU has)
  385. * @num_ases: number of CPUAddressSpaces in @cpu_ases
  386. * @as: Pointer to the first AddressSpace, for the convenience of targets which
  387. * only have a single AddressSpace
  388. * @gdb_regs: Additional GDB registers.
  389. * @gdb_num_regs: Number of total registers accessible to GDB.
  390. * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
  391. * @node: QTAILQ of CPUs sharing TB cache.
  392. * @opaque: User data.
  393. * @mem_io_pc: Host Program Counter at which the memory was accessed.
  394. * @accel: Pointer to accelerator specific state.
  395. * @kvm_fd: vCPU file descriptor for KVM.
  396. * @work_mutex: Lock to prevent multiple access to @work_list.
  397. * @work_list: List of pending asynchronous work.
  398. * @plugin_state: per-CPU plugin state
  399. * @ignore_memory_transaction_failures: Cached copy of the MachineState
  400. * flag of the same name: allows the board to suppress calling of the
  401. * CPU do_transaction_failed hook function.
  402. * @kvm_dirty_gfns: Points to the KVM dirty ring for this CPU when KVM dirty
  403. * ring is enabled.
  404. * @kvm_fetch_index: Keeps the index that we last fetched from the per-vCPU
  405. * dirty ring structure.
  406. *
  407. * @neg_align: The CPUState is the common part of a concrete ArchCPU
  408. * which is allocated when an individual CPU instance is created. As
  409. * such care is taken is ensure there is no gap between between
  410. * CPUState and CPUArchState within ArchCPU.
  411. *
  412. * @neg: The architectural register state ("cpu_env") immediately follows
  413. * CPUState in ArchCPU and is passed to TCG code. The @neg structure holds
  414. * some common TCG CPU variables which are accessed with a negative offset
  415. * from cpu_env.
  416. */
  417. struct CPUState {
  418. /*< private >*/
  419. DeviceState parent_obj;
  420. /* cache to avoid expensive CPU_GET_CLASS */
  421. CPUClass *cc;
  422. /*< public >*/
  423. int nr_threads;
  424. struct QemuThread *thread;
  425. #ifdef _WIN32
  426. QemuSemaphore sem;
  427. #endif
  428. int thread_id;
  429. bool running, has_waiter;
  430. struct QemuCond *halt_cond;
  431. bool thread_kicked;
  432. bool created;
  433. bool stop;
  434. bool stopped;
  435. /* Should CPU start in powered-off state? */
  436. bool start_powered_off;
  437. bool unplug;
  438. bool crash_occurred;
  439. bool exit_request;
  440. int exclusive_context_count;
  441. uint32_t cflags_next_tb;
  442. /* updates protected by BQL */
  443. uint32_t interrupt_request;
  444. int singlestep_enabled;
  445. int64_t icount_budget;
  446. int64_t icount_extra;
  447. uint64_t random_seed;
  448. sigjmp_buf jmp_env;
  449. QemuMutex work_mutex;
  450. QSIMPLEQ_HEAD(, qemu_work_item) work_list;
  451. struct CPUAddressSpace *cpu_ases;
  452. int cpu_ases_count;
  453. int num_ases;
  454. AddressSpace *as;
  455. MemoryRegion *memory;
  456. struct CPUJumpCache *tb_jmp_cache;
  457. GArray *gdb_regs;
  458. int gdb_num_regs;
  459. int gdb_num_g_regs;
  460. QTAILQ_ENTRY(CPUState) node;
  461. /* ice debug support */
  462. QTAILQ_HEAD(, CPUBreakpoint) breakpoints;
  463. QTAILQ_HEAD(, CPUWatchpoint) watchpoints;
  464. CPUWatchpoint *watchpoint_hit;
  465. void *opaque;
  466. /* In order to avoid passing too many arguments to the MMIO helpers,
  467. * we store some rarely used information in the CPU context.
  468. */
  469. uintptr_t mem_io_pc;
  470. /* Only used in KVM */
  471. int kvm_fd;
  472. struct KVMState *kvm_state;
  473. struct kvm_run *kvm_run;
  474. struct kvm_dirty_gfn *kvm_dirty_gfns;
  475. uint32_t kvm_fetch_index;
  476. uint64_t dirty_pages;
  477. int kvm_vcpu_stats_fd;
  478. bool vcpu_dirty;
  479. /* Use by accel-block: CPU is executing an ioctl() */
  480. QemuLockCnt in_ioctl_lock;
  481. #ifdef CONFIG_PLUGIN
  482. CPUPluginState *plugin_state;
  483. #endif
  484. /* TODO Move common fields from CPUArchState here. */
  485. int cpu_index;
  486. int cluster_index;
  487. uint32_t tcg_cflags;
  488. uint32_t halted;
  489. int32_t exception_index;
  490. AccelCPUState *accel;
  491. /* Used to keep track of an outstanding cpu throttle thread for migration
  492. * autoconverge
  493. */
  494. bool throttle_thread_scheduled;
  495. /*
  496. * Sleep throttle_us_per_full microseconds once dirty ring is full
  497. * if dirty page rate limit is enabled.
  498. */
  499. int64_t throttle_us_per_full;
  500. bool ignore_memory_transaction_failures;
  501. /* Used for user-only emulation of prctl(PR_SET_UNALIGN). */
  502. bool prctl_unalign_sigbus;
  503. /* track IOMMUs whose translations we've cached in the TCG TLB */
  504. GArray *iommu_notifiers;
  505. /*
  506. * MUST BE LAST in order to minimize the displacement to CPUArchState.
  507. */
  508. char neg_align[-sizeof(CPUNegativeOffsetState) % 16] QEMU_ALIGNED(16);
  509. CPUNegativeOffsetState neg;
  510. };
  511. /* Validate placement of CPUNegativeOffsetState. */
  512. QEMU_BUILD_BUG_ON(offsetof(CPUState, neg) !=
  513. sizeof(CPUState) - sizeof(CPUNegativeOffsetState));
  514. static inline CPUArchState *cpu_env(CPUState *cpu)
  515. {
  516. /* We validate that CPUArchState follows CPUState in cpu-all.h. */
  517. return (CPUArchState *)(cpu + 1);
  518. }
  519. typedef QTAILQ_HEAD(CPUTailQ, CPUState) CPUTailQ;
  520. extern CPUTailQ cpus_queue;
  521. #define first_cpu QTAILQ_FIRST_RCU(&cpus_queue)
  522. #define CPU_NEXT(cpu) QTAILQ_NEXT_RCU(cpu, node)
  523. #define CPU_FOREACH(cpu) QTAILQ_FOREACH_RCU(cpu, &cpus_queue, node)
  524. #define CPU_FOREACH_SAFE(cpu, next_cpu) \
  525. QTAILQ_FOREACH_SAFE_RCU(cpu, &cpus_queue, node, next_cpu)
  526. extern __thread CPUState *current_cpu;
  527. /**
  528. * qemu_tcg_mttcg_enabled:
  529. * Check whether we are running MultiThread TCG or not.
  530. *
  531. * Returns: %true if we are in MTTCG mode %false otherwise.
  532. */
  533. extern bool mttcg_enabled;
  534. #define qemu_tcg_mttcg_enabled() (mttcg_enabled)
  535. /**
  536. * cpu_paging_enabled:
  537. * @cpu: The CPU whose state is to be inspected.
  538. *
  539. * Returns: %true if paging is enabled, %false otherwise.
  540. */
  541. bool cpu_paging_enabled(const CPUState *cpu);
  542. #if !defined(CONFIG_USER_ONLY)
  543. /**
  544. * cpu_get_memory_mapping:
  545. * @cpu: The CPU whose memory mappings are to be obtained.
  546. * @list: Where to write the memory mappings to.
  547. * @errp: Pointer for reporting an #Error.
  548. *
  549. * Returns: %true on success, %false otherwise.
  550. */
  551. bool cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
  552. Error **errp);
  553. /**
  554. * cpu_write_elf64_note:
  555. * @f: pointer to a function that writes memory to a file
  556. * @cpu: The CPU whose memory is to be dumped
  557. * @cpuid: ID number of the CPU
  558. * @opaque: pointer to the CPUState struct
  559. */
  560. int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
  561. int cpuid, void *opaque);
  562. /**
  563. * cpu_write_elf64_qemunote:
  564. * @f: pointer to a function that writes memory to a file
  565. * @cpu: The CPU whose memory is to be dumped
  566. * @cpuid: ID number of the CPU
  567. * @opaque: pointer to the CPUState struct
  568. */
  569. int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
  570. void *opaque);
  571. /**
  572. * cpu_write_elf32_note:
  573. * @f: pointer to a function that writes memory to a file
  574. * @cpu: The CPU whose memory is to be dumped
  575. * @cpuid: ID number of the CPU
  576. * @opaque: pointer to the CPUState struct
  577. */
  578. int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
  579. int cpuid, void *opaque);
  580. /**
  581. * cpu_write_elf32_qemunote:
  582. * @f: pointer to a function that writes memory to a file
  583. * @cpu: The CPU whose memory is to be dumped
  584. * @cpuid: ID number of the CPU
  585. * @opaque: pointer to the CPUState struct
  586. */
  587. int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
  588. void *opaque);
  589. /**
  590. * cpu_get_crash_info:
  591. * @cpu: The CPU to get crash information for
  592. *
  593. * Gets the previously saved crash information.
  594. * Caller is responsible for freeing the data.
  595. */
  596. GuestPanicInformation *cpu_get_crash_info(CPUState *cpu);
  597. #endif /* !CONFIG_USER_ONLY */
  598. /**
  599. * CPUDumpFlags:
  600. * @CPU_DUMP_CODE:
  601. * @CPU_DUMP_FPU: dump FPU register state, not just integer
  602. * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
  603. * @CPU_DUMP_VPU: dump VPU registers
  604. */
  605. enum CPUDumpFlags {
  606. CPU_DUMP_CODE = 0x00010000,
  607. CPU_DUMP_FPU = 0x00020000,
  608. CPU_DUMP_CCOP = 0x00040000,
  609. CPU_DUMP_VPU = 0x00080000,
  610. };
  611. /**
  612. * cpu_dump_state:
  613. * @cpu: The CPU whose state is to be dumped.
  614. * @f: If non-null, dump to this stream, else to current print sink.
  615. *
  616. * Dumps CPU state.
  617. */
  618. void cpu_dump_state(CPUState *cpu, FILE *f, int flags);
  619. #ifndef CONFIG_USER_ONLY
  620. /**
  621. * cpu_get_phys_page_attrs_debug:
  622. * @cpu: The CPU to obtain the physical page address for.
  623. * @addr: The virtual address.
  624. * @attrs: Updated on return with the memory transaction attributes to use
  625. * for this access.
  626. *
  627. * Obtains the physical page corresponding to a virtual one, together
  628. * with the corresponding memory transaction attributes to use for the access.
  629. * Use it only for debugging because no protection checks are done.
  630. *
  631. * Returns: Corresponding physical page address or -1 if no page found.
  632. */
  633. hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
  634. MemTxAttrs *attrs);
  635. /**
  636. * cpu_get_phys_page_debug:
  637. * @cpu: The CPU to obtain the physical page address for.
  638. * @addr: The virtual address.
  639. *
  640. * Obtains the physical page corresponding to a virtual one.
  641. * Use it only for debugging because no protection checks are done.
  642. *
  643. * Returns: Corresponding physical page address or -1 if no page found.
  644. */
  645. hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr);
  646. /** cpu_asidx_from_attrs:
  647. * @cpu: CPU
  648. * @attrs: memory transaction attributes
  649. *
  650. * Returns the address space index specifying the CPU AddressSpace
  651. * to use for a memory access with the given transaction attributes.
  652. */
  653. int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs);
  654. /**
  655. * cpu_virtio_is_big_endian:
  656. * @cpu: CPU
  657. * Returns %true if a CPU which supports runtime configurable endianness
  658. * is currently big-endian.
  659. */
  660. bool cpu_virtio_is_big_endian(CPUState *cpu);
  661. /**
  662. * cpu_has_work:
  663. * @cpu: The vCPU to check.
  664. *
  665. * Checks whether the CPU has work to do.
  666. *
  667. * Returns: %true if the CPU has work, %false otherwise.
  668. */
  669. bool cpu_has_work(CPUState *cpu);
  670. #endif /* CONFIG_USER_ONLY */
  671. /**
  672. * cpu_list_add:
  673. * @cpu: The CPU to be added to the list of CPUs.
  674. */
  675. void cpu_list_add(CPUState *cpu);
  676. /**
  677. * cpu_list_remove:
  678. * @cpu: The CPU to be removed from the list of CPUs.
  679. */
  680. void cpu_list_remove(CPUState *cpu);
  681. /**
  682. * cpu_reset:
  683. * @cpu: The CPU whose state is to be reset.
  684. */
  685. void cpu_reset(CPUState *cpu);
  686. /**
  687. * cpu_class_by_name:
  688. * @typename: The CPU base type.
  689. * @cpu_model: The model string without any parameters.
  690. *
  691. * Looks up a concrete CPU #ObjectClass matching name @cpu_model.
  692. *
  693. * Returns: A concrete #CPUClass or %NULL if no matching class is found
  694. * or if the matching class is abstract.
  695. */
  696. ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
  697. /**
  698. * cpu_model_from_type:
  699. * @typename: The CPU type name
  700. *
  701. * Extract the CPU model name from the CPU type name. The
  702. * CPU type name is either the combination of the CPU model
  703. * name and suffix, or same to the CPU model name.
  704. *
  705. * Returns: CPU model name or NULL if the CPU class doesn't exist
  706. * The user should g_free() the string once no longer needed.
  707. */
  708. char *cpu_model_from_type(const char *typename);
  709. /**
  710. * cpu_create:
  711. * @typename: The CPU type.
  712. *
  713. * Instantiates a CPU and realizes the CPU.
  714. *
  715. * Returns: A #CPUState or %NULL if an error occurred.
  716. */
  717. CPUState *cpu_create(const char *typename);
  718. /**
  719. * parse_cpu_option:
  720. * @cpu_option: The -cpu option including optional parameters.
  721. *
  722. * processes optional parameters and registers them as global properties
  723. *
  724. * Returns: type of CPU to create or prints error and terminates process
  725. * if an error occurred.
  726. */
  727. const char *parse_cpu_option(const char *cpu_option);
  728. /**
  729. * qemu_cpu_is_self:
  730. * @cpu: The vCPU to check against.
  731. *
  732. * Checks whether the caller is executing on the vCPU thread.
  733. *
  734. * Returns: %true if called from @cpu's thread, %false otherwise.
  735. */
  736. bool qemu_cpu_is_self(CPUState *cpu);
  737. /**
  738. * qemu_cpu_kick:
  739. * @cpu: The vCPU to kick.
  740. *
  741. * Kicks @cpu's thread.
  742. */
  743. void qemu_cpu_kick(CPUState *cpu);
  744. /**
  745. * cpu_is_stopped:
  746. * @cpu: The CPU to check.
  747. *
  748. * Checks whether the CPU is stopped.
  749. *
  750. * Returns: %true if run state is not running or if artificially stopped;
  751. * %false otherwise.
  752. */
  753. bool cpu_is_stopped(CPUState *cpu);
  754. /**
  755. * do_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. * @mutex: Mutex to release while waiting for @func to run.
  760. *
  761. * Used internally in the implementation of run_on_cpu.
  762. */
  763. void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
  764. QemuMutex *mutex);
  765. /**
  766. * run_on_cpu:
  767. * @cpu: The vCPU to run on.
  768. * @func: The function to be executed.
  769. * @data: Data to pass to the function.
  770. *
  771. * Schedules the function @func for execution on the vCPU @cpu.
  772. */
  773. void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
  774. /**
  775. * async_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. */
  782. void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
  783. /**
  784. * async_safe_run_on_cpu:
  785. * @cpu: The vCPU to run on.
  786. * @func: The function to be executed.
  787. * @data: Data to pass to the function.
  788. *
  789. * Schedules the function @func for execution on the vCPU @cpu asynchronously,
  790. * while all other vCPUs are sleeping.
  791. *
  792. * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the
  793. * BQL.
  794. */
  795. void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
  796. /**
  797. * cpu_in_exclusive_context()
  798. * @cpu: The vCPU to check
  799. *
  800. * Returns true if @cpu is an exclusive context, for example running
  801. * something which has previously been queued via async_safe_run_on_cpu().
  802. */
  803. static inline bool cpu_in_exclusive_context(const CPUState *cpu)
  804. {
  805. return cpu->exclusive_context_count;
  806. }
  807. /**
  808. * qemu_get_cpu:
  809. * @index: The CPUState@cpu_index value of the CPU to obtain.
  810. *
  811. * Gets a CPU matching @index.
  812. *
  813. * Returns: The CPU or %NULL if there is no matching CPU.
  814. */
  815. CPUState *qemu_get_cpu(int index);
  816. /**
  817. * cpu_exists:
  818. * @id: Guest-exposed CPU ID to lookup.
  819. *
  820. * Search for CPU with specified ID.
  821. *
  822. * Returns: %true - CPU is found, %false - CPU isn't found.
  823. */
  824. bool cpu_exists(int64_t id);
  825. /**
  826. * cpu_by_arch_id:
  827. * @id: Guest-exposed CPU ID of the CPU to obtain.
  828. *
  829. * Get a CPU with matching @id.
  830. *
  831. * Returns: The CPU or %NULL if there is no matching CPU.
  832. */
  833. CPUState *cpu_by_arch_id(int64_t id);
  834. /**
  835. * cpu_interrupt:
  836. * @cpu: The CPU to set an interrupt on.
  837. * @mask: The interrupts to set.
  838. *
  839. * Invokes the interrupt handler.
  840. */
  841. void cpu_interrupt(CPUState *cpu, int mask);
  842. /**
  843. * cpu_set_pc:
  844. * @cpu: The CPU to set the program counter for.
  845. * @addr: Program counter value.
  846. *
  847. * Sets the program counter for a CPU.
  848. */
  849. static inline void cpu_set_pc(CPUState *cpu, vaddr addr)
  850. {
  851. cpu->cc->set_pc(cpu, addr);
  852. }
  853. /**
  854. * cpu_reset_interrupt:
  855. * @cpu: The CPU to clear the interrupt on.
  856. * @mask: The interrupt mask to clear.
  857. *
  858. * Resets interrupts on the vCPU @cpu.
  859. */
  860. void cpu_reset_interrupt(CPUState *cpu, int mask);
  861. /**
  862. * cpu_exit:
  863. * @cpu: The CPU to exit.
  864. *
  865. * Requests the CPU @cpu to exit execution.
  866. */
  867. void cpu_exit(CPUState *cpu);
  868. /**
  869. * cpu_pause:
  870. * @cpu: The CPU to pause.
  871. *
  872. * Pauses CPU, i.e. puts CPU into stopped state.
  873. */
  874. void cpu_pause(CPUState *cpu);
  875. /**
  876. * cpu_resume:
  877. * @cpu: The CPU to resume.
  878. *
  879. * Resumes CPU, i.e. puts CPU into runnable state.
  880. */
  881. void cpu_resume(CPUState *cpu);
  882. /**
  883. * cpu_remove_sync:
  884. * @cpu: The CPU to remove.
  885. *
  886. * Requests the CPU to be removed and waits till it is removed.
  887. */
  888. void cpu_remove_sync(CPUState *cpu);
  889. /**
  890. * free_queued_cpu_work() - free all items on CPU work queue
  891. * @cpu: The CPU which work queue to free.
  892. */
  893. void free_queued_cpu_work(CPUState *cpu);
  894. /**
  895. * process_queued_cpu_work() - process all items on CPU work queue
  896. * @cpu: The CPU which work queue to process.
  897. */
  898. void process_queued_cpu_work(CPUState *cpu);
  899. /**
  900. * cpu_exec_start:
  901. * @cpu: The CPU for the current thread.
  902. *
  903. * Record that a CPU has started execution and can be interrupted with
  904. * cpu_exit.
  905. */
  906. void cpu_exec_start(CPUState *cpu);
  907. /**
  908. * cpu_exec_end:
  909. * @cpu: The CPU for the current thread.
  910. *
  911. * Record that a CPU has stopped execution and exclusive sections
  912. * can be executed without interrupting it.
  913. */
  914. void cpu_exec_end(CPUState *cpu);
  915. /**
  916. * start_exclusive:
  917. *
  918. * Wait for a concurrent exclusive section to end, and then start
  919. * a section of work that is run while other CPUs are not running
  920. * between cpu_exec_start and cpu_exec_end. CPUs that are running
  921. * cpu_exec are exited immediately. CPUs that call cpu_exec_start
  922. * during the exclusive section go to sleep until this CPU calls
  923. * end_exclusive.
  924. */
  925. void start_exclusive(void);
  926. /**
  927. * end_exclusive:
  928. *
  929. * Concludes an exclusive execution section started by start_exclusive.
  930. */
  931. void end_exclusive(void);
  932. /**
  933. * qemu_init_vcpu:
  934. * @cpu: The vCPU to initialize.
  935. *
  936. * Initializes a vCPU.
  937. */
  938. void qemu_init_vcpu(CPUState *cpu);
  939. #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
  940. #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
  941. #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
  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. /* Breakpoint/watchpoint flags */
  951. #define BP_MEM_READ 0x01
  952. #define BP_MEM_WRITE 0x02
  953. #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
  954. #define BP_STOP_BEFORE_ACCESS 0x04
  955. /* 0x08 currently unused */
  956. #define BP_GDB 0x10
  957. #define BP_CPU 0x20
  958. #define BP_ANY (BP_GDB | BP_CPU)
  959. #define BP_HIT_SHIFT 6
  960. #define BP_WATCHPOINT_HIT_READ (BP_MEM_READ << BP_HIT_SHIFT)
  961. #define BP_WATCHPOINT_HIT_WRITE (BP_MEM_WRITE << BP_HIT_SHIFT)
  962. #define BP_WATCHPOINT_HIT (BP_MEM_ACCESS << BP_HIT_SHIFT)
  963. int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
  964. CPUBreakpoint **breakpoint);
  965. int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
  966. void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
  967. void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
  968. /* Return true if PC matches an installed breakpoint. */
  969. static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask)
  970. {
  971. CPUBreakpoint *bp;
  972. if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
  973. QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
  974. if (bp->pc == pc && (bp->flags & mask)) {
  975. return true;
  976. }
  977. }
  978. }
  979. return false;
  980. }
  981. #if defined(CONFIG_USER_ONLY)
  982. static inline int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
  983. int flags, CPUWatchpoint **watchpoint)
  984. {
  985. return -ENOSYS;
  986. }
  987. static inline int cpu_watchpoint_remove(CPUState *cpu, vaddr addr,
  988. vaddr len, int flags)
  989. {
  990. return -ENOSYS;
  991. }
  992. static inline void cpu_watchpoint_remove_by_ref(CPUState *cpu,
  993. CPUWatchpoint *wp)
  994. {
  995. }
  996. static inline void cpu_watchpoint_remove_all(CPUState *cpu, int mask)
  997. {
  998. }
  999. #else
  1000. int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
  1001. int flags, CPUWatchpoint **watchpoint);
  1002. int cpu_watchpoint_remove(CPUState *cpu, vaddr addr,
  1003. vaddr len, int flags);
  1004. void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint);
  1005. void cpu_watchpoint_remove_all(CPUState *cpu, int mask);
  1006. #endif
  1007. /**
  1008. * cpu_get_address_space:
  1009. * @cpu: CPU to get address space from
  1010. * @asidx: index identifying which address space to get
  1011. *
  1012. * Return the requested address space of this CPU. @asidx
  1013. * specifies which address space to read.
  1014. */
  1015. AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx);
  1016. G_NORETURN void cpu_abort(CPUState *cpu, const char *fmt, ...)
  1017. G_GNUC_PRINTF(2, 3);
  1018. /* $(top_srcdir)/cpu.c */
  1019. void cpu_class_init_props(DeviceClass *dc);
  1020. void cpu_exec_class_post_init(CPUClass *cc);
  1021. void cpu_exec_initfn(CPUState *cpu);
  1022. void cpu_vmstate_register(CPUState *cpu);
  1023. void cpu_vmstate_unregister(CPUState *cpu);
  1024. bool cpu_exec_realizefn(CPUState *cpu, Error **errp);
  1025. void cpu_exec_unrealizefn(CPUState *cpu);
  1026. void cpu_exec_reset_hold(CPUState *cpu);
  1027. const char *target_name(void);
  1028. #ifdef COMPILING_PER_TARGET
  1029. #ifndef CONFIG_USER_ONLY
  1030. extern const VMStateDescription vmstate_cpu_common;
  1031. #define VMSTATE_CPU() { \
  1032. .name = "parent_obj", \
  1033. .size = sizeof(CPUState), \
  1034. .vmsd = &vmstate_cpu_common, \
  1035. .flags = VMS_STRUCT, \
  1036. .offset = 0, \
  1037. }
  1038. #endif /* !CONFIG_USER_ONLY */
  1039. #endif /* COMPILING_PER_TARGET */
  1040. #define UNASSIGNED_CPU_INDEX -1
  1041. #define UNASSIGNED_CLUSTER_INDEX -1
  1042. #endif