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"
26 #define DATA_SIZE (1 << SHIFT)
31 #define DATA_TYPE uint64_t
35 #define DATA_TYPE uint32_t
39 #define DATA_TYPE uint16_t
43 #define DATA_TYPE uint8_t
45 #error unsupported data size
48 #ifdef SOFTMMU_CODE_ACCESS
49 #define READ_ACCESS_TYPE 2
50 #define ADDR_READ addr_code
52 #define READ_ACCESS_TYPE 0
53 #define ADDR_READ addr_read
56 static DATA_TYPE glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
59 static inline DATA_TYPE glue(io_read, SUFFIX)(target_phys_addr_t physaddr,
65 index = (physaddr >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
66 physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
67 env->mem_io_pc = (unsigned long)retaddr;
68 if (index > (IO_MEM_NOTDIRTY >> IO_MEM_SHIFT)
70 cpu_io_recompile(env, retaddr);
73 env->mem_io_vaddr = addr;
75 res = io_mem_read[index][SHIFT](io_mem_opaque[index], physaddr);
77 #ifdef TARGET_WORDS_BIGENDIAN
78 res = (uint64_t)io_mem_read[index][2](io_mem_opaque[index], physaddr) << 32;
79 res |= io_mem_read[index][2](io_mem_opaque[index], physaddr + 4);
81 res = io_mem_read[index][2](io_mem_opaque[index], physaddr);
82 res |= (uint64_t)io_mem_read[index][2](io_mem_opaque[index], physaddr + 4) << 32;
84 #endif /* SHIFT > 2 */
88 /* handle all cases except unaligned access which span two pages */
89 DATA_TYPE REGPARM glue(glue(__ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
94 target_ulong tlb_addr;
95 target_phys_addr_t ioaddr;
99 /* test if there is match for unaligned or IO access */
100 /* XXX: could done more in memory macro in a non portable way */
101 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
103 tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
104 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
105 if (tlb_addr & ~TARGET_PAGE_MASK) {
107 if ((addr & (DATA_SIZE - 1)) != 0)
108 goto do_unaligned_access;
110 ioaddr = env->iotlb[mmu_idx][index];
111 res = glue(io_read, SUFFIX)(ioaddr, addr, retaddr);
112 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
113 /* slow unaligned access (it spans two pages or IO) */
117 do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
119 res = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr,
122 /* unaligned/aligned access in the same page */
124 if ((addr & (DATA_SIZE - 1)) != 0) {
126 do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
129 addend = env->tlb_table[mmu_idx][index].addend;
130 res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)(long)(addr+addend));
133 /* the page is not in the TLB : fill it */
136 if ((addr & (DATA_SIZE - 1)) != 0)
137 do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
139 tlb_fill(env, addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
145 /* handle all unaligned cases */
146 static DATA_TYPE glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
150 DATA_TYPE res, res1, res2;
152 target_phys_addr_t ioaddr;
153 unsigned long addend;
154 target_ulong tlb_addr, addr1, addr2;
156 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
158 tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
159 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
160 if (tlb_addr & ~TARGET_PAGE_MASK) {
162 if ((addr & (DATA_SIZE - 1)) != 0)
163 goto do_unaligned_access;
164 ioaddr = env->iotlb[mmu_idx][index];
165 res = glue(io_read, SUFFIX)(ioaddr, addr, retaddr);
166 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
168 /* slow unaligned access (it spans two pages) */
169 addr1 = addr & ~(DATA_SIZE - 1);
170 addr2 = addr1 + DATA_SIZE;
171 res1 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr1,
173 res2 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr2,
175 shift = (addr & (DATA_SIZE - 1)) * 8;
176 #ifdef TARGET_WORDS_BIGENDIAN
177 res = (res1 << shift) | (res2 >> ((DATA_SIZE * 8) - shift));
179 res = (res1 >> shift) | (res2 << ((DATA_SIZE * 8) - shift));
181 res = (DATA_TYPE)res;
183 /* unaligned/aligned access in the same page */
184 addend = env->tlb_table[mmu_idx][index].addend;
185 res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)(long)(addr+addend));
188 /* the page is not in the TLB : fill it */
189 tlb_fill(env, addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
195 #ifndef SOFTMMU_CODE_ACCESS
197 static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(target_ulong addr,
202 static inline void glue(io_write, SUFFIX)(target_phys_addr_t physaddr,
208 index = (physaddr >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
209 physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
210 if (index > (IO_MEM_NOTDIRTY >> IO_MEM_SHIFT)
211 && !can_do_io(env)) {
212 cpu_io_recompile(env, retaddr);
215 env->mem_io_vaddr = addr;
216 env->mem_io_pc = (unsigned long)retaddr;
218 io_mem_write[index][SHIFT](io_mem_opaque[index], physaddr, val);
220 #ifdef TARGET_WORDS_BIGENDIAN
221 io_mem_write[index][2](io_mem_opaque[index], physaddr, val >> 32);
222 io_mem_write[index][2](io_mem_opaque[index], physaddr + 4, val);
224 io_mem_write[index][2](io_mem_opaque[index], physaddr, val);
225 io_mem_write[index][2](io_mem_opaque[index], physaddr + 4, val >> 32);
227 #endif /* SHIFT > 2 */
230 void REGPARM glue(glue(__st, SUFFIX), MMUSUFFIX)(target_ulong addr,
234 target_phys_addr_t ioaddr;
235 unsigned long addend;
236 target_ulong tlb_addr;
240 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
242 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
243 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
244 if (tlb_addr & ~TARGET_PAGE_MASK) {
246 if ((addr & (DATA_SIZE - 1)) != 0)
247 goto do_unaligned_access;
249 ioaddr = env->iotlb[mmu_idx][index];
250 glue(io_write, SUFFIX)(ioaddr, val, addr, retaddr);
251 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
255 do_unaligned_access(addr, 1, mmu_idx, retaddr);
257 glue(glue(slow_st, SUFFIX), MMUSUFFIX)(addr, val,
260 /* aligned/unaligned access in the same page */
262 if ((addr & (DATA_SIZE - 1)) != 0) {
264 do_unaligned_access(addr, 1, mmu_idx, retaddr);
267 addend = env->tlb_table[mmu_idx][index].addend;
268 glue(glue(st, SUFFIX), _raw)((uint8_t *)(long)(addr+addend), val);
271 /* the page is not in the TLB : fill it */
274 if ((addr & (DATA_SIZE - 1)) != 0)
275 do_unaligned_access(addr, 1, mmu_idx, retaddr);
277 tlb_fill(env, addr, 1, mmu_idx, retaddr);
282 /* handles all unaligned cases */
283 static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(target_ulong addr,
288 target_phys_addr_t ioaddr;
289 unsigned long addend;
290 target_ulong tlb_addr;
293 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
295 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
296 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
297 if (tlb_addr & ~TARGET_PAGE_MASK) {
299 if ((addr & (DATA_SIZE - 1)) != 0)
300 goto do_unaligned_access;
301 ioaddr = env->iotlb[mmu_idx][index];
302 glue(io_write, SUFFIX)(ioaddr, val, addr, retaddr);
303 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
305 /* XXX: not efficient, but simple */
306 /* Note: relies on the fact that tlb_fill() does not remove the
307 * previous page from the TLB cache. */
308 for(i = DATA_SIZE - 1; i >= 0; i--) {
309 #ifdef TARGET_WORDS_BIGENDIAN
310 glue(slow_stb, MMUSUFFIX)(addr + i, val >> (((DATA_SIZE - 1) * 8) - (i * 8)),
313 glue(slow_stb, MMUSUFFIX)(addr + i, val >> (i * 8),
318 /* aligned/unaligned access in the same page */
319 addend = env->tlb_table[mmu_idx][index].addend;
320 glue(glue(st, SUFFIX), _raw)((uint8_t *)(long)(addr+addend), val);
323 /* the page is not in the TLB : fill it */
324 tlb_fill(env, addr, 1, mmu_idx, retaddr);
329 #endif /* !defined(SOFTMMU_CODE_ACCESS) */
331 #undef READ_ACCESS_TYPE