4 * Generate helpers used by TCG for qemu_ld/st ops and code load
7 * Included from target op helpers and exec.c.
9 * Copyright (c) 2003 Fabrice Bellard
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 #define SDATA_TYPE int64_t
28 #define DATA_TYPE uint64_t
32 #define SDATA_TYPE int32_t
33 #define DATA_TYPE uint32_t
37 #define SDATA_TYPE int16_t
38 #define DATA_TYPE uint16_t
42 #define SDATA_TYPE int8_t
43 #define DATA_TYPE uint8_t
45 #error unsupported data size
49 /* For the benefit of TCG generated code, we want to avoid the complication
50 of ABI-specific return type promotion and always return a value extended
51 to the register size of the host. This is tcg_target_long, except in the
52 case of a 32-bit host and 64-bit data, and for that we always have
53 uint64_t. Don't bother with this widened value for SOFTMMU_CODE_ACCESS. */
54 #if defined(SOFTMMU_CODE_ACCESS) || DATA_SIZE == 8
55 # define WORD_TYPE DATA_TYPE
56 # define USUFFIX SUFFIX
58 # define WORD_TYPE tcg_target_ulong
59 # define USUFFIX glue(u, SUFFIX)
60 # define SSUFFIX glue(s, SUFFIX)
63 #ifdef SOFTMMU_CODE_ACCESS
64 #define READ_ACCESS_TYPE MMU_INST_FETCH
65 #define ADDR_READ addr_code
67 #define READ_ACCESS_TYPE MMU_DATA_LOAD
68 #define ADDR_READ addr_read
72 # define BSWAP(X) bswap64(X)
74 # define BSWAP(X) bswap32(X)
76 # define BSWAP(X) bswap16(X)
81 #ifdef TARGET_WORDS_BIGENDIAN
82 # define TGT_BE(X) (X)
83 # define TGT_LE(X) BSWAP(X)
85 # define TGT_BE(X) BSWAP(X)
86 # define TGT_LE(X) (X)
90 # define helper_le_ld_name glue(glue(helper_ret_ld, USUFFIX), MMUSUFFIX)
91 # define helper_be_ld_name helper_le_ld_name
92 # define helper_le_lds_name glue(glue(helper_ret_ld, SSUFFIX), MMUSUFFIX)
93 # define helper_be_lds_name helper_le_lds_name
94 # define helper_le_st_name glue(glue(helper_ret_st, SUFFIX), MMUSUFFIX)
95 # define helper_be_st_name helper_le_st_name
97 # define helper_le_ld_name glue(glue(helper_le_ld, USUFFIX), MMUSUFFIX)
98 # define helper_be_ld_name glue(glue(helper_be_ld, USUFFIX), MMUSUFFIX)
99 # define helper_le_lds_name glue(glue(helper_le_ld, SSUFFIX), MMUSUFFIX)
100 # define helper_be_lds_name glue(glue(helper_be_ld, SSUFFIX), MMUSUFFIX)
101 # define helper_le_st_name glue(glue(helper_le_st, SUFFIX), MMUSUFFIX)
102 # define helper_be_st_name glue(glue(helper_be_st, SUFFIX), MMUSUFFIX)
105 #ifdef TARGET_WORDS_BIGENDIAN
106 # define helper_te_ld_name helper_be_ld_name
107 # define helper_te_st_name helper_be_st_name
109 # define helper_te_ld_name helper_le_ld_name
110 # define helper_te_st_name helper_le_st_name
113 #ifndef SOFTMMU_CODE_ACCESS
114 static inline DATA_TYPE
glue(io_read
, SUFFIX
)(CPUArchState
*env
,
115 size_t mmu_idx
, size_t index
,
119 CPUIOTLBEntry
*iotlbentry
= &env
->iotlb
[mmu_idx
][index
];
120 return io_readx(env
, iotlbentry
, addr
, retaddr
, DATA_SIZE
);
124 WORD_TYPE
helper_le_ld_name(CPUArchState
*env
, target_ulong addr
,
125 TCGMemOpIdx oi
, uintptr_t retaddr
)
127 unsigned mmu_idx
= get_mmuidx(oi
);
128 int index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
129 target_ulong tlb_addr
= env
->tlb_table
[mmu_idx
][index
].ADDR_READ
;
130 unsigned a_bits
= get_alignment_bits(get_memop(oi
));
134 if (addr
& ((1 << a_bits
) - 1)) {
135 cpu_unaligned_access(ENV_GET_CPU(env
), addr
, READ_ACCESS_TYPE
,
139 /* If the TLB entry is for a different page, reload and try again. */
140 if ((addr
& TARGET_PAGE_MASK
)
141 != (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
142 if (!VICTIM_TLB_HIT(ADDR_READ
, addr
)) {
143 tlb_fill(ENV_GET_CPU(env
), addr
, READ_ACCESS_TYPE
,
146 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].ADDR_READ
;
149 /* Handle an IO access. */
150 if (unlikely(tlb_addr
& ~TARGET_PAGE_MASK
)) {
151 if ((addr
& (DATA_SIZE
- 1)) != 0) {
152 goto do_unaligned_access
;
155 /* ??? Note that the io helpers always read data in the target
156 byte ordering. We should push the LE/BE request down into io. */
157 res
= glue(io_read
, SUFFIX
)(env
, mmu_idx
, index
, addr
, retaddr
);
162 /* Handle slow unaligned access (it spans two pages or IO). */
164 && unlikely((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1
165 >= TARGET_PAGE_SIZE
)) {
166 target_ulong addr1
, addr2
;
167 DATA_TYPE res1
, res2
;
170 addr1
= addr
& ~(DATA_SIZE
- 1);
171 addr2
= addr1
+ DATA_SIZE
;
172 res1
= helper_le_ld_name(env
, addr1
, oi
, retaddr
);
173 res2
= helper_le_ld_name(env
, addr2
, oi
, retaddr
);
174 shift
= (addr
& (DATA_SIZE
- 1)) * 8;
176 /* Little-endian combine. */
177 res
= (res1
>> shift
) | (res2
<< ((DATA_SIZE
* 8) - shift
));
181 haddr
= addr
+ env
->tlb_table
[mmu_idx
][index
].addend
;
183 res
= glue(glue(ld
, LSUFFIX
), _p
)((uint8_t *)haddr
);
185 res
= glue(glue(ld
, LSUFFIX
), _le_p
)((uint8_t *)haddr
);
191 WORD_TYPE
helper_be_ld_name(CPUArchState
*env
, target_ulong addr
,
192 TCGMemOpIdx oi
, uintptr_t retaddr
)
194 unsigned mmu_idx
= get_mmuidx(oi
);
195 int index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
196 target_ulong tlb_addr
= env
->tlb_table
[mmu_idx
][index
].ADDR_READ
;
197 unsigned a_bits
= get_alignment_bits(get_memop(oi
));
201 if (addr
& ((1 << a_bits
) - 1)) {
202 cpu_unaligned_access(ENV_GET_CPU(env
), addr
, READ_ACCESS_TYPE
,
206 /* If the TLB entry is for a different page, reload and try again. */
207 if ((addr
& TARGET_PAGE_MASK
)
208 != (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
209 if (!VICTIM_TLB_HIT(ADDR_READ
, addr
)) {
210 tlb_fill(ENV_GET_CPU(env
), addr
, READ_ACCESS_TYPE
,
213 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].ADDR_READ
;
216 /* Handle an IO access. */
217 if (unlikely(tlb_addr
& ~TARGET_PAGE_MASK
)) {
218 if ((addr
& (DATA_SIZE
- 1)) != 0) {
219 goto do_unaligned_access
;
222 /* ??? Note that the io helpers always read data in the target
223 byte ordering. We should push the LE/BE request down into io. */
224 res
= glue(io_read
, SUFFIX
)(env
, mmu_idx
, index
, addr
, retaddr
);
229 /* Handle slow unaligned access (it spans two pages or IO). */
231 && unlikely((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1
232 >= TARGET_PAGE_SIZE
)) {
233 target_ulong addr1
, addr2
;
234 DATA_TYPE res1
, res2
;
237 addr1
= addr
& ~(DATA_SIZE
- 1);
238 addr2
= addr1
+ DATA_SIZE
;
239 res1
= helper_be_ld_name(env
, addr1
, oi
, retaddr
);
240 res2
= helper_be_ld_name(env
, addr2
, oi
, retaddr
);
241 shift
= (addr
& (DATA_SIZE
- 1)) * 8;
243 /* Big-endian combine. */
244 res
= (res1
<< shift
) | (res2
>> ((DATA_SIZE
* 8) - shift
));
248 haddr
= addr
+ env
->tlb_table
[mmu_idx
][index
].addend
;
249 res
= glue(glue(ld
, LSUFFIX
), _be_p
)((uint8_t *)haddr
);
252 #endif /* DATA_SIZE > 1 */
254 #ifndef SOFTMMU_CODE_ACCESS
256 /* Provide signed versions of the load routines as well. We can of course
257 avoid this for 64-bit data, or for 32-bit data on 32-bit host. */
258 #if DATA_SIZE * 8 < TCG_TARGET_REG_BITS
259 WORD_TYPE
helper_le_lds_name(CPUArchState
*env
, target_ulong addr
,
260 TCGMemOpIdx oi
, uintptr_t retaddr
)
262 return (SDATA_TYPE
)helper_le_ld_name(env
, addr
, oi
, retaddr
);
266 WORD_TYPE
helper_be_lds_name(CPUArchState
*env
, target_ulong addr
,
267 TCGMemOpIdx oi
, uintptr_t retaddr
)
269 return (SDATA_TYPE
)helper_be_ld_name(env
, addr
, oi
, retaddr
);
274 static inline void glue(io_write
, SUFFIX
)(CPUArchState
*env
,
275 size_t mmu_idx
, size_t index
,
280 CPUIOTLBEntry
*iotlbentry
= &env
->iotlb
[mmu_idx
][index
];
281 return io_writex(env
, iotlbentry
, val
, addr
, retaddr
, DATA_SIZE
);
284 void helper_le_st_name(CPUArchState
*env
, target_ulong addr
, DATA_TYPE val
,
285 TCGMemOpIdx oi
, uintptr_t retaddr
)
287 unsigned mmu_idx
= get_mmuidx(oi
);
288 int index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
289 target_ulong tlb_addr
= env
->tlb_table
[mmu_idx
][index
].addr_write
;
290 unsigned a_bits
= get_alignment_bits(get_memop(oi
));
293 if (addr
& ((1 << a_bits
) - 1)) {
294 cpu_unaligned_access(ENV_GET_CPU(env
), addr
, MMU_DATA_STORE
,
298 /* If the TLB entry is for a different page, reload and try again. */
299 if ((addr
& TARGET_PAGE_MASK
)
300 != (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
301 if (!VICTIM_TLB_HIT(addr_write
, addr
)) {
302 tlb_fill(ENV_GET_CPU(env
), addr
, MMU_DATA_STORE
, mmu_idx
, retaddr
);
304 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].addr_write
;
307 /* Handle an IO access. */
308 if (unlikely(tlb_addr
& ~TARGET_PAGE_MASK
)) {
309 if ((addr
& (DATA_SIZE
- 1)) != 0) {
310 goto do_unaligned_access
;
313 /* ??? Note that the io helpers always read data in the target
314 byte ordering. We should push the LE/BE request down into io. */
316 glue(io_write
, SUFFIX
)(env
, mmu_idx
, index
, val
, addr
, retaddr
);
320 /* Handle slow unaligned access (it spans two pages or IO). */
322 && unlikely((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1
323 >= TARGET_PAGE_SIZE
)) {
325 target_ulong page2
, tlb_addr2
;
327 /* Ensure the second page is in the TLB. Note that the first page
328 is already guaranteed to be filled, and that the second page
329 cannot evict the first. */
330 page2
= (addr
+ DATA_SIZE
) & TARGET_PAGE_MASK
;
331 index2
= (page2
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
332 tlb_addr2
= env
->tlb_table
[mmu_idx
][index2
].addr_write
;
333 if (page2
!= (tlb_addr2
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))
334 && !VICTIM_TLB_HIT(addr_write
, page2
)) {
335 tlb_fill(ENV_GET_CPU(env
), page2
, MMU_DATA_STORE
,
339 /* XXX: not efficient, but simple. */
340 /* This loop must go in the forward direction to avoid issues
341 with self-modifying code in Windows 64-bit. */
342 for (i
= 0; i
< DATA_SIZE
; ++i
) {
343 /* Little-endian extract. */
344 uint8_t val8
= val
>> (i
* 8);
345 glue(helper_ret_stb
, MMUSUFFIX
)(env
, addr
+ i
, val8
,
351 haddr
= addr
+ env
->tlb_table
[mmu_idx
][index
].addend
;
353 glue(glue(st
, SUFFIX
), _p
)((uint8_t *)haddr
, val
);
355 glue(glue(st
, SUFFIX
), _le_p
)((uint8_t *)haddr
, val
);
360 void helper_be_st_name(CPUArchState
*env
, target_ulong addr
, DATA_TYPE val
,
361 TCGMemOpIdx oi
, uintptr_t retaddr
)
363 unsigned mmu_idx
= get_mmuidx(oi
);
364 int index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
365 target_ulong tlb_addr
= env
->tlb_table
[mmu_idx
][index
].addr_write
;
366 unsigned a_bits
= get_alignment_bits(get_memop(oi
));
369 if (addr
& ((1 << a_bits
) - 1)) {
370 cpu_unaligned_access(ENV_GET_CPU(env
), addr
, MMU_DATA_STORE
,
374 /* If the TLB entry is for a different page, reload and try again. */
375 if ((addr
& TARGET_PAGE_MASK
)
376 != (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
377 if (!VICTIM_TLB_HIT(addr_write
, addr
)) {
378 tlb_fill(ENV_GET_CPU(env
), addr
, MMU_DATA_STORE
, mmu_idx
, retaddr
);
380 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].addr_write
;
383 /* Handle an IO access. */
384 if (unlikely(tlb_addr
& ~TARGET_PAGE_MASK
)) {
385 if ((addr
& (DATA_SIZE
- 1)) != 0) {
386 goto do_unaligned_access
;
389 /* ??? Note that the io helpers always read data in the target
390 byte ordering. We should push the LE/BE request down into io. */
392 glue(io_write
, SUFFIX
)(env
, mmu_idx
, index
, val
, addr
, retaddr
);
396 /* Handle slow unaligned access (it spans two pages or IO). */
398 && unlikely((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1
399 >= TARGET_PAGE_SIZE
)) {
401 target_ulong page2
, tlb_addr2
;
403 /* Ensure the second page is in the TLB. Note that the first page
404 is already guaranteed to be filled, and that the second page
405 cannot evict the first. */
406 page2
= (addr
+ DATA_SIZE
) & TARGET_PAGE_MASK
;
407 index2
= (page2
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
408 tlb_addr2
= env
->tlb_table
[mmu_idx
][index2
].addr_write
;
409 if (page2
!= (tlb_addr2
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))
410 && !VICTIM_TLB_HIT(addr_write
, page2
)) {
411 tlb_fill(ENV_GET_CPU(env
), page2
, MMU_DATA_STORE
,
415 /* XXX: not efficient, but simple */
416 /* This loop must go in the forward direction to avoid issues
417 with self-modifying code. */
418 for (i
= 0; i
< DATA_SIZE
; ++i
) {
419 /* Big-endian extract. */
420 uint8_t val8
= val
>> (((DATA_SIZE
- 1) * 8) - (i
* 8));
421 glue(helper_ret_stb
, MMUSUFFIX
)(env
, addr
+ i
, val8
,
427 haddr
= addr
+ env
->tlb_table
[mmu_idx
][index
].addend
;
428 glue(glue(st
, SUFFIX
), _be_p
)((uint8_t *)haddr
, val
);
430 #endif /* DATA_SIZE > 1 */
431 #endif /* !defined(SOFTMMU_CODE_ACCESS) */
433 #undef READ_ACCESS_TYPE
448 #undef helper_le_ld_name
449 #undef helper_be_ld_name
450 #undef helper_le_lds_name
451 #undef helper_be_lds_name
452 #undef helper_le_st_name
453 #undef helper_be_st_name
454 #undef helper_te_ld_name
455 #undef helper_te_st_name