blob: 0bbc3681cc575d8974c832e09b3ca5f406b96827 [file] [log] [blame]
bellardb92e5a22003-08-08 23:58:05 +00001/*
2 * Software MMU support
3 *
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#if DATA_SIZE == 8
21#define SUFFIX q
bellard61382a52003-10-27 21:22:23 +000022#define USUFFIX q
bellardb92e5a22003-08-08 23:58:05 +000023#define DATA_TYPE uint64_t
24#elif DATA_SIZE == 4
25#define SUFFIX l
bellard61382a52003-10-27 21:22:23 +000026#define USUFFIX l
bellardb92e5a22003-08-08 23:58:05 +000027#define DATA_TYPE uint32_t
28#elif DATA_SIZE == 2
29#define SUFFIX w
bellard61382a52003-10-27 21:22:23 +000030#define USUFFIX uw
bellardb92e5a22003-08-08 23:58:05 +000031#define DATA_TYPE uint16_t
32#define DATA_STYPE int16_t
33#elif DATA_SIZE == 1
34#define SUFFIX b
bellard61382a52003-10-27 21:22:23 +000035#define USUFFIX ub
bellardb92e5a22003-08-08 23:58:05 +000036#define DATA_TYPE uint8_t
37#define DATA_STYPE int8_t
38#else
39#error unsupported data size
40#endif
41
bellard61382a52003-10-27 21:22:23 +000042#if ACCESS_TYPE == 0
43
44#define CPU_MEM_INDEX 0
45#define MMUSUFFIX _mmu
46
47#elif ACCESS_TYPE == 1
48
49#define CPU_MEM_INDEX 1
50#define MMUSUFFIX _mmu
51
52#elif ACCESS_TYPE == 2
53
bellard2d603d22004-01-04 23:56:24 +000054#ifdef TARGET_I386
bellard61382a52003-10-27 21:22:23 +000055#define CPU_MEM_INDEX ((env->hflags & HF_CPL_MASK) == 3)
bellard2d603d22004-01-04 23:56:24 +000056#elif defined (TARGET_PPC)
57#define CPU_MEM_INDEX (msr_pr)
58#endif
bellard61382a52003-10-27 21:22:23 +000059#define MMUSUFFIX _mmu
60
61#elif ACCESS_TYPE == 3
62
bellard2d603d22004-01-04 23:56:24 +000063#ifdef TARGET_I386
bellard61382a52003-10-27 21:22:23 +000064#define CPU_MEM_INDEX ((env->hflags & HF_CPL_MASK) == 3)
bellard2d603d22004-01-04 23:56:24 +000065#elif defined (TARGET_PPC)
66#define CPU_MEM_INDEX (msr_pr)
67#endif
bellard61382a52003-10-27 21:22:23 +000068#define MMUSUFFIX _cmmu
69
bellardb92e5a22003-08-08 23:58:05 +000070#else
bellard61382a52003-10-27 21:22:23 +000071#error invalid ACCESS_TYPE
bellardb92e5a22003-08-08 23:58:05 +000072#endif
73
74#if DATA_SIZE == 8
75#define RES_TYPE uint64_t
76#else
77#define RES_TYPE int
78#endif
79
80
bellard61382a52003-10-27 21:22:23 +000081DATA_TYPE REGPARM(1) glue(glue(__ld, SUFFIX), MMUSUFFIX)(unsigned long addr,
82 int is_user);
83void REGPARM(2) glue(glue(__st, SUFFIX), MMUSUFFIX)(unsigned long addr, DATA_TYPE v, int is_user);
bellardb92e5a22003-08-08 23:58:05 +000084
bellarde16c53f2004-01-04 18:15:29 +000085#if (DATA_SIZE <= 4) && defined(__i386__) && (ACCESS_TYPE <= 1) && defined(ASM_SOFTMMU)
86
87static inline RES_TYPE glue(glue(ld, USUFFIX), MEMSUFFIX)(void *ptr)
88{
89 int res;
90
91 asm volatile ("movl %1, %%edx\n"
92 "movl %1, %%eax\n"
93 "shrl %3, %%edx\n"
94 "andl %4, %%eax\n"
95 "andl %2, %%edx\n"
96 "leal %5(%%edx, %%ebp), %%edx\n"
97 "cmpl (%%edx), %%eax\n"
98 "movl %1, %%eax\n"
99 "je 1f\n"
100 "pushl %6\n"
101 "call %7\n"
102 "popl %%edx\n"
103 "movl %%eax, %0\n"
104 "jmp 2f\n"
105 "1:\n"
106 "addl 4(%%edx), %%eax\n"
107#if DATA_SIZE == 1
108 "movzbl (%%eax), %0\n"
109#elif DATA_SIZE == 2
110 "movzwl (%%eax), %0\n"
111#elif DATA_SIZE == 4
112 "movl (%%eax), %0\n"
113#else
114#error unsupported size
115#endif
116 "2:\n"
117 : "=r" (res)
118 : "r" (ptr),
119 "i" ((CPU_TLB_SIZE - 1) << 3),
120 "i" (TARGET_PAGE_BITS - 3),
121 "i" (TARGET_PAGE_MASK | (DATA_SIZE - 1)),
122 "m" (*(uint32_t *)offsetof(CPUState, tlb_read[CPU_MEM_INDEX][0].address)),
123 "i" (CPU_MEM_INDEX),
124 "m" (*(uint8_t *)&glue(glue(__ld, SUFFIX), MMUSUFFIX))
125 : "%eax", "%ecx", "%edx", "memory", "cc");
126 return res;
127}
128
129#if DATA_SIZE <= 2
130static inline int glue(glue(lds, SUFFIX), MEMSUFFIX)(void *ptr)
131{
132 int res;
133
134 asm volatile ("movl %1, %%edx\n"
135 "movl %1, %%eax\n"
136 "shrl %3, %%edx\n"
137 "andl %4, %%eax\n"
138 "andl %2, %%edx\n"
139 "leal %5(%%edx, %%ebp), %%edx\n"
140 "cmpl (%%edx), %%eax\n"
141 "movl %1, %%eax\n"
142 "je 1f\n"
143 "pushl %6\n"
144 "call %7\n"
145 "popl %%edx\n"
146#if DATA_SIZE == 1
147 "movsbl %%al, %0\n"
148#elif DATA_SIZE == 2
149 "movswl %%ax, %0\n"
150#else
151#error unsupported size
152#endif
153 "jmp 2f\n"
154 "1:\n"
155 "addl 4(%%edx), %%eax\n"
156#if DATA_SIZE == 1
157 "movsbl (%%eax), %0\n"
158#elif DATA_SIZE == 2
159 "movswl (%%eax), %0\n"
160#else
161#error unsupported size
162#endif
163 "2:\n"
164 : "=r" (res)
165 : "r" (ptr),
166 "i" ((CPU_TLB_SIZE - 1) << 3),
167 "i" (TARGET_PAGE_BITS - 3),
168 "i" (TARGET_PAGE_MASK | (DATA_SIZE - 1)),
169 "m" (*(uint32_t *)offsetof(CPUState, tlb_read[CPU_MEM_INDEX][0].address)),
170 "i" (CPU_MEM_INDEX),
171 "m" (*(uint8_t *)&glue(glue(__ld, SUFFIX), MMUSUFFIX))
172 : "%eax", "%ecx", "%edx", "memory", "cc");
173 return res;
174}
175#endif
176
177static inline void glue(glue(st, SUFFIX), MEMSUFFIX)(void *ptr, RES_TYPE v)
178{
179 asm volatile ("movl %0, %%edx\n"
180 "movl %0, %%eax\n"
181 "shrl %3, %%edx\n"
182 "andl %4, %%eax\n"
183 "andl %2, %%edx\n"
184 "leal %5(%%edx, %%ebp), %%edx\n"
185 "cmpl (%%edx), %%eax\n"
186 "movl %0, %%eax\n"
187 "je 1f\n"
188#if DATA_SIZE == 1
189 "movzbl %b1, %%edx\n"
190#elif DATA_SIZE == 2
191 "movzwl %w1, %%edx\n"
192#elif DATA_SIZE == 4
193 "movl %1, %%edx\n"
194#else
195#error unsupported size
196#endif
197 "pushl %6\n"
198 "call %7\n"
199 "popl %%eax\n"
200 "jmp 2f\n"
201 "1:\n"
202 "addl 4(%%edx), %%eax\n"
203#if DATA_SIZE == 1
204 "movb %b1, (%%eax)\n"
205#elif DATA_SIZE == 2
206 "movw %w1, (%%eax)\n"
207#elif DATA_SIZE == 4
208 "movl %1, (%%eax)\n"
209#else
210#error unsupported size
211#endif
212 "2:\n"
213 :
214 : "r" (ptr),
215/* NOTE: 'q' would be needed as constraint, but we could not use it
216 with T1 ! */
217 "r" (v),
218 "i" ((CPU_TLB_SIZE - 1) << 3),
219 "i" (TARGET_PAGE_BITS - 3),
220 "i" (TARGET_PAGE_MASK | (DATA_SIZE - 1)),
221 "m" (*(uint32_t *)offsetof(CPUState, tlb_write[CPU_MEM_INDEX][0].address)),
222 "i" (CPU_MEM_INDEX),
223 "m" (*(uint8_t *)&glue(glue(__st, SUFFIX), MMUSUFFIX))
224 : "%eax", "%ecx", "%edx", "memory", "cc");
225}
226
227#else
228
229/* generic load/store macros */
230
bellard8948b5d2003-10-28 23:04:30 +0000231static inline RES_TYPE glue(glue(ld, USUFFIX), MEMSUFFIX)(void *ptr)
bellardb92e5a22003-08-08 23:58:05 +0000232{
233 int index;
234 RES_TYPE res;
235 unsigned long addr, physaddr;
bellard61382a52003-10-27 21:22:23 +0000236 int is_user;
237
bellardb92e5a22003-08-08 23:58:05 +0000238 addr = (unsigned long)ptr;
239 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard61382a52003-10-27 21:22:23 +0000240 is_user = CPU_MEM_INDEX;
241 if (__builtin_expect(env->tlb_read[is_user][index].address !=
bellardb92e5a22003-08-08 23:58:05 +0000242 (addr & (TARGET_PAGE_MASK | (DATA_SIZE - 1))), 0)) {
bellard61382a52003-10-27 21:22:23 +0000243 res = glue(glue(__ld, SUFFIX), MMUSUFFIX)(addr, is_user);
bellardb92e5a22003-08-08 23:58:05 +0000244 } else {
bellard61382a52003-10-27 21:22:23 +0000245 physaddr = addr + env->tlb_read[is_user][index].addend;
246 res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)physaddr);
bellardb92e5a22003-08-08 23:58:05 +0000247 }
248 return res;
249}
250
251#if DATA_SIZE <= 2
252static inline int glue(glue(lds, SUFFIX), MEMSUFFIX)(void *ptr)
253{
254 int res, index;
255 unsigned long addr, physaddr;
bellard61382a52003-10-27 21:22:23 +0000256 int is_user;
257
bellardb92e5a22003-08-08 23:58:05 +0000258 addr = (unsigned long)ptr;
259 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard61382a52003-10-27 21:22:23 +0000260 is_user = CPU_MEM_INDEX;
261 if (__builtin_expect(env->tlb_read[is_user][index].address !=
bellardb92e5a22003-08-08 23:58:05 +0000262 (addr & (TARGET_PAGE_MASK | (DATA_SIZE - 1))), 0)) {
bellard61382a52003-10-27 21:22:23 +0000263 res = (DATA_STYPE)glue(glue(__ld, SUFFIX), MMUSUFFIX)(addr, is_user);
bellardb92e5a22003-08-08 23:58:05 +0000264 } else {
bellard61382a52003-10-27 21:22:23 +0000265 physaddr = addr + env->tlb_read[is_user][index].addend;
bellardb92e5a22003-08-08 23:58:05 +0000266 res = glue(glue(lds, SUFFIX), _raw)((uint8_t *)physaddr);
267 }
268 return res;
269}
270#endif
271
bellarde16c53f2004-01-04 18:15:29 +0000272/* generic store macro */
273
bellardb92e5a22003-08-08 23:58:05 +0000274static inline void glue(glue(st, SUFFIX), MEMSUFFIX)(void *ptr, RES_TYPE v)
275{
276 int index;
277 unsigned long addr, physaddr;
bellard61382a52003-10-27 21:22:23 +0000278 int is_user;
279
bellardb92e5a22003-08-08 23:58:05 +0000280 addr = (unsigned long)ptr;
281 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
bellard61382a52003-10-27 21:22:23 +0000282 is_user = CPU_MEM_INDEX;
283 if (__builtin_expect(env->tlb_write[is_user][index].address !=
bellardb92e5a22003-08-08 23:58:05 +0000284 (addr & (TARGET_PAGE_MASK | (DATA_SIZE - 1))), 0)) {
bellard61382a52003-10-27 21:22:23 +0000285 glue(glue(__st, SUFFIX), MMUSUFFIX)(addr, v, is_user);
bellardb92e5a22003-08-08 23:58:05 +0000286 } else {
bellard61382a52003-10-27 21:22:23 +0000287 physaddr = addr + env->tlb_write[is_user][index].addend;
bellardb92e5a22003-08-08 23:58:05 +0000288 glue(glue(st, SUFFIX), _raw)((uint8_t *)physaddr, v);
289 }
290}
291
bellarde16c53f2004-01-04 18:15:29 +0000292#endif
293
bellard2d603d22004-01-04 23:56:24 +0000294#if DATA_SIZE == 8
295static inline double glue(ldfq, MEMSUFFIX)(void *ptr)
296{
297 union {
298 double d;
299 uint64_t i;
300 } u;
301 u.i = glue(ldq, MEMSUFFIX)(ptr);
302 return u.d;
303}
304
305static inline void glue(stfq, MEMSUFFIX)(void *ptr, double v)
306{
307 union {
308 double d;
309 uint64_t i;
310 } u;
311 u.d = v;
312 glue(stq, MEMSUFFIX)(ptr, u.i);
313}
314#endif /* DATA_SIZE == 8 */
315
316#if DATA_SIZE == 4
317static inline float glue(ldfl, MEMSUFFIX)(void *ptr)
318{
319 union {
320 float f;
321 uint32_t i;
322 } u;
323 u.i = glue(ldl, MEMSUFFIX)(ptr);
324 return u.f;
325}
326
327static inline void glue(stfl, MEMSUFFIX)(void *ptr, float v)
328{
329 union {
330 float f;
331 uint32_t i;
332 } u;
333 u.f = v;
334 glue(stl, MEMSUFFIX)(ptr, u.i);
335}
336#endif /* DATA_SIZE == 4 */
337
bellardb92e5a22003-08-08 23:58:05 +0000338#undef RES_TYPE
339#undef DATA_TYPE
340#undef DATA_STYPE
341#undef SUFFIX
bellard61382a52003-10-27 21:22:23 +0000342#undef USUFFIX
bellardb92e5a22003-08-08 23:58:05 +0000343#undef DATA_SIZE
bellard61382a52003-10-27 21:22:23 +0000344#undef CPU_MEM_INDEX
345#undef MMUSUFFIX