4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #define DATA_SIZE (1 << SHIFT)
24 #define DATA_TYPE uint64_t
28 #define DATA_TYPE uint32_t
32 #define DATA_TYPE uint16_t
36 #define DATA_TYPE uint8_t
38 #error unsupported data size
41 #ifdef SOFTMMU_CODE_ACCESS
42 #define READ_ACCESS_TYPE 2
43 #define ADDR_READ addr_code
45 #define READ_ACCESS_TYPE 0
46 #define ADDR_READ addr_read
49 static DATA_TYPE
glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
52 static inline DATA_TYPE
glue(io_read
, SUFFIX
)(target_phys_addr_t physaddr
,
58 index
= (physaddr
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
59 physaddr
= (physaddr
& TARGET_PAGE_MASK
) + addr
;
60 env
->mem_io_pc
= (unsigned long)retaddr
;
61 if (index
> (IO_MEM_NOTDIRTY
>> IO_MEM_SHIFT
)
63 cpu_io_recompile(env
, retaddr
);
66 env
->mem_io_vaddr
= addr
;
68 res
= io_mem_read
[index
][SHIFT
](io_mem_opaque
[index
], physaddr
);
70 #ifdef TARGET_WORDS_BIGENDIAN
71 res
= (uint64_t)io_mem_read
[index
][2](io_mem_opaque
[index
], physaddr
) << 32;
72 res
|= io_mem_read
[index
][2](io_mem_opaque
[index
], physaddr
+ 4);
74 res
= io_mem_read
[index
][2](io_mem_opaque
[index
], physaddr
);
75 res
|= (uint64_t)io_mem_read
[index
][2](io_mem_opaque
[index
], physaddr
+ 4) << 32;
77 #endif /* SHIFT > 2 */
81 /* handle all cases except unaligned access which span two pages */
82 DATA_TYPE REGPARM
glue(glue(__ld
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
87 target_ulong tlb_addr
;
88 target_phys_addr_t addend
;
91 /* test if there is match for unaligned or IO access */
92 /* XXX: could done more in memory macro in a non portable way */
93 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
95 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].ADDR_READ
;
96 if ((addr
& TARGET_PAGE_MASK
) == (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
97 if (tlb_addr
& ~TARGET_PAGE_MASK
) {
99 if ((addr
& (DATA_SIZE
- 1)) != 0)
100 goto do_unaligned_access
;
102 addend
= env
->iotlb
[mmu_idx
][index
];
103 res
= glue(io_read
, SUFFIX
)(addend
, addr
, retaddr
);
104 } else if (((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1) >= TARGET_PAGE_SIZE
) {
105 /* slow unaligned access (it spans two pages or IO) */
109 do_unaligned_access(addr
, READ_ACCESS_TYPE
, mmu_idx
, retaddr
);
111 res
= glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(addr
,
114 /* unaligned/aligned access in the same page */
116 if ((addr
& (DATA_SIZE
- 1)) != 0) {
118 do_unaligned_access(addr
, READ_ACCESS_TYPE
, mmu_idx
, retaddr
);
121 addend
= env
->tlb_table
[mmu_idx
][index
].addend
;
122 res
= glue(glue(ld
, USUFFIX
), _raw
)((uint8_t *)(long)(addr
+addend
));
125 /* the page is not in the TLB : fill it */
128 if ((addr
& (DATA_SIZE
- 1)) != 0)
129 do_unaligned_access(addr
, READ_ACCESS_TYPE
, mmu_idx
, retaddr
);
131 tlb_fill(addr
, READ_ACCESS_TYPE
, mmu_idx
, retaddr
);
137 /* handle all unaligned cases */
138 static DATA_TYPE
glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
142 DATA_TYPE res
, res1
, res2
;
144 target_phys_addr_t addend
;
145 target_ulong tlb_addr
, addr1
, addr2
;
147 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
149 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].ADDR_READ
;
150 if ((addr
& TARGET_PAGE_MASK
) == (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
151 if (tlb_addr
& ~TARGET_PAGE_MASK
) {
153 if ((addr
& (DATA_SIZE
- 1)) != 0)
154 goto do_unaligned_access
;
156 addend
= env
->iotlb
[mmu_idx
][index
];
157 res
= glue(io_read
, SUFFIX
)(addend
, addr
, retaddr
);
158 } else if (((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1) >= TARGET_PAGE_SIZE
) {
160 /* slow unaligned access (it spans two pages) */
161 addr1
= addr
& ~(DATA_SIZE
- 1);
162 addr2
= addr1
+ DATA_SIZE
;
163 res1
= glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(addr1
,
165 res2
= glue(glue(slow_ld
, SUFFIX
), MMUSUFFIX
)(addr2
,
167 shift
= (addr
& (DATA_SIZE
- 1)) * 8;
168 #ifdef TARGET_WORDS_BIGENDIAN
169 res
= (res1
<< shift
) | (res2
>> ((DATA_SIZE
* 8) - shift
));
171 res
= (res1
>> shift
) | (res2
<< ((DATA_SIZE
* 8) - shift
));
173 res
= (DATA_TYPE
)res
;
175 /* unaligned/aligned access in the same page */
176 addend
= env
->tlb_table
[mmu_idx
][index
].addend
;
177 res
= glue(glue(ld
, USUFFIX
), _raw
)((uint8_t *)(long)(addr
+addend
));
180 /* the page is not in the TLB : fill it */
181 tlb_fill(addr
, READ_ACCESS_TYPE
, mmu_idx
, retaddr
);
187 #ifndef SOFTMMU_CODE_ACCESS
189 static void glue(glue(slow_st
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
194 static inline void glue(io_write
, SUFFIX
)(target_phys_addr_t physaddr
,
200 index
= (physaddr
>> IO_MEM_SHIFT
) & (IO_MEM_NB_ENTRIES
- 1);
201 physaddr
= (physaddr
& TARGET_PAGE_MASK
) + addr
;
202 if (index
> (IO_MEM_NOTDIRTY
>> IO_MEM_SHIFT
)
203 && !can_do_io(env
)) {
204 cpu_io_recompile(env
, retaddr
);
207 env
->mem_io_vaddr
= addr
;
208 env
->mem_io_pc
= (unsigned long)retaddr
;
210 io_mem_write
[index
][SHIFT
](io_mem_opaque
[index
], physaddr
, val
);
212 #ifdef TARGET_WORDS_BIGENDIAN
213 io_mem_write
[index
][2](io_mem_opaque
[index
], physaddr
, val
>> 32);
214 io_mem_write
[index
][2](io_mem_opaque
[index
], physaddr
+ 4, val
);
216 io_mem_write
[index
][2](io_mem_opaque
[index
], physaddr
, val
);
217 io_mem_write
[index
][2](io_mem_opaque
[index
], physaddr
+ 4, val
>> 32);
219 #endif /* SHIFT > 2 */
222 void REGPARM
glue(glue(__st
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
226 target_phys_addr_t addend
;
227 target_ulong tlb_addr
;
231 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
233 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].addr_write
;
234 if ((addr
& TARGET_PAGE_MASK
) == (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
235 if (tlb_addr
& ~TARGET_PAGE_MASK
) {
237 if ((addr
& (DATA_SIZE
- 1)) != 0)
238 goto do_unaligned_access
;
240 addend
= env
->iotlb
[mmu_idx
][index
];
241 glue(io_write
, SUFFIX
)(addend
, val
, addr
, retaddr
);
242 } else if (((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1) >= TARGET_PAGE_SIZE
) {
246 do_unaligned_access(addr
, 1, mmu_idx
, retaddr
);
248 glue(glue(slow_st
, SUFFIX
), MMUSUFFIX
)(addr
, val
,
251 /* aligned/unaligned access in the same page */
253 if ((addr
& (DATA_SIZE
- 1)) != 0) {
255 do_unaligned_access(addr
, 1, mmu_idx
, retaddr
);
258 addend
= env
->tlb_table
[mmu_idx
][index
].addend
;
259 glue(glue(st
, SUFFIX
), _raw
)((uint8_t *)(long)(addr
+addend
), val
);
262 /* the page is not in the TLB : fill it */
265 if ((addr
& (DATA_SIZE
- 1)) != 0)
266 do_unaligned_access(addr
, 1, mmu_idx
, retaddr
);
268 tlb_fill(addr
, 1, mmu_idx
, retaddr
);
273 /* handles all unaligned cases */
274 static void glue(glue(slow_st
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
279 target_phys_addr_t addend
;
280 target_ulong tlb_addr
;
283 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
285 tlb_addr
= env
->tlb_table
[mmu_idx
][index
].addr_write
;
286 if ((addr
& TARGET_PAGE_MASK
) == (tlb_addr
& (TARGET_PAGE_MASK
| TLB_INVALID_MASK
))) {
287 if (tlb_addr
& ~TARGET_PAGE_MASK
) {
289 if ((addr
& (DATA_SIZE
- 1)) != 0)
290 goto do_unaligned_access
;
291 addend
= env
->iotlb
[mmu_idx
][index
];
292 glue(io_write
, SUFFIX
)(addend
, val
, addr
, retaddr
);
293 } else if (((addr
& ~TARGET_PAGE_MASK
) + DATA_SIZE
- 1) >= TARGET_PAGE_SIZE
) {
295 /* XXX: not efficient, but simple */
296 /* Note: relies on the fact that tlb_fill() does not remove the
297 * previous page from the TLB cache. */
298 for(i
= DATA_SIZE
- 1; i
>= 0; i
--) {
299 #ifdef TARGET_WORDS_BIGENDIAN
300 glue(slow_stb
, MMUSUFFIX
)(addr
+ i
, val
>> (((DATA_SIZE
- 1) * 8) - (i
* 8)),
303 glue(slow_stb
, MMUSUFFIX
)(addr
+ i
, val
>> (i
* 8),
308 /* aligned/unaligned access in the same page */
309 addend
= env
->tlb_table
[mmu_idx
][index
].addend
;
310 glue(glue(st
, SUFFIX
), _raw
)((uint8_t *)(long)(addr
+addend
), val
);
313 /* the page is not in the TLB : fill it */
314 tlb_fill(addr
, 1, mmu_idx
, retaddr
);
319 #endif /* !defined(SOFTMMU_CODE_ACCESS) */
321 #undef READ_ACCESS_TYPE