xref: /qemu/include/exec/cpu-defs.h (revision 79e4208506651660b866f536616a5f8f3175f909)
1ab93bbe2Sbellard /*
2ab93bbe2Sbellard  * common defines for all CPUs
3ab93bbe2Sbellard  *
4ab93bbe2Sbellard  * Copyright (c) 2003 Fabrice Bellard
5ab93bbe2Sbellard  *
6ab93bbe2Sbellard  * This library is free software; you can redistribute it and/or
7ab93bbe2Sbellard  * modify it under the terms of the GNU Lesser General Public
8ab93bbe2Sbellard  * License as published by the Free Software Foundation; either
9ab93bbe2Sbellard  * version 2 of the License, or (at your option) any later version.
10ab93bbe2Sbellard  *
11ab93bbe2Sbellard  * This library is distributed in the hope that it will be useful,
12ab93bbe2Sbellard  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13ab93bbe2Sbellard  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14ab93bbe2Sbellard  * Lesser General Public License for more details.
15ab93bbe2Sbellard  *
16ab93bbe2Sbellard  * You should have received a copy of the GNU Lesser General Public
178167ee88SBlue Swirl  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18ab93bbe2Sbellard  */
19ab93bbe2Sbellard #ifndef CPU_DEFS_H
20ab93bbe2Sbellard #define CPU_DEFS_H
21ab93bbe2Sbellard 
2287ecb68bSpbrook #ifndef NEED_CPU_H
2387ecb68bSpbrook #error cpu.h included from common code
2487ecb68bSpbrook #endif
2587ecb68bSpbrook 
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"
36ab93bbe2Sbellard 
3735b66fc4Sbellard #ifndef TARGET_LONG_BITS
3835b66fc4Sbellard #error TARGET_LONG_BITS must be defined before including this header
3935b66fc4Sbellard #endif
4035b66fc4Sbellard 
4135b66fc4Sbellard #define TARGET_LONG_SIZE (TARGET_LONG_BITS / 8)
4235b66fc4Sbellard 
43ab6d960fSbellard /* target_ulong is the type of a virtual address */
4435b66fc4Sbellard #if TARGET_LONG_SIZE == 4
456cfd9b52SPaolo Bonzini typedef int32_t target_long;
466cfd9b52SPaolo Bonzini typedef uint32_t target_ulong;
47c27004ecSbellard #define TARGET_FMT_lx "%08x"
48b62b461bSj_mayer #define TARGET_FMT_ld "%d"
4971c8b8fdSj_mayer #define TARGET_FMT_lu "%u"
5035b66fc4Sbellard #elif TARGET_LONG_SIZE == 8
516cfd9b52SPaolo Bonzini typedef int64_t target_long;
526cfd9b52SPaolo Bonzini typedef uint64_t target_ulong;
5326a76461Sbellard #define TARGET_FMT_lx "%016" PRIx64
54b62b461bSj_mayer #define TARGET_FMT_ld "%" PRId64
5571c8b8fdSj_mayer #define TARGET_FMT_lu "%" PRIu64
5635b66fc4Sbellard #else
5735b66fc4Sbellard #error TARGET_LONG_SIZE undefined
5835b66fc4Sbellard #endif
5935b66fc4Sbellard 
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
63ab93bbe2Sbellard 
64355b1943SPaul Brook #if HOST_LONG_BITS == 32 && TARGET_LONG_BITS == 32
65d656469fSbellard #define CPU_TLB_ENTRY_BITS 4
66d656469fSbellard #else
67d656469fSbellard #define CPU_TLB_ENTRY_BITS 5
68d656469fSbellard #endif
69d656469fSbellard 
7086e1eff8SEmilio G. Cota #define CPU_TLB_DYN_MIN_BITS 6
7186e1eff8SEmilio G. Cota #define CPU_TLB_DYN_DEFAULT_BITS 8
7286e1eff8SEmilio G. Cota 
7386e1eff8SEmilio G. Cota # if HOST_LONG_BITS == 32
7486e1eff8SEmilio G. Cota /* Make sure we do not require a double-word shift for the TLB load */
7586e1eff8SEmilio G. Cota #  define CPU_TLB_DYN_MAX_BITS (32 - TARGET_PAGE_BITS)
7686e1eff8SEmilio G. Cota # else /* HOST_LONG_BITS == 64 */
7786e1eff8SEmilio G. Cota /*
7886e1eff8SEmilio G. Cota  * Assuming TARGET_PAGE_BITS==12, with 2**22 entries we can cover 2**(22+12) ==
7986e1eff8SEmilio G. Cota  * 2**34 == 16G of address space. This is roughly what one would expect a
8086e1eff8SEmilio G. Cota  * TLB to cover in a modern (as of 2018) x86_64 CPU. For instance, Intel
8186e1eff8SEmilio G. Cota  * Skylake's Level-2 STLB has 16 1G entries.
8286e1eff8SEmilio G. Cota  * Also, make sure we do not size the TLB past the guest's address space.
8386e1eff8SEmilio G. Cota  */
8486e1eff8SEmilio G. Cota #  define CPU_TLB_DYN_MAX_BITS                                  \
8586e1eff8SEmilio G. Cota     MIN(22, TARGET_VIRT_ADDR_SPACE_BITS - TARGET_PAGE_BITS)
8686e1eff8SEmilio G. Cota # endif
8786e1eff8SEmilio G. Cota 
88ab93bbe2Sbellard typedef struct CPUTLBEntry {
890f459d16Spbrook     /* bit TARGET_LONG_BITS to TARGET_PAGE_BITS : virtual address
900f459d16Spbrook        bit TARGET_PAGE_BITS-1..4  : Nonzero for accesses that should not
910f459d16Spbrook                                     go directly to ram.
92db8d7466Sbellard        bit 3                      : indicates that the entry is invalid
93db8d7466Sbellard        bit 2..0                   : zero
94db8d7466Sbellard     */
95b4a4b8d0SPeter Crosthwaite     union {
96b4a4b8d0SPeter Crosthwaite         struct {
9784b7b8e7Sbellard             target_ulong addr_read;
9884b7b8e7Sbellard             target_ulong addr_write;
9984b7b8e7Sbellard             target_ulong addr_code;
100355b1943SPaul Brook             /* Addend to virtual address to get host address.  IO accesses
101ee50add9Spbrook                use the corresponding iotlb value.  */
1023b2992e4SStefan Weil             uintptr_t addend;
103b4a4b8d0SPeter Crosthwaite         };
104d656469fSbellard         /* padding to get a power of two size */
105b4a4b8d0SPeter Crosthwaite         uint8_t dummy[1 << CPU_TLB_ENTRY_BITS];
106b4a4b8d0SPeter Crosthwaite     };
107ab93bbe2Sbellard } CPUTLBEntry;
108ab93bbe2Sbellard 
109e85ef538SRichard Henderson QEMU_BUILD_BUG_ON(sizeof(CPUTLBEntry) != (1 << CPU_TLB_ENTRY_BITS));
110355b1943SPaul Brook 
111e469b22fSPeter Maydell /* The IOTLB is not accessed directly inline by generated TCG code,
112e469b22fSPeter Maydell  * so the CPUIOTLBEntry layout is not as critical as that of the
113e469b22fSPeter Maydell  * CPUTLBEntry. (This is also why we don't want to combine the two
114e469b22fSPeter Maydell  * structs into one.)
115e469b22fSPeter Maydell  */
116e469b22fSPeter Maydell typedef struct CPUIOTLBEntry {
117ace41090SPeter Maydell     /*
118ace41090SPeter Maydell      * @addr contains:
119ace41090SPeter Maydell      *  - in the lower TARGET_PAGE_BITS, a physical section number
120ace41090SPeter Maydell      *  - with the lower TARGET_PAGE_BITS masked off, an offset which
121ace41090SPeter Maydell      *    must be added to the virtual address to obtain:
122ace41090SPeter Maydell      *     + the ram_addr_t of the target RAM (if the physical section
123ace41090SPeter Maydell      *       number is PHYS_SECTION_NOTDIRTY or PHYS_SECTION_ROM)
124ace41090SPeter Maydell      *     + the offset within the target MemoryRegion (otherwise)
125ace41090SPeter Maydell      */
126e469b22fSPeter Maydell     hwaddr addr;
127fadc1cbeSPeter Maydell     MemTxAttrs attrs;
128e469b22fSPeter Maydell } CPUIOTLBEntry;
129e469b22fSPeter Maydell 
1301308e026SRichard Henderson typedef struct CPUTLBDesc {
1311308e026SRichard Henderson     /*
1321308e026SRichard Henderson      * Describe a region covering all of the large pages allocated
1331308e026SRichard Henderson      * into the tlb.  When any page within this region is flushed,
1341308e026SRichard Henderson      * we must flush the entire tlb.  The region is matched if
1351308e026SRichard Henderson      * (addr & large_page_mask) == large_page_addr.
1361308e026SRichard Henderson      */
1371308e026SRichard Henderson     target_ulong large_page_addr;
1381308e026SRichard Henderson     target_ulong large_page_mask;
139*79e42085SRichard Henderson     /* host time (in ns) at the beginning of the time window */
140*79e42085SRichard Henderson     int64_t window_begin_ns;
141*79e42085SRichard Henderson     /* maximum number of entries observed in the window */
142*79e42085SRichard Henderson     size_t window_max_entries;
143d5363e58SRichard Henderson     /* The next index to use in the tlb victim table.  */
144d5363e58SRichard Henderson     size_t vindex;
14586e1eff8SEmilio G. Cota     size_t n_used_entries;
1461308e026SRichard Henderson } CPUTLBDesc;
1471308e026SRichard Henderson 
14853d28455SRichard Henderson /*
14953d28455SRichard Henderson  * Data elements that are shared between all MMU modes.
15053d28455SRichard Henderson  */
15153d28455SRichard Henderson typedef struct CPUTLBCommon {
15260a2ad7dSRichard Henderson     /* Serialize updates to tlb_table and tlb_v_table, and others as noted. */
15353d28455SRichard Henderson     QemuSpin lock;
15460a2ad7dSRichard Henderson     /*
1553d1523ceSRichard Henderson      * Within dirty, for each bit N, modifications have been made to
1563d1523ceSRichard Henderson      * mmu_idx N since the last time that mmu_idx was flushed.
1573d1523ceSRichard Henderson      * Protected by tlb_c.lock.
1583d1523ceSRichard Henderson      */
1593d1523ceSRichard Henderson     uint16_t dirty;
160e09de0a2SRichard Henderson     /*
161e09de0a2SRichard Henderson      * Statistics.  These are not lock protected, but are read and
162e09de0a2SRichard Henderson      * written atomically.  This allows the monitor to print a snapshot
163e09de0a2SRichard Henderson      * of the stats without interfering with the cpu.
164e09de0a2SRichard Henderson      */
165e09de0a2SRichard Henderson     size_t full_flush_count;
166e09de0a2SRichard Henderson     size_t part_flush_count;
167e09de0a2SRichard Henderson     size_t elide_flush_count;
16853d28455SRichard Henderson } CPUTLBCommon;
16953d28455SRichard Henderson 
17086e1eff8SEmilio G. Cota # define CPU_TLB                                                        \
17186e1eff8SEmilio G. Cota     /* tlb_mask[i] contains (n_entries - 1) << CPU_TLB_ENTRY_BITS */    \
17286e1eff8SEmilio G. Cota     uintptr_t tlb_mask[NB_MMU_MODES];                                   \
17386e1eff8SEmilio G. Cota     CPUTLBEntry *tlb_table[NB_MMU_MODES];
17486e1eff8SEmilio G. Cota # define CPU_IOTLB                              \
17586e1eff8SEmilio G. Cota     CPUIOTLBEntry *iotlb[NB_MMU_MODES];
17686e1eff8SEmilio G. Cota 
17753d28455SRichard Henderson /*
17853d28455SRichard Henderson  * The meaning of each of the MMU modes is defined in the target code.
17953d28455SRichard Henderson  * Note that NB_MMU_MODES is not yet defined; we can only reference it
18053d28455SRichard Henderson  * within preprocessor defines that will be expanded later.
18153d28455SRichard Henderson  */
18220cb400dSPaul Brook #define CPU_COMMON_TLB \
18353d28455SRichard Henderson     CPUTLBCommon tlb_c;                                                 \
1841308e026SRichard Henderson     CPUTLBDesc tlb_d[NB_MMU_MODES];                                     \
18586e1eff8SEmilio G. Cota     CPU_TLB                                                             \
18688e89a57SXin Tong     CPUTLBEntry tlb_v_table[NB_MMU_MODES][CPU_VTLB_SIZE];               \
18786e1eff8SEmilio G. Cota     CPU_IOTLB                                                           \
188e09de0a2SRichard Henderson     CPUIOTLBEntry iotlb_v[NB_MMU_MODES][CPU_VTLB_SIZE];
18920cb400dSPaul Brook 
19020cb400dSPaul Brook #else
19120cb400dSPaul Brook 
19220cb400dSPaul Brook #define CPU_COMMON_TLB
19320cb400dSPaul Brook 
19420cb400dSPaul Brook #endif
19520cb400dSPaul Brook 
19620cb400dSPaul Brook 
197a316d335Sbellard #define CPU_COMMON                                                      \
198a316d335Sbellard     /* soft mmu support */                                              \
19920cb400dSPaul Brook     CPU_COMMON_TLB                                                      \
200a316d335Sbellard 
201ab93bbe2Sbellard #endif
202