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"
26 #define DATA_SIZE (1 << SHIFT)
31 #define DATA_TYPE uint64_t
35 #define DATA_TYPE uint32_t
39 #define DATA_TYPE uint16_t
43 #define DATA_TYPE uint8_t
45 #error unsupported data size
48 #ifdef SOFTMMU_CODE_ACCESS
49 #define READ_ACCESS_TYPE 2
50 #define ADDR_READ addr_code
52 #define READ_ACCESS_TYPE 0
53 #define ADDR_READ addr_read
56 static DATA_TYPE
glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
59 static inline DATA_TYPE
glue(io_read
, SUFFIX
)(target_phys_addr_t physaddr
,
65 index
= (physaddr
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
66 physaddr
= (physaddr
& TARGET_PAGE_MASK
) + addr
;
67 env
->mem_io_pc
= (unsigned long)retaddr
;
68 if (index
!= IO_MEM_RAM
&& index
!= IO_MEM_ROM
69 && index
!= IO_MEM_UNASSIGNED
&& index
!= IO_MEM_NOTDIRTY
71 cpu_io_recompile(env
, retaddr
);
74 env
->mem_io_vaddr
= addr
;
76 res
= io_mem_read(index
, physaddr
, 1 << SHIFT
);
78 #ifdef TARGET_WORDS_BIGENDIAN
79 res
= io_mem_read(index
, physaddr
, 4) << 32;
80 res
|= io_mem_read(index
, physaddr
+ 4, 4);
82 res
= io_mem_read(index
, physaddr
, 4);
83 res
|= io_mem_read(index
, physaddr
+ 4, 4) << 32;
85 #endif /* SHIFT > 2 */
89 /* handle all cases except unaligned access which span two pages */
90 DATA_TYPE REGPARM
glue(glue(__ld
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
95 target_ulong tlb_addr
;
96 target_phys_addr_t ioaddr
;
100 /* test if there is match for unaligned or IO access */
101 /* XXX: could done more in memory macro in a non portable way */
102 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
104 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].ADDR_READ
;
105 if ((addr
& TARGET_PAGE_MASK
) == (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
106 if (tlb_addr
& ~TARGET_PAGE_MASK
) {
108 if ((addr
& (DATA_SIZE
- 1)) != 0)
109 goto do_unaligned_access
;
111 ioaddr
= env
->iotlb
[mmu_idx
][index
];
112 res
= glue(io_read
, SUFFIX
)(ioaddr
, addr
, retaddr
);
113 } else if (((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1) >= TARGET_PAGE_SIZE
) {
114 /* slow unaligned access (it spans two pages or IO) */
118 do_unaligned_access(addr
, READ_ACCESS_TYPE
, mmu_idx
, retaddr
);
120 res
= glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(addr
,
123 /* unaligned/aligned access in the same page */
125 if ((addr
& (DATA_SIZE
- 1)) != 0) {
127 do_unaligned_access(addr
, READ_ACCESS_TYPE
, mmu_idx
, retaddr
);
130 addend
= env
->tlb_table
[mmu_idx
][index
].addend
;
131 res
= glue(glue(ld
, USUFFIX
), _raw
)((uint8_t *)(long)(addr
+addend
));
134 /* the page is not in the TLB : fill it */
137 if ((addr
& (DATA_SIZE
- 1)) != 0)
138 do_unaligned_access(addr
, READ_ACCESS_TYPE
, mmu_idx
, retaddr
);
140 tlb_fill(env
, addr
, READ_ACCESS_TYPE
, mmu_idx
, retaddr
);
146 /* handle all unaligned cases */
147 static DATA_TYPE
glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
151 DATA_TYPE res
, res1
, res2
;
153 target_phys_addr_t ioaddr
;
154 unsigned long addend
;
155 target_ulong tlb_addr
, addr1
, addr2
;
157 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
159 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].ADDR_READ
;
160 if ((addr
& TARGET_PAGE_MASK
) == (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
161 if (tlb_addr
& ~TARGET_PAGE_MASK
) {
163 if ((addr
& (DATA_SIZE
- 1)) != 0)
164 goto do_unaligned_access
;
165 ioaddr
= env
->iotlb
[mmu_idx
][index
];
166 res
= glue(io_read
, SUFFIX
)(ioaddr
, addr
, retaddr
);
167 } else if (((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1) >= TARGET_PAGE_SIZE
) {
169 /* slow unaligned access (it spans two pages) */
170 addr1
= addr
& ~(DATA_SIZE
- 1);
171 addr2
= addr1
+ DATA_SIZE
;
172 res1
= glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(addr1
,
174 res2
= glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(addr2
,
176 shift
= (addr
& (DATA_SIZE
- 1)) * 8;
177 #ifdef TARGET_WORDS_BIGENDIAN
178 res
= (res1
<< shift
) | (res2
>> ((DATA_SIZE
* 8) - shift
));
180 res
= (res1
>> shift
) | (res2
<< ((DATA_SIZE
* 8) - shift
));
182 res
= (DATA_TYPE
)res
;
184 /* unaligned/aligned access in the same page */
185 addend
= env
->tlb_table
[mmu_idx
][index
].addend
;
186 res
= glue(glue(ld
, USUFFIX
), _raw
)((uint8_t *)(long)(addr
+addend
));
189 /* the page is not in the TLB : fill it */
190 tlb_fill(env
, addr
, READ_ACCESS_TYPE
, mmu_idx
, retaddr
);
196 #ifndef SOFTMMU_CODE_ACCESS
198 static void glue(glue(slow_st
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
203 static inline void glue(io_write
, SUFFIX
)(target_phys_addr_t physaddr
,
209 index
= (physaddr
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
210 physaddr
= (physaddr
& TARGET_PAGE_MASK
) + addr
;
211 if (index
!= IO_MEM_RAM
&& index
!= IO_MEM_ROM
212 && index
!= IO_MEM_UNASSIGNED
&& index
!= IO_MEM_NOTDIRTY
213 && !can_do_io(env
)) {
214 cpu_io_recompile(env
, retaddr
);
217 env
->mem_io_vaddr
= addr
;
218 env
->mem_io_pc
= (unsigned long)retaddr
;
220 io_mem_write(index
, physaddr
, val
, 1 << SHIFT
);
222 #ifdef TARGET_WORDS_BIGENDIAN
223 io_mem_write(index
, physaddr
, (val
>> 32), 4);
224 io_mem_write(index
, physaddr
+ 4, (uint32_t)val
, 4);
226 io_mem_write(index
, physaddr
, (uint32_t)val
, 4);
227 io_mem_write(index
, physaddr
+ 4, val
>> 32, 4);
229 #endif /* SHIFT > 2 */
232 void REGPARM
glue(glue(__st
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
236 target_phys_addr_t ioaddr
;
237 unsigned long addend
;
238 target_ulong tlb_addr
;
242 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
244 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].addr_write
;
245 if ((addr
& TARGET_PAGE_MASK
) == (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
246 if (tlb_addr
& ~TARGET_PAGE_MASK
) {
248 if ((addr
& (DATA_SIZE
- 1)) != 0)
249 goto do_unaligned_access
;
251 ioaddr
= env
->iotlb
[mmu_idx
][index
];
252 glue(io_write
, SUFFIX
)(ioaddr
, val
, addr
, retaddr
);
253 } else if (((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1) >= TARGET_PAGE_SIZE
) {
257 do_unaligned_access(addr
, 1, mmu_idx
, retaddr
);
259 glue(glue(slow_st
, SUFFIX
), MMUSUFFIX
)(addr
, val
,
262 /* aligned/unaligned access in the same page */
264 if ((addr
& (DATA_SIZE
- 1)) != 0) {
266 do_unaligned_access(addr
, 1, mmu_idx
, retaddr
);
269 addend
= env
->tlb_table
[mmu_idx
][index
].addend
;
270 glue(glue(st
, SUFFIX
), _raw
)((uint8_t *)(long)(addr
+addend
), val
);
273 /* the page is not in the TLB : fill it */
276 if ((addr
& (DATA_SIZE
- 1)) != 0)
277 do_unaligned_access(addr
, 1, mmu_idx
, retaddr
);
279 tlb_fill(env
, addr
, 1, mmu_idx
, retaddr
);
284 /* handles all unaligned cases */
285 static void glue(glue(slow_st
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
290 target_phys_addr_t ioaddr
;
291 unsigned long addend
;
292 target_ulong tlb_addr
;
295 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
297 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].addr_write
;
298 if ((addr
& TARGET_PAGE_MASK
) == (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
299 if (tlb_addr
& ~TARGET_PAGE_MASK
) {
301 if ((addr
& (DATA_SIZE
- 1)) != 0)
302 goto do_unaligned_access
;
303 ioaddr
= env
->iotlb
[mmu_idx
][index
];
304 glue(io_write
, SUFFIX
)(ioaddr
, val
, addr
, retaddr
);
305 } else if (((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1) >= TARGET_PAGE_SIZE
) {
307 /* XXX: not efficient, but simple */
308 /* Note: relies on the fact that tlb_fill() does not remove the
309 * previous page from the TLB cache. */
310 for(i
= DATA_SIZE
- 1; i
>= 0; i
--) {
311 #ifdef TARGET_WORDS_BIGENDIAN
312 glue(slow_stb
, MMUSUFFIX
)(addr
+ i
, val
>> (((DATA_SIZE
- 1) * 8) - (i
* 8)),
315 glue(slow_stb
, MMUSUFFIX
)(addr
+ i
, val
>> (i
* 8),
320 /* aligned/unaligned access in the same page */
321 addend
= env
->tlb_table
[mmu_idx
][index
].addend
;
322 glue(glue(st
, SUFFIX
), _raw
)((uint8_t *)(long)(addr
+addend
), val
);
325 /* the page is not in the TLB : fill it */
326 tlb_fill(env
, addr
, 1, mmu_idx
, retaddr
);
331 #endif /* !defined(SOFTMMU_CODE_ACCESS) */
333 #undef READ_ACCESS_TYPE