ptdump.c 11.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
R
Rashmica Gupta 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright 2016, Rashmica Gupta, IBM Corp.
 *
 * This traverses the kernel pagetables and dumps the
 * information about the used sections of memory to
 * /sys/kernel/debug/kernel_pagetables.
 *
 * Derived from the arm64 implementation:
 * Copyright (c) 2014, The Linux Foundation, Laura Abbott.
 * (C) Copyright 2008 Intel Corporation, Arjan van de Ven.
 */
#include <linux/debugfs.h>
#include <linux/fs.h>
15
#include <linux/hugetlb.h>
R
Rashmica Gupta 已提交
16 17
#include <linux/io.h>
#include <linux/mm.h>
18
#include <linux/highmem.h>
R
Rashmica Gupta 已提交
19 20 21 22 23 24 25 26
#include <linux/sched.h>
#include <linux/seq_file.h>
#include <asm/fixmap.h>
#include <asm/pgtable.h>
#include <linux/const.h>
#include <asm/page.h>
#include <asm/pgalloc.h>

27
#include "ptdump.h"
28

R
Rashmica Gupta 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/*
 * To visualise what is happening,
 *
 *  - PTRS_PER_P** = how many entries there are in the corresponding P**
 *  - P**_SHIFT = how many bits of the address we use to index into the
 * corresponding P**
 *  - P**_SIZE is how much memory we can access through the table - not the
 * size of the table itself.
 * P**={PGD, PUD, PMD, PTE}
 *
 *
 * Each entry of the PGD points to a PUD. Each entry of a PUD points to a
 * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to
 * a page.
 *
 * In the case where there are only 3 levels, the PUD is folded into the
 * PGD: every PUD has only one entry which points to the PMD.
 *
 * The page dumper groups page table entries of the same type into a single
 * description. It uses pg_state to track the range information while
 * iterating over the PTE entries. When the continuity is broken it then
 * dumps out a description of the range - ie PTEs that are virtually contiguous
 * with the same PTE flags are chunked together. This is to make it clear how
 * different areas of the kernel virtual memory are used.
 *
 */
struct pg_state {
	struct seq_file *seq;
	const struct addr_marker *marker;
	unsigned long start_address;
59 60
	unsigned long start_pa;
	unsigned long last_pa;
R
Rashmica Gupta 已提交
61 62
	unsigned int level;
	u64 current_flags;
63 64
	bool check_wx;
	unsigned long wx_pages;
R
Rashmica Gupta 已提交
65 66 67 68 69 70 71 72 73 74 75
};

struct addr_marker {
	unsigned long start_address;
	const char *name;
};

static struct addr_marker address_markers[] = {
	{ 0,	"Start of kernel VM" },
	{ 0,	"vmalloc() Area" },
	{ 0,	"vmalloc() End" },
76
#ifdef CONFIG_PPC64
R
Rashmica Gupta 已提交
77 78 79 80 81 82 83
	{ 0,	"isa I/O start" },
	{ 0,	"isa I/O end" },
	{ 0,	"phb I/O start" },
	{ 0,	"phb I/O end" },
	{ 0,	"I/O remap start" },
	{ 0,	"I/O remap end" },
	{ 0,	"vmemmap start" },
84 85 86 87 88 89 90 91 92 93 94 95 96
#else
	{ 0,	"Early I/O remap start" },
	{ 0,	"Early I/O remap end" },
#ifdef CONFIG_NOT_COHERENT_CACHE
	{ 0,	"Consistent mem start" },
	{ 0,	"Consistent mem end" },
#endif
#ifdef CONFIG_HIGHMEM
	{ 0,	"Highmem PTEs start" },
	{ 0,	"Highmem PTEs end" },
#endif
	{ 0,	"Fixmap start" },
	{ 0,	"Fixmap end" },
97 98 99 100
#endif
#ifdef CONFIG_KASAN
	{ 0,	"kasan shadow mem start" },
	{ 0,	"kasan shadow mem end" },
101
#endif
R
Rashmica Gupta 已提交
102 103 104
	{ -1,	NULL },
};

