Merge tag 'v2.10.0-rc0'
[qemu/ar7.git] / include / exec / exec-all.h
blob75cd1f6380c2d68d3f4b49d95add6958ca01f328
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"
26 /* allow to see translation results - the slowdown should be negligible, so we leave it */
27 #define DEBUG_DISAS
29 /* Page tracking code uses ram addresses in system mode, and virtual
30 addresses in userspace mode. Define tb_page_addr_t to be an appropriate
31 type. */
32 #if defined(CONFIG_USER_ONLY)
33 typedef abi_ulong tb_page_addr_t;
34 #else
35 typedef ram_addr_t tb_page_addr_t;
36 #endif
38 /* DisasContext is_jmp field values
40 * is_jmp starts as DISAS_NEXT. The translator will keep processing
41 * instructions until an exit condition is reached. If we reach the
42 * exit condition and is_jmp is still DISAS_NEXT (because of some
43 * other condition) we simply "jump" to the next address.
44 * The remaining exit cases are:
46 * DISAS_JUMP - Only the PC was modified dynamically (e.g computed)
47 * DISAS_TB_JUMP - Only the PC was modified statically (e.g. branch)
49 * In these cases as long as the PC is updated we can chain to the
50 * next TB either by exiting the loop or looking up the next TB via
51 * the loookup helper.
53 * DISAS_UPDATE - CPU State was modified dynamically
55 * This covers any other CPU state which necessities us exiting the
56 * TCG code to the main run-loop. Typically this includes anything
57 * that might change the interrupt state.
59 * Individual translators may define additional exit cases to deal
60 * with per-target special conditions.
62 #define DISAS_NEXT 0 /* next instruction can be analyzed */
63 #define DISAS_JUMP 1 /* only pc was modified dynamically */
64 #define DISAS_TB_JUMP 2 /* only pc was modified statically */
65 #define DISAS_UPDATE 3 /* cpu state was modified dynamically */
67 #include "qemu/log.h"
69 void gen_intermediate_code(CPUState *cpu, struct TranslationBlock *tb);
70 void restore_state_to_opc(CPUArchState *env, struct TranslationBlock *tb,
71 target_ulong *data);
73 /* Get a backtrace for the guest code. */
74 const char *qemu_sprint_backtrace(char *buffer, size_t length);
76 void cpu_gen_init(void);
77 bool cpu_restore_state(CPUState *cpu, uintptr_t searched_pc);
79 void QEMU_NORETURN cpu_loop_exit_noexc(CPUState *cpu);
80 void QEMU_NORETURN cpu_io_recompile(CPUState *cpu, uintptr_t retaddr);
81 TranslationBlock *tb_gen_code(CPUState *cpu,
82 target_ulong pc, target_ulong cs_base,
83 uint32_t flags,
84 int cflags);
86 void QEMU_NORETURN cpu_loop_exit(CPUState *cpu);
87 void QEMU_NORETURN cpu_loop_exit_restore(CPUState *cpu, uintptr_t pc);
88 void QEMU_NORETURN cpu_loop_exit_atomic(CPUState *cpu, uintptr_t pc);
90 #if !defined(CONFIG_USER_ONLY)
91 void cpu_reloading_memory_map(void);
92 /**
93 * cpu_address_space_init:
94 * @cpu: CPU to add this address space to
95 * @as: address space to add
96 * @asidx: integer index of this address space
98 * Add the specified address space to the CPU's cpu_ases list.
99 * The address space added with @asidx 0 is the one used for the
100 * convenience pointer cpu->as.
101 * The target-specific code which registers ASes is responsible
102 * for defining what semantics address space 0, 1, 2, etc have.
104 * Before the first call to this function, the caller must set
105 * cpu->num_ases to the total number of address spaces it needs
106 * to support.
108 * Note that with KVM only one address space is supported.
110 void cpu_address_space_init(CPUState *cpu, AddressSpace *as, int asidx);
111 #endif
113 #if !defined(CONFIG_USER_ONLY) && defined(CONFIG_TCG)
114 /* cputlb.c */
116 * tlb_flush_page:
117 * @cpu: CPU whose TLB should be flushed
118 * @addr: virtual address of page to be flushed
120 * Flush one page from the TLB of the specified CPU, for all
121 * MMU indexes.
123 void tlb_flush_page(CPUState *cpu, target_ulong addr);
125 * tlb_flush_page_all_cpus:
126 * @cpu: src CPU of the flush
127 * @addr: virtual address of page to be flushed
129 * Flush one page from the TLB of the specified CPU, for all
130 * MMU indexes.
132 void tlb_flush_page_all_cpus(CPUState *src, target_ulong addr);
134 * tlb_flush_page_all_cpus_synced:
135 * @cpu: src CPU of the flush
136 * @addr: virtual address of page to be flushed
138 * Flush one page from the TLB of the specified CPU, for all MMU
139 * indexes like tlb_flush_page_all_cpus except the source vCPUs work
140 * is scheduled as safe work meaning all flushes will be complete once
141 * the source vCPUs safe work is complete. This will depend on when
142 * the guests translation ends the TB.
144 void tlb_flush_page_all_cpus_synced(CPUState *src, target_ulong addr);
146 * tlb_flush:
147 * @cpu: CPU whose TLB should be flushed
149 * Flush the entire TLB for the specified CPU. Most CPU architectures
150 * allow the implementation to drop entries from the TLB at any time
151 * so this is generally safe. If more selective flushing is required
152 * use one of the other functions for efficiency.
154 void tlb_flush(CPUState *cpu);
156 * tlb_flush_all_cpus:
157 * @cpu: src CPU of the flush
159 void tlb_flush_all_cpus(CPUState *src_cpu);
161 * tlb_flush_all_cpus_synced:
162 * @cpu: src CPU of the flush
164 * Like tlb_flush_all_cpus except this except the source vCPUs work is
165 * scheduled as safe work meaning all flushes will be complete once
166 * the source vCPUs safe work is complete. This will depend on when
167 * the guests translation ends the TB.
169 void tlb_flush_all_cpus_synced(CPUState *src_cpu);
171 * tlb_flush_page_by_mmuidx:
172 * @cpu: CPU whose TLB should be flushed
173 * @addr: virtual address of page to be flushed
174 * @idxmap: bitmap of MMU indexes to flush
176 * Flush one page from the TLB of the specified CPU, for the specified
177 * MMU indexes.
179 void tlb_flush_page_by_mmuidx(CPUState *cpu, target_ulong addr,
180 uint16_t idxmap);
182 * tlb_flush_page_by_mmuidx_all_cpus:
183 * @cpu: Originating CPU of the flush
184 * @addr: virtual address of page to be flushed
185 * @idxmap: bitmap of MMU indexes to flush
187 * Flush one page from the TLB of all CPUs, for the specified
188 * MMU indexes.
190 void tlb_flush_page_by_mmuidx_all_cpus(CPUState *cpu, target_ulong addr,
191 uint16_t idxmap);
193 * tlb_flush_page_by_mmuidx_all_cpus_synced:
194 * @cpu: Originating CPU of the flush
195 * @addr: virtual address of page to be flushed
196 * @idxmap: bitmap of MMU indexes to flush
198 * Flush one page from the TLB of all CPUs, for the specified MMU
199 * indexes like tlb_flush_page_by_mmuidx_all_cpus except the source
200 * vCPUs work is scheduled as safe work meaning all flushes will be
201 * complete once the source vCPUs safe work is complete. This will
202 * depend on when the guests translation ends the TB.
204 void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *cpu, target_ulong addr,
205 uint16_t idxmap);
207 * tlb_flush_by_mmuidx:
208 * @cpu: CPU whose TLB should be flushed
209 * @wait: If true ensure synchronisation by exiting the cpu_loop
210 * @idxmap: bitmap of MMU indexes to flush
212 * Flush all entries from the TLB of the specified CPU, for the specified
213 * MMU indexes.
215 void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap);
217 * tlb_flush_by_mmuidx_all_cpus:
218 * @cpu: Originating CPU of the flush
219 * @idxmap: bitmap of MMU indexes to flush
221 * Flush all entries from all TLBs of all CPUs, for the specified
222 * MMU indexes.
224 void tlb_flush_by_mmuidx_all_cpus(CPUState *cpu, uint16_t idxmap);
226 * tlb_flush_by_mmuidx_all_cpus_synced:
227 * @cpu: Originating CPU of the flush
228 * @idxmap: bitmap of MMU indexes to flush
230 * Flush all entries from all TLBs of all CPUs, for the specified
231 * MMU indexes like tlb_flush_by_mmuidx_all_cpus except except the source
232 * vCPUs work is scheduled as safe work meaning all flushes will be
233 * complete once the source vCPUs safe work is complete. This will
234 * depend on when the guests translation ends the TB.
236 void tlb_flush_by_mmuidx_all_cpus_synced(CPUState *cpu, uint16_t idxmap);
238 * tlb_set_page_with_attrs:
239 * @cpu: CPU to add this TLB entry for
240 * @vaddr: virtual address of page to add entry for
241 * @paddr: physical address of the page
242 * @attrs: memory transaction attributes
243 * @prot: access permissions (PAGE_READ/PAGE_WRITE/PAGE_EXEC bits)
244 * @mmu_idx: MMU index to insert TLB entry for
245 * @size: size of the page in bytes
247 * Add an entry to this CPU's TLB (a mapping from virtual address
248 * @vaddr to physical address @paddr) with the specified memory
249 * transaction attributes. This is generally called by the target CPU
250 * specific code after it has been called through the tlb_fill()
251 * entry point and performed a successful page table walk to find
252 * the physical address and attributes for the virtual address
253 * which provoked the TLB miss.
255 * At most one entry for a given virtual address is permitted. Only a
256 * single TARGET_PAGE_SIZE region is mapped; the supplied @size is only
257 * used by tlb_flush_page.
259 void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
260 hwaddr paddr, MemTxAttrs attrs,
261 int prot, int mmu_idx, target_ulong size);
262 /* tlb_set_page:
264 * This function is equivalent to calling tlb_set_page_with_attrs()
265 * with an @attrs argument of MEMTXATTRS_UNSPECIFIED. It's provided
266 * as a convenience for CPUs which don't use memory transaction attributes.
268 void tlb_set_page(CPUState *cpu, target_ulong vaddr,
269 hwaddr paddr, int prot,
270 int mmu_idx, target_ulong size);
271 void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr);
272 void probe_write(CPUArchState *env, target_ulong addr, int mmu_idx,
273 uintptr_t retaddr);
274 #else
275 static inline void tlb_flush_page(CPUState *cpu, target_ulong addr)
278 static inline void tlb_flush_page_all_cpus(CPUState *src, target_ulong addr)
281 static inline void tlb_flush_page_all_cpus_synced(CPUState *src,
282 target_ulong addr)
285 static inline void tlb_flush(CPUState *cpu)
288 static inline void tlb_flush_all_cpus(CPUState *src_cpu)
291 static inline void tlb_flush_all_cpus_synced(CPUState *src_cpu)
294 static inline void tlb_flush_page_by_mmuidx(CPUState *cpu,
295 target_ulong addr, uint16_t idxmap)
299 static inline void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap)
302 static inline void tlb_flush_page_by_mmuidx_all_cpus(CPUState *cpu,
303 target_ulong addr,
304 uint16_t idxmap)
307 static inline void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *cpu,
308 target_ulong addr,
309 uint16_t idxmap)
312 static inline void tlb_flush_by_mmuidx_all_cpus(CPUState *cpu, uint16_t idxmap)
315 static inline void tlb_flush_by_mmuidx_all_cpus_synced(CPUState *cpu,
316 uint16_t idxmap)
319 static inline void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr)
322 #endif
324 #define CODE_GEN_ALIGN 16 /* must be >= of the size of a icache line */
326 /* Estimated block size for TB allocation. */
327 /* ??? The following is based on a 2015 survey of x86_64 host output.
328 Better would seem to be some sort of dynamically sized TB array,
329 adapting to the block sizes actually being produced. */
330 #if defined(CONFIG_SOFTMMU)
331 #define CODE_GEN_AVG_BLOCK_SIZE 400
332 #else
333 #define CODE_GEN_AVG_BLOCK_SIZE 150
334 #endif
336 #if defined(_ARCH_PPC) \
337 || defined(__x86_64__) || defined(__i386__) \
338 || defined(__sparc__) || defined(__aarch64__) \
339 || defined(__s390x__) || defined(__mips__) \
340 || defined(CONFIG_TCG_INTERPRETER)
341 /* NOTE: Direct jump patching must be atomic to be thread-safe. */
342 #define USE_DIRECT_JUMP
343 #endif
345 struct TranslationBlock {
346 target_ulong pc; /* simulated PC corresponding to this block (EIP + CS base) */
347 target_ulong cs_base; /* CS base for this block */
348 uint32_t flags; /* flags defining in which context the code was generated */
349 uint16_t size; /* size of target code for this block (1 <=
350 size <= TARGET_PAGE_SIZE) */
351 uint16_t icount;
352 uint32_t cflags; /* compile flags */
353 #define CF_COUNT_MASK 0x7fff
354 #define CF_LAST_IO 0x8000 /* Last insn may be an IO access. */
355 #define CF_NOCACHE 0x10000 /* To be freed after execution */
356 #define CF_USE_ICOUNT 0x20000
357 #define CF_IGNORE_ICOUNT 0x40000 /* Do not generate icount code */
359 /* Per-vCPU dynamic tracing state used to generate this TB */
360 uint32_t trace_vcpu_dstate;
362 uint16_t invalid;
364 void *tc_ptr; /* pointer to the translated code */
365 uint8_t *tc_search; /* pointer to search data */
366 /* original tb when cflags has CF_NOCACHE */
367 struct TranslationBlock *orig_tb;
368 /* first and second physical page containing code. The lower bit
369 of the pointer tells the index in page_next[] */
370 struct TranslationBlock *page_next[2];
371 tb_page_addr_t page_addr[2];
373 /* The following data are used to directly call another TB from
374 * the code of this one. This can be done either by emitting direct or
375 * indirect native jump instructions. These jumps are reset so that the TB
376 * just continue its execution. The TB can be linked to another one by
377 * setting one of the jump targets (or patching the jump instruction). Only
378 * two of such jumps are supported.
380 uint16_t jmp_reset_offset[2]; /* offset of original jump target */
381 #define TB_JMP_RESET_OFFSET_INVALID 0xffff /* indicates no jump generated */
382 #ifdef USE_DIRECT_JUMP
383 uint16_t jmp_insn_offset[2]; /* offset of native jump instruction */
384 #else
385 uintptr_t jmp_target_addr[2]; /* target address for indirect jump */
386 #endif
387 /* Each TB has an assosiated circular list of TBs jumping to this one.
388 * jmp_list_first points to the first TB jumping to this one.
389 * jmp_list_next is used to point to the next TB in a list.
390 * Since each TB can have two jumps, it can participate in two lists.
391 * jmp_list_first and jmp_list_next are 4-byte aligned pointers to a
392 * TranslationBlock structure, but the two least significant bits of
393 * them are used to encode which data field of the pointed TB should
394 * be used to traverse the list further from that TB:
395 * 0 => jmp_list_next[0], 1 => jmp_list_next[1], 2 => jmp_list_first.
396 * In other words, 0/1 tells which jump is used in the pointed TB,
397 * and 2 means that this is a pointer back to the target TB of this list.
399 uintptr_t jmp_list_next[2];
400 uintptr_t jmp_list_first;
403 void tb_free(TranslationBlock *tb);
404 void tb_flush(CPUState *cpu);
405 void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr);
406 TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc,
407 target_ulong cs_base, uint32_t flags);
409 #if defined(USE_DIRECT_JUMP)
411 #if defined(CONFIG_TCG_INTERPRETER)
412 static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
414 /* patch the branch destination */
415 atomic_set((int32_t *)jmp_addr, addr - (jmp_addr + 4));
416 /* no need to flush icache explicitly */
418 #elif defined(_ARCH_PPC)
419 void ppc_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr);
420 #define tb_set_jmp_target1 ppc_tb_set_jmp_target
421 #elif defined(__i386__) || defined(__x86_64__)
422 static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
424 /* patch the branch destination */
425 atomic_set((int32_t *)jmp_addr, addr - (jmp_addr + 4));
426 /* no need to flush icache explicitly */
428 #elif defined(__s390x__)
429 static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
431 /* patch the branch destination */
432 intptr_t disp = addr - (jmp_addr - 2);
433 atomic_set((int32_t *)jmp_addr, disp / 2);
434 /* no need to flush icache explicitly */
436 #elif defined(__aarch64__)
437 void aarch64_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr);
438 #define tb_set_jmp_target1 aarch64_tb_set_jmp_target
439 #elif defined(__sparc__) || defined(__mips__)
440 void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr);
441 #else
442 #error tb_set_jmp_target1 is missing
443 #endif
445 static inline void tb_set_jmp_target(TranslationBlock *tb,
446 int n, uintptr_t addr)
448 uint16_t offset = tb->jmp_insn_offset[n];
449 tb_set_jmp_target1((uintptr_t)(tb->tc_ptr + offset), addr);
452 #else
454 /* set the jump target */
455 static inline void tb_set_jmp_target(TranslationBlock *tb,
456 int n, uintptr_t addr)
458 tb->jmp_target_addr[n] = addr;
461 #endif
463 /* Called with tb_lock held. */
464 static inline void tb_add_jump(TranslationBlock *tb, int n,
465 TranslationBlock *tb_next)
467 assert(n < ARRAY_SIZE(tb->jmp_list_next));
468 if (tb->jmp_list_next[n]) {
469 /* Another thread has already done this while we were
470 * outside of the lock; nothing to do in this case */
471 return;
473 qemu_log_mask_and_addr(CPU_LOG_EXEC, tb->pc,
474 "Linking TBs %p [" TARGET_FMT_lx
475 "] index %d -> %p [" TARGET_FMT_lx "]\n",
476 tb->tc_ptr, tb->pc, n,
477 tb_next->tc_ptr, tb_next->pc);
479 /* patch the native jump address */
480 tb_set_jmp_target(tb, n, (uintptr_t)tb_next->tc_ptr);
482 /* add in TB jmp circular list */
483 tb->jmp_list_next[n] = tb_next->jmp_list_first;
484 tb_next->jmp_list_first = (uintptr_t)tb | n;
487 /* GETPC is the true target of the return instruction that we'll execute. */
488 #if defined(CONFIG_TCG_INTERPRETER)
489 extern uintptr_t tci_tb_ptr;
490 # define GETPC() tci_tb_ptr
491 #else
492 # define GETPC() \
493 ((uintptr_t)__builtin_extract_return_addr(__builtin_return_address(0)))
494 #endif
496 /* The true return address will often point to a host insn that is part of
497 the next translated guest insn. Adjust the address backward to point to
498 the middle of the call insn. Subtracting one would do the job except for
499 several compressed mode architectures (arm, mips) which set the low bit
500 to indicate the compressed mode; subtracting two works around that. It
501 is also the case that there are no host isas that contain a call insn
502 smaller than 4 bytes, so we don't worry about special-casing this. */
503 #define GETPC_ADJ 2
505 void tb_lock(void);
506 void tb_unlock(void);
507 void tb_lock_reset(void);
509 #if !defined(CONFIG_USER_ONLY)
511 struct MemoryRegion *iotlb_to_region(CPUState *cpu,
512 hwaddr index, MemTxAttrs attrs);
514 void tlb_fill(CPUState *cpu, target_ulong addr, MMUAccessType access_type,
515 int mmu_idx, uintptr_t retaddr);
517 #endif
519 #if defined(CONFIG_USER_ONLY)
520 void mmap_lock(void);
521 void mmap_unlock(void);
522 bool have_mmap_lock(void);
524 static inline tb_page_addr_t get_page_addr_code(CPUArchState *env1, target_ulong addr)
526 return addr;
528 #else
529 static inline void mmap_lock(void) {}
530 static inline void mmap_unlock(void) {}
532 /* cputlb.c */
533 tb_page_addr_t get_page_addr_code(CPUArchState *env1, target_ulong addr);
535 void tlb_reset_dirty(CPUState *cpu, ram_addr_t start1, ram_addr_t length);
536 void tlb_set_dirty(CPUState *cpu, target_ulong vaddr);
538 /* exec.c */
539 void tb_flush_jmp_cache(CPUState *cpu, target_ulong addr);
541 MemoryRegionSection *
542 address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr,
543 hwaddr *xlat, hwaddr *plen);
544 hwaddr memory_region_section_get_iotlb(CPUState *cpu,
545 MemoryRegionSection *section,
546 target_ulong vaddr,
547 hwaddr paddr, hwaddr xlat,
548 int prot,
549 target_ulong *address);
550 bool memory_region_is_unassigned(MemoryRegion *mr);
552 #endif
554 /* vl.c */
555 extern int singlestep;
557 #endif