Merge tag 'v2.11.0-rc1'
[qemu/ar7.git] / include / exec / exec-all.h
blob720782f61de121f6812bd4cce154b4350aed91c5
1 /*
2 * internal execution defines for qemu
4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #ifndef EXEC_ALL_H
21 #define EXEC_ALL_H
23 #include "qemu-common.h"
24 #include "exec/tb-context.h"
25 #include "sysemu/cpus.h"
27 /* allow to see translation results - the slowdown should be negligible, so we leave it */
28 #define DEBUG_DISAS
30 /* Page tracking code uses ram addresses in system mode, and virtual
31 addresses in userspace mode. Define tb_page_addr_t to be an appropriate
32 type. */
33 #if defined(CONFIG_USER_ONLY)
34 typedef abi_ulong tb_page_addr_t;
35 #define TB_PAGE_ADDR_FMT TARGET_ABI_FMT_lx
36 #else
37 typedef ram_addr_t tb_page_addr_t;
38 #define TB_PAGE_ADDR_FMT RAM_ADDR_FMT
39 #endif
41 #include "qemu/log.h"
43 void gen_intermediate_code(CPUState *cpu, struct TranslationBlock *tb);
44 void restore_state_to_opc(CPUArchState *env, struct TranslationBlock *tb,
45 target_ulong *data);
47 /* Get a backtrace for the guest code. */
48 const char *qemu_sprint_backtrace(char *buffer, size_t length);
50 void cpu_gen_init(void);
52 /**
53 * cpu_restore_state:
54 * @cpu: the vCPU state is to be restore to
55 * @searched_pc: the host PC the fault occurred at
56 * @return: true if state was restored, false otherwise
58 * Attempt to restore the state for a fault occurring in translated
59 * code. If the searched_pc is not in translated code no state is
60 * restored and the function returns false.
62 bool cpu_restore_state(CPUState *cpu, uintptr_t searched_pc);
64 void QEMU_NORETURN cpu_loop_exit_noexc(CPUState *cpu);
65 void QEMU_NORETURN cpu_io_recompile(CPUState *cpu, uintptr_t retaddr);
66 TranslationBlock *tb_gen_code(CPUState *cpu,
67 target_ulong pc, target_ulong cs_base,
68 uint32_t flags,
69 int cflags);
71 void QEMU_NORETURN cpu_loop_exit(CPUState *cpu);
72 void QEMU_NORETURN cpu_loop_exit_restore(CPUState *cpu, uintptr_t pc);
73 void QEMU_NORETURN cpu_loop_exit_atomic(CPUState *cpu, uintptr_t pc);
75 #if !defined(CONFIG_USER_ONLY)
76 void cpu_reloading_memory_map(void);
77 /**
78 * cpu_address_space_init:
79 * @cpu: CPU to add this address space to
80 * @as: address space to add
81 * @asidx: integer index of this address space
83 * Add the specified address space to the CPU's cpu_ases list.
84 * The address space added with @asidx 0 is the one used for the
85 * convenience pointer cpu->as.
86 * The target-specific code which registers ASes is responsible
87 * for defining what semantics address space 0, 1, 2, etc have.
89 * Before the first call to this function, the caller must set
90 * cpu->num_ases to the total number of address spaces it needs
91 * to support.
93 * Note that with KVM only one address space is supported.
95 void cpu_address_space_init(CPUState *cpu, AddressSpace *as, int asidx);
96 #endif
98 #if !defined(CONFIG_USER_ONLY) && defined(CONFIG_TCG)
99 /* cputlb.c */
101 * tlb_flush_page:
102 * @cpu: CPU whose TLB should be flushed
103 * @addr: virtual address of page to be flushed
105 * Flush one page from the TLB of the specified CPU, for all
106 * MMU indexes.
108 void tlb_flush_page(CPUState *cpu, target_ulong addr);
110 * tlb_flush_page_all_cpus:
111 * @cpu: src CPU of the flush
112 * @addr: virtual address of page to be flushed
114 * Flush one page from the TLB of the specified CPU, for all
115 * MMU indexes.
117 void tlb_flush_page_all_cpus(CPUState *src, target_ulong addr);
119 * tlb_flush_page_all_cpus_synced:
120 * @cpu: src CPU of the flush
121 * @addr: virtual address of page to be flushed
123 * Flush one page from the TLB of the specified CPU, for all MMU
124 * indexes like tlb_flush_page_all_cpus except the source vCPUs work
125 * is scheduled as safe work meaning all flushes will be complete once
126 * the source vCPUs safe work is complete. This will depend on when
127 * the guests translation ends the TB.
129 void tlb_flush_page_all_cpus_synced(CPUState *src, target_ulong addr);
131 * tlb_flush:
132 * @cpu: CPU whose TLB should be flushed
134 * Flush the entire TLB for the specified CPU. Most CPU architectures
135 * allow the implementation to drop entries from the TLB at any time
136 * so this is generally safe. If more selective flushing is required
137 * use one of the other functions for efficiency.
139 void tlb_flush(CPUState *cpu);
141 * tlb_flush_all_cpus:
142 * @cpu: src CPU of the flush
144 void tlb_flush_all_cpus(CPUState *src_cpu);
146 * tlb_flush_all_cpus_synced:
147 * @cpu: src CPU of the flush
149 * Like tlb_flush_all_cpus except this except the source vCPUs work is
150 * scheduled as safe work meaning all flushes will be complete once
151 * the source vCPUs safe work is complete. This will depend on when
152 * the guests translation ends the TB.
154 void tlb_flush_all_cpus_synced(CPUState *src_cpu);
156 * tlb_flush_page_by_mmuidx:
157 * @cpu: CPU whose TLB should be flushed
158 * @addr: virtual address of page to be flushed
159 * @idxmap: bitmap of MMU indexes to flush
161 * Flush one page from the TLB of the specified CPU, for the specified
162 * MMU indexes.
164 void tlb_flush_page_by_mmuidx(CPUState *cpu, target_ulong addr,
165 uint16_t idxmap);
167 * tlb_flush_page_by_mmuidx_all_cpus:
168 * @cpu: Originating CPU of the flush
169 * @addr: virtual address of page to be flushed
170 * @idxmap: bitmap of MMU indexes to flush
172 * Flush one page from the TLB of all CPUs, for the specified
173 * MMU indexes.
175 void tlb_flush_page_by_mmuidx_all_cpus(CPUState *cpu, target_ulong addr,
176 uint16_t idxmap);
178 * tlb_flush_page_by_mmuidx_all_cpus_synced:
179 * @cpu: Originating CPU of the flush
180 * @addr: virtual address of page to be flushed
181 * @idxmap: bitmap of MMU indexes to flush
183 * Flush one page from the TLB of all CPUs, for the specified MMU
184 * indexes like tlb_flush_page_by_mmuidx_all_cpus except the source
185 * vCPUs work is scheduled as safe work meaning all flushes will be
186 * complete once the source vCPUs safe work is complete. This will
187 * depend on when the guests translation ends the TB.
189 void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *cpu, target_ulong addr,
190 uint16_t idxmap);
192 * tlb_flush_by_mmuidx:
193 * @cpu: CPU whose TLB should be flushed
194 * @wait: If true ensure synchronisation by exiting the cpu_loop
195 * @idxmap: bitmap of MMU indexes to flush
197 * Flush all entries from the TLB of the specified CPU, for the specified
198 * MMU indexes.
200 void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap);
202 * tlb_flush_by_mmuidx_all_cpus:
203 * @cpu: Originating CPU of the flush
204 * @idxmap: bitmap of MMU indexes to flush
206 * Flush all entries from all TLBs of all CPUs, for the specified
207 * MMU indexes.
209 void tlb_flush_by_mmuidx_all_cpus(CPUState *cpu, uint16_t idxmap);
211 * tlb_flush_by_mmuidx_all_cpus_synced:
212 * @cpu: Originating CPU of the flush
213 * @idxmap: bitmap of MMU indexes to flush
215 * Flush all entries from all TLBs of all CPUs, for the specified
216 * MMU indexes like tlb_flush_by_mmuidx_all_cpus except except the source
217 * vCPUs work is scheduled as safe work meaning all flushes will be
218 * complete once the source vCPUs safe work is complete. This will
219 * depend on when the guests translation ends the TB.
221 void tlb_flush_by_mmuidx_all_cpus_synced(CPUState *cpu, uint16_t idxmap);
223 * tlb_set_page_with_attrs:
224 * @cpu: CPU to add this TLB entry for
225 * @vaddr: virtual address of page to add entry for
226 * @paddr: physical address of the page
227 * @attrs: memory transaction attributes
228 * @prot: access permissions (PAGE_READ/PAGE_WRITE/PAGE_EXEC bits)
229 * @mmu_idx: MMU index to insert TLB entry for
230 * @size: size of the page in bytes
232 * Add an entry to this CPU's TLB (a mapping from virtual address
233 * @vaddr to physical address @paddr) with the specified memory
234 * transaction attributes. This is generally called by the target CPU
235 * specific code after it has been called through the tlb_fill()
236 * entry point and performed a successful page table walk to find
237 * the physical address and attributes for the virtual address
238 * which provoked the TLB miss.
240 * At most one entry for a given virtual address is permitted. Only a
241 * single TARGET_PAGE_SIZE region is mapped; the supplied @size is only
242 * used by tlb_flush_page.
244 void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
245 hwaddr paddr, MemTxAttrs attrs,
246 int prot, int mmu_idx, target_ulong size);
247 /* tlb_set_page:
249 * This function is equivalent to calling tlb_set_page_with_attrs()
250 * with an @attrs argument of MEMTXATTRS_UNSPECIFIED. It's provided
251 * as a convenience for CPUs which don't use memory transaction attributes.
253 void tlb_set_page(CPUState *cpu, target_ulong vaddr,
254 hwaddr paddr, int prot,
255 int mmu_idx, target_ulong size);
256 void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr);
257 void probe_write(CPUArchState *env, target_ulong addr, int mmu_idx,
258 uintptr_t retaddr);
259 #else
260 static inline void tlb_flush_page(CPUState *cpu, target_ulong addr)
263 static inline void tlb_flush_page_all_cpus(CPUState *src, target_ulong addr)
266 static inline void tlb_flush_page_all_cpus_synced(CPUState *src,
267 target_ulong addr)
270 static inline void tlb_flush(CPUState *cpu)
273 static inline void tlb_flush_all_cpus(CPUState *src_cpu)
276 static inline void tlb_flush_all_cpus_synced(CPUState *src_cpu)
279 static inline void tlb_flush_page_by_mmuidx(CPUState *cpu,
280 target_ulong addr, uint16_t idxmap)
284 static inline void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap)
287 static inline void tlb_flush_page_by_mmuidx_all_cpus(CPUState *cpu,
288 target_ulong addr,
289 uint16_t idxmap)
292 static inline void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *cpu,
293 target_ulong addr,
294 uint16_t idxmap)
297 static inline void tlb_flush_by_mmuidx_all_cpus(CPUState *cpu, uint16_t idxmap)
300 static inline void tlb_flush_by_mmuidx_all_cpus_synced(CPUState *cpu,
301 uint16_t idxmap)
304 static inline void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr)
307 #endif
309 #define CODE_GEN_ALIGN 16 /* must be >= of the size of a icache line */
311 /* Estimated block size for TB allocation. */
312 /* ??? The following is based on a 2015 survey of x86_64 host output.
313 Better would seem to be some sort of dynamically sized TB array,
314 adapting to the block sizes actually being produced. */
315 #if defined(CONFIG_SOFTMMU)
316 #define CODE_GEN_AVG_BLOCK_SIZE 400
317 #else
318 #define CODE_GEN_AVG_BLOCK_SIZE 150
319 #endif
322 * Translation Cache-related fields of a TB.
323 * This struct exists just for convenience; we keep track of TB's in a binary
324 * search tree, and the only fields needed to compare TB's in the tree are
325 * @ptr and @size.
326 * Note: the address of search data can be obtained by adding @size to @ptr.
328 struct tb_tc {
329 void *ptr; /* pointer to the translated code */
330 size_t size;
333 struct TranslationBlock {
334 target_ulong pc; /* simulated PC corresponding to this block (EIP + CS base) */
335 target_ulong cs_base; /* CS base for this block */
336 uint32_t flags; /* flags defining in which context the code was generated */
337 uint16_t size; /* size of target code for this block (1 <=
338 size <= TARGET_PAGE_SIZE) */
339 uint16_t icount;
340 uint32_t cflags; /* compile flags */
341 #define CF_COUNT_MASK 0x00007fff
342 #define CF_LAST_IO 0x00008000 /* Last insn may be an IO access. */
343 #define CF_NOCACHE 0x00010000 /* To be freed after execution */
344 #define CF_USE_ICOUNT 0x00020000
345 #define CF_INVALID 0x00040000 /* TB is stale. Setters need tb_lock */
346 #define CF_PARALLEL 0x00080000 /* Generate code for a parallel context */
347 /* cflags' mask for hashing/comparison */
348 #define CF_HASH_MASK \
349 (CF_COUNT_MASK | CF_LAST_IO | CF_USE_ICOUNT | CF_PARALLEL)
351 /* Per-vCPU dynamic tracing state used to generate this TB */
352 uint32_t trace_vcpu_dstate;
354 struct tb_tc tc;
356 /* original tb when cflags has CF_NOCACHE */
357 struct TranslationBlock *orig_tb;
358 /* first and second physical page containing code. The lower bit
359 of the pointer tells the index in page_next[] */
360 struct TranslationBlock *page_next[2];
361 tb_page_addr_t page_addr[2];
363 /* The following data are used to directly call another TB from
364 * the code of this one. This can be done either by emitting direct or
365 * indirect native jump instructions. These jumps are reset so that the TB
366 * just continues its execution. The TB can be linked to another one by
367 * setting one of the jump targets (or patching the jump instruction). Only
368 * two of such jumps are supported.
370 uint16_t jmp_reset_offset[2]; /* offset of original jump target */
371 #define TB_JMP_RESET_OFFSET_INVALID 0xffff /* indicates no jump generated */
372 uintptr_t jmp_target_arg[2]; /* target address or offset */
374 /* Each TB has an associated circular list of TBs jumping to this one.
375 * jmp_list_first points to the first TB jumping to this one.
376 * jmp_list_next is used to point to the next TB in a list.
377 * Since each TB can have two jumps, it can participate in two lists.
378 * jmp_list_first and jmp_list_next are 4-byte aligned pointers to a
379 * TranslationBlock structure, but the two least significant bits of
380 * them are used to encode which data field of the pointed TB should
381 * be used to traverse the list further from that TB:
382 * 0 => jmp_list_next[0], 1 => jmp_list_next[1], 2 => jmp_list_first.
383 * In other words, 0/1 tells which jump is used in the pointed TB,
384 * and 2 means that this is a pointer back to the target TB of this list.
386 uintptr_t jmp_list_next[2];
387 uintptr_t jmp_list_first;
390 extern bool parallel_cpus;
392 /* Hide the atomic_read to make code a little easier on the eyes */
393 static inline uint32_t tb_cflags(const TranslationBlock *tb)
395 return atomic_read(&tb->cflags);
398 /* current cflags for hashing/comparison */
399 static inline uint32_t curr_cflags(void)
401 return (parallel_cpus ? CF_PARALLEL : 0)
402 | (use_icount ? CF_USE_ICOUNT : 0);
405 void tb_remove(TranslationBlock *tb);
406 void tb_flush(CPUState *cpu);
407 void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr);
408 TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc,
409 target_ulong cs_base, uint32_t flags,
410 uint32_t cf_mask);
411 void tb_set_jmp_target(TranslationBlock *tb, int n, uintptr_t addr);
413 /* GETPC is the true target of the return instruction that we'll execute. */
414 #if defined(CONFIG_TCG_INTERPRETER)
415 extern uintptr_t tci_tb_ptr;
416 # define GETPC() tci_tb_ptr
417 #else
418 # define GETPC() \
419 ((uintptr_t)__builtin_extract_return_addr(__builtin_return_address(0)))
420 #endif
422 /* The true return address will often point to a host insn that is part of
423 the next translated guest insn. Adjust the address backward to point to
424 the middle of the call insn. Subtracting one would do the job except for
425 several compressed mode architectures (arm, mips) which set the low bit
426 to indicate the compressed mode; subtracting two works around that. It
427 is also the case that there are no host isas that contain a call insn
428 smaller than 4 bytes, so we don't worry about special-casing this. */
429 #define GETPC_ADJ 2
431 void tb_lock(void);
432 void tb_unlock(void);
433 void tb_lock_reset(void);
435 #if !defined(CONFIG_USER_ONLY)
437 struct MemoryRegion *iotlb_to_region(CPUState *cpu,
438 hwaddr index, MemTxAttrs attrs);
440 void tlb_fill(CPUState *cpu, target_ulong addr, MMUAccessType access_type,
441 int mmu_idx, uintptr_t retaddr);
443 #endif
445 #if defined(CONFIG_USER_ONLY)
446 void mmap_lock(void);
447 void mmap_unlock(void);
448 bool have_mmap_lock(void);
450 static inline tb_page_addr_t get_page_addr_code(CPUArchState *env1, target_ulong addr)
452 return addr;
454 #else
455 static inline void mmap_lock(void) {}
456 static inline void mmap_unlock(void) {}
458 /* cputlb.c */
459 tb_page_addr_t get_page_addr_code(CPUArchState *env1, target_ulong addr);
461 void tlb_reset_dirty(CPUState *cpu, ram_addr_t start1, ram_addr_t length);
462 void tlb_set_dirty(CPUState *cpu, target_ulong vaddr);
464 /* exec.c */
465 void tb_flush_jmp_cache(CPUState *cpu, target_ulong addr);
467 MemoryRegionSection *
468 address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr,
469 hwaddr *xlat, hwaddr *plen);
470 hwaddr memory_region_section_get_iotlb(CPUState *cpu,
471 MemoryRegionSection *section,
472 target_ulong vaddr,
473 hwaddr paddr, hwaddr xlat,
474 int prot,
475 target_ulong *address);
476 bool memory_region_is_unassigned(MemoryRegion *mr);
478 #endif
480 /* vl.c */
481 extern int singlestep;
483 #endif