virtio-blk: add bootindex to qom property
[qemu/cris-port.git] / softmmu_template.h
blob88e33900b6f4b1ed002acb2c7204ec2e5d9a4b9e
1 /*
2 * Software MMU support
4 * Generate helpers used by TCG for qemu_ld/st ops and code load
5 * functions.
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"
25 #include "exec/address-spaces.h"
26 #include "exec/memory.h"
28 #define DATA_SIZE (1 << SHIFT)
30 #if DATA_SIZE == 8
31 #define SUFFIX q
32 #define LSUFFIX q
33 #define SDATA_TYPE int64_t
34 #define DATA_TYPE uint64_t
35 #elif DATA_SIZE == 4
36 #define SUFFIX l
37 #define LSUFFIX l
38 #define SDATA_TYPE int32_t
39 #define DATA_TYPE uint32_t
40 #elif DATA_SIZE == 2
41 #define SUFFIX w
42 #define LSUFFIX uw
43 #define SDATA_TYPE int16_t
44 #define DATA_TYPE uint16_t
45 #elif DATA_SIZE == 1
46 #define SUFFIX b
47 #define LSUFFIX ub
48 #define SDATA_TYPE int8_t
49 #define DATA_TYPE uint8_t
50 #else
51 #error unsupported data size
52 #endif
55 /* For the benefit of TCG generated code, we want to avoid the complication
56 of ABI-specific return type promotion and always return a value extended
57 to the register size of the host. This is tcg_target_long, except in the
58 case of a 32-bit host and 64-bit data, and for that we always have
59 uint64_t. Don't bother with this widened value for SOFTMMU_CODE_ACCESS. */
60 #if defined(SOFTMMU_CODE_ACCESS) || DATA_SIZE == 8
61 # define WORD_TYPE DATA_TYPE
62 # define USUFFIX SUFFIX
63 #else
64 # define WORD_TYPE tcg_target_ulong
65 # define USUFFIX glue(u, SUFFIX)
66 # define SSUFFIX glue(s, SUFFIX)
67 #endif
69 #ifdef SOFTMMU_CODE_ACCESS
70 #define READ_ACCESS_TYPE 2
71 #define ADDR_READ addr_code
72 #else
73 #define READ_ACCESS_TYPE 0
74 #define ADDR_READ addr_read
75 #endif
77 #if DATA_SIZE == 8
78 # define BSWAP(X) bswap64(X)
79 #elif DATA_SIZE == 4
80 # define BSWAP(X) bswap32(X)
81 #elif DATA_SIZE == 2
82 # define BSWAP(X) bswap16(X)
83 #else
84 # define BSWAP(X) (X)
85 #endif
87 #ifdef TARGET_WORDS_BIGENDIAN
88 # define TGT_BE(X) (X)
89 # define TGT_LE(X) BSWAP(X)
90 #else
91 # define TGT_BE(X) BSWAP(X)
92 # define TGT_LE(X) (X)
93 #endif
95 #if DATA_SIZE == 1
96 # define helper_le_ld_name glue(glue(helper_ret_ld, USUFFIX), MMUSUFFIX)
97 # define helper_be_ld_name helper_le_ld_name
98 # define helper_le_lds_name glue(glue(helper_ret_ld, SSUFFIX), MMUSUFFIX)
99 # define helper_be_lds_name helper_le_lds_name
100 # define helper_le_st_name glue(glue(helper_ret_st, SUFFIX), MMUSUFFIX)
101 # define helper_be_st_name helper_le_st_name
102 #else
103 # define helper_le_ld_name glue(glue(helper_le_ld, USUFFIX), MMUSUFFIX)
104 # define helper_be_ld_name glue(glue(helper_be_ld, USUFFIX), MMUSUFFIX)
105 # define helper_le_lds_name glue(glue(helper_le_ld, SSUFFIX), MMUSUFFIX)
106 # define helper_be_lds_name glue(glue(helper_be_ld, SSUFFIX), MMUSUFFIX)
107 # define helper_le_st_name glue(glue(helper_le_st, SUFFIX), MMUSUFFIX)
108 # define helper_be_st_name glue(glue(helper_be_st, SUFFIX), MMUSUFFIX)
109 #endif
111 #ifdef TARGET_WORDS_BIGENDIAN
112 # define helper_te_ld_name helper_be_ld_name
113 # define helper_te_st_name helper_be_st_name
114 #else
115 # define helper_te_ld_name helper_le_ld_name
116 # define helper_te_st_name helper_le_st_name
117 #endif
119 /* macro to check the victim tlb */
120 #define VICTIM_TLB_HIT(ty) \
121 ({ \
122 /* we are about to do a page table walk. our last hope is the \
123 * victim tlb. try to refill from the victim tlb before walking the \
124 * page table. */ \
125 int vidx; \
126 hwaddr tmpiotlb; \
127 CPUTLBEntry tmptlb; \
128 for (vidx = CPU_VTLB_SIZE-1; vidx >= 0; --vidx) { \
129 if (env->tlb_v_table[mmu_idx][vidx].ty == (addr & TARGET_PAGE_MASK)) {\
130 /* found entry in victim tlb, swap tlb and iotlb */ \
131 tmptlb = env->tlb_table[mmu_idx][index]; \
132 env->tlb_table[mmu_idx][index] = env->tlb_v_table[mmu_idx][vidx]; \
133 env->tlb_v_table[mmu_idx][vidx] = tmptlb; \
134 tmpiotlb = env->iotlb[mmu_idx][index]; \
135 env->iotlb[mmu_idx][index] = env->iotlb_v[mmu_idx][vidx]; \
136 env->iotlb_v[mmu_idx][vidx] = tmpiotlb; \
137 break; \
140 /* return true when there is a vtlb hit, i.e. vidx >=0 */ \
141 vidx >= 0; \
144 #ifndef SOFTMMU_CODE_ACCESS
145 static inline DATA_TYPE glue(io_read, SUFFIX)(CPUArchState *env,
146 hwaddr physaddr,
147 target_ulong addr,
148 uintptr_t retaddr)
150 uint64_t val;
151 CPUState *cpu = ENV_GET_CPU(env);
152 MemoryRegion *mr = iotlb_to_region(cpu->as, physaddr);
154 physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
155 cpu->mem_io_pc = retaddr;
156 if (mr != &io_mem_rom && mr != &io_mem_notdirty && !cpu_can_do_io(cpu)) {
157 cpu_io_recompile(cpu, retaddr);
160 cpu->mem_io_vaddr = addr;
161 io_mem_read(mr, physaddr, &val, 1 << SHIFT);
162 return val;
164 #endif
166 #ifdef SOFTMMU_CODE_ACCESS
167 static __attribute__((unused))
168 #endif
169 WORD_TYPE helper_le_ld_name(CPUArchState *env, target_ulong addr, int mmu_idx,
170 uintptr_t retaddr)
172 int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
173 target_ulong tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
174 uintptr_t haddr;
175 DATA_TYPE res;
177 /* Adjust the given return address. */
178 retaddr -= GETPC_ADJ;
180 /* If the TLB entry is for a different page, reload and try again. */
181 if ((addr & TARGET_PAGE_MASK)
182 != (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
183 #ifdef ALIGNED_ONLY
184 if ((addr & (DATA_SIZE - 1)) != 0) {
185 cpu_unaligned_access(ENV_GET_CPU(env), addr, READ_ACCESS_TYPE,
186 mmu_idx, retaddr);
188 #endif
189 if (!VICTIM_TLB_HIT(ADDR_READ)) {
190 tlb_fill(ENV_GET_CPU(env), addr, READ_ACCESS_TYPE,
191 mmu_idx, retaddr);
193 tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
196 /* Handle an IO access. */
197 if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
198 hwaddr ioaddr;
199 if ((addr & (DATA_SIZE - 1)) != 0) {
200 goto do_unaligned_access;
202 ioaddr = env->iotlb[mmu_idx][index];
204 /* ??? Note that the io helpers always read data in the target
205 byte ordering. We should push the LE/BE request down into io. */
206 res = glue(io_read, SUFFIX)(env, ioaddr, addr, retaddr);
207 res = TGT_LE(res);
208 return res;
211 /* Handle slow unaligned access (it spans two pages or IO). */
212 if (DATA_SIZE > 1
213 && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
214 >= TARGET_PAGE_SIZE)) {
215 target_ulong addr1, addr2;
216 DATA_TYPE res1, res2;
217 unsigned shift;
218 do_unaligned_access:
219 #ifdef ALIGNED_ONLY
220 cpu_unaligned_access(ENV_GET_CPU(env), addr, READ_ACCESS_TYPE,
221 mmu_idx, retaddr);
222 #endif
223 addr1 = addr & ~(DATA_SIZE - 1);
224 addr2 = addr1 + DATA_SIZE;
225 /* Note the adjustment at the beginning of the function.
226 Undo that for the recursion. */
227 res1 = helper_le_ld_name(env, addr1, mmu_idx, retaddr + GETPC_ADJ);
228 res2 = helper_le_ld_name(env, addr2, mmu_idx, retaddr + GETPC_ADJ);
229 shift = (addr & (DATA_SIZE - 1)) * 8;
231 /* Little-endian combine. */
232 res = (res1 >> shift) | (res2 << ((DATA_SIZE * 8) - shift));
233 return res;
236 /* Handle aligned access or unaligned access in the same page. */
237 #ifdef ALIGNED_ONLY
238 if ((addr & (DATA_SIZE - 1)) != 0) {
239 cpu_unaligned_access(ENV_GET_CPU(env), addr, READ_ACCESS_TYPE,
240 mmu_idx, retaddr);
242 #endif
244 haddr = addr + env->tlb_table[mmu_idx][index].addend;
245 #if DATA_SIZE == 1
246 res = glue(glue(ld, LSUFFIX), _p)((uint8_t *)haddr);
247 #else
248 res = glue(glue(ld, LSUFFIX), _le_p)((uint8_t *)haddr);
249 #endif
250 return res;
253 #if DATA_SIZE > 1
254 #ifdef SOFTMMU_CODE_ACCESS
255 static __attribute__((unused))
256 #endif
257 WORD_TYPE helper_be_ld_name(CPUArchState *env, target_ulong addr, int mmu_idx,
258 uintptr_t retaddr)
260 int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
261 target_ulong tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
262 uintptr_t haddr;
263 DATA_TYPE res;
265 /* Adjust the given return address. */
266 retaddr -= GETPC_ADJ;
268 /* If the TLB entry is for a different page, reload and try again. */
269 if ((addr & TARGET_PAGE_MASK)
270 != (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
271 #ifdef ALIGNED_ONLY
272 if ((addr & (DATA_SIZE - 1)) != 0) {
273 cpu_unaligned_access(ENV_GET_CPU(env), addr, READ_ACCESS_TYPE,
274 mmu_idx, retaddr);
276 #endif
277 if (!VICTIM_TLB_HIT(ADDR_READ)) {
278 tlb_fill(ENV_GET_CPU(env), addr, READ_ACCESS_TYPE,
279 mmu_idx, retaddr);
281 tlb_addr = env->tlb_table[mmu_idx][index].ADDR_READ;
284 /* Handle an IO access. */
285 if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
286 hwaddr ioaddr;
287 if ((addr & (DATA_SIZE - 1)) != 0) {
288 goto do_unaligned_access;
290 ioaddr = env->iotlb[mmu_idx][index];
292 /* ??? Note that the io helpers always read data in the target
293 byte ordering. We should push the LE/BE request down into io. */
294 res = glue(io_read, SUFFIX)(env, ioaddr, addr, retaddr);
295 res = TGT_BE(res);
296 return res;
299 /* Handle slow unaligned access (it spans two pages or IO). */
300 if (DATA_SIZE > 1
301 && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
302 >= TARGET_PAGE_SIZE)) {
303 target_ulong addr1, addr2;
304 DATA_TYPE res1, res2;
305 unsigned shift;
306 do_unaligned_access:
307 #ifdef ALIGNED_ONLY
308 cpu_unaligned_access(ENV_GET_CPU(env), addr, READ_ACCESS_TYPE,
309 mmu_idx, retaddr);
310 #endif
311 addr1 = addr & ~(DATA_SIZE - 1);
312 addr2 = addr1 + DATA_SIZE;
313 /* Note the adjustment at the beginning of the function.
314 Undo that for the recursion. */
315 res1 = helper_be_ld_name(env, addr1, mmu_idx, retaddr + GETPC_ADJ);
316 res2 = helper_be_ld_name(env, addr2, mmu_idx, retaddr + GETPC_ADJ);
317 shift = (addr & (DATA_SIZE - 1)) * 8;
319 /* Big-endian combine. */
320 res = (res1 << shift) | (res2 >> ((DATA_SIZE * 8) - shift));
321 return res;
324 /* Handle aligned access or unaligned access in the same page. */
325 #ifdef ALIGNED_ONLY
326 if ((addr & (DATA_SIZE - 1)) != 0) {
327 cpu_unaligned_access(ENV_GET_CPU(env), addr, READ_ACCESS_TYPE,
328 mmu_idx, retaddr);
330 #endif
332 haddr = addr + env->tlb_table[mmu_idx][index].addend;
333 res = glue(glue(ld, LSUFFIX), _be_p)((uint8_t *)haddr);
334 return res;
336 #endif /* DATA_SIZE > 1 */
338 DATA_TYPE
339 glue(glue(helper_ld, SUFFIX), MMUSUFFIX)(CPUArchState *env, target_ulong addr,
340 int mmu_idx)
342 return helper_te_ld_name (env, addr, mmu_idx, GETRA());
345 #ifndef SOFTMMU_CODE_ACCESS
347 /* Provide signed versions of the load routines as well. We can of course
348 avoid this for 64-bit data, or for 32-bit data on 32-bit host. */
349 #if DATA_SIZE * 8 < TCG_TARGET_REG_BITS
350 WORD_TYPE helper_le_lds_name(CPUArchState *env, target_ulong addr,
351 int mmu_idx, uintptr_t retaddr)
353 return (SDATA_TYPE)helper_le_ld_name(env, addr, mmu_idx, retaddr);
356 # if DATA_SIZE > 1
357 WORD_TYPE helper_be_lds_name(CPUArchState *env, target_ulong addr,
358 int mmu_idx, uintptr_t retaddr)
360 return (SDATA_TYPE)helper_be_ld_name(env, addr, mmu_idx, retaddr);
362 # endif
363 #endif
365 static inline void glue(io_write, SUFFIX)(CPUArchState *env,
366 hwaddr physaddr,
367 DATA_TYPE val,
368 target_ulong addr,
369 uintptr_t retaddr)
371 CPUState *cpu = ENV_GET_CPU(env);
372 MemoryRegion *mr = iotlb_to_region(cpu->as, physaddr);
374 physaddr = (physaddr & TARGET_PAGE_MASK) + addr;
375 if (mr != &io_mem_rom && mr != &io_mem_notdirty && !cpu_can_do_io(cpu)) {
376 cpu_io_recompile(cpu, retaddr);
379 cpu->mem_io_vaddr = addr;
380 cpu->mem_io_pc = retaddr;
381 io_mem_write(mr, physaddr, val, 1 << SHIFT);
384 void helper_le_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
385 int mmu_idx, uintptr_t retaddr)
387 int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
388 target_ulong tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
389 uintptr_t haddr;
391 /* Adjust the given return address. */
392 retaddr -= GETPC_ADJ;
394 /* If the TLB entry is for a different page, reload and try again. */
395 if ((addr & TARGET_PAGE_MASK)
396 != (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
397 #ifdef ALIGNED_ONLY
398 if ((addr & (DATA_SIZE - 1)) != 0) {
399 cpu_unaligned_access(ENV_GET_CPU(env), addr, 1, mmu_idx, retaddr);
401 #endif
402 if (!VICTIM_TLB_HIT(addr_write)) {
403 tlb_fill(ENV_GET_CPU(env), addr, 1, mmu_idx, retaddr);
405 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
408 /* Handle an IO access. */
409 if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
410 hwaddr ioaddr;
411 if ((addr & (DATA_SIZE - 1)) != 0) {
412 goto do_unaligned_access;
414 ioaddr = env->iotlb[mmu_idx][index];
416 /* ??? Note that the io helpers always read data in the target
417 byte ordering. We should push the LE/BE request down into io. */
418 val = TGT_LE(val);
419 glue(io_write, SUFFIX)(env, ioaddr, val, addr, retaddr);
420 return;
423 /* Handle slow unaligned access (it spans two pages or IO). */
424 if (DATA_SIZE > 1
425 && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
426 >= TARGET_PAGE_SIZE)) {
427 int i;
428 do_unaligned_access:
429 #ifdef ALIGNED_ONLY
430 cpu_unaligned_access(ENV_GET_CPU(env), addr, 1, mmu_idx, retaddr);
431 #endif
432 /* XXX: not efficient, but simple */
433 /* Note: relies on the fact that tlb_fill() does not remove the
434 * previous page from the TLB cache. */
435 for (i = DATA_SIZE - 1; i >= 0; i--) {
436 /* Little-endian extract. */
437 uint8_t val8 = val >> (i * 8);
438 /* Note the adjustment at the beginning of the function.
439 Undo that for the recursion. */
440 glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
441 mmu_idx, retaddr + GETPC_ADJ);
443 return;
446 /* Handle aligned access or unaligned access in the same page. */
447 #ifdef ALIGNED_ONLY
448 if ((addr & (DATA_SIZE - 1)) != 0) {
449 cpu_unaligned_access(ENV_GET_CPU(env), addr, 1, mmu_idx, retaddr);
451 #endif
453 haddr = addr + env->tlb_table[mmu_idx][index].addend;
454 #if DATA_SIZE == 1
455 glue(glue(st, SUFFIX), _p)((uint8_t *)haddr, val);
456 #else
457 glue(glue(st, SUFFIX), _le_p)((uint8_t *)haddr, val);
458 #endif
461 #if DATA_SIZE > 1
462 void helper_be_st_name(CPUArchState *env, target_ulong addr, DATA_TYPE val,
463 int mmu_idx, uintptr_t retaddr)
465 int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
466 target_ulong tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
467 uintptr_t haddr;
469 /* Adjust the given return address. */
470 retaddr -= GETPC_ADJ;
472 /* If the TLB entry is for a different page, reload and try again. */
473 if ((addr & TARGET_PAGE_MASK)
474 != (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
475 #ifdef ALIGNED_ONLY
476 if ((addr & (DATA_SIZE - 1)) != 0) {
477 cpu_unaligned_access(ENV_GET_CPU(env), addr, 1, mmu_idx, retaddr);
479 #endif
480 if (!VICTIM_TLB_HIT(addr_write)) {
481 tlb_fill(ENV_GET_CPU(env), addr, 1, mmu_idx, retaddr);
483 tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
486 /* Handle an IO access. */
487 if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
488 hwaddr ioaddr;
489 if ((addr & (DATA_SIZE - 1)) != 0) {
490 goto do_unaligned_access;
492 ioaddr = env->iotlb[mmu_idx][index];
494 /* ??? Note that the io helpers always read data in the target
495 byte ordering. We should push the LE/BE request down into io. */
496 val = TGT_BE(val);
497 glue(io_write, SUFFIX)(env, ioaddr, val, addr, retaddr);
498 return;
501 /* Handle slow unaligned access (it spans two pages or IO). */
502 if (DATA_SIZE > 1
503 && unlikely((addr & ~TARGET_PAGE_MASK) + DATA_SIZE - 1
504 >= TARGET_PAGE_SIZE)) {
505 int i;
506 do_unaligned_access:
507 #ifdef ALIGNED_ONLY
508 cpu_unaligned_access(ENV_GET_CPU(env), addr, 1, mmu_idx, retaddr);
509 #endif
510 /* XXX: not efficient, but simple */
511 /* Note: relies on the fact that tlb_fill() does not remove the
512 * previous page from the TLB cache. */
513 for (i = DATA_SIZE - 1; i >= 0; i--) {
514 /* Big-endian extract. */
515 uint8_t val8 = val >> (((DATA_SIZE - 1) * 8) - (i * 8));
516 /* Note the adjustment at the beginning of the function.
517 Undo that for the recursion. */
518 glue(helper_ret_stb, MMUSUFFIX)(env, addr + i, val8,
519 mmu_idx, retaddr + GETPC_ADJ);
521 return;
524 /* Handle aligned access or unaligned access in the same page. */
525 #ifdef ALIGNED_ONLY
526 if ((addr & (DATA_SIZE - 1)) != 0) {
527 cpu_unaligned_access(ENV_GET_CPU(env), addr, 1, mmu_idx, retaddr);
529 #endif
531 haddr = addr + env->tlb_table[mmu_idx][index].addend;
532 glue(glue(st, SUFFIX), _be_p)((uint8_t *)haddr, val);
534 #endif /* DATA_SIZE > 1 */
536 void
537 glue(glue(helper_st, SUFFIX), MMUSUFFIX)(CPUArchState *env, target_ulong addr,
538 DATA_TYPE val, int mmu_idx)
540 helper_te_st_name(env, addr, val, mmu_idx, GETRA());
543 #endif /* !defined(SOFTMMU_CODE_ACCESS) */
545 #undef READ_ACCESS_TYPE
546 #undef SHIFT
547 #undef DATA_TYPE
548 #undef SUFFIX
549 #undef LSUFFIX
550 #undef DATA_SIZE
551 #undef ADDR_READ
552 #undef WORD_TYPE
553 #undef SDATA_TYPE
554 #undef USUFFIX
555 #undef SSUFFIX
556 #undef BSWAP
557 #undef TGT_BE
558 #undef TGT_LE
559 #undef CPU_BE
560 #undef CPU_LE
561 #undef helper_le_ld_name
562 #undef helper_be_ld_name
563 #undef helper_le_lds_name
564 #undef helper_be_lds_name
565 #undef helper_le_st_name
566 #undef helper_be_st_name
567 #undef helper_te_ld_name
568 #undef helper_te_st_name