2 * Jailhouse, a Linux-based partitioning hypervisor
4 * Copyright (c) Siemens AG, 2013
7 * Jan Kiszka <jan.kiszka@siemens.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
13 #ifndef _JAILHOUSE_ASM_PAGING_H
14 #define _JAILHOUSE_ASM_PAGING_H
16 #include <jailhouse/types.h>
17 #include <jailhouse/utils.h>
18 #include <asm/processor.h>
19 #include <asm/sysregs.h>
22 #define PAGE_SIZE (1 << PAGE_SHIFT)
23 #define PAGE_MASK ~(PAGE_SIZE - 1)
24 #define PAGE_OFFS_MASK (PAGE_SIZE - 1)
26 #define MAX_PAGE_TABLE_LEVELS 3
29 * When T0SZ == 0 and SL0 == 0, the EL2 MMU starts the IPA->PA translation at
30 * the level 2 table. The second table is indexed by IPA[31:21], the third one
32 * This would allows to cover a 4GB memory map by using 4 concatenated level-2
33 * page tables and thus provide better table walk performances.
34 * For the moment, we will implement the first level for AArch32 using only
37 * TODO: implement larger PARange support for AArch32
39 #define ARM_CELL_ROOT_PT_SZ 1
41 #if MAX_PAGE_TABLE_LEVELS < 3
44 #define PADDR_OFF (14 - T0SZ)
45 #define L2_VADDR_MASK BIT_MASK(21, 17 + PADDR_OFF)
49 #define PADDR_OFF (5 - T0SZ)
50 #define L1_VADDR_MASK BIT_MASK(26 + PADDR_OFF, 30)
51 #define L2_VADDR_MASK BIT_MASK(29, 21)
54 #define L3_VADDR_MASK BIT_MASK(20, 12)
57 * Stage-1 and Stage-2 lower attributes.
58 * FIXME: The upper attributes (contiguous hint and XN) are not currently in
59 * use. If needed in the future, they should be shifted towards the lower word,
60 * since the core uses unsigned long to pass the flags.
61 * An arch-specific typedef for the flags as well as the addresses would be
63 * The contiguous bit is a hint that allows the PE to store blocks of 16 pages
64 * in the TLB. This may be a useful optimisation.
66 #define PTE_ACCESS_FLAG (0x1 << 10)
68 * When combining shareability attributes, the stage-1 ones prevail. So we can
69 * safely leave everything non-shareable at stage 2.
71 #define PTE_NON_SHAREABLE (0x0 << 8)
72 #define PTE_OUTER_SHAREABLE (0x2 << 8)
73 #define PTE_INNER_SHAREABLE (0x3 << 8)
75 #define PTE_MEMATTR(val) ((val) << 2)
76 #define PTE_FLAG_TERMINAL (0x1 << 1)
77 #define PTE_FLAG_VALID (0x1 << 0)
79 /* These bits differ in stage 1 and 2 translations */
80 #define S1_PTE_NG (0x1 << 11)
81 #define S1_PTE_ACCESS_RW (0x0 << 7)
82 #define S1_PTE_ACCESS_RO (0x1 << 7)
83 /* Res1 for EL2 stage-1 tables */
84 #define S1_PTE_ACCESS_EL0 (0x1 << 6)
86 #define S2_PTE_ACCESS_RO (0x1 << 6)
87 #define S2_PTE_ACCESS_WO (0x2 << 6)
88 #define S2_PTE_ACCESS_RW (0x3 << 6)
91 * Descriptor pointing to a page table
92 * (only for L1 and L2. L3 uses this encoding for terminal entries...)
94 #define PTE_TABLE_FLAGS 0x3
96 #define PTE_L1_BLOCK_ADDR_MASK BIT_MASK(39, 30)
97 #define PTE_L2_BLOCK_ADDR_MASK BIT_MASK(39, 21)
98 #define PTE_TABLE_ADDR_MASK BIT_MASK(39, 12)
99 #define PTE_PAGE_ADDR_MASK BIT_MASK(39, 12)
101 #define BLOCK_1G_VADDR_MASK BIT_MASK(29, 0)
102 #define BLOCK_2M_VADDR_MASK BIT_MASK(20, 0)
104 #define TTBR_MASK BIT_MASK(47, PADDR_OFF)
105 #define VTTBR_VMID_SHIFT 48
107 #define HTCR_RES1 ((1 << 31) | (1 << 23))
108 #define VTCR_RES1 ((1 << 31))
109 #define TCR_RGN_NON_CACHEABLE 0x0
110 #define TCR_RGN_WB_WA 0x1
111 #define TCR_RGN_WT 0x2
112 #define TCR_RGN_WB 0x3
113 #define TCR_NON_SHAREABLE 0x0
114 #define TCR_OUTER_SHAREABLE 0x2
115 #define TCR_INNER_SHAREABLE 0x3
117 #define TCR_SH0_SHIFT 12
118 #define TCR_ORGN0_SHIFT 10
119 #define TCR_IRGN0_SHIFT 8
120 #define TCR_SL0_SHIFT 6
121 #define TCR_S_SHIFT 4
124 * Hypervisor memory attribute indexes:
125 * 0: normal WB, RA, WA, non-transient
127 * 2: normal non-cacheable
130 #define DEFAULT_HMAIR0 0x004404ff
131 #define DEFAULT_HMAIR1 0x00000000
132 #define HMAIR_IDX_WBRAWA 0
133 #define HMAIR_IDX_DEV 1
134 #define HMAIR_IDX_NC 2
136 /* Stage 2 memory attributes (MemAttr[3:0]) */
137 #define S2_MEMATTR_OWBIWB 0xf
138 #define S2_MEMATTR_DEV 0x1
140 #define S1_PTE_FLAG_NORMAL PTE_MEMATTR(HMAIR_IDX_WBRAWA)
141 #define S1_PTE_FLAG_DEVICE PTE_MEMATTR(HMAIR_IDX_DEV)
142 #define S1_PTE_FLAG_UNCACHED PTE_MEMATTR(HMAIR_IDX_NC)
144 #define S2_PTE_FLAG_NORMAL PTE_MEMATTR(S2_MEMATTR_OWBIWB)
145 #define S2_PTE_FLAG_DEVICE PTE_MEMATTR(S2_MEMATTR_DEV)
147 #define S1_DEFAULT_FLAGS (PTE_FLAG_VALID | PTE_ACCESS_FLAG \
148 | S1_PTE_FLAG_NORMAL | PTE_INNER_SHAREABLE\
151 /* Macros used by the core, only for the EL2 stage-1 mappings */
152 #define PAGE_FLAG_DEVICE S1_PTE_FLAG_DEVICE
153 #define PAGE_DEFAULT_FLAGS (S1_DEFAULT_FLAGS | S1_PTE_ACCESS_RW)
154 #define PAGE_READONLY_FLAGS (S1_DEFAULT_FLAGS | S1_PTE_ACCESS_RO)
155 #define PAGE_PRESENT_FLAGS PTE_FLAG_VALID
156 #define PAGE_NONPRESENT_FLAGS 0
158 #define INVALID_PHYS_ADDR (~0UL)
160 #define REMAP_BASE 0x00100000UL
161 #define NUM_REMAP_BITMAP_PAGES 1
163 #define NUM_TEMPORARY_PAGES 16
167 typedef u64 *pt_entry_t;
169 extern unsigned int cpu_parange;
171 /* return the bits supported for the physical address range for this
172 * machine; in arch_paging_init this value will be kept in
173 * cpu_parange for later reference */
174 static inline unsigned int get_cpu_parange(void)
176 /* TODO: implement proper PARange support on AArch32 */
180 /* Only executed on hypervisor paging struct changes */
181 static inline void arch_paging_flush_page_tlbs(unsigned long page_addr)
184 * This instruction is UNDEF at EL1, but the whole TLB is invalidated
185 * before enabling the EL2 stage 1 MMU anyway.
188 arm_write_sysreg(TLBIMVAH, page_addr & PAGE_MASK);
191 extern unsigned int cache_line_size;
193 /* Used to clean the PAGE_MAP_COHERENT page table changes */
194 static inline void arch_paging_flush_cpu_caches(void *addr, long size)
197 /* Clean & invalidate by MVA to PoC */
198 arm_write_sysreg(DCCIMVAC, addr);
199 size -= cache_line_size;
200 addr += cache_line_size;
204 #endif /* !__ASSEMBLY__ */
206 #endif /* !_JAILHOUSE_ASM_PAGING_H */