1 #include <linux/seq_file.h>
2 #include <linux/debugfs.h>
3 #include <linux/module.h>
5 #include <asm/sections.h>
6 #include <asm/pgtable.h>
8 static unsigned long max_addr
;
11 unsigned long start_address
;
15 enum address_markers_idx
{
26 static struct addr_marker address_markers
[] = {
27 [IDENTITY_NR
] = {0, "Identity Mapping"},
28 [KERNEL_START_NR
] = {(unsigned long)&_stext
, "Kernel Image Start"},
29 [KERNEL_END_NR
] = {(unsigned long)&_end
, "Kernel Image End"},
30 [VMEMMAP_NR
] = {0, "vmemmap Area"},
31 [VMALLOC_NR
] = {0, "vmalloc Area"},
33 [MODULES_NR
] = {0, "Modules Area"},
40 unsigned int current_prot
;
41 unsigned long start_address
;
42 unsigned long current_address
;
43 const struct addr_marker
*marker
;
46 static void print_prot(struct seq_file
*m
, unsigned int pr
, int level
)
48 static const char * const level_name
[] =
49 { "ASCE", "PGD", "PUD", "PMD", "PTE" };
51 seq_printf(m
, "%s ", level_name
[level
]);
52 if (pr
& _PAGE_INVALID
)
55 seq_printf(m
, "%s\n", pr
& _PAGE_RO
? "RO" : "RW");
58 static void note_page(struct seq_file
*m
, struct pg_state
*st
,
59 unsigned int new_prot
, int level
)
61 static const char units
[] = "KMGTPE";
62 int width
= sizeof(unsigned long) * 2;
63 const char *unit
= units
;
64 unsigned int prot
, cur
;
68 * If we have a "break" in the series, we need to flush the state
69 * that we have now. "break" is either changing perms, levels or
70 * address space marker.
73 cur
= st
->current_prot
;
77 st
->current_prot
= new_prot
;
79 st
->marker
= address_markers
;
80 seq_printf(m
, "---[ %s ]---\n", st
->marker
->name
);
81 } else if (prot
!= cur
|| level
!= st
->level
||
82 st
->current_address
>= st
->marker
[1].start_address
) {
83 /* Print the actual finished series */
84 seq_printf(m
, "0x%0*lx-0x%0*lx",
85 width
, st
->start_address
,
86 width
, st
->current_address
);
87 delta
= (st
->current_address
- st
->start_address
) >> 10;
88 while (!(delta
& 0x3ff) && unit
[1]) {
92 seq_printf(m
, "%9lu%c ", delta
, *unit
);
93 print_prot(m
, st
->current_prot
, st
->level
);
94 if (st
->current_address
>= st
->marker
[1].start_address
) {
96 seq_printf(m
, "---[ %s ]---\n", st
->marker
->name
);
98 st
->start_address
= st
->current_address
;
99 st
->current_prot
= new_prot
;
105 * The actual page table walker functions. In order to keep the implementation
106 * of print_prot() short, we only check and pass _PAGE_INVALID and _PAGE_RO
107 * flags to note_page() if a region, segment or page table entry is invalid or
109 * After all it's just a hint that the current level being walked contains an
110 * invalid or read-only entry.
112 static void walk_pte_level(struct seq_file
*m
, struct pg_state
*st
,
113 pmd_t
*pmd
, unsigned long addr
)
119 for (i
= 0; i
< PTRS_PER_PTE
&& addr
< max_addr
; i
++) {
120 st
->current_address
= addr
;
121 pte
= pte_offset_kernel(pmd
, addr
);
122 prot
= pte_val(*pte
) & (_PAGE_RO
| _PAGE_INVALID
);
123 note_page(m
, st
, prot
, 4);
128 static void walk_pmd_level(struct seq_file
*m
, struct pg_state
*st
,
129 pud_t
*pud
, unsigned long addr
)
135 for (i
= 0; i
< PTRS_PER_PMD
&& addr
< max_addr
; i
++) {
136 st
->current_address
= addr
;
137 pmd
= pmd_offset(pud
, addr
);
138 if (!pmd_none(*pmd
)) {
139 if (pmd_large(*pmd
)) {
140 prot
= pmd_val(*pmd
) & _SEGMENT_ENTRY_RO
;
141 note_page(m
, st
, prot
, 3);
143 walk_pte_level(m
, st
, pmd
, addr
);
145 note_page(m
, st
, _PAGE_INVALID
, 3);
150 static void walk_pud_level(struct seq_file
*m
, struct pg_state
*st
,
151 pgd_t
*pgd
, unsigned long addr
)
157 for (i
= 0; i
< PTRS_PER_PUD
&& addr
< max_addr
; i
++) {
158 st
->current_address
= addr
;
159 pud
= pud_offset(pgd
, addr
);
161 if (pud_large(*pud
)) {
162 prot
= pud_val(*pud
) & _PAGE_RO
;
163 note_page(m
, st
, prot
, 2);
165 walk_pmd_level(m
, st
, pud
, addr
);
167 note_page(m
, st
, _PAGE_INVALID
, 2);
172 static void walk_pgd_level(struct seq_file
*m
)
174 unsigned long addr
= 0;
179 memset(&st
, 0, sizeof(st
));
180 for (i
= 0; i
< PTRS_PER_PGD
&& addr
< max_addr
; i
++) {
181 st
.current_address
= addr
;
182 pgd
= pgd_offset_k(addr
);
184 walk_pud_level(m
, &st
, pgd
, addr
);
186 note_page(m
, &st
, _PAGE_INVALID
, 1);
189 /* Flush out the last page */
190 st
.current_address
= max_addr
;
191 note_page(m
, &st
, 0, 0);
194 static int ptdump_show(struct seq_file
*m
, void *v
)
200 static int ptdump_open(struct inode
*inode
, struct file
*filp
)
202 return single_open(filp
, ptdump_show
, NULL
);
205 static const struct file_operations ptdump_fops
= {
209 .release
= single_release
,
212 static int pt_dump_init(void)
215 * Figure out the maximum virtual address being accessible with the
216 * kernel ASCE. We need this to keep the page table walker functions
217 * from accessing non-existent entries.
220 max_addr
= 1UL << 31;
222 max_addr
= (S390_lowcore
.kernel_asce
& _REGION_ENTRY_TYPE_MASK
) >> 2;
223 max_addr
= 1UL << (max_addr
* 11 + 31);
224 address_markers
[MODULES_NR
].start_address
= MODULES_VADDR
;
226 address_markers
[VMEMMAP_NR
].start_address
= (unsigned long) vmemmap
;
227 address_markers
[VMALLOC_NR
].start_address
= VMALLOC_START
;
228 debugfs_create_file("kernel_page_tables", 0400, NULL
, NULL
, &ptdump_fops
);
231 device_initcall(pt_dump_init
);