1022c62cbSPaolo Bonzini /* 2022c62cbSPaolo Bonzini * common defines for all CPUs 3022c62cbSPaolo Bonzini * 4022c62cbSPaolo Bonzini * Copyright (c) 2003 Fabrice Bellard 5022c62cbSPaolo Bonzini * 6022c62cbSPaolo Bonzini * This library is free software; you can redistribute it and/or 7022c62cbSPaolo Bonzini * modify it under the terms of the GNU Lesser General Public 8022c62cbSPaolo Bonzini * License as published by the Free Software Foundation; either 9022c62cbSPaolo Bonzini * version 2 of the License, or (at your option) any later version. 10022c62cbSPaolo Bonzini * 11022c62cbSPaolo Bonzini * This library is distributed in the hope that it will be useful, 12022c62cbSPaolo Bonzini * but WITHOUT ANY WARRANTY; without even the implied warranty of 13022c62cbSPaolo Bonzini * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14022c62cbSPaolo Bonzini * Lesser General Public License for more details. 15022c62cbSPaolo Bonzini * 16022c62cbSPaolo Bonzini * You should have received a copy of the GNU Lesser General Public 17022c62cbSPaolo Bonzini * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18022c62cbSPaolo Bonzini */ 19022c62cbSPaolo Bonzini #ifndef CPU_DEFS_H 20022c62cbSPaolo Bonzini #define CPU_DEFS_H 21022c62cbSPaolo Bonzini 22022c62cbSPaolo Bonzini #ifndef NEED_CPU_H 23022c62cbSPaolo Bonzini #error cpu.h included from common code 24022c62cbSPaolo Bonzini #endif 25022c62cbSPaolo Bonzini 2687776ab7SPaolo Bonzini #include "qemu/host-utils.h" 2771aec354SEmilio G. Cota #include "qemu/thread.h" 281de7afc9SPaolo Bonzini #include "qemu/queue.h" 29b11ec7f2SYang Zhong #ifdef CONFIG_TCG 301de29aefSPaolo Bonzini #include "tcg-target.h" 31b11ec7f2SYang Zhong #endif 32ce927ed9SAndreas Färber #ifndef CONFIG_USER_ONLY 33022c62cbSPaolo Bonzini #include "exec/hwaddr.h" 34ce927ed9SAndreas Färber #endif 35fadc1cbeSPeter Maydell #include "exec/memattrs.h" 36022c62cbSPaolo Bonzini 37022c62cbSPaolo Bonzini #ifndef TARGET_LONG_BITS 38022c62cbSPaolo Bonzini #error TARGET_LONG_BITS must be defined before including this header 39022c62cbSPaolo Bonzini #endif 40022c62cbSPaolo Bonzini 41022c62cbSPaolo Bonzini #define TARGET_LONG_SIZE (TARGET_LONG_BITS / 8) 42022c62cbSPaolo Bonzini 43022c62cbSPaolo Bonzini /* target_ulong is the type of a virtual address */ 44022c62cbSPaolo Bonzini #if TARGET_LONG_SIZE == 4 456cfd9b52SPaolo Bonzini typedef int32_t target_long; 466cfd9b52SPaolo Bonzini typedef uint32_t target_ulong; 47022c62cbSPaolo Bonzini #define TARGET_FMT_lx "%08x" 48022c62cbSPaolo Bonzini #define TARGET_FMT_ld "%d" 49022c62cbSPaolo Bonzini #define TARGET_FMT_lu "%u" 50022c62cbSPaolo Bonzini #elif TARGET_LONG_SIZE == 8 516cfd9b52SPaolo Bonzini typedef int64_t target_long; 526cfd9b52SPaolo Bonzini typedef uint64_t target_ulong; 53022c62cbSPaolo Bonzini #define TARGET_FMT_lx "%016" PRIx64 54022c62cbSPaolo Bonzini #define TARGET_FMT_ld "%" PRId64 55022c62cbSPaolo Bonzini #define TARGET_FMT_lu "%" PRIu64 56022c62cbSPaolo Bonzini #else 57022c62cbSPaolo Bonzini #error TARGET_LONG_SIZE undefined 58022c62cbSPaolo Bonzini #endif 59022c62cbSPaolo Bonzini 60b11ec7f2SYang Zhong #if !defined(CONFIG_USER_ONLY) && defined(CONFIG_TCG) 6188e89a57SXin Tong /* use a fully associative victim tlb of 8 entries */ 6288e89a57SXin Tong #define CPU_VTLB_SIZE 8 63022c62cbSPaolo Bonzini 64022c62cbSPaolo Bonzini #if HOST_LONG_BITS == 32 && TARGET_LONG_BITS == 32 65022c62cbSPaolo Bonzini #define CPU_TLB_ENTRY_BITS 4 66022c62cbSPaolo Bonzini #else 67022c62cbSPaolo Bonzini #define CPU_TLB_ENTRY_BITS 5 68022c62cbSPaolo Bonzini #endif 69022c62cbSPaolo Bonzini 70*86e1eff8SEmilio G. Cota #if TCG_TARGET_IMPLEMENTS_DYN_TLB 71*86e1eff8SEmilio G. Cota #define CPU_TLB_DYN_MIN_BITS 6 72*86e1eff8SEmilio G. Cota #define CPU_TLB_DYN_DEFAULT_BITS 8 73*86e1eff8SEmilio G. Cota 74*86e1eff8SEmilio G. Cota 75*86e1eff8SEmilio G. Cota # if HOST_LONG_BITS == 32 76*86e1eff8SEmilio G. Cota /* Make sure we do not require a double-word shift for the TLB load */ 77*86e1eff8SEmilio G. Cota # define CPU_TLB_DYN_MAX_BITS (32 - TARGET_PAGE_BITS) 78*86e1eff8SEmilio G. Cota # else /* HOST_LONG_BITS == 64 */ 79*86e1eff8SEmilio G. Cota /* 80*86e1eff8SEmilio G. Cota * Assuming TARGET_PAGE_BITS==12, with 2**22 entries we can cover 2**(22+12) == 81*86e1eff8SEmilio G. Cota * 2**34 == 16G of address space. This is roughly what one would expect a 82*86e1eff8SEmilio G. Cota * TLB to cover in a modern (as of 2018) x86_64 CPU. For instance, Intel 83*86e1eff8SEmilio G. Cota * Skylake's Level-2 STLB has 16 1G entries. 84*86e1eff8SEmilio G. Cota * Also, make sure we do not size the TLB past the guest's address space. 85*86e1eff8SEmilio G. Cota */ 86*86e1eff8SEmilio G. Cota # define CPU_TLB_DYN_MAX_BITS \ 87*86e1eff8SEmilio G. Cota MIN(22, TARGET_VIRT_ADDR_SPACE_BITS - TARGET_PAGE_BITS) 88*86e1eff8SEmilio G. Cota # endif 89*86e1eff8SEmilio G. Cota 90*86e1eff8SEmilio G. Cota #else /* !TCG_TARGET_IMPLEMENTS_DYN_TLB */ 91*86e1eff8SEmilio G. Cota 921de29aefSPaolo Bonzini /* TCG_TARGET_TLB_DISPLACEMENT_BITS is used in CPU_TLB_BITS to ensure that 931de29aefSPaolo Bonzini * the TLB is not unnecessarily small, but still small enough for the 941de29aefSPaolo Bonzini * TLB lookup instruction sequence used by the TCG target. 951de29aefSPaolo Bonzini * 961de29aefSPaolo Bonzini * TCG will have to generate an operand as large as the distance between 971de29aefSPaolo Bonzini * env and the tlb_table[NB_MMU_MODES - 1][0].addend. For simplicity, 981de29aefSPaolo Bonzini * the TCG targets just round everything up to the next power of two, and 991de29aefSPaolo Bonzini * count bits. This works because: 1) the size of each TLB is a largish 1001de29aefSPaolo Bonzini * power of two, 2) and because the limit of the displacement is really close 1011de29aefSPaolo Bonzini * to a power of two, 3) the offset of tlb_table[0][0] inside env is smaller 1021de29aefSPaolo Bonzini * than the size of a TLB. 1031de29aefSPaolo Bonzini * 1041de29aefSPaolo Bonzini * For example, the maximum displacement 0xFFF0 on PPC and MIPS, but TCG 1051de29aefSPaolo Bonzini * just says "the displacement is 16 bits". TCG_TARGET_TLB_DISPLACEMENT_BITS 1061de29aefSPaolo Bonzini * then ensures that tlb_table at least 0x8000 bytes large ("not unnecessarily 1071de29aefSPaolo Bonzini * small": 2^15). The operand then will come up smaller than 0xFFF0 without 1081de29aefSPaolo Bonzini * any particular care, because the TLB for a single MMU mode is larger than 1091de29aefSPaolo Bonzini * 0x10000-0xFFF0=16 bytes. In the end, the maximum value of the operand 1101de29aefSPaolo Bonzini * could be something like 0xC000 (the offset of the last TLB table) plus 1111de29aefSPaolo Bonzini * 0x18 (the offset of the addend field in each TLB entry) plus the offset 1121de29aefSPaolo Bonzini * of tlb_table inside env (which is non-trivial but not huge). 1131de29aefSPaolo Bonzini */ 1141de29aefSPaolo Bonzini #define CPU_TLB_BITS \ 1151de29aefSPaolo Bonzini MIN(8, \ 1161de29aefSPaolo Bonzini TCG_TARGET_TLB_DISPLACEMENT_BITS - CPU_TLB_ENTRY_BITS - \ 1171de29aefSPaolo Bonzini (NB_MMU_MODES <= 1 ? 0 : \ 1181de29aefSPaolo Bonzini NB_MMU_MODES <= 2 ? 1 : \ 1191de29aefSPaolo Bonzini NB_MMU_MODES <= 4 ? 2 : \ 1201de29aefSPaolo Bonzini NB_MMU_MODES <= 8 ? 3 : 4)) 1211de29aefSPaolo Bonzini 1221de29aefSPaolo Bonzini #define CPU_TLB_SIZE (1 << CPU_TLB_BITS) 123*86e1eff8SEmilio G. Cota #endif /* TCG_TARGET_IMPLEMENTS_DYN_TLB */ 1241de29aefSPaolo Bonzini 125022c62cbSPaolo Bonzini typedef struct CPUTLBEntry { 126022c62cbSPaolo Bonzini /* bit TARGET_LONG_BITS to TARGET_PAGE_BITS : virtual address 127022c62cbSPaolo Bonzini bit TARGET_PAGE_BITS-1..4 : Nonzero for accesses that should not 128022c62cbSPaolo Bonzini go directly to ram. 129022c62cbSPaolo Bonzini bit 3 : indicates that the entry is invalid 130022c62cbSPaolo Bonzini bit 2..0 : zero 131022c62cbSPaolo Bonzini */ 132b4a4b8d0SPeter Crosthwaite union { 133b4a4b8d0SPeter Crosthwaite struct { 134022c62cbSPaolo Bonzini target_ulong addr_read; 135022c62cbSPaolo Bonzini target_ulong addr_write; 136022c62cbSPaolo Bonzini target_ulong addr_code; 137022c62cbSPaolo Bonzini /* Addend to virtual address to get host address. IO accesses 138022c62cbSPaolo Bonzini use the corresponding iotlb value. */ 139022c62cbSPaolo Bonzini uintptr_t addend; 140b4a4b8d0SPeter Crosthwaite }; 141022c62cbSPaolo Bonzini /* padding to get a power of two size */ 142b4a4b8d0SPeter Crosthwaite uint8_t dummy[1 << CPU_TLB_ENTRY_BITS]; 143b4a4b8d0SPeter Crosthwaite }; 144022c62cbSPaolo Bonzini } CPUTLBEntry; 145022c62cbSPaolo Bonzini 146e85ef538SRichard Henderson QEMU_BUILD_BUG_ON(sizeof(CPUTLBEntry) != (1 << CPU_TLB_ENTRY_BITS)); 147022c62cbSPaolo Bonzini 148e469b22fSPeter Maydell /* The IOTLB is not accessed directly inline by generated TCG code, 149e469b22fSPeter Maydell * so the CPUIOTLBEntry layout is not as critical as that of the 150e469b22fSPeter Maydell * CPUTLBEntry. (This is also why we don't want to combine the two 151e469b22fSPeter Maydell * structs into one.) 152e469b22fSPeter Maydell */ 153e469b22fSPeter Maydell typedef struct CPUIOTLBEntry { 154ace41090SPeter Maydell /* 155ace41090SPeter Maydell * @addr contains: 156ace41090SPeter Maydell * - in the lower TARGET_PAGE_BITS, a physical section number 157ace41090SPeter Maydell * - with the lower TARGET_PAGE_BITS masked off, an offset which 158ace41090SPeter Maydell * must be added to the virtual address to obtain: 159ace41090SPeter Maydell * + the ram_addr_t of the target RAM (if the physical section 160ace41090SPeter Maydell * number is PHYS_SECTION_NOTDIRTY or PHYS_SECTION_ROM) 161ace41090SPeter Maydell * + the offset within the target MemoryRegion (otherwise) 162ace41090SPeter Maydell */ 163e469b22fSPeter Maydell hwaddr addr; 164fadc1cbeSPeter Maydell MemTxAttrs attrs; 165e469b22fSPeter Maydell } CPUIOTLBEntry; 166e469b22fSPeter Maydell 167*86e1eff8SEmilio G. Cota /** 168*86e1eff8SEmilio G. Cota * struct CPUTLBWindow 169*86e1eff8SEmilio G. Cota * @begin_ns: host time (in ns) at the beginning of the time window 170*86e1eff8SEmilio G. Cota * @max_entries: maximum number of entries observed in the window 171*86e1eff8SEmilio G. Cota * 172*86e1eff8SEmilio G. Cota * See also: tlb_mmu_resize_locked() 173*86e1eff8SEmilio G. Cota */ 174*86e1eff8SEmilio G. Cota typedef struct CPUTLBWindow { 175*86e1eff8SEmilio G. Cota int64_t begin_ns; 176*86e1eff8SEmilio G. Cota size_t max_entries; 177*86e1eff8SEmilio G. Cota } CPUTLBWindow; 178*86e1eff8SEmilio G. Cota 1791308e026SRichard Henderson typedef struct CPUTLBDesc { 1801308e026SRichard Henderson /* 1811308e026SRichard Henderson * Describe a region covering all of the large pages allocated 1821308e026SRichard Henderson * into the tlb. When any page within this region is flushed, 1831308e026SRichard Henderson * we must flush the entire tlb. The region is matched if 1841308e026SRichard Henderson * (addr & large_page_mask) == large_page_addr. 1851308e026SRichard Henderson */ 1861308e026SRichard Henderson target_ulong large_page_addr; 1871308e026SRichard Henderson target_ulong large_page_mask; 188d5363e58SRichard Henderson /* The next index to use in the tlb victim table. */ 189d5363e58SRichard Henderson size_t vindex; 190*86e1eff8SEmilio G. Cota #if TCG_TARGET_IMPLEMENTS_DYN_TLB 191*86e1eff8SEmilio G. Cota CPUTLBWindow window; 192*86e1eff8SEmilio G. Cota size_t n_used_entries; 193*86e1eff8SEmilio G. Cota #endif 1941308e026SRichard Henderson } CPUTLBDesc; 1951308e026SRichard Henderson 19653d28455SRichard Henderson /* 19753d28455SRichard Henderson * Data elements that are shared between all MMU modes. 19853d28455SRichard Henderson */ 19953d28455SRichard Henderson typedef struct CPUTLBCommon { 20060a2ad7dSRichard Henderson /* Serialize updates to tlb_table and tlb_v_table, and others as noted. */ 20153d28455SRichard Henderson QemuSpin lock; 20260a2ad7dSRichard Henderson /* 2033d1523ceSRichard Henderson * Within dirty, for each bit N, modifications have been made to 2043d1523ceSRichard Henderson * mmu_idx N since the last time that mmu_idx was flushed. 2053d1523ceSRichard Henderson * Protected by tlb_c.lock. 2063d1523ceSRichard Henderson */ 2073d1523ceSRichard Henderson uint16_t dirty; 208e09de0a2SRichard Henderson /* 209e09de0a2SRichard Henderson * Statistics. These are not lock protected, but are read and 210e09de0a2SRichard Henderson * written atomically. This allows the monitor to print a snapshot 211e09de0a2SRichard Henderson * of the stats without interfering with the cpu. 212e09de0a2SRichard Henderson */ 213e09de0a2SRichard Henderson size_t full_flush_count; 214e09de0a2SRichard Henderson size_t part_flush_count; 215e09de0a2SRichard Henderson size_t elide_flush_count; 21653d28455SRichard Henderson } CPUTLBCommon; 21753d28455SRichard Henderson 218*86e1eff8SEmilio G. Cota #if TCG_TARGET_IMPLEMENTS_DYN_TLB 219*86e1eff8SEmilio G. Cota # define CPU_TLB \ 220*86e1eff8SEmilio G. Cota /* tlb_mask[i] contains (n_entries - 1) << CPU_TLB_ENTRY_BITS */ \ 221*86e1eff8SEmilio G. Cota uintptr_t tlb_mask[NB_MMU_MODES]; \ 222*86e1eff8SEmilio G. Cota CPUTLBEntry *tlb_table[NB_MMU_MODES]; 223*86e1eff8SEmilio G. Cota # define CPU_IOTLB \ 224*86e1eff8SEmilio G. Cota CPUIOTLBEntry *iotlb[NB_MMU_MODES]; 225*86e1eff8SEmilio G. Cota #else 226*86e1eff8SEmilio G. Cota # define CPU_TLB \ 227*86e1eff8SEmilio G. Cota CPUTLBEntry tlb_table[NB_MMU_MODES][CPU_TLB_SIZE]; 228*86e1eff8SEmilio G. Cota # define CPU_IOTLB \ 229*86e1eff8SEmilio G. Cota CPUIOTLBEntry iotlb[NB_MMU_MODES][CPU_TLB_SIZE]; 230*86e1eff8SEmilio G. Cota #endif 231*86e1eff8SEmilio G. Cota 23253d28455SRichard Henderson /* 23353d28455SRichard Henderson * The meaning of each of the MMU modes is defined in the target code. 23453d28455SRichard Henderson * Note that NB_MMU_MODES is not yet defined; we can only reference it 23553d28455SRichard Henderson * within preprocessor defines that will be expanded later. 23653d28455SRichard Henderson */ 237022c62cbSPaolo Bonzini #define CPU_COMMON_TLB \ 23853d28455SRichard Henderson CPUTLBCommon tlb_c; \ 2391308e026SRichard Henderson CPUTLBDesc tlb_d[NB_MMU_MODES]; \ 240*86e1eff8SEmilio G. Cota CPU_TLB \ 24188e89a57SXin Tong CPUTLBEntry tlb_v_table[NB_MMU_MODES][CPU_VTLB_SIZE]; \ 242*86e1eff8SEmilio G. Cota CPU_IOTLB \ 243e09de0a2SRichard Henderson CPUIOTLBEntry iotlb_v[NB_MMU_MODES][CPU_VTLB_SIZE]; 244022c62cbSPaolo Bonzini 245022c62cbSPaolo Bonzini #else 246022c62cbSPaolo Bonzini 247022c62cbSPaolo Bonzini #define CPU_COMMON_TLB 248022c62cbSPaolo Bonzini 249022c62cbSPaolo Bonzini #endif 250022c62cbSPaolo Bonzini 251022c62cbSPaolo Bonzini 252022c62cbSPaolo Bonzini #define CPU_COMMON \ 253022c62cbSPaolo Bonzini /* soft mmu support */ \ 254022c62cbSPaolo Bonzini CPU_COMMON_TLB \ 255022c62cbSPaolo Bonzini 256022c62cbSPaolo Bonzini #endif 257