Merge tag 'v9.1.0'
[qemu/ar7.git] / include / exec / exec-all.h
blob95583db8fc2d496d14ea5547a3b806323e77064a
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.1 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 "cpu.h"
24 #if defined(CONFIG_USER_ONLY)
25 #include "exec/abi_ptr.h"
26 #include "exec/cpu_ldst.h"
27 #endif
28 #include "exec/mmu-access-type.h"
29 #include "exec/translation-block.h"
30 #include "qemu/clang-tsa.h"
32 /* Get a backtrace for the guest code. */
33 const char *qemu_sprint_backtrace(char *buffer, size_t length);
35 /**
36 * cpu_loop_exit_requested:
37 * @cpu: The CPU state to be tested
39 * Indicate if somebody asked for a return of the CPU to the main loop
40 * (e.g., via cpu_exit() or cpu_interrupt()).
42 * This is helpful for architectures that support interruptible
43 * instructions. After writing back all state to registers/memory, this
44 * call can be used to check if it makes sense to return to the main loop
45 * or to continue executing the interruptible instruction.
47 static inline bool cpu_loop_exit_requested(CPUState *cpu)
49 return (int32_t)qatomic_read(&cpu->neg.icount_decr.u32) < 0;
52 #if !defined(CONFIG_USER_ONLY) && defined(CONFIG_TCG)
53 /* cputlb.c */
54 /**
55 * tlb_init - initialize a CPU's TLB
56 * @cpu: CPU whose TLB should be initialized
58 void tlb_init(CPUState *cpu);
59 /**
60 * tlb_destroy - destroy a CPU's TLB
61 * @cpu: CPU whose TLB should be destroyed
63 void tlb_destroy(CPUState *cpu);
64 /**
65 * tlb_flush_page:
66 * @cpu: CPU whose TLB should be flushed
67 * @addr: virtual address of page to be flushed
69 * Flush one page from the TLB of the specified CPU, for all
70 * MMU indexes.
72 void tlb_flush_page(CPUState *cpu, vaddr addr);
73 /**
74 * tlb_flush_page_all_cpus_synced:
75 * @cpu: src CPU of the flush
76 * @addr: virtual address of page to be flushed
78 * Flush one page from the TLB of all CPUs, for all
79 * MMU indexes.
81 * When this function returns, no CPUs will subsequently perform
82 * translations using the flushed TLBs.
84 void tlb_flush_page_all_cpus_synced(CPUState *src, vaddr addr);
85 /**
86 * tlb_flush:
87 * @cpu: CPU whose TLB should be flushed
89 * Flush the entire TLB for the specified CPU. Most CPU architectures
90 * allow the implementation to drop entries from the TLB at any time
91 * so this is generally safe. If more selective flushing is required
92 * use one of the other functions for efficiency.
94 void tlb_flush(CPUState *cpu);
95 /**
96 * tlb_flush_all_cpus_synced:
97 * @cpu: src CPU of the flush
99 * Flush the entire TLB for all CPUs, for all MMU indexes.
101 * When this function returns, no CPUs will subsequently perform
102 * translations using the flushed TLBs.
104 void tlb_flush_all_cpus_synced(CPUState *src_cpu);
106 * tlb_flush_page_by_mmuidx:
107 * @cpu: CPU whose TLB should be flushed
108 * @addr: virtual address of page to be flushed
109 * @idxmap: bitmap of MMU indexes to flush
111 * Flush one page from the TLB of the specified CPU, for the specified
112 * MMU indexes.
114 void tlb_flush_page_by_mmuidx(CPUState *cpu, vaddr addr,
115 uint16_t idxmap);
117 * tlb_flush_page_by_mmuidx_all_cpus_synced:
118 * @cpu: Originating CPU of the flush
119 * @addr: virtual address of page to be flushed
120 * @idxmap: bitmap of MMU indexes to flush
122 * Flush one page from the TLB of all CPUs, for the specified
123 * MMU indexes.
125 * When this function returns, no CPUs will subsequently perform
126 * translations using the flushed TLBs.
128 void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *cpu, vaddr addr,
129 uint16_t idxmap);
131 * tlb_flush_by_mmuidx:
132 * @cpu: CPU whose TLB should be flushed
133 * @wait: If true ensure synchronisation by exiting the cpu_loop
134 * @idxmap: bitmap of MMU indexes to flush
136 * Flush all entries from the TLB of the specified CPU, for the specified
137 * MMU indexes.
139 void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap);
141 * tlb_flush_by_mmuidx_all_cpus_synced:
142 * @cpu: Originating CPU of the flush
143 * @idxmap: bitmap of MMU indexes to flush
145 * Flush all entries from the TLB of all CPUs, for the specified
146 * MMU indexes.
148 * When this function returns, no CPUs will subsequently perform
149 * translations using the flushed TLBs.
151 void tlb_flush_by_mmuidx_all_cpus_synced(CPUState *cpu, uint16_t idxmap);
154 * tlb_flush_page_bits_by_mmuidx
155 * @cpu: CPU whose TLB should be flushed
156 * @addr: virtual address of page to be flushed
157 * @idxmap: bitmap of mmu indexes to flush
158 * @bits: number of significant bits in address
160 * Similar to tlb_flush_page_mask, but with a bitmap of indexes.
162 void tlb_flush_page_bits_by_mmuidx(CPUState *cpu, vaddr addr,
163 uint16_t idxmap, unsigned bits);
165 /* Similarly, with broadcast and syncing. */
166 void tlb_flush_page_bits_by_mmuidx_all_cpus_synced
167 (CPUState *cpu, vaddr addr, uint16_t idxmap, unsigned bits);
170 * tlb_flush_range_by_mmuidx
171 * @cpu: CPU whose TLB should be flushed
172 * @addr: virtual address of the start of the range to be flushed
173 * @len: length of range to be flushed
174 * @idxmap: bitmap of mmu indexes to flush
175 * @bits: number of significant bits in address
177 * For each mmuidx in @idxmap, flush all pages within [@addr,@addr+@len),
178 * comparing only the low @bits worth of each virtual page.
180 void tlb_flush_range_by_mmuidx(CPUState *cpu, vaddr addr,
181 vaddr len, uint16_t idxmap,
182 unsigned bits);
184 /* Similarly, with broadcast and syncing. */
185 void tlb_flush_range_by_mmuidx_all_cpus_synced(CPUState *cpu,
186 vaddr addr,
187 vaddr len,
188 uint16_t idxmap,
189 unsigned bits);
192 * tlb_set_page_full:
193 * @cpu: CPU context
194 * @mmu_idx: mmu index of the tlb to modify
195 * @addr: virtual address of the entry to add
196 * @full: the details of the tlb entry
198 * Add an entry to @cpu tlb index @mmu_idx. All of the fields of
199 * @full must be filled, except for xlat_section, and constitute
200 * the complete description of the translated page.
202 * This is generally called by the target tlb_fill function after
203 * having performed a successful page table walk to find the physical
204 * address and attributes for the translation.
206 * At most one entry for a given virtual address is permitted. Only a
207 * single TARGET_PAGE_SIZE region is mapped; @full->lg_page_size is only
208 * used by tlb_flush_page.
210 void tlb_set_page_full(CPUState *cpu, int mmu_idx, vaddr addr,
211 CPUTLBEntryFull *full);
214 * tlb_set_page_with_attrs:
215 * @cpu: CPU to add this TLB entry for
216 * @addr: virtual address of page to add entry for
217 * @paddr: physical address of the page
218 * @attrs: memory transaction attributes
219 * @prot: access permissions (PAGE_READ/PAGE_WRITE/PAGE_EXEC bits)
220 * @mmu_idx: MMU index to insert TLB entry for
221 * @size: size of the page in bytes
223 * Add an entry to this CPU's TLB (a mapping from virtual address
224 * @addr to physical address @paddr) with the specified memory
225 * transaction attributes. This is generally called by the target CPU
226 * specific code after it has been called through the tlb_fill()
227 * entry point and performed a successful page table walk to find
228 * the physical address and attributes for the virtual address
229 * which provoked the TLB miss.
231 * At most one entry for a given virtual address is permitted. Only a
232 * single TARGET_PAGE_SIZE region is mapped; the supplied @size is only
233 * used by tlb_flush_page.
235 void tlb_set_page_with_attrs(CPUState *cpu, vaddr addr,
236 hwaddr paddr, MemTxAttrs attrs,
237 int prot, int mmu_idx, vaddr size);
238 /* tlb_set_page:
240 * This function is equivalent to calling tlb_set_page_with_attrs()
241 * with an @attrs argument of MEMTXATTRS_UNSPECIFIED. It's provided
242 * as a convenience for CPUs which don't use memory transaction attributes.
244 void tlb_set_page(CPUState *cpu, vaddr addr,
245 hwaddr paddr, int prot,
246 int mmu_idx, vaddr size);
247 #else
248 static inline void tlb_init(CPUState *cpu)
251 static inline void tlb_destroy(CPUState *cpu)
254 static inline void tlb_flush_page(CPUState *cpu, vaddr addr)
257 static inline void tlb_flush_page_all_cpus_synced(CPUState *src, vaddr addr)
260 static inline void tlb_flush(CPUState *cpu)
263 static inline void tlb_flush_all_cpus_synced(CPUState *src_cpu)
266 static inline void tlb_flush_page_by_mmuidx(CPUState *cpu,
267 vaddr addr, uint16_t idxmap)
271 static inline void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap)
274 static inline void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *cpu,
275 vaddr addr,
276 uint16_t idxmap)
279 static inline void tlb_flush_by_mmuidx_all_cpus_synced(CPUState *cpu,
280 uint16_t idxmap)
283 static inline void tlb_flush_page_bits_by_mmuidx(CPUState *cpu,
284 vaddr addr,
285 uint16_t idxmap,
286 unsigned bits)
289 static inline void
290 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(CPUState *cpu, vaddr addr,
291 uint16_t idxmap, unsigned bits)
294 static inline void tlb_flush_range_by_mmuidx(CPUState *cpu, vaddr addr,
295 vaddr len, uint16_t idxmap,
296 unsigned bits)
299 static inline void tlb_flush_range_by_mmuidx_all_cpus_synced(CPUState *cpu,
300 vaddr addr,
301 vaddr len,
302 uint16_t idxmap,
303 unsigned bits)
306 #endif
308 #if defined(CONFIG_TCG)
311 * probe_access:
312 * @env: CPUArchState
313 * @addr: guest virtual address to look up
314 * @size: size of the access
315 * @access_type: read, write or execute permission
316 * @mmu_idx: MMU index to use for lookup
317 * @retaddr: return address for unwinding
319 * Look up the guest virtual address @addr. Raise an exception if the
320 * page does not satisfy @access_type. Raise an exception if the
321 * access (@addr, @size) hits a watchpoint. For writes, mark a clean
322 * page as dirty.
324 * Finally, return the host address for a page that is backed by RAM,
325 * or NULL if the page requires I/O.
327 void *probe_access(CPUArchState *env, vaddr addr, int size,
328 MMUAccessType access_type, int mmu_idx, uintptr_t retaddr);
330 static inline void *probe_write(CPUArchState *env, vaddr addr, int size,
331 int mmu_idx, uintptr_t retaddr)
333 return probe_access(env, addr, size, MMU_DATA_STORE, mmu_idx, retaddr);
336 static inline void *probe_read(CPUArchState *env, vaddr addr, int size,
337 int mmu_idx, uintptr_t retaddr)
339 return probe_access(env, addr, size, MMU_DATA_LOAD, mmu_idx, retaddr);
343 * probe_access_flags:
344 * @env: CPUArchState
345 * @addr: guest virtual address to look up
346 * @size: size of the access
347 * @access_type: read, write or execute permission
348 * @mmu_idx: MMU index to use for lookup
349 * @nonfault: suppress the fault
350 * @phost: return value for host address
351 * @retaddr: return address for unwinding
353 * Similar to probe_access, loosely returning the TLB_FLAGS_MASK for
354 * the page, and storing the host address for RAM in @phost.
356 * If @nonfault is set, do not raise an exception but return TLB_INVALID_MASK.
357 * Do not handle watchpoints, but include TLB_WATCHPOINT in the returned flags.
358 * Do handle clean pages, so exclude TLB_NOTDIRY from the returned flags.
359 * For simplicity, all "mmio-like" flags are folded to TLB_MMIO.
361 int probe_access_flags(CPUArchState *env, vaddr addr, int size,
362 MMUAccessType access_type, int mmu_idx,
363 bool nonfault, void **phost, uintptr_t retaddr);
365 #ifndef CONFIG_USER_ONLY
368 * probe_access_full:
369 * Like probe_access_flags, except also return into @pfull.
371 * The CPUTLBEntryFull structure returned via @pfull is transient
372 * and must be consumed or copied immediately, before any further
373 * access or changes to TLB @mmu_idx.
375 int probe_access_full(CPUArchState *env, vaddr addr, int size,
376 MMUAccessType access_type, int mmu_idx,
377 bool nonfault, void **phost,
378 CPUTLBEntryFull **pfull, uintptr_t retaddr);
381 * probe_access_mmu() - Like probe_access_full except cannot fault and
382 * doesn't trigger instrumentation.
384 * @env: CPUArchState
385 * @vaddr: virtual address to probe
386 * @size: size of the probe
387 * @access_type: read, write or execute permission
388 * @mmu_idx: softmmu index
389 * @phost: ptr to return value host address or NULL
390 * @pfull: ptr to return value CPUTLBEntryFull structure or NULL
392 * The CPUTLBEntryFull structure returned via @pfull is transient
393 * and must be consumed or copied immediately, before any further
394 * access or changes to TLB @mmu_idx.
396 * Returns: TLB flags as per probe_access_flags()
398 int probe_access_full_mmu(CPUArchState *env, vaddr addr, int size,
399 MMUAccessType access_type, int mmu_idx,
400 void **phost, CPUTLBEntryFull **pfull);
402 #endif /* !CONFIG_USER_ONLY */
403 #endif /* CONFIG_TCG */
405 static inline tb_page_addr_t tb_page_addr0(const TranslationBlock *tb)
407 #ifdef CONFIG_USER_ONLY
408 return tb->itree.start;
409 #else
410 return tb->page_addr[0];
411 #endif
414 static inline tb_page_addr_t tb_page_addr1(const TranslationBlock *tb)
416 #ifdef CONFIG_USER_ONLY
417 tb_page_addr_t next = tb->itree.last & TARGET_PAGE_MASK;
418 return next == (tb->itree.start & TARGET_PAGE_MASK) ? -1 : next;
419 #else
420 return tb->page_addr[1];
421 #endif
424 static inline void tb_set_page_addr0(TranslationBlock *tb,
425 tb_page_addr_t addr)
427 #ifdef CONFIG_USER_ONLY
428 tb->itree.start = addr;
430 * To begin, we record an interval of one byte. When the translation
431 * loop encounters a second page, the interval will be extended to
432 * include the first byte of the second page, which is sufficient to
433 * allow tb_page_addr1() above to work properly. The final corrected
434 * interval will be set by tb_page_add() from tb->size before the
435 * node is added to the interval tree.
437 tb->itree.last = addr;
438 #else
439 tb->page_addr[0] = addr;
440 #endif
443 static inline void tb_set_page_addr1(TranslationBlock *tb,
444 tb_page_addr_t addr)
446 #ifdef CONFIG_USER_ONLY
447 /* Extend the interval to the first byte of the second page. See above. */
448 tb->itree.last = addr;
449 #else
450 tb->page_addr[1] = addr;
451 #endif
454 /* TranslationBlock invalidate API */
455 void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr);
456 void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t last);
457 void tb_set_jmp_target(TranslationBlock *tb, int n, uintptr_t addr);
459 /* GETPC is the true target of the return instruction that we'll execute. */
460 #if defined(CONFIG_TCG_INTERPRETER)
461 extern __thread uintptr_t tci_tb_ptr;
462 # define GETPC() tci_tb_ptr
463 #else
464 # define GETPC() \
465 ((uintptr_t)__builtin_extract_return_addr(__builtin_return_address(0)))
466 #endif
468 /* The true return address will often point to a host insn that is part of
469 the next translated guest insn. Adjust the address backward to point to
470 the middle of the call insn. Subtracting one would do the job except for
471 several compressed mode architectures (arm, mips) which set the low bit
472 to indicate the compressed mode; subtracting two works around that. It
473 is also the case that there are no host isas that contain a call insn
474 smaller than 4 bytes, so we don't worry about special-casing this. */
475 #define GETPC_ADJ 2
477 #if !defined(CONFIG_USER_ONLY)
480 * iotlb_to_section:
481 * @cpu: CPU performing the access
482 * @index: TCG CPU IOTLB entry
484 * Given a TCG CPU IOTLB entry, return the MemoryRegionSection that
485 * it refers to. @index will have been initially created and returned
486 * by memory_region_section_get_iotlb().
488 struct MemoryRegionSection *iotlb_to_section(CPUState *cpu,
489 hwaddr index, MemTxAttrs attrs);
490 #endif
493 * get_page_addr_code_hostp()
494 * @env: CPUArchState
495 * @addr: guest virtual address of guest code
497 * See get_page_addr_code() (full-system version) for documentation on the
498 * return value.
500 * Sets *@hostp (when @hostp is non-NULL) as follows.
501 * If the return value is -1, sets *@hostp to NULL. Otherwise, sets *@hostp
502 * to the host address where @addr's content is kept.
504 * Note: this function can trigger an exception.
506 tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, vaddr addr,
507 void **hostp);
510 * get_page_addr_code()
511 * @env: CPUArchState
512 * @addr: guest virtual address of guest code
514 * If we cannot translate and execute from the entire RAM page, or if
515 * the region is not backed by RAM, returns -1. Otherwise, returns the
516 * ram_addr_t corresponding to the guest code at @addr.
518 * Note: this function can trigger an exception.
520 static inline tb_page_addr_t get_page_addr_code(CPUArchState *env,
521 vaddr addr)
523 return get_page_addr_code_hostp(env, addr, NULL);
526 #if defined(CONFIG_USER_ONLY)
527 void TSA_NO_TSA mmap_lock(void);
528 void TSA_NO_TSA mmap_unlock(void);
529 bool have_mmap_lock(void);
531 static inline void mmap_unlock_guard(void *unused)
533 mmap_unlock();
536 #define WITH_MMAP_LOCK_GUARD() \
537 for (int _mmap_lock_iter __attribute__((cleanup(mmap_unlock_guard))) \
538 = (mmap_lock(), 0); _mmap_lock_iter == 0; _mmap_lock_iter = 1)
541 * adjust_signal_pc:
542 * @pc: raw pc from the host signal ucontext_t.
543 * @is_write: host memory operation was write, or read-modify-write.
545 * Alter @pc as required for unwinding. Return the type of the
546 * guest memory access -- host reads may be for guest execution.
548 MMUAccessType adjust_signal_pc(uintptr_t *pc, bool is_write);
551 * handle_sigsegv_accerr_write:
552 * @cpu: the cpu context
553 * @old_set: the sigset_t from the signal ucontext_t
554 * @host_pc: the host pc, adjusted for the signal
555 * @host_addr: the host address of the fault
557 * Return true if the write fault has been handled, and should be re-tried.
559 bool handle_sigsegv_accerr_write(CPUState *cpu, sigset_t *old_set,
560 uintptr_t host_pc, abi_ptr guest_addr);
563 * cpu_loop_exit_sigsegv:
564 * @cpu: the cpu context
565 * @addr: the guest address of the fault
566 * @access_type: access was read/write/execute
567 * @maperr: true for invalid page, false for permission fault
568 * @ra: host pc for unwinding
570 * Use the TCGCPUOps hook to record cpu state, do guest operating system
571 * specific things to raise SIGSEGV, and jump to the main cpu loop.
573 G_NORETURN void cpu_loop_exit_sigsegv(CPUState *cpu, target_ulong addr,
574 MMUAccessType access_type,
575 bool maperr, uintptr_t ra);
578 * cpu_loop_exit_sigbus:
579 * @cpu: the cpu context
580 * @addr: the guest address of the alignment fault
581 * @access_type: access was read/write/execute
582 * @ra: host pc for unwinding
584 * Use the TCGCPUOps hook to record cpu state, do guest operating system
585 * specific things to raise SIGBUS, and jump to the main cpu loop.
587 G_NORETURN void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr,
588 MMUAccessType access_type,
589 uintptr_t ra);
591 #else
592 static inline void mmap_lock(void) {}
593 static inline void mmap_unlock(void) {}
594 #define WITH_MMAP_LOCK_GUARD()
596 void tlb_reset_dirty(CPUState *cpu, ram_addr_t start1, ram_addr_t length);
597 void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t length);
599 MemoryRegionSection *
600 address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr,
601 hwaddr *xlat, hwaddr *plen,
602 MemTxAttrs attrs, int *prot);
603 hwaddr memory_region_section_get_iotlb(CPUState *cpu,
604 MemoryRegionSection *section);
605 #endif
607 #endif