cpu.h 36 KB

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