bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 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 |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 22 | #define USUFFIX q |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 23 | #define DATA_TYPE uint64_t |
| 24 | #elif DATA_SIZE == 4 |
| 25 | #define SUFFIX l |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 26 | #define USUFFIX l |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 27 | #define DATA_TYPE uint32_t |
| 28 | #elif DATA_SIZE == 2 |
| 29 | #define SUFFIX w |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 30 | #define USUFFIX uw |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 31 | #define DATA_TYPE uint16_t |
| 32 | #define DATA_STYPE int16_t |
| 33 | #elif DATA_SIZE == 1 |
| 34 | #define SUFFIX b |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 35 | #define USUFFIX ub |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 36 | #define DATA_TYPE uint8_t |
| 37 | #define DATA_STYPE int8_t |
| 38 | #else |
| 39 | #error unsupported data size |
| 40 | #endif |
| 41 | |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 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 | |
bellard | 2d603d2 | 2004-01-04 23:56:24 +0000 | [diff] [blame] | 54 | #ifdef TARGET_I386 |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 55 | #define CPU_MEM_INDEX ((env->hflags & HF_CPL_MASK) == 3) |
bellard | 2d603d2 | 2004-01-04 23:56:24 +0000 | [diff] [blame] | 56 | #elif defined (TARGET_PPC) |
| 57 | #define CPU_MEM_INDEX (msr_pr) |
| 58 | #endif |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 59 | #define MMUSUFFIX _mmu |
| 60 | |
| 61 | #elif ACCESS_TYPE == 3 |
| 62 | |
bellard | 2d603d2 | 2004-01-04 23:56:24 +0000 | [diff] [blame] | 63 | #ifdef TARGET_I386 |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 64 | #define CPU_MEM_INDEX ((env->hflags & HF_CPL_MASK) == 3) |
bellard | 2d603d2 | 2004-01-04 23:56:24 +0000 | [diff] [blame] | 65 | #elif defined (TARGET_PPC) |
| 66 | #define CPU_MEM_INDEX (msr_pr) |
| 67 | #endif |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 68 | #define MMUSUFFIX _cmmu |
| 69 | |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 70 | #else |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 71 | #error invalid ACCESS_TYPE |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 72 | #endif |
| 73 | |
| 74 | #if DATA_SIZE == 8 |
| 75 | #define RES_TYPE uint64_t |
| 76 | #else |
| 77 | #define RES_TYPE int |
| 78 | #endif |
| 79 | |
| 80 | |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 81 | DATA_TYPE REGPARM(1) glue(glue(__ld, SUFFIX), MMUSUFFIX)(unsigned long addr, |
| 82 | int is_user); |
| 83 | void REGPARM(2) glue(glue(__st, SUFFIX), MMUSUFFIX)(unsigned long addr, DATA_TYPE v, int is_user); |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 84 | |
bellard | e16c53f | 2004-01-04 18:15:29 +0000 | [diff] [blame] | 85 | #if (DATA_SIZE <= 4) && defined(__i386__) && (ACCESS_TYPE <= 1) && defined(ASM_SOFTMMU) |
| 86 | |
| 87 | static 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 |
| 130 | static 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 | |
| 177 | static 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 | |
bellard | 8948b5d | 2003-10-28 23:04:30 +0000 | [diff] [blame] | 231 | static inline RES_TYPE glue(glue(ld, USUFFIX), MEMSUFFIX)(void *ptr) |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 232 | { |
| 233 | int index; |
| 234 | RES_TYPE res; |
| 235 | unsigned long addr, physaddr; |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 236 | int is_user; |
| 237 | |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 238 | addr = (unsigned long)ptr; |
| 239 | index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 240 | is_user = CPU_MEM_INDEX; |
| 241 | if (__builtin_expect(env->tlb_read[is_user][index].address != |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 242 | (addr & (TARGET_PAGE_MASK | (DATA_SIZE - 1))), 0)) { |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 243 | res = glue(glue(__ld, SUFFIX), MMUSUFFIX)(addr, is_user); |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 244 | } else { |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 245 | physaddr = addr + env->tlb_read[is_user][index].addend; |
| 246 | res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)physaddr); |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 247 | } |
| 248 | return res; |
| 249 | } |
| 250 | |
| 251 | #if DATA_SIZE <= 2 |
| 252 | static inline int glue(glue(lds, SUFFIX), MEMSUFFIX)(void *ptr) |
| 253 | { |
| 254 | int res, index; |
| 255 | unsigned long addr, physaddr; |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 256 | int is_user; |
| 257 | |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 258 | addr = (unsigned long)ptr; |
| 259 | index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 260 | is_user = CPU_MEM_INDEX; |
| 261 | if (__builtin_expect(env->tlb_read[is_user][index].address != |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 262 | (addr & (TARGET_PAGE_MASK | (DATA_SIZE - 1))), 0)) { |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 263 | res = (DATA_STYPE)glue(glue(__ld, SUFFIX), MMUSUFFIX)(addr, is_user); |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 264 | } else { |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 265 | physaddr = addr + env->tlb_read[is_user][index].addend; |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 266 | res = glue(glue(lds, SUFFIX), _raw)((uint8_t *)physaddr); |
| 267 | } |
| 268 | return res; |
| 269 | } |
| 270 | #endif |
| 271 | |
bellard | e16c53f | 2004-01-04 18:15:29 +0000 | [diff] [blame] | 272 | /* generic store macro */ |
| 273 | |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 274 | static inline void glue(glue(st, SUFFIX), MEMSUFFIX)(void *ptr, RES_TYPE v) |
| 275 | { |
| 276 | int index; |
| 277 | unsigned long addr, physaddr; |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 278 | int is_user; |
| 279 | |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 280 | addr = (unsigned long)ptr; |
| 281 | index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 282 | is_user = CPU_MEM_INDEX; |
| 283 | if (__builtin_expect(env->tlb_write[is_user][index].address != |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 284 | (addr & (TARGET_PAGE_MASK | (DATA_SIZE - 1))), 0)) { |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 285 | glue(glue(__st, SUFFIX), MMUSUFFIX)(addr, v, is_user); |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 286 | } else { |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 287 | physaddr = addr + env->tlb_write[is_user][index].addend; |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 288 | glue(glue(st, SUFFIX), _raw)((uint8_t *)physaddr, v); |
| 289 | } |
| 290 | } |
| 291 | |
bellard | e16c53f | 2004-01-04 18:15:29 +0000 | [diff] [blame] | 292 | #endif |
| 293 | |
bellard | 2d603d2 | 2004-01-04 23:56:24 +0000 | [diff] [blame] | 294 | #if DATA_SIZE == 8 |
| 295 | static 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 | |
| 305 | static 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 |
| 317 | static 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 | |
| 327 | static 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 | |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 338 | #undef RES_TYPE |
| 339 | #undef DATA_TYPE |
| 340 | #undef DATA_STYPE |
| 341 | #undef SUFFIX |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 342 | #undef USUFFIX |
bellard | b92e5a2 | 2003-08-08 23:58:05 +0000 | [diff] [blame] | 343 | #undef DATA_SIZE |
bellard | 61382a5 | 2003-10-27 21:22:23 +0000 | [diff] [blame] | 344 | #undef CPU_MEM_INDEX |
| 345 | #undef MMUSUFFIX |