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 1378271684SClaudio Fontana #include "hw/core/cpu.h" 1478271684SClaudio Fontana 1578271684SClaudio Fontana struct TCGCPUOps { 1678271684SClaudio Fontana /** 17669dcb60SMichael Tokarev * @initialize: Initialize TCG state 1878271684SClaudio Fontana * 1978271684SClaudio Fontana * Called when the first CPU is realized. 2078271684SClaudio Fontana */ 2178271684SClaudio Fontana void (*initialize)(void); 2278271684SClaudio Fontana /** 2378271684SClaudio Fontana * @synchronize_from_tb: Synchronize state from a TCG #TranslationBlock 2478271684SClaudio Fontana * 2578271684SClaudio Fontana * This is called when we abandon execution of a TB before starting it, 2678271684SClaudio Fontana * and must set all parts of the CPU state which the previous TB in the 2778271684SClaudio Fontana * chain may not have updated. 2878271684SClaudio Fontana * By default, when this is NULL, a call is made to @set_pc(tb->pc). 2978271684SClaudio Fontana * 3078271684SClaudio Fontana * If more state needs to be restored, the target must implement a 3178271684SClaudio Fontana * function to restore all the state, and register it here. 3278271684SClaudio Fontana */ 338349d2aeSRichard Henderson void (*synchronize_from_tb)(CPUState *cpu, const TranslationBlock *tb); 34d2925689SRichard Henderson /** 35d2925689SRichard Henderson * @restore_state_to_opc: Synchronize state from INDEX_op_start_insn 36d2925689SRichard Henderson * 37d2925689SRichard Henderson * This is called when we unwind state in the middle of a TB, 38d2925689SRichard Henderson * usually before raising an exception. Set all part of the CPU 39d2925689SRichard Henderson * state which are tracked insn-by-insn in the target-specific 40d2925689SRichard Henderson * arguments to start_insn, passed as @data. 41d2925689SRichard Henderson */ 42d2925689SRichard Henderson void (*restore_state_to_opc)(CPUState *cpu, const TranslationBlock *tb, 43d2925689SRichard Henderson const uint64_t *data); 44d2925689SRichard Henderson 4578271684SClaudio Fontana /** @cpu_exec_enter: Callback for cpu_exec preparation */ 4678271684SClaudio Fontana void (*cpu_exec_enter)(CPUState *cpu); 4778271684SClaudio Fontana /** @cpu_exec_exit: Callback for cpu_exec cleanup */ 4878271684SClaudio Fontana void (*cpu_exec_exit)(CPUState *cpu); 4978271684SClaudio Fontana /** @debug_excp_handler: Callback for handling debug exceptions */ 5078271684SClaudio Fontana void (*debug_excp_handler)(CPUState *cpu); 5178271684SClaudio Fontana 5278271684SClaudio Fontana #ifdef NEED_CPU_H 5312096421SPhilippe Mathieu-Daudé #if defined(CONFIG_USER_ONLY) && defined(TARGET_I386) 5412096421SPhilippe Mathieu-Daudé /** 5512096421SPhilippe Mathieu-Daudé * @fake_user_interrupt: Callback for 'fake exception' handling. 5612096421SPhilippe Mathieu-Daudé * 5712096421SPhilippe Mathieu-Daudé * Simulate 'fake exception' which will be handled outside the 5812096421SPhilippe Mathieu-Daudé * cpu execution loop (hack for x86 user mode). 5912096421SPhilippe Mathieu-Daudé */ 6012096421SPhilippe Mathieu-Daudé void (*fake_user_interrupt)(CPUState *cpu); 6112096421SPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY || !TARGET_I386 */ 6275fe97b4SPhilippe Mathieu-Daudé #ifdef CONFIG_USER_ONLY 6375fe97b4SPhilippe Mathieu-Daudé /** 6475fe97b4SPhilippe Mathieu-Daudé * record_sigsegv: 6575fe97b4SPhilippe Mathieu-Daudé * @cpu: cpu context 6675fe97b4SPhilippe Mathieu-Daudé * @addr: faulting guest address 6775fe97b4SPhilippe Mathieu-Daudé * @access_type: access was read/write/execute 6875fe97b4SPhilippe Mathieu-Daudé * @maperr: true for invalid page, false for permission fault 6975fe97b4SPhilippe Mathieu-Daudé * @ra: host pc for unwinding 7075fe97b4SPhilippe Mathieu-Daudé * 7175fe97b4SPhilippe Mathieu-Daudé * We are about to raise SIGSEGV with si_code set for @maperr, 7275fe97b4SPhilippe Mathieu-Daudé * and si_addr set for @addr. Record anything further needed 7375fe97b4SPhilippe Mathieu-Daudé * for the signal ucontext_t. 7475fe97b4SPhilippe Mathieu-Daudé * 7575fe97b4SPhilippe Mathieu-Daudé * If the emulated kernel does not provide anything to the signal 7675fe97b4SPhilippe Mathieu-Daudé * handler with anything besides the user context registers, and 7775fe97b4SPhilippe Mathieu-Daudé * the siginfo_t, then this hook need do nothing and may be omitted. 7875fe97b4SPhilippe Mathieu-Daudé * Otherwise, record the data and return; the caller will raise 7975fe97b4SPhilippe Mathieu-Daudé * the signal, unwind the cpu state, and return to the main loop. 8075fe97b4SPhilippe Mathieu-Daudé * 8175fe97b4SPhilippe Mathieu-Daudé * If it is simpler to re-use the sysemu tlb_fill code, @ra is provided 8275fe97b4SPhilippe Mathieu-Daudé * so that a "normal" cpu exception can be raised. In this case, 8375fe97b4SPhilippe Mathieu-Daudé * the signal must be raised by the architecture cpu_loop. 8475fe97b4SPhilippe Mathieu-Daudé */ 8575fe97b4SPhilippe Mathieu-Daudé void (*record_sigsegv)(CPUState *cpu, vaddr addr, 8675fe97b4SPhilippe Mathieu-Daudé MMUAccessType access_type, 8775fe97b4SPhilippe Mathieu-Daudé bool maperr, uintptr_t ra); 8875fe97b4SPhilippe Mathieu-Daudé /** 8975fe97b4SPhilippe Mathieu-Daudé * record_sigbus: 9075fe97b4SPhilippe Mathieu-Daudé * @cpu: cpu context 9175fe97b4SPhilippe Mathieu-Daudé * @addr: misaligned guest address 9275fe97b4SPhilippe Mathieu-Daudé * @access_type: access was read/write/execute 9375fe97b4SPhilippe Mathieu-Daudé * @ra: host pc for unwinding 9475fe97b4SPhilippe Mathieu-Daudé * 9575fe97b4SPhilippe Mathieu-Daudé * We are about to raise SIGBUS with si_code BUS_ADRALN, 9675fe97b4SPhilippe Mathieu-Daudé * and si_addr set for @addr. Record anything further needed 9775fe97b4SPhilippe Mathieu-Daudé * for the signal ucontext_t. 9875fe97b4SPhilippe Mathieu-Daudé * 9975fe97b4SPhilippe Mathieu-Daudé * If the emulated kernel does not provide the signal handler with 10075fe97b4SPhilippe Mathieu-Daudé * anything besides the user context registers, and the siginfo_t, 10175fe97b4SPhilippe Mathieu-Daudé * then this hook need do nothing and may be omitted. 10275fe97b4SPhilippe Mathieu-Daudé * Otherwise, record the data and return; the caller will raise 10375fe97b4SPhilippe Mathieu-Daudé * the signal, unwind the cpu state, and return to the main loop. 10475fe97b4SPhilippe Mathieu-Daudé * 10575fe97b4SPhilippe Mathieu-Daudé * If it is simpler to re-use the sysemu do_unaligned_access code, 10675fe97b4SPhilippe Mathieu-Daudé * @ra is provided so that a "normal" cpu exception can be raised. 10775fe97b4SPhilippe Mathieu-Daudé * In this case, the signal must be raised by the architecture cpu_loop. 10875fe97b4SPhilippe Mathieu-Daudé */ 10975fe97b4SPhilippe Mathieu-Daudé void (*record_sigbus)(CPUState *cpu, vaddr addr, 11075fe97b4SPhilippe Mathieu-Daudé MMUAccessType access_type, uintptr_t ra); 11175fe97b4SPhilippe Mathieu-Daudé #else 112*b11cdf27SAnton Johansson /** @do_interrupt: Callback for interrupt handling. */ 113*b11cdf27SAnton Johansson void (*do_interrupt)(CPUState *cpu); 11477c0fc4eSPhilippe Mathieu-Daudé /** @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec */ 11577c0fc4eSPhilippe Mathieu-Daudé bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request); 11678271684SClaudio Fontana /** 117eeca7dc5SRichard Henderson * @tlb_fill: Handle a softmmu tlb miss 118eeca7dc5SRichard Henderson * 119eeca7dc5SRichard Henderson * If the access is valid, call tlb_set_page and return true; 120eeca7dc5SRichard Henderson * if the access is invalid and probe is true, return false; 121eeca7dc5SRichard Henderson * otherwise raise an exception and do not return. 122eeca7dc5SRichard Henderson */ 123eeca7dc5SRichard Henderson bool (*tlb_fill)(CPUState *cpu, vaddr address, int size, 124eeca7dc5SRichard Henderson MMUAccessType access_type, int mmu_idx, 125eeca7dc5SRichard Henderson bool probe, uintptr_t retaddr); 126eeca7dc5SRichard Henderson /** 12778271684SClaudio Fontana * @do_transaction_failed: Callback for handling failed memory transactions 12878271684SClaudio Fontana * (ie bus faults or external aborts; not MMU faults) 12978271684SClaudio Fontana */ 13078271684SClaudio Fontana void (*do_transaction_failed)(CPUState *cpu, hwaddr physaddr, vaddr addr, 13178271684SClaudio Fontana unsigned size, MMUAccessType access_type, 13278271684SClaudio Fontana int mmu_idx, MemTxAttrs attrs, 13378271684SClaudio Fontana MemTxResult response, uintptr_t retaddr); 13478271684SClaudio Fontana /** 13578271684SClaudio Fontana * @do_unaligned_access: Callback for unaligned access handling 136fa947a66SRichard Henderson * The callback must exit via raising an exception. 13778271684SClaudio Fontana */ 1388905770bSMarc-André Lureau G_NORETURN void (*do_unaligned_access)(CPUState *cpu, vaddr addr, 13978271684SClaudio Fontana MMUAccessType access_type, 1408905770bSMarc-André Lureau int mmu_idx, uintptr_t retaddr); 14178271684SClaudio Fontana 14278271684SClaudio Fontana /** 14378271684SClaudio Fontana * @adjust_watchpoint_address: hack for cpu_check_watchpoint used by ARM 14478271684SClaudio Fontana */ 14578271684SClaudio Fontana vaddr (*adjust_watchpoint_address)(CPUState *cpu, vaddr addr, int len); 14678271684SClaudio Fontana 14778271684SClaudio Fontana /** 14878271684SClaudio Fontana * @debug_check_watchpoint: return true if the architectural 14978271684SClaudio Fontana * watchpoint whose address has matched should really fire, used by ARM 150013577deSBin Meng * and RISC-V 15178271684SClaudio Fontana */ 15278271684SClaudio Fontana bool (*debug_check_watchpoint)(CPUState *cpu, CPUWatchpoint *wp); 15378271684SClaudio Fontana 154d9bcb58aSRichard Henderson /** 155e3f7c801SRichard Henderson * @debug_check_breakpoint: return true if the architectural 156e3f7c801SRichard Henderson * breakpoint whose PC has matched should really fire. 157e3f7c801SRichard Henderson */ 158e3f7c801SRichard Henderson bool (*debug_check_breakpoint)(CPUState *cpu); 159e3f7c801SRichard Henderson 160e3f7c801SRichard Henderson /** 161d9bcb58aSRichard Henderson * @io_recompile_replay_branch: Callback for cpu_io_recompile. 162d9bcb58aSRichard Henderson * 163d9bcb58aSRichard Henderson * The cpu has been stopped, and cpu_restore_state_from_tb has been 164d9bcb58aSRichard Henderson * called. If the faulting instruction is in a delay slot, and the 165d9bcb58aSRichard Henderson * target architecture requires re-execution of the branch, then 166d9bcb58aSRichard Henderson * adjust the cpu state as required and return true. 167d9bcb58aSRichard Henderson */ 168d9bcb58aSRichard Henderson bool (*io_recompile_replay_branch)(CPUState *cpu, 169d9bcb58aSRichard Henderson const TranslationBlock *tb); 17075fe97b4SPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */ 17178271684SClaudio Fontana #endif /* NEED_CPU_H */ 17278271684SClaudio Fontana 17378271684SClaudio Fontana }; 17478271684SClaudio Fontana 1756eece7f5SPhilippe Mathieu-Daudé #if defined(CONFIG_USER_ONLY) 1766eece7f5SPhilippe Mathieu-Daudé 1776eece7f5SPhilippe Mathieu-Daudé static inline void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len, 1786eece7f5SPhilippe Mathieu-Daudé MemTxAttrs atr, int fl, uintptr_t ra) 1796eece7f5SPhilippe Mathieu-Daudé { 1806eece7f5SPhilippe Mathieu-Daudé } 1816eece7f5SPhilippe Mathieu-Daudé 1826eece7f5SPhilippe Mathieu-Daudé static inline int cpu_watchpoint_address_matches(CPUState *cpu, 1836eece7f5SPhilippe Mathieu-Daudé vaddr addr, vaddr len) 1846eece7f5SPhilippe Mathieu-Daudé { 1856eece7f5SPhilippe Mathieu-Daudé return 0; 1866eece7f5SPhilippe Mathieu-Daudé } 1876eece7f5SPhilippe Mathieu-Daudé 1886eece7f5SPhilippe Mathieu-Daudé #else 1896eece7f5SPhilippe Mathieu-Daudé 1906eece7f5SPhilippe Mathieu-Daudé /** 1916eece7f5SPhilippe Mathieu-Daudé * cpu_check_watchpoint: 1926eece7f5SPhilippe Mathieu-Daudé * @cpu: cpu context 1936eece7f5SPhilippe Mathieu-Daudé * @addr: guest virtual address 1946eece7f5SPhilippe Mathieu-Daudé * @len: access length 1956eece7f5SPhilippe Mathieu-Daudé * @attrs: memory access attributes 1966eece7f5SPhilippe Mathieu-Daudé * @flags: watchpoint access type 1976eece7f5SPhilippe Mathieu-Daudé * @ra: unwind return address 1986eece7f5SPhilippe Mathieu-Daudé * 1996eece7f5SPhilippe Mathieu-Daudé * Check for a watchpoint hit in [addr, addr+len) of the type 2006eece7f5SPhilippe Mathieu-Daudé * specified by @flags. Exit via exception with a hit. 2016eece7f5SPhilippe Mathieu-Daudé */ 2026eece7f5SPhilippe Mathieu-Daudé void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len, 2036eece7f5SPhilippe Mathieu-Daudé MemTxAttrs attrs, int flags, uintptr_t ra); 2046eece7f5SPhilippe Mathieu-Daudé 2056eece7f5SPhilippe Mathieu-Daudé /** 2066eece7f5SPhilippe Mathieu-Daudé * cpu_watchpoint_address_matches: 2076eece7f5SPhilippe Mathieu-Daudé * @cpu: cpu context 2086eece7f5SPhilippe Mathieu-Daudé * @addr: guest virtual address 2096eece7f5SPhilippe Mathieu-Daudé * @len: access length 2106eece7f5SPhilippe Mathieu-Daudé * 2116eece7f5SPhilippe Mathieu-Daudé * Return the watchpoint flags that apply to [addr, addr+len). 2126eece7f5SPhilippe Mathieu-Daudé * If no watchpoint is registered for the range, the result is 0. 2136eece7f5SPhilippe Mathieu-Daudé */ 2146eece7f5SPhilippe Mathieu-Daudé int cpu_watchpoint_address_matches(CPUState *cpu, vaddr addr, vaddr len); 2156eece7f5SPhilippe Mathieu-Daudé 2166eece7f5SPhilippe Mathieu-Daudé #endif 2176eece7f5SPhilippe Mathieu-Daudé 21878271684SClaudio Fontana #endif /* TCG_CPU_OPS_H */ 219