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