4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Generate inline load/store functions for all MMU modes (typically
21 * at least _user and _kernel) as well as _data versions, for all data
24 * Used by target op helpers.
26 * The syntax for the accessors is:
28 * load: cpu_ld{sign}{size}{end}_{mmusuffix}(env, ptr)
29 * cpu_ld{sign}{size}{end}_{mmusuffix}_ra(env, ptr, retaddr)
30 * cpu_ld{sign}{size}{end}_mmuidx_ra(env, ptr, mmu_idx, retaddr)
32 * store: cpu_st{size}{end}_{mmusuffix}(env, ptr, val)
33 * cpu_st{size}{end}_{mmusuffix}_ra(env, ptr, val, retaddr)
34 * cpu_st{size}{end}_mmuidx_ra(env, ptr, val, mmu_idx, retaddr)
37 * (empty): for 32 and 64 bit sizes
48 * (empty): for target native endian, or for 8 bit access
49 * _be: for forced big endian
50 * _le: for forced little endian
52 * mmusuffix is one of the generic suffixes "data" or "code", or "mmuidx".
53 * The "mmuidx" suffix carries an extra mmu_idx argument that specifies
54 * the index to use; the "data" and "code" suffixes take the index from
60 #if defined(CONFIG_USER_ONLY)
61 /* sparc32plus has 64bit long but 32bit space address
62 * this can make bad result with g2h() and h2g()
64 #if TARGET_VIRT_ADDR_SPACE_BITS <= 32
65 typedef uint32_t abi_ptr
;
66 #define TARGET_ABI_FMT_ptr "%x"
68 typedef uint64_t abi_ptr
;
69 #define TARGET_ABI_FMT_ptr "%"PRIx64
72 /* All direct uses of g2h and h2g need to go away for usermode softmmu. */
73 #define g2h(x) ((void *)((unsigned long)(abi_ptr)(x) + guest_base))
75 #if HOST_LONG_BITS <= TARGET_VIRT_ADDR_SPACE_BITS
76 #define guest_addr_valid(x) (1)
78 #define guest_addr_valid(x) ((x) <= GUEST_ADDR_MAX)
80 #define h2g_valid(x) guest_addr_valid((unsigned long)(x) - guest_base)
82 static inline int guest_range_valid(unsigned long start
, unsigned long len
)
84 return len
- 1 <= GUEST_ADDR_MAX
&& start
<= GUEST_ADDR_MAX
- len
+ 1;
87 #define h2g_nocheck(x) ({ \
88 unsigned long __ret = (unsigned long)(x) - guest_base; \
93 /* Check if given address fits target address space */ \
94 assert(h2g_valid(x)); \
98 typedef target_ulong abi_ptr
;
99 #define TARGET_ABI_FMT_ptr TARGET_ABI_FMT_lx
102 uint32_t cpu_ldub_data(CPUArchState
*env
, abi_ptr ptr
);
103 int cpu_ldsb_data(CPUArchState
*env
, abi_ptr ptr
);
105 uint32_t cpu_lduw_be_data(CPUArchState
*env
, abi_ptr ptr
);
106 int cpu_ldsw_be_data(CPUArchState
*env
, abi_ptr ptr
);
107 uint32_t cpu_ldl_be_data(CPUArchState
*env
, abi_ptr ptr
);
108 uint64_t cpu_ldq_be_data(CPUArchState
*env
, abi_ptr ptr
);
110 uint32_t cpu_lduw_le_data(CPUArchState
*env
, abi_ptr ptr
);
111 int cpu_ldsw_le_data(CPUArchState
*env
, abi_ptr ptr
);
112 uint32_t cpu_ldl_le_data(CPUArchState
*env
, abi_ptr ptr
);
113 uint64_t cpu_ldq_le_data(CPUArchState
*env
, abi_ptr ptr
);
115 uint32_t cpu_ldub_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t ra
);
116 int cpu_ldsb_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t ra
);
118 uint32_t cpu_lduw_be_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t ra
);
119 int cpu_ldsw_be_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t ra
);
120 uint32_t cpu_ldl_be_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t ra
);
121 uint64_t cpu_ldq_be_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t ra
);
123 uint32_t cpu_lduw_le_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t ra
);
124 int cpu_ldsw_le_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t ra
);
125 uint32_t cpu_ldl_le_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t ra
);
126 uint64_t cpu_ldq_le_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t ra
);
128 void cpu_stb_data(CPUArchState
*env
, abi_ptr ptr
, uint32_t val
);
130 void cpu_stw_be_data(CPUArchState
*env
, abi_ptr ptr
, uint32_t val
);
131 void cpu_stl_be_data(CPUArchState
*env
, abi_ptr ptr
, uint32_t val
);
132 void cpu_stq_be_data(CPUArchState
*env
, abi_ptr ptr
, uint64_t val
);
134 void cpu_stw_le_data(CPUArchState
*env
, abi_ptr ptr
, uint32_t val
);
135 void cpu_stl_le_data(CPUArchState
*env
, abi_ptr ptr
, uint32_t val
);
136 void cpu_stq_le_data(CPUArchState
*env
, abi_ptr ptr
, uint64_t val
);
138 void cpu_stb_data_ra(CPUArchState
*env
, abi_ptr ptr
,
139 uint32_t val
, uintptr_t ra
);
141 void cpu_stw_be_data_ra(CPUArchState
*env
, abi_ptr ptr
,
142 uint32_t val
, uintptr_t ra
);
143 void cpu_stl_be_data_ra(CPUArchState
*env
, abi_ptr ptr
,
144 uint32_t val
, uintptr_t ra
);
145 void cpu_stq_be_data_ra(CPUArchState
*env
, abi_ptr ptr
,
146 uint64_t val
, uintptr_t ra
);
148 void cpu_stw_le_data_ra(CPUArchState
*env
, abi_ptr ptr
,
149 uint32_t val
, uintptr_t ra
);
150 void cpu_stl_le_data_ra(CPUArchState
*env
, abi_ptr ptr
,
151 uint32_t val
, uintptr_t ra
);
152 void cpu_stq_le_data_ra(CPUArchState
*env
, abi_ptr ptr
,
153 uint64_t val
, uintptr_t ra
);
155 #if defined(CONFIG_USER_ONLY)
157 extern __thread
uintptr_t helper_retaddr
;
159 static inline void set_helper_retaddr(uintptr_t ra
)
163 * Ensure that this write is visible to the SIGSEGV handler that
164 * may be invoked due to a subsequent invalid memory operation.
169 static inline void clear_helper_retaddr(void)
172 * Ensure that previous memory operations have succeeded before
173 * removing the data visible to the signal handler.
180 * Provide the same *_mmuidx_ra interface as for softmmu.
181 * The mmu_idx argument is ignored.
184 static inline uint32_t cpu_ldub_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
185 int mmu_idx
, uintptr_t ra
)
187 return cpu_ldub_data_ra(env
, addr
, ra
);
190 static inline int cpu_ldsb_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
191 int mmu_idx
, uintptr_t ra
)
193 return cpu_ldsb_data_ra(env
, addr
, ra
);
196 static inline uint32_t cpu_lduw_be_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
197 int mmu_idx
, uintptr_t ra
)
199 return cpu_lduw_be_data_ra(env
, addr
, ra
);
202 static inline int cpu_ldsw_be_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
203 int mmu_idx
, uintptr_t ra
)
205 return cpu_ldsw_be_data_ra(env
, addr
, ra
);
208 static inline uint32_t cpu_ldl_be_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
209 int mmu_idx
, uintptr_t ra
)
211 return cpu_ldl_be_data_ra(env
, addr
, ra
);
214 static inline uint64_t cpu_ldq_be_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
215 int mmu_idx
, uintptr_t ra
)
217 return cpu_ldq_be_data_ra(env
, addr
, ra
);
220 static inline uint32_t cpu_lduw_le_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
221 int mmu_idx
, uintptr_t ra
)
223 return cpu_lduw_le_data_ra(env
, addr
, ra
);
226 static inline int cpu_ldsw_le_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
227 int mmu_idx
, uintptr_t ra
)
229 return cpu_ldsw_le_data_ra(env
, addr
, ra
);
232 static inline uint32_t cpu_ldl_le_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
233 int mmu_idx
, uintptr_t ra
)
235 return cpu_ldl_le_data_ra(env
, addr
, ra
);
238 static inline uint64_t cpu_ldq_le_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
239 int mmu_idx
, uintptr_t ra
)
241 return cpu_ldq_le_data_ra(env
, addr
, ra
);
244 static inline void cpu_stb_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
245 uint32_t val
, int mmu_idx
, uintptr_t ra
)
247 cpu_stb_data_ra(env
, addr
, val
, ra
);
250 static inline void cpu_stw_be_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
251 uint32_t val
, int mmu_idx
,
254 cpu_stw_be_data_ra(env
, addr
, val
, ra
);
257 static inline void cpu_stl_be_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
258 uint32_t val
, int mmu_idx
,
261 cpu_stl_be_data_ra(env
, addr
, val
, ra
);
264 static inline void cpu_stq_be_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
265 uint64_t val
, int mmu_idx
,
268 cpu_stq_be_data_ra(env
, addr
, val
, ra
);
271 static inline void cpu_stw_le_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
272 uint32_t val
, int mmu_idx
,
275 cpu_stw_le_data_ra(env
, addr
, val
, ra
);
278 static inline void cpu_stl_le_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
279 uint32_t val
, int mmu_idx
,
282 cpu_stl_le_data_ra(env
, addr
, val
, ra
);
285 static inline void cpu_stq_le_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
286 uint64_t val
, int mmu_idx
,
289 cpu_stq_le_data_ra(env
, addr
, val
, ra
);
294 /* Needed for TCG_OVERSIZED_GUEST */
297 static inline target_ulong
tlb_addr_write(const CPUTLBEntry
*entry
)
299 #if TCG_OVERSIZED_GUEST
300 return entry
->addr_write
;
302 return qatomic_read(&entry
->addr_write
);
306 /* Find the TLB index corresponding to the mmu_idx + address pair. */
307 static inline uintptr_t tlb_index(CPUArchState
*env
, uintptr_t mmu_idx
,
310 uintptr_t size_mask
= env_tlb(env
)->f
[mmu_idx
].mask
>> CPU_TLB_ENTRY_BITS
;
312 return (addr
>> TARGET_PAGE_BITS
) & size_mask
;
315 /* Find the TLB entry corresponding to the mmu_idx + address pair. */
316 static inline CPUTLBEntry
*tlb_entry(CPUArchState
*env
, uintptr_t mmu_idx
,
319 return &env_tlb(env
)->f
[mmu_idx
].table
[tlb_index(env
, mmu_idx
, addr
)];
322 uint32_t cpu_ldub_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
323 int mmu_idx
, uintptr_t ra
);
324 int cpu_ldsb_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
325 int mmu_idx
, uintptr_t ra
);
327 uint32_t cpu_lduw_be_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
328 int mmu_idx
, uintptr_t ra
);
329 int cpu_ldsw_be_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
330 int mmu_idx
, uintptr_t ra
);
331 uint32_t cpu_ldl_be_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
332 int mmu_idx
, uintptr_t ra
);
333 uint64_t cpu_ldq_be_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
334 int mmu_idx
, uintptr_t ra
);
336 uint32_t cpu_lduw_le_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
337 int mmu_idx
, uintptr_t ra
);
338 int cpu_ldsw_le_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
339 int mmu_idx
, uintptr_t ra
);
340 uint32_t cpu_ldl_le_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
341 int mmu_idx
, uintptr_t ra
);
342 uint64_t cpu_ldq_le_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
,
343 int mmu_idx
, uintptr_t ra
);
345 void cpu_stb_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
, uint32_t val
,
346 int mmu_idx
, uintptr_t retaddr
);
348 void cpu_stw_be_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
, uint32_t val
,
349 int mmu_idx
, uintptr_t retaddr
);
350 void cpu_stl_be_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
, uint32_t val
,
351 int mmu_idx
, uintptr_t retaddr
);
352 void cpu_stq_be_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
, uint64_t val
,
353 int mmu_idx
, uintptr_t retaddr
);
355 void cpu_stw_le_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
, uint32_t val
,
356 int mmu_idx
, uintptr_t retaddr
);
357 void cpu_stl_le_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
, uint32_t val
,
358 int mmu_idx
, uintptr_t retaddr
);
359 void cpu_stq_le_mmuidx_ra(CPUArchState
*env
, abi_ptr addr
, uint64_t val
,
360 int mmu_idx
, uintptr_t retaddr
);
362 #endif /* defined(CONFIG_USER_ONLY) */
364 #ifdef TARGET_WORDS_BIGENDIAN
365 # define cpu_lduw_data cpu_lduw_be_data
366 # define cpu_ldsw_data cpu_ldsw_be_data
367 # define cpu_ldl_data cpu_ldl_be_data
368 # define cpu_ldq_data cpu_ldq_be_data
369 # define cpu_lduw_data_ra cpu_lduw_be_data_ra
370 # define cpu_ldsw_data_ra cpu_ldsw_be_data_ra
371 # define cpu_ldl_data_ra cpu_ldl_be_data_ra
372 # define cpu_ldq_data_ra cpu_ldq_be_data_ra
373 # define cpu_lduw_mmuidx_ra cpu_lduw_be_mmuidx_ra
374 # define cpu_ldsw_mmuidx_ra cpu_ldsw_be_mmuidx_ra
375 # define cpu_ldl_mmuidx_ra cpu_ldl_be_mmuidx_ra
376 # define cpu_ldq_mmuidx_ra cpu_ldq_be_mmuidx_ra
377 # define cpu_stw_data cpu_stw_be_data
378 # define cpu_stl_data cpu_stl_be_data
379 # define cpu_stq_data cpu_stq_be_data
380 # define cpu_stw_data_ra cpu_stw_be_data_ra
381 # define cpu_stl_data_ra cpu_stl_be_data_ra
382 # define cpu_stq_data_ra cpu_stq_be_data_ra
383 # define cpu_stw_mmuidx_ra cpu_stw_be_mmuidx_ra
384 # define cpu_stl_mmuidx_ra cpu_stl_be_mmuidx_ra
385 # define cpu_stq_mmuidx_ra cpu_stq_be_mmuidx_ra
387 # define cpu_lduw_data cpu_lduw_le_data
388 # define cpu_ldsw_data cpu_ldsw_le_data
389 # define cpu_ldl_data cpu_ldl_le_data
390 # define cpu_ldq_data cpu_ldq_le_data
391 # define cpu_lduw_data_ra cpu_lduw_le_data_ra
392 # define cpu_ldsw_data_ra cpu_ldsw_le_data_ra
393 # define cpu_ldl_data_ra cpu_ldl_le_data_ra
394 # define cpu_ldq_data_ra cpu_ldq_le_data_ra
395 # define cpu_lduw_mmuidx_ra cpu_lduw_le_mmuidx_ra
396 # define cpu_ldsw_mmuidx_ra cpu_ldsw_le_mmuidx_ra
397 # define cpu_ldl_mmuidx_ra cpu_ldl_le_mmuidx_ra
398 # define cpu_ldq_mmuidx_ra cpu_ldq_le_mmuidx_ra
399 # define cpu_stw_data cpu_stw_le_data
400 # define cpu_stl_data cpu_stl_le_data
401 # define cpu_stq_data cpu_stq_le_data
402 # define cpu_stw_data_ra cpu_stw_le_data_ra
403 # define cpu_stl_data_ra cpu_stl_le_data_ra
404 # define cpu_stq_data_ra cpu_stq_le_data_ra
405 # define cpu_stw_mmuidx_ra cpu_stw_le_mmuidx_ra
406 # define cpu_stl_mmuidx_ra cpu_stl_le_mmuidx_ra
407 # define cpu_stq_mmuidx_ra cpu_stq_le_mmuidx_ra
410 uint32_t cpu_ldub_code(CPUArchState
*env
, abi_ptr addr
);
411 uint32_t cpu_lduw_code(CPUArchState
*env
, abi_ptr addr
);
412 uint32_t cpu_ldl_code(CPUArchState
*env
, abi_ptr addr
);
413 uint64_t cpu_ldq_code(CPUArchState
*env
, abi_ptr addr
);
415 static inline int cpu_ldsb_code(CPUArchState
*env
, abi_ptr addr
)
417 return (int8_t)cpu_ldub_code(env
, addr
);
420 static inline int cpu_ldsw_code(CPUArchState
*env
, abi_ptr addr
)
422 return (int16_t)cpu_lduw_code(env
, addr
);
428 * @addr: guest virtual address to look up
429 * @access_type: 0 for read, 1 for write, 2 for execute
430 * @mmu_idx: MMU index to use for lookup
432 * Look up the specified guest virtual index in the TCG softmmu TLB.
433 * If we can translate a host virtual address suitable for direct RAM
434 * access, without causing a guest exception, then return it.
435 * Otherwise (TLB entry is for an I/O access, guest software
436 * TLB fill required, etc) return NULL.
438 #ifdef CONFIG_USER_ONLY
439 static inline void *tlb_vaddr_to_host(CPUArchState
*env
, abi_ptr addr
,
440 MMUAccessType access_type
, int mmu_idx
)
445 void *tlb_vaddr_to_host(CPUArchState
*env
, abi_ptr addr
,
446 MMUAccessType access_type
, int mmu_idx
);
449 #endif /* CPU_LDST_H */