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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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,
54 target_ulong tlb_addr)
59 index = (tlb_addr >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
61 res = io_mem_read[index][SHIFT](io_mem_opaque[index], physaddr);
63 #ifdef TARGET_WORDS_BIGENDIAN
64 res = (uint64_t)io_mem_read[index][2](io_mem_opaque[index], physaddr) << 32;
65 res |= io_mem_read[index][2](io_mem_opaque[index], physaddr + 4);
67 res = io_mem_read[index][2](io_mem_opaque[index], physaddr);
68 res |= (uint64_t)io_mem_read[index][2](io_mem_opaque[index], physaddr + 4) << 32;
70 #endif /* SHIFT > 2 */
72 env->last_io_time = cpu_get_time_fast();
77 /* handle all cases except unaligned access which span two pages */
78 DATA_TYPE REGPARM(1) glue(glue(__ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
83 target_ulong tlb_addr;
84 target_phys_addr_t physaddr;
87 /* test if there is match for unaligned or IO access */
88 /* XXX: could done more in memory macro in a non portable way */
89 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
91 tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
92 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
93 physaddr = addr + env->tlb_table[mmu_idx][index].addend;
94 if (tlb_addr & ~TARGET_PAGE_MASK) {
96 if ((addr & (DATA_SIZE - 1)) != 0)
97 goto do_unaligned_access;
98 res = glue(io_read, SUFFIX)(physaddr, tlb_addr);
99 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
100 /* slow unaligned access (it spans two pages or IO) */
104 do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
106 res = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr,
109 /* unaligned/aligned access in the same page */
111 if ((addr & (DATA_SIZE - 1)) != 0) {
113 do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
116 res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)(long)physaddr);
119 /* the page is not in the TLB : fill it */
122 if ((addr & (DATA_SIZE - 1)) != 0)
123 do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
125 tlb_fill(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
131 /* handle all unaligned cases */
132 static DATA_TYPE glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
136 DATA_TYPE res, res1, res2;
138 target_phys_addr_t physaddr;
139 target_ulong tlb_addr, addr1, addr2;
141 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
143 tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
144 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
145 physaddr = addr + env->tlb_table[mmu_idx][index].addend;
146 if (tlb_addr & ~TARGET_PAGE_MASK) {
148 if ((addr & (DATA_SIZE - 1)) != 0)
149 goto do_unaligned_access;
150 res = glue(io_read, SUFFIX)(physaddr, tlb_addr);
151 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
153 /* slow unaligned access (it spans two pages) */
154 addr1 = addr & ~(DATA_SIZE - 1);
155 addr2 = addr1 + DATA_SIZE;
156 res1 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr1,
158 res2 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr2,
160 shift = (addr & (DATA_SIZE - 1)) * 8;
161 #ifdef TARGET_WORDS_BIGENDIAN
162 res = (res1 << shift) | (res2 >> ((DATA_SIZE * 8) - shift));
164 res = (res1 >> shift) | (res2 << ((DATA_SIZE * 8) - shift));
166 res = (DATA_TYPE)res;
168 /* unaligned/aligned access in the same page */
169 res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)(long)physaddr);
172 /* the page is not in the TLB : fill it */
173 tlb_fill(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
179 #ifndef SOFTMMU_CODE_ACCESS
181 static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(target_ulong addr,
186 static inline void glue(io_write, SUFFIX)(target_phys_addr_t physaddr,
188 target_ulong tlb_addr,
193 index = (tlb_addr >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
194 env->mem_write_vaddr = tlb_addr;
195 env->mem_write_pc = (unsigned long)retaddr;
197 io_mem_write[index][SHIFT](io_mem_opaque[index], physaddr, val);
199 #ifdef TARGET_WORDS_BIGENDIAN
200 io_mem_write[index][2](io_mem_opaque[index], physaddr, val >> 32);
201 io_mem_write[index][2](io_mem_opaque[index], physaddr + 4, val);
203 io_mem_write[index][2](io_mem_opaque[index], physaddr, val);
204 io_mem_write[index][2](io_mem_opaque[index], physaddr + 4, val >> 32);
206 #endif /* SHIFT > 2 */
208 env->last_io_time = cpu_get_time_fast();
212 void REGPARM(2) glue(glue(__st, SUFFIX), MMUSUFFIX)(target_ulong addr,
216 target_phys_addr_t physaddr;
217 target_ulong tlb_addr;
221 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
223 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
224 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
225 physaddr = addr + env->tlb_table[mmu_idx][index].addend;
226 if (tlb_addr & ~TARGET_PAGE_MASK) {
228 if ((addr & (DATA_SIZE - 1)) != 0)
229 goto do_unaligned_access;
231 glue(io_write, SUFFIX)(physaddr, val, tlb_addr, retaddr);
232 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
236 do_unaligned_access(addr, 1, mmu_idx, retaddr);
238 glue(glue(slow_st, SUFFIX), MMUSUFFIX)(addr, val,
241 /* aligned/unaligned access in the same page */
243 if ((addr & (DATA_SIZE - 1)) != 0) {
245 do_unaligned_access(addr, 1, mmu_idx, retaddr);
248 glue(glue(st, SUFFIX), _raw)((uint8_t *)(long)physaddr, val);
251 /* the page is not in the TLB : fill it */
254 if ((addr & (DATA_SIZE - 1)) != 0)
255 do_unaligned_access(addr, 1, mmu_idx, retaddr);
257 tlb_fill(addr, 1, mmu_idx, retaddr);
262 /* handles all unaligned cases */
263 static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(target_ulong addr,
268 target_phys_addr_t physaddr;
269 target_ulong tlb_addr;
272 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
274 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
275 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
276 physaddr = addr + env->tlb_table[mmu_idx][index].addend;
277 if (tlb_addr & ~TARGET_PAGE_MASK) {
279 if ((addr & (DATA_SIZE - 1)) != 0)
280 goto do_unaligned_access;
281 glue(io_write, SUFFIX)(physaddr, val, tlb_addr, retaddr);
282 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
284 /* XXX: not efficient, but simple */
285 for(i = 0;i < DATA_SIZE; i++) {
286 #ifdef TARGET_WORDS_BIGENDIAN
287 glue(slow_stb, MMUSUFFIX)(addr + i, val >> (((DATA_SIZE - 1) * 8) - (i * 8)),
290 glue(slow_stb, MMUSUFFIX)(addr + i, val >> (i * 8),
295 /* aligned/unaligned access in the same page */
296 glue(glue(st, SUFFIX), _raw)((uint8_t *)(long)physaddr, val);
299 /* the page is not in the TLB : fill it */
300 tlb_fill(addr, 1, mmu_idx, retaddr);
305 #endif /* !defined(SOFTMMU_CODE_ACCESS) */
307 #undef READ_ACCESS_TYPE