cpu.h 36 KB

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