xref: /openbmc/qemu/include/exec/cpu-common.h (revision 61d6a915132db4616f601d5144fdac45f429a4cd)
1 #ifndef CPU_COMMON_H
2 #define CPU_COMMON_H
3 
4 /* CPU interfaces that are target independent.  */
5 
6 #include "exec/vaddr.h"
7 #ifndef CONFIG_USER_ONLY
8 #include "exec/hwaddr.h"
9 #endif
10 
11 #define EXCP_INTERRUPT  0x10000 /* async interruption */
12 #define EXCP_HLT        0x10001 /* hlt instruction reached */
13 #define EXCP_DEBUG      0x10002 /* cpu stopped after a breakpoint or singlestep */
14 #define EXCP_HALTED     0x10003 /* cpu is halted (waiting for external event) */
15 #define EXCP_YIELD      0x10004 /* cpu wants to yield timeslice to another */
16 #define EXCP_ATOMIC     0x10005 /* stop-the-world and emulate atomic */
17 
18 void cpu_exec_init_all(void);
19 void cpu_exec_step_atomic(CPUState *cpu);
20 
21 /* Using intptr_t ensures that qemu_*_page_mask is sign-extended even
22  * when intptr_t is 32-bit and we are aligning a long long.
23  */
24 extern uintptr_t qemu_host_page_size;
25 extern intptr_t qemu_host_page_mask;
26 
27 #define HOST_PAGE_ALIGN(addr) ROUND_UP((addr), qemu_host_page_size)
28 #define REAL_HOST_PAGE_ALIGN(addr) ROUND_UP((addr), qemu_real_host_page_size())
29 
30 /* The CPU list lock nests outside page_(un)lock or mmap_(un)lock */
31 extern QemuMutex qemu_cpu_list_lock;
32 void qemu_init_cpu_list(void);
33 void cpu_list_lock(void);
34 void cpu_list_unlock(void);
35 unsigned int cpu_list_generation_id_get(void);
36 
37 void tcg_iommu_init_notifier_list(CPUState *cpu);
38 void tcg_iommu_free_notifier_list(CPUState *cpu);
39 
40 #if !defined(CONFIG_USER_ONLY)
41 
42 enum device_endian {
43     DEVICE_NATIVE_ENDIAN,
44     DEVICE_BIG_ENDIAN,
45     DEVICE_LITTLE_ENDIAN,
46 };
47 
48 #if HOST_BIG_ENDIAN
49 #define DEVICE_HOST_ENDIAN DEVICE_BIG_ENDIAN
50 #else
51 #define DEVICE_HOST_ENDIAN DEVICE_LITTLE_ENDIAN
52 #endif
53 
54 /* address in the RAM (different from a physical address) */
55 #if defined(CONFIG_XEN_BACKEND)
56 typedef uint64_t ram_addr_t;
57 #  define RAM_ADDR_MAX UINT64_MAX
58 #  define RAM_ADDR_FMT "%" PRIx64
59 #else
60 typedef uintptr_t ram_addr_t;
61 #  define RAM_ADDR_MAX UINTPTR_MAX
62 #  define RAM_ADDR_FMT "%" PRIxPTR
63 #endif
64 
65 /* memory API */
66 
67 void qemu_ram_remap(ram_addr_t addr, ram_addr_t length);
68 /* This should not be used by devices.  */
69 ram_addr_t qemu_ram_addr_from_host(void *ptr);
70 ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr);
71 RAMBlock *qemu_ram_block_by_name(const char *name);
72 
73 /*
74  * Translates a host ptr back to a RAMBlock and an offset in that RAMBlock.
75  *
76  * @ptr: The host pointer to translate.
77  * @round_offset: Whether to round the result offset down to a target page
78  * @offset: Will be set to the offset within the returned RAMBlock.
79  *
80  * Returns: RAMBlock (or NULL if not found)
81  *
82  * By the time this function returns, the returned pointer is not protected
83  * by RCU anymore.  If the caller is not within an RCU critical section and
84  * does not hold the BQL, it must have other means of protecting the
85  * pointer, such as a reference to the memory region that owns the RAMBlock.
86  */
87 RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset,
88                                    ram_addr_t *offset);
89 ram_addr_t qemu_ram_block_host_offset(RAMBlock *rb, void *host);
90 void qemu_ram_set_idstr(RAMBlock *block, const char *name, DeviceState *dev);
91 void qemu_ram_unset_idstr(RAMBlock *block);
92 const char *qemu_ram_get_idstr(RAMBlock *rb);
93 void *qemu_ram_get_host_addr(RAMBlock *rb);
94 ram_addr_t qemu_ram_get_offset(RAMBlock *rb);
95 ram_addr_t qemu_ram_get_used_length(RAMBlock *rb);
96 ram_addr_t qemu_ram_get_max_length(RAMBlock *rb);
97 bool qemu_ram_is_shared(RAMBlock *rb);
98 bool qemu_ram_is_noreserve(RAMBlock *rb);
99 bool qemu_ram_is_uf_zeroable(RAMBlock *rb);
100 void qemu_ram_set_uf_zeroable(RAMBlock *rb);
101 bool qemu_ram_is_migratable(RAMBlock *rb);
102 void qemu_ram_set_migratable(RAMBlock *rb);
103 void qemu_ram_unset_migratable(RAMBlock *rb);
104 bool qemu_ram_is_named_file(RAMBlock *rb);
105 int qemu_ram_get_fd(RAMBlock *rb);
106 
107 size_t qemu_ram_pagesize(RAMBlock *block);
108 size_t qemu_ram_pagesize_largest(void);
109 
110 /**
111  * cpu_address_space_init:
112  * @cpu: CPU to add this address space to
113  * @asidx: integer index of this address space
114  * @prefix: prefix to be used as name of address space
115  * @mr: the root memory region of address space
116  *
117  * Add the specified address space to the CPU's cpu_ases list.
118  * The address space added with @asidx 0 is the one used for the
119  * convenience pointer cpu->as.
120  * The target-specific code which registers ASes is responsible
121  * for defining what semantics address space 0, 1, 2, etc have.
122  *
123  * Before the first call to this function, the caller must set
124  * cpu->num_ases to the total number of address spaces it needs
125  * to support.
126  *
127  * Note that with KVM only one address space is supported.
128  */
129 void cpu_address_space_init(CPUState *cpu, int asidx,
130                             const char *prefix, MemoryRegion *mr);
131 
132 void cpu_physical_memory_rw(hwaddr addr, void *buf,
133                             hwaddr len, bool is_write);
134 static inline void cpu_physical_memory_read(hwaddr addr,
135                                             void *buf, hwaddr len)
136 {
137     cpu_physical_memory_rw(addr, buf, len, false);
138 }
139 static inline void cpu_physical_memory_write(hwaddr addr,
140                                              const void *buf, hwaddr len)
141 {
142     cpu_physical_memory_rw(addr, (void *)buf, len, true);
143 }
144 void *cpu_physical_memory_map(hwaddr addr,
145                               hwaddr *plen,
146                               bool is_write);
147 void cpu_physical_memory_unmap(void *buffer, hwaddr len,
148                                bool is_write, hwaddr access_len);
149 void cpu_register_map_client(QEMUBH *bh);
150 void cpu_unregister_map_client(QEMUBH *bh);
151 
152 bool cpu_physical_memory_is_io(hwaddr phys_addr);
153 
154 /* Coalesced MMIO regions are areas where write operations can be reordered.
155  * This usually implies that write operations are side-effect free.  This allows
156  * batching which can make a major impact on performance when using
157  * virtualization.
158  */
159 void qemu_flush_coalesced_mmio_buffer(void);
160 
161 void cpu_flush_icache_range(hwaddr start, hwaddr len);
162 
163 typedef int (RAMBlockIterFunc)(RAMBlock *rb, void *opaque);
164 
165 int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque);
166 int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length);
167 
168 #endif
169 
170 /* Returns: 0 on success, -1 on error */
171 int cpu_memory_rw_debug(CPUState *cpu, vaddr addr,
172                         void *ptr, size_t len, bool is_write);
173 
174 /* vl.c */
175 void list_cpus(void);
176 
177 #ifdef CONFIG_TCG
178 /**
179  * cpu_unwind_state_data:
180  * @cpu: the cpu context
181  * @host_pc: the host pc within the translation
182  * @data: output data
183  *
184  * Attempt to load the the unwind state for a host pc occurring in
185  * translated code.  If @host_pc is not in translated code, the
186  * function returns false; otherwise @data is loaded.
187  * This is the same unwind info as given to restore_state_to_opc.
188  */
189 bool cpu_unwind_state_data(CPUState *cpu, uintptr_t host_pc, uint64_t *data);
190 
191 /**
192  * cpu_restore_state:
193  * @cpu: the cpu context
194  * @host_pc: the host pc within the translation
195  * @return: true if state was restored, false otherwise
196  *
197  * Attempt to restore the state for a fault occurring in translated
198  * code. If @host_pc is not in translated code no state is
199  * restored and the function returns false.
200  */
201 bool cpu_restore_state(CPUState *cpu, uintptr_t host_pc);
202 
203 G_NORETURN void cpu_loop_exit_noexc(CPUState *cpu);
204 G_NORETURN void cpu_loop_exit_atomic(CPUState *cpu, uintptr_t pc);
205 #endif /* CONFIG_TCG */
206 G_NORETURN void cpu_loop_exit(CPUState *cpu);
207 G_NORETURN void cpu_loop_exit_restore(CPUState *cpu, uintptr_t pc);
208 
209 #endif /* CPU_COMMON_H */
210