^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2019 SiFive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/ptdump.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/ptdump.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/pgtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/kasan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define pt_dump_seq_printf(m, fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) ({ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) if (m) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) seq_printf(m, fmt, ##args); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define pt_dump_seq_puts(m, fmt) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) ({ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (m) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) seq_printf(m, fmt); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * The page dumper groups page table entries of the same type into a single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * description. It uses pg_state to track the range information while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * iterating over the pte entries. When the continuity is broken it then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * dumps out a description of the range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct pg_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct ptdump_state ptdump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct seq_file *seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) const struct addr_marker *marker;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) unsigned long start_address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) unsigned long start_pa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unsigned long last_pa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) u64 current_prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) bool check_wx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned long wx_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* Address marker */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct addr_marker {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) unsigned long start_address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* Private information for debugfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct ptd_mm_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct mm_struct *mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) const struct addr_marker *markers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) unsigned long base_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) unsigned long end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static struct addr_marker address_markers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #ifdef CONFIG_KASAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {KASAN_SHADOW_START, "Kasan shadow start"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {KASAN_SHADOW_END, "Kasan shadow end"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {FIXADDR_START, "Fixmap start"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {FIXADDR_TOP, "Fixmap end"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {PCI_IO_START, "PCI I/O start"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {PCI_IO_END, "PCI I/O end"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #ifdef CONFIG_SPARSEMEM_VMEMMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {VMEMMAP_START, "vmemmap start"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {VMEMMAP_END, "vmemmap end"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {VMALLOC_START, "vmalloc() area"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {VMALLOC_END, "vmalloc() end"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {PAGE_OFFSET, "Linear mapping"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {-1, NULL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static struct ptd_mm_info kernel_ptd_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .mm = &init_mm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .markers = address_markers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .base_addr = KERN_VIRT_START,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .end = ULONG_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #ifdef CONFIG_EFI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static struct addr_marker efi_addr_markers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) { 0, "UEFI runtime start" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) { SZ_1G, "UEFI runtime end" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) { -1, NULL }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static struct ptd_mm_info efi_ptd_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) .mm = &efi_mm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) .markers = efi_addr_markers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) .base_addr = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) .end = SZ_2G,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* Page Table Entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct prot_bits {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) u64 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) const char *set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) const char *clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static const struct prot_bits pte_bits[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .mask = _PAGE_SOFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .val = _PAGE_SOFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .set = "RSW",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .clear = " ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) .mask = _PAGE_DIRTY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) .val = _PAGE_DIRTY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) .set = "D",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) .clear = ".",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) .mask = _PAGE_ACCESSED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) .val = _PAGE_ACCESSED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) .set = "A",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) .clear = ".",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) .mask = _PAGE_GLOBAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) .val = _PAGE_GLOBAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) .set = "G",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) .clear = ".",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) .mask = _PAGE_USER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .val = _PAGE_USER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) .set = "U",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) .clear = ".",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) .mask = _PAGE_EXEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) .val = _PAGE_EXEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) .set = "X",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) .clear = ".",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .mask = _PAGE_WRITE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .val = _PAGE_WRITE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .set = "W",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .clear = ".",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .mask = _PAGE_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) .val = _PAGE_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) .set = "R",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) .clear = ".",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) .mask = _PAGE_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) .val = _PAGE_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) .set = "V",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) .clear = ".",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /* Page Level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct pg_level {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) u64 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static struct pg_level pg_level[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) { /* pgd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) .name = "PGD",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }, { /* p4d */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) .name = (CONFIG_PGTABLE_LEVELS > 4) ? "P4D" : "PGD",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }, { /* pud */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) .name = (CONFIG_PGTABLE_LEVELS > 3) ? "PUD" : "PGD",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }, { /* pmd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) .name = (CONFIG_PGTABLE_LEVELS > 2) ? "PMD" : "PGD",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }, { /* pte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .name = "PTE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static void dump_prot(struct pg_state *st)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) for (i = 0; i < ARRAY_SIZE(pte_bits); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) const char *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if ((st->current_prot & pte_bits[i].mask) == pte_bits[i].val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) s = pte_bits[i].set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) s = pte_bits[i].clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) pt_dump_seq_printf(st->seq, " %s", s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) #ifdef CONFIG_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) #define ADDR_FORMAT "0x%016lx"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) #define ADDR_FORMAT "0x%08lx"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static void dump_addr(struct pg_state *st, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static const char units[] = "KMGTPE";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) const char *unit = units;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) unsigned long delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) pt_dump_seq_printf(st->seq, ADDR_FORMAT "-" ADDR_FORMAT " ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) st->start_address, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) pt_dump_seq_printf(st->seq, " " ADDR_FORMAT " ", st->start_pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) delta = (addr - st->start_address) >> 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) while (!(delta & 1023) && unit[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) delta >>= 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) unit++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) pt_dump_seq_printf(st->seq, "%9lu%c %s", delta, *unit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) pg_level[st->level].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static void note_prot_wx(struct pg_state *st, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (!st->check_wx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if ((st->current_prot & (_PAGE_WRITE | _PAGE_EXEC)) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) (_PAGE_WRITE | _PAGE_EXEC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) WARN_ONCE(1, "riscv/mm: Found insecure W+X mapping at address %p/%pS\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) (void *)st->start_address, (void *)st->start_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static void note_page(struct ptdump_state *pt_st, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) int level, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) u64 pa = PFN_PHYS(pte_pfn(__pte(val)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) u64 prot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (level >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) prot = val & pg_level[level].mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (st->level == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) st->level = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) st->current_prot = prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) st->start_address = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) st->start_pa = pa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) st->last_pa = pa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) } else if (prot != st->current_prot ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) level != st->level || addr >= st->marker[1].start_address) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (st->current_prot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) note_prot_wx(st, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) dump_addr(st, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) dump_prot(st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) pt_dump_seq_puts(st->seq, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) while (addr >= st->marker[1].start_address) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) st->marker++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) pt_dump_seq_printf(st->seq, "---[ %s ]---\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) st->marker->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) st->start_address = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) st->start_pa = pa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) st->last_pa = pa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) st->current_prot = prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) st->level = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) st->last_pa = pa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static void ptdump_walk(struct seq_file *s, struct ptd_mm_info *pinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) struct pg_state st = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) .seq = s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) .marker = pinfo->markers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) .level = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) .ptdump = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) .note_page = note_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) .range = (struct ptdump_range[]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {pinfo->base_addr, pinfo->end},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {0, 0}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) ptdump_walk_pgd(&st.ptdump, pinfo->mm, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) void ptdump_check_wx(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct pg_state st = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) .seq = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) .marker = (struct addr_marker[]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {0, NULL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {-1, NULL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) .level = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) .check_wx = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) .ptdump = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) .note_page = note_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) .range = (struct ptdump_range[]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {KERN_VIRT_START, ULONG_MAX},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {0, 0}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (st.wx_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) pr_warn("Checked W+X mappings: failed, %lu W+X pages found\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) st.wx_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) pr_info("Checked W+X mappings: passed, no W+X pages found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static int ptdump_show(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) ptdump_walk(m, m->private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) DEFINE_SHOW_ATTRIBUTE(ptdump);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static int ptdump_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) unsigned int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) for (i = 0; i < ARRAY_SIZE(pg_level); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) for (j = 0; j < ARRAY_SIZE(pte_bits); j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) pg_level[i].mask |= pte_bits[j].mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) debugfs_create_file("kernel_page_tables", 0400, NULL, &kernel_ptd_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) &ptdump_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) #ifdef CONFIG_EFI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (efi_enabled(EFI_RUNTIME_SERVICES))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) debugfs_create_file("efi_page_tables", 0400, NULL, &efi_ptd_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) &ptdump_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) device_initcall(ptdump_init);