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)
67 #error unsupported CPU
69 #define MMUSUFFIX _mmu
71 #elif ACCESS_TYPE == 3
74 #define CPU_MEM_INDEX ((env->hflags & HF_CPL_MASK) == 3)
75 #elif defined (TARGET_PPC)
76 #define CPU_MEM_INDEX (msr_pr)
77 #elif defined (TARGET_MIPS)
78 #define CPU_MEM_INDEX ((env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_UM)
79 #elif defined (TARGET_SPARC)
80 #define CPU_MEM_INDEX ((env->psrs) == 0)
81 #elif defined (TARGET_ARM)
82 #define CPU_MEM_INDEX ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR)
83 #elif defined (TARGET_SH4)
84 #define CPU_MEM_INDEX ((env->sr & SR_MD) == 0)
86 #error unsupported CPU
88 #define MMUSUFFIX _cmmu
91 #error invalid ACCESS_TYPE
95 #define RES_TYPE uint64_t
101 #define ADDR_READ addr_code
103 #define ADDR_READ addr_read
106 DATA_TYPE
REGPARM(1) glue(glue(__ld
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
,
108 void REGPARM(2) glue(glue(__st
, SUFFIX
), MMUSUFFIX
)(target_ulong addr
, DATA_TYPE v
, int is_user
);
110 #if (DATA_SIZE <= 4) && (TARGET_LONG_BITS == 32) && defined(__i386__) && \
111 (ACCESS_TYPE <= 1) && defined(ASM_SOFTMMU)
113 #define CPU_TLB_ENTRY_BITS 4
115 static inline RES_TYPE
glue(glue(ld
, USUFFIX
), MEMSUFFIX
)(target_ulong ptr
)
119 asm volatile ("movl %1, %%edx\n"
124 "leal %5(%%edx, %%ebp), %%edx\n"
125 "cmpl (%%edx), %%eax\n"
134 "addl 12(%%edx), %%eax\n"
136 "movzbl (%%eax), %0\n"
138 "movzwl (%%eax), %0\n"
142 #error unsupported size
147 "i" ((CPU_TLB_SIZE
- 1) << CPU_TLB_ENTRY_BITS
),
148 "i" (TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
),
149 "i" (TARGET_PAGE_MASK
| (DATA_SIZE
- 1)),
150 "m" (*(uint32_t *)offsetof(CPUState
, tlb_table
[CPU_MEM_INDEX
][0].addr_read
)),
152 "m" (*(uint8_t *)&glue(glue(__ld
, SUFFIX
), MMUSUFFIX
))
153 : "%eax", "%ecx", "%edx", "memory", "cc");
158 static inline int glue(glue(lds
, SUFFIX
), MEMSUFFIX
)(target_ulong ptr
)
162 asm volatile ("movl %1, %%edx\n"
167 "leal %5(%%edx, %%ebp), %%edx\n"
168 "cmpl (%%edx), %%eax\n"
179 #error unsupported size
183 "addl 12(%%edx), %%eax\n"
185 "movsbl (%%eax), %0\n"
187 "movswl (%%eax), %0\n"
189 #error unsupported size
194 "i" ((CPU_TLB_SIZE
- 1) << CPU_TLB_ENTRY_BITS
),
195 "i" (TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
),
196 "i" (TARGET_PAGE_MASK
| (DATA_SIZE
- 1)),
197 "m" (*(uint32_t *)offsetof(CPUState
, tlb_table
[CPU_MEM_INDEX
][0].addr_read
)),
199 "m" (*(uint8_t *)&glue(glue(__ld
, SUFFIX
), MMUSUFFIX
))
200 : "%eax", "%ecx", "%edx", "memory", "cc");
205 static inline void glue(glue(st
, SUFFIX
), MEMSUFFIX
)(target_ulong ptr
, RES_TYPE v
)
207 asm volatile ("movl %0, %%edx\n"
212 "leal %5(%%edx, %%ebp), %%edx\n"
213 "cmpl (%%edx), %%eax\n"
217 "movzbl %b1, %%edx\n"
219 "movzwl %w1, %%edx\n"
223 #error unsupported size
230 "addl 8(%%edx), %%eax\n"
232 "movb %b1, (%%eax)\n"
234 "movw %w1, (%%eax)\n"
238 #error unsupported size
243 /* NOTE: 'q' would be needed as constraint, but we could not use it
246 "i" ((CPU_TLB_SIZE
- 1) << CPU_TLB_ENTRY_BITS
),
247 "i" (TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
),
248 "i" (TARGET_PAGE_MASK
| (DATA_SIZE
- 1)),
249 "m" (*(uint32_t *)offsetof(CPUState
, tlb_table
[CPU_MEM_INDEX
][0].addr_write
)),
251 "m" (*(uint8_t *)&glue(glue(__st
, SUFFIX
), MMUSUFFIX
))
252 : "%eax", "%ecx", "%edx", "memory", "cc");
257 /* generic load/store macros */
259 static inline RES_TYPE
glue(glue(ld
, USUFFIX
), MEMSUFFIX
)(target_ulong ptr
)
264 unsigned long physaddr
;
268 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
269 is_user
= CPU_MEM_INDEX
;
270 if (__builtin_expect(env
->tlb_table
[is_user
][index
].ADDR_READ
!=
271 (addr
& (TARGET_PAGE_MASK
| (DATA_SIZE
- 1))), 0)) {
272 res
= glue(glue(__ld
, SUFFIX
), MMUSUFFIX
)(addr
, is_user
);
274 physaddr
= addr
+ env
->tlb_table
[is_user
][index
].addend
;
275 res
= glue(glue(ld
, USUFFIX
), _raw
)((uint8_t *)physaddr
);
281 static inline int glue(glue(lds
, SUFFIX
), MEMSUFFIX
)(target_ulong ptr
)
285 unsigned long physaddr
;
289 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
290 is_user
= CPU_MEM_INDEX
;
291 if (__builtin_expect(env
->tlb_table
[is_user
][index
].ADDR_READ
!=
292 (addr
& (TARGET_PAGE_MASK
| (DATA_SIZE
- 1))), 0)) {
293 res
= (DATA_STYPE
)glue(glue(__ld
, SUFFIX
), MMUSUFFIX
)(addr
, is_user
);
295 physaddr
= addr
+ env
->tlb_table
[is_user
][index
].addend
;
296 res
= glue(glue(lds
, SUFFIX
), _raw
)((uint8_t *)physaddr
);
304 /* generic store macro */
306 static inline void glue(glue(st
, SUFFIX
), MEMSUFFIX
)(target_ulong ptr
, RES_TYPE v
)
310 unsigned long physaddr
;
314 index
= (addr
>> TARGET_PAGE_BITS
) & (CPU_TLB_SIZE
- 1);
315 is_user
= CPU_MEM_INDEX
;
316 if (__builtin_expect(env
->tlb_table
[is_user
][index
].addr_write
!=
317 (addr
& (TARGET_PAGE_MASK
| (DATA_SIZE
- 1))), 0)) {
318 glue(glue(__st
, SUFFIX
), MMUSUFFIX
)(addr
, v
, is_user
);
320 physaddr
= addr
+ env
->tlb_table
[is_user
][index
].addend
;
321 glue(glue(st
, SUFFIX
), _raw
)((uint8_t *)physaddr
, v
);
325 #endif /* ACCESS_TYPE != 3 */
332 static inline float64
glue(ldfq
, MEMSUFFIX
)(target_ulong ptr
)
338 u
.i
= glue(ldq
, MEMSUFFIX
)(ptr
);
342 static inline void glue(stfq
, MEMSUFFIX
)(target_ulong ptr
, float64 v
)
349 glue(stq
, MEMSUFFIX
)(ptr
, u
.i
);
351 #endif /* DATA_SIZE == 8 */
354 static inline float32
glue(ldfl
, MEMSUFFIX
)(target_ulong ptr
)
360 u
.i
= glue(ldl
, MEMSUFFIX
)(ptr
);
364 static inline void glue(stfl
, MEMSUFFIX
)(target_ulong ptr
, float32 v
)
371 glue(stl
, MEMSUFFIX
)(ptr
, u
.i
);
373 #endif /* DATA_SIZE == 4 */
375 #endif /* ACCESS_TYPE != 3 */