xref: /qemu/include/exec/cpu-defs.h (revision ffd824f3f32d135c81cbc70f2e3f2edd0496f330)
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
9d6ea4236SChetan Pant  * version 2.1 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"
28ce927ed9SAndreas Färber #ifndef CONFIG_USER_ONLY
29022c62cbSPaolo Bonzini #include "exec/hwaddr.h"
30ce927ed9SAndreas Färber #endif
31fadc1cbeSPeter Maydell #include "exec/memattrs.h"
322e5b09fdSMarkus Armbruster #include "hw/core/cpu.h"
33ab93bbe2Sbellard 
3474433bf0SRichard Henderson #include "cpu-param.h"
3574433bf0SRichard Henderson 
3635b66fc4Sbellard #ifndef TARGET_LONG_BITS
3774433bf0SRichard Henderson # error TARGET_LONG_BITS must be defined in cpu-param.h
3874433bf0SRichard Henderson #endif
3974433bf0SRichard Henderson #ifndef TARGET_PHYS_ADDR_SPACE_BITS
4074433bf0SRichard Henderson # error TARGET_PHYS_ADDR_SPACE_BITS must be defined in cpu-param.h
4174433bf0SRichard Henderson #endif
4274433bf0SRichard Henderson #ifndef TARGET_VIRT_ADDR_SPACE_BITS
4374433bf0SRichard Henderson # error TARGET_VIRT_ADDR_SPACE_BITS must be defined in cpu-param.h
4474433bf0SRichard Henderson #endif
4574433bf0SRichard Henderson #ifndef TARGET_PAGE_BITS
4674433bf0SRichard Henderson # ifdef TARGET_PAGE_BITS_VARY
4774433bf0SRichard Henderson #  ifndef TARGET_PAGE_BITS_MIN
4874433bf0SRichard Henderson #   error TARGET_PAGE_BITS_MIN must be defined in cpu-param.h
4974433bf0SRichard Henderson #  endif
5074433bf0SRichard Henderson # else
5174433bf0SRichard Henderson #  error TARGET_PAGE_BITS must be defined in cpu-param.h
5274433bf0SRichard Henderson # endif
5335b66fc4Sbellard #endif
5435b66fc4Sbellard 
554692a86fSAlex Bennée #include "exec/target_long.h"
5635b66fc4Sbellard 
57*ffd824f3SAnton Johansson /*
58*ffd824f3SAnton Johansson  * Fix the number of mmu modes to 16, which is also the maximum
59*ffd824f3SAnton Johansson  * supported by the softmmu tlb api.
60*ffd824f3SAnton Johansson  */
61*ffd824f3SAnton Johansson #ifndef NB_MMU_MODES
62*ffd824f3SAnton Johansson #define NB_MMU_MODES 16
63*ffd824f3SAnton Johansson #endif
64*ffd824f3SAnton Johansson 
65b11ec7f2SYang Zhong #if !defined(CONFIG_USER_ONLY) && defined(CONFIG_TCG)
66a40ec84eSRichard Henderson 
6788e89a57SXin Tong /* use a fully associative victim tlb of 8 entries */
6888e89a57SXin Tong #define CPU_VTLB_SIZE 8
69ab93bbe2Sbellard 
70355b1943SPaul Brook #if HOST_LONG_BITS == 32 && TARGET_LONG_BITS == 32
71d656469fSbellard #define CPU_TLB_ENTRY_BITS 4
72d656469fSbellard #else
73d656469fSbellard #define CPU_TLB_ENTRY_BITS 5
74d656469fSbellard #endif
75d656469fSbellard 
7686e1eff8SEmilio G. Cota #define CPU_TLB_DYN_MIN_BITS 6
7786e1eff8SEmilio G. Cota #define CPU_TLB_DYN_DEFAULT_BITS 8
7886e1eff8SEmilio G. Cota 
7986e1eff8SEmilio G. Cota # if HOST_LONG_BITS == 32
8086e1eff8SEmilio G. Cota /* Make sure we do not require a double-word shift for the TLB load */
8186e1eff8SEmilio G. Cota #  define CPU_TLB_DYN_MAX_BITS (32 - TARGET_PAGE_BITS)
8286e1eff8SEmilio G. Cota # else /* HOST_LONG_BITS == 64 */
8386e1eff8SEmilio G. Cota /*
8486e1eff8SEmilio G. Cota  * Assuming TARGET_PAGE_BITS==12, with 2**22 entries we can cover 2**(22+12) ==
8586e1eff8SEmilio G. Cota  * 2**34 == 16G of address space. This is roughly what one would expect a
8686e1eff8SEmilio G. Cota  * TLB to cover in a modern (as of 2018) x86_64 CPU. For instance, Intel
8786e1eff8SEmilio G. Cota  * Skylake's Level-2 STLB has 16 1G entries.
8886e1eff8SEmilio G. Cota  * Also, make sure we do not size the TLB past the guest's address space.
8986e1eff8SEmilio G. Cota  */
90f9919116SEric Blake #  ifdef TARGET_PAGE_BITS_VARY
9186e1eff8SEmilio G. Cota #   define CPU_TLB_DYN_MAX_BITS                                  \
9286e1eff8SEmilio G. Cota     MIN(22, TARGET_VIRT_ADDR_SPACE_BITS - TARGET_PAGE_BITS)
93f9919116SEric Blake #  else
94f9919116SEric Blake #   define CPU_TLB_DYN_MAX_BITS                                  \
95f9919116SEric Blake     MIN_CONST(22, TARGET_VIRT_ADDR_SPACE_BITS - TARGET_PAGE_BITS)
96f9919116SEric Blake #  endif
9786e1eff8SEmilio G. Cota # endif
9886e1eff8SEmilio G. Cota 
9925d3ec58SRichard Henderson /* Minimalized TLB entry for use by TCG fast path. */
100ab93bbe2Sbellard typedef struct CPUTLBEntry {
1010f459d16Spbrook     /* bit TARGET_LONG_BITS to TARGET_PAGE_BITS : virtual address
1020f459d16Spbrook        bit TARGET_PAGE_BITS-1..4  : Nonzero for accesses that should not
1030f459d16Spbrook                                     go directly to ram.
104db8d7466Sbellard        bit 3                      : indicates that the entry is invalid
105db8d7466Sbellard        bit 2..0                   : zero
106db8d7466Sbellard     */
107b4a4b8d0SPeter Crosthwaite     union {
108b4a4b8d0SPeter Crosthwaite         struct {
10984b7b8e7Sbellard             target_ulong addr_read;
11084b7b8e7Sbellard             target_ulong addr_write;
11184b7b8e7Sbellard             target_ulong addr_code;
112355b1943SPaul Brook             /* Addend to virtual address to get host address.  IO accesses
113ee50add9Spbrook                use the corresponding iotlb value.  */
1143b2992e4SStefan Weil             uintptr_t addend;
115b4a4b8d0SPeter Crosthwaite         };
116d656469fSbellard         /* padding to get a power of two size */
117b4a4b8d0SPeter Crosthwaite         uint8_t dummy[1 << CPU_TLB_ENTRY_BITS];
118b4a4b8d0SPeter Crosthwaite     };
119ab93bbe2Sbellard } CPUTLBEntry;
120ab93bbe2Sbellard 
121e85ef538SRichard Henderson QEMU_BUILD_BUG_ON(sizeof(CPUTLBEntry) != (1 << CPU_TLB_ENTRY_BITS));
122355b1943SPaul Brook 
1234cb884e9SFabiano Rosas 
1244cb884e9SFabiano Rosas #endif  /* !CONFIG_USER_ONLY && CONFIG_TCG */
1254cb884e9SFabiano Rosas 
1264cb884e9SFabiano Rosas #if !defined(CONFIG_USER_ONLY)
127ace41090SPeter Maydell /*
12825d3ec58SRichard Henderson  * The full TLB entry, which is not accessed by generated TCG code,
12925d3ec58SRichard Henderson  * so the layout is not as critical as that of CPUTLBEntry. This is
13025d3ec58SRichard Henderson  * also why we don't want to combine the two structs.
13125d3ec58SRichard Henderson  */
13225d3ec58SRichard Henderson typedef struct CPUTLBEntryFull {
13325d3ec58SRichard Henderson     /*
13425d3ec58SRichard Henderson      * @xlat_section contains:
135ace41090SPeter Maydell      *  - in the lower TARGET_PAGE_BITS, a physical section number
136ace41090SPeter Maydell      *  - with the lower TARGET_PAGE_BITS masked off, an offset which
137ace41090SPeter Maydell      *    must be added to the virtual address to obtain:
138ace41090SPeter Maydell      *     + the ram_addr_t of the target RAM (if the physical section
139ace41090SPeter Maydell      *       number is PHYS_SECTION_NOTDIRTY or PHYS_SECTION_ROM)
140ace41090SPeter Maydell      *     + the offset within the target MemoryRegion (otherwise)
141ace41090SPeter Maydell      */
14225d3ec58SRichard Henderson     hwaddr xlat_section;
14340473689SRichard Henderson 
14440473689SRichard Henderson     /*
14540473689SRichard Henderson      * @phys_addr contains the physical address in the address space
14640473689SRichard Henderson      * given by cpu_asidx_from_attrs(cpu, @attrs).
14740473689SRichard Henderson      */
14840473689SRichard Henderson     hwaddr phys_addr;
14940473689SRichard Henderson 
15040473689SRichard Henderson     /* @attrs contains the memory transaction attributes for the page. */
151fadc1cbeSPeter Maydell     MemTxAttrs attrs;
15240473689SRichard Henderson 
15340473689SRichard Henderson     /* @prot contains the complete protections for the page. */
15440473689SRichard Henderson     uint8_t prot;
15540473689SRichard Henderson 
15640473689SRichard Henderson     /* @lg_page_size contains the log2 of the page size. */
15740473689SRichard Henderson     uint8_t lg_page_size;
1588c6953cfSRichard Henderson 
1598c6953cfSRichard Henderson     /*
1608c6953cfSRichard Henderson      * Allow target-specific additions to this structure.
1618c6953cfSRichard Henderson      * This may be used to cache items from the guest cpu
1628c6953cfSRichard Henderson      * page tables for later use by the implementation.
1638c6953cfSRichard Henderson      */
1648c6953cfSRichard Henderson #ifdef TARGET_PAGE_ENTRY_EXTRA
1658c6953cfSRichard Henderson     TARGET_PAGE_ENTRY_EXTRA
1668c6953cfSRichard Henderson #endif
16725d3ec58SRichard Henderson } CPUTLBEntryFull;
1684cb884e9SFabiano Rosas #endif  /* !CONFIG_USER_ONLY */
169e469b22fSPeter Maydell 
1704cb884e9SFabiano Rosas #if !defined(CONFIG_USER_ONLY) && defined(CONFIG_TCG)
171a40ec84eSRichard Henderson /*
172a40ec84eSRichard Henderson  * Data elements that are per MMU mode, minus the bits accessed by
173a40ec84eSRichard Henderson  * the TCG fast path.
174a40ec84eSRichard Henderson  */
1751308e026SRichard Henderson typedef struct CPUTLBDesc {
1761308e026SRichard Henderson     /*
1771308e026SRichard Henderson      * Describe a region covering all of the large pages allocated
1781308e026SRichard Henderson      * into the tlb.  When any page within this region is flushed,
1791308e026SRichard Henderson      * we must flush the entire tlb.  The region is matched if
1801308e026SRichard Henderson      * (addr & large_page_mask) == large_page_addr.
1811308e026SRichard Henderson      */
1821308e026SRichard Henderson     target_ulong large_page_addr;
1831308e026SRichard Henderson     target_ulong large_page_mask;
18479e42085SRichard Henderson     /* host time (in ns) at the beginning of the time window */
18579e42085SRichard Henderson     int64_t window_begin_ns;
18679e42085SRichard Henderson     /* maximum number of entries observed in the window */
18779e42085SRichard Henderson     size_t window_max_entries;
188a40ec84eSRichard Henderson     size_t n_used_entries;
189d5363e58SRichard Henderson     /* The next index to use in the tlb victim table.  */
190d5363e58SRichard Henderson     size_t vindex;
191a40ec84eSRichard Henderson     /* The tlb victim table, in two parts.  */
192a40ec84eSRichard Henderson     CPUTLBEntry vtable[CPU_VTLB_SIZE];
19325d3ec58SRichard Henderson     CPUTLBEntryFull vfulltlb[CPU_VTLB_SIZE];
19425d3ec58SRichard Henderson     CPUTLBEntryFull *fulltlb;
1951308e026SRichard Henderson } CPUTLBDesc;
1961308e026SRichard Henderson 
19753d28455SRichard Henderson /*
198a40ec84eSRichard Henderson  * Data elements that are per MMU mode, accessed by the fast path.
199269bd5d8SRichard Henderson  * The structure is aligned to aid loading the pair with one insn.
200a40ec84eSRichard Henderson  */
201a40ec84eSRichard Henderson typedef struct CPUTLBDescFast {
202a40ec84eSRichard Henderson     /* Contains (n_entries - 1) << CPU_TLB_ENTRY_BITS */
203a40ec84eSRichard Henderson     uintptr_t mask;
204a40ec84eSRichard Henderson     /* The array of tlb entries itself. */
205a40ec84eSRichard Henderson     CPUTLBEntry *table;
206269bd5d8SRichard Henderson } CPUTLBDescFast QEMU_ALIGNED(2 * sizeof(void *));
207a40ec84eSRichard Henderson 
208a40ec84eSRichard Henderson /*
20953d28455SRichard Henderson  * Data elements that are shared between all MMU modes.
21053d28455SRichard Henderson  */
21153d28455SRichard Henderson typedef struct CPUTLBCommon {
212a40ec84eSRichard Henderson     /* Serialize updates to f.table and d.vtable, and others as noted. */
21353d28455SRichard Henderson     QemuSpin lock;
21460a2ad7dSRichard Henderson     /*
2153d1523ceSRichard Henderson      * Within dirty, for each bit N, modifications have been made to
2163d1523ceSRichard Henderson      * mmu_idx N since the last time that mmu_idx was flushed.
2173d1523ceSRichard Henderson      * Protected by tlb_c.lock.
2183d1523ceSRichard Henderson      */
2193d1523ceSRichard Henderson     uint16_t dirty;
220e09de0a2SRichard Henderson     /*
221e09de0a2SRichard Henderson      * Statistics.  These are not lock protected, but are read and
222e09de0a2SRichard Henderson      * written atomically.  This allows the monitor to print a snapshot
223e09de0a2SRichard Henderson      * of the stats without interfering with the cpu.
224e09de0a2SRichard Henderson      */
225e09de0a2SRichard Henderson     size_t full_flush_count;
226e09de0a2SRichard Henderson     size_t part_flush_count;
227e09de0a2SRichard Henderson     size_t elide_flush_count;
22853d28455SRichard Henderson } CPUTLBCommon;
22953d28455SRichard Henderson 
23053d28455SRichard Henderson /*
231a40ec84eSRichard Henderson  * The entire softmmu tlb, for all MMU modes.
23253d28455SRichard Henderson  * The meaning of each of the MMU modes is defined in the target code.
233269bd5d8SRichard Henderson  * Since this is placed within CPUNegativeOffsetState, the smallest
234269bd5d8SRichard Henderson  * negative offsets are at the end of the struct.
23553d28455SRichard Henderson  */
236e6d86bedSEmilio G. Cota 
237a40ec84eSRichard Henderson typedef struct CPUTLB {
238a40ec84eSRichard Henderson     CPUTLBCommon c;
239269bd5d8SRichard Henderson     CPUTLBDesc d[NB_MMU_MODES];
240269bd5d8SRichard Henderson     CPUTLBDescFast f[NB_MMU_MODES];
241a40ec84eSRichard Henderson } CPUTLB;
242a40ec84eSRichard Henderson 
243269bd5d8SRichard Henderson /* This will be used by TCG backends to compute offsets.  */
244269bd5d8SRichard Henderson #define TLB_MASK_TABLE_OFS(IDX) \
245269bd5d8SRichard Henderson     ((int)offsetof(ArchCPU, neg.tlb.f[IDX]) - (int)offsetof(ArchCPU, env))
24620cb400dSPaul Brook 
24720cb400dSPaul Brook #else
24820cb400dSPaul Brook 
249269bd5d8SRichard Henderson typedef struct CPUTLB { } CPUTLB;
25020cb400dSPaul Brook 
251a40ec84eSRichard Henderson #endif  /* !CONFIG_USER_ONLY && CONFIG_TCG */
252a316d335Sbellard 
2535b146dc7SRichard Henderson /*
2541eb21c42SAlex Bennée  * This structure must be placed in ArchCPU immediately
2555b146dc7SRichard Henderson  * before CPUArchState, as a field named "neg".
2565b146dc7SRichard Henderson  */
2575b146dc7SRichard Henderson typedef struct CPUNegativeOffsetState {
258269bd5d8SRichard Henderson     CPUTLB tlb;
2595e140196SRichard Henderson     IcountDecr icount_decr;
2605b146dc7SRichard Henderson } CPUNegativeOffsetState;
2615b146dc7SRichard Henderson 
262ab93bbe2Sbellard #endif
263