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