1 /* 2 * Software MMU support 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, see <http://www.gnu.org/licenses/>. 16 * 17 */ 18 19 /* 20 * Generate inline load/store functions for all MMU modes (typically 21 * at least _user and _kernel) as well as _data versions, for all data 22 * sizes. 23 * 24 * Used by target op helpers. 25 * 26 * The syntax for the accessors is: 27 * 28 * load: cpu_ld{sign}{size}_{mmusuffix}(env, ptr) 29 * 30 * store: cpu_st{sign}{size}_{mmusuffix}(env, ptr, val) 31 * 32 * sign is: 33 * (empty): for 32 and 64 bit sizes 34 * u : unsigned 35 * s : signed 36 * 37 * size is: 38 * b: 8 bits 39 * w: 16 bits 40 * l: 32 bits 41 * q: 64 bits 42 * 43 * mmusuffix is one of the generic suffixes "data" or "code", or 44 * (for softmmu configs) a target-specific MMU mode suffix as defined 45 * in target cpu.h. 46 */ 47 #ifndef CPU_LDST_H 48 #define CPU_LDST_H 49 50 #if defined(CONFIG_USER_ONLY) 51 /* sparc32plus has 64bit long but 32bit space address 52 * this can make bad result with g2h() and h2g() 53 */ 54 #if TARGET_VIRT_ADDR_SPACE_BITS <= 32 55 typedef uint32_t abi_ptr; 56 #define TARGET_ABI_FMT_ptr "%x" 57 #else 58 typedef uint64_t abi_ptr; 59 #define TARGET_ABI_FMT_ptr "%"PRIx64 60 #endif 61 62 /* All direct uses of g2h and h2g need to go away for usermode softmmu. */ 63 #define g2h(x) ((void *)((unsigned long)(abi_ptr)(x) + guest_base)) 64 65 #if HOST_LONG_BITS <= TARGET_VIRT_ADDR_SPACE_BITS 66 #define guest_addr_valid(x) (1) 67 #else 68 #define guest_addr_valid(x) ((x) <= GUEST_ADDR_MAX) 69 #endif 70 #define h2g_valid(x) guest_addr_valid((unsigned long)(x) - guest_base) 71 72 static inline int guest_range_valid(unsigned long start, unsigned long len) 73 { 74 return len - 1 <= GUEST_ADDR_MAX && start <= GUEST_ADDR_MAX - len + 1; 75 } 76 77 #define h2g_nocheck(x) ({ \ 78 unsigned long __ret = (unsigned long)(x) - guest_base; \ 79 (abi_ptr)__ret; \ 80 }) 81 82 #define h2g(x) ({ \ 83 /* Check if given address fits target address space */ \ 84 assert(h2g_valid(x)); \ 85 h2g_nocheck(x); \ 86 }) 87 #else 88 typedef target_ulong abi_ptr; 89 #define TARGET_ABI_FMT_ptr TARGET_ABI_FMT_lx 90 #endif 91 92 #if defined(CONFIG_USER_ONLY) 93 94 extern __thread uintptr_t helper_retaddr; 95 96 static inline void set_helper_retaddr(uintptr_t ra) 97 { 98 helper_retaddr = ra; 99 /* 100 * Ensure that this write is visible to the SIGSEGV handler that 101 * may be invoked due to a subsequent invalid memory operation. 102 */ 103 signal_barrier(); 104 } 105 106 static inline void clear_helper_retaddr(void) 107 { 108 /* 109 * Ensure that previous memory operations have succeeded before 110 * removing the data visible to the signal handler. 111 */ 112 signal_barrier(); 113 helper_retaddr = 0; 114 } 115 116 /* In user-only mode we provide only the _code and _data accessors. */ 117 118 #define MEMSUFFIX _data 119 #define DATA_SIZE 1 120 #include "exec/cpu_ldst_useronly_template.h" 121 122 #define DATA_SIZE 2 123 #include "exec/cpu_ldst_useronly_template.h" 124 125 #define DATA_SIZE 4 126 #include "exec/cpu_ldst_useronly_template.h" 127 128 #define DATA_SIZE 8 129 #include "exec/cpu_ldst_useronly_template.h" 130 #undef MEMSUFFIX 131 132 /* 133 * Code access is deprecated in favour of translator_ld* functions 134 * (see translator.h). However there are still users that need to 135 * converted so for now these stay. 136 */ 137 #define MEMSUFFIX _code 138 #define CODE_ACCESS 139 #define DATA_SIZE 1 140 #include "exec/cpu_ldst_useronly_template.h" 141 142 #define DATA_SIZE 2 143 #include "exec/cpu_ldst_useronly_template.h" 144 145 #define DATA_SIZE 4 146 #include "exec/cpu_ldst_useronly_template.h" 147 148 #define DATA_SIZE 8 149 #include "exec/cpu_ldst_useronly_template.h" 150 #undef MEMSUFFIX 151 #undef CODE_ACCESS 152 153 #else 154 155 /* Needed for TCG_OVERSIZED_GUEST */ 156 #include "tcg.h" 157 158 static inline target_ulong tlb_addr_write(const CPUTLBEntry *entry) 159 { 160 #if TCG_OVERSIZED_GUEST 161 return entry->addr_write; 162 #else 163 return atomic_read(&entry->addr_write); 164 #endif 165 } 166 167 /* Find the TLB index corresponding to the mmu_idx + address pair. */ 168 static inline uintptr_t tlb_index(CPUArchState *env, uintptr_t mmu_idx, 169 target_ulong addr) 170 { 171 uintptr_t size_mask = env_tlb(env)->f[mmu_idx].mask >> CPU_TLB_ENTRY_BITS; 172 173 return (addr >> TARGET_PAGE_BITS) & size_mask; 174 } 175 176 static inline size_t tlb_n_entries(CPUArchState *env, uintptr_t mmu_idx) 177 { 178 return (env_tlb(env)->f[mmu_idx].mask >> CPU_TLB_ENTRY_BITS) + 1; 179 } 180 181 /* Find the TLB entry corresponding to the mmu_idx + address pair. */ 182 static inline CPUTLBEntry *tlb_entry(CPUArchState *env, uintptr_t mmu_idx, 183 target_ulong addr) 184 { 185 return &env_tlb(env)->f[mmu_idx].table[tlb_index(env, mmu_idx, addr)]; 186 } 187 188 uint32_t cpu_ldub_mmuidx_ra(CPUArchState *env, abi_ptr addr, 189 int mmu_idx, uintptr_t ra); 190 uint32_t cpu_lduw_mmuidx_ra(CPUArchState *env, abi_ptr addr, 191 int mmu_idx, uintptr_t ra); 192 uint32_t cpu_ldl_mmuidx_ra(CPUArchState *env, abi_ptr addr, 193 int mmu_idx, uintptr_t ra); 194 uint64_t cpu_ldq_mmuidx_ra(CPUArchState *env, abi_ptr addr, 195 int mmu_idx, uintptr_t ra); 196 197 int cpu_ldsb_mmuidx_ra(CPUArchState *env, abi_ptr addr, 198 int mmu_idx, uintptr_t ra); 199 int cpu_ldsw_mmuidx_ra(CPUArchState *env, abi_ptr addr, 200 int mmu_idx, uintptr_t ra); 201 202 void cpu_stb_mmuidx_ra(CPUArchState *env, abi_ptr addr, uint32_t val, 203 int mmu_idx, uintptr_t retaddr); 204 void cpu_stw_mmuidx_ra(CPUArchState *env, abi_ptr addr, uint32_t val, 205 int mmu_idx, uintptr_t retaddr); 206 void cpu_stl_mmuidx_ra(CPUArchState *env, abi_ptr addr, uint32_t val, 207 int mmu_idx, uintptr_t retaddr); 208 void cpu_stq_mmuidx_ra(CPUArchState *env, abi_ptr addr, uint64_t val, 209 int mmu_idx, uintptr_t retaddr); 210 211 #ifdef MMU_MODE0_SUFFIX 212 #define CPU_MMU_INDEX 0 213 #define MEMSUFFIX MMU_MODE0_SUFFIX 214 #define DATA_SIZE 1 215 #include "exec/cpu_ldst_template.h" 216 217 #define DATA_SIZE 2 218 #include "exec/cpu_ldst_template.h" 219 220 #define DATA_SIZE 4 221 #include "exec/cpu_ldst_template.h" 222 223 #define DATA_SIZE 8 224 #include "exec/cpu_ldst_template.h" 225 #undef CPU_MMU_INDEX 226 #undef MEMSUFFIX 227 #endif 228 229 #if (NB_MMU_MODES >= 2) && defined(MMU_MODE1_SUFFIX) 230 #define CPU_MMU_INDEX 1 231 #define MEMSUFFIX MMU_MODE1_SUFFIX 232 #define DATA_SIZE 1 233 #include "exec/cpu_ldst_template.h" 234 235 #define DATA_SIZE 2 236 #include "exec/cpu_ldst_template.h" 237 238 #define DATA_SIZE 4 239 #include "exec/cpu_ldst_template.h" 240 241 #define DATA_SIZE 8 242 #include "exec/cpu_ldst_template.h" 243 #undef CPU_MMU_INDEX 244 #undef MEMSUFFIX 245 #endif 246 247 #if (NB_MMU_MODES >= 3) && defined(MMU_MODE2_SUFFIX) 248 249 #define CPU_MMU_INDEX 2 250 #define MEMSUFFIX MMU_MODE2_SUFFIX 251 #define DATA_SIZE 1 252 #include "exec/cpu_ldst_template.h" 253 254 #define DATA_SIZE 2 255 #include "exec/cpu_ldst_template.h" 256 257 #define DATA_SIZE 4 258 #include "exec/cpu_ldst_template.h" 259 260 #define DATA_SIZE 8 261 #include "exec/cpu_ldst_template.h" 262 #undef CPU_MMU_INDEX 263 #undef MEMSUFFIX 264 #endif /* (NB_MMU_MODES >= 3) */ 265 266 #if (NB_MMU_MODES >= 4) && defined(MMU_MODE3_SUFFIX) 267 268 #define CPU_MMU_INDEX 3 269 #define MEMSUFFIX MMU_MODE3_SUFFIX 270 #define DATA_SIZE 1 271 #include "exec/cpu_ldst_template.h" 272 273 #define DATA_SIZE 2 274 #include "exec/cpu_ldst_template.h" 275 276 #define DATA_SIZE 4 277 #include "exec/cpu_ldst_template.h" 278 279 #define DATA_SIZE 8 280 #include "exec/cpu_ldst_template.h" 281 #undef CPU_MMU_INDEX 282 #undef MEMSUFFIX 283 #endif /* (NB_MMU_MODES >= 4) */ 284 285 #if (NB_MMU_MODES >= 5) && defined(MMU_MODE4_SUFFIX) 286 287 #define CPU_MMU_INDEX 4 288 #define MEMSUFFIX MMU_MODE4_SUFFIX 289 #define DATA_SIZE 1 290 #include "exec/cpu_ldst_template.h" 291 292 #define DATA_SIZE 2 293 #include "exec/cpu_ldst_template.h" 294 295 #define DATA_SIZE 4 296 #include "exec/cpu_ldst_template.h" 297 298 #define DATA_SIZE 8 299 #include "exec/cpu_ldst_template.h" 300 #undef CPU_MMU_INDEX 301 #undef MEMSUFFIX 302 #endif /* (NB_MMU_MODES >= 5) */ 303 304 #if (NB_MMU_MODES >= 6) && defined(MMU_MODE5_SUFFIX) 305 306 #define CPU_MMU_INDEX 5 307 #define MEMSUFFIX MMU_MODE5_SUFFIX 308 #define DATA_SIZE 1 309 #include "exec/cpu_ldst_template.h" 310 311 #define DATA_SIZE 2 312 #include "exec/cpu_ldst_template.h" 313 314 #define DATA_SIZE 4 315 #include "exec/cpu_ldst_template.h" 316 317 #define DATA_SIZE 8 318 #include "exec/cpu_ldst_template.h" 319 #undef CPU_MMU_INDEX 320 #undef MEMSUFFIX 321 #endif /* (NB_MMU_MODES >= 6) */ 322 323 #if (NB_MMU_MODES >= 7) && defined(MMU_MODE6_SUFFIX) 324 325 #define CPU_MMU_INDEX 6 326 #define MEMSUFFIX MMU_MODE6_SUFFIX 327 #define DATA_SIZE 1 328 #include "exec/cpu_ldst_template.h" 329 330 #define DATA_SIZE 2 331 #include "exec/cpu_ldst_template.h" 332 333 #define DATA_SIZE 4 334 #include "exec/cpu_ldst_template.h" 335 336 #define DATA_SIZE 8 337 #include "exec/cpu_ldst_template.h" 338 #undef CPU_MMU_INDEX 339 #undef MEMSUFFIX 340 #endif /* (NB_MMU_MODES >= 7) */ 341 342 #if (NB_MMU_MODES >= 8) && defined(MMU_MODE7_SUFFIX) 343 344 #define CPU_MMU_INDEX 7 345 #define MEMSUFFIX MMU_MODE7_SUFFIX 346 #define DATA_SIZE 1 347 #include "exec/cpu_ldst_template.h" 348 349 #define DATA_SIZE 2 350 #include "exec/cpu_ldst_template.h" 351 352 #define DATA_SIZE 4 353 #include "exec/cpu_ldst_template.h" 354 355 #define DATA_SIZE 8 356 #include "exec/cpu_ldst_template.h" 357 #undef CPU_MMU_INDEX 358 #undef MEMSUFFIX 359 #endif /* (NB_MMU_MODES >= 8) */ 360 361 #if (NB_MMU_MODES >= 9) && defined(MMU_MODE8_SUFFIX) 362 363 #define CPU_MMU_INDEX 8 364 #define MEMSUFFIX MMU_MODE8_SUFFIX 365 #define DATA_SIZE 1 366 #include "exec/cpu_ldst_template.h" 367 368 #define DATA_SIZE 2 369 #include "exec/cpu_ldst_template.h" 370 371 #define DATA_SIZE 4 372 #include "exec/cpu_ldst_template.h" 373 374 #define DATA_SIZE 8 375 #include "exec/cpu_ldst_template.h" 376 #undef CPU_MMU_INDEX 377 #undef MEMSUFFIX 378 #endif /* (NB_MMU_MODES >= 9) */ 379 380 #if (NB_MMU_MODES >= 10) && defined(MMU_MODE9_SUFFIX) 381 382 #define CPU_MMU_INDEX 9 383 #define MEMSUFFIX MMU_MODE9_SUFFIX 384 #define DATA_SIZE 1 385 #include "exec/cpu_ldst_template.h" 386 387 #define DATA_SIZE 2 388 #include "exec/cpu_ldst_template.h" 389 390 #define DATA_SIZE 4 391 #include "exec/cpu_ldst_template.h" 392 393 #define DATA_SIZE 8 394 #include "exec/cpu_ldst_template.h" 395 #undef CPU_MMU_INDEX 396 #undef MEMSUFFIX 397 #endif /* (NB_MMU_MODES >= 10) */ 398 399 #if (NB_MMU_MODES >= 11) && defined(MMU_MODE10_SUFFIX) 400 401 #define CPU_MMU_INDEX 10 402 #define MEMSUFFIX MMU_MODE10_SUFFIX 403 #define DATA_SIZE 1 404 #include "exec/cpu_ldst_template.h" 405 406 #define DATA_SIZE 2 407 #include "exec/cpu_ldst_template.h" 408 409 #define DATA_SIZE 4 410 #include "exec/cpu_ldst_template.h" 411 412 #define DATA_SIZE 8 413 #include "exec/cpu_ldst_template.h" 414 #undef CPU_MMU_INDEX 415 #undef MEMSUFFIX 416 #endif /* (NB_MMU_MODES >= 11) */ 417 418 #if (NB_MMU_MODES >= 12) && defined(MMU_MODE11_SUFFIX) 419 420 #define CPU_MMU_INDEX 11 421 #define MEMSUFFIX MMU_MODE11_SUFFIX 422 #define DATA_SIZE 1 423 #include "exec/cpu_ldst_template.h" 424 425 #define DATA_SIZE 2 426 #include "exec/cpu_ldst_template.h" 427 428 #define DATA_SIZE 4 429 #include "exec/cpu_ldst_template.h" 430 431 #define DATA_SIZE 8 432 #include "exec/cpu_ldst_template.h" 433 #undef CPU_MMU_INDEX 434 #undef MEMSUFFIX 435 #endif /* (NB_MMU_MODES >= 12) */ 436 437 #if (NB_MMU_MODES > 12) 438 #error "NB_MMU_MODES > 12 is not supported for now" 439 #endif /* (NB_MMU_MODES > 12) */ 440 441 /* these access are slower, they must be as rare as possible */ 442 #define CPU_MMU_INDEX (cpu_mmu_index(env, false)) 443 #define MEMSUFFIX _data 444 #define DATA_SIZE 1 445 #include "exec/cpu_ldst_template.h" 446 447 #define DATA_SIZE 2 448 #include "exec/cpu_ldst_template.h" 449 450 #define DATA_SIZE 4 451 #include "exec/cpu_ldst_template.h" 452 453 #define DATA_SIZE 8 454 #include "exec/cpu_ldst_template.h" 455 #undef CPU_MMU_INDEX 456 #undef MEMSUFFIX 457 458 /* 459 * Code access is deprecated in favour of translator_ld* functions 460 * (see translator.h). However there are still users that need to 461 * converted so for now these stay. 462 */ 463 464 #define CPU_MMU_INDEX (cpu_mmu_index(env, true)) 465 #define MEMSUFFIX _code 466 #define SOFTMMU_CODE_ACCESS 467 468 #define DATA_SIZE 1 469 #include "exec/cpu_ldst_template.h" 470 471 #define DATA_SIZE 2 472 #include "exec/cpu_ldst_template.h" 473 474 #define DATA_SIZE 4 475 #include "exec/cpu_ldst_template.h" 476 477 #define DATA_SIZE 8 478 #include "exec/cpu_ldst_template.h" 479 480 #undef CPU_MMU_INDEX 481 #undef MEMSUFFIX 482 #undef SOFTMMU_CODE_ACCESS 483 484 #endif /* defined(CONFIG_USER_ONLY) */ 485 486 /** 487 * tlb_vaddr_to_host: 488 * @env: CPUArchState 489 * @addr: guest virtual address to look up 490 * @access_type: 0 for read, 1 for write, 2 for execute 491 * @mmu_idx: MMU index to use for lookup 492 * 493 * Look up the specified guest virtual index in the TCG softmmu TLB. 494 * If we can translate a host virtual address suitable for direct RAM 495 * access, without causing a guest exception, then return it. 496 * Otherwise (TLB entry is for an I/O access, guest software 497 * TLB fill required, etc) return NULL. 498 */ 499 #ifdef CONFIG_USER_ONLY 500 static inline void *tlb_vaddr_to_host(CPUArchState *env, abi_ptr addr, 501 MMUAccessType access_type, int mmu_idx) 502 { 503 return g2h(addr); 504 } 505 #else 506 void *tlb_vaddr_to_host(CPUArchState *env, abi_ptr addr, 507 MMUAccessType access_type, int mmu_idx); 508 #endif 509 510 #endif /* CPU_LDST_H */ 511