Commit | Line | Data |
---|---|---|
8eb07b18 RG |
1 | /* |
2 | * Copyright 2016, Rashmica Gupta, IBM Corp. | |
3 | * | |
4 | * This traverses the kernel pagetables and dumps the | |
5 | * information about the used sections of memory to | |
6 | * /sys/kernel/debug/kernel_pagetables. | |
7 | * | |
8 | * Derived from the arm64 implementation: | |
9 | * Copyright (c) 2014, The Linux Foundation, Laura Abbott. | |
10 | * (C) Copyright 2008 Intel Corporation, Arjan van de Ven. | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU General Public License | |
14 | * as published by the Free Software Foundation; version 2 | |
15 | * of the License. | |
16 | */ | |
17 | #include <linux/debugfs.h> | |
18 | #include <linux/fs.h> | |
19 | #include <linux/io.h> | |
20 | #include <linux/mm.h> | |
21 | #include <linux/sched.h> | |
22 | #include <linux/seq_file.h> | |
23 | #include <asm/fixmap.h> | |
24 | #include <asm/pgtable.h> | |
25 | #include <linux/const.h> | |
26 | #include <asm/page.h> | |
27 | #include <asm/pgalloc.h> | |
28 | ||
6c01bbd2 CL |
29 | #ifdef CONFIG_PPC32 |
30 | #define KERN_VIRT_START 0 | |
31 | #endif | |
32 | ||
8eb07b18 RG |
33 | /* |
34 | * To visualise what is happening, | |
35 | * | |
36 | * - PTRS_PER_P** = how many entries there are in the corresponding P** | |
37 | * - P**_SHIFT = how many bits of the address we use to index into the | |
38 | * corresponding P** | |
39 | * - P**_SIZE is how much memory we can access through the table - not the | |
40 | * size of the table itself. | |
41 | * P**={PGD, PUD, PMD, PTE} | |
42 | * | |
43 | * | |
44 | * Each entry of the PGD points to a PUD. Each entry of a PUD points to a | |
45 | * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to | |
46 | * a page. | |
47 | * | |
48 | * In the case where there are only 3 levels, the PUD is folded into the | |
49 | * PGD: every PUD has only one entry which points to the PMD. | |
50 | * | |
51 | * The page dumper groups page table entries of the same type into a single | |
52 | * description. It uses pg_state to track the range information while | |
53 | * iterating over the PTE entries. When the continuity is broken it then | |
54 | * dumps out a description of the range - ie PTEs that are virtually contiguous | |
55 | * with the same PTE flags are chunked together. This is to make it clear how | |
56 | * different areas of the kernel virtual memory are used. | |
57 | * | |
58 | */ | |
59 | struct pg_state { | |
60 | struct seq_file *seq; | |
61 | const struct addr_marker *marker; | |
62 | unsigned long start_address; | |
aaa22952 OH |
63 | unsigned long start_pa; |
64 | unsigned long last_pa; | |
8eb07b18 RG |
65 | unsigned int level; |
66 | u64 current_flags; | |
67 | }; | |
68 | ||
69 | struct addr_marker { | |
70 | unsigned long start_address; | |
71 | const char *name; | |
72 | }; | |
73 | ||
74 | static struct addr_marker address_markers[] = { | |
75 | { 0, "Start of kernel VM" }, | |
76 | { 0, "vmalloc() Area" }, | |
77 | { 0, "vmalloc() End" }, | |
6c01bbd2 | 78 | #ifdef CONFIG_PPC64 |
8eb07b18 RG |
79 | { 0, "isa I/O start" }, |
80 | { 0, "isa I/O end" }, | |
81 | { 0, "phb I/O start" }, | |
82 | { 0, "phb I/O end" }, | |
83 | { 0, "I/O remap start" }, | |
84 | { 0, "I/O remap end" }, | |
85 | { 0, "vmemmap start" }, | |
6c01bbd2 CL |
86 | #else |
87 | { 0, "Early I/O remap start" }, | |
88 | { 0, "Early I/O remap end" }, | |
89 | #ifdef CONFIG_NOT_COHERENT_CACHE | |
90 | { 0, "Consistent mem start" }, | |
91 | { 0, "Consistent mem end" }, | |
92 | #endif | |
93 | #ifdef CONFIG_HIGHMEM | |
94 | { 0, "Highmem PTEs start" }, | |
95 | { 0, "Highmem PTEs end" }, | |
96 | #endif | |
97 | { 0, "Fixmap start" }, | |
98 | { 0, "Fixmap end" }, | |
99 | #endif | |
8eb07b18 RG |
100 | { -1, NULL }, |
101 | }; | |
102 | ||
103 | struct flag_info { | |
104 | u64 mask; | |
105 | u64 val; | |
106 | const char *set; | |
107 | const char *clear; | |
108 | bool is_val; | |
109 | int shift; | |
110 | }; | |
111 | ||
112 | static const struct flag_info flag_array[] = { | |
113 | { | |
114 | #ifdef CONFIG_PPC_STD_MMU_64 | |
115 | .mask = _PAGE_PRIVILEGED, | |
116 | .val = 0, | |
117 | #else | |
118 | .mask = _PAGE_USER, | |
119 | .val = _PAGE_USER, | |
120 | #endif | |
121 | .set = "user", | |
122 | .clear = " ", | |
123 | }, { | |
fd893fe5 | 124 | #if _PAGE_RO == 0 |
8eb07b18 RG |
125 | .mask = _PAGE_RW, |
126 | .val = _PAGE_RW, | |
fd893fe5 CL |
127 | #else |
128 | .mask = _PAGE_RO, | |
129 | .val = 0, | |
130 | #endif | |
8eb07b18 RG |
131 | .set = "rw", |
132 | .clear = "ro", | |
133 | }, { | |
134 | .mask = _PAGE_EXEC, | |
135 | .val = _PAGE_EXEC, | |
136 | .set = " X ", | |
137 | .clear = " ", | |
138 | }, { | |
139 | .mask = _PAGE_PTE, | |
140 | .val = _PAGE_PTE, | |
141 | .set = "pte", | |
142 | .clear = " ", | |
143 | }, { | |
144 | .mask = _PAGE_PRESENT, | |
145 | .val = _PAGE_PRESENT, | |
146 | .set = "present", | |
147 | .clear = " ", | |
148 | }, { | |
149 | #ifdef CONFIG_PPC_STD_MMU_64 | |
150 | .mask = H_PAGE_HASHPTE, | |
151 | .val = H_PAGE_HASHPTE, | |
152 | #else | |
153 | .mask = _PAGE_HASHPTE, | |
154 | .val = _PAGE_HASHPTE, | |
155 | #endif | |
156 | .set = "hpte", | |
157 | .clear = " ", | |
158 | }, { | |
159 | #ifndef CONFIG_PPC_STD_MMU_64 | |
160 | .mask = _PAGE_GUARDED, | |
161 | .val = _PAGE_GUARDED, | |
162 | .set = "guarded", | |
163 | .clear = " ", | |
164 | }, { | |
165 | #endif | |
166 | .mask = _PAGE_DIRTY, | |
167 | .val = _PAGE_DIRTY, | |
168 | .set = "dirty", | |
169 | .clear = " ", | |
170 | }, { | |
171 | .mask = _PAGE_ACCESSED, | |
172 | .val = _PAGE_ACCESSED, | |
173 | .set = "accessed", | |
174 | .clear = " ", | |
175 | }, { | |
176 | #ifndef CONFIG_PPC_STD_MMU_64 | |
177 | .mask = _PAGE_WRITETHRU, | |
178 | .val = _PAGE_WRITETHRU, | |
179 | .set = "write through", | |
180 | .clear = " ", | |
181 | }, { | |
182 | #endif | |
70538eaa | 183 | #ifndef CONFIG_PPC_BOOK3S_64 |
8eb07b18 RG |
184 | .mask = _PAGE_NO_CACHE, |
185 | .val = _PAGE_NO_CACHE, | |
186 | .set = "no cache", | |
187 | .clear = " ", | |
188 | }, { | |
70538eaa OH |
189 | #else |
190 | .mask = _PAGE_NON_IDEMPOTENT, | |
191 | .val = _PAGE_NON_IDEMPOTENT, | |
192 | .set = "non-idempotent", | |
193 | .clear = " ", | |
194 | }, { | |
195 | .mask = _PAGE_TOLERANT, | |
196 | .val = _PAGE_TOLERANT, | |
197 | .set = "tolerant", | |
198 | .clear = " ", | |
199 | }, { | |
200 | #endif | |
dd5ac03e | 201 | #ifdef CONFIG_PPC_BOOK3S_64 |
8eb07b18 RG |
202 | .mask = H_PAGE_BUSY, |
203 | .val = H_PAGE_BUSY, | |
204 | .set = "busy", | |
205 | }, { | |
206 | #ifdef CONFIG_PPC_64K_PAGES | |
207 | .mask = H_PAGE_COMBO, | |
208 | .val = H_PAGE_COMBO, | |
209 | .set = "combo", | |
210 | }, { | |
211 | .mask = H_PAGE_4K_PFN, | |
212 | .val = H_PAGE_4K_PFN, | |
213 | .set = "4K_pfn", | |
214 | }, { | |
215 | #endif | |
216 | .mask = H_PAGE_F_GIX, | |
217 | .val = H_PAGE_F_GIX, | |
218 | .set = "f_gix", | |
219 | .is_val = true, | |
220 | .shift = H_PAGE_F_GIX_SHIFT, | |
221 | }, { | |
222 | .mask = H_PAGE_F_SECOND, | |
223 | .val = H_PAGE_F_SECOND, | |
224 | .set = "f_second", | |
225 | }, { | |
dd5ac03e | 226 | #endif |
8eb07b18 RG |
227 | .mask = _PAGE_SPECIAL, |
228 | .val = _PAGE_SPECIAL, | |
229 | .set = "special", | |
fd893fe5 CL |
230 | }, { |
231 | .mask = _PAGE_SHARED, | |
232 | .val = _PAGE_SHARED, | |
233 | .set = "shared", | |
8eb07b18 RG |
234 | } |
235 | }; | |
236 | ||
237 | struct pgtable_level { | |
238 | const struct flag_info *flag; | |
239 | size_t num; | |
240 | u64 mask; | |
241 | }; | |
242 | ||
243 | static struct pgtable_level pg_level[] = { | |
244 | { | |
245 | }, { /* pgd */ | |
246 | .flag = flag_array, | |
247 | .num = ARRAY_SIZE(flag_array), | |
248 | }, { /* pud */ | |
249 | .flag = flag_array, | |
250 | .num = ARRAY_SIZE(flag_array), | |
251 | }, { /* pmd */ | |
252 | .flag = flag_array, | |
253 | .num = ARRAY_SIZE(flag_array), | |
254 | }, { /* pte */ | |
255 | .flag = flag_array, | |
256 | .num = ARRAY_SIZE(flag_array), | |
257 | }, | |
258 | }; | |
259 | ||
260 | static void dump_flag_info(struct pg_state *st, const struct flag_info | |
261 | *flag, u64 pte, int num) | |
262 | { | |
263 | unsigned int i; | |
264 | ||
265 | for (i = 0; i < num; i++, flag++) { | |
266 | const char *s = NULL; | |
267 | u64 val; | |
268 | ||
269 | /* flag not defined so don't check it */ | |
270 | if (flag->mask == 0) | |
271 | continue; | |
272 | /* Some 'flags' are actually values */ | |
273 | if (flag->is_val) { | |
274 | val = pte & flag->val; | |
275 | if (flag->shift) | |
276 | val = val >> flag->shift; | |
277 | seq_printf(st->seq, " %s:%llx", flag->set, val); | |
278 | } else { | |
279 | if ((pte & flag->mask) == flag->val) | |
280 | s = flag->set; | |
281 | else | |
282 | s = flag->clear; | |
283 | if (s) | |
284 | seq_printf(st->seq, " %s", s); | |
285 | } | |
286 | st->current_flags &= ~flag->mask; | |
287 | } | |
288 | if (st->current_flags != 0) | |
289 | seq_printf(st->seq, " unknown flags:%llx", st->current_flags); | |
290 | } | |
291 | ||
292 | static void dump_addr(struct pg_state *st, unsigned long addr) | |
293 | { | |
294 | static const char units[] = "KMGTPE"; | |
295 | const char *unit = units; | |
296 | unsigned long delta; | |
297 | ||
78a18dbf | 298 | #ifdef CONFIG_PPC64 |
aaa22952 OH |
299 | seq_printf(st->seq, "0x%016lx-0x%016lx ", st->start_address, addr-1); |
300 | seq_printf(st->seq, "0x%016lx ", st->start_pa); | |
78a18dbf CL |
301 | #else |
302 | seq_printf(st->seq, "0x%08lx-0x%08lx ", st->start_address, addr - 1); | |
303 | seq_printf(st->seq, "0x%08lx ", st->start_pa); | |
304 | #endif | |
aaa22952 | 305 | |
8eb07b18 RG |
306 | delta = (addr - st->start_address) >> 10; |
307 | /* Work out what appropriate unit to use */ | |
308 | while (!(delta & 1023) && unit[1]) { | |
309 | delta >>= 10; | |
310 | unit++; | |
311 | } | |
312 | seq_printf(st->seq, "%9lu%c", delta, *unit); | |
313 | ||
314 | } | |
315 | ||
316 | static void note_page(struct pg_state *st, unsigned long addr, | |
317 | unsigned int level, u64 val) | |
318 | { | |
319 | u64 flag = val & pg_level[level].mask; | |
aaa22952 OH |
320 | u64 pa = val & PTE_RPN_MASK; |
321 | ||
8eb07b18 RG |
322 | /* At first no level is set */ |
323 | if (!st->level) { | |
324 | st->level = level; | |
325 | st->current_flags = flag; | |
326 | st->start_address = addr; | |
aaa22952 OH |
327 | st->start_pa = pa; |
328 | st->last_pa = pa; | |
8eb07b18 RG |
329 | seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
330 | /* | |
331 | * Dump the section of virtual memory when: | |
332 | * - the PTE flags from one entry to the next differs. | |
333 | * - we change levels in the tree. | |
334 | * - the address is in a different section of memory and is thus | |
335 | * used for a different purpose, regardless of the flags. | |
aaa22952 | 336 | * - the pa of this page is not adjacent to the last inspected page |
8eb07b18 RG |
337 | */ |
338 | } else if (flag != st->current_flags || level != st->level || | |
aaa22952 OH |
339 | addr >= st->marker[1].start_address || |
340 | pa != st->last_pa + PAGE_SIZE) { | |
8eb07b18 RG |
341 | |
342 | /* Check the PTE flags */ | |
343 | if (st->current_flags) { | |
344 | dump_addr(st, addr); | |
345 | ||
346 | /* Dump all the flags */ | |
347 | if (pg_level[st->level].flag) | |
348 | dump_flag_info(st, pg_level[st->level].flag, | |
349 | st->current_flags, | |
350 | pg_level[st->level].num); | |
351 | ||
352 | seq_puts(st->seq, "\n"); | |
353 | } | |
354 | ||
355 | /* | |
356 | * Address indicates we have passed the end of the | |
357 | * current section of virtual memory | |
358 | */ | |
359 | while (addr >= st->marker[1].start_address) { | |
360 | st->marker++; | |
361 | seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); | |
362 | } | |
363 | st->start_address = addr; | |
aaa22952 OH |
364 | st->start_pa = pa; |
365 | st->last_pa = pa; | |
8eb07b18 RG |
366 | st->current_flags = flag; |
367 | st->level = level; | |
aaa22952 OH |
368 | } else { |
369 | st->last_pa = pa; | |
8eb07b18 RG |
370 | } |
371 | } | |
372 | ||
373 | static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start) | |
374 | { | |
375 | pte_t *pte = pte_offset_kernel(pmd, 0); | |
376 | unsigned long addr; | |
377 | unsigned int i; | |
378 | ||
379 | for (i = 0; i < PTRS_PER_PTE; i++, pte++) { | |
380 | addr = start + i * PAGE_SIZE; | |
381 | note_page(st, addr, 4, pte_val(*pte)); | |
382 | ||
383 | } | |
384 | } | |
385 | ||
386 | static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start) | |
387 | { | |
388 | pmd_t *pmd = pmd_offset(pud, 0); | |
389 | unsigned long addr; | |
390 | unsigned int i; | |
391 | ||
392 | for (i = 0; i < PTRS_PER_PMD; i++, pmd++) { | |
393 | addr = start + i * PMD_SIZE; | |
394 | if (!pmd_none(*pmd)) | |
395 | /* pmd exists */ | |
396 | walk_pte(st, pmd, addr); | |
397 | else | |
398 | note_page(st, addr, 3, pmd_val(*pmd)); | |
399 | } | |
400 | } | |
401 | ||
402 | static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start) | |
403 | { | |
404 | pud_t *pud = pud_offset(pgd, 0); | |
405 | unsigned long addr; | |
406 | unsigned int i; | |
407 | ||
408 | for (i = 0; i < PTRS_PER_PUD; i++, pud++) { | |
409 | addr = start + i * PUD_SIZE; | |
410 | if (!pud_none(*pud)) | |
411 | /* pud exists */ | |
412 | walk_pmd(st, pud, addr); | |
413 | else | |
414 | note_page(st, addr, 2, pud_val(*pud)); | |
415 | } | |
416 | } | |
417 | ||
418 | static void walk_pagetables(struct pg_state *st) | |
419 | { | |
420 | pgd_t *pgd = pgd_offset_k(0UL); | |
421 | unsigned int i; | |
422 | unsigned long addr; | |
423 | ||
424 | /* | |
425 | * Traverse the linux pagetable structure and dump pages that are in | |
426 | * the hash pagetable. | |
427 | */ | |
428 | for (i = 0; i < PTRS_PER_PGD; i++, pgd++) { | |
429 | addr = KERN_VIRT_START + i * PGDIR_SIZE; | |
430 | if (!pgd_none(*pgd)) | |
431 | /* pgd exists */ | |
432 | walk_pud(st, pgd, addr); | |
433 | else | |
434 | note_page(st, addr, 1, pgd_val(*pgd)); | |
435 | } | |
436 | } | |
437 | ||
438 | static void populate_markers(void) | |
439 | { | |
6c01bbd2 CL |
440 | int i = 0; |
441 | ||
442 | address_markers[i++].start_address = PAGE_OFFSET; | |
443 | address_markers[i++].start_address = VMALLOC_START; | |
444 | address_markers[i++].start_address = VMALLOC_END; | |
445 | #ifdef CONFIG_PPC64 | |
446 | address_markers[i++].start_address = ISA_IO_BASE; | |
447 | address_markers[i++].start_address = ISA_IO_END; | |
448 | address_markers[i++].start_address = PHB_IO_BASE; | |
449 | address_markers[i++].start_address = PHB_IO_END; | |
450 | address_markers[i++].start_address = IOREMAP_BASE; | |
451 | address_markers[i++].start_address = IOREMAP_END; | |
8eb07b18 | 452 | #ifdef CONFIG_PPC_STD_MMU_64 |
6c01bbd2 | 453 | address_markers[i++].start_address = H_VMEMMAP_BASE; |
8eb07b18 | 454 | #else |
6c01bbd2 CL |
455 | address_markers[i++].start_address = VMEMMAP_BASE; |
456 | #endif | |
457 | #else /* !CONFIG_PPC64 */ | |
458 | address_markers[i++].start_address = ioremap_bot; | |
459 | address_markers[i++].start_address = IOREMAP_TOP; | |
460 | #ifdef CONFIG_NOT_COHERENT_CACHE | |
461 | address_markers[i++].start_address = IOREMAP_TOP; | |
462 | address_markers[i++].start_address = IOREMAP_TOP + | |
463 | CONFIG_CONSISTENT_SIZE; | |
464 | #endif | |
465 | #ifdef CONFIG_HIGHMEM | |
466 | address_markers[i++].start_address = PKMAP_BASE; | |
467 | address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP); | |
8eb07b18 | 468 | #endif |
6c01bbd2 CL |
469 | address_markers[i++].start_address = FIXADDR_START; |
470 | address_markers[i++].start_address = FIXADDR_TOP; | |
471 | #endif /* CONFIG_PPC64 */ | |
8eb07b18 RG |
472 | } |
473 | ||
474 | static int ptdump_show(struct seq_file *m, void *v) | |
475 | { | |
476 | struct pg_state st = { | |
477 | .seq = m, | |
478 | .start_address = KERN_VIRT_START, | |
479 | .marker = address_markers, | |
480 | }; | |
481 | /* Traverse kernel page tables */ | |
482 | walk_pagetables(&st); | |
483 | note_page(&st, 0, 0, 0); | |
484 | return 0; | |
485 | } | |
486 | ||
487 | ||
488 | static int ptdump_open(struct inode *inode, struct file *file) | |
489 | { | |
490 | return single_open(file, ptdump_show, NULL); | |
491 | } | |
492 | ||
493 | static const struct file_operations ptdump_fops = { | |
494 | .open = ptdump_open, | |
495 | .read = seq_read, | |
496 | .llseek = seq_lseek, | |
497 | .release = single_release, | |
498 | }; | |
499 | ||
500 | static void build_pgtable_complete_mask(void) | |
501 | { | |
502 | unsigned int i, j; | |
503 | ||
504 | for (i = 0; i < ARRAY_SIZE(pg_level); i++) | |
505 | if (pg_level[i].flag) | |
506 | for (j = 0; j < pg_level[i].num; j++) | |
507 | pg_level[i].mask |= pg_level[i].flag[j].mask; | |
508 | } | |
509 | ||
510 | static int ptdump_init(void) | |
511 | { | |
512 | struct dentry *debugfs_file; | |
513 | ||
514 | populate_markers(); | |
515 | build_pgtable_complete_mask(); | |
516 | debugfs_file = debugfs_create_file("kernel_pagetables", 0400, NULL, | |
517 | NULL, &ptdump_fops); | |
518 | return debugfs_file ? 0 : -ENOMEM; | |
519 | } | |
520 | device_initcall(ptdump_init); |