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
23 #define DATA_TYPE uint64_t
27 #define DATA_TYPE uint32_t
31 #define DATA_TYPE uint16_t
32 #define DATA_STYPE int16_t
36 #define DATA_TYPE uint8_t
37 #define DATA_STYPE int8_t
39 #error unsupported data size
44 #define CPU_MEM_INDEX 0
45 #define MMUSUFFIX _mmu
47 #elif ACCESS_TYPE == 1
49 #define CPU_MEM_INDEX 1
50 #define MMUSUFFIX _mmu
52 #elif ACCESS_TYPE == 2
55 #define CPU_MEM_INDEX ((env->hflags & HF_CPL_MASK) == 3)
56 #elif defined (TARGET_PPC)
57 #define CPU_MEM_INDEX (msr_pr)
58 #elif defined (TARGET_MIPS)
59 #define CPU_MEM_INDEX ((env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_UM)
60 #elif defined (TARGET_SPARC)
61 #define CPU_MEM_INDEX ((env->psrs) == 0)
62 #elif defined (TARGET_ARM)
63 #define CPU_MEM_INDEX ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR)
64 #elif defined (TARGET_SH4)
65 #define CPU_MEM_INDEX ((env->sr & SR_MD) == 0)
66 #elif defined (TARGET_ALPHA)
67 #define CPU_MEM_INDEX ((env->ps >> 3) & 3)
68 #elif defined (TARGET_M68K)
69 #define CPU_MEM_INDEX ((env->sr & SR_S) == 0)
71 #error unsupported CPU
73 #define MMUSUFFIX _mmu
75 #elif ACCESS_TYPE == 3
78 #define CPU_MEM_INDEX ((env->hflags & HF_CPL_MASK) == 3)
79 #elif defined (TARGET_PPC)
80 #define CPU_MEM_INDEX (msr_pr)
81 #elif defined (TARGET_MIPS)
82 #define CPU_MEM_INDEX ((env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_UM)
83 #elif defined (TARGET_SPARC)
84 #define CPU_MEM_INDEX ((env->psrs) == 0)
85 #elif defined (TARGET_ARM)
86 #define CPU_MEM_INDEX ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR)
87 #elif defined (TARGET_SH4)
88 #define CPU_MEM_INDEX ((env->sr & SR_MD) == 0)
89 #elif defined (TARGET_ALPHA)
90 #define CPU_MEM_INDEX ((env->ps >> 3) & 3)
91 #elif defined (TARGET_M68K)
92 #define CPU_MEM_INDEX ((env->sr & SR_S) == 0)
93 #elif defined (TARGET_IA64)
94 #define CPU_MEM_INDEX (0)
96 #error unsupported CPU
98 #define MMUSUFFIX _cmmu
101 #error invalid ACCESS_TYPE
105 #define RES_TYPE uint64_t
111 #define ADDR_READ addr_code
113 #define ADDR_READ addr_read
116 DATA_TYPE
REGPARM(1) glue(glue(__ld
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
118 void REGPARM(2) glue(glue(__st
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
, DATA_TYPE v
, int is_user
);
120 #if (DATA_SIZE <= 4) && (TARGET_LONG_BITS == 32) && defined(__i386__) && \
121 (ACCESS_TYPE <= 1) && defined(ASM_SOFTMMU)
123 #define CPU_TLB_ENTRY_BITS 4
125 static inline RES_TYPE
glue(glue(ld
, USUFFIX
), MEMSUFFIX
)(target_ulong ptr
)
129 asm volatile ("movl %1, %%edx\n"
134 "leal %5(%%edx, %%ebp), %%edx\n"
135 "cmpl (%%edx), %%eax\n"
144 "addl 12(%%edx), %%eax\n"
146 "movzbl (%%eax), %0\n"
148 "movzwl (%%eax), %0\n"
152 #error unsupported size
157 "i" ((CPU_TLB_SIZE
- 1) << CPU_TLB_ENTRY_BITS
),
158 "i" (TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
),
159 "i" (TARGET_PAGE_MASK
| (DATA_SIZE
- 1)),
160 "m" (*(uint32_t *)offsetof(CPUState
, tlb_table
[CPU_MEM_INDEX
][0].addr_read
)),
162 "m" (*(uint8_t *)&glue(glue(__ld
, SUFFIX
), MMUSUFFIX
))
163 : "%eax", "%ecx", "%edx", "memory", "cc");
168 static inline int glue(glue(lds
, SUFFIX
), MEMSUFFIX
)(target_ulong ptr
)
172 asm volatile ("movl %1, %%edx\n"
177 "leal %5(%%edx, %%ebp), %%edx\n"
178 "cmpl (%%edx), %%eax\n"
189 #error unsupported size
193 "addl 12(%%edx), %%eax\n"
195 "movsbl (%%eax), %0\n"
197 "movswl (%%eax), %0\n"
199 #error unsupported size
204 "i" ((CPU_TLB_SIZE
- 1) << CPU_TLB_ENTRY_BITS
),
205 "i" (TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
),
206 "i" (TARGET_PAGE_MASK
| (DATA_SIZE
- 1)),
207 "m" (*(uint32_t *)offsetof(CPUState
, tlb_table
[CPU_MEM_INDEX
][0].addr_read
)),
209 "m" (*(uint8_t *)&glue(glue(__ld
, SUFFIX
), MMUSUFFIX
))
210 : "%eax", "%ecx", "%edx", "memory", "cc");
215 static inline void glue(glue(st
, SUFFIX
), MEMSUFFIX
)(target_ulong ptr
, RES_TYPE v
)
217 asm volatile ("movl %0, %%edx\n"
222 "leal %5(%%edx, %%ebp), %%edx\n"
223 "cmpl (%%edx), %%eax\n"
227 "movzbl %b1, %%edx\n"
229 "movzwl %w1, %%edx\n"
233 #error unsupported size
240 "addl 8(%%edx), %%eax\n"
242 "movb %b1, (%%eax)\n"
244 "movw %w1, (%%eax)\n"
248 #error unsupported size
253 /* NOTE: 'q' would be needed as constraint, but we could not use it
256 "i" ((CPU_TLB_SIZE
- 1) << CPU_TLB_ENTRY_BITS
),
257 "i" (TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
),
258 "i" (TARGET_PAGE_MASK
| (DATA_SIZE
- 1)),
259 "m" (*(uint32_t *)offsetof(CPUState
, tlb_table
[CPU_MEM_INDEX
][0].addr_write
)),
261 "m" (*(uint8_t *)&glue(glue(__st
, SUFFIX
), MMUSUFFIX
))
262 : "%eax", "%ecx", "%edx", "memory", "cc");
267 /* generic load/store macros */
269 static inline RES_TYPE
glue(glue(ld
, USUFFIX
), MEMSUFFIX
)(target_ulong ptr
)
274 unsigned long physaddr
;
278 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
279 is_user
= CPU_MEM_INDEX
;
280 if (__builtin_expect(env
->tlb_table
[is_user
][index
].ADDR_READ
!=
281 (addr
& (TARGET_PAGE_MASK
| (DATA_SIZE
- 1))), 0)) {
282 res
= glue(glue(__ld
, SUFFIX
), MMUSUFFIX
)(addr
, is_user
);
284 physaddr
= addr
+ env
->tlb_table
[is_user
][index
].addend
;
285 res
= glue(glue(ld
, USUFFIX
), _raw
)((uint8_t *)physaddr
);
291 static inline int glue(glue(lds
, SUFFIX
), MEMSUFFIX
)(target_ulong ptr
)
295 unsigned long physaddr
;
299 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
300 is_user
= CPU_MEM_INDEX
;
301 if (__builtin_expect(env
->tlb_table
[is_user
][index
].ADDR_READ
!=
302 (addr
& (TARGET_PAGE_MASK
| (DATA_SIZE
- 1))), 0)) {
303 res
= (DATA_STYPE
)glue(glue(__ld
, SUFFIX
), MMUSUFFIX
)(addr
, is_user
);
305 physaddr
= addr
+ env
->tlb_table
[is_user
][index
].addend
;
306 res
= glue(glue(lds
, SUFFIX
), _raw
)((uint8_t *)physaddr
);
314 /* generic store macro */
316 static inline void glue(glue(st
, SUFFIX
), MEMSUFFIX
)(target_ulong ptr
, RES_TYPE v
)
320 unsigned long physaddr
;
324 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
325 is_user
= CPU_MEM_INDEX
;
326 if (__builtin_expect(env
->tlb_table
[is_user
][index
].addr_write
!=
327 (addr
& (TARGET_PAGE_MASK
| (DATA_SIZE
- 1))), 0)) {
328 glue(glue(__st
, SUFFIX
), MMUSUFFIX
)(addr
, v
, is_user
);
330 physaddr
= addr
+ env
->tlb_table
[is_user
][index
].addend
;
331 glue(glue(st
, SUFFIX
), _raw
)((uint8_t *)physaddr
, v
);
335 #endif /* ACCESS_TYPE != 3 */
342 static inline float64
glue(ldfq
, MEMSUFFIX
)(target_ulong ptr
)
348 u
.i
= glue(ldq
, MEMSUFFIX
)(ptr
);
352 static inline void glue(stfq
, MEMSUFFIX
)(target_ulong ptr
, float64 v
)
359 glue(stq
, MEMSUFFIX
)(ptr
, u
.i
);
361 #endif /* DATA_SIZE == 8 */
364 static inline float32
glue(ldfl
, MEMSUFFIX
)(target_ulong ptr
)
370 u
.i
= glue(ldl
, MEMSUFFIX
)(ptr
);
374 static inline void glue(stfl
, MEMSUFFIX
)(target_ulong ptr
, float32 v
)
381 glue(stl
, MEMSUFFIX
)(ptr
, u
.i
);
383 #endif /* DATA_SIZE == 4 */
385 #endif /* ACCESS_TYPE != 3 */