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"
27 #define DATA_SIZE (1 << SHIFT)
32 #define DATA_TYPE uint64_t
36 #define DATA_TYPE uint32_t
40 #define DATA_TYPE uint16_t
44 #define DATA_TYPE uint8_t
46 #error unsupported data size
49 #ifdef SOFTMMU_CODE_ACCESS
50 #define READ_ACCESS_TYPE 2
51 #define ADDR_READ addr_code
53 #define READ_ACCESS_TYPE 0
54 #define ADDR_READ addr_read
57 static DATA_TYPE
glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
60 static inline DATA_TYPE
glue(io_read
, SUFFIX
)(target_phys_addr_t physaddr
,
65 MemoryRegion
*mr
= iotlb_to_region(physaddr
);
67 physaddr
= (physaddr
& TARGET_PAGE_MASK
) + addr
;
68 env
->mem_io_pc
= (unsigned long)retaddr
;
69 if (mr
!= &io_mem_ram
&& mr
!= &io_mem_rom
70 && mr
!= &io_mem_unassigned
71 && mr
!= &io_mem_notdirty
73 cpu_io_recompile(env
, retaddr
);
76 env
->mem_io_vaddr
= addr
;
78 res
= io_mem_read(mr
, physaddr
, 1 << SHIFT
);
80 #ifdef TARGET_WORDS_BIGENDIAN
81 res
= io_mem_read(mr
, physaddr
, 4) << 32;
82 res
|= io_mem_read(mr
, physaddr
+ 4, 4);
84 res
= io_mem_read(mr
, physaddr
, 4);
85 res
|= io_mem_read(mr
, physaddr
+ 4, 4) << 32;
87 #endif /* SHIFT > 2 */
91 /* handle all cases except unaligned access which span two pages */
92 DATA_TYPE
glue(glue(__ld
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
, int mmu_idx
)
96 target_ulong tlb_addr
;
97 target_phys_addr_t ioaddr
;
101 /* test if there is match for unaligned or IO access */
102 /* XXX: could done more in memory macro in a non portable way */
103 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
105 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].ADDR_READ
;
106 if ((addr
& TARGET_PAGE_MASK
) == (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
107 if (tlb_addr
& ~TARGET_PAGE_MASK
) {
109 if ((addr
& (DATA_SIZE
- 1)) != 0)
110 goto do_unaligned_access
;
112 ioaddr
= env
->iotlb
[mmu_idx
][index
];
113 res
= glue(io_read
, SUFFIX
)(ioaddr
, addr
, retaddr
);
114 } else if (((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1) >= TARGET_PAGE_SIZE
) {
115 /* slow unaligned access (it spans two pages or IO) */
119 do_unaligned_access(addr
, READ_ACCESS_TYPE
, mmu_idx
, retaddr
);
121 res
= glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(addr
,
124 /* unaligned/aligned access in the same page */
126 if ((addr
& (DATA_SIZE
- 1)) != 0) {
128 do_unaligned_access(addr
, READ_ACCESS_TYPE
, mmu_idx
, retaddr
);
131 addend
= env
->tlb_table
[mmu_idx
][index
].addend
;
132 res
= glue(glue(ld
, USUFFIX
), _raw
)((uint8_t *)(long)(addr
+addend
));
135 /* the page is not in the TLB : fill it */
138 if ((addr
& (DATA_SIZE
- 1)) != 0)
139 do_unaligned_access(addr
, READ_ACCESS_TYPE
, mmu_idx
, retaddr
);
141 tlb_fill(env
, addr
, READ_ACCESS_TYPE
, mmu_idx
, retaddr
);
147 /* handle all unaligned cases */
148 static DATA_TYPE
glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
152 DATA_TYPE res
, res1
, res2
;
154 target_phys_addr_t ioaddr
;
155 unsigned long addend
;
156 target_ulong tlb_addr
, addr1
, addr2
;
158 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
160 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].ADDR_READ
;
161 if ((addr
& TARGET_PAGE_MASK
) == (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
162 if (tlb_addr
& ~TARGET_PAGE_MASK
) {
164 if ((addr
& (DATA_SIZE
- 1)) != 0)
165 goto do_unaligned_access
;
166 ioaddr
= env
->iotlb
[mmu_idx
][index
];
167 res
= glue(io_read
, SUFFIX
)(ioaddr
, addr
, retaddr
);
168 } else if (((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1) >= TARGET_PAGE_SIZE
) {
170 /* slow unaligned access (it spans two pages) */
171 addr1
= addr
& ~(DATA_SIZE
- 1);
172 addr2
= addr1
+ DATA_SIZE
;
173 res1
= glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(addr1
,
175 res2
= glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(addr2
,
177 shift
= (addr
& (DATA_SIZE
- 1)) * 8;
178 #ifdef TARGET_WORDS_BIGENDIAN
179 res
= (res1
<< shift
) | (res2
>> ((DATA_SIZE
* 8) - shift
));
181 res
= (res1
>> shift
) | (res2
<< ((DATA_SIZE
* 8) - shift
));
183 res
= (DATA_TYPE
)res
;
185 /* unaligned/aligned access in the same page */
186 addend
= env
->tlb_table
[mmu_idx
][index
].addend
;
187 res
= glue(glue(ld
, USUFFIX
), _raw
)((uint8_t *)(long)(addr
+addend
));
190 /* the page is not in the TLB : fill it */
191 tlb_fill(env
, addr
, READ_ACCESS_TYPE
, mmu_idx
, retaddr
);
197 #ifndef SOFTMMU_CODE_ACCESS
199 static void glue(glue(slow_st
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
204 static inline void glue(io_write
, SUFFIX
)(target_phys_addr_t physaddr
,
209 MemoryRegion
*mr
= iotlb_to_region(physaddr
);
211 physaddr
= (physaddr
& TARGET_PAGE_MASK
) + addr
;
212 if (mr
!= &io_mem_ram
&& mr
!= &io_mem_rom
213 && mr
!= &io_mem_unassigned
214 && mr
!= &io_mem_notdirty
215 && !can_do_io(env
)) {
216 cpu_io_recompile(env
, retaddr
);
219 env
->mem_io_vaddr
= addr
;
220 env
->mem_io_pc
= (unsigned long)retaddr
;
222 io_mem_write(mr
, physaddr
, val
, 1 << SHIFT
);
224 #ifdef TARGET_WORDS_BIGENDIAN
225 io_mem_write(mr
, physaddr
, (val
>> 32), 4);
226 io_mem_write(mr
, physaddr
+ 4, (uint32_t)val
, 4);
228 io_mem_write(mr
, physaddr
, (uint32_t)val
, 4);
229 io_mem_write(mr
, physaddr
+ 4, val
>> 32, 4);
231 #endif /* SHIFT > 2 */
234 void glue(glue(__st
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
, DATA_TYPE val
,
237 target_phys_addr_t ioaddr
;
238 unsigned long addend
;
239 target_ulong tlb_addr
;
243 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
245 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].addr_write
;
246 if ((addr
& TARGET_PAGE_MASK
) == (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
247 if (tlb_addr
& ~TARGET_PAGE_MASK
) {
249 if ((addr
& (DATA_SIZE
- 1)) != 0)
250 goto do_unaligned_access
;
252 ioaddr
= env
->iotlb
[mmu_idx
][index
];
253 glue(io_write
, SUFFIX
)(ioaddr
, val
, addr
, retaddr
);
254 } else if (((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1) >= TARGET_PAGE_SIZE
) {
258 do_unaligned_access(addr
, 1, mmu_idx
, retaddr
);
260 glue(glue(slow_st
, SUFFIX
), MMUSUFFIX
)(addr
, val
,
263 /* aligned/unaligned access in the same page */
265 if ((addr
& (DATA_SIZE
- 1)) != 0) {
267 do_unaligned_access(addr
, 1, mmu_idx
, retaddr
);
270 addend
= env
->tlb_table
[mmu_idx
][index
].addend
;
271 glue(glue(st
, SUFFIX
), _raw
)((uint8_t *)(long)(addr
+addend
), val
);
274 /* the page is not in the TLB : fill it */
277 if ((addr
& (DATA_SIZE
- 1)) != 0)
278 do_unaligned_access(addr
, 1, mmu_idx
, retaddr
);
280 tlb_fill(env
, addr
, 1, mmu_idx
, retaddr
);
285 /* handles all unaligned cases */
286 static void glue(glue(slow_st
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
291 target_phys_addr_t ioaddr
;
292 unsigned long addend
;
293 target_ulong tlb_addr
;
296 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
298 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].addr_write
;
299 if ((addr
& TARGET_PAGE_MASK
) == (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
300 if (tlb_addr
& ~TARGET_PAGE_MASK
) {
302 if ((addr
& (DATA_SIZE
- 1)) != 0)
303 goto do_unaligned_access
;
304 ioaddr
= env
->iotlb
[mmu_idx
][index
];
305 glue(io_write
, SUFFIX
)(ioaddr
, val
, addr
, retaddr
);
306 } else if (((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1) >= TARGET_PAGE_SIZE
) {
308 /* XXX: not efficient, but simple */
309 /* Note: relies on the fact that tlb_fill() does not remove the
310 * previous page from the TLB cache. */
311 for(i
= DATA_SIZE
- 1; i
>= 0; i
--) {
312 #ifdef TARGET_WORDS_BIGENDIAN
313 glue(slow_stb
, MMUSUFFIX
)(addr
+ i
, val
>> (((DATA_SIZE
- 1) * 8) - (i
* 8)),
316 glue(slow_stb
, MMUSUFFIX
)(addr
+ i
, val
>> (i
* 8),
321 /* aligned/unaligned access in the same page */
322 addend
= env
->tlb_table
[mmu_idx
][index
].addend
;
323 glue(glue(st
, SUFFIX
), _raw
)((uint8_t *)(long)(addr
+addend
), val
);
326 /* the page is not in the TLB : fill it */
327 tlb_fill(env
, addr
, 1, mmu_idx
, retaddr
);
332 #endif /* !defined(SOFTMMU_CODE_ACCESS) */
334 #undef READ_ACCESS_TYPE