105 106 107 108 109 110 111 112 113 114 115 116
#define pt_dump_seq_printf(m, fmt, args...)	\
({						\
	if (m)					\
		seq_printf(m, fmt, ##args);	\
})

#define pt_dump_seq_putc(m, c)		\
({					\
	if (m)				\
		seq_putc(m, c);		\
})

R
Rashmica Gupta 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
static void dump_flag_info(struct pg_state *st, const struct flag_info
		*flag, u64 pte, int num)
{
	unsigned int i;

	for (i = 0; i < num; i++, flag++) {
		const char *s = NULL;
		u64 val;

		/* flag not defined so don't check it */
		if (flag->mask == 0)
			continue;
		/* Some 'flags' are actually values */
		if (flag->is_val) {
			val = pte & flag->val;
			if (flag->shift)
				val = val >> flag->shift;
134
			pt_dump_seq_printf(st->seq, "  %s:%llx", flag->set, val);
R
Rashmica Gupta 已提交
135 136 137 138 139 140
		} else {
			if ((pte & flag->mask) == flag->val)
				s = flag->set;
			else
				s = flag->clear;
			if (s)
141
				pt_dump_seq_printf(st->seq, "  %s", s);
R
Rashmica Gupta 已提交
142 143 144 145
		}
		st->current_flags &= ~flag->mask;
	}
	if (st->current_flags != 0)
146
		pt_dump_seq_printf(st->seq, "  unknown flags:%llx", st->current_flags);
R
Rashmica Gupta 已提交
147 148 149 150 151 152 153 154
}

static void dump_addr(struct pg_state *st, unsigned long addr)
{
	static const char units[] = "KMGTPE";
	const char *unit = units;
	unsigned long delta;

155
#ifdef CONFIG_PPC64
156
#define REG		"0x%016lx"
157
#else
158
#define REG		"0x%08lx"
159
#endif
160

161
	pt_dump_seq_printf(st->seq, REG "-" REG " ", st->start_address, addr - 1);
162
	if (st->start_pa == st->last_pa && st->start_address + PAGE_SIZE != addr) {
163
		pt_dump_seq_printf(st->seq, "[" REG "]", st->start_pa);
164 165
		delta = PAGE_SIZE >> 10;
	} else {
166
		pt_dump_seq_printf(st->seq, " " REG " ", st->start_pa);
167 168
		delta = (addr - st->start_address) >> 10;
	}
R
Rashmica Gupta 已提交
169 170 171 172 173
	/* Work out what appropriate unit to use */
	while (!(delta & 1023) && unit[1]) {
		delta >>= 10;
		unit++;
	}
174
	pt_dump_seq_printf(st->seq, "%9lu%c", delta, *unit);
R
Rashmica Gupta 已提交
175 176 177

}

178 179
static void note_prot_wx(struct pg_state *st, unsigned long addr)
{
180
	if (!IS_ENABLED(CONFIG_PPC_DEBUG_WX) || !st->check_wx)
181 182 183 184 185 186 187 188 189 190 191
		return;

	if (!((st->current_flags & pgprot_val(PAGE_KERNEL_X)) == pgprot_val(PAGE_KERNEL_X)))
		return;

	WARN_ONCE(1, "powerpc/mm: Found insecure W+X mapping at address %p/%pS\n",
		  (void *)st->start_address, (void *)st->start_address);

	st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
}

R
Rashmica Gupta 已提交
192 193 194 195
static void note_page(struct pg_state *st, unsigned long addr,
	       unsigned int level, u64 val)
{
	u64 flag = val & pg_level[level].mask;
196 197
	u64 pa = val & PTE_RPN_MASK;

R
Rashmica Gupta 已提交
198 199 200 201 202
	/* At first no level is set */
	if (!st->level) {
		st->level = level;
		st->current_flags = flag;
		st->start_address = addr;
203 204
		st->start_pa = pa;
		st->last_pa = pa;
205
		pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
R
Rashmica Gupta 已提交
206 207 208 209 210 211
	/*
	 * Dump the section of virtual memory when:
	 *   - the PTE flags from one entry to the next differs.
	 *   - we change levels in the tree.
	 *   - the address is in a different section of memory and is thus
	 *   used for a different purpose, regardless of the flags.
212
	 *   - the pa of this page is not adjacent to the last inspected page
R
Rashmica Gupta 已提交
213 214
	 */
	} else if (flag != st->current_flags || level != st->level ||
215
		   addr >= st->marker[1].start_address ||
216 217
		   (pa != st->last_pa + PAGE_SIZE &&
		    (pa != st->start_pa || st->start_pa != st->last_pa))) {
R
Rashmica Gupta 已提交
218 219 220

		/* Check the PTE flags */
		if (st->current_flags) {
221
			note_prot_wx(st, addr);
R
Rashmica Gupta 已提交
222 223 224 225 226 227 228 229
			dump_addr(st, addr);

			/* Dump all the flags */
			if (pg_level[st->level].flag)
				dump_flag_info(st, pg_level[st->level].flag,
					  st->current_flags,
					  pg_level[st->level].num);

230
			pt_dump_seq_putc(st->seq, '\n');
R
Rashmica Gupta 已提交
231 232 233 234 235 236 237 238
		}

		/*
		 * Address indicates we have passed the end of the
		 * current section of virtual memory
		 */
		while (addr >= st->marker[1].start_address) {
			st->marker++;
239
			pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
R
Rashmica Gupta 已提交
240 241
		}
		st->start_address = addr;
242 243
		st->start_pa = pa;
		st->last_pa = pa;
R
Rashmica Gupta 已提交
244 245
		st->current_flags = flag;
		st->level = level;
246 247
	} else {
		st->last_pa = pa;
R
Rashmica Gupta 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	}
}

static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
{
	pte_t *pte = pte_offset_kernel(pmd, 0);
	unsigned long addr;
	unsigned int i;

	for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
		addr = start + i * PAGE_SIZE;
		note_page(st, addr, 4, pte_val(*pte));

	}
}

