Statistics
| Branch: | Revision:

root / softmmu_header.h @ abf28337

History | View | Annotate | Download (9.8 kB)

1 b92e5a22 bellard
/*
2 b92e5a22 bellard
 *  Software MMU support
3 5fafdf24 ths
 *
4 b92e5a22 bellard
 *  Copyright (c) 2003 Fabrice Bellard
5 b92e5a22 bellard
 *
6 b92e5a22 bellard
 * This library is free software; you can redistribute it and/or
7 b92e5a22 bellard
 * modify it under the terms of the GNU Lesser General Public
8 b92e5a22 bellard
 * License as published by the Free Software Foundation; either
9 b92e5a22 bellard
 * version 2 of the License, or (at your option) any later version.
10 b92e5a22 bellard
 *
11 b92e5a22 bellard
 * This library is distributed in the hope that it will be useful,
12 b92e5a22 bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 b92e5a22 bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 b92e5a22 bellard
 * Lesser General Public License for more details.
15 b92e5a22 bellard
 *
16 b92e5a22 bellard
 * You should have received a copy of the GNU Lesser General Public
17 b92e5a22 bellard
 * License along with this library; if not, write to the Free Software
18 b92e5a22 bellard
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 b92e5a22 bellard
 */
20 b92e5a22 bellard
#if DATA_SIZE == 8
21 b92e5a22 bellard
#define SUFFIX q
22 61382a50 bellard
#define USUFFIX q
23 b92e5a22 bellard
#define DATA_TYPE uint64_t
24 b92e5a22 bellard
#elif DATA_SIZE == 4
25 b92e5a22 bellard
#define SUFFIX l
26 61382a50 bellard
#define USUFFIX l
27 b92e5a22 bellard
#define DATA_TYPE uint32_t
28 b92e5a22 bellard
#elif DATA_SIZE == 2
29 b92e5a22 bellard
#define SUFFIX w
30 61382a50 bellard
#define USUFFIX uw
31 b92e5a22 bellard
#define DATA_TYPE uint16_t
32 b92e5a22 bellard
#define DATA_STYPE int16_t
33 b92e5a22 bellard
#elif DATA_SIZE == 1
34 b92e5a22 bellard
#define SUFFIX b
35 61382a50 bellard
#define USUFFIX ub
36 b92e5a22 bellard
#define DATA_TYPE uint8_t
37 b92e5a22 bellard
#define DATA_STYPE int8_t
38 b92e5a22 bellard
#else
39 b92e5a22 bellard
#error unsupported data size
40 b92e5a22 bellard
#endif
41 b92e5a22 bellard
42 6ebbf390 j_mayer
#if ACCESS_TYPE < (NB_MMU_MODES)
43 61382a50 bellard
44 6ebbf390 j_mayer
#define CPU_MMU_INDEX ACCESS_TYPE
45 61382a50 bellard
#define MMUSUFFIX _mmu
46 61382a50 bellard
47 6ebbf390 j_mayer
#elif ACCESS_TYPE == (NB_MMU_MODES)
48 61382a50 bellard
49 6ebbf390 j_mayer
#define CPU_MMU_INDEX (cpu_mmu_index(env))
50 61382a50 bellard
#define MMUSUFFIX _mmu
51 61382a50 bellard
52 6ebbf390 j_mayer
#elif ACCESS_TYPE == (NB_MMU_MODES + 1)
53 61382a50 bellard
54 6ebbf390 j_mayer
#define CPU_MMU_INDEX (cpu_mmu_index(env))
55 61382a50 bellard
#define MMUSUFFIX _cmmu
56 61382a50 bellard
57 b92e5a22 bellard
#else
58 61382a50 bellard
#error invalid ACCESS_TYPE
59 b92e5a22 bellard
#endif
60 b92e5a22 bellard
61 b92e5a22 bellard
#if DATA_SIZE == 8
62 b92e5a22 bellard
#define RES_TYPE uint64_t
63 b92e5a22 bellard
#else
64 b92e5a22 bellard
#define RES_TYPE int
65 b92e5a22 bellard
#endif
66 b92e5a22 bellard
67 6ebbf390 j_mayer
#if ACCESS_TYPE == (NB_MMU_MODES + 1)
68 84b7b8e7 bellard
#define ADDR_READ addr_code
69 84b7b8e7 bellard
#else
70 84b7b8e7 bellard
#define ADDR_READ addr_read
71 84b7b8e7 bellard
#endif
72 b92e5a22 bellard
73 c27004ec bellard
DATA_TYPE REGPARM(1) glue(glue(__ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
74 6ebbf390 j_mayer
                                                         int mmu_idx);
75 6ebbf390 j_mayer
void REGPARM(2) glue(glue(__st, SUFFIX), MMUSUFFIX)(target_ulong addr, DATA_TYPE v, int mmu_idx);
76 b92e5a22 bellard
77 c27004ec bellard
#if (DATA_SIZE <= 4) && (TARGET_LONG_BITS == 32) && defined(__i386__) && \
78 6ebbf390 j_mayer
    (ACCESS_TYPE < NB_MMU_MODES) && defined(ASM_SOFTMMU)
79 e16c53fa bellard
80 84b7b8e7 bellard
#define CPU_TLB_ENTRY_BITS 4
81 84b7b8e7 bellard
82 c27004ec bellard
static inline RES_TYPE glue(glue(ld, USUFFIX), MEMSUFFIX)(target_ulong ptr)
83 e16c53fa bellard
{
84 e16c53fa bellard
    int res;
85 e16c53fa bellard
86 e16c53fa bellard
    asm volatile ("movl %1, %%edx\n"
87 e16c53fa bellard
                  "movl %1, %%eax\n"
88 e16c53fa bellard
                  "shrl %3, %%edx\n"
89 e16c53fa bellard
                  "andl %4, %%eax\n"
90 e16c53fa bellard
                  "andl %2, %%edx\n"
91 e16c53fa bellard
                  "leal %5(%%edx, %%ebp), %%edx\n"
92 e16c53fa bellard
                  "cmpl (%%edx), %%eax\n"
93 e16c53fa bellard
                  "movl %1, %%eax\n"
94 e16c53fa bellard
                  "je 1f\n"
95 e16c53fa bellard
                  "pushl %6\n"
96 e16c53fa bellard
                  "call %7\n"
97 e16c53fa bellard
                  "popl %%edx\n"
98 e16c53fa bellard
                  "movl %%eax, %0\n"
99 e16c53fa bellard
                  "jmp 2f\n"
100 e16c53fa bellard
                  "1:\n"
101 84b7b8e7 bellard
                  "addl 12(%%edx), %%eax\n"
102 e16c53fa bellard
#if DATA_SIZE == 1
103 e16c53fa bellard
                  "movzbl (%%eax), %0\n"
104 e16c53fa bellard
#elif DATA_SIZE == 2
105 e16c53fa bellard
                  "movzwl (%%eax), %0\n"
106 e16c53fa bellard
#elif DATA_SIZE == 4
107 e16c53fa bellard
                  "movl (%%eax), %0\n"
108 e16c53fa bellard
#else
109 e16c53fa bellard
#error unsupported size
110 e16c53fa bellard
#endif
111 e16c53fa bellard
                  "2:\n"
112 e16c53fa bellard
                  : "=r" (res)
113 5fafdf24 ths
                  : "r" (ptr),
114 5fafdf24 ths
                  "i" ((CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS),
115 5fafdf24 ths
                  "i" (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS),
116 e16c53fa bellard
                  "i" (TARGET_PAGE_MASK | (DATA_SIZE - 1)),
117 6ebbf390 j_mayer
                  "m" (*(uint32_t *)offsetof(CPUState, tlb_table[CPU_MMU_INDEX][0].addr_read)),
118 6ebbf390 j_mayer
                  "i" (CPU_MMU_INDEX),
119 e16c53fa bellard
                  "m" (*(uint8_t *)&glue(glue(__ld, SUFFIX), MMUSUFFIX))
120 e16c53fa bellard
                  : "%eax", "%ecx", "%edx", "memory", "cc");
121 e16c53fa bellard
    return res;
122 e16c53fa bellard
}
123 e16c53fa bellard
124 e16c53fa bellard
#if DATA_SIZE <= 2
125 c27004ec bellard
static inline int glue(glue(lds, SUFFIX), MEMSUFFIX)(target_ulong ptr)
126 e16c53fa bellard
{
127 e16c53fa bellard
    int res;
128 e16c53fa bellard
129 e16c53fa bellard
    asm volatile ("movl %1, %%edx\n"
130 e16c53fa bellard
                  "movl %1, %%eax\n"
131 e16c53fa bellard
                  "shrl %3, %%edx\n"
132 e16c53fa bellard
                  "andl %4, %%eax\n"
133 e16c53fa bellard
                  "andl %2, %%edx\n"
134 e16c53fa bellard
                  "leal %5(%%edx, %%ebp), %%edx\n"
135 e16c53fa bellard
                  "cmpl (%%edx), %%eax\n"
136 e16c53fa bellard
                  "movl %1, %%eax\n"
137 e16c53fa bellard
                  "je 1f\n"
138 e16c53fa bellard
                  "pushl %6\n"
139 e16c53fa bellard
                  "call %7\n"
140 e16c53fa bellard
                  "popl %%edx\n"
141 e16c53fa bellard
#if DATA_SIZE == 1
142 e16c53fa bellard
                  "movsbl %%al, %0\n"
143 e16c53fa bellard
#elif DATA_SIZE == 2
144 e16c53fa bellard
                  "movswl %%ax, %0\n"
145 e16c53fa bellard
#else
146 e16c53fa bellard
#error unsupported size
147 e16c53fa bellard
#endif
148 e16c53fa bellard
                  "jmp 2f\n"
149 e16c53fa bellard
                  "1:\n"
150 84b7b8e7 bellard
                  "addl 12(%%edx), %%eax\n"
151 e16c53fa bellard
#if DATA_SIZE == 1
152 e16c53fa bellard
                  "movsbl (%%eax), %0\n"
153 e16c53fa bellard
#elif DATA_SIZE == 2
154 e16c53fa bellard
                  "movswl (%%eax), %0\n"
155 e16c53fa bellard
#else
156 e16c53fa bellard
#error unsupported size
157 e16c53fa bellard
#endif
158 e16c53fa bellard
                  "2:\n"
159 e16c53fa bellard
                  : "=r" (res)
160 5fafdf24 ths
                  : "r" (ptr),
161 5fafdf24 ths
                  "i" ((CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS),
162 5fafdf24 ths
                  "i" (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS),
163 e16c53fa bellard
                  "i" (TARGET_PAGE_MASK | (DATA_SIZE - 1)),
164 6ebbf390 j_mayer
                  "m" (*(uint32_t *)offsetof(CPUState, tlb_table[CPU_MMU_INDEX][0].addr_read)),
165 6ebbf390 j_mayer
                  "i" (CPU_MMU_INDEX),
166 e16c53fa bellard
                  "m" (*(uint8_t *)&glue(glue(__ld, SUFFIX), MMUSUFFIX))
167 e16c53fa bellard
                  : "%eax", "%ecx", "%edx", "memory", "cc");
168 e16c53fa bellard
    return res;
169 e16c53fa bellard
}
170 e16c53fa bellard
#endif
171 e16c53fa bellard
172 c27004ec bellard
static inline void glue(glue(st, SUFFIX), MEMSUFFIX)(target_ulong ptr, RES_TYPE v)
173 e16c53fa bellard
{
174 e16c53fa bellard
    asm volatile ("movl %0, %%edx\n"
175 e16c53fa bellard
                  "movl %0, %%eax\n"
176 e16c53fa bellard
                  "shrl %3, %%edx\n"
177 e16c53fa bellard
                  "andl %4, %%eax\n"
178 e16c53fa bellard
                  "andl %2, %%edx\n"
179 e16c53fa bellard
                  "leal %5(%%edx, %%ebp), %%edx\n"
180 e16c53fa bellard
                  "cmpl (%%edx), %%eax\n"
181 e16c53fa bellard
                  "movl %0, %%eax\n"
182 e16c53fa bellard
                  "je 1f\n"
183 e16c53fa bellard
#if DATA_SIZE == 1
184 e16c53fa bellard
                  "movzbl %b1, %%edx\n"
185 e16c53fa bellard
#elif DATA_SIZE == 2
186 e16c53fa bellard
                  "movzwl %w1, %%edx\n"
187 e16c53fa bellard
#elif DATA_SIZE == 4
188 e16c53fa bellard
                  "movl %1, %%edx\n"
189 e16c53fa bellard
#else
190 e16c53fa bellard
#error unsupported size
191 e16c53fa bellard
#endif
192 e16c53fa bellard
                  "pushl %6\n"
193 e16c53fa bellard
                  "call %7\n"
194 e16c53fa bellard
                  "popl %%eax\n"
195 e16c53fa bellard
                  "jmp 2f\n"
196 e16c53fa bellard
                  "1:\n"
197 84b7b8e7 bellard
                  "addl 8(%%edx), %%eax\n"
198 e16c53fa bellard
#if DATA_SIZE == 1
199 e16c53fa bellard
                  "movb %b1, (%%eax)\n"
200 e16c53fa bellard
#elif DATA_SIZE == 2
201 e16c53fa bellard
                  "movw %w1, (%%eax)\n"
202 e16c53fa bellard
#elif DATA_SIZE == 4
203 e16c53fa bellard
                  "movl %1, (%%eax)\n"
204 e16c53fa bellard
#else
205 e16c53fa bellard
#error unsupported size
206 e16c53fa bellard
#endif
207 e16c53fa bellard
                  "2:\n"
208 5fafdf24 ths
                  :
209 5fafdf24 ths
                  : "r" (ptr),
210 e16c53fa bellard
/* NOTE: 'q' would be needed as constraint, but we could not use it
211 e16c53fa bellard
   with T1 ! */
212 5fafdf24 ths
                  "r" (v),
213 5fafdf24 ths
                  "i" ((CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS),
214 5fafdf24 ths
                  "i" (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS),
215 e16c53fa bellard
                  "i" (TARGET_PAGE_MASK | (DATA_SIZE - 1)),
216 6ebbf390 j_mayer
                  "m" (*(uint32_t *)offsetof(CPUState, tlb_table[CPU_MMU_INDEX][0].addr_write)),
217 6ebbf390 j_mayer
                  "i" (CPU_MMU_INDEX),
218 e16c53fa bellard
                  "m" (*(uint8_t *)&glue(glue(__st, SUFFIX), MMUSUFFIX))
219 e16c53fa bellard
                  : "%eax", "%ecx", "%edx", "memory", "cc");
220 e16c53fa bellard
}
221 e16c53fa bellard
222 e16c53fa bellard
#else
223 e16c53fa bellard
224 e16c53fa bellard
/* generic load/store macros */
225 e16c53fa bellard
226 c27004ec bellard
static inline RES_TYPE glue(glue(ld, USUFFIX), MEMSUFFIX)(target_ulong ptr)
227 b92e5a22 bellard
{
228 b92e5a22 bellard
    int index;
229 b92e5a22 bellard
    RES_TYPE res;
230 c27004ec bellard
    target_ulong addr;
231 c27004ec bellard
    unsigned long physaddr;
232 6ebbf390 j_mayer
    int mmu_idx;
233 61382a50 bellard
234 c27004ec bellard
    addr = ptr;
235 b92e5a22 bellard
    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
236 6ebbf390 j_mayer
    mmu_idx = CPU_MMU_INDEX;
237 6ebbf390 j_mayer
    if (__builtin_expect(env->tlb_table[mmu_idx][index].ADDR_READ !=
238 b92e5a22 bellard
                         (addr & (TARGET_PAGE_MASK | (DATA_SIZE - 1))), 0)) {
239 6ebbf390 j_mayer
        res = glue(glue(__ld, SUFFIX), MMUSUFFIX)(addr, mmu_idx);
240 b92e5a22 bellard
    } else {
241 6ebbf390 j_mayer
        physaddr = addr + env->tlb_table[mmu_idx][index].addend;
242 61382a50 bellard
        res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)physaddr);
243 b92e5a22 bellard
    }
244 b92e5a22 bellard
    return res;
245 b92e5a22 bellard
}
246 b92e5a22 bellard
247 b92e5a22 bellard
#if DATA_SIZE <= 2
248 c27004ec bellard
static inline int glue(glue(lds, SUFFIX), MEMSUFFIX)(target_ulong ptr)
249 b92e5a22 bellard
{
250 b92e5a22 bellard
    int res, index;
251 c27004ec bellard
    target_ulong addr;
252 c27004ec bellard
    unsigned long physaddr;
253 6ebbf390 j_mayer
    int mmu_idx;
254 61382a50 bellard
255 c27004ec bellard
    addr = ptr;
256 b92e5a22 bellard
    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
257 6ebbf390 j_mayer
    mmu_idx = CPU_MMU_INDEX;
258 6ebbf390 j_mayer
    if (__builtin_expect(env->tlb_table[mmu_idx][index].ADDR_READ !=
259 b92e5a22 bellard
                         (addr & (TARGET_PAGE_MASK | (DATA_SIZE - 1))), 0)) {
260 6ebbf390 j_mayer
        res = (DATA_STYPE)glue(glue(__ld, SUFFIX), MMUSUFFIX)(addr, mmu_idx);
261 b92e5a22 bellard
    } else {
262 6ebbf390 j_mayer
        physaddr = addr + env->tlb_table[mmu_idx][index].addend;
263 b92e5a22 bellard
        res = glue(glue(lds, SUFFIX), _raw)((uint8_t *)physaddr);
264 b92e5a22 bellard
    }
265 b92e5a22 bellard
    return res;
266 b92e5a22 bellard
}
267 b92e5a22 bellard
#endif
268 b92e5a22 bellard
269 6ebbf390 j_mayer
#if ACCESS_TYPE != (NB_MMU_MODES + 1)
270 84b7b8e7 bellard
271 e16c53fa bellard
/* generic store macro */
272 e16c53fa bellard
273 c27004ec bellard
static inline void glue(glue(st, SUFFIX), MEMSUFFIX)(target_ulong ptr, RES_TYPE v)
274 b92e5a22 bellard
{
275 b92e5a22 bellard
    int index;
276 c27004ec bellard
    target_ulong addr;
277 c27004ec bellard
    unsigned long physaddr;
278 6ebbf390 j_mayer
    int mmu_idx;
279 61382a50 bellard
280 c27004ec bellard
    addr = ptr;
281 b92e5a22 bellard
    index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
282 6ebbf390 j_mayer
    mmu_idx = CPU_MMU_INDEX;
283 6ebbf390 j_mayer
    if (__builtin_expect(env->tlb_table[mmu_idx][index].addr_write !=
284 b92e5a22 bellard
                         (addr & (TARGET_PAGE_MASK | (DATA_SIZE - 1))), 0)) {
285 6ebbf390 j_mayer
        glue(glue(__st, SUFFIX), MMUSUFFIX)(addr, v, mmu_idx);
286 b92e5a22 bellard
    } else {
287 6ebbf390 j_mayer
        physaddr = addr + env->tlb_table[mmu_idx][index].addend;
288 b92e5a22 bellard
        glue(glue(st, SUFFIX), _raw)((uint8_t *)physaddr, v);
289 b92e5a22 bellard
    }
290 b92e5a22 bellard
}
291 b92e5a22 bellard
292 6ebbf390 j_mayer
#endif /* ACCESS_TYPE != (NB_MMU_MODES + 1) */
293 84b7b8e7 bellard
294 84b7b8e7 bellard
#endif /* !asm */
295 84b7b8e7 bellard
296 6ebbf390 j_mayer
#if ACCESS_TYPE != (NB_MMU_MODES + 1)
297 e16c53fa bellard
298 2d603d22 bellard
#if DATA_SIZE == 8
299 3f87bf69 bellard
static inline float64 glue(ldfq, MEMSUFFIX)(target_ulong ptr)
300 2d603d22 bellard
{
301 2d603d22 bellard
    union {
302 3f87bf69 bellard
        float64 d;
303 2d603d22 bellard
        uint64_t i;
304 2d603d22 bellard
    } u;
305 2d603d22 bellard
    u.i = glue(ldq, MEMSUFFIX)(ptr);
306 2d603d22 bellard
    return u.d;
307 2d603d22 bellard
}
308 2d603d22 bellard
309 3f87bf69 bellard
static inline void glue(stfq, MEMSUFFIX)(target_ulong ptr, float64 v)
310 2d603d22 bellard
{
311 2d603d22 bellard
    union {
312 3f87bf69 bellard
        float64 d;
313 2d603d22 bellard
        uint64_t i;
314 2d603d22 bellard
    } u;
315 2d603d22 bellard
    u.d = v;
316 2d603d22 bellard
    glue(stq, MEMSUFFIX)(ptr, u.i);
317 2d603d22 bellard
}
318 2d603d22 bellard
#endif /* DATA_SIZE == 8 */
319 2d603d22 bellard
320 2d603d22 bellard
#if DATA_SIZE == 4
321 3f87bf69 bellard
static inline float32 glue(ldfl, MEMSUFFIX)(target_ulong ptr)
322 2d603d22 bellard
{
323 2d603d22 bellard
    union {
324 3f87bf69 bellard
        float32 f;
325 2d603d22 bellard
        uint32_t i;
326 2d603d22 bellard
    } u;
327 2d603d22 bellard
    u.i = glue(ldl, MEMSUFFIX)(ptr);
328 2d603d22 bellard
    return u.f;
329 2d603d22 bellard
}
330 2d603d22 bellard
331 3f87bf69 bellard
static inline void glue(stfl, MEMSUFFIX)(target_ulong ptr, float32 v)
332 2d603d22 bellard
{
333 2d603d22 bellard
    union {
334 3f87bf69 bellard
        float32 f;
335 2d603d22 bellard
        uint32_t i;
336 2d603d22 bellard
    } u;
337 2d603d22 bellard
    u.f = v;
338 2d603d22 bellard
    glue(stl, MEMSUFFIX)(ptr, u.i);
339 2d603d22 bellard
}
340 2d603d22 bellard
#endif /* DATA_SIZE == 4 */
341 2d603d22 bellard
342 6ebbf390 j_mayer
#endif /* ACCESS_TYPE != (NB_MMU_MODES + 1) */
343 84b7b8e7 bellard
344 b92e5a22 bellard
#undef RES_TYPE
345 b92e5a22 bellard
#undef DATA_TYPE
346 b92e5a22 bellard
#undef DATA_STYPE
347 b92e5a22 bellard
#undef SUFFIX
348 61382a50 bellard
#undef USUFFIX
349 b92e5a22 bellard
#undef DATA_SIZE
350 6ebbf390 j_mayer
#undef CPU_MMU_INDEX
351 61382a50 bellard
#undef MMUSUFFIX
352 84b7b8e7 bellard
#undef ADDR_READ