xref: /qemu/include/exec/cpu-defs.h (revision f9919116b8c226428df28bc69ab33480eaa1ee6d)
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"
28b11ec7f2SYang Zhong #ifdef CONFIG_TCG
291de29aefSPaolo Bonzini #include "tcg-target.h"
30b11ec7f2SYang Zhong #endif
31ce927ed9SAndreas Färber #ifndef CONFIG_USER_ONLY
32022c62cbSPaolo Bonzini #include "exec/hwaddr.h"
33ce927ed9SAndreas Färber #endif
34fadc1cbeSPeter Maydell #include "exec/memattrs.h"
352e5b09fdSMarkus Armbruster #include "hw/core/cpu.h"
36ab93bbe2Sbellard 
3774433bf0SRichard Henderson #include "cpu-param.h"
3874433bf0SRichard Henderson 
3935b66fc4Sbellard #ifndef TARGET_LONG_BITS
4074433bf0SRichard Henderson # error TARGET_LONG_BITS must be defined in cpu-param.h
4174433bf0SRichard Henderson #endif
4274433bf0SRichard Henderson #ifndef NB_MMU_MODES
4374433bf0SRichard Henderson # error NB_MMU_MODES must be defined in cpu-param.h
4474433bf0SRichard Henderson #endif
4574433bf0SRichard Henderson #ifndef TARGET_PHYS_ADDR_SPACE_BITS
4674433bf0SRichard Henderson # error TARGET_PHYS_ADDR_SPACE_BITS must be defined in cpu-param.h
4774433bf0SRichard Henderson #endif
4874433bf0SRichard Henderson #ifndef TARGET_VIRT_ADDR_SPACE_BITS
4974433bf0SRichard Henderson # error TARGET_VIRT_ADDR_SPACE_BITS must be defined in cpu-param.h
5074433bf0SRichard Henderson #endif
5174433bf0SRichard Henderson #ifndef TARGET_PAGE_BITS
5274433bf0SRichard Henderson # ifdef TARGET_PAGE_BITS_VARY
5374433bf0SRichard Henderson #  ifndef TARGET_PAGE_BITS_MIN
5474433bf0SRichard Henderson #   error TARGET_PAGE_BITS_MIN must be defined in cpu-param.h
5574433bf0SRichard Henderson #  endif
5674433bf0SRichard Henderson # else
5774433bf0SRichard Henderson #  error TARGET_PAGE_BITS must be defined in cpu-param.h
5874433bf0SRichard Henderson # endif
5935b66fc4Sbellard #endif
6035b66fc4Sbellard 
6135b66fc4Sbellard #define TARGET_LONG_SIZE (TARGET_LONG_BITS / 8)
6235b66fc4Sbellard 
63ab6d960fSbellard /* target_ulong is the type of a virtual address */
6435b66fc4Sbellard #if TARGET_LONG_SIZE == 4
656cfd9b52SPaolo Bonzini typedef int32_t target_long;
666cfd9b52SPaolo Bonzini typedef uint32_t target_ulong;
67c27004ecSbellard #define TARGET_FMT_lx "%08x"
68b62b461bSj_mayer #define TARGET_FMT_ld "%d"
6971c8b8fdSj_mayer #define TARGET_FMT_lu "%u"
7035b66fc4Sbellard #elif TARGET_LONG_SIZE == 8
716cfd9b52SPaolo Bonzini typedef int64_t target_long;
726cfd9b52SPaolo Bonzini typedef uint64_t target_ulong;
7326a76461Sbellard #define TARGET_FMT_lx "%016" PRIx64
74b62b461bSj_mayer #define TARGET_FMT_ld "%" PRId64
7571c8b8fdSj_mayer #define TARGET_FMT_lu "%" PRIu64
7635b66fc4Sbellard #else
7735b66fc4Sbellard #error TARGET_LONG_SIZE undefined
7835b66fc4Sbellard #endif
7935b66fc4Sbellard 
80b11ec7f2SYang Zhong #if !defined(CONFIG_USER_ONLY) && defined(CONFIG_TCG)
81a40ec84eSRichard Henderson 
8288e89a57SXin Tong /* use a fully associative victim tlb of 8 entries */
8388e89a57SXin Tong #define CPU_VTLB_SIZE 8
84ab93bbe2Sbellard 
85355b1943SPaul Brook #if HOST_LONG_BITS == 32 && TARGET_LONG_BITS == 32
86d656469fSbellard #define CPU_TLB_ENTRY_BITS 4
87d656469fSbellard #else
88d656469fSbellard #define CPU_TLB_ENTRY_BITS 5
89d656469fSbellard #endif
90d656469fSbellard 
9186e1eff8SEmilio G. Cota #define CPU_TLB_DYN_MIN_BITS 6
9286e1eff8SEmilio G. Cota #define CPU_TLB_DYN_DEFAULT_BITS 8
9386e1eff8SEmilio G. Cota 
9486e1eff8SEmilio G. Cota # if HOST_LONG_BITS == 32
9586e1eff8SEmilio G. Cota /* Make sure we do not require a double-word shift for the TLB load */
9686e1eff8SEmilio G. Cota #  define CPU_TLB_DYN_MAX_BITS (32 - TARGET_PAGE_BITS)
9786e1eff8SEmilio G. Cota # else /* HOST_LONG_BITS == 64 */
9886e1eff8SEmilio G. Cota /*
9986e1eff8SEmilio G. Cota  * Assuming TARGET_PAGE_BITS==12, with 2**22 entries we can cover 2**(22+12) ==
10086e1eff8SEmilio G. Cota  * 2**34 == 16G of address space. This is roughly what one would expect a
10186e1eff8SEmilio G. Cota  * TLB to cover in a modern (as of 2018) x86_64 CPU. For instance, Intel
10286e1eff8SEmilio G. Cota  * Skylake's Level-2 STLB has 16 1G entries.
10386e1eff8SEmilio G. Cota  * Also, make sure we do not size the TLB past the guest's address space.
10486e1eff8SEmilio G. Cota  */
105*f9919116SEric Blake #  ifdef TARGET_PAGE_BITS_VARY
10686e1eff8SEmilio G. Cota #   define CPU_TLB_DYN_MAX_BITS                                  \
10786e1eff8SEmilio G. Cota     MIN(22, TARGET_VIRT_ADDR_SPACE_BITS - TARGET_PAGE_BITS)
108*f9919116SEric Blake #  else
109*f9919116SEric Blake #   define CPU_TLB_DYN_MAX_BITS                                  \
110*f9919116SEric Blake     MIN_CONST(22, TARGET_VIRT_ADDR_SPACE_BITS - TARGET_PAGE_BITS)
111*f9919116SEric Blake #  endif
11286e1eff8SEmilio G. Cota # endif
11386e1eff8SEmilio G. Cota 
114ab93bbe2Sbellard typedef struct CPUTLBEntry {
1150f459d16Spbrook     /* bit TARGET_LONG_BITS to TARGET_PAGE_BITS : virtual address
1160f459d16Spbrook        bit TARGET_PAGE_BITS-1..4  : Nonzero for accesses that should not
1170f459d16Spbrook                                     go directly to ram.
118db8d7466Sbellard        bit 3                      : indicates that the entry is invalid
119db8d7466Sbellard        bit 2..0                   : zero
120db8d7466Sbellard     */
121b4a4b8d0SPeter Crosthwaite     union {
122b4a4b8d0SPeter Crosthwaite         struct {
12384b7b8e7Sbellard             target_ulong addr_read;
12484b7b8e7Sbellard             target_ulong addr_write;
12584b7b8e7Sbellard             target_ulong addr_code;
126355b1943SPaul Brook             /* Addend to virtual address to get host address.  IO accesses
127ee50add9Spbrook                use the corresponding iotlb value.  */
1283b2992e4SStefan Weil             uintptr_t addend;
129b4a4b8d0SPeter Crosthwaite         };
130d656469fSbellard         /* padding to get a power of two size */
131b4a4b8d0SPeter Crosthwaite         uint8_t dummy[1 << CPU_TLB_ENTRY_BITS];
132b4a4b8d0SPeter Crosthwaite     };
133ab93bbe2Sbellard } CPUTLBEntry;
134ab93bbe2Sbellard 
135e85ef538SRichard Henderson QEMU_BUILD_BUG_ON(sizeof(CPUTLBEntry) != (1 << CPU_TLB_ENTRY_BITS));
136355b1943SPaul Brook 
137e469b22fSPeter Maydell /* The IOTLB is not accessed directly inline by generated TCG code,
138e469b22fSPeter Maydell  * so the CPUIOTLBEntry layout is not as critical as that of the
139e469b22fSPeter Maydell  * CPUTLBEntry. (This is also why we don't want to combine the two
140e469b22fSPeter Maydell  * structs into one.)
141e469b22fSPeter Maydell  */
142e469b22fSPeter Maydell typedef struct CPUIOTLBEntry {
143ace41090SPeter Maydell     /*
144ace41090SPeter Maydell      * @addr contains:
145ace41090SPeter Maydell      *  - in the lower TARGET_PAGE_BITS, a physical section number
146ace41090SPeter Maydell      *  - with the lower TARGET_PAGE_BITS masked off, an offset which
147ace41090SPeter Maydell      *    must be added to the virtual address to obtain:
148ace41090SPeter Maydell      *     + the ram_addr_t of the target RAM (if the physical section
149ace41090SPeter Maydell      *       number is PHYS_SECTION_NOTDIRTY or PHYS_SECTION_ROM)
150ace41090SPeter Maydell      *     + the offset within the target MemoryRegion (otherwise)
151ace41090SPeter Maydell      */
152e469b22fSPeter Maydell     hwaddr addr;
153fadc1cbeSPeter Maydell     MemTxAttrs attrs;
154e469b22fSPeter Maydell } CPUIOTLBEntry;
155e469b22fSPeter Maydell 
156a40ec84eSRichard Henderson /*
157a40ec84eSRichard Henderson  * Data elements that are per MMU mode, minus the bits accessed by
158a40ec84eSRichard Henderson  * the TCG fast path.
159a40ec84eSRichard Henderson  */
1601308e026SRichard Henderson typedef struct CPUTLBDesc {
1611308e026SRichard Henderson     /*
1621308e026SRichard Henderson      * Describe a region covering all of the large pages allocated
1631308e026SRichard Henderson      * into the tlb.  When any page within this region is flushed,
1641308e026SRichard Henderson      * we must flush the entire tlb.  The region is matched if
1651308e026SRichard Henderson      * (addr & large_page_mask) == large_page_addr.
1661308e026SRichard Henderson      */
1671308e026SRichard Henderson     target_ulong large_page_addr;
1681308e026SRichard Henderson     target_ulong large_page_mask;
16979e42085SRichard Henderson     /* host time (in ns) at the beginning of the time window */
17079e42085SRichard Henderson     int64_t window_begin_ns;
17179e42085SRichard Henderson     /* maximum number of entries observed in the window */
17279e42085SRichard Henderson     size_t window_max_entries;
173a40ec84eSRichard Henderson     size_t n_used_entries;
174d5363e58SRichard Henderson     /* The next index to use in the tlb victim table.  */
175d5363e58SRichard Henderson     size_t vindex;
176a40ec84eSRichard Henderson     /* The tlb victim table, in two parts.  */
177a40ec84eSRichard Henderson     CPUTLBEntry vtable[CPU_VTLB_SIZE];
178a40ec84eSRichard Henderson     CPUIOTLBEntry viotlb[CPU_VTLB_SIZE];
179a40ec84eSRichard Henderson     /* The iotlb.  */
180a40ec84eSRichard Henderson     CPUIOTLBEntry *iotlb;
1811308e026SRichard Henderson } CPUTLBDesc;
1821308e026SRichard Henderson 
18353d28455SRichard Henderson /*
184a40ec84eSRichard Henderson  * Data elements that are per MMU mode, accessed by the fast path.
185269bd5d8SRichard Henderson  * The structure is aligned to aid loading the pair with one insn.
186a40ec84eSRichard Henderson  */
187a40ec84eSRichard Henderson typedef struct CPUTLBDescFast {
188a40ec84eSRichard Henderson     /* Contains (n_entries - 1) << CPU_TLB_ENTRY_BITS */
189a40ec84eSRichard Henderson     uintptr_t mask;
190a40ec84eSRichard Henderson     /* The array of tlb entries itself. */
191a40ec84eSRichard Henderson     CPUTLBEntry *table;
192269bd5d8SRichard Henderson } CPUTLBDescFast QEMU_ALIGNED(2 * sizeof(void *));
193a40ec84eSRichard Henderson 
194a40ec84eSRichard Henderson /*
19553d28455SRichard Henderson  * Data elements that are shared between all MMU modes.
19653d28455SRichard Henderson  */
19753d28455SRichard Henderson typedef struct CPUTLBCommon {
198a40ec84eSRichard Henderson     /* Serialize updates to f.table and d.vtable, and others as noted. */
19953d28455SRichard Henderson     QemuSpin lock;
20060a2ad7dSRichard Henderson     /*
2013d1523ceSRichard Henderson      * Within dirty, for each bit N, modifications have been made to
2023d1523ceSRichard Henderson      * mmu_idx N since the last time that mmu_idx was flushed.
2033d1523ceSRichard Henderson      * Protected by tlb_c.lock.
2043d1523ceSRichard Henderson      */
2053d1523ceSRichard Henderson     uint16_t dirty;
206e09de0a2SRichard Henderson     /*
207e09de0a2SRichard Henderson      * Statistics.  These are not lock protected, but are read and
208e09de0a2SRichard Henderson      * written atomically.  This allows the monitor to print a snapshot
209e09de0a2SRichard Henderson      * of the stats without interfering with the cpu.
210e09de0a2SRichard Henderson      */
211e09de0a2SRichard Henderson     size_t full_flush_count;
212e09de0a2SRichard Henderson     size_t part_flush_count;
213e09de0a2SRichard Henderson     size_t elide_flush_count;
21453d28455SRichard Henderson } CPUTLBCommon;
21553d28455SRichard Henderson 
21653d28455SRichard Henderson /*
217a40ec84eSRichard Henderson  * The entire softmmu tlb, for all MMU modes.
21853d28455SRichard Henderson  * The meaning of each of the MMU modes is defined in the target code.
219269bd5d8SRichard Henderson  * Since this is placed within CPUNegativeOffsetState, the smallest
220269bd5d8SRichard Henderson  * negative offsets are at the end of the struct.
22153d28455SRichard Henderson  */
222e6d86bedSEmilio G. Cota 
223a40ec84eSRichard Henderson typedef struct CPUTLB {
224a40ec84eSRichard Henderson     CPUTLBCommon c;
225269bd5d8SRichard Henderson     CPUTLBDesc d[NB_MMU_MODES];
226269bd5d8SRichard Henderson     CPUTLBDescFast f[NB_MMU_MODES];
227a40ec84eSRichard Henderson } CPUTLB;
228a40ec84eSRichard Henderson 
229269bd5d8SRichard Henderson /* This will be used by TCG backends to compute offsets.  */
230269bd5d8SRichard Henderson #define TLB_MASK_TABLE_OFS(IDX) \
231269bd5d8SRichard Henderson     ((int)offsetof(ArchCPU, neg.tlb.f[IDX]) - (int)offsetof(ArchCPU, env))
23220cb400dSPaul Brook 
23320cb400dSPaul Brook #else
23420cb400dSPaul Brook 
235269bd5d8SRichard Henderson typedef struct CPUTLB { } CPUTLB;
23620cb400dSPaul Brook 
237a40ec84eSRichard Henderson #endif  /* !CONFIG_USER_ONLY && CONFIG_TCG */
238a316d335Sbellard 
2395b146dc7SRichard Henderson /*
2401eb21c42SAlex Bennée  * This structure must be placed in ArchCPU immediately
2415b146dc7SRichard Henderson  * before CPUArchState, as a field named "neg".
2425b146dc7SRichard Henderson  */
2435b146dc7SRichard Henderson typedef struct CPUNegativeOffsetState {
244269bd5d8SRichard Henderson     CPUTLB tlb;
2455e140196SRichard Henderson     IcountDecr icount_decr;
2465b146dc7SRichard Henderson } CPUNegativeOffsetState;
2475b146dc7SRichard Henderson 
248ab93bbe2Sbellard #endif
249