static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
{
	pmd_t *pmd = pmd_offset(pud, 0);
	unsigned long addr;
	unsigned int i;

	for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
		addr = start + i * PMD_SIZE;
272
		if (!pmd_none(*pmd) && !pmd_is_leaf(*pmd))
R
Rashmica Gupta 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
			/* pmd exists */
			walk_pte(st, pmd, addr);
		else
			note_page(st, addr, 3, pmd_val(*pmd));
	}
}

static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
{
	pud_t *pud = pud_offset(pgd, 0);
	unsigned long addr;
	unsigned int i;

	for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
		addr = start + i * PUD_SIZE;
288
		if (!pud_none(*pud) && !pud_is_leaf(*pud))
R
Rashmica Gupta 已提交
289 290 291 292 293 294 295 296 297 298
			/* pud exists */
			walk_pmd(st, pud, addr);
		else
			note_page(st, addr, 2, pud_val(*pud));
	}
}

static void walk_pagetables(struct pg_state *st)
{
	unsigned int i;
299 300
	unsigned long addr = st->start_address & PGDIR_MASK;
	pgd_t *pgd = pgd_offset_k(addr);
301

R
Rashmica Gupta 已提交
302 303 304 305
	/*
	 * Traverse the linux pagetable structure and dump pages that are in
	 * the hash pagetable.
	 */
306
	for (i = pgd_index(addr); i < PTRS_PER_PGD; i++, pgd++, addr += PGDIR_SIZE) {
307
		if (!pgd_none(*pgd) && !pgd_is_leaf(*pgd))
R
Rashmica Gupta 已提交
308 309 310 311 312 313 314 315 316
			/* pgd exists */
			walk_pud(st, pgd, addr);
		else
			note_page(st, addr, 1, pgd_val(*pgd));
	}
}

