fixed register constraint
[qemu] / softmmu_header.h
1 /*
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
22 #define USUFFIX q
23 #define DATA_TYPE uint64_t
24 #elif DATA_SIZE == 4
25 #define SUFFIX l
26 #define USUFFIX l
27 #define DATA_TYPE uint32_t
28 #elif DATA_SIZE == 2
29 #define SUFFIX w
30 #define USUFFIX uw
31 #define DATA_TYPE uint16_t
32 #define DATA_STYPE int16_t
33 #elif DATA_SIZE == 1
34 #define SUFFIX b
35 #define USUFFIX ub
36 #define DATA_TYPE uint8_t
37 #define DATA_STYPE int8_t
38 #else
39 #error unsupported data size
40 #endif
41
42 #if ACCESS_TYPE < (NB_MMU_MODES)
43
44 #define CPU_MMU_INDEX ACCESS_TYPE
45 #define MMUSUFFIX _mmu
46
47 #elif ACCESS_TYPE == (NB_MMU_MODES)
48
49 #define CPU_MMU_INDEX (cpu_mmu_index(env))
50 #define MMUSUFFIX _mmu
51
52 #elif ACCESS_TYPE == (NB_MMU_MODES + 1)
53
54 #define CPU_MMU_INDEX (cpu_mmu_index(env))
55 #define MMUSUFFIX _cmmu
56
57 #else
58 #error invalid ACCESS_TYPE
59 #endif
60
61 #if DATA_SIZE == 8
62 #define RES_TYPE uint64_t
63 #else
64 #define RES_TYPE int
65 #endif
66
67 #if ACCESS_TYPE == (NB_MMU_MODES + 1)
68 #define ADDR_READ addr_code
69 #else
70 #define ADDR_READ addr_read
71 #endif
72
73 DATA_TYPE REGPARM(1) glue(glue(__ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
74                                                          int mmu_idx);
75 void REGPARM(2) glue(glue(__st, SUFFIX), MMUSUFFIX)(target_ulong addr, DATA_TYPE v, int mmu_idx);
76
77 #if (DATA_SIZE <= 4) && (TARGET_LONG_BITS == 32) && defined(__i386__) && \
78     (ACCESS_TYPE < NB_MMU_MODES) && defined(ASM_SOFTMMU)
79
80 #define CPU_TLB_ENTRY_BITS 4
81
82 static inline RES_TYPE glue(glue(ld, USUFFIX), MEMSUFFIX)(target_ulong ptr)
83 {
84     int res;
85
86     asm volatile ("movl %1, %%edx\n"
87                   "movl %1, %%eax\n"
88                   "shrl %3, %%edx\n"
89                   "andl %4, %%eax\n"
90                   "andl %2, %%edx\n"
91                   "leal %5(%%edx, %%ebp), %%edx\n"
92                   "cmpl (%%edx), %%eax\n"
93                   "movl %1, %%eax\n"
94                   "je 1f\n"
95                   "pushl %6\n"
96                   "call %7\n"
97                   "popl %%edx\n"
98                   "movl %%eax, %0\n"
99                   "jmp 2f\n"
100                   "1:\n"
101                   "addl 12(%%edx), %%eax\n"
102 #if DATA_SIZE == 1
103                   "movzbl (%%eax), %0\n"
104 #elif DATA_SIZE == 2
105                   "movzwl (%%eax), %0\n"
106 #elif DATA_SIZE == 4
107                   "movl (%%eax), %0\n"
108 #else
109 #error unsupported size
110 #endif
111                   "2:\n"
112                   : "=r" (res)
113                   : "r" (ptr),
114                   "i" ((CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS),
115                   "i" (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS),
116                   "i" (TARGET_PAGE_MASK | (DATA_SIZE - 1)),
117                   "m" (*(uint32_t *)offsetof(CPUState, tlb_table[CPU_MMU_INDEX][0].addr_read)),
118                   "i" (CPU_MMU_INDEX),
119                   "m" (*(uint8_t *)&glue(glue(__ld, SUFFIX), MMUSUFFIX))
120                   : "%eax", "%ecx", "%edx", "memory", "cc");
121     return res;
122 }
123
124 #if DATA_SIZE <= 2
125 static inline int glue(glue(lds, SUFFIX), MEMSUFFIX)(target_ulong ptr)
126 {
127     int res;
128
129     asm volatile ("movl %1, %%edx\n"
130                   "movl %1, %%eax\n"
131                   "shrl %3, %%edx\n"
132                   "andl %4, %%eax\n"
133                   "andl %2, %%edx\n"
134                   "leal %5(%%edx, %%ebp), %%edx\n"
135                   "cmpl (%%edx), %%eax\n"
136                   "movl %1, %%eax\n"
137                   "je 1f\n"
138                   "pushl %6\n"
139                   "call %7\n"
140                   "popl %%edx\n"
141 #if DATA_SIZE == 1
142                   "movsbl %%al, %0\n"
143 #elif DATA_SIZE == 2
144                   "movswl %%ax, %0\n"
145 #else
146 #error unsupported size
147 #endif
148                   "jmp 2f\n"
149                   "1:\n"
150                   "addl 12(%%edx), %%eax\n"
151 #if DATA_SIZE == 1
152                   "movsbl (%%eax), %0\n"
153 #elif DATA_SIZE == 2
154                   "movswl (%%eax), %0\n"
155 #else
156 #error unsupported size
157 #endif
158                   "2:\n"
159                   : "=r" (res)
160                   : "r" (ptr),
161                   "i" ((CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS),
162                   "i" (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS),
163                   "i" (TARGET_PAGE_MASK | (DATA_SIZE - 1)),
164                   "m" (*(uint32_t *)offsetof(CPUState, tlb_table[CPU_MMU_INDEX][0].addr_read)),
165                   "i" (CPU_MMU_INDEX),
166                   "m" (*(uint8_t *)&glue(glue(__ld, SUFFIX), MMUSUFFIX))
167                   : "%eax", "%ecx", "%edx", "memory", "cc");
168     return res;
169 }
170 #endif
171
172 static inline void glue(glue(st, SUFFIX), MEMSUFFIX)(target_ulong ptr, RES_TYPE v)
173 {
174     asm volatile ("movl %0, %%edx\n"
175                   "movl %0, %%eax\n"
176                   "shrl %3, %%edx\n"
177                   "andl %4, %%eax\n"
178                   "andl %2, %%edx\n"
179                   "leal %5(%%edx, %%ebp), %%edx\n"
180                   "cmpl (%%edx), %%eax\n"
181                   "movl %0, %%eax\n"
182                   "je 1f\n"
183 #if DATA_SIZE == 1
184                   "movzbl %b1, %%edx\n"
185 #elif DATA_SIZE == 2
186                   "movzwl %w1, %%edx\n"
187 #elif DATA_SIZE == 4
188                   "movl %1, %%edx\n"
189 #else
190 #error unsupported size
191 #endif
192                   "pushl %6\n"
193                   "call %7\n"
194                   "popl %%eax\n"
195                   "jmp 2f\n"
196                   "1:\n"
197                   "addl 8(%%edx), %%eax\n"
198 #if DATA_SIZE == 1
199                   "movb %b1, (%%eax)\n"
200 #elif DATA_SIZE == 2
201                   "movw %w1, (%%eax)\n"
202 #elif DATA_SIZE == 4
203                   "movl %1, (%%eax)\n"
204 #else
205 #error unsupported size
206 #endif
207                   "2:\n"
208                   :
209                   : "r" (ptr),
210 #if DATA_SIZE == 1
211                   "q" (v),
212 #else
213                   "r" (v),
214 #endif
215                   "i" ((CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS),
216                   "i" (TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS),
217                   "i" (TARGET_PAGE_MASK | (DATA_SIZE - 1)),
218                   "m" (*(uint32_t *)offsetof(CPUState, tlb_table[CPU_MMU_INDEX][0].addr_write)),
219                   "i" (CPU_MMU_INDEX),
220                   "m" (*(uint8_t *)&glue(glue(__st, SUFFIX), MMUSUFFIX))
221                   : "%eax", "%ecx", "%edx", "memory", "cc");
222 }
223
224 #else
225
226 /* generic load/store macros */
227
228 static inline RES_TYPE glue(glue(ld, USUFFIX), MEMSUFFIX)(target_ulong ptr)
229 {
230     int index;
231     RES_TYPE res;
232     target_ulong addr;
233     unsigned long physaddr;
234     int mmu_idx;
235
236     addr = ptr;
237     index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
238     mmu_idx = CPU_MMU_INDEX;
239     if (__builtin_expect(env->tlb_table[mmu_idx][index].ADDR_READ !=
240                          (addr & (TARGET_PAGE_MASK | (DATA_SIZE - 1))), 0)) {
241         res = glue(glue(__ld, SUFFIX), MMUSUFFIX)(addr, mmu_idx);
242     } else {
243         physaddr = addr + env->tlb_table[mmu_idx][index].addend;
244         res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)physaddr);
245     }
246     return res;
247 }
248
249 #if DATA_SIZE <= 2
250 static inline int glue(glue(lds, SUFFIX), MEMSUFFIX)(target_ulong ptr)
251 {
252     int res, index;
253     target_ulong addr;
254     unsigned long physaddr;
255     int mmu_idx;
256
257     addr = ptr;
258     index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
259     mmu_idx = CPU_MMU_INDEX;
260     if (__builtin_expect(env->tlb_table[mmu_idx][index].ADDR_READ !=
261                          (addr & (TARGET_PAGE_MASK | (DATA_SIZE - 1))), 0)) {
262         res = (DATA_STYPE)glue(glue(__ld, SUFFIX), MMUSUFFIX)(addr, mmu_idx);
263     } else {
264         physaddr = addr + env->tlb_table[mmu_idx][index].addend;
265         res = glue(glue(lds, SUFFIX), _raw)((uint8_t *)physaddr);
266     }
267     return res;
268 }
269 #endif
270
271 #if ACCESS_TYPE != (NB_MMU_MODES + 1)
272
273 /* generic store macro */
274
275 static inline void glue(glue(st, SUFFIX), MEMSUFFIX)(target_ulong ptr, RES_TYPE v)
276 {
277     int index;
278     target_ulong addr;
279     unsigned long physaddr;
280     int mmu_idx;
281
282     addr = ptr;
283     index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
284     mmu_idx = CPU_MMU_INDEX;
285     if (__builtin_expect(env->tlb_table[mmu_idx][index].addr_write !=
286                          (addr & (TARGET_PAGE_MASK | (DATA_SIZE - 1))), 0)) {
287         glue(glue(__st, SUFFIX), MMUSUFFIX)(addr, v, mmu_idx);
288     } else {
289         physaddr = addr + env->tlb_table[mmu_idx][index].addend;
290         glue(glue(st, SUFFIX), _raw)((uint8_t *)physaddr, v);
291     }
292 }
293
294 #endif /* ACCESS_TYPE != (NB_MMU_MODES + 1) */
295
296 #endif /* !asm */
297
298 #if ACCESS_TYPE != (NB_MMU_MODES + 1)
299
300 #if DATA_SIZE == 8
301 static inline float64 glue(ldfq, MEMSUFFIX)(target_ulong ptr)
302 {
303     union {
304         float64 d;
305         uint64_t i;
306     } u;
307     u.i = glue(ldq, MEMSUFFIX)(ptr);
308     return u.d;
309 }
310
311 static inline void glue(stfq, MEMSUFFIX)(target_ulong ptr, float64 v)
312 {
313     union {
314         float64 d;
315         uint64_t i;
316     } u;
317     u.d = v;
318     glue(stq, MEMSUFFIX)(ptr, u.i);
319 }
320 #endif /* DATA_SIZE == 8 */
321
322 #if DATA_SIZE == 4
323 static inline float32 glue(ldfl, MEMSUFFIX)(target_ulong ptr)
324 {
325     union {
326         float32 f;
327         uint32_t i;
328     } u;
329     u.i = glue(ldl, MEMSUFFIX)(ptr);
330     return u.f;
331 }
332
333 static inline void glue(stfl, MEMSUFFIX)(target_ulong ptr, float32 v)
334 {
335     union {
336         float32 f;
337         uint32_t i;
338     } u;
339     u.f = v;
340     glue(stl, MEMSUFFIX)(ptr, u.i);
341 }
342 #endif /* DATA_SIZE == 4 */
343
344 #endif /* ACCESS_TYPE != (NB_MMU_MODES + 1) */
345
346 #undef RES_TYPE
347 #undef DATA_TYPE
348 #undef DATA_STYPE
349 #undef SUFFIX
350 #undef USUFFIX
351 #undef DATA_SIZE
352 #undef CPU_MMU_INDEX
353 #undef MMUSUFFIX
354 #undef ADDR_READ