tcg-cpu-ops.h 10 KB

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