Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright 2016, Rashmica Gupta, IBM Corp.
0004  *
0005  * This traverses the kernel virtual memory and dumps the pages that are in
0006  * the hash pagetable, along with their flags to
0007  * /sys/kernel/debug/kernel_hash_pagetable.
0008  *
0009  * If radix is enabled then there is no hash page table and so no debugfs file
0010  * is generated.
0011  */
0012 #include <linux/debugfs.h>
0013 #include <linux/fs.h>
0014 #include <linux/io.h>
0015 #include <linux/mm.h>
0016 #include <linux/sched.h>
0017 #include <linux/seq_file.h>
0018 #include <linux/const.h>
0019 #include <asm/page.h>
0020 #include <asm/plpar_wrappers.h>
0021 #include <linux/memblock.h>
0022 #include <asm/firmware.h>
0023 #include <asm/pgalloc.h>
0024 
0025 struct pg_state {
0026     struct seq_file *seq;
0027     const struct addr_marker *marker;
0028     unsigned long start_address;
0029     unsigned int level;
0030     u64 current_flags;
0031 };
0032 
0033 struct addr_marker {
0034     unsigned long start_address;
0035     const char *name;
0036 };
0037 
0038 static struct addr_marker address_markers[] = {
0039     { 0,    "Start of kernel VM" },
0040     { 0,    "vmalloc() Area" },
0041     { 0,    "vmalloc() End" },
0042     { 0,    "isa I/O start" },
0043     { 0,    "isa I/O end" },
0044     { 0,    "phb I/O start" },
0045     { 0,    "phb I/O end" },
0046     { 0,    "I/O remap start" },
0047     { 0,    "I/O remap end" },
0048     { 0,    "vmemmap start" },
0049     { -1,   NULL },
0050 };
0051 
0052 struct flag_info {
0053     u64     mask;
0054     u64     val;
0055     const char  *set;
0056     const char  *clear;
0057     bool        is_val;
0058     int     shift;
0059 };
0060 
0061 static const struct flag_info v_flag_array[] = {
0062     {
0063         .mask   = SLB_VSID_B,
0064         .val    = SLB_VSID_B_256M,
0065         .set    = "ssize: 256M",
0066         .clear  = "ssize: 1T  ",
0067     }, {
0068         .mask   = HPTE_V_SECONDARY,
0069         .val    = HPTE_V_SECONDARY,
0070         .set    = "secondary",
0071         .clear  = "primary  ",
0072     }, {
0073         .mask   = HPTE_V_VALID,
0074         .val    = HPTE_V_VALID,
0075         .set    = "valid  ",
0076         .clear  = "invalid",
0077     }, {
0078         .mask   = HPTE_V_BOLTED,
0079         .val    = HPTE_V_BOLTED,
0080         .set    = "bolted",
0081         .clear  = "",
0082     }
0083 };
0084 
0085 static const struct flag_info r_flag_array[] = {
0086     {
0087         .mask   = HPTE_R_PP0 | HPTE_R_PP,
0088         .val    = PP_RWXX,
0089         .set    = "prot:RW--",
0090     }, {
0091         .mask   = HPTE_R_PP0 | HPTE_R_PP,
0092         .val    = PP_RWRX,
0093         .set    = "prot:RWR-",
0094     }, {
0095         .mask   = HPTE_R_PP0 | HPTE_R_PP,
0096         .val    = PP_RWRW,
0097         .set    = "prot:RWRW",
0098     }, {
0099         .mask   = HPTE_R_PP0 | HPTE_R_PP,
0100         .val    = PP_RXRX,
0101         .set    = "prot:R-R-",
0102     }, {
0103         .mask   = HPTE_R_PP0 | HPTE_R_PP,
0104         .val    = PP_RXXX,
0105         .set    = "prot:R---",
0106     }, {
0107         .mask   = HPTE_R_KEY_HI | HPTE_R_KEY_LO,
0108         .val    = HPTE_R_KEY_HI | HPTE_R_KEY_LO,
0109         .set    = "key",
0110         .clear  = "",
0111         .is_val = true,
0112     }, {
0113         .mask   = HPTE_R_R,
0114         .val    = HPTE_R_R,
0115         .set    = "ref",
0116         .clear  = "   ",
0117     }, {
0118         .mask   = HPTE_R_C,
0119         .val    = HPTE_R_C,
0120         .set    = "changed",
0121         .clear  = "       ",
0122     }, {
0123         .mask   = HPTE_R_N,
0124         .val    = HPTE_R_N,
0125         .set    = "no execute",
0126     }, {
0127         .mask   = HPTE_R_WIMG,
0128         .val    = HPTE_R_W,
0129         .set    = "writethru",
0130     }, {
0131         .mask   = HPTE_R_WIMG,
0132         .val    = HPTE_R_I,
0133         .set    = "no cache",
0134     }, {
0135         .mask   = HPTE_R_WIMG,
0136         .val    = HPTE_R_G,
0137         .set    = "guarded",
0138     }
0139 };
0140 
0141 static int calculate_pagesize(struct pg_state *st, int ps, char s[])
0142 {
0143     static const char units[] = "BKMGTPE";
0144     const char *unit = units;
0145 
0146     while (ps > 9 && unit[1]) {
0147         ps -= 10;
0148         unit++;
0149     }
0150     seq_printf(st->seq, "  %s_ps: %i%c\t", s, 1<<ps, *unit);
0151     return ps;
0152 }
0153 
0154 static void dump_flag_info(struct pg_state *st, const struct flag_info
0155         *flag, u64 pte, int num)
0156 {
0157     unsigned int i;
0158 
0159     for (i = 0; i < num; i++, flag++) {
0160         const char *s = NULL;
0161         u64 val;
0162 
0163         /* flag not defined so don't check it */
0164         if (flag->mask == 0)
0165             continue;
0166         /* Some 'flags' are actually values */
0167         if (flag->is_val) {
0168             val = pte & flag->val;
0169             if (flag->shift)
0170                 val = val >> flag->shift;
0171             seq_printf(st->seq, "  %s:%llx", flag->set, val);
0172         } else {
0173             if ((pte & flag->mask) == flag->val)
0174                 s = flag->set;
0175             else
0176                 s = flag->clear;
0177             if (s)
0178                 seq_printf(st->seq, "  %s", s);
0179         }
0180     }
0181 }
0182 
0183 static void dump_hpte_info(struct pg_state *st, unsigned long ea, u64 v, u64 r,
0184         unsigned long rpn, int bps, int aps, unsigned long lp)
0185 {
0186     int aps_index;
0187 
0188     while (ea >= st->marker[1].start_address) {
0189         st->marker++;
0190         seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
0191     }
0192     seq_printf(st->seq, "0x%lx:\t", ea);
0193     seq_printf(st->seq, "AVPN:%llx\t", HPTE_V_AVPN_VAL(v));
0194     dump_flag_info(st, v_flag_array, v, ARRAY_SIZE(v_flag_array));
0195     seq_printf(st->seq, "  rpn: %lx\t", rpn);
0196     dump_flag_info(st, r_flag_array, r, ARRAY_SIZE(r_flag_array));
0197 
0198     calculate_pagesize(st, bps, "base");
0199     aps_index = calculate_pagesize(st, aps, "actual");
0200     if (aps_index != 2)
0201         seq_printf(st->seq, "LP enc: %lx", lp);
0202     seq_putc(st->seq, '\n');
0203 }
0204 
0205 
0206 static int native_find(unsigned long ea, int psize, bool primary, u64 *v, u64
0207         *r)
0208 {
0209     struct hash_pte *hptep;
0210     unsigned long hash, vsid, vpn, hpte_group, want_v, hpte_v;
0211     int i, ssize = mmu_kernel_ssize;
0212     unsigned long shift = mmu_psize_defs[psize].shift;
0213 
0214     /* calculate hash */
0215     vsid = get_kernel_vsid(ea, ssize);
0216     vpn  = hpt_vpn(ea, vsid, ssize);
0217     hash = hpt_hash(vpn, shift, ssize);
0218     want_v = hpte_encode_avpn(vpn, psize, ssize);
0219 
0220     /* to check in the secondary hash table, we invert the hash */
0221     if (!primary)
0222         hash = ~hash;
0223     hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
0224     for (i = 0; i < HPTES_PER_GROUP; i++) {
0225         hptep = htab_address + hpte_group;
0226         hpte_v = be64_to_cpu(hptep->v);
0227 
0228         if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID)) {
0229             /* HPTE matches */
0230             *v = be64_to_cpu(hptep->v);
0231             *r = be64_to_cpu(hptep->r);
0232             return 0;
0233         }
0234         ++hpte_group;
0235     }
0236     return -1;
0237 }
0238 
0239 static int pseries_find(unsigned long ea, int psize, bool primary, u64 *v, u64 *r)
0240 {
0241     struct {
0242         unsigned long v;
0243         unsigned long r;
0244     } ptes[4];
0245     unsigned long vsid, vpn, hash, hpte_group, want_v;
0246     int i, j, ssize = mmu_kernel_ssize;
0247     long lpar_rc = 0;
0248     unsigned long shift = mmu_psize_defs[psize].shift;
0249 
0250     /* calculate hash */
0251     vsid = get_kernel_vsid(ea, ssize);
0252     vpn  = hpt_vpn(ea, vsid, ssize);
0253     hash = hpt_hash(vpn, shift, ssize);
0254     want_v = hpte_encode_avpn(vpn, psize, ssize);
0255 
0256     /* to check in the secondary hash table, we invert the hash */
0257     if (!primary)
0258         hash = ~hash;
0259     hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
0260     /* see if we can find an entry in the hpte with this hash */
0261     for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) {
0262         lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes);
0263 
0264         if (lpar_rc)
0265             continue;
0266         for (j = 0; j < 4; j++) {
0267             if (HPTE_V_COMPARE(ptes[j].v, want_v) &&
0268                     (ptes[j].v & HPTE_V_VALID)) {
0269                 /* HPTE matches */
0270                 *v = ptes[j].v;
0271                 *r = ptes[j].r;
0272                 return 0;
0273             }
0274         }
0275     }
0276     return -1;
0277 }
0278 
0279 static void decode_r(int bps, unsigned long r, unsigned long *rpn, int *aps,
0280         unsigned long *lp_bits)
0281 {
0282     struct mmu_psize_def entry;
0283     unsigned long arpn, mask, lp;
0284     int penc = -2, idx = 0, shift;
0285 
0286     /*.
0287      * The LP field has 8 bits. Depending on the actual page size, some of
0288      * these bits are concatenated with the APRN to get the RPN. The rest
0289      * of the bits in the LP field is the LP value and is an encoding for
0290      * the base page size and the actual page size.
0291      *
0292      *  -   find the mmu entry for our base page size
0293      *  -   go through all page encodings and use the associated mask to
0294      *  find an encoding that matches our encoding in the LP field.
0295      */
0296     arpn = (r & HPTE_R_RPN) >> HPTE_R_RPN_SHIFT;
0297     lp = arpn & 0xff;
0298 
0299     entry = mmu_psize_defs[bps];
0300     while (idx < MMU_PAGE_COUNT) {
0301         penc = entry.penc[idx];
0302         if ((penc != -1) && (mmu_psize_defs[idx].shift)) {
0303             shift = mmu_psize_defs[idx].shift -  HPTE_R_RPN_SHIFT;
0304             mask = (0x1 << (shift)) - 1;
0305             if ((lp & mask) == penc) {
0306                 *aps = mmu_psize_to_shift(idx);
0307                 *lp_bits = lp & mask;
0308                 *rpn = arpn >> shift;
0309                 return;
0310             }
0311         }
0312         idx++;
0313     }
0314 }
0315 
0316 static int base_hpte_find(unsigned long ea, int psize, bool primary, u64 *v,
0317               u64 *r)
0318 {
0319     if (IS_ENABLED(CONFIG_PPC_PSERIES) && firmware_has_feature(FW_FEATURE_LPAR))
0320         return pseries_find(ea, psize, primary, v, r);
0321 
0322     return native_find(ea, psize, primary, v, r);
0323 }
0324 
0325 static unsigned long hpte_find(struct pg_state *st, unsigned long ea, int psize)
0326 {
0327     unsigned long slot;
0328     u64 v  = 0, r = 0;
0329     unsigned long rpn, lp_bits;
0330     int base_psize = 0, actual_psize = 0;
0331 
0332     if (ea < PAGE_OFFSET)
0333         return -1;
0334 
0335     /* Look in primary table */
0336     slot = base_hpte_find(ea, psize, true, &v, &r);
0337 
0338     /* Look in secondary table */
0339     if (slot == -1)
0340         slot = base_hpte_find(ea, psize, false, &v, &r);
0341 
0342     /* No entry found */
0343     if (slot == -1)
0344         return -1;
0345 
0346     /*
0347      * We found an entry in the hash page table:
0348      *  - check that this has the same base page
0349      *  - find the actual page size
0350      *  - find the RPN
0351      */
0352     base_psize = mmu_psize_to_shift(psize);
0353 
0354     if ((v & HPTE_V_LARGE) == HPTE_V_LARGE) {
0355         decode_r(psize, r, &rpn, &actual_psize, &lp_bits);
0356     } else {
0357         /* 4K actual page size */
0358         actual_psize = 12;
0359         rpn = (r & HPTE_R_RPN) >> HPTE_R_RPN_SHIFT;
0360         /* In this case there are no LP bits */
0361         lp_bits = -1;
0362     }
0363     /*
0364      * We didn't find a matching encoding, so the PTE we found isn't for
0365      * this address.
0366      */
0367     if (actual_psize == -1)
0368         return -1;
0369 
0370     dump_hpte_info(st, ea, v, r, rpn, base_psize, actual_psize, lp_bits);
0371     return 0;
0372 }
0373 
0374 static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
0375 {
0376     pte_t *pte = pte_offset_kernel(pmd, 0);
0377     unsigned long addr, pteval, psize;
0378     int i, status;
0379 
0380     for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
0381         addr = start + i * PAGE_SIZE;
0382         pteval = pte_val(*pte);
0383 
0384         if (addr < VMALLOC_END)
0385             psize = mmu_vmalloc_psize;
0386         else
0387             psize = mmu_io_psize;
0388 
0389         /* check for secret 4K mappings */
0390         if (IS_ENABLED(CONFIG_PPC_64K_PAGES) &&
0391             ((pteval & H_PAGE_COMBO) == H_PAGE_COMBO ||
0392              (pteval & H_PAGE_4K_PFN) == H_PAGE_4K_PFN))
0393             psize = mmu_io_psize;
0394 
0395         /* check for hashpte */
0396         status = hpte_find(st, addr, psize);
0397 
0398         if (((pteval & H_PAGE_HASHPTE) != H_PAGE_HASHPTE)
0399                 && (status != -1)) {
0400         /* found a hpte that is not in the linux page tables */
0401             seq_printf(st->seq, "page probably bolted before linux"
0402                 " pagetables were set: addr:%lx, pteval:%lx\n",
0403                 addr, pteval);
0404         }
0405     }
0406 }
0407 
0408 static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
0409 {
0410     pmd_t *pmd = pmd_offset(pud, 0);
0411     unsigned long addr;
0412     unsigned int i;
0413 
0414     for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
0415         addr = start + i * PMD_SIZE;
0416         if (!pmd_none(*pmd))
0417             /* pmd exists */
0418             walk_pte(st, pmd, addr);
0419     }
0420 }
0421 
0422 static void walk_pud(struct pg_state *st, p4d_t *p4d, unsigned long start)
0423 {
0424     pud_t *pud = pud_offset(p4d, 0);
0425     unsigned long addr;
0426     unsigned int i;
0427 
0428     for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
0429         addr = start + i * PUD_SIZE;
0430         if (!pud_none(*pud))
0431             /* pud exists */
0432             walk_pmd(st, pud, addr);
0433     }
0434 }
0435 
0436 static void walk_p4d(struct pg_state *st, pgd_t *pgd, unsigned long start)
0437 {
0438     p4d_t *p4d = p4d_offset(pgd, 0);
0439     unsigned long addr;
0440     unsigned int i;
0441 
0442     for (i = 0; i < PTRS_PER_P4D; i++, p4d++) {
0443         addr = start + i * P4D_SIZE;
0444         if (!p4d_none(*p4d))
0445             /* p4d exists */
0446             walk_pud(st, p4d, addr);
0447     }
0448 }
0449 
0450 static void walk_pagetables(struct pg_state *st)
0451 {
0452     pgd_t *pgd = pgd_offset_k(0UL);
0453     unsigned int i;
0454     unsigned long addr;
0455 
0456     /*
0457      * Traverse the linux pagetable structure and dump pages that are in
0458      * the hash pagetable.
0459      */
0460     for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
0461         addr = KERN_VIRT_START + i * PGDIR_SIZE;
0462         if (!pgd_none(*pgd))
0463             /* pgd exists */
0464             walk_p4d(st, pgd, addr);
0465     }
0466 }
0467 
0468 
0469 static void walk_linearmapping(struct pg_state *st)
0470 {
0471     unsigned long addr;
0472 
0473     /*
0474      * Traverse the linear mapping section of virtual memory and dump pages
0475      * that are in the hash pagetable.
0476      */
0477     unsigned long psize = 1 << mmu_psize_defs[mmu_linear_psize].shift;
0478 
0479     for (addr = PAGE_OFFSET; addr < PAGE_OFFSET +
0480             memblock_end_of_DRAM(); addr += psize)
0481         hpte_find(st, addr, mmu_linear_psize);
0482 }
0483 
0484 static void walk_vmemmap(struct pg_state *st)
0485 {
0486     struct vmemmap_backing *ptr = vmemmap_list;
0487 
0488     if (!IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP))
0489         return;
0490     /*
0491      * Traverse the vmemmaped memory and dump pages that are in the hash
0492      * pagetable.
0493      */
0494     while (ptr->list) {
0495         hpte_find(st, ptr->virt_addr, mmu_vmemmap_psize);
0496         ptr = ptr->list;
0497     }
0498     seq_puts(st->seq, "---[ vmemmap end ]---\n");
0499 }
0500 
0501 static void populate_markers(void)
0502 {
0503     address_markers[0].start_address = PAGE_OFFSET;
0504     address_markers[1].start_address = VMALLOC_START;
0505     address_markers[2].start_address = VMALLOC_END;
0506     address_markers[3].start_address = ISA_IO_BASE;
0507     address_markers[4].start_address = ISA_IO_END;
0508     address_markers[5].start_address = PHB_IO_BASE;
0509     address_markers[6].start_address = PHB_IO_END;
0510     address_markers[7].start_address = IOREMAP_BASE;
0511     address_markers[8].start_address = IOREMAP_END;
0512     address_markers[9].start_address =  H_VMEMMAP_START;
0513 }
0514 
0515 static int ptdump_show(struct seq_file *m, void *v)
0516 {
0517     struct pg_state st = {
0518         .seq = m,
0519         .start_address = PAGE_OFFSET,
0520         .marker = address_markers,
0521     };
0522     /*
0523      * Traverse the 0xc, 0xd and 0xf areas of the kernel virtual memory and
0524      * dump pages that are in the hash pagetable.
0525      */
0526     walk_linearmapping(&st);
0527     walk_pagetables(&st);
0528     walk_vmemmap(&st);
0529     return 0;
0530 }
0531 
0532 DEFINE_SHOW_ATTRIBUTE(ptdump);
0533 
0534 static int ptdump_init(void)
0535 {
0536     if (!radix_enabled()) {
0537         populate_markers();
0538         debugfs_create_file("kernel_hash_pagetable", 0400, NULL, NULL,
0539                     &ptdump_fops);
0540     }
0541     return 0;
0542 }
0543 device_initcall(ptdump_init);