ptdump.c 9.38 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
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>
16 17
#include <linux/io.h>
#include <linux/mm.h>
18
#include <linux/highmem.h>
19
#include <linux/ptdump.h>
20 21 22 23 24
#include <linux/sched.h>
#include <linux/seq_file.h>
#include <asm/fixmap.h>
#include <linux/const.h>
#include <asm/page.h>
25
#include <asm/hugetlb.h>
26

27 28
#include <mm/mmu_decl.h>

29
#include "ptdump.h"
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
/*
 * 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 {
58
	struct ptdump_state ptdump;
59 60 61
	struct seq_file *seq;
	const struct addr_marker *marker;
	unsigned long start_address;
62
	unsigned long start_pa;
63
	int level;
64
	u64 current_flags;
65 66
	bool check_wx;
	unsigned long wx_pages;
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" },
76 77 78 79
#ifdef MODULES_VADDR
	{ 0,	"modules start" },
	{ 0,	"modules end" },
#endif
80 81
	{ 0,	"vmalloc() Area" },
	{ 0,	"vmalloc() End" },
82
#ifdef CONFIG_PPC64
83 84 85 86 87 88 89
	{ 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" },
90 91 92 93 94 95 96 97 98
#else
	{ 0,	"Early I/O remap start" },
	{ 0,	"Early I/O remap end" },
#ifdef CONFIG_HIGHMEM
	{ 0,	"Highmem PTEs start" },
	{ 0,	"Highmem PTEs end" },
#endif
	{ 0,	"Fixmap start" },
	{ 0,	"Fixmap end" },
99 100 101 102
#endif
#ifdef CONFIG_KASAN
	{ 0,	"kasan shadow mem start" },
	{ 0,	"kasan shadow mem end" },
103
#endif
104 105 106
	{ -1,	NULL },
};

107 108 109 110 111
static struct ptdump_range ptdump_range[] __ro_after_init = {
	{TASK_SIZE_MAX, ~0UL},
	{0, 0}
};

112 113 114 115 116 117 118 119 120 121 122 123
#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);		\
})

124 125 126 127 128 129 130 131 132 133 134 135 136
void pt_dump_size(struct seq_file *m, unsigned long size)
{
	static const char units[] = "KMGTPE";
	const char *unit = units;

	/* Work out what appropriate unit to use */
	while (!(size & 1023) && unit[1]) {
		size >>= 10;
		unit++;
	}
	pt_dump_seq_printf(m, "%9lu%c ", size, *unit);
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
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;
154
			pt_dump_seq_printf(st->seq, "  %s:%llx", flag->set, val);
155 156 157 158 159 160
		} else {
			if ((pte & flag->mask) == flag->val)
				s = flag->set;
			else
				s = flag->clear;
			if (s)
161
				pt_dump_seq_printf(st->seq, "  %s", s);
162 163 164 165
		}
		st->current_flags &= ~flag->mask;
	}
	if (st->current_flags != 0)
166
		pt_dump_seq_printf(st->seq, "  unknown flags:%llx", st->current_flags);
167 168 169 170
}

static void dump_addr(struct pg_state *st, unsigned long addr)
{
171
#ifdef CONFIG_PPC64
172
#define REG		"0x%016lx"
173
#else
174
#define REG		"0x%08lx"
175
#endif
176

177
	pt_dump_seq_printf(st->seq, REG "-" REG " ", st->start_address, addr - 1);
178 179
	pt_dump_seq_printf(st->seq, " " REG " ", st->start_pa);
	pt_dump_size(st->seq, (addr - st->start_address) >> 10);
180 181
}

182 183
static void note_prot_wx(struct pg_state *st, unsigned long addr)
{
184 185
	pte_t pte = __pte(st->current_flags);

186
	if (!IS_ENABLED(CONFIG_PPC_DEBUG_WX) || !st->check_wx)
187 188
		return;

189
	if (!pte_write(pte) || !pte_exec(pte))
190 191 192 193 194 195 196 197
		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;
}

