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 /** 1778271684SClaudio Fontana * @initialize: Initalize 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é #else 6212096421SPhilippe Mathieu-Daudé /** 6312096421SPhilippe Mathieu-Daudé * @do_interrupt: Callback for interrupt handling. 6412096421SPhilippe Mathieu-Daudé */ 6512096421SPhilippe Mathieu-Daudé void (*do_interrupt)(CPUState *cpu); 6612096421SPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY || !TARGET_I386 */ 67*75fe97b4SPhilippe Mathieu-Daudé #ifdef CONFIG_USER_ONLY 68*75fe97b4SPhilippe Mathieu-Daudé /** 69*75fe97b4SPhilippe Mathieu-Daudé * record_sigsegv: 70*75fe97b4SPhilippe Mathieu-Daudé * @cpu: cpu context 71*75fe97b4SPhilippe Mathieu-Daudé * @addr: faulting guest address 72*75fe97b4SPhilippe Mathieu-Daudé * @access_type: access was read/write/execute 73*75fe97b4SPhilippe Mathieu-Daudé * @maperr: true for invalid page, false for permission fault 74*75fe97b4SPhilippe Mathieu-Daudé * @ra: host pc for unwinding 75*75fe97b4SPhilippe Mathieu-Daudé * 76*75fe97b4SPhilippe Mathieu-Daudé * We are about to raise SIGSEGV with si_code set for @maperr, 77*75fe97b4SPhilippe Mathieu-Daudé * and si_addr set for @addr. Record anything further needed 78*75fe97b4SPhilippe Mathieu-Daudé * for the signal ucontext_t. 79*75fe97b4SPhilippe Mathieu-Daudé * 80*75fe97b4SPhilippe Mathieu-Daudé * If the emulated kernel does not provide anything to the signal 81*75fe97b4SPhilippe Mathieu-Daudé * handler with anything besides the user context registers, and 82*75fe97b4SPhilippe Mathieu-Daudé * the siginfo_t, then this hook need do nothing and may be omitted. 83*75fe97b4SPhilippe Mathieu-Daudé * Otherwise, record the data and return; the caller will raise 84*75fe97b4SPhilippe Mathieu-Daudé * the signal, unwind the cpu state, and return to the main loop. 85*75fe97b4SPhilippe Mathieu-Daudé * 86*75fe97b4SPhilippe Mathieu-Daudé * If it is simpler to re-use the sysemu tlb_fill code, @ra is provided 87*75fe97b4SPhilippe Mathieu-Daudé * so that a "normal" cpu exception can be raised. In this case, 88*75fe97b4SPhilippe Mathieu-Daudé * the signal must be raised by the architecture cpu_loop. 89*75fe97b4SPhilippe Mathieu-Daudé */ 90*75fe97b4SPhilippe Mathieu-Daudé void (*record_sigsegv)(CPUState *cpu, vaddr addr, 91*75fe97b4SPhilippe Mathieu-Daudé MMUAccessType access_type, 92*75fe97b4SPhilippe Mathieu-Daudé bool maperr, uintptr_t ra); 93*75fe97b4SPhilippe Mathieu-Daudé /** 94*75fe97b4SPhilippe Mathieu-Daudé * record_sigbus: 95*75fe97b4SPhilippe Mathieu-Daudé * @cpu: cpu context 96*75fe97b4SPhilippe Mathieu-Daudé * @addr: misaligned guest address 97*75fe97b4SPhilippe Mathieu-Daudé * @access_type: access was read/write/execute 98*75fe97b4SPhilippe Mathieu-Daudé * @ra: host pc for unwinding 99*75fe97b4SPhilippe Mathieu-Daudé * 100*75fe97b4SPhilippe Mathieu-Daudé * We are about to raise SIGBUS with si_code BUS_ADRALN, 101*75fe97b4SPhilippe Mathieu-Daudé * and si_addr set for @addr. Record anything further needed 102*75fe97b4SPhilippe Mathieu-Daudé * for the signal ucontext_t. 103*75fe97b4SPhilippe Mathieu-Daudé * 104*75fe97b4SPhilippe Mathieu-Daudé * If the emulated kernel does not provide the signal handler with 105*75fe97b4SPhilippe Mathieu-Daudé * anything besides the user context registers, and the siginfo_t, 106*75fe97b4SPhilippe Mathieu-Daudé * then this hook need do nothing and may be omitted. 107*75fe97b4SPhilippe Mathieu-Daudé * Otherwise, record the data and return; the caller will raise 108*75fe97b4SPhilippe Mathieu-Daudé * the signal, unwind the cpu state, and return to the main loop. 109*75fe97b4SPhilippe Mathieu-Daudé * 110*75fe97b4SPhilippe Mathieu-Daudé * If it is simpler to re-use the sysemu do_unaligned_access code, 111*75fe97b4SPhilippe Mathieu-Daudé * @ra is provided so that a "normal" cpu exception can be raised. 112*75fe97b4SPhilippe Mathieu-Daudé * In this case, the signal must be raised by the architecture cpu_loop. 113*75fe97b4SPhilippe Mathieu-Daudé */ 114*75fe97b4SPhilippe Mathieu-Daudé void (*record_sigbus)(CPUState *cpu, vaddr addr, 115*75fe97b4SPhilippe Mathieu-Daudé MMUAccessType access_type, uintptr_t ra); 116*75fe97b4SPhilippe Mathieu-Daudé #else 11777c0fc4eSPhilippe Mathieu-Daudé /** @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec */ 11877c0fc4eSPhilippe Mathieu-Daudé bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request); 11978271684SClaudio Fontana /** 120eeca7dc5SRichard Henderson * @tlb_fill: Handle a softmmu tlb miss 121eeca7dc5SRichard Henderson * 122eeca7dc5SRichard Henderson * If the access is valid, call tlb_set_page and return true; 123eeca7dc5SRichard Henderson * if the access is invalid and probe is true, return false; 124eeca7dc5SRichard Henderson * otherwise raise an exception and do not return. 125eeca7dc5SRichard Henderson */ 126eeca7dc5SRichard Henderson bool (*tlb_fill)(CPUState *cpu, vaddr address, int size, 127eeca7dc5SRichard Henderson MMUAccessType access_type, int mmu_idx, 128eeca7dc5SRichard Henderson bool probe, uintptr_t retaddr); 129eeca7dc5SRichard Henderson /** 13078271684SClaudio Fontana * @do_transaction_failed: Callback for handling failed memory transactions 13178271684SClaudio Fontana * (ie bus faults or external aborts; not MMU faults) 13278271684SClaudio Fontana */ 13378271684SClaudio Fontana void (*do_transaction_failed)(CPUState *cpu, hwaddr physaddr, vaddr addr, 13478271684SClaudio Fontana unsigned size, MMUAccessType access_type, 13578271684SClaudio Fontana int mmu_idx, MemTxAttrs attrs, 13678271684SClaudio Fontana MemTxResult response, uintptr_t retaddr); 13778271684SClaudio Fontana /** 13878271684SClaudio Fontana * @do_unaligned_access: Callback for unaligned access handling 139fa947a66SRichard Henderson * The callback must exit via raising an exception. 14078271684SClaudio Fontana */ 1418905770bSMarc-André Lureau G_NORETURN void (*do_unaligned_access)(CPUState *cpu, vaddr addr, 14278271684SClaudio Fontana MMUAccessType access_type, 1438905770bSMarc-André Lureau int mmu_idx, uintptr_t retaddr); 14478271684SClaudio Fontana 14578271684SClaudio Fontana /** 14678271684SClaudio Fontana * @adjust_watchpoint_address: hack for cpu_check_watchpoint used by ARM 14778271684SClaudio Fontana */ 14878271684SClaudio Fontana vaddr (*adjust_watchpoint_address)(CPUState *cpu, vaddr addr, int len); 14978271684SClaudio Fontana 15078271684SClaudio Fontana /** 15178271684SClaudio Fontana * @debug_check_watchpoint: return true if the architectural 15278271684SClaudio Fontana * watchpoint whose address has matched should really fire, used by ARM 153013577deSBin Meng * and RISC-V 15478271684SClaudio Fontana */ 15578271684SClaudio Fontana bool (*debug_check_watchpoint)(CPUState *cpu, CPUWatchpoint *wp); 15678271684SClaudio Fontana 157d9bcb58aSRichard Henderson /** 158e3f7c801SRichard Henderson * @debug_check_breakpoint: return true if the architectural 159e3f7c801SRichard Henderson * breakpoint whose PC has matched should really fire. 160e3f7c801SRichard Henderson */ 161e3f7c801SRichard Henderson bool (*debug_check_breakpoint)(CPUState *cpu); 162e3f7c801SRichard Henderson 163e3f7c801SRichard Henderson /** 164d9bcb58aSRichard Henderson * @io_recompile_replay_branch: Callback for cpu_io_recompile. 165d9bcb58aSRichard Henderson * 166d9bcb58aSRichard Henderson * The cpu has been stopped, and cpu_restore_state_from_tb has been 167d9bcb58aSRichard Henderson * called. If the faulting instruction is in a delay slot, and the 168d9bcb58aSRichard Henderson * target architecture requires re-execution of the branch, then 169d9bcb58aSRichard Henderson * adjust the cpu state as required and return true. 170d9bcb58aSRichard Henderson */ 171d9bcb58aSRichard Henderson bool (*io_recompile_replay_branch)(CPUState *cpu, 172d9bcb58aSRichard Henderson const TranslationBlock *tb); 173*75fe97b4SPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */ 17478271684SClaudio Fontana #endif /* NEED_CPU_H */ 17578271684SClaudio Fontana 17678271684SClaudio Fontana }; 17778271684SClaudio Fontana 1786eece7f5SPhilippe Mathieu-Daudé #if defined(CONFIG_USER_ONLY) 1796eece7f5SPhilippe Mathieu-Daudé 1806eece7f5SPhilippe Mathieu-Daudé static inline void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len, 1816eece7f5SPhilippe Mathieu-Daudé MemTxAttrs atr, int fl, uintptr_t ra) 1826eece7f5SPhilippe Mathieu-Daudé { 1836eece7f5SPhilippe Mathieu-Daudé } 1846eece7f5SPhilippe Mathieu-Daudé 1856eece7f5SPhilippe Mathieu-Daudé static inline int cpu_watchpoint_address_matches(CPUState *cpu, 1866eece7f5SPhilippe Mathieu-Daudé vaddr addr, vaddr len) 1876eece7f5SPhilippe Mathieu-Daudé { 1886eece7f5SPhilippe Mathieu-Daudé return 0; 1896eece7f5SPhilippe Mathieu-Daudé } 1906eece7f5SPhilippe Mathieu-Daudé 1916eece7f5SPhilippe Mathieu-Daudé #else 1926eece7f5SPhilippe Mathieu-Daudé 1936eece7f5SPhilippe Mathieu-Daudé /** 1946eece7f5SPhilippe Mathieu-Daudé * cpu_check_watchpoint: 1956eece7f5SPhilippe Mathieu-Daudé * @cpu: cpu context 1966eece7f5SPhilippe Mathieu-Daudé * @addr: guest virtual address 1976eece7f5SPhilippe Mathieu-Daudé * @len: access length 1986eece7f5SPhilippe Mathieu-Daudé * @attrs: memory access attributes 1996eece7f5SPhilippe Mathieu-Daudé * @flags: watchpoint access type 2006eece7f5SPhilippe Mathieu-Daudé * @ra: unwind return address 2016eece7f5SPhilippe Mathieu-Daudé * 2026eece7f5SPhilippe Mathieu-Daudé * Check for a watchpoint hit in [addr, addr+len) of the type 2036eece7f5SPhilippe Mathieu-Daudé * specified by @flags. Exit via exception with a hit. 2046eece7f5SPhilippe Mathieu-Daudé */ 2056eece7f5SPhilippe Mathieu-Daudé void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len, 2066eece7f5SPhilippe Mathieu-Daudé MemTxAttrs attrs, int flags, uintptr_t ra); 2076eece7f5SPhilippe Mathieu-Daudé 2086eece7f5SPhilippe Mathieu-Daudé /** 2096eece7f5SPhilippe Mathieu-Daudé * cpu_watchpoint_address_matches: 2106eece7f5SPhilippe Mathieu-Daudé * @cpu: cpu context 2116eece7f5SPhilippe Mathieu-Daudé * @addr: guest virtual address 2126eece7f5SPhilippe Mathieu-Daudé * @len: access length 2136eece7f5SPhilippe Mathieu-Daudé * 2146eece7f5SPhilippe Mathieu-Daudé * Return the watchpoint flags that apply to [addr, addr+len). 2156eece7f5SPhilippe Mathieu-Daudé * If no watchpoint is registered for the range, the result is 0. 2166eece7f5SPhilippe Mathieu-Daudé */ 2176eece7f5SPhilippe Mathieu-Daudé int cpu_watchpoint_address_matches(CPUState *cpu, vaddr addr, vaddr len); 2186eece7f5SPhilippe Mathieu-Daudé 2196eece7f5SPhilippe Mathieu-Daudé #endif 2206eece7f5SPhilippe Mathieu-Daudé 22178271684SClaudio Fontana #endif /* TCG_CPU_OPS_H */ 222