]> git.proxmox.com Git - mirror_qemu.git/blame - softmmu_template.h
Fix curses return key when using -k
[mirror_qemu.git] / softmmu_template.h
CommitLineData
b92e5a22
FB
1/*
2 * Software MMU support
5fafdf24 3 *
b92e5a22
FB
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
b92e5a22
FB
18 */
19#define DATA_SIZE (1 << SHIFT)
20
21#if DATA_SIZE == 8
22#define SUFFIX q
61382a50 23#define USUFFIX q
b92e5a22
FB
24#define DATA_TYPE uint64_t
25#elif DATA_SIZE == 4
26#define SUFFIX l
61382a50 27#define USUFFIX l
b92e5a22
FB
28#define DATA_TYPE uint32_t
29#elif DATA_SIZE == 2
30#define SUFFIX w
61382a50 31#define USUFFIX uw
b92e5a22
FB
32#define DATA_TYPE uint16_t
33#elif DATA_SIZE == 1
34#define SUFFIX b
61382a50 35#define USUFFIX ub
b92e5a22
FB
36#define DATA_TYPE uint8_t
37#else
38#error unsupported data size
39#endif
40
b769d8fe
FB
41#ifdef SOFTMMU_CODE_ACCESS
42#define READ_ACCESS_TYPE 2
84b7b8e7 43#define ADDR_READ addr_code
b769d8fe
FB
44#else
45#define READ_ACCESS_TYPE 0
84b7b8e7 46#define ADDR_READ addr_read
b769d8fe
FB
47#endif
48
5fafdf24 49static DATA_TYPE glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
6ebbf390 50 int mmu_idx,
61382a50 51 void *retaddr);
c227f099 52static inline DATA_TYPE glue(io_read, SUFFIX)(target_phys_addr_t physaddr,
2e70f6ef
PB
53 target_ulong addr,
54 void *retaddr)
b92e5a22
FB
55{
56 DATA_TYPE res;
57 int index;
0f459d16
PB
58 index = (physaddr >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
59 physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
2e70f6ef
PB
60 env->mem_io_pc = (unsigned long)retaddr;
61 if (index > (IO_MEM_NOTDIRTY >> IO_MEM_SHIFT)
62 && !can_do_io(env)) {
63 cpu_io_recompile(env, retaddr);
64 }
b92e5a22 65
db8886d3 66 env->mem_io_vaddr = addr;
b92e5a22 67#if SHIFT <= 2
a4193c8a 68 res = io_mem_read[index][SHIFT](io_mem_opaque[index], physaddr);
b92e5a22
FB
69#else
70#ifdef TARGET_WORDS_BIGENDIAN
a4193c8a
FB
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);
b92e5a22 73#else
a4193c8a
FB
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;
b92e5a22
FB
76#endif
77#endif /* SHIFT > 2 */
78 return res;
79}
80
b92e5a22 81/* handle all cases except unaligned access which span two pages */
d656469f
FB
82DATA_TYPE REGPARM glue(glue(__ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
83 int mmu_idx)
b92e5a22
FB
84{
85 DATA_TYPE res;
61382a50 86 int index;
c27004ec 87 target_ulong tlb_addr;
c227f099 88 target_phys_addr_t addend;
b92e5a22 89 void *retaddr;
3b46e624 90
b92e5a22
FB
91 /* test if there is match for unaligned or IO access */
92 /* XXX: could done more in memory macro in a non portable way */
b92e5a22
FB
93 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
94 redo:
6ebbf390 95 tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
b92e5a22 96 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
b92e5a22
FB
97 if (tlb_addr & ~TARGET_PAGE_MASK) {
98 /* IO access */
99 if ((addr & (DATA_SIZE - 1)) != 0)
100 goto do_unaligned_access;
2e70f6ef 101 retaddr = GETPC();
0f459d16 102 addend = env->iotlb[mmu_idx][index];
2e70f6ef 103 res = glue(io_read, SUFFIX)(addend, addr, retaddr);
98699967 104 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
b92e5a22
FB
105 /* slow unaligned access (it spans two pages or IO) */
106 do_unaligned_access:
61382a50 107 retaddr = GETPC();
a64d4718 108#ifdef ALIGNED_ONLY
6ebbf390 109 do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
a64d4718 110#endif
5fafdf24 111 res = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr,
6ebbf390 112 mmu_idx, retaddr);
b92e5a22 113 } else {
a64d4718
FB
114 /* unaligned/aligned access in the same page */
115#ifdef ALIGNED_ONLY
116 if ((addr & (DATA_SIZE - 1)) != 0) {
117 retaddr = GETPC();
6ebbf390 118 do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
a64d4718
FB
119 }
120#endif
0f459d16
PB
121 addend = env->tlb_table[mmu_idx][index].addend;
122 res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)(long)(addr+addend));
b92e5a22
FB
123 }
124 } else {
125 /* the page is not in the TLB : fill it */
61382a50 126 retaddr = GETPC();
a64d4718
FB
127#ifdef ALIGNED_ONLY
128 if ((addr & (DATA_SIZE - 1)) != 0)
6ebbf390 129 do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
a64d4718 130#endif
6ebbf390 131 tlb_fill(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
b92e5a22
FB
132 goto redo;
133 }
134 return res;
135}
136
137/* handle all unaligned cases */
5fafdf24 138static DATA_TYPE glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
6ebbf390 139 int mmu_idx,
61382a50 140 void *retaddr)
b92e5a22
FB
141{
142 DATA_TYPE res, res1, res2;
61382a50 143 int index, shift;
c227f099 144 target_phys_addr_t addend;
c27004ec 145 target_ulong tlb_addr, addr1, addr2;
b92e5a22 146
b92e5a22
FB
147 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
148 redo:
6ebbf390 149 tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
b92e5a22 150 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
b92e5a22
FB
151 if (tlb_addr & ~TARGET_PAGE_MASK) {
152 /* IO access */
153 if ((addr & (DATA_SIZE - 1)) != 0)
154 goto do_unaligned_access;
0f459d16 155 addend = env->iotlb[mmu_idx][index];
2e70f6ef 156 res = glue(io_read, SUFFIX)(addend, addr, retaddr);
98699967 157 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
b92e5a22
FB
158 do_unaligned_access:
159 /* slow unaligned access (it spans two pages) */
160 addr1 = addr & ~(DATA_SIZE - 1);
161 addr2 = addr1 + DATA_SIZE;
5fafdf24 162 res1 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr1,
6ebbf390 163 mmu_idx, retaddr);
5fafdf24 164 res2 = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr2,
6ebbf390 165 mmu_idx, retaddr);
b92e5a22
FB
166 shift = (addr & (DATA_SIZE - 1)) * 8;
167#ifdef TARGET_WORDS_BIGENDIAN
168 res = (res1 << shift) | (res2 >> ((DATA_SIZE * 8) - shift));
169#else
170 res = (res1 >> shift) | (res2 << ((DATA_SIZE * 8) - shift));
171#endif
6986f88c 172 res = (DATA_TYPE)res;
b92e5a22
FB
173 } else {
174 /* unaligned/aligned access in the same page */
0f459d16
PB
175 addend = env->tlb_table[mmu_idx][index].addend;
176 res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)(long)(addr+addend));
b92e5a22
FB
177 }
178 } else {
179 /* the page is not in the TLB : fill it */
6ebbf390 180 tlb_fill(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);
b92e5a22
FB
181 goto redo;
182 }
183 return res;
184}
185
b769d8fe
FB
186#ifndef SOFTMMU_CODE_ACCESS
187
5fafdf24
TS
188static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(target_ulong addr,
189 DATA_TYPE val,
6ebbf390 190 int mmu_idx,
b769d8fe
FB
191 void *retaddr);
192
c227f099 193static inline void glue(io_write, SUFFIX)(target_phys_addr_t physaddr,
b769d8fe 194 DATA_TYPE val,
0f459d16 195 target_ulong addr,
b769d8fe
FB
196 void *retaddr)
197{
198 int index;
0f459d16
PB
199 index = (physaddr >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
200 physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
2e70f6ef
PB
201 if (index > (IO_MEM_NOTDIRTY >> IO_MEM_SHIFT)
202 && !can_do_io(env)) {
203 cpu_io_recompile(env, retaddr);
204 }
b769d8fe 205
2e70f6ef
PB
206 env->mem_io_vaddr = addr;
207 env->mem_io_pc = (unsigned long)retaddr;
b769d8fe
FB
208#if SHIFT <= 2
209 io_mem_write[index][SHIFT](io_mem_opaque[index], physaddr, val);
210#else
211#ifdef TARGET_WORDS_BIGENDIAN
212 io_mem_write[index][2](io_mem_opaque[index], physaddr, val >> 32);
213 io_mem_write[index][2](io_mem_opaque[index], physaddr + 4, val);
214#else
215 io_mem_write[index][2](io_mem_opaque[index], physaddr, val);
216 io_mem_write[index][2](io_mem_opaque[index], physaddr + 4, val >> 32);
217#endif
218#endif /* SHIFT > 2 */
219}
b92e5a22 220
d656469f
FB
221void REGPARM glue(glue(__st, SUFFIX), MMUSUFFIX)(target_ulong addr,
222 DATA_TYPE val,
223 int mmu_idx)
b92e5a22 224{
c227f099 225 target_phys_addr_t addend;
c27004ec 226 target_ulong tlb_addr;
b92e5a22 227 void *retaddr;
61382a50 228 int index;
3b46e624 229
b92e5a22
FB
230 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
231 redo:
6ebbf390 232 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
b92e5a22 233 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
b92e5a22
FB
234 if (tlb_addr & ~TARGET_PAGE_MASK) {
235 /* IO access */
236 if ((addr & (DATA_SIZE - 1)) != 0)
237 goto do_unaligned_access;
d720b93d 238 retaddr = GETPC();
0f459d16
PB
239 addend = env->iotlb[mmu_idx][index];
240 glue(io_write, SUFFIX)(addend, val, addr, retaddr);
98699967 241 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
b92e5a22 242 do_unaligned_access:
61382a50 243 retaddr = GETPC();
a64d4718 244#ifdef ALIGNED_ONLY
6ebbf390 245 do_unaligned_access(addr, 1, mmu_idx, retaddr);
a64d4718 246#endif
5fafdf24 247 glue(glue(slow_st, SUFFIX), MMUSUFFIX)(addr, val,
6ebbf390 248 mmu_idx, retaddr);
b92e5a22
FB
249 } else {
250 /* aligned/unaligned access in the same page */
a64d4718
FB
251#ifdef ALIGNED_ONLY
252 if ((addr & (DATA_SIZE - 1)) != 0) {
253 retaddr = GETPC();
6ebbf390 254 do_unaligned_access(addr, 1, mmu_idx, retaddr);
a64d4718
FB
255 }
256#endif
0f459d16
PB
257 addend = env->tlb_table[mmu_idx][index].addend;
258 glue(glue(st, SUFFIX), _raw)((uint8_t *)(long)(addr+addend), val);
b92e5a22
FB
259 }
260 } else {
261 /* the page is not in the TLB : fill it */
61382a50 262 retaddr = GETPC();
a64d4718
FB
263#ifdef ALIGNED_ONLY
264 if ((addr & (DATA_SIZE - 1)) != 0)
6ebbf390 265 do_unaligned_access(addr, 1, mmu_idx, retaddr);
a64d4718 266#endif
6ebbf390 267 tlb_fill(addr, 1, mmu_idx, retaddr);
b92e5a22
FB
268 goto redo;
269 }
270}
271
272/* handles all unaligned cases */
5fafdf24 273static void glue(glue(slow_st, SUFFIX), MMUSUFFIX)(target_ulong addr,
61382a50 274 DATA_TYPE val,
6ebbf390 275 int mmu_idx,
61382a50 276 void *retaddr)
b92e5a22 277{
c227f099 278 target_phys_addr_t addend;
c27004ec 279 target_ulong tlb_addr;
61382a50 280 int index, i;
b92e5a22 281
b92e5a22
FB
282 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
283 redo:
6ebbf390 284 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
b92e5a22 285 if ((addr & TARGET_PAGE_MASK) == (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
b92e5a22
FB
286 if (tlb_addr & ~TARGET_PAGE_MASK) {
287 /* IO access */
288 if ((addr & (DATA_SIZE - 1)) != 0)
289 goto do_unaligned_access;
0f459d16
PB
290 addend = env->iotlb[mmu_idx][index];
291 glue(io_write, SUFFIX)(addend, val, addr, retaddr);
98699967 292 } else if (((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1) >= TARGET_PAGE_SIZE) {
b92e5a22
FB
293 do_unaligned_access:
294 /* XXX: not efficient, but simple */
6c41b272
AZ
295 /* Note: relies on the fact that tlb_fill() does not remove the
296 * previous page from the TLB cache. */
7221fa98 297 for(i = DATA_SIZE - 1; i >= 0; i--) {
b92e5a22 298#ifdef TARGET_WORDS_BIGENDIAN
5fafdf24 299 glue(slow_stb, MMUSUFFIX)(addr + i, val >> (((DATA_SIZE - 1) * 8) - (i * 8)),
6ebbf390 300 mmu_idx, retaddr);
b92e5a22 301#else
5fafdf24 302 glue(slow_stb, MMUSUFFIX)(addr + i, val >> (i * 8),
6ebbf390 303 mmu_idx, retaddr);
b92e5a22
FB
304#endif
305 }
306 } else {
307 /* aligned/unaligned access in the same page */
0f459d16
PB
308 addend = env->tlb_table[mmu_idx][index].addend;
309 glue(glue(st, SUFFIX), _raw)((uint8_t *)(long)(addr+addend), val);
b92e5a22
FB
310 }
311 } else {
312 /* the page is not in the TLB : fill it */
6ebbf390 313 tlb_fill(addr, 1, mmu_idx, retaddr);
b92e5a22
FB
314 goto redo;
315 }
316}
317
b769d8fe
FB
318#endif /* !defined(SOFTMMU_CODE_ACCESS) */
319
320#undef READ_ACCESS_TYPE
b92e5a22
FB
321#undef SHIFT
322#undef DATA_TYPE
323#undef SUFFIX
61382a50 324#undef USUFFIX
b92e5a22 325#undef DATA_SIZE
84b7b8e7 326#undef ADDR_READ