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> | |
bfb9956a | 19 | #include <linux/hugetlb.h> |
8eb07b18 RG |
20 | #include <linux/io.h> |
21 | #include <linux/mm.h> | |
462951cd | 22 | #include <linux/highmem.h> |
8eb07b18 RG |
23 | #include <linux/sched.h> |
24 | #include <linux/seq_file.h> | |
25 | #include <asm/fixmap.h> | |
26 | #include <asm/pgtable.h> | |
27 | #include <linux/const.h> | |
28 | #include <asm/page.h> | |
29 | #include <asm/pgalloc.h> | |
30 | ||
97026b5a CL |
31 | #include "dump_linuxpagetables.h" |
32 | ||
6c01bbd2 CL |
33 | #ifdef CONFIG_PPC32 |
34 | #define KERN_VIRT_START 0 | |
35 | #endif | |
36 | ||
8eb07b18 RG |
37 | /* |
38 | * To visualise what is happening, | |
39 | * | |
40 | * - PTRS_PER_P** = how many entries there are in the corresponding P** | |
41 | * - P**_SHIFT = how many bits of the address we use to index into the | |
42 | * corresponding P** | |
43 | * - P**_SIZE is how much memory we can access through the table - not the | |
44 | * size of the table itself. | |
45 | * P**={PGD, PUD, PMD, PTE} | |
46 | * | |
47 | * | |
48 | * Each entry of the PGD points to a PUD. Each entry of a PUD points to a | |
49 | * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to | |
50 | * a page. | |
51 | * | |
52 | * In the case where there are only 3 levels, the PUD is folded into the | |
53 | * PGD: every PUD has only one entry which points to the PMD. | |
54 | * | |
55 | * The page dumper groups page table entries of the same type into a single | |
56 | * description. It uses pg_state to track the range information while | |
57 | * iterating over the PTE entries. When the continuity is broken it then | |
58 | * dumps out a description of the range - ie PTEs that are virtually contiguous | |
59 | * with the same PTE flags are chunked together. This is to make it clear how | |
60 | * different areas of the kernel virtual memory are used. | |
61 | * | |
62 | */ | |
63 | struct pg_state { | |
64 | struct seq_file *seq; | |
65 | const struct addr_marker *marker; | |
66 | unsigned long start_address; | |
aaa22952 OH |
67 | unsigned long start_pa; |
68 | unsigned long last_pa; | |
8eb07b18 RG |
69 | unsigned int level; |
70 | u64 current_flags; | |
71 | }; | |
72 | ||
73 | struct addr_marker { | |
74 | unsigned long start_address; | |
75 | const char *name; | |
76 | }; | |
77 | ||
78 | static struct addr_marker address_markers[] = { | |
79 | { 0, "Start of kernel VM" }, | |
80 | { 0, "vmalloc() Area" }, | |
81 | { 0, "vmalloc() End" }, | |
6c01bbd2 | 82 | #ifdef CONFIG_PPC64 |
8eb07b18 RG |
83 | { 0, "isa I/O start" }, |
84 | { 0, "isa I/O end" }, | |
85 | { 0, "phb I/O start" }, | |
86 | { 0, "phb I/O end" }, | |
87 | { 0, "I/O remap start" }, | |
88 | { 0, "I/O remap end" }, | |
89 | { 0, "vmemmap start" }, | |
6c01bbd2 CL |
90 | #else |
91 | { 0, "Early I/O remap start" }, | |
92 | { 0, "Early I/O remap end" }, | |
93 | #ifdef CONFIG_NOT_COHERENT_CACHE | |
94 | { 0, "Consistent mem start" }, | |
95 | { 0, "Consistent mem end" }, | |
96 | #endif | |
97 | #ifdef CONFIG_HIGHMEM | |
98 | { 0, "Highmem PTEs start" }, | |
99 | { 0, "Highmem PTEs end" }, | |
100 | #endif | |
101 | { 0, "Fixmap start" }, | |
102 | { 0, "Fixmap end" }, | |
103 | #endif | |
8eb07b18 RG |
104 | { -1, NULL }, |
105 | }; | |
106 | ||
8eb07b18 RG |
107 | static void dump_flag_info(struct pg_state *st, const struct flag_info |
108 | *flag, u64 pte, int num) | |
109 | { | |
110 | unsigned int i; | |
111 | ||
112 | for (i = 0; i < num; i++, flag++) { | |
113 | const char *s = NULL; | |
114 | u64 val; | |
115 | ||
116 | /* flag not defined so don't check it */ | |
117 | if (flag->mask == 0) | |
118 | continue; | |
119 | /* Some 'flags' are actually values */ | |
120 | if (flag->is_val) { | |
121 | val = pte & flag->val; | |
122 | if (flag->shift) | |
123 | val = val >> flag->shift; | |
124 | seq_printf(st->seq, " %s:%llx", flag->set, val); | |
125 | } else { | |
126 | if ((pte & flag->mask) == flag->val) | |
127 | s = flag->set; | |
128 | else | |
129 | s = flag->clear; | |
130 | if (s) | |
131 | seq_printf(st->seq, " %s", s); | |
132 | } | |
133 | st->current_flags &= ~flag->mask; | |
134 | } | |
135 | if (st->current_flags != 0) | |
136 | seq_printf(st->seq, " unknown flags:%llx", st->current_flags); | |
137 | } | |
138 | ||
139 | static void dump_addr(struct pg_state *st, unsigned long addr) | |
140 | { | |
141 | static const char units[] = "KMGTPE"; | |
142 | const char *unit = units; | |
143 | unsigned long delta; | |
144 | ||
78a18dbf | 145 | #ifdef CONFIG_PPC64 |
aaa22952 OH |
146 | seq_printf(st->seq, "0x%016lx-0x%016lx ", st->start_address, addr-1); |
147 | seq_printf(st->seq, "0x%016lx ", st->start_pa); | |
78a18dbf CL |
148 | #else |
149 | seq_printf(st->seq, "0x%08lx-0x%08lx ", st->start_address, addr - 1); | |
150 | seq_printf(st->seq, "0x%08lx ", st->start_pa); | |
151 | #endif | |
aaa22952 | 152 | |
8eb07b18 RG |
153 | delta = (addr - st->start_address) >> 10; |
154 | /* Work out what appropriate unit to use */ | |
155 | while (!(delta & 1023) && unit[1]) { | |
156 | delta >>= 10; | |
157 | unit++; | |
158 | } | |
159 | seq_printf(st->seq, "%9lu%c", delta, *unit); | |
160 | ||
161 | } | |
162 | ||
163 | static void note_page(struct pg_state *st, unsigned long addr, | |
164 | unsigned int level, u64 val) | |
165 | { | |
166 | u64 flag = val & pg_level[level].mask; | |
aaa22952 OH |
167 | u64 pa = val & PTE_RPN_MASK; |
168 | ||
8eb07b18 RG |
169 | /* At first no level is set */ |
170 | if (!st->level) { | |
171 | st->level = level; | |
172 | st->current_flags = flag; | |
173 | st->start_address = addr; | |
aaa22952 OH |
174 | st->start_pa = pa; |
175 | st->last_pa = pa; | |
8eb07b18 RG |
176 | seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
177 | /* | |
178 | * Dump the section of virtual memory when: | |
179 | * - the PTE flags from one entry to the next differs. | |
180 | * - we change levels in the tree. | |
181 | * - the address is in a different section of memory and is thus | |
182 | * used for a different purpose, regardless of the flags. | |
aaa22952 | 183 | * - the pa of this page is not adjacent to the last inspected page |
8eb07b18 RG |
184 | */ |
185 | } else if (flag != st->current_flags || level != st->level || | |
aaa22952 OH |
186 | addr >= st->marker[1].start_address || |
187 | pa != st->last_pa + PAGE_SIZE) { | |
8eb07b18 RG |
188 | |
189 | /* Check the PTE flags */ | |
190 | if (st->current_flags) { | |
191 | dump_addr(st, addr); | |
192 | ||
193 | /* Dump all the flags */ | |
194 | if (pg_level[st->level].flag) | |
195 | dump_flag_info(st, pg_level[st->level].flag, | |
196 | st->current_flags, | |
197 | pg_level[st->level].num); | |
198 | ||
aae85e3c | 199 | seq_putc(st->seq, '\n'); |
8eb07b18 RG |
200 | } |
201 | ||
202 | /* | |
203 | * Address indicates we have passed the end of the | |
204 | * current section of virtual memory | |
205 | */ | |
206 | while (addr >= st->marker[1].start_address) { | |
207 | st->marker++; | |
208 | seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); | |
209 | } | |
210 | st->start_address = addr; | |
aaa22952 OH |
211 | st->start_pa = pa; |
212 | st->last_pa = pa; | |
8eb07b18 RG |
213 | st->current_flags = flag; |
214 | st->level = level; | |
aaa22952 OH |
215 | } else { |
216 | st->last_pa = pa; | |
8eb07b18 RG |
217 | } |
218 | } | |
219 | ||
220 | static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start) | |
221 | { | |
222 | pte_t *pte = pte_offset_kernel(pmd, 0); | |
223 | unsigned long addr; | |
224 | unsigned int i; | |
225 | ||
226 | for (i = 0; i < PTRS_PER_PTE; i++, pte++) { | |
227 | addr = start + i * PAGE_SIZE; | |
228 | note_page(st, addr, 4, pte_val(*pte)); | |
229 | ||
230 | } | |
231 | } | |
232 | ||
233 | static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start) | |
234 | { | |
235 | pmd_t *pmd = pmd_offset(pud, 0); | |
236 | unsigned long addr; | |
237 | unsigned int i; | |
238 | ||
239 | for (i = 0; i < PTRS_PER_PMD; i++, pmd++) { | |
240 | addr = start + i * PMD_SIZE; | |
bfb9956a | 241 | if (!pmd_none(*pmd) && !pmd_huge(*pmd)) |
8eb07b18 RG |
242 | /* pmd exists */ |
243 | walk_pte(st, pmd, addr); | |
244 | else | |
245 | note_page(st, addr, 3, pmd_val(*pmd)); | |
246 | } | |
247 | } | |
248 | ||
249 | static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start) | |
250 | { | |
251 | pud_t *pud = pud_offset(pgd, 0); | |
252 | unsigned long addr; | |
253 | unsigned int i; | |
254 | ||
255 | for (i = 0; i < PTRS_PER_PUD; i++, pud++) { | |
256 | addr = start + i * PUD_SIZE; | |
bfb9956a | 257 | if (!pud_none(*pud) && !pud_huge(*pud)) |
8eb07b18 RG |
258 | /* pud exists */ |
259 | walk_pmd(st, pud, addr); | |
260 | else | |
261 | note_page(st, addr, 2, pud_val(*pud)); | |
262 | } | |
263 | } | |
264 | ||
265 | static void walk_pagetables(struct pg_state *st) | |
266 | { | |
267 | pgd_t *pgd = pgd_offset_k(0UL); | |
268 | unsigned int i; | |
269 | unsigned long addr; | |
270 | ||
0d923962 ME |
271 | addr = st->start_address; |
272 | ||
8eb07b18 RG |
273 | /* |
274 | * Traverse the linux pagetable structure and dump pages that are in | |
275 | * the hash pagetable. | |
276 | */ | |
0d923962 | 277 | for (i = 0; i < PTRS_PER_PGD; i++, pgd++, addr += PGDIR_SIZE) { |
bfb9956a | 278 | if (!pgd_none(*pgd) && !pgd_huge(*pgd)) |
8eb07b18 RG |
279 | /* pgd exists */ |
280 | walk_pud(st, pgd, addr); | |
281 | else | |
282 | note_page(st, addr, 1, pgd_val(*pgd)); | |
283 | } | |
284 | } | |
285 | ||
286 | static void populate_markers(void) | |
287 | { | |
6c01bbd2 CL |
288 | int i = 0; |
289 | ||
290 | address_markers[i++].start_address = PAGE_OFFSET; | |
291 | address_markers[i++].start_address = VMALLOC_START; | |
292 | address_markers[i++].start_address = VMALLOC_END; | |
293 | #ifdef CONFIG_PPC64 | |
294 | address_markers[i++].start_address = ISA_IO_BASE; | |
295 | address_markers[i++].start_address = ISA_IO_END; | |
296 | address_markers[i++].start_address = PHB_IO_BASE; | |
297 | address_markers[i++].start_address = PHB_IO_END; | |
298 | address_markers[i++].start_address = IOREMAP_BASE; | |
299 | address_markers[i++].start_address = IOREMAP_END; | |
4e003747 | 300 | #ifdef CONFIG_PPC_BOOK3S_64 |
6c01bbd2 | 301 | address_markers[i++].start_address = H_VMEMMAP_BASE; |
8eb07b18 | 302 | #else |
6c01bbd2 CL |
303 | address_markers[i++].start_address = VMEMMAP_BASE; |
304 | #endif | |
305 | #else /* !CONFIG_PPC64 */ | |
306 | address_markers[i++].start_address = ioremap_bot; | |
307 | address_markers[i++].start_address = IOREMAP_TOP; | |
308 | #ifdef CONFIG_NOT_COHERENT_CACHE | |
309 | address_markers[i++].start_address = IOREMAP_TOP; | |
310 | address_markers[i++].start_address = IOREMAP_TOP + | |
311 | CONFIG_CONSISTENT_SIZE; | |
312 | #endif | |
313 | #ifdef CONFIG_HIGHMEM | |
314 | address_markers[i++].start_address = PKMAP_BASE; | |
315 | address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP); | |
8eb07b18 | 316 | #endif |
6c01bbd2 CL |
317 | address_markers[i++].start_address = FIXADDR_START; |
318 | address_markers[i++].start_address = FIXADDR_TOP; | |
319 | #endif /* CONFIG_PPC64 */ | |
8eb07b18 RG |
320 | } |
321 | ||
322 | static int ptdump_show(struct seq_file *m, void *v) | |
323 | { | |
324 | struct pg_state st = { | |
325 | .seq = m, | |
8eb07b18 RG |
326 | .marker = address_markers, |
327 | }; | |
0d923962 ME |
328 | |
329 | if (radix_enabled()) | |
330 | st.start_address = PAGE_OFFSET; | |
331 | else | |
332 | st.start_address = KERN_VIRT_START; | |
333 | ||
8eb07b18 RG |
334 | /* Traverse kernel page tables */ |
335 | walk_pagetables(&st); | |
336 | note_page(&st, 0, 0, 0); | |
337 | return 0; | |
338 | } | |
339 | ||
340 | ||
341 | static int ptdump_open(struct inode *inode, struct file *file) | |
342 | { | |
343 | return single_open(file, ptdump_show, NULL); | |
344 | } | |
345 | ||
346 | static const struct file_operations ptdump_fops = { | |
347 | .open = ptdump_open, | |
348 | .read = seq_read, | |
349 | .llseek = seq_lseek, | |
350 | .release = single_release, | |
351 | }; | |
352 | ||
353 | static void build_pgtable_complete_mask(void) | |
354 | { | |
355 | unsigned int i, j; | |
356 | ||
357 | for (i = 0; i < ARRAY_SIZE(pg_level); i++) | |
358 | if (pg_level[i].flag) | |
359 | for (j = 0; j < pg_level[i].num; j++) | |
360 | pg_level[i].mask |= pg_level[i].flag[j].mask; | |
361 | } | |
362 | ||
363 | static int ptdump_init(void) | |
364 | { | |
365 | struct dentry *debugfs_file; | |
366 | ||
367 | populate_markers(); | |
368 | build_pgtable_complete_mask(); | |
2505820f | 369 | debugfs_file = debugfs_create_file("kernel_page_tables", 0400, NULL, |
8eb07b18 RG |
370 | NULL, &ptdump_fops); |
371 | return debugfs_file ? 0 : -ENOMEM; | |
372 | } | |
373 | device_initcall(ptdump_init); |