xref: /openbmc/qemu/include/hw/core/tcg-cpu-ops.h (revision 5d9a9a61)
1 /*
2  * TCG CPU-specific operations
3  *
4  * Copyright 2021 SUSE LLC
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  */
9 
10 #ifndef TCG_CPU_OPS_H
11 #define TCG_CPU_OPS_H
12 
13 #include "exec/breakpoint.h"
14 #include "exec/hwaddr.h"
15 #include "exec/memattrs.h"
16 #include "exec/mmu-access-type.h"
17 #include "exec/vaddr.h"
18 
19 struct TCGCPUOps {
20     /**
21      * @initialize: Initialize TCG state
22      *
23      * Called when the first CPU is realized.
24      */
25     void (*initialize)(void);
26     /**
27      * @synchronize_from_tb: Synchronize state from a TCG #TranslationBlock
28      *
29      * This is called when we abandon execution of a TB before starting it,
30      * and must set all parts of the CPU state which the previous TB in the
31      * chain may not have updated.
32      * By default, when this is NULL, a call is made to @set_pc(tb->pc).
33      *
34      * If more state needs to be restored, the target must implement a
35      * function to restore all the state, and register it here.
36      */
37     void (*synchronize_from_tb)(CPUState *cpu, const TranslationBlock *tb);
38     /**
39      * @restore_state_to_opc: Synchronize state from INDEX_op_start_insn
40      *
41      * This is called when we unwind state in the middle of a TB,
42      * usually before raising an exception.  Set all part of the CPU
43      * state which are tracked insn-by-insn in the target-specific
44      * arguments to start_insn, passed as @data.
45      */
46     void (*restore_state_to_opc)(CPUState *cpu, const TranslationBlock *tb,
47                                  const uint64_t *data);
48 
49     /** @cpu_exec_enter: Callback for cpu_exec preparation */
50     void (*cpu_exec_enter)(CPUState *cpu);
51     /** @cpu_exec_exit: Callback for cpu_exec cleanup */
52     void (*cpu_exec_exit)(CPUState *cpu);
53     /** @debug_excp_handler: Callback for handling debug exceptions */
54     void (*debug_excp_handler)(CPUState *cpu);
55 
56 #ifdef CONFIG_USER_ONLY
57     /**
58      * @fake_user_interrupt: Callback for 'fake exception' handling.
59      *
60      * Simulate 'fake exception' which will be handled outside the
61      * cpu execution loop (hack for x86 user mode).
62      */
63     void (*fake_user_interrupt)(CPUState *cpu);
64 
65     /**
66      * record_sigsegv:
67      * @cpu: cpu context
68      * @addr: faulting guest address
69      * @access_type: access was read/write/execute
70      * @maperr: true for invalid page, false for permission fault
71      * @ra: host pc for unwinding
72      *
73      * We are about to raise SIGSEGV with si_code set for @maperr,
74      * and si_addr set for @addr.  Record anything further needed
75      * for the signal ucontext_t.
76      *
77      * If the emulated kernel does not provide anything to the signal
78      * handler with anything besides the user context registers, and
79      * the siginfo_t, then this hook need do nothing and may be omitted.
80      * Otherwise, record the data and return; the caller will raise
81      * the signal, unwind the cpu state, and return to the main loop.
82      *
83      * If it is simpler to re-use the sysemu tlb_fill code, @ra is provided
84      * so that a "normal" cpu exception can be raised.  In this case,
85      * the signal must be raised by the architecture cpu_loop.
86      */
87     void (*record_sigsegv)(CPUState *cpu, vaddr addr,
88                            MMUAccessType access_type,
89                            bool maperr, uintptr_t ra);
90     /**
91      * record_sigbus:
92      * @cpu: cpu context
93      * @addr: misaligned guest address
94      * @access_type: access was read/write/execute
95      * @ra: host pc for unwinding
96      *
97      * We are about to raise SIGBUS with si_code BUS_ADRALN,
98      * and si_addr set for @addr.  Record anything further needed
99      * for the signal ucontext_t.
100      *
101      * If the emulated kernel does not provide the signal handler with
102      * anything besides the user context registers, and the siginfo_t,
103      * then this hook need do nothing and may be omitted.
104      * Otherwise, record the data and return; the caller will raise
105      * the signal, unwind the cpu state, and return to the main loop.
106      *
107      * If it is simpler to re-use the sysemu do_unaligned_access code,
108      * @ra is provided so that a "normal" cpu exception can be raised.
109      * In this case, the signal must be raised by the architecture cpu_loop.
110      */
111     void (*record_sigbus)(CPUState *cpu, vaddr addr,
112                           MMUAccessType access_type, uintptr_t ra);
113 #else
114     /** @do_interrupt: Callback for interrupt handling.  */
115     void (*do_interrupt)(CPUState *cpu);
116     /** @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec */
117     bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request);
118     /**
119      * @cpu_exec_halt: Callback for handling halt in cpu_exec.
120      *
121      * The target CPU should do any special processing here that it needs
122      * to do when the CPU is in the halted state.
123      *
124      * Return true to indicate that the CPU should now leave halt, false
125      * if it should remain in the halted state.
126      *
127      * If this method is not provided, the default is to do nothing, and
128      * to leave halt if cpu_has_work() returns true.
129      */
130     bool (*cpu_exec_halt)(CPUState *cpu);
131     /**
132      * @tlb_fill: Handle a softmmu tlb miss
133      *
134      * If the access is valid, call tlb_set_page and return true;
135      * if the access is invalid and probe is true, return false;
136      * otherwise raise an exception and do not return.
137      */
138     bool (*tlb_fill)(CPUState *cpu, vaddr address, int size,
139                      MMUAccessType access_type, int mmu_idx,
140                      bool probe, uintptr_t retaddr);
141     /**
142      * @do_transaction_failed: Callback for handling failed memory transactions
143      * (ie bus faults or external aborts; not MMU faults)
144      */
145     void (*do_transaction_failed)(CPUState *cpu, hwaddr physaddr, vaddr addr,
146                                   unsigned size, MMUAccessType access_type,
147                                   int mmu_idx, MemTxAttrs attrs,
148                                   MemTxResult response, uintptr_t retaddr);
149     /**
150      * @do_unaligned_access: Callback for unaligned access handling
151      * The callback must exit via raising an exception.
152      */
153     G_NORETURN void (*do_unaligned_access)(CPUState *cpu, vaddr addr,
154                                            MMUAccessType access_type,
155                                            int mmu_idx, uintptr_t retaddr);
156 
157     /**
158      * @adjust_watchpoint_address: hack for cpu_check_watchpoint used by ARM
159      */
160     vaddr (*adjust_watchpoint_address)(CPUState *cpu, vaddr addr, int len);
161 
162     /**
163      * @debug_check_watchpoint: return true if the architectural
164      * watchpoint whose address has matched should really fire, used by ARM
165      * and RISC-V
166      */
167     bool (*debug_check_watchpoint)(CPUState *cpu, CPUWatchpoint *wp);
168 
169     /**
170      * @debug_check_breakpoint: return true if the architectural
171      * breakpoint whose PC has matched should really fire.
172      */
173     bool (*debug_check_breakpoint)(CPUState *cpu);
174 
175     /**
176      * @io_recompile_replay_branch: Callback for cpu_io_recompile.
177      *
178      * The cpu has been stopped, and cpu_restore_state_from_tb has been
179      * called.  If the faulting instruction is in a delay slot, and the
180      * target architecture requires re-execution of the branch, then
181      * adjust the cpu state as required and return true.
182      */
183     bool (*io_recompile_replay_branch)(CPUState *cpu,
184                                        const TranslationBlock *tb);
185     /**
186      * @need_replay_interrupt: Return %true if @interrupt_request
187      * needs to be recorded for replay purposes.
188      */
189     bool (*need_replay_interrupt)(int interrupt_request);
190 #endif /* !CONFIG_USER_ONLY */
191 };
192 
193 #if defined(CONFIG_USER_ONLY)
194 
195 static inline void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len,
196                                         MemTxAttrs atr, int fl, uintptr_t ra)
197 {
198 }
199 
200 static inline int cpu_watchpoint_address_matches(CPUState *cpu,
201                                                  vaddr addr, vaddr len)
202 {
203     return 0;
204 }
205 
206 #else
207 
208 /**
209  * cpu_check_watchpoint:
210  * @cpu: cpu context
211  * @addr: guest virtual address
212  * @len: access length
213  * @attrs: memory access attributes
214  * @flags: watchpoint access type
215  * @ra: unwind return address
216  *
217  * Check for a watchpoint hit in [addr, addr+len) of the type
218  * specified by @flags.  Exit via exception with a hit.
219  */
220 void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len,
221                           MemTxAttrs attrs, int flags, uintptr_t ra);
222 
223 /**
224  * cpu_watchpoint_address_matches:
225  * @cpu: cpu context
226  * @addr: guest virtual address
227  * @len: access length
228  *
229  * Return the watchpoint flags that apply to [addr, addr+len).
230  * If no watchpoint is registered for the range, the result is 0.
231  */
232 int cpu_watchpoint_address_matches(CPUState *cpu, vaddr addr, vaddr len);
233 
234 #endif
235 
236 #endif /* TCG_CPU_OPS_H */
237