tcg: Add generic DISAS_NORETURN
[qemu/ar7.git] / include / exec / exec-all.h
blobb43498897979be54d3ecd798271c375621d570f3
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 */
66 #define DISAS_NORETURN 4 /* the tb has already been exited */
68 #include "qemu/log.h"
70 void gen_intermediate_code(CPUState *cpu, struct TranslationBlock *tb);
71 void restore_state_to_opc(CPUArchState *env, struct TranslationBlock *tb,
72 target_ulong *data);
74 void cpu_gen_init(void);
75 bool cpu_restore_state(CPUState *cpu, uintptr_t searched_pc);
77 void QEMU_NORETURN cpu_loop_exit_noexc(CPUState *cpu);
78 void QEMU_NORETURN cpu_io_recompile(CPUState *cpu, uintptr_t retaddr);
79 TranslationBlock *tb_gen_code(CPUState *cpu,
80 target_ulong pc, target_ulong cs_base,
81 uint32_t flags,
82 int cflags);
84 void QEMU_NORETURN cpu_loop_exit(CPUState *cpu);
85 void QEMU_NORETURN cpu_loop_exit_restore(CPUState *cpu, uintptr_t pc);
86 void QEMU_NORETURN cpu_loop_exit_atomic(CPUState *cpu, uintptr_t pc);
88 #if !defined(CONFIG_USER_ONLY)
89 void cpu_reloading_memory_map(void);
90 /**
91 * cpu_address_space_init:
92 * @cpu: CPU to add this address space to
93 * @as: address space to add
94 * @asidx: integer index of this address space
96 * Add the specified address space to the CPU's cpu_ases list.
97 * The address space added with @asidx 0 is the one used for the
98 * convenience pointer cpu->as.
99 * The target-specific code which registers ASes is responsible
100 * for defining what semantics address space 0, 1, 2, etc have.
102 * Before the first call to this function, the caller must set
103 * cpu->num_ases to the total number of address spaces it needs
104 * to support.
106 * Note that with KVM only one address space is supported.
108 void cpu_address_space_init(CPUState *cpu, AddressSpace *as, int asidx);
109 #endif
111 #if !defined(CONFIG_USER_ONLY) && defined(CONFIG_TCG)
112 /* cputlb.c */
114 * tlb_flush_page:
115 * @cpu: CPU whose TLB should be flushed
116 * @addr: virtual address of page to be flushed
118 * Flush one page from the TLB of the specified CPU, for all
119 * MMU indexes.
121 void tlb_flush_page(CPUState *cpu, target_ulong addr);
123 * tlb_flush_page_all_cpus:
124 * @cpu: src CPU of the flush
125 * @addr: virtual address of page to be flushed
127 * Flush one page from the TLB of the specified CPU, for all
128 * MMU indexes.
130 void tlb_flush_page_all_cpus(CPUState *src, target_ulong addr);
132 * tlb_flush_page_all_cpus_synced:
133 * @cpu: src CPU of the flush
134 * @addr: virtual address of page to be flushed
136 * Flush one page from the TLB of the specified CPU, for all MMU
137 * indexes like tlb_flush_page_all_cpus except the source vCPUs work
138 * is scheduled as safe work meaning all flushes will be complete once
139 * the source vCPUs safe work is complete. This will depend on when
140 * the guests translation ends the TB.
142 void tlb_flush_page_all_cpus_synced(CPUState *src, target_ulong addr);
144 * tlb_flush:
145 * @cpu: CPU whose TLB should be flushed
147 * Flush the entire TLB for the specified CPU. Most CPU architectures
148 * allow the implementation to drop entries from the TLB at any time
149 * so this is generally safe. If more selective flushing is required
150 * use one of the other functions for efficiency.
152 void tlb_flush(CPUState *cpu);
154 * tlb_flush_all_cpus:
155 * @cpu: src CPU of the flush
157 void tlb_flush_all_cpus(CPUState *src_cpu);
159 * tlb_flush_all_cpus_synced:
160 * @cpu: src CPU of the flush
162 * Like tlb_flush_all_cpus except this except the source vCPUs work is
163 * scheduled as safe work meaning all flushes will be complete once
164 * the source vCPUs safe work is complete. This will depend on when
165 * the guests translation ends the TB.
167 void tlb_flush_all_cpus_synced(CPUState *src_cpu);
169 * tlb_flush_page_by_mmuidx:
170 * @cpu: CPU whose TLB should be flushed
171 * @addr: virtual address of page to be flushed
172 * @idxmap: bitmap of MMU indexes to flush
174 * Flush one page from the TLB of the specified CPU, for the specified
175 * MMU indexes.
177 void tlb_flush_page_by_mmuidx(CPUState *cpu, target_ulong addr,
178 uint16_t idxmap);
180 * tlb_flush_page_by_mmuidx_all_cpus:
181 * @cpu: Originating CPU of the flush
182 * @addr: virtual address of page to be flushed
183 * @idxmap: bitmap of MMU indexes to flush
185 * Flush one page from the TLB of all CPUs, for the specified
186 * MMU indexes.
188 void tlb_flush_page_by_mmuidx_all_cpus(CPUState *cpu, target_ulong addr,
189 uint16_t idxmap);
191 * tlb_flush_page_by_mmuidx_all_cpus_synced:
192 * @cpu: Originating CPU of the flush
193 * @addr: virtual address of page to be flushed
194 * @idxmap: bitmap of MMU indexes to flush
196 * Flush one page from the TLB of all CPUs, for the specified MMU
197 * indexes like tlb_flush_page_by_mmuidx_all_cpus except the source
198 * vCPUs work is scheduled as safe work meaning all flushes will be
199 * complete once the source vCPUs safe work is complete. This will
200 * depend on when the guests translation ends the TB.
202 void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *cpu, target_ulong addr,
203 uint16_t idxmap);
205 * tlb_flush_by_mmuidx:
206 * @cpu: CPU whose TLB should be flushed
207 * @wait: If true ensure synchronisation by exiting the cpu_loop
208 * @idxmap: bitmap of MMU indexes to flush
210 * Flush all entries from the TLB of the specified CPU, for the specified
211 * MMU indexes.
213 void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap);
215 * tlb_flush_by_mmuidx_all_cpus:
216 * @cpu: Originating CPU of the flush
217 * @idxmap: bitmap of MMU indexes to flush
219 * Flush all entries from all TLBs of all CPUs, for the specified
220 * MMU indexes.
222 void tlb_flush_by_mmuidx_all_cpus(CPUState *cpu, uint16_t idxmap);
224 * tlb_flush_by_mmuidx_all_cpus_synced:
225 * @cpu: Originating CPU of the flush
226 * @idxmap: bitmap of MMU indexes to flush
228 * Flush all entries from all TLBs of all CPUs, for the specified
229 * MMU indexes like tlb_flush_by_mmuidx_all_cpus except except the source
230 * vCPUs work is scheduled as safe work meaning all flushes will be
231 * complete once the source vCPUs safe work is complete. This will
232 * depend on when the guests translation ends the TB.
234 void tlb_flush_by_mmuidx_all_cpus_synced(CPUState *cpu, uint16_t idxmap);
236 * tlb_set_page_with_attrs:
237 * @cpu: CPU to add this TLB entry for
238 * @vaddr: virtual address of page to add entry for
239 * @paddr: physical address of the page
240 * @attrs: memory transaction attributes
241 * @prot: access permissions (PAGE_READ/PAGE_WRITE/PAGE_EXEC bits)
242 * @mmu_idx: MMU index to insert TLB entry for
243 * @size: size of the page in bytes
245 * Add an entry to this CPU's TLB (a mapping from virtual address
246 * @vaddr to physical address @paddr) with the specified memory
247 * transaction attributes. This is generally called by the target CPU
248 * specific code after it has been called through the tlb_fill()
249 * entry point and performed a successful page table walk to find
250 * the physical address and attributes for the virtual address
251 * which provoked the TLB miss.
253 * At most one entry for a given virtual address is permitted. Only a
254 * single TARGET_PAGE_SIZE region is mapped; the supplied @size is only
255 * used by tlb_flush_page.
257 void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
258 hwaddr paddr, MemTxAttrs attrs,
259 int prot, int mmu_idx, target_ulong size);
260 /* tlb_set_page:
262 * This function is equivalent to calling tlb_set_page_with_attrs()
263 * with an @attrs argument of MEMTXATTRS_UNSPECIFIED. It's provided
264 * as a convenience for CPUs which don't use memory transaction attributes.
266 void tlb_set_page(CPUState *cpu, target_ulong vaddr,
267 hwaddr paddr, int prot,
268 int mmu_idx, target_ulong size);
269 void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr);
270 void probe_write(CPUArchState *env, target_ulong addr, int mmu_idx,
271 uintptr_t retaddr);
272 #else
273 static inline void tlb_flush_page(CPUState *cpu, target_ulong addr)
276 static inline void tlb_flush_page_all_cpus(CPUState *src, target_ulong addr)
279 static inline void tlb_flush_page_all_cpus_synced(CPUState *src,
280 target_ulong addr)
283 static inline void tlb_flush(CPUState *cpu)
286 static inline void tlb_flush_all_cpus(CPUState *src_cpu)
289 static inline void tlb_flush_all_cpus_synced(CPUState *src_cpu)
292 static inline void tlb_flush_page_by_mmuidx(CPUState *cpu,
293 target_ulong addr, uint16_t idxmap)
297 static inline void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap)
300 static inline void tlb_flush_page_by_mmuidx_all_cpus(CPUState *cpu,
301 target_ulong addr,
302 uint16_t idxmap)
305 static inline void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *cpu,
306 target_ulong addr,
307 uint16_t idxmap)
310 static inline void tlb_flush_by_mmuidx_all_cpus(CPUState *cpu, uint16_t idxmap)
313 static inline void tlb_flush_by_mmuidx_all_cpus_synced(CPUState *cpu,
314 uint16_t idxmap)
317 static inline void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr)
320 #endif
322 #define CODE_GEN_ALIGN 16 /* must be >= of the size of a icache line */
324 /* Estimated block size for TB allocation. */
325 /* ??? The following is based on a 2015 survey of x86_64 host output.
326 Better would seem to be some sort of dynamically sized TB array,
327 adapting to the block sizes actually being produced. */
328 #if defined(CONFIG_SOFTMMU)
329 #define CODE_GEN_AVG_BLOCK_SIZE 400
330 #else
331 #define CODE_GEN_AVG_BLOCK_SIZE 150
332 #endif
334 #if defined(_ARCH_PPC) \
335 || defined(__x86_64__) || defined(__i386__) \
336 || defined(__sparc__) || defined(__aarch64__) \
337 || defined(__s390x__) || defined(__mips__) \
338 || defined(CONFIG_TCG_INTERPRETER)
339 /* NOTE: Direct jump patching must be atomic to be thread-safe. */
340 #define USE_DIRECT_JUMP
341 #endif
343 struct TranslationBlock {
344 target_ulong pc; /* simulated PC corresponding to this block (EIP + CS base) */
345 target_ulong cs_base; /* CS base for this block */
346 uint32_t flags; /* flags defining in which context the code was generated */
347 uint16_t size; /* size of target code for this block (1 <=
348 size <= TARGET_PAGE_SIZE) */
349 uint16_t icount;
350 uint32_t cflags; /* compile flags */
351 #define CF_COUNT_MASK 0x7fff
352 #define CF_LAST_IO 0x8000 /* Last insn may be an IO access. */
353 #define CF_NOCACHE 0x10000 /* To be freed after execution */
354 #define CF_USE_ICOUNT 0x20000
355 #define CF_IGNORE_ICOUNT 0x40000 /* Do not generate icount code */
357 /* Per-vCPU dynamic tracing state used to generate this TB */
358 uint32_t trace_vcpu_dstate;
360 uint16_t invalid;
362 void *tc_ptr; /* pointer to the translated code */
363 uint8_t *tc_search; /* pointer to search data */
364 /* original tb when cflags has CF_NOCACHE */
365 struct TranslationBlock *orig_tb;
366 /* first and second physical page containing code. The lower bit
367 of the pointer tells the index in page_next[] */
368 struct TranslationBlock *page_next[2];
369 tb_page_addr_t page_addr[2];
371 /* The following data are used to directly call another TB from
372 * the code of this one. This can be done either by emitting direct or
373 * indirect native jump instructions. These jumps are reset so that the TB
374 * just continue its execution. The TB can be linked to another one by
375 * setting one of the jump targets (or patching the jump instruction). Only
376 * two of such jumps are supported.
378 uint16_t jmp_reset_offset[2]; /* offset of original jump target */
379 #define TB_JMP_RESET_OFFSET_INVALID 0xffff /* indicates no jump generated */
380 #ifdef USE_DIRECT_JUMP
381 uint16_t jmp_insn_offset[2]; /* offset of native jump instruction */
382 #else
383 uintptr_t jmp_target_addr[2]; /* target address for indirect jump */
384 #endif
385 /* Each TB has an assosiated circular list of TBs jumping to this one.
386 * jmp_list_first points to the first TB jumping to this one.
387 * jmp_list_next is used to point to the next TB in a list.
388 * Since each TB can have two jumps, it can participate in two lists.
389 * jmp_list_first and jmp_list_next are 4-byte aligned pointers to a
390 * TranslationBlock structure, but the two least significant bits of
391 * them are used to encode which data field of the pointed TB should
392 * be used to traverse the list further from that TB:
393 * 0 => jmp_list_next[0], 1 => jmp_list_next[1], 2 => jmp_list_first.
394 * In other words, 0/1 tells which jump is used in the pointed TB,
395 * and 2 means that this is a pointer back to the target TB of this list.
397 uintptr_t jmp_list_next[2];
398 uintptr_t jmp_list_first;
401 void tb_free(TranslationBlock *tb);
402 void tb_flush(CPUState *cpu);
403 void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr);
404 TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc,
405 target_ulong cs_base, uint32_t flags);
407 #if defined(USE_DIRECT_JUMP)
409 #if defined(CONFIG_TCG_INTERPRETER)
410 static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
412 /* patch the branch destination */
413 atomic_set((int32_t *)jmp_addr, addr - (jmp_addr + 4));
414 /* no need to flush icache explicitly */
416 #elif defined(_ARCH_PPC)
417 void ppc_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr);
418 #define tb_set_jmp_target1 ppc_tb_set_jmp_target
419 #elif defined(__i386__) || defined(__x86_64__)
420 static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
422 /* patch the branch destination */
423 atomic_set((int32_t *)jmp_addr, addr - (jmp_addr + 4));
424 /* no need to flush icache explicitly */
426 #elif defined(__s390x__)
427 static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
429 /* patch the branch destination */
430 intptr_t disp = addr - (jmp_addr - 2);
431 atomic_set((int32_t *)jmp_addr, disp / 2);
432 /* no need to flush icache explicitly */
434 #elif defined(__aarch64__)
435 void aarch64_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr);
436 #define tb_set_jmp_target1 aarch64_tb_set_jmp_target
437 #elif defined(__sparc__) || defined(__mips__)
438 void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr);
439 #else
440 #error tb_set_jmp_target1 is missing
441 #endif
443 static inline void tb_set_jmp_target(TranslationBlock *tb,
444 int n, uintptr_t addr)
446 uint16_t offset = tb->jmp_insn_offset[n];
447 tb_set_jmp_target1((uintptr_t)(tb->tc_ptr + offset), addr);
450 #else
452 /* set the jump target */
453 static inline void tb_set_jmp_target(TranslationBlock *tb,
454 int n, uintptr_t addr)
456 tb->jmp_target_addr[n] = addr;
459 #endif
461 /* Called with tb_lock held. */
462 static inline void tb_add_jump(TranslationBlock *tb, int n,
463 TranslationBlock *tb_next)
465 assert(n < ARRAY_SIZE(tb->jmp_list_next));
466 if (tb->jmp_list_next[n]) {
467 /* Another thread has already done this while we were
468 * outside of the lock; nothing to do in this case */
469 return;
471 qemu_log_mask_and_addr(CPU_LOG_EXEC, tb->pc,
472 "Linking TBs %p [" TARGET_FMT_lx
473 "] index %d -> %p [" TARGET_FMT_lx "]\n",
474 tb->tc_ptr, tb->pc, n,
475 tb_next->tc_ptr, tb_next->pc);
477 /* patch the native jump address */
478 tb_set_jmp_target(tb, n, (uintptr_t)tb_next->tc_ptr);
480 /* add in TB jmp circular list */
481 tb->jmp_list_next[n] = tb_next->jmp_list_first;
482 tb_next->jmp_list_first = (uintptr_t)tb | n;
485 /* GETPC is the true target of the return instruction that we'll execute. */
486 #if defined(CONFIG_TCG_INTERPRETER)
487 extern uintptr_t tci_tb_ptr;
488 # define GETPC() tci_tb_ptr
489 #else
490 # define GETPC() \
491 ((uintptr_t)__builtin_extract_return_addr(__builtin_return_address(0)))
492 #endif
494 /* The true return address will often point to a host insn that is part of
495 the next translated guest insn. Adjust the address backward to point to
496 the middle of the call insn. Subtracting one would do the job except for
497 several compressed mode architectures (arm, mips) which set the low bit
498 to indicate the compressed mode; subtracting two works around that. It
499 is also the case that there are no host isas that contain a call insn
500 smaller than 4 bytes, so we don't worry about special-casing this. */
501 #define GETPC_ADJ 2
503 void tb_lock(void);
504 void tb_unlock(void);
505 void tb_lock_reset(void);
507 #if !defined(CONFIG_USER_ONLY)
509 struct MemoryRegion *iotlb_to_region(CPUState *cpu,
510 hwaddr index, MemTxAttrs attrs);
512 void tlb_fill(CPUState *cpu, target_ulong addr, MMUAccessType access_type,
513 int mmu_idx, uintptr_t retaddr);
515 #endif
517 #if defined(CONFIG_USER_ONLY)
518 void mmap_lock(void);
519 void mmap_unlock(void);
520 bool have_mmap_lock(void);
522 static inline tb_page_addr_t get_page_addr_code(CPUArchState *env1, target_ulong addr)
524 return addr;
526 #else
527 static inline void mmap_lock(void) {}
528 static inline void mmap_unlock(void) {}
530 /* cputlb.c */
531 tb_page_addr_t get_page_addr_code(CPUArchState *env1, target_ulong addr);
533 void tlb_reset_dirty(CPUState *cpu, ram_addr_t start1, ram_addr_t length);
534 void tlb_set_dirty(CPUState *cpu, target_ulong vaddr);
536 /* exec.c */
537 void tb_flush_jmp_cache(CPUState *cpu, target_ulong addr);
539 MemoryRegionSection *
540 address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr,
541 hwaddr *xlat, hwaddr *plen);
542 hwaddr memory_region_section_get_iotlb(CPUState *cpu,
543 MemoryRegionSection *section,
544 target_ulong vaddr,
545 hwaddr paddr, hwaddr xlat,
546 int prot,
547 target_ulong *address);
548 bool memory_region_is_unassigned(MemoryRegion *mr);
550 #endif
552 /* vl.c */
553 extern int singlestep;
555 #endif