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 #include "qemu-timer.h"
21 #define DATA_SIZE (1 << SHIFT)
26 #define DATA_TYPE uint64_t
30 #define DATA_TYPE uint32_t
34 #define DATA_TYPE uint16_t
38 #define DATA_TYPE uint8_t
40 #error unsupported data size
43 #ifdef SOFTMMU_CODE_ACCESS
44 #define READ_ACCESS_TYPE 2
45 #define ADDR_READ addr_code
47 #define READ_ACCESS_TYPE 0
48 #define ADDR_READ addr_read
51 static DATA_TYPE glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
54 static inline DATA_TYPE glue(io_read, SUFFIX)(target_phys_addr_t physaddr,
60 index = (physaddr >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
61 physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
62 env->mem_io_pc = (unsigned long)retaddr;
63 if (index > (IO_MEM_NOTDIRTY >> IO_MEM_SHIFT)
65 cpu_io_recompile(env, retaddr);
68 env->mem_io_vaddr = addr;
70 res = io_mem_read[index][SHIFT](io_mem_opaque[index], physaddr);
72 #ifdef TARGET_WORDS_BIGENDIAN
73 res = (uint64_t)io_mem_read[index][2](io_mem_opaque[index], physaddr) << 32;
74 res |= io_mem_read[index][2](io_mem_opaque[index], physaddr + 4);
76 res = io_mem_read[index][2](io_mem_opaque[index], physaddr);
77 res |= (uint64_t)io_mem_read[index][2](io_mem_opaque[index], physaddr + 4) << 32;
79 #endif /* SHIFT > 2 */
83 /* handle all cases except unaligned access which span two pages */
84 DATA_TYPE REGPARM glue(glue(__ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
89 target_ulong tlb_addr;
90 target_phys_addr_t addend;
93 /* test if there is match for unaligned or IO access */
94 /* XXX: could done more in memory macro in a non portable way */
95 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
97 tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
98 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
99 if (tlb_addr & ~TARGET_PAGE_MASK) {
101 if ((addr & (DATA_SIZE - 1)) != 0)
102 goto do_unaligned_access;
104 addend = env->iotlb[mmu_idx][index];
105 res = glue(io_read, SUFFIX)(addend, addr, retaddr);
106 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
107 /* slow unaligned access (it spans two pages or IO) */
111 do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
113 res = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr,
116 /* unaligned/aligned access in the same page */
118 if ((addr & (DATA_SIZE - 1)) != 0) {
120 do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
123 addend = env->tlb_table[mmu_idx][index].addend;
124 res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)(long)(addr+addend));
127 /* the page is not in the TLB : fill it */
130 if ((addr & (DATA_SIZE - 1)) != 0)
131 do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
133 tlb_fill(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
139 /* handle all unaligned cases */
140 static DATA_TYPE glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
144 DATA_TYPE res, res1, res2;
146 target_phys_addr_t addend;
147 target_ulong tlb_addr, addr1, addr2;
149 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
151 tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
152 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
153 if (tlb_addr & ~TARGET_PAGE_MASK) {
155 if ((addr & (DATA_SIZE - 1)) != 0)
156 goto do_unaligned_access;
157 addend = env->iotlb[mmu_idx][index];
158 res = glue(io_read, SUFFIX)(addend, addr, retaddr);
159 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
161 /* slow unaligned access (it spans two pages) */
162 addr1 = addr & ~(DATA_SIZE - 1);
163 addr2 = addr1 + DATA_SIZE;
164 res1 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr1,
166 res2 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr2,
168 shift = (addr & (DATA_SIZE - 1)) * 8;
169 #ifdef TARGET_WORDS_BIGENDIAN
170 res = (res1 << shift) | (res2 >> ((DATA_SIZE * 8) - shift));
172 res = (res1 >> shift) | (res2 << ((DATA_SIZE * 8) - shift));
174 res = (DATA_TYPE)res;
176 /* unaligned/aligned access in the same page */
177 addend = env->tlb_table[mmu_idx][index].addend;
178 res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)(long)(addr+addend));
181 /* the page is not in the TLB : fill it */
182 tlb_fill(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
188 #ifndef SOFTMMU_CODE_ACCESS
190 static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(target_ulong addr,
195 static inline void glue(io_write, SUFFIX)(target_phys_addr_t physaddr,
201 index = (physaddr >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
202 physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
203 if (index > (IO_MEM_NOTDIRTY >> IO_MEM_SHIFT)
204 && !can_do_io(env)) {
205 cpu_io_recompile(env, retaddr);
208 env->mem_io_vaddr = addr;
209 env->mem_io_pc = (unsigned long)retaddr;
211 io_mem_write[index][SHIFT](io_mem_opaque[index], physaddr, val);
213 #ifdef TARGET_WORDS_BIGENDIAN
214 io_mem_write[index][2](io_mem_opaque[index], physaddr, val >> 32);
215 io_mem_write[index][2](io_mem_opaque[index], physaddr + 4, val);
217 io_mem_write[index][2](io_mem_opaque[index], physaddr, val);
218 io_mem_write[index][2](io_mem_opaque[index], physaddr + 4, val >> 32);
220 #endif /* SHIFT > 2 */
223 void REGPARM glue(glue(__st, SUFFIX), MMUSUFFIX)(target_ulong addr,
227 target_phys_addr_t addend;
228 target_ulong tlb_addr;
232 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
234 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
235 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
236 if (tlb_addr & ~TARGET_PAGE_MASK) {
238 if ((addr & (DATA_SIZE - 1)) != 0)
239 goto do_unaligned_access;
241 addend = env->iotlb[mmu_idx][index];
242 glue(io_write, SUFFIX)(addend, val, addr, retaddr);
243 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
247 do_unaligned_access(addr, 1, mmu_idx, retaddr);
249 glue(glue(slow_st, SUFFIX), MMUSUFFIX)(addr, val,
252 /* aligned/unaligned access in the same page */
254 if ((addr & (DATA_SIZE - 1)) != 0) {
256 do_unaligned_access(addr, 1, mmu_idx, retaddr);
259 addend = env->tlb_table[mmu_idx][index].addend;
260 glue(glue(st, SUFFIX), _raw)((uint8_t *)(long)(addr+addend), val);
263 /* the page is not in the TLB : fill it */
266 if ((addr & (DATA_SIZE - 1)) != 0)
267 do_unaligned_access(addr, 1, mmu_idx, retaddr);
269 tlb_fill(addr, 1, mmu_idx, retaddr);
274 /* handles all unaligned cases */
275 static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(target_ulong addr,
280 target_phys_addr_t addend;
281 target_ulong tlb_addr;
284 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
286 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
287 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
288 if (tlb_addr & ~TARGET_PAGE_MASK) {
290 if ((addr & (DATA_SIZE - 1)) != 0)
291 goto do_unaligned_access;
292 addend = env->iotlb[mmu_idx][index];
293 glue(io_write, SUFFIX)(addend, val, addr, retaddr);
294 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
296 /* XXX: not efficient, but simple */
297 /* Note: relies on the fact that tlb_fill() does not remove the
298 * previous page from the TLB cache. */
299 for(i = DATA_SIZE - 1; i >= 0; i--) {
300 #ifdef TARGET_WORDS_BIGENDIAN
301 glue(slow_stb, MMUSUFFIX)(addr + i, val >> (((DATA_SIZE - 1) * 8) - (i * 8)),
304 glue(slow_stb, MMUSUFFIX)(addr + i, val >> (i * 8),
309 /* aligned/unaligned access in the same page */
310 addend = env->tlb_table[mmu_idx][index].addend;
311 glue(glue(st, SUFFIX), _raw)((uint8_t *)(long)(addr+addend), val);
314 /* the page is not in the TLB : fill it */
315 tlb_fill(addr, 1, mmu_idx, retaddr);
320 #endif /* !defined(SOFTMMU_CODE_ACCESS) */
322 #undef READ_ACCESS_TYPE