Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
  3 * Debug helper to dump the current kernel pagetables of the system
  4 * so that we can see what the various memory ranges are set to.
  5 *
  6 * Derived from x86 and arm implementation:
  7 * (C) Copyright 2008 Intel Corporation
  8 *
  9 * Author: Arjan van de Ven <arjan@linux.intel.com>
 10 *
 11 * This program is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU General Public License
 13 * as published by the Free Software Foundation; version 2
 14 * of the License.
 15 */
 16#include <linux/debugfs.h>
 17#include <linux/errno.h>
 18#include <linux/fs.h>
 19#include <linux/io.h>
 20#include <linux/init.h>
 21#include <linux/mm.h>
 22#include <linux/sched.h>
 23#include <linux/seq_file.h>
 24
 25#include <asm/fixmap.h>
 
 26#include <asm/memory.h>
 27#include <asm/pgtable.h>
 28#include <asm/pgtable-hwdef.h>
 
 29
 30struct addr_marker {
 31	unsigned long start_address;
 32	const char *name;
 33};
 34
 35enum address_markers_idx {
 36	MODULES_START_NR = 0,
 37	MODULES_END_NR,
 38	VMALLOC_START_NR,
 39	VMALLOC_END_NR,
 40#ifdef CONFIG_SPARSEMEM_VMEMMAP
 41	VMEMMAP_START_NR,
 42	VMEMMAP_END_NR,
 43#endif
 44	FIXADDR_START_NR,
 45	FIXADDR_END_NR,
 46	PCI_START_NR,
 47	PCI_END_NR,
 48	KERNEL_SPACE_NR,
 49};
 50
 51static struct addr_marker address_markers[] = {
 52	{ MODULES_VADDR,	"Modules start" },
 53	{ MODULES_END,		"Modules end" },
 54	{ VMALLOC_START,	"vmalloc() Area" },
 55	{ VMALLOC_END,		"vmalloc() End" },
 
 
 
 
 
 
 
 
 
 
 56#ifdef CONFIG_SPARSEMEM_VMEMMAP
 57	{ 0,			"vmemmap start" },
 58	{ 0,			"vmemmap end" },
 59#endif
 60	{ FIXADDR_START,	"Fixmap start" },
 61	{ FIXADDR_TOP,		"Fixmap end" },
 62	{ PCI_IO_START,		"PCI I/O start" },
 63	{ PCI_IO_END,		"PCI I/O end" },
 64	{ PAGE_OFFSET,		"Linear Mapping" },
 65	{ -1,			NULL },
 66};
 67
 
 
 
 
 
 
 
 
 
 
 
 
 68/*
 69 * The page dumper groups page table entries of the same type into a single
 70 * description. It uses pg_state to track the range information while
 71 * iterating over the pte entries. When the continuity is broken it then
 72 * dumps out a description of the range.
 73 */
 74struct pg_state {
 75	struct seq_file *seq;
 76	const struct addr_marker *marker;
 77	unsigned long start_address;
 78	unsigned level;
 79	u64 current_prot;
 
 
 
 80};
 81
 82struct prot_bits {
 83	u64		mask;
 84	u64		val;
 85	const char	*set;
 86	const char	*clear;
 87};
 88
 89static const struct prot_bits pte_bits[] = {
 90	{
 91		.mask	= PTE_VALID,
 92		.val	= PTE_VALID,
 93		.set	= " ",
 94		.clear	= "F",
 95	}, {
 96		.mask	= PTE_USER,
 97		.val	= PTE_USER,
 98		.set	= "USR",
 99		.clear	= "   ",
100	}, {
101		.mask	= PTE_RDONLY,
102		.val	= PTE_RDONLY,
103		.set	= "ro",
104		.clear	= "RW",
105	}, {
106		.mask	= PTE_PXN,
107		.val	= PTE_PXN,
108		.set	= "NX",
109		.clear	= "x ",
110	}, {
111		.mask	= PTE_SHARED,
112		.val	= PTE_SHARED,
113		.set	= "SHD",
114		.clear	= "   ",
115	}, {
116		.mask	= PTE_AF,
117		.val	= PTE_AF,
118		.set	= "AF",
119		.clear	= "  ",
120	}, {
121		.mask	= PTE_NG,
122		.val	= PTE_NG,
123		.set	= "NG",
124		.clear	= "  ",
125	}, {
126		.mask	= PTE_CONT,
127		.val	= PTE_CONT,
128		.set	= "CON",
129		.clear	= "   ",
130	}, {
131		.mask	= PTE_TABLE_BIT,
132		.val	= PTE_TABLE_BIT,
133		.set	= "   ",
134		.clear	= "BLK",
135	}, {
136		.mask	= PTE_UXN,
137		.val	= PTE_UXN,
138		.set	= "UXN",
139	}, {
140		.mask	= PTE_ATTRINDX_MASK,
141		.val	= PTE_ATTRINDX(MT_DEVICE_nGnRnE),
142		.set	= "DEVICE/nGnRnE",
143	}, {
144		.mask	= PTE_ATTRINDX_MASK,
145		.val	= PTE_ATTRINDX(MT_DEVICE_nGnRE),
146		.set	= "DEVICE/nGnRE",
147	}, {
148		.mask	= PTE_ATTRINDX_MASK,
149		.val	= PTE_ATTRINDX(MT_DEVICE_GRE),
150		.set	= "DEVICE/GRE",
151	}, {
152		.mask	= PTE_ATTRINDX_MASK,
153		.val	= PTE_ATTRINDX(MT_NORMAL_NC),
154		.set	= "MEM/NORMAL-NC",
155	}, {
156		.mask	= PTE_ATTRINDX_MASK,
157		.val	= PTE_ATTRINDX(MT_NORMAL),
158		.set	= "MEM/NORMAL",
159	}
160};
161
162struct pg_level {
163	const struct prot_bits *bits;
 
164	size_t num;
165	u64 mask;
166};
167
168static struct pg_level pg_level[] = {
169	{
170	}, { /* pgd */
 
171		.bits	= pte_bits,
172		.num	= ARRAY_SIZE(pte_bits),
173	}, { /* pud */
 
174		.bits	= pte_bits,
175		.num	= ARRAY_SIZE(pte_bits),
176	}, { /* pmd */
 
177		.bits	= pte_bits,
178		.num	= ARRAY_SIZE(pte_bits),
179	}, { /* pte */
 
180		.bits	= pte_bits,
181		.num	= ARRAY_SIZE(pte_bits),
182	},
183};
184
185static void dump_prot(struct pg_state *st, const struct prot_bits *bits,
186			size_t num)
187{
188	unsigned i;
189
190	for (i = 0; i < num; i++, bits++) {
191		const char *s;
192
193		if ((st->current_prot & bits->mask) == bits->val)
194			s = bits->set;
195		else
196			s = bits->clear;
197
198		if (s)
199			seq_printf(st->seq, " %s", s);
200	}
201}
202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203static void note_page(struct pg_state *st, unsigned long addr, unsigned level,
204				u64 val)
205{
206	static const char units[] = "KMGTPE";
207	u64 prot = val & pg_level[level].mask;
208
209	if (!st->level) {
210		st->level = level;
211		st->current_prot = prot;
212		st->start_address = addr;
213		seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
214	} else if (prot != st->current_prot || level != st->level ||
215		   addr >= st->marker[1].start_address) {
216		const char *unit = units;
217		unsigned long delta;
218
219		if (st->current_prot) {
220			seq_printf(st->seq, "0x%016lx-0x%016lx   ",
 
 
221				   st->start_address, addr);
222
223			delta = (addr - st->start_address) >> 10;
224			while (!(delta & 1023) && unit[1]) {
225				delta >>= 10;
226				unit++;
227			}
228			seq_printf(st->seq, "%9lu%c", delta, *unit);
 
229			if (pg_level[st->level].bits)
230				dump_prot(st, pg_level[st->level].bits,
231					  pg_level[st->level].num);
232			seq_puts(st->seq, "\n");
233		}
234
235		if (addr >= st->marker[1].start_address) {
236			st->marker++;
237			seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
238		}
239
240		st->start_address = addr;
241		st->current_prot = prot;
242		st->level = level;
243	}
244
245	if (addr >= st->marker[1].start_address) {
246		st->marker++;
247		seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
248	}
249
250}
251
252static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
 
