1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/set_memory.h>
3 #include <linux/ptdump.h>
4 #include <linux/seq_file.h>
5 #include <linux/debugfs.h>
7 #include <linux/kfence.h>
8 #include <linux/kasan.h>
9 #include <asm/ptdump.h>
10 #include <asm/kasan.h>
11 #include <asm/nospec-branch.h>
12 #include <asm/sections.h>
14 static unsigned long max_addr;
17 unsigned long start_address;
21 enum address_markers_idx {
22 IDENTITY_BEFORE_NR = 0,
23 IDENTITY_BEFORE_END_NR,
33 IDENTITY_AFTER_END_NR,
35 KASAN_SHADOW_START_NR,
46 static struct addr_marker address_markers[] = {
47 [IDENTITY_BEFORE_NR] = {0, "Identity Mapping Start"},
48 [IDENTITY_BEFORE_END_NR] = {(unsigned long)_stext, "Identity Mapping End"},
49 [AMODE31_START_NR] = {0, "Amode31 Area Start"},
50 [AMODE31_END_NR] = {0, "Amode31 Area End"},
51 [KERNEL_START_NR] = {(unsigned long)_stext, "Kernel Image Start"},
52 [KERNEL_END_NR] = {(unsigned long)_end, "Kernel Image End"},
54 [KFENCE_START_NR] = {0, "KFence Pool Start"},
55 [KFENCE_END_NR] = {0, "KFence Pool End"},
57 [IDENTITY_AFTER_NR] = {(unsigned long)_end, "Identity Mapping Start"},
58 [IDENTITY_AFTER_END_NR] = {0, "Identity Mapping End"},
60 [KASAN_SHADOW_START_NR] = {KASAN_SHADOW_START, "Kasan Shadow Start"},
61 [KASAN_SHADOW_END_NR] = {KASAN_SHADOW_END, "Kasan Shadow End"},
63 [VMEMMAP_NR] = {0, "vmemmap Area Start"},
64 [VMEMMAP_END_NR] = {0, "vmemmap Area End"},
65 [VMALLOC_NR] = {0, "vmalloc Area Start"},
66 [VMALLOC_END_NR] = {0, "vmalloc Area End"},
67 [MODULES_NR] = {0, "Modules Area Start"},
68 [MODULES_END_NR] = {0, "Modules Area End"},
73 struct ptdump_state ptdump;
76 unsigned int current_prot;
78 unsigned long wx_pages;
79 unsigned long start_address;
80 const struct addr_marker *marker;
83 #define pt_dump_seq_printf(m, fmt, args...) \
85 struct seq_file *__m = (m); \
88 seq_printf(__m, fmt, ##args); \
91 #define pt_dump_seq_puts(m, fmt) \
93 struct seq_file *__m = (m); \
96 seq_printf(__m, fmt); \
99 static void print_prot(struct seq_file *m, unsigned int pr, int level)
101 static const char * const level_name[] =
102 { "ASCE", "PGD", "PUD", "PMD", "PTE" };
104 pt_dump_seq_printf(m, "%s ", level_name[level]);
105 if (pr & _PAGE_INVALID) {
106 pt_dump_seq_printf(m, "I\n");
109 pt_dump_seq_puts(m, (pr & _PAGE_PROTECT) ? "RO " : "RW ");
110 pt_dump_seq_puts(m, (pr & _PAGE_NOEXEC) ? "NX\n" : "X\n");
113 static void note_prot_wx(struct pg_state *st, unsigned long addr)
115 #ifdef CONFIG_DEBUG_WX
118 if (st->current_prot & _PAGE_INVALID)
120 if (st->current_prot & _PAGE_PROTECT)
122 if (st->current_prot & _PAGE_NOEXEC)
125 * The first lowcore page is W+X if spectre mitigations are using
126 * trampolines or the BEAR enhancements facility is not installed,
127 * in which case we have two lpswe instructions in lowcore that need
130 if (addr == PAGE_SIZE && (nospec_uses_trampoline() || !static_key_enabled(&cpu_has_bear)))
132 WARN_ONCE(1, "s390/mm: Found insecure W+X mapping at address %pS\n",
133 (void *)st->start_address);
134 st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
135 #endif /* CONFIG_DEBUG_WX */
138 static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, u64 val)
140 int width = sizeof(unsigned long) * 2;
141 static const char units[] = "KMGTPE";
142 const char *unit = units;
148 st = container_of(pt_st, struct pg_state, ptdump);
150 prot = val & (_PAGE_PROTECT | _PAGE_NOEXEC);
151 if (level == 4 && (val & _PAGE_INVALID))
152 prot = _PAGE_INVALID;
153 /* For pmd_none() & friends val gets passed as zero. */
154 if (level != 4 && !val)
155 prot = _PAGE_INVALID;
156 /* Final flush from generic code. */
159 if (st->level == -1) {
160 pt_dump_seq_printf(m, "---[ %s ]---\n", st->marker->name);
161 st->start_address = addr;
162 st->current_prot = prot;
164 } else if (prot != st->current_prot || level != st->level ||
165 addr >= st->marker[1].start_address) {
166 note_prot_wx(st, addr);
167 pt_dump_seq_printf(m, "0x%0*lx-0x%0*lx ",
168 width, st->start_address,
170 delta = (addr - st->start_address) >> 10;
171 while (!(delta & 0x3ff) && unit[1]) {
175 pt_dump_seq_printf(m, "%9lu%c ", delta, *unit);
176 print_prot(m, st->current_prot, st->level);
177 while (addr >= st->marker[1].start_address) {
179 pt_dump_seq_printf(m, "---[ %s ]---\n", st->marker->name);
181 st->start_address = addr;
182 st->current_prot = prot;
187 #ifdef CONFIG_DEBUG_WX
188 void ptdump_check_wx(void)
190 struct pg_state st = {
192 .note_page = note_page,
193 .range = (struct ptdump_range[]) {
194 {.start = 0, .end = max_addr},
195 {.start = 0, .end = 0},
204 .marker = (struct addr_marker[]) {
205 { .start_address = 0, .name = NULL},
206 { .start_address = -1, .name = NULL},
212 ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
214 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found\n", st.wx_pages);
216 pr_info("Checked W+X mappings: passed, no %sW+X pages found\n",
217 (nospec_uses_trampoline() || !static_key_enabled(&cpu_has_bear)) ?
220 #endif /* CONFIG_DEBUG_WX */
222 #ifdef CONFIG_PTDUMP_DEBUGFS
223 static int ptdump_show(struct seq_file *m, void *v)
225 struct pg_state st = {
227 .note_page = note_page,
228 .range = (struct ptdump_range[]) {
229 {.start = 0, .end = max_addr},
230 {.start = 0, .end = 0},
239 .marker = address_markers,
243 mutex_lock(&cpa_mutex);
244 ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
245 mutex_unlock(&cpa_mutex);
249 DEFINE_SHOW_ATTRIBUTE(ptdump);
250 #endif /* CONFIG_PTDUMP_DEBUGFS */
253 * Heapsort from lib/sort.c is not a stable sorting algorithm, do a simple
254 * insertion sort to preserve the original order of markers with the same
257 static void sort_address_markers(void)
259 struct addr_marker tmp;
262 for (i = 1; i < ARRAY_SIZE(address_markers) - 1; i++) {
263 tmp = address_markers[i];
264 for (j = i - 1; j >= 0 && address_markers[j].start_address > tmp.start_address; j--)
265 address_markers[j + 1] = address_markers[j];
266 address_markers[j + 1] = tmp;
270 static int pt_dump_init(void)
273 unsigned long kfence_start = (unsigned long)__kfence_pool;
276 * Figure out the maximum virtual address being accessible with the
277 * kernel ASCE. We need this to keep the page table walker functions
278 * from accessing non-existent entries.
280 max_addr = (S390_lowcore.kernel_asce & _REGION_ENTRY_TYPE_MASK) >> 2;
281 max_addr = 1UL << (max_addr * 11 + 31);
282 address_markers[IDENTITY_AFTER_END_NR].start_address = ident_map_size;
283 address_markers[AMODE31_START_NR].start_address = __samode31;
284 address_markers[AMODE31_END_NR].start_address = __eamode31;
285 address_markers[MODULES_NR].start_address = MODULES_VADDR;
286 address_markers[MODULES_END_NR].start_address = MODULES_END;
287 address_markers[VMEMMAP_NR].start_address = (unsigned long) vmemmap;
288 address_markers[VMEMMAP_END_NR].start_address = (unsigned long)vmemmap + vmemmap_size;
289 address_markers[VMALLOC_NR].start_address = VMALLOC_START;
290 address_markers[VMALLOC_END_NR].start_address = VMALLOC_END;
292 address_markers[KFENCE_START_NR].start_address = kfence_start;
293 address_markers[KFENCE_END_NR].start_address = kfence_start + KFENCE_POOL_SIZE;
295 sort_address_markers();
296 #ifdef CONFIG_PTDUMP_DEBUGFS
297 debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops);
298 #endif /* CONFIG_PTDUMP_DEBUGFS */
301 device_initcall(pt_dump_init);