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