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/>.
24 #include "qemu/timer.h"
25 #include "exec/address-spaces.h"
26 #include "exec/memory.h"
28 #define DATA_SIZE (1 << SHIFT)
33 #define SDATA_TYPE int64_t
34 #define DATA_TYPE uint64_t
38 #define SDATA_TYPE int32_t
39 #define DATA_TYPE uint32_t
43 #define SDATA_TYPE int16_t
44 #define DATA_TYPE uint16_t
48 #define SDATA_TYPE int8_t
49 #define DATA_TYPE uint8_t
51 #error unsupported data size
55 /* For the benefit of TCG generated code, we want to avoid the complication
56 of ABI-specific return type promotion and always return a value extended
57 to the register size of the host. This is tcg_target_long, except in the
58 case of a 32-bit host and 64-bit data, and for that we always have
59 uint64_t. Don't bother with this widened value for SOFTMMU_CODE_ACCESS. */
60 #if defined(SOFTMMU_CODE_ACCESS) || DATA_SIZE == 8
61 # define WORD_TYPE DATA_TYPE
62 # define USUFFIX SUFFIX
64 # define WORD_TYPE tcg_target_ulong
65 # define USUFFIX glue(u, SUFFIX)
66 # define SSUFFIX glue(s, SUFFIX)
69 #ifdef SOFTMMU_CODE_ACCESS
70 #define READ_ACCESS_TYPE 2
71 #define ADDR_READ addr_code
73 #define READ_ACCESS_TYPE 0
74 #define ADDR_READ addr_read
78 # define BSWAP(X) bswap64(X)
80 # define BSWAP(X) bswap32(X)
82 # define BSWAP(X) bswap16(X)
87 #ifdef TARGET_WORDS_BIGENDIAN
88 # define TGT_BE(X) (X)
89 # define TGT_LE(X) BSWAP(X)
91 # define TGT_BE(X) BSWAP(X)
92 # define TGT_LE(X) (X)
96 # define helper_le_ld_name glue(glue(helper_ret_ld, USUFFIX), MMUSUFFIX)
97 # define helper_be_ld_name helper_le_ld_name
98 # define helper_le_lds_name glue(glue(helper_ret_ld, SSUFFIX), MMUSUFFIX)
99 # define helper_be_lds_name helper_le_lds_name
100 # define helper_le_st_name glue(glue(helper_ret_st, SUFFIX), MMUSUFFIX)
101 # define helper_be_st_name helper_le_st_name
103 # define helper_le_ld_name glue(glue(helper_le_ld, USUFFIX), MMUSUFFIX)
104 # define helper_be_ld_name glue(glue(helper_be_ld, USUFFIX), MMUSUFFIX)
105 # define helper_le_lds_name glue(glue(helper_le_ld, SSUFFIX), MMUSUFFIX)
106 # define helper_be_lds_name glue(glue(helper_be_ld, SSUFFIX), MMUSUFFIX)
107 # define helper_le_st_name glue(glue(helper_le_st, SUFFIX), MMUSUFFIX)
108 # define helper_be_st_name glue(glue(helper_be_st, SUFFIX), MMUSUFFIX)
111 #ifdef TARGET_WORDS_BIGENDIAN
112 # define helper_te_ld_name helper_be_ld_name
113 # define helper_te_st_name helper_be_st_name
115 # define helper_te_ld_name helper_le_ld_name
116 # define helper_te_st_name helper_le_st_name
119 #ifndef SOFTMMU_CODE_ACCESS
120 static inline DATA_TYPE
glue(io_read
, SUFFIX
)(CPUArchState
*env
,
126 CPUState
*cpu
= ENV_GET_CPU(env
);
127 MemoryRegion
*mr
= iotlb_to_region(cpu
->as
, physaddr
);
129 physaddr
= (physaddr
& TARGET_PAGE_MASK
) + addr
;
130 cpu
->mem_io_pc
= retaddr
;
131 if (mr
!= &io_mem_rom
&& mr
!= &io_mem_notdirty
&& !cpu_can_do_io(cpu
)) {
132 cpu_io_recompile(cpu
, retaddr
);
135 cpu
->mem_io_vaddr
= addr
;
136 io_mem_read(mr
, physaddr
, &val
, 1 << SHIFT
);
141 #ifdef SOFTMMU_CODE_ACCESS
142 static __attribute__((unused
))
144 WORD_TYPE
helper_le_ld_name(CPUArchState
*env
, target_ulong addr
, int mmu_idx
,
147 int index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
148 target_ulong tlb_addr
= env
->tlb_table
[mmu_idx
][index
].ADDR_READ
;
152 /* Adjust the given return address. */
153 retaddr
-= GETPC_ADJ
;
155 /* If the TLB entry is for a different page, reload and try again. */
156 if ((addr
& TARGET_PAGE_MASK
)
157 != (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
159 if ((addr
& (DATA_SIZE
- 1)) != 0) {
160 cpu_unaligned_access(ENV_GET_CPU(env
), addr
, READ_ACCESS_TYPE
,
164 tlb_fill(ENV_GET_CPU(env
), addr
, READ_ACCESS_TYPE
, mmu_idx
, retaddr
);
165 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].ADDR_READ
;
168 /* Handle an IO access. */
169 if (unlikely(tlb_addr
& ~TARGET_PAGE_MASK
)) {
171 if ((addr
& (DATA_SIZE
- 1)) != 0) {
172 goto do_unaligned_access
;
174 ioaddr
= env
->iotlb
[mmu_idx
][index
];
176 /* ??? Note that the io helpers always read data in the target
177 byte ordering. We should push the LE/BE request down into io. */
178 res
= glue(io_read
, SUFFIX
)(env
, ioaddr
, addr
, retaddr
);
183 /* Handle slow unaligned access (it spans two pages or IO). */
185 && unlikely((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1
186 >= TARGET_PAGE_SIZE
)) {
187 target_ulong addr1
, addr2
;
188 DATA_TYPE res1
, res2
;
192 cpu_unaligned_access(ENV_GET_CPU(env
), addr
, READ_ACCESS_TYPE
,
195 addr1
= addr
& ~(DATA_SIZE
- 1);
196 addr2
= addr1
+ DATA_SIZE
;
197 /* Note the adjustment at the beginning of the function.
198 Undo that for the recursion. */
199 res1
= helper_le_ld_name(env
, addr1
, mmu_idx
, retaddr
+ GETPC_ADJ
);
200 res2
= helper_le_ld_name(env
, addr2
, mmu_idx
, retaddr
+ GETPC_ADJ
);
201 shift
= (addr
& (DATA_SIZE
- 1)) * 8;
203 /* Little-endian combine. */
204 res
= (res1
>> shift
) | (res2
<< ((DATA_SIZE
* 8) - shift
));
208 /* Handle aligned access or unaligned access in the same page. */
210 if ((addr
& (DATA_SIZE
- 1)) != 0) {
211 cpu_unaligned_access(ENV_GET_CPU(env
), addr
, READ_ACCESS_TYPE
,
216 haddr
= addr
+ env
->tlb_table
[mmu_idx
][index
].addend
;
218 res
= glue(glue(ld
, LSUFFIX
), _p
)((uint8_t *)haddr
);
220 res
= glue(glue(ld
, LSUFFIX
), _le_p
)((uint8_t *)haddr
);
226 #ifdef SOFTMMU_CODE_ACCESS
227 static __attribute__((unused
))
229 WORD_TYPE
helper_be_ld_name(CPUArchState
*env
, target_ulong addr
, int mmu_idx
,
232 int index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
233 target_ulong tlb_addr
= env
->tlb_table
[mmu_idx
][index
].ADDR_READ
;
237 /* Adjust the given return address. */
238 retaddr
-= GETPC_ADJ
;
240 /* If the TLB entry is for a different page, reload and try again. */
241 if ((addr
& TARGET_PAGE_MASK
)
242 != (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
244 if ((addr
& (DATA_SIZE
- 1)) != 0) {
245 cpu_unaligned_access(ENV_GET_CPU(env
), addr
, READ_ACCESS_TYPE
,
249 tlb_fill(ENV_GET_CPU(env
), addr
, READ_ACCESS_TYPE
, mmu_idx
, retaddr
);
250 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].ADDR_READ
;
253 /* Handle an IO access. */
254 if (unlikely(tlb_addr
& ~TARGET_PAGE_MASK
)) {
256 if ((addr
& (DATA_SIZE
- 1)) != 0) {
257 goto do_unaligned_access
;
259 ioaddr
= env
->iotlb
[mmu_idx
][index
];
261 /* ??? Note that the io helpers always read data in the target
262 byte ordering. We should push the LE/BE request down into io. */
263 res
= glue(io_read
, SUFFIX
)(env
, ioaddr
, addr
, retaddr
);
268 /* Handle slow unaligned access (it spans two pages or IO). */
270 && unlikely((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1
271 >= TARGET_PAGE_SIZE
)) {
272 target_ulong addr1
, addr2
;
273 DATA_TYPE res1
, res2
;
277 cpu_unaligned_access(ENV_GET_CPU(env
), addr
, READ_ACCESS_TYPE
,
280 addr1
= addr
& ~(DATA_SIZE
- 1);
281 addr2
= addr1
+ DATA_SIZE
;
282 /* Note the adjustment at the beginning of the function.
283 Undo that for the recursion. */
284 res1
= helper_be_ld_name(env
, addr1
, mmu_idx
, retaddr
+ GETPC_ADJ
);
285 res2
= helper_be_ld_name(env
, addr2
, mmu_idx
, retaddr
+ GETPC_ADJ
);
286 shift
= (addr
& (DATA_SIZE
- 1)) * 8;
288 /* Big-endian combine. */
289 res
= (res1
<< shift
) | (res2
>> ((DATA_SIZE
* 8) - shift
));
293 /* Handle aligned access or unaligned access in the same page. */
295 if ((addr
& (DATA_SIZE
- 1)) != 0) {
296 cpu_unaligned_access(ENV_GET_CPU(env
), addr
, READ_ACCESS_TYPE
,
301 haddr
= addr
+ env
->tlb_table
[mmu_idx
][index
].addend
;
302 res
= glue(glue(ld
, LSUFFIX
), _be_p
)((uint8_t *)haddr
);
305 #endif /* DATA_SIZE > 1 */
308 glue(glue(helper_ld
, SUFFIX
), MMUSUFFIX
)(CPUArchState
*env
, target_ulong addr
,
311 return helper_te_ld_name (env
, addr
, mmu_idx
, GETRA());
314 #ifndef SOFTMMU_CODE_ACCESS
316 /* Provide signed versions of the load routines as well. We can of course
317 avoid this for 64-bit data, or for 32-bit data on 32-bit host. */
318 #if DATA_SIZE * 8 < TCG_TARGET_REG_BITS
319 WORD_TYPE
helper_le_lds_name(CPUArchState
*env
, target_ulong addr
,
320 int mmu_idx
, uintptr_t retaddr
)
322 return (SDATA_TYPE
)helper_le_ld_name(env
, addr
, mmu_idx
, retaddr
);
326 WORD_TYPE
helper_be_lds_name(CPUArchState
*env
, target_ulong addr
,
327 int mmu_idx
, uintptr_t retaddr
)
329 return (SDATA_TYPE
)helper_be_ld_name(env
, addr
, mmu_idx
, retaddr
);
334 static inline void glue(io_write
, SUFFIX
)(CPUArchState
*env
,
340 CPUState
*cpu
= ENV_GET_CPU(env
);
341 MemoryRegion
*mr
= iotlb_to_region(cpu
->as
, physaddr
);
343 physaddr
= (physaddr
& TARGET_PAGE_MASK
) + addr
;
344 if (mr
!= &io_mem_rom
&& mr
!= &io_mem_notdirty
&& !cpu_can_do_io(cpu
)) {
345 cpu_io_recompile(cpu
, retaddr
);
348 cpu
->mem_io_vaddr
= addr
;
349 cpu
->mem_io_pc
= retaddr
;
350 io_mem_write(mr
, physaddr
, val
, 1 << SHIFT
);
353 void helper_le_st_name(CPUArchState
*env
, target_ulong addr
, DATA_TYPE val
,
354 int mmu_idx
, uintptr_t retaddr
)
356 int index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
357 target_ulong tlb_addr
= env
->tlb_table
[mmu_idx
][index
].addr_write
;
360 /* Adjust the given return address. */
361 retaddr
-= GETPC_ADJ
;
363 /* If the TLB entry is for a different page, reload and try again. */
364 if ((addr
& TARGET_PAGE_MASK
)
365 != (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
367 if ((addr
& (DATA_SIZE
- 1)) != 0) {
368 cpu_unaligned_access(ENV_GET_CPU(env
), addr
, 1, mmu_idx
, retaddr
);
371 tlb_fill(ENV_GET_CPU(env
), addr
, 1, mmu_idx
, retaddr
);
372 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].addr_write
;
375 /* Handle an IO access. */
376 if (unlikely(tlb_addr
& ~TARGET_PAGE_MASK
)) {
378 if ((addr
& (DATA_SIZE
- 1)) != 0) {
379 goto do_unaligned_access
;
381 ioaddr
= env
->iotlb
[mmu_idx
][index
];
383 /* ??? Note that the io helpers always read data in the target
384 byte ordering. We should push the LE/BE request down into io. */
386 glue(io_write
, SUFFIX
)(env
, ioaddr
, val
, addr
, retaddr
);
390 /* Handle slow unaligned access (it spans two pages or IO). */
392 && unlikely((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1
393 >= TARGET_PAGE_SIZE
)) {
397 cpu_unaligned_access(ENV_GET_CPU(env
), addr
, 1, mmu_idx
, retaddr
);
399 /* XXX: not efficient, but simple */
400 /* Note: relies on the fact that tlb_fill() does not remove the
401 * previous page from the TLB cache. */
402 for (i
= DATA_SIZE
- 1; i
>= 0; i
--) {
403 /* Little-endian extract. */
404 uint8_t val8
= val
>> (i
* 8);
405 /* Note the adjustment at the beginning of the function.
406 Undo that for the recursion. */
407 glue(helper_ret_stb
, MMUSUFFIX
)(env
, addr
+ i
, val8
,
408 mmu_idx
, retaddr
+ GETPC_ADJ
);
413 /* Handle aligned access or unaligned access in the same page. */
415 if ((addr
& (DATA_SIZE
- 1)) != 0) {
416 cpu_unaligned_access(ENV_GET_CPU(env
), addr
, 1, mmu_idx
, retaddr
);
420 haddr
= addr
+ env
->tlb_table
[mmu_idx
][index
].addend
;
422 glue(glue(st
, SUFFIX
), _p
)((uint8_t *)haddr
, val
);
424 glue(glue(st
, SUFFIX
), _le_p
)((uint8_t *)haddr
, val
);
429 void helper_be_st_name(CPUArchState
*env
, target_ulong addr
, DATA_TYPE val
,
430 int mmu_idx
, uintptr_t retaddr
)
432 int index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
433 target_ulong tlb_addr
= env
->tlb_table
[mmu_idx
][index
].addr_write
;
436 /* Adjust the given return address. */
437 retaddr
-= GETPC_ADJ
;
439 /* If the TLB entry is for a different page, reload and try again. */
440 if ((addr
& TARGET_PAGE_MASK
)
441 != (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
443 if ((addr
& (DATA_SIZE
- 1)) != 0) {
444 cpu_unaligned_access(ENV_GET_CPU(env
), addr
, 1, mmu_idx
, retaddr
);
447 tlb_fill(ENV_GET_CPU(env
), addr
, 1, mmu_idx
, retaddr
);
448 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].addr_write
;
451 /* Handle an IO access. */
452 if (unlikely(tlb_addr
& ~TARGET_PAGE_MASK
)) {
454 if ((addr
& (DATA_SIZE
- 1)) != 0) {
455 goto do_unaligned_access
;
457 ioaddr
= env
->iotlb
[mmu_idx
][index
];
459 /* ??? Note that the io helpers always read data in the target
460 byte ordering. We should push the LE/BE request down into io. */
462 glue(io_write
, SUFFIX
)(env
, ioaddr
, val
, addr
, retaddr
);
466 /* Handle slow unaligned access (it spans two pages or IO). */
468 && unlikely((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1
469 >= TARGET_PAGE_SIZE
)) {
473 cpu_unaligned_access(ENV_GET_CPU(env
), addr
, 1, mmu_idx
, retaddr
);
475 /* XXX: not efficient, but simple */
476 /* Note: relies on the fact that tlb_fill() does not remove the
477 * previous page from the TLB cache. */
478 for (i
= DATA_SIZE
- 1; i
>= 0; i
--) {
479 /* Big-endian extract. */
480 uint8_t val8
= val
>> (((DATA_SIZE
- 1) * 8) - (i
* 8));
481 /* Note the adjustment at the beginning of the function.
482 Undo that for the recursion. */
483 glue(helper_ret_stb
, MMUSUFFIX
)(env
, addr
+ i
, val8
,
484 mmu_idx
, retaddr
+ GETPC_ADJ
);
489 /* Handle aligned access or unaligned access in the same page. */
491 if ((addr
& (DATA_SIZE
- 1)) != 0) {
492 cpu_unaligned_access(ENV_GET_CPU(env
), addr
, 1, mmu_idx
, retaddr
);
496 haddr
= addr
+ env
->tlb_table
[mmu_idx
][index
].addend
;
497 glue(glue(st
, SUFFIX
), _be_p
)((uint8_t *)haddr
, val
);
499 #endif /* DATA_SIZE > 1 */
502 glue(glue(helper_st
, SUFFIX
), MMUSUFFIX
)(CPUArchState
*env
, target_ulong addr
,
503 DATA_TYPE val
, int mmu_idx
)
505 helper_te_st_name(env
, addr
, val
, mmu_idx
, GETRA());
508 #endif /* !defined(SOFTMMU_CODE_ACCESS) */
510 #undef READ_ACCESS_TYPE
526 #undef helper_le_ld_name
527 #undef helper_be_ld_name
528 #undef helper_le_lds_name
529 #undef helper_be_lds_name
530 #undef helper_le_st_name
531 #undef helper_be_st_name
532 #undef helper_te_ld_name
533 #undef helper_te_st_name