1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2e66c3209SChristophe Leroy /* 3e66c3209SChristophe Leroy * Copyright 2016, Rashmica Gupta, IBM Corp. 4e66c3209SChristophe Leroy * 5e66c3209SChristophe Leroy * This traverses the kernel virtual memory and dumps the pages that are in 6e66c3209SChristophe Leroy * the hash pagetable, along with their flags to 7e66c3209SChristophe Leroy * /sys/kernel/debug/kernel_hash_pagetable. 8e66c3209SChristophe Leroy * 9e66c3209SChristophe Leroy * If radix is enabled then there is no hash page table and so no debugfs file 10e66c3209SChristophe Leroy * is generated. 11e66c3209SChristophe Leroy */ 12e66c3209SChristophe Leroy #include <linux/debugfs.h> 13e66c3209SChristophe Leroy #include <linux/fs.h> 14e66c3209SChristophe Leroy #include <linux/io.h> 15e66c3209SChristophe Leroy #include <linux/mm.h> 16e66c3209SChristophe Leroy #include <linux/sched.h> 17e66c3209SChristophe Leroy #include <linux/seq_file.h> 18e66c3209SChristophe Leroy #include <asm/pgtable.h> 19e66c3209SChristophe Leroy #include <linux/const.h> 20e66c3209SChristophe Leroy #include <asm/page.h> 21e66c3209SChristophe Leroy #include <asm/pgalloc.h> 22e66c3209SChristophe Leroy #include <asm/plpar_wrappers.h> 23e66c3209SChristophe Leroy #include <linux/memblock.h> 24e66c3209SChristophe Leroy #include <asm/firmware.h> 25e66c3209SChristophe Leroy 26e66c3209SChristophe Leroy struct pg_state { 27e66c3209SChristophe Leroy struct seq_file *seq; 28e66c3209SChristophe Leroy const struct addr_marker *marker; 29e66c3209SChristophe Leroy unsigned long start_address; 30e66c3209SChristophe Leroy unsigned int level; 31e66c3209SChristophe Leroy u64 current_flags; 32e66c3209SChristophe Leroy }; 33e66c3209SChristophe Leroy 34e66c3209SChristophe Leroy struct addr_marker { 35e66c3209SChristophe Leroy unsigned long start_address; 36e66c3209SChristophe Leroy const char *name; 37e66c3209SChristophe Leroy }; 38e66c3209SChristophe Leroy 39e66c3209SChristophe Leroy static struct addr_marker address_markers[] = { 40e66c3209SChristophe Leroy { 0, "Start of kernel VM" }, 41e66c3209SChristophe Leroy { 0, "vmalloc() Area" }, 42e66c3209SChristophe Leroy { 0, "vmalloc() End" }, 43e66c3209SChristophe Leroy { 0, "isa I/O start" }, 44e66c3209SChristophe Leroy { 0, "isa I/O end" }, 45e66c3209SChristophe Leroy { 0, "phb I/O start" }, 46e66c3209SChristophe Leroy { 0, "phb I/O end" }, 47e66c3209SChristophe Leroy { 0, "I/O remap start" }, 48e66c3209SChristophe Leroy { 0, "I/O remap end" }, 49e66c3209SChristophe Leroy { 0, "vmemmap start" }, 50e66c3209SChristophe Leroy { -1, NULL }, 51e66c3209SChristophe Leroy }; 52e66c3209SChristophe Leroy 53e66c3209SChristophe Leroy struct flag_info { 54e66c3209SChristophe Leroy u64 mask; 55e66c3209SChristophe Leroy u64 val; 56e66c3209SChristophe Leroy const char *set; 57e66c3209SChristophe Leroy const char *clear; 58e66c3209SChristophe Leroy bool is_val; 59e66c3209SChristophe Leroy int shift; 60e66c3209SChristophe Leroy }; 61e66c3209SChristophe Leroy 62e66c3209SChristophe Leroy static const struct flag_info v_flag_array[] = { 63e66c3209SChristophe Leroy { 64e66c3209SChristophe Leroy .mask = SLB_VSID_B, 65e66c3209SChristophe Leroy .val = SLB_VSID_B_256M, 66e66c3209SChristophe Leroy .set = "ssize: 256M", 67e66c3209SChristophe Leroy .clear = "ssize: 1T ", 68e66c3209SChristophe Leroy }, { 69e66c3209SChristophe Leroy .mask = HPTE_V_SECONDARY, 70e66c3209SChristophe Leroy .val = HPTE_V_SECONDARY, 71e66c3209SChristophe Leroy .set = "secondary", 72e66c3209SChristophe Leroy .clear = "primary ", 73e66c3209SChristophe Leroy }, { 74e66c3209SChristophe Leroy .mask = HPTE_V_VALID, 75e66c3209SChristophe Leroy .val = HPTE_V_VALID, 76e66c3209SChristophe Leroy .set = "valid ", 77e66c3209SChristophe Leroy .clear = "invalid", 78e66c3209SChristophe Leroy }, { 79e66c3209SChristophe Leroy .mask = HPTE_V_BOLTED, 80e66c3209SChristophe Leroy .val = HPTE_V_BOLTED, 81e66c3209SChristophe Leroy .set = "bolted", 82e66c3209SChristophe Leroy .clear = "", 83e66c3209SChristophe Leroy } 84e66c3209SChristophe Leroy }; 85e66c3209SChristophe Leroy 86e66c3209SChristophe Leroy static const struct flag_info r_flag_array[] = { 87e66c3209SChristophe Leroy { 88e66c3209SChristophe Leroy .mask = HPTE_R_PP0 | HPTE_R_PP, 89e66c3209SChristophe Leroy .val = PP_RWXX, 90e66c3209SChristophe Leroy .set = "prot:RW--", 91e66c3209SChristophe Leroy }, { 92e66c3209SChristophe Leroy .mask = HPTE_R_PP0 | HPTE_R_PP, 93e66c3209SChristophe Leroy .val = PP_RWRX, 94e66c3209SChristophe Leroy .set = "prot:RWR-", 95e66c3209SChristophe Leroy }, { 96e66c3209SChristophe Leroy .mask = HPTE_R_PP0 | HPTE_R_PP, 97e66c3209SChristophe Leroy .val = PP_RWRW, 98e66c3209SChristophe Leroy .set = "prot:RWRW", 99e66c3209SChristophe Leroy }, { 100e66c3209SChristophe Leroy .mask = HPTE_R_PP0 | HPTE_R_PP, 101e66c3209SChristophe Leroy .val = PP_RXRX, 102e66c3209SChristophe Leroy .set = "prot:R-R-", 103e66c3209SChristophe Leroy }, { 104e66c3209SChristophe Leroy .mask = HPTE_R_PP0 | HPTE_R_PP, 105e66c3209SChristophe Leroy .val = PP_RXXX, 106e66c3209SChristophe Leroy .set = "prot:R---", 107e66c3209SChristophe Leroy }, { 108e66c3209SChristophe Leroy .mask = HPTE_R_KEY_HI | HPTE_R_KEY_LO, 109e66c3209SChristophe Leroy .val = HPTE_R_KEY_HI | HPTE_R_KEY_LO, 110e66c3209SChristophe Leroy .set = "key", 111e66c3209SChristophe Leroy .clear = "", 112e66c3209SChristophe Leroy .is_val = true, 113e66c3209SChristophe Leroy }, { 114e66c3209SChristophe Leroy .mask = HPTE_R_R, 115e66c3209SChristophe Leroy .val = HPTE_R_R, 116e66c3209SChristophe Leroy .set = "ref", 117e66c3209SChristophe Leroy .clear = " ", 118e66c3209SChristophe Leroy }, { 119e66c3209SChristophe Leroy .mask = HPTE_R_C, 120e66c3209SChristophe Leroy .val = HPTE_R_C, 121e66c3209SChristophe Leroy .set = "changed", 122e66c3209SChristophe Leroy .clear = " ", 123e66c3209SChristophe Leroy }, { 124e66c3209SChristophe Leroy .mask = HPTE_R_N, 125e66c3209SChristophe Leroy .val = HPTE_R_N, 126e66c3209SChristophe Leroy .set = "no execute", 127e66c3209SChristophe Leroy }, { 128e66c3209SChristophe Leroy .mask = HPTE_R_WIMG, 129e66c3209SChristophe Leroy .val = HPTE_R_W, 130e66c3209SChristophe Leroy .set = "writethru", 131e66c3209SChristophe Leroy }, { 132e66c3209SChristophe Leroy .mask = HPTE_R_WIMG, 133e66c3209SChristophe Leroy .val = HPTE_R_I, 134e66c3209SChristophe Leroy .set = "no cache", 135e66c3209SChristophe Leroy }, { 136e66c3209SChristophe Leroy .mask = HPTE_R_WIMG, 137e66c3209SChristophe Leroy .val = HPTE_R_G, 138e66c3209SChristophe Leroy .set = "guarded", 139e66c3209SChristophe Leroy } 140e66c3209SChristophe Leroy }; 141e66c3209SChristophe Leroy 142e66c3209SChristophe Leroy static int calculate_pagesize(struct pg_state *st, int ps, char s[]) 143e66c3209SChristophe Leroy { 144e66c3209SChristophe Leroy static const char units[] = "BKMGTPE"; 145e66c3209SChristophe Leroy const char *unit = units; 146e66c3209SChristophe Leroy 147e66c3209SChristophe Leroy while (ps > 9 && unit[1]) { 148e66c3209SChristophe Leroy ps -= 10; 149e66c3209SChristophe Leroy unit++; 150e66c3209SChristophe Leroy } 151e66c3209SChristophe Leroy seq_printf(st->seq, " %s_ps: %i%c\t", s, 1<<ps, *unit); 152e66c3209SChristophe Leroy return ps; 153e66c3209SChristophe Leroy } 154e66c3209SChristophe Leroy 155e66c3209SChristophe Leroy static void dump_flag_info(struct pg_state *st, const struct flag_info 156e66c3209SChristophe Leroy *flag, u64 pte, int num) 157e66c3209SChristophe Leroy { 158e66c3209SChristophe Leroy unsigned int i; 159e66c3209SChristophe Leroy 160e66c3209SChristophe Leroy for (i = 0; i < num; i++, flag++) { 161e66c3209SChristophe Leroy const char *s = NULL; 162e66c3209SChristophe Leroy u64 val; 163e66c3209SChristophe Leroy 164e66c3209SChristophe Leroy /* flag not defined so don't check it */ 165e66c3209SChristophe Leroy if (flag->mask == 0) 166e66c3209SChristophe Leroy continue; 167e66c3209SChristophe Leroy /* Some 'flags' are actually values */ 168e66c3209SChristophe Leroy if (flag->is_val) { 169e66c3209SChristophe Leroy val = pte & flag->val; 170e66c3209SChristophe Leroy if (flag->shift) 171e66c3209SChristophe Leroy val = val >> flag->shift; 172e66c3209SChristophe Leroy seq_printf(st->seq, " %s:%llx", flag->set, val); 173e66c3209SChristophe Leroy } else { 174e66c3209SChristophe Leroy if ((pte & flag->mask) == flag->val) 175e66c3209SChristophe Leroy s = flag->set; 176e66c3209SChristophe Leroy else 177e66c3209SChristophe Leroy s = flag->clear; 178e66c3209SChristophe Leroy if (s) 179e66c3209SChristophe Leroy seq_printf(st->seq, " %s", s); 180e66c3209SChristophe Leroy } 181e66c3209SChristophe Leroy } 182e66c3209SChristophe Leroy } 183e66c3209SChristophe Leroy 184e66c3209SChristophe Leroy static void dump_hpte_info(struct pg_state *st, unsigned long ea, u64 v, u64 r, 185e66c3209SChristophe Leroy unsigned long rpn, int bps, int aps, unsigned long lp) 186e66c3209SChristophe Leroy { 187e66c3209SChristophe Leroy int aps_index; 188e66c3209SChristophe Leroy 189e66c3209SChristophe Leroy while (ea >= st->marker[1].start_address) { 190e66c3209SChristophe Leroy st->marker++; 191e66c3209SChristophe Leroy seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); 192e66c3209SChristophe Leroy } 193e66c3209SChristophe Leroy seq_printf(st->seq, "0x%lx:\t", ea); 194e66c3209SChristophe Leroy seq_printf(st->seq, "AVPN:%llx\t", HPTE_V_AVPN_VAL(v)); 195e66c3209SChristophe Leroy dump_flag_info(st, v_flag_array, v, ARRAY_SIZE(v_flag_array)); 196e66c3209SChristophe Leroy seq_printf(st->seq, " rpn: %lx\t", rpn); 197e66c3209SChristophe Leroy dump_flag_info(st, r_flag_array, r, ARRAY_SIZE(r_flag_array)); 198e66c3209SChristophe Leroy 199e66c3209SChristophe Leroy calculate_pagesize(st, bps, "base"); 200e66c3209SChristophe Leroy aps_index = calculate_pagesize(st, aps, "actual"); 201e66c3209SChristophe Leroy if (aps_index != 2) 202e66c3209SChristophe Leroy seq_printf(st->seq, "LP enc: %lx", lp); 203e66c3209SChristophe Leroy seq_putc(st->seq, '\n'); 204e66c3209SChristophe Leroy } 205e66c3209SChristophe Leroy 206e66c3209SChristophe Leroy 207e66c3209SChristophe Leroy static int native_find(unsigned long ea, int psize, bool primary, u64 *v, u64 208e66c3209SChristophe Leroy *r) 209e66c3209SChristophe Leroy { 210e66c3209SChristophe Leroy struct hash_pte *hptep; 211e66c3209SChristophe Leroy unsigned long hash, vsid, vpn, hpte_group, want_v, hpte_v; 212e66c3209SChristophe Leroy int i, ssize = mmu_kernel_ssize; 213e66c3209SChristophe Leroy unsigned long shift = mmu_psize_defs[psize].shift; 214e66c3209SChristophe Leroy 215e66c3209SChristophe Leroy /* calculate hash */ 216e66c3209SChristophe Leroy vsid = get_kernel_vsid(ea, ssize); 217e66c3209SChristophe Leroy vpn = hpt_vpn(ea, vsid, ssize); 218e66c3209SChristophe Leroy hash = hpt_hash(vpn, shift, ssize); 219e66c3209SChristophe Leroy want_v = hpte_encode_avpn(vpn, psize, ssize); 220e66c3209SChristophe Leroy 221e66c3209SChristophe Leroy /* to check in the secondary hash table, we invert the hash */ 222e66c3209SChristophe Leroy if (!primary) 223e66c3209SChristophe Leroy hash = ~hash; 224e66c3209SChristophe Leroy hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP; 225e66c3209SChristophe Leroy for (i = 0; i < HPTES_PER_GROUP; i++) { 226e66c3209SChristophe Leroy hptep = htab_address + hpte_group; 227e66c3209SChristophe Leroy hpte_v = be64_to_cpu(hptep->v); 228e66c3209SChristophe Leroy 229e66c3209SChristophe Leroy if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID)) { 230e66c3209SChristophe Leroy /* HPTE matches */ 231e66c3209SChristophe Leroy *v = be64_to_cpu(hptep->v); 232e66c3209SChristophe Leroy *r = be64_to_cpu(hptep->r); 233e66c3209SChristophe Leroy return 0; 234e66c3209SChristophe Leroy } 235e66c3209SChristophe Leroy ++hpte_group; 236e66c3209SChristophe Leroy } 237e66c3209SChristophe Leroy return -1; 238e66c3209SChristophe Leroy } 239e66c3209SChristophe Leroy 240e66c3209SChristophe Leroy static int pseries_find(unsigned long ea, int psize, bool primary, u64 *v, u64 *r) 241e66c3209SChristophe Leroy { 242e66c3209SChristophe Leroy struct hash_pte ptes[4]; 243e66c3209SChristophe Leroy unsigned long vsid, vpn, hash, hpte_group, want_v; 244e66c3209SChristophe Leroy int i, j, ssize = mmu_kernel_ssize; 245e66c3209SChristophe Leroy long lpar_rc = 0; 246e66c3209SChristophe Leroy unsigned long shift = mmu_psize_defs[psize].shift; 247e66c3209SChristophe Leroy 248e66c3209SChristophe Leroy /* calculate hash */ 249e66c3209SChristophe Leroy vsid = get_kernel_vsid(ea, ssize); 250e66c3209SChristophe Leroy vpn = hpt_vpn(ea, vsid, ssize); 251e66c3209SChristophe Leroy hash = hpt_hash(vpn, shift, ssize); 252e66c3209SChristophe Leroy want_v = hpte_encode_avpn(vpn, psize, ssize); 253e66c3209SChristophe Leroy 254e66c3209SChristophe Leroy /* to check in the secondary hash table, we invert the hash */ 255e66c3209SChristophe Leroy if (!primary) 256e66c3209SChristophe Leroy hash = ~hash; 257e66c3209SChristophe Leroy hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP; 258e66c3209SChristophe Leroy /* see if we can find an entry in the hpte with this hash */ 259e66c3209SChristophe Leroy for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) { 260e66c3209SChristophe Leroy lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes); 261e66c3209SChristophe Leroy 262e66c3209SChristophe Leroy if (lpar_rc != H_SUCCESS) 263e66c3209SChristophe Leroy continue; 264e66c3209SChristophe Leroy for (j = 0; j < 4; j++) { 265e66c3209SChristophe Leroy if (HPTE_V_COMPARE(ptes[j].v, want_v) && 266e66c3209SChristophe Leroy (ptes[j].v & HPTE_V_VALID)) { 267e66c3209SChristophe Leroy /* HPTE matches */ 268e66c3209SChristophe Leroy *v = ptes[j].v; 269e66c3209SChristophe Leroy *r = ptes[j].r; 270e66c3209SChristophe Leroy return 0; 271e66c3209SChristophe Leroy } 272e66c3209SChristophe Leroy } 273e66c3209SChristophe Leroy } 274e66c3209SChristophe Leroy return -1; 275e66c3209SChristophe Leroy } 276e66c3209SChristophe Leroy 277e66c3209SChristophe Leroy static void decode_r(int bps, unsigned long r, unsigned long *rpn, int *aps, 278e66c3209SChristophe Leroy unsigned long *lp_bits) 279e66c3209SChristophe Leroy { 280e66c3209SChristophe Leroy struct mmu_psize_def entry; 281e66c3209SChristophe Leroy unsigned long arpn, mask, lp; 282e66c3209SChristophe Leroy int penc = -2, idx = 0, shift; 283e66c3209SChristophe Leroy 284e66c3209SChristophe Leroy /*. 285e66c3209SChristophe Leroy * The LP field has 8 bits. Depending on the actual page size, some of 286e66c3209SChristophe Leroy * these bits are concatenated with the APRN to get the RPN. The rest 287e66c3209SChristophe Leroy * of the bits in the LP field is the LP value and is an encoding for 288e66c3209SChristophe Leroy * the base page size and the actual page size. 289e66c3209SChristophe Leroy * 290e66c3209SChristophe Leroy * - find the mmu entry for our base page size 291e66c3209SChristophe Leroy * - go through all page encodings and use the associated mask to 292e66c3209SChristophe Leroy * find an encoding that matches our encoding in the LP field. 293e66c3209SChristophe Leroy */ 294e66c3209SChristophe Leroy arpn = (r & HPTE_R_RPN) >> HPTE_R_RPN_SHIFT; 295e66c3209SChristophe Leroy lp = arpn & 0xff; 296e66c3209SChristophe Leroy 297e66c3209SChristophe Leroy entry = mmu_psize_defs[bps]; 298e66c3209SChristophe Leroy while (idx < MMU_PAGE_COUNT) { 299e66c3209SChristophe Leroy penc = entry.penc[idx]; 300e66c3209SChristophe Leroy if ((penc != -1) && (mmu_psize_defs[idx].shift)) { 301e66c3209SChristophe Leroy shift = mmu_psize_defs[idx].shift - HPTE_R_RPN_SHIFT; 302e66c3209SChristophe Leroy mask = (0x1 << (shift)) - 1; 303e66c3209SChristophe Leroy if ((lp & mask) == penc) { 304e66c3209SChristophe Leroy *aps = mmu_psize_to_shift(idx); 305e66c3209SChristophe Leroy *lp_bits = lp & mask; 306e66c3209SChristophe Leroy *rpn = arpn >> shift; 307e66c3209SChristophe Leroy return; 308e66c3209SChristophe Leroy } 309e66c3209SChristophe Leroy } 310e66c3209SChristophe Leroy idx++; 311e66c3209SChristophe Leroy } 312e66c3209SChristophe Leroy } 313e66c3209SChristophe Leroy 314e66c3209SChristophe Leroy static int base_hpte_find(unsigned long ea, int psize, bool primary, u64 *v, 315e66c3209SChristophe Leroy u64 *r) 316e66c3209SChristophe Leroy { 317*65e701b2SChristophe Leroy if (IS_ENABLED(CONFIG_PPC_PSERIES) && firmware_has_feature(FW_FEATURE_LPAR)) 318e66c3209SChristophe Leroy return pseries_find(ea, psize, primary, v, r); 319*65e701b2SChristophe Leroy 320e66c3209SChristophe Leroy return native_find(ea, psize, primary, v, r); 321e66c3209SChristophe Leroy } 322e66c3209SChristophe Leroy 323e66c3209SChristophe Leroy static unsigned long hpte_find(struct pg_state *st, unsigned long ea, int psize) 324e66c3209SChristophe Leroy { 325e66c3209SChristophe Leroy unsigned long slot; 326e66c3209SChristophe Leroy u64 v = 0, r = 0; 327e66c3209SChristophe Leroy unsigned long rpn, lp_bits; 328e66c3209SChristophe Leroy int base_psize = 0, actual_psize = 0; 329e66c3209SChristophe Leroy 330e66c3209SChristophe Leroy if (ea < PAGE_OFFSET) 331e66c3209SChristophe Leroy return -1; 332e66c3209SChristophe Leroy 333e66c3209SChristophe Leroy /* Look in primary table */ 334e66c3209SChristophe Leroy slot = base_hpte_find(ea, psize, true, &v, &r); 335e66c3209SChristophe Leroy 336e66c3209SChristophe Leroy /* Look in secondary table */ 337e66c3209SChristophe Leroy if (slot == -1) 338790845e2SRashmica Gupta slot = base_hpte_find(ea, psize, false, &v, &r); 339e66c3209SChristophe Leroy 340e66c3209SChristophe Leroy /* No entry found */ 341e66c3209SChristophe Leroy if (slot == -1) 342e66c3209SChristophe Leroy return -1; 343e66c3209SChristophe Leroy 344e66c3209SChristophe Leroy /* 345e66c3209SChristophe Leroy * We found an entry in the hash page table: 346e66c3209SChristophe Leroy * - check that this has the same base page 347e66c3209SChristophe Leroy * - find the actual page size 348e66c3209SChristophe Leroy * - find the RPN 349e66c3209SChristophe Leroy */ 350e66c3209SChristophe Leroy base_psize = mmu_psize_to_shift(psize); 351e66c3209SChristophe Leroy 352e66c3209SChristophe Leroy if ((v & HPTE_V_LARGE) == HPTE_V_LARGE) { 353e66c3209SChristophe Leroy decode_r(psize, r, &rpn, &actual_psize, &lp_bits); 354e66c3209SChristophe Leroy } else { 355e66c3209SChristophe Leroy /* 4K actual page size */ 356e66c3209SChristophe Leroy actual_psize = 12; 357e66c3209SChristophe Leroy rpn = (r & HPTE_R_RPN) >> HPTE_R_RPN_SHIFT; 358e66c3209SChristophe Leroy /* In this case there are no LP bits */ 359e66c3209SChristophe Leroy lp_bits = -1; 360e66c3209SChristophe Leroy } 361e66c3209SChristophe Leroy /* 362e66c3209SChristophe Leroy * We didn't find a matching encoding, so the PTE we found isn't for 363e66c3209SChristophe Leroy * this address. 364e66c3209SChristophe Leroy */ 365e66c3209SChristophe Leroy if (actual_psize == -1) 366e66c3209SChristophe Leroy return -1; 367e66c3209SChristophe Leroy 368e66c3209SChristophe Leroy dump_hpte_info(st, ea, v, r, rpn, base_psize, actual_psize, lp_bits); 369e66c3209SChristophe Leroy return 0; 370e66c3209SChristophe Leroy } 371e66c3209SChristophe Leroy 372e66c3209SChristophe Leroy static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start) 373e66c3209SChristophe Leroy { 374e66c3209SChristophe Leroy pte_t *pte = pte_offset_kernel(pmd, 0); 375e66c3209SChristophe Leroy unsigned long addr, pteval, psize; 376e66c3209SChristophe Leroy int i, status; 377e66c3209SChristophe Leroy 378e66c3209SChristophe Leroy for (i = 0; i < PTRS_PER_PTE; i++, pte++) { 379e66c3209SChristophe Leroy addr = start + i * PAGE_SIZE; 380e66c3209SChristophe Leroy pteval = pte_val(*pte); 381e66c3209SChristophe Leroy 382e66c3209SChristophe Leroy if (addr < VMALLOC_END) 383e66c3209SChristophe Leroy psize = mmu_vmalloc_psize; 384e66c3209SChristophe Leroy else 385e66c3209SChristophe Leroy psize = mmu_io_psize; 386*65e701b2SChristophe Leroy 387e66c3209SChristophe Leroy /* check for secret 4K mappings */ 388*65e701b2SChristophe Leroy if (IS_ENABLED(CONFIG_PPC_64K_PAGES) && 389*65e701b2SChristophe Leroy ((pteval & H_PAGE_COMBO) == H_PAGE_COMBO || 390*65e701b2SChristophe Leroy (pteval & H_PAGE_4K_PFN) == H_PAGE_4K_PFN)) 391e66c3209SChristophe Leroy psize = mmu_io_psize; 392*65e701b2SChristophe Leroy 393e66c3209SChristophe Leroy /* check for hashpte */ 394e66c3209SChristophe Leroy status = hpte_find(st, addr, psize); 395e66c3209SChristophe Leroy 396e66c3209SChristophe Leroy if (((pteval & H_PAGE_HASHPTE) != H_PAGE_HASHPTE) 397e66c3209SChristophe Leroy && (status != -1)) { 398e66c3209SChristophe Leroy /* found a hpte that is not in the linux page tables */ 399e66c3209SChristophe Leroy seq_printf(st->seq, "page probably bolted before linux" 400e66c3209SChristophe Leroy " pagetables were set: addr:%lx, pteval:%lx\n", 401e66c3209SChristophe Leroy addr, pteval); 402e66c3209SChristophe Leroy } 403e66c3209SChristophe Leroy } 404e66c3209SChristophe Leroy } 405e66c3209SChristophe Leroy 406e66c3209SChristophe Leroy static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start) 407e66c3209SChristophe Leroy { 408e66c3209SChristophe Leroy pmd_t *pmd = pmd_offset(pud, 0); 409e66c3209SChristophe Leroy unsigned long addr; 410e66c3209SChristophe Leroy unsigned int i; 411e66c3209SChristophe Leroy 412e66c3209SChristophe Leroy for (i = 0; i < PTRS_PER_PMD; i++, pmd++) { 413e66c3209SChristophe Leroy addr = start + i * PMD_SIZE; 414e66c3209SChristophe Leroy if (!pmd_none(*pmd)) 415e66c3209SChristophe Leroy /* pmd exists */ 416e66c3209SChristophe Leroy walk_pte(st, pmd, addr); 417e66c3209SChristophe Leroy } 418e66c3209SChristophe Leroy } 419e66c3209SChristophe Leroy 420e66c3209SChristophe Leroy static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start) 421e66c3209SChristophe Leroy { 422e66c3209SChristophe Leroy pud_t *pud = pud_offset(pgd, 0); 423e66c3209SChristophe Leroy unsigned long addr; 424e66c3209SChristophe Leroy unsigned int i; 425e66c3209SChristophe Leroy 426e66c3209SChristophe Leroy for (i = 0; i < PTRS_PER_PUD; i++, pud++) { 427e66c3209SChristophe Leroy addr = start + i * PUD_SIZE; 428e66c3209SChristophe Leroy if (!pud_none(*pud)) 429e66c3209SChristophe Leroy /* pud exists */ 430e66c3209SChristophe Leroy walk_pmd(st, pud, addr); 431e66c3209SChristophe Leroy } 432e66c3209SChristophe Leroy } 433e66c3209SChristophe Leroy 434e66c3209SChristophe Leroy static void walk_pagetables(struct pg_state *st) 435e66c3209SChristophe Leroy { 436e66c3209SChristophe Leroy pgd_t *pgd = pgd_offset_k(0UL); 437e66c3209SChristophe Leroy unsigned int i; 438e66c3209SChristophe Leroy unsigned long addr; 439e66c3209SChristophe Leroy 440e66c3209SChristophe Leroy /* 441e66c3209SChristophe Leroy * Traverse the linux pagetable structure and dump pages that are in 442e66c3209SChristophe Leroy * the hash pagetable. 443e66c3209SChristophe Leroy */ 444e66c3209SChristophe Leroy for (i = 0; i < PTRS_PER_PGD; i++, pgd++) { 445e66c3209SChristophe Leroy addr = KERN_VIRT_START + i * PGDIR_SIZE; 446e66c3209SChristophe Leroy if (!pgd_none(*pgd)) 447e66c3209SChristophe Leroy /* pgd exists */ 448e66c3209SChristophe Leroy walk_pud(st, pgd, addr); 449e66c3209SChristophe Leroy } 450e66c3209SChristophe Leroy } 451e66c3209SChristophe Leroy 452e66c3209SChristophe Leroy 453e66c3209SChristophe Leroy static void walk_linearmapping(struct pg_state *st) 454e66c3209SChristophe Leroy { 455e66c3209SChristophe Leroy unsigned long addr; 456e66c3209SChristophe Leroy 457e66c3209SChristophe Leroy /* 458e66c3209SChristophe Leroy * Traverse the linear mapping section of virtual memory and dump pages 459e66c3209SChristophe Leroy * that are in the hash pagetable. 460e66c3209SChristophe Leroy */ 461e66c3209SChristophe Leroy unsigned long psize = 1 << mmu_psize_defs[mmu_linear_psize].shift; 462e66c3209SChristophe Leroy 463e66c3209SChristophe Leroy for (addr = PAGE_OFFSET; addr < PAGE_OFFSET + 464e66c3209SChristophe Leroy memblock_end_of_DRAM(); addr += psize) 465e66c3209SChristophe Leroy hpte_find(st, addr, mmu_linear_psize); 466e66c3209SChristophe Leroy } 467e66c3209SChristophe Leroy 468e66c3209SChristophe Leroy static void walk_vmemmap(struct pg_state *st) 469e66c3209SChristophe Leroy { 470e66c3209SChristophe Leroy struct vmemmap_backing *ptr = vmemmap_list; 471e66c3209SChristophe Leroy 472*65e701b2SChristophe Leroy if (!IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) 473*65e701b2SChristophe Leroy return; 474e66c3209SChristophe Leroy /* 475e66c3209SChristophe Leroy * Traverse the vmemmaped memory and dump pages that are in the hash 476e66c3209SChristophe Leroy * pagetable. 477e66c3209SChristophe Leroy */ 478e66c3209SChristophe Leroy while (ptr->list) { 479e66c3209SChristophe Leroy hpte_find(st, ptr->virt_addr, mmu_vmemmap_psize); 480e66c3209SChristophe Leroy ptr = ptr->list; 481e66c3209SChristophe Leroy } 482e66c3209SChristophe Leroy seq_puts(st->seq, "---[ vmemmap end ]---\n"); 483e66c3209SChristophe Leroy } 484e66c3209SChristophe Leroy 485e66c3209SChristophe Leroy static void populate_markers(void) 486e66c3209SChristophe Leroy { 487e66c3209SChristophe Leroy address_markers[0].start_address = PAGE_OFFSET; 488e66c3209SChristophe Leroy address_markers[1].start_address = VMALLOC_START; 489e66c3209SChristophe Leroy address_markers[2].start_address = VMALLOC_END; 490e66c3209SChristophe Leroy address_markers[3].start_address = ISA_IO_BASE; 491e66c3209SChristophe Leroy address_markers[4].start_address = ISA_IO_END; 492e66c3209SChristophe Leroy address_markers[5].start_address = PHB_IO_BASE; 493e66c3209SChristophe Leroy address_markers[6].start_address = PHB_IO_END; 494e66c3209SChristophe Leroy address_markers[7].start_address = IOREMAP_BASE; 495e66c3209SChristophe Leroy address_markers[8].start_address = IOREMAP_END; 4960034d395SAneesh Kumar K.V address_markers[9].start_address = H_VMEMMAP_START; 497e66c3209SChristophe Leroy } 498e66c3209SChristophe Leroy 499e66c3209SChristophe Leroy static int ptdump_show(struct seq_file *m, void *v) 500e66c3209SChristophe Leroy { 501e66c3209SChristophe Leroy struct pg_state st = { 502e66c3209SChristophe Leroy .seq = m, 503e66c3209SChristophe Leroy .start_address = PAGE_OFFSET, 504e66c3209SChristophe Leroy .marker = address_markers, 505e66c3209SChristophe Leroy }; 506e66c3209SChristophe Leroy /* 507e66c3209SChristophe Leroy * Traverse the 0xc, 0xd and 0xf areas of the kernel virtual memory and 508e66c3209SChristophe Leroy * dump pages that are in the hash pagetable. 509e66c3209SChristophe Leroy */ 510e66c3209SChristophe Leroy walk_linearmapping(&st); 511e66c3209SChristophe Leroy walk_pagetables(&st); 512e66c3209SChristophe Leroy walk_vmemmap(&st); 513e66c3209SChristophe Leroy return 0; 514e66c3209SChristophe Leroy } 515e66c3209SChristophe Leroy 516e66c3209SChristophe Leroy static int ptdump_open(struct inode *inode, struct file *file) 517e66c3209SChristophe Leroy { 518e66c3209SChristophe Leroy return single_open(file, ptdump_show, NULL); 519e66c3209SChristophe Leroy } 520e66c3209SChristophe Leroy 521e66c3209SChristophe Leroy static const struct file_operations ptdump_fops = { 522e66c3209SChristophe Leroy .open = ptdump_open, 523e66c3209SChristophe Leroy .read = seq_read, 524e66c3209SChristophe Leroy .llseek = seq_lseek, 525e66c3209SChristophe Leroy .release = single_release, 526e66c3209SChristophe Leroy }; 527e66c3209SChristophe Leroy 528e66c3209SChristophe Leroy static int ptdump_init(void) 529e66c3209SChristophe Leroy { 530e66c3209SChristophe Leroy struct dentry *debugfs_file; 531e66c3209SChristophe Leroy 532e66c3209SChristophe Leroy if (!radix_enabled()) { 533e66c3209SChristophe Leroy populate_markers(); 534e66c3209SChristophe Leroy debugfs_file = debugfs_create_file("kernel_hash_pagetable", 535e66c3209SChristophe Leroy 0400, NULL, NULL, &ptdump_fops); 536e66c3209SChristophe Leroy return debugfs_file ? 0 : -ENOMEM; 537e66c3209SChristophe Leroy } 538e66c3209SChristophe Leroy return 0; 539e66c3209SChristophe Leroy } 540e66c3209SChristophe Leroy device_initcall(ptdump_init); 541