198
static void note_page_update_state(struct pg_state *st, unsigned long addr, int level, u64 val)
199
{
200
	u64 flag = level >= 0 ? val & pg_level[level].mask : 0;
201 202 203 204 205 206 207 208 209 210 211 212 213
	u64 pa = val & PTE_RPN_MASK;

	st->level = level;
	st->current_flags = flag;
	st->start_address = addr;
	st->start_pa = pa;

	while (addr >= st->marker[1].start_address) {
		st->marker++;
		pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
	}
}

214
static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, u64 val)
215
{
216
	u64 flag = level >= 0 ? val & pg_level[level].mask : 0;
217
	struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
218

219
	/* At first no level is set */
220
	if (st->level == -1) {
221
		pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
222
		note_page_update_state(st, addr, level, val);
223 224 225 226 227 228 229 230
	/*
	 * 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.
	 */
	} else if (flag != st->current_flags || level != st->level ||
231
		   addr >= st->marker[1].start_address) {
232 233 234

		/* Check the PTE flags */
		if (st->current_flags) {
235
			note_prot_wx(st, addr);
236 237 238 239 240 241 242 243
			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);

244
			pt_dump_seq_putc(st->seq, '\n');
245 246 247 248 249 250
		}

		/*
		 * Address indicates we have passed the end of the
		 * current section of virtual memory
		 */
251
		note_page_update_state(st, addr, level, val);
252 253 254 255 256
	}
}

static void populate_markers(void)
{
257 258
	int i = 0;

259
#ifdef CONFIG_PPC64
260
	address_markers[i++].start_address = PAGE_OFFSET;
261 262
#else
	address_markers[i++].start_address = TASK_SIZE;
263 264 265 266
#endif
#ifdef MODULES_VADDR
	address_markers[i++].start_address = MODULES_VADDR;
	address_markers[i++].start_address = MODULES_END;
267
#endif
268 269 270 271 272 273 274 275 276
	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;
277
	/* What is the ifdef about? */
278
#ifdef CONFIG_PPC_BOOK3S_64
279
	address_markers[i++].start_address =  H_VMEMMAP_START;
280
#else
281 282 283 284 285 286 287 288
	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_HIGHMEM
	address_markers[i++].start_address = PKMAP_BASE;
	address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
289
#endif
290 291
	address_markers[i++].start_address = FIXADDR_START;
	address_markers[i++].start_address = FIXADDR_TOP;
292 293 294 295
#ifdef CONFIG_KASAN
	address_markers[i++].start_address = KASAN_SHADOW_START;
	address_markers[i++].start_address = KASAN_SHADOW_END;
#endif
296
#endif /* CONFIG_PPC64 */
297 298 299 300 301 302 303
}

static int ptdump_show(struct seq_file *m, void *v)
{
	struct pg_state st = {
		.seq = m,
		.marker = address_markers,
304
		.level = -1,
305 306 307 308
		.ptdump = {
			.note_page = note_page,
			.range = ptdump_range,
		}
309
	};
310

311
	/* Traverse kernel page tables */
312
	ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
313 314 315
	return 0;
}

316
DEFINE_SHOW_ATTRIBUTE(ptdump);
317 318 319 320 321 322 323 324 325 326 327

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;
}

328
#ifdef CONFIG_DEBUG_WX
329 330 331 332
void ptdump_check_wx(void)
{
	struct pg_state st = {
		.seq = NULL,
333 334 335 336
		.marker = (struct addr_marker[]) {
			{ 0, NULL},
			{ -1, NULL},
		},
337
		.level = -1,
338
		.check_wx = true,
339 340 341 342
		.ptdump = {
			.note_page = note_page,
			.range = ptdump_range,
		}
343 344
	};

345
	ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
346 347 348 349 350 351 352 353 354

	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

355
static int __init ptdump_init(void)
356
{
357 358 359 360 361 362 363
#ifdef CONFIG_PPC64
	if (!radix_enabled())
		ptdump_range[0].start = KERN_VIRT_START;
	else
		ptdump_range[0].start = PAGE_OFFSET;
#endif

364 365
	populate_markers();
	build_pgtable_complete_mask();
366 367 368 369

	if (IS_ENABLED(CONFIG_PTDUMP_DEBUGFS))
		debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops);

370
	return 0;
371 372
}
device_initcall(ptdump_init);