178271684SClaudio Fontana /* 278271684SClaudio Fontana * TCG CPU-specific operations 378271684SClaudio Fontana * 478271684SClaudio Fontana * Copyright 2021 SUSE LLC 578271684SClaudio Fontana * 678271684SClaudio Fontana * This work is licensed under the terms of the GNU GPL, version 2 or later. 778271684SClaudio Fontana * See the COPYING file in the top-level directory. 878271684SClaudio Fontana */ 978271684SClaudio Fontana 1078271684SClaudio Fontana #ifndef TCG_CPU_OPS_H 1178271684SClaudio Fontana #define TCG_CPU_OPS_H 1278271684SClaudio Fontana 1376d07d32SPhilippe Mathieu-Daudé #include "exec/breakpoint.h" 1476d07d32SPhilippe Mathieu-Daudé #include "exec/hwaddr.h" 1576d07d32SPhilippe Mathieu-Daudé #include "exec/memattrs.h" 16f168808dSRichard Henderson #include "exec/memop.h" 1776d07d32SPhilippe Mathieu-Daudé #include "exec/mmu-access-type.h" 1876d07d32SPhilippe Mathieu-Daudé #include "exec/vaddr.h" 1904583ce7SPhilippe Mathieu-Daudé #include "tcg/tcg-mo.h" 2078271684SClaudio Fontana 2178271684SClaudio Fontana struct TCGCPUOps { 22*a3d40b5eSPhilippe Mathieu-Daudé /** 23*a3d40b5eSPhilippe Mathieu-Daudé * mttcg_supported: multi-threaded TCG is supported 24*a3d40b5eSPhilippe Mathieu-Daudé * 25*a3d40b5eSPhilippe Mathieu-Daudé * Target (TCG frontend) supports: 26*a3d40b5eSPhilippe Mathieu-Daudé * - atomic instructions 27*a3d40b5eSPhilippe Mathieu-Daudé * - memory ordering primitives (barriers) 28*a3d40b5eSPhilippe Mathieu-Daudé */ 29*a3d40b5eSPhilippe Mathieu-Daudé bool mttcg_supported; 3004583ce7SPhilippe Mathieu-Daudé 3104583ce7SPhilippe Mathieu-Daudé /** 3204583ce7SPhilippe Mathieu-Daudé * @guest_default_memory_order: default barrier that is required 3304583ce7SPhilippe Mathieu-Daudé * for the guest memory ordering. 3404583ce7SPhilippe Mathieu-Daudé */ 3504583ce7SPhilippe Mathieu-Daudé TCGBar guest_default_memory_order; 3604583ce7SPhilippe Mathieu-Daudé 3778271684SClaudio Fontana /** 38669dcb60SMichael Tokarev * @initialize: Initialize TCG state 3978271684SClaudio Fontana * 4078271684SClaudio Fontana * Called when the first CPU is realized. 4178271684SClaudio Fontana */ 4278271684SClaudio Fontana void (*initialize)(void); 4378271684SClaudio Fontana /** 44e4a8e093SRichard Henderson * @translate_code: Translate guest instructions to TCGOps 45e4a8e093SRichard Henderson * @cpu: cpu context 46e4a8e093SRichard Henderson * @tb: translation block 47e4a8e093SRichard Henderson * @max_insns: max number of instructions to translate 48e4a8e093SRichard Henderson * @pc: guest virtual program counter address 49e4a8e093SRichard Henderson * @host_pc: host physical program counter address 50e4a8e093SRichard Henderson * 51e4a8e093SRichard Henderson * This function must be provided by the target, which should create 52e4a8e093SRichard Henderson * the target-specific DisasContext, and then invoke translator_loop. 53e4a8e093SRichard Henderson */ 54e4a8e093SRichard Henderson void (*translate_code)(CPUState *cpu, TranslationBlock *tb, 55e4a8e093SRichard Henderson int *max_insns, vaddr pc, void *host_pc); 56e4a8e093SRichard Henderson /** 5778271684SClaudio Fontana * @synchronize_from_tb: Synchronize state from a TCG #TranslationBlock 5878271684SClaudio Fontana * 5978271684SClaudio Fontana * This is called when we abandon execution of a TB before starting it, 6078271684SClaudio Fontana * and must set all parts of the CPU state which the previous TB in the 6178271684SClaudio Fontana * chain may not have updated. 6278271684SClaudio Fontana * By default, when this is NULL, a call is made to @set_pc(tb->pc). 6378271684SClaudio Fontana * 6478271684SClaudio Fontana * If more state needs to be restored, the target must implement a 6578271684SClaudio Fontana * function to restore all the state, and register it here. 6678271684SClaudio Fontana */ 678349d2aeSRichard Henderson void (*synchronize_from_tb)(CPUState *cpu, const TranslationBlock *tb); 68d2925689SRichard Henderson /** 69d2925689SRichard Henderson * @restore_state_to_opc: Synchronize state from INDEX_op_start_insn 70d2925689SRichard Henderson * 71d2925689SRichard Henderson * This is called when we unwind state in the middle of a TB, 72d2925689SRichard Henderson * usually before raising an exception. Set all part of the CPU 73d2925689SRichard Henderson * state which are tracked insn-by-insn in the target-specific 74d2925689SRichard Henderson * arguments to start_insn, passed as @data. 75d2925689SRichard Henderson */ 76d2925689SRichard Henderson void (*restore_state_to_opc)(CPUState *cpu, const TranslationBlock *tb, 77d2925689SRichard Henderson const uint64_t *data); 78d2925689SRichard Henderson 7978271684SClaudio Fontana /** @cpu_exec_enter: Callback for cpu_exec preparation */ 8078271684SClaudio Fontana void (*cpu_exec_enter)(CPUState *cpu); 8178271684SClaudio Fontana /** @cpu_exec_exit: Callback for cpu_exec cleanup */ 8278271684SClaudio Fontana void (*cpu_exec_exit)(CPUState *cpu); 8378271684SClaudio Fontana /** @debug_excp_handler: Callback for handling debug exceptions */ 8478271684SClaudio Fontana void (*debug_excp_handler)(CPUState *cpu); 8578271684SClaudio Fontana 8617fa8b6fSPhilippe Mathieu-Daudé /** @mmu_index: Callback for choosing softmmu mmu index */ 8717fa8b6fSPhilippe Mathieu-Daudé int (*mmu_index)(CPUState *cpu, bool ifetch); 8817fa8b6fSPhilippe Mathieu-Daudé 89fd3f7d24SAnton Johansson #ifdef CONFIG_USER_ONLY 9012096421SPhilippe Mathieu-Daudé /** 9112096421SPhilippe Mathieu-Daudé * @fake_user_interrupt: Callback for 'fake exception' handling. 9212096421SPhilippe Mathieu-Daudé * 9312096421SPhilippe Mathieu-Daudé * Simulate 'fake exception' which will be handled outside the 9412096421SPhilippe Mathieu-Daudé * cpu execution loop (hack for x86 user mode). 9512096421SPhilippe Mathieu-Daudé */ 9612096421SPhilippe Mathieu-Daudé void (*fake_user_interrupt)(CPUState *cpu); 97fd3f7d24SAnton Johansson 9875fe97b4SPhilippe Mathieu-Daudé /** 9975fe97b4SPhilippe Mathieu-Daudé * record_sigsegv: 10075fe97b4SPhilippe Mathieu-Daudé * @cpu: cpu context 10175fe97b4SPhilippe Mathieu-Daudé * @addr: faulting guest address 10275fe97b4SPhilippe Mathieu-Daudé * @access_type: access was read/write/execute 10375fe97b4SPhilippe Mathieu-Daudé * @maperr: true for invalid page, false for permission fault 10475fe97b4SPhilippe Mathieu-Daudé * @ra: host pc for unwinding 10575fe97b4SPhilippe Mathieu-Daudé * 10675fe97b4SPhilippe Mathieu-Daudé * We are about to raise SIGSEGV with si_code set for @maperr, 10775fe97b4SPhilippe Mathieu-Daudé * and si_addr set for @addr. Record anything further needed 10875fe97b4SPhilippe Mathieu-Daudé * for the signal ucontext_t. 10975fe97b4SPhilippe Mathieu-Daudé * 11075fe97b4SPhilippe Mathieu-Daudé * If the emulated kernel does not provide anything to the signal 11175fe97b4SPhilippe Mathieu-Daudé * handler with anything besides the user context registers, and 11275fe97b4SPhilippe Mathieu-Daudé * the siginfo_t, then this hook need do nothing and may be omitted. 11375fe97b4SPhilippe Mathieu-Daudé * Otherwise, record the data and return; the caller will raise 11475fe97b4SPhilippe Mathieu-Daudé * the signal, unwind the cpu state, and return to the main loop. 11575fe97b4SPhilippe Mathieu-Daudé * 11675fe97b4SPhilippe Mathieu-Daudé * If it is simpler to re-use the sysemu tlb_fill code, @ra is provided 11775fe97b4SPhilippe Mathieu-Daudé * so that a "normal" cpu exception can be raised. In this case, 11875fe97b4SPhilippe Mathieu-Daudé * the signal must be raised by the architecture cpu_loop. 11975fe97b4SPhilippe Mathieu-Daudé */ 12075fe97b4SPhilippe Mathieu-Daudé void (*record_sigsegv)(CPUState *cpu, vaddr addr, 12175fe97b4SPhilippe Mathieu-Daudé MMUAccessType access_type, 12275fe97b4SPhilippe Mathieu-Daudé bool maperr, uintptr_t ra); 12375fe97b4SPhilippe Mathieu-Daudé /** 12475fe97b4SPhilippe Mathieu-Daudé * record_sigbus: 12575fe97b4SPhilippe Mathieu-Daudé * @cpu: cpu context 12675fe97b4SPhilippe Mathieu-Daudé * @addr: misaligned guest address 12775fe97b4SPhilippe Mathieu-Daudé * @access_type: access was read/write/execute 12875fe97b4SPhilippe Mathieu-Daudé * @ra: host pc for unwinding 12975fe97b4SPhilippe Mathieu-Daudé * 13075fe97b4SPhilippe Mathieu-Daudé * We are about to raise SIGBUS with si_code BUS_ADRALN, 13175fe97b4SPhilippe Mathieu-Daudé * and si_addr set for @addr. Record anything further needed 13275fe97b4SPhilippe Mathieu-Daudé * for the signal ucontext_t. 13375fe97b4SPhilippe Mathieu-Daudé * 13475fe97b4SPhilippe Mathieu-Daudé * If the emulated kernel does not provide the signal handler with 13575fe97b4SPhilippe Mathieu-Daudé * anything besides the user context registers, and the siginfo_t, 13675fe97b4SPhilippe Mathieu-Daudé * then this hook need do nothing and may be omitted. 13775fe97b4SPhilippe Mathieu-Daudé * Otherwise, record the data and return; the caller will raise 13875fe97b4SPhilippe Mathieu-Daudé * the signal, unwind the cpu state, and return to the main loop. 13975fe97b4SPhilippe Mathieu-Daudé * 14075fe97b4SPhilippe Mathieu-Daudé * If it is simpler to re-use the sysemu do_unaligned_access code, 14175fe97b4SPhilippe Mathieu-Daudé * @ra is provided so that a "normal" cpu exception can be raised. 14275fe97b4SPhilippe Mathieu-Daudé * In this case, the signal must be raised by the architecture cpu_loop. 14375fe97b4SPhilippe Mathieu-Daudé */ 14475fe97b4SPhilippe Mathieu-Daudé void (*record_sigbus)(CPUState *cpu, vaddr addr, 14575fe97b4SPhilippe Mathieu-Daudé MMUAccessType access_type, uintptr_t ra); 14675fe97b4SPhilippe Mathieu-Daudé #else 147b11cdf27SAnton Johansson /** @do_interrupt: Callback for interrupt handling. */ 148b11cdf27SAnton Johansson void (*do_interrupt)(CPUState *cpu); 14977c0fc4eSPhilippe Mathieu-Daudé /** @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec */ 15077c0fc4eSPhilippe Mathieu-Daudé bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request); 151408b2b3dSPeter Maydell /** 152408b2b3dSPeter Maydell * @cpu_exec_halt: Callback for handling halt in cpu_exec. 153408b2b3dSPeter Maydell * 154408b2b3dSPeter Maydell * The target CPU should do any special processing here that it needs 155408b2b3dSPeter Maydell * to do when the CPU is in the halted state. 156408b2b3dSPeter Maydell * 157408b2b3dSPeter Maydell * Return true to indicate that the CPU should now leave halt, false 1580487c631SPeter Maydell * if it should remain in the halted state. (This should generally 1590487c631SPeter Maydell * be the same value that cpu_has_work() would return.) 160408b2b3dSPeter Maydell * 1610487c631SPeter Maydell * This method must be provided. If the target does not need to 1620487c631SPeter Maydell * do anything special for halt, the same function used for its 16372eacd62SPhilippe Mathieu-Daudé * SysemuCPUOps::has_work method can be used here, as they have the 1640487c631SPeter Maydell * same function signature. 165408b2b3dSPeter Maydell */ 166408b2b3dSPeter Maydell bool (*cpu_exec_halt)(CPUState *cpu); 16778271684SClaudio Fontana /** 168f168808dSRichard Henderson * @tlb_fill_align: Handle a softmmu tlb miss 169f168808dSRichard Henderson * @cpu: cpu context 170f168808dSRichard Henderson * @out: output page properties 171f168808dSRichard Henderson * @addr: virtual address 172f168808dSRichard Henderson * @access_type: read, write or execute 173f168808dSRichard Henderson * @mmu_idx: mmu context 174f168808dSRichard Henderson * @memop: memory operation for the access 175f168808dSRichard Henderson * @size: memory access size, or 0 for whole page 176f168808dSRichard Henderson * @probe: test only, no fault 177f168808dSRichard Henderson * @ra: host return address for exception unwind 178f168808dSRichard Henderson * 179f168808dSRichard Henderson * If the access is valid, fill in @out and return true. 180f168808dSRichard Henderson * Otherwise if probe is true, return false. 181f168808dSRichard Henderson * Otherwise raise an exception and do not return. 182f168808dSRichard Henderson * 183f168808dSRichard Henderson * The alignment check for the access is deferred to this hook, 184f168808dSRichard Henderson * so that the target can determine the priority of any alignment 185f168808dSRichard Henderson * fault with respect to other potential faults from paging. 186f168808dSRichard Henderson * Zero may be passed for @memop to skip any alignment check 187f168808dSRichard Henderson * for non-memory-access operations such as probing. 188f168808dSRichard Henderson */ 189f168808dSRichard Henderson bool (*tlb_fill_align)(CPUState *cpu, CPUTLBEntryFull *out, vaddr addr, 190f168808dSRichard Henderson MMUAccessType access_type, int mmu_idx, 191f168808dSRichard Henderson MemOp memop, int size, bool probe, uintptr_t ra); 192f168808dSRichard Henderson /** 193eeca7dc5SRichard Henderson * @tlb_fill: Handle a softmmu tlb miss 194eeca7dc5SRichard Henderson * 195eeca7dc5SRichard Henderson * If the access is valid, call tlb_set_page and return true; 196eeca7dc5SRichard Henderson * if the access is invalid and probe is true, return false; 197eeca7dc5SRichard Henderson * otherwise raise an exception and do not return. 198eeca7dc5SRichard Henderson */ 199eeca7dc5SRichard Henderson bool (*tlb_fill)(CPUState *cpu, vaddr address, int size, 200eeca7dc5SRichard Henderson MMUAccessType access_type, int mmu_idx, 201eeca7dc5SRichard Henderson bool probe, uintptr_t retaddr); 202eeca7dc5SRichard Henderson /** 20378271684SClaudio Fontana * @do_transaction_failed: Callback for handling failed memory transactions 20478271684SClaudio Fontana * (ie bus faults or external aborts; not MMU faults) 20578271684SClaudio Fontana */ 20678271684SClaudio Fontana void (*do_transaction_failed)(CPUState *cpu, hwaddr physaddr, vaddr addr, 20778271684SClaudio Fontana unsigned size, MMUAccessType access_type, 20878271684SClaudio Fontana int mmu_idx, MemTxAttrs attrs, 20978271684SClaudio Fontana MemTxResult response, uintptr_t retaddr); 21078271684SClaudio Fontana /** 21178271684SClaudio Fontana * @do_unaligned_access: Callback for unaligned access handling 212fa947a66SRichard Henderson * The callback must exit via raising an exception. 21378271684SClaudio Fontana */ 2148905770bSMarc-André Lureau G_NORETURN void (*do_unaligned_access)(CPUState *cpu, vaddr addr, 21578271684SClaudio Fontana MMUAccessType access_type, 2168905770bSMarc-André Lureau int mmu_idx, uintptr_t retaddr); 21778271684SClaudio Fontana 21878271684SClaudio Fontana /** 21978271684SClaudio Fontana * @adjust_watchpoint_address: hack for cpu_check_watchpoint used by ARM 22078271684SClaudio Fontana */ 22178271684SClaudio Fontana vaddr (*adjust_watchpoint_address)(CPUState *cpu, vaddr addr, int len); 22278271684SClaudio Fontana 22378271684SClaudio Fontana /** 22478271684SClaudio Fontana * @debug_check_watchpoint: return true if the architectural 22578271684SClaudio Fontana * watchpoint whose address has matched should really fire, used by ARM 226013577deSBin Meng * and RISC-V 22778271684SClaudio Fontana */ 22878271684SClaudio Fontana bool (*debug_check_watchpoint)(CPUState *cpu, CPUWatchpoint *wp); 22978271684SClaudio Fontana 230d9bcb58aSRichard Henderson /** 231e3f7c801SRichard Henderson * @debug_check_breakpoint: return true if the architectural 232e3f7c801SRichard Henderson * breakpoint whose PC has matched should really fire. 233e3f7c801SRichard Henderson */ 234e3f7c801SRichard Henderson bool (*debug_check_breakpoint)(CPUState *cpu); 235e3f7c801SRichard Henderson 236e3f7c801SRichard Henderson /** 237d9bcb58aSRichard Henderson * @io_recompile_replay_branch: Callback for cpu_io_recompile. 238d9bcb58aSRichard Henderson * 239d9bcb58aSRichard Henderson * The cpu has been stopped, and cpu_restore_state_from_tb has been 240d9bcb58aSRichard Henderson * called. If the faulting instruction is in a delay slot, and the 241d9bcb58aSRichard Henderson * target architecture requires re-execution of the branch, then 242d9bcb58aSRichard Henderson * adjust the cpu state as required and return true. 243d9bcb58aSRichard Henderson */ 244d9bcb58aSRichard Henderson bool (*io_recompile_replay_branch)(CPUState *cpu, 245d9bcb58aSRichard Henderson const TranslationBlock *tb); 2460fdc69b7SPhilippe Mathieu-Daudé /** 2470fdc69b7SPhilippe Mathieu-Daudé * @need_replay_interrupt: Return %true if @interrupt_request 2480fdc69b7SPhilippe Mathieu-Daudé * needs to be recorded for replay purposes. 2490fdc69b7SPhilippe Mathieu-Daudé */ 2500fdc69b7SPhilippe Mathieu-Daudé bool (*need_replay_interrupt)(int interrupt_request); 25175fe97b4SPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */ 25278271684SClaudio Fontana }; 25378271684SClaudio Fontana 2546eece7f5SPhilippe Mathieu-Daudé #if defined(CONFIG_USER_ONLY) 2556eece7f5SPhilippe Mathieu-Daudé 2566eece7f5SPhilippe Mathieu-Daudé static inline void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len, 2576eece7f5SPhilippe Mathieu-Daudé MemTxAttrs atr, int fl, uintptr_t ra) 2586eece7f5SPhilippe Mathieu-Daudé { 2596eece7f5SPhilippe Mathieu-Daudé } 2606eece7f5SPhilippe Mathieu-Daudé 2616eece7f5SPhilippe Mathieu-Daudé static inline int cpu_watchpoint_address_matches(CPUState *cpu, 2626eece7f5SPhilippe Mathieu-Daudé vaddr addr, vaddr len) 2636eece7f5SPhilippe Mathieu-Daudé { 2646eece7f5SPhilippe Mathieu-Daudé return 0; 2656eece7f5SPhilippe Mathieu-Daudé } 2666eece7f5SPhilippe Mathieu-Daudé 2676eece7f5SPhilippe Mathieu-Daudé #else 2686eece7f5SPhilippe Mathieu-Daudé 2696eece7f5SPhilippe Mathieu-Daudé /** 2706eece7f5SPhilippe Mathieu-Daudé * cpu_check_watchpoint: 2716eece7f5SPhilippe Mathieu-Daudé * @cpu: cpu context 2726eece7f5SPhilippe Mathieu-Daudé * @addr: guest virtual address 2736eece7f5SPhilippe Mathieu-Daudé * @len: access length 2746eece7f5SPhilippe Mathieu-Daudé * @attrs: memory access attributes 2756eece7f5SPhilippe Mathieu-Daudé * @flags: watchpoint access type 2766eece7f5SPhilippe Mathieu-Daudé * @ra: unwind return address 2776eece7f5SPhilippe Mathieu-Daudé * 2786eece7f5SPhilippe Mathieu-Daudé * Check for a watchpoint hit in [addr, addr+len) of the type 2796eece7f5SPhilippe Mathieu-Daudé * specified by @flags. Exit via exception with a hit. 2806eece7f5SPhilippe Mathieu-Daudé */ 2816eece7f5SPhilippe Mathieu-Daudé void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len, 2826eece7f5SPhilippe Mathieu-Daudé MemTxAttrs attrs, int flags, uintptr_t ra); 2836eece7f5SPhilippe Mathieu-Daudé 2846eece7f5SPhilippe Mathieu-Daudé /** 2856eece7f5SPhilippe Mathieu-Daudé * cpu_watchpoint_address_matches: 2866eece7f5SPhilippe Mathieu-Daudé * @cpu: cpu context 2876eece7f5SPhilippe Mathieu-Daudé * @addr: guest virtual address 2886eece7f5SPhilippe Mathieu-Daudé * @len: access length 2896eece7f5SPhilippe Mathieu-Daudé * 2906eece7f5SPhilippe Mathieu-Daudé * Return the watchpoint flags that apply to [addr, addr+len). 2916eece7f5SPhilippe Mathieu-Daudé * If no watchpoint is registered for the range, the result is 0. 2926eece7f5SPhilippe Mathieu-Daudé */ 2936eece7f5SPhilippe Mathieu-Daudé int cpu_watchpoint_address_matches(CPUState *cpu, vaddr addr, vaddr len); 2946eece7f5SPhilippe Mathieu-Daudé 2956eece7f5SPhilippe Mathieu-Daudé #endif 2966eece7f5SPhilippe Mathieu-Daudé 29778271684SClaudio Fontana #endif /* TCG_CPU_OPS_H */ 298