xref: /qemu/include/accel/tcg/cpu-ops.h (revision 0487c631801bf21e5e2ca8a54bf207fb78bd64bf)
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"
1676d07d32SPhilippe Mathieu-Daudé #include "exec/mmu-access-type.h"
1776d07d32SPhilippe Mathieu-Daudé #include "exec/vaddr.h"
1878271684SClaudio Fontana 
1978271684SClaudio Fontana struct TCGCPUOps {
2078271684SClaudio Fontana     /**
21669dcb60SMichael Tokarev      * @initialize: Initialize TCG state
2278271684SClaudio Fontana      *
2378271684SClaudio Fontana      * Called when the first CPU is realized.
2478271684SClaudio Fontana      */
2578271684SClaudio Fontana     void (*initialize)(void);
2678271684SClaudio Fontana     /**
2778271684SClaudio Fontana      * @synchronize_from_tb: Synchronize state from a TCG #TranslationBlock
2878271684SClaudio Fontana      *
2978271684SClaudio Fontana      * This is called when we abandon execution of a TB before starting it,
3078271684SClaudio Fontana      * and must set all parts of the CPU state which the previous TB in the
3178271684SClaudio Fontana      * chain may not have updated.
3278271684SClaudio Fontana      * By default, when this is NULL, a call is made to @set_pc(tb->pc).
3378271684SClaudio Fontana      *
3478271684SClaudio Fontana      * If more state needs to be restored, the target must implement a
3578271684SClaudio Fontana      * function to restore all the state, and register it here.
3678271684SClaudio Fontana      */
378349d2aeSRichard Henderson     void (*synchronize_from_tb)(CPUState *cpu, const TranslationBlock *tb);
38d2925689SRichard Henderson     /**
39d2925689SRichard Henderson      * @restore_state_to_opc: Synchronize state from INDEX_op_start_insn
40d2925689SRichard Henderson      *
41d2925689SRichard Henderson      * This is called when we unwind state in the middle of a TB,
42d2925689SRichard Henderson      * usually before raising an exception.  Set all part of the CPU
43d2925689SRichard Henderson      * state which are tracked insn-by-insn in the target-specific
44d2925689SRichard Henderson      * arguments to start_insn, passed as @data.
45d2925689SRichard Henderson      */
46d2925689SRichard Henderson     void (*restore_state_to_opc)(CPUState *cpu, const TranslationBlock *tb,
47d2925689SRichard Henderson                                  const uint64_t *data);
48d2925689SRichard Henderson 
4978271684SClaudio Fontana     /** @cpu_exec_enter: Callback for cpu_exec preparation */
5078271684SClaudio Fontana     void (*cpu_exec_enter)(CPUState *cpu);
5178271684SClaudio Fontana     /** @cpu_exec_exit: Callback for cpu_exec cleanup */
5278271684SClaudio Fontana     void (*cpu_exec_exit)(CPUState *cpu);
5378271684SClaudio Fontana     /** @debug_excp_handler: Callback for handling debug exceptions */
5478271684SClaudio Fontana     void (*debug_excp_handler)(CPUState *cpu);
5578271684SClaudio Fontana 
56fd3f7d24SAnton Johansson #ifdef CONFIG_USER_ONLY
5712096421SPhilippe Mathieu-Daudé     /**
5812096421SPhilippe Mathieu-Daudé      * @fake_user_interrupt: Callback for 'fake exception' handling.
5912096421SPhilippe Mathieu-Daudé      *
6012096421SPhilippe Mathieu-Daudé      * Simulate 'fake exception' which will be handled outside the
6112096421SPhilippe Mathieu-Daudé      * cpu execution loop (hack for x86 user mode).
6212096421SPhilippe Mathieu-Daudé      */
6312096421SPhilippe Mathieu-Daudé     void (*fake_user_interrupt)(CPUState *cpu);
64fd3f7d24SAnton Johansson 
6575fe97b4SPhilippe Mathieu-Daudé     /**
6675fe97b4SPhilippe Mathieu-Daudé      * record_sigsegv:
6775fe97b4SPhilippe Mathieu-Daudé      * @cpu: cpu context
6875fe97b4SPhilippe Mathieu-Daudé      * @addr: faulting guest address
6975fe97b4SPhilippe Mathieu-Daudé      * @access_type: access was read/write/execute
7075fe97b4SPhilippe Mathieu-Daudé      * @maperr: true for invalid page, false for permission fault
7175fe97b4SPhilippe Mathieu-Daudé      * @ra: host pc for unwinding
7275fe97b4SPhilippe Mathieu-Daudé      *
7375fe97b4SPhilippe Mathieu-Daudé      * We are about to raise SIGSEGV with si_code set for @maperr,
7475fe97b4SPhilippe Mathieu-Daudé      * and si_addr set for @addr.  Record anything further needed
7575fe97b4SPhilippe Mathieu-Daudé      * for the signal ucontext_t.
7675fe97b4SPhilippe Mathieu-Daudé      *
7775fe97b4SPhilippe Mathieu-Daudé      * If the emulated kernel does not provide anything to the signal
7875fe97b4SPhilippe Mathieu-Daudé      * handler with anything besides the user context registers, and
7975fe97b4SPhilippe Mathieu-Daudé      * the siginfo_t, then this hook need do nothing and may be omitted.
8075fe97b4SPhilippe Mathieu-Daudé      * Otherwise, record the data and return; the caller will raise
8175fe97b4SPhilippe Mathieu-Daudé      * the signal, unwind the cpu state, and return to the main loop.
8275fe97b4SPhilippe Mathieu-Daudé      *
8375fe97b4SPhilippe Mathieu-Daudé      * If it is simpler to re-use the sysemu tlb_fill code, @ra is provided
8475fe97b4SPhilippe Mathieu-Daudé      * so that a "normal" cpu exception can be raised.  In this case,
8575fe97b4SPhilippe Mathieu-Daudé      * the signal must be raised by the architecture cpu_loop.
8675fe97b4SPhilippe Mathieu-Daudé      */
8775fe97b4SPhilippe Mathieu-Daudé     void (*record_sigsegv)(CPUState *cpu, vaddr addr,
8875fe97b4SPhilippe Mathieu-Daudé                            MMUAccessType access_type,
8975fe97b4SPhilippe Mathieu-Daudé                            bool maperr, uintptr_t ra);
9075fe97b4SPhilippe Mathieu-Daudé     /**
9175fe97b4SPhilippe Mathieu-Daudé      * record_sigbus:
9275fe97b4SPhilippe Mathieu-Daudé      * @cpu: cpu context
9375fe97b4SPhilippe Mathieu-Daudé      * @addr: misaligned guest address
9475fe97b4SPhilippe Mathieu-Daudé      * @access_type: access was read/write/execute
9575fe97b4SPhilippe Mathieu-Daudé      * @ra: host pc for unwinding
9675fe97b4SPhilippe Mathieu-Daudé      *
9775fe97b4SPhilippe Mathieu-Daudé      * We are about to raise SIGBUS with si_code BUS_ADRALN,
9875fe97b4SPhilippe Mathieu-Daudé      * and si_addr set for @addr.  Record anything further needed
9975fe97b4SPhilippe Mathieu-Daudé      * for the signal ucontext_t.
10075fe97b4SPhilippe Mathieu-Daudé      *
10175fe97b4SPhilippe Mathieu-Daudé      * If the emulated kernel does not provide the signal handler with
10275fe97b4SPhilippe Mathieu-Daudé      * anything besides the user context registers, and the siginfo_t,
10375fe97b4SPhilippe Mathieu-Daudé      * then this hook need do nothing and may be omitted.
10475fe97b4SPhilippe Mathieu-Daudé      * Otherwise, record the data and return; the caller will raise
10575fe97b4SPhilippe Mathieu-Daudé      * the signal, unwind the cpu state, and return to the main loop.
10675fe97b4SPhilippe Mathieu-Daudé      *
10775fe97b4SPhilippe Mathieu-Daudé      * If it is simpler to re-use the sysemu do_unaligned_access code,
10875fe97b4SPhilippe Mathieu-Daudé      * @ra is provided so that a "normal" cpu exception can be raised.
10975fe97b4SPhilippe Mathieu-Daudé      * In this case, the signal must be raised by the architecture cpu_loop.
11075fe97b4SPhilippe Mathieu-Daudé      */
11175fe97b4SPhilippe Mathieu-Daudé     void (*record_sigbus)(CPUState *cpu, vaddr addr,
11275fe97b4SPhilippe Mathieu-Daudé                           MMUAccessType access_type, uintptr_t ra);
11375fe97b4SPhilippe Mathieu-Daudé #else
114b11cdf27SAnton Johansson     /** @do_interrupt: Callback for interrupt handling.  */
115b11cdf27SAnton Johansson     void (*do_interrupt)(CPUState *cpu);
11677c0fc4eSPhilippe Mathieu-Daudé     /** @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec */
11777c0fc4eSPhilippe Mathieu-Daudé     bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request);
118408b2b3dSPeter Maydell     /**
119408b2b3dSPeter Maydell      * @cpu_exec_halt: Callback for handling halt in cpu_exec.
120408b2b3dSPeter Maydell      *
121408b2b3dSPeter Maydell      * The target CPU should do any special processing here that it needs
122408b2b3dSPeter Maydell      * to do when the CPU is in the halted state.
123408b2b3dSPeter Maydell      *
124408b2b3dSPeter Maydell      * Return true to indicate that the CPU should now leave halt, false
125*0487c631SPeter Maydell      * if it should remain in the halted state. (This should generally
126*0487c631SPeter Maydell      * be the same value that cpu_has_work() would return.)
127408b2b3dSPeter Maydell      *
128*0487c631SPeter Maydell      * This method must be provided. If the target does not need to
129*0487c631SPeter Maydell      * do anything special for halt, the same function used for its
130*0487c631SPeter Maydell      * CPUClass::has_work method can be used here, as they have the
131*0487c631SPeter Maydell      * same function signature.
132408b2b3dSPeter Maydell      */
133408b2b3dSPeter Maydell     bool (*cpu_exec_halt)(CPUState *cpu);
13478271684SClaudio Fontana     /**
135eeca7dc5SRichard Henderson      * @tlb_fill: Handle a softmmu tlb miss
136eeca7dc5SRichard Henderson      *
137eeca7dc5SRichard Henderson      * If the access is valid, call tlb_set_page and return true;
138eeca7dc5SRichard Henderson      * if the access is invalid and probe is true, return false;
139eeca7dc5SRichard Henderson      * otherwise raise an exception and do not return.
140eeca7dc5SRichard Henderson      */
141eeca7dc5SRichard Henderson     bool (*tlb_fill)(CPUState *cpu, vaddr address, int size,
142eeca7dc5SRichard Henderson                      MMUAccessType access_type, int mmu_idx,
143eeca7dc5SRichard Henderson                      bool probe, uintptr_t retaddr);
144eeca7dc5SRichard Henderson     /**
14578271684SClaudio Fontana      * @do_transaction_failed: Callback for handling failed memory transactions
14678271684SClaudio Fontana      * (ie bus faults or external aborts; not MMU faults)
14778271684SClaudio Fontana      */
14878271684SClaudio Fontana     void (*do_transaction_failed)(CPUState *cpu, hwaddr physaddr, vaddr addr,
14978271684SClaudio Fontana                                   unsigned size, MMUAccessType access_type,
15078271684SClaudio Fontana                                   int mmu_idx, MemTxAttrs attrs,
15178271684SClaudio Fontana                                   MemTxResult response, uintptr_t retaddr);
15278271684SClaudio Fontana     /**
15378271684SClaudio Fontana      * @do_unaligned_access: Callback for unaligned access handling
154fa947a66SRichard Henderson      * The callback must exit via raising an exception.
15578271684SClaudio Fontana      */
1568905770bSMarc-André Lureau     G_NORETURN void (*do_unaligned_access)(CPUState *cpu, vaddr addr,
15778271684SClaudio Fontana                                            MMUAccessType access_type,
1588905770bSMarc-André Lureau                                            int mmu_idx, uintptr_t retaddr);
15978271684SClaudio Fontana 
16078271684SClaudio Fontana     /**
16178271684SClaudio Fontana      * @adjust_watchpoint_address: hack for cpu_check_watchpoint used by ARM
16278271684SClaudio Fontana      */
16378271684SClaudio Fontana     vaddr (*adjust_watchpoint_address)(CPUState *cpu, vaddr addr, int len);
16478271684SClaudio Fontana 
16578271684SClaudio Fontana     /**
16678271684SClaudio Fontana      * @debug_check_watchpoint: return true if the architectural
16778271684SClaudio Fontana      * watchpoint whose address has matched should really fire, used by ARM
168013577deSBin Meng      * and RISC-V
16978271684SClaudio Fontana      */
17078271684SClaudio Fontana     bool (*debug_check_watchpoint)(CPUState *cpu, CPUWatchpoint *wp);
17178271684SClaudio Fontana 
172d9bcb58aSRichard Henderson     /**
173e3f7c801SRichard Henderson      * @debug_check_breakpoint: return true if the architectural
174e3f7c801SRichard Henderson      * breakpoint whose PC has matched should really fire.
175e3f7c801SRichard Henderson      */
176e3f7c801SRichard Henderson     bool (*debug_check_breakpoint)(CPUState *cpu);
177e3f7c801SRichard Henderson 
178e3f7c801SRichard Henderson     /**
179d9bcb58aSRichard Henderson      * @io_recompile_replay_branch: Callback for cpu_io_recompile.
180d9bcb58aSRichard Henderson      *
181d9bcb58aSRichard Henderson      * The cpu has been stopped, and cpu_restore_state_from_tb has been
182d9bcb58aSRichard Henderson      * called.  If the faulting instruction is in a delay slot, and the
183d9bcb58aSRichard Henderson      * target architecture requires re-execution of the branch, then
184d9bcb58aSRichard Henderson      * adjust the cpu state as required and return true.
185d9bcb58aSRichard Henderson      */
186d9bcb58aSRichard Henderson     bool (*io_recompile_replay_branch)(CPUState *cpu,
187d9bcb58aSRichard Henderson                                        const TranslationBlock *tb);
1880fdc69b7SPhilippe Mathieu-Daudé     /**
1890fdc69b7SPhilippe Mathieu-Daudé      * @need_replay_interrupt: Return %true if @interrupt_request
1900fdc69b7SPhilippe Mathieu-Daudé      * needs to be recorded for replay purposes.
1910fdc69b7SPhilippe Mathieu-Daudé      */
1920fdc69b7SPhilippe Mathieu-Daudé     bool (*need_replay_interrupt)(int interrupt_request);
19375fe97b4SPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
19478271684SClaudio Fontana };
19578271684SClaudio Fontana 
1966eece7f5SPhilippe Mathieu-Daudé #if defined(CONFIG_USER_ONLY)
1976eece7f5SPhilippe Mathieu-Daudé 
1986eece7f5SPhilippe Mathieu-Daudé static inline void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len,
1996eece7f5SPhilippe Mathieu-Daudé                                         MemTxAttrs atr, int fl, uintptr_t ra)
2006eece7f5SPhilippe Mathieu-Daudé {
2016eece7f5SPhilippe Mathieu-Daudé }
2026eece7f5SPhilippe Mathieu-Daudé 
2036eece7f5SPhilippe Mathieu-Daudé static inline int cpu_watchpoint_address_matches(CPUState *cpu,
2046eece7f5SPhilippe Mathieu-Daudé                                                  vaddr addr, vaddr len)
2056eece7f5SPhilippe Mathieu-Daudé {
2066eece7f5SPhilippe Mathieu-Daudé     return 0;
2076eece7f5SPhilippe Mathieu-Daudé }
2086eece7f5SPhilippe Mathieu-Daudé 
2096eece7f5SPhilippe Mathieu-Daudé #else
2106eece7f5SPhilippe Mathieu-Daudé 
2116eece7f5SPhilippe Mathieu-Daudé /**
2126eece7f5SPhilippe Mathieu-Daudé  * cpu_check_watchpoint:
2136eece7f5SPhilippe Mathieu-Daudé  * @cpu: cpu context
2146eece7f5SPhilippe Mathieu-Daudé  * @addr: guest virtual address
2156eece7f5SPhilippe Mathieu-Daudé  * @len: access length
2166eece7f5SPhilippe Mathieu-Daudé  * @attrs: memory access attributes
2176eece7f5SPhilippe Mathieu-Daudé  * @flags: watchpoint access type
2186eece7f5SPhilippe Mathieu-Daudé  * @ra: unwind return address
2196eece7f5SPhilippe Mathieu-Daudé  *
2206eece7f5SPhilippe Mathieu-Daudé  * Check for a watchpoint hit in [addr, addr+len) of the type
2216eece7f5SPhilippe Mathieu-Daudé  * specified by @flags.  Exit via exception with a hit.
2226eece7f5SPhilippe Mathieu-Daudé  */
2236eece7f5SPhilippe Mathieu-Daudé void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len,
2246eece7f5SPhilippe Mathieu-Daudé                           MemTxAttrs attrs, int flags, uintptr_t ra);
2256eece7f5SPhilippe Mathieu-Daudé 
2266eece7f5SPhilippe Mathieu-Daudé /**
2276eece7f5SPhilippe Mathieu-Daudé  * cpu_watchpoint_address_matches:
2286eece7f5SPhilippe Mathieu-Daudé  * @cpu: cpu context
2296eece7f5SPhilippe Mathieu-Daudé  * @addr: guest virtual address
2306eece7f5SPhilippe Mathieu-Daudé  * @len: access length
2316eece7f5SPhilippe Mathieu-Daudé  *
2326eece7f5SPhilippe Mathieu-Daudé  * Return the watchpoint flags that apply to [addr, addr+len).
2336eece7f5SPhilippe Mathieu-Daudé  * If no watchpoint is registered for the range, the result is 0.
2346eece7f5SPhilippe Mathieu-Daudé  */
2356eece7f5SPhilippe Mathieu-Daudé int cpu_watchpoint_address_matches(CPUState *cpu, vaddr addr, vaddr len);
2366eece7f5SPhilippe Mathieu-Daudé 
2376eece7f5SPhilippe Mathieu-Daudé #endif
2386eece7f5SPhilippe Mathieu-Daudé 
23978271684SClaudio Fontana #endif /* TCG_CPU_OPS_H */
240