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)
94 #error unsupported CPU
96 #define MMUSUFFIX _cmmu
99 #error invalid ACCESS_TYPE
103 #define RES_TYPE uint64_t
109 #define ADDR_READ addr_code
111 #define ADDR_READ addr_read
114 DATA_TYPE REGPARM(1) glue(glue(__ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
116 void REGPARM(2) glue(glue(__st, SUFFIX), MMUSUFFIX)(target_ulong addr, DATA_TYPE v, int is_user);
118 #if (DATA_SIZE <= 4) && (TARGET_LONG_BITS == 32) && defined(__i386__) && \
119 (ACCESS_TYPE <= 1) && defined(ASM_SOFTMMU)
121 #define CPU_TLB_ENTRY_BITS 4
123 static inline RES_TYPE glue(glue(ld, USUFFIX), MEMSUFFIX)(target_ulong ptr)
127 asm volatile ("movl %1, %%edx\n"
132 "leal %5(%%edx, %%ebp), %%edx\n"
133 "cmpl (%%edx), %%eax\n"
142 "addl 12(%%edx), %%eax\n"
144 "movzbl (%%eax), %0\n"
146 "movzwl (%%eax), %0\n"
150 #error unsupported size
155 "i" ((CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS),
156 "i" (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS),
157 "i" (TARGET_PAGE_MASK | (DATA_SIZE - 1)),
158 "m" (*(uint32_t *)offsetof(CPUState, tlb_table[CPU_MEM_INDEX][0].addr_read)),
160 "m" (*(uint8_t *)&glue(glue(__ld, SUFFIX), MMUSUFFIX))
161 : "%eax", "%ecx", "%edx", "memory", "cc");
166 static inline int glue(glue(lds, SUFFIX), MEMSUFFIX)(target_ulong ptr)
170 asm volatile ("movl %1, %%edx\n"
175 "leal %5(%%edx, %%ebp), %%edx\n"
176 "cmpl (%%edx), %%eax\n"
187 #error unsupported size
191 "addl 12(%%edx), %%eax\n"
193 "movsbl (%%eax), %0\n"
195 "movswl (%%eax), %0\n"
197 #error unsupported size
202 "i" ((CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS),
203 "i" (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS),
204 "i" (TARGET_PAGE_MASK | (DATA_SIZE - 1)),
205 "m" (*(uint32_t *)offsetof(CPUState, tlb_table[CPU_MEM_INDEX][0].addr_read)),
207 "m" (*(uint8_t *)&glue(glue(__ld, SUFFIX), MMUSUFFIX))
208 : "%eax", "%ecx", "%edx", "memory", "cc");
213 static inline void glue(glue(st, SUFFIX), MEMSUFFIX)(target_ulong ptr, RES_TYPE v)
215 asm volatile ("movl %0, %%edx\n"
220 "leal %5(%%edx, %%ebp), %%edx\n"
221 "cmpl (%%edx), %%eax\n"
225 "movzbl %b1, %%edx\n"
227 "movzwl %w1, %%edx\n"
231 #error unsupported size
238 "addl 8(%%edx), %%eax\n"
240 "movb %b1, (%%eax)\n"
242 "movw %w1, (%%eax)\n"
246 #error unsupported size
251 /* NOTE: 'q' would be needed as constraint, but we could not use it
254 "i" ((CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS),
255 "i" (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS),
256 "i" (TARGET_PAGE_MASK | (DATA_SIZE - 1)),
257 "m" (*(uint32_t *)offsetof(CPUState, tlb_table[CPU_MEM_INDEX][0].addr_write)),
259 "m" (*(uint8_t *)&glue(glue(__st, SUFFIX), MMUSUFFIX))
260 : "%eax", "%ecx", "%edx", "memory", "cc");
265 /* generic load/store macros */
267 static inline RES_TYPE glue(glue(ld, USUFFIX), MEMSUFFIX)(target_ulong ptr)
272 unsigned long physaddr;
276 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
277 is_user = CPU_MEM_INDEX;
278 if (__builtin_expect(env->tlb_table[is_user][index].ADDR_READ !=
279 (addr & (TARGET_PAGE_MASK | (DATA_SIZE - 1))), 0)) {
280 res = glue(glue(__ld, SUFFIX), MMUSUFFIX)(addr, is_user);
282 physaddr = addr + env->tlb_table[is_user][index].addend;
283 res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)physaddr);
289 static inline int glue(glue(lds, SUFFIX), MEMSUFFIX)(target_ulong ptr)
293 unsigned long physaddr;
297 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
298 is_user = CPU_MEM_INDEX;
299 if (__builtin_expect(env->tlb_table[is_user][index].ADDR_READ !=
300 (addr & (TARGET_PAGE_MASK | (DATA_SIZE - 1))), 0)) {
301 res = (DATA_STYPE)glue(glue(__ld, SUFFIX), MMUSUFFIX)(addr, is_user);
303 physaddr = addr + env->tlb_table[is_user][index].addend;
304 res = glue(glue(lds, SUFFIX), _raw)((uint8_t *)physaddr);
312 /* generic store macro */
314 static inline void glue(glue(st, SUFFIX), MEMSUFFIX)(target_ulong ptr, RES_TYPE v)
318 unsigned long physaddr;
322 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
323 is_user = CPU_MEM_INDEX;
324 if (__builtin_expect(env->tlb_table[is_user][index].addr_write !=
325 (addr & (TARGET_PAGE_MASK | (DATA_SIZE - 1))), 0)) {
326 glue(glue(__st, SUFFIX), MMUSUFFIX)(addr, v, is_user);
328 physaddr = addr + env->tlb_table[is_user][index].addend;
329 glue(glue(st, SUFFIX), _raw)((uint8_t *)physaddr, v);
333 #endif /* ACCESS_TYPE != 3 */
340 static inline float64 glue(ldfq, MEMSUFFIX)(target_ulong ptr)
346 u.i = glue(ldq, MEMSUFFIX)(ptr);
350 static inline void glue(stfq, MEMSUFFIX)(target_ulong ptr, float64 v)
357 glue(stq, MEMSUFFIX)(ptr, u.i);
359 #endif /* DATA_SIZE == 8 */
362 static inline float32 glue(ldfl, MEMSUFFIX)(target_ulong ptr)
368 u.i = glue(ldl, MEMSUFFIX)(ptr);
372 static inline void glue(stfl, MEMSUFFIX)(target_ulong ptr, float32 v)
379 glue(stl, MEMSUFFIX)(ptr, u.i);
381 #endif /* DATA_SIZE == 4 */
383 #endif /* ACCESS_TYPE != 3 */