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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
20 #define DATA_SIZE (1 << SHIFT)
25 #define DATA_TYPE uint64_t
29 #define DATA_TYPE uint32_t
33 #define DATA_TYPE uint16_t
37 #define DATA_TYPE uint8_t
39 #error unsupported data size
42 #ifdef SOFTMMU_CODE_ACCESS
43 #define READ_ACCESS_TYPE 2
44 #define ADDR_READ addr_code
46 #define READ_ACCESS_TYPE 0
47 #define ADDR_READ addr_read
50 static DATA_TYPE glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
53 static inline DATA_TYPE glue(io_read, SUFFIX)(target_phys_addr_t physaddr,
59 index = (physaddr >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
60 physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
61 env->mem_io_pc = (unsigned long)retaddr;
62 if (index > (IO_MEM_NOTDIRTY >> IO_MEM_SHIFT)
64 cpu_io_recompile(env, retaddr);
67 env->mem_io_vaddr = addr;
69 res = io_mem_read[index][SHIFT](io_mem_opaque[index], physaddr);
71 #ifdef TARGET_WORDS_BIGENDIAN
72 res = (uint64_t)io_mem_read[index][2](io_mem_opaque[index], physaddr) << 32;
73 res |= io_mem_read[index][2](io_mem_opaque[index], physaddr + 4);
75 res = io_mem_read[index][2](io_mem_opaque[index], physaddr);
76 res |= (uint64_t)io_mem_read[index][2](io_mem_opaque[index], physaddr + 4) << 32;
78 #endif /* SHIFT > 2 */
80 env->last_io_time = cpu_get_time_fast();
85 /* handle all cases except unaligned access which span two pages */
86 DATA_TYPE REGPARM glue(glue(__ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
91 target_ulong tlb_addr;
92 target_phys_addr_t addend;
95 /* test if there is match for unaligned or IO access */
96 /* XXX: could done more in memory macro in a non portable way */
97 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
99 tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
100 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
101 if (tlb_addr & ~TARGET_PAGE_MASK) {
103 if ((addr & (DATA_SIZE - 1)) != 0)
104 goto do_unaligned_access;
106 addend = env->iotlb[mmu_idx][index];
107 res = glue(io_read, SUFFIX)(addend, addr, retaddr);
108 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
109 /* slow unaligned access (it spans two pages or IO) */
113 do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
115 res = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr,
118 /* unaligned/aligned access in the same page */
120 if ((addr & (DATA_SIZE - 1)) != 0) {
122 do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
125 addend = env->tlb_table[mmu_idx][index].addend;
126 res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)(long)(addr+addend));
129 /* the page is not in the TLB : fill it */
132 if ((addr & (DATA_SIZE - 1)) != 0)
133 do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
135 tlb_fill(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
141 /* handle all unaligned cases */
142 static DATA_TYPE glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
146 DATA_TYPE res, res1, res2;
148 target_phys_addr_t addend;
149 target_ulong tlb_addr, addr1, addr2;
151 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
153 tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
154 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
155 if (tlb_addr & ~TARGET_PAGE_MASK) {
157 if ((addr & (DATA_SIZE - 1)) != 0)
158 goto do_unaligned_access;
160 addend = env->iotlb[mmu_idx][index];
161 res = glue(io_read, SUFFIX)(addend, addr, retaddr);
162 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
164 /* slow unaligned access (it spans two pages) */
165 addr1 = addr & ~(DATA_SIZE - 1);
166 addr2 = addr1 + DATA_SIZE;
167 res1 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr1,
169 res2 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr2,
171 shift = (addr & (DATA_SIZE - 1)) * 8;
172 #ifdef TARGET_WORDS_BIGENDIAN
173 res = (res1 << shift) | (res2 >> ((DATA_SIZE * 8) - shift));
175 res = (res1 >> shift) | (res2 << ((DATA_SIZE * 8) - shift));
177 res = (DATA_TYPE)res;
179 /* unaligned/aligned access in the same page */
180 addend = env->tlb_table[mmu_idx][index].addend;
181 res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)(long)(addr+addend));
184 /* the page is not in the TLB : fill it */
185 tlb_fill(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
191 #ifndef SOFTMMU_CODE_ACCESS
193 static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(target_ulong addr,
198 static inline void glue(io_write, SUFFIX)(target_phys_addr_t physaddr,
204 index = (physaddr >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
205 physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
206 if (index > (IO_MEM_NOTDIRTY >> IO_MEM_SHIFT)
207 && !can_do_io(env)) {
208 cpu_io_recompile(env, retaddr);
211 env->mem_io_vaddr = addr;
212 env->mem_io_pc = (unsigned long)retaddr;
214 io_mem_write[index][SHIFT](io_mem_opaque[index], physaddr, val);
216 #ifdef TARGET_WORDS_BIGENDIAN
217 io_mem_write[index][2](io_mem_opaque[index], physaddr, val >> 32);
218 io_mem_write[index][2](io_mem_opaque[index], physaddr + 4, val);
220 io_mem_write[index][2](io_mem_opaque[index], physaddr, val);
221 io_mem_write[index][2](io_mem_opaque[index], physaddr + 4, val >> 32);
223 #endif /* SHIFT > 2 */
225 env->last_io_time = cpu_get_time_fast();
229 void REGPARM glue(glue(__st, SUFFIX), MMUSUFFIX)(target_ulong addr,
233 target_phys_addr_t addend;
234 target_ulong tlb_addr;
238 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
240 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
241 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
242 if (tlb_addr & ~TARGET_PAGE_MASK) {
244 if ((addr & (DATA_SIZE - 1)) != 0)
245 goto do_unaligned_access;
247 addend = env->iotlb[mmu_idx][index];
248 glue(io_write, SUFFIX)(addend, val, addr, retaddr);
249 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
253 do_unaligned_access(addr, 1, mmu_idx, retaddr);
255 glue(glue(slow_st, SUFFIX), MMUSUFFIX)(addr, val,
258 /* aligned/unaligned access in the same page */
260 if ((addr & (DATA_SIZE - 1)) != 0) {
262 do_unaligned_access(addr, 1, mmu_idx, retaddr);
265 addend = env->tlb_table[mmu_idx][index].addend;
266 glue(glue(st, SUFFIX), _raw)((uint8_t *)(long)(addr+addend), val);
269 /* the page is not in the TLB : fill it */
272 if ((addr & (DATA_SIZE - 1)) != 0)
273 do_unaligned_access(addr, 1, mmu_idx, retaddr);
275 tlb_fill(addr, 1, mmu_idx, retaddr);
280 /* handles all unaligned cases */
281 static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(target_ulong addr,
286 target_phys_addr_t addend;
287 target_ulong tlb_addr;
290 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
292 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
293 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
294 if (tlb_addr & ~TARGET_PAGE_MASK) {
296 if ((addr & (DATA_SIZE - 1)) != 0)
297 goto do_unaligned_access;
298 addend = env->iotlb[mmu_idx][index];
299 glue(io_write, SUFFIX)(addend, val, addr, retaddr);
300 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
302 /* XXX: not efficient, but simple */
303 /* Note: relies on the fact that tlb_fill() does not remove the
304 * previous page from the TLB cache. */
305 for(i = DATA_SIZE - 1; i >= 0; i--) {
306 #ifdef TARGET_WORDS_BIGENDIAN
307 glue(slow_stb, MMUSUFFIX)(addr + i, val >> (((DATA_SIZE - 1) * 8) - (i * 8)),
310 glue(slow_stb, MMUSUFFIX)(addr + i, val >> (i * 8),
315 /* aligned/unaligned access in the same page */
316 addend = env->tlb_table[mmu_idx][index].addend;
317 glue(glue(st, SUFFIX), _raw)((uint8_t *)(long)(addr+addend), val);
320 /* the page is not in the TLB : fill it */
321 tlb_fill(addr, 1, mmu_idx, retaddr);
326 #endif /* !defined(SOFTMMU_CODE_ACCESS) */
328 #undef READ_ACCESS_TYPE