xref: /openbmc/qemu/include/exec/cpu-defs.h (revision 708906dc)
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
9d6ea4236SChetan Pant  * version 2.1 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"
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"
33022c62cbSPaolo Bonzini 
3474433bf0SRichard Henderson #include "cpu-param.h"
3574433bf0SRichard Henderson 
36022c62cbSPaolo Bonzini #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
53022c62cbSPaolo Bonzini #endif
54022c62cbSPaolo Bonzini 
554692a86fSAlex Bennée #include "exec/target_long.h"
56022c62cbSPaolo Bonzini 
57ffd824f3SAnton Johansson /*
58ffd824f3SAnton Johansson  * Fix the number of mmu modes to 16, which is also the maximum
59ffd824f3SAnton Johansson  * supported by the softmmu tlb api.
60ffd824f3SAnton Johansson  */
61ffd824f3SAnton Johansson #define NB_MMU_MODES 16
62ffd824f3SAnton Johansson 
63*708906dcSPhilippe Mathieu-Daudé #if defined(CONFIG_SOFTMMU) && defined(CONFIG_TCG)
64d0a9bb5eSRichard Henderson #include "exec/tlb-common.h"
65a40ec84eSRichard Henderson 
6688e89a57SXin Tong /* use a fully associative victim tlb of 8 entries */
6788e89a57SXin Tong #define CPU_VTLB_SIZE 8
68022c62cbSPaolo Bonzini 
6986e1eff8SEmilio G. Cota #define CPU_TLB_DYN_MIN_BITS 6
7086e1eff8SEmilio G. Cota #define CPU_TLB_DYN_DEFAULT_BITS 8
7186e1eff8SEmilio G. Cota 
7286e1eff8SEmilio G. Cota # if HOST_LONG_BITS == 32
7386e1eff8SEmilio G. Cota /* Make sure we do not require a double-word shift for the TLB load */
7486e1eff8SEmilio G. Cota #  define CPU_TLB_DYN_MAX_BITS (32 - TARGET_PAGE_BITS)
7586e1eff8SEmilio G. Cota # else /* HOST_LONG_BITS == 64 */
7686e1eff8SEmilio G. Cota /*
7786e1eff8SEmilio G. Cota  * Assuming TARGET_PAGE_BITS==12, with 2**22 entries we can cover 2**(22+12) ==
7886e1eff8SEmilio G. Cota  * 2**34 == 16G of address space. This is roughly what one would expect a
7986e1eff8SEmilio G. Cota  * TLB to cover in a modern (as of 2018) x86_64 CPU. For instance, Intel
8086e1eff8SEmilio G. Cota  * Skylake's Level-2 STLB has 16 1G entries.
8186e1eff8SEmilio G. Cota  * Also, make sure we do not size the TLB past the guest's address space.
8286e1eff8SEmilio G. Cota  */
83f9919116SEric Blake #  ifdef TARGET_PAGE_BITS_VARY
8486e1eff8SEmilio G. Cota #   define CPU_TLB_DYN_MAX_BITS                                  \
8586e1eff8SEmilio G. Cota     MIN(22, TARGET_VIRT_ADDR_SPACE_BITS - TARGET_PAGE_BITS)
86f9919116SEric Blake #  else
87f9919116SEric Blake #   define CPU_TLB_DYN_MAX_BITS                                  \
88f9919116SEric Blake     MIN_CONST(22, TARGET_VIRT_ADDR_SPACE_BITS - TARGET_PAGE_BITS)
89f9919116SEric Blake #  endif
9086e1eff8SEmilio G. Cota # endif
9186e1eff8SEmilio G. Cota 
92*708906dcSPhilippe Mathieu-Daudé #endif /* CONFIG_SOFTMMU && CONFIG_TCG */
934cb884e9SFabiano Rosas 
94*708906dcSPhilippe Mathieu-Daudé #if defined(CONFIG_SOFTMMU)
95ace41090SPeter Maydell /*
9625d3ec58SRichard Henderson  * The full TLB entry, which is not accessed by generated TCG code,
9725d3ec58SRichard Henderson  * so the layout is not as critical as that of CPUTLBEntry. This is
9825d3ec58SRichard Henderson  * also why we don't want to combine the two structs.
9925d3ec58SRichard Henderson  */
10025d3ec58SRichard Henderson typedef struct CPUTLBEntryFull {
10125d3ec58SRichard Henderson     /*
10225d3ec58SRichard Henderson      * @xlat_section contains:
103ace41090SPeter Maydell      *  - in the lower TARGET_PAGE_BITS, a physical section number
104ace41090SPeter Maydell      *  - with the lower TARGET_PAGE_BITS masked off, an offset which
105ace41090SPeter Maydell      *    must be added to the virtual address to obtain:
106ace41090SPeter Maydell      *     + the ram_addr_t of the target RAM (if the physical section
107ace41090SPeter Maydell      *       number is PHYS_SECTION_NOTDIRTY or PHYS_SECTION_ROM)
108ace41090SPeter Maydell      *     + the offset within the target MemoryRegion (otherwise)
109ace41090SPeter Maydell      */
11025d3ec58SRichard Henderson     hwaddr xlat_section;
11140473689SRichard Henderson 
11240473689SRichard Henderson     /*
11340473689SRichard Henderson      * @phys_addr contains the physical address in the address space
11440473689SRichard Henderson      * given by cpu_asidx_from_attrs(cpu, @attrs).
11540473689SRichard Henderson      */
11640473689SRichard Henderson     hwaddr phys_addr;
11740473689SRichard Henderson 
11840473689SRichard Henderson     /* @attrs contains the memory transaction attributes for the page. */
119fadc1cbeSPeter Maydell     MemTxAttrs attrs;
12040473689SRichard Henderson 
12140473689SRichard Henderson     /* @prot contains the complete protections for the page. */
12240473689SRichard Henderson     uint8_t prot;
12340473689SRichard Henderson 
12440473689SRichard Henderson     /* @lg_page_size contains the log2 of the page size. */
12540473689SRichard Henderson     uint8_t lg_page_size;
1268c6953cfSRichard Henderson 
1278c6953cfSRichard Henderson     /*
1288c6953cfSRichard Henderson      * Allow target-specific additions to this structure.
1298c6953cfSRichard Henderson      * This may be used to cache items from the guest cpu
1308c6953cfSRichard Henderson      * page tables for later use by the implementation.
1318c6953cfSRichard Henderson      */
1328c6953cfSRichard Henderson #ifdef TARGET_PAGE_ENTRY_EXTRA
1338c6953cfSRichard Henderson     TARGET_PAGE_ENTRY_EXTRA
1348c6953cfSRichard Henderson #endif
13525d3ec58SRichard Henderson } CPUTLBEntryFull;
136*708906dcSPhilippe Mathieu-Daudé #endif /* CONFIG_SOFTMMU */
137e469b22fSPeter Maydell 
138*708906dcSPhilippe Mathieu-Daudé #if defined(CONFIG_SOFTMMU) && defined(CONFIG_TCG)
139a40ec84eSRichard Henderson /*
140a40ec84eSRichard Henderson  * Data elements that are per MMU mode, minus the bits accessed by
141a40ec84eSRichard Henderson  * the TCG fast path.
142a40ec84eSRichard Henderson  */
1431308e026SRichard Henderson typedef struct CPUTLBDesc {
1441308e026SRichard Henderson     /*
1451308e026SRichard Henderson      * Describe a region covering all of the large pages allocated
1461308e026SRichard Henderson      * into the tlb.  When any page within this region is flushed,
1471308e026SRichard Henderson      * we must flush the entire tlb.  The region is matched if
1481308e026SRichard Henderson      * (addr & large_page_mask) == large_page_addr.
1491308e026SRichard Henderson      */
1501308e026SRichard Henderson     target_ulong large_page_addr;
1511308e026SRichard Henderson     target_ulong large_page_mask;
15279e42085SRichard Henderson     /* host time (in ns) at the beginning of the time window */
15379e42085SRichard Henderson     int64_t window_begin_ns;
15479e42085SRichard Henderson     /* maximum number of entries observed in the window */
15579e42085SRichard Henderson     size_t window_max_entries;
156a40ec84eSRichard Henderson     size_t n_used_entries;
157d5363e58SRichard Henderson     /* The next index to use in the tlb victim table.  */
158d5363e58SRichard Henderson     size_t vindex;
159a40ec84eSRichard Henderson     /* The tlb victim table, in two parts.  */
160a40ec84eSRichard Henderson     CPUTLBEntry vtable[CPU_VTLB_SIZE];
16125d3ec58SRichard Henderson     CPUTLBEntryFull vfulltlb[CPU_VTLB_SIZE];
16225d3ec58SRichard Henderson     CPUTLBEntryFull *fulltlb;
1631308e026SRichard Henderson } CPUTLBDesc;
1641308e026SRichard Henderson 
16553d28455SRichard Henderson /*
16653d28455SRichard Henderson  * Data elements that are shared between all MMU modes.
16753d28455SRichard Henderson  */
16853d28455SRichard Henderson typedef struct CPUTLBCommon {
169a40ec84eSRichard Henderson     /* Serialize updates to f.table and d.vtable, and others as noted. */
17053d28455SRichard Henderson     QemuSpin lock;
17160a2ad7dSRichard Henderson     /*
1723d1523ceSRichard Henderson      * Within dirty, for each bit N, modifications have been made to
1733d1523ceSRichard Henderson      * mmu_idx N since the last time that mmu_idx was flushed.
1743d1523ceSRichard Henderson      * Protected by tlb_c.lock.
1753d1523ceSRichard Henderson      */
1763d1523ceSRichard Henderson     uint16_t dirty;
177e09de0a2SRichard Henderson     /*
178e09de0a2SRichard Henderson      * Statistics.  These are not lock protected, but are read and
179e09de0a2SRichard Henderson      * written atomically.  This allows the monitor to print a snapshot
180e09de0a2SRichard Henderson      * of the stats without interfering with the cpu.
181e09de0a2SRichard Henderson      */
182e09de0a2SRichard Henderson     size_t full_flush_count;
183e09de0a2SRichard Henderson     size_t part_flush_count;
184e09de0a2SRichard Henderson     size_t elide_flush_count;
18553d28455SRichard Henderson } CPUTLBCommon;
18653d28455SRichard Henderson 
18753d28455SRichard Henderson /*
188a40ec84eSRichard Henderson  * The entire softmmu tlb, for all MMU modes.
18953d28455SRichard Henderson  * The meaning of each of the MMU modes is defined in the target code.
190269bd5d8SRichard Henderson  * Since this is placed within CPUNegativeOffsetState, the smallest
191269bd5d8SRichard Henderson  * negative offsets are at the end of the struct.
19253d28455SRichard Henderson  */
193e6d86bedSEmilio G. Cota 
194a40ec84eSRichard Henderson typedef struct CPUTLB {
195a40ec84eSRichard Henderson     CPUTLBCommon c;
196269bd5d8SRichard Henderson     CPUTLBDesc d[NB_MMU_MODES];
197269bd5d8SRichard Henderson     CPUTLBDescFast f[NB_MMU_MODES];
198a40ec84eSRichard Henderson } CPUTLB;
199a40ec84eSRichard Henderson 
200022c62cbSPaolo Bonzini #else
201022c62cbSPaolo Bonzini 
202269bd5d8SRichard Henderson typedef struct CPUTLB { } CPUTLB;
203022c62cbSPaolo Bonzini 
204*708906dcSPhilippe Mathieu-Daudé #endif /* CONFIG_SOFTMMU && CONFIG_TCG */
205022c62cbSPaolo Bonzini 
2065b146dc7SRichard Henderson /*
2071eb21c42SAlex Bennée  * This structure must be placed in ArchCPU immediately
2085b146dc7SRichard Henderson  * before CPUArchState, as a field named "neg".
2095b146dc7SRichard Henderson  */
2105b146dc7SRichard Henderson typedef struct CPUNegativeOffsetState {
211269bd5d8SRichard Henderson     CPUTLB tlb;
2125e140196SRichard Henderson     IcountDecr icount_decr;
2135b146dc7SRichard Henderson } CPUNegativeOffsetState;
2145b146dc7SRichard Henderson 
215022c62cbSPaolo Bonzini #endif
216