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,
31 IDENTITY_AFTER_END_NR,
33 KASAN_SHADOW_START_NR,
44 static struct addr_marker address_markers[] = {
45 [IDENTITY_BEFORE_NR] = {0, "Identity Mapping Start"},
46 [IDENTITY_BEFORE_END_NR] = {(unsigned long)_stext, "Identity Mapping End"},
47 [KERNEL_START_NR] = {(unsigned long)_stext, "Kernel Image Start"},
48 [KERNEL_END_NR] = {(unsigned long)_end, "Kernel Image End"},
50 [KFENCE_START_NR] = {0, "KFence Pool Start"},
51 [KFENCE_END_NR] = {0, "KFence Pool End"},
53 [IDENTITY_AFTER_NR] = {(unsigned long)_end, "Identity Mapping Start"},
54 [IDENTITY_AFTER_END_NR] = {0, "Identity Mapping End"},
56 [KASAN_SHADOW_START_NR] = {KASAN_SHADOW_START, "Kasan Shadow Start"},
57 [KASAN_SHADOW_END_NR] = {KASAN_SHADOW_END, "Kasan Shadow End"},
59 [VMEMMAP_NR] = {0, "vmemmap Area Start"},
60 [VMEMMAP_END_NR] = {0, "vmemmap Area End"},
61 [VMALLOC_NR] = {0, "vmalloc Area Start"},
62 [VMALLOC_END_NR] = {0, "vmalloc Area End"},
63 [MODULES_NR] = {0, "Modules Area Start"},
64 [MODULES_END_NR] = {0, "Modules Area End"},
69 struct ptdump_state ptdump;
72 unsigned int current_prot;
74 unsigned long wx_pages;
75 unsigned long start_address;
76 const struct addr_marker *marker;
79 #define pt_dump_seq_printf(m, fmt, args...) \
81 struct seq_file *__m = (m); \
84 seq_printf(__m, fmt, ##args); \
87 #define pt_dump_seq_puts(m, fmt) \
89 struct seq_file *__m = (m); \
92 seq_printf(__m, fmt); \
95 static void print_prot(struct seq_file *m, unsigned int pr, int level)
97 static const char * const level_name[] =
98 { "ASCE", "PGD", "PUD", "PMD", "PTE" };
100 pt_dump_seq_printf(m, "%s ", level_name[level]);
101 if (pr & _PAGE_INVALID) {
102 pt_dump_seq_printf(m, "I\n");
105 pt_dump_seq_puts(m, (pr & _PAGE_PROTECT) ? "RO " : "RW ");
106 pt_dump_seq_puts(m, (pr & _PAGE_NOEXEC) ? "NX\n" : "X\n");
109 static void note_prot_wx(struct pg_state *st, unsigned long addr)
111 #ifdef CONFIG_DEBUG_WX
114 if (st->current_prot & _PAGE_INVALID)
116 if (st->current_prot & _PAGE_PROTECT)
118 if (st->current_prot & _PAGE_NOEXEC)
121 * The first lowcore page is W+X if spectre mitigations are using
122 * trampolines or the BEAR enhancements facility is not installed,
123 * in which case we have two lpswe instructions in lowcore that need
126 if (addr == PAGE_SIZE && (nospec_uses_trampoline() || !static_key_enabled(&cpu_has_bear)))
128 WARN_ONCE(1, "s390/mm: Found insecure W+X mapping at address %pS\n",
129 (void *)st->start_address);
130 st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
131 #endif /* CONFIG_DEBUG_WX */
134 static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, u64 val)
136 int width = sizeof(unsigned long) * 2;
137 static const char units[] = "KMGTPE";
138 const char *unit = units;
144 st = container_of(pt_st, struct pg_state, ptdump);
146 prot = val & (_PAGE_PROTECT | _PAGE_NOEXEC);
147 if (level == 4 && (val & _PAGE_INVALID))
148 prot = _PAGE_INVALID;
149 /* For pmd_none() & friends val gets passed as zero. */
150 if (level != 4 && !val)
151 prot = _PAGE_INVALID;
152 /* Final flush from generic code. */
155 if (st->level == -1) {
156 pt_dump_seq_printf(m, "---[ %s ]---\n", st->marker->name);
157 st->start_address = addr;
158 st->current_prot = prot;
160 } else if (prot != st->current_prot || level != st->level ||
161 addr >= st->marker[1].start_address) {
162 note_prot_wx(st, addr);
163 pt_dump_seq_printf(m, "0x%0*lx-0x%0*lx ",
164 width, st->start_address,
166 delta = (addr - st->start_address) >> 10;
167 while (!(delta & 0x3ff) && unit[1]) {
171 pt_dump_seq_printf(m, "%9lu%c ", delta, *unit);
172 print_prot(m, st->current_prot, st->level);
173 while (addr >= st->marker[1].start_address) {
175 pt_dump_seq_printf(m, "---[ %s ]---\n", st->marker->name);
177 st->start_address = addr;
178 st->current_prot = prot;
183 #ifdef CONFIG_DEBUG_WX
184 void ptdump_check_wx(void)
186 struct pg_state st = {
188 .note_page = note_page,
189 .range = (struct ptdump_range[]) {
190 {.start = 0, .end = max_addr},
191 {.start = 0, .end = 0},
200 .marker = (struct addr_marker[]) {
201 { .start_address = 0, .name = NULL},
202 { .start_address = -1, .name = NULL},
208 ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
210 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found\n", st.wx_pages);
212 pr_info("Checked W+X mappings: passed, no %sW+X pages found\n",
213 (nospec_uses_trampoline() || !static_key_enabled(&cpu_has_bear)) ?
216 #endif /* CONFIG_DEBUG_WX */
218 #ifdef CONFIG_PTDUMP_DEBUGFS
219 static int ptdump_show(struct seq_file *m, void *v)
221 struct pg_state st = {
223 .note_page = note_page,
224 .range = (struct ptdump_range[]) {
225 {.start = 0, .end = max_addr},
226 {.start = 0, .end = 0},
235 .marker = address_markers,
239 mutex_lock(&cpa_mutex);
240 ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
241 mutex_unlock(&cpa_mutex);
245 DEFINE_SHOW_ATTRIBUTE(ptdump);
246 #endif /* CONFIG_PTDUMP_DEBUGFS */
249 * Heapsort from lib/sort.c is not a stable sorting algorithm, do a simple
250 * insertion sort to preserve the original order of markers with the same
253 static void sort_address_markers(void)
255 struct addr_marker tmp;
258 for (i = 1; i < ARRAY_SIZE(address_markers) - 1; i++) {
259 tmp = address_markers[i];
260 for (j = i - 1; j >= 0 && address_markers[j].start_address > tmp.start_address; j--)
261 address_markers[j + 1] = address_markers[j];
262 address_markers[j + 1] = tmp;
266 static int pt_dump_init(void)
269 unsigned long kfence_start = (unsigned long)__kfence_pool;
272 * Figure out the maximum virtual address being accessible with the
273 * kernel ASCE. We need this to keep the page table walker functions
274 * from accessing non-existent entries.
276 max_addr = (S390_lowcore.kernel_asce & _REGION_ENTRY_TYPE_MASK) >> 2;
277 max_addr = 1UL << (max_addr * 11 + 31);
278 address_markers[IDENTITY_AFTER_END_NR].start_address = ident_map_size;
279 address_markers[MODULES_NR].start_address = MODULES_VADDR;
280 address_markers[MODULES_END_NR].start_address = MODULES_END;
281 address_markers[VMEMMAP_NR].start_address = (unsigned long) vmemmap;
282 address_markers[VMEMMAP_END_NR].start_address = (unsigned long)vmemmap + vmemmap_size;
283 address_markers[VMALLOC_NR].start_address = VMALLOC_START;
284 address_markers[VMALLOC_END_NR].start_address = VMALLOC_END;
286 address_markers[KFENCE_START_NR].start_address = kfence_start;
287 address_markers[KFENCE_END_NR].start_address = kfence_start + KFENCE_POOL_SIZE;
289 sort_address_markers();
290 #ifdef CONFIG_PTDUMP_DEBUGFS
291 debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops);
292 #endif /* CONFIG_PTDUMP_DEBUGFS */
295 device_initcall(pt_dump_init);