xref: /openbmc/qemu/include/exec/cpu-defs.h (revision 2e5b09fd)
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"
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"
35*2e5b09fdSMarkus Armbruster #include "hw/core/cpu.h"
36022c62cbSPaolo Bonzini 
3774433bf0SRichard Henderson #include "cpu-param.h"
3874433bf0SRichard Henderson 
39022c62cbSPaolo Bonzini #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
59022c62cbSPaolo Bonzini #endif
60022c62cbSPaolo Bonzini 
61022c62cbSPaolo Bonzini #define TARGET_LONG_SIZE (TARGET_LONG_BITS / 8)
62022c62cbSPaolo Bonzini 
63022c62cbSPaolo Bonzini /* target_ulong is the type of a virtual address */
64022c62cbSPaolo Bonzini #if TARGET_LONG_SIZE == 4
656cfd9b52SPaolo Bonzini typedef int32_t target_long;
666cfd9b52SPaolo Bonzini typedef uint32_t target_ulong;
67022c62cbSPaolo Bonzini #define TARGET_FMT_lx "%08x"
68022c62cbSPaolo Bonzini #define TARGET_FMT_ld "%d"
69022c62cbSPaolo Bonzini #define TARGET_FMT_lu "%u"
70022c62cbSPaolo Bonzini #elif TARGET_LONG_SIZE == 8
716cfd9b52SPaolo Bonzini typedef int64_t target_long;
726cfd9b52SPaolo Bonzini typedef uint64_t target_ulong;
73022c62cbSPaolo Bonzini #define TARGET_FMT_lx "%016" PRIx64
74022c62cbSPaolo Bonzini #define TARGET_FMT_ld "%" PRId64
75022c62cbSPaolo Bonzini #define TARGET_FMT_lu "%" PRIu64
76022c62cbSPaolo Bonzini #else
77022c62cbSPaolo Bonzini #error TARGET_LONG_SIZE undefined
78022c62cbSPaolo Bonzini #endif
79022c62cbSPaolo Bonzini 
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
84022c62cbSPaolo Bonzini 
85022c62cbSPaolo Bonzini #if HOST_LONG_BITS == 32 && TARGET_LONG_BITS == 32
86022c62cbSPaolo Bonzini #define CPU_TLB_ENTRY_BITS 4
87022c62cbSPaolo Bonzini #else
88022c62cbSPaolo Bonzini #define CPU_TLB_ENTRY_BITS 5
89022c62cbSPaolo Bonzini #endif
90022c62cbSPaolo Bonzini 
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  */
10586e1eff8SEmilio G. Cota #  define CPU_TLB_DYN_MAX_BITS                                  \
10686e1eff8SEmilio G. Cota     MIN(22, TARGET_VIRT_ADDR_SPACE_BITS - TARGET_PAGE_BITS)
10786e1eff8SEmilio G. Cota # endif
10886e1eff8SEmilio G. Cota 
109022c62cbSPaolo Bonzini typedef struct CPUTLBEntry {
110022c62cbSPaolo Bonzini     /* bit TARGET_LONG_BITS to TARGET_PAGE_BITS : virtual address
111022c62cbSPaolo Bonzini        bit TARGET_PAGE_BITS-1..4  : Nonzero for accesses that should not
112022c62cbSPaolo Bonzini                                     go directly to ram.
113022c62cbSPaolo Bonzini        bit 3                      : indicates that the entry is invalid
114022c62cbSPaolo Bonzini        bit 2..0                   : zero
115022c62cbSPaolo Bonzini     */
116b4a4b8d0SPeter Crosthwaite     union {
117b4a4b8d0SPeter Crosthwaite         struct {
118022c62cbSPaolo Bonzini             target_ulong addr_read;
119022c62cbSPaolo Bonzini             target_ulong addr_write;
120022c62cbSPaolo Bonzini             target_ulong addr_code;
121022c62cbSPaolo Bonzini             /* Addend to virtual address to get host address.  IO accesses
122022c62cbSPaolo Bonzini                use the corresponding iotlb value.  */
123022c62cbSPaolo Bonzini             uintptr_t addend;
124b4a4b8d0SPeter Crosthwaite         };
125022c62cbSPaolo Bonzini         /* padding to get a power of two size */
126b4a4b8d0SPeter Crosthwaite         uint8_t dummy[1 << CPU_TLB_ENTRY_BITS];
127b4a4b8d0SPeter Crosthwaite     };
128022c62cbSPaolo Bonzini } CPUTLBEntry;
129022c62cbSPaolo Bonzini 
130e85ef538SRichard Henderson QEMU_BUILD_BUG_ON(sizeof(CPUTLBEntry) != (1 << CPU_TLB_ENTRY_BITS));
131022c62cbSPaolo Bonzini 
132e469b22fSPeter Maydell /* The IOTLB is not accessed directly inline by generated TCG code,
133e469b22fSPeter Maydell  * so the CPUIOTLBEntry layout is not as critical as that of the
134e469b22fSPeter Maydell  * CPUTLBEntry. (This is also why we don't want to combine the two
135e469b22fSPeter Maydell  * structs into one.)
136e469b22fSPeter Maydell  */
137e469b22fSPeter Maydell typedef struct CPUIOTLBEntry {
138ace41090SPeter Maydell     /*
139ace41090SPeter Maydell      * @addr contains:
140ace41090SPeter Maydell      *  - in the lower TARGET_PAGE_BITS, a physical section number
141ace41090SPeter Maydell      *  - with the lower TARGET_PAGE_BITS masked off, an offset which
142ace41090SPeter Maydell      *    must be added to the virtual address to obtain:
143ace41090SPeter Maydell      *     + the ram_addr_t of the target RAM (if the physical section
144ace41090SPeter Maydell      *       number is PHYS_SECTION_NOTDIRTY or PHYS_SECTION_ROM)
145ace41090SPeter Maydell      *     + the offset within the target MemoryRegion (otherwise)
146ace41090SPeter Maydell      */
147e469b22fSPeter Maydell     hwaddr addr;
148fadc1cbeSPeter Maydell     MemTxAttrs attrs;
149e469b22fSPeter Maydell } CPUIOTLBEntry;
150e469b22fSPeter Maydell 
151a40ec84eSRichard Henderson /*
152a40ec84eSRichard Henderson  * Data elements that are per MMU mode, minus the bits accessed by
153a40ec84eSRichard Henderson  * the TCG fast path.
154a40ec84eSRichard Henderson  */
1551308e026SRichard Henderson typedef struct CPUTLBDesc {
1561308e026SRichard Henderson     /*
1571308e026SRichard Henderson      * Describe a region covering all of the large pages allocated
1581308e026SRichard Henderson      * into the tlb.  When any page within this region is flushed,
1591308e026SRichard Henderson      * we must flush the entire tlb.  The region is matched if
1601308e026SRichard Henderson      * (addr & large_page_mask) == large_page_addr.
1611308e026SRichard Henderson      */
1621308e026SRichard Henderson     target_ulong large_page_addr;
1631308e026SRichard Henderson     target_ulong large_page_mask;
16479e42085SRichard Henderson     /* host time (in ns) at the beginning of the time window */
16579e42085SRichard Henderson     int64_t window_begin_ns;
16679e42085SRichard Henderson     /* maximum number of entries observed in the window */
16779e42085SRichard Henderson     size_t window_max_entries;
168a40ec84eSRichard Henderson     size_t n_used_entries;
169d5363e58SRichard Henderson     /* The next index to use in the tlb victim table.  */
170d5363e58SRichard Henderson     size_t vindex;
171a40ec84eSRichard Henderson     /* The tlb victim table, in two parts.  */
172a40ec84eSRichard Henderson     CPUTLBEntry vtable[CPU_VTLB_SIZE];
173a40ec84eSRichard Henderson     CPUIOTLBEntry viotlb[CPU_VTLB_SIZE];
174a40ec84eSRichard Henderson     /* The iotlb.  */
175a40ec84eSRichard Henderson     CPUIOTLBEntry *iotlb;
1761308e026SRichard Henderson } CPUTLBDesc;
1771308e026SRichard Henderson 
17853d28455SRichard Henderson /*
179a40ec84eSRichard Henderson  * Data elements that are per MMU mode, accessed by the fast path.
180269bd5d8SRichard Henderson  * The structure is aligned to aid loading the pair with one insn.
181a40ec84eSRichard Henderson  */
182a40ec84eSRichard Henderson typedef struct CPUTLBDescFast {
183a40ec84eSRichard Henderson     /* Contains (n_entries - 1) << CPU_TLB_ENTRY_BITS */
184a40ec84eSRichard Henderson     uintptr_t mask;
185a40ec84eSRichard Henderson     /* The array of tlb entries itself. */
186a40ec84eSRichard Henderson     CPUTLBEntry *table;
187269bd5d8SRichard Henderson } CPUTLBDescFast QEMU_ALIGNED(2 * sizeof(void *));
188a40ec84eSRichard Henderson 
189a40ec84eSRichard Henderson /*
19053d28455SRichard Henderson  * Data elements that are shared between all MMU modes.
19153d28455SRichard Henderson  */
19253d28455SRichard Henderson typedef struct CPUTLBCommon {
193a40ec84eSRichard Henderson     /* Serialize updates to f.table and d.vtable, and others as noted. */
19453d28455SRichard Henderson     QemuSpin lock;
19560a2ad7dSRichard Henderson     /*
1963d1523ceSRichard Henderson      * Within dirty, for each bit N, modifications have been made to
1973d1523ceSRichard Henderson      * mmu_idx N since the last time that mmu_idx was flushed.
1983d1523ceSRichard Henderson      * Protected by tlb_c.lock.
1993d1523ceSRichard Henderson      */
2003d1523ceSRichard Henderson     uint16_t dirty;
201e09de0a2SRichard Henderson     /*
202e09de0a2SRichard Henderson      * Statistics.  These are not lock protected, but are read and
203e09de0a2SRichard Henderson      * written atomically.  This allows the monitor to print a snapshot
204e09de0a2SRichard Henderson      * of the stats without interfering with the cpu.
205e09de0a2SRichard Henderson      */
206e09de0a2SRichard Henderson     size_t full_flush_count;
207e09de0a2SRichard Henderson     size_t part_flush_count;
208e09de0a2SRichard Henderson     size_t elide_flush_count;
20953d28455SRichard Henderson } CPUTLBCommon;
21053d28455SRichard Henderson 
21153d28455SRichard Henderson /*
212a40ec84eSRichard Henderson  * The entire softmmu tlb, for all MMU modes.
21353d28455SRichard Henderson  * The meaning of each of the MMU modes is defined in the target code.
214269bd5d8SRichard Henderson  * Since this is placed within CPUNegativeOffsetState, the smallest
215269bd5d8SRichard Henderson  * negative offsets are at the end of the struct.
21653d28455SRichard Henderson  */
217a40ec84eSRichard Henderson typedef struct CPUTLB {
218a40ec84eSRichard Henderson     CPUTLBCommon c;
219269bd5d8SRichard Henderson     CPUTLBDesc d[NB_MMU_MODES];
220269bd5d8SRichard Henderson     CPUTLBDescFast f[NB_MMU_MODES];
221a40ec84eSRichard Henderson } CPUTLB;
222a40ec84eSRichard Henderson 
223269bd5d8SRichard Henderson /* This will be used by TCG backends to compute offsets.  */
224269bd5d8SRichard Henderson #define TLB_MASK_TABLE_OFS(IDX) \
225269bd5d8SRichard Henderson     ((int)offsetof(ArchCPU, neg.tlb.f[IDX]) - (int)offsetof(ArchCPU, env))
226022c62cbSPaolo Bonzini 
227022c62cbSPaolo Bonzini #else
228022c62cbSPaolo Bonzini 
229269bd5d8SRichard Henderson typedef struct CPUTLB { } CPUTLB;
230022c62cbSPaolo Bonzini 
231a40ec84eSRichard Henderson #endif  /* !CONFIG_USER_ONLY && CONFIG_TCG */
232022c62cbSPaolo Bonzini 
2335b146dc7SRichard Henderson /*
2345b146dc7SRichard Henderson  * This structure must be placed in ArchCPU immedately
2355b146dc7SRichard Henderson  * before CPUArchState, as a field named "neg".
2365b146dc7SRichard Henderson  */
2375b146dc7SRichard Henderson typedef struct CPUNegativeOffsetState {
238269bd5d8SRichard Henderson     CPUTLB tlb;
2395e140196SRichard Henderson     IcountDecr icount_decr;
2405b146dc7SRichard Henderson } CPUNegativeOffsetState;
2415b146dc7SRichard Henderson 
242022c62cbSPaolo Bonzini #endif
243