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/sections.h>
13 static unsigned long max_addr;
16 unsigned long start_address;
20 enum address_markers_idx {
21 IDENTITY_BEFORE_NR = 0,
22 IDENTITY_BEFORE_END_NR,
30 IDENTITY_AFTER_END_NR,
32 KASAN_SHADOW_START_NR,
43 static struct addr_marker address_markers[] = {
44 [IDENTITY_BEFORE_NR] = {0, "Identity Mapping Start"},
45 [IDENTITY_BEFORE_END_NR] = {(unsigned long)_stext, "Identity Mapping End"},
46 [KERNEL_START_NR] = {(unsigned long)_stext, "Kernel Image Start"},
47 [KERNEL_END_NR] = {(unsigned long)_end, "Kernel Image End"},
49 [KFENCE_START_NR] = {0, "KFence Pool Start"},
50 [KFENCE_END_NR] = {0, "KFence Pool End"},
52 [IDENTITY_AFTER_NR] = {(unsigned long)_end, "Identity Mapping Start"},
53 [IDENTITY_AFTER_END_NR] = {0, "Identity Mapping End"},
55 [KASAN_SHADOW_START_NR] = {KASAN_SHADOW_START, "Kasan Shadow Start"},
56 [KASAN_SHADOW_END_NR] = {KASAN_SHADOW_END, "Kasan Shadow End"},
58 [VMEMMAP_NR] = {0, "vmemmap Area Start"},
59 [VMEMMAP_END_NR] = {0, "vmemmap Area End"},
60 [VMALLOC_NR] = {0, "vmalloc Area Start"},
61 [VMALLOC_END_NR] = {0, "vmalloc Area End"},
62 [MODULES_NR] = {0, "Modules Area Start"},
63 [MODULES_END_NR] = {0, "Modules Area End"},
68 struct ptdump_state ptdump;
71 unsigned int current_prot;
73 unsigned long wx_pages;
74 unsigned long start_address;
75 const struct addr_marker *marker;
78 #define pt_dump_seq_printf(m, fmt, args...) \
80 struct seq_file *__m = (m); \
83 seq_printf(__m, fmt, ##args); \
86 #define pt_dump_seq_puts(m, fmt) \
88 struct seq_file *__m = (m); \
91 seq_printf(__m, fmt); \
94 static void print_prot(struct seq_file *m, unsigned int pr, int level)
96 static const char * const level_name[] =
97 { "ASCE", "PGD", "PUD", "PMD", "PTE" };
99 pt_dump_seq_printf(m, "%s ", level_name[level]);
100 if (pr & _PAGE_INVALID) {
101 pt_dump_seq_printf(m, "I\n");
104 pt_dump_seq_puts(m, (pr & _PAGE_PROTECT) ? "RO " : "RW ");
105 pt_dump_seq_puts(m, (pr & _PAGE_NOEXEC) ? "NX\n" : "X\n");
108 static void note_prot_wx(struct pg_state *st, unsigned long addr)
110 #ifdef CONFIG_DEBUG_WX
113 if (st->current_prot & _PAGE_INVALID)
115 if (st->current_prot & _PAGE_PROTECT)
117 if (st->current_prot & _PAGE_NOEXEC)
119 /* The first lowcore page is currently still W+X. */
120 if (addr == PAGE_SIZE)
122 WARN_ONCE(1, "s390/mm: Found insecure W+X mapping at address %pS\n",
123 (void *)st->start_address);
124 st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
125 #endif /* CONFIG_DEBUG_WX */
128 static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, u64 val)
130 int width = sizeof(unsigned long) * 2;
131 static const char units[] = "KMGTPE";
132 const char *unit = units;
138 st = container_of(pt_st, struct pg_state, ptdump);
140 prot = val & (_PAGE_PROTECT | _PAGE_NOEXEC);
141 if (level == 4 && (val & _PAGE_INVALID))
142 prot = _PAGE_INVALID;
143 /* For pmd_none() & friends val gets passed as zero. */
144 if (level != 4 && !val)
145 prot = _PAGE_INVALID;
146 /* Final flush from generic code. */
149 if (st->level == -1) {
150 pt_dump_seq_printf(m, "---[ %s ]---\n", st->marker->name);
151 st->start_address = addr;
152 st->current_prot = prot;
154 } else if (prot != st->current_prot || level != st->level ||
155 addr >= st->marker[1].start_address) {
156 note_prot_wx(st, addr);
157 pt_dump_seq_printf(m, "0x%0*lx-0x%0*lx ",
158 width, st->start_address,
160 delta = (addr - st->start_address) >> 10;
161 while (!(delta & 0x3ff) && unit[1]) {
165 pt_dump_seq_printf(m, "%9lu%c ", delta, *unit);
166 print_prot(m, st->current_prot, st->level);
167 while (addr >= st->marker[1].start_address) {
169 pt_dump_seq_printf(m, "---[ %s ]---\n", st->marker->name);
171 st->start_address = addr;
172 st->current_prot = prot;
177 #ifdef CONFIG_DEBUG_WX
178 void ptdump_check_wx(void)
180 struct pg_state st = {
182 .note_page = note_page,
183 .range = (struct ptdump_range[]) {
184 {.start = 0, .end = max_addr},
185 {.start = 0, .end = 0},
194 .marker = (struct addr_marker[]) {
195 { .start_address = 0, .name = NULL},
196 { .start_address = -1, .name = NULL},
202 ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
204 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found\n", st.wx_pages);
206 pr_info("Checked W+X mappings: passed, no unexpected W+X pages found\n");
208 #endif /* CONFIG_DEBUG_WX */
210 #ifdef CONFIG_PTDUMP_DEBUGFS
211 static int ptdump_show(struct seq_file *m, void *v)
213 struct pg_state st = {
215 .note_page = note_page,
216 .range = (struct ptdump_range[]) {
217 {.start = 0, .end = max_addr},
218 {.start = 0, .end = 0},
227 .marker = address_markers,
231 mutex_lock(&cpa_mutex);
232 ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
233 mutex_unlock(&cpa_mutex);
237 DEFINE_SHOW_ATTRIBUTE(ptdump);
238 #endif /* CONFIG_PTDUMP_DEBUGFS */
241 * Heapsort from lib/sort.c is not a stable sorting algorithm, do a simple
242 * insertion sort to preserve the original order of markers with the same
245 static void sort_address_markers(void)
247 struct addr_marker tmp;
250 for (i = 1; i < ARRAY_SIZE(address_markers) - 1; i++) {
251 tmp = address_markers[i];
252 for (j = i - 1; j >= 0 && address_markers[j].start_address > tmp.start_address; j--)
253 address_markers[j + 1] = address_markers[j];
254 address_markers[j + 1] = tmp;
258 static int pt_dump_init(void)
261 unsigned long kfence_start = (unsigned long)__kfence_pool;
264 * Figure out the maximum virtual address being accessible with the
265 * kernel ASCE. We need this to keep the page table walker functions
266 * from accessing non-existent entries.
268 max_addr = (S390_lowcore.kernel_asce & _REGION_ENTRY_TYPE_MASK) >> 2;
269 max_addr = 1UL << (max_addr * 11 + 31);
270 address_markers[IDENTITY_AFTER_END_NR].start_address = ident_map_size;
271 address_markers[MODULES_NR].start_address = MODULES_VADDR;
272 address_markers[MODULES_END_NR].start_address = MODULES_END;
273 address_markers[VMEMMAP_NR].start_address = (unsigned long) vmemmap;
274 address_markers[VMEMMAP_END_NR].start_address = (unsigned long)vmemmap + vmemmap_size;
275 address_markers[VMALLOC_NR].start_address = VMALLOC_START;
276 address_markers[VMALLOC_END_NR].start_address = VMALLOC_END;
278 address_markers[KFENCE_START_NR].start_address = kfence_start;
279 address_markers[KFENCE_END_NR].start_address = kfence_start + KFENCE_POOL_SIZE;
281 sort_address_markers();
282 #ifdef CONFIG_PTDUMP_DEBUGFS
283 debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops);
284 #endif /* CONFIG_PTDUMP_DEBUGFS */
287 device_initcall(pt_dump_init);