253{
254	pte_t *pte = pte_offset_kernel(pmd, 0);
255	unsigned long addr;
256	unsigned i;
257
258	for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
259		addr = start + i * PAGE_SIZE;
260		note_page(st, addr, 4, pte_val(*pte));
261	}
262}
263
264static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
 
265{
266	pmd_t *pmd = pmd_offset(pud, 0);
267	unsigned long addr;
268	unsigned i;
 
 
 
269
270	for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
271		addr = start + i * PMD_SIZE;
272		if (pmd_none(*pmd) || pmd_sect(*pmd)) {
273			note_page(st, addr, 3, pmd_val(*pmd));
274		} else {
275			BUG_ON(pmd_bad(*pmd));
276			walk_pte(st, pmd, addr);
277		}
278	}
279}
280
281static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
 
282{
283	pud_t *pud = pud_offset(pgd, 0);
284	unsigned long addr;
285	unsigned i;
 
 
 
286
287	for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
288		addr = start + i * PUD_SIZE;
289		if (pud_none(*pud) || pud_sect(*pud)) {
290			note_page(st, addr, 2, pud_val(*pud));
291		} else {
292			BUG_ON(pud_bad(*pud));
293			walk_pmd(st, pud, addr);
294		}
295	}
296}
297
298static void walk_pgd(struct pg_state *st, struct mm_struct *mm, unsigned long start)
 
