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