1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2da1694adSHeiko Carstens #include <linux/set_memory.h> 39d719d39SHeiko Carstens #include <linux/ptdump.h> 4e76e82d7SHeiko Carstens #include <linux/seq_file.h> 5e76e82d7SHeiko Carstens #include <linux/debugfs.h> 6e76e82d7SHeiko Carstens #include <linux/mm.h> 7d6de72cfSSven Schnelle #include <linux/kfence.h> 8e006222bSVasily Gorbik #include <linux/kasan.h> 908c8e685SHeiko Carstens #include <asm/ptdump.h> 100dac8f6bSVasily Gorbik #include <asm/kasan.h> 1150787755SAlexander Gordeev #include <asm/abs_lowcore.h> 123b051e89SSven Schnelle #include <asm/nospec-branch.h> 13e76e82d7SHeiko Carstens #include <asm/sections.h> 14c0ceb944SAlexander Gordeev #include <asm/maccess.h> 15e76e82d7SHeiko Carstens 16e76e82d7SHeiko Carstens static unsigned long max_addr; 17e76e82d7SHeiko Carstens 18e76e82d7SHeiko Carstens struct addr_marker { 19e76e82d7SHeiko Carstens unsigned long start_address; 20e76e82d7SHeiko Carstens const char *name; 21e76e82d7SHeiko Carstens }; 22e76e82d7SHeiko Carstens 23e76e82d7SHeiko Carstens enum address_markers_idx { 24e670e64aSVasily Gorbik IDENTITY_BEFORE_NR = 0, 25e670e64aSVasily Gorbik IDENTITY_BEFORE_END_NR, 26edcfc9c7SHeiko Carstens AMODE31_START_NR, 27edcfc9c7SHeiko Carstens AMODE31_END_NR, 28e76e82d7SHeiko Carstens KERNEL_START_NR, 29e76e82d7SHeiko Carstens KERNEL_END_NR, 30d6de72cfSSven Schnelle #ifdef CONFIG_KFENCE 31d6de72cfSSven Schnelle KFENCE_START_NR, 32d6de72cfSSven Schnelle KFENCE_END_NR, 33d6de72cfSSven Schnelle #endif 34e670e64aSVasily Gorbik IDENTITY_AFTER_NR, 35e670e64aSVasily Gorbik IDENTITY_AFTER_END_NR, 36e76e82d7SHeiko Carstens VMEMMAP_NR, 37e670e64aSVasily Gorbik VMEMMAP_END_NR, 38e76e82d7SHeiko Carstens VMALLOC_NR, 39e670e64aSVasily Gorbik VMALLOC_END_NR, 40c972cc60SHeiko Carstens MODULES_NR, 41e670e64aSVasily Gorbik MODULES_END_NR, 4250787755SAlexander Gordeev ABS_LOWCORE_NR, 4350787755SAlexander Gordeev ABS_LOWCORE_END_NR, 44c0ceb944SAlexander Gordeev MEMCPY_REAL_NR, 45c0ceb944SAlexander Gordeev MEMCPY_REAL_END_NR, 4605178996SVasily Gorbik #ifdef CONFIG_KASAN 4705178996SVasily Gorbik KASAN_SHADOW_START_NR, 4805178996SVasily Gorbik KASAN_SHADOW_END_NR, 4905178996SVasily Gorbik #endif 50e76e82d7SHeiko Carstens }; 51e76e82d7SHeiko Carstens 52e76e82d7SHeiko Carstens static struct addr_marker address_markers[] = { 53e670e64aSVasily Gorbik [IDENTITY_BEFORE_NR] = {0, "Identity Mapping Start"}, 54e670e64aSVasily Gorbik [IDENTITY_BEFORE_END_NR] = {(unsigned long)_stext, "Identity Mapping End"}, 55edcfc9c7SHeiko Carstens [AMODE31_START_NR] = {0, "Amode31 Area Start"}, 56edcfc9c7SHeiko Carstens [AMODE31_END_NR] = {0, "Amode31 Area End"}, 57320d9555SVasily Gorbik [KERNEL_START_NR] = {(unsigned long)_stext, "Kernel Image Start"}, 58320d9555SVasily Gorbik [KERNEL_END_NR] = {(unsigned long)_end, "Kernel Image End"}, 59d6de72cfSSven Schnelle #ifdef CONFIG_KFENCE 60d6de72cfSSven Schnelle [KFENCE_START_NR] = {0, "KFence Pool Start"}, 61d6de72cfSSven Schnelle [KFENCE_END_NR] = {0, "KFence Pool End"}, 62d6de72cfSSven Schnelle #endif 63e670e64aSVasily Gorbik [IDENTITY_AFTER_NR] = {(unsigned long)_end, "Identity Mapping Start"}, 64e670e64aSVasily Gorbik [IDENTITY_AFTER_END_NR] = {0, "Identity Mapping End"}, 65e670e64aSVasily Gorbik [VMEMMAP_NR] = {0, "vmemmap Area Start"}, 66e670e64aSVasily Gorbik [VMEMMAP_END_NR] = {0, "vmemmap Area End"}, 67e670e64aSVasily Gorbik [VMALLOC_NR] = {0, "vmalloc Area Start"}, 68e670e64aSVasily Gorbik [VMALLOC_END_NR] = {0, "vmalloc Area End"}, 69e670e64aSVasily Gorbik [MODULES_NR] = {0, "Modules Area Start"}, 70e670e64aSVasily Gorbik [MODULES_END_NR] = {0, "Modules Area End"}, 7150787755SAlexander Gordeev [ABS_LOWCORE_NR] = {0, "Lowcore Area Start"}, 7250787755SAlexander Gordeev [ABS_LOWCORE_END_NR] = {0, "Lowcore Area End"}, 73c0ceb944SAlexander Gordeev [MEMCPY_REAL_NR] = {0, "Real Memory Copy Area Start"}, 74c0ceb944SAlexander Gordeev [MEMCPY_REAL_END_NR] = {0, "Real Memory Copy Area End"}, 7505178996SVasily Gorbik #ifdef CONFIG_KASAN 7605178996SVasily Gorbik [KASAN_SHADOW_START_NR] = {KASAN_SHADOW_START, "Kasan Shadow Start"}, 7705178996SVasily Gorbik [KASAN_SHADOW_END_NR] = {KASAN_SHADOW_END, "Kasan Shadow End"}, 7805178996SVasily Gorbik #endif 79e76e82d7SHeiko Carstens { -1, NULL } 80e76e82d7SHeiko Carstens }; 81e76e82d7SHeiko Carstens 82e76e82d7SHeiko Carstens struct pg_state { 839d719d39SHeiko Carstens struct ptdump_state ptdump; 849d719d39SHeiko Carstens struct seq_file *seq; 85e76e82d7SHeiko Carstens int level; 86e76e82d7SHeiko Carstens unsigned int current_prot; 8708c8e685SHeiko Carstens bool check_wx; 8808c8e685SHeiko Carstens unsigned long wx_pages; 89e76e82d7SHeiko Carstens unsigned long start_address; 90e76e82d7SHeiko Carstens const struct addr_marker *marker; 91e76e82d7SHeiko Carstens }; 92e76e82d7SHeiko Carstens 936bf9a639SHeiko Carstens #define pt_dump_seq_printf(m, fmt, args...) \ 946bf9a639SHeiko Carstens ({ \ 956bf9a639SHeiko Carstens struct seq_file *__m = (m); \ 966bf9a639SHeiko Carstens \ 976bf9a639SHeiko Carstens if (__m) \ 986bf9a639SHeiko Carstens seq_printf(__m, fmt, ##args); \ 996bf9a639SHeiko Carstens }) 1006bf9a639SHeiko Carstens 1016bf9a639SHeiko Carstens #define pt_dump_seq_puts(m, fmt) \ 1026bf9a639SHeiko Carstens ({ \ 1036bf9a639SHeiko Carstens struct seq_file *__m = (m); \ 1046bf9a639SHeiko Carstens \ 1056bf9a639SHeiko Carstens if (__m) \ 1066bf9a639SHeiko Carstens seq_printf(__m, fmt); \ 1076bf9a639SHeiko Carstens }) 1086bf9a639SHeiko Carstens 109e76e82d7SHeiko Carstens static void print_prot(struct seq_file *m, unsigned int pr, int level) 110e76e82d7SHeiko Carstens { 111e76e82d7SHeiko Carstens static const char * const level_name[] = 112e76e82d7SHeiko Carstens { "ASCE", "PGD", "PUD", "PMD", "PTE" }; 113e76e82d7SHeiko Carstens 1146bf9a639SHeiko Carstens pt_dump_seq_printf(m, "%s ", level_name[level]); 1151819ed1fSHeiko Carstens if (pr & _PAGE_INVALID) { 1166bf9a639SHeiko Carstens pt_dump_seq_printf(m, "I\n"); 1171819ed1fSHeiko Carstens return; 1181819ed1fSHeiko Carstens } 1196bf9a639SHeiko Carstens pt_dump_seq_puts(m, (pr & _PAGE_PROTECT) ? "RO " : "RW "); 1206bf9a639SHeiko Carstens pt_dump_seq_puts(m, (pr & _PAGE_NOEXEC) ? "NX\n" : "X\n"); 121e76e82d7SHeiko Carstens } 122e76e82d7SHeiko Carstens 12308c8e685SHeiko Carstens static void note_prot_wx(struct pg_state *st, unsigned long addr) 12408c8e685SHeiko Carstens { 12508c8e685SHeiko Carstens #ifdef CONFIG_DEBUG_WX 12608c8e685SHeiko Carstens if (!st->check_wx) 12708c8e685SHeiko Carstens return; 12808c8e685SHeiko Carstens if (st->current_prot & _PAGE_INVALID) 12908c8e685SHeiko Carstens return; 13008c8e685SHeiko Carstens if (st->current_prot & _PAGE_PROTECT) 13108c8e685SHeiko Carstens return; 13208c8e685SHeiko Carstens if (st->current_prot & _PAGE_NOEXEC) 13308c8e685SHeiko Carstens return; 1343b051e89SSven Schnelle /* 1353b051e89SSven Schnelle * The first lowcore page is W+X if spectre mitigations are using 1363b051e89SSven Schnelle * trampolines or the BEAR enhancements facility is not installed, 1373b051e89SSven Schnelle * in which case we have two lpswe instructions in lowcore that need 1383b051e89SSven Schnelle * to be executable. 1393b051e89SSven Schnelle */ 1403b051e89SSven Schnelle if (addr == PAGE_SIZE && (nospec_uses_trampoline() || !static_key_enabled(&cpu_has_bear))) 14108c8e685SHeiko Carstens return; 14208c8e685SHeiko Carstens WARN_ONCE(1, "s390/mm: Found insecure W+X mapping at address %pS\n", 14308c8e685SHeiko Carstens (void *)st->start_address); 14408c8e685SHeiko Carstens st->wx_pages += (addr - st->start_address) / PAGE_SIZE; 14508c8e685SHeiko Carstens #endif /* CONFIG_DEBUG_WX */ 14608c8e685SHeiko Carstens } 14708c8e685SHeiko Carstens 1489d719d39SHeiko Carstens static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, u64 val) 149e76e82d7SHeiko Carstens { 150e76e82d7SHeiko Carstens int width = sizeof(unsigned long) * 2; 1519d719d39SHeiko Carstens static const char units[] = "KMGTPE"; 152e76e82d7SHeiko Carstens const char *unit = units; 153e76e82d7SHeiko Carstens unsigned long delta; 1549d719d39SHeiko Carstens struct pg_state *st; 1559d719d39SHeiko Carstens struct seq_file *m; 1569d719d39SHeiko Carstens unsigned int prot; 157e76e82d7SHeiko Carstens 1589d719d39SHeiko Carstens st = container_of(pt_st, struct pg_state, ptdump); 1599d719d39SHeiko Carstens m = st->seq; 1609d719d39SHeiko Carstens prot = val & (_PAGE_PROTECT | _PAGE_NOEXEC); 1619d719d39SHeiko Carstens if (level == 4 && (val & _PAGE_INVALID)) 1629d719d39SHeiko Carstens prot = _PAGE_INVALID; 1639d719d39SHeiko Carstens /* For pmd_none() & friends val gets passed as zero. */ 1649d719d39SHeiko Carstens if (level != 4 && !val) 1659d719d39SHeiko Carstens prot = _PAGE_INVALID; 1669d719d39SHeiko Carstens /* Final flush from generic code. */ 1679d719d39SHeiko Carstens if (level == -1) 1689d719d39SHeiko Carstens addr = max_addr; 1699d719d39SHeiko Carstens if (st->level == -1) { 1706bf9a639SHeiko Carstens pt_dump_seq_printf(m, "---[ %s ]---\n", st->marker->name); 1719d719d39SHeiko Carstens st->start_address = addr; 1729d719d39SHeiko Carstens st->current_prot = prot; 1739d719d39SHeiko Carstens st->level = level; 1749d719d39SHeiko Carstens } else if (prot != st->current_prot || level != st->level || 1759d719d39SHeiko Carstens addr >= st->marker[1].start_address) { 17608c8e685SHeiko Carstens note_prot_wx(st, addr); 1776bf9a639SHeiko Carstens pt_dump_seq_printf(m, "0x%0*lx-0x%0*lx ", 178e76e82d7SHeiko Carstens width, st->start_address, 1799d719d39SHeiko Carstens width, addr); 1809d719d39SHeiko Carstens delta = (addr - st->start_address) >> 10; 181e76e82d7SHeiko Carstens while (!(delta & 0x3ff) && unit[1]) { 182e76e82d7SHeiko Carstens delta >>= 10; 183e76e82d7SHeiko Carstens unit++; 184e76e82d7SHeiko Carstens } 1856bf9a639SHeiko Carstens pt_dump_seq_printf(m, "%9lu%c ", delta, *unit); 186e76e82d7SHeiko Carstens print_prot(m, st->current_prot, st->level); 1879d719d39SHeiko Carstens while (addr >= st->marker[1].start_address) { 188e76e82d7SHeiko Carstens st->marker++; 1896bf9a639SHeiko Carstens pt_dump_seq_printf(m, "---[ %s ]---\n", st->marker->name); 190e76e82d7SHeiko Carstens } 1919d719d39SHeiko Carstens st->start_address = addr; 1929d719d39SHeiko Carstens st->current_prot = prot; 193e76e82d7SHeiko Carstens st->level = level; 194e76e82d7SHeiko Carstens } 195e76e82d7SHeiko Carstens } 196e76e82d7SHeiko Carstens 19708c8e685SHeiko Carstens #ifdef CONFIG_DEBUG_WX 19808c8e685SHeiko Carstens void ptdump_check_wx(void) 19908c8e685SHeiko Carstens { 20008c8e685SHeiko Carstens struct pg_state st = { 20108c8e685SHeiko Carstens .ptdump = { 20208c8e685SHeiko Carstens .note_page = note_page, 20308c8e685SHeiko Carstens .range = (struct ptdump_range[]) { 20408c8e685SHeiko Carstens {.start = 0, .end = max_addr}, 20508c8e685SHeiko Carstens {.start = 0, .end = 0}, 20608c8e685SHeiko Carstens } 20708c8e685SHeiko Carstens }, 20808c8e685SHeiko Carstens .seq = NULL, 20908c8e685SHeiko Carstens .level = -1, 21008c8e685SHeiko Carstens .current_prot = 0, 21108c8e685SHeiko Carstens .check_wx = true, 21208c8e685SHeiko Carstens .wx_pages = 0, 21308c8e685SHeiko Carstens .start_address = 0, 21408c8e685SHeiko Carstens .marker = (struct addr_marker[]) { 21508c8e685SHeiko Carstens { .start_address = 0, .name = NULL}, 21608c8e685SHeiko Carstens { .start_address = -1, .name = NULL}, 21708c8e685SHeiko Carstens }, 21808c8e685SHeiko Carstens }; 21908c8e685SHeiko Carstens 22008c8e685SHeiko Carstens if (!MACHINE_HAS_NX) 22108c8e685SHeiko Carstens return; 22208c8e685SHeiko Carstens ptdump_walk_pgd(&st.ptdump, &init_mm, NULL); 22308c8e685SHeiko Carstens if (st.wx_pages) 22408c8e685SHeiko Carstens pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found\n", st.wx_pages); 22508c8e685SHeiko Carstens else 2263b051e89SSven Schnelle pr_info("Checked W+X mappings: passed, no %sW+X pages found\n", 2273b051e89SSven Schnelle (nospec_uses_trampoline() || !static_key_enabled(&cpu_has_bear)) ? 2283b051e89SSven Schnelle "unexpected " : ""); 22908c8e685SHeiko Carstens } 23008c8e685SHeiko Carstens #endif /* CONFIG_DEBUG_WX */ 23108c8e685SHeiko Carstens 23208c8e685SHeiko Carstens #ifdef CONFIG_PTDUMP_DEBUGFS 233e76e82d7SHeiko Carstens static int ptdump_show(struct seq_file *m, void *v) 234e76e82d7SHeiko Carstens { 2359d719d39SHeiko Carstens struct pg_state st = { 2369d719d39SHeiko Carstens .ptdump = { 2379d719d39SHeiko Carstens .note_page = note_page, 2389d719d39SHeiko Carstens .range = (struct ptdump_range[]) { 2399d719d39SHeiko Carstens {.start = 0, .end = max_addr}, 2409d719d39SHeiko Carstens {.start = 0, .end = 0}, 2419d719d39SHeiko Carstens } 2429d719d39SHeiko Carstens }, 2439d719d39SHeiko Carstens .seq = m, 2449d719d39SHeiko Carstens .level = -1, 2459d719d39SHeiko Carstens .current_prot = 0, 24608c8e685SHeiko Carstens .check_wx = false, 24708c8e685SHeiko Carstens .wx_pages = 0, 2489d719d39SHeiko Carstens .start_address = 0, 2499d719d39SHeiko Carstens .marker = address_markers, 2509d719d39SHeiko Carstens }; 2519d719d39SHeiko Carstens 25236c2733cSHeiko Carstens get_online_mems(); 253da1694adSHeiko Carstens mutex_lock(&cpa_mutex); 2549d719d39SHeiko Carstens ptdump_walk_pgd(&st.ptdump, &init_mm, NULL); 255da1694adSHeiko Carstens mutex_unlock(&cpa_mutex); 25636c2733cSHeiko Carstens put_online_mems(); 257e76e82d7SHeiko Carstens return 0; 258e76e82d7SHeiko Carstens } 2599d719d39SHeiko Carstens DEFINE_SHOW_ATTRIBUTE(ptdump); 26008c8e685SHeiko Carstens #endif /* CONFIG_PTDUMP_DEBUGFS */ 261e76e82d7SHeiko Carstens 262ee4b2ce6SVasily Gorbik /* 263ee4b2ce6SVasily Gorbik * Heapsort from lib/sort.c is not a stable sorting algorithm, do a simple 264ee4b2ce6SVasily Gorbik * insertion sort to preserve the original order of markers with the same 265ee4b2ce6SVasily Gorbik * start address. 266ee4b2ce6SVasily Gorbik */ 267ee4b2ce6SVasily Gorbik static void sort_address_markers(void) 268ee4b2ce6SVasily Gorbik { 269ee4b2ce6SVasily Gorbik struct addr_marker tmp; 270ee4b2ce6SVasily Gorbik int i, j; 271ee4b2ce6SVasily Gorbik 272ee4b2ce6SVasily Gorbik for (i = 1; i < ARRAY_SIZE(address_markers) - 1; i++) { 273ee4b2ce6SVasily Gorbik tmp = address_markers[i]; 274ee4b2ce6SVasily Gorbik for (j = i - 1; j >= 0 && address_markers[j].start_address > tmp.start_address; j--) 275ee4b2ce6SVasily Gorbik address_markers[j + 1] = address_markers[j]; 276ee4b2ce6SVasily Gorbik address_markers[j + 1] = tmp; 277ee4b2ce6SVasily Gorbik } 278ee4b2ce6SVasily Gorbik } 279ee4b2ce6SVasily Gorbik 280e76e82d7SHeiko Carstens static int pt_dump_init(void) 281e76e82d7SHeiko Carstens { 282d6de72cfSSven Schnelle #ifdef CONFIG_KFENCE 283d6de72cfSSven Schnelle unsigned long kfence_start = (unsigned long)__kfence_pool; 284d6de72cfSSven Schnelle #endif 285e76e82d7SHeiko Carstens /* 286e76e82d7SHeiko Carstens * Figure out the maximum virtual address being accessible with the 287e76e82d7SHeiko Carstens * kernel ASCE. We need this to keep the page table walker functions 288e76e82d7SHeiko Carstens * from accessing non-existent entries. 289e76e82d7SHeiko Carstens */ 290e76e82d7SHeiko Carstens max_addr = (S390_lowcore.kernel_asce & _REGION_ENTRY_TYPE_MASK) >> 2; 291e76e82d7SHeiko Carstens max_addr = 1UL << (max_addr * 11 + 31); 29273045a08SVasily Gorbik address_markers[IDENTITY_AFTER_END_NR].start_address = ident_map_size; 293*3eeb0778SHeiko Carstens address_markers[AMODE31_START_NR].start_address = (unsigned long)__samode31; 294*3eeb0778SHeiko Carstens address_markers[AMODE31_END_NR].start_address = (unsigned long)__eamode31; 295c972cc60SHeiko Carstens address_markers[MODULES_NR].start_address = MODULES_VADDR; 296e670e64aSVasily Gorbik address_markers[MODULES_END_NR].start_address = MODULES_END; 29750787755SAlexander Gordeev address_markers[ABS_LOWCORE_NR].start_address = __abs_lowcore; 29850787755SAlexander Gordeev address_markers[ABS_LOWCORE_END_NR].start_address = __abs_lowcore + ABS_LOWCORE_MAP_SIZE; 299c0ceb944SAlexander Gordeev address_markers[MEMCPY_REAL_NR].start_address = __memcpy_real_area; 300a984f27eSAlexander Gordeev address_markers[MEMCPY_REAL_END_NR].start_address = __memcpy_real_area + MEMCPY_REAL_SIZE; 301e76e82d7SHeiko Carstens address_markers[VMEMMAP_NR].start_address = (unsigned long) vmemmap; 302e670e64aSVasily Gorbik address_markers[VMEMMAP_END_NR].start_address = (unsigned long)vmemmap + vmemmap_size; 303e76e82d7SHeiko Carstens address_markers[VMALLOC_NR].start_address = VMALLOC_START; 304e670e64aSVasily Gorbik address_markers[VMALLOC_END_NR].start_address = VMALLOC_END; 305d6de72cfSSven Schnelle #ifdef CONFIG_KFENCE 306d6de72cfSSven Schnelle address_markers[KFENCE_START_NR].start_address = kfence_start; 307d6de72cfSSven Schnelle address_markers[KFENCE_END_NR].start_address = kfence_start + KFENCE_POOL_SIZE; 308d6de72cfSSven Schnelle #endif 309ee4b2ce6SVasily Gorbik sort_address_markers(); 31048111b48SHeiko Carstens #ifdef CONFIG_PTDUMP_DEBUGFS 311e76e82d7SHeiko Carstens debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops); 31248111b48SHeiko Carstens #endif /* CONFIG_PTDUMP_DEBUGFS */ 313e76e82d7SHeiko Carstens return 0; 314e76e82d7SHeiko Carstens } 315e76e82d7SHeiko Carstens device_initcall(pt_dump_init); 316