299{
300	pgd_t *pgd = pgd_offset(mm, 0UL);
301	unsigned i;
302	unsigned long addr;
 
 
 
 
303
304	for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
305		addr = start + i * PGDIR_SIZE;
306		if (pgd_none(*pgd)) {
307			note_page(st, addr, 1, pgd_val(*pgd));
308		} else {
309			BUG_ON(pgd_bad(*pgd));
310			walk_pud(st, pgd, addr);
311		}
312	}
313}
314
315static int ptdump_show(struct seq_file *m, void *v)
316{
317	struct pg_state st = {
318		.seq = m,
319		.marker = address_markers,
320	};
321
322	walk_pgd(&st, &init_mm, VA_START);
323
324	note_page(&st, 0, 0, 0);
325	return 0;
326}
327
328static int ptdump_open(struct inode *inode, struct file *file)
329{
330	return single_open(file, ptdump_show, NULL);
331}
332
333static const struct file_operations ptdump_fops = {
334	.open		= ptdump_open,
335	.read		= seq_read,
336	.llseek		= seq_lseek,
337	.release	= single_release,
338};
339
340static int ptdump_init(void)
341{
342	struct dentry *pe;
343	unsigned i, j;
344
345	for (i = 0; i < ARRAY_SIZE(pg_level); i++)
346		if (pg_level[i].bits)
347			for (j = 0; j < pg_level[i].num; j++)
348				pg_level[i].mask |= pg_level[i].bits[j].mask;
 
349
350#ifdef CONFIG_SPARSEMEM_VMEMMAP
351	address_markers[VMEMMAP_START_NR].start_address =
352				(unsigned long)virt_to_page(PAGE_OFFSET);
353	address_markers[VMEMMAP_END_NR].start_address =
354				(unsigned long)virt_to_page(high_memory);
355#endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356
357	pe = debugfs_create_file("kernel_page_tables", 0400, NULL, NULL,
358				 &ptdump_fops);
359	return pe ? 0 : -ENOMEM;
 
 
 
 
 
 
360}
361device_initcall(ptdump_init);
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
  4 * Debug helper to dump the current kernel pagetables of the system
  5 * so that we can see what the various memory ranges are set to.
  6 *
  7 * Derived from x86 and arm implementation:
  8 * (C) Copyright 2008 Intel Corporation
  9 *
 10 * Author: Arjan van de Ven <arjan@linux.intel.com>
 
 
 
 
 
 11 */
 12#include <linux/debugfs.h>
 13#include <linux/errno.h>
 14#include <linux/fs.h>
 15#include <linux/io.h>
 16#include <linux/init.h>
 17#include <linux/mm.h>
 18#include <linux/sched.h>
 19#include <linux/seq_file.h>
 20
 21#include <asm/fixmap.h>
 22#include <asm/kasan.h>
 23#include <asm/memory.h>
 24#include <asm/pgtable.h>
 25#include <asm/pgtable-hwdef.h>
 26#include <asm/ptdump.h>
 27
 
 
 
 
 28
 29enum address_markers_idx {
 30	PAGE_OFFSET_NR = 0,
 31	PAGE_END_NR,
 32#ifdef CONFIG_KASAN
 33	KASAN_START_NR,
 
 
 
 34#endif
 
 
 
 
 
 35};
 36
 37static struct addr_marker address_markers[] = {
 38	{ PAGE_OFFSET,			"Linear Mapping start" },
 39	{ 0 /* PAGE_END */,		"Linear Mapping end" },
 40#ifdef CONFIG_KASAN
 41	{ 0 /* KASAN_SHADOW_START */,	"Kasan shadow start" },
 42	{ KASAN_SHADOW_END,		"Kasan shadow end" },
 43#endif
 44	{ MODULES_VADDR,		"Modules start" },
 45	{ MODULES_END,			"Modules end" },
 46	{ VMALLOC_START,		"vmalloc() area" },
 47	{ VMALLOC_END,			"vmalloc() end" },
 48	{ FIXADDR_START,		"Fixmap start" },
 49	{ FIXADDR_TOP,			"Fixmap end" },
 50	{ PCI_IO_START,			"PCI I/O start" },
 51	{ PCI_IO_END,			"PCI I/O end" },
 52#ifdef CONFIG_SPARSEMEM_VMEMMAP
 53	{ VMEMMAP_START,		"vmemmap start" },
 54	{ VMEMMAP_START + VMEMMAP_SIZE,	"vmemmap end" },
 55#endif
 56	{ -1,				NULL },
 
 
 
 
 
 57};
 58
 59#define pt_dump_seq_printf(m, fmt, args...)	\
 60({						\
 61	if (m)					\
 62		seq_printf(m, fmt, ##args);	\
 63})
 64
 65#define pt_dump_seq_puts(m, fmt)	\
 66({					\
 67	if (m)				\
 68		seq_printf(m, fmt);	\
 69})
 70
 71/*
 72 * The page dumper groups page table entries of the same type into a single
 73 * description. It uses pg_state to track the range information while
 74 * iterating over the pte entries. When the continuity is broken it then
 75 * dumps out a description of the range.
 76 */
 77struct pg_state {
 78	struct seq_file *seq;
 79	const struct addr_marker *marker;
 80	unsigned long start_address;
 81	unsigned level;
 82	u64 current_prot;
 83	bool check_wx;
 84	unsigned long wx_pages;
 85	unsigned long uxn_pages;
 86};
 87
 88struct prot_bits {
 89	u64		mask;
 90	u64		val;
 91	const char	*set;
 92	const char	*clear;
 93};
 94
 95static const struct prot_bits pte_bits[] = {
 96	{
 97		.mask	= PTE_VALID,
 98		.val	= PTE_VALID,
 99		.set	= " ",
100		.clear	= "F",
101	}, {
102		.mask	= PTE_USER,
103		.val	= PTE_USER,
104		.set	= "USR",
105		.clear	= "   ",
106	}, {
107		.mask	= PTE_RDONLY,
108		.val	= PTE_RDONLY,
109		.set	= "ro",
110		.clear	= "RW",
111	}, {
112		.mask	= PTE_PXN,
113		.val	= PTE_PXN,
114		.set	= "NX",
115		.clear	= "x ",
116	}, {
117		.mask	= PTE_SHARED,
118		.val	= PTE_SHARED,
119		.set	= "SHD",
120		.clear	= "   ",
121	}, {
122		.mask	= PTE_AF,
123		.val	= PTE_AF,
124		.set	= "AF",
125		.clear	= "  ",
126	}, {
127		.mask	= PTE_NG,
128		.val	= PTE_NG,
129		.set	= "NG",
130		.clear	= "  ",
131	}, {
132		.mask	= PTE_CONT,
133		.val	= PTE_CONT,
134		.set	= "CON",
135		.clear	= "   ",
136	}, {
137		.mask	= PTE_TABLE_BIT,
138		.val	= PTE_TABLE_BIT,
139		.set	= "   ",
140		.clear	= "BLK",
141	}, {
142		.mask	= PTE_UXN,
143		.val	= PTE_UXN,
144		.set	= "UXN",
145	}, {
146		.mask	= PTE_ATTRINDX_MASK,
147		.val	= PTE_ATTRINDX(MT_DEVICE_nGnRnE),
148		.set	= "DEVICE/nGnRnE",
149	}, {
150		.mask	= PTE_ATTRINDX_MASK,
151		.val	= PTE_ATTRINDX(MT_DEVICE_nGnRE),
152		.set	= "DEVICE/nGnRE",
153	}, {
154		.mask	= PTE_ATTRINDX_MASK,
155		.val	= PTE_ATTRINDX(MT_DEVICE_GRE),
156		.set	= "DEVICE/GRE",
157	}, {
158		.mask	= PTE_ATTRINDX_MASK,
159		.val	= PTE_ATTRINDX(MT_NORMAL_NC),
160		.set	= "MEM/NORMAL-NC",
161	}, {
162		.mask	= PTE_ATTRINDX_MASK,
163		.val	= PTE_ATTRINDX(MT_NORMAL),
164		.set	= "MEM/NORMAL",
165	}
166};
167
168struct pg_level {
169	const struct prot_bits *bits;
170	const char *name;
171	size_t num;
172	u64 mask;
173};
174
175static struct pg_level pg_level[] = {
176	{
177	}, { /* pgd */
178		.name	= "PGD",
179		.bits	= pte_bits,
180		.num	= ARRAY_SIZE(pte_bits),
181	}, { /* pud */
182		.name	= (CONFIG_PGTABLE_LEVELS > 3) ? "PUD" : "PGD",
183		.bits	= pte_bits,
184		.num	= ARRAY_SIZE(pte_bits),
185	}, { /* pmd */
186		.name	= (CONFIG_PGTABLE_LEVELS > 2) ? "PMD" : "PGD",
187		.bits	= pte_bits,
188		.num	= ARRAY_SIZE(pte_bits),
189	}, { /* pte */
190		.name	= "PTE",
191		.bits	= pte_bits,
192		.num	= ARRAY_SIZE(pte_bits),
193	},
194};
195
196static void dump_prot(struct pg_state *st, const struct prot_bits *bits,
197			size_t num)
198{
199	unsigned i;
200
201	for (i = 0; i < num; i++, bits++) {
202		const char *s;
203
204		if ((st->current_prot & bits->mask) == bits->val)
205			s = bits->set;
206		else
207			s = bits->clear;
208
209		if (s)
210			pt_dump_seq_printf(st->seq, " %s", s);
211	}
212}
213
214static void note_prot_uxn(struct pg_state *st, unsigned long addr)
215{
216	if (!st->check_wx)
217		return;
218
219	if ((st->current_prot & PTE_UXN) == PTE_UXN)
220		return;
221
222	WARN_ONCE(1, "arm64/mm: Found non-UXN mapping at address %p/%pS\n",
223		  (void *)st->start_address, (void *)st->start_address);
224
225	st->uxn_pages += (addr - st->start_address) / PAGE_SIZE;
226}
227
228static void note_prot_wx(struct pg_state *st, unsigned long addr)
229{
230	if (!st->check_wx)
231		return;
232	if ((st->current_prot & PTE_RDONLY) == PTE_RDONLY)
233		return;
234	if ((st->current_prot & PTE_PXN) == PTE_PXN)
235		return;
236
237	WARN_ONCE(1, "arm64/mm: Found insecure W+X mapping at address %p/%pS\n",
238		  (void *)st->start_address, (void *)st->start_address);
239
240	st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
241}
242
243static void note_page(struct pg_state *st, unsigned long addr, unsigned level,
244				u64 val)
245{
246	static const char units[] = "KMGTPE";
247	u64 prot = val & pg_level[level].mask;
248
249	if (!st->level) {
250		st->level = level;
251		st->current_prot = prot;
252		st->start_address = addr;
253		pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
254	} else if (prot != st->current_prot || level != st->level ||
255		   addr >= st->marker[1].start_address) {
256		const char *unit = units;
257		unsigned long delta;
258
259		if (st->current_prot) {
260			note_prot_uxn(st, addr);
261			note_prot_wx(st, addr);
262			pt_dump_seq_printf(st->seq, "0x%016lx-0x%016lx   ",
263				   st->start_address, addr);
264
265			delta = (addr - st->start_address) >> 10;
266			while (!(delta & 1023) && unit[1]) {
267				delta >>= 10;
268				unit++;
269			}
270			pt_dump_seq_printf(st->seq, "%9lu%c %s", delta, *unit,
271				   pg_level[st->level].name);
272			if (pg_level[st->level].bits)
273				dump_prot(st, pg_level[st->level].bits,
274					  pg_level[st->level].num);
275			pt_dump_seq_puts(st->seq, "\n");
276		}
277
278		if (addr >= st->marker[1].start_address) {
279			st->marker++;
280			pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
281		}
282
283		st->start_address = addr;
284		st->current_prot = prot;
285		st->level = level;
286	}
287
288	if (addr >= st->marker[1].start_address) {
289		st->marker++;
290		pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
291	}
292
293}
294
295static void walk_pte(struct pg_state *st, pmd_t *pmdp, unsigned long start,
296		     unsigned long end)
297{
298	unsigned long addr = start;
299	pte_t *ptep = pte_offset_kernel(pmdp, start);
 
300
301	do {
302		note_page(st, addr, 4, READ_ONCE(pte_val(*ptep)));
303	} while (ptep++, addr += PAGE_SIZE, addr != end);
 
304}
305
306static void walk_pmd(struct pg_state *st, pud_t *pudp, unsigned long start,
307		     unsigned long end)
308{
309	unsigned long next, addr = start;
310	pmd_t *pmdp = pmd_offset(pudp, start);
311
312	do {
313		pmd_t pmd = READ_ONCE(*pmdp);
314		next = pmd_addr_end(addr, end);
315
316		if (pmd_none(pmd) || pmd_sect(pmd)) {
317			note_page(st, addr, 3, pmd_val(pmd));
 
 
318		} else {
319			BUG_ON(pmd_bad(pmd));
320			walk_pte(st, pmdp, addr, next);
321		}
322	} while (pmdp++, addr = next, addr != end);
323}
324
325static void walk_pud(struct pg_state *st, pgd_t *pgdp, unsigned long start,
326		     unsigned long end)
327{
328	unsigned long next, addr = start;
329	pud_t *pudp = pud_offset(pgdp, start);
330
331	do {
332		pud_t pud = READ_ONCE(*pudp);
333		next = pud_addr_end(addr, end);
334
335		if (pud_none(pud) || pud_sect(pud)) {
336			note_page(st, addr, 2, pud_val(pud));
 
 
337		} else {
338			BUG_ON(pud_bad(pud));
339			walk_pmd(st, pudp, addr, next);
340		}
341	} while (pudp++, addr = next, addr != end);
342}
343
344static void walk_pgd(struct pg_state *st, struct mm_struct *mm,
345		     unsigned long start)
346{
347	unsigned long end = (start < TASK_SIZE_64) ? TASK_SIZE_64 : 0;
348	unsigned long next, addr = start;
349	pgd_t *pgdp = pgd_offset(mm, start);
350
351	do {
352		pgd_t pgd = READ_ONCE(*pgdp);
353		next = pgd_addr_end(addr, end);
354
355		if (pgd_none(pgd)) {
356			note_page(st, addr, 1, pgd_val(pgd));
 
 
357		} else {
358			BUG_ON(pgd_bad(pgd));
359			walk_pud(st, pgdp, addr, next);
360		}
361	} while (pgdp++, addr = next, addr != end);
362}
363
364void ptdump_walk_pgd(struct seq_file *m, struct ptdump_info *info)
365{
366	struct pg_state st = {
367		.seq = m,
368		.marker = info->markers,
369	};
370
371	walk_pgd(&st, info->mm, info->base_addr);
372
373	note_page(&st, 0, 0, 0);
 
374}
375
376static void ptdump_initialize(void)
377{
 
 
 
 
 
 
 
 
 
 
 
 
 
378	unsigned i, j;
379
380	for (i = 0; i < ARRAY_SIZE(pg_level); i++)
381		if (pg_level[i].bits)
382			for (j = 0; j < pg_level[i].num; j++)
383				pg_level[i].mask |= pg_level[i].bits[j].mask;
384}
385
386static struct ptdump_info kernel_ptdump_info = {
387	.mm		= &init_mm,
388	.markers	= address_markers,
389	.base_addr	= PAGE_OFFSET,
390};
391
392void ptdump_check_wx(void)
393{
394	struct pg_state st = {
395		.seq = NULL,
396		.marker = (struct addr_marker[]) {
397			{ 0, NULL},
398			{ -1, NULL},
399		},
400		.check_wx = true,
401	};
402
403	walk_pgd(&st, &init_mm, PAGE_OFFSET);
404	note_page(&st, 0, 0, 0);
405	if (st.wx_pages || st.uxn_pages)
406		pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UXN pages found\n",
407			st.wx_pages, st.uxn_pages);
408	else
409		pr_info("Checked W+X mappings: passed, no W+X pages found\n");
410}
411
412static int ptdump_init(void)
413{
414	address_markers[PAGE_END_NR].start_address = PAGE_END;
415#ifdef CONFIG_KASAN
416	address_markers[KASAN_START_NR].start_address = KASAN_SHADOW_START;
417#endif
418	ptdump_initialize();
419	ptdump_debugfs_register(&kernel_ptdump_info, "kernel_page_tables");
420	return 0;
421}
422device_initcall(ptdump_init);