1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e66c3209SChristophe Leroy /*
3e66c3209SChristophe Leroy  * Copyright 2016, Rashmica Gupta, IBM Corp.
4e66c3209SChristophe Leroy  *
5e66c3209SChristophe Leroy  * This traverses the kernel virtual memory and dumps the pages that are in
6e66c3209SChristophe Leroy  * the hash pagetable, along with their flags to
7e66c3209SChristophe Leroy  * /sys/kernel/debug/kernel_hash_pagetable.
8e66c3209SChristophe Leroy  *
9e66c3209SChristophe Leroy  * If radix is enabled then there is no hash page table and so no debugfs file
10e66c3209SChristophe Leroy  * is generated.
11e66c3209SChristophe Leroy  */
12e66c3209SChristophe Leroy #include <linux/debugfs.h>
13e66c3209SChristophe Leroy #include <linux/fs.h>
14e66c3209SChristophe Leroy #include <linux/io.h>
15e66c3209SChristophe Leroy #include <linux/mm.h>
16e66c3209SChristophe Leroy #include <linux/sched.h>
17e66c3209SChristophe Leroy #include <linux/seq_file.h>
18e66c3209SChristophe Leroy #include <linux/const.h>
19e66c3209SChristophe Leroy #include <asm/page.h>
20e66c3209SChristophe Leroy #include <asm/plpar_wrappers.h>
21e66c3209SChristophe Leroy #include <linux/memblock.h>
22e66c3209SChristophe Leroy #include <asm/firmware.h>
23ca15ca40SMike Rapoport #include <asm/pgalloc.h>
24e66c3209SChristophe Leroy 
25e66c3209SChristophe Leroy struct pg_state {
26e66c3209SChristophe Leroy 	struct seq_file *seq;
27e66c3209SChristophe Leroy 	const struct addr_marker *marker;
28e66c3209SChristophe Leroy 	unsigned long start_address;
29e66c3209SChristophe Leroy 	unsigned int level;
30e66c3209SChristophe Leroy 	u64 current_flags;
31e66c3209SChristophe Leroy };
32e66c3209SChristophe Leroy 
33e66c3209SChristophe Leroy struct addr_marker {
34e66c3209SChristophe Leroy 	unsigned long start_address;
35e66c3209SChristophe Leroy 	const char *name;
36e66c3209SChristophe Leroy };
37e66c3209SChristophe Leroy 
38e66c3209SChristophe Leroy static struct addr_marker address_markers[] = {
39e66c3209SChristophe Leroy 	{ 0,	"Start of kernel VM" },
40e66c3209SChristophe Leroy 	{ 0,	"vmalloc() Area" },
41e66c3209SChristophe Leroy 	{ 0,	"vmalloc() End" },
42e66c3209SChristophe Leroy 	{ 0,	"isa I/O start" },
43e66c3209SChristophe Leroy 	{ 0,	"isa I/O end" },
44e66c3209SChristophe Leroy 	{ 0,	"phb I/O start" },
45e66c3209SChristophe Leroy 	{ 0,	"phb I/O end" },
46e66c3209SChristophe Leroy 	{ 0,	"I/O remap start" },
47e66c3209SChristophe Leroy 	{ 0,	"I/O remap end" },
48e66c3209SChristophe Leroy 	{ 0,	"vmemmap start" },
49e66c3209SChristophe Leroy 	{ -1,	NULL },
50e66c3209SChristophe Leroy };
51e66c3209SChristophe Leroy 
52e66c3209SChristophe Leroy struct flag_info {
53e66c3209SChristophe Leroy 	u64		mask;
54e66c3209SChristophe Leroy 	u64		val;
55e66c3209SChristophe Leroy 	const char	*set;
56e66c3209SChristophe Leroy 	const char	*clear;
57e66c3209SChristophe Leroy 	bool		is_val;
58e66c3209SChristophe Leroy 	int		shift;
59e66c3209SChristophe Leroy };
60e66c3209SChristophe Leroy 
61e66c3209SChristophe Leroy static const struct flag_info v_flag_array[] = {
62e66c3209SChristophe Leroy 	{
63e66c3209SChristophe Leroy 		.mask   = SLB_VSID_B,
64e66c3209SChristophe Leroy 		.val    = SLB_VSID_B_256M,
65e66c3209SChristophe Leroy 		.set    = "ssize: 256M",
66e66c3209SChristophe Leroy 		.clear  = "ssize: 1T  ",
67e66c3209SChristophe Leroy 	}, {
68e66c3209SChristophe Leroy 		.mask	= HPTE_V_SECONDARY,
69e66c3209SChristophe Leroy 		.val	= HPTE_V_SECONDARY,
70e66c3209SChristophe Leroy 		.set	= "secondary",
71e66c3209SChristophe Leroy 		.clear	= "primary  ",
72e66c3209SChristophe Leroy 	}, {
73e66c3209SChristophe Leroy 		.mask	= HPTE_V_VALID,
74e66c3209SChristophe Leroy 		.val	= HPTE_V_VALID,
75e66c3209SChristophe Leroy 		.set	= "valid  ",
76e66c3209SChristophe Leroy 		.clear	= "invalid",
77e66c3209SChristophe Leroy 	}, {
78e66c3209SChristophe Leroy 		.mask	= HPTE_V_BOLTED,
79e66c3209SChristophe Leroy 		.val	= HPTE_V_BOLTED,
80e66c3209SChristophe Leroy 		.set	= "bolted",
81e66c3209SChristophe Leroy 		.clear	= "",
82e66c3209SChristophe Leroy 	}
83e66c3209SChristophe Leroy };
84e66c3209SChristophe Leroy 
85e66c3209SChristophe Leroy static const struct flag_info r_flag_array[] = {
86e66c3209SChristophe Leroy 	{
87e66c3209SChristophe Leroy 		.mask	= HPTE_R_PP0 | HPTE_R_PP,
88e66c3209SChristophe Leroy 		.val	= PP_RWXX,
89e66c3209SChristophe Leroy 		.set	= "prot:RW--",
90e66c3209SChristophe Leroy 	}, {
91e66c3209SChristophe Leroy 		.mask	= HPTE_R_PP0 | HPTE_R_PP,
92e66c3209SChristophe Leroy 		.val	= PP_RWRX,
93e66c3209SChristophe Leroy 		.set	= "prot:RWR-",
94e66c3209SChristophe Leroy 	}, {
95e66c3209SChristophe Leroy 		.mask	= HPTE_R_PP0 | HPTE_R_PP,
96e66c3209SChristophe Leroy 		.val	= PP_RWRW,
97e66c3209SChristophe Leroy 		.set	= "prot:RWRW",
98e66c3209SChristophe Leroy 	}, {
99e66c3209SChristophe Leroy 		.mask	= HPTE_R_PP0 | HPTE_R_PP,
100e66c3209SChristophe Leroy 		.val	= PP_RXRX,
101e66c3209SChristophe Leroy 		.set	= "prot:R-R-",
102e66c3209SChristophe Leroy 	}, {
103e66c3209SChristophe Leroy 		.mask	= HPTE_R_PP0 | HPTE_R_PP,
104e66c3209SChristophe Leroy 		.val	= PP_RXXX,
105e66c3209SChristophe Leroy 		.set	= "prot:R---",
106e66c3209SChristophe Leroy 	}, {
107e66c3209SChristophe Leroy 		.mask	= HPTE_R_KEY_HI | HPTE_R_KEY_LO,
108e66c3209SChristophe Leroy 		.val	= HPTE_R_KEY_HI | HPTE_R_KEY_LO,
109e66c3209SChristophe Leroy 		.set	= "key",
110e66c3209SChristophe Leroy 		.clear	= "",
111e66c3209SChristophe Leroy 		.is_val = true,
112e66c3209SChristophe Leroy 	}, {
113e66c3209SChristophe Leroy 		.mask	= HPTE_R_R,
114e66c3209SChristophe Leroy 		.val	= HPTE_R_R,
115e66c3209SChristophe Leroy 		.set	= "ref",
116e66c3209SChristophe Leroy 		.clear	= "   ",
117e66c3209SChristophe Leroy 	}, {
118e66c3209SChristophe Leroy 		.mask	= HPTE_R_C,
119e66c3209SChristophe Leroy 		.val	= HPTE_R_C,
120e66c3209SChristophe Leroy 		.set	= "changed",
121e66c3209SChristophe Leroy 		.clear	= "       ",
122e66c3209SChristophe Leroy 	}, {
123e66c3209SChristophe Leroy 		.mask	= HPTE_R_N,
124e66c3209SChristophe Leroy 		.val	= HPTE_R_N,
125e66c3209SChristophe Leroy 		.set	= "no execute",
126e66c3209SChristophe Leroy 	}, {
127e66c3209SChristophe Leroy 		.mask	= HPTE_R_WIMG,
128e66c3209SChristophe Leroy 		.val	= HPTE_R_W,
129e66c3209SChristophe Leroy 		.set	= "writethru",
130e66c3209SChristophe Leroy 	}, {
131e66c3209SChristophe Leroy 		.mask	= HPTE_R_WIMG,
132e66c3209SChristophe Leroy 		.val	= HPTE_R_I,
133e66c3209SChristophe Leroy 		.set	= "no cache",
134e66c3209SChristophe Leroy 	}, {
135e66c3209SChristophe Leroy 		.mask	= HPTE_R_WIMG,
136e66c3209SChristophe Leroy 		.val	= HPTE_R_G,
137e66c3209SChristophe Leroy 		.set	= "guarded",
138e66c3209SChristophe Leroy 	}
139e66c3209SChristophe Leroy };
140e66c3209SChristophe Leroy 
calculate_pagesize(struct pg_state * st,int ps,char s[])141e66c3209SChristophe Leroy static int calculate_pagesize(struct pg_state *st, int ps, char s[])
142e66c3209SChristophe Leroy {
143e66c3209SChristophe Leroy 	static const char units[] = "BKMGTPE";
144e66c3209SChristophe Leroy 	const char *unit = units;
145e66c3209SChristophe Leroy 
146e66c3209SChristophe Leroy 	while (ps > 9 && unit[1]) {
147e66c3209SChristophe Leroy 		ps -= 10;
148e66c3209SChristophe Leroy 		unit++;
149e66c3209SChristophe Leroy 	}
150e66c3209SChristophe Leroy 	seq_printf(st->seq, "  %s_ps: %i%c\t", s, 1<<ps, *unit);
151e66c3209SChristophe Leroy 	return ps;
152e66c3209SChristophe Leroy }
153e66c3209SChristophe Leroy 
dump_flag_info(struct pg_state * st,const struct flag_info * flag,u64 pte,int num)154e66c3209SChristophe Leroy static void dump_flag_info(struct pg_state *st, const struct flag_info
155e66c3209SChristophe Leroy 		*flag, u64 pte, int num)
156e66c3209SChristophe Leroy {
157e66c3209SChristophe Leroy 	unsigned int i;
158e66c3209SChristophe Leroy 
159e66c3209SChristophe Leroy 	for (i = 0; i < num; i++, flag++) {
160e66c3209SChristophe Leroy 		const char *s = NULL;
161e66c3209SChristophe Leroy 		u64 val;
162e66c3209SChristophe Leroy 
163e66c3209SChristophe Leroy 		/* flag not defined so don't check it */
164e66c3209SChristophe Leroy 		if (flag->mask == 0)
165e66c3209SChristophe Leroy 			continue;
166e66c3209SChristophe Leroy 		/* Some 'flags' are actually values */
167e66c3209SChristophe Leroy 		if (flag->is_val) {
168e66c3209SChristophe Leroy 			val = pte & flag->val;
169e66c3209SChristophe Leroy 			if (flag->shift)
170e66c3209SChristophe Leroy 				val = val >> flag->shift;
171e66c3209SChristophe Leroy 			seq_printf(st->seq, "  %s:%llx", flag->set, val);
172e66c3209SChristophe Leroy 		} else {
173e66c3209SChristophe Leroy 			if ((pte & flag->mask) == flag->val)
174e66c3209SChristophe Leroy 				s = flag->set;
175e66c3209SChristophe Leroy 			else
176e66c3209SChristophe Leroy 				s = flag->clear;
177e66c3209SChristophe Leroy 			if (s)
178e66c3209SChristophe Leroy 				seq_printf(st->seq, "  %s", s);
179e66c3209SChristophe Leroy 		}
180e66c3209SChristophe Leroy 	}
181e66c3209SChristophe Leroy }
182e66c3209SChristophe Leroy 
dump_hpte_info(struct pg_state * st,unsigned long ea,u64 v,u64 r,unsigned long rpn,int bps,int aps,unsigned long lp)183e66c3209SChristophe Leroy static void dump_hpte_info(struct pg_state *st, unsigned long ea, u64 v, u64 r,
184e66c3209SChristophe Leroy 		unsigned long rpn, int bps, int aps, unsigned long lp)
185e66c3209SChristophe Leroy {
186e66c3209SChristophe Leroy 	int aps_index;
187e66c3209SChristophe Leroy 
188e66c3209SChristophe Leroy 	while (ea >= st->marker[1].start_address) {
189e66c3209SChristophe Leroy 		st->marker++;
190e66c3209SChristophe Leroy 		seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
191e66c3209SChristophe Leroy 	}
192e66c3209SChristophe Leroy 	seq_printf(st->seq, "0x%lx:\t", ea);
193e66c3209SChristophe Leroy 	seq_printf(st->seq, "AVPN:%llx\t", HPTE_V_AVPN_VAL(v));
194e66c3209SChristophe Leroy 	dump_flag_info(st, v_flag_array, v, ARRAY_SIZE(v_flag_array));
195e66c3209SChristophe Leroy 	seq_printf(st->seq, "  rpn: %lx\t", rpn);
196e66c3209SChristophe Leroy 	dump_flag_info(st, r_flag_array, r, ARRAY_SIZE(r_flag_array));
197e66c3209SChristophe Leroy 
198e66c3209SChristophe Leroy 	calculate_pagesize(st, bps, "base");
199e66c3209SChristophe Leroy 	aps_index = calculate_pagesize(st, aps, "actual");
200e66c3209SChristophe Leroy 	if (aps_index != 2)
201e66c3209SChristophe Leroy 		seq_printf(st->seq, "LP enc: %lx", lp);
202e66c3209SChristophe Leroy 	seq_putc(st->seq, '\n');
203e66c3209SChristophe Leroy }
204e66c3209SChristophe Leroy 
205e66c3209SChristophe Leroy 
native_find(unsigned long ea,int psize,bool primary,u64 * v,u64 * r)206e66c3209SChristophe Leroy static int native_find(unsigned long ea, int psize, bool primary, u64 *v, u64
207e66c3209SChristophe Leroy 		*r)
208e66c3209SChristophe Leroy {
209e66c3209SChristophe Leroy 	struct hash_pte *hptep;
210e66c3209SChristophe Leroy 	unsigned long hash, vsid, vpn, hpte_group, want_v, hpte_v;
211e66c3209SChristophe Leroy 	int i, ssize = mmu_kernel_ssize;
212e66c3209SChristophe Leroy 	unsigned long shift = mmu_psize_defs[psize].shift;
213e66c3209SChristophe Leroy 
214e66c3209SChristophe Leroy 	/* calculate hash */
215e66c3209SChristophe Leroy 	vsid = get_kernel_vsid(ea, ssize);
216e66c3209SChristophe Leroy 	vpn  = hpt_vpn(ea, vsid, ssize);
217e66c3209SChristophe Leroy 	hash = hpt_hash(vpn, shift, ssize);
218e66c3209SChristophe Leroy 	want_v = hpte_encode_avpn(vpn, psize, ssize);
219e66c3209SChristophe Leroy 
220e66c3209SChristophe Leroy 	/* to check in the secondary hash table, we invert the hash */
221e66c3209SChristophe Leroy 	if (!primary)
222e66c3209SChristophe Leroy 		hash = ~hash;
223e66c3209SChristophe Leroy 	hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
224e66c3209SChristophe Leroy 	for (i = 0; i < HPTES_PER_GROUP; i++) {
225e66c3209SChristophe Leroy 		hptep = htab_address + hpte_group;
226e66c3209SChristophe Leroy 		hpte_v = be64_to_cpu(hptep->v);
227e66c3209SChristophe Leroy 
228e66c3209SChristophe Leroy 		if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID)) {
229e66c3209SChristophe Leroy 			/* HPTE matches */
230e66c3209SChristophe Leroy 			*v = be64_to_cpu(hptep->v);
231e66c3209SChristophe Leroy 			*r = be64_to_cpu(hptep->r);
232e66c3209SChristophe Leroy 			return 0;
233e66c3209SChristophe Leroy 		}
234e66c3209SChristophe Leroy 		++hpte_group;
235e66c3209SChristophe Leroy 	}
236e66c3209SChristophe Leroy 	return -1;
237e66c3209SChristophe Leroy }
238e66c3209SChristophe Leroy 
pseries_find(unsigned long ea,int psize,bool primary,u64 * v,u64 * r)239e66c3209SChristophe Leroy static int pseries_find(unsigned long ea, int psize, bool primary, u64 *v, u64 *r)
240e66c3209SChristophe Leroy {
241*961f649fSMichael Ellerman 	struct {
242*961f649fSMichael Ellerman 		unsigned long v;
243*961f649fSMichael Ellerman 		unsigned long r;
244*961f649fSMichael Ellerman 	} ptes[4];
245e66c3209SChristophe Leroy 	unsigned long vsid, vpn, hash, hpte_group, want_v;
246e66c3209SChristophe Leroy 	int i, j, ssize = mmu_kernel_ssize;
247e66c3209SChristophe Leroy 	long lpar_rc = 0;
248e66c3209SChristophe Leroy 	unsigned long shift = mmu_psize_defs[psize].shift;
249e66c3209SChristophe Leroy 
250e66c3209SChristophe Leroy 	/* calculate hash */
251e66c3209SChristophe Leroy 	vsid = get_kernel_vsid(ea, ssize);
252e66c3209SChristophe Leroy 	vpn  = hpt_vpn(ea, vsid, ssize);
253e66c3209SChristophe Leroy 	hash = hpt_hash(vpn, shift, ssize);
254e66c3209SChristophe Leroy 	want_v = hpte_encode_avpn(vpn, psize, ssize);
255e66c3209SChristophe Leroy 
256e66c3209SChristophe Leroy 	/* to check in the secondary hash table, we invert the hash */
257e66c3209SChristophe Leroy 	if (!primary)
258e66c3209SChristophe Leroy 		hash = ~hash;
259e66c3209SChristophe Leroy 	hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
260e66c3209SChristophe Leroy 	/* see if we can find an entry in the hpte with this hash */
261e66c3209SChristophe Leroy 	for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) {
262e66c3209SChristophe Leroy 		lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes);
263e66c3209SChristophe Leroy 
2647c466b08SChristophe Leroy 		if (lpar_rc)
265e66c3209SChristophe Leroy 			continue;
266e66c3209SChristophe Leroy 		for (j = 0; j < 4; j++) {
267e66c3209SChristophe Leroy 			if (HPTE_V_COMPARE(ptes[j].v, want_v) &&
268e66c3209SChristophe Leroy 					(ptes[j].v & HPTE_V_VALID)) {
269e66c3209SChristophe Leroy 				/* HPTE matches */
270e66c3209SChristophe Leroy 				*v = ptes[j].v;
271e66c3209SChristophe Leroy 				*r = ptes[j].r;
272e66c3209SChristophe Leroy 				return 0;
273e66c3209SChristophe Leroy 			}
274e66c3209SChristophe Leroy 		}
275e66c3209SChristophe Leroy 	}
276e66c3209SChristophe Leroy 	return -1;
277e66c3209SChristophe Leroy }
278e66c3209SChristophe Leroy 
decode_r(int bps,unsigned long r,unsigned long * rpn,int * aps,unsigned long * lp_bits)279e66c3209SChristophe Leroy static void decode_r(int bps, unsigned long r, unsigned long *rpn, int *aps,
280e66c3209SChristophe Leroy 		unsigned long *lp_bits)
281e66c3209SChristophe Leroy {
282e66c3209SChristophe Leroy 	struct mmu_psize_def entry;
283e66c3209SChristophe Leroy 	unsigned long arpn, mask, lp;
284e66c3209SChristophe Leroy 	int penc = -2, idx = 0, shift;
285e66c3209SChristophe Leroy 
286e66c3209SChristophe Leroy 	/*.
287e66c3209SChristophe Leroy 	 * The LP field has 8 bits. Depending on the actual page size, some of
288e66c3209SChristophe Leroy 	 * these bits are concatenated with the APRN to get the RPN. The rest
289e66c3209SChristophe Leroy 	 * of the bits in the LP field is the LP value and is an encoding for
290e66c3209SChristophe Leroy 	 * the base page size and the actual page size.
291e66c3209SChristophe Leroy 	 *
292e66c3209SChristophe Leroy 	 *  -	find the mmu entry for our base page size
293e66c3209SChristophe Leroy 	 *  -	go through all page encodings and use the associated mask to
294e66c3209SChristophe Leroy 	 *	find an encoding that matches our encoding in the LP field.
295e66c3209SChristophe Leroy 	 */
296e66c3209SChristophe Leroy 	arpn = (r & HPTE_R_RPN) >> HPTE_R_RPN_SHIFT;
297e66c3209SChristophe Leroy 	lp = arpn & 0xff;
298e66c3209SChristophe Leroy 
299e66c3209SChristophe Leroy 	entry = mmu_psize_defs[bps];
300e66c3209SChristophe Leroy 	while (idx < MMU_PAGE_COUNT) {
301e66c3209SChristophe Leroy 		penc = entry.penc[idx];
302e66c3209SChristophe Leroy 		if ((penc != -1) && (mmu_psize_defs[idx].shift)) {
303e66c3209SChristophe Leroy 			shift = mmu_psize_defs[idx].shift -  HPTE_R_RPN_SHIFT;
304e66c3209SChristophe Leroy 			mask = (0x1 << (shift)) - 1;
305e66c3209SChristophe Leroy 			if ((lp & mask) == penc) {
306e66c3209SChristophe Leroy 				*aps = mmu_psize_to_shift(idx);
307e66c3209SChristophe Leroy 				*lp_bits = lp & mask;
308e66c3209SChristophe Leroy 				*rpn = arpn >> shift;
309e66c3209SChristophe Leroy 				return;
310e66c3209SChristophe Leroy 			}
311e66c3209SChristophe Leroy 		}
312e66c3209SChristophe Leroy 		idx++;
313e66c3209SChristophe Leroy 	}
314e66c3209SChristophe Leroy }
315e66c3209SChristophe Leroy 
base_hpte_find(unsigned long ea,int psize,bool primary,u64 * v,u64 * r)316e66c3209SChristophe Leroy static int base_hpte_find(unsigned long ea, int psize, bool primary, u64 *v,
317e66c3209SChristophe Leroy 			  u64 *r)
318e66c3209SChristophe Leroy {
31965e701b2SChristophe Leroy 	if (IS_ENABLED(CONFIG_PPC_PSERIES) && firmware_has_feature(FW_FEATURE_LPAR))
320e66c3209SChristophe Leroy 		return pseries_find(ea, psize, primary, v, r);
32165e701b2SChristophe Leroy 
322e66c3209SChristophe Leroy 	return native_find(ea, psize, primary, v, r);
323e66c3209SChristophe Leroy }
324e66c3209SChristophe Leroy 
hpte_find(struct pg_state * st,unsigned long ea,int psize)325e66c3209SChristophe Leroy static unsigned long hpte_find(struct pg_state *st, unsigned long ea, int psize)
326e66c3209SChristophe Leroy {
327e66c3209SChristophe Leroy 	unsigned long slot;
328e66c3209SChristophe Leroy 	u64 v  = 0, r = 0;
329e66c3209SChristophe Leroy 	unsigned long rpn, lp_bits;
330e66c3209SChristophe Leroy 	int base_psize = 0, actual_psize = 0;
331e66c3209SChristophe Leroy 
332e66c3209SChristophe Leroy 	if (ea < PAGE_OFFSET)
333e66c3209SChristophe Leroy 		return -1;
334e66c3209SChristophe Leroy 
335e66c3209SChristophe Leroy 	/* Look in primary table */
336e66c3209SChristophe Leroy 	slot = base_hpte_find(ea, psize, true, &v, &r);
337e66c3209SChristophe Leroy 
338e66c3209SChristophe Leroy 	/* Look in secondary table */
339e66c3209SChristophe Leroy 	if (slot == -1)
340790845e2SRashmica Gupta 		slot = base_hpte_find(ea, psize, false, &v, &r);
341e66c3209SChristophe Leroy 
342e66c3209SChristophe Leroy 	/* No entry found */
343e66c3209SChristophe Leroy 	if (slot == -1)
344e66c3209SChristophe Leroy 		return -1;
345e66c3209SChristophe Leroy 
346e66c3209SChristophe Leroy 	/*
347e66c3209SChristophe Leroy 	 * We found an entry in the hash page table:
348e66c3209SChristophe Leroy 	 *  - check that this has the same base page
349e66c3209SChristophe Leroy 	 *  - find the actual page size
350e66c3209SChristophe Leroy 	 *  - find the RPN
351e66c3209SChristophe Leroy 	 */
352e66c3209SChristophe Leroy 	base_psize = mmu_psize_to_shift(psize);
353e66c3209SChristophe Leroy 
354e66c3209SChristophe Leroy 	if ((v & HPTE_V_LARGE) == HPTE_V_LARGE) {
355e66c3209SChristophe Leroy 		decode_r(psize, r, &rpn, &actual_psize, &lp_bits);
356e66c3209SChristophe Leroy 	} else {
357e66c3209SChristophe Leroy 		/* 4K actual page size */
358e66c3209SChristophe Leroy 		actual_psize = 12;
359e66c3209SChristophe Leroy 		rpn = (r & HPTE_R_RPN) >> HPTE_R_RPN_SHIFT;
360e66c3209SChristophe Leroy 		/* In this case there are no LP bits */
361e66c3209SChristophe Leroy 		lp_bits = -1;
362e66c3209SChristophe Leroy 	}
363e66c3209SChristophe Leroy 	/*
364e66c3209SChristophe Leroy 	 * We didn't find a matching encoding, so the PTE we found isn't for
365e66c3209SChristophe Leroy 	 * this address.
366e66c3209SChristophe Leroy 	 */
367e66c3209SChristophe Leroy 	if (actual_psize == -1)
368e66c3209SChristophe Leroy 		return -1;
369e66c3209SChristophe Leroy 
370e66c3209SChristophe Leroy 	dump_hpte_info(st, ea, v, r, rpn, base_psize, actual_psize, lp_bits);
371e66c3209SChristophe Leroy 	return 0;
372e66c3209SChristophe Leroy }
373e66c3209SChristophe Leroy 
walk_pte(struct pg_state * st,pmd_t * pmd,unsigned long start)374e66c3209SChristophe Leroy static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
375e66c3209SChristophe Leroy {
376e66c3209SChristophe Leroy 	pte_t *pte = pte_offset_kernel(pmd, 0);
377e66c3209SChristophe Leroy 	unsigned long addr, pteval, psize;
378e66c3209SChristophe Leroy 	int i, status;
379e66c3209SChristophe Leroy 
380e66c3209SChristophe Leroy 	for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
381e66c3209SChristophe Leroy 		addr = start + i * PAGE_SIZE;
382e66c3209SChristophe Leroy 		pteval = pte_val(*pte);
383e66c3209SChristophe Leroy 
384e66c3209SChristophe Leroy 		if (addr < VMALLOC_END)
385e66c3209SChristophe Leroy 			psize = mmu_vmalloc_psize;
386e66c3209SChristophe Leroy 		else
387e66c3209SChristophe Leroy 			psize = mmu_io_psize;
38865e701b2SChristophe Leroy 
389e66c3209SChristophe Leroy 		/* check for secret 4K mappings */
39065e701b2SChristophe Leroy 		if (IS_ENABLED(CONFIG_PPC_64K_PAGES) &&
39165e701b2SChristophe Leroy 		    ((pteval & H_PAGE_COMBO) == H_PAGE_COMBO ||
39265e701b2SChristophe Leroy 		     (pteval & H_PAGE_4K_PFN) == H_PAGE_4K_PFN))
393e66c3209SChristophe Leroy 			psize = mmu_io_psize;
39465e701b2SChristophe Leroy 
395e66c3209SChristophe Leroy 		/* check for hashpte */
396e66c3209SChristophe Leroy 		status = hpte_find(st, addr, psize);
397e66c3209SChristophe Leroy 
398e66c3209SChristophe Leroy 		if (((pteval & H_PAGE_HASHPTE) != H_PAGE_HASHPTE)
399e66c3209SChristophe Leroy 				&& (status != -1)) {
400e66c3209SChristophe Leroy 		/* found a hpte that is not in the linux page tables */
401e66c3209SChristophe Leroy 			seq_printf(st->seq, "page probably bolted before linux"
402e66c3209SChristophe Leroy 				" pagetables were set: addr:%lx, pteval:%lx\n",
403e66c3209SChristophe Leroy 				addr, pteval);
404e66c3209SChristophe Leroy 		}
405e66c3209SChristophe Leroy 	}
406e66c3209SChristophe Leroy }
407e66c3209SChristophe Leroy 
walk_pmd(struct pg_state * st,pud_t * pud,unsigned long start)408e66c3209SChristophe Leroy static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
409e66c3209SChristophe Leroy {
410e66c3209SChristophe Leroy 	pmd_t *pmd = pmd_offset(pud, 0);
411e66c3209SChristophe Leroy 	unsigned long addr;
412e66c3209SChristophe Leroy 	unsigned int i;
413e66c3209SChristophe Leroy 
414e66c3209SChristophe Leroy 	for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
415e66c3209SChristophe Leroy 		addr = start + i * PMD_SIZE;
416e66c3209SChristophe Leroy 		if (!pmd_none(*pmd))
417e66c3209SChristophe Leroy 			/* pmd exists */
418e66c3209SChristophe Leroy 			walk_pte(st, pmd, addr);
419e66c3209SChristophe Leroy 	}
420e66c3209SChristophe Leroy }
421e66c3209SChristophe Leroy 
walk_pud(struct pg_state * st,p4d_t * p4d,unsigned long start)4222fb47060SMike Rapoport static void walk_pud(struct pg_state *st, p4d_t *p4d, unsigned long start)
423e66c3209SChristophe Leroy {
4242fb47060SMike Rapoport 	pud_t *pud = pud_offset(p4d, 0);
425e66c3209SChristophe Leroy 	unsigned long addr;
426e66c3209SChristophe Leroy 	unsigned int i;
427e66c3209SChristophe Leroy 
428e66c3209SChristophe Leroy 	for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
429e66c3209SChristophe Leroy 		addr = start + i * PUD_SIZE;
430e66c3209SChristophe Leroy 		if (!pud_none(*pud))
431e66c3209SChristophe Leroy 			/* pud exists */
432e66c3209SChristophe Leroy 			walk_pmd(st, pud, addr);
433e66c3209SChristophe Leroy 	}
434e66c3209SChristophe Leroy }
435e66c3209SChristophe Leroy 
walk_p4d(struct pg_state * st,pgd_t * pgd,unsigned long start)4362fb47060SMike Rapoport static void walk_p4d(struct pg_state *st, pgd_t *pgd, unsigned long start)
4372fb47060SMike Rapoport {
4382fb47060SMike Rapoport 	p4d_t *p4d = p4d_offset(pgd, 0);
4392fb47060SMike Rapoport 	unsigned long addr;
4402fb47060SMike Rapoport 	unsigned int i;
4412fb47060SMike Rapoport 
4422fb47060SMike Rapoport 	for (i = 0; i < PTRS_PER_P4D; i++, p4d++) {
4432fb47060SMike Rapoport 		addr = start + i * P4D_SIZE;
4442fb47060SMike Rapoport 		if (!p4d_none(*p4d))
4452fb47060SMike Rapoport 			/* p4d exists */
4462fb47060SMike Rapoport 			walk_pud(st, p4d, addr);
4472fb47060SMike Rapoport 	}
4482fb47060SMike Rapoport }
4492fb47060SMike Rapoport 
walk_pagetables(struct pg_state * st)450e66c3209SChristophe Leroy static void walk_pagetables(struct pg_state *st)
451e66c3209SChristophe Leroy {
452e66c3209SChristophe Leroy 	pgd_t *pgd = pgd_offset_k(0UL);
453e66c3209SChristophe Leroy 	unsigned int i;
454e66c3209SChristophe Leroy 	unsigned long addr;
455e66c3209SChristophe Leroy 
456e66c3209SChristophe Leroy 	/*
457e66c3209SChristophe Leroy 	 * Traverse the linux pagetable structure and dump pages that are in
458e66c3209SChristophe Leroy 	 * the hash pagetable.
459e66c3209SChristophe Leroy 	 */
460e66c3209SChristophe Leroy 	for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
461e66c3209SChristophe Leroy 		addr = KERN_VIRT_START + i * PGDIR_SIZE;
462e66c3209SChristophe Leroy 		if (!pgd_none(*pgd))
463e66c3209SChristophe Leroy 			/* pgd exists */
4642fb47060SMike Rapoport 			walk_p4d(st, pgd, addr);
465e66c3209SChristophe Leroy 	}
466e66c3209SChristophe Leroy }
467e66c3209SChristophe Leroy 
468e66c3209SChristophe Leroy 
walk_linearmapping(struct pg_state * st)469e66c3209SChristophe Leroy static void walk_linearmapping(struct pg_state *st)
470e66c3209SChristophe Leroy {
471e66c3209SChristophe Leroy 	unsigned long addr;
472e66c3209SChristophe Leroy 
473e66c3209SChristophe Leroy 	/*
474e66c3209SChristophe Leroy 	 * Traverse the linear mapping section of virtual memory and dump pages
475e66c3209SChristophe Leroy 	 * that are in the hash pagetable.
476e66c3209SChristophe Leroy 	 */
477e66c3209SChristophe Leroy 	unsigned long psize = 1 << mmu_psize_defs[mmu_linear_psize].shift;
478e66c3209SChristophe Leroy 
479e66c3209SChristophe Leroy 	for (addr = PAGE_OFFSET; addr < PAGE_OFFSET +
480e66c3209SChristophe Leroy 			memblock_end_of_DRAM(); addr += psize)
481e66c3209SChristophe Leroy 		hpte_find(st, addr, mmu_linear_psize);
482e66c3209SChristophe Leroy }
483e66c3209SChristophe Leroy 
walk_vmemmap(struct pg_state * st)484e66c3209SChristophe Leroy static void walk_vmemmap(struct pg_state *st)
485e66c3209SChristophe Leroy {
486e66c3209SChristophe Leroy 	struct vmemmap_backing *ptr = vmemmap_list;
487e66c3209SChristophe Leroy 
48865e701b2SChristophe Leroy 	if (!IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP))
48965e701b2SChristophe Leroy 		return;
490e66c3209SChristophe Leroy 	/*
491e66c3209SChristophe Leroy 	 * Traverse the vmemmaped memory and dump pages that are in the hash
492e66c3209SChristophe Leroy 	 * pagetable.
493e66c3209SChristophe Leroy 	 */
494e66c3209SChristophe Leroy 	while (ptr->list) {
495e66c3209SChristophe Leroy 		hpte_find(st, ptr->virt_addr, mmu_vmemmap_psize);
496e66c3209SChristophe Leroy 		ptr = ptr->list;
497e66c3209SChristophe Leroy 	}
498e66c3209SChristophe Leroy 	seq_puts(st->seq, "---[ vmemmap end ]---\n");
499e66c3209SChristophe Leroy }
500e66c3209SChristophe Leroy 
populate_markers(void)501e66c3209SChristophe Leroy static void populate_markers(void)
502e66c3209SChristophe Leroy {
503e66c3209SChristophe Leroy 	address_markers[0].start_address = PAGE_OFFSET;
504e66c3209SChristophe Leroy 	address_markers[1].start_address = VMALLOC_START;
505e66c3209SChristophe Leroy 	address_markers[2].start_address = VMALLOC_END;
506e66c3209SChristophe Leroy 	address_markers[3].start_address = ISA_IO_BASE;
507e66c3209SChristophe Leroy 	address_markers[4].start_address = ISA_IO_END;
508e66c3209SChristophe Leroy 	address_markers[5].start_address = PHB_IO_BASE;
509e66c3209SChristophe Leroy 	address_markers[6].start_address = PHB_IO_END;
510e66c3209SChristophe Leroy 	address_markers[7].start_address = IOREMAP_BASE;
511e66c3209SChristophe Leroy 	address_markers[8].start_address = IOREMAP_END;
5120034d395SAneesh Kumar K.V 	address_markers[9].start_address =  H_VMEMMAP_START;
513e66c3209SChristophe Leroy }
514e66c3209SChristophe Leroy 
ptdump_show(struct seq_file * m,void * v)515e66c3209SChristophe Leroy static int ptdump_show(struct seq_file *m, void *v)
516e66c3209SChristophe Leroy {
517e66c3209SChristophe Leroy 	struct pg_state st = {
518e66c3209SChristophe Leroy 		.seq = m,
519e66c3209SChristophe Leroy 		.start_address = PAGE_OFFSET,
520e66c3209SChristophe Leroy 		.marker = address_markers,
521e66c3209SChristophe Leroy 	};
522e66c3209SChristophe Leroy 	/*
523e66c3209SChristophe Leroy 	 * Traverse the 0xc, 0xd and 0xf areas of the kernel virtual memory and
524e66c3209SChristophe Leroy 	 * dump pages that are in the hash pagetable.
525e66c3209SChristophe Leroy 	 */
526e66c3209SChristophe Leroy 	walk_linearmapping(&st);
527e66c3209SChristophe Leroy 	walk_pagetables(&st);
528e66c3209SChristophe Leroy 	walk_vmemmap(&st);
529e66c3209SChristophe Leroy 	return 0;
530e66c3209SChristophe Leroy }
531e66c3209SChristophe Leroy 
53211f27a7fSChristophe Leroy DEFINE_SHOW_ATTRIBUTE(ptdump);
533e66c3209SChristophe Leroy 
ptdump_init(void)534e66c3209SChristophe Leroy static int ptdump_init(void)
535e66c3209SChristophe Leroy {
536e66c3209SChristophe Leroy 	if (!radix_enabled()) {
537e66c3209SChristophe Leroy 		populate_markers();
538f3c05201SGreg Kroah-Hartman 		debugfs_create_file("kernel_hash_pagetable", 0400, NULL, NULL,
539f3c05201SGreg Kroah-Hartman 				    &ptdump_fops);
540e66c3209SChristophe Leroy 	}
541e66c3209SChristophe Leroy 	return 0;
542e66c3209SChristophe Leroy }
543e66c3209SChristophe Leroy device_initcall(ptdump_init);
544