static void populate_markers(void)
{
317 318 319 320 321 322 323 324 325 326 327 328
	int i = 0;

	address_markers[i++].start_address = PAGE_OFFSET;
	address_markers[i++].start_address = VMALLOC_START;
	address_markers[i++].start_address = VMALLOC_END;
#ifdef CONFIG_PPC64
	address_markers[i++].start_address = ISA_IO_BASE;
	address_markers[i++].start_address = ISA_IO_END;
	address_markers[i++].start_address = PHB_IO_BASE;
	address_markers[i++].start_address = PHB_IO_END;
	address_markers[i++].start_address = IOREMAP_BASE;
	address_markers[i++].start_address = IOREMAP_END;
329
	/* What is the ifdef about? */
330
#ifdef CONFIG_PPC_BOOK3S_64
331
	address_markers[i++].start_address =  H_VMEMMAP_START;
R
Rashmica Gupta 已提交
332
#else
333 334 335 336 337 338 339 340 341 342 343 344 345
	address_markers[i++].start_address =  VMEMMAP_BASE;
#endif
#else /* !CONFIG_PPC64 */
	address_markers[i++].start_address = ioremap_bot;
	address_markers[i++].start_address = IOREMAP_TOP;
#ifdef CONFIG_NOT_COHERENT_CACHE
	address_markers[i++].start_address = IOREMAP_TOP;
	address_markers[i++].start_address = IOREMAP_TOP +
					     CONFIG_CONSISTENT_SIZE;
#endif
#ifdef CONFIG_HIGHMEM
	address_markers[i++].start_address = PKMAP_BASE;
	address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
R
Rashmica Gupta 已提交
346
#endif
347 348
	address_markers[i++].start_address = FIXADDR_START;
	address_markers[i++].start_address = FIXADDR_TOP;
349 350 351 352
#ifdef CONFIG_KASAN
	address_markers[i++].start_address = KASAN_SHADOW_START;
	address_markers[i++].start_address = KASAN_SHADOW_END;
#endif
353
#endif /* CONFIG_PPC64 */
R
Rashmica Gupta 已提交
354 355 356 357 358 359 360
}

static int ptdump_show(struct seq_file *m, void *v)
{
	struct pg_state st = {
		.seq = m,
		.marker = address_markers,
361
		.start_address = PAGE_OFFSET,
R
Rashmica Gupta 已提交
362
	};
363

364 365
#ifdef CONFIG_PPC64
	if (!radix_enabled())
366
		st.start_address = KERN_VIRT_START;
367
#endif
368

R
Rashmica Gupta 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
	/* Traverse kernel page tables */
	walk_pagetables(&st);
	note_page(&st, 0, 0, 0);
	return 0;
}


static int ptdump_open(struct inode *inode, struct file *file)
{
	return single_open(file, ptdump_show, NULL);
}

static const struct file_operations ptdump_fops = {
	.open		= ptdump_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static void build_pgtable_complete_mask(void)
{
	unsigned int i, j;

	for (i = 0; i < ARRAY_SIZE(pg_level); i++)
		if (pg_level[i].flag)
			for (j = 0; j < pg_level[i].num; j++)
				pg_level[i].mask |= pg_level[i].flag[j].mask;
}

398 399 400 401 402 403 404
#ifdef CONFIG_PPC_DEBUG_WX
void ptdump_check_wx(void)
{
	struct pg_state st = {
		.seq = NULL,
		.marker = address_markers,
		.check_wx = true,
405
		.start_address = PAGE_OFFSET,
406 407
	};

408 409
#ifdef CONFIG_PPC64
	if (!radix_enabled())
410
		st.start_address = KERN_VIRT_START;
411
#endif
412 413 414 415 416 417 418 419 420 421 422

	walk_pagetables(&st);

	if (st.wx_pages)
		pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found\n",
			st.wx_pages);
	else
		pr_info("Checked W+X mappings: passed, no W+X pages found\n");
}
#endif

R
Rashmica Gupta 已提交
423 424 425 426 427 428
static int ptdump_init(void)
{
	struct dentry *debugfs_file;

	populate_markers();
	build_pgtable_complete_mask();
429
	debugfs_file = debugfs_create_file("kernel_page_tables", 0400, NULL,
R
Rashmica Gupta 已提交
430 431 432 433
			NULL, &ptdump_fops);
	return debugfs_file ? 0 : -ENOMEM;
}
device_initcall(ptdump_init);